1 // SPDX-License-Identifier: GPL-2.0-only
3 * AD5421 Digital to analog converters driver
5 * Copyright 2011 Analog Devices Inc.
8 #include <linux/device.h>
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/events.h>
21 #include <linux/iio/dac/ad5421.h>
24 #define AD5421_REG_DAC_DATA 0x1
25 #define AD5421_REG_CTRL 0x2
26 #define AD5421_REG_OFFSET 0x3
27 #define AD5421_REG_GAIN 0x4
28 /* load dac and fault shared the same register number. Writing to it will cause
29 * a dac load command, reading from it will return the fault status register */
30 #define AD5421_REG_LOAD_DAC 0x5
31 #define AD5421_REG_FAULT 0x5
32 #define AD5421_REG_FORCE_ALARM_CURRENT 0x6
33 #define AD5421_REG_RESET 0x7
34 #define AD5421_REG_START_CONVERSION 0x8
35 #define AD5421_REG_NOOP 0x9
37 #define AD5421_CTRL_WATCHDOG_DISABLE BIT(12)
38 #define AD5421_CTRL_AUTO_FAULT_READBACK BIT(11)
39 #define AD5421_CTRL_MIN_CURRENT BIT(9)
40 #define AD5421_CTRL_ADC_SOURCE_TEMP BIT(8)
41 #define AD5421_CTRL_ADC_ENABLE BIT(7)
42 #define AD5421_CTRL_PWR_DOWN_INT_VREF BIT(6)
44 #define AD5421_FAULT_SPI BIT(15)
45 #define AD5421_FAULT_PEC BIT(14)
46 #define AD5421_FAULT_OVER_CURRENT BIT(13)
47 #define AD5421_FAULT_UNDER_CURRENT BIT(12)
48 #define AD5421_FAULT_TEMP_OVER_140 BIT(11)
49 #define AD5421_FAULT_TEMP_OVER_100 BIT(10)
50 #define AD5421_FAULT_UNDER_VOLTAGE_6V BIT(9)
51 #define AD5421_FAULT_UNDER_VOLTAGE_12V BIT(8)
53 /* These bits will cause the fault pin to go high */
54 #define AD5421_FAULT_TRIGGER_IRQ \
55 (AD5421_FAULT_SPI | AD5421_FAULT_PEC | AD5421_FAULT_OVER_CURRENT | \
56 AD5421_FAULT_UNDER_CURRENT | AD5421_FAULT_TEMP_OVER_140)
59 * struct ad5421_state - driver instance specific data
61 * @ctrl: control register cache
62 * @current_range: current range which the device is configured for
63 * @data: spi transfer buffers
64 * @fault_mask: software masking of events
65 * @lock: lock to protect the data buffer during SPI ops
68 struct spi_device
*spi
;
70 enum ad5421_current_range current_range
;
71 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_event_spec ad5421_current_event
[] = {
86 .type
= IIO_EV_TYPE_THRESH
,
87 .dir
= IIO_EV_DIR_RISING
,
88 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
89 BIT(IIO_EV_INFO_ENABLE
),
91 .type
= IIO_EV_TYPE_THRESH
,
92 .dir
= IIO_EV_DIR_FALLING
,
93 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
94 BIT(IIO_EV_INFO_ENABLE
),
98 static const struct iio_event_spec ad5421_temp_event
[] = {
100 .type
= IIO_EV_TYPE_THRESH
,
101 .dir
= IIO_EV_DIR_RISING
,
102 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
103 BIT(IIO_EV_INFO_ENABLE
),
107 static const struct iio_chan_spec ad5421_channels
[] = {
113 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
114 BIT(IIO_CHAN_INFO_CALIBSCALE
) |
115 BIT(IIO_CHAN_INFO_CALIBBIAS
),
116 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
) |
117 BIT(IIO_CHAN_INFO_OFFSET
),
123 .event_spec
= ad5421_current_event
,
124 .num_event_specs
= ARRAY_SIZE(ad5421_current_event
),
129 .event_spec
= ad5421_temp_event
,
130 .num_event_specs
= ARRAY_SIZE(ad5421_temp_event
),
134 static int ad5421_write_unlocked(struct iio_dev
*indio_dev
,
135 unsigned int reg
, unsigned int val
)
137 struct ad5421_state
*st
= iio_priv(indio_dev
);
139 st
->data
[0].d32
= cpu_to_be32((reg
<< 16) | val
);
141 return spi_write(st
->spi
, &st
->data
[0].d8
[1], 3);
144 static int ad5421_write(struct iio_dev
*indio_dev
, unsigned int reg
,
147 struct ad5421_state
*st
= iio_priv(indio_dev
);
150 mutex_lock(&st
->lock
);
151 ret
= ad5421_write_unlocked(indio_dev
, reg
, val
);
152 mutex_unlock(&st
->lock
);
157 static int ad5421_read(struct iio_dev
*indio_dev
, unsigned int reg
)
159 struct ad5421_state
*st
= iio_priv(indio_dev
);
161 struct spi_transfer t
[] = {
163 .tx_buf
= &st
->data
[0].d8
[1],
167 .rx_buf
= &st
->data
[1].d8
[1],
172 mutex_lock(&st
->lock
);
174 st
->data
[0].d32
= cpu_to_be32((1 << 23) | (reg
<< 16));
176 ret
= spi_sync_transfer(st
->spi
, t
, ARRAY_SIZE(t
));
178 ret
= be32_to_cpu(st
->data
[1].d32
) & 0xffff;
180 mutex_unlock(&st
->lock
);
185 static int ad5421_update_ctrl(struct iio_dev
*indio_dev
, unsigned int set
,
188 struct ad5421_state
*st
= iio_priv(indio_dev
);
191 mutex_lock(&st
->lock
);
196 ret
= ad5421_write_unlocked(indio_dev
, AD5421_REG_CTRL
, st
->ctrl
);
198 mutex_unlock(&st
->lock
);
203 static irqreturn_t
ad5421_fault_handler(int irq
, void *data
)
205 struct iio_dev
*indio_dev
= data
;
206 struct ad5421_state
*st
= iio_priv(indio_dev
);
208 unsigned int old_fault
= 0;
211 fault
= ad5421_read(indio_dev
, AD5421_REG_FAULT
);
215 /* If we had a fault, this might mean that the DAC has lost its state
216 * and has been reset. Make sure that the control register actually
217 * contains what we expect it to contain. Otherwise the watchdog might
218 * be enabled and we get watchdog timeout faults, which will render the
220 ad5421_update_ctrl(indio_dev
, 0, 0);
223 /* The fault pin stays high as long as a fault condition is present and
224 * it is not possible to mask fault conditions. For certain fault
225 * conditions for example like over-temperature it takes some time
226 * until the fault condition disappears. If we would exit the interrupt
227 * handler immediately after handling the event it would be entered
228 * again instantly. Thus we fall back to polling in case we detect that
229 * a interrupt condition is still present.
232 /* 0xffff is a invalid value for the register and will only be
233 * read if there has been a communication error */
237 /* we are only interested in new events */
238 events
= (old_fault
^ fault
) & fault
;
239 events
&= st
->fault_mask
;
241 if (events
& AD5421_FAULT_OVER_CURRENT
) {
242 iio_push_event(indio_dev
,
243 IIO_UNMOD_EVENT_CODE(IIO_CURRENT
,
247 iio_get_time_ns(indio_dev
));
250 if (events
& AD5421_FAULT_UNDER_CURRENT
) {
251 iio_push_event(indio_dev
,
252 IIO_UNMOD_EVENT_CODE(IIO_CURRENT
,
256 iio_get_time_ns(indio_dev
));
259 if (events
& AD5421_FAULT_TEMP_OVER_140
) {
260 iio_push_event(indio_dev
,
261 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
265 iio_get_time_ns(indio_dev
));
269 fault
= ad5421_read(indio_dev
, AD5421_REG_FAULT
);
271 /* still active? go to sleep for some time */
272 if (fault
& AD5421_FAULT_TRIGGER_IRQ
)
275 } while (fault
& AD5421_FAULT_TRIGGER_IRQ
);
281 static void ad5421_get_current_min_max(struct ad5421_state
*st
,
282 unsigned int *min
, unsigned int *max
)
284 /* The current range is configured using external pins, which are
285 * usually hard-wired and not run-time switchable. */
286 switch (st
->current_range
) {
287 case AD5421_CURRENT_RANGE_4mA_20mA
:
291 case AD5421_CURRENT_RANGE_3mA8_21mA
:
295 case AD5421_CURRENT_RANGE_3mA2_24mA
:
306 static inline unsigned int ad5421_get_offset(struct ad5421_state
*st
)
308 unsigned int min
, max
;
310 ad5421_get_current_min_max(st
, &min
, &max
);
311 return (min
* (1 << 16)) / (max
- min
);
314 static int ad5421_read_raw(struct iio_dev
*indio_dev
,
315 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long m
)
317 struct ad5421_state
*st
= iio_priv(indio_dev
);
318 unsigned int min
, max
;
321 if (chan
->type
!= IIO_CURRENT
)
325 case IIO_CHAN_INFO_RAW
:
326 ret
= ad5421_read(indio_dev
, AD5421_REG_DAC_DATA
);
331 case IIO_CHAN_INFO_SCALE
:
332 ad5421_get_current_min_max(st
, &min
, &max
);
334 *val2
= (1 << 16) * 1000;
335 return IIO_VAL_FRACTIONAL
;
336 case IIO_CHAN_INFO_OFFSET
:
337 *val
= ad5421_get_offset(st
);
339 case IIO_CHAN_INFO_CALIBBIAS
:
340 ret
= ad5421_read(indio_dev
, AD5421_REG_OFFSET
);
345 case IIO_CHAN_INFO_CALIBSCALE
:
346 ret
= ad5421_read(indio_dev
, AD5421_REG_GAIN
);
356 static int ad5421_write_raw(struct iio_dev
*indio_dev
,
357 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
359 const unsigned int max_val
= 1 << 16;
362 case IIO_CHAN_INFO_RAW
:
363 if (val
>= max_val
|| val
< 0)
366 return ad5421_write(indio_dev
, AD5421_REG_DAC_DATA
, val
);
367 case IIO_CHAN_INFO_CALIBBIAS
:
369 if (val
>= max_val
|| val
< 0)
372 return ad5421_write(indio_dev
, AD5421_REG_OFFSET
, val
);
373 case IIO_CHAN_INFO_CALIBSCALE
:
374 if (val
>= max_val
|| val
< 0)
377 return ad5421_write(indio_dev
, AD5421_REG_GAIN
, val
);
385 static int ad5421_write_event_config(struct iio_dev
*indio_dev
,
386 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
387 enum iio_event_direction dir
, int state
)
389 struct ad5421_state
*st
= iio_priv(indio_dev
);
392 switch (chan
->type
) {
394 if (dir
== IIO_EV_DIR_RISING
)
395 mask
= AD5421_FAULT_OVER_CURRENT
;
397 mask
= AD5421_FAULT_UNDER_CURRENT
;
400 mask
= AD5421_FAULT_TEMP_OVER_140
;
406 mutex_lock(&st
->lock
);
408 st
->fault_mask
|= mask
;
410 st
->fault_mask
&= ~mask
;
411 mutex_unlock(&st
->lock
);
416 static int ad5421_read_event_config(struct iio_dev
*indio_dev
,
417 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
418 enum iio_event_direction dir
)
420 struct ad5421_state
*st
= iio_priv(indio_dev
);
423 switch (chan
->type
) {
425 if (dir
== IIO_EV_DIR_RISING
)
426 mask
= AD5421_FAULT_OVER_CURRENT
;
428 mask
= AD5421_FAULT_UNDER_CURRENT
;
431 mask
= AD5421_FAULT_TEMP_OVER_140
;
437 return (bool)(st
->fault_mask
& mask
);
440 static int ad5421_read_event_value(struct iio_dev
*indio_dev
,
441 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
442 enum iio_event_direction dir
, enum iio_event_info info
, int *val
,
447 switch (chan
->type
) {
449 ret
= ad5421_read(indio_dev
, AD5421_REG_DAC_DATA
);
464 static const struct iio_info ad5421_info
= {
465 .read_raw
= ad5421_read_raw
,
466 .write_raw
= ad5421_write_raw
,
467 .read_event_config
= ad5421_read_event_config
,
468 .write_event_config
= ad5421_write_event_config
,
469 .read_event_value
= ad5421_read_event_value
,
472 static int ad5421_probe(struct spi_device
*spi
)
474 struct ad5421_platform_data
*pdata
= dev_get_platdata(&spi
->dev
);
475 struct iio_dev
*indio_dev
;
476 struct ad5421_state
*st
;
479 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
480 if (indio_dev
== NULL
) {
481 dev_err(&spi
->dev
, "Failed to allocate iio device\n");
485 st
= iio_priv(indio_dev
);
486 spi_set_drvdata(spi
, indio_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 mutex_init(&st
->lock
);
498 st
->ctrl
= AD5421_CTRL_WATCHDOG_DISABLE
|
499 AD5421_CTRL_AUTO_FAULT_READBACK
;
502 st
->current_range
= pdata
->current_range
;
503 if (pdata
->external_vref
)
504 st
->ctrl
|= AD5421_CTRL_PWR_DOWN_INT_VREF
;
506 st
->current_range
= AD5421_CURRENT_RANGE_4mA_20mA
;
509 /* write initial ctrl register value */
510 ad5421_update_ctrl(indio_dev
, 0, 0);
513 ret
= devm_request_threaded_irq(&spi
->dev
, spi
->irq
,
515 ad5421_fault_handler
,
516 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
523 return devm_iio_device_register(&spi
->dev
, indio_dev
);
526 static struct spi_driver ad5421_driver
= {
530 .probe
= ad5421_probe
,
532 module_spi_driver(ad5421_driver
);
534 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
535 MODULE_DESCRIPTION("Analog Devices AD5421 DAC");
536 MODULE_LICENSE("GPL v2");
537 MODULE_ALIAS("spi:ad5421");