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);
178 static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev,
179 const unsigned long *scan_mask)
181 struct ad799x_state *st = iio_priv(indio_dev);
184 st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
188 st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2;
193 return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG,
194 st->config | (*scan_mask << AD799X_CHANNEL_SHIFT));
202 static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
210 cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
215 cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
219 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
225 return i2c_smbus_read_word_swapped(st->client, cmd);
228 static int ad799x_read_raw(struct iio_dev *indio_dev,
229 struct iio_chan_spec const *chan,
235 struct ad799x_state *st = iio_priv(indio_dev);
238 case IIO_CHAN_INFO_RAW:
239 mutex_lock(&indio_dev->mlock);
240 if (iio_buffer_enabled(indio_dev))
243 ret = ad799x_scan_direct(st, chan->scan_index);
244 mutex_unlock(&indio_dev->mlock);
248 *val = (ret >> chan->scan_type.shift) &
249 RES_MASK(chan->scan_type.realbits);
251 case IIO_CHAN_INFO_SCALE:
252 ret = regulator_get_voltage(st->vref);
256 *val2 = chan->scan_type.realbits;
257 return IIO_VAL_FRACTIONAL_LOG2;
261 static const unsigned int ad7998_frequencies[] = {
262 [AD7998_CYC_DIS] = 0,
263 [AD7998_CYC_TCONF_32] = 15625,
264 [AD7998_CYC_TCONF_64] = 7812,
265 [AD7998_CYC_TCONF_128] = 3906,
266 [AD7998_CYC_TCONF_512] = 976,
267 [AD7998_CYC_TCONF_1024] = 488,
268 [AD7998_CYC_TCONF_2048] = 244,
271 static ssize_t ad799x_read_frequency(struct device *dev,
272 struct device_attribute *attr,
275 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
276 struct ad799x_state *st = iio_priv(indio_dev);
278 int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
282 return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]);
285 static ssize_t ad799x_write_frequency(struct device *dev,
286 struct device_attribute *attr,
290 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
291 struct ad799x_state *st = iio_priv(indio_dev);
296 ret = kstrtol(buf, 10, &val);
300 mutex_lock(&indio_dev->mlock);
301 ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG);
303 goto error_ret_mutex;
304 /* Wipe the bits clean */
305 ret &= ~AD7998_CYC_MASK;
307 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
308 if (val == ad7998_frequencies[i])
310 if (i == ARRAY_SIZE(ad7998_frequencies)) {
312 goto error_ret_mutex;
315 ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG,
318 goto error_ret_mutex;
322 mutex_unlock(&indio_dev->mlock);
327 static int ad799x_read_event_config(struct iio_dev *indio_dev,
328 const struct iio_chan_spec *chan,
329 enum iio_event_type type,
330 enum iio_event_direction dir)
335 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
336 enum iio_event_direction dir,
337 enum iio_event_info info)
340 case IIO_EV_INFO_VALUE:
341 if (dir == IIO_EV_DIR_FALLING)
342 return AD7998_DATALOW_REG(chan->channel);
344 return AD7998_DATAHIGH_REG(chan->channel);
345 case IIO_EV_INFO_HYSTERESIS:
346 return AD7998_HYST_REG(chan->channel);
354 static int ad799x_write_event_value(struct iio_dev *indio_dev,
355 const struct iio_chan_spec *chan,
356 enum iio_event_type type,
357 enum iio_event_direction dir,
358 enum iio_event_info info,
362 struct ad799x_state *st = iio_priv(indio_dev);
364 if (val < 0 || val > RES_MASK(chan->scan_type.realbits))
367 mutex_lock(&indio_dev->mlock);
368 ret = i2c_smbus_write_word_swapped(st->client,
369 ad799x_threshold_reg(chan, dir, info),
370 val << chan->scan_type.shift);
371 mutex_unlock(&indio_dev->mlock);
376 static int ad799x_read_event_value(struct iio_dev *indio_dev,
377 const struct iio_chan_spec *chan,
378 enum iio_event_type type,
379 enum iio_event_direction dir,
380 enum iio_event_info info,
384 struct ad799x_state *st = iio_priv(indio_dev);
386 mutex_lock(&indio_dev->mlock);
387 ret = i2c_smbus_read_word_swapped(st->client,
388 ad799x_threshold_reg(chan, dir, info));
389 mutex_unlock(&indio_dev->mlock);
392 *val = (ret >> chan->scan_type.shift) &
393 RES_MASK(chan->scan_type.realbits);
398 static irqreturn_t ad799x_event_handler(int irq, void *private)
400 struct iio_dev *indio_dev = private;
401 struct ad799x_state *st = iio_priv(private);
404 ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG);
408 if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG,
409 AD7998_ALERT_STAT_CLEAR) < 0)
412 for (i = 0; i < 8; i++) {
414 iio_push_event(indio_dev,
416 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
420 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
431 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
432 ad799x_read_frequency,
433 ad799x_write_frequency);
434 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
436 static struct attribute *ad799x_event_attributes[] = {
437 &iio_dev_attr_sampling_frequency.dev_attr.attr,
438 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
442 static struct attribute_group ad799x_event_attrs_group = {
443 .attrs = ad799x_event_attributes,
447 static const struct iio_info ad7991_info = {
448 .read_raw = &ad799x_read_raw,
449 .driver_module = THIS_MODULE,
452 static const struct iio_info ad7993_4_7_8_info = {
453 .read_raw = &ad799x_read_raw,
454 .event_attrs = &ad799x_event_attrs_group,
455 .read_event_config = &ad799x_read_event_config,
456 .read_event_value = &ad799x_read_event_value,
457 .write_event_value = &ad799x_write_event_value,
458 .driver_module = THIS_MODULE,
459 .update_scan_mode = ad7997_8_update_scan_mode,
462 static const struct iio_event_spec ad799x_events[] = {
464 .type = IIO_EV_TYPE_THRESH,
465 .dir = IIO_EV_DIR_RISING,
466 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
467 BIT(IIO_EV_INFO_ENABLE),
469 .type = IIO_EV_TYPE_THRESH,
470 .dir = IIO_EV_DIR_FALLING,
471 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
472 BIT(IIO_EV_INFO_ENABLE),
474 .type = IIO_EV_TYPE_THRESH,
475 .dir = IIO_EV_DIR_EITHER,
476 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
480 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
481 .type = IIO_VOLTAGE, \
483 .channel = (_index), \
484 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
485 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
486 .scan_index = (_index), \
489 .realbits = (_realbits), \
491 .shift = 12 - (_realbits), \
492 .endianness = IIO_BE, \
494 .event_spec = _ev_spec, \
495 .num_event_specs = _num_ev_spec, \
498 #define AD799X_CHANNEL(_index, _realbits) \
499 _AD799X_CHANNEL(_index, _realbits, NULL, 0)
501 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
502 _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
503 ARRAY_SIZE(ad799x_events))
505 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
508 AD799X_CHANNEL(0, 12),
509 AD799X_CHANNEL(1, 12),
510 AD799X_CHANNEL(2, 12),
511 AD799X_CHANNEL(3, 12),
512 IIO_CHAN_SOFT_TIMESTAMP(4),
515 .info = &ad7991_info,
519 AD799X_CHANNEL(0, 10),
520 AD799X_CHANNEL(1, 10),
521 AD799X_CHANNEL(2, 10),
522 AD799X_CHANNEL(3, 10),
523 IIO_CHAN_SOFT_TIMESTAMP(4),
526 .info = &ad7991_info,
530 AD799X_CHANNEL(0, 8),
531 AD799X_CHANNEL(1, 8),
532 AD799X_CHANNEL(2, 8),
533 AD799X_CHANNEL(3, 8),
534 IIO_CHAN_SOFT_TIMESTAMP(4),
537 .info = &ad7991_info,
541 AD799X_CHANNEL_WITH_EVENTS(0, 12),
542 AD799X_CHANNEL_WITH_EVENTS(1, 12),
543 IIO_CHAN_SOFT_TIMESTAMP(3),
546 .default_config = AD7998_ALERT_EN,
547 .info = &ad7993_4_7_8_info,
551 AD799X_CHANNEL_WITH_EVENTS(0, 10),
552 AD799X_CHANNEL_WITH_EVENTS(1, 10),
553 AD799X_CHANNEL_WITH_EVENTS(2, 10),
554 AD799X_CHANNEL_WITH_EVENTS(3, 10),
555 IIO_CHAN_SOFT_TIMESTAMP(4),
558 .default_config = AD7998_ALERT_EN,
559 .info = &ad7993_4_7_8_info,
563 AD799X_CHANNEL_WITH_EVENTS(0, 12),
564 AD799X_CHANNEL_WITH_EVENTS(1, 12),
565 AD799X_CHANNEL_WITH_EVENTS(2, 12),
566 AD799X_CHANNEL_WITH_EVENTS(3, 12),
567 IIO_CHAN_SOFT_TIMESTAMP(4),
570 .default_config = AD7998_ALERT_EN,
571 .info = &ad7993_4_7_8_info,
575 AD799X_CHANNEL_WITH_EVENTS(0, 10),
576 AD799X_CHANNEL_WITH_EVENTS(1, 10),
577 AD799X_CHANNEL_WITH_EVENTS(2, 10),
578 AD799X_CHANNEL_WITH_EVENTS(3, 10),
579 AD799X_CHANNEL(4, 10),
580 AD799X_CHANNEL(5, 10),
581 AD799X_CHANNEL(6, 10),
582 AD799X_CHANNEL(7, 10),
583 IIO_CHAN_SOFT_TIMESTAMP(8),
586 .default_config = AD7998_ALERT_EN,
587 .info = &ad7993_4_7_8_info,
591 AD799X_CHANNEL_WITH_EVENTS(0, 12),
592 AD799X_CHANNEL_WITH_EVENTS(1, 12),
593 AD799X_CHANNEL_WITH_EVENTS(2, 12),
594 AD799X_CHANNEL_WITH_EVENTS(3, 12),
595 AD799X_CHANNEL(4, 12),
596 AD799X_CHANNEL(5, 12),
597 AD799X_CHANNEL(6, 12),
598 AD799X_CHANNEL(7, 12),
599 IIO_CHAN_SOFT_TIMESTAMP(8),
602 .default_config = AD7998_ALERT_EN,
603 .info = &ad7993_4_7_8_info,
607 static int ad799x_probe(struct i2c_client *client,
608 const struct i2c_device_id *id)
611 struct ad799x_state *st;
612 struct iio_dev *indio_dev;
614 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
615 if (indio_dev == NULL)
618 st = iio_priv(indio_dev);
619 /* this is only used for device removal purposes */
620 i2c_set_clientdata(client, indio_dev);
622 st->id = id->driver_data;
623 st->chip_info = &ad799x_chip_info_tbl[st->id];
624 st->config = st->chip_info->default_config;
626 /* TODO: Add pdata options for filtering and bit delay */
628 st->reg = devm_regulator_get(&client->dev, "vcc");
630 return PTR_ERR(st->reg);
631 ret = regulator_enable(st->reg);
634 st->vref = devm_regulator_get(&client->dev, "vref");
635 if (IS_ERR(st->vref)) {
636 ret = PTR_ERR(st->vref);
637 goto error_disable_reg;
639 ret = regulator_enable(st->vref);
641 goto error_disable_reg;
645 indio_dev->dev.parent = &client->dev;
646 indio_dev->name = id->name;
647 indio_dev->info = st->chip_info->info;
649 indio_dev->modes = INDIO_DIRECT_MODE;
650 indio_dev->channels = st->chip_info->channel;
651 indio_dev->num_channels = st->chip_info->num_channels;
653 ret = iio_triggered_buffer_setup(indio_dev, NULL,
654 &ad799x_trigger_handler, NULL);
656 goto error_disable_vref;
658 if (client->irq > 0) {
659 ret = devm_request_threaded_irq(&client->dev,
662 ad799x_event_handler,
663 IRQF_TRIGGER_FALLING |
668 goto error_cleanup_ring;
670 ret = iio_device_register(indio_dev);
672 goto error_cleanup_ring;
677 iio_triggered_buffer_cleanup(indio_dev);
679 regulator_disable(st->vref);
681 regulator_disable(st->reg);
686 static int ad799x_remove(struct i2c_client *client)
688 struct iio_dev *indio_dev = i2c_get_clientdata(client);
689 struct ad799x_state *st = iio_priv(indio_dev);
691 iio_device_unregister(indio_dev);
693 iio_triggered_buffer_cleanup(indio_dev);
694 regulator_disable(st->vref);
695 regulator_disable(st->reg);
701 static const struct i2c_device_id ad799x_id[] = {
702 { "ad7991", ad7991 },
703 { "ad7995", ad7995 },
704 { "ad7999", ad7999 },
705 { "ad7992", ad7992 },
706 { "ad7993", ad7993 },
707 { "ad7994", ad7994 },
708 { "ad7997", ad7997 },
709 { "ad7998", ad7998 },
713 MODULE_DEVICE_TABLE(i2c, ad799x_id);
715 static struct i2c_driver ad799x_driver = {
719 .probe = ad799x_probe,
720 .remove = ad799x_remove,
721 .id_table = ad799x_id,
723 module_i2c_driver(ad799x_driver);
725 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
726 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
727 MODULE_LICENSE("GPL v2");