Merge branch 'staging-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / staging / iio / adc / ad7150.c
1 /*
2  * AD7150 capacitive sensor driver supporting AD7150/1/6
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/gpio.h>
11 #include <linux/workqueue.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/list.h>
17 #include <linux/i2c.h>
18 #include <linux/rtc.h>
19
20 #include "../iio.h"
21 #include "../sysfs.h"
22
23 /*
24  * AD7150 registers definition
25  */
26
27 #define AD7150_STATUS              0
28 #define AD7150_STATUS_OUT1         (1 << 3)
29 #define AD7150_STATUS_OUT2         (1 << 5)
30 #define AD7150_CH1_DATA_HIGH       1
31 #define AD7150_CH1_DATA_LOW        2
32 #define AD7150_CH2_DATA_HIGH       3
33 #define AD7150_CH2_DATA_LOW        4
34 #define AD7150_CH1_AVG_HIGH        5
35 #define AD7150_CH1_AVG_LOW         6
36 #define AD7150_CH2_AVG_HIGH        7
37 #define AD7150_CH2_AVG_LOW         8
38 #define AD7150_CH1_SENSITIVITY     9
39 #define AD7150_CH1_THR_HOLD_H      9
40 #define AD7150_CH1_TIMEOUT         10
41 #define AD7150_CH1_THR_HOLD_L      10
42 #define AD7150_CH1_SETUP           11
43 #define AD7150_CH2_SENSITIVITY     12
44 #define AD7150_CH2_THR_HOLD_H      12
45 #define AD7150_CH2_TIMEOUT         13
46 #define AD7150_CH2_THR_HOLD_L      13
47 #define AD7150_CH2_SETUP           14
48 #define AD7150_CFG                 15
49 #define AD7150_CFG_FIX             (1 << 7)
50 #define AD7150_PD_TIMER            16
51 #define AD7150_CH1_CAPDAC          17
52 #define AD7150_CH2_CAPDAC          18
53 #define AD7150_SN3                 19
54 #define AD7150_SN2                 20
55 #define AD7150_SN1                 21
56 #define AD7150_SN0                 22
57 #define AD7150_ID                  23
58
59 #define AD7150_MAX_CONV_MODE       4
60
61 /*
62  * struct ad7150_chip_info - chip specifc information
63  */
64
65 struct ad7150_chip_info {
66         const char *name;
67         struct i2c_client *client;
68         struct iio_dev *indio_dev;
69         struct work_struct thresh_work;
70         bool inter;
71         s64 last_timestamp;
72         u16 ch1_threshold;     /* Ch1 Threshold (in fixed threshold mode) */
73         u8  ch1_sensitivity;   /* Ch1 Sensitivity (in adaptive threshold mode) */
74         u8  ch1_timeout;       /* Ch1 Timeout (in adaptive threshold mode) */
75         u8  ch1_setup;
76         u16 ch2_threshold;     /* Ch2 Threshold (in fixed threshold mode) */
77         u8  ch2_sensitivity;   /* Ch1 Sensitivity (in adaptive threshold mode) */
78         u8  ch2_timeout;       /* Ch1 Timeout (in adaptive threshold mode) */
79         u8  ch2_setup;
80         u8  powerdown_timer;
81         char threshold_mode[10]; /* adaptive/fixed threshold mode */
82         int old_state;
83         char *conversion_mode;
84 };
85
86 struct ad7150_conversion_mode {
87         char *name;
88         u8 reg_cfg;
89 };
90
91 struct ad7150_conversion_mode ad7150_conv_mode_table[AD7150_MAX_CONV_MODE] = {
92         { "idle", 0 },
93         { "continuous-conversion", 1 },
94         { "single-conversion", 2 },
95         { "power-down", 3 },
96 };
97
98 /*
99  * ad7150 register access by I2C
100  */
101
102 static int ad7150_i2c_read(struct ad7150_chip_info *chip, u8 reg, u8 *data, int len)
103 {
104         struct i2c_client *client = chip->client;
105         int ret = 0;
106
107         ret = i2c_master_send(client, &reg, 1);
108         if (ret < 0) {
109                 dev_err(&client->dev, "I2C write error\n");
110                 return ret;
111         }
112
113         ret = i2c_master_recv(client, data, len);
114         if (ret < 0) {
115                 dev_err(&client->dev, "I2C read error\n");
116                 return ret;
117         }
118
119         return ret;
120 }
121
122 static int ad7150_i2c_write(struct ad7150_chip_info *chip, u8 reg, u8 data)
123 {
124         struct i2c_client *client = chip->client;
125         int ret = 0;
126
127         u8 tx[2] = {
128                 reg,
129                 data,
130         };
131
132         ret = i2c_master_send(client, tx, 2);
133         if (ret < 0)
134                 dev_err(&client->dev, "I2C write error\n");
135
136         return ret;
137 }
138
139 /*
140  * sysfs nodes
141  */
142
143 #define IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(_show)                              \
144         IIO_DEVICE_ATTR(available_conversion_modes, S_IRUGO, _show, NULL, 0)
145 #define IIO_DEV_ATTR_CONVERSION_MODE(_mode, _show, _store)              \
146         IIO_DEVICE_ATTR(conversion_mode, _mode, _show, _store, 0)
147 #define IIO_DEV_ATTR_AVAIL_THRESHOLD_MODES(_show)                               \
148         IIO_DEVICE_ATTR(available_threshold_modes, S_IRUGO, _show, NULL, 0)
149 #define IIO_DEV_ATTR_THRESHOLD_MODE(_mode, _show, _store)               \
150         IIO_DEVICE_ATTR(threshold_mode, _mode, _show, _store, 0)
151 #define IIO_DEV_ATTR_CH1_THRESHOLD(_mode, _show, _store)              \
152         IIO_DEVICE_ATTR(ch1_threshold, _mode, _show, _store, 0)
153 #define IIO_DEV_ATTR_CH2_THRESHOLD(_mode, _show, _store)              \
154         IIO_DEVICE_ATTR(ch2_threshold, _mode, _show, _store, 0)
155 #define IIO_DEV_ATTR_CH1_SENSITIVITY(_mode, _show, _store)              \
156         IIO_DEVICE_ATTR(ch1_sensitivity, _mode, _show, _store, 0)
157 #define IIO_DEV_ATTR_CH2_SENSITIVITY(_mode, _show, _store)              \
158         IIO_DEVICE_ATTR(ch2_sensitivity, _mode, _show, _store, 0)
159 #define IIO_DEV_ATTR_CH1_TIMEOUT(_mode, _show, _store)          \
160         IIO_DEVICE_ATTR(ch1_timeout, _mode, _show, _store, 0)
161 #define IIO_DEV_ATTR_CH2_TIMEOUT(_mode, _show, _store)          \
162         IIO_DEVICE_ATTR(ch2_timeout, _mode, _show, _store, 0)
163 #define IIO_DEV_ATTR_CH1_VALUE(_show)           \
164         IIO_DEVICE_ATTR(ch1_value, S_IRUGO, _show, NULL, 0)
165 #define IIO_DEV_ATTR_CH2_VALUE(_show)           \
166         IIO_DEVICE_ATTR(ch2_value, S_IRUGO, _show, NULL, 0)
167 #define IIO_DEV_ATTR_CH1_SETUP(_mode, _show, _store)            \
168         IIO_DEVICE_ATTR(ch1_setup, _mode, _show, _store, 0)
169 #define IIO_DEV_ATTR_CH2_SETUP(_mode, _show, _store)              \
170         IIO_DEVICE_ATTR(ch2_setup, _mode, _show, _store, 0)
171 #define IIO_DEV_ATTR_POWERDOWN_TIMER(_mode, _show, _store)              \
172         IIO_DEVICE_ATTR(powerdown_timer, _mode, _show, _store, 0)
173
174 static ssize_t ad7150_show_conversion_modes(struct device *dev,
175                 struct device_attribute *attr,
176                 char *buf)
177 {
178         int i;
179         int len = 0;
180
181         for (i = 0; i < AD7150_MAX_CONV_MODE; i++)
182                 len += sprintf(buf + len, "%s\n", ad7150_conv_mode_table[i].name);
183
184         return len;
185 }
186
187 static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad7150_show_conversion_modes);
188
189 static ssize_t ad7150_show_conversion_mode(struct device *dev,
190                 struct device_attribute *attr,
191                 char *buf)
192 {
193         struct iio_dev *dev_info = dev_get_drvdata(dev);
194         struct ad7150_chip_info *chip = dev_info->dev_data;
195
196         return sprintf(buf, "%s\n", chip->conversion_mode);
197 }
198
199 static ssize_t ad7150_store_conversion_mode(struct device *dev,
200                 struct device_attribute *attr,
201                 const char *buf,
202                 size_t len)
203 {
204         struct iio_dev *dev_info = dev_get_drvdata(dev);
205         struct ad7150_chip_info *chip = dev_info->dev_data;
206         u8 cfg;
207         int i;
208
209         ad7150_i2c_read(chip, AD7150_CFG, &cfg, 1);
210
211         for (i = 0; i < AD7150_MAX_CONV_MODE; i++) {
212                 if (strncmp(buf, ad7150_conv_mode_table[i].name,
213                                 strlen(ad7150_conv_mode_table[i].name) - 1) == 0) {
214                         chip->conversion_mode = ad7150_conv_mode_table[i].name;
215                         cfg |= 0x18 | ad7150_conv_mode_table[i].reg_cfg;
216                         ad7150_i2c_write(chip, AD7150_CFG, cfg);
217                         return len;
218                 }
219         }
220
221         dev_err(dev, "not supported conversion mode\n");
222
223         return -EINVAL;
224 }
225
226 static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR,
227                 ad7150_show_conversion_mode,
228                 ad7150_store_conversion_mode);
229
230 static ssize_t ad7150_show_threshold_modes(struct device *dev,
231                 struct device_attribute *attr,
232                 char *buf)
233 {
234         return sprintf(buf, "adaptive\nfixed\n");
235 }
236
237 static IIO_DEV_ATTR_AVAIL_THRESHOLD_MODES(ad7150_show_threshold_modes);
238
239 static ssize_t ad7150_show_ch1_value(struct device *dev,
240                 struct device_attribute *attr,
241                 char *buf)
242 {
243         struct iio_dev *dev_info = dev_get_drvdata(dev);
244         struct ad7150_chip_info *chip = dev_info->dev_data;
245         u8 data[2];
246
247         ad7150_i2c_read(chip, AD7150_CH1_DATA_HIGH, data, 2);
248         return sprintf(buf, "%d\n", ((int) data[0] << 8) | data[1]);
249 }
250
251 static IIO_DEV_ATTR_CH1_VALUE(ad7150_show_ch1_value);
252
253 static ssize_t ad7150_show_ch2_value(struct device *dev,
254                 struct device_attribute *attr,
255                 char *buf)
256 {
257         struct iio_dev *dev_info = dev_get_drvdata(dev);
258         struct ad7150_chip_info *chip = dev_info->dev_data;
259         u8 data[2];
260
261         ad7150_i2c_read(chip, AD7150_CH2_DATA_HIGH, data, 2);
262         return sprintf(buf, "%d\n", ((int) data[0] << 8) | data[1]);
263 }
264
265 static IIO_DEV_ATTR_CH2_VALUE(ad7150_show_ch2_value);
266
267 static ssize_t ad7150_show_threshold_mode(struct device *dev,
268                 struct device_attribute *attr,
269                 char *buf)
270 {
271         struct iio_dev *dev_info = dev_get_drvdata(dev);
272         struct ad7150_chip_info *chip = dev_info->dev_data;
273
274         return sprintf(buf, "%s\n", chip->threshold_mode);
275 }
276
277 static ssize_t ad7150_store_threshold_mode(struct device *dev,
278                 struct device_attribute *attr,
279                 const char *buf,
280                 size_t len)
281 {
282         struct iio_dev *dev_info = dev_get_drvdata(dev);
283         struct ad7150_chip_info *chip = dev_info->dev_data;
284         u8 cfg;
285
286         ad7150_i2c_read(chip, AD7150_CFG, &cfg, 1);
287
288         if (strncmp(buf, "fixed", 5) == 0) {
289                 strcpy(chip->threshold_mode, "fixed");
290                 cfg |= AD7150_CFG_FIX;
291                 ad7150_i2c_write(chip, AD7150_CFG, cfg);
292
293                 return len;
294         } else if (strncmp(buf, "adaptive", 8) == 0) {
295                 strcpy(chip->threshold_mode, "adaptive");
296                 cfg &= ~AD7150_CFG_FIX;
297                 ad7150_i2c_write(chip, AD7150_CFG, cfg);
298
299                 return len;
300         }
301
302         dev_err(dev, "not supported threshold mode\n");
303         return -EINVAL;
304 }
305
306 static IIO_DEV_ATTR_THRESHOLD_MODE(S_IRUGO | S_IWUSR,
307                 ad7150_show_threshold_mode,
308                 ad7150_store_threshold_mode);
309
310 static ssize_t ad7150_show_ch1_threshold(struct device *dev,
311                 struct device_attribute *attr,
312                 char *buf)
313 {
314         struct iio_dev *dev_info = dev_get_drvdata(dev);
315         struct ad7150_chip_info *chip = dev_info->dev_data;
316
317         return sprintf(buf, "%d\n", chip->ch1_threshold);
318 }
319
320 static ssize_t ad7150_store_ch1_threshold(struct device *dev,
321                 struct device_attribute *attr,
322                 const char *buf,
323                 size_t len)
324 {
325         struct iio_dev *dev_info = dev_get_drvdata(dev);
326         struct ad7150_chip_info *chip = dev_info->dev_data;
327         unsigned long data;
328         int ret;
329
330         ret = strict_strtoul(buf, 10, &data);
331
332         if ((!ret) && (data < 0x10000)) {
333                 ad7150_i2c_write(chip, AD7150_CH1_THR_HOLD_H, data >> 8);
334                 ad7150_i2c_write(chip, AD7150_CH1_THR_HOLD_L, data);
335                 chip->ch1_threshold = data;
336                 return len;
337         }
338
339         return -EINVAL;
340 }
341
342 static IIO_DEV_ATTR_CH1_THRESHOLD(S_IRUGO | S_IWUSR,
343                 ad7150_show_ch1_threshold,
344                 ad7150_store_ch1_threshold);
345
346 static ssize_t ad7150_show_ch2_threshold(struct device *dev,
347                 struct device_attribute *attr,
348                 char *buf)
349 {
350         struct iio_dev *dev_info = dev_get_drvdata(dev);
351         struct ad7150_chip_info *chip = dev_info->dev_data;
352
353         return sprintf(buf, "%d\n", chip->ch2_threshold);
354 }
355
356 static ssize_t ad7150_store_ch2_threshold(struct device *dev,
357                 struct device_attribute *attr,
358                 const char *buf,
359                 size_t len)
360 {
361         struct iio_dev *dev_info = dev_get_drvdata(dev);
362         struct ad7150_chip_info *chip = dev_info->dev_data;
363         unsigned long data;
364         int ret;
365
366         ret = strict_strtoul(buf, 10, &data);
367
368         if ((!ret) && (data < 0x10000)) {
369                 ad7150_i2c_write(chip, AD7150_CH2_THR_HOLD_H, data >> 8);
370                 ad7150_i2c_write(chip, AD7150_CH2_THR_HOLD_L, data);
371                 chip->ch2_threshold = data;
372                 return len;
373         }
374
375         return -EINVAL;
376 }
377
378 static IIO_DEV_ATTR_CH2_THRESHOLD(S_IRUGO | S_IWUSR,
379                 ad7150_show_ch2_threshold,
380                 ad7150_store_ch2_threshold);
381
382 static ssize_t ad7150_show_ch1_sensitivity(struct device *dev,
383                 struct device_attribute *attr,
384                 char *buf)
385 {
386         struct iio_dev *dev_info = dev_get_drvdata(dev);
387         struct ad7150_chip_info *chip = dev_info->dev_data;
388
389         return sprintf(buf, "%d\n", chip->ch1_sensitivity);
390 }
391
392 static ssize_t ad7150_store_ch1_sensitivity(struct device *dev,
393                 struct device_attribute *attr,
394                 const char *buf,
395                 size_t len)
396 {
397         struct iio_dev *dev_info = dev_get_drvdata(dev);
398         struct ad7150_chip_info *chip = dev_info->dev_data;
399         unsigned long data;
400         int ret;
401
402         ret = strict_strtoul(buf, 10, &data);
403
404         if ((!ret) && (data < 0x100)) {
405                 ad7150_i2c_write(chip, AD7150_CH1_SENSITIVITY, data);
406                 chip->ch1_sensitivity = data;
407                 return len;
408         }
409
410         return -EINVAL;
411 }
412
413 static IIO_DEV_ATTR_CH1_SENSITIVITY(S_IRUGO | S_IWUSR,
414                 ad7150_show_ch1_sensitivity,
415                 ad7150_store_ch1_sensitivity);
416
417 static ssize_t ad7150_show_ch2_sensitivity(struct device *dev,
418                 struct device_attribute *attr,
419                 char *buf)
420 {
421         struct iio_dev *dev_info = dev_get_drvdata(dev);
422         struct ad7150_chip_info *chip = dev_info->dev_data;
423
424         return sprintf(buf, "%d\n", chip->ch2_sensitivity);
425 }
426
427 static ssize_t ad7150_store_ch2_sensitivity(struct device *dev,
428                 struct device_attribute *attr,
429                 const char *buf,
430                 size_t len)
431 {
432         struct iio_dev *dev_info = dev_get_drvdata(dev);
433         struct ad7150_chip_info *chip = dev_info->dev_data;
434         unsigned long data;
435         int ret;
436
437         ret = strict_strtoul(buf, 10, &data);
438
439         if ((!ret) && (data < 0x100)) {
440                 ad7150_i2c_write(chip, AD7150_CH2_SENSITIVITY, data);
441                 chip->ch2_sensitivity = data;
442                 return len;
443         }
444
445         return -EINVAL;
446 }
447
448 static IIO_DEV_ATTR_CH2_SENSITIVITY(S_IRUGO | S_IWUSR,
449                 ad7150_show_ch2_sensitivity,
450                 ad7150_store_ch2_sensitivity);
451
452 static ssize_t ad7150_show_ch1_timeout(struct device *dev,
453                 struct device_attribute *attr,
454                 char *buf)
455 {
456         struct iio_dev *dev_info = dev_get_drvdata(dev);
457         struct ad7150_chip_info *chip = dev_info->dev_data;
458
459         return sprintf(buf, "%d\n", chip->ch1_timeout);
460 }
461
462 static ssize_t ad7150_store_ch1_timeout(struct device *dev,
463                 struct device_attribute *attr,
464                 const char *buf,
465                 size_t len)
466 {
467         struct iio_dev *dev_info = dev_get_drvdata(dev);
468         struct ad7150_chip_info *chip = dev_info->dev_data;
469         unsigned long data;
470         int ret;
471
472         ret = strict_strtoul(buf, 10, &data);
473
474         if ((!ret) && (data < 0x100)) {
475                 ad7150_i2c_write(chip, AD7150_CH1_TIMEOUT, data);
476                 chip->ch1_timeout = data;
477                 return len;
478         }
479
480         return -EINVAL;
481 }
482
483 static IIO_DEV_ATTR_CH1_TIMEOUT(S_IRUGO | S_IWUSR,
484                 ad7150_show_ch1_timeout,
485                 ad7150_store_ch1_timeout);
486
487 static ssize_t ad7150_show_ch2_timeout(struct device *dev,
488                 struct device_attribute *attr,
489                 char *buf)
490 {
491         struct iio_dev *dev_info = dev_get_drvdata(dev);
492         struct ad7150_chip_info *chip = dev_info->dev_data;
493
494         return sprintf(buf, "%d\n", chip->ch2_timeout);
495 }
496
497 static ssize_t ad7150_store_ch2_timeout(struct device *dev,
498                 struct device_attribute *attr,
499                 const char *buf,
500                 size_t len)
501 {
502         struct iio_dev *dev_info = dev_get_drvdata(dev);
503         struct ad7150_chip_info *chip = dev_info->dev_data;
504         unsigned long data;
505         int ret;
506
507         ret = strict_strtoul(buf, 10, &data);
508
509         if ((!ret) && (data < 0x100)) {
510                 ad7150_i2c_write(chip, AD7150_CH2_TIMEOUT, data);
511                 chip->ch2_timeout = data;
512                 return len;
513         }
514
515         return -EINVAL;
516 }
517
518 static IIO_DEV_ATTR_CH2_TIMEOUT(S_IRUGO | S_IWUSR,
519                 ad7150_show_ch2_timeout,
520                 ad7150_store_ch2_timeout);
521
522 static ssize_t ad7150_show_ch1_setup(struct device *dev,
523                 struct device_attribute *attr,
524                 char *buf)
525 {
526         struct iio_dev *dev_info = dev_get_drvdata(dev);
527         struct ad7150_chip_info *chip = dev_info->dev_data;
528
529         return sprintf(buf, "0x%02x\n", chip->ch1_setup);
530 }
531
532 static ssize_t ad7150_store_ch1_setup(struct device *dev,
533                 struct device_attribute *attr,
534                 const char *buf,
535                 size_t len)
536 {
537         struct iio_dev *dev_info = dev_get_drvdata(dev);
538         struct ad7150_chip_info *chip = dev_info->dev_data;
539         unsigned long data;
540         int ret;
541
542         ret = strict_strtoul(buf, 10, &data);
543
544         if ((!ret) && (data < 0x100)) {
545                 ad7150_i2c_write(chip, AD7150_CH1_SETUP, data);
546                 chip->ch1_setup = data;
547                 return len;
548         }
549
550
551         return -EINVAL;
552 }
553
554 static IIO_DEV_ATTR_CH1_SETUP(S_IRUGO | S_IWUSR,
555                 ad7150_show_ch1_setup,
556                 ad7150_store_ch1_setup);
557
558 static ssize_t ad7150_show_ch2_setup(struct device *dev,
559                 struct device_attribute *attr,
560                 char *buf)
561 {
562         struct iio_dev *dev_info = dev_get_drvdata(dev);
563         struct ad7150_chip_info *chip = dev_info->dev_data;
564
565         return sprintf(buf, "0x%02x\n", chip->ch2_setup);
566 }
567
568 static ssize_t ad7150_store_ch2_setup(struct device *dev,
569                 struct device_attribute *attr,
570                 const char *buf,
571                 size_t len)
572 {
573         struct iio_dev *dev_info = dev_get_drvdata(dev);
574         struct ad7150_chip_info *chip = dev_info->dev_data;
575         unsigned long data;
576         int ret;
577
578         ret = strict_strtoul(buf, 10, &data);
579
580         if ((!ret) && (data < 0x100)) {
581                 ad7150_i2c_write(chip, AD7150_CH2_SETUP, data);
582                 chip->ch2_setup = data;
583                 return len;
584         }
585
586         return -EINVAL;
587 }
588
589 static IIO_DEV_ATTR_CH2_SETUP(S_IRUGO | S_IWUSR,
590                 ad7150_show_ch2_setup,
591                 ad7150_store_ch2_setup);
592
593 static ssize_t ad7150_show_name(struct device *dev,
594                 struct device_attribute *attr,
595                 char *buf)
596 {
597         struct iio_dev *dev_info = dev_get_drvdata(dev);
598         struct ad7150_chip_info *chip = dev_info->dev_data;
599         return sprintf(buf, "%s\n", chip->name);
600 }
601
602 static IIO_DEVICE_ATTR(name, S_IRUGO, ad7150_show_name, NULL, 0);
603
604 static ssize_t ad7150_show_powerdown_timer(struct device *dev,
605                 struct device_attribute *attr,
606                 char *buf)
607 {
608         struct iio_dev *dev_info = dev_get_drvdata(dev);
609         struct ad7150_chip_info *chip = dev_info->dev_data;
610
611         return sprintf(buf, "0x%02x\n", chip->powerdown_timer);
612 }
613
614 static ssize_t ad7150_store_powerdown_timer(struct device *dev,
615                 struct device_attribute *attr,
616                 const char *buf,
617                 size_t len)
618 {
619         struct iio_dev *dev_info = dev_get_drvdata(dev);
620         struct ad7150_chip_info *chip = dev_info->dev_data;
621         unsigned long data;
622         int ret;
623
624         ret = strict_strtoul(buf, 10, &data);
625
626         if ((!ret) && (data < 0x40)) {
627                 chip->powerdown_timer = data;
628                 return len;
629         }
630
631         return -EINVAL;
632 }
633
634 static IIO_DEV_ATTR_POWERDOWN_TIMER(S_IRUGO | S_IWUSR,
635                 ad7150_show_powerdown_timer,
636                 ad7150_store_powerdown_timer);
637
638 static struct attribute *ad7150_attributes[] = {
639         &iio_dev_attr_available_threshold_modes.dev_attr.attr,
640         &iio_dev_attr_threshold_mode.dev_attr.attr,
641         &iio_dev_attr_ch1_threshold.dev_attr.attr,
642         &iio_dev_attr_ch2_threshold.dev_attr.attr,
643         &iio_dev_attr_ch1_timeout.dev_attr.attr,
644         &iio_dev_attr_ch2_timeout.dev_attr.attr,
645         &iio_dev_attr_ch1_setup.dev_attr.attr,
646         &iio_dev_attr_ch2_setup.dev_attr.attr,
647         &iio_dev_attr_ch1_sensitivity.dev_attr.attr,
648         &iio_dev_attr_ch2_sensitivity.dev_attr.attr,
649         &iio_dev_attr_powerdown_timer.dev_attr.attr,
650         &iio_dev_attr_ch1_value.dev_attr.attr,
651         &iio_dev_attr_ch2_value.dev_attr.attr,
652         &iio_dev_attr_name.dev_attr.attr,
653         NULL,
654 };
655
656 static const struct attribute_group ad7150_attribute_group = {
657         .attrs = ad7150_attributes,
658 };
659
660 /*
661  * threshold events
662  */
663
664 #define IIO_EVENT_CODE_CH1_HIGH    IIO_BUFFER_EVENT_CODE(0)
665 #define IIO_EVENT_CODE_CH1_LOW     IIO_BUFFER_EVENT_CODE(1)
666 #define IIO_EVENT_CODE_CH2_HIGH    IIO_BUFFER_EVENT_CODE(2)
667 #define IIO_EVENT_CODE_CH2_LOW     IIO_BUFFER_EVENT_CODE(3)
668
669 #define IIO_EVENT_ATTR_CH1_HIGH_SH(_evlist, _show, _store, _mask)       \
670         IIO_EVENT_ATTR_SH(ch1_high, _evlist, _show, _store, _mask)
671
672 #define IIO_EVENT_ATTR_CH2_HIGH_SH(_evlist, _show, _store, _mask)       \
673         IIO_EVENT_ATTR_SH(ch2_high, _evlist, _show, _store, _mask)
674
675 #define IIO_EVENT_ATTR_CH1_LOW_SH(_evlist, _show, _store, _mask)        \
676         IIO_EVENT_ATTR_SH(ch1_low, _evlist, _show, _store, _mask)
677
678 #define IIO_EVENT_ATTR_CH2_LOW_SH(_evlist, _show, _store, _mask)        \
679         IIO_EVENT_ATTR_SH(ch2_low, _evlist, _show, _store, _mask)
680
681 static void ad7150_interrupt_handler_bh(struct work_struct *work_s)
682 {
683         struct ad7150_chip_info *chip =
684                 container_of(work_s, struct ad7150_chip_info, thresh_work);
685         u8 int_status;
686
687         enable_irq(chip->client->irq);
688
689         ad7150_i2c_read(chip, AD7150_STATUS, &int_status, 1);
690
691         if ((int_status & AD7150_STATUS_OUT1) && !(chip->old_state & AD7150_STATUS_OUT1))
692                 iio_push_event(chip->indio_dev, 0,
693                                 IIO_EVENT_CODE_CH1_HIGH,
694                                 chip->last_timestamp);
695         else if ((!(int_status & AD7150_STATUS_OUT1)) && (chip->old_state & AD7150_STATUS_OUT1))
696                 iio_push_event(chip->indio_dev, 0,
697                                 IIO_EVENT_CODE_CH1_LOW,
698                                 chip->last_timestamp);
699
700         if ((int_status & AD7150_STATUS_OUT2) && !(chip->old_state & AD7150_STATUS_OUT2))
701                 iio_push_event(chip->indio_dev, 0,
702                                 IIO_EVENT_CODE_CH2_HIGH,
703                                 chip->last_timestamp);
704         else if ((!(int_status & AD7150_STATUS_OUT2)) && (chip->old_state & AD7150_STATUS_OUT2))
705                 iio_push_event(chip->indio_dev, 0,
706                                 IIO_EVENT_CODE_CH2_LOW,
707                                 chip->last_timestamp);
708 }
709
710 static int ad7150_interrupt_handler_th(struct iio_dev *dev_info,
711                 int index,
712                 s64 timestamp,
713                 int no_test)
714 {
715         struct ad7150_chip_info *chip = dev_info->dev_data;
716
717         chip->last_timestamp = timestamp;
718         schedule_work(&chip->thresh_work);
719
720         return 0;
721 }
722
723 IIO_EVENT_SH(threshold, &ad7150_interrupt_handler_th);
724
725 static ssize_t ad7150_query_out_mode(struct device *dev,
726                 struct device_attribute *attr,
727                 char *buf)
728 {
729         /*
730          * AD7150 provides two logic output channels, which can be used as interrupt
731          * but the pins are not configurable
732          */
733         return sprintf(buf, "1\n");
734 }
735
736 static ssize_t ad7150_set_out_mode(struct device *dev,
737                 struct device_attribute *attr,
738                 const char *buf,
739                 size_t len)
740 {
741         return len;
742 }
743
744 IIO_EVENT_ATTR_CH1_HIGH_SH(iio_event_threshold, ad7150_query_out_mode, ad7150_set_out_mode, 0);
745 IIO_EVENT_ATTR_CH2_HIGH_SH(iio_event_threshold, ad7150_query_out_mode, ad7150_set_out_mode, 0);
746 IIO_EVENT_ATTR_CH1_LOW_SH(iio_event_threshold, ad7150_query_out_mode, ad7150_set_out_mode, 0);
747 IIO_EVENT_ATTR_CH2_LOW_SH(iio_event_threshold, ad7150_query_out_mode, ad7150_set_out_mode, 0);
748
749 static struct attribute *ad7150_event_attributes[] = {
750         &iio_event_attr_ch1_high.dev_attr.attr,
751         &iio_event_attr_ch2_high.dev_attr.attr,
752         &iio_event_attr_ch1_low.dev_attr.attr,
753         &iio_event_attr_ch2_low.dev_attr.attr,
754         NULL,
755 };
756
757 static struct attribute_group ad7150_event_attribute_group = {
758         .attrs = ad7150_event_attributes,
759 };
760
761 /*
762  * device probe and remove
763  */
764
765 static int __devinit ad7150_probe(struct i2c_client *client,
766                 const struct i2c_device_id *id)
767 {
768         int ret = 0, regdone = 0;
769         struct ad7150_chip_info *chip = kzalloc(sizeof(*chip), GFP_KERNEL);
770         if (chip == NULL) {
771                 ret = -ENOMEM;
772                 goto error_ret;
773         }
774
775         /* this is only used for device removal purposes */
776         i2c_set_clientdata(client, chip);
777
778         chip->client = client;
779         chip->name = id->name;
780
781         chip->indio_dev = iio_allocate_device();
782         if (chip->indio_dev == NULL) {
783                 ret = -ENOMEM;
784                 goto error_free_chip;
785         }
786
787         /* Echipabilish that the iio_dev is a child of the i2c device */
788         chip->indio_dev->dev.parent = &client->dev;
789         chip->indio_dev->attrs = &ad7150_attribute_group;
790         chip->indio_dev->event_attrs = &ad7150_event_attribute_group;
791         chip->indio_dev->dev_data = (void *)(chip);
792         chip->indio_dev->driver_module = THIS_MODULE;
793         chip->indio_dev->num_interrupt_lines = 1;
794         chip->indio_dev->modes = INDIO_DIRECT_MODE;
795
796         ret = iio_device_register(chip->indio_dev);
797         if (ret)
798                 goto error_free_dev;
799         regdone = 1;
800
801         if (client->irq && gpio_is_valid(irq_to_gpio(client->irq)) > 0) {
802                 ret = iio_register_interrupt_line(client->irq,
803                                 chip->indio_dev,
804                                 0,
805                                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
806                                 "ad7150");
807                 if (ret)
808                         goto error_free_dev;
809
810                 iio_add_event_to_list(iio_event_attr_ch2_low.listel,
811                                 &chip->indio_dev->interrupts[0]->ev_list);
812
813                 INIT_WORK(&chip->thresh_work, ad7150_interrupt_handler_bh);
814         }
815
816         dev_err(&client->dev, "%s capacitive sensor registered, irq: %d\n", id->name, client->irq);
817
818         return 0;
819
820 error_free_dev:
821         if (regdone)
822                 iio_device_unregister(chip->indio_dev);
823         else
824                 iio_free_device(chip->indio_dev);
825 error_free_chip:
826         kfree(chip);
827 error_ret:
828         return ret;
829 }
830
831 static int __devexit ad7150_remove(struct i2c_client *client)
832 {
833         struct ad7150_chip_info *chip = i2c_get_clientdata(client);
834         struct iio_dev *indio_dev = chip->indio_dev;
835
836         if (client->irq && gpio_is_valid(irq_to_gpio(client->irq)) > 0)
837                 iio_unregister_interrupt_line(indio_dev, 0);
838         iio_device_unregister(indio_dev);
839         kfree(chip);
840
841         return 0;
842 }
843
844 static const struct i2c_device_id ad7150_id[] = {
845         { "ad7150", 0 },
846         { "ad7151", 0 },
847         { "ad7156", 0 },
848         {}
849 };
850
851 MODULE_DEVICE_TABLE(i2c, ad7150_id);
852
853 static struct i2c_driver ad7150_driver = {
854         .driver = {
855                 .name = "ad7150",
856         },
857         .probe = ad7150_probe,
858         .remove = __devexit_p(ad7150_remove),
859         .id_table = ad7150_id,
860 };
861
862 static __init int ad7150_init(void)
863 {
864         return i2c_add_driver(&ad7150_driver);
865 }
866
867 static __exit void ad7150_exit(void)
868 {
869         i2c_del_driver(&ad7150_driver);
870 }
871
872 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
873 MODULE_DESCRIPTION("Analog Devices ad7150/1/6 capacitive sensor driver");
874 MODULE_LICENSE("GPL v2");
875
876 module_init(ad7150_init);
877 module_exit(ad7150_exit);