aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-12-24 13:30:57 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-12-24 13:30:57 -0500
commita6e8e3a470bf96575eebfa9dbfda6e1ec8929fb8 (patch)
treec9416f65eb0de11b9ea7e5fd6c6c2167162a3ae7 /drivers/iio
parent912cbd495204ee6fcb67b8704ffbda10615d1f2f (diff)
parent6fd92d3add999fbcc7178a07cbc6360f66a306ba (diff)
Merge tag 'iio-for-3.14b' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-next
Jonathan writes: 2nd round of new IIO drivers, features and cleanups for the 3.14 cycle. New drivers * HID inclinometer driver. * DHT11 humidity driver. Note that previous humidity drivers have been in hwmon, but no one was ever entirely happy with that, and they should find a more comfortable home in IIO (their original placement in hwmon was my fault - oops). As this is our first humidity driver, core support is also added. New features * Two of mxs-lradc channels are internally wired to a temperature sensor, make this explicit in the driver by providing the relevant temperature channel. * Add support for blocking IO on buffers. * Add a data_available call back to the interface between buffer implementations and the core. This is much cleaner than the old, 'stufftoread' flag. Implemented in the kfifo buffer. Cleanups * Last user of the old event configuration interface is converted and the old interface dropped. Nice to be rid of this thanks to Lars-Peter's hard work! * Replace all remaining instances of the IIO_ST macro with explicit filling of the scan_type structure within struct iio_chan_spec. This macro was a bad idea, that rapidly ceased to cover all elements of the structure. Miss reading of the macro arguements has led to a number of bugs so lets just get rid of it. The final removal patch is awaiting for some fixes to make their way into mainline. In a couple of drivers, no elements of scan_type were even being used so in those case, it has been dropped entirely. * Drop a couple of of_match_ptr helper uses in drivers where devicetree is not optional and hence the structures being protected by this always exist. * Fix up some cases where data was read from a device in a particular byte order, but he code placed it into a s16 or similar. These were highlighted by Sparse. * Use the new ATTRIBUTE_GROUPS macro to drop some boiler plate in the triggers core code. * ad7746 and ad7280a - stop storing buffers on the stack, giving cleaner code and possibly avoiding issues with i2c bus drivers that assume they can dma directly into the buffer. Note that this cannot currently happen as the the i2c_smbus_read_i2c_block_data function has a memcpy from the buffer actually passed to the bus driver. I missed this element of the commit message and don't think it is major enough to rebase the iio tree. * ad5791 and ad5504 stop storing buffers on the stack for an SPI driver. Unlike the i2c drivers, this is a real issue for SPI drivers which can dma directly into the buffer supplied.
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/Kconfig2
-rw-r--r--drivers/iio/Makefile2
-rw-r--r--drivers/iio/accel/bma180.c7
-rw-r--r--drivers/iio/adc/ad7266.c21
-rw-r--r--drivers/iio/adc/max1363.c8
-rw-r--r--drivers/iio/dac/ad5064.c7
-rw-r--r--drivers/iio/dac/ad5360.c7
-rw-r--r--drivers/iio/dac/ad5380.c7
-rw-r--r--drivers/iio/dac/ad5421.c14
-rw-r--r--drivers/iio/dac/ad5446.c9
-rw-r--r--drivers/iio/dac/ad5449.c7
-rw-r--r--drivers/iio/dac/ad5504.c46
-rw-r--r--drivers/iio/dac/ad5624r_spi.c7
-rw-r--r--drivers/iio/dac/ad5686.c11
-rw-r--r--drivers/iio/dac/ad5755.c9
-rw-r--r--drivers/iio/dac/ad5764.c7
-rw-r--r--drivers/iio/dac/ad5791.c55
-rw-r--r--drivers/iio/dac/max517.c1
-rw-r--r--drivers/iio/dac/mcp4725.c1
-rw-r--r--drivers/iio/humidity/Kconfig15
-rw-r--r--drivers/iio/humidity/Makefile5
-rw-r--r--drivers/iio/humidity/dht11.c294
-rw-r--r--drivers/iio/industrialio-buffer.c33
-rw-r--r--drivers/iio/industrialio-core.c1
-rw-r--r--drivers/iio/industrialio-event.c154
-rw-r--r--drivers/iio/industrialio-trigger.c12
-rw-r--r--drivers/iio/kfifo_buf.c23
-rw-r--r--drivers/iio/light/adjd_s311.c7
-rw-r--r--drivers/iio/light/apds9300.c8
-rw-r--r--drivers/iio/light/cm36651.c35
-rw-r--r--drivers/iio/light/gp2ap020a00f.c8
-rw-r--r--drivers/iio/light/tcs3472.c7
-rw-r--r--drivers/iio/light/tsl2563.c8
-rw-r--r--drivers/iio/light/vcnl4000.c2
-rw-r--r--drivers/iio/magnetometer/mag3110.c6
-rw-r--r--drivers/iio/orientation/Kconfig19
-rw-r--r--drivers/iio/orientation/Makefile6
-rw-r--r--drivers/iio/orientation/hid-sensor-incl-3d.c428
38 files changed, 1042 insertions, 257 deletions
diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
index 90cf0cda50c4..5dd0e120a504 100644
--- a/drivers/iio/Kconfig
+++ b/drivers/iio/Kconfig
@@ -65,9 +65,11 @@ source "drivers/iio/common/Kconfig"
65source "drivers/iio/dac/Kconfig" 65source "drivers/iio/dac/Kconfig"
66source "drivers/iio/frequency/Kconfig" 66source "drivers/iio/frequency/Kconfig"
67source "drivers/iio/gyro/Kconfig" 67source "drivers/iio/gyro/Kconfig"
68source "drivers/iio/humidity/Kconfig"
68source "drivers/iio/imu/Kconfig" 69source "drivers/iio/imu/Kconfig"
69source "drivers/iio/light/Kconfig" 70source "drivers/iio/light/Kconfig"
70source "drivers/iio/magnetometer/Kconfig" 71source "drivers/iio/magnetometer/Kconfig"
72source "drivers/iio/orientation/Kconfig"
71if IIO_TRIGGER 73if IIO_TRIGGER
72 source "drivers/iio/trigger/Kconfig" 74 source "drivers/iio/trigger/Kconfig"
73endif #IIO_TRIGGER 75endif #IIO_TRIGGER
diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
index bcf7e9e3b053..887d39090d75 100644
--- a/drivers/iio/Makefile
+++ b/drivers/iio/Makefile
@@ -18,9 +18,11 @@ obj-y += common/
18obj-y += dac/ 18obj-y += dac/
19obj-y += gyro/ 19obj-y += gyro/
20obj-y += frequency/ 20obj-y += frequency/
21obj-y += humidity/
21obj-y += imu/ 22obj-y += imu/
22obj-y += light/ 23obj-y += light/
23obj-y += magnetometer/ 24obj-y += magnetometer/
25obj-y += orientation/
24obj-y += pressure/ 26obj-y += pressure/
25obj-y += temperature/ 27obj-y += temperature/
26obj-y += trigger/ 28obj-y += trigger/
diff --git a/drivers/iio/accel/bma180.c b/drivers/iio/accel/bma180.c
index 28b39283bccf..3bec9220df04 100644
--- a/drivers/iio/accel/bma180.c
+++ b/drivers/iio/accel/bma180.c
@@ -455,7 +455,12 @@ static const struct iio_chan_spec_ext_info bma180_ext_info[] = {
455 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 455 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
456 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 456 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
457 .scan_index = (_index), \ 457 .scan_index = (_index), \
458 .scan_type = IIO_ST('s', 14, 16, 2), \ 458 .scan_type = { \
459 .sign = 's', \
460 .realbits = 14, \
461 .storagebits = 16, \
462 .shift = 2, \
463 }, \
459 .ext_info = bma180_ext_info, \ 464 .ext_info = bma180_ext_info, \
460} 465}
461 466
diff --git a/drivers/iio/adc/ad7266.c b/drivers/iio/adc/ad7266.c
index 58e945594c7b..70f78c3062a7 100644
--- a/drivers/iio/adc/ad7266.c
+++ b/drivers/iio/adc/ad7266.c
@@ -43,19 +43,22 @@ struct ad7266_state {
43 * The buffer needs to be large enough to hold two samples (4 bytes) and 43 * The buffer needs to be large enough to hold two samples (4 bytes) and
44 * the naturally aligned timestamp (8 bytes). 44 * the naturally aligned timestamp (8 bytes).
45 */ 45 */
46 uint8_t data[ALIGN(4, sizeof(s64)) + sizeof(s64)] ____cacheline_aligned; 46 struct {
47 __be16 sample[2];
48 s64 timestamp;
49 } data ____cacheline_aligned;
47}; 50};
48 51
49static int ad7266_wakeup(struct ad7266_state *st) 52static int ad7266_wakeup(struct ad7266_state *st)
50{ 53{
51 /* Any read with >= 2 bytes will wake the device */ 54 /* Any read with >= 2 bytes will wake the device */
52 return spi_read(st->spi, st->data, 2); 55 return spi_read(st->spi, &st->data.sample[0], 2);
53} 56}
54 57
55static int ad7266_powerdown(struct ad7266_state *st) 58static int ad7266_powerdown(struct ad7266_state *st)
56{ 59{
57 /* Any read with < 2 bytes will powerdown the device */ 60 /* Any read with < 2 bytes will powerdown the device */
58 return spi_read(st->spi, st->data, 1); 61 return spi_read(st->spi, &st->data.sample[0], 1);
59} 62}
60 63
61static int ad7266_preenable(struct iio_dev *indio_dev) 64static int ad7266_preenable(struct iio_dev *indio_dev)
@@ -84,9 +87,9 @@ static irqreturn_t ad7266_trigger_handler(int irq, void *p)
84 struct ad7266_state *st = iio_priv(indio_dev); 87 struct ad7266_state *st = iio_priv(indio_dev);
85 int ret; 88 int ret;
86 89
87 ret = spi_read(st->spi, st->data, 4); 90 ret = spi_read(st->spi, st->data.sample, 4);
88 if (ret == 0) { 91 if (ret == 0) {
89 iio_push_to_buffers_with_timestamp(indio_dev, st->data, 92 iio_push_to_buffers_with_timestamp(indio_dev, &st->data,
90 pf->timestamp); 93 pf->timestamp);
91 } 94 }
92 95
@@ -137,7 +140,7 @@ static int ad7266_read_single(struct ad7266_state *st, int *val,
137 ad7266_select_input(st, address); 140 ad7266_select_input(st, address);
138 141
139 ret = spi_sync(st->spi, &st->single_msg); 142 ret = spi_sync(st->spi, &st->single_msg);
140 *val = be16_to_cpu(st->data[address % 2]); 143 *val = be16_to_cpu(st->data.sample[address % 2]);
141 144
142 return ret; 145 return ret;
143} 146}
@@ -442,15 +445,15 @@ static int ad7266_probe(struct spi_device *spi)
442 ad7266_init_channels(indio_dev); 445 ad7266_init_channels(indio_dev);
443 446
444 /* wakeup */ 447 /* wakeup */
445 st->single_xfer[0].rx_buf = &st->data; 448 st->single_xfer[0].rx_buf = &st->data.sample[0];
446 st->single_xfer[0].len = 2; 449 st->single_xfer[0].len = 2;
447 st->single_xfer[0].cs_change = 1; 450 st->single_xfer[0].cs_change = 1;
448 /* conversion */ 451 /* conversion */
449 st->single_xfer[1].rx_buf = &st->data; 452 st->single_xfer[1].rx_buf = st->data.sample;
450 st->single_xfer[1].len = 4; 453 st->single_xfer[1].len = 4;
451 st->single_xfer[1].cs_change = 1; 454 st->single_xfer[1].cs_change = 1;
452 /* powerdown */ 455 /* powerdown */
453 st->single_xfer[2].tx_buf = &st->data; 456 st->single_xfer[2].tx_buf = &st->data.sample[0];
454 st->single_xfer[2].len = 1; 457 st->single_xfer[2].len = 1;
455 458
456 spi_message_init(&st->single_msg); 459 spi_message_init(&st->single_msg);
diff --git a/drivers/iio/adc/max1363.c b/drivers/iio/adc/max1363.c
index 6118dced02b6..e283f2f2ee2f 100644
--- a/drivers/iio/adc/max1363.c
+++ b/drivers/iio/adc/max1363.c
@@ -1039,10 +1039,10 @@ static const struct iio_info max1238_info = {
1039}; 1039};
1040 1040
1041static const struct iio_info max1363_info = { 1041static const struct iio_info max1363_info = {
1042 .read_event_value_new = &max1363_read_thresh, 1042 .read_event_value = &max1363_read_thresh,
1043 .write_event_value_new = &max1363_write_thresh, 1043 .write_event_value = &max1363_write_thresh,
1044 .read_event_config_new = &max1363_read_event_config, 1044 .read_event_config = &max1363_read_event_config,
1045 .write_event_config_new = &max1363_write_event_config, 1045 .write_event_config = &max1363_write_event_config,
1046 .read_raw = &max1363_read_raw, 1046 .read_raw = &max1363_read_raw,
1047 .update_scan_mode = &max1363_update_scan_mode, 1047 .update_scan_mode = &max1363_update_scan_mode,
1048 .driver_module = THIS_MODULE, 1048 .driver_module = THIS_MODULE,
diff --git a/drivers/iio/dac/ad5064.c b/drivers/iio/dac/ad5064.c
index cb9c6366032c..f03b92fd3803 100644
--- a/drivers/iio/dac/ad5064.c
+++ b/drivers/iio/dac/ad5064.c
@@ -299,7 +299,12 @@ static const struct iio_chan_spec_ext_info ad5064_ext_info[] = {
299 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 299 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
300 BIT(IIO_CHAN_INFO_SCALE), \ 300 BIT(IIO_CHAN_INFO_SCALE), \
301 .address = addr, \ 301 .address = addr, \
302 .scan_type = IIO_ST('u', (bits), 16, 20 - (bits)), \ 302 .scan_type = { \
303 .sign = 'u', \
304 .realbits = (bits), \
305 .storagebits = 16, \
306 .shift = 20 - bits, \
307 }, \
303 .ext_info = ad5064_ext_info, \ 308 .ext_info = ad5064_ext_info, \
304} 309}
305 310
diff --git a/drivers/iio/dac/ad5360.c b/drivers/iio/dac/ad5360.c
index b968af50db0a..64634d7f578e 100644
--- a/drivers/iio/dac/ad5360.c
+++ b/drivers/iio/dac/ad5360.c
@@ -107,7 +107,12 @@ enum ad5360_type {
107 BIT(IIO_CHAN_INFO_OFFSET) | \ 107 BIT(IIO_CHAN_INFO_OFFSET) | \
108 BIT(IIO_CHAN_INFO_CALIBSCALE) | \ 108 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
109 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 109 BIT(IIO_CHAN_INFO_CALIBBIAS), \
110 .scan_type = IIO_ST('u', (bits), 16, 16 - (bits)) \ 110 .scan_type = { \
111 .sign = 'u', \
112 .realbits = (bits), \
113 .storagebits = 16, \
114 .shift = 16 - (bits), \
115 }, \
111} 116}
112 117
113static const struct ad5360_chip_info ad5360_chip_info_tbl[] = { 118static const struct ad5360_chip_info ad5360_chip_info_tbl[] = {
diff --git a/drivers/iio/dac/ad5380.c b/drivers/iio/dac/ad5380.c
index a59ff0e7b888..9de4c4d38280 100644
--- a/drivers/iio/dac/ad5380.c
+++ b/drivers/iio/dac/ad5380.c
@@ -261,7 +261,12 @@ static struct iio_chan_spec_ext_info ad5380_ext_info[] = {
261 BIT(IIO_CHAN_INFO_CALIBSCALE) | \ 261 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
262 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 262 BIT(IIO_CHAN_INFO_CALIBBIAS), \
263 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 263 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
264 .scan_type = IIO_ST('u', (_bits), 16, 14 - (_bits)), \ 264 .scan_type = { \
265 .sign = 'u', \
266 .realbits = (_bits), \
267 .storagebits = 16, \
268 .shift = 14 - (_bits), \
269 }, \
265 .ext_info = ad5380_ext_info, \ 270 .ext_info = ad5380_ext_info, \
266} 271}
267 272
diff --git a/drivers/iio/dac/ad5421.c b/drivers/iio/dac/ad5421.c
index 7d1e90811c71..787ef1d859c6 100644
--- a/drivers/iio/dac/ad5421.c
+++ b/drivers/iio/dac/ad5421.c
@@ -75,7 +75,7 @@ struct ad5421_state {
75 * transfer buffers to live in their own cache lines. 75 * transfer buffers to live in their own cache lines.
76 */ 76 */
77 union { 77 union {
78 u32 d32; 78 __be32 d32;
79 u8 d8[4]; 79 u8 d8[4];
80 } data[2] ____cacheline_aligned; 80 } data[2] ____cacheline_aligned;
81}; 81};
@@ -114,7 +114,11 @@ static const struct iio_chan_spec ad5421_channels[] = {
114 BIT(IIO_CHAN_INFO_CALIBBIAS), 114 BIT(IIO_CHAN_INFO_CALIBBIAS),
115 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | 115 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
116 BIT(IIO_CHAN_INFO_OFFSET), 116 BIT(IIO_CHAN_INFO_OFFSET),
117 .scan_type = IIO_ST('u', 16, 16, 0), 117 .scan_type = {
118 .sign = 'u',
119 .realbits = 16,
120 .storagebits = 16,
121 },
118 .event_spec = ad5421_current_event, 122 .event_spec = ad5421_current_event,
119 .num_event_specs = ARRAY_SIZE(ad5421_current_event), 123 .num_event_specs = ARRAY_SIZE(ad5421_current_event),
120 }, 124 },
@@ -458,9 +462,9 @@ static int ad5421_read_event_value(struct iio_dev *indio_dev,
458static const struct iio_info ad5421_info = { 462static const struct iio_info ad5421_info = {
459 .read_raw = ad5421_read_raw, 463 .read_raw = ad5421_read_raw,
460 .write_raw = ad5421_write_raw, 464 .write_raw = ad5421_write_raw,
461 .read_event_config_new = ad5421_read_event_config, 465 .read_event_config = ad5421_read_event_config,
462 .write_event_config_new = ad5421_write_event_config, 466 .write_event_config = ad5421_write_event_config,
463 .read_event_value_new = ad5421_read_event_value, 467 .read_event_value = ad5421_read_event_value,
464 .driver_module = THIS_MODULE, 468 .driver_module = THIS_MODULE,
465}; 469};
466 470
diff --git a/drivers/iio/dac/ad5446.c b/drivers/iio/dac/ad5446.c
index 1263b0e5ad84..46bb62a5c1d4 100644
--- a/drivers/iio/dac/ad5446.c
+++ b/drivers/iio/dac/ad5446.c
@@ -139,14 +139,19 @@ static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown[] = {
139 { }, 139 { },
140}; 140};
141 141
142#define _AD5446_CHANNEL(bits, storage, shift, ext) { \ 142#define _AD5446_CHANNEL(bits, storage, _shift, ext) { \
143 .type = IIO_VOLTAGE, \ 143 .type = IIO_VOLTAGE, \
144 .indexed = 1, \ 144 .indexed = 1, \
145 .output = 1, \ 145 .output = 1, \
146 .channel = 0, \ 146 .channel = 0, \
147 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 147 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
148 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 148 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
149 .scan_type = IIO_ST('u', (bits), (storage), (shift)), \ 149 .scan_type = { \
150 .sign = 'u', \
151 .realbits = (bits), \
152 .storagebits = (storage), \
153 .shift = (_shift), \
154 }, \
150 .ext_info = (ext), \ 155 .ext_info = (ext), \
151} 156}
152 157
diff --git a/drivers/iio/dac/ad5449.c b/drivers/iio/dac/ad5449.c
index 82e208f6cde2..64d7256cbb6d 100644
--- a/drivers/iio/dac/ad5449.c
+++ b/drivers/iio/dac/ad5449.c
@@ -204,7 +204,12 @@ static const struct iio_info ad5449_info = {
204 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 204 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
205 BIT(IIO_CHAN_INFO_SCALE), \ 205 BIT(IIO_CHAN_INFO_SCALE), \
206 .address = (chan), \ 206 .address = (chan), \
207 .scan_type = IIO_ST('u', (bits), 16, 12 - (bits)), \ 207 .scan_type = { \
208 .sign = 'u', \
209 .realbits = (bits), \
210 .storagebits = 16, \
211 .shift = 12 - (bits), \
212 }, \
208} 213}
209 214
210#define DECLARE_AD5449_CHANNELS(name, bits) \ 215#define DECLARE_AD5449_CHANNELS(name, bits) \
diff --git a/drivers/iio/dac/ad5504.c b/drivers/iio/dac/ad5504.c
index c0957a918e17..1e6449346b50 100644
--- a/drivers/iio/dac/ad5504.c
+++ b/drivers/iio/dac/ad5504.c
@@ -47,14 +47,16 @@
47 * @vref_mv: actual reference voltage used 47 * @vref_mv: actual reference voltage used
48 * @pwr_down_mask power down mask 48 * @pwr_down_mask power down mask
49 * @pwr_down_mode current power down mode 49 * @pwr_down_mode current power down mode
50 * @data: transfer buffer
50 */ 51 */
51
52struct ad5504_state { 52struct ad5504_state {
53 struct spi_device *spi; 53 struct spi_device *spi;
54 struct regulator *reg; 54 struct regulator *reg;
55 unsigned short vref_mv; 55 unsigned short vref_mv;
56 unsigned pwr_down_mask; 56 unsigned pwr_down_mask;
57 unsigned pwr_down_mode; 57 unsigned pwr_down_mode;
58
59 __be16 data[2] ____cacheline_aligned;
58}; 60};
59 61
60/** 62/**
@@ -66,31 +68,29 @@ enum ad5504_supported_device_ids {
66 ID_AD5501, 68 ID_AD5501,
67}; 69};
68 70
69static int ad5504_spi_write(struct spi_device *spi, u8 addr, u16 val) 71static int ad5504_spi_write(struct ad5504_state *st, u8 addr, u16 val)
70{ 72{
71 u16 tmp = cpu_to_be16(AD5504_CMD_WRITE | 73 st->data[0] = cpu_to_be16(AD5504_CMD_WRITE | AD5504_ADDR(addr) |
72 AD5504_ADDR(addr) |
73 (val & AD5504_RES_MASK)); 74 (val & AD5504_RES_MASK));
74 75
75 return spi_write(spi, (u8 *)&tmp, 2); 76 return spi_write(st->spi, &st->data[0], 2);
76} 77}
77 78
78static int ad5504_spi_read(struct spi_device *spi, u8 addr) 79static int ad5504_spi_read(struct ad5504_state *st, u8 addr)
79{ 80{
80 u16 tmp = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr));
81 u16 val;
82 int ret; 81 int ret;
83 struct spi_transfer t = { 82 struct spi_transfer t = {
84 .tx_buf = &tmp, 83 .tx_buf = &st->data[0],
85 .rx_buf = &val, 84 .rx_buf = &st->data[1],
86 .len = 2, 85 .len = 2,
87 }; 86 };
88 ret = spi_sync_transfer(spi, &t, 1); 87
89 88 st->data[0] = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr));
89 ret = spi_sync_transfer(st->spi, &t, 1);
90 if (ret < 0) 90 if (ret < 0)
91 return ret; 91 return ret;
92 92
93 return be16_to_cpu(val) & AD5504_RES_MASK; 93 return be16_to_cpu(st->data[1]) & AD5504_RES_MASK;
94} 94}
95 95
96static int ad5504_read_raw(struct iio_dev *indio_dev, 96static int ad5504_read_raw(struct iio_dev *indio_dev,
@@ -104,7 +104,7 @@ static int ad5504_read_raw(struct iio_dev *indio_dev,
104 104
105 switch (m) { 105 switch (m) {
106 case IIO_CHAN_INFO_RAW: 106 case IIO_CHAN_INFO_RAW:
107 ret = ad5504_spi_read(st->spi, chan->address); 107 ret = ad5504_spi_read(st, chan->address);
108 if (ret < 0) 108 if (ret < 0)
109 return ret; 109 return ret;
110 110
@@ -133,7 +133,7 @@ static int ad5504_write_raw(struct iio_dev *indio_dev,
133 if (val >= (1 << chan->scan_type.realbits) || val < 0) 133 if (val >= (1 << chan->scan_type.realbits) || val < 0)
134 return -EINVAL; 134 return -EINVAL;
135 135
136 return ad5504_spi_write(st->spi, chan->address, val); 136 return ad5504_spi_write(st, chan->address, val);
137 default: 137 default:
138 ret = -EINVAL; 138 ret = -EINVAL;
139 } 139 }
@@ -197,12 +197,12 @@ static ssize_t ad5504_write_dac_powerdown(struct iio_dev *indio_dev,
197 else 197 else
198 st->pwr_down_mask &= ~(1 << chan->channel); 198 st->pwr_down_mask &= ~(1 << chan->channel);
199 199
200 ret = ad5504_spi_write(st->spi, AD5504_ADDR_CTRL, 200 ret = ad5504_spi_write(st, AD5504_ADDR_CTRL,
201 AD5504_DAC_PWRDWN_MODE(st->pwr_down_mode) | 201 AD5504_DAC_PWRDWN_MODE(st->pwr_down_mode) |
202 AD5504_DAC_PWR(st->pwr_down_mask)); 202 AD5504_DAC_PWR(st->pwr_down_mask));
203 203
204 /* writes to the CTRL register must be followed by a NOOP */ 204 /* writes to the CTRL register must be followed by a NOOP */
205 ad5504_spi_write(st->spi, AD5504_ADDR_NOOP, 0); 205 ad5504_spi_write(st, AD5504_ADDR_NOOP, 0);
206 206
207 return ret ? ret : len; 207 return ret ? ret : len;
208} 208}
@@ -261,7 +261,11 @@ static const struct iio_chan_spec_ext_info ad5504_ext_info[] = {
261 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 261 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
262 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 262 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
263 .address = AD5504_ADDR_DAC(_chan), \ 263 .address = AD5504_ADDR_DAC(_chan), \
264 .scan_type = IIO_ST('u', 12, 16, 0), \ 264 .scan_type = { \
265 .sign = 'u', \
266 .realbits = 12, \
267 .storagebits = 16, \
268 }, \
265 .ext_info = ad5504_ext_info, \ 269 .ext_info = ad5504_ext_info, \
266} 270}
267 271
diff --git a/drivers/iio/dac/ad5624r_spi.c b/drivers/iio/dac/ad5624r_spi.c
index 774dd968145b..e8199cce2aea 100644
--- a/drivers/iio/dac/ad5624r_spi.c
+++ b/drivers/iio/dac/ad5624r_spi.c
@@ -176,7 +176,12 @@ static const struct iio_chan_spec_ext_info ad5624r_ext_info[] = {
176 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 176 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
177 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 177 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
178 .address = (_chan), \ 178 .address = (_chan), \
179 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)), \ 179 .scan_type = { \
180 .sign = 'u', \
181 .realbits = (_bits), \
182 .storagebits = 16, \
183 .shift = 16 - (_bits), \
184 }, \
180 .ext_info = ad5624r_ext_info, \ 185 .ext_info = ad5624r_ext_info, \
181} 186}
182 187
diff --git a/drivers/iio/dac/ad5686.c b/drivers/iio/dac/ad5686.c
index 30e506e37dd2..17aca4d9bd06 100644
--- a/drivers/iio/dac/ad5686.c
+++ b/drivers/iio/dac/ad5686.c
@@ -78,7 +78,7 @@ struct ad5686_state {
78 */ 78 */
79 79
80 union { 80 union {
81 u32 d32; 81 __be32 d32;
82 u8 d8[4]; 82 u8 d8[4];
83 } data[3] ____cacheline_aligned; 83 } data[3] ____cacheline_aligned;
84}; 84};
@@ -267,7 +267,7 @@ static const struct iio_chan_spec_ext_info ad5686_ext_info[] = {
267 { }, 267 { },
268}; 268};
269 269
270#define AD5868_CHANNEL(chan, bits, shift) { \ 270#define AD5868_CHANNEL(chan, bits, _shift) { \
271 .type = IIO_VOLTAGE, \ 271 .type = IIO_VOLTAGE, \
272 .indexed = 1, \ 272 .indexed = 1, \
273 .output = 1, \ 273 .output = 1, \
@@ -275,7 +275,12 @@ static const struct iio_chan_spec_ext_info ad5686_ext_info[] = {
275 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 275 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
276 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\ 276 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\
277 .address = AD5686_ADDR_DAC(chan), \ 277 .address = AD5686_ADDR_DAC(chan), \
278 .scan_type = IIO_ST('u', bits, 16, shift), \ 278 .scan_type = { \
279 .sign = 'u', \
280 .realbits = (bits), \
281 .storagebits = 16, \
282 .shift = (_shift), \
283 }, \
279 .ext_info = ad5686_ext_info, \ 284 .ext_info = ad5686_ext_info, \
280} 285}
281 286
diff --git a/drivers/iio/dac/ad5755.c b/drivers/iio/dac/ad5755.c
index ee1e95a3a0c3..a7c851f62d7c 100644
--- a/drivers/iio/dac/ad5755.c
+++ b/drivers/iio/dac/ad5755.c
@@ -97,7 +97,7 @@ struct ad5755_state {
97 */ 97 */
98 98
99 union { 99 union {
100 u32 d32; 100 __be32 d32;
101 u8 d8[4]; 101 u8 d8[4];
102 } data[2] ____cacheline_aligned; 102 } data[2] ____cacheline_aligned;
103}; 103};
@@ -392,7 +392,12 @@ static const struct iio_chan_spec_ext_info ad5755_ext_info[] = {
392 BIT(IIO_CHAN_INFO_OFFSET) | \ 392 BIT(IIO_CHAN_INFO_OFFSET) | \
393 BIT(IIO_CHAN_INFO_CALIBSCALE) | \ 393 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
394 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 394 BIT(IIO_CHAN_INFO_CALIBBIAS), \
395 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)), \ 395 .scan_type = { \
396 .sign = 'u', \
397 .realbits = (_bits), \
398 .storagebits = 16, \
399 .shift = 16 - (_bits), \
400 }, \
396 .ext_info = ad5755_ext_info, \ 401 .ext_info = ad5755_ext_info, \
397} 402}
398 403
diff --git a/drivers/iio/dac/ad5764.c b/drivers/iio/dac/ad5764.c
index a8ff5b2ed13e..d0d38165339d 100644
--- a/drivers/iio/dac/ad5764.c
+++ b/drivers/iio/dac/ad5764.c
@@ -83,7 +83,12 @@ enum ad5764_type {
83 BIT(IIO_CHAN_INFO_CALIBSCALE) | \ 83 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
84 BIT(IIO_CHAN_INFO_CALIBBIAS), \ 84 BIT(IIO_CHAN_INFO_CALIBBIAS), \
85 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET), \ 85 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET), \
86 .scan_type = IIO_ST('u', (_bits), 16, 16 - (_bits)) \ 86 .scan_type = { \
87 .sign = 'u', \
88 .realbits = (_bits), \
89 .storagebits = 16, \
90 .shift = 16 - (_bits), \
91 }, \
87} 92}
88 93
89#define DECLARE_AD5764_CHANNELS(_name, _bits) \ 94#define DECLARE_AD5764_CHANNELS(_name, _bits) \
diff --git a/drivers/iio/dac/ad5791.c b/drivers/iio/dac/ad5791.c
index d64acbd89482..ae49afe2b380 100644
--- a/drivers/iio/dac/ad5791.c
+++ b/drivers/iio/dac/ad5791.c
@@ -91,6 +91,11 @@ struct ad5791_state {
91 unsigned ctrl; 91 unsigned ctrl;
92 unsigned pwr_down_mode; 92 unsigned pwr_down_mode;
93 bool pwr_down; 93 bool pwr_down;
94
95 union {
96 __be32 d32;
97 u8 d8[4];
98 } data[3] ____cacheline_aligned;
94}; 99};
95 100
96/** 101/**
@@ -104,48 +109,39 @@ enum ad5791_supported_device_ids {
104 ID_AD5791, 109 ID_AD5791,
105}; 110};
106 111
107static int ad5791_spi_write(struct spi_device *spi, u8 addr, u32 val) 112static int ad5791_spi_write(struct ad5791_state *st, u8 addr, u32 val)
108{ 113{
109 union { 114 st->data[0].d32 = cpu_to_be32(AD5791_CMD_WRITE |
110 u32 d32;
111 u8 d8[4];
112 } data;
113
114 data.d32 = cpu_to_be32(AD5791_CMD_WRITE |
115 AD5791_ADDR(addr) | 115 AD5791_ADDR(addr) |
116 (val & AD5791_DAC_MASK)); 116 (val & AD5791_DAC_MASK));
117 117
118 return spi_write(spi, &data.d8[1], 3); 118 return spi_write(st->spi, &st->data[0].d8[1], 3);
119} 119}
120 120
121static int ad5791_spi_read(struct spi_device *spi, u8 addr, u32 *val) 121static int ad5791_spi_read(struct ad5791_state *st, u8 addr, u32 *val)
122{ 122{
123 union {
124 u32 d32;
125 u8 d8[4];
126 } data[3];
127 int ret; 123 int ret;
128 struct spi_transfer xfers[] = { 124 struct spi_transfer xfers[] = {
129 { 125 {
130 .tx_buf = &data[0].d8[1], 126 .tx_buf = &st->data[0].d8[1],
131 .bits_per_word = 8, 127 .bits_per_word = 8,
132 .len = 3, 128 .len = 3,
133 .cs_change = 1, 129 .cs_change = 1,
134 }, { 130 }, {
135 .tx_buf = &data[1].d8[1], 131 .tx_buf = &st->data[1].d8[1],
136 .rx_buf = &data[2].d8[1], 132 .rx_buf = &st->data[2].d8[1],
137 .bits_per_word = 8, 133 .bits_per_word = 8,
138 .len = 3, 134 .len = 3,
139 }, 135 },
140 }; 136 };
141 137
142 data[0].d32 = cpu_to_be32(AD5791_CMD_READ | 138 st->data[0].d32 = cpu_to_be32(AD5791_CMD_READ |
143 AD5791_ADDR(addr)); 139 AD5791_ADDR(addr));
144 data[1].d32 = cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP)); 140 st->data[1].d32 = cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP));
145 141
146 ret = spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers)); 142 ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
147 143
148 *val = be32_to_cpu(data[2].d32); 144 *val = be32_to_cpu(st->data[2].d32);
149 145
150 return ret; 146 return ret;
151} 147}
@@ -210,7 +206,7 @@ static ssize_t ad5791_write_dac_powerdown(struct iio_dev *indio_dev,
210 } 206 }
211 st->pwr_down = pwr_down; 207 st->pwr_down = pwr_down;
212 208
213 ret = ad5791_spi_write(st->spi, AD5791_ADDR_CTRL, st->ctrl); 209 ret = ad5791_spi_write(st, AD5791_ADDR_CTRL, st->ctrl);
214 210
215 return ret ? ret : len; 211 return ret ? ret : len;
216} 212}
@@ -263,7 +259,7 @@ static int ad5791_read_raw(struct iio_dev *indio_dev,
263 259
264 switch (m) { 260 switch (m) {
265 case IIO_CHAN_INFO_RAW: 261 case IIO_CHAN_INFO_RAW:
266 ret = ad5791_spi_read(st->spi, chan->address, val); 262 ret = ad5791_spi_read(st, chan->address, val);
267 if (ret) 263 if (ret)
268 return ret; 264 return ret;
269 *val &= AD5791_DAC_MASK; 265 *val &= AD5791_DAC_MASK;
@@ -297,7 +293,7 @@ static const struct iio_chan_spec_ext_info ad5791_ext_info[] = {
297 { }, 293 { },
298}; 294};
299 295
300#define AD5791_CHAN(bits, shift) { \ 296#define AD5791_CHAN(bits, _shift) { \
301 .type = IIO_VOLTAGE, \ 297 .type = IIO_VOLTAGE, \
302 .output = 1, \ 298 .output = 1, \
303 .indexed = 1, \ 299 .indexed = 1, \
@@ -306,7 +302,12 @@ static const struct iio_chan_spec_ext_info ad5791_ext_info[] = {
306 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 302 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
307 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 303 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
308 BIT(IIO_CHAN_INFO_OFFSET), \ 304 BIT(IIO_CHAN_INFO_OFFSET), \
309 .scan_type = IIO_ST('u', bits, 24, shift), \ 305 .scan_type = { \
306 .sign = 'u', \
307 .realbits = (bits), \
308 .storagebits = 24, \
309 .shift = (_shift), \
310 }, \
310 .ext_info = ad5791_ext_info, \ 311 .ext_info = ad5791_ext_info, \
311} 312}
312 313
@@ -330,7 +331,7 @@ static int ad5791_write_raw(struct iio_dev *indio_dev,
330 val &= AD5791_RES_MASK(chan->scan_type.realbits); 331 val &= AD5791_RES_MASK(chan->scan_type.realbits);
331 val <<= chan->scan_type.shift; 332 val <<= chan->scan_type.shift;
332 333
333 return ad5791_spi_write(st->spi, chan->address, val); 334 return ad5791_spi_write(st, chan->address, val);
334 335
335 default: 336 default:
336 return -EINVAL; 337 return -EINVAL;
@@ -393,7 +394,7 @@ static int ad5791_probe(struct spi_device *spi)
393 dev_warn(&spi->dev, "reference voltage unspecified\n"); 394 dev_warn(&spi->dev, "reference voltage unspecified\n");
394 } 395 }
395 396
396 ret = ad5791_spi_write(spi, AD5791_ADDR_SW_CTRL, AD5791_SWCTRL_RESET); 397 ret = ad5791_spi_write(st, AD5791_ADDR_SW_CTRL, AD5791_SWCTRL_RESET);
397 if (ret) 398 if (ret)
398 goto error_disable_reg_neg; 399 goto error_disable_reg_neg;
399 400
@@ -405,7 +406,7 @@ static int ad5791_probe(struct spi_device *spi)
405 | ((pdata && pdata->use_rbuf_gain2) ? 0 : AD5791_CTRL_RBUF) | 406 | ((pdata && pdata->use_rbuf_gain2) ? 0 : AD5791_CTRL_RBUF) |
406 AD5791_CTRL_BIN2SC; 407 AD5791_CTRL_BIN2SC;
407 408
408 ret = ad5791_spi_write(spi, AD5791_ADDR_CTRL, st->ctrl | 409 ret = ad5791_spi_write(st, AD5791_ADDR_CTRL, st->ctrl |
409 AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI); 410 AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
410 if (ret) 411 if (ret)
411 goto error_disable_reg_neg; 412 goto error_disable_reg_neg;
diff --git a/drivers/iio/dac/max517.c b/drivers/iio/dac/max517.c
index 6e1903537950..de76e6a34c1e 100644
--- a/drivers/iio/dac/max517.c
+++ b/drivers/iio/dac/max517.c
@@ -146,7 +146,6 @@ static const struct iio_info max517_info = {
146 .channel = (chan), \ 146 .channel = (chan), \
147 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 147 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
148 BIT(IIO_CHAN_INFO_SCALE), \ 148 BIT(IIO_CHAN_INFO_SCALE), \
149 .scan_type = IIO_ST('u', 8, 8, 0), \
150} 149}
151 150
152static const struct iio_chan_spec max517_channels[] = { 151static const struct iio_chan_spec max517_channels[] = {
diff --git a/drivers/iio/dac/mcp4725.c b/drivers/iio/dac/mcp4725.c
index 9f57ae84ab89..7d9f5c31d2fc 100644
--- a/drivers/iio/dac/mcp4725.c
+++ b/drivers/iio/dac/mcp4725.c
@@ -209,7 +209,6 @@ static const struct iio_chan_spec mcp4725_channel = {
209 .channel = 0, 209 .channel = 0,
210 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 210 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
211 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 211 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
212 .scan_type = IIO_ST('u', 12, 16, 0),
213 .ext_info = mcp4725_ext_info, 212 .ext_info = mcp4725_ext_info,
214}; 213};
215 214
diff --git a/drivers/iio/humidity/Kconfig b/drivers/iio/humidity/Kconfig
new file mode 100644
index 000000000000..463c4d9da79e
--- /dev/null
+++ b/drivers/iio/humidity/Kconfig
@@ -0,0 +1,15 @@
1#
2# humidity sensor drivers
3#
4menu "Humidity sensors"
5
6config DHT11
7 tristate "DHT11 (and compatible sensors) driver"
8 depends on GPIOLIB
9 help
10 This driver supports reading data via a single interrupt
11 generating GPIO line. Currently tested are DHT11 and DHT22.
12 Other sensors should work as well as long as they speak the
13 same protocol.
14
15endmenu
diff --git a/drivers/iio/humidity/Makefile b/drivers/iio/humidity/Makefile
new file mode 100644
index 000000000000..d5d36c0c95f9
--- /dev/null
+++ b/drivers/iio/humidity/Makefile
@@ -0,0 +1,5 @@
1#
2# Makefile for IIO humidity sensor drivers
3#
4
5obj-$(CONFIG_DHT11) += dht11.o
diff --git a/drivers/iio/humidity/dht11.c b/drivers/iio/humidity/dht11.c
new file mode 100644
index 000000000000..d8771f546bf2
--- /dev/null
+++ b/drivers/iio/humidity/dht11.c
@@ -0,0 +1,294 @@
1/*
2 * DHT11/DHT22 bit banging GPIO driver
3 *
4 * Copyright (c) Harald Geyer <harald@ccbib.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/err.h>
18#include <linux/interrupt.h>
19#include <linux/device.h>
20#include <linux/kernel.h>
21#include <linux/printk.h>
22#include <linux/slab.h>
23#include <linux/of.h>
24#include <linux/of_device.h>
25#include <linux/sysfs.h>
26#include <linux/io.h>
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/wait.h>
30#include <linux/bitops.h>
31#include <linux/completion.h>
32#include <linux/delay.h>
33#include <linux/gpio.h>
34#include <linux/of_gpio.h>
35
36#include <linux/iio/iio.h>
37
38#define DRIVER_NAME "dht11"
39
40#define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
41
42#define DHT11_EDGES_PREAMBLE 4
43#define DHT11_BITS_PER_READ 40
44#define DHT11_EDGES_PER_READ (2*DHT11_BITS_PER_READ + DHT11_EDGES_PREAMBLE + 1)
45
46/* Data transmission timing (nano seconds) */
47#define DHT11_START_TRANSMISSION 18 /* ms */
48#define DHT11_SENSOR_RESPONSE 80000
49#define DHT11_START_BIT 50000
50#define DHT11_DATA_BIT_LOW 27000
51#define DHT11_DATA_BIT_HIGH 70000
52
53struct dht11 {
54 struct device *dev;
55
56 int gpio;
57 int irq;
58
59 struct completion completion;
60
61 s64 timestamp;
62 int temperature;
63 int humidity;
64
65 /* num_edges: -1 means "no transmission in progress" */
66 int num_edges;
67 struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
68};
69
70static unsigned char dht11_decode_byte(int *timing, int threshold)
71{
72 unsigned char ret = 0;
73 int i;
74
75 for (i = 0; i < 8; ++i) {
76 ret <<= 1;
77 if (timing[i] >= threshold)
78 ++ret;
79 }
80
81 return ret;
82}
83
84static int dht11_decode(struct dht11 *dht11, int offset)
85{
86 int i, t, timing[DHT11_BITS_PER_READ], threshold,
87 timeres = DHT11_SENSOR_RESPONSE;
88 unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
89
90 /* Calculate timestamp resolution */
91 for (i = 0; i < dht11->num_edges; ++i) {
92 t = dht11->edges[i].ts - dht11->edges[i-1].ts;
93 if (t > 0 && t < timeres)
94 timeres = t;
95 }
96 if (2*timeres > DHT11_DATA_BIT_HIGH) {
97 pr_err("dht11: timeresolution %d too bad for decoding\n",
98 timeres);
99 return -EIO;
100 }
101 threshold = DHT11_DATA_BIT_HIGH / timeres;
102 if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
103 pr_err("dht11: WARNING: decoding ambiguous\n");
104
105 /* scale down with timeres and check validity */
106 for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
107 t = dht11->edges[offset + 2*i + 2].ts -
108 dht11->edges[offset + 2*i + 1].ts;
109 if (!dht11->edges[offset + 2*i + 1].value)
110 return -EIO; /* lost synchronisation */
111 timing[i] = t / timeres;
112 }
113
114 hum_int = dht11_decode_byte(timing, threshold);
115 hum_dec = dht11_decode_byte(&timing[8], threshold);
116 temp_int = dht11_decode_byte(&timing[16], threshold);
117 temp_dec = dht11_decode_byte(&timing[24], threshold);
118 checksum = dht11_decode_byte(&timing[32], threshold);
119
120 if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
121 return -EIO;
122
123 dht11->timestamp = iio_get_time_ns();
124 if (hum_int < 20) { /* DHT22 */
125 dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
126 ((temp_int & 0x80) ? -100 : 100);
127 dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
128 } else if (temp_dec == 0 && hum_dec == 0) { /* DHT11 */
129 dht11->temperature = temp_int * 1000;
130 dht11->humidity = hum_int * 1000;
131 } else {
132 dev_err(dht11->dev,
133 "Don't know how to decode data: %d %d %d %d\n",
134 hum_int, hum_dec, temp_int, temp_dec);
135 return -EIO;
136 }
137
138 return 0;
139}
140
141static int dht11_read_raw(struct iio_dev *iio_dev,
142 const struct iio_chan_spec *chan,
143 int *val, int *val2, long m)
144{
145 struct dht11 *dht11 = iio_priv(iio_dev);
146 int ret;
147
148 if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
149 reinit_completion(&dht11->completion);
150
151 dht11->num_edges = 0;
152 ret = gpio_direction_output(dht11->gpio, 0);
153 if (ret)
154 goto err;
155 msleep(DHT11_START_TRANSMISSION);
156 ret = gpio_direction_input(dht11->gpio);
157 if (ret)
158 goto err;
159
160 ret = wait_for_completion_killable_timeout(&dht11->completion,
161 HZ);
162 if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
163 dev_err(&iio_dev->dev,
164 "Only %d signal edges detected\n",
165 dht11->num_edges);
166 ret = -ETIMEDOUT;
167 }
168 if (ret < 0)
169 goto err;
170
171 ret = dht11_decode(dht11,
172 dht11->num_edges == DHT11_EDGES_PER_READ ?
173 DHT11_EDGES_PREAMBLE :
174 DHT11_EDGES_PREAMBLE - 2);
175 if (ret)
176 goto err;
177 }
178
179 ret = IIO_VAL_INT;
180 if (chan->type == IIO_TEMP)
181 *val = dht11->temperature;
182 else if (chan->type == IIO_HUMIDITYRELATIVE)
183 *val = dht11->humidity;
184 else
185 ret = -EINVAL;
186err:
187 dht11->num_edges = -1;
188 return ret;
189}
190
191static const struct iio_info dht11_iio_info = {
192 .driver_module = THIS_MODULE,
193 .read_raw = dht11_read_raw,
194};
195
196/*
197 * IRQ handler called on GPIO edges
198*/
199static irqreturn_t dht11_handle_irq(int irq, void *data)
200{
201 struct iio_dev *iio = data;
202 struct dht11 *dht11 = iio_priv(iio);
203
204 /* TODO: Consider making the handler safe for IRQ sharing */
205 if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
206 dht11->edges[dht11->num_edges].ts = iio_get_time_ns();
207 dht11->edges[dht11->num_edges++].value =
208 gpio_get_value(dht11->gpio);
209
210 if (dht11->num_edges >= DHT11_EDGES_PER_READ)
211 complete(&dht11->completion);
212 }
213
214 return IRQ_HANDLED;
215}
216
217static const struct iio_chan_spec dht11_chan_spec[] = {
218 { .type = IIO_TEMP,
219 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), },
220 { .type = IIO_HUMIDITYRELATIVE,
221 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), }
222};
223
224static const struct of_device_id dht11_dt_ids[] = {
225 { .compatible = "dht11", },
226 { }
227};
228MODULE_DEVICE_TABLE(of, dht11_dt_ids);
229
230static int dht11_probe(struct platform_device *pdev)
231{
232 struct device *dev = &pdev->dev;
233 struct device_node *node = dev->of_node;
234 struct dht11 *dht11;
235 struct iio_dev *iio;
236 int ret;
237
238 iio = devm_iio_device_alloc(dev, sizeof(*dht11));
239 if (!iio) {
240 dev_err(dev, "Failed to allocate IIO device\n");
241 return -ENOMEM;
242 }
243
244 dht11 = iio_priv(iio);
245 dht11->dev = dev;
246
247 dht11->gpio = ret = of_get_gpio(node, 0);
248 if (ret < 0)
249 return ret;
250 ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
251 if (ret)
252 return ret;
253
254 dht11->irq = gpio_to_irq(dht11->gpio);
255 if (dht11->irq < 0) {
256 dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
257 return -EINVAL;
258 }
259 ret = devm_request_irq(dev, dht11->irq, dht11_handle_irq,
260 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
261 pdev->name, iio);
262 if (ret)
263 return ret;
264
265 dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
266 dht11->num_edges = -1;
267
268 platform_set_drvdata(pdev, iio);
269
270 init_completion(&dht11->completion);
271 iio->name = pdev->name;
272 iio->dev.parent = &pdev->dev;
273 iio->info = &dht11_iio_info;
274 iio->modes = INDIO_DIRECT_MODE;
275 iio->channels = dht11_chan_spec;
276 iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
277
278 return devm_iio_device_register(dev, iio);
279}
280
281static struct platform_driver dht11_driver = {
282 .driver = {
283 .name = DRIVER_NAME,
284 .owner = THIS_MODULE,
285 .of_match_table = dht11_dt_ids,
286 },
287 .probe = dht11_probe,
288};
289
290module_platform_driver(dht11_driver);
291
292MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
293MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
294MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 7f9152c3c4d3..c67d83bdc8f0 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -37,6 +37,14 @@ static bool iio_buffer_is_active(struct iio_buffer *buf)
37 return !list_empty(&buf->buffer_list); 37 return !list_empty(&buf->buffer_list);
38} 38}
39 39
40static bool iio_buffer_data_available(struct iio_buffer *buf)
41{
42 if (buf->access->data_available)
43 return buf->access->data_available(buf);
44
45 return buf->stufftoread;
46}
47
40/** 48/**
41 * iio_buffer_read_first_n_outer() - chrdev read for buffer access 49 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
42 * 50 *
@@ -48,13 +56,34 @@ ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
48{ 56{
49 struct iio_dev *indio_dev = filp->private_data; 57 struct iio_dev *indio_dev = filp->private_data;
50 struct iio_buffer *rb = indio_dev->buffer; 58 struct iio_buffer *rb = indio_dev->buffer;
59 int ret;
51 60
52 if (!indio_dev->info) 61 if (!indio_dev->info)
53 return -ENODEV; 62 return -ENODEV;
54 63
55 if (!rb || !rb->access->read_first_n) 64 if (!rb || !rb->access->read_first_n)
56 return -EINVAL; 65 return -EINVAL;
57 return rb->access->read_first_n(rb, n, buf); 66
67 do {
68 if (!iio_buffer_data_available(rb)) {
69 if (filp->f_flags & O_NONBLOCK)
70 return -EAGAIN;
71
72 ret = wait_event_interruptible(rb->pollq,
73 iio_buffer_data_available(rb) ||
74 indio_dev->info == NULL);
75 if (ret)
76 return ret;
77 if (indio_dev->info == NULL)
78 return -ENODEV;
79 }
80
81 ret = rb->access->read_first_n(rb, n, buf);
82 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
83 ret = -EAGAIN;
84 } while (ret == 0);
85
86 return ret;
58} 87}
59 88
60/** 89/**
@@ -70,7 +99,7 @@ unsigned int iio_buffer_poll(struct file *filp,
70 return -ENODEV; 99 return -ENODEV;
71 100
72 poll_wait(filp, &rb->pollq, wait); 101 poll_wait(filp, &rb->pollq, wait);
73 if (rb->stufftoread) 102 if (iio_buffer_data_available(rb))
74 return POLLIN | POLLRDNORM; 103 return POLLIN | POLLRDNORM;
75 /* need a way of knowing if there may be enough data... */ 104 /* need a way of knowing if there may be enough data... */
76 return 0; 105 return 0;
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 2fe88c189f74..acc911a836ca 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -69,6 +69,7 @@ static const char * const iio_chan_type_name_spec[] = {
69 [IIO_ALTVOLTAGE] = "altvoltage", 69 [IIO_ALTVOLTAGE] = "altvoltage",
70 [IIO_CCT] = "cct", 70 [IIO_CCT] = "cct",
71 [IIO_PRESSURE] = "pressure", 71 [IIO_PRESSURE] = "pressure",
72 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
72}; 73};
73 74
74static const char * const iio_modifier_names[] = { 75static const char * const iio_modifier_names[] = {
diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index bc043fab4cd1..c9c1419fe6e0 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -242,13 +242,9 @@ static ssize_t iio_ev_state_store(struct device *dev,
242 if (ret < 0) 242 if (ret < 0)
243 return ret; 243 return ret;
244 244
245 if (indio_dev->info->write_event_config) 245 ret = indio_dev->info->write_event_config(indio_dev,
246 ret = indio_dev->info->write_event_config(indio_dev, 246 this_attr->c, iio_ev_attr_type(this_attr),
247 this_attr->address, val); 247 iio_ev_attr_dir(this_attr), val);
248 else
249 ret = indio_dev->info->write_event_config_new(indio_dev,
250 this_attr->c, iio_ev_attr_type(this_attr),
251 iio_ev_attr_dir(this_attr), val);
252 248
253 return (ret < 0) ? ret : len; 249 return (ret < 0) ? ret : len;
254} 250}
@@ -261,13 +257,9 @@ static ssize_t iio_ev_state_show(struct device *dev,
261 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 257 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
262 int val; 258 int val;
263 259
264 if (indio_dev->info->read_event_config) 260 val = indio_dev->info->read_event_config(indio_dev,
265 val = indio_dev->info->read_event_config(indio_dev, 261 this_attr->c, iio_ev_attr_type(this_attr),
266 this_attr->address); 262 iio_ev_attr_dir(this_attr));
267 else
268 val = indio_dev->info->read_event_config_new(indio_dev,
269 this_attr->c, iio_ev_attr_type(this_attr),
270 iio_ev_attr_dir(this_attr));
271 if (val < 0) 263 if (val < 0)
272 return val; 264 return val;
273 else 265 else
@@ -283,21 +275,13 @@ static ssize_t iio_ev_value_show(struct device *dev,
283 int val, val2; 275 int val, val2;
284 int ret; 276 int ret;
285 277
286 if (indio_dev->info->read_event_value) { 278 ret = indio_dev->info->read_event_value(indio_dev,
287 ret = indio_dev->info->read_event_value(indio_dev, 279 this_attr->c, iio_ev_attr_type(this_attr),
288 this_attr->address, &val); 280 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
289 if (ret < 0) 281 &val, &val2);
290 return ret; 282 if (ret < 0)
291 return sprintf(buf, "%d\n", val); 283 return ret;
292 } else { 284 return iio_format_value(buf, ret, val, val2);
293 ret = indio_dev->info->read_event_value_new(indio_dev,
294 this_attr->c, iio_ev_attr_type(this_attr),
295 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
296 &val, &val2);
297 if (ret < 0)
298 return ret;
299 return iio_format_value(buf, ret, val, val2);
300 }
301} 285}
302 286
303static ssize_t iio_ev_value_store(struct device *dev, 287static ssize_t iio_ev_value_store(struct device *dev,
@@ -310,25 +294,16 @@ static ssize_t iio_ev_value_store(struct device *dev,
310 int val, val2; 294 int val, val2;
311 int ret; 295 int ret;
312 296
313 if (!indio_dev->info->write_event_value && 297 if (!indio_dev->info->write_event_value)
314 !indio_dev->info->write_event_value_new)
315 return -EINVAL; 298 return -EINVAL;
316 299
317 if (indio_dev->info->write_event_value) { 300 ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
318 ret = kstrtoint(buf, 10, &val); 301 if (ret)
319 if (ret) 302 return ret;
320 return ret; 303 ret = indio_dev->info->write_event_value(indio_dev,
321 ret = indio_dev->info->write_event_value(indio_dev, 304 this_attr->c, iio_ev_attr_type(this_attr),
322 this_attr->address, val); 305 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
323 } else { 306 val, val2);
324 ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
325 if (ret)
326 return ret;
327 ret = indio_dev->info->write_event_value_new(indio_dev,
328 this_attr->c, iio_ev_attr_type(this_attr),
329 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
330 val, val2);
331 }
332 if (ret < 0) 307 if (ret < 0)
333 return ret; 308 return ret;
334 309
@@ -377,7 +352,7 @@ static int iio_device_add_event(struct iio_dev *indio_dev,
377 return attrcount; 352 return attrcount;
378} 353}
379 354
380static int iio_device_add_event_sysfs_new(struct iio_dev *indio_dev, 355static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
381 struct iio_chan_spec const *chan) 356 struct iio_chan_spec const *chan)
382{ 357{
383 int ret = 0, i, attrcount = 0; 358 int ret = 0, i, attrcount = 0;
@@ -420,89 +395,6 @@ error_ret:
420 return ret; 395 return ret;
421} 396}
422 397
423static int iio_device_add_event_sysfs_old(struct iio_dev *indio_dev,
424 struct iio_chan_spec const *chan)
425{
426 int ret = 0, i, attrcount = 0;
427 u64 mask = 0;
428 char *postfix;
429 if (!chan->event_mask)
430 return 0;
431
432 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
433 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
434 iio_ev_type_text[i/IIO_EV_DIR_MAX],
435 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
436 if (postfix == NULL) {
437 ret = -ENOMEM;
438 goto error_ret;
439 }
440 if (chan->modified)
441 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2,
442 i/IIO_EV_DIR_MAX,
443 i%IIO_EV_DIR_MAX);
444 else if (chan->differential)
445 mask = IIO_EVENT_CODE(chan->type,
446 0, 0,
447 i%IIO_EV_DIR_MAX,
448 i/IIO_EV_DIR_MAX,
449 0,
450 chan->channel,
451 chan->channel2);
452 else
453 mask = IIO_UNMOD_EVENT_CODE(chan->type,
454 chan->channel,
455 i/IIO_EV_DIR_MAX,
456 i%IIO_EV_DIR_MAX);
457
458 ret = __iio_add_chan_devattr(postfix,
459 chan,
460 &iio_ev_state_show,
461 iio_ev_state_store,
462 mask,
463 0,
464 &indio_dev->dev,
465 &indio_dev->event_interface->
466 dev_attr_list);
467 kfree(postfix);
468 if (ret)
469 goto error_ret;
470 attrcount++;
471 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
472 iio_ev_type_text[i/IIO_EV_DIR_MAX],
473 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
474 if (postfix == NULL) {
475 ret = -ENOMEM;
476 goto error_ret;
477 }
478 ret = __iio_add_chan_devattr(postfix, chan,
479 iio_ev_value_show,
480 iio_ev_value_store,
481 mask,
482 0,
483 &indio_dev->dev,
484 &indio_dev->event_interface->
485 dev_attr_list);
486 kfree(postfix);
487 if (ret)
488 goto error_ret;
489 attrcount++;
490 }
491 ret = attrcount;
492error_ret:
493 return ret;
494}
495
496
497static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
498 struct iio_chan_spec const *chan)
499{
500 if (chan->event_mask)
501 return iio_device_add_event_sysfs_old(indio_dev, chan);
502 else
503 return iio_device_add_event_sysfs_new(indio_dev, chan);
504}
505
506static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev) 398static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
507{ 399{
508 int j, ret, attrcount = 0; 400 int j, ret, attrcount = 0;
@@ -523,8 +415,6 @@ static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
523 int j; 415 int j;
524 416
525 for (j = 0; j < indio_dev->num_channels; j++) { 417 for (j = 0; j < indio_dev->num_channels; j++) {
526 if (indio_dev->channels[j].event_mask != 0)
527 return true;
528 if (indio_dev->channels[j].num_event_specs != 0) 418 if (indio_dev->channels[j].num_event_specs != 0)
529 return true; 419 return true;
530 } 420 }
diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 7ba2f002ffca..766fab24b720 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -55,15 +55,7 @@ static struct attribute *iio_trig_dev_attrs[] = {
55 &dev_attr_name.attr, 55 &dev_attr_name.attr,
56 NULL, 56 NULL,
57}; 57};
58 58ATTRIBUTE_GROUPS(iio_trig_dev);
59static struct attribute_group iio_trig_attr_group = {
60 .attrs = iio_trig_dev_attrs,
61};
62
63static const struct attribute_group *iio_trig_attr_groups[] = {
64 &iio_trig_attr_group,
65 NULL
66};
67 59
68int iio_trigger_register(struct iio_trigger *trig_info) 60int iio_trigger_register(struct iio_trigger *trig_info)
69{ 61{
@@ -403,7 +395,7 @@ static void iio_trig_release(struct device *device)
403 395
404static struct device_type iio_trig_type = { 396static struct device_type iio_trig_type = {
405 .release = iio_trig_release, 397 .release = iio_trig_release,
406 .groups = iio_trig_attr_groups, 398 .groups = iio_trig_dev_groups,
407}; 399};
408 400
409static void iio_trig_subirqmask(struct irq_data *d) 401static void iio_trig_subirqmask(struct irq_data *d)
diff --git a/drivers/iio/kfifo_buf.c b/drivers/iio/kfifo_buf.c
index 95c6fc81c2c7..7134e8ada09a 100644
--- a/drivers/iio/kfifo_buf.c
+++ b/drivers/iio/kfifo_buf.c
@@ -42,7 +42,6 @@ static int iio_request_update_kfifo(struct iio_buffer *r)
42 } else { 42 } else {
43 kfifo_reset_out(&buf->kf); 43 kfifo_reset_out(&buf->kf);
44 } 44 }
45 r->stufftoread = false;
46 mutex_unlock(&buf->user_lock); 45 mutex_unlock(&buf->user_lock);
47 46
48 return ret; 47 return ret;
@@ -108,7 +107,7 @@ static int iio_store_to_kfifo(struct iio_buffer *r,
108 ret = kfifo_in(&kf->kf, data, 1); 107 ret = kfifo_in(&kf->kf, data, 1);
109 if (ret != 1) 108 if (ret != 1)
110 return -EBUSY; 109 return -EBUSY;
111 r->stufftoread = true; 110
112 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM); 111 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
113 112
114 return 0; 113 return 0;
@@ -127,13 +126,6 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
127 ret = -EINVAL; 126 ret = -EINVAL;
128 else 127 else
129 ret = kfifo_to_user(&kf->kf, buf, n, &copied); 128 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
130
131 if (kfifo_is_empty(&kf->kf))
132 r->stufftoread = false;
133 /* verify it is still empty to avoid race */
134 if (!kfifo_is_empty(&kf->kf))
135 r->stufftoread = true;
136
137 mutex_unlock(&kf->user_lock); 129 mutex_unlock(&kf->user_lock);
138 if (ret < 0) 130 if (ret < 0)
139 return ret; 131 return ret;
@@ -141,6 +133,18 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
141 return copied; 133 return copied;
142} 134}
143 135
136static bool iio_kfifo_buf_data_available(struct iio_buffer *r)
137{
138 struct iio_kfifo *kf = iio_to_kfifo(r);
139 bool empty;
140
141 mutex_lock(&kf->user_lock);
142 empty = kfifo_is_empty(&kf->kf);
143 mutex_unlock(&kf->user_lock);
144
145 return !empty;
146}
147
144static void iio_kfifo_buffer_release(struct iio_buffer *buffer) 148static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
145{ 149{
146 struct iio_kfifo *kf = iio_to_kfifo(buffer); 150 struct iio_kfifo *kf = iio_to_kfifo(buffer);
@@ -153,6 +157,7 @@ static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
153static const struct iio_buffer_access_funcs kfifo_access_funcs = { 157static const struct iio_buffer_access_funcs kfifo_access_funcs = {
154 .store_to = &iio_store_to_kfifo, 158 .store_to = &iio_store_to_kfifo,
155 .read_first_n = &iio_read_first_n_kfifo, 159 .read_first_n = &iio_read_first_n_kfifo,
160 .data_available = iio_kfifo_buf_data_available,
156 .request_update = &iio_request_update_kfifo, 161 .request_update = &iio_request_update_kfifo,
157 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo, 162 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
158 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, 163 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
diff --git a/drivers/iio/light/adjd_s311.c b/drivers/iio/light/adjd_s311.c
index 83d15c5baf64..f3068477b466 100644
--- a/drivers/iio/light/adjd_s311.c
+++ b/drivers/iio/light/adjd_s311.c
@@ -155,7 +155,12 @@ done:
155 BIT(IIO_CHAN_INFO_INT_TIME), \ 155 BIT(IIO_CHAN_INFO_INT_TIME), \
156 .channel2 = (IIO_MOD_LIGHT_##_color), \ 156 .channel2 = (IIO_MOD_LIGHT_##_color), \
157 .scan_index = (_scan_idx), \ 157 .scan_index = (_scan_idx), \
158 .scan_type = IIO_ST('u', 10, 16, 0), \ 158 .scan_type = { \
159 .sign = 'u', \
160 .realbits = 10, \
161 .storagebits = 16, \
162 .endianness = IIO_CPU, \
163 }, \
159} 164}
160 165
161static const struct iio_chan_spec adjd_s311_channels[] = { 166static const struct iio_chan_spec adjd_s311_channels[] = {
diff --git a/drivers/iio/light/apds9300.c b/drivers/iio/light/apds9300.c
index 51097bbd59c9..9ddde0ca9c34 100644
--- a/drivers/iio/light/apds9300.c
+++ b/drivers/iio/light/apds9300.c
@@ -344,10 +344,10 @@ static const struct iio_info apds9300_info_no_irq = {
344static const struct iio_info apds9300_info = { 344static const struct iio_info apds9300_info = {
345 .driver_module = THIS_MODULE, 345 .driver_module = THIS_MODULE,
346 .read_raw = apds9300_read_raw, 346 .read_raw = apds9300_read_raw,
347 .read_event_value_new = apds9300_read_thresh, 347 .read_event_value = apds9300_read_thresh,
348 .write_event_value_new = apds9300_write_thresh, 348 .write_event_value = apds9300_write_thresh,
349 .read_event_config_new = apds9300_read_interrupt_config, 349 .read_event_config = apds9300_read_interrupt_config,
350 .write_event_config_new = apds9300_write_interrupt_config, 350 .write_event_config = apds9300_write_interrupt_config,
351}; 351};
352 352
353static const struct iio_event_spec apds9300_event_spec[] = { 353static const struct iio_event_spec apds9300_event_spec[] = {
diff --git a/drivers/iio/light/cm36651.c b/drivers/iio/light/cm36651.c
index 0922e39b0ea9..0a142af83e25 100644
--- a/drivers/iio/light/cm36651.c
+++ b/drivers/iio/light/cm36651.c
@@ -488,7 +488,11 @@ static int cm36651_write_raw(struct iio_dev *indio_dev,
488} 488}
489 489
490static int cm36651_read_prox_thresh(struct iio_dev *indio_dev, 490static int cm36651_read_prox_thresh(struct iio_dev *indio_dev,
491 u64 event_code, int *val) 491 const struct iio_chan_spec *chan,
492 enum iio_event_type type,
493 enum iio_event_direction dir,
494 enum iio_event_info info,
495 int *val, int *val2)
492{ 496{
493 struct cm36651_data *cm36651 = iio_priv(indio_dev); 497 struct cm36651_data *cm36651 = iio_priv(indio_dev);
494 498
@@ -498,7 +502,11 @@ static int cm36651_read_prox_thresh(struct iio_dev *indio_dev,
498} 502}
499 503
500static int cm36651_write_prox_thresh(struct iio_dev *indio_dev, 504static int cm36651_write_prox_thresh(struct iio_dev *indio_dev,
501 u64 event_code, int val) 505 const struct iio_chan_spec *chan,
506 enum iio_event_type type,
507 enum iio_event_direction dir,
508 enum iio_event_info info,
509 int val, int val2)
502{ 510{
503 struct cm36651_data *cm36651 = iio_priv(indio_dev); 511 struct cm36651_data *cm36651 = iio_priv(indio_dev);
504 struct i2c_client *client = cm36651->client; 512 struct i2c_client *client = cm36651->client;
@@ -520,7 +528,10 @@ static int cm36651_write_prox_thresh(struct iio_dev *indio_dev,
520} 528}
521 529
522static int cm36651_write_prox_event_config(struct iio_dev *indio_dev, 530static int cm36651_write_prox_event_config(struct iio_dev *indio_dev,
523 u64 event_code, int state) 531 const struct iio_chan_spec *chan,
532 enum iio_event_type type,
533 enum iio_event_direction dir,
534 int state)
524{ 535{
525 struct cm36651_data *cm36651 = iio_priv(indio_dev); 536 struct cm36651_data *cm36651 = iio_priv(indio_dev);
526 int cmd, ret = -EINVAL; 537 int cmd, ret = -EINVAL;
@@ -536,7 +547,9 @@ static int cm36651_write_prox_event_config(struct iio_dev *indio_dev,
536} 547}
537 548
538static int cm36651_read_prox_event_config(struct iio_dev *indio_dev, 549static int cm36651_read_prox_event_config(struct iio_dev *indio_dev,
539 u64 event_code) 550 const struct iio_chan_spec *chan,
551 enum iio_event_type type,
552 enum iio_event_direction dir)
540{ 553{
541 struct cm36651_data *cm36651 = iio_priv(indio_dev); 554 struct cm36651_data *cm36651 = iio_priv(indio_dev);
542 int event_en; 555 int event_en;
@@ -559,12 +572,22 @@ static int cm36651_read_prox_event_config(struct iio_dev *indio_dev,
559 .channel2 = IIO_MOD_LIGHT_##_color, \ 572 .channel2 = IIO_MOD_LIGHT_##_color, \
560} \ 573} \
561 574
575static const struct iio_event_spec cm36651_event_spec[] = {
576 {
577 .type = IIO_EV_TYPE_THRESH,
578 .dir = IIO_EV_DIR_EITHER,
579 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
580 BIT(IIO_EV_INFO_ENABLE),
581 }
582};
583
562static const struct iio_chan_spec cm36651_channels[] = { 584static const struct iio_chan_spec cm36651_channels[] = {
563 { 585 {
564 .type = IIO_PROXIMITY, 586 .type = IIO_PROXIMITY,
565 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 587 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
566 BIT(IIO_CHAN_INFO_INT_TIME), 588 BIT(IIO_CHAN_INFO_INT_TIME),
567 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_EITHER) 589 .event_spec = cm36651_event_spec,
590 .num_event_specs = ARRAY_SIZE(cm36651_event_spec),
568 }, 591 },
569 CM36651_LIGHT_CHANNEL(RED, CM36651_LIGHT_CHANNEL_IDX_RED), 592 CM36651_LIGHT_CHANNEL(RED, CM36651_LIGHT_CHANNEL_IDX_RED),
570 CM36651_LIGHT_CHANNEL(GREEN, CM36651_LIGHT_CHANNEL_IDX_GREEN), 593 CM36651_LIGHT_CHANNEL(GREEN, CM36651_LIGHT_CHANNEL_IDX_GREEN),
@@ -693,7 +716,7 @@ static const struct of_device_id cm36651_of_match[] = {
693static struct i2c_driver cm36651_driver = { 716static struct i2c_driver cm36651_driver = {
694 .driver = { 717 .driver = {
695 .name = "cm36651", 718 .name = "cm36651",
696 .of_match_table = of_match_ptr(cm36651_of_match), 719 .of_match_table = cm36651_of_match,
697 .owner = THIS_MODULE, 720 .owner = THIS_MODULE,
698 }, 721 },
699 .probe = cm36651_probe, 722 .probe = cm36651_probe,
diff --git a/drivers/iio/light/gp2ap020a00f.c b/drivers/iio/light/gp2ap020a00f.c
index dc79835be308..5ea4a03c7e71 100644
--- a/drivers/iio/light/gp2ap020a00f.c
+++ b/drivers/iio/light/gp2ap020a00f.c
@@ -1388,10 +1388,10 @@ static const struct iio_chan_spec gp2ap020a00f_channels[] = {
1388 1388
1389static const struct iio_info gp2ap020a00f_info = { 1389static const struct iio_info gp2ap020a00f_info = {
1390 .read_raw = &gp2ap020a00f_read_raw, 1390 .read_raw = &gp2ap020a00f_read_raw,
1391 .read_event_value_new = &gp2ap020a00f_read_event_val, 1391 .read_event_value = &gp2ap020a00f_read_event_val,
1392 .read_event_config_new = &gp2ap020a00f_read_event_config, 1392 .read_event_config = &gp2ap020a00f_read_event_config,
1393 .write_event_value_new = &gp2ap020a00f_write_event_val, 1393 .write_event_value = &gp2ap020a00f_write_event_val,
1394 .write_event_config_new = &gp2ap020a00f_write_event_config, 1394 .write_event_config = &gp2ap020a00f_write_event_config,
1395 .driver_module = THIS_MODULE, 1395 .driver_module = THIS_MODULE,
1396}; 1396};
1397 1397
diff --git a/drivers/iio/light/tcs3472.c b/drivers/iio/light/tcs3472.c
index 45df2204614a..887fecf1f9bb 100644
--- a/drivers/iio/light/tcs3472.c
+++ b/drivers/iio/light/tcs3472.c
@@ -67,7 +67,12 @@ struct tcs3472_data {
67 .channel2 = IIO_MOD_LIGHT_##_color, \ 67 .channel2 = IIO_MOD_LIGHT_##_color, \
68 .address = _addr, \ 68 .address = _addr, \
69 .scan_index = _si, \ 69 .scan_index = _si, \
70 .scan_type = IIO_ST('u', 16, 16, 0), \ 70 .scan_type = { \
71 .sign = 'u', \
72 .realbits = 16, \
73 .storagebits = 16, \
74 .endianness = IIO_CPU, \
75 }, \
71} 76}
72 77
73static const int tcs3472_agains[] = { 1, 4, 16, 60 }; 78static const int tcs3472_agains[] = { 1, 4, 16, 60 };
diff --git a/drivers/iio/light/tsl2563.c b/drivers/iio/light/tsl2563.c
index 0c6e459c86b1..3d8110157f2d 100644
--- a/drivers/iio/light/tsl2563.c
+++ b/drivers/iio/light/tsl2563.c
@@ -702,10 +702,10 @@ static const struct iio_info tsl2563_info = {
702 .driver_module = THIS_MODULE, 702 .driver_module = THIS_MODULE,
703 .read_raw = &tsl2563_read_raw, 703 .read_raw = &tsl2563_read_raw,
704 .write_raw = &tsl2563_write_raw, 704 .write_raw = &tsl2563_write_raw,
705 .read_event_value_new = &tsl2563_read_thresh, 705 .read_event_value = &tsl2563_read_thresh,
706 .write_event_value_new = &tsl2563_write_thresh, 706 .write_event_value = &tsl2563_write_thresh,
707 .read_event_config_new = &tsl2563_read_interrupt_config, 707 .read_event_config = &tsl2563_read_interrupt_config,
708 .write_event_config_new = &tsl2563_write_interrupt_config, 708 .write_event_config = &tsl2563_write_interrupt_config,
709}; 709};
710 710
711static int tsl2563_probe(struct i2c_client *client, 711static int tsl2563_probe(struct i2c_client *client,
diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index 384ac23f576f..d948c4778ba6 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -56,7 +56,7 @@ static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
56 u8 rdy_mask, u8 data_reg, int *val) 56 u8 rdy_mask, u8 data_reg, int *val)
57{ 57{
58 int tries = 20; 58 int tries = 20;
59 u16 buf; 59 __be16 buf;
60 int ret; 60 int ret;
61 61
62 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 62 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c
index becf54496967..4b65b6d3bdb1 100644
--- a/drivers/iio/magnetometer/mag3110.c
+++ b/drivers/iio/magnetometer/mag3110.c
@@ -266,7 +266,11 @@ static const struct iio_chan_spec mag3110_channels[] = {
266 .type = IIO_TEMP, 266 .type = IIO_TEMP,
267 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 267 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
268 .scan_index = 3, 268 .scan_index = 3,
269 .scan_type = IIO_ST('s', 8, 8, 0), 269 .scan_type = {
270 .sign = 's',
271 .realbits = 8,
272 .storagebits = 8,
273 },
270 }, 274 },
271 IIO_CHAN_SOFT_TIMESTAMP(4), 275 IIO_CHAN_SOFT_TIMESTAMP(4),
272}; 276};
diff --git a/drivers/iio/orientation/Kconfig b/drivers/iio/orientation/Kconfig
new file mode 100644
index 000000000000..58c62c837e12
--- /dev/null
+++ b/drivers/iio/orientation/Kconfig
@@ -0,0 +1,19 @@
1#
2# Inclinometer sensors
3#
4# When adding new entries keep the list in alphabetical order
5
6menu "Inclinometer sensors"
7
8config HID_SENSOR_INCLINOMETER_3D
9 depends on HID_SENSOR_HUB
10 select IIO_BUFFER
11 select IIO_TRIGGERED_BUFFER
12 select HID_SENSOR_IIO_COMMON
13 select HID_SENSOR_IIO_TRIGGER
14 tristate "HID Inclinometer 3D"
15 help
16 Say yes here to build support for the HID SENSOR
17 Inclinometer 3D.
18
19endmenu
diff --git a/drivers/iio/orientation/Makefile b/drivers/iio/orientation/Makefile
new file mode 100644
index 000000000000..2c97572ee919
--- /dev/null
+++ b/drivers/iio/orientation/Makefile
@@ -0,0 +1,6 @@
1#
2# Makefile for industrial I/O Inclinometer sensor drivers
3#
4
5# When adding new entries keep the list in alphabetical order
6obj-$(CONFIG_HID_SENSOR_INCLINOMETER_3D) += hid-sensor-incl-3d.o
diff --git a/drivers/iio/orientation/hid-sensor-incl-3d.c b/drivers/iio/orientation/hid-sensor-incl-3d.c
new file mode 100644
index 000000000000..070feab08faa
--- /dev/null
+++ b/drivers/iio/orientation/hid-sensor-incl-3d.c
@@ -0,0 +1,428 @@
1/*
2 * HID Sensors Driver
3 * Copyright (c) 2013, 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 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.
16 *
17 */
18
19#include <linux/device.h>
20#include <linux/platform_device.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
24#include <linux/slab.h>
25#include <linux/hid-sensor-hub.h>
26#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h>
28#include <linux/iio/buffer.h>
29#include <linux/iio/trigger_consumer.h>
30#include <linux/iio/triggered_buffer.h>
31#include "../common/hid-sensors/hid-sensor-trigger.h"
32
33enum incl_3d_channel {
34 CHANNEL_SCAN_INDEX_X,
35 CHANNEL_SCAN_INDEX_Y,
36 CHANNEL_SCAN_INDEX_Z,
37 INCLI_3D_CHANNEL_MAX,
38};
39
40struct incl_3d_state {
41 struct hid_sensor_hub_callbacks callbacks;
42 struct hid_sensor_common common_attributes;
43 struct hid_sensor_hub_attribute_info incl[INCLI_3D_CHANNEL_MAX];
44 u32 incl_val[INCLI_3D_CHANNEL_MAX];
45};
46
47static const u32 incl_3d_addresses[INCLI_3D_CHANNEL_MAX] = {
48 HID_USAGE_SENSOR_ORIENT_TILT_X,
49 HID_USAGE_SENSOR_ORIENT_TILT_Y,
50 HID_USAGE_SENSOR_ORIENT_TILT_Z
51};
52
53/* Channel definitions */
54static const struct iio_chan_spec incl_3d_channels[] = {
55 {
56 .type = IIO_INCLI,
57 .modified = 1,
58 .channel2 = IIO_MOD_X,
59 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
60 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
61 BIT(IIO_CHAN_INFO_SCALE) |
62 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
63 BIT(IIO_CHAN_INFO_HYSTERESIS),
64 .scan_index = CHANNEL_SCAN_INDEX_X,
65 }, {
66 .type = IIO_INCLI,
67 .modified = 1,
68 .channel2 = IIO_MOD_Y,
69 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
70 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
71 BIT(IIO_CHAN_INFO_SCALE) |
72 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
73 BIT(IIO_CHAN_INFO_HYSTERESIS),
74 .scan_index = CHANNEL_SCAN_INDEX_Y,
75 }, {
76 .type = IIO_INCLI,
77 .modified = 1,
78 .channel2 = IIO_MOD_Z,
79 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
80 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
81 BIT(IIO_CHAN_INFO_SCALE) |
82 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
83 BIT(IIO_CHAN_INFO_HYSTERESIS),
84 .scan_index = CHANNEL_SCAN_INDEX_Z,
85 }
86};
87
88/* Adjust channel real bits based on report descriptor */
89static void incl_3d_adjust_channel_bit_mask(struct iio_chan_spec *chan,
90 int size)
91{
92 chan->scan_type.sign = 's';
93 /* Real storage bits will change based on the report desc. */
94 chan->scan_type.realbits = size * 8;
95 /* Maximum size of a sample to capture is u32 */
96 chan->scan_type.storagebits = sizeof(u32) * 8;
97}
98
99/* Channel read_raw handler */
100static int incl_3d_read_raw(struct iio_dev *indio_dev,
101 struct iio_chan_spec const *chan,
102 int *val, int *val2,
103 long mask)
104{
105 struct incl_3d_state *incl_state = iio_priv(indio_dev);
106 int report_id = -1;
107 u32 address;
108 int ret_type;
109
110 *val = 0;
111 *val2 = 0;
112 switch (mask) {
113 case IIO_CHAN_INFO_RAW:
114 report_id =
115 incl_state->incl[chan->scan_index].report_id;
116 address = incl_3d_addresses[chan->scan_index];
117 if (report_id >= 0)
118 *val = sensor_hub_input_attr_get_raw_value(
119 incl_state->common_attributes.hsdev,
120 HID_USAGE_SENSOR_INCLINOMETER_3D, address,
121 report_id);
122 else {
123 return -EINVAL;
124 }
125 ret_type = IIO_VAL_INT;
126 break;
127 case IIO_CHAN_INFO_SCALE:
128 *val = incl_state->incl[CHANNEL_SCAN_INDEX_X].units;
129 ret_type = IIO_VAL_INT;
130 break;
131 case IIO_CHAN_INFO_OFFSET:
132 *val = hid_sensor_convert_exponent(
133 incl_state->incl[CHANNEL_SCAN_INDEX_X].unit_expo);
134 ret_type = IIO_VAL_INT;
135 break;
136 case IIO_CHAN_INFO_SAMP_FREQ:
137 ret_type = hid_sensor_read_samp_freq_value(
138 &incl_state->common_attributes, val, val2);
139 break;
140 case IIO_CHAN_INFO_HYSTERESIS:
141 ret_type = hid_sensor_read_raw_hyst_value(
142 &incl_state->common_attributes, val, val2);
143 break;
144 default:
145 ret_type = -EINVAL;
146 break;
147 }
148
149 return ret_type;
150}
151
152/* Channel write_raw handler */
153static int incl_3d_write_raw(struct iio_dev *indio_dev,
154 struct iio_chan_spec const *chan,
155 int val,
156 int val2,
157 long mask)
158{
159 struct incl_3d_state *incl_state = iio_priv(indio_dev);
160 int ret;
161
162 switch (mask) {
163 case IIO_CHAN_INFO_SAMP_FREQ:
164 ret = hid_sensor_write_samp_freq_value(
165 &incl_state->common_attributes, val, val2);
166 break;
167 case IIO_CHAN_INFO_HYSTERESIS:
168 ret = hid_sensor_write_raw_hyst_value(
169 &incl_state->common_attributes, val, val2);
170 break;
171 default:
172 ret = -EINVAL;
173 }
174
175 return ret;
176}
177
178static const struct iio_info incl_3d_info = {
179 .driver_module = THIS_MODULE,
180 .read_raw = &incl_3d_read_raw,
181 .write_raw = &incl_3d_write_raw,
182};
183
184/* Function to push data to buffer */
185static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
186{
187 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
188 iio_push_to_buffers(indio_dev, (u8 *)data);
189}
190
191/* Callback handler to send event after all samples are received and captured */
192static int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev,
193 unsigned usage_id,
194 void *priv)
195{
196 struct iio_dev *indio_dev = platform_get_drvdata(priv);
197 struct incl_3d_state *incl_state = iio_priv(indio_dev);
198
199 dev_dbg(&indio_dev->dev, "incl_3d_proc_event [%d]\n",
200 incl_state->common_attributes.data_ready);
201 if (incl_state->common_attributes.data_ready)
202 hid_sensor_push_data(indio_dev,
203 (u8 *)incl_state->incl_val,
204 sizeof(incl_state->incl_val));
205
206 return 0;
207}
208
209/* Capture samples in local storage */
210static int incl_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
211 unsigned usage_id,
212 size_t raw_len, char *raw_data,
213 void *priv)
214{
215 struct iio_dev *indio_dev = platform_get_drvdata(priv);
216 struct incl_3d_state *incl_state = iio_priv(indio_dev);
217 int ret = 0;
218
219 switch (usage_id) {
220 case HID_USAGE_SENSOR_ORIENT_TILT_X:
221 incl_state->incl_val[CHANNEL_SCAN_INDEX_X] = *(u32 *)raw_data;
222 break;
223 case HID_USAGE_SENSOR_ORIENT_TILT_Y:
224 incl_state->incl_val[CHANNEL_SCAN_INDEX_Y] = *(u32 *)raw_data;
225 break;
226 case HID_USAGE_SENSOR_ORIENT_TILT_Z:
227 incl_state->incl_val[CHANNEL_SCAN_INDEX_Z] = *(u32 *)raw_data;
228 break;
229 default:
230 ret = -EINVAL;
231 break;
232 }
233
234 return ret;
235}
236
237/* Parse report which is specific to an usage id*/
238static int incl_3d_parse_report(struct platform_device *pdev,
239 struct hid_sensor_hub_device *hsdev,
240 struct iio_chan_spec *channels,
241 unsigned usage_id,
242 struct incl_3d_state *st)
243{
244 int ret;
245
246 ret = sensor_hub_input_get_attribute_info(hsdev,
247 HID_INPUT_REPORT,
248 usage_id,
249 HID_USAGE_SENSOR_ORIENT_TILT_X,
250 &st->incl[CHANNEL_SCAN_INDEX_X]);
251 if (ret)
252 return ret;
253 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_X],
254 st->incl[CHANNEL_SCAN_INDEX_X].size);
255
256 ret = sensor_hub_input_get_attribute_info(hsdev,
257 HID_INPUT_REPORT,
258 usage_id,
259 HID_USAGE_SENSOR_ORIENT_TILT_Y,
260 &st->incl[CHANNEL_SCAN_INDEX_Y]);
261 if (ret)
262 return ret;
263 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Y],
264 st->incl[CHANNEL_SCAN_INDEX_Y].size);
265
266 ret = sensor_hub_input_get_attribute_info(hsdev,
267 HID_INPUT_REPORT,
268 usage_id,
269 HID_USAGE_SENSOR_ORIENT_TILT_Z,
270 &st->incl[CHANNEL_SCAN_INDEX_Z]);
271 if (ret)
272 return ret;
273 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Z],
274 st->incl[CHANNEL_SCAN_INDEX_Z].size);
275
276 dev_dbg(&pdev->dev, "incl_3d %x:%x, %x:%x, %x:%x\n",
277 st->incl[0].index,
278 st->incl[0].report_id,
279 st->incl[1].index, st->incl[1].report_id,
280 st->incl[2].index, st->incl[2].report_id);
281
282 /* Set Sensitivity field ids, when there is no individual modifier */
283 if (st->common_attributes.sensitivity.index < 0) {
284 sensor_hub_input_get_attribute_info(hsdev,
285 HID_FEATURE_REPORT, usage_id,
286 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
287 HID_USAGE_SENSOR_DATA_ORIENTATION,
288 &st->common_attributes.sensitivity);
289 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
290 st->common_attributes.sensitivity.index,
291 st->common_attributes.sensitivity.report_id);
292 }
293 return ret;
294}
295
296/* Function to initialize the processing for usage id */
297static int hid_incl_3d_probe(struct platform_device *pdev)
298{
299 int ret;
300 static char *name = "incli_3d";
301 struct iio_dev *indio_dev;
302 struct incl_3d_state *incl_state;
303 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
304 struct iio_chan_spec *channels;
305
306 indio_dev = devm_iio_device_alloc(&pdev->dev,
307 sizeof(struct incl_3d_state));
308 if (indio_dev == NULL)
309 return -ENOMEM;
310
311 platform_set_drvdata(pdev, indio_dev);
312
313 incl_state = iio_priv(indio_dev);
314 incl_state->common_attributes.hsdev = hsdev;
315 incl_state->common_attributes.pdev = pdev;
316
317 ret = hid_sensor_parse_common_attributes(hsdev,
318 HID_USAGE_SENSOR_INCLINOMETER_3D,
319 &incl_state->common_attributes);
320 if (ret) {
321 dev_err(&pdev->dev, "failed to setup common attributes\n");
322 return ret;
323 }
324
325 channels = kmemdup(incl_3d_channels, sizeof(incl_3d_channels),
326 GFP_KERNEL);
327 if (!channels) {
328 dev_err(&pdev->dev, "failed to duplicate channels\n");
329 return -ENOMEM;
330 }
331
332 ret = incl_3d_parse_report(pdev, hsdev, channels,
333 HID_USAGE_SENSOR_INCLINOMETER_3D, incl_state);
334 if (ret) {
335 dev_err(&pdev->dev, "failed to setup attributes\n");
336 goto error_free_dev_mem;
337 }
338
339 indio_dev->channels = channels;
340 indio_dev->num_channels = ARRAY_SIZE(incl_3d_channels);
341 indio_dev->dev.parent = &pdev->dev;
342 indio_dev->info = &incl_3d_info;
343 indio_dev->name = name;
344 indio_dev->modes = INDIO_DIRECT_MODE;
345
346 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
347 NULL, NULL);
348 if (ret) {
349 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
350 goto error_free_dev_mem;
351 }
352 incl_state->common_attributes.data_ready = false;
353 ret = hid_sensor_setup_trigger(indio_dev, name,
354 &incl_state->common_attributes);
355 if (ret) {
356 dev_err(&pdev->dev, "trigger setup failed\n");
357 goto error_unreg_buffer_funcs;
358 }
359
360 ret = iio_device_register(indio_dev);
361 if (ret) {
362 dev_err(&pdev->dev, "device register failed\n");
363 goto error_remove_trigger;
364 }
365
366 incl_state->callbacks.send_event = incl_3d_proc_event;
367 incl_state->callbacks.capture_sample = incl_3d_capture_sample;
368 incl_state->callbacks.pdev = pdev;
369 ret = sensor_hub_register_callback(hsdev,
370 HID_USAGE_SENSOR_INCLINOMETER_3D,
371 &incl_state->callbacks);
372 if (ret) {
373 dev_err(&pdev->dev, "callback reg failed\n");
374 goto error_iio_unreg;
375 }
376
377 return 0;
378
379error_iio_unreg:
380 iio_device_unregister(indio_dev);
381error_remove_trigger:
382 hid_sensor_remove_trigger(&incl_state->common_attributes);
383error_unreg_buffer_funcs:
384 iio_triggered_buffer_cleanup(indio_dev);
385error_free_dev_mem:
386 kfree(indio_dev->channels);
387 return ret;
388}
389
390/* Function to deinitialize the processing for usage id */
391static int hid_incl_3d_remove(struct platform_device *pdev)
392{
393 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
394 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
395 struct incl_3d_state *incl_state = iio_priv(indio_dev);
396
397 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_INCLINOMETER_3D);
398 iio_device_unregister(indio_dev);
399 hid_sensor_remove_trigger(&incl_state->common_attributes);
400 iio_triggered_buffer_cleanup(indio_dev);
401 kfree(indio_dev->channels);
402
403 return 0;
404}
405
406static struct platform_device_id hid_incl_3d_ids[] = {
407 {
408 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
409 .name = "HID-SENSOR-200086",
410 },
411 { /* sentinel */ }
412};
413MODULE_DEVICE_TABLE(platform, hid_incl_3d_ids);
414
415static struct platform_driver hid_incl_3d_platform_driver = {
416 .id_table = hid_incl_3d_ids,
417 .driver = {
418 .name = KBUILD_MODNAME,
419 .owner = THIS_MODULE,
420 },
421 .probe = hid_incl_3d_probe,
422 .remove = hid_incl_3d_remove,
423};
424module_platform_driver(hid_incl_3d_platform_driver);
425
426MODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
427MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
428MODULE_LICENSE("GPL");