]> Git Repo - linux.git/blob - drivers/staging/iio/adc/ad7745.c
tracing: Replace syscall_meta_data struct array with pointer array
[linux.git] / drivers / staging / iio / adc / ad7745.c
1 /*
2  * AD774X capacitive sensor driver supporting AD7745/6/7
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  * AD774X registers definition
25  */
26
27 #define AD774X_STATUS           0
28 #define AD774X_STATUS_RDY       (1 << 2)
29 #define AD774X_STATUS_RDYVT     (1 << 1)
30 #define AD774X_STATUS_RDYCAP    (1 << 0)
31 #define AD774X_CAP_DATA_HIGH    1
32 #define AD774X_CAP_DATA_MID     2
33 #define AD774X_CAP_DATA_LOW     3
34 #define AD774X_VT_DATA_HIGH     4
35 #define AD774X_VT_DATA_MID      5
36 #define AD774X_VT_DATA_LOW      6
37 #define AD774X_CAP_SETUP        7
38 #define AD774X_VT_SETUP         8
39 #define AD774X_EXEC_SETUP       9
40 #define AD774X_CFG              10
41 #define AD774X_CAPDACA          11
42 #define AD774X_CAPDACB          12
43 #define AD774X_CAPDAC_EN        (1 << 7)
44 #define AD774X_CAP_OFFH         13
45 #define AD774X_CAP_OFFL         14
46 #define AD774X_CAP_GAINH        15
47 #define AD774X_CAP_GAINL        16
48 #define AD774X_VOLT_GAINH       17
49 #define AD774X_VOLT_GAINL       18
50
51 #define AD774X_MAX_CONV_MODE    6
52
53 /*
54  * struct ad774x_chip_info - chip specifc information
55  */
56
57 struct ad774x_chip_info {
58         const char *name;
59         struct i2c_client *client;
60         struct iio_dev *indio_dev;
61         struct work_struct thresh_work;
62         bool inter;
63         s64 last_timestamp;
64         u16 cap_offs;                   /* Capacitive offset */
65         u16 cap_gain;                   /* Capacitive gain calibration */
66         u16 volt_gain;                  /* Voltage gain calibration */
67         u8  cap_setup;
68         u8  vt_setup;
69         u8  exec_setup;
70
71         char *conversion_mode;
72 };
73
74 struct ad774x_conversion_mode {
75         char *name;
76         u8 reg_cfg;
77 };
78
79 struct ad774x_conversion_mode ad774x_conv_mode_table[AD774X_MAX_CONV_MODE] = {
80         { "idle", 0 },
81         { "continuous-conversion", 1 },
82         { "single-conversion", 2 },
83         { "power-down", 3 },
84         { "offset-calibration", 5 },
85         { "gain-calibration", 6 },
86 };
87
88 /*
89  * ad774x register access by I2C
90  */
91
92 static int ad774x_i2c_read(struct ad774x_chip_info *chip, u8 reg, u8 *data, int len)
93 {
94         struct i2c_client *client = chip->client;
95         int ret;
96
97         ret = i2c_master_send(client, &reg, 1);
98         if (ret < 0) {
99                 dev_err(&client->dev, "I2C write error\n");
100                 return ret;
101         }
102
103         ret = i2c_master_recv(client, data, len);
104         if (ret < 0) {
105                 dev_err(&client->dev, "I2C read error\n");
106                 return ret;
107         }
108
109         return ret;
110 }
111
112 static int ad774x_i2c_write(struct ad774x_chip_info *chip, u8 reg, u8 data)
113 {
114         struct i2c_client *client = chip->client;
115         int ret;
116
117         u8 tx[2] = {
118                 reg,
119                 data,
120         };
121
122         ret = i2c_master_send(client, tx, 2);
123         if (ret < 0)
124                 dev_err(&client->dev, "I2C write error\n");
125
126         return ret;
127 }
128
129 /*
130  * sysfs nodes
131  */
132
133 #define IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(_show)                              \
134         IIO_DEVICE_ATTR(available_conversion_modes, S_IRUGO, _show, NULL, 0)
135 #define IIO_DEV_ATTR_CONVERSION_MODE(_mode, _show, _store)              \
136         IIO_DEVICE_ATTR(conversion_mode, _mode, _show, _store, 0)
137 #define IIO_DEV_ATTR_CAP_SETUP(_mode, _show, _store)            \
138         IIO_DEVICE_ATTR(cap_setup, _mode, _show, _store, 0)
139 #define IIO_DEV_ATTR_VT_SETUP(_mode, _show, _store)              \
140         IIO_DEVICE_ATTR(in0_setup, _mode, _show, _store, 0)
141 #define IIO_DEV_ATTR_EXEC_SETUP(_mode, _show, _store)              \
142         IIO_DEVICE_ATTR(exec_setup, _mode, _show, _store, 0)
143 #define IIO_DEV_ATTR_VOLT_GAIN(_mode, _show, _store)            \
144         IIO_DEVICE_ATTR(in0_gain, _mode, _show, _store, 0)
145 #define IIO_DEV_ATTR_CAP_OFFS(_mode, _show, _store)             \
146         IIO_DEVICE_ATTR(cap_offs, _mode, _show, _store, 0)
147 #define IIO_DEV_ATTR_CAP_GAIN(_mode, _show, _store)             \
148         IIO_DEVICE_ATTR(cap_gain, _mode, _show, _store, 0)
149 #define IIO_DEV_ATTR_CAP_DATA(_show)            \
150         IIO_DEVICE_ATTR(cap0_raw, S_IRUGO, _show, NULL, 0)
151 #define IIO_DEV_ATTR_VT_DATA(_show)             \
152         IIO_DEVICE_ATTR(in0_raw, S_IRUGO, _show, NULL, 0)
153
154 static ssize_t ad774x_show_conversion_modes(struct device *dev,
155                 struct device_attribute *attr,
156                 char *buf)
157 {
158         int i;
159         int len = 0;
160
161         for (i = 0; i < AD774X_MAX_CONV_MODE; i++)
162                 len += sprintf(buf + len, "%s ", ad774x_conv_mode_table[i].name);
163
164         len += sprintf(buf + len, "\n");
165
166         return len;
167 }
168
169 static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad774x_show_conversion_modes);
170
171 static ssize_t ad774x_show_conversion_mode(struct device *dev,
172                 struct device_attribute *attr,
173                 char *buf)
174 {
175         struct iio_dev *dev_info = dev_get_drvdata(dev);
176         struct ad774x_chip_info *chip = dev_info->dev_data;
177
178         return sprintf(buf, "%s\n", chip->conversion_mode);
179 }
180
181 static ssize_t ad774x_store_conversion_mode(struct device *dev,
182                 struct device_attribute *attr,
183                 const char *buf,
184                 size_t len)
185 {
186         struct iio_dev *dev_info = dev_get_drvdata(dev);
187         struct ad774x_chip_info *chip = dev_info->dev_data;
188         u8 cfg;
189         int i;
190
191         ad774x_i2c_read(chip, AD774X_CFG, &cfg, 1);
192
193         for (i = 0; i < AD774X_MAX_CONV_MODE; i++) {
194                 if (strncmp(buf, ad774x_conv_mode_table[i].name,
195                                 strlen(ad774x_conv_mode_table[i].name) - 1) == 0) {
196                         chip->conversion_mode = ad774x_conv_mode_table[i].name;
197                         cfg |= 0x18 | ad774x_conv_mode_table[i].reg_cfg;
198                         ad774x_i2c_write(chip, AD774X_CFG, cfg);
199                         return len;
200                 }
201         }
202
203         dev_err(dev, "not supported conversion mode\n");
204
205         return -EINVAL;
206 }
207
208 static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR,
209                 ad774x_show_conversion_mode,
210                 ad774x_store_conversion_mode);
211
212 static ssize_t ad774x_show_dac_value(struct device *dev,
213                 struct device_attribute *attr,
214                 char *buf)
215 {
216         struct iio_dev *dev_info = dev_get_drvdata(dev);
217         struct ad774x_chip_info *chip = dev_info->dev_data;
218         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
219         u8 data;
220
221         ad774x_i2c_read(chip, this_attr->address, &data, 1);
222
223         return sprintf(buf, "%02x\n", data & 0x7F);
224 }
225
226 static ssize_t ad774x_store_dac_value(struct device *dev,
227                 struct device_attribute *attr,
228                 const char *buf,
229                 size_t len)
230 {
231         struct iio_dev *dev_info = dev_get_drvdata(dev);
232         struct ad774x_chip_info *chip = dev_info->dev_data;
233         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
234         unsigned long data;
235         int ret;
236
237         ret = strict_strtoul(buf, 10, &data);
238
239         if (!ret) {
240                 ad774x_i2c_write(chip, this_attr->address,
241                         (data ? AD774X_CAPDAC_EN : 0) | (data & 0x7F));
242                 return len;
243         }
244
245         return -EINVAL;
246 }
247
248 static IIO_DEVICE_ATTR(capdac0_raw, S_IRUGO | S_IWUSR,
249                         ad774x_show_dac_value,
250                         ad774x_store_dac_value,
251                         AD774X_CAPDACA);
252
253 static IIO_DEVICE_ATTR(capdac1_raw, S_IRUGO | S_IWUSR,
254                         ad774x_show_dac_value,
255                         ad774x_store_dac_value,
256                         AD774X_CAPDACB);
257
258 static ssize_t ad774x_show_cap_setup(struct device *dev,
259                 struct device_attribute *attr,
260                 char *buf)
261 {
262         struct iio_dev *dev_info = dev_get_drvdata(dev);
263         struct ad774x_chip_info *chip = dev_info->dev_data;
264
265         return sprintf(buf, "0x%02x\n", chip->cap_setup);
266 }
267
268 static ssize_t ad774x_store_cap_setup(struct device *dev,
269                 struct device_attribute *attr,
270                 const char *buf,
271                 size_t len)
272 {
273         struct iio_dev *dev_info = dev_get_drvdata(dev);
274         struct ad774x_chip_info *chip = dev_info->dev_data;
275         unsigned long data;
276         int ret;
277
278         ret = strict_strtoul(buf, 10, &data);
279
280         if ((!ret) && (data < 0x100)) {
281                 ad774x_i2c_write(chip, AD774X_CAP_SETUP, data);
282                 chip->cap_setup = data;
283                 return len;
284         }
285
286         return -EINVAL;
287 }
288
289 static IIO_DEV_ATTR_CAP_SETUP(S_IRUGO | S_IWUSR,
290                 ad774x_show_cap_setup,
291                 ad774x_store_cap_setup);
292
293 static ssize_t ad774x_show_vt_setup(struct device *dev,
294                 struct device_attribute *attr,
295                 char *buf)
296 {
297         struct iio_dev *dev_info = dev_get_drvdata(dev);
298         struct ad774x_chip_info *chip = dev_info->dev_data;
299
300         return sprintf(buf, "0x%02x\n", chip->vt_setup);
301 }
302
303 static ssize_t ad774x_store_vt_setup(struct device *dev,
304                 struct device_attribute *attr,
305                 const char *buf,
306                 size_t len)
307 {
308         struct iio_dev *dev_info = dev_get_drvdata(dev);
309         struct ad774x_chip_info *chip = dev_info->dev_data;
310         unsigned long data;
311         int ret;
312
313         ret = strict_strtoul(buf, 10, &data);
314
315         if ((!ret) && (data < 0x100)) {
316                 ad774x_i2c_write(chip, AD774X_VT_SETUP, data);
317                 chip->vt_setup = data;
318                 return len;
319         }
320
321         return -EINVAL;
322 }
323
324 static IIO_DEV_ATTR_VT_SETUP(S_IRUGO | S_IWUSR,
325                 ad774x_show_vt_setup,
326                 ad774x_store_vt_setup);
327
328 static ssize_t ad774x_show_exec_setup(struct device *dev,
329                 struct device_attribute *attr,
330                 char *buf)
331 {
332         struct iio_dev *dev_info = dev_get_drvdata(dev);
333         struct ad774x_chip_info *chip = dev_info->dev_data;
334
335         return sprintf(buf, "0x%02x\n", chip->exec_setup);
336 }
337
338 static ssize_t ad774x_store_exec_setup(struct device *dev,
339                 struct device_attribute *attr,
340                 const char *buf,
341                 size_t len)
342 {
343         struct iio_dev *dev_info = dev_get_drvdata(dev);
344         struct ad774x_chip_info *chip = dev_info->dev_data;
345         unsigned long data;
346         int ret;
347
348         ret = strict_strtoul(buf, 10, &data);
349
350         if ((!ret) && (data < 0x100)) {
351                 ad774x_i2c_write(chip, AD774X_EXEC_SETUP, data);
352                 chip->exec_setup = data;
353                 return len;
354         }
355
356         return -EINVAL;
357 }
358
359 static IIO_DEV_ATTR_EXEC_SETUP(S_IRUGO | S_IWUSR,
360                 ad774x_show_exec_setup,
361                 ad774x_store_exec_setup);
362
363 static ssize_t ad774x_show_volt_gain(struct device *dev,
364                 struct device_attribute *attr,
365                 char *buf)
366 {
367         struct iio_dev *dev_info = dev_get_drvdata(dev);
368         struct ad774x_chip_info *chip = dev_info->dev_data;
369
370         return sprintf(buf, "%d\n", chip->volt_gain);
371 }
372
373 static ssize_t ad774x_store_volt_gain(struct device *dev,
374                 struct device_attribute *attr,
375                 const char *buf,
376                 size_t len)
377 {
378         struct iio_dev *dev_info = dev_get_drvdata(dev);
379         struct ad774x_chip_info *chip = dev_info->dev_data;
380         unsigned long data;
381         int ret;
382
383         ret = strict_strtoul(buf, 10, &data);
384
385         if ((!ret) && (data < 0x10000)) {
386                 ad774x_i2c_write(chip, AD774X_VOLT_GAINH, data >> 8);
387                 ad774x_i2c_write(chip, AD774X_VOLT_GAINL, data);
388                 chip->volt_gain = data;
389                 return len;
390         }
391
392         return -EINVAL;
393 }
394
395 static IIO_DEV_ATTR_VOLT_GAIN(S_IRUGO | S_IWUSR,
396                 ad774x_show_volt_gain,
397                 ad774x_store_volt_gain);
398
399 static ssize_t ad774x_show_cap_data(struct device *dev,
400                 struct device_attribute *attr,
401                 char *buf)
402 {
403         struct iio_dev *dev_info = dev_get_drvdata(dev);
404         struct ad774x_chip_info *chip = dev_info->dev_data;
405         unsigned long data;
406         char tmp[3];
407
408         ad774x_i2c_read(chip, AD774X_CAP_DATA_HIGH, tmp, 3);
409         data = ((int)tmp[0] << 16) | ((int)tmp[1] << 8) | (int)tmp[2];
410
411         return sprintf(buf, "%ld\n", data);
412 }
413
414 static IIO_DEV_ATTR_CAP_DATA(ad774x_show_cap_data);
415
416 static ssize_t ad774x_show_vt_data(struct device *dev,
417                 struct device_attribute *attr,
418                 char *buf)
419 {
420         struct iio_dev *dev_info = dev_get_drvdata(dev);
421         struct ad774x_chip_info *chip = dev_info->dev_data;
422         unsigned long data;
423         char tmp[3];
424
425         ad774x_i2c_read(chip, AD774X_VT_DATA_HIGH, tmp, 3);
426         data = ((int)tmp[0] << 16) | ((int)tmp[1] << 8) | (int)tmp[2];
427
428         return sprintf(buf, "%ld\n", data);
429 }
430
431 static IIO_DEV_ATTR_VT_DATA(ad774x_show_vt_data);
432
433 static ssize_t ad774x_show_cap_offs(struct device *dev,
434                 struct device_attribute *attr,
435                 char *buf)
436 {
437         struct iio_dev *dev_info = dev_get_drvdata(dev);
438         struct ad774x_chip_info *chip = dev_info->dev_data;
439
440         return sprintf(buf, "%d\n", chip->cap_offs);
441 }
442
443 static ssize_t ad774x_store_cap_offs(struct device *dev,
444                 struct device_attribute *attr,
445                 const char *buf,
446                 size_t len)
447 {
448         struct iio_dev *dev_info = dev_get_drvdata(dev);
449         struct ad774x_chip_info *chip = dev_info->dev_data;
450         unsigned long data;
451         int ret;
452
453         ret = strict_strtoul(buf, 10, &data);
454
455         if ((!ret) && (data < 0x10000)) {
456                 ad774x_i2c_write(chip, AD774X_CAP_OFFH, data >> 8);
457                 ad774x_i2c_write(chip, AD774X_CAP_OFFL, data);
458                 chip->cap_offs = data;
459                 return len;
460         }
461
462         return -EINVAL;
463 }
464
465 static IIO_DEV_ATTR_CAP_OFFS(S_IRUGO | S_IWUSR,
466                 ad774x_show_cap_offs,
467                 ad774x_store_cap_offs);
468
469 static ssize_t ad774x_show_cap_gain(struct device *dev,
470                 struct device_attribute *attr,
471                 char *buf)
472 {
473         struct iio_dev *dev_info = dev_get_drvdata(dev);
474         struct ad774x_chip_info *chip = dev_info->dev_data;
475
476         return sprintf(buf, "%d\n", chip->cap_gain);
477 }
478
479 static ssize_t ad774x_store_cap_gain(struct device *dev,
480                 struct device_attribute *attr,
481                 const char *buf,
482                 size_t len)
483 {
484         struct iio_dev *dev_info = dev_get_drvdata(dev);
485         struct ad774x_chip_info *chip = dev_info->dev_data;
486         unsigned long data;
487         int ret;
488
489         ret = strict_strtoul(buf, 10, &data);
490
491         if ((!ret) && (data < 0x10000)) {
492                 ad774x_i2c_write(chip, AD774X_CAP_GAINH, data >> 8);
493                 ad774x_i2c_write(chip, AD774X_CAP_GAINL, data);
494                 chip->cap_gain = data;
495                 return len;
496         }
497
498         return -EINVAL;
499 }
500
501 static IIO_DEV_ATTR_CAP_GAIN(S_IRUGO | S_IWUSR,
502                 ad774x_show_cap_gain,
503                 ad774x_store_cap_gain);
504
505 static ssize_t ad774x_show_name(struct device *dev,
506                 struct device_attribute *attr,
507                 char *buf)
508 {
509         struct iio_dev *dev_info = dev_get_drvdata(dev);
510         struct ad774x_chip_info *chip = dev_info->dev_data;
511         return sprintf(buf, "%s\n", chip->name);
512 }
513
514 static IIO_DEVICE_ATTR(name, S_IRUGO, ad774x_show_name, NULL, 0);
515
516 static struct attribute *ad774x_attributes[] = {
517         &iio_dev_attr_available_conversion_modes.dev_attr.attr,
518         &iio_dev_attr_conversion_mode.dev_attr.attr,
519         &iio_dev_attr_cap_setup.dev_attr.attr,
520         &iio_dev_attr_in0_setup.dev_attr.attr,
521         &iio_dev_attr_exec_setup.dev_attr.attr,
522         &iio_dev_attr_cap_offs.dev_attr.attr,
523         &iio_dev_attr_cap_gain.dev_attr.attr,
524         &iio_dev_attr_in0_gain.dev_attr.attr,
525         &iio_dev_attr_in0_raw.dev_attr.attr,
526         &iio_dev_attr_cap0_raw.dev_attr.attr,
527         &iio_dev_attr_capdac0_raw.dev_attr.attr,
528         &iio_dev_attr_capdac1_raw.dev_attr.attr,
529         &iio_dev_attr_name.dev_attr.attr,
530         NULL,
531 };
532
533 static const struct attribute_group ad774x_attribute_group = {
534         .attrs = ad774x_attributes,
535 };
536
537 /*
538  * data ready events
539  */
540
541 #define IIO_EVENT_CODE_CAP_RDY     IIO_BUFFER_EVENT_CODE(0)
542 #define IIO_EVENT_CODE_VT_RDY      IIO_BUFFER_EVENT_CODE(1)
543
544 #define IIO_EVENT_ATTR_CAP_RDY_SH(_evlist, _show, _store, _mask)        \
545         IIO_EVENT_ATTR_SH(cap_rdy, _evlist, _show, _store, _mask)
546
547 #define IIO_EVENT_ATTR_VT_RDY_SH(_evlist, _show, _store, _mask) \
548         IIO_EVENT_ATTR_SH(vt_rdy, _evlist, _show, _store, _mask)
549
550 static void ad774x_interrupt_handler_bh(struct work_struct *work_s)
551 {
552         struct ad774x_chip_info *chip =
553                 container_of(work_s, struct ad774x_chip_info, thresh_work);
554         u8 int_status;
555
556         enable_irq(chip->client->irq);
557
558         ad774x_i2c_read(chip, AD774X_STATUS, &int_status, 1);
559
560         if (int_status & AD774X_STATUS_RDYCAP)
561                 iio_push_event(chip->indio_dev, 0,
562                                 IIO_EVENT_CODE_CAP_RDY,
563                                 chip->last_timestamp);
564
565         if (int_status & AD774X_STATUS_RDYVT)
566                 iio_push_event(chip->indio_dev, 0,
567                                 IIO_EVENT_CODE_VT_RDY,
568                                 chip->last_timestamp);
569 }
570
571 static int ad774x_interrupt_handler_th(struct iio_dev *dev_info,
572                 int index,
573                 s64 timestamp,
574                 int no_test)
575 {
576         struct ad774x_chip_info *chip = dev_info->dev_data;
577
578         chip->last_timestamp = timestamp;
579         schedule_work(&chip->thresh_work);
580
581         return 0;
582 }
583
584 IIO_EVENT_SH(data_rdy, &ad774x_interrupt_handler_th);
585
586 static ssize_t ad774x_query_out_mode(struct device *dev,
587                 struct device_attribute *attr,
588                 char *buf)
589 {
590         /*
591          * AD774X provides one /RDY pin, which can be used as interrupt
592          * but the pin is not configurable
593          */
594         return sprintf(buf, "1\n");
595 }
596
597 static ssize_t ad774x_set_out_mode(struct device *dev,
598                 struct device_attribute *attr,
599                 const char *buf,
600                 size_t len)
601 {
602         return len;
603 }
604
605 IIO_EVENT_ATTR_CAP_RDY_SH(iio_event_data_rdy, ad774x_query_out_mode, ad774x_set_out_mode, 0);
606 IIO_EVENT_ATTR_VT_RDY_SH(iio_event_data_rdy, ad774x_query_out_mode, ad774x_set_out_mode, 0);
607
608 static struct attribute *ad774x_event_attributes[] = {
609         &iio_event_attr_cap_rdy.dev_attr.attr,
610         &iio_event_attr_vt_rdy.dev_attr.attr,
611         NULL,
612 };
613
614 static struct attribute_group ad774x_event_attribute_group = {
615         .attrs = ad774x_event_attributes,
616 };
617
618 /*
619  * device probe and remove
620  */
621
622 static int __devinit ad774x_probe(struct i2c_client *client,
623                 const struct i2c_device_id *id)
624 {
625         int ret = 0, regdone = 0;
626         struct ad774x_chip_info *chip = kzalloc(sizeof(*chip), GFP_KERNEL);
627         if (chip == NULL) {
628                 ret = -ENOMEM;
629                 goto error_ret;
630         }
631
632         /* this is only used for device removal purposes */
633         i2c_set_clientdata(client, chip);
634
635         chip->client = client;
636         chip->name = id->name;
637
638         chip->indio_dev = iio_allocate_device();
639         if (chip->indio_dev == NULL) {
640                 ret = -ENOMEM;
641                 goto error_free_chip;
642         }
643
644         /* Establish that the iio_dev is a child of the i2c device */
645         chip->indio_dev->dev.parent = &client->dev;
646         chip->indio_dev->attrs = &ad774x_attribute_group;
647         chip->indio_dev->event_attrs = &ad774x_event_attribute_group;
648         chip->indio_dev->dev_data = (void *)(chip);
649         chip->indio_dev->driver_module = THIS_MODULE;
650         chip->indio_dev->num_interrupt_lines = 1;
651         chip->indio_dev->modes = INDIO_DIRECT_MODE;
652
653         ret = iio_device_register(chip->indio_dev);
654         if (ret)
655                 goto error_free_dev;
656         regdone = 1;
657
658         if (client->irq) {
659                 ret = iio_register_interrupt_line(client->irq,
660                                 chip->indio_dev,
661                                 0,
662                                 IRQF_TRIGGER_FALLING,
663                                 "ad774x");
664                 if (ret)
665                         goto error_free_dev;
666
667                 iio_add_event_to_list(iio_event_attr_cap_rdy.listel,
668                                 &chip->indio_dev->interrupts[0]->ev_list);
669
670                 INIT_WORK(&chip->thresh_work, ad774x_interrupt_handler_bh);
671         }
672
673         dev_err(&client->dev, "%s capacitive sensor registered, irq: %d\n", id->name, client->irq);
674
675         return 0;
676
677 error_free_dev:
678         if (regdone)
679                 iio_device_unregister(chip->indio_dev);
680         else
681                 iio_free_device(chip->indio_dev);
682 error_free_chip:
683         kfree(chip);
684 error_ret:
685         return ret;
686 }
687
688 static int __devexit ad774x_remove(struct i2c_client *client)
689 {
690         struct ad774x_chip_info *chip = i2c_get_clientdata(client);
691         struct iio_dev *indio_dev = chip->indio_dev;
692
693         if (client->irq)
694                 iio_unregister_interrupt_line(indio_dev, 0);
695         iio_device_unregister(indio_dev);
696         kfree(chip);
697
698         return 0;
699 }
700
701 static const struct i2c_device_id ad774x_id[] = {
702         { "ad7745", 0 },
703         { "ad7746", 0 },
704         { "ad7747", 0 },
705         {}
706 };
707
708 MODULE_DEVICE_TABLE(i2c, ad774x_id);
709
710 static struct i2c_driver ad774x_driver = {
711         .driver = {
712                 .name = "ad774x",
713         },
714         .probe = ad774x_probe,
715         .remove = __devexit_p(ad774x_remove),
716         .id_table = ad774x_id,
717 };
718
719 static __init int ad774x_init(void)
720 {
721         return i2c_add_driver(&ad774x_driver);
722 }
723
724 static __exit void ad774x_exit(void)
725 {
726         i2c_del_driver(&ad774x_driver);
727 }
728
729 MODULE_AUTHOR("Barry Song <[email protected]>");
730 MODULE_DESCRIPTION("Analog Devices ad7745/6/7 capacitive sensor driver");
731 MODULE_LICENSE("GPL v2");
732
733 module_init(ad774x_init);
734 module_exit(ad774x_exit);
This page took 0.073739 seconds and 4 git commands to generate.