2 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/events.h>
24 #include <linux/platform_data/ad7291.h>
29 * If no events enabled - single polled channel read
30 * If event enabled direct reads disable unless channel
31 * is in the read mask.
33 * The noise-delayed bit as per datasheet suggestion is always enabled.
37 * AD7291 registers definition
39 #define AD7291_COMMAND 0x00
40 #define AD7291_VOLTAGE 0x01
41 #define AD7291_T_SENSE 0x02
42 #define AD7291_T_AVERAGE 0x03
43 #define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
44 #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
45 #define AD7291_HYST(x) ((x) * 3 + 0x6)
46 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
47 #define AD7291_T_ALERT_STATUS 0x20
49 #define AD7291_BITS 12
50 #define AD7291_VOLTAGE_LIMIT_COUNT 8
56 #define AD7291_AUTOCYCLE BIT(0)
57 #define AD7291_RESET BIT(1)
58 #define AD7291_ALERT_CLEAR BIT(2)
59 #define AD7291_ALERT_POLARITY BIT(3)
60 #define AD7291_EXT_REF BIT(4)
61 #define AD7291_NOISE_DELAY BIT(5)
62 #define AD7291_T_SENSE_MASK BIT(7)
63 #define AD7291_VOLTAGE_MASK GENMASK(15, 8)
64 #define AD7291_VOLTAGE_OFFSET 8
69 #define AD7291_VALUE_MASK GENMASK(11, 0)
72 * AD7291 alert register bits
74 #define AD7291_T_LOW BIT(0)
75 #define AD7291_T_HIGH BIT(1)
76 #define AD7291_T_AVG_LOW BIT(2)
77 #define AD7291_T_AVG_HIGH BIT(3)
78 #define AD7291_V_LOW(x) BIT((x) * 2)
79 #define AD7291_V_HIGH(x) BIT((x) * 2 + 1)
82 struct ad7291_chip_info {
83 struct i2c_client *client;
84 struct regulator *reg;
86 u16 c_mask; /* Active voltage channels for events */
87 struct mutex state_lock;
90 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
92 struct i2c_client *client = chip->client;
95 ret = i2c_smbus_read_word_swapped(client, reg);
97 dev_err(&client->dev, "I2C read error\n");
106 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
108 return i2c_smbus_write_word_swapped(chip->client, reg, data);
111 static irqreturn_t ad7291_event_handler(int irq, void *private)
113 struct iio_dev *indio_dev = private;
114 struct ad7291_chip_info *chip = iio_priv(private);
115 u16 t_status, v_status;
118 s64 timestamp = iio_get_time_ns();
120 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
123 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
126 if (!(t_status || v_status))
129 command = chip->command | AD7291_ALERT_CLEAR;
130 ad7291_i2c_write(chip, AD7291_COMMAND, command);
132 command = chip->command & ~AD7291_ALERT_CLEAR;
133 ad7291_i2c_write(chip, AD7291_COMMAND, command);
135 /* For now treat t_sense and t_sense_average the same */
136 if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW))
137 iio_push_event(indio_dev,
138 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
143 if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH))
144 iio_push_event(indio_dev,
145 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
151 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
152 if (v_status & AD7291_V_LOW(i))
153 iio_push_event(indio_dev,
154 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
159 if (v_status & AD7291_V_HIGH(i))
160 iio_push_event(indio_dev,
161 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
171 static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
172 enum iio_event_direction dir,
173 enum iio_event_info info)
177 switch (chan->type) {
179 offset = chan->channel;
182 offset = AD7291_VOLTAGE_OFFSET;
189 case IIO_EV_INFO_VALUE:
190 if (dir == IIO_EV_DIR_FALLING)
191 return AD7291_DATA_HIGH(offset);
193 return AD7291_DATA_LOW(offset);
194 case IIO_EV_INFO_HYSTERESIS:
195 return AD7291_HYST(offset);
202 static int ad7291_read_event_value(struct iio_dev *indio_dev,
203 const struct iio_chan_spec *chan,
204 enum iio_event_type type,
205 enum iio_event_direction dir,
206 enum iio_event_info info,
209 struct ad7291_chip_info *chip = iio_priv(indio_dev);
213 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
218 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
219 *val = uval & AD7291_VALUE_MASK;
222 *val = sign_extend32(uval, 11);
227 static int ad7291_write_event_value(struct iio_dev *indio_dev,
228 const struct iio_chan_spec *chan,
229 enum iio_event_type type,
230 enum iio_event_direction dir,
231 enum iio_event_info info,
234 struct ad7291_chip_info *chip = iio_priv(indio_dev);
236 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
237 if (val > AD7291_VALUE_MASK || val < 0)
240 if (val > 2047 || val < -2048)
244 return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
248 static int ad7291_read_event_config(struct iio_dev *indio_dev,
249 const struct iio_chan_spec *chan,
250 enum iio_event_type type,
251 enum iio_event_direction dir)
253 struct ad7291_chip_info *chip = iio_priv(indio_dev);
255 * To be enabled the channel must simply be on. If any are enabled
256 * we are in continuous sampling mode
259 switch (chan->type) {
261 return !!(chip->c_mask & BIT(15 - chan->channel));
271 static int ad7291_write_event_config(struct iio_dev *indio_dev,
272 const struct iio_chan_spec *chan,
273 enum iio_event_type type,
274 enum iio_event_direction dir,
278 struct ad7291_chip_info *chip = iio_priv(indio_dev);
282 mutex_lock(&chip->state_lock);
283 regval = chip->command;
285 * To be enabled the channel must simply be on. If any are enabled
286 * use continuous sampling mode.
287 * Possible to disable temp as well but that makes single read tricky.
290 mask = BIT(15 - chan->channel);
292 switch (chan->type) {
294 if ((!state) && (chip->c_mask & mask))
295 chip->c_mask &= ~mask;
296 else if (state && (!(chip->c_mask & mask)))
297 chip->c_mask |= mask;
301 regval &= ~AD7291_AUTOCYCLE;
302 regval |= chip->c_mask;
303 if (chip->c_mask) /* Enable autocycle? */
304 regval |= AD7291_AUTOCYCLE;
306 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
310 chip->command = regval;
317 mutex_unlock(&chip->state_lock);
321 static int ad7291_read_raw(struct iio_dev *indio_dev,
322 struct iio_chan_spec const *chan,
328 struct ad7291_chip_info *chip = iio_priv(indio_dev);
332 case IIO_CHAN_INFO_RAW:
333 switch (chan->type) {
335 mutex_lock(&chip->state_lock);
336 /* If in autocycle mode drop through */
337 if (chip->command & AD7291_AUTOCYCLE) {
338 mutex_unlock(&chip->state_lock);
341 /* Enable this channel alone */
342 regval = chip->command & (~AD7291_VOLTAGE_MASK);
343 regval |= BIT(15 - chan->channel);
344 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
346 mutex_unlock(&chip->state_lock);
350 ret = i2c_smbus_read_word_swapped(chip->client,
353 mutex_unlock(&chip->state_lock);
356 *val = ret & AD7291_VALUE_MASK;
357 mutex_unlock(&chip->state_lock);
360 /* Assumes tsense bit of command register always set */
361 ret = i2c_smbus_read_word_swapped(chip->client,
365 *val = sign_extend32(ret, 11);
370 case IIO_CHAN_INFO_AVERAGE_RAW:
371 ret = i2c_smbus_read_word_swapped(chip->client,
375 *val = sign_extend32(ret, 11);
377 case IIO_CHAN_INFO_SCALE:
378 switch (chan->type) {
383 vref = regulator_get_voltage(chip->reg);
391 return IIO_VAL_FRACTIONAL_LOG2;
394 * One LSB of the ADC corresponds to 0.25 deg C.
395 * The temperature reading is in 12-bit twos
408 static const struct iio_event_spec ad7291_events[] = {
410 .type = IIO_EV_TYPE_THRESH,
411 .dir = IIO_EV_DIR_RISING,
412 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
413 BIT(IIO_EV_INFO_ENABLE),
415 .type = IIO_EV_TYPE_THRESH,
416 .dir = IIO_EV_DIR_FALLING,
417 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
418 BIT(IIO_EV_INFO_ENABLE),
420 .type = IIO_EV_TYPE_THRESH,
421 .dir = IIO_EV_DIR_EITHER,
422 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
426 #define AD7291_VOLTAGE_CHAN(_chan) \
428 .type = IIO_VOLTAGE, \
429 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
430 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
433 .event_spec = ad7291_events, \
434 .num_event_specs = ARRAY_SIZE(ad7291_events), \
437 static const struct iio_chan_spec ad7291_channels[] = {
438 AD7291_VOLTAGE_CHAN(0),
439 AD7291_VOLTAGE_CHAN(1),
440 AD7291_VOLTAGE_CHAN(2),
441 AD7291_VOLTAGE_CHAN(3),
442 AD7291_VOLTAGE_CHAN(4),
443 AD7291_VOLTAGE_CHAN(5),
444 AD7291_VOLTAGE_CHAN(6),
445 AD7291_VOLTAGE_CHAN(7),
448 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
449 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
450 BIT(IIO_CHAN_INFO_SCALE),
453 .event_spec = ad7291_events,
454 .num_event_specs = ARRAY_SIZE(ad7291_events),
458 static const struct iio_info ad7291_info = {
459 .read_raw = &ad7291_read_raw,
460 .read_event_config = &ad7291_read_event_config,
461 .write_event_config = &ad7291_write_event_config,
462 .read_event_value = &ad7291_read_event_value,
463 .write_event_value = &ad7291_write_event_value,
464 .driver_module = THIS_MODULE,
467 static int ad7291_probe(struct i2c_client *client,
468 const struct i2c_device_id *id)
470 struct ad7291_platform_data *pdata = client->dev.platform_data;
471 struct ad7291_chip_info *chip;
472 struct iio_dev *indio_dev;
475 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
478 chip = iio_priv(indio_dev);
480 if (pdata && pdata->use_external_ref) {
481 chip->reg = devm_regulator_get(&client->dev, "vref");
482 if (IS_ERR(chip->reg))
483 return PTR_ERR(chip->reg);
485 ret = regulator_enable(chip->reg);
490 mutex_init(&chip->state_lock);
491 /* this is only used for device removal purposes */
492 i2c_set_clientdata(client, indio_dev);
494 chip->client = client;
496 chip->command = AD7291_NOISE_DELAY |
497 AD7291_T_SENSE_MASK | /* Tsense always enabled */
498 AD7291_ALERT_POLARITY; /* set irq polarity low level */
500 if (pdata && pdata->use_external_ref)
501 chip->command |= AD7291_EXT_REF;
503 indio_dev->name = id->name;
504 indio_dev->channels = ad7291_channels;
505 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
507 indio_dev->dev.parent = &client->dev;
508 indio_dev->info = &ad7291_info;
509 indio_dev->modes = INDIO_DIRECT_MODE;
511 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
514 goto error_disable_reg;
517 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
520 goto error_disable_reg;
523 if (client->irq > 0) {
524 ret = request_threaded_irq(client->irq,
526 &ad7291_event_handler,
527 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
531 goto error_disable_reg;
534 ret = iio_device_register(indio_dev);
536 goto error_unreg_irq;
542 free_irq(client->irq, indio_dev);
545 regulator_disable(chip->reg);
550 static int ad7291_remove(struct i2c_client *client)
552 struct iio_dev *indio_dev = i2c_get_clientdata(client);
553 struct ad7291_chip_info *chip = iio_priv(indio_dev);
555 iio_device_unregister(indio_dev);
558 free_irq(client->irq, indio_dev);
561 regulator_disable(chip->reg);
566 static const struct i2c_device_id ad7291_id[] = {
571 MODULE_DEVICE_TABLE(i2c, ad7291_id);
573 static struct i2c_driver ad7291_driver = {
575 .name = KBUILD_MODNAME,
577 .probe = ad7291_probe,
578 .remove = ad7291_remove,
579 .id_table = ad7291_id,
581 module_i2c_driver(ad7291_driver);
583 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
584 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
585 MODULE_LICENSE("GPL v2");