Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'iio-for-6.10a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into char-misc-next

Jonathan writes:

IIO: 1st set of new device support, features and cleanup for 6.10

The Analog Device team (Paul Cercueil and Nuno Sa) have been working on
improving high speed device handling. They have had some support in their
own tree for many years, so it is great to see them bring it to upstream.
Some of that is seen here, with the first output device using the
IIO dmaengine infrastructure and a new DAC backend FPGA IP driver.
This makes use of a new set of interfaces to allow backend and
front end driver communication in a fashion that in theory at least
allows for a single driver for a given ADC / DAC independent of
the IP to which is being used to deal with the data bus and DMA aspects
of working with these devices. It is early days for this new
generic way of handling split devices, but as it's kernel internals only
we can merrily change anything about it as a wider diversity of devices
show up and we get a better feel for what works.

Alongside the usual set of new drivers and features we have
the automatic cleanup of fwnode_handle_put() which didn't quite make
it in last cycle. The equivalent DT version was merged by Rob Herring
via the DT binding tree and one patch using that in IIO can also be
found in this pull request. Rob has been making extensive use of that
infrastructure in the DT core which is good to see and provides more
evidence this basic approach is useful.

In some cases, the IIO driver was converted over from DT only to
using the generic firmware description handling of property.h
including using the new macros. The general preference for IIO
is to use this more generic handling where possible - a bunch of other
drivers have been converted this cycle as well.

New device support
==================

adi,ad7173
- New driver supporting AD7172-2, AD7172-4 AD7173-9, AD7175-2, AD7175-8,
AD7176-2 and AD7177-2 ADCs.
- Follow up fix for an accidental use of logic not instead of bitwise.

adi,ad7944
- New driver supporting AD7944, AD7985 and AD7986 pin compatible ADCs.
- Later patch added use of new spi_optimize_message() to reduce overheads
of setting up a reused message.
- Additional changes later in series reduced code duplication.

adi,ad9739a RF DAC
- New driver for this 14-bit 2.5 GSPS DAC via an LVDS interface.

adi,axi-dac
- Support for this FPGA IP used to send data to high performance DACs over
an interface such as JESD204B/C or parallel interfaces. Used in
conjunction with a DAC driver. The initial user is the ad9739a.
The dmaengine-buffer needed various changes to make it bidirectional.

avago,apds9306
- New driver for this ambient light sensor.
- Fix much later in this pull for an off by 1 error.

New device IDs
==============

For these at most an ID and a instance of chip specific data was needed.
Always nice to see manufacturers sticking to an existing software interface
for new parts.

allwinner,sun20i
- Add support for h616.
invensense,mpu6050
- Add support for ICM42688
maxim,max30102
- Add compatible for MAX30101
ti,dac5571
- Add compatible for DAC081C081

General
=======

fwnode_handle
- Support for cleanup.h based __free(fwnode_handle)
- Loop macro using this for looping over child nodes without needing to
call fwnode_handle_put() in ever early exit from the loop.
- Used in:
* adi,ad3552r
* adi,ad4130
* adi,ad5770r
* adi,ad74413r
* adi,ad7173
* adi,adfm2000
* linear,ltc2688
* linear,ltc2983
* maxim,max11410
* microchip,pac1934
* qcom,spmi-adc
* renesas,rz2gl
* st,ab8500
* st,stm32 (Fix for failure to set return value precedes this patch,
providing an example of why enabling direct returns makes bugs
less likely)
- Conversions to fwnode also using the cleanup logic
* adi,ad7124
* adi,ad7292
* freescale,fsl-imx25-gcq

- Other conversions to fwnode where the new cleanup handling isn't useful
* adi,ad7192
* avia,hx711
* freescale,mma8452
* nxp,fxls8962af
* st,spear
* ti,twl4030

Features
========

adi,adxl345
- Support SPI_3WIRE mode.

adi,ad9944
- Support 3-wire mode, note this isn't normal 3-wire SPI (unlike the
adxl345 change above), but rather a wiring scheme where the SPI
chip select is used to trigger conversions rather than using a
separate pin.
- Add some device specific documentation, mostly around the various wiring
schemes.

invensense,mpu6050
- Add Wake on Motion support as an IIO event and as a wake-up source.

linear,ltc2983
- Add vdd-supply.

ti,hdc3020
- Add power management using trigger on demand mode and adding suspend and
resume handling.
- Use reset GPIO if available.

Cleanup and fixes
================

iio core
- Use the various autocleanup and lock guards from cleanup.h to simplify
the IIO core.
- Don't set the pointer used for iio_priv() if it is zero sized as that
points beyond the end of the allocation. No driver actually uses it
in that case but good to clean this up.

various drivers
- Drop unnecessary casts of other pointer types to void *

docs
- Add missing ABI entry for in_temp_input.

adi,adx345
- General cleanup prior to adding spi-3wire mode.

adi,axi-adc
- Be more flexible and allow minor version changes as these are expected
to be backwards compatible.

avago,apds9300/9600
- Merge near identical bindings. The drivers are quite different, but
the bindings can be shared. The apds9306 binding introduced in this
series uses this shared binding doc as well.
- Add missing vdd-supply
- Update binding to use IRQ_TYPE_LEVEL_LOW instead of 8.

bosch,bmp280
- Organize headers

freescale,fxl-imx25-gcq
- Use devm_ for remaining probe() time setup allowing dropping
of specific error handling and remove() functions.

infineon,dps310
- Fix handling of negative temperatures
- Bring style of other similar calls inline with the form needed
for temperatures
- Ensure error handling of regmap calls is consistent within the driver.
- Simplify scale reading logic.

invensense,mpu6050
- Flip logic in binding to exclude devices without i2c-gate instead
of opting in. The list is expected to be much shorter as all recent
devices support this feature.

honeywell,hsc030pa
- Use spi_read() instead of opening coding.

renesas,rcar
- Use device_for_each_child_of_node_scoped() to remove need to manually
release. Left over from series the rest of which went in during 6.9.

st,ab8500
- Fix naming of function parameters in kernel-doc

* tag 'iio-for-6.10a' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (108 commits)
iio: adc: ti-ads131e08: Use device_for_each_child_node_scoped() to simplify error paths.
iio: adc: adi-axi-adc: only error out in major version mismatch
iio: dac: support the ad9739a RF DAC
iio: dac: add support for AXI DAC IP core
iio: backend: add new functionality
dt-bindings: iio: dac: add docs for AD9739A
dt-bindings: iio: dac: add docs for AXI DAC IP
iio: buffer-dmaengine: Enable write support
iio: buffer-dmaengine: Support specifying buffer direction
iio: buffer-dma: Enable buffer write support
iio: buffer-dma: Rename iio_dma_buffer_data_available()
iio: buffer-dma: add iio_dmaengine_buffer_setup()
iio: pressure: dps310: simplify scale factor reading
iio: pressure: dps310: consistently check return value of `regmap_read`
iio: pressure: dps310: introduce consistent error handling
iio: pressure: dps310: support negative temperature values
dt-bindings: iio: adc: Add GPADC for Allwinner H616
iio: dac: ad5755: make use of of_device_id table
iio: imu: inv_icm42600: add support of ICM-42688-P
dt-bindings: iio: imu: add icm42688 inside inv_icm42600
...

+6965 -1268
+2 -1
Documentation/ABI/testing/sysfs-bus-iio
··· 243 243 less measurements. Units after application of scale and offset 244 244 are milli degrees Celsius. 245 245 246 - What: /sys/bus/iio/devices/iio:deviceX/in_tempX_input 246 + What: /sys/bus/iio/devices/iio:deviceX/in_tempY_input 247 + What: /sys/bus/iio/devices/iio:deviceX/in_temp_input 247 248 KernelVersion: 2.6.38 248 249 Contact: linux-iio@vger.kernel.org 249 250 Description:
+19
Documentation/ABI/testing/sysfs-bus-iio-ad9739a
··· 1 + What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_operating_mode 2 + KernelVersion: 6.9 3 + Contact: linux-iio@vger.kernel.org 4 + Description: 5 + DAC operating mode. One of the following modes can be selected: 6 + 7 + * normal: This is DAC normal mode. 8 + * mixed-mode: In this mode the output is effectively chopped at 9 + the DAC sample rate. This has the effect of 10 + reducing the power of the fundamental signal while 11 + increasing the power of the images centered around 12 + the DAC sample rate, thus improving the output 13 + power of these images. 14 + 15 + What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_operating_mode_available 16 + KernelVersion: 6.9 17 + Contact: linux-iio@vger.kernel.org 18 + Description: 19 + Available operating modes.
+2
Documentation/devicetree/bindings/iio/accel/adi,adxl345.yaml
··· 32 32 33 33 spi-cpol: true 34 34 35 + spi-3wire: true 36 + 35 37 interrupts: 36 38 maxItems: 1 37 39
+279
Documentation/devicetree/bindings/iio/adc/adi,ad7173.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + # Copyright 2023 Analog Devices Inc. 3 + %YAML 1.2 4 + --- 5 + $id: http://devicetree.org/schemas/iio/adc/adi,ad7173.yaml# 6 + $schema: http://devicetree.org/meta-schemas/core.yaml# 7 + 8 + title: Analog Devices AD7173 ADC 9 + 10 + maintainers: 11 + - Ceclan Dumitru <dumitru.ceclan@analog.com> 12 + 13 + description: | 14 + Analog Devices AD717x ADC's: 15 + The AD717x family offer a complete integrated Sigma-Delta ADC solution which 16 + can be used in high precision, low noise single channel applications 17 + (Life Science measurements) or higher speed multiplexed applications 18 + (Factory Automation PLC Input modules). The Sigma-Delta ADC is intended 19 + primarily for measurement of signals close to DC but also delivers 20 + outstanding performance with input bandwidths out to ~10kHz. 21 + 22 + Datasheets for supported chips: 23 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7172-2.pdf 24 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7172-4.pdf 25 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7173-8.pdf 26 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7175-2.pdf 27 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7175-8.pdf 28 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7176-2.pdf 29 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7177-2.pdf 30 + 31 + properties: 32 + compatible: 33 + enum: 34 + - adi,ad7172-2 35 + - adi,ad7172-4 36 + - adi,ad7173-8 37 + - adi,ad7175-2 38 + - adi,ad7175-8 39 + - adi,ad7176-2 40 + - adi,ad7177-2 41 + 42 + reg: 43 + maxItems: 1 44 + 45 + interrupts: 46 + minItems: 1 47 + items: 48 + - description: | 49 + Ready: multiplexed with SPI data out. While SPI CS is low, 50 + can be used to indicate the completion of a conversion. 51 + 52 + - description: | 53 + Error: The three error bits in the status register (ADC_ERROR, CRC_ERROR, 54 + and REG_ERROR) are OR'ed, inverted, and mapped to the ERROR pin. 55 + Therefore, the ERROR pin indicates that an error has occurred. 56 + 57 + interrupt-names: 58 + minItems: 1 59 + items: 60 + - const: rdy 61 + - const: err 62 + 63 + '#address-cells': 64 + const: 1 65 + 66 + '#size-cells': 67 + const: 0 68 + 69 + spi-max-frequency: 70 + maximum: 20000000 71 + 72 + gpio-controller: 73 + description: Marks the device node as a GPIO controller. 74 + 75 + '#gpio-cells': 76 + const: 2 77 + description: 78 + The first cell is the GPIO number and the second cell specifies 79 + GPIO flags, as defined in <dt-bindings/gpio/gpio.h>. 80 + 81 + vref-supply: 82 + description: | 83 + Differential external reference supply used for conversion. The reference 84 + voltage (Vref) specified here must be the voltage difference between the 85 + REF+ and REF- pins: Vref = (REF+) - (REF-). 86 + 87 + vref2-supply: 88 + description: | 89 + Differential external reference supply used for conversion. The reference 90 + voltage (Vref2) specified here must be the voltage difference between the 91 + REF2+ and REF2- pins: Vref2 = (REF2+) - (REF2-). 92 + 93 + avdd-supply: 94 + description: Avdd supply, can be used as reference for conversion. 95 + This supply is referenced to AVSS, voltage specified here 96 + represents (AVDD1 - AVSS). 97 + 98 + avdd2-supply: 99 + description: Avdd2 supply, used as the input to the internal voltage regulator. 100 + This supply is referenced to AVSS, voltage specified here 101 + represents (AVDD2 - AVSS). 102 + 103 + iovdd-supply: 104 + description: iovdd supply, used for the chip digital interface. 105 + 106 + clocks: 107 + maxItems: 1 108 + description: | 109 + Optional external clock source. Can include one clock source: external 110 + clock or external crystal. 111 + 112 + clock-names: 113 + enum: 114 + - ext-clk 115 + - xtal 116 + 117 + '#clock-cells': 118 + const: 0 119 + 120 + patternProperties: 121 + "^channel@[0-9a-f]$": 122 + type: object 123 + $ref: adc.yaml 124 + unevaluatedProperties: false 125 + 126 + properties: 127 + reg: 128 + minimum: 0 129 + maximum: 15 130 + 131 + diff-channels: 132 + items: 133 + minimum: 0 134 + maximum: 31 135 + 136 + adi,reference-select: 137 + description: | 138 + Select the reference source to use when converting on 139 + the specific channel. Valid values are: 140 + vref : REF+ /REF− 141 + vref2 : REF2+ /REF2− 142 + refout-avss: REFOUT/AVSS (Internal reference) 143 + avdd : AVDD /AVSS 144 + 145 + External reference ref2 only available on ad7173-8 and ad7172-4. 146 + Internal reference refout-avss not available on ad7172-4. 147 + 148 + If not specified, internal reference used (if available). 149 + $ref: /schemas/types.yaml#/definitions/string 150 + enum: 151 + - vref 152 + - vref2 153 + - refout-avss 154 + - avdd 155 + default: refout-avss 156 + 157 + required: 158 + - reg 159 + - diff-channels 160 + 161 + required: 162 + - compatible 163 + - reg 164 + 165 + allOf: 166 + - $ref: /schemas/spi/spi-peripheral-props.yaml# 167 + 168 + # Only ad7172-4, ad7173-8 and ad7175-8 support vref2 169 + # Other models have [0-3] channel registers 170 + - if: 171 + properties: 172 + compatible: 173 + not: 174 + contains: 175 + enum: 176 + - adi,ad7172-4 177 + - adi,ad7173-8 178 + - adi,ad7175-8 179 + then: 180 + properties: 181 + vref2-supply: false 182 + patternProperties: 183 + "^channel@[0-9a-f]$": 184 + properties: 185 + adi,reference-select: 186 + enum: 187 + - vref 188 + - refout-avss 189 + - avdd 190 + reg: 191 + maximum: 3 192 + 193 + # Model ad7172-4 does not support internal reference 194 + - if: 195 + properties: 196 + compatible: 197 + contains: 198 + const: adi,ad7172-4 199 + then: 200 + patternProperties: 201 + "^channel@[0-9a-f]$": 202 + properties: 203 + reg: 204 + maximum: 7 205 + adi,reference-select: 206 + enum: 207 + - vref 208 + - vref2 209 + - avdd 210 + required: 211 + - adi,reference-select 212 + 213 + - if: 214 + anyOf: 215 + - required: [clock-names] 216 + - required: [clocks] 217 + then: 218 + properties: 219 + '#clock-cells': false 220 + 221 + unevaluatedProperties: false 222 + 223 + examples: 224 + - | 225 + #include <dt-bindings/gpio/gpio.h> 226 + #include <dt-bindings/interrupt-controller/irq.h> 227 + 228 + spi { 229 + #address-cells = <1>; 230 + #size-cells = <0>; 231 + 232 + adc@0 { 233 + compatible = "adi,ad7173-8"; 234 + reg = <0>; 235 + 236 + #address-cells = <1>; 237 + #size-cells = <0>; 238 + 239 + interrupts = <25 IRQ_TYPE_EDGE_FALLING>; 240 + interrupt-names = "rdy"; 241 + interrupt-parent = <&gpio>; 242 + spi-max-frequency = <5000000>; 243 + gpio-controller; 244 + #gpio-cells = <2>; 245 + #clock-cells = <0>; 246 + 247 + vref-supply = <&dummy_regulator>; 248 + 249 + channel@0 { 250 + reg = <0>; 251 + bipolar; 252 + diff-channels = <0 1>; 253 + adi,reference-select = "vref"; 254 + }; 255 + 256 + channel@1 { 257 + reg = <1>; 258 + diff-channels = <2 3>; 259 + }; 260 + 261 + channel@2 { 262 + reg = <2>; 263 + bipolar; 264 + diff-channels = <4 5>; 265 + }; 266 + 267 + channel@3 { 268 + reg = <3>; 269 + bipolar; 270 + diff-channels = <6 7>; 271 + }; 272 + 273 + channel@4 { 274 + reg = <4>; 275 + diff-channels = <8 9>; 276 + adi,reference-select = "avdd"; 277 + }; 278 + }; 279 + };
+213
Documentation/devicetree/bindings/iio/adc/adi,ad7944.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/adc/adi,ad7944.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices PulSAR LFCSP Analog to Digital Converters 8 + 9 + maintainers: 10 + - Michael Hennerich <Michael.Hennerich@analog.com> 11 + - Nuno Sá <nuno.sa@analog.com> 12 + 13 + description: | 14 + A family of pin-compatible single channel differential analog to digital 15 + converters with SPI support in a LFCSP package. 16 + 17 + * https://www.analog.com/en/products/ad7944.html 18 + * https://www.analog.com/en/products/ad7985.html 19 + * https://www.analog.com/en/products/ad7986.html 20 + 21 + $ref: /schemas/spi/spi-peripheral-props.yaml# 22 + 23 + properties: 24 + compatible: 25 + enum: 26 + - adi,ad7944 27 + - adi,ad7985 28 + - adi,ad7986 29 + 30 + reg: 31 + maxItems: 1 32 + 33 + spi-max-frequency: 34 + maximum: 111111111 35 + 36 + spi-cpol: true 37 + spi-cpha: true 38 + 39 + adi,spi-mode: 40 + $ref: /schemas/types.yaml#/definitions/string 41 + enum: [ single, chain ] 42 + description: | 43 + This property indicates the SPI wiring configuration. 44 + 45 + When this property is omitted, it is assumed that the device is using what 46 + the datasheet calls "4-wire mode". This is the conventional SPI mode used 47 + when there are multiple devices on the same bus. In this mode, the CNV 48 + line is used to initiate the conversion and the SDI line is connected to 49 + CS on the SPI controller. 50 + 51 + When this property is present, it indicates that the device is using one 52 + of the following alternative wiring configurations: 53 + 54 + * single: The datasheet calls this "3-wire mode". (NOTE: The datasheet's 55 + definition of 3-wire mode is NOT at all related to the standard 56 + spi-3wire property!) This mode is often used when the ADC is the only 57 + device on the bus. In this mode, SDI is tied to VIO, and the CNV line 58 + can be connected to the CS line of the SPI controller or to a GPIO, in 59 + which case the CS line of the controller is unused. 60 + * chain: The datasheet calls this "chain mode". This mode is used to save 61 + on wiring when multiple ADCs are used. In this mode, the SDI line of 62 + one chip is tied to the SDO of the next chip in the chain and the SDI of 63 + the last chip in the chain is tied to GND. Only the first chip in the 64 + chain is connected to the SPI bus. The CNV line of all chips are tied 65 + together. The CS line of the SPI controller can be used as the CNV line 66 + only if it is active high. 67 + 68 + '#daisy-chained-devices': true 69 + 70 + avdd-supply: 71 + description: A 2.5V supply that powers the analog circuitry. 72 + 73 + dvdd-supply: 74 + description: A 2.5V supply that powers the digital circuitry. 75 + 76 + vio-supply: 77 + description: 78 + A 1.8V to 2.7V supply for the digital inputs and outputs. 79 + 80 + bvdd-supply: 81 + description: 82 + A voltage supply for the buffered power. When using an external reference 83 + without an internal buffer (PDREF high, REFIN low), this should be 84 + connected to the same supply as ref-supply. Otherwise, when using an 85 + internal reference or an external reference with an internal buffer, this 86 + is connected to a 5V supply. 87 + 88 + ref-supply: 89 + description: 90 + Voltage regulator for the external reference voltage (REF). This property 91 + is omitted when using an internal reference. 92 + 93 + refin-supply: 94 + description: 95 + Voltage regulator for the reference buffer input (REFIN). When using an 96 + external buffer with internal reference, this should be connected to a 97 + 1.2V external reference voltage supply. Otherwise, this property is 98 + omitted. 99 + 100 + cnv-gpios: 101 + description: 102 + The Convert Input (CNV). This input has multiple functions. It initiates 103 + the conversions and selects the SPI mode of the device (chain or CS). In 104 + 'single' mode, this property is omitted if the CNV pin is connected to the 105 + CS line of the SPI controller. 106 + maxItems: 1 107 + 108 + turbo-gpios: 109 + description: 110 + GPIO connected to the TURBO line. If omitted, it is assumed that the TURBO 111 + line is hard-wired and the state is determined by the adi,always-turbo 112 + property. 113 + maxItems: 1 114 + 115 + adi,always-turbo: 116 + type: boolean 117 + description: 118 + When present, this property indicates that the TURBO line is hard-wired 119 + and the state is always high. If neither this property nor turbo-gpios is 120 + present, the TURBO line is assumed to be hard-wired and the state is 121 + always low. 122 + 123 + interrupts: 124 + description: 125 + The SDO pin can also function as a busy indicator. This node should be 126 + connected to an interrupt that is triggered when the SDO line goes low 127 + while the SDI line is high and the CNV line is low ('single' mode) or the 128 + SDI line is low and the CNV line is high ('multi' mode); or when the SDO 129 + line goes high while the SDI and CNV lines are high (chain mode), 130 + maxItems: 1 131 + 132 + required: 133 + - compatible 134 + - reg 135 + - avdd-supply 136 + - dvdd-supply 137 + - vio-supply 138 + - bvdd-supply 139 + 140 + allOf: 141 + # ref-supply and refin-supply are mutually exclusive (neither is also valid) 142 + - if: 143 + required: 144 + - ref-supply 145 + then: 146 + properties: 147 + refin-supply: false 148 + - if: 149 + required: 150 + - refin-supply 151 + then: 152 + properties: 153 + ref-supply: false 154 + # in '4-wire' mode, cnv-gpios is required, for other modes it is optional 155 + - if: 156 + not: 157 + required: 158 + - adi,spi-mode 159 + then: 160 + required: 161 + - cnv-gpios 162 + # chain mode has lower SCLK max rate and doesn't work when TURBO is enabled 163 + - if: 164 + required: 165 + - adi,spi-mode 166 + properties: 167 + adi,spi-mode: 168 + const: chain 169 + then: 170 + properties: 171 + spi-max-frequency: 172 + maximum: 90909090 173 + adi,always-turbo: false 174 + required: 175 + - '#daisy-chained-devices' 176 + else: 177 + properties: 178 + '#daisy-chained-devices': false 179 + # turbo-gpios and adi,always-turbo are mutually exclusive 180 + - if: 181 + required: 182 + - turbo-gpios 183 + then: 184 + properties: 185 + adi,always-turbo: false 186 + - if: 187 + required: 188 + - adi,always-turbo 189 + then: 190 + properties: 191 + turbo-gpios: false 192 + 193 + unevaluatedProperties: false 194 + 195 + examples: 196 + - | 197 + #include <dt-bindings/gpio/gpio.h> 198 + spi { 199 + #address-cells = <1>; 200 + #size-cells = <0>; 201 + adc@0 { 202 + compatible = "adi,ad7944"; 203 + reg = <0>; 204 + spi-cpha; 205 + spi-max-frequency = <111111111>; 206 + avdd-supply = <&supply_2_5V>; 207 + dvdd-supply = <&supply_2_5V>; 208 + vio-supply = <&supply_1_8V>; 209 + bvdd-supply = <&supply_5V>; 210 + cnv-gpios = <&gpio 0 GPIO_ACTIVE_HIGH>; 211 + turbo-gpios = <&gpio 1 GPIO_ACTIVE_HIGH>; 212 + }; 213 + };
+7 -2
Documentation/devicetree/bindings/iio/adc/allwinner,sun20i-d1-gpadc.yaml
··· 11 11 12 12 properties: 13 13 compatible: 14 - enum: 15 - - allwinner,sun20i-d1-gpadc 14 + oneOf: 15 + - enum: 16 + - allwinner,sun20i-d1-gpadc 17 + - items: 18 + - enum: 19 + - allwinner,sun50i-h616-gpadc 20 + - const: allwinner,sun20i-d1-gpadc 16 21 17 22 "#io-channel-cells": 18 23 const: 1
+95
Documentation/devicetree/bindings/iio/dac/adi,ad9739a.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/dac/adi,ad9739a.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices AD9739A RF DAC 8 + 9 + maintainers: 10 + - Dragos Bogdan <dragos.bogdan@analog.com> 11 + - Nuno Sa <nuno.sa@analog.com> 12 + 13 + description: | 14 + The AD9739A is a 14-bit, 2.5 GSPS high performance RF DACs that are capable 15 + of synthesizing wideband signals from dc up to 3 GHz. 16 + 17 + https://www.analog.com/media/en/technical-documentation/data-sheets/ad9737a_9739a.pdf 18 + 19 + properties: 20 + compatible: 21 + enum: 22 + - adi,ad9739a 23 + 24 + reg: 25 + maxItems: 1 26 + 27 + clocks: 28 + maxItems: 1 29 + 30 + reset-gpios: 31 + maxItems: 1 32 + 33 + interrupts: 34 + maxItems: 1 35 + 36 + vdd-3p3-supply: 37 + description: 3.3V Digital input supply. 38 + 39 + vdd-supply: 40 + description: 1.8V Digital input supply. 41 + 42 + vdda-supply: 43 + description: 3.3V Analog input supply. 44 + 45 + vddc-supply: 46 + description: 1.8V Clock input supply. 47 + 48 + vref-supply: 49 + description: Input/Output reference supply. 50 + 51 + io-backends: 52 + maxItems: 1 53 + 54 + adi,full-scale-microamp: 55 + description: This property represents the DAC full scale current. 56 + minimum: 8580 57 + maximum: 31700 58 + default: 20000 59 + 60 + required: 61 + - compatible 62 + - reg 63 + - clocks 64 + - io-backends 65 + - vdd-3p3-supply 66 + - vdd-supply 67 + - vdda-supply 68 + - vddc-supply 69 + 70 + allOf: 71 + - $ref: /schemas/spi/spi-peripheral-props.yaml# 72 + 73 + unevaluatedProperties: false 74 + 75 + examples: 76 + - | 77 + spi { 78 + #address-cells = <1>; 79 + #size-cells = <0>; 80 + 81 + dac@0 { 82 + compatible = "adi,ad9739a"; 83 + reg = <0>; 84 + 85 + clocks = <&dac_clk>; 86 + 87 + io-backends = <&iio_backend>; 88 + 89 + vdd-3p3-supply = <&vdd_3_3>; 90 + vdd-supply = <&vdd>; 91 + vdda-supply = <&vdd_3_3>; 92 + vddc-supply = <&vdd>; 93 + }; 94 + }; 95 + ...
+62
Documentation/devicetree/bindings/iio/dac/adi,axi-dac.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/dac/adi,axi-dac.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices AXI DAC IP core 8 + 9 + maintainers: 10 + - Nuno Sa <nuno.sa@analog.com> 11 + 12 + description: | 13 + Analog Devices Generic AXI DAC IP core for interfacing a DAC device 14 + with a high speed serial (JESD204B/C) or source synchronous parallel 15 + interface (LVDS/CMOS). 16 + Usually, some other interface type (i.e SPI) is used as a control 17 + interface for the actual DAC, while this IP core will interface 18 + to the data-lines of the DAC and handle the streaming of data from 19 + memory via DMA into the DAC. 20 + 21 + https://wiki.analog.com/resources/fpga/docs/axi_dac_ip 22 + 23 + properties: 24 + compatible: 25 + enum: 26 + - adi,axi-dac-9.1.b 27 + 28 + reg: 29 + maxItems: 1 30 + 31 + dmas: 32 + maxItems: 1 33 + 34 + dma-names: 35 + items: 36 + - const: tx 37 + 38 + clocks: 39 + maxItems: 1 40 + 41 + '#io-backend-cells': 42 + const: 0 43 + 44 + required: 45 + - compatible 46 + - dmas 47 + - reg 48 + - clocks 49 + 50 + additionalProperties: false 51 + 52 + examples: 53 + - | 54 + dac@44a00000 { 55 + compatible = "adi,axi-dac-9.1.b"; 56 + reg = <0x44a00000 0x10000>; 57 + dmas = <&tx_dma 0>; 58 + dma-names = "tx"; 59 + #io-backend-cells = <0>; 60 + clocks = <&axi_clk>; 61 + }; 62 + ...
+1
Documentation/devicetree/bindings/iio/dac/ti,dac5571.yaml
··· 21 21 - ti,dac5573 22 22 - ti,dac6573 23 23 - ti,dac7573 24 + - ti,dac081c081 24 25 - ti,dac121c081 25 26 26 27 reg:
+8 -4
Documentation/devicetree/bindings/iio/health/maxim,max30102.yaml
··· 4 4 $id: http://devicetree.org/schemas/iio/health/maxim,max30102.yaml# 5 5 $schema: http://devicetree.org/meta-schemas/core.yaml# 6 6 7 - title: Maxim MAX30102 heart rate and pulse oximeter and MAX30105 particle-sensor 7 + title: Maxim MAX30101/2 heart rate and pulse oximeter and MAX30105 particle-sensor 8 8 9 9 maintainers: 10 10 - Matt Ranostay <matt.ranostay@konsulko.com> 11 11 12 12 properties: 13 13 compatible: 14 - enum: 15 - - maxim,max30102 16 - - maxim,max30105 14 + oneOf: 15 + - enum: 16 + - maxim,max30102 17 + - maxim,max30105 18 + - items: 19 + - const: maxim,max30101 20 + - const: maxim,max30105 17 21 18 22 reg: 19 23 maxItems: 1
+5
Documentation/devicetree/bindings/iio/humidity/ti,hdc3020.yaml
··· 34 34 reg: 35 35 maxItems: 1 36 36 37 + reset-gpios: 38 + maxItems: 1 39 + 37 40 required: 38 41 - compatible 39 42 - reg ··· 46 43 47 44 examples: 48 45 - | 46 + #include <dt-bindings/gpio/gpio.h> 49 47 #include <dt-bindings/interrupt-controller/irq.h> 50 48 i2c { 51 49 #address-cells = <1>; ··· 58 54 vdd-supply = <&vcc_3v3>; 59 55 interrupt-parent = <&gpio3>; 60 56 interrupts = <23 IRQ_TYPE_EDGE_RISING>; 57 + reset-gpios = <&gpio3 27 GPIO_ACTIVE_LOW>; 61 58 }; 62 59 };
+1
Documentation/devicetree/bindings/iio/imu/invensense,icm42600.yaml
··· 32 32 - invensense,icm42605 33 33 - invensense,icm42622 34 34 - invensense,icm42631 35 + - invensense,icm42688 35 36 36 37 reg: 37 38 maxItems: 1
+9 -8
Documentation/devicetree/bindings/iio/imu/invensense,mpu6050.yaml
··· 62 62 allOf: 63 63 - $ref: /schemas/spi/spi-peripheral-props.yaml# 64 64 - if: 65 - not: 66 - properties: 67 - compatible: 68 - contains: 69 - enum: 70 - - invensense,mpu9150 71 - - invensense,mpu9250 72 - - invensense,mpu9255 65 + properties: 66 + compatible: 67 + contains: 68 + enum: 69 + - invensense,iam20680 70 + - invensense,icm20602 71 + - invensense,icm20608 72 + - invensense,icm20609 73 + - invensense,icm20689 73 74 then: 74 75 properties: 75 76 i2c-gate: false
+15 -5
Documentation/devicetree/bindings/iio/light/avago,apds9300.yaml
··· 4 4 $id: http://devicetree.org/schemas/iio/light/avago,apds9300.yaml# 5 5 $schema: http://devicetree.org/meta-schemas/core.yaml# 6 6 7 - title: Avago APDS9300 ambient light sensor 7 + title: Avago Gesture/RGB/ALS/Proximity sensors 8 8 9 9 maintainers: 10 - - Jonathan Cameron <jic23@kernel.org> 10 + - Subhajit Ghosh <subhajit.ghosh@tweaklogic.com> 11 11 12 12 description: | 13 - Datasheet at https://www.avagotech.com/docs/AV02-1077EN 13 + Datasheet: https://www.avagotech.com/docs/AV02-1077EN 14 + Datasheet: https://www.avagotech.com/docs/AV02-4191EN 15 + Datasheet: https://www.avagotech.com/docs/AV02-4755EN 14 16 15 17 properties: 16 18 compatible: 17 - const: avago,apds9300 19 + enum: 20 + - avago,apds9300 21 + - avago,apds9306 22 + - avago,apds9960 18 23 19 24 reg: 20 25 maxItems: 1 21 26 22 27 interrupts: 23 28 maxItems: 1 29 + 30 + vdd-supply: true 24 31 25 32 additionalProperties: false 26 33 ··· 37 30 38 31 examples: 39 32 - | 33 + #include <dt-bindings/interrupt-controller/irq.h> 34 + 40 35 i2c { 41 36 #address-cells = <1>; 42 37 #size-cells = <0>; ··· 47 38 compatible = "avago,apds9300"; 48 39 reg = <0x39>; 49 40 interrupt-parent = <&gpio2>; 50 - interrupts = <29 8>; 41 + interrupts = <29 IRQ_TYPE_LEVEL_LOW>; 42 + vdd-supply = <&regulator_3v3>; 51 43 }; 52 44 }; 53 45 ...
-44
Documentation/devicetree/bindings/iio/light/avago,apds9960.yaml
··· 1 - # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 - %YAML 1.2 3 - --- 4 - $id: http://devicetree.org/schemas/iio/light/avago,apds9960.yaml# 5 - $schema: http://devicetree.org/meta-schemas/core.yaml# 6 - 7 - title: Avago APDS9960 gesture/RGB/ALS/proximity sensor 8 - 9 - maintainers: 10 - - Matt Ranostay <matt.ranostay@konsulko.com> 11 - 12 - description: | 13 - Datasheet at https://www.avagotech.com/docs/AV02-4191EN 14 - 15 - properties: 16 - compatible: 17 - const: avago,apds9960 18 - 19 - reg: 20 - maxItems: 1 21 - 22 - interrupts: 23 - maxItems: 1 24 - 25 - additionalProperties: false 26 - 27 - required: 28 - - compatible 29 - - reg 30 - 31 - examples: 32 - - | 33 - i2c { 34 - #address-cells = <1>; 35 - #size-cells = <0>; 36 - 37 - light-sensor@39 { 38 - compatible = "avago,apds9960"; 39 - reg = <0x39>; 40 - interrupt-parent = <&gpio1>; 41 - interrupts = <16 1>; 42 - }; 43 - }; 44 - ...
+4
Documentation/devicetree/bindings/iio/temperature/adi,ltc2983.yaml
··· 57 57 interrupts: 58 58 maxItems: 1 59 59 60 + vdd-supply: true 61 + 60 62 adi,mux-delay-config-us: 61 63 description: | 62 64 Extra delay prior to each conversion, in addition to the internal 1ms ··· 462 460 - compatible 463 461 - reg 464 462 - interrupts 463 + - vdd-supply 465 464 466 465 additionalProperties: false 467 466 ··· 492 489 #address-cells = <1>; 493 490 #size-cells = <0>; 494 491 492 + vdd-supply = <&supply>; 495 493 interrupts = <20 IRQ_TYPE_EDGE_RISING>; 496 494 interrupt-parent = <&gpio>; 497 495
+130
Documentation/iio/ad7944.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0-only 2 + 3 + ============= 4 + AD7944 driver 5 + ============= 6 + 7 + ADC driver for Analog Devices Inc. AD7944 and similar devices. The module name 8 + is ``ad7944``. 9 + 10 + 11 + Supported devices 12 + ================= 13 + 14 + The following chips are supported by this driver: 15 + 16 + * `AD7944 <https://www.analog.com/AD7944>`_ 17 + * `AD7985 <https://www.analog.com/AD7985>`_ 18 + * `AD7986 <https://www.analog.com/AD7986>`_ 19 + 20 + 21 + Supported features 22 + ================== 23 + 24 + SPI wiring modes 25 + ---------------- 26 + 27 + The driver currently supports two of the many possible SPI wiring configurations. 28 + 29 + CS mode, 3-wire, without busy indicator 30 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 31 + 32 + .. code-block:: 33 + 34 + +-------------+ 35 + +--------------------| CS | 36 + v | | 37 + VIO +--------------------+ | HOST | 38 + | | CNV | | | 39 + +--->| SDI AD7944 SDO |-------->| SDI | 40 + | SCK | | | 41 + +--------------------+ | | 42 + ^ | | 43 + +--------------------| SCLK | 44 + +-------------+ 45 + 46 + To select this mode in the device tree, set the ``adi,spi-mode`` property to 47 + ``"single"`` and omit the ``cnv-gpios`` property. 48 + 49 + CS mode, 4-wire, without busy indicator 50 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 51 + 52 + .. code-block:: 53 + 54 + +-------------+ 55 + +-----------------------------------| CS | 56 + | | | 57 + | +--------------------| GPIO | 58 + | v | | 59 + | +--------------------+ | HOST | 60 + | | CNV | | | 61 + +--->| SDI AD7944 SDO |-------->| SDI | 62 + | SCK | | | 63 + +--------------------+ | | 64 + ^ | | 65 + +--------------------| SCLK | 66 + +-------------+ 67 + 68 + To select this mode in the device tree, omit the ``adi,spi-mode`` property and 69 + provide the ``cnv-gpios`` property. 70 + 71 + Reference voltage 72 + ----------------- 73 + 74 + All 3 possible reference voltage sources are supported: 75 + 76 + - Internal reference 77 + - External 1.2V reference and internal buffer 78 + - External reference 79 + 80 + The source is determined by the device tree. If ``ref-supply`` is present, then 81 + the external reference is used. If ``refin-supply`` is present, then the internal 82 + buffer is used. If neither is present, then the internal reference is used. 83 + 84 + Unimplemented features 85 + ---------------------- 86 + 87 + - ``BUSY`` indication 88 + - ``TURBO`` mode 89 + - Daisy chain mode 90 + 91 + 92 + Device attributes 93 + ================= 94 + 95 + There are two types of ADCs in this family, pseudo-differential and fully 96 + differential. The channel name is different depending on the type of ADC. 97 + 98 + Pseudo-differential ADCs 99 + ------------------------ 100 + 101 + AD7944 and AD7985 are pseudo-differential ADCs and have the following attributes: 102 + 103 + +---------------------------------------+--------------------------------------------------------------+ 104 + | Attribute | Description | 105 + +=======================================+==============================================================+ 106 + | ``in_voltage0_raw`` | Raw ADC voltage value (*IN+* referenced to ground sense). | 107 + +---------------------------------------+--------------------------------------------------------------+ 108 + | ``in_voltage0_scale`` | Scale factor to convert raw value to mV. | 109 + +---------------------------------------+--------------------------------------------------------------+ 110 + 111 + Fully-differential ADCs 112 + ----------------------- 113 + 114 + AD7986 is a fully-differential ADC and has the following attributes: 115 + 116 + +---------------------------------------+--------------------------------------------------------------+ 117 + | Attribute | Description | 118 + +=======================================+==============================================================+ 119 + | ``in_voltage0-voltage1_raw`` | Raw ADC voltage value (*IN+* - *IN-*). | 120 + +---------------------------------------+--------------------------------------------------------------+ 121 + | ``in_voltage0-voltage1_scale`` | Scale factor to convert raw value to mV. | 122 + +---------------------------------------+--------------------------------------------------------------+ 123 + 124 + 125 + Device buffers 126 + ============== 127 + 128 + This driver supports IIO triggered buffers. 129 + 130 + See :doc:`iio_devbuf` for more information.
+1
Documentation/iio/index.rst
··· 16 16 .. toctree:: 17 17 :maxdepth: 1 18 18 19 + ad7944 19 20 adis16475 20 21 bno055 21 22 ep93xx_adc
+28
MAINTAINERS
··· 441 441 W: https://ez.analog.com/linux-software-drivers 442 442 F: drivers/input/touchscreen/ad7879.c 443 443 444 + AD7944 ADC DRIVER (AD7944/AD7985/AD7986) 445 + M: Michael Hennerich <michael.hennerich@analog.com> 446 + M: Nuno Sá <nuno.sa@analog.com> 447 + R: David Lechner <dlechner@baylibre.com> 448 + S: Supported 449 + W: https://ez.analog.com/linux-software-drivers 450 + F: Documentation/devicetree/bindings/iio/adc/adi,ad7944.yaml 451 + F: Documentation/iio/ad7944.rst 452 + F: drivers/iio/adc/ad7944.c 453 + 444 454 ADAFRUIT MINI I2C GAMEPAD 445 455 M: Anshul Dalal <anshulusr@gmail.com> 446 456 L: linux-input@vger.kernel.org ··· 1234 1224 F: Documentation/devicetree/bindings/iio/adc/adi,ad7780.yaml 1235 1225 F: drivers/iio/adc/ad7780.c 1236 1226 1227 + ANALOG DEVICES INC AD9739a DRIVER 1228 + M: Nuno Sa <nuno.sa@analog.com> 1229 + M: Dragos Bogdan <dragos.bogdan@analog.com> 1230 + L: linux-iio@vger.kernel.org 1231 + S: Supported 1232 + W: https://ez.analog.com/linux-software-drivers 1233 + F: Documentation/devicetree/bindings/iio/dac/adi,ad9739a.yaml 1234 + F: drivers/iio/dac/ad9739a.c 1235 + 1237 1236 ANALOG DEVICES INC ADA4250 DRIVER 1238 1237 M: Antoniu Miclaus <antoniu.miclaus@analog.com> 1239 1238 L: linux-iio@vger.kernel.org ··· 1407 1388 F: sound/soc/codecs/adav* 1408 1389 F: sound/soc/codecs/sigmadsp.* 1409 1390 F: sound/soc/codecs/ssm* 1391 + 1392 + ANALOG DEVICES INC AXI DAC DRIVER 1393 + M: Nuno Sa <nuno.sa@analog.com> 1394 + L: linux-iio@vger.kernel.org 1395 + S: Supported 1396 + W: https://ez.analog.com/linux-software-drivers 1397 + F: Documentation/devicetree/bindings/iio/dac/adi,axi-dac.yaml 1398 + F: drivers/iio/dac/adi-axi-dac.c 1410 1399 1411 1400 ANALOG DEVICES INC DMA DRIVERS 1412 1401 M: Lars-Peter Clausen <lars@metafoo.de> ··· 10569 10542 F: Documentation/ABI/testing/configfs-iio* 10570 10543 F: Documentation/ABI/testing/sysfs-bus-iio* 10571 10544 F: Documentation/devicetree/bindings/iio/ 10545 + F: Documentation/iio/ 10572 10546 F: drivers/iio/ 10573 10547 F: drivers/staging/iio/ 10574 10548 F: include/dt-bindings/iio/
-14
drivers/base/property.c
··· 869 869 EXPORT_SYMBOL_GPL(fwnode_handle_get); 870 870 871 871 /** 872 - * fwnode_handle_put - Drop reference to a device node 873 - * @fwnode: Pointer to the device node to drop the reference to. 874 - * 875 - * This has to be used when terminating device_for_each_child_node() iteration 876 - * with break or return to prevent stale device node references from being left 877 - * behind. 878 - */ 879 - void fwnode_handle_put(struct fwnode_handle *fwnode) 880 - { 881 - fwnode_call_void_op(fwnode, put); 882 - } 883 - EXPORT_SYMBOL_GPL(fwnode_handle_put); 884 - 885 - /** 886 872 * fwnode_device_is_available - check if a device is available for use 887 873 * @fwnode: Pointer to the fwnode of the device. 888 874 *
+35 -1
drivers/iio/accel/adxl345.h
··· 8 8 #ifndef _ADXL345_H_ 9 9 #define _ADXL345_H_ 10 10 11 + #define ADXL345_REG_DEVID 0x00 12 + #define ADXL345_REG_OFSX 0x1E 13 + #define ADXL345_REG_OFSY 0x1F 14 + #define ADXL345_REG_OFSZ 0x20 15 + #define ADXL345_REG_OFS_AXIS(index) (ADXL345_REG_OFSX + (index)) 16 + #define ADXL345_REG_BW_RATE 0x2C 17 + #define ADXL345_REG_POWER_CTL 0x2D 18 + #define ADXL345_REG_DATA_FORMAT 0x31 19 + #define ADXL345_REG_DATAX0 0x32 20 + #define ADXL345_REG_DATAY0 0x34 21 + #define ADXL345_REG_DATAZ0 0x36 22 + #define ADXL345_REG_DATA_AXIS(index) \ 23 + (ADXL345_REG_DATAX0 + (index) * sizeof(__le16)) 24 + 25 + #define ADXL345_BW_RATE GENMASK(3, 0) 26 + #define ADXL345_BASE_RATE_NANO_HZ 97656250LL 27 + 28 + #define ADXL345_POWER_CTL_MEASURE BIT(3) 29 + #define ADXL345_POWER_CTL_STANDBY 0x00 30 + 31 + #define ADXL345_DATA_FORMAT_RANGE GENMASK(1, 0) /* Set the g range */ 32 + #define ADXL345_DATA_FORMAT_JUSTIFY BIT(2) /* Left-justified (MSB) mode */ 33 + #define ADXL345_DATA_FORMAT_FULL_RES BIT(3) /* Up to 13-bits resolution */ 34 + #define ADXL345_DATA_FORMAT_SPI_3WIRE BIT(6) /* 3-wire SPI mode */ 35 + #define ADXL345_DATA_FORMAT_SELF_TEST BIT(7) /* Enable a self test */ 36 + 37 + #define ADXL345_DATA_FORMAT_2G 0 38 + #define ADXL345_DATA_FORMAT_4G 1 39 + #define ADXL345_DATA_FORMAT_8G 2 40 + #define ADXL345_DATA_FORMAT_16G 3 41 + 42 + #define ADXL345_DEVID 0xE5 43 + 11 44 /* 12 45 * In full-resolution mode, scale factor is maintained at ~4 mg/LSB 13 46 * in all g ranges. ··· 61 28 int uscale; 62 29 }; 63 30 64 - int adxl345_core_probe(struct device *dev, struct regmap *regmap); 31 + int adxl345_core_probe(struct device *dev, struct regmap *regmap, 32 + int (*setup)(struct device*, struct regmap*)); 65 33 66 34 #endif /* _ADXL345_H_ */
+47 -45
drivers/iio/accel/adxl345_core.c
··· 17 17 18 18 #include "adxl345.h" 19 19 20 - #define ADXL345_REG_DEVID 0x00 21 - #define ADXL345_REG_OFSX 0x1e 22 - #define ADXL345_REG_OFSY 0x1f 23 - #define ADXL345_REG_OFSZ 0x20 24 - #define ADXL345_REG_OFS_AXIS(index) (ADXL345_REG_OFSX + (index)) 25 - #define ADXL345_REG_BW_RATE 0x2C 26 - #define ADXL345_REG_POWER_CTL 0x2D 27 - #define ADXL345_REG_DATA_FORMAT 0x31 28 - #define ADXL345_REG_DATAX0 0x32 29 - #define ADXL345_REG_DATAY0 0x34 30 - #define ADXL345_REG_DATAZ0 0x36 31 - #define ADXL345_REG_DATA_AXIS(index) \ 32 - (ADXL345_REG_DATAX0 + (index) * sizeof(__le16)) 33 - 34 - #define ADXL345_BW_RATE GENMASK(3, 0) 35 - #define ADXL345_BASE_RATE_NANO_HZ 97656250LL 36 - 37 - #define ADXL345_POWER_CTL_MEASURE BIT(3) 38 - #define ADXL345_POWER_CTL_STANDBY 0x00 39 - 40 - #define ADXL345_DATA_FORMAT_FULL_RES BIT(3) /* Up to 13-bits resolution */ 41 - #define ADXL345_DATA_FORMAT_2G 0 42 - #define ADXL345_DATA_FORMAT_4G 1 43 - #define ADXL345_DATA_FORMAT_8G 2 44 - #define ADXL345_DATA_FORMAT_16G 3 45 - 46 - #define ADXL345_DEVID 0xE5 47 - 48 20 struct adxl345_data { 49 21 const struct adxl345_chip_info *info; 50 22 struct regmap *regmap; 51 - u8 data_range; 52 23 }; 53 24 54 25 #define ADXL345_CHANNEL(index, axis) { \ ··· 168 197 regmap_write(regmap, ADXL345_REG_POWER_CTL, ADXL345_POWER_CTL_STANDBY); 169 198 } 170 199 171 - int adxl345_core_probe(struct device *dev, struct regmap *regmap) 200 + /** 201 + * adxl345_core_probe() - probe and setup for the adxl345 accelerometer, 202 + * also covers the adlx375 accelerometer 203 + * @dev: Driver model representation of the device 204 + * @regmap: Regmap instance for the device 205 + * @setup: Setup routine to be executed right before the standard device 206 + * setup 207 + * 208 + * Return: 0 on success, negative errno on error 209 + */ 210 + int adxl345_core_probe(struct device *dev, struct regmap *regmap, 211 + int (*setup)(struct device*, struct regmap*)) 172 212 { 173 213 struct adxl345_data *data; 174 214 struct iio_dev *indio_dev; 175 215 u32 regval; 216 + unsigned int data_format_mask = (ADXL345_DATA_FORMAT_RANGE | 217 + ADXL345_DATA_FORMAT_JUSTIFY | 218 + ADXL345_DATA_FORMAT_FULL_RES | 219 + ADXL345_DATA_FORMAT_SELF_TEST); 176 220 int ret; 177 - 178 - ret = regmap_read(regmap, ADXL345_REG_DEVID, &regval); 179 - if (ret < 0) 180 - return dev_err_probe(dev, ret, "Error reading device ID\n"); 181 - 182 - if (regval != ADXL345_DEVID) 183 - return dev_err_probe(dev, -ENODEV, "Invalid device ID: %x, expected %x\n", 184 - regval, ADXL345_DEVID); 185 221 186 222 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 187 223 if (!indio_dev) ··· 196 218 197 219 data = iio_priv(indio_dev); 198 220 data->regmap = regmap; 199 - /* Enable full-resolution mode */ 200 - data->data_range = ADXL345_DATA_FORMAT_FULL_RES; 201 221 data->info = device_get_match_data(dev); 202 222 if (!data->info) 203 223 return -ENODEV; 204 - 205 - ret = regmap_write(data->regmap, ADXL345_REG_DATA_FORMAT, 206 - data->data_range); 207 - if (ret < 0) 208 - return dev_err_probe(dev, ret, "Failed to set data range\n"); 209 224 210 225 indio_dev->name = data->info->name; 211 226 indio_dev->info = &adxl345_info; 212 227 indio_dev->modes = INDIO_DIRECT_MODE; 213 228 indio_dev->channels = adxl345_channels; 214 229 indio_dev->num_channels = ARRAY_SIZE(adxl345_channels); 230 + 231 + if (setup) { 232 + /* Perform optional initial bus specific configuration */ 233 + ret = setup(dev, data->regmap); 234 + if (ret) 235 + return ret; 236 + 237 + /* Enable full-resolution mode */ 238 + ret = regmap_update_bits(data->regmap, ADXL345_REG_DATA_FORMAT, 239 + data_format_mask, 240 + ADXL345_DATA_FORMAT_FULL_RES); 241 + if (ret) 242 + return dev_err_probe(dev, ret, 243 + "Failed to set data range\n"); 244 + 245 + } else { 246 + /* Enable full-resolution mode (init all data_format bits) */ 247 + ret = regmap_write(data->regmap, ADXL345_REG_DATA_FORMAT, 248 + ADXL345_DATA_FORMAT_FULL_RES); 249 + if (ret) 250 + return dev_err_probe(dev, ret, 251 + "Failed to set data range\n"); 252 + } 253 + 254 + ret = regmap_read(data->regmap, ADXL345_REG_DEVID, &regval); 255 + if (ret < 0) 256 + return dev_err_probe(dev, ret, "Error reading device ID\n"); 257 + 258 + if (regval != ADXL345_DEVID) 259 + return dev_err_probe(dev, -ENODEV, "Invalid device ID: %x, expected %x\n", 260 + regval, ADXL345_DEVID); 215 261 216 262 /* Enable measurement mode */ 217 263 ret = adxl345_powerup(data->regmap);
+1 -1
drivers/iio/accel/adxl345_i2c.c
··· 27 27 if (IS_ERR(regmap)) 28 28 return dev_err_probe(&client->dev, PTR_ERR(regmap), "Error initializing regmap\n"); 29 29 30 - return adxl345_core_probe(&client->dev, regmap); 30 + return adxl345_core_probe(&client->dev, regmap, NULL); 31 31 } 32 32 33 33 static const struct adxl345_chip_info adxl345_i2c_info = {
+9 -1
drivers/iio/accel/adxl345_spi.c
··· 20 20 .read_flag_mask = BIT(7) | BIT(6), 21 21 }; 22 22 23 + static int adxl345_spi_setup(struct device *dev, struct regmap *regmap) 24 + { 25 + return regmap_write(regmap, ADXL345_REG_DATA_FORMAT, ADXL345_DATA_FORMAT_SPI_3WIRE); 26 + } 27 + 23 28 static int adxl345_spi_probe(struct spi_device *spi) 24 29 { 25 30 struct regmap *regmap; ··· 38 33 if (IS_ERR(regmap)) 39 34 return dev_err_probe(&spi->dev, PTR_ERR(regmap), "Error initializing regmap\n"); 40 35 41 - return adxl345_core_probe(&spi->dev, regmap); 36 + if (spi->mode & SPI_3WIRE) 37 + return adxl345_core_probe(&spi->dev, regmap, adxl345_spi_setup); 38 + else 39 + return adxl345_core_probe(&spi->dev, regmap, NULL); 42 40 } 43 41 44 42 static const struct adxl345_chip_info adxl345_spi_info = {
+1 -1
drivers/iio/accel/adxl367.c
··· 621 621 static int adxl367_set_odr(struct iio_dev *indio_dev, enum adxl367_odr odr) 622 622 { 623 623 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { 624 - struct adxl367_state *st = iio_priv(indio_dev);; 624 + struct adxl367_state *st = iio_priv(indio_dev); 625 625 int ret; 626 626 627 627 guard(mutex)(&st->lock);
+6 -4
drivers/iio/accel/fxls8962af-core.c
··· 15 15 #include <linux/bits.h> 16 16 #include <linux/bitfield.h> 17 17 #include <linux/i2c.h> 18 + #include <linux/irq.h> 18 19 #include <linux/module.h> 19 - #include <linux/of_irq.h> 20 + #include <linux/mod_devicetable.h> 20 21 #include <linux/pm_runtime.h> 22 + #include <linux/property.h> 21 23 #include <linux/regulator/consumer.h> 22 24 #include <linux/regmap.h> 23 25 ··· 1064 1062 fxls8962af_standby(iio_priv(indio_dev)); 1065 1063 } 1066 1064 1067 - static void fxls8962af_get_irq(struct device_node *of_node, 1065 + static void fxls8962af_get_irq(struct device *dev, 1068 1066 enum fxls8962af_int_pin *pin) 1069 1067 { 1070 1068 int irq; 1071 1069 1072 - irq = of_irq_get_byname(of_node, "INT2"); 1070 + irq = fwnode_irq_get_byname(dev_fwnode(dev), "INT2"); 1073 1071 if (irq > 0) { 1074 1072 *pin = FXLS8962AF_PIN_INT2; 1075 1073 return; ··· 1088 1086 u8 int_pin_sel; 1089 1087 int ret; 1090 1088 1091 - fxls8962af_get_irq(dev->of_node, &int_pin); 1089 + fxls8962af_get_irq(dev, &int_pin); 1092 1090 switch (int_pin) { 1093 1091 case FXLS8962AF_PIN_INT1: 1094 1092 int_pin_sel = FXLS8962AF_INT_PIN_SEL_INT1;
+3 -3
drivers/iio/accel/mma8452.c
··· 19 19 */ 20 20 21 21 #include <linux/module.h> 22 + #include <linux/mod_devicetable.h> 23 + #include <linux/property.h> 22 24 #include <linux/i2c.h> 23 25 #include <linux/iio/iio.h> 24 26 #include <linux/iio/sysfs.h> ··· 30 28 #include <linux/iio/triggered_buffer.h> 31 29 #include <linux/iio/events.h> 32 30 #include <linux/delay.h> 33 - #include <linux/of.h> 34 - #include <linux/of_irq.h> 35 31 #include <linux/pm_runtime.h> 36 32 #include <linux/regulator/consumer.h> 37 33 ··· 1642 1642 if (client->irq) { 1643 1643 int irq2; 1644 1644 1645 - irq2 = of_irq_get_byname(client->dev.of_node, "INT2"); 1645 + irq2 = fwnode_irq_get_byname(dev_fwnode(&client->dev), "INT2"); 1646 1646 1647 1647 if (irq2 == client->irq) { 1648 1648 dev_dbg(&client->dev, "using interrupt line INT2\n");
+27
drivers/iio/adc/Kconfig
··· 70 70 To compile this driver as a module, choose M here: the module will be 71 71 called ad7124. 72 72 73 + config AD7173 74 + tristate "Analog Devices AD7173 driver" 75 + depends on SPI_MASTER 76 + select AD_SIGMA_DELTA 77 + select GPIO_REGMAP if GPIOLIB 78 + select REGMAP_SPI if GPIOLIB 79 + help 80 + Say yes here to build support for Analog Devices AD7173 and similar ADC 81 + Currently supported models: 82 + - AD7172-2 83 + - AD7173-8 84 + - AD7175-2 85 + - AD7176-2 86 + 87 + To compile this driver as a module, choose M here: the module will be 88 + called ad7173. 89 + 73 90 config AD7192 74 91 tristate "Analog Devices AD7190 AD7192 AD7193 AD7195 ADC driver" 75 92 depends on SPI ··· 280 263 281 264 To compile this driver as a module, choose M here: the 282 265 module will be called ad7923. 266 + 267 + config AD7944 268 + tristate "Analog Devices AD7944 and similar ADCs driver" 269 + depends on SPI 270 + help 271 + Say yes here to build support for Analog Devices 272 + AD7944, AD7985, AD7986 ADCs. 273 + 274 + To compile this driver as a module, choose M here: the 275 + module will be called ad7944 283 276 284 277 config AD7949 285 278 tristate "Analog Devices AD7949 and similar ADCs driver"
+2
drivers/iio/adc/Makefile
··· 11 11 obj-$(CONFIG_AD7091R5) += ad7091r5.o 12 12 obj-$(CONFIG_AD7091R8) += ad7091r8.o 13 13 obj-$(CONFIG_AD7124) += ad7124.o 14 + obj-$(CONFIG_AD7173) += ad7173.o 14 15 obj-$(CONFIG_AD7192) += ad7192.o 15 16 obj-$(CONFIG_AD7266) += ad7266.o 16 17 obj-$(CONFIG_AD7280) += ad7280a.o ··· 29 28 obj-$(CONFIG_AD7791) += ad7791.o 30 29 obj-$(CONFIG_AD7793) += ad7793.o 31 30 obj-$(CONFIG_AD7887) += ad7887.o 31 + obj-$(CONFIG_AD7944) += ad7944.o 32 32 obj-$(CONFIG_AD7949) += ad7949.o 33 33 obj-$(CONFIG_AD799X) += ad799x.o 34 34 obj-$(CONFIG_AD9467) += ad9467.o
+3 -5
drivers/iio/adc/ab8500-gpadc.c
··· 1021 1021 /** 1022 1022 * ab8500_gpadc_parse_channels() - Parse the GPADC channels from DT 1023 1023 * @gpadc: the GPADC to configure the channels for 1024 - * @chans: the IIO channels we parsed 1025 - * @nchans: the number of IIO channels we parsed 1024 + * @chans_parsed: the IIO channels we parsed 1025 + * @nchans_parsed: the number of IIO channels we parsed 1026 1026 */ 1027 1027 static int ab8500_gpadc_parse_channels(struct ab8500_gpadc *gpadc, 1028 1028 struct iio_chan_spec **chans_parsed, 1029 1029 unsigned int *nchans_parsed) 1030 1030 { 1031 - struct fwnode_handle *child; 1032 1031 struct ab8500_gpadc_chan_info *ch; 1033 1032 struct iio_chan_spec *iio_chans; 1034 1033 unsigned int nchans; ··· 1051 1052 return -ENOMEM; 1052 1053 1053 1054 i = 0; 1054 - device_for_each_child_node(gpadc->dev, child) { 1055 + device_for_each_child_node_scoped(gpadc->dev, child) { 1055 1056 struct iio_chan_spec *iio_chan; 1056 1057 int ret; 1057 1058 ··· 1061 1062 ret = ab8500_gpadc_parse_channel(gpadc->dev, child, ch, 1062 1063 iio_chan); 1063 1064 if (ret) { 1064 - fwnode_handle_put(child); 1065 1065 return ret; 1066 1066 } 1067 1067 i++;
+2 -5
drivers/iio/adc/ad4130.c
··· 1600 1600 { 1601 1601 struct ad4130_state *st = iio_priv(indio_dev); 1602 1602 struct device *dev = &st->spi->dev; 1603 - struct fwnode_handle *child; 1604 1603 int ret; 1605 1604 1606 1605 indio_dev->channels = st->chans; 1607 1606 1608 - device_for_each_child_node(dev, child) { 1607 + device_for_each_child_node_scoped(dev, child) { 1609 1608 ret = ad4130_parse_fw_channel(indio_dev, child); 1610 - if (ret) { 1611 - fwnode_handle_put(child); 1609 + if (ret) 1612 1610 return ret; 1613 - } 1614 1611 } 1615 1612 1616 1613 return 0;
+23 -32
drivers/iio/adc/ad7124.c
··· 14 14 #include <linux/kernel.h> 15 15 #include <linux/kfifo.h> 16 16 #include <linux/module.h> 17 - #include <linux/of.h> 17 + #include <linux/mod_devicetable.h> 18 + #include <linux/property.h> 18 19 #include <linux/regulator/consumer.h> 19 20 #include <linux/spi/spi.h> 20 21 ··· 808 807 return 0; 809 808 } 810 809 811 - static int ad7124_of_parse_channel_config(struct iio_dev *indio_dev, 812 - struct device_node *np) 810 + static int ad7124_parse_channel_config(struct iio_dev *indio_dev, 811 + struct device *dev) 813 812 { 814 813 struct ad7124_state *st = iio_priv(indio_dev); 815 814 struct ad7124_channel_config *cfg; 816 815 struct ad7124_channel *channels; 817 - struct device_node *child; 818 816 struct iio_chan_spec *chan; 819 817 unsigned int ain[2], channel = 0, tmp; 820 818 int ret; 821 819 822 - st->num_channels = of_get_available_child_count(np); 823 - if (!st->num_channels) { 824 - dev_err(indio_dev->dev.parent, "no channel children\n"); 825 - return -ENODEV; 826 - } 820 + st->num_channels = device_get_child_node_count(dev); 821 + if (!st->num_channels) 822 + return dev_err_probe(dev, -ENODEV, "no channel children\n"); 827 823 828 824 chan = devm_kcalloc(indio_dev->dev.parent, st->num_channels, 829 825 sizeof(*chan), GFP_KERNEL); ··· 836 838 indio_dev->num_channels = st->num_channels; 837 839 st->channels = channels; 838 840 839 - for_each_available_child_of_node(np, child) { 841 + device_for_each_child_node_scoped(dev, child) { 840 842 cfg = &st->channels[channel].cfg; 841 843 842 - ret = of_property_read_u32(child, "reg", &channel); 844 + ret = fwnode_property_read_u32(child, "reg", &channel); 843 845 if (ret) 844 - goto err; 846 + return ret; 845 847 846 - if (channel >= indio_dev->num_channels) { 847 - dev_err(indio_dev->dev.parent, 848 + if (channel >= indio_dev->num_channels) 849 + return dev_err_probe(dev, -EINVAL, 848 850 "Channel index >= number of channels\n"); 849 - ret = -EINVAL; 850 - goto err; 851 - } 852 851 853 - ret = of_property_read_u32_array(child, "diff-channels", 854 - ain, 2); 852 + ret = fwnode_property_read_u32_array(child, "diff-channels", 853 + ain, 2); 855 854 if (ret) 856 - goto err; 855 + return ret; 857 856 858 857 st->channels[channel].nr = channel; 859 858 st->channels[channel].ain = AD7124_CHANNEL_AINP(ain[0]) | 860 859 AD7124_CHANNEL_AINM(ain[1]); 861 860 862 - cfg->bipolar = of_property_read_bool(child, "bipolar"); 861 + cfg->bipolar = fwnode_property_read_bool(child, "bipolar"); 863 862 864 - ret = of_property_read_u32(child, "adi,reference-select", &tmp); 863 + ret = fwnode_property_read_u32(child, "adi,reference-select", &tmp); 865 864 if (ret) 866 865 cfg->refsel = AD7124_INT_REF; 867 866 else 868 867 cfg->refsel = tmp; 869 868 870 - cfg->buf_positive = of_property_read_bool(child, "adi,buffered-positive"); 871 - cfg->buf_negative = of_property_read_bool(child, "adi,buffered-negative"); 869 + cfg->buf_positive = 870 + fwnode_property_read_bool(child, "adi,buffered-positive"); 871 + cfg->buf_negative = 872 + fwnode_property_read_bool(child, "adi,buffered-negative"); 872 873 873 874 chan[channel] = ad7124_channel_template; 874 875 chan[channel].address = channel; ··· 877 880 } 878 881 879 882 return 0; 880 - err: 881 - of_node_put(child); 882 - 883 - return ret; 884 883 } 885 884 886 885 static int ad7124_setup(struct ad7124_state *st) ··· 936 943 struct iio_dev *indio_dev; 937 944 int i, ret; 938 945 939 - info = of_device_get_match_data(&spi->dev); 940 - if (!info) 941 - info = (void *)spi_get_device_id(spi)->driver_data; 946 + info = spi_get_device_match_data(spi); 942 947 if (!info) 943 948 return -ENODEV; 944 949 ··· 956 965 if (ret < 0) 957 966 return ret; 958 967 959 - ret = ad7124_of_parse_channel_config(indio_dev, spi->dev.of_node); 968 + ret = ad7124_parse_channel_config(indio_dev, &spi->dev); 960 969 if (ret < 0) 961 970 return ret; 962 971
+1180
drivers/iio/adc/ad7173.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * AD717x family SPI ADC driver 4 + * 5 + * Supported devices: 6 + * AD7172-2/AD7172-4/AD7173-8/AD7175-2 7 + * AD7175-8/AD7176-2/AD7177-2 8 + * 9 + * Copyright (C) 2015, 2024 Analog Devices, Inc. 10 + */ 11 + 12 + #include <linux/array_size.h> 13 + #include <linux/bitfield.h> 14 + #include <linux/bitmap.h> 15 + #include <linux/container_of.h> 16 + #include <linux/clk.h> 17 + #include <linux/clk-provider.h> 18 + #include <linux/delay.h> 19 + #include <linux/device.h> 20 + #include <linux/err.h> 21 + #include <linux/gpio/driver.h> 22 + #include <linux/gpio/regmap.h> 23 + #include <linux/idr.h> 24 + #include <linux/interrupt.h> 25 + #include <linux/math64.h> 26 + #include <linux/module.h> 27 + #include <linux/mod_devicetable.h> 28 + #include <linux/property.h> 29 + #include <linux/regmap.h> 30 + #include <linux/regulator/consumer.h> 31 + #include <linux/slab.h> 32 + #include <linux/spi/spi.h> 33 + #include <linux/types.h> 34 + #include <linux/units.h> 35 + 36 + #include <linux/iio/buffer.h> 37 + #include <linux/iio/iio.h> 38 + #include <linux/iio/trigger_consumer.h> 39 + #include <linux/iio/triggered_buffer.h> 40 + 41 + #include <linux/iio/adc/ad_sigma_delta.h> 42 + 43 + #define AD7173_REG_COMMS 0x00 44 + #define AD7173_REG_ADC_MODE 0x01 45 + #define AD7173_REG_INTERFACE_MODE 0x02 46 + #define AD7173_REG_CRC 0x03 47 + #define AD7173_REG_DATA 0x04 48 + #define AD7173_REG_GPIO 0x06 49 + #define AD7173_REG_ID 0x07 50 + #define AD7173_REG_CH(x) (0x10 + (x)) 51 + #define AD7173_REG_SETUP(x) (0x20 + (x)) 52 + #define AD7173_REG_FILTER(x) (0x28 + (x)) 53 + #define AD7173_REG_OFFSET(x) (0x30 + (x)) 54 + #define AD7173_REG_GAIN(x) (0x38 + (x)) 55 + 56 + #define AD7173_RESET_LENGTH BITS_TO_BYTES(64) 57 + 58 + #define AD7173_CH_ENABLE BIT(15) 59 + #define AD7173_CH_SETUP_SEL_MASK GENMASK(14, 12) 60 + #define AD7173_CH_SETUP_AINPOS_MASK GENMASK(9, 5) 61 + #define AD7173_CH_SETUP_AINNEG_MASK GENMASK(4, 0) 62 + 63 + #define AD7173_CH_ADDRESS(pos, neg) \ 64 + (FIELD_PREP(AD7173_CH_SETUP_AINPOS_MASK, pos) | \ 65 + FIELD_PREP(AD7173_CH_SETUP_AINNEG_MASK, neg)) 66 + #define AD7173_AIN_TEMP_POS 17 67 + #define AD7173_AIN_TEMP_NEG 18 68 + 69 + #define AD7172_2_ID 0x00d0 70 + #define AD7175_ID 0x0cd0 71 + #define AD7176_ID 0x0c90 72 + #define AD7175_2_ID 0x0cd0 73 + #define AD7172_4_ID 0x2050 74 + #define AD7173_ID 0x30d0 75 + #define AD7175_8_ID 0x3cd0 76 + #define AD7177_ID 0x4fd0 77 + #define AD7173_ID_MASK GENMASK(15, 4) 78 + 79 + #define AD7173_ADC_MODE_REF_EN BIT(15) 80 + #define AD7173_ADC_MODE_SING_CYC BIT(13) 81 + #define AD7173_ADC_MODE_MODE_MASK GENMASK(6, 4) 82 + #define AD7173_ADC_MODE_CLOCKSEL_MASK GENMASK(3, 2) 83 + #define AD7173_ADC_MODE_CLOCKSEL_INT 0x0 84 + #define AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT 0x1 85 + #define AD7173_ADC_MODE_CLOCKSEL_EXT 0x2 86 + #define AD7173_ADC_MODE_CLOCKSEL_XTAL 0x3 87 + 88 + #define AD7173_GPIO_PDSW BIT(14) 89 + #define AD7173_GPIO_OP_EN2_3 BIT(13) 90 + #define AD7173_GPIO_MUX_IO BIT(12) 91 + #define AD7173_GPIO_SYNC_EN BIT(11) 92 + #define AD7173_GPIO_ERR_EN BIT(10) 93 + #define AD7173_GPIO_ERR_DAT BIT(9) 94 + #define AD7173_GPIO_GP_DATA3 BIT(7) 95 + #define AD7173_GPIO_GP_DATA2 BIT(6) 96 + #define AD7173_GPIO_IP_EN1 BIT(5) 97 + #define AD7173_GPIO_IP_EN0 BIT(4) 98 + #define AD7173_GPIO_OP_EN1 BIT(3) 99 + #define AD7173_GPIO_OP_EN0 BIT(2) 100 + #define AD7173_GPIO_GP_DATA1 BIT(1) 101 + #define AD7173_GPIO_GP_DATA0 BIT(0) 102 + 103 + #define AD7173_GPO12_DATA(x) BIT((x) + 0) 104 + #define AD7173_GPO23_DATA(x) BIT((x) + 4) 105 + #define AD7173_GPO_DATA(x) ((x) < 2 ? AD7173_GPO12_DATA(x) : AD7173_GPO23_DATA(x)) 106 + 107 + #define AD7173_INTERFACE_DATA_STAT BIT(6) 108 + #define AD7173_INTERFACE_DATA_STAT_EN(x) \ 109 + FIELD_PREP(AD7173_INTERFACE_DATA_STAT, x) 110 + 111 + #define AD7173_SETUP_BIPOLAR BIT(12) 112 + #define AD7173_SETUP_AREF_BUF_MASK GENMASK(11, 10) 113 + #define AD7173_SETUP_AIN_BUF_MASK GENMASK(9, 8) 114 + 115 + #define AD7173_SETUP_REF_SEL_MASK GENMASK(5, 4) 116 + #define AD7173_SETUP_REF_SEL_AVDD1_AVSS 0x3 117 + #define AD7173_SETUP_REF_SEL_INT_REF 0x2 118 + #define AD7173_SETUP_REF_SEL_EXT_REF2 0x1 119 + #define AD7173_SETUP_REF_SEL_EXT_REF 0x0 120 + #define AD7173_VOLTAGE_INT_REF_uV 2500000 121 + #define AD7173_TEMP_SENSIIVITY_uV_per_C 477 122 + #define AD7177_ODR_START_VALUE 0x07 123 + 124 + #define AD7173_FILTER_ODR0_MASK GENMASK(5, 0) 125 + #define AD7173_MAX_CONFIGS 8 126 + 127 + enum ad7173_ids { 128 + ID_AD7172_2, 129 + ID_AD7172_4, 130 + ID_AD7173_8, 131 + ID_AD7175_2, 132 + ID_AD7175_8, 133 + ID_AD7176_2, 134 + ID_AD7177_2, 135 + }; 136 + 137 + struct ad7173_device_info { 138 + const unsigned int *sinc5_data_rates; 139 + unsigned int num_sinc5_data_rates; 140 + unsigned int odr_start_value; 141 + unsigned int num_channels; 142 + unsigned int num_configs; 143 + unsigned int num_inputs; 144 + unsigned int clock; 145 + unsigned int id; 146 + char *name; 147 + bool has_temp; 148 + bool has_int_ref; 149 + bool has_ref2; 150 + u8 num_gpios; 151 + }; 152 + 153 + struct ad7173_channel_config { 154 + u8 cfg_slot; 155 + bool live; 156 + 157 + /* Following fields are used to compare equality. */ 158 + struct_group(config_props, 159 + bool bipolar; 160 + bool input_buf; 161 + u8 odr; 162 + u8 ref_sel; 163 + ); 164 + }; 165 + 166 + struct ad7173_channel { 167 + unsigned int chan_reg; 168 + unsigned int ain; 169 + struct ad7173_channel_config cfg; 170 + }; 171 + 172 + struct ad7173_state { 173 + struct ad_sigma_delta sd; 174 + const struct ad7173_device_info *info; 175 + struct ad7173_channel *channels; 176 + struct regulator_bulk_data regulators[3]; 177 + unsigned int adc_mode; 178 + unsigned int interface_mode; 179 + unsigned int num_channels; 180 + struct ida cfg_slots_status; 181 + unsigned long long config_usage_counter; 182 + unsigned long long *config_cnts; 183 + struct clk *ext_clk; 184 + struct clk_hw int_clk_hw; 185 + #if IS_ENABLED(CONFIG_GPIOLIB) 186 + struct regmap *reg_gpiocon_regmap; 187 + struct gpio_regmap *gpio_regmap; 188 + #endif 189 + }; 190 + 191 + static const unsigned int ad7173_sinc5_data_rates[] = { 192 + 6211000, 6211000, 6211000, 6211000, 6211000, 6211000, 5181000, 4444000, /* 0-7 */ 193 + 3115000, 2597000, 1007000, 503800, 381000, 200300, 100500, 59520, /* 8-15 */ 194 + 49680, 20010, 16333, 10000, 5000, 2500, 1250, /* 16-22 */ 195 + }; 196 + 197 + static const unsigned int ad7175_sinc5_data_rates[] = { 198 + 50000000, 41667000, 31250000, 27778000, /* 0-3 */ 199 + 20833000, 17857000, 12500000, 10000000, /* 4-7 */ 200 + 5000000, 2500000, 1000000, 500000, /* 8-11 */ 201 + 397500, 200000, 100000, 59920, /* 12-15 */ 202 + 49960, 20000, 16666, 10000, /* 16-19 */ 203 + 5000, /* 20 */ 204 + }; 205 + 206 + static const struct ad7173_device_info ad7173_device_info[] = { 207 + [ID_AD7172_2] = { 208 + .name = "ad7172-2", 209 + .id = AD7172_2_ID, 210 + .num_inputs = 5, 211 + .num_channels = 4, 212 + .num_configs = 4, 213 + .num_gpios = 2, 214 + .has_temp = true, 215 + .has_int_ref = true, 216 + .clock = 2 * HZ_PER_MHZ, 217 + .sinc5_data_rates = ad7173_sinc5_data_rates, 218 + .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates), 219 + }, 220 + [ID_AD7172_4] = { 221 + .id = AD7172_4_ID, 222 + .num_inputs = 9, 223 + .num_channels = 8, 224 + .num_configs = 8, 225 + .num_gpios = 4, 226 + .has_temp = false, 227 + .has_ref2 = true, 228 + .clock = 2 * HZ_PER_MHZ, 229 + .sinc5_data_rates = ad7173_sinc5_data_rates, 230 + .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates), 231 + }, 232 + [ID_AD7173_8] = { 233 + .name = "ad7173-8", 234 + .id = AD7173_ID, 235 + .num_inputs = 17, 236 + .num_channels = 16, 237 + .num_configs = 8, 238 + .num_gpios = 4, 239 + .has_temp = true, 240 + .has_int_ref = true, 241 + .has_ref2 = true, 242 + .clock = 2 * HZ_PER_MHZ, 243 + .sinc5_data_rates = ad7173_sinc5_data_rates, 244 + .num_sinc5_data_rates = ARRAY_SIZE(ad7173_sinc5_data_rates), 245 + }, 246 + [ID_AD7175_2] = { 247 + .name = "ad7175-2", 248 + .id = AD7175_2_ID, 249 + .num_inputs = 5, 250 + .num_channels = 4, 251 + .num_configs = 4, 252 + .num_gpios = 2, 253 + .has_temp = true, 254 + .has_int_ref = true, 255 + .clock = 16 * HZ_PER_MHZ, 256 + .sinc5_data_rates = ad7175_sinc5_data_rates, 257 + .num_sinc5_data_rates = ARRAY_SIZE(ad7175_sinc5_data_rates), 258 + }, 259 + [ID_AD7175_8] = { 260 + .id = AD7175_8_ID, 261 + .num_inputs = 17, 262 + .num_channels = 16, 263 + .num_configs = 8, 264 + .num_gpios = 4, 265 + .has_temp = true, 266 + .has_int_ref = true, 267 + .has_ref2 = true, 268 + .clock = 16 * HZ_PER_MHZ, 269 + .sinc5_data_rates = ad7175_sinc5_data_rates, 270 + .num_sinc5_data_rates = ARRAY_SIZE(ad7175_sinc5_data_rates), 271 + }, 272 + [ID_AD7176_2] = { 273 + .name = "ad7176-2", 274 + .id = AD7176_ID, 275 + .num_inputs = 5, 276 + .num_channels = 4, 277 + .num_configs = 4, 278 + .num_gpios = 2, 279 + .has_temp = false, 280 + .has_int_ref = true, 281 + .clock = 16 * HZ_PER_MHZ, 282 + .sinc5_data_rates = ad7175_sinc5_data_rates, 283 + .num_sinc5_data_rates = ARRAY_SIZE(ad7175_sinc5_data_rates), 284 + }, 285 + [ID_AD7177_2] = { 286 + .id = AD7177_ID, 287 + .num_inputs = 5, 288 + .num_channels = 4, 289 + .num_configs = 4, 290 + .num_gpios = 2, 291 + .has_temp = true, 292 + .has_int_ref = true, 293 + .clock = 16 * HZ_PER_MHZ, 294 + .odr_start_value = AD7177_ODR_START_VALUE, 295 + .sinc5_data_rates = ad7175_sinc5_data_rates, 296 + .num_sinc5_data_rates = ARRAY_SIZE(ad7175_sinc5_data_rates), 297 + }, 298 + }; 299 + 300 + static const char *const ad7173_ref_sel_str[] = { 301 + [AD7173_SETUP_REF_SEL_EXT_REF] = "vref", 302 + [AD7173_SETUP_REF_SEL_EXT_REF2] = "vref2", 303 + [AD7173_SETUP_REF_SEL_INT_REF] = "refout-avss", 304 + [AD7173_SETUP_REF_SEL_AVDD1_AVSS] = "avdd", 305 + }; 306 + 307 + static const char *const ad7173_clk_sel[] = { 308 + "ext-clk", "xtal" 309 + }; 310 + 311 + #if IS_ENABLED(CONFIG_GPIOLIB) 312 + 313 + static const struct regmap_range ad7173_range_gpio[] = { 314 + regmap_reg_range(AD7173_REG_GPIO, AD7173_REG_GPIO), 315 + }; 316 + 317 + static const struct regmap_access_table ad7173_access_table = { 318 + .yes_ranges = ad7173_range_gpio, 319 + .n_yes_ranges = ARRAY_SIZE(ad7173_range_gpio), 320 + }; 321 + 322 + static const struct regmap_config ad7173_regmap_config = { 323 + .reg_bits = 8, 324 + .val_bits = 16, 325 + .rd_table = &ad7173_access_table, 326 + .wr_table = &ad7173_access_table, 327 + .read_flag_mask = BIT(6), 328 + }; 329 + 330 + static int ad7173_mask_xlate(struct gpio_regmap *gpio, unsigned int base, 331 + unsigned int offset, unsigned int *reg, 332 + unsigned int *mask) 333 + { 334 + *mask = AD7173_GPO_DATA(offset); 335 + *reg = base; 336 + return 0; 337 + } 338 + 339 + static void ad7173_gpio_disable(void *data) 340 + { 341 + struct ad7173_state *st = data; 342 + unsigned int mask; 343 + 344 + mask = AD7173_GPIO_OP_EN0 | AD7173_GPIO_OP_EN1 | AD7173_GPIO_OP_EN2_3; 345 + regmap_update_bits(st->reg_gpiocon_regmap, AD7173_REG_GPIO, mask, ~mask); 346 + } 347 + 348 + static int ad7173_gpio_init(struct ad7173_state *st) 349 + { 350 + struct gpio_regmap_config gpio_regmap = {}; 351 + struct device *dev = &st->sd.spi->dev; 352 + unsigned int mask; 353 + int ret; 354 + 355 + st->reg_gpiocon_regmap = devm_regmap_init_spi(st->sd.spi, &ad7173_regmap_config); 356 + ret = PTR_ERR_OR_ZERO(st->reg_gpiocon_regmap); 357 + if (ret) 358 + return dev_err_probe(dev, ret, "Unable to init regmap\n"); 359 + 360 + mask = AD7173_GPIO_OP_EN0 | AD7173_GPIO_OP_EN1 | AD7173_GPIO_OP_EN2_3; 361 + regmap_update_bits(st->reg_gpiocon_regmap, AD7173_REG_GPIO, mask, mask); 362 + 363 + ret = devm_add_action_or_reset(dev, ad7173_gpio_disable, st); 364 + if (ret) 365 + return ret; 366 + 367 + gpio_regmap.parent = dev; 368 + gpio_regmap.regmap = st->reg_gpiocon_regmap; 369 + gpio_regmap.ngpio = st->info->num_gpios; 370 + gpio_regmap.reg_set_base = AD7173_REG_GPIO; 371 + gpio_regmap.reg_mask_xlate = ad7173_mask_xlate; 372 + 373 + st->gpio_regmap = devm_gpio_regmap_register(dev, &gpio_regmap); 374 + ret = PTR_ERR_OR_ZERO(st->gpio_regmap); 375 + if (ret) 376 + return dev_err_probe(dev, ret, "Unable to init gpio-regmap\n"); 377 + 378 + return 0; 379 + } 380 + #else 381 + static int ad7173_gpio_init(struct ad7173_state *st) 382 + { 383 + return 0; 384 + } 385 + #endif /* CONFIG_GPIOLIB */ 386 + 387 + static struct ad7173_state *ad_sigma_delta_to_ad7173(struct ad_sigma_delta *sd) 388 + { 389 + return container_of(sd, struct ad7173_state, sd); 390 + } 391 + 392 + static struct ad7173_state *clk_hw_to_ad7173(struct clk_hw *hw) 393 + { 394 + return container_of(hw, struct ad7173_state, int_clk_hw); 395 + } 396 + 397 + static void ad7173_ida_destroy(void *data) 398 + { 399 + struct ad7173_state *st = data; 400 + 401 + ida_destroy(&st->cfg_slots_status); 402 + } 403 + 404 + static void ad7173_reset_usage_cnts(struct ad7173_state *st) 405 + { 406 + memset64(st->config_cnts, 0, st->info->num_configs); 407 + st->config_usage_counter = 0; 408 + } 409 + 410 + static struct ad7173_channel_config * 411 + ad7173_find_live_config(struct ad7173_state *st, struct ad7173_channel_config *cfg) 412 + { 413 + struct ad7173_channel_config *cfg_aux; 414 + ptrdiff_t cmp_size; 415 + int i; 416 + 417 + cmp_size = sizeof_field(struct ad7173_channel_config, config_props); 418 + for (i = 0; i < st->num_channels; i++) { 419 + cfg_aux = &st->channels[i].cfg; 420 + 421 + if (cfg_aux->live && 422 + !memcmp(&cfg->config_props, &cfg_aux->config_props, cmp_size)) 423 + return cfg_aux; 424 + } 425 + return NULL; 426 + } 427 + 428 + /* Could be replaced with a generic LRU implementation */ 429 + static int ad7173_free_config_slot_lru(struct ad7173_state *st) 430 + { 431 + int i, lru_position = 0; 432 + 433 + for (i = 1; i < st->info->num_configs; i++) 434 + if (st->config_cnts[i] < st->config_cnts[lru_position]) 435 + lru_position = i; 436 + 437 + for (i = 0; i < st->num_channels; i++) 438 + if (st->channels[i].cfg.cfg_slot == lru_position) 439 + st->channels[i].cfg.live = false; 440 + 441 + ida_free(&st->cfg_slots_status, lru_position); 442 + return ida_alloc(&st->cfg_slots_status, GFP_KERNEL); 443 + } 444 + 445 + /* Could be replaced with a generic LRU implementation */ 446 + static int ad7173_load_config(struct ad7173_state *st, 447 + struct ad7173_channel_config *cfg) 448 + { 449 + unsigned int config; 450 + int free_cfg_slot, ret; 451 + 452 + free_cfg_slot = ida_alloc_range(&st->cfg_slots_status, 0, 453 + st->info->num_configs - 1, GFP_KERNEL); 454 + if (free_cfg_slot < 0) 455 + free_cfg_slot = ad7173_free_config_slot_lru(st); 456 + 457 + cfg->cfg_slot = free_cfg_slot; 458 + config = FIELD_PREP(AD7173_SETUP_REF_SEL_MASK, cfg->ref_sel); 459 + 460 + if (cfg->bipolar) 461 + config |= AD7173_SETUP_BIPOLAR; 462 + 463 + if (cfg->input_buf) 464 + config |= AD7173_SETUP_AIN_BUF_MASK; 465 + 466 + ret = ad_sd_write_reg(&st->sd, AD7173_REG_SETUP(free_cfg_slot), 2, config); 467 + if (ret) 468 + return ret; 469 + 470 + return ad_sd_write_reg(&st->sd, AD7173_REG_FILTER(free_cfg_slot), 2, 471 + AD7173_FILTER_ODR0_MASK & cfg->odr); 472 + } 473 + 474 + static int ad7173_config_channel(struct ad7173_state *st, int addr) 475 + { 476 + struct ad7173_channel_config *cfg = &st->channels[addr].cfg; 477 + struct ad7173_channel_config *live_cfg; 478 + int ret; 479 + 480 + if (!cfg->live) { 481 + live_cfg = ad7173_find_live_config(st, cfg); 482 + if (live_cfg) { 483 + cfg->cfg_slot = live_cfg->cfg_slot; 484 + } else { 485 + ret = ad7173_load_config(st, cfg); 486 + if (ret) 487 + return ret; 488 + cfg->live = true; 489 + } 490 + } 491 + 492 + if (st->config_usage_counter == U64_MAX) 493 + ad7173_reset_usage_cnts(st); 494 + 495 + st->config_usage_counter++; 496 + st->config_cnts[cfg->cfg_slot] = st->config_usage_counter; 497 + 498 + return 0; 499 + } 500 + 501 + static int ad7173_set_channel(struct ad_sigma_delta *sd, unsigned int channel) 502 + { 503 + struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd); 504 + unsigned int val; 505 + int ret; 506 + 507 + ret = ad7173_config_channel(st, channel); 508 + if (ret) 509 + return ret; 510 + 511 + val = AD7173_CH_ENABLE | 512 + FIELD_PREP(AD7173_CH_SETUP_SEL_MASK, st->channels[channel].cfg.cfg_slot) | 513 + st->channels[channel].ain; 514 + 515 + return ad_sd_write_reg(&st->sd, AD7173_REG_CH(channel), 2, val); 516 + } 517 + 518 + static int ad7173_set_mode(struct ad_sigma_delta *sd, 519 + enum ad_sigma_delta_mode mode) 520 + { 521 + struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd); 522 + 523 + st->adc_mode &= ~AD7173_ADC_MODE_MODE_MASK; 524 + st->adc_mode |= FIELD_PREP(AD7173_ADC_MODE_MODE_MASK, mode); 525 + 526 + return ad_sd_write_reg(&st->sd, AD7173_REG_ADC_MODE, 2, st->adc_mode); 527 + } 528 + 529 + static int ad7173_append_status(struct ad_sigma_delta *sd, bool append) 530 + { 531 + struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd); 532 + unsigned int interface_mode = st->interface_mode; 533 + int ret; 534 + 535 + interface_mode |= AD7173_INTERFACE_DATA_STAT_EN(append); 536 + ret = ad_sd_write_reg(&st->sd, AD7173_REG_INTERFACE_MODE, 2, interface_mode); 537 + if (ret) 538 + return ret; 539 + 540 + st->interface_mode = interface_mode; 541 + 542 + return 0; 543 + } 544 + 545 + static int ad7173_disable_all(struct ad_sigma_delta *sd) 546 + { 547 + struct ad7173_state *st = ad_sigma_delta_to_ad7173(sd); 548 + int ret; 549 + int i; 550 + 551 + for (i = 0; i < st->num_channels; i++) { 552 + ret = ad_sd_write_reg(sd, AD7173_REG_CH(i), 2, 0); 553 + if (ret < 0) 554 + return ret; 555 + } 556 + 557 + return 0; 558 + } 559 + 560 + static struct ad_sigma_delta_info ad7173_sigma_delta_info = { 561 + .set_channel = ad7173_set_channel, 562 + .append_status = ad7173_append_status, 563 + .disable_all = ad7173_disable_all, 564 + .set_mode = ad7173_set_mode, 565 + .has_registers = true, 566 + .addr_shift = 0, 567 + .read_mask = BIT(6), 568 + .status_ch_mask = GENMASK(3, 0), 569 + .data_reg = AD7173_REG_DATA, 570 + }; 571 + 572 + static int ad7173_setup(struct iio_dev *indio_dev) 573 + { 574 + struct ad7173_state *st = iio_priv(indio_dev); 575 + struct device *dev = &st->sd.spi->dev; 576 + u8 buf[AD7173_RESET_LENGTH]; 577 + unsigned int id; 578 + int ret; 579 + 580 + /* reset the serial interface */ 581 + memset(buf, 0xff, AD7173_RESET_LENGTH); 582 + ret = spi_write_then_read(st->sd.spi, buf, sizeof(buf), NULL, 0); 583 + if (ret < 0) 584 + return ret; 585 + 586 + /* datasheet recommends a delay of at least 500us after reset */ 587 + fsleep(500); 588 + 589 + ret = ad_sd_read_reg(&st->sd, AD7173_REG_ID, 2, &id); 590 + if (ret) 591 + return ret; 592 + 593 + id &= AD7173_ID_MASK; 594 + if (id != st->info->id) 595 + dev_warn(dev, "Unexpected device id: 0x%04X, expected: 0x%04X\n", 596 + id, st->info->id); 597 + 598 + st->adc_mode |= AD7173_ADC_MODE_SING_CYC; 599 + st->interface_mode = 0x0; 600 + 601 + st->config_usage_counter = 0; 602 + st->config_cnts = devm_kcalloc(dev, st->info->num_configs, 603 + sizeof(*st->config_cnts), GFP_KERNEL); 604 + if (!st->config_cnts) 605 + return -ENOMEM; 606 + 607 + /* All channels are enabled by default after a reset */ 608 + return ad7173_disable_all(&st->sd); 609 + } 610 + 611 + static unsigned int ad7173_get_ref_voltage_milli(struct ad7173_state *st, 612 + u8 reference_select) 613 + { 614 + int vref; 615 + 616 + switch (reference_select) { 617 + case AD7173_SETUP_REF_SEL_EXT_REF: 618 + vref = regulator_get_voltage(st->regulators[0].consumer); 619 + break; 620 + 621 + case AD7173_SETUP_REF_SEL_EXT_REF2: 622 + vref = regulator_get_voltage(st->regulators[1].consumer); 623 + break; 624 + 625 + case AD7173_SETUP_REF_SEL_INT_REF: 626 + vref = AD7173_VOLTAGE_INT_REF_uV; 627 + break; 628 + 629 + case AD7173_SETUP_REF_SEL_AVDD1_AVSS: 630 + vref = regulator_get_voltage(st->regulators[2].consumer); 631 + break; 632 + 633 + default: 634 + return -EINVAL; 635 + } 636 + 637 + if (vref < 0) 638 + return vref; 639 + 640 + return vref / (MICRO / MILLI); 641 + } 642 + 643 + static int ad7173_read_raw(struct iio_dev *indio_dev, 644 + struct iio_chan_spec const *chan, 645 + int *val, int *val2, long info) 646 + { 647 + struct ad7173_state *st = iio_priv(indio_dev); 648 + struct ad7173_channel *ch = &st->channels[chan->address]; 649 + unsigned int reg; 650 + u64 temp; 651 + int ret; 652 + 653 + switch (info) { 654 + case IIO_CHAN_INFO_RAW: 655 + ret = ad_sigma_delta_single_conversion(indio_dev, chan, val); 656 + if (ret < 0) 657 + return ret; 658 + 659 + /* disable channel after single conversion */ 660 + ret = ad_sd_write_reg(&st->sd, AD7173_REG_CH(chan->address), 2, 0); 661 + if (ret < 0) 662 + return ret; 663 + 664 + return IIO_VAL_INT; 665 + case IIO_CHAN_INFO_SCALE: 666 + if (chan->type == IIO_TEMP) { 667 + temp = AD7173_VOLTAGE_INT_REF_uV * MILLI; 668 + temp /= AD7173_TEMP_SENSIIVITY_uV_per_C; 669 + *val = temp; 670 + *val2 = chan->scan_type.realbits; 671 + } else { 672 + *val = ad7173_get_ref_voltage_milli(st, ch->cfg.ref_sel); 673 + *val2 = chan->scan_type.realbits - !!(ch->cfg.bipolar); 674 + } 675 + return IIO_VAL_FRACTIONAL_LOG2; 676 + case IIO_CHAN_INFO_OFFSET: 677 + if (chan->type == IIO_TEMP) { 678 + /* 0 Kelvin -> raw sample */ 679 + temp = -ABSOLUTE_ZERO_MILLICELSIUS; 680 + temp *= AD7173_TEMP_SENSIIVITY_uV_per_C; 681 + temp <<= chan->scan_type.realbits; 682 + temp = DIV_U64_ROUND_CLOSEST(temp, 683 + AD7173_VOLTAGE_INT_REF_uV * 684 + MILLI); 685 + *val = -temp; 686 + } else { 687 + *val = -BIT(chan->scan_type.realbits - 1); 688 + } 689 + return IIO_VAL_INT; 690 + case IIO_CHAN_INFO_SAMP_FREQ: 691 + reg = st->channels[chan->address].cfg.odr; 692 + 693 + *val = st->info->sinc5_data_rates[reg] / MILLI; 694 + *val2 = (st->info->sinc5_data_rates[reg] % MILLI) * (MICRO / MILLI); 695 + 696 + return IIO_VAL_INT_PLUS_MICRO; 697 + default: 698 + return -EINVAL; 699 + } 700 + } 701 + 702 + static int ad7173_write_raw(struct iio_dev *indio_dev, 703 + struct iio_chan_spec const *chan, 704 + int val, int val2, long info) 705 + { 706 + struct ad7173_state *st = iio_priv(indio_dev); 707 + struct ad7173_channel_config *cfg; 708 + unsigned int freq, i, reg; 709 + int ret; 710 + 711 + ret = iio_device_claim_direct_mode(indio_dev); 712 + if (ret) 713 + return ret; 714 + 715 + switch (info) { 716 + case IIO_CHAN_INFO_SAMP_FREQ: 717 + freq = val * MILLI + val2 / MILLI; 718 + for (i = st->info->odr_start_value; i < st->info->num_sinc5_data_rates - 1; i++) 719 + if (freq >= st->info->sinc5_data_rates[i]) 720 + break; 721 + 722 + cfg = &st->channels[chan->address].cfg; 723 + cfg->odr = i; 724 + 725 + if (!cfg->live) 726 + break; 727 + 728 + ret = ad_sd_read_reg(&st->sd, AD7173_REG_FILTER(cfg->cfg_slot), 2, &reg); 729 + if (ret) 730 + break; 731 + reg &= ~AD7173_FILTER_ODR0_MASK; 732 + reg |= FIELD_PREP(AD7173_FILTER_ODR0_MASK, i); 733 + ret = ad_sd_write_reg(&st->sd, AD7173_REG_FILTER(cfg->cfg_slot), 2, reg); 734 + break; 735 + 736 + default: 737 + ret = -EINVAL; 738 + break; 739 + } 740 + 741 + iio_device_release_direct_mode(indio_dev); 742 + return ret; 743 + } 744 + 745 + static int ad7173_update_scan_mode(struct iio_dev *indio_dev, 746 + const unsigned long *scan_mask) 747 + { 748 + struct ad7173_state *st = iio_priv(indio_dev); 749 + int i, ret; 750 + 751 + for (i = 0; i < indio_dev->num_channels; i++) { 752 + if (test_bit(i, scan_mask)) 753 + ret = ad7173_set_channel(&st->sd, i); 754 + else 755 + ret = ad_sd_write_reg(&st->sd, AD7173_REG_CH(i), 2, 0); 756 + if (ret < 0) 757 + return ret; 758 + } 759 + 760 + return 0; 761 + } 762 + 763 + static int ad7173_debug_reg_access(struct iio_dev *indio_dev, unsigned int reg, 764 + unsigned int writeval, unsigned int *readval) 765 + { 766 + struct ad7173_state *st = iio_priv(indio_dev); 767 + u8 reg_size; 768 + 769 + if (reg == AD7173_REG_COMMS) 770 + reg_size = 1; 771 + else if (reg == AD7173_REG_CRC || reg == AD7173_REG_DATA || 772 + reg >= AD7173_REG_OFFSET(0)) 773 + reg_size = 3; 774 + else 775 + reg_size = 2; 776 + 777 + if (readval) 778 + return ad_sd_read_reg(&st->sd, reg, reg_size, readval); 779 + 780 + return ad_sd_write_reg(&st->sd, reg, reg_size, writeval); 781 + } 782 + 783 + static const struct iio_info ad7173_info = { 784 + .read_raw = &ad7173_read_raw, 785 + .write_raw = &ad7173_write_raw, 786 + .debugfs_reg_access = &ad7173_debug_reg_access, 787 + .validate_trigger = ad_sd_validate_trigger, 788 + .update_scan_mode = ad7173_update_scan_mode, 789 + }; 790 + 791 + static const struct iio_chan_spec ad7173_channel_template = { 792 + .type = IIO_VOLTAGE, 793 + .indexed = 1, 794 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 795 + BIT(IIO_CHAN_INFO_SCALE), 796 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 797 + .scan_type = { 798 + .sign = 'u', 799 + .realbits = 24, 800 + .storagebits = 32, 801 + .endianness = IIO_BE, 802 + }, 803 + }; 804 + 805 + static const struct iio_chan_spec ad7173_temp_iio_channel_template = { 806 + .type = IIO_TEMP, 807 + .indexed = 1, 808 + .channel = AD7173_AIN_TEMP_POS, 809 + .channel2 = AD7173_AIN_TEMP_NEG, 810 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 811 + BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), 812 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 813 + .scan_type = { 814 + .sign = 'u', 815 + .realbits = 24, 816 + .storagebits = 32, 817 + .endianness = IIO_BE, 818 + }, 819 + }; 820 + 821 + static void ad7173_disable_regulators(void *data) 822 + { 823 + struct ad7173_state *st = data; 824 + 825 + regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators); 826 + } 827 + 828 + static void ad7173_clk_disable_unprepare(void *clk) 829 + { 830 + clk_disable_unprepare(clk); 831 + } 832 + 833 + static unsigned long ad7173_sel_clk(struct ad7173_state *st, 834 + unsigned int clk_sel) 835 + { 836 + int ret; 837 + 838 + st->adc_mode &= ~AD7173_ADC_MODE_CLOCKSEL_MASK; 839 + st->adc_mode |= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK, clk_sel); 840 + ret = ad_sd_write_reg(&st->sd, AD7173_REG_ADC_MODE, 0x2, st->adc_mode); 841 + 842 + return ret; 843 + } 844 + 845 + static unsigned long ad7173_clk_recalc_rate(struct clk_hw *hw, 846 + unsigned long parent_rate) 847 + { 848 + struct ad7173_state *st = clk_hw_to_ad7173(hw); 849 + 850 + return st->info->clock / HZ_PER_KHZ; 851 + } 852 + 853 + static int ad7173_clk_output_is_enabled(struct clk_hw *hw) 854 + { 855 + struct ad7173_state *st = clk_hw_to_ad7173(hw); 856 + u32 clk_sel; 857 + 858 + clk_sel = FIELD_GET(AD7173_ADC_MODE_CLOCKSEL_MASK, st->adc_mode); 859 + return clk_sel == AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT; 860 + } 861 + 862 + static int ad7173_clk_output_prepare(struct clk_hw *hw) 863 + { 864 + struct ad7173_state *st = clk_hw_to_ad7173(hw); 865 + 866 + return ad7173_sel_clk(st, AD7173_ADC_MODE_CLOCKSEL_INT_OUTPUT); 867 + } 868 + 869 + static void ad7173_clk_output_unprepare(struct clk_hw *hw) 870 + { 871 + struct ad7173_state *st = clk_hw_to_ad7173(hw); 872 + 873 + ad7173_sel_clk(st, AD7173_ADC_MODE_CLOCKSEL_INT); 874 + } 875 + 876 + static const struct clk_ops ad7173_int_clk_ops = { 877 + .recalc_rate = ad7173_clk_recalc_rate, 878 + .is_enabled = ad7173_clk_output_is_enabled, 879 + .prepare = ad7173_clk_output_prepare, 880 + .unprepare = ad7173_clk_output_unprepare, 881 + }; 882 + 883 + static int ad7173_register_clk_provider(struct iio_dev *indio_dev) 884 + { 885 + struct ad7173_state *st = iio_priv(indio_dev); 886 + struct device *dev = indio_dev->dev.parent; 887 + struct fwnode_handle *fwnode = dev_fwnode(dev); 888 + struct clk_init_data init = {}; 889 + int ret; 890 + 891 + if (!IS_ENABLED(CONFIG_COMMON_CLK)) 892 + return 0; 893 + 894 + init.name = fwnode_get_name(fwnode); 895 + init.ops = &ad7173_int_clk_ops; 896 + 897 + st->int_clk_hw.init = &init; 898 + ret = devm_clk_hw_register(dev, &st->int_clk_hw); 899 + if (ret) 900 + return ret; 901 + 902 + return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, 903 + &st->int_clk_hw); 904 + } 905 + 906 + static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev) 907 + { 908 + struct ad7173_channel *chans_st_arr, *chan_st_priv; 909 + struct ad7173_state *st = iio_priv(indio_dev); 910 + struct device *dev = indio_dev->dev.parent; 911 + struct iio_chan_spec *chan_arr, *chan; 912 + unsigned int ain[2], chan_index = 0; 913 + int ref_sel, ret; 914 + 915 + chan_arr = devm_kcalloc(dev, sizeof(*indio_dev->channels), 916 + st->num_channels, GFP_KERNEL); 917 + if (!chan_arr) 918 + return -ENOMEM; 919 + 920 + chans_st_arr = devm_kcalloc(dev, st->num_channels, sizeof(*st->channels), 921 + GFP_KERNEL); 922 + if (!chans_st_arr) 923 + return -ENOMEM; 924 + 925 + indio_dev->channels = chan_arr; 926 + st->channels = chans_st_arr; 927 + 928 + if (st->info->has_temp) { 929 + chan_arr[chan_index] = ad7173_temp_iio_channel_template; 930 + chan_st_priv = &chans_st_arr[chan_index]; 931 + chan_st_priv->ain = 932 + AD7173_CH_ADDRESS(chan_arr[chan_index].channel, 933 + chan_arr[chan_index].channel2); 934 + chan_st_priv->cfg.bipolar = false; 935 + chan_st_priv->cfg.input_buf = true; 936 + chan_st_priv->cfg.ref_sel = AD7173_SETUP_REF_SEL_INT_REF; 937 + st->adc_mode |= AD7173_ADC_MODE_REF_EN; 938 + 939 + chan_index++; 940 + } 941 + 942 + device_for_each_child_node_scoped(dev, child) { 943 + chan = &chan_arr[chan_index]; 944 + chan_st_priv = &chans_st_arr[chan_index]; 945 + ret = fwnode_property_read_u32_array(child, "diff-channels", 946 + ain, ARRAY_SIZE(ain)); 947 + if (ret) 948 + return ret; 949 + 950 + if (ain[0] >= st->info->num_inputs || 951 + ain[1] >= st->info->num_inputs) 952 + return dev_err_probe(dev, -EINVAL, 953 + "Input pin number out of range for pair (%d %d).\n", 954 + ain[0], ain[1]); 955 + 956 + ret = fwnode_property_match_property_string(child, 957 + "adi,reference-select", 958 + ad7173_ref_sel_str, 959 + ARRAY_SIZE(ad7173_ref_sel_str)); 960 + if (ret < 0) 961 + ref_sel = AD7173_SETUP_REF_SEL_INT_REF; 962 + else 963 + ref_sel = ret; 964 + 965 + if (ref_sel == AD7173_SETUP_REF_SEL_INT_REF && 966 + !st->info->has_int_ref) 967 + return dev_err_probe(dev, -EINVAL, 968 + "Internal reference is not available on current model.\n"); 969 + 970 + if (ref_sel == AD7173_SETUP_REF_SEL_EXT_REF2 && !st->info->has_ref2) 971 + return dev_err_probe(dev, -EINVAL, 972 + "External reference 2 is not available on current model.\n"); 973 + 974 + ret = ad7173_get_ref_voltage_milli(st, ref_sel); 975 + if (ret < 0) 976 + return dev_err_probe(dev, ret, 977 + "Cannot use reference %u\n", ref_sel); 978 + 979 + if (ref_sel == AD7173_SETUP_REF_SEL_INT_REF) 980 + st->adc_mode |= AD7173_ADC_MODE_REF_EN; 981 + chan_st_priv->cfg.ref_sel = ref_sel; 982 + 983 + *chan = ad7173_channel_template; 984 + chan->address = chan_index; 985 + chan->scan_index = chan_index; 986 + chan->channel = ain[0]; 987 + chan->channel2 = ain[1]; 988 + chan->differential = true; 989 + 990 + chan_st_priv->ain = AD7173_CH_ADDRESS(ain[0], ain[1]); 991 + chan_st_priv->chan_reg = chan_index; 992 + chan_st_priv->cfg.input_buf = true; 993 + chan_st_priv->cfg.odr = 0; 994 + 995 + chan_st_priv->cfg.bipolar = fwnode_property_read_bool(child, "bipolar"); 996 + if (chan_st_priv->cfg.bipolar) 997 + chan->info_mask_separate |= BIT(IIO_CHAN_INFO_OFFSET); 998 + 999 + chan_index++; 1000 + } 1001 + return 0; 1002 + } 1003 + 1004 + static int ad7173_fw_parse_device_config(struct iio_dev *indio_dev) 1005 + { 1006 + struct ad7173_state *st = iio_priv(indio_dev); 1007 + struct device *dev = indio_dev->dev.parent; 1008 + unsigned int num_channels; 1009 + int ret; 1010 + 1011 + st->regulators[0].supply = ad7173_ref_sel_str[AD7173_SETUP_REF_SEL_EXT_REF]; 1012 + st->regulators[1].supply = ad7173_ref_sel_str[AD7173_SETUP_REF_SEL_EXT_REF2]; 1013 + st->regulators[2].supply = ad7173_ref_sel_str[AD7173_SETUP_REF_SEL_AVDD1_AVSS]; 1014 + 1015 + /* 1016 + * If a regulator is not available, it will be set to a dummy regulator. 1017 + * Each channel reference is checked with regulator_get_voltage() before 1018 + * setting attributes so if any channel uses a dummy supply the driver 1019 + * probe will fail. 1020 + */ 1021 + ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators), 1022 + st->regulators); 1023 + if (ret) 1024 + return dev_err_probe(dev, ret, "Failed to get regulators\n"); 1025 + 1026 + ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators); 1027 + if (ret) 1028 + return dev_err_probe(dev, ret, "Failed to enable regulators\n"); 1029 + 1030 + ret = devm_add_action_or_reset(dev, ad7173_disable_regulators, st); 1031 + if (ret) 1032 + return dev_err_probe(dev, ret, 1033 + "Failed to add regulators disable action\n"); 1034 + 1035 + ret = device_property_match_property_string(dev, "clock-names", 1036 + ad7173_clk_sel, 1037 + ARRAY_SIZE(ad7173_clk_sel)); 1038 + if (ret < 0) { 1039 + st->adc_mode |= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK, 1040 + AD7173_ADC_MODE_CLOCKSEL_INT); 1041 + ad7173_register_clk_provider(indio_dev); 1042 + } else { 1043 + st->adc_mode |= FIELD_PREP(AD7173_ADC_MODE_CLOCKSEL_MASK, 1044 + AD7173_ADC_MODE_CLOCKSEL_EXT + ret); 1045 + st->ext_clk = devm_clk_get(dev, ad7173_clk_sel[ret]); 1046 + if (IS_ERR(st->ext_clk)) 1047 + return dev_err_probe(dev, PTR_ERR(st->ext_clk), 1048 + "Failed to get external clock\n"); 1049 + 1050 + ret = clk_prepare_enable(st->ext_clk); 1051 + if (ret) 1052 + return dev_err_probe(dev, ret, 1053 + "Failed to enable external clock\n"); 1054 + 1055 + ret = devm_add_action_or_reset(dev, ad7173_clk_disable_unprepare, 1056 + st->ext_clk); 1057 + if (ret) 1058 + return ret; 1059 + } 1060 + 1061 + ret = fwnode_irq_get_byname(dev_fwnode(dev), "rdy"); 1062 + if (ret < 0) 1063 + return dev_err_probe(dev, ret, "Interrupt 'rdy' is required\n"); 1064 + 1065 + ad7173_sigma_delta_info.irq_line = ret; 1066 + 1067 + num_channels = device_get_child_node_count(dev); 1068 + 1069 + if (st->info->has_temp) 1070 + num_channels++; 1071 + 1072 + if (num_channels == 0) 1073 + return dev_err_probe(dev, -ENODATA, "No channels specified\n"); 1074 + indio_dev->num_channels = num_channels; 1075 + st->num_channels = num_channels; 1076 + 1077 + return ad7173_fw_parse_channel_config(indio_dev); 1078 + } 1079 + 1080 + static int ad7173_probe(struct spi_device *spi) 1081 + { 1082 + struct device *dev = &spi->dev; 1083 + struct ad7173_state *st; 1084 + struct iio_dev *indio_dev; 1085 + int ret; 1086 + 1087 + indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 1088 + if (!indio_dev) 1089 + return -ENOMEM; 1090 + 1091 + st = iio_priv(indio_dev); 1092 + st->info = spi_get_device_match_data(spi); 1093 + if (!st->info) 1094 + return -ENODEV; 1095 + 1096 + ida_init(&st->cfg_slots_status); 1097 + ret = devm_add_action_or_reset(dev, ad7173_ida_destroy, st); 1098 + if (ret) 1099 + return ret; 1100 + 1101 + indio_dev->name = st->info->name; 1102 + indio_dev->modes = INDIO_DIRECT_MODE; 1103 + indio_dev->info = &ad7173_info; 1104 + 1105 + spi->mode = SPI_MODE_3; 1106 + spi_setup(spi); 1107 + 1108 + ad7173_sigma_delta_info.num_slots = st->info->num_configs; 1109 + ret = ad_sd_init(&st->sd, indio_dev, spi, &ad7173_sigma_delta_info); 1110 + if (ret) 1111 + return ret; 1112 + 1113 + ret = ad7173_fw_parse_device_config(indio_dev); 1114 + if (ret) 1115 + return ret; 1116 + 1117 + ret = devm_ad_sd_setup_buffer_and_trigger(dev, indio_dev); 1118 + if (ret) 1119 + return ret; 1120 + 1121 + ret = ad7173_setup(indio_dev); 1122 + if (ret) 1123 + return ret; 1124 + 1125 + ret = devm_iio_device_register(dev, indio_dev); 1126 + if (ret) 1127 + return ret; 1128 + 1129 + if (IS_ENABLED(CONFIG_GPIOLIB)) 1130 + return ad7173_gpio_init(st); 1131 + 1132 + return 0; 1133 + } 1134 + 1135 + static const struct of_device_id ad7173_of_match[] = { 1136 + { .compatible = "adi,ad7172-2", 1137 + .data = &ad7173_device_info[ID_AD7172_2]}, 1138 + { .compatible = "adi,ad7172-4", 1139 + .data = &ad7173_device_info[ID_AD7172_4]}, 1140 + { .compatible = "adi,ad7173-8", 1141 + .data = &ad7173_device_info[ID_AD7173_8]}, 1142 + { .compatible = "adi,ad7175-2", 1143 + .data = &ad7173_device_info[ID_AD7175_2]}, 1144 + { .compatible = "adi,ad7175-8", 1145 + .data = &ad7173_device_info[ID_AD7175_8]}, 1146 + { .compatible = "adi,ad7176-2", 1147 + .data = &ad7173_device_info[ID_AD7176_2]}, 1148 + { .compatible = "adi,ad7177-2", 1149 + .data = &ad7173_device_info[ID_AD7177_2]}, 1150 + { } 1151 + }; 1152 + MODULE_DEVICE_TABLE(of, ad7173_of_match); 1153 + 1154 + static const struct spi_device_id ad7173_id_table[] = { 1155 + { "ad7172-2", (kernel_ulong_t)&ad7173_device_info[ID_AD7172_2]}, 1156 + { "ad7172-4", (kernel_ulong_t)&ad7173_device_info[ID_AD7172_4]}, 1157 + { "ad7173-8", (kernel_ulong_t)&ad7173_device_info[ID_AD7173_8]}, 1158 + { "ad7175-2", (kernel_ulong_t)&ad7173_device_info[ID_AD7175_2]}, 1159 + { "ad7175-8", (kernel_ulong_t)&ad7173_device_info[ID_AD7175_8]}, 1160 + { "ad7176-2", (kernel_ulong_t)&ad7173_device_info[ID_AD7176_2]}, 1161 + { "ad7177-2", (kernel_ulong_t)&ad7173_device_info[ID_AD7177_2]}, 1162 + { } 1163 + }; 1164 + MODULE_DEVICE_TABLE(spi, ad7173_id_table); 1165 + 1166 + static struct spi_driver ad7173_driver = { 1167 + .driver = { 1168 + .name = "ad7173", 1169 + .of_match_table = ad7173_of_match, 1170 + }, 1171 + .probe = ad7173_probe, 1172 + .id_table = ad7173_id_table, 1173 + }; 1174 + module_spi_driver(ad7173_driver); 1175 + 1176 + MODULE_IMPORT_NS(IIO_AD_SIGMA_DELTA); 1177 + MODULE_AUTHOR("Lars-Peter Clausen <lars@metafo.de>"); 1178 + MODULE_AUTHOR("Dumitru Ceclan <dumitru.ceclan@analog.com>"); 1179 + MODULE_DESCRIPTION("Analog Devices AD7172/AD7173/AD7175/AD7176 ADC driver"); 1180 + MODULE_LICENSE("GPL");
+19 -19
drivers/iio/adc/ad7192.c
··· 17 17 #include <linux/err.h> 18 18 #include <linux/sched.h> 19 19 #include <linux/delay.h> 20 - #include <linux/of.h> 20 + #include <linux/module.h> 21 + #include <linux/mod_devicetable.h> 22 + #include <linux/property.h> 21 23 22 24 #include <linux/iio/iio.h> 23 25 #include <linux/iio/sysfs.h> ··· 366 364 freq <= AD7192_EXT_FREQ_MHZ_MAX); 367 365 } 368 366 369 - static int ad7192_of_clock_select(struct ad7192_state *st) 367 + static int ad7192_clock_select(struct ad7192_state *st) 370 368 { 371 - struct device_node *np = st->sd.spi->dev.of_node; 369 + struct device *dev = &st->sd.spi->dev; 372 370 unsigned int clock_sel; 373 371 374 372 clock_sel = AD7192_CLK_INT; 375 373 376 374 /* use internal clock */ 377 375 if (!st->mclk) { 378 - if (of_property_read_bool(np, "adi,int-clock-output-enable")) 376 + if (device_property_read_bool(dev, "adi,int-clock-output-enable")) 379 377 clock_sel = AD7192_CLK_INT_CO; 380 378 } else { 381 - if (of_property_read_bool(np, "adi,clock-xtal")) 379 + if (device_property_read_bool(dev, "adi,clock-xtal")) 382 380 clock_sel = AD7192_CLK_EXT_MCLK1_2; 383 381 else 384 382 clock_sel = AD7192_CLK_EXT_MCLK2; ··· 387 385 return clock_sel; 388 386 } 389 387 390 - static int ad7192_setup(struct iio_dev *indio_dev, struct device_node *np) 388 + static int ad7192_setup(struct iio_dev *indio_dev, struct device *dev) 391 389 { 392 390 struct ad7192_state *st = iio_priv(indio_dev); 393 391 bool rej60_en, refin2_en; ··· 409 407 id = FIELD_GET(AD7192_ID_MASK, id); 410 408 411 409 if (id != st->chip_info->chip_id) 412 - dev_warn(&st->sd.spi->dev, "device ID query failed (0x%X != 0x%X)\n", 410 + dev_warn(dev, "device ID query failed (0x%X != 0x%X)\n", 413 411 id, st->chip_info->chip_id); 414 412 415 413 st->mode = FIELD_PREP(AD7192_MODE_SEL_MASK, AD7192_MODE_IDLE) | ··· 418 416 419 417 st->conf = FIELD_PREP(AD7192_CONF_GAIN_MASK, 0); 420 418 421 - rej60_en = of_property_read_bool(np, "adi,rejection-60-Hz-enable"); 419 + rej60_en = device_property_read_bool(dev, "adi,rejection-60-Hz-enable"); 422 420 if (rej60_en) 423 421 st->mode |= AD7192_MODE_REJ60; 424 422 425 - refin2_en = of_property_read_bool(np, "adi,refin2-pins-enable"); 423 + refin2_en = device_property_read_bool(dev, "adi,refin2-pins-enable"); 426 424 if (refin2_en && st->chip_info->chip_id != CHIPID_AD7195) 427 425 st->conf |= AD7192_CONF_REFSEL; 428 426 429 427 st->conf &= ~AD7192_CONF_CHOP; 430 428 431 - buf_en = of_property_read_bool(np, "adi,buffer-enable"); 429 + buf_en = device_property_read_bool(dev, "adi,buffer-enable"); 432 430 if (buf_en) 433 431 st->conf |= AD7192_CONF_BUF; 434 432 435 - bipolar = of_property_read_bool(np, "bipolar"); 433 + bipolar = device_property_read_bool(dev, "bipolar"); 436 434 if (!bipolar) 437 435 st->conf |= AD7192_CONF_UNIPOLAR; 438 436 439 - burnout_curr_en = of_property_read_bool(np, 440 - "adi,burnout-currents-enable"); 437 + burnout_curr_en = device_property_read_bool(dev, 438 + "adi,burnout-currents-enable"); 441 439 if (burnout_curr_en && buf_en) { 442 440 st->conf |= AD7192_CONF_BURN; 443 441 } else if (burnout_curr_en) { 444 - dev_warn(&st->sd.spi->dev, 442 + dev_warn(dev, 445 443 "Can't enable burnout currents: see CHOP or buffer\n"); 446 444 } 447 445 ··· 1119 1117 } 1120 1118 st->int_vref_mv = ret / 1000; 1121 1119 1122 - st->chip_info = of_device_get_match_data(&spi->dev); 1123 - if (!st->chip_info) 1124 - st->chip_info = (void *)spi_get_device_id(spi)->driver_data; 1120 + st->chip_info = spi_get_device_match_data(spi); 1125 1121 indio_dev->name = st->chip_info->name; 1126 1122 indio_dev->modes = INDIO_DIRECT_MODE; 1127 1123 indio_dev->channels = st->chip_info->channels; ··· 1140 1140 if (IS_ERR(st->mclk)) 1141 1141 return PTR_ERR(st->mclk); 1142 1142 1143 - st->clock_sel = ad7192_of_clock_select(st); 1143 + st->clock_sel = ad7192_clock_select(st); 1144 1144 1145 1145 if (st->clock_sel == AD7192_CLK_EXT_MCLK1_2 || 1146 1146 st->clock_sel == AD7192_CLK_EXT_MCLK2) { ··· 1152 1152 } 1153 1153 } 1154 1154 1155 - ret = ad7192_setup(indio_dev, spi->dev.of_node); 1155 + ret = ad7192_setup(indio_dev, &spi->dev); 1156 1156 if (ret) 1157 1157 return ret; 1158 1158
+6 -7
drivers/iio/adc/ad7292.c
··· 8 8 #include <linux/bitfield.h> 9 9 #include <linux/device.h> 10 10 #include <linux/module.h> 11 - #include <linux/of.h> 11 + #include <linux/mod_devicetable.h> 12 + #include <linux/property.h> 12 13 #include <linux/regulator/consumer.h> 13 14 #include <linux/spi/spi.h> 14 15 ··· 261 260 { 262 261 struct ad7292_state *st; 263 262 struct iio_dev *indio_dev; 264 - struct device_node *child; 265 263 bool diff_channels = false; 266 264 int ret; 267 265 ··· 305 305 indio_dev->modes = INDIO_DIRECT_MODE; 306 306 indio_dev->info = &ad7292_info; 307 307 308 - for_each_available_child_of_node(spi->dev.of_node, child) { 309 - diff_channels = of_property_read_bool(child, "diff-channels"); 310 - if (diff_channels) { 311 - of_node_put(child); 308 + device_for_each_child_node_scoped(&spi->dev, child) { 309 + diff_channels = fwnode_property_read_bool(child, 310 + "diff-channels"); 311 + if (diff_channels) 312 312 break; 313 - } 314 313 } 315 314 316 315 if (diff_channels) {
+524
drivers/iio/adc/ad7944.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Analog Devices AD7944/85/86 PulSAR ADC family driver. 4 + * 5 + * Copyright 2024 Analog Devices, Inc. 6 + * Copyright 2024 BayLibre, SAS 7 + */ 8 + 9 + #include <linux/bitfield.h> 10 + #include <linux/bitops.h> 11 + #include <linux/delay.h> 12 + #include <linux/device.h> 13 + #include <linux/err.h> 14 + #include <linux/gpio/consumer.h> 15 + #include <linux/module.h> 16 + #include <linux/property.h> 17 + #include <linux/regulator/consumer.h> 18 + #include <linux/spi/spi.h> 19 + #include <linux/string_helpers.h> 20 + 21 + #include <linux/iio/iio.h> 22 + #include <linux/iio/sysfs.h> 23 + #include <linux/iio/trigger_consumer.h> 24 + #include <linux/iio/triggered_buffer.h> 25 + 26 + #define AD7944_INTERNAL_REF_MV 4096 27 + 28 + struct ad7944_timing_spec { 29 + /* Normal mode max conversion time (t_{CONV}). */ 30 + unsigned int conv_ns; 31 + /* TURBO mode max conversion time (t_{CONV}). */ 32 + unsigned int turbo_conv_ns; 33 + }; 34 + 35 + enum ad7944_spi_mode { 36 + /* datasheet calls this "4-wire mode" */ 37 + AD7944_SPI_MODE_DEFAULT, 38 + /* datasheet calls this "3-wire mode" (not related to SPI_3WIRE!) */ 39 + AD7944_SPI_MODE_SINGLE, 40 + /* datasheet calls this "chain mode" */ 41 + AD7944_SPI_MODE_CHAIN, 42 + }; 43 + 44 + /* maps adi,spi-mode property value to enum */ 45 + static const char * const ad7944_spi_modes[] = { 46 + [AD7944_SPI_MODE_DEFAULT] = "", 47 + [AD7944_SPI_MODE_SINGLE] = "single", 48 + [AD7944_SPI_MODE_CHAIN] = "chain", 49 + }; 50 + 51 + struct ad7944_adc { 52 + struct spi_device *spi; 53 + enum ad7944_spi_mode spi_mode; 54 + struct spi_transfer xfers[3]; 55 + struct spi_message msg; 56 + /* Chip-specific timing specifications. */ 57 + const struct ad7944_timing_spec *timing_spec; 58 + /* GPIO connected to CNV pin. */ 59 + struct gpio_desc *cnv; 60 + /* Optional GPIO to enable turbo mode. */ 61 + struct gpio_desc *turbo; 62 + /* Indicates TURBO is hard-wired to be always enabled. */ 63 + bool always_turbo; 64 + /* Reference voltage (millivolts). */ 65 + unsigned int ref_mv; 66 + 67 + /* 68 + * DMA (thus cache coherency maintenance) requires the 69 + * transfer buffers to live in their own cache lines. 70 + */ 71 + struct { 72 + union { 73 + u16 u16; 74 + u32 u32; 75 + } raw; 76 + u64 timestamp __aligned(8); 77 + } sample __aligned(IIO_DMA_MINALIGN); 78 + }; 79 + 80 + /* quite time before CNV rising edge */ 81 + #define T_QUIET_NS 20 82 + 83 + static const struct ad7944_timing_spec ad7944_timing_spec = { 84 + .conv_ns = 420, 85 + .turbo_conv_ns = 320, 86 + }; 87 + 88 + static const struct ad7944_timing_spec ad7986_timing_spec = { 89 + .conv_ns = 500, 90 + .turbo_conv_ns = 400, 91 + }; 92 + 93 + struct ad7944_chip_info { 94 + const char *name; 95 + const struct ad7944_timing_spec *timing_spec; 96 + const struct iio_chan_spec channels[2]; 97 + }; 98 + 99 + /* 100 + * AD7944_DEFINE_CHIP_INFO - Define a chip info structure for a specific chip 101 + * @_name: The name of the chip 102 + * @_ts: The timing specification for the chip 103 + * @_bits: The number of bits in the conversion result 104 + * @_diff: Whether the chip is true differential or not 105 + */ 106 + #define AD7944_DEFINE_CHIP_INFO(_name, _ts, _bits, _diff) \ 107 + static const struct ad7944_chip_info _name##_chip_info = { \ 108 + .name = #_name, \ 109 + .timing_spec = &_ts##_timing_spec, \ 110 + .channels = { \ 111 + { \ 112 + .type = IIO_VOLTAGE, \ 113 + .indexed = 1, \ 114 + .differential = _diff, \ 115 + .channel = 0, \ 116 + .channel2 = _diff ? 1 : 0, \ 117 + .scan_index = 0, \ 118 + .scan_type.sign = _diff ? 's' : 'u', \ 119 + .scan_type.realbits = _bits, \ 120 + .scan_type.storagebits = _bits > 16 ? 32 : 16, \ 121 + .scan_type.endianness = IIO_CPU, \ 122 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \ 123 + | BIT(IIO_CHAN_INFO_SCALE), \ 124 + }, \ 125 + IIO_CHAN_SOFT_TIMESTAMP(1), \ 126 + }, \ 127 + } 128 + 129 + /* pseudo-differential with ground sense */ 130 + AD7944_DEFINE_CHIP_INFO(ad7944, ad7944, 14, 0); 131 + AD7944_DEFINE_CHIP_INFO(ad7985, ad7944, 16, 0); 132 + /* fully differential */ 133 + AD7944_DEFINE_CHIP_INFO(ad7986, ad7986, 18, 1); 134 + 135 + static void ad7944_unoptimize_msg(void *msg) 136 + { 137 + spi_unoptimize_message(msg); 138 + } 139 + 140 + static int ad7944_3wire_cs_mode_init_msg(struct device *dev, struct ad7944_adc *adc, 141 + const struct iio_chan_spec *chan) 142 + { 143 + unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns 144 + : adc->timing_spec->conv_ns; 145 + struct spi_transfer *xfers = adc->xfers; 146 + int ret; 147 + 148 + /* 149 + * NB: can get better performance from some SPI controllers if we use 150 + * the same bits_per_word in every transfer. 151 + */ 152 + xfers[0].bits_per_word = chan->scan_type.realbits; 153 + /* 154 + * CS is tied to CNV and we need a low to high transition to start the 155 + * conversion, so place CNV low for t_QUIET to prepare for this. 156 + */ 157 + xfers[0].delay.value = T_QUIET_NS; 158 + xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS; 159 + 160 + /* 161 + * CS has to be high for full conversion time to avoid triggering the 162 + * busy indication. 163 + */ 164 + xfers[1].cs_off = 1; 165 + xfers[1].delay.value = t_conv_ns; 166 + xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS; 167 + xfers[1].bits_per_word = chan->scan_type.realbits; 168 + 169 + /* Then we can read the data during the acquisition phase */ 170 + xfers[2].rx_buf = &adc->sample.raw; 171 + xfers[2].len = BITS_TO_BYTES(chan->scan_type.storagebits); 172 + xfers[2].bits_per_word = chan->scan_type.realbits; 173 + 174 + spi_message_init_with_transfers(&adc->msg, xfers, 3); 175 + 176 + ret = spi_optimize_message(adc->spi, &adc->msg); 177 + if (ret) 178 + return ret; 179 + 180 + return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg); 181 + } 182 + 183 + static int ad7944_4wire_mode_init_msg(struct device *dev, struct ad7944_adc *adc, 184 + const struct iio_chan_spec *chan) 185 + { 186 + unsigned int t_conv_ns = adc->always_turbo ? adc->timing_spec->turbo_conv_ns 187 + : adc->timing_spec->conv_ns; 188 + struct spi_transfer *xfers = adc->xfers; 189 + int ret; 190 + 191 + /* 192 + * NB: can get better performance from some SPI controllers if we use 193 + * the same bits_per_word in every transfer. 194 + */ 195 + xfers[0].bits_per_word = chan->scan_type.realbits; 196 + /* 197 + * CS has to be high for full conversion time to avoid triggering the 198 + * busy indication. 199 + */ 200 + xfers[0].cs_off = 1; 201 + xfers[0].delay.value = t_conv_ns; 202 + xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS; 203 + 204 + xfers[1].rx_buf = &adc->sample.raw; 205 + xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits); 206 + xfers[1].bits_per_word = chan->scan_type.realbits; 207 + 208 + spi_message_init_with_transfers(&adc->msg, xfers, 2); 209 + 210 + ret = spi_optimize_message(adc->spi, &adc->msg); 211 + if (ret) 212 + return ret; 213 + 214 + return devm_add_action_or_reset(dev, ad7944_unoptimize_msg, &adc->msg); 215 + } 216 + 217 + /** 218 + * ad7944_convert_and_acquire - Perform a single conversion and acquisition 219 + * @adc: The ADC device structure 220 + * @chan: The channel specification 221 + * Return: 0 on success, a negative error code on failure 222 + * 223 + * Perform a conversion and acquisition of a single sample using the 224 + * pre-optimized adc->msg. 225 + * 226 + * Upon successful return adc->sample.raw will contain the conversion result. 227 + */ 228 + static int ad7944_convert_and_acquire(struct ad7944_adc *adc, 229 + const struct iio_chan_spec *chan) 230 + { 231 + int ret; 232 + 233 + /* 234 + * In 4-wire mode, the CNV line is held high for the entire conversion 235 + * and acquisition process. In other modes adc->cnv is NULL and is 236 + * ignored (CS is wired to CNV in those cases). 237 + */ 238 + gpiod_set_value_cansleep(adc->cnv, 1); 239 + ret = spi_sync(adc->spi, &adc->msg); 240 + gpiod_set_value_cansleep(adc->cnv, 0); 241 + 242 + return ret; 243 + } 244 + 245 + static int ad7944_single_conversion(struct ad7944_adc *adc, 246 + const struct iio_chan_spec *chan, 247 + int *val) 248 + { 249 + int ret; 250 + 251 + ret = ad7944_convert_and_acquire(adc, chan); 252 + if (ret) 253 + return ret; 254 + 255 + if (chan->scan_type.storagebits > 16) 256 + *val = adc->sample.raw.u32; 257 + else 258 + *val = adc->sample.raw.u16; 259 + 260 + if (chan->scan_type.sign == 's') 261 + *val = sign_extend32(*val, chan->scan_type.realbits - 1); 262 + 263 + return IIO_VAL_INT; 264 + } 265 + 266 + static int ad7944_read_raw(struct iio_dev *indio_dev, 267 + const struct iio_chan_spec *chan, 268 + int *val, int *val2, long info) 269 + { 270 + struct ad7944_adc *adc = iio_priv(indio_dev); 271 + int ret; 272 + 273 + switch (info) { 274 + case IIO_CHAN_INFO_RAW: 275 + ret = iio_device_claim_direct_mode(indio_dev); 276 + if (ret) 277 + return ret; 278 + 279 + ret = ad7944_single_conversion(adc, chan, val); 280 + iio_device_release_direct_mode(indio_dev); 281 + return ret; 282 + 283 + case IIO_CHAN_INFO_SCALE: 284 + switch (chan->type) { 285 + case IIO_VOLTAGE: 286 + *val = adc->ref_mv; 287 + 288 + if (chan->scan_type.sign == 's') 289 + *val2 = chan->scan_type.realbits - 1; 290 + else 291 + *val2 = chan->scan_type.realbits; 292 + 293 + return IIO_VAL_FRACTIONAL_LOG2; 294 + default: 295 + return -EINVAL; 296 + } 297 + 298 + default: 299 + return -EINVAL; 300 + } 301 + } 302 + 303 + static const struct iio_info ad7944_iio_info = { 304 + .read_raw = &ad7944_read_raw, 305 + }; 306 + 307 + static irqreturn_t ad7944_trigger_handler(int irq, void *p) 308 + { 309 + struct iio_poll_func *pf = p; 310 + struct iio_dev *indio_dev = pf->indio_dev; 311 + struct ad7944_adc *adc = iio_priv(indio_dev); 312 + int ret; 313 + 314 + ret = ad7944_convert_and_acquire(adc, &indio_dev->channels[0]); 315 + if (ret) 316 + goto out; 317 + 318 + iio_push_to_buffers_with_timestamp(indio_dev, &adc->sample.raw, 319 + pf->timestamp); 320 + 321 + out: 322 + iio_trigger_notify_done(indio_dev->trig); 323 + 324 + return IRQ_HANDLED; 325 + } 326 + 327 + static const char * const ad7944_power_supplies[] = { 328 + "avdd", "dvdd", "bvdd", "vio" 329 + }; 330 + 331 + static void ad7944_ref_disable(void *ref) 332 + { 333 + regulator_disable(ref); 334 + } 335 + 336 + static int ad7944_probe(struct spi_device *spi) 337 + { 338 + const struct ad7944_chip_info *chip_info; 339 + struct device *dev = &spi->dev; 340 + struct iio_dev *indio_dev; 341 + struct ad7944_adc *adc; 342 + bool have_refin = false; 343 + struct regulator *ref; 344 + int ret; 345 + 346 + indio_dev = devm_iio_device_alloc(dev, sizeof(*adc)); 347 + if (!indio_dev) 348 + return -ENOMEM; 349 + 350 + adc = iio_priv(indio_dev); 351 + adc->spi = spi; 352 + 353 + chip_info = spi_get_device_match_data(spi); 354 + if (!chip_info) 355 + return dev_err_probe(dev, -EINVAL, "no chip info\n"); 356 + 357 + adc->timing_spec = chip_info->timing_spec; 358 + 359 + ret = device_property_match_property_string(dev, "adi,spi-mode", 360 + ad7944_spi_modes, 361 + ARRAY_SIZE(ad7944_spi_modes)); 362 + /* absence of adi,spi-mode property means default mode */ 363 + if (ret == -EINVAL) 364 + adc->spi_mode = AD7944_SPI_MODE_DEFAULT; 365 + else if (ret < 0) 366 + return dev_err_probe(dev, ret, 367 + "getting adi,spi-mode property failed\n"); 368 + else 369 + adc->spi_mode = ret; 370 + 371 + /* 372 + * Some chips use unusual word sizes, so check now instead of waiting 373 + * for the first xfer. 374 + */ 375 + if (!spi_is_bpw_supported(spi, chip_info->channels[0].scan_type.realbits)) 376 + return dev_err_probe(dev, -EINVAL, 377 + "SPI host does not support %d bits per word\n", 378 + chip_info->channels[0].scan_type.realbits); 379 + 380 + ret = devm_regulator_bulk_get_enable(dev, 381 + ARRAY_SIZE(ad7944_power_supplies), 382 + ad7944_power_supplies); 383 + if (ret) 384 + return dev_err_probe(dev, ret, 385 + "failed to get and enable supplies\n"); 386 + 387 + /* 388 + * Sort out what is being used for the reference voltage. Options are: 389 + * - internal reference: neither REF or REFIN is connected 390 + * - internal reference with external buffer: REF not connected, REFIN 391 + * is connected 392 + * - external reference: REF is connected, REFIN is not connected 393 + */ 394 + 395 + ref = devm_regulator_get_optional(dev, "ref"); 396 + if (IS_ERR(ref)) { 397 + if (PTR_ERR(ref) != -ENODEV) 398 + return dev_err_probe(dev, PTR_ERR(ref), 399 + "failed to get REF supply\n"); 400 + 401 + ref = NULL; 402 + } 403 + 404 + ret = devm_regulator_get_enable_optional(dev, "refin"); 405 + if (ret == 0) 406 + have_refin = true; 407 + else if (ret != -ENODEV) 408 + return dev_err_probe(dev, ret, 409 + "failed to get and enable REFIN supply\n"); 410 + 411 + if (have_refin && ref) 412 + return dev_err_probe(dev, -EINVAL, 413 + "cannot have both refin and ref supplies\n"); 414 + 415 + if (ref) { 416 + ret = regulator_enable(ref); 417 + if (ret) 418 + return dev_err_probe(dev, ret, 419 + "failed to enable REF supply\n"); 420 + 421 + ret = devm_add_action_or_reset(dev, ad7944_ref_disable, ref); 422 + if (ret) 423 + return ret; 424 + 425 + ret = regulator_get_voltage(ref); 426 + if (ret < 0) 427 + return dev_err_probe(dev, ret, 428 + "failed to get REF voltage\n"); 429 + 430 + /* external reference */ 431 + adc->ref_mv = ret / 1000; 432 + } else { 433 + /* internal reference */ 434 + adc->ref_mv = AD7944_INTERNAL_REF_MV; 435 + } 436 + 437 + adc->cnv = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW); 438 + if (IS_ERR(adc->cnv)) 439 + return dev_err_probe(dev, PTR_ERR(adc->cnv), 440 + "failed to get CNV GPIO\n"); 441 + 442 + if (!adc->cnv && adc->spi_mode == AD7944_SPI_MODE_DEFAULT) 443 + return dev_err_probe(&spi->dev, -EINVAL, "CNV GPIO is required\n"); 444 + if (adc->cnv && adc->spi_mode != AD7944_SPI_MODE_DEFAULT) 445 + return dev_err_probe(&spi->dev, -EINVAL, 446 + "CNV GPIO in single and chain mode is not currently supported\n"); 447 + 448 + adc->turbo = devm_gpiod_get_optional(dev, "turbo", GPIOD_OUT_LOW); 449 + if (IS_ERR(adc->turbo)) 450 + return dev_err_probe(dev, PTR_ERR(adc->turbo), 451 + "failed to get TURBO GPIO\n"); 452 + 453 + adc->always_turbo = device_property_present(dev, "adi,always-turbo"); 454 + 455 + if (adc->turbo && adc->always_turbo) 456 + return dev_err_probe(dev, -EINVAL, 457 + "cannot have both turbo-gpios and adi,always-turbo\n"); 458 + 459 + if (adc->spi_mode == AD7944_SPI_MODE_CHAIN && adc->always_turbo) 460 + return dev_err_probe(dev, -EINVAL, 461 + "cannot have both chain mode and always turbo\n"); 462 + 463 + switch (adc->spi_mode) { 464 + case AD7944_SPI_MODE_DEFAULT: 465 + ret = ad7944_4wire_mode_init_msg(dev, adc, &chip_info->channels[0]); 466 + if (ret) 467 + return ret; 468 + 469 + break; 470 + case AD7944_SPI_MODE_SINGLE: 471 + ret = ad7944_3wire_cs_mode_init_msg(dev, adc, &chip_info->channels[0]); 472 + if (ret) 473 + return ret; 474 + 475 + break; 476 + case AD7944_SPI_MODE_CHAIN: 477 + return dev_err_probe(dev, -EINVAL, "chain mode is not implemented\n"); 478 + } 479 + 480 + indio_dev->name = chip_info->name; 481 + indio_dev->modes = INDIO_DIRECT_MODE; 482 + indio_dev->info = &ad7944_iio_info; 483 + indio_dev->channels = chip_info->channels; 484 + indio_dev->num_channels = ARRAY_SIZE(chip_info->channels); 485 + 486 + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 487 + iio_pollfunc_store_time, 488 + ad7944_trigger_handler, NULL); 489 + if (ret) 490 + return ret; 491 + 492 + return devm_iio_device_register(dev, indio_dev); 493 + } 494 + 495 + static const struct of_device_id ad7944_of_match[] = { 496 + { .compatible = "adi,ad7944", .data = &ad7944_chip_info }, 497 + { .compatible = "adi,ad7985", .data = &ad7985_chip_info }, 498 + { .compatible = "adi,ad7986", .data = &ad7986_chip_info }, 499 + { } 500 + }; 501 + MODULE_DEVICE_TABLE(of, ad7944_of_match); 502 + 503 + static const struct spi_device_id ad7944_spi_id[] = { 504 + { "ad7944", (kernel_ulong_t)&ad7944_chip_info }, 505 + { "ad7985", (kernel_ulong_t)&ad7985_chip_info }, 506 + { "ad7986", (kernel_ulong_t)&ad7986_chip_info }, 507 + { } 508 + 509 + }; 510 + MODULE_DEVICE_TABLE(spi, ad7944_spi_id); 511 + 512 + static struct spi_driver ad7944_driver = { 513 + .driver = { 514 + .name = "ad7944", 515 + .of_match_table = ad7944_of_match, 516 + }, 517 + .probe = ad7944_probe, 518 + .id_table = ad7944_spi_id, 519 + }; 520 + module_spi_driver(ad7944_driver); 521 + 522 + MODULE_AUTHOR("David Lechner <dlechner@baylibre.com>"); 523 + MODULE_DESCRIPTION("Analog Devices AD7944 PulSAR ADC family driver"); 524 + MODULE_LICENSE("GPL");
+14 -9
drivers/iio/adc/ad_sigma_delta.c
··· 222 222 goto out; 223 223 224 224 sigma_delta->irq_dis = false; 225 - enable_irq(sigma_delta->spi->irq); 225 + enable_irq(sigma_delta->irq_line); 226 226 timeout = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ); 227 227 if (timeout == 0) { 228 228 sigma_delta->irq_dis = true; 229 - disable_irq_nosync(sigma_delta->spi->irq); 229 + disable_irq_nosync(sigma_delta->irq_line); 230 230 ret = -EIO; 231 231 } else { 232 232 ret = 0; ··· 295 295 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE); 296 296 297 297 sigma_delta->irq_dis = false; 298 - enable_irq(sigma_delta->spi->irq); 298 + enable_irq(sigma_delta->irq_line); 299 299 ret = wait_for_completion_interruptible_timeout( 300 300 &sigma_delta->completion, HZ); 301 301 ··· 315 315 316 316 out: 317 317 if (!sigma_delta->irq_dis) { 318 - disable_irq_nosync(sigma_delta->spi->irq); 318 + disable_irq_nosync(sigma_delta->irq_line); 319 319 sigma_delta->irq_dis = true; 320 320 } 321 321 ··· 396 396 goto err_unlock; 397 397 398 398 sigma_delta->irq_dis = false; 399 - enable_irq(sigma_delta->spi->irq); 399 + enable_irq(sigma_delta->irq_line); 400 400 401 401 return 0; 402 402 ··· 414 414 wait_for_completion_timeout(&sigma_delta->completion, HZ); 415 415 416 416 if (!sigma_delta->irq_dis) { 417 - disable_irq_nosync(sigma_delta->spi->irq); 417 + disable_irq_nosync(sigma_delta->irq_line); 418 418 sigma_delta->irq_dis = true; 419 419 } 420 420 ··· 516 516 irq_handled: 517 517 iio_trigger_notify_done(indio_dev->trig); 518 518 sigma_delta->irq_dis = false; 519 - enable_irq(sigma_delta->spi->irq); 519 + enable_irq(sigma_delta->irq_line); 520 520 521 521 return IRQ_HANDLED; 522 522 } ··· 587 587 sigma_delta->irq_dis = true; 588 588 589 589 /* the IRQ core clears IRQ_DISABLE_UNLAZY flag when freeing an IRQ */ 590 - irq_set_status_flags(sigma_delta->spi->irq, IRQ_DISABLE_UNLAZY); 590 + irq_set_status_flags(sigma_delta->irq_line, IRQ_DISABLE_UNLAZY); 591 591 592 592 /* Allow overwriting the flags from firmware */ 593 593 if (!irq_flags) 594 594 irq_flags = sigma_delta->info->irq_flags; 595 595 596 - ret = devm_request_irq(dev, sigma_delta->spi->irq, 596 + ret = devm_request_irq(dev, sigma_delta->irq_line, 597 597 ad_sd_data_rdy_trig_poll, 598 598 irq_flags | IRQF_NO_AUTOEN, 599 599 indio_dev->name, ··· 672 672 return -EINVAL; 673 673 } 674 674 } 675 + 676 + if (info->irq_line) 677 + sigma_delta->irq_line = info->irq_line; 678 + else 679 + sigma_delta->irq_line = spi->irq; 675 680 676 681 iio_device_set_drvdata(indio_dev, sigma_delta); 677 682
+3 -17
drivers/iio/adc/adi-axi-adc.c
··· 124 124 struct iio_dev *indio_dev) 125 125 { 126 126 struct adi_axi_adc_state *st = iio_backend_get_priv(back); 127 - struct iio_buffer *buffer; 128 127 const char *dma_name; 129 - int ret; 130 128 131 129 if (device_property_read_string(st->dev, "dma-names", &dma_name)) 132 130 dma_name = "rx"; 133 131 134 - buffer = iio_dmaengine_buffer_alloc(st->dev, dma_name); 135 - if (IS_ERR(buffer)) { 136 - dev_err(st->dev, "Could not get DMA buffer, %ld\n", 137 - PTR_ERR(buffer)); 138 - return ERR_CAST(buffer); 139 - } 140 - 141 - indio_dev->modes |= INDIO_BUFFER_HARDWARE; 142 - ret = iio_device_attach_buffer(indio_dev, buffer); 143 - if (ret) 144 - return ERR_PTR(ret); 145 - 146 - return buffer; 132 + return iio_dmaengine_buffer_setup(st->dev, indio_dev, dma_name); 147 133 } 148 134 149 135 static void axi_adc_free_buffer(struct iio_backend *back, ··· 193 207 if (ret) 194 208 return ret; 195 209 196 - if (*expected_ver > ver) { 210 + if (ADI_AXI_PCORE_VER_MAJOR(ver) != ADI_AXI_PCORE_VER_MAJOR(*expected_ver)) { 197 211 dev_err(&pdev->dev, 198 - "IP core version is too old. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n", 212 + "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n", 199 213 ADI_AXI_PCORE_VER_MAJOR(*expected_ver), 200 214 ADI_AXI_PCORE_VER_MINOR(*expected_ver), 201 215 ADI_AXI_PCORE_VER_PATCH(*expected_ver),
+56 -84
drivers/iio/adc/fsl-imx25-gcq.c
··· 12 12 #include <linux/interrupt.h> 13 13 #include <linux/mfd/imx25-tsadc.h> 14 14 #include <linux/module.h> 15 - #include <linux/of.h> 15 + #include <linux/mod_devicetable.h> 16 16 #include <linux/platform_device.h> 17 + #include <linux/property.h> 17 18 #include <linux/regmap.h> 18 19 #include <linux/regulator/consumer.h> 19 20 ··· 199 198 static int mx25_gcq_setup_cfgs(struct platform_device *pdev, 200 199 struct mx25_gcq_priv *priv) 201 200 { 202 - struct device_node *np = pdev->dev.of_node; 203 - struct device_node *child; 204 201 struct device *dev = &pdev->dev; 205 202 int ret, i; 206 203 ··· 215 216 MX25_ADCQ_CFG_IN(i) | 216 217 MX25_ADCQ_CFG_REFN_NGND2); 217 218 218 - for_each_child_of_node(np, child) { 219 + device_for_each_child_node_scoped(dev, child) { 219 220 u32 reg; 220 221 u32 refp = MX25_ADCQ_CFG_REFP_INT; 221 222 u32 refn = MX25_ADCQ_CFG_REFN_NGND2; 222 223 223 - ret = of_property_read_u32(child, "reg", &reg); 224 - if (ret) { 225 - dev_err(dev, "Failed to get reg property\n"); 226 - of_node_put(child); 227 - return ret; 228 - } 224 + ret = fwnode_property_read_u32(child, "reg", &reg); 225 + if (ret) 226 + return dev_err_probe(dev, ret, 227 + "Failed to get reg property\n"); 229 228 230 - if (reg >= MX25_NUM_CFGS) { 231 - dev_err(dev, 229 + if (reg >= MX25_NUM_CFGS) 230 + return dev_err_probe(dev, -EINVAL, 232 231 "reg value is greater than the number of available configuration registers\n"); 233 - of_node_put(child); 234 - return -EINVAL; 235 - } 236 232 237 - of_property_read_u32(child, "fsl,adc-refp", &refp); 238 - of_property_read_u32(child, "fsl,adc-refn", &refn); 233 + fwnode_property_read_u32(child, "fsl,adc-refp", &refp); 234 + fwnode_property_read_u32(child, "fsl,adc-refn", &refn); 239 235 240 236 switch (refp) { 241 237 case MX25_ADC_REFP_EXT: 242 238 case MX25_ADC_REFP_XP: 243 239 case MX25_ADC_REFP_YP: 244 240 ret = mx25_gcq_ext_regulator_setup(&pdev->dev, priv, refp); 245 - if (ret) { 246 - of_node_put(child); 241 + if (ret) 247 242 return ret; 248 - } 249 243 priv->channel_vref_mv[reg] = 250 244 regulator_get_voltage(priv->vref[refp]); 251 245 /* Conversion from uV to mV */ ··· 248 256 priv->channel_vref_mv[reg] = 2500; 249 257 break; 250 258 default: 251 - dev_err(dev, "Invalid positive reference %d\n", refp); 252 - of_node_put(child); 253 - return -EINVAL; 259 + return dev_err_probe(dev, -EINVAL, 260 + "Invalid positive reference %d\n", refp); 254 261 } 255 262 256 263 /* ··· 259 268 refp = MX25_ADCQ_CFG_REFP(refp); 260 269 refn = MX25_ADCQ_CFG_REFN(refn); 261 270 262 - if ((refp & MX25_ADCQ_CFG_REFP_MASK) != refp) { 263 - dev_err(dev, "Invalid fsl,adc-refp property value\n"); 264 - of_node_put(child); 265 - return -EINVAL; 266 - } 267 - if ((refn & MX25_ADCQ_CFG_REFN_MASK) != refn) { 268 - dev_err(dev, "Invalid fsl,adc-refn property value\n"); 269 - of_node_put(child); 270 - return -EINVAL; 271 - } 271 + if ((refp & MX25_ADCQ_CFG_REFP_MASK) != refp) 272 + return dev_err_probe(dev, -EINVAL, 273 + "Invalid fsl,adc-refp property value\n"); 274 + 275 + if ((refn & MX25_ADCQ_CFG_REFN_MASK) != refn) 276 + return dev_err_probe(dev, -EINVAL, 277 + "Invalid fsl,adc-refn property value\n"); 272 278 273 279 regmap_update_bits(priv->regs, MX25_ADCQ_CFG(reg), 274 280 MX25_ADCQ_CFG_REFP_MASK | ··· 280 292 MX25_ADCQ_CR_PDMSK | MX25_ADCQ_CR_QSM_FQS); 281 293 282 294 return 0; 295 + } 296 + 297 + static void mx25_gcq_reg_disable(void *reg) 298 + { 299 + regulator_disable(reg); 300 + } 301 + 302 + /* Custom handling needed as this driver doesn't own the clock */ 303 + static void mx25_gcq_clk_disable(void *clk) 304 + { 305 + clk_disable_unprepare(clk); 283 306 } 284 307 285 308 static int mx25_gcq_probe(struct platform_device *pdev) ··· 314 315 return PTR_ERR(mem); 315 316 316 317 priv->regs = devm_regmap_init_mmio(dev, mem, &mx25_gcq_regconfig); 317 - if (IS_ERR(priv->regs)) { 318 - dev_err(dev, "Failed to initialize regmap\n"); 319 - return PTR_ERR(priv->regs); 320 - } 318 + if (IS_ERR(priv->regs)) 319 + return dev_err_probe(dev, PTR_ERR(priv->regs), 320 + "Failed to initialize regmap\n"); 321 321 322 322 mutex_init(&priv->lock); 323 323 ··· 332 334 333 335 ret = regulator_enable(priv->vref[i]); 334 336 if (ret) 335 - goto err_regulator_disable; 337 + return ret; 338 + 339 + ret = devm_add_action_or_reset(dev, mx25_gcq_reg_disable, 340 + priv->vref[i]); 341 + if (ret) 342 + return ret; 336 343 } 337 344 338 345 priv->clk = tsadc->clk; 339 346 ret = clk_prepare_enable(priv->clk); 340 - if (ret) { 341 - dev_err(dev, "Failed to enable clock\n"); 342 - goto err_vref_disable; 343 - } 347 + if (ret) 348 + return dev_err_probe(dev, ret, "Failed to enable clock\n"); 349 + 350 + ret = devm_add_action_or_reset(dev, mx25_gcq_clk_disable, 351 + priv->clk); 352 + if (ret) 353 + return ret; 344 354 345 355 ret = platform_get_irq(pdev, 0); 346 356 if (ret < 0) 347 - goto err_clk_unprepare; 357 + return ret; 348 358 349 359 priv->irq = ret; 350 - ret = request_irq(priv->irq, mx25_gcq_irq, 0, pdev->name, priv); 351 - if (ret) { 352 - dev_err(dev, "Failed requesting IRQ\n"); 353 - goto err_clk_unprepare; 354 - } 360 + ret = devm_request_irq(dev, priv->irq, mx25_gcq_irq, 0, pdev->name, 361 + priv); 362 + if (ret) 363 + return dev_err_probe(dev, ret, "Failed requesting IRQ\n"); 355 364 356 365 indio_dev->channels = mx25_gcq_channels; 357 366 indio_dev->num_channels = ARRAY_SIZE(mx25_gcq_channels); 358 367 indio_dev->info = &mx25_gcq_iio_info; 359 368 indio_dev->name = driver_name; 360 369 361 - ret = iio_device_register(indio_dev); 362 - if (ret) { 363 - dev_err(dev, "Failed to register iio device\n"); 364 - goto err_irq_free; 365 - } 366 - 367 - platform_set_drvdata(pdev, indio_dev); 370 + ret = devm_iio_device_register(dev, indio_dev); 371 + if (ret) 372 + return dev_err_probe(dev, ret, "Failed to register iio device\n"); 368 373 369 374 return 0; 370 - 371 - err_irq_free: 372 - free_irq(priv->irq, priv); 373 - err_clk_unprepare: 374 - clk_disable_unprepare(priv->clk); 375 - err_vref_disable: 376 - i = 4; 377 - err_regulator_disable: 378 - for (; i-- > 0;) { 379 - if (priv->vref[i]) 380 - regulator_disable(priv->vref[i]); 381 - } 382 - return ret; 383 - } 384 - 385 - static void mx25_gcq_remove(struct platform_device *pdev) 386 - { 387 - struct iio_dev *indio_dev = platform_get_drvdata(pdev); 388 - struct mx25_gcq_priv *priv = iio_priv(indio_dev); 389 - int i; 390 - 391 - iio_device_unregister(indio_dev); 392 - free_irq(priv->irq, priv); 393 - clk_disable_unprepare(priv->clk); 394 - for (i = 4; i-- > 0;) { 395 - if (priv->vref[i]) 396 - regulator_disable(priv->vref[i]); 397 - } 398 375 } 399 376 400 377 static const struct of_device_id mx25_gcq_ids[] = { ··· 384 411 .of_match_table = mx25_gcq_ids, 385 412 }, 386 413 .probe = mx25_gcq_probe, 387 - .remove_new = mx25_gcq_remove, 388 414 }; 389 415 module_platform_driver(mx25_gcq_driver); 390 416
+2 -3
drivers/iio/adc/hx711.c
··· 7 7 #include <linux/err.h> 8 8 #include <linux/kernel.h> 9 9 #include <linux/module.h> 10 - #include <linux/of.h> 10 + #include <linux/mod_devicetable.h> 11 11 #include <linux/platform_device.h> 12 12 #include <linux/property.h> 13 13 #include <linux/slab.h> ··· 459 459 static int hx711_probe(struct platform_device *pdev) 460 460 { 461 461 struct device *dev = &pdev->dev; 462 - struct device_node *np = dev->of_node; 463 462 struct hx711_data *hx711_data; 464 463 struct iio_dev *indio_dev; 465 464 int ret; ··· 532 533 hx711_data->gain_chan_a = 128; 533 534 534 535 hx711_data->clock_frequency = 400000; 535 - ret = of_property_read_u32(np, "clock-frequency", 536 + ret = device_property_read_u32(&pdev->dev, "clock-frequency", 536 537 &hx711_data->clock_frequency); 537 538 538 539 /*
+7 -20
drivers/iio/adc/max11410.c
··· 696 696 struct device *dev = &st->spi_dev->dev; 697 697 struct max11410_channel_config *cfg; 698 698 struct iio_chan_spec *channels; 699 - struct fwnode_handle *child; 700 699 u32 reference, sig_path; 701 700 const char *node_name; 702 701 u32 inputs[2], scale; ··· 719 720 if (!st->channels) 720 721 return -ENOMEM; 721 722 722 - device_for_each_child_node(dev, child) { 723 + device_for_each_child_node_scoped(dev, child) { 723 724 node_name = fwnode_get_name(child); 724 725 if (fwnode_property_present(child, "diff-channels")) { 725 726 ret = fwnode_property_read_u32_array(child, ··· 734 735 inputs[1] = 0; 735 736 chanspec.differential = 0; 736 737 } 737 - if (ret) { 738 - fwnode_handle_put(child); 738 + if (ret) 739 739 return ret; 740 - } 741 740 742 741 if (inputs[0] > MAX11410_CHANNEL_INDEX_MAX || 743 - inputs[1] > MAX11410_CHANNEL_INDEX_MAX) { 744 - fwnode_handle_put(child); 742 + inputs[1] > MAX11410_CHANNEL_INDEX_MAX) 745 743 return dev_err_probe(&indio_dev->dev, -EINVAL, 746 744 "Invalid channel index for %s, should be less than %d\n", 747 745 node_name, 748 746 MAX11410_CHANNEL_INDEX_MAX + 1); 749 - } 750 747 751 748 cfg = &st->channels[chan_idx]; 752 749 753 750 reference = MAX11410_REFSEL_AVDD_AGND; 754 751 fwnode_property_read_u32(child, "adi,reference", &reference); 755 - if (reference > MAX11410_REFSEL_MAX) { 756 - fwnode_handle_put(child); 752 + if (reference > MAX11410_REFSEL_MAX) 757 753 return dev_err_probe(&indio_dev->dev, -EINVAL, 758 754 "Invalid adi,reference value for %s, should be less than %d.\n", 759 755 node_name, MAX11410_REFSEL_MAX + 1); 760 - } 761 756 762 757 if (!max11410_get_vrefp(st, reference) || 763 - (!max11410_get_vrefn(st, reference) && reference <= 2)) { 764 - fwnode_handle_put(child); 758 + (!max11410_get_vrefn(st, reference) && reference <= 2)) 765 759 return dev_err_probe(&indio_dev->dev, -EINVAL, 766 760 "Invalid VREF configuration for %s, either specify corresponding VREF regulators or change adi,reference property.\n", 767 761 node_name); 768 - } 769 762 770 763 sig_path = MAX11410_PGA_SIG_PATH_BUFFERED; 771 764 fwnode_property_read_u32(child, "adi,input-mode", &sig_path); 772 - if (sig_path > MAX11410_SIG_PATH_MAX) { 773 - fwnode_handle_put(child); 765 + if (sig_path > MAX11410_SIG_PATH_MAX) 774 766 return dev_err_probe(&indio_dev->dev, -EINVAL, 775 767 "Invalid adi,input-mode value for %s, should be less than %d.\n", 776 768 node_name, MAX11410_SIG_PATH_MAX + 1); 777 - } 778 769 779 770 fwnode_property_read_u32(child, "settling-time-us", 780 771 &cfg->settling_time_us); ··· 782 793 cfg->scale_avail = devm_kcalloc(dev, MAX11410_SCALE_AVAIL_SIZE * 2, 783 794 sizeof(*cfg->scale_avail), 784 795 GFP_KERNEL); 785 - if (!cfg->scale_avail) { 786 - fwnode_handle_put(child); 796 + if (!cfg->scale_avail) 787 797 return -ENOMEM; 788 - } 789 798 790 799 scale = max11410_get_scale(st, *cfg); 791 800 for (i = 0; i < MAX11410_SCALE_AVAIL_SIZE; i++) {
+31 -46
drivers/iio/adc/pac1934.c
··· 1079 1079 * documentation related to the ACPI device definition 1080 1080 * https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ApplicationNotes/ApplicationNotes/PAC1934-Integration-Notes-for-Microsoft-Windows-10-and-Windows-11-Driver-Support-DS00002534.pdf 1081 1081 */ 1082 - static bool pac1934_acpi_parse_channel_config(struct i2c_client *client, 1083 - struct pac1934_chip_info *info) 1082 + static int pac1934_acpi_parse_channel_config(struct i2c_client *client, 1083 + struct pac1934_chip_info *info) 1084 1084 { 1085 1085 acpi_handle handle; 1086 1086 union acpi_object *rez; ··· 1095 1095 1096 1096 rez = acpi_evaluate_dsm(handle, &guid, 0, PAC1934_ACPI_GET_NAMES_AND_MOHMS_VALS, NULL); 1097 1097 if (!rez) 1098 - return false; 1098 + return -EINVAL; 1099 1099 1100 1100 for (i = 0; i < rez->package.count; i += 2) { 1101 1101 idx = i / 2; ··· 1118 1118 * and assign the default sampling rate 1119 1119 */ 1120 1120 info->sample_rate_value = PAC1934_DEFAULT_CHIP_SAMP_SPEED_HZ; 1121 - return true; 1121 + return 0; 1122 1122 } 1123 1123 1124 1124 for (i = 0; i < rez->package.count; i++) { ··· 1131 1131 1132 1132 rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_BIPOLAR_SETTINGS, NULL); 1133 1133 if (!rez) 1134 - return false; 1134 + return -EINVAL; 1135 1135 1136 1136 bi_dir_mask = rez->package.elements[0].integer.value; 1137 1137 info->bi_dir[0] = ((bi_dir_mask & (1 << 3)) | (bi_dir_mask & (1 << 7))) != 0; ··· 1143 1143 1144 1144 rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_SAMP, NULL); 1145 1145 if (!rez) 1146 - return false; 1146 + return -EINVAL; 1147 1147 1148 1148 info->sample_rate_value = rez->package.elements[0].integer.value; 1149 1149 1150 1150 ACPI_FREE(rez); 1151 1151 1152 - return true; 1152 + return 0; 1153 1153 } 1154 1154 1155 - static bool pac1934_of_parse_channel_config(struct i2c_client *client, 1156 - struct pac1934_chip_info *info) 1155 + static int pac1934_fw_parse_channel_config(struct i2c_client *client, 1156 + struct pac1934_chip_info *info) 1157 1157 { 1158 - struct fwnode_handle *node, *fwnode; 1159 1158 struct device *dev = &client->dev; 1160 1159 unsigned int current_channel; 1161 1160 int idx, ret; ··· 1162 1163 info->sample_rate_value = 1024; 1163 1164 current_channel = 1; 1164 1165 1165 - fwnode = dev_fwnode(dev); 1166 - fwnode_for_each_available_child_node(fwnode, node) { 1166 + device_for_each_child_node_scoped(dev, node) { 1167 1167 ret = fwnode_property_read_u32(node, "reg", &idx); 1168 - if (ret) { 1169 - dev_err_probe(dev, ret, 1170 - "reading invalid channel index\n"); 1171 - goto err_fwnode; 1172 - } 1168 + if (ret) 1169 + return dev_err_probe(dev, ret, 1170 + "reading invalid channel index\n"); 1171 + 1173 1172 /* adjust idx to match channel index (1 to 4) from the datasheet */ 1174 1173 idx--; 1175 1174 1176 1175 if (current_channel >= (info->phys_channels + 1) || 1177 - idx >= info->phys_channels || idx < 0) { 1178 - dev_err_probe(dev, -EINVAL, 1179 - "%s: invalid channel_index %d value\n", 1180 - fwnode_get_name(node), idx); 1181 - goto err_fwnode; 1182 - } 1176 + idx >= info->phys_channels || idx < 0) 1177 + return dev_err_probe(dev, -EINVAL, 1178 + "%s: invalid channel_index %d value\n", 1179 + fwnode_get_name(node), idx); 1183 1180 1184 1181 /* enable channel */ 1185 1182 info->active_channels[idx] = true; 1186 1183 1187 1184 ret = fwnode_property_read_u32(node, "shunt-resistor-micro-ohms", 1188 1185 &info->shunts[idx]); 1189 - if (ret) { 1190 - dev_err_probe(dev, ret, 1191 - "%s: invalid shunt-resistor value: %d\n", 1192 - fwnode_get_name(node), info->shunts[idx]); 1193 - goto err_fwnode; 1194 - } 1186 + if (ret) 1187 + return dev_err_probe(dev, ret, 1188 + "%s: invalid shunt-resistor value: %d\n", 1189 + fwnode_get_name(node), info->shunts[idx]); 1195 1190 1196 1191 if (fwnode_property_present(node, "label")) { 1197 1192 ret = fwnode_property_read_string(node, "label", 1198 1193 (const char **)&info->labels[idx]); 1199 - if (ret) { 1200 - dev_err_probe(dev, ret, 1201 - "%s: invalid rail-name value\n", 1202 - fwnode_get_name(node)); 1203 - goto err_fwnode; 1204 - } 1194 + if (ret) 1195 + return dev_err_probe(dev, ret, 1196 + "%s: invalid rail-name value\n", 1197 + fwnode_get_name(node)); 1205 1198 } 1206 1199 1207 1200 info->bi_dir[idx] = fwnode_property_read_bool(node, "bipolar"); ··· 1201 1210 current_channel++; 1202 1211 } 1203 1212 1204 - return true; 1205 - 1206 - err_fwnode: 1207 - fwnode_handle_put(node); 1208 - 1209 - return false; 1213 + return 0; 1210 1214 } 1211 1215 1212 1216 static void pac1934_cancel_delayed_work(void *dwork) ··· 1471 1485 const struct pac1934_features *chip; 1472 1486 struct iio_dev *indio_dev; 1473 1487 int cnt, ret; 1474 - bool match = false; 1475 1488 struct device *dev = &client->dev; 1476 1489 1477 1490 indio_dev = devm_iio_device_alloc(dev, sizeof(*info)); ··· 1504 1519 } 1505 1520 1506 1521 if (acpi_match_device(dev->driver->acpi_match_table, dev)) 1507 - match = pac1934_acpi_parse_channel_config(client, info); 1522 + ret = pac1934_acpi_parse_channel_config(client, info); 1508 1523 else 1509 1524 /* 1510 1525 * This makes it possible to use also ACPI PRP0001 for 1511 1526 * registering the device using device tree properties. 1512 1527 */ 1513 - match = pac1934_of_parse_channel_config(client, info); 1528 + ret = pac1934_fw_parse_channel_config(client, info); 1514 1529 1515 - if (!match) 1516 - return dev_err_probe(dev, -EINVAL, 1530 + if (ret) 1531 + return dev_err_probe(dev, ret, 1517 1532 "parameter parsing returned an error\n"); 1518 1533 1519 1534 mutex_init(&info->lock);
+2 -5
drivers/iio/adc/qcom-spmi-adc5.c
··· 825 825 const struct adc5_channels *adc_chan; 826 826 struct iio_chan_spec *iio_chan; 827 827 struct adc5_channel_prop prop, *chan_props; 828 - struct fwnode_handle *child; 829 828 unsigned int index = 0; 830 829 int ret; 831 830 ··· 848 849 if (!adc->data) 849 850 adc->data = &adc5_data_pmic; 850 851 851 - device_for_each_child_node(adc->dev, child) { 852 + device_for_each_child_node_scoped(adc->dev, child) { 852 853 ret = adc5_get_fw_channel_data(adc, &prop, child, adc->data); 853 - if (ret) { 854 - fwnode_handle_put(child); 854 + if (ret) 855 855 return ret; 856 - } 857 856 858 857 prop.scale_fn_type = 859 858 adc->data->adc_chans[prop.channel].scale_fn_type;
+6 -15
drivers/iio/adc/rcar-gyroadc.c
··· 318 318 struct rcar_gyroadc *priv = iio_priv(indio_dev); 319 319 struct device *dev = priv->dev; 320 320 struct device_node *np = dev->of_node; 321 - struct device_node *child; 322 321 struct regulator *vref; 323 322 unsigned int reg; 324 323 unsigned int adcmode = -1, childmode; ··· 325 326 unsigned int num_channels; 326 327 int ret, first = 1; 327 328 328 - for_each_child_of_node(np, child) { 329 + for_each_available_child_of_node_scoped(np, child) { 329 330 of_id = of_match_node(rcar_gyroadc_child_match, child); 330 331 if (!of_id) { 331 332 dev_err(dev, "Ignoring unsupported ADC \"%pOFn\".", ··· 351 352 num_channels = ARRAY_SIZE(rcar_gyroadc_iio_channels_3); 352 353 break; 353 354 default: 354 - goto err_e_inval; 355 + return -EINVAL; 355 356 } 356 357 357 358 /* ··· 368 369 dev_err(dev, 369 370 "Failed to get child reg property of ADC \"%pOFn\".\n", 370 371 child); 371 - goto err_of_node_put; 372 + return ret; 372 373 } 373 374 374 375 /* Channel number is too high. */ ··· 376 377 dev_err(dev, 377 378 "Only %i channels supported with %pOFn, but reg = <%i>.\n", 378 379 num_channels, child, reg); 379 - goto err_e_inval; 380 + return -EINVAL; 380 381 } 381 382 } 382 383 ··· 385 386 dev_err(dev, 386 387 "Channel %i uses different ADC mode than the rest.\n", 387 388 reg); 388 - goto err_e_inval; 389 + return -EINVAL; 389 390 } 390 391 391 392 /* Channel is valid, grab the regulator. */ ··· 395 396 if (IS_ERR(vref)) { 396 397 dev_dbg(dev, "Channel %i 'vref' supply not connected.\n", 397 398 reg); 398 - ret = PTR_ERR(vref); 399 - goto err_of_node_put; 399 + return PTR_ERR(vref); 400 400 } 401 401 402 402 priv->vref[reg] = vref; ··· 420 422 * we can stop parsing here. 421 423 */ 422 424 if (childmode == RCAR_GYROADC_MODE_SELECT_1_MB88101A) { 423 - of_node_put(child); 424 425 break; 425 426 } 426 427 } ··· 430 433 } 431 434 432 435 return 0; 433 - 434 - err_e_inval: 435 - ret = -EINVAL; 436 - err_of_node_put: 437 - of_node_put(child); 438 - return ret; 439 436 } 440 437 441 438 static void rcar_gyroadc_deinit_supplies(struct iio_dev *indio_dev)
+3 -8
drivers/iio/adc/rzg2l_adc.c
··· 302 302 static int rzg2l_adc_parse_properties(struct platform_device *pdev, struct rzg2l_adc *adc) 303 303 { 304 304 struct iio_chan_spec *chan_array; 305 - struct fwnode_handle *fwnode; 306 305 struct rzg2l_adc_data *data; 307 306 unsigned int channel; 308 307 int num_channels; ··· 329 330 return -ENOMEM; 330 331 331 332 i = 0; 332 - device_for_each_child_node(&pdev->dev, fwnode) { 333 + device_for_each_child_node_scoped(&pdev->dev, fwnode) { 333 334 ret = fwnode_property_read_u32(fwnode, "reg", &channel); 334 - if (ret) { 335 - fwnode_handle_put(fwnode); 335 + if (ret) 336 336 return ret; 337 - } 338 337 339 - if (channel >= RZG2L_ADC_MAX_CHANNELS) { 340 - fwnode_handle_put(fwnode); 338 + if (channel >= RZG2L_ADC_MAX_CHANNELS) 341 339 return -EINVAL; 342 - } 343 340 344 341 chan_array[i].type = IIO_VOLTAGE; 345 342 chan_array[i].indexed = 1;
+10 -15
drivers/iio/adc/spear_adc.c
··· 5 5 * Copyright 2012 Stefan Roese <sr@denx.de> 6 6 */ 7 7 8 + #include <linux/mod_devicetable.h> 8 9 #include <linux/module.h> 9 10 #include <linux/platform_device.h> 11 + #include <linux/property.h> 10 12 #include <linux/interrupt.h> 11 13 #include <linux/device.h> 12 14 #include <linux/kernel.h> ··· 17 15 #include <linux/clk.h> 18 16 #include <linux/err.h> 19 17 #include <linux/completion.h> 20 - #include <linux/of.h> 21 - #include <linux/of_address.h> 22 18 23 19 #include <linux/iio/iio.h> 24 20 #include <linux/iio/sysfs.h> ··· 70 70 }; 71 71 72 72 struct spear_adc_state { 73 - struct device_node *np; 73 + struct device *dev; 74 74 struct adc_regs_spear3xx __iomem *adc_base_spear3xx; 75 75 struct adc_regs_spear6xx __iomem *adc_base_spear6xx; 76 76 struct clk *clk; ··· 123 123 124 124 static u32 spear_adc_get_average(struct spear_adc_state *st) 125 125 { 126 - if (of_device_is_compatible(st->np, "st,spear600-adc")) { 126 + if (device_is_compatible(st->dev, "st,spear600-adc")) { 127 127 return __raw_readl(&st->adc_base_spear6xx->average.msb) & 128 128 SPEAR_ADC_DATA_MASK; 129 129 } else { ··· 134 134 135 135 static void spear_adc_set_scanrate(struct spear_adc_state *st, u32 rate) 136 136 { 137 - if (of_device_is_compatible(st->np, "st,spear600-adc")) { 137 + if (device_is_compatible(st->dev, "st,spear600-adc")) { 138 138 __raw_writel(SPEAR600_ADC_SCAN_RATE_LO(rate), 139 139 &st->adc_base_spear6xx->scan_rate_lo); 140 140 __raw_writel(SPEAR600_ADC_SCAN_RATE_HI(rate), ··· 266 266 267 267 static int spear_adc_probe(struct platform_device *pdev) 268 268 { 269 - struct device_node *np = pdev->dev.of_node; 270 269 struct device *dev = &pdev->dev; 271 270 struct spear_adc_state *st; 272 271 struct iio_dev *indio_dev = NULL; ··· 278 279 "failed allocating iio device\n"); 279 280 280 281 st = iio_priv(indio_dev); 282 + st->dev = dev; 281 283 282 284 mutex_init(&st->lock); 283 - 284 - st->np = np; 285 285 286 286 /* 287 287 * SPEAr600 has a different register layout than other SPEAr SoC's ··· 308 310 if (ret < 0) 309 311 return dev_err_probe(dev, ret, "failed requesting interrupt\n"); 310 312 311 - if (of_property_read_u32(np, "sampling-frequency", 312 - &st->sampling_freq)) 313 + if (device_property_read_u32(dev, "sampling-frequency", &st->sampling_freq)) 313 314 return dev_err_probe(dev, -EINVAL, 314 315 "sampling-frequency missing in DT\n"); 315 316 ··· 316 319 * Optional avg_samples defaults to 0, resulting in single data 317 320 * conversion 318 321 */ 319 - of_property_read_u32(np, "average-samples", &st->avg_samples); 322 + device_property_read_u32(dev, "average-samples", &st->avg_samples); 320 323 321 324 /* 322 325 * Optional vref_external defaults to 0, resulting in internal vref 323 326 * selection 324 327 */ 325 - of_property_read_u32(np, "vref-external", &st->vref_external); 328 + device_property_read_u32(dev, "vref-external", &st->vref_external); 326 329 327 330 spear_adc_configure(st); 328 331 ··· 343 346 return 0; 344 347 } 345 348 346 - #ifdef CONFIG_OF 347 349 static const struct of_device_id spear_adc_dt_ids[] = { 348 350 { .compatible = "st,spear600-adc", }, 349 351 { /* sentinel */ } 350 352 }; 351 353 MODULE_DEVICE_TABLE(of, spear_adc_dt_ids); 352 - #endif 353 354 354 355 static struct platform_driver spear_adc_driver = { 355 356 .probe = spear_adc_probe, 356 357 .driver = { 357 358 .name = SPEAR_ADC_MOD_NAME, 358 - .of_match_table = of_match_ptr(spear_adc_dt_ids), 359 + .of_match_table = spear_adc_dt_ids, 359 360 }, 360 361 }; 361 362
+24 -37
drivers/iio/adc/stm32-adc.c
··· 2187 2187 struct iio_chan_spec *channels) 2188 2188 { 2189 2189 const struct stm32_adc_info *adc_info = adc->cfg->adc_info; 2190 - struct fwnode_handle *child; 2190 + struct device *dev = &indio_dev->dev; 2191 2191 const char *name; 2192 2192 int val, scan_index = 0, ret; 2193 2193 bool differential; 2194 2194 u32 vin[2]; 2195 2195 2196 - device_for_each_child_node(&indio_dev->dev, child) { 2196 + device_for_each_child_node_scoped(dev, child) { 2197 2197 ret = fwnode_property_read_u32(child, "reg", &val); 2198 - if (ret) { 2199 - dev_err(&indio_dev->dev, "Missing channel index %d\n", ret); 2200 - goto err; 2201 - } 2198 + if (ret) 2199 + return dev_err_probe(dev, ret, 2200 + "Missing channel index\n"); 2202 2201 2203 2202 ret = fwnode_property_read_string(child, "label", &name); 2204 2203 /* label is optional */ 2205 2204 if (!ret) { 2206 - if (strlen(name) >= STM32_ADC_CH_SZ) { 2207 - dev_err(&indio_dev->dev, "Label %s exceeds %d characters\n", 2208 - name, STM32_ADC_CH_SZ); 2209 - ret = -EINVAL; 2210 - goto err; 2211 - } 2205 + if (strlen(name) >= STM32_ADC_CH_SZ) 2206 + return dev_err_probe(dev, -EINVAL, 2207 + "Label %s exceeds %d characters\n", 2208 + name, STM32_ADC_CH_SZ); 2209 + 2212 2210 strscpy(adc->chan_name[val], name, STM32_ADC_CH_SZ); 2213 2211 ret = stm32_adc_populate_int_ch(indio_dev, name, val); 2214 2212 if (ret == -ENOENT) 2215 2213 continue; 2216 2214 else if (ret) 2217 - goto err; 2215 + return ret; 2218 2216 } else if (ret != -EINVAL) { 2219 - dev_err(&indio_dev->dev, "Invalid label %d\n", ret); 2220 - goto err; 2217 + return dev_err_probe(dev, ret, "Invalid label\n"); 2221 2218 } 2222 2219 2223 - if (val >= adc_info->max_channels) { 2224 - dev_err(&indio_dev->dev, "Invalid channel %d\n", val); 2225 - ret = -EINVAL; 2226 - goto err; 2227 - } 2220 + if (val >= adc_info->max_channels) 2221 + return dev_err_probe(dev, -EINVAL, 2222 + "Invalid channel %d\n", val); 2228 2223 2229 2224 differential = false; 2230 2225 ret = fwnode_property_read_u32_array(child, "diff-channels", vin, 2); 2231 2226 /* diff-channels is optional */ 2232 2227 if (!ret) { 2233 2228 differential = true; 2234 - if (vin[0] != val || vin[1] >= adc_info->max_channels) { 2235 - dev_err(&indio_dev->dev, "Invalid channel in%d-in%d\n", 2236 - vin[0], vin[1]); 2237 - goto err; 2238 - } 2229 + if (vin[0] != val || vin[1] >= adc_info->max_channels) 2230 + return dev_err_probe(dev, -EINVAL, 2231 + "Invalid channel in%d-in%d\n", 2232 + vin[0], vin[1]); 2239 2233 } else if (ret != -EINVAL) { 2240 - dev_err(&indio_dev->dev, "Invalid diff-channels property %d\n", ret); 2241 - goto err; 2234 + return dev_err_probe(dev, ret, 2235 + "Invalid diff-channels property\n"); 2242 2236 } 2243 2237 2244 2238 stm32_adc_chan_init_one(indio_dev, &channels[scan_index], val, ··· 2241 2247 val = 0; 2242 2248 ret = fwnode_property_read_u32(child, "st,min-sample-time-ns", &val); 2243 2249 /* st,min-sample-time-ns is optional */ 2244 - if (ret && ret != -EINVAL) { 2245 - dev_err(&indio_dev->dev, "Invalid st,min-sample-time-ns property %d\n", 2246 - ret); 2247 - goto err; 2248 - } 2250 + if (ret && ret != -EINVAL) 2251 + return dev_err_probe(dev, ret, 2252 + "Invalid st,min-sample-time-ns property\n"); 2249 2253 2250 2254 stm32_adc_smpr_init(adc, channels[scan_index].channel, val); 2251 2255 if (differential) ··· 2253 2261 } 2254 2262 2255 2263 return scan_index; 2256 - 2257 - err: 2258 - fwnode_handle_put(child); 2259 - 2260 - return ret; 2261 2264 } 2262 2265 2263 2266 static int stm32_adc_chan_fw_init(struct iio_dev *indio_dev, bool timestamping)
+4 -8
drivers/iio/adc/ti-ads131e08.c
··· 694 694 struct ads131e08_channel_config *channel_config; 695 695 struct device *dev = &st->spi->dev; 696 696 struct iio_chan_spec *channels; 697 - struct fwnode_handle *node; 698 697 unsigned int channel, tmp; 699 698 int num_channels, i, ret; 700 699 ··· 735 736 return -ENOMEM; 736 737 737 738 i = 0; 738 - device_for_each_child_node(dev, node) { 739 + device_for_each_child_node_scoped(dev, node) { 739 740 ret = fwnode_property_read_u32(node, "reg", &channel); 740 741 if (ret) 741 - goto err_child_out; 742 + return ret; 742 743 743 744 ret = fwnode_property_read_u32(node, "ti,gain", &tmp); 744 745 if (ret) { ··· 746 747 } else { 747 748 ret = ads131e08_pga_gain_to_field_value(st, tmp); 748 749 if (ret < 0) 749 - goto err_child_out; 750 + return ret; 750 751 751 752 channel_config[i].pga_gain = tmp; 752 753 } ··· 757 758 } else { 758 759 ret = ads131e08_validate_channel_mux(st, tmp); 759 760 if (ret) 760 - goto err_child_out; 761 + return ret; 761 762 762 763 channel_config[i].mux = tmp; 763 764 } ··· 784 785 785 786 return 0; 786 787 787 - err_child_out: 788 - fwnode_handle_put(node); 789 - return ret; 790 788 } 791 789 792 790 static void ads131e08_regulator_disable(void *data)
+9 -10
drivers/iio/adc/twl4030-madc.c
··· 19 19 #include <linux/interrupt.h> 20 20 #include <linux/kernel.h> 21 21 #include <linux/delay.h> 22 + #include <linux/mod_devicetable.h> 23 + #include <linux/module.h> 22 24 #include <linux/platform_device.h> 25 + #include <linux/property.h> 23 26 #include <linux/slab.h> 24 27 #include <linux/mfd/twl.h> 25 - #include <linux/module.h> 26 28 #include <linux/stddef.h> 27 29 #include <linux/mutex.h> 28 30 #include <linux/bitops.h> ··· 32 30 #include <linux/types.h> 33 31 #include <linux/gfp.h> 34 32 #include <linux/err.h> 35 - #include <linux/of.h> 36 33 #include <linux/regulator/consumer.h> 37 34 38 35 #include <linux/iio/iio.h> ··· 745 744 */ 746 745 static int twl4030_madc_probe(struct platform_device *pdev) 747 746 { 747 + struct device *dev = &pdev->dev; 748 + struct twl4030_madc_platform_data *pdata = dev_get_platdata(dev); 748 749 struct twl4030_madc_data *madc; 749 - struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev); 750 - struct device_node *np = pdev->dev.of_node; 751 750 int irq, ret; 752 751 u8 regval; 753 752 struct iio_dev *iio_dev = NULL; 754 753 755 - if (!pdata && !np) { 754 + if (!pdata && !dev_fwnode(dev)) { 756 755 dev_err(&pdev->dev, "neither platform data nor Device Tree node available\n"); 757 756 return -EINVAL; 758 757 } ··· 780 779 if (pdata) 781 780 madc->use_second_irq = (pdata->irq_line != 1); 782 781 else 783 - madc->use_second_irq = of_property_read_bool(np, 782 + madc->use_second_irq = device_property_read_bool(dev, 784 783 "ti,system-uses-second-madc-irq"); 785 784 786 785 madc->imr = madc->use_second_irq ? TWL4030_MADC_IMR2 : ··· 906 905 regulator_disable(madc->usb3v1); 907 906 } 908 907 909 - #ifdef CONFIG_OF 910 908 static const struct of_device_id twl_madc_of_match[] = { 911 909 { .compatible = "ti,twl4030-madc", }, 912 - { }, 910 + { } 913 911 }; 914 912 MODULE_DEVICE_TABLE(of, twl_madc_of_match); 915 - #endif 916 913 917 914 static struct platform_driver twl4030_madc_driver = { 918 915 .probe = twl4030_madc_probe, 919 916 .remove_new = twl4030_madc_remove, 920 917 .driver = { 921 918 .name = "twl4030_madc", 922 - .of_match_table = of_match_ptr(twl_madc_of_match), 919 + .of_match_table = twl_madc_of_match, 923 920 }, 924 921 }; 925 922
+2 -8
drivers/iio/addac/ad74413r.c
··· 1255 1255 static int ad74413r_parse_channel_configs(struct iio_dev *indio_dev) 1256 1256 { 1257 1257 struct ad74413r_state *st = iio_priv(indio_dev); 1258 - struct fwnode_handle *channel_node = NULL; 1259 1258 int ret; 1260 1259 1261 - fwnode_for_each_available_child_node(dev_fwnode(st->dev), channel_node) { 1260 + device_for_each_child_node_scoped(st->dev, channel_node) { 1262 1261 ret = ad74413r_parse_channel_config(indio_dev, channel_node); 1263 1262 if (ret) 1264 - goto put_channel_node; 1263 + return ret; 1265 1264 } 1266 1265 1267 1266 return 0; 1268 - 1269 - put_channel_node: 1270 - fwnode_handle_put(channel_node); 1271 - 1272 - return ret; 1273 1267 } 1274 1268 1275 1269 static int ad74413r_setup_channels(struct iio_dev *indio_dev)
+79 -21
drivers/iio/buffer/industrialio-buffer-dma.c
··· 195 195 block->state = IIO_BLOCK_STATE_DONE; 196 196 } 197 197 198 + static void iio_dma_buffer_queue_wake(struct iio_dma_buffer_queue *queue) 199 + { 200 + __poll_t flags; 201 + 202 + if (queue->buffer.direction == IIO_BUFFER_DIRECTION_IN) 203 + flags = EPOLLIN | EPOLLRDNORM; 204 + else 205 + flags = EPOLLOUT | EPOLLWRNORM; 206 + 207 + wake_up_interruptible_poll(&queue->buffer.pollq, flags); 208 + } 209 + 198 210 /** 199 211 * iio_dma_buffer_block_done() - Indicate that a block has been completed 200 212 * @block: The completed block ··· 224 212 spin_unlock_irqrestore(&queue->list_lock, flags); 225 213 226 214 iio_buffer_block_put_atomic(block); 227 - wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM); 215 + iio_dma_buffer_queue_wake(queue); 228 216 } 229 217 EXPORT_SYMBOL_GPL(iio_dma_buffer_block_done); 230 218 ··· 253 241 } 254 242 spin_unlock_irqrestore(&queue->list_lock, flags); 255 243 256 - wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM); 244 + iio_dma_buffer_queue_wake(queue); 257 245 } 258 246 EXPORT_SYMBOL_GPL(iio_dma_buffer_block_list_abort); 259 247 ··· 347 335 queue->fileio.blocks[i] = block; 348 336 } 349 337 350 - block->state = IIO_BLOCK_STATE_QUEUED; 351 - list_add_tail(&block->head, &queue->incoming); 338 + /* 339 + * block->bytes_used may have been modified previously, e.g. by 340 + * iio_dma_buffer_block_list_abort(). Reset it here to the 341 + * block's so that iio_dma_buffer_io() will work. 342 + */ 343 + block->bytes_used = block->size; 344 + 345 + /* 346 + * If it's an input buffer, mark the block as queued, and 347 + * iio_dma_buffer_enable() will submit it. Otherwise mark it as 348 + * done, which means it's ready to be dequeued. 349 + */ 350 + if (queue->buffer.direction == IIO_BUFFER_DIRECTION_IN) { 351 + block->state = IIO_BLOCK_STATE_QUEUED; 352 + list_add_tail(&block->head, &queue->incoming); 353 + } else { 354 + block->state = IIO_BLOCK_STATE_DONE; 355 + } 352 356 } 353 357 354 358 out_unlock: ··· 516 488 return block; 517 489 } 518 490 519 - /** 520 - * iio_dma_buffer_read() - DMA buffer read callback 521 - * @buffer: Buffer to read form 522 - * @n: Number of bytes to read 523 - * @user_buffer: Userspace buffer to copy the data to 524 - * 525 - * Should be used as the read callback for iio_buffer_access_ops 526 - * struct for DMA buffers. 527 - */ 528 - int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, 529 - char __user *user_buffer) 491 + static int iio_dma_buffer_io(struct iio_buffer *buffer, size_t n, 492 + char __user *user_buffer, bool is_from_user) 530 493 { 531 494 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer); 532 495 struct iio_dma_buffer_block *block; 496 + void *addr; 533 497 int ret; 534 498 535 499 if (n < buffer->bytes_per_datum) ··· 544 524 n = rounddown(n, buffer->bytes_per_datum); 545 525 if (n > block->bytes_used - queue->fileio.pos) 546 526 n = block->bytes_used - queue->fileio.pos; 527 + addr = block->vaddr + queue->fileio.pos; 547 528 548 - if (copy_to_user(user_buffer, block->vaddr + queue->fileio.pos, n)) { 529 + if (is_from_user) 530 + ret = copy_from_user(addr, user_buffer, n); 531 + else 532 + ret = copy_to_user(user_buffer, addr, n); 533 + if (ret) { 549 534 ret = -EFAULT; 550 535 goto out_unlock; 551 536 } ··· 569 544 570 545 return ret; 571 546 } 547 + 548 + /** 549 + * iio_dma_buffer_read() - DMA buffer read callback 550 + * @buffer: Buffer to read form 551 + * @n: Number of bytes to read 552 + * @user_buffer: Userspace buffer to copy the data to 553 + * 554 + * Should be used as the read callback for iio_buffer_access_ops 555 + * struct for DMA buffers. 556 + */ 557 + int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, 558 + char __user *user_buffer) 559 + { 560 + return iio_dma_buffer_io(buffer, n, user_buffer, false); 561 + } 572 562 EXPORT_SYMBOL_GPL(iio_dma_buffer_read); 573 563 574 564 /** 575 - * iio_dma_buffer_data_available() - DMA buffer data_available callback 576 - * @buf: Buffer to check for data availability 565 + * iio_dma_buffer_write() - DMA buffer write callback 566 + * @buffer: Buffer to read form 567 + * @n: Number of bytes to read 568 + * @user_buffer: Userspace buffer to copy the data from 577 569 * 578 - * Should be used as the data_available callback for iio_buffer_access_ops 570 + * Should be used as the write callback for iio_buffer_access_ops 579 571 * struct for DMA buffers. 580 572 */ 581 - size_t iio_dma_buffer_data_available(struct iio_buffer *buf) 573 + int iio_dma_buffer_write(struct iio_buffer *buffer, size_t n, 574 + const char __user *user_buffer) 575 + { 576 + return iio_dma_buffer_io(buffer, n, 577 + (__force __user char *)user_buffer, true); 578 + } 579 + EXPORT_SYMBOL_GPL(iio_dma_buffer_write); 580 + 581 + /** 582 + * iio_dma_buffer_usage() - DMA buffer data_available and 583 + * space_available callback 584 + * @buf: Buffer to check for data availability 585 + * 586 + * Should be used as the data_available and space_available callbacks for 587 + * iio_buffer_access_ops struct for DMA buffers. 588 + */ 589 + size_t iio_dma_buffer_usage(struct iio_buffer *buf) 582 590 { 583 591 struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buf); 584 592 struct iio_dma_buffer_block *block; ··· 644 586 645 587 return data_available; 646 588 } 647 - EXPORT_SYMBOL_GPL(iio_dma_buffer_data_available); 589 + EXPORT_SYMBOL_GPL(iio_dma_buffer_usage); 648 590 649 591 /** 650 592 * iio_dma_buffer_set_bytes_per_datum() - DMA buffer set_bytes_per_datum callback
+48 -38
drivers/iio/buffer/industrialio-buffer-dmaengine.c
··· 64 64 struct dmaengine_buffer *dmaengine_buffer = 65 65 iio_buffer_to_dmaengine_buffer(&queue->buffer); 66 66 struct dma_async_tx_descriptor *desc; 67 + enum dma_transfer_direction dma_dir; 68 + size_t max_size; 67 69 dma_cookie_t cookie; 68 70 69 - block->bytes_used = min(block->size, dmaengine_buffer->max_size); 70 - block->bytes_used = round_down(block->bytes_used, 71 - dmaengine_buffer->align); 71 + max_size = min(block->size, dmaengine_buffer->max_size); 72 + max_size = round_down(max_size, dmaengine_buffer->align); 73 + 74 + if (queue->buffer.direction == IIO_BUFFER_DIRECTION_IN) { 75 + block->bytes_used = max_size; 76 + dma_dir = DMA_DEV_TO_MEM; 77 + } else { 78 + dma_dir = DMA_MEM_TO_DEV; 79 + } 80 + 81 + if (!block->bytes_used || block->bytes_used > max_size) 82 + return -EINVAL; 72 83 73 84 desc = dmaengine_prep_slave_single(dmaengine_buffer->chan, 74 - block->phys_addr, block->bytes_used, DMA_DEV_TO_MEM, 85 + block->phys_addr, block->bytes_used, dma_dir, 75 86 DMA_PREP_INTERRUPT); 76 87 if (!desc) 77 88 return -ENOMEM; ··· 123 112 124 113 static const struct iio_buffer_access_funcs iio_dmaengine_buffer_ops = { 125 114 .read = iio_dma_buffer_read, 115 + .write = iio_dma_buffer_write, 126 116 .set_bytes_per_datum = iio_dma_buffer_set_bytes_per_datum, 127 117 .set_length = iio_dma_buffer_set_length, 128 118 .request_update = iio_dma_buffer_request_update, 129 119 .enable = iio_dma_buffer_enable, 130 120 .disable = iio_dma_buffer_disable, 131 - .data_available = iio_dma_buffer_data_available, 121 + .data_available = iio_dma_buffer_usage, 122 + .space_available = iio_dma_buffer_usage, 132 123 .release = iio_dmaengine_buffer_release, 133 124 134 125 .modes = INDIO_BUFFER_HARDWARE, ··· 172 159 * Once done using the buffer iio_dmaengine_buffer_free() should be used to 173 160 * release it. 174 161 */ 175 - struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev, 162 + static struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev, 176 163 const char *channel) 177 164 { 178 165 struct dmaengine_buffer *dmaengine_buffer; ··· 223 210 kfree(dmaengine_buffer); 224 211 return ERR_PTR(ret); 225 212 } 226 - EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_alloc, IIO_DMAENGINE_BUFFER); 227 213 228 214 /** 229 215 * iio_dmaengine_buffer_free() - Free dmaengine buffer ··· 242 230 } 243 231 EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_free, IIO_DMAENGINE_BUFFER); 244 232 245 - static void __devm_iio_dmaengine_buffer_free(void *buffer) 246 - { 247 - iio_dmaengine_buffer_free(buffer); 248 - } 249 - 250 - /** 251 - * devm_iio_dmaengine_buffer_alloc() - Resource-managed iio_dmaengine_buffer_alloc() 252 - * @dev: Parent device for the buffer 253 - * @channel: DMA channel name, typically "rx". 254 - * 255 - * This allocates a new IIO buffer which internally uses the DMAengine framework 256 - * to perform its transfers. The parent device will be used to request the DMA 257 - * channel. 258 - * 259 - * The buffer will be automatically de-allocated once the device gets destroyed. 260 - */ 261 - static struct iio_buffer *devm_iio_dmaengine_buffer_alloc(struct device *dev, 262 - const char *channel) 233 + struct iio_buffer *iio_dmaengine_buffer_setup_ext(struct device *dev, 234 + struct iio_dev *indio_dev, 235 + const char *channel, 236 + enum iio_buffer_direction dir) 263 237 { 264 238 struct iio_buffer *buffer; 265 239 int ret; 266 240 267 241 buffer = iio_dmaengine_buffer_alloc(dev, channel); 268 242 if (IS_ERR(buffer)) 269 - return buffer; 243 + return ERR_CAST(buffer); 270 244 271 - ret = devm_add_action_or_reset(dev, __devm_iio_dmaengine_buffer_free, 272 - buffer); 273 - if (ret) 245 + indio_dev->modes |= INDIO_BUFFER_HARDWARE; 246 + 247 + buffer->direction = dir; 248 + 249 + ret = iio_device_attach_buffer(indio_dev, buffer); 250 + if (ret) { 251 + iio_dmaengine_buffer_free(buffer); 274 252 return ERR_PTR(ret); 253 + } 275 254 276 255 return buffer; 277 256 } 257 + EXPORT_SYMBOL_NS_GPL(iio_dmaengine_buffer_setup_ext, IIO_DMAENGINE_BUFFER); 258 + 259 + static void __devm_iio_dmaengine_buffer_free(void *buffer) 260 + { 261 + iio_dmaengine_buffer_free(buffer); 262 + } 278 263 279 264 /** 280 - * devm_iio_dmaengine_buffer_setup() - Setup a DMA buffer for an IIO device 265 + * devm_iio_dmaengine_buffer_setup_ext() - Setup a DMA buffer for an IIO device 281 266 * @dev: Parent device for the buffer 282 267 * @indio_dev: IIO device to which to attach this buffer. 283 268 * @channel: DMA channel name, typically "rx". 269 + * @dir: Direction of buffer (in or out) 284 270 * 285 271 * This allocates a new IIO buffer with devm_iio_dmaengine_buffer_alloc() 286 272 * and attaches it to an IIO device with iio_device_attach_buffer(). 287 273 * It also appends the INDIO_BUFFER_HARDWARE mode to the supported modes of the 288 274 * IIO device. 289 275 */ 290 - int devm_iio_dmaengine_buffer_setup(struct device *dev, 291 - struct iio_dev *indio_dev, 292 - const char *channel) 276 + int devm_iio_dmaengine_buffer_setup_ext(struct device *dev, 277 + struct iio_dev *indio_dev, 278 + const char *channel, 279 + enum iio_buffer_direction dir) 293 280 { 294 281 struct iio_buffer *buffer; 295 282 296 - buffer = devm_iio_dmaengine_buffer_alloc(dev, channel); 283 + buffer = iio_dmaengine_buffer_setup_ext(dev, indio_dev, channel, dir); 297 284 if (IS_ERR(buffer)) 298 285 return PTR_ERR(buffer); 299 286 300 - indio_dev->modes |= INDIO_BUFFER_HARDWARE; 301 - 302 - return iio_device_attach_buffer(indio_dev, buffer); 287 + return devm_add_action_or_reset(dev, __devm_iio_dmaengine_buffer_free, 288 + buffer); 303 289 } 304 - EXPORT_SYMBOL_NS_GPL(devm_iio_dmaengine_buffer_setup, IIO_DMAENGINE_BUFFER); 290 + EXPORT_SYMBOL_NS_GPL(devm_iio_dmaengine_buffer_setup_ext, IIO_DMAENGINE_BUFFER); 305 291 306 292 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 307 293 MODULE_DESCRIPTION("DMA buffer for the IIO framework");
+37
drivers/iio/dac/Kconfig
··· 131 131 Say yes here to build support for Analog Devices AD5624R, AD5644R and 132 132 AD5664R converters (DAC). This driver uses the common SPI interface. 133 133 134 + config AD9739A 135 + tristate "Analog Devices AD9739A RF DAC spi driver" 136 + depends on SPI || COMPILE_TEST 137 + select REGMAP_SPI 138 + select IIO_BACKEND 139 + help 140 + Say yes here to build support for Analog Devices AD9739A Digital-to 141 + Analog Converter. 142 + 143 + The driver requires the assistance of the AXI DAC IP core to operate, 144 + since SPI is used for configuration only, while data has to be 145 + streamed into memory via DMA. 146 + 147 + To compile this driver as a module, choose M here: the module will be 148 + called ad9739a. 149 + 150 + config ADI_AXI_DAC 151 + tristate "Analog Devices Generic AXI DAC IP core driver" 152 + select IIO_BUFFER 153 + select IIO_BUFFER_DMAENGINE 154 + select REGMAP_MMIO 155 + select IIO_BACKEND 156 + help 157 + Say yes here to build support for Analog Devices Generic 158 + AXI DAC IP core. The IP core is used for interfacing with 159 + digital-to-analog (DAC) converters that require either a high-speed 160 + serial interface (JESD204B/C) or a source synchronous parallel 161 + interface (LVDS/CMOS). 162 + Typically (for such devices) SPI will be used for configuration only, 163 + while this IP core handles the streaming of data into memory via DMA. 164 + 165 + Link: https://wiki.analog.com/resources/fpga/docs/axi_dac_ip 166 + If unsure, say N (but it's safe to say "Y"). 167 + 168 + To compile this driver as a module, choose M here: the 169 + module will be called adi-axi-dac. 170 + 134 171 config LTC2688 135 172 tristate "Analog Devices LTC2688 DAC spi driver" 136 173 depends on SPI
+2
drivers/iio/dac/Makefile
··· 29 29 obj-$(CONFIG_AD7293) += ad7293.o 30 30 obj-$(CONFIG_AD7303) += ad7303.o 31 31 obj-$(CONFIG_AD8801) += ad8801.o 32 + obj-$(CONFIG_AD9739A) += ad9739a.o 33 + obj-$(CONFIG_ADI_AXI_DAC) += adi-axi-dac.o 32 34 obj-$(CONFIG_CIO_DAC) += cio-dac.o 33 35 obj-$(CONFIG_DPOT_DAC) += dpot-dac.o 34 36 obj-$(CONFIG_DS4424) += ds4424.o
+42 -68
drivers/iio/dac/ad3552r.c
··· 801 801 u32 ch) 802 802 { 803 803 struct device *dev = &dac->spi->dev; 804 - struct fwnode_handle *gain_child; 805 804 u32 val; 806 805 int err; 807 806 u8 addr; 808 807 u16 reg = 0, offset; 809 808 810 - gain_child = fwnode_get_named_child_node(child, 811 - "custom-output-range-config"); 812 - if (!gain_child) { 813 - dev_err(dev, 814 - "mandatory custom-output-range-config property missing\n"); 815 - return -EINVAL; 816 - } 809 + struct fwnode_handle *gain_child __free(fwnode_handle) 810 + = fwnode_get_named_child_node(child, 811 + "custom-output-range-config"); 812 + if (!gain_child) 813 + return dev_err_probe(dev, -EINVAL, 814 + "mandatory custom-output-range-config property missing\n"); 817 815 818 816 dac->ch_data[ch].range_override = 1; 819 817 reg |= ad3552r_field_prep(1, AD3552R_MASK_CH_RANGE_OVERRIDE); 820 818 821 819 err = fwnode_property_read_u32(gain_child, "adi,gain-scaling-p", &val); 822 - if (err) { 823 - dev_err(dev, "mandatory adi,gain-scaling-p property missing\n"); 824 - goto put_child; 825 - } 820 + if (err) 821 + return dev_err_probe(dev, err, 822 + "mandatory adi,gain-scaling-p property missing\n"); 826 823 reg |= ad3552r_field_prep(val, AD3552R_MASK_CH_GAIN_SCALING_P); 827 824 dac->ch_data[ch].p = val; 828 825 829 826 err = fwnode_property_read_u32(gain_child, "adi,gain-scaling-n", &val); 830 - if (err) { 831 - dev_err(dev, "mandatory adi,gain-scaling-n property missing\n"); 832 - goto put_child; 833 - } 827 + if (err) 828 + return dev_err_probe(dev, err, 829 + "mandatory adi,gain-scaling-n property missing\n"); 834 830 reg |= ad3552r_field_prep(val, AD3552R_MASK_CH_GAIN_SCALING_N); 835 831 dac->ch_data[ch].n = val; 836 832 837 833 err = fwnode_property_read_u32(gain_child, "adi,rfb-ohms", &val); 838 - if (err) { 839 - dev_err(dev, "mandatory adi,rfb-ohms property missing\n"); 840 - goto put_child; 841 - } 834 + if (err) 835 + return dev_err_probe(dev, err, 836 + "mandatory adi,rfb-ohms property missing\n"); 842 837 dac->ch_data[ch].rfb = val; 843 838 844 839 err = fwnode_property_read_u32(gain_child, "adi,gain-offset", &val); 845 - if (err) { 846 - dev_err(dev, "mandatory adi,gain-offset property missing\n"); 847 - goto put_child; 848 - } 840 + if (err) 841 + return dev_err_probe(dev, err, 842 + "mandatory adi,gain-offset property missing\n"); 849 843 dac->ch_data[ch].gain_offset = val; 850 844 851 845 offset = abs((s32)val); ··· 849 855 addr = AD3552R_REG_ADDR_CH_GAIN(ch); 850 856 err = ad3552r_write_reg(dac, addr, 851 857 offset & AD3552R_MASK_CH_OFFSET_BITS_0_7); 852 - if (err) { 853 - dev_err(dev, "Error writing register\n"); 854 - goto put_child; 855 - } 858 + if (err) 859 + return dev_err_probe(dev, err, "Error writing register\n"); 856 860 857 861 err = ad3552r_write_reg(dac, addr, reg); 858 - if (err) { 859 - dev_err(dev, "Error writing register\n"); 860 - goto put_child; 861 - } 862 + if (err) 863 + return dev_err_probe(dev, err, "Error writing register\n"); 862 864 863 - put_child: 864 - fwnode_handle_put(gain_child); 865 - 866 - return err; 865 + return 0; 867 866 } 868 867 869 868 static void ad3552r_reg_disable(void *reg) ··· 867 880 static int ad3552r_configure_device(struct ad3552r_desc *dac) 868 881 { 869 882 struct device *dev = &dac->spi->dev; 870 - struct fwnode_handle *child; 871 883 struct regulator *vref; 872 884 int err, cnt = 0, voltage, delta = 100000; 873 885 u32 vals[2], val, ch; ··· 935 949 return -ENODEV; 936 950 } 937 951 938 - device_for_each_child_node(dev, child) { 952 + device_for_each_child_node_scoped(dev, child) { 939 953 err = fwnode_property_read_u32(child, "reg", &ch); 940 - if (err) { 941 - dev_err(dev, "mandatory reg property missing\n"); 942 - goto put_child; 943 - } 944 - if (ch >= AD3552R_NUM_CH) { 945 - dev_err(dev, "reg must be less than %d\n", 946 - AD3552R_NUM_CH); 947 - err = -EINVAL; 948 - goto put_child; 949 - } 954 + if (err) 955 + return dev_err_probe(dev, err, 956 + "mandatory reg property missing\n"); 957 + if (ch >= AD3552R_NUM_CH) 958 + return dev_err_probe(dev, -EINVAL, 959 + "reg must be less than %d\n", 960 + AD3552R_NUM_CH); 950 961 951 962 if (fwnode_property_present(child, "adi,output-range-microvolt")) { 952 963 err = fwnode_property_read_u32_array(child, 953 964 "adi,output-range-microvolt", 954 965 vals, 955 966 2); 956 - if (err) { 957 - dev_err(dev, 967 + if (err) 968 + return dev_err_probe(dev, err, 958 969 "adi,output-range-microvolt property could not be parsed\n"); 959 - goto put_child; 960 - } 961 970 962 971 err = ad3552r_find_range(dac->chip_id, vals); 963 - if (err < 0) { 964 - dev_err(dev, 965 - "Invalid adi,output-range-microvolt value\n"); 966 - goto put_child; 967 - } 972 + if (err < 0) 973 + return dev_err_probe(dev, err, 974 + "Invalid adi,output-range-microvolt value\n"); 975 + 968 976 val = err; 969 977 err = ad3552r_set_ch_value(dac, 970 978 AD3552R_CH_OUTPUT_RANGE_SEL, 971 979 ch, val); 972 980 if (err) 973 - goto put_child; 981 + return err; 974 982 975 983 dac->ch_data[ch].range = val; 976 984 } else if (dac->chip_id == AD3542R_ID) { 977 - dev_err(dev, 978 - "adi,output-range-microvolt is required for ad3542r\n"); 979 - err = -EINVAL; 980 - goto put_child; 985 + return dev_err_probe(dev, -EINVAL, 986 + "adi,output-range-microvolt is required for ad3542r\n"); 981 987 } else { 982 988 err = ad3552r_configure_custom_gain(dac, child, ch); 983 989 if (err) 984 - goto put_child; 990 + return err; 985 991 } 986 992 987 993 ad3552r_calc_gain_and_offset(dac, ch); ··· 981 1003 982 1004 err = ad3552r_set_ch_value(dac, AD3552R_CH_SELECT, ch, 1); 983 1005 if (err < 0) 984 - goto put_child; 1006 + return err; 985 1007 986 1008 dac->channels[cnt] = AD3552R_CH_DAC(ch); 987 1009 ++cnt; ··· 999 1021 dac->num_ch = cnt; 1000 1022 1001 1023 return 0; 1002 - put_child: 1003 - fwnode_handle_put(child); 1004 - 1005 - return err; 1006 1024 } 1007 1025 1008 1026 static int ad3552r_init(struct ad3552r_desc *dac)
+12 -12
drivers/iio/dac/ad5755.c
··· 809 809 810 810 static int ad5755_probe(struct spi_device *spi) 811 811 { 812 - enum ad5755_type type = spi_get_device_id(spi)->driver_data; 813 812 const struct ad5755_platform_data *pdata; 814 813 struct iio_dev *indio_dev; 815 814 struct ad5755_state *st; ··· 823 824 st = iio_priv(indio_dev); 824 825 spi_set_drvdata(spi, indio_dev); 825 826 826 - st->chip_info = &ad5755_chip_info_tbl[type]; 827 + st->chip_info = spi_get_device_match_data(spi); 827 828 st->spi = spi; 828 829 st->pwr_down = 0xf; 829 830 ··· 853 854 } 854 855 855 856 static const struct spi_device_id ad5755_id[] = { 856 - { "ad5755", ID_AD5755 }, 857 - { "ad5755-1", ID_AD5755 }, 858 - { "ad5757", ID_AD5757 }, 859 - { "ad5735", ID_AD5735 }, 860 - { "ad5737", ID_AD5737 }, 857 + { "ad5755", (kernel_ulong_t)&ad5755_chip_info_tbl[ID_AD5755] }, 858 + { "ad5755-1", (kernel_ulong_t)&ad5755_chip_info_tbl[ID_AD5755] }, 859 + { "ad5757", (kernel_ulong_t)&ad5755_chip_info_tbl[ID_AD5757] }, 860 + { "ad5735", (kernel_ulong_t)&ad5755_chip_info_tbl[ID_AD5735] }, 861 + { "ad5737", (kernel_ulong_t)&ad5755_chip_info_tbl[ID_AD5737] }, 861 862 {} 862 863 }; 863 864 MODULE_DEVICE_TABLE(spi, ad5755_id); 864 865 865 866 static const struct of_device_id ad5755_of_match[] = { 866 - { .compatible = "adi,ad5755" }, 867 - { .compatible = "adi,ad5755-1" }, 868 - { .compatible = "adi,ad5757" }, 869 - { .compatible = "adi,ad5735" }, 870 - { .compatible = "adi,ad5737" }, 867 + { .compatible = "adi,ad5755", &ad5755_chip_info_tbl[ID_AD5755] }, 868 + { .compatible = "adi,ad5755-1", &ad5755_chip_info_tbl[ID_AD5755] }, 869 + { .compatible = "adi,ad5757", &ad5755_chip_info_tbl[ID_AD5757] }, 870 + { .compatible = "adi,ad5735", &ad5755_chip_info_tbl[ID_AD5735] }, 871 + { .compatible = "adi,ad5737", &ad5755_chip_info_tbl[ID_AD5737] }, 871 872 { } 872 873 }; 873 874 MODULE_DEVICE_TABLE(of, ad5755_of_match); ··· 875 876 static struct spi_driver ad5755_driver = { 876 877 .driver = { 877 878 .name = "ad5755", 879 + .of_match_table = ad5755_of_match, 878 880 }, 879 881 .probe = ad5755_probe, 880 882 .id_table = ad5755_id,
+6 -13
drivers/iio/dac/ad5770r.c
··· 515 515 { 516 516 int ret, tmp[2], min, max; 517 517 unsigned int num; 518 - struct fwnode_handle *child; 519 518 520 519 num = device_get_child_node_count(&st->spi->dev); 521 520 if (num != AD5770R_MAX_CHANNELS) 522 521 return -EINVAL; 523 522 524 - device_for_each_child_node(&st->spi->dev, child) { 523 + device_for_each_child_node_scoped(&st->spi->dev, child) { 525 524 ret = fwnode_property_read_u32(child, "reg", &num); 526 525 if (ret) 527 - goto err_child_out; 528 - if (num >= AD5770R_MAX_CHANNELS) { 529 - ret = -EINVAL; 530 - goto err_child_out; 531 - } 526 + return ret; 527 + if (num >= AD5770R_MAX_CHANNELS) 528 + return -EINVAL; 532 529 533 530 ret = fwnode_property_read_u32_array(child, 534 531 "adi,range-microamp", 535 532 tmp, 2); 536 533 if (ret) 537 - goto err_child_out; 534 + return ret; 538 535 539 536 min = tmp[0] / 1000; 540 537 max = tmp[1] / 1000; 541 538 ret = ad5770r_store_output_range(st, min, max, num); 542 539 if (ret) 543 - goto err_child_out; 540 + return ret; 544 541 } 545 542 546 543 return 0; 547 - 548 - err_child_out: 549 - fwnode_handle_put(child); 550 - return ret; 551 544 } 552 545 553 546 static int ad5770r_init(struct ad5770r_state *st)
+463
drivers/iio/dac/ad9739a.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Analog Devices AD9739a SPI DAC driver 4 + * 5 + * Copyright 2015-2024 Analog Devices Inc. 6 + */ 7 + #include <linux/bitfield.h> 8 + #include <linux/bits.h> 9 + #include <linux/clk.h> 10 + #include <linux/delay.h> 11 + #include <linux/device.h> 12 + #include <linux/err.h> 13 + #include <linux/errno.h> 14 + #include <linux/gpio/consumer.h> 15 + #include <linux/minmax.h> 16 + #include <linux/module.h> 17 + #include <linux/mod_devicetable.h> 18 + #include <linux/property.h> 19 + #include <linux/regmap.h> 20 + #include <linux/spi/spi.h> 21 + #include <linux/units.h> 22 + 23 + #include <linux/iio/backend.h> 24 + #include <linux/iio/iio.h> 25 + #include <linux/iio/types.h> 26 + 27 + #define AD9739A_REG_MODE 0 28 + #define AD9739A_RESET_MASK BIT(5) 29 + #define AD9739A_REG_FSC_1 0x06 30 + #define AD9739A_REG_FSC_2 0x07 31 + #define AD9739A_FSC_MSB GENMASK(1, 0) 32 + #define AD9739A_REG_DEC_CNT 0x8 33 + #define AD9739A_NORMAL_MODE 0 34 + #define AD9739A_MIXED_MODE 2 35 + #define AD9739A_DAC_DEC GENMASK(1, 0) 36 + #define AD9739A_REG_LVDS_REC_CNT1 0x10 37 + #define AD9739A_RCVR_LOOP_EN_MASK GENMASK(1, 0) 38 + #define AD9739A_REG_LVDS_REC_CNT4 0x13 39 + #define AD9739A_FINE_DEL_SKW_MASK GENMASK(3, 0) 40 + #define AD9739A_REG_LVDS_REC_STAT9 0x21 41 + #define AD9739A_RCVR_TRACK_AND_LOCK (BIT(3) | BIT(0)) 42 + #define AD9739A_REG_CROSS_CNT1 0x22 43 + #define AD9739A_REG_CROSS_CNT2 0x23 44 + #define AD9739A_REG_PHS_DET 0x24 45 + #define AD9739A_REG_MU_DUTY 0x25 46 + #define AD9739A_REG_MU_CNT1 0x26 47 + #define AD9739A_MU_EN_MASK BIT(0) 48 + #define AD9739A_REG_MU_CNT2 0x27 49 + #define AD9739A_REG_MU_CNT3 0x28 50 + #define AD9739A_REG_MU_CNT4 0x29 51 + #define AD9739A_MU_CNT4_DEFAULT 0xcb 52 + #define AD9739A_REG_MU_STAT1 0x2A 53 + #define AD9739A_MU_LOCK_MASK BIT(0) 54 + #define AD9739A_REG_ANA_CNT_1 0x32 55 + #define AD9739A_REG_ID 0x35 56 + 57 + #define AD9739A_ID 0x24 58 + #define AD9739A_REG_IS_RESERVED(reg) \ 59 + ((reg) == 0x5 || (reg) == 0x9 || (reg) == 0x0E || (reg) == 0x0D || \ 60 + (reg) == 0x2B || (reg) == 0x2C || (reg) == 0x34) 61 + 62 + #define AD9739A_FSC_MIN 8580 63 + #define AD9739A_FSC_MAX 31700 64 + #define AD9739A_FSC_RANGE (AD9739A_FSC_MAX - AD9739A_FSC_MIN + 1) 65 + 66 + #define AD9739A_MIN_DAC_CLK (1600 * MEGA) 67 + #define AD9739A_MAX_DAC_CLK (2500 * MEGA) 68 + #define AD9739A_DAC_CLK_RANGE (AD9739A_MAX_DAC_CLK - AD9739A_MIN_DAC_CLK + 1) 69 + /* as recommended by the datasheet */ 70 + #define AD9739A_LOCK_N_TRIES 3 71 + 72 + struct ad9739a_state { 73 + struct iio_backend *back; 74 + struct regmap *regmap; 75 + unsigned long sample_rate; 76 + }; 77 + 78 + static int ad9739a_oper_mode_get(struct iio_dev *indio_dev, 79 + const struct iio_chan_spec *chan) 80 + { 81 + struct ad9739a_state *st = iio_priv(indio_dev); 82 + u32 mode; 83 + int ret; 84 + 85 + ret = regmap_read(st->regmap, AD9739A_REG_DEC_CNT, &mode); 86 + if (ret) 87 + return ret; 88 + 89 + mode = FIELD_GET(AD9739A_DAC_DEC, mode); 90 + /* sanity check we get valid values from the HW */ 91 + if (mode != AD9739A_NORMAL_MODE && mode != AD9739A_MIXED_MODE) 92 + return -EIO; 93 + if (!mode) 94 + return AD9739A_NORMAL_MODE; 95 + 96 + /* 97 + * We get 2 from the device but for IIO modes, that means 1. Hence the 98 + * minus 1. 99 + */ 100 + return AD9739A_MIXED_MODE - 1; 101 + } 102 + 103 + static int ad9739a_oper_mode_set(struct iio_dev *indio_dev, 104 + const struct iio_chan_spec *chan, u32 mode) 105 + { 106 + struct ad9739a_state *st = iio_priv(indio_dev); 107 + 108 + /* 109 + * On the IIO interface we have 0 and 1 for mode. But for mixed_mode, we 110 + * need to write 2 in the device. That's what the below check is about. 111 + */ 112 + if (mode == AD9739A_MIXED_MODE - 1) 113 + mode = AD9739A_MIXED_MODE; 114 + 115 + return regmap_update_bits(st->regmap, AD9739A_REG_DEC_CNT, 116 + AD9739A_DAC_DEC, mode); 117 + } 118 + 119 + static int ad9739a_read_raw(struct iio_dev *indio_dev, 120 + struct iio_chan_spec const *chan, 121 + int *val, int *val2, long mask) 122 + { 123 + struct ad9739a_state *st = iio_priv(indio_dev); 124 + 125 + switch (mask) { 126 + case IIO_CHAN_INFO_SAMP_FREQ: 127 + *val = st->sample_rate; 128 + *val2 = 0; 129 + return IIO_VAL_INT_64; 130 + default: 131 + return -EINVAL; 132 + } 133 + } 134 + 135 + static int ad9739a_buffer_preenable(struct iio_dev *indio_dev) 136 + { 137 + struct ad9739a_state *st = iio_priv(indio_dev); 138 + 139 + return iio_backend_data_source_set(st->back, 0, IIO_BACKEND_EXTERNAL); 140 + } 141 + 142 + static int ad9739a_buffer_postdisable(struct iio_dev *indio_dev) 143 + { 144 + struct ad9739a_state *st = iio_priv(indio_dev); 145 + 146 + return iio_backend_data_source_set(st->back, 0, 147 + IIO_BACKEND_INTERNAL_CONTINUOS_WAVE); 148 + } 149 + 150 + static bool ad9739a_reg_accessible(struct device *dev, unsigned int reg) 151 + { 152 + if (AD9739A_REG_IS_RESERVED(reg)) 153 + return false; 154 + if (reg > AD9739A_REG_MU_STAT1 && reg < AD9739A_REG_ANA_CNT_1) 155 + return false; 156 + 157 + return true; 158 + } 159 + 160 + static int ad9739a_reset(struct device *dev, const struct ad9739a_state *st) 161 + { 162 + struct gpio_desc *gpio; 163 + int ret; 164 + 165 + gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 166 + if (IS_ERR(gpio)) 167 + return PTR_ERR(gpio); 168 + if (gpio) { 169 + /* minimum pulse width of 40ns */ 170 + ndelay(40); 171 + gpiod_set_value_cansleep(gpio, 0); 172 + return 0; 173 + } 174 + 175 + /* bring all registers to their default state */ 176 + ret = regmap_set_bits(st->regmap, AD9739A_REG_MODE, AD9739A_RESET_MASK); 177 + if (ret) 178 + return ret; 179 + 180 + ndelay(40); 181 + 182 + return regmap_clear_bits(st->regmap, AD9739A_REG_MODE, 183 + AD9739A_RESET_MASK); 184 + } 185 + 186 + /* 187 + * Recommended values (as per datasheet) for the dac clk common mode voltage 188 + * and Mu controller. Look at table 29. 189 + */ 190 + static const struct reg_sequence ad9739a_clk_mu_ctrl[] = { 191 + /* DAC clk common mode voltage */ 192 + { AD9739A_REG_CROSS_CNT1, 0x0f }, 193 + { AD9739A_REG_CROSS_CNT2, 0x0f }, 194 + /* Mu controller configuration */ 195 + { AD9739A_REG_PHS_DET, 0x30 }, 196 + { AD9739A_REG_MU_DUTY, 0x80 }, 197 + { AD9739A_REG_MU_CNT2, 0x44 }, 198 + { AD9739A_REG_MU_CNT3, 0x6c }, 199 + }; 200 + 201 + static int ad9739a_init(struct device *dev, const struct ad9739a_state *st) 202 + { 203 + unsigned int i = 0, lock, fsc; 204 + u32 fsc_raw; 205 + int ret; 206 + 207 + ret = regmap_multi_reg_write(st->regmap, ad9739a_clk_mu_ctrl, 208 + ARRAY_SIZE(ad9739a_clk_mu_ctrl)); 209 + if (ret) 210 + return ret; 211 + 212 + /* 213 + * Try to get the Mu lock. Repeat the below steps AD9739A_LOCK_N_TRIES 214 + * (as specified by the datasheet) until we get the lock. 215 + */ 216 + do { 217 + ret = regmap_write(st->regmap, AD9739A_REG_MU_CNT4, 218 + AD9739A_MU_CNT4_DEFAULT); 219 + if (ret) 220 + return ret; 221 + 222 + /* Enable the Mu controller search and track mode. */ 223 + ret = regmap_set_bits(st->regmap, AD9739A_REG_MU_CNT1, 224 + AD9739A_MU_EN_MASK); 225 + if (ret) 226 + return ret; 227 + 228 + /* Ensure the DLL loop is locked */ 229 + ret = regmap_read_poll_timeout(st->regmap, AD9739A_REG_MU_STAT1, 230 + lock, lock & AD9739A_MU_LOCK_MASK, 231 + 0, 1000); 232 + if (ret && ret != -ETIMEDOUT) 233 + return ret; 234 + } while (ret && ++i < AD9739A_LOCK_N_TRIES); 235 + 236 + if (i == AD9739A_LOCK_N_TRIES) 237 + return dev_err_probe(dev, ret, "Mu lock timeout\n"); 238 + 239 + /* Receiver tracking and lock. Same deal as the Mu controller */ 240 + i = 0; 241 + do { 242 + ret = regmap_update_bits(st->regmap, AD9739A_REG_LVDS_REC_CNT4, 243 + AD9739A_FINE_DEL_SKW_MASK, 244 + FIELD_PREP(AD9739A_FINE_DEL_SKW_MASK, 2)); 245 + if (ret) 246 + return ret; 247 + 248 + /* Disable the receiver and the loop. */ 249 + ret = regmap_write(st->regmap, AD9739A_REG_LVDS_REC_CNT1, 0); 250 + if (ret) 251 + return ret; 252 + 253 + /* 254 + * Re-enable the loop so it falls out of lock and begins the 255 + * search/track routine again. 256 + */ 257 + ret = regmap_set_bits(st->regmap, AD9739A_REG_LVDS_REC_CNT1, 258 + AD9739A_RCVR_LOOP_EN_MASK); 259 + if (ret) 260 + return ret; 261 + 262 + /* Ensure the DLL loop is locked */ 263 + ret = regmap_read_poll_timeout(st->regmap, 264 + AD9739A_REG_LVDS_REC_STAT9, lock, 265 + lock == AD9739A_RCVR_TRACK_AND_LOCK, 266 + 0, 1000); 267 + if (ret && ret != -ETIMEDOUT) 268 + return ret; 269 + } while (ret && ++i < AD9739A_LOCK_N_TRIES); 270 + 271 + if (i == AD9739A_LOCK_N_TRIES) 272 + return dev_err_probe(dev, ret, "Receiver lock timeout\n"); 273 + 274 + ret = device_property_read_u32(dev, "adi,full-scale-microamp", &fsc); 275 + if (ret && ret == -EINVAL) 276 + return 0; 277 + if (ret) 278 + return ret; 279 + if (!in_range(fsc, AD9739A_FSC_MIN, AD9739A_FSC_RANGE)) 280 + return dev_err_probe(dev, -EINVAL, 281 + "Invalid full scale current(%u) [%u %u]\n", 282 + fsc, AD9739A_FSC_MIN, AD9739A_FSC_MAX); 283 + /* 284 + * IOUTFS is given by 285 + * Ioutfs = 0.0226 * FSC + 8.58 286 + * and is given in mA. Hence we'll have to multiply by 10 * MILLI in 287 + * order to get rid of the fractional. 288 + */ 289 + fsc_raw = DIV_ROUND_CLOSEST(fsc * 10 - 85800, 226); 290 + 291 + ret = regmap_write(st->regmap, AD9739A_REG_FSC_1, fsc_raw & 0xff); 292 + if (ret) 293 + return ret; 294 + 295 + return regmap_update_bits(st->regmap, AD9739A_REG_FSC_2, 296 + AD9739A_FSC_MSB, fsc_raw >> 8); 297 + } 298 + 299 + static const char * const ad9739a_modes_avail[] = { "normal", "mixed-mode" }; 300 + 301 + static const struct iio_enum ad9739a_modes = { 302 + .items = ad9739a_modes_avail, 303 + .num_items = ARRAY_SIZE(ad9739a_modes_avail), 304 + .get = ad9739a_oper_mode_get, 305 + .set = ad9739a_oper_mode_set, 306 + }; 307 + 308 + static const struct iio_chan_spec_ext_info ad9739a_ext_info[] = { 309 + IIO_ENUM_AVAILABLE("operating_mode", IIO_SEPARATE, &ad9739a_modes), 310 + IIO_ENUM("operating_mode", IIO_SEPARATE, &ad9739a_modes), 311 + { } 312 + }; 313 + 314 + /* 315 + * The reason for having two different channels is because we have, in reality, 316 + * two sources of data: 317 + * ALTVOLTAGE: It's a Continuous Wave that's internally generated by the 318 + * backend device. 319 + * VOLTAGE: It's the typical data we can have in a DAC device and the source 320 + * of it has nothing to do with the backend. The backend will only 321 + * forward it into our data interface to be sent out. 322 + */ 323 + static struct iio_chan_spec ad9739a_channels[] = { 324 + { 325 + .type = IIO_ALTVOLTAGE, 326 + .indexed = 1, 327 + .output = 1, 328 + .scan_index = -1, 329 + }, 330 + { 331 + .type = IIO_VOLTAGE, 332 + .indexed = 1, 333 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 334 + .output = 1, 335 + .ext_info = ad9739a_ext_info, 336 + .scan_type = { 337 + .sign = 's', 338 + .storagebits = 16, 339 + .realbits = 16, 340 + }, 341 + } 342 + }; 343 + 344 + static const struct iio_info ad9739a_info = { 345 + .read_raw = ad9739a_read_raw, 346 + }; 347 + 348 + static const struct iio_buffer_setup_ops ad9739a_buffer_setup_ops = { 349 + .preenable = &ad9739a_buffer_preenable, 350 + .postdisable = &ad9739a_buffer_postdisable, 351 + }; 352 + 353 + static const struct regmap_config ad9739a_regmap_config = { 354 + .reg_bits = 8, 355 + .val_bits = 8, 356 + .readable_reg = ad9739a_reg_accessible, 357 + .writeable_reg = ad9739a_reg_accessible, 358 + .max_register = AD9739A_REG_ID, 359 + }; 360 + 361 + static int ad9739a_probe(struct spi_device *spi) 362 + { 363 + struct device *dev = &spi->dev; 364 + struct iio_dev *indio_dev; 365 + struct ad9739a_state *st; 366 + unsigned int id; 367 + struct clk *clk; 368 + int ret; 369 + 370 + indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 371 + if (!indio_dev) 372 + return -ENOMEM; 373 + 374 + st = iio_priv(indio_dev); 375 + 376 + clk = devm_clk_get_enabled(dev, NULL); 377 + if (IS_ERR(clk)) 378 + return dev_err_probe(dev, PTR_ERR(clk), "Could not get clkin\n"); 379 + 380 + st->sample_rate = clk_get_rate(clk); 381 + if (!in_range(st->sample_rate, AD9739A_MIN_DAC_CLK, 382 + AD9739A_DAC_CLK_RANGE)) 383 + return dev_err_probe(dev, -EINVAL, 384 + "Invalid dac clk range(%lu) [%lu %lu]\n", 385 + st->sample_rate, AD9739A_MIN_DAC_CLK, 386 + AD9739A_MAX_DAC_CLK); 387 + 388 + st->regmap = devm_regmap_init_spi(spi, &ad9739a_regmap_config); 389 + if (IS_ERR(st->regmap)) 390 + return PTR_ERR(st->regmap); 391 + 392 + ret = regmap_read(st->regmap, AD9739A_REG_ID, &id); 393 + if (ret) 394 + return ret; 395 + 396 + if (id != AD9739A_ID) 397 + dev_warn(dev, "Unrecognized CHIP_ID 0x%X", id); 398 + 399 + ret = ad9739a_reset(dev, st); 400 + if (ret) 401 + return ret; 402 + 403 + ret = ad9739a_init(dev, st); 404 + if (ret) 405 + return ret; 406 + 407 + st->back = devm_iio_backend_get(dev, NULL); 408 + if (IS_ERR(st->back)) 409 + return PTR_ERR(st->back); 410 + 411 + ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev); 412 + if (ret) 413 + return ret; 414 + 415 + ret = iio_backend_extend_chan_spec(indio_dev, st->back, 416 + &ad9739a_channels[0]); 417 + if (ret) 418 + return ret; 419 + 420 + ret = iio_backend_set_sampling_freq(st->back, 0, st->sample_rate); 421 + if (ret) 422 + return ret; 423 + 424 + ret = devm_iio_backend_enable(dev, st->back); 425 + if (ret) 426 + return ret; 427 + 428 + indio_dev->name = "ad9739a"; 429 + indio_dev->info = &ad9739a_info; 430 + indio_dev->channels = ad9739a_channels; 431 + indio_dev->num_channels = ARRAY_SIZE(ad9739a_channels); 432 + indio_dev->setup_ops = &ad9739a_buffer_setup_ops; 433 + 434 + return devm_iio_device_register(&spi->dev, indio_dev); 435 + } 436 + 437 + static const struct of_device_id ad9739a_of_match[] = { 438 + { .compatible = "adi,ad9739a" }, 439 + {} 440 + }; 441 + MODULE_DEVICE_TABLE(of, ad9739a_of_match); 442 + 443 + static const struct spi_device_id ad9739a_id[] = { 444 + {"ad9739a"}, 445 + {} 446 + }; 447 + MODULE_DEVICE_TABLE(spi, ad9739a_id); 448 + 449 + static struct spi_driver ad9739a_driver = { 450 + .driver = { 451 + .name = "ad9739a", 452 + .of_match_table = ad9739a_of_match, 453 + }, 454 + .probe = ad9739a_probe, 455 + .id_table = ad9739a_id, 456 + }; 457 + module_spi_driver(ad9739a_driver); 458 + 459 + MODULE_AUTHOR("Dragos Bogdan <dragos.bogdan@analog.com>"); 460 + MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>"); 461 + MODULE_DESCRIPTION("Analog Devices AD9739 DAC"); 462 + MODULE_LICENSE("GPL"); 463 + MODULE_IMPORT_NS(IIO_BACKEND);
+635
drivers/iio/dac/adi-axi-dac.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Analog Devices Generic AXI DAC IP core 4 + * Link: https://wiki.analog.com/resources/fpga/docs/axi_dac_ip 5 + * 6 + * Copyright 2016-2024 Analog Devices Inc. 7 + */ 8 + #include <linux/bitfield.h> 9 + #include <linux/bits.h> 10 + #include <linux/cleanup.h> 11 + #include <linux/clk.h> 12 + #include <linux/device.h> 13 + #include <linux/err.h> 14 + #include <linux/limits.h> 15 + #include <linux/kstrtox.h> 16 + #include <linux/math.h> 17 + #include <linux/math64.h> 18 + #include <linux/module.h> 19 + #include <linux/mod_devicetable.h> 20 + #include <linux/mutex.h> 21 + #include <linux/platform_device.h> 22 + #include <linux/property.h> 23 + #include <linux/regmap.h> 24 + #include <linux/units.h> 25 + 26 + #include <linux/fpga/adi-axi-common.h> 27 + #include <linux/iio/backend.h> 28 + #include <linux/iio/buffer-dmaengine.h> 29 + #include <linux/iio/buffer.h> 30 + #include <linux/iio/iio.h> 31 + 32 + /* 33 + * Register definitions: 34 + * https://wiki.analog.com/resources/fpga/docs/axi_dac_ip#register_map 35 + */ 36 + 37 + /* Base controls */ 38 + #define AXI_DAC_REG_CONFIG 0x0c 39 + #define AXI_DDS_DISABLE BIT(6) 40 + 41 + /* DAC controls */ 42 + #define AXI_DAC_REG_RSTN 0x0040 43 + #define AXI_DAC_RSTN_CE_N BIT(2) 44 + #define AXI_DAC_RSTN_MMCM_RSTN BIT(1) 45 + #define AXI_DAC_RSTN_RSTN BIT(0) 46 + #define AXI_DAC_REG_CNTRL_1 0x0044 47 + #define AXI_DAC_SYNC BIT(0) 48 + #define AXI_DAC_REG_CNTRL_2 0x0048 49 + #define ADI_DAC_R1_MODE BIT(4) 50 + #define AXI_DAC_DRP_STATUS 0x0074 51 + #define AXI_DAC_DRP_LOCKED BIT(17) 52 + /* DAC Channel controls */ 53 + #define AXI_DAC_REG_CHAN_CNTRL_1(c) (0x0400 + (c) * 0x40) 54 + #define AXI_DAC_REG_CHAN_CNTRL_3(c) (0x0408 + (c) * 0x40) 55 + #define AXI_DAC_SCALE_SIGN BIT(15) 56 + #define AXI_DAC_SCALE_INT BIT(14) 57 + #define AXI_DAC_SCALE GENMASK(14, 0) 58 + #define AXI_DAC_REG_CHAN_CNTRL_2(c) (0x0404 + (c) * 0x40) 59 + #define AXI_DAC_REG_CHAN_CNTRL_4(c) (0x040c + (c) * 0x40) 60 + #define AXI_DAC_PHASE GENMASK(31, 16) 61 + #define AXI_DAC_FREQUENCY GENMASK(15, 0) 62 + #define AXI_DAC_REG_CHAN_CNTRL_7(c) (0x0418 + (c) * 0x40) 63 + #define AXI_DAC_DATA_SEL GENMASK(3, 0) 64 + 65 + /* 360 degrees in rad */ 66 + #define AXI_DAC_2_PI_MEGA 6283190 67 + enum { 68 + AXI_DAC_DATA_INTERNAL_TONE, 69 + AXI_DAC_DATA_DMA = 2, 70 + }; 71 + 72 + struct axi_dac_state { 73 + struct regmap *regmap; 74 + struct device *dev; 75 + /* 76 + * lock to protect multiple accesses to the device registers and global 77 + * data/variables. 78 + */ 79 + struct mutex lock; 80 + u64 dac_clk; 81 + u32 reg_config; 82 + bool int_tone; 83 + }; 84 + 85 + static int axi_dac_enable(struct iio_backend *back) 86 + { 87 + struct axi_dac_state *st = iio_backend_get_priv(back); 88 + unsigned int __val; 89 + int ret; 90 + 91 + guard(mutex)(&st->lock); 92 + ret = regmap_set_bits(st->regmap, AXI_DAC_REG_RSTN, 93 + AXI_DAC_RSTN_MMCM_RSTN); 94 + if (ret) 95 + return ret; 96 + /* 97 + * Make sure the DRP (Dynamic Reconfiguration Port) is locked. Not all 98 + * designs really use it but if they don't we still get the lock bit 99 + * set. So let's do it all the time so the code is generic. 100 + */ 101 + ret = regmap_read_poll_timeout(st->regmap, AXI_DAC_DRP_STATUS, __val, 102 + __val & AXI_DAC_DRP_LOCKED, 100, 1000); 103 + if (ret) 104 + return ret; 105 + 106 + return regmap_set_bits(st->regmap, AXI_DAC_REG_RSTN, 107 + AXI_DAC_RSTN_RSTN | AXI_DAC_RSTN_MMCM_RSTN); 108 + } 109 + 110 + static void axi_dac_disable(struct iio_backend *back) 111 + { 112 + struct axi_dac_state *st = iio_backend_get_priv(back); 113 + 114 + guard(mutex)(&st->lock); 115 + regmap_write(st->regmap, AXI_DAC_REG_RSTN, 0); 116 + } 117 + 118 + static struct iio_buffer *axi_dac_request_buffer(struct iio_backend *back, 119 + struct iio_dev *indio_dev) 120 + { 121 + struct axi_dac_state *st = iio_backend_get_priv(back); 122 + const char *dma_name; 123 + 124 + if (device_property_read_string(st->dev, "dma-names", &dma_name)) 125 + dma_name = "tx"; 126 + 127 + return iio_dmaengine_buffer_setup_ext(st->dev, indio_dev, dma_name, 128 + IIO_BUFFER_DIRECTION_OUT); 129 + } 130 + 131 + static void axi_dac_free_buffer(struct iio_backend *back, 132 + struct iio_buffer *buffer) 133 + { 134 + iio_dmaengine_buffer_free(buffer); 135 + } 136 + 137 + enum { 138 + AXI_DAC_FREQ_TONE_1, 139 + AXI_DAC_FREQ_TONE_2, 140 + AXI_DAC_SCALE_TONE_1, 141 + AXI_DAC_SCALE_TONE_2, 142 + AXI_DAC_PHASE_TONE_1, 143 + AXI_DAC_PHASE_TONE_2, 144 + }; 145 + 146 + static int __axi_dac_frequency_get(struct axi_dac_state *st, unsigned int chan, 147 + unsigned int tone_2, unsigned int *freq) 148 + { 149 + u32 reg, raw; 150 + int ret; 151 + 152 + if (!st->dac_clk) { 153 + dev_err(st->dev, "Sampling rate is 0...\n"); 154 + return -EINVAL; 155 + } 156 + 157 + if (tone_2) 158 + reg = AXI_DAC_REG_CHAN_CNTRL_4(chan); 159 + else 160 + reg = AXI_DAC_REG_CHAN_CNTRL_2(chan); 161 + 162 + ret = regmap_read(st->regmap, reg, &raw); 163 + if (ret) 164 + return ret; 165 + 166 + raw = FIELD_GET(AXI_DAC_FREQUENCY, raw); 167 + *freq = DIV_ROUND_CLOSEST_ULL(raw * st->dac_clk, BIT(16)); 168 + 169 + return 0; 170 + } 171 + 172 + static int axi_dac_frequency_get(struct axi_dac_state *st, 173 + const struct iio_chan_spec *chan, char *buf, 174 + unsigned int tone_2) 175 + { 176 + unsigned int freq; 177 + int ret; 178 + 179 + scoped_guard(mutex, &st->lock) { 180 + ret = __axi_dac_frequency_get(st, chan->channel, tone_2, &freq); 181 + if (ret) 182 + return ret; 183 + } 184 + 185 + return sysfs_emit(buf, "%u\n", freq); 186 + } 187 + 188 + static int axi_dac_scale_get(struct axi_dac_state *st, 189 + const struct iio_chan_spec *chan, char *buf, 190 + unsigned int tone_2) 191 + { 192 + unsigned int scale, sign; 193 + int ret, vals[2]; 194 + u32 reg, raw; 195 + 196 + if (tone_2) 197 + reg = AXI_DAC_REG_CHAN_CNTRL_3(chan->channel); 198 + else 199 + reg = AXI_DAC_REG_CHAN_CNTRL_1(chan->channel); 200 + 201 + ret = regmap_read(st->regmap, reg, &raw); 202 + if (ret) 203 + return ret; 204 + 205 + sign = FIELD_GET(AXI_DAC_SCALE_SIGN, raw); 206 + raw = FIELD_GET(AXI_DAC_SCALE, raw); 207 + scale = DIV_ROUND_CLOSEST_ULL((u64)raw * MEGA, AXI_DAC_SCALE_INT); 208 + 209 + vals[0] = scale / MEGA; 210 + vals[1] = scale % MEGA; 211 + 212 + if (sign) { 213 + vals[0] *= -1; 214 + if (!vals[0]) 215 + vals[1] *= -1; 216 + } 217 + 218 + return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, ARRAY_SIZE(vals), 219 + vals); 220 + } 221 + 222 + static int axi_dac_phase_get(struct axi_dac_state *st, 223 + const struct iio_chan_spec *chan, char *buf, 224 + unsigned int tone_2) 225 + { 226 + u32 reg, raw, phase; 227 + int ret, vals[2]; 228 + 229 + if (tone_2) 230 + reg = AXI_DAC_REG_CHAN_CNTRL_4(chan->channel); 231 + else 232 + reg = AXI_DAC_REG_CHAN_CNTRL_2(chan->channel); 233 + 234 + ret = regmap_read(st->regmap, reg, &raw); 235 + if (ret) 236 + return ret; 237 + 238 + raw = FIELD_GET(AXI_DAC_PHASE, raw); 239 + phase = DIV_ROUND_CLOSEST_ULL((u64)raw * AXI_DAC_2_PI_MEGA, U16_MAX); 240 + 241 + vals[0] = phase / MEGA; 242 + vals[1] = phase % MEGA; 243 + 244 + return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, ARRAY_SIZE(vals), 245 + vals); 246 + } 247 + 248 + static int __axi_dac_frequency_set(struct axi_dac_state *st, unsigned int chan, 249 + u64 sample_rate, unsigned int freq, 250 + unsigned int tone_2) 251 + { 252 + u32 reg; 253 + u16 raw; 254 + int ret; 255 + 256 + if (!sample_rate || freq > sample_rate / 2) { 257 + dev_err(st->dev, "Invalid frequency(%u) dac_clk(%llu)\n", 258 + freq, sample_rate); 259 + return -EINVAL; 260 + } 261 + 262 + if (tone_2) 263 + reg = AXI_DAC_REG_CHAN_CNTRL_4(chan); 264 + else 265 + reg = AXI_DAC_REG_CHAN_CNTRL_2(chan); 266 + 267 + raw = DIV64_U64_ROUND_CLOSEST((u64)freq * BIT(16), sample_rate); 268 + 269 + ret = regmap_update_bits(st->regmap, reg, AXI_DAC_FREQUENCY, raw); 270 + if (ret) 271 + return ret; 272 + 273 + /* synchronize channels */ 274 + return regmap_set_bits(st->regmap, AXI_DAC_REG_CNTRL_1, AXI_DAC_SYNC); 275 + } 276 + 277 + static int axi_dac_frequency_set(struct axi_dac_state *st, 278 + const struct iio_chan_spec *chan, 279 + const char *buf, size_t len, unsigned int tone_2) 280 + { 281 + unsigned int freq; 282 + int ret; 283 + 284 + ret = kstrtou32(buf, 10, &freq); 285 + if (ret) 286 + return ret; 287 + 288 + guard(mutex)(&st->lock); 289 + ret = __axi_dac_frequency_set(st, chan->channel, st->dac_clk, freq, 290 + tone_2); 291 + if (ret) 292 + return ret; 293 + 294 + return len; 295 + } 296 + 297 + static int axi_dac_scale_set(struct axi_dac_state *st, 298 + const struct iio_chan_spec *chan, 299 + const char *buf, size_t len, unsigned int tone_2) 300 + { 301 + int integer, frac, scale; 302 + u32 raw = 0, reg; 303 + int ret; 304 + 305 + ret = iio_str_to_fixpoint(buf, 100000, &integer, &frac); 306 + if (ret) 307 + return ret; 308 + 309 + scale = integer * MEGA + frac; 310 + if (scale <= -2 * (int)MEGA || scale >= 2 * (int)MEGA) 311 + return -EINVAL; 312 + 313 + /* format is 1.1.14 (sign, integer and fractional bits) */ 314 + if (scale < 0) { 315 + raw = FIELD_PREP(AXI_DAC_SCALE_SIGN, 1); 316 + scale *= -1; 317 + } 318 + 319 + raw |= div_u64((u64)scale * AXI_DAC_SCALE_INT, MEGA); 320 + 321 + if (tone_2) 322 + reg = AXI_DAC_REG_CHAN_CNTRL_3(chan->channel); 323 + else 324 + reg = AXI_DAC_REG_CHAN_CNTRL_1(chan->channel); 325 + 326 + guard(mutex)(&st->lock); 327 + ret = regmap_write(st->regmap, reg, raw); 328 + if (ret) 329 + return ret; 330 + 331 + /* synchronize channels */ 332 + ret = regmap_set_bits(st->regmap, AXI_DAC_REG_CNTRL_1, AXI_DAC_SYNC); 333 + if (ret) 334 + return ret; 335 + 336 + return len; 337 + } 338 + 339 + static int axi_dac_phase_set(struct axi_dac_state *st, 340 + const struct iio_chan_spec *chan, 341 + const char *buf, size_t len, unsigned int tone_2) 342 + { 343 + int integer, frac, phase; 344 + u32 raw, reg; 345 + int ret; 346 + 347 + ret = iio_str_to_fixpoint(buf, 100000, &integer, &frac); 348 + if (ret) 349 + return ret; 350 + 351 + phase = integer * MEGA + frac; 352 + if (phase < 0 || phase > AXI_DAC_2_PI_MEGA) 353 + return -EINVAL; 354 + 355 + raw = DIV_ROUND_CLOSEST_ULL((u64)phase * U16_MAX, AXI_DAC_2_PI_MEGA); 356 + 357 + if (tone_2) 358 + reg = AXI_DAC_REG_CHAN_CNTRL_4(chan->channel); 359 + else 360 + reg = AXI_DAC_REG_CHAN_CNTRL_2(chan->channel); 361 + 362 + guard(mutex)(&st->lock); 363 + ret = regmap_update_bits(st->regmap, reg, AXI_DAC_PHASE, 364 + FIELD_PREP(AXI_DAC_PHASE, raw)); 365 + if (ret) 366 + return ret; 367 + 368 + /* synchronize channels */ 369 + ret = regmap_set_bits(st->regmap, AXI_DAC_REG_CNTRL_1, AXI_DAC_SYNC); 370 + if (ret) 371 + return ret; 372 + 373 + return len; 374 + } 375 + 376 + static int axi_dac_ext_info_set(struct iio_backend *back, uintptr_t private, 377 + const struct iio_chan_spec *chan, 378 + const char *buf, size_t len) 379 + { 380 + struct axi_dac_state *st = iio_backend_get_priv(back); 381 + 382 + switch (private) { 383 + case AXI_DAC_FREQ_TONE_1: 384 + case AXI_DAC_FREQ_TONE_2: 385 + return axi_dac_frequency_set(st, chan, buf, len, 386 + private - AXI_DAC_FREQ_TONE_1); 387 + case AXI_DAC_SCALE_TONE_1: 388 + case AXI_DAC_SCALE_TONE_2: 389 + return axi_dac_scale_set(st, chan, buf, len, 390 + private - AXI_DAC_SCALE_TONE_1); 391 + case AXI_DAC_PHASE_TONE_1: 392 + case AXI_DAC_PHASE_TONE_2: 393 + return axi_dac_phase_set(st, chan, buf, len, 394 + private - AXI_DAC_PHASE_TONE_2); 395 + default: 396 + return -EOPNOTSUPP; 397 + } 398 + } 399 + 400 + static int axi_dac_ext_info_get(struct iio_backend *back, uintptr_t private, 401 + const struct iio_chan_spec *chan, char *buf) 402 + { 403 + struct axi_dac_state *st = iio_backend_get_priv(back); 404 + 405 + switch (private) { 406 + case AXI_DAC_FREQ_TONE_1: 407 + case AXI_DAC_FREQ_TONE_2: 408 + return axi_dac_frequency_get(st, chan, buf, 409 + private - AXI_DAC_FREQ_TONE_1); 410 + case AXI_DAC_SCALE_TONE_1: 411 + case AXI_DAC_SCALE_TONE_2: 412 + return axi_dac_scale_get(st, chan, buf, 413 + private - AXI_DAC_SCALE_TONE_1); 414 + case AXI_DAC_PHASE_TONE_1: 415 + case AXI_DAC_PHASE_TONE_2: 416 + return axi_dac_phase_get(st, chan, buf, 417 + private - AXI_DAC_PHASE_TONE_1); 418 + default: 419 + return -EOPNOTSUPP; 420 + } 421 + } 422 + 423 + static const struct iio_chan_spec_ext_info axi_dac_ext_info[] = { 424 + IIO_BACKEND_EX_INFO("frequency0", IIO_SEPARATE, AXI_DAC_FREQ_TONE_1), 425 + IIO_BACKEND_EX_INFO("frequency1", IIO_SEPARATE, AXI_DAC_FREQ_TONE_2), 426 + IIO_BACKEND_EX_INFO("scale0", IIO_SEPARATE, AXI_DAC_SCALE_TONE_1), 427 + IIO_BACKEND_EX_INFO("scale1", IIO_SEPARATE, AXI_DAC_SCALE_TONE_2), 428 + IIO_BACKEND_EX_INFO("phase0", IIO_SEPARATE, AXI_DAC_PHASE_TONE_1), 429 + IIO_BACKEND_EX_INFO("phase1", IIO_SEPARATE, AXI_DAC_PHASE_TONE_2), 430 + {} 431 + }; 432 + 433 + static int axi_dac_extend_chan(struct iio_backend *back, 434 + struct iio_chan_spec *chan) 435 + { 436 + struct axi_dac_state *st = iio_backend_get_priv(back); 437 + 438 + if (chan->type != IIO_ALTVOLTAGE) 439 + return -EINVAL; 440 + if (st->reg_config & AXI_DDS_DISABLE) 441 + /* nothing to extend */ 442 + return 0; 443 + 444 + chan->ext_info = axi_dac_ext_info; 445 + 446 + return 0; 447 + } 448 + 449 + static int axi_dac_data_source_set(struct iio_backend *back, unsigned int chan, 450 + enum iio_backend_data_source data) 451 + { 452 + struct axi_dac_state *st = iio_backend_get_priv(back); 453 + 454 + switch (data) { 455 + case IIO_BACKEND_INTERNAL_CONTINUOS_WAVE: 456 + return regmap_update_bits(st->regmap, 457 + AXI_DAC_REG_CHAN_CNTRL_7(chan), 458 + AXI_DAC_DATA_SEL, 459 + AXI_DAC_DATA_INTERNAL_TONE); 460 + case IIO_BACKEND_EXTERNAL: 461 + return regmap_update_bits(st->regmap, 462 + AXI_DAC_REG_CHAN_CNTRL_7(chan), 463 + AXI_DAC_DATA_SEL, AXI_DAC_DATA_DMA); 464 + default: 465 + return -EINVAL; 466 + } 467 + } 468 + 469 + static int axi_dac_set_sample_rate(struct iio_backend *back, unsigned int chan, 470 + u64 sample_rate) 471 + { 472 + struct axi_dac_state *st = iio_backend_get_priv(back); 473 + unsigned int freq; 474 + int ret, tone; 475 + 476 + if (!sample_rate) 477 + return -EINVAL; 478 + if (st->reg_config & AXI_DDS_DISABLE) 479 + /* sample_rate has no meaning if DDS is disabled */ 480 + return 0; 481 + 482 + guard(mutex)(&st->lock); 483 + /* 484 + * If dac_clk is 0 then this must be the first time we're being notified 485 + * about the interface sample rate. Hence, just update our internal 486 + * variable and bail... If it's not 0, then we get the current DDS 487 + * frequency (for the old rate) and update the registers for the new 488 + * sample rate. 489 + */ 490 + if (!st->dac_clk) { 491 + st->dac_clk = sample_rate; 492 + return 0; 493 + } 494 + 495 + for (tone = 0; tone <= AXI_DAC_FREQ_TONE_2; tone++) { 496 + ret = __axi_dac_frequency_get(st, chan, tone, &freq); 497 + if (ret) 498 + return ret; 499 + 500 + ret = __axi_dac_frequency_set(st, chan, sample_rate, tone, freq); 501 + if (ret) 502 + return ret; 503 + } 504 + 505 + st->dac_clk = sample_rate; 506 + 507 + return 0; 508 + } 509 + 510 + static const struct iio_backend_ops axi_dac_generic = { 511 + .enable = axi_dac_enable, 512 + .disable = axi_dac_disable, 513 + .request_buffer = axi_dac_request_buffer, 514 + .free_buffer = axi_dac_free_buffer, 515 + .extend_chan_spec = axi_dac_extend_chan, 516 + .ext_info_set = axi_dac_ext_info_set, 517 + .ext_info_get = axi_dac_ext_info_get, 518 + .data_source_set = axi_dac_data_source_set, 519 + .set_sample_rate = axi_dac_set_sample_rate, 520 + }; 521 + 522 + static const struct regmap_config axi_dac_regmap_config = { 523 + .val_bits = 32, 524 + .reg_bits = 32, 525 + .reg_stride = 4, 526 + .max_register = 0x0800, 527 + }; 528 + 529 + static int axi_dac_probe(struct platform_device *pdev) 530 + { 531 + const unsigned int *expected_ver; 532 + struct axi_dac_state *st; 533 + void __iomem *base; 534 + unsigned int ver; 535 + struct clk *clk; 536 + int ret; 537 + 538 + st = devm_kzalloc(&pdev->dev, sizeof(*st), GFP_KERNEL); 539 + if (!st) 540 + return -ENOMEM; 541 + 542 + expected_ver = device_get_match_data(&pdev->dev); 543 + if (!expected_ver) 544 + return -ENODEV; 545 + 546 + clk = devm_clk_get_enabled(&pdev->dev, NULL); 547 + if (IS_ERR(clk)) 548 + return PTR_ERR(clk); 549 + 550 + base = devm_platform_ioremap_resource(pdev, 0); 551 + if (IS_ERR(base)) 552 + return PTR_ERR(base); 553 + 554 + st->dev = &pdev->dev; 555 + st->regmap = devm_regmap_init_mmio(&pdev->dev, base, 556 + &axi_dac_regmap_config); 557 + if (IS_ERR(st->regmap)) 558 + return PTR_ERR(st->regmap); 559 + 560 + /* 561 + * Force disable the core. Up to the frontend to enable us. And we can 562 + * still read/write registers... 563 + */ 564 + ret = regmap_write(st->regmap, AXI_DAC_REG_RSTN, 0); 565 + if (ret) 566 + return ret; 567 + 568 + ret = regmap_read(st->regmap, ADI_AXI_REG_VERSION, &ver); 569 + if (ret) 570 + return ret; 571 + 572 + if (ADI_AXI_PCORE_VER_MAJOR(ver) != ADI_AXI_PCORE_VER_MAJOR(*expected_ver)) { 573 + dev_err(&pdev->dev, 574 + "Major version mismatch. Expected %d.%.2d.%c, Reported %d.%.2d.%c\n", 575 + ADI_AXI_PCORE_VER_MAJOR(*expected_ver), 576 + ADI_AXI_PCORE_VER_MINOR(*expected_ver), 577 + ADI_AXI_PCORE_VER_PATCH(*expected_ver), 578 + ADI_AXI_PCORE_VER_MAJOR(ver), 579 + ADI_AXI_PCORE_VER_MINOR(ver), 580 + ADI_AXI_PCORE_VER_PATCH(ver)); 581 + return -ENODEV; 582 + } 583 + 584 + /* Let's get the core read only configuration */ 585 + ret = regmap_read(st->regmap, AXI_DAC_REG_CONFIG, &st->reg_config); 586 + if (ret) 587 + return ret; 588 + 589 + /* 590 + * In some designs, setting the R1_MODE bit to 0 (which is the default 591 + * value) causes all channels of the frontend to be routed to the same 592 + * DMA (so they are sampled together). This is for things like 593 + * Multiple-Input and Multiple-Output (MIMO). As most of the times we 594 + * want independent channels let's override the core's default value and 595 + * set the R1_MODE bit. 596 + */ 597 + ret = regmap_set_bits(st->regmap, AXI_DAC_REG_CNTRL_2, ADI_DAC_R1_MODE); 598 + if (ret) 599 + return ret; 600 + 601 + mutex_init(&st->lock); 602 + ret = devm_iio_backend_register(&pdev->dev, &axi_dac_generic, st); 603 + if (ret) 604 + return ret; 605 + 606 + dev_info(&pdev->dev, "AXI DAC IP core (%d.%.2d.%c) probed\n", 607 + ADI_AXI_PCORE_VER_MAJOR(ver), 608 + ADI_AXI_PCORE_VER_MINOR(ver), 609 + ADI_AXI_PCORE_VER_PATCH(ver)); 610 + 611 + return 0; 612 + } 613 + 614 + static unsigned int axi_dac_9_1_b_info = ADI_AXI_PCORE_VER(9, 1, 'b'); 615 + 616 + static const struct of_device_id axi_dac_of_match[] = { 617 + { .compatible = "adi,axi-dac-9.1.b", .data = &axi_dac_9_1_b_info }, 618 + {} 619 + }; 620 + MODULE_DEVICE_TABLE(of, axi_dac_of_match); 621 + 622 + static struct platform_driver axi_dac_driver = { 623 + .driver = { 624 + .name = "adi-axi-dac", 625 + .of_match_table = axi_dac_of_match, 626 + }, 627 + .probe = axi_dac_probe, 628 + }; 629 + module_platform_driver(axi_dac_driver); 630 + 631 + MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>"); 632 + MODULE_DESCRIPTION("Analog Devices Generic AXI DAC IP core driver"); 633 + MODULE_LICENSE("GPL"); 634 + MODULE_IMPORT_NS(IIO_DMAENGINE_BUFFER); 635 + MODULE_IMPORT_NS(IIO_BACKEND);
+8 -20
drivers/iio/dac/ltc2688.c
··· 746 746 static int ltc2688_channel_config(struct ltc2688_state *st) 747 747 { 748 748 struct device *dev = &st->spi->dev; 749 - struct fwnode_handle *child; 750 749 u32 reg, clk_input, val, tmp[2]; 751 750 int ret, span; 752 751 753 - device_for_each_child_node(dev, child) { 752 + device_for_each_child_node_scoped(dev, child) { 754 753 struct ltc2688_chan *chan; 755 754 756 755 ret = fwnode_property_read_u32(child, "reg", &reg); 757 - if (ret) { 758 - fwnode_handle_put(child); 756 + if (ret) 759 757 return dev_err_probe(dev, ret, 760 758 "Failed to get reg property\n"); 761 - } 762 759 763 - if (reg >= LTC2688_DAC_CHANNELS) { 764 - fwnode_handle_put(child); 760 + if (reg >= LTC2688_DAC_CHANNELS) 765 761 return dev_err_probe(dev, -EINVAL, 766 762 "reg bigger than: %d\n", 767 763 LTC2688_DAC_CHANNELS); 768 - } 769 764 770 765 val = 0; 771 766 chan = &st->channels[reg]; ··· 781 786 if (!ret) { 782 787 span = ltc2688_span_lookup(st, (int)tmp[0] / 1000, 783 788 tmp[1] / 1000); 784 - if (span < 0) { 785 - fwnode_handle_put(child); 786 - return dev_err_probe(dev, -EINVAL, 789 + if (span < 0) 790 + return dev_err_probe(dev, span, 787 791 "output range not valid:[%d %d]\n", 788 792 tmp[0], tmp[1]); 789 - } 790 793 791 794 val |= FIELD_PREP(LTC2688_CH_SPAN_MSK, span); 792 795 } ··· 793 800 &clk_input); 794 801 if (!ret) { 795 802 if (clk_input >= LTC2688_CH_TGP_MAX) { 796 - fwnode_handle_put(child); 797 803 return dev_err_probe(dev, -EINVAL, 798 804 "toggle-dither-input inv value(%d)\n", 799 805 clk_input); 800 806 } 801 807 802 808 ret = ltc2688_tgp_clk_setup(st, chan, child, clk_input); 803 - if (ret) { 804 - fwnode_handle_put(child); 809 + if (ret) 805 810 return ret; 806 - } 807 811 808 812 /* 809 813 * 0 means software toggle which is the default mode. ··· 834 844 835 845 ret = regmap_write(st->regmap, LTC2688_CMD_CH_SETTING(reg), 836 846 val); 837 - if (ret) { 838 - fwnode_handle_put(child); 839 - return dev_err_probe(dev, -EINVAL, 847 + if (ret) 848 + return dev_err_probe(dev, ret, 840 849 "failed to set chan settings\n"); 841 - } 842 850 } 843 851 844 852 return 0;
+3
drivers/iio/dac/ti-dac5571.c
··· 13 13 * https://www.ti.com/lit/ds/symlink/dac5573.pdf 14 14 * https://www.ti.com/lit/ds/symlink/dac6573.pdf 15 15 * https://www.ti.com/lit/ds/symlink/dac7573.pdf 16 + * https://www.ti.com/lit/ds/symlink/dac081c081.pdf 16 17 * https://www.ti.com/lit/ds/symlink/dac121c081.pdf 17 18 */ 18 19 ··· 387 386 } 388 387 389 388 static const struct of_device_id dac5571_of_id[] = { 389 + {.compatible = "ti,dac081c081", .data = &dac5571_spec[single_8bit] }, 390 390 {.compatible = "ti,dac121c081", .data = &dac5571_spec[single_12bit] }, 391 391 {.compatible = "ti,dac5571", .data = &dac5571_spec[single_8bit] }, 392 392 {.compatible = "ti,dac6571", .data = &dac5571_spec[single_10bit] }, ··· 403 401 MODULE_DEVICE_TABLE(of, dac5571_of_id); 404 402 405 403 static const struct i2c_device_id dac5571_id[] = { 404 + {"dac081c081", (kernel_ulong_t)&dac5571_spec[single_8bit] }, 406 405 {"dac121c081", (kernel_ulong_t)&dac5571_spec[single_12bit] }, 407 406 {"dac5571", (kernel_ulong_t)&dac5571_spec[single_8bit] }, 408 407 {"dac6571", (kernel_ulong_t)&dac5571_spec[single_10bit] },
+6 -18
drivers/iio/frequency/admfm2000.c
··· 160 160 { 161 161 struct platform_device *pdev = to_platform_device(indio_dev->dev.parent); 162 162 struct device *dev = &pdev->dev; 163 - struct fwnode_handle *child; 164 163 struct gpio_desc **dsa; 165 164 struct gpio_desc **sw; 166 165 int ret, i; 167 166 bool mode; 168 167 u32 reg; 169 168 170 - device_for_each_child_node(dev, child) { 169 + device_for_each_child_node_scoped(dev, child) { 171 170 ret = fwnode_property_read_u32(child, "reg", &reg); 172 - if (ret) { 173 - fwnode_handle_put(child); 171 + if (ret) 174 172 return dev_err_probe(dev, ret, 175 173 "Failed to get reg property\n"); 176 - } 177 174 178 - if (reg >= indio_dev->num_channels) { 179 - fwnode_handle_put(child); 175 + if (reg >= indio_dev->num_channels) 180 176 return dev_err_probe(dev, -EINVAL, "reg bigger than: %d\n", 181 177 indio_dev->num_channels); 182 - } 183 178 184 179 if (fwnode_property_present(child, "adi,mixer-mode")) 185 180 mode = ADMFM2000_MIXER_MODE; ··· 191 196 dsa = st->dsa2_gpios; 192 197 break; 193 198 default: 194 - fwnode_handle_put(child); 195 199 return -EINVAL; 196 200 } 197 201 198 202 for (i = 0; i < ADMFM2000_MODE_GPIOS; i++) { 199 203 sw[i] = devm_fwnode_gpiod_get_index(dev, child, "switch", 200 204 i, GPIOD_OUT_LOW, NULL); 201 - if (IS_ERR(sw[i])) { 202 - fwnode_handle_put(child); 205 + if (IS_ERR(sw[i])) 203 206 return dev_err_probe(dev, PTR_ERR(sw[i]), 204 207 "Failed to get gpios\n"); 205 - } 206 208 } 207 209 208 210 for (i = 0; i < ADMFM2000_DSA_GPIOS; i++) { 209 211 dsa[i] = devm_fwnode_gpiod_get_index(dev, child, 210 212 "attenuation", i, 211 213 GPIOD_OUT_LOW, NULL); 212 - if (IS_ERR(dsa[i])) { 213 - fwnode_handle_put(child); 214 + if (IS_ERR(dsa[i])) 214 215 return dev_err_probe(dev, PTR_ERR(dsa[i]), 215 216 "Failed to get gpios\n"); 216 - } 217 217 } 218 218 219 219 ret = admfm2000_mode(indio_dev, reg, mode); 220 - if (ret) { 221 - fwnode_handle_put(child); 220 + if (ret) 222 221 return ret; 223 - } 224 222 } 225 223 226 224 return 0;
+2
drivers/iio/health/max30102.c
··· 613 613 } 614 614 615 615 static const struct i2c_device_id max30102_id[] = { 616 + { "max30101", max30105 }, 616 617 { "max30102", max30102 }, 617 618 { "max30105", max30105 }, 618 619 {} ··· 621 620 MODULE_DEVICE_TABLE(i2c, max30102_id); 622 621 623 622 static const struct of_device_id max30102_dt_ids[] = { 623 + { .compatible = "maxim,max30101" }, 624 624 { .compatible = "maxim,max30102" }, 625 625 { .compatible = "maxim,max30105" }, 626 626 { }
+92 -19
drivers/iio/humidity/hdc3020.c
··· 15 15 #include <linux/cleanup.h> 16 16 #include <linux/crc8.h> 17 17 #include <linux/delay.h> 18 + #include <linux/gpio/consumer.h> 18 19 #include <linux/i2c.h> 19 20 #include <linux/init.h> 20 21 #include <linux/interrupt.h> 21 22 #include <linux/module.h> 22 23 #include <linux/mutex.h> 24 + #include <linux/pm.h> 25 + #include <linux/regulator/consumer.h> 23 26 #include <linux/units.h> 24 27 25 28 #include <asm/unaligned.h> ··· 71 68 72 69 struct hdc3020_data { 73 70 struct i2c_client *client; 71 + struct gpio_desc *reset_gpio; 72 + struct regulator *vdd_supply; 74 73 /* 75 74 * Ensure that the sensor configuration (currently only heater is 76 75 * supported) will not be changed during the process of reading ··· 556 551 .write_event_value = hdc3020_write_thresh, 557 552 }; 558 553 559 - static void hdc3020_stop(void *data) 554 + static int hdc3020_power_off(struct hdc3020_data *data) 560 555 { 561 - hdc3020_exec_cmd((struct hdc3020_data *)data, HDC3020_EXIT_AUTO); 556 + hdc3020_exec_cmd(data, HDC3020_EXIT_AUTO); 557 + 558 + if (data->reset_gpio) 559 + gpiod_set_value_cansleep(data->reset_gpio, 1); 560 + 561 + return regulator_disable(data->vdd_supply); 562 + } 563 + 564 + static int hdc3020_power_on(struct hdc3020_data *data) 565 + { 566 + int ret; 567 + 568 + ret = regulator_enable(data->vdd_supply); 569 + if (ret) 570 + return ret; 571 + 572 + fsleep(5000); 573 + 574 + if (data->reset_gpio) { 575 + gpiod_set_value_cansleep(data->reset_gpio, 0); 576 + fsleep(3000); 577 + } 578 + 579 + if (data->client->irq) { 580 + /* 581 + * The alert output is activated by default upon power up, 582 + * hardware reset, and soft reset. Clear the status register. 583 + */ 584 + ret = hdc3020_exec_cmd(data, HDC3020_S_STATUS); 585 + if (ret) { 586 + hdc3020_power_off(data); 587 + return ret; 588 + } 589 + } 590 + 591 + ret = hdc3020_exec_cmd(data, HDC3020_S_AUTO_10HZ_MOD0); 592 + if (ret) 593 + hdc3020_power_off(data); 594 + 595 + return ret; 596 + } 597 + 598 + static void hdc3020_exit(void *data) 599 + { 600 + hdc3020_power_off(data); 562 601 } 563 602 564 603 static int hdc3020_probe(struct i2c_client *client) ··· 618 569 if (!indio_dev) 619 570 return -ENOMEM; 620 571 572 + dev_set_drvdata(&client->dev, indio_dev); 573 + 621 574 data = iio_priv(indio_dev); 622 575 data->client = client; 623 576 mutex_init(&data->lock); ··· 631 580 indio_dev->info = &hdc3020_info; 632 581 indio_dev->channels = hdc3020_channels; 633 582 indio_dev->num_channels = ARRAY_SIZE(hdc3020_channels); 583 + 584 + data->vdd_supply = devm_regulator_get(&client->dev, "vdd"); 585 + if (IS_ERR(data->vdd_supply)) 586 + return dev_err_probe(&client->dev, PTR_ERR(data->vdd_supply), 587 + "Unable to get VDD regulator\n"); 588 + 589 + data->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", 590 + GPIOD_OUT_HIGH); 591 + if (IS_ERR(data->reset_gpio)) 592 + return dev_err_probe(&client->dev, PTR_ERR(data->reset_gpio), 593 + "Cannot get reset GPIO\n"); 594 + 595 + ret = hdc3020_power_on(data); 596 + if (ret) 597 + return dev_err_probe(&client->dev, ret, "Power on failed\n"); 598 + 599 + ret = devm_add_action_or_reset(&data->client->dev, hdc3020_exit, data); 600 + if (ret) 601 + return ret; 602 + 634 603 if (client->irq) { 635 604 ret = devm_request_threaded_irq(&client->dev, client->irq, 636 605 NULL, hdc3020_interrupt_handler, ··· 659 588 if (ret) 660 589 return dev_err_probe(&client->dev, ret, 661 590 "Failed to request IRQ\n"); 662 - 663 - /* 664 - * The alert output is activated by default upon power up, 665 - * hardware reset, and soft reset. Clear the status register. 666 - */ 667 - ret = hdc3020_exec_cmd(data, HDC3020_S_STATUS); 668 - if (ret) 669 - return ret; 670 591 } 671 - 672 - ret = hdc3020_exec_cmd(data, HDC3020_S_AUTO_10HZ_MOD0); 673 - if (ret) 674 - return dev_err_probe(&client->dev, ret, 675 - "Unable to set up measurement\n"); 676 - 677 - ret = devm_add_action_or_reset(&data->client->dev, hdc3020_stop, data); 678 - if (ret) 679 - return ret; 680 592 681 593 ret = devm_iio_device_register(&data->client->dev, indio_dev); 682 594 if (ret) ··· 667 613 668 614 return 0; 669 615 } 616 + 617 + static int hdc3020_suspend(struct device *dev) 618 + { 619 + struct iio_dev *iio_dev = dev_get_drvdata(dev); 620 + struct hdc3020_data *data = iio_priv(iio_dev); 621 + 622 + return hdc3020_power_off(data); 623 + } 624 + 625 + static int hdc3020_resume(struct device *dev) 626 + { 627 + struct iio_dev *iio_dev = dev_get_drvdata(dev); 628 + struct hdc3020_data *data = iio_priv(iio_dev); 629 + 630 + return hdc3020_power_on(data); 631 + } 632 + 633 + static DEFINE_SIMPLE_DEV_PM_OPS(hdc3020_pm_ops, hdc3020_suspend, hdc3020_resume); 670 634 671 635 static const struct i2c_device_id hdc3020_id[] = { 672 636 { "hdc3020" }, ··· 705 633 static struct i2c_driver hdc3020_driver = { 706 634 .driver = { 707 635 .name = "hdc3020", 636 + .pm = pm_sleep_ptr(&hdc3020_pm_ops), 708 637 .of_match_table = hdc3020_dt_ids, 709 638 }, 710 639 .probe = hdc3020_probe,
+1 -1
drivers/iio/humidity/hts221_core.c
··· 573 573 if (!iio_dev) 574 574 return -ENOMEM; 575 575 576 - dev_set_drvdata(dev, (void *)iio_dev); 576 + dev_set_drvdata(dev, iio_dev); 577 577 578 578 hw = iio_priv(iio_dev); 579 579 hw->name = name;
+2
drivers/iio/imu/inv_icm42600/inv_icm42600.h
··· 22 22 INV_CHIP_ICM42602, 23 23 INV_CHIP_ICM42605, 24 24 INV_CHIP_ICM42622, 25 + INV_CHIP_ICM42688, 25 26 INV_CHIP_ICM42631, 26 27 INV_CHIP_NB, 27 28 }; ··· 305 304 #define INV_ICM42600_WHOAMI_ICM42602 0x41 306 305 #define INV_ICM42600_WHOAMI_ICM42605 0x42 307 306 #define INV_ICM42600_WHOAMI_ICM42622 0x46 307 + #define INV_ICM42600_WHOAMI_ICM42688 0x47 308 308 #define INV_ICM42600_WHOAMI_ICM42631 0x5C 309 309 310 310 /* User bank 1 (MSB 0x10) */
+5
drivers/iio/imu/inv_icm42600/inv_icm42600_core.c
··· 87 87 .name = "icm42622", 88 88 .conf = &inv_icm42600_default_conf, 89 89 }, 90 + [INV_CHIP_ICM42688] = { 91 + .whoami = INV_ICM42600_WHOAMI_ICM42688, 92 + .name = "icm42688", 93 + .conf = &inv_icm42600_default_conf, 94 + }, 90 95 [INV_CHIP_ICM42631] = { 91 96 .whoami = INV_ICM42600_WHOAMI_ICM42631, 92 97 .name = "icm42631",
+3
drivers/iio/imu/inv_icm42600/inv_icm42600_i2c.c
··· 85 85 .compatible = "invensense,icm42622", 86 86 .data = (void *)INV_CHIP_ICM42622, 87 87 }, { 88 + .compatible = "invensense,icm42688", 89 + .data = (void *)INV_CHIP_ICM42688, 90 + }, { 88 91 .compatible = "invensense,icm42631", 89 92 .data = (void *)INV_CHIP_ICM42631, 90 93 },
+3
drivers/iio/imu/inv_icm42600/inv_icm42600_spi.c
··· 81 81 .compatible = "invensense,icm42622", 82 82 .data = (void *)INV_CHIP_ICM42622, 83 83 }, { 84 + .compatible = "invensense,icm42688", 85 + .data = (void *)INV_CHIP_ICM42688, 86 + }, { 84 87 .compatible = "invensense,icm42631", 85 88 .data = (void *)INV_CHIP_ICM42631, 86 89 },
+486 -56
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
··· 15 15 #include <linux/acpi.h> 16 16 #include <linux/platform_device.h> 17 17 #include <linux/regulator/consumer.h> 18 + #include <linux/math64.h> 19 + #include <linux/minmax.h> 18 20 #include <linux/pm.h> 19 21 #include <linux/pm_runtime.h> 20 22 #include <linux/property.h> ··· 289 287 }; 290 288 291 289 static int inv_mpu6050_pwr_mgmt_1_write(struct inv_mpu6050_state *st, bool sleep, 292 - int clock, int temp_dis) 290 + bool cycle, int clock, int temp_dis) 293 291 { 294 292 u8 val; 295 293 ··· 303 301 val |= INV_MPU6050_BIT_TEMP_DIS; 304 302 if (sleep) 305 303 val |= INV_MPU6050_BIT_SLEEP; 304 + if (cycle) 305 + val |= INV_MPU6050_BIT_CYCLE; 306 306 307 307 dev_dbg(regmap_get_device(st->map), "pwr_mgmt_1: 0x%x\n", val); 308 308 return regmap_write(st->map, st->reg->pwr_mgmt_1, val); ··· 320 316 case INV_MPU6000: 321 317 case INV_MPU9150: 322 318 /* old chips: switch clock manually */ 323 - ret = inv_mpu6050_pwr_mgmt_1_write(st, false, clock, -1); 319 + ret = inv_mpu6050_pwr_mgmt_1_write(st, false, false, clock, -1); 324 320 if (ret) 325 321 return ret; 326 322 st->chip_config.clk = clock; ··· 336 332 int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, 337 333 unsigned int mask) 338 334 { 339 - unsigned int sleep; 335 + unsigned int sleep, val; 340 336 u8 pwr_mgmt2, user_ctrl; 341 337 int ret; 342 338 ··· 349 345 mask &= ~INV_MPU6050_SENSOR_TEMP; 350 346 if (mask & INV_MPU6050_SENSOR_MAGN && en == st->chip_config.magn_en) 351 347 mask &= ~INV_MPU6050_SENSOR_MAGN; 348 + if (mask & INV_MPU6050_SENSOR_WOM && en == st->chip_config.wom_en) 349 + mask &= ~INV_MPU6050_SENSOR_WOM; 350 + 351 + /* force accel on if WoM is on and not going off */ 352 + if (!en && (mask & INV_MPU6050_SENSOR_ACCL) && st->chip_config.wom_en && 353 + !(mask & INV_MPU6050_SENSOR_WOM)) 354 + mask &= ~INV_MPU6050_SENSOR_ACCL; 355 + 352 356 if (mask == 0) 353 357 return 0; 354 358 355 359 /* turn on/off temperature sensor */ 356 360 if (mask & INV_MPU6050_SENSOR_TEMP) { 357 - ret = inv_mpu6050_pwr_mgmt_1_write(st, false, -1, !en); 361 + ret = inv_mpu6050_pwr_mgmt_1_write(st, false, false, -1, !en); 358 362 if (ret) 359 363 return ret; 360 364 st->chip_config.temp_en = en; ··· 451 439 } 452 440 } 453 441 442 + /* enable/disable accel intelligence control */ 443 + if (mask & INV_MPU6050_SENSOR_WOM) { 444 + val = en ? INV_MPU6500_BIT_ACCEL_INTEL_EN | 445 + INV_MPU6500_BIT_ACCEL_INTEL_MODE : 0; 446 + ret = regmap_write(st->map, INV_MPU6500_REG_ACCEL_INTEL_CTRL, val); 447 + if (ret) 448 + return ret; 449 + st->chip_config.wom_en = en; 450 + } 451 + 454 452 return 0; 455 453 } 456 454 ··· 469 447 { 470 448 int result; 471 449 472 - result = inv_mpu6050_pwr_mgmt_1_write(st, !power_on, -1, -1); 450 + result = inv_mpu6050_pwr_mgmt_1_write(st, !power_on, false, -1, -1); 473 451 if (result) 474 452 return result; 475 453 ··· 499 477 return regmap_write(st->map, st->reg->gyro_config, data); 500 478 } 501 479 502 - /* 503 - * inv_mpu6050_set_lpf_regs() - set low pass filter registers, chip dependent 504 - * 505 - * MPU60xx/MPU9150 use only 1 register for accelerometer + gyroscope 506 - * MPU6500 and above have a dedicated register for accelerometer 507 - */ 508 - static int inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state *st, 509 - enum inv_mpu6050_filter_e val) 480 + static int inv_mpu6050_set_accel_lpf_regs(struct inv_mpu6050_state *st, 481 + enum inv_mpu6050_filter_e val) 510 482 { 511 - int result; 512 - 513 - result = regmap_write(st->map, st->reg->lpf, val); 514 - if (result) 515 - return result; 516 - 517 - /* set accel lpf */ 518 483 switch (st->chip_type) { 519 484 case INV_MPU6050: 520 485 case INV_MPU6000: ··· 518 509 } 519 510 520 511 return regmap_write(st->map, st->reg->accel_lpf, val); 512 + } 513 + 514 + /* 515 + * inv_mpu6050_set_lpf_regs() - set low pass filter registers, chip dependent 516 + * 517 + * MPU60xx/MPU9150 use only 1 register for accelerometer + gyroscope 518 + * MPU6500 and above have a dedicated register for accelerometer 519 + */ 520 + static int inv_mpu6050_set_lpf_regs(struct inv_mpu6050_state *st, 521 + enum inv_mpu6050_filter_e val) 522 + { 523 + int result; 524 + 525 + result = regmap_write(st->map, st->reg->lpf, val); 526 + if (result) 527 + return result; 528 + 529 + /* set accel lpf */ 530 + return inv_mpu6050_set_accel_lpf_regs(st, val); 521 531 } 522 532 523 533 /* ··· 921 893 return result; 922 894 } 923 895 896 + static u64 inv_mpu6050_convert_wom_to_roc(unsigned int threshold, unsigned int freq_div) 897 + { 898 + /* 4mg per LSB converted in m/s² in micro (1000000) */ 899 + const unsigned int convert = 4U * 9807U; 900 + u64 value; 901 + 902 + value = threshold * convert; 903 + 904 + /* compute the differential by multiplying by the frequency */ 905 + return div_u64(value * INV_MPU6050_INTERNAL_FREQ_HZ, freq_div); 906 + } 907 + 908 + static unsigned int inv_mpu6050_convert_roc_to_wom(u64 roc, unsigned int freq_div) 909 + { 910 + /* 4mg per LSB converted in m/s² in micro (1000000) */ 911 + const unsigned int convert = 4U * 9807U; 912 + u64 value; 913 + 914 + /* return 0 only if roc is 0 */ 915 + if (roc == 0) 916 + return 0; 917 + 918 + value = div_u64(roc * freq_div, convert * INV_MPU6050_INTERNAL_FREQ_HZ); 919 + 920 + /* limit value to 8 bits and prevent 0 */ 921 + return min(255, max(1, value)); 922 + } 923 + 924 + static int inv_mpu6050_set_wom_int(struct inv_mpu6050_state *st, bool on) 925 + { 926 + unsigned int reg_val, val; 927 + 928 + switch (st->chip_type) { 929 + case INV_MPU6050: 930 + case INV_MPU6500: 931 + case INV_MPU6515: 932 + case INV_MPU6880: 933 + case INV_MPU6000: 934 + case INV_MPU9150: 935 + case INV_MPU9250: 936 + case INV_MPU9255: 937 + reg_val = INV_MPU6500_BIT_WOM_INT_EN; 938 + break; 939 + default: 940 + reg_val = INV_ICM20608_BIT_WOM_INT_EN; 941 + break; 942 + } 943 + 944 + val = on ? reg_val : 0; 945 + 946 + return regmap_update_bits(st->map, st->reg->int_enable, reg_val, val); 947 + } 948 + 949 + static int inv_mpu6050_set_wom_threshold(struct inv_mpu6050_state *st, u64 value, 950 + unsigned int freq_div) 951 + { 952 + unsigned int threshold; 953 + int result; 954 + 955 + /* convert roc to wom threshold and convert back to handle clipping */ 956 + threshold = inv_mpu6050_convert_roc_to_wom(value, freq_div); 957 + value = inv_mpu6050_convert_wom_to_roc(threshold, freq_div); 958 + 959 + dev_dbg(regmap_get_device(st->map), "wom_threshold: 0x%x\n", threshold); 960 + 961 + switch (st->chip_type) { 962 + case INV_ICM20609: 963 + case INV_ICM20689: 964 + case INV_ICM20600: 965 + case INV_ICM20602: 966 + case INV_ICM20690: 967 + st->data[0] = threshold; 968 + st->data[1] = threshold; 969 + st->data[2] = threshold; 970 + result = regmap_bulk_write(st->map, INV_ICM20609_REG_ACCEL_WOM_X_THR, 971 + st->data, 3); 972 + break; 973 + default: 974 + result = regmap_write(st->map, INV_MPU6500_REG_WOM_THRESHOLD, threshold); 975 + break; 976 + } 977 + if (result) 978 + return result; 979 + 980 + st->chip_config.roc_threshold = value; 981 + 982 + return 0; 983 + } 984 + 985 + static int inv_mpu6050_set_lp_odr(struct inv_mpu6050_state *st, unsigned int freq_div, 986 + unsigned int *lp_div) 987 + { 988 + static const unsigned int freq_dividers[] = {2, 4, 8, 16, 32, 64, 128, 256}; 989 + static const unsigned int reg_values[] = { 990 + INV_MPU6050_LPOSC_500HZ, INV_MPU6050_LPOSC_250HZ, 991 + INV_MPU6050_LPOSC_125HZ, INV_MPU6050_LPOSC_62HZ, 992 + INV_MPU6050_LPOSC_31HZ, INV_MPU6050_LPOSC_16HZ, 993 + INV_MPU6050_LPOSC_8HZ, INV_MPU6050_LPOSC_4HZ, 994 + }; 995 + unsigned int val, i; 996 + 997 + switch (st->chip_type) { 998 + case INV_ICM20609: 999 + case INV_ICM20689: 1000 + case INV_ICM20600: 1001 + case INV_ICM20602: 1002 + case INV_ICM20690: 1003 + /* nothing to do */ 1004 + *lp_div = INV_MPU6050_FREQ_DIVIDER(st); 1005 + return 0; 1006 + default: 1007 + break; 1008 + } 1009 + 1010 + /* found the nearest superior frequency divider */ 1011 + i = ARRAY_SIZE(reg_values) - 1; 1012 + val = reg_values[i]; 1013 + *lp_div = freq_dividers[i]; 1014 + for (i = 0; i < ARRAY_SIZE(freq_dividers); ++i) { 1015 + if (freq_div <= freq_dividers[i]) { 1016 + val = reg_values[i]; 1017 + *lp_div = freq_dividers[i]; 1018 + break; 1019 + } 1020 + } 1021 + 1022 + dev_dbg(regmap_get_device(st->map), "lp_odr: 0x%x\n", val); 1023 + return regmap_write(st->map, INV_MPU6500_REG_LP_ODR, val); 1024 + } 1025 + 1026 + static int inv_mpu6050_set_wom_lp(struct inv_mpu6050_state *st, bool on) 1027 + { 1028 + unsigned int lp_div; 1029 + int result; 1030 + 1031 + if (on) { 1032 + /* set low power ODR */ 1033 + result = inv_mpu6050_set_lp_odr(st, INV_MPU6050_FREQ_DIVIDER(st), &lp_div); 1034 + if (result) 1035 + return result; 1036 + /* disable accel low pass filter */ 1037 + result = inv_mpu6050_set_accel_lpf_regs(st, INV_MPU6050_FILTER_NOLPF); 1038 + if (result) 1039 + return result; 1040 + /* update wom threshold with new low-power frequency divider */ 1041 + result = inv_mpu6050_set_wom_threshold(st, st->chip_config.roc_threshold, lp_div); 1042 + if (result) 1043 + return result; 1044 + /* set cycle mode */ 1045 + result = inv_mpu6050_pwr_mgmt_1_write(st, false, true, -1, -1); 1046 + } else { 1047 + /* disable cycle mode */ 1048 + result = inv_mpu6050_pwr_mgmt_1_write(st, false, false, -1, -1); 1049 + if (result) 1050 + return result; 1051 + /* restore wom threshold */ 1052 + result = inv_mpu6050_set_wom_threshold(st, st->chip_config.roc_threshold, 1053 + INV_MPU6050_FREQ_DIVIDER(st)); 1054 + if (result) 1055 + return result; 1056 + /* restore accel low pass filter */ 1057 + result = inv_mpu6050_set_accel_lpf_regs(st, st->chip_config.lpf); 1058 + } 1059 + 1060 + return result; 1061 + } 1062 + 1063 + static int inv_mpu6050_enable_wom(struct inv_mpu6050_state *st, bool en) 1064 + { 1065 + struct device *pdev = regmap_get_device(st->map); 1066 + unsigned int mask; 1067 + int result; 1068 + 1069 + if (en) { 1070 + result = pm_runtime_resume_and_get(pdev); 1071 + if (result) 1072 + return result; 1073 + 1074 + mask = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_WOM; 1075 + result = inv_mpu6050_switch_engine(st, true, mask); 1076 + if (result) 1077 + goto error_suspend; 1078 + 1079 + result = inv_mpu6050_set_wom_int(st, true); 1080 + if (result) 1081 + goto error_suspend; 1082 + } else { 1083 + result = inv_mpu6050_set_wom_int(st, false); 1084 + if (result) 1085 + dev_err(pdev, "error %d disabling WoM interrupt bit", result); 1086 + 1087 + /* disable only WoM and let accel be disabled by autosuspend */ 1088 + result = inv_mpu6050_switch_engine(st, false, INV_MPU6050_SENSOR_WOM); 1089 + if (result) { 1090 + dev_err(pdev, "error %d disabling WoM force off", result); 1091 + /* force WoM off */ 1092 + st->chip_config.wom_en = false; 1093 + } 1094 + 1095 + pm_runtime_mark_last_busy(pdev); 1096 + pm_runtime_put_autosuspend(pdev); 1097 + } 1098 + 1099 + return result; 1100 + 1101 + error_suspend: 1102 + pm_runtime_mark_last_busy(pdev); 1103 + pm_runtime_put_autosuspend(pdev); 1104 + return result; 1105 + } 1106 + 1107 + static int inv_mpu6050_read_event_config(struct iio_dev *indio_dev, 1108 + const struct iio_chan_spec *chan, 1109 + enum iio_event_type type, 1110 + enum iio_event_direction dir) 1111 + { 1112 + struct inv_mpu6050_state *st = iio_priv(indio_dev); 1113 + 1114 + /* support only WoM (accel roc rising) event */ 1115 + if (chan->type != IIO_ACCEL || type != IIO_EV_TYPE_ROC || 1116 + dir != IIO_EV_DIR_RISING) 1117 + return -EINVAL; 1118 + 1119 + guard(mutex)(&st->lock); 1120 + 1121 + return st->chip_config.wom_en ? 1 : 0; 1122 + } 1123 + 1124 + static int inv_mpu6050_write_event_config(struct iio_dev *indio_dev, 1125 + const struct iio_chan_spec *chan, 1126 + enum iio_event_type type, 1127 + enum iio_event_direction dir, 1128 + int state) 1129 + { 1130 + struct inv_mpu6050_state *st = iio_priv(indio_dev); 1131 + int enable; 1132 + 1133 + /* support only WoM (accel roc rising) event */ 1134 + if (chan->type != IIO_ACCEL || type != IIO_EV_TYPE_ROC || 1135 + dir != IIO_EV_DIR_RISING) 1136 + return -EINVAL; 1137 + 1138 + enable = !!state; 1139 + 1140 + guard(mutex)(&st->lock); 1141 + 1142 + if (st->chip_config.wom_en == enable) 1143 + return 0; 1144 + 1145 + return inv_mpu6050_enable_wom(st, enable); 1146 + } 1147 + 1148 + static int inv_mpu6050_read_event_value(struct iio_dev *indio_dev, 1149 + const struct iio_chan_spec *chan, 1150 + enum iio_event_type type, 1151 + enum iio_event_direction dir, 1152 + enum iio_event_info info, 1153 + int *val, int *val2) 1154 + { 1155 + struct inv_mpu6050_state *st = iio_priv(indio_dev); 1156 + u32 rem; 1157 + 1158 + /* support only WoM (accel roc rising) event value */ 1159 + if (chan->type != IIO_ACCEL || type != IIO_EV_TYPE_ROC || 1160 + dir != IIO_EV_DIR_RISING || info != IIO_EV_INFO_VALUE) 1161 + return -EINVAL; 1162 + 1163 + guard(mutex)(&st->lock); 1164 + 1165 + /* return value in micro */ 1166 + *val = div_u64_rem(st->chip_config.roc_threshold, 1000000U, &rem); 1167 + *val2 = rem; 1168 + 1169 + return IIO_VAL_INT_PLUS_MICRO; 1170 + } 1171 + 1172 + static int inv_mpu6050_write_event_value(struct iio_dev *indio_dev, 1173 + const struct iio_chan_spec *chan, 1174 + enum iio_event_type type, 1175 + enum iio_event_direction dir, 1176 + enum iio_event_info info, 1177 + int val, int val2) 1178 + { 1179 + struct inv_mpu6050_state *st = iio_priv(indio_dev); 1180 + struct device *pdev = regmap_get_device(st->map); 1181 + u64 value; 1182 + int result; 1183 + 1184 + /* support only WoM (accel roc rising) event value */ 1185 + if (chan->type != IIO_ACCEL || type != IIO_EV_TYPE_ROC || 1186 + dir != IIO_EV_DIR_RISING || info != IIO_EV_INFO_VALUE) 1187 + return -EINVAL; 1188 + 1189 + if (val < 0 || val2 < 0) 1190 + return -EINVAL; 1191 + 1192 + guard(mutex)(&st->lock); 1193 + 1194 + result = pm_runtime_resume_and_get(pdev); 1195 + if (result) 1196 + return result; 1197 + 1198 + value = (u64)val * 1000000ULL + (u64)val2; 1199 + result = inv_mpu6050_set_wom_threshold(st, value, INV_MPU6050_FREQ_DIVIDER(st)); 1200 + 1201 + pm_runtime_mark_last_busy(pdev); 1202 + pm_runtime_put_autosuspend(pdev); 1203 + 1204 + return result; 1205 + } 1206 + 924 1207 /* 925 1208 * inv_mpu6050_set_lpf() - set low pass filer based on fifo rate. 926 1209 * ··· 1325 986 1326 987 /* update rate for magn, noop if not present in chip */ 1327 988 result = inv_mpu_magn_set_rate(st, fifo_rate); 989 + if (result) 990 + goto fifo_rate_fail_power_off; 991 + 992 + /* update wom threshold since roc is dependent on sampling frequency */ 993 + result = inv_mpu6050_set_wom_threshold(st, st->chip_config.roc_threshold, 994 + INV_MPU6050_FREQ_DIVIDER(st)); 1328 995 if (result) 1329 996 goto fifo_rate_fail_power_off; 1330 997 ··· 1434 1089 { } 1435 1090 }; 1436 1091 1092 + static const struct iio_event_spec inv_wom_events[] = { 1093 + { 1094 + .type = IIO_EV_TYPE_ROC, 1095 + .dir = IIO_EV_DIR_RISING, 1096 + .mask_separate = BIT(IIO_EV_INFO_ENABLE) | 1097 + BIT(IIO_EV_INFO_VALUE), 1098 + }, 1099 + }; 1100 + 1437 1101 #define INV_MPU6050_CHAN(_type, _channel2, _index) \ 1438 1102 { \ 1439 1103 .type = _type, \ ··· 1478 1124 }, \ 1479 1125 } 1480 1126 1481 - static const struct iio_chan_spec inv_mpu_channels[] = { 1127 + #define INV_MPU6050_EVENT_CHAN(_type, _channel2, _events, _events_nb) \ 1128 + { \ 1129 + .type = _type, \ 1130 + .modified = 1, \ 1131 + .channel2 = _channel2, \ 1132 + .event_spec = _events, \ 1133 + .num_event_specs = _events_nb, \ 1134 + .scan_index = -1, \ 1135 + } 1136 + 1137 + static const struct iio_chan_spec inv_mpu6050_channels[] = { 1482 1138 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP), 1483 1139 1484 1140 INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP), ··· 1500 1136 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X), 1501 1137 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y), 1502 1138 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z), 1139 + }; 1140 + 1141 + static const struct iio_chan_spec inv_mpu6500_channels[] = { 1142 + IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP), 1143 + 1144 + INV_MPU6050_TEMP_CHAN(INV_MPU6050_SCAN_TEMP), 1145 + 1146 + INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X), 1147 + INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y), 1148 + INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z), 1149 + 1150 + INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X), 1151 + INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y), 1152 + INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z), 1153 + 1154 + INV_MPU6050_EVENT_CHAN(IIO_ACCEL, IIO_MOD_X_OR_Y_OR_Z, 1155 + inv_wom_events, ARRAY_SIZE(inv_wom_events)), 1503 1156 }; 1504 1157 1505 1158 #define INV_MPU6050_SCAN_MASK_3AXIS_ACCEL \ ··· 1707 1326 .write_raw = &inv_mpu6050_write_raw, 1708 1327 .write_raw_get_fmt = &inv_write_raw_get_fmt, 1709 1328 .attrs = &inv_attribute_group, 1329 + .read_event_config = inv_mpu6050_read_event_config, 1330 + .write_event_config = inv_mpu6050_write_event_config, 1331 + .read_event_value = inv_mpu6050_read_event_value, 1332 + .write_event_value = inv_mpu6050_write_event_value, 1710 1333 .validate_trigger = inv_mpu6050_validate_trigger, 1711 1334 .debugfs_reg_access = &inv_mpu6050_reg_access, 1712 1335 }; ··· 1922 1537 irq_type); 1923 1538 return -EINVAL; 1924 1539 } 1540 + device_set_wakeup_capable(dev, true); 1925 1541 1926 1542 st->vdd_supply = devm_regulator_get(dev, "vdd"); 1927 1543 if (IS_ERR(st->vdd_supply)) ··· 1999 1613 return result; 2000 1614 2001 1615 switch (chip_type) { 1616 + case INV_MPU6000: 1617 + case INV_MPU6050: 1618 + indio_dev->channels = inv_mpu6050_channels; 1619 + indio_dev->num_channels = ARRAY_SIZE(inv_mpu6050_channels); 1620 + indio_dev->available_scan_masks = inv_mpu_scan_masks; 1621 + break; 2002 1622 case INV_MPU9150: 2003 1623 indio_dev->channels = inv_mpu9150_channels; 2004 1624 indio_dev->num_channels = ARRAY_SIZE(inv_mpu9150_channels); ··· 2018 1626 break; 2019 1627 case INV_ICM20600: 2020 1628 case INV_ICM20602: 2021 - indio_dev->channels = inv_mpu_channels; 2022 - indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels); 1629 + indio_dev->channels = inv_mpu6500_channels; 1630 + indio_dev->num_channels = ARRAY_SIZE(inv_mpu6500_channels); 2023 1631 indio_dev->available_scan_masks = inv_icm20602_scan_masks; 2024 1632 break; 2025 1633 default: 2026 - indio_dev->channels = inv_mpu_channels; 2027 - indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels); 1634 + indio_dev->channels = inv_mpu6500_channels; 1635 + indio_dev->num_channels = ARRAY_SIZE(inv_mpu6500_channels); 2028 1636 indio_dev->available_scan_masks = inv_mpu_scan_masks; 2029 1637 break; 2030 1638 } ··· 2033 1641 * auxiliary device in use. Otherwise Going back to 6-axis only. 2034 1642 */ 2035 1643 if (st->magn_disabled) { 2036 - indio_dev->channels = inv_mpu_channels; 2037 - indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels); 2038 - indio_dev->available_scan_masks = inv_mpu_scan_masks; 1644 + switch (chip_type) { 1645 + case INV_MPU9150: 1646 + indio_dev->channels = inv_mpu6050_channels; 1647 + indio_dev->num_channels = ARRAY_SIZE(inv_mpu6050_channels); 1648 + indio_dev->available_scan_masks = inv_mpu_scan_masks; 1649 + break; 1650 + default: 1651 + indio_dev->channels = inv_mpu6500_channels; 1652 + indio_dev->num_channels = ARRAY_SIZE(inv_mpu6500_channels); 1653 + indio_dev->available_scan_masks = inv_mpu_scan_masks; 1654 + break; 1655 + } 2039 1656 } 2040 1657 2041 1658 indio_dev->info = &mpu_info; ··· 2088 1687 { 2089 1688 struct iio_dev *indio_dev = dev_get_drvdata(dev); 2090 1689 struct inv_mpu6050_state *st = iio_priv(indio_dev); 1690 + bool wakeup; 2091 1691 int result; 2092 1692 2093 - mutex_lock(&st->lock); 2094 - result = inv_mpu_core_enable_regulator_vddio(st); 2095 - if (result) 2096 - goto out_unlock; 1693 + guard(mutex)(&st->lock); 2097 1694 2098 - result = inv_mpu6050_set_power_itg(st, true); 2099 - if (result) 2100 - goto out_unlock; 1695 + wakeup = device_may_wakeup(dev) && st->chip_config.wom_en; 1696 + 1697 + if (wakeup) { 1698 + enable_irq(st->irq); 1699 + disable_irq_wake(st->irq); 1700 + result = inv_mpu6050_set_wom_lp(st, false); 1701 + if (result) 1702 + return result; 1703 + } else { 1704 + result = inv_mpu_core_enable_regulator_vddio(st); 1705 + if (result) 1706 + return result; 1707 + result = inv_mpu6050_set_power_itg(st, true); 1708 + if (result) 1709 + return result; 1710 + } 2101 1711 2102 1712 pm_runtime_disable(dev); 2103 1713 pm_runtime_set_active(dev); ··· 2116 1704 2117 1705 result = inv_mpu6050_switch_engine(st, true, st->suspended_sensors); 2118 1706 if (result) 2119 - goto out_unlock; 1707 + return result; 1708 + 1709 + if (st->chip_config.wom_en && !wakeup) { 1710 + result = inv_mpu6050_set_wom_int(st, true); 1711 + if (result) 1712 + return result; 1713 + } 2120 1714 2121 1715 if (iio_buffer_enabled(indio_dev)) 2122 1716 result = inv_mpu6050_prepare_fifo(st, true); 2123 - 2124 - out_unlock: 2125 - mutex_unlock(&st->lock); 2126 1717 2127 1718 return result; 2128 1719 } ··· 2134 1719 { 2135 1720 struct iio_dev *indio_dev = dev_get_drvdata(dev); 2136 1721 struct inv_mpu6050_state *st = iio_priv(indio_dev); 1722 + bool wakeup; 2137 1723 int result; 2138 1724 2139 - mutex_lock(&st->lock); 1725 + guard(mutex)(&st->lock); 2140 1726 2141 1727 st->suspended_sensors = 0; 2142 - if (pm_runtime_suspended(dev)) { 2143 - result = 0; 2144 - goto out_unlock; 2145 - } 1728 + if (pm_runtime_suspended(dev)) 1729 + return 0; 2146 1730 2147 1731 if (iio_buffer_enabled(indio_dev)) { 2148 1732 result = inv_mpu6050_prepare_fifo(st, false); 2149 1733 if (result) 2150 - goto out_unlock; 1734 + return result; 2151 1735 } 2152 1736 2153 - if (st->chip_config.accl_en) 1737 + wakeup = device_may_wakeup(dev) && st->chip_config.wom_en; 1738 + 1739 + if (st->chip_config.wom_en && !wakeup) { 1740 + result = inv_mpu6050_set_wom_int(st, false); 1741 + if (result) 1742 + return result; 1743 + } 1744 + 1745 + if (st->chip_config.accl_en && !wakeup) 2154 1746 st->suspended_sensors |= INV_MPU6050_SENSOR_ACCL; 2155 1747 if (st->chip_config.gyro_en) 2156 1748 st->suspended_sensors |= INV_MPU6050_SENSOR_GYRO; ··· 2165 1743 st->suspended_sensors |= INV_MPU6050_SENSOR_TEMP; 2166 1744 if (st->chip_config.magn_en) 2167 1745 st->suspended_sensors |= INV_MPU6050_SENSOR_MAGN; 1746 + if (st->chip_config.wom_en && !wakeup) 1747 + st->suspended_sensors |= INV_MPU6050_SENSOR_WOM; 2168 1748 result = inv_mpu6050_switch_engine(st, false, st->suspended_sensors); 2169 1749 if (result) 2170 - goto out_unlock; 1750 + return result; 2171 1751 2172 - result = inv_mpu6050_set_power_itg(st, false); 2173 - if (result) 2174 - goto out_unlock; 1752 + if (wakeup) { 1753 + result = inv_mpu6050_set_wom_lp(st, true); 1754 + if (result) 1755 + return result; 1756 + enable_irq_wake(st->irq); 1757 + disable_irq(st->irq); 1758 + } else { 1759 + result = inv_mpu6050_set_power_itg(st, false); 1760 + if (result) 1761 + return result; 1762 + inv_mpu_core_disable_regulator_vddio(st); 1763 + } 2175 1764 2176 - inv_mpu_core_disable_regulator_vddio(st); 2177 - out_unlock: 2178 - mutex_unlock(&st->lock); 2179 - 2180 - return result; 1765 + return 0; 2181 1766 } 2182 1767 2183 1768 static int inv_mpu_runtime_suspend(struct device *dev) ··· 2196 1767 mutex_lock(&st->lock); 2197 1768 2198 1769 sensors = INV_MPU6050_SENSOR_ACCL | INV_MPU6050_SENSOR_GYRO | 2199 - INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN; 1770 + INV_MPU6050_SENSOR_TEMP | INV_MPU6050_SENSOR_MAGN | 1771 + INV_MPU6050_SENSOR_WOM; 2200 1772 ret = inv_mpu6050_switch_engine(st, false, sensors); 2201 1773 if (ret) 2202 1774 goto out_unlock;
+35 -1
drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
··· 88 88 INV_NUM_PARTS 89 89 }; 90 90 91 - /* chip sensors mask: accelerometer, gyroscope, temperature, magnetometer */ 91 + /* chip sensors mask: accelerometer, gyroscope, temperature, magnetometer, WoM */ 92 92 #define INV_MPU6050_SENSOR_ACCL BIT(0) 93 93 #define INV_MPU6050_SENSOR_GYRO BIT(1) 94 94 #define INV_MPU6050_SENSOR_TEMP BIT(2) 95 95 #define INV_MPU6050_SENSOR_MAGN BIT(3) 96 + #define INV_MPU6050_SENSOR_WOM BIT(4) 96 97 97 98 /** 98 99 * struct inv_mpu6050_chip_config - Cached chip configuration data. ··· 105 104 * @gyro_en: gyro engine enabled 106 105 * @temp_en: temperature sensor enabled 107 106 * @magn_en: magn engine (i2c master) enabled 107 + * @wom_en: Wake-on-Motion enabled 108 108 * @accl_fifo_enable: enable accel data output 109 109 * @gyro_fifo_enable: enable gyro data output 110 110 * @temp_fifo_enable: enable temp data output 111 111 * @magn_fifo_enable: enable magn data output 112 112 * @divider: chip sample rate divider (sample rate divider - 1) 113 + * @roc_threshold: save ROC threshold (WoM) set value 113 114 */ 114 115 struct inv_mpu6050_chip_config { 115 116 unsigned int clk:3; ··· 122 119 unsigned int gyro_en:1; 123 120 unsigned int temp_en:1; 124 121 unsigned int magn_en:1; 122 + unsigned int wom_en:1; 125 123 unsigned int accl_fifo_enable:1; 126 124 unsigned int gyro_fifo_enable:1; 127 125 unsigned int temp_fifo_enable:1; 128 126 unsigned int magn_fifo_enable:1; 129 127 u8 divider; 130 128 u8 user_ctrl; 129 + u64 roc_threshold; 131 130 }; 132 131 133 132 /* ··· 185 180 * @magn_orient: magnetometer sensor chip orientation if available. 186 181 * @suspended_sensors: sensors mask of sensors turned off for suspend 187 182 * @data: read buffer used for bulk reads. 183 + * @it_timestamp: interrupt timestamp. 188 184 */ 189 185 struct inv_mpu6050_state { 190 186 struct mutex lock; ··· 211 205 unsigned int suspended_sensors; 212 206 bool level_shifter; 213 207 u8 *data; 208 + s64 it_timestamp; 214 209 }; 215 210 216 211 /*register and associated bit definition*/ ··· 263 256 #define INV_MPU6050_REG_INT_ENABLE 0x38 264 257 #define INV_MPU6050_BIT_DATA_RDY_EN 0x01 265 258 #define INV_MPU6050_BIT_DMP_INT_EN 0x02 259 + #define INV_MPU6500_BIT_WOM_INT_EN BIT(6) 260 + #define INV_ICM20608_BIT_WOM_INT_EN GENMASK(7, 5) 266 261 267 262 #define INV_MPU6050_REG_RAW_ACCEL 0x3B 268 263 #define INV_MPU6050_REG_TEMPERATURE 0x41 269 264 #define INV_MPU6050_REG_RAW_GYRO 0x43 270 265 271 266 #define INV_MPU6050_REG_INT_STATUS 0x3A 267 + #define INV_MPU6500_BIT_WOM_INT BIT(6) 268 + #define INV_ICM20608_BIT_WOM_INT GENMASK(7, 5) 272 269 #define INV_MPU6050_BIT_FIFO_OVERFLOW_INT 0x10 273 270 #define INV_MPU6050_BIT_RAW_DATA_RDY_INT 0x01 274 271 ··· 305 294 #define INV_MPU6050_REG_PWR_MGMT_1 0x6B 306 295 #define INV_MPU6050_BIT_H_RESET 0x80 307 296 #define INV_MPU6050_BIT_SLEEP 0x40 297 + #define INV_MPU6050_BIT_CYCLE 0x20 308 298 #define INV_MPU6050_BIT_TEMP_DIS 0x08 309 299 #define INV_MPU6050_BIT_CLK_MASK 0x7 310 300 311 301 #define INV_MPU6050_REG_PWR_MGMT_2 0x6C 312 302 #define INV_MPU6050_BIT_PWR_ACCL_STBY 0x38 313 303 #define INV_MPU6050_BIT_PWR_GYRO_STBY 0x07 304 + 305 + /* ICM20609 registers */ 306 + #define INV_ICM20609_REG_ACCEL_WOM_X_THR 0x20 307 + #define INV_ICM20609_REG_ACCEL_WOM_Y_THR 0x21 308 + #define INV_ICM20609_REG_ACCEL_WOM_Z_THR 0x22 314 309 315 310 /* ICM20602 register */ 316 311 #define INV_ICM20602_REG_I2C_IF 0x70 ··· 337 320 /* mpu6500 registers */ 338 321 #define INV_MPU6500_REG_ACCEL_CONFIG_2 0x1D 339 322 #define INV_ICM20689_BITS_FIFO_SIZE_MAX 0xC0 323 + #define INV_MPU6500_REG_LP_ODR 0x1E 324 + #define INV_MPU6500_REG_WOM_THRESHOLD 0x1F 325 + #define INV_MPU6500_REG_ACCEL_INTEL_CTRL 0x69 326 + #define INV_MPU6500_BIT_ACCEL_INTEL_EN BIT(7) 327 + #define INV_MPU6500_BIT_ACCEL_INTEL_MODE BIT(6) 340 328 #define INV_MPU6500_REG_ACCEL_OFFSET 0x77 341 329 342 330 /* delay time in milliseconds */ ··· 452 430 INV_MPU6050_FILTER_5HZ, 453 431 INV_MPU6050_FILTER_NOLPF, 454 432 NUM_MPU6050_FILTER 433 + }; 434 + 435 + enum inv_mpu6050_lposc_e { 436 + INV_MPU6050_LPOSC_4HZ = 4, 437 + INV_MPU6050_LPOSC_8HZ, 438 + INV_MPU6050_LPOSC_16HZ, 439 + INV_MPU6050_LPOSC_31HZ, 440 + INV_MPU6050_LPOSC_62HZ, 441 + INV_MPU6050_LPOSC_125HZ, 442 + INV_MPU6050_LPOSC_250HZ, 443 + INV_MPU6050_LPOSC_500HZ, 444 + NUM_MPU6050_LPOSC, 455 445 }; 456 446 457 447 /* IIO attribute address */
+2 -15
drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
··· 33 33 34 34 reset_fifo_fail: 35 35 dev_err(regmap_get_device(st->map), "reset fifo failed %d\n", result); 36 - result = regmap_write(st->map, st->reg->int_enable, 37 - INV_MPU6050_BIT_DATA_RDY_EN); 38 - 39 - return result; 36 + return regmap_update_bits(st->map, st->reg->int_enable, 37 + INV_MPU6050_BIT_DATA_RDY_EN, INV_MPU6050_BIT_DATA_RDY_EN); 40 38 } 41 39 42 40 /* ··· 51 53 u32 fifo_period; 52 54 s64 timestamp; 53 55 u8 data[INV_MPU6050_OUTPUT_DATA_SIZE]; 54 - int int_status; 55 56 size_t i, nb; 56 57 57 58 mutex_lock(&st->lock); 58 - 59 - /* ack interrupt and check status */ 60 - result = regmap_read(st->map, st->reg->int_status, &int_status); 61 - if (result) { 62 - dev_err(regmap_get_device(st->map), 63 - "failed to ack interrupt\n"); 64 - goto flush_fifo; 65 - } 66 - if (!(int_status & INV_MPU6050_BIT_RAW_DATA_RDY_INT)) 67 - goto end_session; 68 59 69 60 if (!(st->chip_config.accl_fifo_enable | 70 61 st->chip_config.gyro_fifo_enable |
+72 -11
drivers/iio/imu/inv_mpu6050/inv_mpu_trigger.c
··· 6 6 #include <linux/pm_runtime.h> 7 7 8 8 #include <linux/iio/common/inv_sensors_timestamp.h> 9 + #include <linux/iio/events.h> 9 10 10 11 #include "inv_mpu_iio.h" 11 12 ··· 136 135 ret = regmap_write(st->map, st->reg->user_ctrl, d); 137 136 if (ret) 138 137 return ret; 139 - /* enable interrupt */ 140 - ret = regmap_write(st->map, st->reg->int_enable, 141 - INV_MPU6050_BIT_DATA_RDY_EN); 138 + /* enable data interrupt */ 139 + ret = regmap_update_bits(st->map, st->reg->int_enable, 140 + INV_MPU6050_BIT_DATA_RDY_EN, INV_MPU6050_BIT_DATA_RDY_EN); 142 141 } else { 143 - ret = regmap_write(st->map, st->reg->int_enable, 0); 142 + /* disable data interrupt */ 143 + ret = regmap_update_bits(st->map, st->reg->int_enable, 144 + INV_MPU6050_BIT_DATA_RDY_EN, 0); 144 145 if (ret) 145 146 return ret; 146 147 ret = regmap_write(st->map, st->reg->fifo_en, 0); ··· 175 172 return result; 176 173 /* 177 174 * In case autosuspend didn't trigger, turn off first not 178 - * required sensors. 175 + * required sensors excepted WoM 179 176 */ 180 - result = inv_mpu6050_switch_engine(st, false, ~scan); 177 + result = inv_mpu6050_switch_engine(st, false, ~scan & ~INV_MPU6050_SENSOR_WOM); 181 178 if (result) 182 179 goto error_power_off; 183 180 result = inv_mpu6050_switch_engine(st, true, scan); ··· 229 226 .set_trigger_state = &inv_mpu_data_rdy_trigger_set_state, 230 227 }; 231 228 229 + static irqreturn_t inv_mpu6050_interrupt_timestamp(int irq, void *p) 230 + { 231 + struct iio_dev *indio_dev = p; 232 + struct inv_mpu6050_state *st = iio_priv(indio_dev); 233 + 234 + st->it_timestamp = iio_get_time_ns(indio_dev); 235 + 236 + return IRQ_WAKE_THREAD; 237 + } 238 + 239 + static irqreturn_t inv_mpu6050_interrupt_handle(int irq, void *p) 240 + { 241 + struct iio_dev *indio_dev = p; 242 + struct inv_mpu6050_state *st = iio_priv(indio_dev); 243 + unsigned int int_status, wom_bits; 244 + u64 ev_code; 245 + int result; 246 + 247 + switch (st->chip_type) { 248 + case INV_MPU6050: 249 + case INV_MPU6500: 250 + case INV_MPU6515: 251 + case INV_MPU6880: 252 + case INV_MPU6000: 253 + case INV_MPU9150: 254 + case INV_MPU9250: 255 + case INV_MPU9255: 256 + wom_bits = INV_MPU6500_BIT_WOM_INT; 257 + break; 258 + default: 259 + wom_bits = INV_ICM20608_BIT_WOM_INT; 260 + break; 261 + } 262 + 263 + scoped_guard(mutex, &st->lock) { 264 + /* ack interrupt and check status */ 265 + result = regmap_read(st->map, st->reg->int_status, &int_status); 266 + if (result) { 267 + dev_err(regmap_get_device(st->map), "failed to ack interrupt\n"); 268 + return IRQ_HANDLED; 269 + } 270 + 271 + /* handle WoM event */ 272 + if (st->chip_config.wom_en && (int_status & wom_bits)) { 273 + ev_code = IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z, 274 + IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING); 275 + iio_push_event(indio_dev, ev_code, st->it_timestamp); 276 + } 277 + } 278 + 279 + /* handle raw data interrupt */ 280 + if (int_status & INV_MPU6050_BIT_RAW_DATA_RDY_INT) { 281 + indio_dev->pollfunc->timestamp = st->it_timestamp; 282 + iio_trigger_poll_nested(st->trig); 283 + } 284 + 285 + return IRQ_HANDLED; 286 + } 287 + 232 288 int inv_mpu6050_probe_trigger(struct iio_dev *indio_dev, int irq_type) 233 289 { 234 290 int ret; ··· 300 238 if (!st->trig) 301 239 return -ENOMEM; 302 240 303 - ret = devm_request_irq(&indio_dev->dev, st->irq, 304 - &iio_trigger_generic_data_rdy_poll, 305 - irq_type, 306 - "inv_mpu", 307 - st->trig); 241 + ret = devm_request_threaded_irq(&indio_dev->dev, st->irq, 242 + &inv_mpu6050_interrupt_timestamp, 243 + &inv_mpu6050_interrupt_handle, 244 + irq_type, "inv_mpu", indio_dev); 308 245 if (ret) 309 246 return ret; 310 247
+1 -1
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
··· 2726 2726 if (!hw) 2727 2727 return -ENOMEM; 2728 2728 2729 - dev_set_drvdata(dev, (void *)hw); 2729 + dev_set_drvdata(dev, hw); 2730 2730 2731 2731 mutex_init(&hw->fifo_lock); 2732 2732 mutex_init(&hw->conf_lock);
+179
drivers/iio/industrialio-backend.c
··· 43 43 #include <linux/types.h> 44 44 45 45 #include <linux/iio/backend.h> 46 + #include <linux/iio/iio.h> 46 47 47 48 struct iio_backend { 48 49 struct list_head entry; 49 50 const struct iio_backend_ops *ops; 51 + struct device *frontend_dev; 50 52 struct device *dev; 51 53 struct module *owner; 52 54 void *priv; ··· 188 186 } 189 187 EXPORT_SYMBOL_NS_GPL(iio_backend_data_format_set, IIO_BACKEND); 190 188 189 + /** 190 + * iio_backend_data_source_set - Select data source 191 + * @back: Backend device 192 + * @chan: Channel number 193 + * @data: Data source 194 + * 195 + * A given backend may have different sources to stream/sync data. This allows 196 + * to choose that source. 197 + * 198 + * RETURNS: 199 + * 0 on success, negative error number on failure. 200 + */ 201 + int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan, 202 + enum iio_backend_data_source data) 203 + { 204 + if (data >= IIO_BACKEND_DATA_SOURCE_MAX) 205 + return -EINVAL; 206 + 207 + return iio_backend_op_call(back, data_source_set, chan, data); 208 + } 209 + EXPORT_SYMBOL_NS_GPL(iio_backend_data_source_set, IIO_BACKEND); 210 + 211 + /** 212 + * iio_backend_set_sampling_freq - Set channel sampling rate 213 + * @back: Backend device 214 + * @chan: Channel number 215 + * @sample_rate_hz: Sample rate 216 + * 217 + * RETURNS: 218 + * 0 on success, negative error number on failure. 219 + */ 220 + int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan, 221 + u64 sample_rate_hz) 222 + { 223 + return iio_backend_op_call(back, set_sample_rate, chan, sample_rate_hz); 224 + } 225 + EXPORT_SYMBOL_NS_GPL(iio_backend_set_sampling_freq, IIO_BACKEND); 226 + 191 227 static void iio_backend_free_buffer(void *arg) 192 228 { 193 229 struct iio_backend_buffer_pair *pair = arg; ··· 271 231 } 272 232 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, IIO_BACKEND); 273 233 234 + static struct iio_backend *iio_backend_from_indio_dev_parent(const struct device *dev) 235 + { 236 + struct iio_backend *back = ERR_PTR(-ENODEV), *iter; 237 + 238 + /* 239 + * We deliberately go through all backends even after finding a match. 240 + * The reason is that we want to catch frontend devices which have more 241 + * than one backend in which case returning the first we find is bogus. 242 + * For those cases, frontends need to explicitly define 243 + * get_iio_backend() in struct iio_info. 244 + */ 245 + guard(mutex)(&iio_back_lock); 246 + list_for_each_entry(iter, &iio_back_list, entry) { 247 + if (dev == iter->frontend_dev) { 248 + if (!IS_ERR(back)) { 249 + dev_warn(dev, 250 + "Multiple backends! get_iio_backend() needs to be implemented"); 251 + return ERR_PTR(-ENODEV); 252 + } 253 + 254 + back = iter; 255 + } 256 + } 257 + 258 + return back; 259 + } 260 + 261 + /** 262 + * iio_backend_ext_info_get - IIO ext_info read callback 263 + * @indio_dev: IIO device 264 + * @private: Data private to the driver 265 + * @chan: IIO channel 266 + * @buf: Buffer where to place the attribute data 267 + * 268 + * This helper is intended to be used by backends that extend an IIO channel 269 + * (through iio_backend_extend_chan_spec()) with extended info. In that case, 270 + * backends are not supposed to give their own callbacks (as they would not have 271 + * a way to get the backend from indio_dev). This is the getter. 272 + * 273 + * RETURNS: 274 + * Number of bytes written to buf, negative error number on failure. 275 + */ 276 + ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private, 277 + const struct iio_chan_spec *chan, char *buf) 278 + { 279 + struct iio_backend *back; 280 + 281 + /* 282 + * The below should work for the majority of the cases. It will not work 283 + * when one frontend has multiple backends in which case we'll need a 284 + * new callback in struct iio_info so we can directly request the proper 285 + * backend from the frontend. Anyways, let's only introduce new options 286 + * when really needed... 287 + */ 288 + back = iio_backend_from_indio_dev_parent(indio_dev->dev.parent); 289 + if (IS_ERR(back)) 290 + return PTR_ERR(back); 291 + 292 + return iio_backend_op_call(back, ext_info_get, private, chan, buf); 293 + } 294 + EXPORT_SYMBOL_NS_GPL(iio_backend_ext_info_get, IIO_BACKEND); 295 + 296 + /** 297 + * iio_backend_ext_info_set - IIO ext_info write callback 298 + * @indio_dev: IIO device 299 + * @private: Data private to the driver 300 + * @chan: IIO channel 301 + * @buf: Buffer holding the sysfs attribute 302 + * @len: Buffer length 303 + * 304 + * This helper is intended to be used by backends that extend an IIO channel 305 + * (trough iio_backend_extend_chan_spec()) with extended info. In that case, 306 + * backends are not supposed to give their own callbacks (as they would not have 307 + * a way to get the backend from indio_dev). This is the setter. 308 + * 309 + * RETURNS: 310 + * Buffer length on success, negative error number on failure. 311 + */ 312 + ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private, 313 + const struct iio_chan_spec *chan, 314 + const char *buf, size_t len) 315 + { 316 + struct iio_backend *back; 317 + 318 + back = iio_backend_from_indio_dev_parent(indio_dev->dev.parent); 319 + if (IS_ERR(back)) 320 + return PTR_ERR(back); 321 + 322 + return iio_backend_op_call(back, ext_info_set, private, chan, buf, len); 323 + } 324 + EXPORT_SYMBOL_NS_GPL(iio_backend_ext_info_set, IIO_BACKEND); 325 + 326 + /** 327 + * iio_backend_extend_chan_spec - Extend an IIO channel 328 + * @indio_dev: IIO device 329 + * @back: Backend device 330 + * @chan: IIO channel 331 + * 332 + * Some backends may have their own functionalities and hence capable of 333 + * extending a frontend's channel. 334 + * 335 + * RETURNS: 336 + * 0 on success, negative error number on failure. 337 + */ 338 + int iio_backend_extend_chan_spec(struct iio_dev *indio_dev, 339 + struct iio_backend *back, 340 + struct iio_chan_spec *chan) 341 + { 342 + const struct iio_chan_spec_ext_info *frontend_ext_info = chan->ext_info; 343 + const struct iio_chan_spec_ext_info *back_ext_info; 344 + int ret; 345 + 346 + ret = iio_backend_op_call(back, extend_chan_spec, chan); 347 + if (ret) 348 + return ret; 349 + /* 350 + * Let's keep things simple for now. Don't allow to overwrite the 351 + * frontend's extended info. If ever needed, we can support appending 352 + * it. 353 + */ 354 + if (frontend_ext_info && chan->ext_info != frontend_ext_info) 355 + return -EOPNOTSUPP; 356 + if (!chan->ext_info) 357 + return 0; 358 + 359 + /* Don't allow backends to get creative and force their own handlers */ 360 + for (back_ext_info = chan->ext_info; back_ext_info->name; back_ext_info++) { 361 + if (back_ext_info->read != iio_backend_ext_info_get) 362 + return -EINVAL; 363 + if (back_ext_info->write != iio_backend_ext_info_set) 364 + return -EINVAL; 365 + } 366 + 367 + return 0; 368 + } 369 + EXPORT_SYMBOL_NS_GPL(iio_backend_extend_chan_spec, IIO_BACKEND); 370 + 274 371 static void iio_backend_release(void *arg) 275 372 { 276 373 struct iio_backend *back = arg; ··· 439 262 return dev_err_probe(dev, -EINVAL, 440 263 "Could not link to supplier(%s)\n", 441 264 dev_name(back->dev)); 265 + 266 + back->frontend_dev = dev; 442 267 443 268 dev_dbg(dev, "Found backend(%s) device\n", dev_name(back->dev)); 444 269
+48 -74
drivers/iio/industrialio-buffer.c
··· 10 10 * - Alternative access techniques? 11 11 */ 12 12 #include <linux/anon_inodes.h> 13 + #include <linux/cleanup.h> 13 14 #include <linux/kernel.h> 14 15 #include <linux/export.h> 15 16 #include <linux/device.h> ··· 534 533 ret = kstrtobool(buf, &state); 535 534 if (ret < 0) 536 535 return ret; 537 - mutex_lock(&iio_dev_opaque->mlock); 538 - if (iio_buffer_is_active(buffer)) { 539 - ret = -EBUSY; 540 - goto error_ret; 541 - } 536 + 537 + guard(mutex)(&iio_dev_opaque->mlock); 538 + if (iio_buffer_is_active(buffer)) 539 + return -EBUSY; 540 + 542 541 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); 543 542 if (ret < 0) 544 - goto error_ret; 545 - if (!state && ret) { 546 - ret = iio_scan_mask_clear(buffer, this_attr->address); 547 - if (ret) 548 - goto error_ret; 549 - } else if (state && !ret) { 543 + return ret; 544 + 545 + if (state && ret) 546 + return len; 547 + 548 + if (state) 550 549 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); 551 - if (ret) 552 - goto error_ret; 553 - } 550 + else 551 + ret = iio_scan_mask_clear(buffer, this_attr->address); 552 + if (ret) 553 + return ret; 554 554 555 - error_ret: 556 - mutex_unlock(&iio_dev_opaque->mlock); 557 - 558 - return ret < 0 ? ret : len; 555 + return len; 559 556 } 560 557 561 558 static ssize_t iio_scan_el_ts_show(struct device *dev, ··· 580 581 if (ret < 0) 581 582 return ret; 582 583 583 - mutex_lock(&iio_dev_opaque->mlock); 584 - if (iio_buffer_is_active(buffer)) { 585 - ret = -EBUSY; 586 - goto error_ret; 587 - } 588 - buffer->scan_timestamp = state; 589 - error_ret: 590 - mutex_unlock(&iio_dev_opaque->mlock); 584 + guard(mutex)(&iio_dev_opaque->mlock); 585 + if (iio_buffer_is_active(buffer)) 586 + return -EBUSY; 591 587 592 - return ret ? ret : len; 588 + buffer->scan_timestamp = state; 589 + 590 + return len; 593 591 } 594 592 595 593 static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, ··· 670 674 if (val == buffer->length) 671 675 return len; 672 676 673 - mutex_lock(&iio_dev_opaque->mlock); 674 - if (iio_buffer_is_active(buffer)) { 675 - ret = -EBUSY; 676 - } else { 677 - buffer->access->set_length(buffer, val); 678 - ret = 0; 679 - } 680 - if (ret) 681 - goto out; 677 + guard(mutex)(&iio_dev_opaque->mlock); 678 + if (iio_buffer_is_active(buffer)) 679 + return -EBUSY; 680 + 681 + buffer->access->set_length(buffer, val); 682 + 682 683 if (buffer->length && buffer->length < buffer->watermark) 683 684 buffer->watermark = buffer->length; 684 - out: 685 - mutex_unlock(&iio_dev_opaque->mlock); 686 685 687 - return ret ? ret : len; 686 + return len; 688 687 } 689 688 690 689 static ssize_t enable_show(struct device *dev, struct device_attribute *attr, ··· 1259 1268 struct iio_buffer *remove_buffer) 1260 1269 { 1261 1270 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1262 - int ret; 1263 1271 1264 1272 if (insert_buffer == remove_buffer) 1265 1273 return 0; ··· 1267 1277 insert_buffer->direction == IIO_BUFFER_DIRECTION_OUT) 1268 1278 return -EINVAL; 1269 1279 1270 - mutex_lock(&iio_dev_opaque->info_exist_lock); 1271 - mutex_lock(&iio_dev_opaque->mlock); 1280 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 1281 + guard(mutex)(&iio_dev_opaque->mlock); 1272 1282 1273 1283 if (insert_buffer && iio_buffer_is_active(insert_buffer)) 1274 1284 insert_buffer = NULL; ··· 1276 1286 if (remove_buffer && !iio_buffer_is_active(remove_buffer)) 1277 1287 remove_buffer = NULL; 1278 1288 1279 - if (!insert_buffer && !remove_buffer) { 1280 - ret = 0; 1281 - goto out_unlock; 1282 - } 1289 + if (!insert_buffer && !remove_buffer) 1290 + return 0; 1283 1291 1284 - if (!indio_dev->info) { 1285 - ret = -ENODEV; 1286 - goto out_unlock; 1287 - } 1292 + if (!indio_dev->info) 1293 + return -ENODEV; 1288 1294 1289 - ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 1290 - 1291 - out_unlock: 1292 - mutex_unlock(&iio_dev_opaque->mlock); 1293 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 1294 - 1295 - return ret; 1295 + return __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 1296 1296 } 1297 1297 EXPORT_SYMBOL_GPL(iio_update_buffers); 1298 1298 ··· 1306 1326 if (ret < 0) 1307 1327 return ret; 1308 1328 1309 - mutex_lock(&iio_dev_opaque->mlock); 1329 + guard(mutex)(&iio_dev_opaque->mlock); 1310 1330 1311 1331 /* Find out if it is in the list */ 1312 1332 inlist = iio_buffer_is_active(buffer); 1313 1333 /* Already in desired state */ 1314 1334 if (inlist == requested_state) 1315 - goto done; 1335 + return len; 1316 1336 1317 1337 if (requested_state) 1318 1338 ret = __iio_update_buffers(indio_dev, buffer, NULL); 1319 1339 else 1320 1340 ret = __iio_update_buffers(indio_dev, NULL, buffer); 1341 + if (ret) 1342 + return ret; 1321 1343 1322 - done: 1323 - mutex_unlock(&iio_dev_opaque->mlock); 1324 - return (ret < 0) ? ret : len; 1344 + return len; 1325 1345 } 1326 1346 1327 1347 static ssize_t watermark_show(struct device *dev, struct device_attribute *attr, ··· 1348 1368 if (!val) 1349 1369 return -EINVAL; 1350 1370 1351 - mutex_lock(&iio_dev_opaque->mlock); 1371 + guard(mutex)(&iio_dev_opaque->mlock); 1352 1372 1353 - if (val > buffer->length) { 1354 - ret = -EINVAL; 1355 - goto out; 1356 - } 1373 + if (val > buffer->length) 1374 + return -EINVAL; 1357 1375 1358 - if (iio_buffer_is_active(buffer)) { 1359 - ret = -EBUSY; 1360 - goto out; 1361 - } 1376 + if (iio_buffer_is_active(buffer)) 1377 + return -EBUSY; 1362 1378 1363 1379 buffer->watermark = val; 1364 - out: 1365 - mutex_unlock(&iio_dev_opaque->mlock); 1366 1380 1367 - return ret ? ret : len; 1381 + return len; 1368 1382 } 1369 1383 1370 1384 static ssize_t data_available_show(struct device *dev,
+21 -28
drivers/iio/industrialio-core.c
··· 11 11 12 12 #include <linux/anon_inodes.h> 13 13 #include <linux/cdev.h> 14 + #include <linux/cleanup.h> 14 15 #include <linux/debugfs.h> 15 16 #include <linux/device.h> 16 17 #include <linux/err.h> ··· 1644 1643 struct iio_dev *indio_dev; 1645 1644 size_t alloc_size; 1646 1645 1647 - alloc_size = sizeof(struct iio_dev_opaque); 1648 - if (sizeof_priv) { 1649 - alloc_size = ALIGN(alloc_size, IIO_DMA_MINALIGN); 1650 - alloc_size += sizeof_priv; 1651 - } 1646 + if (sizeof_priv) 1647 + alloc_size = ALIGN(sizeof(*iio_dev_opaque), IIO_DMA_MINALIGN) + sizeof_priv; 1648 + else 1649 + alloc_size = sizeof(*iio_dev_opaque); 1652 1650 1653 1651 iio_dev_opaque = kzalloc(alloc_size, GFP_KERNEL); 1654 1652 if (!iio_dev_opaque) 1655 1653 return NULL; 1656 1654 1657 1655 indio_dev = &iio_dev_opaque->indio_dev; 1658 - indio_dev->priv = (char *)iio_dev_opaque + 1659 - ALIGN(sizeof(struct iio_dev_opaque), IIO_DMA_MINALIGN); 1656 + 1657 + if (sizeof_priv) 1658 + indio_dev->priv = (char *)iio_dev_opaque + 1659 + ALIGN(sizeof(*iio_dev_opaque), IIO_DMA_MINALIGN); 1660 1660 1661 1661 indio_dev->dev.parent = parent; 1662 1662 indio_dev->dev.type = &iio_device_type; ··· 1811 1809 struct iio_dev *indio_dev = ib->indio_dev; 1812 1810 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1813 1811 struct iio_ioctl_handler *h; 1814 - int ret = -ENODEV; 1812 + int ret; 1815 1813 1816 - mutex_lock(&iio_dev_opaque->info_exist_lock); 1817 - 1814 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 1818 1815 /* 1819 1816 * The NULL check here is required to prevent crashing when a device 1820 1817 * is being removed while userspace would still have open file handles 1821 1818 * to try to access this device. 1822 1819 */ 1823 1820 if (!indio_dev->info) 1824 - goto out_unlock; 1821 + return -ENODEV; 1825 1822 1826 1823 list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) { 1827 1824 ret = h->ioctl(indio_dev, filp, cmd, arg); 1828 1825 if (ret != IIO_IOCTL_UNHANDLED) 1829 - break; 1826 + return ret; 1830 1827 } 1831 1828 1832 - if (ret == IIO_IOCTL_UNHANDLED) 1833 - ret = -ENODEV; 1834 - 1835 - out_unlock: 1836 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 1837 - 1838 - return ret; 1829 + return -ENODEV; 1839 1830 } 1840 1831 1841 1832 static const struct file_operations iio_buffer_fileops = { ··· 2056 2061 2057 2062 cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev); 2058 2063 2059 - mutex_lock(&iio_dev_opaque->info_exist_lock); 2064 + scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) { 2065 + iio_device_unregister_debugfs(indio_dev); 2060 2066 2061 - iio_device_unregister_debugfs(indio_dev); 2067 + iio_disable_all_buffers(indio_dev); 2062 2068 2063 - iio_disable_all_buffers(indio_dev); 2069 + indio_dev->info = NULL; 2064 2070 2065 - indio_dev->info = NULL; 2066 - 2067 - iio_device_wakeup_eventset(indio_dev); 2068 - iio_buffer_wakeup_poll(indio_dev); 2069 - 2070 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 2071 + iio_device_wakeup_eventset(indio_dev); 2072 + iio_buffer_wakeup_poll(indio_dev); 2073 + } 2071 2074 2072 2075 iio_buffers_free_sysfs_and_mask(indio_dev); 2073 2076 }
+30 -41
drivers/iio/industrialio-trigger.c
··· 4 4 * Copyright (c) 2008 Jonathan Cameron 5 5 */ 6 6 7 + #include <linux/cleanup.h> 7 8 #include <linux/kernel.h> 8 9 #include <linux/idr.h> 9 10 #include <linux/err.h> ··· 81 80 goto error_unregister_id; 82 81 83 82 /* Add to list of available triggers held by the IIO core */ 84 - mutex_lock(&iio_trigger_list_lock); 85 - if (__iio_trigger_find_by_name(trig_info->name)) { 86 - pr_err("Duplicate trigger name '%s'\n", trig_info->name); 87 - ret = -EEXIST; 88 - goto error_device_del; 83 + scoped_guard(mutex, &iio_trigger_list_lock) { 84 + if (__iio_trigger_find_by_name(trig_info->name)) { 85 + pr_err("Duplicate trigger name '%s'\n", trig_info->name); 86 + ret = -EEXIST; 87 + goto error_device_del; 88 + } 89 + list_add_tail(&trig_info->list, &iio_trigger_list); 89 90 } 90 - list_add_tail(&trig_info->list, &iio_trigger_list); 91 - mutex_unlock(&iio_trigger_list_lock); 92 91 93 92 return 0; 94 93 95 94 error_device_del: 96 - mutex_unlock(&iio_trigger_list_lock); 97 95 device_del(&trig_info->dev); 98 96 error_unregister_id: 99 97 ida_free(&iio_trigger_ida, trig_info->id); ··· 102 102 103 103 void iio_trigger_unregister(struct iio_trigger *trig_info) 104 104 { 105 - mutex_lock(&iio_trigger_list_lock); 106 - list_del(&trig_info->list); 107 - mutex_unlock(&iio_trigger_list_lock); 105 + scoped_guard(mutex, &iio_trigger_list_lock) 106 + list_del(&trig_info->list); 108 107 109 108 ida_free(&iio_trigger_ida, trig_info->id); 110 109 /* Possible issue in here */ ··· 119 120 return -EINVAL; 120 121 121 122 iio_dev_opaque = to_iio_dev_opaque(indio_dev); 122 - mutex_lock(&iio_dev_opaque->mlock); 123 + guard(mutex)(&iio_dev_opaque->mlock); 123 124 WARN_ON(iio_dev_opaque->trig_readonly); 124 125 125 126 indio_dev->trig = iio_trigger_get(trig); 126 127 iio_dev_opaque->trig_readonly = true; 127 - mutex_unlock(&iio_dev_opaque->mlock); 128 128 129 129 return 0; 130 130 } ··· 143 145 144 146 static struct iio_trigger *iio_trigger_acquire_by_name(const char *name) 145 147 { 146 - struct iio_trigger *trig = NULL, *iter; 148 + struct iio_trigger *iter; 147 149 148 - mutex_lock(&iio_trigger_list_lock); 150 + guard(mutex)(&iio_trigger_list_lock); 149 151 list_for_each_entry(iter, &iio_trigger_list, list) 150 - if (sysfs_streq(iter->name, name)) { 151 - trig = iter; 152 - iio_trigger_get(trig); 153 - break; 154 - } 155 - mutex_unlock(&iio_trigger_list_lock); 152 + if (sysfs_streq(iter->name, name)) 153 + return iio_trigger_get(iter); 156 154 157 - return trig; 155 + return NULL; 158 156 } 159 157 160 158 static void iio_reenable_work_fn(struct work_struct *work) ··· 253 259 { 254 260 int ret; 255 261 256 - mutex_lock(&trig->pool_lock); 257 - ret = bitmap_find_free_region(trig->pool, 258 - CONFIG_IIO_CONSUMERS_PER_TRIGGER, 259 - ilog2(1)); 260 - mutex_unlock(&trig->pool_lock); 261 - if (ret >= 0) 262 - ret += trig->subirq_base; 262 + scoped_guard(mutex, &trig->pool_lock) { 263 + ret = bitmap_find_free_region(trig->pool, 264 + CONFIG_IIO_CONSUMERS_PER_TRIGGER, 265 + ilog2(1)); 266 + if (ret < 0) 267 + return ret; 268 + } 263 269 264 - return ret; 270 + return ret + trig->subirq_base; 265 271 } 266 272 267 273 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq) 268 274 { 269 - mutex_lock(&trig->pool_lock); 275 + guard(mutex)(&trig->pool_lock); 270 276 clear_bit(irq - trig->subirq_base, trig->pool); 271 - mutex_unlock(&trig->pool_lock); 272 277 } 273 278 274 279 /* Complexity in here. With certain triggers (datardy) an acknowledgement ··· 444 451 struct iio_trigger *trig; 445 452 int ret; 446 453 447 - mutex_lock(&iio_dev_opaque->mlock); 448 - if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) { 449 - mutex_unlock(&iio_dev_opaque->mlock); 450 - return -EBUSY; 454 + scoped_guard(mutex, &iio_dev_opaque->mlock) { 455 + if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) 456 + return -EBUSY; 457 + if (iio_dev_opaque->trig_readonly) 458 + return -EPERM; 451 459 } 452 - if (iio_dev_opaque->trig_readonly) { 453 - mutex_unlock(&iio_dev_opaque->mlock); 454 - return -EPERM; 455 - } 456 - mutex_unlock(&iio_dev_opaque->mlock); 457 460 458 461 trig = iio_trigger_acquire_by_name(buf); 459 462 if (oldtrig == trig) {
+89 -174
drivers/iio/inkern.c
··· 3 3 * 4 4 * Copyright (c) 2011 Jonathan Cameron 5 5 */ 6 + #include <linux/cleanup.h> 6 7 #include <linux/err.h> 7 8 #include <linux/export.h> 8 9 #include <linux/minmax.h> ··· 44 43 45 44 int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps) 46 45 { 47 - int i = 0, ret = 0; 48 46 struct iio_map_internal *mapi; 47 + int i = 0; 48 + int ret; 49 49 50 50 if (!maps) 51 51 return 0; 52 52 53 - mutex_lock(&iio_map_list_lock); 53 + guard(mutex)(&iio_map_list_lock); 54 54 while (maps[i].consumer_dev_name) { 55 55 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL); 56 56 if (!mapi) { ··· 63 61 list_add_tail(&mapi->l, &iio_map_list); 64 62 i++; 65 63 } 66 - error_ret: 67 - if (ret) 68 - iio_map_array_unregister_locked(indio_dev); 69 - mutex_unlock(&iio_map_list_lock); 70 64 65 + return 0; 66 + error_ret: 67 + iio_map_array_unregister_locked(indio_dev); 71 68 return ret; 72 69 } 73 70 EXPORT_SYMBOL_GPL(iio_map_array_register); ··· 76 75 */ 77 76 int iio_map_array_unregister(struct iio_dev *indio_dev) 78 77 { 79 - int ret; 80 - 81 - mutex_lock(&iio_map_list_lock); 82 - ret = iio_map_array_unregister_locked(indio_dev); 83 - mutex_unlock(&iio_map_list_lock); 84 - 85 - return ret; 78 + guard(mutex)(&iio_map_list_lock); 79 + return iio_map_array_unregister_locked(indio_dev); 86 80 } 87 81 EXPORT_SYMBOL_GPL(iio_map_array_unregister); 88 82 ··· 179 183 static struct iio_channel *fwnode_iio_channel_get(struct fwnode_handle *fwnode, 180 184 int index) 181 185 { 182 - struct iio_channel *channel; 183 186 int err; 184 187 185 188 if (index < 0) 186 189 return ERR_PTR(-EINVAL); 187 190 188 - channel = kzalloc(sizeof(*channel), GFP_KERNEL); 191 + struct iio_channel *channel __free(kfree) = 192 + kzalloc(sizeof(*channel), GFP_KERNEL); 189 193 if (!channel) 190 194 return ERR_PTR(-ENOMEM); 191 195 192 196 err = __fwnode_iio_channel_get(channel, fwnode, index); 193 197 if (err) 194 - goto err_free_channel; 198 + return ERR_PTR(err); 195 199 196 - return channel; 197 - 198 - err_free_channel: 199 - kfree(channel); 200 - return ERR_PTR(err); 200 + return_ptr(channel); 201 201 } 202 202 203 203 static struct iio_channel * ··· 283 291 static struct iio_channel *fwnode_iio_channel_get_all(struct device *dev) 284 292 { 285 293 struct fwnode_handle *fwnode = dev_fwnode(dev); 286 - struct iio_channel *chans; 287 294 int i, mapind, nummaps = 0; 288 295 int ret; 289 296 ··· 298 307 return ERR_PTR(-ENODEV); 299 308 300 309 /* NULL terminated array to save passing size */ 301 - chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 310 + struct iio_channel *chans __free(kfree) = 311 + kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 302 312 if (!chans) 303 313 return ERR_PTR(-ENOMEM); 304 314 ··· 309 317 if (ret) 310 318 goto error_free_chans; 311 319 } 312 - return chans; 320 + return_ptr(chans); 313 321 314 322 error_free_chans: 315 323 for (i = 0; i < mapind; i++) 316 324 iio_device_put(chans[i].indio_dev); 317 - kfree(chans); 318 325 return ERR_PTR(ret); 319 326 } 320 327 ··· 321 330 const char *channel_name) 322 331 { 323 332 struct iio_map_internal *c_i = NULL, *c = NULL; 324 - struct iio_channel *channel; 325 333 int err; 326 334 327 335 if (!(name || channel_name)) 328 336 return ERR_PTR(-ENODEV); 329 337 330 338 /* first find matching entry the channel map */ 331 - mutex_lock(&iio_map_list_lock); 332 - list_for_each_entry(c_i, &iio_map_list, l) { 333 - if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || 334 - (channel_name && 335 - strcmp(channel_name, c_i->map->consumer_channel) != 0)) 336 - continue; 337 - c = c_i; 338 - iio_device_get(c->indio_dev); 339 - break; 339 + scoped_guard(mutex, &iio_map_list_lock) { 340 + list_for_each_entry(c_i, &iio_map_list, l) { 341 + if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) || 342 + (channel_name && 343 + strcmp(channel_name, c_i->map->consumer_channel) != 0)) 344 + continue; 345 + c = c_i; 346 + iio_device_get(c->indio_dev); 347 + break; 348 + } 340 349 } 341 - mutex_unlock(&iio_map_list_lock); 342 350 if (!c) 343 351 return ERR_PTR(-ENODEV); 344 352 345 - channel = kzalloc(sizeof(*channel), GFP_KERNEL); 353 + struct iio_channel *channel __free(kfree) = 354 + kzalloc(sizeof(*channel), GFP_KERNEL); 346 355 if (!channel) { 347 356 err = -ENOMEM; 348 357 goto error_no_mem; ··· 357 366 358 367 if (!channel->channel) { 359 368 err = -EINVAL; 360 - goto error_no_chan; 369 + goto error_no_mem; 361 370 } 362 371 } 363 372 364 - return channel; 373 + return_ptr(channel); 365 374 366 - error_no_chan: 367 - kfree(channel); 368 375 error_no_mem: 369 376 iio_device_put(c->indio_dev); 370 377 return ERR_PTR(err); ··· 439 450 struct iio_channel *iio_channel_get_all(struct device *dev) 440 451 { 441 452 const char *name; 442 - struct iio_channel *chans; 443 453 struct iio_map_internal *c = NULL; 454 + struct iio_channel *fw_chans; 444 455 int nummaps = 0; 445 456 int mapind = 0; 446 457 int i, ret; ··· 448 459 if (!dev) 449 460 return ERR_PTR(-EINVAL); 450 461 451 - chans = fwnode_iio_channel_get_all(dev); 462 + fw_chans = fwnode_iio_channel_get_all(dev); 452 463 /* 453 464 * We only want to carry on if the error is -ENODEV. Anything else 454 465 * should be reported up the stack. 455 466 */ 456 - if (!IS_ERR(chans) || PTR_ERR(chans) != -ENODEV) 457 - return chans; 467 + if (!IS_ERR(fw_chans) || PTR_ERR(fw_chans) != -ENODEV) 468 + return fw_chans; 458 469 459 470 name = dev_name(dev); 460 471 461 - mutex_lock(&iio_map_list_lock); 472 + guard(mutex)(&iio_map_list_lock); 462 473 /* first count the matching maps */ 463 474 list_for_each_entry(c, &iio_map_list, l) 464 475 if (name && strcmp(name, c->map->consumer_dev_name) != 0) ··· 466 477 else 467 478 nummaps++; 468 479 469 - if (nummaps == 0) { 470 - ret = -ENODEV; 471 - goto error_ret; 472 - } 480 + if (nummaps == 0) 481 + return ERR_PTR(-ENODEV); 473 482 474 483 /* NULL terminated array to save passing size */ 475 - chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 476 - if (!chans) { 477 - ret = -ENOMEM; 478 - goto error_ret; 479 - } 484 + struct iio_channel *chans __free(kfree) = 485 + kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL); 486 + if (!chans) 487 + return ERR_PTR(-ENOMEM); 480 488 481 489 /* for each map fill in the chans element */ 482 490 list_for_each_entry(c, &iio_map_list, l) { ··· 495 509 ret = -ENODEV; 496 510 goto error_free_chans; 497 511 } 498 - mutex_unlock(&iio_map_list_lock); 499 512 500 - return chans; 513 + return_ptr(chans); 501 514 502 515 error_free_chans: 503 516 for (i = 0; i < nummaps; i++) 504 517 iio_device_put(chans[i].indio_dev); 505 - kfree(chans); 506 - error_ret: 507 - mutex_unlock(&iio_map_list_lock); 508 - 509 518 return ERR_PTR(ret); 510 519 } 511 520 EXPORT_SYMBOL_GPL(iio_channel_get_all); ··· 571 590 int iio_read_channel_raw(struct iio_channel *chan, int *val) 572 591 { 573 592 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 574 - int ret; 575 593 576 - mutex_lock(&iio_dev_opaque->info_exist_lock); 577 - if (!chan->indio_dev->info) { 578 - ret = -ENODEV; 579 - goto err_unlock; 580 - } 594 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 595 + if (!chan->indio_dev->info) 596 + return -ENODEV; 581 597 582 - ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); 583 - err_unlock: 584 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 585 - 586 - return ret; 598 + return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); 587 599 } 588 600 EXPORT_SYMBOL_GPL(iio_read_channel_raw); 589 601 590 602 int iio_read_channel_average_raw(struct iio_channel *chan, int *val) 591 603 { 592 604 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 593 - int ret; 594 605 595 - mutex_lock(&iio_dev_opaque->info_exist_lock); 596 - if (!chan->indio_dev->info) { 597 - ret = -ENODEV; 598 - goto err_unlock; 599 - } 606 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 607 + if (!chan->indio_dev->info) 608 + return -ENODEV; 600 609 601 - ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); 602 - err_unlock: 603 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 604 - 605 - return ret; 610 + return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW); 606 611 } 607 612 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw); 608 613 ··· 675 708 int *processed, unsigned int scale) 676 709 { 677 710 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 678 - int ret; 679 711 680 - mutex_lock(&iio_dev_opaque->info_exist_lock); 681 - if (!chan->indio_dev->info) { 682 - ret = -ENODEV; 683 - goto err_unlock; 684 - } 712 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 713 + if (!chan->indio_dev->info) 714 + return -ENODEV; 685 715 686 - ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed, 687 - scale); 688 - err_unlock: 689 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 690 - 691 - return ret; 716 + return iio_convert_raw_to_processed_unlocked(chan, raw, processed, 717 + scale); 692 718 } 693 719 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed); 694 720 ··· 689 729 enum iio_chan_info_enum attribute) 690 730 { 691 731 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 692 - int ret; 693 732 694 - mutex_lock(&iio_dev_opaque->info_exist_lock); 695 - if (!chan->indio_dev->info) { 696 - ret = -ENODEV; 697 - goto err_unlock; 698 - } 733 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 734 + if (!chan->indio_dev->info) 735 + return -ENODEV; 699 736 700 - ret = iio_channel_read(chan, val, val2, attribute); 701 - err_unlock: 702 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 703 - 704 - return ret; 737 + return iio_channel_read(chan, val, val2, attribute); 705 738 } 706 739 EXPORT_SYMBOL_GPL(iio_read_channel_attribute); 707 740 ··· 710 757 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 711 758 int ret; 712 759 713 - mutex_lock(&iio_dev_opaque->info_exist_lock); 714 - if (!chan->indio_dev->info) { 715 - ret = -ENODEV; 716 - goto err_unlock; 717 - } 760 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 761 + if (!chan->indio_dev->info) 762 + return -ENODEV; 718 763 719 764 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) { 720 765 ret = iio_channel_read(chan, val, NULL, 721 766 IIO_CHAN_INFO_PROCESSED); 722 767 if (ret < 0) 723 - goto err_unlock; 768 + return ret; 724 769 *val *= scale; 770 + 771 + return 0; 725 772 } else { 726 773 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW); 727 774 if (ret < 0) 728 - goto err_unlock; 729 - ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 730 - scale); 775 + return ret; 776 + 777 + return iio_convert_raw_to_processed_unlocked(chan, *val, val, 778 + scale); 731 779 } 732 - 733 - err_unlock: 734 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 735 - 736 - return ret; 737 780 } 738 781 EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale); 739 782 ··· 762 813 enum iio_chan_info_enum attribute) 763 814 { 764 815 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 765 - int ret; 766 816 767 - mutex_lock(&iio_dev_opaque->info_exist_lock); 768 - if (!chan->indio_dev->info) { 769 - ret = -ENODEV; 770 - goto err_unlock; 771 - } 817 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 818 + if (!chan->indio_dev->info) 819 + return -ENODEV; 772 820 773 - ret = iio_channel_read_avail(chan, vals, type, length, attribute); 774 - err_unlock: 775 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 776 - 777 - return ret; 821 + return iio_channel_read_avail(chan, vals, type, length, attribute); 778 822 } 779 823 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute); 780 824 ··· 834 892 int iio_read_max_channel_raw(struct iio_channel *chan, int *val) 835 893 { 836 894 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 837 - int ret; 838 895 int type; 839 896 840 - mutex_lock(&iio_dev_opaque->info_exist_lock); 841 - if (!chan->indio_dev->info) { 842 - ret = -ENODEV; 843 - goto err_unlock; 844 - } 897 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 898 + if (!chan->indio_dev->info) 899 + return -ENODEV; 845 900 846 - ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); 847 - err_unlock: 848 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 849 - 850 - return ret; 901 + return iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); 851 902 } 852 903 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw); 853 904 ··· 890 955 int iio_read_min_channel_raw(struct iio_channel *chan, int *val) 891 956 { 892 957 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 893 - int ret; 894 958 int type; 895 959 896 - mutex_lock(&iio_dev_opaque->info_exist_lock); 897 - if (!chan->indio_dev->info) { 898 - ret = -ENODEV; 899 - goto err_unlock; 900 - } 960 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 961 + if (!chan->indio_dev->info) 962 + return -ENODEV; 901 963 902 - ret = iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); 903 - err_unlock: 904 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 905 - 906 - return ret; 964 + return iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW); 907 965 } 908 966 EXPORT_SYMBOL_GPL(iio_read_min_channel_raw); 909 967 910 968 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type) 911 969 { 912 970 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 913 - int ret = 0; 914 - /* Need to verify underlying driver has not gone away */ 915 971 916 - mutex_lock(&iio_dev_opaque->info_exist_lock); 917 - if (!chan->indio_dev->info) { 918 - ret = -ENODEV; 919 - goto err_unlock; 920 - } 972 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 973 + if (!chan->indio_dev->info) 974 + return -ENODEV; 921 975 922 976 *type = chan->channel->type; 923 - err_unlock: 924 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 925 977 926 - return ret; 978 + return 0; 927 979 } 928 980 EXPORT_SYMBOL_GPL(iio_get_channel_type); 929 981 ··· 925 1003 enum iio_chan_info_enum attribute) 926 1004 { 927 1005 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev); 928 - int ret; 929 1006 930 - mutex_lock(&iio_dev_opaque->info_exist_lock); 931 - if (!chan->indio_dev->info) { 932 - ret = -ENODEV; 933 - goto err_unlock; 934 - } 1007 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 1008 + if (!chan->indio_dev->info) 1009 + return -ENODEV; 935 1010 936 - ret = iio_channel_write(chan, val, val2, attribute); 937 - err_unlock: 938 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 939 - 940 - return ret; 1011 + return iio_channel_write(chan, val, val2, attribute); 941 1012 } 942 1013 EXPORT_SYMBOL_GPL(iio_write_channel_attribute); 943 1014
+12
drivers/iio/light/Kconfig
··· 73 73 To compile this driver as a module, choose M here: the 74 74 module will be called apds9300. 75 75 76 + config APDS9306 77 + tristate "Avago APDS9306 Ambient Light Sensor" 78 + depends on I2C 79 + select REGMAP_I2C 80 + select IIO_GTS_HELPER 81 + help 82 + If you say Y or M here, you get support for Avago APDS9306 83 + Ambient Light Sensor. 84 + 85 + If built as a dynamically linked module, it will be called 86 + apds9306. 87 + 76 88 config APDS9960 77 89 tristate "Avago APDS9960 gesture/RGB/ALS/proximity sensor" 78 90 select REGMAP_I2C
+1
drivers/iio/light/Makefile
··· 10 10 obj-$(CONFIG_AL3010) += al3010.o 11 11 obj-$(CONFIG_AL3320A) += al3320a.o 12 12 obj-$(CONFIG_APDS9300) += apds9300.o 13 + obj-$(CONFIG_APDS9306) += apds9306.o 13 14 obj-$(CONFIG_APDS9960) += apds9960.o 14 15 obj-$(CONFIG_AS73211) += as73211.o 15 16 obj-$(CONFIG_BH1750) += bh1750.o
+1361
drivers/iio/light/apds9306.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * APDS-9306/APDS-9306-065 Ambient Light Sensor 4 + * I2C Address: 0x52 5 + * Datasheet: https://docs.broadcom.com/doc/AV02-4755EN 6 + * 7 + * Copyright (C) 2024 Subhajit Ghosh <subhajit.ghosh@tweaklogic.com> 8 + */ 9 + 10 + #include <linux/bits.h> 11 + #include <linux/cleanup.h> 12 + #include <linux/delay.h> 13 + #include <linux/err.h> 14 + #include <linux/i2c.h> 15 + #include <linux/interrupt.h> 16 + #include <linux/minmax.h> 17 + #include <linux/module.h> 18 + #include <linux/mutex.h> 19 + #include <linux/pm.h> 20 + #include <linux/pm_runtime.h> 21 + #include <linux/regmap.h> 22 + #include <linux/regulator/consumer.h> 23 + #include <linux/types.h> 24 + #include <linux/units.h> 25 + 26 + #include <linux/iio/iio.h> 27 + #include <linux/iio/iio-gts-helper.h> 28 + #include <linux/iio/events.h> 29 + #include <linux/iio/sysfs.h> 30 + 31 + #include <asm/unaligned.h> 32 + 33 + #define APDS9306_MAIN_CTRL_REG 0x00 34 + #define APDS9306_ALS_MEAS_RATE_REG 0x04 35 + #define APDS9306_ALS_GAIN_REG 0x05 36 + #define APDS9306_PART_ID_REG 0x06 37 + #define APDS9306_MAIN_STATUS_REG 0x07 38 + #define APDS9306_CLEAR_DATA_0_REG 0x0A 39 + #define APDS9306_CLEAR_DATA_1_REG 0x0B 40 + #define APDS9306_CLEAR_DATA_2_REG 0x0C 41 + #define APDS9306_ALS_DATA_0_REG 0x0D 42 + #define APDS9306_ALS_DATA_1_REG 0x0E 43 + #define APDS9306_ALS_DATA_2_REG 0x0F 44 + #define APDS9306_INT_CFG_REG 0x19 45 + #define APDS9306_INT_PERSISTENCE_REG 0x1A 46 + #define APDS9306_ALS_THRES_UP_0_REG 0x21 47 + #define APDS9306_ALS_THRES_UP_1_REG 0x22 48 + #define APDS9306_ALS_THRES_UP_2_REG 0x23 49 + #define APDS9306_ALS_THRES_LOW_0_REG 0x24 50 + #define APDS9306_ALS_THRES_LOW_1_REG 0x25 51 + #define APDS9306_ALS_THRES_LOW_2_REG 0x26 52 + #define APDS9306_ALS_THRES_VAR_REG 0x27 53 + 54 + #define APDS9306_ALS_INT_STAT_MASK BIT(4) 55 + #define APDS9306_ALS_DATA_STAT_MASK BIT(3) 56 + 57 + #define APDS9306_ALS_THRES_VAL_MAX (BIT(20) - 1) 58 + #define APDS9306_ALS_THRES_VAR_VAL_MAX (BIT(3) - 1) 59 + #define APDS9306_ALS_PERSIST_VAL_MAX (BIT(4) - 1) 60 + #define APDS9306_ALS_READ_DATA_DELAY_US (20 * USEC_PER_MSEC) 61 + #define APDS9306_NUM_REPEAT_RATES 7 62 + #define APDS9306_INT_SRC_CLEAR 0 63 + #define APDS9306_INT_SRC_ALS 1 64 + #define APDS9306_SAMP_FREQ_10HZ 0 65 + 66 + /** 67 + * struct part_id_gts_multiplier - Part no. and corresponding gts multiplier 68 + * 69 + * GTS (Gain Time Scale) are helper functions for Light sensors which along 70 + * with hardware gains also has gains associated with Integration times. 71 + * 72 + * There are two variants of the device with slightly different characteristics, 73 + * they have same ADC count for different Lux levels as mentioned in the 74 + * datasheet. This multiplier array is used to store the derived Lux per count 75 + * value for the two variants to be used by the GTS helper functions. 76 + * 77 + * @part_id: Part ID of the device 78 + * @max_scale_int: Multiplier for iio_init_iio_gts() 79 + * @max_scale_nano: Multiplier for iio_init_iio_gts() 80 + */ 81 + struct part_id_gts_multiplier { 82 + int part_id; 83 + int max_scale_int; 84 + int max_scale_nano; 85 + }; 86 + 87 + /* 88 + * As per the datasheet, at HW Gain = 3x, Integration time 100mS (32x), 89 + * typical 2000 ADC counts are observed for 49.8 uW per sq cm (340.134 lux) 90 + * for apds9306 and 43 uW per sq cm (293.69 lux) for apds9306-065. 91 + * Assuming lux per count is linear across all integration time ranges. 92 + * 93 + * Lux = (raw + offset) * scale; offset can be any value by userspace. 94 + * HG = Hardware Gain; ITG = Gain by changing integration time. 95 + * Scale table by IIO GTS Helpers = (1 / HG) * (1 / ITG) * Multiplier. 96 + * 97 + * The Lux values provided in the datasheet are at ITG=32x and HG=3x, 98 + * at typical 2000 count for both variants of the device. 99 + * 100 + * Lux per ADC count at 3x and 32x for apds9306 = 340.134 / 2000 101 + * Lux per ADC count at 3x and 32x for apds9306-065 = 293.69 / 2000 102 + * 103 + * The Multiplier for the scale table provided to userspace: 104 + * IIO GTS scale Multiplier for apds9306 = (340.134 / 2000) * 32 * 3 = 16.326432 105 + * and for apds9306-065 = (293.69 / 2000) * 32 * 3 = 14.09712 106 + */ 107 + static const struct part_id_gts_multiplier apds9306_gts_mul[] = { 108 + { 109 + .part_id = 0xB1, 110 + .max_scale_int = 16, 111 + .max_scale_nano = 3264320, 112 + }, { 113 + .part_id = 0xB3, 114 + .max_scale_int = 14, 115 + .max_scale_nano = 9712000, 116 + }, 117 + }; 118 + 119 + static const int apds9306_repeat_rate_freq[APDS9306_NUM_REPEAT_RATES][2] = { 120 + { 40, 0 }, 121 + { 20, 0 }, 122 + { 10, 0 }, 123 + { 5, 0 }, 124 + { 2, 0 }, 125 + { 1, 0 }, 126 + { 0, 500000 }, 127 + }; 128 + 129 + static const int apds9306_repeat_rate_period[APDS9306_NUM_REPEAT_RATES] = { 130 + 25000, 50000, 100000, 200000, 500000, 1000000, 2000000, 131 + }; 132 + 133 + /** 134 + * struct apds9306_regfields - apds9306 regmap fields definitions 135 + * 136 + * @sw_reset: Software reset regfield 137 + * @en: Enable regfield 138 + * @intg_time: Resolution regfield 139 + * @repeat_rate: Measurement Rate regfield 140 + * @gain: Hardware gain regfield 141 + * @int_src: Interrupt channel regfield 142 + * @int_thresh_var_en: Interrupt variance threshold regfield 143 + * @int_en: Interrupt enable regfield 144 + * @int_persist_val: Interrupt persistence regfield 145 + * @int_thresh_var_val: Interrupt threshold variance value regfield 146 + */ 147 + struct apds9306_regfields { 148 + struct regmap_field *sw_reset; 149 + struct regmap_field *en; 150 + struct regmap_field *intg_time; 151 + struct regmap_field *repeat_rate; 152 + struct regmap_field *gain; 153 + struct regmap_field *int_src; 154 + struct regmap_field *int_thresh_var_en; 155 + struct regmap_field *int_en; 156 + struct regmap_field *int_persist_val; 157 + struct regmap_field *int_thresh_var_val; 158 + }; 159 + 160 + /** 161 + * struct apds9306_data - apds9306 private data and registers definitions 162 + * 163 + * @dev: Pointer to the device structure 164 + * @gts: IIO Gain Time Scale structure 165 + * @mutex: Lock for protecting adc reads, device settings changes where 166 + * some calculations are required before or after setting or 167 + * getting the raw settings values from regmap writes or reads 168 + * respectively. 169 + * @regmap: Regmap structure pointer 170 + * @rf: Regmap register fields structure 171 + * @nlux_per_count: Nano lux per ADC count for a particular model 172 + * @read_data_available: Flag set by IRQ handler for ADC data available 173 + */ 174 + struct apds9306_data { 175 + struct device *dev; 176 + struct iio_gts gts; 177 + 178 + struct mutex mutex; 179 + 180 + struct regmap *regmap; 181 + struct apds9306_regfields rf; 182 + 183 + int nlux_per_count; 184 + int read_data_available; 185 + }; 186 + 187 + /* 188 + * Available scales with gain 1x - 18x, timings 3.125, 25, 50, 100, 200, 400 ms 189 + * Time impacts to gain: 1x, 8x, 16x, 32x, 64x, 128x 190 + */ 191 + #define APDS9306_GSEL_1X 0x00 192 + #define APDS9306_GSEL_3X 0x01 193 + #define APDS9306_GSEL_6X 0x02 194 + #define APDS9306_GSEL_9X 0x03 195 + #define APDS9306_GSEL_18X 0x04 196 + 197 + static const struct iio_gain_sel_pair apds9306_gains[] = { 198 + GAIN_SCALE_GAIN(1, APDS9306_GSEL_1X), 199 + GAIN_SCALE_GAIN(3, APDS9306_GSEL_3X), 200 + GAIN_SCALE_GAIN(6, APDS9306_GSEL_6X), 201 + GAIN_SCALE_GAIN(9, APDS9306_GSEL_9X), 202 + GAIN_SCALE_GAIN(18, APDS9306_GSEL_18X), 203 + }; 204 + 205 + #define APDS9306_MEAS_MODE_400MS 0x00 206 + #define APDS9306_MEAS_MODE_200MS 0x01 207 + #define APDS9306_MEAS_MODE_100MS 0x02 208 + #define APDS9306_MEAS_MODE_50MS 0x03 209 + #define APDS9306_MEAS_MODE_25MS 0x04 210 + #define APDS9306_MEAS_MODE_3125US 0x05 211 + 212 + static const struct iio_itime_sel_mul apds9306_itimes[] = { 213 + GAIN_SCALE_ITIME_US(400000, APDS9306_MEAS_MODE_400MS, BIT(7)), 214 + GAIN_SCALE_ITIME_US(200000, APDS9306_MEAS_MODE_200MS, BIT(6)), 215 + GAIN_SCALE_ITIME_US(100000, APDS9306_MEAS_MODE_100MS, BIT(5)), 216 + GAIN_SCALE_ITIME_US(50000, APDS9306_MEAS_MODE_50MS, BIT(4)), 217 + GAIN_SCALE_ITIME_US(25000, APDS9306_MEAS_MODE_25MS, BIT(3)), 218 + GAIN_SCALE_ITIME_US(3125, APDS9306_MEAS_MODE_3125US, BIT(0)), 219 + }; 220 + 221 + static const struct iio_event_spec apds9306_event_spec[] = { 222 + { 223 + .type = IIO_EV_TYPE_THRESH, 224 + .dir = IIO_EV_DIR_RISING, 225 + .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE), 226 + }, { 227 + .type = IIO_EV_TYPE_THRESH, 228 + .dir = IIO_EV_DIR_FALLING, 229 + .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE), 230 + }, { 231 + .type = IIO_EV_TYPE_THRESH, 232 + .dir = IIO_EV_DIR_EITHER, 233 + .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD), 234 + .mask_separate = BIT(IIO_EV_INFO_ENABLE), 235 + }, { 236 + .type = IIO_EV_TYPE_THRESH_ADAPTIVE, 237 + .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE) | 238 + BIT(IIO_EV_INFO_ENABLE), 239 + }, 240 + }; 241 + 242 + static const struct iio_chan_spec apds9306_channels_with_events[] = { 243 + { 244 + .type = IIO_LIGHT, 245 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | 246 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 247 + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | 248 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 249 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 250 + BIT(IIO_CHAN_INFO_SCALE), 251 + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), 252 + .event_spec = apds9306_event_spec, 253 + .num_event_specs = ARRAY_SIZE(apds9306_event_spec), 254 + }, { 255 + .type = IIO_INTENSITY, 256 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | 257 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 258 + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | 259 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 260 + .channel2 = IIO_MOD_LIGHT_CLEAR, 261 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 262 + .modified = 1, 263 + .event_spec = apds9306_event_spec, 264 + .num_event_specs = ARRAY_SIZE(apds9306_event_spec), 265 + }, 266 + }; 267 + 268 + static const struct iio_chan_spec apds9306_channels_without_events[] = { 269 + { 270 + .type = IIO_LIGHT, 271 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | 272 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 273 + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | 274 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 275 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 276 + BIT(IIO_CHAN_INFO_SCALE), 277 + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), 278 + }, { 279 + .type = IIO_INTENSITY, 280 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) | 281 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 282 + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | 283 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 284 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 285 + .modified = 1, 286 + .channel2 = IIO_MOD_LIGHT_CLEAR, 287 + }, 288 + }; 289 + 290 + /* INT_PERSISTENCE available */ 291 + static IIO_CONST_ATTR(thresh_either_period_available, "[0 1 15]"); 292 + 293 + /* ALS_THRESH_VAR available */ 294 + static IIO_CONST_ATTR(thresh_adaptive_either_values_available, "[0 1 7]"); 295 + 296 + static struct attribute *apds9306_event_attributes[] = { 297 + &iio_const_attr_thresh_either_period_available.dev_attr.attr, 298 + &iio_const_attr_thresh_adaptive_either_values_available.dev_attr.attr, 299 + NULL 300 + }; 301 + 302 + static const struct attribute_group apds9306_event_attr_group = { 303 + .attrs = apds9306_event_attributes, 304 + }; 305 + 306 + static const struct regmap_range apds9306_readable_ranges[] = { 307 + regmap_reg_range(APDS9306_MAIN_CTRL_REG, APDS9306_ALS_THRES_VAR_REG) 308 + }; 309 + 310 + static const struct regmap_range apds9306_writable_ranges[] = { 311 + regmap_reg_range(APDS9306_MAIN_CTRL_REG, APDS9306_ALS_GAIN_REG), 312 + regmap_reg_range(APDS9306_INT_CFG_REG, APDS9306_ALS_THRES_VAR_REG) 313 + }; 314 + 315 + static const struct regmap_range apds9306_volatile_ranges[] = { 316 + regmap_reg_range(APDS9306_MAIN_STATUS_REG, APDS9306_MAIN_STATUS_REG), 317 + regmap_reg_range(APDS9306_CLEAR_DATA_0_REG, APDS9306_ALS_DATA_2_REG) 318 + }; 319 + 320 + static const struct regmap_range apds9306_precious_ranges[] = { 321 + regmap_reg_range(APDS9306_MAIN_STATUS_REG, APDS9306_MAIN_STATUS_REG) 322 + }; 323 + 324 + static const struct regmap_access_table apds9306_readable_table = { 325 + .yes_ranges = apds9306_readable_ranges, 326 + .n_yes_ranges = ARRAY_SIZE(apds9306_readable_ranges) 327 + }; 328 + 329 + static const struct regmap_access_table apds9306_writable_table = { 330 + .yes_ranges = apds9306_writable_ranges, 331 + .n_yes_ranges = ARRAY_SIZE(apds9306_writable_ranges) 332 + }; 333 + 334 + static const struct regmap_access_table apds9306_volatile_table = { 335 + .yes_ranges = apds9306_volatile_ranges, 336 + .n_yes_ranges = ARRAY_SIZE(apds9306_volatile_ranges) 337 + }; 338 + 339 + static const struct regmap_access_table apds9306_precious_table = { 340 + .yes_ranges = apds9306_precious_ranges, 341 + .n_yes_ranges = ARRAY_SIZE(apds9306_precious_ranges) 342 + }; 343 + 344 + static const struct regmap_config apds9306_regmap = { 345 + .name = "apds9306_regmap", 346 + .reg_bits = 8, 347 + .val_bits = 8, 348 + .rd_table = &apds9306_readable_table, 349 + .wr_table = &apds9306_writable_table, 350 + .volatile_table = &apds9306_volatile_table, 351 + .precious_table = &apds9306_precious_table, 352 + .max_register = APDS9306_ALS_THRES_VAR_REG, 353 + .cache_type = REGCACHE_RBTREE, 354 + }; 355 + 356 + static const struct reg_field apds9306_rf_sw_reset = 357 + REG_FIELD(APDS9306_MAIN_CTRL_REG, 4, 4); 358 + 359 + static const struct reg_field apds9306_rf_en = 360 + REG_FIELD(APDS9306_MAIN_CTRL_REG, 1, 1); 361 + 362 + static const struct reg_field apds9306_rf_intg_time = 363 + REG_FIELD(APDS9306_ALS_MEAS_RATE_REG, 4, 6); 364 + 365 + static const struct reg_field apds9306_rf_repeat_rate = 366 + REG_FIELD(APDS9306_ALS_MEAS_RATE_REG, 0, 2); 367 + 368 + static const struct reg_field apds9306_rf_gain = 369 + REG_FIELD(APDS9306_ALS_GAIN_REG, 0, 2); 370 + 371 + static const struct reg_field apds9306_rf_int_src = 372 + REG_FIELD(APDS9306_INT_CFG_REG, 4, 5); 373 + 374 + static const struct reg_field apds9306_rf_int_thresh_var_en = 375 + REG_FIELD(APDS9306_INT_CFG_REG, 3, 3); 376 + 377 + static const struct reg_field apds9306_rf_int_en = 378 + REG_FIELD(APDS9306_INT_CFG_REG, 2, 2); 379 + 380 + static const struct reg_field apds9306_rf_int_persist_val = 381 + REG_FIELD(APDS9306_INT_PERSISTENCE_REG, 4, 7); 382 + 383 + static const struct reg_field apds9306_rf_int_thresh_var_val = 384 + REG_FIELD(APDS9306_ALS_THRES_VAR_REG, 0, 2); 385 + 386 + static int apds9306_regfield_init(struct apds9306_data *data) 387 + { 388 + struct device *dev = data->dev; 389 + struct regmap *regmap = data->regmap; 390 + struct regmap_field *tmp; 391 + struct apds9306_regfields *rf = &data->rf; 392 + 393 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_sw_reset); 394 + if (IS_ERR(tmp)) 395 + return PTR_ERR(tmp); 396 + rf->sw_reset = tmp; 397 + 398 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_en); 399 + if (IS_ERR(tmp)) 400 + return PTR_ERR(tmp); 401 + rf->en = tmp; 402 + 403 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_intg_time); 404 + if (IS_ERR(tmp)) 405 + return PTR_ERR(tmp); 406 + rf->intg_time = tmp; 407 + 408 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_repeat_rate); 409 + if (IS_ERR(tmp)) 410 + return PTR_ERR(tmp); 411 + rf->repeat_rate = tmp; 412 + 413 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_gain); 414 + if (IS_ERR(tmp)) 415 + return PTR_ERR(tmp); 416 + rf->gain = tmp; 417 + 418 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_int_src); 419 + if (IS_ERR(tmp)) 420 + return PTR_ERR(tmp); 421 + rf->int_src = tmp; 422 + 423 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_int_thresh_var_en); 424 + if (IS_ERR(tmp)) 425 + return PTR_ERR(tmp); 426 + rf->int_thresh_var_en = tmp; 427 + 428 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_int_en); 429 + if (IS_ERR(tmp)) 430 + return PTR_ERR(tmp); 431 + rf->int_en = tmp; 432 + 433 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_int_persist_val); 434 + if (IS_ERR(tmp)) 435 + return PTR_ERR(tmp); 436 + rf->int_persist_val = tmp; 437 + 438 + tmp = devm_regmap_field_alloc(dev, regmap, apds9306_rf_int_thresh_var_val); 439 + if (IS_ERR(tmp)) 440 + return PTR_ERR(tmp); 441 + rf->int_thresh_var_val = tmp; 442 + 443 + return 0; 444 + } 445 + 446 + static int apds9306_power_state(struct apds9306_data *data, bool state) 447 + { 448 + struct apds9306_regfields *rf = &data->rf; 449 + int ret; 450 + 451 + /* Reset not included as it causes ugly I2C bus error */ 452 + if (state) { 453 + ret = regmap_field_write(rf->en, 1); 454 + if (ret) 455 + return ret; 456 + /* 5ms wake up time */ 457 + fsleep(5000); 458 + return 0; 459 + } 460 + 461 + return regmap_field_write(rf->en, 0); 462 + } 463 + 464 + static int apds9306_read_data(struct apds9306_data *data, int *val, int reg) 465 + { 466 + struct device *dev = data->dev; 467 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 468 + struct apds9306_regfields *rf = &data->rf; 469 + u64 ev_code; 470 + int ret, delay, intg_time, intg_time_idx, repeat_rate_idx, int_src; 471 + int status = 0; 472 + u8 buff[3]; 473 + 474 + ret = pm_runtime_resume_and_get(data->dev); 475 + if (ret) 476 + return ret; 477 + 478 + ret = regmap_field_read(rf->intg_time, &intg_time_idx); 479 + if (ret) 480 + return ret; 481 + 482 + ret = regmap_field_read(rf->repeat_rate, &repeat_rate_idx); 483 + if (ret) 484 + return ret; 485 + 486 + ret = regmap_field_read(rf->int_src, &int_src); 487 + if (ret) 488 + return ret; 489 + 490 + intg_time = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx); 491 + if (intg_time < 0) 492 + return intg_time; 493 + 494 + /* Whichever is greater - integration time period or sampling period. */ 495 + delay = max(intg_time, apds9306_repeat_rate_period[repeat_rate_idx]); 496 + 497 + /* 498 + * Clear stale data flag that might have been set by the interrupt 499 + * handler if it got data available flag set in the status reg. 500 + */ 501 + data->read_data_available = 0; 502 + 503 + /* 504 + * If this function runs parallel with the interrupt handler, either 505 + * this reads and clears the status registers or the interrupt handler 506 + * does. The interrupt handler sets a flag for read data available 507 + * in our private structure which we read here. 508 + */ 509 + ret = regmap_read_poll_timeout(data->regmap, APDS9306_MAIN_STATUS_REG, 510 + status, data->read_data_available || 511 + (status & (APDS9306_ALS_DATA_STAT_MASK | 512 + APDS9306_ALS_INT_STAT_MASK)), 513 + APDS9306_ALS_READ_DATA_DELAY_US, delay * 2); 514 + if (ret) 515 + return ret; 516 + 517 + /* If we reach here before the interrupt handler we push an event */ 518 + if ((status & APDS9306_ALS_INT_STAT_MASK)) { 519 + if (int_src == APDS9306_INT_SRC_ALS) 520 + ev_code = IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0, 521 + IIO_EV_TYPE_THRESH, 522 + IIO_EV_DIR_EITHER); 523 + else 524 + ev_code = IIO_MOD_EVENT_CODE(IIO_INTENSITY, 0, 525 + IIO_MOD_LIGHT_CLEAR, 526 + IIO_EV_TYPE_THRESH, 527 + IIO_EV_DIR_EITHER); 528 + 529 + iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev)); 530 + } 531 + 532 + ret = regmap_bulk_read(data->regmap, reg, buff, sizeof(buff)); 533 + if (ret) { 534 + dev_err_ratelimited(dev, "read data failed\n"); 535 + return ret; 536 + } 537 + 538 + *val = get_unaligned_le24(&buff); 539 + 540 + pm_runtime_mark_last_busy(data->dev); 541 + pm_runtime_put_autosuspend(data->dev); 542 + 543 + return 0; 544 + } 545 + 546 + static int apds9306_intg_time_get(struct apds9306_data *data, int *val2) 547 + { 548 + struct apds9306_regfields *rf = &data->rf; 549 + int ret, intg_time_idx; 550 + 551 + ret = regmap_field_read(rf->intg_time, &intg_time_idx); 552 + if (ret) 553 + return ret; 554 + 555 + ret = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx); 556 + if (ret < 0) 557 + return ret; 558 + 559 + *val2 = ret; 560 + 561 + return 0; 562 + } 563 + 564 + static int apds9306_intg_time_set(struct apds9306_data *data, int val2) 565 + { 566 + struct device *dev = data->dev; 567 + struct apds9306_regfields *rf = &data->rf; 568 + int ret, intg_old, gain_old, gain_new, gain_new_closest, intg_time_idx; 569 + int gain_idx; 570 + bool ok; 571 + 572 + if (!iio_gts_valid_time(&data->gts, val2)) { 573 + dev_err_ratelimited(dev, "Unsupported integration time %u\n", val2); 574 + return -EINVAL; 575 + } 576 + 577 + ret = regmap_field_read(rf->intg_time, &intg_time_idx); 578 + if (ret) 579 + return ret; 580 + 581 + ret = regmap_field_read(rf->gain, &gain_idx); 582 + if (ret) 583 + return ret; 584 + 585 + intg_old = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx); 586 + if (ret < 0) 587 + return ret; 588 + 589 + if (intg_old == val2) 590 + return 0; 591 + 592 + gain_old = iio_gts_find_gain_by_sel(&data->gts, gain_idx); 593 + if (gain_old < 0) 594 + return gain_old; 595 + 596 + iio_gts_find_new_gain_by_old_gain_time(&data->gts, gain_old, intg_old, 597 + val2, &gain_new); 598 + 599 + if (gain_new < 0) { 600 + dev_err_ratelimited(dev, "Unsupported gain with time\n"); 601 + return gain_new; 602 + } 603 + 604 + gain_new_closest = iio_find_closest_gain_low(&data->gts, gain_new, &ok); 605 + if (gain_new_closest < 0) { 606 + gain_new_closest = iio_gts_get_min_gain(&data->gts); 607 + if (gain_new_closest < 0) 608 + return gain_new_closest; 609 + } 610 + if (!ok) 611 + dev_dbg(dev, "Unable to find optimum gain, setting minimum"); 612 + 613 + ret = iio_gts_find_sel_by_int_time(&data->gts, val2); 614 + if (ret < 0) 615 + return ret; 616 + 617 + ret = regmap_field_write(rf->intg_time, ret); 618 + if (ret) 619 + return ret; 620 + 621 + ret = iio_gts_find_sel_by_gain(&data->gts, gain_new_closest); 622 + if (ret < 0) 623 + return ret; 624 + 625 + return regmap_field_write(rf->gain, ret); 626 + } 627 + 628 + static int apds9306_sampling_freq_get(struct apds9306_data *data, int *val, 629 + int *val2) 630 + { 631 + struct apds9306_regfields *rf = &data->rf; 632 + int ret, repeat_rate_idx; 633 + 634 + ret = regmap_field_read(rf->repeat_rate, &repeat_rate_idx); 635 + if (ret) 636 + return ret; 637 + 638 + if (repeat_rate_idx >= ARRAY_SIZE(apds9306_repeat_rate_freq)) 639 + return -EINVAL; 640 + 641 + *val = apds9306_repeat_rate_freq[repeat_rate_idx][0]; 642 + *val2 = apds9306_repeat_rate_freq[repeat_rate_idx][1]; 643 + 644 + return 0; 645 + } 646 + 647 + static int apds9306_sampling_freq_set(struct apds9306_data *data, int val, 648 + int val2) 649 + { 650 + struct apds9306_regfields *rf = &data->rf; 651 + int i; 652 + 653 + for (i = 0; i < ARRAY_SIZE(apds9306_repeat_rate_freq); i++) { 654 + if (apds9306_repeat_rate_freq[i][0] == val && 655 + apds9306_repeat_rate_freq[i][1] == val2) 656 + return regmap_field_write(rf->repeat_rate, i); 657 + } 658 + 659 + return -EINVAL; 660 + } 661 + 662 + static int apds9306_scale_get(struct apds9306_data *data, int *val, int *val2) 663 + { 664 + struct apds9306_regfields *rf = &data->rf; 665 + int gain, intg, ret, intg_time_idx, gain_idx; 666 + 667 + ret = regmap_field_read(rf->gain, &gain_idx); 668 + if (ret) 669 + return ret; 670 + 671 + ret = regmap_field_read(rf->intg_time, &intg_time_idx); 672 + if (ret) 673 + return ret; 674 + 675 + gain = iio_gts_find_gain_by_sel(&data->gts, gain_idx); 676 + if (gain < 0) 677 + return gain; 678 + 679 + intg = iio_gts_find_int_time_by_sel(&data->gts, intg_time_idx); 680 + if (intg < 0) 681 + return intg; 682 + 683 + return iio_gts_get_scale(&data->gts, gain, intg, val, val2); 684 + } 685 + 686 + static int apds9306_scale_set(struct apds9306_data *data, int val, int val2) 687 + { 688 + struct apds9306_regfields *rf = &data->rf; 689 + int i, ret, time_sel, gain_sel, intg_time_idx; 690 + 691 + ret = regmap_field_read(rf->intg_time, &intg_time_idx); 692 + if (ret) 693 + return ret; 694 + 695 + ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, 696 + intg_time_idx, val, val2, &gain_sel); 697 + if (ret) { 698 + for (i = 0; i < data->gts.num_itime; i++) { 699 + time_sel = data->gts.itime_table[i].sel; 700 + 701 + if (time_sel == intg_time_idx) 702 + continue; 703 + 704 + ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, 705 + time_sel, val, val2, &gain_sel); 706 + if (!ret) 707 + break; 708 + } 709 + if (ret) 710 + return -EINVAL; 711 + 712 + ret = regmap_field_write(rf->intg_time, time_sel); 713 + if (ret) 714 + return ret; 715 + } 716 + 717 + return regmap_field_write(rf->gain, gain_sel); 718 + } 719 + 720 + static int apds9306_event_period_get(struct apds9306_data *data, int *val) 721 + { 722 + struct apds9306_regfields *rf = &data->rf; 723 + int period, ret; 724 + 725 + ret = regmap_field_read(rf->int_persist_val, &period); 726 + if (ret) 727 + return ret; 728 + 729 + if (!in_range(period, 0, APDS9306_ALS_PERSIST_VAL_MAX)) 730 + return -EINVAL; 731 + 732 + *val = period; 733 + 734 + return ret; 735 + } 736 + 737 + static int apds9306_event_period_set(struct apds9306_data *data, int val) 738 + { 739 + struct apds9306_regfields *rf = &data->rf; 740 + 741 + if (!in_range(val, 0, APDS9306_ALS_PERSIST_VAL_MAX)) 742 + return -EINVAL; 743 + 744 + return regmap_field_write(rf->int_persist_val, val); 745 + } 746 + 747 + static int apds9306_event_thresh_get(struct apds9306_data *data, int dir, 748 + int *val) 749 + { 750 + int var, ret; 751 + u8 buff[3]; 752 + 753 + if (dir == IIO_EV_DIR_RISING) 754 + var = APDS9306_ALS_THRES_UP_0_REG; 755 + else if (dir == IIO_EV_DIR_FALLING) 756 + var = APDS9306_ALS_THRES_LOW_0_REG; 757 + else 758 + return -EINVAL; 759 + 760 + ret = regmap_bulk_read(data->regmap, var, buff, sizeof(buff)); 761 + if (ret) 762 + return ret; 763 + 764 + *val = get_unaligned_le24(&buff); 765 + 766 + return 0; 767 + } 768 + 769 + static int apds9306_event_thresh_set(struct apds9306_data *data, int dir, 770 + int val) 771 + { 772 + int var; 773 + u8 buff[3]; 774 + 775 + if (dir == IIO_EV_DIR_RISING) 776 + var = APDS9306_ALS_THRES_UP_0_REG; 777 + else if (dir == IIO_EV_DIR_FALLING) 778 + var = APDS9306_ALS_THRES_LOW_0_REG; 779 + else 780 + return -EINVAL; 781 + 782 + if (!in_range(val, 0, APDS9306_ALS_THRES_VAL_MAX)) 783 + return -EINVAL; 784 + 785 + put_unaligned_le24(val, buff); 786 + 787 + return regmap_bulk_write(data->regmap, var, buff, sizeof(buff)); 788 + } 789 + 790 + static int apds9306_event_thresh_adaptive_get(struct apds9306_data *data, int *val) 791 + { 792 + struct apds9306_regfields *rf = &data->rf; 793 + int thr_adpt, ret; 794 + 795 + ret = regmap_field_read(rf->int_thresh_var_val, &thr_adpt); 796 + if (ret) 797 + return ret; 798 + 799 + if (!in_range(thr_adpt, 0, APDS9306_ALS_THRES_VAR_VAL_MAX)) 800 + return -EINVAL; 801 + 802 + *val = thr_adpt; 803 + 804 + return ret; 805 + } 806 + 807 + static int apds9306_event_thresh_adaptive_set(struct apds9306_data *data, int val) 808 + { 809 + struct apds9306_regfields *rf = &data->rf; 810 + 811 + if (!in_range(val, 0, APDS9306_ALS_THRES_VAR_VAL_MAX)) 812 + return -EINVAL; 813 + 814 + return regmap_field_write(rf->int_thresh_var_val, val); 815 + } 816 + 817 + static int apds9306_read_raw(struct iio_dev *indio_dev, 818 + struct iio_chan_spec const *chan, int *val, 819 + int *val2, long mask) 820 + { 821 + struct apds9306_data *data = iio_priv(indio_dev); 822 + int ret, reg; 823 + 824 + switch (mask) { 825 + case IIO_CHAN_INFO_RAW: 826 + if (chan->channel2 == IIO_MOD_LIGHT_CLEAR) 827 + reg = APDS9306_CLEAR_DATA_0_REG; 828 + else 829 + reg = APDS9306_ALS_DATA_0_REG; 830 + /* 831 + * Changing device parameters during adc operation, resets 832 + * the ADC which has to avoided. 833 + */ 834 + ret = iio_device_claim_direct_mode(indio_dev); 835 + if (ret) 836 + return ret; 837 + ret = apds9306_read_data(data, val, reg); 838 + iio_device_release_direct_mode(indio_dev); 839 + if (ret) 840 + return ret; 841 + 842 + return IIO_VAL_INT; 843 + case IIO_CHAN_INFO_INT_TIME: 844 + ret = apds9306_intg_time_get(data, val2); 845 + if (ret) 846 + return ret; 847 + *val = 0; 848 + 849 + return IIO_VAL_INT_PLUS_MICRO; 850 + case IIO_CHAN_INFO_SAMP_FREQ: 851 + ret = apds9306_sampling_freq_get(data, val, val2); 852 + if (ret) 853 + return ret; 854 + 855 + return IIO_VAL_INT_PLUS_MICRO; 856 + case IIO_CHAN_INFO_SCALE: 857 + ret = apds9306_scale_get(data, val, val2); 858 + if (ret) 859 + return ret; 860 + 861 + return IIO_VAL_INT_PLUS_NANO; 862 + default: 863 + return -EINVAL; 864 + } 865 + }; 866 + 867 + static int apds9306_read_avail(struct iio_dev *indio_dev, 868 + struct iio_chan_spec const *chan, 869 + const int **vals, int *type, int *length, 870 + long mask) 871 + { 872 + struct apds9306_data *data = iio_priv(indio_dev); 873 + 874 + switch (mask) { 875 + case IIO_CHAN_INFO_INT_TIME: 876 + return iio_gts_avail_times(&data->gts, vals, type, length); 877 + case IIO_CHAN_INFO_SCALE: 878 + return iio_gts_all_avail_scales(&data->gts, vals, type, length); 879 + case IIO_CHAN_INFO_SAMP_FREQ: 880 + *length = ARRAY_SIZE(apds9306_repeat_rate_freq) * 2; 881 + *vals = (const int *)apds9306_repeat_rate_freq; 882 + *type = IIO_VAL_INT_PLUS_MICRO; 883 + 884 + return IIO_AVAIL_LIST; 885 + default: 886 + return -EINVAL; 887 + } 888 + } 889 + 890 + static int apds9306_write_raw_get_fmt(struct iio_dev *indio_dev, 891 + struct iio_chan_spec const *chan, 892 + long mask) 893 + { 894 + switch (mask) { 895 + case IIO_CHAN_INFO_SCALE: 896 + return IIO_VAL_INT_PLUS_NANO; 897 + case IIO_CHAN_INFO_INT_TIME: 898 + return IIO_VAL_INT_PLUS_MICRO; 899 + case IIO_CHAN_INFO_SAMP_FREQ: 900 + return IIO_VAL_INT_PLUS_MICRO; 901 + default: 902 + return -EINVAL; 903 + } 904 + } 905 + 906 + static int apds9306_write_raw(struct iio_dev *indio_dev, 907 + struct iio_chan_spec const *chan, int val, 908 + int val2, long mask) 909 + { 910 + struct apds9306_data *data = iio_priv(indio_dev); 911 + 912 + guard(mutex)(&data->mutex); 913 + 914 + switch (mask) { 915 + case IIO_CHAN_INFO_INT_TIME: 916 + if (val) 917 + return -EINVAL; 918 + return apds9306_intg_time_set(data, val2); 919 + case IIO_CHAN_INFO_SCALE: 920 + return apds9306_scale_set(data, val, val2); 921 + case IIO_CHAN_INFO_SAMP_FREQ: 922 + return apds9306_sampling_freq_set(data, val, val2); 923 + default: 924 + return -EINVAL; 925 + } 926 + } 927 + 928 + static irqreturn_t apds9306_irq_handler(int irq, void *priv) 929 + { 930 + struct iio_dev *indio_dev = priv; 931 + struct apds9306_data *data = iio_priv(indio_dev); 932 + struct apds9306_regfields *rf = &data->rf; 933 + u64 ev_code; 934 + int ret, status, int_src; 935 + 936 + /* 937 + * The interrupt line is released and the interrupt flag is 938 + * cleared as a result of reading the status register. All the 939 + * status flags are cleared as a result of this read. 940 + */ 941 + ret = regmap_read(data->regmap, APDS9306_MAIN_STATUS_REG, &status); 942 + if (ret < 0) { 943 + dev_err_ratelimited(data->dev, "status reg read failed\n"); 944 + return IRQ_HANDLED; 945 + } 946 + 947 + ret = regmap_field_read(rf->int_src, &int_src); 948 + if (ret) 949 + return ret; 950 + 951 + if ((status & APDS9306_ALS_INT_STAT_MASK)) { 952 + if (int_src == APDS9306_INT_SRC_ALS) 953 + ev_code = IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0, 954 + IIO_EV_TYPE_THRESH, 955 + IIO_EV_DIR_EITHER); 956 + else 957 + ev_code = IIO_MOD_EVENT_CODE(IIO_INTENSITY, 0, 958 + IIO_MOD_LIGHT_CLEAR, 959 + IIO_EV_TYPE_THRESH, 960 + IIO_EV_DIR_EITHER); 961 + 962 + iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev)); 963 + } 964 + 965 + /* 966 + * If a one-shot read through sysfs is underway at the same time 967 + * as this interrupt handler is executing and a read data available 968 + * flag was set, this flag is set to inform read_poll_timeout() 969 + * to exit. 970 + */ 971 + if ((status & APDS9306_ALS_DATA_STAT_MASK)) 972 + data->read_data_available = 1; 973 + 974 + return IRQ_HANDLED; 975 + } 976 + 977 + static int apds9306_read_event(struct iio_dev *indio_dev, 978 + const struct iio_chan_spec *chan, 979 + enum iio_event_type type, 980 + enum iio_event_direction dir, 981 + enum iio_event_info info, 982 + int *val, int *val2) 983 + { 984 + struct apds9306_data *data = iio_priv(indio_dev); 985 + int ret; 986 + 987 + switch (type) { 988 + case IIO_EV_TYPE_THRESH: 989 + if (dir == IIO_EV_DIR_EITHER && info == IIO_EV_INFO_PERIOD) 990 + ret = apds9306_event_period_get(data, val); 991 + else 992 + ret = apds9306_event_thresh_get(data, dir, val); 993 + if (ret) 994 + return ret; 995 + 996 + return IIO_VAL_INT; 997 + case IIO_EV_TYPE_THRESH_ADAPTIVE: 998 + ret = apds9306_event_thresh_adaptive_get(data, val); 999 + if (ret) 1000 + return ret; 1001 + 1002 + return IIO_VAL_INT; 1003 + default: 1004 + return -EINVAL; 1005 + } 1006 + } 1007 + 1008 + static int apds9306_write_event(struct iio_dev *indio_dev, 1009 + const struct iio_chan_spec *chan, 1010 + enum iio_event_type type, 1011 + enum iio_event_direction dir, 1012 + enum iio_event_info info, 1013 + int val, int val2) 1014 + { 1015 + struct apds9306_data *data = iio_priv(indio_dev); 1016 + 1017 + switch (type) { 1018 + case IIO_EV_TYPE_THRESH: 1019 + if (dir == IIO_EV_DIR_EITHER && info == IIO_EV_INFO_PERIOD) 1020 + return apds9306_event_period_set(data, val); 1021 + 1022 + return apds9306_event_thresh_set(data, dir, val); 1023 + case IIO_EV_TYPE_THRESH_ADAPTIVE: 1024 + return apds9306_event_thresh_adaptive_set(data, val); 1025 + default: 1026 + return -EINVAL; 1027 + } 1028 + } 1029 + 1030 + static int apds9306_read_event_config(struct iio_dev *indio_dev, 1031 + const struct iio_chan_spec *chan, 1032 + enum iio_event_type type, 1033 + enum iio_event_direction dir) 1034 + { 1035 + struct apds9306_data *data = iio_priv(indio_dev); 1036 + struct apds9306_regfields *rf = &data->rf; 1037 + int int_en, int_src, ret; 1038 + 1039 + switch (type) { 1040 + case IIO_EV_TYPE_THRESH: { 1041 + guard(mutex)(&data->mutex); 1042 + 1043 + ret = regmap_field_read(rf->int_src, &int_src); 1044 + if (ret) 1045 + return ret; 1046 + 1047 + ret = regmap_field_read(rf->int_en, &int_en); 1048 + if (ret) 1049 + return ret; 1050 + 1051 + if (chan->type == IIO_LIGHT) 1052 + return int_en && (int_src == APDS9306_INT_SRC_ALS); 1053 + 1054 + if (chan->type == IIO_INTENSITY) 1055 + return int_en && (int_src == APDS9306_INT_SRC_CLEAR); 1056 + 1057 + return -EINVAL; 1058 + } 1059 + case IIO_EV_TYPE_THRESH_ADAPTIVE: 1060 + ret = regmap_field_read(rf->int_thresh_var_en, &int_en); 1061 + if (ret) 1062 + return ret; 1063 + 1064 + return int_en; 1065 + default: 1066 + return -EINVAL; 1067 + } 1068 + } 1069 + 1070 + static int apds9306_write_event_config(struct iio_dev *indio_dev, 1071 + const struct iio_chan_spec *chan, 1072 + enum iio_event_type type, 1073 + enum iio_event_direction dir, 1074 + int state) 1075 + { 1076 + struct apds9306_data *data = iio_priv(indio_dev); 1077 + struct apds9306_regfields *rf = &data->rf; 1078 + int ret, enabled; 1079 + 1080 + switch (type) { 1081 + case IIO_EV_TYPE_THRESH: { 1082 + guard(mutex)(&data->mutex); 1083 + 1084 + ret = regmap_field_read(rf->int_en, &enabled); 1085 + if (ret) 1086 + return ret; 1087 + 1088 + /* 1089 + * If interrupt is enabled, the channel is set before enabling 1090 + * the interrupt. In case of disable, no need to switch 1091 + * channels. In case of different channel is selected while 1092 + * interrupt in on, just change the channel. 1093 + */ 1094 + if (state) { 1095 + if (chan->type == IIO_LIGHT) 1096 + ret = regmap_field_write(rf->int_src, 1); 1097 + else if (chan->type == IIO_INTENSITY) 1098 + ret = regmap_field_write(rf->int_src, 0); 1099 + else 1100 + return -EINVAL; 1101 + 1102 + if (ret) 1103 + return ret; 1104 + 1105 + if (enabled) 1106 + return 0; 1107 + 1108 + ret = regmap_field_write(rf->int_en, 1); 1109 + if (ret) 1110 + return ret; 1111 + 1112 + return pm_runtime_resume_and_get(data->dev); 1113 + } else { 1114 + if (!enabled) 1115 + return 0; 1116 + 1117 + ret = regmap_field_write(rf->int_en, 0); 1118 + if (ret) 1119 + return ret; 1120 + 1121 + pm_runtime_mark_last_busy(data->dev); 1122 + pm_runtime_put_autosuspend(data->dev); 1123 + 1124 + return 0; 1125 + } 1126 + } 1127 + case IIO_EV_TYPE_THRESH_ADAPTIVE: 1128 + if (state) 1129 + return regmap_field_write(rf->int_thresh_var_en, 1); 1130 + else 1131 + return regmap_field_write(rf->int_thresh_var_en, 0); 1132 + default: 1133 + return -EINVAL; 1134 + } 1135 + } 1136 + 1137 + static const struct iio_info apds9306_info_no_events = { 1138 + .read_avail = apds9306_read_avail, 1139 + .read_raw = apds9306_read_raw, 1140 + .write_raw = apds9306_write_raw, 1141 + .write_raw_get_fmt = apds9306_write_raw_get_fmt, 1142 + }; 1143 + 1144 + static const struct iio_info apds9306_info = { 1145 + .read_avail = apds9306_read_avail, 1146 + .read_raw = apds9306_read_raw, 1147 + .write_raw = apds9306_write_raw, 1148 + .write_raw_get_fmt = apds9306_write_raw_get_fmt, 1149 + .read_event_value = apds9306_read_event, 1150 + .write_event_value = apds9306_write_event, 1151 + .read_event_config = apds9306_read_event_config, 1152 + .write_event_config = apds9306_write_event_config, 1153 + .event_attrs = &apds9306_event_attr_group, 1154 + }; 1155 + 1156 + static int apds9306_init_iio_gts(struct apds9306_data *data) 1157 + { 1158 + int i, ret, part_id; 1159 + 1160 + ret = regmap_read(data->regmap, APDS9306_PART_ID_REG, &part_id); 1161 + if (ret) 1162 + return ret; 1163 + 1164 + for (i = 0; i < ARRAY_SIZE(apds9306_gts_mul); i++) 1165 + if (part_id == apds9306_gts_mul[i].part_id) 1166 + break; 1167 + 1168 + if (i == ARRAY_SIZE(apds9306_gts_mul)) 1169 + return -ENOENT; 1170 + 1171 + return devm_iio_init_iio_gts(data->dev, 1172 + apds9306_gts_mul[i].max_scale_int, 1173 + apds9306_gts_mul[i].max_scale_nano, 1174 + apds9306_gains, ARRAY_SIZE(apds9306_gains), 1175 + apds9306_itimes, ARRAY_SIZE(apds9306_itimes), 1176 + &data->gts); 1177 + } 1178 + 1179 + static void apds9306_powerdown(void *ptr) 1180 + { 1181 + struct apds9306_data *data = (struct apds9306_data *)ptr; 1182 + struct apds9306_regfields *rf = &data->rf; 1183 + int ret; 1184 + 1185 + ret = regmap_field_write(rf->int_thresh_var_en, 0); 1186 + if (ret) 1187 + return; 1188 + 1189 + ret = regmap_field_write(rf->int_en, 0); 1190 + if (ret) 1191 + return; 1192 + 1193 + apds9306_power_state(data, false); 1194 + } 1195 + 1196 + static int apds9306_device_init(struct apds9306_data *data) 1197 + { 1198 + struct apds9306_regfields *rf = &data->rf; 1199 + int ret; 1200 + 1201 + ret = apds9306_init_iio_gts(data); 1202 + if (ret) 1203 + return ret; 1204 + 1205 + ret = regmap_field_write(rf->intg_time, APDS9306_MEAS_MODE_100MS); 1206 + if (ret) 1207 + return ret; 1208 + 1209 + ret = regmap_field_write(rf->repeat_rate, APDS9306_SAMP_FREQ_10HZ); 1210 + if (ret) 1211 + return ret; 1212 + 1213 + ret = regmap_field_write(rf->gain, APDS9306_GSEL_3X); 1214 + if (ret) 1215 + return ret; 1216 + 1217 + ret = regmap_field_write(rf->int_src, APDS9306_INT_SRC_ALS); 1218 + if (ret) 1219 + return ret; 1220 + 1221 + ret = regmap_field_write(rf->int_en, 0); 1222 + if (ret) 1223 + return ret; 1224 + 1225 + return regmap_field_write(rf->int_thresh_var_en, 0); 1226 + } 1227 + 1228 + static int apds9306_pm_init(struct apds9306_data *data) 1229 + { 1230 + struct device *dev = data->dev; 1231 + int ret; 1232 + 1233 + ret = apds9306_power_state(data, true); 1234 + if (ret) 1235 + return ret; 1236 + 1237 + ret = pm_runtime_set_active(dev); 1238 + if (ret) 1239 + return ret; 1240 + 1241 + ret = devm_pm_runtime_enable(dev); 1242 + if (ret) 1243 + return ret; 1244 + 1245 + pm_runtime_set_autosuspend_delay(dev, 5000); 1246 + pm_runtime_use_autosuspend(dev); 1247 + pm_runtime_get(dev); 1248 + 1249 + return 0; 1250 + } 1251 + 1252 + static int apds9306_probe(struct i2c_client *client) 1253 + { 1254 + struct device *dev = &client->dev; 1255 + struct apds9306_data *data; 1256 + struct iio_dev *indio_dev; 1257 + int ret; 1258 + 1259 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 1260 + if (!indio_dev) 1261 + return -ENOMEM; 1262 + 1263 + data = iio_priv(indio_dev); 1264 + 1265 + mutex_init(&data->mutex); 1266 + 1267 + data->regmap = devm_regmap_init_i2c(client, &apds9306_regmap); 1268 + if (IS_ERR(data->regmap)) 1269 + return dev_err_probe(dev, PTR_ERR(data->regmap), 1270 + "regmap initialization failed\n"); 1271 + 1272 + data->dev = dev; 1273 + i2c_set_clientdata(client, indio_dev); 1274 + 1275 + ret = apds9306_regfield_init(data); 1276 + if (ret) 1277 + return dev_err_probe(dev, ret, "regfield initialization failed\n"); 1278 + 1279 + ret = devm_regulator_get_enable(dev, "vdd"); 1280 + if (ret) 1281 + return dev_err_probe(dev, ret, "Failed to enable regulator\n"); 1282 + 1283 + indio_dev->name = "apds9306"; 1284 + indio_dev->modes = INDIO_DIRECT_MODE; 1285 + if (client->irq) { 1286 + indio_dev->info = &apds9306_info; 1287 + indio_dev->channels = apds9306_channels_with_events; 1288 + indio_dev->num_channels = ARRAY_SIZE(apds9306_channels_with_events); 1289 + ret = devm_request_threaded_irq(dev, client->irq, NULL, 1290 + apds9306_irq_handler, IRQF_ONESHOT, 1291 + "apds9306_event", indio_dev); 1292 + if (ret) 1293 + return dev_err_probe(dev, ret, 1294 + "failed to assign interrupt.\n"); 1295 + } else { 1296 + indio_dev->info = &apds9306_info_no_events; 1297 + indio_dev->channels = apds9306_channels_without_events; 1298 + indio_dev->num_channels = 1299 + ARRAY_SIZE(apds9306_channels_without_events); 1300 + } 1301 + 1302 + ret = apds9306_pm_init(data); 1303 + if (ret) 1304 + return dev_err_probe(dev, ret, "failed pm init\n"); 1305 + 1306 + ret = apds9306_device_init(data); 1307 + if (ret) 1308 + return dev_err_probe(dev, ret, "failed to init device\n"); 1309 + 1310 + ret = devm_add_action_or_reset(dev, apds9306_powerdown, data); 1311 + if (ret) 1312 + return dev_err_probe(dev, ret, "failed to add action or reset\n"); 1313 + 1314 + ret = devm_iio_device_register(dev, indio_dev); 1315 + if (ret) 1316 + return dev_err_probe(dev, ret, "failed iio device registration\n"); 1317 + 1318 + pm_runtime_put_autosuspend(dev); 1319 + 1320 + return 0; 1321 + } 1322 + 1323 + static int apds9306_runtime_suspend(struct device *dev) 1324 + { 1325 + struct apds9306_data *data = iio_priv(dev_get_drvdata(dev)); 1326 + 1327 + return apds9306_power_state(data, false); 1328 + } 1329 + 1330 + static int apds9306_runtime_resume(struct device *dev) 1331 + { 1332 + struct apds9306_data *data = iio_priv(dev_get_drvdata(dev)); 1333 + 1334 + return apds9306_power_state(data, true); 1335 + } 1336 + 1337 + static DEFINE_RUNTIME_DEV_PM_OPS(apds9306_pm_ops, 1338 + apds9306_runtime_suspend, 1339 + apds9306_runtime_resume, 1340 + NULL); 1341 + 1342 + static const struct of_device_id apds9306_of_match[] = { 1343 + { .compatible = "avago,apds9306" }, 1344 + { } 1345 + }; 1346 + MODULE_DEVICE_TABLE(of, apds9306_of_match); 1347 + 1348 + static struct i2c_driver apds9306_driver = { 1349 + .driver = { 1350 + .name = "apds9306", 1351 + .pm = pm_ptr(&apds9306_pm_ops), 1352 + .of_match_table = apds9306_of_match, 1353 + }, 1354 + .probe = apds9306_probe, 1355 + }; 1356 + module_i2c_driver(apds9306_driver); 1357 + 1358 + MODULE_AUTHOR("Subhajit Ghosh <subhajit.ghosh@tweaklogic.com>"); 1359 + MODULE_DESCRIPTION("APDS9306 Ambient Light Sensor driver"); 1360 + MODULE_LICENSE("GPL"); 1361 + MODULE_IMPORT_NS(IIO_GTS_HELPER);
+1 -1
drivers/iio/light/st_uvis25_core.c
··· 291 291 if (!iio_dev) 292 292 return -ENOMEM; 293 293 294 - dev_set_drvdata(dev, (void *)iio_dev); 294 + dev_set_drvdata(dev, iio_dev); 295 295 296 296 hw = iio_priv(iio_dev); 297 297 hw->irq = irq;
+8 -8
drivers/iio/pressure/bmp280-core.c
··· 27 27 28 28 #include <linux/bitops.h> 29 29 #include <linux/bitfield.h> 30 - #include <linux/device.h> 31 - #include <linux/module.h> 32 - #include <linux/nvmem-provider.h> 33 - #include <linux/regmap.h> 30 + #include <linux/completion.h> 34 31 #include <linux/delay.h> 35 - #include <linux/iio/iio.h> 36 - #include <linux/iio/sysfs.h> 32 + #include <linux/device.h> 37 33 #include <linux/gpio/consumer.h> 38 - #include <linux/regulator/consumer.h> 39 34 #include <linux/interrupt.h> 40 35 #include <linux/irq.h> /* For irq_get_irq_data() */ 41 - #include <linux/completion.h> 36 + #include <linux/module.h> 37 + #include <linux/nvmem-provider.h> 42 38 #include <linux/pm_runtime.h> 43 39 #include <linux/random.h> 40 + #include <linux/regmap.h> 41 + #include <linux/regulator/consumer.h> 42 + 43 + #include <linux/iio/iio.h> 44 44 45 45 #include <asm/unaligned.h> 46 46
+77 -61
drivers/iio/pressure/dps310.c
··· 171 171 int reg; 172 172 173 173 rc = regmap_read(data->regmap, 0x32, &reg); 174 - if (rc) 174 + if (rc < 0) 175 175 return rc; 176 176 177 177 /* ··· 256 256 return dps310_temp_workaround(data); 257 257 } 258 258 259 - static int dps310_get_pres_precision(struct dps310_data *data) 259 + static int dps310_get_pres_precision(struct dps310_data *data, int *val) 260 260 { 261 - int rc; 262 - int val; 261 + int reg_val, rc; 263 262 264 - rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val); 263 + rc = regmap_read(data->regmap, DPS310_PRS_CFG, &reg_val); 265 264 if (rc < 0) 266 265 return rc; 267 266 268 - return BIT(val & GENMASK(2, 0)); 267 + *val = BIT(reg_val & GENMASK(2, 0)); 268 + 269 + return 0; 269 270 } 270 271 271 - static int dps310_get_temp_precision(struct dps310_data *data) 272 + static int dps310_get_temp_precision(struct dps310_data *data, int *val) 272 273 { 273 - int rc; 274 - int val; 274 + int reg_val, rc; 275 275 276 - rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val); 276 + rc = regmap_read(data->regmap, DPS310_TMP_CFG, &reg_val); 277 277 if (rc < 0) 278 278 return rc; 279 279 ··· 281 281 * Scale factor is bottom 4 bits of the register, but 1111 is 282 282 * reserved so just grab bottom three 283 283 */ 284 - return BIT(val & GENMASK(2, 0)); 284 + *val = BIT(reg_val & GENMASK(2, 0)); 285 + 286 + return 0; 285 287 } 286 288 287 289 /* Called with lock held */ ··· 352 350 DPS310_TMP_RATE_BITS, val); 353 351 } 354 352 355 - static int dps310_get_pres_samp_freq(struct dps310_data *data) 353 + static int dps310_get_pres_samp_freq(struct dps310_data *data, int *val) 356 354 { 357 - int rc; 358 - int val; 355 + int reg_val, rc; 359 356 360 - rc = regmap_read(data->regmap, DPS310_PRS_CFG, &val); 357 + rc = regmap_read(data->regmap, DPS310_PRS_CFG, &reg_val); 361 358 if (rc < 0) 362 359 return rc; 363 360 364 - return BIT((val & DPS310_PRS_RATE_BITS) >> 4); 361 + *val = BIT((reg_val & DPS310_PRS_RATE_BITS) >> 4); 362 + 363 + return 0; 365 364 } 366 365 367 - static int dps310_get_temp_samp_freq(struct dps310_data *data) 366 + static int dps310_get_temp_samp_freq(struct dps310_data *data, int *val) 368 367 { 369 - int rc; 370 - int val; 368 + int reg_val, rc; 371 369 372 - rc = regmap_read(data->regmap, DPS310_TMP_CFG, &val); 370 + rc = regmap_read(data->regmap, DPS310_TMP_CFG, &reg_val); 373 371 if (rc < 0) 374 372 return rc; 375 373 376 - return BIT((val & DPS310_TMP_RATE_BITS) >> 4); 374 + *val = BIT((reg_val & DPS310_TMP_RATE_BITS) >> 4); 375 + 376 + return 0; 377 377 } 378 378 379 - static int dps310_get_pres_k(struct dps310_data *data) 379 + static int dps310_get_pres_k(struct dps310_data *data, int *val) 380 380 { 381 - int rc = dps310_get_pres_precision(data); 381 + int reg_val, rc; 382 382 383 + rc = regmap_read(data->regmap, DPS310_PRS_CFG, &reg_val); 383 384 if (rc < 0) 384 385 return rc; 385 386 386 - return scale_factors[ilog2(rc)]; 387 + *val = scale_factors[reg_val & GENMASK(2, 0)]; 388 + 389 + return 0; 387 390 } 388 391 389 - static int dps310_get_temp_k(struct dps310_data *data) 392 + static int dps310_get_temp_k(struct dps310_data *data, int *val) 390 393 { 391 - int rc = dps310_get_temp_precision(data); 394 + int reg_val, rc; 392 395 396 + rc = regmap_read(data->regmap, DPS310_TMP_CFG, &reg_val); 393 397 if (rc < 0) 394 398 return rc; 395 399 396 - return scale_factors[ilog2(rc)]; 400 + *val = scale_factors[reg_val & GENMASK(2, 0)]; 401 + 402 + return 0; 397 403 } 398 404 399 405 static int dps310_reset_wait(struct dps310_data *data) ··· 474 464 if (mutex_lock_interruptible(&data->lock)) 475 465 return -EINTR; 476 466 477 - rate = dps310_get_pres_samp_freq(data); 467 + rc = dps310_get_pres_samp_freq(data, &rate); 468 + if (rc) 469 + goto done; 470 + 478 471 timeout = DPS310_POLL_TIMEOUT_US(rate); 479 472 480 473 /* Poll for sensor readiness; base the timeout upon the sample rate. */ ··· 523 510 if (mutex_lock_interruptible(&data->lock)) 524 511 return -EINTR; 525 512 526 - rate = dps310_get_temp_samp_freq(data); 513 + rc = dps310_get_temp_samp_freq(data, &rate); 514 + if (rc) 515 + goto done; 516 + 527 517 timeout = DPS310_POLL_TIMEOUT_US(rate); 528 518 529 519 /* Poll for sensor readiness; base the timeout upon the sample rate. */ ··· 628 612 return rc; 629 613 } 630 614 631 - static int dps310_calculate_pressure(struct dps310_data *data) 615 + static int dps310_calculate_pressure(struct dps310_data *data, int *val) 632 616 { 633 617 int i; 634 618 int rc; 635 619 int t_ready; 636 - int kpi = dps310_get_pres_k(data); 637 - int kti = dps310_get_temp_k(data); 620 + int kpi; 621 + int kti; 638 622 s64 rem = 0ULL; 639 623 s64 pressure = 0ULL; 640 624 s64 p; ··· 645 629 s64 kp; 646 630 s64 kt; 647 631 648 - if (kpi < 0) 649 - return kpi; 632 + rc = dps310_get_pres_k(data, &kpi); 633 + if (rc) 634 + return rc; 650 635 651 - if (kti < 0) 652 - return kti; 636 + rc = dps310_get_temp_k(data, &kti); 637 + if (rc) 638 + return rc; 653 639 654 640 kp = (s64)kpi; 655 641 kt = (s64)kti; ··· 705 687 if (pressure < 0LL) 706 688 return -ERANGE; 707 689 708 - return (int)min_t(s64, pressure, INT_MAX); 690 + *val = (int)min_t(s64, pressure, INT_MAX); 691 + 692 + return 0; 709 693 } 710 694 711 695 static int dps310_read_pressure(struct dps310_data *data, int *val, int *val2, ··· 717 697 718 698 switch (mask) { 719 699 case IIO_CHAN_INFO_SAMP_FREQ: 720 - rc = dps310_get_pres_samp_freq(data); 721 - if (rc < 0) 700 + rc = dps310_get_pres_samp_freq(data, val); 701 + if (rc) 722 702 return rc; 723 703 724 - *val = rc; 725 704 return IIO_VAL_INT; 726 705 727 706 case IIO_CHAN_INFO_PROCESSED: ··· 728 709 if (rc) 729 710 return rc; 730 711 731 - rc = dps310_calculate_pressure(data); 732 - if (rc < 0) 712 + rc = dps310_calculate_pressure(data, val); 713 + if (rc) 733 714 return rc; 734 715 735 - *val = rc; 736 716 *val2 = 1000; /* Convert Pa to KPa per IIO ABI */ 737 717 return IIO_VAL_FRACTIONAL; 738 718 739 719 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 740 - rc = dps310_get_pres_precision(data); 741 - if (rc < 0) 720 + rc = dps310_get_pres_precision(data, val); 721 + if (rc) 742 722 return rc; 743 - 744 - *val = rc; 745 723 return IIO_VAL_INT; 746 724 747 725 default: ··· 746 730 } 747 731 } 748 732 749 - static int dps310_calculate_temp(struct dps310_data *data) 733 + static int dps310_calculate_temp(struct dps310_data *data, int *val) 750 734 { 751 735 s64 c0; 752 736 s64 t; 753 - int kt = dps310_get_temp_k(data); 737 + int kt, rc; 754 738 755 - if (kt < 0) 756 - return kt; 739 + rc = dps310_get_temp_k(data, &kt); 740 + if (rc) 741 + return rc; 757 742 758 743 /* Obtain inverse-scaled offset */ 759 744 c0 = div_s64((s64)kt * (s64)data->c0, 2); ··· 763 746 t = c0 + ((s64)data->temp_raw * (s64)data->c1); 764 747 765 748 /* Convert to milliCelsius and scale the temperature */ 766 - return (int)div_s64(t * 1000LL, kt); 749 + *val = (int)div_s64(t * 1000LL, kt); 750 + 751 + return 0; 767 752 } 768 753 769 754 static int dps310_read_temp(struct dps310_data *data, int *val, int *val2, ··· 775 756 776 757 switch (mask) { 777 758 case IIO_CHAN_INFO_SAMP_FREQ: 778 - rc = dps310_get_temp_samp_freq(data); 779 - if (rc < 0) 759 + rc = dps310_get_temp_samp_freq(data, val); 760 + if (rc) 780 761 return rc; 781 762 782 - *val = rc; 783 763 return IIO_VAL_INT; 784 764 785 765 case IIO_CHAN_INFO_PROCESSED: ··· 786 768 if (rc) 787 769 return rc; 788 770 789 - rc = dps310_calculate_temp(data); 790 - if (rc < 0) 771 + rc = dps310_calculate_temp(data, val); 772 + if (rc) 791 773 return rc; 792 774 793 - *val = rc; 794 775 return IIO_VAL_INT; 795 776 796 777 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 797 - rc = dps310_get_temp_precision(data); 798 - if (rc < 0) 778 + rc = dps310_get_temp_precision(data, val); 779 + if (rc) 799 780 return rc; 800 781 801 - *val = rc; 802 782 return IIO_VAL_INT; 803 783 804 784 default:
+1 -6
drivers/iio/pressure/hsc030pa_spi.c
··· 23 23 static int hsc_spi_recv(struct hsc_data *data) 24 24 { 25 25 struct spi_device *spi = to_spi_device(data->dev); 26 - struct spi_transfer xfer = { 27 - .tx_buf = NULL, 28 - .rx_buf = data->buffer, 29 - .len = HSC_REG_MEASUREMENT_RD_SIZE, 30 - }; 31 26 32 27 msleep_interruptible(HSC_RESP_TIME_MS); 33 - return spi_sync_transfer(spi, &xfer, 1); 28 + return spi_read(spi, data->buffer, HSC_REG_MEASUREMENT_RD_SIZE); 34 29 } 35 30 36 31 static int hsc_spi_probe(struct spi_device *spi)
+55 -87
drivers/iio/temperature/ltc2983.c
··· 16 16 #include <linux/module.h> 17 17 #include <linux/property.h> 18 18 #include <linux/regmap.h> 19 + #include <linux/regulator/consumer.h> 19 20 #include <linux/spi/spi.h> 20 21 21 22 #include <asm/byteorder.h> ··· 658 657 const struct ltc2983_sensor *sensor) 659 658 { 660 659 struct ltc2983_thermocouple *thermo; 661 - struct fwnode_handle *ref; 662 660 u32 oc_current; 663 661 int ret; 664 662 ··· 704 704 return ERR_PTR(-EINVAL); 705 705 } 706 706 707 - ref = fwnode_find_reference(child, "adi,cold-junction-handle", 0); 707 + struct fwnode_handle *ref __free(fwnode_handle) = 708 + fwnode_find_reference(child, "adi,cold-junction-handle", 0); 708 709 if (IS_ERR(ref)) { 709 710 ref = NULL; 710 711 } else { ··· 716 715 * the error right away. 717 716 */ 718 717 dev_err(&st->spi->dev, "Property reg must be given\n"); 719 - goto fail; 718 + return ERR_PTR(ret); 720 719 } 721 720 } 722 721 ··· 727 726 thermo->custom = __ltc2983_custom_sensor_new(st, child, 728 727 propname, false, 729 728 16384, true); 730 - if (IS_ERR(thermo->custom)) { 731 - ret = PTR_ERR(thermo->custom); 732 - goto fail; 733 - } 729 + if (IS_ERR(thermo->custom)) 730 + return ERR_CAST(thermo->custom); 734 731 } 735 732 736 733 /* set common parameters */ 737 734 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; 738 735 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; 739 736 740 - fwnode_handle_put(ref); 741 737 return &thermo->sensor; 742 - 743 - fail: 744 - fwnode_handle_put(ref); 745 - return ERR_PTR(ret); 746 738 } 747 739 748 740 static struct ltc2983_sensor * ··· 745 751 struct ltc2983_rtd *rtd; 746 752 int ret = 0; 747 753 struct device *dev = &st->spi->dev; 748 - struct fwnode_handle *ref; 749 754 u32 excitation_current = 0, n_wires = 0; 750 755 751 756 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL); 752 757 if (!rtd) 753 758 return ERR_PTR(-ENOMEM); 754 759 755 - ref = fwnode_find_reference(child, "adi,rsense-handle", 0); 760 + struct fwnode_handle *ref __free(fwnode_handle) = 761 + fwnode_find_reference(child, "adi,rsense-handle", 0); 756 762 if (IS_ERR(ref)) { 757 763 dev_err(dev, "Property adi,rsense-handle missing or invalid"); 758 764 return ERR_CAST(ref); ··· 761 767 ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan); 762 768 if (ret) { 763 769 dev_err(dev, "Property reg must be given\n"); 764 - goto fail; 770 + return ERR_PTR(ret); 765 771 } 766 772 767 773 ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires); ··· 782 788 break; 783 789 default: 784 790 dev_err(dev, "Invalid number of wires:%u\n", n_wires); 785 - ret = -EINVAL; 786 - goto fail; 791 + return ERR_PTR(-EINVAL); 787 792 } 788 793 } 789 794 ··· 792 799 if (n_wires == 2 || n_wires == 3) { 793 800 dev_err(dev, 794 801 "Rotation not allowed for 2/3 Wire RTDs"); 795 - ret = -EINVAL; 796 - goto fail; 802 + return ERR_PTR(-EINVAL); 797 803 } 798 804 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); 799 805 } else { ··· 822 830 "Invalid rsense chann:%d to use in kelvin rsense", 823 831 rtd->r_sense_chan); 824 832 825 - ret = -EINVAL; 826 - goto fail; 833 + return ERR_PTR(-EINVAL); 827 834 } 828 835 829 836 if (sensor->chan < min || sensor->chan > max) { 830 837 dev_err(dev, "Invalid chann:%d for the rtd config", 831 838 sensor->chan); 832 839 833 - ret = -EINVAL; 834 - goto fail; 840 + return ERR_PTR(-EINVAL); 835 841 } 836 842 } else { 837 843 /* same as differential case */ ··· 837 847 dev_err(&st->spi->dev, 838 848 "Invalid chann:%d for RTD", sensor->chan); 839 849 840 - ret = -EINVAL; 841 - goto fail; 850 + return ERR_PTR(-EINVAL); 842 851 } 843 852 } 844 853 ··· 846 857 rtd->custom = __ltc2983_custom_sensor_new(st, child, 847 858 "adi,custom-rtd", 848 859 false, 2048, false); 849 - if (IS_ERR(rtd->custom)) { 850 - ret = PTR_ERR(rtd->custom); 851 - goto fail; 852 - } 860 + if (IS_ERR(rtd->custom)) 861 + return ERR_CAST(rtd->custom); 853 862 } 854 863 855 864 /* set common parameters */ ··· 889 902 dev_err(&st->spi->dev, 890 903 "Invalid value for excitation current(%u)", 891 904 excitation_current); 892 - ret = -EINVAL; 893 - goto fail; 905 + return ERR_PTR(-EINVAL); 894 906 } 895 907 } 896 908 897 909 fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); 898 910 899 - fwnode_handle_put(ref); 900 911 return &rtd->sensor; 901 - fail: 902 - fwnode_handle_put(ref); 903 - return ERR_PTR(ret); 904 912 } 905 913 906 914 static struct ltc2983_sensor * ··· 904 922 { 905 923 struct ltc2983_thermistor *thermistor; 906 924 struct device *dev = &st->spi->dev; 907 - struct fwnode_handle *ref; 908 925 u32 excitation_current = 0; 909 926 int ret = 0; 910 927 ··· 911 930 if (!thermistor) 912 931 return ERR_PTR(-ENOMEM); 913 932 914 - ref = fwnode_find_reference(child, "adi,rsense-handle", 0); 933 + struct fwnode_handle *ref __free(fwnode_handle) = 934 + fwnode_find_reference(child, "adi,rsense-handle", 0); 915 935 if (IS_ERR(ref)) { 916 936 dev_err(dev, "Property adi,rsense-handle missing or invalid"); 917 937 return ERR_CAST(ref); ··· 921 939 ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan); 922 940 if (ret) { 923 941 dev_err(dev, "rsense channel must be configured...\n"); 924 - goto fail; 942 + return ERR_PTR(ret); 925 943 } 926 944 927 945 if (fwnode_property_read_bool(child, "adi,single-ended")) { ··· 941 959 dev_err(&st->spi->dev, 942 960 "Invalid chann:%d for differential thermistor", 943 961 sensor->chan); 944 - ret = -EINVAL; 945 - goto fail; 962 + return ERR_PTR(-EINVAL); 946 963 } 947 964 948 965 /* check custom sensor */ ··· 960 979 propname, 961 980 steinhart, 962 981 64, false); 963 - if (IS_ERR(thermistor->custom)) { 964 - ret = PTR_ERR(thermistor->custom); 965 - goto fail; 966 - } 982 + if (IS_ERR(thermistor->custom)) 983 + return ERR_CAST(thermistor->custom); 967 984 } 968 985 /* set common parameters */ 969 986 thermistor->sensor.fault_handler = ltc2983_common_fault_handler; ··· 985 1006 LTC2983_SENSOR_THERMISTOR_STEINHART) { 986 1007 dev_err(&st->spi->dev, 987 1008 "Auto Range not allowed for custom sensors\n"); 988 - ret = -EINVAL; 989 - goto fail; 1009 + return ERR_PTR(-EINVAL); 990 1010 } 991 1011 thermistor->excitation_current = 0x0c; 992 1012 break; ··· 1026 1048 dev_err(&st->spi->dev, 1027 1049 "Invalid value for excitation current(%u)", 1028 1050 excitation_current); 1029 - ret = -EINVAL; 1030 - goto fail; 1051 + return ERR_PTR(-EINVAL); 1031 1052 } 1032 1053 } 1033 1054 1034 - fwnode_handle_put(ref); 1035 1055 return &thermistor->sensor; 1036 - fail: 1037 - fwnode_handle_put(ref); 1038 - return ERR_PTR(ret); 1039 1056 } 1040 1057 1041 1058 static struct ltc2983_sensor * ··· 1323 1350 static int ltc2983_parse_fw(struct ltc2983_data *st) 1324 1351 { 1325 1352 struct device *dev = &st->spi->dev; 1326 - struct fwnode_handle *child; 1327 - int ret = 0, chan = 0, channel_avail_mask = 0; 1353 + int ret, chan = 0, channel_avail_mask = 0; 1328 1354 1329 1355 device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config); 1330 1356 ··· 1341 1369 return -ENOMEM; 1342 1370 1343 1371 st->iio_channels = st->num_channels; 1344 - device_for_each_child_node(dev, child) { 1372 + device_for_each_child_node_scoped(dev, child) { 1345 1373 struct ltc2983_sensor sensor; 1346 1374 1347 1375 ret = fwnode_property_read_u32(child, "reg", &sensor.chan); 1348 - if (ret) { 1349 - dev_err(dev, "reg property must given for child nodes\n"); 1350 - goto put_child; 1351 - } 1376 + if (ret) 1377 + return dev_err_probe(dev, ret, 1378 + "reg property must given for child nodes\n"); 1352 1379 1353 1380 /* check if we have a valid channel */ 1354 1381 if (sensor.chan < LTC2983_MIN_CHANNELS_NR || 1355 - sensor.chan > st->info->max_channels_nr) { 1356 - ret = -EINVAL; 1357 - dev_err(dev, "chan:%d must be from %u to %u\n", sensor.chan, 1358 - LTC2983_MIN_CHANNELS_NR, st->info->max_channels_nr); 1359 - goto put_child; 1360 - } else if (channel_avail_mask & BIT(sensor.chan)) { 1361 - ret = -EINVAL; 1362 - dev_err(dev, "chan:%d already in use\n", sensor.chan); 1363 - goto put_child; 1364 - } 1382 + sensor.chan > st->info->max_channels_nr) 1383 + return dev_err_probe(dev, -EINVAL, 1384 + "chan:%d must be from %u to %u\n", 1385 + sensor.chan, 1386 + LTC2983_MIN_CHANNELS_NR, 1387 + st->info->max_channels_nr); 1388 + 1389 + if (channel_avail_mask & BIT(sensor.chan)) 1390 + return dev_err_probe(dev, -EINVAL, 1391 + "chan:%d already in use\n", 1392 + sensor.chan); 1365 1393 1366 1394 ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type); 1367 - if (ret) { 1368 - dev_err(dev, 1395 + if (ret) 1396 + return dev_err_probe(dev, ret, 1369 1397 "adi,sensor-type property must given for child nodes\n"); 1370 - goto put_child; 1371 - } 1372 1398 1373 1399 dev_dbg(dev, "Create new sensor, type %u, chann %u", 1374 - sensor.type, 1375 - sensor.chan); 1400 + sensor.type, sensor.chan); 1376 1401 1377 1402 if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE && 1378 1403 sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { ··· 1396 1427 sensor.type == LTC2983_SENSOR_ACTIVE_TEMP) { 1397 1428 st->sensors[chan] = ltc2983_temp_new(child, st, &sensor); 1398 1429 } else { 1399 - dev_err(dev, "Unknown sensor type %d\n", sensor.type); 1400 - ret = -EINVAL; 1401 - goto put_child; 1430 + return dev_err_probe(dev, -EINVAL, 1431 + "Unknown sensor type %d\n", 1432 + sensor.type); 1402 1433 } 1403 1434 1404 - if (IS_ERR(st->sensors[chan])) { 1405 - dev_err(dev, "Failed to create sensor %ld", 1406 - PTR_ERR(st->sensors[chan])); 1407 - ret = PTR_ERR(st->sensors[chan]); 1408 - goto put_child; 1409 - } 1435 + if (IS_ERR(st->sensors[chan])) 1436 + return dev_err_probe(dev, PTR_ERR(st->sensors[chan]), 1437 + "Failed to create sensor\n"); 1438 + 1410 1439 /* set generic sensor parameters */ 1411 1440 st->sensors[chan]->chan = sensor.chan; 1412 1441 st->sensors[chan]->type = sensor.type; ··· 1414 1447 } 1415 1448 1416 1449 return 0; 1417 - put_child: 1418 - fwnode_handle_put(child); 1419 - return ret; 1420 1450 } 1421 1451 1422 1452 static int ltc2983_eeprom_cmd(struct ltc2983_data *st, unsigned int cmd, ··· 1595 1631 spi_set_drvdata(spi, st); 1596 1632 1597 1633 ret = ltc2983_parse_fw(st); 1634 + if (ret) 1635 + return ret; 1636 + 1637 + ret = devm_regulator_get_enable(&spi->dev, "vdd"); 1598 1638 if (ret) 1599 1639 return ret; 1600 1640
+3
include/linux/iio/adc/ad_sigma_delta.h
··· 48 48 * be used. 49 49 * @irq_flags: flags for the interrupt used by the triggered buffer 50 50 * @num_slots: Number of sequencer slots 51 + * @irq_line: IRQ for reading conversions. If 0, spi->irq will be used 51 52 */ 52 53 struct ad_sigma_delta_info { 53 54 int (*set_channel)(struct ad_sigma_delta *, unsigned int channel); ··· 63 62 unsigned int data_reg; 64 63 unsigned long irq_flags; 65 64 unsigned int num_slots; 65 + int irq_line; 66 66 }; 67 67 68 68 /** ··· 91 89 unsigned int active_slots; 92 90 unsigned int current_slot; 93 91 unsigned int num_slots; 92 + int irq_line; 94 93 bool status_appended; 95 94 /* map slots to channels in order to know what to expect from devices */ 96 95 unsigned int *slots;
+49
include/linux/iio/backend.h
··· 4 4 5 5 #include <linux/types.h> 6 6 7 + struct iio_chan_spec; 7 8 struct fwnode_handle; 8 9 struct iio_backend; 9 10 struct device; ··· 15 14 IIO_BACKEND_OFFSET_BINARY, 16 15 IIO_BACKEND_DATA_TYPE_MAX 17 16 }; 17 + 18 + enum iio_backend_data_source { 19 + IIO_BACKEND_INTERNAL_CONTINUOS_WAVE, 20 + IIO_BACKEND_EXTERNAL, 21 + IIO_BACKEND_DATA_SOURCE_MAX 22 + }; 23 + 24 + /** 25 + * IIO_BACKEND_EX_INFO - Helper for an IIO extended channel attribute 26 + * @_name: Attribute name 27 + * @_shared: Whether the attribute is shared between all channels 28 + * @_what: Data private to the driver 29 + */ 30 + #define IIO_BACKEND_EX_INFO(_name, _shared, _what) { \ 31 + .name = (_name), \ 32 + .shared = (_shared), \ 33 + .read = iio_backend_ext_info_get, \ 34 + .write = iio_backend_ext_info_set, \ 35 + .private = (_what), \ 36 + } 18 37 19 38 /** 20 39 * struct iio_backend_data_fmt - Backend data format ··· 56 35 * @chan_enable: Enable one channel. 57 36 * @chan_disable: Disable one channel. 58 37 * @data_format_set: Configure the data format for a specific channel. 38 + * @data_source_set: Configure the data source for a specific channel. 39 + * @set_sample_rate: Configure the sampling rate for a specific channel. 59 40 * @request_buffer: Request an IIO buffer. 60 41 * @free_buffer: Free an IIO buffer. 42 + * @extend_chan_spec: Extend an IIO channel. 43 + * @ext_info_set: Extended info setter. 44 + * @ext_info_get: Extended info getter. 61 45 **/ 62 46 struct iio_backend_ops { 63 47 int (*enable)(struct iio_backend *back); ··· 71 45 int (*chan_disable)(struct iio_backend *back, unsigned int chan); 72 46 int (*data_format_set)(struct iio_backend *back, unsigned int chan, 73 47 const struct iio_backend_data_fmt *data); 48 + int (*data_source_set)(struct iio_backend *back, unsigned int chan, 49 + enum iio_backend_data_source data); 50 + int (*set_sample_rate)(struct iio_backend *back, unsigned int chan, 51 + u64 sample_rate_hz); 74 52 struct iio_buffer *(*request_buffer)(struct iio_backend *back, 75 53 struct iio_dev *indio_dev); 76 54 void (*free_buffer)(struct iio_backend *back, 77 55 struct iio_buffer *buffer); 56 + int (*extend_chan_spec)(struct iio_backend *back, 57 + struct iio_chan_spec *chan); 58 + int (*ext_info_set)(struct iio_backend *back, uintptr_t private, 59 + const struct iio_chan_spec *chan, 60 + const char *buf, size_t len); 61 + int (*ext_info_get)(struct iio_backend *back, uintptr_t private, 62 + const struct iio_chan_spec *chan, char *buf); 78 63 }; 79 64 80 65 int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan); ··· 93 56 int devm_iio_backend_enable(struct device *dev, struct iio_backend *back); 94 57 int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan, 95 58 const struct iio_backend_data_fmt *data); 59 + int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan, 60 + enum iio_backend_data_source data); 61 + int iio_backend_set_sampling_freq(struct iio_backend *back, unsigned int chan, 62 + u64 sample_rate_hz); 96 63 int devm_iio_backend_request_buffer(struct device *dev, 97 64 struct iio_backend *back, 98 65 struct iio_dev *indio_dev); 66 + ssize_t iio_backend_ext_info_set(struct iio_dev *indio_dev, uintptr_t private, 67 + const struct iio_chan_spec *chan, 68 + const char *buf, size_t len); 69 + ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private, 70 + const struct iio_chan_spec *chan, char *buf); 99 71 72 + int iio_backend_extend_chan_spec(struct iio_dev *indio_dev, 73 + struct iio_backend *back, 74 + struct iio_chan_spec *chan); 100 75 void *iio_backend_get_priv(const struct iio_backend *conv); 101 76 struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name); 102 77 struct iio_backend *
+3 -1
include/linux/iio/buffer-dma.h
··· 132 132 struct iio_dev *indio_dev); 133 133 int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n, 134 134 char __user *user_buffer); 135 - size_t iio_dma_buffer_data_available(struct iio_buffer *buffer); 135 + int iio_dma_buffer_write(struct iio_buffer *buffer, size_t n, 136 + const char __user *user_buffer); 137 + size_t iio_dma_buffer_usage(struct iio_buffer *buffer); 136 138 int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd); 137 139 int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length); 138 140 int iio_dma_buffer_request_update(struct iio_buffer *buffer);
+19 -5
include/linux/iio/buffer-dmaengine.h
··· 7 7 #ifndef __IIO_DMAENGINE_H__ 8 8 #define __IIO_DMAENGINE_H__ 9 9 10 + #include <linux/iio/buffer.h> 11 + 10 12 struct iio_dev; 11 13 struct device; 12 14 13 - struct iio_buffer *iio_dmaengine_buffer_alloc(struct device *dev, 14 - const char *channel); 15 15 void iio_dmaengine_buffer_free(struct iio_buffer *buffer); 16 - int devm_iio_dmaengine_buffer_setup(struct device *dev, 17 - struct iio_dev *indio_dev, 18 - const char *channel); 16 + struct iio_buffer *iio_dmaengine_buffer_setup_ext(struct device *dev, 17 + struct iio_dev *indio_dev, 18 + const char *channel, 19 + enum iio_buffer_direction dir); 20 + 21 + #define iio_dmaengine_buffer_setup(dev, indio_dev, channel) \ 22 + iio_dmaengine_buffer_setup_ext(dev, indio_dev, channel, \ 23 + IIO_BUFFER_DIRECTION_IN) 24 + 25 + int devm_iio_dmaengine_buffer_setup_ext(struct device *dev, 26 + struct iio_dev *indio_dev, 27 + const char *channel, 28 + enum iio_buffer_direction dir); 29 + 30 + #define devm_iio_dmaengine_buffer_setup(dev, indio_dev, channel) \ 31 + devm_iio_dmaengine_buffer_setup_ext(dev, indio_dev, channel, \ 32 + IIO_BUFFER_DIRECTION_IN) 19 33 20 34 #endif
+21 -1
include/linux/property.h
··· 13 13 #include <linux/args.h> 14 14 #include <linux/array_size.h> 15 15 #include <linux/bits.h> 16 + #include <linux/cleanup.h> 16 17 #include <linux/fwnode.h> 17 18 #include <linux/stddef.h> 18 19 #include <linux/types.h> ··· 175 174 for (child = device_get_next_child_node(dev, NULL); child; \ 176 175 child = device_get_next_child_node(dev, child)) 177 176 177 + #define device_for_each_child_node_scoped(dev, child) \ 178 + for (struct fwnode_handle *child __free(fwnode_handle) = \ 179 + device_get_next_child_node(dev, NULL); \ 180 + child; child = device_get_next_child_node(dev, child)) 181 + 178 182 struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode, 179 183 const char *childname); 180 184 struct fwnode_handle *device_get_named_child_node(const struct device *dev, 181 185 const char *childname); 182 186 183 187 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode); 184 - void fwnode_handle_put(struct fwnode_handle *fwnode); 188 + 189 + /** 190 + * fwnode_handle_put - Drop reference to a device node 191 + * @fwnode: Pointer to the device node to drop the reference to. 192 + * 193 + * This has to be used when terminating device_for_each_child_node() iteration 194 + * with break or return to prevent stale device node references from being left 195 + * behind. 196 + */ 197 + static inline void fwnode_handle_put(struct fwnode_handle *fwnode) 198 + { 199 + fwnode_call_void_op(fwnode, put); 200 + } 201 + 202 + DEFINE_FREE(fwnode_handle, struct fwnode_handle *, fwnode_handle_put(_T)) 185 203 186 204 int fwnode_irq_get(const struct fwnode_handle *fwnode, unsigned int index); 187 205 int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name);