GNU Linux-libre 4.19.264-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,
129                                     enum iio_event_type type,
130                                     enum iio_event_direction dir)
131 {
132         int ret;
133         u8 threshtype;
134         bool thrfixed;
135         struct ad7150_chip_info *chip = iio_priv(indio_dev);
136
137         ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
138         if (ret < 0)
139                 return ret;
140
141         threshtype = (ret >> 5) & 0x03;
142
143         /*check if threshold mode is fixed or adaptive*/
144         thrfixed = FIELD_GET(AD7150_CFG_FIX, ret);
145
146         switch (type) {
147         case IIO_EV_TYPE_MAG_ADAPTIVE:
148                 if (dir == IIO_EV_DIR_RISING)
149                         return !thrfixed && (threshtype == 0x1);
150                 return !thrfixed && (threshtype == 0x0);
151         case IIO_EV_TYPE_THRESH_ADAPTIVE:
152                 if (dir == IIO_EV_DIR_RISING)
153                         return !thrfixed && (threshtype == 0x3);
154                 return !thrfixed && (threshtype == 0x2);
155         case IIO_EV_TYPE_THRESH:
156                 if (dir == IIO_EV_DIR_RISING)
157                         return thrfixed && (threshtype == 0x1);
158                 return thrfixed && (threshtype == 0x0);
159         default:
160                 break;
161         }
162         return -EINVAL;
163 }
164
165 /* lock should be held */
166 static int ad7150_write_event_params(struct iio_dev *indio_dev,
167                                      unsigned int chan,
168                                      enum iio_event_type type,
169                                      enum iio_event_direction dir)
170 {
171         int ret;
172         u16 value;
173         u8 sens, timeout;
174         struct ad7150_chip_info *chip = iio_priv(indio_dev);
175         int rising = (dir == IIO_EV_DIR_RISING);
176         u64 event_code;
177
178         event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir);
179
180         if (event_code != chip->current_event)
181                 return 0;
182
183         switch (type) {
184                 /* Note completely different from the adaptive versions */
185         case IIO_EV_TYPE_THRESH:
186                 value = chip->threshold[rising][chan];
187                 return i2c_smbus_write_word_data(chip->client,
188                                                 ad7150_addresses[chan][3],
189                                                 swab16(value));
190         case IIO_EV_TYPE_MAG_ADAPTIVE:
191                 sens = chip->mag_sensitivity[rising][chan];
192                 timeout = chip->mag_timeout[rising][chan];
193                 break;
194         case IIO_EV_TYPE_THRESH_ADAPTIVE:
195                 sens = chip->thresh_sensitivity[rising][chan];
196                 timeout = chip->thresh_timeout[rising][chan];
197                 break;
198         default:
199                 return -EINVAL;
200         }
201         ret = i2c_smbus_write_byte_data(chip->client,
202                                         ad7150_addresses[chan][4],
203                                         sens);
204         if (ret < 0)
205                 return ret;
206
207         ret = i2c_smbus_write_byte_data(chip->client,
208                                         ad7150_addresses[chan][5],
209                                         timeout);
210         if (ret < 0)
211                 return ret;
212
213         return 0;
214 }
215
216 static int ad7150_write_event_config(struct iio_dev *indio_dev,
217                                      const struct iio_chan_spec *chan,
218                                      enum iio_event_type type,
219                                      enum iio_event_direction dir, int state)
220 {
221         u8 thresh_type, cfg, adaptive;
222         int ret;
223         struct ad7150_chip_info *chip = iio_priv(indio_dev);
224         int rising = (dir == IIO_EV_DIR_RISING);
225         u64 event_code;
226
227         /* Something must always be turned on */
228         if (!state)
229                 return -EINVAL;
230
231         event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
232         if (event_code == chip->current_event)
233                 return 0;
234         mutex_lock(&chip->state_lock);
235         ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
236         if (ret < 0)
237                 goto error_ret;
238
239         cfg = ret & ~((0x03 << 5) | BIT(7));
240
241         switch (type) {
242         case IIO_EV_TYPE_MAG_ADAPTIVE:
243                 adaptive = 1;
244                 if (rising)
245                         thresh_type = 0x1;
246                 else
247                         thresh_type = 0x0;
248                 break;
249         case IIO_EV_TYPE_THRESH_ADAPTIVE:
250                 adaptive = 1;
251                 if (rising)
252                         thresh_type = 0x3;
253                 else
254                         thresh_type = 0x2;
255                 break;
256         case IIO_EV_TYPE_THRESH:
257                 adaptive = 0;
258                 if (rising)
259                         thresh_type = 0x1;
260                 else
261                         thresh_type = 0x0;
262                 break;
263         default:
264                 ret = -EINVAL;
265                 goto error_ret;
266         }
267
268         cfg |= (!adaptive << 7) | (thresh_type << 5);
269
270         ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
271         if (ret < 0)
272                 goto error_ret;
273
274         chip->current_event = event_code;
275
276         /* update control attributes */
277         ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
278 error_ret:
279         mutex_unlock(&chip->state_lock);
280
281         return ret;
282 }
283
284 static int ad7150_read_event_value(struct iio_dev *indio_dev,
285                                    const struct iio_chan_spec *chan,
286                                    enum iio_event_type type,
287                                    enum iio_event_direction dir,
288                                    enum iio_event_info info,
289                                    int *val, int *val2)
290 {
291         struct ad7150_chip_info *chip = iio_priv(indio_dev);
292         int rising = (dir == IIO_EV_DIR_RISING);
293
294         /* Complex register sharing going on here */
295         switch (type) {
296         case IIO_EV_TYPE_MAG_ADAPTIVE:
297                 *val = chip->mag_sensitivity[rising][chan->channel];
298                 return IIO_VAL_INT;
299         case IIO_EV_TYPE_THRESH_ADAPTIVE:
300                 *val = chip->thresh_sensitivity[rising][chan->channel];
301                 return IIO_VAL_INT;
302         case IIO_EV_TYPE_THRESH:
303                 *val = chip->threshold[rising][chan->channel];
304                 return IIO_VAL_INT;
305         default:
306                 return -EINVAL;
307         }
308 }
309
310 static int ad7150_write_event_value(struct iio_dev *indio_dev,
311                                     const struct iio_chan_spec *chan,
312                                     enum iio_event_type type,
313                                     enum iio_event_direction dir,
314                                     enum iio_event_info info,
315                                     int val, int val2)
316 {
317         int ret;
318         struct ad7150_chip_info *chip = iio_priv(indio_dev);
319         int rising = (dir == IIO_EV_DIR_RISING);
320
321         mutex_lock(&chip->state_lock);
322         switch (type) {
323         case IIO_EV_TYPE_MAG_ADAPTIVE:
324                 chip->mag_sensitivity[rising][chan->channel] = val;
325                 break;
326         case IIO_EV_TYPE_THRESH_ADAPTIVE:
327                 chip->thresh_sensitivity[rising][chan->channel] = val;
328                 break;
329         case IIO_EV_TYPE_THRESH:
330                 chip->threshold[rising][chan->channel] = val;
331                 break;
332         default:
333                 ret = -EINVAL;
334                 goto error_ret;
335         }
336
337         /* write back if active */
338         ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
339
340 error_ret:
341         mutex_unlock(&chip->state_lock);
342         return ret;
343 }
344
345 static ssize_t ad7150_show_timeout(struct device *dev,
346                                    struct device_attribute *attr,
347                                    char *buf)
348 {
349         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
350         struct ad7150_chip_info *chip = iio_priv(indio_dev);
351         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
352         u8 value;
353
354         /* use the event code for consistency reasons */
355         int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
356         int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
357                         == IIO_EV_DIR_RISING);
358
359         switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
360         case IIO_EV_TYPE_MAG_ADAPTIVE:
361                 value = chip->mag_timeout[rising][chan];
362                 break;
363         case IIO_EV_TYPE_THRESH_ADAPTIVE:
364                 value = chip->thresh_timeout[rising][chan];
365                 break;
366         default:
367                 return -EINVAL;
368         }
369
370         return sprintf(buf, "%d\n", value);
371 }
372
373 static ssize_t ad7150_store_timeout(struct device *dev,
374                                     struct device_attribute *attr,
375                                     const char *buf,
376                                     size_t len)
377 {
378         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
379         struct ad7150_chip_info *chip = iio_priv(indio_dev);
380         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
381         int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
382         enum iio_event_direction dir;
383         enum iio_event_type type;
384         int rising;
385         u8 data;
386         int ret;
387
388         type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
389         dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
390         rising = (dir == IIO_EV_DIR_RISING);
391
392         ret = kstrtou8(buf, 10, &data);
393         if (ret < 0)
394                 return ret;
395
396         mutex_lock(&chip->state_lock);
397         switch (type) {
398         case IIO_EV_TYPE_MAG_ADAPTIVE:
399                 chip->mag_timeout[rising][chan] = data;
400                 break;
401         case IIO_EV_TYPE_THRESH_ADAPTIVE:
402                 chip->thresh_timeout[rising][chan] = data;
403                 break;
404         default:
405                 ret = -EINVAL;
406                 goto error_ret;
407         }
408
409         ret = ad7150_write_event_params(indio_dev, chan, type, dir);
410 error_ret:
411         mutex_unlock(&chip->state_lock);
412
413         if (ret < 0)
414                 return ret;
415
416         return len;
417 }
418
419 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir)                \
420         IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
421                 0644,                                                   \
422                 &ad7150_show_timeout,                                   \
423                 &ad7150_store_timeout,                                  \
424                 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,                   \
425                                      chan,                              \
426                                      IIO_EV_TYPE_##ev_type,             \
427                                      IIO_EV_DIR_##ev_dir))
428 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
429 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
430 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
431 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
432 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
433 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
434 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
435 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
436
437 static const struct iio_event_spec ad7150_events[] = {
438         {
439                 .type = IIO_EV_TYPE_THRESH,
440                 .dir = IIO_EV_DIR_RISING,
441                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
442                         BIT(IIO_EV_INFO_ENABLE),
443         }, {
444                 .type = IIO_EV_TYPE_THRESH,
445                 .dir = IIO_EV_DIR_FALLING,
446                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
447                         BIT(IIO_EV_INFO_ENABLE),
448         }, {
449                 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
450                 .dir = IIO_EV_DIR_RISING,
451                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
452                         BIT(IIO_EV_INFO_ENABLE),
453         }, {
454                 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
455                 .dir = IIO_EV_DIR_FALLING,
456                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
457                         BIT(IIO_EV_INFO_ENABLE),
458         }, {
459                 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
460                 .dir = IIO_EV_DIR_RISING,
461                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
462                         BIT(IIO_EV_INFO_ENABLE),
463         }, {
464                 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
465                 .dir = IIO_EV_DIR_FALLING,
466                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
467                         BIT(IIO_EV_INFO_ENABLE),
468         },
469 };
470
471 static const struct iio_chan_spec ad7150_channels[] = {
472         {
473                 .type = IIO_CAPACITANCE,
474                 .indexed = 1,
475                 .channel = 0,
476                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
477                 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
478                 .event_spec = ad7150_events,
479                 .num_event_specs = ARRAY_SIZE(ad7150_events),
480         }, {
481                 .type = IIO_CAPACITANCE,
482                 .indexed = 1,
483                 .channel = 1,
484                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
485                 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
486                 .event_spec = ad7150_events,
487                 .num_event_specs = ARRAY_SIZE(ad7150_events),
488         },
489 };
490
491 /*
492  * threshold events
493  */
494
495 static irqreturn_t ad7150_event_handler(int irq, void *private)
496 {
497         struct iio_dev *indio_dev = private;
498         struct ad7150_chip_info *chip = iio_priv(indio_dev);
499         u8 int_status;
500         s64 timestamp = iio_get_time_ns(indio_dev);
501         int ret;
502
503         ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
504         if (ret < 0)
505                 return IRQ_HANDLED;
506
507         int_status = ret;
508
509         if ((int_status & AD7150_STATUS_OUT1) &&
510             !(chip->old_state & AD7150_STATUS_OUT1))
511                 iio_push_event(indio_dev,
512                                IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
513                                                     0,
514                                                     IIO_EV_TYPE_THRESH,
515                                                     IIO_EV_DIR_RISING),
516                                 timestamp);
517         else if ((!(int_status & AD7150_STATUS_OUT1)) &&
518                  (chip->old_state & AD7150_STATUS_OUT1))
519                 iio_push_event(indio_dev,
520                                IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
521                                                     0,
522                                                     IIO_EV_TYPE_THRESH,
523                                                     IIO_EV_DIR_FALLING),
524                                timestamp);
525
526         if ((int_status & AD7150_STATUS_OUT2) &&
527             !(chip->old_state & AD7150_STATUS_OUT2))
528                 iio_push_event(indio_dev,
529                                IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
530                                                     1,
531                                                     IIO_EV_TYPE_THRESH,
532                                                     IIO_EV_DIR_RISING),
533                                timestamp);
534         else if ((!(int_status & AD7150_STATUS_OUT2)) &&
535                  (chip->old_state & AD7150_STATUS_OUT2))
536                 iio_push_event(indio_dev,
537                                IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
538                                                     1,
539                                                     IIO_EV_TYPE_THRESH,
540                                                     IIO_EV_DIR_FALLING),
541                                timestamp);
542         /* store the status to avoid repushing same events */
543         chip->old_state = int_status;
544
545         return IRQ_HANDLED;
546 }
547
548 /* Timeouts not currently handled by core */
549 static struct attribute *ad7150_event_attributes[] = {
550         &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
551         .dev_attr.attr,
552         &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
553         .dev_attr.attr,
554         &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
555         .dev_attr.attr,
556         &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
557         .dev_attr.attr,
558         &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
559         .dev_attr.attr,
560         &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
561         .dev_attr.attr,
562         &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
563         .dev_attr.attr,
564         &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
565         .dev_attr.attr,
566         NULL,
567 };
568
569 static const struct attribute_group ad7150_event_attribute_group = {
570         .attrs = ad7150_event_attributes,
571         .name = "events",
572 };
573
574 static const struct iio_info ad7150_info = {
575         .event_attrs = &ad7150_event_attribute_group,
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");