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_chan_spec ad5421_channels
[] = {
89 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
90 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
91 BIT(IIO_CHAN_INFO_CALIBBIAS
),
92 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
) |
93 BIT(IIO_CHAN_INFO_OFFSET
),
94 .scan_type
= IIO_ST('u', 16, 16, 0),
95 .event_mask
= IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_RISING
) |
96 IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_FALLING
),
101 .event_mask
= IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_RISING
),
105 static int ad5421_write_unlocked(struct iio_dev
*indio_dev
,
106 unsigned int reg
, unsigned int val
)
108 struct ad5421_state
*st
= iio_priv(indio_dev
);
110 st
->data
[0].d32
= cpu_to_be32((reg
<< 16) | val
);
112 return spi_write(st
->spi
, &st
->data
[0].d8
[1], 3);
115 static int ad5421_write(struct iio_dev
*indio_dev
, unsigned int reg
,
120 mutex_lock(&indio_dev
->mlock
);
121 ret
= ad5421_write_unlocked(indio_dev
, reg
, val
);
122 mutex_unlock(&indio_dev
->mlock
);
127 static int ad5421_read(struct iio_dev
*indio_dev
, unsigned int reg
)
129 struct ad5421_state
*st
= iio_priv(indio_dev
);
131 struct spi_transfer t
[] = {
133 .tx_buf
= &st
->data
[0].d8
[1],
137 .rx_buf
= &st
->data
[1].d8
[1],
142 mutex_lock(&indio_dev
->mlock
);
144 st
->data
[0].d32
= cpu_to_be32((1 << 23) | (reg
<< 16));
146 ret
= spi_sync_transfer(st
->spi
, t
, ARRAY_SIZE(t
));
148 ret
= be32_to_cpu(st
->data
[1].d32
) & 0xffff;
150 mutex_unlock(&indio_dev
->mlock
);
155 static int ad5421_update_ctrl(struct iio_dev
*indio_dev
, unsigned int set
,
158 struct ad5421_state
*st
= iio_priv(indio_dev
);
161 mutex_lock(&indio_dev
->mlock
);
166 ret
= ad5421_write_unlocked(indio_dev
, AD5421_REG_CTRL
, st
->ctrl
);
168 mutex_unlock(&indio_dev
->mlock
);
173 static irqreturn_t
ad5421_fault_handler(int irq
, void *data
)
175 struct iio_dev
*indio_dev
= data
;
176 struct ad5421_state
*st
= iio_priv(indio_dev
);
178 unsigned int old_fault
= 0;
181 fault
= ad5421_read(indio_dev
, AD5421_REG_FAULT
);
185 /* If we had a fault, this might mean that the DAC has lost its state
186 * and has been reset. Make sure that the control register actually
187 * contains what we expect it to contain. Otherwise the watchdog might
188 * be enabled and we get watchdog timeout faults, which will render the
190 ad5421_update_ctrl(indio_dev
, 0, 0);
193 /* The fault pin stays high as long as a fault condition is present and
194 * it is not possible to mask fault conditions. For certain fault
195 * conditions for example like over-temperature it takes some time
196 * until the fault condition disappears. If we would exit the interrupt
197 * handler immediately after handling the event it would be entered
198 * again instantly. Thus we fall back to polling in case we detect that
199 * a interrupt condition is still present.
202 /* 0xffff is a invalid value for the register and will only be
203 * read if there has been a communication error */
207 /* we are only interested in new events */
208 events
= (old_fault
^ fault
) & fault
;
209 events
&= st
->fault_mask
;
211 if (events
& AD5421_FAULT_OVER_CURRENT
) {
212 iio_push_event(indio_dev
,
213 IIO_UNMOD_EVENT_CODE(IIO_CURRENT
,
220 if (events
& AD5421_FAULT_UNDER_CURRENT
) {
221 iio_push_event(indio_dev
,
222 IIO_UNMOD_EVENT_CODE(IIO_CURRENT
,
229 if (events
& AD5421_FAULT_TEMP_OVER_140
) {
230 iio_push_event(indio_dev
,
231 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
239 fault
= ad5421_read(indio_dev
, AD5421_REG_FAULT
);
241 /* still active? go to sleep for some time */
242 if (fault
& AD5421_FAULT_TRIGGER_IRQ
)
245 } while (fault
& AD5421_FAULT_TRIGGER_IRQ
);
251 static void ad5421_get_current_min_max(struct ad5421_state
*st
,
252 unsigned int *min
, unsigned int *max
)
254 /* The current range is configured using external pins, which are
255 * usually hard-wired and not run-time switchable. */
256 switch (st
->current_range
) {
257 case AD5421_CURRENT_RANGE_4mA_20mA
:
261 case AD5421_CURRENT_RANGE_3mA8_21mA
:
265 case AD5421_CURRENT_RANGE_3mA2_24mA
:
276 static inline unsigned int ad5421_get_offset(struct ad5421_state
*st
)
278 unsigned int min
, max
;
280 ad5421_get_current_min_max(st
, &min
, &max
);
281 return (min
* (1 << 16)) / (max
- min
);
284 static inline unsigned int ad5421_get_scale(struct ad5421_state
*st
)
286 unsigned int min
, max
;
288 ad5421_get_current_min_max(st
, &min
, &max
);
289 return ((max
- min
) * 1000) / (1 << 16);
292 static int ad5421_read_raw(struct iio_dev
*indio_dev
,
293 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long m
)
295 struct ad5421_state
*st
= iio_priv(indio_dev
);
298 if (chan
->type
!= IIO_CURRENT
)
302 case IIO_CHAN_INFO_RAW
:
303 ret
= ad5421_read(indio_dev
, AD5421_REG_DAC_DATA
);
308 case IIO_CHAN_INFO_SCALE
:
310 *val2
= ad5421_get_scale(st
);
311 return IIO_VAL_INT_PLUS_MICRO
;
312 case IIO_CHAN_INFO_OFFSET
:
313 *val
= ad5421_get_offset(st
);
315 case IIO_CHAN_INFO_CALIBBIAS
:
316 ret
= ad5421_read(indio_dev
, AD5421_REG_OFFSET
);
321 case IIO_CHAN_INFO_CALIBSCALE
:
322 ret
= ad5421_read(indio_dev
, AD5421_REG_GAIN
);
332 static int ad5421_write_raw(struct iio_dev
*indio_dev
,
333 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
335 const unsigned int max_val
= 1 << 16;
338 case IIO_CHAN_INFO_RAW
:
339 if (val
>= max_val
|| val
< 0)
342 return ad5421_write(indio_dev
, AD5421_REG_DAC_DATA
, val
);
343 case IIO_CHAN_INFO_CALIBBIAS
:
345 if (val
>= max_val
|| val
< 0)
348 return ad5421_write(indio_dev
, AD5421_REG_OFFSET
, val
);
349 case IIO_CHAN_INFO_CALIBSCALE
:
350 if (val
>= max_val
|| val
< 0)
353 return ad5421_write(indio_dev
, AD5421_REG_GAIN
, val
);
361 static int ad5421_write_event_config(struct iio_dev
*indio_dev
,
362 u64 event_code
, int state
)
364 struct ad5421_state
*st
= iio_priv(indio_dev
);
367 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
)) {
369 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
371 mask
= AD5421_FAULT_OVER_CURRENT
;
373 mask
= AD5421_FAULT_UNDER_CURRENT
;
376 mask
= AD5421_FAULT_TEMP_OVER_140
;
382 mutex_lock(&indio_dev
->mlock
);
384 st
->fault_mask
|= mask
;
386 st
->fault_mask
&= ~mask
;
387 mutex_unlock(&indio_dev
->mlock
);
392 static int ad5421_read_event_config(struct iio_dev
*indio_dev
,
395 struct ad5421_state
*st
= iio_priv(indio_dev
);
398 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
)) {
400 if (IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
402 mask
= AD5421_FAULT_OVER_CURRENT
;
404 mask
= AD5421_FAULT_UNDER_CURRENT
;
407 mask
= AD5421_FAULT_TEMP_OVER_140
;
413 return (bool)(st
->fault_mask
& mask
);
416 static int ad5421_read_event_value(struct iio_dev
*indio_dev
, u64 event_code
,
421 switch (IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event_code
)) {
423 ret
= ad5421_read(indio_dev
, AD5421_REG_DAC_DATA
);
438 static const struct iio_info ad5421_info
= {
439 .read_raw
= ad5421_read_raw
,
440 .write_raw
= ad5421_write_raw
,
441 .read_event_config
= ad5421_read_event_config
,
442 .write_event_config
= ad5421_write_event_config
,
443 .read_event_value
= ad5421_read_event_value
,
444 .driver_module
= THIS_MODULE
,
447 static int ad5421_probe(struct spi_device
*spi
)
449 struct ad5421_platform_data
*pdata
= dev_get_platdata(&spi
->dev
);
450 struct iio_dev
*indio_dev
;
451 struct ad5421_state
*st
;
454 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
455 if (indio_dev
== NULL
) {
456 dev_err(&spi
->dev
, "Failed to allocate iio device\n");
460 st
= iio_priv(indio_dev
);
461 spi_set_drvdata(spi
, indio_dev
);
465 indio_dev
->dev
.parent
= &spi
->dev
;
466 indio_dev
->name
= "ad5421";
467 indio_dev
->info
= &ad5421_info
;
468 indio_dev
->modes
= INDIO_DIRECT_MODE
;
469 indio_dev
->channels
= ad5421_channels
;
470 indio_dev
->num_channels
= ARRAY_SIZE(ad5421_channels
);
472 st
->ctrl
= AD5421_CTRL_WATCHDOG_DISABLE
|
473 AD5421_CTRL_AUTO_FAULT_READBACK
;
476 st
->current_range
= pdata
->current_range
;
477 if (pdata
->external_vref
)
478 st
->ctrl
|= AD5421_CTRL_PWR_DOWN_INT_VREF
;
480 st
->current_range
= AD5421_CURRENT_RANGE_4mA_20mA
;
483 /* write initial ctrl register value */
484 ad5421_update_ctrl(indio_dev
, 0, 0);
487 ret
= devm_request_threaded_irq(&spi
->dev
, spi
->irq
,
489 ad5421_fault_handler
,
490 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
497 ret
= iio_device_register(indio_dev
);
499 dev_err(&spi
->dev
, "Failed to register iio device: %d\n", ret
);
506 static int ad5421_remove(struct spi_device
*spi
)
508 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
510 iio_device_unregister(indio_dev
);
515 static struct spi_driver ad5421_driver
= {
518 .owner
= THIS_MODULE
,
520 .probe
= ad5421_probe
,
521 .remove
= ad5421_remove
,
523 module_spi_driver(ad5421_driver
);
525 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
526 MODULE_DESCRIPTION("Analog Devices AD5421 DAC");
527 MODULE_LICENSE("GPL v2");
528 MODULE_ALIAS("spi:ad5421");