blob: 64ac31666739ab7e8a61e8d3813199d4c6155009 [file] [log] [blame]
Michael Hennerich985dbe72010-10-01 16:41:51 +02001/*
2 * iio/adc/ad799x.c
Michael Hennerichd22fd9c2011-05-18 14:41:52 +01003 * Copyright (C) 2010-1011 Michael Hennerich, Analog Devices Inc.
Michael Hennerich985dbe72010-10-01 16:41:51 +02004 *
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>
Michael Hennerich985dbe72010-10-01 16:41:51 +020026#include <linux/device.h>
27#include <linux/kernel.h>
28#include <linux/sysfs.h>
Michael Hennerich985dbe72010-10-01 16:41:51 +020029#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>
Paul Gortmaker99c97852011-07-03 15:49:50 -040034#include <linux/module.h>
Michael Hennerich985dbe72010-10-01 16:41:51 +020035
36#include "../iio.h"
37#include "../sysfs.h"
Jonathan Cameronaf5046a2011-10-26 17:41:32 +010038#include "../events.h"
39#include "../buffer.h"
Jonathan Cameroncdf38702011-08-12 17:08:43 +010040
Michael Hennerich985dbe72010-10-01 16:41:51 +020041#include "ad799x.h"
42
43/*
44 * ad799x register access by I2C
45 */
46static 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
62static 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
Michael Hennerichaecac192010-10-06 10:25:49 +020067 ret = i2c_smbus_read_byte_data(client, reg);
Michael Hennerich985dbe72010-10-01 16:41:51 +020068 if (ret < 0) {
69 dev_err(&client->dev, "I2C read error\n");
70 return ret;
71 }
72
Michael Hennerichaecac192010-10-06 10:25:49 +020073 *data = (u8)ret;
Michael Hennerich985dbe72010-10-01 16:41:51 +020074
75 return 0;
76}
77
78static 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
90static 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
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100102int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
Michael Hennerich985dbe72010-10-01 16:41:51 +0200103{
104 return ad799x_i2c_write16(st, AD7998_CONF_REG,
105 st->config | (mask << AD799X_CHANNEL_SHIFT));
106}
107
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100108static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
Michael Hennerich985dbe72010-10-01 16:41:51 +0200109{
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100110 u16 rxbuf;
111 u8 cmd;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200112 int ret;
113
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100114 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;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200131 }
132
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100133 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
134 if (ret < 0)
135 return ret;
136
137 return rxbuf;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200138}
139
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100140static int ad799x_read_raw(struct iio_dev *indio_dev,
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100141 struct iio_chan_spec const *chan,
142 int *val,
143 int *val2,
144 long m)
Michael Hennerich985dbe72010-10-01 16:41:51 +0200145{
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100146 int ret;
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100147 struct ad799x_state *st = iio_priv(indio_dev);
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100148 unsigned int scale_uv;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200149
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100150 switch (m) {
151 case 0:
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100152 mutex_lock(&indio_dev->mlock);
153 if (iio_buffer_enabled(indio_dev))
154 ret = ad799x_single_channel_from_ring(indio_dev,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100155 chan->scan_index);
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100156 else
Jonathan Cameron58dffae2011-09-30 10:05:37 +0100157 ret = ad799x_scan_direct(st, chan->scan_index);
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100158 mutex_unlock(&indio_dev->mlock);
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100159
Michael Hennerichaecac192010-10-06 10:25:49 +0200160 if (ret < 0)
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100161 return ret;
Jonathan Cameron5357ba32011-09-30 10:05:32 +0100162 *val = (ret >> chan->scan_type.shift) &
163 RES_MASK(chan->scan_type.realbits);
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100164 return IIO_VAL_INT;
165 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
Jonathan Cameron5357ba32011-09-30 10:05:32 +0100166 scale_uv = (st->int_vref_mv * 1000) >> chan->scan_type.realbits;
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100167 *val = scale_uv / 1000;
168 *val2 = (scale_uv % 1000) * 1000;
169 return IIO_VAL_INT_PLUS_MICRO;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200170 }
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100171 return -EINVAL;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200172}
Jonathan Cameron24cba402011-09-30 10:05:33 +0100173static const unsigned int ad7998_frequencies[] = {
174 [AD7998_CYC_DIS] = 0,
175 [AD7998_CYC_TCONF_32] = 15625,
176 [AD7998_CYC_TCONF_64] = 7812,
177 [AD7998_CYC_TCONF_128] = 3906,
178 [AD7998_CYC_TCONF_512] = 976,
179 [AD7998_CYC_TCONF_1024] = 488,
180 [AD7998_CYC_TCONF_2048] = 244,
181};
Michael Hennerich985dbe72010-10-01 16:41:51 +0200182static ssize_t ad799x_read_frequency(struct device *dev,
183 struct device_attribute *attr,
184 char *buf)
185{
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100186 struct iio_dev *indio_dev = dev_get_drvdata(dev);
187 struct ad799x_state *st = iio_priv(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200188
Jonathan Cameron24cba402011-09-30 10:05:33 +0100189 int ret;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200190 u8 val;
191 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
192 if (ret)
193 return ret;
194
195 val &= AD7998_CYC_MASK;
196
Jonathan Cameron24cba402011-09-30 10:05:33 +0100197 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200198}
199
200static ssize_t ad799x_write_frequency(struct device *dev,
201 struct device_attribute *attr,
202 const char *buf,
203 size_t len)
204{
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100205 struct iio_dev *indio_dev = dev_get_drvdata(dev);
206 struct ad799x_state *st = iio_priv(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200207
208 long val;
Jonathan Cameron24cba402011-09-30 10:05:33 +0100209 int ret, i;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200210 u8 t;
211
212 ret = strict_strtol(buf, 10, &val);
213 if (ret)
214 return ret;
215
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100216 mutex_lock(&indio_dev->mlock);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200217 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
218 if (ret)
219 goto error_ret_mutex;
220 /* Wipe the bits clean */
221 t &= ~AD7998_CYC_MASK;
222
Jonathan Cameron24cba402011-09-30 10:05:33 +0100223 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
224 if (val == ad7998_frequencies[i])
225 break;
226 if (i == ARRAY_SIZE(ad7998_frequencies)) {
Michael Hennerich985dbe72010-10-01 16:41:51 +0200227 ret = -EINVAL;
228 goto error_ret_mutex;
229 }
Jonathan Cameron24cba402011-09-30 10:05:33 +0100230 t |= i;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200231 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
232
233error_ret_mutex:
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100234 mutex_unlock(&indio_dev->mlock);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200235
236 return ret ? ret : len;
237}
238
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100239static int ad799x_read_event_config(struct iio_dev *indio_dev,
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100240 u64 event_code)
241{
242 return 1;
243}
244
245static const u8 ad799x_threshold_addresses[][2] = {
246 { AD7998_DATALOW_CH1_REG, AD7998_DATAHIGH_CH1_REG },
247 { AD7998_DATALOW_CH2_REG, AD7998_DATAHIGH_CH2_REG },
248 { AD7998_DATALOW_CH3_REG, AD7998_DATAHIGH_CH3_REG },
249 { AD7998_DATALOW_CH4_REG, AD7998_DATAHIGH_CH4_REG },
250};
251
252static int ad799x_write_event_value(struct iio_dev *indio_dev,
253 u64 event_code,
254 int val)
255{
256 int ret;
257 struct ad799x_state *st = iio_priv(indio_dev);
258 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
259 IIO_EV_DIR_FALLING);
260 int number = IIO_EVENT_CODE_EXTRACT_NUM(event_code);
261
262 mutex_lock(&indio_dev->mlock);
263 ret = ad799x_i2c_write16(st,
264 ad799x_threshold_addresses[number][direction],
265 val);
266 mutex_unlock(&indio_dev->mlock);
267
268 return ret;
269}
270
271static int ad799x_read_event_value(struct iio_dev *indio_dev,
272 u64 event_code,
273 int *val)
274{
275 int ret;
276 struct ad799x_state *st = iio_priv(indio_dev);
277 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
278 IIO_EV_DIR_FALLING);
279 int number = IIO_EVENT_CODE_EXTRACT_NUM(event_code);
280 u16 valin;
281
282 mutex_lock(&indio_dev->mlock);
283 ret = ad799x_i2c_read16(st,
284 ad799x_threshold_addresses[number][direction],
285 &valin);
286 mutex_unlock(&indio_dev->mlock);
287 if (ret < 0)
288 return ret;
289 *val = valin;
290
291 return 0;
292}
293
Michael Hennerich985dbe72010-10-01 16:41:51 +0200294static ssize_t ad799x_read_channel_config(struct device *dev,
295 struct device_attribute *attr,
296 char *buf)
297{
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100298 struct iio_dev *indio_dev = dev_get_drvdata(dev);
299 struct ad799x_state *st = iio_priv(indio_dev);
Jonathan Cameron72148f62011-05-18 14:41:15 +0100300 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200301
302 int ret;
303 u16 val;
Jonathan Cameron72148f62011-05-18 14:41:15 +0100304 ret = ad799x_i2c_read16(st, this_attr->address, &val);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200305 if (ret)
306 return ret;
307
308 return sprintf(buf, "%d\n", val);
309}
310
311static ssize_t ad799x_write_channel_config(struct device *dev,
312 struct device_attribute *attr,
313 const char *buf,
314 size_t len)
315{
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100316 struct iio_dev *indio_dev = dev_get_drvdata(dev);
317 struct ad799x_state *st = iio_priv(indio_dev);
Jonathan Cameron72148f62011-05-18 14:41:15 +0100318 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200319
320 long val;
321 int ret;
322
323 ret = strict_strtol(buf, 10, &val);
324 if (ret)
325 return ret;
326
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100327 mutex_lock(&indio_dev->mlock);
Jonathan Cameron72148f62011-05-18 14:41:15 +0100328 ret = ad799x_i2c_write16(st, this_attr->address, val);
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100329 mutex_unlock(&indio_dev->mlock);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200330
331 return ret ? ret : len;
332}
333
Jonathan Cameron72148f62011-05-18 14:41:15 +0100334static irqreturn_t ad799x_event_handler(int irq, void *private)
Michael Hennerich985dbe72010-10-01 16:41:51 +0200335{
Jonathan Cameron72148f62011-05-18 14:41:15 +0100336 struct iio_dev *indio_dev = private;
Jonathan Camerond8aea292011-06-27 13:07:20 +0100337 struct ad799x_state *st = iio_priv(private);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200338 u8 status;
Jonathan Cameron72148f62011-05-18 14:41:15 +0100339 int i, ret;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200340
Jonathan Cameron72148f62011-05-18 14:41:15 +0100341 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
342 if (ret)
343 return ret;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200344
345 if (!status)
Jonathan Cameron72148f62011-05-18 14:41:15 +0100346 return -EIO;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200347
348 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
349
350 for (i = 0; i < 8; i++) {
351 if (status & (1 << i))
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100352 iio_push_event(indio_dev,
Jonathan Cameron72148f62011-05-18 14:41:15 +0100353 i & 0x1 ?
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100354 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
Jonathan Cameroncdf38702011-08-12 17:08:43 +0100355 (i >> 1),
356 IIO_EV_TYPE_THRESH,
357 IIO_EV_DIR_RISING) :
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100358 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
Jonathan Cameroncdf38702011-08-12 17:08:43 +0100359 (i >> 1),
360 IIO_EV_TYPE_THRESH,
361 IIO_EV_DIR_FALLING),
Jonathan Cameron72148f62011-05-18 14:41:15 +0100362 iio_get_time_ns());
Michael Hennerich985dbe72010-10-01 16:41:51 +0200363 }
364
Jonathan Cameron72148f62011-05-18 14:41:15 +0100365 return IRQ_HANDLED;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200366}
367
Jonathan Cameron322c9562011-09-14 13:01:23 +0100368static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
Jonathan Cameron72148f62011-05-18 14:41:15 +0100369 S_IRUGO | S_IWUSR,
370 ad799x_read_channel_config,
371 ad799x_write_channel_config,
372 AD7998_HYST_CH1_REG);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200373
Jonathan Cameron322c9562011-09-14 13:01:23 +0100374static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
Jonathan Cameron72148f62011-05-18 14:41:15 +0100375 S_IRUGO | S_IWUSR,
376 ad799x_read_channel_config,
377 ad799x_write_channel_config,
378 AD7998_HYST_CH2_REG);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200379
Jonathan Cameron322c9562011-09-14 13:01:23 +0100380static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
Jonathan Cameron72148f62011-05-18 14:41:15 +0100381 S_IRUGO | S_IWUSR,
382 ad799x_read_channel_config,
383 ad799x_write_channel_config,
384 AD7998_HYST_CH3_REG);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200385
Jonathan Cameron322c9562011-09-14 13:01:23 +0100386static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
Jonathan Cameron72148f62011-05-18 14:41:15 +0100387 S_IRUGO | S_IWUSR,
388 ad799x_read_channel_config,
389 ad799x_write_channel_config,
390 AD7998_HYST_CH4_REG);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200391
392static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
393 ad799x_read_frequency,
394 ad799x_write_frequency);
395static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
396
397static struct attribute *ad7993_4_7_8_event_attributes[] = {
Jonathan Cameron322c9562011-09-14 13:01:23 +0100398 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
Jonathan Cameron322c9562011-09-14 13:01:23 +0100399 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
Jonathan Cameron322c9562011-09-14 13:01:23 +0100400 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
Jonathan Cameron322c9562011-09-14 13:01:23 +0100401 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200402 &iio_dev_attr_sampling_frequency.dev_attr.attr,
403 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
404 NULL,
405};
406
407static struct attribute_group ad7993_4_7_8_event_attrs_group = {
408 .attrs = ad7993_4_7_8_event_attributes,
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100409 .name = "events",
Michael Hennerich985dbe72010-10-01 16:41:51 +0200410};
411
412static struct attribute *ad7992_event_attributes[] = {
Jonathan Cameron322c9562011-09-14 13:01:23 +0100413 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
Jonathan Cameron322c9562011-09-14 13:01:23 +0100414 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200415 &iio_dev_attr_sampling_frequency.dev_attr.attr,
416 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
417 NULL,
418};
419
420static struct attribute_group ad7992_event_attrs_group = {
421 .attrs = ad7992_event_attributes,
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100422 .name = "events",
Michael Hennerich985dbe72010-10-01 16:41:51 +0200423};
424
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100425static const struct iio_info ad7991_info = {
426 .read_raw = &ad799x_read_raw,
427 .driver_module = THIS_MODULE,
428};
429
430static const struct iio_info ad7992_info = {
431 .read_raw = &ad799x_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100432 .event_attrs = &ad7992_event_attrs_group,
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100433 .read_event_config = &ad799x_read_event_config,
434 .read_event_value = &ad799x_read_event_value,
435 .write_event_value = &ad799x_write_event_value,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100436 .driver_module = THIS_MODULE,
437};
438
439static const struct iio_info ad7993_4_7_8_info = {
440 .read_raw = &ad799x_read_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100441 .event_attrs = &ad7993_4_7_8_event_attrs_group,
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100442 .read_event_config = &ad799x_read_event_config,
443 .read_event_value = &ad799x_read_event_value,
444 .write_event_value = &ad799x_write_event_value,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100445 .driver_module = THIS_MODULE,
446};
447
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100448#define AD799X_EV_MASK (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
449 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
450
Michael Hennerich985dbe72010-10-01 16:41:51 +0200451static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
452 [ad7991] = {
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100453 .channel = {
454 [0] = {
455 .type = IIO_VOLTAGE,
456 .indexed = 1,
457 .channel = 0,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100458 .scan_index = 0,
459 .scan_type = IIO_ST('u', 12, 16, 0),
460 },
461 [1] = {
462 .type = IIO_VOLTAGE,
463 .indexed = 1,
464 .channel = 1,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100465 .scan_index = 1,
466 .scan_type = IIO_ST('u', 12, 16, 0),
467 },
468 [2] = {
469 .type = IIO_VOLTAGE,
470 .indexed = 1,
471 .channel = 2,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100472 .scan_index = 2,
473 .scan_type = IIO_ST('u', 12, 16, 0),
474 },
475 [3] = {
476 .type = IIO_VOLTAGE,
477 .indexed = 1,
478 .channel = 3,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100479 .scan_index = 3,
480 .scan_type = IIO_ST('u', 12, 16, 0),
481 },
482 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
483 },
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100484 .num_channels = 5,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200485 .int_vref_mv = 4096,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100486 .info = &ad7991_info,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200487 },
488 [ad7995] = {
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100489 .channel = {
490 [0] = {
491 .type = IIO_VOLTAGE,
492 .indexed = 1,
493 .channel = 0,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100494 .scan_index = 0,
495 .scan_type = IIO_ST('u', 10, 16, 2),
496 },
497 [1] = {
498 .type = IIO_VOLTAGE,
499 .indexed = 1,
500 .channel = 1,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100501 .scan_index = 1,
502 .scan_type = IIO_ST('u', 10, 16, 2),
503 },
504 [2] = {
505 .type = IIO_VOLTAGE,
506 .indexed = 1,
507 .channel = 2,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100508 .scan_index = 2,
509 .scan_type = IIO_ST('u', 10, 16, 2),
510 },
511 [3] = {
512 .type = IIO_VOLTAGE,
513 .indexed = 1,
514 .channel = 3,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100515 .scan_index = 3,
516 .scan_type = IIO_ST('u', 10, 16, 2),
517 },
518 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
519 },
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100520 .num_channels = 5,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200521 .int_vref_mv = 1024,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100522 .info = &ad7991_info,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200523 },
524 [ad7999] = {
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100525 .channel = {
526 [0] = {
527 .type = IIO_VOLTAGE,
528 .indexed = 1,
529 .channel = 0,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100530 .scan_index = 0,
531 .scan_type = IIO_ST('u', 8, 16, 4),
532 },
533 [1] = {
534 .type = IIO_VOLTAGE,
535 .indexed = 1,
536 .channel = 1,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100537 .scan_index = 1,
538 .scan_type = IIO_ST('u', 8, 16, 4),
539 },
540 [2] = {
541 .type = IIO_VOLTAGE,
542 .indexed = 1,
543 .channel = 2,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100544 .scan_index = 2,
545 .scan_type = IIO_ST('u', 8, 16, 4),
546 },
547 [3] = {
548 .type = IIO_VOLTAGE,
549 .indexed = 1,
550 .channel = 3,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100551 .scan_index = 3,
552 .scan_type = IIO_ST('u', 8, 16, 4),
553 },
554 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
555 },
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100556 .num_channels = 5,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200557 .int_vref_mv = 1024,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100558 .info = &ad7991_info,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200559 },
560 [ad7992] = {
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100561 .channel = {
562 [0] = {
563 .type = IIO_VOLTAGE,
564 .indexed = 1,
565 .channel = 0,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100566 .scan_index = 0,
567 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100568 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100569 },
570 [1] = {
571 .type = IIO_VOLTAGE,
572 .indexed = 1,
573 .channel = 1,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100574 .scan_index = 1,
575 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100576 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100577 },
578 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
579 },
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100580 .num_channels = 3,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200581 .int_vref_mv = 4096,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200582 .default_config = AD7998_ALERT_EN,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100583 .info = &ad7992_info,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200584 },
585 [ad7993] = {
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100586 .channel = {
587 [0] = {
588 .type = IIO_VOLTAGE,
589 .indexed = 1,
590 .channel = 0,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100591 .scan_index = 0,
592 .scan_type = IIO_ST('u', 10, 16, 2),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100593 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100594 },
595 [1] = {
596 .type = IIO_VOLTAGE,
597 .indexed = 1,
598 .channel = 1,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100599 .scan_index = 1,
600 .scan_type = IIO_ST('u', 10, 16, 2),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100601 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100602 },
603 [2] = {
604 .type = IIO_VOLTAGE,
605 .indexed = 1,
606 .channel = 2,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100607 .scan_index = 2,
608 .scan_type = IIO_ST('u', 10, 16, 2),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100609 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100610 },
611 [3] = {
612 .type = IIO_VOLTAGE,
613 .indexed = 1,
614 .channel = 3,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100615 .scan_index = 3,
616 .scan_type = IIO_ST('u', 10, 16, 2),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100617 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100618 },
619 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
620 },
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100621 .num_channels = 5,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200622 .int_vref_mv = 1024,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200623 .default_config = AD7998_ALERT_EN,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100624 .info = &ad7993_4_7_8_info,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200625 },
626 [ad7994] = {
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100627 .channel = {
628 [0] = {
629 .type = IIO_VOLTAGE,
630 .indexed = 1,
631 .channel = 0,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100632 .scan_index = 0,
633 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100634 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100635 },
636 [1] = {
637 .type = IIO_VOLTAGE,
638 .indexed = 1,
639 .channel = 1,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100640 .scan_index = 1,
641 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100642 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100643 },
644 [2] = {
645 .type = IIO_VOLTAGE,
646 .indexed = 1,
647 .channel = 2,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100648 .scan_index = 2,
649 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100650 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100651 },
652 [3] = {
653 .type = IIO_VOLTAGE,
654 .indexed = 1,
655 .channel = 3,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100656 .scan_index = 3,
657 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100658 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100659 },
660 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
661 },
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100662 .num_channels = 5,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200663 .int_vref_mv = 4096,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200664 .default_config = AD7998_ALERT_EN,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100665 .info = &ad7993_4_7_8_info,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200666 },
667 [ad7997] = {
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100668 .channel = {
669 [0] = {
670 .type = IIO_VOLTAGE,
671 .indexed = 1,
672 .channel = 0,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100673 .scan_index = 0,
674 .scan_type = IIO_ST('u', 10, 16, 2),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100675 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100676 },
677 [1] = {
678 .type = IIO_VOLTAGE,
679 .indexed = 1,
680 .channel = 1,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100681 .scan_index = 1,
682 .scan_type = IIO_ST('u', 10, 16, 2),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100683 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100684 },
685 [2] = {
686 .type = IIO_VOLTAGE,
687 .indexed = 1,
688 .channel = 2,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100689 .scan_index = 2,
690 .scan_type = IIO_ST('u', 10, 16, 2),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100691 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100692 },
693 [3] = {
694 .type = IIO_VOLTAGE,
695 .indexed = 1,
696 .channel = 3,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100697 .scan_index = 3,
698 .scan_type = IIO_ST('u', 10, 16, 2),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100699 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100700 },
701 [4] = {
702 .type = IIO_VOLTAGE,
703 .indexed = 1,
704 .channel = 4,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100705 .scan_index = 4,
706 .scan_type = IIO_ST('u', 10, 16, 2),
707 },
708 [5] = {
709 .type = IIO_VOLTAGE,
710 .indexed = 1,
711 .channel = 5,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100712 .scan_index = 5,
713 .scan_type = IIO_ST('u', 10, 16, 2),
714 },
715 [6] = {
716 .type = IIO_VOLTAGE,
717 .indexed = 1,
718 .channel = 6,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100719 .scan_index = 6,
720 .scan_type = IIO_ST('u', 10, 16, 2),
721 },
722 [7] = {
723 .type = IIO_VOLTAGE,
724 .indexed = 1,
725 .channel = 7,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100726 .scan_index = 7,
727 .scan_type = IIO_ST('u', 10, 16, 2),
728 },
729 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
730 },
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100731 .num_channels = 9,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200732 .int_vref_mv = 1024,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200733 .default_config = AD7998_ALERT_EN,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100734 .info = &ad7993_4_7_8_info,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200735 },
736 [ad7998] = {
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100737 .channel = {
738 [0] = {
739 .type = IIO_VOLTAGE,
740 .indexed = 1,
741 .channel = 0,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100742 .scan_index = 0,
743 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100744 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100745 },
746 [1] = {
747 .type = IIO_VOLTAGE,
748 .indexed = 1,
749 .channel = 1,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100750 .scan_index = 1,
751 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100752 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100753 },
754 [2] = {
755 .type = IIO_VOLTAGE,
756 .indexed = 1,
757 .channel = 2,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100758 .scan_index = 2,
759 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100760 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100761 },
762 [3] = {
763 .type = IIO_VOLTAGE,
764 .indexed = 1,
765 .channel = 3,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100766 .scan_index = 3,
767 .scan_type = IIO_ST('u', 12, 16, 0),
Jonathan Cameron231c5c32011-09-30 10:05:35 +0100768 .event_mask = AD799X_EV_MASK,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100769 },
770 [4] = {
771 .type = IIO_VOLTAGE,
772 .indexed = 1,
773 .channel = 4,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100774 .scan_index = 4,
775 .scan_type = IIO_ST('u', 12, 16, 0),
776 },
777 [5] = {
778 .type = IIO_VOLTAGE,
779 .indexed = 1,
780 .channel = 5,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100781 .scan_index = 5,
782 .scan_type = IIO_ST('u', 12, 16, 0),
783 },
784 [6] = {
785 .type = IIO_VOLTAGE,
786 .indexed = 1,
787 .channel = 6,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100788 .scan_index = 6,
789 .scan_type = IIO_ST('u', 12, 16, 0),
790 },
791 [7] = {
792 .type = IIO_VOLTAGE,
793 .indexed = 1,
794 .channel = 7,
Jonathan Cameron7c626f52011-09-30 10:05:31 +0100795 .scan_index = 7,
796 .scan_type = IIO_ST('u', 12, 16, 0),
797 },
798 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
799 },
Michael Hennerichd22fd9c2011-05-18 14:41:52 +0100800 .num_channels = 9,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200801 .int_vref_mv = 4096,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200802 .default_config = AD7998_ALERT_EN,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100803 .info = &ad7993_4_7_8_info,
Michael Hennerich985dbe72010-10-01 16:41:51 +0200804 },
805};
806
807static int __devinit ad799x_probe(struct i2c_client *client,
808 const struct i2c_device_id *id)
809{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100810 int ret;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200811 struct ad799x_platform_data *pdata = client->dev.platform_data;
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100812 struct ad799x_state *st;
813 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
Michael Hennerich985dbe72010-10-01 16:41:51 +0200814
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100815 if (indio_dev == NULL)
816 return -ENOMEM;
817
818 st = iio_priv(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200819 /* this is only used for device removal purposes */
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100820 i2c_set_clientdata(client, indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200821
Michael Hennerich985dbe72010-10-01 16:41:51 +0200822 st->id = id->driver_data;
823 st->chip_info = &ad799x_chip_info_tbl[st->id];
824 st->config = st->chip_info->default_config;
825
826 /* TODO: Add pdata options for filtering and bit delay */
827
828 if (pdata)
829 st->int_vref_mv = pdata->vref_mv;
830 else
831 st->int_vref_mv = st->chip_info->int_vref_mv;
832
833 st->reg = regulator_get(&client->dev, "vcc");
834 if (!IS_ERR(st->reg)) {
835 ret = regulator_enable(st->reg);
836 if (ret)
837 goto error_put_reg;
838 }
839 st->client = client;
840
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100841 indio_dev->dev.parent = &client->dev;
842 indio_dev->name = id->name;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100843 indio_dev->info = st->chip_info->info;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100844
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100845 indio_dev->modes = INDIO_DIRECT_MODE;
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100846 indio_dev->channels = st->chip_info->channel;
847 indio_dev->num_channels = st->chip_info->num_channels;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200848
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100849 ret = ad799x_register_ring_funcs_and_init(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200850 if (ret)
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100851 goto error_disable_reg;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200852
Jonathan Cameron14555b12011-09-21 11:15:57 +0100853 ret = iio_buffer_register(indio_dev,
854 indio_dev->channels,
855 indio_dev->num_channels);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200856 if (ret)
857 goto error_cleanup_ring;
858
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100859 if (client->irq > 0) {
Jonathan Cameron72148f62011-05-18 14:41:15 +0100860 ret = request_threaded_irq(client->irq,
861 NULL,
862 ad799x_event_handler,
863 IRQF_TRIGGER_FALLING |
864 IRQF_ONESHOT,
865 client->name,
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100866 indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200867 if (ret)
868 goto error_cleanup_ring;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200869 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100870 ret = iio_device_register(indio_dev);
871 if (ret)
872 goto error_free_irq;
Michael Hennerich985dbe72010-10-01 16:41:51 +0200873
874 return 0;
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100875
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100876error_free_irq:
877 free_irq(client->irq, indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200878error_cleanup_ring:
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100879 ad799x_ring_cleanup(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200880error_disable_reg:
881 if (!IS_ERR(st->reg))
882 regulator_disable(st->reg);
883error_put_reg:
884 if (!IS_ERR(st->reg))
885 regulator_put(st->reg);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100886 iio_free_device(indio_dev);
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100887
Michael Hennerich985dbe72010-10-01 16:41:51 +0200888 return ret;
889}
890
891static __devexit int ad799x_remove(struct i2c_client *client)
892{
Michael Hennerich1bf7ac72011-05-18 14:41:53 +0100893 struct iio_dev *indio_dev = i2c_get_clientdata(client);
894 struct ad799x_state *st = iio_priv(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200895
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100896 iio_device_unregister(indio_dev);
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100897 if (client->irq > 0)
Jonathan Cameron72148f62011-05-18 14:41:15 +0100898 free_irq(client->irq, indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200899
Jonathan Cameron14555b12011-09-21 11:15:57 +0100900 iio_buffer_unregister(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200901 ad799x_ring_cleanup(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200902 if (!IS_ERR(st->reg)) {
903 regulator_disable(st->reg);
904 regulator_put(st->reg);
905 }
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100906 iio_free_device(indio_dev);
Michael Hennerich985dbe72010-10-01 16:41:51 +0200907
908 return 0;
909}
910
911static 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
923MODULE_DEVICE_TABLE(i2c, ad799x_id);
924
925static 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
934static __init int ad799x_init(void)
935{
936 return i2c_add_driver(&ad799x_driver);
937}
938
939static __exit void ad799x_exit(void)
940{
941 i2c_del_driver(&ad799x_driver);
942}
943
944MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
945MODULE_DESCRIPTION("Analog Devices AD799x ADC");
946MODULE_LICENSE("GPL v2");
947MODULE_ALIAS("i2c:ad799x");
948
949module_init(ad799x_init);
950module_exit(ad799x_exit);