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