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 *dev_info,
140 struct iio_chan_spec const *chan,
146 struct ad799x_state *st = iio_priv(dev_info);
147 unsigned int scale_uv;
151 mutex_lock(&dev_info->mlock);
152 if (iio_buffer_enabled(dev_info))
153 ret = ad799x_single_channel_from_ring(st,
156 ret = ad799x_scan_direct(st, chan->address);
157 mutex_unlock(&dev_info->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;
173 static ssize_t ad799x_read_frequency(struct device *dev,
174 struct device_attribute *attr,
177 struct iio_dev *dev_info = dev_get_drvdata(dev);
178 struct ad799x_state *st = iio_priv(dev_info);
182 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
186 val &= AD7998_CYC_MASK;
190 len = sprintf(buf, "0\n");
192 case AD7998_CYC_TCONF_32:
193 len = sprintf(buf, "15625\n");
195 case AD7998_CYC_TCONF_64:
196 len = sprintf(buf, "7812\n");
198 case AD7998_CYC_TCONF_128:
199 len = sprintf(buf, "3906\n");
201 case AD7998_CYC_TCONF_256:
202 len = sprintf(buf, "1953\n");
204 case AD7998_CYC_TCONF_512:
205 len = sprintf(buf, "976\n");
207 case AD7998_CYC_TCONF_1024:
208 len = sprintf(buf, "488\n");
210 case AD7998_CYC_TCONF_2048:
211 len = sprintf(buf, "244\n");
217 static ssize_t ad799x_write_frequency(struct device *dev,
218 struct device_attribute *attr,
222 struct iio_dev *dev_info = dev_get_drvdata(dev);
223 struct ad799x_state *st = iio_priv(dev_info);
229 ret = strict_strtol(buf, 10, &val);
233 mutex_lock(&dev_info->mlock);
234 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
236 goto error_ret_mutex;
237 /* Wipe the bits clean */
238 t &= ~AD7998_CYC_MASK;
242 t |= AD7998_CYC_TCONF_32;
245 t |= AD7998_CYC_TCONF_64;
248 t |= AD7998_CYC_TCONF_128;
251 t |= AD7998_CYC_TCONF_256;
254 t |= AD7998_CYC_TCONF_512;
257 t |= AD7998_CYC_TCONF_1024;
260 t |= AD7998_CYC_TCONF_2048;
267 goto error_ret_mutex;
270 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
273 mutex_unlock(&dev_info->mlock);
275 return ret ? ret : len;
278 static ssize_t ad799x_read_channel_config(struct device *dev,
279 struct device_attribute *attr,
282 struct iio_dev *dev_info = dev_get_drvdata(dev);
283 struct ad799x_state *st = iio_priv(dev_info);
284 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
288 ret = ad799x_i2c_read16(st, this_attr->address, &val);
292 return sprintf(buf, "%d\n", val);
295 static ssize_t ad799x_write_channel_config(struct device *dev,
296 struct device_attribute *attr,
300 struct iio_dev *dev_info = dev_get_drvdata(dev);
301 struct ad799x_state *st = iio_priv(dev_info);
302 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
307 ret = strict_strtol(buf, 10, &val);
311 mutex_lock(&dev_info->mlock);
312 ret = ad799x_i2c_write16(st, this_attr->address, val);
313 mutex_unlock(&dev_info->mlock);
315 return ret ? ret : len;
318 static irqreturn_t ad799x_event_handler(int irq, void *private)
320 struct iio_dev *indio_dev = private;
321 struct ad799x_state *st = iio_priv(private);
325 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
332 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
334 for (i = 0; i < 8; i++) {
335 if (status & (1 << i))
336 iio_push_event(indio_dev,
338 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
342 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
352 static IIO_DEVICE_ATTR(in_voltage0_thresh_low_value,
354 ad799x_read_channel_config,
355 ad799x_write_channel_config,
356 AD7998_DATALOW_CH1_REG);
358 static IIO_DEVICE_ATTR(in_voltage0_thresh_high_value,
360 ad799x_read_channel_config,
361 ad799x_write_channel_config,
362 AD7998_DATAHIGH_CH1_REG);
364 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
366 ad799x_read_channel_config,
367 ad799x_write_channel_config,
368 AD7998_HYST_CH1_REG);
370 static IIO_DEVICE_ATTR(in_voltage1_thresh_low_value,
372 ad799x_read_channel_config,
373 ad799x_write_channel_config,
374 AD7998_DATALOW_CH2_REG);
376 static IIO_DEVICE_ATTR(in_voltage1_thresh_high_value,
378 ad799x_read_channel_config,
379 ad799x_write_channel_config,
380 AD7998_DATAHIGH_CH2_REG);
382 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
384 ad799x_read_channel_config,
385 ad799x_write_channel_config,
386 AD7998_HYST_CH2_REG);
388 static IIO_DEVICE_ATTR(in_voltage2_thresh_low_value,
390 ad799x_read_channel_config,
391 ad799x_write_channel_config,
392 AD7998_DATALOW_CH3_REG);
394 static IIO_DEVICE_ATTR(in_voltage2_thresh_high_value,
396 ad799x_read_channel_config,
397 ad799x_write_channel_config,
398 AD7998_DATAHIGH_CH3_REG);
400 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
402 ad799x_read_channel_config,
403 ad799x_write_channel_config,
404 AD7998_HYST_CH3_REG);
406 static IIO_DEVICE_ATTR(in_voltage3_thresh_low_value,
408 ad799x_read_channel_config,
409 ad799x_write_channel_config,
410 AD7998_DATALOW_CH4_REG);
412 static IIO_DEVICE_ATTR(in_voltage3_thresh_high_value,
414 ad799x_read_channel_config,
415 ad799x_write_channel_config,
416 AD7998_DATAHIGH_CH4_REG);
418 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
420 ad799x_read_channel_config,
421 ad799x_write_channel_config,
422 AD7998_HYST_CH4_REG);
424 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
425 ad799x_read_frequency,
426 ad799x_write_frequency);
427 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
429 static struct attribute *ad7993_4_7_8_event_attributes[] = {
430 &iio_dev_attr_in_voltage0_thresh_low_value.dev_attr.attr,
431 &iio_dev_attr_in_voltage0_thresh_high_value.dev_attr.attr,
432 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
433 &iio_dev_attr_in_voltage1_thresh_low_value.dev_attr.attr,
434 &iio_dev_attr_in_voltage1_thresh_high_value.dev_attr.attr,
435 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
436 &iio_dev_attr_in_voltage2_thresh_low_value.dev_attr.attr,
437 &iio_dev_attr_in_voltage2_thresh_high_value.dev_attr.attr,
438 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
439 &iio_dev_attr_in_voltage3_thresh_low_value.dev_attr.attr,
440 &iio_dev_attr_in_voltage3_thresh_high_value.dev_attr.attr,
441 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
442 &iio_dev_attr_sampling_frequency.dev_attr.attr,
443 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
447 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
448 .attrs = ad7993_4_7_8_event_attributes,
452 static struct attribute *ad7992_event_attributes[] = {
453 &iio_dev_attr_in_voltage0_thresh_low_value.dev_attr.attr,
454 &iio_dev_attr_in_voltage0_thresh_high_value.dev_attr.attr,
455 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
456 &iio_dev_attr_in_voltage1_thresh_low_value.dev_attr.attr,
457 &iio_dev_attr_in_voltage1_thresh_high_value.dev_attr.attr,
458 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
459 &iio_dev_attr_sampling_frequency.dev_attr.attr,
460 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
464 static struct attribute_group ad7992_event_attrs_group = {
465 .attrs = ad7992_event_attributes,
469 static const struct iio_info ad7991_info = {
470 .read_raw = &ad799x_read_raw,
471 .driver_module = THIS_MODULE,
474 static const struct iio_info ad7992_info = {
475 .read_raw = &ad799x_read_raw,
476 .event_attrs = &ad7992_event_attrs_group,
477 .driver_module = THIS_MODULE,
480 static const struct iio_info ad7993_4_7_8_info = {
481 .read_raw = &ad799x_read_raw,
482 .event_attrs = &ad7993_4_7_8_event_attrs_group,
483 .driver_module = THIS_MODULE,
486 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
495 .scan_type = IIO_ST('u', 12, 16, 0),
503 .scan_type = IIO_ST('u', 12, 16, 0),
511 .scan_type = IIO_ST('u', 12, 16, 0),
519 .scan_type = IIO_ST('u', 12, 16, 0),
521 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
525 .info = &ad7991_info,
535 .scan_type = IIO_ST('u', 10, 16, 2),
543 .scan_type = IIO_ST('u', 10, 16, 2),
551 .scan_type = IIO_ST('u', 10, 16, 2),
559 .scan_type = IIO_ST('u', 10, 16, 2),
561 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
565 .info = &ad7991_info,
575 .scan_type = IIO_ST('u', 8, 16, 4),
583 .scan_type = IIO_ST('u', 8, 16, 4),
591 .scan_type = IIO_ST('u', 8, 16, 4),
599 .scan_type = IIO_ST('u', 8, 16, 4),
601 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
605 .info = &ad7991_info,
615 .scan_type = IIO_ST('u', 12, 16, 0),
623 .scan_type = IIO_ST('u', 12, 16, 0),
625 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
629 .default_config = AD7998_ALERT_EN,
630 .info = &ad7992_info,
640 .scan_type = IIO_ST('u', 10, 16, 2),
648 .scan_type = IIO_ST('u', 10, 16, 2),
656 .scan_type = IIO_ST('u', 10, 16, 2),
664 .scan_type = IIO_ST('u', 10, 16, 2),
666 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
670 .default_config = AD7998_ALERT_EN,
671 .info = &ad7993_4_7_8_info,
681 .scan_type = IIO_ST('u', 12, 16, 0),
689 .scan_type = IIO_ST('u', 12, 16, 0),
697 .scan_type = IIO_ST('u', 12, 16, 0),
705 .scan_type = IIO_ST('u', 12, 16, 0),
707 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
711 .default_config = AD7998_ALERT_EN,
712 .info = &ad7993_4_7_8_info,
722 .scan_type = IIO_ST('u', 10, 16, 2),
730 .scan_type = IIO_ST('u', 10, 16, 2),
738 .scan_type = IIO_ST('u', 10, 16, 2),
746 .scan_type = IIO_ST('u', 10, 16, 2),
754 .scan_type = IIO_ST('u', 10, 16, 2),
762 .scan_type = IIO_ST('u', 10, 16, 2),
770 .scan_type = IIO_ST('u', 10, 16, 2),
778 .scan_type = IIO_ST('u', 10, 16, 2),
780 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
784 .default_config = AD7998_ALERT_EN,
785 .info = &ad7993_4_7_8_info,
795 .scan_type = IIO_ST('u', 12, 16, 0),
803 .scan_type = IIO_ST('u', 12, 16, 0),
811 .scan_type = IIO_ST('u', 12, 16, 0),
819 .scan_type = IIO_ST('u', 12, 16, 0),
827 .scan_type = IIO_ST('u', 12, 16, 0),
835 .scan_type = IIO_ST('u', 12, 16, 0),
843 .scan_type = IIO_ST('u', 12, 16, 0),
851 .scan_type = IIO_ST('u', 12, 16, 0),
853 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
857 .default_config = AD7998_ALERT_EN,
858 .info = &ad7993_4_7_8_info,
862 static int __devinit ad799x_probe(struct i2c_client *client,
863 const struct i2c_device_id *id)
866 struct ad799x_platform_data *pdata = client->dev.platform_data;
867 struct ad799x_state *st;
868 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
870 if (indio_dev == NULL)
873 st = iio_priv(indio_dev);
874 /* this is only used for device removal purposes */
875 i2c_set_clientdata(client, indio_dev);
877 st->id = id->driver_data;
878 st->chip_info = &ad799x_chip_info_tbl[st->id];
879 st->config = st->chip_info->default_config;
881 /* TODO: Add pdata options for filtering and bit delay */
884 st->int_vref_mv = pdata->vref_mv;
886 st->int_vref_mv = st->chip_info->int_vref_mv;
888 st->reg = regulator_get(&client->dev, "vcc");
889 if (!IS_ERR(st->reg)) {
890 ret = regulator_enable(st->reg);
896 indio_dev->dev.parent = &client->dev;
897 indio_dev->name = id->name;
898 indio_dev->info = st->chip_info->info;
899 indio_dev->name = id->name;
901 indio_dev->modes = INDIO_DIRECT_MODE;
902 indio_dev->channels = st->chip_info->channel;
903 indio_dev->num_channels = st->chip_info->num_channels;
905 ret = ad799x_register_ring_funcs_and_init(indio_dev);
907 goto error_disable_reg;
909 ret = iio_buffer_register(indio_dev,
911 indio_dev->num_channels);
913 goto error_cleanup_ring;
915 if (client->irq > 0) {
916 ret = request_threaded_irq(client->irq,
918 ad799x_event_handler,
919 IRQF_TRIGGER_FALLING |
924 goto error_cleanup_ring;
926 ret = iio_device_register(indio_dev);
933 free_irq(client->irq, indio_dev);
935 ad799x_ring_cleanup(indio_dev);
937 if (!IS_ERR(st->reg))
938 regulator_disable(st->reg);
940 if (!IS_ERR(st->reg))
941 regulator_put(st->reg);
942 iio_free_device(indio_dev);
947 static __devexit int ad799x_remove(struct i2c_client *client)
949 struct iio_dev *indio_dev = i2c_get_clientdata(client);
950 struct ad799x_state *st = iio_priv(indio_dev);
953 free_irq(client->irq, indio_dev);
955 iio_buffer_unregister(indio_dev);
956 ad799x_ring_cleanup(indio_dev);
957 if (!IS_ERR(st->reg)) {
958 regulator_disable(st->reg);
959 regulator_put(st->reg);
961 iio_device_unregister(indio_dev);
966 static const struct i2c_device_id ad799x_id[] = {
967 { "ad7991", ad7991 },
968 { "ad7995", ad7995 },
969 { "ad7999", ad7999 },
970 { "ad7992", ad7992 },
971 { "ad7993", ad7993 },
972 { "ad7994", ad7994 },
973 { "ad7997", ad7997 },
974 { "ad7998", ad7998 },
978 MODULE_DEVICE_TABLE(i2c, ad799x_id);
980 static struct i2c_driver ad799x_driver = {
984 .probe = ad799x_probe,
985 .remove = __devexit_p(ad799x_remove),
986 .id_table = ad799x_id,
989 static __init int ad799x_init(void)
991 return i2c_add_driver(&ad799x_driver);
994 static __exit void ad799x_exit(void)
996 i2c_del_driver(&ad799x_driver);
999 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
1000 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
1001 MODULE_LICENSE("GPL v2");
1002 MODULE_ALIAS("i2c:ad799x");
1004 module_init(ad799x_init);
1005 module_exit(ad799x_exit);