sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / iio / adc / ad7291.c
blob1d90b02732bbea7b31e394e20d298c89b146e9c2
1 /*
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.
7 */
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>
27 * Simplified handling
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
54 * AD7291 command
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
67 * AD7291 value masks
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;
85 u16 command;
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;
93 int ret = 0;
95 ret = i2c_smbus_read_word_swapped(client, reg);
96 if (ret < 0) {
97 dev_err(&client->dev, "I2C read error\n");
98 return ret;
101 *data = ret;
103 return 0;
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;
116 u16 command;
117 int i;
118 s64 timestamp = iio_get_time_ns(indio_dev);
120 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
121 return IRQ_HANDLED;
123 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
124 return IRQ_HANDLED;
126 if (!(t_status || v_status))
127 return IRQ_HANDLED;
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,
140 IIO_EV_TYPE_THRESH,
141 IIO_EV_DIR_FALLING),
142 timestamp);
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,
147 IIO_EV_TYPE_THRESH,
148 IIO_EV_DIR_RISING),
149 timestamp);
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,
156 IIO_EV_TYPE_THRESH,
157 IIO_EV_DIR_FALLING),
158 timestamp);
159 if (v_status & AD7291_V_HIGH(i))
160 iio_push_event(indio_dev,
161 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
163 IIO_EV_TYPE_THRESH,
164 IIO_EV_DIR_RISING),
165 timestamp);
168 return IRQ_HANDLED;
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)
175 unsigned int offset;
177 switch (chan->type) {
178 case IIO_VOLTAGE:
179 offset = chan->channel;
180 break;
181 case IIO_TEMP:
182 offset = AD7291_VOLTAGE_OFFSET;
183 break;
184 default:
185 return 0;
188 switch (info) {
189 case IIO_EV_INFO_VALUE:
190 if (dir == IIO_EV_DIR_FALLING)
191 return AD7291_DATA_HIGH(offset);
192 else
193 return AD7291_DATA_LOW(offset);
194 case IIO_EV_INFO_HYSTERESIS:
195 return AD7291_HYST(offset);
196 default:
197 break;
199 return 0;
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,
207 int *val, int *val2)
209 struct ad7291_chip_info *chip = iio_priv(indio_dev);
210 int ret;
211 u16 uval;
213 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
214 &uval);
215 if (ret < 0)
216 return ret;
218 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
219 *val = uval & AD7291_VALUE_MASK;
221 else
222 *val = sign_extend32(uval, 11);
224 return IIO_VAL_INT;
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,
232 int val, int val2)
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)
238 return -EINVAL;
239 } else {
240 if (val > 2047 || val < -2048)
241 return -EINVAL;
244 return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
245 val);
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) {
260 case IIO_VOLTAGE:
261 return !!(chip->c_mask & BIT(15 - chan->channel));
262 case IIO_TEMP:
263 /* always on */
264 return 1;
265 default:
266 return -EINVAL;
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,
275 int state)
277 int ret = 0;
278 struct ad7291_chip_info *chip = iio_priv(indio_dev);
279 unsigned int mask;
280 u16 regval;
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) {
293 case IIO_VOLTAGE:
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;
298 else
299 break;
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);
307 if (ret < 0)
308 goto error_ret;
310 chip->command = regval;
311 break;
312 default:
313 ret = -EINVAL;
316 error_ret:
317 mutex_unlock(&chip->state_lock);
318 return ret;
321 static 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)
327 int ret;
328 struct ad7291_chip_info *chip = iio_priv(indio_dev);
329 u16 regval;
331 switch (mask) {
332 case IIO_CHAN_INFO_RAW:
333 switch (chan->type) {
334 case IIO_VOLTAGE:
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);
339 return -EBUSY;
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);
345 if (ret < 0) {
346 mutex_unlock(&chip->state_lock);
347 return ret;
349 /* Read voltage */
350 ret = i2c_smbus_read_word_swapped(chip->client,
351 AD7291_VOLTAGE);
352 if (ret < 0) {
353 mutex_unlock(&chip->state_lock);
354 return ret;
356 *val = ret & AD7291_VALUE_MASK;
357 mutex_unlock(&chip->state_lock);
358 return IIO_VAL_INT;
359 case IIO_TEMP:
360 /* Assumes tsense bit of command register always set */
361 ret = i2c_smbus_read_word_swapped(chip->client,
362 AD7291_T_SENSE);
363 if (ret < 0)
364 return ret;
365 *val = sign_extend32(ret, 11);
366 return IIO_VAL_INT;
367 default:
368 return -EINVAL;
370 case IIO_CHAN_INFO_AVERAGE_RAW:
371 ret = i2c_smbus_read_word_swapped(chip->client,
372 AD7291_T_AVERAGE);
373 if (ret < 0)
374 return ret;
375 *val = sign_extend32(ret, 11);
376 return IIO_VAL_INT;
377 case IIO_CHAN_INFO_SCALE:
378 switch (chan->type) {
379 case IIO_VOLTAGE:
380 if (chip->reg) {
381 int vref;
383 vref = regulator_get_voltage(chip->reg);
384 if (vref < 0)
385 return vref;
386 *val = vref / 1000;
387 } else {
388 *val = 2500;
390 *val2 = AD7291_BITS;
391 return IIO_VAL_FRACTIONAL_LOG2;
392 case IIO_TEMP:
394 * One LSB of the ADC corresponds to 0.25 deg C.
395 * The temperature reading is in 12-bit twos
396 * complement format
398 *val = 250;
399 return IIO_VAL_INT;
400 default:
401 return -EINVAL;
403 default:
404 return -EINVAL;
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),
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),
419 }, {
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), \
431 .indexed = 1, \
432 .channel = _chan, \
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),
447 .type = IIO_TEMP,
448 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
449 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
450 BIT(IIO_CHAN_INFO_SCALE),
451 .indexed = 1,
452 .channel = 0,
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;
473 int ret;
475 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
476 if (!indio_dev)
477 return -ENOMEM;
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);
486 if (ret)
487 return ret;
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->dev.of_node = client->dev.of_node;
509 indio_dev->info = &ad7291_info;
510 indio_dev->modes = INDIO_DIRECT_MODE;
512 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
513 if (ret) {
514 ret = -EIO;
515 goto error_disable_reg;
518 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
519 if (ret) {
520 ret = -EIO;
521 goto error_disable_reg;
524 if (client->irq > 0) {
525 ret = request_threaded_irq(client->irq,
526 NULL,
527 &ad7291_event_handler,
528 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
529 id->name,
530 indio_dev);
531 if (ret)
532 goto error_disable_reg;
535 ret = iio_device_register(indio_dev);
536 if (ret)
537 goto error_unreg_irq;
539 return 0;
541 error_unreg_irq:
542 if (client->irq)
543 free_irq(client->irq, indio_dev);
544 error_disable_reg:
545 if (chip->reg)
546 regulator_disable(chip->reg);
548 return ret;
551 static int ad7291_remove(struct i2c_client *client)
553 struct iio_dev *indio_dev = i2c_get_clientdata(client);
554 struct ad7291_chip_info *chip = iio_priv(indio_dev);
556 iio_device_unregister(indio_dev);
558 if (client->irq)
559 free_irq(client->irq, indio_dev);
561 if (chip->reg)
562 regulator_disable(chip->reg);
564 return 0;
567 static const struct i2c_device_id ad7291_id[] = {
568 { "ad7291", 0 },
572 MODULE_DEVICE_TABLE(i2c, ad7291_id);
574 static struct i2c_driver ad7291_driver = {
575 .driver = {
576 .name = KBUILD_MODNAME,
578 .probe = ad7291_probe,
579 .remove = ad7291_remove,
580 .id_table = ad7291_id,
582 module_i2c_driver(ad7291_driver);
584 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
585 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
586 MODULE_LICENSE("GPL v2");