aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2016-07-13 23:05:29 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2016-07-13 23:05:29 -0400
commit6c71ee3b6157fdbde5c839405b593876d3687eb9 (patch)
treeb22fc024193c447affb729423f0e661b60b59e5a
parent7813029a509c0c92a5b16d846defaa50a2d3d7f1 (diff)
parent0ddfd857130d16b1fb1e45c5ab2321a65738d70b (diff)
Merge tag 'iio-for-4.8c' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-next
Jonathan writes: Third set of IIO new device support, features and cleanups for the 4.8 cycle. New core features - Selection of the clock source for IIO timestamps. This is done per device as it makes little sense to have events in one timebase and data timestamped on another. Biggest reason for this is that we currently use a clock source which is non monotonic which can result in 'interesting' data sets. (Includes export for get_monotonic_corse64 which Thomas Gleixner didn't mind in an earlier version.) - MAINTAINERS add the git tree to the list for IIO. New device support + a kind of indirect staging graduation. * Broadcom iproc-static-adc - new driver * mcp4531 - support for MCP454x, MCP456x, MCP464x and MCP466x potentiometers * mpu6050 - support the IC20608 6 axis motion tracking device * st-sensors - support the lis3l02dq + drop the lis3l02dq driver from staging. The general purpose driver is missing event support, but good to get rid of this driver which was rather long in the tooth. New driver features * ak8975 - Add vid regulator support and refactor handling in general. - Allow a delay after enabling regulators. - Runtime and system PM. * bmg160 - filter frequency control support. * bmp280 - SPI device support. - EOC interrupt support for the BMP085 - power management support. - supply regulator support. - reset gpio support - dt bindings for reset gpio and regulators. - of table to support device tree registration * max1363 - Device tree bindings. * mcp4531 - Device tree bindings. * st-pressure - temperature channels as part of triggered buffer (previously not due probably to alignment issues - see below). - lps22hb open drain interrupt support. - lps22hb temperature channel support Cleanups and reworkings. * numerous ADC drivers - ensure the iio_dev->dev.of_node is set to the parent dev.of_node so as to allow client bindings to find the device. * ak8975 - Fix incorrect handling of missing regulator - make sure power is down and remove. * bmp280 - read the calibration data only once as it doesn't change. * isl29125 - Use a few macros to make code a touch more readable. * mma8452 - fix a memory leak on error. - drop an unecessary bit of return value handling. * potentiometer kconfig - typo fix. * st-pressure - drop some uninformative default assignments of elements of the channel array structure (aids readability). * st-sensors - Harden interrupt handling considerably. These are actually all using level interrupts, but at least two known boards have them wired to edge only interrupt chips. Hence a slightly interesting bit of handling is needed in which we first allow for the easy option (level triggered) and secondly check the status registers before reenabling edge interrupts and fall back to a tight loop in the thread until we successfully clear the interrupt. No harm is done if we never succeed in doing so. It's an odd patch that has been through a lot of revisions to reach a consensus on how to handle what is basically broken hardware (which the previous defaults allowed to kind of work). - Fix alignment to defined storagebytes boundaries. - Ensure alignment of power of 2 byte boundaries. This has always in theory been part of the ABI of IIO, but we missed a few that snuck in that need fixing. The effect was minor as they were only followed by timestamp channels which were correctly aligned, - Add some docs to explain the gain calculations.
-rw-r--r--Documentation/ABI/testing/sysfs-bus-iio7
-rw-r--r--Documentation/DocBook/iio.tmpl2
-rw-r--r--Documentation/devicetree/bindings/i2c/trivial-devices.txt64
-rw-r--r--Documentation/devicetree/bindings/iio/adc/brcm,iproc-static-adc.txt41
-rw-r--r--Documentation/devicetree/bindings/iio/adc/max1363.txt63
-rw-r--r--Documentation/devicetree/bindings/iio/pressure/bmp085.txt15
-rw-r--r--MAINTAINERS1
-rw-r--r--drivers/iio/accel/bma180.c2
-rw-r--r--drivers/iio/accel/bmc150-accel-core.c4
-rw-r--r--drivers/iio/accel/kxcjk-1013.c2
-rw-r--r--drivers/iio/accel/mma7455_core.c3
-rw-r--r--drivers/iio/accel/mma8452.c14
-rw-r--r--drivers/iio/accel/mma9551.c2
-rw-r--r--drivers/iio/accel/mma9553.c2
-rw-r--r--drivers/iio/accel/st_accel.h1
-rw-r--r--drivers/iio/accel/st_accel_core.c64
-rw-r--r--drivers/iio/accel/st_accel_i2c.c5
-rw-r--r--drivers/iio/accel/st_accel_spi.c1
-rw-r--r--drivers/iio/adc/Kconfig12
-rw-r--r--drivers/iio/adc/Makefile1
-rw-r--r--drivers/iio/adc/ad7266.c1
-rw-r--r--drivers/iio/adc/ad7291.c3
-rw-r--r--drivers/iio/adc/ad7298.c3
-rw-r--r--drivers/iio/adc/ad7476.c3
-rw-r--r--drivers/iio/adc/ad7791.c1
-rw-r--r--drivers/iio/adc/ad7793.c1
-rw-r--r--drivers/iio/adc/ad7887.c3
-rw-r--r--drivers/iio/adc/ad7923.c3
-rw-r--r--drivers/iio/adc/ad799x.c5
-rw-r--r--drivers/iio/adc/bcm_iproc_adc.c644
-rw-r--r--drivers/iio/adc/cc10001_adc.c2
-rw-r--r--drivers/iio/adc/hi8435.c3
-rw-r--r--drivers/iio/adc/ina2xx-adc.c7
-rw-r--r--drivers/iio/adc/max1027.c1
-rw-r--r--drivers/iio/adc/max1363.c67
-rw-r--r--drivers/iio/adc/mcp320x.c1
-rw-r--r--drivers/iio/adc/mcp3422.c1
-rw-r--r--drivers/iio/adc/nau7802.c1
-rw-r--r--drivers/iio/adc/ti-adc081c.c4
-rw-r--r--drivers/iio/adc/ti-adc0832.c1
-rw-r--r--drivers/iio/adc/ti-adc128s052.c1
-rw-r--r--drivers/iio/adc/ti-ads1015.c4
-rw-r--r--drivers/iio/adc/ti-ads8688.c1
-rw-r--r--drivers/iio/adc/vf610_adc.c3
-rw-r--r--drivers/iio/adc/xilinx-xadc-events.c4
-rw-r--r--drivers/iio/chemical/atlas-ph-sensor.c2
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_buffer.c46
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c28
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_trigger.c156
-rw-r--r--drivers/iio/dac/ad5421.c6
-rw-r--r--drivers/iio/dac/ad5504.c2
-rw-r--r--drivers/iio/dummy/iio_simple_dummy_buffer.c3
-rw-r--r--drivers/iio/dummy/iio_simple_dummy_events.c2
-rw-r--r--drivers/iio/gyro/bmg160_core.c138
-rw-r--r--drivers/iio/iio_core.h3
-rw-r--r--drivers/iio/imu/bmi160/bmi160_core.c3
-rw-r--r--drivers/iio/imu/inv_mpu6050/Kconfig8
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_core.c6
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c1
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h2
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c2
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c1
-rw-r--r--drivers/iio/industrialio-core.c179
-rw-r--r--drivers/iio/industrialio-event.c19
-rw-r--r--drivers/iio/industrialio-trigger.c2
-rw-r--r--drivers/iio/light/acpi-als.c2
-rw-r--r--drivers/iio/light/adjd_s311.c2
-rw-r--r--drivers/iio/light/apds9300.c2
-rw-r--r--drivers/iio/light/apds9960.c4
-rw-r--r--drivers/iio/light/cm36651.c2
-rw-r--r--drivers/iio/light/gp2ap020a00f.c8
-rw-r--r--drivers/iio/light/isl29125.c13
-rw-r--r--drivers/iio/light/lm3533-als.c2
-rw-r--r--drivers/iio/light/ltr501.c7
-rw-r--r--drivers/iio/light/max44000.c3
-rw-r--r--drivers/iio/light/opt3001.c4
-rw-r--r--drivers/iio/light/stk3310.c2
-rw-r--r--drivers/iio/light/tcs3414.c2
-rw-r--r--drivers/iio/light/tcs3472.c2
-rw-r--r--drivers/iio/light/tsl2563.c2
-rw-r--r--drivers/iio/light/us5182d.c2
-rw-r--r--drivers/iio/magnetometer/ak8975.c134
-rw-r--r--drivers/iio/magnetometer/hmc5843_core.c2
-rw-r--r--drivers/iio/magnetometer/mag3110.c2
-rw-r--r--drivers/iio/potentiometer/Kconfig12
-rw-r--r--drivers/iio/potentiometer/mcp4531.c159
-rw-r--r--drivers/iio/pressure/Kconfig28
-rw-r--r--drivers/iio/pressure/Makefile3
-rw-r--r--drivers/iio/pressure/bmp280-core.c (renamed from drivers/iio/pressure/bmp280.c)573
-rw-r--r--drivers/iio/pressure/bmp280-i2c.c91
-rw-r--r--drivers/iio/pressure/bmp280-regmap.c84
-rw-r--r--drivers/iio/pressure/bmp280-spi.c125
-rw-r--r--drivers/iio/pressure/bmp280.h112
-rw-r--r--drivers/iio/pressure/mpl3115.c2
-rw-r--r--drivers/iio/pressure/ms5611_core.c3
-rw-r--r--drivers/iio/pressure/st_pressure_core.c155
-rw-r--r--drivers/iio/proximity/pulsedlight-lidar-lite-v2.c2
-rw-r--r--drivers/iio/proximity/sx9500.c4
-rw-r--r--drivers/staging/iio/accel/Kconfig14
-rw-r--r--drivers/staging/iio/accel/Makefile4
-rw-r--r--drivers/staging/iio/accel/lis3l02dq.h217
-rw-r--r--drivers/staging/iio/accel/lis3l02dq_core.c814
-rw-r--r--drivers/staging/iio/accel/lis3l02dq_ring.c428
-rw-r--r--drivers/staging/iio/accel/sca3000_core.c2
-rw-r--r--drivers/staging/iio/adc/ad7280a.c8
-rw-r--r--drivers/staging/iio/adc/ad7606_ring.c3
-rw-r--r--drivers/staging/iio/adc/ad7816.c3
-rw-r--r--drivers/staging/iio/addac/adt7316.c4
-rw-r--r--drivers/staging/iio/cdc/ad7150.c2
-rw-r--r--drivers/staging/iio/light/tsl2x7x_core.c2
-rw-r--r--include/linux/iio/common/st_sensors.h2
-rw-r--r--include/linux/iio/iio.h22
-rw-r--r--kernel/time/timekeeping.c1
113 files changed, 2714 insertions, 2045 deletions
diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio
index e7f590c6ef8e..fee35c00cc4e 100644
--- a/Documentation/ABI/testing/sysfs-bus-iio
+++ b/Documentation/ABI/testing/sysfs-bus-iio
@@ -32,6 +32,13 @@ Description:
32 Description of the physical chip / device for device X. 32 Description of the physical chip / device for device X.
33 Typically a part number. 33 Typically a part number.
34 34
35What: /sys/bus/iio/devices/iio:deviceX/timestamp_clock
36KernelVersion: 4.5
37Contact: linux-iio@vger.kernel.org
38Description:
39 String identifying current posix clock used to timestamp
40 buffered samples and events for device X.
41
35What: /sys/bus/iio/devices/iio:deviceX/sampling_frequency 42What: /sys/bus/iio/devices/iio:deviceX/sampling_frequency
36What: /sys/bus/iio/devices/iio:deviceX/buffer/sampling_frequency 43What: /sys/bus/iio/devices/iio:deviceX/buffer/sampling_frequency
37What: /sys/bus/iio/devices/triggerX/sampling_frequency 44What: /sys/bus/iio/devices/triggerX/sampling_frequency
diff --git a/Documentation/DocBook/iio.tmpl b/Documentation/DocBook/iio.tmpl
index f525bf56d1dd..e2ab6a1f223e 100644
--- a/Documentation/DocBook/iio.tmpl
+++ b/Documentation/DocBook/iio.tmpl
@@ -594,7 +594,7 @@
594 594
595 irqreturn_t sensor_iio_pollfunc(int irq, void *p) 595 irqreturn_t sensor_iio_pollfunc(int irq, void *p)
596 { 596 {
597 pf->timestamp = iio_get_time_ns(); 597 pf->timestamp = iio_get_time_ns((struct indio_dev *)p);
598 return IRQ_WAKE_THREAD; 598 return IRQ_WAKE_THREAD;
599 } 599 }
600 600
diff --git a/Documentation/devicetree/bindings/i2c/trivial-devices.txt b/Documentation/devicetree/bindings/i2c/trivial-devices.txt
index 539874490492..acc5cd64711c 100644
--- a/Documentation/devicetree/bindings/i2c/trivial-devices.txt
+++ b/Documentation/devicetree/bindings/i2c/trivial-devices.txt
@@ -56,6 +56,70 @@ maxim,ds1050 5 Bit Programmable, Pulse-Width Modulator
56maxim,max1237 Low-Power, 4-/12-Channel, 2-Wire Serial, 12-Bit ADCs 56maxim,max1237 Low-Power, 4-/12-Channel, 2-Wire Serial, 12-Bit ADCs
57maxim,max6625 9-Bit/12-Bit Temperature Sensors with I²C-Compatible Serial Interface 57maxim,max6625 9-Bit/12-Bit Temperature Sensors with I²C-Compatible Serial Interface
58mc,rv3029c2 Real Time Clock Module with I2C-Bus 58mc,rv3029c2 Real Time Clock Module with I2C-Bus
59microchip,mcp4531-502 Microchip 7-bit Single I2C Digital Potentiometer (5k)
60microchip,mcp4531-103 Microchip 7-bit Single I2C Digital Potentiometer (10k)
61microchip,mcp4531-503 Microchip 7-bit Single I2C Digital Potentiometer (50k)
62microchip,mcp4531-104 Microchip 7-bit Single I2C Digital Potentiometer (100k)
63microchip,mcp4532-502 Microchip 7-bit Single I2C Digital Potentiometer (5k)
64microchip,mcp4532-103 Microchip 7-bit Single I2C Digital Potentiometer (10k)
65microchip,mcp4532-503 Microchip 7-bit Single I2C Digital Potentiometer (50k)
66microchip,mcp4532-104 Microchip 7-bit Single I2C Digital Potentiometer (100k)
67microchip,mcp4541-502 Microchip 7-bit Single I2C Digital Potentiometer with NV Memory (5k)
68microchip,mcp4541-103 Microchip 7-bit Single I2C Digital Potentiometer with NV Memory (10k)
69microchip,mcp4541-503 Microchip 7-bit Single I2C Digital Potentiometer with NV Memory (50k)
70microchip,mcp4541-104 Microchip 7-bit Single I2C Digital Potentiometer with NV Memory (100k)
71microchip,mcp4542-502 Microchip 7-bit Single I2C Digital Potentiometer with NV Memory (5k)
72microchip,mcp4542-103 Microchip 7-bit Single I2C Digital Potentiometer with NV Memory (10k)
73microchip,mcp4542-503 Microchip 7-bit Single I2C Digital Potentiometer with NV Memory (50k)
74microchip,mcp4542-104 Microchip 7-bit Single I2C Digital Potentiometer with NV Memory (100k)
75microchip,mcp4551-502 Microchip 8-bit Single I2C Digital Potentiometer (5k)
76microchip,mcp4551-103 Microchip 8-bit Single I2C Digital Potentiometer (10k)
77microchip,mcp4551-503 Microchip 8-bit Single I2C Digital Potentiometer (50k)
78microchip,mcp4551-104 Microchip 8-bit Single I2C Digital Potentiometer (100k)
79microchip,mcp4552-502 Microchip 8-bit Single I2C Digital Potentiometer (5k)
80microchip,mcp4552-103 Microchip 8-bit Single I2C Digital Potentiometer (10k)
81microchip,mcp4552-503 Microchip 8-bit Single I2C Digital Potentiometer (50k)
82microchip,mcp4552-104 Microchip 8-bit Single I2C Digital Potentiometer (100k)
83microchip,mcp4561-502 Microchip 8-bit Single I2C Digital Potentiometer with NV Memory (5k)
84microchip,mcp4561-103 Microchip 8-bit Single I2C Digital Potentiometer with NV Memory (10k)
85microchip,mcp4561-503 Microchip 8-bit Single I2C Digital Potentiometer with NV Memory (50k)
86microchip,mcp4561-104 Microchip 8-bit Single I2C Digital Potentiometer with NV Memory (100k)
87microchip,mcp4562-502 Microchip 8-bit Single I2C Digital Potentiometer with NV Memory (5k)
88microchip,mcp4562-103 Microchip 8-bit Single I2C Digital Potentiometer with NV Memory (10k)
89microchip,mcp4562-503 Microchip 8-bit Single I2C Digital Potentiometer with NV Memory (50k)
90microchip,mcp4562-104 Microchip 8-bit Single I2C Digital Potentiometer with NV Memory (100k)
91microchip,mcp4631-502 Microchip 7-bit Dual I2C Digital Potentiometer (5k)
92microchip,mcp4631-103 Microchip 7-bit Dual I2C Digital Potentiometer (10k)
93microchip,mcp4631-503 Microchip 7-bit Dual I2C Digital Potentiometer (50k)
94microchip,mcp4631-104 Microchip 7-bit Dual I2C Digital Potentiometer (100k)
95microchip,mcp4632-502 Microchip 7-bit Dual I2C Digital Potentiometer (5k)
96microchip,mcp4632-103 Microchip 7-bit Dual I2C Digital Potentiometer (10k)
97microchip,mcp4632-503 Microchip 7-bit Dual I2C Digital Potentiometer (50k)
98microchip,mcp4632-104 Microchip 7-bit Dual I2C Digital Potentiometer (100k)
99microchip,mcp4641-502 Microchip 7-bit Dual I2C Digital Potentiometer with NV Memory (5k)
100microchip,mcp4641-103 Microchip 7-bit Dual I2C Digital Potentiometer with NV Memory (10k)
101microchip,mcp4641-503 Microchip 7-bit Dual I2C Digital Potentiometer with NV Memory (50k)
102microchip,mcp4641-104 Microchip 7-bit Dual I2C Digital Potentiometer with NV Memory (100k)
103microchip,mcp4642-502 Microchip 7-bit Dual I2C Digital Potentiometer with NV Memory (5k)
104microchip,mcp4642-103 Microchip 7-bit Dual I2C Digital Potentiometer with NV Memory (10k)
105microchip,mcp4642-503 Microchip 7-bit Dual I2C Digital Potentiometer with NV Memory (50k)
106microchip,mcp4642-104 Microchip 7-bit Dual I2C Digital Potentiometer with NV Memory (100k)
107microchip,mcp4651-502 Microchip 8-bit Dual I2C Digital Potentiometer (5k)
108microchip,mcp4651-103 Microchip 8-bit Dual I2C Digital Potentiometer (10k)
109microchip,mcp4651-503 Microchip 8-bit Dual I2C Digital Potentiometer (50k)
110microchip,mcp4651-104 Microchip 8-bit Dual I2C Digital Potentiometer (100k)
111microchip,mcp4652-502 Microchip 8-bit Dual I2C Digital Potentiometer (5k)
112microchip,mcp4652-103 Microchip 8-bit Dual I2C Digital Potentiometer (10k)
113microchip,mcp4652-503 Microchip 8-bit Dual I2C Digital Potentiometer (50k)
114microchip,mcp4652-104 Microchip 8-bit Dual I2C Digital Potentiometer (100k)
115microchip,mcp4661-502 Microchip 8-bit Dual I2C Digital Potentiometer with NV Memory (5k)
116microchip,mcp4661-103 Microchip 8-bit Dual I2C Digital Potentiometer with NV Memory (10k)
117microchip,mcp4661-503 Microchip 8-bit Dual I2C Digital Potentiometer with NV Memory (50k)
118microchip,mcp4661-104 Microchip 8-bit Dual I2C Digital Potentiometer with NV Memory (100k)
119microchip,mcp4662-502 Microchip 8-bit Dual I2C Digital Potentiometer with NV Memory (5k)
120microchip,mcp4662-103 Microchip 8-bit Dual I2C Digital Potentiometer with NV Memory (10k)
121microchip,mcp4662-503 Microchip 8-bit Dual I2C Digital Potentiometer with NV Memory (50k)
122microchip,mcp4662-104 Microchip 8-bit Dual I2C Digital Potentiometer with NV Memory (100k)
59national,lm63 Temperature sensor with integrated fan control 123national,lm63 Temperature sensor with integrated fan control
60national,lm75 I2C TEMP SENSOR 124national,lm75 I2C TEMP SENSOR
61national,lm80 Serial Interface ACPI-Compatible Microprocessor System Hardware Monitor 125national,lm80 Serial Interface ACPI-Compatible Microprocessor System Hardware Monitor
diff --git a/Documentation/devicetree/bindings/iio/adc/brcm,iproc-static-adc.txt b/Documentation/devicetree/bindings/iio/adc/brcm,iproc-static-adc.txt
new file mode 100644
index 000000000000..caaaed765ce4
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/brcm,iproc-static-adc.txt
@@ -0,0 +1,41 @@
1* Broadcom's IPROC Static ADC controller
2
3Broadcom iProc ADC controller has 8 channels 10bit ADC.
4Allows user to convert analog input voltage values to digital.
5
6Required properties:
7
8- compatible: Must be "brcm,iproc-static-adc"
9
10- adc-syscon: Handler of syscon node defining physical base address of the
11 controller and length of memory mapped region.
12
13- #io-channel-cells = <1>; As ADC has multiple outputs
14 refer to Documentation/devicetree/bindings/iio/iio-bindings.txt for details.
15
16- io-channel-ranges:
17 refer to Documentation/devicetree/bindings/iio/iio-bindings.txt for details.
18
19- clocks: Clock used for this block.
20
21- clock-names: Clock name should be given as tsc_clk.
22
23- interrupts: interrupt line number.
24
25For example:
26
27 ts_adc_syscon: ts_adc_syscon@180a6000 {
28 compatible = "brcm,iproc-ts-adc-syscon","syscon";
29 reg = <0x180a6000 0xc30>;
30 };
31
32 adc: adc@180a6000 {
33 compatible = "brcm,iproc-static-adc";
34 adc-syscon = <&ts_adc_syscon>;
35 #io-channel-cells = <1>;
36 io-channel-ranges;
37 clocks = <&asiu_clks BCM_CYGNUS_ASIU_ADC_CLK>;
38 clock-names = "tsc_clk";
39 interrupts = <GIC_SPI 164 IRQ_TYPE_LEVEL_HIGH>;
40 status = "disabled";
41 };
diff --git a/Documentation/devicetree/bindings/iio/adc/max1363.txt b/Documentation/devicetree/bindings/iio/adc/max1363.txt
new file mode 100644
index 000000000000..94a9011dd860
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/max1363.txt
@@ -0,0 +1,63 @@
1* Maxim 1x3x/136x/116xx Analog to Digital Converter (ADC)
2
3The node for this driver must be a child node of a I2C controller, hence
4all mandatory properties for your controller must be specified. See directory:
5
6 Documentation/devicetree/bindings/i2c
7
8for more details.
9
10Required properties:
11 - compatible: Should be one of
12 "maxim,max1361"
13 "maxim,max1362"
14 "maxim,max1363"
15 "maxim,max1364"
16 "maxim,max1036"
17 "maxim,max1037"
18 "maxim,max1038"
19 "maxim,max1039"
20 "maxim,max1136"
21 "maxim,max1137"
22 "maxim,max1138"
23 "maxim,max1139"
24 "maxim,max1236"
25 "maxim,max1237"
26 "maxim,max1238"
27 "maxim,max1239"
28 "maxim,max11600"
29 "maxim,max11601"
30 "maxim,max11602"
31 "maxim,max11603"
32 "maxim,max11604"
33 "maxim,max11605"
34 "maxim,max11606"
35 "maxim,max11607"
36 "maxim,max11608"
37 "maxim,max11609"
38 "maxim,max11610"
39 "maxim,max11611"
40 "maxim,max11612"
41 "maxim,max11613"
42 "maxim,max11614"
43 "maxim,max11615"
44 "maxim,max11616"
45 "maxim,max11617"
46 "maxim,max11644"
47 "maxim,max11645"
48 "maxim,max11646"
49 "maxim,max11647"
50 - reg: Should contain the ADC I2C address
51
52Optional properties:
53 - vcc-supply: phandle to the regulator that provides power to the ADC.
54 - vref-supply: phandle to the regulator for ADC reference voltage.
55 - interrupts: IRQ line for the ADC. If not used the driver will use
56 polling.
57
58Example:
59adc: max11644@36 {
60 compatible = "maxim,max11644";
61 reg = <0x36>;
62 vref-supply = <&adc_vref>;
63};
diff --git a/Documentation/devicetree/bindings/iio/pressure/bmp085.txt b/Documentation/devicetree/bindings/iio/pressure/bmp085.txt
index d7a6deb6b21e..c7198a03c906 100644
--- a/Documentation/devicetree/bindings/iio/pressure/bmp085.txt
+++ b/Documentation/devicetree/bindings/iio/pressure/bmp085.txt
@@ -1,7 +1,11 @@
1BMP085/BMP18x digital pressure sensors 1BMP085/BMP18x/BMP28x digital pressure sensors
2 2
3Required properties: 3Required properties:
4- compatible: bosch,bmp085 4- compatible: must be one of:
5 "bosch,bmp085"
6 "bosch,bmp180"
7 "bosch,bmp280"
8 "bosch,bme280"
5 9
6Optional properties: 10Optional properties:
7- chip-id: configurable chip id for non-default chip revisions 11- chip-id: configurable chip id for non-default chip revisions
@@ -10,6 +14,10 @@ Optional properties:
10 value range is 0-3 with rising sensitivity. 14 value range is 0-3 with rising sensitivity.
11- interrupt-parent: should be the phandle for the interrupt controller 15- interrupt-parent: should be the phandle for the interrupt controller
12- interrupts: interrupt mapping for IRQ 16- interrupts: interrupt mapping for IRQ
17- reset-gpios: a GPIO line handling reset of the sensor: as the line is
18 active low, it should be marked GPIO_ACTIVE_LOW (see gpio/gpio.txt)
19- vddd-supply: digital voltage regulator (see regulator/regulator.txt)
20- vdda-supply: analog voltage regulator (see regulator/regulator.txt)
13 21
14Example: 22Example:
15 23
@@ -21,4 +29,7 @@ pressure@77 {
21 default-oversampling = <2>; 29 default-oversampling = <2>;
22 interrupt-parent = <&gpio0>; 30 interrupt-parent = <&gpio0>;
23 interrupts = <25 IRQ_TYPE_EDGE_RISING>; 31 interrupts = <25 IRQ_TYPE_EDGE_RISING>;
32 reset-gpios = <&gpio0 26 GPIO_ACTIVE_LOW>;
33 vddd-supply = <&foo>;
34 vdda-supply = <&bar>;
24}; 35};
diff --git a/MAINTAINERS b/MAINTAINERS
index 95c1b3a40906..613179fc8556 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5789,6 +5789,7 @@ R: Hartmut Knaack <knaack.h@gmx.de>
5789R: Lars-Peter Clausen <lars@metafoo.de> 5789R: Lars-Peter Clausen <lars@metafoo.de>
5790R: Peter Meerwald-Stadler <pmeerw@pmeerw.net> 5790R: Peter Meerwald-Stadler <pmeerw@pmeerw.net>
5791L: linux-iio@vger.kernel.org 5791L: linux-iio@vger.kernel.org
5792T: git git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git
5792S: Maintained 5793S: Maintained
5793F: Documentation/devicetree/bindings/iio/ 5794F: Documentation/devicetree/bindings/iio/
5794F: drivers/iio/ 5795F: drivers/iio/
diff --git a/drivers/iio/accel/bma180.c b/drivers/iio/accel/bma180.c
index f04b88406995..e3f88ba5faf3 100644
--- a/drivers/iio/accel/bma180.c
+++ b/drivers/iio/accel/bma180.c
@@ -654,7 +654,7 @@ static irqreturn_t bma180_trigger_handler(int irq, void *p)
654 struct iio_poll_func *pf = p; 654 struct iio_poll_func *pf = p;
655 struct iio_dev *indio_dev = pf->indio_dev; 655 struct iio_dev *indio_dev = pf->indio_dev;
656 struct bma180_data *data = iio_priv(indio_dev); 656 struct bma180_data *data = iio_priv(indio_dev);
657 int64_t time_ns = iio_get_time_ns(); 657 s64 time_ns = iio_get_time_ns(indio_dev);
658 int bit, ret, i = 0; 658 int bit, ret, i = 0;
659 659
660 mutex_lock(&data->mutex); 660 mutex_lock(&data->mutex);
diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c
index 197e693e7e7b..bf17aae66145 100644
--- a/drivers/iio/accel/bmc150-accel-core.c
+++ b/drivers/iio/accel/bmc150-accel-core.c
@@ -901,7 +901,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev,
901 */ 901 */
902 if (!irq) { 902 if (!irq) {
903 data->old_timestamp = data->timestamp; 903 data->old_timestamp = data->timestamp;
904 data->timestamp = iio_get_time_ns(); 904 data->timestamp = iio_get_time_ns(indio_dev);
905 } 905 }
906 906
907 /* 907 /*
@@ -1303,7 +1303,7 @@ static irqreturn_t bmc150_accel_irq_handler(int irq, void *private)
1303 int i; 1303 int i;
1304 1304
1305 data->old_timestamp = data->timestamp; 1305 data->old_timestamp = data->timestamp;
1306 data->timestamp = iio_get_time_ns(); 1306 data->timestamp = iio_get_time_ns(indio_dev);
1307 1307
1308 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) { 1308 for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) {
1309 if (data->triggers[i].enabled) { 1309 if (data->triggers[i].enabled) {
diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c
index bfe219a8bea2..765a72362dc6 100644
--- a/drivers/iio/accel/kxcjk-1013.c
+++ b/drivers/iio/accel/kxcjk-1013.c
@@ -1129,7 +1129,7 @@ static irqreturn_t kxcjk1013_data_rdy_trig_poll(int irq, void *private)
1129 struct iio_dev *indio_dev = private; 1129 struct iio_dev *indio_dev = private;
1130 struct kxcjk1013_data *data = iio_priv(indio_dev); 1130 struct kxcjk1013_data *data = iio_priv(indio_dev);
1131 1131
1132 data->timestamp = iio_get_time_ns(); 1132 data->timestamp = iio_get_time_ns(indio_dev);
1133 1133
1134 if (data->dready_trigger_on) 1134 if (data->dready_trigger_on)
1135 iio_trigger_poll(data->dready_trig); 1135 iio_trigger_poll(data->dready_trig);
diff --git a/drivers/iio/accel/mma7455_core.c b/drivers/iio/accel/mma7455_core.c
index c902f54c23f5..6551085bedd7 100644
--- a/drivers/iio/accel/mma7455_core.c
+++ b/drivers/iio/accel/mma7455_core.c
@@ -97,7 +97,8 @@ static irqreturn_t mma7455_trigger_handler(int irq, void *p)
97 if (ret) 97 if (ret)
98 goto done; 98 goto done;
99 99
100 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); 100 iio_push_to_buffers_with_timestamp(indio_dev, buf,
101 iio_get_time_ns(indio_dev));
101 102
102done: 103done:
103 iio_trigger_notify_done(indio_dev->trig); 104 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
index 799fe64fc286..d41e1b588e68 100644
--- a/drivers/iio/accel/mma8452.c
+++ b/drivers/iio/accel/mma8452.c
@@ -634,11 +634,7 @@ static int mma8452_set_freefall_mode(struct mma8452_data *data, bool state)
634 val |= MMA8452_FF_MT_CFG_OAE; 634 val |= MMA8452_FF_MT_CFG_OAE;
635 } 635 }
636 636
637 val = mma8452_change_config(data, chip->ev_cfg, val); 637 return mma8452_change_config(data, chip->ev_cfg, val);
638 if (val)
639 return val;
640
641 return 0;
642} 638}
643 639
644static int mma8452_set_hp_filter_frequency(struct mma8452_data *data, 640static int mma8452_set_hp_filter_frequency(struct mma8452_data *data,
@@ -917,7 +913,7 @@ static int mma8452_write_event_config(struct iio_dev *indio_dev,
917static void mma8452_transient_interrupt(struct iio_dev *indio_dev) 913static void mma8452_transient_interrupt(struct iio_dev *indio_dev)
918{ 914{
919 struct mma8452_data *data = iio_priv(indio_dev); 915 struct mma8452_data *data = iio_priv(indio_dev);
920 s64 ts = iio_get_time_ns(); 916 s64 ts = iio_get_time_ns(indio_dev);
921 int src; 917 int src;
922 918
923 src = i2c_smbus_read_byte_data(data->client, data->chip_info->ev_src); 919 src = i2c_smbus_read_byte_data(data->client, data->chip_info->ev_src);
@@ -997,7 +993,7 @@ static irqreturn_t mma8452_trigger_handler(int irq, void *p)
997 goto done; 993 goto done;
998 994
999 iio_push_to_buffers_with_timestamp(indio_dev, buffer, 995 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
1000 iio_get_time_ns()); 996 iio_get_time_ns(indio_dev));
1001 997
1002done: 998done:
1003 iio_trigger_notify_done(indio_dev->trig); 999 iio_trigger_notify_done(indio_dev->trig);
@@ -1579,8 +1575,8 @@ static int mma8452_probe(struct i2c_client *client,
1579 goto buffer_cleanup; 1575 goto buffer_cleanup;
1580 1576
1581 ret = mma8452_set_freefall_mode(data, false); 1577 ret = mma8452_set_freefall_mode(data, false);
1582 if (ret) 1578 if (ret < 0)
1583 return ret; 1579 goto buffer_cleanup;
1584 1580
1585 return 0; 1581 return 0;
1586 1582
diff --git a/drivers/iio/accel/mma9551.c b/drivers/iio/accel/mma9551.c
index d899a4d4307f..bf2704435629 100644
--- a/drivers/iio/accel/mma9551.c
+++ b/drivers/iio/accel/mma9551.c
@@ -391,7 +391,7 @@ static irqreturn_t mma9551_event_handler(int irq, void *private)
391 iio_push_event(indio_dev, 391 iio_push_event(indio_dev,
392 IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1), 392 IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1),
393 IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING), 393 IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING),
394 iio_get_time_ns()); 394 iio_get_time_ns(indio_dev));
395 395
396out: 396out:
397 mutex_unlock(&data->mutex); 397 mutex_unlock(&data->mutex);
diff --git a/drivers/iio/accel/mma9553.c b/drivers/iio/accel/mma9553.c
index bb05f3efddca..36bf19733be0 100644
--- a/drivers/iio/accel/mma9553.c
+++ b/drivers/iio/accel/mma9553.c
@@ -1001,7 +1001,7 @@ static irqreturn_t mma9553_irq_handler(int irq, void *private)
1001 struct iio_dev *indio_dev = private; 1001 struct iio_dev *indio_dev = private;
1002 struct mma9553_data *data = iio_priv(indio_dev); 1002 struct mma9553_data *data = iio_priv(indio_dev);
1003 1003
1004 data->timestamp = iio_get_time_ns(); 1004 data->timestamp = iio_get_time_ns(indio_dev);
1005 /* 1005 /*
1006 * Since we only configure the interrupt pin when an 1006 * Since we only configure the interrupt pin when an
1007 * event is enabled, we are sure we have at least 1007 * event is enabled, we are sure we have at least
diff --git a/drivers/iio/accel/st_accel.h b/drivers/iio/accel/st_accel.h
index 57f83a67948c..f8dfdb690563 100644
--- a/drivers/iio/accel/st_accel.h
+++ b/drivers/iio/accel/st_accel.h
@@ -29,6 +29,7 @@
29#define LSM330_ACCEL_DEV_NAME "lsm330_accel" 29#define LSM330_ACCEL_DEV_NAME "lsm330_accel"
30#define LSM303AGR_ACCEL_DEV_NAME "lsm303agr_accel" 30#define LSM303AGR_ACCEL_DEV_NAME "lsm303agr_accel"
31#define LIS2DH12_ACCEL_DEV_NAME "lis2dh12_accel" 31#define LIS2DH12_ACCEL_DEV_NAME "lis2dh12_accel"
32#define LIS3L02DQ_ACCEL_DEV_NAME "lis3l02dq"
32 33
33/** 34/**
34* struct st_sensors_platform_data - default accel platform data 35* struct st_sensors_platform_data - default accel platform data
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index dce289aa40f2..da3fb069ec5c 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -215,6 +215,22 @@
215#define ST_ACCEL_6_IHL_IRQ_MASK 0x80 215#define ST_ACCEL_6_IHL_IRQ_MASK 0x80
216#define ST_ACCEL_6_MULTIREAD_BIT true 216#define ST_ACCEL_6_MULTIREAD_BIT true
217 217
218/* CUSTOM VALUES FOR SENSOR 7 */
219#define ST_ACCEL_7_ODR_ADDR 0x20
220#define ST_ACCEL_7_ODR_MASK 0x30
221#define ST_ACCEL_7_ODR_AVL_280HZ_VAL 0x00
222#define ST_ACCEL_7_ODR_AVL_560HZ_VAL 0x01
223#define ST_ACCEL_7_ODR_AVL_1120HZ_VAL 0x02
224#define ST_ACCEL_7_ODR_AVL_4480HZ_VAL 0x03
225#define ST_ACCEL_7_PW_ADDR 0x20
226#define ST_ACCEL_7_PW_MASK 0xc0
227#define ST_ACCEL_7_FS_AVL_2_GAIN IIO_G_TO_M_S_2(488)
228#define ST_ACCEL_7_BDU_ADDR 0x21
229#define ST_ACCEL_7_BDU_MASK 0x40
230#define ST_ACCEL_7_DRDY_IRQ_ADDR 0x21
231#define ST_ACCEL_7_DRDY_IRQ_INT1_MASK 0x04
232#define ST_ACCEL_7_MULTIREAD_BIT false
233
218static const struct iio_chan_spec st_accel_8bit_channels[] = { 234static const struct iio_chan_spec st_accel_8bit_channels[] = {
219 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL, 235 ST_SENSORS_LSM_CHANNELS(IIO_ACCEL,
220 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 236 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
@@ -662,6 +678,54 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = {
662 .multi_read_bit = ST_ACCEL_6_MULTIREAD_BIT, 678 .multi_read_bit = ST_ACCEL_6_MULTIREAD_BIT,
663 .bootime = 2, 679 .bootime = 2,
664 }, 680 },
681 {
682 /* No WAI register present */
683 .sensors_supported = {
684 [0] = LIS3L02DQ_ACCEL_DEV_NAME,
685 },
686 .ch = (struct iio_chan_spec *)st_accel_12bit_channels,
687 .odr = {
688 .addr = ST_ACCEL_7_ODR_ADDR,
689 .mask = ST_ACCEL_7_ODR_MASK,
690 .odr_avl = {
691 { 280, ST_ACCEL_7_ODR_AVL_280HZ_VAL, },
692 { 560, ST_ACCEL_7_ODR_AVL_560HZ_VAL, },
693 { 1120, ST_ACCEL_7_ODR_AVL_1120HZ_VAL, },
694 { 4480, ST_ACCEL_7_ODR_AVL_4480HZ_VAL, },
695 },
696 },
697 .pw = {
698 .addr = ST_ACCEL_7_PW_ADDR,
699 .mask = ST_ACCEL_7_PW_MASK,
700 .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
701 .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
702 },
703 .enable_axis = {
704 .addr = ST_SENSORS_DEFAULT_AXIS_ADDR,
705 .mask = ST_SENSORS_DEFAULT_AXIS_MASK,
706 },
707 .fs = {
708 .fs_avl = {
709 [0] = {
710 .num = ST_ACCEL_FS_AVL_2G,
711 .gain = ST_ACCEL_7_FS_AVL_2_GAIN,
712 },
713 },
714 },
715 /*
716 * The part has a BDU bit but if set the data is never
717 * updated so don't set it.
718 */
719 .bdu = {
720 },
721 .drdy_irq = {
722 .addr = ST_ACCEL_7_DRDY_IRQ_ADDR,
723 .mask_int1 = ST_ACCEL_7_DRDY_IRQ_INT1_MASK,
724 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
725 },
726 .multi_read_bit = ST_ACCEL_7_MULTIREAD_BIT,
727 .bootime = 2,
728 },
665}; 729};
666 730
667static int st_accel_read_raw(struct iio_dev *indio_dev, 731static int st_accel_read_raw(struct iio_dev *indio_dev,
diff --git a/drivers/iio/accel/st_accel_i2c.c b/drivers/iio/accel/st_accel_i2c.c
index 7333ee9fb11b..e9d427a5df7c 100644
--- a/drivers/iio/accel/st_accel_i2c.c
+++ b/drivers/iio/accel/st_accel_i2c.c
@@ -80,6 +80,10 @@ static const struct of_device_id st_accel_of_match[] = {
80 .compatible = "st,h3lis331dl-accel", 80 .compatible = "st,h3lis331dl-accel",
81 .data = H3LIS331DL_DRIVER_NAME, 81 .data = H3LIS331DL_DRIVER_NAME,
82 }, 82 },
83 {
84 .compatible = "st,lis3l02dq",
85 .data = LIS3L02DQ_ACCEL_DEV_NAME,
86 },
83 {}, 87 {},
84}; 88};
85MODULE_DEVICE_TABLE(of, st_accel_of_match); 89MODULE_DEVICE_TABLE(of, st_accel_of_match);
@@ -130,6 +134,7 @@ static const struct i2c_device_id st_accel_id_table[] = {
130 { LSM330_ACCEL_DEV_NAME }, 134 { LSM330_ACCEL_DEV_NAME },
131 { LSM303AGR_ACCEL_DEV_NAME }, 135 { LSM303AGR_ACCEL_DEV_NAME },
132 { LIS2DH12_ACCEL_DEV_NAME }, 136 { LIS2DH12_ACCEL_DEV_NAME },
137 { LIS3L02DQ_ACCEL_DEV_NAME },
133 {}, 138 {},
134}; 139};
135MODULE_DEVICE_TABLE(i2c, st_accel_id_table); 140MODULE_DEVICE_TABLE(i2c, st_accel_id_table);
diff --git a/drivers/iio/accel/st_accel_spi.c b/drivers/iio/accel/st_accel_spi.c
index fcd5847a3fd3..efd43941d45d 100644
--- a/drivers/iio/accel/st_accel_spi.c
+++ b/drivers/iio/accel/st_accel_spi.c
@@ -59,6 +59,7 @@ static const struct spi_device_id st_accel_id_table[] = {
59 { LSM330_ACCEL_DEV_NAME }, 59 { LSM330_ACCEL_DEV_NAME },
60 { LSM303AGR_ACCEL_DEV_NAME }, 60 { LSM303AGR_ACCEL_DEV_NAME },
61 { LIS2DH12_ACCEL_DEV_NAME }, 61 { LIS2DH12_ACCEL_DEV_NAME },
62 { LIS3L02DQ_ACCEL_DEV_NAME },
62 {}, 63 {},
63}; 64};
64MODULE_DEVICE_TABLE(spi, st_accel_id_table); 65MODULE_DEVICE_TABLE(spi, st_accel_id_table);
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 25378c5882e2..1de31bdd4ce4 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -153,6 +153,18 @@ config AXP288_ADC
153 To compile this driver as a module, choose M here: the module will be 153 To compile this driver as a module, choose M here: the module will be
154 called axp288_adc. 154 called axp288_adc.
155 155
156config BCM_IPROC_ADC
157 tristate "Broadcom IPROC ADC driver"
158 depends on ARCH_BCM_IPROC || COMPILE_TEST
159 depends on MFD_SYSCON
160 default ARCH_BCM_CYGNUS
161 help
162 Say Y here if you want to add support for the Broadcom static
163 ADC driver.
164
165 Broadcom iProc ADC driver. Broadcom iProc ADC controller has 8
166 channels. The driver allows the user to read voltage values.
167
156config BERLIN2_ADC 168config BERLIN2_ADC
157 tristate "Marvell Berlin2 ADC driver" 169 tristate "Marvell Berlin2 ADC driver"
158 depends on ARCH_BERLIN 170 depends on ARCH_BERLIN
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 38638d46f972..0ba0d500eedb 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_AD799X) += ad799x.o
16obj-$(CONFIG_AT91_ADC) += at91_adc.o 16obj-$(CONFIG_AT91_ADC) += at91_adc.o
17obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o 17obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o
18obj-$(CONFIG_AXP288_ADC) += axp288_adc.o 18obj-$(CONFIG_AXP288_ADC) += axp288_adc.o
19obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o
19obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o 20obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o
20obj-$(CONFIG_CC10001_ADC) += cc10001_adc.o 21obj-$(CONFIG_CC10001_ADC) += cc10001_adc.o
21obj-$(CONFIG_DA9150_GPADC) += da9150-gpadc.o 22obj-$(CONFIG_DA9150_GPADC) += da9150-gpadc.o
diff --git a/drivers/iio/adc/ad7266.c b/drivers/iio/adc/ad7266.c
index 11c65db14747..c0f6a98fd9bd 100644
--- a/drivers/iio/adc/ad7266.c
+++ b/drivers/iio/adc/ad7266.c
@@ -440,6 +440,7 @@ static int ad7266_probe(struct spi_device *spi)
440 st->spi = spi; 440 st->spi = spi;
441 441
442 indio_dev->dev.parent = &spi->dev; 442 indio_dev->dev.parent = &spi->dev;
443 indio_dev->dev.of_node = spi->dev.of_node;
443 indio_dev->name = spi_get_device_id(spi)->name; 444 indio_dev->name = spi_get_device_id(spi)->name;
444 indio_dev->modes = INDIO_DIRECT_MODE; 445 indio_dev->modes = INDIO_DIRECT_MODE;
445 indio_dev->info = &ad7266_info; 446 indio_dev->info = &ad7266_info;
diff --git a/drivers/iio/adc/ad7291.c b/drivers/iio/adc/ad7291.c
index c0eabf156702..1d90b02732bb 100644
--- a/drivers/iio/adc/ad7291.c
+++ b/drivers/iio/adc/ad7291.c
@@ -115,7 +115,7 @@ static irqreturn_t ad7291_event_handler(int irq, void *private)
115 u16 t_status, v_status; 115 u16 t_status, v_status;
116 u16 command; 116 u16 command;
117 int i; 117 int i;
118 s64 timestamp = iio_get_time_ns(); 118 s64 timestamp = iio_get_time_ns(indio_dev);
119 119
120 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status)) 120 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
121 return IRQ_HANDLED; 121 return IRQ_HANDLED;
@@ -505,6 +505,7 @@ static int ad7291_probe(struct i2c_client *client,
505 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels); 505 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
506 506
507 indio_dev->dev.parent = &client->dev; 507 indio_dev->dev.parent = &client->dev;
508 indio_dev->dev.of_node = client->dev.of_node;
508 indio_dev->info = &ad7291_info; 509 indio_dev->info = &ad7291_info;
509 indio_dev->modes = INDIO_DIRECT_MODE; 510 indio_dev->modes = INDIO_DIRECT_MODE;
510 511
diff --git a/drivers/iio/adc/ad7298.c b/drivers/iio/adc/ad7298.c
index 62bb8f7ce4a0..10ec8fce395f 100644
--- a/drivers/iio/adc/ad7298.c
+++ b/drivers/iio/adc/ad7298.c
@@ -163,7 +163,7 @@ static irqreturn_t ad7298_trigger_handler(int irq, void *p)
163 goto done; 163 goto done;
164 164
165 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf, 165 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
166 iio_get_time_ns()); 166 iio_get_time_ns(indio_dev));
167 167
168done: 168done:
169 iio_trigger_notify_done(indio_dev->trig); 169 iio_trigger_notify_done(indio_dev->trig);
@@ -315,6 +315,7 @@ static int ad7298_probe(struct spi_device *spi)
315 315
316 indio_dev->name = spi_get_device_id(spi)->name; 316 indio_dev->name = spi_get_device_id(spi)->name;
317 indio_dev->dev.parent = &spi->dev; 317 indio_dev->dev.parent = &spi->dev;
318 indio_dev->dev.of_node = spi->dev.of_node;
318 indio_dev->modes = INDIO_DIRECT_MODE; 319 indio_dev->modes = INDIO_DIRECT_MODE;
319 indio_dev->channels = ad7298_channels; 320 indio_dev->channels = ad7298_channels;
320 indio_dev->num_channels = ARRAY_SIZE(ad7298_channels); 321 indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
diff --git a/drivers/iio/adc/ad7476.c b/drivers/iio/adc/ad7476.c
index 810c9a9fa62f..b7ecf9aab90f 100644
--- a/drivers/iio/adc/ad7476.c
+++ b/drivers/iio/adc/ad7476.c
@@ -70,7 +70,7 @@ static irqreturn_t ad7476_trigger_handler(int irq, void *p)
70 goto done; 70 goto done;
71 71
72 iio_push_to_buffers_with_timestamp(indio_dev, st->data, 72 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
73 iio_get_time_ns()); 73 iio_get_time_ns(indio_dev));
74done: 74done:
75 iio_trigger_notify_done(indio_dev->trig); 75 iio_trigger_notify_done(indio_dev->trig);
76 76
@@ -227,6 +227,7 @@ static int ad7476_probe(struct spi_device *spi)
227 227
228 /* Establish that the iio_dev is a child of the spi device */ 228 /* Establish that the iio_dev is a child of the spi device */
229 indio_dev->dev.parent = &spi->dev; 229 indio_dev->dev.parent = &spi->dev;
230 indio_dev->dev.of_node = spi->dev.of_node;
230 indio_dev->name = spi_get_device_id(spi)->name; 231 indio_dev->name = spi_get_device_id(spi)->name;
231 indio_dev->modes = INDIO_DIRECT_MODE; 232 indio_dev->modes = INDIO_DIRECT_MODE;
232 indio_dev->channels = st->chip_info->channel; 233 indio_dev->channels = st->chip_info->channel;
diff --git a/drivers/iio/adc/ad7791.c b/drivers/iio/adc/ad7791.c
index 1dfe6410c64c..1817ebf5ad84 100644
--- a/drivers/iio/adc/ad7791.c
+++ b/drivers/iio/adc/ad7791.c
@@ -375,6 +375,7 @@ static int ad7791_probe(struct spi_device *spi)
375 spi_set_drvdata(spi, indio_dev); 375 spi_set_drvdata(spi, indio_dev);
376 376
377 indio_dev->dev.parent = &spi->dev; 377 indio_dev->dev.parent = &spi->dev;
378 indio_dev->dev.of_node = spi->dev.of_node;
378 indio_dev->name = spi_get_device_id(spi)->name; 379 indio_dev->name = spi_get_device_id(spi)->name;
379 indio_dev->modes = INDIO_DIRECT_MODE; 380 indio_dev->modes = INDIO_DIRECT_MODE;
380 indio_dev->channels = st->info->channels; 381 indio_dev->channels = st->info->channels;
diff --git a/drivers/iio/adc/ad7793.c b/drivers/iio/adc/ad7793.c
index a43722fbf03a..847789bae821 100644
--- a/drivers/iio/adc/ad7793.c
+++ b/drivers/iio/adc/ad7793.c
@@ -784,6 +784,7 @@ static int ad7793_probe(struct spi_device *spi)
784 spi_set_drvdata(spi, indio_dev); 784 spi_set_drvdata(spi, indio_dev);
785 785
786 indio_dev->dev.parent = &spi->dev; 786 indio_dev->dev.parent = &spi->dev;
787 indio_dev->dev.of_node = spi->dev.of_node;
787 indio_dev->name = spi_get_device_id(spi)->name; 788 indio_dev->name = spi_get_device_id(spi)->name;
788 indio_dev->modes = INDIO_DIRECT_MODE; 789 indio_dev->modes = INDIO_DIRECT_MODE;
789 indio_dev->channels = st->chip_info->channels; 790 indio_dev->channels = st->chip_info->channels;
diff --git a/drivers/iio/adc/ad7887.c b/drivers/iio/adc/ad7887.c
index ee2ccc19fab6..7a483bfbd70c 100644
--- a/drivers/iio/adc/ad7887.c
+++ b/drivers/iio/adc/ad7887.c
@@ -122,7 +122,7 @@ static irqreturn_t ad7887_trigger_handler(int irq, void *p)
122 goto done; 122 goto done;
123 123
124 iio_push_to_buffers_with_timestamp(indio_dev, st->data, 124 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
125 iio_get_time_ns()); 125 iio_get_time_ns(indio_dev));
126done: 126done:
127 iio_trigger_notify_done(indio_dev->trig); 127 iio_trigger_notify_done(indio_dev->trig);
128 128
@@ -264,6 +264,7 @@ static int ad7887_probe(struct spi_device *spi)
264 264
265 /* Estabilish that the iio_dev is a child of the spi device */ 265 /* Estabilish that the iio_dev is a child of the spi device */
266 indio_dev->dev.parent = &spi->dev; 266 indio_dev->dev.parent = &spi->dev;
267 indio_dev->dev.of_node = spi->dev.of_node;
267 indio_dev->name = spi_get_device_id(spi)->name; 268 indio_dev->name = spi_get_device_id(spi)->name;
268 indio_dev->info = &ad7887_info; 269 indio_dev->info = &ad7887_info;
269 indio_dev->modes = INDIO_DIRECT_MODE; 270 indio_dev->modes = INDIO_DIRECT_MODE;
diff --git a/drivers/iio/adc/ad7923.c b/drivers/iio/adc/ad7923.c
index ff444c19d749..77a675e11ebb 100644
--- a/drivers/iio/adc/ad7923.c
+++ b/drivers/iio/adc/ad7923.c
@@ -181,7 +181,7 @@ static irqreturn_t ad7923_trigger_handler(int irq, void *p)
181 goto done; 181 goto done;
182 182
183 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf, 183 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
184 iio_get_time_ns()); 184 iio_get_time_ns(indio_dev));
185 185
186done: 186done:
187 iio_trigger_notify_done(indio_dev->trig); 187 iio_trigger_notify_done(indio_dev->trig);
@@ -288,6 +288,7 @@ static int ad7923_probe(struct spi_device *spi)
288 288
289 indio_dev->name = spi_get_device_id(spi)->name; 289 indio_dev->name = spi_get_device_id(spi)->name;
290 indio_dev->dev.parent = &spi->dev; 290 indio_dev->dev.parent = &spi->dev;
291 indio_dev->dev.of_node = spi->dev.of_node;
291 indio_dev->modes = INDIO_DIRECT_MODE; 292 indio_dev->modes = INDIO_DIRECT_MODE;
292 indio_dev->channels = info->channels; 293 indio_dev->channels = info->channels;
293 indio_dev->num_channels = info->num_channels; 294 indio_dev->num_channels = info->num_channels;
diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
index ec0200dd52cb..b6163764489c 100644
--- a/drivers/iio/adc/ad799x.c
+++ b/drivers/iio/adc/ad799x.c
@@ -212,7 +212,7 @@ static irqreturn_t ad799x_trigger_handler(int irq, void *p)
212 goto out; 212 goto out;
213 213
214 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf, 214 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
215 iio_get_time_ns()); 215 iio_get_time_ns(indio_dev));
216out: 216out:
217 iio_trigger_notify_done(indio_dev->trig); 217 iio_trigger_notify_done(indio_dev->trig);
218 218
@@ -502,7 +502,7 @@ static irqreturn_t ad799x_event_handler(int irq, void *private)
502 (i >> 1), 502 (i >> 1),
503 IIO_EV_TYPE_THRESH, 503 IIO_EV_TYPE_THRESH,
504 IIO_EV_DIR_FALLING), 504 IIO_EV_DIR_FALLING),
505 iio_get_time_ns()); 505 iio_get_time_ns(indio_dev));
506 } 506 }
507 507
508done: 508done:
@@ -806,6 +806,7 @@ static int ad799x_probe(struct i2c_client *client,
806 st->client = client; 806 st->client = client;
807 807
808 indio_dev->dev.parent = &client->dev; 808 indio_dev->dev.parent = &client->dev;
809 indio_dev->dev.of_node = client->dev.of_node;
809 indio_dev->name = id->name; 810 indio_dev->name = id->name;
810 indio_dev->info = st->chip_config->info; 811 indio_dev->info = st->chip_config->info;
811 812
diff --git a/drivers/iio/adc/bcm_iproc_adc.c b/drivers/iio/adc/bcm_iproc_adc.c
new file mode 100644
index 000000000000..21d38c8af21e
--- /dev/null
+++ b/drivers/iio/adc/bcm_iproc_adc.c
@@ -0,0 +1,644 @@
1/*
2 * Copyright 2016 Broadcom
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2, as
6 * published by the Free Software Foundation (the "GPL").
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License version 2 (GPLv2) for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * version 2 (GPLv2) along with this source code.
15 */
16
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/io.h>
20#include <linux/clk.h>
21#include <linux/mfd/syscon.h>
22#include <linux/regmap.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/platform_device.h>
26
27#include <linux/iio/iio.h>
28
29/* Below Register's are common to IPROC ADC and Touchscreen IP */
30#define IPROC_REGCTL1 0x00
31#define IPROC_REGCTL2 0x04
32#define IPROC_INTERRUPT_THRES 0x08
33#define IPROC_INTERRUPT_MASK 0x0c
34#define IPROC_INTERRUPT_STATUS 0x10
35#define IPROC_ANALOG_CONTROL 0x1c
36#define IPROC_CONTROLLER_STATUS 0x14
37#define IPROC_AUX_DATA 0x20
38#define IPROC_SOFT_BYPASS_CONTROL 0x38
39#define IPROC_SOFT_BYPASS_DATA 0x3C
40
41/* IPROC ADC Channel register offsets */
42#define IPROC_ADC_CHANNEL_REGCTL1 0x800
43#define IPROC_ADC_CHANNEL_REGCTL2 0x804
44#define IPROC_ADC_CHANNEL_STATUS 0x808
45#define IPROC_ADC_CHANNEL_INTERRUPT_STATUS 0x80c
46#define IPROC_ADC_CHANNEL_INTERRUPT_MASK 0x810
47#define IPROC_ADC_CHANNEL_DATA 0x814
48#define IPROC_ADC_CHANNEL_OFFSET 0x20
49
50/* Bit definitions for IPROC_REGCTL2 */
51#define IPROC_ADC_AUXIN_SCAN_ENA BIT(0)
52#define IPROC_ADC_PWR_LDO BIT(5)
53#define IPROC_ADC_PWR_ADC BIT(4)
54#define IPROC_ADC_PWR_BG BIT(3)
55#define IPROC_ADC_CONTROLLER_EN BIT(17)
56
57/* Bit definitions for IPROC_INTERRUPT_MASK and IPROC_INTERRUPT_STATUS */
58#define IPROC_ADC_AUXDATA_RDY_INTR BIT(3)
59#define IPROC_ADC_INTR 9
60#define IPROC_ADC_INTR_MASK (0xFF << IPROC_ADC_INTR)
61
62/* Bit definitions for IPROC_ANALOG_CONTROL */
63#define IPROC_ADC_CHANNEL_SEL 11
64#define IPROC_ADC_CHANNEL_SEL_MASK (0x7 << IPROC_ADC_CHANNEL_SEL)
65
66/* Bit definitions for IPROC_ADC_CHANNEL_REGCTL1 */
67#define IPROC_ADC_CHANNEL_ROUNDS 0x2
68#define IPROC_ADC_CHANNEL_ROUNDS_MASK (0x3F << IPROC_ADC_CHANNEL_ROUNDS)
69#define IPROC_ADC_CHANNEL_MODE 0x1
70#define IPROC_ADC_CHANNEL_MODE_MASK (0x1 << IPROC_ADC_CHANNEL_MODE)
71#define IPROC_ADC_CHANNEL_MODE_TDM 0x1
72#define IPROC_ADC_CHANNEL_MODE_SNAPSHOT 0x0
73#define IPROC_ADC_CHANNEL_ENABLE 0x0
74#define IPROC_ADC_CHANNEL_ENABLE_MASK 0x1
75
76/* Bit definitions for IPROC_ADC_CHANNEL_REGCTL2 */
77#define IPROC_ADC_CHANNEL_WATERMARK 0x0
78#define IPROC_ADC_CHANNEL_WATERMARK_MASK \
79 (0x3F << IPROC_ADC_CHANNEL_WATERMARK)
80
81#define IPROC_ADC_WATER_MARK_LEVEL 0x1
82
83/* Bit definitions for IPROC_ADC_CHANNEL_STATUS */
84#define IPROC_ADC_CHANNEL_DATA_LOST 0x0
85#define IPROC_ADC_CHANNEL_DATA_LOST_MASK \
86 (0x0 << IPROC_ADC_CHANNEL_DATA_LOST)
87#define IPROC_ADC_CHANNEL_VALID_ENTERIES 0x1
88#define IPROC_ADC_CHANNEL_VALID_ENTERIES_MASK \
89 (0xFF << IPROC_ADC_CHANNEL_VALID_ENTERIES)
90#define IPROC_ADC_CHANNEL_TOTAL_ENTERIES 0x9
91#define IPROC_ADC_CHANNEL_TOTAL_ENTERIES_MASK \
92 (0xFF << IPROC_ADC_CHANNEL_TOTAL_ENTERIES)
93
94/* Bit definitions for IPROC_ADC_CHANNEL_INTERRUPT_MASK */
95#define IPROC_ADC_CHANNEL_WTRMRK_INTR 0x0
96#define IPROC_ADC_CHANNEL_WTRMRK_INTR_MASK \
97 (0x1 << IPROC_ADC_CHANNEL_WTRMRK_INTR)
98#define IPROC_ADC_CHANNEL_FULL_INTR 0x1
99#define IPROC_ADC_CHANNEL_FULL_INTR_MASK \
100 (0x1 << IPROC_ADC_IPROC_ADC_CHANNEL_FULL_INTR)
101#define IPROC_ADC_CHANNEL_EMPTY_INTR 0x2
102#define IPROC_ADC_CHANNEL_EMPTY_INTR_MASK \
103 (0x1 << IPROC_ADC_CHANNEL_EMPTY_INTR)
104
105#define IPROC_ADC_WATER_MARK_INTR_ENABLE 0x1
106
107/* Number of time to retry a set of the interrupt mask reg */
108#define IPROC_ADC_INTMASK_RETRY_ATTEMPTS 10
109
110#define IPROC_ADC_READ_TIMEOUT (HZ*2)
111
112#define iproc_adc_dbg_reg(dev, priv, reg) \
113do { \
114 u32 val; \
115 regmap_read(priv->regmap, reg, &val); \
116 dev_dbg(dev, "%20s= 0x%08x\n", #reg, val); \
117} while (0)
118
119struct iproc_adc_priv {
120 struct regmap *regmap;
121 struct clk *adc_clk;
122 struct mutex mutex;
123 int irqno;
124 int chan_val;
125 int chan_id;
126 struct completion completion;
127};
128
129static void iproc_adc_reg_dump(struct iio_dev *indio_dev)
130{
131 struct device *dev = &indio_dev->dev;
132 struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
133
134 iproc_adc_dbg_reg(dev, adc_priv, IPROC_REGCTL1);
135 iproc_adc_dbg_reg(dev, adc_priv, IPROC_REGCTL2);
136 iproc_adc_dbg_reg(dev, adc_priv, IPROC_INTERRUPT_THRES);
137 iproc_adc_dbg_reg(dev, adc_priv, IPROC_INTERRUPT_MASK);
138 iproc_adc_dbg_reg(dev, adc_priv, IPROC_INTERRUPT_STATUS);
139 iproc_adc_dbg_reg(dev, adc_priv, IPROC_CONTROLLER_STATUS);
140 iproc_adc_dbg_reg(dev, adc_priv, IPROC_ANALOG_CONTROL);
141 iproc_adc_dbg_reg(dev, adc_priv, IPROC_AUX_DATA);
142 iproc_adc_dbg_reg(dev, adc_priv, IPROC_SOFT_BYPASS_CONTROL);
143 iproc_adc_dbg_reg(dev, adc_priv, IPROC_SOFT_BYPASS_DATA);
144}
145
146static irqreturn_t iproc_adc_interrupt_handler(int irq, void *data)
147{
148 u32 channel_intr_status;
149 u32 intr_status;
150 u32 intr_mask;
151 struct iio_dev *indio_dev = data;
152 struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
153
154 /*
155 * This interrupt is shared with the touchscreen driver.
156 * Make sure this interrupt is intended for us.
157 * Handle only ADC channel specific interrupts.
158 */
159 regmap_read(adc_priv->regmap, IPROC_INTERRUPT_STATUS, &intr_status);
160 regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &intr_mask);
161 intr_status = intr_status & intr_mask;
162 channel_intr_status = (intr_status & IPROC_ADC_INTR_MASK) >>
163 IPROC_ADC_INTR;
164 if (channel_intr_status)
165 return IRQ_WAKE_THREAD;
166
167 return IRQ_NONE;
168}
169
170static irqreturn_t iproc_adc_interrupt_thread(int irq, void *data)
171{
172 irqreturn_t retval = IRQ_NONE;
173 struct iproc_adc_priv *adc_priv;
174 struct iio_dev *indio_dev = data;
175 unsigned int valid_entries;
176 u32 intr_status;
177 u32 intr_channels;
178 u32 channel_status;
179 u32 ch_intr_status;
180
181 adc_priv = iio_priv(indio_dev);
182
183 regmap_read(adc_priv->regmap, IPROC_INTERRUPT_STATUS, &intr_status);
184 dev_dbg(&indio_dev->dev, "iproc_adc_interrupt_thread(),INTRPT_STS:%x\n",
185 intr_status);
186
187 intr_channels = (intr_status & IPROC_ADC_INTR_MASK) >> IPROC_ADC_INTR;
188 if (intr_channels) {
189 regmap_read(adc_priv->regmap,
190 IPROC_ADC_CHANNEL_INTERRUPT_STATUS +
191 IPROC_ADC_CHANNEL_OFFSET * adc_priv->chan_id,
192 &ch_intr_status);
193
194 if (ch_intr_status & IPROC_ADC_CHANNEL_WTRMRK_INTR_MASK) {
195 regmap_read(adc_priv->regmap,
196 IPROC_ADC_CHANNEL_STATUS +
197 IPROC_ADC_CHANNEL_OFFSET *
198 adc_priv->chan_id,
199 &channel_status);
200
201 valid_entries = ((channel_status &
202 IPROC_ADC_CHANNEL_VALID_ENTERIES_MASK) >>
203 IPROC_ADC_CHANNEL_VALID_ENTERIES);
204 if (valid_entries >= 1) {
205 regmap_read(adc_priv->regmap,
206 IPROC_ADC_CHANNEL_DATA +
207 IPROC_ADC_CHANNEL_OFFSET *
208 adc_priv->chan_id,
209 &adc_priv->chan_val);
210 complete(&adc_priv->completion);
211 } else {
212 dev_err(&indio_dev->dev,
213 "No data rcvd on channel %d\n",
214 adc_priv->chan_id);
215 }
216 regmap_write(adc_priv->regmap,
217 IPROC_ADC_CHANNEL_INTERRUPT_MASK +
218 IPROC_ADC_CHANNEL_OFFSET *
219 adc_priv->chan_id,
220 (ch_intr_status &
221 ~(IPROC_ADC_CHANNEL_WTRMRK_INTR_MASK)));
222 }
223 regmap_write(adc_priv->regmap,
224 IPROC_ADC_CHANNEL_INTERRUPT_STATUS +
225 IPROC_ADC_CHANNEL_OFFSET * adc_priv->chan_id,
226 ch_intr_status);
227 regmap_write(adc_priv->regmap, IPROC_INTERRUPT_STATUS,
228 intr_channels);
229 retval = IRQ_HANDLED;
230 }
231
232 return retval;
233}
234
235static int iproc_adc_do_read(struct iio_dev *indio_dev,
236 int channel,
237 u16 *p_adc_data)
238{
239 int read_len = 0;
240 u32 val;
241 u32 mask;
242 u32 val_check;
243 int failed_cnt = 0;
244 struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
245
246 mutex_lock(&adc_priv->mutex);
247
248 /*
249 * After a read is complete the ADC interrupts will be disabled so
250 * we can assume this section of code is safe from interrupts.
251 */
252 adc_priv->chan_val = -1;
253 adc_priv->chan_id = channel;
254
255 reinit_completion(&adc_priv->completion);
256 /* Clear any pending interrupt */
257 regmap_update_bits(adc_priv->regmap, IPROC_INTERRUPT_STATUS,
258 IPROC_ADC_INTR_MASK | IPROC_ADC_AUXDATA_RDY_INTR,
259 ((0x0 << channel) << IPROC_ADC_INTR) |
260 IPROC_ADC_AUXDATA_RDY_INTR);
261
262 /* Configure channel for snapshot mode and enable */
263 val = (BIT(IPROC_ADC_CHANNEL_ROUNDS) |
264 (IPROC_ADC_CHANNEL_MODE_SNAPSHOT << IPROC_ADC_CHANNEL_MODE) |
265 (0x1 << IPROC_ADC_CHANNEL_ENABLE));
266
267 mask = IPROC_ADC_CHANNEL_ROUNDS_MASK | IPROC_ADC_CHANNEL_MODE_MASK |
268 IPROC_ADC_CHANNEL_ENABLE_MASK;
269 regmap_update_bits(adc_priv->regmap, (IPROC_ADC_CHANNEL_REGCTL1 +
270 IPROC_ADC_CHANNEL_OFFSET * channel),
271 mask, val);
272
273 /* Set the Watermark for a channel */
274 regmap_update_bits(adc_priv->regmap, (IPROC_ADC_CHANNEL_REGCTL2 +
275 IPROC_ADC_CHANNEL_OFFSET * channel),
276 IPROC_ADC_CHANNEL_WATERMARK_MASK,
277 0x1);
278
279 /* Enable water mark interrupt */
280 regmap_update_bits(adc_priv->regmap, (IPROC_ADC_CHANNEL_INTERRUPT_MASK +
281 IPROC_ADC_CHANNEL_OFFSET *
282 channel),
283 IPROC_ADC_CHANNEL_WTRMRK_INTR_MASK,
284 IPROC_ADC_WATER_MARK_INTR_ENABLE);
285 regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &val);
286
287 /* Enable ADC interrupt for a channel */
288 val |= (BIT(channel) << IPROC_ADC_INTR);
289 regmap_write(adc_priv->regmap, IPROC_INTERRUPT_MASK, val);
290
291 /*
292 * There seems to be a very rare issue where writing to this register
293 * does not take effect. To work around the issue we will try multiple
294 * writes. In total we will spend about 10*10 = 100 us attempting this.
295 * Testing has shown that this may loop a few time, but we have never
296 * hit the full count.
297 */
298 regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &val_check);
299 while (val_check != val) {
300 failed_cnt++;
301
302 if (failed_cnt > IPROC_ADC_INTMASK_RETRY_ATTEMPTS)
303 break;
304
305 udelay(10);
306 regmap_update_bits(adc_priv->regmap, IPROC_INTERRUPT_MASK,
307 IPROC_ADC_INTR_MASK,
308 ((0x1 << channel) <<
309 IPROC_ADC_INTR));
310
311 regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &val_check);
312 }
313
314 if (failed_cnt) {
315 dev_dbg(&indio_dev->dev,
316 "IntMask failed (%d times)", failed_cnt);
317 if (failed_cnt > IPROC_ADC_INTMASK_RETRY_ATTEMPTS) {
318 dev_err(&indio_dev->dev,
319 "IntMask set failed. Read will likely fail.");
320 read_len = -EIO;
321 goto adc_err;
322 };
323 }
324 regmap_read(adc_priv->regmap, IPROC_INTERRUPT_MASK, &val_check);
325
326 if (wait_for_completion_timeout(&adc_priv->completion,
327 IPROC_ADC_READ_TIMEOUT) > 0) {
328
329 /* Only the lower 16 bits are relevant */
330 *p_adc_data = adc_priv->chan_val & 0xFFFF;
331 read_len = sizeof(*p_adc_data);
332
333 } else {
334 /*
335 * We never got the interrupt, something went wrong.
336 * Perhaps the interrupt may still be coming, we do not want
337 * that now. Lets disable the ADC interrupt, and clear the
338 * status to put it back in to normal state.
339 */
340 read_len = -ETIMEDOUT;
341 goto adc_err;
342 }
343 mutex_unlock(&adc_priv->mutex);
344
345 return read_len;
346
347adc_err:
348 regmap_update_bits(adc_priv->regmap, IPROC_INTERRUPT_MASK,
349 IPROC_ADC_INTR_MASK,
350 ((0x0 << channel) << IPROC_ADC_INTR));
351
352 regmap_update_bits(adc_priv->regmap, IPROC_INTERRUPT_STATUS,
353 IPROC_ADC_INTR_MASK,
354 ((0x0 << channel) << IPROC_ADC_INTR));
355
356 dev_err(&indio_dev->dev, "Timed out waiting for ADC data!\n");
357 iproc_adc_reg_dump(indio_dev);
358 mutex_unlock(&adc_priv->mutex);
359
360 return read_len;
361}
362
363static int iproc_adc_enable(struct iio_dev *indio_dev)
364{
365 u32 val;
366 u32 channel_id;
367 struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
368 int ret;
369
370 /* Set i_amux = 3b'000, select channel 0 */
371 ret = regmap_update_bits(adc_priv->regmap, IPROC_ANALOG_CONTROL,
372 IPROC_ADC_CHANNEL_SEL_MASK, 0);
373 if (ret) {
374 dev_err(&indio_dev->dev,
375 "failed to write IPROC_ANALOG_CONTROL %d\n", ret);
376 return ret;
377 }
378 adc_priv->chan_val = -1;
379
380 /*
381 * PWR up LDO, ADC, and Band Gap (0 to enable)
382 * Also enable ADC controller (set high)
383 */
384 ret = regmap_read(adc_priv->regmap, IPROC_REGCTL2, &val);
385 if (ret) {
386 dev_err(&indio_dev->dev,
387 "failed to read IPROC_REGCTL2 %d\n", ret);
388 return ret;
389 }
390
391 val &= ~(IPROC_ADC_PWR_LDO | IPROC_ADC_PWR_ADC | IPROC_ADC_PWR_BG);
392
393 ret = regmap_write(adc_priv->regmap, IPROC_REGCTL2, val);
394 if (ret) {
395 dev_err(&indio_dev->dev,
396 "failed to write IPROC_REGCTL2 %d\n", ret);
397 return ret;
398 }
399
400 ret = regmap_read(adc_priv->regmap, IPROC_REGCTL2, &val);
401 if (ret) {
402 dev_err(&indio_dev->dev,
403 "failed to read IPROC_REGCTL2 %d\n", ret);
404 return ret;
405 }
406
407 val |= IPROC_ADC_CONTROLLER_EN;
408 ret = regmap_write(adc_priv->regmap, IPROC_REGCTL2, val);
409 if (ret) {
410 dev_err(&indio_dev->dev,
411 "failed to write IPROC_REGCTL2 %d\n", ret);
412 return ret;
413 }
414
415 for (channel_id = 0; channel_id < indio_dev->num_channels;
416 channel_id++) {
417 ret = regmap_write(adc_priv->regmap,
418 IPROC_ADC_CHANNEL_INTERRUPT_MASK +
419 IPROC_ADC_CHANNEL_OFFSET * channel_id, 0);
420 if (ret) {
421 dev_err(&indio_dev->dev,
422 "failed to write ADC_CHANNEL_INTERRUPT_MASK %d\n",
423 ret);
424 return ret;
425 }
426
427 ret = regmap_write(adc_priv->regmap,
428 IPROC_ADC_CHANNEL_INTERRUPT_STATUS +
429 IPROC_ADC_CHANNEL_OFFSET * channel_id, 0);
430 if (ret) {
431 dev_err(&indio_dev->dev,
432 "failed to write ADC_CHANNEL_INTERRUPT_STATUS %d\n",
433 ret);
434 return ret;
435 }
436 }
437
438 return 0;
439}
440
441static void iproc_adc_disable(struct iio_dev *indio_dev)
442{
443 u32 val;
444 int ret;
445 struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
446
447 ret = regmap_read(adc_priv->regmap, IPROC_REGCTL2, &val);
448 if (ret) {
449 dev_err(&indio_dev->dev,
450 "failed to read IPROC_REGCTL2 %d\n", ret);
451 return;
452 }
453
454 val &= ~IPROC_ADC_CONTROLLER_EN;
455 ret = regmap_write(adc_priv->regmap, IPROC_REGCTL2, val);
456 if (ret) {
457 dev_err(&indio_dev->dev,
458 "failed to write IPROC_REGCTL2 %d\n", ret);
459 return;
460 }
461}
462
463static int iproc_adc_read_raw(struct iio_dev *indio_dev,
464 struct iio_chan_spec const *chan,
465 int *val,
466 int *val2,
467 long mask)
468{
469 u16 adc_data;
470 int err;
471
472 switch (mask) {
473 case IIO_CHAN_INFO_RAW:
474 err = iproc_adc_do_read(indio_dev, chan->channel, &adc_data);
475 if (err < 0)
476 return err;
477 *val = adc_data;
478 return IIO_VAL_INT;
479 case IIO_CHAN_INFO_SCALE:
480 switch (chan->type) {
481 case IIO_VOLTAGE:
482 *val = 1800;
483 *val2 = 10;
484 return IIO_VAL_FRACTIONAL_LOG2;
485 default:
486 return -EINVAL;
487 }
488 default:
489 return -EINVAL;
490 }
491}
492
493static const struct iio_info iproc_adc_iio_info = {
494 .read_raw = &iproc_adc_read_raw,
495 .driver_module = THIS_MODULE,
496};
497
498#define IPROC_ADC_CHANNEL(_index, _id) { \
499 .type = IIO_VOLTAGE, \
500 .indexed = 1, \
501 .channel = _index, \
502 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
503 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
504 .datasheet_name = _id, \
505}
506
507static const struct iio_chan_spec iproc_adc_iio_channels[] = {
508 IPROC_ADC_CHANNEL(0, "adc0"),
509 IPROC_ADC_CHANNEL(1, "adc1"),
510 IPROC_ADC_CHANNEL(2, "adc2"),
511 IPROC_ADC_CHANNEL(3, "adc3"),
512 IPROC_ADC_CHANNEL(4, "adc4"),
513 IPROC_ADC_CHANNEL(5, "adc5"),
514 IPROC_ADC_CHANNEL(6, "adc6"),
515 IPROC_ADC_CHANNEL(7, "adc7"),
516};
517
518static int iproc_adc_probe(struct platform_device *pdev)
519{
520 struct iproc_adc_priv *adc_priv;
521 struct iio_dev *indio_dev = NULL;
522 int ret;
523
524 indio_dev = devm_iio_device_alloc(&pdev->dev,
525 sizeof(*adc_priv));
526 if (!indio_dev) {
527 dev_err(&pdev->dev, "failed to allocate iio device\n");
528 return -ENOMEM;
529 }
530
531 adc_priv = iio_priv(indio_dev);
532 platform_set_drvdata(pdev, indio_dev);
533
534 mutex_init(&adc_priv->mutex);
535
536 init_completion(&adc_priv->completion);
537
538 adc_priv->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
539 "adc-syscon");
540 if (IS_ERR(adc_priv->regmap)) {
541 dev_err(&pdev->dev, "failed to get handle for tsc syscon\n");
542 ret = PTR_ERR(adc_priv->regmap);
543 return ret;
544 }
545
546 adc_priv->adc_clk = devm_clk_get(&pdev->dev, "tsc_clk");
547 if (IS_ERR(adc_priv->adc_clk)) {
548 dev_err(&pdev->dev,
549 "failed getting clock tsc_clk\n");
550 ret = PTR_ERR(adc_priv->adc_clk);
551 return ret;
552 }
553
554 adc_priv->irqno = platform_get_irq(pdev, 0);
555 if (adc_priv->irqno <= 0) {
556 dev_err(&pdev->dev, "platform_get_irq failed\n");
557 ret = -ENODEV;
558 return ret;
559 }
560
561 ret = regmap_update_bits(adc_priv->regmap, IPROC_REGCTL2,
562 IPROC_ADC_AUXIN_SCAN_ENA, 0);
563 if (ret) {
564 dev_err(&pdev->dev, "failed to write IPROC_REGCTL2 %d\n", ret);
565 return ret;
566 }
567
568 ret = devm_request_threaded_irq(&pdev->dev, adc_priv->irqno,
569 iproc_adc_interrupt_thread,
570 iproc_adc_interrupt_handler,
571 IRQF_SHARED, "iproc-adc", indio_dev);
572 if (ret) {
573 dev_err(&pdev->dev, "request_irq error %d\n", ret);
574 return ret;
575 }
576
577 ret = clk_prepare_enable(adc_priv->adc_clk);
578 if (ret) {
579 dev_err(&pdev->dev,
580 "clk_prepare_enable failed %d\n", ret);
581 return ret;
582 }
583
584 ret = iproc_adc_enable(indio_dev);
585 if (ret) {
586 dev_err(&pdev->dev, "failed to enable adc %d\n", ret);
587 goto err_adc_enable;
588 }
589
590 indio_dev->name = "iproc-static-adc";
591 indio_dev->dev.parent = &pdev->dev;
592 indio_dev->dev.of_node = pdev->dev.of_node;
593 indio_dev->info = &iproc_adc_iio_info;
594 indio_dev->modes = INDIO_DIRECT_MODE;
595 indio_dev->channels = iproc_adc_iio_channels;
596 indio_dev->num_channels = ARRAY_SIZE(iproc_adc_iio_channels);
597
598 ret = iio_device_register(indio_dev);
599 if (ret) {
600 dev_err(&pdev->dev, "iio_device_register failed:err %d\n", ret);
601 goto err_clk;
602 }
603
604 return 0;
605
606err_clk:
607 iproc_adc_disable(indio_dev);
608err_adc_enable:
609 clk_disable_unprepare(adc_priv->adc_clk);
610
611 return ret;
612}
613
614static int iproc_adc_remove(struct platform_device *pdev)
615{
616 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
617 struct iproc_adc_priv *adc_priv = iio_priv(indio_dev);
618
619 iio_device_unregister(indio_dev);
620 iproc_adc_disable(indio_dev);
621 clk_disable_unprepare(adc_priv->adc_clk);
622
623 return 0;
624}
625
626static const struct of_device_id iproc_adc_of_match[] = {
627 {.compatible = "brcm,iproc-static-adc", },
628 { },
629};
630MODULE_DEVICE_TABLE(of, iproc_adc_of_match);
631
632static struct platform_driver iproc_adc_driver = {
633 .probe = iproc_adc_probe,
634 .remove = iproc_adc_remove,
635 .driver = {
636 .name = "iproc-static-adc",
637 .of_match_table = of_match_ptr(iproc_adc_of_match),
638 },
639};
640module_platform_driver(iproc_adc_driver);
641
642MODULE_DESCRIPTION("Broadcom iProc ADC controller driver");
643MODULE_AUTHOR("Raveendra Padasalagi <raveendra.padasalagi@broadcom.com>");
644MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/cc10001_adc.c b/drivers/iio/adc/cc10001_adc.c
index 8254f529b2a9..91636c0ba5b5 100644
--- a/drivers/iio/adc/cc10001_adc.c
+++ b/drivers/iio/adc/cc10001_adc.c
@@ -186,7 +186,7 @@ done:
186 186
187 if (!sample_invalid) 187 if (!sample_invalid)
188 iio_push_to_buffers_with_timestamp(indio_dev, data, 188 iio_push_to_buffers_with_timestamp(indio_dev, data,
189 iio_get_time_ns()); 189 iio_get_time_ns(indio_dev));
190 iio_trigger_notify_done(indio_dev->trig); 190 iio_trigger_notify_done(indio_dev->trig);
191 191
192 return IRQ_HANDLED; 192 return IRQ_HANDLED;
diff --git a/drivers/iio/adc/hi8435.c b/drivers/iio/adc/hi8435.c
index c73c6c62a6ac..678e8c7ea763 100644
--- a/drivers/iio/adc/hi8435.c
+++ b/drivers/iio/adc/hi8435.c
@@ -400,7 +400,7 @@ static void hi8435_iio_push_event(struct iio_dev *idev, unsigned int val)
400 iio_push_event(idev, 400 iio_push_event(idev,
401 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i, 401 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
402 IIO_EV_TYPE_THRESH, dir), 402 IIO_EV_TYPE_THRESH, dir),
403 iio_get_time_ns()); 403 iio_get_time_ns(idev));
404 } 404 }
405 } 405 }
406 406
@@ -455,6 +455,7 @@ static int hi8435_probe(struct spi_device *spi)
455 mutex_init(&priv->lock); 455 mutex_init(&priv->lock);
456 456
457 idev->dev.parent = &spi->dev; 457 idev->dev.parent = &spi->dev;
458 idev->dev.of_node = spi->dev.of_node;
458 idev->name = spi_get_device_id(spi)->name; 459 idev->name = spi_get_device_id(spi)->name;
459 idev->modes = INDIO_DIRECT_MODE; 460 idev->modes = INDIO_DIRECT_MODE;
460 idev->info = &hi8435_info; 461 idev->info = &hi8435_info;
diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
index 502f2fbe8aef..955f3fdaf519 100644
--- a/drivers/iio/adc/ina2xx-adc.c
+++ b/drivers/iio/adc/ina2xx-adc.c
@@ -465,7 +465,7 @@ static int ina2xx_work_buffer(struct iio_dev *indio_dev)
465 s64 time_a, time_b; 465 s64 time_a, time_b;
466 unsigned int alert; 466 unsigned int alert;
467 467
468 time_a = iio_get_time_ns(); 468 time_a = iio_get_time_ns(indio_dev);
469 469
470 /* 470 /*
471 * Because the timer thread and the chip conversion clock 471 * Because the timer thread and the chip conversion clock
@@ -504,7 +504,7 @@ static int ina2xx_work_buffer(struct iio_dev *indio_dev)
504 data[i++] = val; 504 data[i++] = val;
505 } 505 }
506 506
507 time_b = iio_get_time_ns(); 507 time_b = iio_get_time_ns(indio_dev);
508 508
509 iio_push_to_buffers_with_timestamp(indio_dev, 509 iio_push_to_buffers_with_timestamp(indio_dev,
510 (unsigned int *)data, time_a); 510 (unsigned int *)data, time_a);
@@ -554,7 +554,7 @@ static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
554 dev_dbg(&indio_dev->dev, "Async readout mode: %d\n", 554 dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
555 chip->allow_async_readout); 555 chip->allow_async_readout);
556 556
557 chip->prev_ns = iio_get_time_ns(); 557 chip->prev_ns = iio_get_time_ns(indio_dev);
558 558
559 chip->task = kthread_run(ina2xx_capture_thread, (void *)indio_dev, 559 chip->task = kthread_run(ina2xx_capture_thread, (void *)indio_dev,
560 "%s:%d-%uus", indio_dev->name, indio_dev->id, 560 "%s:%d-%uus", indio_dev->name, indio_dev->id,
@@ -691,6 +691,7 @@ static int ina2xx_probe(struct i2c_client *client,
691 691
692 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; 692 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
693 indio_dev->dev.parent = &client->dev; 693 indio_dev->dev.parent = &client->dev;
694 indio_dev->dev.of_node = client->dev.of_node;
694 indio_dev->channels = ina2xx_channels; 695 indio_dev->channels = ina2xx_channels;
695 indio_dev->num_channels = ARRAY_SIZE(ina2xx_channels); 696 indio_dev->num_channels = ARRAY_SIZE(ina2xx_channels);
696 indio_dev->name = id->name; 697 indio_dev->name = id->name;
diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
index 41d495c6035e..712fbd2b1f16 100644
--- a/drivers/iio/adc/max1027.c
+++ b/drivers/iio/adc/max1027.c
@@ -426,6 +426,7 @@ static int max1027_probe(struct spi_device *spi)
426 426
427 indio_dev->name = spi_get_device_id(spi)->name; 427 indio_dev->name = spi_get_device_id(spi)->name;
428 indio_dev->dev.parent = &spi->dev; 428 indio_dev->dev.parent = &spi->dev;
429 indio_dev->dev.of_node = spi->dev.of_node;
429 indio_dev->info = &max1027_info; 430 indio_dev->info = &max1027_info;
430 indio_dev->modes = INDIO_DIRECT_MODE; 431 indio_dev->modes = INDIO_DIRECT_MODE;
431 indio_dev->channels = st->info->channels; 432 indio_dev->channels = st->info->channels;
diff --git a/drivers/iio/adc/max1363.c b/drivers/iio/adc/max1363.c
index 998dc3caad4c..841a13c9b6ea 100644
--- a/drivers/iio/adc/max1363.c
+++ b/drivers/iio/adc/max1363.c
@@ -25,6 +25,8 @@
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/err.h> 26#include <linux/err.h>
27#include <linux/module.h> 27#include <linux/module.h>
28#include <linux/of.h>
29#include <linux/of_device.h>
28 30
29#include <linux/iio/iio.h> 31#include <linux/iio/iio.h>
30#include <linux/iio/sysfs.h> 32#include <linux/iio/sysfs.h>
@@ -788,7 +790,7 @@ static irqreturn_t max1363_event_handler(int irq, void *private)
788{ 790{
789 struct iio_dev *indio_dev = private; 791 struct iio_dev *indio_dev = private;
790 struct max1363_state *st = iio_priv(indio_dev); 792 struct max1363_state *st = iio_priv(indio_dev);
791 s64 timestamp = iio_get_time_ns(); 793 s64 timestamp = iio_get_time_ns(indio_dev);
792 unsigned long mask, loc; 794 unsigned long mask, loc;
793 u8 rx; 795 u8 rx;
794 u8 tx[2] = { st->setupbyte, 796 u8 tx[2] = { st->setupbyte,
@@ -1506,7 +1508,8 @@ static irqreturn_t max1363_trigger_handler(int irq, void *p)
1506 if (b_sent < 0) 1508 if (b_sent < 0)
1507 goto done_free; 1509 goto done_free;
1508 1510
1509 iio_push_to_buffers_with_timestamp(indio_dev, rxbuf, iio_get_time_ns()); 1511 iio_push_to_buffers_with_timestamp(indio_dev, rxbuf,
1512 iio_get_time_ns(indio_dev));
1510 1513
1511done_free: 1514done_free:
1512 kfree(rxbuf); 1515 kfree(rxbuf);
@@ -1516,6 +1519,56 @@ done:
1516 return IRQ_HANDLED; 1519 return IRQ_HANDLED;
1517} 1520}
1518 1521
1522#ifdef CONFIG_OF
1523
1524#define MAX1363_COMPATIBLE(of_compatible, cfg) { \
1525 .compatible = of_compatible, \
1526 .data = &max1363_chip_info_tbl[cfg], \
1527}
1528
1529static const struct of_device_id max1363_of_match[] = {
1530 MAX1363_COMPATIBLE("maxim,max1361", max1361),
1531 MAX1363_COMPATIBLE("maxim,max1362", max1362),
1532 MAX1363_COMPATIBLE("maxim,max1363", max1363),
1533 MAX1363_COMPATIBLE("maxim,max1364", max1364),
1534 MAX1363_COMPATIBLE("maxim,max1036", max1036),
1535 MAX1363_COMPATIBLE("maxim,max1037", max1037),
1536 MAX1363_COMPATIBLE("maxim,max1038", max1038),
1537 MAX1363_COMPATIBLE("maxim,max1039", max1039),
1538 MAX1363_COMPATIBLE("maxim,max1136", max1136),
1539 MAX1363_COMPATIBLE("maxim,max1137", max1137),
1540 MAX1363_COMPATIBLE("maxim,max1138", max1138),
1541 MAX1363_COMPATIBLE("maxim,max1139", max1139),
1542 MAX1363_COMPATIBLE("maxim,max1236", max1236),
1543 MAX1363_COMPATIBLE("maxim,max1237", max1237),
1544 MAX1363_COMPATIBLE("maxim,max1238", max1238),
1545 MAX1363_COMPATIBLE("maxim,max1239", max1239),
1546 MAX1363_COMPATIBLE("maxim,max11600", max11600),
1547 MAX1363_COMPATIBLE("maxim,max11601", max11601),
1548 MAX1363_COMPATIBLE("maxim,max11602", max11602),
1549 MAX1363_COMPATIBLE("maxim,max11603", max11603),
1550 MAX1363_COMPATIBLE("maxim,max11604", max11604),
1551 MAX1363_COMPATIBLE("maxim,max11605", max11605),
1552 MAX1363_COMPATIBLE("maxim,max11606", max11606),
1553 MAX1363_COMPATIBLE("maxim,max11607", max11607),
1554 MAX1363_COMPATIBLE("maxim,max11608", max11608),
1555 MAX1363_COMPATIBLE("maxim,max11609", max11609),
1556 MAX1363_COMPATIBLE("maxim,max11610", max11610),
1557 MAX1363_COMPATIBLE("maxim,max11611", max11611),
1558 MAX1363_COMPATIBLE("maxim,max11612", max11612),
1559 MAX1363_COMPATIBLE("maxim,max11613", max11613),
1560 MAX1363_COMPATIBLE("maxim,max11614", max11614),
1561 MAX1363_COMPATIBLE("maxim,max11615", max11615),
1562 MAX1363_COMPATIBLE("maxim,max11616", max11616),
1563 MAX1363_COMPATIBLE("maxim,max11617", max11617),
1564 MAX1363_COMPATIBLE("maxim,max11644", max11644),
1565 MAX1363_COMPATIBLE("maxim,max11645", max11645),
1566 MAX1363_COMPATIBLE("maxim,max11646", max11646),
1567 MAX1363_COMPATIBLE("maxim,max11647", max11647),
1568 { /* sentinel */ }
1569};
1570#endif
1571
1519static int max1363_probe(struct i2c_client *client, 1572static int max1363_probe(struct i2c_client *client,
1520 const struct i2c_device_id *id) 1573 const struct i2c_device_id *id)
1521{ 1574{
@@ -1523,6 +1576,7 @@ static int max1363_probe(struct i2c_client *client,
1523 struct max1363_state *st; 1576 struct max1363_state *st;
1524 struct iio_dev *indio_dev; 1577 struct iio_dev *indio_dev;
1525 struct regulator *vref; 1578 struct regulator *vref;
1579 const struct of_device_id *match;
1526 1580
1527 indio_dev = devm_iio_device_alloc(&client->dev, 1581 indio_dev = devm_iio_device_alloc(&client->dev,
1528 sizeof(struct max1363_state)); 1582 sizeof(struct max1363_state));
@@ -1549,7 +1603,12 @@ static int max1363_probe(struct i2c_client *client,
1549 /* this is only used for device removal purposes */ 1603 /* this is only used for device removal purposes */
1550 i2c_set_clientdata(client, indio_dev); 1604 i2c_set_clientdata(client, indio_dev);
1551 1605
1552 st->chip_info = &max1363_chip_info_tbl[id->driver_data]; 1606 match = of_match_device(of_match_ptr(max1363_of_match),
1607 &client->dev);
1608 if (match)
1609 st->chip_info = of_device_get_match_data(&client->dev);
1610 else
1611 st->chip_info = &max1363_chip_info_tbl[id->driver_data];
1553 st->client = client; 1612 st->client = client;
1554 1613
1555 st->vref_uv = st->chip_info->int_vref_mv * 1000; 1614 st->vref_uv = st->chip_info->int_vref_mv * 1000;
@@ -1587,6 +1646,7 @@ static int max1363_probe(struct i2c_client *client,
1587 1646
1588 /* Establish that the iio_dev is a child of the i2c device */ 1647 /* Establish that the iio_dev is a child of the i2c device */
1589 indio_dev->dev.parent = &client->dev; 1648 indio_dev->dev.parent = &client->dev;
1649 indio_dev->dev.of_node = client->dev.of_node;
1590 indio_dev->name = id->name; 1650 indio_dev->name = id->name;
1591 indio_dev->channels = st->chip_info->channels; 1651 indio_dev->channels = st->chip_info->channels;
1592 indio_dev->num_channels = st->chip_info->num_channels; 1652 indio_dev->num_channels = st->chip_info->num_channels;
@@ -1692,6 +1752,7 @@ MODULE_DEVICE_TABLE(i2c, max1363_id);
1692static struct i2c_driver max1363_driver = { 1752static struct i2c_driver max1363_driver = {
1693 .driver = { 1753 .driver = {
1694 .name = "max1363", 1754 .name = "max1363",
1755 .of_match_table = of_match_ptr(max1363_of_match),
1695 }, 1756 },
1696 .probe = max1363_probe, 1757 .probe = max1363_probe,
1697 .remove = max1363_remove, 1758 .remove = max1363_remove,
diff --git a/drivers/iio/adc/mcp320x.c b/drivers/iio/adc/mcp320x.c
index a850ca7d1eda..634717ae12f3 100644
--- a/drivers/iio/adc/mcp320x.c
+++ b/drivers/iio/adc/mcp320x.c
@@ -308,6 +308,7 @@ static int mcp320x_probe(struct spi_device *spi)
308 adc->spi = spi; 308 adc->spi = spi;
309 309
310 indio_dev->dev.parent = &spi->dev; 310 indio_dev->dev.parent = &spi->dev;
311 indio_dev->dev.of_node = spi->dev.of_node;
311 indio_dev->name = spi_get_device_id(spi)->name; 312 indio_dev->name = spi_get_device_id(spi)->name;
312 indio_dev->modes = INDIO_DIRECT_MODE; 313 indio_dev->modes = INDIO_DIRECT_MODE;
313 indio_dev->info = &mcp320x_info; 314 indio_dev->info = &mcp320x_info;
diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c
index d1172dc1e8e2..254135e07792 100644
--- a/drivers/iio/adc/mcp3422.c
+++ b/drivers/iio/adc/mcp3422.c
@@ -352,6 +352,7 @@ static int mcp3422_probe(struct i2c_client *client,
352 mutex_init(&adc->lock); 352 mutex_init(&adc->lock);
353 353
354 indio_dev->dev.parent = &client->dev; 354 indio_dev->dev.parent = &client->dev;
355 indio_dev->dev.of_node = client->dev.of_node;
355 indio_dev->name = dev_name(&client->dev); 356 indio_dev->name = dev_name(&client->dev);
356 indio_dev->modes = INDIO_DIRECT_MODE; 357 indio_dev->modes = INDIO_DIRECT_MODE;
357 indio_dev->info = &mcp3422_info; 358 indio_dev->info = &mcp3422_info;
diff --git a/drivers/iio/adc/nau7802.c b/drivers/iio/adc/nau7802.c
index 57365c504093..db9b829ccf0d 100644
--- a/drivers/iio/adc/nau7802.c
+++ b/drivers/iio/adc/nau7802.c
@@ -433,6 +433,7 @@ static int nau7802_probe(struct i2c_client *client,
433 i2c_set_clientdata(client, indio_dev); 433 i2c_set_clientdata(client, indio_dev);
434 434
435 indio_dev->dev.parent = &client->dev; 435 indio_dev->dev.parent = &client->dev;
436 indio_dev->dev.of_node = client->dev.of_node;
436 indio_dev->name = dev_name(&client->dev); 437 indio_dev->name = dev_name(&client->dev);
437 indio_dev->modes = INDIO_DIRECT_MODE; 438 indio_dev->modes = INDIO_DIRECT_MODE;
438 indio_dev->info = &nau7802_info; 439 indio_dev->info = &nau7802_info;
diff --git a/drivers/iio/adc/ti-adc081c.c b/drivers/iio/adc/ti-adc081c.c
index f8807adc08c4..319172cf7da8 100644
--- a/drivers/iio/adc/ti-adc081c.c
+++ b/drivers/iio/adc/ti-adc081c.c
@@ -139,7 +139,8 @@ static irqreturn_t adc081c_trigger_handler(int irq, void *p)
139 if (ret < 0) 139 if (ret < 0)
140 goto out; 140 goto out;
141 buf[0] = ret; 141 buf[0] = ret;
142 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); 142 iio_push_to_buffers_with_timestamp(indio_dev, buf,
143 iio_get_time_ns(indio_dev));
143out: 144out:
144 iio_trigger_notify_done(indio_dev->trig); 145 iio_trigger_notify_done(indio_dev->trig);
145 return IRQ_HANDLED; 146 return IRQ_HANDLED;
@@ -185,6 +186,7 @@ static int adc081c_probe(struct i2c_client *client,
185 return err; 186 return err;
186 187
187 iio->dev.parent = &client->dev; 188 iio->dev.parent = &client->dev;
189 iio->dev.of_node = client->dev.of_node;
188 iio->name = dev_name(&client->dev); 190 iio->name = dev_name(&client->dev);
189 iio->modes = INDIO_DIRECT_MODE; 191 iio->modes = INDIO_DIRECT_MODE;
190 iio->info = &adc081c_info; 192 iio->info = &adc081c_info;
diff --git a/drivers/iio/adc/ti-adc0832.c b/drivers/iio/adc/ti-adc0832.c
index 0afeac0c9bad..f4ba23effe9a 100644
--- a/drivers/iio/adc/ti-adc0832.c
+++ b/drivers/iio/adc/ti-adc0832.c
@@ -194,6 +194,7 @@ static int adc0832_probe(struct spi_device *spi)
194 194
195 indio_dev->name = spi_get_device_id(spi)->name; 195 indio_dev->name = spi_get_device_id(spi)->name;
196 indio_dev->dev.parent = &spi->dev; 196 indio_dev->dev.parent = &spi->dev;
197 indio_dev->dev.of_node = spi->dev.of_node;
197 indio_dev->info = &adc0832_info; 198 indio_dev->info = &adc0832_info;
198 indio_dev->modes = INDIO_DIRECT_MODE; 199 indio_dev->modes = INDIO_DIRECT_MODE;
199 200
diff --git a/drivers/iio/adc/ti-adc128s052.c b/drivers/iio/adc/ti-adc128s052.c
index bc58867d6e8d..89dfbd31be5c 100644
--- a/drivers/iio/adc/ti-adc128s052.c
+++ b/drivers/iio/adc/ti-adc128s052.c
@@ -150,6 +150,7 @@ static int adc128_probe(struct spi_device *spi)
150 spi_set_drvdata(spi, indio_dev); 150 spi_set_drvdata(spi, indio_dev);
151 151
152 indio_dev->dev.parent = &spi->dev; 152 indio_dev->dev.parent = &spi->dev;
153 indio_dev->dev.of_node = spi->dev.of_node;
153 indio_dev->name = spi_get_device_id(spi)->name; 154 indio_dev->name = spi_get_device_id(spi)->name;
154 indio_dev->modes = INDIO_DIRECT_MODE; 155 indio_dev->modes = INDIO_DIRECT_MODE;
155 indio_dev->info = &adc128_info; 156 indio_dev->info = &adc128_info;
diff --git a/drivers/iio/adc/ti-ads1015.c b/drivers/iio/adc/ti-ads1015.c
index 8be192a84893..1ef398770a1f 100644
--- a/drivers/iio/adc/ti-ads1015.c
+++ b/drivers/iio/adc/ti-ads1015.c
@@ -288,7 +288,8 @@ static irqreturn_t ads1015_trigger_handler(int irq, void *p)
288 buf[0] = res; 288 buf[0] = res;
289 mutex_unlock(&data->lock); 289 mutex_unlock(&data->lock);
290 290
291 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); 291 iio_push_to_buffers_with_timestamp(indio_dev, buf,
292 iio_get_time_ns(indio_dev));
292 293
293err: 294err:
294 iio_trigger_notify_done(indio_dev->trig); 295 iio_trigger_notify_done(indio_dev->trig);
@@ -585,6 +586,7 @@ static int ads1015_probe(struct i2c_client *client,
585 mutex_init(&data->lock); 586 mutex_init(&data->lock);
586 587
587 indio_dev->dev.parent = &client->dev; 588 indio_dev->dev.parent = &client->dev;
589 indio_dev->dev.of_node = client->dev.of_node;
588 indio_dev->name = ADS1015_DRV_NAME; 590 indio_dev->name = ADS1015_DRV_NAME;
589 indio_dev->modes = INDIO_DIRECT_MODE; 591 indio_dev->modes = INDIO_DIRECT_MODE;
590 592
diff --git a/drivers/iio/adc/ti-ads8688.c b/drivers/iio/adc/ti-ads8688.c
index 03e907028cb6..c400439900af 100644
--- a/drivers/iio/adc/ti-ads8688.c
+++ b/drivers/iio/adc/ti-ads8688.c
@@ -421,6 +421,7 @@ static int ads8688_probe(struct spi_device *spi)
421 421
422 indio_dev->name = spi_get_device_id(spi)->name; 422 indio_dev->name = spi_get_device_id(spi)->name;
423 indio_dev->dev.parent = &spi->dev; 423 indio_dev->dev.parent = &spi->dev;
424 indio_dev->dev.of_node = spi->dev.of_node;
424 indio_dev->modes = INDIO_DIRECT_MODE; 425 indio_dev->modes = INDIO_DIRECT_MODE;
425 indio_dev->channels = st->chip_info->channels; 426 indio_dev->channels = st->chip_info->channels;
426 indio_dev->num_channels = st->chip_info->num_channels; 427 indio_dev->num_channels = st->chip_info->num_channels;
diff --git a/drivers/iio/adc/vf610_adc.c b/drivers/iio/adc/vf610_adc.c
index 653bf1379d2e..228a003adeed 100644
--- a/drivers/iio/adc/vf610_adc.c
+++ b/drivers/iio/adc/vf610_adc.c
@@ -594,7 +594,8 @@ static irqreturn_t vf610_adc_isr(int irq, void *dev_id)
594 if (iio_buffer_enabled(indio_dev)) { 594 if (iio_buffer_enabled(indio_dev)) {
595 info->buffer[0] = info->value; 595 info->buffer[0] = info->value;
596 iio_push_to_buffers_with_timestamp(indio_dev, 596 iio_push_to_buffers_with_timestamp(indio_dev,
597 info->buffer, iio_get_time_ns()); 597 info->buffer,
598 iio_get_time_ns(indio_dev));
598 iio_trigger_notify_done(indio_dev->trig); 599 iio_trigger_notify_done(indio_dev->trig);
599 } else 600 } else
600 complete(&info->completion); 601 complete(&info->completion);
diff --git a/drivers/iio/adc/xilinx-xadc-events.c b/drivers/iio/adc/xilinx-xadc-events.c
index edcf3aabd70d..6d5c2a6f4e6e 100644
--- a/drivers/iio/adc/xilinx-xadc-events.c
+++ b/drivers/iio/adc/xilinx-xadc-events.c
@@ -46,7 +46,7 @@ static void xadc_handle_event(struct iio_dev *indio_dev, unsigned int event)
46 iio_push_event(indio_dev, 46 iio_push_event(indio_dev,
47 IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, 47 IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
48 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 48 IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
49 iio_get_time_ns()); 49 iio_get_time_ns(indio_dev));
50 } else { 50 } else {
51 /* 51 /*
52 * For other channels we don't know whether it is a upper or 52 * For other channels we don't know whether it is a upper or
@@ -56,7 +56,7 @@ static void xadc_handle_event(struct iio_dev *indio_dev, unsigned int event)
56 iio_push_event(indio_dev, 56 iio_push_event(indio_dev,
57 IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, 57 IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
58 IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER), 58 IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER),
59 iio_get_time_ns()); 59 iio_get_time_ns(indio_dev));
60 } 60 }
61} 61}
62 62
diff --git a/drivers/iio/chemical/atlas-ph-sensor.c b/drivers/iio/chemical/atlas-ph-sensor.c
index 02e85db5d31f..ae038a59d256 100644
--- a/drivers/iio/chemical/atlas-ph-sensor.c
+++ b/drivers/iio/chemical/atlas-ph-sensor.c
@@ -343,7 +343,7 @@ static irqreturn_t atlas_trigger_handler(int irq, void *private)
343 343
344 if (!ret) 344 if (!ret)
345 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 345 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
346 iio_get_time_ns()); 346 iio_get_time_ns(indio_dev));
347 347
348 iio_trigger_notify_done(indio_dev->trig); 348 iio_trigger_notify_done(indio_dev->trig);
349 349
diff --git a/drivers/iio/common/st_sensors/st_sensors_buffer.c b/drivers/iio/common/st_sensors/st_sensors_buffer.c
index 50afc0f8f16b..d06e728cea37 100644
--- a/drivers/iio/common/st_sensors/st_sensors_buffer.c
+++ b/drivers/iio/common/st_sensors/st_sensors_buffer.c
@@ -24,30 +24,29 @@
24 24
25static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf) 25static int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf)
26{ 26{
27 int i, len; 27 int i;
28 int total = 0;
29 struct st_sensor_data *sdata = iio_priv(indio_dev); 28 struct st_sensor_data *sdata = iio_priv(indio_dev);
30 unsigned int num_data_channels = sdata->num_data_channels; 29 unsigned int num_data_channels = sdata->num_data_channels;
31 30
32 for (i = 0; i < num_data_channels; i++) { 31 for_each_set_bit(i, indio_dev->active_scan_mask, num_data_channels) {
33 unsigned int bytes_to_read; 32 const struct iio_chan_spec *channel = &indio_dev->channels[i];
34 33 unsigned int bytes_to_read = channel->scan_type.realbits >> 3;
35 if (test_bit(i, indio_dev->active_scan_mask)) { 34 unsigned int storage_bytes =
36 bytes_to_read = indio_dev->channels[i].scan_type.storagebits >> 3; 35 channel->scan_type.storagebits >> 3;
37 len = sdata->tf->read_multiple_byte(&sdata->tb, 36
38 sdata->dev, indio_dev->channels[i].address, 37 buf = PTR_ALIGN(buf, storage_bytes);
39 bytes_to_read, 38 if (sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev,
40 buf + total, sdata->multiread_bit); 39 channel->address,
41 40 bytes_to_read, buf,
42 if (len < bytes_to_read) 41 sdata->multiread_bit) <
43 return -EIO; 42 bytes_to_read)
44 43 return -EIO;
45 /* Advance the buffer pointer */ 44
46 total += len; 45 /* Advance the buffer pointer */
47 } 46 buf += storage_bytes;
48 } 47 }
49 48
50 return total; 49 return 0;
51} 50}
52 51
53irqreturn_t st_sensors_trigger_handler(int irq, void *p) 52irqreturn_t st_sensors_trigger_handler(int irq, void *p)
@@ -58,11 +57,16 @@ irqreturn_t st_sensors_trigger_handler(int irq, void *p)
58 struct st_sensor_data *sdata = iio_priv(indio_dev); 57 struct st_sensor_data *sdata = iio_priv(indio_dev);
59 s64 timestamp; 58 s64 timestamp;
60 59
61 /* If we do timetamping here, do it before reading the values */ 60 /*
61 * If we do timetamping here, do it before reading the values, because
62 * once we've read the values, new interrupts can occur (when using
63 * the hardware trigger) and the hw_timestamp may get updated.
64 * By storing it in a local variable first, we are safe.
65 */
62 if (sdata->hw_irq_trigger) 66 if (sdata->hw_irq_trigger)
63 timestamp = sdata->hw_timestamp; 67 timestamp = sdata->hw_timestamp;
64 else 68 else
65 timestamp = iio_get_time_ns(); 69 timestamp = iio_get_time_ns(indio_dev);
66 70
67 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data); 71 len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data);
68 if (len < 0) 72 if (len < 0)
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index 6db12ea034d3..2d5282e05482 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -490,7 +490,7 @@ static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
490 int err; 490 int err;
491 u8 *outdata; 491 u8 *outdata;
492 struct st_sensor_data *sdata = iio_priv(indio_dev); 492 struct st_sensor_data *sdata = iio_priv(indio_dev);
493 unsigned int byte_for_channel = ch->scan_type.storagebits >> 3; 493 unsigned int byte_for_channel = ch->scan_type.realbits >> 3;
494 494
495 outdata = kmalloc(byte_for_channel, GFP_KERNEL); 495 outdata = kmalloc(byte_for_channel, GFP_KERNEL);
496 if (!outdata) 496 if (!outdata)
@@ -550,7 +550,7 @@ int st_sensors_check_device_support(struct iio_dev *indio_dev,
550 int num_sensors_list, 550 int num_sensors_list,
551 const struct st_sensor_settings *sensor_settings) 551 const struct st_sensor_settings *sensor_settings)
552{ 552{
553 int i, n, err; 553 int i, n, err = 0;
554 u8 wai; 554 u8 wai;
555 struct st_sensor_data *sdata = iio_priv(indio_dev); 555 struct st_sensor_data *sdata = iio_priv(indio_dev);
556 556
@@ -570,17 +570,21 @@ int st_sensors_check_device_support(struct iio_dev *indio_dev,
570 return -ENODEV; 570 return -ENODEV;
571 } 571 }
572 572
573 err = sdata->tf->read_byte(&sdata->tb, sdata->dev, 573 if (sensor_settings[i].wai_addr) {
574 sensor_settings[i].wai_addr, &wai); 574 err = sdata->tf->read_byte(&sdata->tb, sdata->dev,
575 if (err < 0) { 575 sensor_settings[i].wai_addr, &wai);
576 dev_err(&indio_dev->dev, "failed to read Who-Am-I register.\n"); 576 if (err < 0) {
577 return err; 577 dev_err(&indio_dev->dev,
578 } 578 "failed to read Who-Am-I register.\n");
579 return err;
580 }
579 581
580 if (sensor_settings[i].wai != wai) { 582 if (sensor_settings[i].wai != wai) {
581 dev_err(&indio_dev->dev, "%s: WhoAmI mismatch (0x%x).\n", 583 dev_err(&indio_dev->dev,
582 indio_dev->name, wai); 584 "%s: WhoAmI mismatch (0x%x).\n",
583 return -EINVAL; 585 indio_dev->name, wai);
586 return -EINVAL;
587 }
584 } 588 }
585 589
586 sdata->sensor_settings = 590 sdata->sensor_settings =
diff --git a/drivers/iio/common/st_sensors/st_sensors_trigger.c b/drivers/iio/common/st_sensors/st_sensors_trigger.c
index 296e4ff19ae8..e66f12ee8a55 100644
--- a/drivers/iio/common/st_sensors/st_sensors_trigger.c
+++ b/drivers/iio/common/st_sensors/st_sensors_trigger.c
@@ -18,6 +18,50 @@
18#include "st_sensors_core.h" 18#include "st_sensors_core.h"
19 19
20/** 20/**
21 * st_sensors_new_samples_available() - check if more samples came in
22 * returns:
23 * 0 - no new samples available
24 * 1 - new samples available
25 * negative - error or unknown
26 */
27static int st_sensors_new_samples_available(struct iio_dev *indio_dev,
28 struct st_sensor_data *sdata)
29{
30 u8 status;
31 int ret;
32
33 /* How would I know if I can't check it? */
34 if (!sdata->sensor_settings->drdy_irq.addr_stat_drdy)
35 return -EINVAL;
36
37 /* No scan mask, no interrupt */
38 if (!indio_dev->active_scan_mask)
39 return 0;
40
41 ret = sdata->tf->read_byte(&sdata->tb, sdata->dev,
42 sdata->sensor_settings->drdy_irq.addr_stat_drdy,
43 &status);
44 if (ret < 0) {
45 dev_err(sdata->dev,
46 "error checking samples available\n");
47 return ret;
48 }
49 /*
50 * the lower bits of .active_scan_mask[0] is directly mapped
51 * to the channels on the sensor: either bit 0 for
52 * one-dimensional sensors, or e.g. x,y,z for accelerometers,
53 * gyroscopes or magnetometers. No sensor use more than 3
54 * channels, so cut the other status bits here.
55 */
56 status &= 0x07;
57
58 if (status & (u8)indio_dev->active_scan_mask[0])
59 return 1;
60
61 return 0;
62}
63
64/**
21 * st_sensors_irq_handler() - top half of the IRQ-based triggers 65 * st_sensors_irq_handler() - top half of the IRQ-based triggers
22 * @irq: irq number 66 * @irq: irq number
23 * @p: private handler data 67 * @p: private handler data
@@ -29,7 +73,7 @@ irqreturn_t st_sensors_irq_handler(int irq, void *p)
29 struct st_sensor_data *sdata = iio_priv(indio_dev); 73 struct st_sensor_data *sdata = iio_priv(indio_dev);
30 74
31 /* Get the time stamp as close in time as possible */ 75 /* Get the time stamp as close in time as possible */
32 sdata->hw_timestamp = iio_get_time_ns(); 76 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
33 return IRQ_WAKE_THREAD; 77 return IRQ_WAKE_THREAD;
34} 78}
35 79
@@ -43,44 +87,43 @@ irqreturn_t st_sensors_irq_thread(int irq, void *p)
43 struct iio_trigger *trig = p; 87 struct iio_trigger *trig = p;
44 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 88 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
45 struct st_sensor_data *sdata = iio_priv(indio_dev); 89 struct st_sensor_data *sdata = iio_priv(indio_dev);
46 int ret;
47 90
48 /* 91 /*
49 * If this trigger is backed by a hardware interrupt and we have a 92 * If this trigger is backed by a hardware interrupt and we have a
50 * status register, check if this IRQ came from us 93 * status register, check if this IRQ came from us. Notice that
94 * we will process also if st_sensors_new_samples_available()
95 * returns negative: if we can't check status, then poll
96 * unconditionally.
51 */ 97 */
52 if (sdata->sensor_settings->drdy_irq.addr_stat_drdy) { 98 if (sdata->hw_irq_trigger &&
53 u8 status; 99 st_sensors_new_samples_available(indio_dev, sdata)) {
54 100 iio_trigger_poll_chained(p);
55 ret = sdata->tf->read_byte(&sdata->tb, sdata->dev, 101 } else {
56 sdata->sensor_settings->drdy_irq.addr_stat_drdy, 102 dev_dbg(sdata->dev, "spurious IRQ\n");
57 &status); 103 return IRQ_NONE;
58 if (ret < 0) { 104 }
59 dev_err(sdata->dev, "could not read channel status\n");
60 goto out_poll;
61 }
62 /*
63 * the lower bits of .active_scan_mask[0] is directly mapped
64 * to the channels on the sensor: either bit 0 for
65 * one-dimensional sensors, or e.g. x,y,z for accelerometers,
66 * gyroscopes or magnetometers. No sensor use more than 3
67 * channels, so cut the other status bits here.
68 */
69 status &= 0x07;
70 105
71 /* 106 /*
72 * If this was not caused by any channels on this sensor, 107 * If we have proper level IRQs the handler will be re-entered if
73 * return IRQ_NONE 108 * the line is still active, so return here and come back in through
74 */ 109 * the top half if need be.
75 if (!indio_dev->active_scan_mask) 110 */
76 return IRQ_NONE; 111 if (!sdata->edge_irq)
77 if (!(status & (u8)indio_dev->active_scan_mask[0])) 112 return IRQ_HANDLED;
78 return IRQ_NONE; 113
114 /*
115 * If we are using egde IRQs, new samples arrived while processing
116 * the IRQ and those may be missed unless we pick them here, so poll
117 * again. If the sensor delivery frequency is very high, this thread
118 * turns into a polled loop handler.
119 */
120 while (sdata->hw_irq_trigger &&
121 st_sensors_new_samples_available(indio_dev, sdata)) {
122 dev_dbg(sdata->dev, "more samples came in during polling\n");
123 sdata->hw_timestamp = iio_get_time_ns(indio_dev);
124 iio_trigger_poll_chained(p);
79 } 125 }
80 126
81out_poll:
82 /* It's our IRQ: proceed to handle the register polling */
83 iio_trigger_poll_chained(p);
84 return IRQ_HANDLED; 127 return IRQ_HANDLED;
85} 128}
86 129
@@ -107,13 +150,18 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
107 * If the IRQ is triggered on falling edge, we need to mark the 150 * If the IRQ is triggered on falling edge, we need to mark the
108 * interrupt as active low, if the hardware supports this. 151 * interrupt as active low, if the hardware supports this.
109 */ 152 */
110 if (irq_trig == IRQF_TRIGGER_FALLING) { 153 switch(irq_trig) {
154 case IRQF_TRIGGER_FALLING:
155 case IRQF_TRIGGER_LOW:
111 if (!sdata->sensor_settings->drdy_irq.addr_ihl) { 156 if (!sdata->sensor_settings->drdy_irq.addr_ihl) {
112 dev_err(&indio_dev->dev, 157 dev_err(&indio_dev->dev,
113 "falling edge specified for IRQ but hardware " 158 "falling/low specified for IRQ "
114 "only support rising edge, will request " 159 "but hardware only support rising/high: "
115 "rising edge\n"); 160 "will request rising/high\n");
116 irq_trig = IRQF_TRIGGER_RISING; 161 if (irq_trig == IRQF_TRIGGER_FALLING)
162 irq_trig = IRQF_TRIGGER_RISING;
163 if (irq_trig == IRQF_TRIGGER_LOW)
164 irq_trig = IRQF_TRIGGER_HIGH;
117 } else { 165 } else {
118 /* Set up INT active low i.e. falling edge */ 166 /* Set up INT active low i.e. falling edge */
119 err = st_sensors_write_data_with_mask(indio_dev, 167 err = st_sensors_write_data_with_mask(indio_dev,
@@ -122,20 +170,39 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
122 if (err < 0) 170 if (err < 0)
123 goto iio_trigger_free; 171 goto iio_trigger_free;
124 dev_info(&indio_dev->dev, 172 dev_info(&indio_dev->dev,
125 "interrupts on the falling edge\n"); 173 "interrupts on the falling edge or "
174 "active low level\n");
126 } 175 }
127 } else if (irq_trig == IRQF_TRIGGER_RISING) { 176 break;
177 case IRQF_TRIGGER_RISING:
128 dev_info(&indio_dev->dev, 178 dev_info(&indio_dev->dev,
129 "interrupts on the rising edge\n"); 179 "interrupts on the rising edge\n");
130 180 break;
131 } else { 181 case IRQF_TRIGGER_HIGH:
182 dev_info(&indio_dev->dev,
183 "interrupts active high level\n");
184 break;
185 default:
186 /* This is the most preferred mode, if possible */
132 dev_err(&indio_dev->dev, 187 dev_err(&indio_dev->dev,
133 "unsupported IRQ trigger specified (%lx), only " 188 "unsupported IRQ trigger specified (%lx), enforce "
134 "rising and falling edges supported, enforce "
135 "rising edge\n", irq_trig); 189 "rising edge\n", irq_trig);
136 irq_trig = IRQF_TRIGGER_RISING; 190 irq_trig = IRQF_TRIGGER_RISING;
137 } 191 }
138 192
193 /* Tell the interrupt handler that we're dealing with edges */
194 if (irq_trig == IRQF_TRIGGER_FALLING ||
195 irq_trig == IRQF_TRIGGER_RISING)
196 sdata->edge_irq = true;
197 else
198 /*
199 * If we're not using edges (i.e. level interrupts) we
200 * just mask off the IRQ, handle one interrupt, then
201 * if the line is still low, we return to the
202 * interrupt handler top half again and start over.
203 */
204 irq_trig |= IRQF_ONESHOT;
205
139 /* 206 /*
140 * If the interrupt pin is Open Drain, by definition this 207 * If the interrupt pin is Open Drain, by definition this
141 * means that the interrupt line may be shared with other 208 * means that the interrupt line may be shared with other
@@ -148,9 +215,6 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
148 sdata->sensor_settings->drdy_irq.addr_stat_drdy) 215 sdata->sensor_settings->drdy_irq.addr_stat_drdy)
149 irq_trig |= IRQF_SHARED; 216 irq_trig |= IRQF_SHARED;
150 217
151 /* Let's create an interrupt thread masking the hard IRQ here */
152 irq_trig |= IRQF_ONESHOT;
153
154 err = request_threaded_irq(sdata->get_irq_data_ready(indio_dev), 218 err = request_threaded_irq(sdata->get_irq_data_ready(indio_dev),
155 st_sensors_irq_handler, 219 st_sensors_irq_handler,
156 st_sensors_irq_thread, 220 st_sensors_irq_thread,
diff --git a/drivers/iio/dac/ad5421.c b/drivers/iio/dac/ad5421.c
index 968712be967f..559061ab1982 100644
--- a/drivers/iio/dac/ad5421.c
+++ b/drivers/iio/dac/ad5421.c
@@ -242,7 +242,7 @@ static irqreturn_t ad5421_fault_handler(int irq, void *data)
242 0, 242 0,
243 IIO_EV_TYPE_THRESH, 243 IIO_EV_TYPE_THRESH,
244 IIO_EV_DIR_RISING), 244 IIO_EV_DIR_RISING),
245 iio_get_time_ns()); 245 iio_get_time_ns(indio_dev));
246 } 246 }
247 247
248 if (events & AD5421_FAULT_UNDER_CURRENT) { 248 if (events & AD5421_FAULT_UNDER_CURRENT) {
@@ -251,7 +251,7 @@ static irqreturn_t ad5421_fault_handler(int irq, void *data)
251 0, 251 0,
252 IIO_EV_TYPE_THRESH, 252 IIO_EV_TYPE_THRESH,
253 IIO_EV_DIR_FALLING), 253 IIO_EV_DIR_FALLING),
254 iio_get_time_ns()); 254 iio_get_time_ns(indio_dev));
255 } 255 }
256 256
257 if (events & AD5421_FAULT_TEMP_OVER_140) { 257 if (events & AD5421_FAULT_TEMP_OVER_140) {
@@ -260,7 +260,7 @@ static irqreturn_t ad5421_fault_handler(int irq, void *data)
260 0, 260 0,
261 IIO_EV_TYPE_MAG, 261 IIO_EV_TYPE_MAG,
262 IIO_EV_DIR_RISING), 262 IIO_EV_DIR_RISING),
263 iio_get_time_ns()); 263 iio_get_time_ns(indio_dev));
264 } 264 }
265 265
266 old_fault = fault; 266 old_fault = fault;
diff --git a/drivers/iio/dac/ad5504.c b/drivers/iio/dac/ad5504.c
index 4e4c20d6d8b5..788b3d6fd1cc 100644
--- a/drivers/iio/dac/ad5504.c
+++ b/drivers/iio/dac/ad5504.c
@@ -223,7 +223,7 @@ static irqreturn_t ad5504_event_handler(int irq, void *private)
223 0, 223 0,
224 IIO_EV_TYPE_THRESH, 224 IIO_EV_TYPE_THRESH,
225 IIO_EV_DIR_RISING), 225 IIO_EV_DIR_RISING),
226 iio_get_time_ns()); 226 iio_get_time_ns((struct iio_dev *)private));
227 227
228 return IRQ_HANDLED; 228 return IRQ_HANDLED;
229} 229}
diff --git a/drivers/iio/dummy/iio_simple_dummy_buffer.c b/drivers/iio/dummy/iio_simple_dummy_buffer.c
index cf44a6f79431..b383892a5193 100644
--- a/drivers/iio/dummy/iio_simple_dummy_buffer.c
+++ b/drivers/iio/dummy/iio_simple_dummy_buffer.c
@@ -85,7 +85,8 @@ static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
85 } 85 }
86 } 86 }
87 87
88 iio_push_to_buffers_with_timestamp(indio_dev, data, iio_get_time_ns()); 88 iio_push_to_buffers_with_timestamp(indio_dev, data,
89 iio_get_time_ns(indio_dev));
89 90
90 kfree(data); 91 kfree(data);
91 92
diff --git a/drivers/iio/dummy/iio_simple_dummy_events.c b/drivers/iio/dummy/iio_simple_dummy_events.c
index 6eb600ff7056..ed63ffd849f8 100644
--- a/drivers/iio/dummy/iio_simple_dummy_events.c
+++ b/drivers/iio/dummy/iio_simple_dummy_events.c
@@ -158,7 +158,7 @@ static irqreturn_t iio_simple_dummy_get_timestamp(int irq, void *private)
158 struct iio_dev *indio_dev = private; 158 struct iio_dev *indio_dev = private;
159 struct iio_dummy_state *st = iio_priv(indio_dev); 159 struct iio_dummy_state *st = iio_priv(indio_dev);
160 160
161 st->event_timestamp = iio_get_time_ns(); 161 st->event_timestamp = iio_get_time_ns(indio_dev);
162 return IRQ_WAKE_THREAD; 162 return IRQ_WAKE_THREAD;
163} 163}
164 164
diff --git a/drivers/iio/gyro/bmg160_core.c b/drivers/iio/gyro/bmg160_core.c
index 7ccc044063f6..f7fcfa886f72 100644
--- a/drivers/iio/gyro/bmg160_core.c
+++ b/drivers/iio/gyro/bmg160_core.c
@@ -50,6 +50,7 @@
50#define BMG160_REG_PMU_BW 0x10 50#define BMG160_REG_PMU_BW 0x10
51#define BMG160_NO_FILTER 0 51#define BMG160_NO_FILTER 0
52#define BMG160_DEF_BW 100 52#define BMG160_DEF_BW 100
53#define BMG160_REG_PMU_BW_RES BIT(7)
53 54
54#define BMG160_REG_INT_MAP_0 0x17 55#define BMG160_REG_INT_MAP_0 0x17
55#define BMG160_INT_MAP_0_BIT_ANY BIT(1) 56#define BMG160_INT_MAP_0_BIT_ANY BIT(1)
@@ -100,7 +101,6 @@ struct bmg160_data {
100 struct iio_trigger *motion_trig; 101 struct iio_trigger *motion_trig;
101 struct mutex mutex; 102 struct mutex mutex;
102 s16 buffer[8]; 103 s16 buffer[8];
103 u8 bw_bits;
104 u32 dps_range; 104 u32 dps_range;
105 int ev_enable_state; 105 int ev_enable_state;
106 int slope_thres; 106 int slope_thres;
@@ -117,13 +117,16 @@ enum bmg160_axis {
117}; 117};
118 118
119static const struct { 119static const struct {
120 int val; 120 int odr;
121 int filter;
121 int bw_bits; 122 int bw_bits;
122} bmg160_samp_freq_table[] = { {100, 0x07}, 123} bmg160_samp_freq_table[] = { {100, 32, 0x07},
123 {200, 0x06}, 124 {200, 64, 0x06},
124 {400, 0x03}, 125 {100, 12, 0x05},
125 {1000, 0x02}, 126 {200, 23, 0x04},
126 {2000, 0x01} }; 127 {400, 47, 0x03},
128 {1000, 116, 0x02},
129 {2000, 230, 0x01} };
127 130
128static const struct { 131static const struct {
129 int scale; 132 int scale;
@@ -153,7 +156,7 @@ static int bmg160_convert_freq_to_bit(int val)
153 int i; 156 int i;
154 157
155 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) { 158 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
156 if (bmg160_samp_freq_table[i].val == val) 159 if (bmg160_samp_freq_table[i].odr == val)
157 return bmg160_samp_freq_table[i].bw_bits; 160 return bmg160_samp_freq_table[i].bw_bits;
158 } 161 }
159 162
@@ -176,7 +179,53 @@ static int bmg160_set_bw(struct bmg160_data *data, int val)
176 return ret; 179 return ret;
177 } 180 }
178 181
179 data->bw_bits = bw_bits; 182 return 0;
183}
184
185static int bmg160_get_filter(struct bmg160_data *data, int *val)
186{
187 struct device *dev = regmap_get_device(data->regmap);
188 int ret;
189 int i;
190 unsigned int bw_bits;
191
192 ret = regmap_read(data->regmap, BMG160_REG_PMU_BW, &bw_bits);
193 if (ret < 0) {
194 dev_err(dev, "Error reading reg_pmu_bw\n");
195 return ret;
196 }
197
198 /* Ignore the readonly reserved bit. */
199 bw_bits &= ~BMG160_REG_PMU_BW_RES;
200
201 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
202 if (bmg160_samp_freq_table[i].bw_bits == bw_bits)
203 break;
204 }
205
206 *val = bmg160_samp_freq_table[i].filter;
207
208 return ret ? ret : IIO_VAL_INT;
209}
210
211
212static int bmg160_set_filter(struct bmg160_data *data, int val)
213{
214 struct device *dev = regmap_get_device(data->regmap);
215 int ret;
216 int i;
217
218 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
219 if (bmg160_samp_freq_table[i].filter == val)
220 break;
221 }
222
223 ret = regmap_write(data->regmap, BMG160_REG_PMU_BW,
224 bmg160_samp_freq_table[i].bw_bits);
225 if (ret < 0) {
226 dev_err(dev, "Error writing reg_pmu_bw\n");
227 return ret;
228 }
180 229
181 return 0; 230 return 0;
182} 231}
@@ -386,11 +435,23 @@ static int bmg160_setup_new_data_interrupt(struct bmg160_data *data,
386 435
387static int bmg160_get_bw(struct bmg160_data *data, int *val) 436static int bmg160_get_bw(struct bmg160_data *data, int *val)
388{ 437{
438 struct device *dev = regmap_get_device(data->regmap);
389 int i; 439 int i;
440 unsigned int bw_bits;
441 int ret;
442
443 ret = regmap_read(data->regmap, BMG160_REG_PMU_BW, &bw_bits);
444 if (ret < 0) {
445 dev_err(dev, "Error reading reg_pmu_bw\n");
446 return ret;
447 }
448
449 /* Ignore the readonly reserved bit. */
450 bw_bits &= ~BMG160_REG_PMU_BW_RES;
390 451
391 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) { 452 for (i = 0; i < ARRAY_SIZE(bmg160_samp_freq_table); ++i) {
392 if (bmg160_samp_freq_table[i].bw_bits == data->bw_bits) { 453 if (bmg160_samp_freq_table[i].bw_bits == bw_bits) {
393 *val = bmg160_samp_freq_table[i].val; 454 *val = bmg160_samp_freq_table[i].odr;
394 return IIO_VAL_INT; 455 return IIO_VAL_INT;
395 } 456 }
396 } 457 }
@@ -507,6 +568,8 @@ static int bmg160_read_raw(struct iio_dev *indio_dev,
507 return IIO_VAL_INT; 568 return IIO_VAL_INT;
508 } else 569 } else
509 return -EINVAL; 570 return -EINVAL;
571 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
572 return bmg160_get_filter(data, val);
510 case IIO_CHAN_INFO_SCALE: 573 case IIO_CHAN_INFO_SCALE:
511 *val = 0; 574 *val = 0;
512 switch (chan->type) { 575 switch (chan->type) {
@@ -571,6 +634,26 @@ static int bmg160_write_raw(struct iio_dev *indio_dev,
571 ret = bmg160_set_power_state(data, false); 634 ret = bmg160_set_power_state(data, false);
572 mutex_unlock(&data->mutex); 635 mutex_unlock(&data->mutex);
573 return ret; 636 return ret;
637 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
638 if (val2)
639 return -EINVAL;
640
641 mutex_lock(&data->mutex);
642 ret = bmg160_set_power_state(data, true);
643 if (ret < 0) {
644 bmg160_set_power_state(data, false);
645 mutex_unlock(&data->mutex);
646 return ret;
647 }
648 ret = bmg160_set_filter(data, val);
649 if (ret < 0) {
650 bmg160_set_power_state(data, false);
651 mutex_unlock(&data->mutex);
652 return ret;
653 }
654 ret = bmg160_set_power_state(data, false);
655 mutex_unlock(&data->mutex);
656 return ret;
574 case IIO_CHAN_INFO_SCALE: 657 case IIO_CHAN_INFO_SCALE:
575 if (val) 658 if (val)
576 return -EINVAL; 659 return -EINVAL;
@@ -728,7 +811,8 @@ static const struct iio_event_spec bmg160_event = {
728 .channel2 = IIO_MOD_##_axis, \ 811 .channel2 = IIO_MOD_##_axis, \
729 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 812 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
730 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 813 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
731 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 814 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
815 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
732 .scan_index = AXIS_##_axis, \ 816 .scan_index = AXIS_##_axis, \
733 .scan_type = { \ 817 .scan_type = { \
734 .sign = 's', \ 818 .sign = 's', \
@@ -885,25 +969,25 @@ static irqreturn_t bmg160_event_handler(int irq, void *private)
885 969
886 if (val & BMG160_ANY_MOTION_BIT_X) 970 if (val & BMG160_ANY_MOTION_BIT_X)
887 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL, 971 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
888 0, 972 0,
889 IIO_MOD_X, 973 IIO_MOD_X,
890 IIO_EV_TYPE_ROC, 974 IIO_EV_TYPE_ROC,
891 dir), 975 dir),
892 iio_get_time_ns()); 976 iio_get_time_ns(indio_dev));
893 if (val & BMG160_ANY_MOTION_BIT_Y) 977 if (val & BMG160_ANY_MOTION_BIT_Y)
894 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL, 978 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
895 0, 979 0,
896 IIO_MOD_Y, 980 IIO_MOD_Y,
897 IIO_EV_TYPE_ROC, 981 IIO_EV_TYPE_ROC,
898 dir), 982 dir),
899 iio_get_time_ns()); 983 iio_get_time_ns(indio_dev));
900 if (val & BMG160_ANY_MOTION_BIT_Z) 984 if (val & BMG160_ANY_MOTION_BIT_Z)
901 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL, 985 iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ANGL_VEL,
902 0, 986 0,
903 IIO_MOD_Z, 987 IIO_MOD_Z,
904 IIO_EV_TYPE_ROC, 988 IIO_EV_TYPE_ROC,
905 dir), 989 dir),
906 iio_get_time_ns()); 990 iio_get_time_ns(indio_dev));
907 991
908ack_intr_status: 992ack_intr_status:
909 if (!data->dready_trigger_on) { 993 if (!data->dready_trigger_on) {
diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
index 359883525ab7..4c45488e3a7f 100644
--- a/drivers/iio/iio_core.h
+++ b/drivers/iio/iio_core.h
@@ -79,4 +79,7 @@ void iio_device_unregister_eventset(struct iio_dev *indio_dev);
79void iio_device_wakeup_eventset(struct iio_dev *indio_dev); 79void iio_device_wakeup_eventset(struct iio_dev *indio_dev);
80int iio_event_getfd(struct iio_dev *indio_dev); 80int iio_event_getfd(struct iio_dev *indio_dev);
81 81
82struct iio_event_interface;
83bool iio_event_enabled(const struct iio_event_interface *ev_int);
84
82#endif 85#endif
diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c
index 97928d558a15..e0251b8c1a52 100644
--- a/drivers/iio/imu/bmi160/bmi160_core.c
+++ b/drivers/iio/imu/bmi160/bmi160_core.c
@@ -411,7 +411,8 @@ static irqreturn_t bmi160_trigger_handler(int irq, void *p)
411 buf[j++] = sample; 411 buf[j++] = sample;
412 } 412 }
413 413
414 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); 414 iio_push_to_buffers_with_timestamp(indio_dev, buf,
415 iio_get_time_ns(indio_dev));
415done: 416done:
416 iio_trigger_notify_done(indio_dev->trig); 417 iio_trigger_notify_done(indio_dev->trig);
417 return IRQ_HANDLED; 418 return IRQ_HANDLED;
diff --git a/drivers/iio/imu/inv_mpu6050/Kconfig b/drivers/iio/imu/inv_mpu6050/Kconfig
index f756feecfa4c..5483b2ea754d 100644
--- a/drivers/iio/imu/inv_mpu6050/Kconfig
+++ b/drivers/iio/imu/inv_mpu6050/Kconfig
@@ -13,8 +13,8 @@ config INV_MPU6050_I2C
13 select INV_MPU6050_IIO 13 select INV_MPU6050_IIO
14 select REGMAP_I2C 14 select REGMAP_I2C
15 help 15 help
16 This driver supports the Invensense MPU6050/6500/9150 motion tracking 16 This driver supports the Invensense MPU6050/6500/9150 and ICM20608
17 devices over I2C. 17 motion tracking devices over I2C.
18 This driver can be built as a module. The module will be called 18 This driver can be built as a module. The module will be called
19 inv-mpu6050-i2c. 19 inv-mpu6050-i2c.
20 20
@@ -24,7 +24,7 @@ config INV_MPU6050_SPI
24 select INV_MPU6050_IIO 24 select INV_MPU6050_IIO
25 select REGMAP_SPI 25 select REGMAP_SPI
26 help 26 help
27 This driver supports the Invensense MPU6000/6500/9150 motion tracking 27 This driver supports the Invensense MPU6050/6500/9150 and ICM20608
28 devices over SPI. 28 motion tracking devices over SPI.
29 This driver can be built as a module. The module will be called 29 This driver can be built as a module. The module will be called
30 inv-mpu6050-spi. 30 inv-mpu6050-spi.
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
index ee40dae5ab58..b9fcbf18aa99 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
@@ -113,6 +113,12 @@ static const struct inv_mpu6050_hw hw_info[] = {
113 .reg = &reg_set_6050, 113 .reg = &reg_set_6050,
114 .config = &chip_config_6050, 114 .config = &chip_config_6050,
115 }, 115 },
116 {
117 .whoami = INV_ICM20608_WHOAMI_VALUE,
118 .name = "ICM20608",
119 .reg = &reg_set_6500,
120 .config = &chip_config_6050,
121 },
116}; 122};
117 123
118int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask) 124int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
index e1fd7fa53e3b..19580d1db597 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
@@ -170,6 +170,7 @@ static const struct i2c_device_id inv_mpu_id[] = {
170 {"mpu6050", INV_MPU6050}, 170 {"mpu6050", INV_MPU6050},
171 {"mpu6500", INV_MPU6500}, 171 {"mpu6500", INV_MPU6500},
172 {"mpu9150", INV_MPU9150}, 172 {"mpu9150", INV_MPU9150},
173 {"icm20608", INV_ICM20608},
173 {} 174 {}
174}; 175};
175 176
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
index 3bf8544ccc9f..f0e8c5dd9fae 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
@@ -70,6 +70,7 @@ enum inv_devices {
70 INV_MPU6500, 70 INV_MPU6500,
71 INV_MPU6000, 71 INV_MPU6000,
72 INV_MPU9150, 72 INV_MPU9150,
73 INV_ICM20608,
73 INV_NUM_PARTS 74 INV_NUM_PARTS
74}; 75};
75 76
@@ -225,6 +226,7 @@ struct inv_mpu6050_state {
225#define INV_MPU6050_WHOAMI_VALUE 0x68 226#define INV_MPU6050_WHOAMI_VALUE 0x68
226#define INV_MPU6500_WHOAMI_VALUE 0x70 227#define INV_MPU6500_WHOAMI_VALUE 0x70
227#define INV_MPU9150_WHOAMI_VALUE 0x68 228#define INV_MPU9150_WHOAMI_VALUE 0x68
229#define INV_ICM20608_WHOAMI_VALUE 0xAF
228 230
229/* scan element definition */ 231/* scan element definition */
230enum inv_mpu6050_scan { 232enum inv_mpu6050_scan {
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
index d0700628ee6d..3a9f3eac91ab 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_ring.c
@@ -107,7 +107,7 @@ irqreturn_t inv_mpu6050_irq_handler(int irq, void *p)
107 struct inv_mpu6050_state *st = iio_priv(indio_dev); 107 struct inv_mpu6050_state *st = iio_priv(indio_dev);
108 s64 timestamp; 108 s64 timestamp;
109 109
110 timestamp = iio_get_time_ns(); 110 timestamp = iio_get_time_ns(indio_dev);
111 kfifo_in_spinlocked(&st->timestamps, &timestamp, 1, 111 kfifo_in_spinlocked(&st->timestamps, &timestamp, 1,
112 &st->time_stamp_lock); 112 &st->time_stamp_lock);
113 113
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c
index 190a4a51c830..6e6476dfa188 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c
@@ -82,6 +82,7 @@ static const struct spi_device_id inv_mpu_id[] = {
82 {"mpu6000", INV_MPU6000}, 82 {"mpu6000", INV_MPU6000},
83 {"mpu6500", INV_MPU6500}, 83 {"mpu6500", INV_MPU6500},
84 {"mpu9150", INV_MPU9150}, 84 {"mpu9150", INV_MPU9150},
85 {"icm20608", INV_ICM20608},
85 {} 86 {}
86}; 87};
87 88
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 2a85bd888619..f914d5d140e4 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -178,6 +178,86 @@ ssize_t iio_read_const_attr(struct device *dev,
178} 178}
179EXPORT_SYMBOL(iio_read_const_attr); 179EXPORT_SYMBOL(iio_read_const_attr);
180 180
181static int iio_device_set_clock(struct iio_dev *indio_dev, clockid_t clock_id)
182{
183 int ret;
184 const struct iio_event_interface *ev_int = indio_dev->event_interface;
185
186 ret = mutex_lock_interruptible(&indio_dev->mlock);
187 if (ret)
188 return ret;
189 if ((ev_int && iio_event_enabled(ev_int)) ||
190 iio_buffer_enabled(indio_dev)) {
191 mutex_unlock(&indio_dev->mlock);
192 return -EBUSY;
193 }
194 indio_dev->clock_id = clock_id;
195 mutex_unlock(&indio_dev->mlock);
196
197 return 0;
198}
199
200/**
201 * iio_get_time_ns() - utility function to get a time stamp for events etc
202 * @indio_dev: device
203 */
204s64 iio_get_time_ns(const struct iio_dev *indio_dev)
205{
206 struct timespec tp;
207
208 switch (iio_device_get_clock(indio_dev)) {
209 case CLOCK_REALTIME:
210 ktime_get_real_ts(&tp);
211 break;
212 case CLOCK_MONOTONIC:
213 ktime_get_ts(&tp);
214 break;
215 case CLOCK_MONOTONIC_RAW:
216 getrawmonotonic(&tp);
217 break;
218 case CLOCK_REALTIME_COARSE:
219 tp = current_kernel_time();
220 break;
221 case CLOCK_MONOTONIC_COARSE:
222 tp = get_monotonic_coarse();
223 break;
224 case CLOCK_BOOTTIME:
225 get_monotonic_boottime(&tp);
226 break;
227 case CLOCK_TAI:
228 timekeeping_clocktai(&tp);
229 break;
230 default:
231 BUG();
232 }
233
234 return timespec_to_ns(&tp);
235}
236EXPORT_SYMBOL(iio_get_time_ns);
237
238/**
239 * iio_get_time_res() - utility function to get time stamp clock resolution in
240 * nano seconds.
241 * @indio_dev: device
242 */
243unsigned int iio_get_time_res(const struct iio_dev *indio_dev)
244{
245 switch (iio_device_get_clock(indio_dev)) {
246 case CLOCK_REALTIME:
247 case CLOCK_MONOTONIC:
248 case CLOCK_MONOTONIC_RAW:
249 case CLOCK_BOOTTIME:
250 case CLOCK_TAI:
251 return hrtimer_resolution;
252 case CLOCK_REALTIME_COARSE:
253 case CLOCK_MONOTONIC_COARSE:
254 return LOW_RES_NSEC;
255 default:
256 BUG();
257 }
258}
259EXPORT_SYMBOL(iio_get_time_res);
260
181static int __init iio_init(void) 261static int __init iio_init(void)
182{ 262{
183 int ret; 263 int ret;
@@ -990,11 +1070,91 @@ static ssize_t iio_show_dev_name(struct device *dev,
990 1070
991static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL); 1071static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
992 1072
1073static ssize_t iio_show_timestamp_clock(struct device *dev,
1074 struct device_attribute *attr,
1075 char *buf)
1076{
1077 const struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1078 const clockid_t clk = iio_device_get_clock(indio_dev);
1079 const char *name;
1080 ssize_t sz;
1081
1082 switch (clk) {
1083 case CLOCK_REALTIME:
1084 name = "realtime\n";
1085 sz = sizeof("realtime\n");
1086 break;
1087 case CLOCK_MONOTONIC:
1088 name = "monotonic\n";
1089 sz = sizeof("monotonic\n");
1090 break;
1091 case CLOCK_MONOTONIC_RAW:
1092 name = "monotonic_raw\n";
1093 sz = sizeof("monotonic_raw\n");
1094 break;
1095 case CLOCK_REALTIME_COARSE:
1096 name = "realtime_coarse\n";
1097 sz = sizeof("realtime_coarse\n");
1098 break;
1099 case CLOCK_MONOTONIC_COARSE:
1100 name = "monotonic_coarse\n";
1101 sz = sizeof("monotonic_coarse\n");
1102 break;
1103 case CLOCK_BOOTTIME:
1104 name = "boottime\n";
1105 sz = sizeof("boottime\n");
1106 break;
1107 case CLOCK_TAI:
1108 name = "tai\n";
1109 sz = sizeof("tai\n");
1110 break;
1111 default:
1112 BUG();
1113 }
1114
1115 memcpy(buf, name, sz);
1116 return sz;
1117}
1118
1119static ssize_t iio_store_timestamp_clock(struct device *dev,
1120 struct device_attribute *attr,
1121 const char *buf, size_t len)
1122{
1123 clockid_t clk;
1124 int ret;
1125
1126 if (sysfs_streq(buf, "realtime"))
1127 clk = CLOCK_REALTIME;
1128 else if (sysfs_streq(buf, "monotonic"))
1129 clk = CLOCK_MONOTONIC;
1130 else if (sysfs_streq(buf, "monotonic_raw"))
1131 clk = CLOCK_MONOTONIC_RAW;
1132 else if (sysfs_streq(buf, "realtime_coarse"))
1133 clk = CLOCK_REALTIME_COARSE;
1134 else if (sysfs_streq(buf, "monotonic_coarse"))
1135 clk = CLOCK_MONOTONIC_COARSE;
1136 else if (sysfs_streq(buf, "boottime"))
1137 clk = CLOCK_BOOTTIME;
1138 else if (sysfs_streq(buf, "tai"))
1139 clk = CLOCK_TAI;
1140 else
1141 return -EINVAL;
1142
1143 ret = iio_device_set_clock(dev_to_iio_dev(dev), clk);
1144 if (ret)
1145 return ret;
1146
1147 return len;
1148}
1149
1150static DEVICE_ATTR(current_timestamp_clock, S_IRUGO | S_IWUSR,
1151 iio_show_timestamp_clock, iio_store_timestamp_clock);
1152
993static int iio_device_register_sysfs(struct iio_dev *indio_dev) 1153static int iio_device_register_sysfs(struct iio_dev *indio_dev)
994{ 1154{
995 int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 1155 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
996 struct iio_dev_attr *p; 1156 struct iio_dev_attr *p;
997 struct attribute **attr; 1157 struct attribute **attr, *clk = NULL;
998 1158
999 /* First count elements in any existing group */ 1159 /* First count elements in any existing group */
1000 if (indio_dev->info->attrs) { 1160 if (indio_dev->info->attrs) {
@@ -1009,16 +1169,25 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1009 */ 1169 */
1010 if (indio_dev->channels) 1170 if (indio_dev->channels)
1011 for (i = 0; i < indio_dev->num_channels; i++) { 1171 for (i = 0; i < indio_dev->num_channels; i++) {
1012 ret = iio_device_add_channel_sysfs(indio_dev, 1172 const struct iio_chan_spec *chan =
1013 &indio_dev 1173 &indio_dev->channels[i];
1014 ->channels[i]); 1174
1175 if (chan->type == IIO_TIMESTAMP)
1176 clk = &dev_attr_current_timestamp_clock.attr;
1177
1178 ret = iio_device_add_channel_sysfs(indio_dev, chan);
1015 if (ret < 0) 1179 if (ret < 0)
1016 goto error_clear_attrs; 1180 goto error_clear_attrs;
1017 attrcount += ret; 1181 attrcount += ret;
1018 } 1182 }
1019 1183
1184 if (indio_dev->event_interface)
1185 clk = &dev_attr_current_timestamp_clock.attr;
1186
1020 if (indio_dev->name) 1187 if (indio_dev->name)
1021 attrcount++; 1188 attrcount++;
1189 if (clk)
1190 attrcount++;
1022 1191
1023 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1, 1192 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
1024 sizeof(indio_dev->chan_attr_group.attrs[0]), 1193 sizeof(indio_dev->chan_attr_group.attrs[0]),
@@ -1039,6 +1208,8 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1039 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr; 1208 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
1040 if (indio_dev->name) 1209 if (indio_dev->name)
1041 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr; 1210 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
1211 if (clk)
1212 indio_dev->chan_attr_group.attrs[attrn++] = clk;
1042 1213
1043 indio_dev->groups[indio_dev->groupcounter++] = 1214 indio_dev->groups[indio_dev->groupcounter++] =
1044 &indio_dev->chan_attr_group; 1215 &indio_dev->chan_attr_group;
diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index cae332b1d7ea..0ebfc923a997 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -44,6 +44,11 @@ struct iio_event_interface {
44 struct mutex read_lock; 44 struct mutex read_lock;
45}; 45};
46 46
47bool iio_event_enabled(const struct iio_event_interface *ev_int)
48{
49 return !!test_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
50}
51
47/** 52/**
48 * iio_push_event() - try to add event to the list for userspace reading 53 * iio_push_event() - try to add event to the list for userspace reading
49 * @indio_dev: IIO device structure 54 * @indio_dev: IIO device structure
@@ -60,7 +65,7 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
60 int copied; 65 int copied;
61 66
62 /* Does anyone care? */ 67 /* Does anyone care? */
63 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) { 68 if (iio_event_enabled(ev_int)) {
64 69
65 ev.id = ev_code; 70 ev.id = ev_code;
66 ev.timestamp = timestamp; 71 ev.timestamp = timestamp;
@@ -180,8 +185,14 @@ int iio_event_getfd(struct iio_dev *indio_dev)
180 if (ev_int == NULL) 185 if (ev_int == NULL)
181 return -ENODEV; 186 return -ENODEV;
182 187
183 if (test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) 188 fd = mutex_lock_interruptible(&indio_dev->mlock);
184 return -EBUSY; 189 if (fd)
190 return fd;
191
192 if (test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
193 fd = -EBUSY;
194 goto unlock;
195 }
185 196
186 iio_device_get(indio_dev); 197 iio_device_get(indio_dev);
187 198
@@ -194,6 +205,8 @@ int iio_event_getfd(struct iio_dev *indio_dev)
194 kfifo_reset_out(&ev_int->det_events); 205 kfifo_reset_out(&ev_int->det_events);
195 } 206 }
196 207
208unlock:
209 mutex_unlock(&indio_dev->mlock);
197 return fd; 210 return fd;
198} 211}
199 212
diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 98457f044aa5..7ad82fdd3e5b 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -289,7 +289,7 @@ static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
289irqreturn_t iio_pollfunc_store_time(int irq, void *p) 289irqreturn_t iio_pollfunc_store_time(int irq, void *p)
290{ 290{
291 struct iio_poll_func *pf = p; 291 struct iio_poll_func *pf = p;
292 pf->timestamp = iio_get_time_ns(); 292 pf->timestamp = iio_get_time_ns(pf->indio_dev);
293 return IRQ_WAKE_THREAD; 293 return IRQ_WAKE_THREAD;
294} 294}
295EXPORT_SYMBOL(iio_pollfunc_store_time); 295EXPORT_SYMBOL(iio_pollfunc_store_time);
diff --git a/drivers/iio/light/acpi-als.c b/drivers/iio/light/acpi-als.c
index 53201d99a16c..f0b47c501f4e 100644
--- a/drivers/iio/light/acpi-als.c
+++ b/drivers/iio/light/acpi-als.c
@@ -118,7 +118,7 @@ static void acpi_als_notify(struct acpi_device *device, u32 event)
118 struct iio_dev *indio_dev = acpi_driver_data(device); 118 struct iio_dev *indio_dev = acpi_driver_data(device);
119 struct acpi_als *als = iio_priv(indio_dev); 119 struct acpi_als *als = iio_priv(indio_dev);
120 s32 *buffer = als->evt_buffer; 120 s32 *buffer = als->evt_buffer;
121 s64 time_ns = iio_get_time_ns(); 121 s64 time_ns = iio_get_time_ns(indio_dev);
122 s32 val; 122 s32 val;
123 int ret; 123 int ret;
124 124
diff --git a/drivers/iio/light/adjd_s311.c b/drivers/iio/light/adjd_s311.c
index 09ad5f1ce539..0113fc843a81 100644
--- a/drivers/iio/light/adjd_s311.c
+++ b/drivers/iio/light/adjd_s311.c
@@ -118,7 +118,7 @@ static irqreturn_t adjd_s311_trigger_handler(int irq, void *p)
118 struct iio_poll_func *pf = p; 118 struct iio_poll_func *pf = p;
119 struct iio_dev *indio_dev = pf->indio_dev; 119 struct iio_dev *indio_dev = pf->indio_dev;
120 struct adjd_s311_data *data = iio_priv(indio_dev); 120 struct adjd_s311_data *data = iio_priv(indio_dev);
121 s64 time_ns = iio_get_time_ns(); 121 s64 time_ns = iio_get_time_ns(indio_dev);
122 int i, j = 0; 122 int i, j = 0;
123 123
124 int ret = adjd_s311_req_data(indio_dev); 124 int ret = adjd_s311_req_data(indio_dev);
diff --git a/drivers/iio/light/apds9300.c b/drivers/iio/light/apds9300.c
index e1b9fa5a7e91..649b26f67813 100644
--- a/drivers/iio/light/apds9300.c
+++ b/drivers/iio/light/apds9300.c
@@ -396,7 +396,7 @@ static irqreturn_t apds9300_interrupt_handler(int irq, void *private)
396 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0, 396 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
397 IIO_EV_TYPE_THRESH, 397 IIO_EV_TYPE_THRESH,
398 IIO_EV_DIR_EITHER), 398 IIO_EV_DIR_EITHER),
399 iio_get_time_ns()); 399 iio_get_time_ns(dev_info));
400 400
401 apds9300_clear_intr(data); 401 apds9300_clear_intr(data);
402 402
diff --git a/drivers/iio/light/apds9960.c b/drivers/iio/light/apds9960.c
index 651d57b8abbf..a4304edc3e0f 100644
--- a/drivers/iio/light/apds9960.c
+++ b/drivers/iio/light/apds9960.c
@@ -807,7 +807,7 @@ static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
807 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0, 807 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
808 IIO_EV_TYPE_THRESH, 808 IIO_EV_TYPE_THRESH,
809 IIO_EV_DIR_EITHER), 809 IIO_EV_DIR_EITHER),
810 iio_get_time_ns()); 810 iio_get_time_ns(indio_dev));
811 regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1); 811 regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1);
812 } 812 }
813 813
@@ -816,7 +816,7 @@ static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
816 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0, 816 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
817 IIO_EV_TYPE_THRESH, 817 IIO_EV_TYPE_THRESH,
818 IIO_EV_DIR_EITHER), 818 IIO_EV_DIR_EITHER),
819 iio_get_time_ns()); 819 iio_get_time_ns(indio_dev));
820 regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1); 820 regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1);
821 } 821 }
822 822
diff --git a/drivers/iio/light/cm36651.c b/drivers/iio/light/cm36651.c
index c8d7b5ea7e78..9d66e89c57ef 100644
--- a/drivers/iio/light/cm36651.c
+++ b/drivers/iio/light/cm36651.c
@@ -268,7 +268,7 @@ static irqreturn_t cm36651_irq_handler(int irq, void *data)
268 CM36651_CMD_READ_RAW_PROXIMITY, 268 CM36651_CMD_READ_RAW_PROXIMITY,
269 IIO_EV_TYPE_THRESH, ev_dir); 269 IIO_EV_TYPE_THRESH, ev_dir);
270 270
271 iio_push_event(indio_dev, ev_code, iio_get_time_ns()); 271 iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev));
272 272
273 return IRQ_HANDLED; 273 return IRQ_HANDLED;
274} 274}
diff --git a/drivers/iio/light/gp2ap020a00f.c b/drivers/iio/light/gp2ap020a00f.c
index af73af3586a9..6ada9149f142 100644
--- a/drivers/iio/light/gp2ap020a00f.c
+++ b/drivers/iio/light/gp2ap020a00f.c
@@ -851,7 +851,7 @@ static irqreturn_t gp2ap020a00f_prox_sensing_handler(int irq, void *data)
851 GP2AP020A00F_SCAN_MODE_PROXIMITY, 851 GP2AP020A00F_SCAN_MODE_PROXIMITY,
852 IIO_EV_TYPE_ROC, 852 IIO_EV_TYPE_ROC,
853 IIO_EV_DIR_RISING), 853 IIO_EV_DIR_RISING),
854 iio_get_time_ns()); 854 iio_get_time_ns(indio_dev));
855 } else { 855 } else {
856 iio_push_event(indio_dev, 856 iio_push_event(indio_dev,
857 IIO_UNMOD_EVENT_CODE( 857 IIO_UNMOD_EVENT_CODE(
@@ -859,7 +859,7 @@ static irqreturn_t gp2ap020a00f_prox_sensing_handler(int irq, void *data)
859 GP2AP020A00F_SCAN_MODE_PROXIMITY, 859 GP2AP020A00F_SCAN_MODE_PROXIMITY,
860 IIO_EV_TYPE_ROC, 860 IIO_EV_TYPE_ROC,
861 IIO_EV_DIR_FALLING), 861 IIO_EV_DIR_FALLING),
862 iio_get_time_ns()); 862 iio_get_time_ns(indio_dev));
863 } 863 }
864 } 864 }
865 865
@@ -925,7 +925,7 @@ static irqreturn_t gp2ap020a00f_thresh_event_handler(int irq, void *data)
925 IIO_MOD_LIGHT_CLEAR, 925 IIO_MOD_LIGHT_CLEAR,
926 IIO_EV_TYPE_THRESH, 926 IIO_EV_TYPE_THRESH,
927 IIO_EV_DIR_RISING), 927 IIO_EV_DIR_RISING),
928 iio_get_time_ns()); 928 iio_get_time_ns(indio_dev));
929 } 929 }
930 930
931 if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV, &priv->flags)) { 931 if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV, &priv->flags)) {
@@ -939,7 +939,7 @@ static irqreturn_t gp2ap020a00f_thresh_event_handler(int irq, void *data)
939 IIO_MOD_LIGHT_CLEAR, 939 IIO_MOD_LIGHT_CLEAR,
940 IIO_EV_TYPE_THRESH, 940 IIO_EV_TYPE_THRESH,
941 IIO_EV_DIR_FALLING), 941 IIO_EV_DIR_FALLING),
942 iio_get_time_ns()); 942 iio_get_time_ns(indio_dev));
943 } 943 }
944 } 944 }
945 945
diff --git a/drivers/iio/light/isl29125.c b/drivers/iio/light/isl29125.c
index a6b9d66233d5..1d2c0c8a1d4f 100644
--- a/drivers/iio/light/isl29125.c
+++ b/drivers/iio/light/isl29125.c
@@ -44,6 +44,9 @@
44#define ISL29125_MODE_B 0x3 44#define ISL29125_MODE_B 0x3
45#define ISL29125_MODE_RGB 0x5 45#define ISL29125_MODE_RGB 0x5
46 46
47#define ISL29125_SENSING_RANGE_0 5722 /* 375 lux full range */
48#define ISL29125_SENSING_RANGE_1 152590 /* 10k lux full range */
49
47#define ISL29125_MODE_RANGE BIT(3) 50#define ISL29125_MODE_RANGE BIT(3)
48 51
49#define ISL29125_STATUS_CONV BIT(1) 52#define ISL29125_STATUS_CONV BIT(1)
@@ -139,9 +142,9 @@ static int isl29125_read_raw(struct iio_dev *indio_dev,
139 case IIO_CHAN_INFO_SCALE: 142 case IIO_CHAN_INFO_SCALE:
140 *val = 0; 143 *val = 0;
141 if (data->conf1 & ISL29125_MODE_RANGE) 144 if (data->conf1 & ISL29125_MODE_RANGE)
142 *val2 = 152590; /* 10k lux full range */ 145 *val2 = ISL29125_SENSING_RANGE_1; /*10k lux full range*/
143 else 146 else
144 *val2 = 5722; /* 375 lux full range */ 147 *val2 = ISL29125_SENSING_RANGE_0; /*375 lux full range*/
145 return IIO_VAL_INT_PLUS_MICRO; 148 return IIO_VAL_INT_PLUS_MICRO;
146 } 149 }
147 return -EINVAL; 150 return -EINVAL;
@@ -157,9 +160,9 @@ static int isl29125_write_raw(struct iio_dev *indio_dev,
157 case IIO_CHAN_INFO_SCALE: 160 case IIO_CHAN_INFO_SCALE:
158 if (val != 0) 161 if (val != 0)
159 return -EINVAL; 162 return -EINVAL;
160 if (val2 == 152590) 163 if (val2 == ISL29125_SENSING_RANGE_1)
161 data->conf1 |= ISL29125_MODE_RANGE; 164 data->conf1 |= ISL29125_MODE_RANGE;
162 else if (val2 == 5722) 165 else if (val2 == ISL29125_SENSING_RANGE_0)
163 data->conf1 &= ~ISL29125_MODE_RANGE; 166 data->conf1 &= ~ISL29125_MODE_RANGE;
164 else 167 else
165 return -EINVAL; 168 return -EINVAL;
@@ -188,7 +191,7 @@ static irqreturn_t isl29125_trigger_handler(int irq, void *p)
188 } 191 }
189 192
190 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 193 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
191 iio_get_time_ns()); 194 iio_get_time_ns(indio_dev));
192 195
193done: 196done:
194 iio_trigger_notify_done(indio_dev->trig); 197 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/light/lm3533-als.c b/drivers/iio/light/lm3533-als.c
index e56937c40a18..f409c2047c05 100644
--- a/drivers/iio/light/lm3533-als.c
+++ b/drivers/iio/light/lm3533-als.c
@@ -267,7 +267,7 @@ static irqreturn_t lm3533_als_isr(int irq, void *dev_id)
267 0, 267 0,
268 IIO_EV_TYPE_THRESH, 268 IIO_EV_TYPE_THRESH,
269 IIO_EV_DIR_EITHER), 269 IIO_EV_DIR_EITHER),
270 iio_get_time_ns()); 270 iio_get_time_ns(indio_dev));
271out: 271out:
272 return IRQ_HANDLED; 272 return IRQ_HANDLED;
273} 273}
diff --git a/drivers/iio/light/ltr501.c b/drivers/iio/light/ltr501.c
index 6bf89d8f3741..3afc53a3d0b6 100644
--- a/drivers/iio/light/ltr501.c
+++ b/drivers/iio/light/ltr501.c
@@ -1256,7 +1256,8 @@ static irqreturn_t ltr501_trigger_handler(int irq, void *p)
1256 buf[j++] = psdata & LTR501_PS_DATA_MASK; 1256 buf[j++] = psdata & LTR501_PS_DATA_MASK;
1257 } 1257 }
1258 1258
1259 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); 1259 iio_push_to_buffers_with_timestamp(indio_dev, buf,
1260 iio_get_time_ns(indio_dev));
1260 1261
1261done: 1262done:
1262 iio_trigger_notify_done(indio_dev->trig); 1263 iio_trigger_notify_done(indio_dev->trig);
@@ -1282,14 +1283,14 @@ static irqreturn_t ltr501_interrupt_handler(int irq, void *private)
1282 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0, 1283 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
1283 IIO_EV_TYPE_THRESH, 1284 IIO_EV_TYPE_THRESH,
1284 IIO_EV_DIR_EITHER), 1285 IIO_EV_DIR_EITHER),
1285 iio_get_time_ns()); 1286 iio_get_time_ns(indio_dev));
1286 1287
1287 if (status & LTR501_STATUS_PS_INTR) 1288 if (status & LTR501_STATUS_PS_INTR)
1288 iio_push_event(indio_dev, 1289 iio_push_event(indio_dev,
1289 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0, 1290 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
1290 IIO_EV_TYPE_THRESH, 1291 IIO_EV_TYPE_THRESH,
1291 IIO_EV_DIR_EITHER), 1292 IIO_EV_DIR_EITHER),
1292 iio_get_time_ns()); 1293 iio_get_time_ns(indio_dev));
1293 1294
1294 return IRQ_HANDLED; 1295 return IRQ_HANDLED;
1295} 1296}
diff --git a/drivers/iio/light/max44000.c b/drivers/iio/light/max44000.c
index f17cb2ea18f5..6511b20a2a29 100644
--- a/drivers/iio/light/max44000.c
+++ b/drivers/iio/light/max44000.c
@@ -511,7 +511,8 @@ static irqreturn_t max44000_trigger_handler(int irq, void *p)
511 } 511 }
512 mutex_unlock(&data->lock); 512 mutex_unlock(&data->lock);
513 513
514 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); 514 iio_push_to_buffers_with_timestamp(indio_dev, buf,
515 iio_get_time_ns(indio_dev));
515 iio_trigger_notify_done(indio_dev->trig); 516 iio_trigger_notify_done(indio_dev->trig);
516 return IRQ_HANDLED; 517 return IRQ_HANDLED;
517 518
diff --git a/drivers/iio/light/opt3001.c b/drivers/iio/light/opt3001.c
index b776c8ed4387..78c9b3a6453a 100644
--- a/drivers/iio/light/opt3001.c
+++ b/drivers/iio/light/opt3001.c
@@ -713,13 +713,13 @@ static irqreturn_t opt3001_irq(int irq, void *_iio)
713 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0, 713 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
714 IIO_EV_TYPE_THRESH, 714 IIO_EV_TYPE_THRESH,
715 IIO_EV_DIR_RISING), 715 IIO_EV_DIR_RISING),
716 iio_get_time_ns()); 716 iio_get_time_ns(iio));
717 if (ret & OPT3001_CONFIGURATION_FL) 717 if (ret & OPT3001_CONFIGURATION_FL)
718 iio_push_event(iio, 718 iio_push_event(iio,
719 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0, 719 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
720 IIO_EV_TYPE_THRESH, 720 IIO_EV_TYPE_THRESH,
721 IIO_EV_DIR_FALLING), 721 IIO_EV_DIR_FALLING),
722 iio_get_time_ns()); 722 iio_get_time_ns(iio));
723 } else if (ret & OPT3001_CONFIGURATION_CRF) { 723 } else if (ret & OPT3001_CONFIGURATION_CRF) {
724 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT); 724 ret = i2c_smbus_read_word_swapped(opt->client, OPT3001_RESULT);
725 if (ret < 0) { 725 if (ret < 0) {
diff --git a/drivers/iio/light/stk3310.c b/drivers/iio/light/stk3310.c
index 9e847f8f4f0c..45cf8b0a4363 100644
--- a/drivers/iio/light/stk3310.c
+++ b/drivers/iio/light/stk3310.c
@@ -528,7 +528,7 @@ static irqreturn_t stk3310_irq_handler(int irq, void *private)
528 struct iio_dev *indio_dev = private; 528 struct iio_dev *indio_dev = private;
529 struct stk3310_data *data = iio_priv(indio_dev); 529 struct stk3310_data *data = iio_priv(indio_dev);
530 530
531 data->timestamp = iio_get_time_ns(); 531 data->timestamp = iio_get_time_ns(indio_dev);
532 532
533 return IRQ_WAKE_THREAD; 533 return IRQ_WAKE_THREAD;
534} 534}
diff --git a/drivers/iio/light/tcs3414.c b/drivers/iio/light/tcs3414.c
index 8a15fb541dc3..a795afb7667b 100644
--- a/drivers/iio/light/tcs3414.c
+++ b/drivers/iio/light/tcs3414.c
@@ -216,7 +216,7 @@ static irqreturn_t tcs3414_trigger_handler(int irq, void *p)
216 } 216 }
217 217
218 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 218 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
219 iio_get_time_ns()); 219 iio_get_time_ns(indio_dev));
220 220
221done: 221done:
222 iio_trigger_notify_done(indio_dev->trig); 222 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/light/tcs3472.c b/drivers/iio/light/tcs3472.c
index b29312f99077..3aa71e34ae28 100644
--- a/drivers/iio/light/tcs3472.c
+++ b/drivers/iio/light/tcs3472.c
@@ -202,7 +202,7 @@ static irqreturn_t tcs3472_trigger_handler(int irq, void *p)
202 } 202 }
203 203
204 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 204 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
205 iio_get_time_ns()); 205 iio_get_time_ns(indio_dev));
206 206
207done: 207done:
208 iio_trigger_notify_done(indio_dev->trig); 208 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/light/tsl2563.c b/drivers/iio/light/tsl2563.c
index 57b108c30e98..04598ae993d4 100644
--- a/drivers/iio/light/tsl2563.c
+++ b/drivers/iio/light/tsl2563.c
@@ -630,7 +630,7 @@ static irqreturn_t tsl2563_event_handler(int irq, void *private)
630 0, 630 0,
631 IIO_EV_TYPE_THRESH, 631 IIO_EV_TYPE_THRESH,
632 IIO_EV_DIR_EITHER), 632 IIO_EV_DIR_EITHER),
633 iio_get_time_ns()); 633 iio_get_time_ns(dev_info));
634 634
635 /* clear the interrupt and push the event */ 635 /* clear the interrupt and push the event */
636 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT); 636 i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
diff --git a/drivers/iio/light/us5182d.c b/drivers/iio/light/us5182d.c
index 45bc2f742f46..20c40f780964 100644
--- a/drivers/iio/light/us5182d.c
+++ b/drivers/iio/light/us5182d.c
@@ -833,7 +833,7 @@ static irqreturn_t us5182d_irq_thread_handler(int irq, void *private)
833 dir = ret & US5182D_CFG0_PROX ? IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING; 833 dir = ret & US5182D_CFG0_PROX ? IIO_EV_DIR_RISING : IIO_EV_DIR_FALLING;
834 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1, IIO_EV_TYPE_THRESH, dir); 834 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1, IIO_EV_TYPE_THRESH, dir);
835 835
836 iio_push_event(indio_dev, ev, iio_get_time_ns()); 836 iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
837 837
838 ret = i2c_smbus_write_byte_data(data->client, US5182D_REG_CFG0, 838 ret = i2c_smbus_write_byte_data(data->client, US5182D_REG_CFG0,
839 ret & ~US5182D_CFG0_PX_IRQ); 839 ret & ~US5182D_CFG0_PX_IRQ);
diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c
index 57d3654d7caf..af8606cc7812 100644
--- a/drivers/iio/magnetometer/ak8975.c
+++ b/drivers/iio/magnetometer/ak8975.c
@@ -33,6 +33,7 @@
33#include <linux/of_gpio.h> 33#include <linux/of_gpio.h>
34#include <linux/acpi.h> 34#include <linux/acpi.h>
35#include <linux/regulator/consumer.h> 35#include <linux/regulator/consumer.h>
36#include <linux/pm_runtime.h>
36 37
37#include <linux/iio/iio.h> 38#include <linux/iio/iio.h>
38#include <linux/iio/sysfs.h> 39#include <linux/iio/sysfs.h>
@@ -379,37 +380,40 @@ struct ak8975_data {
379 u8 cntl_cache; 380 u8 cntl_cache;
380 struct iio_mount_matrix orientation; 381 struct iio_mount_matrix orientation;
381 struct regulator *vdd; 382 struct regulator *vdd;
383 struct regulator *vid;
382}; 384};
383 385
384/* Enable attached power regulator if any. */ 386/* Enable attached power regulator if any. */
385static int ak8975_power_on(struct i2c_client *client) 387static int ak8975_power_on(const struct ak8975_data *data)
386{ 388{
387 const struct iio_dev *indio_dev = i2c_get_clientdata(client);
388 struct ak8975_data *data = iio_priv(indio_dev);
389 int ret; 389 int ret;
390 390
391 data->vdd = devm_regulator_get(&client->dev, "vdd"); 391 ret = regulator_enable(data->vdd);
392 if (IS_ERR_OR_NULL(data->vdd)) { 392 if (ret) {
393 ret = PTR_ERR(data->vdd); 393 dev_warn(&data->client->dev,
394 if (ret == -ENODEV) 394 "Failed to enable specified Vdd supply\n");
395 ret = 0; 395 return ret;
396 } else {
397 ret = regulator_enable(data->vdd);
398 } 396 }
399 397 ret = regulator_enable(data->vid);
400 if (ret) 398 if (ret) {
401 dev_err(&client->dev, "failed to enable Vdd supply: %d\n", ret); 399 dev_warn(&data->client->dev,
402 return ret; 400 "Failed to enable specified Vid supply\n");
401 return ret;
402 }
403 /*
404 * According to the datasheet the power supply rise time i 200us
405 * and the minimum wait time before mode setting is 100us, in
406 * total 300 us. Add some margin and say minimum 500us here.
407 */
408 usleep_range(500, 1000);
409 return 0;
403} 410}
404 411
405/* Disable attached power regulator if any. */ 412/* Disable attached power regulator if any. */
406static void ak8975_power_off(const struct i2c_client *client) 413static void ak8975_power_off(const struct ak8975_data *data)
407{ 414{
408 const struct iio_dev *indio_dev = i2c_get_clientdata(client); 415 regulator_disable(data->vid);
409 const struct ak8975_data *data = iio_priv(indio_dev); 416 regulator_disable(data->vdd);
410
411 if (!IS_ERR_OR_NULL(data->vdd))
412 regulator_disable(data->vdd);
413} 417}
414 418
415/* 419/*
@@ -689,6 +693,8 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
689 u16 buff; 693 u16 buff;
690 int ret; 694 int ret;
691 695
696 pm_runtime_get_sync(&data->client->dev);
697
692 mutex_lock(&data->lock); 698 mutex_lock(&data->lock);
693 699
694 ret = ak8975_start_read_axis(data, client); 700 ret = ak8975_start_read_axis(data, client);
@@ -703,6 +709,9 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
703 709
704 mutex_unlock(&data->lock); 710 mutex_unlock(&data->lock);
705 711
712 pm_runtime_mark_last_busy(&data->client->dev);
713 pm_runtime_put_autosuspend(&data->client->dev);
714
706 /* Swap bytes and convert to valid range. */ 715 /* Swap bytes and convert to valid range. */
707 buff = le16_to_cpu(buff); 716 buff = le16_to_cpu(buff);
708 *val = clamp_t(s16, buff, -def->range, def->range); 717 *val = clamp_t(s16, buff, -def->range, def->range);
@@ -829,7 +838,8 @@ static void ak8975_fill_buffer(struct iio_dev *indio_dev)
829 buff[1] = clamp_t(s16, le16_to_cpu(buff[1]), -def->range, def->range); 838 buff[1] = clamp_t(s16, le16_to_cpu(buff[1]), -def->range, def->range);
830 buff[2] = clamp_t(s16, le16_to_cpu(buff[2]), -def->range, def->range); 839 buff[2] = clamp_t(s16, le16_to_cpu(buff[2]), -def->range, def->range);
831 840
832 iio_push_to_buffers_with_timestamp(indio_dev, buff, iio_get_time_ns()); 841 iio_push_to_buffers_with_timestamp(indio_dev, buff,
842 iio_get_time_ns(indio_dev));
833 return; 843 return;
834 844
835unlock: 845unlock:
@@ -923,7 +933,15 @@ static int ak8975_probe(struct i2c_client *client,
923 933
924 data->def = &ak_def_array[chipset]; 934 data->def = &ak_def_array[chipset];
925 935
926 err = ak8975_power_on(client); 936 /* Fetch the regulators */
937 data->vdd = devm_regulator_get(&client->dev, "vdd");
938 if (IS_ERR(data->vdd))
939 return PTR_ERR(data->vdd);
940 data->vid = devm_regulator_get(&client->dev, "vid");
941 if (IS_ERR(data->vid))
942 return PTR_ERR(data->vid);
943
944 err = ak8975_power_on(data);
927 if (err) 945 if (err)
928 return err; 946 return err;
929 947
@@ -963,25 +981,92 @@ static int ak8975_probe(struct i2c_client *client,
963 goto cleanup_buffer; 981 goto cleanup_buffer;
964 } 982 }
965 983
984 /* Enable runtime PM */
985 pm_runtime_get_noresume(&client->dev);
986 pm_runtime_set_active(&client->dev);
987 pm_runtime_enable(&client->dev);
988 /*
989 * The device comes online in 500us, so add two orders of magnitude
990 * of delay before autosuspending: 50 ms.
991 */
992 pm_runtime_set_autosuspend_delay(&client->dev, 50);
993 pm_runtime_use_autosuspend(&client->dev);
994 pm_runtime_put(&client->dev);
995
966 return 0; 996 return 0;
967 997
968cleanup_buffer: 998cleanup_buffer:
969 iio_triggered_buffer_cleanup(indio_dev); 999 iio_triggered_buffer_cleanup(indio_dev);
970power_off: 1000power_off:
971 ak8975_power_off(client); 1001 ak8975_power_off(data);
972 return err; 1002 return err;
973} 1003}
974 1004
975static int ak8975_remove(struct i2c_client *client) 1005static int ak8975_remove(struct i2c_client *client)
976{ 1006{
977 struct iio_dev *indio_dev = i2c_get_clientdata(client); 1007 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1008 struct ak8975_data *data = iio_priv(indio_dev);
978 1009
1010 pm_runtime_get_sync(&client->dev);
1011 pm_runtime_put_noidle(&client->dev);
1012 pm_runtime_disable(&client->dev);
979 iio_device_unregister(indio_dev); 1013 iio_device_unregister(indio_dev);
980 iio_triggered_buffer_cleanup(indio_dev); 1014 iio_triggered_buffer_cleanup(indio_dev);
981 ak8975_power_off(client); 1015 ak8975_set_mode(data, POWER_DOWN);
1016 ak8975_power_off(data);
1017
1018 return 0;
1019}
1020
1021#ifdef CONFIG_PM
1022static int ak8975_runtime_suspend(struct device *dev)
1023{
1024 struct i2c_client *client = to_i2c_client(dev);
1025 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1026 struct ak8975_data *data = iio_priv(indio_dev);
1027 int ret;
1028
1029 /* Set the device in power down if it wasn't already */
1030 ret = ak8975_set_mode(data, POWER_DOWN);
1031 if (ret < 0) {
1032 dev_err(&client->dev, "Error in setting power-down mode\n");
1033 return ret;
1034 }
1035 /* Next cut the regulators */
1036 ak8975_power_off(data);
1037
1038 return 0;
1039}
1040
1041static int ak8975_runtime_resume(struct device *dev)
1042{
1043 struct i2c_client *client = to_i2c_client(dev);
1044 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1045 struct ak8975_data *data = iio_priv(indio_dev);
1046 int ret;
1047
1048 /* Take up the regulators */
1049 ak8975_power_on(data);
1050 /*
1051 * We come up in powered down mode, the reading routines will
1052 * put us in the mode to read values later.
1053 */
1054 ret = ak8975_set_mode(data, POWER_DOWN);
1055 if (ret < 0) {
1056 dev_err(&client->dev, "Error in setting power-down mode\n");
1057 return ret;
1058 }
982 1059
983 return 0; 1060 return 0;
984} 1061}
1062#endif /* CONFIG_PM */
1063
1064static const struct dev_pm_ops ak8975_dev_pm_ops = {
1065 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1066 pm_runtime_force_resume)
1067 SET_RUNTIME_PM_OPS(ak8975_runtime_suspend,
1068 ak8975_runtime_resume, NULL)
1069};
985 1070
986static const struct i2c_device_id ak8975_id[] = { 1071static const struct i2c_device_id ak8975_id[] = {
987 {"ak8975", AK8975}, 1072 {"ak8975", AK8975},
@@ -1010,6 +1095,7 @@ MODULE_DEVICE_TABLE(of, ak8975_of_match);
1010static struct i2c_driver ak8975_driver = { 1095static struct i2c_driver ak8975_driver = {
1011 .driver = { 1096 .driver = {
1012 .name = "ak8975", 1097 .name = "ak8975",
1098 .pm = &ak8975_dev_pm_ops,
1013 .of_match_table = of_match_ptr(ak8975_of_match), 1099 .of_match_table = of_match_ptr(ak8975_of_match),
1014 .acpi_match_table = ACPI_PTR(ak_acpi_match), 1100 .acpi_match_table = ACPI_PTR(ak_acpi_match),
1015 }, 1101 },
diff --git a/drivers/iio/magnetometer/hmc5843_core.c b/drivers/iio/magnetometer/hmc5843_core.c
index 77882b466e0f..ba3e2a374ee5 100644
--- a/drivers/iio/magnetometer/hmc5843_core.c
+++ b/drivers/iio/magnetometer/hmc5843_core.c
@@ -451,7 +451,7 @@ static irqreturn_t hmc5843_trigger_handler(int irq, void *p)
451 goto done; 451 goto done;
452 452
453 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 453 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
454 iio_get_time_ns()); 454 iio_get_time_ns(indio_dev));
455 455
456done: 456done:
457 iio_trigger_notify_done(indio_dev->trig); 457 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c
index 261d517428e4..f2be4a049056 100644
--- a/drivers/iio/magnetometer/mag3110.c
+++ b/drivers/iio/magnetometer/mag3110.c
@@ -261,7 +261,7 @@ static irqreturn_t mag3110_trigger_handler(int irq, void *p)
261 } 261 }
262 262
263 iio_push_to_buffers_with_timestamp(indio_dev, buffer, 263 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
264 iio_get_time_ns()); 264 iio_get_time_ns(indio_dev));
265 265
266done: 266done:
267 iio_trigger_notify_done(indio_dev->trig); 267 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/potentiometer/Kconfig b/drivers/iio/potentiometer/Kconfig
index 0941c8d490a0..2e9da1cf3297 100644
--- a/drivers/iio/potentiometer/Kconfig
+++ b/drivers/iio/potentiometer/Kconfig
@@ -10,7 +10,7 @@ config DS1803
10 depends on I2C 10 depends on I2C
11 help 11 help
12 Say yes here to build support for the Maxim Integrated DS1803 12 Say yes here to build support for the Maxim Integrated DS1803
13 digital potentiomenter chip. 13 digital potentiometer chip.
14 14
15 To compile this driver as a module, choose M here: the 15 To compile this driver as a module, choose M here: the
16 module will be called ds1803. 16 module will be called ds1803.
@@ -39,7 +39,7 @@ config MCP4131
39 MCP4241, MCP4242, 39 MCP4241, MCP4242,
40 MCP4251, MCP4252, 40 MCP4251, MCP4252,
41 MCP4261, MCP4262, 41 MCP4261, MCP4262,
42 digital potentiomenter chips. 42 digital potentiometer chips.
43 43
44 To compile this driver as a module, choose M here: the 44 To compile this driver as a module, choose M here: the
45 module will be called mcp4131. 45 module will be called mcp4131.
@@ -49,9 +49,11 @@ config MCP4531
49 depends on I2C 49 depends on I2C
50 help 50 help
51 Say yes here to build support for the Microchip 51 Say yes here to build support for the Microchip
52 MCP4531, MCP4532, MCP4551, MCP4552, 52 MCP4531, MCP4532, MCP4541, MCP4542,
53 MCP4631, MCP4632, MCP4651, MCP4652 53 MCP4551, MCP4552, MCP4561, MCP4562,
54 digital potentiomenter chips. 54 MCP4631, MCP4632, MCP4641, MCP4642,
55 MCP4651, MCP4652, MCP4661, MCP4662
56 digital potentiometer chips.
55 57
56 To compile this driver as a module, choose M here: the 58 To compile this driver as a module, choose M here: the
57 module will be called mcp4531. 59 module will be called mcp4531.
diff --git a/drivers/iio/potentiometer/mcp4531.c b/drivers/iio/potentiometer/mcp4531.c
index 3b72e1a595db..13b6ae2fcf7b 100644
--- a/drivers/iio/potentiometer/mcp4531.c
+++ b/drivers/iio/potentiometer/mcp4531.c
@@ -8,12 +8,20 @@
8 * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address 8 * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
9 * mcp4531 1 129 5, 10, 50, 100 010111x 9 * mcp4531 1 129 5, 10, 50, 100 010111x
10 * mcp4532 1 129 5, 10, 50, 100 01011xx 10 * mcp4532 1 129 5, 10, 50, 100 01011xx
11 * mcp4541 1 129 5, 10, 50, 100 010111x
12 * mcp4542 1 129 5, 10, 50, 100 01011xx
11 * mcp4551 1 257 5, 10, 50, 100 010111x 13 * mcp4551 1 257 5, 10, 50, 100 010111x
12 * mcp4552 1 257 5, 10, 50, 100 01011xx 14 * mcp4552 1 257 5, 10, 50, 100 01011xx
15 * mcp4561 1 257 5, 10, 50, 100 010111x
16 * mcp4562 1 257 5, 10, 50, 100 01011xx
13 * mcp4631 2 129 5, 10, 50, 100 0101xxx 17 * mcp4631 2 129 5, 10, 50, 100 0101xxx
14 * mcp4632 2 129 5, 10, 50, 100 01011xx 18 * mcp4632 2 129 5, 10, 50, 100 01011xx
19 * mcp4641 2 129 5, 10, 50, 100 0101xxx
20 * mcp4642 2 129 5, 10, 50, 100 01011xx
15 * mcp4651 2 257 5, 10, 50, 100 0101xxx 21 * mcp4651 2 257 5, 10, 50, 100 0101xxx
16 * mcp4652 2 257 5, 10, 50, 100 01011xx 22 * mcp4652 2 257 5, 10, 50, 100 01011xx
23 * mcp4661 2 257 5, 10, 50, 100 0101xxx
24 * mcp4662 2 257 5, 10, 50, 100 01011xx
17 * 25 *
18 * This program is free software; you can redistribute it and/or modify it 26 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License version 2 as published by 27 * under the terms of the GNU General Public License version 2 as published by
@@ -23,6 +31,8 @@
23#include <linux/module.h> 31#include <linux/module.h>
24#include <linux/i2c.h> 32#include <linux/i2c.h>
25#include <linux/err.h> 33#include <linux/err.h>
34#include <linux/of.h>
35#include <linux/of_device.h>
26 36
27#include <linux/iio/iio.h> 37#include <linux/iio/iio.h>
28 38
@@ -37,18 +47,34 @@ enum mcp4531_type {
37 MCP453x_103, 47 MCP453x_103,
38 MCP453x_503, 48 MCP453x_503,
39 MCP453x_104, 49 MCP453x_104,
50 MCP454x_502,
51 MCP454x_103,
52 MCP454x_503,
53 MCP454x_104,
40 MCP455x_502, 54 MCP455x_502,
41 MCP455x_103, 55 MCP455x_103,
42 MCP455x_503, 56 MCP455x_503,
43 MCP455x_104, 57 MCP455x_104,
58 MCP456x_502,
59 MCP456x_103,
60 MCP456x_503,
61 MCP456x_104,
44 MCP463x_502, 62 MCP463x_502,
45 MCP463x_103, 63 MCP463x_103,
46 MCP463x_503, 64 MCP463x_503,
47 MCP463x_104, 65 MCP463x_104,
66 MCP464x_502,
67 MCP464x_103,
68 MCP464x_503,
69 MCP464x_104,
48 MCP465x_502, 70 MCP465x_502,
49 MCP465x_103, 71 MCP465x_103,
50 MCP465x_503, 72 MCP465x_503,
51 MCP465x_104, 73 MCP465x_104,
74 MCP466x_502,
75 MCP466x_103,
76 MCP466x_503,
77 MCP466x_104,
52}; 78};
53 79
54static const struct mcp4531_cfg mcp4531_cfg[] = { 80static const struct mcp4531_cfg mcp4531_cfg[] = {
@@ -56,18 +82,34 @@ static const struct mcp4531_cfg mcp4531_cfg[] = {
56 [MCP453x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, }, 82 [MCP453x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
57 [MCP453x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, }, 83 [MCP453x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, },
58 [MCP453x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, }, 84 [MCP453x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, },
85 [MCP454x_502] = { .wipers = 1, .max_pos = 128, .kohms = 5, },
86 [MCP454x_103] = { .wipers = 1, .max_pos = 128, .kohms = 10, },
87 [MCP454x_503] = { .wipers = 1, .max_pos = 128, .kohms = 50, },
88 [MCP454x_104] = { .wipers = 1, .max_pos = 128, .kohms = 100, },
59 [MCP455x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, }, 89 [MCP455x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, },
60 [MCP455x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, }, 90 [MCP455x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, },
61 [MCP455x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, }, 91 [MCP455x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
62 [MCP455x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, }, 92 [MCP455x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
93 [MCP456x_502] = { .wipers = 1, .max_pos = 256, .kohms = 5, },
94 [MCP456x_103] = { .wipers = 1, .max_pos = 256, .kohms = 10, },
95 [MCP456x_503] = { .wipers = 1, .max_pos = 256, .kohms = 50, },
96 [MCP456x_104] = { .wipers = 1, .max_pos = 256, .kohms = 100, },
63 [MCP463x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, }, 97 [MCP463x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, },
64 [MCP463x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, }, 98 [MCP463x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, },
65 [MCP463x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, }, 99 [MCP463x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, },
66 [MCP463x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, }, 100 [MCP463x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, },
101 [MCP464x_502] = { .wipers = 2, .max_pos = 128, .kohms = 5, },
102 [MCP464x_103] = { .wipers = 2, .max_pos = 128, .kohms = 10, },
103 [MCP464x_503] = { .wipers = 2, .max_pos = 128, .kohms = 50, },
104 [MCP464x_104] = { .wipers = 2, .max_pos = 128, .kohms = 100, },
67 [MCP465x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, }, 105 [MCP465x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, },
68 [MCP465x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, }, 106 [MCP465x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, },
69 [MCP465x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, }, 107 [MCP465x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, },
70 [MCP465x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, }, 108 [MCP465x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, },
109 [MCP466x_502] = { .wipers = 2, .max_pos = 256, .kohms = 5, },
110 [MCP466x_103] = { .wipers = 2, .max_pos = 256, .kohms = 10, },
111 [MCP466x_503] = { .wipers = 2, .max_pos = 256, .kohms = 50, },
112 [MCP466x_104] = { .wipers = 2, .max_pos = 256, .kohms = 100, },
71}; 113};
72 114
73#define MCP4531_WRITE (0 << 2) 115#define MCP4531_WRITE (0 << 2)
@@ -148,12 +190,89 @@ static const struct iio_info mcp4531_info = {
148 .driver_module = THIS_MODULE, 190 .driver_module = THIS_MODULE,
149}; 191};
150 192
193#ifdef CONFIG_OF
194
195#define MCP4531_COMPATIBLE(of_compatible, cfg) { \
196 .compatible = of_compatible, \
197 .data = &mcp4531_cfg[cfg], \
198}
199
200static const struct of_device_id mcp4531_of_match[] = {
201 MCP4531_COMPATIBLE("microchip,mcp4531-502", MCP453x_502),
202 MCP4531_COMPATIBLE("microchip,mcp4531-103", MCP453x_103),
203 MCP4531_COMPATIBLE("microchip,mcp4531-503", MCP453x_503),
204 MCP4531_COMPATIBLE("microchip,mcp4531-104", MCP453x_104),
205 MCP4531_COMPATIBLE("microchip,mcp4532-502", MCP453x_502),
206 MCP4531_COMPATIBLE("microchip,mcp4532-103", MCP453x_103),
207 MCP4531_COMPATIBLE("microchip,mcp4532-503", MCP453x_503),
208 MCP4531_COMPATIBLE("microchip,mcp4532-104", MCP453x_104),
209 MCP4531_COMPATIBLE("microchip,mcp4541-502", MCP454x_502),
210 MCP4531_COMPATIBLE("microchip,mcp4541-103", MCP454x_103),
211 MCP4531_COMPATIBLE("microchip,mcp4541-503", MCP454x_503),
212 MCP4531_COMPATIBLE("microchip,mcp4541-104", MCP454x_104),
213 MCP4531_COMPATIBLE("microchip,mcp4542-502", MCP454x_502),
214 MCP4531_COMPATIBLE("microchip,mcp4542-103", MCP454x_103),
215 MCP4531_COMPATIBLE("microchip,mcp4542-503", MCP454x_503),
216 MCP4531_COMPATIBLE("microchip,mcp4542-104", MCP454x_104),
217 MCP4531_COMPATIBLE("microchip,mcp4551-502", MCP455x_502),
218 MCP4531_COMPATIBLE("microchip,mcp4551-103", MCP455x_103),
219 MCP4531_COMPATIBLE("microchip,mcp4551-503", MCP455x_503),
220 MCP4531_COMPATIBLE("microchip,mcp4551-104", MCP455x_104),
221 MCP4531_COMPATIBLE("microchip,mcp4552-502", MCP455x_502),
222 MCP4531_COMPATIBLE("microchip,mcp4552-103", MCP455x_103),
223 MCP4531_COMPATIBLE("microchip,mcp4552-503", MCP455x_503),
224 MCP4531_COMPATIBLE("microchip,mcp4552-104", MCP455x_104),
225 MCP4531_COMPATIBLE("microchip,mcp4561-502", MCP456x_502),
226 MCP4531_COMPATIBLE("microchip,mcp4561-103", MCP456x_103),
227 MCP4531_COMPATIBLE("microchip,mcp4561-503", MCP456x_503),
228 MCP4531_COMPATIBLE("microchip,mcp4561-104", MCP456x_104),
229 MCP4531_COMPATIBLE("microchip,mcp4562-502", MCP456x_502),
230 MCP4531_COMPATIBLE("microchip,mcp4562-103", MCP456x_103),
231 MCP4531_COMPATIBLE("microchip,mcp4562-503", MCP456x_503),
232 MCP4531_COMPATIBLE("microchip,mcp4562-104", MCP456x_104),
233 MCP4531_COMPATIBLE("microchip,mcp4631-502", MCP463x_502),
234 MCP4531_COMPATIBLE("microchip,mcp4631-103", MCP463x_103),
235 MCP4531_COMPATIBLE("microchip,mcp4631-503", MCP463x_503),
236 MCP4531_COMPATIBLE("microchip,mcp4631-104", MCP463x_104),
237 MCP4531_COMPATIBLE("microchip,mcp4632-502", MCP463x_502),
238 MCP4531_COMPATIBLE("microchip,mcp4632-103", MCP463x_103),
239 MCP4531_COMPATIBLE("microchip,mcp4632-503", MCP463x_503),
240 MCP4531_COMPATIBLE("microchip,mcp4632-104", MCP463x_104),
241 MCP4531_COMPATIBLE("microchip,mcp4641-502", MCP464x_502),
242 MCP4531_COMPATIBLE("microchip,mcp4641-103", MCP464x_103),
243 MCP4531_COMPATIBLE("microchip,mcp4641-503", MCP464x_503),
244 MCP4531_COMPATIBLE("microchip,mcp4641-104", MCP464x_104),
245 MCP4531_COMPATIBLE("microchip,mcp4642-502", MCP464x_502),
246 MCP4531_COMPATIBLE("microchip,mcp4642-103", MCP464x_103),
247 MCP4531_COMPATIBLE("microchip,mcp4642-503", MCP464x_503),
248 MCP4531_COMPATIBLE("microchip,mcp4642-104", MCP464x_104),
249 MCP4531_COMPATIBLE("microchip,mcp4651-502", MCP465x_502),
250 MCP4531_COMPATIBLE("microchip,mcp4651-103", MCP465x_103),
251 MCP4531_COMPATIBLE("microchip,mcp4651-503", MCP465x_503),
252 MCP4531_COMPATIBLE("microchip,mcp4651-104", MCP465x_104),
253 MCP4531_COMPATIBLE("microchip,mcp4652-502", MCP465x_502),
254 MCP4531_COMPATIBLE("microchip,mcp4652-103", MCP465x_103),
255 MCP4531_COMPATIBLE("microchip,mcp4652-503", MCP465x_503),
256 MCP4531_COMPATIBLE("microchip,mcp4652-104", MCP465x_104),
257 MCP4531_COMPATIBLE("microchip,mcp4661-502", MCP466x_502),
258 MCP4531_COMPATIBLE("microchip,mcp4661-103", MCP466x_103),
259 MCP4531_COMPATIBLE("microchip,mcp4661-503", MCP466x_503),
260 MCP4531_COMPATIBLE("microchip,mcp4661-104", MCP466x_104),
261 MCP4531_COMPATIBLE("microchip,mcp4662-502", MCP466x_502),
262 MCP4531_COMPATIBLE("microchip,mcp4662-103", MCP466x_103),
263 MCP4531_COMPATIBLE("microchip,mcp4662-503", MCP466x_503),
264 MCP4531_COMPATIBLE("microchip,mcp4662-104", MCP466x_104),
265 { /* sentinel */ }
266};
267#endif
268
151static int mcp4531_probe(struct i2c_client *client, 269static int mcp4531_probe(struct i2c_client *client,
152 const struct i2c_device_id *id) 270 const struct i2c_device_id *id)
153{ 271{
154 struct device *dev = &client->dev; 272 struct device *dev = &client->dev;
155 struct mcp4531_data *data; 273 struct mcp4531_data *data;
156 struct iio_dev *indio_dev; 274 struct iio_dev *indio_dev;
275 const struct of_device_id *match;
157 276
158 if (!i2c_check_functionality(client->adapter, 277 if (!i2c_check_functionality(client->adapter,
159 I2C_FUNC_SMBUS_WORD_DATA)) { 278 I2C_FUNC_SMBUS_WORD_DATA)) {
@@ -167,7 +286,12 @@ static int mcp4531_probe(struct i2c_client *client,
167 data = iio_priv(indio_dev); 286 data = iio_priv(indio_dev);
168 i2c_set_clientdata(client, indio_dev); 287 i2c_set_clientdata(client, indio_dev);
169 data->client = client; 288 data->client = client;
170 data->cfg = &mcp4531_cfg[id->driver_data]; 289
290 match = of_match_device(of_match_ptr(mcp4531_of_match), dev);
291 if (match)
292 data->cfg = of_device_get_match_data(dev);
293 else
294 data->cfg = &mcp4531_cfg[id->driver_data];
171 295
172 indio_dev->dev.parent = dev; 296 indio_dev->dev.parent = dev;
173 indio_dev->info = &mcp4531_info; 297 indio_dev->info = &mcp4531_info;
@@ -187,6 +311,14 @@ static const struct i2c_device_id mcp4531_id[] = {
187 { "mcp4532-103", MCP453x_103 }, 311 { "mcp4532-103", MCP453x_103 },
188 { "mcp4532-503", MCP453x_503 }, 312 { "mcp4532-503", MCP453x_503 },
189 { "mcp4532-104", MCP453x_104 }, 313 { "mcp4532-104", MCP453x_104 },
314 { "mcp4541-502", MCP454x_502 },
315 { "mcp4541-103", MCP454x_103 },
316 { "mcp4541-503", MCP454x_503 },
317 { "mcp4541-104", MCP454x_104 },
318 { "mcp4542-502", MCP454x_502 },
319 { "mcp4542-103", MCP454x_103 },
320 { "mcp4542-503", MCP454x_503 },
321 { "mcp4542-104", MCP454x_104 },
190 { "mcp4551-502", MCP455x_502 }, 322 { "mcp4551-502", MCP455x_502 },
191 { "mcp4551-103", MCP455x_103 }, 323 { "mcp4551-103", MCP455x_103 },
192 { "mcp4551-503", MCP455x_503 }, 324 { "mcp4551-503", MCP455x_503 },
@@ -195,6 +327,14 @@ static const struct i2c_device_id mcp4531_id[] = {
195 { "mcp4552-103", MCP455x_103 }, 327 { "mcp4552-103", MCP455x_103 },
196 { "mcp4552-503", MCP455x_503 }, 328 { "mcp4552-503", MCP455x_503 },
197 { "mcp4552-104", MCP455x_104 }, 329 { "mcp4552-104", MCP455x_104 },
330 { "mcp4561-502", MCP456x_502 },
331 { "mcp4561-103", MCP456x_103 },
332 { "mcp4561-503", MCP456x_503 },
333 { "mcp4561-104", MCP456x_104 },
334 { "mcp4562-502", MCP456x_502 },
335 { "mcp4562-103", MCP456x_103 },
336 { "mcp4562-503", MCP456x_503 },
337 { "mcp4562-104", MCP456x_104 },
198 { "mcp4631-502", MCP463x_502 }, 338 { "mcp4631-502", MCP463x_502 },
199 { "mcp4631-103", MCP463x_103 }, 339 { "mcp4631-103", MCP463x_103 },
200 { "mcp4631-503", MCP463x_503 }, 340 { "mcp4631-503", MCP463x_503 },
@@ -203,6 +343,14 @@ static const struct i2c_device_id mcp4531_id[] = {
203 { "mcp4632-103", MCP463x_103 }, 343 { "mcp4632-103", MCP463x_103 },
204 { "mcp4632-503", MCP463x_503 }, 344 { "mcp4632-503", MCP463x_503 },
205 { "mcp4632-104", MCP463x_104 }, 345 { "mcp4632-104", MCP463x_104 },
346 { "mcp4641-502", MCP464x_502 },
347 { "mcp4641-103", MCP464x_103 },
348 { "mcp4641-503", MCP464x_503 },
349 { "mcp4641-104", MCP464x_104 },
350 { "mcp4642-502", MCP464x_502 },
351 { "mcp4642-103", MCP464x_103 },
352 { "mcp4642-503", MCP464x_503 },
353 { "mcp4642-104", MCP464x_104 },
206 { "mcp4651-502", MCP465x_502 }, 354 { "mcp4651-502", MCP465x_502 },
207 { "mcp4651-103", MCP465x_103 }, 355 { "mcp4651-103", MCP465x_103 },
208 { "mcp4651-503", MCP465x_503 }, 356 { "mcp4651-503", MCP465x_503 },
@@ -211,6 +359,14 @@ static const struct i2c_device_id mcp4531_id[] = {
211 { "mcp4652-103", MCP465x_103 }, 359 { "mcp4652-103", MCP465x_103 },
212 { "mcp4652-503", MCP465x_503 }, 360 { "mcp4652-503", MCP465x_503 },
213 { "mcp4652-104", MCP465x_104 }, 361 { "mcp4652-104", MCP465x_104 },
362 { "mcp4661-502", MCP466x_502 },
363 { "mcp4661-103", MCP466x_103 },
364 { "mcp4661-503", MCP466x_503 },
365 { "mcp4661-104", MCP466x_104 },
366 { "mcp4662-502", MCP466x_502 },
367 { "mcp4662-103", MCP466x_103 },
368 { "mcp4662-503", MCP466x_503 },
369 { "mcp4662-104", MCP466x_104 },
214 {} 370 {}
215}; 371};
216MODULE_DEVICE_TABLE(i2c, mcp4531_id); 372MODULE_DEVICE_TABLE(i2c, mcp4531_id);
@@ -218,6 +374,7 @@ MODULE_DEVICE_TABLE(i2c, mcp4531_id);
218static struct i2c_driver mcp4531_driver = { 374static struct i2c_driver mcp4531_driver = {
219 .driver = { 375 .driver = {
220 .name = "mcp4531", 376 .name = "mcp4531",
377 .of_match_table = of_match_ptr(mcp4531_of_match),
221 }, 378 },
222 .probe = mcp4531_probe, 379 .probe = mcp4531_probe,
223 .id_table = mcp4531_id, 380 .id_table = mcp4531_id,
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index 8d654f671b6b..d130cdc78f43 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -6,17 +6,33 @@
6menu "Pressure sensors" 6menu "Pressure sensors"
7 7
8config BMP280 8config BMP280
9 tristate "Bosch Sensortec BMP180 and BMP280 pressure sensor driver" 9 tristate "Bosch Sensortec BMP180/BMP280 pressure sensor I2C driver"
10 depends on I2C 10 depends on (I2C || SPI_MASTER)
11 depends on !(BMP085_I2C=y || BMP085_I2C=m) 11 depends on !(BMP085_I2C=y || BMP085_I2C=m)
12 select REGMAP_I2C 12 depends on !(BMP085_SPI=y || BMP085_SPI=m)
13 select REGMAP
14 select BMP280_I2C if (I2C)
15 select BMP280_SPI if (SPI_MASTER)
13 help 16 help
14 Say yes here to build support for Bosch Sensortec BMP180 and BMP280 17 Say yes here to build support for Bosch Sensortec BMP180 and BMP280
15 pressure and temperature sensors. Also supports the BE280 with 18 pressure and temperature sensors. Also supports the BE280 with
16 an additional humidty sensor channel. 19 an additional humidity sensor channel.
17 20
18 To compile this driver as a module, choose M here: the module 21 To compile this driver as a module, choose M here: the core module
19 will be called bmp280. 22 will be called bmp280 and you will also get bmp280-i2c for I2C
23 and/or bmp280-spi for SPI support.
24
25config BMP280_I2C
26 tristate
27 depends on BMP280
28 depends on I2C
29 select REGMAP_I2C
30
31config BMP280_SPI
32 tristate
33 depends on BMP280
34 depends on SPI_MASTER
35 select REGMAP
20 36
21config HID_SENSOR_PRESS 37config HID_SENSOR_PRESS
22 depends on HID_SENSOR_HUB 38 depends on HID_SENSOR_HUB
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile
index 17d6e7afa1ff..7f395bed5e88 100644
--- a/drivers/iio/pressure/Makefile
+++ b/drivers/iio/pressure/Makefile
@@ -4,6 +4,9 @@
4 4
5# When adding new entries keep the list in alphabetical order 5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_BMP280) += bmp280.o 6obj-$(CONFIG_BMP280) += bmp280.o
7bmp280-objs := bmp280-core.o bmp280-regmap.o
8obj-$(CONFIG_BMP280_I2C) += bmp280-i2c.o
9obj-$(CONFIG_BMP280_SPI) += bmp280-spi.o
7obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o 10obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o
8obj-$(CONFIG_HP03) += hp03.o 11obj-$(CONFIG_HP03) += hp03.o
9obj-$(CONFIG_MPL115) += mpl115.o 12obj-$(CONFIG_MPL115) += mpl115.o
diff --git a/drivers/iio/pressure/bmp280.c b/drivers/iio/pressure/bmp280-core.c
index dbbcd6d83e3b..6943688e66df 100644
--- a/drivers/iio/pressure/bmp280.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -1,5 +1,9 @@
1/* 1/*
2 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
3 * Copyright (c) 2012 Bosch Sensortec GmbH
4 * Copyright (c) 2012 Unixphere AB
2 * Copyright (c) 2014 Intel Corporation 5 * Copyright (c) 2014 Intel Corporation
6 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
3 * 7 *
4 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor. 8 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
5 * 9 *
@@ -15,113 +19,53 @@
15 19
16#define pr_fmt(fmt) "bmp280: " fmt 20#define pr_fmt(fmt) "bmp280: " fmt
17 21
22#include <linux/device.h>
18#include <linux/module.h> 23#include <linux/module.h>
19#include <linux/i2c.h>
20#include <linux/acpi.h>
21#include <linux/regmap.h> 24#include <linux/regmap.h>
22#include <linux/delay.h> 25#include <linux/delay.h>
23#include <linux/iio/iio.h> 26#include <linux/iio/iio.h>
24#include <linux/iio/sysfs.h> 27#include <linux/iio/sysfs.h>
28#include <linux/gpio/consumer.h>
29#include <linux/regulator/consumer.h>
30#include <linux/interrupt.h>
31#include <linux/irq.h> /* For irq_get_irq_data() */
32#include <linux/completion.h>
33#include <linux/pm_runtime.h>
34#include <linux/random.h>
25 35
26/* BMP280 specific registers */ 36#include "bmp280.h"
27#define BMP280_REG_HUMIDITY_LSB 0xFE 37
28#define BMP280_REG_HUMIDITY_MSB 0xFD 38/*
29#define BMP280_REG_TEMP_XLSB 0xFC 39 * These enums are used for indexing into the array of calibration
30#define BMP280_REG_TEMP_LSB 0xFB 40 * coefficients for BMP180.
31#define BMP280_REG_TEMP_MSB 0xFA 41 */
32#define BMP280_REG_PRESS_XLSB 0xF9 42enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
33#define BMP280_REG_PRESS_LSB 0xF8 43
34#define BMP280_REG_PRESS_MSB 0xF7 44struct bmp180_calib {
35 45 s16 AC1;
36#define BMP280_REG_CONFIG 0xF5 46 s16 AC2;
37#define BMP280_REG_CTRL_MEAS 0xF4 47 s16 AC3;
38#define BMP280_REG_STATUS 0xF3 48 u16 AC4;
39#define BMP280_REG_CTRL_HUMIDITY 0xF2 49 u16 AC5;
40 50 u16 AC6;
41/* Due to non linear mapping, and data sizes we can't do a bulk read */ 51 s16 B1;
42#define BMP280_REG_COMP_H1 0xA1 52 s16 B2;
43#define BMP280_REG_COMP_H2 0xE1 53 s16 MB;
44#define BMP280_REG_COMP_H3 0xE3 54 s16 MC;
45#define BMP280_REG_COMP_H4 0xE4 55 s16 MD;
46#define BMP280_REG_COMP_H5 0xE5 56};
47#define BMP280_REG_COMP_H6 0xE7
48
49#define BMP280_REG_COMP_TEMP_START 0x88
50#define BMP280_COMP_TEMP_REG_COUNT 6
51
52#define BMP280_REG_COMP_PRESS_START 0x8E
53#define BMP280_COMP_PRESS_REG_COUNT 18
54
55#define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
56#define BMP280_FILTER_OFF 0
57#define BMP280_FILTER_2X BIT(2)
58#define BMP280_FILTER_4X BIT(3)
59#define BMP280_FILTER_8X (BIT(3) | BIT(2))
60#define BMP280_FILTER_16X BIT(4)
61
62#define BMP280_OSRS_HUMIDITY_MASK (BIT(2) | BIT(1) | BIT(0))
63#define BMP280_OSRS_HUMIDITIY_X(osrs_h) ((osrs_h) << 0)
64#define BMP280_OSRS_HUMIDITY_SKIP 0
65#define BMP280_OSRS_HUMIDITY_1X BMP280_OSRS_HUMIDITIY_X(1)
66#define BMP280_OSRS_HUMIDITY_2X BMP280_OSRS_HUMIDITIY_X(2)
67#define BMP280_OSRS_HUMIDITY_4X BMP280_OSRS_HUMIDITIY_X(3)
68#define BMP280_OSRS_HUMIDITY_8X BMP280_OSRS_HUMIDITIY_X(4)
69#define BMP280_OSRS_HUMIDITY_16X BMP280_OSRS_HUMIDITIY_X(5)
70
71#define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
72#define BMP280_OSRS_TEMP_SKIP 0
73#define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
74#define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
75#define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
76#define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
77#define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
78#define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
79
80#define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
81#define BMP280_OSRS_PRESS_SKIP 0
82#define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
83#define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
84#define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
85#define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
86#define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
87#define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
88
89#define BMP280_MODE_MASK (BIT(1) | BIT(0))
90#define BMP280_MODE_SLEEP 0
91#define BMP280_MODE_FORCED BIT(0)
92#define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
93
94/* BMP180 specific registers */
95#define BMP180_REG_OUT_XLSB 0xF8
96#define BMP180_REG_OUT_LSB 0xF7
97#define BMP180_REG_OUT_MSB 0xF6
98
99#define BMP180_REG_CALIB_START 0xAA
100#define BMP180_REG_CALIB_COUNT 22
101
102#define BMP180_MEAS_SCO BIT(5)
103#define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
104#define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
105#define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
106#define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
107#define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
108#define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
109
110/* BMP180 and BMP280 common registers */
111#define BMP280_REG_CTRL_MEAS 0xF4
112#define BMP280_REG_RESET 0xE0
113#define BMP280_REG_ID 0xD0
114
115#define BMP180_CHIP_ID 0x55
116#define BMP280_CHIP_ID 0x58
117#define BME280_CHIP_ID 0x60
118#define BMP280_SOFT_RESET_VAL 0xB6
119 57
120struct bmp280_data { 58struct bmp280_data {
121 struct i2c_client *client; 59 struct device *dev;
122 struct mutex lock; 60 struct mutex lock;
123 struct regmap *regmap; 61 struct regmap *regmap;
62 struct completion done;
63 bool use_eoc;
124 const struct bmp280_chip_info *chip_info; 64 const struct bmp280_chip_info *chip_info;
65 struct bmp180_calib calib;
66 struct regulator *vddd;
67 struct regulator *vdda;
68 unsigned int start_up_time; /* in milliseconds */
125 69
126 /* log of base 2 of oversampling rate */ 70 /* log of base 2 of oversampling rate */
127 u8 oversampling_press; 71 u8 oversampling_press;
@@ -136,8 +80,6 @@ struct bmp280_data {
136}; 80};
137 81
138struct bmp280_chip_info { 82struct bmp280_chip_info {
139 const struct regmap_config *regmap_config;
140
141 const int *oversampling_temp_avail; 83 const int *oversampling_temp_avail;
142 int num_oversampling_temp_avail; 84 int num_oversampling_temp_avail;
143 85
@@ -178,48 +120,6 @@ static const struct iio_chan_spec bmp280_channels[] = {
178 }, 120 },
179}; 121};
180 122
181static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
182{
183 switch (reg) {
184 case BMP280_REG_CONFIG:
185 case BMP280_REG_CTRL_HUMIDITY:
186 case BMP280_REG_CTRL_MEAS:
187 case BMP280_REG_RESET:
188 return true;
189 default:
190 return false;
191 };
192}
193
194static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
195{
196 switch (reg) {
197 case BMP280_REG_HUMIDITY_LSB:
198 case BMP280_REG_HUMIDITY_MSB:
199 case BMP280_REG_TEMP_XLSB:
200 case BMP280_REG_TEMP_LSB:
201 case BMP280_REG_TEMP_MSB:
202 case BMP280_REG_PRESS_XLSB:
203 case BMP280_REG_PRESS_LSB:
204 case BMP280_REG_PRESS_MSB:
205 case BMP280_REG_STATUS:
206 return true;
207 default:
208 return false;
209 }
210}
211
212static const struct regmap_config bmp280_regmap_config = {
213 .reg_bits = 8,
214 .val_bits = 8,
215
216 .max_register = BMP280_REG_HUMIDITY_LSB,
217 .cache_type = REGCACHE_RBTREE,
218
219 .writeable_reg = bmp280_is_writeable_reg,
220 .volatile_reg = bmp280_is_volatile_reg,
221};
222
223/* 123/*
224 * Returns humidity in percent, resolution is 0.01 percent. Output value of 124 * Returns humidity in percent, resolution is 0.01 percent. Output value of
225 * "47445" represents 47445/1024 = 46.333 %RH. 125 * "47445" represents 47445/1024 = 46.333 %RH.
@@ -230,7 +130,7 @@ static const struct regmap_config bmp280_regmap_config = {
230static u32 bmp280_compensate_humidity(struct bmp280_data *data, 130static u32 bmp280_compensate_humidity(struct bmp280_data *data,
231 s32 adc_humidity) 131 s32 adc_humidity)
232{ 132{
233 struct device *dev = &data->client->dev; 133 struct device *dev = data->dev;
234 unsigned int H1, H3, tmp; 134 unsigned int H1, H3, tmp;
235 int H2, H4, H5, H6, ret, var; 135 int H2, H4, H5, H6, ret, var;
236 136
@@ -301,7 +201,7 @@ static s32 bmp280_compensate_temp(struct bmp280_data *data,
301 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START, 201 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
302 buf, BMP280_COMP_TEMP_REG_COUNT); 202 buf, BMP280_COMP_TEMP_REG_COUNT);
303 if (ret < 0) { 203 if (ret < 0) {
304 dev_err(&data->client->dev, 204 dev_err(data->dev,
305 "failed to read temperature calibration parameters\n"); 205 "failed to read temperature calibration parameters\n");
306 return ret; 206 return ret;
307 } 207 }
@@ -341,7 +241,7 @@ static u32 bmp280_compensate_press(struct bmp280_data *data,
341 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START, 241 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
342 buf, BMP280_COMP_PRESS_REG_COUNT); 242 buf, BMP280_COMP_PRESS_REG_COUNT);
343 if (ret < 0) { 243 if (ret < 0) {
344 dev_err(&data->client->dev, 244 dev_err(data->dev,
345 "failed to read pressure calibration parameters\n"); 245 "failed to read pressure calibration parameters\n");
346 return ret; 246 return ret;
347 } 247 }
@@ -376,7 +276,7 @@ static int bmp280_read_temp(struct bmp280_data *data,
376 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, 276 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
377 (u8 *) &tmp, 3); 277 (u8 *) &tmp, 3);
378 if (ret < 0) { 278 if (ret < 0) {
379 dev_err(&data->client->dev, "failed to read temperature\n"); 279 dev_err(data->dev, "failed to read temperature\n");
380 return ret; 280 return ret;
381 } 281 }
382 282
@@ -411,7 +311,7 @@ static int bmp280_read_press(struct bmp280_data *data,
411 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, 311 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
412 (u8 *) &tmp, 3); 312 (u8 *) &tmp, 3);
413 if (ret < 0) { 313 if (ret < 0) {
414 dev_err(&data->client->dev, "failed to read pressure\n"); 314 dev_err(data->dev, "failed to read pressure\n");
415 return ret; 315 return ret;
416 } 316 }
417 317
@@ -439,7 +339,7 @@ static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
439 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB, 339 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
440 (u8 *) &tmp, 2); 340 (u8 *) &tmp, 2);
441 if (ret < 0) { 341 if (ret < 0) {
442 dev_err(&data->client->dev, "failed to read humidity\n"); 342 dev_err(data->dev, "failed to read humidity\n");
443 return ret; 343 return ret;
444 } 344 }
445 345
@@ -459,6 +359,7 @@ static int bmp280_read_raw(struct iio_dev *indio_dev,
459 int ret; 359 int ret;
460 struct bmp280_data *data = iio_priv(indio_dev); 360 struct bmp280_data *data = iio_priv(indio_dev);
461 361
362 pm_runtime_get_sync(data->dev);
462 mutex_lock(&data->lock); 363 mutex_lock(&data->lock);
463 364
464 switch (mask) { 365 switch (mask) {
@@ -503,6 +404,8 @@ static int bmp280_read_raw(struct iio_dev *indio_dev,
503 } 404 }
504 405
505 mutex_unlock(&data->lock); 406 mutex_unlock(&data->lock);
407 pm_runtime_mark_last_busy(data->dev);
408 pm_runtime_put_autosuspend(data->dev);
506 409
507 return ret; 410 return ret;
508} 411}
@@ -567,6 +470,7 @@ static int bmp280_write_raw(struct iio_dev *indio_dev,
567 470
568 switch (mask) { 471 switch (mask) {
569 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 472 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
473 pm_runtime_get_sync(data->dev);
570 mutex_lock(&data->lock); 474 mutex_lock(&data->lock);
571 switch (chan->type) { 475 switch (chan->type) {
572 case IIO_HUMIDITYRELATIVE: 476 case IIO_HUMIDITYRELATIVE:
@@ -583,6 +487,8 @@ static int bmp280_write_raw(struct iio_dev *indio_dev,
583 break; 487 break;
584 } 488 }
585 mutex_unlock(&data->lock); 489 mutex_unlock(&data->lock);
490 pm_runtime_mark_last_busy(data->dev);
491 pm_runtime_put_autosuspend(data->dev);
586 break; 492 break;
587 default: 493 default:
588 return -EINVAL; 494 return -EINVAL;
@@ -657,7 +563,7 @@ static int bmp280_chip_config(struct bmp280_data *data)
657 BMP280_MODE_MASK, 563 BMP280_MODE_MASK,
658 osrs | BMP280_MODE_NORMAL); 564 osrs | BMP280_MODE_NORMAL);
659 if (ret < 0) { 565 if (ret < 0) {
660 dev_err(&data->client->dev, 566 dev_err(data->dev,
661 "failed to write ctrl_meas register\n"); 567 "failed to write ctrl_meas register\n");
662 return ret; 568 return ret;
663 } 569 }
@@ -666,7 +572,7 @@ static int bmp280_chip_config(struct bmp280_data *data)
666 BMP280_FILTER_MASK, 572 BMP280_FILTER_MASK,
667 BMP280_FILTER_4X); 573 BMP280_FILTER_4X);
668 if (ret < 0) { 574 if (ret < 0) {
669 dev_err(&data->client->dev, 575 dev_err(data->dev,
670 "failed to write config register\n"); 576 "failed to write config register\n");
671 return ret; 577 return ret;
672 } 578 }
@@ -677,8 +583,6 @@ static int bmp280_chip_config(struct bmp280_data *data)
677static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; 583static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
678 584
679static const struct bmp280_chip_info bmp280_chip_info = { 585static const struct bmp280_chip_info bmp280_chip_info = {
680 .regmap_config = &bmp280_regmap_config,
681
682 .oversampling_temp_avail = bmp280_oversampling_avail, 586 .oversampling_temp_avail = bmp280_oversampling_avail,
683 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), 587 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
684 588
@@ -703,8 +607,6 @@ static int bme280_chip_config(struct bmp280_data *data)
703} 607}
704 608
705static const struct bmp280_chip_info bme280_chip_info = { 609static const struct bmp280_chip_info bme280_chip_info = {
706 .regmap_config = &bmp280_regmap_config,
707
708 .oversampling_temp_avail = bmp280_oversampling_avail, 610 .oversampling_temp_avail = bmp280_oversampling_avail,
709 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), 611 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
710 612
@@ -720,42 +622,6 @@ static const struct bmp280_chip_info bme280_chip_info = {
720 .read_humid = bmp280_read_humid, 622 .read_humid = bmp280_read_humid,
721}; 623};
722 624
723
724static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
725{
726 switch (reg) {
727 case BMP280_REG_CTRL_MEAS:
728 case BMP280_REG_RESET:
729 return true;
730 default:
731 return false;
732 };
733}
734
735static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
736{
737 switch (reg) {
738 case BMP180_REG_OUT_XLSB:
739 case BMP180_REG_OUT_LSB:
740 case BMP180_REG_OUT_MSB:
741 case BMP280_REG_CTRL_MEAS:
742 return true;
743 default:
744 return false;
745 }
746}
747
748static const struct regmap_config bmp180_regmap_config = {
749 .reg_bits = 8,
750 .val_bits = 8,
751
752 .max_register = BMP180_REG_OUT_XLSB,
753 .cache_type = REGCACHE_RBTREE,
754
755 .writeable_reg = bmp180_is_writeable_reg,
756 .volatile_reg = bmp180_is_volatile_reg,
757};
758
759static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas) 625static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
760{ 626{
761 int ret; 627 int ret;
@@ -763,16 +629,32 @@ static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
763 unsigned int delay_us; 629 unsigned int delay_us;
764 unsigned int ctrl; 630 unsigned int ctrl;
765 631
632 if (data->use_eoc)
633 init_completion(&data->done);
634
766 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas); 635 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
767 if (ret) 636 if (ret)
768 return ret; 637 return ret;
769 638
770 if (ctrl_meas == BMP180_MEAS_TEMP) 639 if (data->use_eoc) {
771 delay_us = 4500; 640 /*
772 else 641 * If we have a completion interrupt, use it, wait up to
773 delay_us = conversion_time_max[data->oversampling_press]; 642 * 100ms. The longest conversion time listed is 76.5 ms for
774 643 * advanced resolution mode.
775 usleep_range(delay_us, delay_us + 1000); 644 */
645 ret = wait_for_completion_timeout(&data->done,
646 1 + msecs_to_jiffies(100));
647 if (!ret)
648 dev_err(data->dev, "timeout waiting for completion\n");
649 } else {
650 if (ctrl_meas == BMP180_MEAS_TEMP)
651 delay_us = 4500;
652 else
653 delay_us =
654 conversion_time_max[data->oversampling_press];
655
656 usleep_range(delay_us, delay_us + 1000);
657 }
776 658
777 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl); 659 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
778 if (ret) 660 if (ret)
@@ -803,26 +685,6 @@ static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
803 return 0; 685 return 0;
804} 686}
805 687
806/*
807 * These enums are used for indexing into the array of calibration
808 * coefficients for BMP180.
809 */
810enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
811
812struct bmp180_calib {
813 s16 AC1;
814 s16 AC2;
815 s16 AC3;
816 u16 AC4;
817 u16 AC5;
818 u16 AC6;
819 s16 B1;
820 s16 B2;
821 s16 MB;
822 s16 MC;
823 s16 MD;
824};
825
826static int bmp180_read_calib(struct bmp280_data *data, 688static int bmp180_read_calib(struct bmp280_data *data,
827 struct bmp180_calib *calib) 689 struct bmp180_calib *calib)
828{ 690{
@@ -842,6 +704,9 @@ static int bmp180_read_calib(struct bmp280_data *data,
842 return -EIO; 704 return -EIO;
843 } 705 }
844 706
707 /* Toss the calibration data into the entropy pool */
708 add_device_randomness(buf, sizeof(buf));
709
845 calib->AC1 = be16_to_cpu(buf[AC1]); 710 calib->AC1 = be16_to_cpu(buf[AC1]);
846 calib->AC2 = be16_to_cpu(buf[AC2]); 711 calib->AC2 = be16_to_cpu(buf[AC2]);
847 calib->AC3 = be16_to_cpu(buf[AC3]); 712 calib->AC3 = be16_to_cpu(buf[AC3]);
@@ -865,19 +730,11 @@ static int bmp180_read_calib(struct bmp280_data *data,
865 */ 730 */
866static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp) 731static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
867{ 732{
868 int ret;
869 s32 x1, x2; 733 s32 x1, x2;
870 struct bmp180_calib calib; 734 struct bmp180_calib *calib = &data->calib;
871 735
872 ret = bmp180_read_calib(data, &calib); 736 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
873 if (ret < 0) { 737 x2 = (calib->MC << 11) / (x1 + calib->MD);
874 dev_err(&data->client->dev,
875 "failed to read calibration coefficients\n");
876 return ret;
877 }
878
879 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
880 x2 = (calib.MC << 11) / (x1 + calib.MD);
881 data->t_fine = x1 + x2; 738 data->t_fine = x1 + x2;
882 739
883 return (data->t_fine + 8) >> 4; 740 return (data->t_fine + 8) >> 4;
@@ -932,29 +789,21 @@ static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
932 */ 789 */
933static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press) 790static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
934{ 791{
935 int ret;
936 s32 x1, x2, x3, p; 792 s32 x1, x2, x3, p;
937 s32 b3, b6; 793 s32 b3, b6;
938 u32 b4, b7; 794 u32 b4, b7;
939 s32 oss = data->oversampling_press; 795 s32 oss = data->oversampling_press;
940 struct bmp180_calib calib; 796 struct bmp180_calib *calib = &data->calib;
941
942 ret = bmp180_read_calib(data, &calib);
943 if (ret < 0) {
944 dev_err(&data->client->dev,
945 "failed to read calibration coefficients\n");
946 return ret;
947 }
948 797
949 b6 = data->t_fine - 4000; 798 b6 = data->t_fine - 4000;
950 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11; 799 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
951 x2 = calib.AC2 * b6 >> 11; 800 x2 = calib->AC2 * b6 >> 11;
952 x3 = x1 + x2; 801 x3 = x1 + x2;
953 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4; 802 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
954 x1 = calib.AC3 * b6 >> 13; 803 x1 = calib->AC3 * b6 >> 13;
955 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16; 804 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
956 x3 = (x1 + x2 + 2) >> 2; 805 x3 = (x1 + x2 + 2) >> 2;
957 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15; 806 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
958 b7 = ((u32)adc_press - b3) * (50000 >> oss); 807 b7 = ((u32)adc_press - b3) * (50000 >> oss);
959 if (b7 < 0x80000000) 808 if (b7 < 0x80000000)
960 p = (b7 * 2) / b4; 809 p = (b7 * 2) / b4;
@@ -1001,8 +850,6 @@ static const int bmp180_oversampling_temp_avail[] = { 1 };
1001static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; 850static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
1002 851
1003static const struct bmp280_chip_info bmp180_chip_info = { 852static const struct bmp280_chip_info bmp180_chip_info = {
1004 .regmap_config = &bmp180_regmap_config,
1005
1006 .oversampling_temp_avail = bmp180_oversampling_temp_avail, 853 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
1007 .num_oversampling_temp_avail = 854 .num_oversampling_temp_avail =
1008 ARRAY_SIZE(bmp180_oversampling_temp_avail), 855 ARRAY_SIZE(bmp180_oversampling_temp_avail),
@@ -1016,40 +863,86 @@ static const struct bmp280_chip_info bmp180_chip_info = {
1016 .read_press = bmp180_read_press, 863 .read_press = bmp180_read_press,
1017}; 864};
1018 865
1019static int bmp280_probe(struct i2c_client *client, 866static irqreturn_t bmp085_eoc_irq(int irq, void *d)
1020 const struct i2c_device_id *id) 867{
868 struct bmp280_data *data = d;
869
870 complete(&data->done);
871
872 return IRQ_HANDLED;
873}
874
875static int bmp085_fetch_eoc_irq(struct device *dev,
876 const char *name,
877 int irq,
878 struct bmp280_data *data)
879{
880 unsigned long irq_trig;
881 int ret;
882
883 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
884 if (irq_trig != IRQF_TRIGGER_RISING) {
885 dev_err(dev, "non-rising trigger given for EOC interrupt, "
886 "trying to enforce it\n");
887 irq_trig = IRQF_TRIGGER_RISING;
888 }
889 ret = devm_request_threaded_irq(dev,
890 irq,
891 bmp085_eoc_irq,
892 NULL,
893 irq_trig,
894 name,
895 data);
896 if (ret) {
897 /* Bail out without IRQ but keep the driver in place */
898 dev_err(dev, "unable to request DRDY IRQ\n");
899 return 0;
900 }
901
902 data->use_eoc = true;
903 return 0;
904}
905
906int bmp280_common_probe(struct device *dev,
907 struct regmap *regmap,
908 unsigned int chip,
909 const char *name,
910 int irq)
1021{ 911{
1022 int ret; 912 int ret;
1023 struct iio_dev *indio_dev; 913 struct iio_dev *indio_dev;
1024 struct bmp280_data *data; 914 struct bmp280_data *data;
1025 unsigned int chip_id; 915 unsigned int chip_id;
916 struct gpio_desc *gpiod;
1026 917
1027 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 918 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1028 if (!indio_dev) 919 if (!indio_dev)
1029 return -ENOMEM; 920 return -ENOMEM;
1030 921
1031 data = iio_priv(indio_dev); 922 data = iio_priv(indio_dev);
1032 mutex_init(&data->lock); 923 mutex_init(&data->lock);
1033 data->client = client; 924 data->dev = dev;
1034 925
1035 indio_dev->dev.parent = &client->dev; 926 indio_dev->dev.parent = dev;
1036 indio_dev->name = id->name; 927 indio_dev->name = name;
1037 indio_dev->channels = bmp280_channels; 928 indio_dev->channels = bmp280_channels;
1038 indio_dev->info = &bmp280_info; 929 indio_dev->info = &bmp280_info;
1039 indio_dev->modes = INDIO_DIRECT_MODE; 930 indio_dev->modes = INDIO_DIRECT_MODE;
1040 931
1041 switch (id->driver_data) { 932 switch (chip) {
1042 case BMP180_CHIP_ID: 933 case BMP180_CHIP_ID:
1043 indio_dev->num_channels = 2; 934 indio_dev->num_channels = 2;
1044 data->chip_info = &bmp180_chip_info; 935 data->chip_info = &bmp180_chip_info;
1045 data->oversampling_press = ilog2(8); 936 data->oversampling_press = ilog2(8);
1046 data->oversampling_temp = ilog2(1); 937 data->oversampling_temp = ilog2(1);
938 data->start_up_time = 10;
1047 break; 939 break;
1048 case BMP280_CHIP_ID: 940 case BMP280_CHIP_ID:
1049 indio_dev->num_channels = 2; 941 indio_dev->num_channels = 2;
1050 data->chip_info = &bmp280_chip_info; 942 data->chip_info = &bmp280_chip_info;
1051 data->oversampling_press = ilog2(16); 943 data->oversampling_press = ilog2(16);
1052 data->oversampling_temp = ilog2(2); 944 data->oversampling_temp = ilog2(2);
945 data->start_up_time = 2;
1053 break; 946 break;
1054 case BME280_CHIP_ID: 947 case BME280_CHIP_ID:
1055 indio_dev->num_channels = 3; 948 indio_dev->num_channels = 3;
@@ -1057,61 +950,167 @@ static int bmp280_probe(struct i2c_client *client,
1057 data->oversampling_press = ilog2(16); 950 data->oversampling_press = ilog2(16);
1058 data->oversampling_humid = ilog2(16); 951 data->oversampling_humid = ilog2(16);
1059 data->oversampling_temp = ilog2(2); 952 data->oversampling_temp = ilog2(2);
953 data->start_up_time = 2;
1060 break; 954 break;
1061 default: 955 default:
1062 return -EINVAL; 956 return -EINVAL;
1063 } 957 }
1064 958
1065 data->regmap = devm_regmap_init_i2c(client, 959 /* Bring up regulators */
1066 data->chip_info->regmap_config); 960 data->vddd = devm_regulator_get(dev, "vddd");
1067 if (IS_ERR(data->regmap)) { 961 if (IS_ERR(data->vddd)) {
1068 dev_err(&client->dev, "failed to allocate register map\n"); 962 dev_err(dev, "failed to get VDDD regulator\n");
1069 return PTR_ERR(data->regmap); 963 return PTR_ERR(data->vddd);
964 }
965 ret = regulator_enable(data->vddd);
966 if (ret) {
967 dev_err(dev, "failed to enable VDDD regulator\n");
968 return ret;
969 }
970 data->vdda = devm_regulator_get(dev, "vdda");
971 if (IS_ERR(data->vdda)) {
972 dev_err(dev, "failed to get VDDA regulator\n");
973 ret = PTR_ERR(data->vddd);
974 goto out_disable_vddd;
975 }
976 ret = regulator_enable(data->vdda);
977 if (ret) {
978 dev_err(dev, "failed to enable VDDA regulator\n");
979 goto out_disable_vddd;
980 }
981 /* Wait to make sure we started up properly */
982 mdelay(data->start_up_time);
983
984 /* Bring chip out of reset if there is an assigned GPIO line */
985 gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
986 /* Deassert the signal */
987 if (!IS_ERR(gpiod)) {
988 dev_info(dev, "release reset\n");
989 gpiod_set_value(gpiod, 0);
1070 } 990 }
1071 991
1072 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id); 992 data->regmap = regmap;
993 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
1073 if (ret < 0) 994 if (ret < 0)
1074 return ret; 995 goto out_disable_vdda;
1075 if (chip_id != id->driver_data) { 996 if (chip_id != chip) {
1076 dev_err(&client->dev, "bad chip id. expected %lx got %x\n", 997 dev_err(dev, "bad chip id: expected %x got %x\n",
1077 id->driver_data, chip_id); 998 chip, chip_id);
1078 return -EINVAL; 999 ret = -EINVAL;
1000 goto out_disable_vdda;
1079 } 1001 }
1080 1002
1081 ret = data->chip_info->chip_config(data); 1003 ret = data->chip_info->chip_config(data);
1082 if (ret < 0) 1004 if (ret < 0)
1083 return ret; 1005 goto out_disable_vdda;
1006
1007 dev_set_drvdata(dev, indio_dev);
1008
1009 /*
1010 * The BMP085 and BMP180 has calibration in an E2PROM, read it out
1011 * at probe time. It will not change.
1012 */
1013 if (chip_id == BMP180_CHIP_ID) {
1014 ret = bmp180_read_calib(data, &data->calib);
1015 if (ret < 0) {
1016 dev_err(data->dev,
1017 "failed to read calibration coefficients\n");
1018 goto out_disable_vdda;
1019 }
1020 }
1021
1022 /*
1023 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1024 * however as it happens, the BMP085 shares the chip ID of BMP180
1025 * so we look for an IRQ if we have that.
1026 */
1027 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1028 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1029 if (ret)
1030 goto out_disable_vdda;
1031 }
1032
1033 /* Enable runtime PM */
1034 pm_runtime_get_noresume(dev);
1035 pm_runtime_set_active(dev);
1036 pm_runtime_enable(dev);
1037 /*
1038 * Set autosuspend to two orders of magnitude larger than the
1039 * start-up time.
1040 */
1041 pm_runtime_set_autosuspend_delay(dev, data->start_up_time *100);
1042 pm_runtime_use_autosuspend(dev);
1043 pm_runtime_put(dev);
1044
1045 ret = iio_device_register(indio_dev);
1046 if (ret)
1047 goto out_runtime_pm_disable;
1048
1084 1049
1085 return devm_iio_device_register(&client->dev, indio_dev); 1050 return 0;
1051
1052out_runtime_pm_disable:
1053 pm_runtime_get_sync(data->dev);
1054 pm_runtime_put_noidle(data->dev);
1055 pm_runtime_disable(data->dev);
1056out_disable_vdda:
1057 regulator_disable(data->vdda);
1058out_disable_vddd:
1059 regulator_disable(data->vddd);
1060 return ret;
1086} 1061}
1062EXPORT_SYMBOL(bmp280_common_probe);
1087 1063
1088static const struct acpi_device_id bmp280_acpi_match[] = { 1064int bmp280_common_remove(struct device *dev)
1089 {"BMP0280", BMP280_CHIP_ID }, 1065{
1090 {"BMP0180", BMP180_CHIP_ID }, 1066 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1091 {"BMP0085", BMP180_CHIP_ID }, 1067 struct bmp280_data *data = iio_priv(indio_dev);
1092 {"BME0280", BME280_CHIP_ID },
1093 { },
1094};
1095MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
1096
1097static const struct i2c_device_id bmp280_id[] = {
1098 {"bmp280", BMP280_CHIP_ID },
1099 {"bmp180", BMP180_CHIP_ID },
1100 {"bmp085", BMP180_CHIP_ID },
1101 {"bme280", BME280_CHIP_ID },
1102 { },
1103};
1104MODULE_DEVICE_TABLE(i2c, bmp280_id);
1105 1068
1106static struct i2c_driver bmp280_driver = { 1069 iio_device_unregister(indio_dev);
1107 .driver = { 1070 pm_runtime_get_sync(data->dev);
1108 .name = "bmp280", 1071 pm_runtime_put_noidle(data->dev);
1109 .acpi_match_table = ACPI_PTR(bmp280_acpi_match), 1072 pm_runtime_disable(data->dev);
1110 }, 1073 regulator_disable(data->vdda);
1111 .probe = bmp280_probe, 1074 regulator_disable(data->vddd);
1112 .id_table = bmp280_id, 1075 return 0;
1076}
1077EXPORT_SYMBOL(bmp280_common_remove);
1078
1079#ifdef CONFIG_PM
1080static int bmp280_runtime_suspend(struct device *dev)
1081{
1082 struct bmp280_data *data = dev_get_drvdata(dev);
1083 int ret;
1084
1085 ret = regulator_disable(data->vdda);
1086 if (ret)
1087 return ret;
1088 return regulator_disable(data->vddd);
1089}
1090
1091static int bmp280_runtime_resume(struct device *dev)
1092{
1093 struct bmp280_data *data = dev_get_drvdata(dev);
1094 int ret;
1095
1096 ret = regulator_enable(data->vddd);
1097 if (ret)
1098 return ret;
1099 ret = regulator_enable(data->vdda);
1100 if (ret)
1101 return ret;
1102 msleep(data->start_up_time);
1103 return data->chip_info->chip_config(data);
1104}
1105#endif /* CONFIG_PM */
1106
1107const struct dev_pm_ops bmp280_dev_pm_ops = {
1108 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1109 pm_runtime_force_resume)
1110 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
1111 bmp280_runtime_resume, NULL)
1113}; 1112};
1114module_i2c_driver(bmp280_driver); 1113EXPORT_SYMBOL(bmp280_dev_pm_ops);
1115 1114
1116MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); 1115MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1117MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor"); 1116MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
diff --git a/drivers/iio/pressure/bmp280-i2c.c b/drivers/iio/pressure/bmp280-i2c.c
new file mode 100644
index 000000000000..03742b15b72a
--- /dev/null
+++ b/drivers/iio/pressure/bmp280-i2c.c
@@ -0,0 +1,91 @@
1#include <linux/module.h>
2#include <linux/i2c.h>
3#include <linux/acpi.h>
4#include <linux/of.h>
5#include <linux/regmap.h>
6
7#include "bmp280.h"
8
9static int bmp280_i2c_probe(struct i2c_client *client,
10 const struct i2c_device_id *id)
11{
12 struct regmap *regmap;
13 const struct regmap_config *regmap_config;
14
15 switch (id->driver_data) {
16 case BMP180_CHIP_ID:
17 regmap_config = &bmp180_regmap_config;
18 break;
19 case BMP280_CHIP_ID:
20 case BME280_CHIP_ID:
21 regmap_config = &bmp280_regmap_config;
22 break;
23 default:
24 return -EINVAL;
25 }
26
27 regmap = devm_regmap_init_i2c(client, regmap_config);
28 if (IS_ERR(regmap)) {
29 dev_err(&client->dev, "failed to allocate register map\n");
30 return PTR_ERR(regmap);
31 }
32
33 return bmp280_common_probe(&client->dev,
34 regmap,
35 id->driver_data,
36 id->name,
37 client->irq);
38}
39
40static int bmp280_i2c_remove(struct i2c_client *client)
41{
42 return bmp280_common_remove(&client->dev);
43}
44
45static const struct acpi_device_id bmp280_acpi_i2c_match[] = {
46 {"BMP0280", BMP280_CHIP_ID },
47 {"BMP0180", BMP180_CHIP_ID },
48 {"BMP0085", BMP180_CHIP_ID },
49 {"BME0280", BME280_CHIP_ID },
50 { },
51};
52MODULE_DEVICE_TABLE(acpi, bmp280_acpi_i2c_match);
53
54#ifdef CONFIG_OF
55static const struct of_device_id bmp280_of_i2c_match[] = {
56 { .compatible = "bosch,bme280", .data = (void *)BME280_CHIP_ID },
57 { .compatible = "bosch,bmp280", .data = (void *)BMP280_CHIP_ID },
58 { .compatible = "bosch,bmp180", .data = (void *)BMP180_CHIP_ID },
59 { .compatible = "bosch,bmp085", .data = (void *)BMP180_CHIP_ID },
60 { },
61};
62MODULE_DEVICE_TABLE(of, bmp280_of_i2c_match);
63#else
64#define bmp280_of_i2c_match NULL
65#endif
66
67static const struct i2c_device_id bmp280_i2c_id[] = {
68 {"bmp280", BMP280_CHIP_ID },
69 {"bmp180", BMP180_CHIP_ID },
70 {"bmp085", BMP180_CHIP_ID },
71 {"bme280", BME280_CHIP_ID },
72 { },
73};
74MODULE_DEVICE_TABLE(i2c, bmp280_i2c_id);
75
76static struct i2c_driver bmp280_i2c_driver = {
77 .driver = {
78 .name = "bmp280",
79 .acpi_match_table = ACPI_PTR(bmp280_acpi_i2c_match),
80 .of_match_table = of_match_ptr(bmp280_of_i2c_match),
81 .pm = &bmp280_dev_pm_ops,
82 },
83 .probe = bmp280_i2c_probe,
84 .remove = bmp280_i2c_remove,
85 .id_table = bmp280_i2c_id,
86};
87module_i2c_driver(bmp280_i2c_driver);
88
89MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
90MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
91MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/pressure/bmp280-regmap.c b/drivers/iio/pressure/bmp280-regmap.c
new file mode 100644
index 000000000000..6807113ec09f
--- /dev/null
+++ b/drivers/iio/pressure/bmp280-regmap.c
@@ -0,0 +1,84 @@
1#include <linux/device.h>
2#include <linux/module.h>
3#include <linux/regmap.h>
4
5#include "bmp280.h"
6
7static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
8{
9 switch (reg) {
10 case BMP280_REG_CTRL_MEAS:
11 case BMP280_REG_RESET:
12 return true;
13 default:
14 return false;
15 };
16}
17
18static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
19{
20 switch (reg) {
21 case BMP180_REG_OUT_XLSB:
22 case BMP180_REG_OUT_LSB:
23 case BMP180_REG_OUT_MSB:
24 case BMP280_REG_CTRL_MEAS:
25 return true;
26 default:
27 return false;
28 }
29}
30
31const struct regmap_config bmp180_regmap_config = {
32 .reg_bits = 8,
33 .val_bits = 8,
34
35 .max_register = BMP180_REG_OUT_XLSB,
36 .cache_type = REGCACHE_RBTREE,
37
38 .writeable_reg = bmp180_is_writeable_reg,
39 .volatile_reg = bmp180_is_volatile_reg,
40};
41EXPORT_SYMBOL(bmp180_regmap_config);
42
43static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
44{
45 switch (reg) {
46 case BMP280_REG_CONFIG:
47 case BMP280_REG_CTRL_HUMIDITY:
48 case BMP280_REG_CTRL_MEAS:
49 case BMP280_REG_RESET:
50 return true;
51 default:
52 return false;
53 };
54}
55
56static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
57{
58 switch (reg) {
59 case BMP280_REG_HUMIDITY_LSB:
60 case BMP280_REG_HUMIDITY_MSB:
61 case BMP280_REG_TEMP_XLSB:
62 case BMP280_REG_TEMP_LSB:
63 case BMP280_REG_TEMP_MSB:
64 case BMP280_REG_PRESS_XLSB:
65 case BMP280_REG_PRESS_LSB:
66 case BMP280_REG_PRESS_MSB:
67 case BMP280_REG_STATUS:
68 return true;
69 default:
70 return false;
71 }
72}
73
74const struct regmap_config bmp280_regmap_config = {
75 .reg_bits = 8,
76 .val_bits = 8,
77
78 .max_register = BMP280_REG_HUMIDITY_LSB,
79 .cache_type = REGCACHE_RBTREE,
80
81 .writeable_reg = bmp280_is_writeable_reg,
82 .volatile_reg = bmp280_is_volatile_reg,
83};
84EXPORT_SYMBOL(bmp280_regmap_config);
diff --git a/drivers/iio/pressure/bmp280-spi.c b/drivers/iio/pressure/bmp280-spi.c
new file mode 100644
index 000000000000..17bc95586f9e
--- /dev/null
+++ b/drivers/iio/pressure/bmp280-spi.c
@@ -0,0 +1,125 @@
1/*
2 * SPI interface for the BMP280 driver
3 *
4 * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
5 */
6#include <linux/module.h>
7#include <linux/spi/spi.h>
8#include <linux/err.h>
9#include <linux/regmap.h>
10
11#include "bmp280.h"
12
13static int bmp280_regmap_spi_write(void *context, const void *data,
14 size_t count)
15{
16 struct device *dev = context;
17 struct spi_device *spi = to_spi_device(dev);
18 u8 buf[2];
19
20 memcpy(buf, data, 2);
21 /*
22 * The SPI register address (= full register address without bit 7) and
23 * the write command (bit7 = RW = '0')
24 */
25 buf[0] &= ~0x80;
26
27 return spi_write_then_read(spi, buf, 2, NULL, 0);
28}
29
30static int bmp280_regmap_spi_read(void *context, const void *reg,
31 size_t reg_size, void *val, size_t val_size)
32{
33 struct device *dev = context;
34 struct spi_device *spi = to_spi_device(dev);
35
36 return spi_write_then_read(spi, reg, reg_size, val, val_size);
37}
38
39static struct regmap_bus bmp280_regmap_bus = {
40 .write = bmp280_regmap_spi_write,
41 .read = bmp280_regmap_spi_read,
42 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
43 .val_format_endian_default = REGMAP_ENDIAN_BIG,
44};
45
46static int bmp280_spi_probe(struct spi_device *spi)
47{
48 const struct spi_device_id *id = spi_get_device_id(spi);
49 struct regmap *regmap;
50 const struct regmap_config *regmap_config;
51 int ret;
52
53 spi->bits_per_word = 8;
54 ret = spi_setup(spi);
55 if (ret < 0) {
56 dev_err(&spi->dev, "spi_setup failed!\n");
57 return ret;
58 }
59
60 switch (id->driver_data) {
61 case BMP180_CHIP_ID:
62 regmap_config = &bmp180_regmap_config;
63 break;
64 case BMP280_CHIP_ID:
65 case BME280_CHIP_ID:
66 regmap_config = &bmp280_regmap_config;
67 break;
68 default:
69 return -EINVAL;
70 }
71
72 regmap = devm_regmap_init(&spi->dev,
73 &bmp280_regmap_bus,
74 &spi->dev,
75 regmap_config);
76 if (IS_ERR(regmap)) {
77 dev_err(&spi->dev, "failed to allocate register map\n");
78 return PTR_ERR(regmap);
79 }
80
81 return bmp280_common_probe(&spi->dev,
82 regmap,
83 id->driver_data,
84 id->name,
85 spi->irq);
86}
87
88static int bmp280_spi_remove(struct spi_device *spi)
89{
90 return bmp280_common_remove(&spi->dev);
91}
92
93static const struct of_device_id bmp280_of_spi_match[] = {
94 { .compatible = "bosch,bmp085", },
95 { .compatible = "bosch,bmp180", },
96 { .compatible = "bosch,bmp181", },
97 { .compatible = "bosch,bmp280", },
98 { .compatible = "bosch,bme280", },
99 { },
100};
101MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
102
103static const struct spi_device_id bmp280_spi_id[] = {
104 { "bmp180", BMP180_CHIP_ID },
105 { "bmp181", BMP180_CHIP_ID },
106 { "bmp280", BMP280_CHIP_ID },
107 { "bme280", BME280_CHIP_ID },
108 { }
109};
110MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
111
112static struct spi_driver bmp280_spi_driver = {
113 .driver = {
114 .name = "bmp280",
115 .of_match_table = bmp280_of_spi_match,
116 .pm = &bmp280_dev_pm_ops,
117 },
118 .id_table = bmp280_spi_id,
119 .probe = bmp280_spi_probe,
120 .remove = bmp280_spi_remove,
121};
122module_spi_driver(bmp280_spi_driver);
123
124MODULE_DESCRIPTION("BMP280 SPI bus driver");
125MODULE_LICENSE("GPL");
diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h
new file mode 100644
index 000000000000..2c770e13be0e
--- /dev/null
+++ b/drivers/iio/pressure/bmp280.h
@@ -0,0 +1,112 @@
1#include <linux/bitops.h>
2#include <linux/device.h>
3#include <linux/regmap.h>
4
5/* BMP280 specific registers */
6#define BMP280_REG_HUMIDITY_LSB 0xFE
7#define BMP280_REG_HUMIDITY_MSB 0xFD
8#define BMP280_REG_TEMP_XLSB 0xFC
9#define BMP280_REG_TEMP_LSB 0xFB
10#define BMP280_REG_TEMP_MSB 0xFA
11#define BMP280_REG_PRESS_XLSB 0xF9
12#define BMP280_REG_PRESS_LSB 0xF8
13#define BMP280_REG_PRESS_MSB 0xF7
14
15#define BMP280_REG_CONFIG 0xF5
16#define BMP280_REG_CTRL_MEAS 0xF4
17#define BMP280_REG_STATUS 0xF3
18#define BMP280_REG_CTRL_HUMIDITY 0xF2
19
20/* Due to non linear mapping, and data sizes we can't do a bulk read */
21#define BMP280_REG_COMP_H1 0xA1
22#define BMP280_REG_COMP_H2 0xE1
23#define BMP280_REG_COMP_H3 0xE3
24#define BMP280_REG_COMP_H4 0xE4
25#define BMP280_REG_COMP_H5 0xE5
26#define BMP280_REG_COMP_H6 0xE7
27
28#define BMP280_REG_COMP_TEMP_START 0x88
29#define BMP280_COMP_TEMP_REG_COUNT 6
30
31#define BMP280_REG_COMP_PRESS_START 0x8E
32#define BMP280_COMP_PRESS_REG_COUNT 18
33
34#define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
35#define BMP280_FILTER_OFF 0
36#define BMP280_FILTER_2X BIT(2)
37#define BMP280_FILTER_4X BIT(3)
38#define BMP280_FILTER_8X (BIT(3) | BIT(2))
39#define BMP280_FILTER_16X BIT(4)
40
41#define BMP280_OSRS_HUMIDITY_MASK (BIT(2) | BIT(1) | BIT(0))
42#define BMP280_OSRS_HUMIDITIY_X(osrs_h) ((osrs_h) << 0)
43#define BMP280_OSRS_HUMIDITY_SKIP 0
44#define BMP280_OSRS_HUMIDITY_1X BMP280_OSRS_HUMIDITIY_X(1)
45#define BMP280_OSRS_HUMIDITY_2X BMP280_OSRS_HUMIDITIY_X(2)
46#define BMP280_OSRS_HUMIDITY_4X BMP280_OSRS_HUMIDITIY_X(3)
47#define BMP280_OSRS_HUMIDITY_8X BMP280_OSRS_HUMIDITIY_X(4)
48#define BMP280_OSRS_HUMIDITY_16X BMP280_OSRS_HUMIDITIY_X(5)
49
50#define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
51#define BMP280_OSRS_TEMP_SKIP 0
52#define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
53#define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
54#define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
55#define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
56#define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
57#define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
58
59#define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
60#define BMP280_OSRS_PRESS_SKIP 0
61#define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
62#define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
63#define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
64#define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
65#define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
66#define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
67
68#define BMP280_MODE_MASK (BIT(1) | BIT(0))
69#define BMP280_MODE_SLEEP 0
70#define BMP280_MODE_FORCED BIT(0)
71#define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
72
73/* BMP180 specific registers */
74#define BMP180_REG_OUT_XLSB 0xF8
75#define BMP180_REG_OUT_LSB 0xF7
76#define BMP180_REG_OUT_MSB 0xF6
77
78#define BMP180_REG_CALIB_START 0xAA
79#define BMP180_REG_CALIB_COUNT 22
80
81#define BMP180_MEAS_SCO BIT(5)
82#define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
83#define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
84#define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
85#define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
86#define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
87#define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
88
89/* BMP180 and BMP280 common registers */
90#define BMP280_REG_CTRL_MEAS 0xF4
91#define BMP280_REG_RESET 0xE0
92#define BMP280_REG_ID 0xD0
93
94#define BMP180_CHIP_ID 0x55
95#define BMP280_CHIP_ID 0x58
96#define BME280_CHIP_ID 0x60
97#define BMP280_SOFT_RESET_VAL 0xB6
98
99/* Regmap configurations */
100extern const struct regmap_config bmp180_regmap_config;
101extern const struct regmap_config bmp280_regmap_config;
102
103/* Probe called from different transports */
104int bmp280_common_probe(struct device *dev,
105 struct regmap *regmap,
106 unsigned int chip,
107 const char *name,
108 int irq);
109int bmp280_common_remove(struct device *dev);
110
111/* PM ops */
112extern const struct dev_pm_ops bmp280_dev_pm_ops;
diff --git a/drivers/iio/pressure/mpl3115.c b/drivers/iio/pressure/mpl3115.c
index 01b2e0b18878..6392d7b62841 100644
--- a/drivers/iio/pressure/mpl3115.c
+++ b/drivers/iio/pressure/mpl3115.c
@@ -171,7 +171,7 @@ static irqreturn_t mpl3115_trigger_handler(int irq, void *p)
171 mutex_unlock(&data->lock); 171 mutex_unlock(&data->lock);
172 172
173 iio_push_to_buffers_with_timestamp(indio_dev, buffer, 173 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
174 iio_get_time_ns()); 174 iio_get_time_ns(indio_dev));
175 175
176done: 176done:
177 iio_trigger_notify_done(indio_dev->trig); 177 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/pressure/ms5611_core.c b/drivers/iio/pressure/ms5611_core.c
index 76578b07bb6e..feb41f82c64a 100644
--- a/drivers/iio/pressure/ms5611_core.c
+++ b/drivers/iio/pressure/ms5611_core.c
@@ -224,7 +224,8 @@ static irqreturn_t ms5611_trigger_handler(int irq, void *p)
224 if (ret < 0) 224 if (ret < 0)
225 goto err; 225 goto err;
226 226
227 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); 227 iio_push_to_buffers_with_timestamp(indio_dev, buf,
228 iio_get_time_ns(indio_dev));
228 229
229err: 230err:
230 iio_trigger_notify_done(indio_dev->trig); 231 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/iio/pressure/st_pressure_core.c b/drivers/iio/pressure/st_pressure_core.c
index 56d8f5eb9239..55df9a75eb3a 100644
--- a/drivers/iio/pressure/st_pressure_core.c
+++ b/drivers/iio/pressure/st_pressure_core.c
@@ -28,6 +28,72 @@
28#include <linux/iio/common/st_sensors.h> 28#include <linux/iio/common/st_sensors.h>
29#include "st_pressure.h" 29#include "st_pressure.h"
30 30
31/*
32 * About determining pressure scaling factors
33 * ------------------------------------------
34 *
35 * Datasheets specify typical pressure sensitivity so that pressure is computed
36 * according to the following equation :
37 * pressure[mBar] = raw / sensitivity
38 * where :
39 * raw the 24 bits long raw sampled pressure
40 * sensitivity a scaling factor specified by the datasheet in LSB/mBar
41 *
42 * IIO ABI expects pressure to be expressed as kPascal, hence pressure should be
43 * computed according to :
44 * pressure[kPascal] = pressure[mBar] / 10
45 * = raw / (sensitivity * 10) (1)
46 *
47 * Finally, st_press_read_raw() returns pressure scaling factor as an
48 * IIO_VAL_INT_PLUS_NANO with a zero integral part and "gain" as decimal part.
49 * Therefore, from (1), "gain" becomes :
50 * gain = 10^9 / (sensitivity * 10)
51 * = 10^8 / sensitivity
52 *
53 * About determining temperature scaling factors and offsets
54 * ---------------------------------------------------------
55 *
56 * Datasheets specify typical temperature sensitivity and offset so that
57 * temperature is computed according to the following equation :
58 * temp[Celsius] = offset[Celsius] + (raw / sensitivity)
59 * where :
60 * raw the 16 bits long raw sampled temperature
61 * offset a constant specified by the datasheet in degree Celsius
62 * (sometimes zero)
63 * sensitivity a scaling factor specified by the datasheet in LSB/Celsius
64 *
65 * IIO ABI expects temperature to be expressed as milli degree Celsius such as
66 * user space should compute temperature according to :
67 * temp[mCelsius] = temp[Celsius] * 10^3
68 * = (offset[Celsius] + (raw / sensitivity)) * 10^3
69 * = ((offset[Celsius] * sensitivity) + raw) *
70 * (10^3 / sensitivity) (2)
71 *
72 * IIO ABI expects user space to apply offset and scaling factors to raw samples
73 * according to :
74 * temp[mCelsius] = (OFFSET + raw) * SCALE
75 * where :
76 * OFFSET an arbitrary constant exposed by device
77 * SCALE an arbitrary scaling factor exposed by device
78 *
79 * Matching OFFSET and SCALE with members of (2) gives :
80 * OFFSET = offset[Celsius] * sensitivity (3)
81 * SCALE = 10^3 / sensitivity (4)
82 *
83 * st_press_read_raw() returns temperature scaling factor as an
84 * IIO_VAL_FRACTIONAL with a 10^3 numerator and "gain2" as denominator.
85 * Therefore, from (3), "gain2" becomes :
86 * gain2 = sensitivity
87 *
88 * When declared within channel, i.e. for a non zero specified offset,
89 * st_press_read_raw() will return the latter as an IIO_VAL_FRACTIONAL such as :
90 * numerator = OFFSET * 10^3
91 * denominator = 10^3
92 * giving from (4):
93 * numerator = offset[Celsius] * 10^3 * sensitivity
94 * = offset[mCelsius] * gain2
95 */
96
31#define MCELSIUS_PER_CELSIUS 1000 97#define MCELSIUS_PER_CELSIUS 1000
32 98
33/* Default pressure sensitivity */ 99/* Default pressure sensitivity */
@@ -39,8 +105,6 @@
39#define ST_PRESS_LSB_PER_CELSIUS 480UL 105#define ST_PRESS_LSB_PER_CELSIUS 480UL
40#define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL 106#define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
41 107
42#define ST_PRESS_NUMBER_DATA_CHANNELS 1
43
44/* FULLSCALE */ 108/* FULLSCALE */
45#define ST_PRESS_FS_AVL_1100MB 1100 109#define ST_PRESS_FS_AVL_1100MB 1100
46#define ST_PRESS_FS_AVL_1260MB 1260 110#define ST_PRESS_FS_AVL_1260MB 1260
@@ -48,7 +112,11 @@
48#define ST_PRESS_1_OUT_XL_ADDR 0x28 112#define ST_PRESS_1_OUT_XL_ADDR 0x28
49#define ST_TEMP_1_OUT_L_ADDR 0x2b 113#define ST_TEMP_1_OUT_L_ADDR 0x2b
50 114
51/* CUSTOM VALUES FOR LPS331AP SENSOR */ 115/*
116 * CUSTOM VALUES FOR LPS331AP SENSOR
117 * See LPS331AP datasheet:
118 * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
119 */
52#define ST_PRESS_LPS331AP_WAI_EXP 0xbb 120#define ST_PRESS_LPS331AP_WAI_EXP 0xbb
53#define ST_PRESS_LPS331AP_ODR_ADDR 0x20 121#define ST_PRESS_LPS331AP_ODR_ADDR 0x20
54#define ST_PRESS_LPS331AP_ODR_MASK 0x70 122#define ST_PRESS_LPS331AP_ODR_MASK 0x70
@@ -71,7 +139,9 @@
71#define ST_PRESS_LPS331AP_OD_IRQ_MASK 0x40 139#define ST_PRESS_LPS331AP_OD_IRQ_MASK 0x40
72#define ST_PRESS_LPS331AP_MULTIREAD_BIT true 140#define ST_PRESS_LPS331AP_MULTIREAD_BIT true
73 141
74/* CUSTOM VALUES FOR LPS001WP SENSOR */ 142/*
143 * CUSTOM VALUES FOR THE OBSOLETE LPS001WP SENSOR
144 */
75 145
76/* LPS001WP pressure resolution */ 146/* LPS001WP pressure resolution */
77#define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL 147#define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
@@ -94,7 +164,11 @@
94#define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28 164#define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
95#define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a 165#define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
96 166
97/* CUSTOM VALUES FOR LPS25H SENSOR */ 167/*
168 * CUSTOM VALUES FOR LPS25H SENSOR
169 * See LPS25H datasheet:
170 * http://www2.st.com/resource/en/datasheet/lps25h.pdf
171 */
98#define ST_PRESS_LPS25H_WAI_EXP 0xbd 172#define ST_PRESS_LPS25H_WAI_EXP 0xbd
99#define ST_PRESS_LPS25H_ODR_ADDR 0x20 173#define ST_PRESS_LPS25H_ODR_ADDR 0x20
100#define ST_PRESS_LPS25H_ODR_MASK 0x70 174#define ST_PRESS_LPS25H_ODR_MASK 0x70
@@ -117,7 +191,15 @@
117#define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28 191#define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
118#define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b 192#define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
119 193
120/* CUSTOM VALUES FOR LPS22HB SENSOR */ 194/*
195 * CUSTOM VALUES FOR LPS22HB SENSOR
196 * See LPS22HB datasheet:
197 * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
198 */
199
200/* LPS22HB temperature sensitivity */
201#define ST_PRESS_LPS22HB_LSB_PER_CELSIUS 100UL
202
121#define ST_PRESS_LPS22HB_WAI_EXP 0xb1 203#define ST_PRESS_LPS22HB_WAI_EXP 0xb1
122#define ST_PRESS_LPS22HB_ODR_ADDR 0x10 204#define ST_PRESS_LPS22HB_ODR_ADDR 0x10
123#define ST_PRESS_LPS22HB_ODR_MASK 0x70 205#define ST_PRESS_LPS22HB_ODR_MASK 0x70
@@ -135,29 +217,28 @@
135#define ST_PRESS_LPS22HB_DRDY_IRQ_INT2_MASK 0x08 217#define ST_PRESS_LPS22HB_DRDY_IRQ_INT2_MASK 0x08
136#define ST_PRESS_LPS22HB_IHL_IRQ_ADDR 0x12 218#define ST_PRESS_LPS22HB_IHL_IRQ_ADDR 0x12
137#define ST_PRESS_LPS22HB_IHL_IRQ_MASK 0x80 219#define ST_PRESS_LPS22HB_IHL_IRQ_MASK 0x80
220#define ST_PRESS_LPS22HB_OD_IRQ_ADDR 0x12
221#define ST_PRESS_LPS22HB_OD_IRQ_MASK 0x40
138#define ST_PRESS_LPS22HB_MULTIREAD_BIT true 222#define ST_PRESS_LPS22HB_MULTIREAD_BIT true
139 223
140static const struct iio_chan_spec st_press_1_channels[] = { 224static const struct iio_chan_spec st_press_1_channels[] = {
141 { 225 {
142 .type = IIO_PRESSURE, 226 .type = IIO_PRESSURE,
143 .channel2 = IIO_NO_MOD,
144 .address = ST_PRESS_1_OUT_XL_ADDR, 227 .address = ST_PRESS_1_OUT_XL_ADDR,
145 .scan_index = ST_SENSORS_SCAN_X, 228 .scan_index = 0,
146 .scan_type = { 229 .scan_type = {
147 .sign = 'u', 230 .sign = 'u',
148 .realbits = 24, 231 .realbits = 24,
149 .storagebits = 24, 232 .storagebits = 32,
150 .endianness = IIO_LE, 233 .endianness = IIO_LE,
151 }, 234 },
152 .info_mask_separate = 235 .info_mask_separate =
153 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 236 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
154 .modified = 0,
155 }, 237 },
156 { 238 {
157 .type = IIO_TEMP, 239 .type = IIO_TEMP,
158 .channel2 = IIO_NO_MOD,
159 .address = ST_TEMP_1_OUT_L_ADDR, 240 .address = ST_TEMP_1_OUT_L_ADDR,
160 .scan_index = -1, 241 .scan_index = 1,
161 .scan_type = { 242 .scan_type = {
162 .sign = 'u', 243 .sign = 'u',
163 .realbits = 16, 244 .realbits = 16,
@@ -168,17 +249,15 @@ static const struct iio_chan_spec st_press_1_channels[] = {
168 BIT(IIO_CHAN_INFO_RAW) | 249 BIT(IIO_CHAN_INFO_RAW) |
169 BIT(IIO_CHAN_INFO_SCALE) | 250 BIT(IIO_CHAN_INFO_SCALE) |
170 BIT(IIO_CHAN_INFO_OFFSET), 251 BIT(IIO_CHAN_INFO_OFFSET),
171 .modified = 0,
172 }, 252 },
173 IIO_CHAN_SOFT_TIMESTAMP(1) 253 IIO_CHAN_SOFT_TIMESTAMP(2)
174}; 254};
175 255
176static const struct iio_chan_spec st_press_lps001wp_channels[] = { 256static const struct iio_chan_spec st_press_lps001wp_channels[] = {
177 { 257 {
178 .type = IIO_PRESSURE, 258 .type = IIO_PRESSURE,
179 .channel2 = IIO_NO_MOD,
180 .address = ST_PRESS_LPS001WP_OUT_L_ADDR, 259 .address = ST_PRESS_LPS001WP_OUT_L_ADDR,
181 .scan_index = ST_SENSORS_SCAN_X, 260 .scan_index = 0,
182 .scan_type = { 261 .scan_type = {
183 .sign = 'u', 262 .sign = 'u',
184 .realbits = 16, 263 .realbits = 16,
@@ -188,13 +267,11 @@ static const struct iio_chan_spec st_press_lps001wp_channels[] = {
188 .info_mask_separate = 267 .info_mask_separate =
189 BIT(IIO_CHAN_INFO_RAW) | 268 BIT(IIO_CHAN_INFO_RAW) |
190 BIT(IIO_CHAN_INFO_SCALE), 269 BIT(IIO_CHAN_INFO_SCALE),
191 .modified = 0,
192 }, 270 },
193 { 271 {
194 .type = IIO_TEMP, 272 .type = IIO_TEMP,
195 .channel2 = IIO_NO_MOD,
196 .address = ST_TEMP_LPS001WP_OUT_L_ADDR, 273 .address = ST_TEMP_LPS001WP_OUT_L_ADDR,
197 .scan_index = -1, 274 .scan_index = 1,
198 .scan_type = { 275 .scan_type = {
199 .sign = 'u', 276 .sign = 'u',
200 .realbits = 16, 277 .realbits = 16,
@@ -204,30 +281,42 @@ static const struct iio_chan_spec st_press_lps001wp_channels[] = {
204 .info_mask_separate = 281 .info_mask_separate =
205 BIT(IIO_CHAN_INFO_RAW) | 282 BIT(IIO_CHAN_INFO_RAW) |
206 BIT(IIO_CHAN_INFO_SCALE), 283 BIT(IIO_CHAN_INFO_SCALE),
207 .modified = 0,
208 }, 284 },
209 IIO_CHAN_SOFT_TIMESTAMP(1) 285 IIO_CHAN_SOFT_TIMESTAMP(2)
210}; 286};
211 287
212static const struct iio_chan_spec st_press_lps22hb_channels[] = { 288static const struct iio_chan_spec st_press_lps22hb_channels[] = {
213 { 289 {
214 .type = IIO_PRESSURE, 290 .type = IIO_PRESSURE,
215 .channel2 = IIO_NO_MOD,
216 .address = ST_PRESS_1_OUT_XL_ADDR, 291 .address = ST_PRESS_1_OUT_XL_ADDR,
217 .scan_index = 0, 292 .scan_index = 0,
218 .scan_type = { 293 .scan_type = {
219 .sign = 'u', 294 .sign = 'u',
220 .realbits = 24, 295 .realbits = 24,
221 .storagebits = 24, 296 .storagebits = 32,
222 .endianness = IIO_LE, 297 .endianness = IIO_LE,
223 }, 298 },
224 .info_mask_separate = 299 .info_mask_separate =
225 BIT(IIO_CHAN_INFO_RAW) | 300 BIT(IIO_CHAN_INFO_RAW) |
226 BIT(IIO_CHAN_INFO_SCALE), 301 BIT(IIO_CHAN_INFO_SCALE),
227 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 302 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
228 .modified = 0,
229 }, 303 },
230 IIO_CHAN_SOFT_TIMESTAMP(1) 304 {
305 .type = IIO_TEMP,
306 .address = ST_TEMP_1_OUT_L_ADDR,
307 .scan_index = 1,
308 .scan_type = {
309 .sign = 's',
310 .realbits = 16,
311 .storagebits = 16,
312 .endianness = IIO_LE,
313 },
314 .info_mask_separate =
315 BIT(IIO_CHAN_INFO_RAW) |
316 BIT(IIO_CHAN_INFO_SCALE),
317 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
318 },
319 IIO_CHAN_SOFT_TIMESTAMP(2)
231}; 320};
232 321
233static const struct st_sensor_settings st_press_sensors_settings[] = { 322static const struct st_sensor_settings st_press_sensors_settings[] = {
@@ -413,9 +502,14 @@ static const struct st_sensor_settings st_press_sensors_settings[] = {
413 }, 502 },
414 .fs = { 503 .fs = {
415 .fs_avl = { 504 .fs_avl = {
505 /*
506 * Pressure and temperature sensitivity values
507 * as defined in table 3 of LPS22HB datasheet.
508 */
416 [0] = { 509 [0] = {
417 .num = ST_PRESS_FS_AVL_1260MB, 510 .num = ST_PRESS_FS_AVL_1260MB,
418 .gain = ST_PRESS_KPASCAL_NANO_SCALE, 511 .gain = ST_PRESS_KPASCAL_NANO_SCALE,
512 .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
419 }, 513 },
420 }, 514 },
421 }, 515 },
@@ -429,6 +523,9 @@ static const struct st_sensor_settings st_press_sensors_settings[] = {
429 .mask_int2 = ST_PRESS_LPS22HB_DRDY_IRQ_INT2_MASK, 523 .mask_int2 = ST_PRESS_LPS22HB_DRDY_IRQ_INT2_MASK,
430 .addr_ihl = ST_PRESS_LPS22HB_IHL_IRQ_ADDR, 524 .addr_ihl = ST_PRESS_LPS22HB_IHL_IRQ_ADDR,
431 .mask_ihl = ST_PRESS_LPS22HB_IHL_IRQ_MASK, 525 .mask_ihl = ST_PRESS_LPS22HB_IHL_IRQ_MASK,
526 .addr_od = ST_PRESS_LPS22HB_OD_IRQ_ADDR,
527 .mask_od = ST_PRESS_LPS22HB_OD_IRQ_MASK,
528 .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
432 }, 529 },
433 .multi_read_bit = ST_PRESS_LPS22HB_MULTIREAD_BIT, 530 .multi_read_bit = ST_PRESS_LPS22HB_MULTIREAD_BIT,
434 }, 531 },
@@ -558,7 +655,13 @@ int st_press_common_probe(struct iio_dev *indio_dev)
558 if (err < 0) 655 if (err < 0)
559 goto st_press_power_off; 656 goto st_press_power_off;
560 657
561 press_data->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS; 658 /*
659 * Skip timestamping channel while declaring available channels to
660 * common st_sensor layer. Look at st_sensors_get_buffer_element() to
661 * see how timestamps are explicitly pushed as last samples block
662 * element.
663 */
664 press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
562 press_data->multiread_bit = press_data->sensor_settings->multi_read_bit; 665 press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
563 indio_dev->channels = press_data->sensor_settings->ch; 666 indio_dev->channels = press_data->sensor_settings->ch;
564 indio_dev->num_channels = press_data->sensor_settings->num_ch; 667 indio_dev->num_channels = press_data->sensor_settings->num_ch;
diff --git a/drivers/iio/proximity/pulsedlight-lidar-lite-v2.c b/drivers/iio/proximity/pulsedlight-lidar-lite-v2.c
index c0b0e82abf94..3141c3c161bb 100644
--- a/drivers/iio/proximity/pulsedlight-lidar-lite-v2.c
+++ b/drivers/iio/proximity/pulsedlight-lidar-lite-v2.c
@@ -238,7 +238,7 @@ static irqreturn_t lidar_trigger_handler(int irq, void *private)
238 ret = lidar_get_measurement(data, data->buffer); 238 ret = lidar_get_measurement(data, data->buffer);
239 if (!ret) { 239 if (!ret) {
240 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 240 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
241 iio_get_time_ns()); 241 iio_get_time_ns(indio_dev));
242 } else if (ret != -EINVAL) { 242 } else if (ret != -EINVAL) {
243 dev_err(&data->client->dev, "cannot read LIDAR measurement"); 243 dev_err(&data->client->dev, "cannot read LIDAR measurement");
244 } 244 }
diff --git a/drivers/iio/proximity/sx9500.c b/drivers/iio/proximity/sx9500.c
index 66cd09a18786..1d74b3aafeed 100644
--- a/drivers/iio/proximity/sx9500.c
+++ b/drivers/iio/proximity/sx9500.c
@@ -492,7 +492,7 @@ static void sx9500_push_events(struct iio_dev *indio_dev)
492 dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; 492 dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
493 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan, 493 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
494 IIO_EV_TYPE_THRESH, dir); 494 IIO_EV_TYPE_THRESH, dir);
495 iio_push_event(indio_dev, ev, iio_get_time_ns()); 495 iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
496 data->prox_stat[chan] = new_prox; 496 data->prox_stat[chan] = new_prox;
497 } 497 }
498} 498}
@@ -669,7 +669,7 @@ static irqreturn_t sx9500_trigger_handler(int irq, void *private)
669 } 669 }
670 670
671 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 671 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
672 iio_get_time_ns()); 672 iio_get_time_ns(indio_dev));
673 673
674out: 674out:
675 mutex_unlock(&data->mutex); 675 mutex_unlock(&data->mutex);
diff --git a/drivers/staging/iio/accel/Kconfig b/drivers/staging/iio/accel/Kconfig
index f066aa30f0ac..1c994b57c7d2 100644
--- a/drivers/staging/iio/accel/Kconfig
+++ b/drivers/staging/iio/accel/Kconfig
@@ -51,20 +51,6 @@ config ADIS16240
51 To compile this driver as a module, say M here: the module will be 51 To compile this driver as a module, say M here: the module will be
52 called adis16240. 52 called adis16240.
53 53
54config LIS3L02DQ
55 tristate "ST Microelectronics LIS3L02DQ Accelerometer Driver"
56 depends on SPI
57 select IIO_TRIGGER if IIO_BUFFER
58 depends on !IIO_BUFFER || IIO_KFIFO_BUF
59 depends on GPIOLIB || COMPILE_TEST
60 help
61 Say Y here to build SPI support for the ST microelectronics
62 accelerometer. The driver supplies direct access via sysfs files
63 and an event interface via a character device.
64
65 To compile this driver as a module, say M here: the module will be
66 called lis3l02dq.
67
68config SCA3000 54config SCA3000
69 depends on IIO_BUFFER 55 depends on IIO_BUFFER
70 depends on SPI 56 depends on SPI
diff --git a/drivers/staging/iio/accel/Makefile b/drivers/staging/iio/accel/Makefile
index 415329c96f0c..1810a434a755 100644
--- a/drivers/staging/iio/accel/Makefile
+++ b/drivers/staging/iio/accel/Makefile
@@ -14,9 +14,5 @@ obj-$(CONFIG_ADIS16209) += adis16209.o
14adis16240-y := adis16240_core.o 14adis16240-y := adis16240_core.o
15obj-$(CONFIG_ADIS16240) += adis16240.o 15obj-$(CONFIG_ADIS16240) += adis16240.o
16 16
17lis3l02dq-y := lis3l02dq_core.o
18lis3l02dq-$(CONFIG_IIO_BUFFER) += lis3l02dq_ring.o
19obj-$(CONFIG_LIS3L02DQ) += lis3l02dq.o
20
21sca3000-y := sca3000_core.o sca3000_ring.o 17sca3000-y := sca3000_core.o sca3000_ring.o
22obj-$(CONFIG_SCA3000) += sca3000.o 18obj-$(CONFIG_SCA3000) += sca3000.o
diff --git a/drivers/staging/iio/accel/lis3l02dq.h b/drivers/staging/iio/accel/lis3l02dq.h
deleted file mode 100644
index 6bd3d4d5bc9d..000000000000
--- a/drivers/staging/iio/accel/lis3l02dq.h
+++ /dev/null
@@ -1,217 +0,0 @@
1/*
2 * LISL02DQ.h -- support STMicroelectronics LISD02DQ
3 * 3d 2g Linear Accelerometers via SPI
4 *
5 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
6 *
7 * Loosely based upon tle62x0.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#ifndef SPI_LIS3L02DQ_H_
15#define SPI_LIS3L02DQ_H_
16#define LIS3L02DQ_READ_REG(a) ((a) | 0x80)
17#define LIS3L02DQ_WRITE_REG(a) a
18
19/* Calibration parameters */
20#define LIS3L02DQ_REG_OFFSET_X_ADDR 0x16
21#define LIS3L02DQ_REG_OFFSET_Y_ADDR 0x17
22#define LIS3L02DQ_REG_OFFSET_Z_ADDR 0x18
23
24#define LIS3L02DQ_REG_GAIN_X_ADDR 0x19
25#define LIS3L02DQ_REG_GAIN_Y_ADDR 0x1A
26#define LIS3L02DQ_REG_GAIN_Z_ADDR 0x1B
27
28/* Control Register (1 of 2) */
29#define LIS3L02DQ_REG_CTRL_1_ADDR 0x20
30/* Power ctrl - either bit set corresponds to on*/
31#define LIS3L02DQ_REG_CTRL_1_PD_ON 0xC0
32
33/* Decimation Factor */
34#define LIS3L02DQ_DEC_MASK 0x30
35#define LIS3L02DQ_REG_CTRL_1_DF_128 0x00
36#define LIS3L02DQ_REG_CTRL_1_DF_64 0x10
37#define LIS3L02DQ_REG_CTRL_1_DF_32 0x20
38#define LIS3L02DQ_REG_CTRL_1_DF_8 (0x10 | 0x20)
39
40/* Self Test Enable */
41#define LIS3L02DQ_REG_CTRL_1_SELF_TEST_ON 0x08
42
43/* Axes enable ctrls */
44#define LIS3L02DQ_REG_CTRL_1_AXES_Z_ENABLE 0x04
45#define LIS3L02DQ_REG_CTRL_1_AXES_Y_ENABLE 0x02
46#define LIS3L02DQ_REG_CTRL_1_AXES_X_ENABLE 0x01
47
48/* Control Register (2 of 2) */
49#define LIS3L02DQ_REG_CTRL_2_ADDR 0x21
50
51/* Block Data Update only after MSB and LSB read */
52#define LIS3L02DQ_REG_CTRL_2_BLOCK_UPDATE 0x40
53
54/* Set to big endian output */
55#define LIS3L02DQ_REG_CTRL_2_BIG_ENDIAN 0x20
56
57/* Reboot memory content */
58#define LIS3L02DQ_REG_CTRL_2_REBOOT_MEMORY 0x10
59
60/* Interrupt Enable - applies data ready to the RDY pad */
61#define LIS3L02DQ_REG_CTRL_2_ENABLE_INTERRUPT 0x08
62
63/* Enable Data Ready Generation - relationship with previous unclear in docs */
64#define LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION 0x04
65
66/* SPI 3 wire mode */
67#define LIS3L02DQ_REG_CTRL_2_THREE_WIRE_SPI_MODE 0x02
68
69/* Data alignment, default is 12 bit right justified
70 * - option for 16 bit left justified
71 */
72#define LIS3L02DQ_REG_CTRL_2_DATA_ALIGNMENT_16_BIT_LEFT_JUSTIFIED 0x01
73
74/* Interrupt related stuff */
75#define LIS3L02DQ_REG_WAKE_UP_CFG_ADDR 0x23
76
77/* Switch from or combination of conditions to and */
78#define LIS3L02DQ_REG_WAKE_UP_CFG_BOOLEAN_AND 0x80
79
80/* Latch interrupt request,
81 * if on ack must be given by reading the ack register
82 */
83#define LIS3L02DQ_REG_WAKE_UP_CFG_LATCH_SRC 0x40
84
85/* Z Interrupt on High (above threshold) */
86#define LIS3L02DQ_REG_WAKE_UP_CFG_INTERRUPT_Z_HIGH 0x20
87/* Z Interrupt on Low */
88#define LIS3L02DQ_REG_WAKE_UP_CFG_INTERRUPT_Z_LOW 0x10
89/* Y Interrupt on High */
90#define LIS3L02DQ_REG_WAKE_UP_CFG_INTERRUPT_Y_HIGH 0x08
91/* Y Interrupt on Low */
92#define LIS3L02DQ_REG_WAKE_UP_CFG_INTERRUPT_Y_LOW 0x04
93/* X Interrupt on High */
94#define LIS3L02DQ_REG_WAKE_UP_CFG_INTERRUPT_X_HIGH 0x02
95/* X Interrupt on Low */
96#define LIS3L02DQ_REG_WAKE_UP_CFG_INTERRUPT_X_LOW 0x01
97
98/* Register that gives description of what caused interrupt
99 * - latched if set in CFG_ADDRES
100 */
101#define LIS3L02DQ_REG_WAKE_UP_SRC_ADDR 0x24
102/* top bit ignored */
103/* Interrupt Active */
104#define LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_ACTIVATED 0x40
105/* Interupts that have been triggered */
106#define LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Z_HIGH 0x20
107#define LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Z_LOW 0x10
108#define LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Y_HIGH 0x08
109#define LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Y_LOW 0x04
110#define LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_X_HIGH 0x02
111#define LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_X_LOW 0x01
112
113#define LIS3L02DQ_REG_WAKE_UP_ACK_ADDR 0x25
114
115/* Status register */
116#define LIS3L02DQ_REG_STATUS_ADDR 0x27
117/* XYZ axis data overrun - first is all overrun? */
118#define LIS3L02DQ_REG_STATUS_XYZ_OVERRUN 0x80
119#define LIS3L02DQ_REG_STATUS_Z_OVERRUN 0x40
120#define LIS3L02DQ_REG_STATUS_Y_OVERRUN 0x20
121#define LIS3L02DQ_REG_STATUS_X_OVERRUN 0x10
122/* XYZ new data available - first is all 3 available? */
123#define LIS3L02DQ_REG_STATUS_XYZ_NEW_DATA 0x08
124#define LIS3L02DQ_REG_STATUS_Z_NEW_DATA 0x04
125#define LIS3L02DQ_REG_STATUS_Y_NEW_DATA 0x02
126#define LIS3L02DQ_REG_STATUS_X_NEW_DATA 0x01
127
128/* The accelerometer readings - low and high bytes.
129 * Form of high byte dependent on justification set in ctrl reg
130 */
131#define LIS3L02DQ_REG_OUT_X_L_ADDR 0x28
132#define LIS3L02DQ_REG_OUT_X_H_ADDR 0x29
133#define LIS3L02DQ_REG_OUT_Y_L_ADDR 0x2A
134#define LIS3L02DQ_REG_OUT_Y_H_ADDR 0x2B
135#define LIS3L02DQ_REG_OUT_Z_L_ADDR 0x2C
136#define LIS3L02DQ_REG_OUT_Z_H_ADDR 0x2D
137
138/* Threshold values for all axes and both above and below thresholds
139 * - i.e. there is only one value
140 */
141#define LIS3L02DQ_REG_THS_L_ADDR 0x2E
142#define LIS3L02DQ_REG_THS_H_ADDR 0x2F
143
144#define LIS3L02DQ_DEFAULT_CTRL1 (LIS3L02DQ_REG_CTRL_1_PD_ON \
145 | LIS3L02DQ_REG_CTRL_1_AXES_Z_ENABLE \
146 | LIS3L02DQ_REG_CTRL_1_AXES_Y_ENABLE \
147 | LIS3L02DQ_REG_CTRL_1_AXES_X_ENABLE \
148 | LIS3L02DQ_REG_CTRL_1_DF_128)
149
150#define LIS3L02DQ_DEFAULT_CTRL2 0
151
152#define LIS3L02DQ_MAX_TX 12
153#define LIS3L02DQ_MAX_RX 12
154/**
155 * struct lis3l02dq_state - device instance specific data
156 * @us: actual spi_device
157 * @trig: data ready trigger registered with iio
158 * @buf_lock: mutex to protect tx and rx
159 * @tx: transmit buffer
160 * @rx: receive buffer
161 **/
162struct lis3l02dq_state {
163 struct spi_device *us;
164 struct iio_trigger *trig;
165 struct mutex buf_lock;
166 int gpio;
167 bool trigger_on;
168
169 u8 tx[LIS3L02DQ_MAX_RX] ____cacheline_aligned;
170 u8 rx[LIS3L02DQ_MAX_RX] ____cacheline_aligned;
171};
172
173int lis3l02dq_spi_read_reg_8(struct iio_dev *indio_dev,
174 u8 reg_address,
175 u8 *val);
176
177int lis3l02dq_spi_write_reg_8(struct iio_dev *indio_dev,
178 u8 reg_address,
179 u8 val);
180
181int lis3l02dq_disable_all_events(struct iio_dev *indio_dev);
182
183#ifdef CONFIG_IIO_BUFFER
184/* At the moment triggers are only used for buffer
185 * filling. This may change!
186 */
187void lis3l02dq_remove_trigger(struct iio_dev *indio_dev);
188int lis3l02dq_probe_trigger(struct iio_dev *indio_dev);
189
190int lis3l02dq_configure_buffer(struct iio_dev *indio_dev);
191void lis3l02dq_unconfigure_buffer(struct iio_dev *indio_dev);
192
193irqreturn_t lis3l02dq_data_rdy_trig_poll(int irq, void *private);
194#define lis3l02dq_th lis3l02dq_data_rdy_trig_poll
195
196#else /* CONFIG_IIO_BUFFER */
197#define lis3l02dq_th lis3l02dq_nobuffer
198
199static inline void lis3l02dq_remove_trigger(struct iio_dev *indio_dev)
200{
201}
202
203static inline int lis3l02dq_probe_trigger(struct iio_dev *indio_dev)
204{
205 return 0;
206}
207
208static int lis3l02dq_configure_buffer(struct iio_dev *indio_dev)
209{
210 return 0;
211}
212
213static inline void lis3l02dq_unconfigure_buffer(struct iio_dev *indio_dev)
214{
215}
216#endif /* CONFIG_IIO_BUFFER */
217#endif /* SPI_LIS3L02DQ_H_ */
diff --git a/drivers/staging/iio/accel/lis3l02dq_core.c b/drivers/staging/iio/accel/lis3l02dq_core.c
deleted file mode 100644
index 7a6fed3f2d3f..000000000000
--- a/drivers/staging/iio/accel/lis3l02dq_core.c
+++ /dev/null
@@ -1,814 +0,0 @@
1/*
2 * lis3l02dq.c support STMicroelectronics LISD02DQ
3 * 3d 2g Linear Accelerometers via SPI
4 *
5 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Settings:
12 * 16 bit left justified mode used.
13 */
14
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/gpio.h>
18#include <linux/of_gpio.h>
19#include <linux/mutex.h>
20#include <linux/device.h>
21#include <linux/kernel.h>
22#include <linux/spi/spi.h>
23#include <linux/slab.h>
24#include <linux/sysfs.h>
25#include <linux/module.h>
26
27#include <linux/iio/iio.h>
28#include <linux/iio/sysfs.h>
29#include <linux/iio/events.h>
30#include <linux/iio/buffer.h>
31
32#include "lis3l02dq.h"
33
34/* At the moment the spi framework doesn't allow global setting of cs_change.
35 * It's in the likely to be added comment at the top of spi.h.
36 * This means that use cannot be made of spi_write etc.
37 */
38/* direct copy of the irq_default_primary_handler */
39#ifndef CONFIG_IIO_BUFFER
40static irqreturn_t lis3l02dq_nobuffer(int irq, void *private)
41{
42 return IRQ_WAKE_THREAD;
43}
44#endif
45
46/**
47 * lis3l02dq_spi_read_reg_8() - read single byte from a single register
48 * @indio_dev: iio_dev for this actual device
49 * @reg_address: the address of the register to be read
50 * @val: pass back the resulting value
51 **/
52int lis3l02dq_spi_read_reg_8(struct iio_dev *indio_dev,
53 u8 reg_address, u8 *val)
54{
55 struct lis3l02dq_state *st = iio_priv(indio_dev);
56 int ret;
57 struct spi_transfer xfer = {
58 .tx_buf = st->tx,
59 .rx_buf = st->rx,
60 .bits_per_word = 8,
61 .len = 2,
62 };
63
64 mutex_lock(&st->buf_lock);
65 st->tx[0] = LIS3L02DQ_READ_REG(reg_address);
66 st->tx[1] = 0;
67
68 ret = spi_sync_transfer(st->us, &xfer, 1);
69 *val = st->rx[1];
70 mutex_unlock(&st->buf_lock);
71
72 return ret;
73}
74
75/**
76 * lis3l02dq_spi_write_reg_8() - write single byte to a register
77 * @indio_dev: iio_dev for this device
78 * @reg_address: the address of the register to be written
79 * @val: the value to write
80 **/
81int lis3l02dq_spi_write_reg_8(struct iio_dev *indio_dev,
82 u8 reg_address,
83 u8 val)
84{
85 int ret;
86 struct lis3l02dq_state *st = iio_priv(indio_dev);
87
88 mutex_lock(&st->buf_lock);
89 st->tx[0] = LIS3L02DQ_WRITE_REG(reg_address);
90 st->tx[1] = val;
91 ret = spi_write(st->us, st->tx, 2);
92 mutex_unlock(&st->buf_lock);
93
94 return ret;
95}
96
97/**
98 * lisl302dq_spi_write_reg_s16() - write 2 bytes to a pair of registers
99 * @indio_dev: iio_dev for this device
100 * @lower_reg_address: the address of the lower of the two registers.
101 * Second register is assumed to have address one greater.
102 * @value: value to be written
103 **/
104static int lis3l02dq_spi_write_reg_s16(struct iio_dev *indio_dev,
105 u8 lower_reg_address,
106 s16 value)
107{
108 int ret;
109 struct lis3l02dq_state *st = iio_priv(indio_dev);
110 struct spi_transfer xfers[] = { {
111 .tx_buf = st->tx,
112 .bits_per_word = 8,
113 .len = 2,
114 .cs_change = 1,
115 }, {
116 .tx_buf = st->tx + 2,
117 .bits_per_word = 8,
118 .len = 2,
119 },
120 };
121
122 mutex_lock(&st->buf_lock);
123 st->tx[0] = LIS3L02DQ_WRITE_REG(lower_reg_address);
124 st->tx[1] = value & 0xFF;
125 st->tx[2] = LIS3L02DQ_WRITE_REG(lower_reg_address + 1);
126 st->tx[3] = (value >> 8) & 0xFF;
127
128 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
129 mutex_unlock(&st->buf_lock);
130
131 return ret;
132}
133
134static int lis3l02dq_read_reg_s16(struct iio_dev *indio_dev,
135 u8 lower_reg_address,
136 int *val)
137{
138 struct lis3l02dq_state *st = iio_priv(indio_dev);
139 int ret;
140 s16 tempval;
141 struct spi_transfer xfers[] = { {
142 .tx_buf = st->tx,
143 .rx_buf = st->rx,
144 .bits_per_word = 8,
145 .len = 2,
146 .cs_change = 1,
147 }, {
148 .tx_buf = st->tx + 2,
149 .rx_buf = st->rx + 2,
150 .bits_per_word = 8,
151 .len = 2,
152 },
153 };
154
155 mutex_lock(&st->buf_lock);
156 st->tx[0] = LIS3L02DQ_READ_REG(lower_reg_address);
157 st->tx[1] = 0;
158 st->tx[2] = LIS3L02DQ_READ_REG(lower_reg_address + 1);
159 st->tx[3] = 0;
160
161 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
162 if (ret) {
163 dev_err(&st->us->dev, "problem when reading 16 bit register");
164 goto error_ret;
165 }
166 tempval = (s16)(st->rx[1]) | ((s16)(st->rx[3]) << 8);
167
168 *val = tempval;
169error_ret:
170 mutex_unlock(&st->buf_lock);
171 return ret;
172}
173
174enum lis3l02dq_rm_ind {
175 LIS3L02DQ_ACCEL,
176 LIS3L02DQ_GAIN,
177 LIS3L02DQ_BIAS,
178};
179
180static u8 lis3l02dq_axis_map[3][3] = {
181 [LIS3L02DQ_ACCEL] = { LIS3L02DQ_REG_OUT_X_L_ADDR,
182 LIS3L02DQ_REG_OUT_Y_L_ADDR,
183 LIS3L02DQ_REG_OUT_Z_L_ADDR },
184 [LIS3L02DQ_GAIN] = { LIS3L02DQ_REG_GAIN_X_ADDR,
185 LIS3L02DQ_REG_GAIN_Y_ADDR,
186 LIS3L02DQ_REG_GAIN_Z_ADDR },
187 [LIS3L02DQ_BIAS] = { LIS3L02DQ_REG_OFFSET_X_ADDR,
188 LIS3L02DQ_REG_OFFSET_Y_ADDR,
189 LIS3L02DQ_REG_OFFSET_Z_ADDR }
190};
191
192static int lis3l02dq_read_thresh(struct iio_dev *indio_dev,
193 const struct iio_chan_spec *chan,
194 enum iio_event_type type,
195 enum iio_event_direction dir,
196 enum iio_event_info info,
197 int *val, int *val2)
198{
199 int ret;
200
201 ret = lis3l02dq_read_reg_s16(indio_dev, LIS3L02DQ_REG_THS_L_ADDR, val);
202 if (ret)
203 return ret;
204 return IIO_VAL_INT;
205}
206
207static int lis3l02dq_write_thresh(struct iio_dev *indio_dev,
208 const struct iio_chan_spec *chan,
209 enum iio_event_type type,
210 enum iio_event_direction dir,
211 enum iio_event_info info,
212 int val, int val2)
213{
214 u16 value = val;
215
216 return lis3l02dq_spi_write_reg_s16(indio_dev,
217 LIS3L02DQ_REG_THS_L_ADDR,
218 value);
219}
220
221static int lis3l02dq_write_raw(struct iio_dev *indio_dev,
222 struct iio_chan_spec const *chan,
223 int val,
224 int val2,
225 long mask)
226{
227 int ret = -EINVAL, reg;
228 u8 uval;
229 s8 sval;
230
231 switch (mask) {
232 case IIO_CHAN_INFO_CALIBBIAS:
233 if (val > 255 || val < -256)
234 return -EINVAL;
235 sval = val;
236 reg = lis3l02dq_axis_map[LIS3L02DQ_BIAS][chan->address];
237 ret = lis3l02dq_spi_write_reg_8(indio_dev, reg, sval);
238 break;
239 case IIO_CHAN_INFO_CALIBSCALE:
240 if (val & ~0xFF)
241 return -EINVAL;
242 uval = val;
243 reg = lis3l02dq_axis_map[LIS3L02DQ_GAIN][chan->address];
244 ret = lis3l02dq_spi_write_reg_8(indio_dev, reg, uval);
245 break;
246 }
247 return ret;
248}
249
250static int lis3l02dq_read_raw(struct iio_dev *indio_dev,
251 struct iio_chan_spec const *chan,
252 int *val,
253 int *val2,
254 long mask)
255{
256 u8 utemp;
257 s8 stemp;
258 ssize_t ret = 0;
259 u8 reg;
260
261 switch (mask) {
262 case IIO_CHAN_INFO_RAW:
263 /* Take the iio_dev status lock */
264 mutex_lock(&indio_dev->mlock);
265 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
266 ret = -EBUSY;
267 } else {
268 reg = lis3l02dq_axis_map
269 [LIS3L02DQ_ACCEL][chan->address];
270 ret = lis3l02dq_read_reg_s16(indio_dev, reg, val);
271 }
272 mutex_unlock(&indio_dev->mlock);
273 if (ret < 0)
274 goto error_ret;
275 return IIO_VAL_INT;
276 case IIO_CHAN_INFO_SCALE:
277 *val = 0;
278 *val2 = 9580;
279 return IIO_VAL_INT_PLUS_MICRO;
280 case IIO_CHAN_INFO_CALIBSCALE:
281 reg = lis3l02dq_axis_map[LIS3L02DQ_GAIN][chan->address];
282 ret = lis3l02dq_spi_read_reg_8(indio_dev, reg, &utemp);
283 if (ret)
284 goto error_ret;
285 /* to match with what previous code does */
286 *val = utemp;
287 return IIO_VAL_INT;
288
289 case IIO_CHAN_INFO_CALIBBIAS:
290 reg = lis3l02dq_axis_map[LIS3L02DQ_BIAS][chan->address];
291 ret = lis3l02dq_spi_read_reg_8(indio_dev, reg, (u8 *)&stemp);
292 /* to match with what previous code does */
293 *val = stemp;
294 return IIO_VAL_INT;
295 }
296error_ret:
297 return ret;
298}
299
300static ssize_t lis3l02dq_read_frequency(struct device *dev,
301 struct device_attribute *attr,
302 char *buf)
303{
304 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
305 int ret, len = 0;
306 s8 t;
307
308 ret = lis3l02dq_spi_read_reg_8(indio_dev,
309 LIS3L02DQ_REG_CTRL_1_ADDR,
310 (u8 *)&t);
311 if (ret)
312 return ret;
313 t &= LIS3L02DQ_DEC_MASK;
314 switch (t) {
315 case LIS3L02DQ_REG_CTRL_1_DF_128:
316 len = sprintf(buf, "280\n");
317 break;
318 case LIS3L02DQ_REG_CTRL_1_DF_64:
319 len = sprintf(buf, "560\n");
320 break;
321 case LIS3L02DQ_REG_CTRL_1_DF_32:
322 len = sprintf(buf, "1120\n");
323 break;
324 case LIS3L02DQ_REG_CTRL_1_DF_8:
325 len = sprintf(buf, "4480\n");
326 break;
327 }
328 return len;
329}
330
331static ssize_t lis3l02dq_write_frequency(struct device *dev,
332 struct device_attribute *attr,
333 const char *buf,
334 size_t len)
335{
336 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
337 unsigned long val;
338 int ret;
339 u8 t;
340
341 ret = kstrtoul(buf, 10, &val);
342 if (ret)
343 return ret;
344
345 mutex_lock(&indio_dev->mlock);
346 ret = lis3l02dq_spi_read_reg_8(indio_dev,
347 LIS3L02DQ_REG_CTRL_1_ADDR,
348 &t);
349 if (ret)
350 goto error_ret_mutex;
351 /* Wipe the bits clean */
352 t &= ~LIS3L02DQ_DEC_MASK;
353 switch (val) {
354 case 280:
355 t |= LIS3L02DQ_REG_CTRL_1_DF_128;
356 break;
357 case 560:
358 t |= LIS3L02DQ_REG_CTRL_1_DF_64;
359 break;
360 case 1120:
361 t |= LIS3L02DQ_REG_CTRL_1_DF_32;
362 break;
363 case 4480:
364 t |= LIS3L02DQ_REG_CTRL_1_DF_8;
365 break;
366 default:
367 ret = -EINVAL;
368 goto error_ret_mutex;
369 }
370
371 ret = lis3l02dq_spi_write_reg_8(indio_dev,
372 LIS3L02DQ_REG_CTRL_1_ADDR,
373 t);
374
375error_ret_mutex:
376 mutex_unlock(&indio_dev->mlock);
377
378 return ret ? ret : len;
379}
380
381static int lis3l02dq_initial_setup(struct iio_dev *indio_dev)
382{
383 struct lis3l02dq_state *st = iio_priv(indio_dev);
384 int ret;
385 u8 val, valtest;
386
387 st->us->mode = SPI_MODE_3;
388
389 spi_setup(st->us);
390
391 val = LIS3L02DQ_DEFAULT_CTRL1;
392 /* Write suitable defaults to ctrl1 */
393 ret = lis3l02dq_spi_write_reg_8(indio_dev,
394 LIS3L02DQ_REG_CTRL_1_ADDR,
395 val);
396 if (ret) {
397 dev_err(&st->us->dev, "problem with setup control register 1");
398 goto err_ret;
399 }
400 /* Repeat as sometimes doesn't work first time? */
401 ret = lis3l02dq_spi_write_reg_8(indio_dev,
402 LIS3L02DQ_REG_CTRL_1_ADDR,
403 val);
404 if (ret) {
405 dev_err(&st->us->dev, "problem with setup control register 1");
406 goto err_ret;
407 }
408
409 /*
410 * Read back to check this has worked acts as loose test of correct
411 * chip
412 */
413 ret = lis3l02dq_spi_read_reg_8(indio_dev,
414 LIS3L02DQ_REG_CTRL_1_ADDR,
415 &valtest);
416 if (ret || (valtest != val)) {
417 dev_err(&indio_dev->dev,
418 "device not playing ball %d %d\n", valtest, val);
419 ret = -EINVAL;
420 goto err_ret;
421 }
422
423 val = LIS3L02DQ_DEFAULT_CTRL2;
424 ret = lis3l02dq_spi_write_reg_8(indio_dev,
425 LIS3L02DQ_REG_CTRL_2_ADDR,
426 val);
427 if (ret) {
428 dev_err(&st->us->dev, "problem with setup control register 2");
429 goto err_ret;
430 }
431
432 val = LIS3L02DQ_REG_WAKE_UP_CFG_LATCH_SRC;
433 ret = lis3l02dq_spi_write_reg_8(indio_dev,
434 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
435 val);
436 if (ret)
437 dev_err(&st->us->dev, "problem with interrupt cfg register");
438err_ret:
439
440 return ret;
441}
442
443static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
444 lis3l02dq_read_frequency,
445 lis3l02dq_write_frequency);
446
447static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("280 560 1120 4480");
448
449static irqreturn_t lis3l02dq_event_handler(int irq, void *private)
450{
451 struct iio_dev *indio_dev = private;
452 u8 t;
453
454 s64 timestamp = iio_get_time_ns();
455
456 lis3l02dq_spi_read_reg_8(indio_dev,
457 LIS3L02DQ_REG_WAKE_UP_SRC_ADDR,
458 &t);
459
460 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Z_HIGH)
461 iio_push_event(indio_dev,
462 IIO_MOD_EVENT_CODE(IIO_ACCEL,
463 0,
464 IIO_MOD_Z,
465 IIO_EV_TYPE_THRESH,
466 IIO_EV_DIR_RISING),
467 timestamp);
468
469 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Z_LOW)
470 iio_push_event(indio_dev,
471 IIO_MOD_EVENT_CODE(IIO_ACCEL,
472 0,
473 IIO_MOD_Z,
474 IIO_EV_TYPE_THRESH,
475 IIO_EV_DIR_FALLING),
476 timestamp);
477
478 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Y_HIGH)
479 iio_push_event(indio_dev,
480 IIO_MOD_EVENT_CODE(IIO_ACCEL,
481 0,
482 IIO_MOD_Y,
483 IIO_EV_TYPE_THRESH,
484 IIO_EV_DIR_RISING),
485 timestamp);
486
487 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_Y_LOW)
488 iio_push_event(indio_dev,
489 IIO_MOD_EVENT_CODE(IIO_ACCEL,
490 0,
491 IIO_MOD_Y,
492 IIO_EV_TYPE_THRESH,
493 IIO_EV_DIR_FALLING),
494 timestamp);
495
496 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_X_HIGH)
497 iio_push_event(indio_dev,
498 IIO_MOD_EVENT_CODE(IIO_ACCEL,
499 0,
500 IIO_MOD_X,
501 IIO_EV_TYPE_THRESH,
502 IIO_EV_DIR_RISING),
503 timestamp);
504
505 if (t & LIS3L02DQ_REG_WAKE_UP_SRC_INTERRUPT_X_LOW)
506 iio_push_event(indio_dev,
507 IIO_MOD_EVENT_CODE(IIO_ACCEL,
508 0,
509 IIO_MOD_X,
510 IIO_EV_TYPE_THRESH,
511 IIO_EV_DIR_FALLING),
512 timestamp);
513
514 /* Ack and allow for new interrupts */
515 lis3l02dq_spi_read_reg_8(indio_dev,
516 LIS3L02DQ_REG_WAKE_UP_ACK_ADDR,
517 &t);
518
519 return IRQ_HANDLED;
520}
521
522static const struct iio_event_spec lis3l02dq_event[] = {
523 {
524 .type = IIO_EV_TYPE_THRESH,
525 .dir = IIO_EV_DIR_RISING,
526 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
527 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
528 }, {
529 .type = IIO_EV_TYPE_THRESH,
530 .dir = IIO_EV_DIR_FALLING,
531 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
532 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
533 }
534};
535
536#define LIS3L02DQ_CHAN(index, mod) \
537 { \
538 .type = IIO_ACCEL, \
539 .modified = 1, \
540 .channel2 = mod, \
541 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
542 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
543 BIT(IIO_CHAN_INFO_CALIBBIAS), \
544 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
545 .address = index, \
546 .scan_index = index, \
547 .scan_type = { \
548 .sign = 's', \
549 .realbits = 12, \
550 .storagebits = 16, \
551 }, \
552 .event_spec = lis3l02dq_event, \
553 .num_event_specs = ARRAY_SIZE(lis3l02dq_event), \
554 }
555
556static const struct iio_chan_spec lis3l02dq_channels[] = {
557 LIS3L02DQ_CHAN(0, IIO_MOD_X),
558 LIS3L02DQ_CHAN(1, IIO_MOD_Y),
559 LIS3L02DQ_CHAN(2, IIO_MOD_Z),
560 IIO_CHAN_SOFT_TIMESTAMP(3)
561};
562
563static int lis3l02dq_read_event_config(struct iio_dev *indio_dev,
564 const struct iio_chan_spec *chan,
565 enum iio_event_type type,
566 enum iio_event_direction dir)
567{
568 u8 val;
569 int ret;
570 u8 mask = 1 << (chan->channel2 * 2 + (dir == IIO_EV_DIR_RISING));
571
572 ret = lis3l02dq_spi_read_reg_8(indio_dev,
573 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
574 &val);
575 if (ret < 0)
576 return ret;
577
578 return !!(val & mask);
579}
580
581int lis3l02dq_disable_all_events(struct iio_dev *indio_dev)
582{
583 int ret;
584 u8 control, val;
585
586 ret = lis3l02dq_spi_read_reg_8(indio_dev,
587 LIS3L02DQ_REG_CTRL_2_ADDR,
588 &control);
589
590 control &= ~LIS3L02DQ_REG_CTRL_2_ENABLE_INTERRUPT;
591 ret = lis3l02dq_spi_write_reg_8(indio_dev,
592 LIS3L02DQ_REG_CTRL_2_ADDR,
593 control);
594 if (ret)
595 goto error_ret;
596 /* Also for consistency clear the mask */
597 ret = lis3l02dq_spi_read_reg_8(indio_dev,
598 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
599 &val);
600 if (ret)
601 goto error_ret;
602 val &= ~0x3f;
603
604 ret = lis3l02dq_spi_write_reg_8(indio_dev,
605 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
606 val);
607 if (ret)
608 goto error_ret;
609
610 ret = control;
611error_ret:
612 return ret;
613}
614
615static int lis3l02dq_write_event_config(struct iio_dev *indio_dev,
616 const struct iio_chan_spec *chan,
617 enum iio_event_type type,
618 enum iio_event_direction dir,
619 int state)
620{
621 int ret = 0;
622 u8 val, control;
623 u8 currentlyset;
624 bool changed = false;
625 u8 mask = 1 << (chan->channel2 * 2 + (dir == IIO_EV_DIR_RISING));
626
627 mutex_lock(&indio_dev->mlock);
628 /* read current control */
629 ret = lis3l02dq_spi_read_reg_8(indio_dev,
630 LIS3L02DQ_REG_CTRL_2_ADDR,
631 &control);
632 if (ret)
633 goto error_ret;
634 ret = lis3l02dq_spi_read_reg_8(indio_dev,
635 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
636 &val);
637 if (ret < 0)
638 goto error_ret;
639 currentlyset = val & mask;
640
641 if (!currentlyset && state) {
642 changed = true;
643 val |= mask;
644 } else if (currentlyset && !state) {
645 changed = true;
646 val &= ~mask;
647 }
648
649 if (changed) {
650 ret = lis3l02dq_spi_write_reg_8(indio_dev,
651 LIS3L02DQ_REG_WAKE_UP_CFG_ADDR,
652 val);
653 if (ret)
654 goto error_ret;
655 control = val & 0x3f ?
656 (control | LIS3L02DQ_REG_CTRL_2_ENABLE_INTERRUPT) :
657 (control & ~LIS3L02DQ_REG_CTRL_2_ENABLE_INTERRUPT);
658 ret = lis3l02dq_spi_write_reg_8(indio_dev,
659 LIS3L02DQ_REG_CTRL_2_ADDR,
660 control);
661 if (ret)
662 goto error_ret;
663 }
664
665error_ret:
666 mutex_unlock(&indio_dev->mlock);
667 return ret;
668}
669
670static struct attribute *lis3l02dq_attributes[] = {
671 &iio_dev_attr_sampling_frequency.dev_attr.attr,
672 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
673 NULL
674};
675
676static const struct attribute_group lis3l02dq_attribute_group = {
677 .attrs = lis3l02dq_attributes,
678};
679
680static const struct iio_info lis3l02dq_info = {
681 .read_raw = &lis3l02dq_read_raw,
682 .write_raw = &lis3l02dq_write_raw,
683 .read_event_value = &lis3l02dq_read_thresh,
684 .write_event_value = &lis3l02dq_write_thresh,
685 .write_event_config = &lis3l02dq_write_event_config,
686 .read_event_config = &lis3l02dq_read_event_config,
687 .driver_module = THIS_MODULE,
688 .attrs = &lis3l02dq_attribute_group,
689};
690
691static int lis3l02dq_probe(struct spi_device *spi)
692{
693 int ret;
694 struct lis3l02dq_state *st;
695 struct iio_dev *indio_dev;
696
697 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
698 if (!indio_dev)
699 return -ENOMEM;
700 st = iio_priv(indio_dev);
701 /* this is only used for removal purposes */
702 spi_set_drvdata(spi, indio_dev);
703
704 st->us = spi;
705 st->gpio = of_get_gpio(spi->dev.of_node, 0);
706 mutex_init(&st->buf_lock);
707 indio_dev->name = spi->dev.driver->name;
708 indio_dev->dev.parent = &spi->dev;
709 indio_dev->info = &lis3l02dq_info;
710 indio_dev->channels = lis3l02dq_channels;
711 indio_dev->num_channels = ARRAY_SIZE(lis3l02dq_channels);
712
713 indio_dev->modes = INDIO_DIRECT_MODE;
714
715 ret = lis3l02dq_configure_buffer(indio_dev);
716 if (ret)
717 return ret;
718
719 if (spi->irq) {
720 ret = request_threaded_irq(st->us->irq,
721 &lis3l02dq_th,
722 &lis3l02dq_event_handler,
723 IRQF_TRIGGER_RISING,
724 "lis3l02dq",
725 indio_dev);
726 if (ret)
727 goto error_unreg_buffer_funcs;
728
729 ret = lis3l02dq_probe_trigger(indio_dev);
730 if (ret)
731 goto error_free_interrupt;
732 }
733
734 /* Get the device into a sane initial state */
735 ret = lis3l02dq_initial_setup(indio_dev);
736 if (ret)
737 goto error_remove_trigger;
738
739 ret = iio_device_register(indio_dev);
740 if (ret)
741 goto error_remove_trigger;
742
743 return 0;
744
745error_remove_trigger:
746 if (spi->irq)
747 lis3l02dq_remove_trigger(indio_dev);
748error_free_interrupt:
749 if (spi->irq)
750 free_irq(st->us->irq, indio_dev);
751error_unreg_buffer_funcs:
752 lis3l02dq_unconfigure_buffer(indio_dev);
753 return ret;
754}
755
756/* Power down the device */
757static int lis3l02dq_stop_device(struct iio_dev *indio_dev)
758{
759 int ret;
760 struct lis3l02dq_state *st = iio_priv(indio_dev);
761 u8 val = 0;
762
763 mutex_lock(&indio_dev->mlock);
764 ret = lis3l02dq_spi_write_reg_8(indio_dev,
765 LIS3L02DQ_REG_CTRL_1_ADDR,
766 val);
767 if (ret) {
768 dev_err(&st->us->dev, "problem with turning device off: ctrl1");
769 goto err_ret;
770 }
771
772 ret = lis3l02dq_spi_write_reg_8(indio_dev,
773 LIS3L02DQ_REG_CTRL_2_ADDR,
774 val);
775 if (ret)
776 dev_err(&st->us->dev, "problem with turning device off: ctrl2");
777err_ret:
778 mutex_unlock(&indio_dev->mlock);
779 return ret;
780}
781
782/* fixme, confirm ordering in this function */
783static int lis3l02dq_remove(struct spi_device *spi)
784{
785 struct iio_dev *indio_dev = spi_get_drvdata(spi);
786 struct lis3l02dq_state *st = iio_priv(indio_dev);
787
788 iio_device_unregister(indio_dev);
789
790 lis3l02dq_disable_all_events(indio_dev);
791 lis3l02dq_stop_device(indio_dev);
792
793 if (spi->irq)
794 free_irq(st->us->irq, indio_dev);
795
796 lis3l02dq_remove_trigger(indio_dev);
797 lis3l02dq_unconfigure_buffer(indio_dev);
798
799 return 0;
800}
801
802static struct spi_driver lis3l02dq_driver = {
803 .driver = {
804 .name = "lis3l02dq",
805 },
806 .probe = lis3l02dq_probe,
807 .remove = lis3l02dq_remove,
808};
809module_spi_driver(lis3l02dq_driver);
810
811MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
812MODULE_DESCRIPTION("ST LIS3L02DQ Accelerometer SPI driver");
813MODULE_LICENSE("GPL v2");
814MODULE_ALIAS("spi:lis3l02dq");
diff --git a/drivers/staging/iio/accel/lis3l02dq_ring.c b/drivers/staging/iio/accel/lis3l02dq_ring.c
deleted file mode 100644
index 50c162e0c31f..000000000000
--- a/drivers/staging/iio/accel/lis3l02dq_ring.c
+++ /dev/null
@@ -1,428 +0,0 @@
1#include <linux/interrupt.h>
2#include <linux/gpio.h>
3#include <linux/mutex.h>
4#include <linux/kernel.h>
5#include <linux/spi/spi.h>
6#include <linux/slab.h>
7#include <linux/export.h>
8
9#include <linux/iio/iio.h>
10#include <linux/iio/kfifo_buf.h>
11#include <linux/iio/trigger.h>
12#include <linux/iio/trigger_consumer.h>
13#include "lis3l02dq.h"
14
15/**
16 * combine_8_to_16() utility function to munge two u8s into u16
17 **/
18static inline u16 combine_8_to_16(u8 lower, u8 upper)
19{
20 u16 _lower = lower;
21 u16 _upper = upper;
22
23 return _lower | (_upper << 8);
24}
25
26/**
27 * lis3l02dq_data_rdy_trig_poll() the event handler for the data rdy trig
28 **/
29irqreturn_t lis3l02dq_data_rdy_trig_poll(int irq, void *private)
30{
31 struct iio_dev *indio_dev = private;
32 struct lis3l02dq_state *st = iio_priv(indio_dev);
33
34 if (st->trigger_on) {
35 iio_trigger_poll(st->trig);
36 return IRQ_HANDLED;
37 }
38
39 return IRQ_WAKE_THREAD;
40}
41
42static const u8 read_all_tx_array[] = {
43 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_X_L_ADDR), 0,
44 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_X_H_ADDR), 0,
45 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Y_L_ADDR), 0,
46 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Y_H_ADDR), 0,
47 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Z_L_ADDR), 0,
48 LIS3L02DQ_READ_REG(LIS3L02DQ_REG_OUT_Z_H_ADDR), 0,
49};
50
51/**
52 * lis3l02dq_read_all() Reads all channels currently selected
53 * @indio_dev: IIO device state
54 * @rx_array: (dma capable) receive array, must be at least
55 * 4*number of channels
56 **/
57static int lis3l02dq_read_all(struct iio_dev *indio_dev, u8 *rx_array)
58{
59 struct lis3l02dq_state *st = iio_priv(indio_dev);
60 struct spi_transfer *xfers;
61 struct spi_message msg;
62 int ret, i, j = 0;
63
64 xfers = kcalloc(bitmap_weight(indio_dev->active_scan_mask,
65 indio_dev->masklength) * 2,
66 sizeof(*xfers), GFP_KERNEL);
67 if (!xfers)
68 return -ENOMEM;
69
70 mutex_lock(&st->buf_lock);
71
72 for (i = 0; i < ARRAY_SIZE(read_all_tx_array) / 4; i++)
73 if (test_bit(i, indio_dev->active_scan_mask)) {
74 /* lower byte */
75 xfers[j].tx_buf = st->tx + (2 * j);
76 st->tx[2 * j] = read_all_tx_array[i * 4];
77 st->tx[2 * j + 1] = 0;
78 if (rx_array)
79 xfers[j].rx_buf = rx_array + (j * 2);
80 xfers[j].bits_per_word = 8;
81 xfers[j].len = 2;
82 xfers[j].cs_change = 1;
83 j++;
84
85 /* upper byte */
86 xfers[j].tx_buf = st->tx + (2 * j);
87 st->tx[2 * j] = read_all_tx_array[i * 4 + 2];
88 st->tx[2 * j + 1] = 0;
89 if (rx_array)
90 xfers[j].rx_buf = rx_array + (j * 2);
91 xfers[j].bits_per_word = 8;
92 xfers[j].len = 2;
93 xfers[j].cs_change = 1;
94 j++;
95 }
96
97 /* After these are transmitted, the rx_buff should have
98 * values in alternate bytes
99 */
100 spi_message_init(&msg);
101 for (j = 0; j < bitmap_weight(indio_dev->active_scan_mask,
102 indio_dev->masklength) * 2; j++)
103 spi_message_add_tail(&xfers[j], &msg);
104
105 ret = spi_sync(st->us, &msg);
106 mutex_unlock(&st->buf_lock);
107 kfree(xfers);
108
109 return ret;
110}
111
112static int lis3l02dq_get_buffer_element(struct iio_dev *indio_dev,
113 u8 *buf)
114{
115 int ret, i;
116 u8 *rx_array;
117 s16 *data = (s16 *)buf;
118 int scan_count = bitmap_weight(indio_dev->active_scan_mask,
119 indio_dev->masklength);
120
121 rx_array = kcalloc(4, scan_count, GFP_KERNEL);
122 if (!rx_array)
123 return -ENOMEM;
124 ret = lis3l02dq_read_all(indio_dev, rx_array);
125 if (ret < 0) {
126 kfree(rx_array);
127 return ret;
128 }
129 for (i = 0; i < scan_count; i++)
130 data[i] = combine_8_to_16(rx_array[i * 4 + 1],
131 rx_array[i * 4 + 3]);
132 kfree(rx_array);
133
134 return i * sizeof(data[0]);
135}
136
137static irqreturn_t lis3l02dq_trigger_handler(int irq, void *p)
138{
139 struct iio_poll_func *pf = p;
140 struct iio_dev *indio_dev = pf->indio_dev;
141 int len = 0;
142 char *data;
143
144 data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
145 if (!data)
146 goto done;
147
148 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
149 len = lis3l02dq_get_buffer_element(indio_dev, data);
150
151 iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
152
153 kfree(data);
154done:
155 iio_trigger_notify_done(indio_dev->trig);
156 return IRQ_HANDLED;
157}
158
159/* Caller responsible for locking as necessary. */
160static int
161__lis3l02dq_write_data_ready_config(struct iio_dev *indio_dev, bool state)
162{
163 int ret;
164 u8 valold;
165 bool currentlyset;
166 struct lis3l02dq_state *st = iio_priv(indio_dev);
167
168 /* Get the current event mask register */
169 ret = lis3l02dq_spi_read_reg_8(indio_dev,
170 LIS3L02DQ_REG_CTRL_2_ADDR,
171 &valold);
172 if (ret)
173 goto error_ret;
174 /* Find out if data ready is already on */
175 currentlyset
176 = valold & LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
177
178 /* Disable requested */
179 if (!state && currentlyset) {
180 /* Disable the data ready signal */
181 valold &= ~LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
182
183 /* The double write is to overcome a hardware bug? */
184 ret = lis3l02dq_spi_write_reg_8(indio_dev,
185 LIS3L02DQ_REG_CTRL_2_ADDR,
186 valold);
187 if (ret)
188 goto error_ret;
189 ret = lis3l02dq_spi_write_reg_8(indio_dev,
190 LIS3L02DQ_REG_CTRL_2_ADDR,
191 valold);
192 if (ret)
193 goto error_ret;
194 st->trigger_on = false;
195 /* Enable requested */
196 } else if (state && !currentlyset) {
197 /* If not set, enable requested
198 * first disable all events
199 */
200 ret = lis3l02dq_disable_all_events(indio_dev);
201 if (ret < 0)
202 goto error_ret;
203
204 valold = ret |
205 LIS3L02DQ_REG_CTRL_2_ENABLE_DATA_READY_GENERATION;
206
207 st->trigger_on = true;
208 ret = lis3l02dq_spi_write_reg_8(indio_dev,
209 LIS3L02DQ_REG_CTRL_2_ADDR,
210 valold);
211 if (ret)
212 goto error_ret;
213 }
214
215 return 0;
216error_ret:
217 return ret;
218}
219
220/**
221 * lis3l02dq_data_rdy_trigger_set_state() set datardy interrupt state
222 *
223 * If disabling the interrupt also does a final read to ensure it is clear.
224 * This is only important in some cases where the scan enable elements are
225 * switched before the buffer is reenabled.
226 **/
227static int lis3l02dq_data_rdy_trigger_set_state(struct iio_trigger *trig,
228 bool state)
229{
230 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
231 int ret = 0;
232 u8 t;
233
234 __lis3l02dq_write_data_ready_config(indio_dev, state);
235 if (!state) {
236 /*
237 * A possible quirk with the handler is currently worked around
238 * by ensuring outstanding read events are cleared.
239 */
240 ret = lis3l02dq_read_all(indio_dev, NULL);
241 }
242 lis3l02dq_spi_read_reg_8(indio_dev,
243 LIS3L02DQ_REG_WAKE_UP_SRC_ADDR,
244 &t);
245 return ret;
246}
247
248/**
249 * lis3l02dq_trig_try_reen() try reenabling irq for data rdy trigger
250 * @trig: the datardy trigger
251 */
252static int lis3l02dq_trig_try_reen(struct iio_trigger *trig)
253{
254 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
255 struct lis3l02dq_state *st = iio_priv(indio_dev);
256 int i;
257
258 /* If gpio still high (or high again)
259 * In theory possible we will need to do this several times
260 */
261 for (i = 0; i < 5; i++)
262 if (gpio_get_value(st->gpio))
263 lis3l02dq_read_all(indio_dev, NULL);
264 else
265 break;
266 if (i == 5)
267 pr_info("Failed to clear the interrupt for lis3l02dq\n");
268
269 /* irq reenabled so success! */
270 return 0;
271}
272
273static const struct iio_trigger_ops lis3l02dq_trigger_ops = {
274 .owner = THIS_MODULE,
275 .set_trigger_state = &lis3l02dq_data_rdy_trigger_set_state,
276 .try_reenable = &lis3l02dq_trig_try_reen,
277};
278
279int lis3l02dq_probe_trigger(struct iio_dev *indio_dev)
280{
281 int ret;
282 struct lis3l02dq_state *st = iio_priv(indio_dev);
283
284 st->trig = iio_trigger_alloc("lis3l02dq-dev%d", indio_dev->id);
285 if (!st->trig) {
286 ret = -ENOMEM;
287 goto error_ret;
288 }
289
290 st->trig->dev.parent = &st->us->dev;
291 st->trig->ops = &lis3l02dq_trigger_ops;
292 iio_trigger_set_drvdata(st->trig, indio_dev);
293 ret = iio_trigger_register(st->trig);
294 if (ret)
295 goto error_free_trig;
296
297 return 0;
298
299error_free_trig:
300 iio_trigger_free(st->trig);
301error_ret:
302 return ret;
303}
304
305void lis3l02dq_remove_trigger(struct iio_dev *indio_dev)
306{
307 struct lis3l02dq_state *st = iio_priv(indio_dev);
308
309 iio_trigger_unregister(st->trig);
310 iio_trigger_free(st->trig);
311}
312
313void lis3l02dq_unconfigure_buffer(struct iio_dev *indio_dev)
314{
315 iio_dealloc_pollfunc(indio_dev->pollfunc);
316 iio_kfifo_free(indio_dev->buffer);
317}
318
319static int lis3l02dq_buffer_postenable(struct iio_dev *indio_dev)
320{
321 /* Disable unwanted channels otherwise the interrupt will not clear */
322 u8 t;
323 int ret;
324 bool oneenabled = false;
325
326 ret = lis3l02dq_spi_read_reg_8(indio_dev,
327 LIS3L02DQ_REG_CTRL_1_ADDR,
328 &t);
329 if (ret)
330 goto error_ret;
331
332 if (test_bit(0, indio_dev->active_scan_mask)) {
333 t |= LIS3L02DQ_REG_CTRL_1_AXES_X_ENABLE;
334 oneenabled = true;
335 } else {
336 t &= ~LIS3L02DQ_REG_CTRL_1_AXES_X_ENABLE;
337 }
338 if (test_bit(1, indio_dev->active_scan_mask)) {
339 t |= LIS3L02DQ_REG_CTRL_1_AXES_Y_ENABLE;
340 oneenabled = true;
341 } else {
342 t &= ~LIS3L02DQ_REG_CTRL_1_AXES_Y_ENABLE;
343 }
344 if (test_bit(2, indio_dev->active_scan_mask)) {
345 t |= LIS3L02DQ_REG_CTRL_1_AXES_Z_ENABLE;
346 oneenabled = true;
347 } else {
348 t &= ~LIS3L02DQ_REG_CTRL_1_AXES_Z_ENABLE;
349 }
350 if (!oneenabled) /* what happens in this case is unknown */
351 return -EINVAL;
352 ret = lis3l02dq_spi_write_reg_8(indio_dev,
353 LIS3L02DQ_REG_CTRL_1_ADDR,
354 t);
355 if (ret)
356 goto error_ret;
357
358 return iio_triggered_buffer_postenable(indio_dev);
359error_ret:
360 return ret;
361}
362
363/* Turn all channels on again */
364static int lis3l02dq_buffer_predisable(struct iio_dev *indio_dev)
365{
366 u8 t;
367 int ret;
368
369 ret = iio_triggered_buffer_predisable(indio_dev);
370 if (ret)
371 goto error_ret;
372
373 ret = lis3l02dq_spi_read_reg_8(indio_dev,
374 LIS3L02DQ_REG_CTRL_1_ADDR,
375 &t);
376 if (ret)
377 goto error_ret;
378 t |= LIS3L02DQ_REG_CTRL_1_AXES_X_ENABLE |
379 LIS3L02DQ_REG_CTRL_1_AXES_Y_ENABLE |
380 LIS3L02DQ_REG_CTRL_1_AXES_Z_ENABLE;
381
382 ret = lis3l02dq_spi_write_reg_8(indio_dev,
383 LIS3L02DQ_REG_CTRL_1_ADDR,
384 t);
385
386error_ret:
387 return ret;
388}
389
390static const struct iio_buffer_setup_ops lis3l02dq_buffer_setup_ops = {
391 .postenable = &lis3l02dq_buffer_postenable,
392 .predisable = &lis3l02dq_buffer_predisable,
393};
394
395int lis3l02dq_configure_buffer(struct iio_dev *indio_dev)
396{
397 int ret;
398 struct iio_buffer *buffer;
399
400 buffer = iio_kfifo_allocate();
401 if (!buffer)
402 return -ENOMEM;
403
404 iio_device_attach_buffer(indio_dev, buffer);
405
406 buffer->scan_timestamp = true;
407 indio_dev->setup_ops = &lis3l02dq_buffer_setup_ops;
408
409 /* Functions are NULL as we set handler below */
410 indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
411 &lis3l02dq_trigger_handler,
412 0,
413 indio_dev,
414 "lis3l02dq_consumer%d",
415 indio_dev->id);
416
417 if (!indio_dev->pollfunc) {
418 ret = -ENOMEM;
419 goto error_iio_sw_rb_free;
420 }
421
422 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
423 return 0;
424
425error_iio_sw_rb_free:
426 iio_kfifo_free(indio_dev->buffer);
427 return ret;
428}
diff --git a/drivers/staging/iio/accel/sca3000_core.c b/drivers/staging/iio/accel/sca3000_core.c
index b3f6dbf8b3a6..b5625f5d5e0e 100644
--- a/drivers/staging/iio/accel/sca3000_core.c
+++ b/drivers/staging/iio/accel/sca3000_core.c
@@ -774,7 +774,7 @@ static irqreturn_t sca3000_event_handler(int irq, void *private)
774 struct iio_dev *indio_dev = private; 774 struct iio_dev *indio_dev = private;
775 struct sca3000_state *st = iio_priv(indio_dev); 775 struct sca3000_state *st = iio_priv(indio_dev);
776 int ret, val; 776 int ret, val;
777 s64 last_timestamp = iio_get_time_ns(); 777 s64 last_timestamp = iio_get_time_ns(indio_dev);
778 778
779 /* 779 /*
780 * Could lead if badly timed to an extra read of status reg, 780 * Could lead if badly timed to an extra read of status reg,
diff --git a/drivers/staging/iio/adc/ad7280a.c b/drivers/staging/iio/adc/ad7280a.c
index a06b46cb81ca..2177f1dd2b5d 100644
--- a/drivers/staging/iio/adc/ad7280a.c
+++ b/drivers/staging/iio/adc/ad7280a.c
@@ -705,7 +705,7 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
705 IIO_EV_DIR_RISING, 705 IIO_EV_DIR_RISING,
706 IIO_EV_TYPE_THRESH, 706 IIO_EV_TYPE_THRESH,
707 0, 0, 0), 707 0, 0, 0),
708 iio_get_time_ns()); 708 iio_get_time_ns(indio_dev));
709 else if (((channels[i] >> 11) & 0xFFF) <= 709 else if (((channels[i] >> 11) & 0xFFF) <=
710 st->cell_threshlow) 710 st->cell_threshlow)
711 iio_push_event(indio_dev, 711 iio_push_event(indio_dev,
@@ -715,7 +715,7 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
715 IIO_EV_DIR_FALLING, 715 IIO_EV_DIR_FALLING,
716 IIO_EV_TYPE_THRESH, 716 IIO_EV_TYPE_THRESH,
717 0, 0, 0), 717 0, 0, 0),
718 iio_get_time_ns()); 718 iio_get_time_ns(indio_dev));
719 } else { 719 } else {
720 if (((channels[i] >> 11) & 0xFFF) >= st->aux_threshhigh) 720 if (((channels[i] >> 11) & 0xFFF) >= st->aux_threshhigh)
721 iio_push_event(indio_dev, 721 iio_push_event(indio_dev,
@@ -724,7 +724,7 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
724 0, 724 0,
725 IIO_EV_TYPE_THRESH, 725 IIO_EV_TYPE_THRESH,
726 IIO_EV_DIR_RISING), 726 IIO_EV_DIR_RISING),
727 iio_get_time_ns()); 727 iio_get_time_ns(indio_dev));
728 else if (((channels[i] >> 11) & 0xFFF) <= 728 else if (((channels[i] >> 11) & 0xFFF) <=
729 st->aux_threshlow) 729 st->aux_threshlow)
730 iio_push_event(indio_dev, 730 iio_push_event(indio_dev,
@@ -733,7 +733,7 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
733 0, 733 0,
734 IIO_EV_TYPE_THRESH, 734 IIO_EV_TYPE_THRESH,
735 IIO_EV_DIR_FALLING), 735 IIO_EV_DIR_FALLING),
736 iio_get_time_ns()); 736 iio_get_time_ns(indio_dev));
737 } 737 }
738 } 738 }
739 739
diff --git a/drivers/staging/iio/adc/ad7606_ring.c b/drivers/staging/iio/adc/ad7606_ring.c
index a6f8eb11242c..0572df9aad85 100644
--- a/drivers/staging/iio/adc/ad7606_ring.c
+++ b/drivers/staging/iio/adc/ad7606_ring.c
@@ -77,7 +77,8 @@ static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
77 goto done; 77 goto done;
78 } 78 }
79 79
80 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); 80 iio_push_to_buffers_with_timestamp(indio_dev, buf,
81 iio_get_time_ns(indio_dev));
81done: 82done:
82 gpio_set_value(st->pdata->gpio_convst, 0); 83 gpio_set_value(st->pdata->gpio_convst, 0);
83 iio_trigger_notify_done(indio_dev->trig); 84 iio_trigger_notify_done(indio_dev->trig);
diff --git a/drivers/staging/iio/adc/ad7816.c b/drivers/staging/iio/adc/ad7816.c
index ac3735c7f4a9..5e8115b01011 100644
--- a/drivers/staging/iio/adc/ad7816.c
+++ b/drivers/staging/iio/adc/ad7816.c
@@ -253,7 +253,8 @@ static const struct attribute_group ad7816_attribute_group = {
253 253
254static irqreturn_t ad7816_event_handler(int irq, void *private) 254static irqreturn_t ad7816_event_handler(int irq, void *private)
255{ 255{
256 iio_push_event(private, IIO_EVENT_CODE_AD7816_OTI, iio_get_time_ns()); 256 iio_push_event(private, IIO_EVENT_CODE_AD7816_OTI,
257 iio_get_time_ns((struct iio_dev *)private));
257 return IRQ_HANDLED; 258 return IRQ_HANDLED;
258} 259}
259 260
diff --git a/drivers/staging/iio/addac/adt7316.c b/drivers/staging/iio/addac/adt7316.c
index a10e7d8e6002..3faffe59c933 100644
--- a/drivers/staging/iio/addac/adt7316.c
+++ b/drivers/staging/iio/addac/adt7316.c
@@ -1752,7 +1752,7 @@ static irqreturn_t adt7316_event_handler(int irq, void *private)
1752 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX) 1752 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
1753 stat1 &= 0x1F; 1753 stat1 &= 0x1F;
1754 1754
1755 time = iio_get_time_ns(); 1755 time = iio_get_time_ns(indio_dev);
1756 if (stat1 & BIT(0)) 1756 if (stat1 & BIT(0))
1757 iio_push_event(indio_dev, 1757 iio_push_event(indio_dev,
1758 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0, 1758 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
@@ -1804,7 +1804,7 @@ static irqreturn_t adt7316_event_handler(int irq, void *private)
1804 0, 1804 0,
1805 IIO_EV_TYPE_THRESH, 1805 IIO_EV_TYPE_THRESH,
1806 IIO_EV_DIR_RISING), 1806 IIO_EV_DIR_RISING),
1807 iio_get_time_ns()); 1807 iio_get_time_ns(indio_dev));
1808 } 1808 }
1809 1809
1810 return IRQ_HANDLED; 1810 return IRQ_HANDLED;
diff --git a/drivers/staging/iio/cdc/ad7150.c b/drivers/staging/iio/cdc/ad7150.c
index f6b9a10326ea..5578a077fcfb 100644
--- a/drivers/staging/iio/cdc/ad7150.c
+++ b/drivers/staging/iio/cdc/ad7150.c
@@ -493,7 +493,7 @@ static irqreturn_t ad7150_event_handler(int irq, void *private)
493 struct iio_dev *indio_dev = private; 493 struct iio_dev *indio_dev = private;
494 struct ad7150_chip_info *chip = iio_priv(indio_dev); 494 struct ad7150_chip_info *chip = iio_priv(indio_dev);
495 u8 int_status; 495 u8 int_status;
496 s64 timestamp = iio_get_time_ns(); 496 s64 timestamp = iio_get_time_ns(indio_dev);
497 int ret; 497 int ret;
498 498
499 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS); 499 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
diff --git a/drivers/staging/iio/light/tsl2x7x_core.c b/drivers/staging/iio/light/tsl2x7x_core.c
index d553c8e18fcc..ea15bc1c300c 100644
--- a/drivers/staging/iio/light/tsl2x7x_core.c
+++ b/drivers/staging/iio/light/tsl2x7x_core.c
@@ -1554,7 +1554,7 @@ static irqreturn_t tsl2x7x_event_handler(int irq, void *private)
1554{ 1554{
1555 struct iio_dev *indio_dev = private; 1555 struct iio_dev *indio_dev = private;
1556 struct tsl2X7X_chip *chip = iio_priv(indio_dev); 1556 struct tsl2X7X_chip *chip = iio_priv(indio_dev);
1557 s64 timestamp = iio_get_time_ns(); 1557 s64 timestamp = iio_get_time_ns(indio_dev);
1558 int ret; 1558 int ret;
1559 u8 value; 1559 u8 value;
1560 1560
diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h
index 28052cddaa03..228bd44efa4c 100644
--- a/include/linux/iio/common/st_sensors.h
+++ b/include/linux/iio/common/st_sensors.h
@@ -223,6 +223,7 @@ struct st_sensor_settings {
223 * @get_irq_data_ready: Function to get the IRQ used for data ready signal. 223 * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
224 * @tf: Transfer function structure used by I/O operations. 224 * @tf: Transfer function structure used by I/O operations.
225 * @tb: Transfer buffers and mutex used by I/O operations. 225 * @tb: Transfer buffers and mutex used by I/O operations.
226 * @edge_irq: the IRQ triggers on edges and need special handling.
226 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor. 227 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
227 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use. 228 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
228 */ 229 */
@@ -250,6 +251,7 @@ struct st_sensor_data {
250 const struct st_sensor_transfer_function *tf; 251 const struct st_sensor_transfer_function *tf;
251 struct st_sensor_transfer_buffer tb; 252 struct st_sensor_transfer_buffer tb;
252 253
254 bool edge_irq;
253 bool hw_irq_trigger; 255 bool hw_irq_trigger;
254 s64 hw_timestamp; 256 s64 hw_timestamp;
255}; 257};
diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h
index 7c29cb0124ae..854e2dad1e0d 100644
--- a/include/linux/iio/iio.h
+++ b/include/linux/iio/iio.h
@@ -312,13 +312,8 @@ static inline bool iio_channel_has_info(const struct iio_chan_spec *chan,
312 }, \ 312 }, \
313} 313}
314 314
315/** 315s64 iio_get_time_ns(const struct iio_dev *indio_dev);
316 * iio_get_time_ns() - utility function to get a time stamp for events etc 316unsigned int iio_get_time_res(const struct iio_dev *indio_dev);
317 **/
318static inline s64 iio_get_time_ns(void)
319{
320 return ktime_get_real_ns();
321}
322 317
323/* Device operating modes */ 318/* Device operating modes */
324#define INDIO_DIRECT_MODE 0x01 319#define INDIO_DIRECT_MODE 0x01
@@ -497,6 +492,7 @@ struct iio_buffer_setup_ops {
497 * @chan_attr_group: [INTERN] group for all attrs in base directory 492 * @chan_attr_group: [INTERN] group for all attrs in base directory
498 * @name: [DRIVER] name of the device. 493 * @name: [DRIVER] name of the device.
499 * @info: [DRIVER] callbacks and constant info from driver 494 * @info: [DRIVER] callbacks and constant info from driver
495 * @clock_id: [INTERN] timestamping clock posix identifier
500 * @info_exist_lock: [INTERN] lock to prevent use during removal 496 * @info_exist_lock: [INTERN] lock to prevent use during removal
501 * @setup_ops: [DRIVER] callbacks to call before and after buffer 497 * @setup_ops: [DRIVER] callbacks to call before and after buffer
502 * enable/disable 498 * enable/disable
@@ -537,6 +533,7 @@ struct iio_dev {
537 struct attribute_group chan_attr_group; 533 struct attribute_group chan_attr_group;
538 const char *name; 534 const char *name;
539 const struct iio_info *info; 535 const struct iio_info *info;
536 clockid_t clock_id;
540 struct mutex info_exist_lock; 537 struct mutex info_exist_lock;
541 const struct iio_buffer_setup_ops *setup_ops; 538 const struct iio_buffer_setup_ops *setup_ops;
542 struct cdev chrdev; 539 struct cdev chrdev;
@@ -565,7 +562,7 @@ extern struct bus_type iio_bus_type;
565 562
566/** 563/**
567 * iio_device_put() - reference counted deallocation of struct device 564 * iio_device_put() - reference counted deallocation of struct device
568 * @indio_dev: IIO device structure containing the device 565 * @indio_dev: IIO device structure containing the device
569 **/ 566 **/
570static inline void iio_device_put(struct iio_dev *indio_dev) 567static inline void iio_device_put(struct iio_dev *indio_dev)
571{ 568{
@@ -574,6 +571,15 @@ static inline void iio_device_put(struct iio_dev *indio_dev)
574} 571}
575 572
576/** 573/**
574 * iio_device_get_clock() - Retrieve current timestamping clock for the device
575 * @indio_dev: IIO device structure containing the device
576 */
577static inline clockid_t iio_device_get_clock(const struct iio_dev *indio_dev)
578{
579 return indio_dev->clock_id;
580}
581
582/**
577 * dev_to_iio_dev() - Get IIO device struct from a device struct 583 * dev_to_iio_dev() - Get IIO device struct from a device struct
578 * @dev: The device embedded in the IIO device 584 * @dev: The device embedded in the IIO device
579 * 585 *
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 479d25cd3d4f..255e225393ac 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -2186,6 +2186,7 @@ struct timespec64 get_monotonic_coarse64(void)
2186 2186
2187 return now; 2187 return now;
2188} 2188}
2189EXPORT_SYMBOL(get_monotonic_coarse64);
2189 2190
2190/* 2191/*
2191 * Must hold jiffies_lock 2192 * Must hold jiffies_lock