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/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/events.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)
71 struct ad7291_chip_info {
72 struct i2c_client *client;
73 struct regulator *reg;
75 u16 c_mask; /* Active voltage channels for events */
76 struct mutex state_lock;
79 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
81 struct i2c_client *client = chip->client;
84 ret = i2c_smbus_read_word_swapped(client, reg);
86 dev_err(&client->dev, "I2C read error\n");
95 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
97 return i2c_smbus_write_word_swapped(chip->client, reg, data);
100 static irqreturn_t ad7291_event_handler(int irq, void *private)
102 struct iio_dev *indio_dev = private;
103 struct ad7291_chip_info *chip = iio_priv(private);
104 u16 t_status, v_status;
107 s64 timestamp = iio_get_time_ns();
109 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
112 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
115 if (!(t_status || v_status))
118 command = chip->command | AD7291_ALERT_CLEAR;
119 ad7291_i2c_write(chip, AD7291_COMMAND, command);
121 command = chip->command & ~AD7291_ALERT_CLEAR;
122 ad7291_i2c_write(chip, AD7291_COMMAND, command);
124 /* For now treat t_sense and t_sense_average the same */
125 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
126 iio_push_event(indio_dev,
127 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
132 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
133 iio_push_event(indio_dev,
134 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
140 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
141 if (v_status & (1 << i))
142 iio_push_event(indio_dev,
143 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
148 if (v_status & (1 << (i + 1)))
149 iio_push_event(indio_dev,
150 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
160 static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
161 enum iio_event_direction dir,
162 enum iio_event_info info)
166 switch (chan->type) {
168 offset = chan->channel;
171 offset = AD7291_VOLTAGE_OFFSET;
178 case IIO_EV_INFO_VALUE:
179 if (dir == IIO_EV_DIR_FALLING)
180 return AD7291_DATA_HIGH(offset);
182 return AD7291_DATA_LOW(offset);
183 case IIO_EV_INFO_HYSTERESIS:
184 return AD7291_HYST(offset);
191 static int ad7291_read_event_value(struct iio_dev *indio_dev,
192 const struct iio_chan_spec *chan,
193 enum iio_event_type type,
194 enum iio_event_direction dir,
195 enum iio_event_info info,
198 struct ad7291_chip_info *chip = iio_priv(indio_dev);
202 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
207 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
208 *val = uval & AD7291_VALUE_MASK;
211 *val = sign_extend32(uval, 11);
216 static int ad7291_write_event_value(struct iio_dev *indio_dev,
217 const struct iio_chan_spec *chan,
218 enum iio_event_type type,
219 enum iio_event_direction dir,
220 enum iio_event_info info,
223 struct ad7291_chip_info *chip = iio_priv(indio_dev);
225 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
226 if (val > AD7291_VALUE_MASK || val < 0)
229 if (val > 2047 || val < -2048)
233 return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
237 static int ad7291_read_event_config(struct iio_dev *indio_dev,
238 const struct iio_chan_spec *chan,
239 enum iio_event_type type,
240 enum iio_event_direction dir)
242 struct ad7291_chip_info *chip = iio_priv(indio_dev);
244 * To be enabled the channel must simply be on. If any are enabled
245 * we are in continuous sampling mode
248 switch (chan->type) {
250 if (chip->c_mask & BIT(15 - chan->channel))
263 static int ad7291_write_event_config(struct iio_dev *indio_dev,
264 const struct iio_chan_spec *chan,
265 enum iio_event_type type,
266 enum iio_event_direction dir,
270 struct ad7291_chip_info *chip = iio_priv(indio_dev);
274 mutex_lock(&chip->state_lock);
275 regval = chip->command;
277 * To be enabled the channel must simply be on. If any are enabled
278 * use continuous sampling mode.
279 * Possible to disable temp as well but that makes single read tricky.
282 mask = BIT(15 - chan->channel);
284 switch (chan->type) {
286 if ((!state) && (chip->c_mask & mask))
287 chip->c_mask &= ~mask;
288 else if (state && (!(chip->c_mask & mask)))
289 chip->c_mask |= mask;
293 regval &= ~AD7291_AUTOCYCLE;
294 regval |= chip->c_mask;
295 if (chip->c_mask) /* Enable autocycle? */
296 regval |= AD7291_AUTOCYCLE;
298 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
302 chip->command = regval;
309 mutex_unlock(&chip->state_lock);
313 static int ad7291_read_raw(struct iio_dev *indio_dev,
314 struct iio_chan_spec const *chan,
320 struct ad7291_chip_info *chip = iio_priv(indio_dev);
324 case IIO_CHAN_INFO_RAW:
325 switch (chan->type) {
327 mutex_lock(&chip->state_lock);
328 /* If in autocycle mode drop through */
329 if (chip->command & AD7291_AUTOCYCLE) {
330 mutex_unlock(&chip->state_lock);
333 /* Enable this channel alone */
334 regval = chip->command & (~AD7291_VOLTAGE_MASK);
335 regval |= BIT(15 - chan->channel);
336 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
338 mutex_unlock(&chip->state_lock);
342 ret = i2c_smbus_read_word_swapped(chip->client,
345 mutex_unlock(&chip->state_lock);
348 *val = ret & AD7291_VALUE_MASK;
349 mutex_unlock(&chip->state_lock);
352 /* Assumes tsense bit of command register always set */
353 ret = i2c_smbus_read_word_swapped(chip->client,
357 *val = sign_extend32(ret, 11);
362 case IIO_CHAN_INFO_AVERAGE_RAW:
363 ret = i2c_smbus_read_word_swapped(chip->client,
367 *val = sign_extend32(ret, 11);
369 case IIO_CHAN_INFO_SCALE:
370 switch (chan->type) {
374 vref = regulator_get_voltage(chip->reg);
382 return IIO_VAL_FRACTIONAL_LOG2;
385 * One LSB of the ADC corresponds to 0.25 deg C.
386 * The temperature reading is in 12-bit twos
399 static const struct iio_event_spec ad7291_events[] = {
401 .type = IIO_EV_TYPE_THRESH,
402 .dir = IIO_EV_DIR_RISING,
403 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
404 BIT(IIO_EV_INFO_ENABLE),
406 .type = IIO_EV_TYPE_THRESH,
407 .dir = IIO_EV_DIR_FALLING,
408 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
409 BIT(IIO_EV_INFO_ENABLE),
411 .type = IIO_EV_TYPE_THRESH,
412 .dir = IIO_EV_DIR_EITHER,
413 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
417 #define AD7291_VOLTAGE_CHAN(_chan) \
419 .type = IIO_VOLTAGE, \
420 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
421 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
424 .event_spec = ad7291_events, \
425 .num_event_specs = ARRAY_SIZE(ad7291_events), \
428 static const struct iio_chan_spec ad7291_channels[] = {
429 AD7291_VOLTAGE_CHAN(0),
430 AD7291_VOLTAGE_CHAN(1),
431 AD7291_VOLTAGE_CHAN(2),
432 AD7291_VOLTAGE_CHAN(3),
433 AD7291_VOLTAGE_CHAN(4),
434 AD7291_VOLTAGE_CHAN(5),
435 AD7291_VOLTAGE_CHAN(6),
436 AD7291_VOLTAGE_CHAN(7),
439 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
440 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
441 BIT(IIO_CHAN_INFO_SCALE),
444 .event_spec = ad7291_events,
445 .num_event_specs = ARRAY_SIZE(ad7291_events),
449 static const struct iio_info ad7291_info = {
450 .read_raw = &ad7291_read_raw,
451 .read_event_config = &ad7291_read_event_config,
452 .write_event_config = &ad7291_write_event_config,
453 .read_event_value = &ad7291_read_event_value,
454 .write_event_value = &ad7291_write_event_value,
455 .driver_module = THIS_MODULE,
458 static int ad7291_probe(struct i2c_client *client,
459 const struct i2c_device_id *id)
461 struct ad7291_platform_data *pdata = client->dev.platform_data;
462 struct ad7291_chip_info *chip;
463 struct iio_dev *indio_dev;
466 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
469 chip = iio_priv(indio_dev);
471 if (pdata && pdata->use_external_ref) {
472 chip->reg = devm_regulator_get(&client->dev, "vref");
473 if (IS_ERR(chip->reg))
476 ret = regulator_enable(chip->reg);
481 mutex_init(&chip->state_lock);
482 /* this is only used for device removal purposes */
483 i2c_set_clientdata(client, indio_dev);
485 chip->client = client;
487 chip->command = AD7291_NOISE_DELAY |
488 AD7291_T_SENSE_MASK | /* Tsense always enabled */
489 AD7291_ALERT_POLARITY; /* set irq polarity low level */
491 if (pdata && pdata->use_external_ref)
492 chip->command |= AD7291_EXT_REF;
494 indio_dev->name = id->name;
495 indio_dev->channels = ad7291_channels;
496 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
498 indio_dev->dev.parent = &client->dev;
499 indio_dev->info = &ad7291_info;
500 indio_dev->modes = INDIO_DIRECT_MODE;
502 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
505 goto error_disable_reg;
508 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
511 goto error_disable_reg;
514 if (client->irq > 0) {
515 ret = request_threaded_irq(client->irq,
517 &ad7291_event_handler,
518 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
522 goto error_disable_reg;
525 ret = iio_device_register(indio_dev);
527 goto error_unreg_irq;
533 free_irq(client->irq, indio_dev);
536 regulator_disable(chip->reg);
541 static int ad7291_remove(struct i2c_client *client)
543 struct iio_dev *indio_dev = i2c_get_clientdata(client);
544 struct ad7291_chip_info *chip = iio_priv(indio_dev);
546 iio_device_unregister(indio_dev);
549 free_irq(client->irq, indio_dev);
552 regulator_disable(chip->reg);
557 static const struct i2c_device_id ad7291_id[] = {
562 MODULE_DEVICE_TABLE(i2c, ad7291_id);
564 static struct i2c_driver ad7291_driver = {
566 .name = KBUILD_MODNAME,
568 .probe = ad7291_probe,
569 .remove = ad7291_remove,
570 .id_table = ad7291_id,
572 module_i2c_driver(ad7291_driver);
574 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
575 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
576 MODULE_LICENSE("GPL v2");