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.
38 * AD7291 registers definition
40 #define AD7291_COMMAND 0x00
41 #define AD7291_VOLTAGE 0x01
42 #define AD7291_T_SENSE 0x02
43 #define AD7291_T_AVERAGE 0x03
44 #define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
45 #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
46 #define AD7291_HYST(x) ((x) * 3 + 0x6)
47 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
48 #define AD7291_T_ALERT_STATUS 0x20
50 #define AD7291_VOLTAGE_LIMIT_COUNT 8
56 #define AD7291_AUTOCYCLE (1 << 0)
57 #define AD7291_RESET (1 << 1)
58 #define AD7291_ALERT_CLEAR (1 << 2)
59 #define AD7291_ALERT_POLARITY (1 << 3)
60 #define AD7291_EXT_REF (1 << 4)
61 #define AD7291_NOISE_DELAY (1 << 5)
62 #define AD7291_T_SENSE_MASK (1 << 7)
63 #define AD7291_VOLTAGE_MASK 0xFF00
64 #define AD7291_VOLTAGE_OFFSET 0x8
69 #define AD7291_CHANNEL_MASK 0xF000
70 #define AD7291_BITS 12
71 #define AD7291_VALUE_MASK 0xFFF
72 #define AD7291_T_VALUE_SIGN 0x400
73 #define AD7291_T_VALUE_FLOAT_OFFSET 2
74 #define AD7291_T_VALUE_FLOAT_MASK 0x2
76 #define AD7291_BITS 12
78 struct ad7291_chip_info {
79 struct i2c_client *client;
80 struct regulator *reg;
82 u16 c_mask; /* Active voltage channels for events */
83 struct mutex state_lock;
86 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
88 struct i2c_client *client = chip->client;
91 ret = i2c_smbus_read_word_swapped(client, reg);
93 dev_err(&client->dev, "I2C read error\n");
102 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
104 return i2c_smbus_write_word_swapped(chip->client, reg, data);
107 static irqreturn_t ad7291_event_handler(int irq, void *private)
109 struct iio_dev *indio_dev = private;
110 struct ad7291_chip_info *chip = iio_priv(private);
111 u16 t_status, v_status;
114 s64 timestamp = iio_get_time_ns();
116 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
119 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
122 if (!(t_status || v_status))
125 command = chip->command | AD7291_ALERT_CLEAR;
126 ad7291_i2c_write(chip, AD7291_COMMAND, command);
128 command = chip->command & ~AD7291_ALERT_CLEAR;
129 ad7291_i2c_write(chip, AD7291_COMMAND, command);
131 /* For now treat t_sense and t_sense_average the same */
132 if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
133 iio_push_event(indio_dev,
134 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
139 if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
140 iio_push_event(indio_dev,
141 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
147 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
148 if (v_status & (1 << i))
149 iio_push_event(indio_dev,
150 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
155 if (v_status & (1 << (i + 1)))
156 iio_push_event(indio_dev,
157 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
167 static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
168 enum iio_event_direction dir, enum iio_event_info info)
172 switch (chan->type) {
174 offset = chan->channel;
184 case IIO_EV_INFO_VALUE:
185 if (dir == IIO_EV_DIR_FALLING)
186 return AD7291_DATA_HIGH(offset);
188 return AD7291_DATA_LOW(offset);
189 case IIO_EV_INFO_HYSTERESIS:
190 return AD7291_HYST(offset);
197 static int ad7291_read_event_value(struct iio_dev *indio_dev,
198 const struct iio_chan_spec *chan,
199 enum iio_event_type type,
200 enum iio_event_direction dir,
201 enum iio_event_info info,
204 struct ad7291_chip_info *chip = iio_priv(indio_dev);
208 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
213 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
214 *val = uval & AD7291_VALUE_MASK;
217 *val = sign_extend32(uval, 11);
222 static int ad7291_write_event_value(struct iio_dev *indio_dev,
223 const struct iio_chan_spec *chan,
224 enum iio_event_type type,
225 enum iio_event_direction dir,
226 enum iio_event_info info,
229 struct ad7291_chip_info *chip = iio_priv(indio_dev);
231 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
232 if (val > AD7291_VALUE_MASK || val < 0)
235 if (val > 2047 || val < -2048)
239 return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
243 static int ad7291_read_event_config(struct iio_dev *indio_dev,
244 const struct iio_chan_spec *chan,
245 enum iio_event_type type,
246 enum iio_event_direction dir)
248 struct ad7291_chip_info *chip = iio_priv(indio_dev);
249 /* To be enabled the channel must simply be on. If any are enabled
250 we are in continuous sampling mode */
252 switch (chan->type) {
254 if (chip->c_mask & (1 << (15 - chan->channel)))
267 static int ad7291_write_event_config(struct iio_dev *indio_dev,
268 const struct iio_chan_spec *chan,
269 enum iio_event_type type,
270 enum iio_event_direction dir,
274 struct ad7291_chip_info *chip = iio_priv(indio_dev);
278 mutex_lock(&chip->state_lock);
279 regval = chip->command;
281 * To be enabled the channel must simply be on. If any are enabled
282 * use continuous sampling mode.
283 * Possible to disable temp as well but that makes single read tricky.
286 mask = BIT(15 - chan->channel);
288 switch (chan->type) {
290 if ((!state) && (chip->c_mask & mask))
291 chip->c_mask &= ~mask;
292 else if (state && (!(chip->c_mask & mask)))
293 chip->c_mask |= mask;
297 regval &= ~AD7291_AUTOCYCLE;
298 regval |= chip->c_mask;
299 if (chip->c_mask) /* Enable autocycle? */
300 regval |= AD7291_AUTOCYCLE;
302 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
306 chip->command = regval;
313 mutex_unlock(&chip->state_lock);
317 static int ad7291_read_raw(struct iio_dev *indio_dev,
318 struct iio_chan_spec const *chan,
324 struct ad7291_chip_info *chip = iio_priv(indio_dev);
328 case IIO_CHAN_INFO_RAW:
329 switch (chan->type) {
331 mutex_lock(&chip->state_lock);
332 /* If in autocycle mode drop through */
333 if (chip->command & AD7291_AUTOCYCLE) {
334 mutex_unlock(&chip->state_lock);
337 /* Enable this channel alone */
338 regval = chip->command & (~AD7291_VOLTAGE_MASK);
339 regval |= 1 << (15 - chan->channel);
340 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
342 mutex_unlock(&chip->state_lock);
346 ret = i2c_smbus_read_word_swapped(chip->client,
349 mutex_unlock(&chip->state_lock);
352 *val = ret & AD7291_VALUE_MASK;
353 mutex_unlock(&chip->state_lock);
356 /* Assumes tsense bit of command register always set */
357 ret = i2c_smbus_read_word_swapped(chip->client,
361 *val = sign_extend32(ret, 11);
366 case IIO_CHAN_INFO_AVERAGE_RAW:
367 ret = i2c_smbus_read_word_swapped(chip->client,
371 *val = sign_extend32(ret, 11);
373 case IIO_CHAN_INFO_SCALE:
374 switch (chan->type) {
378 vref = regulator_get_voltage(chip->reg);
386 return IIO_VAL_FRACTIONAL_LOG2;
389 * One LSB of the ADC corresponds to 0.25 deg C.
390 * The temperature reading is in 12-bit twos
403 static const struct iio_event_spec ad7291_events[] = {
405 .type = IIO_EV_TYPE_THRESH,
406 .dir = IIO_EV_DIR_RISING,
407 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
408 BIT(IIO_EV_INFO_ENABLE),
410 .type = IIO_EV_TYPE_THRESH,
411 .dir = IIO_EV_DIR_FALLING,
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_EITHER,
417 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
421 #define AD7291_VOLTAGE_CHAN(_chan) \
423 .type = IIO_VOLTAGE, \
424 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
425 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
428 .event_spec = ad7291_events, \
429 .num_event_specs = ARRAY_SIZE(ad7291_events), \
432 static const struct iio_chan_spec ad7291_channels[] = {
433 AD7291_VOLTAGE_CHAN(0),
434 AD7291_VOLTAGE_CHAN(1),
435 AD7291_VOLTAGE_CHAN(2),
436 AD7291_VOLTAGE_CHAN(3),
437 AD7291_VOLTAGE_CHAN(4),
438 AD7291_VOLTAGE_CHAN(5),
439 AD7291_VOLTAGE_CHAN(6),
440 AD7291_VOLTAGE_CHAN(7),
443 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
444 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
445 BIT(IIO_CHAN_INFO_SCALE),
448 .event_spec = ad7291_events,
449 .num_event_specs = ARRAY_SIZE(ad7291_events),
453 static const struct iio_info ad7291_info = {
454 .read_raw = &ad7291_read_raw,
455 .read_event_config = &ad7291_read_event_config,
456 .write_event_config = &ad7291_write_event_config,
457 .read_event_value = &ad7291_read_event_value,
458 .write_event_value = &ad7291_write_event_value,
459 .driver_module = THIS_MODULE,
462 static int ad7291_probe(struct i2c_client *client,
463 const struct i2c_device_id *id)
465 struct ad7291_platform_data *pdata = client->dev.platform_data;
466 struct ad7291_chip_info *chip;
467 struct iio_dev *indio_dev;
470 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
473 chip = iio_priv(indio_dev);
475 if (pdata && pdata->use_external_ref) {
476 chip->reg = devm_regulator_get(&client->dev, "vref");
477 if (IS_ERR(chip->reg))
480 ret = regulator_enable(chip->reg);
485 mutex_init(&chip->state_lock);
486 /* this is only used for device removal purposes */
487 i2c_set_clientdata(client, indio_dev);
489 chip->client = client;
491 chip->command = AD7291_NOISE_DELAY |
492 AD7291_T_SENSE_MASK | /* Tsense always enabled */
493 AD7291_ALERT_POLARITY; /* set irq polarity low level */
495 if (pdata && pdata->use_external_ref)
496 chip->command |= AD7291_EXT_REF;
498 indio_dev->name = id->name;
499 indio_dev->channels = ad7291_channels;
500 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
502 indio_dev->dev.parent = &client->dev;
503 indio_dev->info = &ad7291_info;
504 indio_dev->modes = INDIO_DIRECT_MODE;
506 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
509 goto error_disable_reg;
512 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
515 goto error_disable_reg;
518 if (client->irq > 0) {
519 ret = request_threaded_irq(client->irq,
521 &ad7291_event_handler,
522 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
526 goto error_disable_reg;
529 ret = iio_device_register(indio_dev);
531 goto error_unreg_irq;
537 free_irq(client->irq, indio_dev);
540 regulator_disable(chip->reg);
545 static int ad7291_remove(struct i2c_client *client)
547 struct iio_dev *indio_dev = i2c_get_clientdata(client);
548 struct ad7291_chip_info *chip = iio_priv(indio_dev);
550 iio_device_unregister(indio_dev);
553 free_irq(client->irq, indio_dev);
556 regulator_disable(chip->reg);
561 static const struct i2c_device_id ad7291_id[] = {
566 MODULE_DEVICE_TABLE(i2c, ad7291_id);
568 static struct i2c_driver ad7291_driver = {
570 .name = KBUILD_MODNAME,
572 .probe = ad7291_probe,
573 .remove = ad7291_remove,
574 .id_table = ad7291_id,
576 module_i2c_driver(ad7291_driver);
578 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
579 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
580 MODULE_LICENSE("GPL v2");