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>
26 #define AD5421_REG_DAC_DATA 0x1
27 #define AD5421_REG_CTRL 0x2
28 #define AD5421_REG_OFFSET 0x3
29 #define AD5421_REG_GAIN 0x4
30 /* load dac and fault shared the same register number. Writing to it will cause
31 * a dac load command, reading from it will return the fault status register */
32 #define AD5421_REG_LOAD_DAC 0x5
33 #define AD5421_REG_FAULT 0x5
34 #define AD5421_REG_FORCE_ALARM_CURRENT 0x6
35 #define AD5421_REG_RESET 0x7
36 #define AD5421_REG_START_CONVERSION 0x8
37 #define AD5421_REG_NOOP 0x9
39 #define AD5421_CTRL_WATCHDOG_DISABLE BIT(12)
40 #define AD5421_CTRL_AUTO_FAULT_READBACK BIT(11)
41 #define AD5421_CTRL_MIN_CURRENT BIT(9)
42 #define AD5421_CTRL_ADC_SOURCE_TEMP BIT(8)
43 #define AD5421_CTRL_ADC_ENABLE BIT(7)
44 #define AD5421_CTRL_PWR_DOWN_INT_VREF BIT(6)
46 #define AD5421_FAULT_SPI BIT(15)
47 #define AD5421_FAULT_PEC BIT(14)
48 #define AD5421_FAULT_OVER_CURRENT BIT(13)
49 #define AD5421_FAULT_UNDER_CURRENT BIT(12)
50 #define AD5421_FAULT_TEMP_OVER_140 BIT(11)
51 #define AD5421_FAULT_TEMP_OVER_100 BIT(10)
52 #define AD5421_FAULT_UNDER_VOLTAGE_6V BIT(9)
53 #define AD5421_FAULT_UNDER_VOLTAGE_12V BIT(8)
55 /* These bits will cause the fault pin to go high */
56 #define AD5421_FAULT_TRIGGER_IRQ \
57 (AD5421_FAULT_SPI | AD5421_FAULT_PEC | AD5421_FAULT_OVER_CURRENT | \
58 AD5421_FAULT_UNDER_CURRENT | AD5421_FAULT_TEMP_OVER_140)
61 * struct ad5421_state - driver instance specific data
63 * @ctrl: control register cache
64 * @current_range: current range which the device is configured for
65 * @data: spi transfer buffers
66 * @fault_mask: software masking of events
69 struct spi_device *spi;
71 enum ad5421_current_range current_range;
72 unsigned int fault_mask;
75 * DMA (thus cache coherency maintenance) requires the
76 * transfer buffers to live in their own cache lines.
81 } data[2] ____cacheline_aligned;
84 static const struct iio_chan_spec ad5421_channels[] = {
90 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
91 IIO_CHAN_INFO_SCALE_SHARED_BIT |
92 IIO_CHAN_INFO_OFFSET_SHARED_BIT |
93 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |
94 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
95 .scan_type = IIO_ST('u', 16, 16, 0),
96 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) |
97 IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
102 .event_mask = IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
106 static int ad5421_write_unlocked(struct iio_dev *indio_dev,
107 unsigned int reg, unsigned int val)
109 struct ad5421_state *st = iio_priv(indio_dev);
111 st->data[0].d32 = cpu_to_be32((reg << 16) | val);
113 return spi_write(st->spi, &st->data[0].d8[1], 3);
116 static int ad5421_write(struct iio_dev *indio_dev, unsigned int reg,
121 mutex_lock(&indio_dev->mlock);
122 ret = ad5421_write_unlocked(indio_dev, reg, val);
123 mutex_unlock(&indio_dev->mlock);
128 static int ad5421_read(struct iio_dev *indio_dev, unsigned int reg)
130 struct ad5421_state *st = iio_priv(indio_dev);
131 struct spi_message m;
133 struct spi_transfer t[] = {
135 .tx_buf = &st->data[0].d8[1],
139 .rx_buf = &st->data[1].d8[1],
144 spi_message_init(&m);
145 spi_message_add_tail(&t[0], &m);
146 spi_message_add_tail(&t[1], &m);
148 mutex_lock(&indio_dev->mlock);
150 st->data[0].d32 = cpu_to_be32((1 << 23) | (reg << 16));
152 ret = spi_sync(st->spi, &m);
154 ret = be32_to_cpu(st->data[1].d32) & 0xffff;
156 mutex_unlock(&indio_dev->mlock);
161 static int ad5421_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
164 struct ad5421_state *st = iio_priv(indio_dev);
167 mutex_lock(&indio_dev->mlock);
172 ret = ad5421_write_unlocked(indio_dev, AD5421_REG_CTRL, st->ctrl);
174 mutex_unlock(&indio_dev->mlock);
179 static irqreturn_t ad5421_fault_handler(int irq, void *data)
181 struct iio_dev *indio_dev = data;
182 struct ad5421_state *st = iio_priv(indio_dev);
184 unsigned int old_fault = 0;
187 fault = ad5421_read(indio_dev, AD5421_REG_FAULT);
191 /* If we had a fault, this might mean that the DAC has lost its state
192 * and has been reset. Make sure that the control register actually
193 * contains what we expect it to contain. Otherwise the watchdog might
194 * be enabled and we get watchdog timeout faults, which will render the
196 ad5421_update_ctrl(indio_dev, 0, 0);
199 /* The fault pin stays high as long as a fault condition is present and
200 * it is not possible to mask fault conditions. For certain fault
201 * conditions for example like over-temperature it takes some time
202 * until the fault condition disappears. If we would exit the interrupt
203 * handler immediately after handling the event it would be entered
204 * again instantly. Thus we fall back to polling in case we detect that
205 * a interrupt condition is still present.
208 /* 0xffff is a invalid value for the register and will only be
209 * read if there has been a communication error */
213 /* we are only interested in new events */
214 events = (old_fault ^ fault) & fault;
215 events &= st->fault_mask;
217 if (events & AD5421_FAULT_OVER_CURRENT) {
218 iio_push_event(indio_dev,
219 IIO_UNMOD_EVENT_CODE(IIO_CURRENT,
226 if (events & AD5421_FAULT_UNDER_CURRENT) {
227 iio_push_event(indio_dev,
228 IIO_UNMOD_EVENT_CODE(IIO_CURRENT,
235 if (events & AD5421_FAULT_TEMP_OVER_140) {
236 iio_push_event(indio_dev,
237 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
245 fault = ad5421_read(indio_dev, AD5421_REG_FAULT);
247 /* still active? go to sleep for some time */
248 if (fault & AD5421_FAULT_TRIGGER_IRQ)
251 } while (fault & AD5421_FAULT_TRIGGER_IRQ);
257 static void ad5421_get_current_min_max(struct ad5421_state *st,
258 unsigned int *min, unsigned int *max)
260 /* The current range is configured using external pins, which are
261 * usually hard-wired and not run-time switchable. */
262 switch (st->current_range) {
263 case AD5421_CURRENT_RANGE_4mA_20mA:
267 case AD5421_CURRENT_RANGE_3mA8_21mA:
271 case AD5421_CURRENT_RANGE_3mA2_24mA:
282 static inline unsigned int ad5421_get_offset(struct ad5421_state *st)
284 unsigned int min, max;
286 ad5421_get_current_min_max(st, &min, &max);
287 return (min * (1 << 16)) / (max - min);
290 static inline unsigned int ad5421_get_scale(struct ad5421_state *st)
292 unsigned int min, max;
294 ad5421_get_current_min_max(st, &min, &max);
295 return ((max - min) * 1000) / (1 << 16);
298 static int ad5421_read_raw(struct iio_dev *indio_dev,
299 struct iio_chan_spec const *chan, int *val, int *val2, long m)
301 struct ad5421_state *st = iio_priv(indio_dev);
304 if (chan->type != IIO_CURRENT)
308 case IIO_CHAN_INFO_RAW:
309 ret = ad5421_read(indio_dev, AD5421_REG_DAC_DATA);
314 case IIO_CHAN_INFO_SCALE:
316 *val2 = ad5421_get_scale(st);
317 return IIO_VAL_INT_PLUS_MICRO;
318 case IIO_CHAN_INFO_OFFSET:
319 *val = ad5421_get_offset(st);
321 case IIO_CHAN_INFO_CALIBBIAS:
322 ret = ad5421_read(indio_dev, AD5421_REG_OFFSET);
327 case IIO_CHAN_INFO_CALIBSCALE:
328 ret = ad5421_read(indio_dev, AD5421_REG_GAIN);
338 static int ad5421_write_raw(struct iio_dev *indio_dev,
339 struct iio_chan_spec const *chan, int val, int val2, long mask)
341 const unsigned int max_val = 1 << 16;
344 case IIO_CHAN_INFO_RAW:
345 if (val >= max_val || val < 0)
348 return ad5421_write(indio_dev, AD5421_REG_DAC_DATA, val);
349 case IIO_CHAN_INFO_CALIBBIAS:
351 if (val >= max_val || val < 0)
354 return ad5421_write(indio_dev, AD5421_REG_OFFSET, val);
355 case IIO_CHAN_INFO_CALIBSCALE:
356 if (val >= max_val || val < 0)
359 return ad5421_write(indio_dev, AD5421_REG_GAIN, val);
367 static int ad5421_write_event_config(struct iio_dev *indio_dev,
368 u64 event_code, int state)
370 struct ad5421_state *st = iio_priv(indio_dev);
373 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
375 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
377 mask = AD5421_FAULT_OVER_CURRENT;
379 mask = AD5421_FAULT_UNDER_CURRENT;
382 mask = AD5421_FAULT_TEMP_OVER_140;
388 mutex_lock(&indio_dev->mlock);
390 st->fault_mask |= mask;
392 st->fault_mask &= ~mask;
393 mutex_unlock(&indio_dev->mlock);
398 static int ad5421_read_event_config(struct iio_dev *indio_dev,
401 struct ad5421_state *st = iio_priv(indio_dev);
404 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
406 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) ==
408 mask = AD5421_FAULT_OVER_CURRENT;
410 mask = AD5421_FAULT_UNDER_CURRENT;
413 mask = AD5421_FAULT_TEMP_OVER_140;
419 return (bool)(st->fault_mask & mask);
422 static int ad5421_read_event_value(struct iio_dev *indio_dev, u64 event_code,
427 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code)) {
429 ret = ad5421_read(indio_dev, AD5421_REG_DAC_DATA);
444 static const struct iio_info ad5421_info = {
445 .read_raw = ad5421_read_raw,
446 .write_raw = ad5421_write_raw,
447 .read_event_config = ad5421_read_event_config,
448 .write_event_config = ad5421_write_event_config,
449 .read_event_value = ad5421_read_event_value,
450 .driver_module = THIS_MODULE,
453 static int __devinit ad5421_probe(struct spi_device *spi)
455 struct ad5421_platform_data *pdata = dev_get_platdata(&spi->dev);
456 struct iio_dev *indio_dev;
457 struct ad5421_state *st;
460 indio_dev = iio_device_alloc(sizeof(*st));
461 if (indio_dev == NULL) {
462 dev_err(&spi->dev, "Failed to allocate iio device\n");
466 st = iio_priv(indio_dev);
467 spi_set_drvdata(spi, indio_dev);
471 indio_dev->dev.parent = &spi->dev;
472 indio_dev->name = "ad5421";
473 indio_dev->info = &ad5421_info;
474 indio_dev->modes = INDIO_DIRECT_MODE;
475 indio_dev->channels = ad5421_channels;
476 indio_dev->num_channels = ARRAY_SIZE(ad5421_channels);
478 st->ctrl = AD5421_CTRL_WATCHDOG_DISABLE |
479 AD5421_CTRL_AUTO_FAULT_READBACK;
482 st->current_range = pdata->current_range;
483 if (pdata->external_vref)
484 st->ctrl |= AD5421_CTRL_PWR_DOWN_INT_VREF;
486 st->current_range = AD5421_CURRENT_RANGE_4mA_20mA;
489 /* write initial ctrl register value */
490 ad5421_update_ctrl(indio_dev, 0, 0);
493 ret = request_threaded_irq(spi->irq,
495 ad5421_fault_handler,
496 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
503 ret = iio_device_register(indio_dev);
505 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
513 free_irq(spi->irq, indio_dev);
515 iio_device_free(indio_dev);
520 static int __devexit ad5421_remove(struct spi_device *spi)
522 struct iio_dev *indio_dev = spi_get_drvdata(spi);
524 iio_device_unregister(indio_dev);
526 free_irq(spi->irq, indio_dev);
527 iio_device_free(indio_dev);
532 static struct spi_driver ad5421_driver = {
535 .owner = THIS_MODULE,
537 .probe = ad5421_probe,
538 .remove = __devexit_p(ad5421_remove),
540 module_spi_driver(ad5421_driver);
542 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
543 MODULE_DESCRIPTION("Analog Devices AD5421 DAC");
544 MODULE_LICENSE("GPL v2");
545 MODULE_ALIAS("spi:ad5421");