3 * Copyright (C) 2010-1011 Michael Hennerich, Analog Devices Inc.
5 * based on iio/adc/max1363
6 * Copyright (C) 2008-2010 Jonathan Cameron
8 * based on linux/drivers/i2c/chips/max123x
9 * Copyright (C) 2002-2004 Stefan Eletzhofer
11 * based on linux/drivers/acron/char/pcf8583.c
12 * Copyright (C) 2000 Russell King
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.
20 * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997,
21 * ad7998 and similar chips.
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>
38 #include "../buffer_generic.h"
43 * ad799x register access by I2C
45 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
47 struct i2c_client *client = st->client;
50 ret = i2c_smbus_read_word_data(client, reg);
52 dev_err(&client->dev, "I2C read error\n");
56 *data = swab16((u16)ret);
61 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
63 struct i2c_client *client = st->client;
66 ret = i2c_smbus_read_byte_data(client, reg);
68 dev_err(&client->dev, "I2C read error\n");
77 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
79 struct i2c_client *client = st->client;
82 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
84 dev_err(&client->dev, "I2C write error\n");
89 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
91 struct i2c_client *client = st->client;
94 ret = i2c_smbus_write_byte_data(client, reg, data);
96 dev_err(&client->dev, "I2C write error\n");
101 int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
103 return ad799x_i2c_write16(st, AD7998_CONF_REG,
104 st->config | (mask << AD799X_CHANNEL_SHIFT));
107 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
117 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
122 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
126 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
132 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
139 static int ad799x_read_raw(struct iio_dev *indio_dev,
140 struct iio_chan_spec const *chan,
146 struct ad799x_state *st = iio_priv(indio_dev);
147 unsigned int scale_uv;
151 mutex_lock(&indio_dev->mlock);
152 if (iio_buffer_enabled(indio_dev))
153 ret = ad799x_single_channel_from_ring(indio_dev,
156 ret = ad799x_scan_direct(st, chan->scan_index);
157 mutex_unlock(&indio_dev->mlock);
161 *val = (ret >> chan->scan_type.shift) &
162 RES_MASK(chan->scan_type.realbits);
164 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
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;
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,
181 static ssize_t ad799x_read_frequency(struct device *dev,
182 struct device_attribute *attr,
185 struct iio_dev *indio_dev = dev_get_drvdata(dev);
186 struct ad799x_state *st = iio_priv(indio_dev);
190 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
194 val &= AD7998_CYC_MASK;
196 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
199 static ssize_t ad799x_write_frequency(struct device *dev,
200 struct device_attribute *attr,
204 struct iio_dev *indio_dev = dev_get_drvdata(dev);
205 struct ad799x_state *st = iio_priv(indio_dev);
211 ret = strict_strtol(buf, 10, &val);
215 mutex_lock(&indio_dev->mlock);
216 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
218 goto error_ret_mutex;
219 /* Wipe the bits clean */
220 t &= ~AD7998_CYC_MASK;
222 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
223 if (val == ad7998_frequencies[i])
225 if (i == ARRAY_SIZE(ad7998_frequencies)) {
227 goto error_ret_mutex;
230 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
233 mutex_unlock(&indio_dev->mlock);
235 return ret ? ret : len;
238 static int ad799x_read_event_config(struct iio_dev *indio_dev,
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 },
251 static int ad799x_write_event_value(struct iio_dev *indio_dev,
256 struct ad799x_state *st = iio_priv(indio_dev);
257 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
259 int number = IIO_EVENT_CODE_EXTRACT_NUM(event_code);
261 mutex_lock(&indio_dev->mlock);
262 ret = ad799x_i2c_write16(st,
263 ad799x_threshold_addresses[number][direction],
265 mutex_unlock(&indio_dev->mlock);
270 static int ad799x_read_event_value(struct iio_dev *indio_dev,
275 struct ad799x_state *st = iio_priv(indio_dev);
276 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
278 int number = IIO_EVENT_CODE_EXTRACT_NUM(event_code);
281 mutex_lock(&indio_dev->mlock);
282 ret = ad799x_i2c_read16(st,
283 ad799x_threshold_addresses[number][direction],
285 mutex_unlock(&indio_dev->mlock);
293 static ssize_t ad799x_read_channel_config(struct device *dev,
294 struct device_attribute *attr,
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);
303 ret = ad799x_i2c_read16(st, this_attr->address, &val);
307 return sprintf(buf, "%d\n", val);
310 static ssize_t ad799x_write_channel_config(struct device *dev,
311 struct device_attribute *attr,
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);
322 ret = strict_strtol(buf, 10, &val);
326 mutex_lock(&indio_dev->mlock);
327 ret = ad799x_i2c_write16(st, this_attr->address, val);
328 mutex_unlock(&indio_dev->mlock);
330 return ret ? ret : len;
333 static irqreturn_t ad799x_event_handler(int irq, void *private)
335 struct iio_dev *indio_dev = private;
336 struct ad799x_state *st = iio_priv(private);
340 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
347 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
349 for (i = 0; i < 8; i++) {
350 if (status & (1 << i))
351 iio_push_event(indio_dev,
353 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
357 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
367 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
369 ad799x_read_channel_config,
370 ad799x_write_channel_config,
371 AD7998_HYST_CH1_REG);
373 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
375 ad799x_read_channel_config,
376 ad799x_write_channel_config,
377 AD7998_HYST_CH2_REG);
379 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
381 ad799x_read_channel_config,
382 ad799x_write_channel_config,
383 AD7998_HYST_CH3_REG);
385 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
387 ad799x_read_channel_config,
388 ad799x_write_channel_config,
389 AD7998_HYST_CH4_REG);
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");
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,
406 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
407 .attrs = ad7993_4_7_8_event_attributes,
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,
419 static struct attribute_group ad7992_event_attrs_group = {
420 .attrs = ad7992_event_attributes,
424 static const struct iio_info ad7991_info = {
425 .read_raw = &ad799x_read_raw,
426 .driver_module = THIS_MODULE,
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,
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,
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))
450 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
458 .scan_type = IIO_ST('u', 12, 16, 0),
465 .scan_type = IIO_ST('u', 12, 16, 0),
472 .scan_type = IIO_ST('u', 12, 16, 0),
479 .scan_type = IIO_ST('u', 12, 16, 0),
481 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
485 .info = &ad7991_info,
494 .scan_type = IIO_ST('u', 10, 16, 2),
501 .scan_type = IIO_ST('u', 10, 16, 2),
508 .scan_type = IIO_ST('u', 10, 16, 2),
515 .scan_type = IIO_ST('u', 10, 16, 2),
517 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
521 .info = &ad7991_info,
530 .scan_type = IIO_ST('u', 8, 16, 4),
537 .scan_type = IIO_ST('u', 8, 16, 4),
544 .scan_type = IIO_ST('u', 8, 16, 4),
551 .scan_type = IIO_ST('u', 8, 16, 4),
553 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
557 .info = &ad7991_info,
566 .scan_type = IIO_ST('u', 12, 16, 0),
567 .event_mask = AD799X_EV_MASK,
574 .scan_type = IIO_ST('u', 12, 16, 0),
575 .event_mask = AD799X_EV_MASK,
577 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
581 .default_config = AD7998_ALERT_EN,
582 .info = &ad7992_info,
591 .scan_type = IIO_ST('u', 10, 16, 2),
592 .event_mask = AD799X_EV_MASK,
599 .scan_type = IIO_ST('u', 10, 16, 2),
600 .event_mask = AD799X_EV_MASK,
607 .scan_type = IIO_ST('u', 10, 16, 2),
608 .event_mask = AD799X_EV_MASK,
615 .scan_type = IIO_ST('u', 10, 16, 2),
616 .event_mask = AD799X_EV_MASK,
618 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
622 .default_config = AD7998_ALERT_EN,
623 .info = &ad7993_4_7_8_info,
632 .scan_type = IIO_ST('u', 12, 16, 0),
633 .event_mask = AD799X_EV_MASK,
640 .scan_type = IIO_ST('u', 12, 16, 0),
641 .event_mask = AD799X_EV_MASK,
648 .scan_type = IIO_ST('u', 12, 16, 0),
649 .event_mask = AD799X_EV_MASK,
656 .scan_type = IIO_ST('u', 12, 16, 0),
657 .event_mask = AD799X_EV_MASK,
659 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
663 .default_config = AD7998_ALERT_EN,
664 .info = &ad7993_4_7_8_info,
673 .scan_type = IIO_ST('u', 10, 16, 2),
674 .event_mask = AD799X_EV_MASK,
681 .scan_type = IIO_ST('u', 10, 16, 2),
682 .event_mask = AD799X_EV_MASK,
689 .scan_type = IIO_ST('u', 10, 16, 2),
690 .event_mask = AD799X_EV_MASK,
697 .scan_type = IIO_ST('u', 10, 16, 2),
698 .event_mask = AD799X_EV_MASK,
705 .scan_type = IIO_ST('u', 10, 16, 2),
712 .scan_type = IIO_ST('u', 10, 16, 2),
719 .scan_type = IIO_ST('u', 10, 16, 2),
726 .scan_type = IIO_ST('u', 10, 16, 2),
728 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
732 .default_config = AD7998_ALERT_EN,
733 .info = &ad7993_4_7_8_info,
742 .scan_type = IIO_ST('u', 12, 16, 0),
743 .event_mask = AD799X_EV_MASK,
750 .scan_type = IIO_ST('u', 12, 16, 0),
751 .event_mask = AD799X_EV_MASK,
758 .scan_type = IIO_ST('u', 12, 16, 0),
759 .event_mask = AD799X_EV_MASK,
766 .scan_type = IIO_ST('u', 12, 16, 0),
767 .event_mask = AD799X_EV_MASK,
774 .scan_type = IIO_ST('u', 12, 16, 0),
781 .scan_type = IIO_ST('u', 12, 16, 0),
788 .scan_type = IIO_ST('u', 12, 16, 0),
795 .scan_type = IIO_ST('u', 12, 16, 0),
797 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
801 .default_config = AD7998_ALERT_EN,
802 .info = &ad7993_4_7_8_info,
806 static int __devinit ad799x_probe(struct i2c_client *client,
807 const struct i2c_device_id *id)
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));
814 if (indio_dev == NULL)
817 st = iio_priv(indio_dev);
818 /* this is only used for device removal purposes */
819 i2c_set_clientdata(client, indio_dev);
821 st->id = id->driver_data;
822 st->chip_info = &ad799x_chip_info_tbl[st->id];
823 st->config = st->chip_info->default_config;
825 /* TODO: Add pdata options for filtering and bit delay */
828 st->int_vref_mv = pdata->vref_mv;
830 st->int_vref_mv = st->chip_info->int_vref_mv;
832 st->reg = regulator_get(&client->dev, "vcc");
833 if (!IS_ERR(st->reg)) {
834 ret = regulator_enable(st->reg);
840 indio_dev->dev.parent = &client->dev;
841 indio_dev->name = id->name;
842 indio_dev->info = st->chip_info->info;
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;
848 ret = ad799x_register_ring_funcs_and_init(indio_dev);
850 goto error_disable_reg;
852 ret = iio_buffer_register(indio_dev,
854 indio_dev->num_channels);
856 goto error_cleanup_ring;
858 if (client->irq > 0) {
859 ret = request_threaded_irq(client->irq,
861 ad799x_event_handler,
862 IRQF_TRIGGER_FALLING |
867 goto error_cleanup_ring;
869 ret = iio_device_register(indio_dev);
876 free_irq(client->irq, indio_dev);
878 ad799x_ring_cleanup(indio_dev);
880 if (!IS_ERR(st->reg))
881 regulator_disable(st->reg);
883 if (!IS_ERR(st->reg))
884 regulator_put(st->reg);
885 iio_free_device(indio_dev);
890 static __devexit int ad799x_remove(struct i2c_client *client)
892 struct iio_dev *indio_dev = i2c_get_clientdata(client);
893 struct ad799x_state *st = iio_priv(indio_dev);
895 iio_device_unregister(indio_dev);
897 free_irq(client->irq, indio_dev);
899 iio_buffer_unregister(indio_dev);
900 ad799x_ring_cleanup(indio_dev);
901 if (!IS_ERR(st->reg)) {
902 regulator_disable(st->reg);
903 regulator_put(st->reg);
905 iio_free_device(indio_dev);
910 static const struct i2c_device_id ad799x_id[] = {
911 { "ad7991", ad7991 },
912 { "ad7995", ad7995 },
913 { "ad7999", ad7999 },
914 { "ad7992", ad7992 },
915 { "ad7993", ad7993 },
916 { "ad7994", ad7994 },
917 { "ad7997", ad7997 },
918 { "ad7998", ad7998 },
922 MODULE_DEVICE_TABLE(i2c, ad799x_id);
924 static struct i2c_driver ad799x_driver = {
928 .probe = ad799x_probe,
929 .remove = __devexit_p(ad799x_remove),
930 .id_table = ad799x_id,
932 module_i2c_driver(ad799x_driver);
934 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
935 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
936 MODULE_LICENSE("GPL v2");
937 MODULE_ALIAS("i2c:ad799x");