xref: /linux/drivers/iio/dac/ti-dac5571.c (revision bde5d79d00255db609fe9d859eef8c7b6d38b137)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ti-dac5571.c - Texas Instruments 8/10/12-bit 1/4-channel DAC driver
4  *
5  * Copyright (C) 2018 Prevas A/S
6  *
7  * https://www.ti.com/lit/ds/symlink/dac5571.pdf
8  * https://www.ti.com/lit/ds/symlink/dac6571.pdf
9  * https://www.ti.com/lit/ds/symlink/dac7571.pdf
10  * https://www.ti.com/lit/ds/symlink/dac5574.pdf
11  * https://www.ti.com/lit/ds/symlink/dac6574.pdf
12  * https://www.ti.com/lit/ds/symlink/dac7574.pdf
13  * https://www.ti.com/lit/ds/symlink/dac5573.pdf
14  * https://www.ti.com/lit/ds/symlink/dac6573.pdf
15  * https://www.ti.com/lit/ds/symlink/dac7573.pdf
16  * https://www.ti.com/lit/ds/symlink/dac081c081.pdf
17  * https://www.ti.com/lit/ds/symlink/dac121c081.pdf
18  */
19 
20 #include <linux/iio/iio.h>
21 #include <linux/i2c.h>
22 #include <linux/module.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/property.h>
25 #include <linux/regulator/consumer.h>
26 
27 enum chip_id {
28 	single_8bit, single_10bit, single_12bit,
29 	quad_8bit, quad_10bit, quad_12bit
30 };
31 
32 struct dac5571_spec {
33 	u8 num_channels;
34 	u8 resolution;
35 };
36 
37 static const struct dac5571_spec dac5571_spec[] = {
38 	[single_8bit]  = {.num_channels = 1, .resolution =  8},
39 	[single_10bit] = {.num_channels = 1, .resolution = 10},
40 	[single_12bit] = {.num_channels = 1, .resolution = 12},
41 	[quad_8bit]    = {.num_channels = 4, .resolution =  8},
42 	[quad_10bit]   = {.num_channels = 4, .resolution = 10},
43 	[quad_12bit]   = {.num_channels = 4, .resolution = 12},
44 };
45 
46 struct dac5571_data {
47 	struct i2c_client *client;
48 	int id;
49 	struct mutex lock;
50 	struct regulator *vref;
51 	u16 val[4];
52 	bool powerdown[4];
53 	u8 powerdown_mode[4];
54 	struct dac5571_spec const *spec;
55 	int (*dac5571_cmd)(struct dac5571_data *data, int channel, u16 val);
56 	int (*dac5571_pwrdwn)(struct dac5571_data *data, int channel, u8 pwrdwn);
57 	u8 buf[3] __aligned(IIO_DMA_MINALIGN);
58 };
59 
60 #define DAC5571_POWERDOWN(mode)		((mode) + 1)
61 #define DAC5571_POWERDOWN_FLAG		BIT(0)
62 #define DAC5571_CHANNEL_SELECT		1
63 #define DAC5571_LOADMODE_DIRECT		BIT(4)
64 #define DAC5571_SINGLE_PWRDWN_BITS	4
65 #define DAC5571_QUAD_PWRDWN_BITS	6
66 
67 static int dac5571_cmd_single(struct dac5571_data *data, int channel, u16 val)
68 {
69 	unsigned int shift;
70 
71 	shift = 12 - data->spec->resolution;
72 	data->buf[1] = val << shift;
73 	data->buf[0] = val >> (8 - shift);
74 
75 	if (i2c_master_send(data->client, data->buf, 2) != 2)
76 		return -EIO;
77 
78 	return 0;
79 }
80 
81 static int dac5571_cmd_quad(struct dac5571_data *data, int channel, u16 val)
82 {
83 	unsigned int shift;
84 
85 	shift = 16 - data->spec->resolution;
86 	data->buf[2] = val << shift;
87 	data->buf[1] = (val >> (8 - shift));
88 	data->buf[0] = (channel << DAC5571_CHANNEL_SELECT) |
89 		       DAC5571_LOADMODE_DIRECT;
90 
91 	if (i2c_master_send(data->client, data->buf, 3) != 3)
92 		return -EIO;
93 
94 	return 0;
95 }
96 
97 static int dac5571_pwrdwn_single(struct dac5571_data *data, int channel, u8 pwrdwn)
98 {
99 	data->buf[1] = 0;
100 	data->buf[0] = pwrdwn << DAC5571_SINGLE_PWRDWN_BITS;
101 
102 	if (i2c_master_send(data->client, data->buf, 2) != 2)
103 		return -EIO;
104 
105 	return 0;
106 }
107 
108 static int dac5571_pwrdwn_quad(struct dac5571_data *data, int channel, u8 pwrdwn)
109 {
110 	data->buf[2] = 0;
111 	data->buf[1] = pwrdwn << DAC5571_QUAD_PWRDWN_BITS;
112 	data->buf[0] = (channel << DAC5571_CHANNEL_SELECT) |
113 		       DAC5571_LOADMODE_DIRECT | DAC5571_POWERDOWN_FLAG;
114 
115 	if (i2c_master_send(data->client, data->buf, 3) != 3)
116 		return -EIO;
117 
118 	return 0;
119 }
120 
121 static const char *const dac5571_powerdown_modes[] = {
122 	"1kohm_to_gnd", "100kohm_to_gnd", "three_state",
123 };
124 
125 static int dac5571_get_powerdown_mode(struct iio_dev *indio_dev,
126 				      const struct iio_chan_spec *chan)
127 {
128 	struct dac5571_data *data = iio_priv(indio_dev);
129 
130 	return data->powerdown_mode[chan->channel];
131 }
132 
133 static int dac5571_set_powerdown_mode(struct iio_dev *indio_dev,
134 				      const struct iio_chan_spec *chan,
135 				      unsigned int mode)
136 {
137 	struct dac5571_data *data = iio_priv(indio_dev);
138 	int ret = 0;
139 
140 	if (data->powerdown_mode[chan->channel] == mode)
141 		return 0;
142 
143 	mutex_lock(&data->lock);
144 	if (data->powerdown[chan->channel]) {
145 		ret = data->dac5571_pwrdwn(data, chan->channel,
146 					   DAC5571_POWERDOWN(mode));
147 		if (ret)
148 			goto out;
149 	}
150 	data->powerdown_mode[chan->channel] = mode;
151 
152  out:
153 	mutex_unlock(&data->lock);
154 
155 	return ret;
156 }
157 
158 static const struct iio_enum dac5571_powerdown_mode = {
159 	.items = dac5571_powerdown_modes,
160 	.num_items = ARRAY_SIZE(dac5571_powerdown_modes),
161 	.get = dac5571_get_powerdown_mode,
162 	.set = dac5571_set_powerdown_mode,
163 };
164 
165 static ssize_t dac5571_read_powerdown(struct iio_dev *indio_dev,
166 				      uintptr_t private,
167 				      const struct iio_chan_spec *chan,
168 				      char *buf)
169 {
170 	struct dac5571_data *data = iio_priv(indio_dev);
171 
172 	return sysfs_emit(buf, "%d\n", data->powerdown[chan->channel]);
173 }
174 
175 static ssize_t dac5571_write_powerdown(struct iio_dev *indio_dev,
176 				       uintptr_t private,
177 				       const struct iio_chan_spec *chan,
178 				       const char *buf, size_t len)
179 {
180 	struct dac5571_data *data = iio_priv(indio_dev);
181 	bool powerdown;
182 	int ret;
183 
184 	ret = kstrtobool(buf, &powerdown);
185 	if (ret)
186 		return ret;
187 
188 	if (data->powerdown[chan->channel] == powerdown)
189 		return len;
190 
191 	mutex_lock(&data->lock);
192 	if (powerdown)
193 		ret = data->dac5571_pwrdwn(data, chan->channel,
194 			    DAC5571_POWERDOWN(data->powerdown_mode[chan->channel]));
195 	else
196 		ret = data->dac5571_cmd(data, chan->channel,
197 				data->val[chan->channel]);
198 	if (ret)
199 		goto out;
200 
201 	data->powerdown[chan->channel] = powerdown;
202 
203  out:
204 	mutex_unlock(&data->lock);
205 
206 	return ret ? ret : len;
207 }
208 
209 
210 static const struct iio_chan_spec_ext_info dac5571_ext_info[] = {
211 	{
212 		.name	   = "powerdown",
213 		.read	   = dac5571_read_powerdown,
214 		.write	   = dac5571_write_powerdown,
215 		.shared	   = IIO_SEPARATE,
216 	},
217 	IIO_ENUM("powerdown_mode", IIO_SEPARATE, &dac5571_powerdown_mode),
218 	IIO_ENUM_AVAILABLE("powerdown_mode", IIO_SHARED_BY_TYPE, &dac5571_powerdown_mode),
219 	{},
220 };
221 
222 #define dac5571_CHANNEL(chan, name) {				\
223 	.type = IIO_VOLTAGE,					\
224 	.channel = (chan),					\
225 	.address = (chan),					\
226 	.indexed = true,					\
227 	.output = true,						\
228 	.datasheet_name = name,					\
229 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
230 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
231 	.ext_info = dac5571_ext_info,				\
232 }
233 
234 static const struct iio_chan_spec dac5571_channels[] = {
235 	dac5571_CHANNEL(0, "A"),
236 	dac5571_CHANNEL(1, "B"),
237 	dac5571_CHANNEL(2, "C"),
238 	dac5571_CHANNEL(3, "D"),
239 };
240 
241 static int dac5571_read_raw(struct iio_dev *indio_dev,
242 			    struct iio_chan_spec const *chan,
243 			    int *val, int *val2, long mask)
244 {
245 	struct dac5571_data *data = iio_priv(indio_dev);
246 	int ret;
247 
248 	switch (mask) {
249 	case IIO_CHAN_INFO_RAW:
250 		*val = data->val[chan->channel];
251 		return IIO_VAL_INT;
252 
253 	case IIO_CHAN_INFO_SCALE:
254 		ret = regulator_get_voltage(data->vref);
255 		if (ret < 0)
256 			return ret;
257 
258 		*val = ret / 1000;
259 		*val2 = data->spec->resolution;
260 		return IIO_VAL_FRACTIONAL_LOG2;
261 
262 	default:
263 		return -EINVAL;
264 	}
265 }
266 
267 static int dac5571_write_raw(struct iio_dev *indio_dev,
268 			     struct iio_chan_spec const *chan,
269 			     int val, int val2, long mask)
270 {
271 	struct dac5571_data *data = iio_priv(indio_dev);
272 	int ret;
273 
274 	switch (mask) {
275 	case IIO_CHAN_INFO_RAW:
276 		if (data->val[chan->channel] == val)
277 			return 0;
278 
279 		if (val >= (1 << data->spec->resolution) || val < 0)
280 			return -EINVAL;
281 
282 		if (data->powerdown[chan->channel])
283 			return -EBUSY;
284 
285 		mutex_lock(&data->lock);
286 		ret = data->dac5571_cmd(data, chan->channel, val);
287 		if (ret == 0)
288 			data->val[chan->channel] = val;
289 		mutex_unlock(&data->lock);
290 		return ret;
291 
292 	default:
293 		return -EINVAL;
294 	}
295 }
296 
297 static int dac5571_write_raw_get_fmt(struct iio_dev *indio_dev,
298 				     struct iio_chan_spec const *chan,
299 				     long mask)
300 {
301 	return IIO_VAL_INT;
302 }
303 
304 static const struct iio_info dac5571_info = {
305 	.read_raw = dac5571_read_raw,
306 	.write_raw = dac5571_write_raw,
307 	.write_raw_get_fmt = dac5571_write_raw_get_fmt,
308 };
309 
310 static int dac5571_probe(struct i2c_client *client)
311 {
312 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
313 	struct device *dev = &client->dev;
314 	const struct dac5571_spec *spec;
315 	struct dac5571_data *data;
316 	struct iio_dev *indio_dev;
317 	int ret, i;
318 
319 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
320 	if (!indio_dev)
321 		return -ENOMEM;
322 
323 	data = iio_priv(indio_dev);
324 	i2c_set_clientdata(client, indio_dev);
325 	data->client = client;
326 
327 	indio_dev->info = &dac5571_info;
328 	indio_dev->name = id->name;
329 	indio_dev->modes = INDIO_DIRECT_MODE;
330 	indio_dev->channels = dac5571_channels;
331 
332 	spec = i2c_get_match_data(client);
333 
334 	indio_dev->num_channels = spec->num_channels;
335 	data->spec = spec;
336 
337 	data->vref = devm_regulator_get(dev, "vref");
338 	if (IS_ERR(data->vref))
339 		return PTR_ERR(data->vref);
340 
341 	ret = regulator_enable(data->vref);
342 	if (ret < 0)
343 		return ret;
344 
345 	mutex_init(&data->lock);
346 
347 	switch (spec->num_channels) {
348 	case 1:
349 		data->dac5571_cmd = dac5571_cmd_single;
350 		data->dac5571_pwrdwn = dac5571_pwrdwn_single;
351 		break;
352 	case 4:
353 		data->dac5571_cmd = dac5571_cmd_quad;
354 		data->dac5571_pwrdwn = dac5571_pwrdwn_quad;
355 		break;
356 	default:
357 		ret = -EINVAL;
358 		goto err;
359 	}
360 
361 	for (i = 0; i < spec->num_channels; i++) {
362 		ret = data->dac5571_cmd(data, i, 0);
363 		if (ret) {
364 			dev_err(dev, "failed to initialize channel %d to 0\n", i);
365 			goto err;
366 		}
367 	}
368 
369 	ret = iio_device_register(indio_dev);
370 	if (ret)
371 		goto err;
372 
373 	return 0;
374 
375  err:
376 	regulator_disable(data->vref);
377 	return ret;
378 }
379 
380 static void dac5571_remove(struct i2c_client *i2c)
381 {
382 	struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
383 	struct dac5571_data *data = iio_priv(indio_dev);
384 
385 	iio_device_unregister(indio_dev);
386 	regulator_disable(data->vref);
387 }
388 
389 static const struct of_device_id dac5571_of_id[] = {
390 	{.compatible = "ti,dac081c081", .data = &dac5571_spec[single_8bit] },
391 	{.compatible = "ti,dac121c081", .data = &dac5571_spec[single_12bit] },
392 	{.compatible = "ti,dac5571", .data = &dac5571_spec[single_8bit] },
393 	{.compatible = "ti,dac6571", .data = &dac5571_spec[single_10bit] },
394 	{.compatible = "ti,dac7571", .data = &dac5571_spec[single_12bit] },
395 	{.compatible = "ti,dac5574", .data = &dac5571_spec[quad_8bit] },
396 	{.compatible = "ti,dac6574", .data = &dac5571_spec[quad_10bit] },
397 	{.compatible = "ti,dac7574", .data = &dac5571_spec[quad_12bit] },
398 	{.compatible = "ti,dac5573", .data = &dac5571_spec[quad_8bit] },
399 	{.compatible = "ti,dac6573", .data = &dac5571_spec[quad_10bit] },
400 	{.compatible = "ti,dac7573", .data = &dac5571_spec[quad_12bit] },
401 	{}
402 };
403 MODULE_DEVICE_TABLE(of, dac5571_of_id);
404 
405 static const struct i2c_device_id dac5571_id[] = {
406 	{"dac081c081", (kernel_ulong_t)&dac5571_spec[single_8bit] },
407 	{"dac121c081", (kernel_ulong_t)&dac5571_spec[single_12bit] },
408 	{"dac5571", (kernel_ulong_t)&dac5571_spec[single_8bit] },
409 	{"dac6571", (kernel_ulong_t)&dac5571_spec[single_10bit] },
410 	{"dac7571", (kernel_ulong_t)&dac5571_spec[single_12bit] },
411 	{"dac5574", (kernel_ulong_t)&dac5571_spec[quad_8bit] },
412 	{"dac6574", (kernel_ulong_t)&dac5571_spec[quad_10bit] },
413 	{"dac7574", (kernel_ulong_t)&dac5571_spec[quad_12bit] },
414 	{"dac5573", (kernel_ulong_t)&dac5571_spec[quad_8bit] },
415 	{"dac6573", (kernel_ulong_t)&dac5571_spec[quad_10bit] },
416 	{"dac7573", (kernel_ulong_t)&dac5571_spec[quad_12bit] },
417 	{}
418 };
419 MODULE_DEVICE_TABLE(i2c, dac5571_id);
420 
421 static struct i2c_driver dac5571_driver = {
422 	.driver = {
423 		   .name = "ti-dac5571",
424 		   .of_match_table = dac5571_of_id,
425 	},
426 	.probe = dac5571_probe,
427 	.remove   = dac5571_remove,
428 	.id_table = dac5571_id,
429 };
430 module_i2c_driver(dac5571_driver);
431 
432 MODULE_AUTHOR("Sean Nyekjaer <sean@geanix.dk>");
433 MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 1/4-channel DAC driver");
434 MODULE_LICENSE("GPL v2");
435