2 * IIO DAC driver for Analog Devices AD8801 DAC
4 * Copyright (C) 2016 Gwenhael Goavec-Merou
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
16 #include <linux/iio/iio.h>
17 #include <linux/module.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/spi/spi.h>
20 #include <linux/sysfs.h>
22 #define AD8801_CFG_ADDR_OFFSET 8
24 enum ad8801_device_ids
{
30 struct spi_device
*spi
;
31 unsigned char dac_cache
[8]; /* Value write on each channel */
32 unsigned int vrefh_mv
;
33 unsigned int vrefl_mv
;
34 struct regulator
*vrefh_reg
;
35 struct regulator
*vrefl_reg
;
37 __be16 data ____cacheline_aligned
;
40 static int ad8801_spi_write(struct ad8801_state
*state
,
41 u8 channel
, unsigned char value
)
43 state
->data
= cpu_to_be16((channel
<< AD8801_CFG_ADDR_OFFSET
) | value
);
44 return spi_write(state
->spi
, &state
->data
, sizeof(state
->data
));
47 static int ad8801_write_raw(struct iio_dev
*indio_dev
,
48 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
50 struct ad8801_state
*state
= iio_priv(indio_dev
);
54 case IIO_CHAN_INFO_RAW
:
55 if (val
>= 256 || val
< 0)
58 ret
= ad8801_spi_write(state
, chan
->channel
, val
);
60 state
->dac_cache
[chan
->channel
] = val
;
69 static int ad8801_read_raw(struct iio_dev
*indio_dev
,
70 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
72 struct ad8801_state
*state
= iio_priv(indio_dev
);
75 case IIO_CHAN_INFO_RAW
:
76 *val
= state
->dac_cache
[chan
->channel
];
78 case IIO_CHAN_INFO_SCALE
:
79 *val
= state
->vrefh_mv
- state
->vrefl_mv
;
81 return IIO_VAL_FRACTIONAL_LOG2
;
82 case IIO_CHAN_INFO_OFFSET
:
83 *val
= state
->vrefl_mv
;
92 static const struct iio_info ad8801_info
= {
93 .read_raw
= ad8801_read_raw
,
94 .write_raw
= ad8801_write_raw
,
95 .driver_module
= THIS_MODULE
,
98 #define AD8801_CHANNEL(chan) { \
99 .type = IIO_VOLTAGE, \
103 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
104 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
105 BIT(IIO_CHAN_INFO_OFFSET), \
108 static const struct iio_chan_spec ad8801_channels
[] = {
119 static int ad8801_probe(struct spi_device
*spi
)
121 struct iio_dev
*indio_dev
;
122 struct ad8801_state
*state
;
123 const struct spi_device_id
*id
;
126 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*state
));
127 if (indio_dev
== NULL
)
130 state
= iio_priv(indio_dev
);
132 id
= spi_get_device_id(spi
);
134 state
->vrefh_reg
= devm_regulator_get(&spi
->dev
, "vrefh");
135 if (IS_ERR(state
->vrefh_reg
)) {
136 dev_err(&spi
->dev
, "Vrefh regulator not specified\n");
137 return PTR_ERR(state
->vrefh_reg
);
140 ret
= regulator_enable(state
->vrefh_reg
);
142 dev_err(&spi
->dev
, "Failed to enable vrefh regulator: %d\n",
147 ret
= regulator_get_voltage(state
->vrefh_reg
);
149 dev_err(&spi
->dev
, "Failed to read vrefh regulator: %d\n",
151 goto error_disable_vrefh_reg
;
153 state
->vrefh_mv
= ret
/ 1000;
155 if (id
->driver_data
== ID_AD8803
) {
156 state
->vrefl_reg
= devm_regulator_get(&spi
->dev
, "vrefl");
157 if (IS_ERR(state
->vrefl_reg
)) {
158 dev_err(&spi
->dev
, "Vrefl regulator not specified\n");
159 ret
= PTR_ERR(state
->vrefl_reg
);
160 goto error_disable_vrefh_reg
;
163 ret
= regulator_enable(state
->vrefl_reg
);
165 dev_err(&spi
->dev
, "Failed to enable vrefl regulator: %d\n",
167 goto error_disable_vrefh_reg
;
170 ret
= regulator_get_voltage(state
->vrefl_reg
);
172 dev_err(&spi
->dev
, "Failed to read vrefl regulator: %d\n",
174 goto error_disable_vrefl_reg
;
176 state
->vrefl_mv
= ret
/ 1000;
179 state
->vrefl_reg
= NULL
;
182 spi_set_drvdata(spi
, indio_dev
);
183 indio_dev
->dev
.parent
= &spi
->dev
;
184 indio_dev
->info
= &ad8801_info
;
185 indio_dev
->modes
= INDIO_DIRECT_MODE
;
186 indio_dev
->channels
= ad8801_channels
;
187 indio_dev
->num_channels
= ARRAY_SIZE(ad8801_channels
);
188 indio_dev
->name
= id
->name
;
190 ret
= iio_device_register(indio_dev
);
192 dev_err(&spi
->dev
, "Failed to register iio device: %d\n",
194 goto error_disable_vrefl_reg
;
199 error_disable_vrefl_reg
:
200 if (state
->vrefl_reg
)
201 regulator_disable(state
->vrefl_reg
);
202 error_disable_vrefh_reg
:
203 regulator_disable(state
->vrefh_reg
);
207 static int ad8801_remove(struct spi_device
*spi
)
209 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
210 struct ad8801_state
*state
= iio_priv(indio_dev
);
212 iio_device_unregister(indio_dev
);
213 if (state
->vrefl_reg
)
214 regulator_disable(state
->vrefl_reg
);
215 regulator_disable(state
->vrefh_reg
);
220 static const struct spi_device_id ad8801_ids
[] = {
221 {"ad8801", ID_AD8801
},
222 {"ad8803", ID_AD8803
},
225 MODULE_DEVICE_TABLE(spi
, ad8801_ids
);
227 static struct spi_driver ad8801_driver
= {
231 .probe
= ad8801_probe
,
232 .remove
= ad8801_remove
,
233 .id_table
= ad8801_ids
,
235 module_spi_driver(ad8801_driver
);
237 MODULE_AUTHOR("Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>");
238 MODULE_DESCRIPTION("Analog Devices AD8801/AD8803 DAC");
239 MODULE_LICENSE("GPL v2");