staging: iio: dac: Enable driver support for AD5444 and AD5446 DA converters
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / dac / ad5446.c
1 /*
2  * AD5446 SPI DAC driver
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
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>
19
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "dac.h"
23
24 #include "ad5446.h"
25
26 static ssize_t ad5446_write(struct device *dev,
27                 struct device_attribute *attr,
28                 const char *buf,
29                 size_t len)
30 {
31         struct iio_dev *dev_info = dev_get_drvdata(dev);
32         struct ad5446_state *st = dev_info->dev_data;
33         int ret;
34         long val;
35
36         ret = strict_strtol(buf, 10, &val);
37         if (ret)
38                 goto error_ret;
39
40         if (val > RES_MASK(st->chip_info->bits)) {
41                 ret = -EINVAL;
42                 goto error_ret;
43         }
44
45         mutex_lock(&dev_info->mlock);
46         switch (spi_get_device_id(st->spi)->driver_data) {
47         case ID_AD5444:
48         case ID_AD5446:
49                         st->data = cpu_to_be16(AD5446_LOAD |
50                                         (val << st->chip_info->left_shift));
51                         break;
52         case ID_AD5542A:
53         case ID_AD5512A:
54                         st->data = cpu_to_be16(val << st->chip_info->left_shift);
55                         break;
56         }
57
58         ret = spi_sync(st->spi, &st->msg);
59         mutex_unlock(&dev_info->mlock);
60
61 error_ret:
62         return ret ? ret : len;
63 }
64
65 static IIO_DEV_ATTR_OUT_RAW(0, ad5446_write, 0);
66
67 static ssize_t ad5446_show_scale(struct device *dev,
68                                 struct device_attribute *attr,
69                                 char *buf)
70 {
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;
75
76         return sprintf(buf, "%d.%d\n", scale_uv / 1000, scale_uv % 1000);
77 }
78 static IIO_DEVICE_ATTR(out_scale, S_IRUGO, ad5446_show_scale, NULL, 0);
79
80 static ssize_t ad5446_show_name(struct device *dev,
81                                  struct device_attribute *attr,
82                                  char *buf)
83 {
84         struct iio_dev *dev_info = dev_get_drvdata(dev);
85         struct ad5446_state *st = iio_dev_get_devdata(dev_info);
86
87         return sprintf(buf, "%s\n", spi_get_device_id(st->spi)->name);
88 }
89 static IIO_DEVICE_ATTR(name, S_IRUGO, ad5446_show_name, NULL, 0);
90
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,
95         NULL,
96 };
97
98 static const struct attribute_group ad5446_attribute_group = {
99         .attrs = ad5446_attributes,
100 };
101
102 static const struct ad5446_chip_info ad5446_chip_info_tbl[] = {
103         [ID_AD5444] = {
104                 .bits = 12,
105                 .storagebits = 16,
106                 .left_shift = 2,
107                 .sign = 'u', /* IIO_SCAN_EL_TYPE_UNSIGNED */
108         },
109         [ID_AD5446] = {
110                 .bits = 14,
111                 .storagebits = 16,
112                 .left_shift = 0,
113                 .sign = 'u', /* IIO_SCAN_EL_TYPE_UNSIGNED */
114         },
115         [ID_AD5542A] = {
116                 .bits = 16,
117                 .storagebits = 16,
118                 .left_shift = 0,
119                 .sign = 'u', /* IIO_SCAN_EL_TYPE_UNSIGNED */
120         },
121         [ID_AD5512A] = {
122                 .bits = 12,
123                 .storagebits = 16,
124                 .left_shift = 4,
125                 .sign = 'u', /* IIO_SCAN_EL_TYPE_UNSIGNED */
126         },
127 };
128
129 static int __devinit ad5446_probe(struct spi_device *spi)
130 {
131         struct ad5446_state *st;
132         int ret, voltage_uv = 0;
133
134         st = kzalloc(sizeof(*st), GFP_KERNEL);
135         if (st == NULL) {
136                 ret = -ENOMEM;
137                 goto error_ret;
138         }
139
140         st->reg = regulator_get(&spi->dev, "vcc");
141         if (!IS_ERR(st->reg)) {
142                 ret = regulator_enable(st->reg);
143                 if (ret)
144                         goto error_put_reg;
145
146                 voltage_uv = regulator_get_voltage(st->reg);
147         }
148
149         st->chip_info =
150                 &ad5446_chip_info_tbl[spi_get_device_id(spi)->driver_data];
151
152         spi_set_drvdata(spi, st);
153
154         st->spi = spi;
155
156         st->indio_dev = iio_allocate_device();
157         if (st->indio_dev == NULL) {
158                 ret = -ENOMEM;
159                 goto error_disable_reg;
160         }
161
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;
168
169         /* Setup default message */
170
171         st->xfer.tx_buf = &st->data,
172         st->xfer.len = 2,
173
174         spi_message_init(&st->msg);
175         spi_message_add_tail(&st->xfer, &st->msg);
176
177         if (voltage_uv)
178                 st->vref_mv = voltage_uv / 1000;
179         else
180                 dev_warn(&spi->dev, "reference voltage unspecified\n");
181
182         ret = iio_device_register(st->indio_dev);
183         if (ret)
184                 goto error_free_device;
185
186         return 0;
187
188 error_free_device:
189         iio_free_device(st->indio_dev);
190 error_disable_reg:
191         if (!IS_ERR(st->reg))
192                 regulator_disable(st->reg);
193 error_put_reg:
194         if (!IS_ERR(st->reg))
195                 regulator_put(st->reg);
196         kfree(st);
197 error_ret:
198         return ret;
199 }
200
201 static int ad5446_remove(struct spi_device *spi)
202 {
203         struct ad5446_state *st = spi_get_drvdata(spi);
204         struct iio_dev *indio_dev = st->indio_dev;
205
206         iio_device_unregister(indio_dev);
207         if (!IS_ERR(st->reg)) {
208                 regulator_disable(st->reg);
209                 regulator_put(st->reg);
210         }
211         kfree(st);
212         return 0;
213 }
214
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},
220         {}
221 };
222
223 static struct spi_driver ad5446_driver = {
224         .driver = {
225                 .name   = "ad5446",
226                 .bus    = &spi_bus_type,
227                 .owner  = THIS_MODULE,
228         },
229         .probe          = ad5446_probe,
230         .remove         = __devexit_p(ad5446_remove),
231         .id_table       = ad5446_id,
232 };
233
234 static int __init ad5446_init(void)
235 {
236         return spi_register_driver(&ad5446_driver);
237 }
238 module_init(ad5446_init);
239
240 static void __exit ad5446_exit(void)
241 {
242         spi_unregister_driver(&ad5446_driver);
243 }
244 module_exit(ad5446_exit);
245
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");