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

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

Jonathan writes:

First set of IIO new device and feature support for the 5.16 cycle

Counter subsystem changes now sent separately.

This has been a busy cycle, so lots here and a few more stragglers to
come next week.

Big new feature in this cycle is probably output buffer support.
This has been in the works for a very long time so it's great to see
Mihail pick up the challenge and build upon his predecessors work to finally
bring this feature to mainline.

New device support
------------------

* adi,adxl313
- New driver and dt bindings for this low power accelerometer.
* adi,adxl355
- New driver and dt bindings for this accelerometer.
- Later series adds buffer support.
* asahi-kasei,ak8975
- Minor additions to driver to support ak09916
* aspeed,aspeed-adc
- Substantial rework plus feature additions to add support for the
ast2600 including a new dt bindings doc.
* atmel,at91_sama5d2
- Rework and support introduced for the sama7g5 parts.
* maxim,max31865
- New driver and bindings for this RTD temperature sensor chip.
* nxp,imx8qxp
- New driver and bindings for the ADC found on the i.MX 8QuadXPlus Soc.
* senseair,sunrise
- New driver and bindings for this family of carbon dioxide gas sensors.
* sensiron,scd4x
- New driver and bindings for this carbon dioxide gas sensor.

New features
------------

* Output buffer support. Works in a similar fashion to input buffers, but
in this case userspace pushes data into the kfifo which is then drained
to the device when a trigger occurs. Support added to the ad5766 DAC
driver.
* Core, devm_iio_map_array_register() to avoid need for
devm_add_action_or_reset() based cleanup in fully managed allocation
drivers.
* Core iio_push_to_buffers_with_ts_unaligned() function to safely handle a
few drivers where it really hard to ensure the correct data alignment in
an iio_push_to_buffers_with_timestamp() call. Note this uses a bounce
buffer so should be avoided whenever possible. Used in the ti,adc108s102,
invense,mpu3050 and adi,adis16400. This closes the last known set
of drivers with alignment issues at this interface.
* maxim,max1027
- Substantial rework to this driver main target of which was supporting
use of other triggers than it's own EOC interrupt.
- Transfer optimization.
* nxp,fxls8962af
- Threshold even support including using it as a wakeup source.

Cleanups, minor fixes etc
-------------------------

Chances of a common type to multiple drivers:

* devm_ conversion and drop of .remove() callbacks in:
- adi,ad5064
- adi,ad7291
- adi,ad7303
- adi,ad7746
- adi,ad9832
- adi,adis16080
- dialog,da9150-gpadc
- intel,mrfld_adc
- marvell,berlin2
- maxim,max1363
- maxim,max44000
- nuvoton,nau7802
- st_sensors (includes a lot of rework!)
- ti,ads8344
- ti,lp8788

* devm_platform_ioremap_resource() used to reduce boilerplate
- cirrus,ep93xx
- rockchip,saradc
- stm,stm32-dac

* Use dev_err_probe() in more places to both not print on deferred probe and
ensure a reason for the deferral is available for debug purposes.
- adi,ad8801
- capella,cm36651
- linear,ltc1660
- maxim,ds4424
- maxim,max5821
- microchip,mcp4922
- nxp,lpc18xx
- onnn,noa1305
- st,lsm9ds0
- st,st_sensors
- st,stm32-dac
- ti,afe4403
- ti,afe4404
- ti,dac7311

* Drop error returns in SPI and I2C remove() functions as they are ignored and
long term plan is to change these all over to returning void. In some cases
these patches just make it 'obvious' they always return 0 where it was the
case before but not easy to tell.
- adi,ad5380
- adi,ad5446
- adi,ad5686
- adi,ad5592r
- bosch,bma400
- bosch,bmc150
- fsl,mma7455
- honeywell,hmc5843
- kionix,kxsd9
- maxim,max5487
- meas,ms5611
- ti,afe4403

Driver specific changes

* adi,ad5770r
- Bring driver inline with documented bindings.
* adi,ad7746
- Trivial style fix
* adi,ad7949
- Express some magic values as the underlying parts via new #defines.
- Make it work with SPI controllers that don't support 14 or 16 bit messages
- Support selection of voltage reference from dt including expanding the
dt-bindings to cover this new functionality.
* adi,ad799x
- Implement selection of external reference voltage on AD7991, AD7995 and
AD7999.
- Add missing dt-bindings doc for devices supported by this driver.
* adi,adislib
- Move interrupt startup to better location in startup flow.
- Handle devices that cannot mask/unmask the drdy pin and must instead mask
at the interrupt controller. Applies to the adis16460 and adis16475 from
which we then drop equivalent code.
* adi,ltc2983
- Add support for optional reset pin.
- Fail to probe if no channels specified in dt binding.
* asahi-kasei,ak8975
- dt-binding additions of missing vid-supply regulator.
* aspeed,aspeed-adc
- Typo fix.
* fsl,mma7660
- Mark acpi_device_id table __maybe_unused to avoid build warning.
* fsl,imx25-gcq
- Avoid initializing regulators that aren't used.
* invensense,mpu3050
- Drop a dead protection against a clash with the old input driver.
* invensense,mpu6050
- Rework code to not use strcpy() and hence avoid possibility of wrong sized
buffers. Note this wasn't a bug, but the new code is a lot more readable.
- Mark acpi_device_id table __maybe_unused to avoid build warning.
* kionix,kxcjk1013
- dt-binding addition to note it supports interrupts.
* marvell,berlin2-adc
- Enable COMPILE_TEST building.
* maxim,max1027
- Avoid returning success in an error path.
* nxp,imx8qxp
- Fix warning when runtime pm not enabled via __maybe_unused.
* ricoh,rn5t618
- Use the new devm_iio_map_array_register() instead of open coding the same.
* samsung,exynos_adc
- Improve kconfig help text.
* st,lsm6dsx
- Move max_fifo_size into the fifo_ops structure where the other configuration
parameters are found.
* st,st_sensors:
- Reorder to ensure we turn the power off after removing userspace interfaces.
* senseair,sunrise
- Add missing I2C dependency.
* ti,twl6030
- Small code tidy up.

* tag 'iio-for-5.16a-split-take4' of https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (148 commits)
iio: imx8qxp-adc: mark PM functions as __maybe_unused
iio: pressure: ms5611: Make ms5611_remove() return void
iio: potentiometer: max5487: Don't return an error in .remove()
iio: magn: hmc5843: Make hmc5843_common_remove() return void
iio: health: afe4403: Don't return an error in .remove()
iio: dac: ad5686: Make ad5686_remove() return void
iio: dac: ad5592r: Make ad5592r_remove() return void
iio: dac: ad5446: Make ad5446_remove() return void
iio: dac: ad5380: Make ad5380_remove() return void
iio: accel: mma7455: Make mma7455_core_remove() return void
iio: accel: kxsd9: Make kxsd9_common_remove() return void
iio: accel: bmi088: Make bmi088_accel_core_remove() return void
iio: accel: bmc150: Make bmc150_accel_core_remove() return void
iio: accel: bma400: Make bma400_remove() return void
drivers:iio:dac:ad5766.c: Add trigger buffer
iio: triggered-buffer: extend support to configure output buffers
iio: kfifo-buffer: Add output buffer support
iio: Add output buffer support
iio: documentation: Document scd4x calibration use
drivers: iio: chemical: Add support for Sensirion SCD4x CO2 sensor
...

+7000 -1594
+42
Documentation/ABI/testing/sysfs-bus-iio
··· 429 429 What: /sys/bus/iio/devices/iio:deviceX/in_intensity_x_scale 430 430 What: /sys/bus/iio/devices/iio:deviceX/in_intensity_y_scale 431 431 What: /sys/bus/iio/devices/iio:deviceX/in_intensity_z_scale 432 + What: /sys/bus/iio/devices/iio:deviceX/in_concentration_co2_scale 432 433 KernelVersion: 2.6.35 433 434 Contact: linux-iio@vger.kernel.org 434 435 Description: ··· 1958 1957 Specify the percent for light sensor relative to the channel 1959 1958 absolute value that a data field should change before an event 1960 1959 is generated. Units are a percentage of the prior reading. 1960 + 1961 + What: /sys/bus/iio/devices/iio:deviceX/calibration_auto_enable 1962 + Date: June 2020 1963 + KernelVersion: 5.8 1964 + Contact: linux-iio@vger.kernel.org 1965 + Description: 1966 + Some sensors have the ability to apply auto calibration at 1967 + runtime. For example, it may be necessary to compensate for 1968 + contaminant build-up in a measurement chamber or optical 1969 + element deterioration that would otherwise lead to sensor drift. 1970 + 1971 + Writing 1 or 0 to this attribute will respectively activate or 1972 + deactivate this auto calibration function. 1973 + 1974 + Upon reading, the current status is returned. 1975 + 1976 + What: /sys/bus/iio/devices/iio:deviceX/calibration_forced_value 1977 + Date: June 2020 1978 + KernelVersion: 5.8 1979 + Contact: linux-iio@vger.kernel.org 1980 + Description: 1981 + Some sensors have the ability to apply a manual calibration using 1982 + a known measurement value, perhaps obtained from an external 1983 + reference device. 1984 + 1985 + Writing a value to this function will force such a calibration 1986 + change. For the scd30 the value should be from the range 1987 + [400 1 2000]. 1988 + 1989 + Note for the scd30 that a valid value may only be obtained once 1990 + it is has been written. Until then any read back of this value 1991 + should be ignored. As for the scd4x an error will be returned 1992 + immediately if the manual calibration has failed. 1993 + 1994 + What: /sys/bus/iio/devices/iio:deviceX/calibration_forced_value_available 1995 + KernelVersion: 5.15 1996 + Contact: linux-iio@vger.kernel.org 1997 + Description: 1998 + Available range for the forced calibration value, expressed as: 1999 + 2000 + - a range specified as "[min step max]"
+38
Documentation/ABI/testing/sysfs-bus-iio-chemical-sunrise-co2
··· 1 + What: /sys/bus/iio/devices/iio:deviceX/in_concentration_co2_calibration_factory 2 + Date: August 2021 3 + KernelVersion: 5.16 4 + Contact: Jacopo Mondi <jacopo@jmondi.org> 5 + Description: 6 + Writing '1' triggers a 'Factory' calibration cycle. 7 + 8 + What: /sys/bus/iio/devices/iio:deviceX/in_concentration_co2_calibration_background 9 + Date: August 2021 10 + KernelVersion: 5.16 11 + Contact: Jacopo Mondi <jacopo@jmondi.org> 12 + Description: 13 + Writing '1' triggers a 'Background' calibration cycle. 14 + 15 + What: /sys/bus/iio/devices/iio:deviceX/error_status_available 16 + Date: August 2021 17 + KernelVersion: 5.16 18 + Contact: Jacopo Mondi <jacopo@jmondi.org> 19 + Description: 20 + Reading returns the list of possible chip error status. 21 + Available options are: 22 + - 'error_fatal': Analog front-end initialization error 23 + - 'error_i2c': Read/write to non-existing register 24 + - 'error_algorithm': Corrupted parameters 25 + - 'error_calibration': Calibration has failed 26 + - 'error_self_diagnostic': Internal interface failure 27 + - 'error_out_of_range': Measured concentration out of scale 28 + - 'error_memory': Error during memory operations 29 + - 'error_no_measurement': Cleared at first measurement 30 + - 'error_low_voltage': Sensor regulated voltage too low 31 + - 'error_measurement_timeout': Unable to complete measurement 32 + 33 + What: /sys/bus/iio/devices/iio:deviceX/error_status 34 + Date: August 2021 35 + KernelVersion: 5.16 36 + Contact: Jacopo Mondi <jacopo@jmondi.org> 37 + Description: 38 + Reading returns the current chip error status.
-34
Documentation/ABI/testing/sysfs-bus-iio-scd30
··· 1 - What: /sys/bus/iio/devices/iio:deviceX/calibration_auto_enable 2 - Date: June 2020 3 - KernelVersion: 5.8 4 - Contact: linux-iio@vger.kernel.org 5 - Description: 6 - Contaminants build-up in the measurement chamber or optical 7 - elements deterioration leads to sensor drift. 8 - 9 - One can compensate for sensor drift by using automatic self 10 - calibration procedure (asc). 11 - 12 - Writing 1 or 0 to this attribute will respectively activate or 13 - deactivate asc. 14 - 15 - Upon reading current asc status is returned. 16 - 17 - What: /sys/bus/iio/devices/iio:deviceX/calibration_forced_value 18 - Date: June 2020 19 - KernelVersion: 5.8 20 - Contact: linux-iio@vger.kernel.org 21 - Description: 22 - Contaminants build-up in the measurement chamber or optical 23 - elements deterioration leads to sensor drift. 24 - 25 - One can compensate for sensor drift by using forced 26 - recalibration (frc). This is useful in case there's known 27 - co2 reference available nearby the sensor. 28 - 29 - Picking value from the range [400 1 2000] and writing it to the 30 - sensor will set frc. 31 - 32 - Upon reading current frc value is returned. Note that after 33 - power cycling default value (i.e 400) is returned even though 34 - internally sensor had recalibrated itself.
+20
Documentation/ABI/testing/sysfs-bus-iio-temperature-max31865
··· 1 + What: /sys/bus/iio/devices/iio:deviceX/fault_ovuv 2 + KernelVersion: 5.11 3 + Contact: linux-iio@vger.kernel.org 4 + Description: 5 + Overvoltage or Undervoltage Input fault. The internal circuitry 6 + is protected from excessive voltages applied to the thermocouple 7 + cables at FORCE+, FORCE2, RTDIN+ & RTDIN-. This circuitry turn 8 + off when the input voltage is negative or greater than VDD. 9 + 10 + Reading returns '1' if input voltage is negative or greater 11 + than VDD, otherwise '0'. 12 + 13 + What: /sys/bus/iio/devices/iio:deviceX/in_filter_notch_center_frequency 14 + KernelVersion: 5.11 15 + Contact: linux-iio@vger.kernel.org 16 + Description: 17 + Notch frequency in Hz for a noise rejection filter. Used i.e for 18 + line noise rejection. 19 + 20 + Valid notch filter values are 50 Hz and 60 Hz.
+86
Documentation/devicetree/bindings/iio/accel/adi,adxl313.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/accel/adi,adxl313.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices ADXL313 3-Axis Digital Accelerometer 8 + 9 + maintainers: 10 + - Lucas Stankus <lucas.p.stankus@gmail.com> 11 + 12 + description: | 13 + Analog Devices ADXL313 3-Axis Digital Accelerometer that supports 14 + both I2C & SPI interfaces. 15 + https://www.analog.com/en/products/adxl313.html 16 + 17 + properties: 18 + compatible: 19 + enum: 20 + - adi,adxl313 21 + 22 + reg: 23 + maxItems: 1 24 + 25 + spi-3wire: true 26 + 27 + spi-max-frequency: true 28 + 29 + vs-supply: 30 + description: Regulator that supplies power to the accelerometer 31 + 32 + vdd-supply: 33 + description: Regulator that supplies the digital interface supply voltage 34 + 35 + interrupts: 36 + minItems: 1 37 + maxItems: 2 38 + 39 + interrupt-names: 40 + minItems: 1 41 + maxItems: 2 42 + items: 43 + enum: 44 + - INT1 45 + - INT2 46 + 47 + required: 48 + - compatible 49 + - reg 50 + 51 + additionalProperties: false 52 + 53 + examples: 54 + - | 55 + #include <dt-bindings/gpio/gpio.h> 56 + #include <dt-bindings/interrupt-controller/irq.h> 57 + i2c0 { 58 + #address-cells = <1>; 59 + #size-cells = <0>; 60 + 61 + /* Example for a I2C device node */ 62 + accelerometer@53 { 63 + compatible = "adi,adxl313"; 64 + reg = <0x53>; 65 + interrupt-parent = <&gpio0>; 66 + interrupts = <0 IRQ_TYPE_LEVEL_HIGH>; 67 + interrupt-names = "INT1"; 68 + }; 69 + }; 70 + - | 71 + #include <dt-bindings/gpio/gpio.h> 72 + #include <dt-bindings/interrupt-controller/irq.h> 73 + spi { 74 + #address-cells = <1>; 75 + #size-cells = <0>; 76 + 77 + /* Example for a SPI device node */ 78 + accelerometer@0 { 79 + compatible = "adi,adxl313"; 80 + reg = <0>; 81 + spi-max-frequency = <5000000>; 82 + interrupt-parent = <&gpio0>; 83 + interrupts = <0 IRQ_TYPE_LEVEL_HIGH>; 84 + interrupt-names = "INT1"; 85 + }; 86 + };
+88
Documentation/devicetree/bindings/iio/accel/adi,adxl355.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/accel/adi,adxl355.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices ADXL355 3-Axis, Low noise MEMS Accelerometer 8 + 9 + maintainers: 10 + - Puranjay Mohan <puranjay12@gmail.com> 11 + 12 + description: | 13 + Analog Devices ADXL355 3-Axis, Low noise MEMS Accelerometer that supports 14 + both I2C & SPI interfaces 15 + https://www.analog.com/en/products/adxl355.html 16 + 17 + properties: 18 + compatible: 19 + enum: 20 + - adi,adxl355 21 + 22 + reg: 23 + maxItems: 1 24 + 25 + interrupts: 26 + minItems: 1 27 + maxItems: 3 28 + description: | 29 + Type for DRDY should be IRQ_TYPE_EDGE_RISING. 30 + Three configurable interrupt lines exist. 31 + 32 + interrupt-names: 33 + description: Specify which interrupt line is in use. 34 + items: 35 + enum: 36 + - INT1 37 + - INT2 38 + - DRDY 39 + minItems: 1 40 + maxItems: 3 41 + 42 + vdd-supply: 43 + description: Regulator that provides power to the sensor 44 + 45 + vddio-supply: 46 + description: Regulator that provides power to the bus 47 + 48 + spi-max-frequency: true 49 + 50 + required: 51 + - compatible 52 + - reg 53 + 54 + additionalProperties: false 55 + 56 + examples: 57 + - | 58 + #include <dt-bindings/gpio/gpio.h> 59 + #include <dt-bindings/interrupt-controller/irq.h> 60 + i2c { 61 + #address-cells = <1>; 62 + #size-cells = <0>; 63 + 64 + /* Example for a I2C device node */ 65 + accelerometer@1d { 66 + compatible = "adi,adxl355"; 67 + reg = <0x1d>; 68 + interrupt-parent = <&gpio>; 69 + interrupts = <25 IRQ_TYPE_EDGE_RISING>; 70 + interrupt-names = "DRDY"; 71 + }; 72 + }; 73 + - | 74 + #include <dt-bindings/gpio/gpio.h> 75 + #include <dt-bindings/interrupt-controller/irq.h> 76 + spi { 77 + #address-cells = <1>; 78 + #size-cells = <0>; 79 + 80 + accelerometer@0 { 81 + compatible = "adi,adxl355"; 82 + reg = <0>; 83 + spi-max-frequency = <1000000>; 84 + interrupt-parent = <&gpio>; 85 + interrupts = <25 IRQ_TYPE_EDGE_RISING>; 86 + interrupt-names = "DRDY"; 87 + }; 88 + };
+3
Documentation/devicetree/bindings/iio/accel/kionix,kxcjk1013.yaml
··· 21 21 reg: 22 22 maxItems: 1 23 23 24 + interrupts: 25 + maxItems: 1 26 + 24 27 vdd-supply: true 25 28 vddio-supply: true 26 29
+48 -3
Documentation/devicetree/bindings/iio/adc/adi,ad7949.yaml
··· 26 26 reg: 27 27 maxItems: 1 28 28 29 + vrefin-supply: 30 + description: 31 + Buffered ADC reference voltage supply. 32 + 29 33 vref-supply: 30 34 description: 31 - ADC reference voltage supply 35 + Unbuffered ADC reference voltage supply. 36 + 37 + adi,internal-ref-microvolt: 38 + description: | 39 + Internal reference voltage selection in microvolts. 40 + 41 + If no internal reference is specified, the channel will default to the 42 + external reference defined by vrefin-supply (or vref-supply). 43 + vrefin-supply will take precedence over vref-supply if both are defined. 44 + 45 + If no supplies are defined, the reference selection will default to 46 + 4096mV internal reference. 47 + 48 + enum: [2500000, 4096000] 49 + default: 4096000 50 + 32 51 33 52 spi-max-frequency: true 34 53 35 - "#io-channel-cells": 54 + '#io-channel-cells': 36 55 const: 1 56 + 57 + '#address-cells': 58 + const: 1 59 + 60 + '#size-cells': 61 + const: 0 37 62 38 63 required: 39 64 - compatible 40 65 - reg 41 - - vref-supply 42 66 43 67 additionalProperties: false 44 68 ··· 73 49 #size-cells = <0>; 74 50 75 51 adc@0 { 52 + #address-cells = <1>; 53 + #size-cells = <0>; 54 + 76 55 compatible = "adi,ad7949"; 77 56 reg = <0>; 78 57 vref-supply = <&vdd_supply>; 58 + }; 59 + 60 + adc@1 { 61 + #address-cells = <1>; 62 + #size-cells = <0>; 63 + 64 + compatible = "adi,ad7949"; 65 + reg = <1>; 66 + vrefin-supply = <&vdd_supply>; 67 + }; 68 + 69 + adc@2 { 70 + #address-cells = <1>; 71 + #size-cells = <0>; 72 + 73 + compatible = "adi,ad7949"; 74 + reg = <2>; 75 + adi,internal-ref-microvolt = <4096000>; 79 76 }; 80 77 }; 81 78 ...
+73
Documentation/devicetree/bindings/iio/adc/adi,ad799x.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/adc/adi,ad799x.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices AD799x analog to digital converters 8 + 9 + maintainers: 10 + - Michael Hennerich <Michael.Hennerich@analog.com> 11 + 12 + description: | 13 + Support for Analog Devices AD7991, AD7992, AD7993, AD7994, AD7995, AD7997, AD7998, 14 + AD7999 and similar analog to digital converters. 15 + Specifications on the converters can be found at: 16 + AD7991, AD7995, AD7999: 17 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7991_7995_7999.pdf 18 + AD7992: 19 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7992.pdf 20 + AD7993, AD7994: 21 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7993_7994.pdf 22 + AD7997, AD7998: 23 + https://www.analog.com/media/en/technical-documentation/data-sheets/AD7997_7998.pdf 24 + 25 + properties: 26 + compatible: 27 + enum: 28 + - adi,ad7991 29 + - adi,ad7992 30 + - adi,ad7993 31 + - adi,ad7994 32 + - adi,ad7995 33 + - adi,ad7997 34 + - adi,ad7998 35 + - adi,ad7999 36 + 37 + reg: 38 + maxItems: 1 39 + 40 + interrupts: 41 + maxItems: 1 42 + 43 + vcc-supply: 44 + description: 45 + ADC power supply 46 + 47 + vref-supply: 48 + description: 49 + ADC reference voltage supply, optional for AD7991, AD7995 and AD7999 50 + 51 + required: 52 + - compatible 53 + - reg 54 + 55 + additionalProperties: false 56 + 57 + examples: 58 + - | 59 + i2c { 60 + #address-cells = <1>; 61 + #size-cells = <0>; 62 + 63 + adc1: adc@28 { 64 + reg = <0x28>; 65 + compatible = "adi,ad7991"; 66 + interrupts = <13 2>; 67 + interrupt-parent = <&gpio6>; 68 + 69 + vcc-supply = <&vcc_3v3>; 70 + vref-supply = <&adc_vref>; 71 + }; 72 + }; 73 + ...
+100
Documentation/devicetree/bindings/iio/adc/aspeed,ast2600-adc.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/adc/aspeed,ast2600-adc.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: ADC that forms part of an ASPEED server management processor. 8 + 9 + maintainers: 10 + - Billy Tsai <billy_tsai@aspeedtech.com> 11 + 12 + description: | 13 + • 10-bits resolution for 16 voltage channels. 14 + • The device split into two individual engine and each contains 8 voltage 15 + channels. 16 + • Channel scanning can be non-continuous. 17 + • Programmable ADC clock frequency. 18 + • Programmable upper and lower threshold for each channels. 19 + • Interrupt when larger or less than threshold for each channels. 20 + • Support hysteresis for each channels. 21 + • Built-in a compensating method. 22 + • Built-in a register to trim internal reference voltage. 23 + • Internal or External reference voltage. 24 + • Support 2 Internal reference voltage 1.2v or 2.5v. 25 + • Integrate dividing circuit for battery sensing. 26 + 27 + properties: 28 + compatible: 29 + enum: 30 + - aspeed,ast2600-adc0 31 + - aspeed,ast2600-adc1 32 + description: 33 + Their trimming data, which is used to calibrate internal reference volage, 34 + locates in different address of OTP. 35 + 36 + reg: 37 + maxItems: 1 38 + 39 + clocks: 40 + maxItems: 1 41 + description: 42 + Input clock used to derive the sample clock. Expected to be the 43 + SoC's APB clock. 44 + 45 + resets: 46 + maxItems: 1 47 + 48 + "#io-channel-cells": 49 + const: 1 50 + 51 + vref-supply: 52 + description: 53 + The external regulator supply ADC reference voltage. 54 + 55 + aspeed,int-vref-microvolt: 56 + enum: [1200000, 2500000] 57 + description: 58 + ADC internal reference voltage in microvolts. 59 + 60 + aspeed,battery-sensing: 61 + type: boolean 62 + description: 63 + Inform the driver that last channel will be used to sensor battery. 64 + 65 + aspeed,trim-data-valid: 66 + type: boolean 67 + description: | 68 + The ADC reference voltage can be calibrated to obtain the trimming 69 + data which will be stored in otp. This property informs the driver that 70 + the data store in the otp is valid. 71 + 72 + required: 73 + - compatible 74 + - reg 75 + - clocks 76 + - resets 77 + - "#io-channel-cells" 78 + 79 + additionalProperties: false 80 + 81 + examples: 82 + - | 83 + #include <dt-bindings/clock/ast2600-clock.h> 84 + adc0: adc@1e6e9000 { 85 + compatible = "aspeed,ast2600-adc0"; 86 + reg = <0x1e6e9000 0x100>; 87 + clocks = <&syscon ASPEED_CLK_APB2>; 88 + resets = <&syscon ASPEED_RESET_ADC>; 89 + #io-channel-cells = <1>; 90 + aspeed,int-vref-microvolt = <2500000>; 91 + }; 92 + adc1: adc@1e6e9100 { 93 + compatible = "aspeed,ast2600-adc1"; 94 + reg = <0x1e6e9100 0x100>; 95 + clocks = <&syscon ASPEED_CLK_APB2>; 96 + resets = <&syscon ASPEED_RESET_ADC>; 97 + #io-channel-cells = <1>; 98 + aspeed,int-vref-microvolt = <2500000>; 99 + }; 100 + ...
+1
Documentation/devicetree/bindings/iio/adc/atmel,sama5d2-adc.yaml
··· 15 15 enum: 16 16 - atmel,sama5d2-adc 17 17 - microchip,sam9x60-adc 18 + - microchip,sama7g5-adc 18 19 19 20 reg: 20 21 maxItems: 1
+78
Documentation/devicetree/bindings/iio/adc/nxp,imx8qxp-adc.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/nxp,imx8qxp-adc.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: NXP IMX8QXP ADC bindings 8 + 9 + maintainers: 10 + - Cai Huoqing <caihuoqing@baidu.com> 11 + 12 + description: 13 + Supports the ADC found on the IMX8QXP SoC. 14 + 15 + properties: 16 + compatible: 17 + const: nxp,imx8qxp-adc 18 + 19 + reg: 20 + maxItems: 1 21 + 22 + interrupts: 23 + maxItems: 1 24 + 25 + clocks: 26 + maxItems: 2 27 + 28 + clock-names: 29 + items: 30 + - const: per 31 + - const: ipg 32 + 33 + assigned-clocks: 34 + maxItems: 1 35 + 36 + assigned-clock-rates: 37 + maxItems: 1 38 + 39 + power-domains: 40 + maxItems: 1 41 + 42 + "#io-channel-cells": 43 + const: 1 44 + 45 + required: 46 + - compatible 47 + - reg 48 + - interrupts 49 + - clocks 50 + - clock-names 51 + - assigned-clocks 52 + - assigned-clock-rates 53 + - power-domains 54 + - "#io-channel-cells" 55 + 56 + additionalProperties: false 57 + 58 + examples: 59 + - | 60 + #include <dt-bindings/interrupt-controller/arm-gic.h> 61 + #include <dt-bindings/firmware/imx/rsrc.h> 62 + soc { 63 + #address-cells = <2>; 64 + #size-cells = <2>; 65 + adc@5a880000 { 66 + compatible = "nxp,imx8qxp-adc"; 67 + reg = <0x0 0x5a880000 0x0 0x10000>; 68 + interrupts = <GIC_SPI 240 IRQ_TYPE_LEVEL_HIGH>; 69 + clocks = <&clk IMX_SC_R_ADC_0>, 70 + <&clk IMX_SC_R_ADC_0>; 71 + clock-names = "per", "ipg"; 72 + assigned-clocks = <&clk IMX_SC_R_ADC_0>; 73 + assigned-clock-rates = <24000000>; 74 + power-domains = <&pd IMX_SC_R_ADC_0>; 75 + #io-channel-cells = <1>; 76 + }; 77 + }; 78 + ...
+55
Documentation/devicetree/bindings/iio/chemical/senseair,sunrise.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/chemical/senseair,sunrise.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Senseair Sunrise 006-0-0007 CO2 Sensor 8 + 9 + maintainers: 10 + - Jacopo Mondi <jacopo@jmondi.org> 11 + 12 + description: | 13 + Senseair Sunrise 006-0-0007 is a NDIR CO2 sensor. It supports I2C or UART buses 14 + for communications and control. 15 + 16 + Datasheets: 17 + https://rmtplusstoragesenseair.blob.core.windows.net/docs/Dev/publicerat/PSP11704.pdf 18 + https://rmtplusstoragesenseair.blob.core.windows.net/docs/Dev/publicerat/PSH11649.pdf 19 + https://rmtplusstoragesenseair.blob.core.windows.net/docs/Dev/publicerat/TDE5531.pdf 20 + https://rmtplusstoragesenseair.blob.core.windows.net/docs/Market/publicerat/TDE7318.pdf 21 + 22 + properties: 23 + compatible: 24 + const: senseair,sunrise-006-0-0007 25 + 26 + reg: 27 + maxItems: 1 28 + 29 + ndry-gpios: 30 + maxItems: 1 31 + description: 32 + Phandle to the GPIO line connected to the nDRY pin. Typically active low. 33 + 34 + en-gpios: 35 + maxItems: 1 36 + description: 37 + Phandle to the GPIO line connected to the EN pin. Typically active high. 38 + 39 + required: 40 + - compatible 41 + - reg 42 + 43 + additionalProperties: false 44 + 45 + examples: 46 + - | 47 + i2c { 48 + #address-cells = <1>; 49 + #size-cells = <0>; 50 + 51 + co2-sensor@68 { 52 + compatible = "senseair,sunrise-006-0-0007"; 53 + reg = <0x68>; 54 + }; 55 + };
+46
Documentation/devicetree/bindings/iio/chemical/sensirion,scd4x.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/chemical/sensirion,scd4x.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Sensirion SCD4X carbon dioxide sensor 8 + 9 + maintainers: 10 + - Roan van Dijk <roan@protonic.nl> 11 + 12 + description: | 13 + Air quality sensor capable of measuring co2 concentration, temperature 14 + and relative humidity. 15 + 16 + properties: 17 + compatible: 18 + enum: 19 + - sensirion,scd40 20 + - sensirion,scd41 21 + 22 + reg: 23 + maxItems: 1 24 + 25 + interrupts: 26 + maxItems: 1 27 + 28 + vdd-supply: true 29 + 30 + required: 31 + - compatible 32 + - reg 33 + 34 + additionalProperties: false 35 + 36 + examples: 37 + - | 38 + i2c { 39 + #address-cells = <1>; 40 + #size-cells = <0>; 41 + 42 + co2-sensor@62 { 43 + compatible = "sensirion,scd41"; 44 + reg = <0x62>; 45 + }; 46 + };
+7
Documentation/devicetree/bindings/iio/magnetometer/asahi-kasei,ak8975.yaml
··· 17 17 - asahi-kasei,ak8963 18 18 - asahi-kasei,ak09911 19 19 - asahi-kasei,ak09912 20 + - asahi-kasei,ak09916 20 21 - enum: 21 22 - ak8975 22 23 - ak8963 23 24 - ak09911 24 25 - ak09912 26 + - ak09916 25 27 deprecated: true 26 28 27 29 reg: ··· 43 41 vdd-supply: 44 42 description: | 45 43 an optional regulator that needs to be on to provide VDD power to 44 + the sensor. 45 + 46 + vid-supply: 47 + description: | 48 + an optional regulator that needs to be on to provide VID power to 46 49 the sensor. 47 50 48 51 mount-matrix:
+52
Documentation/devicetree/bindings/iio/temperature/maxim,max31865.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/temperature/maxim,max31865.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Maxim MAX31865 Resistance Temperature Detector. 8 + 9 + maintainers: 10 + - Navin Sankar Velliangiri <navin@linumiz.com> 11 + 12 + description: | 13 + https://datasheets.maximintegrated.com/en/ds/MAX31865.pdf 14 + 15 + properties: 16 + compatible: 17 + const: maxim,max31865 18 + 19 + reg: 20 + maxItems: 1 21 + 22 + maxim,3-wire: 23 + description: 24 + Identifies the number of wires used by the RTD. Setting this property 25 + enables 3-wire RTD connection. Else 2-wire or 4-wire RTD connection. 26 + type: boolean 27 + 28 + spi-max-frequency: true 29 + spi-cpha: true 30 + 31 + required: 32 + - compatible 33 + - reg 34 + - spi-cpha 35 + 36 + additionalProperties: false 37 + 38 + examples: 39 + - | 40 + spi { 41 + #address-cells = <1>; 42 + #size-cells = <0>; 43 + 44 + temp_sensor@0 { 45 + compatible = "maxim,max31865"; 46 + reg = <0>; 47 + spi-max-frequency = <400000>; 48 + spi-cpha; 49 + maxim,3-wire; 50 + }; 51 + }; 52 + ...
+2
Documentation/devicetree/bindings/vendor-prefixes.yaml
··· 1018 1018 description: Shenzhen SEI Robotics Co., Ltd 1019 1019 "^semtech,.*": 1020 1020 description: Semtech Corporation 1021 + "^senseair,.*": 1022 + description: Senseair AB 1021 1023 "^sensirion,.*": 1022 1024 description: Sensirion AG 1023 1025 "^sensortek,.*":
+1
Documentation/driver-api/driver-model/devres.rst
··· 287 287 devm_iio_device_register() 288 288 devm_iio_dmaengine_buffer_setup() 289 289 devm_iio_kfifo_buffer_setup() 290 + devm_iio_map_array_register() 290 291 devm_iio_triggered_buffer_setup() 291 292 devm_iio_trigger_alloc() 292 293 devm_iio_trigger_register()
+36
MAINTAINERS
··· 591 591 S: Maintained 592 592 F: drivers/platform/x86/adv_swbutton.c 593 593 594 + ADXL313 THREE-AXIS DIGITAL ACCELEROMETER DRIVER 595 + M: Lucas Stankus <lucas.p.stankus@gmail.com> 596 + S: Supported 597 + F: Documentation/devicetree/bindings/iio/accel/adi,adxl313.yaml 598 + F: drivers/iio/accel/adxl313* 599 + 594 600 ADXL34X THREE-AXIS DIGITAL ACCELEROMETER DRIVER (ADXL345/ADXL346) 595 601 M: Michael Hennerich <michael.hennerich@analog.com> 596 602 S: Supported ··· 604 598 W: http://ez.analog.com/community/linux-device-drivers 605 599 F: Documentation/devicetree/bindings/iio/accel/adi,adxl345.yaml 606 600 F: drivers/input/misc/adxl34x.c 601 + 602 + ADXL355 THREE-AXIS DIGITAL ACCELEROMETER DRIVER 603 + M: Puranjay Mohan <puranjay12@gmail.com> 604 + L: linux-iio@vger.kernel.org 605 + S: Supported 606 + F: Documentation/devicetree/bindings/iio/accel/adi,adxl355.yaml 607 + F: drivers/iio/accel/adxl355.h 608 + F: drivers/iio/accel/adxl355_core.c 609 + F: drivers/iio/accel/adxl355_i2c.c 610 + F: drivers/iio/accel/adxl355_spi.c 607 611 608 612 ADXL372 THREE-AXIS DIGITAL ACCELEROMETER DRIVER 609 613 M: Michael Hennerich <michael.hennerich@analog.com> ··· 13488 13472 F: Documentation/devicetree/bindings/display/imx/nxp,imx8mq-dcss.yaml 13489 13473 F: drivers/gpu/drm/imx/dcss/ 13490 13474 13475 + NXP i.MX 8QXP ADC DRIVER 13476 + M: Cai Huoqing <caihuoqing@baidu.com> 13477 + L: linux-iio@vger.kernel.org 13478 + S: Supported 13479 + F: Documentation/devicetree/bindings/iio/adc/nxp,imx8qxp-adc.yaml 13480 + F: drivers/iio/adc/imx8qxp-adc.c 13481 + 13491 13482 NXP PF8100/PF8121A/PF8200 PMIC REGULATOR DEVICE DRIVER 13492 13483 M: Jagan Teki <jagan@amarulasolutions.com> 13493 13484 S: Maintained ··· 16895 16872 F: drivers/misc/phantom.c 16896 16873 F: include/uapi/linux/phantom.h 16897 16874 16875 + SENSEAIR SUNRISE 006-0-0007 16876 + M: Jacopo Mondi <jacopo@jmondi.org> 16877 + S: Maintained 16878 + F: Documentation/ABI/testing/sysfs-bus-iio-chemical-sunrise-co2 16879 + F: Documentation/devicetree/bindings/iio/chemical/senseair,sunrise.yaml 16880 + F: drivers/iio/chemical/sunrise_co2.c 16881 + 16898 16882 SENSIRION SCD30 CARBON DIOXIDE SENSOR DRIVER 16899 16883 M: Tomasz Duszynski <tomasz.duszynski@octakon.com> 16900 16884 S: Maintained ··· 16910 16880 F: drivers/iio/chemical/scd30_core.c 16911 16881 F: drivers/iio/chemical/scd30_i2c.c 16912 16882 F: drivers/iio/chemical/scd30_serial.c 16883 + 16884 + SENSIRION SCD4X CARBON DIOXIDE SENSOR DRIVER 16885 + M: Roan van Dijk <roan@protonic.nl> 16886 + S: Maintained 16887 + F: Documentation/devicetree/bindings/iio/chemical/sensirion,scd4x.yaml 16888 + F: drivers/iio/chemical/scd4x.c 16913 16889 16914 16890 SENSIRION SGP40 GAS SENSOR DRIVER 16915 16891 M: Andreas Klinger <ak@it-klinger.de>
+62
drivers/iio/accel/Kconfig
··· 30 30 To compile this driver as a module, say M here: the module will be 31 31 called adis16209. 32 32 33 + config ADXL313 34 + tristate 35 + 36 + config ADXL313_I2C 37 + tristate "Analog Devices ADXL313 3-Axis Digital Accelerometer I2C Driver" 38 + depends on I2C 39 + select ADXL313 40 + select REGMAP_I2C 41 + help 42 + Say Y here if you want to build i2c support for the Analog Devices 43 + ADXL313 3-axis digital accelerometer. 44 + 45 + To compile this driver as a module, choose M here: the module 46 + will be called adxl313_i2c and you will also get adxl313_core 47 + for the core module. 48 + 49 + config ADXL313_SPI 50 + tristate "Analog Devices ADXL313 3-Axis Digital Accelerometer SPI Driver" 51 + depends on SPI 52 + select ADXL313 53 + select REGMAP_SPI 54 + help 55 + Say Y here if you want to build spi support for the Analog Devices 56 + ADXL313 3-axis digital accelerometer. 57 + 58 + To compile this driver as a module, choose M here: the module 59 + will be called adxl313_spi and you will also get adxl313_core 60 + for the core module. 61 + 33 62 config ADXL345 34 63 tristate 35 64 ··· 88 59 89 60 To compile this driver as a module, choose M here: the module 90 61 will be called adxl345_spi and you will also get adxl345_core 62 + for the core module. 63 + 64 + config ADXL355 65 + tristate 66 + 67 + config ADXL355_I2C 68 + tristate "Analog Devices ADXL355 3-Axis Digital Accelerometer I2C Driver" 69 + depends on I2C 70 + select ADXL355 71 + select REGMAP_I2C 72 + select IIO_BUFFER 73 + select IIO_TRIGGERED_BUFFER 74 + help 75 + Say Y here if you want to build i2c support for the Analog Devices 76 + ADXL355 3-axis digital accelerometer. 77 + 78 + To compile this driver as a module, choose M here: the module 79 + will be called adxl355_i2c and you will also get adxl355_core 80 + for the core module. 81 + 82 + config ADXL355_SPI 83 + tristate "Analog Devices ADXL355 3-Axis Digital Accelerometer SPI Driver" 84 + depends on SPI 85 + select ADXL355 86 + select REGMAP_SPI 87 + select IIO_BUFFER 88 + select IIO_TRIGGERED_BUFFER 89 + help 90 + Say Y here if you want to build spi support for the Analog Devices 91 + ADXL355 3-axis digital accelerometer. 92 + 93 + To compile this driver as a module, choose M here: the module 94 + will be called adxl355_spi and you will also get adxl355_core 91 95 for the core module. 92 96 93 97 config ADXL372
+6
drivers/iio/accel/Makefile
··· 6 6 # When adding new entries keep the list in alphabetical order 7 7 obj-$(CONFIG_ADIS16201) += adis16201.o 8 8 obj-$(CONFIG_ADIS16209) += adis16209.o 9 + obj-$(CONFIG_ADXL313) += adxl313_core.o 10 + obj-$(CONFIG_ADXL313_I2C) += adxl313_i2c.o 11 + obj-$(CONFIG_ADXL313_SPI) += adxl313_spi.o 9 12 obj-$(CONFIG_ADXL345) += adxl345_core.o 10 13 obj-$(CONFIG_ADXL345_I2C) += adxl345_i2c.o 11 14 obj-$(CONFIG_ADXL345_SPI) += adxl345_spi.o 15 + obj-$(CONFIG_ADXL355) += adxl355_core.o 16 + obj-$(CONFIG_ADXL355_I2C) += adxl355_i2c.o 17 + obj-$(CONFIG_ADXL355_SPI) += adxl355_spi.o 12 18 obj-$(CONFIG_ADXL372) += adxl372.o 13 19 obj-$(CONFIG_ADXL372_I2C) += adxl372_i2c.o 14 20 obj-$(CONFIG_ADXL372_SPI) += adxl372_spi.o
+54
drivers/iio/accel/adxl313.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * ADXL313 3-Axis Digital Accelerometer 4 + * 5 + * Copyright (c) 2021 Lucas Stankus <lucas.p.stankus@gmail.com> 6 + */ 7 + 8 + #ifndef _ADXL313_H_ 9 + #define _ADXL313_H_ 10 + 11 + /* ADXL313 register definitions */ 12 + #define ADXL313_REG_DEVID0 0x00 13 + #define ADXL313_REG_DEVID1 0x01 14 + #define ADXL313_REG_PARTID 0x02 15 + #define ADXL313_REG_XID 0x04 16 + #define ADXL313_REG_SOFT_RESET 0x18 17 + #define ADXL313_REG_OFS_AXIS(index) (0x1E + (index)) 18 + #define ADXL313_REG_THRESH_ACT 0x24 19 + #define ADXL313_REG_ACT_INACT_CTL 0x27 20 + #define ADXL313_REG_BW_RATE 0x2C 21 + #define ADXL313_REG_POWER_CTL 0x2D 22 + #define ADXL313_REG_INT_MAP 0x2F 23 + #define ADXL313_REG_DATA_FORMAT 0x31 24 + #define ADXL313_REG_DATA_AXIS(index) (0x32 + ((index) * 2)) 25 + #define ADXL313_REG_FIFO_CTL 0x38 26 + #define ADXL313_REG_FIFO_STATUS 0x39 27 + 28 + #define ADXL313_DEVID0 0xAD 29 + #define ADXL313_DEVID1 0x1D 30 + #define ADXL313_PARTID 0xCB 31 + #define ADXL313_SOFT_RESET 0x52 32 + 33 + #define ADXL313_RATE_MSK GENMASK(3, 0) 34 + #define ADXL313_RATE_BASE 6 35 + 36 + #define ADXL313_POWER_CTL_MSK GENMASK(3, 2) 37 + #define ADXL313_MEASUREMENT_MODE BIT(3) 38 + 39 + #define ADXL313_RANGE_MSK GENMASK(1, 0) 40 + #define ADXL313_RANGE_4G 3 41 + 42 + #define ADXL313_FULL_RES BIT(3) 43 + #define ADXL313_SPI_3WIRE BIT(6) 44 + #define ADXL313_I2C_DISABLE BIT(6) 45 + 46 + extern const struct regmap_access_table adxl313_readable_regs_table; 47 + 48 + extern const struct regmap_access_table adxl313_writable_regs_table; 49 + 50 + int adxl313_core_probe(struct device *dev, 51 + struct regmap *regmap, 52 + const char *name, 53 + int (*setup)(struct device *, struct regmap *)); 54 + #endif /* _ADXL313_H_ */
+332
drivers/iio/accel/adxl313_core.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * ADXL313 3-Axis Digital Accelerometer 4 + * 5 + * Copyright (c) 2021 Lucas Stankus <lucas.p.stankus@gmail.com> 6 + * 7 + * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL313.pdf 8 + */ 9 + 10 + #include <linux/bitfield.h> 11 + #include <linux/iio/iio.h> 12 + #include <linux/module.h> 13 + #include <linux/regmap.h> 14 + 15 + #include "adxl313.h" 16 + 17 + static const struct regmap_range adxl313_readable_reg_range[] = { 18 + regmap_reg_range(ADXL313_REG_DEVID0, ADXL313_REG_XID), 19 + regmap_reg_range(ADXL313_REG_SOFT_RESET, ADXL313_REG_SOFT_RESET), 20 + regmap_reg_range(ADXL313_REG_OFS_AXIS(0), ADXL313_REG_OFS_AXIS(2)), 21 + regmap_reg_range(ADXL313_REG_THRESH_ACT, ADXL313_REG_ACT_INACT_CTL), 22 + regmap_reg_range(ADXL313_REG_BW_RATE, ADXL313_REG_FIFO_STATUS), 23 + }; 24 + 25 + const struct regmap_access_table adxl313_readable_regs_table = { 26 + .yes_ranges = adxl313_readable_reg_range, 27 + .n_yes_ranges = ARRAY_SIZE(adxl313_readable_reg_range), 28 + }; 29 + EXPORT_SYMBOL_GPL(adxl313_readable_regs_table); 30 + 31 + static const struct regmap_range adxl313_writable_reg_range[] = { 32 + regmap_reg_range(ADXL313_REG_SOFT_RESET, ADXL313_REG_SOFT_RESET), 33 + regmap_reg_range(ADXL313_REG_OFS_AXIS(0), ADXL313_REG_OFS_AXIS(2)), 34 + regmap_reg_range(ADXL313_REG_THRESH_ACT, ADXL313_REG_ACT_INACT_CTL), 35 + regmap_reg_range(ADXL313_REG_BW_RATE, ADXL313_REG_INT_MAP), 36 + regmap_reg_range(ADXL313_REG_DATA_FORMAT, ADXL313_REG_DATA_FORMAT), 37 + regmap_reg_range(ADXL313_REG_FIFO_CTL, ADXL313_REG_FIFO_CTL), 38 + }; 39 + 40 + const struct regmap_access_table adxl313_writable_regs_table = { 41 + .yes_ranges = adxl313_writable_reg_range, 42 + .n_yes_ranges = ARRAY_SIZE(adxl313_writable_reg_range), 43 + }; 44 + EXPORT_SYMBOL_GPL(adxl313_writable_regs_table); 45 + 46 + struct adxl313_data { 47 + struct regmap *regmap; 48 + struct mutex lock; /* lock to protect transf_buf */ 49 + __le16 transf_buf ____cacheline_aligned; 50 + }; 51 + 52 + static const int adxl313_odr_freqs[][2] = { 53 + [0] = { 6, 250000 }, 54 + [1] = { 12, 500000 }, 55 + [2] = { 25, 0 }, 56 + [3] = { 50, 0 }, 57 + [4] = { 100, 0 }, 58 + [5] = { 200, 0 }, 59 + [6] = { 400, 0 }, 60 + [7] = { 800, 0 }, 61 + [8] = { 1600, 0 }, 62 + [9] = { 3200, 0 }, 63 + }; 64 + 65 + #define ADXL313_ACCEL_CHANNEL(index, axis) { \ 66 + .type = IIO_ACCEL, \ 67 + .address = index, \ 68 + .modified = 1, \ 69 + .channel2 = IIO_MOD_##axis, \ 70 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 71 + BIT(IIO_CHAN_INFO_CALIBBIAS), \ 72 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 73 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 74 + .info_mask_shared_by_type_available = \ 75 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 76 + .scan_type = { \ 77 + .realbits = 13, \ 78 + }, \ 79 + } 80 + 81 + static const struct iio_chan_spec adxl313_channels[] = { 82 + ADXL313_ACCEL_CHANNEL(0, X), 83 + ADXL313_ACCEL_CHANNEL(1, Y), 84 + ADXL313_ACCEL_CHANNEL(2, Z), 85 + }; 86 + 87 + static int adxl313_set_odr(struct adxl313_data *data, 88 + unsigned int freq1, unsigned int freq2) 89 + { 90 + unsigned int i; 91 + 92 + for (i = 0; i < ARRAY_SIZE(adxl313_odr_freqs); i++) { 93 + if (adxl313_odr_freqs[i][0] == freq1 && 94 + adxl313_odr_freqs[i][1] == freq2) 95 + break; 96 + } 97 + 98 + if (i == ARRAY_SIZE(adxl313_odr_freqs)) 99 + return -EINVAL; 100 + 101 + return regmap_update_bits(data->regmap, ADXL313_REG_BW_RATE, 102 + ADXL313_RATE_MSK, 103 + FIELD_PREP(ADXL313_RATE_MSK, ADXL313_RATE_BASE + i)); 104 + } 105 + 106 + static int adxl313_read_axis(struct adxl313_data *data, 107 + struct iio_chan_spec const *chan) 108 + { 109 + int ret; 110 + 111 + mutex_lock(&data->lock); 112 + 113 + ret = regmap_bulk_read(data->regmap, 114 + ADXL313_REG_DATA_AXIS(chan->address), 115 + &data->transf_buf, sizeof(data->transf_buf)); 116 + if (ret) 117 + goto unlock_ret; 118 + 119 + ret = le16_to_cpu(data->transf_buf); 120 + 121 + unlock_ret: 122 + mutex_unlock(&data->lock); 123 + return ret; 124 + } 125 + 126 + static int adxl313_read_freq_avail(struct iio_dev *indio_dev, 127 + struct iio_chan_spec const *chan, 128 + const int **vals, int *type, int *length, 129 + long mask) 130 + { 131 + switch (mask) { 132 + case IIO_CHAN_INFO_SAMP_FREQ: 133 + *vals = (const int *)adxl313_odr_freqs; 134 + *length = ARRAY_SIZE(adxl313_odr_freqs) * 2; 135 + *type = IIO_VAL_INT_PLUS_MICRO; 136 + return IIO_AVAIL_LIST; 137 + default: 138 + return -EINVAL; 139 + } 140 + } 141 + 142 + static int adxl313_read_raw(struct iio_dev *indio_dev, 143 + struct iio_chan_spec const *chan, 144 + int *val, int *val2, long mask) 145 + { 146 + struct adxl313_data *data = iio_priv(indio_dev); 147 + unsigned int regval; 148 + int ret; 149 + 150 + switch (mask) { 151 + case IIO_CHAN_INFO_RAW: 152 + ret = adxl313_read_axis(data, chan); 153 + if (ret < 0) 154 + return ret; 155 + 156 + *val = sign_extend32(ret, chan->scan_type.realbits - 1); 157 + return IIO_VAL_INT; 158 + case IIO_CHAN_INFO_SCALE: 159 + /* 160 + * Scale for any g range is given in datasheet as 161 + * 1024 LSB/g = 0.0009765625 * 9.80665 = 0.009576806640625 m/s^2 162 + */ 163 + *val = 0; 164 + *val2 = 9576806; 165 + return IIO_VAL_INT_PLUS_NANO; 166 + case IIO_CHAN_INFO_CALIBBIAS: 167 + ret = regmap_read(data->regmap, 168 + ADXL313_REG_OFS_AXIS(chan->address), &regval); 169 + if (ret) 170 + return ret; 171 + 172 + /* 173 + * 8-bit resolution at +/- 0.5g, that is 4x accel data scale 174 + * factor at full resolution 175 + */ 176 + *val = sign_extend32(regval, 7) * 4; 177 + return IIO_VAL_INT; 178 + case IIO_CHAN_INFO_SAMP_FREQ: 179 + ret = regmap_read(data->regmap, ADXL313_REG_BW_RATE, &regval); 180 + if (ret) 181 + return ret; 182 + 183 + ret = FIELD_GET(ADXL313_RATE_MSK, regval) - ADXL313_RATE_BASE; 184 + *val = adxl313_odr_freqs[ret][0]; 185 + *val2 = adxl313_odr_freqs[ret][1]; 186 + return IIO_VAL_INT_PLUS_MICRO; 187 + default: 188 + return -EINVAL; 189 + } 190 + } 191 + 192 + static int adxl313_write_raw(struct iio_dev *indio_dev, 193 + struct iio_chan_spec const *chan, 194 + int val, int val2, long mask) 195 + { 196 + struct adxl313_data *data = iio_priv(indio_dev); 197 + 198 + switch (mask) { 199 + case IIO_CHAN_INFO_CALIBBIAS: 200 + /* 201 + * 8-bit resolution at +/- 0.5g, that is 4x accel data scale 202 + * factor at full resolution 203 + */ 204 + if (clamp_val(val, -128 * 4, 127 * 4) != val) 205 + return -EINVAL; 206 + 207 + return regmap_write(data->regmap, 208 + ADXL313_REG_OFS_AXIS(chan->address), 209 + val / 4); 210 + case IIO_CHAN_INFO_SAMP_FREQ: 211 + return adxl313_set_odr(data, val, val2); 212 + default: 213 + return -EINVAL; 214 + } 215 + } 216 + 217 + static const struct iio_info adxl313_info = { 218 + .read_raw = adxl313_read_raw, 219 + .write_raw = adxl313_write_raw, 220 + .read_avail = adxl313_read_freq_avail, 221 + }; 222 + 223 + static int adxl313_setup(struct device *dev, struct adxl313_data *data, 224 + int (*setup)(struct device *, struct regmap *)) 225 + { 226 + unsigned int regval; 227 + int ret; 228 + 229 + /* Ensures the device is in a consistent state after start up */ 230 + ret = regmap_write(data->regmap, ADXL313_REG_SOFT_RESET, 231 + ADXL313_SOFT_RESET); 232 + if (ret) 233 + return ret; 234 + 235 + if (setup) { 236 + ret = setup(dev, data->regmap); 237 + if (ret) 238 + return ret; 239 + } 240 + 241 + ret = regmap_read(data->regmap, ADXL313_REG_DEVID0, &regval); 242 + if (ret) 243 + return ret; 244 + 245 + if (regval != ADXL313_DEVID0) { 246 + dev_err(dev, "Invalid manufacturer ID: 0x%02x\n", regval); 247 + return -ENODEV; 248 + } 249 + 250 + ret = regmap_read(data->regmap, ADXL313_REG_DEVID1, &regval); 251 + if (ret) 252 + return ret; 253 + 254 + if (regval != ADXL313_DEVID1) { 255 + dev_err(dev, "Invalid mems ID: 0x%02x\n", regval); 256 + return -ENODEV; 257 + } 258 + 259 + ret = regmap_read(data->regmap, ADXL313_REG_PARTID, &regval); 260 + if (ret) 261 + return ret; 262 + 263 + if (regval != ADXL313_PARTID) { 264 + dev_err(dev, "Invalid device ID: 0x%02x\n", regval); 265 + return -ENODEV; 266 + } 267 + 268 + /* Sets the range to +/- 4g */ 269 + ret = regmap_update_bits(data->regmap, ADXL313_REG_DATA_FORMAT, 270 + ADXL313_RANGE_MSK, 271 + FIELD_PREP(ADXL313_RANGE_MSK, ADXL313_RANGE_4G)); 272 + if (ret) 273 + return ret; 274 + 275 + /* Enables full resolution */ 276 + ret = regmap_update_bits(data->regmap, ADXL313_REG_DATA_FORMAT, 277 + ADXL313_FULL_RES, ADXL313_FULL_RES); 278 + if (ret) 279 + return ret; 280 + 281 + /* Enables measurement mode */ 282 + return regmap_update_bits(data->regmap, ADXL313_REG_POWER_CTL, 283 + ADXL313_POWER_CTL_MSK, 284 + ADXL313_MEASUREMENT_MODE); 285 + } 286 + 287 + /** 288 + * adxl313_core_probe() - probe and setup for adxl313 accelerometer 289 + * @dev: Driver model representation of the device 290 + * @regmap: Register map of the device 291 + * @name: Device name buffer reference 292 + * @setup: Setup routine to be executed right before the standard device 293 + * setup, can also be set to NULL if not required 294 + * 295 + * Return: 0 on success, negative errno on error cases 296 + */ 297 + int adxl313_core_probe(struct device *dev, 298 + struct regmap *regmap, 299 + const char *name, 300 + int (*setup)(struct device *, struct regmap *)) 301 + { 302 + struct adxl313_data *data; 303 + struct iio_dev *indio_dev; 304 + int ret; 305 + 306 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 307 + if (!indio_dev) 308 + return -ENOMEM; 309 + 310 + data = iio_priv(indio_dev); 311 + data->regmap = regmap; 312 + mutex_init(&data->lock); 313 + 314 + indio_dev->name = name; 315 + indio_dev->info = &adxl313_info; 316 + indio_dev->modes = INDIO_DIRECT_MODE; 317 + indio_dev->channels = adxl313_channels; 318 + indio_dev->num_channels = ARRAY_SIZE(adxl313_channels); 319 + 320 + ret = adxl313_setup(dev, data, setup); 321 + if (ret) { 322 + dev_err(dev, "ADXL313 setup failed\n"); 323 + return ret; 324 + } 325 + 326 + return devm_iio_device_register(dev, indio_dev); 327 + } 328 + EXPORT_SYMBOL_GPL(adxl313_core_probe); 329 + 330 + MODULE_AUTHOR("Lucas Stankus <lucas.p.stankus@gmail.com>"); 331 + MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer core driver"); 332 + MODULE_LICENSE("GPL v2");
+66
drivers/iio/accel/adxl313_i2c.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * ADXL313 3-Axis Digital Accelerometer 4 + * 5 + * Copyright (c) 2021 Lucas Stankus <lucas.p.stankus@gmail.com> 6 + * 7 + * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL313.pdf 8 + */ 9 + 10 + #include <linux/i2c.h> 11 + #include <linux/mod_devicetable.h> 12 + #include <linux/module.h> 13 + #include <linux/regmap.h> 14 + 15 + #include "adxl313.h" 16 + 17 + static const struct regmap_config adxl313_i2c_regmap_config = { 18 + .reg_bits = 8, 19 + .val_bits = 8, 20 + .rd_table = &adxl313_readable_regs_table, 21 + .wr_table = &adxl313_writable_regs_table, 22 + .max_register = 0x39, 23 + }; 24 + 25 + static int adxl313_i2c_probe(struct i2c_client *client) 26 + { 27 + struct regmap *regmap; 28 + 29 + regmap = devm_regmap_init_i2c(client, &adxl313_i2c_regmap_config); 30 + if (IS_ERR(regmap)) { 31 + dev_err(&client->dev, "Error initializing i2c regmap: %ld\n", 32 + PTR_ERR(regmap)); 33 + return PTR_ERR(regmap); 34 + } 35 + 36 + return adxl313_core_probe(&client->dev, regmap, client->name, NULL); 37 + } 38 + 39 + static const struct i2c_device_id adxl313_i2c_id[] = { 40 + { "adxl313" }, 41 + { } 42 + }; 43 + 44 + MODULE_DEVICE_TABLE(i2c, adxl313_i2c_id); 45 + 46 + static const struct of_device_id adxl313_of_match[] = { 47 + { .compatible = "adi,adxl313" }, 48 + { } 49 + }; 50 + 51 + MODULE_DEVICE_TABLE(of, adxl313_of_match); 52 + 53 + static struct i2c_driver adxl313_i2c_driver = { 54 + .driver = { 55 + .name = "adxl313_i2c", 56 + .of_match_table = adxl313_of_match, 57 + }, 58 + .probe_new = adxl313_i2c_probe, 59 + .id_table = adxl313_i2c_id, 60 + }; 61 + 62 + module_i2c_driver(adxl313_i2c_driver); 63 + 64 + MODULE_AUTHOR("Lucas Stankus <lucas.p.stankus@gmail.com>"); 65 + MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer I2C driver"); 66 + MODULE_LICENSE("GPL v2");
+92
drivers/iio/accel/adxl313_spi.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * ADXL313 3-Axis Digital Accelerometer 4 + * 5 + * Copyright (c) 2021 Lucas Stankus <lucas.p.stankus@gmail.com> 6 + * 7 + * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL313.pdf 8 + */ 9 + 10 + #include <linux/mod_devicetable.h> 11 + #include <linux/module.h> 12 + #include <linux/regmap.h> 13 + #include <linux/spi/spi.h> 14 + 15 + #include "adxl313.h" 16 + 17 + static const struct regmap_config adxl313_spi_regmap_config = { 18 + .reg_bits = 8, 19 + .val_bits = 8, 20 + .rd_table = &adxl313_readable_regs_table, 21 + .wr_table = &adxl313_writable_regs_table, 22 + .max_register = 0x39, 23 + /* Setting bits 7 and 6 enables multiple-byte read */ 24 + .read_flag_mask = BIT(7) | BIT(6), 25 + }; 26 + 27 + static int adxl313_spi_setup(struct device *dev, struct regmap *regmap) 28 + { 29 + struct spi_device *spi = container_of(dev, struct spi_device, dev); 30 + int ret; 31 + 32 + if (spi->mode & SPI_3WIRE) { 33 + ret = regmap_write(regmap, ADXL313_REG_DATA_FORMAT, 34 + ADXL313_SPI_3WIRE); 35 + if (ret) 36 + return ret; 37 + } 38 + 39 + return regmap_update_bits(regmap, ADXL313_REG_POWER_CTL, 40 + ADXL313_I2C_DISABLE, ADXL313_I2C_DISABLE); 41 + } 42 + 43 + static int adxl313_spi_probe(struct spi_device *spi) 44 + { 45 + const struct spi_device_id *id = spi_get_device_id(spi); 46 + struct regmap *regmap; 47 + int ret; 48 + 49 + spi->mode |= SPI_MODE_3; 50 + ret = spi_setup(spi); 51 + if (ret) 52 + return ret; 53 + 54 + regmap = devm_regmap_init_spi(spi, &adxl313_spi_regmap_config); 55 + if (IS_ERR(regmap)) { 56 + dev_err(&spi->dev, "Error initializing spi regmap: %ld\n", 57 + PTR_ERR(regmap)); 58 + return PTR_ERR(regmap); 59 + } 60 + 61 + return adxl313_core_probe(&spi->dev, regmap, id->name, 62 + &adxl313_spi_setup); 63 + } 64 + 65 + static const struct spi_device_id adxl313_spi_id[] = { 66 + { "adxl313" }, 67 + { } 68 + }; 69 + 70 + MODULE_DEVICE_TABLE(spi, adxl313_spi_id); 71 + 72 + static const struct of_device_id adxl313_of_match[] = { 73 + { .compatible = "adi,adxl313" }, 74 + { } 75 + }; 76 + 77 + MODULE_DEVICE_TABLE(of, adxl313_of_match); 78 + 79 + static struct spi_driver adxl313_spi_driver = { 80 + .driver = { 81 + .name = "adxl313_spi", 82 + .of_match_table = adxl313_of_match, 83 + }, 84 + .probe = adxl313_spi_probe, 85 + .id_table = adxl313_spi_id, 86 + }; 87 + 88 + module_spi_driver(adxl313_spi_driver); 89 + 90 + MODULE_AUTHOR("Lucas Stankus <lucas.p.stankus@gmail.com>"); 91 + MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer SPI driver"); 92 + MODULE_LICENSE("GPL v2");
+21
drivers/iio/accel/adxl355.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * ADXL355 3-Axis Digital Accelerometer 4 + * 5 + * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com> 6 + */ 7 + 8 + #ifndef _ADXL355_H_ 9 + #define _ADXL355_H_ 10 + 11 + #include <linux/regmap.h> 12 + 13 + struct device; 14 + 15 + extern const struct regmap_access_table adxl355_readable_regs_tbl; 16 + extern const struct regmap_access_table adxl355_writeable_regs_tbl; 17 + 18 + int adxl355_core_probe(struct device *dev, struct regmap *regmap, 19 + const char *name); 20 + 21 + #endif /* _ADXL355_H_ */
+765
drivers/iio/accel/adxl355_core.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * ADXL355 3-Axis Digital Accelerometer IIO core driver 4 + * 5 + * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com> 6 + * 7 + * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/adxl354_adxl355.pdf 8 + */ 9 + 10 + #include <linux/bits.h> 11 + #include <linux/bitfield.h> 12 + #include <linux/iio/buffer.h> 13 + #include <linux/iio/iio.h> 14 + #include <linux/iio/trigger.h> 15 + #include <linux/iio/triggered_buffer.h> 16 + #include <linux/iio/trigger_consumer.h> 17 + #include <linux/limits.h> 18 + #include <linux/math64.h> 19 + #include <linux/module.h> 20 + #include <linux/mod_devicetable.h> 21 + #include <linux/of_irq.h> 22 + #include <linux/regmap.h> 23 + #include <asm/unaligned.h> 24 + 25 + #include "adxl355.h" 26 + 27 + /* ADXL355 Register Definitions */ 28 + #define ADXL355_DEVID_AD_REG 0x00 29 + #define ADXL355_DEVID_MST_REG 0x01 30 + #define ADXL355_PARTID_REG 0x02 31 + #define ADXL355_STATUS_REG 0x04 32 + #define ADXL355_FIFO_ENTRIES_REG 0x05 33 + #define ADXL355_TEMP2_REG 0x06 34 + #define ADXL355_XDATA3_REG 0x08 35 + #define ADXL355_YDATA3_REG 0x0B 36 + #define ADXL355_ZDATA3_REG 0x0E 37 + #define ADXL355_FIFO_DATA_REG 0x11 38 + #define ADXL355_OFFSET_X_H_REG 0x1E 39 + #define ADXL355_OFFSET_Y_H_REG 0x20 40 + #define ADXL355_OFFSET_Z_H_REG 0x22 41 + #define ADXL355_ACT_EN_REG 0x24 42 + #define ADXL355_ACT_THRESH_H_REG 0x25 43 + #define ADXL355_ACT_THRESH_L_REG 0x26 44 + #define ADXL355_ACT_COUNT_REG 0x27 45 + #define ADXL355_FILTER_REG 0x28 46 + #define ADXL355_FILTER_ODR_MSK GENMASK(3, 0) 47 + #define ADXL355_FILTER_HPF_MSK GENMASK(6, 4) 48 + #define ADXL355_FIFO_SAMPLES_REG 0x29 49 + #define ADXL355_INT_MAP_REG 0x2A 50 + #define ADXL355_SYNC_REG 0x2B 51 + #define ADXL355_RANGE_REG 0x2C 52 + #define ADXL355_POWER_CTL_REG 0x2D 53 + #define ADXL355_POWER_CTL_MODE_MSK GENMASK(1, 0) 54 + #define ADXL355_POWER_CTL_DRDY_MSK BIT(2) 55 + #define ADXL355_SELF_TEST_REG 0x2E 56 + #define ADXL355_RESET_REG 0x2F 57 + 58 + #define ADXL355_DEVID_AD_VAL 0xAD 59 + #define ADXL355_DEVID_MST_VAL 0x1D 60 + #define ADXL355_PARTID_VAL 0xED 61 + #define ADXL355_RESET_CODE 0x52 62 + 63 + #define MEGA 1000000UL 64 + #define TERA 1000000000000ULL 65 + 66 + static const struct regmap_range adxl355_read_reg_range[] = { 67 + regmap_reg_range(ADXL355_DEVID_AD_REG, ADXL355_FIFO_DATA_REG), 68 + regmap_reg_range(ADXL355_OFFSET_X_H_REG, ADXL355_SELF_TEST_REG), 69 + }; 70 + 71 + const struct regmap_access_table adxl355_readable_regs_tbl = { 72 + .yes_ranges = adxl355_read_reg_range, 73 + .n_yes_ranges = ARRAY_SIZE(adxl355_read_reg_range), 74 + }; 75 + EXPORT_SYMBOL_GPL(adxl355_readable_regs_tbl); 76 + 77 + static const struct regmap_range adxl355_write_reg_range[] = { 78 + regmap_reg_range(ADXL355_OFFSET_X_H_REG, ADXL355_RESET_REG), 79 + }; 80 + 81 + const struct regmap_access_table adxl355_writeable_regs_tbl = { 82 + .yes_ranges = adxl355_write_reg_range, 83 + .n_yes_ranges = ARRAY_SIZE(adxl355_write_reg_range), 84 + }; 85 + EXPORT_SYMBOL_GPL(adxl355_writeable_regs_tbl); 86 + 87 + enum adxl355_op_mode { 88 + ADXL355_MEASUREMENT, 89 + ADXL355_STANDBY, 90 + ADXL355_TEMP_OFF, 91 + }; 92 + 93 + enum adxl355_odr { 94 + ADXL355_ODR_4000HZ, 95 + ADXL355_ODR_2000HZ, 96 + ADXL355_ODR_1000HZ, 97 + ADXL355_ODR_500HZ, 98 + ADXL355_ODR_250HZ, 99 + ADXL355_ODR_125HZ, 100 + ADXL355_ODR_62_5HZ, 101 + ADXL355_ODR_31_25HZ, 102 + ADXL355_ODR_15_625HZ, 103 + ADXL355_ODR_7_813HZ, 104 + ADXL355_ODR_3_906HZ, 105 + }; 106 + 107 + enum adxl355_hpf_3db { 108 + ADXL355_HPF_OFF, 109 + ADXL355_HPF_24_7, 110 + ADXL355_HPF_6_2084, 111 + ADXL355_HPF_1_5545, 112 + ADXL355_HPF_0_3862, 113 + ADXL355_HPF_0_0954, 114 + ADXL355_HPF_0_0238, 115 + }; 116 + 117 + static const int adxl355_odr_table[][2] = { 118 + [0] = {4000, 0}, 119 + [1] = {2000, 0}, 120 + [2] = {1000, 0}, 121 + [3] = {500, 0}, 122 + [4] = {250, 0}, 123 + [5] = {125, 0}, 124 + [6] = {62, 500000}, 125 + [7] = {31, 250000}, 126 + [8] = {15, 625000}, 127 + [9] = {7, 813000}, 128 + [10] = {3, 906000}, 129 + }; 130 + 131 + static const int adxl355_hpf_3db_multipliers[] = { 132 + 0, 133 + 247000, 134 + 62084, 135 + 15545, 136 + 3862, 137 + 954, 138 + 238, 139 + }; 140 + 141 + enum adxl355_chans { 142 + chan_x, chan_y, chan_z, 143 + }; 144 + 145 + struct adxl355_chan_info { 146 + u8 data_reg; 147 + u8 offset_reg; 148 + }; 149 + 150 + static const struct adxl355_chan_info adxl355_chans[] = { 151 + [chan_x] = { 152 + .data_reg = ADXL355_XDATA3_REG, 153 + .offset_reg = ADXL355_OFFSET_X_H_REG 154 + }, 155 + [chan_y] = { 156 + .data_reg = ADXL355_YDATA3_REG, 157 + .offset_reg = ADXL355_OFFSET_Y_H_REG 158 + }, 159 + [chan_z] = { 160 + .data_reg = ADXL355_ZDATA3_REG, 161 + .offset_reg = ADXL355_OFFSET_Z_H_REG 162 + }, 163 + }; 164 + 165 + struct adxl355_data { 166 + struct regmap *regmap; 167 + struct device *dev; 168 + struct mutex lock; /* lock to protect op_mode */ 169 + enum adxl355_op_mode op_mode; 170 + enum adxl355_odr odr; 171 + enum adxl355_hpf_3db hpf_3db; 172 + int calibbias[3]; 173 + int adxl355_hpf_3db_table[7][2]; 174 + struct iio_trigger *dready_trig; 175 + union { 176 + u8 transf_buf[3]; 177 + struct { 178 + u8 buf[14]; 179 + s64 ts; 180 + } buffer; 181 + } ____cacheline_aligned; 182 + }; 183 + 184 + static int adxl355_set_op_mode(struct adxl355_data *data, 185 + enum adxl355_op_mode op_mode) 186 + { 187 + int ret; 188 + 189 + if (data->op_mode == op_mode) 190 + return 0; 191 + 192 + ret = regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG, 193 + ADXL355_POWER_CTL_MODE_MSK, op_mode); 194 + if (ret) 195 + return ret; 196 + 197 + data->op_mode = op_mode; 198 + 199 + return ret; 200 + } 201 + 202 + static int adxl355_data_rdy_trigger_set_state(struct iio_trigger *trig, 203 + bool state) 204 + { 205 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 206 + struct adxl355_data *data = iio_priv(indio_dev); 207 + int ret; 208 + 209 + mutex_lock(&data->lock); 210 + ret = regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG, 211 + ADXL355_POWER_CTL_DRDY_MSK, 212 + FIELD_PREP(ADXL355_POWER_CTL_DRDY_MSK, 213 + state ? 0 : 1)); 214 + mutex_unlock(&data->lock); 215 + 216 + return ret; 217 + } 218 + 219 + static void adxl355_fill_3db_frequency_table(struct adxl355_data *data) 220 + { 221 + u32 multiplier; 222 + u64 div, rem; 223 + u64 odr; 224 + int i; 225 + 226 + odr = mul_u64_u32_shr(adxl355_odr_table[data->odr][0], MEGA, 0) + 227 + adxl355_odr_table[data->odr][1]; 228 + 229 + for (i = 0; i < ARRAY_SIZE(adxl355_hpf_3db_multipliers); i++) { 230 + multiplier = adxl355_hpf_3db_multipliers[i]; 231 + div = div64_u64_rem(mul_u64_u32_shr(odr, multiplier, 0), 232 + TERA * 100, &rem); 233 + 234 + data->adxl355_hpf_3db_table[i][0] = div; 235 + data->adxl355_hpf_3db_table[i][1] = div_u64(rem, MEGA * 100); 236 + } 237 + } 238 + 239 + static int adxl355_setup(struct adxl355_data *data) 240 + { 241 + unsigned int regval; 242 + int ret; 243 + 244 + ret = regmap_read(data->regmap, ADXL355_DEVID_AD_REG, &regval); 245 + if (ret) 246 + return ret; 247 + 248 + if (regval != ADXL355_DEVID_AD_VAL) { 249 + dev_err(data->dev, "Invalid ADI ID 0x%02x\n", regval); 250 + return -ENODEV; 251 + } 252 + 253 + ret = regmap_read(data->regmap, ADXL355_DEVID_MST_REG, &regval); 254 + if (ret) 255 + return ret; 256 + 257 + if (regval != ADXL355_DEVID_MST_VAL) { 258 + dev_err(data->dev, "Invalid MEMS ID 0x%02x\n", regval); 259 + return -ENODEV; 260 + } 261 + 262 + ret = regmap_read(data->regmap, ADXL355_PARTID_REG, &regval); 263 + if (ret) 264 + return ret; 265 + 266 + if (regval != ADXL355_PARTID_VAL) { 267 + dev_err(data->dev, "Invalid DEV ID 0x%02x\n", regval); 268 + return -ENODEV; 269 + } 270 + 271 + /* 272 + * Perform a software reset to make sure the device is in a consistent 273 + * state after start-up. 274 + */ 275 + ret = regmap_write(data->regmap, ADXL355_RESET_REG, ADXL355_RESET_CODE); 276 + if (ret) 277 + return ret; 278 + 279 + ret = regmap_update_bits(data->regmap, ADXL355_POWER_CTL_REG, 280 + ADXL355_POWER_CTL_DRDY_MSK, 281 + FIELD_PREP(ADXL355_POWER_CTL_DRDY_MSK, 1)); 282 + if (ret) 283 + return ret; 284 + 285 + adxl355_fill_3db_frequency_table(data); 286 + 287 + return adxl355_set_op_mode(data, ADXL355_MEASUREMENT); 288 + } 289 + 290 + static int adxl355_get_temp_data(struct adxl355_data *data, u8 addr) 291 + { 292 + return regmap_bulk_read(data->regmap, addr, data->transf_buf, 2); 293 + } 294 + 295 + static int adxl355_read_axis(struct adxl355_data *data, u8 addr) 296 + { 297 + int ret; 298 + 299 + ret = regmap_bulk_read(data->regmap, addr, data->transf_buf, 300 + ARRAY_SIZE(data->transf_buf)); 301 + if (ret) 302 + return ret; 303 + 304 + return get_unaligned_be24(data->transf_buf); 305 + } 306 + 307 + static int adxl355_find_match(const int (*freq_tbl)[2], const int n, 308 + const int val, const int val2) 309 + { 310 + int i; 311 + 312 + for (i = 0; i < n; i++) { 313 + if (freq_tbl[i][0] == val && freq_tbl[i][1] == val2) 314 + return i; 315 + } 316 + 317 + return -EINVAL; 318 + } 319 + 320 + static int adxl355_set_odr(struct adxl355_data *data, 321 + enum adxl355_odr odr) 322 + { 323 + int ret; 324 + 325 + mutex_lock(&data->lock); 326 + 327 + if (data->odr == odr) { 328 + mutex_unlock(&data->lock); 329 + return 0; 330 + } 331 + 332 + ret = adxl355_set_op_mode(data, ADXL355_STANDBY); 333 + if (ret) 334 + goto err_unlock; 335 + 336 + ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG, 337 + ADXL355_FILTER_ODR_MSK, 338 + FIELD_PREP(ADXL355_FILTER_ODR_MSK, odr)); 339 + if (ret) 340 + goto err_set_opmode; 341 + 342 + data->odr = odr; 343 + adxl355_fill_3db_frequency_table(data); 344 + 345 + ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT); 346 + if (ret) 347 + goto err_set_opmode; 348 + 349 + mutex_unlock(&data->lock); 350 + return 0; 351 + 352 + err_set_opmode: 353 + adxl355_set_op_mode(data, ADXL355_MEASUREMENT); 354 + err_unlock: 355 + mutex_unlock(&data->lock); 356 + return ret; 357 + } 358 + 359 + static int adxl355_set_hpf_3db(struct adxl355_data *data, 360 + enum adxl355_hpf_3db hpf) 361 + { 362 + int ret; 363 + 364 + mutex_lock(&data->lock); 365 + 366 + if (data->hpf_3db == hpf) { 367 + mutex_unlock(&data->lock); 368 + return 0; 369 + } 370 + 371 + ret = adxl355_set_op_mode(data, ADXL355_STANDBY); 372 + if (ret) 373 + goto err_unlock; 374 + 375 + ret = regmap_update_bits(data->regmap, ADXL355_FILTER_REG, 376 + ADXL355_FILTER_HPF_MSK, 377 + FIELD_PREP(ADXL355_FILTER_HPF_MSK, hpf)); 378 + if (ret) 379 + goto err_set_opmode; 380 + 381 + data->hpf_3db = hpf; 382 + 383 + ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT); 384 + if (ret) 385 + goto err_set_opmode; 386 + 387 + mutex_unlock(&data->lock); 388 + return 0; 389 + 390 + err_set_opmode: 391 + adxl355_set_op_mode(data, ADXL355_MEASUREMENT); 392 + err_unlock: 393 + mutex_unlock(&data->lock); 394 + return ret; 395 + } 396 + 397 + static int adxl355_set_calibbias(struct adxl355_data *data, 398 + enum adxl355_chans chan, int calibbias) 399 + { 400 + int ret; 401 + 402 + mutex_lock(&data->lock); 403 + 404 + ret = adxl355_set_op_mode(data, ADXL355_STANDBY); 405 + if (ret) 406 + goto err_unlock; 407 + 408 + put_unaligned_be16(calibbias, data->transf_buf); 409 + ret = regmap_bulk_write(data->regmap, 410 + adxl355_chans[chan].offset_reg, 411 + data->transf_buf, 2); 412 + if (ret) 413 + goto err_set_opmode; 414 + 415 + data->calibbias[chan] = calibbias; 416 + 417 + ret = adxl355_set_op_mode(data, ADXL355_MEASUREMENT); 418 + if (ret) 419 + goto err_set_opmode; 420 + 421 + mutex_unlock(&data->lock); 422 + return 0; 423 + 424 + err_set_opmode: 425 + adxl355_set_op_mode(data, ADXL355_MEASUREMENT); 426 + err_unlock: 427 + mutex_unlock(&data->lock); 428 + return ret; 429 + } 430 + 431 + static int adxl355_read_raw(struct iio_dev *indio_dev, 432 + struct iio_chan_spec const *chan, 433 + int *val, int *val2, long mask) 434 + { 435 + struct adxl355_data *data = iio_priv(indio_dev); 436 + int ret; 437 + 438 + switch (mask) { 439 + case IIO_CHAN_INFO_RAW: 440 + switch (chan->type) { 441 + case IIO_TEMP: 442 + ret = adxl355_get_temp_data(data, chan->address); 443 + if (ret < 0) 444 + return ret; 445 + *val = get_unaligned_be16(data->transf_buf); 446 + 447 + return IIO_VAL_INT; 448 + case IIO_ACCEL: 449 + ret = adxl355_read_axis(data, adxl355_chans[ 450 + chan->address].data_reg); 451 + if (ret < 0) 452 + return ret; 453 + *val = sign_extend32(ret >> chan->scan_type.shift, 454 + chan->scan_type.realbits - 1); 455 + return IIO_VAL_INT; 456 + default: 457 + return -EINVAL; 458 + } 459 + 460 + case IIO_CHAN_INFO_SCALE: 461 + switch (chan->type) { 462 + /* 463 + * The datasheet defines an intercept of 1885 LSB at 25 degC 464 + * and a slope of -9.05 LSB/C. The following formula can be used 465 + * to find the temperature: 466 + * Temp = ((RAW - 1885)/(-9.05)) + 25 but this doesn't follow 467 + * the format of the IIO which is Temp = (RAW + OFFSET) * SCALE. 468 + * Hence using some rearranging we get the scale as -110.497238 469 + * and offset as -2111.25. 470 + */ 471 + case IIO_TEMP: 472 + *val = -110; 473 + *val2 = 497238; 474 + return IIO_VAL_INT_PLUS_MICRO; 475 + /* 476 + * At +/- 2g with 20-bit resolution, scale is given in datasheet 477 + * as 3.9ug/LSB = 0.0000039 * 9.80665 = 0.00003824593 m/s^2. 478 + */ 479 + case IIO_ACCEL: 480 + *val = 0; 481 + *val2 = 38245; 482 + return IIO_VAL_INT_PLUS_NANO; 483 + default: 484 + return -EINVAL; 485 + } 486 + case IIO_CHAN_INFO_OFFSET: 487 + *val = -2111; 488 + *val2 = 250000; 489 + return IIO_VAL_INT_PLUS_MICRO; 490 + case IIO_CHAN_INFO_CALIBBIAS: 491 + *val = sign_extend32(data->calibbias[chan->address], 15); 492 + return IIO_VAL_INT; 493 + case IIO_CHAN_INFO_SAMP_FREQ: 494 + *val = adxl355_odr_table[data->odr][0]; 495 + *val2 = adxl355_odr_table[data->odr][1]; 496 + return IIO_VAL_INT_PLUS_MICRO; 497 + case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: 498 + *val = data->adxl355_hpf_3db_table[data->hpf_3db][0]; 499 + *val2 = data->adxl355_hpf_3db_table[data->hpf_3db][1]; 500 + return IIO_VAL_INT_PLUS_MICRO; 501 + default: 502 + return -EINVAL; 503 + } 504 + } 505 + 506 + static int adxl355_write_raw(struct iio_dev *indio_dev, 507 + struct iio_chan_spec const *chan, 508 + int val, int val2, long mask) 509 + { 510 + struct adxl355_data *data = iio_priv(indio_dev); 511 + int odr_idx, hpf_idx, calibbias; 512 + 513 + switch (mask) { 514 + case IIO_CHAN_INFO_SAMP_FREQ: 515 + odr_idx = adxl355_find_match(adxl355_odr_table, 516 + ARRAY_SIZE(adxl355_odr_table), 517 + val, val2); 518 + if (odr_idx < 0) 519 + return odr_idx; 520 + 521 + return adxl355_set_odr(data, odr_idx); 522 + case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: 523 + hpf_idx = adxl355_find_match(data->adxl355_hpf_3db_table, 524 + ARRAY_SIZE(data->adxl355_hpf_3db_table), 525 + val, val2); 526 + if (hpf_idx < 0) 527 + return hpf_idx; 528 + 529 + return adxl355_set_hpf_3db(data, hpf_idx); 530 + case IIO_CHAN_INFO_CALIBBIAS: 531 + calibbias = clamp_t(int, val, S16_MIN, S16_MAX); 532 + 533 + return adxl355_set_calibbias(data, chan->address, calibbias); 534 + default: 535 + return -EINVAL; 536 + } 537 + } 538 + 539 + static int adxl355_read_avail(struct iio_dev *indio_dev, 540 + struct iio_chan_spec const *chan, 541 + const int **vals, int *type, int *length, 542 + long mask) 543 + { 544 + struct adxl355_data *data = iio_priv(indio_dev); 545 + 546 + switch (mask) { 547 + case IIO_CHAN_INFO_SAMP_FREQ: 548 + *vals = (const int *)adxl355_odr_table; 549 + *type = IIO_VAL_INT_PLUS_MICRO; 550 + /* Values are stored in a 2D matrix */ 551 + *length = ARRAY_SIZE(adxl355_odr_table) * 2; 552 + 553 + return IIO_AVAIL_LIST; 554 + case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: 555 + *vals = (const int *)data->adxl355_hpf_3db_table; 556 + *type = IIO_VAL_INT_PLUS_MICRO; 557 + /* Values are stored in a 2D matrix */ 558 + *length = ARRAY_SIZE(data->adxl355_hpf_3db_table) * 2; 559 + 560 + return IIO_AVAIL_LIST; 561 + default: 562 + return -EINVAL; 563 + } 564 + } 565 + 566 + static const unsigned long adxl355_avail_scan_masks[] = { 567 + GENMASK(3, 0), 568 + 0 569 + }; 570 + 571 + static const struct iio_info adxl355_info = { 572 + .read_raw = adxl355_read_raw, 573 + .write_raw = adxl355_write_raw, 574 + .read_avail = &adxl355_read_avail, 575 + }; 576 + 577 + static const struct iio_trigger_ops adxl355_trigger_ops = { 578 + .set_trigger_state = &adxl355_data_rdy_trigger_set_state, 579 + .validate_device = &iio_trigger_validate_own_device, 580 + }; 581 + 582 + static irqreturn_t adxl355_trigger_handler(int irq, void *p) 583 + { 584 + struct iio_poll_func *pf = p; 585 + struct iio_dev *indio_dev = pf->indio_dev; 586 + struct adxl355_data *data = iio_priv(indio_dev); 587 + int ret; 588 + 589 + mutex_lock(&data->lock); 590 + 591 + /* 592 + * data->buffer is used both for triggered buffer support 593 + * and read/write_raw(), hence, it has to be zeroed here before usage. 594 + */ 595 + data->buffer.buf[0] = 0; 596 + 597 + /* 598 + * The acceleration data is 24 bits and big endian. It has to be saved 599 + * in 32 bits, hence, it is saved in the 2nd byte of the 4 byte buffer. 600 + * The buf array is 14 bytes as it includes 3x4=12 bytes for 601 + * accelaration data of x, y, and z axis. It also includes 2 bytes for 602 + * temperature data. 603 + */ 604 + ret = regmap_bulk_read(data->regmap, ADXL355_XDATA3_REG, 605 + &data->buffer.buf[1], 3); 606 + if (ret) 607 + goto out_unlock_notify; 608 + 609 + ret = regmap_bulk_read(data->regmap, ADXL355_YDATA3_REG, 610 + &data->buffer.buf[5], 3); 611 + if (ret) 612 + goto out_unlock_notify; 613 + 614 + ret = regmap_bulk_read(data->regmap, ADXL355_ZDATA3_REG, 615 + &data->buffer.buf[9], 3); 616 + if (ret) 617 + goto out_unlock_notify; 618 + 619 + ret = regmap_bulk_read(data->regmap, ADXL355_TEMP2_REG, 620 + &data->buffer.buf[12], 2); 621 + if (ret) 622 + goto out_unlock_notify; 623 + 624 + iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, 625 + pf->timestamp); 626 + 627 + out_unlock_notify: 628 + mutex_unlock(&data->lock); 629 + iio_trigger_notify_done(indio_dev->trig); 630 + 631 + return IRQ_HANDLED; 632 + } 633 + 634 + #define ADXL355_ACCEL_CHANNEL(index, reg, axis) { \ 635 + .type = IIO_ACCEL, \ 636 + .address = reg, \ 637 + .modified = 1, \ 638 + .channel2 = IIO_MOD_##axis, \ 639 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 640 + BIT(IIO_CHAN_INFO_CALIBBIAS), \ 641 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 642 + BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 643 + BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \ 644 + .info_mask_shared_by_type_available = \ 645 + BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 646 + BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \ 647 + .scan_index = index, \ 648 + .scan_type = { \ 649 + .sign = 's', \ 650 + .realbits = 20, \ 651 + .storagebits = 32, \ 652 + .shift = 4, \ 653 + .endianness = IIO_BE, \ 654 + } \ 655 + } 656 + 657 + static const struct iio_chan_spec adxl355_channels[] = { 658 + ADXL355_ACCEL_CHANNEL(0, chan_x, X), 659 + ADXL355_ACCEL_CHANNEL(1, chan_y, Y), 660 + ADXL355_ACCEL_CHANNEL(2, chan_z, Z), 661 + { 662 + .type = IIO_TEMP, 663 + .address = ADXL355_TEMP2_REG, 664 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 665 + BIT(IIO_CHAN_INFO_SCALE) | 666 + BIT(IIO_CHAN_INFO_OFFSET), 667 + .scan_index = 3, 668 + .scan_type = { 669 + .sign = 's', 670 + .realbits = 12, 671 + .storagebits = 16, 672 + .endianness = IIO_BE, 673 + }, 674 + }, 675 + IIO_CHAN_SOFT_TIMESTAMP(4), 676 + }; 677 + 678 + static int adxl355_probe_trigger(struct iio_dev *indio_dev, int irq) 679 + { 680 + struct adxl355_data *data = iio_priv(indio_dev); 681 + int ret; 682 + 683 + data->dready_trig = devm_iio_trigger_alloc(data->dev, "%s-dev%d", 684 + indio_dev->name, 685 + iio_device_id(indio_dev)); 686 + if (!data->dready_trig) 687 + return -ENOMEM; 688 + 689 + data->dready_trig->ops = &adxl355_trigger_ops; 690 + iio_trigger_set_drvdata(data->dready_trig, indio_dev); 691 + 692 + ret = devm_request_irq(data->dev, irq, 693 + &iio_trigger_generic_data_rdy_poll, 694 + IRQF_ONESHOT, "adxl355_irq", data->dready_trig); 695 + if (ret) 696 + return dev_err_probe(data->dev, ret, "request irq %d failed\n", 697 + irq); 698 + 699 + ret = devm_iio_trigger_register(data->dev, data->dready_trig); 700 + if (ret) { 701 + dev_err(data->dev, "iio trigger register failed\n"); 702 + return ret; 703 + } 704 + 705 + indio_dev->trig = iio_trigger_get(data->dready_trig); 706 + 707 + return 0; 708 + } 709 + 710 + int adxl355_core_probe(struct device *dev, struct regmap *regmap, 711 + const char *name) 712 + { 713 + struct adxl355_data *data; 714 + struct iio_dev *indio_dev; 715 + int ret; 716 + int irq; 717 + 718 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 719 + if (!indio_dev) 720 + return -ENOMEM; 721 + 722 + data = iio_priv(indio_dev); 723 + data->regmap = regmap; 724 + data->dev = dev; 725 + data->op_mode = ADXL355_STANDBY; 726 + mutex_init(&data->lock); 727 + 728 + indio_dev->name = name; 729 + indio_dev->info = &adxl355_info; 730 + indio_dev->modes = INDIO_DIRECT_MODE; 731 + indio_dev->channels = adxl355_channels; 732 + indio_dev->num_channels = ARRAY_SIZE(adxl355_channels); 733 + indio_dev->available_scan_masks = adxl355_avail_scan_masks; 734 + 735 + ret = adxl355_setup(data); 736 + if (ret) { 737 + dev_err(dev, "ADXL355 setup failed\n"); 738 + return ret; 739 + } 740 + 741 + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 742 + &iio_pollfunc_store_time, 743 + &adxl355_trigger_handler, NULL); 744 + if (ret) { 745 + dev_err(dev, "iio triggered buffer setup failed\n"); 746 + return ret; 747 + } 748 + 749 + /* 750 + * TODO: Would be good to move it to the generic version. 751 + */ 752 + irq = of_irq_get_byname(dev->of_node, "DRDY"); 753 + if (irq > 0) { 754 + ret = adxl355_probe_trigger(indio_dev, irq); 755 + if (ret) 756 + return ret; 757 + } 758 + 759 + return devm_iio_device_register(dev, indio_dev); 760 + } 761 + EXPORT_SYMBOL_GPL(adxl355_core_probe); 762 + 763 + MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>"); 764 + MODULE_DESCRIPTION("ADXL355 3-Axis Digital Accelerometer core driver"); 765 + MODULE_LICENSE("GPL v2");
+62
drivers/iio/accel/adxl355_i2c.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * ADXL355 3-Axis Digital Accelerometer I2C driver 4 + * 5 + * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com> 6 + */ 7 + 8 + #include <linux/i2c.h> 9 + #include <linux/module.h> 10 + #include <linux/mod_devicetable.h> 11 + #include <linux/regmap.h> 12 + 13 + #include "adxl355.h" 14 + 15 + static const struct regmap_config adxl355_i2c_regmap_config = { 16 + .reg_bits = 8, 17 + .val_bits = 8, 18 + .max_register = 0x2F, 19 + .rd_table = &adxl355_readable_regs_tbl, 20 + .wr_table = &adxl355_writeable_regs_tbl, 21 + }; 22 + 23 + static int adxl355_i2c_probe(struct i2c_client *client) 24 + { 25 + struct regmap *regmap; 26 + 27 + regmap = devm_regmap_init_i2c(client, &adxl355_i2c_regmap_config); 28 + if (IS_ERR(regmap)) { 29 + dev_err(&client->dev, "Error initializing i2c regmap: %ld\n", 30 + PTR_ERR(regmap)); 31 + 32 + return PTR_ERR(regmap); 33 + } 34 + 35 + return adxl355_core_probe(&client->dev, regmap, client->name); 36 + } 37 + 38 + static const struct i2c_device_id adxl355_i2c_id[] = { 39 + { "adxl355", 0 }, 40 + { } 41 + }; 42 + MODULE_DEVICE_TABLE(i2c, adxl355_i2c_id); 43 + 44 + static const struct of_device_id adxl355_of_match[] = { 45 + { .compatible = "adi,adxl355" }, 46 + { } 47 + }; 48 + MODULE_DEVICE_TABLE(of, adxl355_of_match); 49 + 50 + static struct i2c_driver adxl355_i2c_driver = { 51 + .driver = { 52 + .name = "adxl355_i2c", 53 + .of_match_table = adxl355_of_match, 54 + }, 55 + .probe_new = adxl355_i2c_probe, 56 + .id_table = adxl355_i2c_id, 57 + }; 58 + module_i2c_driver(adxl355_i2c_driver); 59 + 60 + MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>"); 61 + MODULE_DESCRIPTION("ADXL355 3-Axis Digital Accelerometer I2C driver"); 62 + MODULE_LICENSE("GPL v2");
+65
drivers/iio/accel/adxl355_spi.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * ADXL355 3-Axis Digital Accelerometer SPI driver 4 + * 5 + * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com> 6 + */ 7 + 8 + #include <linux/module.h> 9 + #include <linux/mod_devicetable.h> 10 + #include <linux/regmap.h> 11 + #include <linux/spi/spi.h> 12 + 13 + #include "adxl355.h" 14 + 15 + static const struct regmap_config adxl355_spi_regmap_config = { 16 + .reg_bits = 7, 17 + .pad_bits = 1, 18 + .val_bits = 8, 19 + .read_flag_mask = BIT(0), 20 + .max_register = 0x2F, 21 + .rd_table = &adxl355_readable_regs_tbl, 22 + .wr_table = &adxl355_writeable_regs_tbl, 23 + }; 24 + 25 + static int adxl355_spi_probe(struct spi_device *spi) 26 + { 27 + const struct spi_device_id *id = spi_get_device_id(spi); 28 + struct regmap *regmap; 29 + 30 + regmap = devm_regmap_init_spi(spi, &adxl355_spi_regmap_config); 31 + if (IS_ERR(regmap)) { 32 + dev_err(&spi->dev, "Error initializing spi regmap: %ld\n", 33 + PTR_ERR(regmap)); 34 + 35 + return PTR_ERR(regmap); 36 + } 37 + 38 + return adxl355_core_probe(&spi->dev, regmap, id->name); 39 + } 40 + 41 + static const struct spi_device_id adxl355_spi_id[] = { 42 + { "adxl355", 0 }, 43 + { } 44 + }; 45 + MODULE_DEVICE_TABLE(spi, adxl355_spi_id); 46 + 47 + static const struct of_device_id adxl355_of_match[] = { 48 + { .compatible = "adi,adxl355" }, 49 + { } 50 + }; 51 + MODULE_DEVICE_TABLE(of, adxl355_of_match); 52 + 53 + static struct spi_driver adxl355_spi_driver = { 54 + .driver = { 55 + .name = "adxl355_spi", 56 + .of_match_table = adxl355_of_match, 57 + }, 58 + .probe = adxl355_spi_probe, 59 + .id_table = adxl355_spi_id, 60 + }; 61 + module_spi_driver(adxl355_spi_driver); 62 + 63 + MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>"); 64 + MODULE_DESCRIPTION("ADXL355 3-Axis Digital Accelerometer SPI driver"); 65 + MODULE_LICENSE("GPL v2");
+1
drivers/iio/accel/adxl372.c
··· 1214 1214 ret = devm_iio_triggered_buffer_setup_ext(dev, 1215 1215 indio_dev, NULL, 1216 1216 adxl372_trigger_handler, 1217 + IIO_BUFFER_DIRECTION_IN, 1217 1218 &adxl372_buffer_ops, 1218 1219 adxl372_fifo_attributes); 1219 1220 if (ret < 0)
+1 -1
drivers/iio/accel/bma400.h
··· 94 94 95 95 int bma400_probe(struct device *dev, struct regmap *regmap, const char *name); 96 96 97 - int bma400_remove(struct device *dev); 97 + void bma400_remove(struct device *dev); 98 98 99 99 #endif
+4 -3
drivers/iio/accel/bma400_core.c
··· 828 828 } 829 829 EXPORT_SYMBOL(bma400_probe); 830 830 831 - int bma400_remove(struct device *dev) 831 + void bma400_remove(struct device *dev) 832 832 { 833 833 struct iio_dev *indio_dev = dev_get_drvdata(dev); 834 834 struct bma400_data *data = iio_priv(indio_dev); ··· 838 838 ret = bma400_set_power_mode(data, POWER_MODE_SLEEP); 839 839 mutex_unlock(&data->mutex); 840 840 841 + if (ret) 842 + dev_warn(dev, "Failed to put device into sleep mode (%pe)\n", ERR_PTR(ret)); 843 + 841 844 regulator_bulk_disable(ARRAY_SIZE(data->regulators), 842 845 data->regulators); 843 846 844 847 iio_device_unregister(indio_dev); 845 - 846 - return ret; 847 848 } 848 849 EXPORT_SYMBOL(bma400_remove); 849 850
+3 -1
drivers/iio/accel/bma400_i2c.c
··· 29 29 30 30 static int bma400_i2c_remove(struct i2c_client *client) 31 31 { 32 - return bma400_remove(&client->dev); 32 + bma400_remove(&client->dev); 33 + 34 + return 0; 33 35 } 34 36 35 37 static const struct i2c_device_id bma400_i2c_ids[] = {
+3 -1
drivers/iio/accel/bma400_spi.c
··· 89 89 90 90 static int bma400_spi_remove(struct spi_device *spi) 91 91 { 92 - return bma400_remove(&spi->dev); 92 + bma400_remove(&spi->dev); 93 + 94 + return 0; 93 95 } 94 96 95 97 static const struct spi_device_id bma400_spi_ids[] = {
+2 -3
drivers/iio/accel/bmc150-accel-core.c
··· 1734 1734 ret = iio_triggered_buffer_setup_ext(indio_dev, 1735 1735 &iio_pollfunc_store_time, 1736 1736 bmc150_accel_trigger_handler, 1737 + IIO_BUFFER_DIRECTION_IN, 1737 1738 &bmc150_accel_buffer_ops, 1738 1739 fifo_attrs); 1739 1740 if (ret < 0) { ··· 1800 1799 } 1801 1800 EXPORT_SYMBOL_GPL(bmc150_accel_core_probe); 1802 1801 1803 - int bmc150_accel_core_remove(struct device *dev) 1802 + void bmc150_accel_core_remove(struct device *dev) 1804 1803 { 1805 1804 struct iio_dev *indio_dev = dev_get_drvdata(dev); 1806 1805 struct bmc150_accel_data *data = iio_priv(indio_dev); ··· 1820 1819 1821 1820 regulator_bulk_disable(ARRAY_SIZE(data->regulators), 1822 1821 data->regulators); 1823 - 1824 - return 0; 1825 1822 } 1826 1823 EXPORT_SYMBOL_GPL(bmc150_accel_core_remove); 1827 1824
+3 -1
drivers/iio/accel/bmc150-accel-i2c.c
··· 213 213 { 214 214 bmc150_acpi_dual_accel_remove(client); 215 215 216 - return bmc150_accel_core_remove(&client->dev); 216 + bmc150_accel_core_remove(&client->dev); 217 + 218 + return 0; 217 219 } 218 220 219 221 static const struct acpi_device_id bmc150_accel_acpi_match[] = {
+3 -1
drivers/iio/accel/bmc150-accel-spi.c
··· 37 37 38 38 static int bmc150_accel_remove(struct spi_device *spi) 39 39 { 40 - return bmc150_accel_core_remove(&spi->dev); 40 + bmc150_accel_core_remove(&spi->dev); 41 + 42 + return 0; 41 43 } 42 44 43 45 static const struct acpi_device_id bmc150_accel_acpi_match[] = {
+1 -1
drivers/iio/accel/bmc150-accel.h
··· 88 88 int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq, 89 89 enum bmc150_type type, const char *name, 90 90 bool block_supported); 91 - int bmc150_accel_core_remove(struct device *dev); 91 + void bmc150_accel_core_remove(struct device *dev); 92 92 extern const struct dev_pm_ops bmc150_accel_pm_ops; 93 93 extern const struct regmap_config bmc150_regmap_conf; 94 94
+1 -3
drivers/iio/accel/bmi088-accel-core.c
··· 536 536 EXPORT_SYMBOL_GPL(bmi088_accel_core_probe); 537 537 538 538 539 - int bmi088_accel_core_remove(struct device *dev) 539 + void bmi088_accel_core_remove(struct device *dev) 540 540 { 541 541 struct iio_dev *indio_dev = dev_get_drvdata(dev); 542 542 struct bmi088_accel_data *data = iio_priv(indio_dev); ··· 546 546 pm_runtime_disable(dev); 547 547 pm_runtime_set_suspended(dev); 548 548 bmi088_accel_power_down(data); 549 - 550 - return 0; 551 549 } 552 550 EXPORT_SYMBOL_GPL(bmi088_accel_core_remove); 553 551
+3 -1
drivers/iio/accel/bmi088-accel-spi.c
··· 58 58 59 59 static int bmi088_accel_remove(struct spi_device *spi) 60 60 { 61 - return bmi088_accel_core_remove(&spi->dev); 61 + bmi088_accel_core_remove(&spi->dev); 62 + 63 + return 0; 62 64 } 63 65 64 66 static const struct spi_device_id bmi088_accel_id[] = {
+1 -1
drivers/iio/accel/bmi088-accel.h
··· 13 13 14 14 int bmi088_accel_core_probe(struct device *dev, struct regmap *regmap, int irq, 15 15 const char *name, bool block_supported); 16 - int bmi088_accel_core_remove(struct device *dev); 16 + void bmi088_accel_core_remove(struct device *dev); 17 17 18 18 #endif /* BMI088_ACCEL_H */
+342 -5
drivers/iio/accel/fxls8962af-core.c
··· 22 22 #include <linux/regmap.h> 23 23 24 24 #include <linux/iio/buffer.h> 25 + #include <linux/iio/events.h> 25 26 #include <linux/iio/iio.h> 26 27 #include <linux/iio/kfifo_buf.h> 27 28 #include <linux/iio/sysfs.h> ··· 31 30 32 31 #define FXLS8962AF_INT_STATUS 0x00 33 32 #define FXLS8962AF_INT_STATUS_SRC_BOOT BIT(0) 33 + #define FXLS8962AF_INT_STATUS_SRC_SDCD_OT BIT(4) 34 34 #define FXLS8962AF_INT_STATUS_SRC_BUF BIT(5) 35 35 #define FXLS8962AF_INT_STATUS_SRC_DRDY BIT(7) 36 36 #define FXLS8962AF_TEMP_OUT 0x01 ··· 75 73 #define FXLS8962AF_ASLP_COUNT_LSB 0x1e 76 74 77 75 #define FXLS8962AF_INT_EN 0x20 76 + #define FXLS8962AF_INT_EN_SDCD_OT_EN BIT(5) 78 77 #define FXLS8962AF_INT_EN_BUF_EN BIT(6) 79 78 #define FXLS8962AF_INT_PIN_SEL 0x21 80 79 #define FXLS8962AF_INT_PIN_SEL_MASK GENMASK(7, 0) ··· 99 96 #define FXLS8962AF_ORIENT_THS_REG 0x2c 100 97 101 98 #define FXLS8962AF_SDCD_INT_SRC1 0x2d 99 + #define FXLS8962AF_SDCD_INT_SRC1_X_OT BIT(5) 100 + #define FXLS8962AF_SDCD_INT_SRC1_X_POL BIT(4) 101 + #define FXLS8962AF_SDCD_INT_SRC1_Y_OT BIT(3) 102 + #define FXLS8962AF_SDCD_INT_SRC1_Y_POL BIT(2) 103 + #define FXLS8962AF_SDCD_INT_SRC1_Z_OT BIT(1) 104 + #define FXLS8962AF_SDCD_INT_SRC1_Z_POL BIT(0) 102 105 #define FXLS8962AF_SDCD_INT_SRC2 0x2e 103 106 #define FXLS8962AF_SDCD_CONFIG1 0x2f 107 + #define FXLS8962AF_SDCD_CONFIG1_Z_OT_EN BIT(3) 108 + #define FXLS8962AF_SDCD_CONFIG1_Y_OT_EN BIT(4) 109 + #define FXLS8962AF_SDCD_CONFIG1_X_OT_EN BIT(5) 110 + #define FXLS8962AF_SDCD_CONFIG1_OT_ELE BIT(7) 104 111 #define FXLS8962AF_SDCD_CONFIG2 0x30 112 + #define FXLS8962AF_SDCD_CONFIG2_SDCD_EN BIT(7) 113 + #define FXLS8962AF_SC2_REF_UPDM_AC GENMASK(6, 5) 105 114 #define FXLS8962AF_SDCD_OT_DBCNT 0x31 106 115 #define FXLS8962AF_SDCD_WT_DBCNT 0x32 107 116 #define FXLS8962AF_SDCD_LTHS_LSB 0x33 ··· 166 151 } scan; 167 152 int64_t timestamp, old_timestamp; /* Only used in hw fifo mode. */ 168 153 struct iio_mount_matrix orientation; 154 + int irq; 169 155 u8 watermark; 156 + u8 enable_event; 157 + u16 lower_thres; 158 + u16 upper_thres; 170 159 }; 171 160 172 161 const struct regmap_config fxls8962af_regmap_conf = { ··· 257 238 } 258 239 259 240 ret = regmap_bulk_read(data->regmap, chan->address, 260 - &raw_val, (chan->scan_type.storagebits / 8)); 241 + &raw_val, sizeof(data->lower_thres)); 261 242 262 243 if (!is_active) 263 244 fxls8962af_power_off(data); ··· 470 451 } 471 452 } 472 453 454 + static int fxls8962af_event_setup(struct fxls8962af_data *data, int state) 455 + { 456 + /* Enable wakeup interrupt */ 457 + int mask = FXLS8962AF_INT_EN_SDCD_OT_EN; 458 + int value = state ? mask : 0; 459 + 460 + return regmap_update_bits(data->regmap, FXLS8962AF_INT_EN, mask, value); 461 + } 462 + 473 463 static int fxls8962af_set_watermark(struct iio_dev *indio_dev, unsigned val) 474 464 { 475 465 struct fxls8962af_data *data = iio_priv(indio_dev); ··· 490 462 491 463 return 0; 492 464 } 465 + 466 + static int __fxls8962af_set_thresholds(struct fxls8962af_data *data, 467 + const struct iio_chan_spec *chan, 468 + enum iio_event_direction dir, 469 + int val) 470 + { 471 + switch (dir) { 472 + case IIO_EV_DIR_FALLING: 473 + data->lower_thres = val; 474 + return regmap_bulk_write(data->regmap, FXLS8962AF_SDCD_LTHS_LSB, 475 + &data->lower_thres, sizeof(data->lower_thres)); 476 + case IIO_EV_DIR_RISING: 477 + data->upper_thres = val; 478 + return regmap_bulk_write(data->regmap, FXLS8962AF_SDCD_UTHS_LSB, 479 + &data->upper_thres, sizeof(data->upper_thres)); 480 + default: 481 + return -EINVAL; 482 + } 483 + } 484 + 485 + static int fxls8962af_read_event(struct iio_dev *indio_dev, 486 + const struct iio_chan_spec *chan, 487 + enum iio_event_type type, 488 + enum iio_event_direction dir, 489 + enum iio_event_info info, 490 + int *val, int *val2) 491 + { 492 + struct fxls8962af_data *data = iio_priv(indio_dev); 493 + int ret; 494 + 495 + if (type != IIO_EV_TYPE_THRESH) 496 + return -EINVAL; 497 + 498 + switch (dir) { 499 + case IIO_EV_DIR_FALLING: 500 + ret = regmap_bulk_read(data->regmap, FXLS8962AF_SDCD_LTHS_LSB, 501 + &data->lower_thres, sizeof(data->lower_thres)); 502 + if (ret) 503 + return ret; 504 + 505 + *val = sign_extend32(data->lower_thres, chan->scan_type.realbits - 1); 506 + return IIO_VAL_INT; 507 + case IIO_EV_DIR_RISING: 508 + ret = regmap_bulk_read(data->regmap, FXLS8962AF_SDCD_UTHS_LSB, 509 + &data->upper_thres, sizeof(data->upper_thres)); 510 + if (ret) 511 + return ret; 512 + 513 + *val = sign_extend32(data->upper_thres, chan->scan_type.realbits - 1); 514 + return IIO_VAL_INT; 515 + default: 516 + return -EINVAL; 517 + } 518 + } 519 + 520 + static int fxls8962af_write_event(struct iio_dev *indio_dev, 521 + const struct iio_chan_spec *chan, 522 + enum iio_event_type type, 523 + enum iio_event_direction dir, 524 + enum iio_event_info info, 525 + int val, int val2) 526 + { 527 + struct fxls8962af_data *data = iio_priv(indio_dev); 528 + int ret, val_masked; 529 + 530 + if (type != IIO_EV_TYPE_THRESH) 531 + return -EINVAL; 532 + 533 + if (val < -2048 || val > 2047) 534 + return -EINVAL; 535 + 536 + if (data->enable_event) 537 + return -EBUSY; 538 + 539 + val_masked = val & GENMASK(11, 0); 540 + if (fxls8962af_is_active(data)) { 541 + ret = fxls8962af_standby(data); 542 + if (ret) 543 + return ret; 544 + 545 + ret = __fxls8962af_set_thresholds(data, chan, dir, val_masked); 546 + if (ret) 547 + return ret; 548 + 549 + return fxls8962af_active(data); 550 + } else { 551 + return __fxls8962af_set_thresholds(data, chan, dir, val_masked); 552 + } 553 + } 554 + 555 + static int 556 + fxls8962af_read_event_config(struct iio_dev *indio_dev, 557 + const struct iio_chan_spec *chan, 558 + enum iio_event_type type, 559 + enum iio_event_direction dir) 560 + { 561 + struct fxls8962af_data *data = iio_priv(indio_dev); 562 + 563 + if (type != IIO_EV_TYPE_THRESH) 564 + return -EINVAL; 565 + 566 + switch (chan->channel2) { 567 + case IIO_MOD_X: 568 + return !!(FXLS8962AF_SDCD_CONFIG1_X_OT_EN & data->enable_event); 569 + case IIO_MOD_Y: 570 + return !!(FXLS8962AF_SDCD_CONFIG1_Y_OT_EN & data->enable_event); 571 + case IIO_MOD_Z: 572 + return !!(FXLS8962AF_SDCD_CONFIG1_Z_OT_EN & data->enable_event); 573 + default: 574 + return -EINVAL; 575 + } 576 + } 577 + 578 + static int 579 + fxls8962af_write_event_config(struct iio_dev *indio_dev, 580 + const struct iio_chan_spec *chan, 581 + enum iio_event_type type, 582 + enum iio_event_direction dir, int state) 583 + { 584 + struct fxls8962af_data *data = iio_priv(indio_dev); 585 + u8 enable_event, enable_bits; 586 + int ret, value; 587 + 588 + if (type != IIO_EV_TYPE_THRESH) 589 + return -EINVAL; 590 + 591 + switch (chan->channel2) { 592 + case IIO_MOD_X: 593 + enable_bits = FXLS8962AF_SDCD_CONFIG1_X_OT_EN; 594 + break; 595 + case IIO_MOD_Y: 596 + enable_bits = FXLS8962AF_SDCD_CONFIG1_Y_OT_EN; 597 + break; 598 + case IIO_MOD_Z: 599 + enable_bits = FXLS8962AF_SDCD_CONFIG1_Z_OT_EN; 600 + break; 601 + default: 602 + return -EINVAL; 603 + } 604 + 605 + if (state) 606 + enable_event = data->enable_event | enable_bits; 607 + else 608 + enable_event = data->enable_event & ~enable_bits; 609 + 610 + if (data->enable_event == enable_event) 611 + return 0; 612 + 613 + ret = fxls8962af_standby(data); 614 + if (ret) 615 + return ret; 616 + 617 + /* Enable events */ 618 + value = enable_event | FXLS8962AF_SDCD_CONFIG1_OT_ELE; 619 + ret = regmap_write(data->regmap, FXLS8962AF_SDCD_CONFIG1, value); 620 + if (ret) 621 + return ret; 622 + 623 + /* 624 + * Enable update of SDCD_REF_X/Y/Z values with the current decimated and 625 + * trimmed X/Y/Z acceleration input data. This allows for acceleration 626 + * slope detection with Data(n) to Data(n–1) always used as the input 627 + * to the window comparator. 628 + */ 629 + value = enable_event ? 630 + FXLS8962AF_SDCD_CONFIG2_SDCD_EN | FXLS8962AF_SC2_REF_UPDM_AC : 631 + 0x00; 632 + ret = regmap_write(data->regmap, FXLS8962AF_SDCD_CONFIG2, value); 633 + if (ret) 634 + return ret; 635 + 636 + ret = fxls8962af_event_setup(data, state); 637 + if (ret) 638 + return ret; 639 + 640 + data->enable_event = enable_event; 641 + 642 + if (data->enable_event) { 643 + fxls8962af_active(data); 644 + ret = fxls8962af_power_on(data); 645 + } else { 646 + ret = iio_device_claim_direct_mode(indio_dev); 647 + if (ret) 648 + return ret; 649 + 650 + /* Not in buffered mode so disable power */ 651 + ret = fxls8962af_power_off(data); 652 + 653 + iio_device_release_direct_mode(indio_dev); 654 + } 655 + 656 + return ret; 657 + } 658 + 659 + static const struct iio_event_spec fxls8962af_event[] = { 660 + { 661 + .type = IIO_EV_TYPE_THRESH, 662 + .dir = IIO_EV_DIR_EITHER, 663 + .mask_separate = BIT(IIO_EV_INFO_ENABLE), 664 + }, 665 + { 666 + .type = IIO_EV_TYPE_THRESH, 667 + .dir = IIO_EV_DIR_FALLING, 668 + .mask_separate = BIT(IIO_EV_INFO_VALUE), 669 + }, 670 + { 671 + .type = IIO_EV_TYPE_THRESH, 672 + .dir = IIO_EV_DIR_RISING, 673 + .mask_separate = BIT(IIO_EV_INFO_VALUE), 674 + }, 675 + }; 493 676 494 677 #define FXLS8962AF_CHANNEL(axis, reg, idx) { \ 495 678 .type = IIO_ACCEL, \ ··· 720 481 .shift = 4, \ 721 482 .endianness = IIO_BE, \ 722 483 }, \ 484 + .event_spec = fxls8962af_event, \ 485 + .num_event_specs = ARRAY_SIZE(fxls8962af_event), \ 723 486 } 724 487 725 488 #define FXLS8962AF_TEMP_CHANNEL { \ ··· 763 522 .read_raw = &fxls8962af_read_raw, 764 523 .write_raw = &fxls8962af_write_raw, 765 524 .write_raw_get_fmt = fxls8962af_write_raw_get_fmt, 525 + .read_event_value = fxls8962af_read_event, 526 + .write_event_value = fxls8962af_write_event, 527 + .read_event_config = fxls8962af_read_event_config, 528 + .write_event_config = fxls8962af_write_event_config, 766 529 .read_avail = fxls8962af_read_avail, 767 530 .hwfifo_set_watermark = fxls8962af_set_watermark, 768 531 }; ··· 850 605 851 606 ret = __fxls8962af_fifo_set_mode(data, false); 852 607 853 - fxls8962af_active(data); 608 + if (data->enable_event) 609 + fxls8962af_active(data); 854 610 855 611 return ret; 856 612 } ··· 860 614 { 861 615 struct fxls8962af_data *data = iio_priv(indio_dev); 862 616 863 - return fxls8962af_power_off(data); 617 + if (!data->enable_event) 618 + fxls8962af_power_off(data); 619 + 620 + return 0; 864 621 } 865 622 866 623 static const struct iio_buffer_setup_ops fxls8962af_buffer_ops = { ··· 974 725 return count; 975 726 } 976 727 728 + static int fxls8962af_event_interrupt(struct iio_dev *indio_dev) 729 + { 730 + struct fxls8962af_data *data = iio_priv(indio_dev); 731 + s64 ts = iio_get_time_ns(indio_dev); 732 + unsigned int reg; 733 + u64 ev_code; 734 + int ret; 735 + 736 + ret = regmap_read(data->regmap, FXLS8962AF_SDCD_INT_SRC1, &reg); 737 + if (ret) 738 + return ret; 739 + 740 + if (reg & FXLS8962AF_SDCD_INT_SRC1_X_OT) { 741 + ev_code = reg & FXLS8962AF_SDCD_INT_SRC1_X_POL ? 742 + IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING; 743 + iio_push_event(indio_dev, 744 + IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X, 745 + IIO_EV_TYPE_THRESH, ev_code), ts); 746 + } 747 + 748 + if (reg & FXLS8962AF_SDCD_INT_SRC1_Y_OT) { 749 + ev_code = reg & FXLS8962AF_SDCD_INT_SRC1_Y_POL ? 750 + IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING; 751 + iio_push_event(indio_dev, 752 + IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X, 753 + IIO_EV_TYPE_THRESH, ev_code), ts); 754 + } 755 + 756 + if (reg & FXLS8962AF_SDCD_INT_SRC1_Z_OT) { 757 + ev_code = reg & FXLS8962AF_SDCD_INT_SRC1_Z_POL ? 758 + IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING; 759 + iio_push_event(indio_dev, 760 + IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X, 761 + IIO_EV_TYPE_THRESH, ev_code), ts); 762 + } 763 + 764 + return 0; 765 + } 766 + 977 767 static irqreturn_t fxls8962af_interrupt(int irq, void *p) 978 768 { 979 769 struct iio_dev *indio_dev = p; ··· 1026 738 1027 739 if (reg & FXLS8962AF_INT_STATUS_SRC_BUF) { 1028 740 ret = fxls8962af_fifo_flush(indio_dev); 741 + if (ret < 0) 742 + return IRQ_NONE; 743 + 744 + return IRQ_HANDLED; 745 + } 746 + 747 + if (reg & FXLS8962AF_INT_STATUS_SRC_SDCD_OT) { 748 + ret = fxls8962af_event_interrupt(indio_dev); 1029 749 if (ret < 0) 1030 750 return IRQ_NONE; 1031 751 ··· 1157 861 data = iio_priv(indio_dev); 1158 862 dev_set_drvdata(dev, indio_dev); 1159 863 data->regmap = regmap; 864 + data->irq = irq; 1160 865 1161 866 ret = iio_read_mount_matrix(dev, &data->orientation); 1162 867 if (ret) ··· 1227 930 if (ret) 1228 931 return ret; 1229 932 933 + if (device_property_read_bool(dev, "wakeup-source")) 934 + device_init_wakeup(dev, true); 935 + 1230 936 return devm_iio_device_register(dev, indio_dev); 1231 937 } 1232 938 EXPORT_SYMBOL_GPL(fxls8962af_core_probe); ··· 1255 955 return fxls8962af_active(data); 1256 956 } 1257 957 958 + static int __maybe_unused fxls8962af_suspend(struct device *dev) 959 + { 960 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 961 + struct fxls8962af_data *data = iio_priv(indio_dev); 962 + 963 + if (device_may_wakeup(dev) && data->enable_event) { 964 + enable_irq_wake(data->irq); 965 + 966 + /* 967 + * Disable buffer, as the buffer is so small the device will wake 968 + * almost immediately. 969 + */ 970 + if (iio_buffer_enabled(indio_dev)) 971 + fxls8962af_buffer_predisable(indio_dev); 972 + } else { 973 + fxls8962af_runtime_suspend(dev); 974 + } 975 + 976 + return 0; 977 + } 978 + 979 + static int __maybe_unused fxls8962af_resume(struct device *dev) 980 + { 981 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 982 + struct fxls8962af_data *data = iio_priv(indio_dev); 983 + 984 + if (device_may_wakeup(dev) && data->enable_event) { 985 + disable_irq_wake(data->irq); 986 + 987 + if (iio_buffer_enabled(indio_dev)) 988 + fxls8962af_buffer_postenable(indio_dev); 989 + } else { 990 + fxls8962af_runtime_resume(dev); 991 + } 992 + 993 + return 0; 994 + } 995 + 1258 996 const struct dev_pm_ops fxls8962af_pm_ops = { 1259 - SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1260 - pm_runtime_force_resume) 997 + SET_SYSTEM_SLEEP_PM_OPS(fxls8962af_suspend, fxls8962af_resume) 1261 998 SET_RUNTIME_PM_OPS(fxls8962af_runtime_suspend, 1262 999 fxls8962af_runtime_resume, NULL) 1263 1000 };
+3 -1
drivers/iio/accel/kxsd9-i2c.c
··· 34 34 35 35 static int kxsd9_i2c_remove(struct i2c_client *client) 36 36 { 37 - return kxsd9_common_remove(&client->dev); 37 + kxsd9_common_remove(&client->dev); 38 + 39 + return 0; 38 40 } 39 41 40 42 static const struct of_device_id kxsd9_of_match[] = {
+3 -1
drivers/iio/accel/kxsd9-spi.c
··· 34 34 35 35 static int kxsd9_spi_remove(struct spi_device *spi) 36 36 { 37 - return kxsd9_common_remove(&spi->dev); 37 + kxsd9_common_remove(&spi->dev); 38 + 39 + return 0; 38 40 } 39 41 40 42 static const struct spi_device_id kxsd9_spi_id[] = {
+1 -3
drivers/iio/accel/kxsd9.c
··· 478 478 } 479 479 EXPORT_SYMBOL(kxsd9_common_probe); 480 480 481 - int kxsd9_common_remove(struct device *dev) 481 + void kxsd9_common_remove(struct device *dev) 482 482 { 483 483 struct iio_dev *indio_dev = dev_get_drvdata(dev); 484 484 struct kxsd9_state *st = iio_priv(indio_dev); ··· 489 489 pm_runtime_put_noidle(dev); 490 490 pm_runtime_disable(dev); 491 491 kxsd9_power_down(st); 492 - 493 - return 0; 494 492 } 495 493 EXPORT_SYMBOL(kxsd9_common_remove); 496 494
+1 -1
drivers/iio/accel/kxsd9.h
··· 8 8 int kxsd9_common_probe(struct device *dev, 9 9 struct regmap *map, 10 10 const char *name); 11 - int kxsd9_common_remove(struct device *dev); 11 + void kxsd9_common_remove(struct device *dev); 12 12 13 13 extern const struct dev_pm_ops kxsd9_dev_pm_ops;
+1 -1
drivers/iio/accel/mma7455.h
··· 11 11 12 12 int mma7455_core_probe(struct device *dev, struct regmap *regmap, 13 13 const char *name); 14 - int mma7455_core_remove(struct device *dev); 14 + void mma7455_core_remove(struct device *dev); 15 15 16 16 #endif
+1 -3
drivers/iio/accel/mma7455_core.c
··· 294 294 } 295 295 EXPORT_SYMBOL_GPL(mma7455_core_probe); 296 296 297 - int mma7455_core_remove(struct device *dev) 297 + void mma7455_core_remove(struct device *dev) 298 298 { 299 299 struct iio_dev *indio_dev = dev_get_drvdata(dev); 300 300 struct mma7455_data *mma7455 = iio_priv(indio_dev); ··· 304 304 305 305 regmap_write(mma7455->regmap, MMA7455_REG_MCTL, 306 306 MMA7455_MCTL_MODE_STANDBY); 307 - 308 - return 0; 309 307 } 310 308 EXPORT_SYMBOL_GPL(mma7455_core_remove); 311 309
+3 -1
drivers/iio/accel/mma7455_i2c.c
··· 28 28 29 29 static int mma7455_i2c_remove(struct i2c_client *i2c) 30 30 { 31 - return mma7455_core_remove(&i2c->dev); 31 + mma7455_core_remove(&i2c->dev); 32 + 33 + return 0; 32 34 } 33 35 34 36 static const struct i2c_device_id mma7455_i2c_ids[] = {
+3 -1
drivers/iio/accel/mma7455_spi.c
··· 24 24 25 25 static int mma7455_spi_remove(struct spi_device *spi) 26 26 { 27 - return mma7455_core_remove(&spi->dev); 27 + mma7455_core_remove(&spi->dev); 28 + 29 + return 0; 28 30 } 29 31 30 32 static const struct spi_device_id mma7455_spi_ids[] = {
+1 -1
drivers/iio/accel/mma7660.c
··· 254 254 }; 255 255 MODULE_DEVICE_TABLE(of, mma7660_of_match); 256 256 257 - static const struct acpi_device_id mma7660_acpi_id[] = { 257 + static const struct acpi_device_id __maybe_unused mma7660_acpi_id[] = { 258 258 {"MMA7660", 0}, 259 259 {} 260 260 };
+5 -26
drivers/iio/accel/st_accel_core.c
··· 1210 1210 }; 1211 1211 1212 1212 1213 - adev = ACPI_COMPANION(adata->dev); 1213 + adev = ACPI_COMPANION(indio_dev->dev.parent); 1214 1214 if (!adev) 1215 1215 return 0; 1216 1216 ··· 1334 1334 int st_accel_common_probe(struct iio_dev *indio_dev) 1335 1335 { 1336 1336 struct st_sensor_data *adata = iio_priv(indio_dev); 1337 - struct st_sensors_platform_data *pdata = dev_get_platdata(adata->dev); 1337 + struct device *parent = indio_dev->dev.parent; 1338 + struct st_sensors_platform_data *pdata = dev_get_platdata(parent); 1338 1339 int err; 1339 1340 1340 1341 indio_dev->modes = INDIO_DIRECT_MODE; ··· 1355 1354 */ 1356 1355 err = apply_acpi_orientation(indio_dev); 1357 1356 if (err) { 1358 - err = iio_read_mount_matrix(adata->dev, &adata->mount_matrix); 1357 + err = iio_read_mount_matrix(parent, &adata->mount_matrix); 1359 1358 if (err) 1360 1359 return err; 1361 1360 } ··· 1381 1380 return err; 1382 1381 } 1383 1382 1384 - err = iio_device_register(indio_dev); 1385 - if (err) 1386 - goto st_accel_device_register_error; 1387 - 1388 - dev_info(&indio_dev->dev, "registered accelerometer %s\n", 1389 - indio_dev->name); 1390 - 1391 - return 0; 1392 - 1393 - st_accel_device_register_error: 1394 - if (adata->irq > 0) 1395 - st_sensors_deallocate_trigger(indio_dev); 1396 - return err; 1383 + return devm_iio_device_register(parent, indio_dev); 1397 1384 } 1398 1385 EXPORT_SYMBOL(st_accel_common_probe); 1399 - 1400 - void st_accel_common_remove(struct iio_dev *indio_dev) 1401 - { 1402 - struct st_sensor_data *adata = iio_priv(indio_dev); 1403 - 1404 - iio_device_unregister(indio_dev); 1405 - if (adata->irq > 0) 1406 - st_sensors_deallocate_trigger(indio_dev); 1407 - } 1408 - EXPORT_SYMBOL(st_accel_common_remove); 1409 1386 1410 1387 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 1411 1388 MODULE_DESCRIPTION("STMicroelectronics accelerometers driver");
+1 -22
drivers/iio/accel/st_accel_i2c.c
··· 177 177 if (ret) 178 178 return ret; 179 179 180 - ret = st_accel_common_probe(indio_dev); 181 - if (ret < 0) 182 - goto st_accel_power_off; 183 - 184 - return 0; 185 - 186 - st_accel_power_off: 187 - st_sensors_power_disable(indio_dev); 188 - 189 - return ret; 190 - } 191 - 192 - static int st_accel_i2c_remove(struct i2c_client *client) 193 - { 194 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 195 - 196 - st_sensors_power_disable(indio_dev); 197 - 198 - st_accel_common_remove(indio_dev); 199 - 200 - return 0; 180 + return st_accel_common_probe(indio_dev); 201 181 } 202 182 203 183 static struct i2c_driver st_accel_driver = { ··· 187 207 .acpi_match_table = ACPI_PTR(st_accel_acpi_match), 188 208 }, 189 209 .probe_new = st_accel_i2c_probe, 190 - .remove = st_accel_i2c_remove, 191 210 .id_table = st_accel_id_table, 192 211 }; 193 212 module_i2c_driver(st_accel_driver);
+1 -22
drivers/iio/accel/st_accel_spi.c
··· 127 127 if (err) 128 128 return err; 129 129 130 - err = st_accel_common_probe(indio_dev); 131 - if (err < 0) 132 - goto st_accel_power_off; 133 - 134 - return 0; 135 - 136 - st_accel_power_off: 137 - st_sensors_power_disable(indio_dev); 138 - 139 - return err; 140 - } 141 - 142 - static int st_accel_spi_remove(struct spi_device *spi) 143 - { 144 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 145 - 146 - st_sensors_power_disable(indio_dev); 147 - 148 - st_accel_common_remove(indio_dev); 149 - 150 - return 0; 130 + return st_accel_common_probe(indio_dev); 151 131 } 152 132 153 133 static const struct spi_device_id st_accel_id_table[] = { ··· 157 177 .of_match_table = st_accel_of_match, 158 178 }, 159 179 .probe = st_accel_spi_probe, 160 - .remove = st_accel_spi_remove, 161 180 .id_table = st_accel_id_table, 162 181 }; 163 182 module_spi_driver(st_accel_driver);
+14 -4
drivers/iio/adc/Kconfig
··· 354 354 355 355 config BERLIN2_ADC 356 356 tristate "Marvell Berlin2 ADC driver" 357 - depends on ARCH_BERLIN 357 + depends on ARCH_BERLIN || COMPILE_TEST 358 358 help 359 359 Marvell Berlin2 ADC driver. This ADC has 8 channels, with one used for 360 360 temperature measurement. ··· 430 430 depends on ARCH_EXYNOS || ARCH_S3C24XX || ARCH_S3C64XX || ARCH_S5PV210 || (OF && COMPILE_TEST) 431 431 depends on HAS_IOMEM 432 432 help 433 - Core support for the ADC block found in the Samsung EXYNOS series 434 - of SoCs for drivers such as the touchscreen and hwmon to use to share 435 - this resource. 433 + Driver for the ADC block found in the Samsung S3C (S3C2410, S3C2416, 434 + S3C2440, S3C2443, S3C6410), S5Pv210 and Exynos SoCs. 435 + Choose Y here only if you build for such Samsung SoC. 436 436 437 437 To compile this driver as a module, choose M here: the module will be 438 438 called exynos_adc. ··· 529 529 530 530 This driver can also be built as a module. If so, the module will be 531 531 called imx7d_adc. 532 + 533 + config IMX8QXP_ADC 534 + tristate "NXP IMX8QXP ADC driver" 535 + depends on ARCH_MXC_ARM64 || COMPILE_TEST 536 + depends on HAS_IOMEM 537 + help 538 + Say yes here to build support for IMX8QXP ADC. 539 + 540 + This driver can also be built as a module. If so, the module will be 541 + called imx8qxp-adc. 532 542 533 543 config LP8788_ADC 534 544 tristate "LP8788 ADC driver"
+1
drivers/iio/adc/Makefile
··· 46 46 obj-$(CONFIG_HI8435) += hi8435.o 47 47 obj-$(CONFIG_HX711) += hx711.o 48 48 obj-$(CONFIG_IMX7D_ADC) += imx7d_adc.o 49 + obj-$(CONFIG_IMX8QXP_ADC) += imx8qxp-adc.o 49 50 obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o 50 51 obj-$(CONFIG_INGENIC_ADC) += ingenic-adc.o 51 52 obj-$(CONFIG_INTEL_MRFLD_ADC) += intel_mrfld_adc.o
+22 -48
drivers/iio/adc/ad7291.c
··· 460 460 .write_event_value = &ad7291_write_event_value, 461 461 }; 462 462 463 + static void ad7291_reg_disable(void *reg) 464 + { 465 + regulator_disable(reg); 466 + } 467 + 463 468 static int ad7291_probe(struct i2c_client *client, 464 469 const struct i2c_device_id *id) 465 470 { ··· 478 473 chip = iio_priv(indio_dev); 479 474 480 475 mutex_init(&chip->state_lock); 481 - /* this is only used for device removal purposes */ 482 - i2c_set_clientdata(client, indio_dev); 483 476 484 477 chip->client = client; 485 478 ··· 498 495 if (ret) 499 496 return ret; 500 497 498 + ret = devm_add_action_or_reset(&client->dev, ad7291_reg_disable, 499 + chip->reg); 500 + if (ret) 501 + return ret; 502 + 501 503 chip->command |= AD7291_EXT_REF; 502 504 } 503 505 ··· 514 506 indio_dev->modes = INDIO_DIRECT_MODE; 515 507 516 508 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET); 517 - if (ret) { 518 - ret = -EIO; 519 - goto error_disable_reg; 520 - } 509 + if (ret) 510 + return -EIO; 521 511 522 512 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command); 523 - if (ret) { 524 - ret = -EIO; 525 - goto error_disable_reg; 526 - } 513 + if (ret) 514 + return -EIO; 527 515 528 516 if (client->irq > 0) { 529 - ret = request_threaded_irq(client->irq, 530 - NULL, 531 - &ad7291_event_handler, 532 - IRQF_TRIGGER_LOW | IRQF_ONESHOT, 533 - id->name, 534 - indio_dev); 517 + ret = devm_request_threaded_irq(&client->dev, client->irq, 518 + NULL, 519 + &ad7291_event_handler, 520 + IRQF_TRIGGER_LOW | IRQF_ONESHOT, 521 + id->name, 522 + indio_dev); 535 523 if (ret) 536 - goto error_disable_reg; 524 + return ret; 537 525 } 538 526 539 - ret = iio_device_register(indio_dev); 540 - if (ret) 541 - goto error_unreg_irq; 542 - 543 - return 0; 544 - 545 - error_unreg_irq: 546 - if (client->irq) 547 - free_irq(client->irq, indio_dev); 548 - error_disable_reg: 549 - if (chip->reg) 550 - regulator_disable(chip->reg); 551 - 552 - return ret; 553 - } 554 - 555 - static int ad7291_remove(struct i2c_client *client) 556 - { 557 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 558 - struct ad7291_chip_info *chip = iio_priv(indio_dev); 559 - 560 - iio_device_unregister(indio_dev); 561 - 562 - if (client->irq) 563 - free_irq(client->irq, indio_dev); 564 - 565 - if (chip->reg) 566 - regulator_disable(chip->reg); 567 - 568 - return 0; 527 + return devm_iio_device_register(&client->dev, indio_dev); 569 528 } 570 529 571 530 static const struct i2c_device_id ad7291_id[] = { ··· 554 579 .of_match_table = ad7291_of_match, 555 580 }, 556 581 .probe = ad7291_probe, 557 - .remove = ad7291_remove, 558 582 .id_table = ad7291_id, 559 583 }; 560 584 module_i2c_driver(ad7291_driver);
+181 -75
drivers/iio/adc/ad7949.c
··· 11 11 #include <linux/module.h> 12 12 #include <linux/regulator/consumer.h> 13 13 #include <linux/spi/spi.h> 14 + #include <linux/bitfield.h> 14 15 15 - #define AD7949_MASK_CHANNEL_SEL GENMASK(9, 7) 16 - #define AD7949_MASK_TOTAL GENMASK(13, 0) 17 - #define AD7949_OFFSET_CHANNEL_SEL 7 18 - #define AD7949_CFG_READ_BACK 0x1 19 - #define AD7949_CFG_REG_SIZE_BITS 14 16 + #define AD7949_CFG_MASK_TOTAL GENMASK(13, 0) 17 + 18 + /* CFG: Configuration Update */ 19 + #define AD7949_CFG_MASK_OVERWRITE BIT(13) 20 + 21 + /* INCC: Input Channel Configuration */ 22 + #define AD7949_CFG_MASK_INCC GENMASK(12, 10) 23 + #define AD7949_CFG_VAL_INCC_UNIPOLAR_GND 7 24 + #define AD7949_CFG_VAL_INCC_UNIPOLAR_COMM 6 25 + #define AD7949_CFG_VAL_INCC_UNIPOLAR_DIFF 4 26 + #define AD7949_CFG_VAL_INCC_TEMP 3 27 + #define AD7949_CFG_VAL_INCC_BIPOLAR 2 28 + #define AD7949_CFG_VAL_INCC_BIPOLAR_DIFF 0 29 + 30 + /* INX: Input channel Selection in a binary fashion */ 31 + #define AD7949_CFG_MASK_INX GENMASK(9, 7) 32 + 33 + /* BW: select bandwidth for low-pass filter. Full or Quarter */ 34 + #define AD7949_CFG_MASK_BW_FULL BIT(6) 35 + 36 + /* REF: reference/buffer selection */ 37 + #define AD7949_CFG_MASK_REF GENMASK(5, 3) 38 + #define AD7949_CFG_VAL_REF_EXT_TEMP_BUF 3 39 + #define AD7949_CFG_VAL_REF_EXT_TEMP 2 40 + #define AD7949_CFG_VAL_REF_INT_4096 1 41 + #define AD7949_CFG_VAL_REF_INT_2500 0 42 + #define AD7949_CFG_VAL_REF_EXTERNAL BIT(1) 43 + 44 + /* SEQ: channel sequencer. Allows for scanning channels */ 45 + #define AD7949_CFG_MASK_SEQ GENMASK(2, 1) 46 + 47 + /* RB: Read back the CFG register */ 48 + #define AD7949_CFG_MASK_RBN BIT(0) 20 49 21 50 enum { 22 51 ID_AD7949 = 0, ··· 70 41 * @vref: regulator generating Vref 71 42 * @indio_dev: reference to iio structure 72 43 * @spi: reference to spi structure 44 + * @refsel: reference selection 73 45 * @resolution: resolution of the chip 74 46 * @cfg: copy of the configuration register 75 47 * @current_channel: current channel in use 76 48 * @buffer: buffer to send / receive data to / from device 49 + * @buf8b: be16 buffer to exchange data with the device in 8-bit transfers 77 50 */ 78 51 struct ad7949_adc_chip { 79 52 struct mutex lock; 80 53 struct regulator *vref; 81 54 struct iio_dev *indio_dev; 82 55 struct spi_device *spi; 56 + u32 refsel; 83 57 u8 resolution; 84 58 u16 cfg; 85 59 unsigned int current_channel; 86 60 u16 buffer ____cacheline_aligned; 61 + __be16 buf8b; 87 62 }; 88 63 89 64 static int ad7949_spi_write_cfg(struct ad7949_adc_chip *ad7949_adc, u16 val, 90 65 u16 mask) 91 66 { 92 67 int ret; 93 - int bits_per_word = ad7949_adc->resolution; 94 - int shift = bits_per_word - AD7949_CFG_REG_SIZE_BITS; 95 - struct spi_message msg; 96 - struct spi_transfer tx[] = { 97 - { 98 - .tx_buf = &ad7949_adc->buffer, 99 - .len = 2, 100 - .bits_per_word = bits_per_word, 101 - }, 102 - }; 103 68 104 69 ad7949_adc->cfg = (val & mask) | (ad7949_adc->cfg & ~mask); 105 - ad7949_adc->buffer = ad7949_adc->cfg << shift; 106 - spi_message_init_with_transfers(&msg, tx, 1); 107 - ret = spi_sync(ad7949_adc->spi, &msg); 70 + 71 + switch (ad7949_adc->spi->bits_per_word) { 72 + case 16: 73 + ad7949_adc->buffer = ad7949_adc->cfg << 2; 74 + ret = spi_write(ad7949_adc->spi, &ad7949_adc->buffer, 2); 75 + break; 76 + case 14: 77 + ad7949_adc->buffer = ad7949_adc->cfg; 78 + ret = spi_write(ad7949_adc->spi, &ad7949_adc->buffer, 2); 79 + break; 80 + case 8: 81 + /* Here, type is big endian as it must be sent in two transfers */ 82 + ad7949_adc->buf8b = cpu_to_be16(ad7949_adc->cfg << 2); 83 + ret = spi_write(ad7949_adc->spi, &ad7949_adc->buf8b, 2); 84 + break; 85 + default: 86 + dev_err(&ad7949_adc->indio_dev->dev, "unsupported BPW\n"); 87 + return -EINVAL; 88 + } 108 89 109 90 /* 110 91 * This delay is to avoid a new request before the required time to ··· 129 90 { 130 91 int ret; 131 92 int i; 132 - int bits_per_word = ad7949_adc->resolution; 133 - int mask = GENMASK(ad7949_adc->resolution - 1, 0); 134 - struct spi_message msg; 135 - struct spi_transfer tx[] = { 136 - { 137 - .rx_buf = &ad7949_adc->buffer, 138 - .len = 2, 139 - .bits_per_word = bits_per_word, 140 - }, 141 - }; 142 93 143 94 /* 144 95 * 1: write CFG for sample N and read old data (sample N-2) ··· 138 109 */ 139 110 for (i = 0; i < 2; i++) { 140 111 ret = ad7949_spi_write_cfg(ad7949_adc, 141 - channel << AD7949_OFFSET_CHANNEL_SEL, 142 - AD7949_MASK_CHANNEL_SEL); 112 + FIELD_PREP(AD7949_CFG_MASK_INX, channel), 113 + AD7949_CFG_MASK_INX); 143 114 if (ret) 144 115 return ret; 145 116 if (channel == ad7949_adc->current_channel) ··· 147 118 } 148 119 149 120 /* 3: write something and read actual data */ 150 - ad7949_adc->buffer = 0; 151 - spi_message_init_with_transfers(&msg, tx, 1); 152 - ret = spi_sync(ad7949_adc->spi, &msg); 121 + if (ad7949_adc->spi->bits_per_word == 8) 122 + ret = spi_read(ad7949_adc->spi, &ad7949_adc->buf8b, 2); 123 + else 124 + ret = spi_read(ad7949_adc->spi, &ad7949_adc->buffer, 2); 125 + 153 126 if (ret) 154 127 return ret; 155 128 ··· 163 132 164 133 ad7949_adc->current_channel = channel; 165 134 166 - *val = ad7949_adc->buffer & mask; 135 + switch (ad7949_adc->spi->bits_per_word) { 136 + case 16: 137 + *val = ad7949_adc->buffer; 138 + /* Shift-out padding bits */ 139 + *val >>= 16 - ad7949_adc->resolution; 140 + break; 141 + case 14: 142 + *val = ad7949_adc->buffer & GENMASK(13, 0); 143 + break; 144 + case 8: 145 + /* Here, type is big endian as data was sent in two transfers */ 146 + *val = be16_to_cpu(ad7949_adc->buf8b); 147 + /* Shift-out padding bits */ 148 + *val >>= 16 - ad7949_adc->resolution; 149 + break; 150 + default: 151 + dev_err(&ad7949_adc->indio_dev->dev, "unsupported BPW\n"); 152 + return -EINVAL; 153 + } 167 154 168 155 return 0; 169 156 } ··· 227 178 return IIO_VAL_INT; 228 179 229 180 case IIO_CHAN_INFO_SCALE: 230 - ret = regulator_get_voltage(ad7949_adc->vref); 231 - if (ret < 0) 232 - return ret; 181 + switch (ad7949_adc->refsel) { 182 + case AD7949_CFG_VAL_REF_INT_2500: 183 + *val = 2500; 184 + break; 185 + case AD7949_CFG_VAL_REF_INT_4096: 186 + *val = 4096; 187 + break; 188 + case AD7949_CFG_VAL_REF_EXT_TEMP: 189 + case AD7949_CFG_VAL_REF_EXT_TEMP_BUF: 190 + ret = regulator_get_voltage(ad7949_adc->vref); 191 + if (ret < 0) 192 + return ret; 233 193 234 - *val = ret / 5000; 235 - return IIO_VAL_INT; 194 + /* convert value back to mV */ 195 + *val = ret / 1000; 196 + break; 197 + } 198 + 199 + *val2 = (1 << ad7949_adc->resolution) - 1; 200 + return IIO_VAL_FRACTIONAL; 236 201 } 237 202 238 203 return -EINVAL; ··· 262 199 if (readval) 263 200 *readval = ad7949_adc->cfg; 264 201 else 265 - ret = ad7949_spi_write_cfg(ad7949_adc, 266 - writeval & AD7949_MASK_TOTAL, AD7949_MASK_TOTAL); 202 + ret = ad7949_spi_write_cfg(ad7949_adc, writeval, 203 + AD7949_CFG_MASK_TOTAL); 267 204 268 205 return ret; 269 206 } ··· 277 214 { 278 215 int ret; 279 216 int val; 217 + u16 cfg; 280 218 281 - /* Sequencer disabled, CFG readback disabled, IN0 as default channel */ 282 219 ad7949_adc->current_channel = 0; 283 - ret = ad7949_spi_write_cfg(ad7949_adc, 0x3C79, AD7949_MASK_TOTAL); 220 + 221 + cfg = FIELD_PREP(AD7949_CFG_MASK_OVERWRITE, 1) | 222 + FIELD_PREP(AD7949_CFG_MASK_INCC, AD7949_CFG_VAL_INCC_UNIPOLAR_GND) | 223 + FIELD_PREP(AD7949_CFG_MASK_INX, ad7949_adc->current_channel) | 224 + FIELD_PREP(AD7949_CFG_MASK_BW_FULL, 1) | 225 + FIELD_PREP(AD7949_CFG_MASK_REF, ad7949_adc->refsel) | 226 + FIELD_PREP(AD7949_CFG_MASK_SEQ, 0x0) | 227 + FIELD_PREP(AD7949_CFG_MASK_RBN, 1); 228 + 229 + ret = ad7949_spi_write_cfg(ad7949_adc, cfg, AD7949_CFG_MASK_TOTAL); 284 230 285 231 /* 286 232 * Do two dummy conversions to apply the first configuration setting. ··· 301 229 return ret; 302 230 } 303 231 232 + static void ad7949_disable_reg(void *reg) 233 + { 234 + regulator_disable(reg); 235 + } 236 + 304 237 static int ad7949_spi_probe(struct spi_device *spi) 305 238 { 239 + u32 spi_ctrl_mask = spi->controller->bits_per_word_mask; 306 240 struct device *dev = &spi->dev; 307 241 const struct ad7949_adc_spec *spec; 308 242 struct ad7949_adc_chip *ad7949_adc; 309 243 struct iio_dev *indio_dev; 244 + u32 tmp; 310 245 int ret; 311 246 312 247 indio_dev = devm_iio_device_alloc(dev, sizeof(*ad7949_adc)); ··· 336 257 indio_dev->num_channels = spec->num_channels; 337 258 ad7949_adc->resolution = spec->resolution; 338 259 339 - ad7949_adc->vref = devm_regulator_get(dev, "vref"); 340 - if (IS_ERR(ad7949_adc->vref)) { 341 - dev_err(dev, "fail to request regulator\n"); 342 - return PTR_ERR(ad7949_adc->vref); 260 + /* Set SPI bits per word */ 261 + if (spi_ctrl_mask & SPI_BPW_MASK(ad7949_adc->resolution)) { 262 + spi->bits_per_word = ad7949_adc->resolution; 263 + } else if (spi_ctrl_mask == SPI_BPW_MASK(16)) { 264 + spi->bits_per_word = 16; 265 + } else if (spi_ctrl_mask == SPI_BPW_MASK(8)) { 266 + spi->bits_per_word = 8; 267 + } else { 268 + dev_err(dev, "unable to find common BPW with spi controller\n"); 269 + return -EINVAL; 343 270 } 344 271 345 - ret = regulator_enable(ad7949_adc->vref); 346 - if (ret < 0) { 347 - dev_err(dev, "fail to enable regulator\n"); 348 - return ret; 272 + /* Setup internal voltage reference */ 273 + tmp = 4096000; 274 + device_property_read_u32(dev, "adi,internal-ref-microvolt", &tmp); 275 + 276 + switch (tmp) { 277 + case 2500000: 278 + ad7949_adc->refsel = AD7949_CFG_VAL_REF_INT_2500; 279 + break; 280 + case 4096000: 281 + ad7949_adc->refsel = AD7949_CFG_VAL_REF_INT_4096; 282 + break; 283 + default: 284 + dev_err(dev, "unsupported internal voltage reference\n"); 285 + return -EINVAL; 286 + } 287 + 288 + /* Setup external voltage reference, buffered? */ 289 + ad7949_adc->vref = devm_regulator_get_optional(dev, "vrefin"); 290 + if (IS_ERR(ad7949_adc->vref)) { 291 + ret = PTR_ERR(ad7949_adc->vref); 292 + if (ret != -ENODEV) 293 + return ret; 294 + /* unbuffered? */ 295 + ad7949_adc->vref = devm_regulator_get_optional(dev, "vref"); 296 + if (IS_ERR(ad7949_adc->vref)) { 297 + ret = PTR_ERR(ad7949_adc->vref); 298 + if (ret != -ENODEV) 299 + return ret; 300 + } else { 301 + ad7949_adc->refsel = AD7949_CFG_VAL_REF_EXT_TEMP; 302 + } 303 + } else { 304 + ad7949_adc->refsel = AD7949_CFG_VAL_REF_EXT_TEMP_BUF; 305 + } 306 + 307 + if (ad7949_adc->refsel & AD7949_CFG_VAL_REF_EXTERNAL) { 308 + ret = regulator_enable(ad7949_adc->vref); 309 + if (ret < 0) { 310 + dev_err(dev, "fail to enable regulator\n"); 311 + return ret; 312 + } 313 + 314 + ret = devm_add_action_or_reset(dev, ad7949_disable_reg, 315 + ad7949_adc->vref); 316 + if (ret) 317 + return ret; 349 318 } 350 319 351 320 mutex_init(&ad7949_adc->lock); ··· 401 274 ret = ad7949_spi_init(ad7949_adc); 402 275 if (ret) { 403 276 dev_err(dev, "enable to init this device: %d\n", ret); 404 - goto err; 277 + return ret; 405 278 } 406 279 407 - ret = iio_device_register(indio_dev); 408 - if (ret) { 280 + ret = devm_iio_device_register(dev, indio_dev); 281 + if (ret) 409 282 dev_err(dev, "fail to register iio device: %d\n", ret); 410 - goto err; 411 - } 412 - 413 - return 0; 414 - 415 - err: 416 - mutex_destroy(&ad7949_adc->lock); 417 - regulator_disable(ad7949_adc->vref); 418 283 419 284 return ret; 420 - } 421 - 422 - static int ad7949_spi_remove(struct spi_device *spi) 423 - { 424 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 425 - struct ad7949_adc_chip *ad7949_adc = iio_priv(indio_dev); 426 - 427 - iio_device_unregister(indio_dev); 428 - mutex_destroy(&ad7949_adc->lock); 429 - regulator_disable(ad7949_adc->vref); 430 - 431 - return 0; 432 285 } 433 286 434 287 static const struct of_device_id ad7949_spi_of_id[] = { ··· 433 326 .of_match_table = ad7949_spi_of_id, 434 327 }, 435 328 .probe = ad7949_spi_probe, 436 - .remove = ad7949_spi_remove, 437 329 .id_table = ad7949_spi_id, 438 330 }; 439 331 module_spi_driver(ad7949_spi_driver);
+51 -17
drivers/iio/adc/ad799x.c
··· 299 299 GENMASK(chan->scan_type.realbits - 1, 0); 300 300 return IIO_VAL_INT; 301 301 case IIO_CHAN_INFO_SCALE: 302 - ret = regulator_get_voltage(st->vref); 302 + if (st->vref) 303 + ret = regulator_get_voltage(st->vref); 304 + else 305 + ret = regulator_get_voltage(st->reg); 306 + 303 307 if (ret < 0) 304 308 return ret; 305 309 *val = ret / 1000; ··· 774 770 const struct i2c_device_id *id) 775 771 { 776 772 int ret; 773 + int extra_config = 0; 777 774 struct ad799x_state *st; 778 775 struct iio_dev *indio_dev; 779 776 const struct ad799x_chip_info *chip_info = ··· 802 797 ret = regulator_enable(st->reg); 803 798 if (ret) 804 799 return ret; 805 - st->vref = devm_regulator_get(&client->dev, "vref"); 800 + 801 + /* check if an external reference is supplied */ 802 + st->vref = devm_regulator_get_optional(&client->dev, "vref"); 803 + 806 804 if (IS_ERR(st->vref)) { 807 - ret = PTR_ERR(st->vref); 808 - goto error_disable_reg; 805 + if (PTR_ERR(st->vref) == -ENODEV) { 806 + st->vref = NULL; 807 + dev_info(&client->dev, "Using VCC reference voltage\n"); 808 + } else { 809 + ret = PTR_ERR(st->vref); 810 + goto error_disable_reg; 811 + } 809 812 } 810 - ret = regulator_enable(st->vref); 811 - if (ret) 812 - goto error_disable_reg; 813 + 814 + if (st->vref) { 815 + /* 816 + * Use external reference voltage if supported by hardware. 817 + * This is optional if voltage / regulator present, use VCC otherwise. 818 + */ 819 + if ((st->id == ad7991) || (st->id == ad7995) || (st->id == ad7999)) { 820 + dev_info(&client->dev, "Using external reference voltage\n"); 821 + extra_config |= AD7991_REF_SEL; 822 + ret = regulator_enable(st->vref); 823 + if (ret) 824 + goto error_disable_reg; 825 + } else { 826 + st->vref = NULL; 827 + dev_warn(&client->dev, "Supplied reference not supported\n"); 828 + } 829 + } 813 830 814 831 st->client = client; 815 832 ··· 842 815 indio_dev->channels = st->chip_config->channel; 843 816 indio_dev->num_channels = chip_info->num_channels; 844 817 845 - ret = ad799x_update_config(st, st->chip_config->default_config); 818 + ret = ad799x_update_config(st, st->chip_config->default_config | extra_config); 846 819 if (ret) 847 820 goto error_disable_vref; 848 821 ··· 872 845 error_cleanup_ring: 873 846 iio_triggered_buffer_cleanup(indio_dev); 874 847 error_disable_vref: 875 - regulator_disable(st->vref); 848 + if (st->vref) 849 + regulator_disable(st->vref); 876 850 error_disable_reg: 877 851 regulator_disable(st->reg); 878 852 ··· 888 860 iio_device_unregister(indio_dev); 889 861 890 862 iio_triggered_buffer_cleanup(indio_dev); 891 - regulator_disable(st->vref); 863 + if (st->vref) 864 + regulator_disable(st->vref); 892 865 regulator_disable(st->reg); 893 866 kfree(st->rx_buf); 894 867 ··· 901 872 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 902 873 struct ad799x_state *st = iio_priv(indio_dev); 903 874 904 - regulator_disable(st->vref); 875 + if (st->vref) 876 + regulator_disable(st->vref); 905 877 regulator_disable(st->reg); 906 878 907 879 return 0; ··· 919 889 dev_err(dev, "Unable to enable vcc regulator\n"); 920 890 return ret; 921 891 } 922 - ret = regulator_enable(st->vref); 923 - if (ret) { 924 - regulator_disable(st->reg); 925 - dev_err(dev, "Unable to enable vref regulator\n"); 926 - return ret; 892 + 893 + if (st->vref) { 894 + ret = regulator_enable(st->vref); 895 + if (ret) { 896 + regulator_disable(st->reg); 897 + dev_err(dev, "Unable to enable vref regulator\n"); 898 + return ret; 899 + } 927 900 } 928 901 929 902 /* resync config */ 930 903 ret = ad799x_update_config(st, st->config); 931 904 if (ret) { 932 - regulator_disable(st->vref); 905 + if (st->vref) 906 + regulator_disable(st->vref); 933 907 regulator_disable(st->reg); 934 908 return ret; 935 909 }
+492 -98
drivers/iio/adc/aspeed_adc.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 2 /* 3 - * Aspeed AST2400/2500 ADC 3 + * Aspeed AST2400/2500/2600 ADC 4 4 * 5 5 * Copyright (C) 2017 Google, Inc. 6 + * Copyright (C) 2021 Aspeed Technology Inc. 7 + * 8 + * ADC clock formula: 9 + * Ast2400/Ast2500: 10 + * clock period = period of PCLK * 2 * (ADC0C[31:17] + 1) * (ADC0C[9:0] + 1) 11 + * Ast2600: 12 + * clock period = period of PCLK * 2 * (ADC0C[15:0] + 1) 6 13 */ 7 14 8 15 #include <linux/clk.h> ··· 20 13 #include <linux/module.h> 21 14 #include <linux/of_platform.h> 22 15 #include <linux/platform_device.h> 16 + #include <linux/regulator/consumer.h> 23 17 #include <linux/reset.h> 24 18 #include <linux/spinlock.h> 25 19 #include <linux/types.h> 20 + #include <linux/bitfield.h> 21 + #include <linux/regmap.h> 22 + #include <linux/mfd/syscon.h> 26 23 27 24 #include <linux/iio/iio.h> 28 25 #include <linux/iio/driver.h> ··· 39 28 #define ASPEED_REG_INTERRUPT_CONTROL 0x04 40 29 #define ASPEED_REG_VGA_DETECT_CONTROL 0x08 41 30 #define ASPEED_REG_CLOCK_CONTROL 0x0C 42 - #define ASPEED_REG_MAX 0xC0 31 + #define ASPEED_REG_COMPENSATION_TRIM 0xC4 32 + /* 33 + * The register offset between 0xC8~0xCC can be read and won't affect the 34 + * hardware logic in each version of ADC. 35 + */ 36 + #define ASPEED_REG_MAX 0xD0 43 37 44 - #define ASPEED_OPERATION_MODE_POWER_DOWN (0x0 << 1) 45 - #define ASPEED_OPERATION_MODE_STANDBY (0x1 << 1) 46 - #define ASPEED_OPERATION_MODE_NORMAL (0x7 << 1) 47 - 48 - #define ASPEED_ENGINE_ENABLE BIT(0) 49 - 50 - #define ASPEED_ADC_CTRL_INIT_RDY BIT(8) 38 + #define ASPEED_ADC_ENGINE_ENABLE BIT(0) 39 + #define ASPEED_ADC_OP_MODE GENMASK(3, 1) 40 + #define ASPEED_ADC_OP_MODE_PWR_DOWN 0 41 + #define ASPEED_ADC_OP_MODE_STANDBY 1 42 + #define ASPEED_ADC_OP_MODE_NORMAL 7 43 + #define ASPEED_ADC_CTRL_COMPENSATION BIT(4) 44 + #define ASPEED_ADC_AUTO_COMPENSATION BIT(5) 45 + /* 46 + * Bit 6 determines not only the reference voltage range but also the dividing 47 + * circuit for battery sensing. 48 + */ 49 + #define ASPEED_ADC_REF_VOLTAGE GENMASK(7, 6) 50 + #define ASPEED_ADC_REF_VOLTAGE_2500mV 0 51 + #define ASPEED_ADC_REF_VOLTAGE_1200mV 1 52 + #define ASPEED_ADC_REF_VOLTAGE_EXT_HIGH 2 53 + #define ASPEED_ADC_REF_VOLTAGE_EXT_LOW 3 54 + #define ASPEED_ADC_BAT_SENSING_DIV BIT(6) 55 + #define ASPEED_ADC_BAT_SENSING_DIV_2_3 0 56 + #define ASPEED_ADC_BAT_SENSING_DIV_1_3 1 57 + #define ASPEED_ADC_CTRL_INIT_RDY BIT(8) 58 + #define ASPEED_ADC_CH7_MODE BIT(12) 59 + #define ASPEED_ADC_CH7_NORMAL 0 60 + #define ASPEED_ADC_CH7_BAT 1 61 + #define ASPEED_ADC_BAT_SENSING_ENABLE BIT(13) 62 + #define ASPEED_ADC_CTRL_CHANNEL GENMASK(31, 16) 63 + #define ASPEED_ADC_CTRL_CHANNEL_ENABLE(ch) FIELD_PREP(ASPEED_ADC_CTRL_CHANNEL, BIT(ch)) 51 64 52 65 #define ASPEED_ADC_INIT_POLLING_TIME 500 53 66 #define ASPEED_ADC_INIT_TIMEOUT 500000 67 + /* 68 + * When the sampling rate is too high, the ADC may not have enough charging 69 + * time, resulting in a low voltage value. Thus, the default uses a slow 70 + * sampling rate for most use cases. 71 + */ 72 + #define ASPEED_ADC_DEF_SAMPLING_RATE 65000 73 + 74 + struct aspeed_adc_trim_locate { 75 + const unsigned int offset; 76 + const unsigned int field; 77 + }; 54 78 55 79 struct aspeed_adc_model_data { 56 80 const char *model_name; 57 81 unsigned int min_sampling_rate; // Hz 58 82 unsigned int max_sampling_rate; // Hz 59 - unsigned int vref_voltage; // mV 83 + unsigned int vref_fixed_mv; 60 84 bool wait_init_sequence; 85 + bool need_prescaler; 86 + bool bat_sense_sup; 87 + u8 scaler_bit_width; 88 + unsigned int num_channels; 89 + const struct aspeed_adc_trim_locate *trim_locate; 90 + }; 91 + 92 + struct adc_gain { 93 + u8 mult; 94 + u8 div; 61 95 }; 62 96 63 97 struct aspeed_adc_data { 64 98 struct device *dev; 99 + const struct aspeed_adc_model_data *model_data; 100 + struct regulator *regulator; 65 101 void __iomem *base; 66 102 spinlock_t clk_lock; 103 + struct clk_hw *fixed_div_clk; 67 104 struct clk_hw *clk_prescaler; 68 105 struct clk_hw *clk_scaler; 69 106 struct reset_control *rst; 107 + int vref_mv; 108 + u32 sample_period_ns; 109 + int cv; 110 + bool battery_sensing; 111 + struct adc_gain battery_mode_gain; 70 112 }; 71 113 72 114 #define ASPEED_CHAN(_idx, _data_reg_addr) { \ ··· 129 65 .address = (_data_reg_addr), \ 130 66 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 131 67 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 132 - BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 68 + BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 69 + BIT(IIO_CHAN_INFO_OFFSET), \ 133 70 } 134 71 135 72 static const struct iio_chan_spec aspeed_adc_iio_channels[] = { ··· 152 87 ASPEED_CHAN(15, 0x2E), 153 88 }; 154 89 90 + #define ASPEED_BAT_CHAN(_idx, _data_reg_addr) { \ 91 + .type = IIO_VOLTAGE, \ 92 + .indexed = 1, \ 93 + .channel = (_idx), \ 94 + .address = (_data_reg_addr), \ 95 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 96 + BIT(IIO_CHAN_INFO_OFFSET), \ 97 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 98 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 99 + } 100 + static const struct iio_chan_spec aspeed_adc_iio_bat_channels[] = { 101 + ASPEED_CHAN(0, 0x10), 102 + ASPEED_CHAN(1, 0x12), 103 + ASPEED_CHAN(2, 0x14), 104 + ASPEED_CHAN(3, 0x16), 105 + ASPEED_CHAN(4, 0x18), 106 + ASPEED_CHAN(5, 0x1A), 107 + ASPEED_CHAN(6, 0x1C), 108 + ASPEED_BAT_CHAN(7, 0x1E), 109 + }; 110 + 111 + static int aspeed_adc_set_trim_data(struct iio_dev *indio_dev) 112 + { 113 + struct device_node *syscon; 114 + struct regmap *scu; 115 + u32 scu_otp, trimming_val; 116 + struct aspeed_adc_data *data = iio_priv(indio_dev); 117 + 118 + syscon = of_find_node_by_name(NULL, "syscon"); 119 + if (syscon == NULL) { 120 + dev_warn(data->dev, "Couldn't find syscon node\n"); 121 + return -EOPNOTSUPP; 122 + } 123 + scu = syscon_node_to_regmap(syscon); 124 + if (IS_ERR(scu)) { 125 + dev_warn(data->dev, "Failed to get syscon regmap\n"); 126 + return -EOPNOTSUPP; 127 + } 128 + if (data->model_data->trim_locate) { 129 + if (regmap_read(scu, data->model_data->trim_locate->offset, 130 + &scu_otp)) { 131 + dev_warn(data->dev, 132 + "Failed to get adc trimming data\n"); 133 + trimming_val = 0x8; 134 + } else { 135 + trimming_val = 136 + ((scu_otp) & 137 + (data->model_data->trim_locate->field)) >> 138 + __ffs(data->model_data->trim_locate->field); 139 + } 140 + dev_dbg(data->dev, 141 + "trimming val = %d, offset = %08x, fields = %08x\n", 142 + trimming_val, data->model_data->trim_locate->offset, 143 + data->model_data->trim_locate->field); 144 + writel(trimming_val, data->base + ASPEED_REG_COMPENSATION_TRIM); 145 + } 146 + return 0; 147 + } 148 + 149 + static int aspeed_adc_compensation(struct iio_dev *indio_dev) 150 + { 151 + struct aspeed_adc_data *data = iio_priv(indio_dev); 152 + u32 index, adc_raw = 0; 153 + u32 adc_engine_control_reg_val; 154 + 155 + adc_engine_control_reg_val = 156 + readl(data->base + ASPEED_REG_ENGINE_CONTROL); 157 + adc_engine_control_reg_val &= ~ASPEED_ADC_OP_MODE; 158 + adc_engine_control_reg_val |= 159 + (FIELD_PREP(ASPEED_ADC_OP_MODE, ASPEED_ADC_OP_MODE_NORMAL) | 160 + ASPEED_ADC_ENGINE_ENABLE); 161 + /* 162 + * Enable compensating sensing: 163 + * After that, the input voltage of ADC will force to half of the reference 164 + * voltage. So the expected reading raw data will become half of the max 165 + * value. We can get compensating value = 0x200 - ADC read raw value. 166 + * It is recommended to average at least 10 samples to get a final CV. 167 + */ 168 + writel(adc_engine_control_reg_val | ASPEED_ADC_CTRL_COMPENSATION | 169 + ASPEED_ADC_CTRL_CHANNEL_ENABLE(0), 170 + data->base + ASPEED_REG_ENGINE_CONTROL); 171 + /* 172 + * After enable compensating sensing mode need to wait some time for ADC stable 173 + * Experiment result is 1ms. 174 + */ 175 + mdelay(1); 176 + 177 + for (index = 0; index < 16; index++) { 178 + /* 179 + * Waiting for the sampling period ensures that the value acquired 180 + * is fresh each time. 181 + */ 182 + ndelay(data->sample_period_ns); 183 + adc_raw += readw(data->base + aspeed_adc_iio_channels[0].address); 184 + } 185 + adc_raw >>= 4; 186 + data->cv = BIT(ASPEED_RESOLUTION_BITS - 1) - adc_raw; 187 + writel(adc_engine_control_reg_val, 188 + data->base + ASPEED_REG_ENGINE_CONTROL); 189 + dev_dbg(data->dev, "Compensating value = %d\n", data->cv); 190 + 191 + return 0; 192 + } 193 + 194 + static int aspeed_adc_set_sampling_rate(struct iio_dev *indio_dev, u32 rate) 195 + { 196 + struct aspeed_adc_data *data = iio_priv(indio_dev); 197 + 198 + if (rate < data->model_data->min_sampling_rate || 199 + rate > data->model_data->max_sampling_rate) 200 + return -EINVAL; 201 + /* Each sampling needs 12 clocks to convert.*/ 202 + clk_set_rate(data->clk_scaler->clk, rate * ASPEED_CLOCKS_PER_SAMPLE); 203 + rate = clk_get_rate(data->clk_scaler->clk); 204 + data->sample_period_ns = DIV_ROUND_UP_ULL( 205 + (u64)NSEC_PER_SEC * ASPEED_CLOCKS_PER_SAMPLE, rate); 206 + dev_dbg(data->dev, "Adc clock = %d sample period = %d ns", rate, 207 + data->sample_period_ns); 208 + 209 + return 0; 210 + } 211 + 155 212 static int aspeed_adc_read_raw(struct iio_dev *indio_dev, 156 213 struct iio_chan_spec const *chan, 157 214 int *val, int *val2, long mask) 158 215 { 159 216 struct aspeed_adc_data *data = iio_priv(indio_dev); 160 - const struct aspeed_adc_model_data *model_data = 161 - of_device_get_match_data(data->dev); 217 + u32 adc_engine_control_reg_val; 162 218 163 219 switch (mask) { 164 220 case IIO_CHAN_INFO_RAW: 165 - *val = readw(data->base + chan->address); 221 + if (data->battery_sensing && chan->channel == 7) { 222 + adc_engine_control_reg_val = 223 + readl(data->base + ASPEED_REG_ENGINE_CONTROL); 224 + writel(adc_engine_control_reg_val | 225 + FIELD_PREP(ASPEED_ADC_CH7_MODE, 226 + ASPEED_ADC_CH7_BAT) | 227 + ASPEED_ADC_BAT_SENSING_ENABLE, 228 + data->base + ASPEED_REG_ENGINE_CONTROL); 229 + /* 230 + * After enable battery sensing mode need to wait some time for adc stable 231 + * Experiment result is 1ms. 232 + */ 233 + mdelay(1); 234 + *val = readw(data->base + chan->address); 235 + *val = (*val * data->battery_mode_gain.mult) / 236 + data->battery_mode_gain.div; 237 + /* Restore control register value */ 238 + writel(adc_engine_control_reg_val, 239 + data->base + ASPEED_REG_ENGINE_CONTROL); 240 + } else 241 + *val = readw(data->base + chan->address); 242 + return IIO_VAL_INT; 243 + 244 + case IIO_CHAN_INFO_OFFSET: 245 + if (data->battery_sensing && chan->channel == 7) 246 + *val = (data->cv * data->battery_mode_gain.mult) / 247 + data->battery_mode_gain.div; 248 + else 249 + *val = data->cv; 166 250 return IIO_VAL_INT; 167 251 168 252 case IIO_CHAN_INFO_SCALE: 169 - *val = model_data->vref_voltage; 253 + *val = data->vref_mv; 170 254 *val2 = ASPEED_RESOLUTION_BITS; 171 255 return IIO_VAL_FRACTIONAL_LOG2; 172 256 ··· 333 119 struct iio_chan_spec const *chan, 334 120 int val, int val2, long mask) 335 121 { 336 - struct aspeed_adc_data *data = iio_priv(indio_dev); 337 - const struct aspeed_adc_model_data *model_data = 338 - of_device_get_match_data(data->dev); 339 - 340 122 switch (mask) { 341 123 case IIO_CHAN_INFO_SAMP_FREQ: 342 - if (val < model_data->min_sampling_rate || 343 - val > model_data->max_sampling_rate) 344 - return -EINVAL; 345 - 346 - clk_set_rate(data->clk_scaler->clk, 347 - val * ASPEED_CLOCKS_PER_SAMPLE); 348 - return 0; 124 + return aspeed_adc_set_sampling_rate(indio_dev, val); 349 125 350 126 case IIO_CHAN_INFO_SCALE: 351 127 case IIO_CHAN_INFO_RAW: ··· 372 168 .debugfs_reg_access = aspeed_adc_reg_access, 373 169 }; 374 170 171 + static void aspeed_adc_unregister_fixed_divider(void *data) 172 + { 173 + struct clk_hw *clk = data; 174 + 175 + clk_hw_unregister_fixed_factor(clk); 176 + } 177 + 178 + static void aspeed_adc_reset_assert(void *data) 179 + { 180 + struct reset_control *rst = data; 181 + 182 + reset_control_assert(rst); 183 + } 184 + 185 + static void aspeed_adc_clk_disable_unprepare(void *data) 186 + { 187 + struct clk *clk = data; 188 + 189 + clk_disable_unprepare(clk); 190 + } 191 + 192 + static void aspeed_adc_power_down(void *data) 193 + { 194 + struct aspeed_adc_data *priv_data = data; 195 + 196 + writel(FIELD_PREP(ASPEED_ADC_OP_MODE, ASPEED_ADC_OP_MODE_PWR_DOWN), 197 + priv_data->base + ASPEED_REG_ENGINE_CONTROL); 198 + } 199 + 200 + static void aspeed_adc_reg_disable(void *data) 201 + { 202 + struct regulator *reg = data; 203 + 204 + regulator_disable(reg); 205 + } 206 + 207 + static int aspeed_adc_vref_config(struct iio_dev *indio_dev) 208 + { 209 + struct aspeed_adc_data *data = iio_priv(indio_dev); 210 + int ret; 211 + u32 adc_engine_control_reg_val; 212 + 213 + if (data->model_data->vref_fixed_mv) { 214 + data->vref_mv = data->model_data->vref_fixed_mv; 215 + return 0; 216 + } 217 + adc_engine_control_reg_val = 218 + readl(data->base + ASPEED_REG_ENGINE_CONTROL); 219 + data->regulator = devm_regulator_get_optional(data->dev, "vref"); 220 + if (!IS_ERR(data->regulator)) { 221 + ret = regulator_enable(data->regulator); 222 + if (ret) 223 + return ret; 224 + ret = devm_add_action_or_reset( 225 + data->dev, aspeed_adc_reg_disable, data->regulator); 226 + if (ret) 227 + return ret; 228 + data->vref_mv = regulator_get_voltage(data->regulator); 229 + /* Conversion from uV to mV */ 230 + data->vref_mv /= 1000; 231 + if ((data->vref_mv >= 1550) && (data->vref_mv <= 2700)) 232 + writel(adc_engine_control_reg_val | 233 + FIELD_PREP( 234 + ASPEED_ADC_REF_VOLTAGE, 235 + ASPEED_ADC_REF_VOLTAGE_EXT_HIGH), 236 + data->base + ASPEED_REG_ENGINE_CONTROL); 237 + else if ((data->vref_mv >= 900) && (data->vref_mv <= 1650)) 238 + writel(adc_engine_control_reg_val | 239 + FIELD_PREP( 240 + ASPEED_ADC_REF_VOLTAGE, 241 + ASPEED_ADC_REF_VOLTAGE_EXT_LOW), 242 + data->base + ASPEED_REG_ENGINE_CONTROL); 243 + else { 244 + dev_err(data->dev, "Regulator voltage %d not support", 245 + data->vref_mv); 246 + return -EOPNOTSUPP; 247 + } 248 + } else { 249 + if (PTR_ERR(data->regulator) != -ENODEV) 250 + return PTR_ERR(data->regulator); 251 + data->vref_mv = 2500000; 252 + of_property_read_u32(data->dev->of_node, 253 + "aspeed,int-vref-microvolt", 254 + &data->vref_mv); 255 + /* Conversion from uV to mV */ 256 + data->vref_mv /= 1000; 257 + if (data->vref_mv == 2500) 258 + writel(adc_engine_control_reg_val | 259 + FIELD_PREP(ASPEED_ADC_REF_VOLTAGE, 260 + ASPEED_ADC_REF_VOLTAGE_2500mV), 261 + data->base + ASPEED_REG_ENGINE_CONTROL); 262 + else if (data->vref_mv == 1200) 263 + writel(adc_engine_control_reg_val | 264 + FIELD_PREP(ASPEED_ADC_REF_VOLTAGE, 265 + ASPEED_ADC_REF_VOLTAGE_1200mV), 266 + data->base + ASPEED_REG_ENGINE_CONTROL); 267 + else { 268 + dev_err(data->dev, "Voltage %d not support", data->vref_mv); 269 + return -EOPNOTSUPP; 270 + } 271 + } 272 + 273 + return 0; 274 + } 275 + 375 276 static int aspeed_adc_probe(struct platform_device *pdev) 376 277 { 377 278 struct iio_dev *indio_dev; 378 279 struct aspeed_adc_data *data; 379 - const struct aspeed_adc_model_data *model_data; 380 - const char *clk_parent_name; 381 280 int ret; 382 281 u32 adc_engine_control_reg_val; 282 + unsigned long scaler_flags = 0; 283 + char clk_name[32], clk_parent_name[32]; 383 284 384 285 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*data)); 385 286 if (!indio_dev) ··· 492 183 493 184 data = iio_priv(indio_dev); 494 185 data->dev = &pdev->dev; 186 + data->model_data = of_device_get_match_data(&pdev->dev); 495 187 platform_set_drvdata(pdev, indio_dev); 496 188 497 189 data->base = devm_platform_ioremap_resource(pdev, 0); ··· 501 191 502 192 /* Register ADC clock prescaler with source specified by device tree. */ 503 193 spin_lock_init(&data->clk_lock); 504 - clk_parent_name = of_clk_get_parent_name(pdev->dev.of_node, 0); 194 + snprintf(clk_parent_name, ARRAY_SIZE(clk_parent_name), "%s", 195 + of_clk_get_parent_name(pdev->dev.of_node, 0)); 196 + snprintf(clk_name, ARRAY_SIZE(clk_name), "%s-fixed-div", 197 + data->model_data->model_name); 198 + data->fixed_div_clk = clk_hw_register_fixed_factor( 199 + &pdev->dev, clk_name, clk_parent_name, 0, 1, 2); 200 + if (IS_ERR(data->fixed_div_clk)) 201 + return PTR_ERR(data->fixed_div_clk); 505 202 506 - data->clk_prescaler = clk_hw_register_divider( 507 - &pdev->dev, "prescaler", clk_parent_name, 0, 508 - data->base + ASPEED_REG_CLOCK_CONTROL, 509 - 17, 15, 0, &data->clk_lock); 510 - if (IS_ERR(data->clk_prescaler)) 511 - return PTR_ERR(data->clk_prescaler); 203 + ret = devm_add_action_or_reset(data->dev, 204 + aspeed_adc_unregister_fixed_divider, 205 + data->fixed_div_clk); 206 + if (ret) 207 + return ret; 208 + snprintf(clk_parent_name, ARRAY_SIZE(clk_parent_name), clk_name); 512 209 210 + if (data->model_data->need_prescaler) { 211 + snprintf(clk_name, ARRAY_SIZE(clk_name), "%s-prescaler", 212 + data->model_data->model_name); 213 + data->clk_prescaler = devm_clk_hw_register_divider( 214 + &pdev->dev, clk_name, clk_parent_name, 0, 215 + data->base + ASPEED_REG_CLOCK_CONTROL, 17, 15, 0, 216 + &data->clk_lock); 217 + if (IS_ERR(data->clk_prescaler)) 218 + return PTR_ERR(data->clk_prescaler); 219 + snprintf(clk_parent_name, ARRAY_SIZE(clk_parent_name), 220 + clk_name); 221 + scaler_flags = CLK_SET_RATE_PARENT; 222 + } 513 223 /* 514 224 * Register ADC clock scaler downstream from the prescaler. Allow rate 515 225 * setting to adjust the prescaler as well. 516 226 */ 517 - data->clk_scaler = clk_hw_register_divider( 518 - &pdev->dev, "scaler", "prescaler", 519 - CLK_SET_RATE_PARENT, 520 - data->base + ASPEED_REG_CLOCK_CONTROL, 521 - 0, 10, 0, &data->clk_lock); 522 - if (IS_ERR(data->clk_scaler)) { 523 - ret = PTR_ERR(data->clk_scaler); 524 - goto scaler_error; 525 - } 227 + snprintf(clk_name, ARRAY_SIZE(clk_name), "%s-scaler", 228 + data->model_data->model_name); 229 + data->clk_scaler = devm_clk_hw_register_divider( 230 + &pdev->dev, clk_name, clk_parent_name, scaler_flags, 231 + data->base + ASPEED_REG_CLOCK_CONTROL, 0, 232 + data->model_data->scaler_bit_width, 0, &data->clk_lock); 233 + if (IS_ERR(data->clk_scaler)) 234 + return PTR_ERR(data->clk_scaler); 526 235 527 - data->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL); 236 + data->rst = devm_reset_control_get_shared(&pdev->dev, NULL); 528 237 if (IS_ERR(data->rst)) { 529 238 dev_err(&pdev->dev, 530 239 "invalid or missing reset controller device tree entry"); 531 - ret = PTR_ERR(data->rst); 532 - goto reset_error; 240 + return PTR_ERR(data->rst); 533 241 } 534 242 reset_control_deassert(data->rst); 535 243 536 - model_data = of_device_get_match_data(&pdev->dev); 244 + ret = devm_add_action_or_reset(data->dev, aspeed_adc_reset_assert, 245 + data->rst); 246 + if (ret) 247 + return ret; 537 248 538 - if (model_data->wait_init_sequence) { 539 - /* Enable engine in normal mode. */ 540 - writel(ASPEED_OPERATION_MODE_NORMAL | ASPEED_ENGINE_ENABLE, 541 - data->base + ASPEED_REG_ENGINE_CONTROL); 249 + ret = aspeed_adc_vref_config(indio_dev); 250 + if (ret) 251 + return ret; 542 252 253 + if (of_find_property(data->dev->of_node, "aspeed,trim-data-valid", 254 + NULL)) { 255 + ret = aspeed_adc_set_trim_data(indio_dev); 256 + if (ret) 257 + return ret; 258 + } 259 + 260 + if (of_find_property(data->dev->of_node, "aspeed,battery-sensing", 261 + NULL)) { 262 + if (data->model_data->bat_sense_sup) { 263 + data->battery_sensing = 1; 264 + if (readl(data->base + ASPEED_REG_ENGINE_CONTROL) & 265 + ASPEED_ADC_BAT_SENSING_DIV) { 266 + data->battery_mode_gain.mult = 3; 267 + data->battery_mode_gain.div = 1; 268 + } else { 269 + data->battery_mode_gain.mult = 3; 270 + data->battery_mode_gain.div = 2; 271 + } 272 + } else 273 + dev_warn(&pdev->dev, 274 + "Failed to enable battery-sensing mode\n"); 275 + } 276 + 277 + ret = clk_prepare_enable(data->clk_scaler->clk); 278 + if (ret) 279 + return ret; 280 + ret = devm_add_action_or_reset(data->dev, 281 + aspeed_adc_clk_disable_unprepare, 282 + data->clk_scaler->clk); 283 + if (ret) 284 + return ret; 285 + ret = aspeed_adc_set_sampling_rate(indio_dev, 286 + ASPEED_ADC_DEF_SAMPLING_RATE); 287 + if (ret) 288 + return ret; 289 + 290 + adc_engine_control_reg_val = 291 + readl(data->base + ASPEED_REG_ENGINE_CONTROL); 292 + adc_engine_control_reg_val |= 293 + FIELD_PREP(ASPEED_ADC_OP_MODE, ASPEED_ADC_OP_MODE_NORMAL) | 294 + ASPEED_ADC_ENGINE_ENABLE; 295 + /* Enable engine in normal mode. */ 296 + writel(adc_engine_control_reg_val, 297 + data->base + ASPEED_REG_ENGINE_CONTROL); 298 + 299 + ret = devm_add_action_or_reset(data->dev, aspeed_adc_power_down, 300 + data); 301 + if (ret) 302 + return ret; 303 + 304 + if (data->model_data->wait_init_sequence) { 543 305 /* Wait for initial sequence complete. */ 544 306 ret = readl_poll_timeout(data->base + ASPEED_REG_ENGINE_CONTROL, 545 307 adc_engine_control_reg_val, ··· 620 238 ASPEED_ADC_INIT_POLLING_TIME, 621 239 ASPEED_ADC_INIT_TIMEOUT); 622 240 if (ret) 623 - goto poll_timeout_error; 241 + return ret; 624 242 } 625 243 244 + aspeed_adc_compensation(indio_dev); 626 245 /* Start all channels in normal mode. */ 627 - ret = clk_prepare_enable(data->clk_scaler->clk); 628 - if (ret) 629 - goto clk_enable_error; 630 - 631 - adc_engine_control_reg_val = GENMASK(31, 16) | 632 - ASPEED_OPERATION_MODE_NORMAL | ASPEED_ENGINE_ENABLE; 246 + adc_engine_control_reg_val = 247 + readl(data->base + ASPEED_REG_ENGINE_CONTROL); 248 + adc_engine_control_reg_val |= ASPEED_ADC_CTRL_CHANNEL; 633 249 writel(adc_engine_control_reg_val, 634 - data->base + ASPEED_REG_ENGINE_CONTROL); 250 + data->base + ASPEED_REG_ENGINE_CONTROL); 635 251 636 - model_data = of_device_get_match_data(&pdev->dev); 637 - indio_dev->name = model_data->model_name; 252 + indio_dev->name = data->model_data->model_name; 638 253 indio_dev->info = &aspeed_adc_iio_info; 639 254 indio_dev->modes = INDIO_DIRECT_MODE; 640 - indio_dev->channels = aspeed_adc_iio_channels; 641 - indio_dev->num_channels = ARRAY_SIZE(aspeed_adc_iio_channels); 255 + indio_dev->channels = data->battery_sensing ? 256 + aspeed_adc_iio_bat_channels : 257 + aspeed_adc_iio_channels; 258 + indio_dev->num_channels = data->model_data->num_channels; 642 259 643 - ret = iio_device_register(indio_dev); 644 - if (ret) 645 - goto iio_register_error; 646 - 647 - return 0; 648 - 649 - iio_register_error: 650 - writel(ASPEED_OPERATION_MODE_POWER_DOWN, 651 - data->base + ASPEED_REG_ENGINE_CONTROL); 652 - clk_disable_unprepare(data->clk_scaler->clk); 653 - clk_enable_error: 654 - poll_timeout_error: 655 - reset_control_assert(data->rst); 656 - reset_error: 657 - clk_hw_unregister_divider(data->clk_scaler); 658 - scaler_error: 659 - clk_hw_unregister_divider(data->clk_prescaler); 260 + ret = devm_iio_device_register(data->dev, indio_dev); 660 261 return ret; 661 262 } 662 263 663 - static int aspeed_adc_remove(struct platform_device *pdev) 664 - { 665 - struct iio_dev *indio_dev = platform_get_drvdata(pdev); 666 - struct aspeed_adc_data *data = iio_priv(indio_dev); 264 + static const struct aspeed_adc_trim_locate ast2500_adc_trim = { 265 + .offset = 0x154, 266 + .field = GENMASK(31, 28), 267 + }; 667 268 668 - iio_device_unregister(indio_dev); 669 - writel(ASPEED_OPERATION_MODE_POWER_DOWN, 670 - data->base + ASPEED_REG_ENGINE_CONTROL); 671 - clk_disable_unprepare(data->clk_scaler->clk); 672 - reset_control_assert(data->rst); 673 - clk_hw_unregister_divider(data->clk_scaler); 674 - clk_hw_unregister_divider(data->clk_prescaler); 269 + static const struct aspeed_adc_trim_locate ast2600_adc0_trim = { 270 + .offset = 0x5d0, 271 + .field = GENMASK(3, 0), 272 + }; 675 273 676 - return 0; 677 - } 274 + static const struct aspeed_adc_trim_locate ast2600_adc1_trim = { 275 + .offset = 0x5d0, 276 + .field = GENMASK(7, 4), 277 + }; 678 278 679 279 static const struct aspeed_adc_model_data ast2400_model_data = { 680 280 .model_name = "ast2400-adc", 681 - .vref_voltage = 2500, // mV 281 + .vref_fixed_mv = 2500, 682 282 .min_sampling_rate = 10000, 683 283 .max_sampling_rate = 500000, 284 + .need_prescaler = true, 285 + .scaler_bit_width = 10, 286 + .num_channels = 16, 684 287 }; 685 288 686 289 static const struct aspeed_adc_model_data ast2500_model_data = { 687 290 .model_name = "ast2500-adc", 688 - .vref_voltage = 1800, // mV 291 + .vref_fixed_mv = 1800, 689 292 .min_sampling_rate = 1, 690 293 .max_sampling_rate = 1000000, 691 294 .wait_init_sequence = true, 295 + .need_prescaler = true, 296 + .scaler_bit_width = 10, 297 + .num_channels = 16, 298 + .trim_locate = &ast2500_adc_trim, 299 + }; 300 + 301 + static const struct aspeed_adc_model_data ast2600_adc0_model_data = { 302 + .model_name = "ast2600-adc0", 303 + .min_sampling_rate = 10000, 304 + .max_sampling_rate = 500000, 305 + .wait_init_sequence = true, 306 + .bat_sense_sup = true, 307 + .scaler_bit_width = 16, 308 + .num_channels = 8, 309 + .trim_locate = &ast2600_adc0_trim, 310 + }; 311 + 312 + static const struct aspeed_adc_model_data ast2600_adc1_model_data = { 313 + .model_name = "ast2600-adc1", 314 + .min_sampling_rate = 10000, 315 + .max_sampling_rate = 500000, 316 + .wait_init_sequence = true, 317 + .bat_sense_sup = true, 318 + .scaler_bit_width = 16, 319 + .num_channels = 8, 320 + .trim_locate = &ast2600_adc1_trim, 692 321 }; 693 322 694 323 static const struct of_device_id aspeed_adc_matches[] = { 695 324 { .compatible = "aspeed,ast2400-adc", .data = &ast2400_model_data }, 696 325 { .compatible = "aspeed,ast2500-adc", .data = &ast2500_model_data }, 326 + { .compatible = "aspeed,ast2600-adc0", .data = &ast2600_adc0_model_data }, 327 + { .compatible = "aspeed,ast2600-adc1", .data = &ast2600_adc1_model_data }, 697 328 {}, 698 329 }; 699 330 MODULE_DEVICE_TABLE(of, aspeed_adc_matches); 700 331 701 332 static struct platform_driver aspeed_adc_driver = { 702 333 .probe = aspeed_adc_probe, 703 - .remove = aspeed_adc_remove, 704 334 .driver = { 705 335 .name = KBUILD_MODNAME, 706 336 .of_match_table = aspeed_adc_matches, ··· 722 328 module_platform_driver(aspeed_adc_driver); 723 329 724 330 MODULE_AUTHOR("Rick Altherr <raltherr@google.com>"); 725 - MODULE_DESCRIPTION("Aspeed AST2400/2500 ADC Driver"); 331 + MODULE_DESCRIPTION("Aspeed AST2400/2500/2600 ADC Driver"); 726 332 MODULE_LICENSE("GPL");
+410 -190
drivers/iio/adc/at91-sama5d2_adc.c
··· 4 4 * 5 5 * Copyright (C) 2015 Atmel, 6 6 * 2015 Ludovic Desroches <ludovic.desroches@atmel.com> 7 + * 2021 Microchip Technology, Inc. and its subsidiaries 8 + * 2021 Eugen Hristev <eugen.hristev@microchip.com> 7 9 */ 8 10 9 11 #include <linux/bitops.h> ··· 29 27 #include <linux/pinctrl/consumer.h> 30 28 #include <linux/regulator/consumer.h> 31 29 30 + struct at91_adc_reg_layout { 32 31 /* Control Register */ 33 - #define AT91_SAMA5D2_CR 0x00 32 + u16 CR; 34 33 /* Software Reset */ 35 34 #define AT91_SAMA5D2_CR_SWRST BIT(0) 36 35 /* Start Conversion */ ··· 42 39 #define AT91_SAMA5D2_CR_CMPRST BIT(4) 43 40 44 41 /* Mode Register */ 45 - #define AT91_SAMA5D2_MR 0x04 42 + u16 MR; 46 43 /* Trigger Selection */ 47 44 #define AT91_SAMA5D2_MR_TRGSEL(v) ((v) << 1) 48 45 /* ADTRG */ ··· 85 82 #define AT91_SAMA5D2_MR_USEQ BIT(31) 86 83 87 84 /* Channel Sequence Register 1 */ 88 - #define AT91_SAMA5D2_SEQR1 0x08 85 + u16 SEQR1; 89 86 /* Channel Sequence Register 2 */ 90 - #define AT91_SAMA5D2_SEQR2 0x0c 87 + u16 SEQR2; 91 88 /* Channel Enable Register */ 92 - #define AT91_SAMA5D2_CHER 0x10 89 + u16 CHER; 93 90 /* Channel Disable Register */ 94 - #define AT91_SAMA5D2_CHDR 0x14 91 + u16 CHDR; 95 92 /* Channel Status Register */ 96 - #define AT91_SAMA5D2_CHSR 0x18 93 + u16 CHSR; 97 94 /* Last Converted Data Register */ 98 - #define AT91_SAMA5D2_LCDR 0x20 95 + u16 LCDR; 99 96 /* Interrupt Enable Register */ 100 - #define AT91_SAMA5D2_IER 0x24 97 + u16 IER; 101 98 /* Interrupt Enable Register - TS X measurement ready */ 102 99 #define AT91_SAMA5D2_IER_XRDY BIT(20) 103 100 /* Interrupt Enable Register - TS Y measurement ready */ ··· 112 109 #define AT91_SAMA5D2_IER_PEN BIT(29) 113 110 /* Interrupt Enable Register - No pen detect */ 114 111 #define AT91_SAMA5D2_IER_NOPEN BIT(30) 112 + 115 113 /* Interrupt Disable Register */ 116 - #define AT91_SAMA5D2_IDR 0x28 114 + u16 IDR; 117 115 /* Interrupt Mask Register */ 118 - #define AT91_SAMA5D2_IMR 0x2c 116 + u16 IMR; 119 117 /* Interrupt Status Register */ 120 - #define AT91_SAMA5D2_ISR 0x30 118 + u16 ISR; 119 + /* End of Conversion Interrupt Enable Register */ 120 + u16 EOC_IER; 121 + /* End of Conversion Interrupt Disable Register */ 122 + u16 EOC_IDR; 123 + /* End of Conversion Interrupt Mask Register */ 124 + u16 EOC_IMR; 125 + /* End of Conversion Interrupt Status Register */ 126 + u16 EOC_ISR; 121 127 /* Interrupt Status Register - Pen touching sense status */ 122 128 #define AT91_SAMA5D2_ISR_PENS BIT(31) 123 129 /* Last Channel Trigger Mode Register */ 124 - #define AT91_SAMA5D2_LCTMR 0x34 130 + u16 LCTMR; 125 131 /* Last Channel Compare Window Register */ 126 - #define AT91_SAMA5D2_LCCWR 0x38 132 + u16 LCCWR; 127 133 /* Overrun Status Register */ 128 - #define AT91_SAMA5D2_OVER 0x3c 134 + u16 OVER; 129 135 /* Extended Mode Register */ 130 - #define AT91_SAMA5D2_EMR 0x40 136 + u16 EMR; 131 137 /* Extended Mode Register - Oversampling rate */ 132 138 #define AT91_SAMA5D2_EMR_OSR(V) ((V) << 16) 133 139 #define AT91_SAMA5D2_EMR_OSR_MASK GENMASK(17, 16) ··· 146 134 147 135 /* Extended Mode Register - Averaging on single trigger event */ 148 136 #define AT91_SAMA5D2_EMR_ASTE(V) ((V) << 20) 137 + 149 138 /* Compare Window Register */ 150 - #define AT91_SAMA5D2_CWR 0x44 139 + u16 CWR; 151 140 /* Channel Gain Register */ 152 - #define AT91_SAMA5D2_CGR 0x48 153 - 141 + u16 CGR; 154 142 /* Channel Offset Register */ 155 - #define AT91_SAMA5D2_COR 0x4c 156 - #define AT91_SAMA5D2_COR_DIFF_OFFSET 16 157 - 158 - /* Channel Data Register 0 */ 159 - #define AT91_SAMA5D2_CDR0 0x50 143 + u16 COR; 144 + /* Channel Offset Register differential offset - constant, not a register */ 145 + u16 COR_diff_offset; 160 146 /* Analog Control Register */ 161 - #define AT91_SAMA5D2_ACR 0x94 147 + u16 ACR; 162 148 /* Analog Control Register - Pen detect sensitivity mask */ 163 149 #define AT91_SAMA5D2_ACR_PENDETSENS_MASK GENMASK(1, 0) 164 150 165 151 /* Touchscreen Mode Register */ 166 - #define AT91_SAMA5D2_TSMR 0xb0 152 + u16 TSMR; 167 153 /* Touchscreen Mode Register - No touch mode */ 168 154 #define AT91_SAMA5D2_TSMR_TSMODE_NONE 0 169 155 /* Touchscreen Mode Register - 4 wire screen, no pressure measurement */ ··· 190 180 #define AT91_SAMA5D2_TSMR_PENDET_ENA BIT(24) 191 181 192 182 /* Touchscreen X Position Register */ 193 - #define AT91_SAMA5D2_XPOSR 0xb4 183 + u16 XPOSR; 194 184 /* Touchscreen Y Position Register */ 195 - #define AT91_SAMA5D2_YPOSR 0xb8 185 + u16 YPOSR; 196 186 /* Touchscreen Pressure Register */ 197 - #define AT91_SAMA5D2_PRESSR 0xbc 187 + u16 PRESSR; 198 188 /* Trigger Register */ 199 - #define AT91_SAMA5D2_TRGR 0xc0 189 + u16 TRGR; 200 190 /* Mask for TRGMOD field of TRGR register */ 201 191 #define AT91_SAMA5D2_TRGR_TRGMOD_MASK GENMASK(2, 0) 202 192 /* No trigger, only software trigger can start conversions */ ··· 215 205 #define AT91_SAMA5D2_TRGR_TRGPER(x) ((x) << 16) 216 206 217 207 /* Correction Select Register */ 218 - #define AT91_SAMA5D2_COSR 0xd0 208 + u16 COSR; 219 209 /* Correction Value Register */ 220 - #define AT91_SAMA5D2_CVR 0xd4 210 + u16 CVR; 221 211 /* Channel Error Correction Register */ 222 - #define AT91_SAMA5D2_CECR 0xd8 212 + u16 CECR; 223 213 /* Write Protection Mode Register */ 224 - #define AT91_SAMA5D2_WPMR 0xe4 214 + u16 WPMR; 225 215 /* Write Protection Status Register */ 226 - #define AT91_SAMA5D2_WPSR 0xe8 216 + u16 WPSR; 227 217 /* Version Register */ 228 - #define AT91_SAMA5D2_VERSION 0xfc 218 + u16 VERSION; 219 + }; 229 220 230 - #define AT91_SAMA5D2_HW_TRIG_CNT 3 231 - #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12 232 - #define AT91_SAMA5D2_DIFF_CHAN_CNT 6 221 + static const struct at91_adc_reg_layout sama5d2_layout = { 222 + .CR = 0x00, 223 + .MR = 0x04, 224 + .SEQR1 = 0x08, 225 + .SEQR2 = 0x0c, 226 + .CHER = 0x10, 227 + .CHDR = 0x14, 228 + .CHSR = 0x18, 229 + .LCDR = 0x20, 230 + .IER = 0x24, 231 + .IDR = 0x28, 232 + .IMR = 0x2c, 233 + .ISR = 0x30, 234 + .LCTMR = 0x34, 235 + .LCCWR = 0x38, 236 + .OVER = 0x3c, 237 + .EMR = 0x40, 238 + .CWR = 0x44, 239 + .CGR = 0x48, 240 + .COR = 0x4c, 241 + .COR_diff_offset = 16, 242 + .ACR = 0x94, 243 + .TSMR = 0xb0, 244 + .XPOSR = 0xb4, 245 + .YPOSR = 0xb8, 246 + .PRESSR = 0xbc, 247 + .TRGR = 0xc0, 248 + .COSR = 0xd0, 249 + .CVR = 0xd4, 250 + .CECR = 0xd8, 251 + .WPMR = 0xe4, 252 + .WPSR = 0xe8, 253 + .VERSION = 0xfc, 254 + }; 233 255 234 - #define AT91_SAMA5D2_TIMESTAMP_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \ 235 - AT91_SAMA5D2_DIFF_CHAN_CNT + 1) 236 - 237 - #define AT91_SAMA5D2_TOUCH_X_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \ 238 - AT91_SAMA5D2_DIFF_CHAN_CNT * 2) 239 - #define AT91_SAMA5D2_TOUCH_Y_CHAN_IDX (AT91_SAMA5D2_TOUCH_X_CHAN_IDX + 1) 240 - #define AT91_SAMA5D2_TOUCH_P_CHAN_IDX (AT91_SAMA5D2_TOUCH_Y_CHAN_IDX + 1) 241 - #define AT91_SAMA5D2_MAX_CHAN_IDX AT91_SAMA5D2_TOUCH_P_CHAN_IDX 256 + static const struct at91_adc_reg_layout sama7g5_layout = { 257 + .CR = 0x00, 258 + .MR = 0x04, 259 + .SEQR1 = 0x08, 260 + .SEQR2 = 0x0c, 261 + .CHER = 0x10, 262 + .CHDR = 0x14, 263 + .CHSR = 0x18, 264 + .LCDR = 0x20, 265 + .IER = 0x24, 266 + .IDR = 0x28, 267 + .IMR = 0x2c, 268 + .ISR = 0x30, 269 + .EOC_IER = 0x34, 270 + .EOC_IDR = 0x38, 271 + .EOC_IMR = 0x3c, 272 + .EOC_ISR = 0x40, 273 + .OVER = 0x4c, 274 + .EMR = 0x50, 275 + .CWR = 0x54, 276 + .COR = 0x5c, 277 + .COR_diff_offset = 0, 278 + .ACR = 0xe0, 279 + .TRGR = 0x100, 280 + .COSR = 0x104, 281 + .CVR = 0x108, 282 + .CECR = 0x10c, 283 + .WPMR = 0x118, 284 + .WPSR = 0x11c, 285 + .VERSION = 0x130, 286 + }; 242 287 243 288 #define AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */ 244 289 #define AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US 200 ··· 301 236 #define AT91_SAMA5D2_XYZ_MASK GENMASK(11, 0) 302 237 303 238 #define AT91_SAMA5D2_MAX_POS_BITS 12 304 - 305 - /* 306 - * Maximum number of bytes to hold conversion from all channels 307 - * without the timestamp. 308 - */ 309 - #define AT91_BUFFER_MAX_CONVERSION_BYTES ((AT91_SAMA5D2_SINGLE_CHAN_CNT + \ 310 - AT91_SAMA5D2_DIFF_CHAN_CNT) * 2) 311 - 312 - /* This total must also include the timestamp */ 313 - #define AT91_BUFFER_MAX_BYTES (AT91_BUFFER_MAX_CONVERSION_BYTES + 8) 314 - 315 - #define AT91_BUFFER_MAX_HWORDS (AT91_BUFFER_MAX_BYTES / 2) 316 239 317 240 #define AT91_HWFIFO_MAX_SIZE_STR "128" 318 241 #define AT91_HWFIFO_MAX_SIZE 128 ··· 310 257 #define AT91_OSR_4SAMPLES 4 311 258 #define AT91_OSR_16SAMPLES 16 312 259 313 - #define AT91_SAMA5D2_CHAN_SINGLE(num, addr) \ 260 + #define AT91_SAMA5D2_CHAN_SINGLE(index, num, addr) \ 314 261 { \ 315 262 .type = IIO_VOLTAGE, \ 316 263 .channel = num, \ 317 264 .address = addr, \ 318 - .scan_index = num, \ 265 + .scan_index = index, \ 319 266 .scan_type = { \ 320 267 .sign = 'u', \ 321 268 .realbits = 14, \ ··· 329 276 .indexed = 1, \ 330 277 } 331 278 332 - #define AT91_SAMA5D2_CHAN_DIFF(num, num2, addr) \ 279 + #define AT91_SAMA5D2_CHAN_DIFF(index, num, num2, addr) \ 333 280 { \ 334 281 .type = IIO_VOLTAGE, \ 335 282 .differential = 1, \ 336 283 .channel = num, \ 337 284 .channel2 = num2, \ 338 285 .address = addr, \ 339 - .scan_index = num + AT91_SAMA5D2_SINGLE_CHAN_CNT, \ 286 + .scan_index = index, \ 340 287 .scan_type = { \ 341 288 .sign = 's', \ 342 289 .realbits = 14, \ ··· 383 330 .datasheet_name = name, \ 384 331 } 385 332 386 - #define at91_adc_readl(st, reg) readl_relaxed(st->base + reg) 387 - #define at91_adc_writel(st, reg, val) writel_relaxed(val, st->base + reg) 333 + #define at91_adc_readl(st, reg) \ 334 + readl_relaxed((st)->base + (st)->soc_info.platform->layout->reg) 335 + #define at91_adc_read_chan(st, reg) \ 336 + readl_relaxed((st)->base + reg) 337 + #define at91_adc_writel(st, reg, val) \ 338 + writel_relaxed(val, (st)->base + (st)->soc_info.platform->layout->reg) 388 339 340 + /** 341 + * struct at91_adc_platform - at91-sama5d2 platform information struct 342 + * @layout: pointer to the reg layout struct 343 + * @adc_channels: pointer to an array of channels for registering in 344 + * the iio subsystem 345 + * @nr_channels: number of physical channels available 346 + * @touch_chan_x: index of the touchscreen X channel 347 + * @touch_chan_y: index of the touchscreen Y channel 348 + * @touch_chan_p: index of the touchscreen P channel 349 + * @max_channels: number of total channels 350 + * @max_index: highest channel index (highest index may be higher 351 + * than the total channel number) 352 + * @hw_trig_cnt: number of possible hardware triggers 353 + */ 354 + struct at91_adc_platform { 355 + const struct at91_adc_reg_layout *layout; 356 + const struct iio_chan_spec (*adc_channels)[]; 357 + unsigned int nr_channels; 358 + unsigned int touch_chan_x; 359 + unsigned int touch_chan_y; 360 + unsigned int touch_chan_p; 361 + unsigned int max_channels; 362 + unsigned int max_index; 363 + unsigned int hw_trig_cnt; 364 + }; 365 + 366 + /** 367 + * struct at91_adc_soc_info - at91-sama5d2 soc information struct 368 + * @startup_time: device startup time 369 + * @min_sample_rate: minimum sample rate in Hz 370 + * @max_sample_rate: maximum sample rate in Hz 371 + * @platform: pointer to the platform structure 372 + */ 389 373 struct at91_adc_soc_info { 390 374 unsigned startup_time; 391 375 unsigned min_sample_rate; 392 376 unsigned max_sample_rate; 377 + const struct at91_adc_platform *platform; 393 378 }; 394 379 395 380 struct at91_adc_trigger { ··· 474 383 unsigned long channels_bitmask; 475 384 struct work_struct workq; 476 385 }; 386 + 387 + /* 388 + * Buffer size requirements: 389 + * No channels * bytes_per_channel(2) + timestamp bytes (8) 390 + * Divided by 2 because we need half words. 391 + * We assume 32 channels for now, has to be increased if needed. 392 + * Nobody minds a buffer being too big. 393 + */ 394 + #define AT91_BUFFER_MAX_HWORDS ((32 * 2 + 8) / 2) 477 395 478 396 struct at91_adc_state { 479 397 void __iomem *base; ··· 539 439 }, 540 440 }; 541 441 542 - static const struct iio_chan_spec at91_adc_channels[] = { 543 - AT91_SAMA5D2_CHAN_SINGLE(0, 0x50), 544 - AT91_SAMA5D2_CHAN_SINGLE(1, 0x54), 545 - AT91_SAMA5D2_CHAN_SINGLE(2, 0x58), 546 - AT91_SAMA5D2_CHAN_SINGLE(3, 0x5c), 547 - AT91_SAMA5D2_CHAN_SINGLE(4, 0x60), 548 - AT91_SAMA5D2_CHAN_SINGLE(5, 0x64), 549 - AT91_SAMA5D2_CHAN_SINGLE(6, 0x68), 550 - AT91_SAMA5D2_CHAN_SINGLE(7, 0x6c), 551 - AT91_SAMA5D2_CHAN_SINGLE(8, 0x70), 552 - AT91_SAMA5D2_CHAN_SINGLE(9, 0x74), 553 - AT91_SAMA5D2_CHAN_SINGLE(10, 0x78), 554 - AT91_SAMA5D2_CHAN_SINGLE(11, 0x7c), 555 - AT91_SAMA5D2_CHAN_DIFF(0, 1, 0x50), 556 - AT91_SAMA5D2_CHAN_DIFF(2, 3, 0x58), 557 - AT91_SAMA5D2_CHAN_DIFF(4, 5, 0x60), 558 - AT91_SAMA5D2_CHAN_DIFF(6, 7, 0x68), 559 - AT91_SAMA5D2_CHAN_DIFF(8, 9, 0x70), 560 - AT91_SAMA5D2_CHAN_DIFF(10, 11, 0x78), 561 - IIO_CHAN_SOFT_TIMESTAMP(AT91_SAMA5D2_TIMESTAMP_CHAN_IDX), 562 - AT91_SAMA5D2_CHAN_TOUCH(AT91_SAMA5D2_TOUCH_X_CHAN_IDX, "x", IIO_MOD_X), 563 - AT91_SAMA5D2_CHAN_TOUCH(AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, "y", IIO_MOD_Y), 564 - AT91_SAMA5D2_CHAN_PRESSURE(AT91_SAMA5D2_TOUCH_P_CHAN_IDX, "pressure"), 442 + static const struct iio_chan_spec at91_sama5d2_adc_channels[] = { 443 + AT91_SAMA5D2_CHAN_SINGLE(0, 0, 0x50), 444 + AT91_SAMA5D2_CHAN_SINGLE(1, 1, 0x54), 445 + AT91_SAMA5D2_CHAN_SINGLE(2, 2, 0x58), 446 + AT91_SAMA5D2_CHAN_SINGLE(3, 3, 0x5c), 447 + AT91_SAMA5D2_CHAN_SINGLE(4, 4, 0x60), 448 + AT91_SAMA5D2_CHAN_SINGLE(5, 5, 0x64), 449 + AT91_SAMA5D2_CHAN_SINGLE(6, 6, 0x68), 450 + AT91_SAMA5D2_CHAN_SINGLE(7, 7, 0x6c), 451 + AT91_SAMA5D2_CHAN_SINGLE(8, 8, 0x70), 452 + AT91_SAMA5D2_CHAN_SINGLE(9, 9, 0x74), 453 + AT91_SAMA5D2_CHAN_SINGLE(10, 10, 0x78), 454 + AT91_SAMA5D2_CHAN_SINGLE(11, 11, 0x7c), 455 + /* original ABI has the differential channels with a gap in between */ 456 + AT91_SAMA5D2_CHAN_DIFF(12, 0, 1, 0x50), 457 + AT91_SAMA5D2_CHAN_DIFF(14, 2, 3, 0x58), 458 + AT91_SAMA5D2_CHAN_DIFF(16, 4, 5, 0x60), 459 + AT91_SAMA5D2_CHAN_DIFF(18, 6, 7, 0x68), 460 + AT91_SAMA5D2_CHAN_DIFF(20, 8, 9, 0x70), 461 + AT91_SAMA5D2_CHAN_DIFF(22, 10, 11, 0x78), 462 + IIO_CHAN_SOFT_TIMESTAMP(23), 463 + AT91_SAMA5D2_CHAN_TOUCH(24, "x", IIO_MOD_X), 464 + AT91_SAMA5D2_CHAN_TOUCH(25, "y", IIO_MOD_Y), 465 + AT91_SAMA5D2_CHAN_PRESSURE(26, "pressure"), 466 + }; 467 + 468 + static const struct iio_chan_spec at91_sama7g5_adc_channels[] = { 469 + AT91_SAMA5D2_CHAN_SINGLE(0, 0, 0x60), 470 + AT91_SAMA5D2_CHAN_SINGLE(1, 1, 0x64), 471 + AT91_SAMA5D2_CHAN_SINGLE(2, 2, 0x68), 472 + AT91_SAMA5D2_CHAN_SINGLE(3, 3, 0x6c), 473 + AT91_SAMA5D2_CHAN_SINGLE(4, 4, 0x70), 474 + AT91_SAMA5D2_CHAN_SINGLE(5, 5, 0x74), 475 + AT91_SAMA5D2_CHAN_SINGLE(6, 6, 0x78), 476 + AT91_SAMA5D2_CHAN_SINGLE(7, 7, 0x7c), 477 + AT91_SAMA5D2_CHAN_SINGLE(8, 8, 0x80), 478 + AT91_SAMA5D2_CHAN_SINGLE(9, 9, 0x84), 479 + AT91_SAMA5D2_CHAN_SINGLE(10, 10, 0x88), 480 + AT91_SAMA5D2_CHAN_SINGLE(11, 11, 0x8c), 481 + AT91_SAMA5D2_CHAN_SINGLE(12, 12, 0x90), 482 + AT91_SAMA5D2_CHAN_SINGLE(13, 13, 0x94), 483 + AT91_SAMA5D2_CHAN_SINGLE(14, 14, 0x98), 484 + AT91_SAMA5D2_CHAN_SINGLE(15, 15, 0x9c), 485 + AT91_SAMA5D2_CHAN_DIFF(16, 0, 1, 0x60), 486 + AT91_SAMA5D2_CHAN_DIFF(17, 2, 3, 0x68), 487 + AT91_SAMA5D2_CHAN_DIFF(18, 4, 5, 0x70), 488 + AT91_SAMA5D2_CHAN_DIFF(19, 6, 7, 0x78), 489 + AT91_SAMA5D2_CHAN_DIFF(20, 8, 9, 0x80), 490 + AT91_SAMA5D2_CHAN_DIFF(21, 10, 11, 0x88), 491 + AT91_SAMA5D2_CHAN_DIFF(22, 12, 13, 0x90), 492 + AT91_SAMA5D2_CHAN_DIFF(23, 14, 15, 0x98), 493 + IIO_CHAN_SOFT_TIMESTAMP(24), 494 + }; 495 + 496 + static const struct at91_adc_platform sama5d2_platform = { 497 + .layout = &sama5d2_layout, 498 + .adc_channels = &at91_sama5d2_adc_channels, 499 + #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12 500 + #define AT91_SAMA5D2_DIFF_CHAN_CNT 6 501 + .nr_channels = AT91_SAMA5D2_SINGLE_CHAN_CNT + 502 + AT91_SAMA5D2_DIFF_CHAN_CNT, 503 + #define AT91_SAMA5D2_TOUCH_X_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \ 504 + AT91_SAMA5D2_DIFF_CHAN_CNT * 2) 505 + .touch_chan_x = AT91_SAMA5D2_TOUCH_X_CHAN_IDX, 506 + #define AT91_SAMA5D2_TOUCH_Y_CHAN_IDX (AT91_SAMA5D2_TOUCH_X_CHAN_IDX + 1) 507 + .touch_chan_y = AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, 508 + #define AT91_SAMA5D2_TOUCH_P_CHAN_IDX (AT91_SAMA5D2_TOUCH_Y_CHAN_IDX + 1) 509 + .touch_chan_p = AT91_SAMA5D2_TOUCH_P_CHAN_IDX, 510 + #define AT91_SAMA5D2_MAX_CHAN_IDX AT91_SAMA5D2_TOUCH_P_CHAN_IDX 511 + .max_channels = ARRAY_SIZE(at91_sama5d2_adc_channels), 512 + .max_index = AT91_SAMA5D2_MAX_CHAN_IDX, 513 + #define AT91_SAMA5D2_HW_TRIG_CNT 3 514 + .hw_trig_cnt = AT91_SAMA5D2_HW_TRIG_CNT, 515 + }; 516 + 517 + static const struct at91_adc_platform sama7g5_platform = { 518 + .layout = &sama7g5_layout, 519 + .adc_channels = &at91_sama7g5_adc_channels, 520 + #define AT91_SAMA7G5_SINGLE_CHAN_CNT 16 521 + #define AT91_SAMA7G5_DIFF_CHAN_CNT 8 522 + .nr_channels = AT91_SAMA7G5_SINGLE_CHAN_CNT + 523 + AT91_SAMA7G5_DIFF_CHAN_CNT, 524 + #define AT91_SAMA7G5_MAX_CHAN_IDX (AT91_SAMA7G5_SINGLE_CHAN_CNT + \ 525 + AT91_SAMA7G5_DIFF_CHAN_CNT) 526 + .max_channels = ARRAY_SIZE(at91_sama7g5_adc_channels), 527 + .max_index = AT91_SAMA7G5_MAX_CHAN_IDX, 528 + #define AT91_SAMA7G5_HW_TRIG_CNT 3 529 + .hw_trig_cnt = AT91_SAMA7G5_HW_TRIG_CNT, 565 530 }; 566 531 567 532 static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan) ··· 660 495 { 661 496 u32 mask = 0; 662 497 u8 bit; 498 + struct at91_adc_state *st = iio_priv(indio_dev); 663 499 664 500 for_each_set_bit(bit, indio_dev->active_scan_mask, 665 501 indio_dev->num_channels) { ··· 669 503 mask |= BIT(chan->channel); 670 504 } 671 505 672 - return mask & GENMASK(11, 0); 506 + return mask & GENMASK(st->soc_info.platform->nr_channels, 0); 507 + } 508 + 509 + static void at91_adc_cor(struct at91_adc_state *st, 510 + struct iio_chan_spec const *chan) 511 + { 512 + u32 cor, cur_cor; 513 + 514 + cor = BIT(chan->channel) | BIT(chan->channel2); 515 + 516 + cur_cor = at91_adc_readl(st, COR); 517 + cor <<= st->soc_info.platform->layout->COR_diff_offset; 518 + if (chan->differential) 519 + at91_adc_writel(st, COR, cur_cor | cor); 520 + else 521 + at91_adc_writel(st, COR, cur_cor & ~cor); 522 + } 523 + 524 + static void at91_adc_irq_status(struct at91_adc_state *st, u32 *status, 525 + u32 *eoc) 526 + { 527 + *status = at91_adc_readl(st, ISR); 528 + if (st->soc_info.platform->layout->EOC_ISR) 529 + *eoc = at91_adc_readl(st, EOC_ISR); 530 + else 531 + *eoc = *status; 532 + } 533 + 534 + static void at91_adc_irq_mask(struct at91_adc_state *st, u32 *status, u32 *eoc) 535 + { 536 + *status = at91_adc_readl(st, IMR); 537 + if (st->soc_info.platform->layout->EOC_IMR) 538 + *eoc = at91_adc_readl(st, EOC_IMR); 539 + else 540 + *eoc = *status; 541 + } 542 + 543 + static void at91_adc_eoc_dis(struct at91_adc_state *st, unsigned int channel) 544 + { 545 + /* 546 + * On some products having the EOC bits in a separate register, 547 + * errata recommends not writing this register (EOC_IDR). 548 + * On products having the EOC bits in the IDR register, it's fine to write it. 549 + */ 550 + if (!st->soc_info.platform->layout->EOC_IDR) 551 + at91_adc_writel(st, IDR, BIT(channel)); 552 + } 553 + 554 + static void at91_adc_eoc_ena(struct at91_adc_state *st, unsigned int channel) 555 + { 556 + if (!st->soc_info.platform->layout->EOC_IDR) 557 + at91_adc_writel(st, IER, BIT(channel)); 558 + else 559 + at91_adc_writel(st, EOC_IER, BIT(channel)); 673 560 } 674 561 675 562 static void at91_adc_config_emr(struct at91_adc_state *st) 676 563 { 677 564 /* configure the extended mode register */ 678 - unsigned int emr = at91_adc_readl(st, AT91_SAMA5D2_EMR); 565 + unsigned int emr = at91_adc_readl(st, EMR); 679 566 680 567 /* select oversampling per single trigger event */ 681 568 emr |= AT91_SAMA5D2_EMR_ASTE(1); ··· 752 533 break; 753 534 } 754 535 755 - at91_adc_writel(st, AT91_SAMA5D2_EMR, emr); 536 + at91_adc_writel(st, EMR, emr); 756 537 } 757 538 758 539 static int at91_adc_adjust_val_osr(struct at91_adc_state *st, int *val) ··· 805 586 806 587 if (!state) { 807 588 /* disabling touch IRQs and setting mode to no touch enabled */ 808 - at91_adc_writel(st, AT91_SAMA5D2_IDR, 589 + at91_adc_writel(st, IDR, 809 590 AT91_SAMA5D2_IER_PEN | AT91_SAMA5D2_IER_NOPEN); 810 - at91_adc_writel(st, AT91_SAMA5D2_TSMR, 0); 591 + at91_adc_writel(st, TSMR, 0); 811 592 return 0; 812 593 } 813 594 /* ··· 833 614 tsmr |= AT91_SAMA5D2_TSMR_PENDET_ENA; 834 615 tsmr |= AT91_SAMA5D2_TSMR_TSFREQ(2) & AT91_SAMA5D2_TSMR_TSFREQ_MASK; 835 616 836 - at91_adc_writel(st, AT91_SAMA5D2_TSMR, tsmr); 617 + at91_adc_writel(st, TSMR, tsmr); 837 618 838 - acr = at91_adc_readl(st, AT91_SAMA5D2_ACR); 619 + acr = at91_adc_readl(st, ACR); 839 620 acr &= ~AT91_SAMA5D2_ACR_PENDETSENS_MASK; 840 621 acr |= 0x02 & AT91_SAMA5D2_ACR_PENDETSENS_MASK; 841 - at91_adc_writel(st, AT91_SAMA5D2_ACR, acr); 622 + at91_adc_writel(st, ACR, acr); 842 623 843 624 /* Sample Period Time = (TRGPER + 1) / ADCClock */ 844 625 st->touch_st.sample_period_val = 845 626 round_up((AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US * 846 627 clk_khz / 1000) - 1, 1); 847 628 /* enable pen detect IRQ */ 848 - at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_PEN); 629 + at91_adc_writel(st, IER, AT91_SAMA5D2_IER_PEN); 849 630 850 631 return 0; 851 632 } 852 633 853 634 static u16 at91_adc_touch_pos(struct at91_adc_state *st, int reg) 854 635 { 855 - u32 val; 636 + u32 val = 0; 856 637 u32 scale, result, pos; 857 638 858 639 /* ··· 861 642 * max = 2^AT91_SAMA5D2_MAX_POS_BITS - 1 862 643 */ 863 644 /* first half of register is the x or y, second half is the scale */ 864 - val = at91_adc_readl(st, reg); 645 + if (reg == st->soc_info.platform->layout->XPOSR) 646 + val = at91_adc_readl(st, XPOSR); 647 + else if (reg == st->soc_info.platform->layout->YPOSR) 648 + val = at91_adc_readl(st, YPOSR); 649 + 865 650 if (!val) 866 651 dev_dbg(&st->indio_dev->dev, "pos is 0\n"); 867 652 ··· 883 660 884 661 static u16 at91_adc_touch_x_pos(struct at91_adc_state *st) 885 662 { 886 - st->touch_st.x_pos = at91_adc_touch_pos(st, AT91_SAMA5D2_XPOSR); 663 + st->touch_st.x_pos = at91_adc_touch_pos(st, st->soc_info.platform->layout->XPOSR); 887 664 return st->touch_st.x_pos; 888 665 } 889 666 890 667 static u16 at91_adc_touch_y_pos(struct at91_adc_state *st) 891 668 { 892 - return at91_adc_touch_pos(st, AT91_SAMA5D2_YPOSR); 669 + return at91_adc_touch_pos(st, st->soc_info.platform->layout->YPOSR); 893 670 } 894 671 895 672 static u16 at91_adc_touch_pressure(struct at91_adc_state *st) ··· 901 678 u32 factor = 1000; 902 679 903 680 /* calculate the pressure */ 904 - val = at91_adc_readl(st, AT91_SAMA5D2_PRESSR); 681 + val = at91_adc_readl(st, PRESSR); 905 682 z1 = val & AT91_SAMA5D2_XYZ_MASK; 906 683 z2 = (val >> 16) & AT91_SAMA5D2_XYZ_MASK; 907 684 ··· 925 702 *val = 0; 926 703 if (!st->touch_st.touching) 927 704 return -ENODATA; 928 - if (chan == AT91_SAMA5D2_TOUCH_X_CHAN_IDX) 705 + if (chan == st->soc_info.platform->touch_chan_x) 929 706 *val = at91_adc_touch_x_pos(st); 930 - else if (chan == AT91_SAMA5D2_TOUCH_Y_CHAN_IDX) 707 + else if (chan == st->soc_info.platform->touch_chan_y) 931 708 *val = at91_adc_touch_y_pos(st); 932 709 else 933 710 return -ENODATA; ··· 940 717 *val = 0; 941 718 if (!st->touch_st.touching) 942 719 return -ENODATA; 943 - if (chan == AT91_SAMA5D2_TOUCH_P_CHAN_IDX) 720 + if (chan == st->soc_info.platform->touch_chan_p) 944 721 *val = at91_adc_touch_pressure(st); 945 722 else 946 723 return -ENODATA; ··· 952 729 { 953 730 struct iio_dev *indio = iio_trigger_get_drvdata(trig); 954 731 struct at91_adc_state *st = iio_priv(indio); 955 - u32 status = at91_adc_readl(st, AT91_SAMA5D2_TRGR); 732 + u32 status = at91_adc_readl(st, TRGR); 956 733 957 734 /* clear TRGMOD */ 958 735 status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK; ··· 961 738 status |= st->selected_trig->trgmod_value; 962 739 963 740 /* set/unset hw trigger */ 964 - at91_adc_writel(st, AT91_SAMA5D2_TRGR, status); 741 + at91_adc_writel(st, TRGR, status); 965 742 966 743 return 0; 967 744 } ··· 978 755 enable_irq(st->irq); 979 756 980 757 /* Needed to ACK the DRDY interruption */ 981 - at91_adc_readl(st, AT91_SAMA5D2_LCDR); 758 + at91_adc_readl(st, LCDR); 982 759 } 983 760 984 761 static const struct iio_trigger_ops at91_adc_trigger_ops = { ··· 1073 850 } 1074 851 1075 852 /* enable general overrun error signaling */ 1076 - at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_GOVRE); 853 + at91_adc_writel(st, IER, AT91_SAMA5D2_IER_GOVRE); 1077 854 /* Issue pending DMA requests */ 1078 855 dma_async_issue_pending(st->dma_st.dma_chan); 1079 856 ··· 1103 880 1104 881 return !!bitmap_subset(indio_dev->active_scan_mask, 1105 882 &st->touch_st.channels_bitmask, 1106 - AT91_SAMA5D2_MAX_CHAN_IDX + 1); 883 + st->soc_info.platform->max_index + 1); 1107 884 } 1108 885 1109 886 static int at91_adc_buffer_prepare(struct iio_dev *indio_dev) ··· 1131 908 indio_dev->num_channels) { 1132 909 struct iio_chan_spec const *chan = 1133 910 at91_adc_chan_get(indio_dev, bit); 1134 - u32 cor; 1135 - 1136 911 if (!chan) 1137 912 continue; 1138 913 /* these channel types cannot be handled by this trigger */ ··· 1138 917 chan->type == IIO_PRESSURE) 1139 918 continue; 1140 919 1141 - cor = at91_adc_readl(st, AT91_SAMA5D2_COR); 920 + at91_adc_cor(st, chan); 1142 921 1143 - if (chan->differential) 1144 - cor |= (BIT(chan->channel) | BIT(chan->channel2)) << 1145 - AT91_SAMA5D2_COR_DIFF_OFFSET; 1146 - else 1147 - cor &= ~(BIT(chan->channel) << 1148 - AT91_SAMA5D2_COR_DIFF_OFFSET); 1149 - 1150 - at91_adc_writel(st, AT91_SAMA5D2_COR, cor); 1151 - 1152 - at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel)); 922 + at91_adc_writel(st, CHER, BIT(chan->channel)); 1153 923 } 1154 924 1155 925 if (at91_adc_buffer_check_use_irq(indio_dev, st)) 1156 - at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_DRDY); 926 + at91_adc_writel(st, IER, AT91_SAMA5D2_IER_DRDY); 1157 927 1158 928 return 0; 1159 929 } ··· 1180 968 chan->type == IIO_PRESSURE) 1181 969 continue; 1182 970 1183 - at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel)); 971 + at91_adc_writel(st, CHDR, BIT(chan->channel)); 1184 972 1185 973 if (st->dma_st.dma_chan) 1186 - at91_adc_readl(st, chan->address); 974 + at91_adc_read_chan(st, chan->address); 1187 975 } 1188 976 1189 977 if (at91_adc_buffer_check_use_irq(indio_dev, st)) 1190 - at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_DRDY); 978 + at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_DRDY); 1191 979 1192 980 /* read overflow register to clear possible overflow status */ 1193 - at91_adc_readl(st, AT91_SAMA5D2_OVER); 981 + at91_adc_readl(st, OVER); 1194 982 1195 983 /* if we are using DMA we must clear registers and end DMA */ 1196 984 if (st->dma_st.dma_chan) ··· 1233 1021 u8 bit; 1234 1022 u32 mask = at91_adc_active_scan_mask_to_reg(indio_dev); 1235 1023 unsigned int timeout = 50; 1024 + u32 status, imr, eoc = 0, eoc_imr; 1236 1025 1237 1026 /* 1238 1027 * Check if the conversion is ready. If not, wait a little bit, and 1239 1028 * in case of timeout exit with an error. 1240 1029 */ 1241 - while ((at91_adc_readl(st, AT91_SAMA5D2_ISR) & mask) != mask && 1242 - timeout) { 1030 + while (((eoc & mask) != mask) && timeout) { 1031 + at91_adc_irq_status(st, &status, &eoc); 1032 + at91_adc_irq_mask(st, &imr, &eoc_imr); 1243 1033 usleep_range(50, 100); 1244 1034 timeout--; 1245 1035 } ··· 1268 1054 * Thus, emit a warning. 1269 1055 */ 1270 1056 if (chan->type == IIO_VOLTAGE) { 1271 - val = at91_adc_readl(st, chan->address); 1057 + val = at91_adc_read_chan(st, chan->address); 1272 1058 at91_adc_adjust_val_osr(st, &val); 1273 1059 st->buffer[i] = val; 1274 1060 } else { ··· 1289 1075 s64 interval; 1290 1076 int sample_index = 0, sample_count, sample_size; 1291 1077 1292 - u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR); 1078 + u32 status = at91_adc_readl(st, ISR); 1293 1079 /* if we reached this point, we cannot sample faster */ 1294 1080 if (status & AT91_SAMA5D2_IER_GOVRE) 1295 1081 pr_info_ratelimited("%s: conversion overrun detected\n", ··· 1341 1127 * actually polling the trigger now. 1342 1128 */ 1343 1129 if (iio_trigger_validate_own_device(indio_dev->trig, indio_dev)) 1344 - at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START); 1130 + at91_adc_writel(st, CR, AT91_SAMA5D2_CR_START); 1345 1131 1346 1132 if (st->dma_st.dma_chan) 1347 1133 at91_adc_trigger_handler_dma(indio_dev); ··· 1388 1174 startup = at91_adc_startup_time(st->soc_info.startup_time, 1389 1175 freq / 1000); 1390 1176 1391 - mr = at91_adc_readl(st, AT91_SAMA5D2_MR); 1177 + mr = at91_adc_readl(st, MR); 1392 1178 mr &= ~(AT91_SAMA5D2_MR_STARTUP_MASK | AT91_SAMA5D2_MR_PRESCAL_MASK); 1393 1179 mr |= AT91_SAMA5D2_MR_STARTUP(startup); 1394 1180 mr |= AT91_SAMA5D2_MR_PRESCAL(prescal); 1395 - at91_adc_writel(st, AT91_SAMA5D2_MR, mr); 1181 + at91_adc_writel(st, MR, mr); 1396 1182 1397 1183 dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n", 1398 1184 freq, startup, prescal); ··· 1412 1198 int i = 0; 1413 1199 1414 1200 for_each_set_bit(bit, indio_dev->active_scan_mask, 1415 - AT91_SAMA5D2_MAX_CHAN_IDX + 1) { 1201 + st->soc_info.platform->max_index + 1) { 1416 1202 struct iio_chan_spec const *chan = 1417 1203 at91_adc_chan_get(indio_dev, bit); 1418 1204 ··· 1438 1224 1439 1225 static void at91_adc_pen_detect_interrupt(struct at91_adc_state *st) 1440 1226 { 1441 - at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_PEN); 1442 - at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_NOPEN | 1227 + at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_PEN); 1228 + at91_adc_writel(st, IER, AT91_SAMA5D2_IER_NOPEN | 1443 1229 AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY | 1444 1230 AT91_SAMA5D2_IER_PRDY); 1445 - at91_adc_writel(st, AT91_SAMA5D2_TRGR, 1446 - AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC | 1231 + at91_adc_writel(st, TRGR, AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC | 1447 1232 AT91_SAMA5D2_TRGR_TRGPER(st->touch_st.sample_period_val)); 1448 1233 st->touch_st.touching = true; 1449 1234 } ··· 1451 1238 { 1452 1239 struct at91_adc_state *st = iio_priv(indio_dev); 1453 1240 1454 - at91_adc_writel(st, AT91_SAMA5D2_TRGR, 1455 - AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER); 1456 - at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_NOPEN | 1241 + at91_adc_writel(st, TRGR, AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER); 1242 + at91_adc_writel(st, IDR, AT91_SAMA5D2_IER_NOPEN | 1457 1243 AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY | 1458 1244 AT91_SAMA5D2_IER_PRDY); 1459 1245 st->touch_st.touching = false; 1460 1246 1461 1247 at91_adc_touch_data_handler(indio_dev); 1462 1248 1463 - at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_PEN); 1249 + at91_adc_writel(st, IER, AT91_SAMA5D2_IER_PEN); 1464 1250 } 1465 1251 1466 1252 static void at91_adc_workq_handler(struct work_struct *workq) ··· 1477 1265 { 1478 1266 struct iio_dev *indio = private; 1479 1267 struct at91_adc_state *st = iio_priv(indio); 1480 - u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR); 1481 - u32 imr = at91_adc_readl(st, AT91_SAMA5D2_IMR); 1268 + u32 status, eoc, imr, eoc_imr; 1482 1269 u32 rdy_mask = AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY | 1483 1270 AT91_SAMA5D2_IER_PRDY; 1484 1271 1485 - if (!(status & imr)) 1272 + at91_adc_irq_status(st, &status, &eoc); 1273 + at91_adc_irq_mask(st, &imr, &eoc_imr); 1274 + 1275 + if (!(status & imr) && !(eoc & eoc_imr)) 1486 1276 return IRQ_NONE; 1487 1277 if (status & AT91_SAMA5D2_IER_PEN) { 1488 1278 /* pen detected IRQ */ ··· 1501 1287 * touching, but the measurements are not ready yet. 1502 1288 * read and ignore. 1503 1289 */ 1504 - status = at91_adc_readl(st, AT91_SAMA5D2_XPOSR); 1505 - status = at91_adc_readl(st, AT91_SAMA5D2_YPOSR); 1506 - status = at91_adc_readl(st, AT91_SAMA5D2_PRESSR); 1290 + status = at91_adc_readl(st, XPOSR); 1291 + status = at91_adc_readl(st, YPOSR); 1292 + status = at91_adc_readl(st, PRESSR); 1507 1293 } else if (iio_buffer_enabled(indio) && 1508 1294 (status & AT91_SAMA5D2_IER_DRDY)) { 1509 1295 /* triggered buffer without DMA */ ··· 1515 1301 WARN(true, "Unexpected irq occurred\n"); 1516 1302 } else if (!iio_buffer_enabled(indio)) { 1517 1303 /* software requested conversion */ 1518 - st->conversion_value = at91_adc_readl(st, st->chan->address); 1304 + st->conversion_value = at91_adc_read_chan(st, st->chan->address); 1519 1305 st->conversion_done = true; 1520 1306 wake_up_interruptible(&st->wq_data_available); 1521 1307 } ··· 1526 1312 struct iio_chan_spec const *chan, int *val) 1527 1313 { 1528 1314 struct at91_adc_state *st = iio_priv(indio_dev); 1529 - u32 cor = 0; 1530 1315 u16 tmp_val; 1531 1316 int ret; 1532 1317 ··· 1571 1358 1572 1359 st->chan = chan; 1573 1360 1574 - if (chan->differential) 1575 - cor = (BIT(chan->channel) | BIT(chan->channel2)) << 1576 - AT91_SAMA5D2_COR_DIFF_OFFSET; 1577 - 1578 - at91_adc_writel(st, AT91_SAMA5D2_COR, cor); 1579 - at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel)); 1580 - at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel)); 1581 - at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START); 1361 + at91_adc_cor(st, chan); 1362 + at91_adc_writel(st, CHER, BIT(chan->channel)); 1363 + at91_adc_eoc_ena(st, chan->channel); 1364 + at91_adc_writel(st, CR, AT91_SAMA5D2_CR_START); 1582 1365 1583 1366 ret = wait_event_interruptible_timeout(st->wq_data_available, 1584 1367 st->conversion_done, ··· 1590 1381 st->conversion_done = false; 1591 1382 } 1592 1383 1593 - at91_adc_writel(st, AT91_SAMA5D2_IDR, BIT(chan->channel)); 1594 - at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel)); 1384 + at91_adc_eoc_dis(st, st->chan->channel); 1385 + at91_adc_writel(st, CHDR, BIT(chan->channel)); 1595 1386 1596 1387 /* Needed to ACK the DRDY interruption */ 1597 - at91_adc_readl(st, AT91_SAMA5D2_LCDR); 1388 + at91_adc_readl(st, LCDR); 1598 1389 1599 1390 mutex_unlock(&st->lock); 1600 1391 ··· 1666 1457 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 1667 1458 struct at91_adc_state *st = iio_priv(indio_dev); 1668 1459 struct dma_slave_config config = {0}; 1460 + /* we have 2 bytes for each channel */ 1461 + unsigned int sample_size = st->soc_info.platform->nr_channels * 2; 1669 1462 /* 1670 1463 * We make the buffer double the size of the fifo, 1671 1464 * such that DMA uses one half of the buffer (full fifo size) 1672 1465 * and the software uses the other half to read/write. 1673 1466 */ 1674 1467 unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE * 1675 - AT91_BUFFER_MAX_CONVERSION_BYTES * 2, 1676 - PAGE_SIZE); 1468 + sample_size * 2, PAGE_SIZE); 1677 1469 1678 1470 if (st->dma_st.dma_chan) 1679 1471 return; ··· 1698 1488 /* Configure DMA channel to read data register */ 1699 1489 config.direction = DMA_DEV_TO_MEM; 1700 1490 config.src_addr = (phys_addr_t)(st->dma_st.phys_addr 1701 - + AT91_SAMA5D2_LCDR); 1491 + + st->soc_info.platform->layout->LCDR); 1702 1492 config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; 1703 1493 config.src_maxburst = 1; 1704 1494 config.dst_maxburst = 1; ··· 1727 1517 { 1728 1518 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 1729 1519 struct at91_adc_state *st = iio_priv(indio_dev); 1520 + /* we have 2 bytes for each channel */ 1521 + unsigned int sample_size = st->soc_info.platform->nr_channels * 2; 1730 1522 unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE * 1731 - AT91_BUFFER_MAX_CONVERSION_BYTES * 2, 1732 - PAGE_SIZE); 1523 + sample_size * 2, PAGE_SIZE); 1733 1524 1734 1525 /* if we are not using DMA, just return */ 1735 1526 if (!st->dma_st.dma_chan) ··· 1791 1580 struct at91_adc_state *st = iio_priv(indio_dev); 1792 1581 1793 1582 if (bitmap_subset(scan_mask, &st->touch_st.channels_bitmask, 1794 - AT91_SAMA5D2_MAX_CHAN_IDX + 1)) 1583 + st->soc_info.platform->max_index + 1)) 1795 1584 return 0; 1796 1585 /* 1797 1586 * if the new bitmap is a combination of touchscreen and regular 1798 1587 * channels, then we are not fine 1799 1588 */ 1800 1589 if (bitmap_intersects(&st->touch_st.channels_bitmask, scan_mask, 1801 - AT91_SAMA5D2_MAX_CHAN_IDX + 1)) 1590 + st->soc_info.platform->max_index + 1)) 1802 1591 return -EINVAL; 1803 1592 return 0; 1804 1593 } ··· 1807 1596 { 1808 1597 struct at91_adc_state *st = iio_priv(indio_dev); 1809 1598 1810 - at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST); 1811 - at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff); 1599 + at91_adc_writel(st, CR, AT91_SAMA5D2_CR_SWRST); 1600 + if (st->soc_info.platform->layout->EOC_IDR) 1601 + at91_adc_writel(st, EOC_IDR, 0xffffffff); 1602 + at91_adc_writel(st, IDR, 0xffffffff); 1812 1603 /* 1813 1604 * Transfer field must be set to 2 according to the datasheet and 1814 1605 * allows different analog settings for each channel. 1815 1606 */ 1816 - at91_adc_writel(st, AT91_SAMA5D2_MR, 1607 + at91_adc_writel(st, MR, 1817 1608 AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH); 1818 1609 1819 1610 at91_adc_setup_samp_freq(indio_dev, st->soc_info.min_sample_rate); ··· 1894 1681 fifo_attrs = NULL; 1895 1682 1896 1683 ret = devm_iio_triggered_buffer_setup_ext(&indio->dev, indio, 1897 - &iio_pollfunc_store_time, 1898 - &at91_adc_trigger_handler, &at91_buffer_setup_ops, fifo_attrs); 1684 + &iio_pollfunc_store_time, &at91_adc_trigger_handler, 1685 + IIO_BUFFER_DIRECTION_IN, &at91_buffer_setup_ops, fifo_attrs); 1899 1686 if (ret < 0) { 1900 1687 dev_err(dev, "couldn't initialize the buffer.\n"); 1901 1688 return ret; ··· 1931 1718 if (!indio_dev) 1932 1719 return -ENOMEM; 1933 1720 1934 - indio_dev->name = dev_name(&pdev->dev); 1935 - indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; 1936 - indio_dev->info = &at91_adc_info; 1937 - indio_dev->channels = at91_adc_channels; 1938 - indio_dev->num_channels = ARRAY_SIZE(at91_adc_channels); 1939 - 1940 1721 st = iio_priv(indio_dev); 1941 1722 st->indio_dev = indio_dev; 1942 1723 1724 + st->soc_info.platform = of_device_get_match_data(&pdev->dev); 1725 + 1726 + indio_dev->name = dev_name(&pdev->dev); 1727 + indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; 1728 + indio_dev->info = &at91_adc_info; 1729 + indio_dev->channels = *st->soc_info.platform->adc_channels; 1730 + indio_dev->num_channels = st->soc_info.platform->max_channels; 1731 + 1943 1732 bitmap_set(&st->touch_st.channels_bitmask, 1944 - AT91_SAMA5D2_TOUCH_X_CHAN_IDX, 1); 1733 + st->soc_info.platform->touch_chan_x, 1); 1945 1734 bitmap_set(&st->touch_st.channels_bitmask, 1946 - AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, 1); 1735 + st->soc_info.platform->touch_chan_y, 1); 1947 1736 bitmap_set(&st->touch_st.channels_bitmask, 1948 - AT91_SAMA5D2_TOUCH_P_CHAN_IDX, 1); 1737 + st->soc_info.platform->touch_chan_p, 1); 1949 1738 1950 1739 st->oversampling_ratio = AT91_OSR_1SAMPLES; 1951 1740 ··· 1987 1772 st->selected_trig = NULL; 1988 1773 1989 1774 /* find the right trigger, or no trigger at all */ 1990 - for (i = 0; i < AT91_SAMA5D2_HW_TRIG_CNT + 1; i++) 1775 + for (i = 0; i < st->soc_info.platform->hw_trig_cnt + 1; i++) 1991 1776 if (at91_adc_trigger_list[i].edge_type == edge_type) { 1992 1777 st->selected_trig = &at91_adc_trigger_list[i]; 1993 1778 break; ··· 2048 1833 goto vref_disable; 2049 1834 } 2050 1835 2051 - at91_adc_hw_init(indio_dev); 2052 - 2053 1836 ret = clk_prepare_enable(st->per_clk); 2054 1837 if (ret) 2055 1838 goto vref_disable; 1839 + 1840 + at91_adc_hw_init(indio_dev); 2056 1841 2057 1842 platform_set_drvdata(pdev, indio_dev); 2058 1843 ··· 2072 1857 st->selected_trig->name); 2073 1858 2074 1859 dev_info(&pdev->dev, "version: %x\n", 2075 - readl_relaxed(st->base + AT91_SAMA5D2_VERSION)); 1860 + readl_relaxed(st->base + st->soc_info.platform->layout->VERSION)); 2076 1861 2077 1862 return 0; 2078 1863 ··· 2115 1900 * and can be used by for other devices. 2116 1901 * Otherwise, ADC will hog them and we can't go to suspend mode. 2117 1902 */ 2118 - at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST); 1903 + at91_adc_writel(st, CR, AT91_SAMA5D2_CR_SWRST); 2119 1904 2120 1905 clk_disable_unprepare(st->per_clk); 2121 1906 regulator_disable(st->vref); ··· 2175 1960 static const struct of_device_id at91_adc_dt_match[] = { 2176 1961 { 2177 1962 .compatible = "atmel,sama5d2-adc", 1963 + .data = (const void *)&sama5d2_platform, 1964 + }, { 1965 + .compatible = "microchip,sama7g5-adc", 1966 + .data = (const void *)&sama7g5_platform, 2178 1967 }, { 2179 1968 /* sentinel */ 2180 1969 } ··· 2196 1977 }; 2197 1978 module_platform_driver(at91_adc_driver) 2198 1979 2199 - MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>"); 1980 + MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@microchip.com>"); 1981 + MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com"); 2200 1982 MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC"); 2201 1983 MODULE_LICENSE("GPL v2");
+4 -24
drivers/iio/adc/axp288_adc.c
··· 259 259 info->irq = platform_get_irq(pdev, 0); 260 260 if (info->irq < 0) 261 261 return info->irq; 262 - platform_set_drvdata(pdev, indio_dev); 262 + 263 263 info->regmap = axp20x->regmap; 264 264 /* 265 265 * Set ADC to enabled state at all time, including system suspend. ··· 276 276 indio_dev->num_channels = ARRAY_SIZE(axp288_adc_channels); 277 277 indio_dev->info = &axp288_adc_iio_info; 278 278 indio_dev->modes = INDIO_DIRECT_MODE; 279 - ret = iio_map_array_register(indio_dev, axp288_adc_default_maps); 279 + 280 + ret = devm_iio_map_array_register(&pdev->dev, indio_dev, axp288_adc_default_maps); 280 281 if (ret < 0) 281 282 return ret; 282 283 283 - ret = iio_device_register(indio_dev); 284 - if (ret < 0) { 285 - dev_err(&pdev->dev, "unable to register iio device\n"); 286 - goto err_array_unregister; 287 - } 288 - return 0; 289 - 290 - err_array_unregister: 291 - iio_map_array_unregister(indio_dev); 292 - 293 - return ret; 294 - } 295 - 296 - static int axp288_adc_remove(struct platform_device *pdev) 297 - { 298 - struct iio_dev *indio_dev = platform_get_drvdata(pdev); 299 - 300 - iio_device_unregister(indio_dev); 301 - iio_map_array_unregister(indio_dev); 302 - 303 - return 0; 284 + return devm_iio_device_register(&pdev->dev, indio_dev); 304 285 } 305 286 306 287 static const struct platform_device_id axp288_adc_id_table[] = { ··· 291 310 292 311 static struct platform_driver axp288_adc_driver = { 293 312 .probe = axp288_adc_probe, 294 - .remove = axp288_adc_remove, 295 313 .id_table = axp288_adc_id_table, 296 314 .driver = { 297 315 .name = "axp288_adc",
+11 -23
drivers/iio/adc/berlin2-adc.c
··· 280 280 .read_raw = berlin2_adc_read_raw, 281 281 }; 282 282 283 + static void berlin2_adc_powerdown(void *regmap) 284 + { 285 + regmap_update_bits(regmap, BERLIN2_SM_CTRL, 286 + BERLIN2_SM_CTRL_ADC_POWER, 0); 287 + 288 + } 289 + 283 290 static int berlin2_adc_probe(struct platform_device *pdev) 284 291 { 285 292 struct iio_dev *indio_dev; ··· 300 293 return -ENOMEM; 301 294 302 295 priv = iio_priv(indio_dev); 303 - platform_set_drvdata(pdev, indio_dev); 304 296 305 297 priv->regmap = syscon_node_to_regmap(parent_np); 306 298 of_node_put(parent_np); ··· 339 333 BERLIN2_SM_CTRL_ADC_POWER, 340 334 BERLIN2_SM_CTRL_ADC_POWER); 341 335 342 - ret = iio_device_register(indio_dev); 343 - if (ret) { 344 - /* Power down the ADC */ 345 - regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL, 346 - BERLIN2_SM_CTRL_ADC_POWER, 0); 336 + ret = devm_add_action_or_reset(&pdev->dev, berlin2_adc_powerdown, 337 + priv->regmap); 338 + if (ret) 347 339 return ret; 348 - } 349 340 350 - return 0; 351 - } 352 - 353 - static int berlin2_adc_remove(struct platform_device *pdev) 354 - { 355 - struct iio_dev *indio_dev = platform_get_drvdata(pdev); 356 - struct berlin2_adc_priv *priv = iio_priv(indio_dev); 357 - 358 - iio_device_unregister(indio_dev); 359 - 360 - /* Power down the ADC */ 361 - regmap_update_bits(priv->regmap, BERLIN2_SM_CTRL, 362 - BERLIN2_SM_CTRL_ADC_POWER, 0); 363 - 364 - return 0; 341 + return devm_iio_device_register(&pdev->dev, indio_dev); 365 342 } 366 343 367 344 static const struct of_device_id berlin2_adc_match[] = { ··· 359 370 .of_match_table = berlin2_adc_match, 360 371 }, 361 372 .probe = berlin2_adc_probe, 362 - .remove = berlin2_adc_remove, 363 373 }; 364 374 module_platform_driver(berlin2_adc_driver); 365 375
+2 -25
drivers/iio/adc/da9150-gpadc.c
··· 330 330 } 331 331 gpadc = iio_priv(indio_dev); 332 332 333 - platform_set_drvdata(pdev, indio_dev); 334 333 gpadc->da9150 = da9150; 335 334 gpadc->dev = dev; 336 335 mutex_init(&gpadc->lock); ··· 346 347 return ret; 347 348 } 348 349 349 - ret = iio_map_array_register(indio_dev, da9150_gpadc_default_maps); 350 + ret = devm_iio_map_array_register(&pdev->dev, indio_dev, da9150_gpadc_default_maps); 350 351 if (ret) { 351 352 dev_err(dev, "Failed to register IIO maps: %d\n", ret); 352 353 return ret; ··· 358 359 indio_dev->channels = da9150_gpadc_channels; 359 360 indio_dev->num_channels = ARRAY_SIZE(da9150_gpadc_channels); 360 361 361 - ret = iio_device_register(indio_dev); 362 - if (ret) { 363 - dev_err(dev, "Failed to register IIO device: %d\n", ret); 364 - goto iio_map_unreg; 365 - } 366 - 367 - return 0; 368 - 369 - iio_map_unreg: 370 - iio_map_array_unregister(indio_dev); 371 - 372 - return ret; 373 - } 374 - 375 - static int da9150_gpadc_remove(struct platform_device *pdev) 376 - { 377 - struct iio_dev *indio_dev = platform_get_drvdata(pdev); 378 - 379 - iio_device_unregister(indio_dev); 380 - iio_map_array_unregister(indio_dev); 381 - 382 - return 0; 362 + return devm_iio_device_register(&pdev->dev, indio_dev); 383 363 } 384 364 385 365 static struct platform_driver da9150_gpadc_driver = { ··· 366 388 .name = "da9150-gpadc", 367 389 }, 368 390 .probe = da9150_gpadc_probe, 369 - .remove = da9150_gpadc_remove, 370 391 }; 371 392 372 393 module_platform_driver(da9150_gpadc_driver);
+1 -3
drivers/iio/adc/ep93xx_adc.c
··· 156 156 struct iio_dev *iiodev; 157 157 struct ep93xx_adc_priv *priv; 158 158 struct clk *pclk; 159 - struct resource *res; 160 159 161 160 iiodev = devm_iio_device_alloc(&pdev->dev, sizeof(*priv)); 162 161 if (!iiodev) 163 162 return -ENOMEM; 164 163 priv = iio_priv(iiodev); 165 164 166 - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 167 - priv->base = devm_ioremap_resource(&pdev->dev, res); 165 + priv->base = devm_platform_ioremap_resource(pdev, 0); 168 166 if (IS_ERR(priv->base)) 169 167 return PTR_ERR(priv->base); 170 168
+26 -29
drivers/iio/adc/fsl-imx25-gcq.c
··· 172 172 .reg_stride = 4, 173 173 }; 174 174 175 + static int mx25_gcq_ext_regulator_setup(struct device *dev, 176 + struct mx25_gcq_priv *priv, u32 refp) 177 + { 178 + char reg_name[12]; 179 + int ret; 180 + 181 + if (priv->vref[refp]) 182 + return 0; 183 + 184 + ret = snprintf(reg_name, sizeof(reg_name), "vref-%s", 185 + mx25_gcq_refp_names[refp]); 186 + if (ret < 0) 187 + return ret; 188 + 189 + priv->vref[refp] = devm_regulator_get_optional(dev, reg_name); 190 + if (IS_ERR(priv->vref[refp])) 191 + return dev_err_probe(dev, PTR_ERR(priv->vref[refp]), 192 + "Error, trying to use external voltage reference without a %s regulator.", 193 + reg_name); 194 + 195 + return 0; 196 + } 197 + 175 198 static int mx25_gcq_setup_cfgs(struct platform_device *pdev, 176 199 struct mx25_gcq_priv *priv) 177 200 { 178 201 struct device_node *np = pdev->dev.of_node; 179 202 struct device_node *child; 180 203 struct device *dev = &pdev->dev; 181 - unsigned int refp_used[4] = {}; 182 204 int ret, i; 183 205 184 206 /* ··· 215 193 MX25_ADCQ_CFG_REFP_INT | 216 194 MX25_ADCQ_CFG_IN(i) | 217 195 MX25_ADCQ_CFG_REFN_NGND2); 218 - 219 - /* 220 - * First get all regulators to store them in channel_vref_mv if 221 - * necessary. Later we use that information for proper IIO scale 222 - * information. 223 - */ 224 - priv->vref[MX25_ADC_REFP_INT] = NULL; 225 - priv->vref[MX25_ADC_REFP_EXT] = 226 - devm_regulator_get_optional(dev, "vref-ext"); 227 - priv->vref[MX25_ADC_REFP_XP] = 228 - devm_regulator_get_optional(dev, "vref-xp"); 229 - priv->vref[MX25_ADC_REFP_YP] = 230 - devm_regulator_get_optional(dev, "vref-yp"); 231 196 232 197 for_each_child_of_node(np, child) { 233 198 u32 reg; ··· 242 233 case MX25_ADC_REFP_EXT: 243 234 case MX25_ADC_REFP_XP: 244 235 case MX25_ADC_REFP_YP: 245 - if (IS_ERR(priv->vref[refp])) { 246 - dev_err(dev, "Error, trying to use external voltage reference without a vref-%s regulator.", 247 - mx25_gcq_refp_names[refp]); 236 + ret = mx25_gcq_ext_regulator_setup(&pdev->dev, priv, refp); 237 + if (ret) { 248 238 of_node_put(child); 249 - return PTR_ERR(priv->vref[refp]); 239 + return ret; 250 240 } 251 241 priv->channel_vref_mv[reg] = 252 242 regulator_get_voltage(priv->vref[refp]); ··· 260 252 of_node_put(child); 261 253 return -EINVAL; 262 254 } 263 - 264 - ++refp_used[refp]; 265 255 266 256 /* 267 257 * Shift the read values to the correct positions within the ··· 290 284 291 285 regmap_write(priv->regs, MX25_ADCQ_CR, 292 286 MX25_ADCQ_CR_PDMSK | MX25_ADCQ_CR_QSM_FQS); 293 - 294 - /* Remove unused regulators */ 295 - for (i = 0; i != 4; ++i) { 296 - if (!refp_used[i]) { 297 - if (!IS_ERR_OR_NULL(priv->vref[i])) 298 - devm_regulator_put(priv->vref[i]); 299 - priv->vref[i] = NULL; 300 - } 301 - } 302 287 303 288 return 0; 304 289 }
+494
drivers/iio/adc/imx8qxp-adc.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * NXP i.MX8QXP ADC driver 4 + * 5 + * Based on the work of Haibo Chen <haibo.chen@nxp.com> 6 + * The initial developer of the original code is Haibo Chen. 7 + * Portions created by Haibo Chen are Copyright (C) 2018 NXP. 8 + * All Rights Reserved. 9 + * 10 + * Copyright (C) 2018 NXP 11 + * Copyright (C) 2021 Cai Huoqing 12 + */ 13 + #include <linux/bitfield.h> 14 + #include <linux/bits.h> 15 + #include <linux/clk.h> 16 + #include <linux/completion.h> 17 + #include <linux/delay.h> 18 + #include <linux/err.h> 19 + #include <linux/interrupt.h> 20 + #include <linux/io.h> 21 + #include <linux/kernel.h> 22 + #include <linux/module.h> 23 + #include <linux/platform_device.h> 24 + #include <linux/pm_runtime.h> 25 + #include <linux/regulator/consumer.h> 26 + 27 + #include <linux/iio/iio.h> 28 + 29 + #define ADC_DRIVER_NAME "imx8qxp-adc" 30 + 31 + /* Register map definition */ 32 + #define IMX8QXP_ADR_ADC_CTRL 0x10 33 + #define IMX8QXP_ADR_ADC_STAT 0x14 34 + #define IMX8QXP_ADR_ADC_IE 0x18 35 + #define IMX8QXP_ADR_ADC_DE 0x1c 36 + #define IMX8QXP_ADR_ADC_CFG 0x20 37 + #define IMX8QXP_ADR_ADC_FCTRL 0x30 38 + #define IMX8QXP_ADR_ADC_SWTRIG 0x34 39 + #define IMX8QXP_ADR_ADC_TCTRL(tid) (0xc0 + (tid) * 4) 40 + #define IMX8QXP_ADR_ADC_CMDH(cid) (0x100 + (cid) * 8) 41 + #define IMX8QXP_ADR_ADC_CMDL(cid) (0x104 + (cid) * 8) 42 + #define IMX8QXP_ADR_ADC_RESFIFO 0x300 43 + #define IMX8QXP_ADR_ADC_TST 0xffc 44 + 45 + /* ADC bit shift */ 46 + #define IMX8QXP_ADC_IE_FWMIE_MASK GENMASK(1, 0) 47 + #define IMX8QXP_ADC_CTRL_FIFO_RESET_MASK BIT(8) 48 + #define IMX8QXP_ADC_CTRL_SOFTWARE_RESET_MASK BIT(1) 49 + #define IMX8QXP_ADC_CTRL_ADC_EN_MASK BIT(0) 50 + #define IMX8QXP_ADC_TCTRL_TCMD_MASK GENMASK(31, 24) 51 + #define IMX8QXP_ADC_TCTRL_TDLY_MASK GENMASK(23, 16) 52 + #define IMX8QXP_ADC_TCTRL_TPRI_MASK GENMASK(15, 8) 53 + #define IMX8QXP_ADC_TCTRL_HTEN_MASK GENMASK(7, 0) 54 + #define IMX8QXP_ADC_CMDL_CSCALE_MASK GENMASK(13, 8) 55 + #define IMX8QXP_ADC_CMDL_MODE_MASK BIT(7) 56 + #define IMX8QXP_ADC_CMDL_DIFF_MASK BIT(6) 57 + #define IMX8QXP_ADC_CMDL_ABSEL_MASK BIT(5) 58 + #define IMX8QXP_ADC_CMDL_ADCH_MASK GENMASK(2, 0) 59 + #define IMX8QXP_ADC_CMDH_NEXT_MASK GENMASK(31, 24) 60 + #define IMX8QXP_ADC_CMDH_LOOP_MASK GENMASK(23, 16) 61 + #define IMX8QXP_ADC_CMDH_AVGS_MASK GENMASK(15, 12) 62 + #define IMX8QXP_ADC_CMDH_STS_MASK BIT(8) 63 + #define IMX8QXP_ADC_CMDH_LWI_MASK GENMASK(7, 7) 64 + #define IMX8QXP_ADC_CMDH_CMPEN_MASK GENMASK(0, 0) 65 + #define IMX8QXP_ADC_CFG_PWREN_MASK BIT(28) 66 + #define IMX8QXP_ADC_CFG_PUDLY_MASK GENMASK(23, 16) 67 + #define IMX8QXP_ADC_CFG_REFSEL_MASK GENMASK(7, 6) 68 + #define IMX8QXP_ADC_CFG_PWRSEL_MASK GENMASK(5, 4) 69 + #define IMX8QXP_ADC_CFG_TPRICTRL_MASK GENMASK(3, 0) 70 + #define IMX8QXP_ADC_FCTRL_FWMARK_MASK GENMASK(20, 16) 71 + #define IMX8QXP_ADC_FCTRL_FCOUNT_MASK GENMASK(4, 0) 72 + #define IMX8QXP_ADC_RESFIFO_VAL_MASK GENMASK(18, 3) 73 + 74 + /* ADC PARAMETER*/ 75 + #define IMX8QXP_ADC_CMDL_CHANNEL_SCALE_FULL GENMASK(5, 0) 76 + #define IMX8QXP_ADC_CMDL_SEL_A_A_B_CHANNEL 0 77 + #define IMX8QXP_ADC_CMDL_STANDARD_RESOLUTION 0 78 + #define IMX8QXP_ADC_CMDL_MODE_SINGLE 0 79 + #define IMX8QXP_ADC_CMDH_LWI_INCREMENT_DIS 0 80 + #define IMX8QXP_ADC_CMDH_CMPEN_DIS 0 81 + #define IMX8QXP_ADC_PAUSE_EN BIT(31) 82 + #define IMX8QXP_ADC_TCTRL_TPRI_PRIORITY_HIGH 0 83 + 84 + #define IMX8QXP_ADC_TCTRL_HTEN_HW_TIRG_DIS 0 85 + 86 + #define IMX8QXP_ADC_TIMEOUT msecs_to_jiffies(100) 87 + 88 + struct imx8qxp_adc { 89 + struct device *dev; 90 + void __iomem *regs; 91 + struct clk *clk; 92 + struct clk *ipg_clk; 93 + struct regulator *vref; 94 + /* Serialise ADC channel reads */ 95 + struct mutex lock; 96 + struct completion completion; 97 + }; 98 + 99 + #define IMX8QXP_ADC_CHAN(_idx) { \ 100 + .type = IIO_VOLTAGE, \ 101 + .indexed = 1, \ 102 + .channel = (_idx), \ 103 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 104 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 105 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 106 + } 107 + 108 + static const struct iio_chan_spec imx8qxp_adc_iio_channels[] = { 109 + IMX8QXP_ADC_CHAN(0), 110 + IMX8QXP_ADC_CHAN(1), 111 + IMX8QXP_ADC_CHAN(2), 112 + IMX8QXP_ADC_CHAN(3), 113 + IMX8QXP_ADC_CHAN(4), 114 + IMX8QXP_ADC_CHAN(5), 115 + IMX8QXP_ADC_CHAN(6), 116 + IMX8QXP_ADC_CHAN(7), 117 + }; 118 + 119 + static void imx8qxp_adc_reset(struct imx8qxp_adc *adc) 120 + { 121 + u32 ctrl; 122 + 123 + /*software reset, need to clear the set bit*/ 124 + ctrl = readl(adc->regs + IMX8QXP_ADR_ADC_CTRL); 125 + ctrl |= FIELD_PREP(IMX8QXP_ADC_CTRL_SOFTWARE_RESET_MASK, 1); 126 + writel(ctrl, adc->regs + IMX8QXP_ADR_ADC_CTRL); 127 + udelay(10); 128 + ctrl &= ~FIELD_PREP(IMX8QXP_ADC_CTRL_SOFTWARE_RESET_MASK, 1); 129 + writel(ctrl, adc->regs + IMX8QXP_ADR_ADC_CTRL); 130 + 131 + /* reset the fifo */ 132 + ctrl |= FIELD_PREP(IMX8QXP_ADC_CTRL_FIFO_RESET_MASK, 1); 133 + writel(ctrl, adc->regs + IMX8QXP_ADR_ADC_CTRL); 134 + } 135 + 136 + static void imx8qxp_adc_reg_config(struct imx8qxp_adc *adc, int channel) 137 + { 138 + u32 adc_cfg, adc_tctrl, adc_cmdl, adc_cmdh; 139 + 140 + /* ADC configuration */ 141 + adc_cfg = FIELD_PREP(IMX8QXP_ADC_CFG_PWREN_MASK, 1) | 142 + FIELD_PREP(IMX8QXP_ADC_CFG_PUDLY_MASK, 0x80)| 143 + FIELD_PREP(IMX8QXP_ADC_CFG_REFSEL_MASK, 0) | 144 + FIELD_PREP(IMX8QXP_ADC_CFG_PWRSEL_MASK, 3) | 145 + FIELD_PREP(IMX8QXP_ADC_CFG_TPRICTRL_MASK, 0); 146 + writel(adc_cfg, adc->regs + IMX8QXP_ADR_ADC_CFG); 147 + 148 + /* config the trigger control */ 149 + adc_tctrl = FIELD_PREP(IMX8QXP_ADC_TCTRL_TCMD_MASK, 1) | 150 + FIELD_PREP(IMX8QXP_ADC_TCTRL_TDLY_MASK, 0) | 151 + FIELD_PREP(IMX8QXP_ADC_TCTRL_TPRI_MASK, IMX8QXP_ADC_TCTRL_TPRI_PRIORITY_HIGH) | 152 + FIELD_PREP(IMX8QXP_ADC_TCTRL_HTEN_MASK, IMX8QXP_ADC_TCTRL_HTEN_HW_TIRG_DIS); 153 + writel(adc_tctrl, adc->regs + IMX8QXP_ADR_ADC_TCTRL(0)); 154 + 155 + /* config the cmd */ 156 + adc_cmdl = FIELD_PREP(IMX8QXP_ADC_CMDL_CSCALE_MASK, IMX8QXP_ADC_CMDL_CHANNEL_SCALE_FULL) | 157 + FIELD_PREP(IMX8QXP_ADC_CMDL_MODE_MASK, IMX8QXP_ADC_CMDL_STANDARD_RESOLUTION) | 158 + FIELD_PREP(IMX8QXP_ADC_CMDL_DIFF_MASK, IMX8QXP_ADC_CMDL_MODE_SINGLE) | 159 + FIELD_PREP(IMX8QXP_ADC_CMDL_ABSEL_MASK, IMX8QXP_ADC_CMDL_SEL_A_A_B_CHANNEL) | 160 + FIELD_PREP(IMX8QXP_ADC_CMDL_ADCH_MASK, channel); 161 + writel(adc_cmdl, adc->regs + IMX8QXP_ADR_ADC_CMDL(0)); 162 + 163 + adc_cmdh = FIELD_PREP(IMX8QXP_ADC_CMDH_NEXT_MASK, 0) | 164 + FIELD_PREP(IMX8QXP_ADC_CMDH_LOOP_MASK, 0) | 165 + FIELD_PREP(IMX8QXP_ADC_CMDH_AVGS_MASK, 7) | 166 + FIELD_PREP(IMX8QXP_ADC_CMDH_STS_MASK, 0) | 167 + FIELD_PREP(IMX8QXP_ADC_CMDH_LWI_MASK, IMX8QXP_ADC_CMDH_LWI_INCREMENT_DIS) | 168 + FIELD_PREP(IMX8QXP_ADC_CMDH_CMPEN_MASK, IMX8QXP_ADC_CMDH_CMPEN_DIS); 169 + writel(adc_cmdh, adc->regs + IMX8QXP_ADR_ADC_CMDH(0)); 170 + } 171 + 172 + static void imx8qxp_adc_fifo_config(struct imx8qxp_adc *adc) 173 + { 174 + u32 fifo_ctrl, interrupt_en; 175 + 176 + fifo_ctrl = readl(adc->regs + IMX8QXP_ADR_ADC_FCTRL); 177 + fifo_ctrl &= ~IMX8QXP_ADC_FCTRL_FWMARK_MASK; 178 + /* set the watermark level to 1 */ 179 + fifo_ctrl |= FIELD_PREP(IMX8QXP_ADC_FCTRL_FWMARK_MASK, 0); 180 + writel(fifo_ctrl, adc->regs + IMX8QXP_ADR_ADC_FCTRL); 181 + 182 + /* FIFO Watermark Interrupt Enable */ 183 + interrupt_en = readl(adc->regs + IMX8QXP_ADR_ADC_IE); 184 + interrupt_en |= FIELD_PREP(IMX8QXP_ADC_IE_FWMIE_MASK, 1); 185 + writel(interrupt_en, adc->regs + IMX8QXP_ADR_ADC_IE); 186 + } 187 + 188 + static void imx8qxp_adc_disable(struct imx8qxp_adc *adc) 189 + { 190 + u32 ctrl; 191 + 192 + ctrl = readl(adc->regs + IMX8QXP_ADR_ADC_CTRL); 193 + ctrl &= ~FIELD_PREP(IMX8QXP_ADC_CTRL_ADC_EN_MASK, 1); 194 + writel(ctrl, adc->regs + IMX8QXP_ADR_ADC_CTRL); 195 + } 196 + 197 + static int imx8qxp_adc_read_raw(struct iio_dev *indio_dev, 198 + struct iio_chan_spec const *chan, 199 + int *val, int *val2, long mask) 200 + { 201 + struct imx8qxp_adc *adc = iio_priv(indio_dev); 202 + struct device *dev = adc->dev; 203 + 204 + u32 ctrl, vref_uv; 205 + long ret; 206 + 207 + switch (mask) { 208 + case IIO_CHAN_INFO_RAW: 209 + pm_runtime_get_sync(dev); 210 + 211 + mutex_lock(&adc->lock); 212 + reinit_completion(&adc->completion); 213 + 214 + imx8qxp_adc_reg_config(adc, chan->channel); 215 + 216 + imx8qxp_adc_fifo_config(adc); 217 + 218 + /* adc enable */ 219 + ctrl = readl(adc->regs + IMX8QXP_ADR_ADC_CTRL); 220 + ctrl |= FIELD_PREP(IMX8QXP_ADC_CTRL_ADC_EN_MASK, 1); 221 + writel(ctrl, adc->regs + IMX8QXP_ADR_ADC_CTRL); 222 + /* adc start */ 223 + writel(1, adc->regs + IMX8QXP_ADR_ADC_SWTRIG); 224 + 225 + ret = wait_for_completion_interruptible_timeout(&adc->completion, 226 + IMX8QXP_ADC_TIMEOUT); 227 + 228 + pm_runtime_mark_last_busy(dev); 229 + pm_runtime_put_sync_autosuspend(dev); 230 + 231 + if (ret == 0) { 232 + mutex_unlock(&adc->lock); 233 + return -ETIMEDOUT; 234 + } 235 + if (ret < 0) { 236 + mutex_unlock(&adc->lock); 237 + return ret; 238 + } 239 + 240 + *val = FIELD_GET(IMX8QXP_ADC_RESFIFO_VAL_MASK, 241 + readl(adc->regs + IMX8QXP_ADR_ADC_RESFIFO)); 242 + 243 + mutex_unlock(&adc->lock); 244 + return IIO_VAL_INT; 245 + 246 + case IIO_CHAN_INFO_SCALE: 247 + vref_uv = regulator_get_voltage(adc->vref); 248 + *val = vref_uv / 1000; 249 + *val2 = 12; 250 + return IIO_VAL_FRACTIONAL_LOG2; 251 + 252 + case IIO_CHAN_INFO_SAMP_FREQ: 253 + *val = clk_get_rate(adc->clk) / 3; 254 + return IIO_VAL_INT; 255 + 256 + default: 257 + return -EINVAL; 258 + } 259 + } 260 + 261 + static irqreturn_t imx8qxp_adc_isr(int irq, void *dev_id) 262 + { 263 + struct imx8qxp_adc *adc = dev_id; 264 + u32 fifo_count; 265 + 266 + fifo_count = FIELD_GET(IMX8QXP_ADC_FCTRL_FCOUNT_MASK, 267 + readl(adc->regs + IMX8QXP_ADR_ADC_FCTRL)); 268 + 269 + if (fifo_count) 270 + complete(&adc->completion); 271 + 272 + return IRQ_HANDLED; 273 + } 274 + 275 + static int imx8qxp_adc_reg_access(struct iio_dev *indio_dev, unsigned int reg, 276 + unsigned int writeval, unsigned int *readval) 277 + { 278 + struct imx8qxp_adc *adc = iio_priv(indio_dev); 279 + struct device *dev = adc->dev; 280 + 281 + if (!readval || reg % 4 || reg > IMX8QXP_ADR_ADC_TST) 282 + return -EINVAL; 283 + 284 + pm_runtime_get_sync(dev); 285 + 286 + *readval = readl(adc->regs + reg); 287 + 288 + pm_runtime_mark_last_busy(dev); 289 + pm_runtime_put_sync_autosuspend(dev); 290 + 291 + return 0; 292 + } 293 + 294 + static const struct iio_info imx8qxp_adc_iio_info = { 295 + .read_raw = &imx8qxp_adc_read_raw, 296 + .debugfs_reg_access = &imx8qxp_adc_reg_access, 297 + }; 298 + 299 + static int imx8qxp_adc_probe(struct platform_device *pdev) 300 + { 301 + struct imx8qxp_adc *adc; 302 + struct iio_dev *indio_dev; 303 + struct device *dev = &pdev->dev; 304 + int irq; 305 + int ret; 306 + 307 + indio_dev = devm_iio_device_alloc(dev, sizeof(*adc)); 308 + if (!indio_dev) { 309 + dev_err(dev, "Failed allocating iio device\n"); 310 + return -ENOMEM; 311 + } 312 + 313 + adc = iio_priv(indio_dev); 314 + adc->dev = dev; 315 + 316 + mutex_init(&adc->lock); 317 + adc->regs = devm_platform_ioremap_resource(pdev, 0); 318 + if (IS_ERR(adc->regs)) 319 + return PTR_ERR(adc->regs); 320 + 321 + irq = platform_get_irq(pdev, 0); 322 + if (irq < 0) 323 + return irq; 324 + 325 + adc->clk = devm_clk_get(dev, "per"); 326 + if (IS_ERR(adc->clk)) 327 + return dev_err_probe(dev, PTR_ERR(adc->clk), "Failed getting clock\n"); 328 + 329 + adc->ipg_clk = devm_clk_get(dev, "ipg"); 330 + if (IS_ERR(adc->ipg_clk)) 331 + return dev_err_probe(dev, PTR_ERR(adc->ipg_clk), "Failed getting clock\n"); 332 + 333 + adc->vref = devm_regulator_get(dev, "vref"); 334 + if (IS_ERR(adc->vref)) 335 + return dev_err_probe(dev, PTR_ERR(adc->vref), "Failed getting reference voltage\n"); 336 + 337 + ret = regulator_enable(adc->vref); 338 + if (ret) { 339 + dev_err(dev, "Can't enable adc reference top voltage\n"); 340 + return ret; 341 + } 342 + 343 + platform_set_drvdata(pdev, indio_dev); 344 + 345 + init_completion(&adc->completion); 346 + 347 + indio_dev->name = ADC_DRIVER_NAME; 348 + indio_dev->info = &imx8qxp_adc_iio_info; 349 + indio_dev->modes = INDIO_DIRECT_MODE; 350 + indio_dev->channels = imx8qxp_adc_iio_channels; 351 + indio_dev->num_channels = ARRAY_SIZE(imx8qxp_adc_iio_channels); 352 + 353 + ret = clk_prepare_enable(adc->clk); 354 + if (ret) { 355 + dev_err(&pdev->dev, "Could not prepare or enable the clock.\n"); 356 + goto error_regulator_disable; 357 + } 358 + 359 + ret = clk_prepare_enable(adc->ipg_clk); 360 + if (ret) { 361 + dev_err(&pdev->dev, "Could not prepare or enable the clock.\n"); 362 + goto error_adc_clk_disable; 363 + } 364 + 365 + ret = devm_request_irq(dev, irq, imx8qxp_adc_isr, 0, ADC_DRIVER_NAME, adc); 366 + if (ret < 0) { 367 + dev_err(dev, "Failed requesting irq, irq = %d\n", irq); 368 + goto error_ipg_clk_disable; 369 + } 370 + 371 + imx8qxp_adc_reset(adc); 372 + 373 + ret = iio_device_register(indio_dev); 374 + if (ret) { 375 + imx8qxp_adc_disable(adc); 376 + dev_err(dev, "Couldn't register the device.\n"); 377 + goto error_ipg_clk_disable; 378 + } 379 + 380 + pm_runtime_set_active(dev); 381 + pm_runtime_set_autosuspend_delay(dev, 50); 382 + pm_runtime_use_autosuspend(dev); 383 + pm_runtime_enable(dev); 384 + 385 + return 0; 386 + 387 + error_ipg_clk_disable: 388 + clk_disable_unprepare(adc->ipg_clk); 389 + error_adc_clk_disable: 390 + clk_disable_unprepare(adc->clk); 391 + error_regulator_disable: 392 + regulator_disable(adc->vref); 393 + 394 + return ret; 395 + } 396 + 397 + static int imx8qxp_adc_remove(struct platform_device *pdev) 398 + { 399 + struct iio_dev *indio_dev = platform_get_drvdata(pdev); 400 + struct imx8qxp_adc *adc = iio_priv(indio_dev); 401 + struct device *dev = adc->dev; 402 + 403 + pm_runtime_get_sync(dev); 404 + 405 + iio_device_unregister(indio_dev); 406 + 407 + imx8qxp_adc_disable(adc); 408 + 409 + clk_disable_unprepare(adc->clk); 410 + clk_disable_unprepare(adc->ipg_clk); 411 + regulator_disable(adc->vref); 412 + 413 + pm_runtime_disable(dev); 414 + pm_runtime_put_noidle(dev); 415 + 416 + return 0; 417 + } 418 + 419 + static __maybe_unused int imx8qxp_adc_runtime_suspend(struct device *dev) 420 + { 421 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 422 + struct imx8qxp_adc *adc = iio_priv(indio_dev); 423 + 424 + imx8qxp_adc_disable(adc); 425 + 426 + clk_disable_unprepare(adc->clk); 427 + clk_disable_unprepare(adc->ipg_clk); 428 + regulator_disable(adc->vref); 429 + 430 + return 0; 431 + } 432 + 433 + static __maybe_unused int imx8qxp_adc_runtime_resume(struct device *dev) 434 + { 435 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 436 + struct imx8qxp_adc *adc = iio_priv(indio_dev); 437 + int ret; 438 + 439 + ret = regulator_enable(adc->vref); 440 + if (ret) { 441 + dev_err(dev, "Can't enable adc reference top voltage, err = %d\n", ret); 442 + return ret; 443 + } 444 + 445 + ret = clk_prepare_enable(adc->clk); 446 + if (ret) { 447 + dev_err(dev, "Could not prepare or enable clock.\n"); 448 + goto err_disable_reg; 449 + } 450 + 451 + ret = clk_prepare_enable(adc->ipg_clk); 452 + if (ret) { 453 + dev_err(dev, "Could not prepare or enable clock.\n"); 454 + goto err_unprepare_clk; 455 + } 456 + 457 + imx8qxp_adc_reset(adc); 458 + 459 + return 0; 460 + 461 + err_unprepare_clk: 462 + clk_disable_unprepare(adc->clk); 463 + 464 + err_disable_reg: 465 + regulator_disable(adc->vref); 466 + 467 + return ret; 468 + } 469 + 470 + static const struct dev_pm_ops imx8qxp_adc_pm_ops = { 471 + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) 472 + SET_RUNTIME_PM_OPS(imx8qxp_adc_runtime_suspend, imx8qxp_adc_runtime_resume, NULL) 473 + }; 474 + 475 + static const struct of_device_id imx8qxp_adc_match[] = { 476 + { .compatible = "nxp,imx8qxp-adc", }, 477 + { /* sentinel */ } 478 + }; 479 + MODULE_DEVICE_TABLE(of, imx8qxp_adc_match); 480 + 481 + static struct platform_driver imx8qxp_adc_driver = { 482 + .probe = imx8qxp_adc_probe, 483 + .remove = imx8qxp_adc_remove, 484 + .driver = { 485 + .name = ADC_DRIVER_NAME, 486 + .of_match_table = imx8qxp_adc_match, 487 + .pm = &imx8qxp_adc_pm_ops, 488 + }, 489 + }; 490 + 491 + module_platform_driver(imx8qxp_adc_driver); 492 + 493 + MODULE_DESCRIPTION("i.MX8QuadXPlus ADC driver"); 494 + MODULE_LICENSE("GPL v2");
+2 -22
drivers/iio/adc/intel_mrfld_adc.c
··· 205 205 if (ret) 206 206 return ret; 207 207 208 - platform_set_drvdata(pdev, indio_dev); 209 - 210 208 indio_dev->name = pdev->name; 211 209 212 210 indio_dev->channels = mrfld_adc_channels; ··· 212 214 indio_dev->info = &mrfld_adc_iio_info; 213 215 indio_dev->modes = INDIO_DIRECT_MODE; 214 216 215 - ret = iio_map_array_register(indio_dev, iio_maps); 217 + ret = devm_iio_map_array_register(dev, indio_dev, iio_maps); 216 218 if (ret) 217 219 return ret; 218 220 219 - ret = devm_iio_device_register(dev, indio_dev); 220 - if (ret < 0) 221 - goto err_array_unregister; 222 - 223 - return 0; 224 - 225 - err_array_unregister: 226 - iio_map_array_unregister(indio_dev); 227 - return ret; 228 - } 229 - 230 - static int mrfld_adc_remove(struct platform_device *pdev) 231 - { 232 - struct iio_dev *indio_dev = platform_get_drvdata(pdev); 233 - 234 - iio_map_array_unregister(indio_dev); 235 - 236 - return 0; 221 + return devm_iio_device_register(dev, indio_dev); 237 222 } 238 223 239 224 static const struct platform_device_id mrfld_adc_id_table[] = { ··· 230 249 .name = "mrfld_bcove_adc", 231 250 }, 232 251 .probe = mrfld_adc_probe, 233 - .remove = mrfld_adc_remove, 234 252 .id_table = mrfld_adc_id_table, 235 253 }; 236 254 module_platform_driver(mrfld_adc_driver);
+5 -26
drivers/iio/adc/lp8788_adc.c
··· 163 163 { } 164 164 }; 165 165 166 - static int lp8788_iio_map_register(struct iio_dev *indio_dev, 166 + static int lp8788_iio_map_register(struct device *dev, 167 + struct iio_dev *indio_dev, 167 168 struct lp8788_platform_data *pdata, 168 169 struct lp8788_adc *adc) 169 170 { ··· 174 173 map = (!pdata || !pdata->adc_pdata) ? 175 174 lp8788_default_iio_maps : pdata->adc_pdata; 176 175 177 - ret = iio_map_array_register(indio_dev, map); 176 + ret = devm_iio_map_array_register(dev, indio_dev, map); 178 177 if (ret) { 179 178 dev_err(&indio_dev->dev, "iio map err: %d\n", ret); 180 179 return ret; ··· 197 196 198 197 adc = iio_priv(indio_dev); 199 198 adc->lp = lp; 200 - platform_set_drvdata(pdev, indio_dev); 201 199 202 - ret = lp8788_iio_map_register(indio_dev, lp->pdata, adc); 200 + ret = lp8788_iio_map_register(&pdev->dev, indio_dev, lp->pdata, adc); 203 201 if (ret) 204 202 return ret; 205 203 ··· 210 210 indio_dev->channels = lp8788_adc_channels; 211 211 indio_dev->num_channels = ARRAY_SIZE(lp8788_adc_channels); 212 212 213 - ret = iio_device_register(indio_dev); 214 - if (ret) { 215 - dev_err(&pdev->dev, "iio dev register err: %d\n", ret); 216 - goto err_iio_device; 217 - } 218 - 219 - return 0; 220 - 221 - err_iio_device: 222 - iio_map_array_unregister(indio_dev); 223 - return ret; 224 - } 225 - 226 - static int lp8788_adc_remove(struct platform_device *pdev) 227 - { 228 - struct iio_dev *indio_dev = platform_get_drvdata(pdev); 229 - 230 - iio_device_unregister(indio_dev); 231 - iio_map_array_unregister(indio_dev); 232 - 233 - return 0; 213 + return devm_iio_device_register(&pdev->dev, indio_dev); 234 214 } 235 215 236 216 static struct platform_driver lp8788_adc_driver = { 237 217 .probe = lp8788_adc_probe, 238 - .remove = lp8788_adc_remove, 239 218 .driver = { 240 219 .name = LP8788_DEV_ADC, 241 220 },
+205 -81
drivers/iio/adc/max1027.c
··· 60 60 #define MAX1027_NAVG_32 (0x03 << 2) 61 61 #define MAX1027_AVG_EN BIT(4) 62 62 63 + /* Device can achieve 300ksps so we assume a 3.33us conversion delay */ 64 + #define MAX1027_CONVERSION_UDELAY 4 65 + 63 66 enum max1027_id { 64 67 max1027, 65 68 max1029, ··· 175 172 MAX1X31_CHANNELS(12), 176 173 }; 177 174 175 + /* 176 + * These devices are able to scan from 0 to N, N being the highest voltage 177 + * channel requested by the user. The temperature can be included or not, 178 + * but cannot be retrieved alone. Based on the below 179 + * ->available_scan_masks, the core will select the most appropriate 180 + * ->active_scan_mask and the "minimum" number of channels will be 181 + * scanned and pushed to the buffers. 182 + * 183 + * For example, if the user wants channels 1, 4 and 5, all channels from 184 + * 0 to 5 will be scanned and pushed to the IIO buffers. The core will then 185 + * filter out the unneeded samples based on the ->active_scan_mask that has 186 + * been selected and only channels 1, 4 and 5 will be available to the user 187 + * in the shared buffer. 188 + */ 189 + #define MAX1X27_SCAN_MASK_TEMP BIT(0) 190 + 191 + #define MAX1X27_SCAN_MASKS(temp) \ 192 + GENMASK(1, 1 - (temp)), GENMASK(2, 1 - (temp)), \ 193 + GENMASK(3, 1 - (temp)), GENMASK(4, 1 - (temp)), \ 194 + GENMASK(5, 1 - (temp)), GENMASK(6, 1 - (temp)), \ 195 + GENMASK(7, 1 - (temp)), GENMASK(8, 1 - (temp)) 196 + 197 + #define MAX1X29_SCAN_MASKS(temp) \ 198 + MAX1X27_SCAN_MASKS(temp), \ 199 + GENMASK(9, 1 - (temp)), GENMASK(10, 1 - (temp)), \ 200 + GENMASK(11, 1 - (temp)), GENMASK(12, 1 - (temp)) 201 + 202 + #define MAX1X31_SCAN_MASKS(temp) \ 203 + MAX1X29_SCAN_MASKS(temp), \ 204 + GENMASK(13, 1 - (temp)), GENMASK(14, 1 - (temp)), \ 205 + GENMASK(15, 1 - (temp)), GENMASK(16, 1 - (temp)) 206 + 178 207 static const unsigned long max1027_available_scan_masks[] = { 179 - 0x000001ff, 208 + MAX1X27_SCAN_MASKS(0), 209 + MAX1X27_SCAN_MASKS(1), 180 210 0x00000000, 181 211 }; 182 212 183 213 static const unsigned long max1029_available_scan_masks[] = { 184 - 0x00001fff, 214 + MAX1X29_SCAN_MASKS(0), 215 + MAX1X29_SCAN_MASKS(1), 185 216 0x00000000, 186 217 }; 187 218 188 219 static const unsigned long max1031_available_scan_masks[] = { 189 - 0x0001ffff, 220 + MAX1X31_SCAN_MASKS(0), 221 + MAX1X31_SCAN_MASKS(1), 190 222 0x00000000, 191 223 }; 192 224 ··· 270 232 struct iio_trigger *trig; 271 233 __be16 *buffer; 272 234 struct mutex lock; 235 + struct completion complete; 273 236 274 237 u8 reg ____cacheline_aligned; 275 238 }; 239 + 240 + static int max1027_wait_eoc(struct iio_dev *indio_dev) 241 + { 242 + struct max1027_state *st = iio_priv(indio_dev); 243 + unsigned int conversion_time = MAX1027_CONVERSION_UDELAY; 244 + int ret; 245 + 246 + if (st->spi->irq) { 247 + ret = wait_for_completion_timeout(&st->complete, 248 + msecs_to_jiffies(1000)); 249 + reinit_completion(&st->complete); 250 + if (!ret) 251 + return -ETIMEDOUT; 252 + } else { 253 + if (indio_dev->active_scan_mask) 254 + conversion_time *= hweight32(*indio_dev->active_scan_mask); 255 + 256 + usleep_range(conversion_time, conversion_time * 2); 257 + } 258 + 259 + return 0; 260 + } 261 + 262 + /* Scan from chan 0 to the highest requested channel. Include temperature on demand. */ 263 + static int max1027_configure_chans_and_start(struct iio_dev *indio_dev) 264 + { 265 + struct max1027_state *st = iio_priv(indio_dev); 266 + 267 + st->reg = MAX1027_CONV_REG | MAX1027_SCAN_0_N; 268 + st->reg |= MAX1027_CHAN(fls(*indio_dev->active_scan_mask) - 2); 269 + if (*indio_dev->active_scan_mask & MAX1X27_SCAN_MASK_TEMP) 270 + st->reg |= MAX1027_TEMP; 271 + 272 + return spi_write(st->spi, &st->reg, 1); 273 + } 274 + 275 + static int max1027_enable_trigger(struct iio_dev *indio_dev, bool enable) 276 + { 277 + struct max1027_state *st = iio_priv(indio_dev); 278 + 279 + st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2; 280 + 281 + /* 282 + * Start acquisition on: 283 + * MODE0: external hardware trigger wired to the cnvst input pin 284 + * MODE2: conversion register write 285 + */ 286 + if (enable) 287 + st->reg |= MAX1027_CKS_MODE0; 288 + else 289 + st->reg |= MAX1027_CKS_MODE2; 290 + 291 + return spi_write(st->spi, &st->reg, 1); 292 + } 276 293 277 294 static int max1027_read_single_value(struct iio_dev *indio_dev, 278 295 struct iio_chan_spec const *chan, ··· 336 243 int ret; 337 244 struct max1027_state *st = iio_priv(indio_dev); 338 245 339 - if (iio_buffer_enabled(indio_dev)) { 340 - dev_warn(&indio_dev->dev, "trigger mode already enabled"); 341 - return -EBUSY; 342 - } 343 - 344 - /* Start acquisition on conversion register write */ 345 - st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2; 346 - ret = spi_write(st->spi, &st->reg, 1); 347 - if (ret < 0) { 348 - dev_err(&indio_dev->dev, 349 - "Failed to configure setup register\n"); 246 + ret = iio_device_claim_direct_mode(indio_dev); 247 + if (ret) 350 248 return ret; 351 - } 352 249 353 250 /* Configure conversion register with the requested chan */ 354 251 st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) | ··· 349 266 if (ret < 0) { 350 267 dev_err(&indio_dev->dev, 351 268 "Failed to configure conversion register\n"); 269 + iio_device_release_direct_mode(indio_dev); 352 270 return ret; 353 271 } 354 272 355 273 /* 356 274 * For an unknown reason, when we use the mode "10" (write 357 275 * conversion register), the interrupt doesn't occur every time. 358 - * So we just wait 1 ms. 276 + * So we just wait the maximum conversion time and deliver the value. 359 277 */ 360 - mdelay(1); 278 + ret = max1027_wait_eoc(indio_dev); 279 + if (ret) 280 + return ret; 361 281 362 282 /* Read result */ 363 283 ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2); 284 + 285 + iio_device_release_direct_mode(indio_dev); 286 + 364 287 if (ret < 0) 365 288 return ret; 366 289 ··· 416 327 } 417 328 418 329 static int max1027_debugfs_reg_access(struct iio_dev *indio_dev, 419 - unsigned reg, unsigned writeval, 420 - unsigned *readval) 330 + unsigned int reg, unsigned int writeval, 331 + unsigned int *readval) 421 332 { 422 333 struct max1027_state *st = iio_priv(indio_dev); 423 334 u8 *val = (u8 *)st->buffer; ··· 432 343 return spi_write(st->spi, val, 1); 433 344 } 434 345 435 - static int max1027_validate_trigger(struct iio_dev *indio_dev, 436 - struct iio_trigger *trig) 437 - { 438 - struct max1027_state *st = iio_priv(indio_dev); 439 - 440 - if (st->trig != trig) 441 - return -EINVAL; 442 - 443 - return 0; 444 - } 445 - 446 - static int max1027_set_trigger_state(struct iio_trigger *trig, bool state) 346 + static int max1027_set_cnvst_trigger_state(struct iio_trigger *trig, bool state) 447 347 { 448 348 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 449 - struct max1027_state *st = iio_priv(indio_dev); 450 349 int ret; 451 350 452 - if (state) { 453 - /* Start acquisition on cnvst */ 454 - st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 | 455 - MAX1027_REF_MODE2; 456 - ret = spi_write(st->spi, &st->reg, 1); 457 - if (ret < 0) 458 - return ret; 351 + /* 352 + * In order to disable the convst trigger, start acquisition on 353 + * conversion register write, which basically disables triggering 354 + * conversions upon cnvst changes and thus has the effect of disabling 355 + * the external hardware trigger. 356 + */ 357 + ret = max1027_enable_trigger(indio_dev, state); 358 + if (ret) 359 + return ret; 459 360 460 - /* Scan from 0 to max */ 461 - st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) | 462 - MAX1027_SCAN_N_M | MAX1027_TEMP; 463 - ret = spi_write(st->spi, &st->reg, 1); 464 - if (ret < 0) 465 - return ret; 466 - } else { 467 - /* Start acquisition on conversion register write */ 468 - st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2 | 469 - MAX1027_REF_MODE2; 470 - ret = spi_write(st->spi, &st->reg, 1); 471 - if (ret < 0) 361 + if (state) { 362 + ret = max1027_configure_chans_and_start(indio_dev); 363 + if (ret) 472 364 return ret; 473 365 } 474 366 475 367 return 0; 476 368 } 477 369 370 + static int max1027_read_scan(struct iio_dev *indio_dev) 371 + { 372 + struct max1027_state *st = iio_priv(indio_dev); 373 + unsigned int scanned_chans; 374 + int ret; 375 + 376 + scanned_chans = fls(*indio_dev->active_scan_mask) - 1; 377 + if (*indio_dev->active_scan_mask & MAX1X27_SCAN_MASK_TEMP) 378 + scanned_chans++; 379 + 380 + /* fill buffer with all channel */ 381 + ret = spi_read(st->spi, st->buffer, scanned_chans * 2); 382 + if (ret < 0) 383 + return ret; 384 + 385 + iio_push_to_buffers(indio_dev, st->buffer); 386 + 387 + return 0; 388 + } 389 + 390 + static irqreturn_t max1027_handler(int irq, void *private) 391 + { 392 + struct iio_dev *indio_dev = private; 393 + struct max1027_state *st = iio_priv(indio_dev); 394 + 395 + /* 396 + * If buffers are disabled (raw read) or when using external triggers, 397 + * we just need to unlock the waiters which will then handle the data. 398 + * 399 + * When using the internal trigger, we must hand-off the choice of the 400 + * handler to the core which will then lookup through the interrupt tree 401 + * for the right handler registered with iio_triggered_buffer_setup() 402 + * to execute, as this trigger might very well be used in conjunction 403 + * with another device. The core will then call the relevant handler to 404 + * perform the data processing step. 405 + */ 406 + if (!iio_buffer_enabled(indio_dev)) 407 + complete(&st->complete); 408 + else 409 + iio_trigger_poll(indio_dev->trig); 410 + 411 + return IRQ_HANDLED; 412 + } 413 + 478 414 static irqreturn_t max1027_trigger_handler(int irq, void *private) 479 415 { 480 416 struct iio_poll_func *pf = private; 481 417 struct iio_dev *indio_dev = pf->indio_dev; 482 - struct max1027_state *st = iio_priv(indio_dev); 418 + int ret; 483 419 484 - pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private); 420 + if (!iio_trigger_using_own(indio_dev)) { 421 + ret = max1027_configure_chans_and_start(indio_dev); 422 + if (ret) 423 + goto out; 485 424 486 - /* fill buffer with all channel */ 487 - spi_read(st->spi, st->buffer, indio_dev->masklength * 2); 425 + /* This is a threaded handler, it is fine to wait for an IRQ */ 426 + ret = max1027_wait_eoc(indio_dev); 427 + if (ret) 428 + goto out; 429 + } 488 430 489 - iio_push_to_buffers(indio_dev, st->buffer); 431 + ret = max1027_read_scan(indio_dev); 432 + out: 433 + if (ret) 434 + dev_err(&indio_dev->dev, 435 + "Cannot read scanned values (%d)\n", ret); 490 436 491 437 iio_trigger_notify_done(indio_dev->trig); 492 438 ··· 530 406 531 407 static const struct iio_trigger_ops max1027_trigger_ops = { 532 408 .validate_device = &iio_trigger_validate_own_device, 533 - .set_trigger_state = &max1027_set_trigger_state, 409 + .set_trigger_state = &max1027_set_cnvst_trigger_state, 534 410 }; 535 411 536 412 static const struct iio_info max1027_info = { 537 413 .read_raw = &max1027_read_raw, 538 - .validate_trigger = &max1027_validate_trigger, 539 414 .debugfs_reg_access = &max1027_debugfs_reg_access, 540 415 }; 541 416 ··· 544 421 struct iio_dev *indio_dev; 545 422 struct max1027_state *st; 546 423 547 - pr_debug("%s: probe(spi = 0x%p)\n", __func__, spi); 548 - 549 424 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 550 - if (indio_dev == NULL) { 425 + if (!indio_dev) { 551 426 pr_err("Can't allocate iio device\n"); 552 427 return -ENOMEM; 553 428 } ··· 555 434 st->info = &max1027_chip_info_tbl[spi_get_device_id(spi)->driver_data]; 556 435 557 436 mutex_init(&st->lock); 437 + init_completion(&st->complete); 558 438 559 439 indio_dev->name = spi_get_device_id(spi)->name; 560 440 indio_dev->info = &max1027_info; ··· 565 443 indio_dev->available_scan_masks = st->info->available_scan_masks; 566 444 567 445 st->buffer = devm_kmalloc_array(&indio_dev->dev, 568 - indio_dev->num_channels, 2, 569 - GFP_KERNEL); 570 - if (st->buffer == NULL) { 571 - dev_err(&indio_dev->dev, "Can't allocate buffer\n"); 446 + indio_dev->num_channels, 2, 447 + GFP_KERNEL); 448 + if (!st->buffer) 572 449 return -ENOMEM; 450 + 451 + /* Enable triggered buffers */ 452 + ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, 453 + &iio_pollfunc_store_time, 454 + &max1027_trigger_handler, 455 + NULL); 456 + if (ret < 0) { 457 + dev_err(&indio_dev->dev, "Failed to setup buffer\n"); 458 + return ret; 573 459 } 574 460 461 + /* If there is an EOC interrupt, register the cnvst hardware trigger */ 575 462 if (spi->irq) { 576 - ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, 577 - &iio_pollfunc_store_time, 578 - &max1027_trigger_handler, 579 - NULL); 580 - if (ret < 0) { 581 - dev_err(&indio_dev->dev, "Failed to setup buffer\n"); 582 - return ret; 583 - } 584 - 585 463 st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-trigger", 586 464 indio_dev->name); 587 - if (st->trig == NULL) { 465 + if (!st->trig) { 588 466 ret = -ENOMEM; 589 467 dev_err(&indio_dev->dev, 590 468 "Failed to allocate iio trigger\n"); ··· 601 479 return ret; 602 480 } 603 481 604 - ret = devm_request_threaded_irq(&spi->dev, spi->irq, 605 - iio_trigger_generic_data_rdy_poll, 606 - NULL, 607 - IRQF_TRIGGER_FALLING, 608 - spi->dev.driver->name, 609 - st->trig); 482 + ret = devm_request_irq(&spi->dev, spi->irq, max1027_handler, 483 + IRQF_TRIGGER_FALLING, 484 + spi->dev.driver->name, indio_dev); 610 485 if (ret < 0) { 611 486 dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n"); 612 487 return ret; ··· 625 506 dev_err(&indio_dev->dev, "Failed to configure averaging register\n"); 626 507 return ret; 627 508 } 509 + 510 + /* Assume conversion on register write for now */ 511 + ret = max1027_enable_trigger(indio_dev, false); 512 + if (ret) 513 + return ret; 628 514 629 515 return devm_iio_device_register(&spi->dev, indio_dev); 630 516 }
+30 -52
drivers/iio/adc/max1363.c
··· 1577 1577 }; 1578 1578 MODULE_DEVICE_TABLE(of, max1363_of_match); 1579 1579 1580 + static void max1363_reg_disable(void *reg) 1581 + { 1582 + regulator_disable(reg); 1583 + } 1584 + 1580 1585 static int max1363_probe(struct i2c_client *client, 1581 1586 const struct i2c_device_id *id) 1582 1587 { ··· 1595 1590 if (!indio_dev) 1596 1591 return -ENOMEM; 1597 1592 1598 - ret = iio_map_array_register(indio_dev, client->dev.platform_data); 1593 + ret = devm_iio_map_array_register(&client->dev, indio_dev, 1594 + client->dev.platform_data); 1599 1595 if (ret < 0) 1600 1596 return ret; 1601 1597 ··· 1604 1598 1605 1599 mutex_init(&st->lock); 1606 1600 st->reg = devm_regulator_get(&client->dev, "vcc"); 1607 - if (IS_ERR(st->reg)) { 1608 - ret = PTR_ERR(st->reg); 1609 - goto error_unregister_map; 1610 - } 1601 + if (IS_ERR(st->reg)) 1602 + return PTR_ERR(st->reg); 1611 1603 1612 1604 ret = regulator_enable(st->reg); 1613 1605 if (ret) 1614 - goto error_unregister_map; 1606 + return ret; 1615 1607 1616 - /* this is only used for device removal purposes */ 1617 - i2c_set_clientdata(client, indio_dev); 1608 + ret = devm_add_action_or_reset(&client->dev, max1363_reg_disable, st->reg); 1609 + if (ret) 1610 + return ret; 1618 1611 1619 1612 st->chip_info = device_get_match_data(&client->dev); 1620 1613 if (!st->chip_info) ··· 1627 1622 1628 1623 ret = regulator_enable(vref); 1629 1624 if (ret) 1630 - goto error_disable_reg; 1625 + return ret; 1626 + 1627 + ret = devm_add_action_or_reset(&client->dev, max1363_reg_disable, vref); 1628 + if (ret) 1629 + return ret; 1630 + 1631 1631 st->vref = vref; 1632 1632 vref_uv = regulator_get_voltage(vref); 1633 - if (vref_uv <= 0) { 1634 - ret = -EINVAL; 1635 - goto error_disable_reg; 1636 - } 1633 + if (vref_uv <= 0) 1634 + return -EINVAL; 1635 + 1637 1636 st->vref_uv = vref_uv; 1638 1637 } 1639 1638 ··· 1649 1640 st->send = max1363_smbus_send; 1650 1641 st->recv = max1363_smbus_recv; 1651 1642 } else { 1652 - ret = -EOPNOTSUPP; 1653 - goto error_disable_reg; 1643 + return -EOPNOTSUPP; 1654 1644 } 1655 1645 1656 1646 ret = max1363_alloc_scan_masks(indio_dev); 1657 1647 if (ret) 1658 - goto error_disable_reg; 1648 + return ret; 1659 1649 1660 1650 indio_dev->name = id->name; 1661 1651 indio_dev->channels = st->chip_info->channels; ··· 1663 1655 indio_dev->modes = INDIO_DIRECT_MODE; 1664 1656 ret = max1363_initial_setup(st); 1665 1657 if (ret < 0) 1666 - goto error_disable_reg; 1658 + return ret; 1667 1659 1668 - ret = iio_triggered_buffer_setup(indio_dev, NULL, 1669 - &max1363_trigger_handler, NULL); 1660 + ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, 1661 + &max1363_trigger_handler, NULL); 1670 1662 if (ret) 1671 - goto error_disable_reg; 1663 + return ret; 1672 1664 1673 1665 if (client->irq) { 1674 1666 ret = devm_request_threaded_irq(&client->dev, st->client->irq, ··· 1679 1671 indio_dev); 1680 1672 1681 1673 if (ret) 1682 - goto error_uninit_buffer; 1674 + return ret; 1683 1675 } 1684 1676 1685 - ret = iio_device_register(indio_dev); 1686 - if (ret < 0) 1687 - goto error_uninit_buffer; 1688 - 1689 - return 0; 1690 - 1691 - error_uninit_buffer: 1692 - iio_triggered_buffer_cleanup(indio_dev); 1693 - error_disable_reg: 1694 - if (st->vref) 1695 - regulator_disable(st->vref); 1696 - regulator_disable(st->reg); 1697 - error_unregister_map: 1698 - iio_map_array_unregister(indio_dev); 1699 - return ret; 1700 - } 1701 - 1702 - static int max1363_remove(struct i2c_client *client) 1703 - { 1704 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 1705 - struct max1363_state *st = iio_priv(indio_dev); 1706 - 1707 - iio_device_unregister(indio_dev); 1708 - iio_triggered_buffer_cleanup(indio_dev); 1709 - if (st->vref) 1710 - regulator_disable(st->vref); 1711 - regulator_disable(st->reg); 1712 - iio_map_array_unregister(indio_dev); 1713 - 1714 - return 0; 1677 + return devm_iio_device_register(&client->dev, indio_dev); 1715 1678 } 1716 1679 1717 1680 static const struct i2c_device_id max1363_id[] = { ··· 1735 1756 .of_match_table = max1363_of_match, 1736 1757 }, 1737 1758 .probe = max1363_probe, 1738 - .remove = max1363_remove, 1739 1759 .id_table = max1363_id, 1740 1760 }; 1741 1761 module_i2c_driver(max1363_driver);
+9 -41
drivers/iio/adc/nau7802.c
··· 428 428 429 429 st = iio_priv(indio_dev); 430 430 431 - i2c_set_clientdata(client, indio_dev); 432 - 433 431 indio_dev->name = dev_name(&client->dev); 434 432 indio_dev->modes = INDIO_DIRECT_MODE; 435 433 indio_dev->info = &nau7802_info; ··· 493 495 * will enable them back when we will need them.. 494 496 */ 495 497 if (client->irq) { 496 - ret = request_threaded_irq(client->irq, 497 - NULL, 498 - nau7802_eoc_trigger, 499 - IRQF_TRIGGER_HIGH | IRQF_ONESHOT | 500 - IRQF_NO_AUTOEN, 501 - client->dev.driver->name, 502 - indio_dev); 498 + ret = devm_request_threaded_irq(&client->dev, client->irq, 499 + NULL, 500 + nau7802_eoc_trigger, 501 + IRQF_TRIGGER_HIGH | IRQF_ONESHOT | 502 + IRQF_NO_AUTOEN, 503 + client->dev.driver->name, 504 + indio_dev); 503 505 if (ret) { 504 506 /* 505 507 * What may happen here is that our IRQ controller is ··· 524 526 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL2, 525 527 NAU7802_CTRL2_CRS(st->sample_rate)); 526 528 if (ret) 527 - goto error_free_irq; 529 + return ret; 528 530 } 529 531 530 532 /* Setup the ADC channels available on the board */ ··· 534 536 mutex_init(&st->lock); 535 537 mutex_init(&st->data_lock); 536 538 537 - ret = iio_device_register(indio_dev); 538 - if (ret < 0) { 539 - dev_err(&client->dev, "Couldn't register the device.\n"); 540 - goto error_device_register; 541 - } 542 - 543 - return 0; 544 - 545 - error_device_register: 546 - mutex_destroy(&st->lock); 547 - mutex_destroy(&st->data_lock); 548 - error_free_irq: 549 - if (client->irq) 550 - free_irq(client->irq, indio_dev); 551 - 552 - return ret; 553 - } 554 - 555 - static int nau7802_remove(struct i2c_client *client) 556 - { 557 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 558 - struct nau7802_state *st = iio_priv(indio_dev); 559 - 560 - iio_device_unregister(indio_dev); 561 - mutex_destroy(&st->lock); 562 - mutex_destroy(&st->data_lock); 563 - if (client->irq) 564 - free_irq(client->irq, indio_dev); 565 - 566 - return 0; 539 + return devm_iio_device_register(&client->dev, indio_dev); 567 540 } 568 541 569 542 static const struct i2c_device_id nau7802_i2c_id[] = { ··· 551 582 552 583 static struct i2c_driver nau7802_driver = { 553 584 .probe = nau7802_probe, 554 - .remove = nau7802_remove, 555 585 .id_table = nau7802_i2c_id, 556 586 .driver = { 557 587 .name = "nau7802",
+1 -12
drivers/iio/adc/rn5t618-adc.c
··· 197 197 { /* sentinel */ } 198 198 }; 199 199 200 - static void unregister_map(void *data) 201 - { 202 - struct iio_dev *iio_dev = (struct iio_dev *) data; 203 - 204 - iio_map_array_unregister(iio_dev); 205 - } 206 - 207 200 static int rn5t618_adc_probe(struct platform_device *pdev) 208 201 { 209 202 int ret; ··· 247 254 return ret; 248 255 } 249 256 250 - ret = iio_map_array_register(iio_dev, rn5t618_maps); 251 - if (ret < 0) 252 - return ret; 253 - 254 - ret = devm_add_action_or_reset(adc->dev, unregister_map, iio_dev); 257 + ret = devm_iio_map_array_register(adc->dev, iio_dev, rn5t618_maps); 255 258 if (ret < 0) 256 259 return ret; 257 260
+1 -3
drivers/iio/adc/rockchip_saradc.c
··· 319 319 struct rockchip_saradc *info = NULL; 320 320 struct device_node *np = pdev->dev.of_node; 321 321 struct iio_dev *indio_dev = NULL; 322 - struct resource *mem; 323 322 const struct of_device_id *match; 324 323 int ret; 325 324 int irq; ··· 347 348 return -EINVAL; 348 349 } 349 350 350 - mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 351 - info->regs = devm_ioremap_resource(&pdev->dev, mem); 351 + info->regs = devm_platform_ioremap_resource(pdev, 0); 352 352 if (IS_ERR(info->regs)) 353 353 return PTR_ERR(info->regs); 354 354
+6 -5
drivers/iio/adc/ti-adc108s102.c
··· 75 75 * rx_buf: |XX|R0|R1|R2|R3|R4|R5|R6|R7|tt|tt|tt|tt| 76 76 * 77 77 * tx_buf: 8 channel read commands, plus 1 dummy command 78 - * rx_buf: 1 dummy response, 8 channel responses, plus 64-bit timestamp 78 + * rx_buf: 1 dummy response, 8 channel responses 79 79 */ 80 - __be16 rx_buf[13] ____cacheline_aligned; 80 + __be16 rx_buf[9] ____cacheline_aligned; 81 81 __be16 tx_buf[9] ____cacheline_aligned; 82 82 }; 83 83 ··· 149 149 goto out_notify; 150 150 151 151 /* Skip the dummy response in the first slot */ 152 - iio_push_to_buffers_with_timestamp(indio_dev, 153 - (u8 *)&st->rx_buf[1], 154 - iio_get_time_ns(indio_dev)); 152 + iio_push_to_buffers_with_ts_unaligned(indio_dev, 153 + &st->rx_buf[1], 154 + st->ring_xfer.len - sizeof(st->rx_buf[1]), 155 + iio_get_time_ns(indio_dev)); 155 156 156 157 out_notify: 157 158 iio_trigger_notify_done(indio_dev->trig);
+8 -19
drivers/iio/adc/ti-ads8344.c
··· 133 133 .read_raw = ads8344_read_raw, 134 134 }; 135 135 136 + static void ads8344_reg_disable(void *data) 137 + { 138 + regulator_disable(data); 139 + } 140 + 136 141 static int ads8344_probe(struct spi_device *spi) 137 142 { 138 143 struct iio_dev *indio_dev; ··· 166 161 if (ret) 167 162 return ret; 168 163 169 - spi_set_drvdata(spi, indio_dev); 170 - 171 - ret = iio_device_register(indio_dev); 172 - if (ret) { 173 - regulator_disable(adc->reg); 164 + ret = devm_add_action_or_reset(&spi->dev, ads8344_reg_disable, adc->reg); 165 + if (ret) 174 166 return ret; 175 - } 176 167 177 - return 0; 178 - } 179 - 180 - static int ads8344_remove(struct spi_device *spi) 181 - { 182 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 183 - struct ads8344 *adc = iio_priv(indio_dev); 184 - 185 - iio_device_unregister(indio_dev); 186 - regulator_disable(adc->reg); 187 - 188 - return 0; 168 + return devm_iio_device_register(&spi->dev, indio_dev); 189 169 } 190 170 191 171 static const struct of_device_id ads8344_of_match[] = { ··· 185 195 .of_match_table = ads8344_of_match, 186 196 }, 187 197 .probe = ads8344_probe, 188 - .remove = ads8344_remove, 189 198 }; 190 199 module_spi_driver(ads8344_driver); 191 200
+3 -3
drivers/iio/adc/twl6030-gpadc.c
··· 900 900 901 901 ret = pdata->calibrate(gpadc); 902 902 if (ret < 0) { 903 - dev_err(&pdev->dev, "failed to read calibration registers\n"); 903 + dev_err(dev, "failed to read calibration registers\n"); 904 904 return ret; 905 905 } 906 906 ··· 914 914 915 915 ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK); 916 916 if (ret < 0) { 917 - dev_err(&pdev->dev, "failed to enable GPADC interrupt\n"); 917 + dev_err(dev, "failed to enable GPADC interrupt\n"); 918 918 return ret; 919 919 } 920 920 921 921 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS, 922 922 TWL6030_REG_TOGGLE1); 923 923 if (ret < 0) { 924 - dev_err(&pdev->dev, "failed to enable GPADC module\n"); 924 + dev_err(dev, "failed to enable GPADC module\n"); 925 925 return ret; 926 926 } 927 927
+6 -2
drivers/iio/buffer/industrialio-triggered-buffer.c
··· 19 19 * @indio_dev: IIO device structure 20 20 * @h: Function which will be used as pollfunc top half 21 21 * @thread: Function which will be used as pollfunc bottom half 22 + * @direction: Direction of the data stream (in/out). 22 23 * @setup_ops: Buffer setup functions to use for this device. 23 24 * If NULL the default setup functions for triggered 24 25 * buffers will be used. ··· 39 38 int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev, 40 39 irqreturn_t (*h)(int irq, void *p), 41 40 irqreturn_t (*thread)(int irq, void *p), 41 + enum iio_buffer_direction direction, 42 42 const struct iio_buffer_setup_ops *setup_ops, 43 43 const struct attribute **buffer_attrs) 44 44 { ··· 70 68 /* Flag that polled ring buffering is possible */ 71 69 indio_dev->modes |= INDIO_BUFFER_TRIGGERED; 72 70 71 + buffer->direction = direction; 73 72 buffer->attrs = buffer_attrs; 74 73 75 74 ret = iio_device_attach_buffer(indio_dev, buffer); ··· 108 105 struct iio_dev *indio_dev, 109 106 irqreturn_t (*h)(int irq, void *p), 110 107 irqreturn_t (*thread)(int irq, void *p), 108 + enum iio_buffer_direction direction, 111 109 const struct iio_buffer_setup_ops *ops, 112 110 const struct attribute **buffer_attrs) 113 111 { 114 112 int ret; 115 113 116 - ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, ops, 117 - buffer_attrs); 114 + ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, direction, 115 + ops, buffer_attrs); 118 116 if (ret) 119 117 return ret; 120 118
+50
drivers/iio/buffer/kfifo_buf.c
··· 138 138 kfree(kf); 139 139 } 140 140 141 + static size_t iio_kfifo_buf_space_available(struct iio_buffer *r) 142 + { 143 + struct iio_kfifo *kf = iio_to_kfifo(r); 144 + size_t avail; 145 + 146 + mutex_lock(&kf->user_lock); 147 + avail = kfifo_avail(&kf->kf); 148 + mutex_unlock(&kf->user_lock); 149 + 150 + return avail; 151 + } 152 + 153 + static int iio_kfifo_remove_from(struct iio_buffer *r, void *data) 154 + { 155 + int ret; 156 + struct iio_kfifo *kf = iio_to_kfifo(r); 157 + 158 + if (kfifo_size(&kf->kf) < 1) 159 + return -EBUSY; 160 + 161 + ret = kfifo_out(&kf->kf, data, 1); 162 + if (ret != 1) 163 + return -EBUSY; 164 + 165 + wake_up_interruptible_poll(&r->pollq, EPOLLOUT | EPOLLWRNORM); 166 + 167 + return 0; 168 + } 169 + 170 + static int iio_kfifo_write(struct iio_buffer *r, size_t n, 171 + const char __user *buf) 172 + { 173 + struct iio_kfifo *kf = iio_to_kfifo(r); 174 + int ret, copied; 175 + 176 + mutex_lock(&kf->user_lock); 177 + if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf)) 178 + ret = -EINVAL; 179 + else 180 + ret = kfifo_from_user(&kf->kf, buf, n, &copied); 181 + mutex_unlock(&kf->user_lock); 182 + if (ret) 183 + return ret; 184 + 185 + return copied; 186 + } 187 + 141 188 static const struct iio_buffer_access_funcs kfifo_access_funcs = { 142 189 .store_to = &iio_store_to_kfifo, 143 190 .read = &iio_read_kfifo, 144 191 .data_available = iio_kfifo_buf_data_available, 192 + .remove_from = &iio_kfifo_remove_from, 193 + .write = &iio_kfifo_write, 194 + .space_available = &iio_kfifo_buf_space_available, 145 195 .request_update = &iio_request_update_kfifo, 146 196 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, 147 197 .set_length = &iio_set_length_kfifo,
+24
drivers/iio/chemical/Kconfig
··· 118 118 To compile this driver as a module, choose M here: the module will 119 119 be called scd30_serial. 120 120 121 + config SCD4X 122 + tristate "SCD4X carbon dioxide sensor driver" 123 + select IIO_BUFFER 124 + select IIO_TRIGGERED_BUFFER 125 + depends on I2C 126 + select CRC8 127 + help 128 + Say Y here to build support for the Sensirion SCD4X sensor with carbon 129 + dioxide, relative humidity and temperature sensing capabilities. 130 + 131 + To compile this driver as a module, choose M here: the module will 132 + be called scd4x. 133 + 121 134 config SENSIRION_SGP30 122 135 tristate "Sensirion SGPxx gas sensors" 123 136 depends on I2C ··· 182 169 183 170 To compile this driver as a module, choose M here: the module will 184 171 be called sps30_serial. 172 + 173 + config SENSEAIR_SUNRISE_CO2 174 + tristate "Senseair Sunrise 006-0-0007 CO2 sensor" 175 + depends on I2C 176 + select REGMAP_I2C 177 + help 178 + Say yes here to build support for Senseair Sunrise 006-0-0007 CO2 179 + sensor. 180 + 181 + To compile this driver as a module, choose M here: the 182 + module will be called sunrise_co2. 185 183 186 184 config VZ89X 187 185 tristate "SGX Sensortech MiCS VZ89X VOC sensor"
+2
drivers/iio/chemical/Makefile
··· 15 15 obj-$(CONFIG_SCD30_CORE) += scd30_core.o 16 16 obj-$(CONFIG_SCD30_I2C) += scd30_i2c.o 17 17 obj-$(CONFIG_SCD30_SERIAL) += scd30_serial.o 18 + obj-$(CONFIG_SCD4X) += scd4x.o 19 + obj-$(CONFIG_SENSEAIR_SUNRISE_CO2) += sunrise_co2.o 18 20 obj-$(CONFIG_SENSIRION_SGP30) += sgp30.o 19 21 obj-$(CONFIG_SENSIRION_SGP40) += sgp40.o 20 22 obj-$(CONFIG_SPS30) += sps30.o
+691
drivers/iio/chemical/scd4x.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Sensirion SCD4X carbon dioxide sensor i2c driver 4 + * 5 + * Copyright (C) 2021 Protonic Holland 6 + * Author: Roan van Dijk <roan@protonic.nl> 7 + * 8 + * I2C slave address: 0x62 9 + * 10 + * Datasheets: 11 + * https://www.sensirion.com/file/datasheet_scd4x 12 + */ 13 + 14 + #include <asm/unaligned.h> 15 + #include <linux/crc8.h> 16 + #include <linux/delay.h> 17 + #include <linux/device.h> 18 + #include <linux/i2c.h> 19 + #include <linux/iio/buffer.h> 20 + #include <linux/iio/iio.h> 21 + #include <linux/iio/sysfs.h> 22 + #include <linux/iio/trigger.h> 23 + #include <linux/iio/trigger_consumer.h> 24 + #include <linux/iio/triggered_buffer.h> 25 + #include <linux/iio/types.h> 26 + #include <linux/kernel.h> 27 + #include <linux/mutex.h> 28 + #include <linux/string.h> 29 + #include <linux/sysfs.h> 30 + #include <linux/types.h> 31 + 32 + #define SCD4X_CRC8_POLYNOMIAL 0x31 33 + #define SCD4X_TIMEOUT_ERR 1000 34 + #define SCD4X_READ_BUF_SIZE 9 35 + #define SCD4X_COMMAND_BUF_SIZE 2 36 + #define SCD4X_WRITE_BUF_SIZE 5 37 + #define SCD4X_FRC_MIN_PPM 0 38 + #define SCD4X_FRC_MAX_PPM 2000 39 + #define SCD4X_READY_MASK 0x01 40 + 41 + /*Commands SCD4X*/ 42 + enum scd4x_cmd { 43 + CMD_START_MEAS = 0x21b1, 44 + CMD_READ_MEAS = 0xec05, 45 + CMD_STOP_MEAS = 0x3f86, 46 + CMD_SET_TEMP_OFFSET = 0x241d, 47 + CMD_GET_TEMP_OFFSET = 0x2318, 48 + CMD_FRC = 0x362f, 49 + CMD_SET_ASC = 0x2416, 50 + CMD_GET_ASC = 0x2313, 51 + CMD_GET_DATA_READY = 0xe4b8, 52 + }; 53 + 54 + enum scd4x_channel_idx { 55 + SCD4X_CO2, 56 + SCD4X_TEMP, 57 + SCD4X_HR, 58 + }; 59 + 60 + struct scd4x_state { 61 + struct i2c_client *client; 62 + /* maintain access to device, to prevent concurrent reads/writes */ 63 + struct mutex lock; 64 + struct regulator *vdd; 65 + }; 66 + 67 + DECLARE_CRC8_TABLE(scd4x_crc8_table); 68 + 69 + static int scd4x_i2c_xfer(struct scd4x_state *state, char *txbuf, int txsize, 70 + char *rxbuf, int rxsize) 71 + { 72 + struct i2c_client *client = state->client; 73 + int ret; 74 + 75 + ret = i2c_master_send(client, txbuf, txsize); 76 + 77 + if (ret < 0) 78 + return ret; 79 + if (ret != txsize) 80 + return -EIO; 81 + 82 + if (rxsize == 0) 83 + return 0; 84 + 85 + ret = i2c_master_recv(client, rxbuf, rxsize); 86 + if (ret < 0) 87 + return ret; 88 + if (ret != rxsize) 89 + return -EIO; 90 + 91 + return 0; 92 + } 93 + 94 + static int scd4x_send_command(struct scd4x_state *state, enum scd4x_cmd cmd) 95 + { 96 + char buf[SCD4X_COMMAND_BUF_SIZE]; 97 + int ret; 98 + 99 + /* 100 + * Measurement needs to be stopped before sending commands. 101 + * Except stop and start command. 102 + */ 103 + if ((cmd != CMD_STOP_MEAS) && (cmd != CMD_START_MEAS)) { 104 + 105 + ret = scd4x_send_command(state, CMD_STOP_MEAS); 106 + if (ret) 107 + return ret; 108 + 109 + /* execution time for stopping measurement */ 110 + msleep_interruptible(500); 111 + } 112 + 113 + put_unaligned_be16(cmd, buf); 114 + ret = scd4x_i2c_xfer(state, buf, 2, buf, 0); 115 + if (ret) 116 + return ret; 117 + 118 + if ((cmd != CMD_STOP_MEAS) && (cmd != CMD_START_MEAS)) { 119 + ret = scd4x_send_command(state, CMD_START_MEAS); 120 + if (ret) 121 + return ret; 122 + } 123 + 124 + return 0; 125 + } 126 + 127 + static int scd4x_read(struct scd4x_state *state, enum scd4x_cmd cmd, 128 + void *response, int response_sz) 129 + { 130 + struct i2c_client *client = state->client; 131 + char buf[SCD4X_READ_BUF_SIZE]; 132 + char *rsp = response; 133 + int i, ret; 134 + char crc; 135 + 136 + /* 137 + * Measurement needs to be stopped before sending commands. 138 + * Except for reading measurement and data ready command. 139 + */ 140 + if ((cmd != CMD_GET_DATA_READY) && (cmd != CMD_READ_MEAS)) { 141 + ret = scd4x_send_command(state, CMD_STOP_MEAS); 142 + if (ret) 143 + return ret; 144 + 145 + /* execution time for stopping measurement */ 146 + msleep_interruptible(500); 147 + } 148 + 149 + /* CRC byte for every 2 bytes of data */ 150 + response_sz += response_sz / 2; 151 + 152 + put_unaligned_be16(cmd, buf); 153 + ret = scd4x_i2c_xfer(state, buf, 2, buf, response_sz); 154 + if (ret) 155 + return ret; 156 + 157 + for (i = 0; i < response_sz; i += 3) { 158 + crc = crc8(scd4x_crc8_table, buf + i, 2, CRC8_INIT_VALUE); 159 + if (crc != buf[i + 2]) { 160 + dev_err(&client->dev, "CRC error\n"); 161 + return -EIO; 162 + } 163 + 164 + *rsp++ = buf[i]; 165 + *rsp++ = buf[i + 1]; 166 + } 167 + 168 + /* start measurement */ 169 + if ((cmd != CMD_GET_DATA_READY) && (cmd != CMD_READ_MEAS)) { 170 + ret = scd4x_send_command(state, CMD_START_MEAS); 171 + if (ret) 172 + return ret; 173 + } 174 + 175 + return 0; 176 + } 177 + 178 + static int scd4x_write(struct scd4x_state *state, enum scd4x_cmd cmd, uint16_t arg) 179 + { 180 + char buf[SCD4X_WRITE_BUF_SIZE]; 181 + int ret; 182 + char crc; 183 + 184 + put_unaligned_be16(cmd, buf); 185 + put_unaligned_be16(arg, buf + 2); 186 + 187 + crc = crc8(scd4x_crc8_table, buf + 2, 2, CRC8_INIT_VALUE); 188 + buf[4] = crc; 189 + 190 + /* measurement needs to be stopped before sending commands */ 191 + ret = scd4x_send_command(state, CMD_STOP_MEAS); 192 + if (ret) 193 + return ret; 194 + 195 + /* execution time */ 196 + msleep_interruptible(500); 197 + 198 + ret = scd4x_i2c_xfer(state, buf, SCD4X_WRITE_BUF_SIZE, buf, 0); 199 + if (ret) 200 + return ret; 201 + 202 + /* start measurement, except for forced calibration command */ 203 + if (cmd != CMD_FRC) { 204 + ret = scd4x_send_command(state, CMD_START_MEAS); 205 + if (ret) 206 + return ret; 207 + } 208 + 209 + return 0; 210 + } 211 + 212 + static int scd4x_write_and_fetch(struct scd4x_state *state, enum scd4x_cmd cmd, 213 + uint16_t arg, void *response, int response_sz) 214 + { 215 + struct i2c_client *client = state->client; 216 + char buf[SCD4X_READ_BUF_SIZE]; 217 + char *rsp = response; 218 + int i, ret; 219 + char crc; 220 + 221 + ret = scd4x_write(state, CMD_FRC, arg); 222 + if (ret) 223 + goto err; 224 + 225 + /* execution time */ 226 + msleep_interruptible(400); 227 + 228 + /* CRC byte for every 2 bytes of data */ 229 + response_sz += response_sz / 2; 230 + 231 + ret = i2c_master_recv(client, buf, response_sz); 232 + if (ret < 0) 233 + goto err; 234 + if (ret != response_sz) { 235 + ret = -EIO; 236 + goto err; 237 + } 238 + 239 + for (i = 0; i < response_sz; i += 3) { 240 + crc = crc8(scd4x_crc8_table, buf + i, 2, CRC8_INIT_VALUE); 241 + if (crc != buf[i + 2]) { 242 + dev_err(&client->dev, "CRC error\n"); 243 + ret = -EIO; 244 + goto err; 245 + } 246 + 247 + *rsp++ = buf[i]; 248 + *rsp++ = buf[i + 1]; 249 + } 250 + 251 + return scd4x_send_command(state, CMD_START_MEAS); 252 + 253 + err: 254 + /* 255 + * on error try to start the measurement, 256 + * puts sensor back into continuous measurement 257 + */ 258 + scd4x_send_command(state, CMD_START_MEAS); 259 + 260 + return ret; 261 + } 262 + 263 + static int scd4x_read_meas(struct scd4x_state *state, uint16_t *meas) 264 + { 265 + int i, ret; 266 + __be16 buf[3]; 267 + 268 + ret = scd4x_read(state, CMD_READ_MEAS, buf, sizeof(buf)); 269 + if (ret) 270 + return ret; 271 + 272 + for (i = 0; i < ARRAY_SIZE(buf); i++) 273 + meas[i] = be16_to_cpu(buf[i]); 274 + 275 + return 0; 276 + } 277 + 278 + static int scd4x_wait_meas_poll(struct scd4x_state *state) 279 + { 280 + struct i2c_client *client = state->client; 281 + int tries = 6; 282 + int ret; 283 + 284 + do { 285 + __be16 bval; 286 + uint16_t val; 287 + 288 + ret = scd4x_read(state, CMD_GET_DATA_READY, &bval, sizeof(bval)); 289 + if (ret) 290 + return -EIO; 291 + val = be16_to_cpu(bval); 292 + 293 + /* new measurement available */ 294 + if (val & 0x7FF) 295 + return 0; 296 + 297 + msleep_interruptible(1000); 298 + } while (--tries); 299 + 300 + /* try to start sensor on timeout */ 301 + ret = scd4x_send_command(state, CMD_START_MEAS); 302 + if (ret) 303 + dev_err(&client->dev, "failed to start measurement: %d\n", ret); 304 + 305 + return -ETIMEDOUT; 306 + } 307 + 308 + static int scd4x_read_poll(struct scd4x_state *state, uint16_t *buf) 309 + { 310 + int ret; 311 + 312 + ret = scd4x_wait_meas_poll(state); 313 + if (ret) 314 + return ret; 315 + 316 + return scd4x_read_meas(state, buf); 317 + } 318 + 319 + static int scd4x_read_channel(struct scd4x_state *state, int chan) 320 + { 321 + int ret; 322 + uint16_t buf[3]; 323 + 324 + ret = scd4x_read_poll(state, buf); 325 + if (ret) 326 + return ret; 327 + 328 + return buf[chan]; 329 + } 330 + 331 + static int scd4x_read_raw(struct iio_dev *indio_dev, 332 + struct iio_chan_spec const *chan, int *val, 333 + int *val2, long mask) 334 + { 335 + struct scd4x_state *state = iio_priv(indio_dev); 336 + int ret; 337 + __be16 tmp; 338 + 339 + switch (mask) { 340 + case IIO_CHAN_INFO_RAW: 341 + ret = iio_device_claim_direct_mode(indio_dev); 342 + if (ret) 343 + return ret; 344 + 345 + mutex_lock(&state->lock); 346 + ret = scd4x_read_channel(state, chan->address); 347 + mutex_unlock(&state->lock); 348 + 349 + iio_device_release_direct_mode(indio_dev); 350 + if (ret < 0) 351 + return ret; 352 + 353 + *val = ret; 354 + return IIO_VAL_INT; 355 + case IIO_CHAN_INFO_SCALE: 356 + if (chan->type == IIO_TEMP) { 357 + *val = 175000; 358 + *val2 = 65536; 359 + return IIO_VAL_FRACTIONAL; 360 + } else if (chan->type == IIO_HUMIDITYRELATIVE) { 361 + *val = 100000; 362 + *val2 = 65536; 363 + return IIO_VAL_FRACTIONAL; 364 + } 365 + return -EINVAL; 366 + case IIO_CHAN_INFO_OFFSET: 367 + *val = -16852; 368 + *val2 = 114286; 369 + return IIO_VAL_INT_PLUS_MICRO; 370 + case IIO_CHAN_INFO_CALIBBIAS: 371 + mutex_lock(&state->lock); 372 + ret = scd4x_read(state, CMD_GET_TEMP_OFFSET, &tmp, sizeof(tmp)); 373 + mutex_unlock(&state->lock); 374 + if (ret) 375 + return ret; 376 + 377 + *val = be16_to_cpu(tmp); 378 + 379 + return IIO_VAL_INT; 380 + default: 381 + return -EINVAL; 382 + } 383 + } 384 + 385 + static int scd4x_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, 386 + int val, int val2, long mask) 387 + { 388 + struct scd4x_state *state = iio_priv(indio_dev); 389 + int ret = 0; 390 + 391 + switch (mask) { 392 + case IIO_CHAN_INFO_CALIBBIAS: 393 + mutex_lock(&state->lock); 394 + ret = scd4x_write(state, CMD_SET_TEMP_OFFSET, val); 395 + mutex_unlock(&state->lock); 396 + 397 + return ret; 398 + default: 399 + return -EINVAL; 400 + } 401 + } 402 + 403 + static ssize_t calibration_auto_enable_show(struct device *dev, 404 + struct device_attribute *attr, char *buf) 405 + { 406 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 407 + struct scd4x_state *state = iio_priv(indio_dev); 408 + int ret; 409 + __be16 bval; 410 + u16 val; 411 + 412 + mutex_lock(&state->lock); 413 + ret = scd4x_read(state, CMD_GET_ASC, &bval, sizeof(bval)); 414 + mutex_unlock(&state->lock); 415 + if (ret) { 416 + dev_err(dev, "failed to read automatic calibration"); 417 + return ret; 418 + } 419 + 420 + val = (be16_to_cpu(bval) & SCD4X_READY_MASK) ? 1 : 0; 421 + 422 + return sprintf(buf, "%d\n", val); 423 + } 424 + 425 + static ssize_t calibration_auto_enable_store(struct device *dev, 426 + struct device_attribute *attr, 427 + const char *buf, size_t len) 428 + { 429 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 430 + struct scd4x_state *state = iio_priv(indio_dev); 431 + bool val; 432 + int ret; 433 + uint16_t value; 434 + 435 + ret = kstrtobool(buf, &val); 436 + if (ret) 437 + return ret; 438 + 439 + value = val; 440 + 441 + mutex_lock(&state->lock); 442 + ret = scd4x_write(state, CMD_SET_ASC, value); 443 + mutex_unlock(&state->lock); 444 + if (ret) 445 + dev_err(dev, "failed to set automatic calibration"); 446 + 447 + return ret ?: len; 448 + } 449 + 450 + static ssize_t calibration_forced_value_store(struct device *dev, 451 + struct device_attribute *attr, 452 + const char *buf, size_t len) 453 + { 454 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 455 + struct scd4x_state *state = iio_priv(indio_dev); 456 + uint16_t val, arg; 457 + int ret; 458 + 459 + ret = kstrtou16(buf, 0, &arg); 460 + if (ret) 461 + return ret; 462 + 463 + if (arg < SCD4X_FRC_MIN_PPM || arg > SCD4X_FRC_MAX_PPM) 464 + return -EINVAL; 465 + 466 + mutex_lock(&state->lock); 467 + ret = scd4x_write_and_fetch(state, CMD_FRC, arg, &val, sizeof(val)); 468 + mutex_unlock(&state->lock); 469 + 470 + if (val == 0xff) { 471 + dev_err(dev, "forced calibration has failed"); 472 + return -EINVAL; 473 + } 474 + 475 + return ret ?: len; 476 + } 477 + 478 + static IIO_DEVICE_ATTR_RW(calibration_auto_enable, 0); 479 + static IIO_DEVICE_ATTR_WO(calibration_forced_value, 0); 480 + 481 + static IIO_CONST_ATTR(calibration_forced_value_available, 482 + __stringify([SCD4X_FRC_MIN_PPM 1 SCD4X_FRC_MAX_PPM])); 483 + 484 + static struct attribute *scd4x_attrs[] = { 485 + &iio_dev_attr_calibration_auto_enable.dev_attr.attr, 486 + &iio_dev_attr_calibration_forced_value.dev_attr.attr, 487 + &iio_const_attr_calibration_forced_value_available.dev_attr.attr, 488 + NULL 489 + }; 490 + 491 + static const struct attribute_group scd4x_attr_group = { 492 + .attrs = scd4x_attrs, 493 + }; 494 + 495 + static const struct iio_info scd4x_info = { 496 + .attrs = &scd4x_attr_group, 497 + .read_raw = scd4x_read_raw, 498 + .write_raw = scd4x_write_raw, 499 + }; 500 + 501 + static const struct iio_chan_spec scd4x_channels[] = { 502 + { 503 + .type = IIO_CONCENTRATION, 504 + .channel2 = IIO_MOD_CO2, 505 + .modified = 1, 506 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 507 + .address = SCD4X_CO2, 508 + .scan_index = SCD4X_CO2, 509 + .scan_type = { 510 + .sign = 'u', 511 + .realbits = 16, 512 + .storagebits = 16, 513 + .endianness = IIO_BE, 514 + }, 515 + }, 516 + { 517 + .type = IIO_TEMP, 518 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 519 + BIT(IIO_CHAN_INFO_SCALE) | 520 + BIT(IIO_CHAN_INFO_OFFSET) | 521 + BIT(IIO_CHAN_INFO_CALIBBIAS), 522 + .address = SCD4X_TEMP, 523 + .scan_index = SCD4X_TEMP, 524 + .scan_type = { 525 + .sign = 'u', 526 + .realbits = 16, 527 + .storagebits = 16, 528 + .endianness = IIO_BE, 529 + }, 530 + }, 531 + { 532 + .type = IIO_HUMIDITYRELATIVE, 533 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 534 + BIT(IIO_CHAN_INFO_SCALE), 535 + .address = SCD4X_HR, 536 + .scan_index = SCD4X_HR, 537 + .scan_type = { 538 + .sign = 'u', 539 + .realbits = 16, 540 + .storagebits = 16, 541 + .endianness = IIO_BE, 542 + }, 543 + }, 544 + }; 545 + 546 + static int __maybe_unused scd4x_suspend(struct device *dev) 547 + { 548 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 549 + struct scd4x_state *state = iio_priv(indio_dev); 550 + int ret; 551 + 552 + ret = scd4x_send_command(state, CMD_STOP_MEAS); 553 + if (ret) 554 + return ret; 555 + 556 + return regulator_disable(state->vdd); 557 + } 558 + 559 + static int __maybe_unused scd4x_resume(struct device *dev) 560 + { 561 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 562 + struct scd4x_state *state = iio_priv(indio_dev); 563 + int ret; 564 + 565 + ret = regulator_enable(state->vdd); 566 + if (ret) 567 + return ret; 568 + 569 + return scd4x_send_command(state, CMD_START_MEAS); 570 + } 571 + 572 + static __maybe_unused SIMPLE_DEV_PM_OPS(scd4x_pm_ops, scd4x_suspend, scd4x_resume); 573 + 574 + static void scd4x_stop_meas(void *state) 575 + { 576 + scd4x_send_command(state, CMD_STOP_MEAS); 577 + } 578 + 579 + static void scd4x_disable_regulator(void *data) 580 + { 581 + struct scd4x_state *state = data; 582 + 583 + regulator_disable(state->vdd); 584 + } 585 + 586 + static irqreturn_t scd4x_trigger_handler(int irq, void *p) 587 + { 588 + struct iio_poll_func *pf = p; 589 + struct iio_dev *indio_dev = pf->indio_dev; 590 + struct scd4x_state *state = iio_priv(indio_dev); 591 + struct { 592 + uint16_t data[3]; 593 + int64_t ts __aligned(8); 594 + } scan; 595 + int ret; 596 + 597 + memset(&scan, 0, sizeof(scan)); 598 + mutex_lock(&state->lock); 599 + ret = scd4x_read_poll(state, scan.data); 600 + mutex_unlock(&state->lock); 601 + if (ret) 602 + goto out; 603 + 604 + iio_push_to_buffers_with_timestamp(indio_dev, &scan, iio_get_time_ns(indio_dev)); 605 + out: 606 + iio_trigger_notify_done(indio_dev->trig); 607 + return IRQ_HANDLED; 608 + } 609 + 610 + static int scd4x_probe(struct i2c_client *client, const struct i2c_device_id *id) 611 + { 612 + static const unsigned long scd4x_scan_masks[] = { 0x07, 0x00 }; 613 + struct device *dev = &client->dev; 614 + struct iio_dev *indio_dev; 615 + struct scd4x_state *state; 616 + int ret; 617 + 618 + indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); 619 + if (!indio_dev) 620 + return -ENOMEM; 621 + 622 + state = iio_priv(indio_dev); 623 + mutex_init(&state->lock); 624 + state->client = client; 625 + crc8_populate_msb(scd4x_crc8_table, SCD4X_CRC8_POLYNOMIAL); 626 + 627 + indio_dev->info = &scd4x_info; 628 + indio_dev->name = client->name; 629 + indio_dev->channels = scd4x_channels; 630 + indio_dev->num_channels = ARRAY_SIZE(scd4x_channels); 631 + indio_dev->modes = INDIO_DIRECT_MODE; 632 + indio_dev->available_scan_masks = scd4x_scan_masks; 633 + 634 + state->vdd = devm_regulator_get(dev, "vdd"); 635 + if (IS_ERR(state->vdd)) 636 + return dev_err_probe(dev, PTR_ERR(state->vdd), "failed to get regulator\n"); 637 + 638 + ret = regulator_enable(state->vdd); 639 + if (ret) 640 + return ret; 641 + 642 + ret = devm_add_action_or_reset(dev, scd4x_disable_regulator, state); 643 + if (ret) 644 + return ret; 645 + 646 + ret = scd4x_send_command(state, CMD_STOP_MEAS); 647 + if (ret) { 648 + dev_err(dev, "failed to stop measurement: %d\n", ret); 649 + return ret; 650 + } 651 + 652 + /* execution time */ 653 + msleep_interruptible(500); 654 + 655 + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, scd4x_trigger_handler, NULL); 656 + if (ret) 657 + return ret; 658 + 659 + ret = scd4x_send_command(state, CMD_START_MEAS); 660 + if (ret) { 661 + dev_err(dev, "failed to start measurement: %d\n", ret); 662 + return ret; 663 + } 664 + 665 + ret = devm_add_action_or_reset(dev, scd4x_stop_meas, state); 666 + if (ret) 667 + return ret; 668 + 669 + return devm_iio_device_register(dev, indio_dev); 670 + } 671 + 672 + static const struct of_device_id scd4x_dt_ids[] = { 673 + { .compatible = "sensirion,scd40" }, 674 + { .compatible = "sensirion,scd41" }, 675 + { } 676 + }; 677 + MODULE_DEVICE_TABLE(of, scd4x_dt_ids); 678 + 679 + static struct i2c_driver scd4x_i2c_driver = { 680 + .driver = { 681 + .name = KBUILD_MODNAME, 682 + .of_match_table = scd4x_dt_ids, 683 + .pm = &scd4x_pm_ops 684 + }, 685 + .probe = scd4x_probe, 686 + }; 687 + module_i2c_driver(scd4x_i2c_driver); 688 + 689 + MODULE_AUTHOR("Roan van Dijk <roan@protonic.nl>"); 690 + MODULE_DESCRIPTION("Sensirion SCD4X carbon dioxide sensor core driver"); 691 + MODULE_LICENSE("GPL v2");
+537
drivers/iio/chemical/sunrise_co2.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Senseair Sunrise 006-0-0007 CO2 sensor driver. 4 + * 5 + * Copyright (C) 2021 Jacopo Mondi 6 + * 7 + * List of features not yet supported by the driver: 8 + * - controllable EN pin 9 + * - single-shot operations using the nDRY pin. 10 + * - ABC/target calibration 11 + */ 12 + 13 + #include <linux/bitops.h> 14 + #include <linux/i2c.h> 15 + #include <linux/kernel.h> 16 + #include <linux/mod_devicetable.h> 17 + #include <linux/module.h> 18 + #include <linux/mutex.h> 19 + #include <linux/regmap.h> 20 + #include <linux/time64.h> 21 + 22 + #include <linux/iio/iio.h> 23 + 24 + #define DRIVER_NAME "sunrise_co2" 25 + 26 + #define SUNRISE_ERROR_STATUS_REG 0x00 27 + #define SUNRISE_CO2_FILTERED_COMP_REG 0x06 28 + #define SUNRISE_CHIP_TEMPERATURE_REG 0x08 29 + #define SUNRISE_CALIBRATION_STATUS_REG 0x81 30 + #define SUNRISE_CALIBRATION_COMMAND_REG 0x82 31 + #define SUNRISE_CALIBRATION_FACTORY_CMD 0x7c02 32 + #define SUNRISE_CALIBRATION_BACKGROUND_CMD 0x7c06 33 + /* 34 + * The calibration timeout is not characterized in the datasheet. 35 + * Use 30 seconds as a reasonable upper limit. 36 + */ 37 + #define SUNRISE_CALIBRATION_TIMEOUT_US (30 * USEC_PER_SEC) 38 + 39 + struct sunrise_dev { 40 + struct i2c_client *client; 41 + struct regmap *regmap; 42 + /* Protects access to IIO attributes. */ 43 + struct mutex lock; 44 + bool ignore_nak; 45 + }; 46 + 47 + /* Custom regmap read/write operations: perform unlocked access to the i2c bus. */ 48 + 49 + static int sunrise_regmap_read(void *context, const void *reg_buf, 50 + size_t reg_size, void *val_buf, size_t val_size) 51 + { 52 + struct i2c_client *client = context; 53 + struct sunrise_dev *sunrise = i2c_get_clientdata(client); 54 + union i2c_smbus_data data; 55 + int ret; 56 + 57 + if (reg_size != 1 || !val_size) 58 + return -EINVAL; 59 + 60 + memset(&data, 0, sizeof(data)); 61 + data.block[0] = val_size; 62 + 63 + /* 64 + * Wake up sensor by sending sensor address: START, sensor address, 65 + * STOP. Sensor will not ACK this byte. 66 + * 67 + * The chip enters a low power state after 15ms without 68 + * communications or after a complete read/write sequence. 69 + */ 70 + __i2c_smbus_xfer(client->adapter, client->addr, 71 + sunrise->ignore_nak ? I2C_M_IGNORE_NAK : 0, 72 + I2C_SMBUS_WRITE, 0, I2C_SMBUS_BYTE_DATA, &data); 73 + 74 + usleep_range(500, 1500); 75 + 76 + ret = __i2c_smbus_xfer(client->adapter, client->addr, client->flags, 77 + I2C_SMBUS_READ, ((u8 *)reg_buf)[0], 78 + I2C_SMBUS_I2C_BLOCK_DATA, &data); 79 + if (ret < 0) 80 + return ret; 81 + 82 + memcpy(val_buf, &data.block[1], data.block[0]); 83 + 84 + return 0; 85 + } 86 + 87 + static int sunrise_regmap_write(void *context, const void *val_buf, size_t count) 88 + { 89 + struct i2c_client *client = context; 90 + struct sunrise_dev *sunrise = i2c_get_clientdata(client); 91 + union i2c_smbus_data data; 92 + 93 + /* Discard reg address from values count. */ 94 + if (!count) 95 + return -EINVAL; 96 + count--; 97 + 98 + memset(&data, 0, sizeof(data)); 99 + data.block[0] = count; 100 + memcpy(&data.block[1], (u8 *)val_buf + 1, count); 101 + 102 + __i2c_smbus_xfer(client->adapter, client->addr, 103 + sunrise->ignore_nak ? I2C_M_IGNORE_NAK : 0, 104 + I2C_SMBUS_WRITE, 0, I2C_SMBUS_BYTE_DATA, &data); 105 + 106 + usleep_range(500, 1500); 107 + 108 + return __i2c_smbus_xfer(client->adapter, client->addr, client->flags, 109 + I2C_SMBUS_WRITE, ((u8 *)val_buf)[0], 110 + I2C_SMBUS_I2C_BLOCK_DATA, &data); 111 + } 112 + 113 + /* 114 + * Sunrise i2c read/write operations: lock the i2c segment to avoid losing the 115 + * wake up session. Use custom regmap operations that perform unlocked access to 116 + * the i2c bus. 117 + */ 118 + static int sunrise_read_byte(struct sunrise_dev *sunrise, u8 reg) 119 + { 120 + const struct i2c_client *client = sunrise->client; 121 + const struct device *dev = &client->dev; 122 + unsigned int val; 123 + int ret; 124 + 125 + i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT); 126 + ret = regmap_read(sunrise->regmap, reg, &val); 127 + i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT); 128 + if (ret) { 129 + dev_err(dev, "Read byte failed: reg 0x%02x (%d)\n", reg, ret); 130 + return ret; 131 + } 132 + 133 + return val; 134 + } 135 + 136 + static int sunrise_read_word(struct sunrise_dev *sunrise, u8 reg, u16 *val) 137 + { 138 + const struct i2c_client *client = sunrise->client; 139 + const struct device *dev = &client->dev; 140 + __be16 be_val; 141 + int ret; 142 + 143 + i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT); 144 + ret = regmap_bulk_read(sunrise->regmap, reg, &be_val, sizeof(be_val)); 145 + i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT); 146 + if (ret) { 147 + dev_err(dev, "Read word failed: reg 0x%02x (%d)\n", reg, ret); 148 + return ret; 149 + } 150 + 151 + *val = be16_to_cpu(be_val); 152 + 153 + return 0; 154 + } 155 + 156 + static int sunrise_write_byte(struct sunrise_dev *sunrise, u8 reg, u8 val) 157 + { 158 + const struct i2c_client *client = sunrise->client; 159 + const struct device *dev = &client->dev; 160 + int ret; 161 + 162 + i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT); 163 + ret = regmap_write(sunrise->regmap, reg, val); 164 + i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT); 165 + if (ret) 166 + dev_err(dev, "Write byte failed: reg 0x%02x (%d)\n", reg, ret); 167 + 168 + return ret; 169 + } 170 + 171 + static int sunrise_write_word(struct sunrise_dev *sunrise, u8 reg, u16 data) 172 + { 173 + const struct i2c_client *client = sunrise->client; 174 + const struct device *dev = &client->dev; 175 + __be16 be_data = cpu_to_be16(data); 176 + int ret; 177 + 178 + i2c_lock_bus(client->adapter, I2C_LOCK_SEGMENT); 179 + ret = regmap_bulk_write(sunrise->regmap, reg, &be_data, sizeof(be_data)); 180 + i2c_unlock_bus(client->adapter, I2C_LOCK_SEGMENT); 181 + if (ret) 182 + dev_err(dev, "Write word failed: reg 0x%02x (%d)\n", reg, ret); 183 + 184 + return ret; 185 + } 186 + 187 + /* Trigger a calibration cycle. */ 188 + 189 + enum { 190 + SUNRISE_CALIBRATION_FACTORY, 191 + SUNRISE_CALIBRATION_BACKGROUND, 192 + }; 193 + 194 + static const struct sunrise_calib_data { 195 + u16 cmd; 196 + u8 bit; 197 + const char * const name; 198 + } calib_data[] = { 199 + [SUNRISE_CALIBRATION_FACTORY] = { 200 + SUNRISE_CALIBRATION_FACTORY_CMD, 201 + BIT(2), 202 + "factory_calibration", 203 + }, 204 + [SUNRISE_CALIBRATION_BACKGROUND] = { 205 + SUNRISE_CALIBRATION_BACKGROUND_CMD, 206 + BIT(5), 207 + "background_calibration", 208 + }, 209 + }; 210 + 211 + static int sunrise_calibrate(struct sunrise_dev *sunrise, 212 + const struct sunrise_calib_data *data) 213 + { 214 + unsigned int status; 215 + int ret; 216 + 217 + /* Reset the calibration status reg. */ 218 + ret = sunrise_write_byte(sunrise, SUNRISE_CALIBRATION_STATUS_REG, 0x00); 219 + if (ret) 220 + return ret; 221 + 222 + /* Write a calibration command and poll the calibration status bit. */ 223 + ret = sunrise_write_word(sunrise, SUNRISE_CALIBRATION_COMMAND_REG, data->cmd); 224 + if (ret) 225 + return ret; 226 + 227 + dev_dbg(&sunrise->client->dev, "%s in progress\n", data->name); 228 + 229 + /* 230 + * Calibration takes several seconds, so the sleep time between reads 231 + * can be pretty relaxed. 232 + */ 233 + return read_poll_timeout(sunrise_read_byte, status, status & data->bit, 234 + 200000, SUNRISE_CALIBRATION_TIMEOUT_US, false, 235 + sunrise, SUNRISE_CALIBRATION_STATUS_REG); 236 + } 237 + 238 + static ssize_t sunrise_cal_factory_write(struct iio_dev *iiodev, 239 + uintptr_t private, 240 + const struct iio_chan_spec *chan, 241 + const char *buf, size_t len) 242 + { 243 + struct sunrise_dev *sunrise = iio_priv(iiodev); 244 + bool enable; 245 + int ret; 246 + 247 + ret = kstrtobool(buf, &enable); 248 + if (ret) 249 + return ret; 250 + 251 + if (!enable) 252 + return len; 253 + 254 + mutex_lock(&sunrise->lock); 255 + ret = sunrise_calibrate(sunrise, &calib_data[SUNRISE_CALIBRATION_FACTORY]); 256 + mutex_unlock(&sunrise->lock); 257 + if (ret) 258 + return ret; 259 + 260 + return len; 261 + } 262 + 263 + static ssize_t sunrise_cal_background_write(struct iio_dev *iiodev, 264 + uintptr_t private, 265 + const struct iio_chan_spec *chan, 266 + const char *buf, size_t len) 267 + { 268 + struct sunrise_dev *sunrise = iio_priv(iiodev); 269 + bool enable; 270 + int ret; 271 + 272 + ret = kstrtobool(buf, &enable); 273 + if (ret) 274 + return ret; 275 + 276 + if (!enable) 277 + return len; 278 + 279 + mutex_lock(&sunrise->lock); 280 + ret = sunrise_calibrate(sunrise, &calib_data[SUNRISE_CALIBRATION_BACKGROUND]); 281 + mutex_unlock(&sunrise->lock); 282 + if (ret) 283 + return ret; 284 + 285 + return len; 286 + } 287 + 288 + /* Enumerate and retrieve the chip error status. */ 289 + enum { 290 + SUNRISE_ERROR_FATAL, 291 + SUNRISE_ERROR_I2C, 292 + SUNRISE_ERROR_ALGORITHM, 293 + SUNRISE_ERROR_CALIBRATION, 294 + SUNRISE_ERROR_SELF_DIAGNOSTIC, 295 + SUNRISE_ERROR_OUT_OF_RANGE, 296 + SUNRISE_ERROR_MEMORY, 297 + SUNRISE_ERROR_NO_MEASUREMENT, 298 + SUNRISE_ERROR_LOW_VOLTAGE, 299 + SUNRISE_ERROR_MEASUREMENT_TIMEOUT, 300 + }; 301 + 302 + static const char * const sunrise_error_statuses[] = { 303 + [SUNRISE_ERROR_FATAL] = "error_fatal", 304 + [SUNRISE_ERROR_I2C] = "error_i2c", 305 + [SUNRISE_ERROR_ALGORITHM] = "error_algorithm", 306 + [SUNRISE_ERROR_CALIBRATION] = "error_calibration", 307 + [SUNRISE_ERROR_SELF_DIAGNOSTIC] = "error_self_diagnostic", 308 + [SUNRISE_ERROR_OUT_OF_RANGE] = "error_out_of_range", 309 + [SUNRISE_ERROR_MEMORY] = "error_memory", 310 + [SUNRISE_ERROR_NO_MEASUREMENT] = "error_no_measurement", 311 + [SUNRISE_ERROR_LOW_VOLTAGE] = "error_low_voltage", 312 + [SUNRISE_ERROR_MEASUREMENT_TIMEOUT] = "error_measurement_timeout", 313 + }; 314 + 315 + static const struct iio_enum sunrise_error_statuses_enum = { 316 + .items = sunrise_error_statuses, 317 + .num_items = ARRAY_SIZE(sunrise_error_statuses), 318 + }; 319 + 320 + static ssize_t sunrise_error_status_read(struct iio_dev *iiodev, 321 + uintptr_t private, 322 + const struct iio_chan_spec *chan, 323 + char *buf) 324 + { 325 + struct sunrise_dev *sunrise = iio_priv(iiodev); 326 + unsigned long errors; 327 + ssize_t len = 0; 328 + u16 value; 329 + int ret; 330 + u8 i; 331 + 332 + mutex_lock(&sunrise->lock); 333 + ret = sunrise_read_word(sunrise, SUNRISE_ERROR_STATUS_REG, &value); 334 + if (ret) { 335 + mutex_unlock(&sunrise->lock); 336 + return ret; 337 + } 338 + 339 + errors = value; 340 + for_each_set_bit(i, &errors, ARRAY_SIZE(sunrise_error_statuses)) 341 + len += sysfs_emit_at(buf, len, "%s ", sunrise_error_statuses[i]); 342 + 343 + if (len) 344 + buf[len - 1] = '\n'; 345 + 346 + mutex_unlock(&sunrise->lock); 347 + 348 + return len; 349 + } 350 + 351 + static const struct iio_chan_spec_ext_info sunrise_concentration_ext_info[] = { 352 + /* Calibration triggers. */ 353 + { 354 + .name = "calibration_factory", 355 + .write = sunrise_cal_factory_write, 356 + .shared = IIO_SEPARATE, 357 + }, 358 + { 359 + .name = "calibration_background", 360 + .write = sunrise_cal_background_write, 361 + .shared = IIO_SEPARATE, 362 + }, 363 + 364 + /* Error statuses. */ 365 + { 366 + .name = "error_status", 367 + .read = sunrise_error_status_read, 368 + .shared = IIO_SHARED_BY_ALL, 369 + }, 370 + { 371 + .name = "error_status_available", 372 + .shared = IIO_SHARED_BY_ALL, 373 + .read = iio_enum_available_read, 374 + .private = (uintptr_t)&sunrise_error_statuses_enum, 375 + }, 376 + {} 377 + }; 378 + 379 + static const struct iio_chan_spec sunrise_channels[] = { 380 + { 381 + .type = IIO_CONCENTRATION, 382 + .modified = 1, 383 + .channel2 = IIO_MOD_CO2, 384 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 385 + BIT(IIO_CHAN_INFO_SCALE), 386 + .ext_info = sunrise_concentration_ext_info, 387 + }, 388 + { 389 + .type = IIO_TEMP, 390 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 391 + BIT(IIO_CHAN_INFO_SCALE), 392 + }, 393 + }; 394 + 395 + static int sunrise_read_raw(struct iio_dev *iio_dev, 396 + const struct iio_chan_spec *chan, 397 + int *val, int *val2, long mask) 398 + { 399 + struct sunrise_dev *sunrise = iio_priv(iio_dev); 400 + u16 value; 401 + int ret; 402 + 403 + switch (mask) { 404 + case IIO_CHAN_INFO_RAW: 405 + switch (chan->type) { 406 + case IIO_CONCENTRATION: 407 + mutex_lock(&sunrise->lock); 408 + ret = sunrise_read_word(sunrise, SUNRISE_CO2_FILTERED_COMP_REG, 409 + &value); 410 + *val = value; 411 + mutex_unlock(&sunrise->lock); 412 + 413 + if (ret) 414 + return ret; 415 + 416 + return IIO_VAL_INT; 417 + 418 + case IIO_TEMP: 419 + mutex_lock(&sunrise->lock); 420 + ret = sunrise_read_word(sunrise, SUNRISE_CHIP_TEMPERATURE_REG, 421 + &value); 422 + *val = value; 423 + mutex_unlock(&sunrise->lock); 424 + 425 + if (ret) 426 + return ret; 427 + 428 + return IIO_VAL_INT; 429 + 430 + default: 431 + return -EINVAL; 432 + } 433 + 434 + case IIO_CHAN_INFO_SCALE: 435 + switch (chan->type) { 436 + case IIO_CONCENTRATION: 437 + /* 438 + * 1 / 10^4 to comply with IIO scale for CO2 439 + * (percentage). The chip CO2 reading range is [400 - 440 + * 5000] ppm which corresponds to [0,004 - 0,5] %. 441 + */ 442 + *val = 1; 443 + *val2 = 10000; 444 + return IIO_VAL_FRACTIONAL; 445 + 446 + case IIO_TEMP: 447 + /* x10 to comply with IIO scale (millidegrees celsius). */ 448 + *val = 10; 449 + return IIO_VAL_INT; 450 + 451 + default: 452 + return -EINVAL; 453 + } 454 + 455 + default: 456 + return -EINVAL; 457 + } 458 + } 459 + 460 + static const struct iio_info sunrise_info = { 461 + .read_raw = sunrise_read_raw, 462 + }; 463 + 464 + static const struct regmap_bus sunrise_regmap_bus = { 465 + .read = sunrise_regmap_read, 466 + .write = sunrise_regmap_write, 467 + }; 468 + 469 + static const struct regmap_config sunrise_regmap_config = { 470 + .reg_bits = 8, 471 + .val_bits = 8, 472 + }; 473 + 474 + static int sunrise_probe(struct i2c_client *client) 475 + { 476 + struct sunrise_dev *sunrise; 477 + struct iio_dev *iio_dev; 478 + 479 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA | 480 + I2C_FUNC_SMBUS_BLOCK_DATA)) { 481 + dev_err(&client->dev, 482 + "Adapter does not support required functionalities\n"); 483 + return -EOPNOTSUPP; 484 + } 485 + 486 + iio_dev = devm_iio_device_alloc(&client->dev, sizeof(*sunrise)); 487 + if (!iio_dev) 488 + return -ENOMEM; 489 + 490 + sunrise = iio_priv(iio_dev); 491 + sunrise->client = client; 492 + mutex_init(&sunrise->lock); 493 + 494 + i2c_set_clientdata(client, sunrise); 495 + 496 + sunrise->regmap = devm_regmap_init(&client->dev, &sunrise_regmap_bus, 497 + client, &sunrise_regmap_config); 498 + if (IS_ERR(sunrise->regmap)) { 499 + dev_err(&client->dev, "Failed to initialize regmap\n"); 500 + return PTR_ERR(sunrise->regmap); 501 + } 502 + 503 + /* 504 + * The chip nacks the wake up message. If the adapter does not support 505 + * protocol mangling do not set the I2C_M_IGNORE_NAK flag at the expense 506 + * of possible cruft in the logs. 507 + */ 508 + if (i2c_check_functionality(client->adapter, I2C_FUNC_PROTOCOL_MANGLING)) 509 + sunrise->ignore_nak = true; 510 + 511 + iio_dev->info = &sunrise_info; 512 + iio_dev->name = DRIVER_NAME; 513 + iio_dev->channels = sunrise_channels; 514 + iio_dev->num_channels = ARRAY_SIZE(sunrise_channels); 515 + iio_dev->modes = INDIO_DIRECT_MODE; 516 + 517 + return devm_iio_device_register(&client->dev, iio_dev); 518 + } 519 + 520 + static const struct of_device_id sunrise_of_match[] = { 521 + { .compatible = "senseair,sunrise-006-0-0007" }, 522 + {} 523 + }; 524 + MODULE_DEVICE_TABLE(of, sunrise_of_match); 525 + 526 + static struct i2c_driver sunrise_driver = { 527 + .driver = { 528 + .name = DRIVER_NAME, 529 + .of_match_table = sunrise_of_match, 530 + }, 531 + .probe_new = sunrise_probe, 532 + }; 533 + module_i2c_driver(sunrise_driver); 534 + 535 + MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>"); 536 + MODULE_DESCRIPTION("Senseair Sunrise 006-0-0007 CO2 sensor IIO driver"); 537 + MODULE_LICENSE("GPL v2");
+3 -2
drivers/iio/common/hid-sensors/hid-sensor-trigger.c
··· 241 241 fifo_attrs = NULL; 242 242 243 243 ret = iio_triggered_buffer_setup_ext(indio_dev, 244 - &iio_pollfunc_store_time, 245 - NULL, NULL, fifo_attrs); 244 + &iio_pollfunc_store_time, NULL, 245 + IIO_BUFFER_DIRECTION_IN, 246 + NULL, fifo_attrs); 246 247 if (ret) { 247 248 dev_err(&indio_dev->dev, "Triggered Buffer Setup Failed\n"); 248 249 return ret;
+22 -26
drivers/iio/common/st_sensors/st_sensors_core.c
··· 215 215 } 216 216 EXPORT_SYMBOL(st_sensors_set_axis_enable); 217 217 218 + static void st_reg_disable(void *reg) 219 + { 220 + regulator_disable(reg); 221 + } 222 + 218 223 int st_sensors_power_enable(struct iio_dev *indio_dev) 219 224 { 220 225 struct st_sensor_data *pdata = iio_priv(indio_dev); 226 + struct device *parent = indio_dev->dev.parent; 221 227 int err; 222 228 223 229 /* Regulators not mandatory, but if requested we should enable them. */ 224 - pdata->vdd = devm_regulator_get(indio_dev->dev.parent, "vdd"); 225 - if (IS_ERR(pdata->vdd)) { 226 - dev_err(&indio_dev->dev, "unable to get Vdd supply\n"); 227 - return PTR_ERR(pdata->vdd); 228 - } 230 + pdata->vdd = devm_regulator_get(parent, "vdd"); 231 + if (IS_ERR(pdata->vdd)) 232 + return dev_err_probe(&indio_dev->dev, PTR_ERR(pdata->vdd), 233 + "unable to get Vdd supply\n"); 234 + 229 235 err = regulator_enable(pdata->vdd); 230 236 if (err != 0) { 231 237 dev_warn(&indio_dev->dev, ··· 239 233 return err; 240 234 } 241 235 242 - pdata->vdd_io = devm_regulator_get(indio_dev->dev.parent, "vddio"); 243 - if (IS_ERR(pdata->vdd_io)) { 244 - dev_err(&indio_dev->dev, "unable to get Vdd_IO supply\n"); 245 - err = PTR_ERR(pdata->vdd_io); 246 - goto st_sensors_disable_vdd; 247 - } 236 + err = devm_add_action_or_reset(parent, st_reg_disable, pdata->vdd); 237 + if (err) 238 + return err; 239 + 240 + pdata->vdd_io = devm_regulator_get(parent, "vddio"); 241 + if (IS_ERR(pdata->vdd_io)) 242 + return dev_err_probe(&indio_dev->dev, PTR_ERR(pdata->vdd_io), 243 + "unable to get Vdd_IO supply\n"); 244 + 248 245 err = regulator_enable(pdata->vdd_io); 249 246 if (err != 0) { 250 247 dev_warn(&indio_dev->dev, 251 248 "Failed to enable specified Vdd_IO supply\n"); 252 - goto st_sensors_disable_vdd; 249 + return err; 253 250 } 254 251 255 - return 0; 256 - 257 - st_sensors_disable_vdd: 258 - regulator_disable(pdata->vdd); 259 - return err; 252 + return devm_add_action_or_reset(parent, st_reg_disable, pdata->vdd_io); 260 253 } 261 254 EXPORT_SYMBOL(st_sensors_power_enable); 262 - 263 - void st_sensors_power_disable(struct iio_dev *indio_dev) 264 - { 265 - struct st_sensor_data *pdata = iio_priv(indio_dev); 266 - 267 - regulator_disable(pdata->vdd); 268 - regulator_disable(pdata->vdd_io); 269 - } 270 - EXPORT_SYMBOL(st_sensors_power_disable); 271 255 272 256 static int st_sensors_set_drdy_int_pin(struct iio_dev *indio_dev, 273 257 struct st_sensors_platform_data *pdata)
-1
drivers/iio/common/st_sensors/st_sensors_i2c.c
··· 57 57 58 58 indio_dev->name = client->name; 59 59 60 - sdata->dev = &client->dev; 61 60 sdata->irq = client->irq; 62 61 63 62 return 0;
-1
drivers/iio/common/st_sensors/st_sensors_spi.c
··· 109 109 110 110 indio_dev->name = spi->modalias; 111 111 112 - sdata->dev = &spi->dev; 113 112 sdata->irq = spi->irq; 114 113 115 114 return 0;
+20 -33
drivers/iio/common/st_sensors/st_sensors_trigger.c
··· 42 42 sdata->sensor_settings->drdy_irq.stat_drdy.addr, 43 43 &status); 44 44 if (ret < 0) { 45 - dev_err(sdata->dev, "error checking samples available\n"); 45 + dev_err(indio_dev->dev.parent, 46 + "error checking samples available\n"); 46 47 return false; 47 48 } 48 49 ··· 88 87 st_sensors_new_samples_available(indio_dev, sdata)) { 89 88 iio_trigger_poll_chained(p); 90 89 } else { 91 - dev_dbg(sdata->dev, "spurious IRQ\n"); 90 + dev_dbg(indio_dev->dev.parent, "spurious IRQ\n"); 92 91 return IRQ_NONE; 93 92 } 94 93 ··· 108 107 */ 109 108 while (sdata->hw_irq_trigger && 110 109 st_sensors_new_samples_available(indio_dev, sdata)) { 111 - dev_dbg(sdata->dev, "more samples came in during polling\n"); 110 + dev_dbg(indio_dev->dev.parent, 111 + "more samples came in during polling\n"); 112 112 sdata->hw_timestamp = iio_get_time_ns(indio_dev); 113 113 iio_trigger_poll_chained(p); 114 114 } ··· 121 119 const struct iio_trigger_ops *trigger_ops) 122 120 { 123 121 struct st_sensor_data *sdata = iio_priv(indio_dev); 122 + struct device *parent = indio_dev->dev.parent; 124 123 unsigned long irq_trig; 125 124 int err; 126 125 127 - sdata->trig = iio_trigger_alloc(sdata->dev, "%s-trigger", 128 - indio_dev->name); 126 + sdata->trig = devm_iio_trigger_alloc(parent, "%s-trigger", 127 + indio_dev->name); 129 128 if (sdata->trig == NULL) { 130 129 dev_err(&indio_dev->dev, "failed to allocate iio trigger.\n"); 131 130 return -ENOMEM; ··· 156 153 sdata->sensor_settings->drdy_irq.addr_ihl, 157 154 sdata->sensor_settings->drdy_irq.mask_ihl, 1); 158 155 if (err < 0) 159 - goto iio_trigger_free; 156 + return err; 160 157 dev_info(&indio_dev->dev, 161 158 "interrupts on the falling edge or active low level\n"); 162 159 } ··· 182 179 if (!sdata->sensor_settings->drdy_irq.stat_drdy.addr) { 183 180 dev_err(&indio_dev->dev, 184 181 "edge IRQ not supported w/o stat register.\n"); 185 - err = -EOPNOTSUPP; 186 - goto iio_trigger_free; 182 + return -EOPNOTSUPP; 187 183 } 188 184 sdata->edge_irq = true; 189 185 } else { ··· 207 205 sdata->sensor_settings->drdy_irq.stat_drdy.addr) 208 206 irq_trig |= IRQF_SHARED; 209 207 210 - err = request_threaded_irq(sdata->irq, 211 - st_sensors_irq_handler, 212 - st_sensors_irq_thread, 213 - irq_trig, 214 - sdata->trig->name, 215 - sdata->trig); 208 + err = devm_request_threaded_irq(parent, 209 + sdata->irq, 210 + st_sensors_irq_handler, 211 + st_sensors_irq_thread, 212 + irq_trig, 213 + sdata->trig->name, 214 + sdata->trig); 216 215 if (err) { 217 216 dev_err(&indio_dev->dev, "failed to request trigger IRQ.\n"); 218 - goto iio_trigger_free; 217 + return err; 219 218 } 220 219 221 - err = iio_trigger_register(sdata->trig); 220 + err = devm_iio_trigger_register(parent, sdata->trig); 222 221 if (err < 0) { 223 222 dev_err(&indio_dev->dev, "failed to register iio trigger.\n"); 224 - goto iio_trigger_register_error; 223 + return err; 225 224 } 226 225 indio_dev->trig = iio_trigger_get(sdata->trig); 227 226 228 227 return 0; 229 - 230 - iio_trigger_register_error: 231 - free_irq(sdata->irq, sdata->trig); 232 - iio_trigger_free: 233 - iio_trigger_free(sdata->trig); 234 - return err; 235 228 } 236 229 EXPORT_SYMBOL(st_sensors_allocate_trigger); 237 - 238 - void st_sensors_deallocate_trigger(struct iio_dev *indio_dev) 239 - { 240 - struct st_sensor_data *sdata = iio_priv(indio_dev); 241 - 242 - iio_trigger_unregister(sdata->trig); 243 - free_irq(sdata->irq, sdata->trig); 244 - iio_trigger_free(sdata->trig); 245 - } 246 - EXPORT_SYMBOL(st_sensors_deallocate_trigger); 247 230 248 231 int st_sensors_validate_device(struct iio_trigger *trig, 249 232 struct iio_dev *indio_dev)
+12 -37
drivers/iio/dac/ad5064.c
··· 843 843 return ret; 844 844 } 845 845 846 + static void ad5064_bulk_reg_disable(void *data) 847 + { 848 + struct ad5064_state *st = data; 849 + 850 + regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg); 851 + } 852 + 846 853 static int ad5064_probe(struct device *dev, enum ad5064_type type, 847 854 const char *name, ad5064_write_func write) 848 855 { ··· 865 858 866 859 st = iio_priv(indio_dev); 867 860 mutex_init(&st->lock); 868 - dev_set_drvdata(dev, indio_dev); 869 861 870 862 st->chip_info = &ad5064_chip_info_tbl[type]; 871 863 st->dev = dev; ··· 876 870 877 871 if (!st->use_internal_vref) { 878 872 ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg); 873 + if (ret) 874 + return ret; 875 + 876 + ret = devm_add_action_or_reset(dev, ad5064_bulk_reg_disable, st); 879 877 if (ret) 880 878 return ret; 881 879 } ··· 897 887 st->dac_cache[i] = midscale; 898 888 } 899 889 900 - ret = iio_device_register(indio_dev); 901 - if (ret) 902 - goto error_disable_reg; 903 - 904 - return 0; 905 - 906 - error_disable_reg: 907 - if (!st->use_internal_vref) 908 - regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg); 909 - 910 - return ret; 911 - } 912 - 913 - static int ad5064_remove(struct device *dev) 914 - { 915 - struct iio_dev *indio_dev = dev_get_drvdata(dev); 916 - struct ad5064_state *st = iio_priv(indio_dev); 917 - 918 - iio_device_unregister(indio_dev); 919 - 920 - if (!st->use_internal_vref) 921 - regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg); 922 - 923 - return 0; 890 + return devm_iio_device_register(dev, indio_dev); 924 891 } 925 892 926 893 #if IS_ENABLED(CONFIG_SPI_MASTER) ··· 917 930 918 931 return ad5064_probe(&spi->dev, id->driver_data, id->name, 919 932 ad5064_spi_write); 920 - } 921 - 922 - static int ad5064_spi_remove(struct spi_device *spi) 923 - { 924 - return ad5064_remove(&spi->dev); 925 933 } 926 934 927 935 static const struct spi_device_id ad5064_spi_ids[] = { ··· 945 963 .name = "ad5064", 946 964 }, 947 965 .probe = ad5064_spi_probe, 948 - .remove = ad5064_spi_remove, 949 966 .id_table = ad5064_spi_ids, 950 967 }; 951 968 ··· 998 1017 { 999 1018 return ad5064_probe(&i2c->dev, id->driver_data, id->name, 1000 1019 ad5064_i2c_write); 1001 - } 1002 - 1003 - static int ad5064_i2c_remove(struct i2c_client *i2c) 1004 - { 1005 - return ad5064_remove(&i2c->dev); 1006 1020 } 1007 1021 1008 1022 static const struct i2c_device_id ad5064_i2c_ids[] = { ··· 1057 1081 .name = "ad5064", 1058 1082 }, 1059 1083 .probe = ad5064_i2c_probe, 1060 - .remove = ad5064_i2c_remove, 1061 1084 .id_table = ad5064_i2c_ids, 1062 1085 }; 1063 1086
+8 -7
drivers/iio/dac/ad5380.c
··· 444 444 return ret; 445 445 } 446 446 447 - static int ad5380_remove(struct device *dev) 447 + static void ad5380_remove(struct device *dev) 448 448 { 449 449 struct iio_dev *indio_dev = dev_get_drvdata(dev); 450 450 struct ad5380_state *st = iio_priv(indio_dev); ··· 453 453 454 454 kfree(indio_dev->channels); 455 455 456 - if (!IS_ERR(st->vref_reg)) { 456 + if (!IS_ERR(st->vref_reg)) 457 457 regulator_disable(st->vref_reg); 458 - } 459 - 460 - return 0; 461 458 } 462 459 463 460 static bool ad5380_reg_false(struct device *dev, unsigned int reg) ··· 490 493 491 494 static int ad5380_spi_remove(struct spi_device *spi) 492 495 { 493 - return ad5380_remove(&spi->dev); 496 + ad5380_remove(&spi->dev); 497 + 498 + return 0; 494 499 } 495 500 496 501 static const struct spi_device_id ad5380_spi_ids[] = { ··· 565 566 566 567 static int ad5380_i2c_remove(struct i2c_client *i2c) 567 568 { 568 - return ad5380_remove(&i2c->dev); 569 + ad5380_remove(&i2c->dev); 570 + 571 + return 0; 569 572 } 570 573 571 574 static const struct i2c_device_id ad5380_i2c_ids[] = {
+7 -5
drivers/iio/dac/ad5446.c
··· 283 283 return ret; 284 284 } 285 285 286 - static int ad5446_remove(struct device *dev) 286 + static void ad5446_remove(struct device *dev) 287 287 { 288 288 struct iio_dev *indio_dev = dev_get_drvdata(dev); 289 289 struct ad5446_state *st = iio_priv(indio_dev); ··· 291 291 iio_device_unregister(indio_dev); 292 292 if (!IS_ERR(st->reg)) 293 293 regulator_disable(st->reg); 294 - 295 - return 0; 296 294 } 297 295 298 296 #if IS_ENABLED(CONFIG_SPI_MASTER) ··· 493 495 494 496 static int ad5446_spi_remove(struct spi_device *spi) 495 497 { 496 - return ad5446_remove(&spi->dev); 498 + ad5446_remove(&spi->dev); 499 + 500 + return 0; 497 501 } 498 502 499 503 static struct spi_driver ad5446_spi_driver = { ··· 572 572 573 573 static int ad5446_i2c_remove(struct i2c_client *i2c) 574 574 { 575 - return ad5446_remove(&i2c->dev); 575 + ad5446_remove(&i2c->dev); 576 + 577 + return 0; 576 578 } 577 579 578 580 static const struct i2c_device_id ad5446_i2c_ids[] = {
+1 -3
drivers/iio/dac/ad5592r-base.c
··· 663 663 } 664 664 EXPORT_SYMBOL_GPL(ad5592r_probe); 665 665 666 - int ad5592r_remove(struct device *dev) 666 + void ad5592r_remove(struct device *dev) 667 667 { 668 668 struct iio_dev *iio_dev = dev_get_drvdata(dev); 669 669 struct ad5592r_state *st = iio_priv(iio_dev); ··· 674 674 675 675 if (st->reg) 676 676 regulator_disable(st->reg); 677 - 678 - return 0; 679 677 } 680 678 EXPORT_SYMBOL_GPL(ad5592r_remove); 681 679
+1 -1
drivers/iio/dac/ad5592r-base.h
··· 71 71 72 72 int ad5592r_probe(struct device *dev, const char *name, 73 73 const struct ad5592r_rw_ops *ops); 74 - int ad5592r_remove(struct device *dev); 74 + void ad5592r_remove(struct device *dev); 75 75 76 76 #endif /* __DRIVERS_IIO_DAC_AD5592R_BASE_H__ */
+3 -1
drivers/iio/dac/ad5592r.c
··· 132 132 133 133 static int ad5592r_spi_remove(struct spi_device *spi) 134 134 { 135 - return ad5592r_remove(&spi->dev); 135 + ad5592r_remove(&spi->dev); 136 + 137 + return 0; 136 138 } 137 139 138 140 static const struct spi_device_id ad5592r_spi_ids[] = {
+3 -1
drivers/iio/dac/ad5593r.c
··· 99 99 100 100 static int ad5593r_i2c_remove(struct i2c_client *i2c) 101 101 { 102 - return ad5592r_remove(&i2c->dev); 102 + ad5592r_remove(&i2c->dev); 103 + 104 + return 0; 103 105 } 104 106 105 107 static const struct i2c_device_id ad5593r_i2c_ids[] = {
+3 -1
drivers/iio/dac/ad5686-spi.c
··· 97 97 98 98 static int ad5686_spi_remove(struct spi_device *spi) 99 99 { 100 - return ad5686_remove(&spi->dev); 100 + ad5686_remove(&spi->dev); 101 + 102 + return 0; 101 103 } 102 104 103 105 static const struct spi_device_id ad5686_spi_id[] = {
+1 -3
drivers/iio/dac/ad5686.c
··· 538 538 } 539 539 EXPORT_SYMBOL_GPL(ad5686_probe); 540 540 541 - int ad5686_remove(struct device *dev) 541 + void ad5686_remove(struct device *dev) 542 542 { 543 543 struct iio_dev *indio_dev = dev_get_drvdata(dev); 544 544 struct ad5686_state *st = iio_priv(indio_dev); ··· 546 546 iio_device_unregister(indio_dev); 547 547 if (!IS_ERR(st->reg)) 548 548 regulator_disable(st->reg); 549 - 550 - return 0; 551 549 } 552 550 EXPORT_SYMBOL_GPL(ad5686_remove); 553 551
+1 -1
drivers/iio/dac/ad5686.h
··· 154 154 const char *name, ad5686_write_func write, 155 155 ad5686_read_func read); 156 156 157 - int ad5686_remove(struct device *dev); 157 + void ad5686_remove(struct device *dev); 158 158 159 159 160 160 #endif /* __DRIVERS_IIO_DAC_AD5686_H__ */
+3 -1
drivers/iio/dac/ad5696-i2c.c
··· 67 67 68 68 static int ad5686_i2c_remove(struct i2c_client *i2c) 69 69 { 70 - return ad5686_remove(&i2c->dev); 70 + ad5686_remove(&i2c->dev); 71 + 72 + return 0; 71 73 } 72 74 73 75 static const struct i2c_device_id ad5686_i2c_id[] = {
+42
drivers/iio/dac/ad5766.c
··· 5 5 * Copyright 2019-2020 Analog Devices Inc. 6 6 */ 7 7 #include <linux/bitfield.h> 8 + #include <linux/bitops.h> 8 9 #include <linux/delay.h> 9 10 #include <linux/device.h> 10 11 #include <linux/gpio/consumer.h> 11 12 #include <linux/iio/iio.h> 13 + #include <linux/iio/triggered_buffer.h> 14 + #include <linux/iio/trigger_consumer.h> 12 15 #include <linux/module.h> 13 16 #include <linux/spi/spi.h> 14 17 #include <asm/unaligned.h> ··· 458 455 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 459 456 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | \ 460 457 BIT(IIO_CHAN_INFO_SCALE), \ 458 + .scan_index = (_chan), \ 461 459 .scan_type = { \ 462 460 .sign = 'u', \ 463 461 .realbits = (_bits), \ ··· 580 576 return __ad5766_spi_write(st, AD5766_CMD_SPAN_REG, st->crt_range); 581 577 } 582 578 579 + static irqreturn_t ad5766_trigger_handler(int irq, void *p) 580 + { 581 + struct iio_poll_func *pf = p; 582 + struct iio_dev *indio_dev = pf->indio_dev; 583 + struct iio_buffer *buffer = indio_dev->buffer; 584 + struct ad5766_state *st = iio_priv(indio_dev); 585 + int ret, ch, i; 586 + u16 data[ARRAY_SIZE(ad5766_channels)]; 587 + 588 + ret = iio_pop_from_buffer(buffer, data); 589 + if (ret) 590 + goto done; 591 + 592 + i = 0; 593 + mutex_lock(&st->lock); 594 + for_each_set_bit(ch, indio_dev->active_scan_mask, 595 + st->chip_info->num_channels - 1) 596 + __ad5766_spi_write(st, AD5766_CMD_WR_IN_REG(ch), data[i++]); 597 + 598 + __ad5766_spi_write(st, AD5766_CMD_SW_LDAC, 599 + *indio_dev->active_scan_mask); 600 + mutex_unlock(&st->lock); 601 + 602 + done: 603 + iio_trigger_notify_done(indio_dev->trig); 604 + 605 + return IRQ_HANDLED; 606 + } 607 + 583 608 static int ad5766_probe(struct spi_device *spi) 584 609 { 585 610 enum ad5766_type type; ··· 639 606 return PTR_ERR(st->gpio_reset); 640 607 641 608 ret = ad5766_default_setup(st); 609 + if (ret) 610 + return ret; 611 + 612 + /* Configure trigger buffer */ 613 + ret = devm_iio_triggered_buffer_setup_ext(&spi->dev, indio_dev, NULL, 614 + ad5766_trigger_handler, 615 + IIO_BUFFER_DIRECTION_OUT, 616 + NULL, 617 + NULL); 642 618 if (ret) 643 619 return ret; 644 620
+1 -1
drivers/iio/dac/ad5770r.c
··· 522 522 return -EINVAL; 523 523 524 524 device_for_each_child_node(&st->spi->dev, child) { 525 - ret = fwnode_property_read_u32(child, "num", &num); 525 + ret = fwnode_property_read_u32(child, "reg", &num); 526 526 if (ret) 527 527 goto err_child_out; 528 528 if (num >= AD5770R_MAX_CHANNELS) {
+17 -30
drivers/iio/dac/ad7303.c
··· 198 198 AD7303_CHANNEL(1), 199 199 }; 200 200 201 + static void ad7303_reg_disable(void *reg) 202 + { 203 + regulator_disable(reg); 204 + } 205 + 201 206 static int ad7303_probe(struct spi_device *spi) 202 207 { 203 208 const struct spi_device_id *id = spi_get_device_id(spi); ··· 215 210 return -ENOMEM; 216 211 217 212 st = iio_priv(indio_dev); 218 - spi_set_drvdata(spi, indio_dev); 219 213 220 214 st->spi = spi; 221 215 ··· 228 224 if (ret) 229 225 return ret; 230 226 227 + ret = devm_add_action_or_reset(&spi->dev, ad7303_reg_disable, st->vdd_reg); 228 + if (ret) 229 + return ret; 230 + 231 231 st->vref_reg = devm_regulator_get_optional(&spi->dev, "REF"); 232 232 if (IS_ERR(st->vref_reg)) { 233 233 ret = PTR_ERR(st->vref_reg); 234 234 if (ret != -ENODEV) 235 - goto err_disable_vdd_reg; 235 + return ret; 236 236 st->vref_reg = NULL; 237 237 } 238 238 239 239 if (st->vref_reg) { 240 240 ret = regulator_enable(st->vref_reg); 241 241 if (ret) 242 - goto err_disable_vdd_reg; 242 + return ret; 243 + 244 + ret = devm_add_action_or_reset(&spi->dev, ad7303_reg_disable, 245 + st->vref_reg); 246 + if (ret) 247 + return ret; 243 248 244 249 st->config |= AD7303_CFG_EXTERNAL_VREF; 245 250 } ··· 259 246 indio_dev->channels = ad7303_channels; 260 247 indio_dev->num_channels = ARRAY_SIZE(ad7303_channels); 261 248 262 - ret = iio_device_register(indio_dev); 263 - if (ret) 264 - goto err_disable_vref_reg; 265 - 266 - return 0; 267 - 268 - err_disable_vref_reg: 269 - if (st->vref_reg) 270 - regulator_disable(st->vref_reg); 271 - err_disable_vdd_reg: 272 - regulator_disable(st->vdd_reg); 273 - return ret; 274 - } 275 - 276 - static int ad7303_remove(struct spi_device *spi) 277 - { 278 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 279 - struct ad7303_state *st = iio_priv(indio_dev); 280 - 281 - iio_device_unregister(indio_dev); 282 - 283 - if (st->vref_reg) 284 - regulator_disable(st->vref_reg); 285 - regulator_disable(st->vdd_reg); 286 - 287 - return 0; 249 + return devm_iio_device_register(&spi->dev, indio_dev); 288 250 } 289 251 290 252 static const struct of_device_id ad7303_spi_of_match[] = { ··· 280 292 .of_match_table = ad7303_spi_of_match, 281 293 }, 282 294 .probe = ad7303_probe, 283 - .remove = ad7303_remove, 284 295 .id_table = ad7303_spi_ids, 285 296 }; 286 297 module_spi_driver(ad7303_driver);
+5 -6
drivers/iio/dac/ad8801.c
··· 123 123 id = spi_get_device_id(spi); 124 124 125 125 state->vrefh_reg = devm_regulator_get(&spi->dev, "vrefh"); 126 - if (IS_ERR(state->vrefh_reg)) { 127 - dev_err(&spi->dev, "Vrefh regulator not specified\n"); 128 - return PTR_ERR(state->vrefh_reg); 129 - } 126 + if (IS_ERR(state->vrefh_reg)) 127 + return dev_err_probe(&spi->dev, PTR_ERR(state->vrefh_reg), 128 + "Vrefh regulator not specified\n"); 130 129 131 130 ret = regulator_enable(state->vrefh_reg); 132 131 if (ret) { ··· 145 146 if (id->driver_data == ID_AD8803) { 146 147 state->vrefl_reg = devm_regulator_get(&spi->dev, "vrefl"); 147 148 if (IS_ERR(state->vrefl_reg)) { 148 - dev_err(&spi->dev, "Vrefl regulator not specified\n"); 149 - ret = PTR_ERR(state->vrefl_reg); 149 + ret = dev_err_probe(&spi->dev, PTR_ERR(state->vrefl_reg), 150 + "Vrefl regulator not specified\n"); 150 151 goto error_disable_vrefh_reg; 151 152 } 152 153
+3 -6
drivers/iio/dac/ds4424.c
··· 232 232 indio_dev->name = id->name; 233 233 234 234 data->vcc_reg = devm_regulator_get(&client->dev, "vcc"); 235 - if (IS_ERR(data->vcc_reg)) { 236 - dev_err(&client->dev, 237 - "Failed to get vcc-supply regulator. err: %ld\n", 238 - PTR_ERR(data->vcc_reg)); 239 - return PTR_ERR(data->vcc_reg); 240 - } 235 + if (IS_ERR(data->vcc_reg)) 236 + return dev_err_probe(&client->dev, PTR_ERR(data->vcc_reg), 237 + "Failed to get vcc-supply regulator.\n"); 241 238 242 239 mutex_init(&data->lock); 243 240 ret = regulator_enable(data->vcc_reg);
+6 -8
drivers/iio/dac/lpc18xx_dac.c
··· 121 121 return PTR_ERR(dac->base); 122 122 123 123 dac->clk = devm_clk_get(&pdev->dev, NULL); 124 - if (IS_ERR(dac->clk)) { 125 - dev_err(&pdev->dev, "error getting clock\n"); 126 - return PTR_ERR(dac->clk); 127 - } 124 + if (IS_ERR(dac->clk)) 125 + return dev_err_probe(&pdev->dev, PTR_ERR(dac->clk), 126 + "error getting clock\n"); 128 127 129 128 dac->vref = devm_regulator_get(&pdev->dev, "vref"); 130 - if (IS_ERR(dac->vref)) { 131 - dev_err(&pdev->dev, "error getting regulator\n"); 132 - return PTR_ERR(dac->vref); 133 - } 129 + if (IS_ERR(dac->vref)) 130 + return dev_err_probe(&pdev->dev, PTR_ERR(dac->vref), 131 + "error getting regulator\n"); 134 132 135 133 indio_dev->name = dev_name(&pdev->dev); 136 134 indio_dev->info = &lpc18xx_dac_info;
+3 -4
drivers/iio/dac/ltc1660.c
··· 172 172 } 173 173 174 174 priv->vref_reg = devm_regulator_get(&spi->dev, "vref"); 175 - if (IS_ERR(priv->vref_reg)) { 176 - dev_err(&spi->dev, "vref regulator not specified\n"); 177 - return PTR_ERR(priv->vref_reg); 178 - } 175 + if (IS_ERR(priv->vref_reg)) 176 + return dev_err_probe(&spi->dev, PTR_ERR(priv->vref_reg), 177 + "vref regulator not specified\n"); 179 178 180 179 ret = regulator_enable(priv->vref_reg); 181 180 if (ret) {
+3 -6
drivers/iio/dac/max5821.c
··· 321 321 } 322 322 323 323 data->vref_reg = devm_regulator_get(&client->dev, "vref"); 324 - if (IS_ERR(data->vref_reg)) { 325 - ret = PTR_ERR(data->vref_reg); 326 - dev_err(&client->dev, 327 - "Failed to get vref regulator: %d\n", ret); 328 - return ret; 329 - } 324 + if (IS_ERR(data->vref_reg)) 325 + return dev_err_probe(&client->dev, PTR_ERR(data->vref_reg), 326 + "Failed to get vref regulator\n"); 330 327 331 328 ret = regulator_enable(data->vref_reg); 332 329 if (ret) {
+3 -4
drivers/iio/dac/mcp4922.c
··· 130 130 state = iio_priv(indio_dev); 131 131 state->spi = spi; 132 132 state->vref_reg = devm_regulator_get(&spi->dev, "vref"); 133 - if (IS_ERR(state->vref_reg)) { 134 - dev_err(&spi->dev, "Vref regulator not specified\n"); 135 - return PTR_ERR(state->vref_reg); 136 - } 133 + if (IS_ERR(state->vref_reg)) 134 + return dev_err_probe(&spi->dev, PTR_ERR(state->vref_reg), 135 + "Vref regulator not specified\n"); 137 136 138 137 ret = regulator_enable(state->vref_reg); 139 138 if (ret) {
+5 -13
drivers/iio/dac/stm32-dac-core.c
··· 90 90 const struct stm32_dac_cfg *cfg; 91 91 struct stm32_dac_priv *priv; 92 92 struct regmap *regmap; 93 - struct resource *res; 94 93 void __iomem *mmio; 95 94 struct reset_control *rst; 96 95 int ret; ··· 105 106 cfg = (const struct stm32_dac_cfg *) 106 107 of_match_device(dev->driver->of_match_table, dev)->data; 107 108 108 - res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 109 - mmio = devm_ioremap_resource(dev, res); 109 + mmio = devm_platform_ioremap_resource(pdev, 0); 110 110 if (IS_ERR(mmio)) 111 111 return PTR_ERR(mmio); 112 112 ··· 116 118 priv->common.regmap = regmap; 117 119 118 120 priv->pclk = devm_clk_get(dev, "pclk"); 119 - if (IS_ERR(priv->pclk)) { 120 - ret = PTR_ERR(priv->pclk); 121 - dev_err(dev, "pclk get failed\n"); 122 - return ret; 123 - } 121 + if (IS_ERR(priv->pclk)) 122 + return dev_err_probe(dev, PTR_ERR(priv->pclk), "pclk get failed\n"); 124 123 125 124 priv->vref = devm_regulator_get(dev, "vref"); 126 - if (IS_ERR(priv->vref)) { 127 - ret = PTR_ERR(priv->vref); 128 - dev_err(dev, "vref get failed, %d\n", ret); 129 - return ret; 130 - } 125 + if (IS_ERR(priv->vref)) 126 + return dev_err_probe(dev, PTR_ERR(priv->vref), "vref get failed\n"); 131 127 132 128 pm_runtime_get_noresume(dev); 133 129 pm_runtime_set_active(dev);
+3 -4
drivers/iio/dac/ti-dac7311.c
··· 266 266 ti_dac->resolution = spec->resolution; 267 267 268 268 ti_dac->vref = devm_regulator_get(dev, "vref"); 269 - if (IS_ERR(ti_dac->vref)) { 270 - dev_err(dev, "error to get regulator\n"); 271 - return PTR_ERR(ti_dac->vref); 272 - } 269 + if (IS_ERR(ti_dac->vref)) 270 + return dev_err_probe(dev, PTR_ERR(ti_dac->vref), 271 + "error to get regulator\n"); 273 272 274 273 ret = regulator_enable(ti_dac->vref); 275 274 if (ret < 0) {
-1
drivers/iio/gyro/Kconfig
··· 126 126 127 127 config MPU3050_I2C 128 128 tristate "Invensense MPU3050 devices on I2C" 129 - depends on !(INPUT_MPU3050=y || INPUT_MPU3050=m) 130 129 depends on I2C 131 130 select MPU3050 132 131 select REGMAP_I2C
+1 -10
drivers/iio/gyro/adis16080.c
··· 195 195 if (!indio_dev) 196 196 return -ENOMEM; 197 197 st = iio_priv(indio_dev); 198 - /* this is only used for removal purposes */ 199 - spi_set_drvdata(spi, indio_dev); 200 198 201 199 mutex_init(&st->lock); 202 200 ··· 208 210 indio_dev->info = &adis16080_info; 209 211 indio_dev->modes = INDIO_DIRECT_MODE; 210 212 211 - return iio_device_register(indio_dev); 212 - } 213 - 214 - static int adis16080_remove(struct spi_device *spi) 215 - { 216 - iio_device_unregister(spi_get_drvdata(spi)); 217 - return 0; 213 + return devm_iio_device_register(&spi->dev, indio_dev); 218 214 } 219 215 220 216 static const struct spi_device_id adis16080_ids[] = { ··· 223 231 .name = "adis16080", 224 232 }, 225 233 .probe = adis16080_probe, 226 - .remove = adis16080_remove, 227 234 .id_table = adis16080_ids, 228 235 }; 229 236 module_spi_driver(adis16080_driver);
+11 -13
drivers/iio/gyro/mpu3050-core.c
··· 471 471 struct iio_dev *indio_dev = pf->indio_dev; 472 472 struct mpu3050 *mpu3050 = iio_priv(indio_dev); 473 473 int ret; 474 - /* 475 - * Temperature 1*16 bits 476 - * Three axes 3*16 bits 477 - * Timestamp 64 bits (4*16 bits) 478 - * Sum total 8*16 bits 479 - */ 480 - __be16 hw_values[8]; 474 + struct { 475 + __be16 chans[4]; 476 + s64 timestamp __aligned(8); 477 + } scan; 481 478 s64 timestamp; 482 479 unsigned int datums_from_fifo = 0; 483 480 ··· 569 572 fifo_values[4]); 570 573 571 574 /* Index past the footer (fifo_values[0]) and push */ 572 - iio_push_to_buffers_with_timestamp(indio_dev, 573 - &fifo_values[1], 574 - timestamp); 575 + iio_push_to_buffers_with_ts_unaligned(indio_dev, 576 + &fifo_values[1], 577 + sizeof(__be16) * 4, 578 + timestamp); 575 579 576 580 fifocnt -= toread; 577 581 datums_from_fifo++; ··· 630 632 goto out_trigger_unlock; 631 633 } 632 634 633 - ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, &hw_values, 634 - sizeof(hw_values)); 635 + ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, scan.chans, 636 + sizeof(scan.chans)); 635 637 if (ret) { 636 638 dev_err(mpu3050->dev, 637 639 "error reading axis data\n"); 638 640 goto out_trigger_unlock; 639 641 } 640 642 641 - iio_push_to_buffers_with_timestamp(indio_dev, hw_values, timestamp); 643 + iio_push_to_buffers_with_timestamp(indio_dev, &scan, timestamp); 642 644 643 645 out_trigger_unlock: 644 646 mutex_unlock(&mpu3050->lock);
+3 -24
drivers/iio/gyro/st_gyro_core.c
··· 478 478 { 479 479 struct st_sensor_data *gdata = iio_priv(indio_dev); 480 480 struct st_sensors_platform_data *pdata; 481 + struct device *parent = indio_dev->dev.parent; 481 482 int err; 482 483 483 484 indio_dev->modes = INDIO_DIRECT_MODE; ··· 492 491 indio_dev->channels = gdata->sensor_settings->ch; 493 492 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 494 493 495 - err = iio_read_mount_matrix(gdata->dev, &gdata->mount_matrix); 494 + err = iio_read_mount_matrix(parent, &gdata->mount_matrix); 496 495 if (err) 497 496 return err; 498 497 ··· 516 515 return err; 517 516 } 518 517 519 - err = iio_device_register(indio_dev); 520 - if (err) 521 - goto st_gyro_device_register_error; 522 - 523 - dev_info(&indio_dev->dev, "registered gyroscope %s\n", 524 - indio_dev->name); 525 - 526 - return 0; 527 - 528 - st_gyro_device_register_error: 529 - if (gdata->irq > 0) 530 - st_sensors_deallocate_trigger(indio_dev); 531 - return err; 518 + return devm_iio_device_register(parent, indio_dev); 532 519 } 533 520 EXPORT_SYMBOL(st_gyro_common_probe); 534 - 535 - void st_gyro_common_remove(struct iio_dev *indio_dev) 536 - { 537 - struct st_sensor_data *gdata = iio_priv(indio_dev); 538 - 539 - iio_device_unregister(indio_dev); 540 - if (gdata->irq > 0) 541 - st_sensors_deallocate_trigger(indio_dev); 542 - } 543 - EXPORT_SYMBOL(st_gyro_common_remove); 544 521 545 522 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 546 523 MODULE_DESCRIPTION("STMicroelectronics gyroscopes driver");
+1 -22
drivers/iio/gyro/st_gyro_i2c.c
··· 90 90 if (err) 91 91 return err; 92 92 93 - err = st_gyro_common_probe(indio_dev); 94 - if (err < 0) 95 - goto st_gyro_power_off; 96 - 97 - return 0; 98 - 99 - st_gyro_power_off: 100 - st_sensors_power_disable(indio_dev); 101 - 102 - return err; 103 - } 104 - 105 - static int st_gyro_i2c_remove(struct i2c_client *client) 106 - { 107 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 108 - 109 - st_sensors_power_disable(indio_dev); 110 - 111 - st_gyro_common_remove(indio_dev); 112 - 113 - return 0; 93 + return st_gyro_common_probe(indio_dev); 114 94 } 115 95 116 96 static const struct i2c_device_id st_gyro_id_table[] = { ··· 113 133 .of_match_table = st_gyro_of_match, 114 134 }, 115 135 .probe = st_gyro_i2c_probe, 116 - .remove = st_gyro_i2c_remove, 117 136 .id_table = st_gyro_id_table, 118 137 }; 119 138 module_i2c_driver(st_gyro_driver);
+1 -22
drivers/iio/gyro/st_gyro_spi.c
··· 94 94 if (err) 95 95 return err; 96 96 97 - err = st_gyro_common_probe(indio_dev); 98 - if (err < 0) 99 - goto st_gyro_power_off; 100 - 101 - return 0; 102 - 103 - st_gyro_power_off: 104 - st_sensors_power_disable(indio_dev); 105 - 106 - return err; 107 - } 108 - 109 - static int st_gyro_spi_remove(struct spi_device *spi) 110 - { 111 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 112 - 113 - st_sensors_power_disable(indio_dev); 114 - 115 - st_gyro_common_remove(indio_dev); 116 - 117 - return 0; 97 + return st_gyro_common_probe(indio_dev); 118 98 } 119 99 120 100 static const struct spi_device_id st_gyro_id_table[] = { ··· 117 137 .of_match_table = st_gyro_of_match, 118 138 }, 119 139 .probe = st_gyro_spi_probe, 120 - .remove = st_gyro_spi_remove, 121 140 .id_table = st_gyro_id_table, 122 141 }; 123 142 module_spi_driver(st_gyro_driver);
+6 -8
drivers/iio/health/afe4403.c
··· 487 487 } 488 488 489 489 afe->regulator = devm_regulator_get(afe->dev, "tx_sup"); 490 - if (IS_ERR(afe->regulator)) { 491 - dev_err(afe->dev, "Unable to get regulator\n"); 492 - return PTR_ERR(afe->regulator); 493 - } 490 + if (IS_ERR(afe->regulator)) 491 + return dev_err_probe(afe->dev, PTR_ERR(afe->regulator), 492 + "Unable to get regulator\n"); 493 + 494 494 ret = regulator_enable(afe->regulator); 495 495 if (ret) { 496 496 dev_err(afe->dev, "Unable to enable regulator\n"); ··· 589 589 iio_trigger_unregister(afe->trig); 590 590 591 591 ret = regulator_disable(afe->regulator); 592 - if (ret) { 593 - dev_err(afe->dev, "Unable to disable regulator\n"); 594 - return ret; 595 - } 592 + if (ret) 593 + dev_warn(afe->dev, "Unable to disable regulator\n"); 596 594 597 595 return 0; 598 596 }
+4 -4
drivers/iio/health/afe4404.c
··· 494 494 } 495 495 496 496 afe->regulator = devm_regulator_get(afe->dev, "tx_sup"); 497 - if (IS_ERR(afe->regulator)) { 498 - dev_err(afe->dev, "Unable to get regulator\n"); 499 - return PTR_ERR(afe->regulator); 500 - } 497 + if (IS_ERR(afe->regulator)) 498 + return dev_err_probe(afe->dev, PTR_ERR(afe->regulator), 499 + "Unable to get regulator\n"); 500 + 501 501 ret = regulator_enable(afe->regulator); 502 502 if (ret) { 503 503 dev_err(afe->dev, "Unable to enable regulator\n");
+4
drivers/iio/iio_core.h
··· 68 68 struct poll_table_struct *wait); 69 69 ssize_t iio_buffer_read_wrapper(struct file *filp, char __user *buf, 70 70 size_t n, loff_t *f_ps); 71 + ssize_t iio_buffer_write_wrapper(struct file *filp, const char __user *buf, 72 + size_t n, loff_t *f_ps); 71 73 72 74 int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev); 73 75 void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev); 74 76 75 77 #define iio_buffer_poll_addr (&iio_buffer_poll_wrapper) 76 78 #define iio_buffer_read_outer_addr (&iio_buffer_read_wrapper) 79 + #define iio_buffer_write_outer_addr (&iio_buffer_write_wrapper) 77 80 78 81 void iio_disable_all_buffers(struct iio_dev *indio_dev); 79 82 void iio_buffer_wakeup_poll(struct iio_dev *indio_dev); ··· 86 83 87 84 #define iio_buffer_poll_addr NULL 88 85 #define iio_buffer_read_outer_addr NULL 86 + #define iio_buffer_write_outer_addr NULL 89 87 90 88 static inline int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev) 91 89 {
+16 -1
drivers/iio/imu/adis.c
··· 286 286 if (adis->data->enable_irq) { 287 287 ret = adis->data->enable_irq(adis, enable); 288 288 goto out_unlock; 289 + } else if (adis->data->unmasked_drdy) { 290 + if (enable) 291 + enable_irq(adis->spi->irq); 292 + else 293 + disable_irq(adis->spi->irq); 294 + 295 + goto out_unlock; 289 296 } 290 297 291 298 ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc); ··· 437 430 if (ret) 438 431 return ret; 439 432 433 + /* 434 + * don't bother calling this if we can't unmask the IRQ as in this case 435 + * the IRQ is most likely not yet requested and we will request it 436 + * with 'IRQF_NO_AUTOEN' anyways. 437 + */ 438 + if (!adis->data->unmasked_drdy) 439 + adis_enable_irq(adis, false); 440 + 440 441 if (!adis->data->prod_id_reg) 441 442 return 0; 442 443 ··· 541 526 adis->current_page = 0; 542 527 } 543 528 544 - return adis_enable_irq(adis, false); 529 + return 0; 545 530 } 546 531 EXPORT_SYMBOL_GPL(adis_init); 547 532
+15 -5
drivers/iio/imu/adis16400.c
··· 641 641 if (ret) 642 642 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret); 643 643 644 - if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) 644 + if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) { 645 645 buffer = adis->buffer + sizeof(u16); 646 - else 647 - buffer = adis->buffer; 646 + /* 647 + * The size here is always larger than, or equal to the true 648 + * size of the channel data. This may result in a larger copy 649 + * than necessary, but as the target buffer will be 650 + * buffer->scan_bytes this will be safe. 651 + */ 652 + iio_push_to_buffers_with_ts_unaligned(indio_dev, buffer, 653 + indio_dev->scan_bytes - sizeof(pf->timestamp), 654 + pf->timestamp); 655 + } else { 656 + iio_push_to_buffers_with_timestamp(indio_dev, 657 + adis->buffer, 658 + pf->timestamp); 659 + } 648 660 649 - iio_push_to_buffers_with_timestamp(indio_dev, buffer, 650 - pf->timestamp); 651 661 652 662 iio_trigger_notify_done(indio_dev->trig); 653 663
+1 -17
drivers/iio/imu/adis16460.c
··· 319 319 .debugfs_reg_access = adis_debugfs_reg_access, 320 320 }; 321 321 322 - static int adis16460_enable_irq(struct adis *adis, bool enable) 323 - { 324 - /* 325 - * There is no way to gate the data-ready signal internally inside the 326 - * ADIS16460 :( 327 - */ 328 - if (enable) 329 - enable_irq(adis->spi->irq); 330 - else 331 - disable_irq(adis->spi->irq); 332 - 333 - return 0; 334 - } 335 - 336 322 #define ADIS16460_DIAG_STAT_IN_CLK_OOS 7 337 323 #define ADIS16460_DIAG_STAT_FLASH_MEM 6 338 324 #define ADIS16460_DIAG_STAT_SELF_TEST 5 ··· 359 373 BIT(ADIS16460_DIAG_STAT_OVERRANGE) | 360 374 BIT(ADIS16460_DIAG_STAT_SPI_COMM) | 361 375 BIT(ADIS16460_DIAG_STAT_FLASH_UPT), 362 - .enable_irq = adis16460_enable_irq, 376 + .unmasked_drdy = true, 363 377 .timeouts = &adis16460_timeouts, 364 378 }; 365 379 ··· 386 400 if (ret) 387 401 return ret; 388 402 389 - /* We cannot mask the interrupt, so ensure it isn't auto enabled */ 390 - st->adis.irq_flag |= IRQF_NO_AUTOEN; 391 403 ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev, NULL); 392 404 if (ret) 393 405 return ret;
+1 -18
drivers/iio/imu/adis16475.c
··· 607 607 [ADIS16475_DIAG_STAT_CLK] = "Clock error", 608 608 }; 609 609 610 - static int adis16475_enable_irq(struct adis *adis, bool enable) 611 - { 612 - /* 613 - * There is no way to gate the data-ready signal internally inside the 614 - * ADIS16475. We can only control it's polarity... 615 - */ 616 - if (enable) 617 - enable_irq(adis->spi->irq); 618 - else 619 - disable_irq(adis->spi->irq); 620 - 621 - return 0; 622 - } 623 - 624 610 #define ADIS16475_DATA(_prod_id, _timeouts) \ 625 611 { \ 626 612 .msc_ctrl_reg = ADIS16475_REG_MSG_CTRL, \ ··· 627 641 BIT(ADIS16475_DIAG_STAT_SENSOR) | \ 628 642 BIT(ADIS16475_DIAG_STAT_MEMORY) | \ 629 643 BIT(ADIS16475_DIAG_STAT_CLK), \ 630 - .enable_irq = adis16475_enable_irq, \ 644 + .unmasked_drdy = true, \ 631 645 .timeouts = (_timeouts), \ 632 646 .burst_reg_cmd = ADIS16475_REG_GLOB_CMD, \ 633 647 .burst_len = ADIS16475_BURST_MAX_DATA, \ ··· 1240 1254 irq_type); 1241 1255 return -EINVAL; 1242 1256 } 1243 - 1244 - /* We cannot mask the interrupt so ensure it's not enabled at request */ 1245 - st->adis.irq_flag |= IRQF_NO_AUTOEN; 1246 1257 1247 1258 val = ADIS16475_MSG_CTRL_DR_POL(polarity); 1248 1259 ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL,
+4
drivers/iio/imu/adis_trigger.c
··· 30 30 static int adis_validate_irq_flag(struct adis *adis) 31 31 { 32 32 unsigned long direction = adis->irq_flag & IRQF_TRIGGER_MASK; 33 + 34 + /* We cannot mask the interrupt so ensure it's not enabled at request */ 35 + if (adis->data->unmasked_drdy) 36 + adis->irq_flag |= IRQF_NO_AUTOEN; 33 37 /* 34 38 * Typically this devices have data ready either on the rising edge or 35 39 * on the falling edge of the data ready pin. This checks enforces that
+1 -1
drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
··· 249 249 }; 250 250 MODULE_DEVICE_TABLE(of, inv_of_match); 251 251 252 - static const struct acpi_device_id inv_acpi_match[] = { 252 + static const struct acpi_device_id __maybe_unused inv_acpi_match[] = { 253 253 {"INVN6500", INV_MPU6500}, 254 254 { }, 255 255 };
+21 -15
drivers/iio/imu/inv_mpu6050/inv_mpu_magn.c
··· 261 261 */ 262 262 int inv_mpu_magn_set_orient(struct inv_mpu6050_state *st) 263 263 { 264 + struct device *dev = regmap_get_device(st->map); 264 265 const char *orient; 265 266 char *str; 266 267 int i; ··· 280 279 st->magn_orient.rotation[4] = st->orientation.rotation[1]; 281 280 st->magn_orient.rotation[5] = st->orientation.rotation[2]; 282 281 /* z <- -z */ 283 - for (i = 0; i < 3; ++i) { 284 - orient = st->orientation.rotation[6 + i]; 285 - /* use length + 2 for adding minus sign if needed */ 286 - str = devm_kzalloc(regmap_get_device(st->map), 287 - strlen(orient) + 2, GFP_KERNEL); 288 - if (str == NULL) 282 + for (i = 6; i < 9; ++i) { 283 + orient = st->orientation.rotation[i]; 284 + 285 + /* 286 + * The value is negated according to one of the following 287 + * rules: 288 + * 289 + * 1) Drop leading minus. 290 + * 2) Leave 0 as is. 291 + * 3) Add leading minus. 292 + */ 293 + if (orient[0] == '-') 294 + str = devm_kstrdup(dev, orient + 1, GFP_KERNEL); 295 + else if (!strcmp(orient, "0")) 296 + str = devm_kstrdup(dev, orient, GFP_KERNEL); 297 + else 298 + str = devm_kasprintf(dev, GFP_KERNEL, "-%s", orient); 299 + if (!str) 289 300 return -ENOMEM; 290 - if (strcmp(orient, "0") == 0) { 291 - strcpy(str, orient); 292 - } else if (orient[0] == '-') { 293 - strcpy(str, &orient[1]); 294 - } else { 295 - str[0] = '-'; 296 - strcpy(&str[1], orient); 297 - } 298 - st->magn_orient.rotation[6 + i] = str; 301 + 302 + st->magn_orient.rotation[i] = str; 299 303 } 300 304 break; 301 305 default:
+2 -2
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
··· 143 143 * @read_fifo: Read FIFO callback. 144 144 * @fifo_th: FIFO threshold register info (addr + mask). 145 145 * @fifo_diff: FIFO diff status register info (addr + mask). 146 + * @max_size: Sensor max fifo length in FIFO words. 146 147 * @th_wl: FIFO threshold word length. 147 148 */ 148 149 struct st_lsm6dsx_fifo_ops { ··· 157 156 u8 addr; 158 157 u16 mask; 159 158 } fifo_diff; 159 + u16 max_size; 160 160 u8 th_wl; 161 161 }; 162 162 ··· 273 271 * @reset: register address for reset. 274 272 * @boot: register address for boot. 275 273 * @bdu: register address for Block Data Update. 276 - * @max_fifo_size: Sensor max fifo length in FIFO words. 277 274 * @id: List of hw id/device name supported by the driver configuration. 278 275 * @channels: IIO channels supported by the device. 279 276 * @irq_config: interrupts related registers. ··· 289 288 struct st_lsm6dsx_reg reset; 290 289 struct st_lsm6dsx_reg boot; 291 290 struct st_lsm6dsx_reg bdu; 292 - u16 max_fifo_size; 293 291 struct { 294 292 enum st_lsm6dsx_hw_id hw_id; 295 293 const char *name;
+9 -7
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
··· 102 102 .addr = 0x22, 103 103 .mask = BIT(6), 104 104 }, 105 - .max_fifo_size = 32, 106 105 .id = { 107 106 { 108 107 .hw_id = ST_LSM9DS1_ID, ··· 193 194 .mask = BIT(4), 194 195 }, 195 196 }, 197 + .fifo_ops = { 198 + .max_size = 32, 199 + }, 196 200 }, 197 201 { 198 202 .reset = { ··· 210 208 .addr = 0x12, 211 209 .mask = BIT(6), 212 210 }, 213 - .max_fifo_size = 1365, 214 211 .id = { 215 212 { 216 213 .hw_id = ST_LSM6DS3_ID, ··· 330 329 .addr = 0x3a, 331 330 .mask = GENMASK(11, 0), 332 331 }, 332 + .max_size = 1365, 333 333 .th_wl = 3, /* 1LSB = 2B */ 334 334 }, 335 335 .ts_settings = { ··· 376 374 .addr = 0x12, 377 375 .mask = BIT(6), 378 376 }, 379 - .max_fifo_size = 682, 380 377 .id = { 381 378 { 382 379 .hw_id = ST_LSM6DS3H_ID, ··· 496 495 .addr = 0x3a, 497 496 .mask = GENMASK(11, 0), 498 497 }, 498 + .max_size = 682, 499 499 .th_wl = 3, /* 1LSB = 2B */ 500 500 }, 501 501 .ts_settings = { ··· 542 540 .addr = 0x12, 543 541 .mask = BIT(6), 544 542 }, 545 - .max_fifo_size = 682, 546 543 .id = { 547 544 { 548 545 .hw_id = ST_LSM6DSL_ID, ··· 678 677 .addr = 0x3a, 679 678 .mask = GENMASK(10, 0), 680 679 }, 680 + .max_size = 682, 681 681 .th_wl = 3, /* 1LSB = 2B */ 682 682 }, 683 683 .ts_settings = { ··· 761 759 .addr = 0x12, 762 760 .mask = BIT(6), 763 761 }, 764 - .max_fifo_size = 512, 765 762 .id = { 766 763 { 767 764 .hw_id = ST_LSM6DSR_ID, ··· 911 910 .addr = 0x3a, 912 911 .mask = GENMASK(9, 0), 913 912 }, 913 + .max_size = 512, 914 914 .th_wl = 1, 915 915 }, 916 916 .ts_settings = { ··· 986 984 .addr = 0x12, 987 985 .mask = BIT(6), 988 986 }, 989 - .max_fifo_size = 512, 990 987 .id = { 991 988 { 992 989 .hw_id = ST_ASM330LHH_ID, ··· 1120 1119 .addr = 0x3a, 1121 1120 .mask = GENMASK(9, 0), 1122 1121 }, 1122 + .max_size = 512, 1123 1123 .th_wl = 1, 1124 1124 }, 1125 1125 .ts_settings = { ··· 1605 1603 struct st_lsm6dsx_hw *hw = sensor->hw; 1606 1604 int err; 1607 1605 1608 - if (val < 1 || val > hw->settings->max_fifo_size) 1606 + if (val < 1 || val > hw->settings->fifo_ops.max_size) 1609 1607 return -EINVAL; 1610 1608 1611 1609 mutex_lock(&hw->conf_lock);
-1
drivers/iio/imu/st_lsm9ds0/st_lsm9ds0.h
··· 18 18 }; 19 19 20 20 int st_lsm9ds0_probe(struct st_lsm9ds0 *lsm9ds0, struct regmap *regmap); 21 - int st_lsm9ds0_remove(struct st_lsm9ds0 *lsm9ds0); 22 21 23 22 #endif /* ST_LSM9DS0_H */
+7 -22
drivers/iio/imu/st_lsm9ds0/st_lsm9ds0_core.c
··· 24 24 25 25 /* Regulators not mandatory, but if requested we should enable them. */ 26 26 lsm9ds0->vdd = devm_regulator_get(dev, "vdd"); 27 - if (IS_ERR(lsm9ds0->vdd)) { 28 - dev_err(dev, "unable to get Vdd supply\n"); 29 - return PTR_ERR(lsm9ds0->vdd); 30 - } 27 + if (IS_ERR(lsm9ds0->vdd)) 28 + return dev_err_probe(dev, PTR_ERR(lsm9ds0->vdd), 29 + "unable to get Vdd supply\n"); 30 + 31 31 ret = regulator_enable(lsm9ds0->vdd); 32 32 if (ret) { 33 33 dev_warn(dev, "Failed to enable specified Vdd supply\n"); ··· 36 36 37 37 lsm9ds0->vdd_io = devm_regulator_get(dev, "vddio"); 38 38 if (IS_ERR(lsm9ds0->vdd_io)) { 39 - dev_err(dev, "unable to get Vdd_IO supply\n"); 40 39 regulator_disable(lsm9ds0->vdd); 41 - return PTR_ERR(lsm9ds0->vdd_io); 40 + return dev_err_probe(dev, PTR_ERR(lsm9ds0->vdd_io), 41 + "unable to get Vdd_IO supply\n"); 42 42 } 43 43 ret = regulator_enable(lsm9ds0->vdd_io); 44 44 if (ret) { ··· 90 90 91 91 data = iio_priv(lsm9ds0->accel); 92 92 data->sensor_settings = (struct st_sensor_settings *)settings; 93 - data->dev = dev; 94 93 data->irq = lsm9ds0->irq; 95 94 data->regmap = regmap; 96 95 data->vdd = lsm9ds0->vdd; ··· 118 119 119 120 data = iio_priv(lsm9ds0->magn); 120 121 data->sensor_settings = (struct st_sensor_settings *)settings; 121 - data->dev = dev; 122 122 data->irq = lsm9ds0->irq; 123 123 data->regmap = regmap; 124 124 data->vdd = lsm9ds0->vdd; ··· 140 142 return ret; 141 143 142 144 /* Setup magnetometer device */ 143 - ret = st_lsm9ds0_probe_magn(lsm9ds0, regmap); 144 - if (ret) 145 - st_accel_common_remove(lsm9ds0->accel); 146 - 147 - return ret; 145 + return st_lsm9ds0_probe_magn(lsm9ds0, regmap); 148 146 } 149 147 EXPORT_SYMBOL_GPL(st_lsm9ds0_probe); 150 - 151 - int st_lsm9ds0_remove(struct st_lsm9ds0 *lsm9ds0) 152 - { 153 - st_magn_common_remove(lsm9ds0->magn); 154 - st_accel_common_remove(lsm9ds0->accel); 155 - 156 - return 0; 157 - } 158 - EXPORT_SYMBOL_GPL(st_lsm9ds0_remove); 159 148 160 149 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>"); 161 150 MODULE_DESCRIPTION("STMicroelectronics LSM9DS0 IMU core driver");
-6
drivers/iio/imu/st_lsm9ds0/st_lsm9ds0_i2c.c
··· 64 64 return st_lsm9ds0_probe(lsm9ds0, regmap); 65 65 } 66 66 67 - static int st_lsm9ds0_i2c_remove(struct i2c_client *client) 68 - { 69 - return st_lsm9ds0_remove(i2c_get_clientdata(client)); 70 - } 71 - 72 67 static struct i2c_driver st_lsm9ds0_driver = { 73 68 .driver = { 74 69 .name = "st-lsm9ds0-i2c", 75 70 .of_match_table = st_lsm9ds0_of_match, 76 71 }, 77 72 .probe_new = st_lsm9ds0_i2c_probe, 78 - .remove = st_lsm9ds0_i2c_remove, 79 73 .id_table = st_lsm9ds0_id_table, 80 74 }; 81 75 module_i2c_driver(st_lsm9ds0_driver);
-6
drivers/iio/imu/st_lsm9ds0/st_lsm9ds0_spi.c
··· 63 63 return st_lsm9ds0_probe(lsm9ds0, regmap); 64 64 } 65 65 66 - static int st_lsm9ds0_spi_remove(struct spi_device *spi) 67 - { 68 - return st_lsm9ds0_remove(spi_get_drvdata(spi)); 69 - } 70 - 71 66 static struct spi_driver st_lsm9ds0_driver = { 72 67 .driver = { 73 68 .name = "st-lsm9ds0-spi", 74 69 .of_match_table = st_lsm9ds0_of_match, 75 70 }, 76 71 .probe = st_lsm9ds0_spi_probe, 77 - .remove = st_lsm9ds0_spi_remove, 78 72 .id_table = st_lsm9ds0_id_table, 79 73 }; 80 74 module_spi_driver(st_lsm9ds0_driver);
+171 -2
drivers/iio/industrialio-buffer.c
··· 120 120 if (!rb || !rb->access->read) 121 121 return -EINVAL; 122 122 123 + if (rb->direction != IIO_BUFFER_DIRECTION_IN) 124 + return -EPERM; 125 + 123 126 datum_size = rb->bytes_per_datum; 124 127 125 128 /* ··· 164 161 return ret; 165 162 } 166 163 164 + static size_t iio_buffer_space_available(struct iio_buffer *buf) 165 + { 166 + if (buf->access->space_available) 167 + return buf->access->space_available(buf); 168 + 169 + return SIZE_MAX; 170 + } 171 + 172 + static ssize_t iio_buffer_write(struct file *filp, const char __user *buf, 173 + size_t n, loff_t *f_ps) 174 + { 175 + struct iio_dev_buffer_pair *ib = filp->private_data; 176 + struct iio_buffer *rb = ib->buffer; 177 + struct iio_dev *indio_dev = ib->indio_dev; 178 + DEFINE_WAIT_FUNC(wait, woken_wake_function); 179 + int ret; 180 + size_t written; 181 + 182 + if (!indio_dev->info) 183 + return -ENODEV; 184 + 185 + if (!rb || !rb->access->write) 186 + return -EINVAL; 187 + 188 + if (rb->direction != IIO_BUFFER_DIRECTION_OUT) 189 + return -EPERM; 190 + 191 + written = 0; 192 + add_wait_queue(&rb->pollq, &wait); 193 + do { 194 + if (indio_dev->info == NULL) 195 + return -ENODEV; 196 + 197 + if (!iio_buffer_space_available(rb)) { 198 + if (signal_pending(current)) { 199 + ret = -ERESTARTSYS; 200 + break; 201 + } 202 + 203 + wait_woken(&wait, TASK_INTERRUPTIBLE, 204 + MAX_SCHEDULE_TIMEOUT); 205 + continue; 206 + } 207 + 208 + ret = rb->access->write(rb, n - written, buf + written); 209 + if (ret == 0 && (filp->f_flags & O_NONBLOCK)) 210 + ret = -EAGAIN; 211 + 212 + if (ret > 0) { 213 + written += ret; 214 + if (written != n && !(filp->f_flags & O_NONBLOCK)) 215 + continue; 216 + } 217 + } while (ret == 0); 218 + remove_wait_queue(&rb->pollq, &wait); 219 + 220 + return ret < 0 ? ret : n; 221 + } 222 + 167 223 /** 168 224 * iio_buffer_poll() - poll the buffer to find out if it has data 169 225 * @filp: File structure pointer for device access ··· 243 181 return 0; 244 182 245 183 poll_wait(filp, &rb->pollq, wait); 246 - if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0)) 247 - return EPOLLIN | EPOLLRDNORM; 184 + 185 + switch (rb->direction) { 186 + case IIO_BUFFER_DIRECTION_IN: 187 + if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0)) 188 + return EPOLLIN | EPOLLRDNORM; 189 + break; 190 + case IIO_BUFFER_DIRECTION_OUT: 191 + if (iio_buffer_space_available(rb)) 192 + return EPOLLOUT | EPOLLWRNORM; 193 + break; 194 + } 195 + 248 196 return 0; 249 197 } 250 198 ··· 269 197 return -EBUSY; 270 198 271 199 return iio_buffer_read(filp, buf, n, f_ps); 200 + } 201 + 202 + ssize_t iio_buffer_write_wrapper(struct file *filp, const char __user *buf, 203 + size_t n, loff_t *f_ps) 204 + { 205 + struct iio_dev_buffer_pair *ib = filp->private_data; 206 + struct iio_buffer *rb = ib->buffer; 207 + 208 + /* check if buffer was opened through new API */ 209 + if (test_bit(IIO_BUSY_BIT_POS, &rb->flags)) 210 + return -EBUSY; 211 + 212 + return iio_buffer_write(filp, buf, n, f_ps); 272 213 } 273 214 274 215 __poll_t iio_buffer_poll_wrapper(struct file *filp, ··· 315 230 wake_up(&buffer->pollq); 316 231 } 317 232 } 233 + 234 + int iio_pop_from_buffer(struct iio_buffer *buffer, void *data) 235 + { 236 + if (!buffer || !buffer->access || !buffer->access->remove_from) 237 + return -EINVAL; 238 + 239 + return buffer->access->remove_from(buffer, data); 240 + } 241 + EXPORT_SYMBOL_GPL(iio_pop_from_buffer); 318 242 319 243 void iio_buffer_init(struct iio_buffer *buffer) 320 244 { ··· 1250 1156 if (insert_buffer == remove_buffer) 1251 1157 return 0; 1252 1158 1159 + if (insert_buffer && 1160 + (insert_buffer->direction == IIO_BUFFER_DIRECTION_OUT)) 1161 + return -EINVAL; 1162 + 1253 1163 mutex_lock(&iio_dev_opaque->info_exist_lock); 1254 1164 mutex_lock(&indio_dev->mlock); 1255 1165 ··· 1375 1277 return sysfs_emit(buf, "%zu\n", iio_buffer_data_available(buffer)); 1376 1278 } 1377 1279 1280 + static ssize_t direction_show(struct device *dev, 1281 + struct device_attribute *attr, 1282 + char *buf) 1283 + { 1284 + struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer; 1285 + 1286 + switch (buffer->direction) { 1287 + case IIO_BUFFER_DIRECTION_IN: 1288 + return sprintf(buf, "in\n"); 1289 + case IIO_BUFFER_DIRECTION_OUT: 1290 + return sprintf(buf, "out\n"); 1291 + default: 1292 + return -EINVAL; 1293 + } 1294 + } 1295 + 1378 1296 static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, 1379 1297 iio_buffer_write_length); 1380 1298 static struct device_attribute dev_attr_length_ro = __ATTR(length, ··· 1403 1289 S_IRUGO, iio_buffer_show_watermark, NULL); 1404 1290 static DEVICE_ATTR(data_available, S_IRUGO, 1405 1291 iio_dma_show_data_available, NULL); 1292 + static DEVICE_ATTR_RO(direction); 1406 1293 1294 + /* 1295 + * When adding new attributes here, put the at the end, at least until 1296 + * the code that handles the length/length_ro & watermark/watermark_ro 1297 + * assignments gets cleaned up. Otherwise these can create some weird 1298 + * duplicate attributes errors under some setups. 1299 + */ 1407 1300 static struct attribute *iio_buffer_attrs[] = { 1408 1301 &dev_attr_length.attr, 1409 1302 &dev_attr_enable.attr, 1410 1303 &dev_attr_watermark.attr, 1411 1304 &dev_attr_data_available.attr, 1305 + &dev_attr_direction.attr, 1412 1306 }; 1413 1307 1414 1308 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) ··· 1519 1397 .owner = THIS_MODULE, 1520 1398 .llseek = noop_llseek, 1521 1399 .read = iio_buffer_read, 1400 + .write = iio_buffer_write, 1522 1401 .poll = iio_buffer_poll, 1523 1402 .release = iio_buffer_chrdev_release, 1524 1403 }; ··· 1853 1730 return 0; 1854 1731 } 1855 1732 EXPORT_SYMBOL_GPL(iio_push_to_buffers); 1733 + 1734 + /** 1735 + * iio_push_to_buffers_with_ts_unaligned() - push to registered buffer, 1736 + * no alignment or space requirements. 1737 + * @indio_dev: iio_dev structure for device. 1738 + * @data: channel data excluding the timestamp. 1739 + * @data_sz: size of data. 1740 + * @timestamp: timestamp for the sample data. 1741 + * 1742 + * This special variant of iio_push_to_buffers_with_timestamp() does 1743 + * not require space for the timestamp, or 8 byte alignment of data. 1744 + * It does however require an allocation on first call and additional 1745 + * copies on all calls, so should be avoided if possible. 1746 + */ 1747 + int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, 1748 + const void *data, 1749 + size_t data_sz, 1750 + int64_t timestamp) 1751 + { 1752 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1753 + 1754 + /* 1755 + * Conservative estimate - we can always safely copy the minimum 1756 + * of either the data provided or the length of the destination buffer. 1757 + * This relaxed limit allows the calling drivers to be lax about 1758 + * tracking the size of the data they are pushing, at the cost of 1759 + * unnecessary copying of padding. 1760 + */ 1761 + data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz); 1762 + if (iio_dev_opaque->bounce_buffer_size != indio_dev->scan_bytes) { 1763 + void *bb; 1764 + 1765 + bb = devm_krealloc(&indio_dev->dev, 1766 + iio_dev_opaque->bounce_buffer, 1767 + indio_dev->scan_bytes, GFP_KERNEL); 1768 + if (!bb) 1769 + return -ENOMEM; 1770 + iio_dev_opaque->bounce_buffer = bb; 1771 + iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes; 1772 + } 1773 + memcpy(iio_dev_opaque->bounce_buffer, data, data_sz); 1774 + return iio_push_to_buffers_with_timestamp(indio_dev, 1775 + iio_dev_opaque->bounce_buffer, 1776 + timestamp); 1777 + } 1778 + EXPORT_SYMBOL_GPL(iio_push_to_buffers_with_ts_unaligned); 1856 1779 1857 1780 /** 1858 1781 * iio_buffer_release() - Free a buffer's resources
+1
drivers/iio/industrialio-core.c
··· 1822 1822 .owner = THIS_MODULE, 1823 1823 .llseek = noop_llseek, 1824 1824 .read = iio_buffer_read_outer_addr, 1825 + .write = iio_buffer_write_outer_addr, 1825 1826 .poll = iio_buffer_poll_addr, 1826 1827 .unlocked_ioctl = iio_ioctl, 1827 1828 .compat_ioctl = compat_ptr_ioctl,
+17
drivers/iio/inkern.c
··· 85 85 } 86 86 EXPORT_SYMBOL_GPL(iio_map_array_unregister); 87 87 88 + static void iio_map_array_unregister_cb(void *indio_dev) 89 + { 90 + iio_map_array_unregister(indio_dev); 91 + } 92 + 93 + int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, struct iio_map *maps) 94 + { 95 + int ret; 96 + 97 + ret = iio_map_array_register(indio_dev, maps); 98 + if (ret) 99 + return ret; 100 + 101 + return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev); 102 + } 103 + EXPORT_SYMBOL_GPL(devm_iio_map_array_register); 104 + 88 105 static const struct iio_chan_spec 89 106 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name) 90 107 {
+3 -4
drivers/iio/light/cm36651.c
··· 632 632 cm36651 = iio_priv(indio_dev); 633 633 634 634 cm36651->vled_reg = devm_regulator_get(&client->dev, "vled"); 635 - if (IS_ERR(cm36651->vled_reg)) { 636 - dev_err(&client->dev, "get regulator vled failed\n"); 637 - return PTR_ERR(cm36651->vled_reg); 638 - } 635 + if (IS_ERR(cm36651->vled_reg)) 636 + return dev_err_probe(&client->dev, PTR_ERR(cm36651->vled_reg), 637 + "get regulator vled failed\n"); 639 638 640 639 ret = regulator_enable(cm36651->vled_reg); 641 640 if (ret) {
+3 -14
drivers/iio/light/max44000.c
··· 540 540 return PTR_ERR(data->regmap); 541 541 } 542 542 543 - i2c_set_clientdata(client, indio_dev); 544 543 mutex_init(&data->lock); 545 544 indio_dev->info = &max44000_info; 546 545 indio_dev->name = MAX44000_DRV_NAME; ··· 588 589 return ret; 589 590 } 590 591 591 - ret = iio_triggered_buffer_setup(indio_dev, NULL, max44000_trigger_handler, NULL); 592 + ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, 593 + max44000_trigger_handler, NULL); 592 594 if (ret < 0) { 593 595 dev_err(&client->dev, "iio triggered buffer setup failed\n"); 594 596 return ret; 595 597 } 596 598 597 - return iio_device_register(indio_dev); 598 - } 599 - 600 - static int max44000_remove(struct i2c_client *client) 601 - { 602 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 603 - 604 - iio_device_unregister(indio_dev); 605 - iio_triggered_buffer_cleanup(indio_dev); 606 - 607 - return 0; 599 + return devm_iio_device_register(&client->dev, indio_dev); 608 600 } 609 601 610 602 static const struct i2c_device_id max44000_id[] = { ··· 618 628 .acpi_match_table = ACPI_PTR(max44000_acpi_match), 619 629 }, 620 630 .probe = max44000_probe, 621 - .remove = max44000_remove, 622 631 .id_table = max44000_id, 623 632 }; 624 633
+3 -4
drivers/iio/light/noa1305.c
··· 217 217 priv = iio_priv(indio_dev); 218 218 219 219 priv->vin_reg = devm_regulator_get(&client->dev, "vin"); 220 - if (IS_ERR(priv->vin_reg)) { 221 - dev_err(&client->dev, "get regulator vin failed\n"); 222 - return PTR_ERR(priv->vin_reg); 223 - } 220 + if (IS_ERR(priv->vin_reg)) 221 + return dev_err_probe(&client->dev, PTR_ERR(priv->vin_reg), 222 + "get regulator vin failed\n"); 224 223 225 224 ret = regulator_enable(priv->vin_reg); 226 225 if (ret) {
+1 -1
drivers/iio/magnetometer/Kconfig
··· 28 28 select IIO_TRIGGERED_BUFFER 29 29 help 30 30 Say yes here to build support for Asahi Kasei AK8975, AK8963, 31 - AK09911 or AK09912 3-Axis Magnetometer. 31 + AK09911, AK09912 or AK09916 3-Axis Magnetometer. 32 32 33 33 To compile this driver as a module, choose M here: the module 34 34 will be called ak8975.
+35
drivers/iio/magnetometer/ak8975.c
··· 78 78 */ 79 79 #define AK09912_REG_WIA1 0x00 80 80 #define AK09912_REG_WIA2 0x01 81 + #define AK09916_DEVICE_ID 0x09 81 82 #define AK09912_DEVICE_ID 0x04 82 83 #define AK09911_DEVICE_ID 0x05 83 84 ··· 209 208 AK8963, 210 209 AK09911, 211 210 AK09912, 211 + AK09916, 212 212 }; 213 213 214 214 enum ak_ctrl_reg_addr { ··· 347 345 AK09912_REG_HXL, 348 346 AK09912_REG_HYL, 349 347 AK09912_REG_HZL}, 348 + }, 349 + { 350 + .type = AK09916, 351 + .raw_to_gauss = ak09912_raw_to_gauss, 352 + .range = 32752, 353 + .ctrl_regs = { 354 + AK09912_REG_ST1, 355 + AK09912_REG_ST2, 356 + AK09912_REG_CNTL2, 357 + AK09912_REG_ASAX, 358 + AK09912_MAX_REGS}, 359 + .ctrl_masks = { 360 + AK09912_REG_ST1_DRDY_MASK, 361 + AK09912_REG_ST2_HOFL_MASK, 362 + 0, 363 + AK09912_REG_CNTL2_MODE_MASK}, 364 + .ctrl_modes = { 365 + AK09912_REG_CNTL_MODE_POWER_DOWN, 366 + AK09912_REG_CNTL_MODE_ONCE, 367 + AK09912_REG_CNTL_MODE_SELF_TEST, 368 + AK09912_REG_CNTL_MODE_FUSE_ROM}, 369 + .data_regs = { 370 + AK09912_REG_HXL, 371 + AK09912_REG_HYL, 372 + AK09912_REG_HZL}, 350 373 } 351 374 }; 352 375 ··· 452 425 /* 453 426 * Signature for each device: 454 427 * Device | WIA1 | WIA2 428 + * AK09916 | DEVICE_ID_| AK09916_DEVICE_ID 455 429 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID 456 430 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID 457 431 * AK8975 | DEVICE_ID | NA ··· 478 450 break; 479 451 case AK09912: 480 452 if (wia_val[1] == AK09912_DEVICE_ID) 453 + return 0; 454 + break; 455 + case AK09916: 456 + if (wia_val[1] == AK09916_DEVICE_ID) 481 457 return 0; 482 458 break; 483 459 default: ··· 1089 1057 {"AK8963", AK8963}, 1090 1058 {"ak09911", AK09911}, 1091 1059 {"ak09912", AK09912}, 1060 + {"ak09916", AK09916}, 1092 1061 {} 1093 1062 }; 1094 1063 ··· 1104 1071 { .compatible = "ak09911", }, 1105 1072 { .compatible = "asahi-kasei,ak09912", }, 1106 1073 { .compatible = "ak09912", }, 1074 + { .compatible = "asahi-kasei,ak09916", }, 1075 + { .compatible = "ak09916", }, 1107 1076 {} 1108 1077 }; 1109 1078 MODULE_DEVICE_TABLE(of, ak8975_of_match);
+1 -1
drivers/iio/magnetometer/hmc5843.h
··· 50 50 51 51 int hmc5843_common_probe(struct device *dev, struct regmap *regmap, 52 52 enum hmc5843_ids id, const char *name); 53 - int hmc5843_common_remove(struct device *dev); 53 + void hmc5843_common_remove(struct device *dev); 54 54 55 55 int hmc5843_common_suspend(struct device *dev); 56 56 int hmc5843_common_resume(struct device *dev);
+1 -3
drivers/iio/magnetometer/hmc5843_core.c
··· 671 671 } 672 672 EXPORT_SYMBOL(hmc5843_common_probe); 673 673 674 - int hmc5843_common_remove(struct device *dev) 674 + void hmc5843_common_remove(struct device *dev) 675 675 { 676 676 struct iio_dev *indio_dev = dev_get_drvdata(dev); 677 677 ··· 680 680 681 681 /* sleep mode to save power */ 682 682 hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP); 683 - 684 - return 0; 685 683 } 686 684 EXPORT_SYMBOL(hmc5843_common_remove); 687 685
+3 -1
drivers/iio/magnetometer/hmc5843_i2c.c
··· 67 67 68 68 static int hmc5843_i2c_remove(struct i2c_client *client) 69 69 { 70 - return hmc5843_common_remove(&client->dev); 70 + hmc5843_common_remove(&client->dev); 71 + 72 + return 0; 71 73 } 72 74 73 75 static const struct i2c_device_id hmc5843_id[] = {
+3 -1
drivers/iio/magnetometer/hmc5843_spi.c
··· 76 76 77 77 static int hmc5843_spi_remove(struct spi_device *spi) 78 78 { 79 - return hmc5843_common_remove(&spi->dev); 79 + hmc5843_common_remove(&spi->dev); 80 + 81 + return 0; 80 82 } 81 83 82 84 static const struct spi_device_id hmc5843_id[] = {
+4 -25
drivers/iio/magnetometer/st_magn_core.c
··· 611 611 int st_magn_common_probe(struct iio_dev *indio_dev) 612 612 { 613 613 struct st_sensor_data *mdata = iio_priv(indio_dev); 614 - struct st_sensors_platform_data *pdata = dev_get_platdata(mdata->dev); 614 + struct device *parent = indio_dev->dev.parent; 615 + struct st_sensors_platform_data *pdata = dev_get_platdata(parent); 615 616 int err; 616 617 617 618 indio_dev->modes = INDIO_DIRECT_MODE; ··· 626 625 indio_dev->channels = mdata->sensor_settings->ch; 627 626 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 628 627 629 - err = iio_read_mount_matrix(mdata->dev, &mdata->mount_matrix); 628 + err = iio_read_mount_matrix(parent, &mdata->mount_matrix); 630 629 if (err) 631 630 return err; 632 631 ··· 651 650 return err; 652 651 } 653 652 654 - err = iio_device_register(indio_dev); 655 - if (err) 656 - goto st_magn_device_register_error; 657 - 658 - dev_info(&indio_dev->dev, "registered magnetometer %s\n", 659 - indio_dev->name); 660 - 661 - return 0; 662 - 663 - st_magn_device_register_error: 664 - if (mdata->irq > 0) 665 - st_sensors_deallocate_trigger(indio_dev); 666 - return err; 653 + return devm_iio_device_register(parent, indio_dev); 667 654 } 668 655 EXPORT_SYMBOL(st_magn_common_probe); 669 - 670 - void st_magn_common_remove(struct iio_dev *indio_dev) 671 - { 672 - struct st_sensor_data *mdata = iio_priv(indio_dev); 673 - 674 - iio_device_unregister(indio_dev); 675 - if (mdata->irq > 0) 676 - st_sensors_deallocate_trigger(indio_dev); 677 - } 678 - EXPORT_SYMBOL(st_magn_common_remove); 679 656 680 657 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 681 658 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
+1 -22
drivers/iio/magnetometer/st_magn_i2c.c
··· 86 86 if (err) 87 87 return err; 88 88 89 - err = st_magn_common_probe(indio_dev); 90 - if (err < 0) 91 - goto st_magn_power_off; 92 - 93 - return 0; 94 - 95 - st_magn_power_off: 96 - st_sensors_power_disable(indio_dev); 97 - 98 - return err; 99 - } 100 - 101 - static int st_magn_i2c_remove(struct i2c_client *client) 102 - { 103 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 104 - 105 - st_sensors_power_disable(indio_dev); 106 - 107 - st_magn_common_remove(indio_dev); 108 - 109 - return 0; 89 + return st_magn_common_probe(indio_dev); 110 90 } 111 91 112 92 static const struct i2c_device_id st_magn_id_table[] = { ··· 108 128 .of_match_table = st_magn_of_match, 109 129 }, 110 130 .probe = st_magn_i2c_probe, 111 - .remove = st_magn_i2c_remove, 112 131 .id_table = st_magn_id_table, 113 132 }; 114 133 module_i2c_driver(st_magn_driver);
+1 -22
drivers/iio/magnetometer/st_magn_spi.c
··· 80 80 if (err) 81 81 return err; 82 82 83 - err = st_magn_common_probe(indio_dev); 84 - if (err < 0) 85 - goto st_magn_power_off; 86 - 87 - return 0; 88 - 89 - st_magn_power_off: 90 - st_sensors_power_disable(indio_dev); 91 - 92 - return err; 93 - } 94 - 95 - static int st_magn_spi_remove(struct spi_device *spi) 96 - { 97 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 98 - 99 - st_sensors_power_disable(indio_dev); 100 - 101 - st_magn_common_remove(indio_dev); 102 - 103 - return 0; 83 + return st_magn_common_probe(indio_dev); 104 84 } 105 85 106 86 static const struct spi_device_id st_magn_id_table[] = { ··· 99 119 .of_match_table = st_magn_of_match, 100 120 }, 101 121 .probe = st_magn_spi_probe, 102 - .remove = st_magn_spi_remove, 103 122 .id_table = st_magn_id_table, 104 123 }; 105 124 module_spi_driver(st_magn_driver);
+6 -1
drivers/iio/potentiometer/max5487.c
··· 115 115 static int max5487_spi_remove(struct spi_device *spi) 116 116 { 117 117 struct iio_dev *indio_dev = spi_get_drvdata(spi); 118 + int ret; 118 119 119 120 iio_device_unregister(indio_dev); 120 121 121 122 /* save both wiper regs to NV regs */ 122 - return max5487_write_cmd(spi, MAX5487_COPY_AB_TO_NV); 123 + ret = max5487_write_cmd(spi, MAX5487_COPY_AB_TO_NV); 124 + if (ret) 125 + dev_warn(&spi->dev, "Failed to save wiper regs to NV regs\n"); 126 + 127 + return 0; 123 128 } 124 129 125 130 static const struct spi_device_id max5487_id[] = {
+1 -1
drivers/iio/pressure/ms5611.h
··· 61 61 62 62 int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, 63 63 const char *name, int type); 64 - int ms5611_remove(struct iio_dev *indio_dev); 64 + void ms5611_remove(struct iio_dev *indio_dev); 65 65 66 66 #endif /* _MS5611_H */
+1 -3
drivers/iio/pressure/ms5611_core.c
··· 474 474 } 475 475 EXPORT_SYMBOL(ms5611_probe); 476 476 477 - int ms5611_remove(struct iio_dev *indio_dev) 477 + void ms5611_remove(struct iio_dev *indio_dev) 478 478 { 479 479 iio_device_unregister(indio_dev); 480 480 iio_triggered_buffer_cleanup(indio_dev); 481 481 ms5611_fini(indio_dev); 482 - 483 - return 0; 484 482 } 485 483 EXPORT_SYMBOL(ms5611_remove); 486 484
+3 -1
drivers/iio/pressure/ms5611_i2c.c
··· 110 110 111 111 static int ms5611_i2c_remove(struct i2c_client *client) 112 112 { 113 - return ms5611_remove(i2c_get_clientdata(client)); 113 + ms5611_remove(i2c_get_clientdata(client)); 114 + 115 + return 0; 114 116 } 115 117 116 118 static const struct of_device_id ms5611_i2c_matches[] = {
+3 -1
drivers/iio/pressure/ms5611_spi.c
··· 112 112 113 113 static int ms5611_spi_remove(struct spi_device *spi) 114 114 { 115 - return ms5611_remove(spi_get_drvdata(spi)); 115 + ms5611_remove(spi_get_drvdata(spi)); 116 + 117 + return 0; 116 118 } 117 119 118 120 static const struct of_device_id ms5611_spi_matches[] = {
+3 -24
drivers/iio/pressure/st_pressure_core.c
··· 677 677 int st_press_common_probe(struct iio_dev *indio_dev) 678 678 { 679 679 struct st_sensor_data *press_data = iio_priv(indio_dev); 680 - struct st_sensors_platform_data *pdata = dev_get_platdata(press_data->dev); 680 + struct device *parent = indio_dev->dev.parent; 681 + struct st_sensors_platform_data *pdata = dev_get_platdata(parent); 681 682 int err; 682 683 683 684 indio_dev->modes = INDIO_DIRECT_MODE; ··· 722 721 return err; 723 722 } 724 723 725 - err = iio_device_register(indio_dev); 726 - if (err) 727 - goto st_press_device_register_error; 728 - 729 - dev_info(&indio_dev->dev, "registered pressure sensor %s\n", 730 - indio_dev->name); 731 - 732 - return err; 733 - 734 - st_press_device_register_error: 735 - if (press_data->irq > 0) 736 - st_sensors_deallocate_trigger(indio_dev); 737 - return err; 724 + return devm_iio_device_register(parent, indio_dev); 738 725 } 739 726 EXPORT_SYMBOL(st_press_common_probe); 740 - 741 - void st_press_common_remove(struct iio_dev *indio_dev) 742 - { 743 - struct st_sensor_data *press_data = iio_priv(indio_dev); 744 - 745 - iio_device_unregister(indio_dev); 746 - if (press_data->irq > 0) 747 - st_sensors_deallocate_trigger(indio_dev); 748 - } 749 - EXPORT_SYMBOL(st_press_common_remove); 750 727 751 728 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 752 729 MODULE_DESCRIPTION("STMicroelectronics pressures driver");
+1 -22
drivers/iio/pressure/st_pressure_i2c.c
··· 103 103 if (ret) 104 104 return ret; 105 105 106 - ret = st_press_common_probe(indio_dev); 107 - if (ret < 0) 108 - goto st_press_power_off; 109 - 110 - return 0; 111 - 112 - st_press_power_off: 113 - st_sensors_power_disable(indio_dev); 114 - 115 - return ret; 116 - } 117 - 118 - static int st_press_i2c_remove(struct i2c_client *client) 119 - { 120 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 121 - 122 - st_sensors_power_disable(indio_dev); 123 - 124 - st_press_common_remove(indio_dev); 125 - 126 - return 0; 106 + return st_press_common_probe(indio_dev); 127 107 } 128 108 129 109 static struct i2c_driver st_press_driver = { ··· 113 133 .acpi_match_table = ACPI_PTR(st_press_acpi_match), 114 134 }, 115 135 .probe = st_press_i2c_probe, 116 - .remove = st_press_i2c_remove, 117 136 .id_table = st_press_id_table, 118 137 }; 119 138 module_i2c_driver(st_press_driver);
+1 -22
drivers/iio/pressure/st_pressure_spi.c
··· 86 86 if (err) 87 87 return err; 88 88 89 - err = st_press_common_probe(indio_dev); 90 - if (err < 0) 91 - goto st_press_power_off; 92 - 93 - return 0; 94 - 95 - st_press_power_off: 96 - st_sensors_power_disable(indio_dev); 97 - 98 - return err; 99 - } 100 - 101 - static int st_press_spi_remove(struct spi_device *spi) 102 - { 103 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 104 - 105 - st_sensors_power_disable(indio_dev); 106 - 107 - st_press_common_remove(indio_dev); 108 - 109 - return 0; 89 + return st_press_common_probe(indio_dev); 110 90 } 111 91 112 92 static const struct spi_device_id st_press_id_table[] = { ··· 107 127 .of_match_table = st_press_of_match, 108 128 }, 109 129 .probe = st_press_spi_probe, 110 - .remove = st_press_spi_remove, 111 130 .id_table = st_press_id_table, 112 131 }; 113 132 module_spi_driver(st_press_driver);
+10
drivers/iio/temperature/Kconfig
··· 138 138 This driver can also be built as a module. If so, the module 139 139 will be called max31856. 140 140 141 + config MAX31865 142 + tristate "MAX31865 RTD to Digital converter" 143 + depends on SPI 144 + help 145 + If you say yes here you get support for MAX31865 146 + thermocouple sensor chip connected via SPI. 147 + 148 + This driver can also be build as a module. If so, the module 149 + will be called max31865. 150 + 141 151 endmenu
+1
drivers/iio/temperature/Makefile
··· 8 8 obj-$(CONFIG_HID_SENSOR_TEMP) += hid-sensor-temperature.o 9 9 obj-$(CONFIG_MAXIM_THERMOCOUPLE) += maxim_thermocouple.o 10 10 obj-$(CONFIG_MAX31856) += max31856.o 11 + obj-$(CONFIG_MAX31865) += max31865.o 11 12 obj-$(CONFIG_MLX90614) += mlx90614.o 12 13 obj-$(CONFIG_MLX90632) += mlx90632.o 13 14 obj-$(CONFIG_TMP006) += tmp006.o
+16
drivers/iio/temperature/ltc2983.c
··· 1275 1275 &st->filter_notch_freq); 1276 1276 1277 1277 st->num_channels = of_get_available_child_count(dev->of_node); 1278 + if (!st->num_channels) { 1279 + dev_err(&st->spi->dev, "At least one channel must be given!"); 1280 + return -EINVAL; 1281 + } 1282 + 1278 1283 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors), 1279 1284 GFP_KERNEL); 1280 1285 if (!st->sensors) ··· 1475 1470 { 1476 1471 struct ltc2983_data *st; 1477 1472 struct iio_dev *indio_dev; 1473 + struct gpio_desc *gpio; 1478 1474 const char *name = spi_get_device_id(spi)->name; 1479 1475 int ret; 1480 1476 ··· 1499 1493 ret = ltc2983_parse_dt(st); 1500 1494 if (ret) 1501 1495 return ret; 1496 + 1497 + gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH); 1498 + if (IS_ERR(gpio)) 1499 + return PTR_ERR(gpio); 1500 + 1501 + if (gpio) { 1502 + /* bring the device out of reset */ 1503 + usleep_range(1000, 1200); 1504 + gpiod_set_value_cansleep(gpio, 0); 1505 + } 1502 1506 1503 1507 ret = ltc2983_setup(st, true); 1504 1508 if (ret)
+349
drivers/iio/temperature/max31865.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + 3 + /* 4 + * Copyright (c) Linumiz 2021 5 + * 6 + * max31865.c - Maxim MAX31865 RTD-to-Digital Converter sensor driver 7 + * 8 + * Author: Navin Sankar Velliangiri <navin@linumiz.com> 9 + */ 10 + 11 + #include <linux/ctype.h> 12 + #include <linux/delay.h> 13 + #include <linux/err.h> 14 + #include <linux/init.h> 15 + #include <linux/module.h> 16 + #include <linux/iio/iio.h> 17 + #include <linux/iio/sysfs.h> 18 + #include <linux/spi/spi.h> 19 + #include <asm/unaligned.h> 20 + 21 + /* 22 + * The MSB of the register value determines whether the following byte will 23 + * be written or read. If it is 0, read will follow and if it is 1, write 24 + * will follow. 25 + */ 26 + #define MAX31865_RD_WR_BIT BIT(7) 27 + 28 + #define MAX31865_CFG_VBIAS BIT(7) 29 + #define MAX31865_CFG_1SHOT BIT(5) 30 + #define MAX31865_3WIRE_RTD BIT(4) 31 + #define MAX31865_FAULT_STATUS_CLEAR BIT(1) 32 + #define MAX31865_FILTER_50HZ BIT(0) 33 + 34 + /* The MAX31865 registers */ 35 + #define MAX31865_CFG_REG 0x00 36 + #define MAX31865_RTD_MSB 0x01 37 + #define MAX31865_FAULT_STATUS 0x07 38 + 39 + #define MAX31865_FAULT_OVUV BIT(2) 40 + 41 + static const char max31865_show_samp_freq[] = "50 60"; 42 + 43 + static const struct iio_chan_spec max31865_channels[] = { 44 + { /* RTD Temperature */ 45 + .type = IIO_TEMP, 46 + .info_mask_separate = 47 + BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) 48 + }, 49 + }; 50 + 51 + struct max31865_data { 52 + struct spi_device *spi; 53 + struct mutex lock; 54 + bool filter_50hz; 55 + bool three_wire; 56 + u8 buf[2] ____cacheline_aligned; 57 + }; 58 + 59 + static int max31865_read(struct max31865_data *data, u8 reg, 60 + unsigned int read_size) 61 + { 62 + return spi_write_then_read(data->spi, &reg, 1, data->buf, read_size); 63 + } 64 + 65 + static int max31865_write(struct max31865_data *data, size_t len) 66 + { 67 + return spi_write(data->spi, data->buf, len); 68 + } 69 + 70 + static int enable_bias(struct max31865_data *data) 71 + { 72 + u8 cfg; 73 + int ret; 74 + 75 + ret = max31865_read(data, MAX31865_CFG_REG, 1); 76 + if (ret) 77 + return ret; 78 + 79 + cfg = data->buf[0]; 80 + 81 + data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT; 82 + data->buf[1] = cfg | MAX31865_CFG_VBIAS; 83 + 84 + return max31865_write(data, 2); 85 + } 86 + 87 + static int disable_bias(struct max31865_data *data) 88 + { 89 + u8 cfg; 90 + int ret; 91 + 92 + ret = max31865_read(data, MAX31865_CFG_REG, 1); 93 + if (ret) 94 + return ret; 95 + 96 + cfg = data->buf[0]; 97 + cfg &= ~MAX31865_CFG_VBIAS; 98 + 99 + data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT; 100 + data->buf[1] = cfg; 101 + 102 + return max31865_write(data, 2); 103 + } 104 + 105 + static int max31865_rtd_read(struct max31865_data *data, int *val) 106 + { 107 + u8 reg; 108 + int ret; 109 + 110 + /* Enable BIAS to start the conversion */ 111 + ret = enable_bias(data); 112 + if (ret) 113 + return ret; 114 + 115 + /* wait 10.5ms before initiating the conversion */ 116 + msleep(11); 117 + 118 + ret = max31865_read(data, MAX31865_CFG_REG, 1); 119 + if (ret) 120 + return ret; 121 + 122 + reg = data->buf[0]; 123 + reg |= MAX31865_CFG_1SHOT | MAX31865_FAULT_STATUS_CLEAR; 124 + data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT; 125 + data->buf[1] = reg; 126 + 127 + ret = max31865_write(data, 2); 128 + if (ret) 129 + return ret; 130 + 131 + if (data->filter_50hz) { 132 + /* 50Hz filter mode requires 62.5ms to complete */ 133 + msleep(63); 134 + } else { 135 + /* 60Hz filter mode requires 52ms to complete */ 136 + msleep(52); 137 + } 138 + 139 + ret = max31865_read(data, MAX31865_RTD_MSB, 2); 140 + if (ret) 141 + return ret; 142 + 143 + *val = get_unaligned_be16(&data->buf) >> 1; 144 + 145 + return disable_bias(data); 146 + } 147 + 148 + static int max31865_read_raw(struct iio_dev *indio_dev, 149 + struct iio_chan_spec const *chan, 150 + int *val, int *val2, long mask) 151 + { 152 + struct max31865_data *data = iio_priv(indio_dev); 153 + int ret; 154 + 155 + switch (mask) { 156 + case IIO_CHAN_INFO_RAW: 157 + mutex_lock(&data->lock); 158 + ret = max31865_rtd_read(data, val); 159 + mutex_unlock(&data->lock); 160 + if (ret) 161 + return ret; 162 + return IIO_VAL_INT; 163 + case IIO_CHAN_INFO_SCALE: 164 + /* Temp. Data resolution is 0.03125 degree centigrade */ 165 + *val = 31; 166 + *val2 = 250000; /* 1000 * 0.03125 */ 167 + return IIO_VAL_INT_PLUS_MICRO; 168 + default: 169 + return -EINVAL; 170 + } 171 + } 172 + 173 + static int max31865_init(struct max31865_data *data) 174 + { 175 + u8 cfg; 176 + int ret; 177 + 178 + ret = max31865_read(data, MAX31865_CFG_REG, 1); 179 + if (ret) 180 + return ret; 181 + 182 + cfg = data->buf[0]; 183 + 184 + if (data->three_wire) 185 + /* 3-wire RTD connection */ 186 + cfg |= MAX31865_3WIRE_RTD; 187 + 188 + if (data->filter_50hz) 189 + /* 50Hz noise rejection filter */ 190 + cfg |= MAX31865_FILTER_50HZ; 191 + 192 + data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT; 193 + data->buf[1] = cfg; 194 + 195 + return max31865_write(data, 2); 196 + } 197 + 198 + static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf) 199 + { 200 + int ret; 201 + bool fault; 202 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 203 + struct max31865_data *data = iio_priv(indio_dev); 204 + 205 + ret = max31865_read(data, MAX31865_FAULT_STATUS, 1); 206 + if (ret) 207 + return ret; 208 + 209 + fault = data->buf[0] & faultbit; 210 + 211 + return sprintf(buf, "%d\n", fault); 212 + } 213 + 214 + static ssize_t show_fault_ovuv(struct device *dev, 215 + struct device_attribute *attr, 216 + char *buf) 217 + { 218 + return show_fault(dev, MAX31865_FAULT_OVUV, buf); 219 + } 220 + 221 + static ssize_t show_filter(struct device *dev, 222 + struct device_attribute *attr, 223 + char *buf) 224 + { 225 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 226 + struct max31865_data *data = iio_priv(indio_dev); 227 + 228 + return sprintf(buf, "%d\n", data->filter_50hz ? 50 : 60); 229 + } 230 + 231 + static ssize_t set_filter(struct device *dev, 232 + struct device_attribute *attr, 233 + const char *buf, 234 + size_t len) 235 + { 236 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 237 + struct max31865_data *data = iio_priv(indio_dev); 238 + unsigned int freq; 239 + int ret; 240 + 241 + ret = kstrtouint(buf, 10, &freq); 242 + if (ret) 243 + return ret; 244 + 245 + switch (freq) { 246 + case 50: 247 + data->filter_50hz = true; 248 + break; 249 + case 60: 250 + data->filter_50hz = false; 251 + break; 252 + default: 253 + return -EINVAL; 254 + } 255 + 256 + mutex_lock(&data->lock); 257 + ret = max31865_init(data); 258 + mutex_unlock(&data->lock); 259 + if (ret) 260 + return ret; 261 + 262 + return len; 263 + } 264 + 265 + static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(max31865_show_samp_freq); 266 + static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0); 267 + static IIO_DEVICE_ATTR(in_filter_notch_center_frequency, 0644, 268 + show_filter, set_filter, 0); 269 + 270 + static struct attribute *max31865_attributes[] = { 271 + &iio_dev_attr_fault_ovuv.dev_attr.attr, 272 + &iio_const_attr_sampling_frequency_available.dev_attr.attr, 273 + &iio_dev_attr_in_filter_notch_center_frequency.dev_attr.attr, 274 + NULL, 275 + }; 276 + 277 + static const struct attribute_group max31865_group = { 278 + .attrs = max31865_attributes, 279 + }; 280 + 281 + static const struct iio_info max31865_info = { 282 + .read_raw = max31865_read_raw, 283 + .attrs = &max31865_group, 284 + }; 285 + 286 + static int max31865_probe(struct spi_device *spi) 287 + { 288 + const struct spi_device_id *id = spi_get_device_id(spi); 289 + struct iio_dev *indio_dev; 290 + struct max31865_data *data; 291 + int ret; 292 + 293 + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); 294 + if (!indio_dev) 295 + return -ENOMEM; 296 + 297 + data = iio_priv(indio_dev); 298 + data->spi = spi; 299 + data->filter_50hz = false; 300 + mutex_init(&data->lock); 301 + 302 + indio_dev->info = &max31865_info; 303 + indio_dev->name = id->name; 304 + indio_dev->modes = INDIO_DIRECT_MODE; 305 + indio_dev->channels = max31865_channels; 306 + indio_dev->num_channels = ARRAY_SIZE(max31865_channels); 307 + 308 + if (of_property_read_bool(spi->dev.of_node, "maxim,3-wire")) { 309 + /* select 3 wire */ 310 + data->three_wire = 1; 311 + } else { 312 + /* select 2 or 4 wire */ 313 + data->three_wire = 0; 314 + } 315 + 316 + ret = max31865_init(data); 317 + if (ret) { 318 + dev_err(&spi->dev, "error: Failed to configure max31865\n"); 319 + return ret; 320 + } 321 + 322 + return devm_iio_device_register(&spi->dev, indio_dev); 323 + } 324 + 325 + static const struct spi_device_id max31865_id[] = { 326 + { "max31865", 0 }, 327 + { } 328 + }; 329 + MODULE_DEVICE_TABLE(spi, max31865_id); 330 + 331 + static const struct of_device_id max31865_of_match[] = { 332 + { .compatible = "maxim,max31865" }, 333 + { } 334 + }; 335 + MODULE_DEVICE_TABLE(of, max31865_of_match); 336 + 337 + static struct spi_driver max31865_driver = { 338 + .driver = { 339 + .name = "max31865", 340 + .of_match_table = max31865_of_match, 341 + }, 342 + .probe = max31865_probe, 343 + .id_table = max31865_id, 344 + }; 345 + module_spi_driver(max31865_driver); 346 + 347 + MODULE_AUTHOR("Navin Sankar Velliangiri <navin@linumiz.com>"); 348 + MODULE_DESCRIPTION("Maxim MAX31865 RTD-to-Digital Converter sensor driver"); 349 + MODULE_LICENSE("GPL v2");
+1 -3
drivers/staging/iio/cdc/ad7746.c
··· 241 241 if (ret < 0) 242 242 return ret; 243 243 244 - if (chip->capdac_set != chan->channel) { 245 - 244 + if (chip->capdac_set != chan->channel) 246 245 chip->capdac_set = chan->channel; 247 - } 248 246 break; 249 247 case IIO_VOLTAGE: 250 248 case IIO_TEMP:
+36 -46
drivers/staging/iio/frequency/ad9832.c
··· 294 294 .attrs = &ad9832_attribute_group, 295 295 }; 296 296 297 + static void ad9832_reg_disable(void *reg) 298 + { 299 + regulator_disable(reg); 300 + } 301 + 302 + static void ad9832_clk_disable(void *clk) 303 + { 304 + clk_disable_unprepare(clk); 305 + } 306 + 297 307 static int ad9832_probe(struct spi_device *spi) 298 308 { 299 309 struct ad9832_platform_data *pdata = dev_get_platdata(&spi->dev); ··· 320 310 if (!indio_dev) 321 311 return -ENOMEM; 322 312 323 - spi_set_drvdata(spi, indio_dev); 324 313 st = iio_priv(indio_dev); 325 314 326 315 st->avdd = devm_regulator_get(&spi->dev, "avdd"); ··· 332 323 return ret; 333 324 } 334 325 326 + ret = devm_add_action_or_reset(&spi->dev, ad9832_reg_disable, st->avdd); 327 + if (ret) 328 + return ret; 329 + 335 330 st->dvdd = devm_regulator_get(&spi->dev, "dvdd"); 336 - if (IS_ERR(st->dvdd)) { 337 - ret = PTR_ERR(st->dvdd); 338 - goto error_disable_avdd; 339 - } 331 + if (IS_ERR(st->dvdd)) 332 + return PTR_ERR(st->dvdd); 340 333 341 334 ret = regulator_enable(st->dvdd); 342 335 if (ret) { 343 336 dev_err(&spi->dev, "Failed to enable specified DVDD supply\n"); 344 - goto error_disable_avdd; 337 + return ret; 345 338 } 346 339 340 + ret = devm_add_action_or_reset(&spi->dev, ad9832_reg_disable, st->dvdd); 341 + if (ret) 342 + return ret; 343 + 347 344 st->mclk = devm_clk_get(&spi->dev, "mclk"); 348 - if (IS_ERR(st->mclk)) { 349 - ret = PTR_ERR(st->mclk); 350 - goto error_disable_dvdd; 351 - } 345 + if (IS_ERR(st->mclk)) 346 + return PTR_ERR(st->mclk); 352 347 353 348 ret = clk_prepare_enable(st->mclk); 354 349 if (ret < 0) 355 - goto error_disable_dvdd; 350 + return ret; 351 + 352 + ret = devm_add_action_or_reset(&spi->dev, ad9832_clk_disable, st->mclk); 353 + if (ret) 354 + return ret; 356 355 357 356 st->spi = spi; 358 357 mutex_init(&st->lock); ··· 411 394 ret = spi_sync(st->spi, &st->msg); 412 395 if (ret) { 413 396 dev_err(&spi->dev, "device init failed\n"); 414 - goto error_unprepare_mclk; 397 + return ret; 415 398 } 416 399 417 400 ret = ad9832_write_frequency(st, AD9832_FREQ0HM, pdata->freq0); 418 401 if (ret) 419 - goto error_unprepare_mclk; 402 + return ret; 420 403 421 404 ret = ad9832_write_frequency(st, AD9832_FREQ1HM, pdata->freq1); 422 405 if (ret) 423 - goto error_unprepare_mclk; 406 + return ret; 424 407 425 408 ret = ad9832_write_phase(st, AD9832_PHASE0H, pdata->phase0); 426 409 if (ret) 427 - goto error_unprepare_mclk; 410 + return ret; 428 411 429 412 ret = ad9832_write_phase(st, AD9832_PHASE1H, pdata->phase1); 430 413 if (ret) 431 - goto error_unprepare_mclk; 414 + return ret; 432 415 433 416 ret = ad9832_write_phase(st, AD9832_PHASE2H, pdata->phase2); 434 417 if (ret) 435 - goto error_unprepare_mclk; 418 + return ret; 436 419 437 420 ret = ad9832_write_phase(st, AD9832_PHASE3H, pdata->phase3); 438 421 if (ret) 439 - goto error_unprepare_mclk; 422 + return ret; 440 423 441 - ret = iio_device_register(indio_dev); 442 - if (ret) 443 - goto error_unprepare_mclk; 444 - 445 - return 0; 446 - 447 - error_unprepare_mclk: 448 - clk_disable_unprepare(st->mclk); 449 - error_disable_dvdd: 450 - regulator_disable(st->dvdd); 451 - error_disable_avdd: 452 - regulator_disable(st->avdd); 453 - 454 - return ret; 455 - } 456 - 457 - static int ad9832_remove(struct spi_device *spi) 458 - { 459 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 460 - struct ad9832_state *st = iio_priv(indio_dev); 461 - 462 - iio_device_unregister(indio_dev); 463 - clk_disable_unprepare(st->mclk); 464 - regulator_disable(st->dvdd); 465 - regulator_disable(st->avdd); 466 - 467 - return 0; 424 + return devm_iio_device_register(&spi->dev, indio_dev); 468 425 } 469 426 470 427 static const struct spi_device_id ad9832_id[] = { ··· 453 462 .name = "ad9832", 454 463 }, 455 464 .probe = ad9832_probe, 456 - .remove = ad9832_remove, 457 465 .id_table = ad9832_id, 458 466 }; 459 467 module_spi_driver(ad9832_driver);
+11
include/linux/iio/buffer.h
··· 11 11 12 12 struct iio_buffer; 13 13 14 + enum iio_buffer_direction { 15 + IIO_BUFFER_DIRECTION_IN, 16 + IIO_BUFFER_DIRECTION_OUT, 17 + }; 18 + 14 19 int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data); 20 + 21 + int iio_pop_from_buffer(struct iio_buffer *buffer, void *data); 15 22 16 23 /** 17 24 * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers ··· 44 37 45 38 return iio_push_to_buffers(indio_dev, data); 46 39 } 40 + 41 + int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, 42 + const void *data, size_t data_sz, 43 + int64_t timestamp); 47 44 48 45 bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, 49 46 const unsigned long *mask);
+11
include/linux/iio/buffer_impl.h
··· 7 7 #ifdef CONFIG_IIO_BUFFER 8 8 9 9 #include <uapi/linux/iio/buffer.h> 10 + #include <linux/iio/buffer.h> 10 11 11 12 struct iio_dev; 12 13 struct iio_buffer; ··· 24 23 * @read: try to get a specified number of bytes (must exist) 25 24 * @data_available: indicates how much data is available for reading from 26 25 * the buffer. 26 + * @remove_from: remove scan from buffer. Drivers should calls this to 27 + * remove a scan from a buffer. 28 + * @write: try to write a number of bytes 29 + * @space_available: returns the amount of bytes available in a buffer 27 30 * @request_update: if a parameter change has been marked, update underlying 28 31 * storage. 29 32 * @set_bytes_per_datum:set number of bytes per datum ··· 54 49 int (*store_to)(struct iio_buffer *buffer, const void *data); 55 50 int (*read)(struct iio_buffer *buffer, size_t n, char __user *buf); 56 51 size_t (*data_available)(struct iio_buffer *buffer); 52 + int (*remove_from)(struct iio_buffer *buffer, void *data); 53 + int (*write)(struct iio_buffer *buffer, size_t n, const char __user *buf); 54 + size_t (*space_available)(struct iio_buffer *buffer); 57 55 58 56 int (*request_update)(struct iio_buffer *buffer); 59 57 ··· 87 79 88 80 /** @bytes_per_datum: Size of individual datum including timestamp. */ 89 81 size_t bytes_per_datum; 82 + 83 + /* @direction: Direction of the data stream (in/out). */ 84 + enum iio_buffer_direction direction; 90 85 91 86 /** 92 87 * @access: Buffer access functions associated with the
-13
include/linux/iio/common/st_sensors.h
··· 220 220 221 221 /** 222 222 * struct st_sensor_data - ST sensor device status 223 - * @dev: Pointer to instance of struct device (I2C or SPI). 224 223 * @trig: The trigger in use by the core driver. 225 224 * @mount_matrix: The mounting matrix of the sensor. 226 225 * @sensor_settings: Pointer to the specific sensor settings in use. ··· 239 240 * @buffer_data: Data used by buffer part. 240 241 */ 241 242 struct st_sensor_data { 242 - struct device *dev; 243 243 struct iio_trigger *trig; 244 244 struct iio_mount_matrix mount_matrix; 245 245 struct st_sensor_settings *sensor_settings; ··· 271 273 int st_sensors_allocate_trigger(struct iio_dev *indio_dev, 272 274 const struct iio_trigger_ops *trigger_ops); 273 275 274 - void st_sensors_deallocate_trigger(struct iio_dev *indio_dev); 275 276 int st_sensors_validate_device(struct iio_trigger *trig, 276 277 struct iio_dev *indio_dev); 277 278 #else ··· 278 281 const struct iio_trigger_ops *trigger_ops) 279 282 { 280 283 return 0; 281 - } 282 - static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev) 283 - { 284 - return; 285 284 } 286 285 #define st_sensors_validate_device NULL 287 286 #endif ··· 290 297 int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable); 291 298 292 299 int st_sensors_power_enable(struct iio_dev *indio_dev); 293 - 294 - void st_sensors_power_disable(struct iio_dev *indio_dev); 295 300 296 301 int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev, 297 302 unsigned reg, unsigned writeval, ··· 321 330 /* Accelerometer */ 322 331 const struct st_sensor_settings *st_accel_get_settings(const char *name); 323 332 int st_accel_common_probe(struct iio_dev *indio_dev); 324 - void st_accel_common_remove(struct iio_dev *indio_dev); 325 333 326 334 /* Gyroscope */ 327 335 const struct st_sensor_settings *st_gyro_get_settings(const char *name); 328 336 int st_gyro_common_probe(struct iio_dev *indio_dev); 329 - void st_gyro_common_remove(struct iio_dev *indio_dev); 330 337 331 338 /* Magnetometer */ 332 339 const struct st_sensor_settings *st_magn_get_settings(const char *name); 333 340 int st_magn_common_probe(struct iio_dev *indio_dev); 334 - void st_magn_common_remove(struct iio_dev *indio_dev); 335 341 336 342 /* Pressure */ 337 343 const struct st_sensor_settings *st_press_get_settings(const char *name); 338 344 int st_press_common_probe(struct iio_dev *indio_dev); 339 - void st_press_common_remove(struct iio_dev *indio_dev); 340 345 341 346 #endif /* ST_SENSORS_H */
+14
include/linux/iio/driver.h
··· 8 8 #ifndef _IIO_INKERN_H_ 9 9 #define _IIO_INKERN_H_ 10 10 11 + struct device; 11 12 struct iio_dev; 12 13 struct iio_map; 13 14 ··· 26 25 * @indio_dev: provider device 27 26 */ 28 27 int iio_map_array_unregister(struct iio_dev *indio_dev); 28 + 29 + /** 30 + * devm_iio_map_array_register - device-managed version of iio_map_array_register 31 + * @dev: Device object to which to bind the unwinding of this registration 32 + * @indio_dev: Pointer to the iio_dev structure 33 + * @maps: Pointer to an IIO map object which is to be registered to this IIO device 34 + * 35 + * This function will call iio_map_array_register() to register an IIO map object 36 + * and will also hook a callback to the iio_map_array_unregister() function to 37 + * handle de-registration of the IIO map object when the device's refcount goes to 38 + * zero. 39 + */ 40 + int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, struct iio_map *maps); 29 41 30 42 #endif
+4
include/linux/iio/iio-opaque.h
··· 23 23 * @groupcounter: index of next attribute group 24 24 * @legacy_scan_el_group: attribute group for legacy scan elements attribute group 25 25 * @legacy_buffer_group: attribute group for legacy buffer attributes group 26 + * @bounce_buffer: for devices that call iio_push_to_buffers_with_timestamp_unaligned() 27 + * @bounce_buffer_size: size of currently allocate bounce buffer 26 28 * @scan_index_timestamp: cache of the index to the timestamp 27 29 * @clock_id: timestamping clock posix identifier 28 30 * @chrdev: associated character device ··· 52 50 int groupcounter; 53 51 struct attribute_group legacy_scan_el_group; 54 52 struct attribute_group legacy_buffer_group; 53 + void *bounce_buffer; 54 + size_t bounce_buffer_size; 55 55 56 56 unsigned int scan_index_timestamp; 57 57 clockid_t clock_id;
+2
include/linux/iio/imu/adis.h
··· 49 49 * @status_error_mask: Bitmask of errors supported by the device 50 50 * @timeouts: Chip specific delays 51 51 * @enable_irq: Hook for ADIS devices that have a special IRQ enable/disable 52 + * @unmasked_drdy: True for devices that cannot mask/unmask the data ready pin 52 53 * @has_paging: True if ADIS device has paged registers 53 54 * @burst_reg_cmd: Register command that triggers burst 54 55 * @burst_len: Burst size in the SPI RX buffer. If @burst_max_len is defined, ··· 79 78 unsigned int status_error_mask; 80 79 81 80 int (*enable_irq)(struct adis *adis, bool enable); 81 + bool unmasked_drdy; 82 82 83 83 bool has_paging; 84 84
+9 -2
include/linux/iio/triggered_buffer.h
··· 2 2 #ifndef _LINUX_IIO_TRIGGERED_BUFFER_H_ 3 3 #define _LINUX_IIO_TRIGGERED_BUFFER_H_ 4 4 5 + #include <linux/iio/buffer.h> 5 6 #include <linux/interrupt.h> 6 7 7 8 struct attribute; ··· 12 11 int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev, 13 12 irqreturn_t (*h)(int irq, void *p), 14 13 irqreturn_t (*thread)(int irq, void *p), 14 + enum iio_buffer_direction direction, 15 15 const struct iio_buffer_setup_ops *setup_ops, 16 16 const struct attribute **buffer_attrs); 17 17 void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev); 18 18 19 19 #define iio_triggered_buffer_setup(indio_dev, h, thread, setup_ops) \ 20 - iio_triggered_buffer_setup_ext((indio_dev), (h), (thread), (setup_ops), NULL) 20 + iio_triggered_buffer_setup_ext((indio_dev), (h), (thread), \ 21 + IIO_BUFFER_DIRECTION_IN, (setup_ops), \ 22 + NULL) 21 23 22 24 int devm_iio_triggered_buffer_setup_ext(struct device *dev, 23 25 struct iio_dev *indio_dev, 24 26 irqreturn_t (*h)(int irq, void *p), 25 27 irqreturn_t (*thread)(int irq, void *p), 28 + enum iio_buffer_direction direction, 26 29 const struct iio_buffer_setup_ops *ops, 27 30 const struct attribute **buffer_attrs); 28 31 29 32 #define devm_iio_triggered_buffer_setup(dev, indio_dev, h, thread, setup_ops) \ 30 - devm_iio_triggered_buffer_setup_ext((dev), (indio_dev), (h), (thread), (setup_ops), NULL) 33 + devm_iio_triggered_buffer_setup_ext((dev), (indio_dev), (h), (thread), \ 34 + IIO_BUFFER_DIRECTION_IN, \ 35 + (setup_ops), NULL) 31 36 32 37 #endif