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