aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-08-04 21:36:12 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2014-08-04 21:36:12 -0400
commit53ee983378ff23e8f3ff95ecf99dea7c6c221900 (patch)
tree85e09b2bf6317a155f1405be0d45c69051b6e041 /drivers/iio
parent29b88e23a9212136d39b0161a39afe587d0170a5 (diff)
parentb9aaea39f65e242303103b5283abeaefd8e538a4 (diff)
Merge tag 'staging-3.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
Pull staging driver updates from Greg KH: "Here's the big pull request for the staging driver tree for 3.17-rc1. Lots of things in here, over 2000 patches, but the best part is this: 1480 files changed, 39070 insertions(+), 254659 deletions(-) Thanks to the great work of Kristina Martšenko, 14 different staging drivers have been removed from the tree as they were obsolete and no one was willing to work on cleaning them up. Other than the driver removals, loads of cleanups are in here (comedi, lustre, etc.) as well as the usual IIO driver updates and additions. All of this has been in the linux-next tree for a while" * tag 'staging-3.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (2199 commits) staging: comedi: addi_apci_1564: remove diagnostic interrupt support code staging: comedi: addi_apci_1564: add subdevice to check diagnostic status staging: wlan-ng: coding style problem fix staging: wlan-ng: fixing coding style problems staging: comedi: ii_pci20kc: request and ioremap memory staging: lustre: bitwise vs logical typo staging: dgnc: Remove unneeded dgnc_trace.c and dgnc_trace.h staging: dgnc: rephrase comment staging: comedi: ni_tio: remove some dead code staging: rtl8723au: Fix static symbol sparse warning staging: rtl8723au: usb_dvobj_init(): Remove unused variable 'pdev_desc' staging: rtl8723au: Do not duplicate kernel provided USB macros staging: rtl8723au: Remove never set struct pwrctrl_priv.bHWPowerdown staging: rtl8723au: Remove two never set variables staging: rtl8723au: RSSI_test is never set staging:r8190: coding style: Fixed checkpatch reported Error staging:r8180: coding style: Fixed too long lines staging:r8180: coding style: Fixed commenting style staging: lustre: ptlrpc: lproc_ptlrpc.c - fix dereferenceing user space buffer staging: lustre: ldlm: ldlm_resource.c - fix dereferenceing user space buffer ...
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/accel/Kconfig12
-rw-r--r--drivers/iio/accel/Makefile1
-rw-r--r--drivers/iio/accel/kxcjk-1013.c764
-rw-r--r--drivers/iio/accel/mma8452.c6
-rw-r--r--drivers/iio/accel/st_accel_core.c12
-rw-r--r--drivers/iio/accel/st_accel_i2c.c51
-rw-r--r--drivers/iio/adc/Kconfig19
-rw-r--r--drivers/iio/adc/Makefile2
-rw-r--r--drivers/iio/adc/ad7291.c585
-rw-r--r--drivers/iio/adc/ad7298.c21
-rw-r--r--drivers/iio/adc/ad7476.c5
-rw-r--r--drivers/iio/adc/ad7887.c21
-rw-r--r--drivers/iio/adc/ad799x.c507
-rw-r--r--drivers/iio/adc/ad_sigma_delta.c2
-rw-r--r--drivers/iio/adc/at91_adc.c2
-rw-r--r--drivers/iio/adc/exynos_adc.c335
-rw-r--r--drivers/iio/adc/max1027.c521
-rw-r--r--drivers/iio/adc/xilinx-xadc-core.c2
-rw-r--r--drivers/iio/adc/xilinx-xadc-events.c6
-rw-r--r--drivers/iio/common/hid-sensors/hid-sensor-attributes.c5
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c64
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_i2c.c30
-rw-r--r--drivers/iio/dac/Kconfig10
-rw-r--r--drivers/iio/dac/Makefile1
-rw-r--r--drivers/iio/dac/ad5504.c16
-rw-r--r--drivers/iio/dac/ad5624r_spi.c5
-rw-r--r--drivers/iio/dac/ad5686.c3
-rw-r--r--drivers/iio/dac/ad5791.c29
-rw-r--r--drivers/iio/dac/mcp4922.c216
-rw-r--r--drivers/iio/gyro/adis16260.c124
-rw-r--r--drivers/iio/gyro/itg3200_core.c101
-rw-r--r--drivers/iio/gyro/st_gyro_core.c12
-rw-r--r--drivers/iio/gyro/st_gyro_i2c.c39
-rw-r--r--drivers/iio/imu/adis16400_buffer.c3
-rw-r--r--drivers/iio/imu/adis16400_core.c78
-rw-r--r--drivers/iio/imu/adis16480.c82
-rw-r--r--drivers/iio/industrialio-buffer.c5
-rw-r--r--drivers/iio/industrialio-core.c4
-rw-r--r--drivers/iio/industrialio-event.c1
-rw-r--r--drivers/iio/industrialio-trigger.c8
-rw-r--r--drivers/iio/light/Kconfig24
-rw-r--r--drivers/iio/light/Makefile2
-rw-r--r--drivers/iio/light/cm32181.c11
-rw-r--r--drivers/iio/light/gp2ap020a00f.c2
-rw-r--r--drivers/iio/light/isl29125.c347
-rw-r--r--drivers/iio/light/tcs3414.c405
-rw-r--r--drivers/iio/magnetometer/Kconfig10
-rw-r--r--drivers/iio/magnetometer/Makefile1
-rw-r--r--drivers/iio/magnetometer/ak09911.c326
-rw-r--r--drivers/iio/magnetometer/ak8975.c58
-rw-r--r--drivers/iio/magnetometer/hid-sensor-magn-3d.c202
-rw-r--r--drivers/iio/magnetometer/st_magn_core.c12
-rw-r--r--drivers/iio/magnetometer/st_magn_i2c.c23
-rw-r--r--drivers/iio/pressure/Kconfig10
-rw-r--r--drivers/iio/pressure/Makefile1
-rw-r--r--drivers/iio/pressure/st_pressure_core.c27
-rw-r--r--drivers/iio/pressure/st_pressure_i2c.c23
-rw-r--r--drivers/iio/pressure/t5403.c275
-rw-r--r--drivers/iio/proximity/as3935.c2
-rw-r--r--drivers/iio/trigger/iio-trig-interrupt.c3
-rw-r--r--drivers/iio/trigger/iio-trig-sysfs.c2
61 files changed, 4717 insertions, 759 deletions
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index 1e120fa1e156..12addf272a61 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -77,4 +77,16 @@ config MMA8452
77 To compile this driver as a module, choose M here: the module 77 To compile this driver as a module, choose M here: the module
78 will be called mma8452. 78 will be called mma8452.
79 79
80config KXCJK1013
81 tristate "Kionix 3-Axis Accelerometer Driver"
82 depends on I2C
83 select IIO_BUFFER
84 select IIO_TRIGGERED_BUFFER
85 help
86 Say Y here if you want to build a driver for the Kionix KXCJK-1013
87 triaxial acceleration sensor.
88
89 To compile this driver as a module, choose M here: the module will
90 be called kxcjk-1013.
91
80endmenu 92endmenu
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index dc0e379c2592..6578ca1a8e09 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -5,6 +5,7 @@
5# When adding new entries keep the list in alphabetical order 5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_BMA180) += bma180.o 6obj-$(CONFIG_BMA180) += bma180.o
7obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o 7obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
8obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
8obj-$(CONFIG_KXSD9) += kxsd9.o 9obj-$(CONFIG_KXSD9) += kxsd9.o
9obj-$(CONFIG_MMA8452) += mma8452.o 10obj-$(CONFIG_MMA8452) += mma8452.o
10 11
diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c
new file mode 100644
index 000000000000..7941cf2d31ee
--- /dev/null
+++ b/drivers/iio/accel/kxcjk-1013.c
@@ -0,0 +1,764 @@
1/*
2 * KXCJK-1013 3-axis accelerometer driver
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/interrupt.h>
18#include <linux/delay.h>
19#include <linux/bitops.h>
20#include <linux/slab.h>
21#include <linux/string.h>
22#include <linux/acpi.h>
23#include <linux/gpio/consumer.h>
24#include <linux/iio/iio.h>
25#include <linux/iio/sysfs.h>
26#include <linux/iio/buffer.h>
27#include <linux/iio/trigger.h>
28#include <linux/iio/trigger_consumer.h>
29#include <linux/iio/triggered_buffer.h>
30#include <linux/iio/accel/kxcjk_1013.h>
31
32#define KXCJK1013_DRV_NAME "kxcjk1013"
33#define KXCJK1013_IRQ_NAME "kxcjk1013_event"
34
35#define KXCJK1013_REG_XOUT_L 0x06
36/*
37 * From low byte X axis register, all the other addresses of Y and Z can be
38 * obtained by just applying axis offset. The following axis defines are just
39 * provide clarity, but not used.
40 */
41#define KXCJK1013_REG_XOUT_H 0x07
42#define KXCJK1013_REG_YOUT_L 0x08
43#define KXCJK1013_REG_YOUT_H 0x09
44#define KXCJK1013_REG_ZOUT_L 0x0A
45#define KXCJK1013_REG_ZOUT_H 0x0B
46
47#define KXCJK1013_REG_DCST_RESP 0x0C
48#define KXCJK1013_REG_WHO_AM_I 0x0F
49#define KXCJK1013_REG_INT_SRC1 0x16
50#define KXCJK1013_REG_INT_SRC2 0x17
51#define KXCJK1013_REG_STATUS_REG 0x18
52#define KXCJK1013_REG_INT_REL 0x1A
53#define KXCJK1013_REG_CTRL1 0x1B
54#define KXCJK1013_REG_CTRL2 0x1D
55#define KXCJK1013_REG_INT_CTRL1 0x1E
56#define KXCJK1013_REG_INT_CTRL2 0x1F
57#define KXCJK1013_REG_DATA_CTRL 0x21
58#define KXCJK1013_REG_WAKE_TIMER 0x29
59#define KXCJK1013_REG_SELF_TEST 0x3A
60#define KXCJK1013_REG_WAKE_THRES 0x6A
61
62#define KXCJK1013_REG_CTRL1_BIT_PC1 BIT(7)
63#define KXCJK1013_REG_CTRL1_BIT_RES BIT(6)
64#define KXCJK1013_REG_CTRL1_BIT_DRDY BIT(5)
65#define KXCJK1013_REG_CTRL1_BIT_GSEL1 BIT(4)
66#define KXCJK1013_REG_CTRL1_BIT_GSEL0 BIT(3)
67#define KXCJK1013_REG_CTRL1_BIT_WUFE BIT(1)
68#define KXCJK1013_REG_INT_REG1_BIT_IEA BIT(4)
69#define KXCJK1013_REG_INT_REG1_BIT_IEN BIT(5)
70
71#define KXCJK1013_DATA_MASK_12_BIT 0x0FFF
72#define KXCJK1013_MAX_STARTUP_TIME_US 100000
73
74struct kxcjk1013_data {
75 struct i2c_client *client;
76 struct iio_trigger *trig;
77 bool trig_mode;
78 struct mutex mutex;
79 s16 buffer[8];
80 int power_state;
81 u8 odr_bits;
82 bool active_high_intr;
83};
84
85enum kxcjk1013_axis {
86 AXIS_X,
87 AXIS_Y,
88 AXIS_Z,
89};
90
91enum kxcjk1013_mode {
92 STANDBY,
93 OPERATION,
94};
95
96static const struct {
97 int val;
98 int val2;
99 int odr_bits;
100} samp_freq_table[] = { {0, 781000, 0x08}, {1, 563000, 0x09},
101 {3, 125000, 0x0A}, {6, 250000, 0x0B}, {12, 500000, 0},
102 {25, 0, 0x01}, {50, 0, 0x02}, {100, 0, 0x03},
103 {200, 0, 0x04}, {400, 0, 0x05}, {800, 0, 0x06},
104 {1600, 0, 0x07} };
105
106/* Refer to section 4 of the specification */
107static const struct {
108 int odr_bits;
109 int usec;
110} odr_start_up_times[] = { {0x08, 100000}, {0x09, 100000}, {0x0A, 100000},
111 {0x0B, 100000}, { 0, 80000}, {0x01, 41000},
112 {0x02, 21000}, {0x03, 11000}, {0x04, 6400},
113 {0x05, 3900}, {0x06, 2700}, {0x07, 2100} };
114
115static int kxcjk1013_set_mode(struct kxcjk1013_data *data,
116 enum kxcjk1013_mode mode)
117{
118 int ret;
119
120 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
121 if (ret < 0) {
122 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
123 return ret;
124 }
125
126 if (mode == STANDBY)
127 ret &= ~KXCJK1013_REG_CTRL1_BIT_PC1;
128 else
129 ret |= KXCJK1013_REG_CTRL1_BIT_PC1;
130
131 ret = i2c_smbus_write_byte_data(data->client,
132 KXCJK1013_REG_CTRL1, ret);
133 if (ret < 0) {
134 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
135 return ret;
136 }
137
138 return 0;
139}
140
141static int kxcjk1013_chip_init(struct kxcjk1013_data *data)
142{
143 int ret;
144
145 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_WHO_AM_I);
146 if (ret < 0) {
147 dev_err(&data->client->dev, "Error reading who_am_i\n");
148 return ret;
149 }
150
151 dev_dbg(&data->client->dev, "KXCJK1013 Chip Id %x\n", ret);
152
153 ret = kxcjk1013_set_mode(data, STANDBY);
154 if (ret < 0)
155 return ret;
156
157 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
158 if (ret < 0) {
159 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
160 return ret;
161 }
162
163 /* Setting range to 4G */
164 ret |= KXCJK1013_REG_CTRL1_BIT_GSEL0;
165 ret &= ~KXCJK1013_REG_CTRL1_BIT_GSEL1;
166
167 /* Set 12 bit mode */
168 ret |= KXCJK1013_REG_CTRL1_BIT_RES;
169
170 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_CTRL1,
171 ret);
172 if (ret < 0) {
173 dev_err(&data->client->dev, "Error reading reg_ctrl\n");
174 return ret;
175 }
176
177 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_DATA_CTRL);
178 if (ret < 0) {
179 dev_err(&data->client->dev, "Error reading reg_data_ctrl\n");
180 return ret;
181 }
182
183 data->odr_bits = ret;
184
185 /* Set up INT polarity */
186 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
187 if (ret < 0) {
188 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
189 return ret;
190 }
191
192 if (data->active_high_intr)
193 ret |= KXCJK1013_REG_INT_REG1_BIT_IEA;
194 else
195 ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEA;
196
197 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
198 ret);
199 if (ret < 0) {
200 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
201 return ret;
202 }
203
204 return 0;
205}
206
207static int kxcjk1013_chip_setup_interrupt(struct kxcjk1013_data *data,
208 bool status)
209{
210 int ret;
211
212 /* This is requirement by spec to change state to STANDBY */
213 ret = kxcjk1013_set_mode(data, STANDBY);
214 if (ret < 0)
215 return ret;
216
217 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_CTRL1);
218 if (ret < 0) {
219 dev_err(&data->client->dev, "Error reading reg_int_ctrl1\n");
220 return ret;
221 }
222
223 if (status)
224 ret |= KXCJK1013_REG_INT_REG1_BIT_IEN;
225 else
226 ret &= ~KXCJK1013_REG_INT_REG1_BIT_IEN;
227
228 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_INT_CTRL1,
229 ret);
230 if (ret < 0) {
231 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
232 return ret;
233 }
234
235 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_CTRL1);
236 if (ret < 0) {
237 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
238 return ret;
239 }
240
241 if (status)
242 ret |= KXCJK1013_REG_CTRL1_BIT_DRDY;
243 else
244 ret &= ~KXCJK1013_REG_CTRL1_BIT_DRDY;
245
246 ret = i2c_smbus_write_byte_data(data->client,
247 KXCJK1013_REG_CTRL1, ret);
248 if (ret < 0) {
249 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
250 return ret;
251 }
252
253 return ret;
254}
255
256static int kxcjk1013_convert_freq_to_bit(int val, int val2)
257{
258 int i;
259
260 for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) {
261 if (samp_freq_table[i].val == val &&
262 samp_freq_table[i].val2 == val2) {
263 return samp_freq_table[i].odr_bits;
264 }
265 }
266
267 return -EINVAL;
268}
269
270static int kxcjk1013_set_odr(struct kxcjk1013_data *data, int val, int val2)
271{
272 int ret;
273 int odr_bits;
274
275 odr_bits = kxcjk1013_convert_freq_to_bit(val, val2);
276 if (odr_bits < 0)
277 return odr_bits;
278
279 /* To change ODR, the chip must be set to STANDBY as per spec */
280 ret = kxcjk1013_set_mode(data, STANDBY);
281 if (ret < 0)
282 return ret;
283
284 ret = i2c_smbus_write_byte_data(data->client, KXCJK1013_REG_DATA_CTRL,
285 odr_bits);
286 if (ret < 0) {
287 dev_err(&data->client->dev, "Error writing data_ctrl\n");
288 return ret;
289 }
290
291 data->odr_bits = odr_bits;
292
293 /* Check, if the ODR is changed after data enable */
294 if (data->power_state) {
295 /* Set the state back to operation */
296 ret = kxcjk1013_set_mode(data, OPERATION);
297 if (ret < 0)
298 return ret;
299 }
300
301 return 0;
302}
303
304static int kxcjk1013_get_odr(struct kxcjk1013_data *data, int *val, int *val2)
305{
306 int i;
307
308 for (i = 0; i < ARRAY_SIZE(samp_freq_table); ++i) {
309 if (samp_freq_table[i].odr_bits == data->odr_bits) {
310 *val = samp_freq_table[i].val;
311 *val2 = samp_freq_table[i].val2;
312 return IIO_VAL_INT_PLUS_MICRO;
313 }
314 }
315
316 return -EINVAL;
317}
318
319static int kxcjk1013_get_acc_reg(struct kxcjk1013_data *data, int axis)
320{
321 u8 reg = KXCJK1013_REG_XOUT_L + axis * 2;
322 int ret;
323
324 ret = i2c_smbus_read_word_data(data->client, reg);
325 if (ret < 0) {
326 dev_err(&data->client->dev,
327 "failed to read accel_%c registers\n", 'x' + axis);
328 return ret;
329 }
330
331 return ret;
332}
333
334static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data)
335{
336 int i;
337
338 for (i = 0; i < ARRAY_SIZE(odr_start_up_times); ++i) {
339 if (odr_start_up_times[i].odr_bits == data->odr_bits)
340 return odr_start_up_times[i].usec;
341 }
342
343 return KXCJK1013_MAX_STARTUP_TIME_US;
344}
345
346static int kxcjk1013_read_raw(struct iio_dev *indio_dev,
347 struct iio_chan_spec const *chan, int *val,
348 int *val2, long mask)
349{
350 struct kxcjk1013_data *data = iio_priv(indio_dev);
351 int ret;
352
353 switch (mask) {
354 case IIO_CHAN_INFO_RAW:
355 mutex_lock(&data->mutex);
356 if (iio_buffer_enabled(indio_dev))
357 ret = -EBUSY;
358 else {
359 int sleep_val;
360
361 ret = kxcjk1013_set_mode(data, OPERATION);
362 if (ret < 0) {
363 mutex_unlock(&data->mutex);
364 return ret;
365 }
366 ++data->power_state;
367 sleep_val = kxcjk1013_get_startup_times(data);
368 if (sleep_val < 20000)
369 usleep_range(sleep_val, 20000);
370 else
371 msleep_interruptible(sleep_val/1000);
372 ret = kxcjk1013_get_acc_reg(data, chan->scan_index);
373 if (--data->power_state == 0)
374 kxcjk1013_set_mode(data, STANDBY);
375 }
376 mutex_unlock(&data->mutex);
377
378 if (ret < 0)
379 return ret;
380
381 *val = sign_extend32(ret >> 4, 11);
382 return IIO_VAL_INT;
383
384 case IIO_CHAN_INFO_SCALE:
385 *val = 0;
386 *val2 = 19163; /* range +-4g (4/2047*9.806650) */
387 return IIO_VAL_INT_PLUS_MICRO;
388
389 case IIO_CHAN_INFO_SAMP_FREQ:
390 mutex_lock(&data->mutex);
391 ret = kxcjk1013_get_odr(data, val, val2);
392 mutex_unlock(&data->mutex);
393 return ret;
394
395 default:
396 return -EINVAL;
397 }
398}
399
400static int kxcjk1013_write_raw(struct iio_dev *indio_dev,
401 struct iio_chan_spec const *chan, int val,
402 int val2, long mask)
403{
404 struct kxcjk1013_data *data = iio_priv(indio_dev);
405 int ret;
406
407 switch (mask) {
408 case IIO_CHAN_INFO_SAMP_FREQ:
409 mutex_lock(&data->mutex);
410 ret = kxcjk1013_set_odr(data, val, val2);
411 mutex_unlock(&data->mutex);
412 break;
413 default:
414 ret = -EINVAL;
415 }
416
417 return ret;
418}
419
420static int kxcjk1013_validate_trigger(struct iio_dev *indio_dev,
421 struct iio_trigger *trig)
422{
423 struct kxcjk1013_data *data = iio_priv(indio_dev);
424
425 if (data->trig != trig)
426 return -EINVAL;
427
428 return 0;
429}
430
431static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
432 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800 1600");
433
434static struct attribute *kxcjk1013_attributes[] = {
435 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
436 NULL,
437};
438
439static const struct attribute_group kxcjk1013_attrs_group = {
440 .attrs = kxcjk1013_attributes,
441};
442
443#define KXCJK1013_CHANNEL(_axis) { \
444 .type = IIO_ACCEL, \
445 .modified = 1, \
446 .channel2 = IIO_MOD_##_axis, \
447 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
448 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
449 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
450 .scan_index = AXIS_##_axis, \
451 .scan_type = { \
452 .sign = 's', \
453 .realbits = 12, \
454 .storagebits = 16, \
455 .shift = 4, \
456 .endianness = IIO_CPU, \
457 }, \
458}
459
460static const struct iio_chan_spec kxcjk1013_channels[] = {
461 KXCJK1013_CHANNEL(X),
462 KXCJK1013_CHANNEL(Y),
463 KXCJK1013_CHANNEL(Z),
464 IIO_CHAN_SOFT_TIMESTAMP(3),
465};
466
467static const struct iio_info kxcjk1013_info = {
468 .attrs = &kxcjk1013_attrs_group,
469 .read_raw = kxcjk1013_read_raw,
470 .write_raw = kxcjk1013_write_raw,
471 .validate_trigger = kxcjk1013_validate_trigger,
472 .driver_module = THIS_MODULE,
473};
474
475static irqreturn_t kxcjk1013_trigger_handler(int irq, void *p)
476{
477 struct iio_poll_func *pf = p;
478 struct iio_dev *indio_dev = pf->indio_dev;
479 struct kxcjk1013_data *data = iio_priv(indio_dev);
480 int bit, ret, i = 0;
481
482 mutex_lock(&data->mutex);
483
484 for_each_set_bit(bit, indio_dev->buffer->scan_mask,
485 indio_dev->masklength) {
486 ret = kxcjk1013_get_acc_reg(data, bit);
487 if (ret < 0) {
488 mutex_unlock(&data->mutex);
489 goto err;
490 }
491 data->buffer[i++] = ret;
492 }
493 mutex_unlock(&data->mutex);
494
495 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
496 pf->timestamp);
497err:
498 iio_trigger_notify_done(indio_dev->trig);
499
500 return IRQ_HANDLED;
501}
502
503static int kxcjk1013_trig_try_reen(struct iio_trigger *trig)
504{
505 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
506 struct kxcjk1013_data *data = iio_priv(indio_dev);
507 int ret;
508
509 ret = i2c_smbus_read_byte_data(data->client, KXCJK1013_REG_INT_REL);
510 if (ret < 0) {
511 dev_err(&data->client->dev, "Error reading reg_int_rel\n");
512 return ret;
513 }
514
515 return 0;
516}
517
518static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig,
519 bool state)
520{
521 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
522 struct kxcjk1013_data *data = iio_priv(indio_dev);
523
524 mutex_lock(&data->mutex);
525 if (state) {
526 kxcjk1013_chip_setup_interrupt(data, true);
527 kxcjk1013_set_mode(data, OPERATION);
528 ++data->power_state;
529 } else {
530 if (--data->power_state) {
531 mutex_unlock(&data->mutex);
532 return 0;
533 }
534 kxcjk1013_chip_setup_interrupt(data, false);
535 kxcjk1013_set_mode(data, STANDBY);
536 }
537 mutex_unlock(&data->mutex);
538
539 return 0;
540}
541
542static const struct iio_trigger_ops kxcjk1013_trigger_ops = {
543 .set_trigger_state = kxcjk1013_data_rdy_trigger_set_state,
544 .try_reenable = kxcjk1013_trig_try_reen,
545 .owner = THIS_MODULE,
546};
547
548static int kxcjk1013_acpi_gpio_probe(struct i2c_client *client,
549 struct kxcjk1013_data *data)
550{
551 const struct acpi_device_id *id;
552 struct device *dev;
553 struct gpio_desc *gpio;
554 int ret;
555
556 if (!client)
557 return -EINVAL;
558
559 dev = &client->dev;
560 if (!ACPI_HANDLE(dev))
561 return -ENODEV;
562
563 id = acpi_match_device(dev->driver->acpi_match_table, dev);
564 if (!id)
565 return -ENODEV;
566
567 /* data ready gpio interrupt pin */
568 gpio = devm_gpiod_get_index(dev, "kxcjk1013_int", 0);
569 if (IS_ERR(gpio)) {
570 dev_err(dev, "acpi gpio get index failed\n");
571 return PTR_ERR(gpio);
572 }
573
574 ret = gpiod_direction_input(gpio);
575 if (ret)
576 return ret;
577
578 ret = gpiod_to_irq(gpio);
579
580 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
581
582 return ret;
583}
584
585static int kxcjk1013_probe(struct i2c_client *client,
586 const struct i2c_device_id *id)
587{
588 struct kxcjk1013_data *data;
589 struct iio_dev *indio_dev;
590 struct iio_trigger *trig = NULL;
591 struct kxcjk_1013_platform_data *pdata;
592 int ret;
593
594 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
595 if (!indio_dev)
596 return -ENOMEM;
597
598 data = iio_priv(indio_dev);
599 i2c_set_clientdata(client, indio_dev);
600 data->client = client;
601
602 pdata = dev_get_platdata(&client->dev);
603 if (pdata)
604 data->active_high_intr = pdata->active_high_intr;
605 else
606 data->active_high_intr = true; /* default polarity */
607
608 ret = kxcjk1013_chip_init(data);
609 if (ret < 0)
610 return ret;
611
612 mutex_init(&data->mutex);
613
614 indio_dev->dev.parent = &client->dev;
615 indio_dev->channels = kxcjk1013_channels;
616 indio_dev->num_channels = ARRAY_SIZE(kxcjk1013_channels);
617 indio_dev->name = KXCJK1013_DRV_NAME;
618 indio_dev->modes = INDIO_DIRECT_MODE;
619 indio_dev->info = &kxcjk1013_info;
620
621 if (client->irq < 0)
622 client->irq = kxcjk1013_acpi_gpio_probe(client, data);
623
624 if (client->irq >= 0) {
625 trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
626 indio_dev->id);
627 if (!trig)
628 return -ENOMEM;
629
630 data->trig_mode = true;
631
632 ret = devm_request_irq(&client->dev, client->irq,
633 iio_trigger_generic_data_rdy_poll,
634 IRQF_TRIGGER_RISING,
635 KXCJK1013_IRQ_NAME,
636 trig);
637 if (ret) {
638 dev_err(&client->dev, "unable to request IRQ\n");
639 goto err_trigger_free;
640 }
641
642 trig->dev.parent = &client->dev;
643 trig->ops = &kxcjk1013_trigger_ops;
644 iio_trigger_set_drvdata(trig, indio_dev);
645 data->trig = trig;
646 indio_dev->trig = trig;
647 iio_trigger_get(indio_dev->trig);
648
649 ret = iio_trigger_register(trig);
650 if (ret)
651 goto err_trigger_free;
652
653 ret = iio_triggered_buffer_setup(indio_dev,
654 &iio_pollfunc_store_time,
655 kxcjk1013_trigger_handler,
656 NULL);
657 if (ret < 0) {
658 dev_err(&client->dev,
659 "iio triggered buffer setup failed\n");
660 goto err_trigger_unregister;
661 }
662 }
663
664 ret = devm_iio_device_register(&client->dev, indio_dev);
665 if (ret < 0) {
666 dev_err(&client->dev, "unable to register iio device\n");
667 goto err_buffer_cleanup;
668 }
669
670 return 0;
671
672err_buffer_cleanup:
673 if (data->trig_mode)
674 iio_triggered_buffer_cleanup(indio_dev);
675err_trigger_unregister:
676 if (data->trig_mode)
677 iio_trigger_unregister(trig);
678err_trigger_free:
679 if (data->trig_mode)
680 iio_trigger_free(trig);
681
682 return ret;
683}
684
685static int kxcjk1013_remove(struct i2c_client *client)
686{
687 struct iio_dev *indio_dev = i2c_get_clientdata(client);
688 struct kxcjk1013_data *data = iio_priv(indio_dev);
689
690 if (data->trig_mode) {
691 iio_triggered_buffer_cleanup(indio_dev);
692 iio_trigger_unregister(data->trig);
693 iio_trigger_free(data->trig);
694 }
695
696 mutex_lock(&data->mutex);
697 kxcjk1013_set_mode(data, STANDBY);
698 mutex_unlock(&data->mutex);
699
700 return 0;
701}
702
703#ifdef CONFIG_PM_SLEEP
704static int kxcjk1013_suspend(struct device *dev)
705{
706 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
707 struct kxcjk1013_data *data = iio_priv(indio_dev);
708
709 mutex_lock(&data->mutex);
710 kxcjk1013_set_mode(data, STANDBY);
711 mutex_unlock(&data->mutex);
712
713 return 0;
714}
715
716static int kxcjk1013_resume(struct device *dev)
717{
718 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
719 struct kxcjk1013_data *data = iio_priv(indio_dev);
720
721 mutex_lock(&data->mutex);
722
723 if (data->power_state)
724 kxcjk1013_set_mode(data, OPERATION);
725
726 mutex_unlock(&data->mutex);
727
728 return 0;
729}
730
731static SIMPLE_DEV_PM_OPS(kxcjk1013_pm_ops, kxcjk1013_suspend, kxcjk1013_resume);
732#define KXCJK1013_PM_OPS (&kxcjk1013_pm_ops)
733#else
734#define KXCJK1013_PM_OPS NULL
735#endif
736
737static const struct acpi_device_id kx_acpi_match[] = {
738 {"KXCJ1013", 0},
739 { },
740};
741MODULE_DEVICE_TABLE(acpi, kx_acpi_match);
742
743static const struct i2c_device_id kxcjk1013_id[] = {
744 {"kxcjk1013", 0},
745 {}
746};
747
748MODULE_DEVICE_TABLE(i2c, kxcjk1013_id);
749
750static struct i2c_driver kxcjk1013_driver = {
751 .driver = {
752 .name = KXCJK1013_DRV_NAME,
753 .acpi_match_table = ACPI_PTR(kx_acpi_match),
754 .pm = KXCJK1013_PM_OPS,
755 },
756 .probe = kxcjk1013_probe,
757 .remove = kxcjk1013_remove,
758 .id_table = kxcjk1013_id,
759};
760module_i2c_driver(kxcjk1013_driver);
761
762MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
763MODULE_LICENSE("GPL v2");
764MODULE_DESCRIPTION("KXCJK1013 accelerometer driver");
diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
index 2a5fa9a436e5..3c12d4966376 100644
--- a/drivers/iio/accel/mma8452.c
+++ b/drivers/iio/accel/mma8452.c
@@ -429,9 +429,15 @@ static const struct i2c_device_id mma8452_id[] = {
429}; 429};
430MODULE_DEVICE_TABLE(i2c, mma8452_id); 430MODULE_DEVICE_TABLE(i2c, mma8452_id);
431 431
432static const struct of_device_id mma8452_dt_ids[] = {
433 { .compatible = "fsl,mma8452" },
434 { }
435};
436
432static struct i2c_driver mma8452_driver = { 437static struct i2c_driver mma8452_driver = {
433 .driver = { 438 .driver = {
434 .name = "mma8452", 439 .name = "mma8452",
440 .of_match_table = of_match_ptr(mma8452_dt_ids),
435 .pm = MMA8452_PM_OPS, 441 .pm = MMA8452_PM_OPS,
436 }, 442 },
437 .probe = mma8452_probe, 443 .probe = mma8452_probe,
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c
index a2abf7c2ce3b..087864854c61 100644
--- a/drivers/iio/accel/st_accel_core.c
+++ b/drivers/iio/accel/st_accel_core.c
@@ -393,6 +393,9 @@ static int st_accel_read_raw(struct iio_dev *indio_dev,
393 *val = 0; 393 *val = 0;
394 *val2 = adata->current_fullscale->gain; 394 *val2 = adata->current_fullscale->gain;
395 return IIO_VAL_INT_PLUS_MICRO; 395 return IIO_VAL_INT_PLUS_MICRO;
396 case IIO_CHAN_INFO_SAMP_FREQ:
397 *val = adata->odr;
398 return IIO_VAL_INT;
396 default: 399 default:
397 return -EINVAL; 400 return -EINVAL;
398 } 401 }
@@ -410,6 +413,13 @@ static int st_accel_write_raw(struct iio_dev *indio_dev,
410 case IIO_CHAN_INFO_SCALE: 413 case IIO_CHAN_INFO_SCALE:
411 err = st_sensors_set_fullscale_by_gain(indio_dev, val2); 414 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
412 break; 415 break;
416 case IIO_CHAN_INFO_SAMP_FREQ:
417 if (val2)
418 return -EINVAL;
419 mutex_lock(&indio_dev->mlock);
420 err = st_sensors_set_odr(indio_dev, val);
421 mutex_unlock(&indio_dev->mlock);
422 return err;
413 default: 423 default:
414 return -EINVAL; 424 return -EINVAL;
415 } 425 }
@@ -417,14 +427,12 @@ static int st_accel_write_raw(struct iio_dev *indio_dev,
417 return err; 427 return err;
418} 428}
419 429
420static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
421static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 430static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
422static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available); 431static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_accel_scale_available);
423 432
424static struct attribute *st_accel_attributes[] = { 433static struct attribute *st_accel_attributes[] = {
425 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 434 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
426 &iio_dev_attr_in_accel_scale_available.dev_attr.attr, 435 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
427 &iio_dev_attr_sampling_frequency.dev_attr.attr,
428 NULL, 436 NULL,
429}; 437};
430 438
diff --git a/drivers/iio/accel/st_accel_i2c.c b/drivers/iio/accel/st_accel_i2c.c
index d7bedbdfc81d..7164aeff3ab1 100644
--- a/drivers/iio/accel/st_accel_i2c.c
+++ b/drivers/iio/accel/st_accel_i2c.c
@@ -18,6 +18,55 @@
18#include <linux/iio/common/st_sensors_i2c.h> 18#include <linux/iio/common/st_sensors_i2c.h>
19#include "st_accel.h" 19#include "st_accel.h"
20 20
21#ifdef CONFIG_OF
22static const struct of_device_id st_accel_of_match[] = {
23 {
24 .compatible = "st,lsm303dlh-accel",
25 .data = LSM303DLH_ACCEL_DEV_NAME,
26 },
27 {
28 .compatible = "st,lsm303dlhc-accel",
29 .data = LSM303DLHC_ACCEL_DEV_NAME,
30 },
31 {
32 .compatible = "st,lis3dh-accel",
33 .data = LIS3DH_ACCEL_DEV_NAME,
34 },
35 {
36 .compatible = "st,lsm330d-accel",
37 .data = LSM330D_ACCEL_DEV_NAME,
38 },
39 {
40 .compatible = "st,lsm330dl-accel",
41 .data = LSM330DL_ACCEL_DEV_NAME,
42 },
43 {
44 .compatible = "st,lsm330dlc-accel",
45 .data = LSM330DLC_ACCEL_DEV_NAME,
46 },
47 {
48 .compatible = "st,lis331dlh-accel",
49 .data = LIS331DLH_ACCEL_DEV_NAME,
50 },
51 {
52 .compatible = "st,lsm303dl-accel",
53 .data = LSM303DL_ACCEL_DEV_NAME,
54 },
55 {
56 .compatible = "st,lsm303dlm-accel",
57 .data = LSM303DLM_ACCEL_DEV_NAME,
58 },
59 {
60 .compatible = "st,lsm330-accel",
61 .data = LSM330_ACCEL_DEV_NAME,
62 },
63 {},
64};
65MODULE_DEVICE_TABLE(of, st_accel_of_match);
66#else
67#define st_accel_of_match NULL
68#endif
69
21static int st_accel_i2c_probe(struct i2c_client *client, 70static int st_accel_i2c_probe(struct i2c_client *client,
22 const struct i2c_device_id *id) 71 const struct i2c_device_id *id)
23{ 72{
@@ -31,6 +80,7 @@ static int st_accel_i2c_probe(struct i2c_client *client,
31 80
32 adata = iio_priv(indio_dev); 81 adata = iio_priv(indio_dev);
33 adata->dev = &client->dev; 82 adata->dev = &client->dev;
83 st_sensors_of_i2c_probe(client, st_accel_of_match);
34 84
35 st_sensors_i2c_configure(indio_dev, client, adata); 85 st_sensors_i2c_configure(indio_dev, client, adata);
36 86
@@ -67,6 +117,7 @@ static struct i2c_driver st_accel_driver = {
67 .driver = { 117 .driver = {
68 .owner = THIS_MODULE, 118 .owner = THIS_MODULE,
69 .name = "st-accel-i2c", 119 .name = "st-accel-i2c",
120 .of_match_table = of_match_ptr(st_accel_of_match),
70 }, 121 },
71 .probe = st_accel_i2c_probe, 122 .probe = st_accel_i2c_probe,
72 .remove = st_accel_i2c_remove, 123 .remove = st_accel_i2c_remove,
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index a80d23628f14..11b048a59fde 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -20,6 +20,16 @@ config AD7266
20 Say yes here to build support for Analog Devices AD7265 and AD7266 20 Say yes here to build support for Analog Devices AD7265 and AD7266
21 ADCs. 21 ADCs.
22 22
23config AD7291
24 tristate "Analog Devices AD7291 ADC driver"
25 depends on I2C
26 help
27 Say yes here to build support for Analog Devices AD7291
28 8 Channel ADC with temperature sensor.
29
30 To compile this driver as a module, choose M here: the
31 module will be called ad7291.
32
23config AD7298 33config AD7298
24 tristate "Analog Devices AD7298 ADC driver" 34 tristate "Analog Devices AD7298 ADC driver"
25 depends on SPI 35 depends on SPI
@@ -131,6 +141,15 @@ config LP8788_ADC
131 help 141 help
132 Say yes here to build support for TI LP8788 ADC. 142 Say yes here to build support for TI LP8788 ADC.
133 143
144config MAX1027
145 tristate "Maxim max1027 ADC driver"
146 depends on SPI
147 select IIO_BUFFER
148 select IIO_TRIGGERED_BUFFER
149 help
150 Say yes here to build support for Maxim SPI ADC models
151 max1027, max1029 and max1031.
152
134config MAX1363 153config MAX1363
135 tristate "Maxim max1363 ADC driver" 154 tristate "Maxim max1363 ADC driver"
136 depends on I2C 155 depends on I2C
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 9d60f2deaaaf..ad81b512aa3d 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -5,6 +5,7 @@
5# When adding new entries keep the list in alphabetical order 5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o 6obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o
7obj-$(CONFIG_AD7266) += ad7266.o 7obj-$(CONFIG_AD7266) += ad7266.o
8obj-$(CONFIG_AD7291) += ad7291.o
8obj-$(CONFIG_AD7298) += ad7298.o 9obj-$(CONFIG_AD7298) += ad7298.o
9obj-$(CONFIG_AD7923) += ad7923.o 10obj-$(CONFIG_AD7923) += ad7923.o
10obj-$(CONFIG_AD7476) += ad7476.o 11obj-$(CONFIG_AD7476) += ad7476.o
@@ -15,6 +16,7 @@ obj-$(CONFIG_AD799X) += ad799x.o
15obj-$(CONFIG_AT91_ADC) += at91_adc.o 16obj-$(CONFIG_AT91_ADC) += at91_adc.o
16obj-$(CONFIG_EXYNOS_ADC) += exynos_adc.o 17obj-$(CONFIG_EXYNOS_ADC) += exynos_adc.o
17obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o 18obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
19obj-$(CONFIG_MAX1027) += max1027.o
18obj-$(CONFIG_MAX1363) += max1363.o 20obj-$(CONFIG_MAX1363) += max1363.o
19obj-$(CONFIG_MCP320X) += mcp320x.o 21obj-$(CONFIG_MCP320X) += mcp320x.o
20obj-$(CONFIG_MCP3422) += mcp3422.o 22obj-$(CONFIG_MCP3422) += mcp3422.o
diff --git a/drivers/iio/adc/ad7291.c b/drivers/iio/adc/ad7291.c
new file mode 100644
index 000000000000..c0eabf156702
--- /dev/null
+++ b/drivers/iio/adc/ad7291.c
@@ -0,0 +1,585 @@
1/*
2 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
3 *
4 * Copyright 2010-2011 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/i2c.h>
12#include <linux/interrupt.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/regulator/consumer.h>
17#include <linux/slab.h>
18#include <linux/sysfs.h>
19
20#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h>
22#include <linux/iio/events.h>
23
24#include <linux/platform_data/ad7291.h>
25
26/*
27 * Simplified handling
28 *
29 * If no events enabled - single polled channel read
30 * If event enabled direct reads disable unless channel
31 * is in the read mask.
32 *
33 * The noise-delayed bit as per datasheet suggestion is always enabled.
34 */
35
36/*
37 * AD7291 registers definition
38 */
39#define AD7291_COMMAND 0x00
40#define AD7291_VOLTAGE 0x01
41#define AD7291_T_SENSE 0x02
42#define AD7291_T_AVERAGE 0x03
43#define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
44#define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
45#define AD7291_HYST(x) ((x) * 3 + 0x6)
46#define AD7291_VOLTAGE_ALERT_STATUS 0x1F
47#define AD7291_T_ALERT_STATUS 0x20
48
49#define AD7291_BITS 12
50#define AD7291_VOLTAGE_LIMIT_COUNT 8
51
52
53/*
54 * AD7291 command
55 */
56#define AD7291_AUTOCYCLE BIT(0)
57#define AD7291_RESET BIT(1)
58#define AD7291_ALERT_CLEAR BIT(2)
59#define AD7291_ALERT_POLARITY BIT(3)
60#define AD7291_EXT_REF BIT(4)
61#define AD7291_NOISE_DELAY BIT(5)
62#define AD7291_T_SENSE_MASK BIT(7)
63#define AD7291_VOLTAGE_MASK GENMASK(15, 8)
64#define AD7291_VOLTAGE_OFFSET 8
65
66/*
67 * AD7291 value masks
68 */
69#define AD7291_VALUE_MASK GENMASK(11, 0)
70
71/*
72 * AD7291 alert register bits
73 */
74#define AD7291_T_LOW BIT(0)
75#define AD7291_T_HIGH BIT(1)
76#define AD7291_T_AVG_LOW BIT(2)
77#define AD7291_T_AVG_HIGH BIT(3)
78#define AD7291_V_LOW(x) BIT((x) * 2)
79#define AD7291_V_HIGH(x) BIT((x) * 2 + 1)
80
81
82struct ad7291_chip_info {
83 struct i2c_client *client;
84 struct regulator *reg;
85 u16 command;
86 u16 c_mask; /* Active voltage channels for events */
87 struct mutex state_lock;
88};
89
90static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
91{
92 struct i2c_client *client = chip->client;
93 int ret = 0;
94
95 ret = i2c_smbus_read_word_swapped(client, reg);
96 if (ret < 0) {
97 dev_err(&client->dev, "I2C read error\n");
98 return ret;
99 }
100
101 *data = ret;
102
103 return 0;
104}
105
106static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
107{
108 return i2c_smbus_write_word_swapped(chip->client, reg, data);
109}
110
111static irqreturn_t ad7291_event_handler(int irq, void *private)
112{
113 struct iio_dev *indio_dev = private;
114 struct ad7291_chip_info *chip = iio_priv(private);
115 u16 t_status, v_status;
116 u16 command;
117 int i;
118 s64 timestamp = iio_get_time_ns();
119
120 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
121 return IRQ_HANDLED;
122
123 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
124 return IRQ_HANDLED;
125
126 if (!(t_status || v_status))
127 return IRQ_HANDLED;
128
129 command = chip->command | AD7291_ALERT_CLEAR;
130 ad7291_i2c_write(chip, AD7291_COMMAND, command);
131
132 command = chip->command & ~AD7291_ALERT_CLEAR;
133 ad7291_i2c_write(chip, AD7291_COMMAND, command);
134
135 /* For now treat t_sense and t_sense_average the same */
136 if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW))
137 iio_push_event(indio_dev,
138 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
139 0,
140 IIO_EV_TYPE_THRESH,
141 IIO_EV_DIR_FALLING),
142 timestamp);
143 if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH))
144 iio_push_event(indio_dev,
145 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
146 0,
147 IIO_EV_TYPE_THRESH,
148 IIO_EV_DIR_RISING),
149 timestamp);
150
151 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
152 if (v_status & AD7291_V_LOW(i))
153 iio_push_event(indio_dev,
154 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
155 i,
156 IIO_EV_TYPE_THRESH,
157 IIO_EV_DIR_FALLING),
158 timestamp);
159 if (v_status & AD7291_V_HIGH(i))
160 iio_push_event(indio_dev,
161 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
162 i,
163 IIO_EV_TYPE_THRESH,
164 IIO_EV_DIR_RISING),
165 timestamp);
166 }
167
168 return IRQ_HANDLED;
169}
170
171static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
172 enum iio_event_direction dir,
173 enum iio_event_info info)
174{
175 unsigned int offset;
176
177 switch (chan->type) {
178 case IIO_VOLTAGE:
179 offset = chan->channel;
180 break;
181 case IIO_TEMP:
182 offset = AD7291_VOLTAGE_OFFSET;
183 break;
184 default:
185 return 0;
186 }
187
188 switch (info) {
189 case IIO_EV_INFO_VALUE:
190 if (dir == IIO_EV_DIR_FALLING)
191 return AD7291_DATA_HIGH(offset);
192 else
193 return AD7291_DATA_LOW(offset);
194 case IIO_EV_INFO_HYSTERESIS:
195 return AD7291_HYST(offset);
196 default:
197 break;
198 }
199 return 0;
200}
201
202static int ad7291_read_event_value(struct iio_dev *indio_dev,
203 const struct iio_chan_spec *chan,
204 enum iio_event_type type,
205 enum iio_event_direction dir,
206 enum iio_event_info info,
207 int *val, int *val2)
208{
209 struct ad7291_chip_info *chip = iio_priv(indio_dev);
210 int ret;
211 u16 uval;
212
213 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
214 &uval);
215 if (ret < 0)
216 return ret;
217
218 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
219 *val = uval & AD7291_VALUE_MASK;
220
221 else
222 *val = sign_extend32(uval, 11);
223
224 return IIO_VAL_INT;
225}
226
227static int ad7291_write_event_value(struct iio_dev *indio_dev,
228 const struct iio_chan_spec *chan,
229 enum iio_event_type type,
230 enum iio_event_direction dir,
231 enum iio_event_info info,
232 int val, int val2)
233{
234 struct ad7291_chip_info *chip = iio_priv(indio_dev);
235
236 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
237 if (val > AD7291_VALUE_MASK || val < 0)
238 return -EINVAL;
239 } else {
240 if (val > 2047 || val < -2048)
241 return -EINVAL;
242 }
243
244 return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
245 val);
246}
247
248static int ad7291_read_event_config(struct iio_dev *indio_dev,
249 const struct iio_chan_spec *chan,
250 enum iio_event_type type,
251 enum iio_event_direction dir)
252{
253 struct ad7291_chip_info *chip = iio_priv(indio_dev);
254 /*
255 * To be enabled the channel must simply be on. If any are enabled
256 * we are in continuous sampling mode
257 */
258
259 switch (chan->type) {
260 case IIO_VOLTAGE:
261 return !!(chip->c_mask & BIT(15 - chan->channel));
262 case IIO_TEMP:
263 /* always on */
264 return 1;
265 default:
266 return -EINVAL;
267 }
268
269}
270
271static int ad7291_write_event_config(struct iio_dev *indio_dev,
272 const struct iio_chan_spec *chan,
273 enum iio_event_type type,
274 enum iio_event_direction dir,
275 int state)
276{
277 int ret = 0;
278 struct ad7291_chip_info *chip = iio_priv(indio_dev);
279 unsigned int mask;
280 u16 regval;
281
282 mutex_lock(&chip->state_lock);
283 regval = chip->command;
284 /*
285 * To be enabled the channel must simply be on. If any are enabled
286 * use continuous sampling mode.
287 * Possible to disable temp as well but that makes single read tricky.
288 */
289
290 mask = BIT(15 - chan->channel);
291
292 switch (chan->type) {
293 case IIO_VOLTAGE:
294 if ((!state) && (chip->c_mask & mask))
295 chip->c_mask &= ~mask;
296 else if (state && (!(chip->c_mask & mask)))
297 chip->c_mask |= mask;
298 else
299 break;
300
301 regval &= ~AD7291_AUTOCYCLE;
302 regval |= chip->c_mask;
303 if (chip->c_mask) /* Enable autocycle? */
304 regval |= AD7291_AUTOCYCLE;
305
306 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
307 if (ret < 0)
308 goto error_ret;
309
310 chip->command = regval;
311 break;
312 default:
313 ret = -EINVAL;
314 }
315
316error_ret:
317 mutex_unlock(&chip->state_lock);
318 return ret;
319}
320
321static int ad7291_read_raw(struct iio_dev *indio_dev,
322 struct iio_chan_spec const *chan,
323 int *val,
324 int *val2,
325 long mask)
326{
327 int ret;
328 struct ad7291_chip_info *chip = iio_priv(indio_dev);
329 u16 regval;
330
331 switch (mask) {
332 case IIO_CHAN_INFO_RAW:
333 switch (chan->type) {
334 case IIO_VOLTAGE:
335 mutex_lock(&chip->state_lock);
336 /* If in autocycle mode drop through */
337 if (chip->command & AD7291_AUTOCYCLE) {
338 mutex_unlock(&chip->state_lock);
339 return -EBUSY;
340 }
341 /* Enable this channel alone */
342 regval = chip->command & (~AD7291_VOLTAGE_MASK);
343 regval |= BIT(15 - chan->channel);
344 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
345 if (ret < 0) {
346 mutex_unlock(&chip->state_lock);
347 return ret;
348 }
349 /* Read voltage */
350 ret = i2c_smbus_read_word_swapped(chip->client,
351 AD7291_VOLTAGE);
352 if (ret < 0) {
353 mutex_unlock(&chip->state_lock);
354 return ret;
355 }
356 *val = ret & AD7291_VALUE_MASK;
357 mutex_unlock(&chip->state_lock);
358 return IIO_VAL_INT;
359 case IIO_TEMP:
360 /* Assumes tsense bit of command register always set */
361 ret = i2c_smbus_read_word_swapped(chip->client,
362 AD7291_T_SENSE);
363 if (ret < 0)
364 return ret;
365 *val = sign_extend32(ret, 11);
366 return IIO_VAL_INT;
367 default:
368 return -EINVAL;
369 }
370 case IIO_CHAN_INFO_AVERAGE_RAW:
371 ret = i2c_smbus_read_word_swapped(chip->client,
372 AD7291_T_AVERAGE);
373 if (ret < 0)
374 return ret;
375 *val = sign_extend32(ret, 11);
376 return IIO_VAL_INT;
377 case IIO_CHAN_INFO_SCALE:
378 switch (chan->type) {
379 case IIO_VOLTAGE:
380 if (chip->reg) {
381 int vref;
382
383 vref = regulator_get_voltage(chip->reg);
384 if (vref < 0)
385 return vref;
386 *val = vref / 1000;
387 } else {
388 *val = 2500;
389 }
390 *val2 = AD7291_BITS;
391 return IIO_VAL_FRACTIONAL_LOG2;
392 case IIO_TEMP:
393 /*
394 * One LSB of the ADC corresponds to 0.25 deg C.
395 * The temperature reading is in 12-bit twos
396 * complement format
397 */
398 *val = 250;
399 return IIO_VAL_INT;
400 default:
401 return -EINVAL;
402 }
403 default:
404 return -EINVAL;
405 }
406}
407
408static const struct iio_event_spec ad7291_events[] = {
409 {
410 .type = IIO_EV_TYPE_THRESH,
411 .dir = IIO_EV_DIR_RISING,
412 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
413 BIT(IIO_EV_INFO_ENABLE),
414 }, {
415 .type = IIO_EV_TYPE_THRESH,
416 .dir = IIO_EV_DIR_FALLING,
417 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
418 BIT(IIO_EV_INFO_ENABLE),
419 }, {
420 .type = IIO_EV_TYPE_THRESH,
421 .dir = IIO_EV_DIR_EITHER,
422 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
423 },
424};
425
426#define AD7291_VOLTAGE_CHAN(_chan) \
427{ \
428 .type = IIO_VOLTAGE, \
429 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
430 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
431 .indexed = 1, \
432 .channel = _chan, \
433 .event_spec = ad7291_events, \
434 .num_event_specs = ARRAY_SIZE(ad7291_events), \
435}
436
437static const struct iio_chan_spec ad7291_channels[] = {
438 AD7291_VOLTAGE_CHAN(0),
439 AD7291_VOLTAGE_CHAN(1),
440 AD7291_VOLTAGE_CHAN(2),
441 AD7291_VOLTAGE_CHAN(3),
442 AD7291_VOLTAGE_CHAN(4),
443 AD7291_VOLTAGE_CHAN(5),
444 AD7291_VOLTAGE_CHAN(6),
445 AD7291_VOLTAGE_CHAN(7),
446 {
447 .type = IIO_TEMP,
448 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
449 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
450 BIT(IIO_CHAN_INFO_SCALE),
451 .indexed = 1,
452 .channel = 0,
453 .event_spec = ad7291_events,
454 .num_event_specs = ARRAY_SIZE(ad7291_events),
455 }
456};
457
458static const struct iio_info ad7291_info = {
459 .read_raw = &ad7291_read_raw,
460 .read_event_config = &ad7291_read_event_config,
461 .write_event_config = &ad7291_write_event_config,
462 .read_event_value = &ad7291_read_event_value,
463 .write_event_value = &ad7291_write_event_value,
464 .driver_module = THIS_MODULE,
465};
466
467static int ad7291_probe(struct i2c_client *client,
468 const struct i2c_device_id *id)
469{
470 struct ad7291_platform_data *pdata = client->dev.platform_data;
471 struct ad7291_chip_info *chip;
472 struct iio_dev *indio_dev;
473 int ret;
474
475 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
476 if (!indio_dev)
477 return -ENOMEM;
478 chip = iio_priv(indio_dev);
479
480 if (pdata && pdata->use_external_ref) {
481 chip->reg = devm_regulator_get(&client->dev, "vref");
482 if (IS_ERR(chip->reg))
483 return PTR_ERR(chip->reg);
484
485 ret = regulator_enable(chip->reg);
486 if (ret)
487 return ret;
488 }
489
490 mutex_init(&chip->state_lock);
491 /* this is only used for device removal purposes */
492 i2c_set_clientdata(client, indio_dev);
493
494 chip->client = client;
495
496 chip->command = AD7291_NOISE_DELAY |
497 AD7291_T_SENSE_MASK | /* Tsense always enabled */
498 AD7291_ALERT_POLARITY; /* set irq polarity low level */
499
500 if (pdata && pdata->use_external_ref)
501 chip->command |= AD7291_EXT_REF;
502
503 indio_dev->name = id->name;
504 indio_dev->channels = ad7291_channels;
505 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
506
507 indio_dev->dev.parent = &client->dev;
508 indio_dev->info = &ad7291_info;
509 indio_dev->modes = INDIO_DIRECT_MODE;
510
511 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
512 if (ret) {
513 ret = -EIO;
514 goto error_disable_reg;
515 }
516
517 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
518 if (ret) {
519 ret = -EIO;
520 goto error_disable_reg;
521 }
522
523 if (client->irq > 0) {
524 ret = request_threaded_irq(client->irq,
525 NULL,
526 &ad7291_event_handler,
527 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
528 id->name,
529 indio_dev);
530 if (ret)
531 goto error_disable_reg;
532 }
533
534 ret = iio_device_register(indio_dev);
535 if (ret)
536 goto error_unreg_irq;
537
538 return 0;
539
540error_unreg_irq:
541 if (client->irq)
542 free_irq(client->irq, indio_dev);
543error_disable_reg:
544 if (chip->reg)
545 regulator_disable(chip->reg);
546
547 return ret;
548}
549
550static int ad7291_remove(struct i2c_client *client)
551{
552 struct iio_dev *indio_dev = i2c_get_clientdata(client);
553 struct ad7291_chip_info *chip = iio_priv(indio_dev);
554
555 iio_device_unregister(indio_dev);
556
557 if (client->irq)
558 free_irq(client->irq, indio_dev);
559
560 if (chip->reg)
561 regulator_disable(chip->reg);
562
563 return 0;
564}
565
566static const struct i2c_device_id ad7291_id[] = {
567 { "ad7291", 0 },
568 {}
569};
570
571MODULE_DEVICE_TABLE(i2c, ad7291_id);
572
573static struct i2c_driver ad7291_driver = {
574 .driver = {
575 .name = KBUILD_MODNAME,
576 },
577 .probe = ad7291_probe,
578 .remove = ad7291_remove,
579 .id_table = ad7291_id,
580};
581module_i2c_driver(ad7291_driver);
582
583MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
584MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
585MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/ad7298.c b/drivers/iio/adc/ad7298.c
index 2a3b65c74af9..4a8c0a2f49b6 100644
--- a/drivers/iio/adc/ad7298.c
+++ b/drivers/iio/adc/ad7298.c
@@ -16,6 +16,7 @@
16#include <linux/delay.h> 16#include <linux/delay.h>
17#include <linux/module.h> 17#include <linux/module.h>
18#include <linux/interrupt.h> 18#include <linux/interrupt.h>
19#include <linux/bitops.h>
19 20
20#include <linux/iio/iio.h> 21#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h> 22#include <linux/iio/sysfs.h>
@@ -25,23 +26,19 @@
25 26
26#include <linux/platform_data/ad7298.h> 27#include <linux/platform_data/ad7298.h>
27 28
28#define AD7298_WRITE (1 << 15) /* write to the control register */ 29#define AD7298_WRITE BIT(15) /* write to the control register */
29#define AD7298_REPEAT (1 << 14) /* repeated conversion enable */ 30#define AD7298_REPEAT BIT(14) /* repeated conversion enable */
30#define AD7298_CH(x) (1 << (13 - (x))) /* channel select */ 31#define AD7298_CH(x) BIT(13 - (x)) /* channel select */
31#define AD7298_TSENSE (1 << 5) /* temperature conversion enable */ 32#define AD7298_TSENSE BIT(5) /* temperature conversion enable */
32#define AD7298_EXTREF (1 << 2) /* external reference enable */ 33#define AD7298_EXTREF BIT(2) /* external reference enable */
33#define AD7298_TAVG (1 << 1) /* temperature sensor averaging enable */ 34#define AD7298_TAVG BIT(1) /* temperature sensor averaging enable */
34#define AD7298_PDD (1 << 0) /* partial power down enable */ 35#define AD7298_PDD BIT(0) /* partial power down enable */
35 36
36#define AD7298_MAX_CHAN 8 37#define AD7298_MAX_CHAN 8
37#define AD7298_BITS 12
38#define AD7298_STORAGE_BITS 16
39#define AD7298_INTREF_mV 2500 38#define AD7298_INTREF_mV 2500
40 39
41#define AD7298_CH_TEMP 9 40#define AD7298_CH_TEMP 9
42 41
43#define RES_MASK(bits) ((1 << (bits)) - 1)
44
45struct ad7298_state { 42struct ad7298_state {
46 struct spi_device *spi; 43 struct spi_device *spi;
47 struct regulator *reg; 44 struct regulator *reg;
@@ -257,7 +254,7 @@ static int ad7298_read_raw(struct iio_dev *indio_dev,
257 return ret; 254 return ret;
258 255
259 if (chan->address != AD7298_CH_TEMP) 256 if (chan->address != AD7298_CH_TEMP)
260 *val = ret & RES_MASK(AD7298_BITS); 257 *val = ret & GENMASK(chan->scan_type.realbits - 1, 0);
261 258
262 return IIO_VAL_INT; 259 return IIO_VAL_INT;
263 case IIO_CHAN_INFO_SCALE: 260 case IIO_CHAN_INFO_SCALE:
diff --git a/drivers/iio/adc/ad7476.c b/drivers/iio/adc/ad7476.c
index d141d452c3d1..ce400ec176f1 100644
--- a/drivers/iio/adc/ad7476.c
+++ b/drivers/iio/adc/ad7476.c
@@ -14,6 +14,7 @@
14#include <linux/regulator/consumer.h> 14#include <linux/regulator/consumer.h>
15#include <linux/err.h> 15#include <linux/err.h>
16#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/bitops.h>
17 18
18#include <linux/iio/iio.h> 19#include <linux/iio/iio.h>
19#include <linux/iio/sysfs.h> 20#include <linux/iio/sysfs.h>
@@ -21,8 +22,6 @@
21#include <linux/iio/trigger_consumer.h> 22#include <linux/iio/trigger_consumer.h>
22#include <linux/iio/triggered_buffer.h> 23#include <linux/iio/triggered_buffer.h>
23 24
24#define RES_MASK(bits) ((1 << (bits)) - 1)
25
26struct ad7476_state; 25struct ad7476_state;
27 26
28struct ad7476_chip_info { 27struct ad7476_chip_info {
@@ -117,7 +116,7 @@ static int ad7476_read_raw(struct iio_dev *indio_dev,
117 if (ret < 0) 116 if (ret < 0)
118 return ret; 117 return ret;
119 *val = (ret >> st->chip_info->channel[0].scan_type.shift) & 118 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
120 RES_MASK(st->chip_info->channel[0].scan_type.realbits); 119 GENMASK(st->chip_info->channel[0].scan_type.realbits - 1, 0);
121 return IIO_VAL_INT; 120 return IIO_VAL_INT;
122 case IIO_CHAN_INFO_SCALE: 121 case IIO_CHAN_INFO_SCALE:
123 if (!st->chip_info->int_vref_uv) { 122 if (!st->chip_info->int_vref_uv) {
diff --git a/drivers/iio/adc/ad7887.c b/drivers/iio/adc/ad7887.c
index 749a6cadab8b..2fd012ee99f5 100644
--- a/drivers/iio/adc/ad7887.c
+++ b/drivers/iio/adc/ad7887.c
@@ -15,6 +15,7 @@
15#include <linux/err.h> 15#include <linux/err.h>
16#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/interrupt.h> 17#include <linux/interrupt.h>
18#include <linux/bitops.h>
18 19
19#include <linux/iio/iio.h> 20#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h> 21#include <linux/iio/sysfs.h>
@@ -25,14 +26,14 @@
25 26
26#include <linux/platform_data/ad7887.h> 27#include <linux/platform_data/ad7887.h>
27 28
28#define AD7887_REF_DIS (1 << 5) /* on-chip reference disable */ 29#define AD7887_REF_DIS BIT(5) /* on-chip reference disable */
29#define AD7887_DUAL (1 << 4) /* dual-channel mode */ 30#define AD7887_DUAL BIT(4) /* dual-channel mode */
30#define AD7887_CH_AIN1 (1 << 3) /* convert on channel 1, DUAL=1 */ 31#define AD7887_CH_AIN1 BIT(3) /* convert on channel 1, DUAL=1 */
31#define AD7887_CH_AIN0 (0 << 3) /* convert on channel 0, DUAL=0,1 */ 32#define AD7887_CH_AIN0 0 /* convert on channel 0, DUAL=0,1 */
32#define AD7887_PM_MODE1 (0) /* CS based shutdown */ 33#define AD7887_PM_MODE1 0 /* CS based shutdown */
33#define AD7887_PM_MODE2 (1) /* full on */ 34#define AD7887_PM_MODE2 1 /* full on */
34#define AD7887_PM_MODE3 (2) /* auto shutdown after conversion */ 35#define AD7887_PM_MODE3 2 /* auto shutdown after conversion */
35#define AD7887_PM_MODE4 (3) /* standby mode */ 36#define AD7887_PM_MODE4 3 /* standby mode */
36 37
37enum ad7887_channels { 38enum ad7887_channels {
38 AD7887_CH0, 39 AD7887_CH0,
@@ -40,8 +41,6 @@ enum ad7887_channels {
40 AD7887_CH1, 41 AD7887_CH1,
41}; 42};
42 43
43#define RES_MASK(bits) ((1 << (bits)) - 1)
44
45/** 44/**
46 * struct ad7887_chip_info - chip specifc information 45 * struct ad7887_chip_info - chip specifc information
47 * @int_vref_mv: the internal reference voltage 46 * @int_vref_mv: the internal reference voltage
@@ -167,7 +166,7 @@ static int ad7887_read_raw(struct iio_dev *indio_dev,
167 if (ret < 0) 166 if (ret < 0)
168 return ret; 167 return ret;
169 *val = ret >> chan->scan_type.shift; 168 *val = ret >> chan->scan_type.shift;
170 *val &= RES_MASK(chan->scan_type.realbits); 169 *val &= GENMASK(chan->scan_type.realbits - 1, 0);
171 return IIO_VAL_INT; 170 return IIO_VAL_INT;
172 case IIO_CHAN_INFO_SCALE: 171 case IIO_CHAN_INFO_SCALE:
173 if (st->reg) { 172 if (st->reg) {
diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c
index 6eba301ee03d..e37412da15f5 100644
--- a/drivers/iio/adc/ad799x.c
+++ b/drivers/iio/adc/ad799x.c
@@ -32,6 +32,7 @@
32#include <linux/types.h> 32#include <linux/types.h>
33#include <linux/err.h> 33#include <linux/err.h>
34#include <linux/module.h> 34#include <linux/module.h>
35#include <linux/bitops.h>
35 36
36#include <linux/iio/iio.h> 37#include <linux/iio/iio.h>
37#include <linux/iio/sysfs.h> 38#include <linux/iio/sysfs.h>
@@ -41,7 +42,7 @@
41#include <linux/iio/triggered_buffer.h> 42#include <linux/iio/triggered_buffer.h>
42 43
43#define AD799X_CHANNEL_SHIFT 4 44#define AD799X_CHANNEL_SHIFT 4
44#define AD799X_STORAGEBITS 16 45
45/* 46/*
46 * AD7991, AD7995 and AD7999 defines 47 * AD7991, AD7995 and AD7999 defines
47 */ 48 */
@@ -55,10 +56,10 @@
55 * AD7992, AD7993, AD7994, AD7997 and AD7998 defines 56 * AD7992, AD7993, AD7994, AD7997 and AD7998 defines
56 */ 57 */
57 58
58#define AD7998_FLTR 0x08 59#define AD7998_FLTR BIT(3)
59#define AD7998_ALERT_EN 0x04 60#define AD7998_ALERT_EN BIT(2)
60#define AD7998_BUSY_ALERT 0x02 61#define AD7998_BUSY_ALERT BIT(1)
61#define AD7998_BUSY_ALERT_POL 0x01 62#define AD7998_BUSY_ALERT_POL BIT(0)
62 63
63#define AD7998_CONV_RES_REG 0x0 64#define AD7998_CONV_RES_REG 0x0
64#define AD7998_ALERT_STAT_REG 0x1 65#define AD7998_ALERT_STAT_REG 0x1
@@ -69,7 +70,7 @@
69#define AD7998_DATAHIGH_REG(x) ((x) * 3 + 0x5) 70#define AD7998_DATAHIGH_REG(x) ((x) * 3 + 0x5)
70#define AD7998_HYST_REG(x) ((x) * 3 + 0x6) 71#define AD7998_HYST_REG(x) ((x) * 3 + 0x6)
71 72
72#define AD7998_CYC_MASK 0x7 73#define AD7998_CYC_MASK GENMASK(2, 0)
73#define AD7998_CYC_DIS 0x0 74#define AD7998_CYC_DIS 0x0
74#define AD7998_CYC_TCONF_32 0x1 75#define AD7998_CYC_TCONF_32 0x1
75#define AD7998_CYC_TCONF_64 0x2 76#define AD7998_CYC_TCONF_64 0x2
@@ -85,10 +86,8 @@
85 * AD7997 and AD7997 defines 86 * AD7997 and AD7997 defines
86 */ 87 */
87 88
88#define AD7997_8_READ_SINGLE 0x80 89#define AD7997_8_READ_SINGLE BIT(7)
89#define AD7997_8_READ_SEQUENCE 0x70 90#define AD7997_8_READ_SEQUENCE (BIT(6) | BIT(5) | BIT(4))
90/* TODO: move this into a common header */
91#define RES_MASK(bits) ((1 << (bits)) - 1)
92 91
93enum { 92enum {
94 ad7991, 93 ad7991,
@@ -102,23 +101,32 @@ enum {
102}; 101};
103 102
104/** 103/**
105 * struct ad799x_chip_info - chip specific information 104 * struct ad799x_chip_config - chip specific information
106 * @channel: channel specification 105 * @channel: channel specification
107 * @num_channels: number of channels
108 * @monitor_mode: whether the chip supports monitor interrupts
109 * @default_config: device default configuration 106 * @default_config: device default configuration
110 * @event_attrs: pointer to the monitor event attribute group 107 * @info: pointer to iio_info struct
111 */ 108 */
112struct ad799x_chip_info { 109struct ad799x_chip_config {
113 struct iio_chan_spec channel[9]; 110 const struct iio_chan_spec channel[9];
114 int num_channels;
115 u16 default_config; 111 u16 default_config;
116 const struct iio_info *info; 112 const struct iio_info *info;
117}; 113};
118 114
115/**
116 * struct ad799x_chip_info - chip specific information
117 * @num_channels: number of channels
118 * @noirq_config: device configuration w/o IRQ
119 * @irq_config: device configuration w/IRQ
120 */
121struct ad799x_chip_info {
122 int num_channels;
123 const struct ad799x_chip_config noirq_config;
124 const struct ad799x_chip_config irq_config;
125};
126
119struct ad799x_state { 127struct ad799x_state {
120 struct i2c_client *client; 128 struct i2c_client *client;
121 const struct ad799x_chip_info *chip_info; 129 const struct ad799x_chip_config *chip_config;
122 struct regulator *reg; 130 struct regulator *reg;
123 struct regulator *vref; 131 struct regulator *vref;
124 unsigned id; 132 unsigned id;
@@ -128,6 +136,30 @@ struct ad799x_state {
128 unsigned int transfer_size; 136 unsigned int transfer_size;
129}; 137};
130 138
139static int ad799x_write_config(struct ad799x_state *st, u16 val)
140{
141 switch (st->id) {
142 case ad7997:
143 case ad7998:
144 return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG,
145 val);
146 default:
147 return i2c_smbus_write_byte_data(st->client, AD7998_CONF_REG,
148 val);
149 }
150}
151
152static int ad799x_read_config(struct ad799x_state *st)
153{
154 switch (st->id) {
155 case ad7997:
156 case ad7998:
157 return i2c_smbus_read_word_swapped(st->client, AD7998_CONF_REG);
158 default:
159 return i2c_smbus_read_byte_data(st->client, AD7998_CONF_REG);
160 }
161}
162
131/** 163/**
132 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer 164 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
133 * 165 *
@@ -176,66 +208,7 @@ out:
176 return IRQ_HANDLED; 208 return IRQ_HANDLED;
177} 209}
178 210
179/* 211static int ad799x_update_scan_mode(struct iio_dev *indio_dev,
180 * ad799x register access by I2C
181 */
182static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
183{
184 struct i2c_client *client = st->client;
185 int ret = 0;
186
187 ret = i2c_smbus_read_word_swapped(client, reg);
188 if (ret < 0) {
189 dev_err(&client->dev, "I2C read error\n");
190 return ret;
191 }
192
193 *data = (u16)ret;
194
195 return 0;
196}
197
198static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
199{
200 struct i2c_client *client = st->client;
201 int ret = 0;
202
203 ret = i2c_smbus_read_byte_data(client, reg);
204 if (ret < 0) {
205 dev_err(&client->dev, "I2C read error\n");
206 return ret;
207 }
208
209 *data = (u8)ret;
210
211 return 0;
212}
213
214static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
215{
216 struct i2c_client *client = st->client;
217 int ret = 0;
218
219 ret = i2c_smbus_write_word_swapped(client, reg, data);
220 if (ret < 0)
221 dev_err(&client->dev, "I2C write error\n");
222
223 return ret;
224}
225
226static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
227{
228 struct i2c_client *client = st->client;
229 int ret = 0;
230
231 ret = i2c_smbus_write_byte_data(client, reg, data);
232 if (ret < 0)
233 dev_err(&client->dev, "I2C write error\n");
234
235 return ret;
236}
237
238static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
239 const unsigned long *scan_mask) 212 const unsigned long *scan_mask)
240{ 213{
241 struct ad799x_state *st = iio_priv(indio_dev); 214 struct ad799x_state *st = iio_priv(indio_dev);
@@ -248,33 +221,33 @@ static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
248 st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2; 221 st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
249 222
250 switch (st->id) { 223 switch (st->id) {
224 case ad7992:
225 case ad7993:
226 case ad7994:
251 case ad7997: 227 case ad7997:
252 case ad7998: 228 case ad7998:
253 return ad799x_i2c_write16(st, AD7998_CONF_REG, 229 st->config &= ~(GENMASK(7, 0) << AD799X_CHANNEL_SHIFT);
254 st->config | (*scan_mask << AD799X_CHANNEL_SHIFT)); 230 st->config |= (*scan_mask << AD799X_CHANNEL_SHIFT);
231 return ad799x_write_config(st, st->config);
255 default: 232 default:
256 break; 233 return 0;
257 } 234 }
258
259 return 0;
260} 235}
261 236
262static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch) 237static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
263{ 238{
264 u16 rxbuf;
265 u8 cmd; 239 u8 cmd;
266 int ret;
267 240
268 switch (st->id) { 241 switch (st->id) {
269 case ad7991: 242 case ad7991:
270 case ad7995: 243 case ad7995:
271 case ad7999: 244 case ad7999:
272 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT); 245 cmd = st->config | (BIT(ch) << AD799X_CHANNEL_SHIFT);
273 break; 246 break;
274 case ad7992: 247 case ad7992:
275 case ad7993: 248 case ad7993:
276 case ad7994: 249 case ad7994:
277 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT; 250 cmd = BIT(ch) << AD799X_CHANNEL_SHIFT;
278 break; 251 break;
279 case ad7997: 252 case ad7997:
280 case ad7998: 253 case ad7998:
@@ -284,11 +257,7 @@ static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
284 return -EINVAL; 257 return -EINVAL;
285 } 258 }
286 259
287 ret = ad799x_i2c_read16(st, cmd, &rxbuf); 260 return i2c_smbus_read_word_swapped(st->client, cmd);
288 if (ret < 0)
289 return ret;
290
291 return rxbuf;
292} 261}
293 262
294static int ad799x_read_raw(struct iio_dev *indio_dev, 263static int ad799x_read_raw(struct iio_dev *indio_dev,
@@ -312,7 +281,7 @@ static int ad799x_read_raw(struct iio_dev *indio_dev,
312 if (ret < 0) 281 if (ret < 0)
313 return ret; 282 return ret;
314 *val = (ret >> chan->scan_type.shift) & 283 *val = (ret >> chan->scan_type.shift) &
315 RES_MASK(chan->scan_type.realbits); 284 GENMASK(chan->scan_type.realbits - 1, 0);
316 return IIO_VAL_INT; 285 return IIO_VAL_INT;
317 case IIO_CHAN_INFO_SCALE: 286 case IIO_CHAN_INFO_SCALE:
318 ret = regulator_get_voltage(st->vref); 287 ret = regulator_get_voltage(st->vref);
@@ -333,6 +302,7 @@ static const unsigned int ad7998_frequencies[] = {
333 [AD7998_CYC_TCONF_1024] = 488, 302 [AD7998_CYC_TCONF_1024] = 488,
334 [AD7998_CYC_TCONF_2048] = 244, 303 [AD7998_CYC_TCONF_2048] = 244,
335}; 304};
305
336static ssize_t ad799x_read_frequency(struct device *dev, 306static ssize_t ad799x_read_frequency(struct device *dev,
337 struct device_attribute *attr, 307 struct device_attribute *attr,
338 char *buf) 308 char *buf)
@@ -340,15 +310,11 @@ static ssize_t ad799x_read_frequency(struct device *dev,
340 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 310 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
341 struct ad799x_state *st = iio_priv(indio_dev); 311 struct ad799x_state *st = iio_priv(indio_dev);
342 312
343 int ret; 313 int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
344 u8 val; 314 if (ret < 0)
345 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
346 if (ret)
347 return ret; 315 return ret;
348 316
349 val &= AD7998_CYC_MASK; 317 return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]);
350
351 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
352} 318}
353 319
354static ssize_t ad799x_write_frequency(struct device *dev, 320static ssize_t ad799x_write_frequency(struct device *dev,
@@ -361,18 +327,17 @@ static ssize_t ad799x_write_frequency(struct device *dev,
361 327
362 long val; 328 long val;
363 int ret, i; 329 int ret, i;
364 u8 t;
365 330
366 ret = kstrtol(buf, 10, &val); 331 ret = kstrtol(buf, 10, &val);
367 if (ret) 332 if (ret)
368 return ret; 333 return ret;
369 334
370 mutex_lock(&indio_dev->mlock); 335 mutex_lock(&indio_dev->mlock);
371 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t); 336 ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
372 if (ret) 337 if (ret < 0)
373 goto error_ret_mutex; 338 goto error_ret_mutex;
374 /* Wipe the bits clean */ 339 /* Wipe the bits clean */
375 t &= ~AD7998_CYC_MASK; 340 ret &= ~AD7998_CYC_MASK;
376 341
377 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++) 342 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
378 if (val == ad7998_frequencies[i]) 343 if (val == ad7998_frequencies[i])
@@ -381,13 +346,17 @@ static ssize_t ad799x_write_frequency(struct device *dev,
381 ret = -EINVAL; 346 ret = -EINVAL;
382 goto error_ret_mutex; 347 goto error_ret_mutex;
383 } 348 }
384 t |= i; 349
385 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t); 350 ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG,
351 ret | i);
352 if (ret < 0)
353 goto error_ret_mutex;
354 ret = len;
386 355
387error_ret_mutex: 356error_ret_mutex:
388 mutex_unlock(&indio_dev->mlock); 357 mutex_unlock(&indio_dev->mlock);
389 358
390 return ret ? ret : len; 359 return ret;
391} 360}
392 361
393static int ad799x_read_event_config(struct iio_dev *indio_dev, 362static int ad799x_read_event_config(struct iio_dev *indio_dev,
@@ -395,7 +364,48 @@ static int ad799x_read_event_config(struct iio_dev *indio_dev,
395 enum iio_event_type type, 364 enum iio_event_type type,
396 enum iio_event_direction dir) 365 enum iio_event_direction dir)
397{ 366{
398 return 1; 367 struct ad799x_state *st = iio_priv(indio_dev);
368
369 if (!(st->config & AD7998_ALERT_EN))
370 return 0;
371
372 if ((st->config >> AD799X_CHANNEL_SHIFT) & BIT(chan->scan_index))
373 return 1;
374
375 return 0;
376}
377
378static int ad799x_write_event_config(struct iio_dev *indio_dev,
379 const struct iio_chan_spec *chan,
380 enum iio_event_type type,
381 enum iio_event_direction dir,
382 int state)
383{
384 struct ad799x_state *st = iio_priv(indio_dev);
385 int ret;
386
387 mutex_lock(&indio_dev->mlock);
388 if (iio_buffer_enabled(indio_dev)) {
389 ret = -EBUSY;
390 goto done;
391 }
392
393 if (state)
394 st->config |= BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT;
395 else
396 st->config &= ~(BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT);
397
398 if (st->config >> AD799X_CHANNEL_SHIFT)
399 st->config |= AD7998_ALERT_EN;
400 else
401 st->config &= ~AD7998_ALERT_EN;
402
403 ret = ad799x_write_config(st, st->config);
404
405done:
406 mutex_unlock(&indio_dev->mlock);
407
408 return ret;
399} 409}
400 410
401static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan, 411static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
@@ -427,11 +437,12 @@ static int ad799x_write_event_value(struct iio_dev *indio_dev,
427 int ret; 437 int ret;
428 struct ad799x_state *st = iio_priv(indio_dev); 438 struct ad799x_state *st = iio_priv(indio_dev);
429 439
430 if (val < 0 || val > RES_MASK(chan->scan_type.realbits)) 440 if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0))
431 return -EINVAL; 441 return -EINVAL;
432 442
433 mutex_lock(&indio_dev->mlock); 443 mutex_lock(&indio_dev->mlock);
434 ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info), 444 ret = i2c_smbus_write_word_swapped(st->client,
445 ad799x_threshold_reg(chan, dir, info),
435 val << chan->scan_type.shift); 446 val << chan->scan_type.shift);
436 mutex_unlock(&indio_dev->mlock); 447 mutex_unlock(&indio_dev->mlock);
437 448
@@ -447,16 +458,15 @@ static int ad799x_read_event_value(struct iio_dev *indio_dev,
447{ 458{
448 int ret; 459 int ret;
449 struct ad799x_state *st = iio_priv(indio_dev); 460 struct ad799x_state *st = iio_priv(indio_dev);
450 u16 valin;
451 461
452 mutex_lock(&indio_dev->mlock); 462 mutex_lock(&indio_dev->mlock);
453 ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info), 463 ret = i2c_smbus_read_word_swapped(st->client,
454 &valin); 464 ad799x_threshold_reg(chan, dir, info));
455 mutex_unlock(&indio_dev->mlock); 465 mutex_unlock(&indio_dev->mlock);
456 if (ret < 0) 466 if (ret < 0)
457 return ret; 467 return ret;
458 *val = (valin >> chan->scan_type.shift) & 468 *val = (ret >> chan->scan_type.shift) &
459 RES_MASK(chan->scan_type.realbits); 469 GENMASK(chan->scan_type.realbits - 1 , 0);
460 470
461 return IIO_VAL_INT; 471 return IIO_VAL_INT;
462} 472}
@@ -465,20 +475,18 @@ static irqreturn_t ad799x_event_handler(int irq, void *private)
465{ 475{
466 struct iio_dev *indio_dev = private; 476 struct iio_dev *indio_dev = private;
467 struct ad799x_state *st = iio_priv(private); 477 struct ad799x_state *st = iio_priv(private);
468 u8 status;
469 int i, ret; 478 int i, ret;
470 479
471 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status); 480 ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG);
472 if (ret) 481 if (ret <= 0)
473 goto done; 482 goto done;
474 483
475 if (!status) 484 if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG,
485 AD7998_ALERT_STAT_CLEAR) < 0)
476 goto done; 486 goto done;
477 487
478 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
479
480 for (i = 0; i < 8; i++) { 488 for (i = 0; i < 8; i++) {
481 if (status & (1 << i)) 489 if (ret & BIT(i))
482 iio_push_event(indio_dev, 490 iio_push_event(indio_dev,
483 i & 0x1 ? 491 i & 0x1 ?
484 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 492 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
@@ -517,14 +525,21 @@ static const struct iio_info ad7991_info = {
517 .driver_module = THIS_MODULE, 525 .driver_module = THIS_MODULE,
518}; 526};
519 527
520static const struct iio_info ad7993_4_7_8_info = { 528static const struct iio_info ad7993_4_7_8_noirq_info = {
529 .read_raw = &ad799x_read_raw,
530 .driver_module = THIS_MODULE,
531 .update_scan_mode = ad799x_update_scan_mode,
532};
533
534static const struct iio_info ad7993_4_7_8_irq_info = {
521 .read_raw = &ad799x_read_raw, 535 .read_raw = &ad799x_read_raw,
522 .event_attrs = &ad799x_event_attrs_group, 536 .event_attrs = &ad799x_event_attrs_group,
523 .read_event_config = &ad799x_read_event_config, 537 .read_event_config = &ad799x_read_event_config,
538 .write_event_config = &ad799x_write_event_config,
524 .read_event_value = &ad799x_read_event_value, 539 .read_event_value = &ad799x_read_event_value,
525 .write_event_value = &ad799x_write_event_value, 540 .write_event_value = &ad799x_write_event_value,
526 .driver_module = THIS_MODULE, 541 .driver_module = THIS_MODULE,
527 .update_scan_mode = ad7997_8_update_scan_mode, 542 .update_scan_mode = ad799x_update_scan_mode,
528}; 543};
529 544
530static const struct iio_event_spec ad799x_events[] = { 545static const struct iio_event_spec ad799x_events[] = {
@@ -572,103 +587,175 @@ static const struct iio_event_spec ad799x_events[] = {
572 587
573static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { 588static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
574 [ad7991] = { 589 [ad7991] = {
575 .channel = {
576 AD799X_CHANNEL(0, 12),
577 AD799X_CHANNEL(1, 12),
578 AD799X_CHANNEL(2, 12),
579 AD799X_CHANNEL(3, 12),
580 IIO_CHAN_SOFT_TIMESTAMP(4),
581 },
582 .num_channels = 5, 590 .num_channels = 5,
583 .info = &ad7991_info, 591 .noirq_config = {
592 .channel = {
593 AD799X_CHANNEL(0, 12),
594 AD799X_CHANNEL(1, 12),
595 AD799X_CHANNEL(2, 12),
596 AD799X_CHANNEL(3, 12),
597 IIO_CHAN_SOFT_TIMESTAMP(4),
598 },
599 .info = &ad7991_info,
600 },
584 }, 601 },
585 [ad7995] = { 602 [ad7995] = {
586 .channel = {
587 AD799X_CHANNEL(0, 10),
588 AD799X_CHANNEL(1, 10),
589 AD799X_CHANNEL(2, 10),
590 AD799X_CHANNEL(3, 10),
591 IIO_CHAN_SOFT_TIMESTAMP(4),
592 },
593 .num_channels = 5, 603 .num_channels = 5,
594 .info = &ad7991_info, 604 .noirq_config = {
605 .channel = {
606 AD799X_CHANNEL(0, 10),
607 AD799X_CHANNEL(1, 10),
608 AD799X_CHANNEL(2, 10),
609 AD799X_CHANNEL(3, 10),
610 IIO_CHAN_SOFT_TIMESTAMP(4),
611 },
612 .info = &ad7991_info,
613 },
595 }, 614 },
596 [ad7999] = { 615 [ad7999] = {
597 .channel = {
598 AD799X_CHANNEL(0, 8),
599 AD799X_CHANNEL(1, 8),
600 AD799X_CHANNEL(2, 8),
601 AD799X_CHANNEL(3, 8),
602 IIO_CHAN_SOFT_TIMESTAMP(4),
603 },
604 .num_channels = 5, 616 .num_channels = 5,
605 .info = &ad7991_info, 617 .noirq_config = {
618 .channel = {
619 AD799X_CHANNEL(0, 8),
620 AD799X_CHANNEL(1, 8),
621 AD799X_CHANNEL(2, 8),
622 AD799X_CHANNEL(3, 8),
623 IIO_CHAN_SOFT_TIMESTAMP(4),
624 },
625 .info = &ad7991_info,
626 },
606 }, 627 },
607 [ad7992] = { 628 [ad7992] = {
608 .channel = {
609 AD799X_CHANNEL_WITH_EVENTS(0, 12),
610 AD799X_CHANNEL_WITH_EVENTS(1, 12),
611 IIO_CHAN_SOFT_TIMESTAMP(3),
612 },
613 .num_channels = 3, 629 .num_channels = 3,
614 .default_config = AD7998_ALERT_EN, 630 .noirq_config = {
615 .info = &ad7993_4_7_8_info, 631 .channel = {
632 AD799X_CHANNEL(0, 12),
633 AD799X_CHANNEL(1, 12),
634 IIO_CHAN_SOFT_TIMESTAMP(3),
635 },
636 .info = &ad7993_4_7_8_noirq_info,
637 },
638 .irq_config = {
639 .channel = {
640 AD799X_CHANNEL_WITH_EVENTS(0, 12),
641 AD799X_CHANNEL_WITH_EVENTS(1, 12),
642 IIO_CHAN_SOFT_TIMESTAMP(3),
643 },
644 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
645 .info = &ad7993_4_7_8_irq_info,
646 },
616 }, 647 },
617 [ad7993] = { 648 [ad7993] = {
618 .channel = {
619 AD799X_CHANNEL_WITH_EVENTS(0, 10),
620 AD799X_CHANNEL_WITH_EVENTS(1, 10),
621 AD799X_CHANNEL_WITH_EVENTS(2, 10),
622 AD799X_CHANNEL_WITH_EVENTS(3, 10),
623 IIO_CHAN_SOFT_TIMESTAMP(4),
624 },
625 .num_channels = 5, 649 .num_channels = 5,
626 .default_config = AD7998_ALERT_EN, 650 .noirq_config = {
627 .info = &ad7993_4_7_8_info, 651 .channel = {
652 AD799X_CHANNEL(0, 10),
653 AD799X_CHANNEL(1, 10),
654 AD799X_CHANNEL(2, 10),
655 AD799X_CHANNEL(3, 10),
656 IIO_CHAN_SOFT_TIMESTAMP(4),
657 },
658 .info = &ad7993_4_7_8_noirq_info,
659 },
660 .irq_config = {
661 .channel = {
662 AD799X_CHANNEL_WITH_EVENTS(0, 10),
663 AD799X_CHANNEL_WITH_EVENTS(1, 10),
664 AD799X_CHANNEL_WITH_EVENTS(2, 10),
665 AD799X_CHANNEL_WITH_EVENTS(3, 10),
666 IIO_CHAN_SOFT_TIMESTAMP(4),
667 },
668 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
669 .info = &ad7993_4_7_8_irq_info,
670 },
628 }, 671 },
629 [ad7994] = { 672 [ad7994] = {
630 .channel = {
631 AD799X_CHANNEL_WITH_EVENTS(0, 12),
632 AD799X_CHANNEL_WITH_EVENTS(1, 12),
633 AD799X_CHANNEL_WITH_EVENTS(2, 12),
634 AD799X_CHANNEL_WITH_EVENTS(3, 12),
635 IIO_CHAN_SOFT_TIMESTAMP(4),
636 },
637 .num_channels = 5, 673 .num_channels = 5,
638 .default_config = AD7998_ALERT_EN, 674 .noirq_config = {
639 .info = &ad7993_4_7_8_info, 675 .channel = {
676 AD799X_CHANNEL(0, 12),
677 AD799X_CHANNEL(1, 12),
678 AD799X_CHANNEL(2, 12),
679 AD799X_CHANNEL(3, 12),
680 IIO_CHAN_SOFT_TIMESTAMP(4),
681 },
682 .info = &ad7993_4_7_8_noirq_info,
683 },
684 .irq_config = {
685 .channel = {
686 AD799X_CHANNEL_WITH_EVENTS(0, 12),
687 AD799X_CHANNEL_WITH_EVENTS(1, 12),
688 AD799X_CHANNEL_WITH_EVENTS(2, 12),
689 AD799X_CHANNEL_WITH_EVENTS(3, 12),
690 IIO_CHAN_SOFT_TIMESTAMP(4),
691 },
692 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
693 .info = &ad7993_4_7_8_irq_info,
694 },
640 }, 695 },
641 [ad7997] = { 696 [ad7997] = {
642 .channel = {
643 AD799X_CHANNEL_WITH_EVENTS(0, 10),
644 AD799X_CHANNEL_WITH_EVENTS(1, 10),
645 AD799X_CHANNEL_WITH_EVENTS(2, 10),
646 AD799X_CHANNEL_WITH_EVENTS(3, 10),
647 AD799X_CHANNEL(4, 10),
648 AD799X_CHANNEL(5, 10),
649 AD799X_CHANNEL(6, 10),
650 AD799X_CHANNEL(7, 10),
651 IIO_CHAN_SOFT_TIMESTAMP(8),
652 },
653 .num_channels = 9, 697 .num_channels = 9,
654 .default_config = AD7998_ALERT_EN, 698 .noirq_config = {
655 .info = &ad7993_4_7_8_info, 699 .channel = {
700 AD799X_CHANNEL(0, 10),
701 AD799X_CHANNEL(1, 10),
702 AD799X_CHANNEL(2, 10),
703 AD799X_CHANNEL(3, 10),
704 AD799X_CHANNEL(4, 10),
705 AD799X_CHANNEL(5, 10),
706 AD799X_CHANNEL(6, 10),
707 AD799X_CHANNEL(7, 10),
708 IIO_CHAN_SOFT_TIMESTAMP(8),
709 },
710 .info = &ad7993_4_7_8_noirq_info,
711 },
712 .irq_config = {
713 .channel = {
714 AD799X_CHANNEL_WITH_EVENTS(0, 10),
715 AD799X_CHANNEL_WITH_EVENTS(1, 10),
716 AD799X_CHANNEL_WITH_EVENTS(2, 10),
717 AD799X_CHANNEL_WITH_EVENTS(3, 10),
718 AD799X_CHANNEL(4, 10),
719 AD799X_CHANNEL(5, 10),
720 AD799X_CHANNEL(6, 10),
721 AD799X_CHANNEL(7, 10),
722 IIO_CHAN_SOFT_TIMESTAMP(8),
723 },
724 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
725 .info = &ad7993_4_7_8_irq_info,
726 },
656 }, 727 },
657 [ad7998] = { 728 [ad7998] = {
658 .channel = {
659 AD799X_CHANNEL_WITH_EVENTS(0, 12),
660 AD799X_CHANNEL_WITH_EVENTS(1, 12),
661 AD799X_CHANNEL_WITH_EVENTS(2, 12),
662 AD799X_CHANNEL_WITH_EVENTS(3, 12),
663 AD799X_CHANNEL(4, 12),
664 AD799X_CHANNEL(5, 12),
665 AD799X_CHANNEL(6, 12),
666 AD799X_CHANNEL(7, 12),
667 IIO_CHAN_SOFT_TIMESTAMP(8),
668 },
669 .num_channels = 9, 729 .num_channels = 9,
670 .default_config = AD7998_ALERT_EN, 730 .noirq_config = {
671 .info = &ad7993_4_7_8_info, 731 .channel = {
732 AD799X_CHANNEL(0, 12),
733 AD799X_CHANNEL(1, 12),
734 AD799X_CHANNEL(2, 12),
735 AD799X_CHANNEL(3, 12),
736 AD799X_CHANNEL(4, 12),
737 AD799X_CHANNEL(5, 12),
738 AD799X_CHANNEL(6, 12),
739 AD799X_CHANNEL(7, 12),
740 IIO_CHAN_SOFT_TIMESTAMP(8),
741 },
742 .info = &ad7993_4_7_8_noirq_info,
743 },
744 .irq_config = {
745 .channel = {
746 AD799X_CHANNEL_WITH_EVENTS(0, 12),
747 AD799X_CHANNEL_WITH_EVENTS(1, 12),
748 AD799X_CHANNEL_WITH_EVENTS(2, 12),
749 AD799X_CHANNEL_WITH_EVENTS(3, 12),
750 AD799X_CHANNEL(4, 12),
751 AD799X_CHANNEL(5, 12),
752 AD799X_CHANNEL(6, 12),
753 AD799X_CHANNEL(7, 12),
754 IIO_CHAN_SOFT_TIMESTAMP(8),
755 },
756 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT,
757 .info = &ad7993_4_7_8_irq_info,
758 },
672 }, 759 },
673}; 760};
674 761
@@ -678,6 +765,8 @@ static int ad799x_probe(struct i2c_client *client,
678 int ret; 765 int ret;
679 struct ad799x_state *st; 766 struct ad799x_state *st;
680 struct iio_dev *indio_dev; 767 struct iio_dev *indio_dev;
768 const struct ad799x_chip_info *chip_info =
769 &ad799x_chip_info_tbl[id->driver_data];
681 770
682 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st)); 771 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
683 if (indio_dev == NULL) 772 if (indio_dev == NULL)
@@ -688,8 +777,10 @@ static int ad799x_probe(struct i2c_client *client,
688 i2c_set_clientdata(client, indio_dev); 777 i2c_set_clientdata(client, indio_dev);
689 778
690 st->id = id->driver_data; 779 st->id = id->driver_data;
691 st->chip_info = &ad799x_chip_info_tbl[st->id]; 780 if (client->irq > 0 && chip_info->irq_config.info)
692 st->config = st->chip_info->default_config; 781 st->chip_config = &chip_info->irq_config;
782 else
783 st->chip_config = &chip_info->noirq_config;
693 784
694 /* TODO: Add pdata options for filtering and bit delay */ 785 /* TODO: Add pdata options for filtering and bit delay */
695 786
@@ -712,11 +803,19 @@ static int ad799x_probe(struct i2c_client *client,
712 803
713 indio_dev->dev.parent = &client->dev; 804 indio_dev->dev.parent = &client->dev;
714 indio_dev->name = id->name; 805 indio_dev->name = id->name;
715 indio_dev->info = st->chip_info->info; 806 indio_dev->info = st->chip_config->info;
716 807
717 indio_dev->modes = INDIO_DIRECT_MODE; 808 indio_dev->modes = INDIO_DIRECT_MODE;
718 indio_dev->channels = st->chip_info->channel; 809 indio_dev->channels = st->chip_config->channel;
719 indio_dev->num_channels = st->chip_info->num_channels; 810 indio_dev->num_channels = chip_info->num_channels;
811
812 ret = ad799x_write_config(st, st->chip_config->default_config);
813 if (ret < 0)
814 goto error_disable_reg;
815 ret = ad799x_read_config(st);
816 if (ret < 0)
817 goto error_disable_reg;
818 st->config = ret;
720 819
721 ret = iio_triggered_buffer_setup(indio_dev, NULL, 820 ret = iio_triggered_buffer_setup(indio_dev, NULL,
722 &ad799x_trigger_handler, NULL); 821 &ad799x_trigger_handler, NULL);
diff --git a/drivers/iio/adc/ad_sigma_delta.c b/drivers/iio/adc/ad_sigma_delta.c
index 9a4e0e32a771..c55b81f7f970 100644
--- a/drivers/iio/adc/ad_sigma_delta.c
+++ b/drivers/iio/adc/ad_sigma_delta.c
@@ -410,7 +410,7 @@ static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
410 complete(&sigma_delta->completion); 410 complete(&sigma_delta->completion);
411 disable_irq_nosync(irq); 411 disable_irq_nosync(irq);
412 sigma_delta->irq_dis = true; 412 sigma_delta->irq_dis = true;
413 iio_trigger_poll(sigma_delta->trig, iio_get_time_ns()); 413 iio_trigger_poll(sigma_delta->trig);
414 414
415 return IRQ_HANDLED; 415 return IRQ_HANDLED;
416} 416}
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index 2b6a9ce9927c..772e869c280e 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -272,7 +272,7 @@ void handle_adc_eoc_trigger(int irq, struct iio_dev *idev)
272 272
273 if (iio_buffer_enabled(idev)) { 273 if (iio_buffer_enabled(idev)) {
274 disable_irq_nosync(irq); 274 disable_irq_nosync(irq);
275 iio_trigger_poll(idev->trig, iio_get_time_ns()); 275 iio_trigger_poll(idev->trig);
276 } else { 276 } else {
277 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR); 277 st->last_value = at91_adc_readl(st, AT91_ADC_LCDR);
278 st->done = true; 278 st->done = true;
diff --git a/drivers/iio/adc/exynos_adc.c b/drivers/iio/adc/exynos_adc.c
index 010578f1d762..fc9dfc23ecb7 100644
--- a/drivers/iio/adc/exynos_adc.c
+++ b/drivers/iio/adc/exynos_adc.c
@@ -24,6 +24,7 @@
24#include <linux/platform_device.h> 24#include <linux/platform_device.h>
25#include <linux/interrupt.h> 25#include <linux/interrupt.h>
26#include <linux/delay.h> 26#include <linux/delay.h>
27#include <linux/errno.h>
27#include <linux/kernel.h> 28#include <linux/kernel.h>
28#include <linux/slab.h> 29#include <linux/slab.h>
29#include <linux/io.h> 30#include <linux/io.h>
@@ -39,11 +40,6 @@
39#include <linux/iio/machine.h> 40#include <linux/iio/machine.h>
40#include <linux/iio/driver.h> 41#include <linux/iio/driver.h>
41 42
42enum adc_version {
43 ADC_V1,
44 ADC_V2
45};
46
47/* EXYNOS4412/5250 ADC_V1 registers definitions */ 43/* EXYNOS4412/5250 ADC_V1 registers definitions */
48#define ADC_V1_CON(x) ((x) + 0x00) 44#define ADC_V1_CON(x) ((x) + 0x00)
49#define ADC_V1_DLY(x) ((x) + 0x08) 45#define ADC_V1_DLY(x) ((x) + 0x08)
@@ -75,8 +71,9 @@ enum adc_version {
75#define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0) 71#define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
76#define ADC_V2_CON2_ACH_MASK 0xF 72#define ADC_V2_CON2_ACH_MASK 0xF
77 73
78#define MAX_ADC_V2_CHANNELS 10 74#define MAX_ADC_V2_CHANNELS 10
79#define MAX_ADC_V1_CHANNELS 8 75#define MAX_ADC_V1_CHANNELS 8
76#define MAX_EXYNOS3250_ADC_CHANNELS 2
80 77
81/* Bit definitions common for ADC_V1 and ADC_V2 */ 78/* Bit definitions common for ADC_V1 and ADC_V2 */
82#define ADC_CON_EN_START (1u << 0) 79#define ADC_CON_EN_START (1u << 0)
@@ -85,9 +82,12 @@ enum adc_version {
85#define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100)) 82#define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100))
86 83
87struct exynos_adc { 84struct exynos_adc {
85 struct exynos_adc_data *data;
86 struct device *dev;
88 void __iomem *regs; 87 void __iomem *regs;
89 void __iomem *enable_reg; 88 void __iomem *enable_reg;
90 struct clk *clk; 89 struct clk *clk;
90 struct clk *sclk;
91 unsigned int irq; 91 unsigned int irq;
92 struct regulator *vdd; 92 struct regulator *vdd;
93 93
@@ -97,43 +97,213 @@ struct exynos_adc {
97 unsigned int version; 97 unsigned int version;
98}; 98};
99 99
100static const struct of_device_id exynos_adc_match[] = { 100struct exynos_adc_data {
101 { .compatible = "samsung,exynos-adc-v1", .data = (void *)ADC_V1 }, 101 int num_channels;
102 { .compatible = "samsung,exynos-adc-v2", .data = (void *)ADC_V2 }, 102 bool needs_sclk;
103 {}, 103
104 void (*init_hw)(struct exynos_adc *info);
105 void (*exit_hw)(struct exynos_adc *info);
106 void (*clear_irq)(struct exynos_adc *info);
107 void (*start_conv)(struct exynos_adc *info, unsigned long addr);
104}; 108};
105MODULE_DEVICE_TABLE(of, exynos_adc_match);
106 109
107static inline unsigned int exynos_adc_get_version(struct platform_device *pdev) 110static void exynos_adc_unprepare_clk(struct exynos_adc *info)
108{ 111{
109 const struct of_device_id *match; 112 if (info->data->needs_sclk)
113 clk_unprepare(info->sclk);
114 clk_unprepare(info->clk);
115}
110 116
111 match = of_match_node(exynos_adc_match, pdev->dev.of_node); 117static int exynos_adc_prepare_clk(struct exynos_adc *info)
112 return (unsigned int)match->data; 118{
119 int ret;
120
121 ret = clk_prepare(info->clk);
122 if (ret) {
123 dev_err(info->dev, "failed preparing adc clock: %d\n", ret);
124 return ret;
125 }
126
127 if (info->data->needs_sclk) {
128 ret = clk_prepare(info->sclk);
129 if (ret) {
130 clk_unprepare(info->clk);
131 dev_err(info->dev,
132 "failed preparing sclk_adc clock: %d\n", ret);
133 return ret;
134 }
135 }
136
137 return 0;
138}
139
140static void exynos_adc_disable_clk(struct exynos_adc *info)
141{
142 if (info->data->needs_sclk)
143 clk_disable(info->sclk);
144 clk_disable(info->clk);
145}
146
147static int exynos_adc_enable_clk(struct exynos_adc *info)
148{
149 int ret;
150
151 ret = clk_enable(info->clk);
152 if (ret) {
153 dev_err(info->dev, "failed enabling adc clock: %d\n", ret);
154 return ret;
155 }
156
157 if (info->data->needs_sclk) {
158 ret = clk_enable(info->sclk);
159 if (ret) {
160 clk_disable(info->clk);
161 dev_err(info->dev,
162 "failed enabling sclk_adc clock: %d\n", ret);
163 return ret;
164 }
165 }
166
167 return 0;
168}
169
170static void exynos_adc_v1_init_hw(struct exynos_adc *info)
171{
172 u32 con1;
173
174 writel(1, info->enable_reg);
175
176 /* set default prescaler values and Enable prescaler */
177 con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
178
179 /* Enable 12-bit ADC resolution */
180 con1 |= ADC_V1_CON_RES;
181 writel(con1, ADC_V1_CON(info->regs));
182}
183
184static void exynos_adc_v1_exit_hw(struct exynos_adc *info)
185{
186 u32 con;
187
188 writel(0, info->enable_reg);
189
190 con = readl(ADC_V1_CON(info->regs));
191 con |= ADC_V1_CON_STANDBY;
192 writel(con, ADC_V1_CON(info->regs));
193}
194
195static void exynos_adc_v1_clear_irq(struct exynos_adc *info)
196{
197 writel(1, ADC_V1_INTCLR(info->regs));
198}
199
200static void exynos_adc_v1_start_conv(struct exynos_adc *info,
201 unsigned long addr)
202{
203 u32 con1;
204
205 writel(addr, ADC_V1_MUX(info->regs));
206
207 con1 = readl(ADC_V1_CON(info->regs));
208 writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
209}
210
211static const struct exynos_adc_data exynos_adc_v1_data = {
212 .num_channels = MAX_ADC_V1_CHANNELS,
213
214 .init_hw = exynos_adc_v1_init_hw,
215 .exit_hw = exynos_adc_v1_exit_hw,
216 .clear_irq = exynos_adc_v1_clear_irq,
217 .start_conv = exynos_adc_v1_start_conv,
218};
219
220static void exynos_adc_v2_init_hw(struct exynos_adc *info)
221{
222 u32 con1, con2;
223
224 writel(1, info->enable_reg);
225
226 con1 = ADC_V2_CON1_SOFT_RESET;
227 writel(con1, ADC_V2_CON1(info->regs));
228
229 con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL |
230 ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0);
231 writel(con2, ADC_V2_CON2(info->regs));
232
233 /* Enable interrupts */
234 writel(1, ADC_V2_INT_EN(info->regs));
235}
236
237static void exynos_adc_v2_exit_hw(struct exynos_adc *info)
238{
239 u32 con;
240
241 writel(0, info->enable_reg);
242
243 con = readl(ADC_V2_CON1(info->regs));
244 con &= ~ADC_CON_EN_START;
245 writel(con, ADC_V2_CON1(info->regs));
113} 246}
114 247
115static void exynos_adc_hw_init(struct exynos_adc *info) 248static void exynos_adc_v2_clear_irq(struct exynos_adc *info)
249{
250 writel(1, ADC_V2_INT_ST(info->regs));
251}
252
253static void exynos_adc_v2_start_conv(struct exynos_adc *info,
254 unsigned long addr)
116{ 255{
117 u32 con1, con2; 256 u32 con1, con2;
118 257
119 if (info->version == ADC_V2) { 258 con2 = readl(ADC_V2_CON2(info->regs));
120 con1 = ADC_V2_CON1_SOFT_RESET; 259 con2 &= ~ADC_V2_CON2_ACH_MASK;
121 writel(con1, ADC_V2_CON1(info->regs)); 260 con2 |= ADC_V2_CON2_ACH_SEL(addr);
261 writel(con2, ADC_V2_CON2(info->regs));
122 262
123 con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL | 263 con1 = readl(ADC_V2_CON1(info->regs));
124 ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0); 264 writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs));
125 writel(con2, ADC_V2_CON2(info->regs)); 265}
126 266
127 /* Enable interrupts */ 267static const struct exynos_adc_data exynos_adc_v2_data = {
128 writel(1, ADC_V2_INT_EN(info->regs)); 268 .num_channels = MAX_ADC_V2_CHANNELS,
129 } else {
130 /* set default prescaler values and Enable prescaler */
131 con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
132 269
133 /* Enable 12-bit ADC resolution */ 270 .init_hw = exynos_adc_v2_init_hw,
134 con1 |= ADC_V1_CON_RES; 271 .exit_hw = exynos_adc_v2_exit_hw,
135 writel(con1, ADC_V1_CON(info->regs)); 272 .clear_irq = exynos_adc_v2_clear_irq,
136 } 273 .start_conv = exynos_adc_v2_start_conv,
274};
275
276static const struct exynos_adc_data exynos3250_adc_data = {
277 .num_channels = MAX_EXYNOS3250_ADC_CHANNELS,
278 .needs_sclk = true,
279
280 .init_hw = exynos_adc_v2_init_hw,
281 .exit_hw = exynos_adc_v2_exit_hw,
282 .clear_irq = exynos_adc_v2_clear_irq,
283 .start_conv = exynos_adc_v2_start_conv,
284};
285
286static const struct of_device_id exynos_adc_match[] = {
287 {
288 .compatible = "samsung,exynos-adc-v1",
289 .data = &exynos_adc_v1_data,
290 }, {
291 .compatible = "samsung,exynos-adc-v2",
292 .data = &exynos_adc_v2_data,
293 }, {
294 .compatible = "samsung,exynos3250-adc",
295 .data = &exynos3250_adc_data,
296 },
297 {},
298};
299MODULE_DEVICE_TABLE(of, exynos_adc_match);
300
301static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev)
302{
303 const struct of_device_id *match;
304
305 match = of_match_node(exynos_adc_match, pdev->dev.of_node);
306 return (struct exynos_adc_data *)match->data;
137} 307}
138 308
139static int exynos_read_raw(struct iio_dev *indio_dev, 309static int exynos_read_raw(struct iio_dev *indio_dev,
@@ -144,7 +314,6 @@ static int exynos_read_raw(struct iio_dev *indio_dev,
144{ 314{
145 struct exynos_adc *info = iio_priv(indio_dev); 315 struct exynos_adc *info = iio_priv(indio_dev);
146 unsigned long timeout; 316 unsigned long timeout;
147 u32 con1, con2;
148 int ret; 317 int ret;
149 318
150 if (mask != IIO_CHAN_INFO_RAW) 319 if (mask != IIO_CHAN_INFO_RAW)
@@ -154,28 +323,15 @@ static int exynos_read_raw(struct iio_dev *indio_dev,
154 reinit_completion(&info->completion); 323 reinit_completion(&info->completion);
155 324
156 /* Select the channel to be used and Trigger conversion */ 325 /* Select the channel to be used and Trigger conversion */
157 if (info->version == ADC_V2) { 326 if (info->data->start_conv)
158 con2 = readl(ADC_V2_CON2(info->regs)); 327 info->data->start_conv(info, chan->address);
159 con2 &= ~ADC_V2_CON2_ACH_MASK;
160 con2 |= ADC_V2_CON2_ACH_SEL(chan->address);
161 writel(con2, ADC_V2_CON2(info->regs));
162
163 con1 = readl(ADC_V2_CON1(info->regs));
164 writel(con1 | ADC_CON_EN_START,
165 ADC_V2_CON1(info->regs));
166 } else {
167 writel(chan->address, ADC_V1_MUX(info->regs));
168
169 con1 = readl(ADC_V1_CON(info->regs));
170 writel(con1 | ADC_CON_EN_START,
171 ADC_V1_CON(info->regs));
172 }
173 328
174 timeout = wait_for_completion_timeout 329 timeout = wait_for_completion_timeout
175 (&info->completion, EXYNOS_ADC_TIMEOUT); 330 (&info->completion, EXYNOS_ADC_TIMEOUT);
176 if (timeout == 0) { 331 if (timeout == 0) {
177 dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n"); 332 dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
178 exynos_adc_hw_init(info); 333 if (info->data->init_hw)
334 info->data->init_hw(info);
179 ret = -ETIMEDOUT; 335 ret = -ETIMEDOUT;
180 } else { 336 } else {
181 *val = info->value; 337 *val = info->value;
@@ -193,13 +349,11 @@ static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
193 struct exynos_adc *info = (struct exynos_adc *)dev_id; 349 struct exynos_adc *info = (struct exynos_adc *)dev_id;
194 350
195 /* Read value */ 351 /* Read value */
196 info->value = readl(ADC_V1_DATX(info->regs)) & 352 info->value = readl(ADC_V1_DATX(info->regs)) & ADC_DATX_MASK;
197 ADC_DATX_MASK; 353
198 /* clear irq */ 354 /* clear irq */
199 if (info->version == ADC_V2) 355 if (info->data->clear_irq)
200 writel(1, ADC_V2_INT_ST(info->regs)); 356 info->data->clear_irq(info);
201 else
202 writel(1, ADC_V1_INTCLR(info->regs));
203 357
204 complete(&info->completion); 358 complete(&info->completion);
205 359
@@ -277,6 +431,12 @@ static int exynos_adc_probe(struct platform_device *pdev)
277 431
278 info = iio_priv(indio_dev); 432 info = iio_priv(indio_dev);
279 433
434 info->data = exynos_adc_get_data(pdev);
435 if (!info->data) {
436 dev_err(&pdev->dev, "failed getting exynos_adc_data\n");
437 return -EINVAL;
438 }
439
280 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 440 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
281 info->regs = devm_ioremap_resource(&pdev->dev, mem); 441 info->regs = devm_ioremap_resource(&pdev->dev, mem);
282 if (IS_ERR(info->regs)) 442 if (IS_ERR(info->regs))
@@ -294,6 +454,7 @@ static int exynos_adc_probe(struct platform_device *pdev)
294 } 454 }
295 455
296 info->irq = irq; 456 info->irq = irq;
457 info->dev = &pdev->dev;
297 458
298 init_completion(&info->completion); 459 init_completion(&info->completion);
299 460
@@ -304,6 +465,16 @@ static int exynos_adc_probe(struct platform_device *pdev)
304 return PTR_ERR(info->clk); 465 return PTR_ERR(info->clk);
305 } 466 }
306 467
468 if (info->data->needs_sclk) {
469 info->sclk = devm_clk_get(&pdev->dev, "sclk");
470 if (IS_ERR(info->sclk)) {
471 dev_err(&pdev->dev,
472 "failed getting sclk clock, err = %ld\n",
473 PTR_ERR(info->sclk));
474 return PTR_ERR(info->sclk);
475 }
476 }
477
307 info->vdd = devm_regulator_get(&pdev->dev, "vdd"); 478 info->vdd = devm_regulator_get(&pdev->dev, "vdd");
308 if (IS_ERR(info->vdd)) { 479 if (IS_ERR(info->vdd)) {
309 dev_err(&pdev->dev, "failed getting regulator, err = %ld\n", 480 dev_err(&pdev->dev, "failed getting regulator, err = %ld\n",
@@ -315,13 +486,13 @@ static int exynos_adc_probe(struct platform_device *pdev)
315 if (ret) 486 if (ret)
316 return ret; 487 return ret;
317 488
318 ret = clk_prepare_enable(info->clk); 489 ret = exynos_adc_prepare_clk(info);
319 if (ret) 490 if (ret)
320 goto err_disable_reg; 491 goto err_disable_reg;
321 492
322 writel(1, info->enable_reg); 493 ret = exynos_adc_enable_clk(info);
323 494 if (ret)
324 info->version = exynos_adc_get_version(pdev); 495 goto err_unprepare_clk;
325 496
326 platform_set_drvdata(pdev, indio_dev); 497 platform_set_drvdata(pdev, indio_dev);
327 498
@@ -331,11 +502,7 @@ static int exynos_adc_probe(struct platform_device *pdev)
331 indio_dev->info = &exynos_adc_iio_info; 502 indio_dev->info = &exynos_adc_iio_info;
332 indio_dev->modes = INDIO_DIRECT_MODE; 503 indio_dev->modes = INDIO_DIRECT_MODE;
333 indio_dev->channels = exynos_adc_iio_channels; 504 indio_dev->channels = exynos_adc_iio_channels;
334 505 indio_dev->num_channels = info->data->num_channels;
335 if (info->version == ADC_V1)
336 indio_dev->num_channels = MAX_ADC_V1_CHANNELS;
337 else
338 indio_dev->num_channels = MAX_ADC_V2_CHANNELS;
339 506
340 ret = request_irq(info->irq, exynos_adc_isr, 507 ret = request_irq(info->irq, exynos_adc_isr,
341 0, dev_name(&pdev->dev), info); 508 0, dev_name(&pdev->dev), info);
@@ -349,7 +516,8 @@ static int exynos_adc_probe(struct platform_device *pdev)
349 if (ret) 516 if (ret)
350 goto err_irq; 517 goto err_irq;
351 518
352 exynos_adc_hw_init(info); 519 if (info->data->init_hw)
520 info->data->init_hw(info);
353 521
354 ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev); 522 ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev);
355 if (ret < 0) { 523 if (ret < 0) {
@@ -366,8 +534,11 @@ err_of_populate:
366err_irq: 534err_irq:
367 free_irq(info->irq, info); 535 free_irq(info->irq, info);
368err_disable_clk: 536err_disable_clk:
369 writel(0, info->enable_reg); 537 if (info->data->exit_hw)
370 clk_disable_unprepare(info->clk); 538 info->data->exit_hw(info);
539 exynos_adc_disable_clk(info);
540err_unprepare_clk:
541 exynos_adc_unprepare_clk(info);
371err_disable_reg: 542err_disable_reg:
372 regulator_disable(info->vdd); 543 regulator_disable(info->vdd);
373 return ret; 544 return ret;
@@ -382,8 +553,10 @@ static int exynos_adc_remove(struct platform_device *pdev)
382 exynos_adc_remove_devices); 553 exynos_adc_remove_devices);
383 iio_device_unregister(indio_dev); 554 iio_device_unregister(indio_dev);
384 free_irq(info->irq, info); 555 free_irq(info->irq, info);
385 writel(0, info->enable_reg); 556 if (info->data->exit_hw)
386 clk_disable_unprepare(info->clk); 557 info->data->exit_hw(info);
558 exynos_adc_disable_clk(info);
559 exynos_adc_unprepare_clk(info);
387 regulator_disable(info->vdd); 560 regulator_disable(info->vdd);
388 561
389 return 0; 562 return 0;
@@ -394,20 +567,10 @@ static int exynos_adc_suspend(struct device *dev)
394{ 567{
395 struct iio_dev *indio_dev = dev_get_drvdata(dev); 568 struct iio_dev *indio_dev = dev_get_drvdata(dev);
396 struct exynos_adc *info = iio_priv(indio_dev); 569 struct exynos_adc *info = iio_priv(indio_dev);
397 u32 con;
398 570
399 if (info->version == ADC_V2) { 571 if (info->data->exit_hw)
400 con = readl(ADC_V2_CON1(info->regs)); 572 info->data->exit_hw(info);
401 con &= ~ADC_CON_EN_START; 573 exynos_adc_disable_clk(info);
402 writel(con, ADC_V2_CON1(info->regs));
403 } else {
404 con = readl(ADC_V1_CON(info->regs));
405 con |= ADC_V1_CON_STANDBY;
406 writel(con, ADC_V1_CON(info->regs));
407 }
408
409 writel(0, info->enable_reg);
410 clk_disable_unprepare(info->clk);
411 regulator_disable(info->vdd); 574 regulator_disable(info->vdd);
412 575
413 return 0; 576 return 0;
@@ -423,12 +586,12 @@ static int exynos_adc_resume(struct device *dev)
423 if (ret) 586 if (ret)
424 return ret; 587 return ret;
425 588
426 ret = clk_prepare_enable(info->clk); 589 ret = exynos_adc_enable_clk(info);
427 if (ret) 590 if (ret)
428 return ret; 591 return ret;
429 592
430 writel(1, info->enable_reg); 593 if (info->data->init_hw)
431 exynos_adc_hw_init(info); 594 info->data->init_hw(info);
432 595
433 return 0; 596 return 0;
434} 597}
diff --git a/drivers/iio/adc/max1027.c b/drivers/iio/adc/max1027.c
new file mode 100644
index 000000000000..87ee1c7d0b54
--- /dev/null
+++ b/drivers/iio/adc/max1027.c
@@ -0,0 +1,521 @@
1 /*
2 * iio/adc/max1027.c
3 * Copyright (C) 2014 Philippe Reynes
4 *
5 * based on linux/drivers/iio/ad7923.c
6 * Copyright 2011 Analog Devices Inc (from AD7923 Driver)
7 * Copyright 2012 CS Systemes d'Information
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * max1027.c
14 *
15 * Partial support for max1027 and similar chips.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/spi/spi.h>
21#include <linux/delay.h>
22
23#include <linux/iio/iio.h>
24#include <linux/iio/buffer.h>
25#include <linux/iio/trigger.h>
26#include <linux/iio/trigger_consumer.h>
27#include <linux/iio/triggered_buffer.h>
28
29#define MAX1027_CONV_REG BIT(7)
30#define MAX1027_SETUP_REG BIT(6)
31#define MAX1027_AVG_REG BIT(5)
32#define MAX1027_RST_REG BIT(4)
33
34/* conversion register */
35#define MAX1027_TEMP BIT(0)
36#define MAX1027_SCAN_0_N (0x00 << 1)
37#define MAX1027_SCAN_N_M (0x01 << 1)
38#define MAX1027_SCAN_N (0x02 << 1)
39#define MAX1027_NOSCAN (0x03 << 1)
40#define MAX1027_CHAN(n) ((n) << 3)
41
42/* setup register */
43#define MAX1027_UNIPOLAR 0x02
44#define MAX1027_BIPOLAR 0x03
45#define MAX1027_REF_MODE0 (0x00 << 2)
46#define MAX1027_REF_MODE1 (0x01 << 2)
47#define MAX1027_REF_MODE2 (0x02 << 2)
48#define MAX1027_REF_MODE3 (0x03 << 2)
49#define MAX1027_CKS_MODE0 (0x00 << 4)
50#define MAX1027_CKS_MODE1 (0x01 << 4)
51#define MAX1027_CKS_MODE2 (0x02 << 4)
52#define MAX1027_CKS_MODE3 (0x03 << 4)
53
54/* averaging register */
55#define MAX1027_NSCAN_4 0x00
56#define MAX1027_NSCAN_8 0x01
57#define MAX1027_NSCAN_12 0x02
58#define MAX1027_NSCAN_16 0x03
59#define MAX1027_NAVG_4 (0x00 << 2)
60#define MAX1027_NAVG_8 (0x01 << 2)
61#define MAX1027_NAVG_16 (0x02 << 2)
62#define MAX1027_NAVG_32 (0x03 << 2)
63#define MAX1027_AVG_EN BIT(4)
64
65enum max1027_id {
66 max1027,
67 max1029,
68 max1031,
69};
70
71static const struct spi_device_id max1027_id[] = {
72 {"max1027", max1027},
73 {"max1029", max1029},
74 {"max1031", max1031},
75 {}
76};
77MODULE_DEVICE_TABLE(spi, max1027_id);
78
79#ifdef CONFIG_OF
80static const struct of_device_id max1027_adc_dt_ids[] = {
81 { .compatible = "maxim,max1027" },
82 { .compatible = "maxim,max1029" },
83 { .compatible = "maxim,max1031" },
84 {},
85};
86MODULE_DEVICE_TABLE(of, max1027_adc_dt_ids);
87#endif
88
89#define MAX1027_V_CHAN(index) \
90 { \
91 .type = IIO_VOLTAGE, \
92 .indexed = 1, \
93 .channel = index, \
94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
95 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
96 .scan_index = index + 1, \
97 .scan_type = { \
98 .sign = 'u', \
99 .realbits = 10, \
100 .storagebits = 16, \
101 .shift = 2, \
102 .endianness = IIO_BE, \
103 }, \
104 }
105
106#define MAX1027_T_CHAN \
107 { \
108 .type = IIO_TEMP, \
109 .channel = 0, \
110 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
111 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
112 .scan_index = 0, \
113 .scan_type = { \
114 .sign = 'u', \
115 .realbits = 12, \
116 .storagebits = 16, \
117 .endianness = IIO_BE, \
118 }, \
119 }
120
121static const struct iio_chan_spec max1027_channels[] = {
122 MAX1027_T_CHAN,
123 MAX1027_V_CHAN(0),
124 MAX1027_V_CHAN(1),
125 MAX1027_V_CHAN(2),
126 MAX1027_V_CHAN(3),
127 MAX1027_V_CHAN(4),
128 MAX1027_V_CHAN(5),
129 MAX1027_V_CHAN(6),
130 MAX1027_V_CHAN(7)
131};
132
133static const struct iio_chan_spec max1029_channels[] = {
134 MAX1027_T_CHAN,
135 MAX1027_V_CHAN(0),
136 MAX1027_V_CHAN(1),
137 MAX1027_V_CHAN(2),
138 MAX1027_V_CHAN(3),
139 MAX1027_V_CHAN(4),
140 MAX1027_V_CHAN(5),
141 MAX1027_V_CHAN(6),
142 MAX1027_V_CHAN(7),
143 MAX1027_V_CHAN(8),
144 MAX1027_V_CHAN(9),
145 MAX1027_V_CHAN(10),
146 MAX1027_V_CHAN(11)
147};
148
149static const struct iio_chan_spec max1031_channels[] = {
150 MAX1027_T_CHAN,
151 MAX1027_V_CHAN(0),
152 MAX1027_V_CHAN(1),
153 MAX1027_V_CHAN(2),
154 MAX1027_V_CHAN(3),
155 MAX1027_V_CHAN(4),
156 MAX1027_V_CHAN(5),
157 MAX1027_V_CHAN(6),
158 MAX1027_V_CHAN(7),
159 MAX1027_V_CHAN(8),
160 MAX1027_V_CHAN(9),
161 MAX1027_V_CHAN(10),
162 MAX1027_V_CHAN(11),
163 MAX1027_V_CHAN(12),
164 MAX1027_V_CHAN(13),
165 MAX1027_V_CHAN(14),
166 MAX1027_V_CHAN(15)
167};
168
169static const unsigned long max1027_available_scan_masks[] = {
170 0x000001ff,
171 0x00000000,
172};
173
174static const unsigned long max1029_available_scan_masks[] = {
175 0x00001fff,
176 0x00000000,
177};
178
179static const unsigned long max1031_available_scan_masks[] = {
180 0x0001ffff,
181 0x00000000,
182};
183
184struct max1027_chip_info {
185 const struct iio_chan_spec *channels;
186 unsigned int num_channels;
187 const unsigned long *available_scan_masks;
188};
189
190static const struct max1027_chip_info max1027_chip_info_tbl[] = {
191 [max1027] = {
192 .channels = max1027_channels,
193 .num_channels = ARRAY_SIZE(max1027_channels),
194 .available_scan_masks = max1027_available_scan_masks,
195 },
196 [max1029] = {
197 .channels = max1029_channels,
198 .num_channels = ARRAY_SIZE(max1029_channels),
199 .available_scan_masks = max1029_available_scan_masks,
200 },
201 [max1031] = {
202 .channels = max1031_channels,
203 .num_channels = ARRAY_SIZE(max1031_channels),
204 .available_scan_masks = max1031_available_scan_masks,
205 },
206};
207
208struct max1027_state {
209 const struct max1027_chip_info *info;
210 struct spi_device *spi;
211 struct iio_trigger *trig;
212 __be16 *buffer;
213 struct mutex lock;
214
215 u8 reg ____cacheline_aligned;
216};
217
218static int max1027_read_single_value(struct iio_dev *indio_dev,
219 struct iio_chan_spec const *chan,
220 int *val)
221{
222 int ret;
223 struct max1027_state *st = iio_priv(indio_dev);
224
225 if (iio_buffer_enabled(indio_dev)) {
226 dev_warn(&indio_dev->dev, "trigger mode already enabled");
227 return -EBUSY;
228 }
229
230 /* Start acquisition on conversion register write */
231 st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2;
232 ret = spi_write(st->spi, &st->reg, 1);
233 if (ret < 0) {
234 dev_err(&indio_dev->dev,
235 "Failed to configure setup register\n");
236 return ret;
237 }
238
239 /* Configure conversion register with the requested chan */
240 st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) |
241 MAX1027_NOSCAN | !!(chan->type == IIO_TEMP);
242 ret = spi_write(st->spi, &st->reg, 1);
243 if (ret < 0) {
244 dev_err(&indio_dev->dev,
245 "Failed to configure conversion register\n");
246 return ret;
247 }
248
249 /*
250 * For an unknown reason, when we use the mode "10" (write
251 * conversion register), the interrupt doesn't occur every time.
252 * So we just wait 1 ms.
253 */
254 mdelay(1);
255
256 /* Read result */
257 ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2);
258 if (ret < 0)
259 return ret;
260
261 *val = be16_to_cpu(st->buffer[0]);
262
263 return IIO_VAL_INT;
264}
265
266static int max1027_read_raw(struct iio_dev *indio_dev,
267 struct iio_chan_spec const *chan,
268 int *val, int *val2, long mask)
269{
270 int ret = 0;
271 struct max1027_state *st = iio_priv(indio_dev);
272
273 mutex_lock(&st->lock);
274
275 switch (mask) {
276 case IIO_CHAN_INFO_RAW:
277 ret = max1027_read_single_value(indio_dev, chan, val);
278 break;
279 case IIO_CHAN_INFO_SCALE:
280 switch (chan->type) {
281 case IIO_TEMP:
282 *val = 1;
283 *val2 = 8;
284 ret = IIO_VAL_FRACTIONAL;
285 break;
286 case IIO_VOLTAGE:
287 *val = 2500;
288 *val2 = 10;
289 ret = IIO_VAL_FRACTIONAL_LOG2;
290 break;
291 default:
292 ret = -EINVAL;
293 break;
294 }
295 break;
296 default:
297 ret = -EINVAL;
298 break;
299 }
300
301 mutex_unlock(&st->lock);
302
303 return ret;
304}
305
306static int max1027_debugfs_reg_access(struct iio_dev *indio_dev,
307 unsigned reg, unsigned writeval,
308 unsigned *readval)
309{
310 struct max1027_state *st = iio_priv(indio_dev);
311 u8 *val = (u8 *)st->buffer;
312
313 if (readval != NULL)
314 return -EINVAL;
315
316 *val = (u8)writeval;
317 return spi_write(st->spi, val, 1);
318}
319
320static int max1027_validate_trigger(struct iio_dev *indio_dev,
321 struct iio_trigger *trig)
322{
323 struct max1027_state *st = iio_priv(indio_dev);
324
325 if (st->trig != trig)
326 return -EINVAL;
327
328 return 0;
329}
330
331static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)
332{
333 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
334 struct max1027_state *st = iio_priv(indio_dev);
335 int ret;
336
337 if (state) {
338 /* Start acquisition on cnvst */
339 st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 |
340 MAX1027_REF_MODE2;
341 ret = spi_write(st->spi, &st->reg, 1);
342 if (ret < 0)
343 return ret;
344
345 /* Scan from 0 to max */
346 st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) |
347 MAX1027_SCAN_N_M | MAX1027_TEMP;
348 ret = spi_write(st->spi, &st->reg, 1);
349 if (ret < 0)
350 return ret;
351 } else {
352 /* Start acquisition on conversion register write */
353 st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2 |
354 MAX1027_REF_MODE2;
355 ret = spi_write(st->spi, &st->reg, 1);
356 if (ret < 0)
357 return ret;
358 }
359
360 return 0;
361}
362
363static int max1027_validate_device(struct iio_trigger *trig,
364 struct iio_dev *indio_dev)
365{
366 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
367
368 if (indio != indio_dev)
369 return -EINVAL;
370
371 return 0;
372}
373
374static irqreturn_t max1027_trigger_handler(int irq, void *private)
375{
376 struct iio_poll_func *pf = (struct iio_poll_func *)private;
377 struct iio_dev *indio_dev = pf->indio_dev;
378 struct max1027_state *st = iio_priv(indio_dev);
379
380 pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
381
382 /* fill buffer with all channel */
383 spi_read(st->spi, st->buffer, indio_dev->masklength * 2);
384
385 iio_push_to_buffers(indio_dev, st->buffer);
386
387 iio_trigger_notify_done(indio_dev->trig);
388
389 return IRQ_HANDLED;
390}
391
392static const struct iio_trigger_ops max1027_trigger_ops = {
393 .owner = THIS_MODULE,
394 .validate_device = &max1027_validate_device,
395 .set_trigger_state = &max1027_set_trigger_state,
396};
397
398static const struct iio_info max1027_info = {
399 .driver_module = THIS_MODULE,
400 .read_raw = &max1027_read_raw,
401 .validate_trigger = &max1027_validate_trigger,
402 .debugfs_reg_access = &max1027_debugfs_reg_access,
403};
404
405static int max1027_probe(struct spi_device *spi)
406{
407 int ret;
408 struct iio_dev *indio_dev;
409 struct max1027_state *st;
410
411 pr_debug("%s: probe(spi = 0x%p)\n", __func__, spi);
412
413 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
414 if (indio_dev == NULL) {
415 pr_err("Can't allocate iio device\n");
416 return -ENOMEM;
417 }
418
419 spi_set_drvdata(spi, indio_dev);
420
421 st = iio_priv(indio_dev);
422 st->spi = spi;
423 st->info = &max1027_chip_info_tbl[spi_get_device_id(spi)->driver_data];
424
425 mutex_init(&st->lock);
426
427 indio_dev->name = spi_get_device_id(spi)->name;
428 indio_dev->dev.parent = &spi->dev;
429 indio_dev->info = &max1027_info;
430 indio_dev->modes = INDIO_DIRECT_MODE;
431 indio_dev->channels = st->info->channels;
432 indio_dev->num_channels = st->info->num_channels;
433 indio_dev->available_scan_masks = st->info->available_scan_masks;
434
435 st->buffer = devm_kmalloc(&indio_dev->dev,
436 indio_dev->num_channels * 2,
437 GFP_KERNEL);
438 if (st->buffer == NULL) {
439 dev_err(&indio_dev->dev, "Can't allocate bufffer\n");
440 return -ENOMEM;
441 }
442
443 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
444 &max1027_trigger_handler, NULL);
445 if (ret < 0) {
446 dev_err(&indio_dev->dev, "Failed to setup buffer\n");
447 return ret;
448 }
449
450 st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-trigger",
451 indio_dev->name);
452 if (st->trig == NULL) {
453 ret = -ENOMEM;
454 dev_err(&indio_dev->dev, "Failed to allocate iio trigger\n");
455 goto fail_trigger_alloc;
456 }
457
458 st->trig->ops = &max1027_trigger_ops;
459 st->trig->dev.parent = &spi->dev;
460 iio_trigger_set_drvdata(st->trig, indio_dev);
461 iio_trigger_register(st->trig);
462
463 ret = devm_request_threaded_irq(&spi->dev, spi->irq,
464 iio_trigger_generic_data_rdy_poll,
465 NULL,
466 IRQF_TRIGGER_FALLING,
467 spi->dev.driver->name, st->trig);
468 if (ret < 0) {
469 dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n");
470 goto fail_dev_register;
471 }
472
473 /* Disable averaging */
474 st->reg = MAX1027_AVG_REG;
475 ret = spi_write(st->spi, &st->reg, 1);
476 if (ret < 0) {
477 dev_err(&indio_dev->dev, "Failed to configure averaging register\n");
478 goto fail_dev_register;
479 }
480
481 ret = iio_device_register(indio_dev);
482 if (ret < 0) {
483 dev_err(&indio_dev->dev, "Failed to register iio device\n");
484 goto fail_dev_register;
485 }
486
487 return 0;
488
489fail_dev_register:
490fail_trigger_alloc:
491 iio_triggered_buffer_cleanup(indio_dev);
492
493 return ret;
494}
495
496static int max1027_remove(struct spi_device *spi)
497{
498 struct iio_dev *indio_dev = spi_get_drvdata(spi);
499
500 pr_debug("%s: remove(spi = 0x%p)\n", __func__, spi);
501
502 iio_device_unregister(indio_dev);
503 iio_triggered_buffer_cleanup(indio_dev);
504
505 return 0;
506}
507
508static struct spi_driver max1027_driver = {
509 .driver = {
510 .name = "max1027",
511 .owner = THIS_MODULE,
512 },
513 .probe = max1027_probe,
514 .remove = max1027_remove,
515 .id_table = max1027_id,
516};
517module_spi_driver(max1027_driver);
518
519MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>");
520MODULE_DESCRIPTION("MAX1027/MAX1029/MAX1031 ADC");
521MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/xilinx-xadc-core.c b/drivers/iio/adc/xilinx-xadc-core.c
index ab52be29141b..fd2745c62943 100644
--- a/drivers/iio/adc/xilinx-xadc-core.c
+++ b/drivers/iio/adc/xilinx-xadc-core.c
@@ -486,7 +486,7 @@ static irqreturn_t xadc_axi_interrupt_handler(int irq, void *devid)
486 return IRQ_NONE; 486 return IRQ_NONE;
487 487
488 if ((status & XADC_AXI_INT_EOS) && xadc->trigger) 488 if ((status & XADC_AXI_INT_EOS) && xadc->trigger)
489 iio_trigger_poll(xadc->trigger, 0); 489 iio_trigger_poll(xadc->trigger);
490 490
491 if (status & XADC_AXI_INT_ALARM_MASK) { 491 if (status & XADC_AXI_INT_ALARM_MASK) {
492 /* 492 /*
diff --git a/drivers/iio/adc/xilinx-xadc-events.c b/drivers/iio/adc/xilinx-xadc-events.c
index 3e7f0d7a80c3..edcf3aabd70d 100644
--- a/drivers/iio/adc/xilinx-xadc-events.c
+++ b/drivers/iio/adc/xilinx-xadc-events.c
@@ -31,17 +31,11 @@ static const struct iio_chan_spec *xadc_event_to_channel(
31static void xadc_handle_event(struct iio_dev *indio_dev, unsigned int event) 31static void xadc_handle_event(struct iio_dev *indio_dev, unsigned int event)
32{ 32{
33 const struct iio_chan_spec *chan; 33 const struct iio_chan_spec *chan;
34 unsigned int offset;
35 34
36 /* Temperature threshold error, we don't handle this yet */ 35 /* Temperature threshold error, we don't handle this yet */
37 if (event == 0) 36 if (event == 0)
38 return; 37 return;
39 38
40 if (event < 4)
41 offset = event;
42 else
43 offset = event + 4;
44
45 chan = xadc_event_to_channel(indio_dev, event); 39 chan = xadc_event_to_channel(indio_dev, event);
46 40
47 if (chan->type == IIO_TEMP) { 41 if (chan->type == IIO_TEMP) {
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
index 403dd3d8986e..25b01e156d82 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-attributes.c
@@ -26,12 +26,12 @@
26#include <linux/iio/iio.h> 26#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h> 27#include <linux/iio/sysfs.h>
28 28
29struct { 29static struct {
30 u32 usage_id; 30 u32 usage_id;
31 int unit; /* 0 for default others from HID sensor spec */ 31 int unit; /* 0 for default others from HID sensor spec */
32 int scale_val0; /* scale, whole number */ 32 int scale_val0; /* scale, whole number */
33 int scale_val1; /* scale, fraction in micros */ 33 int scale_val1; /* scale, fraction in micros */
34} static unit_conversion[] = { 34} unit_conversion[] = {
35 {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650}, 35 {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650},
36 {HID_USAGE_SENSOR_ACCEL_3D, 36 {HID_USAGE_SENSOR_ACCEL_3D,
37 HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0}, 37 HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
@@ -343,6 +343,7 @@ int hid_sensor_format_scale(u32 usage_id,
343} 343}
344EXPORT_SYMBOL(hid_sensor_format_scale); 344EXPORT_SYMBOL(hid_sensor_format_scale);
345 345
346static
346int hid_sensor_get_reporting_interval(struct hid_sensor_hub_device *hsdev, 347int hid_sensor_get_reporting_interval(struct hid_sensor_hub_device *hsdev,
347 u32 usage_id, 348 u32 usage_id,
348 struct hid_sensor_common *st) 349 struct hid_sensor_common *st)
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index e8b932fed70e..8a4ec00a91a0 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -14,8 +14,8 @@
14#include <linux/delay.h> 14#include <linux/delay.h>
15#include <linux/iio/iio.h> 15#include <linux/iio/iio.h>
16#include <linux/regulator/consumer.h> 16#include <linux/regulator/consumer.h>
17#include <linux/of.h>
17#include <asm/unaligned.h> 18#include <asm/unaligned.h>
18
19#include <linux/iio/common/st_sensors.h> 19#include <linux/iio/common/st_sensors.h>
20 20
21 21
@@ -265,14 +265,47 @@ static int st_sensors_set_drdy_int_pin(struct iio_dev *indio_dev,
265 return 0; 265 return 0;
266} 266}
267 267
268#ifdef CONFIG_OF
269static struct st_sensors_platform_data *st_sensors_of_probe(struct device *dev,
270 struct st_sensors_platform_data *defdata)
271{
272 struct st_sensors_platform_data *pdata;
273 struct device_node *np = dev->of_node;
274 u32 val;
275
276 if (!np)
277 return NULL;
278
279 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
280 if (!of_property_read_u32(np, "st,drdy-int-pin", &val) && (val <= 2))
281 pdata->drdy_int_pin = (u8) val;
282 else
283 pdata->drdy_int_pin = defdata ? defdata->drdy_int_pin : 1;
284
285 return pdata;
286}
287#else
288static struct st_sensors_platform_data *st_sensors_of_probe(struct device *dev,
289 struct st_sensors_platform_data *defdata)
290{
291 return NULL;
292}
293#endif
294
268int st_sensors_init_sensor(struct iio_dev *indio_dev, 295int st_sensors_init_sensor(struct iio_dev *indio_dev,
269 struct st_sensors_platform_data *pdata) 296 struct st_sensors_platform_data *pdata)
270{ 297{
271 struct st_sensor_data *sdata = iio_priv(indio_dev); 298 struct st_sensor_data *sdata = iio_priv(indio_dev);
299 struct st_sensors_platform_data *of_pdata;
272 int err = 0; 300 int err = 0;
273 301
274 mutex_init(&sdata->tb.buf_lock); 302 mutex_init(&sdata->tb.buf_lock);
275 303
304 /* If OF/DT pdata exists, it will take precedence of anything else */
305 of_pdata = st_sensors_of_probe(indio_dev->dev.parent, pdata);
306 if (of_pdata)
307 pdata = of_pdata;
308
276 if (pdata) 309 if (pdata)
277 err = st_sensors_set_drdy_int_pin(indio_dev, pdata); 310 err = st_sensors_set_drdy_int_pin(indio_dev, pdata);
278 311
@@ -463,35 +496,6 @@ read_wai_error:
463} 496}
464EXPORT_SYMBOL(st_sensors_check_device_support); 497EXPORT_SYMBOL(st_sensors_check_device_support);
465 498
466ssize_t st_sensors_sysfs_get_sampling_frequency(struct device *dev,
467 struct device_attribute *attr, char *buf)
468{
469 struct st_sensor_data *adata = iio_priv(dev_get_drvdata(dev));
470
471 return sprintf(buf, "%d\n", adata->odr);
472}
473EXPORT_SYMBOL(st_sensors_sysfs_get_sampling_frequency);
474
475ssize_t st_sensors_sysfs_set_sampling_frequency(struct device *dev,
476 struct device_attribute *attr, const char *buf, size_t size)
477{
478 int err;
479 unsigned int odr;
480 struct iio_dev *indio_dev = dev_get_drvdata(dev);
481
482 err = kstrtoint(buf, 10, &odr);
483 if (err < 0)
484 goto conversion_error;
485
486 mutex_lock(&indio_dev->mlock);
487 err = st_sensors_set_odr(indio_dev, odr);
488 mutex_unlock(&indio_dev->mlock);
489
490conversion_error:
491 return err < 0 ? err : size;
492}
493EXPORT_SYMBOL(st_sensors_sysfs_set_sampling_frequency);
494
495ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev, 499ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
496 struct device_attribute *attr, char *buf) 500 struct device_attribute *attr, char *buf)
497{ 501{
diff --git a/drivers/iio/common/st_sensors/st_sensors_i2c.c b/drivers/iio/common/st_sensors/st_sensors_i2c.c
index 38af9440c103..bb6f3085f57b 100644
--- a/drivers/iio/common/st_sensors/st_sensors_i2c.c
+++ b/drivers/iio/common/st_sensors/st_sensors_i2c.c
@@ -12,6 +12,7 @@
12#include <linux/module.h> 12#include <linux/module.h>
13#include <linux/slab.h> 13#include <linux/slab.h>
14#include <linux/iio/iio.h> 14#include <linux/iio/iio.h>
15#include <linux/of_device.h>
15 16
16#include <linux/iio/common/st_sensors_i2c.h> 17#include <linux/iio/common/st_sensors_i2c.h>
17 18
@@ -76,6 +77,35 @@ void st_sensors_i2c_configure(struct iio_dev *indio_dev,
76} 77}
77EXPORT_SYMBOL(st_sensors_i2c_configure); 78EXPORT_SYMBOL(st_sensors_i2c_configure);
78 79
80#ifdef CONFIG_OF
81/**
82 * st_sensors_of_i2c_probe() - device tree probe for ST I2C sensors
83 * @client: the I2C client device for the sensor
84 * @match: the OF match table for the device, containing compatible strings
85 * but also a .data field with the corresponding internal kernel name
86 * used by this sensor.
87 *
88 * In effect this function matches a compatible string to an internal kernel
89 * name for a certain sensor device, so that the rest of the autodetection can
90 * rely on that name from this point on. I2C client devices will be renamed
91 * to match the internal kernel convention.
92 */
93void st_sensors_of_i2c_probe(struct i2c_client *client,
94 const struct of_device_id *match)
95{
96 const struct of_device_id *of_id;
97
98 of_id = of_match_device(match, &client->dev);
99 if (!of_id)
100 return;
101
102 /* The name from the OF match takes precedence if present */
103 strncpy(client->name, of_id->data, sizeof(client->name));
104 client->name[sizeof(client->name) - 1] = '\0';
105}
106EXPORT_SYMBOL(st_sensors_of_i2c_probe);
107#endif
108
79MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 109MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
80MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver"); 110MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
81MODULE_LICENSE("GPL v2"); 111MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index f378ca8033db..f278eff42a4c 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -163,4 +163,14 @@ config MCP4725
163 To compile this driver as a module, choose M here: the module 163 To compile this driver as a module, choose M here: the module
164 will be called mcp4725. 164 will be called mcp4725.
165 165
166config MCP4922
167 tristate "MCP4902, MCP4912, MCP4922 DAC driver"
168 depends on SPI
169 help
170 Say yes here to build the driver for the Microchip MCP4902
171 MCP4912, and MCP4922 DAC devices.
172
173 To compile this driver as a module, choose M here: the module
174 will be called mcp4922.
175
166endmenu 176endmenu
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index bb84ad64463f..10107640bb46 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_AD5686) += ad5686.o
18obj-$(CONFIG_AD7303) += ad7303.o 18obj-$(CONFIG_AD7303) += ad7303.o
19obj-$(CONFIG_MAX517) += max517.o 19obj-$(CONFIG_MAX517) += max517.o
20obj-$(CONFIG_MCP4725) += mcp4725.o 20obj-$(CONFIG_MCP4725) += mcp4725.o
21obj-$(CONFIG_MCP4922) += mcp4922.o
diff --git a/drivers/iio/dac/ad5504.c b/drivers/iio/dac/ad5504.c
index 1e6449346b50..581ec141de3d 100644
--- a/drivers/iio/dac/ad5504.c
+++ b/drivers/iio/dac/ad5504.c
@@ -15,17 +15,16 @@
15#include <linux/sysfs.h> 15#include <linux/sysfs.h>
16#include <linux/regulator/consumer.h> 16#include <linux/regulator/consumer.h>
17#include <linux/module.h> 17#include <linux/module.h>
18#include <linux/bitops.h>
18 19
19#include <linux/iio/iio.h> 20#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h> 21#include <linux/iio/sysfs.h>
21#include <linux/iio/events.h> 22#include <linux/iio/events.h>
22#include <linux/iio/dac/ad5504.h> 23#include <linux/iio/dac/ad5504.h>
23 24
24#define AD5505_BITS 12 25#define AD5504_RES_MASK GENMASK(11, 0)
25#define AD5504_RES_MASK ((1 << (AD5505_BITS)) - 1) 26#define AD5504_CMD_READ BIT(15)
26 27#define AD5504_CMD_WRITE 0
27#define AD5504_CMD_READ (1 << 15)
28#define AD5504_CMD_WRITE (0 << 15)
29#define AD5504_ADDR(addr) ((addr) << 12) 28#define AD5504_ADDR(addr) ((addr) << 12)
30 29
31/* Registers */ 30/* Registers */
@@ -42,7 +41,7 @@
42 41
43/** 42/**
44 * struct ad5446_state - driver instance specific data 43 * struct ad5446_state - driver instance specific data
45 * @us: spi_device 44 * @spi: spi_device
46 * @reg: supply regulator 45 * @reg: supply regulator
47 * @vref_mv: actual reference voltage used 46 * @vref_mv: actual reference voltage used
48 * @pwr_down_mask power down mask 47 * @pwr_down_mask power down mask
@@ -126,7 +125,6 @@ static int ad5504_write_raw(struct iio_dev *indio_dev,
126 long mask) 125 long mask)
127{ 126{
128 struct ad5504_state *st = iio_priv(indio_dev); 127 struct ad5504_state *st = iio_priv(indio_dev);
129 int ret;
130 128
131 switch (mask) { 129 switch (mask) {
132 case IIO_CHAN_INFO_RAW: 130 case IIO_CHAN_INFO_RAW:
@@ -135,10 +133,8 @@ static int ad5504_write_raw(struct iio_dev *indio_dev,
135 133
136 return ad5504_spi_write(st, chan->address, val); 134 return ad5504_spi_write(st, chan->address, val);
137 default: 135 default:
138 ret = -EINVAL; 136 return -EINVAL;
139 } 137 }
140
141 return -EINVAL;
142} 138}
143 139
144static const char * const ad5504_powerdown_modes[] = { 140static const char * const ad5504_powerdown_modes[] = {
diff --git a/drivers/iio/dac/ad5624r_spi.c b/drivers/iio/dac/ad5624r_spi.c
index e8199cce2aea..61bb9d4239ea 100644
--- a/drivers/iio/dac/ad5624r_spi.c
+++ b/drivers/iio/dac/ad5624r_spi.c
@@ -67,7 +67,6 @@ static int ad5624r_write_raw(struct iio_dev *indio_dev,
67 long mask) 67 long mask)
68{ 68{
69 struct ad5624r_state *st = iio_priv(indio_dev); 69 struct ad5624r_state *st = iio_priv(indio_dev);
70 int ret;
71 70
72 switch (mask) { 71 switch (mask) {
73 case IIO_CHAN_INFO_RAW: 72 case IIO_CHAN_INFO_RAW:
@@ -79,10 +78,8 @@ static int ad5624r_write_raw(struct iio_dev *indio_dev,
79 chan->address, val, 78 chan->address, val,
80 chan->scan_type.shift); 79 chan->scan_type.shift);
81 default: 80 default:
82 ret = -EINVAL; 81 return -EINVAL;
83 } 82 }
84
85 return -EINVAL;
86} 83}
87 84
88static const char * const ad5624r_powerdown_modes[] = { 85static const char * const ad5624r_powerdown_modes[] = {
diff --git a/drivers/iio/dac/ad5686.c b/drivers/iio/dac/ad5686.c
index 17aca4d9bd06..f57562aa396f 100644
--- a/drivers/iio/dac/ad5686.c
+++ b/drivers/iio/dac/ad5686.c
@@ -313,7 +313,7 @@ static int ad5686_probe(struct spi_device *spi)
313{ 313{
314 struct ad5686_state *st; 314 struct ad5686_state *st;
315 struct iio_dev *indio_dev; 315 struct iio_dev *indio_dev;
316 int ret, regdone = 0, voltage_uv = 0; 316 int ret, voltage_uv = 0;
317 317
318 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 318 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
319 if (indio_dev == NULL) 319 if (indio_dev == NULL)
@@ -355,7 +355,6 @@ static int ad5686_probe(struct spi_device *spi)
355 indio_dev->channels = st->chip_info->channel; 355 indio_dev->channels = st->chip_info->channel;
356 indio_dev->num_channels = AD5686_DAC_CHANNELS; 356 indio_dev->num_channels = AD5686_DAC_CHANNELS;
357 357
358 regdone = 1;
359 ret = ad5686_spi_write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0, 358 ret = ad5686_spi_write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
360 !!voltage_uv, 0); 359 !!voltage_uv, 0);
361 if (ret) 360 if (ret)
diff --git a/drivers/iio/dac/ad5791.c b/drivers/iio/dac/ad5791.c
index ae49afe2b380..5ba785f18589 100644
--- a/drivers/iio/dac/ad5791.c
+++ b/drivers/iio/dac/ad5791.c
@@ -16,17 +16,16 @@
16#include <linux/sysfs.h> 16#include <linux/sysfs.h>
17#include <linux/regulator/consumer.h> 17#include <linux/regulator/consumer.h>
18#include <linux/module.h> 18#include <linux/module.h>
19#include <linux/bitops.h>
19 20
20#include <linux/iio/iio.h> 21#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h> 22#include <linux/iio/sysfs.h>
22#include <linux/iio/dac/ad5791.h> 23#include <linux/iio/dac/ad5791.h>
23 24
24#define AD5791_RES_MASK(x) ((1 << (x)) - 1) 25#define AD5791_DAC_MASK GENMASK(19, 0)
25#define AD5791_DAC_MASK AD5791_RES_MASK(20)
26#define AD5791_DAC_MSB (1 << 19)
27 26
28#define AD5791_CMD_READ (1 << 23) 27#define AD5791_CMD_READ BIT(23)
29#define AD5791_CMD_WRITE (0 << 23) 28#define AD5791_CMD_WRITE 0
30#define AD5791_ADDR(addr) ((addr) << 20) 29#define AD5791_ADDR(addr) ((addr) << 20)
31 30
32/* Registers */ 31/* Registers */
@@ -37,11 +36,11 @@
37#define AD5791_ADDR_SW_CTRL 4 36#define AD5791_ADDR_SW_CTRL 4
38 37
39/* Control Register */ 38/* Control Register */
40#define AD5791_CTRL_RBUF (1 << 1) 39#define AD5791_CTRL_RBUF BIT(1)
41#define AD5791_CTRL_OPGND (1 << 2) 40#define AD5791_CTRL_OPGND BIT(2)
42#define AD5791_CTRL_DACTRI (1 << 3) 41#define AD5791_CTRL_DACTRI BIT(3)
43#define AD5791_CTRL_BIN2SC (1 << 4) 42#define AD5791_CTRL_BIN2SC BIT(4)
44#define AD5791_CTRL_SDODIS (1 << 5) 43#define AD5791_CTRL_SDODIS BIT(5)
45#define AD5761_CTRL_LINCOMP(x) ((x) << 6) 44#define AD5761_CTRL_LINCOMP(x) ((x) << 6)
46 45
47#define AD5791_LINCOMP_0_10 0 46#define AD5791_LINCOMP_0_10 0
@@ -54,9 +53,9 @@
54#define AD5780_LINCOMP_10_20 12 53#define AD5780_LINCOMP_10_20 12
55 54
56/* Software Control Register */ 55/* Software Control Register */
57#define AD5791_SWCTRL_LDAC (1 << 0) 56#define AD5791_SWCTRL_LDAC BIT(0)
58#define AD5791_SWCTRL_CLR (1 << 1) 57#define AD5791_SWCTRL_CLR BIT(1)
59#define AD5791_SWCTRL_RESET (1 << 2) 58#define AD5791_SWCTRL_RESET BIT(2)
60 59
61#define AD5791_DAC_PWRDN_6K 0 60#define AD5791_DAC_PWRDN_6K 0
62#define AD5791_DAC_PWRDN_3STATE 1 61#define AD5791_DAC_PWRDN_3STATE 1
@@ -72,7 +71,7 @@ struct ad5791_chip_info {
72 71
73/** 72/**
74 * struct ad5791_state - driver instance specific data 73 * struct ad5791_state - driver instance specific data
75 * @us: spi_device 74 * @spi: spi_device
76 * @reg_vdd: positive supply regulator 75 * @reg_vdd: positive supply regulator
77 * @reg_vss: negative supply regulator 76 * @reg_vss: negative supply regulator
78 * @chip_info: chip model specific constants 77 * @chip_info: chip model specific constants
@@ -328,7 +327,7 @@ static int ad5791_write_raw(struct iio_dev *indio_dev,
328 327
329 switch (mask) { 328 switch (mask) {
330 case IIO_CHAN_INFO_RAW: 329 case IIO_CHAN_INFO_RAW:
331 val &= AD5791_RES_MASK(chan->scan_type.realbits); 330 val &= GENMASK(chan->scan_type.realbits - 1, 0);
332 val <<= chan->scan_type.shift; 331 val <<= chan->scan_type.shift;
333 332
334 return ad5791_spi_write(st, chan->address, val); 333 return ad5791_spi_write(st, chan->address, val);
diff --git a/drivers/iio/dac/mcp4922.c b/drivers/iio/dac/mcp4922.c
new file mode 100644
index 000000000000..92cf4ca6981d
--- /dev/null
+++ b/drivers/iio/dac/mcp4922.c
@@ -0,0 +1,216 @@
1/*
2 * mcp4922.c
3 *
4 * Driver for Microchip Digital to Analog Converters.
5 * Supports MCP4902, MCP4912, and MCP4922.
6 *
7 * Copyright (c) 2014 EMAC Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/spi/spi.h>
24#include <linux/iio/iio.h>
25#include <linux/iio/sysfs.h>
26#include <linux/regulator/consumer.h>
27#include <linux/bitops.h>
28
29#define MCP4922_NUM_CHANNELS 2
30
31enum mcp4922_supported_device_ids {
32 ID_MCP4902,
33 ID_MCP4912,
34 ID_MCP4922,
35};
36
37struct mcp4922_state {
38 struct spi_device *spi;
39 unsigned int value[MCP4922_NUM_CHANNELS];
40 unsigned int vref_mv;
41 struct regulator *vref_reg;
42 u8 mosi[2] ____cacheline_aligned;
43};
44
45#define MCP4922_CHAN(chan, bits) { \
46 .type = IIO_VOLTAGE, \
47 .output = 1, \
48 .indexed = 1, \
49 .channel = chan, \
50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
51 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
52 .scan_type = { \
53 .sign = 'u', \
54 .realbits = (bits), \
55 .storagebits = 16, \
56 .shift = 12 - (bits), \
57 }, \
58}
59
60static int mcp4922_spi_write(struct mcp4922_state *state, u8 addr, u32 val)
61{
62 state->mosi[1] = val & 0xff;
63 state->mosi[0] = (addr == 0) ? 0x00 : 0x80;
64 state->mosi[0] |= 0x30 | ((val >> 8) & 0x0f);
65
66 return spi_write(state->spi, state->mosi, 2);
67}
68
69static int mcp4922_read_raw(struct iio_dev *indio_dev,
70 struct iio_chan_spec const *chan,
71 int *val,
72 int *val2,
73 long mask)
74{
75 struct mcp4922_state *state = iio_priv(indio_dev);
76
77 switch (mask) {
78 case IIO_CHAN_INFO_RAW:
79 *val = state->value[chan->channel];
80 return IIO_VAL_INT;
81 case IIO_CHAN_INFO_SCALE:
82 *val = state->vref_mv;
83 *val2 = chan->scan_type.realbits;
84 return IIO_VAL_FRACTIONAL_LOG2;
85 default:
86 return -EINVAL;
87 }
88}
89
90static int mcp4922_write_raw(struct iio_dev *indio_dev,
91 struct iio_chan_spec const *chan,
92 int val,
93 int val2,
94 long mask)
95{
96 struct mcp4922_state *state = iio_priv(indio_dev);
97
98 if (val2 != 0)
99 return -EINVAL;
100
101 switch (mask) {
102 case IIO_CHAN_INFO_RAW:
103 if (val > GENMASK(chan->scan_type.realbits-1, 0))
104 return -EINVAL;
105 val <<= chan->scan_type.shift;
106 state->value[chan->channel] = val;
107 return mcp4922_spi_write(state, chan->channel, val);
108 default:
109 return -EINVAL;
110 }
111}
112
113static const struct iio_chan_spec mcp4922_channels[3][MCP4922_NUM_CHANNELS] = {
114 [ID_MCP4902] = { MCP4922_CHAN(0, 8), MCP4922_CHAN(1, 8) },
115 [ID_MCP4912] = { MCP4922_CHAN(0, 10), MCP4922_CHAN(1, 10) },
116 [ID_MCP4922] = { MCP4922_CHAN(0, 12), MCP4922_CHAN(1, 12) },
117};
118
119static const struct iio_info mcp4922_info = {
120 .read_raw = &mcp4922_read_raw,
121 .write_raw = &mcp4922_write_raw,
122 .driver_module = THIS_MODULE,
123};
124
125static int mcp4922_probe(struct spi_device *spi)
126{
127 struct iio_dev *indio_dev;
128 struct mcp4922_state *state;
129 const struct spi_device_id *id;
130 int ret;
131
132 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
133 if (indio_dev == NULL)
134 return -ENOMEM;
135
136 state = iio_priv(indio_dev);
137 state->spi = spi;
138 state->vref_reg = devm_regulator_get(&spi->dev, "vref");
139 if (IS_ERR(state->vref_reg)) {
140 dev_err(&spi->dev, "Vref regulator not specified\n");
141 return PTR_ERR(state->vref_reg);
142 }
143
144 ret = regulator_enable(state->vref_reg);
145 if (ret) {
146 dev_err(&spi->dev, "Failed to enable vref regulator: %d\n",
147 ret);
148 return ret;
149 }
150
151 ret = regulator_get_voltage(state->vref_reg);
152 if (ret < 0) {
153 dev_err(&spi->dev, "Failed to read vref regulator: %d\n",
154 ret);
155 goto error_disable_reg;
156 }
157 state->vref_mv = ret / 1000;
158
159 spi_set_drvdata(spi, indio_dev);
160 id = spi_get_device_id(spi);
161 indio_dev->dev.parent = &spi->dev;
162 indio_dev->info = &mcp4922_info;
163 indio_dev->modes = INDIO_DIRECT_MODE;
164 indio_dev->channels = mcp4922_channels[id->driver_data];
165 indio_dev->num_channels = MCP4922_NUM_CHANNELS;
166 indio_dev->name = id->name;
167
168 ret = iio_device_register(indio_dev);
169 if (ret) {
170 dev_err(&spi->dev, "Failed to register iio device: %d\n",
171 ret);
172 goto error_disable_reg;
173 }
174
175 return 0;
176
177error_disable_reg:
178 regulator_disable(state->vref_reg);
179
180 return ret;
181}
182
183static int mcp4922_remove(struct spi_device *spi)
184{
185 struct iio_dev *indio_dev = spi_get_drvdata(spi);
186 struct mcp4922_state *state;
187
188 iio_device_unregister(indio_dev);
189 state = iio_priv(indio_dev);
190 regulator_disable(state->vref_reg);
191
192 return 0;
193}
194
195static const struct spi_device_id mcp4922_id[] = {
196 {"mcp4902", ID_MCP4902},
197 {"mcp4912", ID_MCP4912},
198 {"mcp4922", ID_MCP4922},
199 {}
200};
201MODULE_DEVICE_TABLE(spi, mcp4922_id);
202
203static struct spi_driver mcp4922_driver = {
204 .driver = {
205 .name = "mcp4922",
206 .owner = THIS_MODULE,
207 },
208 .probe = mcp4922_probe,
209 .remove = mcp4922_remove,
210 .id_table = mcp4922_id,
211};
212module_spi_driver(mcp4922_driver);
213
214MODULE_AUTHOR("Michael Welling <mwelling@ieee.org>");
215MODULE_DESCRIPTION("Microchip MCP4902, MCP4912, MCP4922 DAC");
216MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/adis16260.c b/drivers/iio/gyro/adis16260.c
index 22b6fb80fa1a..75fe0edd3d0f 100644
--- a/drivers/iio/gyro/adis16260.c
+++ b/drivers/iio/gyro/adis16260.c
@@ -101,65 +101,6 @@
101#define ADIS16260_SCAN_TEMP 3 101#define ADIS16260_SCAN_TEMP 3
102#define ADIS16260_SCAN_ANGL 4 102#define ADIS16260_SCAN_ANGL 4
103 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 */ 104/* Power down the device */
164static int adis16260_stop_device(struct iio_dev *indio_dev) 105static int adis16260_stop_device(struct iio_dev *indio_dev)
165{ 106{
@@ -174,18 +115,19 @@ static int adis16260_stop_device(struct iio_dev *indio_dev)
174 return ret; 115 return ret;
175} 116}
176 117
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[] = { 118static const struct iio_chan_spec adis16260_channels[] = {
182 ADIS_GYRO_CHAN(X, ADIS16260_GYRO_OUT, ADIS16260_SCAN_GYRO, 119 ADIS_GYRO_CHAN(X, ADIS16260_GYRO_OUT, ADIS16260_SCAN_GYRO,
183 BIT(IIO_CHAN_INFO_CALIBBIAS) | 120 BIT(IIO_CHAN_INFO_CALIBBIAS) |
184 BIT(IIO_CHAN_INFO_CALIBSCALE), 14), 121 BIT(IIO_CHAN_INFO_CALIBSCALE),
185 ADIS_INCLI_CHAN(X, ADIS16260_ANGL_OUT, ADIS16260_SCAN_ANGL, 0, 14), 122 BIT(IIO_CHAN_INFO_SAMP_FREQ), 14),
186 ADIS_TEMP_CHAN(ADIS16260_TEMP_OUT, ADIS16260_SCAN_TEMP, 12), 123 ADIS_INCLI_CHAN(X, ADIS16260_ANGL_OUT, ADIS16260_SCAN_ANGL, 0,
187 ADIS_SUPPLY_CHAN(ADIS16260_SUPPLY_OUT, ADIS16260_SCAN_SUPPLY, 12), 124 BIT(IIO_CHAN_INFO_SAMP_FREQ), 14),
188 ADIS_AUX_ADC_CHAN(ADIS16260_AUX_ADC, ADIS16260_SCAN_AUX_ADC, 12), 125 ADIS_TEMP_CHAN(ADIS16260_TEMP_OUT, ADIS16260_SCAN_TEMP,
126 BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
127 ADIS_SUPPLY_CHAN(ADIS16260_SUPPLY_OUT, ADIS16260_SCAN_SUPPLY,
128 BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
129 ADIS_AUX_ADC_CHAN(ADIS16260_AUX_ADC, ADIS16260_SCAN_AUX_ADC,
130 BIT(IIO_CHAN_INFO_SAMP_FREQ), 12),
189 IIO_CHAN_SOFT_TIMESTAMP(5), 131 IIO_CHAN_SOFT_TIMESTAMP(5),
190}; 132};
191 133
@@ -258,6 +200,20 @@ static int adis16260_read_raw(struct iio_dev *indio_dev,
258 200
259 *val = val16; 201 *val = val16;
260 return IIO_VAL_INT; 202 return IIO_VAL_INT;
203 case IIO_CHAN_INFO_SAMP_FREQ:
204 ret = adis_read_reg_16(adis, ADIS16260_SMPL_PRD, &val16);
205 if (ret)
206 return ret;
207
208 if (spi_get_device_id(adis->spi)->driver_data)
209 /* If an adis16251 */
210 *val = (val16 & ADIS16260_SMPL_PRD_TIME_BASE) ?
211 8 : 256;
212 else
213 *val = (val16 & ADIS16260_SMPL_PRD_TIME_BASE) ?
214 66 : 2048;
215 *val /= (val16 & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
216 return IIO_VAL_INT;
261 } 217 }
262 return -EINVAL; 218 return -EINVAL;
263} 219}
@@ -269,7 +225,9 @@ static int adis16260_write_raw(struct iio_dev *indio_dev,
269 long mask) 225 long mask)
270{ 226{
271 struct adis *adis = iio_priv(indio_dev); 227 struct adis *adis = iio_priv(indio_dev);
228 int ret;
272 u8 addr; 229 u8 addr;
230 u8 t;
273 231
274 switch (mask) { 232 switch (mask) {
275 case IIO_CHAN_INFO_CALIBBIAS: 233 case IIO_CHAN_INFO_CALIBBIAS:
@@ -284,21 +242,31 @@ static int adis16260_write_raw(struct iio_dev *indio_dev,
284 242
285 addr = adis16260_addresses[chan->scan_index][1]; 243 addr = adis16260_addresses[chan->scan_index][1];
286 return adis_write_reg_16(adis, addr, val); 244 return adis_write_reg_16(adis, addr, val);
245 case IIO_CHAN_INFO_SAMP_FREQ:
246 mutex_lock(&indio_dev->mlock);
247 if (spi_get_device_id(adis->spi)->driver_data)
248 t = 256 / val;
249 else
250 t = 2048 / val;
251
252 if (t > ADIS16260_SMPL_PRD_DIV_MASK)
253 t = ADIS16260_SMPL_PRD_DIV_MASK;
254 else if (t > 0)
255 t--;
256
257 if (t >= 0x0A)
258 adis->spi->max_speed_hz = ADIS16260_SPI_SLOW;
259 else
260 adis->spi->max_speed_hz = ADIS16260_SPI_FAST;
261 ret = adis_write_reg_8(adis, ADIS16260_SMPL_PRD, t);
262
263 mutex_unlock(&indio_dev->mlock);
264 return ret;
287 } 265 }
288 return -EINVAL; 266 return -EINVAL;
289} 267}
290 268
291static struct attribute *adis16260_attributes[] = {
292 &iio_dev_attr_sampling_frequency.dev_attr.attr,
293 NULL
294};
295
296static const struct attribute_group adis16260_attribute_group = {
297 .attrs = adis16260_attributes,
298};
299
300static const struct iio_info adis16260_info = { 269static const struct iio_info adis16260_info = {
301 .attrs = &adis16260_attribute_group,
302 .read_raw = &adis16260_read_raw, 270 .read_raw = &adis16260_read_raw,
303 .write_raw = &adis16260_write_raw, 271 .write_raw = &adis16260_write_raw,
304 .update_scan_mode = adis_update_scan_mode, 272 .update_scan_mode = adis_update_scan_mode,
diff --git a/drivers/iio/gyro/itg3200_core.c b/drivers/iio/gyro/itg3200_core.c
index 8295e318399f..6a8020d48140 100644
--- a/drivers/iio/gyro/itg3200_core.c
+++ b/drivers/iio/gyro/itg3200_core.c
@@ -90,6 +90,7 @@ static int itg3200_read_raw(struct iio_dev *indio_dev,
90{ 90{
91 int ret = 0; 91 int ret = 0;
92 u8 reg; 92 u8 reg;
93 u8 regval;
93 94
94 switch (info) { 95 switch (info) {
95 case IIO_CHAN_INFO_RAW: 96 case IIO_CHAN_INFO_RAW:
@@ -107,65 +108,60 @@ static int itg3200_read_raw(struct iio_dev *indio_dev,
107 /* Only the temperature channel has an offset */ 108 /* Only the temperature channel has an offset */
108 *val = 23000; 109 *val = 23000;
109 return IIO_VAL_INT; 110 return IIO_VAL_INT;
110 default: 111 case IIO_CHAN_INFO_SAMP_FREQ:
111 return -EINVAL; 112 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &regval);
112 } 113 if (ret)
113} 114 return ret;
114
115static ssize_t itg3200_read_frequency(struct device *dev,
116 struct device_attribute *attr, char *buf)
117{
118 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
119 int ret, sps;
120 u8 val;
121
122 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &val);
123 if (ret)
124 return ret;
125 115
126 sps = (val & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000; 116 *val = (regval & ITG3200_DLPF_CFG_MASK) ? 1000 : 8000;
127 117
128 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_SAMPLE_RATE_DIV, &val); 118 ret = itg3200_read_reg_8(indio_dev,
129 if (ret) 119 ITG3200_REG_SAMPLE_RATE_DIV,
130 return ret; 120 &regval);
121 if (ret)
122 return ret;
131 123
132 sps /= val + 1; 124 *val /= regval + 1;
125 return IIO_VAL_INT;
133 126
134 return sprintf(buf, "%d\n", sps); 127 default:
128 return -EINVAL;
129 }
135} 130}
136 131
137static ssize_t itg3200_write_frequency(struct device *dev, 132static int itg3200_write_raw(struct iio_dev *indio_dev,
138 struct device_attribute *attr, 133 struct iio_chan_spec const *chan,
139 const char *buf, 134 int val,
140 size_t len) 135 int val2,
136 long mask)
141{ 137{
142 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
143 unsigned val;
144 int ret; 138 int ret;
145 u8 t; 139 u8 t;
146 140
147 ret = kstrtouint(buf, 10, &val); 141 switch (mask) {
148 if (ret) 142 case IIO_CHAN_INFO_SAMP_FREQ:
149 return ret; 143 if (val == 0 || val2 != 0)
144 return -EINVAL;
150 145
151 mutex_lock(&indio_dev->mlock); 146 mutex_lock(&indio_dev->mlock);
152 147
153 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t); 148 ret = itg3200_read_reg_8(indio_dev, ITG3200_REG_DLPF, &t);
154 if (ret) 149 if (ret) {
155 goto err_ret; 150 mutex_unlock(&indio_dev->mlock);
151 return ret;
152 }
153 t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
156 154
157 if (val == 0) { 155 ret = itg3200_write_reg_8(indio_dev,
158 ret = -EINVAL; 156 ITG3200_REG_SAMPLE_RATE_DIV,
159 goto err_ret; 157 t);
160 }
161 t = ((t & ITG3200_DLPF_CFG_MASK) ? 1000u : 8000u) / val - 1;
162 158
163 ret = itg3200_write_reg_8(indio_dev, ITG3200_REG_SAMPLE_RATE_DIV, t); 159 mutex_unlock(&indio_dev->mlock);
164 160 return ret;
165err_ret:
166 mutex_unlock(&indio_dev->mlock);
167 161
168 return ret ? ret : len; 162 default:
163 return -EINVAL;
164 }
169} 165}
170 166
171/* 167/*
@@ -255,6 +251,7 @@ err_ret:
255 .channel2 = IIO_MOD_ ## _mod, \ 251 .channel2 = IIO_MOD_ ## _mod, \
256 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 252 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
257 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 253 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
254 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
258 .address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \ 255 .address = ITG3200_REG_GYRO_ ## _mod ## OUT_H, \
259 .scan_index = ITG3200_SCAN_GYRO_ ## _mod, \ 256 .scan_index = ITG3200_SCAN_GYRO_ ## _mod, \
260 .scan_type = ITG3200_ST, \ 257 .scan_type = ITG3200_ST, \
@@ -267,6 +264,7 @@ static const struct iio_chan_spec itg3200_channels[] = {
267 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 264 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
268 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 265 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
269 BIT(IIO_CHAN_INFO_SCALE), 266 BIT(IIO_CHAN_INFO_SCALE),
267 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
270 .address = ITG3200_REG_TEMP_OUT_H, 268 .address = ITG3200_REG_TEMP_OUT_H,
271 .scan_index = ITG3200_SCAN_TEMP, 269 .scan_index = ITG3200_SCAN_TEMP,
272 .scan_type = ITG3200_ST, 270 .scan_type = ITG3200_ST,
@@ -277,22 +275,9 @@ static const struct iio_chan_spec itg3200_channels[] = {
277 IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS), 275 IIO_CHAN_SOFT_TIMESTAMP(ITG3200_SCAN_ELEMENTS),
278}; 276};
279 277
280/* IIO device attributes */
281static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, itg3200_read_frequency,
282 itg3200_write_frequency);
283
284static struct attribute *itg3200_attributes[] = {
285 &iio_dev_attr_sampling_frequency.dev_attr.attr,
286 NULL
287};
288
289static const struct attribute_group itg3200_attribute_group = {
290 .attrs = itg3200_attributes,
291};
292
293static const struct iio_info itg3200_info = { 278static const struct iio_info itg3200_info = {
294 .attrs = &itg3200_attribute_group,
295 .read_raw = &itg3200_read_raw, 279 .read_raw = &itg3200_read_raw,
280 .write_raw = &itg3200_write_raw,
296 .driver_module = THIS_MODULE, 281 .driver_module = THIS_MODULE,
297}; 282};
298 283
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c
index ed74a9069989..f156fc6c5c6c 100644
--- a/drivers/iio/gyro/st_gyro_core.c
+++ b/drivers/iio/gyro/st_gyro_core.c
@@ -245,6 +245,9 @@ static int st_gyro_read_raw(struct iio_dev *indio_dev,
245 *val = 0; 245 *val = 0;
246 *val2 = gdata->current_fullscale->gain; 246 *val2 = gdata->current_fullscale->gain;
247 return IIO_VAL_INT_PLUS_MICRO; 247 return IIO_VAL_INT_PLUS_MICRO;
248 case IIO_CHAN_INFO_SAMP_FREQ:
249 *val = gdata->odr;
250 return IIO_VAL_INT;
248 default: 251 default:
249 return -EINVAL; 252 return -EINVAL;
250 } 253 }
@@ -262,6 +265,13 @@ static int st_gyro_write_raw(struct iio_dev *indio_dev,
262 case IIO_CHAN_INFO_SCALE: 265 case IIO_CHAN_INFO_SCALE:
263 err = st_sensors_set_fullscale_by_gain(indio_dev, val2); 266 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
264 break; 267 break;
268 case IIO_CHAN_INFO_SAMP_FREQ:
269 if (val2)
270 return -EINVAL;
271 mutex_lock(&indio_dev->mlock);
272 err = st_sensors_set_odr(indio_dev, val);
273 mutex_unlock(&indio_dev->mlock);
274 return err;
265 default: 275 default:
266 err = -EINVAL; 276 err = -EINVAL;
267 } 277 }
@@ -269,14 +279,12 @@ static int st_gyro_write_raw(struct iio_dev *indio_dev,
269 return err; 279 return err;
270} 280}
271 281
272static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
273static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 282static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
274static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available); 283static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_anglvel_scale_available);
275 284
276static struct attribute *st_gyro_attributes[] = { 285static struct attribute *st_gyro_attributes[] = {
277 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 286 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
278 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr, 287 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
279 &iio_dev_attr_sampling_frequency.dev_attr.attr,
280 NULL, 288 NULL,
281}; 289};
282 290
diff --git a/drivers/iio/gyro/st_gyro_i2c.c b/drivers/iio/gyro/st_gyro_i2c.c
index 23c12f361b05..8fa0ad2ef4ef 100644
--- a/drivers/iio/gyro/st_gyro_i2c.c
+++ b/drivers/iio/gyro/st_gyro_i2c.c
@@ -18,6 +18,43 @@
18#include <linux/iio/common/st_sensors_i2c.h> 18#include <linux/iio/common/st_sensors_i2c.h>
19#include "st_gyro.h" 19#include "st_gyro.h"
20 20
21#ifdef CONFIG_OF
22static const struct of_device_id st_gyro_of_match[] = {
23 {
24 .compatible = "st,l3g4200d-gyro",
25 .data = L3G4200D_GYRO_DEV_NAME,
26 },
27 {
28 .compatible = "st,lsm330d-gyro",
29 .data = LSM330D_GYRO_DEV_NAME,
30 },
31 {
32 .compatible = "st,lsm330dl-gyro",
33 .data = LSM330DL_GYRO_DEV_NAME,
34 },
35 {
36 .compatible = "st,lsm330dlc-gyro",
37 .data = LSM330DLC_GYRO_DEV_NAME,
38 },
39 {
40 .compatible = "st,l3gd20-gyro",
41 .data = L3GD20_GYRO_DEV_NAME,
42 },
43 {
44 .compatible = "st,l3g4is-gyro",
45 .data = L3G4IS_GYRO_DEV_NAME,
46 },
47 {
48 .compatible = "st,lsm330-gyro",
49 .data = LSM330_GYRO_DEV_NAME,
50 },
51 {},
52};
53MODULE_DEVICE_TABLE(of, st_gyro_of_match);
54#else
55#define st_gyro_of_match NULL
56#endif
57
21static int st_gyro_i2c_probe(struct i2c_client *client, 58static int st_gyro_i2c_probe(struct i2c_client *client,
22 const struct i2c_device_id *id) 59 const struct i2c_device_id *id)
23{ 60{
@@ -31,6 +68,7 @@ static int st_gyro_i2c_probe(struct i2c_client *client,
31 68
32 gdata = iio_priv(indio_dev); 69 gdata = iio_priv(indio_dev);
33 gdata->dev = &client->dev; 70 gdata->dev = &client->dev;
71 st_sensors_of_i2c_probe(client, st_gyro_of_match);
34 72
35 st_sensors_i2c_configure(indio_dev, client, gdata); 73 st_sensors_i2c_configure(indio_dev, client, gdata);
36 74
@@ -65,6 +103,7 @@ static struct i2c_driver st_gyro_driver = {
65 .driver = { 103 .driver = {
66 .owner = THIS_MODULE, 104 .owner = THIS_MODULE,
67 .name = "st-gyro-i2c", 105 .name = "st-gyro-i2c",
106 .of_match_table = of_match_ptr(st_gyro_of_match),
68 }, 107 },
69 .probe = st_gyro_i2c_probe, 108 .probe = st_gyro_i2c_probe,
70 .remove = st_gyro_i2c_remove, 109 .remove = st_gyro_i2c_remove,
diff --git a/drivers/iio/imu/adis16400_buffer.c b/drivers/iio/imu/adis16400_buffer.c
index f2cf829e5df1..6e727ffe5262 100644
--- a/drivers/iio/imu/adis16400_buffer.c
+++ b/drivers/iio/imu/adis16400_buffer.c
@@ -18,7 +18,7 @@ int adis16400_update_scan_mode(struct iio_dev *indio_dev,
18{ 18{
19 struct adis16400_state *st = iio_priv(indio_dev); 19 struct adis16400_state *st = iio_priv(indio_dev);
20 struct adis *adis = &st->adis; 20 struct adis *adis = &st->adis;
21 uint16_t *tx, *rx; 21 uint16_t *tx;
22 22
23 if (st->variant->flags & ADIS16400_NO_BURST) 23 if (st->variant->flags & ADIS16400_NO_BURST)
24 return adis_update_scan_mode(indio_dev, scan_mask); 24 return adis_update_scan_mode(indio_dev, scan_mask);
@@ -35,7 +35,6 @@ int adis16400_update_scan_mode(struct iio_dev *indio_dev,
35 if (!adis->buffer) 35 if (!adis->buffer)
36 return -ENOMEM; 36 return -ENOMEM;
37 37
38 rx = adis->buffer;
39 tx = adis->buffer + indio_dev->scan_bytes; 38 tx = adis->buffer + indio_dev->scan_bytes;
40 39
41 tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD); 40 tx[0] = ADIS_READ_REG(ADIS16400_GLOB_CMD);
diff --git a/drivers/iio/imu/adis16400_core.c b/drivers/iio/imu/adis16400_core.c
index 433583b6f800..b70873de04ea 100644
--- a/drivers/iio/imu/adis16400_core.c
+++ b/drivers/iio/imu/adis16400_core.c
@@ -214,21 +214,6 @@ static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
214 return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val); 214 return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
215} 215}
216 216
217static ssize_t adis16400_read_frequency(struct device *dev,
218 struct device_attribute *attr,
219 char *buf)
220{
221 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
222 struct adis16400_state *st = iio_priv(indio_dev);
223 int ret;
224
225 ret = st->variant->get_freq(st);
226 if (ret < 0)
227 return ret;
228
229 return sprintf(buf, "%d.%.3d\n", ret / 1000, ret % 1000);
230}
231
232static const unsigned adis16400_3db_divisors[] = { 217static const unsigned adis16400_3db_divisors[] = {
233 [0] = 2, /* Special case */ 218 [0] = 2, /* Special case */
234 [1] = 6, 219 [1] = 6,
@@ -260,30 +245,6 @@ static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
260 return ret; 245 return ret;
261} 246}
262 247
263static ssize_t adis16400_write_frequency(struct device *dev,
264 struct device_attribute *attr, const char *buf, size_t len)
265{
266 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
267 struct adis16400_state *st = iio_priv(indio_dev);
268 int i, f, val;
269 int ret;
270
271 ret = iio_str_to_fixpoint(buf, 100, &i, &f);
272 if (ret)
273 return ret;
274
275 val = i * 1000 + f;
276
277 if (val <= 0)
278 return -EINVAL;
279
280 mutex_lock(&indio_dev->mlock);
281 st->variant->set_freq(st, val);
282 mutex_unlock(&indio_dev->mlock);
283
284 return len;
285}
286
287/* Power down the device */ 248/* Power down the device */
288static int adis16400_stop_device(struct iio_dev *indio_dev) 249static int adis16400_stop_device(struct iio_dev *indio_dev)
289{ 250{
@@ -350,10 +311,6 @@ err_ret:
350 return ret; 311 return ret;
351} 312}
352 313
353static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
354 adis16400_read_frequency,
355 adis16400_write_frequency);
356
357static const uint8_t adis16400_addresses[] = { 314static const uint8_t adis16400_addresses[] = {
358 [ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF, 315 [ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
359 [ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF, 316 [ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
@@ -394,6 +351,16 @@ static int adis16400_write_raw(struct iio_dev *indio_dev,
394 val * 1000 + val2 / 1000); 351 val * 1000 + val2 / 1000);
395 mutex_unlock(&indio_dev->mlock); 352 mutex_unlock(&indio_dev->mlock);
396 return ret; 353 return ret;
354 case IIO_CHAN_INFO_SAMP_FREQ:
355 sps = val * 1000 + val2 / 1000;
356
357 if (sps <= 0)
358 return -EINVAL;
359
360 mutex_lock(&indio_dev->mlock);
361 ret = st->variant->set_freq(st, sps);
362 mutex_unlock(&indio_dev->mlock);
363 return ret;
397 default: 364 default:
398 return -EINVAL; 365 return -EINVAL;
399 } 366 }
@@ -474,6 +441,13 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
474 if (ret < 0) 441 if (ret < 0)
475 return ret; 442 return ret;
476 return IIO_VAL_INT_PLUS_MICRO; 443 return IIO_VAL_INT_PLUS_MICRO;
444 case IIO_CHAN_INFO_SAMP_FREQ:
445 ret = st->variant->get_freq(st);
446 if (ret < 0)
447 return ret;
448 *val = ret / 1000;
449 *val2 = (ret % 1000) * 1000;
450 return IIO_VAL_INT_PLUS_MICRO;
477 default: 451 default:
478 return -EINVAL; 452 return -EINVAL;
479 } 453 }
@@ -486,6 +460,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
486 .extend_name = name, \ 460 .extend_name = name, \
487 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 461 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
488 BIT(IIO_CHAN_INFO_SCALE), \ 462 BIT(IIO_CHAN_INFO_SCALE), \
463 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
489 .address = (addr), \ 464 .address = (addr), \
490 .scan_index = (si), \ 465 .scan_index = (si), \
491 .scan_type = { \ 466 .scan_type = { \
@@ -511,6 +486,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
511 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 486 BIT(IIO_CHAN_INFO_CALIBBIAS), \
512 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 487 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
513 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 488 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
489 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
514 .address = addr, \ 490 .address = addr, \
515 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \ 491 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
516 .scan_type = { \ 492 .scan_type = { \
@@ -530,6 +506,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
530 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 506 BIT(IIO_CHAN_INFO_CALIBBIAS), \
531 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 507 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
532 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 508 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
509 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
533 .address = (addr), \ 510 .address = (addr), \
534 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \ 511 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
535 .scan_type = { \ 512 .scan_type = { \
@@ -548,6 +525,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
548 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 525 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
549 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 526 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
550 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 527 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
528 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
551 .address = (addr), \ 529 .address = (addr), \
552 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \ 530 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
553 .scan_type = { \ 531 .scan_type = { \
@@ -573,6 +551,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
573 BIT(IIO_CHAN_INFO_SCALE), \ 551 BIT(IIO_CHAN_INFO_SCALE), \
574 .info_mask_shared_by_type = \ 552 .info_mask_shared_by_type = \
575 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 553 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
554 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
576 .address = (addr), \ 555 .address = (addr), \
577 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \ 556 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
578 .scan_type = { \ 557 .scan_type = { \
@@ -591,6 +570,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
591 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 570 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
592 BIT(IIO_CHAN_INFO_OFFSET) | \ 571 BIT(IIO_CHAN_INFO_OFFSET) | \
593 BIT(IIO_CHAN_INFO_SCALE), \ 572 BIT(IIO_CHAN_INFO_SCALE), \
573 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
594 .address = (addr), \ 574 .address = (addr), \
595 .scan_index = ADIS16350_SCAN_TEMP_X, \ 575 .scan_index = ADIS16350_SCAN_TEMP_X, \
596 .scan_type = { \ 576 .scan_type = { \
@@ -608,6 +588,7 @@ static int adis16400_read_raw(struct iio_dev *indio_dev,
608 .channel2 = IIO_MOD_ ## mod, \ 588 .channel2 = IIO_MOD_ ## mod, \
609 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 589 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
610 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 590 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
591 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
611 .address = (addr), \ 592 .address = (addr), \
612 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \ 593 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
613 .scan_type = { \ 594 .scan_type = { \
@@ -649,6 +630,7 @@ static const struct iio_chan_spec adis16448_channels[] = {
649 .type = IIO_PRESSURE, 630 .type = IIO_PRESSURE,
650 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 631 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
651 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 632 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
633 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
652 .address = ADIS16448_BARO_OUT, 634 .address = ADIS16448_BARO_OUT,
653 .scan_index = ADIS16400_SCAN_BARO, 635 .scan_index = ADIS16400_SCAN_BARO,
654 .scan_type = { 636 .scan_type = {
@@ -704,15 +686,6 @@ static const struct iio_chan_spec adis16334_channels[] = {
704 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP), 686 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
705}; 687};
706 688
707static struct attribute *adis16400_attributes[] = {
708 &iio_dev_attr_sampling_frequency.dev_attr.attr,
709 NULL
710};
711
712static const struct attribute_group adis16400_attribute_group = {
713 .attrs = adis16400_attributes,
714};
715
716static struct adis16400_chip_info adis16400_chips[] = { 689static struct adis16400_chip_info adis16400_chips[] = {
717 [ADIS16300] = { 690 [ADIS16300] = {
718 .channels = adis16300_channels, 691 .channels = adis16300_channels,
@@ -813,7 +786,6 @@ static const struct iio_info adis16400_info = {
813 .driver_module = THIS_MODULE, 786 .driver_module = THIS_MODULE,
814 .read_raw = &adis16400_read_raw, 787 .read_raw = &adis16400_read_raw,
815 .write_raw = &adis16400_write_raw, 788 .write_raw = &adis16400_write_raw,
816 .attrs = &adis16400_attribute_group,
817 .update_scan_mode = adis16400_update_scan_mode, 789 .update_scan_mode = adis16400_update_scan_mode,
818 .debugfs_reg_access = adis_debugfs_reg_access, 790 .debugfs_reg_access = adis_debugfs_reg_access,
819}; 791};
diff --git a/drivers/iio/imu/adis16480.c b/drivers/iio/imu/adis16480.c
index dd4206cac62d..989605dd6f78 100644
--- a/drivers/iio/imu/adis16480.c
+++ b/drivers/iio/imu/adis16480.c
@@ -257,11 +257,16 @@ static int adis16480_debugfs_init(struct iio_dev *indio_dev)
257 257
258#endif 258#endif
259 259
260static int adis16480_set_freq(struct adis16480 *st, unsigned int freq) 260static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2)
261{ 261{
262 struct adis16480 *st = iio_priv(indio_dev);
262 unsigned int t; 263 unsigned int t;
263 264
264 t = 2460000 / freq; 265 t = val * 1000 + val2 / 1000;
266 if (t <= 0)
267 return -EINVAL;
268
269 t = 2460000 / t;
265 if (t > 2048) 270 if (t > 2048)
266 t = 2048; 271 t = 2048;
267 272
@@ -271,65 +276,24 @@ static int adis16480_set_freq(struct adis16480 *st, unsigned int freq)
271 return adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t); 276 return adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t);
272} 277}
273 278
274static int adis16480_get_freq(struct adis16480 *st, unsigned int *freq) 279static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
275{ 280{
281 struct adis16480 *st = iio_priv(indio_dev);
276 uint16_t t; 282 uint16_t t;
277 int ret; 283 int ret;
284 unsigned freq;
278 285
279 ret = adis_read_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, &t); 286 ret = adis_read_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, &t);
280 if (ret < 0) 287 if (ret < 0)
281 return ret; 288 return ret;
282 289
283 *freq = 2460000 / (t + 1); 290 freq = 2460000 / (t + 1);
291 *val = freq / 1000;
292 *val2 = (freq % 1000) * 1000;
284 293
285 return 0; 294 return IIO_VAL_INT_PLUS_MICRO;
286} 295}
287 296
288static ssize_t adis16480_read_frequency(struct device *dev,
289 struct device_attribute *attr,
290 char *buf)
291{
292 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
293 struct adis16480 *st = iio_priv(indio_dev);
294 unsigned int freq;
295 int ret;
296
297 ret = adis16480_get_freq(st, &freq);
298 if (ret < 0)
299 return ret;
300
301 return sprintf(buf, "%d.%.3d\n", freq / 1000, freq % 1000);
302}
303
304static ssize_t adis16480_write_frequency(struct device *dev,
305 struct device_attribute *attr,
306 const char *buf,
307 size_t len)
308{
309 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
310 struct adis16480 *st = iio_priv(indio_dev);
311 int freq_int, freq_fract;
312 long val;
313 int ret;
314
315 ret = iio_str_to_fixpoint(buf, 100, &freq_int, &freq_fract);
316 if (ret)
317 return ret;
318
319 val = freq_int * 1000 + freq_fract;
320
321 if (val <= 0)
322 return -EINVAL;
323
324 ret = adis16480_set_freq(st, val);
325
326 return ret ? ret : len;
327}
328
329static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
330 adis16480_read_frequency,
331 adis16480_write_frequency);
332
333enum { 297enum {
334 ADIS16480_SCAN_GYRO_X, 298 ADIS16480_SCAN_GYRO_X,
335 ADIS16480_SCAN_GYRO_Y, 299 ADIS16480_SCAN_GYRO_Y,
@@ -571,6 +535,8 @@ static int adis16480_read_raw(struct iio_dev *indio_dev,
571 return adis16480_get_calibscale(indio_dev, chan, val); 535 return adis16480_get_calibscale(indio_dev, chan, val);
572 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 536 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
573 return adis16480_get_filter_freq(indio_dev, chan, val); 537 return adis16480_get_filter_freq(indio_dev, chan, val);
538 case IIO_CHAN_INFO_SAMP_FREQ:
539 return adis16480_get_freq(indio_dev, val, val2);
574 default: 540 default:
575 return -EINVAL; 541 return -EINVAL;
576 } 542 }
@@ -586,6 +552,9 @@ static int adis16480_write_raw(struct iio_dev *indio_dev,
586 return adis16480_set_calibscale(indio_dev, chan, val); 552 return adis16480_set_calibscale(indio_dev, chan, val);
587 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 553 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
588 return adis16480_set_filter_freq(indio_dev, chan, val); 554 return adis16480_set_filter_freq(indio_dev, chan, val);
555 case IIO_CHAN_INFO_SAMP_FREQ:
556 return adis16480_set_freq(indio_dev, val, val2);
557
589 default: 558 default:
590 return -EINVAL; 559 return -EINVAL;
591 } 560 }
@@ -600,6 +569,7 @@ static int adis16480_write_raw(struct iio_dev *indio_dev,
600 BIT(IIO_CHAN_INFO_CALIBBIAS) | \ 569 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
601 _info_sep, \ 570 _info_sep, \
602 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 571 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
572 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
603 .address = (_address), \ 573 .address = (_address), \
604 .scan_index = (_si), \ 574 .scan_index = (_si), \
605 .scan_type = { \ 575 .scan_type = { \
@@ -638,6 +608,7 @@ static int adis16480_write_raw(struct iio_dev *indio_dev,
638 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 608 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
639 BIT(IIO_CHAN_INFO_CALIBBIAS) | \ 609 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
640 BIT(IIO_CHAN_INFO_SCALE), \ 610 BIT(IIO_CHAN_INFO_SCALE), \
611 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
641 .address = ADIS16480_REG_BAROM_OUT, \ 612 .address = ADIS16480_REG_BAROM_OUT, \
642 .scan_index = ADIS16480_SCAN_BARO, \ 613 .scan_index = ADIS16480_SCAN_BARO, \
643 .scan_type = { \ 614 .scan_type = { \
@@ -655,6 +626,7 @@ static int adis16480_write_raw(struct iio_dev *indio_dev,
655 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 626 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
656 BIT(IIO_CHAN_INFO_SCALE) | \ 627 BIT(IIO_CHAN_INFO_SCALE) | \
657 BIT(IIO_CHAN_INFO_OFFSET), \ 628 BIT(IIO_CHAN_INFO_OFFSET), \
629 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
658 .address = ADIS16480_REG_TEMP_OUT, \ 630 .address = ADIS16480_REG_TEMP_OUT, \
659 .scan_index = ADIS16480_SCAN_TEMP, \ 631 .scan_index = ADIS16480_SCAN_TEMP, \
660 .scan_type = { \ 632 .scan_type = { \
@@ -717,17 +689,7 @@ static const struct adis16480_chip_info adis16480_chip_info[] = {
717 }, 689 },
718}; 690};
719 691
720static struct attribute *adis16480_attributes[] = {
721 &iio_dev_attr_sampling_frequency.dev_attr.attr,
722 NULL
723};
724
725static const struct attribute_group adis16480_attribute_group = {
726 .attrs = adis16480_attributes,
727};
728
729static const struct iio_info adis16480_info = { 692static const struct iio_info adis16480_info = {
730 .attrs = &adis16480_attribute_group,
731 .read_raw = &adis16480_read_raw, 693 .read_raw = &adis16480_read_raw,
732 .write_raw = &adis16480_write_raw, 694 .write_raw = &adis16480_write_raw,
733 .update_scan_mode = adis_update_scan_mode, 695 .update_scan_mode = adis_update_scan_mode,
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 9f1a14009901..0472ee268271 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -39,10 +39,7 @@ static bool iio_buffer_is_active(struct iio_buffer *buf)
39 39
40static bool iio_buffer_data_available(struct iio_buffer *buf) 40static bool iio_buffer_data_available(struct iio_buffer *buf)
41{ 41{
42 if (buf->access->data_available) 42 return buf->access->data_available(buf);
43 return buf->access->data_available(buf);
44
45 return buf->stufftoread;
46} 43}
47 44
48/** 45/**
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 4b1f375c5659..af3e76d652ba 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -87,6 +87,10 @@ static const char * const iio_modifier_names[] = {
87 [IIO_MOD_QUATERNION] = "quaternion", 87 [IIO_MOD_QUATERNION] = "quaternion",
88 [IIO_MOD_TEMP_AMBIENT] = "ambient", 88 [IIO_MOD_TEMP_AMBIENT] = "ambient",
89 [IIO_MOD_TEMP_OBJECT] = "object", 89 [IIO_MOD_TEMP_OBJECT] = "object",
90 [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
91 [IIO_MOD_NORTH_TRUE] = "from_north_true",
92 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
93 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
90}; 94};
91 95
92/* relies on pairs of these shared then separate */ 96/* relies on pairs of these shared then separate */
diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index bfbf4d419f41..0c1e37e3120a 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -209,6 +209,7 @@ static const char * const iio_ev_info_text[] = {
209 [IIO_EV_INFO_ENABLE] = "en", 209 [IIO_EV_INFO_ENABLE] = "en",
210 [IIO_EV_INFO_VALUE] = "value", 210 [IIO_EV_INFO_VALUE] = "value",
211 [IIO_EV_INFO_HYSTERESIS] = "hysteresis", 211 [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
212 [IIO_EV_INFO_PERIOD] = "period",
212}; 213};
213 214
214static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr) 215static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 3383b025f62e..d31098e0c43f 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -114,7 +114,7 @@ static struct iio_trigger *iio_trigger_find_by_name(const char *name,
114 return trig; 114 return trig;
115} 115}
116 116
117void iio_trigger_poll(struct iio_trigger *trig, s64 time) 117void iio_trigger_poll(struct iio_trigger *trig)
118{ 118{
119 int i; 119 int i;
120 120
@@ -133,12 +133,12 @@ EXPORT_SYMBOL(iio_trigger_poll);
133 133
134irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private) 134irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
135{ 135{
136 iio_trigger_poll(private, iio_get_time_ns()); 136 iio_trigger_poll(private);
137 return IRQ_HANDLED; 137 return IRQ_HANDLED;
138} 138}
139EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll); 139EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
140 140
141void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time) 141void iio_trigger_poll_chained(struct iio_trigger *trig)
142{ 142{
143 int i; 143 int i;
144 144
@@ -161,7 +161,7 @@ void iio_trigger_notify_done(struct iio_trigger *trig)
161 trig->ops->try_reenable) 161 trig->ops->try_reenable)
162 if (trig->ops->try_reenable(trig)) 162 if (trig->ops->try_reenable(trig))
163 /* Missed an interrupt so launch new poll now */ 163 /* Missed an interrupt so launch new poll now */
164 iio_trigger_poll(trig, 0); 164 iio_trigger_poll(trig);
165} 165}
166EXPORT_SYMBOL(iio_trigger_notify_done); 166EXPORT_SYMBOL(iio_trigger_notify_done);
167 167
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index c89740d4748f..bf05ca5b0a57 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -62,6 +62,18 @@ config GP2AP020A00F
62 To compile this driver as a module, choose M here: the 62 To compile this driver as a module, choose M here: the
63 module will be called gp2ap020a00f. 63 module will be called gp2ap020a00f.
64 64
65config ISL29125
66 tristate "Intersil ISL29125 digital color light sensor"
67 depends on I2C
68 select IIO_BUFFER
69 select IIO_TRIGGERED_BUFFER
70 help
71 Say Y here if you want to build a driver for the Intersil ISL29125
72 RGB light sensor for I2C.
73
74 To compile this driver as a module, choose M here: the module will be
75 called isl29125.
76
65config HID_SENSOR_ALS 77config HID_SENSOR_ALS
66 depends on HID_SENSOR_HUB 78 depends on HID_SENSOR_HUB
67 select IIO_BUFFER 79 select IIO_BUFFER
@@ -116,6 +128,18 @@ config LTR501
116 This driver can also be built as a module. If so, the module 128 This driver can also be built as a module. If so, the module
117 will be called ltr501. 129 will be called ltr501.
118 130
131config TCS3414
132 tristate "TAOS TCS3414 digital color sensor"
133 depends on I2C
134 select IIO_BUFFER
135 select IIO_TRIGGERED_BUFFER
136 help
137 If you say yes here you get support for the TAOS TCS3414
138 family of digital color sensors.
139
140 This driver can also be built as a module. If so, the module
141 will be called tcs3414.
142
119config TCS3472 143config TCS3472
120 tristate "TAOS TCS3472 color light-to-digital converter" 144 tristate "TAOS TCS3472 color light-to-digital converter"
121 depends on I2C 145 depends on I2C
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 3eb36e5151fa..8b8c09f9c1f8 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -10,9 +10,11 @@ obj-$(CONFIG_CM36651) += cm36651.o
10obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o 10obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o
11obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o 11obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o
12obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o 12obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o
13obj-$(CONFIG_ISL29125) += isl29125.o
13obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o 14obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o
14obj-$(CONFIG_LTR501) += ltr501.o 15obj-$(CONFIG_LTR501) += ltr501.o
15obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o 16obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o
17obj-$(CONFIG_TCS3414) += tcs3414.o
16obj-$(CONFIG_TCS3472) += tcs3472.o 18obj-$(CONFIG_TCS3472) += tcs3472.o
17obj-$(CONFIG_TSL4531) += tsl4531.o 19obj-$(CONFIG_TSL4531) += tsl4531.o
18obj-$(CONFIG_VCNL4000) += vcnl4000.o 20obj-$(CONFIG_VCNL4000) += vcnl4000.o
diff --git a/drivers/iio/light/cm32181.c b/drivers/iio/light/cm32181.c
index d976e6ce60db..ad36b294e4d5 100644
--- a/drivers/iio/light/cm32181.c
+++ b/drivers/iio/light/cm32181.c
@@ -331,7 +331,7 @@ static int cm32181_probe(struct i2c_client *client,
331 return ret; 331 return ret;
332 } 332 }
333 333
334 ret = iio_device_register(indio_dev); 334 ret = devm_iio_device_register(&client->dev, indio_dev);
335 if (ret) { 335 if (ret) {
336 dev_err(&client->dev, 336 dev_err(&client->dev,
337 "%s: regist device failed\n", 337 "%s: regist device failed\n",
@@ -342,14 +342,6 @@ static int cm32181_probe(struct i2c_client *client,
342 return 0; 342 return 0;
343} 343}
344 344
345static int cm32181_remove(struct i2c_client *client)
346{
347 struct iio_dev *indio_dev = i2c_get_clientdata(client);
348
349 iio_device_unregister(indio_dev);
350 return 0;
351}
352
353static const struct i2c_device_id cm32181_id[] = { 345static const struct i2c_device_id cm32181_id[] = {
354 { "cm32181", 0 }, 346 { "cm32181", 0 },
355 { } 347 { }
@@ -370,7 +362,6 @@ static struct i2c_driver cm32181_driver = {
370 }, 362 },
371 .id_table = cm32181_id, 363 .id_table = cm32181_id,
372 .probe = cm32181_probe, 364 .probe = cm32181_probe,
373 .remove = cm32181_remove,
374}; 365};
375 366
376module_i2c_driver(cm32181_driver); 367module_i2c_driver(cm32181_driver);
diff --git a/drivers/iio/light/gp2ap020a00f.c b/drivers/iio/light/gp2ap020a00f.c
index 04bdb85d2d9f..221ed16de1f7 100644
--- a/drivers/iio/light/gp2ap020a00f.c
+++ b/drivers/iio/light/gp2ap020a00f.c
@@ -827,7 +827,7 @@ static void gp2ap020a00f_iio_trigger_work(struct irq_work *work)
827 struct gp2ap020a00f_data *data = 827 struct gp2ap020a00f_data *data =
828 container_of(work, struct gp2ap020a00f_data, work); 828 container_of(work, struct gp2ap020a00f_data, work);
829 829
830 iio_trigger_poll(data->trig, 0); 830 iio_trigger_poll(data->trig);
831} 831}
832 832
833static irqreturn_t gp2ap020a00f_prox_sensing_handler(int irq, void *data) 833static irqreturn_t gp2ap020a00f_prox_sensing_handler(int irq, void *data)
diff --git a/drivers/iio/light/isl29125.c b/drivers/iio/light/isl29125.c
new file mode 100644
index 000000000000..c82f4a6f8464
--- /dev/null
+++ b/drivers/iio/light/isl29125.c
@@ -0,0 +1,347 @@
1/*
2 * isl29125.c - Support for Intersil ISL29125 RGB light sensor
3 *
4 * Copyright (c) 2014 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 * RGB light sensor with 16-bit channels for red, green, blue);
11 * 7-bit I2C slave address 0x44
12 *
13 * TODO: interrupt support, IR compensation, thresholds, 12bit
14 */
15
16#include <linux/module.h>
17#include <linux/i2c.h>
18#include <linux/delay.h>
19#include <linux/pm.h>
20
21#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23#include <linux/iio/trigger_consumer.h>
24#include <linux/iio/buffer.h>
25#include <linux/iio/triggered_buffer.h>
26
27#define ISL29125_DRV_NAME "isl29125"
28
29#define ISL29125_DEVICE_ID 0x00
30#define ISL29125_CONF1 0x01
31#define ISL29125_CONF2 0x02
32#define ISL29125_CONF3 0x03
33#define ISL29125_STATUS 0x08
34#define ISL29125_GREEN_DATA 0x09
35#define ISL29125_RED_DATA 0x0b
36#define ISL29125_BLUE_DATA 0x0d
37
38#define ISL29125_ID 0x7d
39
40#define ISL29125_MODE_MASK GENMASK(2, 0)
41#define ISL29125_MODE_PD 0x0
42#define ISL29125_MODE_G 0x1
43#define ISL29125_MODE_R 0x2
44#define ISL29125_MODE_B 0x3
45#define ISL29125_MODE_RGB 0x5
46
47#define ISL29125_MODE_RANGE BIT(3)
48
49#define ISL29125_STATUS_CONV BIT(1)
50
51struct isl29125_data {
52 struct i2c_client *client;
53 struct mutex lock;
54 u8 conf1;
55 u16 buffer[8]; /* 3x 16-bit, padding, 8 bytes timestamp */
56};
57
58#define ISL29125_CHANNEL(_color, _si) { \
59 .type = IIO_INTENSITY, \
60 .modified = 1, \
61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
62 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
63 .channel2 = IIO_MOD_LIGHT_##_color, \
64 .scan_index = _si, \
65 .scan_type = { \
66 .sign = 'u', \
67 .realbits = 16, \
68 .storagebits = 16, \
69 .endianness = IIO_CPU, \
70 }, \
71}
72
73static const struct iio_chan_spec isl29125_channels[] = {
74 ISL29125_CHANNEL(GREEN, 0),
75 ISL29125_CHANNEL(RED, 1),
76 ISL29125_CHANNEL(BLUE, 2),
77 IIO_CHAN_SOFT_TIMESTAMP(3),
78};
79
80static const struct {
81 u8 mode, data;
82} isl29125_regs[] = {
83 {ISL29125_MODE_G, ISL29125_GREEN_DATA},
84 {ISL29125_MODE_R, ISL29125_RED_DATA},
85 {ISL29125_MODE_B, ISL29125_BLUE_DATA},
86};
87
88static int isl29125_read_data(struct isl29125_data *data, int si)
89{
90 int tries = 5;
91 int ret;
92
93 ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
94 data->conf1 | isl29125_regs[si].mode);
95 if (ret < 0)
96 return ret;
97
98 msleep(101);
99
100 while (tries--) {
101 ret = i2c_smbus_read_byte_data(data->client, ISL29125_STATUS);
102 if (ret < 0)
103 goto fail;
104 if (ret & ISL29125_STATUS_CONV)
105 break;
106 msleep(20);
107 }
108
109 if (tries < 0) {
110 dev_err(&data->client->dev, "data not ready\n");
111 ret = -EIO;
112 goto fail;
113 }
114
115 ret = i2c_smbus_read_word_data(data->client, isl29125_regs[si].data);
116
117fail:
118 i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, data->conf1);
119 return ret;
120}
121
122static int isl29125_read_raw(struct iio_dev *indio_dev,
123 struct iio_chan_spec const *chan,
124 int *val, int *val2, long mask)
125{
126 struct isl29125_data *data = iio_priv(indio_dev);
127 int ret;
128
129 switch (mask) {
130 case IIO_CHAN_INFO_RAW:
131 if (iio_buffer_enabled(indio_dev))
132 return -EBUSY;
133 mutex_lock(&data->lock);
134 ret = isl29125_read_data(data, chan->scan_index);
135 mutex_unlock(&data->lock);
136 if (ret < 0)
137 return ret;
138 *val = ret;
139 return IIO_VAL_INT;
140 case IIO_CHAN_INFO_SCALE:
141 *val = 0;
142 if (data->conf1 & ISL29125_MODE_RANGE)
143 *val2 = 152590; /* 10k lux full range */
144 else
145 *val2 = 5722; /* 375 lux full range */
146 return IIO_VAL_INT_PLUS_MICRO;
147 }
148 return -EINVAL;
149}
150
151static int isl29125_write_raw(struct iio_dev *indio_dev,
152 struct iio_chan_spec const *chan,
153 int val, int val2, long mask)
154{
155 struct isl29125_data *data = iio_priv(indio_dev);
156
157 switch (mask) {
158 case IIO_CHAN_INFO_SCALE:
159 if (val != 0)
160 return -EINVAL;
161 if (val2 == 152590)
162 data->conf1 |= ISL29125_MODE_RANGE;
163 else if (val2 == 5722)
164 data->conf1 &= ~ISL29125_MODE_RANGE;
165 else
166 return -EINVAL;
167 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
168 data->conf1);
169 default:
170 return -EINVAL;
171 }
172}
173
174static irqreturn_t isl29125_trigger_handler(int irq, void *p)
175{
176 struct iio_poll_func *pf = p;
177 struct iio_dev *indio_dev = pf->indio_dev;
178 struct isl29125_data *data = iio_priv(indio_dev);
179 int i, j = 0;
180
181 for_each_set_bit(i, indio_dev->active_scan_mask,
182 indio_dev->masklength) {
183 int ret = i2c_smbus_read_word_data(data->client,
184 isl29125_regs[i].data);
185 if (ret < 0)
186 goto done;
187
188 data->buffer[j++] = ret;
189 }
190
191 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
192 iio_get_time_ns());
193
194done:
195 iio_trigger_notify_done(indio_dev->trig);
196
197 return IRQ_HANDLED;
198}
199
200static const struct iio_info isl29125_info = {
201 .read_raw = isl29125_read_raw,
202 .write_raw = isl29125_write_raw,
203 .driver_module = THIS_MODULE,
204};
205
206static int isl29125_buffer_preenable(struct iio_dev *indio_dev)
207{
208 struct isl29125_data *data = iio_priv(indio_dev);
209
210 data->conf1 |= ISL29125_MODE_RGB;
211 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
212 data->conf1);
213}
214
215static int isl29125_buffer_predisable(struct iio_dev *indio_dev)
216{
217 struct isl29125_data *data = iio_priv(indio_dev);
218 int ret;
219
220 ret = iio_triggered_buffer_predisable(indio_dev);
221 if (ret < 0)
222 return ret;
223
224 data->conf1 &= ~ISL29125_MODE_MASK;
225 data->conf1 |= ISL29125_MODE_PD;
226 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
227 data->conf1);
228}
229
230static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops = {
231 .preenable = isl29125_buffer_preenable,
232 .postenable = &iio_triggered_buffer_postenable,
233 .predisable = isl29125_buffer_predisable,
234};
235
236static int isl29125_probe(struct i2c_client *client,
237 const struct i2c_device_id *id)
238{
239 struct isl29125_data *data;
240 struct iio_dev *indio_dev;
241 int ret;
242
243 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
244 if (indio_dev == NULL)
245 return -ENOMEM;
246
247 data = iio_priv(indio_dev);
248 i2c_set_clientdata(client, indio_dev);
249 data->client = client;
250 mutex_init(&data->lock);
251
252 indio_dev->dev.parent = &client->dev;
253 indio_dev->info = &isl29125_info;
254 indio_dev->name = ISL29125_DRV_NAME;
255 indio_dev->channels = isl29125_channels;
256 indio_dev->num_channels = ARRAY_SIZE(isl29125_channels);
257 indio_dev->modes = INDIO_DIRECT_MODE;
258
259 ret = i2c_smbus_read_byte_data(data->client, ISL29125_DEVICE_ID);
260 if (ret < 0)
261 return ret;
262 if (ret != ISL29125_ID)
263 return -ENODEV;
264
265 data->conf1 = ISL29125_MODE_PD | ISL29125_MODE_RANGE;
266 ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
267 data->conf1);
268 if (ret < 0)
269 return ret;
270
271 ret = i2c_smbus_write_byte_data(data->client, ISL29125_STATUS, 0);
272 if (ret < 0)
273 return ret;
274
275 ret = iio_triggered_buffer_setup(indio_dev, NULL,
276 isl29125_trigger_handler, &isl29125_buffer_setup_ops);
277 if (ret < 0)
278 return ret;
279
280 ret = iio_device_register(indio_dev);
281 if (ret < 0)
282 goto buffer_cleanup;
283
284 return 0;
285
286buffer_cleanup:
287 iio_triggered_buffer_cleanup(indio_dev);
288 return ret;
289}
290
291static int isl29125_powerdown(struct isl29125_data *data)
292{
293 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
294 (data->conf1 & ~ISL29125_MODE_MASK) | ISL29125_MODE_PD);
295}
296
297static int isl29125_remove(struct i2c_client *client)
298{
299 struct iio_dev *indio_dev = i2c_get_clientdata(client);
300
301 iio_device_unregister(indio_dev);
302 iio_triggered_buffer_cleanup(indio_dev);
303 isl29125_powerdown(iio_priv(indio_dev));
304
305 return 0;
306}
307
308#ifdef CONFIG_PM_SLEEP
309static int isl29125_suspend(struct device *dev)
310{
311 struct isl29125_data *data = iio_priv(i2c_get_clientdata(
312 to_i2c_client(dev)));
313 return isl29125_powerdown(data);
314}
315
316static int isl29125_resume(struct device *dev)
317{
318 struct isl29125_data *data = iio_priv(i2c_get_clientdata(
319 to_i2c_client(dev)));
320 return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
321 data->conf1);
322}
323#endif
324
325static SIMPLE_DEV_PM_OPS(isl29125_pm_ops, isl29125_suspend, isl29125_resume);
326
327static const struct i2c_device_id isl29125_id[] = {
328 { "isl29125", 0 },
329 { }
330};
331MODULE_DEVICE_TABLE(i2c, isl29125_id);
332
333static struct i2c_driver isl29125_driver = {
334 .driver = {
335 .name = ISL29125_DRV_NAME,
336 .pm = &isl29125_pm_ops,
337 .owner = THIS_MODULE,
338 },
339 .probe = isl29125_probe,
340 .remove = isl29125_remove,
341 .id_table = isl29125_id,
342};
343module_i2c_driver(isl29125_driver);
344
345MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
346MODULE_DESCRIPTION("ISL29125 RGB light sensor driver");
347MODULE_LICENSE("GPL");
diff --git a/drivers/iio/light/tcs3414.c b/drivers/iio/light/tcs3414.c
new file mode 100644
index 000000000000..a9e449b0be0c
--- /dev/null
+++ b/drivers/iio/light/tcs3414.c
@@ -0,0 +1,405 @@
1/*
2 * tcs3414.c - Support for TAOS TCS3414 digital color sensor
3 *
4 * Copyright (c) 2014 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 * Digital color sensor with 16-bit channels for red, green, blue, clear);
11 * 7-bit I2C slave address 0x39 (TCS3414) or 0x29, 0x49, 0x59 (TCS3413,
12 * TCS3415, TCS3416, resp.)
13 *
14 * TODO: sync, interrupt support, thresholds, prescaler
15 */
16
17#include <linux/module.h>
18#include <linux/i2c.h>
19#include <linux/delay.h>
20#include <linux/pm.h>
21
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/trigger_consumer.h>
25#include <linux/iio/buffer.h>
26#include <linux/iio/triggered_buffer.h>
27
28#define TCS3414_DRV_NAME "tcs3414"
29
30#define TCS3414_COMMAND BIT(7)
31#define TCS3414_COMMAND_WORD (TCS3414_COMMAND | BIT(5))
32
33#define TCS3414_CONTROL (TCS3414_COMMAND | 0x00)
34#define TCS3414_TIMING (TCS3414_COMMAND | 0x01)
35#define TCS3414_ID (TCS3414_COMMAND | 0x04)
36#define TCS3414_GAIN (TCS3414_COMMAND | 0x07)
37#define TCS3414_DATA_GREEN (TCS3414_COMMAND_WORD | 0x10)
38#define TCS3414_DATA_RED (TCS3414_COMMAND_WORD | 0x12)
39#define TCS3414_DATA_BLUE (TCS3414_COMMAND_WORD | 0x14)
40#define TCS3414_DATA_CLEAR (TCS3414_COMMAND_WORD | 0x16)
41
42#define TCS3414_CONTROL_ADC_VALID BIT(4)
43#define TCS3414_CONTROL_ADC_EN BIT(1)
44#define TCS3414_CONTROL_POWER BIT(0)
45
46#define TCS3414_INTEG_MASK GENMASK(1, 0)
47#define TCS3414_INTEG_12MS 0x0
48#define TCS3414_INTEG_100MS 0x1
49#define TCS3414_INTEG_400MS 0x2
50
51#define TCS3414_GAIN_MASK GENMASK(5, 4)
52#define TCS3414_GAIN_SHIFT 4
53
54struct tcs3414_data {
55 struct i2c_client *client;
56 struct mutex lock;
57 u8 control;
58 u8 gain;
59 u8 timing;
60 u16 buffer[8]; /* 4x 16-bit + 8 bytes timestamp */
61};
62
63#define TCS3414_CHANNEL(_color, _si, _addr) { \
64 .type = IIO_INTENSITY, \
65 .modified = 1, \
66 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
67 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
68 BIT(IIO_CHAN_INFO_INT_TIME), \
69 .channel2 = IIO_MOD_LIGHT_##_color, \
70 .address = _addr, \
71 .scan_index = _si, \
72 .scan_type = { \
73 .sign = 'u', \
74 .realbits = 16, \
75 .storagebits = 16, \
76 .endianness = IIO_CPU, \
77 }, \
78}
79
80/* scale factors: 1/gain */
81static const int tcs3414_scales[][2] = {
82 {1, 0}, {0, 250000}, {0, 62500}, {0, 15625}
83};
84
85/* integration time in ms */
86static const int tcs3414_times[] = { 12, 100, 400 };
87
88static const struct iio_chan_spec tcs3414_channels[] = {
89 TCS3414_CHANNEL(GREEN, 0, TCS3414_DATA_GREEN),
90 TCS3414_CHANNEL(RED, 1, TCS3414_DATA_RED),
91 TCS3414_CHANNEL(BLUE, 2, TCS3414_DATA_BLUE),
92 TCS3414_CHANNEL(CLEAR, 3, TCS3414_DATA_CLEAR),
93 IIO_CHAN_SOFT_TIMESTAMP(4),
94};
95
96static int tcs3414_req_data(struct tcs3414_data *data)
97{
98 int tries = 25;
99 int ret;
100
101 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
102 data->control | TCS3414_CONTROL_ADC_EN);
103 if (ret < 0)
104 return ret;
105
106 while (tries--) {
107 ret = i2c_smbus_read_byte_data(data->client, TCS3414_CONTROL);
108 if (ret < 0)
109 return ret;
110 if (ret & TCS3414_CONTROL_ADC_VALID)
111 break;
112 msleep(20);
113 }
114
115 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
116 data->control);
117 if (ret < 0)
118 return ret;
119
120 if (tries < 0) {
121 dev_err(&data->client->dev, "data not ready\n");
122 return -EIO;
123 }
124
125 return 0;
126}
127
128static int tcs3414_read_raw(struct iio_dev *indio_dev,
129 struct iio_chan_spec const *chan,
130 int *val, int *val2, long mask)
131{
132 struct tcs3414_data *data = iio_priv(indio_dev);
133 int i, ret;
134
135 switch (mask) {
136 case IIO_CHAN_INFO_RAW:
137 if (iio_buffer_enabled(indio_dev))
138 return -EBUSY;
139 mutex_lock(&data->lock);
140 ret = tcs3414_req_data(data);
141 if (ret < 0) {
142 mutex_unlock(&data->lock);
143 return ret;
144 }
145 ret = i2c_smbus_read_word_data(data->client, chan->address);
146 mutex_unlock(&data->lock);
147 if (ret < 0)
148 return ret;
149 *val = ret;
150 return IIO_VAL_INT;
151 case IIO_CHAN_INFO_SCALE:
152 i = (data->gain & TCS3414_GAIN_MASK) >> TCS3414_GAIN_SHIFT;
153 *val = tcs3414_scales[i][0];
154 *val2 = tcs3414_scales[i][1];
155 return IIO_VAL_INT_PLUS_MICRO;
156 case IIO_CHAN_INFO_INT_TIME:
157 *val = 0;
158 *val2 = tcs3414_times[data->timing & TCS3414_INTEG_MASK] * 1000;
159 return IIO_VAL_INT_PLUS_MICRO;
160 }
161 return -EINVAL;
162}
163
164static int tcs3414_write_raw(struct iio_dev *indio_dev,
165 struct iio_chan_spec const *chan,
166 int val, int val2, long mask)
167{
168 struct tcs3414_data *data = iio_priv(indio_dev);
169 int i;
170
171 switch (mask) {
172 case IIO_CHAN_INFO_SCALE:
173 for (i = 0; i < ARRAY_SIZE(tcs3414_scales); i++) {
174 if (val == tcs3414_scales[i][0] &&
175 val2 == tcs3414_scales[i][1]) {
176 data->gain &= ~TCS3414_GAIN_MASK;
177 data->gain |= i << TCS3414_GAIN_SHIFT;
178 return i2c_smbus_write_byte_data(
179 data->client, TCS3414_GAIN,
180 data->gain);
181 }
182 }
183 return -EINVAL;
184 case IIO_CHAN_INFO_INT_TIME:
185 if (val != 0)
186 return -EINVAL;
187 for (i = 0; i < ARRAY_SIZE(tcs3414_times); i++) {
188 if (val == tcs3414_times[i] * 1000) {
189 data->timing &= ~TCS3414_INTEG_MASK;
190 data->timing |= i;
191 return i2c_smbus_write_byte_data(
192 data->client, TCS3414_TIMING,
193 data->timing);
194 }
195 }
196 return -EINVAL;
197 default:
198 return -EINVAL;
199 }
200}
201
202static irqreturn_t tcs3414_trigger_handler(int irq, void *p)
203{
204 struct iio_poll_func *pf = p;
205 struct iio_dev *indio_dev = pf->indio_dev;
206 struct tcs3414_data *data = iio_priv(indio_dev);
207 int i, j = 0;
208
209 for_each_set_bit(i, indio_dev->active_scan_mask,
210 indio_dev->masklength) {
211 int ret = i2c_smbus_read_word_data(data->client,
212 TCS3414_DATA_GREEN + 2*i);
213 if (ret < 0)
214 goto done;
215
216 data->buffer[j++] = ret;
217 }
218
219 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
220 iio_get_time_ns());
221
222done:
223 iio_trigger_notify_done(indio_dev->trig);
224
225 return IRQ_HANDLED;
226}
227
228static IIO_CONST_ATTR(scale_available, "1 0.25 0.0625 0.015625");
229static IIO_CONST_ATTR_INT_TIME_AVAIL("0.012 0.1 0.4");
230
231static struct attribute *tcs3414_attributes[] = {
232 &iio_const_attr_scale_available.dev_attr.attr,
233 &iio_const_attr_integration_time_available.dev_attr.attr,
234 NULL
235};
236
237static const struct attribute_group tcs3414_attribute_group = {
238 .attrs = tcs3414_attributes,
239};
240
241static const struct iio_info tcs3414_info = {
242 .read_raw = tcs3414_read_raw,
243 .write_raw = tcs3414_write_raw,
244 .attrs = &tcs3414_attribute_group,
245 .driver_module = THIS_MODULE,
246};
247
248static int tcs3414_buffer_preenable(struct iio_dev *indio_dev)
249{
250 struct tcs3414_data *data = iio_priv(indio_dev);
251
252 data->control |= TCS3414_CONTROL_ADC_EN;
253 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
254 data->control);
255}
256
257static int tcs3414_buffer_predisable(struct iio_dev *indio_dev)
258{
259 struct tcs3414_data *data = iio_priv(indio_dev);
260 int ret;
261
262 ret = iio_triggered_buffer_predisable(indio_dev);
263 if (ret < 0)
264 return ret;
265
266 data->control &= ~TCS3414_CONTROL_ADC_EN;
267 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
268 data->control);
269}
270
271static const struct iio_buffer_setup_ops tcs3414_buffer_setup_ops = {
272 .preenable = tcs3414_buffer_preenable,
273 .postenable = &iio_triggered_buffer_postenable,
274 .predisable = tcs3414_buffer_predisable,
275};
276
277static int tcs3414_probe(struct i2c_client *client,
278 const struct i2c_device_id *id)
279{
280 struct tcs3414_data *data;
281 struct iio_dev *indio_dev;
282 int ret;
283
284 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
285 if (indio_dev == NULL)
286 return -ENOMEM;
287
288 data = iio_priv(indio_dev);
289 i2c_set_clientdata(client, indio_dev);
290 data->client = client;
291 mutex_init(&data->lock);
292
293 indio_dev->dev.parent = &client->dev;
294 indio_dev->info = &tcs3414_info;
295 indio_dev->name = TCS3414_DRV_NAME;
296 indio_dev->channels = tcs3414_channels;
297 indio_dev->num_channels = ARRAY_SIZE(tcs3414_channels);
298 indio_dev->modes = INDIO_DIRECT_MODE;
299
300 ret = i2c_smbus_read_byte_data(data->client, TCS3414_ID);
301 if (ret < 0)
302 return ret;
303
304 switch (ret & 0xf0) {
305 case 0x00:
306 dev_info(&client->dev, "TCS3404 found\n");
307 break;
308 case 0x10:
309 dev_info(&client->dev, "TCS3413/14/15/16 found\n");
310 break;
311 default:
312 return -ENODEV;
313 }
314
315 data->control = TCS3414_CONTROL_POWER;
316 ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
317 data->control);
318 if (ret < 0)
319 return ret;
320
321 data->timing = TCS3414_INTEG_12MS; /* free running */
322 ret = i2c_smbus_write_byte_data(data->client, TCS3414_TIMING,
323 data->timing);
324 if (ret < 0)
325 return ret;
326
327 ret = i2c_smbus_read_byte_data(data->client, TCS3414_GAIN);
328 if (ret < 0)
329 return ret;
330 data->gain = ret;
331
332 ret = iio_triggered_buffer_setup(indio_dev, NULL,
333 tcs3414_trigger_handler, &tcs3414_buffer_setup_ops);
334 if (ret < 0)
335 return ret;
336
337 ret = iio_device_register(indio_dev);
338 if (ret < 0)
339 goto buffer_cleanup;
340
341 return 0;
342
343buffer_cleanup:
344 iio_triggered_buffer_cleanup(indio_dev);
345 return ret;
346}
347
348static int tcs3414_powerdown(struct tcs3414_data *data)
349{
350 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
351 data->control & ~(TCS3414_CONTROL_POWER |
352 TCS3414_CONTROL_ADC_EN));
353}
354
355static int tcs3414_remove(struct i2c_client *client)
356{
357 struct iio_dev *indio_dev = i2c_get_clientdata(client);
358
359 iio_device_unregister(indio_dev);
360 iio_triggered_buffer_cleanup(indio_dev);
361 tcs3414_powerdown(iio_priv(indio_dev));
362
363 return 0;
364}
365
366#ifdef CONFIG_PM_SLEEP
367static int tcs3414_suspend(struct device *dev)
368{
369 struct tcs3414_data *data = iio_priv(i2c_get_clientdata(
370 to_i2c_client(dev)));
371 return tcs3414_powerdown(data);
372}
373
374static int tcs3414_resume(struct device *dev)
375{
376 struct tcs3414_data *data = iio_priv(i2c_get_clientdata(
377 to_i2c_client(dev)));
378 return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
379 data->control);
380}
381#endif
382
383static SIMPLE_DEV_PM_OPS(tcs3414_pm_ops, tcs3414_suspend, tcs3414_resume);
384
385static const struct i2c_device_id tcs3414_id[] = {
386 { "tcs3414", 0 },
387 { }
388};
389MODULE_DEVICE_TABLE(i2c, tcs3414_id);
390
391static struct i2c_driver tcs3414_driver = {
392 .driver = {
393 .name = TCS3414_DRV_NAME,
394 .pm = &tcs3414_pm_ops,
395 .owner = THIS_MODULE,
396 },
397 .probe = tcs3414_probe,
398 .remove = tcs3414_remove,
399 .id_table = tcs3414_id,
400};
401module_i2c_driver(tcs3414_driver);
402
403MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
404MODULE_DESCRIPTION("TCS3414 digital color sensors driver");
405MODULE_LICENSE("GPL");
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig
index 05a364c543f8..b2dba9e506ab 100644
--- a/drivers/iio/magnetometer/Kconfig
+++ b/drivers/iio/magnetometer/Kconfig
@@ -17,6 +17,16 @@ config AK8975
17 To compile this driver as a module, choose M here: the module 17 To compile this driver as a module, choose M here: the module
18 will be called ak8975. 18 will be called ak8975.
19 19
20config AK09911
21 tristate "Asahi Kasei AK09911 3-axis Compass"
22 depends on I2C
23 help
24 Say yes here to build support for Asahi Kasei AK09911 3-Axis
25 Magnetometer.
26
27 To compile this driver as a module, choose M here: the module
28 will be called ak09911.
29
20config MAG3110 30config MAG3110
21 tristate "Freescale MAG3110 3-Axis Magnetometer" 31 tristate "Freescale MAG3110 3-Axis Magnetometer"
22 depends on I2C 32 depends on I2C
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile
index 0f5d3c985799..b91315e0b826 100644
--- a/drivers/iio/magnetometer/Makefile
+++ b/drivers/iio/magnetometer/Makefile
@@ -3,6 +3,7 @@
3# 3#
4 4
5# When adding new entries keep the list in alphabetical order 5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_AK09911) += ak09911.o
6obj-$(CONFIG_AK8975) += ak8975.o 7obj-$(CONFIG_AK8975) += ak8975.o
7obj-$(CONFIG_MAG3110) += mag3110.o 8obj-$(CONFIG_MAG3110) += mag3110.o
8obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o 9obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o
diff --git a/drivers/iio/magnetometer/ak09911.c b/drivers/iio/magnetometer/ak09911.c
new file mode 100644
index 000000000000..b2bc942ff6b8
--- /dev/null
+++ b/drivers/iio/magnetometer/ak09911.c
@@ -0,0 +1,326 @@
1/*
2 * AK09911 3-axis compass driver
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/types.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/i2c.h>
22#include <linux/acpi.h>
23#include <linux/iio/iio.h>
24
25#define AK09911_REG_WIA1 0x00
26#define AK09911_REG_WIA2 0x01
27#define AK09911_WIA1_VALUE 0x48
28#define AK09911_WIA2_VALUE 0x05
29
30#define AK09911_REG_ST1 0x10
31#define AK09911_REG_HXL 0x11
32#define AK09911_REG_HXH 0x12
33#define AK09911_REG_HYL 0x13
34#define AK09911_REG_HYH 0x14
35#define AK09911_REG_HZL 0x15
36#define AK09911_REG_HZH 0x16
37
38#define AK09911_REG_ASAX 0x60
39#define AK09911_REG_ASAY 0x61
40#define AK09911_REG_ASAZ 0x62
41
42#define AK09911_REG_CNTL1 0x30
43#define AK09911_REG_CNTL2 0x31
44#define AK09911_REG_CNTL3 0x32
45
46#define AK09911_MODE_SNG_MEASURE 0x01
47#define AK09911_MODE_SELF_TEST 0x10
48#define AK09911_MODE_FUSE_ACCESS 0x1F
49#define AK09911_MODE_POWERDOWN 0x00
50#define AK09911_RESET_DATA 0x01
51
52#define AK09911_REG_CNTL1 0x30
53#define AK09911_REG_CNTL2 0x31
54#define AK09911_REG_CNTL3 0x32
55
56#define AK09911_RAW_TO_GAUSS(asa) ((((asa) + 128) * 6000) / 256)
57
58#define AK09911_MAX_CONVERSION_TIMEOUT_MS 500
59#define AK09911_CONVERSION_DONE_POLL_TIME_MS 10
60
61struct ak09911_data {
62 struct i2c_client *client;
63 struct mutex lock;
64 u8 asa[3];
65 long raw_to_gauss[3];
66};
67
68static const int ak09911_index_to_reg[] = {
69 AK09911_REG_HXL, AK09911_REG_HYL, AK09911_REG_HZL,
70};
71
72static int ak09911_set_mode(struct i2c_client *client, u8 mode)
73{
74 int ret;
75
76 switch (mode) {
77 case AK09911_MODE_SNG_MEASURE:
78 case AK09911_MODE_SELF_TEST:
79 case AK09911_MODE_FUSE_ACCESS:
80 case AK09911_MODE_POWERDOWN:
81 ret = i2c_smbus_write_byte_data(client,
82 AK09911_REG_CNTL2, mode);
83 if (ret < 0) {
84 dev_err(&client->dev, "set_mode error\n");
85 return ret;
86 }
87 /* After mode change wait atleast 100us */
88 usleep_range(100, 500);
89 break;
90 default:
91 dev_err(&client->dev,
92 "%s: Unknown mode(%d).", __func__, mode);
93 return -EINVAL;
94 }
95
96 return ret;
97}
98
99/* Get Sensitivity Adjustment value */
100static int ak09911_get_asa(struct i2c_client *client)
101{
102 struct iio_dev *indio_dev = i2c_get_clientdata(client);
103 struct ak09911_data *data = iio_priv(indio_dev);
104 int ret;
105
106 ret = ak09911_set_mode(client, AK09911_MODE_FUSE_ACCESS);
107 if (ret < 0)
108 return ret;
109
110 /* Get asa data and store in the device data. */
111 ret = i2c_smbus_read_i2c_block_data(client, AK09911_REG_ASAX,
112 3, data->asa);
113 if (ret < 0) {
114 dev_err(&client->dev, "Not able to read asa data\n");
115 return ret;
116 }
117
118 ret = ak09911_set_mode(client, AK09911_MODE_POWERDOWN);
119 if (ret < 0)
120 return ret;
121
122 data->raw_to_gauss[0] = AK09911_RAW_TO_GAUSS(data->asa[0]);
123 data->raw_to_gauss[1] = AK09911_RAW_TO_GAUSS(data->asa[1]);
124 data->raw_to_gauss[2] = AK09911_RAW_TO_GAUSS(data->asa[2]);
125
126 return 0;
127}
128
129static int ak09911_verify_chip_id(struct i2c_client *client)
130{
131 u8 wia_val[2];
132 int ret;
133
134 ret = i2c_smbus_read_i2c_block_data(client, AK09911_REG_WIA1,
135 2, wia_val);
136 if (ret < 0) {
137 dev_err(&client->dev, "Error reading WIA\n");
138 return ret;
139 }
140
141 dev_dbg(&client->dev, "WIA %02x %02x\n", wia_val[0], wia_val[1]);
142
143 if (wia_val[0] != AK09911_WIA1_VALUE ||
144 wia_val[1] != AK09911_WIA2_VALUE) {
145 dev_err(&client->dev, "Device ak09911 not found\n");
146 return -ENODEV;
147 }
148
149 return 0;
150}
151
152static int wait_conversion_complete_polled(struct ak09911_data *data)
153{
154 struct i2c_client *client = data->client;
155 u8 read_status;
156 u32 timeout_ms = AK09911_MAX_CONVERSION_TIMEOUT_MS;
157 int ret;
158
159 /* Wait for the conversion to complete. */
160 while (timeout_ms) {
161 msleep_interruptible(AK09911_CONVERSION_DONE_POLL_TIME_MS);
162 ret = i2c_smbus_read_byte_data(client, AK09911_REG_ST1);
163 if (ret < 0) {
164 dev_err(&client->dev, "Error in reading ST1\n");
165 return ret;
166 }
167 read_status = ret & 0x01;
168 if (read_status)
169 break;
170 timeout_ms -= AK09911_CONVERSION_DONE_POLL_TIME_MS;
171 }
172 if (!timeout_ms) {
173 dev_err(&client->dev, "Conversion timeout happened\n");
174 return -EIO;
175 }
176
177 return read_status;
178}
179
180static int ak09911_read_axis(struct iio_dev *indio_dev, int index, int *val)
181{
182 struct ak09911_data *data = iio_priv(indio_dev);
183 struct i2c_client *client = data->client;
184 int ret;
185
186 mutex_lock(&data->lock);
187
188 ret = ak09911_set_mode(client, AK09911_MODE_SNG_MEASURE);
189 if (ret < 0)
190 goto fn_exit;
191
192 ret = wait_conversion_complete_polled(data);
193 if (ret < 0)
194 goto fn_exit;
195
196 /* Read data */
197 ret = i2c_smbus_read_word_data(client, ak09911_index_to_reg[index]);
198 if (ret < 0) {
199 dev_err(&client->dev, "Read axis data fails\n");
200 goto fn_exit;
201 }
202
203 mutex_unlock(&data->lock);
204
205 /* Clamp to valid range. */
206 *val = sign_extend32(clamp_t(s16, ret, -8192, 8191), 13);
207
208 return IIO_VAL_INT;
209
210fn_exit:
211 mutex_unlock(&data->lock);
212
213 return ret;
214}
215
216static int ak09911_read_raw(struct iio_dev *indio_dev,
217 struct iio_chan_spec const *chan,
218 int *val, int *val2,
219 long mask)
220{
221 struct ak09911_data *data = iio_priv(indio_dev);
222
223 switch (mask) {
224 case IIO_CHAN_INFO_RAW:
225 return ak09911_read_axis(indio_dev, chan->address, val);
226 case IIO_CHAN_INFO_SCALE:
227 *val = 0;
228 *val2 = data->raw_to_gauss[chan->address];
229 return IIO_VAL_INT_PLUS_MICRO;
230 }
231
232 return -EINVAL;
233}
234
235#define AK09911_CHANNEL(axis, index) \
236 { \
237 .type = IIO_MAGN, \
238 .modified = 1, \
239 .channel2 = IIO_MOD_##axis, \
240 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
241 BIT(IIO_CHAN_INFO_SCALE), \
242 .address = index, \
243 }
244
245static const struct iio_chan_spec ak09911_channels[] = {
246 AK09911_CHANNEL(X, 0), AK09911_CHANNEL(Y, 1), AK09911_CHANNEL(Z, 2),
247};
248
249static const struct iio_info ak09911_info = {
250 .read_raw = &ak09911_read_raw,
251 .driver_module = THIS_MODULE,
252};
253
254static const struct acpi_device_id ak_acpi_match[] = {
255 {"AK009911", 0},
256 { },
257};
258MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
259
260static int ak09911_probe(struct i2c_client *client,
261 const struct i2c_device_id *id)
262{
263 struct iio_dev *indio_dev;
264 struct ak09911_data *data;
265 const char *name;
266 int ret;
267
268 ret = ak09911_verify_chip_id(client);
269 if (ret) {
270 dev_err(&client->dev, "AK00911 not detected\n");
271 return -ENODEV;
272 }
273
274 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
275 if (indio_dev == NULL)
276 return -ENOMEM;
277
278 data = iio_priv(indio_dev);
279 i2c_set_clientdata(client, indio_dev);
280
281 data->client = client;
282 mutex_init(&data->lock);
283
284 ret = ak09911_get_asa(client);
285 if (ret)
286 return ret;
287
288 if (id)
289 name = id->name;
290 else if (ACPI_HANDLE(&client->dev))
291 name = dev_name(&client->dev);
292 else
293 return -ENODEV;
294
295 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
296
297 indio_dev->dev.parent = &client->dev;
298 indio_dev->channels = ak09911_channels;
299 indio_dev->num_channels = ARRAY_SIZE(ak09911_channels);
300 indio_dev->info = &ak09911_info;
301 indio_dev->modes = INDIO_DIRECT_MODE;
302 indio_dev->name = name;
303
304 return devm_iio_device_register(&client->dev, indio_dev);
305}
306
307static const struct i2c_device_id ak09911_id[] = {
308 {"ak09911", 0},
309 {}
310};
311
312MODULE_DEVICE_TABLE(i2c, ak09911_id);
313
314static struct i2c_driver ak09911_driver = {
315 .driver = {
316 .name = "ak09911",
317 .acpi_match_table = ACPI_PTR(ak_acpi_match),
318 },
319 .probe = ak09911_probe,
320 .id_table = ak09911_id,
321};
322module_i2c_driver(ak09911_driver);
323
324MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
325MODULE_LICENSE("GPL v2");
326MODULE_DESCRIPTION("AK09911 Compass driver");
diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c
index ea08313af0d2..a2357921d761 100644
--- a/drivers/iio/magnetometer/ak8975.c
+++ b/drivers/iio/magnetometer/ak8975.c
@@ -165,7 +165,7 @@ static int ak8975_setup_irq(struct ak8975_data *data)
165 else 165 else
166 irq = gpio_to_irq(data->eoc_gpio); 166 irq = gpio_to_irq(data->eoc_gpio);
167 167
168 rc = request_irq(irq, ak8975_irq_handler, 168 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
169 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 169 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
170 dev_name(&client->dev), data); 170 dev_name(&client->dev), data);
171 if (rc < 0) { 171 if (rc < 0) {
@@ -513,21 +513,21 @@ static int ak8975_probe(struct i2c_client *client,
513 /* We may not have a GPIO based IRQ to scan, that is fine, we will 513 /* We may not have a GPIO based IRQ to scan, that is fine, we will
514 poll if so */ 514 poll if so */
515 if (gpio_is_valid(eoc_gpio)) { 515 if (gpio_is_valid(eoc_gpio)) {
516 err = gpio_request_one(eoc_gpio, GPIOF_IN, "ak_8975"); 516 err = devm_gpio_request_one(&client->dev, eoc_gpio,
517 GPIOF_IN, "ak_8975");
517 if (err < 0) { 518 if (err < 0) {
518 dev_err(&client->dev, 519 dev_err(&client->dev,
519 "failed to request GPIO %d, error %d\n", 520 "failed to request GPIO %d, error %d\n",
520 eoc_gpio, err); 521 eoc_gpio, err);
521 goto exit; 522 return err;
522 } 523 }
523 } 524 }
524 525
525 /* Register with IIO */ 526 /* Register with IIO */
526 indio_dev = iio_device_alloc(sizeof(*data)); 527 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
527 if (indio_dev == NULL) { 528 if (indio_dev == NULL)
528 err = -ENOMEM; 529 return -ENOMEM;
529 goto exit_gpio; 530
530 }
531 data = iio_priv(indio_dev); 531 data = iio_priv(indio_dev);
532 i2c_set_clientdata(client, indio_dev); 532 i2c_set_clientdata(client, indio_dev);
533 533
@@ -542,17 +542,16 @@ static int ak8975_probe(struct i2c_client *client,
542 name = (char *) id->name; 542 name = (char *) id->name;
543 } else if (ACPI_HANDLE(&client->dev)) 543 } else if (ACPI_HANDLE(&client->dev))
544 name = ak8975_match_acpi_device(&client->dev, &data->chipset); 544 name = ak8975_match_acpi_device(&client->dev, &data->chipset);
545 else { 545 else
546 err = -ENOSYS; 546 return -ENOSYS;
547 goto exit_free_iio; 547
548 }
549 dev_dbg(&client->dev, "Asahi compass chip %s\n", name); 548 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
550 549
551 /* Perform some basic start-of-day setup of the device. */ 550 /* Perform some basic start-of-day setup of the device. */
552 err = ak8975_setup(client); 551 err = ak8975_setup(client);
553 if (err < 0) { 552 if (err < 0) {
554 dev_err(&client->dev, "AK8975 initialization fails\n"); 553 dev_err(&client->dev, "AK8975 initialization fails\n");
555 goto exit_free_iio; 554 return err;
556 } 555 }
557 556
558 data->client = client; 557 data->client = client;
@@ -564,37 +563,9 @@ static int ak8975_probe(struct i2c_client *client,
564 indio_dev->info = &ak8975_info; 563 indio_dev->info = &ak8975_info;
565 indio_dev->modes = INDIO_DIRECT_MODE; 564 indio_dev->modes = INDIO_DIRECT_MODE;
566 indio_dev->name = name; 565 indio_dev->name = name;
567 err = iio_device_register(indio_dev); 566 err = devm_iio_device_register(&client->dev, indio_dev);
568 if (err < 0) 567 if (err < 0)
569 goto exit_free_iio; 568 return err;
570
571 return 0;
572
573exit_free_iio:
574 iio_device_free(indio_dev);
575 if (data->eoc_irq)
576 free_irq(data->eoc_irq, data);
577exit_gpio:
578 if (gpio_is_valid(eoc_gpio))
579 gpio_free(eoc_gpio);
580exit:
581 return err;
582}
583
584static int ak8975_remove(struct i2c_client *client)
585{
586 struct iio_dev *indio_dev = i2c_get_clientdata(client);
587 struct ak8975_data *data = iio_priv(indio_dev);
588
589 iio_device_unregister(indio_dev);
590
591 if (data->eoc_irq)
592 free_irq(data->eoc_irq, data);
593
594 if (gpio_is_valid(data->eoc_gpio))
595 gpio_free(data->eoc_gpio);
596
597 iio_device_free(indio_dev);
598 569
599 return 0; 570 return 0;
600} 571}
@@ -621,7 +592,6 @@ static struct i2c_driver ak8975_driver = {
621 .acpi_match_table = ACPI_PTR(ak_acpi_match), 592 .acpi_match_table = ACPI_PTR(ak_acpi_match),
622 }, 593 },
623 .probe = ak8975_probe, 594 .probe = ak8975_probe,
624 .remove = ak8975_remove,
625 .id_table = ak8975_id, 595 .id_table = ak8975_id,
626}; 596};
627module_i2c_driver(ak8975_driver); 597module_i2c_driver(ak8975_driver);
diff --git a/drivers/iio/magnetometer/hid-sensor-magn-3d.c b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
index b2b0937d5133..3ec777a8f64e 100644
--- a/drivers/iio/magnetometer/hid-sensor-magn-3d.c
+++ b/drivers/iio/magnetometer/hid-sensor-magn-3d.c
@@ -35,6 +35,10 @@ enum magn_3d_channel {
35 CHANNEL_SCAN_INDEX_X, 35 CHANNEL_SCAN_INDEX_X,
36 CHANNEL_SCAN_INDEX_Y, 36 CHANNEL_SCAN_INDEX_Y,
37 CHANNEL_SCAN_INDEX_Z, 37 CHANNEL_SCAN_INDEX_Z,
38 CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP,
39 CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP,
40 CHANNEL_SCAN_INDEX_NORTH_MAGN,
41 CHANNEL_SCAN_INDEX_NORTH_TRUE,
38 MAGN_3D_CHANNEL_MAX, 42 MAGN_3D_CHANNEL_MAX,
39}; 43};
40 44
@@ -42,7 +46,12 @@ struct magn_3d_state {
42 struct hid_sensor_hub_callbacks callbacks; 46 struct hid_sensor_hub_callbacks callbacks;
43 struct hid_sensor_common common_attributes; 47 struct hid_sensor_common common_attributes;
44 struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX]; 48 struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
45 u32 magn_val[MAGN_3D_CHANNEL_MAX]; 49
50 /* dynamically sized array to hold sensor values */
51 u32 *iio_vals;
52 /* array of pointers to sensor value */
53 u32 *magn_val_addr[MAGN_3D_CHANNEL_MAX];
54
46 int scale_pre_decml; 55 int scale_pre_decml;
47 int scale_post_decml; 56 int scale_post_decml;
48 int scale_precision; 57 int scale_precision;
@@ -52,7 +61,11 @@ struct magn_3d_state {
52static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = { 61static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
53 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS, 62 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS,
54 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS, 63 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS,
55 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS 64 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS,
65 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH,
66 HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH,
67 HID_USAGE_SENSOR_ORIENT_MAGN_NORTH,
68 HID_USAGE_SENSOR_ORIENT_TRUE_NORTH,
56}; 69};
57 70
58/* Channel definitions */ 71/* Channel definitions */
@@ -66,7 +79,6 @@ static const struct iio_chan_spec magn_3d_channels[] = {
66 BIT(IIO_CHAN_INFO_SCALE) | 79 BIT(IIO_CHAN_INFO_SCALE) |
67 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 80 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
68 BIT(IIO_CHAN_INFO_HYSTERESIS), 81 BIT(IIO_CHAN_INFO_HYSTERESIS),
69 .scan_index = CHANNEL_SCAN_INDEX_X,
70 }, { 82 }, {
71 .type = IIO_MAGN, 83 .type = IIO_MAGN,
72 .modified = 1, 84 .modified = 1,
@@ -76,7 +88,6 @@ static const struct iio_chan_spec magn_3d_channels[] = {
76 BIT(IIO_CHAN_INFO_SCALE) | 88 BIT(IIO_CHAN_INFO_SCALE) |
77 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 89 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
78 BIT(IIO_CHAN_INFO_HYSTERESIS), 90 BIT(IIO_CHAN_INFO_HYSTERESIS),
79 .scan_index = CHANNEL_SCAN_INDEX_Y,
80 }, { 91 }, {
81 .type = IIO_MAGN, 92 .type = IIO_MAGN,
82 .modified = 1, 93 .modified = 1,
@@ -86,7 +97,42 @@ static const struct iio_chan_spec magn_3d_channels[] = {
86 BIT(IIO_CHAN_INFO_SCALE) | 97 BIT(IIO_CHAN_INFO_SCALE) |
87 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 98 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
88 BIT(IIO_CHAN_INFO_HYSTERESIS), 99 BIT(IIO_CHAN_INFO_HYSTERESIS),
89 .scan_index = CHANNEL_SCAN_INDEX_Z, 100 }, {
101 .type = IIO_ROT,
102 .modified = 1,
103 .channel2 = IIO_MOD_NORTH_MAGN_TILT_COMP,
104 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
105 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
106 BIT(IIO_CHAN_INFO_SCALE) |
107 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
108 BIT(IIO_CHAN_INFO_HYSTERESIS),
109 }, {
110 .type = IIO_ROT,
111 .modified = 1,
112 .channel2 = IIO_MOD_NORTH_TRUE_TILT_COMP,
113 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
114 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
115 BIT(IIO_CHAN_INFO_SCALE) |
116 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
117 BIT(IIO_CHAN_INFO_HYSTERESIS),
118 }, {
119 .type = IIO_ROT,
120 .modified = 1,
121 .channel2 = IIO_MOD_NORTH_MAGN,
122 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
123 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
124 BIT(IIO_CHAN_INFO_SCALE) |
125 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
126 BIT(IIO_CHAN_INFO_HYSTERESIS),
127 }, {
128 .type = IIO_ROT,
129 .modified = 1,
130 .channel2 = IIO_MOD_NORTH_TRUE,
131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
132 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
133 BIT(IIO_CHAN_INFO_SCALE) |
134 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
135 BIT(IIO_CHAN_INFO_HYSTERESIS),
90 } 136 }
91}; 137};
92 138
@@ -126,8 +172,8 @@ static int magn_3d_read_raw(struct iio_dev *indio_dev,
126 msleep_interruptible(poll_value * 2); 172 msleep_interruptible(poll_value * 2);
127 173
128 report_id = 174 report_id =
129 magn_state->magn[chan->scan_index].report_id; 175 magn_state->magn[chan->address].report_id;
130 address = magn_3d_addresses[chan->scan_index]; 176 address = magn_3d_addresses[chan->address];
131 if (report_id >= 0) 177 if (report_id >= 0)
132 *val = sensor_hub_input_attr_get_raw_value( 178 *val = sensor_hub_input_attr_get_raw_value(
133 magn_state->common_attributes.hsdev, 179 magn_state->common_attributes.hsdev,
@@ -218,8 +264,8 @@ static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
218 dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n"); 264 dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n");
219 if (atomic_read(&magn_state->common_attributes.data_ready)) 265 if (atomic_read(&magn_state->common_attributes.data_ready))
220 hid_sensor_push_data(indio_dev, 266 hid_sensor_push_data(indio_dev,
221 magn_state->magn_val, 267 magn_state->iio_vals,
222 sizeof(magn_state->magn_val)); 268 sizeof(magn_state->iio_vals));
223 269
224 return 0; 270 return 0;
225} 271}
@@ -233,52 +279,126 @@ static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
233 struct iio_dev *indio_dev = platform_get_drvdata(priv); 279 struct iio_dev *indio_dev = platform_get_drvdata(priv);
234 struct magn_3d_state *magn_state = iio_priv(indio_dev); 280 struct magn_3d_state *magn_state = iio_priv(indio_dev);
235 int offset; 281 int offset;
236 int ret = -EINVAL; 282 int ret = 0;
283 u32 *iio_val = NULL;
237 284
238 switch (usage_id) { 285 switch (usage_id) {
239 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS: 286 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS:
240 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS: 287 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS:
241 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS: 288 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
242 offset = usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS; 289 offset = (usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS)
243 magn_state->magn_val[CHANNEL_SCAN_INDEX_X + offset] = 290 + CHANNEL_SCAN_INDEX_X;
244 *(u32 *)raw_data; 291 break;
245 ret = 0; 292 case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH:
293 case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH:
294 case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH:
295 case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH:
296 offset = (usage_id - HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH)
297 + CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP;
246 break; 298 break;
247 default: 299 default:
248 break; 300 return -EINVAL;
249 } 301 }
250 302
303 iio_val = magn_state->magn_val_addr[offset];
304
305 if (iio_val != NULL)
306 *iio_val = *((u32 *)raw_data);
307 else
308 ret = -EINVAL;
309
251 return ret; 310 return ret;
252} 311}
253 312
254/* Parse report which is specific to an usage id*/ 313/* Parse report which is specific to an usage id*/
255static int magn_3d_parse_report(struct platform_device *pdev, 314static int magn_3d_parse_report(struct platform_device *pdev,
256 struct hid_sensor_hub_device *hsdev, 315 struct hid_sensor_hub_device *hsdev,
257 struct iio_chan_spec *channels, 316 struct iio_chan_spec **channels,
317 int *chan_count,
258 unsigned usage_id, 318 unsigned usage_id,
259 struct magn_3d_state *st) 319 struct magn_3d_state *st)
260{ 320{
261 int ret;
262 int i; 321 int i;
322 int attr_count = 0;
323 struct iio_chan_spec *_channels;
324
325 /* Scan for each usage attribute supported */
326 for (i = 0; i < MAGN_3D_CHANNEL_MAX; i++) {
327 int status;
328 u32 address = magn_3d_addresses[i];
329
330 /* Check if usage attribute exists in the sensor hub device */
331 status = sensor_hub_input_get_attribute_info(hsdev,
332 HID_INPUT_REPORT,
333 usage_id,
334 address,
335 &(st->magn[i]));
336 if (!status)
337 attr_count++;
338 }
263 339
264 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) { 340 if (attr_count <= 0) {
265 ret = sensor_hub_input_get_attribute_info(hsdev, 341 dev_err(&pdev->dev,
266 HID_INPUT_REPORT, 342 "failed to find any supported usage attributes in report\n");
267 usage_id, 343 return -EINVAL;
268 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS + i,
269 &st->magn[CHANNEL_SCAN_INDEX_X + i]);
270 if (ret < 0)
271 break;
272 magn_3d_adjust_channel_bit_mask(channels,
273 CHANNEL_SCAN_INDEX_X + i,
274 st->magn[CHANNEL_SCAN_INDEX_X + i].size);
275 } 344 }
276 dev_dbg(&pdev->dev, "magn_3d %x:%x, %x:%x, %x:%x\n", 345
346 dev_dbg(&pdev->dev, "magn_3d Found %d usage attributes\n",
347 attr_count);
348 dev_dbg(&pdev->dev, "magn_3d X: %x:%x Y: %x:%x Z: %x:%x\n",
277 st->magn[0].index, 349 st->magn[0].index,
278 st->magn[0].report_id, 350 st->magn[0].report_id,
279 st->magn[1].index, st->magn[1].report_id, 351 st->magn[1].index, st->magn[1].report_id,
280 st->magn[2].index, st->magn[2].report_id); 352 st->magn[2].index, st->magn[2].report_id);
281 353
354 /* Setup IIO channel array */
355 _channels = devm_kcalloc(&pdev->dev, attr_count,
356 sizeof(struct iio_chan_spec),
357 GFP_KERNEL);
358 if (!_channels) {
359 dev_err(&pdev->dev,
360 "failed to allocate space for iio channels\n");
361 return -ENOMEM;
362 }
363
364 st->iio_vals = devm_kcalloc(&pdev->dev, attr_count,
365 sizeof(u32),
366 GFP_KERNEL);
367 if (!st->iio_vals) {
368 dev_err(&pdev->dev,
369 "failed to allocate space for iio values array\n");
370 return -ENOMEM;
371 }
372
373 for (i = 0, *chan_count = 0;
374 i < MAGN_3D_CHANNEL_MAX && *chan_count < attr_count;
375 i++){
376 if (st->magn[i].index >= 0) {
377 /* Setup IIO channel struct */
378 (_channels[*chan_count]) = magn_3d_channels[i];
379 (_channels[*chan_count]).scan_index = *chan_count;
380 (_channels[*chan_count]).address = i;
381
382 /* Set magn_val_addr to iio value address */
383 st->magn_val_addr[i] = &(st->iio_vals[*chan_count]);
384 magn_3d_adjust_channel_bit_mask(_channels,
385 *chan_count,
386 st->magn[i].size);
387 (*chan_count)++;
388 }
389 }
390
391 if (*chan_count <= 0) {
392 dev_err(&pdev->dev,
393 "failed to find any magnetic channels setup\n");
394 return -EINVAL;
395 }
396
397 *channels = _channels;
398
399 dev_dbg(&pdev->dev, "magn_3d Setup %d IIO channels\n",
400 *chan_count);
401
282 st->scale_precision = hid_sensor_format_scale( 402 st->scale_precision = hid_sensor_format_scale(
283 HID_USAGE_SENSOR_COMPASS_3D, 403 HID_USAGE_SENSOR_COMPASS_3D,
284 &st->magn[CHANNEL_SCAN_INDEX_X], 404 &st->magn[CHANNEL_SCAN_INDEX_X],
@@ -296,7 +416,7 @@ static int magn_3d_parse_report(struct platform_device *pdev,
296 st->common_attributes.sensitivity.report_id); 416 st->common_attributes.sensitivity.report_id);
297 } 417 }
298 418
299 return ret; 419 return 0;
300} 420}
301 421
302/* Function to initialize the processing for usage id */ 422/* Function to initialize the processing for usage id */
@@ -308,6 +428,7 @@ static int hid_magn_3d_probe(struct platform_device *pdev)
308 struct magn_3d_state *magn_state; 428 struct magn_3d_state *magn_state;
309 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 429 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
310 struct iio_chan_spec *channels; 430 struct iio_chan_spec *channels;
431 int chan_count = 0;
311 432
312 indio_dev = devm_iio_device_alloc(&pdev->dev, 433 indio_dev = devm_iio_device_alloc(&pdev->dev,
313 sizeof(struct magn_3d_state)); 434 sizeof(struct magn_3d_state));
@@ -328,22 +449,16 @@ static int hid_magn_3d_probe(struct platform_device *pdev)
328 return ret; 449 return ret;
329 } 450 }
330 451
331 channels = kmemdup(magn_3d_channels, sizeof(magn_3d_channels), 452 ret = magn_3d_parse_report(pdev, hsdev,
332 GFP_KERNEL); 453 &channels, &chan_count,
333 if (!channels) {
334 dev_err(&pdev->dev, "failed to duplicate channels\n");
335 return -ENOMEM;
336 }
337
338 ret = magn_3d_parse_report(pdev, hsdev, channels,
339 HID_USAGE_SENSOR_COMPASS_3D, magn_state); 454 HID_USAGE_SENSOR_COMPASS_3D, magn_state);
340 if (ret) { 455 if (ret) {
341 dev_err(&pdev->dev, "failed to setup attributes\n"); 456 dev_err(&pdev->dev, "failed to parse report\n");
342 goto error_free_dev_mem; 457 return ret;
343 } 458 }
344 459
345 indio_dev->channels = channels; 460 indio_dev->channels = channels;
346 indio_dev->num_channels = ARRAY_SIZE(magn_3d_channels); 461 indio_dev->num_channels = chan_count;
347 indio_dev->dev.parent = &pdev->dev; 462 indio_dev->dev.parent = &pdev->dev;
348 indio_dev->info = &magn_3d_info; 463 indio_dev->info = &magn_3d_info;
349 indio_dev->name = name; 464 indio_dev->name = name;
@@ -353,7 +468,7 @@ static int hid_magn_3d_probe(struct platform_device *pdev)
353 NULL, NULL); 468 NULL, NULL);
354 if (ret) { 469 if (ret) {
355 dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 470 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
356 goto error_free_dev_mem; 471 return ret;
357 } 472 }
358 atomic_set(&magn_state->common_attributes.data_ready, 0); 473 atomic_set(&magn_state->common_attributes.data_ready, 0);
359 ret = hid_sensor_setup_trigger(indio_dev, name, 474 ret = hid_sensor_setup_trigger(indio_dev, name,
@@ -387,8 +502,6 @@ error_remove_trigger:
387 hid_sensor_remove_trigger(&magn_state->common_attributes); 502 hid_sensor_remove_trigger(&magn_state->common_attributes);
388error_unreg_buffer_funcs: 503error_unreg_buffer_funcs:
389 iio_triggered_buffer_cleanup(indio_dev); 504 iio_triggered_buffer_cleanup(indio_dev);
390error_free_dev_mem:
391 kfree(indio_dev->channels);
392 return ret; 505 return ret;
393} 506}
394 507
@@ -403,7 +516,6 @@ static int hid_magn_3d_remove(struct platform_device *pdev)
403 iio_device_unregister(indio_dev); 516 iio_device_unregister(indio_dev);
404 hid_sensor_remove_trigger(&magn_state->common_attributes); 517 hid_sensor_remove_trigger(&magn_state->common_attributes);
405 iio_triggered_buffer_cleanup(indio_dev); 518 iio_triggered_buffer_cleanup(indio_dev);
406 kfree(indio_dev->channels);
407 519
408 return 0; 520 return 0;
409} 521}
diff --git a/drivers/iio/magnetometer/st_magn_core.c b/drivers/iio/magnetometer/st_magn_core.c
index 240a21dd0c61..a4b64130ac2f 100644
--- a/drivers/iio/magnetometer/st_magn_core.c
+++ b/drivers/iio/magnetometer/st_magn_core.c
@@ -299,6 +299,9 @@ static int st_magn_read_raw(struct iio_dev *indio_dev,
299 else 299 else
300 *val2 = mdata->current_fullscale->gain; 300 *val2 = mdata->current_fullscale->gain;
301 return IIO_VAL_INT_PLUS_MICRO; 301 return IIO_VAL_INT_PLUS_MICRO;
302 case IIO_CHAN_INFO_SAMP_FREQ:
303 *val = mdata->odr;
304 return IIO_VAL_INT;
302 default: 305 default:
303 return -EINVAL; 306 return -EINVAL;
304 } 307 }
@@ -316,6 +319,13 @@ static int st_magn_write_raw(struct iio_dev *indio_dev,
316 case IIO_CHAN_INFO_SCALE: 319 case IIO_CHAN_INFO_SCALE:
317 err = st_sensors_set_fullscale_by_gain(indio_dev, val2); 320 err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
318 break; 321 break;
322 case IIO_CHAN_INFO_SAMP_FREQ:
323 if (val2)
324 return -EINVAL;
325 mutex_lock(&indio_dev->mlock);
326 err = st_sensors_set_odr(indio_dev, val);
327 mutex_unlock(&indio_dev->mlock);
328 return err;
319 default: 329 default:
320 err = -EINVAL; 330 err = -EINVAL;
321 } 331 }
@@ -323,14 +333,12 @@ static int st_magn_write_raw(struct iio_dev *indio_dev,
323 return err; 333 return err;
324} 334}
325 335
326static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
327static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 336static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
328static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available); 337static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
329 338
330static struct attribute *st_magn_attributes[] = { 339static struct attribute *st_magn_attributes[] = {
331 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 340 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
332 &iio_dev_attr_in_magn_scale_available.dev_attr.attr, 341 &iio_dev_attr_in_magn_scale_available.dev_attr.attr,
333 &iio_dev_attr_sampling_frequency.dev_attr.attr,
334 NULL, 342 NULL,
335}; 343};
336 344
diff --git a/drivers/iio/magnetometer/st_magn_i2c.c b/drivers/iio/magnetometer/st_magn_i2c.c
index 892e0feeb5c1..689250058442 100644
--- a/drivers/iio/magnetometer/st_magn_i2c.c
+++ b/drivers/iio/magnetometer/st_magn_i2c.c
@@ -18,6 +18,27 @@
18#include <linux/iio/common/st_sensors_i2c.h> 18#include <linux/iio/common/st_sensors_i2c.h>
19#include "st_magn.h" 19#include "st_magn.h"
20 20
21#ifdef CONFIG_OF
22static const struct of_device_id st_magn_of_match[] = {
23 {
24 .compatible = "st,lsm303dlhc-magn",
25 .data = LSM303DLHC_MAGN_DEV_NAME,
26 },
27 {
28 .compatible = "st,lsm303dlm-magn",
29 .data = LSM303DLM_MAGN_DEV_NAME,
30 },
31 {
32 .compatible = "st,lis3mdl-magn",
33 .data = LIS3MDL_MAGN_DEV_NAME,
34 },
35 {},
36};
37MODULE_DEVICE_TABLE(of, st_magn_of_match);
38#else
39#define st_magn_of_match NULL
40#endif
41
21static int st_magn_i2c_probe(struct i2c_client *client, 42static int st_magn_i2c_probe(struct i2c_client *client,
22 const struct i2c_device_id *id) 43 const struct i2c_device_id *id)
23{ 44{
@@ -31,6 +52,7 @@ static int st_magn_i2c_probe(struct i2c_client *client,
31 52
32 mdata = iio_priv(indio_dev); 53 mdata = iio_priv(indio_dev);
33 mdata->dev = &client->dev; 54 mdata->dev = &client->dev;
55 st_sensors_of_i2c_probe(client, st_magn_of_match);
34 56
35 st_sensors_i2c_configure(indio_dev, client, mdata); 57 st_sensors_i2c_configure(indio_dev, client, mdata);
36 58
@@ -61,6 +83,7 @@ static struct i2c_driver st_magn_driver = {
61 .driver = { 83 .driver = {
62 .owner = THIS_MODULE, 84 .owner = THIS_MODULE,
63 .name = "st-magn-i2c", 85 .name = "st-magn-i2c",
86 .of_match_table = of_match_ptr(st_magn_of_match),
64 }, 87 },
65 .probe = st_magn_i2c_probe, 88 .probe = st_magn_i2c_probe,
66 .remove = st_magn_i2c_remove, 89 .remove = st_magn_i2c_remove,
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index ffac8ac1efca..15afbc919521 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -70,4 +70,14 @@ config IIO_ST_PRESS_SPI
70 depends on IIO_ST_PRESS 70 depends on IIO_ST_PRESS
71 depends on IIO_ST_SENSORS_SPI 71 depends on IIO_ST_SENSORS_SPI
72 72
73config T5403
74 tristate "EPCOS T5403 digital barometric pressure sensor driver"
75 depends on I2C
76 help
77 Say yes here to build support for the EPCOS T5403 pressure sensor
78 connected via I2C.
79
80 To compile this driver as a module, choose M here: the module
81 will be called t5403.
82
73endmenu 83endmenu
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile
index c53d2500737a..90a37e85cf21 100644
--- a/drivers/iio/pressure/Makefile
+++ b/drivers/iio/pressure/Makefile
@@ -9,6 +9,7 @@ obj-$(CONFIG_MPL3115) += mpl3115.o
9obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o 9obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o
10st_pressure-y := st_pressure_core.o 10st_pressure-y := st_pressure_core.o
11st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o 11st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o
12obj-$(CONFIG_T5403) += t5403.o
12 13
13obj-$(CONFIG_IIO_ST_PRESS_I2C) += st_pressure_i2c.o 14obj-$(CONFIG_IIO_ST_PRESS_I2C) += st_pressure_i2c.o
14obj-$(CONFIG_IIO_ST_PRESS_SPI) += st_pressure_spi.o 15obj-$(CONFIG_IIO_ST_PRESS_SPI) += st_pressure_spi.o
diff --git a/drivers/iio/pressure/st_pressure_core.c b/drivers/iio/pressure/st_pressure_core.c
index cd7e01f3a93b..473d914ef470 100644
--- a/drivers/iio/pressure/st_pressure_core.c
+++ b/drivers/iio/pressure/st_pressure_core.c
@@ -307,6 +307,27 @@ static const struct st_sensors st_press_sensors[] = {
307 }, 307 },
308}; 308};
309 309
310static int st_press_write_raw(struct iio_dev *indio_dev,
311 struct iio_chan_spec const *ch,
312 int val,
313 int val2,
314 long mask)
315{
316 int err;
317
318 switch (mask) {
319 case IIO_CHAN_INFO_SAMP_FREQ:
320 if (val2)
321 return -EINVAL;
322 mutex_lock(&indio_dev->mlock);
323 err = st_sensors_set_odr(indio_dev, val);
324 mutex_unlock(&indio_dev->mlock);
325 return err;
326 default:
327 return -EINVAL;
328 }
329}
330
310static int st_press_read_raw(struct iio_dev *indio_dev, 331static int st_press_read_raw(struct iio_dev *indio_dev,
311 struct iio_chan_spec const *ch, int *val, 332 struct iio_chan_spec const *ch, int *val,
312 int *val2, long mask) 333 int *val2, long mask)
@@ -349,6 +370,9 @@ static int st_press_read_raw(struct iio_dev *indio_dev,
349 } 370 }
350 371
351 return IIO_VAL_FRACTIONAL; 372 return IIO_VAL_FRACTIONAL;
373 case IIO_CHAN_INFO_SAMP_FREQ:
374 *val = pdata->odr;
375 return IIO_VAL_INT;
352 default: 376 default:
353 return -EINVAL; 377 return -EINVAL;
354 } 378 }
@@ -357,12 +381,10 @@ read_error:
357 return err; 381 return err;
358} 382}
359 383
360static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
361static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 384static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
362 385
363static struct attribute *st_press_attributes[] = { 386static struct attribute *st_press_attributes[] = {
364 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 387 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
365 &iio_dev_attr_sampling_frequency.dev_attr.attr,
366 NULL, 388 NULL,
367}; 389};
368 390
@@ -374,6 +396,7 @@ static const struct iio_info press_info = {
374 .driver_module = THIS_MODULE, 396 .driver_module = THIS_MODULE,
375 .attrs = &st_press_attribute_group, 397 .attrs = &st_press_attribute_group,
376 .read_raw = &st_press_read_raw, 398 .read_raw = &st_press_read_raw,
399 .write_raw = &st_press_write_raw,
377}; 400};
378 401
379#ifdef CONFIG_IIO_TRIGGER 402#ifdef CONFIG_IIO_TRIGGER
diff --git a/drivers/iio/pressure/st_pressure_i2c.c b/drivers/iio/pressure/st_pressure_i2c.c
index 3cd73e39b840..acaf165260bb 100644
--- a/drivers/iio/pressure/st_pressure_i2c.c
+++ b/drivers/iio/pressure/st_pressure_i2c.c
@@ -18,6 +18,27 @@
18#include <linux/iio/common/st_sensors_i2c.h> 18#include <linux/iio/common/st_sensors_i2c.h>
19#include "st_pressure.h" 19#include "st_pressure.h"
20 20
21#ifdef CONFIG_OF
22static const struct of_device_id st_press_of_match[] = {
23 {
24 .compatible = "st,lps001wp-press",
25 .data = LPS001WP_PRESS_DEV_NAME,
26 },
27 {
28 .compatible = "st,lps25h-press",
29 .data = LPS25H_PRESS_DEV_NAME,
30 },
31 {
32 .compatible = "st,lps331ap-press",
33 .data = LPS331AP_PRESS_DEV_NAME,
34 },
35 {},
36};
37MODULE_DEVICE_TABLE(of, st_press_of_match);
38#else
39#define st_press_of_match NULL
40#endif
41
21static int st_press_i2c_probe(struct i2c_client *client, 42static int st_press_i2c_probe(struct i2c_client *client,
22 const struct i2c_device_id *id) 43 const struct i2c_device_id *id)
23{ 44{
@@ -31,6 +52,7 @@ static int st_press_i2c_probe(struct i2c_client *client,
31 52
32 pdata = iio_priv(indio_dev); 53 pdata = iio_priv(indio_dev);
33 pdata->dev = &client->dev; 54 pdata->dev = &client->dev;
55 st_sensors_of_i2c_probe(client, st_press_of_match);
34 56
35 st_sensors_i2c_configure(indio_dev, client, pdata); 57 st_sensors_i2c_configure(indio_dev, client, pdata);
36 58
@@ -60,6 +82,7 @@ static struct i2c_driver st_press_driver = {
60 .driver = { 82 .driver = {
61 .owner = THIS_MODULE, 83 .owner = THIS_MODULE,
62 .name = "st-press-i2c", 84 .name = "st-press-i2c",
85 .of_match_table = of_match_ptr(st_press_of_match),
63 }, 86 },
64 .probe = st_press_i2c_probe, 87 .probe = st_press_i2c_probe,
65 .remove = st_press_i2c_remove, 88 .remove = st_press_i2c_remove,
diff --git a/drivers/iio/pressure/t5403.c b/drivers/iio/pressure/t5403.c
new file mode 100644
index 000000000000..e11cd3938d67
--- /dev/null
+++ b/drivers/iio/pressure/t5403.c
@@ -0,0 +1,275 @@
1/*
2 * t5403.c - Support for EPCOS T5403 pressure/temperature sensor
3 *
4 * Copyright (c) 2014 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 * (7-bit I2C slave address 0x77)
11 *
12 * TODO: end-of-conversion irq
13 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/iio/iio.h>
18#include <linux/iio/sysfs.h>
19#include <linux/delay.h>
20
21#define T5403_DATA 0xf5 /* data, LSB first, 16 bit */
22#define T5403_CALIB_DATA 0x8e /* 10 calibration coeff., LSB first, 16 bit */
23#define T5403_SLAVE_ADDR 0x88 /* I2C slave address, 0x77 */
24#define T5403_COMMAND 0xf1
25
26/* command bits */
27#define T5403_MODE_SHIFT 3 /* conversion time: 2, 8, 16, 66 ms */
28#define T5403_PT BIT(1) /* 0 .. pressure, 1 .. temperature measurement */
29#define T5403_SCO BIT(0) /* start conversion */
30
31#define T5403_MODE_LOW 0
32#define T5403_MODE_STANDARD 1
33#define T5403_MODE_HIGH 2
34#define T5403_MODE_ULTRA_HIGH 3
35
36#define T5403_I2C_MASK (~BIT(7))
37#define T5403_I2C_ADDR 0x77
38
39static const int t5403_pressure_conv_ms[] = {2, 8, 16, 66};
40
41struct t5403_data {
42 struct i2c_client *client;
43 struct mutex lock;
44 int mode;
45 __le16 c[10];
46};
47
48#define T5403_C_U16(i) le16_to_cpu(data->c[(i) - 1])
49#define T5403_C(i) sign_extend32(T5403_C_U16(i), 15)
50
51static int t5403_read(struct t5403_data *data, bool pressure)
52{
53 int wait_time = 3; /* wakeup time in ms */
54
55 int ret = i2c_smbus_write_byte_data(data->client, T5403_COMMAND,
56 (pressure ? (data->mode << T5403_MODE_SHIFT) : T5403_PT) |
57 T5403_SCO);
58 if (ret < 0)
59 return ret;
60
61 wait_time += pressure ? t5403_pressure_conv_ms[data->mode] : 2;
62
63 msleep(wait_time);
64
65 return i2c_smbus_read_word_data(data->client, T5403_DATA);
66}
67
68static int t5403_comp_pressure(struct t5403_data *data, int *val, int *val2)
69{
70 int ret;
71 s16 t_r;
72 u16 p_r;
73 s32 S, O, X;
74
75 mutex_lock(&data->lock);
76
77 ret = t5403_read(data, false);
78 if (ret < 0)
79 goto done;
80 t_r = ret;
81
82 ret = t5403_read(data, true);
83 if (ret < 0)
84 goto done;
85 p_r = ret;
86
87 /* see EPCOS application note */
88 S = T5403_C_U16(3) + (s32) T5403_C_U16(4) * t_r / 0x20000 +
89 T5403_C(5) * t_r / 0x8000 * t_r / 0x80000 +
90 T5403_C(9) * t_r / 0x8000 * t_r / 0x8000 * t_r / 0x10000;
91
92 O = T5403_C(6) * 0x4000 + T5403_C(7) * t_r / 8 +
93 T5403_C(8) * t_r / 0x8000 * t_r / 16 +
94 T5403_C(9) * t_r / 0x8000 * t_r / 0x10000 * t_r;
95
96 X = (S * p_r + O) / 0x4000;
97
98 X += ((X - 75000) * (X - 75000) / 0x10000 - 9537) *
99 T5403_C(10) / 0x10000;
100
101 *val = X / 1000;
102 *val2 = (X % 1000) * 1000;
103
104done:
105 mutex_unlock(&data->lock);
106 return ret;
107}
108
109static int t5403_comp_temp(struct t5403_data *data, int *val)
110{
111 int ret;
112 s16 t_r;
113
114 mutex_lock(&data->lock);
115 ret = t5403_read(data, false);
116 if (ret < 0)
117 goto done;
118 t_r = ret;
119
120 /* see EPCOS application note */
121 *val = ((s32) T5403_C_U16(1) * t_r / 0x100 +
122 (s32) T5403_C_U16(2) * 0x40) * 1000 / 0x10000;
123
124done:
125 mutex_unlock(&data->lock);
126 return ret;
127}
128
129static int t5403_read_raw(struct iio_dev *indio_dev,
130 struct iio_chan_spec const *chan,
131 int *val, int *val2, long mask)
132{
133 struct t5403_data *data = iio_priv(indio_dev);
134 int ret;
135
136 switch (mask) {
137 case IIO_CHAN_INFO_PROCESSED:
138 switch (chan->type) {
139 case IIO_PRESSURE:
140 ret = t5403_comp_pressure(data, val, val2);
141 if (ret < 0)
142 return ret;
143 return IIO_VAL_INT_PLUS_MICRO;
144 case IIO_TEMP:
145 ret = t5403_comp_temp(data, val);
146 if (ret < 0)
147 return ret;
148 return IIO_VAL_INT;
149 default:
150 return -EINVAL;
151 }
152 case IIO_CHAN_INFO_INT_TIME:
153 *val = 0;
154 *val2 = t5403_pressure_conv_ms[data->mode] * 1000;
155 return IIO_VAL_INT_PLUS_MICRO;
156 default:
157 return -EINVAL;
158 }
159}
160
161static int t5403_write_raw(struct iio_dev *indio_dev,
162 struct iio_chan_spec const *chan,
163 int val, int val2, long mask)
164{
165 struct t5403_data *data = iio_priv(indio_dev);
166 int i;
167
168 switch (mask) {
169 case IIO_CHAN_INFO_INT_TIME:
170 if (val != 0)
171 return -EINVAL;
172 for (i = 0; i < ARRAY_SIZE(t5403_pressure_conv_ms); i++)
173 if (val2 == t5403_pressure_conv_ms[i] * 1000) {
174 mutex_lock(&data->lock);
175 data->mode = i;
176 mutex_unlock(&data->lock);
177 return 0;
178 }
179 return -EINVAL;
180 default:
181 return -EINVAL;
182 }
183}
184
185static const struct iio_chan_spec t5403_channels[] = {
186 {
187 .type = IIO_PRESSURE,
188 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
189 BIT(IIO_CHAN_INFO_INT_TIME),
190 },
191 {
192 .type = IIO_TEMP,
193 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
194 },
195};
196
197static IIO_CONST_ATTR_INT_TIME_AVAIL("0.002 0.008 0.016 0.066");
198
199static struct attribute *t5403_attributes[] = {
200 &iio_const_attr_integration_time_available.dev_attr.attr,
201 NULL
202};
203
204static const struct attribute_group t5403_attribute_group = {
205 .attrs = t5403_attributes,
206};
207
208static const struct iio_info t5403_info = {
209 .read_raw = &t5403_read_raw,
210 .write_raw = &t5403_write_raw,
211 .attrs = &t5403_attribute_group,
212 .driver_module = THIS_MODULE,
213};
214
215static int t5403_probe(struct i2c_client *client,
216 const struct i2c_device_id *id)
217{
218 struct t5403_data *data;
219 struct iio_dev *indio_dev;
220 int ret;
221
222 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA |
223 I2C_FUNC_SMBUS_I2C_BLOCK))
224 return -ENODEV;
225
226 ret = i2c_smbus_read_byte_data(client, T5403_SLAVE_ADDR);
227 if (ret < 0)
228 return ret;
229 if ((ret & T5403_I2C_MASK) != T5403_I2C_ADDR)
230 return -ENODEV;
231
232 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
233 if (!indio_dev)
234 return -ENOMEM;
235
236 data = iio_priv(indio_dev);
237 data->client = client;
238 mutex_init(&data->lock);
239
240 i2c_set_clientdata(client, indio_dev);
241 indio_dev->info = &t5403_info;
242 indio_dev->name = id->name;
243 indio_dev->dev.parent = &client->dev;
244 indio_dev->modes = INDIO_DIRECT_MODE;
245 indio_dev->channels = t5403_channels;
246 indio_dev->num_channels = ARRAY_SIZE(t5403_channels);
247
248 data->mode = T5403_MODE_STANDARD;
249
250 ret = i2c_smbus_read_i2c_block_data(data->client, T5403_CALIB_DATA,
251 sizeof(data->c), (u8 *) data->c);
252 if (ret < 0)
253 return ret;
254
255 return devm_iio_device_register(&client->dev, indio_dev);
256}
257
258static const struct i2c_device_id t5403_id[] = {
259 { "t5403", 0 },
260 { }
261};
262MODULE_DEVICE_TABLE(i2c, t5403_id);
263
264static struct i2c_driver t5403_driver = {
265 .driver = {
266 .name = "t5403",
267 },
268 .probe = t5403_probe,
269 .id_table = t5403_id,
270};
271module_i2c_driver(t5403_driver);
272
273MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
274MODULE_DESCRIPTION("EPCOS T5403 pressure/temperature sensor driver");
275MODULE_LICENSE("GPL");
diff --git a/drivers/iio/proximity/as3935.c b/drivers/iio/proximity/as3935.c
index bf677bfe8eb2..5e780ef206f3 100644
--- a/drivers/iio/proximity/as3935.c
+++ b/drivers/iio/proximity/as3935.c
@@ -232,7 +232,7 @@ static void as3935_event_work(struct work_struct *work)
232 232
233 switch (val) { 233 switch (val) {
234 case AS3935_EVENT_INT: 234 case AS3935_EVENT_INT:
235 iio_trigger_poll(st->trig, iio_get_time_ns()); 235 iio_trigger_poll(st->trig);
236 break; 236 break;
237 case AS3935_NOISE_INT: 237 case AS3935_NOISE_INT:
238 dev_warn(&st->spi->dev, "noise level is too high"); 238 dev_warn(&st->spi->dev, "noise level is too high");
diff --git a/drivers/iio/trigger/iio-trig-interrupt.c b/drivers/iio/trigger/iio-trig-interrupt.c
index 02577ec54c6b..7a149a7822bc 100644
--- a/drivers/iio/trigger/iio-trig-interrupt.c
+++ b/drivers/iio/trigger/iio-trig-interrupt.c
@@ -24,8 +24,7 @@ struct iio_interrupt_trigger_info {
24 24
25static irqreturn_t iio_interrupt_trigger_poll(int irq, void *private) 25static irqreturn_t iio_interrupt_trigger_poll(int irq, void *private)
26{ 26{
27 /* Timestamp not currently provided */ 27 iio_trigger_poll(private);
28 iio_trigger_poll(private, 0);
29 return IRQ_HANDLED; 28 return IRQ_HANDLED;
30} 29}
31 30
diff --git a/drivers/iio/trigger/iio-trig-sysfs.c b/drivers/iio/trigger/iio-trig-sysfs.c
index 15e3b850f513..254c7e906127 100644
--- a/drivers/iio/trigger/iio-trig-sysfs.c
+++ b/drivers/iio/trigger/iio-trig-sysfs.c
@@ -96,7 +96,7 @@ static void iio_sysfs_trigger_work(struct irq_work *work)
96 struct iio_sysfs_trig *trig = container_of(work, struct iio_sysfs_trig, 96 struct iio_sysfs_trig *trig = container_of(work, struct iio_sysfs_trig,
97 work); 97 work);
98 98
99 iio_trigger_poll(trig->trig, 0); 99 iio_trigger_poll(trig->trig);
100} 100}
101 101
102static ssize_t iio_sysfs_trigger_poll(struct device *dev, 102static ssize_t iio_sysfs_trigger_poll(struct device *dev,