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