blob: c0eabf156702a7d4da6a26a477d8cbc50e62c3cb [file] [log] [blame]
Sonic Zhangddaecd52010-10-27 21:43:55 -04001/*
Michael Hennerichf22c1cf2011-09-02 17:25:37 +01002 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
Sonic Zhangddaecd52010-10-27 21:43:55 -04003 *
Michael Hennerichf22c1cf2011-09-02 17:25:37 +01004 * Copyright 2010-2011 Analog Devices Inc.
Sonic Zhangddaecd52010-10-27 21:43:55 -04005 *
6 * Licensed under the GPL-2 or later.
7 */
8
Sonic Zhangddaecd52010-10-27 21:43:55 -04009#include <linux/device.h>
Lars-Peter Clausen3b1cae72014-06-30 09:50:00 +010010#include <linux/err.h>
Sonic Zhangddaecd52010-10-27 21:43:55 -040011#include <linux/i2c.h>
Lars-Peter Clausen3b1cae72014-06-30 09:50:00 +010012#include <linux/interrupt.h>
13#include <linux/kernel.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040014#include <linux/module.h>
Jonathan Cameron4fd24242011-09-02 17:25:32 +010015#include <linux/mutex.h>
Michael Hennerich3b424f32011-09-02 17:25:39 +010016#include <linux/regulator/consumer.h>
Lars-Peter Clausen3b1cae72014-06-30 09:50:00 +010017#include <linux/slab.h>
18#include <linux/sysfs.h>
Sonic Zhangddaecd52010-10-27 21:43:55 -040019
Jonathan Cameron06458e22012-04-25 15:54:58 +010020#include <linux/iio/iio.h>
21#include <linux/iio/sysfs.h>
22#include <linux/iio/events.h>
Sonic Zhangddaecd52010-10-27 21:43:55 -040023
Lars-Peter Clausen3b1cae72014-06-30 09:50:00 +010024#include <linux/platform_data/ad7291.h>
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +010025
Sonic Zhangddaecd52010-10-27 21:43:55 -040026/*
Jonathan Cameron4fd24242011-09-02 17:25:32 +010027 * Simplified handling
28 *
29 * If no events enabled - single polled channel read
30 * If event enabled direct reads disable unless channel
31 * is in the read mask.
32 *
33 * The noise-delayed bit as per datasheet suggestion is always enabled.
Jonathan Cameron4fd24242011-09-02 17:25:32 +010034 */
Michael Hennerich3b424f32011-09-02 17:25:39 +010035
Jonathan Cameron4fd24242011-09-02 17:25:32 +010036/*
Sonic Zhangddaecd52010-10-27 21:43:55 -040037 * AD7291 registers definition
38 */
Michael Hennerich81e2edb2011-09-02 17:25:40 +010039#define AD7291_COMMAND 0x00
40#define AD7291_VOLTAGE 0x01
41#define AD7291_T_SENSE 0x02
42#define AD7291_T_AVERAGE 0x03
Lars-Peter Clausen3f8db0d2013-06-14 16:58:00 +010043#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)
Michael Hennerich00feefb2011-09-02 17:25:38 +010046#define AD7291_VOLTAGE_ALERT_STATUS 0x1F
Sonic Zhangddaecd52010-10-27 21:43:55 -040047#define AD7291_T_ALERT_STATUS 0x20
48
Hartmut Knaack5714d0c2014-08-07 12:06:00 +010049#define AD7291_BITS 12
Michael Hennerich81e2edb2011-09-02 17:25:40 +010050#define AD7291_VOLTAGE_LIMIT_COUNT 8
51
52
Sonic Zhangddaecd52010-10-27 21:43:55 -040053/*
54 * AD7291 command
55 */
Hartmut Knaack5714d0c2014-08-07 12:06:00 +010056#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
Sonic Zhangddaecd52010-10-27 21:43:55 -040065
66/*
67 * AD7291 value masks
68 */
Hartmut Knaack5714d0c2014-08-07 12:06:00 +010069#define AD7291_VALUE_MASK GENMASK(11, 0)
Sonic Zhangddaecd52010-10-27 21:43:55 -040070
Hartmut Knaackcfa71bf2014-08-07 12:06:00 +010071/*
72 * AD7291 alert register bits
73 */
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)
80
Michael Hennerich3b424f32011-09-02 17:25:39 +010081
Sonic Zhangddaecd52010-10-27 21:43:55 -040082struct ad7291_chip_info {
Michael Hennerich3b424f32011-09-02 17:25:39 +010083 struct i2c_client *client;
84 struct regulator *reg;
Michael Hennerich3b424f32011-09-02 17:25:39 +010085 u16 command;
Michael Hennerich06420c22011-09-02 17:25:43 +010086 u16 c_mask; /* Active voltage channels for events */
Michael Henneriche68b1db2011-09-02 17:25:45 +010087 struct mutex state_lock;
Sonic Zhangddaecd52010-10-27 21:43:55 -040088};
89
Sonic Zhangddaecd52010-10-27 21:43:55 -040090static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
91{
92 struct i2c_client *client = chip->client;
93 int ret = 0;
94
Lars-Peter Clausen7bd49fe2013-06-14 16:58:00 +010095 ret = i2c_smbus_read_word_swapped(client, reg);
Sonic Zhangddaecd52010-10-27 21:43:55 -040096 if (ret < 0) {
97 dev_err(&client->dev, "I2C read error\n");
98 return ret;
99 }
100
Lars-Peter Clausen7bd49fe2013-06-14 16:58:00 +0100101 *data = ret;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400102
103 return 0;
104}
105
106static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
107{
Lars-Peter Clausen7bd49fe2013-06-14 16:58:00 +0100108 return i2c_smbus_write_word_swapped(chip->client, reg, data);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400109}
110
Jonathan Cameron58c03232011-05-18 14:41:11 +0100111static irqreturn_t ad7291_event_handler(int irq, void *private)
Sonic Zhangddaecd52010-10-27 21:43:55 -0400112{
Jonathan Cameron58c03232011-05-18 14:41:11 +0100113 struct iio_dev *indio_dev = private;
Jonathan Camerond4397972011-06-27 13:07:23 +0100114 struct ad7291_chip_info *chip = iio_priv(private);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400115 u16 t_status, v_status;
116 u16 command;
117 int i;
Jonathan Cameron58c03232011-05-18 14:41:11 +0100118 s64 timestamp = iio_get_time_ns();
Sonic Zhangddaecd52010-10-27 21:43:55 -0400119
120 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
Jonathan Cameron58c03232011-05-18 14:41:11 +0100121 return IRQ_HANDLED;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400122
123 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
Jonathan Cameron58c03232011-05-18 14:41:11 +0100124 return IRQ_HANDLED;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400125
126 if (!(t_status || v_status))
Jonathan Cameron58c03232011-05-18 14:41:11 +0100127 return IRQ_HANDLED;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400128
Michael Hennerichf22c1cf2011-09-02 17:25:37 +0100129 command = chip->command | AD7291_ALERT_CLEAR;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400130 ad7291_i2c_write(chip, AD7291_COMMAND, command);
131
Michael Hennerichf22c1cf2011-09-02 17:25:37 +0100132 command = chip->command & ~AD7291_ALERT_CLEAR;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400133 ad7291_i2c_write(chip, AD7291_COMMAND, command);
134
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100135 /* For now treat t_sense and t_sense_average the same */
Hartmut Knaackcfa71bf2014-08-07 12:06:00 +0100136 if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW))
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100137 iio_push_event(indio_dev,
Jonathan Cameronb206c3b2011-05-18 14:42:15 +0100138 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
139 0,
140 IIO_EV_TYPE_THRESH,
141 IIO_EV_DIR_FALLING),
142 timestamp);
Hartmut Knaackcfa71bf2014-08-07 12:06:00 +0100143 if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH))
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100144 iio_push_event(indio_dev,
Jonathan Cameronb206c3b2011-05-18 14:42:15 +0100145 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
146 0,
147 IIO_EV_TYPE_THRESH,
148 IIO_EV_DIR_RISING),
149 timestamp);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400150
Hartmut Knaackcfa71bf2014-08-07 12:06:00 +0100151 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
152 if (v_status & AD7291_V_LOW(i))
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100153 iio_push_event(indio_dev,
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100154 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
Hartmut Knaackcfa71bf2014-08-07 12:06:00 +0100155 i,
Jonathan Cameronb206c3b2011-05-18 14:42:15 +0100156 IIO_EV_TYPE_THRESH,
157 IIO_EV_DIR_FALLING),
158 timestamp);
Hartmut Knaackcfa71bf2014-08-07 12:06:00 +0100159 if (v_status & AD7291_V_HIGH(i))
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100160 iio_push_event(indio_dev,
Jonathan Cameron6835cb62011-09-27 09:56:41 +0100161 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
Hartmut Knaackcfa71bf2014-08-07 12:06:00 +0100162 i,
Jonathan Cameronb206c3b2011-05-18 14:42:15 +0100163 IIO_EV_TYPE_THRESH,
164 IIO_EV_DIR_RISING),
165 timestamp);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400166 }
Jonathan Cameron58c03232011-05-18 14:41:11 +0100167
168 return IRQ_HANDLED;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400169}
170
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100171static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100172 enum iio_event_direction dir,
173 enum iio_event_info info)
Lars-Peter Clausen3f8db0d2013-06-14 16:58:00 +0100174{
175 unsigned int offset;
176
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100177 switch (chan->type) {
Lars-Peter Clausen3f8db0d2013-06-14 16:58:00 +0100178 case IIO_VOLTAGE:
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100179 offset = chan->channel;
Lars-Peter Clausen3f8db0d2013-06-14 16:58:00 +0100180 break;
181 case IIO_TEMP:
Hartmut Knaack5714d0c2014-08-07 12:06:00 +0100182 offset = AD7291_VOLTAGE_OFFSET;
Lars-Peter Clausen3f8db0d2013-06-14 16:58:00 +0100183 break;
184 default:
185 return 0;
186 }
187
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100188 switch (info) {
189 case IIO_EV_INFO_VALUE:
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100190 if (dir == IIO_EV_DIR_FALLING)
191 return AD7291_DATA_HIGH(offset);
192 else
193 return AD7291_DATA_LOW(offset);
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100194 case IIO_EV_INFO_HYSTERESIS:
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100195 return AD7291_HYST(offset);
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100196 default:
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100197 break;
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100198 }
199 return 0;
Lars-Peter Clausen3f8db0d2013-06-14 16:58:00 +0100200}
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100201
202static int ad7291_read_event_value(struct iio_dev *indio_dev,
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100203 const struct iio_chan_spec *chan,
204 enum iio_event_type type,
205 enum iio_event_direction dir,
206 enum iio_event_info info,
207 int *val, int *val2)
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100208{
209 struct ad7291_chip_info *chip = iio_priv(indio_dev);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100210 int ret;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100211 u16 uval;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100212
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100213 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100214 &uval);
Lars-Peter Clausen3f8db0d2013-06-14 16:58:00 +0100215 if (ret < 0)
216 return ret;
217
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100218 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
Michael Hennerich81e2edb2011-09-02 17:25:40 +0100219 *val = uval & AD7291_VALUE_MASK;
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100220
221 else
Lars-Peter Clausen88198d22013-06-14 16:58:00 +0100222 *val = sign_extend32(uval, 11);
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100223
224 return IIO_VAL_INT;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100225}
226
227static int ad7291_write_event_value(struct iio_dev *indio_dev,
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100228 const struct iio_chan_spec *chan,
229 enum iio_event_type type,
230 enum iio_event_direction dir,
231 enum iio_event_info info,
232 int val, int val2)
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100233{
234 struct ad7291_chip_info *chip = iio_priv(indio_dev);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100235
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100236 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
Michael Hennericha3ef65d2011-09-02 17:25:46 +0100237 if (val > AD7291_VALUE_MASK || val < 0)
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100238 return -EINVAL;
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100239 } else {
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100240 if (val > 2047 || val < -2048)
241 return -EINVAL;
Lars-Peter Clausen3f8db0d2013-06-14 16:58:00 +0100242 }
243
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100244 return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100245 val);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100246}
247
248static int ad7291_read_event_config(struct iio_dev *indio_dev,
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100249 const struct iio_chan_spec *chan,
250 enum iio_event_type type,
251 enum iio_event_direction dir)
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100252{
253 struct ad7291_chip_info *chip = iio_priv(indio_dev);
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100254 /*
255 * To be enabled the channel must simply be on. If any are enabled
256 * we are in continuous sampling mode
257 */
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100258
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100259 switch (chan->type) {
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100260 case IIO_VOLTAGE:
Hartmut Knaack8a32f652014-08-07 12:06:00 +0100261 return !!(chip->c_mask & BIT(15 - chan->channel));
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100262 case IIO_TEMP:
263 /* always on */
264 return 1;
265 default:
266 return -EINVAL;
267 }
268
269}
270
271static int ad7291_write_event_config(struct iio_dev *indio_dev,
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100272 const struct iio_chan_spec *chan,
273 enum iio_event_type type,
274 enum iio_event_direction dir,
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100275 int state)
276{
277 int ret = 0;
278 struct ad7291_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100279 unsigned int mask;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100280 u16 regval;
281
282 mutex_lock(&chip->state_lock);
283 regval = chip->command;
284 /*
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.
288 */
Michael Hennerich81e2edb2011-09-02 17:25:40 +0100289
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100290 mask = BIT(15 - chan->channel);
291
292 switch (chan->type) {
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100293 case IIO_VOLTAGE:
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100294 if ((!state) && (chip->c_mask & mask))
295 chip->c_mask &= ~mask;
296 else if (state && (!(chip->c_mask & mask)))
297 chip->c_mask |= mask;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100298 else
299 break;
300
Michael Hennerich81e2edb2011-09-02 17:25:40 +0100301 regval &= ~AD7291_AUTOCYCLE;
Michael Hennerich06420c22011-09-02 17:25:43 +0100302 regval |= chip->c_mask;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100303 if (chip->c_mask) /* Enable autocycle? */
304 regval |= AD7291_AUTOCYCLE;
305
306 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
307 if (ret < 0)
308 goto error_ret;
309
310 chip->command = regval;
311 break;
312 default:
313 ret = -EINVAL;
314 }
315
316error_ret:
317 mutex_unlock(&chip->state_lock);
318 return ret;
319}
320
321static int ad7291_read_raw(struct iio_dev *indio_dev,
322 struct iio_chan_spec const *chan,
323 int *val,
324 int *val2,
325 long mask)
326{
327 int ret;
328 struct ad7291_chip_info *chip = iio_priv(indio_dev);
329 u16 regval;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100330
331 switch (mask) {
Jonathan Cameronb11f98f2012-04-15 17:41:18 +0100332 case IIO_CHAN_INFO_RAW:
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100333 switch (chan->type) {
334 case IIO_VOLTAGE:
335 mutex_lock(&chip->state_lock);
336 /* If in autocycle mode drop through */
Michael Hennerich06420c22011-09-02 17:25:43 +0100337 if (chip->command & AD7291_AUTOCYCLE) {
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100338 mutex_unlock(&chip->state_lock);
339 return -EBUSY;
340 }
341 /* Enable this channel alone */
342 regval = chip->command & (~AD7291_VOLTAGE_MASK);
Hartmut Knaack5714d0c2014-08-07 12:06:00 +0100343 regval |= BIT(15 - chan->channel);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100344 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
345 if (ret < 0) {
346 mutex_unlock(&chip->state_lock);
347 return ret;
348 }
349 /* Read voltage */
Lars-Peter Clausen7bd49fe2013-06-14 16:58:00 +0100350 ret = i2c_smbus_read_word_swapped(chip->client,
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100351 AD7291_VOLTAGE);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100352 if (ret < 0) {
353 mutex_unlock(&chip->state_lock);
354 return ret;
355 }
Lars-Peter Clausen7bd49fe2013-06-14 16:58:00 +0100356 *val = ret & AD7291_VALUE_MASK;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100357 mutex_unlock(&chip->state_lock);
358 return IIO_VAL_INT;
359 case IIO_TEMP:
360 /* Assumes tsense bit of command register always set */
Lars-Peter Clausen7bd49fe2013-06-14 16:58:00 +0100361 ret = i2c_smbus_read_word_swapped(chip->client,
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100362 AD7291_T_SENSE);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100363 if (ret < 0)
364 return ret;
Lars-Peter Clausen88198d22013-06-14 16:58:00 +0100365 *val = sign_extend32(ret, 11);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100366 return IIO_VAL_INT;
367 default:
368 return -EINVAL;
369 }
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100370 case IIO_CHAN_INFO_AVERAGE_RAW:
Lars-Peter Clausen7bd49fe2013-06-14 16:58:00 +0100371 ret = i2c_smbus_read_word_swapped(chip->client,
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100372 AD7291_T_AVERAGE);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100373 if (ret < 0)
374 return ret;
Lars-Peter Clausen88198d22013-06-14 16:58:00 +0100375 *val = sign_extend32(ret, 11);
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100376 return IIO_VAL_INT;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100377 case IIO_CHAN_INFO_SCALE:
378 switch (chan->type) {
379 case IIO_VOLTAGE:
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +0100380 if (chip->reg) {
381 int vref;
Lars-Peter Clausen3b1cae72014-06-30 09:50:00 +0100382
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +0100383 vref = regulator_get_voltage(chip->reg);
384 if (vref < 0)
385 return vref;
386 *val = vref / 1000;
387 } else {
388 *val = 2500;
389 }
Lars-Peter Clausen73a44452013-06-14 16:58:00 +0100390 *val2 = AD7291_BITS;
391 return IIO_VAL_FRACTIONAL_LOG2;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100392 case IIO_TEMP:
393 /*
394 * One LSB of the ADC corresponds to 0.25 deg C.
395 * The temperature reading is in 12-bit twos
396 * complement format
397 */
398 *val = 250;
399 return IIO_VAL_INT;
400 default:
401 return -EINVAL;
402 }
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100403 default:
404 return -EINVAL;
405 }
406}
407
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100408static const struct iio_event_spec ad7291_events[] = {
409 {
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),
414 }, {
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),
Lars-Peter Clausenec871972013-10-07 15:11:00 +0100419 }, {
420 .type = IIO_EV_TYPE_THRESH,
421 .dir = IIO_EV_DIR_EITHER,
422 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100423 },
424};
425
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100426#define AD7291_VOLTAGE_CHAN(_chan) \
427{ \
428 .type = IIO_VOLTAGE, \
Jonathan Cameron03259482013-02-27 19:37:16 +0000429 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
430 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100431 .indexed = 1, \
432 .channel = _chan, \
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100433 .event_spec = ad7291_events, \
434 .num_event_specs = ARRAY_SIZE(ad7291_events), \
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100435}
436
437static 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),
446 {
447 .type = IIO_TEMP,
Jonathan Cameron03259482013-02-27 19:37:16 +0000448 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
449 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
450 BIT(IIO_CHAN_INFO_SCALE),
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100451 .indexed = 1,
452 .channel = 0,
Lars-Peter Clausenb80a6742013-10-07 15:11:00 +0100453 .event_spec = ad7291_events,
454 .num_event_specs = ARRAY_SIZE(ad7291_events),
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100455 }
456};
457
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100458static const struct iio_info ad7291_info = {
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100459 .read_raw = &ad7291_read_raw,
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000460 .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,
Wei Yongjunbb377962013-07-04 14:46:00 +0100464 .driver_module = THIS_MODULE,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100465};
466
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500467static int ad7291_probe(struct i2c_client *client,
Hartmut Knaack975d98c2014-08-07 12:06:00 +0100468 const struct i2c_device_id *id)
Sonic Zhangddaecd52010-10-27 21:43:55 -0400469{
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +0100470 struct ad7291_platform_data *pdata = client->dev.platform_data;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400471 struct ad7291_chip_info *chip;
Jonathan Camerond4397972011-06-27 13:07:23 +0100472 struct iio_dev *indio_dev;
Dan Carpenterb70e19c2014-06-20 20:22:00 +0100473 int ret;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400474
Sachin Kamat3e70bad2013-08-31 18:12:00 +0100475 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
476 if (!indio_dev)
477 return -ENOMEM;
Dan Carpenter03bda052011-06-29 22:50:48 +0300478 chip = iio_priv(indio_dev);
Michael Hennerich3b424f32011-09-02 17:25:39 +0100479
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +0100480 if (pdata && pdata->use_external_ref) {
Sachin Kamat3e70bad2013-08-31 18:12:00 +0100481 chip->reg = devm_regulator_get(&client->dev, "vref");
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +0100482 if (IS_ERR(chip->reg))
Dan Carpenterb70e19c2014-06-20 20:22:00 +0100483 return PTR_ERR(chip->reg);
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +0100484
Michael Hennerich3b424f32011-09-02 17:25:39 +0100485 ret = regulator_enable(chip->reg);
486 if (ret)
Sachin Kamat3e70bad2013-08-31 18:12:00 +0100487 return ret;
Michael Hennerich3b424f32011-09-02 17:25:39 +0100488 }
489
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100490 mutex_init(&chip->state_lock);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400491 /* this is only used for device removal purposes */
Jonathan Camerond4397972011-06-27 13:07:23 +0100492 i2c_set_clientdata(client, indio_dev);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400493
494 chip->client = client;
Michael Hennerichc11f02e2011-09-02 17:25:44 +0100495
496 chip->command = AD7291_NOISE_DELAY |
497 AD7291_T_SENSE_MASK | /* Tsense always enabled */
498 AD7291_ALERT_POLARITY; /* set irq polarity low level */
Sonic Zhangddaecd52010-10-27 21:43:55 -0400499
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +0100500 if (pdata && pdata->use_external_ref)
Michael Hennerich3b424f32011-09-02 17:25:39 +0100501 chip->command |= AD7291_EXT_REF;
Michael Hennerich3b424f32011-09-02 17:25:39 +0100502
Jonathan Camerond4397972011-06-27 13:07:23 +0100503 indio_dev->name = id->name;
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100504 indio_dev->channels = ad7291_channels;
505 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
506
Jonathan Camerond4397972011-06-27 13:07:23 +0100507 indio_dev->dev.parent = &client->dev;
508 indio_dev->info = &ad7291_info;
509 indio_dev->modes = INDIO_DIRECT_MODE;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400510
Michael Hennerichc11f02e2011-09-02 17:25:44 +0100511 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
512 if (ret) {
513 ret = -EIO;
514 goto error_disable_reg;
515 }
516
517 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
518 if (ret) {
519 ret = -EIO;
520 goto error_disable_reg;
521 }
522
Sonic Zhangddaecd52010-10-27 21:43:55 -0400523 if (client->irq > 0) {
Jonathan Cameron58c03232011-05-18 14:41:11 +0100524 ret = request_threaded_irq(client->irq,
525 NULL,
526 &ad7291_event_handler,
527 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
Jonathan Cameron845bd122011-05-18 14:41:44 +0100528 id->name,
Jonathan Camerond4397972011-06-27 13:07:23 +0100529 indio_dev);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400530 if (ret)
Michael Hennerich3b424f32011-09-02 17:25:39 +0100531 goto error_disable_reg;
Sonic Zhangddaecd52010-10-27 21:43:55 -0400532 }
533
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100534 ret = iio_device_register(indio_dev);
535 if (ret)
536 goto error_unreg_irq;
537
Sonic Zhangddaecd52010-10-27 21:43:55 -0400538 return 0;
539
540error_unreg_irq:
Jonathan Cameron4fd24242011-09-02 17:25:32 +0100541 if (client->irq)
542 free_irq(client->irq, indio_dev);
Michael Hennerich3b424f32011-09-02 17:25:39 +0100543error_disable_reg:
Lars-Peter Clausen1df9d1d2013-06-14 16:58:00 +0100544 if (chip->reg)
Michael Hennerich3b424f32011-09-02 17:25:39 +0100545 regulator_disable(chip->reg);
Sachin Kamat3e70bad2013-08-31 18:12:00 +0100546
Sonic Zhangddaecd52010-10-27 21:43:55 -0400547 return ret;
548}
549
Bill Pemberton447d4f22012-11-19 13:26:37 -0500550static int ad7291_remove(struct i2c_client *client)
Sonic Zhangddaecd52010-10-27 21:43:55 -0400551{
Jonathan Camerond4397972011-06-27 13:07:23 +0100552 struct iio_dev *indio_dev = i2c_get_clientdata(client);
Michael Hennerich3b424f32011-09-02 17:25:39 +0100553 struct ad7291_chip_info *chip = iio_priv(indio_dev);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400554
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100555 iio_device_unregister(indio_dev);
556
Sonic Zhangddaecd52010-10-27 21:43:55 -0400557 if (client->irq)
Jonathan Camerond4397972011-06-27 13:07:23 +0100558 free_irq(client->irq, indio_dev);
Michael Hennerich3b424f32011-09-02 17:25:39 +0100559
Sachin Kamat3e70bad2013-08-31 18:12:00 +0100560 if (chip->reg)
Michael Hennerich3b424f32011-09-02 17:25:39 +0100561 regulator_disable(chip->reg);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400562
563 return 0;
564}
565
566static const struct i2c_device_id ad7291_id[] = {
567 { "ad7291", 0 },
568 {}
569};
570
571MODULE_DEVICE_TABLE(i2c, ad7291_id);
572
573static struct i2c_driver ad7291_driver = {
574 .driver = {
Michael Henneriche68b1db2011-09-02 17:25:45 +0100575 .name = KBUILD_MODNAME,
Sonic Zhangddaecd52010-10-27 21:43:55 -0400576 },
577 .probe = ad7291_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500578 .remove = ad7291_remove,
Sonic Zhangddaecd52010-10-27 21:43:55 -0400579 .id_table = ad7291_id,
580};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100581module_i2c_driver(ad7291_driver);
Sonic Zhangddaecd52010-10-27 21:43:55 -0400582
583MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
Michael Henneriche68b1db2011-09-02 17:25:45 +0100584MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
Sonic Zhangddaecd52010-10-27 21:43:55 -0400585MODULE_LICENSE("GPL v2");