3 * Copyright (C) 2010-2011 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>
40 #include <linux/iio/trigger_consumer.h>
41 #include <linux/iio/triggered_buffer.h>
43 #define AD799X_CHANNEL_SHIFT 4
44 #define AD799X_STORAGEBITS 16
46 * AD7991, AD7995 and AD7999 defines
49 #define AD7991_REF_SEL 0x08
50 #define AD7991_FLTR 0x04
51 #define AD7991_BIT_TRIAL_DELAY 0x02
52 #define AD7991_SAMPLE_DELAY 0x01
55 * AD7992, AD7993, AD7994, AD7997 and AD7998 defines
58 #define AD7998_FLTR 0x08
59 #define AD7998_ALERT_EN 0x04
60 #define AD7998_BUSY_ALERT 0x02
61 #define AD7998_BUSY_ALERT_POL 0x01
63 #define AD7998_CONV_RES_REG 0x0
64 #define AD7998_ALERT_STAT_REG 0x1
65 #define AD7998_CONF_REG 0x2
66 #define AD7998_CYCLE_TMR_REG 0x3
68 #define AD7998_DATALOW_REG(x) ((x) * 3 + 0x4)
69 #define AD7998_DATAHIGH_REG(x) ((x) * 3 + 0x5)
70 #define AD7998_HYST_REG(x) ((x) * 3 + 0x6)
72 #define AD7998_CYC_MASK 0x7
73 #define AD7998_CYC_DIS 0x0
74 #define AD7998_CYC_TCONF_32 0x1
75 #define AD7998_CYC_TCONF_64 0x2
76 #define AD7998_CYC_TCONF_128 0x3
77 #define AD7998_CYC_TCONF_256 0x4
78 #define AD7998_CYC_TCONF_512 0x5
79 #define AD7998_CYC_TCONF_1024 0x6
80 #define AD7998_CYC_TCONF_2048 0x7
82 #define AD7998_ALERT_STAT_CLEAR 0xFF
85 * AD7997 and AD7997 defines
88 #define AD7997_8_READ_SINGLE 0x80
89 #define AD7997_8_READ_SEQUENCE 0x70
90 /* TODO: move this into a common header */
91 #define RES_MASK(bits) ((1 << (bits)) - 1)
105 * struct ad799x_chip_info - chip specific information
106 * @channel: channel specification
107 * @num_channels: number of channels
108 * @default_config: device default configuration
109 * @info: pointer to iio_info struct
111 struct ad799x_chip_info {
112 struct iio_chan_spec channel[9];
115 const struct iio_info *info;
118 struct ad799x_state {
119 struct i2c_client *client;
120 const struct ad799x_chip_info *chip_info;
121 struct regulator *reg;
122 struct regulator *vref;
127 unsigned int transfer_size;
131 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
133 * Currently there is no option in this driver to disable the saving of
134 * timestamps within the ring.
136 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
138 struct iio_poll_func *pf = p;
139 struct iio_dev *indio_dev = pf->indio_dev;
140 struct ad799x_state *st = iio_priv(indio_dev);
149 (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
154 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
159 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
165 b_sent = i2c_smbus_read_i2c_block_data(st->client,
166 cmd, st->transfer_size, st->rx_buf);
170 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
173 iio_trigger_notify_done(indio_dev->trig);
179 * ad799x register access by I2C
181 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
183 struct i2c_client *client = st->client;
186 ret = i2c_smbus_read_word_swapped(client, reg);
188 dev_err(&client->dev, "I2C read error\n");
197 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
199 struct i2c_client *client = st->client;
202 ret = i2c_smbus_read_byte_data(client, reg);
204 dev_err(&client->dev, "I2C read error\n");
213 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
215 struct i2c_client *client = st->client;
218 ret = i2c_smbus_write_word_swapped(client, reg, data);
220 dev_err(&client->dev, "I2C write error\n");
225 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
227 struct i2c_client *client = st->client;
230 ret = i2c_smbus_write_byte_data(client, reg, data);
232 dev_err(&client->dev, "I2C write error\n");
237 static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
238 const unsigned long *scan_mask)
240 struct ad799x_state *st = iio_priv(indio_dev);
243 st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
247 st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
252 return ad799x_i2c_write16(st, AD7998_CONF_REG,
253 st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
261 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
271 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
276 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
280 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
286 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
293 static int ad799x_read_raw(struct iio_dev *indio_dev,
294 struct iio_chan_spec const *chan,
300 struct ad799x_state *st = iio_priv(indio_dev);
303 case IIO_CHAN_INFO_RAW:
304 mutex_lock(&indio_dev->mlock);
305 if (iio_buffer_enabled(indio_dev))
308 ret = ad799x_scan_direct(st, chan->scan_index);
309 mutex_unlock(&indio_dev->mlock);
313 *val = (ret >> chan->scan_type.shift) &
314 RES_MASK(chan->scan_type.realbits);
316 case IIO_CHAN_INFO_SCALE:
317 ret = regulator_get_voltage(st->vref);
321 *val2 = chan->scan_type.realbits;
322 return IIO_VAL_FRACTIONAL_LOG2;
326 static const unsigned int ad7998_frequencies[] = {
327 [AD7998_CYC_DIS] = 0,
328 [AD7998_CYC_TCONF_32] = 15625,
329 [AD7998_CYC_TCONF_64] = 7812,
330 [AD7998_CYC_TCONF_128] = 3906,
331 [AD7998_CYC_TCONF_512] = 976,
332 [AD7998_CYC_TCONF_1024] = 488,
333 [AD7998_CYC_TCONF_2048] = 244,
335 static ssize_t ad799x_read_frequency(struct device *dev,
336 struct device_attribute *attr,
339 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
340 struct ad799x_state *st = iio_priv(indio_dev);
344 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
348 val &= AD7998_CYC_MASK;
350 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
353 static ssize_t ad799x_write_frequency(struct device *dev,
354 struct device_attribute *attr,
358 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
359 struct ad799x_state *st = iio_priv(indio_dev);
365 ret = kstrtol(buf, 10, &val);
369 mutex_lock(&indio_dev->mlock);
370 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
372 goto error_ret_mutex;
373 /* Wipe the bits clean */
374 t &= ~AD7998_CYC_MASK;
376 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
377 if (val == ad7998_frequencies[i])
379 if (i == ARRAY_SIZE(ad7998_frequencies)) {
381 goto error_ret_mutex;
384 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
387 mutex_unlock(&indio_dev->mlock);
389 return ret ? ret : len;
392 static int ad799x_read_event_config(struct iio_dev *indio_dev,
393 const struct iio_chan_spec *chan,
394 enum iio_event_type type,
395 enum iio_event_direction dir)
400 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
401 enum iio_event_direction dir,
402 enum iio_event_info info)
405 case IIO_EV_INFO_VALUE:
406 if (dir == IIO_EV_DIR_FALLING)
407 return AD7998_DATALOW_REG(chan->channel);
409 return AD7998_DATAHIGH_REG(chan->channel);
410 case IIO_EV_INFO_HYSTERESIS:
411 return AD7998_HYST_REG(chan->channel);
419 static int ad799x_write_event_value(struct iio_dev *indio_dev,
420 const struct iio_chan_spec *chan,
421 enum iio_event_type type,
422 enum iio_event_direction dir,
423 enum iio_event_info info,
427 struct ad799x_state *st = iio_priv(indio_dev);
429 if (val < 0 || val > RES_MASK(chan->scan_type.realbits))
432 mutex_lock(&indio_dev->mlock);
433 ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info),
434 val << chan->scan_type.shift);
435 mutex_unlock(&indio_dev->mlock);
440 static int ad799x_read_event_value(struct iio_dev *indio_dev,
441 const struct iio_chan_spec *chan,
442 enum iio_event_type type,
443 enum iio_event_direction dir,
444 enum iio_event_info info,
448 struct ad799x_state *st = iio_priv(indio_dev);
451 mutex_lock(&indio_dev->mlock);
452 ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info),
454 mutex_unlock(&indio_dev->mlock);
457 *val = (valin >> chan->scan_type.shift) &
458 RES_MASK(chan->scan_type.realbits);
463 static irqreturn_t ad799x_event_handler(int irq, void *private)
465 struct iio_dev *indio_dev = private;
466 struct ad799x_state *st = iio_priv(private);
470 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
477 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
479 for (i = 0; i < 8; i++) {
480 if (status & (1 << i))
481 iio_push_event(indio_dev,
483 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
487 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
498 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
499 ad799x_read_frequency,
500 ad799x_write_frequency);
501 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
503 static struct attribute *ad799x_event_attributes[] = {
504 &iio_dev_attr_sampling_frequency.dev_attr.attr,
505 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
509 static struct attribute_group ad799x_event_attrs_group = {
510 .attrs = ad799x_event_attributes,
514 static const struct iio_info ad7991_info = {
515 .read_raw = &ad799x_read_raw,
516 .driver_module = THIS_MODULE,
519 static const struct iio_info ad7993_4_7_8_info = {
520 .read_raw = &ad799x_read_raw,
521 .event_attrs = &ad799x_event_attrs_group,
522 .read_event_config = &ad799x_read_event_config,
523 .read_event_value = &ad799x_read_event_value,
524 .write_event_value = &ad799x_write_event_value,
525 .driver_module = THIS_MODULE,
526 .update_scan_mode = ad7997_8_update_scan_mode,
529 static const struct iio_event_spec ad799x_events[] = {
531 .type = IIO_EV_TYPE_THRESH,
532 .dir = IIO_EV_DIR_RISING,
533 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
534 BIT(IIO_EV_INFO_ENABLE),
536 .type = IIO_EV_TYPE_THRESH,
537 .dir = IIO_EV_DIR_FALLING,
538 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
539 BIT(IIO_EV_INFO_ENABLE),
541 .type = IIO_EV_TYPE_THRESH,
542 .dir = IIO_EV_DIR_EITHER,
543 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
547 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
548 .type = IIO_VOLTAGE, \
550 .channel = (_index), \
551 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
552 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
553 .scan_index = (_index), \
556 .realbits = (_realbits), \
558 .shift = 12 - (_realbits), \
559 .endianness = IIO_BE, \
561 .event_spec = _ev_spec, \
562 .num_event_specs = _num_ev_spec, \
565 #define AD799X_CHANNEL(_index, _realbits) \
566 _AD799X_CHANNEL(_index, _realbits, NULL, 0)
568 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
569 _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
570 ARRAY_SIZE(ad799x_events))
572 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
575 AD799X_CHANNEL(0, 12),
576 AD799X_CHANNEL(1, 12),
577 AD799X_CHANNEL(2, 12),
578 AD799X_CHANNEL(3, 12),
579 IIO_CHAN_SOFT_TIMESTAMP(4),
582 .info = &ad7991_info,
586 AD799X_CHANNEL(0, 10),
587 AD799X_CHANNEL(1, 10),
588 AD799X_CHANNEL(2, 10),
589 AD799X_CHANNEL(3, 10),
590 IIO_CHAN_SOFT_TIMESTAMP(4),
593 .info = &ad7991_info,
597 AD799X_CHANNEL(0, 8),
598 AD799X_CHANNEL(1, 8),
599 AD799X_CHANNEL(2, 8),
600 AD799X_CHANNEL(3, 8),
601 IIO_CHAN_SOFT_TIMESTAMP(4),
604 .info = &ad7991_info,
608 AD799X_CHANNEL_WITH_EVENTS(0, 12),
609 AD799X_CHANNEL_WITH_EVENTS(1, 12),
610 IIO_CHAN_SOFT_TIMESTAMP(3),
613 .default_config = AD7998_ALERT_EN,
614 .info = &ad7993_4_7_8_info,
618 AD799X_CHANNEL_WITH_EVENTS(0, 10),
619 AD799X_CHANNEL_WITH_EVENTS(1, 10),
620 AD799X_CHANNEL_WITH_EVENTS(2, 10),
621 AD799X_CHANNEL_WITH_EVENTS(3, 10),
622 IIO_CHAN_SOFT_TIMESTAMP(4),
625 .default_config = AD7998_ALERT_EN,
626 .info = &ad7993_4_7_8_info,
630 AD799X_CHANNEL_WITH_EVENTS(0, 12),
631 AD799X_CHANNEL_WITH_EVENTS(1, 12),
632 AD799X_CHANNEL_WITH_EVENTS(2, 12),
633 AD799X_CHANNEL_WITH_EVENTS(3, 12),
634 IIO_CHAN_SOFT_TIMESTAMP(4),
637 .default_config = AD7998_ALERT_EN,
638 .info = &ad7993_4_7_8_info,
642 AD799X_CHANNEL_WITH_EVENTS(0, 10),
643 AD799X_CHANNEL_WITH_EVENTS(1, 10),
644 AD799X_CHANNEL_WITH_EVENTS(2, 10),
645 AD799X_CHANNEL_WITH_EVENTS(3, 10),
646 AD799X_CHANNEL(4, 10),
647 AD799X_CHANNEL(5, 10),
648 AD799X_CHANNEL(6, 10),
649 AD799X_CHANNEL(7, 10),
650 IIO_CHAN_SOFT_TIMESTAMP(8),
653 .default_config = AD7998_ALERT_EN,
654 .info = &ad7993_4_7_8_info,
658 AD799X_CHANNEL_WITH_EVENTS(0, 12),
659 AD799X_CHANNEL_WITH_EVENTS(1, 12),
660 AD799X_CHANNEL_WITH_EVENTS(2, 12),
661 AD799X_CHANNEL_WITH_EVENTS(3, 12),
662 AD799X_CHANNEL(4, 12),
663 AD799X_CHANNEL(5, 12),
664 AD799X_CHANNEL(6, 12),
665 AD799X_CHANNEL(7, 12),
666 IIO_CHAN_SOFT_TIMESTAMP(8),
669 .default_config = AD7998_ALERT_EN,
670 .info = &ad7993_4_7_8_info,
674 static int ad799x_probe(struct i2c_client *client,
675 const struct i2c_device_id *id)
678 struct ad799x_state *st;
679 struct iio_dev *indio_dev;
681 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
682 if (indio_dev == NULL)
685 st = iio_priv(indio_dev);
686 /* this is only used for device removal purposes */
687 i2c_set_clientdata(client, indio_dev);
689 st->id = id->driver_data;
690 st->chip_info = &ad799x_chip_info_tbl[st->id];
691 st->config = st->chip_info->default_config;
693 /* TODO: Add pdata options for filtering and bit delay */
695 st->reg = devm_regulator_get(&client->dev, "vcc");
697 return PTR_ERR(st->reg);
698 ret = regulator_enable(st->reg);
701 st->vref = devm_regulator_get(&client->dev, "vref");
702 if (IS_ERR(st->vref)) {
703 ret = PTR_ERR(st->vref);
704 goto error_disable_reg;
706 ret = regulator_enable(st->vref);
708 goto error_disable_reg;
712 indio_dev->dev.parent = &client->dev;
713 indio_dev->name = id->name;
714 indio_dev->info = st->chip_info->info;
716 indio_dev->modes = INDIO_DIRECT_MODE;
717 indio_dev->channels = st->chip_info->channel;
718 indio_dev->num_channels = st->chip_info->num_channels;
720 ret = iio_triggered_buffer_setup(indio_dev, NULL,
721 &ad799x_trigger_handler, NULL);
723 goto error_disable_vref;
725 if (client->irq > 0) {
726 ret = devm_request_threaded_irq(&client->dev,
729 ad799x_event_handler,
730 IRQF_TRIGGER_FALLING |
735 goto error_cleanup_ring;
737 ret = iio_device_register(indio_dev);
739 goto error_cleanup_ring;
744 iio_triggered_buffer_cleanup(indio_dev);
746 regulator_disable(st->vref);
748 regulator_disable(st->reg);
753 static int ad799x_remove(struct i2c_client *client)
755 struct iio_dev *indio_dev = i2c_get_clientdata(client);
756 struct ad799x_state *st = iio_priv(indio_dev);
758 iio_device_unregister(indio_dev);
760 iio_triggered_buffer_cleanup(indio_dev);
761 regulator_disable(st->vref);
762 regulator_disable(st->reg);
768 static const struct i2c_device_id ad799x_id[] = {
769 { "ad7991", ad7991 },
770 { "ad7995", ad7995 },
771 { "ad7999", ad7999 },
772 { "ad7992", ad7992 },
773 { "ad7993", ad7993 },
774 { "ad7994", ad7994 },
775 { "ad7997", ad7997 },
776 { "ad7998", ad7998 },
780 MODULE_DEVICE_TABLE(i2c, ad799x_id);
782 static struct i2c_driver ad799x_driver = {
786 .probe = ad799x_probe,
787 .remove = ad799x_remove,
788 .id_table = ad799x_id,
790 module_i2c_driver(ad799x_driver);
792 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
793 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
794 MODULE_LICENSE("GPL v2");