drm/log: select CONFIG_FONT_SUPPORT
[drm/drm-misc.git] / drivers / iio / adc / ad7291.c
blob60e12faa32070183c64a2037c968177cb903cc11
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
5 * Copyright 2010-2011 Analog Devices Inc.
6 */
8 #include <linux/device.h>
9 #include <linux/err.h>
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/events.h>
24 * Simplified handling
26 * If no events enabled - single polled channel read
27 * If event enabled direct reads disable unless channel
28 * is in the read mask.
30 * The noise-delayed bit as per datasheet suggestion is always enabled.
34 * AD7291 registers definition
36 #define AD7291_COMMAND 0x00
37 #define AD7291_VOLTAGE 0x01
38 #define AD7291_T_SENSE 0x02
39 #define AD7291_T_AVERAGE 0x03
40 #define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4)
41 #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5)
42 #define AD7291_HYST(x) ((x) * 3 + 0x6)
43 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
44 #define AD7291_T_ALERT_STATUS 0x20
46 #define AD7291_BITS 12
47 #define AD7291_VOLTAGE_LIMIT_COUNT 8
51 * AD7291 command
53 #define AD7291_AUTOCYCLE BIT(0)
54 #define AD7291_RESET BIT(1)
55 #define AD7291_ALERT_CLEAR BIT(2)
56 #define AD7291_ALERT_POLARITY BIT(3)
57 #define AD7291_EXT_REF BIT(4)
58 #define AD7291_NOISE_DELAY BIT(5)
59 #define AD7291_T_SENSE_MASK BIT(7)
60 #define AD7291_VOLTAGE_MASK GENMASK(15, 8)
61 #define AD7291_VOLTAGE_OFFSET 8
64 * AD7291 value masks
66 #define AD7291_VALUE_MASK GENMASK(11, 0)
69 * AD7291 alert register bits
71 #define AD7291_T_LOW BIT(0)
72 #define AD7291_T_HIGH BIT(1)
73 #define AD7291_T_AVG_LOW BIT(2)
74 #define AD7291_T_AVG_HIGH BIT(3)
75 #define AD7291_V_LOW(x) BIT((x) * 2)
76 #define AD7291_V_HIGH(x) BIT((x) * 2 + 1)
79 struct ad7291_chip_info {
80 struct i2c_client *client;
81 struct regulator *reg;
82 u16 command;
83 u16 c_mask; /* Active voltage channels for events */
84 struct mutex state_lock;
87 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
89 struct i2c_client *client = chip->client;
90 int ret = 0;
92 ret = i2c_smbus_read_word_swapped(client, reg);
93 if (ret < 0) {
94 dev_err(&client->dev, "I2C read error\n");
95 return ret;
98 *data = ret;
100 return 0;
103 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
105 return i2c_smbus_write_word_swapped(chip->client, reg, data);
108 static irqreturn_t ad7291_event_handler(int irq, void *private)
110 struct iio_dev *indio_dev = private;
111 struct ad7291_chip_info *chip = iio_priv(private);
112 u16 t_status, v_status;
113 u16 command;
114 int i;
115 s64 timestamp = iio_get_time_ns(indio_dev);
117 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
118 return IRQ_HANDLED;
120 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
121 return IRQ_HANDLED;
123 if (!(t_status || v_status))
124 return IRQ_HANDLED;
126 command = chip->command | AD7291_ALERT_CLEAR;
127 ad7291_i2c_write(chip, AD7291_COMMAND, command);
129 command = chip->command & ~AD7291_ALERT_CLEAR;
130 ad7291_i2c_write(chip, AD7291_COMMAND, command);
132 /* For now treat t_sense and t_sense_average the same */
133 if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW))
134 iio_push_event(indio_dev,
135 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
137 IIO_EV_TYPE_THRESH,
138 IIO_EV_DIR_FALLING),
139 timestamp);
140 if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH))
141 iio_push_event(indio_dev,
142 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
144 IIO_EV_TYPE_THRESH,
145 IIO_EV_DIR_RISING),
146 timestamp);
148 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
149 if (v_status & AD7291_V_LOW(i))
150 iio_push_event(indio_dev,
151 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
153 IIO_EV_TYPE_THRESH,
154 IIO_EV_DIR_FALLING),
155 timestamp);
156 if (v_status & AD7291_V_HIGH(i))
157 iio_push_event(indio_dev,
158 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
160 IIO_EV_TYPE_THRESH,
161 IIO_EV_DIR_RISING),
162 timestamp);
165 return IRQ_HANDLED;
168 static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
169 enum iio_event_direction dir,
170 enum iio_event_info info)
172 unsigned int offset;
174 switch (chan->type) {
175 case IIO_VOLTAGE:
176 offset = chan->channel;
177 break;
178 case IIO_TEMP:
179 offset = AD7291_VOLTAGE_OFFSET;
180 break;
181 default:
182 return 0;
185 switch (info) {
186 case IIO_EV_INFO_VALUE:
187 if (dir == IIO_EV_DIR_FALLING)
188 return AD7291_DATA_HIGH(offset);
189 else
190 return AD7291_DATA_LOW(offset);
191 case IIO_EV_INFO_HYSTERESIS:
192 return AD7291_HYST(offset);
193 default:
194 break;
196 return 0;
199 static int ad7291_read_event_value(struct iio_dev *indio_dev,
200 const struct iio_chan_spec *chan,
201 enum iio_event_type type,
202 enum iio_event_direction dir,
203 enum iio_event_info info,
204 int *val, int *val2)
206 struct ad7291_chip_info *chip = iio_priv(indio_dev);
207 int ret;
208 u16 uval;
210 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
211 &uval);
212 if (ret < 0)
213 return ret;
215 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
216 *val = uval & AD7291_VALUE_MASK;
218 else
219 *val = sign_extend32(uval, 11);
221 return IIO_VAL_INT;
224 static int ad7291_write_event_value(struct iio_dev *indio_dev,
225 const struct iio_chan_spec *chan,
226 enum iio_event_type type,
227 enum iio_event_direction dir,
228 enum iio_event_info info,
229 int val, int val2)
231 struct ad7291_chip_info *chip = iio_priv(indio_dev);
233 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
234 if (val > AD7291_VALUE_MASK || val < 0)
235 return -EINVAL;
236 } else {
237 if (val > 2047 || val < -2048)
238 return -EINVAL;
241 return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
242 val);
245 static int ad7291_read_event_config(struct iio_dev *indio_dev,
246 const struct iio_chan_spec *chan,
247 enum iio_event_type type,
248 enum iio_event_direction dir)
250 struct ad7291_chip_info *chip = iio_priv(indio_dev);
252 * To be enabled the channel must simply be on. If any are enabled
253 * we are in continuous sampling mode
256 switch (chan->type) {
257 case IIO_VOLTAGE:
258 return !!(chip->c_mask & BIT(15 - chan->channel));
259 case IIO_TEMP:
260 /* always on */
261 return 1;
262 default:
263 return -EINVAL;
268 static int ad7291_write_event_config(struct iio_dev *indio_dev,
269 const struct iio_chan_spec *chan,
270 enum iio_event_type type,
271 enum iio_event_direction dir,
272 bool state)
274 int ret = 0;
275 struct ad7291_chip_info *chip = iio_priv(indio_dev);
276 unsigned int mask;
277 u16 regval;
279 mutex_lock(&chip->state_lock);
280 regval = chip->command;
282 * To be enabled the channel must simply be on. If any are enabled
283 * use continuous sampling mode.
284 * Possible to disable temp as well but that makes single read tricky.
287 mask = BIT(15 - chan->channel);
289 switch (chan->type) {
290 case IIO_VOLTAGE:
291 if ((!state) && (chip->c_mask & mask))
292 chip->c_mask &= ~mask;
293 else if (state && (!(chip->c_mask & mask)))
294 chip->c_mask |= mask;
295 else
296 break;
298 regval &= ~AD7291_AUTOCYCLE;
299 regval |= chip->c_mask;
300 if (chip->c_mask) /* Enable autocycle? */
301 regval |= AD7291_AUTOCYCLE;
303 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
304 if (ret < 0)
305 goto error_ret;
307 chip->command = regval;
308 break;
309 default:
310 ret = -EINVAL;
313 error_ret:
314 mutex_unlock(&chip->state_lock);
315 return ret;
318 static int ad7291_read_raw(struct iio_dev *indio_dev,
319 struct iio_chan_spec const *chan,
320 int *val,
321 int *val2,
322 long mask)
324 int ret;
325 struct ad7291_chip_info *chip = iio_priv(indio_dev);
326 u16 regval;
328 switch (mask) {
329 case IIO_CHAN_INFO_RAW:
330 switch (chan->type) {
331 case IIO_VOLTAGE:
332 mutex_lock(&chip->state_lock);
333 /* If in autocycle mode drop through */
334 if (chip->command & AD7291_AUTOCYCLE) {
335 mutex_unlock(&chip->state_lock);
336 return -EBUSY;
338 /* Enable this channel alone */
339 regval = chip->command & (~AD7291_VOLTAGE_MASK);
340 regval |= BIT(15 - chan->channel);
341 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
342 if (ret < 0) {
343 mutex_unlock(&chip->state_lock);
344 return ret;
346 /* Read voltage */
347 ret = i2c_smbus_read_word_swapped(chip->client,
348 AD7291_VOLTAGE);
349 if (ret < 0) {
350 mutex_unlock(&chip->state_lock);
351 return ret;
353 *val = ret & AD7291_VALUE_MASK;
354 mutex_unlock(&chip->state_lock);
355 return IIO_VAL_INT;
356 case IIO_TEMP:
357 /* Assumes tsense bit of command register always set */
358 ret = i2c_smbus_read_word_swapped(chip->client,
359 AD7291_T_SENSE);
360 if (ret < 0)
361 return ret;
362 *val = sign_extend32(ret, 11);
363 return IIO_VAL_INT;
364 default:
365 return -EINVAL;
367 case IIO_CHAN_INFO_AVERAGE_RAW:
368 ret = i2c_smbus_read_word_swapped(chip->client,
369 AD7291_T_AVERAGE);
370 if (ret < 0)
371 return ret;
372 *val = sign_extend32(ret, 11);
373 return IIO_VAL_INT;
374 case IIO_CHAN_INFO_SCALE:
375 switch (chan->type) {
376 case IIO_VOLTAGE:
377 if (chip->reg) {
378 int vref;
380 vref = regulator_get_voltage(chip->reg);
381 if (vref < 0)
382 return vref;
383 *val = vref / 1000;
384 } else {
385 *val = 2500;
387 *val2 = AD7291_BITS;
388 return IIO_VAL_FRACTIONAL_LOG2;
389 case IIO_TEMP:
391 * One LSB of the ADC corresponds to 0.25 deg C.
392 * The temperature reading is in 12-bit twos
393 * complement format
395 *val = 250;
396 return IIO_VAL_INT;
397 default:
398 return -EINVAL;
400 default:
401 return -EINVAL;
405 static const struct iio_event_spec ad7291_events[] = {
407 .type = IIO_EV_TYPE_THRESH,
408 .dir = IIO_EV_DIR_RISING,
409 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
410 BIT(IIO_EV_INFO_ENABLE),
411 }, {
412 .type = IIO_EV_TYPE_THRESH,
413 .dir = IIO_EV_DIR_FALLING,
414 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
415 BIT(IIO_EV_INFO_ENABLE),
416 }, {
417 .type = IIO_EV_TYPE_THRESH,
418 .dir = IIO_EV_DIR_EITHER,
419 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
423 #define AD7291_VOLTAGE_CHAN(_chan) \
425 .type = IIO_VOLTAGE, \
426 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
427 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
428 .indexed = 1, \
429 .channel = _chan, \
430 .event_spec = ad7291_events, \
431 .num_event_specs = ARRAY_SIZE(ad7291_events), \
434 static const struct iio_chan_spec ad7291_channels[] = {
435 AD7291_VOLTAGE_CHAN(0),
436 AD7291_VOLTAGE_CHAN(1),
437 AD7291_VOLTAGE_CHAN(2),
438 AD7291_VOLTAGE_CHAN(3),
439 AD7291_VOLTAGE_CHAN(4),
440 AD7291_VOLTAGE_CHAN(5),
441 AD7291_VOLTAGE_CHAN(6),
442 AD7291_VOLTAGE_CHAN(7),
444 .type = IIO_TEMP,
445 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
446 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
447 BIT(IIO_CHAN_INFO_SCALE),
448 .indexed = 1,
449 .channel = 0,
450 .event_spec = ad7291_events,
451 .num_event_specs = ARRAY_SIZE(ad7291_events),
455 static const struct iio_info ad7291_info = {
456 .read_raw = &ad7291_read_raw,
457 .read_event_config = &ad7291_read_event_config,
458 .write_event_config = &ad7291_write_event_config,
459 .read_event_value = &ad7291_read_event_value,
460 .write_event_value = &ad7291_write_event_value,
463 static void ad7291_reg_disable(void *reg)
465 regulator_disable(reg);
468 static int ad7291_probe(struct i2c_client *client)
470 const struct i2c_device_id *id = i2c_client_get_device_id(client);
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 mutex_init(&chip->state_lock);
482 chip->client = client;
484 chip->command = AD7291_NOISE_DELAY |
485 AD7291_T_SENSE_MASK | /* Tsense always enabled */
486 AD7291_ALERT_POLARITY; /* set irq polarity low level */
488 chip->reg = devm_regulator_get_optional(&client->dev, "vref");
489 if (IS_ERR(chip->reg)) {
490 if (PTR_ERR(chip->reg) != -ENODEV)
491 return PTR_ERR(chip->reg);
493 chip->reg = NULL;
496 if (chip->reg) {
497 ret = regulator_enable(chip->reg);
498 if (ret)
499 return ret;
501 ret = devm_add_action_or_reset(&client->dev, ad7291_reg_disable,
502 chip->reg);
503 if (ret)
504 return ret;
506 chip->command |= AD7291_EXT_REF;
509 indio_dev->name = id->name;
510 indio_dev->channels = ad7291_channels;
511 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
513 indio_dev->info = &ad7291_info;
514 indio_dev->modes = INDIO_DIRECT_MODE;
516 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
517 if (ret)
518 return -EIO;
520 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
521 if (ret)
522 return -EIO;
524 if (client->irq > 0) {
525 ret = devm_request_threaded_irq(&client->dev, client->irq,
526 NULL,
527 &ad7291_event_handler,
528 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
529 id->name,
530 indio_dev);
531 if (ret)
532 return ret;
535 return devm_iio_device_register(&client->dev, indio_dev);
538 static const struct i2c_device_id ad7291_id[] = {
539 { "ad7291" },
543 MODULE_DEVICE_TABLE(i2c, ad7291_id);
545 static const struct of_device_id ad7291_of_match[] = {
546 { .compatible = "adi,ad7291" },
549 MODULE_DEVICE_TABLE(of, ad7291_of_match);
551 static struct i2c_driver ad7291_driver = {
552 .driver = {
553 .name = KBUILD_MODNAME,
554 .of_match_table = ad7291_of_match,
556 .probe = ad7291_probe,
557 .id_table = ad7291_id,
559 module_i2c_driver(ad7291_driver);
561 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
562 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
563 MODULE_LICENSE("GPL v2");