]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/iio/adc/ad799x.c
staging: cxt1e1: remove driver
[karo-tx-linux.git] / drivers / iio / adc / ad799x.c
1 /*
2  * iio/adc/ad799x.c
3  * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc.
4  *
5  * based on iio/adc/max1363
6  * Copyright (C) 2008-2010 Jonathan Cameron
7  *
8  * based on linux/drivers/i2c/chips/max123x
9  * Copyright (C) 2002-2004 Stefan Eletzhofer
10  *
11  * based on linux/drivers/acron/char/pcf8583.c
12  * Copyright (C) 2000 Russell King
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License version 2 as
16  * published by the Free Software Foundation.
17  *
18  * ad799x.c
19  *
20  * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
21  * ad7998 and similar chips.
22  *
23  */
24
25 #include <linux/interrupt.h>
26 #include <linux/device.h>
27 #include <linux/kernel.h>
28 #include <linux/sysfs.h>
29 #include <linux/i2c.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/err.h>
34 #include <linux/module.h>
35
36 #include <linux/iio/iio.h>
37 #include <linux/iio/sysfs.h>
38 #include <linux/iio/events.h>
39 #include <linux/iio/buffer.h>
40 #include <linux/iio/trigger_consumer.h>
41 #include <linux/iio/triggered_buffer.h>
42
43 #define AD799X_CHANNEL_SHIFT                    4
44 #define AD799X_STORAGEBITS                      16
45 /*
46  * AD7991, AD7995 and AD7999 defines
47  */
48
49 #define AD7991_REF_SEL                          0x08
50 #define AD7991_FLTR                             0x04
51 #define AD7991_BIT_TRIAL_DELAY                  0x02
52 #define AD7991_SAMPLE_DELAY                     0x01
53
54 /*
55  * AD7992, AD7993, AD7994, AD7997 and AD7998 defines
56  */
57
58 #define AD7998_FLTR                             0x08
59 #define AD7998_ALERT_EN                         0x04
60 #define AD7998_BUSY_ALERT                       0x02
61 #define AD7998_BUSY_ALERT_POL                   0x01
62
63 #define AD7998_CONV_RES_REG                     0x0
64 #define AD7998_ALERT_STAT_REG                   0x1
65 #define AD7998_CONF_REG                         0x2
66 #define AD7998_CYCLE_TMR_REG                    0x3
67
68 #define AD7998_DATALOW_REG(x)                   ((x) * 3 + 0x4)
69 #define AD7998_DATAHIGH_REG(x)                  ((x) * 3 + 0x5)
70 #define AD7998_HYST_REG(x)                      ((x) * 3 + 0x6)
71
72 #define AD7998_CYC_MASK                         0x7
73 #define AD7998_CYC_DIS                          0x0
74 #define AD7998_CYC_TCONF_32                     0x1
75 #define AD7998_CYC_TCONF_64                     0x2
76 #define AD7998_CYC_TCONF_128                    0x3
77 #define AD7998_CYC_TCONF_256                    0x4
78 #define AD7998_CYC_TCONF_512                    0x5
79 #define AD7998_CYC_TCONF_1024                   0x6
80 #define AD7998_CYC_TCONF_2048                   0x7
81
82 #define AD7998_ALERT_STAT_CLEAR                 0xFF
83
84 /*
85  * AD7997 and AD7997 defines
86  */
87
88 #define AD7997_8_READ_SINGLE                    0x80
89 #define AD7997_8_READ_SEQUENCE                  0x70
90 /* TODO: move this into a common header */
91 #define RES_MASK(bits)  ((1 << (bits)) - 1)
92
93 enum {
94         ad7991,
95         ad7995,
96         ad7999,
97         ad7992,
98         ad7993,
99         ad7994,
100         ad7997,
101         ad7998
102 };
103
104 /**
105  * struct ad799x_chip_info - chip specific information
106  * @channel:            channel specification
107  * @num_channels:       number of channels
108  * @default_config:     device default configuration
109  * @info:               pointer to iio_info struct
110  */
111 struct ad799x_chip_info {
112         struct iio_chan_spec            channel[9];
113         int                             num_channels;
114         u16                             default_config;
115         const struct iio_info           *info;
116 };
117
118 struct ad799x_state {
119         struct i2c_client               *client;
120         const struct ad799x_chip_info   *chip_info;
121         struct regulator                *reg;
122         struct regulator                *vref;
123         unsigned                        id;
124         u16                             config;
125
126         u8                              *rx_buf;
127         unsigned int                    transfer_size;
128 };
129
130 /**
131  * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
132  *
133  * Currently there is no option in this driver to disable the saving of
134  * timestamps within the ring.
135  **/
136 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
137 {
138         struct iio_poll_func *pf = p;
139         struct iio_dev *indio_dev = pf->indio_dev;
140         struct ad799x_state *st = iio_priv(indio_dev);
141         int b_sent;
142         u8 cmd;
143
144         switch (st->id) {
145         case ad7991:
146         case ad7995:
147         case ad7999:
148                 cmd = st->config |
149                         (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
150                 break;
151         case ad7992:
152         case ad7993:
153         case ad7994:
154                 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
155                         AD7998_CONV_RES_REG;
156                 break;
157         case ad7997:
158         case ad7998:
159                 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
160                 break;
161         default:
162                 cmd = 0;
163         }
164
165         b_sent = i2c_smbus_read_i2c_block_data(st->client,
166                         cmd, st->transfer_size, st->rx_buf);
167         if (b_sent < 0)
168                 goto out;
169
170         iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
171                         iio_get_time_ns());
172 out:
173         iio_trigger_notify_done(indio_dev->trig);
174
175         return IRQ_HANDLED;
176 }
177
178 /*
179  * ad799x register access by I2C
180  */
181 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
182 {
183         struct i2c_client *client = st->client;
184         int ret = 0;
185
186         ret = i2c_smbus_read_word_swapped(client, reg);
187         if (ret < 0) {
188                 dev_err(&client->dev, "I2C read error\n");
189                 return ret;
190         }
191
192         *data = (u16)ret;
193
194         return 0;
195 }
196
197 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
198 {
199         struct i2c_client *client = st->client;
200         int ret = 0;
201
202         ret = i2c_smbus_read_byte_data(client, reg);
203         if (ret < 0) {
204                 dev_err(&client->dev, "I2C read error\n");
205                 return ret;
206         }
207
208         *data = (u8)ret;
209
210         return 0;
211 }
212
213 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
214 {
215         struct i2c_client *client = st->client;
216         int ret = 0;
217
218         ret = i2c_smbus_write_word_swapped(client, reg, data);
219         if (ret < 0)
220                 dev_err(&client->dev, "I2C write error\n");
221
222         return ret;
223 }
224
225 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
226 {
227         struct i2c_client *client = st->client;
228         int ret = 0;
229
230         ret = i2c_smbus_write_byte_data(client, reg, data);
231         if (ret < 0)
232                 dev_err(&client->dev, "I2C write error\n");
233
234         return ret;
235 }
236
237 static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
238         const unsigned long *scan_mask)
239 {
240         struct ad799x_state *st = iio_priv(indio_dev);
241
242         kfree(st->rx_buf);
243         st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
244         if (!st->rx_buf)
245                 return -ENOMEM;
246
247         st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
248
249         switch (st->id) {
250         case ad7997:
251         case ad7998:
252                 return ad799x_i2c_write16(st, AD7998_CONF_REG,
253                         st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
254         default:
255                 break;
256         }
257
258         return 0;
259 }
260
261 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
262 {
263         u16 rxbuf;
264         u8 cmd;
265         int ret;
266
267         switch (st->id) {
268         case ad7991:
269         case ad7995:
270         case ad7999:
271                 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
272                 break;
273         case ad7992:
274         case ad7993:
275         case ad7994:
276                 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
277                 break;
278         case ad7997:
279         case ad7998:
280                 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
281                 break;
282         default:
283                 return -EINVAL;
284         }
285
286         ret = ad799x_i2c_read16(st, cmd, &rxbuf);
287         if (ret < 0)
288                 return ret;
289
290         return rxbuf;
291 }
292
293 static int ad799x_read_raw(struct iio_dev *indio_dev,
294                            struct iio_chan_spec const *chan,
295                            int *val,
296                            int *val2,
297                            long m)
298 {
299         int ret;
300         struct ad799x_state *st = iio_priv(indio_dev);
301
302         switch (m) {
303         case IIO_CHAN_INFO_RAW:
304                 mutex_lock(&indio_dev->mlock);
305                 if (iio_buffer_enabled(indio_dev))
306                         ret = -EBUSY;
307                 else
308                         ret = ad799x_scan_direct(st, chan->scan_index);
309                 mutex_unlock(&indio_dev->mlock);
310
311                 if (ret < 0)
312                         return ret;
313                 *val = (ret >> chan->scan_type.shift) &
314                         RES_MASK(chan->scan_type.realbits);
315                 return IIO_VAL_INT;
316         case IIO_CHAN_INFO_SCALE:
317                 ret = regulator_get_voltage(st->vref);
318                 if (ret < 0)
319                         return ret;
320                 *val = ret / 1000;
321                 *val2 = chan->scan_type.realbits;
322                 return IIO_VAL_FRACTIONAL_LOG2;
323         }
324         return -EINVAL;
325 }
326 static const unsigned int ad7998_frequencies[] = {
327         [AD7998_CYC_DIS]        = 0,
328         [AD7998_CYC_TCONF_32]   = 15625,
329         [AD7998_CYC_TCONF_64]   = 7812,
330         [AD7998_CYC_TCONF_128]  = 3906,
331         [AD7998_CYC_TCONF_512]  = 976,
332         [AD7998_CYC_TCONF_1024] = 488,
333         [AD7998_CYC_TCONF_2048] = 244,
334 };
335 static ssize_t ad799x_read_frequency(struct device *dev,
336                                         struct device_attribute *attr,
337                                         char *buf)
338 {
339         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
340         struct ad799x_state *st = iio_priv(indio_dev);
341
342         int ret;
343         u8 val;
344         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
345         if (ret)
346                 return ret;
347
348         val &= AD7998_CYC_MASK;
349
350         return sprintf(buf, "%u\n", ad7998_frequencies[val]);
351 }
352
353 static ssize_t ad799x_write_frequency(struct device *dev,
354                                          struct device_attribute *attr,
355                                          const char *buf,
356                                          size_t len)
357 {
358         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
359         struct ad799x_state *st = iio_priv(indio_dev);
360
361         long val;
362         int ret, i;
363         u8 t;
364
365         ret = kstrtol(buf, 10, &val);
366         if (ret)
367                 return ret;
368
369         mutex_lock(&indio_dev->mlock);
370         ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
371         if (ret)
372                 goto error_ret_mutex;
373         /* Wipe the bits clean */
374         t &= ~AD7998_CYC_MASK;
375
376         for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
377                 if (val == ad7998_frequencies[i])
378                         break;
379         if (i == ARRAY_SIZE(ad7998_frequencies)) {
380                 ret = -EINVAL;
381                 goto error_ret_mutex;
382         }
383         t |= i;
384         ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
385
386 error_ret_mutex:
387         mutex_unlock(&indio_dev->mlock);
388
389         return ret ? ret : len;
390 }
391
392 static int ad799x_read_event_config(struct iio_dev *indio_dev,
393                                     const struct iio_chan_spec *chan,
394                                     enum iio_event_type type,
395                                     enum iio_event_direction dir)
396 {
397         return 1;
398 }
399
400 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
401                                          enum iio_event_direction dir,
402                                          enum iio_event_info info)
403 {
404         switch (info) {
405         case IIO_EV_INFO_VALUE:
406                 if (dir == IIO_EV_DIR_FALLING)
407                         return AD7998_DATALOW_REG(chan->channel);
408                 else
409                         return AD7998_DATAHIGH_REG(chan->channel);
410         case IIO_EV_INFO_HYSTERESIS:
411                 return AD7998_HYST_REG(chan->channel);
412         default:
413                 return -EINVAL;
414         }
415
416         return 0;
417 }
418
419 static int ad799x_write_event_value(struct iio_dev *indio_dev,
420                                     const struct iio_chan_spec *chan,
421                                     enum iio_event_type type,
422                                     enum iio_event_direction dir,
423                                     enum iio_event_info info,
424                                     int val, int val2)
425 {
426         int ret;
427         struct ad799x_state *st = iio_priv(indio_dev);
428
429         mutex_lock(&indio_dev->mlock);
430         ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info),
431                 val);
432         mutex_unlock(&indio_dev->mlock);
433
434         return ret;
435 }
436
437 static int ad799x_read_event_value(struct iio_dev *indio_dev,
438                                     const struct iio_chan_spec *chan,
439                                     enum iio_event_type type,
440                                     enum iio_event_direction dir,
441                                     enum iio_event_info info,
442                                     int *val, int *val2)
443 {
444         int ret;
445         struct ad799x_state *st = iio_priv(indio_dev);
446         u16 valin;
447
448         mutex_lock(&indio_dev->mlock);
449         ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info),
450                 &valin);
451         mutex_unlock(&indio_dev->mlock);
452         if (ret < 0)
453                 return ret;
454         *val = valin;
455
456         return IIO_VAL_INT;
457 }
458
459 static irqreturn_t ad799x_event_handler(int irq, void *private)
460 {
461         struct iio_dev *indio_dev = private;
462         struct ad799x_state *st = iio_priv(private);
463         u8 status;
464         int i, ret;
465
466         ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
467         if (ret)
468                 goto done;
469
470         if (!status)
471                 goto done;
472
473         ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
474
475         for (i = 0; i < 8; i++) {
476                 if (status & (1 << i))
477                         iio_push_event(indio_dev,
478                                        i & 0x1 ?
479                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
480                                                             (i >> 1),
481                                                             IIO_EV_TYPE_THRESH,
482                                                             IIO_EV_DIR_RISING) :
483                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
484                                                             (i >> 1),
485                                                             IIO_EV_TYPE_THRESH,
486                                                             IIO_EV_DIR_FALLING),
487                                        iio_get_time_ns());
488         }
489
490 done:
491         return IRQ_HANDLED;
492 }
493
494 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
495                               ad799x_read_frequency,
496                               ad799x_write_frequency);
497 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
498
499 static struct attribute *ad799x_event_attributes[] = {
500         &iio_dev_attr_sampling_frequency.dev_attr.attr,
501         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
502         NULL,
503 };
504
505 static struct attribute_group ad799x_event_attrs_group = {
506         .attrs = ad799x_event_attributes,
507         .name = "events",
508 };
509
510 static const struct iio_info ad7991_info = {
511         .read_raw = &ad799x_read_raw,
512         .driver_module = THIS_MODULE,
513 };
514
515 static const struct iio_info ad7993_4_7_8_info = {
516         .read_raw = &ad799x_read_raw,
517         .event_attrs = &ad799x_event_attrs_group,
518         .read_event_config = &ad799x_read_event_config,
519         .read_event_value = &ad799x_read_event_value,
520         .write_event_value = &ad799x_write_event_value,
521         .driver_module = THIS_MODULE,
522         .update_scan_mode = ad7997_8_update_scan_mode,
523 };
524
525 static const struct iio_event_spec ad799x_events[] = {
526         {
527                 .type = IIO_EV_TYPE_THRESH,
528                 .dir = IIO_EV_DIR_RISING,
529                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
530                         BIT(IIO_EV_INFO_ENABLE),
531         }, {
532                 .type = IIO_EV_TYPE_THRESH,
533                 .dir = IIO_EV_DIR_FALLING,
534                 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
535                         BIT(IIO_EV_INFO_ENABLE),
536         }, {
537                 .type = IIO_EV_TYPE_THRESH,
538                 .dir = IIO_EV_DIR_EITHER,
539                 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
540         },
541 };
542
543 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
544         .type = IIO_VOLTAGE, \
545         .indexed = 1, \
546         .channel = (_index), \
547         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
548         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
549         .scan_index = (_index), \
550         .scan_type = { \
551                 .sign = 'u', \
552                 .realbits = (_realbits), \
553                 .storagebits = 16, \
554                 .shift = 12 - (_realbits), \
555                 .endianness = IIO_BE, \
556         }, \
557         .event_spec = _ev_spec, \
558         .num_event_specs = _num_ev_spec, \
559 }
560
561 #define AD799X_CHANNEL(_index, _realbits) \
562         _AD799X_CHANNEL(_index, _realbits, NULL, 0)
563
564 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
565         _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
566                 ARRAY_SIZE(ad799x_events))
567
568 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
569         [ad7991] = {
570                 .channel = {
571                         AD799X_CHANNEL(0, 12),
572                         AD799X_CHANNEL(1, 12),
573                         AD799X_CHANNEL(2, 12),
574                         AD799X_CHANNEL(3, 12),
575                         IIO_CHAN_SOFT_TIMESTAMP(4),
576                 },
577                 .num_channels = 5,
578                 .info = &ad7991_info,
579         },
580         [ad7995] = {
581                 .channel = {
582                         AD799X_CHANNEL(0, 10),
583                         AD799X_CHANNEL(1, 10),
584                         AD799X_CHANNEL(2, 10),
585                         AD799X_CHANNEL(3, 10),
586                         IIO_CHAN_SOFT_TIMESTAMP(4),
587                 },
588                 .num_channels = 5,
589                 .info = &ad7991_info,
590         },
591         [ad7999] = {
592                 .channel = {
593                         AD799X_CHANNEL(0, 8),
594                         AD799X_CHANNEL(1, 8),
595                         AD799X_CHANNEL(2, 8),
596                         AD799X_CHANNEL(3, 8),
597                         IIO_CHAN_SOFT_TIMESTAMP(4),
598                 },
599                 .num_channels = 5,
600                 .info = &ad7991_info,
601         },
602         [ad7992] = {
603                 .channel = {
604                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
605                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
606                         IIO_CHAN_SOFT_TIMESTAMP(3),
607                 },
608                 .num_channels = 3,
609                 .default_config = AD7998_ALERT_EN,
610                 .info = &ad7993_4_7_8_info,
611         },
612         [ad7993] = {
613                 .channel = {
614                         AD799X_CHANNEL_WITH_EVENTS(0, 10),
615                         AD799X_CHANNEL_WITH_EVENTS(1, 10),
616                         AD799X_CHANNEL_WITH_EVENTS(2, 10),
617                         AD799X_CHANNEL_WITH_EVENTS(3, 10),
618                         IIO_CHAN_SOFT_TIMESTAMP(4),
619                 },
620                 .num_channels = 5,
621                 .default_config = AD7998_ALERT_EN,
622                 .info = &ad7993_4_7_8_info,
623         },
624         [ad7994] = {
625                 .channel = {
626                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
627                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
628                         AD799X_CHANNEL_WITH_EVENTS(2, 12),
629                         AD799X_CHANNEL_WITH_EVENTS(3, 12),
630                         IIO_CHAN_SOFT_TIMESTAMP(4),
631                 },
632                 .num_channels = 5,
633                 .default_config = AD7998_ALERT_EN,
634                 .info = &ad7993_4_7_8_info,
635         },
636         [ad7997] = {
637                 .channel = {
638                         AD799X_CHANNEL_WITH_EVENTS(0, 10),
639                         AD799X_CHANNEL_WITH_EVENTS(1, 10),
640                         AD799X_CHANNEL_WITH_EVENTS(2, 10),
641                         AD799X_CHANNEL_WITH_EVENTS(3, 10),
642                         AD799X_CHANNEL(4, 10),
643                         AD799X_CHANNEL(5, 10),
644                         AD799X_CHANNEL(6, 10),
645                         AD799X_CHANNEL(7, 10),
646                         IIO_CHAN_SOFT_TIMESTAMP(8),
647                 },
648                 .num_channels = 9,
649                 .default_config = AD7998_ALERT_EN,
650                 .info = &ad7993_4_7_8_info,
651         },
652         [ad7998] = {
653                 .channel = {
654                         AD799X_CHANNEL_WITH_EVENTS(0, 12),
655                         AD799X_CHANNEL_WITH_EVENTS(1, 12),
656                         AD799X_CHANNEL_WITH_EVENTS(2, 12),
657                         AD799X_CHANNEL_WITH_EVENTS(3, 12),
658                         AD799X_CHANNEL(4, 12),
659                         AD799X_CHANNEL(5, 12),
660                         AD799X_CHANNEL(6, 12),
661                         AD799X_CHANNEL(7, 12),
662                         IIO_CHAN_SOFT_TIMESTAMP(8),
663                 },
664                 .num_channels = 9,
665                 .default_config = AD7998_ALERT_EN,
666                 .info = &ad7993_4_7_8_info,
667         },
668 };
669
670 static int ad799x_probe(struct i2c_client *client,
671                                    const struct i2c_device_id *id)
672 {
673         int ret;
674         struct ad799x_state *st;
675         struct iio_dev *indio_dev;
676
677         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
678         if (indio_dev == NULL)
679                 return -ENOMEM;
680
681         st = iio_priv(indio_dev);
682         /* this is only used for device removal purposes */
683         i2c_set_clientdata(client, indio_dev);
684
685         st->id = id->driver_data;
686         st->chip_info = &ad799x_chip_info_tbl[st->id];
687         st->config = st->chip_info->default_config;
688
689         /* TODO: Add pdata options for filtering and bit delay */
690
691         st->reg = devm_regulator_get(&client->dev, "vcc");
692         if (IS_ERR(st->reg))
693                 return PTR_ERR(st->reg);
694         ret = regulator_enable(st->reg);
695         if (ret)
696                 return ret;
697         st->vref = devm_regulator_get(&client->dev, "vref");
698         if (IS_ERR(st->vref)) {
699                 ret = PTR_ERR(st->vref);
700                 goto error_disable_reg;
701         }
702         ret = regulator_enable(st->vref);
703         if (ret)
704                 goto error_disable_reg;
705
706         st->client = client;
707
708         indio_dev->dev.parent = &client->dev;
709         indio_dev->name = id->name;
710         indio_dev->info = st->chip_info->info;
711
712         indio_dev->modes = INDIO_DIRECT_MODE;
713         indio_dev->channels = st->chip_info->channel;
714         indio_dev->num_channels = st->chip_info->num_channels;
715
716         ret = iio_triggered_buffer_setup(indio_dev, NULL,
717                 &ad799x_trigger_handler, NULL);
718         if (ret)
719                 goto error_disable_vref;
720
721         if (client->irq > 0) {
722                 ret = devm_request_threaded_irq(&client->dev,
723                                                 client->irq,
724                                                 NULL,
725                                                 ad799x_event_handler,
726                                                 IRQF_TRIGGER_FALLING |
727                                                 IRQF_ONESHOT,
728                                                 client->name,
729                                                 indio_dev);
730                 if (ret)
731                         goto error_cleanup_ring;
732         }
733         ret = iio_device_register(indio_dev);
734         if (ret)
735                 goto error_cleanup_ring;
736
737         return 0;
738
739 error_cleanup_ring:
740         iio_triggered_buffer_cleanup(indio_dev);
741 error_disable_vref:
742         regulator_disable(st->vref);
743 error_disable_reg:
744         regulator_disable(st->reg);
745
746         return ret;
747 }
748
749 static int ad799x_remove(struct i2c_client *client)
750 {
751         struct iio_dev *indio_dev = i2c_get_clientdata(client);
752         struct ad799x_state *st = iio_priv(indio_dev);
753
754         iio_device_unregister(indio_dev);
755
756         iio_triggered_buffer_cleanup(indio_dev);
757         regulator_disable(st->vref);
758         regulator_disable(st->reg);
759         kfree(st->rx_buf);
760
761         return 0;
762 }
763
764 static const struct i2c_device_id ad799x_id[] = {
765         { "ad7991", ad7991 },
766         { "ad7995", ad7995 },
767         { "ad7999", ad7999 },
768         { "ad7992", ad7992 },
769         { "ad7993", ad7993 },
770         { "ad7994", ad7994 },
771         { "ad7997", ad7997 },
772         { "ad7998", ad7998 },
773         {}
774 };
775
776 MODULE_DEVICE_TABLE(i2c, ad799x_id);
777
778 static struct i2c_driver ad799x_driver = {
779         .driver = {
780                 .name = "ad799x",
781         },
782         .probe = ad799x_probe,
783         .remove = ad799x_remove,
784         .id_table = ad799x_id,
785 };
786 module_i2c_driver(ad799x_driver);
787
788 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
789 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
790 MODULE_LICENSE("GPL v2");