1 /*
2 * ti-dac5571.c - Texas Instruments 8/10/12-bit 1/4-channel DAC driver
3 *
4 * Copyright (C) 2018 Prevas A/S
5 *
6 * http://www.ti.com/lit/ds/symlink/dac5571.pdf
7 * http://www.ti.com/lit/ds/symlink/dac6571.pdf
8 * http://www.ti.com/lit/ds/symlink/dac7571.pdf
9 * http://www.ti.com/lit/ds/symlink/dac5574.pdf
10 * http://www.ti.com/lit/ds/symlink/dac6574.pdf
11 * http://www.ti.com/lit/ds/symlink/dac7574.pdf
12 * http://www.ti.com/lit/ds/symlink/dac5573.pdf
13 * http://www.ti.com/lit/ds/symlink/dac6573.pdf
14 * http://www.ti.com/lit/ds/symlink/dac7573.pdf
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License (version 2) as
18 * published by the Free Software Foundation.
19 */
20
21 #include <linux/iio/iio.h>
22 #include <linux/i2c.h>
23 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/of.h>
26 #include <linux/regulator/consumer.h>
27
28 enum chip_id {
29 single_8bit, single_10bit, single_12bit,
30 quad_8bit, quad_10bit, quad_12bit
31 };
32
33 struct dac5571_spec {
34 u8 num_channels;
35 u8 resolution;
36 };
37
38 static const struct dac5571_spec dac5571_spec[] = {
39 [single_8bit] = {.num_channels = 1, .resolution = 8},
40 [single_10bit] = {.num_channels = 1, .resolution = 10},
41 [single_12bit] = {.num_channels = 1, .resolution = 12},
42 [quad_8bit] = {.num_channels = 4, .resolution = 8},
43 [quad_10bit] = {.num_channels = 4, .resolution = 10},
44 [quad_12bit] = {.num_channels = 4, .resolution = 12},
45 };
46
47 struct dac5571_data {
48 struct i2c_client *client;
49 int id;
50 struct mutex lock;
51 struct regulator *vref;
52 u16 val[4];
53 bool powerdown;
54 u8 powerdown_mode;
55 struct dac5571_spec const *spec;
56 int (*dac5571_cmd)(struct dac5571_data *data, int channel, u16 val);
57 int (*dac5571_pwrdwn)(struct dac5571_data *data, int channel, u8 pwrdwn);
58 u8 buf[3] ____cacheline_aligned;
59 };
60
61 #define DAC5571_POWERDOWN(mode) ((mode) + 1)
62 #define DAC5571_POWERDOWN_FLAG BIT(0)
63 #define DAC5571_CHANNEL_SELECT 1
64 #define DAC5571_LOADMODE_DIRECT BIT(4)
65 #define DAC5571_SINGLE_PWRDWN_BITS 4
66 #define DAC5571_QUAD_PWRDWN_BITS 6
67
dac5571_cmd_single(struct dac5571_data * data,int channel,u16 val)68 static int dac5571_cmd_single(struct dac5571_data *data, int channel, u16 val)
69 {
70 unsigned int shift;
71
72 shift = 12 - data->spec->resolution;
73 data->buf[1] = val << shift;
74 data->buf[0] = val >> (8 - shift);
75
76 if (i2c_master_send(data->client, data->buf, 2) != 2)
77 return -EIO;
78
79 return 0;
80 }
81
dac5571_cmd_quad(struct dac5571_data * data,int channel,u16 val)82 static int dac5571_cmd_quad(struct dac5571_data *data, int channel, u16 val)
83 {
84 unsigned int shift;
85
86 shift = 16 - data->spec->resolution;
87 data->buf[2] = val << shift;
88 data->buf[1] = (val >> (8 - shift));
89 data->buf[0] = (channel << DAC5571_CHANNEL_SELECT) |
90 DAC5571_LOADMODE_DIRECT;
91
92 if (i2c_master_send(data->client, data->buf, 3) != 3)
93 return -EIO;
94
95 return 0;
96 }
97
dac5571_pwrdwn_single(struct dac5571_data * data,int channel,u8 pwrdwn)98 static int dac5571_pwrdwn_single(struct dac5571_data *data, int channel, u8 pwrdwn)
99 {
100 data->buf[1] = 0;
101 data->buf[0] = pwrdwn << DAC5571_SINGLE_PWRDWN_BITS;
102
103 if (i2c_master_send(data->client, data->buf, 2) != 2)
104 return -EIO;
105
106 return 0;
107 }
108
dac5571_pwrdwn_quad(struct dac5571_data * data,int channel,u8 pwrdwn)109 static int dac5571_pwrdwn_quad(struct dac5571_data *data, int channel, u8 pwrdwn)
110 {
111 data->buf[2] = 0;
112 data->buf[1] = pwrdwn << DAC5571_QUAD_PWRDWN_BITS;
113 data->buf[0] = (channel << DAC5571_CHANNEL_SELECT) |
114 DAC5571_LOADMODE_DIRECT | DAC5571_POWERDOWN_FLAG;
115
116 if (i2c_master_send(data->client, data->buf, 3) != 3)
117 return -EIO;
118
119 return 0;
120 }
121
122 static const char *const dac5571_powerdown_modes[] = {
123 "1kohm_to_gnd", "100kohm_to_gnd", "three_state",
124 };
125
dac5571_get_powerdown_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan)126 static int dac5571_get_powerdown_mode(struct iio_dev *indio_dev,
127 const struct iio_chan_spec *chan)
128 {
129 struct dac5571_data *data = iio_priv(indio_dev);
130
131 return data->powerdown_mode;
132 }
133
dac5571_set_powerdown_mode(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,unsigned int mode)134 static int dac5571_set_powerdown_mode(struct iio_dev *indio_dev,
135 const struct iio_chan_spec *chan,
136 unsigned int mode)
137 {
138 struct dac5571_data *data = iio_priv(indio_dev);
139 int ret = 0;
140
141 if (data->powerdown_mode == mode)
142 return 0;
143
144 mutex_lock(&data->lock);
145 if (data->powerdown) {
146 ret = data->dac5571_pwrdwn(data, chan->channel,
147 DAC5571_POWERDOWN(mode));
148 if (ret)
149 goto out;
150 }
151 data->powerdown_mode = mode;
152
153 out:
154 mutex_unlock(&data->lock);
155
156 return ret;
157 }
158
159 static const struct iio_enum dac5571_powerdown_mode = {
160 .items = dac5571_powerdown_modes,
161 .num_items = ARRAY_SIZE(dac5571_powerdown_modes),
162 .get = dac5571_get_powerdown_mode,
163 .set = dac5571_set_powerdown_mode,
164 };
165
dac5571_read_powerdown(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,char * buf)166 static ssize_t dac5571_read_powerdown(struct iio_dev *indio_dev,
167 uintptr_t private,
168 const struct iio_chan_spec *chan,
169 char *buf)
170 {
171 struct dac5571_data *data = iio_priv(indio_dev);
172
173 return sprintf(buf, "%d\n", data->powerdown);
174 }
175
dac5571_write_powerdown(struct iio_dev * indio_dev,uintptr_t private,const struct iio_chan_spec * chan,const char * buf,size_t len)176 static ssize_t dac5571_write_powerdown(struct iio_dev *indio_dev,
177 uintptr_t private,
178 const struct iio_chan_spec *chan,
179 const char *buf, size_t len)
180 {
181 struct dac5571_data *data = iio_priv(indio_dev);
182 bool powerdown;
183 int ret;
184
185 ret = strtobool(buf, &powerdown);
186 if (ret)
187 return ret;
188
189 if (data->powerdown == powerdown)
190 return len;
191
192 mutex_lock(&data->lock);
193 if (powerdown)
194 ret = data->dac5571_pwrdwn(data, chan->channel,
195 DAC5571_POWERDOWN(data->powerdown_mode));
196 else
197 ret = data->dac5571_cmd(data, chan->channel, data->val[0]);
198 if (ret)
199 goto out;
200
201 data->powerdown = 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_SHARED_BY_TYPE,
216 },
217 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE, &dac5571_powerdown_mode),
218 IIO_ENUM_AVAILABLE("powerdown_mode", &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
dac5571_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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
dac5571_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)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)
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
dac5571_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)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
dac5571_probe(struct i2c_client * client,const struct i2c_device_id * id)310 static int dac5571_probe(struct i2c_client *client,
311 const struct i2c_device_id *id)
312 {
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->dev.parent = dev;
328 indio_dev->dev.of_node = client->dev.of_node;
329 indio_dev->info = &dac5571_info;
330 indio_dev->name = id->name;
331 indio_dev->modes = INDIO_DIRECT_MODE;
332 indio_dev->channels = dac5571_channels;
333
334 spec = &dac5571_spec[id->driver_data];
335 indio_dev->num_channels = spec->num_channels;
336 data->spec = spec;
337
338 data->vref = devm_regulator_get(dev, "vref");
339 if (IS_ERR(data->vref))
340 return PTR_ERR(data->vref);
341
342 ret = regulator_enable(data->vref);
343 if (ret < 0)
344 return ret;
345
346 mutex_init(&data->lock);
347
348 switch (spec->num_channels) {
349 case 1:
350 data->dac5571_cmd = dac5571_cmd_single;
351 data->dac5571_pwrdwn = dac5571_pwrdwn_single;
352 break;
353 case 4:
354 data->dac5571_cmd = dac5571_cmd_quad;
355 data->dac5571_pwrdwn = dac5571_pwrdwn_quad;
356 break;
357 default:
358 ret = -EINVAL;
359 goto err;
360 }
361
362 for (i = 0; i < spec->num_channels; i++) {
363 ret = data->dac5571_cmd(data, i, 0);
364 if (ret) {
365 dev_err(dev, "failed to initialize channel %d to 0\n", i);
366 goto err;
367 }
368 }
369
370 ret = iio_device_register(indio_dev);
371 if (ret)
372 goto err;
373
374 return 0;
375
376 err:
377 regulator_disable(data->vref);
378 return ret;
379 }
380
dac5571_remove(struct i2c_client * i2c)381 static int dac5571_remove(struct i2c_client *i2c)
382 {
383 struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
384 struct dac5571_data *data = iio_priv(indio_dev);
385
386 iio_device_unregister(indio_dev);
387 regulator_disable(data->vref);
388
389 return 0;
390 }
391
392 #ifdef CONFIG_OF
393 static const struct of_device_id dac5571_of_id[] = {
394 {.compatible = "ti,dac5571"},
395 {.compatible = "ti,dac6571"},
396 {.compatible = "ti,dac7571"},
397 {.compatible = "ti,dac5574"},
398 {.compatible = "ti,dac6574"},
399 {.compatible = "ti,dac7574"},
400 {.compatible = "ti,dac5573"},
401 {.compatible = "ti,dac6573"},
402 {.compatible = "ti,dac7573"},
403 {}
404 };
405 MODULE_DEVICE_TABLE(of, dac5571_of_id);
406 #endif
407
408 static const struct i2c_device_id dac5571_id[] = {
409 {"dac5571", single_8bit},
410 {"dac6571", single_10bit},
411 {"dac7571", single_12bit},
412 {"dac5574", quad_8bit},
413 {"dac6574", quad_10bit},
414 {"dac7574", quad_12bit},
415 {"dac5573", quad_8bit},
416 {"dac6573", quad_10bit},
417 {"dac7573", quad_12bit},
418 {}
419 };
420 MODULE_DEVICE_TABLE(i2c, dac5571_id);
421
422 static struct i2c_driver dac5571_driver = {
423 .driver = {
424 .name = "ti-dac5571",
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.nyekjaer@prevas.dk>");
433 MODULE_DESCRIPTION("Texas Instruments 8/10/12-bit 1/4-channel DAC driver");
434 MODULE_LICENSE("GPL v2");
435