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 * @monitor_mode: whether the chip supports monitor interrupts
109 * @default_config: device default configuration
110 * @event_attrs: pointer to the monitor event attribute group
112 struct ad799x_chip_info {
113 struct iio_chan_spec channel[9];
116 const struct iio_info *info;
119 struct ad799x_state {
120 struct i2c_client *client;
121 const struct ad799x_chip_info *chip_info;
122 struct regulator *reg;
123 struct regulator *vref;
128 unsigned int transfer_size;
132 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
134 * Currently there is no option in this driver to disable the saving of
135 * timestamps within the ring.
137 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
139 struct iio_poll_func *pf = p;
140 struct iio_dev *indio_dev = pf->indio_dev;
141 struct ad799x_state *st = iio_priv(indio_dev);
150 (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
155 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
160 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
166 b_sent = i2c_smbus_read_i2c_block_data(st->client,
167 cmd, st->transfer_size, st->rx_buf);
171 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
174 iio_trigger_notify_done(indio_dev->trig);
180 * ad799x register access by I2C
182 static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data)
184 struct i2c_client *client = st->client;
187 ret = i2c_smbus_read_word_swapped(client, reg);
189 dev_err(&client->dev, "I2C read error\n");
198 static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data)
200 struct i2c_client *client = st->client;
203 ret = i2c_smbus_read_byte_data(client, reg);
205 dev_err(&client->dev, "I2C read error\n");
214 static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data)
216 struct i2c_client *client = st->client;
219 ret = i2c_smbus_write_word_swapped(client, reg, data);
221 dev_err(&client->dev, "I2C write error\n");
226 static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data)
228 struct i2c_client *client = st->client;
231 ret = i2c_smbus_write_byte_data(client, reg, data);
233 dev_err(&client->dev, "I2C write error\n");
238 static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
239 const unsigned long *scan_mask)
241 struct ad799x_state *st = iio_priv(indio_dev);
244 st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
248 st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
253 return ad799x_i2c_write16(st, AD7998_CONF_REG,
254 st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
262 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
272 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
277 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
281 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
287 ret = ad799x_i2c_read16(st, cmd, &rxbuf);
294 static int ad799x_read_raw(struct iio_dev *indio_dev,
295 struct iio_chan_spec const *chan,
301 struct ad799x_state *st = iio_priv(indio_dev);
304 case IIO_CHAN_INFO_RAW:
305 mutex_lock(&indio_dev->mlock);
306 if (iio_buffer_enabled(indio_dev))
309 ret = ad799x_scan_direct(st, chan->scan_index);
310 mutex_unlock(&indio_dev->mlock);
314 *val = (ret >> chan->scan_type.shift) &
315 RES_MASK(chan->scan_type.realbits);
317 case IIO_CHAN_INFO_SCALE:
318 ret = regulator_get_voltage(st->vref);
322 *val2 = chan->scan_type.realbits;
323 return IIO_VAL_FRACTIONAL_LOG2;
327 static const unsigned int ad7998_frequencies[] = {
328 [AD7998_CYC_DIS] = 0,
329 [AD7998_CYC_TCONF_32] = 15625,
330 [AD7998_CYC_TCONF_64] = 7812,
331 [AD7998_CYC_TCONF_128] = 3906,
332 [AD7998_CYC_TCONF_512] = 976,
333 [AD7998_CYC_TCONF_1024] = 488,
334 [AD7998_CYC_TCONF_2048] = 244,
336 static ssize_t ad799x_read_frequency(struct device *dev,
337 struct device_attribute *attr,
340 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
341 struct ad799x_state *st = iio_priv(indio_dev);
345 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
349 val &= AD7998_CYC_MASK;
351 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
354 static ssize_t ad799x_write_frequency(struct device *dev,
355 struct device_attribute *attr,
359 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
360 struct ad799x_state *st = iio_priv(indio_dev);
366 ret = kstrtol(buf, 10, &val);
370 mutex_lock(&indio_dev->mlock);
371 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
373 goto error_ret_mutex;
374 /* Wipe the bits clean */
375 t &= ~AD7998_CYC_MASK;
377 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
378 if (val == ad7998_frequencies[i])
380 if (i == ARRAY_SIZE(ad7998_frequencies)) {
382 goto error_ret_mutex;
385 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
388 mutex_unlock(&indio_dev->mlock);
390 return ret ? ret : len;
393 static int ad799x_read_event_config(struct iio_dev *indio_dev,
394 const struct iio_chan_spec *chan,
395 enum iio_event_type type,
396 enum iio_event_direction dir)
401 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
402 enum iio_event_direction dir,
403 enum iio_event_info info)
406 case IIO_EV_INFO_VALUE:
407 if (dir == IIO_EV_DIR_FALLING)
408 return AD7998_DATALOW_REG(chan->channel);
410 return AD7998_DATAHIGH_REG(chan->channel);
411 case IIO_EV_INFO_HYSTERESIS:
412 return AD7998_HYST_REG(chan->channel);
420 static int ad799x_write_event_value(struct iio_dev *indio_dev,
421 const struct iio_chan_spec *chan,
422 enum iio_event_type type,
423 enum iio_event_direction dir,
424 enum iio_event_info info,
428 struct ad799x_state *st = iio_priv(indio_dev);
430 if (val < 0 || val > RES_MASK(chan->scan_type.realbits))
433 mutex_lock(&indio_dev->mlock);
434 ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info),
435 val << chan->scan_type.shift);
436 mutex_unlock(&indio_dev->mlock);
441 static int ad799x_read_event_value(struct iio_dev *indio_dev,
442 const struct iio_chan_spec *chan,
443 enum iio_event_type type,
444 enum iio_event_direction dir,
445 enum iio_event_info info,
449 struct ad799x_state *st = iio_priv(indio_dev);
452 mutex_lock(&indio_dev->mlock);
453 ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info),
455 mutex_unlock(&indio_dev->mlock);
458 *val = (valin >> chan->scan_type.shift) &
459 RES_MASK(chan->scan_type.realbits);
464 static irqreturn_t ad799x_event_handler(int irq, void *private)
466 struct iio_dev *indio_dev = private;
467 struct ad799x_state *st = iio_priv(private);
471 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
478 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
480 for (i = 0; i < 8; i++) {
481 if (status & (1 << i))
482 iio_push_event(indio_dev,
484 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
488 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
499 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
500 ad799x_read_frequency,
501 ad799x_write_frequency);
502 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
504 static struct attribute *ad799x_event_attributes[] = {
505 &iio_dev_attr_sampling_frequency.dev_attr.attr,
506 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
510 static struct attribute_group ad799x_event_attrs_group = {
511 .attrs = ad799x_event_attributes,
515 static const struct iio_info ad7991_info = {
516 .read_raw = &ad799x_read_raw,
517 .driver_module = THIS_MODULE,
520 static const struct iio_info ad7993_4_7_8_info = {
521 .read_raw = &ad799x_read_raw,
522 .event_attrs = &ad799x_event_attrs_group,
523 .read_event_config = &ad799x_read_event_config,
524 .read_event_value = &ad799x_read_event_value,
525 .write_event_value = &ad799x_write_event_value,
526 .driver_module = THIS_MODULE,
527 .update_scan_mode = ad7997_8_update_scan_mode,
530 static const struct iio_event_spec ad799x_events[] = {
532 .type = IIO_EV_TYPE_THRESH,
533 .dir = IIO_EV_DIR_RISING,
534 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
535 BIT(IIO_EV_INFO_ENABLE),
537 .type = IIO_EV_TYPE_THRESH,
538 .dir = IIO_EV_DIR_FALLING,
539 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
540 BIT(IIO_EV_INFO_ENABLE),
542 .type = IIO_EV_TYPE_THRESH,
543 .dir = IIO_EV_DIR_EITHER,
544 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
548 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
549 .type = IIO_VOLTAGE, \
551 .channel = (_index), \
552 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
553 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
554 .scan_index = (_index), \
557 .realbits = (_realbits), \
559 .shift = 12 - (_realbits), \
560 .endianness = IIO_BE, \
562 .event_spec = _ev_spec, \
563 .num_event_specs = _num_ev_spec, \
566 #define AD799X_CHANNEL(_index, _realbits) \
567 _AD799X_CHANNEL(_index, _realbits, NULL, 0)
569 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
570 _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
571 ARRAY_SIZE(ad799x_events))
573 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
576 AD799X_CHANNEL(0, 12),
577 AD799X_CHANNEL(1, 12),
578 AD799X_CHANNEL(2, 12),
579 AD799X_CHANNEL(3, 12),
580 IIO_CHAN_SOFT_TIMESTAMP(4),
583 .info = &ad7991_info,
587 AD799X_CHANNEL(0, 10),
588 AD799X_CHANNEL(1, 10),
589 AD799X_CHANNEL(2, 10),
590 AD799X_CHANNEL(3, 10),
591 IIO_CHAN_SOFT_TIMESTAMP(4),
594 .info = &ad7991_info,
598 AD799X_CHANNEL(0, 8),
599 AD799X_CHANNEL(1, 8),
600 AD799X_CHANNEL(2, 8),
601 AD799X_CHANNEL(3, 8),
602 IIO_CHAN_SOFT_TIMESTAMP(4),
605 .info = &ad7991_info,
609 AD799X_CHANNEL_WITH_EVENTS(0, 12),
610 AD799X_CHANNEL_WITH_EVENTS(1, 12),
611 IIO_CHAN_SOFT_TIMESTAMP(3),
614 .default_config = AD7998_ALERT_EN,
615 .info = &ad7993_4_7_8_info,
619 AD799X_CHANNEL_WITH_EVENTS(0, 10),
620 AD799X_CHANNEL_WITH_EVENTS(1, 10),
621 AD799X_CHANNEL_WITH_EVENTS(2, 10),
622 AD799X_CHANNEL_WITH_EVENTS(3, 10),
623 IIO_CHAN_SOFT_TIMESTAMP(4),
626 .default_config = AD7998_ALERT_EN,
627 .info = &ad7993_4_7_8_info,
631 AD799X_CHANNEL_WITH_EVENTS(0, 12),
632 AD799X_CHANNEL_WITH_EVENTS(1, 12),
633 AD799X_CHANNEL_WITH_EVENTS(2, 12),
634 AD799X_CHANNEL_WITH_EVENTS(3, 12),
635 IIO_CHAN_SOFT_TIMESTAMP(4),
638 .default_config = AD7998_ALERT_EN,
639 .info = &ad7993_4_7_8_info,
643 AD799X_CHANNEL_WITH_EVENTS(0, 10),
644 AD799X_CHANNEL_WITH_EVENTS(1, 10),
645 AD799X_CHANNEL_WITH_EVENTS(2, 10),
646 AD799X_CHANNEL_WITH_EVENTS(3, 10),
647 AD799X_CHANNEL(4, 10),
648 AD799X_CHANNEL(5, 10),
649 AD799X_CHANNEL(6, 10),
650 AD799X_CHANNEL(7, 10),
651 IIO_CHAN_SOFT_TIMESTAMP(8),
654 .default_config = AD7998_ALERT_EN,
655 .info = &ad7993_4_7_8_info,
659 AD799X_CHANNEL_WITH_EVENTS(0, 12),
660 AD799X_CHANNEL_WITH_EVENTS(1, 12),
661 AD799X_CHANNEL_WITH_EVENTS(2, 12),
662 AD799X_CHANNEL_WITH_EVENTS(3, 12),
663 AD799X_CHANNEL(4, 12),
664 AD799X_CHANNEL(5, 12),
665 AD799X_CHANNEL(6, 12),
666 AD799X_CHANNEL(7, 12),
667 IIO_CHAN_SOFT_TIMESTAMP(8),
670 .default_config = AD7998_ALERT_EN,
671 .info = &ad7993_4_7_8_info,
675 static int ad799x_probe(struct i2c_client *client,
676 const struct i2c_device_id *id)
679 struct ad799x_state *st;
680 struct iio_dev *indio_dev;
682 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
683 if (indio_dev == NULL)
686 st = iio_priv(indio_dev);
687 /* this is only used for device removal purposes */
688 i2c_set_clientdata(client, indio_dev);
690 st->id = id->driver_data;
691 st->chip_info = &ad799x_chip_info_tbl[st->id];
692 st->config = st->chip_info->default_config;
694 /* TODO: Add pdata options for filtering and bit delay */
696 st->reg = devm_regulator_get(&client->dev, "vcc");
698 return PTR_ERR(st->reg);
699 ret = regulator_enable(st->reg);
702 st->vref = devm_regulator_get(&client->dev, "vref");
703 if (IS_ERR(st->vref)) {
704 ret = PTR_ERR(st->vref);
705 goto error_disable_reg;
707 ret = regulator_enable(st->vref);
709 goto error_disable_reg;
713 indio_dev->dev.parent = &client->dev;
714 indio_dev->name = id->name;
715 indio_dev->info = st->chip_info->info;
717 indio_dev->modes = INDIO_DIRECT_MODE;
718 indio_dev->channels = st->chip_info->channel;
719 indio_dev->num_channels = st->chip_info->num_channels;
721 ret = iio_triggered_buffer_setup(indio_dev, NULL,
722 &ad799x_trigger_handler, NULL);
724 goto error_disable_vref;
726 if (client->irq > 0) {
727 ret = devm_request_threaded_irq(&client->dev,
730 ad799x_event_handler,
731 IRQF_TRIGGER_FALLING |
736 goto error_cleanup_ring;
738 ret = iio_device_register(indio_dev);
740 goto error_cleanup_ring;
745 iio_triggered_buffer_cleanup(indio_dev);
747 regulator_disable(st->vref);
749 regulator_disable(st->reg);
754 static int ad799x_remove(struct i2c_client *client)
756 struct iio_dev *indio_dev = i2c_get_clientdata(client);
757 struct ad799x_state *st = iio_priv(indio_dev);
759 iio_device_unregister(indio_dev);
761 iio_triggered_buffer_cleanup(indio_dev);
762 regulator_disable(st->vref);
763 regulator_disable(st->reg);
769 static const struct i2c_device_id ad799x_id[] = {
770 { "ad7991", ad7991 },
771 { "ad7995", ad7995 },
772 { "ad7999", ad7999 },
773 { "ad7992", ad7992 },
774 { "ad7993", ad7993 },
775 { "ad7994", ad7994 },
776 { "ad7997", ad7997 },
777 { "ad7998", ad7998 },
781 MODULE_DEVICE_TABLE(i2c, ad799x_id);
783 static struct i2c_driver ad799x_driver = {
787 .probe = ad799x_probe,
788 .remove = ad799x_remove,
789 .id_table = ad799x_id,
791 module_i2c_driver(ad799x_driver);
793 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
794 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
795 MODULE_LICENSE("GPL v2");