3 * Copyright (C) 2010-1011 Michael Hennerich, Analog Devices Inc.
5 * based on iio/adc/max1363
6 * Copyright (C) 2008-2010 Jonathan Cameron
8 * based on linux/drivers/i2c/chips/max123x
9 * Copyright (C) 2002-2004 Stefan Eletzhofer
11 * based on linux/drivers/acron/char/pcf8583.c
12 * Copyright (C) 2000 Russell King
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.
20 * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
21 * ad7998 and similar chips.
25 #include <linux/interrupt.h>
26 #include <linux/workqueue.h>
27 #include <linux/device.h>
28 #include <linux/kernel.h>
29 #include <linux/sysfs.h>
30 #include <linux/list.h>
31 #include <linux/i2c.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/slab.h>
34 #include <linux/types.h>
35 #include <linux/err.h>
40 #include "../ring_generic.h"
45 * ad799x register access by I2C
47 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
49 struct i2c_client *client = st->client;
52 ret = i2c_smbus_read_word_data(client, reg);
54 dev_err(&client->dev, "I2C read error\n");
58 *data = swab16((u16)ret);
63 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
65 struct i2c_client *client = st->client;
68 ret = i2c_smbus_read_byte_data(client, reg);
70 dev_err(&client->dev, "I2C read error\n");
79 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
81 struct i2c_client *client = st->client;
84 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
86 dev_err(&client->dev, "I2C write error\n");
91 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
93 struct i2c_client *client = st->client;
96 ret = i2c_smbus_write_byte_data(client, reg, data);
98 dev_err(&client->dev, "I2C write error\n");
103 int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
105 return ad799x_i2c_write16(st, AD7998_CONF_REG,
106 st->config | (mask << AD799X_CHANNEL_SHIFT));
109 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
119 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
124 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
128 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
134 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
141 static int ad799x_read_raw(struct iio_dev *dev_info,
142 struct iio_chan_spec const *chan,
148 struct ad799x_state *st = dev_info->dev_data;
149 unsigned int scale_uv;
153 mutex_lock(&dev_info->mlock);
154 if (iio_ring_enabled(dev_info))
155 ret = ad799x_single_channel_from_ring(st,
158 ret = ad799x_scan_direct(st, chan->address);
159 mutex_unlock(&dev_info->mlock);
163 *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
164 RES_MASK(st->chip_info->channel[0].scan_type.realbits);
166 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
167 scale_uv = (st->int_vref_mv * 1000)
168 >> st->chip_info->channel[0].scan_type.realbits;
169 *val = scale_uv / 1000;
170 *val2 = (scale_uv % 1000) * 1000;
171 return IIO_VAL_INT_PLUS_MICRO;
176 static ssize_t ad799x_read_frequency(struct device *dev,
177 struct device_attribute *attr,
180 struct iio_dev *dev_info = dev_get_drvdata(dev);
181 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
185 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
189 val &= AD7998_CYC_MASK;
193 len = sprintf(buf, "0\n");
195 case AD7998_CYC_TCONF_32:
196 len = sprintf(buf, "15625\n");
198 case AD7998_CYC_TCONF_64:
199 len = sprintf(buf, "7812\n");
201 case AD7998_CYC_TCONF_128:
202 len = sprintf(buf, "3906\n");
204 case AD7998_CYC_TCONF_256:
205 len = sprintf(buf, "1953\n");
207 case AD7998_CYC_TCONF_512:
208 len = sprintf(buf, "976\n");
210 case AD7998_CYC_TCONF_1024:
211 len = sprintf(buf, "488\n");
213 case AD7998_CYC_TCONF_2048:
214 len = sprintf(buf, "244\n");
220 static ssize_t ad799x_write_frequency(struct device *dev,
221 struct device_attribute *attr,
225 struct iio_dev *dev_info = dev_get_drvdata(dev);
226 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
232 ret = strict_strtol(buf, 10, &val);
236 mutex_lock(&dev_info->mlock);
237 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
239 goto error_ret_mutex;
240 /* Wipe the bits clean */
241 t &= ~AD7998_CYC_MASK;
245 t |= AD7998_CYC_TCONF_32;
248 t |= AD7998_CYC_TCONF_64;
251 t |= AD7998_CYC_TCONF_128;
254 t |= AD7998_CYC_TCONF_256;
257 t |= AD7998_CYC_TCONF_512;
260 t |= AD7998_CYC_TCONF_1024;
263 t |= AD7998_CYC_TCONF_2048;
270 goto error_ret_mutex;
273 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
276 mutex_unlock(&dev_info->mlock);
278 return ret ? ret : len;
281 static ssize_t ad799x_read_channel_config(struct device *dev,
282 struct device_attribute *attr,
285 struct iio_dev *dev_info = dev_get_drvdata(dev);
286 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
287 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
291 ret = ad799x_i2c_read16(st, this_attr->address, &val);
295 return sprintf(buf, "%d\n", val);
298 static ssize_t ad799x_write_channel_config(struct device *dev,
299 struct device_attribute *attr,
303 struct iio_dev *dev_info = dev_get_drvdata(dev);
304 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
305 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
310 ret = strict_strtol(buf, 10, &val);
314 mutex_lock(&dev_info->mlock);
315 ret = ad799x_i2c_write16(st, this_attr->address, val);
316 mutex_unlock(&dev_info->mlock);
318 return ret ? ret : len;
321 static irqreturn_t ad799x_event_handler(int irq, void *private)
323 struct iio_dev *indio_dev = private;
324 struct ad799x_state *st = iio_dev_get_devdata(private);
328 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
335 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
337 for (i = 0; i < 8; i++) {
338 if (status & (1 << i))
339 iio_push_event(indio_dev, 0,
341 IIO_EVENT_CODE_IN_HIGH_THRESH(i >> 1) :
342 IIO_EVENT_CODE_IN_LOW_THRESH(i >> 1),
349 static IIO_DEVICE_ATTR(in0_thresh_low_value,
351 ad799x_read_channel_config,
352 ad799x_write_channel_config,
353 AD7998_DATALOW_CH1_REG);
355 static IIO_DEVICE_ATTR(in0_thresh_high_value,
357 ad799x_read_channel_config,
358 ad799x_write_channel_config,
359 AD7998_DATAHIGH_CH1_REG);
361 static IIO_DEVICE_ATTR(in0_thresh_both_hyst_raw,
363 ad799x_read_channel_config,
364 ad799x_write_channel_config,
365 AD7998_HYST_CH1_REG);
367 static IIO_DEVICE_ATTR(in1_thresh_low_value,
369 ad799x_read_channel_config,
370 ad799x_write_channel_config,
371 AD7998_DATALOW_CH2_REG);
373 static IIO_DEVICE_ATTR(in1_thresh_high_value,
375 ad799x_read_channel_config,
376 ad799x_write_channel_config,
377 AD7998_DATAHIGH_CH2_REG);
379 static IIO_DEVICE_ATTR(in1_thresh_both_hyst_raw,
381 ad799x_read_channel_config,
382 ad799x_write_channel_config,
383 AD7998_HYST_CH2_REG);
385 static IIO_DEVICE_ATTR(in2_thresh_low_value,
387 ad799x_read_channel_config,
388 ad799x_write_channel_config,
389 AD7998_DATALOW_CH3_REG);
391 static IIO_DEVICE_ATTR(in2_thresh_high_value,
393 ad799x_read_channel_config,
394 ad799x_write_channel_config,
395 AD7998_DATAHIGH_CH3_REG);
397 static IIO_DEVICE_ATTR(in2_thresh_both_hyst_raw,
399 ad799x_read_channel_config,
400 ad799x_write_channel_config,
401 AD7998_HYST_CH3_REG);
403 static IIO_DEVICE_ATTR(in3_thresh_low_value,
405 ad799x_read_channel_config,
406 ad799x_write_channel_config,
407 AD7998_DATALOW_CH4_REG);
409 static IIO_DEVICE_ATTR(in3_thresh_high_value,
411 ad799x_read_channel_config,
412 ad799x_write_channel_config,
413 AD7998_DATAHIGH_CH4_REG);
415 static IIO_DEVICE_ATTR(in3_thresh_both_hyst_raw,
417 ad799x_read_channel_config,
418 ad799x_write_channel_config,
419 AD7998_HYST_CH4_REG);
421 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
422 ad799x_read_frequency,
423 ad799x_write_frequency);
424 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
426 static struct attribute *ad7993_4_7_8_event_attributes[] = {
427 &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
428 &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
429 &iio_dev_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
430 &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
431 &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
432 &iio_dev_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
433 &iio_dev_attr_in2_thresh_low_value.dev_attr.attr,
434 &iio_dev_attr_in2_thresh_high_value.dev_attr.attr,
435 &iio_dev_attr_in2_thresh_both_hyst_raw.dev_attr.attr,
436 &iio_dev_attr_in3_thresh_low_value.dev_attr.attr,
437 &iio_dev_attr_in3_thresh_high_value.dev_attr.attr,
438 &iio_dev_attr_in3_thresh_both_hyst_raw.dev_attr.attr,
439 &iio_dev_attr_sampling_frequency.dev_attr.attr,
440 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
444 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
445 .attrs = ad7993_4_7_8_event_attributes,
448 static struct attribute *ad7992_event_attributes[] = {
449 &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
450 &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
451 &iio_dev_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
452 &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
453 &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
454 &iio_dev_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
455 &iio_dev_attr_sampling_frequency.dev_attr.attr,
456 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
460 static struct attribute_group ad7992_event_attrs_group = {
461 .attrs = ad7992_event_attributes,
464 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
466 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
467 (1 << IIO_CHAN_INFO_SCALE_SHARED),
468 0, 0, IIO_ST('u', 12, 16, 0), 0),
469 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
470 (1 << IIO_CHAN_INFO_SCALE_SHARED),
471 1, 1, IIO_ST('u', 12, 16, 0), 0),
472 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
473 (1 << IIO_CHAN_INFO_SCALE_SHARED),
474 2, 2, IIO_ST('u', 12, 16, 0), 0),
475 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
476 (1 << IIO_CHAN_INFO_SCALE_SHARED),
477 3, 3, IIO_ST('u', 12, 16, 0), 0),
478 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
483 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
484 (1 << IIO_CHAN_INFO_SCALE_SHARED),
485 0, 0, IIO_ST('u', 10, 16, 0), 0),
486 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
487 (1 << IIO_CHAN_INFO_SCALE_SHARED),
488 1, 1, IIO_ST('u', 10, 16, 0), 0),
489 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
490 (1 << IIO_CHAN_INFO_SCALE_SHARED),
491 2, 2, IIO_ST('u', 10, 16, 0), 0),
492 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
493 (1 << IIO_CHAN_INFO_SCALE_SHARED),
494 3, 3, IIO_ST('u', 10, 16, 0), 0),
495 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
500 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
501 (1 << IIO_CHAN_INFO_SCALE_SHARED),
502 0, 0, IIO_ST('u', 10, 16, 0), 0),
503 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
504 (1 << IIO_CHAN_INFO_SCALE_SHARED),
505 1, 1, IIO_ST('u', 10, 16, 0), 0),
506 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
507 (1 << IIO_CHAN_INFO_SCALE_SHARED),
508 2, 2, IIO_ST('u', 10, 16, 0), 0),
509 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
510 (1 << IIO_CHAN_INFO_SCALE_SHARED),
511 3, 3, IIO_ST('u', 10, 16, 0), 0),
512 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
517 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
518 (1 << IIO_CHAN_INFO_SCALE_SHARED),
519 0, 0, IIO_ST('u', 12, 16, 0), 0),
520 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
521 (1 << IIO_CHAN_INFO_SCALE_SHARED),
522 1, 1, IIO_ST('u', 12, 16, 0), 0),
523 .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
526 .monitor_mode = true,
527 .default_config = AD7998_ALERT_EN,
528 .event_attrs = &ad7992_event_attrs_group,
531 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
532 (1 << IIO_CHAN_INFO_SCALE_SHARED),
533 0, 0, IIO_ST('u', 10, 16, 0), 0),
534 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
535 (1 << IIO_CHAN_INFO_SCALE_SHARED),
536 1, 1, IIO_ST('u', 10, 16, 0), 0),
537 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
538 (1 << IIO_CHAN_INFO_SCALE_SHARED),
539 2, 2, IIO_ST('u', 10, 16, 0), 0),
540 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
541 (1 << IIO_CHAN_INFO_SCALE_SHARED),
542 3, 3, IIO_ST('u', 10, 16, 0), 0),
543 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
546 .monitor_mode = true,
547 .default_config = AD7998_ALERT_EN,
548 .event_attrs = &ad7993_4_7_8_event_attrs_group,
551 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
552 (1 << IIO_CHAN_INFO_SCALE_SHARED),
553 0, 0, IIO_ST('u', 12, 16, 0), 0),
554 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
555 (1 << IIO_CHAN_INFO_SCALE_SHARED),
556 1, 1, IIO_ST('u', 12, 16, 0), 0),
557 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
558 (1 << IIO_CHAN_INFO_SCALE_SHARED),
559 2, 2, IIO_ST('u', 12, 16, 0), 0),
560 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
561 (1 << IIO_CHAN_INFO_SCALE_SHARED),
562 3, 3, IIO_ST('u', 12, 16, 0), 0),
563 .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
566 .monitor_mode = true,
567 .default_config = AD7998_ALERT_EN,
568 .event_attrs = &ad7993_4_7_8_event_attrs_group,
571 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
572 (1 << IIO_CHAN_INFO_SCALE_SHARED),
573 0, 0, IIO_ST('u', 10, 16, 0), 0),
574 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
575 (1 << IIO_CHAN_INFO_SCALE_SHARED),
576 1, 1, IIO_ST('u', 10, 16, 0), 0),
577 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
578 (1 << IIO_CHAN_INFO_SCALE_SHARED),
579 2, 2, IIO_ST('u', 10, 16, 0), 0),
580 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
581 (1 << IIO_CHAN_INFO_SCALE_SHARED),
582 3, 3, IIO_ST('u', 10, 16, 0), 0),
583 .channel[4] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
584 (1 << IIO_CHAN_INFO_SCALE_SHARED),
585 4, 4, IIO_ST('u', 10, 16, 0), 0),
586 .channel[5] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
587 (1 << IIO_CHAN_INFO_SCALE_SHARED),
588 5, 5, IIO_ST('u', 10, 16, 0), 0),
589 .channel[6] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
590 (1 << IIO_CHAN_INFO_SCALE_SHARED),
591 6, 6, IIO_ST('u', 10, 16, 0), 0),
592 .channel[7] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
593 (1 << IIO_CHAN_INFO_SCALE_SHARED),
594 7, 7, IIO_ST('u', 10, 16, 0), 0),
595 .channel[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
598 .monitor_mode = true,
599 .default_config = AD7998_ALERT_EN,
600 .event_attrs = &ad7993_4_7_8_event_attrs_group,
603 .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
604 (1 << IIO_CHAN_INFO_SCALE_SHARED),
605 0, 0, IIO_ST('u', 12, 16, 0), 0),
606 .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
607 (1 << IIO_CHAN_INFO_SCALE_SHARED),
608 1, 1, IIO_ST('u', 12, 16, 0), 0),
609 .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
610 (1 << IIO_CHAN_INFO_SCALE_SHARED),
611 2, 2, IIO_ST('u', 12, 16, 0), 0),
612 .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
613 (1 << IIO_CHAN_INFO_SCALE_SHARED),
614 3, 3, IIO_ST('u', 12, 16, 0), 0),
615 .channel[4] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
616 (1 << IIO_CHAN_INFO_SCALE_SHARED),
617 4, 4, IIO_ST('u', 12, 16, 0), 0),
618 .channel[5] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
619 (1 << IIO_CHAN_INFO_SCALE_SHARED),
620 5, 5, IIO_ST('u', 12, 16, 0), 0),
621 .channel[6] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
622 (1 << IIO_CHAN_INFO_SCALE_SHARED),
623 6, 6, IIO_ST('u', 12, 16, 0), 0),
624 .channel[7] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
625 (1 << IIO_CHAN_INFO_SCALE_SHARED),
626 7, 7, IIO_ST('u', 12, 16, 0), 0),
627 .channel[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
630 .monitor_mode = true,
631 .default_config = AD7998_ALERT_EN,
632 .event_attrs = &ad7993_4_7_8_event_attrs_group,
636 static int __devinit ad799x_probe(struct i2c_client *client,
637 const struct i2c_device_id *id)
639 int ret, regdone = 0;
640 struct ad799x_platform_data *pdata = client->dev.platform_data;
641 struct ad799x_state *st;
642 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
644 if (indio_dev == NULL)
647 st = iio_priv(indio_dev);
648 /* this is only used for device removal purposes */
649 i2c_set_clientdata(client, indio_dev);
651 st->id = id->driver_data;
652 st->chip_info = &ad799x_chip_info_tbl[st->id];
653 st->config = st->chip_info->default_config;
655 /* TODO: Add pdata options for filtering and bit delay */
658 st->int_vref_mv = pdata->vref_mv;
660 st->int_vref_mv = st->chip_info->int_vref_mv;
662 st->reg = regulator_get(&client->dev, "vcc");
663 if (!IS_ERR(st->reg)) {
664 ret = regulator_enable(st->reg);
670 indio_dev->dev.parent = &client->dev;
671 indio_dev->name = id->name;
672 indio_dev->event_attrs = st->chip_info->event_attrs;
673 indio_dev->name = id->name;
674 indio_dev->dev_data = (void *)(st);
675 indio_dev->driver_module = THIS_MODULE;
676 indio_dev->modes = INDIO_DIRECT_MODE;
677 indio_dev->num_interrupt_lines = 1;
678 indio_dev->channels = st->chip_info->channel;
679 indio_dev->num_channels = st->chip_info->num_channels;
680 indio_dev->read_raw = &ad799x_read_raw;
682 ret = ad799x_register_ring_funcs_and_init(indio_dev);
684 goto error_disable_reg;
686 ret = iio_device_register(indio_dev);
688 goto error_cleanup_ring;
691 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
693 indio_dev->num_channels);
695 goto error_cleanup_ring;
697 if (client->irq > 0 && st->chip_info->monitor_mode) {
698 ret = request_threaded_irq(client->irq,
700 ad799x_event_handler,
701 IRQF_TRIGGER_FALLING |
706 goto error_cleanup_ring;
712 ad799x_ring_cleanup(indio_dev);
714 if (!IS_ERR(st->reg))
715 regulator_disable(st->reg);
717 if (!IS_ERR(st->reg))
718 regulator_put(st->reg);
720 iio_device_unregister(indio_dev);
722 iio_free_device(indio_dev);
727 static __devexit int ad799x_remove(struct i2c_client *client)
729 struct iio_dev *indio_dev = i2c_get_clientdata(client);
730 struct ad799x_state *st = iio_priv(indio_dev);
732 if (client->irq > 0 && st->chip_info->monitor_mode)
733 free_irq(client->irq, indio_dev);
735 iio_ring_buffer_unregister(indio_dev->ring);
736 ad799x_ring_cleanup(indio_dev);
737 if (!IS_ERR(st->reg)) {
738 regulator_disable(st->reg);
739 regulator_put(st->reg);
741 iio_device_unregister(indio_dev);
746 static const struct i2c_device_id ad799x_id[] = {
747 { "ad7991", ad7991 },
748 { "ad7995", ad7995 },
749 { "ad7999", ad7999 },
750 { "ad7992", ad7992 },
751 { "ad7993", ad7993 },
752 { "ad7994", ad7994 },
753 { "ad7997", ad7997 },
754 { "ad7998", ad7998 },
758 MODULE_DEVICE_TABLE(i2c, ad799x_id);
760 static struct i2c_driver ad799x_driver = {
764 .probe = ad799x_probe,
765 .remove = __devexit_p(ad799x_remove),
766 .id_table = ad799x_id,
769 static __init int ad799x_init(void)
771 return i2c_add_driver(&ad799x_driver);
774 static __exit void ad799x_exit(void)
776 i2c_del_driver(&ad799x_driver);
779 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
780 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
781 MODULE_LICENSE("GPL v2");
782 MODULE_ALIAS("i2c:ad799x");
784 module_init(ad799x_init);
785 module_exit(ad799x_exit);