2 * AD5446 SPI DAC driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/spi/spi.h>
17 #include <linux/i2c.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/err.h>
20 #include <linux/module.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
25 #define MODE_PWRDWN_1k 0x1
26 #define MODE_PWRDWN_100k 0x2
27 #define MODE_PWRDWN_TRISTATE 0x3
30 * struct ad5446_state - driver instance specific data
32 * @chip_info: chip model specific constants, available modes etc
33 * @reg: supply regulator
34 * @vref_mv: actual reference voltage used
39 const struct ad5446_chip_info *chip_info;
40 struct regulator *reg;
41 unsigned short vref_mv;
43 unsigned pwr_down_mode;
48 * struct ad5446_chip_info - chip specific information
49 * @channel: channel spec for the DAC
50 * @int_vref_mv: AD5620/40/60: the internal reference voltage
51 * @write: chip specific helper function to write to the register
54 struct ad5446_chip_info {
55 struct iio_chan_spec channel;
57 int (*write)(struct ad5446_state *st, unsigned val);
60 static const char * const ad5446_powerdown_modes[] = {
61 "1kohm_to_gnd", "100kohm_to_gnd", "three_state"
64 static int ad5446_set_powerdown_mode(struct iio_dev *indio_dev,
65 const struct iio_chan_spec *chan, unsigned int mode)
67 struct ad5446_state *st = iio_priv(indio_dev);
69 st->pwr_down_mode = mode + 1;
74 static int ad5446_get_powerdown_mode(struct iio_dev *indio_dev,
75 const struct iio_chan_spec *chan)
77 struct ad5446_state *st = iio_priv(indio_dev);
79 return st->pwr_down_mode - 1;
82 static const struct iio_enum ad5446_powerdown_mode_enum = {
83 .items = ad5446_powerdown_modes,
84 .num_items = ARRAY_SIZE(ad5446_powerdown_modes),
85 .get = ad5446_get_powerdown_mode,
86 .set = ad5446_set_powerdown_mode,
89 static ssize_t ad5446_read_dac_powerdown(struct iio_dev *indio_dev,
91 const struct iio_chan_spec *chan,
94 struct ad5446_state *st = iio_priv(indio_dev);
96 return sprintf(buf, "%d\n", st->pwr_down);
99 static ssize_t ad5446_write_dac_powerdown(struct iio_dev *indio_dev,
101 const struct iio_chan_spec *chan,
102 const char *buf, size_t len)
104 struct ad5446_state *st = iio_priv(indio_dev);
110 ret = strtobool(buf, &powerdown);
114 mutex_lock(&indio_dev->mlock);
115 st->pwr_down = powerdown;
118 shift = chan->scan_type.realbits + chan->scan_type.shift;
119 val = st->pwr_down_mode << shift;
121 val = st->cached_val;
124 ret = st->chip_info->write(st, val);
125 mutex_unlock(&indio_dev->mlock);
127 return ret ? ret : len;
130 static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown[] = {
133 .read = ad5446_read_dac_powerdown,
134 .write = ad5446_write_dac_powerdown,
136 IIO_ENUM("powerdown_mode", false, &ad5446_powerdown_mode_enum),
137 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum),
141 #define _AD5446_CHANNEL(bits, storage, shift, ext) { \
142 .type = IIO_VOLTAGE, \
146 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
147 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
148 .scan_type = IIO_ST('u', (bits), (storage), (shift)), \
152 #define AD5446_CHANNEL(bits, storage, shift) \
153 _AD5446_CHANNEL(bits, storage, shift, NULL)
155 #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \
156 _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown)
158 static int ad5446_read_raw(struct iio_dev *indio_dev,
159 struct iio_chan_spec const *chan,
164 struct ad5446_state *st = iio_priv(indio_dev);
165 unsigned long scale_uv;
168 case IIO_CHAN_INFO_RAW:
169 *val = st->cached_val;
171 case IIO_CHAN_INFO_SCALE:
172 scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits;
173 *val = scale_uv / 1000;
174 *val2 = (scale_uv % 1000) * 1000;
175 return IIO_VAL_INT_PLUS_MICRO;
181 static int ad5446_write_raw(struct iio_dev *indio_dev,
182 struct iio_chan_spec const *chan,
187 struct ad5446_state *st = iio_priv(indio_dev);
191 case IIO_CHAN_INFO_RAW:
192 if (val >= (1 << chan->scan_type.realbits) || val < 0)
195 val <<= chan->scan_type.shift;
196 mutex_lock(&indio_dev->mlock);
197 st->cached_val = val;
199 ret = st->chip_info->write(st, val);
200 mutex_unlock(&indio_dev->mlock);
209 static const struct iio_info ad5446_info = {
210 .read_raw = ad5446_read_raw,
211 .write_raw = ad5446_write_raw,
212 .driver_module = THIS_MODULE,
215 static int ad5446_probe(struct device *dev, const char *name,
216 const struct ad5446_chip_info *chip_info)
218 struct ad5446_state *st;
219 struct iio_dev *indio_dev;
220 struct regulator *reg;
221 int ret, voltage_uv = 0;
223 reg = regulator_get(dev, "vcc");
225 ret = regulator_enable(reg);
229 ret = regulator_get_voltage(reg);
231 goto error_disable_reg;
236 indio_dev = iio_device_alloc(sizeof(*st));
237 if (indio_dev == NULL) {
239 goto error_disable_reg;
241 st = iio_priv(indio_dev);
242 st->chip_info = chip_info;
244 dev_set_drvdata(dev, indio_dev);
248 /* Establish that the iio_dev is a child of the device */
249 indio_dev->dev.parent = dev;
250 indio_dev->name = name;
251 indio_dev->info = &ad5446_info;
252 indio_dev->modes = INDIO_DIRECT_MODE;
253 indio_dev->channels = &st->chip_info->channel;
254 indio_dev->num_channels = 1;
256 st->pwr_down_mode = MODE_PWRDWN_1k;
258 if (st->chip_info->int_vref_mv)
259 st->vref_mv = st->chip_info->int_vref_mv;
261 st->vref_mv = voltage_uv / 1000;
263 dev_warn(dev, "reference voltage unspecified\n");
265 ret = iio_device_register(indio_dev);
267 goto error_free_device;
272 iio_device_free(indio_dev);
275 regulator_disable(reg);
283 static int ad5446_remove(struct device *dev)
285 struct iio_dev *indio_dev = dev_get_drvdata(dev);
286 struct ad5446_state *st = iio_priv(indio_dev);
288 iio_device_unregister(indio_dev);
289 if (!IS_ERR(st->reg)) {
290 regulator_disable(st->reg);
291 regulator_put(st->reg);
293 iio_device_free(indio_dev);
298 #if IS_ENABLED(CONFIG_SPI_MASTER)
300 static int ad5446_write(struct ad5446_state *st, unsigned val)
302 struct spi_device *spi = to_spi_device(st->dev);
303 __be16 data = cpu_to_be16(val);
305 return spi_write(spi, &data, sizeof(data));
308 static int ad5660_write(struct ad5446_state *st, unsigned val)
310 struct spi_device *spi = to_spi_device(st->dev);
313 data[0] = (val >> 16) & 0xFF;
314 data[1] = (val >> 8) & 0xFF;
315 data[2] = val & 0xFF;
317 return spi_write(spi, data, sizeof(data));
321 * ad5446_supported_spi_device_ids:
322 * The AD5620/40/60 parts are available in different fixed internal reference
323 * voltage options. The actual part numbers may look differently
324 * (and a bit cryptic), however this style is used to make clear which
325 * parts are supported here.
327 enum ad5446_supported_spi_device_ids {
350 static const struct ad5446_chip_info ad5446_spi_chip_info[] = {
352 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
353 .write = ad5446_write,
356 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
357 .write = ad5446_write,
360 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
361 .write = ad5446_write,
364 .channel = AD5446_CHANNEL(12, 16, 2),
365 .write = ad5446_write,
368 .channel = AD5446_CHANNEL(14, 16, 0),
369 .write = ad5446_write,
372 .channel = AD5446_CHANNEL(8, 16, 6),
373 .write = ad5446_write,
376 .channel = AD5446_CHANNEL(10, 16, 4),
377 .write = ad5446_write,
380 .channel = AD5446_CHANNEL(16, 16, 0),
381 .write = ad5446_write,
384 .channel = AD5446_CHANNEL(12, 16, 4),
385 .write = ad5446_write,
388 .channel = AD5446_CHANNEL(14, 16, 0),
389 .write = ad5446_write,
392 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6),
393 .write = ad5446_write,
396 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4),
397 .write = ad5446_write,
400 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
401 .write = ad5446_write,
404 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
406 .write = ad5446_write,
409 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2),
411 .write = ad5446_write,
414 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
416 .write = ad5446_write,
419 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0),
421 .write = ad5446_write,
424 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
426 .write = ad5660_write,
429 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
431 .write = ad5660_write,
434 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0),
435 .write = ad5660_write,
439 static const struct spi_device_id ad5446_spi_ids[] = {
440 {"ad5300", ID_AD5300},
441 {"ad5310", ID_AD5310},
442 {"ad5320", ID_AD5320},
443 {"ad5444", ID_AD5444},
444 {"ad5446", ID_AD5446},
445 {"ad5450", ID_AD5450},
446 {"ad5451", ID_AD5451},
447 {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */
448 {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */
449 {"ad5512a", ID_AD5512A},
450 {"ad5541a", ID_AD5541A},
451 {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */
452 {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */
453 {"ad5553", ID_AD5553},
454 {"ad5601", ID_AD5601},
455 {"ad5611", ID_AD5611},
456 {"ad5621", ID_AD5621},
457 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
458 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
459 {"ad5640-2500", ID_AD5640_2500},
460 {"ad5640-1250", ID_AD5640_1250},
461 {"ad5660-2500", ID_AD5660_2500},
462 {"ad5660-1250", ID_AD5660_1250},
463 {"ad5662", ID_AD5662},
466 MODULE_DEVICE_TABLE(spi, ad5446_spi_ids);
468 static int ad5446_spi_probe(struct spi_device *spi)
470 const struct spi_device_id *id = spi_get_device_id(spi);
472 return ad5446_probe(&spi->dev, id->name,
473 &ad5446_spi_chip_info[id->driver_data]);
476 static int ad5446_spi_remove(struct spi_device *spi)
478 return ad5446_remove(&spi->dev);
481 static struct spi_driver ad5446_spi_driver = {
484 .owner = THIS_MODULE,
486 .probe = ad5446_spi_probe,
487 .remove = ad5446_spi_remove,
488 .id_table = ad5446_spi_ids,
491 static int __init ad5446_spi_register_driver(void)
493 return spi_register_driver(&ad5446_spi_driver);
496 static void ad5446_spi_unregister_driver(void)
498 spi_unregister_driver(&ad5446_spi_driver);
503 static inline int ad5446_spi_register_driver(void) { return 0; }
504 static inline void ad5446_spi_unregister_driver(void) { }
508 #if IS_ENABLED(CONFIG_I2C)
510 static int ad5622_write(struct ad5446_state *st, unsigned val)
512 struct i2c_client *client = to_i2c_client(st->dev);
513 __be16 data = cpu_to_be16(val);
515 return i2c_master_send(client, (char *)&data, sizeof(data));
519 * ad5446_supported_i2c_device_ids:
520 * The AD5620/40/60 parts are available in different fixed internal reference
521 * voltage options. The actual part numbers may look differently
522 * (and a bit cryptic), however this style is used to make clear which
523 * parts are supported here.
525 enum ad5446_supported_i2c_device_ids {
531 static const struct ad5446_chip_info ad5446_i2c_chip_info[] = {
533 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4),
534 .write = ad5622_write,
537 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2),
538 .write = ad5622_write,
541 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0),
542 .write = ad5622_write,
546 static int ad5446_i2c_probe(struct i2c_client *i2c,
547 const struct i2c_device_id *id)
549 return ad5446_probe(&i2c->dev, id->name,
550 &ad5446_i2c_chip_info[id->driver_data]);
553 static int ad5446_i2c_remove(struct i2c_client *i2c)
555 return ad5446_remove(&i2c->dev);
558 static const struct i2c_device_id ad5446_i2c_ids[] = {
559 {"ad5301", ID_AD5602},
560 {"ad5311", ID_AD5612},
561 {"ad5321", ID_AD5622},
562 {"ad5602", ID_AD5602},
563 {"ad5612", ID_AD5612},
564 {"ad5622", ID_AD5622},
567 MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids);
569 static struct i2c_driver ad5446_i2c_driver = {
572 .owner = THIS_MODULE,
574 .probe = ad5446_i2c_probe,
575 .remove = ad5446_i2c_remove,
576 .id_table = ad5446_i2c_ids,
579 static int __init ad5446_i2c_register_driver(void)
581 return i2c_add_driver(&ad5446_i2c_driver);
584 static void __exit ad5446_i2c_unregister_driver(void)
586 i2c_del_driver(&ad5446_i2c_driver);
591 static inline int ad5446_i2c_register_driver(void) { return 0; }
592 static inline void ad5446_i2c_unregister_driver(void) { }
596 static int __init ad5446_init(void)
600 ret = ad5446_spi_register_driver();
604 ret = ad5446_i2c_register_driver();
606 ad5446_spi_unregister_driver();
612 module_init(ad5446_init);
614 static void __exit ad5446_exit(void)
616 ad5446_i2c_unregister_driver();
617 ad5446_spi_unregister_driver();
619 module_exit(ad5446_exit);
621 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
622 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
623 MODULE_LICENSE("GPL v2");