GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / iio / cdc / ad7150.c
1 /*
2  * AD7150 capacitive sensor driver supporting AD7150/1/6
3  *
4  * Copyright 2010-2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/bitfield.h>
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/events.h>
20 /*
21  * AD7150 registers definition
22  */
23
24 #define AD7150_STATUS              0
25 #define AD7150_STATUS_OUT1         BIT(3)
26 #define AD7150_STATUS_OUT2         BIT(5)
27 #define AD7150_CH1_DATA_HIGH       1
28 #define AD7150_CH2_DATA_HIGH       3
29 #define AD7150_CH1_AVG_HIGH        5
30 #define AD7150_CH2_AVG_HIGH        7
31 #define AD7150_CH1_SENSITIVITY     9
32 #define AD7150_CH1_THR_HOLD_H      9
33 #define AD7150_CH1_TIMEOUT         10
34 #define AD7150_CH1_SETUP           11
35 #define AD7150_CH2_SENSITIVITY     12
36 #define AD7150_CH2_THR_HOLD_H      12
37 #define AD7150_CH2_TIMEOUT         13
38 #define AD7150_CH2_SETUP           14
39 #define AD7150_CFG                 15
40 #define AD7150_CFG_FIX             BIT(7)
41 #define AD7150_PD_TIMER            16
42 #define AD7150_CH1_CAPDAC          17
43 #define AD7150_CH2_CAPDAC          18
44 #define AD7150_SN3                 19
45 #define AD7150_SN2                 20
46 #define AD7150_SN1                 21
47 #define AD7150_SN0                 22
48 #define AD7150_ID                  23
49
50 /**
51  * struct ad7150_chip_info - instance specific chip data
52  * @client: i2c client for this device
53  * @current_event: device always has one type of event enabled.
54  *      This element stores the event code of the current one.
55  * @threshold: thresholds for simple capacitance value events
56  * @thresh_sensitivity: threshold for simple capacitance offset
57  *      from 'average' value.
58  * @mag_sensitity: threshold for magnitude of capacitance offset from
59  *      from 'average' value.
60  * @thresh_timeout: a timeout, in samples from the moment an
61  *      adaptive threshold event occurs to when the average
62  *      value jumps to current value.
63  * @mag_timeout: a timeout, in sample from the moment an
64  *      adaptive magnitude event occurs to when the average
65  *      value jumps to the current value.
66  * @old_state: store state from previous event, allowing confirmation
67  *      of new condition.
68  * @conversion_mode: the current conversion mode.
69  * @state_lock: ensure consistent state of this structure wrt the
70  *      hardware.
71  */
72 struct ad7150_chip_info {
73         struct i2c_client *client;
74         u64 current_event;
75         u16 threshold[2][2];
76         u8 thresh_sensitivity[2][2];
77         u8 mag_sensitivity[2][2];
78         u8 thresh_timeout[2][2];
79         u8 mag_timeout[2][2];
80         int old_state;
81         char *conversion_mode;
82         struct mutex state_lock;
83 };
84
85 /*
86  * sysfs nodes
87  */
88
89 static const u8 ad7150_addresses[][6] = {
90         { AD7150_CH1_DATA_HIGH, AD7150_CH1_AVG_HIGH,
91           AD7150_CH1_SETUP, AD7150_CH1_THR_HOLD_H,
92           AD7150_CH1_SENSITIVITY, AD7150_CH1_TIMEOUT },
93         { AD7150_CH2_DATA_HIGH, AD7150_CH2_AVG_HIGH,
94           AD7150_CH2_SETUP, AD7150_CH2_THR_HOLD_H,
95           AD7150_CH2_SENSITIVITY, AD7150_CH2_TIMEOUT },
96 };
97
98 static int ad7150_read_raw(struct iio_dev *indio_dev,
99                            struct iio_chan_spec const *chan,
100                            int *val,
101                            int *val2,
102                            long mask)
103 {
104         int ret;
105         struct ad7150_chip_info *chip = iio_priv(indio_dev);
106
107         switch (mask) {
108         case IIO_CHAN_INFO_RAW:
109                 ret = i2c_smbus_read_word_data(chip->client,
110                                         ad7150_addresses[chan->channel][0]);
111                 if (ret < 0)
112                         return ret;
113                 *val = swab16(ret);
114                 return IIO_VAL_INT;
115         case IIO_CHAN_INFO_AVERAGE_RAW:
116                 ret = i2c_smbus_read_word_data(chip->client,
117                                         ad7150_addresses[chan->channel][1]);
118                 if (ret < 0)
119                         return ret;
120                 *val = swab16(ret);
121                 return IIO_VAL_INT;
122         default:
123                 return -EINVAL;
124         }
125 }
126
127 static int ad7150_read_event_config(struct iio_dev *indio_dev,
128         const struct iio_chan_spec *chan, enum iio_event_type type,
129         enum iio_event_direction dir)
130 {
131         int ret;
132         u8 threshtype;
133         bool thrfixed;
134         struct ad7150_chip_info *chip = iio_priv(indio_dev);
135
136         ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
137         if (ret < 0)
138                 return ret;
139
140         threshtype = (ret >> 5) & 0x03;
141
142         /*check if threshold mode is fixed or adaptive*/
143         thrfixed = FIELD_GET(AD7150_CFG_FIX, ret);
144
145         switch (type) {
146         case IIO_EV_TYPE_MAG_ADAPTIVE:
147                 if (dir == IIO_EV_DIR_RISING)
148                         return !thrfixed && (threshtype == 0x1);
149                 return !thrfixed && (threshtype == 0x0);
150         case IIO_EV_TYPE_THRESH_ADAPTIVE:
151                 if (dir == IIO_EV_DIR_RISING)
152                         return !thrfixed && (threshtype == 0x3);
153                 return !thrfixed && (threshtype == 0x2);
154         case IIO_EV_TYPE_THRESH:
155                 if (dir == IIO_EV_DIR_RISING)
156                         return thrfixed && (threshtype == 0x1);
157                 return thrfixed && (threshtype == 0x0);
158         default:
159                 break;
160         }
161         return -EINVAL;
162 }
163
164 /* lock should be held */
165 static int ad7150_write_event_params(struct iio_dev *indio_dev,
166                                      unsigned int chan,
167                                      enum iio_event_type type,
168                                      enum iio_event_direction dir)
169 {
170         int ret;
171         u16 value;
172         u8 sens, timeout;
173         struct ad7150_chip_info *chip = iio_priv(indio_dev);
174         int rising = (dir == IIO_EV_DIR_RISING);
175         u64 event_code;
176
177         event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir);
178
179         if (event_code != chip->current_event)
180                 return 0;
181
182         switch (type) {
183                 /* Note completely different from the adaptive versions */
184         case IIO_EV_TYPE_THRESH:
185                 value = chip->threshold[rising][chan];
186                 return i2c_smbus_write_word_data(chip->client,
187                                                 ad7150_addresses[chan][3],
188                                                 swab16(value));
189         case IIO_EV_TYPE_MAG_ADAPTIVE:
190                 sens = chip->mag_sensitivity[rising][chan];
191                 timeout = chip->mag_timeout[rising][chan];
192                 break;
193         case IIO_EV_TYPE_THRESH_ADAPTIVE:
194                 sens = chip->thresh_sensitivity[rising][chan];
195                 timeout = chip->thresh_timeout[rising][chan];
196                 break;
197         default:
198                 return -EINVAL;
199         }
200         ret = i2c_smbus_write_byte_data(chip->client,
201                                         ad7150_addresses[chan][4],
202                                         sens);
203         if (ret < 0)
204                 return ret;
205
206         ret = i2c_smbus_write_byte_data(chip->client,
207                                         ad7150_addresses[chan][5],
208                                         timeout);
209         if (ret < 0)
210                 return ret;
211
212         return 0;
213 }
214
215 static int ad7150_write_event_config(struct iio_dev *indio_dev,
216                                      const struct iio_chan_spec *chan,
217                                      enum iio_event_type type,
218                                      enum iio_event_direction dir, int state)
219 {
220         u8 thresh_type, cfg, adaptive;
221         int ret;
222         struct ad7150_chip_info *chip = iio_priv(indio_dev);
223         int rising = (dir == IIO_EV_DIR_RISING);
224         u64 event_code;
225
226         /* Something must always be turned on */
227         if (!state)
228                 return -EINVAL;
229
230         event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
231         if (event_code == chip->current_event)
232                 return 0;
233         mutex_lock(&chip->state_lock);
234         ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
235         if (ret < 0)
236                 goto error_ret;
237
238         cfg = ret & ~((0x03 << 5) | BIT(7));
239
240         switch (type) {
241         case IIO_EV_TYPE_MAG_ADAPTIVE:
242                 adaptive = 1;
243                 if (rising)
244                         thresh_type = 0x1;
245                 else
246                         thresh_type = 0x0;
247                 break;
248         case IIO_EV_TYPE_THRESH_ADAPTIVE:
249                 adaptive = 1;
250                 if (rising)
251                         thresh_type = 0x3;
252                 else
253                         thresh_type = 0x2;
254                 break;
255         case IIO_EV_TYPE_THRESH:
256                 adaptive = 0;
257                 if (rising)
258                         thresh_type = 0x1;
259                 else
260                         thresh_type = 0x0;
261                 break;
262         default:
263                 ret = -EINVAL;
264                 goto error_ret;
265         }
266
267         cfg |= (!adaptive << 7) | (thresh_type << 5);
268
269         ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
270         if (ret < 0)
271                 goto error_ret;
272
273         chip->current_event = event_code;
274
275         /* update control attributes */
276         ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
277 error_ret:
278         mutex_unlock(&chip->state_lock);
279
280         return ret;
281 }
282
283 static int ad7150_read_event_value(struct iio_dev *indio_dev,
284                                    const struct iio_chan_spec *chan,
285                                    enum iio_event_type type,
286                                    enum iio_event_direction dir,
287                                    enum iio_event_info info,
288                                    int *val, int *val2)
289 {
290         struct ad7150_chip_info *chip = iio_priv(indio_dev);
291         int rising = (dir == IIO_EV_DIR_RISING);
292
293         /* Complex register sharing going on here */
294         switch (type) {
295         case IIO_EV_TYPE_MAG_ADAPTIVE:
296                 *val = chip->mag_sensitivity[rising][chan->channel];
297                 return IIO_VAL_INT;
298         case IIO_EV_TYPE_THRESH_ADAPTIVE:
299                 *val = chip->thresh_sensitivity[rising][chan->channel];
300                 return IIO_VAL_INT;
301         case IIO_EV_TYPE_THRESH:
302                 *val = chip->threshold[rising][chan->channel];
303                 return IIO_VAL_INT;
304         default:
305                 return -EINVAL;
306         }
307 }
308
309 static int ad7150_write_event_value(struct iio_dev *indio_dev,
310                                     const struct iio_chan_spec *chan,
311                                     enum iio_event_type type,
312                                     enum iio_event_direction dir,
313                                     enum iio_event_info info,
314                                     int val, int val2)
315 {
316         int ret;
317         struct ad7150_chip_info *chip = iio_priv(indio_dev);
318         int rising = (dir == IIO_EV_DIR_RISING);
319
320         mutex_lock(&chip->state_lock);
321         switch (type) {
322         case IIO_EV_TYPE_MAG_ADAPTIVE:
323                 chip->mag_sensitivity[rising][chan->channel] = val;
324                 break;
325         case IIO_EV_TYPE_THRESH_ADAPTIVE:
326                 chip->thresh_sensitivity[rising][chan->channel] = val;
327                 break;
328         case IIO_EV_TYPE_THRESH:
329                 chip->threshold[rising][chan->channel] = val;
330                 break;
331         default:
332                 ret = -EINVAL;
333                 goto error_ret;
334         }
335
336         /* write back if active */
337         ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
338
339 error_ret:
340         mutex_unlock(&chip->state_lock);
341         return ret;
342 }
343
344 static ssize_t ad7150_show_timeout(struct device *dev,
345                                    struct device_attribute *attr,
346                                    char *buf)
347 {
348         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
349         struct ad7150_chip_info *chip = iio_priv(indio_dev);
350         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
351         u8 value;
352
353         /* use the event code for consistency reasons */
354         int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
355         int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
356                         == IIO_EV_DIR_RISING);
357
358         switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
359         case IIO_EV_TYPE_MAG_ADAPTIVE:
360                 value = chip->mag_timeout[rising][chan];
361                 break;
362         case IIO_EV_TYPE_THRESH_ADAPTIVE:
363                 value = chip->thresh_timeout[rising][chan];
364                 break;
365         default:
366                 return -EINVAL;
367         }
368
369         return sprintf(buf, "%d\n", value);
370 }
371
372 static ssize_t ad7150_store_timeout(struct device *dev,
373                                     struct device_attribute *attr,
374                                     const char *buf,
375                                     size_t len)
376 {
377         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
378         struct ad7150_chip_info *chip = iio_priv(indio_dev);
379         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
380         int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
381         enum iio_event_direction dir;
382         enum iio_event_type type;
383         int rising;
384         u8 data;
385         int ret;
386
387         type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
388         dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
389         rising = (dir == IIO_EV_DIR_RISING);
390
391         ret = kstrtou8(buf, 10, &data);
392         if (ret < 0)
393                 return ret;
394
395         mutex_lock(&chip->state_lock);
396         switch (type) {
397         case IIO_EV_TYPE_MAG_ADAPTIVE:
398                 chip->mag_timeout[rising][chan] = data;
399                 break;
400         case IIO_EV_TYPE_THRESH_ADAPTIVE:
401                 chip->thresh_timeout[rising][chan] = data;
402                 break;
403         default:
404                 ret = -EINVAL;
405                 goto error_ret;
406         }
407
408         ret = ad7150_write_event_params(indio_dev, chan, type, dir);
409 error_ret:
410         mutex_unlock(&chip->state_lock);
411
412         if (ret < 0)
413                 return ret;
414
415         return len;
416 }
417
418 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir)                \
419         IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
420                 0644,                                                   \
421                 &ad7150_show_timeout,                                   \
422                 &ad7150_store_timeout,                                  \
423                 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,                   \
424                                      chan,                              \
425                                      IIO_EV_TYPE_##ev_type,             \
426                                      IIO_EV_DIR_##ev_dir))
427 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
428 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
429 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
430 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
431 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
432 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
433 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
434 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
435
436 static const struct iio_event_spec ad7150_events[] = {
437         {
438                 .type = IIO_EV_TYPE_THRESH,
439                 .dir = IIO_EV_DIR_RISING,
440                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
441                         BIT(IIO_EV_INFO_ENABLE),
442         }, {
443                 .type = IIO_EV_TYPE_THRESH,
444                 .dir = IIO_EV_DIR_FALLING,
445                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
446                         BIT(IIO_EV_INFO_ENABLE),
447         }, {
448                 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
449                 .dir = IIO_EV_DIR_RISING,
450                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
451                         BIT(IIO_EV_INFO_ENABLE),
452         }, {
453                 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
454                 .dir = IIO_EV_DIR_FALLING,
455                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
456                         BIT(IIO_EV_INFO_ENABLE),
457         }, {
458                 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
459                 .dir = IIO_EV_DIR_RISING,
460                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
461                         BIT(IIO_EV_INFO_ENABLE),
462         }, {
463                 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
464                 .dir = IIO_EV_DIR_FALLING,
465                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
466                         BIT(IIO_EV_INFO_ENABLE),
467         },
468 };
469
470 static const struct iio_chan_spec ad7150_channels[] = {
471         {
472                 .type = IIO_CAPACITANCE,
473                 .indexed = 1,
474                 .channel = 0,
475                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
476                 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
477                 .event_spec = ad7150_events,
478                 .num_event_specs = ARRAY_SIZE(ad7150_events),
479         }, {
480                 .type = IIO_CAPACITANCE,
481                 .indexed = 1,
482                 .channel = 1,
483                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
484                 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
485                 .event_spec = ad7150_events,
486                 .num_event_specs = ARRAY_SIZE(ad7150_events),
487         },
488 };
489
490 /*
491  * threshold events
492  */
493
494 static irqreturn_t ad7150_event_handler(int irq, void *private)
495 {
496         struct iio_dev *indio_dev = private;
497         struct ad7150_chip_info *chip = iio_priv(indio_dev);
498         u8 int_status;
499         s64 timestamp = iio_get_time_ns(indio_dev);
500         int ret;
501
502         ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
503         if (ret < 0)
504                 return IRQ_HANDLED;
505
506         int_status = ret;
507
508         if ((int_status & AD7150_STATUS_OUT1) &&
509             !(chip->old_state & AD7150_STATUS_OUT1))
510                 iio_push_event(indio_dev,
511                                IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
512                                                     0,
513                                                     IIO_EV_TYPE_THRESH,
514                                                     IIO_EV_DIR_RISING),
515                                 timestamp);
516         else if ((!(int_status & AD7150_STATUS_OUT1)) &&
517                  (chip->old_state & AD7150_STATUS_OUT1))
518                 iio_push_event(indio_dev,
519                                IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
520                                                     0,
521                                                     IIO_EV_TYPE_THRESH,
522                                                     IIO_EV_DIR_FALLING),
523                                timestamp);
524
525         if ((int_status & AD7150_STATUS_OUT2) &&
526             !(chip->old_state & AD7150_STATUS_OUT2))
527                 iio_push_event(indio_dev,
528                                IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
529                                                     1,
530                                                     IIO_EV_TYPE_THRESH,
531                                                     IIO_EV_DIR_RISING),
532                                timestamp);
533         else if ((!(int_status & AD7150_STATUS_OUT2)) &&
534                  (chip->old_state & AD7150_STATUS_OUT2))
535                 iio_push_event(indio_dev,
536                                IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
537                                                     1,
538                                                     IIO_EV_TYPE_THRESH,
539                                                     IIO_EV_DIR_FALLING),
540                                timestamp);
541         /* store the status to avoid repushing same events */
542         chip->old_state = int_status;
543
544         return IRQ_HANDLED;
545 }
546
547 /* Timeouts not currently handled by core */
548 static struct attribute *ad7150_event_attributes[] = {
549         &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
550         .dev_attr.attr,
551         &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
552         .dev_attr.attr,
553         &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
554         .dev_attr.attr,
555         &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
556         .dev_attr.attr,
557         &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
558         .dev_attr.attr,
559         &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
560         .dev_attr.attr,
561         &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
562         .dev_attr.attr,
563         &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
564         .dev_attr.attr,
565         NULL,
566 };
567
568 static const struct attribute_group ad7150_event_attribute_group = {
569         .attrs = ad7150_event_attributes,
570         .name = "events",
571 };
572
573 static const struct iio_info ad7150_info = {
574         .event_attrs = &ad7150_event_attribute_group,
575         .driver_module = THIS_MODULE,
576         .read_raw = &ad7150_read_raw,
577         .read_event_config = &ad7150_read_event_config,
578         .write_event_config = &ad7150_write_event_config,
579         .read_event_value = &ad7150_read_event_value,
580         .write_event_value = &ad7150_write_event_value,
581 };
582
583 /*
584  * device probe and remove
585  */
586
587 static int ad7150_probe(struct i2c_client *client,
588                         const struct i2c_device_id *id)
589 {
590         int ret;
591         struct ad7150_chip_info *chip;
592         struct iio_dev *indio_dev;
593
594         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
595         if (!indio_dev)
596                 return -ENOMEM;
597         chip = iio_priv(indio_dev);
598         mutex_init(&chip->state_lock);
599         /* this is only used for device removal purposes */
600         i2c_set_clientdata(client, indio_dev);
601
602         chip->client = client;
603
604         indio_dev->name = id->name;
605         indio_dev->channels = ad7150_channels;
606         indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
607         /* Establish that the iio_dev is a child of the i2c device */
608         indio_dev->dev.parent = &client->dev;
609
610         indio_dev->info = &ad7150_info;
611
612         indio_dev->modes = INDIO_DIRECT_MODE;
613
614         if (client->irq) {
615                 ret = devm_request_threaded_irq(&client->dev, client->irq,
616                                                 NULL,
617                                                 &ad7150_event_handler,
618                                                 IRQF_TRIGGER_RISING |
619                                                 IRQF_TRIGGER_FALLING |
620                                                 IRQF_ONESHOT,
621                                                 "ad7150_irq1",
622                                                 indio_dev);
623                 if (ret)
624                         return ret;
625         }
626
627         if (client->dev.platform_data) {
628                 ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
629                                                 client->dev.platform_data,
630                                                 NULL,
631                                                 &ad7150_event_handler,
632                                                 IRQF_TRIGGER_RISING |
633                                                 IRQF_TRIGGER_FALLING |
634                                                 IRQF_ONESHOT,
635                                                 "ad7150_irq2",
636                                                 indio_dev);
637                 if (ret)
638                         return ret;
639         }
640
641         ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
642         if (ret)
643                 return ret;
644
645         dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
646                  id->name, client->irq);
647
648         return 0;
649 }
650
651 static const struct i2c_device_id ad7150_id[] = {
652         { "ad7150", 0 },
653         { "ad7151", 0 },
654         { "ad7156", 0 },
655         {}
656 };
657
658 MODULE_DEVICE_TABLE(i2c, ad7150_id);
659
660 static struct i2c_driver ad7150_driver = {
661         .driver = {
662                 .name = "ad7150",
663         },
664         .probe = ad7150_probe,
665         .id_table = ad7150_id,
666 };
667 module_i2c_driver(ad7150_driver);
668
669 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
670 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
671 MODULE_LICENSE("GPL v2");