Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / adc / ad7150.c
blob04017ef6688afa9809a2ec3d4ed662a678082061
1 /*
2 * AD7150 capacitive sensor driver supporting AD7150/1/6
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
15 #include "../iio.h"
16 #include "../sysfs.h"
19 * AD7150 registers definition
22 #define AD7150_STATUS 0
23 #define AD7150_STATUS_OUT1 (1 << 3)
24 #define AD7150_STATUS_OUT2 (1 << 5)
25 #define AD7150_CH1_DATA_HIGH 1
26 #define AD7150_CH1_DATA_LOW 2
27 #define AD7150_CH2_DATA_HIGH 3
28 #define AD7150_CH2_DATA_LOW 4
29 #define AD7150_CH1_AVG_HIGH 5
30 #define AD7150_CH1_AVG_LOW 6
31 #define AD7150_CH2_AVG_HIGH 7
32 #define AD7150_CH2_AVG_LOW 8
33 #define AD7150_CH1_SENSITIVITY 9
34 #define AD7150_CH1_THR_HOLD_H 9
35 #define AD7150_CH1_TIMEOUT 10
36 #define AD7150_CH1_THR_HOLD_L 10
37 #define AD7150_CH1_SETUP 11
38 #define AD7150_CH2_SENSITIVITY 12
39 #define AD7150_CH2_THR_HOLD_H 12
40 #define AD7150_CH2_TIMEOUT 13
41 #define AD7150_CH2_THR_HOLD_L 13
42 #define AD7150_CH2_SETUP 14
43 #define AD7150_CFG 15
44 #define AD7150_CFG_FIX (1 << 7)
45 #define AD7150_PD_TIMER 16
46 #define AD7150_CH1_CAPDAC 17
47 #define AD7150_CH2_CAPDAC 18
48 #define AD7150_SN3 19
49 #define AD7150_SN2 20
50 #define AD7150_SN1 21
51 #define AD7150_SN0 22
52 #define AD7150_ID 23
54 #define AD7150_MAX_CONV_MODE 4
57 * struct ad7150_chip_info - chip specifc information
60 struct ad7150_chip_info {
61 struct i2c_client *client;
62 bool inter;
63 u16 ch1_threshold; /* Ch1 Threshold (in fixed threshold mode) */
64 u8 ch1_sensitivity; /* Ch1 Sensitivity (in adaptive threshold mode) */
65 u8 ch1_timeout; /* Ch1 Timeout (in adaptive threshold mode) */
66 u8 ch1_setup;
67 u16 ch2_threshold; /* Ch2 Threshold (in fixed threshold mode) */
68 u8 ch2_sensitivity; /* Ch1 Sensitivity (in adaptive threshold mode) */
69 u8 ch2_timeout; /* Ch1 Timeout (in adaptive threshold mode) */
70 u8 ch2_setup;
71 u8 powerdown_timer;
72 char threshold_mode[10]; /* adaptive/fixed threshold mode */
73 int old_state;
74 char *conversion_mode;
77 struct ad7150_conversion_mode {
78 char *name;
79 u8 reg_cfg;
82 static struct ad7150_conversion_mode
83 ad7150_conv_mode_table[AD7150_MAX_CONV_MODE] = {
84 { "idle", 0 },
85 { "continuous-conversion", 1 },
86 { "single-conversion", 2 },
87 { "power-down", 3 },
91 * ad7150 register access by I2C
94 static int ad7150_i2c_read(struct ad7150_chip_info *chip, u8 reg, u8 *data, int len)
96 struct i2c_client *client = chip->client;
97 int ret = 0;
99 ret = i2c_master_send(client, &reg, 1);
100 if (ret < 0) {
101 dev_err(&client->dev, "I2C write error\n");
102 return ret;
105 ret = i2c_master_recv(client, data, len);
106 if (ret < 0) {
107 dev_err(&client->dev, "I2C read error\n");
108 return ret;
111 return ret;
114 static int ad7150_i2c_write(struct ad7150_chip_info *chip, u8 reg, u8 data)
116 struct i2c_client *client = chip->client;
117 int ret = 0;
119 u8 tx[2] = {
120 reg,
121 data,
124 ret = i2c_master_send(client, tx, 2);
125 if (ret < 0)
126 dev_err(&client->dev, "I2C write error\n");
128 return ret;
132 * sysfs nodes
135 #define IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(_show) \
136 IIO_DEVICE_ATTR(available_conversion_modes, S_IRUGO, _show, NULL, 0)
137 #define IIO_DEV_ATTR_CONVERSION_MODE(_mode, _show, _store) \
138 IIO_DEVICE_ATTR(conversion_mode, _mode, _show, _store, 0)
139 #define IIO_DEV_ATTR_AVAIL_THRESHOLD_MODES(_show) \
140 IIO_DEVICE_ATTR(available_threshold_modes, S_IRUGO, _show, NULL, 0)
141 #define IIO_DEV_ATTR_THRESHOLD_MODE(_mode, _show, _store) \
142 IIO_DEVICE_ATTR(threshold_mode, _mode, _show, _store, 0)
143 #define IIO_DEV_ATTR_CH1_THRESHOLD(_mode, _show, _store) \
144 IIO_DEVICE_ATTR(ch1_threshold, _mode, _show, _store, 0)
145 #define IIO_DEV_ATTR_CH2_THRESHOLD(_mode, _show, _store) \
146 IIO_DEVICE_ATTR(ch2_threshold, _mode, _show, _store, 0)
147 #define IIO_DEV_ATTR_CH1_SENSITIVITY(_mode, _show, _store) \
148 IIO_DEVICE_ATTR(ch1_sensitivity, _mode, _show, _store, 0)
149 #define IIO_DEV_ATTR_CH2_SENSITIVITY(_mode, _show, _store) \
150 IIO_DEVICE_ATTR(ch2_sensitivity, _mode, _show, _store, 0)
151 #define IIO_DEV_ATTR_CH1_TIMEOUT(_mode, _show, _store) \
152 IIO_DEVICE_ATTR(ch1_timeout, _mode, _show, _store, 0)
153 #define IIO_DEV_ATTR_CH2_TIMEOUT(_mode, _show, _store) \
154 IIO_DEVICE_ATTR(ch2_timeout, _mode, _show, _store, 0)
155 #define IIO_DEV_ATTR_CH1_VALUE(_show) \
156 IIO_DEVICE_ATTR(ch1_value, S_IRUGO, _show, NULL, 0)
157 #define IIO_DEV_ATTR_CH2_VALUE(_show) \
158 IIO_DEVICE_ATTR(ch2_value, S_IRUGO, _show, NULL, 0)
159 #define IIO_DEV_ATTR_CH1_SETUP(_mode, _show, _store) \
160 IIO_DEVICE_ATTR(ch1_setup, _mode, _show, _store, 0)
161 #define IIO_DEV_ATTR_CH2_SETUP(_mode, _show, _store) \
162 IIO_DEVICE_ATTR(ch2_setup, _mode, _show, _store, 0)
163 #define IIO_DEV_ATTR_POWERDOWN_TIMER(_mode, _show, _store) \
164 IIO_DEVICE_ATTR(powerdown_timer, _mode, _show, _store, 0)
166 static ssize_t ad7150_show_conversion_modes(struct device *dev,
167 struct device_attribute *attr,
168 char *buf)
170 int i;
171 int len = 0;
173 for (i = 0; i < AD7150_MAX_CONV_MODE; i++)
174 len += sprintf(buf + len, "%s\n", ad7150_conv_mode_table[i].name);
176 return len;
179 static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad7150_show_conversion_modes);
181 static ssize_t ad7150_show_conversion_mode(struct device *dev,
182 struct device_attribute *attr,
183 char *buf)
185 struct iio_dev *dev_info = dev_get_drvdata(dev);
186 struct ad7150_chip_info *chip = iio_priv(dev_info);
188 return sprintf(buf, "%s\n", chip->conversion_mode);
191 static ssize_t ad7150_store_conversion_mode(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf,
194 size_t len)
196 struct iio_dev *dev_info = dev_get_drvdata(dev);
197 struct ad7150_chip_info *chip = iio_priv(dev_info);
198 u8 cfg;
199 int i;
201 ad7150_i2c_read(chip, AD7150_CFG, &cfg, 1);
203 for (i = 0; i < AD7150_MAX_CONV_MODE; i++) {
204 if (strncmp(buf, ad7150_conv_mode_table[i].name,
205 strlen(ad7150_conv_mode_table[i].name) - 1) == 0) {
206 chip->conversion_mode = ad7150_conv_mode_table[i].name;
207 cfg |= 0x18 | ad7150_conv_mode_table[i].reg_cfg;
208 ad7150_i2c_write(chip, AD7150_CFG, cfg);
209 return len;
213 dev_err(dev, "not supported conversion mode\n");
215 return -EINVAL;
218 static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR,
219 ad7150_show_conversion_mode,
220 ad7150_store_conversion_mode);
222 static ssize_t ad7150_show_threshold_modes(struct device *dev,
223 struct device_attribute *attr,
224 char *buf)
226 return sprintf(buf, "adaptive\nfixed\n");
229 static IIO_DEV_ATTR_AVAIL_THRESHOLD_MODES(ad7150_show_threshold_modes);
231 static ssize_t ad7150_show_ch1_value(struct device *dev,
232 struct device_attribute *attr,
233 char *buf)
235 struct iio_dev *dev_info = dev_get_drvdata(dev);
236 struct ad7150_chip_info *chip = iio_priv(dev_info);
237 u8 data[2];
239 ad7150_i2c_read(chip, AD7150_CH1_DATA_HIGH, data, 2);
240 return sprintf(buf, "%d\n", ((int) data[0] << 8) | data[1]);
243 static IIO_DEV_ATTR_CH1_VALUE(ad7150_show_ch1_value);
245 static ssize_t ad7150_show_ch2_value(struct device *dev,
246 struct device_attribute *attr,
247 char *buf)
249 struct iio_dev *dev_info = dev_get_drvdata(dev);
250 struct ad7150_chip_info *chip = iio_priv(dev_info);
251 u8 data[2];
253 ad7150_i2c_read(chip, AD7150_CH2_DATA_HIGH, data, 2);
254 return sprintf(buf, "%d\n", ((int) data[0] << 8) | data[1]);
257 static IIO_DEV_ATTR_CH2_VALUE(ad7150_show_ch2_value);
259 static ssize_t ad7150_show_threshold_mode(struct device *dev,
260 struct device_attribute *attr,
261 char *buf)
263 struct iio_dev *dev_info = dev_get_drvdata(dev);
264 struct ad7150_chip_info *chip = iio_priv(dev_info);
266 return sprintf(buf, "%s\n", chip->threshold_mode);
269 static ssize_t ad7150_store_threshold_mode(struct device *dev,
270 struct device_attribute *attr,
271 const char *buf,
272 size_t len)
274 struct iio_dev *dev_info = dev_get_drvdata(dev);
275 struct ad7150_chip_info *chip = iio_priv(dev_info);
276 u8 cfg;
278 ad7150_i2c_read(chip, AD7150_CFG, &cfg, 1);
280 if (strncmp(buf, "fixed", 5) == 0) {
281 strcpy(chip->threshold_mode, "fixed");
282 cfg |= AD7150_CFG_FIX;
283 ad7150_i2c_write(chip, AD7150_CFG, cfg);
285 return len;
286 } else if (strncmp(buf, "adaptive", 8) == 0) {
287 strcpy(chip->threshold_mode, "adaptive");
288 cfg &= ~AD7150_CFG_FIX;
289 ad7150_i2c_write(chip, AD7150_CFG, cfg);
291 return len;
294 dev_err(dev, "not supported threshold mode\n");
295 return -EINVAL;
298 static IIO_DEV_ATTR_THRESHOLD_MODE(S_IRUGO | S_IWUSR,
299 ad7150_show_threshold_mode,
300 ad7150_store_threshold_mode);
302 static ssize_t ad7150_show_ch1_threshold(struct device *dev,
303 struct device_attribute *attr,
304 char *buf)
306 struct iio_dev *dev_info = dev_get_drvdata(dev);
307 struct ad7150_chip_info *chip = iio_priv(dev_info);
309 return sprintf(buf, "%d\n", chip->ch1_threshold);
312 static ssize_t ad7150_store_ch1_threshold(struct device *dev,
313 struct device_attribute *attr,
314 const char *buf,
315 size_t len)
317 struct iio_dev *dev_info = dev_get_drvdata(dev);
318 struct ad7150_chip_info *chip = iio_priv(dev_info);
319 unsigned long data;
320 int ret;
322 ret = strict_strtoul(buf, 10, &data);
324 if ((!ret) && (data < 0x10000)) {
325 ad7150_i2c_write(chip, AD7150_CH1_THR_HOLD_H, data >> 8);
326 ad7150_i2c_write(chip, AD7150_CH1_THR_HOLD_L, data);
327 chip->ch1_threshold = data;
328 return len;
331 return -EINVAL;
334 static IIO_DEV_ATTR_CH1_THRESHOLD(S_IRUGO | S_IWUSR,
335 ad7150_show_ch1_threshold,
336 ad7150_store_ch1_threshold);
338 static ssize_t ad7150_show_ch2_threshold(struct device *dev,
339 struct device_attribute *attr,
340 char *buf)
342 struct iio_dev *dev_info = dev_get_drvdata(dev);
343 struct ad7150_chip_info *chip = iio_priv(dev_info);
345 return sprintf(buf, "%d\n", chip->ch2_threshold);
348 static ssize_t ad7150_store_ch2_threshold(struct device *dev,
349 struct device_attribute *attr,
350 const char *buf,
351 size_t len)
353 struct iio_dev *dev_info = dev_get_drvdata(dev);
354 struct ad7150_chip_info *chip = iio_priv(dev_info);
355 unsigned long data;
356 int ret;
358 ret = strict_strtoul(buf, 10, &data);
360 if ((!ret) && (data < 0x10000)) {
361 ad7150_i2c_write(chip, AD7150_CH2_THR_HOLD_H, data >> 8);
362 ad7150_i2c_write(chip, AD7150_CH2_THR_HOLD_L, data);
363 chip->ch2_threshold = data;
364 return len;
367 return -EINVAL;
370 static IIO_DEV_ATTR_CH2_THRESHOLD(S_IRUGO | S_IWUSR,
371 ad7150_show_ch2_threshold,
372 ad7150_store_ch2_threshold);
374 static ssize_t ad7150_show_ch1_sensitivity(struct device *dev,
375 struct device_attribute *attr,
376 char *buf)
378 struct iio_dev *dev_info = dev_get_drvdata(dev);
379 struct ad7150_chip_info *chip = iio_priv(dev_info);
381 return sprintf(buf, "%d\n", chip->ch1_sensitivity);
384 static ssize_t ad7150_store_ch1_sensitivity(struct device *dev,
385 struct device_attribute *attr,
386 const char *buf,
387 size_t len)
389 struct iio_dev *dev_info = dev_get_drvdata(dev);
390 struct ad7150_chip_info *chip = iio_priv(dev_info);
391 unsigned long data;
392 int ret;
394 ret = strict_strtoul(buf, 10, &data);
396 if ((!ret) && (data < 0x100)) {
397 ad7150_i2c_write(chip, AD7150_CH1_SENSITIVITY, data);
398 chip->ch1_sensitivity = data;
399 return len;
402 return -EINVAL;
405 static IIO_DEV_ATTR_CH1_SENSITIVITY(S_IRUGO | S_IWUSR,
406 ad7150_show_ch1_sensitivity,
407 ad7150_store_ch1_sensitivity);
409 static ssize_t ad7150_show_ch2_sensitivity(struct device *dev,
410 struct device_attribute *attr,
411 char *buf)
413 struct iio_dev *dev_info = dev_get_drvdata(dev);
414 struct ad7150_chip_info *chip = iio_priv(dev_info);
416 return sprintf(buf, "%d\n", chip->ch2_sensitivity);
419 static ssize_t ad7150_store_ch2_sensitivity(struct device *dev,
420 struct device_attribute *attr,
421 const char *buf,
422 size_t len)
424 struct iio_dev *dev_info = dev_get_drvdata(dev);
425 struct ad7150_chip_info *chip = iio_priv(dev_info);
426 unsigned long data;
427 int ret;
429 ret = strict_strtoul(buf, 10, &data);
431 if ((!ret) && (data < 0x100)) {
432 ad7150_i2c_write(chip, AD7150_CH2_SENSITIVITY, data);
433 chip->ch2_sensitivity = data;
434 return len;
437 return -EINVAL;
440 static IIO_DEV_ATTR_CH2_SENSITIVITY(S_IRUGO | S_IWUSR,
441 ad7150_show_ch2_sensitivity,
442 ad7150_store_ch2_sensitivity);
444 static ssize_t ad7150_show_ch1_timeout(struct device *dev,
445 struct device_attribute *attr,
446 char *buf)
448 struct iio_dev *dev_info = dev_get_drvdata(dev);
449 struct ad7150_chip_info *chip = iio_priv(dev_info);
451 return sprintf(buf, "%d\n", chip->ch1_timeout);
454 static ssize_t ad7150_store_ch1_timeout(struct device *dev,
455 struct device_attribute *attr,
456 const char *buf,
457 size_t len)
459 struct iio_dev *dev_info = dev_get_drvdata(dev);
460 struct ad7150_chip_info *chip = iio_priv(dev_info);
461 unsigned long data;
462 int ret;
464 ret = strict_strtoul(buf, 10, &data);
466 if ((!ret) && (data < 0x100)) {
467 ad7150_i2c_write(chip, AD7150_CH1_TIMEOUT, data);
468 chip->ch1_timeout = data;
469 return len;
472 return -EINVAL;
475 static IIO_DEV_ATTR_CH1_TIMEOUT(S_IRUGO | S_IWUSR,
476 ad7150_show_ch1_timeout,
477 ad7150_store_ch1_timeout);
479 static ssize_t ad7150_show_ch2_timeout(struct device *dev,
480 struct device_attribute *attr,
481 char *buf)
483 struct iio_dev *dev_info = dev_get_drvdata(dev);
484 struct ad7150_chip_info *chip = iio_priv(dev_info);
486 return sprintf(buf, "%d\n", chip->ch2_timeout);
489 static ssize_t ad7150_store_ch2_timeout(struct device *dev,
490 struct device_attribute *attr,
491 const char *buf,
492 size_t len)
494 struct iio_dev *dev_info = dev_get_drvdata(dev);
495 struct ad7150_chip_info *chip = iio_priv(dev_info);
496 unsigned long data;
497 int ret;
499 ret = strict_strtoul(buf, 10, &data);
501 if ((!ret) && (data < 0x100)) {
502 ad7150_i2c_write(chip, AD7150_CH2_TIMEOUT, data);
503 chip->ch2_timeout = data;
504 return len;
507 return -EINVAL;
510 static IIO_DEV_ATTR_CH2_TIMEOUT(S_IRUGO | S_IWUSR,
511 ad7150_show_ch2_timeout,
512 ad7150_store_ch2_timeout);
514 static ssize_t ad7150_show_ch1_setup(struct device *dev,
515 struct device_attribute *attr,
516 char *buf)
518 struct iio_dev *dev_info = dev_get_drvdata(dev);
519 struct ad7150_chip_info *chip = iio_priv(dev_info);
521 return sprintf(buf, "0x%02x\n", chip->ch1_setup);
524 static ssize_t ad7150_store_ch1_setup(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf,
527 size_t len)
529 struct iio_dev *dev_info = dev_get_drvdata(dev);
530 struct ad7150_chip_info *chip = iio_priv(dev_info);
531 unsigned long data;
532 int ret;
534 ret = strict_strtoul(buf, 10, &data);
536 if ((!ret) && (data < 0x100)) {
537 ad7150_i2c_write(chip, AD7150_CH1_SETUP, data);
538 chip->ch1_setup = data;
539 return len;
543 return -EINVAL;
546 static IIO_DEV_ATTR_CH1_SETUP(S_IRUGO | S_IWUSR,
547 ad7150_show_ch1_setup,
548 ad7150_store_ch1_setup);
550 static ssize_t ad7150_show_ch2_setup(struct device *dev,
551 struct device_attribute *attr,
552 char *buf)
554 struct iio_dev *dev_info = dev_get_drvdata(dev);
555 struct ad7150_chip_info *chip = iio_priv(dev_info);
557 return sprintf(buf, "0x%02x\n", chip->ch2_setup);
560 static ssize_t ad7150_store_ch2_setup(struct device *dev,
561 struct device_attribute *attr,
562 const char *buf,
563 size_t len)
565 struct iio_dev *dev_info = dev_get_drvdata(dev);
566 struct ad7150_chip_info *chip = iio_priv(dev_info);
567 unsigned long data;
568 int ret;
570 ret = strict_strtoul(buf, 10, &data);
572 if ((!ret) && (data < 0x100)) {
573 ad7150_i2c_write(chip, AD7150_CH2_SETUP, data);
574 chip->ch2_setup = data;
575 return len;
578 return -EINVAL;
581 static IIO_DEV_ATTR_CH2_SETUP(S_IRUGO | S_IWUSR,
582 ad7150_show_ch2_setup,
583 ad7150_store_ch2_setup);
585 static ssize_t ad7150_show_powerdown_timer(struct device *dev,
586 struct device_attribute *attr,
587 char *buf)
589 struct iio_dev *dev_info = dev_get_drvdata(dev);
590 struct ad7150_chip_info *chip = iio_priv(dev_info);
592 return sprintf(buf, "0x%02x\n", chip->powerdown_timer);
595 static ssize_t ad7150_store_powerdown_timer(struct device *dev,
596 struct device_attribute *attr,
597 const char *buf,
598 size_t len)
600 struct iio_dev *dev_info = dev_get_drvdata(dev);
601 struct ad7150_chip_info *chip = iio_priv(dev_info);
602 unsigned long data;
603 int ret;
605 ret = strict_strtoul(buf, 10, &data);
607 if ((!ret) && (data < 0x40)) {
608 chip->powerdown_timer = data;
609 return len;
612 return -EINVAL;
615 static IIO_DEV_ATTR_POWERDOWN_TIMER(S_IRUGO | S_IWUSR,
616 ad7150_show_powerdown_timer,
617 ad7150_store_powerdown_timer);
619 static struct attribute *ad7150_attributes[] = {
620 &iio_dev_attr_available_threshold_modes.dev_attr.attr,
621 &iio_dev_attr_threshold_mode.dev_attr.attr,
622 &iio_dev_attr_ch1_threshold.dev_attr.attr,
623 &iio_dev_attr_ch2_threshold.dev_attr.attr,
624 &iio_dev_attr_ch1_timeout.dev_attr.attr,
625 &iio_dev_attr_ch2_timeout.dev_attr.attr,
626 &iio_dev_attr_ch1_setup.dev_attr.attr,
627 &iio_dev_attr_ch2_setup.dev_attr.attr,
628 &iio_dev_attr_ch1_sensitivity.dev_attr.attr,
629 &iio_dev_attr_ch2_sensitivity.dev_attr.attr,
630 &iio_dev_attr_powerdown_timer.dev_attr.attr,
631 &iio_dev_attr_ch1_value.dev_attr.attr,
632 &iio_dev_attr_ch2_value.dev_attr.attr,
633 NULL,
636 static const struct attribute_group ad7150_attribute_group = {
637 .attrs = ad7150_attributes,
641 * threshold events
644 static irqreturn_t ad7150_event_handler(int irq, void *private)
646 struct iio_dev *indio_dev = private;
647 struct ad7150_chip_info *chip = iio_priv(indio_dev);
648 u8 int_status;
649 s64 timestamp = iio_get_time_ns();
651 ad7150_i2c_read(chip, AD7150_STATUS, &int_status, 1);
653 if ((int_status & AD7150_STATUS_OUT1) && !(chip->old_state & AD7150_STATUS_OUT1))
654 iio_push_event(indio_dev, 0,
655 IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_IN,
657 IIO_EV_TYPE_THRESH,
658 IIO_EV_DIR_RISING),
659 timestamp);
660 else if ((!(int_status & AD7150_STATUS_OUT1)) && (chip->old_state & AD7150_STATUS_OUT1))
661 iio_push_event(indio_dev, 0,
662 IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_IN,
664 IIO_EV_TYPE_THRESH,
665 IIO_EV_DIR_FALLING),
666 timestamp);
668 if ((int_status & AD7150_STATUS_OUT2) && !(chip->old_state & AD7150_STATUS_OUT2))
669 iio_push_event(indio_dev, 0,
670 IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_IN,
672 IIO_EV_TYPE_THRESH,
673 IIO_EV_DIR_RISING),
674 timestamp);
675 else if ((!(int_status & AD7150_STATUS_OUT2)) && (chip->old_state & AD7150_STATUS_OUT2))
676 iio_push_event(indio_dev, 0,
677 IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_IN,
679 IIO_EV_TYPE_THRESH,
680 IIO_EV_DIR_FALLING),
681 timestamp);
682 return IRQ_HANDLED;
685 static IIO_CONST_ATTR(ch1_high_en, "1");
686 static IIO_CONST_ATTR(ch2_high_en, "1");
687 static IIO_CONST_ATTR(ch1_low_en, "1");
688 static IIO_CONST_ATTR(ch2_low_en, "1");
690 static struct attribute *ad7150_event_attributes[] = {
691 &iio_const_attr_ch1_high_en.dev_attr.attr,
692 &iio_const_attr_ch2_high_en.dev_attr.attr,
693 &iio_const_attr_ch1_low_en.dev_attr.attr,
694 &iio_const_attr_ch2_low_en.dev_attr.attr,
695 NULL,
698 static struct attribute_group ad7150_event_attribute_group = {
699 .attrs = ad7150_event_attributes,
702 static const struct iio_info ad7150_info = {
703 .attrs = &ad7150_attribute_group,
704 .num_interrupt_lines = 1,
705 .event_attrs = &ad7150_event_attribute_group,
706 .driver_module = THIS_MODULE,
709 * device probe and remove
712 static int __devinit ad7150_probe(struct i2c_client *client,
713 const struct i2c_device_id *id)
715 int ret = 0, regdone = 0;
716 struct ad7150_chip_info *chip;
717 struct iio_dev *indio_dev;
719 indio_dev = iio_allocate_device(sizeof(*chip));
720 if (indio_dev == NULL) {
721 ret = -ENOMEM;
722 goto error_ret;
724 chip = iio_priv(indio_dev);
725 /* this is only used for device removal purposes */
726 i2c_set_clientdata(client, indio_dev);
728 chip->client = client;
730 /* Establish that the iio_dev is a child of the i2c device */
731 indio_dev->name = id->name;
732 indio_dev->dev.parent = &client->dev;
734 indio_dev->info = &ad7150_info;
736 indio_dev->modes = INDIO_DIRECT_MODE;
738 ret = iio_device_register(indio_dev);
739 if (ret)
740 goto error_free_dev;
741 regdone = 1;
743 if (client->irq) {
744 ret = request_threaded_irq(client->irq,
745 NULL,
746 &ad7150_event_handler,
747 IRQF_TRIGGER_RISING |
748 IRQF_TRIGGER_FALLING,
749 "ad7150",
750 indio_dev);
751 if (ret)
752 goto error_free_dev;
755 dev_err(&client->dev, "%s capacitive sensor registered, irq: %d\n", id->name, client->irq);
757 return 0;
759 error_free_dev:
760 if (regdone)
761 iio_device_unregister(indio_dev);
762 else
763 iio_free_device(indio_dev);
764 error_ret:
765 return ret;
768 static int __devexit ad7150_remove(struct i2c_client *client)
770 struct iio_dev *indio_dev = i2c_get_clientdata(client);
772 if (client->irq)
773 free_irq(client->irq, indio_dev);
774 iio_device_unregister(indio_dev);
776 return 0;
779 static const struct i2c_device_id ad7150_id[] = {
780 { "ad7150", 0 },
781 { "ad7151", 0 },
782 { "ad7156", 0 },
786 MODULE_DEVICE_TABLE(i2c, ad7150_id);
788 static struct i2c_driver ad7150_driver = {
789 .driver = {
790 .name = "ad7150",
792 .probe = ad7150_probe,
793 .remove = __devexit_p(ad7150_remove),
794 .id_table = ad7150_id,
797 static __init int ad7150_init(void)
799 return i2c_add_driver(&ad7150_driver);
802 static __exit void ad7150_exit(void)
804 i2c_del_driver(&ad7150_driver);
807 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
808 MODULE_DESCRIPTION("Analog Devices ad7150/1/6 capacitive sensor driver");
809 MODULE_LICENSE("GPL v2");
811 module_init(ad7150_init);
812 module_exit(ad7150_exit);