staging: iio: adc: ad799x drop in_precision in favor of new in_type
[deliverable/linux.git] / drivers / staging / iio / adc / ad799x_core.c
CommitLineData
985dbe77
MH
1/*
2 * iio/adc/ad799x.c
3 * Copyright (C) 2010 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/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>
36
37#include "../iio.h"
38#include "../sysfs.h"
39
40#include "../ring_generic.h"
41#include "adc.h"
42#include "ad799x.h"
43
44/*
45 * ad799x register access by I2C
46 */
47static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
48{
49 struct i2c_client *client = st->client;
50 int ret = 0;
51
52 ret = i2c_smbus_read_word_data(client, reg);
53 if (ret < 0) {
54 dev_err(&client->dev, "I2C read error\n");
55 return ret;
56 }
57
58 *data = swab16((u16)ret);
59
60 return 0;
61}
62
63static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
64{
65 struct i2c_client *client = st->client;
66 int ret = 0;
67
aecac191 68 ret = i2c_smbus_read_byte_data(client, reg);
985dbe77
MH
69 if (ret < 0) {
70 dev_err(&client->dev, "I2C read error\n");
71 return ret;
72 }
73
aecac191 74 *data = (u8)ret;
985dbe77
MH
75
76 return 0;
77}
78
79static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
80{
81 struct i2c_client *client = st->client;
82 int ret = 0;
83
84 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
85 if (ret < 0)
86 dev_err(&client->dev, "I2C write error\n");
87
88 return ret;
89}
90
91static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
92{
93 struct i2c_client *client = st->client;
94 int ret = 0;
95
96 ret = i2c_smbus_write_byte_data(client, reg, data);
97 if (ret < 0)
98 dev_err(&client->dev, "I2C write error\n");
99
100 return ret;
101}
102
103static int ad799x_scan_el_set_state(struct iio_scan_el *scan_el,
104 struct iio_dev *indio_dev,
105 bool state)
106{
107 struct ad799x_state *st = indio_dev->dev_data;
108 return ad799x_set_scan_mode(st, st->indio_dev->ring->scan_mask);
109}
110
111/* Here we claim all are 16 bits. This currently does no harm and saves
112 * us a lot of scan element listings */
113
114#define AD799X_SCAN_EL(number) \
115 IIO_SCAN_EL_C(in##number, number, 0, ad799x_scan_el_set_state);
116
117static AD799X_SCAN_EL(0);
118static AD799X_SCAN_EL(1);
119static AD799X_SCAN_EL(2);
120static AD799X_SCAN_EL(3);
121static AD799X_SCAN_EL(4);
122static AD799X_SCAN_EL(5);
123static AD799X_SCAN_EL(6);
124static AD799X_SCAN_EL(7);
125
3ba2e493 126static ssize_t ad799x_show_type(struct device *dev,
985dbe77
MH
127 struct device_attribute *attr,
128 char *buf)
129{
3ba2e493
MH
130 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
131 struct iio_dev *indio_dev = ring->indio_dev;
132 struct ad799x_state *st = indio_dev->dev_data;
985dbe77 133
3ba2e493
MH
134 return sprintf(buf, "%c%d/%d\n", st->chip_info->sign,
135 st->chip_info->bits, AD799X_STORAGEBITS);
136}
137static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad799x_show_type, NULL, 0);
985dbe77
MH
138
139static int ad7991_5_9_set_scan_mode(struct ad799x_state *st, unsigned mask)
140{
141 return i2c_smbus_write_byte(st->client,
142 st->config | (mask << AD799X_CHANNEL_SHIFT));
143}
144
145static int ad7992_3_4_set_scan_mode(struct ad799x_state *st, unsigned mask)
146{
147 return ad799x_i2c_write8(st, AD7998_CONF_REG,
148 st->config | (mask << AD799X_CHANNEL_SHIFT));
149}
150
151static int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
152{
153 return ad799x_i2c_write16(st, AD7998_CONF_REG,
154 st->config | (mask << AD799X_CHANNEL_SHIFT));
155}
156
157int ad799x_set_scan_mode(struct ad799x_state *st, unsigned mask)
158{
159 int ret;
160
161 if (st->chip_info->ad799x_set_scan_mode != NULL) {
162 ret = st->chip_info->ad799x_set_scan_mode(st, mask);
163 return (ret > 0) ? 0 : ret;
164 }
165
166 return 0;
167}
168
169static ssize_t ad799x_read_single_channel(struct device *dev,
170 struct device_attribute *attr,
171 char *buf)
172{
173 struct iio_dev *dev_info = dev_get_drvdata(dev);
174 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
175 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
176 int ret = 0, len = 0;
177 u32 data ;
178 u16 rxbuf[1];
179 u8 cmd;
180 long mask;
181
182 mutex_lock(&dev_info->mlock);
183 mask = 1 << this_attr->address;
184 /* If ring buffer capture is occuring, query the buffer */
185 if (iio_ring_enabled(dev_info)) {
aecac191
MH
186 data = ret = ad799x_single_channel_from_ring(st, mask);
187 if (ret < 0)
985dbe77 188 goto error_ret;
aecac191 189 ret = 0;
985dbe77
MH
190 } else {
191 switch (st->id) {
192 case ad7991:
193 case ad7995:
194 case ad7999:
195 cmd = st->config | (mask << AD799X_CHANNEL_SHIFT);
196 break;
197 case ad7992:
198 case ad7993:
199 case ad7994:
200 cmd = mask << AD799X_CHANNEL_SHIFT;
201 break;
202 case ad7997:
203 case ad7998:
204 cmd = (this_attr->address <<
205 AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
206 break;
207 default:
208 cmd = 0;
209
210 }
211 ret = ad799x_i2c_read16(st, cmd, rxbuf);
212 if (ret < 0)
213 goto error_ret;
214
3ba2e493 215 data = rxbuf[0];
985dbe77
MH
216 }
217
218 /* Pretty print the result */
3ba2e493 219 len = sprintf(buf, "%u\n", data & ((1 << (st->chip_info->bits)) - 1));
985dbe77
MH
220
221error_ret:
222 mutex_unlock(&dev_info->mlock);
223 return ret ? ret : len;
224}
225
226static ssize_t ad799x_read_frequency(struct device *dev,
227 struct device_attribute *attr,
228 char *buf)
229{
230 struct iio_dev *dev_info = dev_get_drvdata(dev);
231 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
232
233 int ret, len = 0;
234 u8 val;
235 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
236 if (ret)
237 return ret;
238
239 val &= AD7998_CYC_MASK;
240
241 switch (val) {
242 case AD7998_CYC_DIS:
243 len = sprintf(buf, "0\n");
244 break;
245 case AD7998_CYC_TCONF_32:
246 len = sprintf(buf, "15625\n");
247 break;
248 case AD7998_CYC_TCONF_64:
249 len = sprintf(buf, "7812\n");
250 break;
251 case AD7998_CYC_TCONF_128:
252 len = sprintf(buf, "3906\n");
253 break;
254 case AD7998_CYC_TCONF_256:
255 len = sprintf(buf, "1953\n");
256 break;
257 case AD7998_CYC_TCONF_512:
258 len = sprintf(buf, "976\n");
259 break;
260 case AD7998_CYC_TCONF_1024:
261 len = sprintf(buf, "488\n");
262 break;
263 case AD7998_CYC_TCONF_2048:
264 len = sprintf(buf, "244\n");
265 break;
266 }
267 return len;
268}
269
270static ssize_t ad799x_write_frequency(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf,
273 size_t len)
274{
275 struct iio_dev *dev_info = dev_get_drvdata(dev);
276 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
277
278 long val;
279 int ret;
280 u8 t;
281
282 ret = strict_strtol(buf, 10, &val);
283 if (ret)
284 return ret;
285
286 mutex_lock(&dev_info->mlock);
287 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
288 if (ret)
289 goto error_ret_mutex;
290 /* Wipe the bits clean */
291 t &= ~AD7998_CYC_MASK;
292
293 switch (val) {
294 case 15625:
295 t |= AD7998_CYC_TCONF_32;
296 break;
297 case 7812:
298 t |= AD7998_CYC_TCONF_64;
299 break;
300 case 3906:
301 t |= AD7998_CYC_TCONF_128;
302 break;
303 case 1953:
304 t |= AD7998_CYC_TCONF_256;
305 break;
306 case 976:
307 t |= AD7998_CYC_TCONF_512;
308 break;
309 case 488:
310 t |= AD7998_CYC_TCONF_1024;
311 break;
312 case 244:
313 t |= AD7998_CYC_TCONF_2048;
314 break;
315 case 0:
316 t |= AD7998_CYC_DIS;
317 break;
318 default:
319 ret = -EINVAL;
320 goto error_ret_mutex;
321 }
322
323 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
324
325error_ret_mutex:
326 mutex_unlock(&dev_info->mlock);
327
328 return ret ? ret : len;
329}
330
331
332static ssize_t ad799x_read_channel_config(struct device *dev,
333 struct device_attribute *attr,
334 char *buf)
335{
336 struct iio_dev *dev_info = dev_get_drvdata(dev);
337 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
338 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
339
340 int ret;
341 u16 val;
342 ret = ad799x_i2c_read16(st, this_attr->mask, &val);
343 if (ret)
344 return ret;
345
346 return sprintf(buf, "%d\n", val);
347}
348
349static ssize_t ad799x_write_channel_config(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf,
352 size_t len)
353{
354 struct iio_dev *dev_info = dev_get_drvdata(dev);
355 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
356 struct iio_event_attr *this_attr = to_iio_event_attr(attr);
357
358 long val;
359 int ret;
360
361 ret = strict_strtol(buf, 10, &val);
362 if (ret)
363 return ret;
364
365 mutex_lock(&dev_info->mlock);
366 ret = ad799x_i2c_write16(st, this_attr->mask, val);
367 mutex_unlock(&dev_info->mlock);
368
369 return ret ? ret : len;
370}
371
372static void ad799x_interrupt_bh(struct work_struct *work_s)
373{
374 struct ad799x_state *st = container_of(work_s,
375 struct ad799x_state, work_thresh);
376 u8 status;
377 int i;
378
379 if (ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status))
380 goto err_out;
381
382 if (!status)
383 goto err_out;
384
385 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
386
387 for (i = 0; i < 8; i++) {
388 if (status & (1 << i))
389 iio_push_event(st->indio_dev, 0,
390 i & 0x1 ?
391 IIO_EVENT_CODE_IN_HIGH_THRESH(i >> 1) :
392 IIO_EVENT_CODE_IN_LOW_THRESH(i >> 1),
393 st->last_timestamp);
394 }
395
396err_out:
397 enable_irq(st->client->irq);
398}
399
400static int ad799x_interrupt(struct iio_dev *dev_info,
401 int index,
402 s64 timestamp,
403 int no_test)
404{
405 struct ad799x_state *st = dev_info->dev_data;
406
407 st->last_timestamp = timestamp;
408 schedule_work(&st->work_thresh);
409 return 0;
410}
411
412IIO_EVENT_SH(ad799x, &ad799x_interrupt);
413
414/* Direct read attribtues */
415static IIO_DEV_ATTR_IN_RAW(0, ad799x_read_single_channel, 0);
416static IIO_DEV_ATTR_IN_RAW(1, ad799x_read_single_channel, 1);
417static IIO_DEV_ATTR_IN_RAW(2, ad799x_read_single_channel, 2);
418static IIO_DEV_ATTR_IN_RAW(3, ad799x_read_single_channel, 3);
419static IIO_DEV_ATTR_IN_RAW(4, ad799x_read_single_channel, 4);
420static IIO_DEV_ATTR_IN_RAW(5, ad799x_read_single_channel, 5);
421static IIO_DEV_ATTR_IN_RAW(6, ad799x_read_single_channel, 6);
422static IIO_DEV_ATTR_IN_RAW(7, ad799x_read_single_channel, 7);
423
424static ssize_t ad799x_show_scale(struct device *dev,
425 struct device_attribute *attr,
426 char *buf)
427{
428 /* Driver currently only support internal vref */
429 struct iio_dev *dev_info = dev_get_drvdata(dev);
430 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
431 /* Corresponds to Vref / 2^(bits) */
432
433 if ((1 << (st->chip_info->bits + 1))
434 > st->int_vref_mv)
435 return sprintf(buf, "0.5\n");
436 else
437 return sprintf(buf, "%d\n",
438 st->int_vref_mv >> st->chip_info->bits);
439}
440
441static IIO_DEVICE_ATTR(in_scale, S_IRUGO, ad799x_show_scale, NULL, 0);
442
443static ssize_t ad799x_show_name(struct device *dev,
444 struct device_attribute *attr,
445 char *buf)
446{
447 struct iio_dev *dev_info = dev_get_drvdata(dev);
448 struct ad799x_state *st = iio_dev_get_devdata(dev_info);
449 return sprintf(buf, "%s\n", st->client->name);
450}
451
452static IIO_DEVICE_ATTR(name, S_IRUGO, ad799x_show_name, NULL, 0);
453
454static struct attribute *ad7991_5_9_3_4_device_attrs[] = {
455 &iio_dev_attr_in0_raw.dev_attr.attr,
456 &iio_dev_attr_in1_raw.dev_attr.attr,
457 &iio_dev_attr_in2_raw.dev_attr.attr,
458 &iio_dev_attr_in3_raw.dev_attr.attr,
459 &iio_dev_attr_name.dev_attr.attr,
460 &iio_dev_attr_in_scale.dev_attr.attr,
461 NULL
462};
463
464static struct attribute_group ad7991_5_9_3_4_dev_attr_group = {
465 .attrs = ad7991_5_9_3_4_device_attrs,
466};
467
468static struct attribute *ad7991_5_9_3_4_scan_el_attrs[] = {
469 &iio_scan_el_in0.dev_attr.attr,
470 &iio_const_attr_in0_index.dev_attr.attr,
471 &iio_scan_el_in1.dev_attr.attr,
472 &iio_const_attr_in1_index.dev_attr.attr,
473 &iio_scan_el_in2.dev_attr.attr,
474 &iio_const_attr_in2_index.dev_attr.attr,
475 &iio_scan_el_in3.dev_attr.attr,
476 &iio_const_attr_in3_index.dev_attr.attr,
3ba2e493 477 &iio_dev_attr_in_type.dev_attr.attr,
985dbe77
MH
478 NULL,
479};
480
481static struct attribute_group ad7991_5_9_3_4_scan_el_group = {
482 .name = "scan_elements",
483 .attrs = ad7991_5_9_3_4_scan_el_attrs,
484};
485
486static struct attribute *ad7992_device_attrs[] = {
487 &iio_dev_attr_in0_raw.dev_attr.attr,
488 &iio_dev_attr_in1_raw.dev_attr.attr,
489 &iio_dev_attr_name.dev_attr.attr,
490 &iio_dev_attr_in_scale.dev_attr.attr,
491 NULL
492};
493
494static struct attribute_group ad7992_dev_attr_group = {
495 .attrs = ad7992_device_attrs,
496};
497
498static struct attribute *ad7992_scan_el_attrs[] = {
499 &iio_scan_el_in0.dev_attr.attr,
500 &iio_const_attr_in0_index.dev_attr.attr,
501 &iio_scan_el_in1.dev_attr.attr,
502 &iio_const_attr_in1_index.dev_attr.attr,
3ba2e493 503 &iio_dev_attr_in_type.dev_attr.attr,
985dbe77
MH
504 NULL,
505};
506
507static struct attribute_group ad7992_scan_el_group = {
508 .name = "scan_elements",
509 .attrs = ad7992_scan_el_attrs,
510};
511
512static struct attribute *ad7997_8_device_attrs[] = {
513 &iio_dev_attr_in0_raw.dev_attr.attr,
514 &iio_dev_attr_in1_raw.dev_attr.attr,
515 &iio_dev_attr_in2_raw.dev_attr.attr,
516 &iio_dev_attr_in3_raw.dev_attr.attr,
517 &iio_dev_attr_in4_raw.dev_attr.attr,
518 &iio_dev_attr_in5_raw.dev_attr.attr,
519 &iio_dev_attr_in6_raw.dev_attr.attr,
520 &iio_dev_attr_in7_raw.dev_attr.attr,
521 &iio_dev_attr_name.dev_attr.attr,
522 &iio_dev_attr_in_scale.dev_attr.attr,
523 NULL
524};
525
526static struct attribute_group ad7997_8_dev_attr_group = {
527 .attrs = ad7997_8_device_attrs,
528};
529
530static struct attribute *ad7997_8_scan_el_attrs[] = {
531 &iio_scan_el_in0.dev_attr.attr,
532 &iio_const_attr_in0_index.dev_attr.attr,
533 &iio_scan_el_in1.dev_attr.attr,
534 &iio_const_attr_in1_index.dev_attr.attr,
535 &iio_scan_el_in2.dev_attr.attr,
536 &iio_const_attr_in2_index.dev_attr.attr,
537 &iio_scan_el_in3.dev_attr.attr,
538 &iio_const_attr_in3_index.dev_attr.attr,
539 &iio_scan_el_in4.dev_attr.attr,
540 &iio_const_attr_in4_index.dev_attr.attr,
541 &iio_scan_el_in5.dev_attr.attr,
542 &iio_const_attr_in5_index.dev_attr.attr,
543 &iio_scan_el_in6.dev_attr.attr,
544 &iio_const_attr_in6_index.dev_attr.attr,
545 &iio_scan_el_in7.dev_attr.attr,
546 &iio_const_attr_in7_index.dev_attr.attr,
3ba2e493 547 &iio_dev_attr_in_type.dev_attr.attr,
985dbe77
MH
548 NULL,
549};
550
551static struct attribute_group ad7997_8_scan_el_group = {
552 .name = "scan_elements",
553 .attrs = ad7997_8_scan_el_attrs,
554};
555
556IIO_EVENT_ATTR_SH(in0_thresh_low_value,
557 iio_event_ad799x,
558 ad799x_read_channel_config,
559 ad799x_write_channel_config,
560 AD7998_DATALOW_CH1_REG);
561
562IIO_EVENT_ATTR_SH(in0_thresh_high_value,
563 iio_event_ad799x,
564 ad799x_read_channel_config,
565 ad799x_write_channel_config,
566 AD7998_DATAHIGH_CH1_REG);
567
568IIO_EVENT_ATTR_SH(in0_thresh_both_hyst_raw,
569 iio_event_ad799x,
570 ad799x_read_channel_config,
571 ad799x_write_channel_config,
572 AD7998_HYST_CH1_REG);
573
574IIO_EVENT_ATTR_SH(in1_thresh_low_value,
575 iio_event_ad799x,
576 ad799x_read_channel_config,
577 ad799x_write_channel_config,
578 AD7998_DATALOW_CH2_REG);
579
580IIO_EVENT_ATTR_SH(in1_thresh_high_value,
581 iio_event_ad799x,
582 ad799x_read_channel_config,
583 ad799x_write_channel_config,
584 AD7998_DATAHIGH_CH2_REG);
585
586IIO_EVENT_ATTR_SH(in1_thresh_both_hyst_raw,
587 iio_event_ad799x,
588 ad799x_read_channel_config,
589 ad799x_write_channel_config,
590 AD7998_HYST_CH2_REG);
591
592IIO_EVENT_ATTR_SH(in2_thresh_low_value,
593 iio_event_ad799x,
594 ad799x_read_channel_config,
595 ad799x_write_channel_config,
596 AD7998_DATALOW_CH3_REG);
597
598IIO_EVENT_ATTR_SH(in2_thresh_high_value,
599 iio_event_ad799x,
600 ad799x_read_channel_config,
601 ad799x_write_channel_config,
602 AD7998_DATAHIGH_CH3_REG);
603
604IIO_EVENT_ATTR_SH(in2_thresh_both_hyst_raw,
605 iio_event_ad799x,
606 ad799x_read_channel_config,
607 ad799x_write_channel_config,
608 AD7998_HYST_CH3_REG);
609
610IIO_EVENT_ATTR_SH(in3_thresh_low_value,
611 iio_event_ad799x,
612 ad799x_read_channel_config,
613 ad799x_write_channel_config,
614 AD7998_DATALOW_CH4_REG);
615
616IIO_EVENT_ATTR_SH(in3_thresh_high_value,
617 iio_event_ad799x,
618 ad799x_read_channel_config,
619 ad799x_write_channel_config,
620 AD7998_DATAHIGH_CH4_REG);
621
622IIO_EVENT_ATTR_SH(in3_thresh_both_hyst_raw,
623 iio_event_ad799x,
624 ad799x_read_channel_config,
625 ad799x_write_channel_config,
626 AD7998_HYST_CH4_REG);
627
628static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
629 ad799x_read_frequency,
630 ad799x_write_frequency);
631static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
632
633static struct attribute *ad7993_4_7_8_event_attributes[] = {
634 &iio_event_attr_in0_thresh_low_value.dev_attr.attr,
635 &iio_event_attr_in0_thresh_high_value.dev_attr.attr,
636 &iio_event_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
637 &iio_event_attr_in1_thresh_low_value.dev_attr.attr,
638 &iio_event_attr_in1_thresh_high_value.dev_attr.attr,
639 &iio_event_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
640 &iio_event_attr_in2_thresh_low_value.dev_attr.attr,
641 &iio_event_attr_in2_thresh_high_value.dev_attr.attr,
642 &iio_event_attr_in2_thresh_both_hyst_raw.dev_attr.attr,
643 &iio_event_attr_in3_thresh_low_value.dev_attr.attr,
644 &iio_event_attr_in3_thresh_high_value.dev_attr.attr,
645 &iio_event_attr_in3_thresh_both_hyst_raw.dev_attr.attr,
646 &iio_dev_attr_sampling_frequency.dev_attr.attr,
647 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
648 NULL,
649};
650
651static struct attribute_group ad7993_4_7_8_event_attrs_group = {
652 .attrs = ad7993_4_7_8_event_attributes,
653};
654
655static struct attribute *ad7992_event_attributes[] = {
656 &iio_event_attr_in0_thresh_low_value.dev_attr.attr,
657 &iio_event_attr_in0_thresh_high_value.dev_attr.attr,
658 &iio_event_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
659 &iio_event_attr_in1_thresh_low_value.dev_attr.attr,
660 &iio_event_attr_in1_thresh_high_value.dev_attr.attr,
661 &iio_event_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
662 &iio_dev_attr_sampling_frequency.dev_attr.attr,
663 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
664 NULL,
665};
666
667static struct attribute_group ad7992_event_attrs_group = {
668 .attrs = ad7992_event_attributes,
669};
670
671static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
672 [ad7991] = {
673 .num_inputs = 4,
674 .bits = 12,
3ba2e493 675 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
985dbe77
MH
676 .int_vref_mv = 4096,
677 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
678 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
679 .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
680 },
681 [ad7995] = {
682 .num_inputs = 4,
683 .bits = 10,
3ba2e493 684 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
985dbe77
MH
685 .int_vref_mv = 1024,
686 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
687 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
688 .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
689 },
690 [ad7999] = {
691 .num_inputs = 4,
692 .bits = 10,
3ba2e493 693 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
985dbe77
MH
694 .int_vref_mv = 1024,
695 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
696 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
697 .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
698 },
699 [ad7992] = {
700 .num_inputs = 2,
701 .bits = 12,
3ba2e493 702 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
985dbe77
MH
703 .int_vref_mv = 4096,
704 .monitor_mode = true,
705 .default_config = AD7998_ALERT_EN,
706 .dev_attrs = &ad7992_dev_attr_group,
707 .scan_attrs = &ad7992_scan_el_group,
708 .event_attrs = &ad7992_event_attrs_group,
709 .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
710 },
711 [ad7993] = {
712 .num_inputs = 4,
713 .bits = 10,
3ba2e493 714 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
985dbe77
MH
715 .int_vref_mv = 1024,
716 .monitor_mode = true,
717 .default_config = AD7998_ALERT_EN,
718 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
719 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
720 .event_attrs = &ad7993_4_7_8_event_attrs_group,
721 .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
722 },
723 [ad7994] = {
724 .num_inputs = 4,
725 .bits = 12,
3ba2e493 726 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
985dbe77
MH
727 .int_vref_mv = 4096,
728 .monitor_mode = true,
729 .default_config = AD7998_ALERT_EN,
730 .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
731 .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
732 .event_attrs = &ad7993_4_7_8_event_attrs_group,
733 .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
734 },
735 [ad7997] = {
736 .num_inputs = 8,
737 .bits = 10,
3ba2e493 738 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
985dbe77
MH
739 .int_vref_mv = 1024,
740 .monitor_mode = true,
741 .default_config = AD7998_ALERT_EN,
742 .dev_attrs = &ad7997_8_dev_attr_group,
743 .scan_attrs = &ad7997_8_scan_el_group,
744 .event_attrs = &ad7993_4_7_8_event_attrs_group,
745 .ad799x_set_scan_mode = ad7997_8_set_scan_mode,
746 },
747 [ad7998] = {
748 .num_inputs = 8,
749 .bits = 12,
3ba2e493 750 .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
985dbe77
MH
751 .int_vref_mv = 4096,
752 .monitor_mode = true,
753 .default_config = AD7998_ALERT_EN,
754 .dev_attrs = &ad7997_8_dev_attr_group,
755 .scan_attrs = &ad7997_8_scan_el_group,
756 .event_attrs = &ad7993_4_7_8_event_attrs_group,
757 .ad799x_set_scan_mode = ad7997_8_set_scan_mode,
758 },
759};
760
761static int __devinit ad799x_probe(struct i2c_client *client,
762 const struct i2c_device_id *id)
763{
764 int ret, regdone = 0;
765 struct ad799x_platform_data *pdata = client->dev.platform_data;
766 struct ad799x_state *st = kzalloc(sizeof(*st), GFP_KERNEL);
767 if (st == NULL) {
768 ret = -ENOMEM;
769 goto error_ret;
770 }
771
772 /* this is only used for device removal purposes */
773 i2c_set_clientdata(client, st);
774
775 atomic_set(&st->protect_ring, 0);
776 st->id = id->driver_data;
777 st->chip_info = &ad799x_chip_info_tbl[st->id];
778 st->config = st->chip_info->default_config;
779
780 /* TODO: Add pdata options for filtering and bit delay */
781
782 if (pdata)
783 st->int_vref_mv = pdata->vref_mv;
784 else
785 st->int_vref_mv = st->chip_info->int_vref_mv;
786
787 st->reg = regulator_get(&client->dev, "vcc");
788 if (!IS_ERR(st->reg)) {
789 ret = regulator_enable(st->reg);
790 if (ret)
791 goto error_put_reg;
792 }
793 st->client = client;
794
795 st->indio_dev = iio_allocate_device();
796 if (st->indio_dev == NULL) {
797 ret = -ENOMEM;
798 goto error_disable_reg;
799 }
800
801 /* Estabilish that the iio_dev is a child of the i2c device */
802 st->indio_dev->dev.parent = &client->dev;
803 st->indio_dev->attrs = st->chip_info->dev_attrs;
804 st->indio_dev->event_attrs = st->chip_info->event_attrs;
805
806 st->indio_dev->dev_data = (void *)(st);
807 st->indio_dev->driver_module = THIS_MODULE;
808 st->indio_dev->modes = INDIO_DIRECT_MODE;
809 st->indio_dev->num_interrupt_lines = 1;
810
811 ret = ad799x_set_scan_mode(st, 0);
812 if (ret)
813 goto error_free_device;
814
815 ret = ad799x_register_ring_funcs_and_init(st->indio_dev);
816 if (ret)
817 goto error_free_device;
818
819 ret = iio_device_register(st->indio_dev);
820 if (ret)
821 goto error_cleanup_ring;
822 regdone = 1;
823
824 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
825 if (ret)
826 goto error_cleanup_ring;
827
828 if (client->irq > 0 && st->chip_info->monitor_mode) {
829 INIT_WORK(&st->work_thresh, ad799x_interrupt_bh);
830
831 ret = iio_register_interrupt_line(client->irq,
832 st->indio_dev,
833 0,
834 IRQF_TRIGGER_FALLING,
835 client->name);
836 if (ret)
837 goto error_cleanup_ring;
838
839 /*
840 * The event handler list element refer to iio_event_ad799x.
841 * All event attributes bind to the same event handler.
842 * So, only register event handler once.
843 */
844 iio_add_event_to_list(&iio_event_ad799x,
845 &st->indio_dev->interrupts[0]->ev_list);
846 }
847
848 return 0;
849error_cleanup_ring:
850 ad799x_ring_cleanup(st->indio_dev);
851error_free_device:
852 if (!regdone)
853 iio_free_device(st->indio_dev);
854 else
855 iio_device_unregister(st->indio_dev);
856error_disable_reg:
857 if (!IS_ERR(st->reg))
858 regulator_disable(st->reg);
859error_put_reg:
860 if (!IS_ERR(st->reg))
861 regulator_put(st->reg);
862 kfree(st);
863error_ret:
864 return ret;
865}
866
867static __devexit int ad799x_remove(struct i2c_client *client)
868{
869 struct ad799x_state *st = i2c_get_clientdata(client);
870 struct iio_dev *indio_dev = st->indio_dev;
871
872 if (client->irq > 0 && st->chip_info->monitor_mode)
873 iio_unregister_interrupt_line(indio_dev, 0);
874
875 iio_ring_buffer_unregister(indio_dev->ring);
876 ad799x_ring_cleanup(indio_dev);
877 iio_device_unregister(indio_dev);
878 if (!IS_ERR(st->reg)) {
879 regulator_disable(st->reg);
880 regulator_put(st->reg);
881 }
882 kfree(st);
883
884 return 0;
885}
886
887static const struct i2c_device_id ad799x_id[] = {
888 { "ad7991", ad7991 },
889 { "ad7995", ad7995 },
890 { "ad7999", ad7999 },
891 { "ad7992", ad7992 },
892 { "ad7993", ad7993 },
893 { "ad7994", ad7994 },
894 { "ad7997", ad7997 },
895 { "ad7998", ad7998 },
896 {}
897};
898
899MODULE_DEVICE_TABLE(i2c, ad799x_id);
900
901static struct i2c_driver ad799x_driver = {
902 .driver = {
903 .name = "ad799x",
904 },
905 .probe = ad799x_probe,
906 .remove = __devexit_p(ad799x_remove),
907 .id_table = ad799x_id,
908};
909
910static __init int ad799x_init(void)
911{
912 return i2c_add_driver(&ad799x_driver);
913}
914
915static __exit void ad799x_exit(void)
916{
917 i2c_del_driver(&ad799x_driver);
918}
919
920MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
921MODULE_DESCRIPTION("Analog Devices AD799x ADC");
922MODULE_LICENSE("GPL v2");
923MODULE_ALIAS("i2c:ad799x");
924
925module_init(ad799x_init);
926module_exit(ad799x_exit);
This page took 0.059168 seconds and 5 git commands to generate.