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/regulator/consumer.h>
18 #include <linux/err.h>
26 static ssize_t ad5446_write(struct device *dev,
27 struct device_attribute *attr,
31 struct iio_dev *dev_info = dev_get_drvdata(dev);
32 struct ad5446_state *st = dev_info->dev_data;
36 ret = strict_strtol(buf, 10, &val);
40 if (val > RES_MASK(st->chip_info->bits)) {
45 mutex_lock(&dev_info->mlock);
46 switch (spi_get_device_id(st->spi)->driver_data) {
49 st->data = cpu_to_be16(AD5446_LOAD |
50 (val << st->chip_info->left_shift));
54 st->data = cpu_to_be16(val << st->chip_info->left_shift);
58 ret = spi_sync(st->spi, &st->msg);
59 mutex_unlock(&dev_info->mlock);
62 return ret ? ret : len;
65 static IIO_DEV_ATTR_OUT_RAW(0, ad5446_write, 0);
67 static ssize_t ad5446_show_scale(struct device *dev,
68 struct device_attribute *attr,
71 struct iio_dev *dev_info = dev_get_drvdata(dev);
72 struct ad5446_state *st = iio_dev_get_devdata(dev_info);
73 /* Corresponds to Vref / 2^(bits) */
74 unsigned int scale_uv = (st->vref_mv * 1000) >> st->chip_info->bits;
76 return sprintf(buf, "%d.%d\n", scale_uv / 1000, scale_uv % 1000);
78 static IIO_DEVICE_ATTR(out_scale, S_IRUGO, ad5446_show_scale, NULL, 0);
80 static ssize_t ad5446_show_name(struct device *dev,
81 struct device_attribute *attr,
84 struct iio_dev *dev_info = dev_get_drvdata(dev);
85 struct ad5446_state *st = iio_dev_get_devdata(dev_info);
87 return sprintf(buf, "%s\n", spi_get_device_id(st->spi)->name);
89 static IIO_DEVICE_ATTR(name, S_IRUGO, ad5446_show_name, NULL, 0);
91 static struct attribute *ad5446_attributes[] = {
92 &iio_dev_attr_out0_raw.dev_attr.attr,
93 &iio_dev_attr_out_scale.dev_attr.attr,
94 &iio_dev_attr_name.dev_attr.attr,
98 static const struct attribute_group ad5446_attribute_group = {
99 .attrs = ad5446_attributes,
102 static const struct ad5446_chip_info ad5446_chip_info_tbl[] = {
107 .sign = 'u', /* IIO_SCAN_EL_TYPE_UNSIGNED */
113 .sign = 'u', /* IIO_SCAN_EL_TYPE_UNSIGNED */
119 .sign = 'u', /* IIO_SCAN_EL_TYPE_UNSIGNED */
125 .sign = 'u', /* IIO_SCAN_EL_TYPE_UNSIGNED */
129 static int __devinit ad5446_probe(struct spi_device *spi)
131 struct ad5446_state *st;
132 int ret, voltage_uv = 0;
134 st = kzalloc(sizeof(*st), GFP_KERNEL);
140 st->reg = regulator_get(&spi->dev, "vcc");
141 if (!IS_ERR(st->reg)) {
142 ret = regulator_enable(st->reg);
146 voltage_uv = regulator_get_voltage(st->reg);
150 &ad5446_chip_info_tbl[spi_get_device_id(spi)->driver_data];
152 spi_set_drvdata(spi, st);
156 st->indio_dev = iio_allocate_device();
157 if (st->indio_dev == NULL) {
159 goto error_disable_reg;
162 /* Estabilish that the iio_dev is a child of the spi device */
163 st->indio_dev->dev.parent = &spi->dev;
164 st->indio_dev->attrs = &ad5446_attribute_group;
165 st->indio_dev->dev_data = (void *)(st);
166 st->indio_dev->driver_module = THIS_MODULE;
167 st->indio_dev->modes = INDIO_DIRECT_MODE;
169 /* Setup default message */
171 st->xfer.tx_buf = &st->data,
174 spi_message_init(&st->msg);
175 spi_message_add_tail(&st->xfer, &st->msg);
178 st->vref_mv = voltage_uv / 1000;
180 dev_warn(&spi->dev, "reference voltage unspecified\n");
182 ret = iio_device_register(st->indio_dev);
184 goto error_free_device;
189 iio_free_device(st->indio_dev);
191 if (!IS_ERR(st->reg))
192 regulator_disable(st->reg);
194 if (!IS_ERR(st->reg))
195 regulator_put(st->reg);
201 static int ad5446_remove(struct spi_device *spi)
203 struct ad5446_state *st = spi_get_drvdata(spi);
204 struct iio_dev *indio_dev = st->indio_dev;
206 iio_device_unregister(indio_dev);
207 if (!IS_ERR(st->reg)) {
208 regulator_disable(st->reg);
209 regulator_put(st->reg);
215 static const struct spi_device_id ad5446_id[] = {
216 {"ad5444", ID_AD5444},
217 {"ad5446", ID_AD5446},
218 {"ad5542a", ID_AD5542A},
219 {"ad5512a", ID_AD5512A},
223 static struct spi_driver ad5446_driver = {
226 .bus = &spi_bus_type,
227 .owner = THIS_MODULE,
229 .probe = ad5446_probe,
230 .remove = __devexit_p(ad5446_remove),
231 .id_table = ad5446_id,
234 static int __init ad5446_init(void)
236 return spi_register_driver(&ad5446_driver);
238 module_init(ad5446_init);
240 static void __exit ad5446_exit(void)
242 spi_unregister_driver(&ad5446_driver);
244 module_exit(ad5446_exit);
246 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
247 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
248 MODULE_LICENSE("GPL v2");
249 MODULE_ALIAS("spi:ad5446");