2 * AD5421 Digital to analog converters driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/device.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.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>
22 #include <linux/iio/dac/ad5421.h>
25 #define AD5421_REG_DAC_DATA 0x1
26 #define AD5421_REG_CTRL 0x2
27 #define AD5421_REG_OFFSET 0x3
28 #define AD5421_REG_GAIN 0x4
29 /* load dac and fault shared the same register number. Writing to it will cause
30 * a dac load command, reading from it will return the fault status register */
31 #define AD5421_REG_LOAD_DAC 0x5
32 #define AD5421_REG_FAULT 0x5
33 #define AD5421_REG_FORCE_ALARM_CURRENT 0x6
34 #define AD5421_REG_RESET 0x7
35 #define AD5421_REG_START_CONVERSION 0x8
36 #define AD5421_REG_NOOP 0x9
38 #define AD5421_CTRL_WATCHDOG_DISABLE BIT(12)
39 #define AD5421_CTRL_AUTO_FAULT_READBACK BIT(11)
40 #define AD5421_CTRL_MIN_CURRENT BIT(9)
41 #define AD5421_CTRL_ADC_SOURCE_TEMP BIT(8)
42 #define AD5421_CTRL_ADC_ENABLE BIT(7)
43 #define AD5421_CTRL_PWR_DOWN_INT_VREF BIT(6)
45 #define AD5421_FAULT_SPI BIT(15)
46 #define AD5421_FAULT_PEC BIT(14)
47 #define AD5421_FAULT_OVER_CURRENT BIT(13)
48 #define AD5421_FAULT_UNDER_CURRENT BIT(12)
49 #define AD5421_FAULT_TEMP_OVER_140 BIT(11)
50 #define AD5421_FAULT_TEMP_OVER_100 BIT(10)
51 #define AD5421_FAULT_UNDER_VOLTAGE_6V BIT(9)
52 #define AD5421_FAULT_UNDER_VOLTAGE_12V BIT(8)
54 /* These bits will cause the fault pin to go high */
55 #define AD5421_FAULT_TRIGGER_IRQ \
56 (AD5421_FAULT_SPI | AD5421_FAULT_PEC | AD5421_FAULT_OVER_CURRENT | \
57 AD5421_FAULT_UNDER_CURRENT | AD5421_FAULT_TEMP_OVER_140)
60 * struct ad5421_state - driver instance specific data
62 * @ctrl: control register cache
63 * @current_range: current range which the device is configured for
64 * @data: spi transfer buffers
65 * @fault_mask: software masking of events
68 struct spi_device *spi;
70 enum ad5421_current_range current_range;
71 unsigned int fault_mask;
74 * DMA (thus cache coherency maintenance) requires the
75 * transfer buffers to live in their own cache lines.
80 } data[2] ____cacheline_aligned;
83 static const struct iio_event_spec ad5421_current_event[] = {
85 .type = IIO_EV_TYPE_THRESH,
86 .dir = IIO_EV_DIR_RISING,
87 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
88 BIT(IIO_EV_INFO_ENABLE),
90 .type = IIO_EV_TYPE_THRESH,
91 .dir = IIO_EV_DIR_FALLING,
92 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
93 BIT(IIO_EV_INFO_ENABLE),
97 static const struct iio_event_spec ad5421_temp_event[] = {
99 .type = IIO_EV_TYPE_THRESH,
100 .dir = IIO_EV_DIR_RISING,
101 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
102 BIT(IIO_EV_INFO_ENABLE),
106 static const struct iio_chan_spec ad5421_channels[] = {
112 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
113 BIT(IIO_CHAN_INFO_CALIBSCALE) |
114 BIT(IIO_CHAN_INFO_CALIBBIAS),
115 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
116 BIT(IIO_CHAN_INFO_OFFSET),
122 .event_spec = ad5421_current_event,
123 .num_event_specs = ARRAY_SIZE(ad5421_current_event),
128 .event_spec = ad5421_temp_event,
129 .num_event_specs = ARRAY_SIZE(ad5421_temp_event),
133 static int ad5421_write_unlocked(struct iio_dev *indio_dev,
134 unsigned int reg, unsigned int val)
136 struct ad5421_state *st = iio_priv(indio_dev);
138 st->data[0].d32 = cpu_to_be32((reg << 16) | val);
140 return spi_write(st->spi, &st->data[0].d8[1], 3);
143 static int ad5421_write(struct iio_dev *indio_dev, unsigned int reg,
148 mutex_lock(&indio_dev->mlock);
149 ret = ad5421_write_unlocked(indio_dev, reg, val);
150 mutex_unlock(&indio_dev->mlock);
155 static int ad5421_read(struct iio_dev *indio_dev, unsigned int reg)
157 struct ad5421_state *st = iio_priv(indio_dev);
159 struct spi_transfer t[] = {
161 .tx_buf = &st->data[0].d8[1],
165 .rx_buf = &st->data[1].d8[1],
170 mutex_lock(&indio_dev->mlock);
172 st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16));
174 ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
176 ret = be32_to_cpu(st->data[1].d32) & 0xffff;
178 mutex_unlock(&indio_dev->mlock);
183 static int ad5421_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
186 struct ad5421_state *st = iio_priv(indio_dev);
189 mutex_lock(&indio_dev->mlock);
194 ret = ad5421_write_unlocked(indio_dev, AD5421_REG_CTRL, st->ctrl);
196 mutex_unlock(&indio_dev->mlock);
201 static irqreturn_t ad5421_fault_handler(int irq, void *data)
203 struct iio_dev *indio_dev = data;
204 struct ad5421_state *st = iio_priv(indio_dev);
206 unsigned int old_fault = 0;
209 fault = ad5421_read(indio_dev, AD5421_REG_FAULT);
213 /* If we had a fault, this might mean that the DAC has lost its state
214 * and has been reset. Make sure that the control register actually
215 * contains what we expect it to contain. Otherwise the watchdog might
216 * be enabled and we get watchdog timeout faults, which will render the
218 ad5421_update_ctrl(indio_dev, 0, 0);
221 /* The fault pin stays high as long as a fault condition is present and
222 * it is not possible to mask fault conditions. For certain fault
223 * conditions for example like over-temperature it takes some time
224 * until the fault condition disappears. If we would exit the interrupt
225 * handler immediately after handling the event it would be entered
226 * again instantly. Thus we fall back to polling in case we detect that
227 * a interrupt condition is still present.
230 /* 0xffff is a invalid value for the register and will only be
231 * read if there has been a communication error */
235 /* we are only interested in new events */
236 events = (old_fault ^ fault) & fault;
237 events &= st->fault_mask;
239 if (events & AD5421_FAULT_OVER_CURRENT) {
240 iio_push_event(indio_dev,
241 IIO_UNMOD_EVENT_CODE(IIO_CURRENT,
248 if (events & AD5421_FAULT_UNDER_CURRENT) {
249 iio_push_event(indio_dev,
250 IIO_UNMOD_EVENT_CODE(IIO_CURRENT,
257 if (events & AD5421_FAULT_TEMP_OVER_140) {
258 iio_push_event(indio_dev,
259 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
267 fault = ad5421_read(indio_dev, AD5421_REG_FAULT);
269 /* still active? go to sleep for some time */
270 if (fault & AD5421_FAULT_TRIGGER_IRQ)
273 } while (fault & AD5421_FAULT_TRIGGER_IRQ);
279 static void ad5421_get_current_min_max(struct ad5421_state *st,
280 unsigned int *min, unsigned int *max)
282 /* The current range is configured using external pins, which are
283 * usually hard-wired and not run-time switchable. */
284 switch (st->current_range) {
285 case AD5421_CURRENT_RANGE_4mA_20mA:
289 case AD5421_CURRENT_RANGE_3mA8_21mA:
293 case AD5421_CURRENT_RANGE_3mA2_24mA:
304 static inline unsigned int ad5421_get_offset(struct ad5421_state *st)
306 unsigned int min, max;
308 ad5421_get_current_min_max(st, &min, &max);
309 return (min * (1 << 16)) / (max - min);
312 static int ad5421_read_raw(struct iio_dev *indio_dev,
313 struct iio_chan_spec const *chan, int *val, int *val2, long m)
315 struct ad5421_state *st = iio_priv(indio_dev);
316 unsigned int min, max;
319 if (chan->type != IIO_CURRENT)
323 case IIO_CHAN_INFO_RAW:
324 ret = ad5421_read(indio_dev, AD5421_REG_DAC_DATA);
329 case IIO_CHAN_INFO_SCALE:
330 ad5421_get_current_min_max(st, &min, &max);
332 *val2 = (1 << 16) * 1000;
333 return IIO_VAL_FRACTIONAL;
334 case IIO_CHAN_INFO_OFFSET:
335 *val = ad5421_get_offset(st);
337 case IIO_CHAN_INFO_CALIBBIAS:
338 ret = ad5421_read(indio_dev, AD5421_REG_OFFSET);
343 case IIO_CHAN_INFO_CALIBSCALE:
344 ret = ad5421_read(indio_dev, AD5421_REG_GAIN);
354 static int ad5421_write_raw(struct iio_dev *indio_dev,
355 struct iio_chan_spec const *chan, int val, int val2, long mask)
357 const unsigned int max_val = 1 << 16;
360 case IIO_CHAN_INFO_RAW:
361 if (val >= max_val || val < 0)
364 return ad5421_write(indio_dev, AD5421_REG_DAC_DATA, val);
365 case IIO_CHAN_INFO_CALIBBIAS:
367 if (val >= max_val || val < 0)
370 return ad5421_write(indio_dev, AD5421_REG_OFFSET, val);
371 case IIO_CHAN_INFO_CALIBSCALE:
372 if (val >= max_val || val < 0)
375 return ad5421_write(indio_dev, AD5421_REG_GAIN, val);
383 static int ad5421_write_event_config(struct iio_dev *indio_dev,
384 const struct iio_chan_spec *chan, enum iio_event_type type,
385 enum iio_event_direction dir, int state)
387 struct ad5421_state *st = iio_priv(indio_dev);
390 switch (chan->type) {
392 if (dir == IIO_EV_DIR_RISING)
393 mask = AD5421_FAULT_OVER_CURRENT;
395 mask = AD5421_FAULT_UNDER_CURRENT;
398 mask = AD5421_FAULT_TEMP_OVER_140;
404 mutex_lock(&indio_dev->mlock);
406 st->fault_mask |= mask;
408 st->fault_mask &= ~mask;
409 mutex_unlock(&indio_dev->mlock);
414 static int ad5421_read_event_config(struct iio_dev *indio_dev,
415 const struct iio_chan_spec *chan, enum iio_event_type type,
416 enum iio_event_direction dir)
418 struct ad5421_state *st = iio_priv(indio_dev);
421 switch (chan->type) {
423 if (dir == IIO_EV_DIR_RISING)
424 mask = AD5421_FAULT_OVER_CURRENT;
426 mask = AD5421_FAULT_UNDER_CURRENT;
429 mask = AD5421_FAULT_TEMP_OVER_140;
435 return (bool)(st->fault_mask & mask);
438 static int ad5421_read_event_value(struct iio_dev *indio_dev,
439 const struct iio_chan_spec *chan, enum iio_event_type type,
440 enum iio_event_direction dir, enum iio_event_info info, int *val,
445 switch (chan->type) {
447 ret = ad5421_read(indio_dev, AD5421_REG_DAC_DATA);
462 static const struct iio_info ad5421_info = {
463 .read_raw = ad5421_read_raw,
464 .write_raw = ad5421_write_raw,
465 .read_event_config = ad5421_read_event_config,
466 .write_event_config = ad5421_write_event_config,
467 .read_event_value = ad5421_read_event_value,
468 .driver_module = THIS_MODULE,
471 static int ad5421_probe(struct spi_device *spi)
473 struct ad5421_platform_data *pdata = dev_get_platdata(&spi->dev);
474 struct iio_dev *indio_dev;
475 struct ad5421_state *st;
478 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
479 if (indio_dev == NULL) {
480 dev_err(&spi->dev, "Failed to allocate iio device\n");
484 st = iio_priv(indio_dev);
485 spi_set_drvdata(spi, indio_dev);
489 indio_dev->dev.parent = &spi->dev;
490 indio_dev->name = "ad5421";
491 indio_dev->info = &ad5421_info;
492 indio_dev->modes = INDIO_DIRECT_MODE;
493 indio_dev->channels = ad5421_channels;
494 indio_dev->num_channels = ARRAY_SIZE(ad5421_channels);
496 st->ctrl = AD5421_CTRL_WATCHDOG_DISABLE |
497 AD5421_CTRL_AUTO_FAULT_READBACK;
500 st->current_range = pdata->current_range;
501 if (pdata->external_vref)
502 st->ctrl |= AD5421_CTRL_PWR_DOWN_INT_VREF;
504 st->current_range = AD5421_CURRENT_RANGE_4mA_20mA;
507 /* write initial ctrl register value */
508 ad5421_update_ctrl(indio_dev, 0, 0);
511 ret = devm_request_threaded_irq(&spi->dev, spi->irq,
513 ad5421_fault_handler,
514 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
521 return devm_iio_device_register(&spi->dev, indio_dev);
524 static struct spi_driver ad5421_driver = {
528 .probe = ad5421_probe,
530 module_spi_driver(ad5421_driver);
533 MODULE_DESCRIPTION("Analog Devices AD5421 DAC");
534 MODULE_LICENSE("GPL v2");
535 MODULE_ALIAS("spi:ad5421");