aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-09-03 14:37:57 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-09-03 14:37:57 -0400
commit751144271f4b63d5de9005ea4e5e6e5c7c6fd629 (patch)
tree2e5cb8223d4f6146f01f123a9f33cf6d468205c6 /drivers/iio
parent542a086ac72fb193cbc1b996963a572269e57743 (diff)
parent91121c103ae93ef117e58712786864270d7f488e (diff)
Merge tag 'staging-3.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
Pull staging tree merge from Greg KH: "Here's the bit staging tree pull request for 3.12-rc1. Lots of staging driver updates, and fixes. Lustre is finally enabled in the build, and lots of cleanup started happening in it. There's a new wireless driver in here, and 2 new TTY drivers, which cause the overall lines added/removed to be quite large on the "added" side. The IIO driver updates are also coming through here, as they are tied to the staging iio drivers" * tag 'staging-3.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (942 commits) staging: dwc2: make dwc2_core_params documentation more complete staging: dwc2: validate the value for phy_utmi_width staging: dwc2: interpret all hwcfg and related register at init time staging: dwc2: properly mask the GRXFSIZ register staging: dwc2: remove redundant register reads staging: dwc2: re-use hptxfsiz variable staging: dwc2: simplify debug output in dwc_hc_init staging: dwc2: add missing shift staging: dwc2: simplify register shift expressions staging: dwc2: only read the snpsid register once staging: dwc2: unshift non-bool register value constants staging: dwc2: fix off-by-one in check for max_packet_count parameter staging: dwc2: remove specific fifo size constants Staging:BCM:DDRInit.c:Renaming __FUNCTION__ staging: bcm: remove Version.h file. staging: rtl8188eu: off by one in rtw_set_802_11_add_wep() staging: r8188eu: copying one byte too much staging: rtl8188eu: || vs && typo staging: r8188eu: off by one bugs staging: crystalhd: Resolve sparse 'different base types' warnings. ...
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/Kconfig8
-rw-r--r--drivers/iio/Makefile3
-rw-r--r--drivers/iio/accel/Kconfig32
-rw-r--r--drivers/iio/accel/Makefile5
-rw-r--r--drivers/iio/accel/bma180.c676
-rw-r--r--drivers/iio/accel/hid-sensor-accel-3d.c43
-rw-r--r--drivers/iio/accel/kxsd9.c17
-rw-r--r--drivers/iio/accel/st_accel.h11
-rw-r--r--drivers/iio/accel/st_accel_core.c28
-rw-r--r--drivers/iio/accel/st_accel_i2c.c17
-rw-r--r--drivers/iio/accel/st_accel_spi.c17
-rw-r--r--drivers/iio/adc/Kconfig61
-rw-r--r--drivers/iio/adc/Makefile3
-rw-r--r--drivers/iio/adc/ad7266.c16
-rw-r--r--drivers/iio/adc/ad7298.c24
-rw-r--r--drivers/iio/adc/ad7476.c26
-rw-r--r--drivers/iio/adc/ad7791.c19
-rw-r--r--drivers/iio/adc/ad7793.c23
-rw-r--r--drivers/iio/adc/ad7887.c23
-rw-r--r--drivers/iio/adc/ad7923.c20
-rw-r--r--drivers/iio/adc/at91_adc.c113
-rw-r--r--drivers/iio/adc/exynos_adc.c23
-rw-r--r--drivers/iio/adc/lp8788_adc.c7
-rw-r--r--drivers/iio/adc/max1363.c15
-rw-r--r--drivers/iio/adc/mcp320x.c18
-rw-r--r--drivers/iio/adc/nau7802.c581
-rw-r--r--drivers/iio/adc/ti-adc081c.c18
-rw-r--r--drivers/iio/adc/ti_am335x_adc.c13
-rw-r--r--drivers/iio/adc/twl6030-gpadc.c1013
-rw-r--r--drivers/iio/adc/viperboard_adc.c9
-rw-r--r--drivers/iio/amplifiers/Kconfig2
-rw-r--r--drivers/iio/amplifiers/Makefile1
-rw-r--r--drivers/iio/amplifiers/ad8366.c13
-rw-r--r--drivers/iio/common/Makefile1
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c41
-rw-r--r--drivers/iio/dac/Kconfig44
-rw-r--r--drivers/iio/dac/Makefile1
-rw-r--r--drivers/iio/dac/ad5064.c21
-rw-r--r--drivers/iio/dac/ad5360.c15
-rw-r--r--drivers/iio/dac/ad5380.c18
-rw-r--r--drivers/iio/dac/ad5421.c19
-rw-r--r--drivers/iio/dac/ad5446.c19
-rw-r--r--drivers/iio/dac/ad5449.c15
-rw-r--r--drivers/iio/dac/ad5504.c31
-rw-r--r--drivers/iio/dac/ad5624r_spi.c22
-rw-r--r--drivers/iio/dac/ad5686.c17
-rw-r--r--drivers/iio/dac/ad5755.c14
-rw-r--r--drivers/iio/dac/ad5764.c20
-rw-r--r--drivers/iio/dac/ad5791.c35
-rw-r--r--drivers/iio/dac/ad7303.c31
-rw-r--r--drivers/iio/dac/max517.c17
-rw-r--r--drivers/iio/dac/mcp4725.c177
-rw-r--r--drivers/iio/frequency/Kconfig1
-rw-r--r--drivers/iio/frequency/Makefile1
-rw-r--r--drivers/iio/frequency/ad9523.c17
-rw-r--r--drivers/iio/frequency/adf4350.c31
-rw-r--r--drivers/iio/gyro/Kconfig18
-rw-r--r--drivers/iio/gyro/Makefile2
-rw-r--r--drivers/iio/gyro/adis16080.c21
-rw-r--r--drivers/iio/gyro/adis16130.c23
-rw-r--r--drivers/iio/gyro/adis16136.c10
-rw-r--r--drivers/iio/gyro/adis16260.c422
-rw-r--r--drivers/iio/gyro/adxrs450.c15
-rw-r--r--drivers/iio/gyro/hid-sensor-gyro-3d.c41
-rw-r--r--drivers/iio/gyro/itg3200_core.c15
-rw-r--r--drivers/iio/gyro/st_gyro.h11
-rw-r--r--drivers/iio/gyro/st_gyro_core.c14
-rw-r--r--drivers/iio/gyro/st_gyro_i2c.c18
-rw-r--r--drivers/iio/gyro/st_gyro_spi.c18
-rw-r--r--drivers/iio/iio_core_trigger.h7
-rw-r--r--drivers/iio/imu/Kconfig2
-rw-r--r--drivers/iio/imu/Makefile1
-rw-r--r--drivers/iio/imu/adis16400_core.c10
-rw-r--r--drivers/iio/imu/adis16480.c10
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_core.c29
-rw-r--r--drivers/iio/industrialio-core.c51
-rw-r--r--drivers/iio/industrialio-trigger.c72
-rw-r--r--drivers/iio/light/Kconfig34
-rw-r--r--drivers/iio/light/Makefile4
-rw-r--r--drivers/iio/light/adjd_s311.c43
-rw-r--r--drivers/iio/light/apds9300.c512
-rw-r--r--drivers/iio/light/hid-sensor-als.c41
-rw-r--r--drivers/iio/light/lm3533-als.c7
-rw-r--r--drivers/iio/light/tsl2563.c25
-rw-r--r--drivers/iio/light/vcnl4000.c16
-rw-r--r--drivers/iio/magnetometer/Kconfig6
-rw-r--r--drivers/iio/magnetometer/Makefile1
-rw-r--r--drivers/iio/magnetometer/hid-sensor-magn-3d.c43
-rw-r--r--drivers/iio/magnetometer/st_magn.h3
-rw-r--r--drivers/iio/magnetometer/st_magn_core.c6
-rw-r--r--drivers/iio/magnetometer/st_magn_i2c.c17
-rw-r--r--drivers/iio/magnetometer/st_magn_spi.c17
-rw-r--r--drivers/iio/pressure/Kconfig14
-rw-r--r--drivers/iio/pressure/Makefile1
-rw-r--r--drivers/iio/pressure/st_pressure.h11
-rw-r--r--drivers/iio/pressure/st_pressure_core.c21
-rw-r--r--drivers/iio/pressure/st_pressure_i2c.c17
-rw-r--r--drivers/iio/pressure/st_pressure_spi.c17
-rw-r--r--drivers/iio/temperature/Kconfig16
-rw-r--r--drivers/iio/temperature/Makefile5
-rw-r--r--drivers/iio/temperature/tmp006.c291
-rw-r--r--drivers/iio/trigger/Kconfig4
-rw-r--r--drivers/iio/trigger/Makefile1
103 files changed, 4478 insertions, 1011 deletions
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index 9af763a90d93..cbea3271c1b1 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -23,15 +23,14 @@ if IIO_BUFFER
23config IIO_BUFFER_CB 23config IIO_BUFFER_CB
24boolean "IIO callback buffer used for push in-kernel interfaces" 24boolean "IIO callback buffer used for push in-kernel interfaces"
25 help 25 help
26 Should be selected by any drivers that do-inkernel push 26 Should be selected by any drivers that do in-kernel push
27 usage. That is, those where the data is pushed to the consumer. 27 usage. That is, those where the data is pushed to the consumer.
28 28
29config IIO_KFIFO_BUF 29config IIO_KFIFO_BUF
30 select IIO_TRIGGER 30 select IIO_TRIGGER
31 tristate "Industrial I/O buffering based on kfifo" 31 tristate "Industrial I/O buffering based on kfifo"
32 help 32 help
33 A simple fifo based on kfifo. Use this if you want a fifo 33 A simple fifo based on kfifo. Note that this currently provides
34 rather than a ring buffer. Note that this currently provides
35 no buffer events so it is up to userspace to work out how 34 no buffer events so it is up to userspace to work out how
36 often to read from the buffer. 35 often to read from the buffer.
37 36
@@ -49,7 +48,7 @@ config IIO_TRIGGER
49 help 48 help
50 Provides IIO core support for triggers. Currently these 49 Provides IIO core support for triggers. Currently these
51 are used to initialize capture of samples to push into 50 are used to initialize capture of samples to push into
52 ring buffers. The triggers are effectively a 'capture 51 buffers. The triggers are effectively a 'capture
53 data now' interrupt. 52 data now' interrupt.
54 53
55config IIO_CONSUMERS_PER_TRIGGER 54config IIO_CONSUMERS_PER_TRIGGER
@@ -74,5 +73,6 @@ if IIO_TRIGGER
74 source "drivers/iio/trigger/Kconfig" 73 source "drivers/iio/trigger/Kconfig"
75endif #IIO_TRIGGER 74endif #IIO_TRIGGER
76source "drivers/iio/pressure/Kconfig" 75source "drivers/iio/pressure/Kconfig"
76source "drivers/iio/temperature/Kconfig"
77 77
78endif # IIO 78endif # IIO
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index 7a3866c2d2a1..bcf7e9e3b053 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -21,5 +21,6 @@ obj-y += frequency/
21obj-y += imu/ 21obj-y += imu/
22obj-y += light/ 22obj-y += light/
23obj-y += magnetometer/ 23obj-y += magnetometer/
24obj-y += trigger/
25obj-y += pressure/ 24obj-y += pressure/
25obj-y += temperature/
26obj-y += trigger/
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index 719d83fe51dd..e23e50850655 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -1,8 +1,22 @@
1# 1#
2# Accelerometer drivers 2# Accelerometer drivers
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Accelerometers" 6menu "Accelerometers"
5 7
8config BMA180
9 tristate "Bosch BMA180 3-Axis Accelerometer Driver"
10 depends on I2C
11 select IIO_BUFFER
12 select IIO_TRIGGERED_BUFFER
13 help
14 Say Y here if you want to build a driver for the Bosch BMA180
15 triaxial acceleration sensor.
16
17 To compile this driver as a module, choose M here: the
18 module will be called bma180.
19
6config HID_SENSOR_ACCEL_3D 20config HID_SENSOR_ACCEL_3D
7 depends on HID_SENSOR_HUB 21 depends on HID_SENSOR_HUB
8 select IIO_BUFFER 22 select IIO_BUFFER
@@ -14,13 +28,6 @@ config HID_SENSOR_ACCEL_3D
14 Say yes here to build support for the HID SENSOR 28 Say yes here to build support for the HID SENSOR
15 accelerometers 3D. 29 accelerometers 3D.
16 30
17config KXSD9
18 tristate "Kionix KXSD9 Accelerometer Driver"
19 depends on SPI
20 help
21 Say yes here to build support for the Kionix KXSD9 accelerometer.
22 Currently this only supports the device via an SPI interface.
23
24config IIO_ST_ACCEL_3AXIS 31config IIO_ST_ACCEL_3AXIS
25 tristate "STMicroelectronics accelerometers 3-Axis Driver" 32 tristate "STMicroelectronics accelerometers 3-Axis Driver"
26 depends on (I2C || SPI_MASTER) && SYSFS 33 depends on (I2C || SPI_MASTER) && SYSFS
@@ -33,8 +40,8 @@ config IIO_ST_ACCEL_3AXIS
33 LSM303DLH, LSM303DLHC, LIS3DH, LSM330D, LSM330DL, LSM330DLC, 40 LSM303DLH, LSM303DLHC, LIS3DH, LSM330D, LSM330DL, LSM330DLC,
34 LIS331DLH, LSM303DL, LSM303DLM, LSM330. 41 LIS331DLH, LSM303DL, LSM303DLM, LSM330.
35 42
36 This driver can also be built as a module. If so, will be created 43 This driver can also be built as a module. If so, these modules
37 these modules: 44 will be created:
38 - st_accel (core functions for the driver [it is mandatory]); 45 - st_accel (core functions for the driver [it is mandatory]);
39 - st_accel_i2c (necessary for the I2C devices [optional*]); 46 - st_accel_i2c (necessary for the I2C devices [optional*]);
40 - st_accel_spi (necessary for the SPI devices [optional*]); 47 - st_accel_spi (necessary for the SPI devices [optional*]);
@@ -51,4 +58,11 @@ config IIO_ST_ACCEL_SPI_3AXIS
51 depends on IIO_ST_ACCEL_3AXIS 58 depends on IIO_ST_ACCEL_3AXIS
52 depends on IIO_ST_SENSORS_SPI 59 depends on IIO_ST_SENSORS_SPI
53 60
61config KXSD9
62 tristate "Kionix KXSD9 Accelerometer Driver"
63 depends on SPI
64 help
65 Say yes here to build support for the Kionix KXSD9 accelerometer.
66 Currently this only supports the device via an SPI interface.
67
54endmenu 68endmenu
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index 87d8fa264894..c48d15f25616 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -2,7 +2,10 @@
2# Makefile for industrial I/O accelerometer drivers 2# Makefile for industrial I/O accelerometer drivers
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_BMA180) += bma180.o
5obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o 7obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
8obj-$(CONFIG_KXSD9) += kxsd9.o
6 9
7obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o 10obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o
8st_accel-y := st_accel_core.o 11st_accel-y := st_accel_core.o
@@ -10,5 +13,3 @@ st_accel-$(CONFIG_IIO_BUFFER) += st_accel_buffer.o
10 13
11obj-$(CONFIG_IIO_ST_ACCEL_I2C_3AXIS) += st_accel_i2c.o 14obj-$(CONFIG_IIO_ST_ACCEL_I2C_3AXIS) += st_accel_i2c.o
12obj-$(CONFIG_IIO_ST_ACCEL_SPI_3AXIS) += st_accel_spi.o 15obj-$(CONFIG_IIO_ST_ACCEL_SPI_3AXIS) += st_accel_spi.o
13
14obj-$(CONFIG_KXSD9) += kxsd9.o
diff --git a/drivers/iio/accel/bma180.c b/drivers/iio/accel/bma180.c
new file mode 100644
index 000000000000..12e32e6b4103
--- /dev/null
+++ b/drivers/iio/accel/bma180.c
@@ -0,0 +1,676 @@
1/*
2 * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor
3 *
4 * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com>
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 */
10
11#include <linux/module.h>
12#include <linux/i2c.h>
13#include <linux/interrupt.h>
14#include <linux/delay.h>
15#include <linux/of.h>
16#include <linux/bitops.h>
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21#include <linux/iio/buffer.h>
22#include <linux/iio/trigger.h>
23#include <linux/iio/trigger_consumer.h>
24#include <linux/iio/triggered_buffer.h>
25
26#define BMA180_DRV_NAME "bma180"
27#define BMA180_IRQ_NAME "bma180_event"
28
29/* Register set */
30#define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */
31#define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */
32#define BMA180_CTRL_REG0 0x0d
33#define BMA180_RESET 0x10
34#define BMA180_BW_TCS 0x20
35#define BMA180_CTRL_REG3 0x21
36#define BMA180_TCO_Z 0x30
37#define BMA180_OFFSET_LSB1 0x35
38
39/* BMA180_CTRL_REG0 bits */
40#define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */
41#define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */
42#define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */
43#define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */
44
45/* BMA180_CTRL_REG3 bits */
46#define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */
47
48/* BMA180_OFFSET_LSB1 skipping mode bit */
49#define BMA180_SMP_SKIP BIT(0)
50
51/* Bit masks for registers bit fields */
52#define BMA180_RANGE 0x0e /* Range of measured accel values*/
53#define BMA180_BW 0xf0 /* Accel bandwidth */
54#define BMA180_MODE_CONFIG 0x03 /* Config operation modes */
55
56/* We have to write this value in reset register to do soft reset */
57#define BMA180_RESET_VAL 0xb6
58
59#define BMA_180_ID_REG_VAL 0x03
60
61/* Chip power modes */
62#define BMA180_LOW_NOISE 0x00
63#define BMA180_LOW_POWER 0x03
64
65#define BMA180_LOW_NOISE_STR "low_noise"
66#define BMA180_LOW_POWER_STR "low_power"
67
68/* Defaults values */
69#define BMA180_DEF_PMODE 0
70#define BMA180_DEF_BW 20
71#define BMA180_DEF_SCALE 250
72
73/* Available values for sysfs */
74#define BMA180_FLP_FREQ_AVAILABLE \
75 "10 20 40 75 150 300"
76#define BMA180_SCALE_AVAILABLE \
77 "0.000130 0.000190 0.000250 0.000380 0.000500 0.000990 0.001980"
78
79struct bma180_data {
80 struct i2c_client *client;
81 struct iio_trigger *trig;
82 struct mutex mutex;
83 int sleep_state;
84 int scale;
85 int bw;
86 int pmode;
87 char *buff;
88};
89
90enum bma180_axis {
91 AXIS_X,
92 AXIS_Y,
93 AXIS_Z,
94};
95
96static int bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */
97static int scale_table[] = { 130, 190, 250, 380, 500, 990, 1980 };
98
99static int bma180_get_acc_reg(struct bma180_data *data, enum bma180_axis axis)
100{
101 u8 reg = BMA180_ACC_X_LSB + axis * 2;
102 int ret;
103
104 if (data->sleep_state)
105 return -EBUSY;
106
107 ret = i2c_smbus_read_word_data(data->client, reg);
108 if (ret < 0)
109 dev_err(&data->client->dev,
110 "failed to read accel_%c registers\n", 'x' + axis);
111
112 return ret;
113}
114
115static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val)
116{
117 int ret = i2c_smbus_read_byte_data(data->client, reg);
118 u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1));
119
120 if (ret < 0)
121 return ret;
122
123 return i2c_smbus_write_byte_data(data->client, reg, reg_val);
124}
125
126static int bma180_reset_intr(struct bma180_data *data)
127{
128 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_RESET_INT, 1);
129
130 if (ret)
131 dev_err(&data->client->dev, "failed to reset interrupt\n");
132
133 return ret;
134}
135
136static int bma180_set_new_data_intr_state(struct bma180_data *data, int state)
137{
138 u8 reg_val = state ? BMA180_NEW_DATA_INT : 0x00;
139 int ret = i2c_smbus_write_byte_data(data->client, BMA180_CTRL_REG3,
140 reg_val);
141
142 if (ret)
143 goto err;
144 ret = bma180_reset_intr(data);
145 if (ret)
146 goto err;
147
148 return 0;
149
150err:
151 dev_err(&data->client->dev,
152 "failed to set new data interrupt state %d\n", state);
153 return ret;
154}
155
156static int bma180_set_sleep_state(struct bma180_data *data, int state)
157{
158 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_SLEEP, state);
159
160 if (ret) {
161 dev_err(&data->client->dev,
162 "failed to set sleep state %d\n", state);
163 return ret;
164 }
165 data->sleep_state = state;
166
167 return 0;
168}
169
170static int bma180_set_ee_writing_state(struct bma180_data *data, int state)
171{
172 int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state);
173
174 if (ret)
175 dev_err(&data->client->dev,
176 "failed to set ee writing state %d\n", state);
177
178 return ret;
179}
180
181static int bma180_set_bw(struct bma180_data *data, int val)
182{
183 int ret, i;
184
185 if (data->sleep_state)
186 return -EBUSY;
187
188 for (i = 0; i < ARRAY_SIZE(bw_table); ++i) {
189 if (bw_table[i] == val) {
190 ret = bma180_set_bits(data,
191 BMA180_BW_TCS, BMA180_BW, i);
192 if (ret) {
193 dev_err(&data->client->dev,
194 "failed to set bandwidth\n");
195 return ret;
196 }
197 data->bw = val;
198 return 0;
199 }
200 }
201
202 return -EINVAL;
203}
204
205static int bma180_set_scale(struct bma180_data *data, int val)
206{
207 int ret, i;
208
209 if (data->sleep_state)
210 return -EBUSY;
211
212 for (i = 0; i < ARRAY_SIZE(scale_table); ++i)
213 if (scale_table[i] == val) {
214 ret = bma180_set_bits(data,
215 BMA180_OFFSET_LSB1, BMA180_RANGE, i);
216 if (ret) {
217 dev_err(&data->client->dev,
218 "failed to set scale\n");
219 return ret;
220 }
221 data->scale = val;
222 return 0;
223 }
224
225 return -EINVAL;
226}
227
228static int bma180_set_pmode(struct bma180_data *data, int mode)
229{
230 u8 reg_val = mode ? BMA180_LOW_POWER : BMA180_LOW_NOISE;
231 int ret = bma180_set_bits(data, BMA180_TCO_Z, BMA180_MODE_CONFIG,
232 reg_val);
233
234 if (ret) {
235 dev_err(&data->client->dev, "failed to set power mode\n");
236 return ret;
237 }
238 data->pmode = mode;
239
240 return 0;
241}
242
243static int bma180_soft_reset(struct bma180_data *data)
244{
245 int ret = i2c_smbus_write_byte_data(data->client,
246 BMA180_RESET, BMA180_RESET_VAL);
247
248 if (ret)
249 dev_err(&data->client->dev, "failed to reset the chip\n");
250
251 return ret;
252}
253
254static int bma180_chip_init(struct bma180_data *data)
255{
256 /* Try to read chip_id register. It must return 0x03. */
257 int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID);
258
259 if (ret < 0)
260 goto err;
261 if (ret != BMA_180_ID_REG_VAL) {
262 ret = -ENODEV;
263 goto err;
264 }
265
266 ret = bma180_soft_reset(data);
267 if (ret)
268 goto err;
269 /*
270 * No serial transaction should occur within minimum 10 us
271 * after soft_reset command
272 */
273 msleep(20);
274
275 ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1);
276 if (ret)
277 goto err;
278 ret = bma180_set_ee_writing_state(data, 1);
279 if (ret)
280 goto err;
281 ret = bma180_set_new_data_intr_state(data, 0);
282 if (ret)
283 goto err;
284 ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1);
285 if (ret)
286 goto err;
287 ret = bma180_set_pmode(data, BMA180_DEF_PMODE);
288 if (ret)
289 goto err;
290 ret = bma180_set_bw(data, BMA180_DEF_BW);
291 if (ret)
292 goto err;
293 ret = bma180_set_scale(data, BMA180_DEF_SCALE);
294 if (ret)
295 goto err;
296
297 return 0;
298
299err:
300 dev_err(&data->client->dev, "failed to init the chip\n");
301 return ret;
302}
303
304static void bma180_chip_disable(struct bma180_data *data)
305{
306 if (bma180_set_new_data_intr_state(data, 0))
307 goto err;
308 if (bma180_set_ee_writing_state(data, 0))
309 goto err;
310 if (bma180_set_sleep_state(data, 1))
311 goto err;
312
313 return;
314
315err:
316 dev_err(&data->client->dev, "failed to disable the chip\n");
317}
318
319static IIO_CONST_ATTR(in_accel_filter_low_pass_3db_frequency_available,
320 BMA180_FLP_FREQ_AVAILABLE);
321static IIO_CONST_ATTR(in_accel_scale_available, BMA180_SCALE_AVAILABLE);
322
323static struct attribute *bma180_attributes[] = {
324 &iio_const_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr,
325 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
326 NULL,
327};
328
329static const struct attribute_group bma180_attrs_group = {
330 .attrs = bma180_attributes,
331};
332
333static int bma180_read_raw(struct iio_dev *indio_dev,
334 struct iio_chan_spec const *chan, int *val, int *val2,
335 long mask)
336{
337 struct bma180_data *data = iio_priv(indio_dev);
338 int ret;
339
340 switch (mask) {
341 case IIO_CHAN_INFO_RAW:
342 mutex_lock(&data->mutex);
343 if (iio_buffer_enabled(indio_dev))
344 ret = -EBUSY;
345 else
346 ret = bma180_get_acc_reg(data, chan->scan_index);
347 mutex_unlock(&data->mutex);
348 if (ret < 0)
349 return ret;
350 *val = (s16)ret >> chan->scan_type.shift;
351 return IIO_VAL_INT;
352 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
353 *val = data->bw;
354 return IIO_VAL_INT;
355 case IIO_CHAN_INFO_SCALE:
356 *val = 0;
357 *val2 = data->scale;
358 return IIO_VAL_INT_PLUS_MICRO;
359 default:
360 return -EINVAL;
361 }
362}
363
364static int bma180_write_raw(struct iio_dev *indio_dev,
365 struct iio_chan_spec const *chan, int val, int val2, long mask)
366{
367 struct bma180_data *data = iio_priv(indio_dev);
368 int ret;
369
370 switch (mask) {
371 case IIO_CHAN_INFO_SCALE:
372 if (val)
373 return -EINVAL;
374 mutex_lock(&data->mutex);
375 ret = bma180_set_scale(data, val2);
376 mutex_unlock(&data->mutex);
377 return ret;
378 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
379 mutex_lock(&data->mutex);
380 ret = bma180_set_bw(data, val);
381 mutex_unlock(&data->mutex);
382 return ret;
383 default:
384 return -EINVAL;
385 }
386}
387
388static int bma180_update_scan_mode(struct iio_dev *indio_dev,
389 const unsigned long *scan_mask)
390{
391 struct bma180_data *data = iio_priv(indio_dev);
392
393 if (data->buff)
394 devm_kfree(&indio_dev->dev, data->buff);
395 data->buff = devm_kzalloc(&indio_dev->dev,
396 indio_dev->scan_bytes, GFP_KERNEL);
397 if (!data->buff)
398 return -ENOMEM;
399
400 return 0;
401}
402
403static const struct iio_info bma180_info = {
404 .attrs = &bma180_attrs_group,
405 .read_raw = bma180_read_raw,
406 .write_raw = bma180_write_raw,
407 .update_scan_mode = bma180_update_scan_mode,
408 .driver_module = THIS_MODULE,
409};
410
411static const char * const bma180_power_modes[] = {
412 BMA180_LOW_NOISE_STR,
413 BMA180_LOW_POWER_STR,
414};
415
416static int bma180_get_power_mode(struct iio_dev *indio_dev,
417 const struct iio_chan_spec *chan)
418{
419 struct bma180_data *data = iio_priv(indio_dev);
420
421 return data->pmode;
422}
423
424static int bma180_set_power_mode(struct iio_dev *indio_dev,
425 const struct iio_chan_spec *chan, unsigned int mode)
426{
427 struct bma180_data *data = iio_priv(indio_dev);
428 int ret;
429
430 mutex_lock(&data->mutex);
431 ret = bma180_set_pmode(data, mode);
432 mutex_unlock(&data->mutex);
433
434 return ret;
435}
436
437static const struct iio_enum bma180_power_mode_enum = {
438 .items = bma180_power_modes,
439 .num_items = ARRAY_SIZE(bma180_power_modes),
440 .get = bma180_get_power_mode,
441 .set = bma180_set_power_mode,
442};
443
444static const struct iio_chan_spec_ext_info bma180_ext_info[] = {
445 IIO_ENUM("power_mode", true, &bma180_power_mode_enum),
446 IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum),
447 { },
448};
449
450#define BMA180_CHANNEL(_index) { \
451 .type = IIO_ACCEL, \
452 .indexed = 1, \
453 .channel = (_index), \
454 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
455 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
456 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
457 .scan_index = (_index), \
458 .scan_type = IIO_ST('s', 14, 16, 2), \
459 .ext_info = bma180_ext_info, \
460}
461
462static const struct iio_chan_spec bma180_channels[] = {
463 BMA180_CHANNEL(AXIS_X),
464 BMA180_CHANNEL(AXIS_Y),
465 BMA180_CHANNEL(AXIS_Z),
466 IIO_CHAN_SOFT_TIMESTAMP(4),
467};
468
469static irqreturn_t bma180_trigger_handler(int irq, void *p)
470{
471 struct iio_poll_func *pf = p;
472 struct iio_dev *indio_dev = pf->indio_dev;
473 struct bma180_data *data = iio_priv(indio_dev);
474 int bit, ret, i = 0;
475
476 mutex_lock(&data->mutex);
477 if (indio_dev->scan_timestamp) {
478 ret = indio_dev->scan_bytes / sizeof(s64) - 1;
479 ((s64 *)data->buff)[ret] = iio_get_time_ns();
480 }
481
482 for_each_set_bit(bit, indio_dev->buffer->scan_mask,
483 indio_dev->masklength) {
484 ret = bma180_get_acc_reg(data, bit);
485 if (ret < 0) {
486 mutex_unlock(&data->mutex);
487 goto err;
488 }
489 ((s16 *)data->buff)[i++] = ret;
490 }
491 mutex_unlock(&data->mutex);
492
493 iio_push_to_buffers(indio_dev, (u8 *)data->buff);
494err:
495 iio_trigger_notify_done(indio_dev->trig);
496
497 return IRQ_HANDLED;
498}
499
500static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig,
501 bool state)
502{
503 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
504 struct bma180_data *data = iio_priv(indio_dev);
505
506 return bma180_set_new_data_intr_state(data, state);
507}
508
509static int bma180_trig_try_reen(struct iio_trigger *trig)
510{
511 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
512 struct bma180_data *data = iio_priv(indio_dev);
513
514 return bma180_reset_intr(data);
515}
516
517static const struct iio_trigger_ops bma180_trigger_ops = {
518 .set_trigger_state = bma180_data_rdy_trigger_set_state,
519 .try_reenable = bma180_trig_try_reen,
520 .owner = THIS_MODULE,
521};
522
523static int bma180_probe(struct i2c_client *client,
524 const struct i2c_device_id *id)
525{
526 struct bma180_data *data;
527 struct iio_dev *indio_dev;
528 struct iio_trigger *trig;
529 int ret;
530
531 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
532 if (!indio_dev)
533 return -ENOMEM;
534
535 data = iio_priv(indio_dev);
536 i2c_set_clientdata(client, indio_dev);
537 data->client = client;
538
539 ret = bma180_chip_init(data);
540 if (ret < 0)
541 goto err_chip_disable;
542
543 mutex_init(&data->mutex);
544
545 indio_dev->dev.parent = &client->dev;
546 indio_dev->channels = bma180_channels;
547 indio_dev->num_channels = ARRAY_SIZE(bma180_channels);
548 indio_dev->name = BMA180_DRV_NAME;
549 indio_dev->modes = INDIO_DIRECT_MODE;
550 indio_dev->info = &bma180_info;
551
552 trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, indio_dev->id);
553 if (!trig) {
554 ret = -ENOMEM;
555 goto err_chip_disable;
556 }
557
558 ret = devm_request_irq(&client->dev, client->irq,
559 iio_trigger_generic_data_rdy_poll,
560 IRQF_TRIGGER_RISING, BMA180_IRQ_NAME, trig);
561 if (ret) {
562 dev_err(&client->dev, "unable to request IRQ\n");
563 goto err_trigger_free;
564 }
565
566 trig->dev.parent = &client->dev;
567 trig->ops = &bma180_trigger_ops;
568 iio_trigger_set_drvdata(trig, indio_dev);
569 data->trig = trig;
570 indio_dev->trig = trig;
571
572 ret = iio_trigger_register(trig);
573 if (ret)
574 goto err_trigger_free;
575
576 ret = iio_triggered_buffer_setup(indio_dev, NULL,
577 bma180_trigger_handler, NULL);
578 if (ret < 0) {
579 dev_err(&client->dev, "unable to setup iio triggered buffer\n");
580 goto err_trigger_unregister;
581 }
582
583 ret = iio_device_register(indio_dev);
584 if (ret < 0) {
585 dev_err(&client->dev, "unable to register iio device\n");
586 goto err_buffer_cleanup;
587 }
588
589 return 0;
590
591err_buffer_cleanup:
592 iio_triggered_buffer_cleanup(indio_dev);
593err_trigger_unregister:
594 iio_trigger_unregister(trig);
595err_trigger_free:
596 iio_trigger_free(trig);
597err_chip_disable:
598 bma180_chip_disable(data);
599
600 return ret;
601}
602
603static int bma180_remove(struct i2c_client *client)
604{
605 struct iio_dev *indio_dev = i2c_get_clientdata(client);
606 struct bma180_data *data = iio_priv(indio_dev);
607
608 iio_device_unregister(indio_dev);
609 iio_triggered_buffer_cleanup(indio_dev);
610 iio_trigger_unregister(data->trig);
611 iio_trigger_free(data->trig);
612
613 mutex_lock(&data->mutex);
614 bma180_chip_disable(data);
615 mutex_unlock(&data->mutex);
616
617 return 0;
618}
619
620#ifdef CONFIG_PM_SLEEP
621static int bma180_suspend(struct device *dev)
622{
623 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
624 struct bma180_data *data = iio_priv(indio_dev);
625 int ret;
626
627 mutex_lock(&data->mutex);
628 ret = bma180_set_sleep_state(data, 1);
629 mutex_unlock(&data->mutex);
630
631 return ret;
632}
633
634static int bma180_resume(struct device *dev)
635{
636 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
637 struct bma180_data *data = iio_priv(indio_dev);
638 int ret;
639
640 mutex_lock(&data->mutex);
641 ret = bma180_set_sleep_state(data, 0);
642 mutex_unlock(&data->mutex);
643
644 return ret;
645}
646
647static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume);
648#define BMA180_PM_OPS (&bma180_pm_ops)
649#else
650#define BMA180_PM_OPS NULL
651#endif
652
653static struct i2c_device_id bma180_id[] = {
654 { BMA180_DRV_NAME, 0 },
655 { }
656};
657
658MODULE_DEVICE_TABLE(i2c, bma180_id);
659
660static struct i2c_driver bma180_driver = {
661 .driver = {
662 .name = BMA180_DRV_NAME,
663 .owner = THIS_MODULE,
664 .pm = BMA180_PM_OPS,
665 },
666 .probe = bma180_probe,
667 .remove = bma180_remove,
668 .id_table = bma180_id,
669};
670
671module_i2c_driver(bma180_driver);
672
673MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>");
674MODULE_AUTHOR("Texas Instruments, Inc.");
675MODULE_DESCRIPTION("Bosch BMA180 triaxial acceleration sensor");
676MODULE_LICENSE("GPL");
diff --git a/drivers/iio/accel/hid-sensor-accel-3d.c b/drivers/iio/accel/hid-sensor-accel-3d.c
index bbcbd7101f33..46d22f3fb1a9 100644
--- a/drivers/iio/accel/hid-sensor-accel-3d.c
+++ b/drivers/iio/accel/hid-sensor-accel-3d.c
@@ -30,10 +30,6 @@
30#include <linux/iio/triggered_buffer.h> 30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-trigger.h" 31#include "../common/hid-sensors/hid-sensor-trigger.h"
32 32
33/*Format: HID-SENSOR-usage_id_in_hex*/
34/*Usage ID from spec for Accelerometer-3D: 0x200073*/
35#define DRIVER_NAME "HID-SENSOR-200073"
36
37enum accel_3d_channel { 33enum accel_3d_channel {
38 CHANNEL_SCAN_INDEX_X, 34 CHANNEL_SCAN_INDEX_X,
39 CHANNEL_SCAN_INDEX_Y, 35 CHANNEL_SCAN_INDEX_Y,
@@ -179,18 +175,10 @@ static int accel_3d_write_raw(struct iio_dev *indio_dev,
179 return ret; 175 return ret;
180} 176}
181 177
182static int accel_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
183 struct iio_chan_spec const *chan,
184 long mask)
185{
186 return IIO_VAL_INT_PLUS_MICRO;
187}
188
189static const struct iio_info accel_3d_info = { 178static const struct iio_info accel_3d_info = {
190 .driver_module = THIS_MODULE, 179 .driver_module = THIS_MODULE,
191 .read_raw = &accel_3d_read_raw, 180 .read_raw = &accel_3d_read_raw,
192 .write_raw = &accel_3d_write_raw, 181 .write_raw = &accel_3d_write_raw,
193 .write_raw_get_fmt = &accel_3d_write_raw_get_fmt,
194}; 182};
195 183
196/* Function to push data to buffer */ 184/* Function to push data to buffer */
@@ -286,11 +274,11 @@ static int hid_accel_3d_probe(struct platform_device *pdev)
286 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 274 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
287 struct iio_chan_spec *channels; 275 struct iio_chan_spec *channels;
288 276
289 indio_dev = iio_device_alloc(sizeof(struct accel_3d_state)); 277 indio_dev = devm_iio_device_alloc(&pdev->dev,
290 if (indio_dev == NULL) { 278 sizeof(struct accel_3d_state));
291 ret = -ENOMEM; 279 if (indio_dev == NULL)
292 goto error_ret; 280 return -ENOMEM;
293 } 281
294 platform_set_drvdata(pdev, indio_dev); 282 platform_set_drvdata(pdev, indio_dev);
295 283
296 accel_state = iio_priv(indio_dev); 284 accel_state = iio_priv(indio_dev);
@@ -302,15 +290,14 @@ static int hid_accel_3d_probe(struct platform_device *pdev)
302 &accel_state->common_attributes); 290 &accel_state->common_attributes);
303 if (ret) { 291 if (ret) {
304 dev_err(&pdev->dev, "failed to setup common attributes\n"); 292 dev_err(&pdev->dev, "failed to setup common attributes\n");
305 goto error_free_dev; 293 return ret;
306 } 294 }
307 295
308 channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels), 296 channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels),
309 GFP_KERNEL); 297 GFP_KERNEL);
310 if (!channels) { 298 if (!channels) {
311 ret = -ENOMEM;
312 dev_err(&pdev->dev, "failed to duplicate channels\n"); 299 dev_err(&pdev->dev, "failed to duplicate channels\n");
313 goto error_free_dev; 300 return -ENOMEM;
314 } 301 }
315 302
316 ret = accel_3d_parse_report(pdev, hsdev, channels, 303 ret = accel_3d_parse_report(pdev, hsdev, channels,
@@ -367,9 +354,6 @@ error_unreg_buffer_funcs:
367 iio_triggered_buffer_cleanup(indio_dev); 354 iio_triggered_buffer_cleanup(indio_dev);
368error_free_dev_mem: 355error_free_dev_mem:
369 kfree(indio_dev->channels); 356 kfree(indio_dev->channels);
370error_free_dev:
371 iio_device_free(indio_dev);
372error_ret:
373 return ret; 357 return ret;
374} 358}
375 359
@@ -384,14 +368,23 @@ static int hid_accel_3d_remove(struct platform_device *pdev)
384 hid_sensor_remove_trigger(indio_dev); 368 hid_sensor_remove_trigger(indio_dev);
385 iio_triggered_buffer_cleanup(indio_dev); 369 iio_triggered_buffer_cleanup(indio_dev);
386 kfree(indio_dev->channels); 370 kfree(indio_dev->channels);
387 iio_device_free(indio_dev);
388 371
389 return 0; 372 return 0;
390} 373}
391 374
375static struct platform_device_id hid_accel_3d_ids[] = {
376 {
377 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
378 .name = "HID-SENSOR-200073",
379 },
380 { /* sentinel */ }
381};
382MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
383
392static struct platform_driver hid_accel_3d_platform_driver = { 384static struct platform_driver hid_accel_3d_platform_driver = {
385 .id_table = hid_accel_3d_ids,
393 .driver = { 386 .driver = {
394 .name = DRIVER_NAME, 387 .name = KBUILD_MODNAME,
395 .owner = THIS_MODULE, 388 .owner = THIS_MODULE,
396 }, 389 },
397 .probe = hid_accel_3d_probe, 390 .probe = hid_accel_3d_probe,
diff --git a/drivers/iio/accel/kxsd9.c b/drivers/iio/accel/kxsd9.c
index 7229645bf1d7..709c13259f14 100644
--- a/drivers/iio/accel/kxsd9.c
+++ b/drivers/iio/accel/kxsd9.c
@@ -224,11 +224,10 @@ static int kxsd9_probe(struct spi_device *spi)
224 struct kxsd9_state *st; 224 struct kxsd9_state *st;
225 int ret; 225 int ret;
226 226
227 indio_dev = iio_device_alloc(sizeof(*st)); 227 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
228 if (indio_dev == NULL) { 228 if (!indio_dev)
229 ret = -ENOMEM; 229 return -ENOMEM;
230 goto error_ret; 230
231 }
232 st = iio_priv(indio_dev); 231 st = iio_priv(indio_dev);
233 spi_set_drvdata(spi, indio_dev); 232 spi_set_drvdata(spi, indio_dev);
234 233
@@ -247,20 +246,14 @@ static int kxsd9_probe(struct spi_device *spi)
247 246
248 ret = iio_device_register(indio_dev); 247 ret = iio_device_register(indio_dev);
249 if (ret) 248 if (ret)
250 goto error_free_dev; 249 return ret;
251 250
252 return 0; 251 return 0;
253
254error_free_dev:
255 iio_device_free(indio_dev);
256error_ret:
257 return ret;
258} 252}
259 253
260static int kxsd9_remove(struct spi_device *spi) 254static int kxsd9_remove(struct spi_device *spi)
261{ 255{
262 iio_device_unregister(spi_get_drvdata(spi)); 256 iio_device_unregister(spi_get_drvdata(spi));
263 iio_device_free(spi_get_drvdata(spi));
264 257
265 return 0; 258 return 0;
266} 259}
diff --git a/drivers/iio/accel/st_accel.h b/drivers/iio/accel/st_accel.h
index 37949b94377d..c3877630b2e4 100644
--- a/drivers/iio/accel/st_accel.h
+++ b/drivers/iio/accel/st_accel.h
@@ -25,7 +25,16 @@
25#define LSM303DLM_ACCEL_DEV_NAME "lsm303dlm_accel" 25#define LSM303DLM_ACCEL_DEV_NAME "lsm303dlm_accel"
26#define LSM330_ACCEL_DEV_NAME "lsm330_accel" 26#define LSM330_ACCEL_DEV_NAME "lsm330_accel"
27 27
28int st_accel_common_probe(struct iio_dev *indio_dev); 28/**
29* struct st_sensors_platform_data - default accel platform data
30* @drdy_int_pin: default accel DRDY is available on INT1 pin.
31*/
32static const struct st_sensors_platform_data default_accel_pdata = {
33 .drdy_int_pin = 1,
34};
35
36int st_accel_common_probe(struct iio_dev *indio_dev,
37 struct st_sensors_platform_data *pdata);
29void st_accel_common_remove(struct iio_dev *indio_dev); 38void st_accel_common_remove(struct iio_dev *indio_dev);
30 39
31#ifdef CONFIG_IIO_BUFFER 40#ifdef CONFIG_IIO_BUFFER
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index 4aec121261d7..1458343f6f3f 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -65,7 +65,8 @@
65#define ST_ACCEL_1_BDU_ADDR 0x23 65#define ST_ACCEL_1_BDU_ADDR 0x23
66#define ST_ACCEL_1_BDU_MASK 0x80 66#define ST_ACCEL_1_BDU_MASK 0x80
67#define ST_ACCEL_1_DRDY_IRQ_ADDR 0x22 67#define ST_ACCEL_1_DRDY_IRQ_ADDR 0x22
68#define ST_ACCEL_1_DRDY_IRQ_MASK 0x10 68#define ST_ACCEL_1_DRDY_IRQ_INT1_MASK 0x10
69#define ST_ACCEL_1_DRDY_IRQ_INT2_MASK 0x08
69#define ST_ACCEL_1_MULTIREAD_BIT true 70#define ST_ACCEL_1_MULTIREAD_BIT true
70 71
71/* CUSTOM VALUES FOR SENSOR 2 */ 72/* CUSTOM VALUES FOR SENSOR 2 */
@@ -89,7 +90,8 @@
89#define ST_ACCEL_2_BDU_ADDR 0x23 90#define ST_ACCEL_2_BDU_ADDR 0x23
90#define ST_ACCEL_2_BDU_MASK 0x80 91#define ST_ACCEL_2_BDU_MASK 0x80
91#define ST_ACCEL_2_DRDY_IRQ_ADDR 0x22 92#define ST_ACCEL_2_DRDY_IRQ_ADDR 0x22
92#define ST_ACCEL_2_DRDY_IRQ_MASK 0x02 93#define ST_ACCEL_2_DRDY_IRQ_INT1_MASK 0x02
94#define ST_ACCEL_2_DRDY_IRQ_INT2_MASK 0x10
93#define ST_ACCEL_2_MULTIREAD_BIT true 95#define ST_ACCEL_2_MULTIREAD_BIT true
94 96
95/* CUSTOM VALUES FOR SENSOR 3 */ 97/* CUSTOM VALUES FOR SENSOR 3 */
@@ -121,7 +123,8 @@
121#define ST_ACCEL_3_BDU_ADDR 0x20 123#define ST_ACCEL_3_BDU_ADDR 0x20
122#define ST_ACCEL_3_BDU_MASK 0x08 124#define ST_ACCEL_3_BDU_MASK 0x08
123#define ST_ACCEL_3_DRDY_IRQ_ADDR 0x23 125#define ST_ACCEL_3_DRDY_IRQ_ADDR 0x23
124#define ST_ACCEL_3_DRDY_IRQ_MASK 0x80 126#define ST_ACCEL_3_DRDY_IRQ_INT1_MASK 0x80
127#define ST_ACCEL_3_DRDY_IRQ_INT2_MASK 0x00
125#define ST_ACCEL_3_IG1_EN_ADDR 0x23 128#define ST_ACCEL_3_IG1_EN_ADDR 0x23
126#define ST_ACCEL_3_IG1_EN_MASK 0x08 129#define ST_ACCEL_3_IG1_EN_MASK 0x08
127#define ST_ACCEL_3_MULTIREAD_BIT false 130#define ST_ACCEL_3_MULTIREAD_BIT false
@@ -224,7 +227,8 @@ static const struct st_sensors st_accel_sensors[] = {
224 }, 227 },
225 .drdy_irq = { 228 .drdy_irq = {
226 .addr = ST_ACCEL_1_DRDY_IRQ_ADDR, 229 .addr = ST_ACCEL_1_DRDY_IRQ_ADDR,
227 .mask = ST_ACCEL_1_DRDY_IRQ_MASK, 230 .mask_int1 = ST_ACCEL_1_DRDY_IRQ_INT1_MASK,
231 .mask_int2 = ST_ACCEL_1_DRDY_IRQ_INT2_MASK,
228 }, 232 },
229 .multi_read_bit = ST_ACCEL_1_MULTIREAD_BIT, 233 .multi_read_bit = ST_ACCEL_1_MULTIREAD_BIT,
230 .bootime = 2, 234 .bootime = 2,
@@ -285,7 +289,8 @@ static const struct st_sensors st_accel_sensors[] = {
285 }, 289 },
286 .drdy_irq = { 290 .drdy_irq = {
287 .addr = ST_ACCEL_2_DRDY_IRQ_ADDR, 291 .addr = ST_ACCEL_2_DRDY_IRQ_ADDR,
288 .mask = ST_ACCEL_2_DRDY_IRQ_MASK, 292 .mask_int1 = ST_ACCEL_2_DRDY_IRQ_INT1_MASK,
293 .mask_int2 = ST_ACCEL_2_DRDY_IRQ_INT2_MASK,
289 }, 294 },
290 .multi_read_bit = ST_ACCEL_2_MULTIREAD_BIT, 295 .multi_read_bit = ST_ACCEL_2_MULTIREAD_BIT,
291 .bootime = 2, 296 .bootime = 2,
@@ -358,7 +363,8 @@ static const struct st_sensors st_accel_sensors[] = {
358 }, 363 },
359 .drdy_irq = { 364 .drdy_irq = {
360 .addr = ST_ACCEL_3_DRDY_IRQ_ADDR, 365 .addr = ST_ACCEL_3_DRDY_IRQ_ADDR,
361 .mask = ST_ACCEL_3_DRDY_IRQ_MASK, 366 .mask_int1 = ST_ACCEL_3_DRDY_IRQ_INT1_MASK,
367 .mask_int2 = ST_ACCEL_3_DRDY_IRQ_INT2_MASK,
362 .ig1 = { 368 .ig1 = {
363 .en_addr = ST_ACCEL_3_IG1_EN_ADDR, 369 .en_addr = ST_ACCEL_3_IG1_EN_ADDR,
364 .en_mask = ST_ACCEL_3_IG1_EN_MASK, 370 .en_mask = ST_ACCEL_3_IG1_EN_MASK,
@@ -443,7 +449,8 @@ static const struct iio_trigger_ops st_accel_trigger_ops = {
443#define ST_ACCEL_TRIGGER_OPS NULL 449#define ST_ACCEL_TRIGGER_OPS NULL
444#endif 450#endif
445 451
446int st_accel_common_probe(struct iio_dev *indio_dev) 452int st_accel_common_probe(struct iio_dev *indio_dev,
453 struct st_sensors_platform_data *plat_data)
447{ 454{
448 int err; 455 int err;
449 struct st_sensor_data *adata = iio_priv(indio_dev); 456 struct st_sensor_data *adata = iio_priv(indio_dev);
@@ -465,7 +472,11 @@ int st_accel_common_probe(struct iio_dev *indio_dev)
465 &adata->sensor->fs.fs_avl[0]; 472 &adata->sensor->fs.fs_avl[0];
466 adata->odr = adata->sensor->odr.odr_avl[0].hz; 473 adata->odr = adata->sensor->odr.odr_avl[0].hz;
467 474
468 err = st_sensors_init_sensor(indio_dev); 475 if (!plat_data)
476 plat_data =
477 (struct st_sensors_platform_data *)&default_accel_pdata;
478
479 err = st_sensors_init_sensor(indio_dev, plat_data);
469 if (err < 0) 480 if (err < 0)
470 goto st_accel_common_probe_error; 481 goto st_accel_common_probe_error;
471 482
@@ -506,7 +517,6 @@ void st_accel_common_remove(struct iio_dev *indio_dev)
506 st_sensors_deallocate_trigger(indio_dev); 517 st_sensors_deallocate_trigger(indio_dev);
507 st_accel_deallocate_ring(indio_dev); 518 st_accel_deallocate_ring(indio_dev);
508 } 519 }
509 iio_device_free(indio_dev);
510} 520}
511EXPORT_SYMBOL(st_accel_common_remove); 521EXPORT_SYMBOL(st_accel_common_remove);
512 522
diff --git a/drivers/iio/accel/st_accel_i2c.c b/drivers/iio/accel/st_accel_i2c.c
index ffc9d097e484..d7bedbdfc81d 100644
--- a/drivers/iio/accel/st_accel_i2c.c
+++ b/drivers/iio/accel/st_accel_i2c.c
@@ -25,27 +25,20 @@ static int st_accel_i2c_probe(struct i2c_client *client,
25 struct st_sensor_data *adata; 25 struct st_sensor_data *adata;
26 int err; 26 int err;
27 27
28 indio_dev = iio_device_alloc(sizeof(*adata)); 28 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adata));
29 if (indio_dev == NULL) { 29 if (!indio_dev)
30 err = -ENOMEM; 30 return -ENOMEM;
31 goto iio_device_alloc_error;
32 }
33 31
34 adata = iio_priv(indio_dev); 32 adata = iio_priv(indio_dev);
35 adata->dev = &client->dev; 33 adata->dev = &client->dev;
36 34
37 st_sensors_i2c_configure(indio_dev, client, adata); 35 st_sensors_i2c_configure(indio_dev, client, adata);
38 36
39 err = st_accel_common_probe(indio_dev); 37 err = st_accel_common_probe(indio_dev, client->dev.platform_data);
40 if (err < 0) 38 if (err < 0)
41 goto st_accel_common_probe_error; 39 return err;
42 40
43 return 0; 41 return 0;
44
45st_accel_common_probe_error:
46 iio_device_free(indio_dev);
47iio_device_alloc_error:
48 return err;
49} 42}
50 43
51static int st_accel_i2c_remove(struct i2c_client *client) 44static int st_accel_i2c_remove(struct i2c_client *client)
diff --git a/drivers/iio/accel/st_accel_spi.c b/drivers/iio/accel/st_accel_spi.c
index 22b35bfea7d2..195639646e34 100644
--- a/drivers/iio/accel/st_accel_spi.c
+++ b/drivers/iio/accel/st_accel_spi.c
@@ -24,27 +24,20 @@ static int st_accel_spi_probe(struct spi_device *spi)
24 struct st_sensor_data *adata; 24 struct st_sensor_data *adata;
25 int err; 25 int err;
26 26
27 indio_dev = iio_device_alloc(sizeof(*adata)); 27 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adata));
28 if (indio_dev == NULL) { 28 if (!indio_dev)
29 err = -ENOMEM; 29 return -ENOMEM;
30 goto iio_device_alloc_error;
31 }
32 30
33 adata = iio_priv(indio_dev); 31 adata = iio_priv(indio_dev);
34 adata->dev = &spi->dev; 32 adata->dev = &spi->dev;
35 33
36 st_sensors_spi_configure(indio_dev, spi, adata); 34 st_sensors_spi_configure(indio_dev, spi, adata);
37 35
38 err = st_accel_common_probe(indio_dev); 36 err = st_accel_common_probe(indio_dev, spi->dev.platform_data);
39 if (err < 0) 37 if (err < 0)
40 goto st_accel_common_probe_error; 38 return err;
41 39
42 return 0; 40 return 0;
43
44st_accel_common_probe_error:
45 iio_device_free(indio_dev);
46iio_device_alloc_error:
47 return err;
48} 41}
49 42
50static int st_accel_spi_remove(struct spi_device *spi) 43static int st_accel_spi_remove(struct spi_device *spi)
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 93129ec4b649..09371cbc9dc1 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1,6 +1,8 @@
1# 1#
2# ADC drivers 2# ADC drivers
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Analog to digital converters" 6menu "Analog to digital converters"
5 7
6config AD_SIGMA_DELTA 8config AD_SIGMA_DELTA
@@ -30,17 +32,20 @@ config AD7298
30 To compile this driver as a module, choose M here: the 32 To compile this driver as a module, choose M here: the
31 module will be called ad7298. 33 module will be called ad7298.
32 34
33config AD7923 35config AD7476
34 tristate "Analog Devices AD7923 and similar ADCs driver" 36 tristate "Analog Devices AD7476 and similar 1-channel ADCs driver"
35 depends on SPI 37 depends on SPI
36 select IIO_BUFFER 38 select IIO_BUFFER
37 select IIO_TRIGGERED_BUFFER 39 select IIO_TRIGGERED_BUFFER
38 help 40 help
39 Say yes here to build support for Analog Devices 41 Say yes here to build support for Analog Devices AD7273, AD7274, AD7276,
40 AD7904, AD7914, AD7923, AD7924 4 Channel ADCs. 42 AD7277, AD7278, AD7475, AD7476, AD7477, AD7478, AD7466, AD7467, AD7468,
43 AD7495, AD7910, AD7920, AD7920 SPI analog to digital converters (ADC).
44
45 If unsure, say N (but it's safe to say "Y").
41 46
42 To compile this driver as a module, choose M here: the 47 To compile this driver as a module, choose M here: the
43 module will be called ad7923. 48 module will be called ad7476.
44 49
45config AD7791 50config AD7791
46 tristate "Analog Devices AD7791 ADC driver" 51 tristate "Analog Devices AD7791 ADC driver"
@@ -66,33 +71,30 @@ config AD7793
66 To compile this driver as a module, choose M here: the 71 To compile this driver as a module, choose M here: the
67 module will be called AD7793. 72 module will be called AD7793.
68 73
69config AD7476 74config AD7887
70 tristate "Analog Devices AD7476 and similar 1-channel ADCs driver" 75 tristate "Analog Devices AD7887 ADC driver"
71 depends on SPI 76 depends on SPI
72 select IIO_BUFFER 77 select IIO_BUFFER
73 select IIO_TRIGGERED_BUFFER 78 select IIO_TRIGGERED_BUFFER
74 help 79 help
75 Say yes here to build support for Analog Devices AD7273, AD7274, AD7276, 80 Say yes here to build support for Analog Devices
76 AD7277, AD7278, AD7475, AD7476, AD7477, AD7478, AD7466, AD7467, AD7468, 81 AD7887 SPI analog to digital converter (ADC).
77 AD7495, AD7910, AD7920, AD7920 SPI analog to digital converters (ADC).
78
79 If unsure, say N (but it's safe to say "Y"). 82 If unsure, say N (but it's safe to say "Y").
80 83
81 To compile this driver as a module, choose M here: the 84 To compile this driver as a module, choose M here: the
82 module will be called ad7476. 85 module will be called ad7887.
83 86
84config AD7887 87config AD7923
85 tristate "Analog Devices AD7887 ADC driver" 88 tristate "Analog Devices AD7923 and similar ADCs driver"
86 depends on SPI 89 depends on SPI
87 select IIO_BUFFER 90 select IIO_BUFFER
88 select IIO_TRIGGERED_BUFFER 91 select IIO_TRIGGERED_BUFFER
89 help 92 help
90 Say yes here to build support for Analog Devices 93 Say yes here to build support for Analog Devices
91 AD7887 SPI analog to digital converter (ADC). 94 AD7904, AD7914, AD7923, AD7924 4 Channel ADCs.
92 If unsure, say N (but it's safe to say "Y").
93 95
94 To compile this driver as a module, choose M here: the 96 To compile this driver as a module, choose M here: the
95 module will be called ad7887. 97 module will be called ad7923.
96 98
97config AT91_ADC 99config AT91_ADC
98 tristate "Atmel AT91 ADC" 100 tristate "Atmel AT91 ADC"
@@ -143,6 +145,15 @@ config MCP320X
143 This driver can also be built as a module. If so, the module will be 145 This driver can also be built as a module. If so, the module will be
144 called mcp320x. 146 called mcp320x.
145 147
148config NAU7802
149 tristate "Nuvoton NAU7802 ADC driver"
150 depends on I2C
151 help
152 Say yes here to build support for Nuvoton NAU7802 ADC.
153
154 To compile this driver as a module, choose M here: the
155 module will be called nau7802.
156
146config TI_ADC081C 157config TI_ADC081C
147 tristate "Texas Instruments ADC081C021/027" 158 tristate "Texas Instruments ADC081C021/027"
148 depends on I2C 159 depends on I2C
@@ -154,12 +165,26 @@ config TI_ADC081C
154 called ti-adc081c. 165 called ti-adc081c.
155 166
156config TI_AM335X_ADC 167config TI_AM335X_ADC
157 tristate "TI's ADC driver" 168 tristate "TI's AM335X ADC driver"
158 depends on MFD_TI_AM335X_TSCADC 169 depends on MFD_TI_AM335X_TSCADC
159 help 170 help
160 Say yes here to build support for Texas Instruments ADC 171 Say yes here to build support for Texas Instruments ADC
161 driver which is also a MFD client. 172 driver which is also a MFD client.
162 173
174config TWL6030_GPADC
175 tristate "TWL6030 GPADC (General Purpose A/D Converter) Support"
176 depends on TWL4030_CORE
177 default n
178 help
179 Say yes here if you want support for the TWL6030/TWL6032 General
180 Purpose A/D Converter. This will add support for battery type
181 detection, battery voltage and temperature measurement, die
182 temperature measurement, system supply voltage, audio accessory,
183 USB ID detection.
184
185 This driver can also be built as a module. If so, the module will be
186 called twl6030-gpadc.
187
163config VIPERBOARD_ADC 188config VIPERBOARD_ADC
164 tristate "Viperboard ADC support" 189 tristate "Viperboard ADC support"
165 depends on MFD_VIPERBOARD && USB 190 depends on MFD_VIPERBOARD && USB
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 8f475d31fe4d..33656ef7d1f6 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -2,6 +2,7 @@
2# Makefile for IIO ADC drivers 2# Makefile for IIO ADC drivers
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o 6obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o
6obj-$(CONFIG_AD7266) += ad7266.o 7obj-$(CONFIG_AD7266) += ad7266.o
7obj-$(CONFIG_AD7298) += ad7298.o 8obj-$(CONFIG_AD7298) += ad7298.o
@@ -15,6 +16,8 @@ obj-$(CONFIG_EXYNOS_ADC) += exynos_adc.o
15obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o 16obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
16obj-$(CONFIG_MAX1363) += max1363.o 17obj-$(CONFIG_MAX1363) += max1363.o
17obj-$(CONFIG_MCP320X) += mcp320x.o 18obj-$(CONFIG_MCP320X) += mcp320x.o
19obj-$(CONFIG_NAU7802) += nau7802.o
18obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o 20obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
19obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o 21obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
22obj-$(CONFIG_TWL6030_GPADC) += twl6030-gpadc.o
20obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o 23obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
diff --git a/drivers/iio/adc/ad7266.c b/drivers/iio/adc/ad7266.c
index c2744a75c3b0..371731df1634 100644
--- a/drivers/iio/adc/ad7266.c
+++ b/drivers/iio/adc/ad7266.c
@@ -399,17 +399,17 @@ static int ad7266_probe(struct spi_device *spi)
399 unsigned int i; 399 unsigned int i;
400 int ret; 400 int ret;
401 401
402 indio_dev = iio_device_alloc(sizeof(*st)); 402 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
403 if (indio_dev == NULL) 403 if (indio_dev == NULL)
404 return -ENOMEM; 404 return -ENOMEM;
405 405
406 st = iio_priv(indio_dev); 406 st = iio_priv(indio_dev);
407 407
408 st->reg = regulator_get(&spi->dev, "vref"); 408 st->reg = devm_regulator_get(&spi->dev, "vref");
409 if (!IS_ERR_OR_NULL(st->reg)) { 409 if (!IS_ERR_OR_NULL(st->reg)) {
410 ret = regulator_enable(st->reg); 410 ret = regulator_enable(st->reg);
411 if (ret) 411 if (ret)
412 goto error_put_reg; 412 return ret;
413 413
414 ret = regulator_get_voltage(st->reg); 414 ret = regulator_get_voltage(st->reg);
415 if (ret < 0) 415 if (ret < 0)
@@ -489,11 +489,6 @@ error_free_gpios:
489error_disable_reg: 489error_disable_reg:
490 if (!IS_ERR_OR_NULL(st->reg)) 490 if (!IS_ERR_OR_NULL(st->reg))
491 regulator_disable(st->reg); 491 regulator_disable(st->reg);
492error_put_reg:
493 if (!IS_ERR_OR_NULL(st->reg))
494 regulator_put(st->reg);
495
496 iio_device_free(indio_dev);
497 492
498 return ret; 493 return ret;
499} 494}
@@ -507,11 +502,8 @@ static int ad7266_remove(struct spi_device *spi)
507 iio_triggered_buffer_cleanup(indio_dev); 502 iio_triggered_buffer_cleanup(indio_dev);
508 if (!st->fixed_addr) 503 if (!st->fixed_addr)
509 gpio_free_array(st->gpios, ARRAY_SIZE(st->gpios)); 504 gpio_free_array(st->gpios, ARRAY_SIZE(st->gpios));
510 if (!IS_ERR_OR_NULL(st->reg)) { 505 if (!IS_ERR_OR_NULL(st->reg))
511 regulator_disable(st->reg); 506 regulator_disable(st->reg);
512 regulator_put(st->reg);
513 }
514 iio_device_free(indio_dev);
515 507
516 return 0; 508 return 0;
517} 509}
diff --git a/drivers/iio/adc/ad7298.c b/drivers/iio/adc/ad7298.c
index 03b77189dbfe..85d1481c312f 100644
--- a/drivers/iio/adc/ad7298.c
+++ b/drivers/iio/adc/ad7298.c
@@ -296,9 +296,10 @@ static int ad7298_probe(struct spi_device *spi)
296{ 296{
297 struct ad7298_platform_data *pdata = spi->dev.platform_data; 297 struct ad7298_platform_data *pdata = spi->dev.platform_data;
298 struct ad7298_state *st; 298 struct ad7298_state *st;
299 struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); 299 struct iio_dev *indio_dev;
300 int ret; 300 int ret;
301 301
302 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
302 if (indio_dev == NULL) 303 if (indio_dev == NULL)
303 return -ENOMEM; 304 return -ENOMEM;
304 305
@@ -308,14 +309,13 @@ static int ad7298_probe(struct spi_device *spi)
308 st->ext_ref = AD7298_EXTREF; 309 st->ext_ref = AD7298_EXTREF;
309 310
310 if (st->ext_ref) { 311 if (st->ext_ref) {
311 st->reg = regulator_get(&spi->dev, "vref"); 312 st->reg = devm_regulator_get(&spi->dev, "vref");
312 if (IS_ERR(st->reg)) { 313 if (IS_ERR(st->reg))
313 ret = PTR_ERR(st->reg); 314 return PTR_ERR(st->reg);
314 goto error_free; 315
315 }
316 ret = regulator_enable(st->reg); 316 ret = regulator_enable(st->reg);
317 if (ret) 317 if (ret)
318 goto error_put_reg; 318 return ret;
319 } 319 }
320 320
321 spi_set_drvdata(spi, indio_dev); 321 spi_set_drvdata(spi, indio_dev);
@@ -361,11 +361,6 @@ error_cleanup_ring:
361error_disable_reg: 361error_disable_reg:
362 if (st->ext_ref) 362 if (st->ext_ref)
363 regulator_disable(st->reg); 363 regulator_disable(st->reg);
364error_put_reg:
365 if (st->ext_ref)
366 regulator_put(st->reg);
367error_free:
368 iio_device_free(indio_dev);
369 364
370 return ret; 365 return ret;
371} 366}
@@ -377,11 +372,8 @@ static int ad7298_remove(struct spi_device *spi)
377 372
378 iio_device_unregister(indio_dev); 373 iio_device_unregister(indio_dev);
379 iio_triggered_buffer_cleanup(indio_dev); 374 iio_triggered_buffer_cleanup(indio_dev);
380 if (st->ext_ref) { 375 if (st->ext_ref)
381 regulator_disable(st->reg); 376 regulator_disable(st->reg);
382 regulator_put(st->reg);
383 }
384 iio_device_free(indio_dev);
385 377
386 return 0; 378 return 0;
387} 379}
diff --git a/drivers/iio/adc/ad7476.c b/drivers/iio/adc/ad7476.c
index 2e98bef4af67..6d2b1d8d1a1f 100644
--- a/drivers/iio/adc/ad7476.c
+++ b/drivers/iio/adc/ad7476.c
@@ -213,24 +213,21 @@ static int ad7476_probe(struct spi_device *spi)
213 struct iio_dev *indio_dev; 213 struct iio_dev *indio_dev;
214 int ret; 214 int ret;
215 215
216 indio_dev = iio_device_alloc(sizeof(*st)); 216 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
217 if (indio_dev == NULL) { 217 if (!indio_dev)
218 ret = -ENOMEM; 218 return -ENOMEM;
219 goto error_ret; 219
220 }
221 st = iio_priv(indio_dev); 220 st = iio_priv(indio_dev);
222 st->chip_info = 221 st->chip_info =
223 &ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data]; 222 &ad7476_chip_info_tbl[spi_get_device_id(spi)->driver_data];
224 223
225 st->reg = regulator_get(&spi->dev, "vcc"); 224 st->reg = devm_regulator_get(&spi->dev, "vcc");
226 if (IS_ERR(st->reg)) { 225 if (IS_ERR(st->reg))
227 ret = PTR_ERR(st->reg); 226 return PTR_ERR(st->reg);
228 goto error_free_dev;
229 }
230 227
231 ret = regulator_enable(st->reg); 228 ret = regulator_enable(st->reg);
232 if (ret) 229 if (ret)
233 goto error_put_reg; 230 return ret;
234 231
235 spi_set_drvdata(spi, indio_dev); 232 spi_set_drvdata(spi, indio_dev);
236 233
@@ -268,12 +265,7 @@ error_ring_unregister:
268 iio_triggered_buffer_cleanup(indio_dev); 265 iio_triggered_buffer_cleanup(indio_dev);
269error_disable_reg: 266error_disable_reg:
270 regulator_disable(st->reg); 267 regulator_disable(st->reg);
271error_put_reg:
272 regulator_put(st->reg);
273error_free_dev:
274 iio_device_free(indio_dev);
275 268
276error_ret:
277 return ret; 269 return ret;
278} 270}
279 271
@@ -285,8 +277,6 @@ static int ad7476_remove(struct spi_device *spi)
285 iio_device_unregister(indio_dev); 277 iio_device_unregister(indio_dev);
286 iio_triggered_buffer_cleanup(indio_dev); 278 iio_triggered_buffer_cleanup(indio_dev);
287 regulator_disable(st->reg); 279 regulator_disable(st->reg);
288 regulator_put(st->reg);
289 iio_device_free(indio_dev);
290 280
291 return 0; 281 return 0;
292} 282}
diff --git a/drivers/iio/adc/ad7791.c b/drivers/iio/adc/ad7791.c
index 5e8d1da6887f..c20203577d2d 100644
--- a/drivers/iio/adc/ad7791.c
+++ b/drivers/iio/adc/ad7791.c
@@ -361,21 +361,19 @@ static int ad7791_probe(struct spi_device *spi)
361 return -ENXIO; 361 return -ENXIO;
362 } 362 }
363 363
364 indio_dev = iio_device_alloc(sizeof(*st)); 364 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
365 if (!indio_dev) 365 if (!indio_dev)
366 return -ENOMEM; 366 return -ENOMEM;
367 367
368 st = iio_priv(indio_dev); 368 st = iio_priv(indio_dev);
369 369
370 st->reg = regulator_get(&spi->dev, "refin"); 370 st->reg = devm_regulator_get(&spi->dev, "refin");
371 if (IS_ERR(st->reg)) { 371 if (IS_ERR(st->reg))
372 ret = PTR_ERR(st->reg); 372 return PTR_ERR(st->reg);
373 goto err_iio_free;
374 }
375 373
376 ret = regulator_enable(st->reg); 374 ret = regulator_enable(st->reg);
377 if (ret) 375 if (ret)
378 goto error_put_reg; 376 return ret;
379 377
380 st->info = &ad7791_chip_infos[spi_get_device_id(spi)->driver_data]; 378 st->info = &ad7791_chip_infos[spi_get_device_id(spi)->driver_data];
381 ad_sd_init(&st->sd, indio_dev, spi, &ad7791_sigma_delta_info); 379 ad_sd_init(&st->sd, indio_dev, spi, &ad7791_sigma_delta_info);
@@ -410,10 +408,6 @@ error_remove_trigger:
410 ad_sd_cleanup_buffer_and_trigger(indio_dev); 408 ad_sd_cleanup_buffer_and_trigger(indio_dev);
411error_disable_reg: 409error_disable_reg:
412 regulator_disable(st->reg); 410 regulator_disable(st->reg);
413error_put_reg:
414 regulator_put(st->reg);
415err_iio_free:
416 iio_device_free(indio_dev);
417 411
418 return ret; 412 return ret;
419} 413}
@@ -427,9 +421,6 @@ static int ad7791_remove(struct spi_device *spi)
427 ad_sd_cleanup_buffer_and_trigger(indio_dev); 421 ad_sd_cleanup_buffer_and_trigger(indio_dev);
428 422
429 regulator_disable(st->reg); 423 regulator_disable(st->reg);
430 regulator_put(st->reg);
431
432 iio_device_free(indio_dev);
433 424
434 return 0; 425 return 0;
435} 426}
diff --git a/drivers/iio/adc/ad7793.c b/drivers/iio/adc/ad7793.c
index 334e31ff7a4e..4dddeabdfbb0 100644
--- a/drivers/iio/adc/ad7793.c
+++ b/drivers/iio/adc/ad7793.c
@@ -757,7 +757,7 @@ static int ad7793_probe(struct spi_device *spi)
757 return -ENODEV; 757 return -ENODEV;
758 } 758 }
759 759
760 indio_dev = iio_device_alloc(sizeof(*st)); 760 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
761 if (indio_dev == NULL) 761 if (indio_dev == NULL)
762 return -ENOMEM; 762 return -ENOMEM;
763 763
@@ -766,15 +766,13 @@ static int ad7793_probe(struct spi_device *spi)
766 ad_sd_init(&st->sd, indio_dev, spi, &ad7793_sigma_delta_info); 766 ad_sd_init(&st->sd, indio_dev, spi, &ad7793_sigma_delta_info);
767 767
768 if (pdata->refsel != AD7793_REFSEL_INTERNAL) { 768 if (pdata->refsel != AD7793_REFSEL_INTERNAL) {
769 st->reg = regulator_get(&spi->dev, "refin"); 769 st->reg = devm_regulator_get(&spi->dev, "refin");
770 if (IS_ERR(st->reg)) { 770 if (IS_ERR(st->reg))
771 ret = PTR_ERR(st->reg); 771 return PTR_ERR(st->reg);
772 goto error_device_free;
773 }
774 772
775 ret = regulator_enable(st->reg); 773 ret = regulator_enable(st->reg);
776 if (ret) 774 if (ret)
777 goto error_put_reg; 775 return ret;
778 776
779 vref_mv = regulator_get_voltage(st->reg); 777 vref_mv = regulator_get_voltage(st->reg);
780 if (vref_mv < 0) { 778 if (vref_mv < 0) {
@@ -818,11 +816,6 @@ error_remove_trigger:
818error_disable_reg: 816error_disable_reg:
819 if (pdata->refsel != AD7793_REFSEL_INTERNAL) 817 if (pdata->refsel != AD7793_REFSEL_INTERNAL)
820 regulator_disable(st->reg); 818 regulator_disable(st->reg);
821error_put_reg:
822 if (pdata->refsel != AD7793_REFSEL_INTERNAL)
823 regulator_put(st->reg);
824error_device_free:
825 iio_device_free(indio_dev);
826 819
827 return ret; 820 return ret;
828} 821}
@@ -836,12 +829,8 @@ static int ad7793_remove(struct spi_device *spi)
836 iio_device_unregister(indio_dev); 829 iio_device_unregister(indio_dev);
837 ad_sd_cleanup_buffer_and_trigger(indio_dev); 830 ad_sd_cleanup_buffer_and_trigger(indio_dev);
838 831
839 if (pdata->refsel != AD7793_REFSEL_INTERNAL) { 832 if (pdata->refsel != AD7793_REFSEL_INTERNAL)
840 regulator_disable(st->reg); 833 regulator_disable(st->reg);
841 regulator_put(st->reg);
842 }
843
844 iio_device_free(indio_dev);
845 834
846 return 0; 835 return 0;
847} 836}
diff --git a/drivers/iio/adc/ad7887.c b/drivers/iio/adc/ad7887.c
index dd15a5b0f701..9dd077b78759 100644
--- a/drivers/iio/adc/ad7887.c
+++ b/drivers/iio/adc/ad7887.c
@@ -237,25 +237,24 @@ static int ad7887_probe(struct spi_device *spi)
237{ 237{
238 struct ad7887_platform_data *pdata = spi->dev.platform_data; 238 struct ad7887_platform_data *pdata = spi->dev.platform_data;
239 struct ad7887_state *st; 239 struct ad7887_state *st;
240 struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); 240 struct iio_dev *indio_dev;
241 uint8_t mode; 241 uint8_t mode;
242 int ret; 242 int ret;
243 243
244 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
244 if (indio_dev == NULL) 245 if (indio_dev == NULL)
245 return -ENOMEM; 246 return -ENOMEM;
246 247
247 st = iio_priv(indio_dev); 248 st = iio_priv(indio_dev);
248 249
249 if (!pdata || !pdata->use_onchip_ref) { 250 if (!pdata || !pdata->use_onchip_ref) {
250 st->reg = regulator_get(&spi->dev, "vref"); 251 st->reg = devm_regulator_get(&spi->dev, "vref");
251 if (IS_ERR(st->reg)) { 252 if (IS_ERR(st->reg))
252 ret = PTR_ERR(st->reg); 253 return PTR_ERR(st->reg);
253 goto error_free;
254 }
255 254
256 ret = regulator_enable(st->reg); 255 ret = regulator_enable(st->reg);
257 if (ret) 256 if (ret)
258 goto error_put_reg; 257 return ret;
259 } 258 }
260 259
261 st->chip_info = 260 st->chip_info =
@@ -331,11 +330,6 @@ error_unregister_ring:
331error_disable_reg: 330error_disable_reg:
332 if (st->reg) 331 if (st->reg)
333 regulator_disable(st->reg); 332 regulator_disable(st->reg);
334error_put_reg:
335 if (st->reg)
336 regulator_put(st->reg);
337error_free:
338 iio_device_free(indio_dev);
339 333
340 return ret; 334 return ret;
341} 335}
@@ -347,11 +341,8 @@ static int ad7887_remove(struct spi_device *spi)
347 341
348 iio_device_unregister(indio_dev); 342 iio_device_unregister(indio_dev);
349 iio_triggered_buffer_cleanup(indio_dev); 343 iio_triggered_buffer_cleanup(indio_dev);
350 if (st->reg) { 344 if (st->reg)
351 regulator_disable(st->reg); 345 regulator_disable(st->reg);
352 regulator_put(st->reg);
353 }
354 iio_device_free(indio_dev);
355 346
356 return 0; 347 return 0;
357} 348}
diff --git a/drivers/iio/adc/ad7923.c b/drivers/iio/adc/ad7923.c
index 97fa0d3dc4aa..4108dbb28c3d 100644
--- a/drivers/iio/adc/ad7923.c
+++ b/drivers/iio/adc/ad7923.c
@@ -275,10 +275,11 @@ static const struct iio_info ad7923_info = {
275static int ad7923_probe(struct spi_device *spi) 275static int ad7923_probe(struct spi_device *spi)
276{ 276{
277 struct ad7923_state *st; 277 struct ad7923_state *st;
278 struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); 278 struct iio_dev *indio_dev;
279 const struct ad7923_chip_info *info; 279 const struct ad7923_chip_info *info;
280 int ret; 280 int ret;
281 281
282 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
282 if (indio_dev == NULL) 283 if (indio_dev == NULL)
283 return -ENOMEM; 284 return -ENOMEM;
284 285
@@ -311,14 +312,13 @@ static int ad7923_probe(struct spi_device *spi)
311 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg); 312 spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
312 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg); 313 spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
313 314
314 st->reg = regulator_get(&spi->dev, "refin"); 315 st->reg = devm_regulator_get(&spi->dev, "refin");
315 if (IS_ERR(st->reg)) { 316 if (IS_ERR(st->reg))
316 ret = PTR_ERR(st->reg); 317 return PTR_ERR(st->reg);
317 goto error_free; 318
318 }
319 ret = regulator_enable(st->reg); 319 ret = regulator_enable(st->reg);
320 if (ret) 320 if (ret)
321 goto error_put_reg; 321 return ret;
322 322
323 ret = iio_triggered_buffer_setup(indio_dev, NULL, 323 ret = iio_triggered_buffer_setup(indio_dev, NULL,
324 &ad7923_trigger_handler, NULL); 324 &ad7923_trigger_handler, NULL);
@@ -335,10 +335,6 @@ error_cleanup_ring:
335 iio_triggered_buffer_cleanup(indio_dev); 335 iio_triggered_buffer_cleanup(indio_dev);
336error_disable_reg: 336error_disable_reg:
337 regulator_disable(st->reg); 337 regulator_disable(st->reg);
338error_put_reg:
339 regulator_put(st->reg);
340error_free:
341 iio_device_free(indio_dev);
342 338
343 return ret; 339 return ret;
344} 340}
@@ -351,8 +347,6 @@ static int ad7923_remove(struct spi_device *spi)
351 iio_device_unregister(indio_dev); 347 iio_device_unregister(indio_dev);
352 iio_triggered_buffer_cleanup(indio_dev); 348 iio_triggered_buffer_cleanup(indio_dev);
353 regulator_disable(st->reg); 349 regulator_disable(st->reg);
354 regulator_put(st->reg);
355 iio_device_free(indio_dev);
356 350
357 return 0; 351 return 0;
358} 352}
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index b6db6a0e09cd..84be63bdf038 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -39,6 +39,10 @@
39#define at91_adc_writel(st, reg, val) \ 39#define at91_adc_writel(st, reg, val) \
40 (writel_relaxed(val, st->reg_base + reg)) 40 (writel_relaxed(val, st->reg_base + reg))
41 41
42struct at91_adc_caps {
43 struct at91_adc_reg_desc registers;
44};
45
42struct at91_adc_state { 46struct at91_adc_state {
43 struct clk *adc_clk; 47 struct clk *adc_clk;
44 u16 *buffer; 48 u16 *buffer;
@@ -62,6 +66,7 @@ struct at91_adc_state {
62 u32 res; /* resolution used for convertions */ 66 u32 res; /* resolution used for convertions */
63 bool low_res; /* the resolution corresponds to the lowest one */ 67 bool low_res; /* the resolution corresponds to the lowest one */
64 wait_queue_head_t wq_data_avail; 68 wait_queue_head_t wq_data_avail;
69 struct at91_adc_caps *caps;
65}; 70};
66 71
67static irqreturn_t at91_adc_trigger_handler(int irq, void *p) 72static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
@@ -429,6 +434,8 @@ ret:
429 return ret; 434 return ret;
430} 435}
431 436
437static const struct of_device_id at91_adc_dt_ids[];
438
432static int at91_adc_probe_dt(struct at91_adc_state *st, 439static int at91_adc_probe_dt(struct at91_adc_state *st,
433 struct platform_device *pdev) 440 struct platform_device *pdev)
434{ 441{
@@ -441,6 +448,9 @@ static int at91_adc_probe_dt(struct at91_adc_state *st,
441 if (!node) 448 if (!node)
442 return -EINVAL; 449 return -EINVAL;
443 450
451 st->caps = (struct at91_adc_caps *)
452 of_match_device(at91_adc_dt_ids, &pdev->dev)->data;
453
444 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers"); 454 st->use_external = of_property_read_bool(node, "atmel,adc-use-external-triggers");
445 455
446 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) { 456 if (of_property_read_u32(node, "atmel,adc-channels-used", &prop)) {
@@ -481,43 +491,7 @@ static int at91_adc_probe_dt(struct at91_adc_state *st,
481 if (ret) 491 if (ret)
482 goto error_ret; 492 goto error_ret;
483 493
484 st->registers = devm_kzalloc(&idev->dev, 494 st->registers = &st->caps->registers;
485 sizeof(struct at91_adc_reg_desc),
486 GFP_KERNEL);
487 if (!st->registers) {
488 dev_err(&idev->dev, "Could not allocate register memory.\n");
489 ret = -ENOMEM;
490 goto error_ret;
491 }
492
493 if (of_property_read_u32(node, "atmel,adc-channel-base", &prop)) {
494 dev_err(&idev->dev, "Missing adc-channel-base property in the DT.\n");
495 ret = -EINVAL;
496 goto error_ret;
497 }
498 st->registers->channel_base = prop;
499
500 if (of_property_read_u32(node, "atmel,adc-drdy-mask", &prop)) {
501 dev_err(&idev->dev, "Missing adc-drdy-mask property in the DT.\n");
502 ret = -EINVAL;
503 goto error_ret;
504 }
505 st->registers->drdy_mask = prop;
506
507 if (of_property_read_u32(node, "atmel,adc-status-register", &prop)) {
508 dev_err(&idev->dev, "Missing adc-status-register property in the DT.\n");
509 ret = -EINVAL;
510 goto error_ret;
511 }
512 st->registers->status_register = prop;
513
514 if (of_property_read_u32(node, "atmel,adc-trigger-register", &prop)) {
515 dev_err(&idev->dev, "Missing adc-trigger-register property in the DT.\n");
516 ret = -EINVAL;
517 goto error_ret;
518 }
519 st->registers->trigger_register = prop;
520
521 st->trigger_number = of_get_child_count(node); 495 st->trigger_number = of_get_child_count(node);
522 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number * 496 st->trigger_list = devm_kzalloc(&idev->dev, st->trigger_number *
523 sizeof(struct at91_adc_trigger), 497 sizeof(struct at91_adc_trigger),
@@ -589,11 +563,9 @@ static int at91_adc_probe(struct platform_device *pdev)
589 struct resource *res; 563 struct resource *res;
590 u32 reg; 564 u32 reg;
591 565
592 idev = iio_device_alloc(sizeof(struct at91_adc_state)); 566 idev = devm_iio_device_alloc(&pdev->dev, sizeof(struct at91_adc_state));
593 if (idev == NULL) { 567 if (!idev)
594 ret = -ENOMEM; 568 return -ENOMEM;
595 goto error_ret;
596 }
597 569
598 st = iio_priv(idev); 570 st = iio_priv(idev);
599 571
@@ -604,8 +576,7 @@ static int at91_adc_probe(struct platform_device *pdev)
604 576
605 if (ret) { 577 if (ret) {
606 dev_err(&pdev->dev, "No platform data available.\n"); 578 dev_err(&pdev->dev, "No platform data available.\n");
607 ret = -EINVAL; 579 return -EINVAL;
608 goto error_free_device;
609 } 580 }
610 581
611 platform_set_drvdata(pdev, idev); 582 platform_set_drvdata(pdev, idev);
@@ -618,16 +589,14 @@ static int at91_adc_probe(struct platform_device *pdev)
618 st->irq = platform_get_irq(pdev, 0); 589 st->irq = platform_get_irq(pdev, 0);
619 if (st->irq < 0) { 590 if (st->irq < 0) {
620 dev_err(&pdev->dev, "No IRQ ID is designated\n"); 591 dev_err(&pdev->dev, "No IRQ ID is designated\n");
621 ret = -ENODEV; 592 return -ENODEV;
622 goto error_free_device;
623 } 593 }
624 594
625 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 595 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
626 596
627 st->reg_base = devm_ioremap_resource(&pdev->dev, res); 597 st->reg_base = devm_ioremap_resource(&pdev->dev, res);
628 if (IS_ERR(st->reg_base)) { 598 if (IS_ERR(st->reg_base)) {
629 ret = PTR_ERR(st->reg_base); 599 return PTR_ERR(st->reg_base);
630 goto error_free_device;
631 } 600 }
632 601
633 /* 602 /*
@@ -642,7 +611,7 @@ static int at91_adc_probe(struct platform_device *pdev)
642 idev); 611 idev);
643 if (ret) { 612 if (ret) {
644 dev_err(&pdev->dev, "Failed to allocate IRQ.\n"); 613 dev_err(&pdev->dev, "Failed to allocate IRQ.\n");
645 goto error_free_device; 614 return ret;
646 } 615 }
647 616
648 st->clk = devm_clk_get(&pdev->dev, "adc_clk"); 617 st->clk = devm_clk_get(&pdev->dev, "adc_clk");
@@ -703,8 +672,8 @@ static int at91_adc_probe(struct platform_device *pdev)
703 shtim = round_up((st->sample_hold_time * adc_clk / 672 shtim = round_up((st->sample_hold_time * adc_clk /
704 1000000) - 1, 1); 673 1000000) - 1, 1);
705 674
706 reg = AT91_ADC_PRESCAL_(prsc) & AT91_ADC_PRESCAL; 675 reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask;
707 reg |= AT91_ADC_STARTUP_(ticks) & AT91_ADC_STARTUP; 676 reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
708 if (st->low_res) 677 if (st->low_res)
709 reg |= AT91_ADC_LOWRES; 678 reg |= AT91_ADC_LOWRES;
710 if (st->sleep_mode) 679 if (st->sleep_mode)
@@ -752,9 +721,6 @@ error_disable_clk:
752 clk_disable_unprepare(st->clk); 721 clk_disable_unprepare(st->clk);
753error_free_irq: 722error_free_irq:
754 free_irq(st->irq, idev); 723 free_irq(st->irq, idev);
755error_free_device:
756 iio_device_free(idev);
757error_ret:
758 return ret; 724 return ret;
759} 725}
760 726
@@ -769,14 +735,49 @@ static int at91_adc_remove(struct platform_device *pdev)
769 clk_disable_unprepare(st->adc_clk); 735 clk_disable_unprepare(st->adc_clk);
770 clk_disable_unprepare(st->clk); 736 clk_disable_unprepare(st->clk);
771 free_irq(st->irq, idev); 737 free_irq(st->irq, idev);
772 iio_device_free(idev);
773 738
774 return 0; 739 return 0;
775} 740}
776 741
777#ifdef CONFIG_OF 742#ifdef CONFIG_OF
743static struct at91_adc_caps at91sam9260_caps = {
744 .registers = {
745 .channel_base = AT91_ADC_CHR(0),
746 .drdy_mask = AT91_ADC_DRDY,
747 .status_register = AT91_ADC_SR,
748 .trigger_register = AT91_ADC_TRGR_9260,
749 .mr_prescal_mask = AT91_ADC_PRESCAL_9260,
750 .mr_startup_mask = AT91_ADC_STARTUP_9260,
751 },
752};
753
754static struct at91_adc_caps at91sam9g45_caps = {
755 .registers = {
756 .channel_base = AT91_ADC_CHR(0),
757 .drdy_mask = AT91_ADC_DRDY,
758 .status_register = AT91_ADC_SR,
759 .trigger_register = AT91_ADC_TRGR_9G45,
760 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
761 .mr_startup_mask = AT91_ADC_STARTUP_9G45,
762 },
763};
764
765static struct at91_adc_caps at91sam9x5_caps = {
766 .registers = {
767 .channel_base = AT91_ADC_CDR0_9X5,
768 .drdy_mask = AT91_ADC_SR_DRDY_9X5,
769 .status_register = AT91_ADC_SR_9X5,
770 .trigger_register = AT91_ADC_TRGR_9X5,
771 /* prescal mask is same as 9G45 */
772 .mr_prescal_mask = AT91_ADC_PRESCAL_9G45,
773 .mr_startup_mask = AT91_ADC_STARTUP_9X5,
774 },
775};
776
778static const struct of_device_id at91_adc_dt_ids[] = { 777static const struct of_device_id at91_adc_dt_ids[] = {
779 { .compatible = "atmel,at91sam9260-adc" }, 778 { .compatible = "atmel,at91sam9260-adc", .data = &at91sam9260_caps },
779 { .compatible = "atmel,at91sam9g45-adc", .data = &at91sam9g45_caps },
780 { .compatible = "atmel,at91sam9x5-adc", .data = &at91sam9x5_caps },
780 {}, 781 {},
781}; 782};
782MODULE_DEVICE_TABLE(of, at91_adc_dt_ids); 783MODULE_DEVICE_TABLE(of, at91_adc_dt_ids);
diff --git a/drivers/iio/adc/exynos_adc.c b/drivers/iio/adc/exynos_adc.c
index 9809fc9a35d2..d25b262193a7 100644
--- a/drivers/iio/adc/exynos_adc.c
+++ b/drivers/iio/adc/exynos_adc.c
@@ -33,6 +33,7 @@
33#include <linux/of_irq.h> 33#include <linux/of_irq.h>
34#include <linux/regulator/consumer.h> 34#include <linux/regulator/consumer.h>
35#include <linux/of_platform.h> 35#include <linux/of_platform.h>
36#include <linux/err.h>
36 37
37#include <linux/iio/iio.h> 38#include <linux/iio/iio.h>
38#include <linux/iio/machine.h> 39#include <linux/iio/machine.h>
@@ -261,7 +262,7 @@ static int exynos_adc_probe(struct platform_device *pdev)
261 if (!np) 262 if (!np)
262 return ret; 263 return ret;
263 264
264 indio_dev = iio_device_alloc(sizeof(struct exynos_adc)); 265 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct exynos_adc));
265 if (!indio_dev) { 266 if (!indio_dev) {
266 dev_err(&pdev->dev, "failed allocating iio device\n"); 267 dev_err(&pdev->dev, "failed allocating iio device\n");
267 return -ENOMEM; 268 return -ENOMEM;
@@ -271,23 +272,18 @@ static int exynos_adc_probe(struct platform_device *pdev)
271 272
272 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 273 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
273 info->regs = devm_ioremap_resource(&pdev->dev, mem); 274 info->regs = devm_ioremap_resource(&pdev->dev, mem);
274 if (IS_ERR(info->regs)) { 275 if (IS_ERR(info->regs))
275 ret = PTR_ERR(info->regs); 276 return PTR_ERR(info->regs);
276 goto err_iio;
277 }
278 277
279 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); 278 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
280 info->enable_reg = devm_ioremap_resource(&pdev->dev, mem); 279 info->enable_reg = devm_ioremap_resource(&pdev->dev, mem);
281 if (IS_ERR(info->enable_reg)) { 280 if (IS_ERR(info->enable_reg))
282 ret = PTR_ERR(info->enable_reg); 281 return PTR_ERR(info->enable_reg);
283 goto err_iio;
284 }
285 282
286 irq = platform_get_irq(pdev, 0); 283 irq = platform_get_irq(pdev, 0);
287 if (irq < 0) { 284 if (irq < 0) {
288 dev_err(&pdev->dev, "no irq resource?\n"); 285 dev_err(&pdev->dev, "no irq resource?\n");
289 ret = irq; 286 return irq;
290 goto err_iio;
291 } 287 }
292 288
293 info->irq = irq; 289 info->irq = irq;
@@ -299,7 +295,7 @@ static int exynos_adc_probe(struct platform_device *pdev)
299 if (ret < 0) { 295 if (ret < 0) {
300 dev_err(&pdev->dev, "failed requesting irq, irq = %d\n", 296 dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
301 info->irq); 297 info->irq);
302 goto err_iio; 298 return ret;
303 } 299 }
304 300
305 writel(1, info->enable_reg); 301 writel(1, info->enable_reg);
@@ -365,8 +361,6 @@ err_iio_dev:
365 iio_device_unregister(indio_dev); 361 iio_device_unregister(indio_dev);
366err_irq: 362err_irq:
367 free_irq(info->irq, info); 363 free_irq(info->irq, info);
368err_iio:
369 iio_device_free(indio_dev);
370 return ret; 364 return ret;
371} 365}
372 366
@@ -382,7 +376,6 @@ static int exynos_adc_remove(struct platform_device *pdev)
382 writel(0, info->enable_reg); 376 writel(0, info->enable_reg);
383 iio_device_unregister(indio_dev); 377 iio_device_unregister(indio_dev);
384 free_irq(info->irq, info); 378 free_irq(info->irq, info);
385 iio_device_free(indio_dev);
386 379
387 return 0; 380 return 0;
388} 381}
diff --git a/drivers/iio/adc/lp8788_adc.c b/drivers/iio/adc/lp8788_adc.c
index 62bc39e9c94f..5c8c91595f47 100644
--- a/drivers/iio/adc/lp8788_adc.c
+++ b/drivers/iio/adc/lp8788_adc.c
@@ -194,7 +194,7 @@ static int lp8788_adc_probe(struct platform_device *pdev)
194 struct lp8788_adc *adc; 194 struct lp8788_adc *adc;
195 int ret; 195 int ret;
196 196
197 indio_dev = iio_device_alloc(sizeof(*adc)); 197 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
198 if (!indio_dev) 198 if (!indio_dev)
199 return -ENOMEM; 199 return -ENOMEM;
200 200
@@ -205,7 +205,7 @@ static int lp8788_adc_probe(struct platform_device *pdev)
205 indio_dev->dev.of_node = pdev->dev.of_node; 205 indio_dev->dev.of_node = pdev->dev.of_node;
206 ret = lp8788_iio_map_register(indio_dev, lp->pdata, adc); 206 ret = lp8788_iio_map_register(indio_dev, lp->pdata, adc);
207 if (ret) 207 if (ret)
208 goto err_iio_map; 208 return ret;
209 209
210 mutex_init(&adc->lock); 210 mutex_init(&adc->lock);
211 211
@@ -226,8 +226,6 @@ static int lp8788_adc_probe(struct platform_device *pdev)
226 226
227err_iio_device: 227err_iio_device:
228 iio_map_array_unregister(indio_dev); 228 iio_map_array_unregister(indio_dev);
229err_iio_map:
230 iio_device_free(indio_dev);
231 return ret; 229 return ret;
232} 230}
233 231
@@ -237,7 +235,6 @@ static int lp8788_adc_remove(struct platform_device *pdev)
237 235
238 iio_device_unregister(indio_dev); 236 iio_device_unregister(indio_dev);
239 iio_map_array_unregister(indio_dev); 237 iio_map_array_unregister(indio_dev);
240 iio_device_free(indio_dev);
241 238
242 return 0; 239 return 0;
243} 240}
diff --git a/drivers/iio/adc/max1363.c b/drivers/iio/adc/max1363.c
index f148d00b83f7..4fb35d1d7494 100644
--- a/drivers/iio/adc/max1363.c
+++ b/drivers/iio/adc/max1363.c
@@ -1498,16 +1498,15 @@ static int max1363_probe(struct i2c_client *client,
1498 struct iio_dev *indio_dev; 1498 struct iio_dev *indio_dev;
1499 struct regulator *vref; 1499 struct regulator *vref;
1500 1500
1501 indio_dev = iio_device_alloc(sizeof(struct max1363_state)); 1501 indio_dev = devm_iio_device_alloc(&client->dev,
1502 if (indio_dev == NULL) { 1502 sizeof(struct max1363_state));
1503 ret = -ENOMEM; 1503 if (!indio_dev)
1504 goto error_out; 1504 return -ENOMEM;
1505 }
1506 1505
1507 indio_dev->dev.of_node = client->dev.of_node; 1506 indio_dev->dev.of_node = client->dev.of_node;
1508 ret = iio_map_array_register(indio_dev, client->dev.platform_data); 1507 ret = iio_map_array_register(indio_dev, client->dev.platform_data);
1509 if (ret < 0) 1508 if (ret < 0)
1510 goto error_free_device; 1509 return ret;
1511 1510
1512 st = iio_priv(indio_dev); 1511 st = iio_priv(indio_dev);
1513 1512
@@ -1590,9 +1589,6 @@ error_disable_reg:
1590 regulator_disable(st->reg); 1589 regulator_disable(st->reg);
1591error_unregister_map: 1590error_unregister_map:
1592 iio_map_array_unregister(indio_dev); 1591 iio_map_array_unregister(indio_dev);
1593error_free_device:
1594 iio_device_free(indio_dev);
1595error_out:
1596 return ret; 1592 return ret;
1597} 1593}
1598 1594
@@ -1607,7 +1603,6 @@ static int max1363_remove(struct i2c_client *client)
1607 regulator_disable(st->vref); 1603 regulator_disable(st->vref);
1608 regulator_disable(st->reg); 1604 regulator_disable(st->reg);
1609 iio_map_array_unregister(indio_dev); 1605 iio_map_array_unregister(indio_dev);
1610 iio_device_free(indio_dev);
1611 1606
1612 return 0; 1607 return 0;
1613} 1608}
diff --git a/drivers/iio/adc/mcp320x.c b/drivers/iio/adc/mcp320x.c
index ebc015922a79..28a086e48776 100644
--- a/drivers/iio/adc/mcp320x.c
+++ b/drivers/iio/adc/mcp320x.c
@@ -169,7 +169,7 @@ static int mcp320x_probe(struct spi_device *spi)
169 const struct mcp3208_chip_info *chip_info; 169 const struct mcp3208_chip_info *chip_info;
170 int ret; 170 int ret;
171 171
172 indio_dev = iio_device_alloc(sizeof(*adc)); 172 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
173 if (!indio_dev) 173 if (!indio_dev)
174 return -ENOMEM; 174 return -ENOMEM;
175 175
@@ -193,15 +193,13 @@ static int mcp320x_probe(struct spi_device *spi)
193 spi_message_init_with_transfers(&adc->msg, adc->transfer, 193 spi_message_init_with_transfers(&adc->msg, adc->transfer,
194 ARRAY_SIZE(adc->transfer)); 194 ARRAY_SIZE(adc->transfer));
195 195
196 adc->reg = regulator_get(&spi->dev, "vref"); 196 adc->reg = devm_regulator_get(&spi->dev, "vref");
197 if (IS_ERR(adc->reg)) { 197 if (IS_ERR(adc->reg))
198 ret = PTR_ERR(adc->reg); 198 return PTR_ERR(adc->reg);
199 goto iio_free;
200 }
201 199
202 ret = regulator_enable(adc->reg); 200 ret = regulator_enable(adc->reg);
203 if (ret < 0) 201 if (ret < 0)
204 goto reg_free; 202 return ret;
205 203
206 mutex_init(&adc->lock); 204 mutex_init(&adc->lock);
207 205
@@ -213,10 +211,6 @@ static int mcp320x_probe(struct spi_device *spi)
213 211
214reg_disable: 212reg_disable:
215 regulator_disable(adc->reg); 213 regulator_disable(adc->reg);
216reg_free:
217 regulator_put(adc->reg);
218iio_free:
219 iio_device_free(indio_dev);
220 214
221 return ret; 215 return ret;
222} 216}
@@ -228,8 +222,6 @@ static int mcp320x_remove(struct spi_device *spi)
228 222
229 iio_device_unregister(indio_dev); 223 iio_device_unregister(indio_dev);
230 regulator_disable(adc->reg); 224 regulator_disable(adc->reg);
231 regulator_put(adc->reg);
232 iio_device_free(indio_dev);
233 225
234 return 0; 226 return 0;
235} 227}
diff --git a/drivers/iio/adc/nau7802.c b/drivers/iio/adc/nau7802.c
new file mode 100644
index 000000000000..bdf03468f3b8
--- /dev/null
+++ b/drivers/iio/adc/nau7802.c
@@ -0,0 +1,581 @@
1/*
2 * Driver for the Nuvoton NAU7802 ADC
3 *
4 * Copyright 2013 Free Electrons
5 *
6 * Licensed under the GPLv2 or later.
7 */
8
9#include <linux/delay.h>
10#include <linux/i2c.h>
11#include <linux/interrupt.h>
12#include <linux/module.h>
13#include <linux/wait.h>
14#include <linux/log2.h>
15
16#include <linux/iio/iio.h>
17#include <linux/iio/sysfs.h>
18
19#define NAU7802_REG_PUCTRL 0x00
20#define NAU7802_PUCTRL_RR(x) (x << 0)
21#define NAU7802_PUCTRL_RR_BIT NAU7802_PUCTRL_RR(1)
22#define NAU7802_PUCTRL_PUD(x) (x << 1)
23#define NAU7802_PUCTRL_PUD_BIT NAU7802_PUCTRL_PUD(1)
24#define NAU7802_PUCTRL_PUA(x) (x << 2)
25#define NAU7802_PUCTRL_PUA_BIT NAU7802_PUCTRL_PUA(1)
26#define NAU7802_PUCTRL_PUR(x) (x << 3)
27#define NAU7802_PUCTRL_PUR_BIT NAU7802_PUCTRL_PUR(1)
28#define NAU7802_PUCTRL_CS(x) (x << 4)
29#define NAU7802_PUCTRL_CS_BIT NAU7802_PUCTRL_CS(1)
30#define NAU7802_PUCTRL_CR(x) (x << 5)
31#define NAU7802_PUCTRL_CR_BIT NAU7802_PUCTRL_CR(1)
32#define NAU7802_PUCTRL_AVDDS(x) (x << 7)
33#define NAU7802_PUCTRL_AVDDS_BIT NAU7802_PUCTRL_AVDDS(1)
34#define NAU7802_REG_CTRL1 0x01
35#define NAU7802_CTRL1_VLDO(x) (x << 3)
36#define NAU7802_CTRL1_GAINS(x) (x)
37#define NAU7802_CTRL1_GAINS_BITS 0x07
38#define NAU7802_REG_CTRL2 0x02
39#define NAU7802_CTRL2_CHS(x) (x << 7)
40#define NAU7802_CTRL2_CRS(x) (x << 4)
41#define NAU7802_SAMP_FREQ_320 0x07
42#define NAU7802_CTRL2_CHS_BIT NAU7802_CTRL2_CHS(1)
43#define NAU7802_REG_ADC_B2 0x12
44#define NAU7802_REG_ADC_B1 0x13
45#define NAU7802_REG_ADC_B0 0x14
46#define NAU7802_REG_ADC_CTRL 0x15
47
48#define NAU7802_MIN_CONVERSIONS 6
49
50struct nau7802_state {
51 struct i2c_client *client;
52 s32 last_value;
53 struct mutex lock;
54 struct mutex data_lock;
55 u32 vref_mv;
56 u32 conversion_count;
57 u32 min_conversions;
58 u8 sample_rate;
59 u32 scale_avail[8];
60 struct completion value_ok;
61};
62
63#define NAU7802_CHANNEL(chan) { \
64 .type = IIO_VOLTAGE, \
65 .indexed = 1, \
66 .channel = (chan), \
67 .scan_index = (chan), \
68 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
69 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
70 BIT(IIO_CHAN_INFO_SAMP_FREQ) \
71}
72
73static const struct iio_chan_spec nau7802_chan_array[] = {
74 NAU7802_CHANNEL(0),
75 NAU7802_CHANNEL(1),
76};
77
78static const u16 nau7802_sample_freq_avail[] = {10, 20, 40, 80,
79 10, 10, 10, 320};
80
81static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 40 80 320");
82
83static struct attribute *nau7802_attributes[] = {
84 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
85 NULL
86};
87
88static const struct attribute_group nau7802_attribute_group = {
89 .attrs = nau7802_attributes,
90};
91
92static int nau7802_set_gain(struct nau7802_state *st, int gain)
93{
94 int ret;
95
96 mutex_lock(&st->lock);
97 st->conversion_count = 0;
98
99 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL1);
100 if (ret < 0)
101 goto nau7802_sysfs_set_gain_out;
102 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL1,
103 (ret & (~NAU7802_CTRL1_GAINS_BITS)) |
104 gain);
105
106nau7802_sysfs_set_gain_out:
107 mutex_unlock(&st->lock);
108
109 return ret;
110}
111
112static int nau7802_read_conversion(struct nau7802_state *st)
113{
114 int data;
115
116 mutex_lock(&st->data_lock);
117 data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B2);
118 if (data < 0)
119 goto nau7802_read_conversion_out;
120 st->last_value = data << 16;
121
122 data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B1);
123 if (data < 0)
124 goto nau7802_read_conversion_out;
125 st->last_value |= data << 8;
126
127 data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B0);
128 if (data < 0)
129 goto nau7802_read_conversion_out;
130 st->last_value |= data;
131
132 st->last_value = sign_extend32(st->last_value, 23);
133
134nau7802_read_conversion_out:
135 mutex_unlock(&st->data_lock);
136
137 return data;
138}
139
140/*
141 * Conversions are synchronised on the rising edge of NAU7802_PUCTRL_CS_BIT
142 */
143static int nau7802_sync(struct nau7802_state *st)
144{
145 int ret;
146
147 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL);
148 if (ret < 0)
149 return ret;
150 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
151 ret | NAU7802_PUCTRL_CS_BIT);
152
153 return ret;
154}
155
156static irqreturn_t nau7802_eoc_trigger(int irq, void *private)
157{
158 struct iio_dev *indio_dev = private;
159 struct nau7802_state *st = iio_priv(indio_dev);
160 int status;
161
162 status = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL);
163 if (status < 0)
164 return IRQ_HANDLED;
165
166 if (!(status & NAU7802_PUCTRL_CR_BIT))
167 return IRQ_NONE;
168
169 if (nau7802_read_conversion(st) < 0)
170 return IRQ_HANDLED;
171
172 /*
173 * Because there is actually only one ADC for both channels, we have to
174 * wait for enough conversions to happen before getting a significant
175 * value when changing channels and the values are far apart.
176 */
177 if (st->conversion_count < NAU7802_MIN_CONVERSIONS)
178 st->conversion_count++;
179 if (st->conversion_count >= NAU7802_MIN_CONVERSIONS)
180 complete_all(&st->value_ok);
181
182 return IRQ_HANDLED;
183}
184
185static int nau7802_read_irq(struct iio_dev *indio_dev,
186 struct iio_chan_spec const *chan,
187 int *val)
188{
189 struct nau7802_state *st = iio_priv(indio_dev);
190 int ret;
191
192 INIT_COMPLETION(st->value_ok);
193 enable_irq(st->client->irq);
194
195 nau7802_sync(st);
196
197 /* read registers to ensure we flush everything */
198 ret = nau7802_read_conversion(st);
199 if (ret < 0)
200 goto read_chan_info_failure;
201
202 /* Wait for a conversion to finish */
203 ret = wait_for_completion_interruptible_timeout(&st->value_ok,
204 msecs_to_jiffies(1000));
205 if (ret == 0)
206 ret = -ETIMEDOUT;
207
208 if (ret < 0)
209 goto read_chan_info_failure;
210
211 disable_irq(st->client->irq);
212
213 *val = st->last_value;
214
215 return IIO_VAL_INT;
216
217read_chan_info_failure:
218 disable_irq(st->client->irq);
219
220 return ret;
221}
222
223static int nau7802_read_poll(struct iio_dev *indio_dev,
224 struct iio_chan_spec const *chan,
225 int *val)
226{
227 struct nau7802_state *st = iio_priv(indio_dev);
228 int ret;
229
230 nau7802_sync(st);
231
232 /* read registers to ensure we flush everything */
233 ret = nau7802_read_conversion(st);
234 if (ret < 0)
235 return ret;
236
237 /*
238 * Because there is actually only one ADC for both channels, we have to
239 * wait for enough conversions to happen before getting a significant
240 * value when changing channels and the values are far appart.
241 */
242 do {
243 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL);
244 if (ret < 0)
245 return ret;
246
247 while (!(ret & NAU7802_PUCTRL_CR_BIT)) {
248 if (st->sample_rate != NAU7802_SAMP_FREQ_320)
249 msleep(20);
250 else
251 mdelay(4);
252 ret = i2c_smbus_read_byte_data(st->client,
253 NAU7802_REG_PUCTRL);
254 if (ret < 0)
255 return ret;
256 }
257
258 ret = nau7802_read_conversion(st);
259 if (ret < 0)
260 return ret;
261 if (st->conversion_count < NAU7802_MIN_CONVERSIONS)
262 st->conversion_count++;
263 } while (st->conversion_count < NAU7802_MIN_CONVERSIONS);
264
265 *val = st->last_value;
266
267 return IIO_VAL_INT;
268}
269
270static int nau7802_read_raw(struct iio_dev *indio_dev,
271 struct iio_chan_spec const *chan,
272 int *val, int *val2, long mask)
273{
274 struct nau7802_state *st = iio_priv(indio_dev);
275 int ret;
276
277 switch (mask) {
278 case IIO_CHAN_INFO_RAW:
279 mutex_lock(&st->lock);
280 /*
281 * Select the channel to use
282 * - Channel 1 is value 0 in the CHS register
283 * - Channel 2 is value 1 in the CHS register
284 */
285 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL2);
286 if (ret < 0) {
287 mutex_unlock(&st->lock);
288 return ret;
289 }
290
291 if (((ret & NAU7802_CTRL2_CHS_BIT) && !chan->channel) ||
292 (!(ret & NAU7802_CTRL2_CHS_BIT) &&
293 chan->channel)) {
294 st->conversion_count = 0;
295 ret = i2c_smbus_write_byte_data(st->client,
296 NAU7802_REG_CTRL2,
297 NAU7802_CTRL2_CHS(chan->channel) |
298 NAU7802_CTRL2_CRS(st->sample_rate));
299
300 if (ret < 0) {
301 mutex_unlock(&st->lock);
302 return ret;
303 }
304 }
305
306 if (st->client->irq)
307 ret = nau7802_read_irq(indio_dev, chan, val);
308 else
309 ret = nau7802_read_poll(indio_dev, chan, val);
310
311 mutex_unlock(&st->lock);
312 return ret;
313
314 case IIO_CHAN_INFO_SCALE:
315 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL1);
316 if (ret < 0)
317 return ret;
318
319 /*
320 * We have 24 bits of signed data, that means 23 bits of data
321 * plus the sign bit
322 */
323 *val = st->vref_mv;
324 *val2 = 23 + (ret & NAU7802_CTRL1_GAINS_BITS);
325
326 return IIO_VAL_FRACTIONAL_LOG2;
327
328 case IIO_CHAN_INFO_SAMP_FREQ:
329 *val = nau7802_sample_freq_avail[st->sample_rate];
330 *val2 = 0;
331 return IIO_VAL_INT;
332
333 default:
334 break;
335 }
336
337 return -EINVAL;
338}
339
340static int nau7802_write_raw(struct iio_dev *indio_dev,
341 struct iio_chan_spec const *chan,
342 int val, int val2, long mask)
343{
344 struct nau7802_state *st = iio_priv(indio_dev);
345 int i, ret;
346
347 switch (mask) {
348 case IIO_CHAN_INFO_SCALE:
349 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
350 if (val2 == st->scale_avail[i])
351 return nau7802_set_gain(st, i);
352
353 break;
354
355 case IIO_CHAN_INFO_SAMP_FREQ:
356 for (i = 0; i < ARRAY_SIZE(nau7802_sample_freq_avail); i++)
357 if (val == nau7802_sample_freq_avail[i]) {
358 mutex_lock(&st->lock);
359 st->sample_rate = i;
360 st->conversion_count = 0;
361 ret = i2c_smbus_write_byte_data(st->client,
362 NAU7802_REG_CTRL2,
363 NAU7802_CTRL2_CRS(st->sample_rate));
364 mutex_unlock(&st->lock);
365 return ret;
366 }
367
368 break;
369
370 default:
371 break;
372 }
373
374 return -EINVAL;
375}
376
377static int nau7802_write_raw_get_fmt(struct iio_dev *indio_dev,
378 struct iio_chan_spec const *chan,
379 long mask)
380{
381 return IIO_VAL_INT_PLUS_NANO;
382}
383
384static const struct iio_info nau7802_info = {
385 .driver_module = THIS_MODULE,
386 .read_raw = &nau7802_read_raw,
387 .write_raw = &nau7802_write_raw,
388 .write_raw_get_fmt = nau7802_write_raw_get_fmt,
389 .attrs = &nau7802_attribute_group,
390};
391
392static int nau7802_probe(struct i2c_client *client,
393 const struct i2c_device_id *id)
394{
395 struct iio_dev *indio_dev;
396 struct nau7802_state *st;
397 struct device_node *np = client->dev.of_node;
398 int i, ret;
399 u8 data;
400 u32 tmp = 0;
401
402 if (!client->dev.of_node) {
403 dev_err(&client->dev, "No device tree node available.\n");
404 return -EINVAL;
405 }
406
407 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
408 if (indio_dev == NULL)
409 return -ENOMEM;
410
411 st = iio_priv(indio_dev);
412
413 i2c_set_clientdata(client, indio_dev);
414
415 indio_dev->dev.parent = &client->dev;
416 indio_dev->name = dev_name(&client->dev);
417 indio_dev->modes = INDIO_DIRECT_MODE;
418 indio_dev->info = &nau7802_info;
419
420 st->client = client;
421
422 /* Reset the device */
423 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
424 NAU7802_PUCTRL_RR_BIT);
425 if (ret < 0)
426 return ret;
427
428 /* Enter normal operation mode */
429 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
430 NAU7802_PUCTRL_PUD_BIT);
431 if (ret < 0)
432 return ret;
433
434 /*
435 * After about 200 usecs, the device should be ready and then
436 * the Power Up bit will be set to 1. If not, wait for it.
437 */
438 udelay(210);
439 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL);
440 if (ret < 0)
441 return ret;
442 if (!(ret & NAU7802_PUCTRL_PUR_BIT))
443 return ret;
444
445 of_property_read_u32(np, "nuvoton,vldo", &tmp);
446 st->vref_mv = tmp;
447
448 data = NAU7802_PUCTRL_PUD_BIT | NAU7802_PUCTRL_PUA_BIT |
449 NAU7802_PUCTRL_CS_BIT;
450 if (tmp >= 2400)
451 data |= NAU7802_PUCTRL_AVDDS_BIT;
452
453 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL, data);
454 if (ret < 0)
455 return ret;
456 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_ADC_CTRL, 0x30);
457 if (ret < 0)
458 return ret;
459
460 if (tmp >= 2400) {
461 data = NAU7802_CTRL1_VLDO((4500 - tmp) / 300);
462 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL1,
463 data);
464 if (ret < 0)
465 return ret;
466 }
467
468 /* Populate available ADC input ranges */
469 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
470 st->scale_avail[i] = (((u64)st->vref_mv) * 1000000000ULL)
471 >> (23 + i);
472
473 init_completion(&st->value_ok);
474
475 /*
476 * The ADC fires continuously and we can't do anything about
477 * it. So we need to have the IRQ disabled by default, and we
478 * will enable them back when we will need them..
479 */
480 if (client->irq) {
481 ret = request_threaded_irq(client->irq,
482 NULL,
483 nau7802_eoc_trigger,
484 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
485 client->dev.driver->name,
486 indio_dev);
487 if (ret) {
488 /*
489 * What may happen here is that our IRQ controller is
490 * not able to get level interrupt but this is required
491 * by this ADC as when going over 40 sample per second,
492 * the interrupt line may stay high between conversions.
493 * So, we continue no matter what but we switch to
494 * polling mode.
495 */
496 dev_info(&client->dev,
497 "Failed to allocate IRQ, using polling mode\n");
498 client->irq = 0;
499 } else
500 disable_irq(client->irq);
501 }
502
503 if (!client->irq) {
504 /*
505 * We are polling, use the fastest sample rate by
506 * default
507 */
508 st->sample_rate = NAU7802_SAMP_FREQ_320;
509 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL2,
510 NAU7802_CTRL2_CRS(st->sample_rate));
511 if (ret)
512 goto error_free_irq;
513 }
514
515 /* Setup the ADC channels available on the board */
516 indio_dev->num_channels = ARRAY_SIZE(nau7802_chan_array);
517 indio_dev->channels = nau7802_chan_array;
518
519 mutex_init(&st->lock);
520 mutex_init(&st->data_lock);
521
522 ret = iio_device_register(indio_dev);
523 if (ret < 0) {
524 dev_err(&client->dev, "Couldn't register the device.\n");
525 goto error_device_register;
526 }
527
528 return 0;
529
530error_device_register:
531 mutex_destroy(&st->lock);
532 mutex_destroy(&st->data_lock);
533error_free_irq:
534 if (client->irq)
535 free_irq(client->irq, indio_dev);
536
537 return ret;
538}
539
540static int nau7802_remove(struct i2c_client *client)
541{
542 struct iio_dev *indio_dev = i2c_get_clientdata(client);
543 struct nau7802_state *st = iio_priv(indio_dev);
544
545 iio_device_unregister(indio_dev);
546 mutex_destroy(&st->lock);
547 mutex_destroy(&st->data_lock);
548 if (client->irq)
549 free_irq(client->irq, indio_dev);
550
551 return 0;
552}
553
554static const struct i2c_device_id nau7802_i2c_id[] = {
555 { "nau7802", 0 },
556 { }
557};
558MODULE_DEVICE_TABLE(i2c, nau7802_i2c_id);
559
560static const struct of_device_id nau7802_dt_ids[] = {
561 { .compatible = "nuvoton,nau7802" },
562 {},
563};
564MODULE_DEVICE_TABLE(of, nau7802_dt_ids);
565
566static struct i2c_driver nau7802_driver = {
567 .probe = nau7802_probe,
568 .remove = nau7802_remove,
569 .id_table = nau7802_i2c_id,
570 .driver = {
571 .name = "nau7802",
572 .of_match_table = of_match_ptr(nau7802_dt_ids),
573 },
574};
575
576module_i2c_driver(nau7802_driver);
577
578MODULE_LICENSE("GPL");
579MODULE_DESCRIPTION("Nuvoton NAU7802 ADC Driver");
580MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
581MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
diff --git a/drivers/iio/adc/ti-adc081c.c b/drivers/iio/adc/ti-adc081c.c
index 2826faae706c..ee5f72bffe5a 100644
--- a/drivers/iio/adc/ti-adc081c.c
+++ b/drivers/iio/adc/ti-adc081c.c
@@ -74,22 +74,20 @@ static int adc081c_probe(struct i2c_client *client,
74 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) 74 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
75 return -ENODEV; 75 return -ENODEV;
76 76
77 iio = iio_device_alloc(sizeof(*adc)); 77 iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
78 if (!iio) 78 if (!iio)
79 return -ENOMEM; 79 return -ENOMEM;
80 80
81 adc = iio_priv(iio); 81 adc = iio_priv(iio);
82 adc->i2c = client; 82 adc->i2c = client;
83 83
84 adc->ref = regulator_get(&client->dev, "vref"); 84 adc->ref = devm_regulator_get(&client->dev, "vref");
85 if (IS_ERR(adc->ref)) { 85 if (IS_ERR(adc->ref))
86 err = PTR_ERR(adc->ref); 86 return PTR_ERR(adc->ref);
87 goto iio_free;
88 }
89 87
90 err = regulator_enable(adc->ref); 88 err = regulator_enable(adc->ref);
91 if (err < 0) 89 if (err < 0)
92 goto regulator_put; 90 return err;
93 91
94 iio->dev.parent = &client->dev; 92 iio->dev.parent = &client->dev;
95 iio->name = dev_name(&client->dev); 93 iio->name = dev_name(&client->dev);
@@ -109,10 +107,6 @@ static int adc081c_probe(struct i2c_client *client,
109 107
110regulator_disable: 108regulator_disable:
111 regulator_disable(adc->ref); 109 regulator_disable(adc->ref);
112regulator_put:
113 regulator_put(adc->ref);
114iio_free:
115 iio_device_free(iio);
116 110
117 return err; 111 return err;
118} 112}
@@ -124,8 +118,6 @@ static int adc081c_remove(struct i2c_client *client)
124 118
125 iio_device_unregister(iio); 119 iio_device_unregister(iio);
126 regulator_disable(adc->ref); 120 regulator_disable(adc->ref);
127 regulator_put(adc->ref);
128 iio_device_free(iio);
129 121
130 return 0; 122 return 0;
131} 123}
diff --git a/drivers/iio/adc/ti_am335x_adc.c b/drivers/iio/adc/ti_am335x_adc.c
index 3ceac3e91dde..a952538a1a8b 100644
--- a/drivers/iio/adc/ti_am335x_adc.c
+++ b/drivers/iio/adc/ti_am335x_adc.c
@@ -216,11 +216,11 @@ static int tiadc_probe(struct platform_device *pdev)
216 return -EINVAL; 216 return -EINVAL;
217 } 217 }
218 218
219 indio_dev = iio_device_alloc(sizeof(struct tiadc_device)); 219 indio_dev = devm_iio_device_alloc(&pdev->dev,
220 sizeof(struct tiadc_device));
220 if (indio_dev == NULL) { 221 if (indio_dev == NULL) {
221 dev_err(&pdev->dev, "failed to allocate iio device\n"); 222 dev_err(&pdev->dev, "failed to allocate iio device\n");
222 err = -ENOMEM; 223 return -ENOMEM;
223 goto err_ret;
224 } 224 }
225 adc_dev = iio_priv(indio_dev); 225 adc_dev = iio_priv(indio_dev);
226 226
@@ -241,7 +241,7 @@ static int tiadc_probe(struct platform_device *pdev)
241 241
242 err = tiadc_channel_init(indio_dev, adc_dev->channels); 242 err = tiadc_channel_init(indio_dev, adc_dev->channels);
243 if (err < 0) 243 if (err < 0)
244 goto err_free_device; 244 return err;
245 245
246 err = iio_device_register(indio_dev); 246 err = iio_device_register(indio_dev);
247 if (err) 247 if (err)
@@ -253,9 +253,6 @@ static int tiadc_probe(struct platform_device *pdev)
253 253
254err_free_channels: 254err_free_channels:
255 tiadc_channels_remove(indio_dev); 255 tiadc_channels_remove(indio_dev);
256err_free_device:
257 iio_device_free(indio_dev);
258err_ret:
259 return err; 256 return err;
260} 257}
261 258
@@ -271,8 +268,6 @@ static int tiadc_remove(struct platform_device *pdev)
271 step_en = get_adc_step_mask(adc_dev); 268 step_en = get_adc_step_mask(adc_dev);
272 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en); 269 am335x_tsc_se_clr(adc_dev->mfd_tscadc, step_en);
273 270
274 iio_device_free(indio_dev);
275
276 return 0; 271 return 0;
277} 272}
278 273
diff --git a/drivers/iio/adc/twl6030-gpadc.c b/drivers/iio/adc/twl6030-gpadc.c
new file mode 100644
index 000000000000..0ea96c058c08
--- /dev/null
+++ b/drivers/iio/adc/twl6030-gpadc.c
@@ -0,0 +1,1013 @@
1/*
2 * TWL6030 GPADC module driver
3 *
4 * Copyright (C) 2009-2013 Texas Instruments Inc.
5 * Nishant Kamat <nskamat@ti.com>
6 * Balaji T K <balajitk@ti.com>
7 * Graeme Gregory <gg@slimlogic.co.uk>
8 * Girish S Ghongdemath <girishsg@ti.com>
9 * Ambresh K <ambresh@ti.com>
10 * Oleksandr Kozaruk <oleksandr.kozaruk@ti.com
11 *
12 * Based on twl4030-madc.c
13 * Copyright (C) 2008 Nokia Corporation
14 * Mikko Ylinen <mikko.k.ylinen@nokia.com>
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * version 2 as published by the Free Software Foundation.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * 02110-1301 USA
29 *
30 */
31#include <linux/init.h>
32#include <linux/interrupt.h>
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/platform_device.h>
36#include <linux/of_platform.h>
37#include <linux/i2c/twl.h>
38#include <linux/iio/iio.h>
39#include <linux/iio/sysfs.h>
40
41#define DRIVER_NAME "twl6030_gpadc"
42
43/*
44 * twl6030 per TRM has 17 channels, and twl6032 has 19 channels
45 * 2 test network channels are not used,
46 * 2 die temperature channels are not used either, as it is not
47 * defined how to convert ADC value to temperature
48 */
49#define TWL6030_GPADC_USED_CHANNELS 13
50#define TWL6030_GPADC_MAX_CHANNELS 15
51#define TWL6032_GPADC_USED_CHANNELS 15
52#define TWL6032_GPADC_MAX_CHANNELS 19
53#define TWL6030_GPADC_NUM_TRIM_REGS 16
54
55#define TWL6030_GPADC_CTRL_P1 0x05
56
57#define TWL6032_GPADC_GPSELECT_ISB 0x07
58#define TWL6032_GPADC_CTRL_P1 0x08
59
60#define TWL6032_GPADC_GPCH0_LSB 0x0d
61#define TWL6032_GPADC_GPCH0_MSB 0x0e
62
63#define TWL6030_GPADC_CTRL_P1_SP1 BIT(3)
64
65#define TWL6030_GPADC_GPCH0_LSB (0x29)
66
67#define TWL6030_GPADC_RT_SW1_EOC_MASK BIT(5)
68
69#define TWL6030_GPADC_TRIM1 0xCD
70
71#define TWL6030_REG_TOGGLE1 0x90
72#define TWL6030_GPADCS BIT(1)
73#define TWL6030_GPADCR BIT(0)
74
75/**
76 * struct twl6030_chnl_calib - channel calibration
77 * @gain: slope coefficient for ideal curve
78 * @gain_error: gain error
79 * @offset_error: offset of the real curve
80 */
81struct twl6030_chnl_calib {
82 s32 gain;
83 s32 gain_error;
84 s32 offset_error;
85};
86
87/**
88 * struct twl6030_ideal_code - GPADC calibration parameters
89 * GPADC is calibrated in two points: close to the beginning and
90 * to the and of the measurable input range
91 *
92 * @channel: channel number
93 * @code1: ideal code for the input at the beginning
94 * @code2: ideal code for at the end of the range
95 * @volt1: voltage input at the beginning(low voltage)
96 * @volt2: voltage input at the end(high voltage)
97 */
98struct twl6030_ideal_code {
99 int channel;
100 u16 code1;
101 u16 code2;
102 u16 volt1;
103 u16 volt2;
104};
105
106struct twl6030_gpadc_data;
107
108/**
109 * struct twl6030_gpadc_platform_data - platform specific data
110 * @nchannels: number of GPADC channels
111 * @iio_channels: iio channels
112 * @twl6030_ideal: pointer to calibration parameters
113 * @start_conversion: pointer to ADC start conversion function
114 * @channel_to_reg pointer to ADC function to convert channel to
115 * register address for reading conversion result
116 * @calibrate: pointer to calibration function
117 */
118struct twl6030_gpadc_platform_data {
119 const int nchannels;
120 const struct iio_chan_spec *iio_channels;
121 const struct twl6030_ideal_code *ideal;
122 int (*start_conversion)(int channel);
123 u8 (*channel_to_reg)(int channel);
124 int (*calibrate)(struct twl6030_gpadc_data *gpadc);
125};
126
127/**
128 * struct twl6030_gpadc_data - GPADC data
129 * @dev: device pointer
130 * @lock: mutual exclusion lock for the structure
131 * @irq_complete: completion to signal end of conversion
132 * @twl6030_cal_tbl: pointer to calibration data for each
133 * channel with gain error and offset
134 * @pdata: pointer to device specific data
135 */
136struct twl6030_gpadc_data {
137 struct device *dev;
138 struct mutex lock;
139 struct completion irq_complete;
140 struct twl6030_chnl_calib *twl6030_cal_tbl;
141 const struct twl6030_gpadc_platform_data *pdata;
142};
143
144/*
145 * channels 11, 12, 13, 15 and 16 have no calibration data
146 * calibration offset is same for channels 1, 3, 4, 5
147 *
148 * The data is taken from GPADC_TRIM registers description.
149 * GPADC_TRIM registers keep difference between the code measured
150 * at volt1 and volt2 input voltages and corresponding code1 and code2
151 */
152static const struct twl6030_ideal_code
153 twl6030_ideal[TWL6030_GPADC_USED_CHANNELS] = {
154 [0] = { /* ch 0, external, battery type, resistor value */
155 .channel = 0,
156 .code1 = 116,
157 .code2 = 745,
158 .volt1 = 141,
159 .volt2 = 910,
160 },
161 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
162 .channel = 1,
163 .code1 = 82,
164 .code2 = 900,
165 .volt1 = 100,
166 .volt2 = 1100,
167 },
168 [2] = { /* ch 2, external, audio accessory/general purpose */
169 .channel = 2,
170 .code1 = 55,
171 .code2 = 818,
172 .volt1 = 101,
173 .volt2 = 1499,
174 },
175 [3] = { /* ch 3, external, general purpose */
176 .channel = 3,
177 .code1 = 82,
178 .code2 = 900,
179 .volt1 = 100,
180 .volt2 = 1100,
181 },
182 [4] = { /* ch 4, external, temperature measurement/general purpose */
183 .channel = 4,
184 .code1 = 82,
185 .code2 = 900,
186 .volt1 = 100,
187 .volt2 = 1100,
188 },
189 [5] = { /* ch 5, external, general purpose */
190 .channel = 5,
191 .code1 = 82,
192 .code2 = 900,
193 .volt1 = 100,
194 .volt2 = 1100,
195 },
196 [6] = { /* ch 6, external, general purpose */
197 .channel = 6,
198 .code1 = 82,
199 .code2 = 900,
200 .volt1 = 100,
201 .volt2 = 1100,
202 },
203 [7] = { /* ch 7, internal, main battery */
204 .channel = 7,
205 .code1 = 614,
206 .code2 = 941,
207 .volt1 = 3001,
208 .volt2 = 4599,
209 },
210 [8] = { /* ch 8, internal, backup battery */
211 .channel = 8,
212 .code1 = 82,
213 .code2 = 688,
214 .volt1 = 501,
215 .volt2 = 4203,
216 },
217 [9] = { /* ch 9, internal, external charger input */
218 .channel = 9,
219 .code1 = 182,
220 .code2 = 818,
221 .volt1 = 2001,
222 .volt2 = 8996,
223 },
224 [10] = { /* ch 10, internal, VBUS */
225 .channel = 10,
226 .code1 = 149,
227 .code2 = 818,
228 .volt1 = 1001,
229 .volt2 = 5497,
230 },
231 [11] = { /* ch 11, internal, VBUS charging current */
232 .channel = 11,
233 },
234 /* ch 12, internal, Die temperature */
235 /* ch 13, internal, Die temperature */
236 [12] = { /* ch 14, internal, USB ID line */
237 .channel = 14,
238 .code1 = 48,
239 .code2 = 714,
240 .volt1 = 323,
241 .volt2 = 4800,
242 },
243};
244
245static const struct twl6030_ideal_code
246 twl6032_ideal[TWL6032_GPADC_USED_CHANNELS] = {
247 [0] = { /* ch 0, external, battery type, resistor value */
248 .channel = 0,
249 .code1 = 1441,
250 .code2 = 3276,
251 .volt1 = 440,
252 .volt2 = 1000,
253 },
254 [1] = { /* ch 1, external, battery temperature, NTC resistor value */
255 .channel = 1,
256 .code1 = 1441,
257 .code2 = 3276,
258 .volt1 = 440,
259 .volt2 = 1000,
260 },
261 [2] = { /* ch 2, external, audio accessory/general purpose */
262 .channel = 2,
263 .code1 = 1441,
264 .code2 = 3276,
265 .volt1 = 660,
266 .volt2 = 1500,
267 },
268 [3] = { /* ch 3, external, temperature with external diode/general
269 purpose */
270 .channel = 3,
271 .code1 = 1441,
272 .code2 = 3276,
273 .volt1 = 440,
274 .volt2 = 1000,
275 },
276 [4] = { /* ch 4, external, temperature measurement/general purpose */
277 .channel = 4,
278 .code1 = 1441,
279 .code2 = 3276,
280 .volt1 = 440,
281 .volt2 = 1000,
282 },
283 [5] = { /* ch 5, external, general purpose */
284 .channel = 5,
285 .code1 = 1441,
286 .code2 = 3276,
287 .volt1 = 440,
288 .volt2 = 1000,
289 },
290 [6] = { /* ch 6, external, general purpose */
291 .channel = 6,
292 .code1 = 1441,
293 .code2 = 3276,
294 .volt1 = 440,
295 .volt2 = 1000,
296 },
297 [7] = { /* ch7, internal, system supply */
298 .channel = 7,
299 .code1 = 1441,
300 .code2 = 3276,
301 .volt1 = 2200,
302 .volt2 = 5000,
303 },
304 [8] = { /* ch8, internal, backup battery */
305 .channel = 8,
306 .code1 = 1441,
307 .code2 = 3276,
308 .volt1 = 2200,
309 .volt2 = 5000,
310 },
311 [9] = { /* ch 9, internal, external charger input */
312 .channel = 9,
313 .code1 = 1441,
314 .code2 = 3276,
315 .volt1 = 3960,
316 .volt2 = 9000,
317 },
318 [10] = { /* ch10, internal, VBUS */
319 .channel = 10,
320 .code1 = 150,
321 .code2 = 751,
322 .volt1 = 1000,
323 .volt2 = 5000,
324 },
325 [11] = { /* ch 11, internal, VBUS DC-DC output current */
326 .channel = 11,
327 .code1 = 1441,
328 .code2 = 3276,
329 .volt1 = 660,
330 .volt2 = 1500,
331 },
332 /* ch 12, internal, Die temperature */
333 /* ch 13, internal, Die temperature */
334 [12] = { /* ch 14, internal, USB ID line */
335 .channel = 14,
336 .code1 = 1441,
337 .code2 = 3276,
338 .volt1 = 2420,
339 .volt2 = 5500,
340 },
341 /* ch 15, internal, test network */
342 /* ch 16, internal, test network */
343 [13] = { /* ch 17, internal, battery charging current */
344 .channel = 17,
345 },
346 [14] = { /* ch 18, internal, battery voltage */
347 .channel = 18,
348 .code1 = 1441,
349 .code2 = 3276,
350 .volt1 = 2200,
351 .volt2 = 5000,
352 },
353};
354
355static inline int twl6030_gpadc_write(u8 reg, u8 val)
356{
357 return twl_i2c_write_u8(TWL6030_MODULE_GPADC, val, reg);
358}
359
360static inline int twl6030_gpadc_read(u8 reg, u8 *val)
361{
362
363 return twl_i2c_read(TWL6030_MODULE_GPADC, val, reg, 2);
364}
365
366static int twl6030_gpadc_enable_irq(u8 mask)
367{
368 int ret;
369
370 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_LINE_B);
371 if (ret < 0)
372 return ret;
373
374 ret = twl6030_interrupt_unmask(mask, REG_INT_MSK_STS_B);
375
376 return ret;
377}
378
379static void twl6030_gpadc_disable_irq(u8 mask)
380{
381 twl6030_interrupt_mask(mask, REG_INT_MSK_LINE_B);
382 twl6030_interrupt_mask(mask, REG_INT_MSK_STS_B);
383}
384
385static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *indio_dev)
386{
387 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
388
389 complete(&gpadc->irq_complete);
390
391 return IRQ_HANDLED;
392}
393
394static int twl6030_start_conversion(int channel)
395{
396 return twl6030_gpadc_write(TWL6030_GPADC_CTRL_P1,
397 TWL6030_GPADC_CTRL_P1_SP1);
398}
399
400static int twl6032_start_conversion(int channel)
401{
402 int ret;
403
404 ret = twl6030_gpadc_write(TWL6032_GPADC_GPSELECT_ISB, channel);
405 if (ret)
406 return ret;
407
408 return twl6030_gpadc_write(TWL6032_GPADC_CTRL_P1,
409 TWL6030_GPADC_CTRL_P1_SP1);
410}
411
412static u8 twl6030_channel_to_reg(int channel)
413{
414 return TWL6030_GPADC_GPCH0_LSB + 2 * channel;
415}
416
417static u8 twl6032_channel_to_reg(int channel)
418{
419 /*
420 * for any prior chosen channel, when the conversion is ready
421 * the result is avalable in GPCH0_LSB, GPCH0_MSB.
422 */
423
424 return TWL6032_GPADC_GPCH0_LSB;
425}
426
427static int twl6030_gpadc_lookup(const struct twl6030_ideal_code *ideal,
428 int channel, int size)
429{
430 int i;
431
432 for (i = 0; i < size; i++)
433 if (ideal[i].channel == channel)
434 break;
435
436 return i;
437}
438
439static int twl6030_channel_calibrated(const struct twl6030_gpadc_platform_data
440 *pdata, int channel)
441{
442 const struct twl6030_ideal_code *ideal = pdata->ideal;
443 int i;
444
445 i = twl6030_gpadc_lookup(ideal, channel, pdata->nchannels);
446 /* not calibrated channels have 0 in all structure members */
447 return pdata->ideal[i].code2;
448}
449
450static int twl6030_gpadc_make_correction(struct twl6030_gpadc_data *gpadc,
451 int channel, int raw_code)
452{
453 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
454 int corrected_code;
455 int i;
456
457 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
458 corrected_code = ((raw_code * 1000) -
459 gpadc->twl6030_cal_tbl[i].offset_error) /
460 gpadc->twl6030_cal_tbl[i].gain_error;
461
462 return corrected_code;
463}
464
465static int twl6030_gpadc_get_raw(struct twl6030_gpadc_data *gpadc,
466 int channel, int *res)
467{
468 u8 reg = gpadc->pdata->channel_to_reg(channel);
469 __le16 val;
470 int raw_code;
471 int ret;
472
473 ret = twl6030_gpadc_read(reg, (u8 *)&val);
474 if (ret) {
475 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
476 return ret;
477 }
478
479 raw_code = le16_to_cpu(val);
480 dev_dbg(gpadc->dev, "GPADC raw code: %d", raw_code);
481
482 if (twl6030_channel_calibrated(gpadc->pdata, channel))
483 *res = twl6030_gpadc_make_correction(gpadc, channel, raw_code);
484 else
485 *res = raw_code;
486
487 return ret;
488}
489
490static int twl6030_gpadc_get_processed(struct twl6030_gpadc_data *gpadc,
491 int channel, int *val)
492{
493 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
494 int corrected_code;
495 int channel_value;
496 int i;
497 int ret;
498
499 ret = twl6030_gpadc_get_raw(gpadc, channel, &corrected_code);
500 if (ret)
501 return ret;
502
503 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
504 channel_value = corrected_code *
505 gpadc->twl6030_cal_tbl[i].gain;
506
507 /* Shift back into mV range */
508 channel_value /= 1000;
509
510 dev_dbg(gpadc->dev, "GPADC corrected code: %d", corrected_code);
511 dev_dbg(gpadc->dev, "GPADC value: %d", channel_value);
512
513 *val = channel_value;
514
515 return ret;
516}
517
518static int twl6030_gpadc_read_raw(struct iio_dev *indio_dev,
519 const struct iio_chan_spec *chan,
520 int *val, int *val2, long mask)
521{
522 struct twl6030_gpadc_data *gpadc = iio_priv(indio_dev);
523 int ret;
524 long timeout;
525
526 mutex_lock(&gpadc->lock);
527
528 ret = gpadc->pdata->start_conversion(chan->channel);
529 if (ret) {
530 dev_err(gpadc->dev, "failed to start conversion\n");
531 goto err;
532 }
533 /* wait for conversion to complete */
534 timeout = wait_for_completion_interruptible_timeout(
535 &gpadc->irq_complete, msecs_to_jiffies(5000));
536 if (timeout == 0) {
537 ret = -ETIMEDOUT;
538 goto err;
539 } else if (timeout < 0) {
540 ret = -EINTR;
541 goto err;
542 }
543
544 switch (mask) {
545 case IIO_CHAN_INFO_RAW:
546 ret = twl6030_gpadc_get_raw(gpadc, chan->channel, val);
547 ret = ret ? -EIO : IIO_VAL_INT;
548 break;
549
550 case IIO_CHAN_INFO_PROCESSED:
551 ret = twl6030_gpadc_get_processed(gpadc, chan->channel, val);
552 ret = ret ? -EIO : IIO_VAL_INT;
553 break;
554
555 default:
556 break;
557 }
558err:
559 mutex_unlock(&gpadc->lock);
560
561 return ret;
562}
563
564/*
565 * The GPADC channels are calibrated using a two point calibration method.
566 * The channels measured with two known values: volt1 and volt2, and
567 * ideal corresponding output codes are known: code1, code2.
568 * The difference(d1, d2) between ideal and measured codes stored in trim
569 * registers.
570 * The goal is to find offset and gain of the real curve for each calibrated
571 * channel.
572 * gain: k = 1 + ((d2 - d1) / (x2 - x1))
573 * offset: b = d1 + (k - 1) * x1
574 */
575static void twl6030_calibrate_channel(struct twl6030_gpadc_data *gpadc,
576 int channel, int d1, int d2)
577{
578 int b, k, gain, x1, x2, i;
579 const struct twl6030_ideal_code *ideal = gpadc->pdata->ideal;
580
581 i = twl6030_gpadc_lookup(ideal, channel, gpadc->pdata->nchannels);
582
583 /* Gain */
584 gain = ((ideal[i].volt2 - ideal[i].volt1) * 1000) /
585 (ideal[i].code2 - ideal[i].code1);
586
587 x1 = ideal[i].code1;
588 x2 = ideal[i].code2;
589
590 /* k - real curve gain */
591 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
592
593 /* b - offset of the real curve gain */
594 b = (d1 * 1000) - (k - 1000) * x1;
595
596 gpadc->twl6030_cal_tbl[i].gain = gain;
597 gpadc->twl6030_cal_tbl[i].gain_error = k;
598 gpadc->twl6030_cal_tbl[i].offset_error = b;
599
600 dev_dbg(gpadc->dev, "GPADC d1 for Chn: %d = %d\n", channel, d1);
601 dev_dbg(gpadc->dev, "GPADC d2 for Chn: %d = %d\n", channel, d2);
602 dev_dbg(gpadc->dev, "GPADC x1 for Chn: %d = %d\n", channel, x1);
603 dev_dbg(gpadc->dev, "GPADC x2 for Chn: %d = %d\n", channel, x2);
604 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", channel, gain);
605 dev_dbg(gpadc->dev, "GPADC k for Chn: %d = %d\n", channel, k);
606 dev_dbg(gpadc->dev, "GPADC b for Chn: %d = %d\n", channel, b);
607}
608
609static inline int twl6030_gpadc_get_trim_offset(s8 d)
610{
611 /*
612 * XXX NOTE!
613 * bit 0 - sign, bit 7 - reserved, 6..1 - trim value
614 * though, the documentation states that trim value
615 * is absolute value, the correct conversion results are
616 * obtained if the value is interpreted as 2's complement.
617 */
618 __u32 temp = ((d & 0x7f) >> 1) | ((d & 1) << 6);
619
620 return sign_extend32(temp, 6);
621}
622
623static int twl6030_calibration(struct twl6030_gpadc_data *gpadc)
624{
625 int ret;
626 int chn;
627 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
628 s8 d1, d2;
629
630 /*
631 * for calibration two measurements have been performed at
632 * factory, for some channels, during the production test and
633 * have been stored in registers. This two stored values are
634 * used to correct the measurements. The values represent
635 * offsets for the given input from the output on ideal curve.
636 */
637 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
638 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
639 if (ret < 0) {
640 dev_err(gpadc->dev, "calibration failed\n");
641 return ret;
642 }
643
644 for (chn = 0; chn < TWL6030_GPADC_MAX_CHANNELS; chn++) {
645
646 switch (chn) {
647 case 0:
648 d1 = trim_regs[0];
649 d2 = trim_regs[1];
650 break;
651 case 1:
652 case 3:
653 case 4:
654 case 5:
655 case 6:
656 d1 = trim_regs[4];
657 d2 = trim_regs[5];
658 break;
659 case 2:
660 d1 = trim_regs[12];
661 d2 = trim_regs[13];
662 break;
663 case 7:
664 d1 = trim_regs[6];
665 d2 = trim_regs[7];
666 break;
667 case 8:
668 d1 = trim_regs[2];
669 d2 = trim_regs[3];
670 break;
671 case 9:
672 d1 = trim_regs[8];
673 d2 = trim_regs[9];
674 break;
675 case 10:
676 d1 = trim_regs[10];
677 d2 = trim_regs[11];
678 break;
679 case 14:
680 d1 = trim_regs[14];
681 d2 = trim_regs[15];
682 break;
683 default:
684 continue;
685 }
686
687 d1 = twl6030_gpadc_get_trim_offset(d1);
688 d2 = twl6030_gpadc_get_trim_offset(d2);
689
690 twl6030_calibrate_channel(gpadc, chn, d1, d2);
691 }
692
693 return 0;
694}
695
696static int twl6032_get_trim_value(u8 *trim_regs, unsigned int reg0,
697 unsigned int reg1, unsigned int mask0, unsigned int mask1,
698 unsigned int shift0)
699{
700 int val;
701
702 val = (trim_regs[reg0] & mask0) << shift0;
703 val |= (trim_regs[reg1] & mask1) >> 1;
704 if (trim_regs[reg1] & 0x01)
705 val = -val;
706
707 return val;
708}
709
710static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
711{
712 int chn, d1 = 0, d2 = 0, temp;
713 u8 trim_regs[TWL6030_GPADC_NUM_TRIM_REGS];
714 int ret;
715
716 ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs,
717 TWL6030_GPADC_TRIM1, TWL6030_GPADC_NUM_TRIM_REGS);
718 if (ret < 0) {
719 dev_err(gpadc->dev, "calibration failed\n");
720 return ret;
721 }
722
723 /*
724 * Loop to calculate the value needed for returning voltages from
725 * GPADC not values.
726 *
727 * gain is calculated to 3 decimal places fixed point.
728 */
729 for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
730
731 switch (chn) {
732 case 0:
733 case 1:
734 case 2:
735 case 3:
736 case 4:
737 case 5:
738 case 6:
739 case 11:
740 case 14:
741 d1 = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
742 0x06, 2);
743 d2 = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
744 0x06, 2);
745 break;
746 case 8:
747 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
748 0x06, 2);
749 d1 = temp + twl6032_get_trim_value(trim_regs, 7, 6,
750 0x18, 0x1E, 1);
751
752 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3F,
753 0x06, 2);
754 d2 = temp + twl6032_get_trim_value(trim_regs, 9, 7,
755 0x1F, 0x06, 2);
756 break;
757 case 9:
758 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
759 0x06, 2);
760 d1 = temp + twl6032_get_trim_value(trim_regs, 13, 11,
761 0x18, 0x1E, 1);
762
763 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
764 0x06, 2);
765 d2 = temp + twl6032_get_trim_value(trim_regs, 15, 13,
766 0x1F, 0x06, 1);
767 break;
768 case 10:
769 d1 = twl6032_get_trim_value(trim_regs, 10, 8, 0x0f,
770 0x0E, 3);
771 d2 = twl6032_get_trim_value(trim_regs, 14, 12, 0x0f,
772 0x0E, 3);
773 break;
774 case 7:
775 case 18:
776 temp = twl6032_get_trim_value(trim_regs, 2, 0, 0x1f,
777 0x06, 2);
778
779 d1 = (trim_regs[4] & 0x7E) >> 1;
780 if (trim_regs[4] & 0x01)
781 d1 = -d1;
782 d1 += temp;
783
784 temp = twl6032_get_trim_value(trim_regs, 3, 1, 0x3f,
785 0x06, 2);
786
787 d2 = (trim_regs[5] & 0xFE) >> 1;
788 if (trim_regs[5] & 0x01)
789 d2 = -d2;
790
791 d2 += temp;
792 break;
793 default:
794 /* No data for other channels */
795 continue;
796 }
797
798 twl6030_calibrate_channel(gpadc, chn, d1, d2);
799 }
800
801 return 0;
802}
803
804#define TWL6030_GPADC_CHAN(chn, _type, chan_info) { \
805 .type = _type, \
806 .channel = chn, \
807 .info_mask_separate = BIT(chan_info), \
808 .indexed = 1, \
809}
810
811static const struct iio_chan_spec twl6030_gpadc_iio_channels[] = {
812 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
813 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
814 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
815 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
816 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
817 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
818 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
819 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
820 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
821 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
822 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
823 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
824 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
825};
826
827static const struct iio_chan_spec twl6032_gpadc_iio_channels[] = {
828 TWL6030_GPADC_CHAN(0, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
829 TWL6030_GPADC_CHAN(1, IIO_TEMP, IIO_CHAN_INFO_RAW),
830 TWL6030_GPADC_CHAN(2, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
831 TWL6030_GPADC_CHAN(3, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
832 TWL6030_GPADC_CHAN(4, IIO_TEMP, IIO_CHAN_INFO_RAW),
833 TWL6030_GPADC_CHAN(5, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
834 TWL6030_GPADC_CHAN(6, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
835 TWL6030_GPADC_CHAN(7, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
836 TWL6030_GPADC_CHAN(8, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
837 TWL6030_GPADC_CHAN(9, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
838 TWL6030_GPADC_CHAN(10, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
839 TWL6030_GPADC_CHAN(11, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
840 TWL6030_GPADC_CHAN(14, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
841 TWL6030_GPADC_CHAN(17, IIO_VOLTAGE, IIO_CHAN_INFO_RAW),
842 TWL6030_GPADC_CHAN(18, IIO_VOLTAGE, IIO_CHAN_INFO_PROCESSED),
843};
844
845static const struct iio_info twl6030_gpadc_iio_info = {
846 .read_raw = &twl6030_gpadc_read_raw,
847 .driver_module = THIS_MODULE,
848};
849
850static const struct twl6030_gpadc_platform_data twl6030_pdata = {
851 .iio_channels = twl6030_gpadc_iio_channels,
852 .nchannels = TWL6030_GPADC_USED_CHANNELS,
853 .ideal = twl6030_ideal,
854 .start_conversion = twl6030_start_conversion,
855 .channel_to_reg = twl6030_channel_to_reg,
856 .calibrate = twl6030_calibration,
857};
858
859static const struct twl6030_gpadc_platform_data twl6032_pdata = {
860 .iio_channels = twl6032_gpadc_iio_channels,
861 .nchannels = TWL6032_GPADC_USED_CHANNELS,
862 .ideal = twl6032_ideal,
863 .start_conversion = twl6032_start_conversion,
864 .channel_to_reg = twl6032_channel_to_reg,
865 .calibrate = twl6032_calibration,
866};
867
868static const struct of_device_id of_twl6030_match_tbl[] = {
869 {
870 .compatible = "ti,twl6030-gpadc",
871 .data = &twl6030_pdata,
872 },
873 {
874 .compatible = "ti,twl6032-gpadc",
875 .data = &twl6032_pdata,
876 },
877 { /* end */ }
878};
879
880static int twl6030_gpadc_probe(struct platform_device *pdev)
881{
882 struct device *dev = &pdev->dev;
883 struct twl6030_gpadc_data *gpadc;
884 const struct twl6030_gpadc_platform_data *pdata;
885 const struct of_device_id *match;
886 struct iio_dev *indio_dev;
887 int irq;
888 int ret;
889
890 match = of_match_device(of_match_ptr(of_twl6030_match_tbl), dev);
891 if (!match)
892 return -EINVAL;
893
894 pdata = match->data;
895
896 indio_dev = devm_iio_device_alloc(dev, sizeof(*gpadc));
897 if (!indio_dev)
898 return -ENOMEM;
899
900 gpadc = iio_priv(indio_dev);
901
902 gpadc->twl6030_cal_tbl = devm_kzalloc(dev,
903 sizeof(*gpadc->twl6030_cal_tbl) *
904 pdata->nchannels, GFP_KERNEL);
905 if (!gpadc->twl6030_cal_tbl)
906 return -ENOMEM;
907
908 gpadc->dev = dev;
909 gpadc->pdata = pdata;
910
911 platform_set_drvdata(pdev, indio_dev);
912 mutex_init(&gpadc->lock);
913 init_completion(&gpadc->irq_complete);
914
915 ret = pdata->calibrate(gpadc);
916 if (ret < 0) {
917 dev_err(&pdev->dev, "failed to read calibration registers\n");
918 return ret;
919 }
920
921 irq = platform_get_irq(pdev, 0);
922 if (irq < 0) {
923 dev_err(&pdev->dev, "failed to get irq\n");
924 return irq;
925 }
926
927 ret = devm_request_threaded_irq(dev, irq, NULL,
928 twl6030_gpadc_irq_handler,
929 IRQF_ONESHOT, "twl6030_gpadc", indio_dev);
930
931 ret = twl6030_gpadc_enable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
932 if (ret < 0) {
933 dev_err(&pdev->dev, "failed to enable GPADC interrupt\n");
934 return ret;
935 }
936
937 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
938 TWL6030_REG_TOGGLE1);
939 if (ret < 0) {
940 dev_err(&pdev->dev, "failed to enable GPADC module\n");
941 return ret;
942 }
943
944 indio_dev->name = DRIVER_NAME;
945 indio_dev->dev.parent = dev;
946 indio_dev->info = &twl6030_gpadc_iio_info;
947 indio_dev->modes = INDIO_DIRECT_MODE;
948 indio_dev->channels = pdata->iio_channels;
949 indio_dev->num_channels = pdata->nchannels;
950
951 ret = iio_device_register(indio_dev);
952
953 return ret;
954}
955
956static int twl6030_gpadc_remove(struct platform_device *pdev)
957{
958 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
959
960 twl6030_gpadc_disable_irq(TWL6030_GPADC_RT_SW1_EOC_MASK);
961 iio_device_unregister(indio_dev);
962
963 return 0;
964}
965
966#ifdef CONFIG_PM_SLEEP
967static int twl6030_gpadc_suspend(struct device *pdev)
968{
969 int ret;
970
971 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCR,
972 TWL6030_REG_TOGGLE1);
973 if (ret)
974 dev_err(pdev, "error reseting GPADC (%d)!\n", ret);
975
976 return 0;
977};
978
979static int twl6030_gpadc_resume(struct device *pdev)
980{
981 int ret;
982
983 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, TWL6030_GPADCS,
984 TWL6030_REG_TOGGLE1);
985 if (ret)
986 dev_err(pdev, "error setting GPADC (%d)!\n", ret);
987
988 return 0;
989};
990#endif
991
992static SIMPLE_DEV_PM_OPS(twl6030_gpadc_pm_ops, twl6030_gpadc_suspend,
993 twl6030_gpadc_resume);
994
995static struct platform_driver twl6030_gpadc_driver = {
996 .probe = twl6030_gpadc_probe,
997 .remove = twl6030_gpadc_remove,
998 .driver = {
999 .name = DRIVER_NAME,
1000 .owner = THIS_MODULE,
1001 .pm = &twl6030_gpadc_pm_ops,
1002 .of_match_table = of_twl6030_match_tbl,
1003 },
1004};
1005
1006module_platform_driver(twl6030_gpadc_driver);
1007
1008MODULE_ALIAS("platform: " DRIVER_NAME);
1009MODULE_AUTHOR("Balaji T K <balajitk@ti.com>");
1010MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1011MODULE_AUTHOR("Oleksandr Kozaruk <oleksandr.kozaruk@ti.com");
1012MODULE_DESCRIPTION("twl6030 ADC driver");
1013MODULE_LICENSE("GPL");
diff --git a/drivers/iio/adc/viperboard_adc.c b/drivers/iio/adc/viperboard_adc.c
index 56ac481c73c0..09727a71e9fa 100644
--- a/drivers/iio/adc/viperboard_adc.c
+++ b/drivers/iio/adc/viperboard_adc.c
@@ -124,7 +124,7 @@ static int vprbrd_adc_probe(struct platform_device *pdev)
124 int ret; 124 int ret;
125 125
126 /* registering iio */ 126 /* registering iio */
127 indio_dev = iio_device_alloc(sizeof(*adc)); 127 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
128 if (!indio_dev) { 128 if (!indio_dev) {
129 dev_err(&pdev->dev, "failed allocating iio device\n"); 129 dev_err(&pdev->dev, "failed allocating iio device\n");
130 return -ENOMEM; 130 return -ENOMEM;
@@ -142,16 +142,12 @@ static int vprbrd_adc_probe(struct platform_device *pdev)
142 ret = iio_device_register(indio_dev); 142 ret = iio_device_register(indio_dev);
143 if (ret) { 143 if (ret) {
144 dev_err(&pdev->dev, "could not register iio (adc)"); 144 dev_err(&pdev->dev, "could not register iio (adc)");
145 goto error; 145 return ret;
146 } 146 }
147 147
148 platform_set_drvdata(pdev, indio_dev); 148 platform_set_drvdata(pdev, indio_dev);
149 149
150 return 0; 150 return 0;
151
152error:
153 iio_device_free(indio_dev);
154 return ret;
155} 151}
156 152
157static int vprbrd_adc_remove(struct platform_device *pdev) 153static int vprbrd_adc_remove(struct platform_device *pdev)
@@ -159,7 +155,6 @@ static int vprbrd_adc_remove(struct platform_device *pdev)
159 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 155 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
160 156
161 iio_device_unregister(indio_dev); 157 iio_device_unregister(indio_dev);
162 iio_device_free(indio_dev);
163 158
164 return 0; 159 return 0;
165} 160}
diff --git a/drivers/iio/amplifiers/Kconfig b/drivers/iio/amplifiers/Kconfig
index 05d707ed7d4f..e9c5f2cd9257 100644
--- a/drivers/iio/amplifiers/Kconfig
+++ b/drivers/iio/amplifiers/Kconfig
@@ -1,6 +1,8 @@
1# 1#
2# Gain Amplifiers, etc. 2# Gain Amplifiers, etc.
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Amplifiers" 6menu "Amplifiers"
5 7
6config AD8366 8config AD8366
diff --git a/drivers/iio/amplifiers/Makefile b/drivers/iio/amplifiers/Makefile
index a6ca366908e0..8da4b787898a 100644
--- a/drivers/iio/amplifiers/Makefile
+++ b/drivers/iio/amplifiers/Makefile
@@ -2,4 +2,5 @@
2# Makefile iio/amplifiers 2# Makefile iio/amplifiers
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_AD8366) += ad8366.o 6obj-$(CONFIG_AD8366) += ad8366.o
diff --git a/drivers/iio/amplifiers/ad8366.c b/drivers/iio/amplifiers/ad8366.c
index d354554b51b3..d0a79a4bce1c 100644
--- a/drivers/iio/amplifiers/ad8366.c
+++ b/drivers/iio/amplifiers/ad8366.c
@@ -139,17 +139,17 @@ static int ad8366_probe(struct spi_device *spi)
139 struct ad8366_state *st; 139 struct ad8366_state *st;
140 int ret; 140 int ret;
141 141
142 indio_dev = iio_device_alloc(sizeof(*st)); 142 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
143 if (indio_dev == NULL) 143 if (indio_dev == NULL)
144 return -ENOMEM; 144 return -ENOMEM;
145 145
146 st = iio_priv(indio_dev); 146 st = iio_priv(indio_dev);
147 147
148 st->reg = regulator_get(&spi->dev, "vcc"); 148 st->reg = devm_regulator_get(&spi->dev, "vcc");
149 if (!IS_ERR(st->reg)) { 149 if (!IS_ERR(st->reg)) {
150 ret = regulator_enable(st->reg); 150 ret = regulator_enable(st->reg);
151 if (ret) 151 if (ret)
152 goto error_put_reg; 152 return ret;
153 } 153 }
154 154
155 spi_set_drvdata(spi, indio_dev); 155 spi_set_drvdata(spi, indio_dev);
@@ -173,11 +173,6 @@ static int ad8366_probe(struct spi_device *spi)
173error_disable_reg: 173error_disable_reg:
174 if (!IS_ERR(st->reg)) 174 if (!IS_ERR(st->reg))
175 regulator_disable(st->reg); 175 regulator_disable(st->reg);
176error_put_reg:
177 if (!IS_ERR(st->reg))
178 regulator_put(st->reg);
179
180 iio_device_free(indio_dev);
181 176
182 return ret; 177 return ret;
183} 178}
@@ -195,8 +190,6 @@ static int ad8366_remove(struct spi_device *spi)
195 regulator_put(reg); 190 regulator_put(reg);
196 } 191 }
197 192
198 iio_device_free(indio_dev);
199
200 return 0; 193 return 0;
201} 194}
202 195
diff --git a/drivers/iio/common/Makefile b/drivers/iio/common/Makefile
index c2352beb5d97..3112df0060e9 100644
--- a/drivers/iio/common/Makefile
+++ b/drivers/iio/common/Makefile
@@ -6,5 +6,6 @@
6# instead of duplicating in each module. 6# instead of duplicating in each module.
7# 7#
8 8
9# When adding new entries keep the list in alphabetical order
9obj-y += hid-sensors/ 10obj-y += hid-sensors/
10obj-y += st_sensors/ 11obj-y += st_sensors/
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index 865b1781df66..965ee22d3ac8 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -22,7 +22,7 @@
22 22
23static inline u32 st_sensors_get_unaligned_le24(const u8 *p) 23static inline u32 st_sensors_get_unaligned_le24(const u8 *p)
24{ 24{
25 return ((s32)((p[0] | p[1] << 8 | p[2] << 16) << 8) >> 8); 25 return (s32)((p[0] | p[1] << 8 | p[2] << 16) << 8) >> 8;
26} 26}
27 27
28static int st_sensors_write_data_with_mask(struct iio_dev *indio_dev, 28static int st_sensors_write_data_with_mask(struct iio_dev *indio_dev,
@@ -118,7 +118,7 @@ st_sensors_match_odr_error:
118} 118}
119 119
120static int st_sensors_set_fullscale(struct iio_dev *indio_dev, 120static int st_sensors_set_fullscale(struct iio_dev *indio_dev,
121 unsigned int fs) 121 unsigned int fs)
122{ 122{
123 int err, i = 0; 123 int err, i = 0;
124 struct st_sensor_data *sdata = iio_priv(indio_dev); 124 struct st_sensor_data *sdata = iio_priv(indio_dev);
@@ -198,13 +198,39 @@ int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable)
198} 198}
199EXPORT_SYMBOL(st_sensors_set_axis_enable); 199EXPORT_SYMBOL(st_sensors_set_axis_enable);
200 200
201int st_sensors_init_sensor(struct iio_dev *indio_dev) 201int st_sensors_init_sensor(struct iio_dev *indio_dev,
202 struct st_sensors_platform_data *pdata)
202{ 203{
203 int err; 204 int err;
204 struct st_sensor_data *sdata = iio_priv(indio_dev); 205 struct st_sensor_data *sdata = iio_priv(indio_dev);
205 206
206 mutex_init(&sdata->tb.buf_lock); 207 mutex_init(&sdata->tb.buf_lock);
207 208
209 switch (pdata->drdy_int_pin) {
210 case 1:
211 if (sdata->sensor->drdy_irq.mask_int1 == 0) {
212 dev_err(&indio_dev->dev,
213 "DRDY on INT1 not available.\n");
214 err = -EINVAL;
215 goto init_error;
216 }
217 sdata->drdy_int_pin = 1;
218 break;
219 case 2:
220 if (sdata->sensor->drdy_irq.mask_int2 == 0) {
221 dev_err(&indio_dev->dev,
222 "DRDY on INT2 not available.\n");
223 err = -EINVAL;
224 goto init_error;
225 }
226 sdata->drdy_int_pin = 2;
227 break;
228 default:
229 dev_err(&indio_dev->dev, "DRDY on pdata not valid.\n");
230 err = -EINVAL;
231 goto init_error;
232 }
233
208 err = st_sensors_set_enable(indio_dev, false); 234 err = st_sensors_set_enable(indio_dev, false);
209 if (err < 0) 235 if (err < 0)
210 goto init_error; 236 goto init_error;
@@ -234,6 +260,7 @@ EXPORT_SYMBOL(st_sensors_init_sensor);
234int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable) 260int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable)
235{ 261{
236 int err; 262 int err;
263 u8 drdy_mask;
237 struct st_sensor_data *sdata = iio_priv(indio_dev); 264 struct st_sensor_data *sdata = iio_priv(indio_dev);
238 265
239 /* Enable/Disable the interrupt generator 1. */ 266 /* Enable/Disable the interrupt generator 1. */
@@ -245,10 +272,14 @@ int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable)
245 goto st_accel_set_dataready_irq_error; 272 goto st_accel_set_dataready_irq_error;
246 } 273 }
247 274
275 if (sdata->drdy_int_pin == 1)
276 drdy_mask = sdata->sensor->drdy_irq.mask_int1;
277 else
278 drdy_mask = sdata->sensor->drdy_irq.mask_int2;
279
248 /* Enable/Disable the interrupt generator for data ready. */ 280 /* Enable/Disable the interrupt generator for data ready. */
249 err = st_sensors_write_data_with_mask(indio_dev, 281 err = st_sensors_write_data_with_mask(indio_dev,
250 sdata->sensor->drdy_irq.addr, 282 sdata->sensor->drdy_irq.addr, drdy_mask, (int)enable);
251 sdata->sensor->drdy_irq.mask, (int)enable);
252 283
253st_accel_set_dataready_irq_error: 284st_accel_set_dataready_irq_error:
254 return err; 285 return err;
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index c9c33ce32d3a..3c6a78a75b78 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -1,6 +1,8 @@
1# 1#
2# DAC drivers 2# DAC drivers
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Digital to analog converters" 6menu "Digital to analog converters"
5 7
6config AD5064 8config AD5064
@@ -15,7 +17,7 @@ config AD5064
15 module will be called ad5064. 17 module will be called ad5064.
16 18
17config AD5360 19config AD5360
18 tristate "Analog Devices Analog Devices AD5360/61/62/63/70/71/73 DAC driver" 20 tristate "Analog Devices AD5360/61/62/63/70/71/73 DAC driver"
19 depends on SPI 21 depends on SPI
20 help 22 help
21 Say yes here to build support for Analog Devices AD5360, AD5361, 23 Say yes here to build support for Analog Devices AD5360, AD5361,
@@ -48,13 +50,6 @@ config AD5421
48 To compile this driver as module choose M here: the module will be called 50 To compile this driver as module choose M here: the module will be called
49 ad5421. 51 ad5421.
50 52
51config AD5624R_SPI
52 tristate "Analog Devices AD5624/44/64R DAC spi driver"
53 depends on SPI
54 help
55 Say yes here to build support for Analog Devices AD5624R, AD5644R and
56 AD5664R converters (DAC). This driver uses the common SPI interface.
57
58config AD5446 53config AD5446
59 tristate "Analog Devices AD5446 and similar single channel DACs driver" 54 tristate "Analog Devices AD5446 and similar single channel DACs driver"
60 depends on (SPI_MASTER && I2C!=m) || I2C 55 depends on (SPI_MASTER && I2C!=m) || I2C
@@ -68,7 +63,7 @@ config AD5446
68 module will be called ad5446. 63 module will be called ad5446.
69 64
70config AD5449 65config AD5449
71 tristate "Analog Device AD5449 and similar DACs driver" 66 tristate "Analog Devices AD5449 and similar DACs driver"
72 depends on SPI_MASTER 67 depends on SPI_MASTER
73 help 68 help
74 Say yes here to build support for Analog Devices AD5415, AD5426, AD5429, 69 Say yes here to build support for Analog Devices AD5415, AD5426, AD5429,
@@ -87,6 +82,24 @@ config AD5504
87 To compile this driver as a module, choose M here: the 82 To compile this driver as a module, choose M here: the
88 module will be called ad5504. 83 module will be called ad5504.
89 84
85config AD5624R_SPI
86 tristate "Analog Devices AD5624/44/64R DAC spi driver"
87 depends on SPI
88 help
89 Say yes here to build support for Analog Devices AD5624R, AD5644R and
90 AD5664R converters (DAC). This driver uses the common SPI interface.
91
92config AD5686
93 tristate "Analog Devices AD5686R/AD5685R/AD5684R DAC SPI driver"
94 depends on SPI
95 help
96 Say yes here to build support for Analog Devices AD5686R, AD5685R,
97 AD5684R, AD5791 Voltage Output Digital to
98 Analog Converter.
99
100 To compile this driver as a module, choose M here: the
101 module will be called ad5686.
102
90config AD5755 103config AD5755
91 tristate "Analog Devices AD5755/AD5755-1/AD5757/AD5735/AD5737 DAC driver" 104 tristate "Analog Devices AD5755/AD5755-1/AD5757/AD5735/AD5737 DAC driver"
92 depends on SPI_MASTER 105 depends on SPI_MASTER
@@ -119,19 +132,8 @@ config AD5791
119 To compile this driver as a module, choose M here: the 132 To compile this driver as a module, choose M here: the
120 module will be called ad5791. 133 module will be called ad5791.
121 134
122config AD5686
123 tristate "Analog Devices AD5686R/AD5685R/AD5684R DAC SPI driver"
124 depends on SPI
125 help
126 Say yes here to build support for Analog Devices AD5686R, AD5685R,
127 AD5684R, AD5791 Voltage Output Digital to
128 Analog Converter.
129
130 To compile this driver as a module, choose M here: the
131 module will be called ad5686.
132
133config AD7303 135config AD7303
134 tristate "Analog Devices Analog Devices AD7303 DAC driver" 136 tristate "Analog Devices AD7303 DAC driver"
135 depends on SPI 137 depends on SPI
136 help 138 help
137 Say yes here to build support for Analog Devices AD7303 Digital to Analog 139 Say yes here to build support for Analog Devices AD7303 Digital to Analog
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index c8d7ab6bff01..bb84ad64463f 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -2,6 +2,7 @@
2# Makefile for industrial I/O DAC drivers 2# Makefile for industrial I/O DAC drivers
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_AD5360) += ad5360.o 6obj-$(CONFIG_AD5360) += ad5360.o
6obj-$(CONFIG_AD5380) += ad5380.o 7obj-$(CONFIG_AD5380) += ad5380.o
7obj-$(CONFIG_AD5421) += ad5421.o 8obj-$(CONFIG_AD5421) += ad5421.o
diff --git a/drivers/iio/dac/ad5064.c b/drivers/iio/dac/ad5064.c
index aa26d50ab638..a3a52be4852c 100644
--- a/drivers/iio/dac/ad5064.c
+++ b/drivers/iio/dac/ad5064.c
@@ -442,7 +442,7 @@ static int ad5064_probe(struct device *dev, enum ad5064_type type,
442 unsigned int i; 442 unsigned int i;
443 int ret; 443 int ret;
444 444
445 indio_dev = iio_device_alloc(sizeof(*st)); 445 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
446 if (indio_dev == NULL) 446 if (indio_dev == NULL)
447 return -ENOMEM; 447 return -ENOMEM;
448 448
@@ -456,23 +456,23 @@ static int ad5064_probe(struct device *dev, enum ad5064_type type,
456 for (i = 0; i < ad5064_num_vref(st); ++i) 456 for (i = 0; i < ad5064_num_vref(st); ++i)
457 st->vref_reg[i].supply = ad5064_vref_name(st, i); 457 st->vref_reg[i].supply = ad5064_vref_name(st, i);
458 458
459 ret = regulator_bulk_get(dev, ad5064_num_vref(st), 459 ret = devm_regulator_bulk_get(dev, ad5064_num_vref(st),
460 st->vref_reg); 460 st->vref_reg);
461 if (ret) { 461 if (ret) {
462 if (!st->chip_info->internal_vref) 462 if (!st->chip_info->internal_vref)
463 goto error_free; 463 return ret;
464 st->use_internal_vref = true; 464 st->use_internal_vref = true;
465 ret = ad5064_write(st, AD5064_CMD_CONFIG, 0, 465 ret = ad5064_write(st, AD5064_CMD_CONFIG, 0,
466 AD5064_CONFIG_INT_VREF_ENABLE, 0); 466 AD5064_CONFIG_INT_VREF_ENABLE, 0);
467 if (ret) { 467 if (ret) {
468 dev_err(dev, "Failed to enable internal vref: %d\n", 468 dev_err(dev, "Failed to enable internal vref: %d\n",
469 ret); 469 ret);
470 goto error_free; 470 return ret;
471 } 471 }
472 } else { 472 } else {
473 ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg); 473 ret = regulator_bulk_enable(ad5064_num_vref(st), st->vref_reg);
474 if (ret) 474 if (ret)
475 goto error_free_reg; 475 return ret;
476 } 476 }
477 477
478 indio_dev->dev.parent = dev; 478 indio_dev->dev.parent = dev;
@@ -498,11 +498,6 @@ static int ad5064_probe(struct device *dev, enum ad5064_type type,
498error_disable_reg: 498error_disable_reg:
499 if (!st->use_internal_vref) 499 if (!st->use_internal_vref)
500 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg); 500 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
501error_free_reg:
502 if (!st->use_internal_vref)
503 regulator_bulk_free(ad5064_num_vref(st), st->vref_reg);
504error_free:
505 iio_device_free(indio_dev);
506 501
507 return ret; 502 return ret;
508} 503}
@@ -514,12 +509,8 @@ static int ad5064_remove(struct device *dev)
514 509
515 iio_device_unregister(indio_dev); 510 iio_device_unregister(indio_dev);
516 511
517 if (!st->use_internal_vref) { 512 if (!st->use_internal_vref)
518 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg); 513 regulator_bulk_disable(ad5064_num_vref(st), st->vref_reg);
519 regulator_bulk_free(ad5064_num_vref(st), st->vref_reg);
520 }
521
522 iio_device_free(indio_dev);
523 514
524 return 0; 515 return 0;
525} 516}
diff --git a/drivers/iio/dac/ad5360.c b/drivers/iio/dac/ad5360.c
index 80923af424f2..d2da71ece740 100644
--- a/drivers/iio/dac/ad5360.c
+++ b/drivers/iio/dac/ad5360.c
@@ -459,7 +459,7 @@ static int ad5360_probe(struct spi_device *spi)
459 unsigned int i; 459 unsigned int i;
460 int ret; 460 int ret;
461 461
462 indio_dev = iio_device_alloc(sizeof(*st)); 462 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
463 if (indio_dev == NULL) { 463 if (indio_dev == NULL) {
464 dev_err(&spi->dev, "Failed to allocate iio device\n"); 464 dev_err(&spi->dev, "Failed to allocate iio device\n");
465 return -ENOMEM; 465 return -ENOMEM;
@@ -480,13 +480,13 @@ static int ad5360_probe(struct spi_device *spi)
480 ret = ad5360_alloc_channels(indio_dev); 480 ret = ad5360_alloc_channels(indio_dev);
481 if (ret) { 481 if (ret) {
482 dev_err(&spi->dev, "Failed to allocate channel spec: %d\n", ret); 482 dev_err(&spi->dev, "Failed to allocate channel spec: %d\n", ret);
483 goto error_free; 483 return ret;
484 } 484 }
485 485
486 for (i = 0; i < st->chip_info->num_vrefs; ++i) 486 for (i = 0; i < st->chip_info->num_vrefs; ++i)
487 st->vref_reg[i].supply = ad5360_vref_name[i]; 487 st->vref_reg[i].supply = ad5360_vref_name[i];
488 488
489 ret = regulator_bulk_get(&st->spi->dev, st->chip_info->num_vrefs, 489 ret = devm_regulator_bulk_get(&st->spi->dev, st->chip_info->num_vrefs,
490 st->vref_reg); 490 st->vref_reg);
491 if (ret) { 491 if (ret) {
492 dev_err(&spi->dev, "Failed to request vref regulators: %d\n", ret); 492 dev_err(&spi->dev, "Failed to request vref regulators: %d\n", ret);
@@ -496,7 +496,7 @@ static int ad5360_probe(struct spi_device *spi)
496 ret = regulator_bulk_enable(st->chip_info->num_vrefs, st->vref_reg); 496 ret = regulator_bulk_enable(st->chip_info->num_vrefs, st->vref_reg);
497 if (ret) { 497 if (ret) {
498 dev_err(&spi->dev, "Failed to enable vref regulators: %d\n", ret); 498 dev_err(&spi->dev, "Failed to enable vref regulators: %d\n", ret);
499 goto error_free_reg; 499 goto error_free_channels;
500 } 500 }
501 501
502 ret = iio_device_register(indio_dev); 502 ret = iio_device_register(indio_dev);
@@ -509,12 +509,8 @@ static int ad5360_probe(struct spi_device *spi)
509 509
510error_disable_reg: 510error_disable_reg:
511 regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg); 511 regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
512error_free_reg:
513 regulator_bulk_free(st->chip_info->num_vrefs, st->vref_reg);
514error_free_channels: 512error_free_channels:
515 kfree(indio_dev->channels); 513 kfree(indio_dev->channels);
516error_free:
517 iio_device_free(indio_dev);
518 514
519 return ret; 515 return ret;
520} 516}
@@ -529,9 +525,6 @@ static int ad5360_remove(struct spi_device *spi)
529 kfree(indio_dev->channels); 525 kfree(indio_dev->channels);
530 526
531 regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg); 527 regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
532 regulator_bulk_free(st->chip_info->num_vrefs, st->vref_reg);
533
534 iio_device_free(indio_dev);
535 528
536 return 0; 529 return 0;
537} 530}
diff --git a/drivers/iio/dac/ad5380.c b/drivers/iio/dac/ad5380.c
index bf2db02215c2..1c44ae3920e2 100644
--- a/drivers/iio/dac/ad5380.c
+++ b/drivers/iio/dac/ad5380.c
@@ -369,11 +369,10 @@ static int ad5380_probe(struct device *dev, struct regmap *regmap,
369 unsigned int ctrl = 0; 369 unsigned int ctrl = 0;
370 int ret; 370 int ret;
371 371
372 indio_dev = iio_device_alloc(sizeof(*st)); 372 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
373 if (indio_dev == NULL) { 373 if (indio_dev == NULL) {
374 dev_err(dev, "Failed to allocate iio device\n"); 374 dev_err(dev, "Failed to allocate iio device\n");
375 ret = -ENOMEM; 375 return -ENOMEM;
376 goto error_out;
377 } 376 }
378 377
379 st = iio_priv(indio_dev); 378 st = iio_priv(indio_dev);
@@ -391,13 +390,13 @@ static int ad5380_probe(struct device *dev, struct regmap *regmap,
391 ret = ad5380_alloc_channels(indio_dev); 390 ret = ad5380_alloc_channels(indio_dev);
392 if (ret) { 391 if (ret) {
393 dev_err(dev, "Failed to allocate channel spec: %d\n", ret); 392 dev_err(dev, "Failed to allocate channel spec: %d\n", ret);
394 goto error_free; 393 return ret;
395 } 394 }
396 395
397 if (st->chip_info->int_vref == 2500000) 396 if (st->chip_info->int_vref == 2500000)
398 ctrl |= AD5380_CTRL_INT_VREF_2V5; 397 ctrl |= AD5380_CTRL_INT_VREF_2V5;
399 398
400 st->vref_reg = regulator_get(dev, "vref"); 399 st->vref_reg = devm_regulator_get(dev, "vref");
401 if (!IS_ERR(st->vref_reg)) { 400 if (!IS_ERR(st->vref_reg)) {
402 ret = regulator_enable(st->vref_reg); 401 ret = regulator_enable(st->vref_reg);
403 if (ret) { 402 if (ret) {
@@ -434,13 +433,7 @@ error_disable_reg:
434 if (!IS_ERR(st->vref_reg)) 433 if (!IS_ERR(st->vref_reg))
435 regulator_disable(st->vref_reg); 434 regulator_disable(st->vref_reg);
436error_free_reg: 435error_free_reg:
437 if (!IS_ERR(st->vref_reg))
438 regulator_put(st->vref_reg);
439
440 kfree(indio_dev->channels); 436 kfree(indio_dev->channels);
441error_free:
442 iio_device_free(indio_dev);
443error_out:
444 437
445 return ret; 438 return ret;
446} 439}
@@ -456,11 +449,8 @@ static int ad5380_remove(struct device *dev)
456 449
457 if (!IS_ERR(st->vref_reg)) { 450 if (!IS_ERR(st->vref_reg)) {
458 regulator_disable(st->vref_reg); 451 regulator_disable(st->vref_reg);
459 regulator_put(st->vref_reg);
460 } 452 }
461 453
462 iio_device_free(indio_dev);
463
464 return 0; 454 return 0;
465} 455}
466 456
diff --git a/drivers/iio/dac/ad5421.c b/drivers/iio/dac/ad5421.c
index 98f24407c3ce..1f78b14abb7d 100644
--- a/drivers/iio/dac/ad5421.c
+++ b/drivers/iio/dac/ad5421.c
@@ -451,7 +451,7 @@ static int ad5421_probe(struct spi_device *spi)
451 struct ad5421_state *st; 451 struct ad5421_state *st;
452 int ret; 452 int ret;
453 453
454 indio_dev = iio_device_alloc(sizeof(*st)); 454 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
455 if (indio_dev == NULL) { 455 if (indio_dev == NULL) {
456 dev_err(&spi->dev, "Failed to allocate iio device\n"); 456 dev_err(&spi->dev, "Failed to allocate iio device\n");
457 return -ENOMEM; 457 return -ENOMEM;
@@ -484,31 +484,23 @@ static int ad5421_probe(struct spi_device *spi)
484 ad5421_update_ctrl(indio_dev, 0, 0); 484 ad5421_update_ctrl(indio_dev, 0, 0);
485 485
486 if (spi->irq) { 486 if (spi->irq) {
487 ret = request_threaded_irq(spi->irq, 487 ret = devm_request_threaded_irq(&spi->dev, spi->irq,
488 NULL, 488 NULL,
489 ad5421_fault_handler, 489 ad5421_fault_handler,
490 IRQF_TRIGGER_HIGH | IRQF_ONESHOT, 490 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
491 "ad5421 fault", 491 "ad5421 fault",
492 indio_dev); 492 indio_dev);
493 if (ret) 493 if (ret)
494 goto error_free; 494 return ret;
495 } 495 }
496 496
497 ret = iio_device_register(indio_dev); 497 ret = iio_device_register(indio_dev);
498 if (ret) { 498 if (ret) {
499 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret); 499 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
500 goto error_free_irq; 500 return ret;
501 } 501 }
502 502
503 return 0; 503 return 0;
504
505error_free_irq:
506 if (spi->irq)
507 free_irq(spi->irq, indio_dev);
508error_free:
509 iio_device_free(indio_dev);
510
511 return ret;
512} 504}
513 505
514static int ad5421_remove(struct spi_device *spi) 506static int ad5421_remove(struct spi_device *spi)
@@ -516,9 +508,6 @@ static int ad5421_remove(struct spi_device *spi)
516 struct iio_dev *indio_dev = spi_get_drvdata(spi); 508 struct iio_dev *indio_dev = spi_get_drvdata(spi);
517 509
518 iio_device_unregister(indio_dev); 510 iio_device_unregister(indio_dev);
519 if (spi->irq)
520 free_irq(spi->irq, indio_dev);
521 iio_device_free(indio_dev);
522 511
523 return 0; 512 return 0;
524} 513}
diff --git a/drivers/iio/dac/ad5446.c b/drivers/iio/dac/ad5446.c
index cae8f6056ac3..96e9ed4c2d01 100644
--- a/drivers/iio/dac/ad5446.c
+++ b/drivers/iio/dac/ad5446.c
@@ -220,11 +220,11 @@ static int ad5446_probe(struct device *dev, const char *name,
220 struct regulator *reg; 220 struct regulator *reg;
221 int ret, voltage_uv = 0; 221 int ret, voltage_uv = 0;
222 222
223 reg = regulator_get(dev, "vcc"); 223 reg = devm_regulator_get(dev, "vcc");
224 if (!IS_ERR(reg)) { 224 if (!IS_ERR(reg)) {
225 ret = regulator_enable(reg); 225 ret = regulator_enable(reg);
226 if (ret) 226 if (ret)
227 goto error_put_reg; 227 return ret;
228 228
229 ret = regulator_get_voltage(reg); 229 ret = regulator_get_voltage(reg);
230 if (ret < 0) 230 if (ret < 0)
@@ -233,7 +233,7 @@ static int ad5446_probe(struct device *dev, const char *name,
233 voltage_uv = ret; 233 voltage_uv = ret;
234 } 234 }
235 235
236 indio_dev = iio_device_alloc(sizeof(*st)); 236 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
237 if (indio_dev == NULL) { 237 if (indio_dev == NULL) {
238 ret = -ENOMEM; 238 ret = -ENOMEM;
239 goto error_disable_reg; 239 goto error_disable_reg;
@@ -264,19 +264,13 @@ static int ad5446_probe(struct device *dev, const char *name,
264 264
265 ret = iio_device_register(indio_dev); 265 ret = iio_device_register(indio_dev);
266 if (ret) 266 if (ret)
267 goto error_free_device; 267 goto error_disable_reg;
268 268
269 return 0; 269 return 0;
270 270
271error_free_device:
272 iio_device_free(indio_dev);
273error_disable_reg: 271error_disable_reg:
274 if (!IS_ERR(reg)) 272 if (!IS_ERR(reg))
275 regulator_disable(reg); 273 regulator_disable(reg);
276error_put_reg:
277 if (!IS_ERR(reg))
278 regulator_put(reg);
279
280 return ret; 274 return ret;
281} 275}
282 276
@@ -286,11 +280,8 @@ static int ad5446_remove(struct device *dev)
286 struct ad5446_state *st = iio_priv(indio_dev); 280 struct ad5446_state *st = iio_priv(indio_dev);
287 281
288 iio_device_unregister(indio_dev); 282 iio_device_unregister(indio_dev);
289 if (!IS_ERR(st->reg)) { 283 if (!IS_ERR(st->reg))
290 regulator_disable(st->reg); 284 regulator_disable(st->reg);
291 regulator_put(st->reg);
292 }
293 iio_device_free(indio_dev);
294 285
295 return 0; 286 return 0;
296} 287}
diff --git a/drivers/iio/dac/ad5449.c b/drivers/iio/dac/ad5449.c
index ba1c914b0399..fff7d0762c0c 100644
--- a/drivers/iio/dac/ad5449.c
+++ b/drivers/iio/dac/ad5449.c
@@ -275,7 +275,7 @@ static int ad5449_spi_probe(struct spi_device *spi)
275 unsigned int i; 275 unsigned int i;
276 int ret; 276 int ret;
277 277
278 indio_dev = iio_device_alloc(sizeof(*st)); 278 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
279 if (indio_dev == NULL) 279 if (indio_dev == NULL)
280 return -ENOMEM; 280 return -ENOMEM;
281 281
@@ -288,14 +288,14 @@ static int ad5449_spi_probe(struct spi_device *spi)
288 for (i = 0; i < st->chip_info->num_channels; ++i) 288 for (i = 0; i < st->chip_info->num_channels; ++i)
289 st->vref_reg[i].supply = ad5449_vref_name(st, i); 289 st->vref_reg[i].supply = ad5449_vref_name(st, i);
290 290
291 ret = regulator_bulk_get(&spi->dev, st->chip_info->num_channels, 291 ret = devm_regulator_bulk_get(&spi->dev, st->chip_info->num_channels,
292 st->vref_reg); 292 st->vref_reg);
293 if (ret) 293 if (ret)
294 goto error_free; 294 return ret;
295 295
296 ret = regulator_bulk_enable(st->chip_info->num_channels, st->vref_reg); 296 ret = regulator_bulk_enable(st->chip_info->num_channels, st->vref_reg);
297 if (ret) 297 if (ret)
298 goto error_free_reg; 298 return ret;
299 299
300 indio_dev->dev.parent = &spi->dev; 300 indio_dev->dev.parent = &spi->dev;
301 indio_dev->name = id->name; 301 indio_dev->name = id->name;
@@ -325,10 +325,6 @@ static int ad5449_spi_probe(struct spi_device *spi)
325 325
326error_disable_reg: 326error_disable_reg:
327 regulator_bulk_disable(st->chip_info->num_channels, st->vref_reg); 327 regulator_bulk_disable(st->chip_info->num_channels, st->vref_reg);
328error_free_reg:
329 regulator_bulk_free(st->chip_info->num_channels, st->vref_reg);
330error_free:
331 iio_device_free(indio_dev);
332 328
333 return ret; 329 return ret;
334} 330}
@@ -341,9 +337,6 @@ static int ad5449_spi_remove(struct spi_device *spi)
341 iio_device_unregister(indio_dev); 337 iio_device_unregister(indio_dev);
342 338
343 regulator_bulk_disable(st->chip_info->num_channels, st->vref_reg); 339 regulator_bulk_disable(st->chip_info->num_channels, st->vref_reg);
344 regulator_bulk_free(st->chip_info->num_channels, st->vref_reg);
345
346 iio_device_free(indio_dev);
347 340
348 return 0; 341 return 0;
349} 342}
diff --git a/drivers/iio/dac/ad5504.c b/drivers/iio/dac/ad5504.c
index 139206e84cb7..caffb16bc05c 100644
--- a/drivers/iio/dac/ad5504.c
+++ b/drivers/iio/dac/ad5504.c
@@ -281,16 +281,14 @@ static int ad5504_probe(struct spi_device *spi)
281 struct regulator *reg; 281 struct regulator *reg;
282 int ret, voltage_uv = 0; 282 int ret, voltage_uv = 0;
283 283
284 indio_dev = iio_device_alloc(sizeof(*st)); 284 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
285 if (indio_dev == NULL) { 285 if (!indio_dev)
286 ret = -ENOMEM; 286 return -ENOMEM;
287 goto error_ret; 287 reg = devm_regulator_get(&spi->dev, "vcc");
288 }
289 reg = regulator_get(&spi->dev, "vcc");
290 if (!IS_ERR(reg)) { 288 if (!IS_ERR(reg)) {
291 ret = regulator_enable(reg); 289 ret = regulator_enable(reg);
292 if (ret) 290 if (ret)
293 goto error_put_reg; 291 return ret;
294 292
295 ret = regulator_get_voltage(reg); 293 ret = regulator_get_voltage(reg);
296 if (ret < 0) 294 if (ret < 0)
@@ -321,7 +319,7 @@ static int ad5504_probe(struct spi_device *spi)
321 indio_dev->modes = INDIO_DIRECT_MODE; 319 indio_dev->modes = INDIO_DIRECT_MODE;
322 320
323 if (spi->irq) { 321 if (spi->irq) {
324 ret = request_threaded_irq(spi->irq, 322 ret = devm_request_threaded_irq(&spi->dev, spi->irq,
325 NULL, 323 NULL,
326 &ad5504_event_handler, 324 &ad5504_event_handler,
327 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 325 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
@@ -333,22 +331,14 @@ static int ad5504_probe(struct spi_device *spi)
333 331
334 ret = iio_device_register(indio_dev); 332 ret = iio_device_register(indio_dev);
335 if (ret) 333 if (ret)
336 goto error_free_irq; 334 goto error_disable_reg;
337 335
338 return 0; 336 return 0;
339 337
340error_free_irq:
341 if (spi->irq)
342 free_irq(spi->irq, indio_dev);
343error_disable_reg: 338error_disable_reg:
344 if (!IS_ERR(reg)) 339 if (!IS_ERR(reg))
345 regulator_disable(reg); 340 regulator_disable(reg);
346error_put_reg:
347 if (!IS_ERR(reg))
348 regulator_put(reg);
349 341
350 iio_device_free(indio_dev);
351error_ret:
352 return ret; 342 return ret;
353} 343}
354 344
@@ -358,14 +348,9 @@ static int ad5504_remove(struct spi_device *spi)
358 struct ad5504_state *st = iio_priv(indio_dev); 348 struct ad5504_state *st = iio_priv(indio_dev);
359 349
360 iio_device_unregister(indio_dev); 350 iio_device_unregister(indio_dev);
361 if (spi->irq)
362 free_irq(spi->irq, indio_dev);
363 351
364 if (!IS_ERR(st->reg)) { 352 if (!IS_ERR(st->reg))
365 regulator_disable(st->reg); 353 regulator_disable(st->reg);
366 regulator_put(st->reg);
367 }
368 iio_device_free(indio_dev);
369 354
370 return 0; 355 return 0;
371} 356}
diff --git a/drivers/iio/dac/ad5624r_spi.c b/drivers/iio/dac/ad5624r_spi.c
index bb298aaff321..714af757cd56 100644
--- a/drivers/iio/dac/ad5624r_spi.c
+++ b/drivers/iio/dac/ad5624r_spi.c
@@ -226,17 +226,15 @@ static int ad5624r_probe(struct spi_device *spi)
226 struct iio_dev *indio_dev; 226 struct iio_dev *indio_dev;
227 int ret, voltage_uv = 0; 227 int ret, voltage_uv = 0;
228 228
229 indio_dev = iio_device_alloc(sizeof(*st)); 229 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
230 if (indio_dev == NULL) { 230 if (!indio_dev)
231 ret = -ENOMEM; 231 return -ENOMEM;
232 goto error_ret;
233 }
234 st = iio_priv(indio_dev); 232 st = iio_priv(indio_dev);
235 st->reg = regulator_get(&spi->dev, "vcc"); 233 st->reg = devm_regulator_get(&spi->dev, "vcc");
236 if (!IS_ERR(st->reg)) { 234 if (!IS_ERR(st->reg)) {
237 ret = regulator_enable(st->reg); 235 ret = regulator_enable(st->reg);
238 if (ret) 236 if (ret)
239 goto error_put_reg; 237 return ret;
240 238
241 ret = regulator_get_voltage(st->reg); 239 ret = regulator_get_voltage(st->reg);
242 if (ret < 0) 240 if (ret < 0)
@@ -277,11 +275,6 @@ static int ad5624r_probe(struct spi_device *spi)
277error_disable_reg: 275error_disable_reg:
278 if (!IS_ERR(st->reg)) 276 if (!IS_ERR(st->reg))
279 regulator_disable(st->reg); 277 regulator_disable(st->reg);
280error_put_reg:
281 if (!IS_ERR(st->reg))
282 regulator_put(st->reg);
283 iio_device_free(indio_dev);
284error_ret:
285 278
286 return ret; 279 return ret;
287} 280}
@@ -292,11 +285,8 @@ static int ad5624r_remove(struct spi_device *spi)
292 struct ad5624r_state *st = iio_priv(indio_dev); 285 struct ad5624r_state *st = iio_priv(indio_dev);
293 286
294 iio_device_unregister(indio_dev); 287 iio_device_unregister(indio_dev);
295 if (!IS_ERR(st->reg)) { 288 if (!IS_ERR(st->reg))
296 regulator_disable(st->reg); 289 regulator_disable(st->reg);
297 regulator_put(st->reg);
298 }
299 iio_device_free(indio_dev);
300 290
301 return 0; 291 return 0;
302} 292}
diff --git a/drivers/iio/dac/ad5686.c b/drivers/iio/dac/ad5686.c
index 06439b1af9b6..57825ead7db2 100644
--- a/drivers/iio/dac/ad5686.c
+++ b/drivers/iio/dac/ad5686.c
@@ -314,18 +314,18 @@ static int ad5686_probe(struct spi_device *spi)
314 struct iio_dev *indio_dev; 314 struct iio_dev *indio_dev;
315 int ret, regdone = 0, voltage_uv = 0; 315 int ret, regdone = 0, voltage_uv = 0;
316 316
317 indio_dev = iio_device_alloc(sizeof(*st)); 317 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
318 if (indio_dev == NULL) 318 if (indio_dev == NULL)
319 return -ENOMEM; 319 return -ENOMEM;
320 320
321 st = iio_priv(indio_dev); 321 st = iio_priv(indio_dev);
322 spi_set_drvdata(spi, indio_dev); 322 spi_set_drvdata(spi, indio_dev);
323 323
324 st->reg = regulator_get(&spi->dev, "vcc"); 324 st->reg = devm_regulator_get(&spi->dev, "vcc");
325 if (!IS_ERR(st->reg)) { 325 if (!IS_ERR(st->reg)) {
326 ret = regulator_enable(st->reg); 326 ret = regulator_enable(st->reg);
327 if (ret) 327 if (ret)
328 goto error_put_reg; 328 return ret;
329 329
330 ret = regulator_get_voltage(st->reg); 330 ret = regulator_get_voltage(st->reg);
331 if (ret < 0) 331 if (ret < 0)
@@ -369,12 +369,6 @@ static int ad5686_probe(struct spi_device *spi)
369error_disable_reg: 369error_disable_reg:
370 if (!IS_ERR(st->reg)) 370 if (!IS_ERR(st->reg))
371 regulator_disable(st->reg); 371 regulator_disable(st->reg);
372error_put_reg:
373 if (!IS_ERR(st->reg))
374 regulator_put(st->reg);
375
376 iio_device_free(indio_dev);
377
378 return ret; 372 return ret;
379} 373}
380 374
@@ -384,11 +378,8 @@ static int ad5686_remove(struct spi_device *spi)
384 struct ad5686_state *st = iio_priv(indio_dev); 378 struct ad5686_state *st = iio_priv(indio_dev);
385 379
386 iio_device_unregister(indio_dev); 380 iio_device_unregister(indio_dev);
387 if (!IS_ERR(st->reg)) { 381 if (!IS_ERR(st->reg))
388 regulator_disable(st->reg); 382 regulator_disable(st->reg);
389 regulator_put(st->reg);
390 }
391 iio_device_free(indio_dev);
392 383
393 return 0; 384 return 0;
394} 385}
diff --git a/drivers/iio/dac/ad5755.c b/drivers/iio/dac/ad5755.c
index 12bb315e55f8..36a4361aece1 100644
--- a/drivers/iio/dac/ad5755.c
+++ b/drivers/iio/dac/ad5755.c
@@ -565,7 +565,7 @@ static int ad5755_probe(struct spi_device *spi)
565 struct ad5755_state *st; 565 struct ad5755_state *st;
566 int ret; 566 int ret;
567 567
568 indio_dev = iio_device_alloc(sizeof(*st)); 568 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
569 if (indio_dev == NULL) { 569 if (indio_dev == NULL) {
570 dev_err(&spi->dev, "Failed to allocate iio device\n"); 570 dev_err(&spi->dev, "Failed to allocate iio device\n");
571 return -ENOMEM; 571 return -ENOMEM;
@@ -589,24 +589,19 @@ static int ad5755_probe(struct spi_device *spi)
589 589
590 ret = ad5755_init_channels(indio_dev, pdata); 590 ret = ad5755_init_channels(indio_dev, pdata);
591 if (ret) 591 if (ret)
592 goto error_free; 592 return ret;
593 593
594 ret = ad5755_setup_pdata(indio_dev, pdata); 594 ret = ad5755_setup_pdata(indio_dev, pdata);
595 if (ret) 595 if (ret)
596 goto error_free; 596 return ret;
597 597
598 ret = iio_device_register(indio_dev); 598 ret = iio_device_register(indio_dev);
599 if (ret) { 599 if (ret) {
600 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret); 600 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
601 goto error_free; 601 return ret;
602 } 602 }
603 603
604 return 0; 604 return 0;
605
606error_free:
607 iio_device_free(indio_dev);
608
609 return ret;
610} 605}
611 606
612static int ad5755_remove(struct spi_device *spi) 607static int ad5755_remove(struct spi_device *spi)
@@ -614,7 +609,6 @@ static int ad5755_remove(struct spi_device *spi)
614 struct iio_dev *indio_dev = spi_get_drvdata(spi); 609 struct iio_dev *indio_dev = spi_get_drvdata(spi);
615 610
616 iio_device_unregister(indio_dev); 611 iio_device_unregister(indio_dev);
617 iio_device_free(indio_dev);
618 612
619 return 0; 613 return 0;
620} 614}
diff --git a/drivers/iio/dac/ad5764.c b/drivers/iio/dac/ad5764.c
index 7a53f7d70dac..df7e028d9db5 100644
--- a/drivers/iio/dac/ad5764.c
+++ b/drivers/iio/dac/ad5764.c
@@ -275,7 +275,7 @@ static int ad5764_probe(struct spi_device *spi)
275 struct ad5764_state *st; 275 struct ad5764_state *st;
276 int ret; 276 int ret;
277 277
278 indio_dev = iio_device_alloc(sizeof(*st)); 278 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
279 if (indio_dev == NULL) { 279 if (indio_dev == NULL) {
280 dev_err(&spi->dev, "Failed to allocate iio device\n"); 280 dev_err(&spi->dev, "Failed to allocate iio device\n");
281 return -ENOMEM; 281 return -ENOMEM;
@@ -298,12 +298,12 @@ static int ad5764_probe(struct spi_device *spi)
298 st->vref_reg[0].supply = "vrefAB"; 298 st->vref_reg[0].supply = "vrefAB";
299 st->vref_reg[1].supply = "vrefCD"; 299 st->vref_reg[1].supply = "vrefCD";
300 300
301 ret = regulator_bulk_get(&st->spi->dev, 301 ret = devm_regulator_bulk_get(&st->spi->dev,
302 ARRAY_SIZE(st->vref_reg), st->vref_reg); 302 ARRAY_SIZE(st->vref_reg), st->vref_reg);
303 if (ret) { 303 if (ret) {
304 dev_err(&spi->dev, "Failed to request vref regulators: %d\n", 304 dev_err(&spi->dev, "Failed to request vref regulators: %d\n",
305 ret); 305 ret);
306 goto error_free; 306 return ret;
307 } 307 }
308 308
309 ret = regulator_bulk_enable(ARRAY_SIZE(st->vref_reg), 309 ret = regulator_bulk_enable(ARRAY_SIZE(st->vref_reg),
@@ -311,7 +311,7 @@ static int ad5764_probe(struct spi_device *spi)
311 if (ret) { 311 if (ret) {
312 dev_err(&spi->dev, "Failed to enable vref regulators: %d\n", 312 dev_err(&spi->dev, "Failed to enable vref regulators: %d\n",
313 ret); 313 ret);
314 goto error_free_reg; 314 return ret;
315 } 315 }
316 } 316 }
317 317
@@ -326,12 +326,6 @@ static int ad5764_probe(struct spi_device *spi)
326error_disable_reg: 326error_disable_reg:
327 if (st->chip_info->int_vref == 0) 327 if (st->chip_info->int_vref == 0)
328 regulator_bulk_disable(ARRAY_SIZE(st->vref_reg), st->vref_reg); 328 regulator_bulk_disable(ARRAY_SIZE(st->vref_reg), st->vref_reg);
329error_free_reg:
330 if (st->chip_info->int_vref == 0)
331 regulator_bulk_free(ARRAY_SIZE(st->vref_reg), st->vref_reg);
332error_free:
333 iio_device_free(indio_dev);
334
335 return ret; 329 return ret;
336} 330}
337 331
@@ -342,12 +336,8 @@ static int ad5764_remove(struct spi_device *spi)
342 336
343 iio_device_unregister(indio_dev); 337 iio_device_unregister(indio_dev);
344 338
345 if (st->chip_info->int_vref == 0) { 339 if (st->chip_info->int_vref == 0)
346 regulator_bulk_disable(ARRAY_SIZE(st->vref_reg), st->vref_reg); 340 regulator_bulk_disable(ARRAY_SIZE(st->vref_reg), st->vref_reg);
347 regulator_bulk_free(ARRAY_SIZE(st->vref_reg), st->vref_reg);
348 }
349
350 iio_device_free(indio_dev);
351 341
352 return 0; 342 return 0;
353} 343}
diff --git a/drivers/iio/dac/ad5791.c b/drivers/iio/dac/ad5791.c
index 97c1e5d780df..ce7458963309 100644
--- a/drivers/iio/dac/ad5791.c
+++ b/drivers/iio/dac/ad5791.c
@@ -349,17 +349,15 @@ static int ad5791_probe(struct spi_device *spi)
349 struct ad5791_state *st; 349 struct ad5791_state *st;
350 int ret, pos_voltage_uv = 0, neg_voltage_uv = 0; 350 int ret, pos_voltage_uv = 0, neg_voltage_uv = 0;
351 351
352 indio_dev = iio_device_alloc(sizeof(*st)); 352 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
353 if (indio_dev == NULL) { 353 if (!indio_dev)
354 ret = -ENOMEM; 354 return -ENOMEM;
355 goto error_ret;
356 }
357 st = iio_priv(indio_dev); 355 st = iio_priv(indio_dev);
358 st->reg_vdd = regulator_get(&spi->dev, "vdd"); 356 st->reg_vdd = devm_regulator_get(&spi->dev, "vdd");
359 if (!IS_ERR(st->reg_vdd)) { 357 if (!IS_ERR(st->reg_vdd)) {
360 ret = regulator_enable(st->reg_vdd); 358 ret = regulator_enable(st->reg_vdd);
361 if (ret) 359 if (ret)
362 goto error_put_reg_pos; 360 return ret;
363 361
364 ret = regulator_get_voltage(st->reg_vdd); 362 ret = regulator_get_voltage(st->reg_vdd);
365 if (ret < 0) 363 if (ret < 0)
@@ -368,11 +366,11 @@ static int ad5791_probe(struct spi_device *spi)
368 pos_voltage_uv = ret; 366 pos_voltage_uv = ret;
369 } 367 }
370 368
371 st->reg_vss = regulator_get(&spi->dev, "vss"); 369 st->reg_vss = devm_regulator_get(&spi->dev, "vss");
372 if (!IS_ERR(st->reg_vss)) { 370 if (!IS_ERR(st->reg_vss)) {
373 ret = regulator_enable(st->reg_vss); 371 ret = regulator_enable(st->reg_vss);
374 if (ret) 372 if (ret)
375 goto error_put_reg_neg; 373 goto error_disable_reg_pos;
376 374
377 ret = regulator_get_voltage(st->reg_vss); 375 ret = regulator_get_voltage(st->reg_vss);
378 if (ret < 0) 376 if (ret < 0)
@@ -428,19 +426,9 @@ static int ad5791_probe(struct spi_device *spi)
428error_disable_reg_neg: 426error_disable_reg_neg:
429 if (!IS_ERR(st->reg_vss)) 427 if (!IS_ERR(st->reg_vss))
430 regulator_disable(st->reg_vss); 428 regulator_disable(st->reg_vss);
431error_put_reg_neg:
432 if (!IS_ERR(st->reg_vss))
433 regulator_put(st->reg_vss);
434
435error_disable_reg_pos: 429error_disable_reg_pos:
436 if (!IS_ERR(st->reg_vdd)) 430 if (!IS_ERR(st->reg_vdd))
437 regulator_disable(st->reg_vdd); 431 regulator_disable(st->reg_vdd);
438error_put_reg_pos:
439 if (!IS_ERR(st->reg_vdd))
440 regulator_put(st->reg_vdd);
441 iio_device_free(indio_dev);
442error_ret:
443
444 return ret; 432 return ret;
445} 433}
446 434
@@ -450,16 +438,11 @@ static int ad5791_remove(struct spi_device *spi)
450 struct ad5791_state *st = iio_priv(indio_dev); 438 struct ad5791_state *st = iio_priv(indio_dev);
451 439
452 iio_device_unregister(indio_dev); 440 iio_device_unregister(indio_dev);
453 if (!IS_ERR(st->reg_vdd)) { 441 if (!IS_ERR(st->reg_vdd))
454 regulator_disable(st->reg_vdd); 442 regulator_disable(st->reg_vdd);
455 regulator_put(st->reg_vdd);
456 }
457 443
458 if (!IS_ERR(st->reg_vss)) { 444 if (!IS_ERR(st->reg_vss))
459 regulator_disable(st->reg_vss); 445 regulator_disable(st->reg_vss);
460 regulator_put(st->reg_vss);
461 }
462 iio_device_free(indio_dev);
463 446
464 return 0; 447 return 0;
465} 448}
diff --git a/drivers/iio/dac/ad7303.c b/drivers/iio/dac/ad7303.c
index d546f50f9258..ed2d276477bd 100644
--- a/drivers/iio/dac/ad7303.c
+++ b/drivers/iio/dac/ad7303.c
@@ -203,7 +203,7 @@ static int ad7303_probe(struct spi_device *spi)
203 bool ext_ref; 203 bool ext_ref;
204 int ret; 204 int ret;
205 205
206 indio_dev = iio_device_alloc(sizeof(*st)); 206 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
207 if (indio_dev == NULL) 207 if (indio_dev == NULL)
208 return -ENOMEM; 208 return -ENOMEM;
209 209
@@ -212,15 +212,13 @@ static int ad7303_probe(struct spi_device *spi)
212 212
213 st->spi = spi; 213 st->spi = spi;
214 214
215 st->vdd_reg = regulator_get(&spi->dev, "Vdd"); 215 st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
216 if (IS_ERR(st->vdd_reg)) { 216 if (IS_ERR(st->vdd_reg))
217 ret = PTR_ERR(st->vdd_reg); 217 return PTR_ERR(st->vdd_reg);
218 goto err_free;
219 }
220 218
221 ret = regulator_enable(st->vdd_reg); 219 ret = regulator_enable(st->vdd_reg);
222 if (ret) 220 if (ret)
223 goto err_put_vdd_reg; 221 return ret;
224 222
225 if (spi->dev.of_node) { 223 if (spi->dev.of_node) {
226 ext_ref = of_property_read_bool(spi->dev.of_node, 224 ext_ref = of_property_read_bool(spi->dev.of_node,
@@ -234,7 +232,7 @@ static int ad7303_probe(struct spi_device *spi)
234 } 232 }
235 233
236 if (ext_ref) { 234 if (ext_ref) {
237 st->vref_reg = regulator_get(&spi->dev, "REF"); 235 st->vref_reg = devm_regulator_get(&spi->dev, "REF");
238 if (IS_ERR(st->vref_reg)) { 236 if (IS_ERR(st->vref_reg)) {
239 ret = PTR_ERR(st->vref_reg); 237 ret = PTR_ERR(st->vref_reg);
240 goto err_disable_vdd_reg; 238 goto err_disable_vdd_reg;
@@ -242,7 +240,7 @@ static int ad7303_probe(struct spi_device *spi)
242 240
243 ret = regulator_enable(st->vref_reg); 241 ret = regulator_enable(st->vref_reg);
244 if (ret) 242 if (ret)
245 goto err_put_vref_reg; 243 goto err_disable_vdd_reg;
246 244
247 st->config |= AD7303_CFG_EXTERNAL_VREF; 245 st->config |= AD7303_CFG_EXTERNAL_VREF;
248 } 246 }
@@ -263,16 +261,8 @@ static int ad7303_probe(struct spi_device *spi)
263err_disable_vref_reg: 261err_disable_vref_reg:
264 if (st->vref_reg) 262 if (st->vref_reg)
265 regulator_disable(st->vref_reg); 263 regulator_disable(st->vref_reg);
266err_put_vref_reg:
267 if (st->vref_reg)
268 regulator_put(st->vref_reg);
269err_disable_vdd_reg: 264err_disable_vdd_reg:
270 regulator_disable(st->vdd_reg); 265 regulator_disable(st->vdd_reg);
271err_put_vdd_reg:
272 regulator_put(st->vdd_reg);
273err_free:
274 iio_device_free(indio_dev);
275
276 return ret; 266 return ret;
277} 267}
278 268
@@ -283,14 +273,9 @@ static int ad7303_remove(struct spi_device *spi)
283 273
284 iio_device_unregister(indio_dev); 274 iio_device_unregister(indio_dev);
285 275
286 if (st->vref_reg) { 276 if (st->vref_reg)
287 regulator_disable(st->vref_reg); 277 regulator_disable(st->vref_reg);
288 regulator_put(st->vref_reg);
289 }
290 regulator_disable(st->vdd_reg); 278 regulator_disable(st->vdd_reg);
291 regulator_put(st->vdd_reg);
292
293 iio_device_free(indio_dev);
294 279
295 return 0; 280 return 0;
296} 281}
diff --git a/drivers/iio/dac/max517.c b/drivers/iio/dac/max517.c
index ebfaa4156246..83adcbf1a205 100644
--- a/drivers/iio/dac/max517.c
+++ b/drivers/iio/dac/max517.c
@@ -164,11 +164,9 @@ static int max517_probe(struct i2c_client *client,
164 struct max517_platform_data *platform_data = client->dev.platform_data; 164 struct max517_platform_data *platform_data = client->dev.platform_data;
165 int err; 165 int err;
166 166
167 indio_dev = iio_device_alloc(sizeof(*data)); 167 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
168 if (indio_dev == NULL) { 168 if (!indio_dev)
169 err = -ENOMEM; 169 return -ENOMEM;
170 goto exit;
171 }
172 data = iio_priv(indio_dev); 170 data = iio_priv(indio_dev);
173 i2c_set_clientdata(client, indio_dev); 171 i2c_set_clientdata(client, indio_dev);
174 data->client = client; 172 data->client = client;
@@ -198,23 +196,16 @@ static int max517_probe(struct i2c_client *client,
198 196
199 err = iio_device_register(indio_dev); 197 err = iio_device_register(indio_dev);
200 if (err) 198 if (err)
201 goto exit_free_device; 199 return err;
202 200
203 dev_info(&client->dev, "DAC registered\n"); 201 dev_info(&client->dev, "DAC registered\n");
204 202
205 return 0; 203 return 0;
206
207exit_free_device:
208 iio_device_free(indio_dev);
209exit:
210 return err;
211} 204}
212 205
213static int max517_remove(struct i2c_client *client) 206static int max517_remove(struct i2c_client *client)
214{ 207{
215 iio_device_unregister(i2c_get_clientdata(client)); 208 iio_device_unregister(i2c_get_clientdata(client));
216 iio_device_free(i2c_get_clientdata(client));
217
218 return 0; 209 return 0;
219} 210}
220 211
diff --git a/drivers/iio/dac/mcp4725.c b/drivers/iio/dac/mcp4725.c
index a612ec766d96..1f4a48e6a82c 100644
--- a/drivers/iio/dac/mcp4725.c
+++ b/drivers/iio/dac/mcp4725.c
@@ -12,14 +12,13 @@
12 * driver for the Microchip I2C 12-bit digital-to-analog converter (DAC) 12 * driver for the Microchip I2C 12-bit digital-to-analog converter (DAC)
13 * (7-bit I2C slave address 0x60, the three LSBs can be configured in 13 * (7-bit I2C slave address 0x60, the three LSBs can be configured in
14 * hardware) 14 * hardware)
15 *
16 * writing the DAC value to EEPROM is not supported
17 */ 15 */
18 16
19#include <linux/module.h> 17#include <linux/module.h>
20#include <linux/init.h> 18#include <linux/init.h>
21#include <linux/i2c.h> 19#include <linux/i2c.h>
22#include <linux/err.h> 20#include <linux/err.h>
21#include <linux/delay.h>
23 22
24#include <linux/iio/iio.h> 23#include <linux/iio/iio.h>
25#include <linux/iio/sysfs.h> 24#include <linux/iio/sysfs.h>
@@ -32,15 +31,19 @@ struct mcp4725_data {
32 struct i2c_client *client; 31 struct i2c_client *client;
33 u16 vref_mv; 32 u16 vref_mv;
34 u16 dac_value; 33 u16 dac_value;
34 bool powerdown;
35 unsigned powerdown_mode;
35}; 36};
36 37
37#ifdef CONFIG_PM_SLEEP
38static int mcp4725_suspend(struct device *dev) 38static int mcp4725_suspend(struct device *dev)
39{ 39{
40 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
41 struct mcp4725_data *data = iio_priv(indio_dev);
40 u8 outbuf[2]; 42 u8 outbuf[2];
41 43
42 outbuf[0] = 0x3 << 4; /* power-down bits, 500 kOhm resistor */ 44 outbuf[0] = (data->powerdown_mode + 1) << 4;
43 outbuf[1] = 0; 45 outbuf[1] = 0;
46 data->powerdown = true;
44 47
45 return i2c_master_send(to_i2c_client(dev), outbuf, 2); 48 return i2c_master_send(to_i2c_client(dev), outbuf, 2);
46} 49}
@@ -54,16 +57,150 @@ static int mcp4725_resume(struct device *dev)
54 /* restore previous DAC value */ 57 /* restore previous DAC value */
55 outbuf[0] = (data->dac_value >> 8) & 0xf; 58 outbuf[0] = (data->dac_value >> 8) & 0xf;
56 outbuf[1] = data->dac_value & 0xff; 59 outbuf[1] = data->dac_value & 0xff;
60 data->powerdown = false;
57 61
58 return i2c_master_send(to_i2c_client(dev), outbuf, 2); 62 return i2c_master_send(to_i2c_client(dev), outbuf, 2);
59} 63}
60 64
65#ifdef CONFIG_PM_SLEEP
61static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume); 66static SIMPLE_DEV_PM_OPS(mcp4725_pm_ops, mcp4725_suspend, mcp4725_resume);
62#define MCP4725_PM_OPS (&mcp4725_pm_ops) 67#define MCP4725_PM_OPS (&mcp4725_pm_ops)
63#else 68#else
64#define MCP4725_PM_OPS NULL 69#define MCP4725_PM_OPS NULL
65#endif 70#endif
66 71
72static ssize_t mcp4725_store_eeprom(struct device *dev,
73 struct device_attribute *attr, const char *buf, size_t len)
74{
75 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
76 struct mcp4725_data *data = iio_priv(indio_dev);
77 int tries = 20;
78 u8 inoutbuf[3];
79 bool state;
80 int ret;
81
82 ret = strtobool(buf, &state);
83 if (ret < 0)
84 return ret;
85
86 if (!state)
87 return 0;
88
89 inoutbuf[0] = 0x60; /* write EEPROM */
90 inoutbuf[1] = data->dac_value >> 4;
91 inoutbuf[2] = (data->dac_value & 0xf) << 4;
92
93 ret = i2c_master_send(data->client, inoutbuf, 3);
94 if (ret < 0)
95 return ret;
96 else if (ret != 3)
97 return -EIO;
98
99 /* wait for write complete, takes up to 50ms */
100 while (tries--) {
101 msleep(20);
102 ret = i2c_master_recv(data->client, inoutbuf, 3);
103 if (ret < 0)
104 return ret;
105 else if (ret != 3)
106 return -EIO;
107
108 if (inoutbuf[0] & 0x80)
109 break;
110 }
111
112 if (tries < 0) {
113 dev_err(&data->client->dev,
114 "mcp4725_store_eeprom() failed, incomplete\n");
115 return -EIO;
116 }
117
118 return len;
119}
120
121static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, NULL, mcp4725_store_eeprom, 0);
122
123static struct attribute *mcp4725_attributes[] = {
124 &iio_dev_attr_store_eeprom.dev_attr.attr,
125 NULL,
126};
127
128static const struct attribute_group mcp4725_attribute_group = {
129 .attrs = mcp4725_attributes,
130};
131
132static const char * const mcp4725_powerdown_modes[] = {
133 "1kohm_to_gnd",
134 "100kohm_to_gnd",
135 "500kohm_to_gnd"
136};
137
138static int mcp4725_get_powerdown_mode(struct iio_dev *indio_dev,
139 const struct iio_chan_spec *chan)
140{
141 struct mcp4725_data *data = iio_priv(indio_dev);
142
143 return data->powerdown_mode;
144}
145
146static int mcp4725_set_powerdown_mode(struct iio_dev *indio_dev,
147 const struct iio_chan_spec *chan, unsigned mode)
148{
149 struct mcp4725_data *data = iio_priv(indio_dev);
150
151 data->powerdown_mode = mode;
152
153 return 0;
154}
155
156static ssize_t mcp4725_read_powerdown(struct iio_dev *indio_dev,
157 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
158{
159 struct mcp4725_data *data = iio_priv(indio_dev);
160
161 return sprintf(buf, "%d\n", data->powerdown);
162}
163
164static ssize_t mcp4725_write_powerdown(struct iio_dev *indio_dev,
165 uintptr_t private, const struct iio_chan_spec *chan,
166 const char *buf, size_t len)
167{
168 struct mcp4725_data *data = iio_priv(indio_dev);
169 bool state;
170 int ret;
171
172 ret = strtobool(buf, &state);
173 if (ret)
174 return ret;
175
176 if (state)
177 ret = mcp4725_suspend(&data->client->dev);
178 else
179 ret = mcp4725_resume(&data->client->dev);
180 if (ret < 0)
181 return ret;
182
183 return len;
184}
185
186static const struct iio_enum mcp4725_powerdown_mode_enum = {
187 .items = mcp4725_powerdown_modes,
188 .num_items = ARRAY_SIZE(mcp4725_powerdown_modes),
189 .get = mcp4725_get_powerdown_mode,
190 .set = mcp4725_set_powerdown_mode,
191};
192
193static const struct iio_chan_spec_ext_info mcp4725_ext_info[] = {
194 {
195 .name = "powerdown",
196 .read = mcp4725_read_powerdown,
197 .write = mcp4725_write_powerdown,
198 },
199 IIO_ENUM("powerdown_mode", false, &mcp4725_powerdown_mode_enum),
200 IIO_ENUM_AVAILABLE("powerdown_mode", &mcp4725_powerdown_mode_enum),
201 { },
202};
203
67static const struct iio_chan_spec mcp4725_channel = { 204static const struct iio_chan_spec mcp4725_channel = {
68 .type = IIO_VOLTAGE, 205 .type = IIO_VOLTAGE,
69 .indexed = 1, 206 .indexed = 1,
@@ -72,6 +209,7 @@ static const struct iio_chan_spec mcp4725_channel = {
72 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 209 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
73 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 210 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
74 .scan_type = IIO_ST('u', 12, 16, 0), 211 .scan_type = IIO_ST('u', 12, 16, 0),
212 .ext_info = mcp4725_ext_info,
75}; 213};
76 214
77static int mcp4725_set_value(struct iio_dev *indio_dev, int val) 215static int mcp4725_set_value(struct iio_dev *indio_dev, int val)
@@ -138,6 +276,7 @@ static int mcp4725_write_raw(struct iio_dev *indio_dev,
138static const struct iio_info mcp4725_info = { 276static const struct iio_info mcp4725_info = {
139 .read_raw = mcp4725_read_raw, 277 .read_raw = mcp4725_read_raw,
140 .write_raw = mcp4725_write_raw, 278 .write_raw = mcp4725_write_raw,
279 .attrs = &mcp4725_attribute_group,
141 .driver_module = THIS_MODULE, 280 .driver_module = THIS_MODULE,
142}; 281};
143 282
@@ -148,19 +287,17 @@ static int mcp4725_probe(struct i2c_client *client,
148 struct iio_dev *indio_dev; 287 struct iio_dev *indio_dev;
149 struct mcp4725_platform_data *platform_data = client->dev.platform_data; 288 struct mcp4725_platform_data *platform_data = client->dev.platform_data;
150 u8 inbuf[3]; 289 u8 inbuf[3];
290 u8 pd;
151 int err; 291 int err;
152 292
153 if (!platform_data || !platform_data->vref_mv) { 293 if (!platform_data || !platform_data->vref_mv) {
154 dev_err(&client->dev, "invalid platform data"); 294 dev_err(&client->dev, "invalid platform data");
155 err = -EINVAL; 295 return -EINVAL;
156 goto exit;
157 } 296 }
158 297
159 indio_dev = iio_device_alloc(sizeof(*data)); 298 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
160 if (indio_dev == NULL) { 299 if (indio_dev == NULL)
161 err = -ENOMEM; 300 return -ENOMEM;
162 goto exit;
163 }
164 data = iio_priv(indio_dev); 301 data = iio_priv(indio_dev);
165 i2c_set_clientdata(client, indio_dev); 302 i2c_set_clientdata(client, indio_dev);
166 data->client = client; 303 data->client = client;
@@ -177,31 +314,25 @@ static int mcp4725_probe(struct i2c_client *client,
177 err = i2c_master_recv(client, inbuf, 3); 314 err = i2c_master_recv(client, inbuf, 3);
178 if (err < 0) { 315 if (err < 0) {
179 dev_err(&client->dev, "failed to read DAC value"); 316 dev_err(&client->dev, "failed to read DAC value");
180 goto exit_free_device; 317 return err;
181 } 318 }
319 pd = (inbuf[0] >> 1) & 0x3;
320 data->powerdown = pd > 0 ? true : false;
321 data->powerdown_mode = pd ? pd-1 : 2; /* 500kohm_to_gnd */
182 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4); 322 data->dac_value = (inbuf[1] << 4) | (inbuf[2] >> 4);
183 323
184 err = iio_device_register(indio_dev); 324 err = iio_device_register(indio_dev);
185 if (err) 325 if (err)
186 goto exit_free_device; 326 return err;
187 327
188 dev_info(&client->dev, "MCP4725 DAC registered\n"); 328 dev_info(&client->dev, "MCP4725 DAC registered\n");
189 329
190 return 0; 330 return 0;
191
192exit_free_device:
193 iio_device_free(indio_dev);
194exit:
195 return err;
196} 331}
197 332
198static int mcp4725_remove(struct i2c_client *client) 333static int mcp4725_remove(struct i2c_client *client)
199{ 334{
200 struct iio_dev *indio_dev = i2c_get_clientdata(client); 335 iio_device_unregister(i2c_get_clientdata(client));
201
202 iio_device_unregister(indio_dev);
203 iio_device_free(indio_dev);
204
205 return 0; 336 return 0;
206} 337}
207 338
diff --git a/drivers/iio/frequency/Kconfig b/drivers/iio/frequency/Kconfig
index 6aaa33ef4544..dc5e0b72882f 100644
--- a/drivers/iio/frequency/Kconfig
+++ b/drivers/iio/frequency/Kconfig
@@ -4,6 +4,7 @@
4# Clock Distribution device drivers 4# Clock Distribution device drivers
5# Phase-Locked Loop (PLL) frequency synthesizers 5# Phase-Locked Loop (PLL) frequency synthesizers
6# 6#
7# When adding new entries keep the list in alphabetical order
7 8
8menu "Frequency Synthesizers DDS/PLL" 9menu "Frequency Synthesizers DDS/PLL"
9 10
diff --git a/drivers/iio/frequency/Makefile b/drivers/iio/frequency/Makefile
index 00d26e5d1dc2..2bca03f3e2e3 100644
--- a/drivers/iio/frequency/Makefile
+++ b/drivers/iio/frequency/Makefile
@@ -2,5 +2,6 @@
2# Makefile iio/frequency 2# Makefile iio/frequency
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_AD9523) += ad9523.o 6obj-$(CONFIG_AD9523) += ad9523.o
6obj-$(CONFIG_ADF4350) += adf4350.o 7obj-$(CONFIG_ADF4350) += adf4350.o
diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c
index 92276deeb026..7c5245d9f99c 100644
--- a/drivers/iio/frequency/ad9523.c
+++ b/drivers/iio/frequency/ad9523.c
@@ -961,17 +961,17 @@ static int ad9523_probe(struct spi_device *spi)
961 return -EINVAL; 961 return -EINVAL;
962 } 962 }
963 963
964 indio_dev = iio_device_alloc(sizeof(*st)); 964 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
965 if (indio_dev == NULL) 965 if (indio_dev == NULL)
966 return -ENOMEM; 966 return -ENOMEM;
967 967
968 st = iio_priv(indio_dev); 968 st = iio_priv(indio_dev);
969 969
970 st->reg = regulator_get(&spi->dev, "vcc"); 970 st->reg = devm_regulator_get(&spi->dev, "vcc");
971 if (!IS_ERR(st->reg)) { 971 if (!IS_ERR(st->reg)) {
972 ret = regulator_enable(st->reg); 972 ret = regulator_enable(st->reg);
973 if (ret) 973 if (ret)
974 goto error_put_reg; 974 return ret;
975 } 975 }
976 976
977 spi_set_drvdata(spi, indio_dev); 977 spi_set_drvdata(spi, indio_dev);
@@ -1001,11 +1001,6 @@ static int ad9523_probe(struct spi_device *spi)
1001error_disable_reg: 1001error_disable_reg:
1002 if (!IS_ERR(st->reg)) 1002 if (!IS_ERR(st->reg))
1003 regulator_disable(st->reg); 1003 regulator_disable(st->reg);
1004error_put_reg:
1005 if (!IS_ERR(st->reg))
1006 regulator_put(st->reg);
1007
1008 iio_device_free(indio_dev);
1009 1004
1010 return ret; 1005 return ret;
1011} 1006}
@@ -1017,12 +1012,8 @@ static int ad9523_remove(struct spi_device *spi)
1017 1012
1018 iio_device_unregister(indio_dev); 1013 iio_device_unregister(indio_dev);
1019 1014
1020 if (!IS_ERR(st->reg)) { 1015 if (!IS_ERR(st->reg))
1021 regulator_disable(st->reg); 1016 regulator_disable(st->reg);
1022 regulator_put(st->reg);
1023 }
1024
1025 iio_device_free(indio_dev);
1026 1017
1027 return 0; 1018 return 0;
1028} 1019}
diff --git a/drivers/iio/frequency/adf4350.c b/drivers/iio/frequency/adf4350.c
index a4157cdb314d..a7b30be86ae0 100644
--- a/drivers/iio/frequency/adf4350.c
+++ b/drivers/iio/frequency/adf4350.c
@@ -515,7 +515,7 @@ static int adf4350_probe(struct spi_device *spi)
515 } 515 }
516 516
517 if (!pdata->clkin) { 517 if (!pdata->clkin) {
518 clk = clk_get(&spi->dev, "clkin"); 518 clk = devm_clk_get(&spi->dev, "clkin");
519 if (IS_ERR(clk)) 519 if (IS_ERR(clk))
520 return -EPROBE_DEFER; 520 return -EPROBE_DEFER;
521 521
@@ -524,17 +524,17 @@ static int adf4350_probe(struct spi_device *spi)
524 return ret; 524 return ret;
525 } 525 }
526 526
527 indio_dev = iio_device_alloc(sizeof(*st)); 527 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
528 if (indio_dev == NULL) 528 if (indio_dev == NULL)
529 return -ENOMEM; 529 return -ENOMEM;
530 530
531 st = iio_priv(indio_dev); 531 st = iio_priv(indio_dev);
532 532
533 st->reg = regulator_get(&spi->dev, "vcc"); 533 st->reg = devm_regulator_get(&spi->dev, "vcc");
534 if (!IS_ERR(st->reg)) { 534 if (!IS_ERR(st->reg)) {
535 ret = regulator_enable(st->reg); 535 ret = regulator_enable(st->reg);
536 if (ret) 536 if (ret)
537 goto error_put_reg; 537 goto error_disable_clk;
538 } 538 }
539 539
540 spi_set_drvdata(spi, indio_dev); 540 spi_set_drvdata(spi, indio_dev);
@@ -564,7 +564,8 @@ static int adf4350_probe(struct spi_device *spi)
564 memset(st->regs_hw, 0xFF, sizeof(st->regs_hw)); 564 memset(st->regs_hw, 0xFF, sizeof(st->regs_hw));
565 565
566 if (gpio_is_valid(pdata->gpio_lock_detect)) { 566 if (gpio_is_valid(pdata->gpio_lock_detect)) {
567 ret = gpio_request(pdata->gpio_lock_detect, indio_dev->name); 567 ret = devm_gpio_request(&spi->dev, pdata->gpio_lock_detect,
568 indio_dev->name);
568 if (ret) { 569 if (ret) {
569 dev_err(&spi->dev, "fail to request lock detect GPIO-%d", 570 dev_err(&spi->dev, "fail to request lock detect GPIO-%d",
570 pdata->gpio_lock_detect); 571 pdata->gpio_lock_detect);
@@ -576,29 +577,21 @@ static int adf4350_probe(struct spi_device *spi)
576 if (pdata->power_up_frequency) { 577 if (pdata->power_up_frequency) {
577 ret = adf4350_set_freq(st, pdata->power_up_frequency); 578 ret = adf4350_set_freq(st, pdata->power_up_frequency);
578 if (ret) 579 if (ret)
579 goto error_free_gpio; 580 goto error_disable_reg;
580 } 581 }
581 582
582 ret = iio_device_register(indio_dev); 583 ret = iio_device_register(indio_dev);
583 if (ret) 584 if (ret)
584 goto error_free_gpio; 585 goto error_disable_reg;
585 586
586 return 0; 587 return 0;
587 588
588error_free_gpio:
589 if (gpio_is_valid(pdata->gpio_lock_detect))
590 gpio_free(pdata->gpio_lock_detect);
591
592error_disable_reg: 589error_disable_reg:
593 if (!IS_ERR(st->reg)) 590 if (!IS_ERR(st->reg))
594 regulator_disable(st->reg); 591 regulator_disable(st->reg);
595error_put_reg: 592error_disable_clk:
596 if (!IS_ERR(st->reg))
597 regulator_put(st->reg);
598
599 if (clk) 593 if (clk)
600 clk_disable_unprepare(clk); 594 clk_disable_unprepare(clk);
601 iio_device_free(indio_dev);
602 595
603 return ret; 596 return ret;
604} 597}
@@ -619,14 +612,8 @@ static int adf4350_remove(struct spi_device *spi)
619 612
620 if (!IS_ERR(reg)) { 613 if (!IS_ERR(reg)) {
621 regulator_disable(reg); 614 regulator_disable(reg);
622 regulator_put(reg);
623 } 615 }
624 616
625 if (gpio_is_valid(st->pdata->gpio_lock_detect))
626 gpio_free(st->pdata->gpio_lock_detect);
627
628 iio_device_free(indio_dev);
629
630 return 0; 617 return 0;
631} 618}
632 619
diff --git a/drivers/iio/gyro/Kconfig b/drivers/iio/gyro/Kconfig
index 8498e9dcda68..41c64a43bcab 100644
--- a/drivers/iio/gyro/Kconfig
+++ b/drivers/iio/gyro/Kconfig
@@ -1,6 +1,8 @@
1# 1#
2# IIO Digital Gyroscope Sensor drivers configuration 2# IIO Digital Gyroscope Sensor drivers configuration
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Digital gyroscope sensors" 6menu "Digital gyroscope sensors"
5 7
6config ADIS16080 8config ADIS16080
@@ -26,6 +28,18 @@ config ADIS16136
26 Say yes here to build support for the Analog Devices ADIS16133, ADIS16135, 28 Say yes here to build support for the Analog Devices ADIS16133, ADIS16135,
27 ADIS16136 gyroscope devices. 29 ADIS16136 gyroscope devices.
28 30
31config ADIS16260
32 tristate "Analog Devices ADIS16260 Digital Gyroscope Sensor SPI driver"
33 depends on SPI
34 select IIO_ADIS_LIB
35 select IIO_ADIS_LIB_BUFFER if IIO_BUFFER
36 help
37 Say yes here to build support for Analog Devices ADIS16260 ADIS16265
38 ADIS16250 ADIS16255 and ADIS16251 programmable digital gyroscope sensors.
39
40 This driver can also be built as a module. If so, the module
41 will be called adis16260.
42
29config ADXRS450 43config ADXRS450
30 tristate "Analog Devices ADXRS450/3 Digital Output Gyroscope SPI driver" 44 tristate "Analog Devices ADXRS450/3 Digital Output Gyroscope SPI driver"
31 depends on SPI 45 depends on SPI
@@ -58,8 +72,8 @@ config IIO_ST_GYRO_3AXIS
58 Say yes here to build support for STMicroelectronics gyroscopes: 72 Say yes here to build support for STMicroelectronics gyroscopes:
59 L3G4200D, LSM330DL, L3GD20, L3GD20H, LSM330DLC, L3G4IS, LSM330. 73 L3G4200D, LSM330DL, L3GD20, L3GD20H, LSM330DLC, L3G4IS, LSM330.
60 74
61 This driver can also be built as a module. If so, will be created 75 This driver can also be built as a module. If so, these modules
62 these modules: 76 will be created:
63 - st_gyro (core functions for the driver [it is mandatory]); 77 - st_gyro (core functions for the driver [it is mandatory]);
64 - st_gyro_i2c (necessary for the I2C devices [optional*]); 78 - st_gyro_i2c (necessary for the I2C devices [optional*]);
65 - st_gyro_spi (necessary for the SPI devices [optional*]); 79 - st_gyro_spi (necessary for the SPI devices [optional*]);
diff --git a/drivers/iio/gyro/Makefile b/drivers/iio/gyro/Makefile
index e9dc034aa18b..2f2752a4ea83 100644
--- a/drivers/iio/gyro/Makefile
+++ b/drivers/iio/gyro/Makefile
@@ -2,9 +2,11 @@
2# Makefile for industrial I/O gyroscope sensor drivers 2# Makefile for industrial I/O gyroscope sensor drivers
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_ADIS16080) += adis16080.o 6obj-$(CONFIG_ADIS16080) += adis16080.o
6obj-$(CONFIG_ADIS16130) += adis16130.o 7obj-$(CONFIG_ADIS16130) += adis16130.o
7obj-$(CONFIG_ADIS16136) += adis16136.o 8obj-$(CONFIG_ADIS16136) += adis16136.o
9obj-$(CONFIG_ADIS16260) += adis16260.o
8obj-$(CONFIG_ADXRS450) += adxrs450.o 10obj-$(CONFIG_ADXRS450) += adxrs450.o
9 11
10obj-$(CONFIG_HID_SENSOR_GYRO_3D) += hid-sensor-gyro-3d.o 12obj-$(CONFIG_HID_SENSOR_GYRO_3D) += hid-sensor-gyro-3d.o
diff --git a/drivers/iio/gyro/adis16080.c b/drivers/iio/gyro/adis16080.c
index e1bb5f994a54..e9ec022ae225 100644
--- a/drivers/iio/gyro/adis16080.c
+++ b/drivers/iio/gyro/adis16080.c
@@ -192,16 +192,13 @@ static const struct adis16080_chip_info adis16080_chip_info[] = {
192static int adis16080_probe(struct spi_device *spi) 192static int adis16080_probe(struct spi_device *spi)
193{ 193{
194 const struct spi_device_id *id = spi_get_device_id(spi); 194 const struct spi_device_id *id = spi_get_device_id(spi);
195 int ret;
196 struct adis16080_state *st; 195 struct adis16080_state *st;
197 struct iio_dev *indio_dev; 196 struct iio_dev *indio_dev;
198 197
199 /* setup the industrialio driver allocated elements */ 198 /* setup the industrialio driver allocated elements */
200 indio_dev = iio_device_alloc(sizeof(*st)); 199 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
201 if (indio_dev == NULL) { 200 if (!indio_dev)
202 ret = -ENOMEM; 201 return -ENOMEM;
203 goto error_ret;
204 }
205 st = iio_priv(indio_dev); 202 st = iio_priv(indio_dev);
206 /* this is only used for removal purposes */ 203 /* this is only used for removal purposes */
207 spi_set_drvdata(spi, indio_dev); 204 spi_set_drvdata(spi, indio_dev);
@@ -217,22 +214,12 @@ static int adis16080_probe(struct spi_device *spi)
217 indio_dev->info = &adis16080_info; 214 indio_dev->info = &adis16080_info;
218 indio_dev->modes = INDIO_DIRECT_MODE; 215 indio_dev->modes = INDIO_DIRECT_MODE;
219 216
220 ret = iio_device_register(indio_dev); 217 return iio_device_register(indio_dev);
221 if (ret)
222 goto error_free_dev;
223 return 0;
224
225error_free_dev:
226 iio_device_free(indio_dev);
227error_ret:
228 return ret;
229} 218}
230 219
231static int adis16080_remove(struct spi_device *spi) 220static int adis16080_remove(struct spi_device *spi)
232{ 221{
233 iio_device_unregister(spi_get_drvdata(spi)); 222 iio_device_unregister(spi_get_drvdata(spi));
234 iio_device_free(spi_get_drvdata(spi));
235
236 return 0; 223 return 0;
237} 224}
238 225
diff --git a/drivers/iio/gyro/adis16130.c b/drivers/iio/gyro/adis16130.c
index 129acdf801a4..ac66fc184042 100644
--- a/drivers/iio/gyro/adis16130.c
+++ b/drivers/iio/gyro/adis16130.c
@@ -148,16 +148,13 @@ static const struct iio_info adis16130_info = {
148 148
149static int adis16130_probe(struct spi_device *spi) 149static int adis16130_probe(struct spi_device *spi)
150{ 150{
151 int ret;
152 struct adis16130_state *st; 151 struct adis16130_state *st;
153 struct iio_dev *indio_dev; 152 struct iio_dev *indio_dev;
154 153
155 /* setup the industrialio driver allocated elements */ 154 /* setup the industrialio driver allocated elements */
156 indio_dev = iio_device_alloc(sizeof(*st)); 155 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
157 if (indio_dev == NULL) { 156 if (!indio_dev)
158 ret = -ENOMEM; 157 return -ENOMEM;
159 goto error_ret;
160 }
161 st = iio_priv(indio_dev); 158 st = iio_priv(indio_dev);
162 /* this is only used for removal purposes */ 159 /* this is only used for removal purposes */
163 spi_set_drvdata(spi, indio_dev); 160 spi_set_drvdata(spi, indio_dev);
@@ -170,24 +167,12 @@ static int adis16130_probe(struct spi_device *spi)
170 indio_dev->info = &adis16130_info; 167 indio_dev->info = &adis16130_info;
171 indio_dev->modes = INDIO_DIRECT_MODE; 168 indio_dev->modes = INDIO_DIRECT_MODE;
172 169
173 ret = iio_device_register(indio_dev); 170 return iio_device_register(indio_dev);
174 if (ret)
175 goto error_free_dev;
176
177 return 0;
178
179error_free_dev:
180 iio_device_free(indio_dev);
181
182error_ret:
183 return ret;
184} 171}
185 172
186static int adis16130_remove(struct spi_device *spi) 173static int adis16130_remove(struct spi_device *spi)
187{ 174{
188 iio_device_unregister(spi_get_drvdata(spi)); 175 iio_device_unregister(spi_get_drvdata(spi));
189 iio_device_free(spi_get_drvdata(spi));
190
191 return 0; 176 return 0;
192} 177}
193 178
diff --git a/drivers/iio/gyro/adis16136.c b/drivers/iio/gyro/adis16136.c
index 058e6d5c955f..591bd555e1f3 100644
--- a/drivers/iio/gyro/adis16136.c
+++ b/drivers/iio/gyro/adis16136.c
@@ -497,7 +497,7 @@ static int adis16136_probe(struct spi_device *spi)
497 struct iio_dev *indio_dev; 497 struct iio_dev *indio_dev;
498 int ret; 498 int ret;
499 499
500 indio_dev = iio_device_alloc(sizeof(*adis16136)); 500 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis16136));
501 if (indio_dev == NULL) 501 if (indio_dev == NULL)
502 return -ENOMEM; 502 return -ENOMEM;
503 503
@@ -515,11 +515,11 @@ static int adis16136_probe(struct spi_device *spi)
515 515
516 ret = adis_init(&adis16136->adis, indio_dev, spi, &adis16136_data); 516 ret = adis_init(&adis16136->adis, indio_dev, spi, &adis16136_data);
517 if (ret) 517 if (ret)
518 goto error_free_dev; 518 return ret;
519 519
520 ret = adis_setup_buffer_and_trigger(&adis16136->adis, indio_dev, NULL); 520 ret = adis_setup_buffer_and_trigger(&adis16136->adis, indio_dev, NULL);
521 if (ret) 521 if (ret)
522 goto error_free_dev; 522 return ret;
523 523
524 ret = adis16136_initial_setup(indio_dev); 524 ret = adis16136_initial_setup(indio_dev);
525 if (ret) 525 if (ret)
@@ -537,8 +537,6 @@ error_stop_device:
537 adis16136_stop_device(indio_dev); 537 adis16136_stop_device(indio_dev);
538error_cleanup_buffer: 538error_cleanup_buffer:
539 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev); 539 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev);
540error_free_dev:
541 iio_device_free(indio_dev);
542 return ret; 540 return ret;
543} 541}
544 542
@@ -552,8 +550,6 @@ static int adis16136_remove(struct spi_device *spi)
552 550
553 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev); 551 adis_cleanup_buffer_and_trigger(&adis16136->adis, indio_dev);
554 552
555 iio_device_free(indio_dev);
556
557 return 0; 553 return 0;
558} 554}
559 555
diff --git a/drivers/iio/gyro/adis16260.c b/drivers/iio/gyro/adis16260.c
new file mode 100644
index 000000000000..06541162fc02
--- /dev/null
+++ b/drivers/iio/gyro/adis16260.c
@@ -0,0 +1,422 @@
1/*
2 * ADIS16260/ADIS16265 Programmable Digital Gyroscope Sensor Driver
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/interrupt.h>
10#include <linux/mutex.h>
11#include <linux/device.h>
12#include <linux/kernel.h>
13#include <linux/spi/spi.h>
14#include <linux/sysfs.h>
15#include <linux/module.h>
16
17#include <linux/iio/iio.h>
18#include <linux/iio/sysfs.h>
19#include <linux/iio/buffer.h>
20#include <linux/iio/imu/adis.h>
21
22#define ADIS16260_STARTUP_DELAY 220 /* ms */
23
24#define ADIS16260_FLASH_CNT 0x00 /* Flash memory write count */
25#define ADIS16260_SUPPLY_OUT 0x02 /* Power supply measurement */
26#define ADIS16260_GYRO_OUT 0x04 /* X-axis gyroscope output */
27#define ADIS16260_AUX_ADC 0x0A /* analog input channel measurement */
28#define ADIS16260_TEMP_OUT 0x0C /* internal temperature measurement */
29#define ADIS16260_ANGL_OUT 0x0E /* angle displacement */
30#define ADIS16260_GYRO_OFF 0x14 /* Calibration, offset/bias adjustment */
31#define ADIS16260_GYRO_SCALE 0x16 /* Calibration, scale adjustment */
32#define ADIS16260_ALM_MAG1 0x20 /* Alarm 1 magnitude/polarity setting */
33#define ADIS16260_ALM_MAG2 0x22 /* Alarm 2 magnitude/polarity setting */
34#define ADIS16260_ALM_SMPL1 0x24 /* Alarm 1 dynamic rate of change setting */
35#define ADIS16260_ALM_SMPL2 0x26 /* Alarm 2 dynamic rate of change setting */
36#define ADIS16260_ALM_CTRL 0x28 /* Alarm control */
37#define ADIS16260_AUX_DAC 0x30 /* Auxiliary DAC data */
38#define ADIS16260_GPIO_CTRL 0x32 /* Control, digital I/O line */
39#define ADIS16260_MSC_CTRL 0x34 /* Control, data ready, self-test settings */
40#define ADIS16260_SMPL_PRD 0x36 /* Control, internal sample rate */
41#define ADIS16260_SENS_AVG 0x38 /* Control, dynamic range, filtering */
42#define ADIS16260_SLP_CNT 0x3A /* Control, sleep mode initiation */
43#define ADIS16260_DIAG_STAT 0x3C /* Diagnostic, error flags */
44#define ADIS16260_GLOB_CMD 0x3E /* Control, global commands */
45#define ADIS16260_LOT_ID1 0x52 /* Lot Identification Code 1 */
46#define ADIS16260_LOT_ID2 0x54 /* Lot Identification Code 2 */
47#define ADIS16260_PROD_ID 0x56 /* Product identifier;
48 * convert to decimal = 16,265/16,260 */
49#define ADIS16260_SERIAL_NUM 0x58 /* Serial number */
50
51#define ADIS16260_ERROR_ACTIVE (1<<14)
52#define ADIS16260_NEW_DATA (1<<15)
53
54/* MSC_CTRL */
55#define ADIS16260_MSC_CTRL_MEM_TEST (1<<11)
56/* Internal self-test enable */
57#define ADIS16260_MSC_CTRL_INT_SELF_TEST (1<<10)
58#define ADIS16260_MSC_CTRL_NEG_SELF_TEST (1<<9)
59#define ADIS16260_MSC_CTRL_POS_SELF_TEST (1<<8)
60#define ADIS16260_MSC_CTRL_DATA_RDY_EN (1<<2)
61#define ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH (1<<1)
62#define ADIS16260_MSC_CTRL_DATA_RDY_DIO2 (1<<0)
63
64/* SMPL_PRD */
65/* Time base (tB): 0 = 1.953 ms, 1 = 60.54 ms */
66#define ADIS16260_SMPL_PRD_TIME_BASE (1<<7)
67#define ADIS16260_SMPL_PRD_DIV_MASK 0x7F
68
69/* SLP_CNT */
70#define ADIS16260_SLP_CNT_POWER_OFF 0x80
71
72/* DIAG_STAT */
73#define ADIS16260_DIAG_STAT_ALARM2 (1<<9)
74#define ADIS16260_DIAG_STAT_ALARM1 (1<<8)
75#define ADIS16260_DIAG_STAT_FLASH_CHK_BIT 6
76#define ADIS16260_DIAG_STAT_SELF_TEST_BIT 5
77#define ADIS16260_DIAG_STAT_OVERFLOW_BIT 4
78#define ADIS16260_DIAG_STAT_SPI_FAIL_BIT 3
79#define ADIS16260_DIAG_STAT_FLASH_UPT_BIT 2
80#define ADIS16260_DIAG_STAT_POWER_HIGH_BIT 1
81#define ADIS16260_DIAG_STAT_POWER_LOW_BIT 0
82
83/* GLOB_CMD */
84#define ADIS16260_GLOB_CMD_SW_RESET (1<<7)
85#define ADIS16260_GLOB_CMD_FLASH_UPD (1<<3)
86#define ADIS16260_GLOB_CMD_DAC_LATCH (1<<2)
87#define ADIS16260_GLOB_CMD_FAC_CALIB (1<<1)
88#define ADIS16260_GLOB_CMD_AUTO_NULL (1<<0)
89
90#define ADIS16260_SPI_SLOW (u32)(300 * 1000)
91#define ADIS16260_SPI_BURST (u32)(1000 * 1000)
92#define ADIS16260_SPI_FAST (u32)(2000 * 1000)
93
94/* At the moment triggers are only used for ring buffer
95 * filling. This may change!
96 */
97
98#define ADIS16260_SCAN_GYRO 0
99#define ADIS16260_SCAN_SUPPLY 1
100#define ADIS16260_SCAN_AUX_ADC 2
101#define ADIS16260_SCAN_TEMP 3
102#define ADIS16260_SCAN_ANGL 4
103
104static ssize_t adis16260_read_frequency(struct device *dev,
105 struct device_attribute *attr,
106 char *buf)
107{
108 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
109 struct adis *adis = iio_priv(indio_dev);
110 int ret, len = 0;
111 u16 t;
112 int sps;
113 ret = adis_read_reg_16(adis, ADIS16260_SMPL_PRD, &t);
114 if (ret)
115 return ret;
116
117 if (spi_get_device_id(adis->spi)->driver_data) /* If an adis16251 */
118 sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 8 : 256;
119 else
120 sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048;
121 sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
122 len = sprintf(buf, "%d\n", sps);
123 return len;
124}
125
126static ssize_t adis16260_write_frequency(struct device *dev,
127 struct device_attribute *attr,
128 const char *buf,
129 size_t len)
130{
131 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
132 struct adis *adis = iio_priv(indio_dev);
133 unsigned int val;
134 int ret;
135 u8 t;
136
137 ret = kstrtouint(buf, 10, &val);
138 if (ret)
139 return ret;
140
141 mutex_lock(&indio_dev->mlock);
142 if (spi_get_device_id(adis->spi)->driver_data)
143 t = 256 / val;
144 else
145 t = 2048 / val;
146
147 if (t > ADIS16260_SMPL_PRD_DIV_MASK)
148 t = ADIS16260_SMPL_PRD_DIV_MASK;
149 else if (t > 0)
150 t--;
151
152 if (t >= 0x0A)
153 adis->spi->max_speed_hz = ADIS16260_SPI_SLOW;
154 else
155 adis->spi->max_speed_hz = ADIS16260_SPI_FAST;
156 ret = adis_write_reg_8(adis, ADIS16260_SMPL_PRD, t);
157
158 mutex_unlock(&indio_dev->mlock);
159
160 return ret ? ret : len;
161}
162
163/* Power down the device */
164static int adis16260_stop_device(struct iio_dev *indio_dev)
165{
166 struct adis *adis = iio_priv(indio_dev);
167 int ret;
168 u16 val = ADIS16260_SLP_CNT_POWER_OFF;
169
170 ret = adis_write_reg_16(adis, ADIS16260_SLP_CNT, val);
171 if (ret)
172 dev_err(&indio_dev->dev, "problem with turning device off: SLP_CNT");
173
174 return ret;
175}
176
177static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
178 adis16260_read_frequency,
179 adis16260_write_frequency);
180
181static const struct iio_chan_spec adis16260_channels[] = {
182 ADIS_GYRO_CHAN(X, ADIS16260_GYRO_OUT, ADIS16260_SCAN_GYRO,
183 BIT(IIO_CHAN_INFO_CALIBBIAS) |
184 BIT(IIO_CHAN_INFO_CALIBSCALE), 14),
185 ADIS_INCLI_CHAN(X, ADIS16260_ANGL_OUT, ADIS16260_SCAN_ANGL, 0, 14),
186 ADIS_TEMP_CHAN(ADIS16260_TEMP_OUT, ADIS16260_SCAN_TEMP, 12),
187 ADIS_SUPPLY_CHAN(ADIS16260_SUPPLY_OUT, ADIS16260_SCAN_SUPPLY, 12),
188 ADIS_AUX_ADC_CHAN(ADIS16260_AUX_ADC, ADIS16260_SCAN_AUX_ADC, 12),
189 IIO_CHAN_SOFT_TIMESTAMP(5),
190};
191
192static const u8 adis16260_addresses[][2] = {
193 [ADIS16260_SCAN_GYRO] = { ADIS16260_GYRO_OFF, ADIS16260_GYRO_SCALE },
194};
195
196static int adis16260_read_raw(struct iio_dev *indio_dev,
197 struct iio_chan_spec const *chan,
198 int *val, int *val2,
199 long mask)
200{
201 struct adis *adis = iio_priv(indio_dev);
202 int ret;
203 u8 addr;
204 s16 val16;
205
206 switch (mask) {
207 case IIO_CHAN_INFO_RAW:
208 return adis_single_conversion(indio_dev, chan,
209 ADIS16260_ERROR_ACTIVE, val);
210 case IIO_CHAN_INFO_SCALE:
211 switch (chan->type) {
212 case IIO_ANGL_VEL:
213 *val = 0;
214 if (spi_get_device_id(adis->spi)->driver_data) {
215 /* 0.01832 degree / sec */
216 *val2 = IIO_DEGREE_TO_RAD(18320);
217 } else {
218 /* 0.07326 degree / sec */
219 *val2 = IIO_DEGREE_TO_RAD(73260);
220 }
221 return IIO_VAL_INT_PLUS_MICRO;
222 case IIO_INCLI:
223 *val = 0;
224 *val2 = IIO_DEGREE_TO_RAD(36630);
225 return IIO_VAL_INT_PLUS_MICRO;
226 case IIO_VOLTAGE:
227 if (chan->channel == 0) {
228 *val = 1;
229 *val2 = 831500; /* 1.8315 mV */
230 } else {
231 *val = 0;
232 *val2 = 610500; /* 610.5 uV */
233 }
234 return IIO_VAL_INT_PLUS_MICRO;
235 case IIO_TEMP:
236 *val = 145;
237 *val2 = 300000; /* 0.1453 C */
238 return IIO_VAL_INT_PLUS_MICRO;
239 default:
240 return -EINVAL;
241 }
242 break;
243 case IIO_CHAN_INFO_OFFSET:
244 *val = 250000 / 1453; /* 25 C = 0x00 */
245 return IIO_VAL_INT;
246 case IIO_CHAN_INFO_CALIBBIAS:
247 addr = adis16260_addresses[chan->scan_index][0];
248 ret = adis_read_reg_16(adis, addr, &val16);
249 if (ret)
250 return ret;
251
252 *val = sign_extend32(val16, 11);
253 return IIO_VAL_INT;
254 case IIO_CHAN_INFO_CALIBSCALE:
255 addr = adis16260_addresses[chan->scan_index][1];
256 ret = adis_read_reg_16(adis, addr, &val16);
257 if (ret)
258 return ret;
259
260 *val = val16;
261 return IIO_VAL_INT;
262 }
263 return -EINVAL;
264}
265
266static int adis16260_write_raw(struct iio_dev *indio_dev,
267 struct iio_chan_spec const *chan,
268 int val,
269 int val2,
270 long mask)
271{
272 struct adis *adis = iio_priv(indio_dev);
273 u8 addr;
274
275 switch (mask) {
276 case IIO_CHAN_INFO_CALIBBIAS:
277 if (val < -2048 || val >= 2048)
278 return -EINVAL;
279
280 addr = adis16260_addresses[chan->scan_index][0];
281 return adis_write_reg_16(adis, addr, val);
282 case IIO_CHAN_INFO_CALIBSCALE:
283 if (val < 0 || val >= 4096)
284 return -EINVAL;
285
286 addr = adis16260_addresses[chan->scan_index][1];
287 return adis_write_reg_16(adis, addr, val);
288 }
289 return -EINVAL;
290}
291
292static struct attribute *adis16260_attributes[] = {
293 &iio_dev_attr_sampling_frequency.dev_attr.attr,
294 NULL
295};
296
297static const struct attribute_group adis16260_attribute_group = {
298 .attrs = adis16260_attributes,
299};
300
301static const struct iio_info adis16260_info = {
302 .attrs = &adis16260_attribute_group,
303 .read_raw = &adis16260_read_raw,
304 .write_raw = &adis16260_write_raw,
305 .update_scan_mode = adis_update_scan_mode,
306 .driver_module = THIS_MODULE,
307};
308
309static const char * const adis1620_status_error_msgs[] = {
310 [ADIS16260_DIAG_STAT_FLASH_CHK_BIT] = "Flash checksum error",
311 [ADIS16260_DIAG_STAT_SELF_TEST_BIT] = "Self test error",
312 [ADIS16260_DIAG_STAT_OVERFLOW_BIT] = "Sensor overrange",
313 [ADIS16260_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
314 [ADIS16260_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
315 [ADIS16260_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 5.25",
316 [ADIS16260_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 4.75",
317};
318
319static const struct adis_data adis16260_data = {
320 .write_delay = 30,
321 .read_delay = 30,
322 .msc_ctrl_reg = ADIS16260_MSC_CTRL,
323 .glob_cmd_reg = ADIS16260_GLOB_CMD,
324 .diag_stat_reg = ADIS16260_DIAG_STAT,
325
326 .self_test_mask = ADIS16260_MSC_CTRL_MEM_TEST,
327 .startup_delay = ADIS16260_STARTUP_DELAY,
328
329 .status_error_msgs = adis1620_status_error_msgs,
330 .status_error_mask = BIT(ADIS16260_DIAG_STAT_FLASH_CHK_BIT) |
331 BIT(ADIS16260_DIAG_STAT_SELF_TEST_BIT) |
332 BIT(ADIS16260_DIAG_STAT_OVERFLOW_BIT) |
333 BIT(ADIS16260_DIAG_STAT_SPI_FAIL_BIT) |
334 BIT(ADIS16260_DIAG_STAT_FLASH_UPT_BIT) |
335 BIT(ADIS16260_DIAG_STAT_POWER_HIGH_BIT) |
336 BIT(ADIS16260_DIAG_STAT_POWER_LOW_BIT),
337};
338
339static int adis16260_probe(struct spi_device *spi)
340{
341 struct iio_dev *indio_dev;
342 struct adis *adis;
343 int ret;
344
345 /* setup the industrialio driver allocated elements */
346 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adis));
347 if (!indio_dev)
348 return -ENOMEM;
349 adis = iio_priv(indio_dev);
350 /* this is only used for removal purposes */
351 spi_set_drvdata(spi, indio_dev);
352
353 indio_dev->name = spi_get_device_id(spi)->name;
354 indio_dev->dev.parent = &spi->dev;
355 indio_dev->info = &adis16260_info;
356 indio_dev->channels = adis16260_channels;
357 indio_dev->num_channels = ARRAY_SIZE(adis16260_channels);
358 indio_dev->modes = INDIO_DIRECT_MODE;
359
360 ret = adis_init(adis, indio_dev, spi, &adis16260_data);
361 if (ret)
362 return ret;
363
364 ret = adis_setup_buffer_and_trigger(adis, indio_dev, NULL);
365 if (ret)
366 return ret;
367
368 /* Get the device into a sane initial state */
369 ret = adis_initial_startup(adis);
370 if (ret)
371 goto error_cleanup_buffer_trigger;
372 ret = iio_device_register(indio_dev);
373 if (ret)
374 goto error_cleanup_buffer_trigger;
375
376 return 0;
377
378error_cleanup_buffer_trigger:
379 adis_cleanup_buffer_and_trigger(adis, indio_dev);
380 return ret;
381}
382
383static int adis16260_remove(struct spi_device *spi)
384{
385 struct iio_dev *indio_dev = spi_get_drvdata(spi);
386 struct adis *adis = iio_priv(indio_dev);
387
388 iio_device_unregister(indio_dev);
389 adis16260_stop_device(indio_dev);
390 adis_cleanup_buffer_and_trigger(adis, indio_dev);
391
392 return 0;
393}
394
395/*
396 * These parts do not need to be differentiated until someone adds
397 * support for the on chip filtering.
398 */
399static const struct spi_device_id adis16260_id[] = {
400 {"adis16260", 0},
401 {"adis16265", 0},
402 {"adis16250", 0},
403 {"adis16255", 0},
404 {"adis16251", 1},
405 {}
406};
407MODULE_DEVICE_TABLE(spi, adis16260_id);
408
409static struct spi_driver adis16260_driver = {
410 .driver = {
411 .name = "adis16260",
412 .owner = THIS_MODULE,
413 },
414 .probe = adis16260_probe,
415 .remove = adis16260_remove,
416 .id_table = adis16260_id,
417};
418module_spi_driver(adis16260_driver);
419
420MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
421MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
422MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/adxrs450.c b/drivers/iio/gyro/adxrs450.c
index 8bd72b490b7f..6dab2995f0f2 100644
--- a/drivers/iio/gyro/adxrs450.c
+++ b/drivers/iio/gyro/adxrs450.c
@@ -426,11 +426,9 @@ static int adxrs450_probe(struct spi_device *spi)
426 struct iio_dev *indio_dev; 426 struct iio_dev *indio_dev;
427 427
428 /* setup the industrialio driver allocated elements */ 428 /* setup the industrialio driver allocated elements */
429 indio_dev = iio_device_alloc(sizeof(*st)); 429 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
430 if (indio_dev == NULL) { 430 if (!indio_dev)
431 ret = -ENOMEM; 431 return -ENOMEM;
432 goto error_ret;
433 }
434 st = iio_priv(indio_dev); 432 st = iio_priv(indio_dev);
435 st->us = spi; 433 st->us = spi;
436 mutex_init(&st->buf_lock); 434 mutex_init(&st->buf_lock);
@@ -447,7 +445,7 @@ static int adxrs450_probe(struct spi_device *spi)
447 445
448 ret = iio_device_register(indio_dev); 446 ret = iio_device_register(indio_dev);
449 if (ret) 447 if (ret)
450 goto error_free_dev; 448 return ret;
451 449
452 /* Get the device into a sane initial state */ 450 /* Get the device into a sane initial state */
453 ret = adxrs450_initial_setup(indio_dev); 451 ret = adxrs450_initial_setup(indio_dev);
@@ -456,17 +454,12 @@ static int adxrs450_probe(struct spi_device *spi)
456 return 0; 454 return 0;
457error_initial: 455error_initial:
458 iio_device_unregister(indio_dev); 456 iio_device_unregister(indio_dev);
459error_free_dev:
460 iio_device_free(indio_dev);
461
462error_ret:
463 return ret; 457 return ret;
464} 458}
465 459
466static int adxrs450_remove(struct spi_device *spi) 460static int adxrs450_remove(struct spi_device *spi)
467{ 461{
468 iio_device_unregister(spi_get_drvdata(spi)); 462 iio_device_unregister(spi_get_drvdata(spi));
469 iio_device_free(spi_get_drvdata(spi));
470 463
471 return 0; 464 return 0;
472} 465}
diff --git a/drivers/iio/gyro/hid-sensor-gyro-3d.c b/drivers/iio/gyro/hid-sensor-gyro-3d.c
index bc943dd47da5..c688d974d3e3 100644
--- a/drivers/iio/gyro/hid-sensor-gyro-3d.c
+++ b/drivers/iio/gyro/hid-sensor-gyro-3d.c
@@ -30,10 +30,6 @@
30#include <linux/iio/triggered_buffer.h> 30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-trigger.h" 31#include "../common/hid-sensors/hid-sensor-trigger.h"
32 32
33/*Format: HID-SENSOR-usage_id_in_hex*/
34/*Usage ID from spec for Gyro-3D: 0x200076*/
35#define DRIVER_NAME "HID-SENSOR-200076"
36
37enum gyro_3d_channel { 33enum gyro_3d_channel {
38 CHANNEL_SCAN_INDEX_X, 34 CHANNEL_SCAN_INDEX_X,
39 CHANNEL_SCAN_INDEX_Y, 35 CHANNEL_SCAN_INDEX_Y,
@@ -179,18 +175,10 @@ static int gyro_3d_write_raw(struct iio_dev *indio_dev,
179 return ret; 175 return ret;
180} 176}
181 177
182static int gyro_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
183 struct iio_chan_spec const *chan,
184 long mask)
185{
186 return IIO_VAL_INT_PLUS_MICRO;
187}
188
189static const struct iio_info gyro_3d_info = { 178static const struct iio_info gyro_3d_info = {
190 .driver_module = THIS_MODULE, 179 .driver_module = THIS_MODULE,
191 .read_raw = &gyro_3d_read_raw, 180 .read_raw = &gyro_3d_read_raw,
192 .write_raw = &gyro_3d_write_raw, 181 .write_raw = &gyro_3d_write_raw,
193 .write_raw_get_fmt = &gyro_3d_write_raw_get_fmt,
194}; 182};
195 183
196/* Function to push data to buffer */ 184/* Function to push data to buffer */
@@ -286,11 +274,9 @@ static int hid_gyro_3d_probe(struct platform_device *pdev)
286 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 274 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
287 struct iio_chan_spec *channels; 275 struct iio_chan_spec *channels;
288 276
289 indio_dev = iio_device_alloc(sizeof(struct gyro_3d_state)); 277 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
290 if (indio_dev == NULL) { 278 if (!indio_dev)
291 ret = -ENOMEM; 279 return -ENOMEM;
292 goto error_ret;
293 }
294 platform_set_drvdata(pdev, indio_dev); 280 platform_set_drvdata(pdev, indio_dev);
295 281
296 gyro_state = iio_priv(indio_dev); 282 gyro_state = iio_priv(indio_dev);
@@ -302,15 +288,14 @@ static int hid_gyro_3d_probe(struct platform_device *pdev)
302 &gyro_state->common_attributes); 288 &gyro_state->common_attributes);
303 if (ret) { 289 if (ret) {
304 dev_err(&pdev->dev, "failed to setup common attributes\n"); 290 dev_err(&pdev->dev, "failed to setup common attributes\n");
305 goto error_free_dev; 291 return ret;
306 } 292 }
307 293
308 channels = kmemdup(gyro_3d_channels, sizeof(gyro_3d_channels), 294 channels = kmemdup(gyro_3d_channels, sizeof(gyro_3d_channels),
309 GFP_KERNEL); 295 GFP_KERNEL);
310 if (!channels) { 296 if (!channels) {
311 ret = -ENOMEM;
312 dev_err(&pdev->dev, "failed to duplicate channels\n"); 297 dev_err(&pdev->dev, "failed to duplicate channels\n");
313 goto error_free_dev; 298 return -ENOMEM;
314 } 299 }
315 300
316 ret = gyro_3d_parse_report(pdev, hsdev, channels, 301 ret = gyro_3d_parse_report(pdev, hsdev, channels,
@@ -367,9 +352,6 @@ error_unreg_buffer_funcs:
367 iio_triggered_buffer_cleanup(indio_dev); 352 iio_triggered_buffer_cleanup(indio_dev);
368error_free_dev_mem: 353error_free_dev_mem:
369 kfree(indio_dev->channels); 354 kfree(indio_dev->channels);
370error_free_dev:
371 iio_device_free(indio_dev);
372error_ret:
373 return ret; 355 return ret;
374} 356}
375 357
@@ -384,14 +366,23 @@ static int hid_gyro_3d_remove(struct platform_device *pdev)
384 hid_sensor_remove_trigger(indio_dev); 366 hid_sensor_remove_trigger(indio_dev);
385 iio_triggered_buffer_cleanup(indio_dev); 367 iio_triggered_buffer_cleanup(indio_dev);
386 kfree(indio_dev->channels); 368 kfree(indio_dev->channels);
387 iio_device_free(indio_dev);
388 369
389 return 0; 370 return 0;
390} 371}
391 372
373static struct platform_device_id hid_gyro_3d_ids[] = {
374 {
375 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
376 .name = "HID-SENSOR-200076",
377 },
378 { /* sentinel */ }
379};
380MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
381
392static struct platform_driver hid_gyro_3d_platform_driver = { 382static struct platform_driver hid_gyro_3d_platform_driver = {
383 .id_table = hid_gyro_3d_ids,
393 .driver = { 384 .driver = {
394 .name = DRIVER_NAME, 385 .name = KBUILD_MODNAME,
395 .owner = THIS_MODULE, 386 .owner = THIS_MODULE,
396 }, 387 },
397 .probe = hid_gyro_3d_probe, 388 .probe = hid_gyro_3d_probe,
diff --git a/drivers/iio/gyro/itg3200_core.c b/drivers/iio/gyro/itg3200_core.c
index d66605d2629d..4d3f3b92b361 100644
--- a/drivers/iio/gyro/itg3200_core.c
+++ b/drivers/iio/gyro/itg3200_core.c
@@ -309,11 +309,9 @@ static int itg3200_probe(struct i2c_client *client,
309 309
310 dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq); 310 dev_dbg(&client->dev, "probe I2C dev with IRQ %i", client->irq);
311 311
312 indio_dev = iio_device_alloc(sizeof(*st)); 312 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
313 if (indio_dev == NULL) { 313 if (!indio_dev)
314 ret = -ENOMEM; 314 return -ENOMEM;
315 goto error_ret;
316 }
317 315
318 st = iio_priv(indio_dev); 316 st = iio_priv(indio_dev);
319 317
@@ -330,7 +328,7 @@ static int itg3200_probe(struct i2c_client *client,
330 328
331 ret = itg3200_buffer_configure(indio_dev); 329 ret = itg3200_buffer_configure(indio_dev);
332 if (ret) 330 if (ret)
333 goto error_free_dev; 331 return ret;
334 332
335 if (client->irq) { 333 if (client->irq) {
336 ret = itg3200_probe_trigger(indio_dev); 334 ret = itg3200_probe_trigger(indio_dev);
@@ -353,9 +351,6 @@ error_remove_trigger:
353 itg3200_remove_trigger(indio_dev); 351 itg3200_remove_trigger(indio_dev);
354error_unconfigure_buffer: 352error_unconfigure_buffer:
355 itg3200_buffer_unconfigure(indio_dev); 353 itg3200_buffer_unconfigure(indio_dev);
356error_free_dev:
357 iio_device_free(indio_dev);
358error_ret:
359 return ret; 354 return ret;
360} 355}
361 356
@@ -370,8 +365,6 @@ static int itg3200_remove(struct i2c_client *client)
370 365
371 itg3200_buffer_unconfigure(indio_dev); 366 itg3200_buffer_unconfigure(indio_dev);
372 367
373 iio_device_free(indio_dev);
374
375 return 0; 368 return 0;
376} 369}
377 370
diff --git a/drivers/iio/gyro/st_gyro.h b/drivers/iio/gyro/st_gyro.h
index 3ad9907bb154..f8f2bf84a5a2 100644
--- a/drivers/iio/gyro/st_gyro.h
+++ b/drivers/iio/gyro/st_gyro.h
@@ -23,7 +23,16 @@
23#define L3G4IS_GYRO_DEV_NAME "l3g4is_ui" 23#define L3G4IS_GYRO_DEV_NAME "l3g4is_ui"
24#define LSM330_GYRO_DEV_NAME "lsm330_gyro" 24#define LSM330_GYRO_DEV_NAME "lsm330_gyro"
25 25
26int st_gyro_common_probe(struct iio_dev *indio_dev); 26/**
27 * struct st_sensors_platform_data - gyro platform data
28 * @drdy_int_pin: DRDY on gyros is available only on INT2 pin.
29 */
30static const struct st_sensors_platform_data gyro_pdata = {
31 .drdy_int_pin = 2,
32};
33
34int st_gyro_common_probe(struct iio_dev *indio_dev,
35 struct st_sensors_platform_data *pdata);
27void st_gyro_common_remove(struct iio_dev *indio_dev); 36void st_gyro_common_remove(struct iio_dev *indio_dev);
28 37
29#ifdef CONFIG_IIO_BUFFER 38#ifdef CONFIG_IIO_BUFFER
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c
index f9ed3488c314..e13c2b0bf3d1 100644
--- a/drivers/iio/gyro/st_gyro_core.c
+++ b/drivers/iio/gyro/st_gyro_core.c
@@ -60,7 +60,7 @@
60#define ST_GYRO_1_BDU_ADDR 0x23 60#define ST_GYRO_1_BDU_ADDR 0x23
61#define ST_GYRO_1_BDU_MASK 0x80 61#define ST_GYRO_1_BDU_MASK 0x80
62#define ST_GYRO_1_DRDY_IRQ_ADDR 0x22 62#define ST_GYRO_1_DRDY_IRQ_ADDR 0x22
63#define ST_GYRO_1_DRDY_IRQ_MASK 0x08 63#define ST_GYRO_1_DRDY_IRQ_INT2_MASK 0x08
64#define ST_GYRO_1_MULTIREAD_BIT true 64#define ST_GYRO_1_MULTIREAD_BIT true
65 65
66/* CUSTOM VALUES FOR SENSOR 2 */ 66/* CUSTOM VALUES FOR SENSOR 2 */
@@ -84,7 +84,7 @@
84#define ST_GYRO_2_BDU_ADDR 0x23 84#define ST_GYRO_2_BDU_ADDR 0x23
85#define ST_GYRO_2_BDU_MASK 0x80 85#define ST_GYRO_2_BDU_MASK 0x80
86#define ST_GYRO_2_DRDY_IRQ_ADDR 0x22 86#define ST_GYRO_2_DRDY_IRQ_ADDR 0x22
87#define ST_GYRO_2_DRDY_IRQ_MASK 0x08 87#define ST_GYRO_2_DRDY_IRQ_INT2_MASK 0x08
88#define ST_GYRO_2_MULTIREAD_BIT true 88#define ST_GYRO_2_MULTIREAD_BIT true
89 89
90static const struct iio_chan_spec st_gyro_16bit_channels[] = { 90static const struct iio_chan_spec st_gyro_16bit_channels[] = {
@@ -158,7 +158,7 @@ static const struct st_sensors st_gyro_sensors[] = {
158 }, 158 },
159 .drdy_irq = { 159 .drdy_irq = {
160 .addr = ST_GYRO_1_DRDY_IRQ_ADDR, 160 .addr = ST_GYRO_1_DRDY_IRQ_ADDR,
161 .mask = ST_GYRO_1_DRDY_IRQ_MASK, 161 .mask_int2 = ST_GYRO_1_DRDY_IRQ_INT2_MASK,
162 }, 162 },
163 .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT, 163 .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT,
164 .bootime = 2, 164 .bootime = 2,
@@ -221,7 +221,7 @@ static const struct st_sensors st_gyro_sensors[] = {
221 }, 221 },
222 .drdy_irq = { 222 .drdy_irq = {
223 .addr = ST_GYRO_2_DRDY_IRQ_ADDR, 223 .addr = ST_GYRO_2_DRDY_IRQ_ADDR,
224 .mask = ST_GYRO_2_DRDY_IRQ_MASK, 224 .mask_int2 = ST_GYRO_2_DRDY_IRQ_INT2_MASK,
225 }, 225 },
226 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT, 226 .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT,
227 .bootime = 2, 227 .bootime = 2,
@@ -302,7 +302,8 @@ static const struct iio_trigger_ops st_gyro_trigger_ops = {
302#define ST_GYRO_TRIGGER_OPS NULL 302#define ST_GYRO_TRIGGER_OPS NULL
303#endif 303#endif
304 304
305int st_gyro_common_probe(struct iio_dev *indio_dev) 305int st_gyro_common_probe(struct iio_dev *indio_dev,
306 struct st_sensors_platform_data *pdata)
306{ 307{
307 int err; 308 int err;
308 struct st_sensor_data *gdata = iio_priv(indio_dev); 309 struct st_sensor_data *gdata = iio_priv(indio_dev);
@@ -324,7 +325,7 @@ int st_gyro_common_probe(struct iio_dev *indio_dev)
324 &gdata->sensor->fs.fs_avl[0]; 325 &gdata->sensor->fs.fs_avl[0];
325 gdata->odr = gdata->sensor->odr.odr_avl[0].hz; 326 gdata->odr = gdata->sensor->odr.odr_avl[0].hz;
326 327
327 err = st_sensors_init_sensor(indio_dev); 328 err = st_sensors_init_sensor(indio_dev, pdata);
328 if (err < 0) 329 if (err < 0)
329 goto st_gyro_common_probe_error; 330 goto st_gyro_common_probe_error;
330 331
@@ -365,7 +366,6 @@ void st_gyro_common_remove(struct iio_dev *indio_dev)
365 st_sensors_deallocate_trigger(indio_dev); 366 st_sensors_deallocate_trigger(indio_dev);
366 st_gyro_deallocate_ring(indio_dev); 367 st_gyro_deallocate_ring(indio_dev);
367 } 368 }
368 iio_device_free(indio_dev);
369} 369}
370EXPORT_SYMBOL(st_gyro_common_remove); 370EXPORT_SYMBOL(st_gyro_common_remove);
371 371
diff --git a/drivers/iio/gyro/st_gyro_i2c.c b/drivers/iio/gyro/st_gyro_i2c.c
index 8a310500573d..16b8b8d70bf1 100644
--- a/drivers/iio/gyro/st_gyro_i2c.c
+++ b/drivers/iio/gyro/st_gyro_i2c.c
@@ -25,27 +25,21 @@ static int st_gyro_i2c_probe(struct i2c_client *client,
25 struct st_sensor_data *gdata; 25 struct st_sensor_data *gdata;
26 int err; 26 int err;
27 27
28 indio_dev = iio_device_alloc(sizeof(*gdata)); 28 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*gdata));
29 if (indio_dev == NULL) { 29 if (!indio_dev)
30 err = -ENOMEM; 30 return -ENOMEM;
31 goto iio_device_alloc_error;
32 }
33 31
34 gdata = iio_priv(indio_dev); 32 gdata = iio_priv(indio_dev);
35 gdata->dev = &client->dev; 33 gdata->dev = &client->dev;
36 34
37 st_sensors_i2c_configure(indio_dev, client, gdata); 35 st_sensors_i2c_configure(indio_dev, client, gdata);
38 36
39 err = st_gyro_common_probe(indio_dev); 37 err = st_gyro_common_probe(indio_dev,
38 (struct st_sensors_platform_data *)&gyro_pdata);
40 if (err < 0) 39 if (err < 0)
41 goto st_gyro_common_probe_error; 40 return err;
42 41
43 return 0; 42 return 0;
44
45st_gyro_common_probe_error:
46 iio_device_free(indio_dev);
47iio_device_alloc_error:
48 return err;
49} 43}
50 44
51static int st_gyro_i2c_remove(struct i2c_client *client) 45static int st_gyro_i2c_remove(struct i2c_client *client)
diff --git a/drivers/iio/gyro/st_gyro_spi.c b/drivers/iio/gyro/st_gyro_spi.c
index f3540390eb22..94763e25caf9 100644
--- a/drivers/iio/gyro/st_gyro_spi.c
+++ b/drivers/iio/gyro/st_gyro_spi.c
@@ -24,27 +24,21 @@ static int st_gyro_spi_probe(struct spi_device *spi)
24 struct st_sensor_data *gdata; 24 struct st_sensor_data *gdata;
25 int err; 25 int err;
26 26
27 indio_dev = iio_device_alloc(sizeof(*gdata)); 27 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*gdata));
28 if (indio_dev == NULL) { 28 if (!indio_dev)
29 err = -ENOMEM; 29 return -ENOMEM;
30 goto iio_device_alloc_error;
31 }
32 30
33 gdata = iio_priv(indio_dev); 31 gdata = iio_priv(indio_dev);
34 gdata->dev = &spi->dev; 32 gdata->dev = &spi->dev;
35 33
36 st_sensors_spi_configure(indio_dev, spi, gdata); 34 st_sensors_spi_configure(indio_dev, spi, gdata);
37 35
38 err = st_gyro_common_probe(indio_dev); 36 err = st_gyro_common_probe(indio_dev,
37 (struct st_sensors_platform_data *)&gyro_pdata);
39 if (err < 0) 38 if (err < 0)
40 goto st_gyro_common_probe_error; 39 return err;
41 40
42 return 0; 41 return 0;
43
44st_gyro_common_probe_error:
45 iio_device_free(indio_dev);
46iio_device_alloc_error:
47 return err;
48} 42}
49 43
50static int st_gyro_spi_remove(struct spi_device *spi) 44static int st_gyro_spi_remove(struct spi_device *spi)
diff --git a/drivers/iio/iio_core_trigger.h b/drivers/iio/iio_core_trigger.h
index 6f7c56fcbe78..1fdb1e4ea4a5 100644
--- a/drivers/iio/iio_core_trigger.h
+++ b/drivers/iio/iio_core_trigger.h
@@ -30,7 +30,7 @@ void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev);
30static int iio_device_register_trigger_consumer(struct iio_dev *indio_dev) 30static int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
31{ 31{
32 return 0; 32 return 0;
33}; 33}
34 34
35/** 35/**
36 * iio_device_unregister_trigger_consumer() - reverse the registration process 36 * iio_device_unregister_trigger_consumer() - reverse the registration process
@@ -38,9 +38,6 @@ static int iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
38 **/ 38 **/
39static void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev) 39static void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
40{ 40{
41}; 41}
42 42
43#endif /* CONFIG_TRIGGER_CONSUMER */ 43#endif /* CONFIG_TRIGGER_CONSUMER */
44
45
46
diff --git a/drivers/iio/imu/Kconfig b/drivers/iio/imu/Kconfig
index 4f40a10cb74f..663e88a1a3c1 100644
--- a/drivers/iio/imu/Kconfig
+++ b/drivers/iio/imu/Kconfig
@@ -1,6 +1,8 @@
1# 1#
2# IIO imu drivers configuration 2# IIO imu drivers configuration
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Inertial measurement units" 6menu "Inertial measurement units"
5 7
6config ADIS16400 8config ADIS16400
diff --git a/drivers/iio/imu/Makefile b/drivers/iio/imu/Makefile
index f2f56ceaed26..114d2c17cbe2 100644
--- a/drivers/iio/imu/Makefile
+++ b/drivers/iio/imu/Makefile
@@ -2,6 +2,7 @@
2# Makefile for Inertial Measurement Units 2# Makefile for Inertial Measurement Units
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5adis16400-y := adis16400_core.o 6adis16400-y := adis16400_core.o
6adis16400-$(CONFIG_IIO_BUFFER) += adis16400_buffer.o 7adis16400-$(CONFIG_IIO_BUFFER) += adis16400_buffer.o
7obj-$(CONFIG_ADIS16400) += adis16400.o 8obj-$(CONFIG_ADIS16400) += adis16400.o
diff --git a/drivers/iio/imu/adis16400_core.c b/drivers/iio/imu/adis16400_core.c
index f60591f0b925..3fb7757a1028 100644
--- a/drivers/iio/imu/adis16400_core.c
+++ b/drivers/iio/imu/adis16400_core.c
@@ -871,7 +871,7 @@ static int adis16400_probe(struct spi_device *spi)
871 struct iio_dev *indio_dev; 871 struct iio_dev *indio_dev;
872 int ret; 872 int ret;
873 873
874 indio_dev = iio_device_alloc(sizeof(*st)); 874 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
875 if (indio_dev == NULL) 875 if (indio_dev == NULL)
876 return -ENOMEM; 876 return -ENOMEM;
877 877
@@ -893,12 +893,12 @@ static int adis16400_probe(struct spi_device *spi)
893 893
894 ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data); 894 ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
895 if (ret) 895 if (ret)
896 goto error_free_dev; 896 return ret;
897 897
898 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev, 898 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
899 adis16400_trigger_handler); 899 adis16400_trigger_handler);
900 if (ret) 900 if (ret)
901 goto error_free_dev; 901 return ret;
902 902
903 /* Get the device into a sane initial state */ 903 /* Get the device into a sane initial state */
904 ret = adis16400_initial_setup(indio_dev); 904 ret = adis16400_initial_setup(indio_dev);
@@ -913,8 +913,6 @@ static int adis16400_probe(struct spi_device *spi)
913 913
914error_cleanup_buffer: 914error_cleanup_buffer:
915 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev); 915 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
916error_free_dev:
917 iio_device_free(indio_dev);
918 return ret; 916 return ret;
919} 917}
920 918
@@ -928,8 +926,6 @@ static int adis16400_remove(struct spi_device *spi)
928 926
929 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev); 927 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
930 928
931 iio_device_free(indio_dev);
932
933 return 0; 929 return 0;
934} 930}
935 931
diff --git a/drivers/iio/imu/adis16480.c b/drivers/iio/imu/adis16480.c
index b7db38376295..dd4206cac62d 100644
--- a/drivers/iio/imu/adis16480.c
+++ b/drivers/iio/imu/adis16480.c
@@ -839,7 +839,7 @@ static int adis16480_probe(struct spi_device *spi)
839 struct adis16480 *st; 839 struct adis16480 *st;
840 int ret; 840 int ret;
841 841
842 indio_dev = iio_device_alloc(sizeof(*st)); 842 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
843 if (indio_dev == NULL) 843 if (indio_dev == NULL)
844 return -ENOMEM; 844 return -ENOMEM;
845 845
@@ -857,11 +857,11 @@ static int adis16480_probe(struct spi_device *spi)
857 857
858 ret = adis_init(&st->adis, indio_dev, spi, &adis16480_data); 858 ret = adis_init(&st->adis, indio_dev, spi, &adis16480_data);
859 if (ret) 859 if (ret)
860 goto error_free_dev; 860 return ret;
861 861
862 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev, NULL); 862 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev, NULL);
863 if (ret) 863 if (ret)
864 goto error_free_dev; 864 return ret;
865 865
866 ret = adis16480_initial_setup(indio_dev); 866 ret = adis16480_initial_setup(indio_dev);
867 if (ret) 867 if (ret)
@@ -879,8 +879,6 @@ error_stop_device:
879 adis16480_stop_device(indio_dev); 879 adis16480_stop_device(indio_dev);
880error_cleanup_buffer: 880error_cleanup_buffer:
881 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev); 881 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
882error_free_dev:
883 iio_device_free(indio_dev);
884 return ret; 882 return ret;
885} 883}
886 884
@@ -894,8 +892,6 @@ static int adis16480_remove(struct spi_device *spi)
894 892
895 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev); 893 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
896 894
897 iio_device_free(indio_dev);
898
899 return 0; 895 return 0;
900} 896}
901 897
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
index fe4c61e219f3..df7f1e1157ae 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
@@ -23,6 +23,7 @@
23#include <linux/interrupt.h> 23#include <linux/interrupt.h>
24#include <linux/kfifo.h> 24#include <linux/kfifo.h>
25#include <linux/spinlock.h> 25#include <linux/spinlock.h>
26#include <linux/iio/iio.h>
26#include "inv_mpu_iio.h" 27#include "inv_mpu_iio.h"
27 28
28/* 29/*
@@ -663,16 +664,13 @@ static int inv_mpu_probe(struct i2c_client *client,
663 int result; 664 int result;
664 665
665 if (!i2c_check_functionality(client->adapter, 666 if (!i2c_check_functionality(client->adapter,
666 I2C_FUNC_SMBUS_READ_I2C_BLOCK | 667 I2C_FUNC_SMBUS_I2C_BLOCK))
667 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)) { 668 return -ENOSYS;
668 result = -ENOSYS; 669
669 goto out_no_free; 670 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
670 } 671 if (!indio_dev)
671 indio_dev = iio_device_alloc(sizeof(*st)); 672 return -ENOMEM;
672 if (indio_dev == NULL) { 673
673 result = -ENOMEM;
674 goto out_no_free;
675 }
676 st = iio_priv(indio_dev); 674 st = iio_priv(indio_dev);
677 st->client = client; 675 st->client = client;
678 st->plat_data = *(struct inv_mpu6050_platform_data 676 st->plat_data = *(struct inv_mpu6050_platform_data
@@ -680,13 +678,13 @@ static int inv_mpu_probe(struct i2c_client *client,
680 /* power is turned on inside check chip type*/ 678 /* power is turned on inside check chip type*/
681 result = inv_check_and_setup_chip(st, id); 679 result = inv_check_and_setup_chip(st, id);
682 if (result) 680 if (result)
683 goto out_free; 681 return result;
684 682
685 result = inv_mpu6050_init_config(indio_dev); 683 result = inv_mpu6050_init_config(indio_dev);
686 if (result) { 684 if (result) {
687 dev_err(&client->dev, 685 dev_err(&client->dev,
688 "Could not initialize device.\n"); 686 "Could not initialize device.\n");
689 goto out_free; 687 return result;
690 } 688 }
691 689
692 i2c_set_clientdata(client, indio_dev); 690 i2c_set_clientdata(client, indio_dev);
@@ -705,7 +703,7 @@ static int inv_mpu_probe(struct i2c_client *client,
705 if (result) { 703 if (result) {
706 dev_err(&st->client->dev, "configure buffer fail %d\n", 704 dev_err(&st->client->dev, "configure buffer fail %d\n",
707 result); 705 result);
708 goto out_free; 706 return result;
709 } 707 }
710 result = inv_mpu6050_probe_trigger(indio_dev); 708 result = inv_mpu6050_probe_trigger(indio_dev);
711 if (result) { 709 if (result) {
@@ -727,10 +725,6 @@ out_remove_trigger:
727 inv_mpu6050_remove_trigger(st); 725 inv_mpu6050_remove_trigger(st);
728out_unreg_ring: 726out_unreg_ring:
729 iio_triggered_buffer_cleanup(indio_dev); 727 iio_triggered_buffer_cleanup(indio_dev);
730out_free:
731 iio_device_free(indio_dev);
732out_no_free:
733
734 return result; 728 return result;
735} 729}
736 730
@@ -742,7 +736,6 @@ static int inv_mpu_remove(struct i2c_client *client)
742 iio_device_unregister(indio_dev); 736 iio_device_unregister(indio_dev);
743 inv_mpu6050_remove_trigger(st); 737 inv_mpu6050_remove_trigger(st);
744 iio_triggered_buffer_cleanup(indio_dev); 738 iio_triggered_buffer_cleanup(indio_dev);
745 iio_device_free(indio_dev);
746 739
747 return 0; 740 return 0;
748} 741}
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index e145931ef1b8..97f0297b120f 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -383,14 +383,14 @@ static ssize_t iio_read_channel_info(struct device *dev,
383 scale_db = true; 383 scale_db = true;
384 case IIO_VAL_INT_PLUS_MICRO: 384 case IIO_VAL_INT_PLUS_MICRO:
385 if (val2 < 0) 385 if (val2 < 0)
386 return sprintf(buf, "-%d.%06u%s\n", val, -val2, 386 return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
387 scale_db ? " dB" : ""); 387 scale_db ? " dB" : "");
388 else 388 else
389 return sprintf(buf, "%d.%06u%s\n", val, val2, 389 return sprintf(buf, "%d.%06u%s\n", val, val2,
390 scale_db ? " dB" : ""); 390 scale_db ? " dB" : "");
391 case IIO_VAL_INT_PLUS_NANO: 391 case IIO_VAL_INT_PLUS_NANO:
392 if (val2 < 0) 392 if (val2 < 0)
393 return sprintf(buf, "-%d.%09u\n", val, -val2); 393 return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
394 else 394 else
395 return sprintf(buf, "%d.%09u\n", val, val2); 395 return sprintf(buf, "%d.%09u\n", val, val2);
396 case IIO_VAL_FRACTIONAL: 396 case IIO_VAL_FRACTIONAL:
@@ -912,6 +912,53 @@ void iio_device_free(struct iio_dev *dev)
912} 912}
913EXPORT_SYMBOL(iio_device_free); 913EXPORT_SYMBOL(iio_device_free);
914 914
915static void devm_iio_device_release(struct device *dev, void *res)
916{
917 iio_device_free(*(struct iio_dev **)res);
918}
919
920static int devm_iio_device_match(struct device *dev, void *res, void *data)
921{
922 struct iio_dev **r = res;
923 if (!r || !*r) {
924 WARN_ON(!r || !*r);
925 return 0;
926 }
927 return *r == data;
928}
929
930struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
931{
932 struct iio_dev **ptr, *iio_dev;
933
934 ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
935 GFP_KERNEL);
936 if (!ptr)
937 return NULL;
938
939 /* use raw alloc_dr for kmalloc caller tracing */
940 iio_dev = iio_device_alloc(sizeof_priv);
941 if (iio_dev) {
942 *ptr = iio_dev;
943 devres_add(dev, ptr);
944 } else {
945 devres_free(ptr);
946 }
947
948 return iio_dev;
949}
950EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
951
952void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
953{
954 int rc;
955
956 rc = devres_release(dev, devm_iio_device_release,
957 devm_iio_device_match, iio_dev);
958 WARN_ON(rc);
959}
960EXPORT_SYMBOL_GPL(devm_iio_device_free);
961
915/** 962/**
916 * iio_chrdev_open() - chrdev file open for buffer access and ioctls 963 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
917 **/ 964 **/
diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 0dd9bb873130..bf5e70a32d3f 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -424,9 +424,8 @@ static void iio_trig_subirqunmask(struct irq_data *d)
424 trig->subirqs[d->irq - trig->subirq_base].enabled = true; 424 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
425} 425}
426 426
427struct iio_trigger *iio_trigger_alloc(const char *fmt, ...) 427static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
428{ 428{
429 va_list vargs;
430 struct iio_trigger *trig; 429 struct iio_trigger *trig;
431 trig = kzalloc(sizeof *trig, GFP_KERNEL); 430 trig = kzalloc(sizeof *trig, GFP_KERNEL);
432 if (trig) { 431 if (trig) {
@@ -444,9 +443,8 @@ struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
444 kfree(trig); 443 kfree(trig);
445 return NULL; 444 return NULL;
446 } 445 }
447 va_start(vargs, fmt); 446
448 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs); 447 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
449 va_end(vargs);
450 if (trig->name == NULL) { 448 if (trig->name == NULL) {
451 irq_free_descs(trig->subirq_base, 449 irq_free_descs(trig->subirq_base,
452 CONFIG_IIO_CONSUMERS_PER_TRIGGER); 450 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
@@ -467,6 +465,19 @@ struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
467 } 465 }
468 get_device(&trig->dev); 466 get_device(&trig->dev);
469 } 467 }
468
469 return trig;
470}
471
472struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
473{
474 struct iio_trigger *trig;
475 va_list vargs;
476
477 va_start(vargs, fmt);
478 trig = viio_trigger_alloc(fmt, vargs);
479 va_end(vargs);
480
470 return trig; 481 return trig;
471} 482}
472EXPORT_SYMBOL(iio_trigger_alloc); 483EXPORT_SYMBOL(iio_trigger_alloc);
@@ -478,6 +489,59 @@ void iio_trigger_free(struct iio_trigger *trig)
478} 489}
479EXPORT_SYMBOL(iio_trigger_free); 490EXPORT_SYMBOL(iio_trigger_free);
480 491
492static void devm_iio_trigger_release(struct device *dev, void *res)
493{
494 iio_trigger_free(*(struct iio_trigger **)res);
495}
496
497static int devm_iio_trigger_match(struct device *dev, void *res, void *data)
498{
499 struct iio_trigger **r = res;
500
501 if (!r || !*r) {
502 WARN_ON(!r || !*r);
503 return 0;
504 }
505
506 return *r == data;
507}
508
509struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
510 const char *fmt, ...)
511{
512 struct iio_trigger **ptr, *trig;
513 va_list vargs;
514
515 ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
516 GFP_KERNEL);
517 if (!ptr)
518 return NULL;
519
520 /* use raw alloc_dr for kmalloc caller tracing */
521 va_start(vargs, fmt);
522 trig = viio_trigger_alloc(fmt, vargs);
523 va_end(vargs);
524 if (trig) {
525 *ptr = trig;
526 devres_add(dev, ptr);
527 } else {
528 devres_free(ptr);
529 }
530
531 return trig;
532}
533EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
534
535void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
536{
537 int rc;
538
539 rc = devres_release(dev, devm_iio_trigger_release,
540 devm_iio_trigger_match, iio_trig);
541 WARN_ON(rc);
542}
543EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
544
481void iio_device_register_trigger_consumer(struct iio_dev *indio_dev) 545void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
482{ 546{
483 indio_dev->groups[indio_dev->groupcounter++] = 547 indio_dev->groups[indio_dev->groupcounter++] =
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 5ef1a396e0c9..bf9fa0d7aff9 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -1,6 +1,8 @@
1# 1#
2# Light sensors 2# Light sensors
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Light sensors" 6menu "Light sensors"
5 7
6config ADJD_S311 8config ADJD_S311
@@ -15,6 +17,27 @@ config ADJD_S311
15 This driver can also be built as a module. If so, the module 17 This driver can also be built as a module. If so, the module
16 will be called adjd_s311. 18 will be called adjd_s311.
17 19
20config APDS9300
21 tristate "APDS9300 ambient light sensor"
22 depends on I2C
23 help
24 Say Y here if you want to build a driver for the Avago APDS9300
25 ambient light sensor.
26
27 To compile this driver as a module, choose M here: the
28 module will be called apds9300.
29
30config HID_SENSOR_ALS
31 depends on HID_SENSOR_HUB
32 select IIO_BUFFER
33 select IIO_TRIGGERED_BUFFER
34 select HID_SENSOR_IIO_COMMON
35 select HID_SENSOR_IIO_TRIGGER
36 tristate "HID ALS"
37 help
38 Say yes here to build support for the HID SENSOR
39 Ambient light sensor.
40
18config SENSORS_LM3533 41config SENSORS_LM3533
19 tristate "LM3533 ambient light sensor" 42 tristate "LM3533 ambient light sensor"
20 depends on MFD_LM3533 43 depends on MFD_LM3533
@@ -52,15 +75,4 @@ config VCNL4000
52 To compile this driver as a module, choose M here: the 75 To compile this driver as a module, choose M here: the
53 module will be called vcnl4000. 76 module will be called vcnl4000.
54 77
55config HID_SENSOR_ALS
56 depends on HID_SENSOR_HUB
57 select IIO_BUFFER
58 select IIO_TRIGGERED_BUFFER
59 select HID_SENSOR_IIO_COMMON
60 select HID_SENSOR_IIO_TRIGGER
61 tristate "HID ALS"
62 help
63 Say yes here to build support for the HID SENSOR
64 Ambient light sensor.
65
66endmenu 78endmenu
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 040d9c75f8e6..354ee9ab2379 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -2,8 +2,10 @@
2# Makefile for IIO Light sensors 2# Makefile for IIO Light sensors
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_ADJD_S311) += adjd_s311.o 6obj-$(CONFIG_ADJD_S311) += adjd_s311.o
7obj-$(CONFIG_APDS9300) += apds9300.o
8obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o
6obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o 9obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o
7obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o 10obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o
8obj-$(CONFIG_VCNL4000) += vcnl4000.o 11obj-$(CONFIG_VCNL4000) += vcnl4000.o
9obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o
diff --git a/drivers/iio/light/adjd_s311.c b/drivers/iio/light/adjd_s311.c
index c1cd5698b8ae..23cff798598a 100644
--- a/drivers/iio/light/adjd_s311.c
+++ b/drivers/iio/light/adjd_s311.c
@@ -37,22 +37,14 @@
37#define ADJD_S311_CAP_GREEN 0x07 37#define ADJD_S311_CAP_GREEN 0x07
38#define ADJD_S311_CAP_BLUE 0x08 38#define ADJD_S311_CAP_BLUE 0x08
39#define ADJD_S311_CAP_CLEAR 0x09 39#define ADJD_S311_CAP_CLEAR 0x09
40#define ADJD_S311_INT_RED_LO 0x0a 40#define ADJD_S311_INT_RED 0x0a
41#define ADJD_S311_INT_RED_HI 0x0b 41#define ADJD_S311_INT_GREEN 0x0c
42#define ADJD_S311_INT_GREEN_LO 0x0c 42#define ADJD_S311_INT_BLUE 0x0e
43#define ADJD_S311_INT_GREEN_HI 0x0d 43#define ADJD_S311_INT_CLEAR 0x10
44#define ADJD_S311_INT_BLUE_LO 0x0e 44#define ADJD_S311_DATA_RED 0x40
45#define ADJD_S311_INT_BLUE_HI 0x0f 45#define ADJD_S311_DATA_GREEN 0x42
46#define ADJD_S311_INT_CLEAR_LO 0x10 46#define ADJD_S311_DATA_BLUE 0x44
47#define ADJD_S311_INT_CLEAR_HI 0x11 47#define ADJD_S311_DATA_CLEAR 0x46
48#define ADJD_S311_DATA_RED_LO 0x40
49#define ADJD_S311_DATA_RED_HI 0x41
50#define ADJD_S311_DATA_GREEN_LO 0x42
51#define ADJD_S311_DATA_GREEN_HI 0x43
52#define ADJD_S311_DATA_BLUE_LO 0x44
53#define ADJD_S311_DATA_BLUE_HI 0x45
54#define ADJD_S311_DATA_CLEAR_LO 0x46
55#define ADJD_S311_DATA_CLEAR_HI 0x47
56#define ADJD_S311_OFFSET_RED 0x48 48#define ADJD_S311_OFFSET_RED 0x48
57#define ADJD_S311_OFFSET_GREEN 0x49 49#define ADJD_S311_OFFSET_GREEN 0x49
58#define ADJD_S311_OFFSET_BLUE 0x4a 50#define ADJD_S311_OFFSET_BLUE 0x4a
@@ -73,8 +65,8 @@ enum adjd_s311_channel_idx {
73 IDX_RED, IDX_GREEN, IDX_BLUE, IDX_CLEAR 65 IDX_RED, IDX_GREEN, IDX_BLUE, IDX_CLEAR
74}; 66};
75 67
76#define ADJD_S311_DATA_REG(chan) (ADJD_S311_DATA_RED_LO + (chan) * 2) 68#define ADJD_S311_DATA_REG(chan) (ADJD_S311_DATA_RED + (chan) * 2)
77#define ADJD_S311_INT_REG(chan) (ADJD_S311_INT_RED_LO + (chan) * 2) 69#define ADJD_S311_INT_REG(chan) (ADJD_S311_INT_RED + (chan) * 2)
78#define ADJD_S311_CAP_REG(chan) (ADJD_S311_CAP_RED + (chan)) 70#define ADJD_S311_CAP_REG(chan) (ADJD_S311_CAP_RED + (chan))
79 71
80static int adjd_s311_req_data(struct iio_dev *indio_dev) 72static int adjd_s311_req_data(struct iio_dev *indio_dev)
@@ -294,11 +286,10 @@ static int adjd_s311_probe(struct i2c_client *client,
294 struct iio_dev *indio_dev; 286 struct iio_dev *indio_dev;
295 int err; 287 int err;
296 288
297 indio_dev = iio_device_alloc(sizeof(*data)); 289 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
298 if (indio_dev == NULL) { 290 if (indio_dev == NULL)
299 err = -ENOMEM; 291 return -ENOMEM;
300 goto exit; 292
301 }
302 data = iio_priv(indio_dev); 293 data = iio_priv(indio_dev);
303 i2c_set_clientdata(client, indio_dev); 294 i2c_set_clientdata(client, indio_dev);
304 data->client = client; 295 data->client = client;
@@ -313,7 +304,7 @@ static int adjd_s311_probe(struct i2c_client *client,
313 err = iio_triggered_buffer_setup(indio_dev, NULL, 304 err = iio_triggered_buffer_setup(indio_dev, NULL,
314 adjd_s311_trigger_handler, NULL); 305 adjd_s311_trigger_handler, NULL);
315 if (err < 0) 306 if (err < 0)
316 goto exit_free_device; 307 return err;
317 308
318 err = iio_device_register(indio_dev); 309 err = iio_device_register(indio_dev);
319 if (err) 310 if (err)
@@ -325,9 +316,6 @@ static int adjd_s311_probe(struct i2c_client *client,
325 316
326exit_unreg_buffer: 317exit_unreg_buffer:
327 iio_triggered_buffer_cleanup(indio_dev); 318 iio_triggered_buffer_cleanup(indio_dev);
328exit_free_device:
329 iio_device_free(indio_dev);
330exit:
331 return err; 319 return err;
332} 320}
333 321
@@ -339,7 +327,6 @@ static int adjd_s311_remove(struct i2c_client *client)
339 iio_device_unregister(indio_dev); 327 iio_device_unregister(indio_dev);
340 iio_triggered_buffer_cleanup(indio_dev); 328 iio_triggered_buffer_cleanup(indio_dev);
341 kfree(data->buffer); 329 kfree(data->buffer);
342 iio_device_free(indio_dev);
343 330
344 return 0; 331 return 0;
345} 332}
diff --git a/drivers/iio/light/apds9300.c b/drivers/iio/light/apds9300.c
new file mode 100644
index 000000000000..66a58bda6dc8
--- /dev/null
+++ b/drivers/iio/light/apds9300.c
@@ -0,0 +1,512 @@
1/*
2 * apds9300.c - IIO driver for Avago APDS9300 ambient light sensor
3 *
4 * Copyright 2013 Oleksandr Kravchenko <o.v.kravchenko@globallogic.com>
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 */
10
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/pm.h>
14#include <linux/i2c.h>
15#include <linux/err.h>
16#include <linux/mutex.h>
17#include <linux/interrupt.h>
18#include <linux/iio/iio.h>
19#include <linux/iio/sysfs.h>
20#include <linux/iio/events.h>
21
22#define APDS9300_DRV_NAME "apds9300"
23#define APDS9300_IRQ_NAME "apds9300_event"
24
25/* Command register bits */
26#define APDS9300_CMD BIT(7) /* Select command register. Must write as 1 */
27#define APDS9300_WORD BIT(5) /* I2C write/read: if 1 word, if 0 byte */
28#define APDS9300_CLEAR BIT(6) /* Interrupt clear. Clears pending interrupt */
29
30/* Register set */
31#define APDS9300_CONTROL 0x00 /* Control of basic functions */
32#define APDS9300_THRESHLOWLOW 0x02 /* Low byte of low interrupt threshold */
33#define APDS9300_THRESHHIGHLOW 0x04 /* Low byte of high interrupt threshold */
34#define APDS9300_INTERRUPT 0x06 /* Interrupt control */
35#define APDS9300_DATA0LOW 0x0c /* Low byte of ADC channel 0 */
36#define APDS9300_DATA1LOW 0x0e /* Low byte of ADC channel 1 */
37
38/* Power on/off value for APDS9300_CONTROL register */
39#define APDS9300_POWER_ON 0x03
40#define APDS9300_POWER_OFF 0x00
41
42/* Interrupts */
43#define APDS9300_INTR_ENABLE 0x10
44/* Interrupt Persist Function: Any value outside of threshold range */
45#define APDS9300_THRESH_INTR 0x01
46
47#define APDS9300_THRESH_MAX 0xffff /* Max threshold value */
48
49struct apds9300_data {
50 struct i2c_client *client;
51 struct mutex mutex;
52 int power_state;
53 int thresh_low;
54 int thresh_hi;
55 int intr_en;
56};
57
58/* Lux calculation */
59
60/* Calculated values 1000 * (CH1/CH0)^1.4 for CH1/CH0 from 0 to 0.52 */
61static const u16 apds9300_lux_ratio[] = {
62 0, 2, 4, 7, 11, 15, 19, 24, 29, 34, 40, 45, 51, 57, 64, 70, 77, 84, 91,
63 98, 105, 112, 120, 128, 136, 144, 152, 160, 168, 177, 185, 194, 203,
64 212, 221, 230, 239, 249, 258, 268, 277, 287, 297, 307, 317, 327, 337,
65 347, 358, 368, 379, 390, 400,
66};
67
68static unsigned long apds9300_calculate_lux(u16 ch0, u16 ch1)
69{
70 unsigned long lux, tmp;
71
72 /* avoid division by zero */
73 if (ch0 == 0)
74 return 0;
75
76 tmp = DIV_ROUND_UP(ch1 * 100, ch0);
77 if (tmp <= 52) {
78 lux = 3150 * ch0 - (unsigned long)DIV_ROUND_UP_ULL(ch0
79 * apds9300_lux_ratio[tmp] * 5930ull, 1000);
80 } else if (tmp <= 65) {
81 lux = 2290 * ch0 - 2910 * ch1;
82 } else if (tmp <= 80) {
83 lux = 1570 * ch0 - 1800 * ch1;
84 } else if (tmp <= 130) {
85 lux = 338 * ch0 - 260 * ch1;
86 } else {
87 lux = 0;
88 }
89
90 return lux / 100000;
91}
92
93static int apds9300_get_adc_val(struct apds9300_data *data, int adc_number)
94{
95 int ret;
96 u8 flags = APDS9300_CMD | APDS9300_WORD;
97
98 if (!data->power_state)
99 return -EBUSY;
100
101 /* Select ADC0 or ADC1 data register */
102 flags |= adc_number ? APDS9300_DATA1LOW : APDS9300_DATA0LOW;
103
104 ret = i2c_smbus_read_word_data(data->client, flags);
105 if (ret < 0)
106 dev_err(&data->client->dev,
107 "failed to read ADC%d value\n", adc_number);
108
109 return ret;
110}
111
112static int apds9300_set_thresh_low(struct apds9300_data *data, int value)
113{
114 int ret;
115
116 if (!data->power_state)
117 return -EBUSY;
118
119 if (value > APDS9300_THRESH_MAX)
120 return -EINVAL;
121
122 ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHLOWLOW
123 | APDS9300_CMD | APDS9300_WORD, value);
124 if (ret) {
125 dev_err(&data->client->dev, "failed to set thresh_low\n");
126 return ret;
127 }
128 data->thresh_low = value;
129
130 return 0;
131}
132
133static int apds9300_set_thresh_hi(struct apds9300_data *data, int value)
134{
135 int ret;
136
137 if (!data->power_state)
138 return -EBUSY;
139
140 if (value > APDS9300_THRESH_MAX)
141 return -EINVAL;
142
143 ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHHIGHLOW
144 | APDS9300_CMD | APDS9300_WORD, value);
145 if (ret) {
146 dev_err(&data->client->dev, "failed to set thresh_hi\n");
147 return ret;
148 }
149 data->thresh_hi = value;
150
151 return 0;
152}
153
154static int apds9300_set_intr_state(struct apds9300_data *data, int state)
155{
156 int ret;
157 u8 cmd;
158
159 if (!data->power_state)
160 return -EBUSY;
161
162 cmd = state ? APDS9300_INTR_ENABLE | APDS9300_THRESH_INTR : 0x00;
163 ret = i2c_smbus_write_byte_data(data->client,
164 APDS9300_INTERRUPT | APDS9300_CMD, cmd);
165 if (ret) {
166 dev_err(&data->client->dev,
167 "failed to set interrupt state %d\n", state);
168 return ret;
169 }
170 data->intr_en = state;
171
172 return 0;
173}
174
175static int apds9300_set_power_state(struct apds9300_data *data, int state)
176{
177 int ret;
178 u8 cmd;
179
180 cmd = state ? APDS9300_POWER_ON : APDS9300_POWER_OFF;
181 ret = i2c_smbus_write_byte_data(data->client,
182 APDS9300_CONTROL | APDS9300_CMD, cmd);
183 if (ret) {
184 dev_err(&data->client->dev,
185 "failed to set power state %d\n", state);
186 return ret;
187 }
188 data->power_state = state;
189
190 return 0;
191}
192
193static void apds9300_clear_intr(struct apds9300_data *data)
194{
195 int ret;
196
197 ret = i2c_smbus_write_byte(data->client, APDS9300_CLEAR | APDS9300_CMD);
198 if (ret < 0)
199 dev_err(&data->client->dev, "failed to clear interrupt\n");
200}
201
202static int apds9300_chip_init(struct apds9300_data *data)
203{
204 int ret;
205
206 /* Need to set power off to ensure that the chip is off */
207 ret = apds9300_set_power_state(data, 0);
208 if (ret < 0)
209 goto err;
210 /*
211 * Probe the chip. To do so we try to power up the device and then to
212 * read back the 0x03 code
213 */
214 ret = apds9300_set_power_state(data, 1);
215 if (ret < 0)
216 goto err;
217 ret = i2c_smbus_read_byte_data(data->client,
218 APDS9300_CONTROL | APDS9300_CMD);
219 if (ret != APDS9300_POWER_ON) {
220 ret = -ENODEV;
221 goto err;
222 }
223 /*
224 * Disable interrupt to ensure thai it is doesn't enable
225 * i.e. after device soft reset
226 */
227 ret = apds9300_set_intr_state(data, 0);
228 if (ret < 0)
229 goto err;
230
231 return 0;
232
233err:
234 dev_err(&data->client->dev, "failed to init the chip\n");
235 return ret;
236}
237
238static int apds9300_read_raw(struct iio_dev *indio_dev,
239 struct iio_chan_spec const *chan, int *val, int *val2,
240 long mask)
241{
242 int ch0, ch1, ret = -EINVAL;
243 struct apds9300_data *data = iio_priv(indio_dev);
244
245 mutex_lock(&data->mutex);
246 switch (chan->type) {
247 case IIO_LIGHT:
248 ch0 = apds9300_get_adc_val(data, 0);
249 if (ch0 < 0) {
250 ret = ch0;
251 break;
252 }
253 ch1 = apds9300_get_adc_val(data, 1);
254 if (ch1 < 0) {
255 ret = ch1;
256 break;
257 }
258 *val = apds9300_calculate_lux(ch0, ch1);
259 ret = IIO_VAL_INT;
260 break;
261 case IIO_INTENSITY:
262 ret = apds9300_get_adc_val(data, chan->channel);
263 if (ret < 0)
264 break;
265 *val = ret;
266 ret = IIO_VAL_INT;
267 break;
268 default:
269 break;
270 }
271 mutex_unlock(&data->mutex);
272
273 return ret;
274}
275
276static int apds9300_read_thresh(struct iio_dev *indio_dev, u64 event_code,
277 int *val)
278{
279 struct apds9300_data *data = iio_priv(indio_dev);
280
281 switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
282 case IIO_EV_DIR_RISING:
283 *val = data->thresh_hi;
284 break;
285 case IIO_EV_DIR_FALLING:
286 *val = data->thresh_low;
287 break;
288 default:
289 return -EINVAL;
290 }
291
292 return 0;
293}
294
295static int apds9300_write_thresh(struct iio_dev *indio_dev, u64 event_code,
296 int val)
297{
298 struct apds9300_data *data = iio_priv(indio_dev);
299 int ret;
300
301 mutex_lock(&data->mutex);
302 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
303 ret = apds9300_set_thresh_hi(data, val);
304 else
305 ret = apds9300_set_thresh_low(data, val);
306 mutex_unlock(&data->mutex);
307
308 return ret;
309}
310
311static int apds9300_read_interrupt_config(struct iio_dev *indio_dev,
312 u64 event_code)
313{
314 struct apds9300_data *data = iio_priv(indio_dev);
315
316 return data->intr_en;
317}
318
319static int apds9300_write_interrupt_config(struct iio_dev *indio_dev,
320 u64 event_code, int state)
321{
322 struct apds9300_data *data = iio_priv(indio_dev);
323 int ret;
324
325 mutex_lock(&data->mutex);
326 ret = apds9300_set_intr_state(data, state);
327 mutex_unlock(&data->mutex);
328
329 return ret;
330}
331
332static const struct iio_info apds9300_info_no_irq = {
333 .driver_module = THIS_MODULE,
334 .read_raw = apds9300_read_raw,
335};
336
337static const struct iio_info apds9300_info = {
338 .driver_module = THIS_MODULE,
339 .read_raw = apds9300_read_raw,
340 .read_event_value = apds9300_read_thresh,
341 .write_event_value = apds9300_write_thresh,
342 .read_event_config = apds9300_read_interrupt_config,
343 .write_event_config = apds9300_write_interrupt_config,
344};
345
346static const struct iio_chan_spec apds9300_channels[] = {
347 {
348 .type = IIO_LIGHT,
349 .channel = 0,
350 .indexed = true,
351 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
352 }, {
353 .type = IIO_INTENSITY,
354 .channel = 0,
355 .channel2 = IIO_MOD_LIGHT_BOTH,
356 .indexed = true,
357 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
358 .event_mask = (IIO_EV_BIT(IIO_EV_TYPE_THRESH,
359 IIO_EV_DIR_RISING) |
360 IIO_EV_BIT(IIO_EV_TYPE_THRESH,
361 IIO_EV_DIR_FALLING)),
362 }, {
363 .type = IIO_INTENSITY,
364 .channel = 1,
365 .channel2 = IIO_MOD_LIGHT_IR,
366 .indexed = true,
367 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
368 },
369};
370
371static irqreturn_t apds9300_interrupt_handler(int irq, void *private)
372{
373 struct iio_dev *dev_info = private;
374 struct apds9300_data *data = iio_priv(dev_info);
375
376 iio_push_event(dev_info,
377 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
378 IIO_EV_TYPE_THRESH,
379 IIO_EV_DIR_EITHER),
380 iio_get_time_ns());
381
382 apds9300_clear_intr(data);
383
384 return IRQ_HANDLED;
385}
386
387static int apds9300_probe(struct i2c_client *client,
388 const struct i2c_device_id *id)
389{
390 struct apds9300_data *data;
391 struct iio_dev *indio_dev;
392 int ret;
393
394 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
395 if (!indio_dev)
396 return -ENOMEM;
397
398 data = iio_priv(indio_dev);
399 i2c_set_clientdata(client, indio_dev);
400 data->client = client;
401
402 ret = apds9300_chip_init(data);
403 if (ret < 0)
404 goto err;
405
406 mutex_init(&data->mutex);
407
408 indio_dev->dev.parent = &client->dev;
409 indio_dev->channels = apds9300_channels;
410 indio_dev->num_channels = ARRAY_SIZE(apds9300_channels);
411 indio_dev->name = APDS9300_DRV_NAME;
412 indio_dev->modes = INDIO_DIRECT_MODE;
413
414 if (client->irq)
415 indio_dev->info = &apds9300_info;
416 else
417 indio_dev->info = &apds9300_info_no_irq;
418
419 if (client->irq) {
420 ret = devm_request_threaded_irq(&client->dev, client->irq,
421 NULL, apds9300_interrupt_handler,
422 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
423 APDS9300_IRQ_NAME, indio_dev);
424 if (ret) {
425 dev_err(&client->dev, "irq request error %d\n", -ret);
426 goto err;
427 }
428 }
429
430 ret = iio_device_register(indio_dev);
431 if (ret < 0)
432 goto err;
433
434 return 0;
435
436err:
437 /* Ensure that power off in case of error */
438 apds9300_set_power_state(data, 0);
439 return ret;
440}
441
442static int apds9300_remove(struct i2c_client *client)
443{
444 struct iio_dev *indio_dev = i2c_get_clientdata(client);
445 struct apds9300_data *data = iio_priv(indio_dev);
446
447 iio_device_unregister(indio_dev);
448
449 /* Ensure that power off and interrupts are disabled */
450 apds9300_set_intr_state(data, 0);
451 apds9300_set_power_state(data, 0);
452
453 return 0;
454}
455
456#ifdef CONFIG_PM_SLEEP
457static int apds9300_suspend(struct device *dev)
458{
459 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
460 struct apds9300_data *data = iio_priv(indio_dev);
461 int ret;
462
463 mutex_lock(&data->mutex);
464 ret = apds9300_set_power_state(data, 0);
465 mutex_unlock(&data->mutex);
466
467 return ret;
468}
469
470static int apds9300_resume(struct device *dev)
471{
472 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
473 struct apds9300_data *data = iio_priv(indio_dev);
474 int ret;
475
476 mutex_lock(&data->mutex);
477 ret = apds9300_set_power_state(data, 1);
478 mutex_unlock(&data->mutex);
479
480 return ret;
481}
482
483static SIMPLE_DEV_PM_OPS(apds9300_pm_ops, apds9300_suspend, apds9300_resume);
484#define APDS9300_PM_OPS (&apds9300_pm_ops)
485#else
486#define APDS9300_PM_OPS NULL
487#endif
488
489static struct i2c_device_id apds9300_id[] = {
490 { APDS9300_DRV_NAME, 0 },
491 { }
492};
493
494MODULE_DEVICE_TABLE(i2c, apds9300_id);
495
496static struct i2c_driver apds9300_driver = {
497 .driver = {
498 .name = APDS9300_DRV_NAME,
499 .owner = THIS_MODULE,
500 .pm = APDS9300_PM_OPS,
501 },
502 .probe = apds9300_probe,
503 .remove = apds9300_remove,
504 .id_table = apds9300_id,
505};
506
507module_i2c_driver(apds9300_driver);
508
509MODULE_AUTHOR("Kravchenko Oleksandr <o.v.kravchenko@globallogic.com>");
510MODULE_AUTHOR("GlobalLogic inc.");
511MODULE_DESCRIPTION("APDS9300 ambient light photo sensor driver");
512MODULE_LICENSE("GPL");
diff --git a/drivers/iio/light/hid-sensor-als.c b/drivers/iio/light/hid-sensor-als.c
index cdc2cad0f01b..e59d00c3139c 100644
--- a/drivers/iio/light/hid-sensor-als.c
+++ b/drivers/iio/light/hid-sensor-als.c
@@ -30,10 +30,6 @@
30#include <linux/iio/triggered_buffer.h> 30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-trigger.h" 31#include "../common/hid-sensors/hid-sensor-trigger.h"
32 32
33/*Format: HID-SENSOR-usage_id_in_hex*/
34/*Usage ID from spec for Ambiant-Light: 0x200041*/
35#define DRIVER_NAME "HID-SENSOR-200041"
36
37#define CHANNEL_SCAN_INDEX_ILLUM 0 33#define CHANNEL_SCAN_INDEX_ILLUM 0
38 34
39struct als_state { 35struct als_state {
@@ -158,18 +154,10 @@ static int als_write_raw(struct iio_dev *indio_dev,
158 return ret; 154 return ret;
159} 155}
160 156
161static int als_write_raw_get_fmt(struct iio_dev *indio_dev,
162 struct iio_chan_spec const *chan,
163 long mask)
164{
165 return IIO_VAL_INT_PLUS_MICRO;
166}
167
168static const struct iio_info als_info = { 157static const struct iio_info als_info = {
169 .driver_module = THIS_MODULE, 158 .driver_module = THIS_MODULE,
170 .read_raw = &als_read_raw, 159 .read_raw = &als_read_raw,
171 .write_raw = &als_write_raw, 160 .write_raw = &als_write_raw,
172 .write_raw_get_fmt = &als_write_raw_get_fmt,
173}; 161};
174 162
175/* Function to push data to buffer */ 163/* Function to push data to buffer */
@@ -253,11 +241,9 @@ static int hid_als_probe(struct platform_device *pdev)
253 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 241 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
254 struct iio_chan_spec *channels; 242 struct iio_chan_spec *channels;
255 243
256 indio_dev = iio_device_alloc(sizeof(struct als_state)); 244 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state));
257 if (indio_dev == NULL) { 245 if (!indio_dev)
258 ret = -ENOMEM; 246 return -ENOMEM;
259 goto error_ret;
260 }
261 platform_set_drvdata(pdev, indio_dev); 247 platform_set_drvdata(pdev, indio_dev);
262 248
263 als_state = iio_priv(indio_dev); 249 als_state = iio_priv(indio_dev);
@@ -268,14 +254,13 @@ static int hid_als_probe(struct platform_device *pdev)
268 &als_state->common_attributes); 254 &als_state->common_attributes);
269 if (ret) { 255 if (ret) {
270 dev_err(&pdev->dev, "failed to setup common attributes\n"); 256 dev_err(&pdev->dev, "failed to setup common attributes\n");
271 goto error_free_dev; 257 return ret;
272 } 258 }
273 259
274 channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL); 260 channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL);
275 if (!channels) { 261 if (!channels) {
276 ret = -ENOMEM;
277 dev_err(&pdev->dev, "failed to duplicate channels\n"); 262 dev_err(&pdev->dev, "failed to duplicate channels\n");
278 goto error_free_dev; 263 return -ENOMEM;
279 } 264 }
280 265
281 ret = als_parse_report(pdev, hsdev, channels, 266 ret = als_parse_report(pdev, hsdev, channels,
@@ -333,9 +318,6 @@ error_unreg_buffer_funcs:
333 iio_triggered_buffer_cleanup(indio_dev); 318 iio_triggered_buffer_cleanup(indio_dev);
334error_free_dev_mem: 319error_free_dev_mem:
335 kfree(indio_dev->channels); 320 kfree(indio_dev->channels);
336error_free_dev:
337 iio_device_free(indio_dev);
338error_ret:
339 return ret; 321 return ret;
340} 322}
341 323
@@ -350,14 +332,23 @@ static int hid_als_remove(struct platform_device *pdev)
350 hid_sensor_remove_trigger(indio_dev); 332 hid_sensor_remove_trigger(indio_dev);
351 iio_triggered_buffer_cleanup(indio_dev); 333 iio_triggered_buffer_cleanup(indio_dev);
352 kfree(indio_dev->channels); 334 kfree(indio_dev->channels);
353 iio_device_free(indio_dev);
354 335
355 return 0; 336 return 0;
356} 337}
357 338
339static struct platform_device_id hid_als_ids[] = {
340 {
341 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
342 .name = "HID-SENSOR-200041",
343 },
344 { /* sentinel */ }
345};
346MODULE_DEVICE_TABLE(platform, hid_als_ids);
347
358static struct platform_driver hid_als_platform_driver = { 348static struct platform_driver hid_als_platform_driver = {
349 .id_table = hid_als_ids,
359 .driver = { 350 .driver = {
360 .name = DRIVER_NAME, 351 .name = KBUILD_MODNAME,
361 .owner = THIS_MODULE, 352 .owner = THIS_MODULE,
362 }, 353 },
363 .probe = hid_als_probe, 354 .probe = hid_als_probe,
diff --git a/drivers/iio/light/lm3533-als.c b/drivers/iio/light/lm3533-als.c
index 5fa31a4ef82a..c1aadc6b865a 100644
--- a/drivers/iio/light/lm3533-als.c
+++ b/drivers/iio/light/lm3533-als.c
@@ -847,7 +847,7 @@ static int lm3533_als_probe(struct platform_device *pdev)
847 return -EINVAL; 847 return -EINVAL;
848 } 848 }
849 849
850 indio_dev = iio_device_alloc(sizeof(*als)); 850 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*als));
851 if (!indio_dev) 851 if (!indio_dev)
852 return -ENOMEM; 852 return -ENOMEM;
853 853
@@ -870,7 +870,7 @@ static int lm3533_als_probe(struct platform_device *pdev)
870 if (als->irq) { 870 if (als->irq) {
871 ret = lm3533_als_setup_irq(als, indio_dev); 871 ret = lm3533_als_setup_irq(als, indio_dev);
872 if (ret) 872 if (ret)
873 goto err_free_dev; 873 return ret;
874 } 874 }
875 875
876 ret = lm3533_als_setup(als, pdata); 876 ret = lm3533_als_setup(als, pdata);
@@ -894,8 +894,6 @@ err_disable:
894err_free_irq: 894err_free_irq:
895 if (als->irq) 895 if (als->irq)
896 free_irq(als->irq, indio_dev); 896 free_irq(als->irq, indio_dev);
897err_free_dev:
898 iio_device_free(indio_dev);
899 897
900 return ret; 898 return ret;
901} 899}
@@ -910,7 +908,6 @@ static int lm3533_als_remove(struct platform_device *pdev)
910 lm3533_als_disable(als); 908 lm3533_als_disable(als);
911 if (als->irq) 909 if (als->irq)
912 free_irq(als->irq, indio_dev); 910 free_irq(als->irq, indio_dev);
913 iio_device_free(indio_dev);
914 911
915 return 0; 912 return 0;
916} 913}
diff --git a/drivers/iio/light/tsl2563.c b/drivers/iio/light/tsl2563.c
index 1f529f36f138..ebb962c5c323 100644
--- a/drivers/iio/light/tsl2563.c
+++ b/drivers/iio/light/tsl2563.c
@@ -702,7 +702,7 @@ static int tsl2563_probe(struct i2c_client *client,
702 int err = 0; 702 int err = 0;
703 u8 id = 0; 703 u8 id = 0;
704 704
705 indio_dev = iio_device_alloc(sizeof(*chip)); 705 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
706 if (!indio_dev) 706 if (!indio_dev)
707 return -ENOMEM; 707 return -ENOMEM;
708 708
@@ -714,13 +714,13 @@ static int tsl2563_probe(struct i2c_client *client,
714 err = tsl2563_detect(chip); 714 err = tsl2563_detect(chip);
715 if (err) { 715 if (err) {
716 dev_err(&client->dev, "detect error %d\n", -err); 716 dev_err(&client->dev, "detect error %d\n", -err);
717 goto fail1; 717 return err;
718 } 718 }
719 719
720 err = tsl2563_read_id(chip, &id); 720 err = tsl2563_read_id(chip, &id);
721 if (err) { 721 if (err) {
722 dev_err(&client->dev, "read id error %d\n", -err); 722 dev_err(&client->dev, "read id error %d\n", -err);
723 goto fail1; 723 return err;
724 } 724 }
725 725
726 mutex_init(&chip->lock); 726 mutex_init(&chip->lock);
@@ -751,7 +751,7 @@ static int tsl2563_probe(struct i2c_client *client,
751 indio_dev->info = &tsl2563_info_no_irq; 751 indio_dev->info = &tsl2563_info_no_irq;
752 752
753 if (client->irq) { 753 if (client->irq) {
754 err = request_threaded_irq(client->irq, 754 err = devm_request_threaded_irq(&client->dev, client->irq,
755 NULL, 755 NULL,
756 &tsl2563_event_handler, 756 &tsl2563_event_handler,
757 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 757 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
@@ -759,14 +759,14 @@ static int tsl2563_probe(struct i2c_client *client,
759 indio_dev); 759 indio_dev);
760 if (err) { 760 if (err) {
761 dev_err(&client->dev, "irq request error %d\n", -err); 761 dev_err(&client->dev, "irq request error %d\n", -err);
762 goto fail1; 762 return err;
763 } 763 }
764 } 764 }
765 765
766 err = tsl2563_configure(chip); 766 err = tsl2563_configure(chip);
767 if (err) { 767 if (err) {
768 dev_err(&client->dev, "configure error %d\n", -err); 768 dev_err(&client->dev, "configure error %d\n", -err);
769 goto fail2; 769 return err;
770 } 770 }
771 771
772 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work); 772 INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
@@ -777,19 +777,14 @@ static int tsl2563_probe(struct i2c_client *client,
777 err = iio_device_register(indio_dev); 777 err = iio_device_register(indio_dev);
778 if (err) { 778 if (err) {
779 dev_err(&client->dev, "iio registration error %d\n", -err); 779 dev_err(&client->dev, "iio registration error %d\n", -err);
780 goto fail3; 780 goto fail;
781 } 781 }
782 782
783 return 0; 783 return 0;
784 784
785fail3: 785fail:
786 cancel_delayed_work(&chip->poweroff_work); 786 cancel_delayed_work(&chip->poweroff_work);
787 flush_scheduled_work(); 787 flush_scheduled_work();
788fail2:
789 if (client->irq)
790 free_irq(client->irq, indio_dev);
791fail1:
792 iio_device_free(indio_dev);
793 return err; 788 return err;
794} 789}
795 790
@@ -807,10 +802,6 @@ static int tsl2563_remove(struct i2c_client *client)
807 chip->intr); 802 chip->intr);
808 flush_scheduled_work(); 803 flush_scheduled_work();
809 tsl2563_set_power(chip, 0); 804 tsl2563_set_power(chip, 0);
810 if (client->irq)
811 free_irq(client->irq, indio_dev);
812
813 iio_device_free(indio_dev);
814 805
815 return 0; 806 return 0;
816} 807}
diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 1014943d949a..2bb304215b1d 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -157,7 +157,7 @@ static int vcnl4000_probe(struct i2c_client *client,
157 struct iio_dev *indio_dev; 157 struct iio_dev *indio_dev;
158 int ret; 158 int ret;
159 159
160 indio_dev = iio_device_alloc(sizeof(*data)); 160 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
161 if (!indio_dev) 161 if (!indio_dev)
162 return -ENOMEM; 162 return -ENOMEM;
163 163
@@ -167,7 +167,7 @@ static int vcnl4000_probe(struct i2c_client *client,
167 167
168 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV); 168 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV);
169 if (ret < 0) 169 if (ret < 0)
170 goto error_free_dev; 170 return ret;
171 171
172 dev_info(&client->dev, "VCNL4000 Ambient light/proximity sensor, Prod %02x, Rev: %02x\n", 172 dev_info(&client->dev, "VCNL4000 Ambient light/proximity sensor, Prod %02x, Rev: %02x\n",
173 ret >> 4, ret & 0xf); 173 ret >> 4, ret & 0xf);
@@ -181,22 +181,14 @@ static int vcnl4000_probe(struct i2c_client *client,
181 181
182 ret = iio_device_register(indio_dev); 182 ret = iio_device_register(indio_dev);
183 if (ret < 0) 183 if (ret < 0)
184 goto error_free_dev; 184 return ret;
185 185
186 return 0; 186 return 0;
187
188error_free_dev:
189 iio_device_free(indio_dev);
190 return ret;
191} 187}
192 188
193static int vcnl4000_remove(struct i2c_client *client) 189static int vcnl4000_remove(struct i2c_client *client)
194{ 190{
195 struct iio_dev *indio_dev = i2c_get_clientdata(client); 191 iio_device_unregister(i2c_get_clientdata(client));
196
197 iio_device_unregister(indio_dev);
198 iio_device_free(indio_dev);
199
200 return 0; 192 return 0;
201} 193}
202 194
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig
index c332b0ae4a3b..4fa923f37b97 100644
--- a/drivers/iio/magnetometer/Kconfig
+++ b/drivers/iio/magnetometer/Kconfig
@@ -1,6 +1,8 @@
1# 1#
2# Magnetometer sensors 2# Magnetometer sensors
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Magnetometer sensors" 6menu "Magnetometer sensors"
5 7
6config AK8975 8config AK8975
@@ -36,8 +38,8 @@ config IIO_ST_MAGN_3AXIS
36 Say yes here to build support for STMicroelectronics magnetometers: 38 Say yes here to build support for STMicroelectronics magnetometers:
37 LSM303DLHC, LSM303DLM, LIS3MDL. 39 LSM303DLHC, LSM303DLM, LIS3MDL.
38 40
39 This driver can also be built as a module. If so, will be created 41 This driver can also be built as a module. If so, these modules
40 these modules: 42 will be created:
41 - st_magn (core functions for the driver [it is mandatory]); 43 - st_magn (core functions for the driver [it is mandatory]);
42 - st_magn_i2c (necessary for the I2C devices [optional*]); 44 - st_magn_i2c (necessary for the I2C devices [optional*]);
43 - st_magn_spi (necessary for the SPI devices [optional*]); 45 - st_magn_spi (necessary for the SPI devices [optional*]);
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile
index 7f328e37fbab..f91b1b68d392 100644
--- a/drivers/iio/magnetometer/Makefile
+++ b/drivers/iio/magnetometer/Makefile
@@ -2,6 +2,7 @@
2# Makefile for industrial I/O Magnetometer sensor drivers 2# Makefile for industrial I/O Magnetometer sensor drivers
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_AK8975) += ak8975.o 6obj-$(CONFIG_AK8975) += ak8975.o
6obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o 7obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o
7 8
diff --git a/drivers/iio/magnetometer/hid-sensor-magn-3d.c b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
index 99f4e494513b..a98460b15e4b 100644
--- a/drivers/iio/magnetometer/hid-sensor-magn-3d.c
+++ b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
@@ -30,10 +30,6 @@
30#include <linux/iio/triggered_buffer.h> 30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-trigger.h" 31#include "../common/hid-sensors/hid-sensor-trigger.h"
32 32
33/*Format: HID-SENSOR-usage_id_in_hex*/
34/*Usage ID from spec for Magnetometer-3D: 0x200083*/
35#define DRIVER_NAME "HID-SENSOR-200083"
36
37enum magn_3d_channel { 33enum magn_3d_channel {
38 CHANNEL_SCAN_INDEX_X, 34 CHANNEL_SCAN_INDEX_X,
39 CHANNEL_SCAN_INDEX_Y, 35 CHANNEL_SCAN_INDEX_Y,
@@ -180,18 +176,10 @@ static int magn_3d_write_raw(struct iio_dev *indio_dev,
180 return ret; 176 return ret;
181} 177}
182 178
183static int magn_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
184 struct iio_chan_spec const *chan,
185 long mask)
186{
187 return IIO_VAL_INT_PLUS_MICRO;
188}
189
190static const struct iio_info magn_3d_info = { 179static const struct iio_info magn_3d_info = {
191 .driver_module = THIS_MODULE, 180 .driver_module = THIS_MODULE,
192 .read_raw = &magn_3d_read_raw, 181 .read_raw = &magn_3d_read_raw,
193 .write_raw = &magn_3d_write_raw, 182 .write_raw = &magn_3d_write_raw,
194 .write_raw_get_fmt = &magn_3d_write_raw_get_fmt,
195}; 183};
196 184
197/* Function to push data to buffer */ 185/* Function to push data to buffer */
@@ -287,11 +275,11 @@ static int hid_magn_3d_probe(struct platform_device *pdev)
287 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 275 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
288 struct iio_chan_spec *channels; 276 struct iio_chan_spec *channels;
289 277
290 indio_dev = iio_device_alloc(sizeof(struct magn_3d_state)); 278 indio_dev = devm_iio_device_alloc(&pdev->dev,
291 if (indio_dev == NULL) { 279 sizeof(struct magn_3d_state));
292 ret = -ENOMEM; 280 if (indio_dev == NULL)
293 goto error_ret; 281 return -ENOMEM;
294 } 282
295 platform_set_drvdata(pdev, indio_dev); 283 platform_set_drvdata(pdev, indio_dev);
296 284
297 magn_state = iio_priv(indio_dev); 285 magn_state = iio_priv(indio_dev);
@@ -303,15 +291,14 @@ static int hid_magn_3d_probe(struct platform_device *pdev)
303 &magn_state->common_attributes); 291 &magn_state->common_attributes);
304 if (ret) { 292 if (ret) {
305 dev_err(&pdev->dev, "failed to setup common attributes\n"); 293 dev_err(&pdev->dev, "failed to setup common attributes\n");
306 goto error_free_dev; 294 return ret;
307 } 295 }
308 296
309 channels = kmemdup(magn_3d_channels, sizeof(magn_3d_channels), 297 channels = kmemdup(magn_3d_channels, sizeof(magn_3d_channels),
310 GFP_KERNEL); 298 GFP_KERNEL);
311 if (!channels) { 299 if (!channels) {
312 ret = -ENOMEM;
313 dev_err(&pdev->dev, "failed to duplicate channels\n"); 300 dev_err(&pdev->dev, "failed to duplicate channels\n");
314 goto error_free_dev; 301 return -ENOMEM;
315 } 302 }
316 303
317 ret = magn_3d_parse_report(pdev, hsdev, channels, 304 ret = magn_3d_parse_report(pdev, hsdev, channels,
@@ -368,9 +355,6 @@ error_unreg_buffer_funcs:
368 iio_triggered_buffer_cleanup(indio_dev); 355 iio_triggered_buffer_cleanup(indio_dev);
369error_free_dev_mem: 356error_free_dev_mem:
370 kfree(indio_dev->channels); 357 kfree(indio_dev->channels);
371error_free_dev:
372 iio_device_free(indio_dev);
373error_ret:
374 return ret; 358 return ret;
375} 359}
376 360
@@ -385,14 +369,23 @@ static int hid_magn_3d_remove(struct platform_device *pdev)
385 hid_sensor_remove_trigger(indio_dev); 369 hid_sensor_remove_trigger(indio_dev);
386 iio_triggered_buffer_cleanup(indio_dev); 370 iio_triggered_buffer_cleanup(indio_dev);
387 kfree(indio_dev->channels); 371 kfree(indio_dev->channels);
388 iio_device_free(indio_dev);
389 372
390 return 0; 373 return 0;
391} 374}
392 375
376static struct platform_device_id hid_magn_3d_ids[] = {
377 {
378 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
379 .name = "HID-SENSOR-200083",
380 },
381 { /* sentinel */ }
382};
383MODULE_DEVICE_TABLE(platform, hid_magn_3d_ids);
384
393static struct platform_driver hid_magn_3d_platform_driver = { 385static struct platform_driver hid_magn_3d_platform_driver = {
386 .id_table = hid_magn_3d_ids,
394 .driver = { 387 .driver = {
395 .name = DRIVER_NAME, 388 .name = KBUILD_MODNAME,
396 .owner = THIS_MODULE, 389 .owner = THIS_MODULE,
397 }, 390 },
398 .probe = hid_magn_3d_probe, 391 .probe = hid_magn_3d_probe,
diff --git a/drivers/iio/magnetometer/st_magn.h b/drivers/iio/magnetometer/st_magn.h
index 7e81d00ef0c3..694e33e0fb72 100644
--- a/drivers/iio/magnetometer/st_magn.h
+++ b/drivers/iio/magnetometer/st_magn.h
@@ -18,7 +18,8 @@
18#define LSM303DLM_MAGN_DEV_NAME "lsm303dlm_magn" 18#define LSM303DLM_MAGN_DEV_NAME "lsm303dlm_magn"
19#define LIS3MDL_MAGN_DEV_NAME "lis3mdl" 19#define LIS3MDL_MAGN_DEV_NAME "lis3mdl"
20 20
21int st_magn_common_probe(struct iio_dev *indio_dev); 21int st_magn_common_probe(struct iio_dev *indio_dev,
22 struct st_sensors_platform_data *pdata);
22void st_magn_common_remove(struct iio_dev *indio_dev); 23void st_magn_common_remove(struct iio_dev *indio_dev);
23 24
24#ifdef CONFIG_IIO_BUFFER 25#ifdef CONFIG_IIO_BUFFER
diff --git a/drivers/iio/magnetometer/st_magn_core.c b/drivers/iio/magnetometer/st_magn_core.c
index ebfe8f11a0c2..e8d2849cc81d 100644
--- a/drivers/iio/magnetometer/st_magn_core.c
+++ b/drivers/iio/magnetometer/st_magn_core.c
@@ -345,7 +345,8 @@ static const struct iio_info magn_info = {
345 .write_raw = &st_magn_write_raw, 345 .write_raw = &st_magn_write_raw,
346}; 346};
347 347
348int st_magn_common_probe(struct iio_dev *indio_dev) 348int st_magn_common_probe(struct iio_dev *indio_dev,
349 struct st_sensors_platform_data *pdata)
349{ 350{
350 int err; 351 int err;
351 struct st_sensor_data *mdata = iio_priv(indio_dev); 352 struct st_sensor_data *mdata = iio_priv(indio_dev);
@@ -367,7 +368,7 @@ int st_magn_common_probe(struct iio_dev *indio_dev)
367 &mdata->sensor->fs.fs_avl[0]; 368 &mdata->sensor->fs.fs_avl[0];
368 mdata->odr = mdata->sensor->odr.odr_avl[0].hz; 369 mdata->odr = mdata->sensor->odr.odr_avl[0].hz;
369 370
370 err = st_sensors_init_sensor(indio_dev); 371 err = st_sensors_init_sensor(indio_dev, pdata);
371 if (err < 0) 372 if (err < 0)
372 goto st_magn_common_probe_error; 373 goto st_magn_common_probe_error;
373 374
@@ -406,7 +407,6 @@ void st_magn_common_remove(struct iio_dev *indio_dev)
406 st_sensors_deallocate_trigger(indio_dev); 407 st_sensors_deallocate_trigger(indio_dev);
407 st_magn_deallocate_ring(indio_dev); 408 st_magn_deallocate_ring(indio_dev);
408 } 409 }
409 iio_device_free(indio_dev);
410} 410}
411EXPORT_SYMBOL(st_magn_common_remove); 411EXPORT_SYMBOL(st_magn_common_remove);
412 412
diff --git a/drivers/iio/magnetometer/st_magn_i2c.c b/drivers/iio/magnetometer/st_magn_i2c.c
index e6adc4a86425..892e0feeb5c1 100644
--- a/drivers/iio/magnetometer/st_magn_i2c.c
+++ b/drivers/iio/magnetometer/st_magn_i2c.c
@@ -25,27 +25,20 @@ static int st_magn_i2c_probe(struct i2c_client *client,
25 struct st_sensor_data *mdata; 25 struct st_sensor_data *mdata;
26 int err; 26 int err;
27 27
28 indio_dev = iio_device_alloc(sizeof(*mdata)); 28 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mdata));
29 if (indio_dev == NULL) { 29 if (!indio_dev)
30 err = -ENOMEM; 30 return -ENOMEM;
31 goto iio_device_alloc_error;
32 }
33 31
34 mdata = iio_priv(indio_dev); 32 mdata = iio_priv(indio_dev);
35 mdata->dev = &client->dev; 33 mdata->dev = &client->dev;
36 34
37 st_sensors_i2c_configure(indio_dev, client, mdata); 35 st_sensors_i2c_configure(indio_dev, client, mdata);
38 36
39 err = st_magn_common_probe(indio_dev); 37 err = st_magn_common_probe(indio_dev, NULL);
40 if (err < 0) 38 if (err < 0)
41 goto st_magn_common_probe_error; 39 return err;
42 40
43 return 0; 41 return 0;
44
45st_magn_common_probe_error:
46 iio_device_free(indio_dev);
47iio_device_alloc_error:
48 return err;
49} 42}
50 43
51static int st_magn_i2c_remove(struct i2c_client *client) 44static int st_magn_i2c_remove(struct i2c_client *client)
diff --git a/drivers/iio/magnetometer/st_magn_spi.c b/drivers/iio/magnetometer/st_magn_spi.c
index 51adb797cb7d..a6143ea51dfc 100644
--- a/drivers/iio/magnetometer/st_magn_spi.c
+++ b/drivers/iio/magnetometer/st_magn_spi.c
@@ -24,27 +24,20 @@ static int st_magn_spi_probe(struct spi_device *spi)
24 struct st_sensor_data *mdata; 24 struct st_sensor_data *mdata;
25 int err; 25 int err;
26 26
27 indio_dev = iio_device_alloc(sizeof(*mdata)); 27 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
28 if (indio_dev == NULL) { 28 if (!indio_dev)
29 err = -ENOMEM; 29 return -ENOMEM;
30 goto iio_device_alloc_error;
31 }
32 30
33 mdata = iio_priv(indio_dev); 31 mdata = iio_priv(indio_dev);
34 mdata->dev = &spi->dev; 32 mdata->dev = &spi->dev;
35 33
36 st_sensors_spi_configure(indio_dev, spi, mdata); 34 st_sensors_spi_configure(indio_dev, spi, mdata);
37 35
38 err = st_magn_common_probe(indio_dev); 36 err = st_magn_common_probe(indio_dev, NULL);
39 if (err < 0) 37 if (err < 0)
40 goto st_magn_common_probe_error; 38 return err;
41 39
42 return 0; 40 return 0;
43
44st_magn_common_probe_error:
45 iio_device_free(indio_dev);
46iio_device_alloc_error:
47 return err;
48} 41}
49 42
50static int st_magn_spi_remove(struct spi_device *spi) 43static int st_magn_spi_remove(struct spi_device *spi)
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index 9427f01e1499..26fdc0bdb99d 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -1,21 +1,23 @@
1# 1#
2# Pressure drivers 2# Pressure drivers
3# 3#
4menu "Pressure Sensors" 4# When adding new entries keep the list in alphabetical order
5
6menu "Pressure sensors"
5 7
6config IIO_ST_PRESS 8config IIO_ST_PRESS
7 tristate "STMicroelectronics pressures Driver" 9 tristate "STMicroelectronics pressure sensor Driver"
8 depends on (I2C || SPI_MASTER) && SYSFS 10 depends on (I2C || SPI_MASTER) && SYSFS
9 select IIO_ST_SENSORS_CORE 11 select IIO_ST_SENSORS_CORE
10 select IIO_ST_PRESS_I2C if (I2C) 12 select IIO_ST_PRESS_I2C if (I2C)
11 select IIO_ST_PRESS_SPI if (SPI_MASTER) 13 select IIO_ST_PRESS_SPI if (SPI_MASTER)
12 select IIO_TRIGGERED_BUFFER if (IIO_BUFFER) 14 select IIO_TRIGGERED_BUFFER if (IIO_BUFFER)
13 help 15 help
14 Say yes here to build support for STMicroelectronics pressures: 16 Say yes here to build support for STMicroelectronics pressure
15 LPS331AP. 17 sensors: LPS331AP.
16 18
17 This driver can also be built as a module. If so, will be created 19 This driver can also be built as a module. If so, these modules
18 these modules: 20 will be created:
19 - st_pressure (core functions for the driver [it is mandatory]); 21 - st_pressure (core functions for the driver [it is mandatory]);
20 - st_pressure_i2c (necessary for the I2C devices [optional*]); 22 - st_pressure_i2c (necessary for the I2C devices [optional*]);
21 - st_pressure_spi (necessary for the SPI devices [optional*]); 23 - st_pressure_spi (necessary for the SPI devices [optional*]);
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile
index d4bb33e5c846..be71464c2752 100644
--- a/drivers/iio/pressure/Makefile
+++ b/drivers/iio/pressure/Makefile
@@ -2,6 +2,7 @@
2# Makefile for industrial I/O pressure drivers 2# Makefile for industrial I/O pressure drivers
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o 6obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o
6st_pressure-y := st_pressure_core.o 7st_pressure-y := st_pressure_core.o
7st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o 8st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o
diff --git a/drivers/iio/pressure/st_pressure.h b/drivers/iio/pressure/st_pressure.h
index 414e45ac9b9b..b0b630688da6 100644
--- a/drivers/iio/pressure/st_pressure.h
+++ b/drivers/iio/pressure/st_pressure.h
@@ -16,7 +16,16 @@
16 16
17#define LPS331AP_PRESS_DEV_NAME "lps331ap" 17#define LPS331AP_PRESS_DEV_NAME "lps331ap"
18 18
19int st_press_common_probe(struct iio_dev *indio_dev); 19/**
20 * struct st_sensors_platform_data - default press platform data
21 * @drdy_int_pin: default press DRDY is available on INT1 pin.
22 */
23static const struct st_sensors_platform_data default_press_pdata = {
24 .drdy_int_pin = 1,
25};
26
27int st_press_common_probe(struct iio_dev *indio_dev,
28 struct st_sensors_platform_data *pdata);
20void st_press_common_remove(struct iio_dev *indio_dev); 29void st_press_common_remove(struct iio_dev *indio_dev);
21 30
22#ifdef CONFIG_IIO_BUFFER 31#ifdef CONFIG_IIO_BUFFER
diff --git a/drivers/iio/pressure/st_pressure_core.c b/drivers/iio/pressure/st_pressure_core.c
index 3ffbc56917b4..ceebd3c27892 100644
--- a/drivers/iio/pressure/st_pressure_core.c
+++ b/drivers/iio/pressure/st_pressure_core.c
@@ -31,6 +31,9 @@
31#define ST_PRESS_LSB_PER_MBAR 4096UL 31#define ST_PRESS_LSB_PER_MBAR 4096UL
32#define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \ 32#define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
33 ST_PRESS_LSB_PER_MBAR) 33 ST_PRESS_LSB_PER_MBAR)
34#define ST_PRESS_LSB_PER_CELSIUS 480UL
35#define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \
36 ST_PRESS_LSB_PER_CELSIUS)
34#define ST_PRESS_NUMBER_DATA_CHANNELS 1 37#define ST_PRESS_NUMBER_DATA_CHANNELS 1
35 38
36/* DEFAULT VALUE FOR SENSORS */ 39/* DEFAULT VALUE FOR SENSORS */
@@ -53,12 +56,13 @@
53#define ST_PRESS_1_FS_ADDR 0x23 56#define ST_PRESS_1_FS_ADDR 0x23
54#define ST_PRESS_1_FS_MASK 0x30 57#define ST_PRESS_1_FS_MASK 0x30
55#define ST_PRESS_1_FS_AVL_1260_VAL 0x00 58#define ST_PRESS_1_FS_AVL_1260_VAL 0x00
56#define ST_PRESS_1_FS_AVL_TEMP_GAIN 2083000
57#define ST_PRESS_1_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE 59#define ST_PRESS_1_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE
60#define ST_PRESS_1_FS_AVL_TEMP_GAIN ST_PRESS_CELSIUS_NANO_SCALE
58#define ST_PRESS_1_BDU_ADDR 0x20 61#define ST_PRESS_1_BDU_ADDR 0x20
59#define ST_PRESS_1_BDU_MASK 0x04 62#define ST_PRESS_1_BDU_MASK 0x04
60#define ST_PRESS_1_DRDY_IRQ_ADDR 0x22 63#define ST_PRESS_1_DRDY_IRQ_ADDR 0x22
61#define ST_PRESS_1_DRDY_IRQ_MASK 0x04 64#define ST_PRESS_1_DRDY_IRQ_INT1_MASK 0x04
65#define ST_PRESS_1_DRDY_IRQ_INT2_MASK 0x20
62#define ST_PRESS_1_MULTIREAD_BIT true 66#define ST_PRESS_1_MULTIREAD_BIT true
63#define ST_PRESS_1_TEMP_OFFSET 42500 67#define ST_PRESS_1_TEMP_OFFSET 42500
64 68
@@ -116,7 +120,8 @@ static const struct st_sensors st_press_sensors[] = {
116 }, 120 },
117 .drdy_irq = { 121 .drdy_irq = {
118 .addr = ST_PRESS_1_DRDY_IRQ_ADDR, 122 .addr = ST_PRESS_1_DRDY_IRQ_ADDR,
119 .mask = ST_PRESS_1_DRDY_IRQ_MASK, 123 .mask_int1 = ST_PRESS_1_DRDY_IRQ_INT1_MASK,
124 .mask_int2 = ST_PRESS_1_DRDY_IRQ_INT2_MASK,
120 }, 125 },
121 .multi_read_bit = ST_PRESS_1_MULTIREAD_BIT, 126 .multi_read_bit = ST_PRESS_1_MULTIREAD_BIT,
122 .bootime = 2, 127 .bootime = 2,
@@ -202,7 +207,8 @@ static const struct iio_trigger_ops st_press_trigger_ops = {
202#define ST_PRESS_TRIGGER_OPS NULL 207#define ST_PRESS_TRIGGER_OPS NULL
203#endif 208#endif
204 209
205int st_press_common_probe(struct iio_dev *indio_dev) 210int st_press_common_probe(struct iio_dev *indio_dev,
211 struct st_sensors_platform_data *plat_data)
206{ 212{
207 int err; 213 int err;
208 struct st_sensor_data *pdata = iio_priv(indio_dev); 214 struct st_sensor_data *pdata = iio_priv(indio_dev);
@@ -224,7 +230,11 @@ int st_press_common_probe(struct iio_dev *indio_dev)
224 &pdata->sensor->fs.fs_avl[0]; 230 &pdata->sensor->fs.fs_avl[0];
225 pdata->odr = pdata->sensor->odr.odr_avl[0].hz; 231 pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
226 232
227 err = st_sensors_init_sensor(indio_dev); 233 if (!plat_data)
234 plat_data =
235 (struct st_sensors_platform_data *)&default_press_pdata;
236
237 err = st_sensors_init_sensor(indio_dev, plat_data);
228 if (err < 0) 238 if (err < 0)
229 goto st_press_common_probe_error; 239 goto st_press_common_probe_error;
230 240
@@ -265,7 +275,6 @@ void st_press_common_remove(struct iio_dev *indio_dev)
265 st_sensors_deallocate_trigger(indio_dev); 275 st_sensors_deallocate_trigger(indio_dev);
266 st_press_deallocate_ring(indio_dev); 276 st_press_deallocate_ring(indio_dev);
267 } 277 }
268 iio_device_free(indio_dev);
269} 278}
270EXPORT_SYMBOL(st_press_common_remove); 279EXPORT_SYMBOL(st_press_common_remove);
271 280
diff --git a/drivers/iio/pressure/st_pressure_i2c.c b/drivers/iio/pressure/st_pressure_i2c.c
index 7cebcc73bfb0..08aac5e6251d 100644
--- a/drivers/iio/pressure/st_pressure_i2c.c
+++ b/drivers/iio/pressure/st_pressure_i2c.c
@@ -25,27 +25,20 @@ static int st_press_i2c_probe(struct i2c_client *client,
25 struct st_sensor_data *pdata; 25 struct st_sensor_data *pdata;
26 int err; 26 int err;
27 27
28 indio_dev = iio_device_alloc(sizeof(*pdata)); 28 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*pdata));
29 if (indio_dev == NULL) { 29 if (!indio_dev)
30 err = -ENOMEM; 30 return -ENOMEM;
31 goto iio_device_alloc_error;
32 }
33 31
34 pdata = iio_priv(indio_dev); 32 pdata = iio_priv(indio_dev);
35 pdata->dev = &client->dev; 33 pdata->dev = &client->dev;
36 34
37 st_sensors_i2c_configure(indio_dev, client, pdata); 35 st_sensors_i2c_configure(indio_dev, client, pdata);
38 36
39 err = st_press_common_probe(indio_dev); 37 err = st_press_common_probe(indio_dev, client->dev.platform_data);
40 if (err < 0) 38 if (err < 0)
41 goto st_press_common_probe_error; 39 return err;
42 40
43 return 0; 41 return 0;
44
45st_press_common_probe_error:
46 iio_device_free(indio_dev);
47iio_device_alloc_error:
48 return err;
49} 42}
50 43
51static int st_press_i2c_remove(struct i2c_client *client) 44static int st_press_i2c_remove(struct i2c_client *client)
diff --git a/drivers/iio/pressure/st_pressure_spi.c b/drivers/iio/pressure/st_pressure_spi.c
index 17a14907940a..399a29b6017b 100644
--- a/drivers/iio/pressure/st_pressure_spi.c
+++ b/drivers/iio/pressure/st_pressure_spi.c
@@ -24,27 +24,20 @@ static int st_press_spi_probe(struct spi_device *spi)
24 struct st_sensor_data *pdata; 24 struct st_sensor_data *pdata;
25 int err; 25 int err;
26 26
27 indio_dev = iio_device_alloc(sizeof(*pdata)); 27 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*pdata));
28 if (indio_dev == NULL) { 28 if (indio_dev == NULL)
29 err = -ENOMEM; 29 return -ENOMEM;
30 goto iio_device_alloc_error;
31 }
32 30
33 pdata = iio_priv(indio_dev); 31 pdata = iio_priv(indio_dev);
34 pdata->dev = &spi->dev; 32 pdata->dev = &spi->dev;
35 33
36 st_sensors_spi_configure(indio_dev, spi, pdata); 34 st_sensors_spi_configure(indio_dev, spi, pdata);
37 35
38 err = st_press_common_probe(indio_dev); 36 err = st_press_common_probe(indio_dev, spi->dev.platform_data);
39 if (err < 0) 37 if (err < 0)
40 goto st_press_common_probe_error; 38 return err;
41 39
42 return 0; 40 return 0;
43
44st_press_common_probe_error:
45 iio_device_free(indio_dev);
46iio_device_alloc_error:
47 return err;
48} 41}
49 42
50static int st_press_spi_remove(struct spi_device *spi) 43static int st_press_spi_remove(struct spi_device *spi)
diff --git a/drivers/iio/temperature/Kconfig b/drivers/iio/temperature/Kconfig
new file mode 100644
index 000000000000..372f8fb3085f
--- /dev/null
+++ b/drivers/iio/temperature/Kconfig
@@ -0,0 +1,16 @@
1#
2# Temperature sensor drivers
3#
4menu "Temperature sensors"
5
6config TMP006
7 tristate "TMP006 infrared thermopile sensor"
8 depends on I2C
9 help
10 If you say yes here you get support for the Texas Instruments
11 TMP006 infrared thermopile sensor.
12
13 This driver can also be built as a module. If so, the module will
14 be called tmp006.
15
16endmenu
diff --git a/drivers/iio/temperature/Makefile b/drivers/iio/temperature/Makefile
new file mode 100644
index 000000000000..24d7b602db3e
--- /dev/null
+++ b/drivers/iio/temperature/Makefile
@@ -0,0 +1,5 @@
1#
2# Makefile for industrial I/O temperature drivers
3#
4
5obj-$(CONFIG_TMP006) += tmp006.o
diff --git a/drivers/iio/temperature/tmp006.c b/drivers/iio/temperature/tmp006.c
new file mode 100644
index 000000000000..64ccde3f1f7a
--- /dev/null
+++ b/drivers/iio/temperature/tmp006.c
@@ -0,0 +1,291 @@
1/*
2 * tmp006.c - Support for TI TMP006 IR thermopile sensor
3 *
4 * Copyright (c) 2013 Peter Meerwald <pmeerw@pmeerw.net>
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * Driver for the Texas Instruments I2C 16-bit IR thermopile sensor
11 *
12 * (7-bit I2C slave address 0x40, changeable via ADR pins)
13 *
14 * TODO: data ready irq
15 */
16
17#include <linux/err.h>
18#include <linux/i2c.h>
19#include <linux/delay.h>
20#include <linux/module.h>
21#include <linux/pm.h>
22#include <linux/bitops.h>
23
24#include <linux/iio/iio.h>
25#include <linux/iio/sysfs.h>
26
27#define TMP006_VOBJECT 0x00
28#define TMP006_TAMBIENT 0x01
29#define TMP006_CONFIG 0x02
30#define TMP006_MANUFACTURER_ID 0xfe
31#define TMP006_DEVICE_ID 0xff
32
33#define TMP006_TAMBIENT_SHIFT 2
34
35#define TMP006_CONFIG_RESET BIT(15)
36#define TMP006_CONFIG_DRDY_EN BIT(8)
37#define TMP006_CONFIG_DRDY BIT(7)
38
39#define TMP006_CONFIG_MOD_MASK 0x7000
40
41#define TMP006_CONFIG_CR_MASK 0x0e00
42#define TMP006_CONFIG_CR_SHIFT 9
43
44#define MANUFACTURER_MAGIC 0x5449
45#define DEVICE_MAGIC 0x0067
46
47struct tmp006_data {
48 struct i2c_client *client;
49 u16 config;
50};
51
52static int tmp006_read_measurement(struct tmp006_data *data, u8 reg)
53{
54 s32 ret;
55 int tries = 50;
56
57 while (tries-- > 0) {
58 ret = i2c_smbus_read_word_swapped(data->client,
59 TMP006_CONFIG);
60 if (ret < 0)
61 return ret;
62 if (ret & TMP006_CONFIG_DRDY)
63 break;
64 msleep(100);
65 }
66
67 if (tries < 0)
68 return -EIO;
69
70 return i2c_smbus_read_word_swapped(data->client, reg);
71}
72
73static int tmp006_read_raw(struct iio_dev *indio_dev,
74 struct iio_chan_spec const *channel, int *val,
75 int *val2, long mask)
76{
77 struct tmp006_data *data = iio_priv(indio_dev);
78 s32 ret;
79
80 switch (mask) {
81 case IIO_CHAN_INFO_RAW:
82 if (channel->type == IIO_VOLTAGE) {
83 /* LSB is 156.25 nV */
84 ret = tmp006_read_measurement(data, TMP006_VOBJECT);
85 if (ret < 0)
86 return ret;
87 *val = sign_extend32(ret, 15);
88 } else if (channel->type == IIO_TEMP) {
89 /* LSB is 0.03125 degrees Celsius */
90 ret = tmp006_read_measurement(data, TMP006_TAMBIENT);
91 if (ret < 0)
92 return ret;
93 *val = sign_extend32(ret, 15) >> TMP006_TAMBIENT_SHIFT;
94 } else {
95 break;
96 }
97 return IIO_VAL_INT;
98 case IIO_CHAN_INFO_SCALE:
99 if (channel->type == IIO_VOLTAGE) {
100 *val = 0;
101 *val2 = 156250;
102 } else if (channel->type == IIO_TEMP) {
103 *val = 31;
104 *val2 = 250000;
105 } else {
106 break;
107 }
108 return IIO_VAL_INT_PLUS_MICRO;
109 default:
110 break;
111 }
112
113 return -EINVAL;
114}
115
116static const char * const tmp006_freqs[] = { "4", "2", "1", "0.5", "0.25" };
117
118static ssize_t tmp006_show_freq(struct device *dev,
119 struct device_attribute *attr, char *buf)
120{
121 struct tmp006_data *data = iio_priv(dev_to_iio_dev(dev));
122 int cr = (data->config & TMP006_CONFIG_CR_MASK)
123 >> TMP006_CONFIG_CR_SHIFT;
124 return sprintf(buf, "%s\n", tmp006_freqs[cr]);
125}
126
127static ssize_t tmp006_store_freq(struct device *dev,
128 struct device_attribute *attr,
129 const char *buf, size_t len)
130{
131 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
132 struct tmp006_data *data = iio_priv(indio_dev);
133 int i;
134 bool found = false;
135
136 for (i = 0; i < ARRAY_SIZE(tmp006_freqs); i++)
137 if (sysfs_streq(buf, tmp006_freqs[i])) {
138 found = true;
139 break;
140 }
141 if (!found)
142 return -EINVAL;
143
144 data->config &= ~TMP006_CONFIG_CR_MASK;
145 data->config |= i << TMP006_CONFIG_CR_SHIFT;
146
147 return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG,
148 data->config);
149}
150
151static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
152 tmp006_show_freq, tmp006_store_freq);
153
154static IIO_CONST_ATTR(sampling_frequency_available, "4 2 1 0.5 0.25");
155
156static struct attribute *tmp006_attributes[] = {
157 &iio_dev_attr_sampling_frequency.dev_attr.attr,
158 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
159 NULL
160};
161
162static const struct attribute_group tmp006_attribute_group = {
163 .attrs = tmp006_attributes,
164};
165
166static const struct iio_chan_spec tmp006_channels[] = {
167 {
168 .type = IIO_VOLTAGE,
169 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
170 BIT(IIO_CHAN_INFO_SCALE),
171 },
172 {
173 .type = IIO_TEMP,
174 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
175 BIT(IIO_CHAN_INFO_SCALE),
176 }
177};
178
179static const struct iio_info tmp006_info = {
180 .read_raw = tmp006_read_raw,
181 .attrs = &tmp006_attribute_group,
182 .driver_module = THIS_MODULE,
183};
184
185static bool tmp006_check_identification(struct i2c_client *client)
186{
187 int mid, did;
188
189 mid = i2c_smbus_read_word_swapped(client, TMP006_MANUFACTURER_ID);
190 if (mid < 0)
191 return false;
192
193 did = i2c_smbus_read_word_swapped(client, TMP006_DEVICE_ID);
194 if (did < 0)
195 return false;
196
197 return mid == MANUFACTURER_MAGIC && did == DEVICE_MAGIC;
198}
199
200static int tmp006_probe(struct i2c_client *client,
201 const struct i2c_device_id *id)
202{
203 struct iio_dev *indio_dev;
204 struct tmp006_data *data;
205 int ret;
206
207 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
208 return -ENODEV;
209
210 if (!tmp006_check_identification(client)) {
211 dev_err(&client->dev, "no TMP006 sensor\n");
212 return -ENODEV;
213 }
214
215 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
216 if (!indio_dev)
217 return -ENOMEM;
218
219 data = iio_priv(indio_dev);
220 i2c_set_clientdata(client, indio_dev);
221 data->client = client;
222
223 indio_dev->dev.parent = &client->dev;
224 indio_dev->name = dev_name(&client->dev);
225 indio_dev->modes = INDIO_DIRECT_MODE;
226 indio_dev->info = &tmp006_info;
227
228 indio_dev->channels = tmp006_channels;
229 indio_dev->num_channels = ARRAY_SIZE(tmp006_channels);
230
231 ret = i2c_smbus_read_word_swapped(data->client, TMP006_CONFIG);
232 if (ret < 0)
233 return ret;
234 data->config = ret;
235
236 return iio_device_register(indio_dev);
237}
238
239static int tmp006_powerdown(struct tmp006_data *data)
240{
241 return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG,
242 data->config & ~TMP006_CONFIG_MOD_MASK);
243}
244
245static int tmp006_remove(struct i2c_client *client)
246{
247 struct iio_dev *indio_dev = i2c_get_clientdata(client);
248
249 iio_device_unregister(indio_dev);
250 tmp006_powerdown(iio_priv(indio_dev));
251
252 return 0;
253}
254
255#ifdef CONFIG_PM_SLEEP
256static int tmp006_suspend(struct device *dev)
257{
258 return tmp006_powerdown(iio_priv(dev_to_iio_dev(dev)));
259}
260
261static int tmp006_resume(struct device *dev)
262{
263 struct tmp006_data *data = iio_priv(dev_to_iio_dev(dev));
264 return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG,
265 data->config | TMP006_CONFIG_MOD_MASK);
266}
267#endif
268
269static SIMPLE_DEV_PM_OPS(tmp006_pm_ops, tmp006_suspend, tmp006_resume);
270
271static const struct i2c_device_id tmp006_id[] = {
272 { "tmp006", 0 },
273 { }
274};
275MODULE_DEVICE_TABLE(i2c, tmp006_id);
276
277static struct i2c_driver tmp006_driver = {
278 .driver = {
279 .name = "tmp006",
280 .pm = &tmp006_pm_ops,
281 .owner = THIS_MODULE,
282 },
283 .probe = tmp006_probe,
284 .remove = tmp006_remove,
285 .id_table = tmp006_id,
286};
287module_i2c_driver(tmp006_driver);
288
289MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
290MODULE_DESCRIPTION("TI TMP006 IR thermopile sensor driver");
291MODULE_LICENSE("GPL");
diff --git a/drivers/iio/trigger/Kconfig b/drivers/iio/trigger/Kconfig
index 360fd508b088..79996123a71b 100644
--- a/drivers/iio/trigger/Kconfig
+++ b/drivers/iio/trigger/Kconfig
@@ -1,6 +1,8 @@
1# 1#
2# Industrial I/O standalone triggers 2# Industrial I/O standalone triggers
3# 3#
4# When adding new entries keep the list in alphabetical order
5
4menu "Triggers - standalone" 6menu "Triggers - standalone"
5 7
6config IIO_INTERRUPT_TRIGGER 8config IIO_INTERRUPT_TRIGGER
@@ -17,7 +19,7 @@ config IIO_SYSFS_TRIGGER
17 depends on SYSFS 19 depends on SYSFS
18 select IRQ_WORK 20 select IRQ_WORK
19 help 21 help
20 Provides support for using SYSFS entry as IIO triggers. 22 Provides support for using SYSFS entries as IIO triggers.
21 If unsure, say N (but it's safe to say "Y"). 23 If unsure, say N (but it's safe to say "Y").
22 24
23 To compile this driver as a module, choose M here: the 25 To compile this driver as a module, choose M here: the
diff --git a/drivers/iio/trigger/Makefile b/drivers/iio/trigger/Makefile
index ce319a51b6af..0694daecaf22 100644
--- a/drivers/iio/trigger/Makefile
+++ b/drivers/iio/trigger/Makefile
@@ -2,5 +2,6 @@
2# Makefile for triggers not associated with iio-devices 2# Makefile for triggers not associated with iio-devices
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order
5obj-$(CONFIG_IIO_INTERRUPT_TRIGGER) += iio-trig-interrupt.o 6obj-$(CONFIG_IIO_INTERRUPT_TRIGGER) += iio-trig-interrupt.o
6obj-$(CONFIG_IIO_SYSFS_TRIGGER) += iio-trig-sysfs.o 7obj-$(CONFIG_IIO_SYSFS_TRIGGER) += iio-trig-sysfs.o