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 "../events.h"
39 #include "../buffer.h"
44 * ad799x register access by I2C
46 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
48 struct i2c_client *client = st->client;
51 ret = i2c_smbus_read_word_data(client, reg);
53 dev_err(&client->dev, "I2C read error\n");
57 *data = swab16((u16)ret);
62 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
64 struct i2c_client *client = st->client;
67 ret = i2c_smbus_read_byte_data(client, reg);
69 dev_err(&client->dev, "I2C read error\n");
78 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
80 struct i2c_client *client = st->client;
83 ret = i2c_smbus_write_word_data(client, reg, swab16(data));
85 dev_err(&client->dev, "I2C write error\n");
90 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
92 struct i2c_client *client = st->client;
95 ret = i2c_smbus_write_byte_data(client, reg, data);
97 dev_err(&client->dev, "I2C write error\n");
102 int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
104 return ad799x_i2c_write16(st, AD7998_CONF_REG,
105 st->config | (mask << AD799X_CHANNEL_SHIFT));
108 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
118 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
123 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
127 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
133 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
140 static int ad799x_read_raw(struct iio_dev *indio_dev,
141 struct iio_chan_spec const *chan,
147 struct ad799x_state *st = iio_priv(indio_dev);
148 unsigned int scale_uv;
152 mutex_lock(&indio_dev->mlock);
153 if (iio_buffer_enabled(indio_dev))
154 ret = ad799x_single_channel_from_ring(indio_dev,
157 ret = ad799x_scan_direct(st, chan->scan_index);
158 mutex_unlock(&indio_dev->mlock);
162 *val = (ret >> chan->scan_type.shift) &
163 RES_MASK(chan->scan_type.realbits);
165 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
166 scale_uv = (st->int_vref_mv * 1000) >> chan->scan_type.realbits;
167 *val = scale_uv / 1000;
168 *val2 = (scale_uv % 1000) * 1000;
169 return IIO_VAL_INT_PLUS_MICRO;
173 static 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,
182 static ssize_t ad799x_read_frequency(struct device *dev,
183 struct device_attribute *attr,
186 struct iio_dev *indio_dev = dev_get_drvdata(dev);
187 struct ad799x_state *st = iio_priv(indio_dev);
191 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
195 val &= AD7998_CYC_MASK;
197 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
200 static ssize_t ad799x_write_frequency(struct device *dev,
201 struct device_attribute *attr,
205 struct iio_dev *indio_dev = dev_get_drvdata(dev);
206 struct ad799x_state *st = iio_priv(indio_dev);
212 ret = strict_strtol(buf, 10, &val);
216 mutex_lock(&indio_dev->mlock);
217 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
219 goto error_ret_mutex;
220 /* Wipe the bits clean */
221 t &= ~AD7998_CYC_MASK;
223 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
224 if (val == ad7998_frequencies[i])
226 if (i == ARRAY_SIZE(ad7998_frequencies)) {
228 goto error_ret_mutex;
231 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
234 mutex_unlock(&indio_dev->mlock);
236 return ret ? ret : len;
239 static int ad799x_read_event_config(struct iio_dev *indio_dev,
245 static 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 },
252 static int ad799x_write_event_value(struct iio_dev *indio_dev,
257 struct ad799x_state *st = iio_priv(indio_dev);
258 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
260 int number = IIO_EVENT_CODE_EXTRACT_NUM(event_code);
262 mutex_lock(&indio_dev->mlock);
263 ret = ad799x_i2c_write16(st,
264 ad799x_threshold_addresses[number][direction],
266 mutex_unlock(&indio_dev->mlock);
271 static int ad799x_read_event_value(struct iio_dev *indio_dev,
276 struct ad799x_state *st = iio_priv(indio_dev);
277 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
279 int number = IIO_EVENT_CODE_EXTRACT_NUM(event_code);
282 mutex_lock(&indio_dev->mlock);
283 ret = ad799x_i2c_read16(st,
284 ad799x_threshold_addresses[number][direction],
286 mutex_unlock(&indio_dev->mlock);
294 static ssize_t ad799x_read_channel_config(struct device *dev,
295 struct device_attribute *attr,
298 struct iio_dev *indio_dev = dev_get_drvdata(dev);
299 struct ad799x_state *st = iio_priv(indio_dev);
300 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
304 ret = ad799x_i2c_read16(st, this_attr->address, &val);
308 return sprintf(buf, "%d\n", val);
311 static ssize_t ad799x_write_channel_config(struct device *dev,
312 struct device_attribute *attr,
316 struct iio_dev *indio_dev = dev_get_drvdata(dev);
317 struct ad799x_state *st = iio_priv(indio_dev);
318 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
323 ret = strict_strtol(buf, 10, &val);
327 mutex_lock(&indio_dev->mlock);
328 ret = ad799x_i2c_write16(st, this_attr->address, val);
329 mutex_unlock(&indio_dev->mlock);
331 return ret ? ret : len;
334 static irqreturn_t ad799x_event_handler(int irq, void *private)
336 struct iio_dev *indio_dev = private;
337 struct ad799x_state *st = iio_priv(private);
341 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
348 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
350 for (i = 0; i < 8; i++) {
351 if (status & (1 << i))
352 iio_push_event(indio_dev,
354 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
358 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
368 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
370 ad799x_read_channel_config,
371 ad799x_write_channel_config,
372 AD7998_HYST_CH1_REG);
374 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
376 ad799x_read_channel_config,
377 ad799x_write_channel_config,
378 AD7998_HYST_CH2_REG);
380 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
382 ad799x_read_channel_config,
383 ad799x_write_channel_config,
384 AD7998_HYST_CH3_REG);
386 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
388 ad799x_read_channel_config,
389 ad799x_write_channel_config,
390 AD7998_HYST_CH4_REG);
392 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
393 ad799x_read_frequency,
394 ad799x_write_frequency);
395 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
397 static struct attribute *ad7993_4_7_8_event_attributes[] = {
398 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
399 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
400 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
401 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
402 &iio_dev_attr_sampling_frequency.dev_attr.attr,
403 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
407 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
408 .attrs = ad7993_4_7_8_event_attributes,
412 static struct attribute *ad7992_event_attributes[] = {
413 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
414 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
415 &iio_dev_attr_sampling_frequency.dev_attr.attr,
416 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
420 static struct attribute_group ad7992_event_attrs_group = {
421 .attrs = ad7992_event_attributes,
425 static const struct iio_info ad7991_info = {
426 .read_raw = &ad799x_read_raw,
427 .driver_module = THIS_MODULE,
430 static const struct iio_info ad7992_info = {
431 .read_raw = &ad799x_read_raw,
432 .event_attrs = &ad7992_event_attrs_group,
433 .read_event_config = &ad799x_read_event_config,
434 .read_event_value = &ad799x_read_event_value,
435 .write_event_value = &ad799x_write_event_value,
436 .driver_module = THIS_MODULE,
439 static const struct iio_info ad7993_4_7_8_info = {
440 .read_raw = &ad799x_read_raw,
441 .event_attrs = &ad7993_4_7_8_event_attrs_group,
442 .read_event_config = &ad799x_read_event_config,
443 .read_event_value = &ad799x_read_event_value,
444 .write_event_value = &ad799x_write_event_value,
445 .driver_module = THIS_MODULE,
448 #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))
451 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
459 .scan_type = IIO_ST('u', 12, 16, 0),
466 .scan_type = IIO_ST('u', 12, 16, 0),
473 .scan_type = IIO_ST('u', 12, 16, 0),
480 .scan_type = IIO_ST('u', 12, 16, 0),
482 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
486 .info = &ad7991_info,
495 .scan_type = IIO_ST('u', 10, 16, 2),
502 .scan_type = IIO_ST('u', 10, 16, 2),
509 .scan_type = IIO_ST('u', 10, 16, 2),
516 .scan_type = IIO_ST('u', 10, 16, 2),
518 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
522 .info = &ad7991_info,
531 .scan_type = IIO_ST('u', 8, 16, 4),
538 .scan_type = IIO_ST('u', 8, 16, 4),
545 .scan_type = IIO_ST('u', 8, 16, 4),
552 .scan_type = IIO_ST('u', 8, 16, 4),
554 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
558 .info = &ad7991_info,
567 .scan_type = IIO_ST('u', 12, 16, 0),
568 .event_mask = AD799X_EV_MASK,
575 .scan_type = IIO_ST('u', 12, 16, 0),
576 .event_mask = AD799X_EV_MASK,
578 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
582 .default_config = AD7998_ALERT_EN,
583 .info = &ad7992_info,
592 .scan_type = IIO_ST('u', 10, 16, 2),
593 .event_mask = AD799X_EV_MASK,
600 .scan_type = IIO_ST('u', 10, 16, 2),
601 .event_mask = AD799X_EV_MASK,
608 .scan_type = IIO_ST('u', 10, 16, 2),
609 .event_mask = AD799X_EV_MASK,
616 .scan_type = IIO_ST('u', 10, 16, 2),
617 .event_mask = AD799X_EV_MASK,
619 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
623 .default_config = AD7998_ALERT_EN,
624 .info = &ad7993_4_7_8_info,
633 .scan_type = IIO_ST('u', 12, 16, 0),
634 .event_mask = AD799X_EV_MASK,
641 .scan_type = IIO_ST('u', 12, 16, 0),
642 .event_mask = AD799X_EV_MASK,
649 .scan_type = IIO_ST('u', 12, 16, 0),
650 .event_mask = AD799X_EV_MASK,
657 .scan_type = IIO_ST('u', 12, 16, 0),
658 .event_mask = AD799X_EV_MASK,
660 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
664 .default_config = AD7998_ALERT_EN,
665 .info = &ad7993_4_7_8_info,
674 .scan_type = IIO_ST('u', 10, 16, 2),
675 .event_mask = AD799X_EV_MASK,
682 .scan_type = IIO_ST('u', 10, 16, 2),
683 .event_mask = AD799X_EV_MASK,
690 .scan_type = IIO_ST('u', 10, 16, 2),
691 .event_mask = AD799X_EV_MASK,
698 .scan_type = IIO_ST('u', 10, 16, 2),
699 .event_mask = AD799X_EV_MASK,
706 .scan_type = IIO_ST('u', 10, 16, 2),
713 .scan_type = IIO_ST('u', 10, 16, 2),
720 .scan_type = IIO_ST('u', 10, 16, 2),
727 .scan_type = IIO_ST('u', 10, 16, 2),
729 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
733 .default_config = AD7998_ALERT_EN,
734 .info = &ad7993_4_7_8_info,
743 .scan_type = IIO_ST('u', 12, 16, 0),
744 .event_mask = AD799X_EV_MASK,
751 .scan_type = IIO_ST('u', 12, 16, 0),
752 .event_mask = AD799X_EV_MASK,
759 .scan_type = IIO_ST('u', 12, 16, 0),
760 .event_mask = AD799X_EV_MASK,
767 .scan_type = IIO_ST('u', 12, 16, 0),
768 .event_mask = AD799X_EV_MASK,
775 .scan_type = IIO_ST('u', 12, 16, 0),
782 .scan_type = IIO_ST('u', 12, 16, 0),
789 .scan_type = IIO_ST('u', 12, 16, 0),
796 .scan_type = IIO_ST('u', 12, 16, 0),
798 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
802 .default_config = AD7998_ALERT_EN,
803 .info = &ad7993_4_7_8_info,
807 static int __devinit ad799x_probe(struct i2c_client *client,
808 const struct i2c_device_id *id)
811 struct ad799x_platform_data *pdata = client->dev.platform_data;
812 struct ad799x_state *st;
813 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
815 if (indio_dev == NULL)
818 st = iio_priv(indio_dev);
819 /* this is only used for device removal purposes */
820 i2c_set_clientdata(client, indio_dev);
822 st->id = id->driver_data;
823 st->chip_info = &ad799x_chip_info_tbl[st->id];
824 st->config = st->chip_info->default_config;
826 /* TODO: Add pdata options for filtering and bit delay */
829 st->int_vref_mv = pdata->vref_mv;
831 st->int_vref_mv = st->chip_info->int_vref_mv;
833 st->reg = regulator_get(&client->dev, "vcc");
834 if (!IS_ERR(st->reg)) {
835 ret = regulator_enable(st->reg);
841 indio_dev->dev.parent = &client->dev;
842 indio_dev->name = id->name;
843 indio_dev->info = st->chip_info->info;
845 indio_dev->modes = INDIO_DIRECT_MODE;
846 indio_dev->channels = st->chip_info->channel;
847 indio_dev->num_channels = st->chip_info->num_channels;
849 ret = ad799x_register_ring_funcs_and_init(indio_dev);
851 goto error_disable_reg;
853 ret = iio_buffer_register(indio_dev,
855 indio_dev->num_channels);
857 goto error_cleanup_ring;
859 if (client->irq > 0) {
860 ret = request_threaded_irq(client->irq,
862 ad799x_event_handler,
863 IRQF_TRIGGER_FALLING |
868 goto error_cleanup_ring;
870 ret = iio_device_register(indio_dev);
877 free_irq(client->irq, indio_dev);
879 ad799x_ring_cleanup(indio_dev);
881 if (!IS_ERR(st->reg))
882 regulator_disable(st->reg);
884 if (!IS_ERR(st->reg))
885 regulator_put(st->reg);
886 iio_free_device(indio_dev);
891 static __devexit int ad799x_remove(struct i2c_client *client)
893 struct iio_dev *indio_dev = i2c_get_clientdata(client);
894 struct ad799x_state *st = iio_priv(indio_dev);
896 iio_device_unregister(indio_dev);
898 free_irq(client->irq, indio_dev);
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);
906 iio_free_device(indio_dev);
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 },
923 MODULE_DEVICE_TABLE(i2c, ad799x_id);
925 static struct i2c_driver ad799x_driver = {
929 .probe = ad799x_probe,
930 .remove = __devexit_p(ad799x_remove),
931 .id_table = ad799x_id,
934 static __init int ad799x_init(void)
936 return i2c_add_driver(&ad799x_driver);
939 static __exit void ad799x_exit(void)
941 i2c_del_driver(&ad799x_driver);
944 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
945 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
946 MODULE_LICENSE("GPL v2");
947 MODULE_ALIAS("i2c:ad799x");
949 module_init(ad799x_init);
950 module_exit(ad799x_exit);