1
2
3
4
5
6
7
8
9
10#include <linux/device.h>
11#include <linux/err.h>
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/spi/spi.h>
15#include <linux/slab.h>
16#include <linux/sysfs.h>
17#include <linux/regulator/consumer.h>
18
19#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
21#include "dac.h"
22
23#define AD5360_CMD(x) ((x) << 22)
24#define AD5360_ADDR(x) ((x) << 16)
25
26#define AD5360_READBACK_TYPE(x) ((x) << 13)
27#define AD5360_READBACK_ADDR(x) ((x) << 7)
28
29#define AD5360_CHAN_ADDR(chan) ((chan) + 0x8)
30
31#define AD5360_CMD_WRITE_DATA 0x3
32#define AD5360_CMD_WRITE_OFFSET 0x2
33#define AD5360_CMD_WRITE_GAIN 0x1
34#define AD5360_CMD_SPECIAL_FUNCTION 0x0
35
36
37#define AD5360_REG_SF_NOP 0x0
38#define AD5360_REG_SF_CTRL 0x1
39#define AD5360_REG_SF_OFS(x) (0x2 + (x))
40#define AD5360_REG_SF_READBACK 0x5
41
42#define AD5360_SF_CTRL_PWR_DOWN BIT(0)
43
44#define AD5360_READBACK_X1A 0x0
45#define AD5360_READBACK_X1B 0x1
46#define AD5360_READBACK_OFFSET 0x2
47#define AD5360_READBACK_GAIN 0x3
48#define AD5360_READBACK_SF 0x4
49
50
51
52
53
54
55
56
57
58
59struct ad5360_chip_info {
60 struct iio_chan_spec channel_template;
61 unsigned int num_channels;
62 unsigned int channels_per_group;
63 unsigned int num_vrefs;
64};
65
66
67
68
69
70
71
72
73
74
75struct ad5360_state {
76 struct spi_device *spi;
77 const struct ad5360_chip_info *chip_info;
78 struct regulator_bulk_data vref_reg[3];
79 unsigned int ctrl;
80
81
82
83
84
85 union {
86 __be32 d32;
87 u8 d8[4];
88 } data[2] ____cacheline_aligned;
89};
90
91enum ad5360_type {
92 ID_AD5360,
93 ID_AD5361,
94 ID_AD5362,
95 ID_AD5363,
96 ID_AD5370,
97 ID_AD5371,
98 ID_AD5372,
99 ID_AD5373,
100};
101
102#define AD5360_CHANNEL(bits) { \
103 .type = IIO_VOLTAGE, \
104 .indexed = 1, \
105 .output = 1, \
106 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
107 IIO_CHAN_INFO_SCALE_SEPARATE_BIT | \
108 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
109 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
110 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT, \
111 .scan_type = IIO_ST('u', (bits), 16, 16 - (bits)) \
112}
113
114static const struct ad5360_chip_info ad5360_chip_info_tbl[] = {
115 [ID_AD5360] = {
116 .channel_template = AD5360_CHANNEL(16),
117 .num_channels = 16,
118 .channels_per_group = 8,
119 .num_vrefs = 2,
120 },
121 [ID_AD5361] = {
122 .channel_template = AD5360_CHANNEL(14),
123 .num_channels = 16,
124 .channels_per_group = 8,
125 .num_vrefs = 2,
126 },
127 [ID_AD5362] = {
128 .channel_template = AD5360_CHANNEL(16),
129 .num_channels = 8,
130 .channels_per_group = 4,
131 .num_vrefs = 2,
132 },
133 [ID_AD5363] = {
134 .channel_template = AD5360_CHANNEL(14),
135 .num_channels = 8,
136 .channels_per_group = 4,
137 .num_vrefs = 2,
138 },
139 [ID_AD5370] = {
140 .channel_template = AD5360_CHANNEL(16),
141 .num_channels = 40,
142 .channels_per_group = 8,
143 .num_vrefs = 2,
144 },
145 [ID_AD5371] = {
146 .channel_template = AD5360_CHANNEL(14),
147 .num_channels = 40,
148 .channels_per_group = 8,
149 .num_vrefs = 3,
150 },
151 [ID_AD5372] = {
152 .channel_template = AD5360_CHANNEL(16),
153 .num_channels = 32,
154 .channels_per_group = 8,
155 .num_vrefs = 2,
156 },
157 [ID_AD5373] = {
158 .channel_template = AD5360_CHANNEL(14),
159 .num_channels = 32,
160 .channels_per_group = 8,
161 .num_vrefs = 2,
162 },
163};
164
165static unsigned int ad5360_get_channel_vref_index(struct ad5360_state *st,
166 unsigned int channel)
167{
168 unsigned int i;
169
170
171
172 i = channel / st->chip_info->channels_per_group;
173 if (i >= st->chip_info->num_vrefs)
174 i = st->chip_info->num_vrefs - 1;
175
176 return i;
177}
178
179static int ad5360_get_channel_vref(struct ad5360_state *st,
180 unsigned int channel)
181{
182 unsigned int i = ad5360_get_channel_vref_index(st, channel);
183
184 return regulator_get_voltage(st->vref_reg[i].consumer);
185}
186
187
188static int ad5360_write_unlocked(struct iio_dev *indio_dev,
189 unsigned int cmd, unsigned int addr, unsigned int val,
190 unsigned int shift)
191{
192 struct ad5360_state *st = iio_priv(indio_dev);
193
194 val <<= shift;
195 val |= AD5360_CMD(cmd) | AD5360_ADDR(addr);
196 st->data[0].d32 = cpu_to_be32(val);
197
198 return spi_write(st->spi, &st->data[0].d8[1], 3);
199}
200
201static int ad5360_write(struct iio_dev *indio_dev, unsigned int cmd,
202 unsigned int addr, unsigned int val, unsigned int shift)
203{
204 int ret;
205
206 mutex_lock(&indio_dev->mlock);
207 ret = ad5360_write_unlocked(indio_dev, cmd, addr, val, shift);
208 mutex_unlock(&indio_dev->mlock);
209
210 return ret;
211}
212
213static int ad5360_read(struct iio_dev *indio_dev, unsigned int type,
214 unsigned int addr)
215{
216 struct ad5360_state *st = iio_priv(indio_dev);
217 struct spi_message m;
218 int ret;
219 struct spi_transfer t[] = {
220 {
221 .tx_buf = &st->data[0].d8[1],
222 .len = 3,
223 .cs_change = 1,
224 }, {
225 .rx_buf = &st->data[1].d8[1],
226 .len = 3,
227 },
228 };
229
230 spi_message_init(&m);
231 spi_message_add_tail(&t[0], &m);
232 spi_message_add_tail(&t[1], &m);
233
234 mutex_lock(&indio_dev->mlock);
235
236 st->data[0].d32 = cpu_to_be32(AD5360_CMD(AD5360_CMD_SPECIAL_FUNCTION) |
237 AD5360_ADDR(AD5360_REG_SF_READBACK) |
238 AD5360_READBACK_TYPE(type) |
239 AD5360_READBACK_ADDR(addr));
240
241 ret = spi_sync(st->spi, &m);
242 if (ret >= 0)
243 ret = be32_to_cpu(st->data[1].d32) & 0xffff;
244
245 mutex_unlock(&indio_dev->mlock);
246
247 return ret;
248}
249
250static ssize_t ad5360_read_dac_powerdown(struct device *dev,
251 struct device_attribute *attr,
252 char *buf)
253{
254 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
255 struct ad5360_state *st = iio_priv(indio_dev);
256
257 return sprintf(buf, "%d\n", (bool)(st->ctrl & AD5360_SF_CTRL_PWR_DOWN));
258}
259
260static int ad5360_update_ctrl(struct iio_dev *indio_dev, unsigned int set,
261 unsigned int clr)
262{
263 struct ad5360_state *st = iio_priv(indio_dev);
264 unsigned int ret;
265
266 mutex_lock(&indio_dev->mlock);
267
268 st->ctrl |= set;
269 st->ctrl &= ~clr;
270
271 ret = ad5360_write_unlocked(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
272 AD5360_REG_SF_CTRL, st->ctrl, 0);
273
274 mutex_unlock(&indio_dev->mlock);
275
276 return ret;
277}
278
279static ssize_t ad5360_write_dac_powerdown(struct device *dev,
280 struct device_attribute *attr, const char *buf, size_t len)
281{
282 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
283 bool pwr_down;
284 int ret;
285
286 ret = strtobool(buf, &pwr_down);
287 if (ret)
288 return ret;
289
290 if (pwr_down)
291 ret = ad5360_update_ctrl(indio_dev, AD5360_SF_CTRL_PWR_DOWN, 0);
292 else
293 ret = ad5360_update_ctrl(indio_dev, 0, AD5360_SF_CTRL_PWR_DOWN);
294
295 return ret ? ret : len;
296}
297
298static IIO_DEVICE_ATTR(out_voltage_powerdown,
299 S_IRUGO | S_IWUSR,
300 ad5360_read_dac_powerdown,
301 ad5360_write_dac_powerdown, 0);
302
303static struct attribute *ad5360_attributes[] = {
304 &iio_dev_attr_out_voltage_powerdown.dev_attr.attr,
305 NULL,
306};
307
308static const struct attribute_group ad5360_attribute_group = {
309 .attrs = ad5360_attributes,
310};
311
312static int ad5360_write_raw(struct iio_dev *indio_dev,
313 struct iio_chan_spec const *chan,
314 int val,
315 int val2,
316 long mask)
317{
318 struct ad5360_state *st = iio_priv(indio_dev);
319 int max_val = (1 << chan->scan_type.realbits);
320 unsigned int ofs_index;
321
322 switch (mask) {
323 case IIO_CHAN_INFO_RAW:
324 if (val >= max_val || val < 0)
325 return -EINVAL;
326
327 return ad5360_write(indio_dev, AD5360_CMD_WRITE_DATA,
328 chan->address, val, chan->scan_type.shift);
329
330 case IIO_CHAN_INFO_CALIBBIAS:
331 if (val >= max_val || val < 0)
332 return -EINVAL;
333
334 return ad5360_write(indio_dev, AD5360_CMD_WRITE_OFFSET,
335 chan->address, val, chan->scan_type.shift);
336
337 case IIO_CHAN_INFO_CALIBSCALE:
338 if (val >= max_val || val < 0)
339 return -EINVAL;
340
341 return ad5360_write(indio_dev, AD5360_CMD_WRITE_GAIN,
342 chan->address, val, chan->scan_type.shift);
343
344 case IIO_CHAN_INFO_OFFSET:
345 if (val <= -max_val || val > 0)
346 return -EINVAL;
347
348 val = -val;
349
350
351
352
353 val >>= (chan->scan_type.realbits - 14);
354
355
356
357
358 ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
359 return ad5360_write(indio_dev, AD5360_CMD_SPECIAL_FUNCTION,
360 AD5360_REG_SF_OFS(ofs_index), val, 0);
361 default:
362 break;
363 }
364
365 return -EINVAL;
366}
367
368static int ad5360_read_raw(struct iio_dev *indio_dev,
369 struct iio_chan_spec const *chan,
370 int *val,
371 int *val2,
372 long m)
373{
374 struct ad5360_state *st = iio_priv(indio_dev);
375 unsigned int ofs_index;
376 int scale_uv;
377 int ret;
378
379 switch (m) {
380 case IIO_CHAN_INFO_RAW:
381 ret = ad5360_read(indio_dev, AD5360_READBACK_X1A,
382 chan->address);
383 if (ret < 0)
384 return ret;
385 *val = ret >> chan->scan_type.shift;
386 return IIO_VAL_INT;
387 case IIO_CHAN_INFO_SCALE:
388
389 scale_uv = ad5360_get_channel_vref(st, chan->channel) * 4 * 100;
390 if (scale_uv < 0)
391 return scale_uv;
392
393 scale_uv >>= (chan->scan_type.realbits);
394 *val = scale_uv / 100000;
395 *val2 = (scale_uv % 100000) * 10;
396 return IIO_VAL_INT_PLUS_MICRO;
397 case IIO_CHAN_INFO_CALIBBIAS:
398 ret = ad5360_read(indio_dev, AD5360_READBACK_OFFSET,
399 chan->address);
400 if (ret < 0)
401 return ret;
402 *val = ret;
403 return IIO_VAL_INT;
404 case IIO_CHAN_INFO_CALIBSCALE:
405 ret = ad5360_read(indio_dev, AD5360_READBACK_GAIN,
406 chan->address);
407 if (ret < 0)
408 return ret;
409 *val = ret;
410 return IIO_VAL_INT;
411 case IIO_CHAN_INFO_OFFSET:
412 ofs_index = ad5360_get_channel_vref_index(st, chan->channel);
413 ret = ad5360_read(indio_dev, AD5360_READBACK_SF,
414 AD5360_REG_SF_OFS(ofs_index));
415 if (ret < 0)
416 return ret;
417
418 ret <<= (chan->scan_type.realbits - 14);
419 *val = -ret;
420 return IIO_VAL_INT;
421 }
422
423 return -EINVAL;
424}
425
426static const struct iio_info ad5360_info = {
427 .read_raw = ad5360_read_raw,
428 .write_raw = ad5360_write_raw,
429 .attrs = &ad5360_attribute_group,
430 .driver_module = THIS_MODULE,
431};
432
433static const char * const ad5360_vref_name[] = {
434 "vref0", "vref1", "vref2"
435};
436
437static int __devinit ad5360_alloc_channels(struct iio_dev *indio_dev)
438{
439 struct ad5360_state *st = iio_priv(indio_dev);
440 struct iio_chan_spec *channels;
441 unsigned int i;
442
443 channels = kcalloc(st->chip_info->num_channels,
444 sizeof(struct iio_chan_spec), GFP_KERNEL);
445
446 if (!channels)
447 return -ENOMEM;
448
449 for (i = 0; i < st->chip_info->num_channels; ++i) {
450 channels[i] = st->chip_info->channel_template;
451 channels[i].channel = i;
452 channels[i].address = AD5360_CHAN_ADDR(i);
453 }
454
455 indio_dev->channels = channels;
456
457 return 0;
458}
459
460static int __devinit ad5360_probe(struct spi_device *spi)
461{
462 enum ad5360_type type = spi_get_device_id(spi)->driver_data;
463 struct iio_dev *indio_dev;
464 struct ad5360_state *st;
465 unsigned int i;
466 int ret;
467
468 indio_dev = iio_device_alloc(sizeof(*st));
469 if (indio_dev == NULL) {
470 dev_err(&spi->dev, "Failed to allocate iio device\n");
471 return -ENOMEM;
472 }
473
474 st = iio_priv(indio_dev);
475 spi_set_drvdata(spi, indio_dev);
476
477 st->chip_info = &ad5360_chip_info_tbl[type];
478 st->spi = spi;
479
480 indio_dev->dev.parent = &spi->dev;
481 indio_dev->name = spi_get_device_id(spi)->name;
482 indio_dev->info = &ad5360_info;
483 indio_dev->modes = INDIO_DIRECT_MODE;
484 indio_dev->num_channels = st->chip_info->num_channels;
485
486 ret = ad5360_alloc_channels(indio_dev);
487 if (ret) {
488 dev_err(&spi->dev, "Failed to allocate channel spec: %d\n", ret);
489 goto error_free;
490 }
491
492 for (i = 0; i < st->chip_info->num_vrefs; ++i)
493 st->vref_reg[i].supply = ad5360_vref_name[i];
494
495 ret = regulator_bulk_get(&st->spi->dev, st->chip_info->num_vrefs,
496 st->vref_reg);
497 if (ret) {
498 dev_err(&spi->dev, "Failed to request vref regulators: %d\n", ret);
499 goto error_free_channels;
500 }
501
502 ret = regulator_bulk_enable(st->chip_info->num_vrefs, st->vref_reg);
503 if (ret) {
504 dev_err(&spi->dev, "Failed to enable vref regulators: %d\n", ret);
505 goto error_free_reg;
506 }
507
508 ret = iio_device_register(indio_dev);
509 if (ret) {
510 dev_err(&spi->dev, "Failed to register iio device: %d\n", ret);
511 goto error_disable_reg;
512 }
513
514 return 0;
515
516error_disable_reg:
517 regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
518error_free_reg:
519 regulator_bulk_free(st->chip_info->num_vrefs, st->vref_reg);
520error_free_channels:
521 kfree(indio_dev->channels);
522error_free:
523 iio_device_free(indio_dev);
524
525 return ret;
526}
527
528static int __devexit ad5360_remove(struct spi_device *spi)
529{
530 struct iio_dev *indio_dev = spi_get_drvdata(spi);
531 struct ad5360_state *st = iio_priv(indio_dev);
532
533 iio_device_unregister(indio_dev);
534
535 kfree(indio_dev->channels);
536
537 regulator_bulk_disable(st->chip_info->num_vrefs, st->vref_reg);
538 regulator_bulk_free(st->chip_info->num_vrefs, st->vref_reg);
539
540 iio_device_free(indio_dev);
541
542 return 0;
543}
544
545static const struct spi_device_id ad5360_ids[] = {
546 { "ad5360", ID_AD5360 },
547 { "ad5361", ID_AD5361 },
548 { "ad5362", ID_AD5362 },
549 { "ad5363", ID_AD5363 },
550 { "ad5370", ID_AD5370 },
551 { "ad5371", ID_AD5371 },
552 { "ad5372", ID_AD5372 },
553 { "ad5373", ID_AD5373 },
554 {}
555};
556MODULE_DEVICE_TABLE(spi, ad5360_ids);
557
558static struct spi_driver ad5360_driver = {
559 .driver = {
560 .name = "ad5360",
561 .owner = THIS_MODULE,
562 },
563 .probe = ad5360_probe,
564 .remove = __devexit_p(ad5360_remove),
565 .id_table = ad5360_ids,
566};
567module_spi_driver(ad5360_driver);
568
569MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
570MODULE_DESCRIPTION("Analog Devices AD5360/61/62/63/70/71/72/73 DAC");
571MODULE_LICENSE("GPL v2");
572