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);
168 case IIO_CHAN_INFO_RAW:
169 mutex_lock(&indio_dev->mlock);
170 if (iio_buffer_enabled(indio_dev))
173 ret = ad799x_scan_direct(st, chan->scan_index);
174 mutex_unlock(&indio_dev->mlock);
178 *val = (ret >> chan->scan_type.shift) &
179 RES_MASK(chan->scan_type.realbits);
181 case IIO_CHAN_INFO_SCALE:
182 *val = st->int_vref_mv;
183 *val2 = chan->scan_type.realbits;
184 return IIO_VAL_FRACTIONAL_LOG2;
188 static const unsigned int ad7998_frequencies[] = {
189 [AD7998_CYC_DIS] = 0,
190 [AD7998_CYC_TCONF_32] = 15625,
191 [AD7998_CYC_TCONF_64] = 7812,
192 [AD7998_CYC_TCONF_128] = 3906,
193 [AD7998_CYC_TCONF_512] = 976,
194 [AD7998_CYC_TCONF_1024] = 488,
195 [AD7998_CYC_TCONF_2048] = 244,
197 static ssize_t ad799x_read_frequency(struct device *dev,
198 struct device_attribute *attr,
201 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
202 struct ad799x_state *st = iio_priv(indio_dev);
206 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val);
210 val &= AD7998_CYC_MASK;
212 return sprintf(buf, "%u\n", ad7998_frequencies[val]);
215 static ssize_t ad799x_write_frequency(struct device *dev,
216 struct device_attribute *attr,
220 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
221 struct ad799x_state *st = iio_priv(indio_dev);
227 ret = kstrtol(buf, 10, &val);
231 mutex_lock(&indio_dev->mlock);
232 ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t);
234 goto error_ret_mutex;
235 /* Wipe the bits clean */
236 t &= ~AD7998_CYC_MASK;
238 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++)
239 if (val == ad7998_frequencies[i])
241 if (i == ARRAY_SIZE(ad7998_frequencies)) {
243 goto error_ret_mutex;
246 ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t);
249 mutex_unlock(&indio_dev->mlock);
251 return ret ? ret : len;
254 static int ad799x_read_event_config(struct iio_dev *indio_dev,
255 const struct iio_chan_spec *chan,
256 enum iio_event_type type,
257 enum iio_event_direction dir)
262 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan,
263 enum iio_event_direction dir,
264 enum iio_event_info info)
267 case IIO_EV_INFO_VALUE:
268 if (dir == IIO_EV_DIR_FALLING)
269 return AD7998_DATALOW_REG(chan->channel);
271 return AD7998_DATAHIGH_REG(chan->channel);
272 case IIO_EV_INFO_HYSTERESIS:
273 return AD7998_HYST_REG(chan->channel);
281 static int ad799x_write_event_value(struct iio_dev *indio_dev,
282 const struct iio_chan_spec *chan,
283 enum iio_event_type type,
284 enum iio_event_direction dir,
285 enum iio_event_info info,
289 struct ad799x_state *st = iio_priv(indio_dev);
291 mutex_lock(&indio_dev->mlock);
292 ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info),
294 mutex_unlock(&indio_dev->mlock);
299 static int ad799x_read_event_value(struct iio_dev *indio_dev,
300 const struct iio_chan_spec *chan,
301 enum iio_event_type type,
302 enum iio_event_direction dir,
303 enum iio_event_info info,
307 struct ad799x_state *st = iio_priv(indio_dev);
310 mutex_lock(&indio_dev->mlock);
311 ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info),
313 mutex_unlock(&indio_dev->mlock);
321 static irqreturn_t ad799x_event_handler(int irq, void *private)
323 struct iio_dev *indio_dev = private;
324 struct ad799x_state *st = iio_priv(private);
328 ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
335 ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
337 for (i = 0; i < 8; i++) {
338 if (status & (1 << i))
339 iio_push_event(indio_dev,
341 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
345 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
356 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
357 ad799x_read_frequency,
358 ad799x_write_frequency);
359 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
361 static struct attribute *ad799x_event_attributes[] = {
362 &iio_dev_attr_sampling_frequency.dev_attr.attr,
363 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
367 static struct attribute_group ad799x_event_attrs_group = {
368 .attrs = ad799x_event_attributes,
372 static const struct iio_info ad7991_info = {
373 .read_raw = &ad799x_read_raw,
374 .driver_module = THIS_MODULE,
377 static const struct iio_info ad7993_4_7_8_info = {
378 .read_raw = &ad799x_read_raw,
379 .event_attrs = &ad799x_event_attrs_group,
380 .read_event_config = &ad799x_read_event_config,
381 .read_event_value = &ad799x_read_event_value,
382 .write_event_value = &ad799x_write_event_value,
383 .driver_module = THIS_MODULE,
384 .update_scan_mode = ad7997_8_update_scan_mode,
387 static const struct iio_event_spec ad799x_events[] = {
389 .type = IIO_EV_TYPE_THRESH,
390 .dir = IIO_EV_DIR_RISING,
391 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
392 BIT(IIO_EV_INFO_ENABLE),
394 .type = IIO_EV_TYPE_THRESH,
395 .dir = IIO_EV_DIR_FALLING,
396 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
397 BIT(IIO_EV_INFO_ENABLE),
399 .type = IIO_EV_TYPE_THRESH,
400 .dir = IIO_EV_DIR_EITHER,
401 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
405 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \
406 .type = IIO_VOLTAGE, \
408 .channel = (_index), \
409 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
410 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
411 .scan_index = (_index), \
414 .realbits = (_realbits), \
416 .shift = 12 - (_realbits), \
417 .endianness = IIO_BE, \
419 .event_spec = _ev_spec, \
420 .num_event_specs = _num_ev_spec, \
423 #define AD799X_CHANNEL(_index, _realbits) \
424 _AD799X_CHANNEL(_index, _realbits, NULL, 0)
426 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
427 _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
428 ARRAY_SIZE(ad799x_events))
430 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
433 AD799X_CHANNEL(0, 12),
434 AD799X_CHANNEL(1, 12),
435 AD799X_CHANNEL(2, 12),
436 AD799X_CHANNEL(3, 12),
437 IIO_CHAN_SOFT_TIMESTAMP(4),
440 .info = &ad7991_info,
444 AD799X_CHANNEL(0, 10),
445 AD799X_CHANNEL(1, 10),
446 AD799X_CHANNEL(2, 10),
447 AD799X_CHANNEL(3, 10),
448 IIO_CHAN_SOFT_TIMESTAMP(4),
451 .info = &ad7991_info,
455 AD799X_CHANNEL(0, 8),
456 AD799X_CHANNEL(1, 8),
457 AD799X_CHANNEL(2, 8),
458 AD799X_CHANNEL(3, 8),
459 IIO_CHAN_SOFT_TIMESTAMP(4),
462 .info = &ad7991_info,
466 AD799X_CHANNEL_WITH_EVENTS(0, 12),
467 AD799X_CHANNEL_WITH_EVENTS(1, 12),
468 IIO_CHAN_SOFT_TIMESTAMP(3),
471 .default_config = AD7998_ALERT_EN,
472 .info = &ad7993_4_7_8_info,
476 AD799X_CHANNEL_WITH_EVENTS(0, 10),
477 AD799X_CHANNEL_WITH_EVENTS(1, 10),
478 AD799X_CHANNEL_WITH_EVENTS(2, 10),
479 AD799X_CHANNEL_WITH_EVENTS(3, 10),
480 IIO_CHAN_SOFT_TIMESTAMP(4),
483 .default_config = AD7998_ALERT_EN,
484 .info = &ad7993_4_7_8_info,
488 AD799X_CHANNEL_WITH_EVENTS(0, 12),
489 AD799X_CHANNEL_WITH_EVENTS(1, 12),
490 AD799X_CHANNEL_WITH_EVENTS(2, 12),
491 AD799X_CHANNEL_WITH_EVENTS(3, 12),
492 IIO_CHAN_SOFT_TIMESTAMP(4),
495 .default_config = AD7998_ALERT_EN,
496 .info = &ad7993_4_7_8_info,
500 AD799X_CHANNEL_WITH_EVENTS(0, 10),
501 AD799X_CHANNEL_WITH_EVENTS(1, 10),
502 AD799X_CHANNEL_WITH_EVENTS(2, 10),
503 AD799X_CHANNEL_WITH_EVENTS(3, 10),
504 AD799X_CHANNEL(4, 10),
505 AD799X_CHANNEL(5, 10),
506 AD799X_CHANNEL(6, 10),
507 AD799X_CHANNEL(7, 10),
508 IIO_CHAN_SOFT_TIMESTAMP(8),
511 .default_config = AD7998_ALERT_EN,
512 .info = &ad7993_4_7_8_info,
516 AD799X_CHANNEL_WITH_EVENTS(0, 12),
517 AD799X_CHANNEL_WITH_EVENTS(1, 12),
518 AD799X_CHANNEL_WITH_EVENTS(2, 12),
519 AD799X_CHANNEL_WITH_EVENTS(3, 12),
520 AD799X_CHANNEL(4, 12),
521 AD799X_CHANNEL(5, 12),
522 AD799X_CHANNEL(6, 12),
523 AD799X_CHANNEL(7, 12),
524 IIO_CHAN_SOFT_TIMESTAMP(8),
527 .default_config = AD7998_ALERT_EN,
528 .info = &ad7993_4_7_8_info,
532 static int ad799x_probe(struct i2c_client *client,
533 const struct i2c_device_id *id)
536 struct ad799x_platform_data *pdata = client->dev.platform_data;
537 struct ad799x_state *st;
538 struct iio_dev *indio_dev;
540 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
541 if (indio_dev == NULL)
544 st = iio_priv(indio_dev);
545 /* this is only used for device removal purposes */
546 i2c_set_clientdata(client, indio_dev);
548 st->id = id->driver_data;
549 st->chip_info = &ad799x_chip_info_tbl[st->id];
550 st->config = st->chip_info->default_config;
552 /* TODO: Add pdata options for filtering and bit delay */
557 st->int_vref_mv = pdata->vref_mv;
559 st->reg = devm_regulator_get(&client->dev, "vcc");
560 if (!IS_ERR(st->reg)) {
561 ret = regulator_enable(st->reg);
567 indio_dev->dev.parent = &client->dev;
568 indio_dev->name = id->name;
569 indio_dev->info = st->chip_info->info;
571 indio_dev->modes = INDIO_DIRECT_MODE;
572 indio_dev->channels = st->chip_info->channel;
573 indio_dev->num_channels = st->chip_info->num_channels;
575 ret = ad799x_register_ring_funcs_and_init(indio_dev);
577 goto error_disable_reg;
579 if (client->irq > 0) {
580 ret = request_threaded_irq(client->irq,
582 ad799x_event_handler,
583 IRQF_TRIGGER_FALLING |
588 goto error_cleanup_ring;
590 ret = iio_device_register(indio_dev);
598 free_irq(client->irq, indio_dev);
600 ad799x_ring_cleanup(indio_dev);
602 if (!IS_ERR(st->reg))
603 regulator_disable(st->reg);
608 static int ad799x_remove(struct i2c_client *client)
610 struct iio_dev *indio_dev = i2c_get_clientdata(client);
611 struct ad799x_state *st = iio_priv(indio_dev);
613 iio_device_unregister(indio_dev);
615 free_irq(client->irq, indio_dev);
617 ad799x_ring_cleanup(indio_dev);
618 if (!IS_ERR(st->reg))
619 regulator_disable(st->reg);
625 static const struct i2c_device_id ad799x_id[] = {
626 { "ad7991", ad7991 },
627 { "ad7995", ad7995 },
628 { "ad7999", ad7999 },
629 { "ad7992", ad7992 },
630 { "ad7993", ad7993 },
631 { "ad7994", ad7994 },
632 { "ad7997", ad7997 },
633 { "ad7998", ad7998 },
637 MODULE_DEVICE_TABLE(i2c, ad799x_id);
639 static struct i2c_driver ad799x_driver = {
643 .probe = ad799x_probe,
644 .remove = ad799x_remove,
645 .id_table = ad799x_id,
647 module_i2c_driver(ad799x_driver);
649 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
650 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
651 MODULE_LICENSE("GPL v2");