xref: /linux/drivers/iio/dac/ad7303.c (revision bd4af432cc71b5fbfe4833510359a6ad3ada250d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AD7303 Digital to analog converters driver
4  *
5  * Copyright 2013 Analog Devices Inc.
6  */
7 
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/spi/spi.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 
19 #include <linux/platform_data/ad7303.h>
20 
21 #define AD7303_CFG_EXTERNAL_VREF BIT(15)
22 #define AD7303_CFG_POWER_DOWN(ch) BIT(11 + (ch))
23 #define AD7303_CFG_ADDR_OFFSET	10
24 
25 #define AD7303_CMD_UPDATE_DAC	(0x3 << 8)
26 
27 /**
28  * struct ad7303_state - driver instance specific data
29  * @spi:		the device for this driver instance
30  * @config:		cached config register value
31  * @dac_cache:		current DAC raw value (chip does not support readback)
32  * @data:		spi transfer buffer
33  */
34 
35 struct ad7303_state {
36 	struct spi_device *spi;
37 	uint16_t config;
38 	uint8_t dac_cache[2];
39 
40 	struct regulator *vdd_reg;
41 	struct regulator *vref_reg;
42 
43 	struct mutex lock;
44 	/*
45 	 * DMA (thus cache coherency maintenance) requires the
46 	 * transfer buffers to live in their own cache lines.
47 	 */
48 	__be16 data ____cacheline_aligned;
49 };
50 
51 static int ad7303_write(struct ad7303_state *st, unsigned int chan,
52 	uint8_t val)
53 {
54 	st->data = cpu_to_be16(AD7303_CMD_UPDATE_DAC |
55 		(chan << AD7303_CFG_ADDR_OFFSET) |
56 		st->config | val);
57 
58 	return spi_write(st->spi, &st->data, sizeof(st->data));
59 }
60 
61 static ssize_t ad7303_read_dac_powerdown(struct iio_dev *indio_dev,
62 	uintptr_t private, const struct iio_chan_spec *chan, char *buf)
63 {
64 	struct ad7303_state *st = iio_priv(indio_dev);
65 
66 	return sprintf(buf, "%d\n", (bool)(st->config &
67 		AD7303_CFG_POWER_DOWN(chan->channel)));
68 }
69 
70 static ssize_t ad7303_write_dac_powerdown(struct iio_dev *indio_dev,
71 	 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
72 	 size_t len)
73 {
74 	struct ad7303_state *st = iio_priv(indio_dev);
75 	bool pwr_down;
76 	int ret;
77 
78 	ret = strtobool(buf, &pwr_down);
79 	if (ret)
80 		return ret;
81 
82 	mutex_lock(&st->lock);
83 
84 	if (pwr_down)
85 		st->config |= AD7303_CFG_POWER_DOWN(chan->channel);
86 	else
87 		st->config &= ~AD7303_CFG_POWER_DOWN(chan->channel);
88 
89 	/* There is no noop cmd which allows us to only update the powerdown
90 	 * mode, so just write one of the DAC channels again */
91 	ad7303_write(st, chan->channel, st->dac_cache[chan->channel]);
92 
93 	mutex_unlock(&st->lock);
94 	return len;
95 }
96 
97 static int ad7303_get_vref(struct ad7303_state *st,
98 	struct iio_chan_spec const *chan)
99 {
100 	int ret;
101 
102 	if (st->config & AD7303_CFG_EXTERNAL_VREF)
103 		return regulator_get_voltage(st->vref_reg);
104 
105 	ret = regulator_get_voltage(st->vdd_reg);
106 	if (ret < 0)
107 		return ret;
108 	return ret / 2;
109 }
110 
111 static int ad7303_read_raw(struct iio_dev *indio_dev,
112 	struct iio_chan_spec const *chan, int *val, int *val2, long info)
113 {
114 	struct ad7303_state *st = iio_priv(indio_dev);
115 	int vref_uv;
116 
117 	switch (info) {
118 	case IIO_CHAN_INFO_RAW:
119 		mutex_lock(&st->lock);
120 		*val = st->dac_cache[chan->channel];
121 		mutex_unlock(&st->lock);
122 		return IIO_VAL_INT;
123 	case IIO_CHAN_INFO_SCALE:
124 		vref_uv = ad7303_get_vref(st, chan);
125 		if (vref_uv < 0)
126 			return vref_uv;
127 
128 		*val = 2 * vref_uv / 1000;
129 		*val2 = chan->scan_type.realbits;
130 
131 		return IIO_VAL_FRACTIONAL_LOG2;
132 	default:
133 		break;
134 	}
135 	return -EINVAL;
136 }
137 
138 static int ad7303_write_raw(struct iio_dev *indio_dev,
139 	struct iio_chan_spec const *chan, int val, int val2, long mask)
140 {
141 	struct ad7303_state *st = iio_priv(indio_dev);
142 	int ret;
143 
144 	switch (mask) {
145 	case IIO_CHAN_INFO_RAW:
146 		if (val >= (1 << chan->scan_type.realbits) || val < 0)
147 			return -EINVAL;
148 
149 		mutex_lock(&st->lock);
150 		ret = ad7303_write(st, chan->address, val);
151 		if (ret == 0)
152 			st->dac_cache[chan->channel] = val;
153 		mutex_unlock(&st->lock);
154 		break;
155 	default:
156 		ret = -EINVAL;
157 	}
158 
159 	return ret;
160 }
161 
162 static const struct iio_info ad7303_info = {
163 	.read_raw = ad7303_read_raw,
164 	.write_raw = ad7303_write_raw,
165 };
166 
167 static const struct iio_chan_spec_ext_info ad7303_ext_info[] = {
168 	{
169 		.name = "powerdown",
170 		.read = ad7303_read_dac_powerdown,
171 		.write = ad7303_write_dac_powerdown,
172 		.shared = IIO_SEPARATE,
173 	},
174 	{ },
175 };
176 
177 #define AD7303_CHANNEL(chan) {					\
178 	.type = IIO_VOLTAGE,					\
179 	.indexed = 1,						\
180 	.output = 1,						\
181 	.channel = (chan),					\
182 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
183 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
184 	.address = (chan),					\
185 	.scan_type = {						\
186 		.sign = 'u',					\
187 		.realbits = 8,					\
188 		.storagebits = 8,				\
189 		.shift = 0,					\
190 	},							\
191 	.ext_info = ad7303_ext_info,				\
192 }
193 
194 static const struct iio_chan_spec ad7303_channels[] = {
195 	AD7303_CHANNEL(0),
196 	AD7303_CHANNEL(1),
197 };
198 
199 static int ad7303_probe(struct spi_device *spi)
200 {
201 	const struct spi_device_id *id = spi_get_device_id(spi);
202 	struct iio_dev *indio_dev;
203 	struct ad7303_state *st;
204 	int ret;
205 
206 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
207 	if (indio_dev == NULL)
208 		return -ENOMEM;
209 
210 	st = iio_priv(indio_dev);
211 	spi_set_drvdata(spi, indio_dev);
212 
213 	st->spi = spi;
214 
215 	mutex_init(&st->lock);
216 
217 	st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
218 	if (IS_ERR(st->vdd_reg))
219 		return PTR_ERR(st->vdd_reg);
220 
221 	ret = regulator_enable(st->vdd_reg);
222 	if (ret)
223 		return ret;
224 
225 	st->vref_reg = devm_regulator_get_optional(&spi->dev, "REF");
226 	if (IS_ERR(st->vref_reg)) {
227 		ret = PTR_ERR(st->vref_reg);
228 		if (ret != -ENODEV)
229 			goto err_disable_vdd_reg;
230 		st->vref_reg = NULL;
231 	}
232 
233 	if (st->vref_reg) {
234 		ret = regulator_enable(st->vref_reg);
235 		if (ret)
236 			goto err_disable_vdd_reg;
237 
238 		st->config |= AD7303_CFG_EXTERNAL_VREF;
239 	}
240 
241 	indio_dev->dev.parent = &spi->dev;
242 	indio_dev->name = id->name;
243 	indio_dev->info = &ad7303_info;
244 	indio_dev->modes = INDIO_DIRECT_MODE;
245 	indio_dev->channels = ad7303_channels;
246 	indio_dev->num_channels = ARRAY_SIZE(ad7303_channels);
247 
248 	ret = iio_device_register(indio_dev);
249 	if (ret)
250 		goto err_disable_vref_reg;
251 
252 	return 0;
253 
254 err_disable_vref_reg:
255 	if (st->vref_reg)
256 		regulator_disable(st->vref_reg);
257 err_disable_vdd_reg:
258 	regulator_disable(st->vdd_reg);
259 	return ret;
260 }
261 
262 static int ad7303_remove(struct spi_device *spi)
263 {
264 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
265 	struct ad7303_state *st = iio_priv(indio_dev);
266 
267 	iio_device_unregister(indio_dev);
268 
269 	if (st->vref_reg)
270 		regulator_disable(st->vref_reg);
271 	regulator_disable(st->vdd_reg);
272 
273 	return 0;
274 }
275 
276 static const struct of_device_id ad7303_spi_of_match[] = {
277 	{ .compatible = "adi,ad7303", },
278 	{ /* sentinel */ },
279 };
280 MODULE_DEVICE_TABLE(of, ad7303_spi_of_match);
281 
282 static const struct spi_device_id ad7303_spi_ids[] = {
283 	{ "ad7303", 0 },
284 	{}
285 };
286 MODULE_DEVICE_TABLE(spi, ad7303_spi_ids);
287 
288 static struct spi_driver ad7303_driver = {
289 	.driver = {
290 		.name = "ad7303",
291 		.of_match_table = of_match_ptr(ad7303_spi_of_match),
292 	},
293 	.probe = ad7303_probe,
294 	.remove = ad7303_remove,
295 	.id_table = ad7303_spi_ids,
296 };
297 module_spi_driver(ad7303_driver);
298 
299 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
300 MODULE_DESCRIPTION("Analog Devices AD7303 DAC driver");
301 MODULE_LICENSE("GPL v2");
302