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>
36 #include <linux/iio/iio.h>
37 #include <linux/iio/sysfs.h>
38 #include <linux/iio/events.h>
39 #include <linux/iio/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 static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
103 const unsigned long *scan_mask)
105 struct ad799x_state *st = iio_priv(indio_dev);
110 return ad799x_i2c_write16(st, AD7998_CONF_REG,
111 st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
119 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
129 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
134 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
138 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
144 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
151 static int ad799x_read_raw(struct iio_dev *indio_dev,
152 struct iio_chan_spec const *chan,
158 struct ad799x_state *st = iio_priv(indio_dev);
159 unsigned int scale_uv;
162 case IIO_CHAN_INFO_RAW:
163 mutex_lock(&indio_dev->mlock);
164 if (iio_buffer_enabled(indio_dev))
167 ret = ad799x_scan_direct(st, chan->scan_index);
168 mutex_unlock(&indio_dev->mlock);
172 *val = (ret >> chan->scan_type.shift) &
173 RES_MASK(chan->scan_type.realbits);
175 case IIO_CHAN_INFO_SCALE:
176 scale_uv = (st->int_vref_mv * 1000) >> chan->scan_type.realbits;
177 *val = scale_uv / 1000;
178 *val2 = (scale_uv % 1000) * 1000;
179 return IIO_VAL_INT_PLUS_MICRO;
183 static const unsigned int ad7998_frequencies[] = {
184 [AD7998_CYC_DIS] = 0,
185 [AD7998_CYC_TCONF_32] = 15625,
186 [AD7998_CYC_TCONF_64] = 7812,
187 [AD7998_CYC_TCONF_128] = 3906,
188 [AD7998_CYC_TCONF_512] = 976,
189 [AD7998_CYC_TCONF_1024] = 488,
190 [AD7998_CYC_TCONF_2048] = 244,
192 static ssize_t ad799x_read_frequency(struct device *dev,
193 struct device_attribute *attr,
196 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
197 struct ad799x_state *st = iio_priv(indio_dev);
201 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
205 val &= AD7998_CYC_MASK;
207 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
210 static ssize_t ad799x_write_frequency(struct device *dev,
211 struct device_attribute *attr,
215 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
216 struct ad799x_state *st = iio_priv(indio_dev);
222 ret = strict_strtol(buf, 10, &val);
226 mutex_lock(&indio_dev->mlock);
227 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
229 goto error_ret_mutex;
230 /* Wipe the bits clean */
231 t &= ~AD7998_CYC_MASK;
233 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
234 if (val == ad7998_frequencies[i])
236 if (i == ARRAY_SIZE(ad7998_frequencies)) {
238 goto error_ret_mutex;
241 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
244 mutex_unlock(&indio_dev->mlock);
246 return ret ? ret : len;
249 static int ad799x_read_event_config(struct iio_dev *indio_dev,
255 static const u8 ad799x_threshold_addresses[][2] = {
256 { AD7998_DATALOW_CH1_REG, AD7998_DATAHIGH_CH1_REG },
257 { AD7998_DATALOW_CH2_REG, AD7998_DATAHIGH_CH2_REG },
258 { AD7998_DATALOW_CH3_REG, AD7998_DATAHIGH_CH3_REG },
259 { AD7998_DATALOW_CH4_REG, AD7998_DATAHIGH_CH4_REG },
262 static int ad799x_write_event_value(struct iio_dev *indio_dev,
267 struct ad799x_state *st = iio_priv(indio_dev);
268 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
270 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
272 mutex_lock(&indio_dev->mlock);
273 ret = ad799x_i2c_write16(st,
274 ad799x_threshold_addresses[number][direction],
276 mutex_unlock(&indio_dev->mlock);
281 static int ad799x_read_event_value(struct iio_dev *indio_dev,
286 struct ad799x_state *st = iio_priv(indio_dev);
287 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
289 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
292 mutex_lock(&indio_dev->mlock);
293 ret = ad799x_i2c_read16(st,
294 ad799x_threshold_addresses[number][direction],
296 mutex_unlock(&indio_dev->mlock);
304 static ssize_t ad799x_read_channel_config(struct device *dev,
305 struct device_attribute *attr,
308 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
309 struct ad799x_state *st = iio_priv(indio_dev);
310 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
314 ret = ad799x_i2c_read16(st, this_attr->address, &val);
318 return sprintf(buf, "%d\n", val);
321 static ssize_t ad799x_write_channel_config(struct device *dev,
322 struct device_attribute *attr,
326 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
327 struct ad799x_state *st = iio_priv(indio_dev);
328 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
333 ret = strict_strtol(buf, 10, &val);
337 mutex_lock(&indio_dev->mlock);
338 ret = ad799x_i2c_write16(st, this_attr->address, val);
339 mutex_unlock(&indio_dev->mlock);
341 return ret ? ret : len;
344 static irqreturn_t ad799x_event_handler(int irq, void *private)
346 struct iio_dev *indio_dev = private;
347 struct ad799x_state *st = iio_priv(private);
351 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
358 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
360 for (i = 0; i < 8; i++) {
361 if (status & (1 << i))
362 iio_push_event(indio_dev,
364 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
368 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
379 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
381 ad799x_read_channel_config,
382 ad799x_write_channel_config,
383 AD7998_HYST_CH1_REG);
385 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
387 ad799x_read_channel_config,
388 ad799x_write_channel_config,
389 AD7998_HYST_CH2_REG);
391 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
393 ad799x_read_channel_config,
394 ad799x_write_channel_config,
395 AD7998_HYST_CH3_REG);
397 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
399 ad799x_read_channel_config,
400 ad799x_write_channel_config,
401 AD7998_HYST_CH4_REG);
403 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
404 ad799x_read_frequency,
405 ad799x_write_frequency);
406 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
408 static struct attribute *ad7993_4_7_8_event_attributes[] = {
409 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
410 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
411 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
412 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
413 &iio_dev_attr_sampling_frequency.dev_attr.attr,
414 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
418 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
419 .attrs = ad7993_4_7_8_event_attributes,
423 static struct attribute *ad7992_event_attributes[] = {
424 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
425 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
426 &iio_dev_attr_sampling_frequency.dev_attr.attr,
427 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
431 static struct attribute_group ad7992_event_attrs_group = {
432 .attrs = ad7992_event_attributes,
436 static const struct iio_info ad7991_info = {
437 .read_raw = &ad799x_read_raw,
438 .driver_module = THIS_MODULE,
441 static const struct iio_info ad7992_info = {
442 .read_raw = &ad799x_read_raw,
443 .event_attrs = &ad7992_event_attrs_group,
444 .read_event_config = &ad799x_read_event_config,
445 .read_event_value = &ad799x_read_event_value,
446 .write_event_value = &ad799x_write_event_value,
447 .driver_module = THIS_MODULE,
450 static const struct iio_info ad7993_4_7_8_info = {
451 .read_raw = &ad799x_read_raw,
452 .event_attrs = &ad7993_4_7_8_event_attrs_group,
453 .read_event_config = &ad799x_read_event_config,
454 .read_event_value = &ad799x_read_event_value,
455 .write_event_value = &ad799x_write_event_value,
456 .driver_module = THIS_MODULE,
457 .update_scan_mode = ad7997_8_update_scan_mode,
460 #define AD799X_EV_MASK (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
461 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
463 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
470 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
472 .scan_type = IIO_ST('u', 12, 16, 0),
478 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
480 .scan_type = IIO_ST('u', 12, 16, 0),
486 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
488 .scan_type = IIO_ST('u', 12, 16, 0),
494 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
496 .scan_type = IIO_ST('u', 12, 16, 0),
498 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
502 .info = &ad7991_info,
510 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
512 .scan_type = IIO_ST('u', 10, 16, 2),
518 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
520 .scan_type = IIO_ST('u', 10, 16, 2),
526 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
528 .scan_type = IIO_ST('u', 10, 16, 2),
534 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
536 .scan_type = IIO_ST('u', 10, 16, 2),
538 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
542 .info = &ad7991_info,
550 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
552 .scan_type = IIO_ST('u', 8, 16, 4),
558 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
560 .scan_type = IIO_ST('u', 8, 16, 4),
566 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
568 .scan_type = IIO_ST('u', 8, 16, 4),
574 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
576 .scan_type = IIO_ST('u', 8, 16, 4),
578 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
582 .info = &ad7991_info,
590 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
592 .scan_type = IIO_ST('u', 12, 16, 0),
593 .event_mask = AD799X_EV_MASK,
599 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
601 .scan_type = IIO_ST('u', 12, 16, 0),
602 .event_mask = AD799X_EV_MASK,
604 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
608 .default_config = AD7998_ALERT_EN,
609 .info = &ad7992_info,
617 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
619 .scan_type = IIO_ST('u', 10, 16, 2),
620 .event_mask = AD799X_EV_MASK,
627 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
628 .scan_type = IIO_ST('u', 10, 16, 2),
629 .event_mask = AD799X_EV_MASK,
635 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
637 .scan_type = IIO_ST('u', 10, 16, 2),
638 .event_mask = AD799X_EV_MASK,
644 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
646 .scan_type = IIO_ST('u', 10, 16, 2),
647 .event_mask = AD799X_EV_MASK,
649 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
653 .default_config = AD7998_ALERT_EN,
654 .info = &ad7993_4_7_8_info,
662 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
664 .scan_type = IIO_ST('u', 12, 16, 0),
665 .event_mask = AD799X_EV_MASK,
671 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
673 .scan_type = IIO_ST('u', 12, 16, 0),
674 .event_mask = AD799X_EV_MASK,
680 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
682 .scan_type = IIO_ST('u', 12, 16, 0),
683 .event_mask = AD799X_EV_MASK,
689 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
691 .scan_type = IIO_ST('u', 12, 16, 0),
692 .event_mask = AD799X_EV_MASK,
694 [4] = IIO_CHAN_SOFT_TIMESTAMP(4),
698 .default_config = AD7998_ALERT_EN,
699 .info = &ad7993_4_7_8_info,
707 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
709 .scan_type = IIO_ST('u', 10, 16, 2),
710 .event_mask = AD799X_EV_MASK,
716 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
718 .scan_type = IIO_ST('u', 10, 16, 2),
719 .event_mask = AD799X_EV_MASK,
725 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
727 .scan_type = IIO_ST('u', 10, 16, 2),
728 .event_mask = AD799X_EV_MASK,
734 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
736 .scan_type = IIO_ST('u', 10, 16, 2),
737 .event_mask = AD799X_EV_MASK,
743 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
745 .scan_type = IIO_ST('u', 10, 16, 2),
751 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
753 .scan_type = IIO_ST('u', 10, 16, 2),
759 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
761 .scan_type = IIO_ST('u', 10, 16, 2),
767 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
769 .scan_type = IIO_ST('u', 10, 16, 2),
771 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
775 .default_config = AD7998_ALERT_EN,
776 .info = &ad7993_4_7_8_info,
784 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
786 .scan_type = IIO_ST('u', 12, 16, 0),
787 .event_mask = AD799X_EV_MASK,
793 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
795 .scan_type = IIO_ST('u', 12, 16, 0),
796 .event_mask = AD799X_EV_MASK,
802 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
804 .scan_type = IIO_ST('u', 12, 16, 0),
805 .event_mask = AD799X_EV_MASK,
811 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
813 .scan_type = IIO_ST('u', 12, 16, 0),
814 .event_mask = AD799X_EV_MASK,
820 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
822 .scan_type = IIO_ST('u', 12, 16, 0),
828 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
830 .scan_type = IIO_ST('u', 12, 16, 0),
836 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
838 .scan_type = IIO_ST('u', 12, 16, 0),
844 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
846 .scan_type = IIO_ST('u', 12, 16, 0),
848 [8] = IIO_CHAN_SOFT_TIMESTAMP(8),
852 .default_config = AD7998_ALERT_EN,
853 .info = &ad7993_4_7_8_info,
857 static int __devinit ad799x_probe(struct i2c_client *client,
858 const struct i2c_device_id *id)
861 struct ad799x_platform_data *pdata = client->dev.platform_data;
862 struct ad799x_state *st;
863 struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
865 if (indio_dev == NULL)
868 st = iio_priv(indio_dev);
869 /* this is only used for device removal purposes */
870 i2c_set_clientdata(client, indio_dev);
872 st->id = id->driver_data;
873 st->chip_info = &ad799x_chip_info_tbl[st->id];
874 st->config = st->chip_info->default_config;
876 /* TODO: Add pdata options for filtering and bit delay */
879 st->int_vref_mv = pdata->vref_mv;
881 st->int_vref_mv = st->chip_info->int_vref_mv;
883 st->reg = regulator_get(&client->dev, "vcc");
884 if (!IS_ERR(st->reg)) {
885 ret = regulator_enable(st->reg);
891 indio_dev->dev.parent = &client->dev;
892 indio_dev->name = id->name;
893 indio_dev->info = st->chip_info->info;
895 indio_dev->modes = INDIO_DIRECT_MODE;
896 indio_dev->channels = st->chip_info->channel;
897 indio_dev->num_channels = st->chip_info->num_channels;
899 ret = ad799x_register_ring_funcs_and_init(indio_dev);
901 goto error_disable_reg;
903 if (client->irq > 0) {
904 ret = request_threaded_irq(client->irq,
906 ad799x_event_handler,
907 IRQF_TRIGGER_FALLING |
912 goto error_cleanup_ring;
914 ret = iio_device_register(indio_dev);
921 free_irq(client->irq, indio_dev);
923 ad799x_ring_cleanup(indio_dev);
925 if (!IS_ERR(st->reg))
926 regulator_disable(st->reg);
928 if (!IS_ERR(st->reg))
929 regulator_put(st->reg);
930 iio_device_free(indio_dev);
935 static __devexit int ad799x_remove(struct i2c_client *client)
937 struct iio_dev *indio_dev = i2c_get_clientdata(client);
938 struct ad799x_state *st = iio_priv(indio_dev);
940 iio_device_unregister(indio_dev);
942 free_irq(client->irq, indio_dev);
944 ad799x_ring_cleanup(indio_dev);
945 if (!IS_ERR(st->reg)) {
946 regulator_disable(st->reg);
947 regulator_put(st->reg);
949 iio_device_free(indio_dev);
954 static const struct i2c_device_id ad799x_id[] = {
955 { "ad7991", ad7991 },
956 { "ad7995", ad7995 },
957 { "ad7999", ad7999 },
958 { "ad7992", ad7992 },
959 { "ad7993", ad7993 },
960 { "ad7994", ad7994 },
961 { "ad7997", ad7997 },
962 { "ad7998", ad7998 },
966 MODULE_DEVICE_TABLE(i2c, ad799x_id);
968 static struct i2c_driver ad799x_driver = {
972 .probe = ad799x_probe,
973 .remove = __devexit_p(ad799x_remove),
974 .id_table = ad799x_id,
976 module_i2c_driver(ad799x_driver);
978 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
979 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
980 MODULE_LICENSE("GPL v2");