xref: /openbmc/linux/drivers/iio/dac/ad7303.c (revision 0e96cf7f)
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 	/*
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(&indio_dev->mlock);
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(&indio_dev->mlock);
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 		*val = st->dac_cache[chan->channel];
120 		return IIO_VAL_INT;
121 	case IIO_CHAN_INFO_SCALE:
122 		vref_uv = ad7303_get_vref(st, chan);
123 		if (vref_uv < 0)
124 			return vref_uv;
125 
126 		*val = 2 * vref_uv / 1000;
127 		*val2 = chan->scan_type.realbits;
128 
129 		return IIO_VAL_FRACTIONAL_LOG2;
130 	default:
131 		break;
132 	}
133 	return -EINVAL;
134 }
135 
136 static int ad7303_write_raw(struct iio_dev *indio_dev,
137 	struct iio_chan_spec const *chan, int val, int val2, long mask)
138 {
139 	struct ad7303_state *st = iio_priv(indio_dev);
140 	int ret;
141 
142 	switch (mask) {
143 	case IIO_CHAN_INFO_RAW:
144 		if (val >= (1 << chan->scan_type.realbits) || val < 0)
145 			return -EINVAL;
146 
147 		mutex_lock(&indio_dev->mlock);
148 		ret = ad7303_write(st, chan->address, val);
149 		if (ret == 0)
150 			st->dac_cache[chan->channel] = val;
151 		mutex_unlock(&indio_dev->mlock);
152 		break;
153 	default:
154 		ret = -EINVAL;
155 	}
156 
157 	return ret;
158 }
159 
160 static const struct iio_info ad7303_info = {
161 	.read_raw = ad7303_read_raw,
162 	.write_raw = ad7303_write_raw,
163 };
164 
165 static const struct iio_chan_spec_ext_info ad7303_ext_info[] = {
166 	{
167 		.name = "powerdown",
168 		.read = ad7303_read_dac_powerdown,
169 		.write = ad7303_write_dac_powerdown,
170 		.shared = IIO_SEPARATE,
171 	},
172 	{ },
173 };
174 
175 #define AD7303_CHANNEL(chan) {					\
176 	.type = IIO_VOLTAGE,					\
177 	.indexed = 1,						\
178 	.output = 1,						\
179 	.channel = (chan),					\
180 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
181 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
182 	.address = (chan),					\
183 	.scan_type = {						\
184 		.sign = 'u',					\
185 		.realbits = 8,					\
186 		.storagebits = 8,				\
187 		.shift = 0,					\
188 	},							\
189 	.ext_info = ad7303_ext_info,				\
190 }
191 
192 static const struct iio_chan_spec ad7303_channels[] = {
193 	AD7303_CHANNEL(0),
194 	AD7303_CHANNEL(1),
195 };
196 
197 static int ad7303_probe(struct spi_device *spi)
198 {
199 	const struct spi_device_id *id = spi_get_device_id(spi);
200 	struct iio_dev *indio_dev;
201 	struct ad7303_state *st;
202 	bool ext_ref;
203 	int ret;
204 
205 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
206 	if (indio_dev == NULL)
207 		return -ENOMEM;
208 
209 	st = iio_priv(indio_dev);
210 	spi_set_drvdata(spi, indio_dev);
211 
212 	st->spi = spi;
213 
214 	st->vdd_reg = devm_regulator_get(&spi->dev, "Vdd");
215 	if (IS_ERR(st->vdd_reg))
216 		return PTR_ERR(st->vdd_reg);
217 
218 	ret = regulator_enable(st->vdd_reg);
219 	if (ret)
220 		return ret;
221 
222 	if (spi->dev.of_node) {
223 		ext_ref = of_property_read_bool(spi->dev.of_node,
224 				"REF-supply");
225 	} else {
226 		struct ad7303_platform_data *pdata = spi->dev.platform_data;
227 		if (pdata && pdata->use_external_ref)
228 			ext_ref = true;
229 		else
230 		    ext_ref = false;
231 	}
232 
233 	if (ext_ref) {
234 		st->vref_reg = devm_regulator_get(&spi->dev, "REF");
235 		if (IS_ERR(st->vref_reg)) {
236 			ret = PTR_ERR(st->vref_reg);
237 			goto err_disable_vdd_reg;
238 		}
239 
240 		ret = regulator_enable(st->vref_reg);
241 		if (ret)
242 			goto err_disable_vdd_reg;
243 
244 		st->config |= AD7303_CFG_EXTERNAL_VREF;
245 	}
246 
247 	indio_dev->dev.parent = &spi->dev;
248 	indio_dev->name = id->name;
249 	indio_dev->info = &ad7303_info;
250 	indio_dev->modes = INDIO_DIRECT_MODE;
251 	indio_dev->channels = ad7303_channels;
252 	indio_dev->num_channels = ARRAY_SIZE(ad7303_channels);
253 
254 	ret = iio_device_register(indio_dev);
255 	if (ret)
256 		goto err_disable_vref_reg;
257 
258 	return 0;
259 
260 err_disable_vref_reg:
261 	if (st->vref_reg)
262 		regulator_disable(st->vref_reg);
263 err_disable_vdd_reg:
264 	regulator_disable(st->vdd_reg);
265 	return ret;
266 }
267 
268 static int ad7303_remove(struct spi_device *spi)
269 {
270 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
271 	struct ad7303_state *st = iio_priv(indio_dev);
272 
273 	iio_device_unregister(indio_dev);
274 
275 	if (st->vref_reg)
276 		regulator_disable(st->vref_reg);
277 	regulator_disable(st->vdd_reg);
278 
279 	return 0;
280 }
281 
282 static const struct of_device_id ad7303_spi_of_match[] = {
283 	{ .compatible = "adi,ad7303", },
284 	{ /* sentinel */ },
285 };
286 MODULE_DEVICE_TABLE(of, ad7303_spi_of_match);
287 
288 static const struct spi_device_id ad7303_spi_ids[] = {
289 	{ "ad7303", 0 },
290 	{}
291 };
292 MODULE_DEVICE_TABLE(spi, ad7303_spi_ids);
293 
294 static struct spi_driver ad7303_driver = {
295 	.driver = {
296 		.name = "ad7303",
297 		.of_match_table = of_match_ptr(ad7303_spi_of_match),
298 	},
299 	.probe = ad7303_probe,
300 	.remove = ad7303_remove,
301 	.id_table = ad7303_spi_ids,
302 };
303 module_spi_driver(ad7303_driver);
304 
305 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
306 MODULE_DESCRIPTION("Analog Devices AD7303 DAC driver");
307 MODULE_LICENSE("GPL v2");
308