1 /*
2 * iio/adc/ad799x.c
3 * Copyright (C) 2010-1011 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 "../iio.h"
37 #include "../sysfs.h"
38 #include "../events.h"
39 #include "../buffer.h"
40
41 #include "ad799x.h"
42
43 /*
44 * ad799x register access by I2C
45 */
ad799x_i2c_read16(struct ad799x_state * st,u8 reg,u16 * data)46 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
47 {
48 struct i2c_client *client = st->client;
49 int ret = 0;
50
51 ret = i2c_smbus_read_word_data(client, reg);
52 if (ret < 0) {
53 dev_err(&client->dev, "I2C read error\n");
54 return ret;
55 }
56
57 *data = swab16((u16)ret);
58
59 return 0;
60 }
61
ad799x_i2c_read8(struct ad799x_state * st,u8 reg,u8 * data)62 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
63 {
64 struct i2c_client *client = st->client;
65 int ret = 0;
66
67 ret = i2c_smbus_read_byte_data(client, reg);
68 if (ret < 0) {
69 dev_err(&client->dev, "I2C read error\n");
70 return ret;
71 }
72
73 *data = (u8)ret;
74
75 return 0;
76 }
77
ad799x_i2c_write16(struct ad799x_state * st,u8 reg,u16 data)78 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
79 {
80 struct i2c_client *client = st->client;
81 int ret = 0;
82
83 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
84 if (ret < 0)
85 dev_err(&client->dev, "I2C write error\n");
86
87 return ret;
88 }
89
ad799x_i2c_write8(struct ad799x_state * st,u8 reg,u8 data)90 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
91 {
92 struct i2c_client *client = st->client;
93 int ret = 0;
94
95 ret = i2c_smbus_write_byte_data(client, reg, data);
96 if (ret < 0)
97 dev_err(&client->dev, "I2C write error\n");
98
99 return ret;
100 }
101
ad7997_8_set_scan_mode(struct ad799x_state * st,unsigned mask)102 int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
103 {
104 return ad799x_i2c_write16(st, AD7998_CONF_REG,
105 st->config | (mask << AD799X_CHANNEL_SHIFT));
106 }
107
ad799x_scan_direct(struct ad799x_state * st,unsigned ch)108 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
109 {
110 u16 rxbuf;
111 u8 cmd;
112 int ret;
113
114 switch (st->id) {
115 case ad7991:
116 case ad7995:
117 case ad7999:
118 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
119 break;
120 case ad7992:
121 case ad7993:
122 case ad7994:
123 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
124 break;
125 case ad7997:
126 case ad7998:
127 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
128 break;
129 default:
130 return -EINVAL;
131 }
132
133 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
134 if (ret < 0)
135 return ret;
136
137 return rxbuf;
138 }
139
ad799x_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long m)140 static int ad799x_read_raw(struct iio_dev *indio_dev,
141 struct iio_chan_spec const *chan,
142 int *val,
143 int *val2,
144 long m)
145 {
146 int ret;
147 struct ad799x_state *st = iio_priv(indio_dev);
148 unsigned int scale_uv;
149
150 switch (m) {
151 case 0:
152 mutex_lock(&indio_dev->mlock);
153 if (iio_buffer_enabled(indio_dev))
154 ret = -EBUSY;
155 else
156 ret = ad799x_scan_direct(st, chan->scan_index);
157 mutex_unlock(&indio_dev->mlock);
158
159 if (ret < 0)
160 return ret;
161 *val = (ret >> chan->scan_type.shift) &
162 RES_MASK(chan->scan_type.realbits);
163 return IIO_VAL_INT;
164 case IIO_CHAN_INFO_SCALE:
165 scale_uv = (st->int_vref_mv * 1000) >> chan->scan_type.realbits;
166 *val = scale_uv / 1000;
167 *val2 = (scale_uv % 1000) * 1000;
168 return IIO_VAL_INT_PLUS_MICRO;
169 }
170 return -EINVAL;
171 }
172 static const unsigned int ad7998_frequencies[] = {
173 [AD7998_CYC_DIS] = 0,
174 [AD7998_CYC_TCONF_32] = 15625,
175 [AD7998_CYC_TCONF_64] = 7812,
176 [AD7998_CYC_TCONF_128] = 3906,
177 [AD7998_CYC_TCONF_512] = 976,
178 [AD7998_CYC_TCONF_1024] = 488,
179 [AD7998_CYC_TCONF_2048] = 244,
180 };
ad799x_read_frequency(struct device * dev,struct device_attribute * attr,char * buf)181 static ssize_t ad799x_read_frequency(struct device *dev,
182 struct device_attribute *attr,
183 char *buf)
184 {
185 struct iio_dev *indio_dev = dev_get_drvdata(dev);
186 struct ad799x_state *st = iio_priv(indio_dev);
187
188 int ret;
189 u8 val;
190 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
191 if (ret)
192 return ret;
193
194 val &= AD7998_CYC_MASK;
195
196 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
197 }
198
ad799x_write_frequency(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)199 static ssize_t ad799x_write_frequency(struct device *dev,
200 struct device_attribute *attr,
201 const char *buf,
202 size_t len)
203 {
204 struct iio_dev *indio_dev = dev_get_drvdata(dev);
205 struct ad799x_state *st = iio_priv(indio_dev);
206
207 long val;
208 int ret, i;
209 u8 t;
210
211 ret = strict_strtol(buf, 10, &val);
212 if (ret)
213 return ret;
214
215 mutex_lock(&indio_dev->mlock);
216 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
217 if (ret)
218 goto error_ret_mutex;
219 /* Wipe the bits clean */
220 t &= ~AD7998_CYC_MASK;
221
222 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
223 if (val == ad7998_frequencies[i])
224 break;
225 if (i == ARRAY_SIZE(ad7998_frequencies)) {
226 ret = -EINVAL;
227 goto error_ret_mutex;
228 }
229 t |= i;
230 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
231
232 error_ret_mutex:
233 mutex_unlock(&indio_dev->mlock);
234
235 return ret ? ret : len;
236 }
237
ad799x_read_event_config(struct iio_dev * indio_dev,u64 event_code)238 static int ad799x_read_event_config(struct iio_dev *indio_dev,
239 u64 event_code)
240 {
241 return 1;
242 }
243
244 static const u8 ad799x_threshold_addresses[][2] = {
245 { AD7998_DATALOW_CH1_REG, AD7998_DATAHIGH_CH1_REG },
246 { AD7998_DATALOW_CH2_REG, AD7998_DATAHIGH_CH2_REG },
247 { AD7998_DATALOW_CH3_REG, AD7998_DATAHIGH_CH3_REG },
248 { AD7998_DATALOW_CH4_REG, AD7998_DATAHIGH_CH4_REG },
249 };
250
ad799x_write_event_value(struct iio_dev * indio_dev,u64 event_code,int val)251 static int ad799x_write_event_value(struct iio_dev *indio_dev,
252 u64 event_code,
253 int val)
254 {
255 int ret;
256 struct ad799x_state *st = iio_priv(indio_dev);
257 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
258 IIO_EV_DIR_FALLING);
259 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
260
261 mutex_lock(&indio_dev->mlock);
262 ret = ad799x_i2c_write16(st,
263 ad799x_threshold_addresses[number][direction],
264 val);
265 mutex_unlock(&indio_dev->mlock);
266
267 return ret;
268 }
269
ad799x_read_event_value(struct iio_dev * indio_dev,u64 event_code,int * val)270 static int ad799x_read_event_value(struct iio_dev *indio_dev,
271 u64 event_code,
272 int *val)
273 {
274 int ret;
275 struct ad799x_state *st = iio_priv(indio_dev);
276 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
277 IIO_EV_DIR_FALLING);
278 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
279 u16 valin;
280
281 mutex_lock(&indio_dev->mlock);
282 ret = ad799x_i2c_read16(st,
283 ad799x_threshold_addresses[number][direction],
284 &valin);
285 mutex_unlock(&indio_dev->mlock);
286 if (ret < 0)
287 return ret;
288 *val = valin;
289
290 return 0;
291 }
292
ad799x_read_channel_config(struct device * dev,struct device_attribute * attr,char * buf)293 static ssize_t ad799x_read_channel_config(struct device *dev,
294 struct device_attribute *attr,
295 char *buf)
296 {
297 struct iio_dev *indio_dev = dev_get_drvdata(dev);
298 struct ad799x_state *st = iio_priv(indio_dev);
299 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
300
301 int ret;
302 u16 val;
303 ret = ad799x_i2c_read16(st, this_attr->address, &val);
304 if (ret)
305 return ret;
306
307 return sprintf(buf, "%d\n", val);
308 }
309
ad799x_write_channel_config(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)310 static ssize_t ad799x_write_channel_config(struct device *dev,
311 struct device_attribute *attr,
312 const char *buf,
313 size_t len)
314 {
315 struct iio_dev *indio_dev = dev_get_drvdata(dev);
316 struct ad799x_state *st = iio_priv(indio_dev);
317 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
318
319 long val;
320 int ret;
321
322 ret = strict_strtol(buf, 10, &val);
323 if (ret)
324 return ret;
325
326 mutex_lock(&indio_dev->mlock);
327 ret = ad799x_i2c_write16(st, this_attr->address, val);
328 mutex_unlock(&indio_dev->mlock);
329
330 return ret ? ret : len;
331 }
332
ad799x_event_handler(int irq,void * private)333 static irqreturn_t ad799x_event_handler(int irq, void *private)
334 {
335 struct iio_dev *indio_dev = private;
336 struct ad799x_state *st = iio_priv(private);
337 u8 status;
338 int i, ret;
339
340 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
341 if (ret)
342 return ret;
343
344 if (!status)
345 return -EIO;
346
347 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
348
349 for (i = 0; i < 8; i++) {
350 if (status & (1 << i))
351 iio_push_event(indio_dev,
352 i & 0x1 ?
353 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
354 (i >> 1),
355 IIO_EV_TYPE_THRESH,
356 IIO_EV_DIR_RISING) :
357 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
358 (i >> 1),
359 IIO_EV_TYPE_THRESH,
360 IIO_EV_DIR_FALLING),
361 iio_get_time_ns());
362 }
363
364 return IRQ_HANDLED;
365 }
366
367 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
368 S_IRUGO | S_IWUSR,
369 ad799x_read_channel_config,
370 ad799x_write_channel_config,
371 AD7998_HYST_CH1_REG);
372
373 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
374 S_IRUGO | S_IWUSR,
375 ad799x_read_channel_config,
376 ad799x_write_channel_config,
377 AD7998_HYST_CH2_REG);
378
379 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
380 S_IRUGO | S_IWUSR,
381 ad799x_read_channel_config,
382 ad799x_write_channel_config,
383 AD7998_HYST_CH3_REG);
384
385 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
386 S_IRUGO | S_IWUSR,
387 ad799x_read_channel_config,
388 ad799x_write_channel_config,
389 AD7998_HYST_CH4_REG);
390
391 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
392 ad799x_read_frequency,
393 ad799x_write_frequency);
394 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
395
396 static struct attribute *ad7993_4_7_8_event_attributes[] = {
397 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
398 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
399 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
400 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
401 &iio_dev_attr_sampling_frequency.dev_attr.attr,
402 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
403 NULL,
404 };
405
406 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
407 .attrs = ad7993_4_7_8_event_attributes,
408 .name = "events",
409 };
410
411 static struct attribute *ad7992_event_attributes[] = {
412 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
413 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
414 &iio_dev_attr_sampling_frequency.dev_attr.attr,
415 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
416 NULL,
417 };
418
419 static struct attribute_group ad7992_event_attrs_group = {
420 .attrs = ad7992_event_attributes,
421 .name = "events",
422 };
423
424 static const struct iio_info ad7991_info = {
425 .read_raw = &ad799x_read_raw,
426 .driver_module = THIS_MODULE,
427 };
428
429 static const struct iio_info ad7992_info = {
430 .read_raw = &ad799x_read_raw,
431 .event_attrs = &ad7992_event_attrs_group,
432 .read_event_config = &ad799x_read_event_config,
433 .read_event_value = &ad799x_read_event_value,
434 .write_event_value = &ad799x_write_event_value,
435 .driver_module = THIS_MODULE,
436 };
437
438 static const struct iio_info ad7993_4_7_8_info = {
439 .read_raw = &ad799x_read_raw,
440 .event_attrs = &ad7993_4_7_8_event_attrs_group,
441 .read_event_config = &ad799x_read_event_config,
442 .read_event_value = &ad799x_read_event_value,
443 .write_event_value = &ad799x_write_event_value,
444 .driver_module = THIS_MODULE,
445 };
446
447 #define AD799X_EV_MASK (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
448 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
449
450 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
451 [ad7991] = {
452 .channel = {
453 [0] = {
454 .type = IIO_VOLTAGE,
455 .indexed = 1,
456 .channel = 0,
457 .scan_index = 0,
458 .scan_type = IIO_ST('u', 12, 16, 0),
459 },
460 [1] = {
461 .type = IIO_VOLTAGE,
462 .indexed = 1,
463 .channel = 1,
464 .scan_index = 1,
465 .scan_type = IIO_ST('u', 12, 16, 0),
466 },
467 [2] = {
468 .type = IIO_VOLTAGE,
469 .indexed = 1,
470 .channel = 2,
471 .scan_index = 2,
472 .scan_type = IIO_ST('u', 12, 16, 0),
473 },
474 [3] = {
475 .type = IIO_VOLTAGE,
476 .indexed = 1,
477 .channel = 3,
478 .scan_index = 3,
479 .scan_type = IIO_ST('u', 12, 16, 0),
480 },
481 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
482 },
483 .num_channels = 5,
484 .int_vref_mv = 4096,
485 .info = &ad7991_info,
486 },
487 [ad7995] = {
488 .channel = {
489 [0] = {
490 .type = IIO_VOLTAGE,
491 .indexed = 1,
492 .channel = 0,
493 .scan_index = 0,
494 .scan_type = IIO_ST('u', 10, 16, 2),
495 },
496 [1] = {
497 .type = IIO_VOLTAGE,
498 .indexed = 1,
499 .channel = 1,
500 .scan_index = 1,
501 .scan_type = IIO_ST('u', 10, 16, 2),
502 },
503 [2] = {
504 .type = IIO_VOLTAGE,
505 .indexed = 1,
506 .channel = 2,
507 .scan_index = 2,
508 .scan_type = IIO_ST('u', 10, 16, 2),
509 },
510 [3] = {
511 .type = IIO_VOLTAGE,
512 .indexed = 1,
513 .channel = 3,
514 .scan_index = 3,
515 .scan_type = IIO_ST('u', 10, 16, 2),
516 },
517 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
518 },
519 .num_channels = 5,
520 .int_vref_mv = 1024,
521 .info = &ad7991_info,
522 },
523 [ad7999] = {
524 .channel = {
525 [0] = {
526 .type = IIO_VOLTAGE,
527 .indexed = 1,
528 .channel = 0,
529 .scan_index = 0,
530 .scan_type = IIO_ST('u', 8, 16, 4),
531 },
532 [1] = {
533 .type = IIO_VOLTAGE,
534 .indexed = 1,
535 .channel = 1,
536 .scan_index = 1,
537 .scan_type = IIO_ST('u', 8, 16, 4),
538 },
539 [2] = {
540 .type = IIO_VOLTAGE,
541 .indexed = 1,
542 .channel = 2,
543 .scan_index = 2,
544 .scan_type = IIO_ST('u', 8, 16, 4),
545 },
546 [3] = {
547 .type = IIO_VOLTAGE,
548 .indexed = 1,
549 .channel = 3,
550 .scan_index = 3,
551 .scan_type = IIO_ST('u', 8, 16, 4),
552 },
553 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
554 },
555 .num_channels = 5,
556 .int_vref_mv = 1024,
557 .info = &ad7991_info,
558 },
559 [ad7992] = {
560 .channel = {
561 [0] = {
562 .type = IIO_VOLTAGE,
563 .indexed = 1,
564 .channel = 0,
565 .scan_index = 0,
566 .scan_type = IIO_ST('u', 12, 16, 0),
567 .event_mask = AD799X_EV_MASK,
568 },
569 [1] = {
570 .type = IIO_VOLTAGE,
571 .indexed = 1,
572 .channel = 1,
573 .scan_index = 1,
574 .scan_type = IIO_ST('u', 12, 16, 0),
575 .event_mask = AD799X_EV_MASK,
576 },
577 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
578 },
579 .num_channels = 3,
580 .int_vref_mv = 4096,
581 .default_config = AD7998_ALERT_EN,
582 .info = &ad7992_info,
583 },
584 [ad7993] = {
585 .channel = {
586 [0] = {
587 .type = IIO_VOLTAGE,
588 .indexed = 1,
589 .channel = 0,
590 .scan_index = 0,
591 .scan_type = IIO_ST('u', 10, 16, 2),
592 .event_mask = AD799X_EV_MASK,
593 },
594 [1] = {
595 .type = IIO_VOLTAGE,
596 .indexed = 1,
597 .channel = 1,
598 .scan_index = 1,
599 .scan_type = IIO_ST('u', 10, 16, 2),
600 .event_mask = AD799X_EV_MASK,
601 },
602 [2] = {
603 .type = IIO_VOLTAGE,
604 .indexed = 1,
605 .channel = 2,
606 .scan_index = 2,
607 .scan_type = IIO_ST('u', 10, 16, 2),
608 .event_mask = AD799X_EV_MASK,
609 },
610 [3] = {
611 .type = IIO_VOLTAGE,
612 .indexed = 1,
613 .channel = 3,
614 .scan_index = 3,
615 .scan_type = IIO_ST('u', 10, 16, 2),
616 .event_mask = AD799X_EV_MASK,
617 },
618 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
619 },
620 .num_channels = 5,
621 .int_vref_mv = 1024,
622 .default_config = AD7998_ALERT_EN,
623 .info = &ad7993_4_7_8_info,
624 },
625 [ad7994] = {
626 .channel = {
627 [0] = {
628 .type = IIO_VOLTAGE,
629 .indexed = 1,
630 .channel = 0,
631 .scan_index = 0,
632 .scan_type = IIO_ST('u', 12, 16, 0),
633 .event_mask = AD799X_EV_MASK,
634 },
635 [1] = {
636 .type = IIO_VOLTAGE,
637 .indexed = 1,
638 .channel = 1,
639 .scan_index = 1,
640 .scan_type = IIO_ST('u', 12, 16, 0),
641 .event_mask = AD799X_EV_MASK,
642 },
643 [2] = {
644 .type = IIO_VOLTAGE,
645 .indexed = 1,
646 .channel = 2,
647 .scan_index = 2,
648 .scan_type = IIO_ST('u', 12, 16, 0),
649 .event_mask = AD799X_EV_MASK,
650 },
651 [3] = {
652 .type = IIO_VOLTAGE,
653 .indexed = 1,
654 .channel = 3,
655 .scan_index = 3,
656 .scan_type = IIO_ST('u', 12, 16, 0),
657 .event_mask = AD799X_EV_MASK,
658 },
659 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
660 },
661 .num_channels = 5,
662 .int_vref_mv = 4096,
663 .default_config = AD7998_ALERT_EN,
664 .info = &ad7993_4_7_8_info,
665 },
666 [ad7997] = {
667 .channel = {
668 [0] = {
669 .type = IIO_VOLTAGE,
670 .indexed = 1,
671 .channel = 0,
672 .scan_index = 0,
673 .scan_type = IIO_ST('u', 10, 16, 2),
674 .event_mask = AD799X_EV_MASK,
675 },
676 [1] = {
677 .type = IIO_VOLTAGE,
678 .indexed = 1,
679 .channel = 1,
680 .scan_index = 1,
681 .scan_type = IIO_ST('u', 10, 16, 2),
682 .event_mask = AD799X_EV_MASK,
683 },
684 [2] = {
685 .type = IIO_VOLTAGE,
686 .indexed = 1,
687 .channel = 2,
688 .scan_index = 2,
689 .scan_type = IIO_ST('u', 10, 16, 2),
690 .event_mask = AD799X_EV_MASK,
691 },
692 [3] = {
693 .type = IIO_VOLTAGE,
694 .indexed = 1,
695 .channel = 3,
696 .scan_index = 3,
697 .scan_type = IIO_ST('u', 10, 16, 2),
698 .event_mask = AD799X_EV_MASK,
699 },
700 [4] = {
701 .type = IIO_VOLTAGE,
702 .indexed = 1,
703 .channel = 4,
704 .scan_index = 4,
705 .scan_type = IIO_ST('u', 10, 16, 2),
706 },
707 [5] = {
708 .type = IIO_VOLTAGE,
709 .indexed = 1,
710 .channel = 5,
711 .scan_index = 5,
712 .scan_type = IIO_ST('u', 10, 16, 2),
713 },
714 [6] = {
715 .type = IIO_VOLTAGE,
716 .indexed = 1,
717 .channel = 6,
718 .scan_index = 6,
719 .scan_type = IIO_ST('u', 10, 16, 2),
720 },
721 [7] = {
722 .type = IIO_VOLTAGE,
723 .indexed = 1,
724 .channel = 7,
725 .scan_index = 7,
726 .scan_type = IIO_ST('u', 10, 16, 2),
727 },
728 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
729 },
730 .num_channels = 9,
731 .int_vref_mv = 1024,
732 .default_config = AD7998_ALERT_EN,
733 .info = &ad7993_4_7_8_info,
734 },
735 [ad7998] = {
736 .channel = {
737 [0] = {
738 .type = IIO_VOLTAGE,
739 .indexed = 1,
740 .channel = 0,
741 .scan_index = 0,
742 .scan_type = IIO_ST('u', 12, 16, 0),
743 .event_mask = AD799X_EV_MASK,
744 },
745 [1] = {
746 .type = IIO_VOLTAGE,
747 .indexed = 1,
748 .channel = 1,
749 .scan_index = 1,
750 .scan_type = IIO_ST('u', 12, 16, 0),
751 .event_mask = AD799X_EV_MASK,
752 },
753 [2] = {
754 .type = IIO_VOLTAGE,
755 .indexed = 1,
756 .channel = 2,
757 .scan_index = 2,
758 .scan_type = IIO_ST('u', 12, 16, 0),
759 .event_mask = AD799X_EV_MASK,
760 },
761 [3] = {
762 .type = IIO_VOLTAGE,
763 .indexed = 1,
764 .channel = 3,
765 .scan_index = 3,
766 .scan_type = IIO_ST('u', 12, 16, 0),
767 .event_mask = AD799X_EV_MASK,
768 },
769 [4] = {
770 .type = IIO_VOLTAGE,
771 .indexed = 1,
772 .channel = 4,
773 .scan_index = 4,
774 .scan_type = IIO_ST('u', 12, 16, 0),
775 },
776 [5] = {
777 .type = IIO_VOLTAGE,
778 .indexed = 1,
779 .channel = 5,
780 .scan_index = 5,
781 .scan_type = IIO_ST('u', 12, 16, 0),
782 },
783 [6] = {
784 .type = IIO_VOLTAGE,
785 .indexed = 1,
786 .channel = 6,
787 .scan_index = 6,
788 .scan_type = IIO_ST('u', 12, 16, 0),
789 },
790 [7] = {
791 .type = IIO_VOLTAGE,
792 .indexed = 1,
793 .channel = 7,
794 .scan_index = 7,
795 .scan_type = IIO_ST('u', 12, 16, 0),
796 },
797 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
798 },
799 .num_channels = 9,
800 .int_vref_mv = 4096,
801 .default_config = AD7998_ALERT_EN,
802 .info = &ad7993_4_7_8_info,
803 },
804 };
805
ad799x_probe(struct i2c_client * client,const struct i2c_device_id * id)806 static int __devinit ad799x_probe(struct i2c_client *client,
807 const struct i2c_device_id *id)
808 {
809 int ret;
810 struct ad799x_platform_data *pdata = client->dev.platform_data;
811 struct ad799x_state *st;
812 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
813
814 if (indio_dev == NULL)
815 return -ENOMEM;
816
817 st = iio_priv(indio_dev);
818 /* this is only used for device removal purposes */
819 i2c_set_clientdata(client, indio_dev);
820
821 st->id = id->driver_data;
822 st->chip_info = &ad799x_chip_info_tbl[st->id];
823 st->config = st->chip_info->default_config;
824
825 /* TODO: Add pdata options for filtering and bit delay */
826
827 if (pdata)
828 st->int_vref_mv = pdata->vref_mv;
829 else
830 st->int_vref_mv = st->chip_info->int_vref_mv;
831
832 st->reg = regulator_get(&client->dev, "vcc");
833 if (!IS_ERR(st->reg)) {
834 ret = regulator_enable(st->reg);
835 if (ret)
836 goto error_put_reg;
837 }
838 st->client = client;
839
840 indio_dev->dev.parent = &client->dev;
841 indio_dev->name = id->name;
842 indio_dev->info = st->chip_info->info;
843
844 indio_dev->modes = INDIO_DIRECT_MODE;
845 indio_dev->channels = st->chip_info->channel;
846 indio_dev->num_channels = st->chip_info->num_channels;
847
848 ret = ad799x_register_ring_funcs_and_init(indio_dev);
849 if (ret)
850 goto error_disable_reg;
851
852 ret = iio_buffer_register(indio_dev,
853 indio_dev->channels,
854 indio_dev->num_channels);
855 if (ret)
856 goto error_cleanup_ring;
857
858 if (client->irq > 0) {
859 ret = request_threaded_irq(client->irq,
860 NULL,
861 ad799x_event_handler,
862 IRQF_TRIGGER_FALLING |
863 IRQF_ONESHOT,
864 client->name,
865 indio_dev);
866 if (ret)
867 goto error_cleanup_ring;
868 }
869 ret = iio_device_register(indio_dev);
870 if (ret)
871 goto error_free_irq;
872
873 return 0;
874
875 error_free_irq:
876 if (client->irq > 0)
877 free_irq(client->irq, indio_dev);
878 error_cleanup_ring:
879 ad799x_ring_cleanup(indio_dev);
880 error_disable_reg:
881 if (!IS_ERR(st->reg))
882 regulator_disable(st->reg);
883 error_put_reg:
884 if (!IS_ERR(st->reg))
885 regulator_put(st->reg);
886 iio_free_device(indio_dev);
887
888 return ret;
889 }
890
ad799x_remove(struct i2c_client * client)891 static __devexit int ad799x_remove(struct i2c_client *client)
892 {
893 struct iio_dev *indio_dev = i2c_get_clientdata(client);
894 struct ad799x_state *st = iio_priv(indio_dev);
895
896 iio_device_unregister(indio_dev);
897 if (client->irq > 0)
898 free_irq(client->irq, indio_dev);
899
900 iio_buffer_unregister(indio_dev);
901 ad799x_ring_cleanup(indio_dev);
902 if (!IS_ERR(st->reg)) {
903 regulator_disable(st->reg);
904 regulator_put(st->reg);
905 }
906 iio_free_device(indio_dev);
907
908 return 0;
909 }
910
911 static const struct i2c_device_id ad799x_id[] = {
912 { "ad7991", ad7991 },
913 { "ad7995", ad7995 },
914 { "ad7999", ad7999 },
915 { "ad7992", ad7992 },
916 { "ad7993", ad7993 },
917 { "ad7994", ad7994 },
918 { "ad7997", ad7997 },
919 { "ad7998", ad7998 },
920 {}
921 };
922
923 MODULE_DEVICE_TABLE(i2c, ad799x_id);
924
925 static struct i2c_driver ad799x_driver = {
926 .driver = {
927 .name = "ad799x",
928 },
929 .probe = ad799x_probe,
930 .remove = __devexit_p(ad799x_remove),
931 .id_table = ad799x_id,
932 };
933 module_i2c_driver(ad799x_driver);
934
935 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
936 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
937 MODULE_LICENSE("GPL v2");
938