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_swapped(client, reg);
53 dev_err(&client->dev, "I2C read error\n");
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_swapped(client, reg, 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);
108 st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
112 st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
117 return ad799x_i2c_write16(st, AD7998_CONF_REG,
118 st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
126 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
136 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
141 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
145 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
151 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
158 static int ad799x_read_raw(struct iio_dev *indio_dev,
159 struct iio_chan_spec const *chan,
165 struct ad799x_state *st = iio_priv(indio_dev);
166 unsigned int scale_uv;
169 case IIO_CHAN_INFO_RAW:
170 mutex_lock(&indio_dev->mlock);
171 if (iio_buffer_enabled(indio_dev))
174 ret = ad799x_scan_direct(st, chan->scan_index);
175 mutex_unlock(&indio_dev->mlock);
179 *val = (ret >> chan->scan_type.shift) &
180 RES_MASK(chan->scan_type.realbits);
182 case IIO_CHAN_INFO_SCALE:
183 scale_uv = (st->int_vref_mv * 1000) >> chan->scan_type.realbits;
184 *val = scale_uv / 1000;
185 *val2 = (scale_uv % 1000) * 1000;
186 return IIO_VAL_INT_PLUS_MICRO;
190 static const unsigned int ad7998_frequencies[] = {
191 [AD7998_CYC_DIS] = 0,
192 [AD7998_CYC_TCONF_32] = 15625,
193 [AD7998_CYC_TCONF_64] = 7812,
194 [AD7998_CYC_TCONF_128] = 3906,
195 [AD7998_CYC_TCONF_512] = 976,
196 [AD7998_CYC_TCONF_1024] = 488,
197 [AD7998_CYC_TCONF_2048] = 244,
199 static ssize_t ad799x_read_frequency(struct device *dev,
200 struct device_attribute *attr,
203 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
204 struct ad799x_state *st = iio_priv(indio_dev);
208 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
212 val &= AD7998_CYC_MASK;
214 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
217 static ssize_t ad799x_write_frequency(struct device *dev,
218 struct device_attribute *attr,
222 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
223 struct ad799x_state *st = iio_priv(indio_dev);
229 ret = strict_strtol(buf, 10, &val);
233 mutex_lock(&indio_dev->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;
240 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
241 if (val == ad7998_frequencies[i])
243 if (i == ARRAY_SIZE(ad7998_frequencies)) {
245 goto error_ret_mutex;
248 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
251 mutex_unlock(&indio_dev->mlock);
253 return ret ? ret : len;
256 static int ad799x_read_event_config(struct iio_dev *indio_dev,
262 static const u8 ad799x_threshold_addresses[][2] = {
263 { AD7998_DATALOW_CH1_REG, AD7998_DATAHIGH_CH1_REG },
264 { AD7998_DATALOW_CH2_REG, AD7998_DATAHIGH_CH2_REG },
265 { AD7998_DATALOW_CH3_REG, AD7998_DATAHIGH_CH3_REG },
266 { AD7998_DATALOW_CH4_REG, AD7998_DATAHIGH_CH4_REG },
269 static int ad799x_write_event_value(struct iio_dev *indio_dev,
274 struct ad799x_state *st = iio_priv(indio_dev);
275 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
277 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
279 mutex_lock(&indio_dev->mlock);
280 ret = ad799x_i2c_write16(st,
281 ad799x_threshold_addresses[number][direction],
283 mutex_unlock(&indio_dev->mlock);
288 static int ad799x_read_event_value(struct iio_dev *indio_dev,
293 struct ad799x_state *st = iio_priv(indio_dev);
294 int direction = !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
296 int number = IIO_EVENT_CODE_EXTRACT_CHAN(event_code);
299 mutex_lock(&indio_dev->mlock);
300 ret = ad799x_i2c_read16(st,
301 ad799x_threshold_addresses[number][direction],
303 mutex_unlock(&indio_dev->mlock);
311 static ssize_t ad799x_read_channel_config(struct device *dev,
312 struct device_attribute *attr,
315 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
316 struct ad799x_state *st = iio_priv(indio_dev);
317 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
321 ret = ad799x_i2c_read16(st, this_attr->address, &val);
325 return sprintf(buf, "%d\n", val);
328 static ssize_t ad799x_write_channel_config(struct device *dev,
329 struct device_attribute *attr,
333 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
334 struct ad799x_state *st = iio_priv(indio_dev);
335 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
340 ret = strict_strtol(buf, 10, &val);
344 mutex_lock(&indio_dev->mlock);
345 ret = ad799x_i2c_write16(st, this_attr->address, val);
346 mutex_unlock(&indio_dev->mlock);
348 return ret ? ret : len;
351 static irqreturn_t ad799x_event_handler(int irq, void *private)
353 struct iio_dev *indio_dev = private;
354 struct ad799x_state *st = iio_priv(private);
358 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
365 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
367 for (i = 0; i < 8; i++) {
368 if (status & (1 << i))
369 iio_push_event(indio_dev,
371 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
375 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
386 static IIO_DEVICE_ATTR(in_voltage0_thresh_both_hyst_raw,
388 ad799x_read_channel_config,
389 ad799x_write_channel_config,
390 AD7998_HYST_CH1_REG);
392 static IIO_DEVICE_ATTR(in_voltage1_thresh_both_hyst_raw,
394 ad799x_read_channel_config,
395 ad799x_write_channel_config,
396 AD7998_HYST_CH2_REG);
398 static IIO_DEVICE_ATTR(in_voltage2_thresh_both_hyst_raw,
400 ad799x_read_channel_config,
401 ad799x_write_channel_config,
402 AD7998_HYST_CH3_REG);
404 static IIO_DEVICE_ATTR(in_voltage3_thresh_both_hyst_raw,
406 ad799x_read_channel_config,
407 ad799x_write_channel_config,
408 AD7998_HYST_CH4_REG);
410 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
411 ad799x_read_frequency,
412 ad799x_write_frequency);
413 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
415 static struct attribute *ad7993_4_7_8_event_attributes[] = {
416 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
417 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
418 &iio_dev_attr_in_voltage2_thresh_both_hyst_raw.dev_attr.attr,
419 &iio_dev_attr_in_voltage3_thresh_both_hyst_raw.dev_attr.attr,
420 &iio_dev_attr_sampling_frequency.dev_attr.attr,
421 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
425 static struct attribute_group ad7993_4_7_8_event_attrs_group = {
426 .attrs = ad7993_4_7_8_event_attributes,
430 static struct attribute *ad7992_event_attributes[] = {
431 &iio_dev_attr_in_voltage0_thresh_both_hyst_raw.dev_attr.attr,
432 &iio_dev_attr_in_voltage1_thresh_both_hyst_raw.dev_attr.attr,
433 &iio_dev_attr_sampling_frequency.dev_attr.attr,
434 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
438 static struct attribute_group ad7992_event_attrs_group = {
439 .attrs = ad7992_event_attributes,
443 static const struct iio_info ad7991_info = {
444 .read_raw = &ad799x_read_raw,
445 .driver_module = THIS_MODULE,
448 static const struct iio_info ad7992_info = {
449 .read_raw = &ad799x_read_raw,
450 .event_attrs = &ad7992_event_attrs_group,
451 .read_event_config = &ad799x_read_event_config,
452 .read_event_value = &ad799x_read_event_value,
453 .write_event_value = &ad799x_write_event_value,
454 .driver_module = THIS_MODULE,
457 static const struct iio_info ad7993_4_7_8_info = {
458 .read_raw = &ad799x_read_raw,
459 .event_attrs = &ad7993_4_7_8_event_attrs_group,
460 .read_event_config = &ad799x_read_event_config,
461 .read_event_value = &ad799x_read_event_value,
462 .write_event_value = &ad799x_write_event_value,
463 .driver_module = THIS_MODULE,
464 .update_scan_mode = ad7997_8_update_scan_mode,
467 #define AD799X_EV_MASK (IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) | \
468 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING))
470 #define AD799X_CHANNEL(_index, _realbits, _evmask) { \
471 .type = IIO_VOLTAGE, \
473 .channel = (_index), \
474 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
475 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
476 .scan_index = (_index), \
477 .scan_type = IIO_ST('u', _realbits, 16, 12 - (_realbits)), \
478 .event_mask = (_evmask), \
481 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
484 AD799X_CHANNEL(0, 12, 0),
485 AD799X_CHANNEL(1, 12, 0),
486 AD799X_CHANNEL(2, 12, 0),
487 AD799X_CHANNEL(3, 12, 0),
488 IIO_CHAN_SOFT_TIMESTAMP(4),
491 .info = &ad7991_info,
495 AD799X_CHANNEL(0, 10, 0),
496 AD799X_CHANNEL(1, 10, 0),
497 AD799X_CHANNEL(2, 10, 0),
498 AD799X_CHANNEL(3, 10, 0),
499 IIO_CHAN_SOFT_TIMESTAMP(4),
502 .info = &ad7991_info,
506 AD799X_CHANNEL(0, 8, 0),
507 AD799X_CHANNEL(1, 8, 0),
508 AD799X_CHANNEL(2, 8, 0),
509 AD799X_CHANNEL(3, 8, 0),
510 IIO_CHAN_SOFT_TIMESTAMP(4),
513 .info = &ad7991_info,
517 AD799X_CHANNEL(0, 12, AD799X_EV_MASK),
518 AD799X_CHANNEL(1, 12, AD799X_EV_MASK),
519 IIO_CHAN_SOFT_TIMESTAMP(3),
522 .default_config = AD7998_ALERT_EN,
523 .info = &ad7992_info,
527 AD799X_CHANNEL(0, 10, AD799X_EV_MASK),
528 AD799X_CHANNEL(1, 10, AD799X_EV_MASK),
529 AD799X_CHANNEL(2, 10, AD799X_EV_MASK),
530 AD799X_CHANNEL(3, 10, AD799X_EV_MASK),
531 IIO_CHAN_SOFT_TIMESTAMP(4),
534 .default_config = AD7998_ALERT_EN,
535 .info = &ad7993_4_7_8_info,
539 AD799X_CHANNEL(0, 12, AD799X_EV_MASK),
540 AD799X_CHANNEL(1, 12, AD799X_EV_MASK),
541 AD799X_CHANNEL(2, 12, AD799X_EV_MASK),
542 AD799X_CHANNEL(3, 12, AD799X_EV_MASK),
543 IIO_CHAN_SOFT_TIMESTAMP(4),
546 .default_config = AD7998_ALERT_EN,
547 .info = &ad7993_4_7_8_info,
551 AD799X_CHANNEL(0, 10, AD799X_EV_MASK),
552 AD799X_CHANNEL(1, 10, AD799X_EV_MASK),
553 AD799X_CHANNEL(2, 10, AD799X_EV_MASK),
554 AD799X_CHANNEL(3, 10, AD799X_EV_MASK),
555 AD799X_CHANNEL(4, 10, 0),
556 AD799X_CHANNEL(5, 10, 0),
557 AD799X_CHANNEL(6, 10, 0),
558 AD799X_CHANNEL(7, 10, 0),
559 IIO_CHAN_SOFT_TIMESTAMP(8),
562 .default_config = AD7998_ALERT_EN,
563 .info = &ad7993_4_7_8_info,
567 AD799X_CHANNEL(0, 12, AD799X_EV_MASK),
568 AD799X_CHANNEL(1, 12, AD799X_EV_MASK),
569 AD799X_CHANNEL(2, 12, AD799X_EV_MASK),
570 AD799X_CHANNEL(3, 12, AD799X_EV_MASK),
571 AD799X_CHANNEL(4, 12, 0),
572 AD799X_CHANNEL(5, 12, 0),
573 AD799X_CHANNEL(6, 12, 0),
574 AD799X_CHANNEL(7, 12, 0),
575 IIO_CHAN_SOFT_TIMESTAMP(8),
578 .default_config = AD7998_ALERT_EN,
579 .info = &ad7993_4_7_8_info,
583 static int ad799x_probe(struct i2c_client *client,
584 const struct i2c_device_id *id)
587 struct ad799x_platform_data *pdata = client->dev.platform_data;
588 struct ad799x_state *st;
589 struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
591 if (indio_dev == NULL)
594 st = iio_priv(indio_dev);
595 /* this is only used for device removal purposes */
596 i2c_set_clientdata(client, indio_dev);
598 st->id = id->driver_data;
599 st->chip_info = &ad799x_chip_info_tbl[st->id];
600 st->config = st->chip_info->default_config;
602 /* TODO: Add pdata options for filtering and bit delay */
607 st->int_vref_mv = pdata->vref_mv;
609 st->reg = regulator_get(&client->dev, "vcc");
610 if (!IS_ERR(st->reg)) {
611 ret = regulator_enable(st->reg);
617 indio_dev->dev.parent = &client->dev;
618 indio_dev->name = id->name;
619 indio_dev->info = st->chip_info->info;
621 indio_dev->modes = INDIO_DIRECT_MODE;
622 indio_dev->channels = st->chip_info->channel;
623 indio_dev->num_channels = st->chip_info->num_channels;
625 ret = ad799x_register_ring_funcs_and_init(indio_dev);
627 goto error_disable_reg;
629 if (client->irq > 0) {
630 ret = request_threaded_irq(client->irq,
632 ad799x_event_handler,
633 IRQF_TRIGGER_FALLING |
638 goto error_cleanup_ring;
640 ret = iio_device_register(indio_dev);
648 free_irq(client->irq, indio_dev);
650 ad799x_ring_cleanup(indio_dev);
652 if (!IS_ERR(st->reg))
653 regulator_disable(st->reg);
655 if (!IS_ERR(st->reg))
656 regulator_put(st->reg);
657 iio_device_free(indio_dev);
662 static int ad799x_remove(struct i2c_client *client)
664 struct iio_dev *indio_dev = i2c_get_clientdata(client);
665 struct ad799x_state *st = iio_priv(indio_dev);
667 iio_device_unregister(indio_dev);
669 free_irq(client->irq, indio_dev);
671 ad799x_ring_cleanup(indio_dev);
672 if (!IS_ERR(st->reg)) {
673 regulator_disable(st->reg);
674 regulator_put(st->reg);
677 iio_device_free(indio_dev);
682 static const struct i2c_device_id ad799x_id[] = {
683 { "ad7991", ad7991 },
684 { "ad7995", ad7995 },
685 { "ad7999", ad7999 },
686 { "ad7992", ad7992 },
687 { "ad7993", ad7993 },
688 { "ad7994", ad7994 },
689 { "ad7997", ad7997 },
690 { "ad7998", ad7998 },
694 MODULE_DEVICE_TABLE(i2c, ad799x_id);
696 static struct i2c_driver ad799x_driver = {
700 .probe = ad799x_probe,
701 .remove = ad799x_remove,
702 .id_table = ad799x_id,
704 module_i2c_driver(ad799x_driver);
706 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
707 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
708 MODULE_LICENSE("GPL v2");