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), \
412 .scan_type = IIO_ST('u', _realbits, 16, 12 - (_realbits)), \
413 .event_spec = _ev_spec, \
414 .num_event_specs = _num_ev_spec, \
417 #define AD799X_CHANNEL(_index, _realbits) \
418 _AD799X_CHANNEL(_index, _realbits, NULL, 0)
420 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \
421 _AD799X_CHANNEL(_index, _realbits, ad799x_events, \
422 ARRAY_SIZE(ad799x_events))
424 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
427 AD799X_CHANNEL(0, 12),
428 AD799X_CHANNEL(1, 12),
429 AD799X_CHANNEL(2, 12),
430 AD799X_CHANNEL(3, 12),
431 IIO_CHAN_SOFT_TIMESTAMP(4),
434 .info = &ad7991_info,
438 AD799X_CHANNEL(0, 10),
439 AD799X_CHANNEL(1, 10),
440 AD799X_CHANNEL(2, 10),
441 AD799X_CHANNEL(3, 10),
442 IIO_CHAN_SOFT_TIMESTAMP(4),
445 .info = &ad7991_info,
449 AD799X_CHANNEL(0, 8),
450 AD799X_CHANNEL(1, 8),
451 AD799X_CHANNEL(2, 8),
452 AD799X_CHANNEL(3, 8),
453 IIO_CHAN_SOFT_TIMESTAMP(4),
456 .info = &ad7991_info,
460 AD799X_CHANNEL_WITH_EVENTS(0, 12),
461 AD799X_CHANNEL_WITH_EVENTS(1, 12),
462 IIO_CHAN_SOFT_TIMESTAMP(3),
465 .default_config = AD7998_ALERT_EN,
466 .info = &ad7993_4_7_8_info,
470 AD799X_CHANNEL_WITH_EVENTS(0, 10),
471 AD799X_CHANNEL_WITH_EVENTS(1, 10),
472 AD799X_CHANNEL_WITH_EVENTS(2, 10),
473 AD799X_CHANNEL_WITH_EVENTS(3, 10),
474 IIO_CHAN_SOFT_TIMESTAMP(4),
477 .default_config = AD7998_ALERT_EN,
478 .info = &ad7993_4_7_8_info,
482 AD799X_CHANNEL_WITH_EVENTS(0, 12),
483 AD799X_CHANNEL_WITH_EVENTS(1, 12),
484 AD799X_CHANNEL_WITH_EVENTS(2, 12),
485 AD799X_CHANNEL_WITH_EVENTS(3, 12),
486 IIO_CHAN_SOFT_TIMESTAMP(4),
489 .default_config = AD7998_ALERT_EN,
490 .info = &ad7993_4_7_8_info,
494 AD799X_CHANNEL_WITH_EVENTS(0, 10),
495 AD799X_CHANNEL_WITH_EVENTS(1, 10),
496 AD799X_CHANNEL_WITH_EVENTS(2, 10),
497 AD799X_CHANNEL_WITH_EVENTS(3, 10),
498 AD799X_CHANNEL(4, 10),
499 AD799X_CHANNEL(5, 10),
500 AD799X_CHANNEL(6, 10),
501 AD799X_CHANNEL(7, 10),
502 IIO_CHAN_SOFT_TIMESTAMP(8),
505 .default_config = AD7998_ALERT_EN,
506 .info = &ad7993_4_7_8_info,
510 AD799X_CHANNEL_WITH_EVENTS(0, 12),
511 AD799X_CHANNEL_WITH_EVENTS(1, 12),
512 AD799X_CHANNEL_WITH_EVENTS(2, 12),
513 AD799X_CHANNEL_WITH_EVENTS(3, 12),
514 AD799X_CHANNEL(4, 12),
515 AD799X_CHANNEL(5, 12),
516 AD799X_CHANNEL(6, 12),
517 AD799X_CHANNEL(7, 12),
518 IIO_CHAN_SOFT_TIMESTAMP(8),
521 .default_config = AD7998_ALERT_EN,
522 .info = &ad7993_4_7_8_info,
526 static int ad799x_probe(struct i2c_client *client,
527 const struct i2c_device_id *id)
530 struct ad799x_platform_data *pdata = client->dev.platform_data;
531 struct ad799x_state *st;
532 struct iio_dev *indio_dev;
534 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
535 if (indio_dev == NULL)
538 st = iio_priv(indio_dev);
539 /* this is only used for device removal purposes */
540 i2c_set_clientdata(client, indio_dev);
542 st->id = id->driver_data;
543 st->chip_info = &ad799x_chip_info_tbl[st->id];
544 st->config = st->chip_info->default_config;
546 /* TODO: Add pdata options for filtering and bit delay */
551 st->int_vref_mv = pdata->vref_mv;
553 st->reg = devm_regulator_get(&client->dev, "vcc");
554 if (!IS_ERR(st->reg)) {
555 ret = regulator_enable(st->reg);
561 indio_dev->dev.parent = &client->dev;
562 indio_dev->name = id->name;
563 indio_dev->info = st->chip_info->info;
565 indio_dev->modes = INDIO_DIRECT_MODE;
566 indio_dev->channels = st->chip_info->channel;
567 indio_dev->num_channels = st->chip_info->num_channels;
569 ret = ad799x_register_ring_funcs_and_init(indio_dev);
571 goto error_disable_reg;
573 if (client->irq > 0) {
574 ret = request_threaded_irq(client->irq,
576 ad799x_event_handler,
577 IRQF_TRIGGER_FALLING |
582 goto error_cleanup_ring;
584 ret = iio_device_register(indio_dev);
591 free_irq(client->irq, indio_dev);
593 ad799x_ring_cleanup(indio_dev);
595 if (!IS_ERR(st->reg))
596 regulator_disable(st->reg);
601 static int ad799x_remove(struct i2c_client *client)
603 struct iio_dev *indio_dev = i2c_get_clientdata(client);
604 struct ad799x_state *st = iio_priv(indio_dev);
606 iio_device_unregister(indio_dev);
608 free_irq(client->irq, indio_dev);
610 ad799x_ring_cleanup(indio_dev);
611 if (!IS_ERR(st->reg))
612 regulator_disable(st->reg);
618 static const struct i2c_device_id ad799x_id[] = {
619 { "ad7991", ad7991 },
620 { "ad7995", ad7995 },
621 { "ad7999", ad7999 },
622 { "ad7992", ad7992 },
623 { "ad7993", ad7993 },
624 { "ad7994", ad7994 },
625 { "ad7997", ad7997 },
626 { "ad7998", ad7998 },
630 MODULE_DEVICE_TABLE(i2c, ad799x_id);
632 static struct i2c_driver ad799x_driver = {
636 .probe = ad799x_probe,
637 .remove = ad799x_remove,
638 .id_table = ad799x_id,
640 module_i2c_driver(ad799x_driver);
642 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
643 MODULE_DESCRIPTION("Analog Devices AD799x ADC");
644 MODULE_LICENSE("GPL v2");