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