1 // SPDX-License-Identifier: GPL-2.0-only
3 * IIO DAC driver for Analog Devices AD8801 DAC
5 * Copyright (C) 2016 Gwenhael Goavec-Merou
8 #include <linux/iio/iio.h>
9 #include <linux/module.h>
10 #include <linux/regulator/consumer.h>
11 #include <linux/spi/spi.h>
12 #include <linux/sysfs.h>
14 #define AD8801_CFG_ADDR_OFFSET 8
16 enum ad8801_device_ids
{
22 struct spi_device
*spi
;
23 unsigned char dac_cache
[8]; /* Value write on each channel */
24 unsigned int vrefh_mv
;
25 unsigned int vrefl_mv
;
26 struct regulator
*vrefh_reg
;
27 struct regulator
*vrefl_reg
;
29 __be16 data ____cacheline_aligned
;
32 static int ad8801_spi_write(struct ad8801_state
*state
,
33 u8 channel
, unsigned char value
)
35 state
->data
= cpu_to_be16((channel
<< AD8801_CFG_ADDR_OFFSET
) | value
);
36 return spi_write(state
->spi
, &state
->data
, sizeof(state
->data
));
39 static int ad8801_write_raw(struct iio_dev
*indio_dev
,
40 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
42 struct ad8801_state
*state
= iio_priv(indio_dev
);
46 case IIO_CHAN_INFO_RAW
:
47 if (val
>= 256 || val
< 0)
50 ret
= ad8801_spi_write(state
, chan
->channel
, val
);
52 state
->dac_cache
[chan
->channel
] = val
;
61 static int ad8801_read_raw(struct iio_dev
*indio_dev
,
62 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
64 struct ad8801_state
*state
= iio_priv(indio_dev
);
67 case IIO_CHAN_INFO_RAW
:
68 *val
= state
->dac_cache
[chan
->channel
];
70 case IIO_CHAN_INFO_SCALE
:
71 *val
= state
->vrefh_mv
- state
->vrefl_mv
;
73 return IIO_VAL_FRACTIONAL_LOG2
;
74 case IIO_CHAN_INFO_OFFSET
:
75 *val
= state
->vrefl_mv
;
84 static const struct iio_info ad8801_info
= {
85 .read_raw
= ad8801_read_raw
,
86 .write_raw
= ad8801_write_raw
,
89 #define AD8801_CHANNEL(chan) { \
90 .type = IIO_VOLTAGE, \
94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
95 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
96 BIT(IIO_CHAN_INFO_OFFSET), \
99 static const struct iio_chan_spec ad8801_channels
[] = {
110 static int ad8801_probe(struct spi_device
*spi
)
112 struct iio_dev
*indio_dev
;
113 struct ad8801_state
*state
;
114 const struct spi_device_id
*id
;
117 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*state
));
118 if (indio_dev
== NULL
)
121 state
= iio_priv(indio_dev
);
123 id
= spi_get_device_id(spi
);
125 state
->vrefh_reg
= devm_regulator_get(&spi
->dev
, "vrefh");
126 if (IS_ERR(state
->vrefh_reg
)) {
127 dev_err(&spi
->dev
, "Vrefh regulator not specified\n");
128 return PTR_ERR(state
->vrefh_reg
);
131 ret
= regulator_enable(state
->vrefh_reg
);
133 dev_err(&spi
->dev
, "Failed to enable vrefh regulator: %d\n",
138 ret
= regulator_get_voltage(state
->vrefh_reg
);
140 dev_err(&spi
->dev
, "Failed to read vrefh regulator: %d\n",
142 goto error_disable_vrefh_reg
;
144 state
->vrefh_mv
= ret
/ 1000;
146 if (id
->driver_data
== ID_AD8803
) {
147 state
->vrefl_reg
= devm_regulator_get(&spi
->dev
, "vrefl");
148 if (IS_ERR(state
->vrefl_reg
)) {
149 dev_err(&spi
->dev
, "Vrefl regulator not specified\n");
150 ret
= PTR_ERR(state
->vrefl_reg
);
151 goto error_disable_vrefh_reg
;
154 ret
= regulator_enable(state
->vrefl_reg
);
156 dev_err(&spi
->dev
, "Failed to enable vrefl regulator: %d\n",
158 goto error_disable_vrefh_reg
;
161 ret
= regulator_get_voltage(state
->vrefl_reg
);
163 dev_err(&spi
->dev
, "Failed to read vrefl regulator: %d\n",
165 goto error_disable_vrefl_reg
;
167 state
->vrefl_mv
= ret
/ 1000;
170 state
->vrefl_reg
= NULL
;
173 spi_set_drvdata(spi
, indio_dev
);
174 indio_dev
->dev
.parent
= &spi
->dev
;
175 indio_dev
->info
= &ad8801_info
;
176 indio_dev
->modes
= INDIO_DIRECT_MODE
;
177 indio_dev
->channels
= ad8801_channels
;
178 indio_dev
->num_channels
= ARRAY_SIZE(ad8801_channels
);
179 indio_dev
->name
= id
->name
;
181 ret
= iio_device_register(indio_dev
);
183 dev_err(&spi
->dev
, "Failed to register iio device: %d\n",
185 goto error_disable_vrefl_reg
;
190 error_disable_vrefl_reg
:
191 if (state
->vrefl_reg
)
192 regulator_disable(state
->vrefl_reg
);
193 error_disable_vrefh_reg
:
194 regulator_disable(state
->vrefh_reg
);
198 static int ad8801_remove(struct spi_device
*spi
)
200 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
201 struct ad8801_state
*state
= iio_priv(indio_dev
);
203 iio_device_unregister(indio_dev
);
204 if (state
->vrefl_reg
)
205 regulator_disable(state
->vrefl_reg
);
206 regulator_disable(state
->vrefh_reg
);
211 static const struct spi_device_id ad8801_ids
[] = {
212 {"ad8801", ID_AD8801
},
213 {"ad8803", ID_AD8803
},
216 MODULE_DEVICE_TABLE(spi
, ad8801_ids
);
218 static struct spi_driver ad8801_driver
= {
222 .probe
= ad8801_probe
,
223 .remove
= ad8801_remove
,
224 .id_table
= ad8801_ids
,
226 module_spi_driver(ad8801_driver
);
228 MODULE_AUTHOR("Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>");
229 MODULE_DESCRIPTION("Analog Devices AD8801/AD8803 DAC");
230 MODULE_LICENSE("GPL v2");