xref: /openbmc/linux/drivers/iio/dac/ti-dac082s085.c (revision 4bb1eb3c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ti-dac082s085.c - Texas Instruments 8/10/12-bit 2/4-channel DAC driver
4  *
5  * Copyright (C) 2017 KUNBUS GmbH
6  *
7  * https://www.ti.com/lit/ds/symlink/dac082s085.pdf
8  * https://www.ti.com/lit/ds/symlink/dac102s085.pdf
9  * https://www.ti.com/lit/ds/symlink/dac122s085.pdf
10  * https://www.ti.com/lit/ds/symlink/dac084s085.pdf
11  * https://www.ti.com/lit/ds/symlink/dac104s085.pdf
12  * https://www.ti.com/lit/ds/symlink/dac124s085.pdf
13  */
14 
15 #include <linux/iio/iio.h>
16 #include <linux/module.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
19 
20 enum { dual_8bit, dual_10bit, dual_12bit, quad_8bit, quad_10bit, quad_12bit };
21 
22 struct ti_dac_spec {
23 	u8 num_channels;
24 	u8 resolution;
25 };
26 
27 static const struct ti_dac_spec ti_dac_spec[] = {
28 	[dual_8bit]  = { .num_channels = 2, .resolution = 8  },
29 	[dual_10bit] = { .num_channels = 2, .resolution = 10 },
30 	[dual_12bit] = { .num_channels = 2, .resolution = 12 },
31 	[quad_8bit]  = { .num_channels = 4, .resolution = 8  },
32 	[quad_10bit] = { .num_channels = 4, .resolution = 10 },
33 	[quad_12bit] = { .num_channels = 4, .resolution = 12 },
34 };
35 
36 /**
37  * struct ti_dac_chip - TI DAC chip
38  * @lock: protects write sequences
39  * @vref: regulator generating Vref
40  * @mesg: SPI message to perform a write
41  * @xfer: SPI transfer used by @mesg
42  * @val: cached value of each output
43  * @powerdown: whether the chip is powered down
44  * @powerdown_mode: selected by the user
45  * @resolution: resolution of the chip
46  * @buf: buffer for @xfer
47  */
48 struct ti_dac_chip {
49 	struct mutex lock;
50 	struct regulator *vref;
51 	struct spi_message mesg;
52 	struct spi_transfer xfer;
53 	u16 val[4];
54 	bool powerdown;
55 	u8 powerdown_mode;
56 	u8 resolution;
57 	u8 buf[2] ____cacheline_aligned;
58 };
59 
60 #define WRITE_NOT_UPDATE(chan)	(0x00 | (chan) << 6)
61 #define WRITE_AND_UPDATE(chan)	(0x10 | (chan) << 6)
62 #define WRITE_ALL_UPDATE	 0x20
63 #define POWERDOWN(mode) 	(0x30 | ((mode) + 1) << 6)
64 
65 static int ti_dac_cmd(struct ti_dac_chip *ti_dac, u8 cmd, u16 val)
66 {
67 	u8 shift = 12 - ti_dac->resolution;
68 
69 	ti_dac->buf[0] = cmd | (val >> (8 - shift));
70 	ti_dac->buf[1] = (val << shift) & 0xff;
71 	return spi_sync(ti_dac->mesg.spi, &ti_dac->mesg);
72 }
73 
74 static const char * const ti_dac_powerdown_modes[] = {
75 	"2.5kohm_to_gnd", "100kohm_to_gnd", "three_state",
76 };
77 
78 static int ti_dac_get_powerdown_mode(struct iio_dev *indio_dev,
79 				     const struct iio_chan_spec *chan)
80 {
81 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
82 
83 	return ti_dac->powerdown_mode;
84 }
85 
86 static int ti_dac_set_powerdown_mode(struct iio_dev *indio_dev,
87 				     const struct iio_chan_spec *chan,
88 				     unsigned int mode)
89 {
90 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
91 	int ret = 0;
92 
93 	if (ti_dac->powerdown_mode == mode)
94 		return 0;
95 
96 	mutex_lock(&ti_dac->lock);
97 	if (ti_dac->powerdown) {
98 		ret = ti_dac_cmd(ti_dac, POWERDOWN(mode), 0);
99 		if (ret)
100 			goto out;
101 	}
102 	ti_dac->powerdown_mode = mode;
103 
104 out:
105 	mutex_unlock(&ti_dac->lock);
106 	return ret;
107 }
108 
109 static const struct iio_enum ti_dac_powerdown_mode = {
110 	.items = ti_dac_powerdown_modes,
111 	.num_items = ARRAY_SIZE(ti_dac_powerdown_modes),
112 	.get = ti_dac_get_powerdown_mode,
113 	.set = ti_dac_set_powerdown_mode,
114 };
115 
116 static ssize_t ti_dac_read_powerdown(struct iio_dev *indio_dev,
117 				     uintptr_t private,
118 				     const struct iio_chan_spec *chan,
119 				     char *buf)
120 {
121 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
122 
123 	return sprintf(buf, "%d\n", ti_dac->powerdown);
124 }
125 
126 static ssize_t ti_dac_write_powerdown(struct iio_dev *indio_dev,
127 				      uintptr_t private,
128 				      const struct iio_chan_spec *chan,
129 				      const char *buf, size_t len)
130 {
131 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
132 	bool powerdown;
133 	int ret;
134 
135 	ret = strtobool(buf, &powerdown);
136 	if (ret)
137 		return ret;
138 
139 	if (ti_dac->powerdown == powerdown)
140 		return len;
141 
142 	mutex_lock(&ti_dac->lock);
143 	if (powerdown)
144 		ret = ti_dac_cmd(ti_dac, POWERDOWN(ti_dac->powerdown_mode), 0);
145 	else
146 		ret = ti_dac_cmd(ti_dac, WRITE_AND_UPDATE(0), ti_dac->val[0]);
147 	if (!ret)
148 		ti_dac->powerdown = powerdown;
149 	mutex_unlock(&ti_dac->lock);
150 
151 	return ret ? ret : len;
152 }
153 
154 static const struct iio_chan_spec_ext_info ti_dac_ext_info[] = {
155 	{
156 		.name	   = "powerdown",
157 		.read	   = ti_dac_read_powerdown,
158 		.write	   = ti_dac_write_powerdown,
159 		.shared	   = IIO_SHARED_BY_TYPE,
160 	},
161 	IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE, &ti_dac_powerdown_mode),
162 	IIO_ENUM_AVAILABLE("powerdown_mode", &ti_dac_powerdown_mode),
163 	{ },
164 };
165 
166 #define TI_DAC_CHANNEL(chan) {					\
167 	.type = IIO_VOLTAGE,					\
168 	.channel = (chan),					\
169 	.address = (chan),					\
170 	.indexed = true,					\
171 	.output = true,						\
172 	.datasheet_name = (const char[]){ 'A' + (chan), 0 },	\
173 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
174 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
175 	.ext_info = ti_dac_ext_info,				\
176 }
177 
178 static const struct iio_chan_spec ti_dac_channels[] = {
179 	TI_DAC_CHANNEL(0),
180 	TI_DAC_CHANNEL(1),
181 	TI_DAC_CHANNEL(2),
182 	TI_DAC_CHANNEL(3),
183 };
184 
185 static int ti_dac_read_raw(struct iio_dev *indio_dev,
186 			   struct iio_chan_spec const *chan,
187 			   int *val, int *val2, long mask)
188 {
189 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
190 	int ret;
191 
192 	switch (mask) {
193 	case IIO_CHAN_INFO_RAW:
194 		*val = ti_dac->val[chan->channel];
195 		ret = IIO_VAL_INT;
196 		break;
197 
198 	case IIO_CHAN_INFO_SCALE:
199 		ret = regulator_get_voltage(ti_dac->vref);
200 		if (ret < 0)
201 			return ret;
202 
203 		*val = ret / 1000;
204 		*val2 = ti_dac->resolution;
205 		ret = IIO_VAL_FRACTIONAL_LOG2;
206 		break;
207 
208 	default:
209 		ret = -EINVAL;
210 	}
211 
212 	return ret;
213 }
214 
215 static int ti_dac_write_raw(struct iio_dev *indio_dev,
216 			    struct iio_chan_spec const *chan,
217 			    int val, int val2, long mask)
218 {
219 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
220 	int ret;
221 
222 	switch (mask) {
223 	case IIO_CHAN_INFO_RAW:
224 		if (ti_dac->val[chan->channel] == val)
225 			return 0;
226 
227 		if (val >= (1 << ti_dac->resolution) || val < 0)
228 			return -EINVAL;
229 
230 		if (ti_dac->powerdown)
231 			return -EBUSY;
232 
233 		mutex_lock(&ti_dac->lock);
234 		ret = ti_dac_cmd(ti_dac, WRITE_AND_UPDATE(chan->channel), val);
235 		if (!ret)
236 			ti_dac->val[chan->channel] = val;
237 		mutex_unlock(&ti_dac->lock);
238 		break;
239 
240 	default:
241 		ret = -EINVAL;
242 	}
243 
244 	return ret;
245 }
246 
247 static int ti_dac_write_raw_get_fmt(struct iio_dev *indio_dev,
248 				    struct iio_chan_spec const *chan, long mask)
249 {
250 	return IIO_VAL_INT;
251 }
252 
253 static const struct iio_info ti_dac_info = {
254 	.read_raw	   = ti_dac_read_raw,
255 	.write_raw	   = ti_dac_write_raw,
256 	.write_raw_get_fmt = ti_dac_write_raw_get_fmt,
257 };
258 
259 static int ti_dac_probe(struct spi_device *spi)
260 {
261 	struct device *dev = &spi->dev;
262 	const struct ti_dac_spec *spec;
263 	struct ti_dac_chip *ti_dac;
264 	struct iio_dev *indio_dev;
265 	int ret;
266 
267 	indio_dev = devm_iio_device_alloc(dev, sizeof(*ti_dac));
268 	if (!indio_dev)
269 		return -ENOMEM;
270 
271 	indio_dev->info = &ti_dac_info;
272 	indio_dev->name = spi->modalias;
273 	indio_dev->modes = INDIO_DIRECT_MODE;
274 	indio_dev->channels = ti_dac_channels;
275 	spi_set_drvdata(spi, indio_dev);
276 
277 	ti_dac = iio_priv(indio_dev);
278 	ti_dac->xfer.tx_buf = &ti_dac->buf;
279 	ti_dac->xfer.len = sizeof(ti_dac->buf);
280 	spi_message_init_with_transfers(&ti_dac->mesg, &ti_dac->xfer, 1);
281 	ti_dac->mesg.spi = spi;
282 
283 	spec = &ti_dac_spec[spi_get_device_id(spi)->driver_data];
284 	indio_dev->num_channels = spec->num_channels;
285 	ti_dac->resolution = spec->resolution;
286 
287 	ti_dac->vref = devm_regulator_get(dev, "vref");
288 	if (IS_ERR(ti_dac->vref))
289 		return PTR_ERR(ti_dac->vref);
290 
291 	ret = regulator_enable(ti_dac->vref);
292 	if (ret < 0)
293 		return ret;
294 
295 	mutex_init(&ti_dac->lock);
296 
297 	ret = ti_dac_cmd(ti_dac, WRITE_ALL_UPDATE, 0);
298 	if (ret) {
299 		dev_err(dev, "failed to initialize outputs to 0\n");
300 		goto err;
301 	}
302 
303 	ret = iio_device_register(indio_dev);
304 	if (ret)
305 		goto err;
306 
307 	return 0;
308 
309 err:
310 	mutex_destroy(&ti_dac->lock);
311 	regulator_disable(ti_dac->vref);
312 	return ret;
313 }
314 
315 static int ti_dac_remove(struct spi_device *spi)
316 {
317 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
318 	struct ti_dac_chip *ti_dac = iio_priv(indio_dev);
319 
320 	iio_device_unregister(indio_dev);
321 	mutex_destroy(&ti_dac->lock);
322 	regulator_disable(ti_dac->vref);
323 
324 	return 0;
325 }
326 
327 #ifdef CONFIG_OF
328 static const struct of_device_id ti_dac_of_id[] = {
329 	{ .compatible = "ti,dac082s085" },
330 	{ .compatible = "ti,dac102s085" },
331 	{ .compatible = "ti,dac122s085" },
332 	{ .compatible = "ti,dac084s085" },
333 	{ .compatible = "ti,dac104s085" },
334 	{ .compatible = "ti,dac124s085" },
335 	{ }
336 };
337 MODULE_DEVICE_TABLE(of, ti_dac_of_id);
338 #endif
339 
340 static const struct spi_device_id ti_dac_spi_id[] = {
341 	{ "dac082s085", dual_8bit  },
342 	{ "dac102s085", dual_10bit },
343 	{ "dac122s085", dual_12bit },
344 	{ "dac084s085", quad_8bit  },
345 	{ "dac104s085", quad_10bit },
346 	{ "dac124s085", quad_12bit },
347 	{ }
348 };
349 MODULE_DEVICE_TABLE(spi, ti_dac_spi_id);
350 
351 static struct spi_driver ti_dac_driver = {
352 	.driver = {
353 		.name		= "ti-dac082s085",
354 		.of_match_table	= of_match_ptr(ti_dac_of_id),
355 	},
356 	.probe	  = ti_dac_probe,
357 	.remove   = ti_dac_remove,
358 	.id_table = ti_dac_spi_id,
359 };
360 module_spi_driver(ti_dac_driver);
361 
362 MODULE_AUTHOR("Lukas Wunner <lukas@wunner.de>");
363 MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 2/4-channel DAC driver");
364 MODULE_LICENSE("GPL v2");
365