Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / staging / iio / adc / ad7291.c
blob10e79e8ee64d98922f3adb3f75d141843a5ad81a
1 /*
2 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
20 #include "../iio.h"
21 #include "../sysfs.h"
24 * Simplified handling
26 * If no events enabled - single polled channel read
27 * If event enabled direct reads disable unless channel
28 * is in the read mask.
30 * The noise-delayed bit as per datasheet suggestion is always enabled.
35 * AD7291 registers definition
37 #define AD7291_COMMAND 0x00
38 #define AD7291_VOLTAGE 0x01
39 #define AD7291_T_SENSE 0x02
40 #define AD7291_T_AVERAGE 0x03
41 #define AD7291_CH0_DATA_HIGH 0x04
42 #define AD7291_CH0_DATA_LOW 0x05
43 #define AD7291_CH0_HYST 0x06
44 #define AD7291_CH1_DATA_HIGH 0x07
45 #define AD7291_CH1_DATA_LOW 0x08
46 #define AD7291_CH1_HYST 0x09
47 #define AD7291_CH2_DATA_HIGH 0x0A
48 #define AD7291_CH2_DATA_LOW 0x0B
49 #define AD7291_CH2_HYST 0x0C
50 #define AD7291_CH3_DATA_HIGH 0x0D
51 #define AD7291_CH3_DATA_LOW 0x0E
52 #define AD7291_CH3_HYST 0x0F
53 #define AD7291_CH4_DATA_HIGH 0x10
54 #define AD7291_CH4_DATA_LOW 0x11
55 #define AD7291_CH4_HYST 0x12
56 #define AD7291_CH5_DATA_HIGH 0x13
57 #define AD7291_CH5_DATA_LOW 0x14
58 #define AD7291_CH5_HYST 0x15
59 #define AD7291_CH6_DATA_HIGH 0x16
60 #define AD7291_CH6_DATA_LOW 0x17
61 #define AD7291_CH6_HYST 0x18
62 #define AD7291_CH7_DATA_HIGH 0x19
63 #define AD7291_CH7_DATA_LOW 0x1A
64 #define AD7291_CH7_HYST 0x2B
65 #define AD7291_T_SENSE_HIGH 0x1C
66 #define AD7291_T_SENSE_LOW 0x1D
67 #define AD7291_T_SENSE_HYST 0x1E
68 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
69 #define AD7291_T_ALERT_STATUS 0x20
71 #define AD7291_VOLTAGE_LIMIT_COUNT 8
75 * AD7291 command
77 #define AD7291_AUTOCYCLE (1 << 0)
78 #define AD7291_RESET (1 << 1)
79 #define AD7291_ALERT_CLEAR (1 << 2)
80 #define AD7291_ALERT_POLARITY (1 << 3)
81 #define AD7291_EXT_REF (1 << 4)
82 #define AD7291_NOISE_DELAY (1 << 5)
83 #define AD7291_T_SENSE_MASK (1 << 7)
84 #define AD7291_VOLTAGE_MASK 0xFF00
85 #define AD7291_VOLTAGE_OFFSET 0x8
88 * AD7291 value masks
90 #define AD7291_CHANNEL_MASK 0xF000
91 #define AD7291_BITS 12
92 #define AD7291_VALUE_MASK 0xFFF
93 #define AD7291_T_VALUE_SIGN 0x400
94 #define AD7291_T_VALUE_FLOAT_OFFSET 2
95 #define AD7291_T_VALUE_FLOAT_MASK 0x2
97 #define AD7291_BITS 12
99 struct ad7291_chip_info {
100 struct i2c_client *client;
101 struct regulator *reg;
102 u16 int_vref_mv;
103 u16 command;
104 u16 c_mask; /* Active voltage channels for events */
105 struct mutex state_lock;
108 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
110 struct i2c_client *client = chip->client;
111 int ret = 0;
113 ret = i2c_smbus_read_word_data(client, reg);
114 if (ret < 0) {
115 dev_err(&client->dev, "I2C read error\n");
116 return ret;
119 *data = swab16((u16)ret);
121 return 0;
124 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
126 return i2c_smbus_write_word_data(chip->client, reg, swab16(data));
129 static ssize_t ad7291_store_reset(struct device *dev,
130 struct device_attribute *attr,
131 const char *buf,
132 size_t len)
134 struct iio_dev *indio_dev = dev_get_drvdata(dev);
135 struct ad7291_chip_info *chip = iio_priv(indio_dev);
137 return ad7291_i2c_write(chip, AD7291_COMMAND,
138 chip->command | AD7291_RESET);
141 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, ad7291_store_reset, 0);
143 static struct attribute *ad7291_attributes[] = {
144 &iio_dev_attr_reset.dev_attr.attr,
145 NULL,
148 static const struct attribute_group ad7291_attribute_group = {
149 .attrs = ad7291_attributes,
152 static irqreturn_t ad7291_event_handler(int irq, void *private)
154 struct iio_dev *indio_dev = private;
155 struct ad7291_chip_info *chip = iio_priv(private);
156 u16 t_status, v_status;
157 u16 command;
158 int i;
159 s64 timestamp = iio_get_time_ns();
161 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
162 return IRQ_HANDLED;
164 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
165 return IRQ_HANDLED;
167 if (!(t_status || v_status))
168 return IRQ_HANDLED;
170 command = chip->command | AD7291_ALERT_CLEAR;
171 ad7291_i2c_write(chip, AD7291_COMMAND, command);
173 command = chip->command & ~AD7291_ALERT_CLEAR;
174 ad7291_i2c_write(chip, AD7291_COMMAND, command);
176 /* For now treat t_sense and t_sense_average the same */
177 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
178 iio_push_event(indio_dev,
179 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
181 IIO_EV_TYPE_THRESH,
182 IIO_EV_DIR_FALLING),
183 timestamp);
184 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
185 iio_push_event(indio_dev,
186 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
188 IIO_EV_TYPE_THRESH,
189 IIO_EV_DIR_RISING),
190 timestamp);
192 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
193 if (v_status & (1 << i))
194 iio_push_event(indio_dev,
195 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
196 i/2,
197 IIO_EV_TYPE_THRESH,
198 IIO_EV_DIR_FALLING),
199 timestamp);
200 if (v_status & (1 << (i + 1)))
201 iio_push_event(indio_dev,
202 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
203 i/2,
204 IIO_EV_TYPE_THRESH,
205 IIO_EV_DIR_RISING),
206 timestamp);
209 return IRQ_HANDLED;
212 static inline ssize_t ad7291_show_hyst(struct device *dev,
213 struct device_attribute *attr,
214 char *buf)
216 struct iio_dev *indio_dev = dev_get_drvdata(dev);
217 struct ad7291_chip_info *chip = iio_priv(indio_dev);
218 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
219 u16 data;
220 int ret;
222 ret = ad7291_i2c_read(chip, this_attr->address, &data);
223 if (ret < 0)
224 return ret;
226 return sprintf(buf, "%d\n", data & AD7291_VALUE_MASK);
229 static inline ssize_t ad7291_set_hyst(struct device *dev,
230 struct device_attribute *attr,
231 const char *buf,
232 size_t len)
234 struct iio_dev *indio_dev = dev_get_drvdata(dev);
235 struct ad7291_chip_info *chip = iio_priv(indio_dev);
236 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
237 u16 data;
238 int ret;
240 ret = kstrtou16(buf, 10, &data);
242 if (ret < 0)
243 return ret;
244 if (data > AD7291_VALUE_MASK)
245 return -EINVAL;
247 ret = ad7291_i2c_write(chip, this_attr->address, data);
248 if (ret < 0)
249 return ret;
251 return len;
254 static IIO_DEVICE_ATTR(in_temp0_thresh_both_hyst_raw,
255 S_IRUGO | S_IWUSR,
256 ad7291_show_hyst, ad7291_set_hyst,
257 AD7291_T_SENSE_HYST);
258 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
259 S_IRUGO | S_IWUSR,
260 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH0_HYST);
261 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
262 S_IRUGO | S_IWUSR,
263 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH1_HYST);
264 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
265 S_IRUGO | S_IWUSR,
266 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH2_HYST);
267 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
268 S_IRUGO | S_IWUSR,
269 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH3_HYST);
270 static IIO_DEVICE_ATTR(in_voltage4_thresh_both_hyst_raw,
271 S_IRUGO | S_IWUSR,
272 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH4_HYST);
273 static IIO_DEVICE_ATTR(in_voltage5_thresh_both_hyst_raw,
274 S_IRUGO | S_IWUSR,
275 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH5_HYST);
276 static IIO_DEVICE_ATTR(in_voltage6_thresh_both_hyst_raw,
277 S_IRUGO | S_IWUSR,
278 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH6_HYST);
279 static IIO_DEVICE_ATTR(in_voltage7_thresh_both_hyst_raw,
280 S_IRUGO | S_IWUSR,
281 ad7291_show_hyst, ad7291_set_hyst, AD7291_CH7_HYST);
283 static struct attribute *ad7291_event_attributes[] = {
284 &iio_dev_attr_in_temp0_thresh_both_hyst_raw.dev_attr.attr,
285 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
286 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
287 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
288 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
289 &iio_dev_attr_in_voltage4_thresh_both_hyst_raw.dev_attr.attr,
290 &iio_dev_attr_in_voltage5_thresh_both_hyst_raw.dev_attr.attr,
291 &iio_dev_attr_in_voltage6_thresh_both_hyst_raw.dev_attr.attr,
292 &iio_dev_attr_in_voltage7_thresh_both_hyst_raw.dev_attr.attr,
293 NULL,
296 /* high / low */
297 static u8 ad7291_limit_regs[9][2] = {
298 { AD7291_CH0_DATA_HIGH, AD7291_CH0_DATA_LOW },
299 { AD7291_CH1_DATA_HIGH, AD7291_CH1_DATA_LOW },
300 { AD7291_CH2_DATA_HIGH, AD7291_CH2_DATA_LOW },
301 { AD7291_CH3_DATA_HIGH, AD7291_CH3_DATA_LOW }, /* FIXME: ? */
302 { AD7291_CH4_DATA_HIGH, AD7291_CH4_DATA_LOW },
303 { AD7291_CH5_DATA_HIGH, AD7291_CH5_DATA_LOW },
304 { AD7291_CH6_DATA_HIGH, AD7291_CH6_DATA_LOW },
305 { AD7291_CH7_DATA_HIGH, AD7291_CH7_DATA_LOW },
306 /* temp */
307 { AD7291_T_SENSE_HIGH, AD7291_T_SENSE_LOW },
310 static int ad7291_read_event_value(struct iio_dev *indio_dev,
311 u64 event_code,
312 int *val)
314 struct ad7291_chip_info *chip = iio_priv(indio_dev);
316 int ret;
317 u8 reg;
318 u16 uval;
319 s16 signval;
321 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
322 case IIO_VOLTAGE:
323 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)]
324 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
325 IIO_EV_DIR_RISING)];
327 ret = ad7291_i2c_read(chip, reg, &uval);
328 if (ret < 0)
329 return ret;
330 *val = uval & AD7291_VALUE_MASK;
331 return 0;
333 case IIO_TEMP:
334 reg = ad7291_limit_regs[8]
335 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
336 IIO_EV_DIR_RISING)];
338 ret = ad7291_i2c_read(chip, reg, &signval);
339 if (ret < 0)
340 return ret;
341 signval = (s16)((signval & AD7291_VALUE_MASK) << 4) >> 4;
342 *val = signval;
343 return 0;
344 default:
345 return -EINVAL;
349 static int ad7291_write_event_value(struct iio_dev *indio_dev,
350 u64 event_code,
351 int val)
353 struct ad7291_chip_info *chip = iio_priv(indio_dev);
354 u8 reg;
355 s16 signval;
357 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
358 case IIO_VOLTAGE:
359 if (val > AD7291_VALUE_MASK || val < 0)
360 return -EINVAL;
361 reg = ad7291_limit_regs[IIO_EVENT_CODE_EXTRACT_NUM(event_code)]
362 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
363 IIO_EV_DIR_RISING)];
364 return ad7291_i2c_write(chip, reg, val);
365 case IIO_TEMP:
366 if (val > 2047 || val < -2048)
367 return -EINVAL;
368 reg = ad7291_limit_regs[8]
369 [!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
370 IIO_EV_DIR_RISING)];
371 signval = val;
372 return ad7291_i2c_write(chip, reg, *(u16 *)&signval);
373 default:
374 return -EINVAL;
378 static int ad7291_read_event_config(struct iio_dev *indio_dev,
379 u64 event_code)
381 struct ad7291_chip_info *chip = iio_priv(indio_dev);
382 /* To be enabled the channel must simply be on. If any are enabled
383 we are in continuous sampling mode */
385 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
386 case IIO_VOLTAGE:
387 if (chip->c_mask &
388 (1 << (15 - IIO_EVENT_CODE_EXTRACT_NUM(event_code))))
389 return 1;
390 else
391 return 0;
392 case IIO_TEMP:
393 /* always on */
394 return 1;
395 default:
396 return -EINVAL;
401 static int ad7291_write_event_config(struct iio_dev *indio_dev,
402 u64 event_code,
403 int state)
405 int ret = 0;
406 struct ad7291_chip_info *chip = iio_priv(indio_dev);
407 u16 regval;
409 mutex_lock(&chip->state_lock);
410 regval = chip->command;
412 * To be enabled the channel must simply be on. If any are enabled
413 * use continuous sampling mode.
414 * Possible to disable temp as well but that makes single read tricky.
417 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code)) {
418 case IIO_VOLTAGE:
419 if ((!state) && (chip->c_mask & (1 << (15 -
420 IIO_EVENT_CODE_EXTRACT_NUM(event_code)))))
421 chip->c_mask &= ~(1 << (15 - IIO_EVENT_CODE_EXTRACT_NUM
422 (event_code)));
423 else if (state && (!(chip->c_mask & (1 << (15 -
424 IIO_EVENT_CODE_EXTRACT_NUM(event_code))))))
425 chip->c_mask |= (1 << (15 - IIO_EVENT_CODE_EXTRACT_NUM
426 (event_code)));
427 else
428 break;
430 regval &= ~AD7291_AUTOCYCLE;
431 regval |= chip->c_mask;
432 if (chip->c_mask) /* Enable autocycle? */
433 regval |= AD7291_AUTOCYCLE;
435 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
436 if (ret < 0)
437 goto error_ret;
439 chip->command = regval;
440 break;
441 default:
442 ret = -EINVAL;
445 error_ret:
446 mutex_unlock(&chip->state_lock);
447 return ret;
450 static int ad7291_read_raw(struct iio_dev *indio_dev,
451 struct iio_chan_spec const *chan,
452 int *val,
453 int *val2,
454 long mask)
456 int ret;
457 struct ad7291_chip_info *chip = iio_priv(indio_dev);
458 unsigned int scale_uv;
459 u16 regval;
460 s16 signval;
462 switch (mask) {
463 case 0:
464 switch (chan->type) {
465 case IIO_VOLTAGE:
466 mutex_lock(&chip->state_lock);
467 /* If in autocycle mode drop through */
468 if (chip->command & AD7291_AUTOCYCLE) {
469 mutex_unlock(&chip->state_lock);
470 return -EBUSY;
472 /* Enable this channel alone */
473 regval = chip->command & (~AD7291_VOLTAGE_MASK);
474 regval |= 1 << (15 - chan->channel);
475 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
476 if (ret < 0) {
477 mutex_unlock(&chip->state_lock);
478 return ret;
480 /* Read voltage */
481 ret = i2c_smbus_read_word_data(chip->client,
482 AD7291_VOLTAGE);
483 if (ret < 0) {
484 mutex_unlock(&chip->state_lock);
485 return ret;
487 *val = swab16((u16)ret) & AD7291_VALUE_MASK;
488 mutex_unlock(&chip->state_lock);
489 return IIO_VAL_INT;
490 case IIO_TEMP:
491 /* Assumes tsense bit of command register always set */
492 ret = i2c_smbus_read_word_data(chip->client,
493 AD7291_T_SENSE);
494 if (ret < 0)
495 return ret;
496 signval = (s16)((swab16((u16)ret) &
497 AD7291_VALUE_MASK) << 4) >> 4;
498 *val = signval;
499 return IIO_VAL_INT;
500 default:
501 return -EINVAL;
503 case (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE):
504 ret = i2c_smbus_read_word_data(chip->client,
505 AD7291_T_AVERAGE);
506 if (ret < 0)
507 return ret;
508 signval = (s16)((swab16((u16)ret) &
509 AD7291_VALUE_MASK) << 4) >> 4;
510 *val = signval;
511 return IIO_VAL_INT;
512 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
513 scale_uv = (chip->int_vref_mv * 1000) >> AD7291_BITS;
514 *val = scale_uv / 1000;
515 *val2 = (scale_uv % 1000) * 1000;
516 return IIO_VAL_INT_PLUS_MICRO;
517 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
519 * One LSB of the ADC corresponds to 0.25 deg C.
520 * The temperature reading is in 12-bit twos complement format
522 *val = 250;
523 return IIO_VAL_INT;
524 default:
525 return -EINVAL;
529 #define AD7291_VOLTAGE_CHAN(_chan) \
531 .type = IIO_VOLTAGE, \
532 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED), \
533 .indexed = 1, \
534 .channel = _chan, \
535 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|\
536 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING) \
539 static const struct iio_chan_spec ad7291_channels[] = {
540 AD7291_VOLTAGE_CHAN(0),
541 AD7291_VOLTAGE_CHAN(1),
542 AD7291_VOLTAGE_CHAN(2),
543 AD7291_VOLTAGE_CHAN(3),
544 AD7291_VOLTAGE_CHAN(4),
545 AD7291_VOLTAGE_CHAN(5),
546 AD7291_VOLTAGE_CHAN(6),
547 AD7291_VOLTAGE_CHAN(7),
549 .type = IIO_TEMP,
550 .info_mask = (1 << IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE) |
551 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
552 .indexed = 1,
553 .channel = 0,
554 .event_mask =
555 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING)|
556 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)
560 static struct attribute_group ad7291_event_attribute_group = {
561 .attrs = ad7291_event_attributes,
564 static const struct iio_info ad7291_info = {
565 .attrs = &ad7291_attribute_group,
566 .read_raw = &ad7291_read_raw,
567 .read_event_config = &ad7291_read_event_config,
568 .write_event_config = &ad7291_write_event_config,
569 .read_event_value = &ad7291_read_event_value,
570 .write_event_value = &ad7291_write_event_value,
571 .event_attrs = &ad7291_event_attribute_group,
574 static int __devinit ad7291_probe(struct i2c_client *client,
575 const struct i2c_device_id *id)
577 struct ad7291_chip_info *chip;
578 struct iio_dev *indio_dev;
579 int ret = 0, voltage_uv = 0;
581 indio_dev = iio_allocate_device(sizeof(*chip));
582 if (indio_dev == NULL) {
583 ret = -ENOMEM;
584 goto error_ret;
586 chip = iio_priv(indio_dev);
588 chip->reg = regulator_get(&client->dev, "vcc");
589 if (!IS_ERR(chip->reg)) {
590 ret = regulator_enable(chip->reg);
591 if (ret)
592 goto error_put_reg;
593 voltage_uv = regulator_get_voltage(chip->reg);
596 mutex_init(&chip->state_lock);
597 /* this is only used for device removal purposes */
598 i2c_set_clientdata(client, indio_dev);
600 chip->client = client;
602 chip->command = AD7291_NOISE_DELAY |
603 AD7291_T_SENSE_MASK | /* Tsense always enabled */
604 AD7291_ALERT_POLARITY; /* set irq polarity low level */
606 if (voltage_uv) {
607 chip->int_vref_mv = voltage_uv / 1000;
608 chip->command |= AD7291_EXT_REF;
609 } else {
610 chip->int_vref_mv = 2500; /* Build-in ref */
613 indio_dev->name = id->name;
614 indio_dev->channels = ad7291_channels;
615 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
617 indio_dev->dev.parent = &client->dev;
618 indio_dev->info = &ad7291_info;
619 indio_dev->modes = INDIO_DIRECT_MODE;
621 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
622 if (ret) {
623 ret = -EIO;
624 goto error_disable_reg;
627 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
628 if (ret) {
629 ret = -EIO;
630 goto error_disable_reg;
633 if (client->irq > 0) {
634 ret = request_threaded_irq(client->irq,
635 NULL,
636 &ad7291_event_handler,
637 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
638 id->name,
639 indio_dev);
640 if (ret)
641 goto error_disable_reg;
644 ret = iio_device_register(indio_dev);
645 if (ret)
646 goto error_unreg_irq;
648 dev_info(&client->dev, "%s ADC registered.\n",
649 id->name);
651 return 0;
653 error_unreg_irq:
654 if (client->irq)
655 free_irq(client->irq, indio_dev);
656 error_disable_reg:
657 if (!IS_ERR(chip->reg))
658 regulator_disable(chip->reg);
659 error_put_reg:
660 if (!IS_ERR(chip->reg))
661 regulator_put(chip->reg);
663 iio_free_device(indio_dev);
664 error_ret:
665 return ret;
668 static int __devexit ad7291_remove(struct i2c_client *client)
670 struct iio_dev *indio_dev = i2c_get_clientdata(client);
671 struct ad7291_chip_info *chip = iio_priv(indio_dev);
673 iio_device_unregister(indio_dev);
675 if (client->irq)
676 free_irq(client->irq, indio_dev);
678 if (!IS_ERR(chip->reg)) {
679 regulator_disable(chip->reg);
680 regulator_put(chip->reg);
683 iio_free_device(indio_dev);
685 return 0;
688 static const struct i2c_device_id ad7291_id[] = {
689 { "ad7291", 0 },
693 MODULE_DEVICE_TABLE(i2c, ad7291_id);
695 static struct i2c_driver ad7291_driver = {
696 .driver = {
697 .name = KBUILD_MODNAME,
699 .probe = ad7291_probe,
700 .remove = __devexit_p(ad7291_remove),
701 .id_table = ad7291_id,
704 static __init int ad7291_init(void)
706 return i2c_add_driver(&ad7291_driver);
709 static __exit void ad7291_exit(void)
711 i2c_del_driver(&ad7291_driver);
714 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
715 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
716 MODULE_LICENSE("GPL v2");
718 module_init(ad7291_init);
719 module_exit(ad7291_exit);