blob: 2e46def9d8ee10562674ade0b7266d3bd2ebf815 [file] [log] [blame]
Thomas Gleixnerfda8d262019-05-28 09:57:06 -07001// SPDX-License-Identifier: GPL-2.0-only
Lars-Peter Clausenf8347822013-06-11 17:56:00 +01002/*
3 * AD7303 Digital to analog converters driver
4 *
5 * Copyright 2013 Analog Devices Inc.
Lars-Peter Clausenf8347822013-06-11 17:56:00 +01006 */
7
8#include <linux/err.h>
9#include <linux/module.h>
Jonathan Cameron34860a12020-09-10 18:32:16 +010010#include <linux/mod_devicetable.h>
Lars-Peter Clausenf8347822013-06-11 17:56:00 +010011#include <linux/kernel.h>
12#include <linux/spi/spi.h>
13#include <linux/slab.h>
14#include <linux/sysfs.h>
15#include <linux/regulator/consumer.h>
Lars-Peter Clausenf8347822013-06-11 17:56:00 +010016
17#include <linux/iio/iio.h>
18#include <linux/iio/sysfs.h>
19
20#include <linux/platform_data/ad7303.h>
21
22#define AD7303_CFG_EXTERNAL_VREF BIT(15)
23#define AD7303_CFG_POWER_DOWN(ch) BIT(11 + (ch))
24#define AD7303_CFG_ADDR_OFFSET 10
25
26#define AD7303_CMD_UPDATE_DAC (0x3 << 8)
27
28/**
29 * struct ad7303_state - driver instance specific data
30 * @spi: the device for this driver instance
31 * @config: cached config register value
32 * @dac_cache: current DAC raw value (chip does not support readback)
Lee Jones60262922020-07-16 14:59:28 +010033 * @vdd_reg: reference to VDD regulator
34 * @vref_reg: reference to VREF regulator
35 * @lock: protect writes and cache updates
Lars-Peter Clausenf8347822013-06-11 17:56:00 +010036 * @data: spi transfer buffer
37 */
38
39struct ad7303_state {
40 struct spi_device *spi;
41 uint16_t config;
42 uint8_t dac_cache[2];
43
44 struct regulator *vdd_reg;
45 struct regulator *vref_reg;
46
Alexandru Ardeleanc991bf92019-10-07 11:26:42 +030047 struct mutex lock;
Lars-Peter Clausenf8347822013-06-11 17:56:00 +010048 /*
49 * DMA (thus cache coherency maintenance) requires the
50 * transfer buffers to live in their own cache lines.
51 */
52 __be16 data ____cacheline_aligned;
53};
54
55static int ad7303_write(struct ad7303_state *st, unsigned int chan,
56 uint8_t val)
57{
58 st->data = cpu_to_be16(AD7303_CMD_UPDATE_DAC |
59 (chan << AD7303_CFG_ADDR_OFFSET) |
60 st->config | val);
61
62 return spi_write(st->spi, &st->data, sizeof(st->data));
63}
64
65static ssize_t ad7303_read_dac_powerdown(struct iio_dev *indio_dev,
66 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
67{
68 struct ad7303_state *st = iio_priv(indio_dev);
69
70 return sprintf(buf, "%d\n", (bool)(st->config &
71 AD7303_CFG_POWER_DOWN(chan->channel)));
72}
73
74static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev,
75 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
76 size_t len)
77{
78 struct ad7303_state *st = iio_priv(indio_dev);
79 bool pwr_down;
80 int ret;
81
82 ret = strtobool(buf, &pwr_down);
83 if (ret)
84 return ret;
85
Alexandru Ardeleanc991bf92019-10-07 11:26:42 +030086 mutex_lock(&st->lock);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +010087
88 if (pwr_down)
89 st->config |= AD7303_CFG_POWER_DOWN(chan->channel);
90 else
91 st->config &= ~AD7303_CFG_POWER_DOWN(chan->channel);
92
93 /* There is no noop cmd which allows us to only update the powerdown
94 * mode, so just write one of the DAC channels again */
95 ad7303_write(st, chan->channel, st->dac_cache[chan->channel]);
96
Alexandru Ardeleanc991bf92019-10-07 11:26:42 +030097 mutex_unlock(&st->lock);
Dan Carpentera04cf552014-01-30 11:58:00 +000098 return len;
Lars-Peter Clausenf8347822013-06-11 17:56:00 +010099}
100
101static int ad7303_get_vref(struct ad7303_state *st,
102 struct iio_chan_spec const *chan)
103{
104 int ret;
105
106 if (st->config & AD7303_CFG_EXTERNAL_VREF)
107 return regulator_get_voltage(st->vref_reg);
108
109 ret = regulator_get_voltage(st->vdd_reg);
110 if (ret < 0)
111 return ret;
112 return ret / 2;
113}
114
115static int ad7303_read_raw(struct iio_dev *indio_dev,
116 struct iio_chan_spec const *chan, int *val, int *val2, long info)
117{
118 struct ad7303_state *st = iio_priv(indio_dev);
119 int vref_uv;
120
121 switch (info) {
122 case IIO_CHAN_INFO_RAW:
Alexandru Ardeleanc991bf92019-10-07 11:26:42 +0300123 mutex_lock(&st->lock);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100124 *val = st->dac_cache[chan->channel];
Alexandru Ardeleanc991bf92019-10-07 11:26:42 +0300125 mutex_unlock(&st->lock);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100126 return IIO_VAL_INT;
127 case IIO_CHAN_INFO_SCALE:
128 vref_uv = ad7303_get_vref(st, chan);
129 if (vref_uv < 0)
130 return vref_uv;
131
132 *val = 2 * vref_uv / 1000;
133 *val2 = chan->scan_type.realbits;
134
135 return IIO_VAL_FRACTIONAL_LOG2;
136 default:
137 break;
138 }
139 return -EINVAL;
140}
141
142static int ad7303_write_raw(struct iio_dev *indio_dev,
143 struct iio_chan_spec const *chan, int val, int val2, long mask)
144{
145 struct ad7303_state *st = iio_priv(indio_dev);
146 int ret;
147
148 switch (mask) {
149 case IIO_CHAN_INFO_RAW:
150 if (val >= (1 << chan->scan_type.realbits) || val < 0)
151 return -EINVAL;
152
Alexandru Ardeleanc991bf92019-10-07 11:26:42 +0300153 mutex_lock(&st->lock);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100154 ret = ad7303_write(st, chan->address, val);
155 if (ret == 0)
156 st->dac_cache[chan->channel] = val;
Alexandru Ardeleanc991bf92019-10-07 11:26:42 +0300157 mutex_unlock(&st->lock);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100158 break;
159 default:
160 ret = -EINVAL;
161 }
162
163 return ret;
164}
165
166static const struct iio_info ad7303_info = {
167 .read_raw = ad7303_read_raw,
168 .write_raw = ad7303_write_raw,
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100169};
170
171static const struct iio_chan_spec_ext_info ad7303_ext_info[] = {
172 {
173 .name = "powerdown",
174 .read = ad7303_read_dac_powerdown,
175 .write = ad7303_write_dac_powerdown,
Jonathan Cameron37044322013-09-08 14:57:00 +0100176 .shared = IIO_SEPARATE,
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100177 },
178 { },
179};
180
181#define AD7303_CHANNEL(chan) { \
182 .type = IIO_VOLTAGE, \
183 .indexed = 1, \
184 .output = 1, \
185 .channel = (chan), \
186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
188 .address = (chan), \
189 .scan_type = { \
190 .sign = 'u', \
Pavel Roskince420fd2017-04-13 14:54:23 -0700191 .realbits = 8, \
192 .storagebits = 8, \
193 .shift = 0, \
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100194 }, \
195 .ext_info = ad7303_ext_info, \
196}
197
198static const struct iio_chan_spec ad7303_channels[] = {
199 AD7303_CHANNEL(0),
200 AD7303_CHANNEL(1),
201};
202
203static int ad7303_probe(struct spi_device *spi)
204{
205 const struct spi_device_id *id = spi_get_device_id(spi);
206 struct iio_dev *indio_dev;
207 struct ad7303_state *st;
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100208 int ret;
209
Sachin Kamat66e670aa2013-08-19 12:38:00 +0100210 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100211 if (indio_dev == NULL)
212 return -ENOMEM;
213
214 st = iio_priv(indio_dev);
215 spi_set_drvdata(spi, indio_dev);
216
217 st->spi = spi;
218
Alexandru Ardeleanc991bf92019-10-07 11:26:42 +0300219 mutex_init(&st->lock);
220
Sachin Kamat66e670aa2013-08-19 12:38:00 +0100221 st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
222 if (IS_ERR(st->vdd_reg))
223 return PTR_ERR(st->vdd_reg);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100224
225 ret = regulator_enable(st->vdd_reg);
226 if (ret)
Sachin Kamat66e670aa2013-08-19 12:38:00 +0100227 return ret;
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100228
Alexandru Tachicia8b26c22019-11-18 12:58:07 +0200229 st->vref_reg = devm_regulator_get_optional(&spi->dev, "REF");
230 if (IS_ERR(st->vref_reg)) {
231 ret = PTR_ERR(st->vref_reg);
232 if (ret != -ENODEV)
233 goto err_disable_vdd_reg;
234 st->vref_reg = NULL;
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100235 }
236
Alexandru Tachicia8b26c22019-11-18 12:58:07 +0200237 if (st->vref_reg) {
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100238 ret = regulator_enable(st->vref_reg);
239 if (ret)
Sachin Kamat66e670aa2013-08-19 12:38:00 +0100240 goto err_disable_vdd_reg;
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100241
242 st->config |= AD7303_CFG_EXTERNAL_VREF;
243 }
244
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100245 indio_dev->name = id->name;
246 indio_dev->info = &ad7303_info;
247 indio_dev->modes = INDIO_DIRECT_MODE;
248 indio_dev->channels = ad7303_channels;
249 indio_dev->num_channels = ARRAY_SIZE(ad7303_channels);
250
251 ret = iio_device_register(indio_dev);
252 if (ret)
253 goto err_disable_vref_reg;
254
255 return 0;
256
257err_disable_vref_reg:
258 if (st->vref_reg)
259 regulator_disable(st->vref_reg);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100260err_disable_vdd_reg:
261 regulator_disable(st->vdd_reg);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100262 return ret;
263}
264
265static int ad7303_remove(struct spi_device *spi)
266{
267 struct iio_dev *indio_dev = spi_get_drvdata(spi);
268 struct ad7303_state *st = iio_priv(indio_dev);
269
270 iio_device_unregister(indio_dev);
271
Sachin Kamat66e670aa2013-08-19 12:38:00 +0100272 if (st->vref_reg)
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100273 regulator_disable(st->vref_reg);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100274 regulator_disable(st->vdd_reg);
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100275
276 return 0;
277}
278
Javier Martinez Canillas1c00dcd2015-08-20 09:07:23 +0200279static const struct of_device_id ad7303_spi_of_match[] = {
280 { .compatible = "adi,ad7303", },
281 { /* sentinel */ },
282};
283MODULE_DEVICE_TABLE(of, ad7303_spi_of_match);
284
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100285static const struct spi_device_id ad7303_spi_ids[] = {
286 { "ad7303", 0 },
287 {}
288};
289MODULE_DEVICE_TABLE(spi, ad7303_spi_ids);
290
291static struct spi_driver ad7303_driver = {
292 .driver = {
293 .name = "ad7303",
Jonathan Cameron34860a12020-09-10 18:32:16 +0100294 .of_match_table = ad7303_spi_of_match,
Lars-Peter Clausenf8347822013-06-11 17:56:00 +0100295 },
296 .probe = ad7303_probe,
297 .remove = ad7303_remove,
298 .id_table = ad7303_spi_ids,
299};
300module_spi_driver(ad7303_driver);
301
302MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
303MODULE_DESCRIPTION("Analog Devices AD7303 DAC driver");
304MODULE_LICENSE("GPL v2");