staging: IIO: DAC: Add support for the AD5543/AD5553
[deliverable/linux.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 void ad5446_store_sample(struct ad5446_state *st, unsigned val)
27 {
28 st->data.d16 = cpu_to_be16(AD5446_LOAD |
29 (val << st->chip_info->left_shift));
30 }
31
32 static void ad5542_store_sample(struct ad5446_state *st, unsigned val)
33 {
34 st->data.d16 = cpu_to_be16(val << st->chip_info->left_shift);
35 }
36
37 static void ad5620_store_sample(struct ad5446_state *st, unsigned val)
38 {
39 st->data.d16 = cpu_to_be16(AD5620_LOAD |
40 (val << st->chip_info->left_shift));
41 }
42
43 static void ad5660_store_sample(struct ad5446_state *st, unsigned val)
44 {
45 val |= AD5660_LOAD;
46 st->data.d24[0] = (val >> 16) & 0xFF;
47 st->data.d24[1] = (val >> 8) & 0xFF;
48 st->data.d24[2] = val & 0xFF;
49 }
50
51 static ssize_t ad5446_write(struct device *dev,
52 struct device_attribute *attr,
53 const char *buf,
54 size_t len)
55 {
56 struct iio_dev *dev_info = dev_get_drvdata(dev);
57 struct ad5446_state *st = dev_info->dev_data;
58 int ret;
59 long val;
60
61 ret = strict_strtol(buf, 10, &val);
62 if (ret)
63 goto error_ret;
64
65 if (val > RES_MASK(st->chip_info->bits)) {
66 ret = -EINVAL;
67 goto error_ret;
68 }
69
70 mutex_lock(&dev_info->mlock);
71 st->chip_info->store_sample(st, val);
72 ret = spi_sync(st->spi, &st->msg);
73 mutex_unlock(&dev_info->mlock);
74
75 error_ret:
76 return ret ? ret : len;
77 }
78
79 static IIO_DEV_ATTR_OUT_RAW(0, ad5446_write, 0);
80
81 static ssize_t ad5446_show_scale(struct device *dev,
82 struct device_attribute *attr,
83 char *buf)
84 {
85 struct iio_dev *dev_info = dev_get_drvdata(dev);
86 struct ad5446_state *st = iio_dev_get_devdata(dev_info);
87 /* Corresponds to Vref / 2^(bits) */
88 unsigned int scale_uv = (st->vref_mv * 1000) >> st->chip_info->bits;
89
90 return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
91 }
92 static IIO_DEVICE_ATTR(out_scale, S_IRUGO, ad5446_show_scale, NULL, 0);
93
94 static ssize_t ad5446_show_name(struct device *dev,
95 struct device_attribute *attr,
96 char *buf)
97 {
98 struct iio_dev *dev_info = dev_get_drvdata(dev);
99 struct ad5446_state *st = iio_dev_get_devdata(dev_info);
100
101 return sprintf(buf, "%s\n", spi_get_device_id(st->spi)->name);
102 }
103 static IIO_DEVICE_ATTR(name, S_IRUGO, ad5446_show_name, NULL, 0);
104
105 static struct attribute *ad5446_attributes[] = {
106 &iio_dev_attr_out0_raw.dev_attr.attr,
107 &iio_dev_attr_out_scale.dev_attr.attr,
108 &iio_dev_attr_name.dev_attr.attr,
109 NULL,
110 };
111
112 static const struct attribute_group ad5446_attribute_group = {
113 .attrs = ad5446_attributes,
114 };
115
116 static const struct ad5446_chip_info ad5446_chip_info_tbl[] = {
117 [ID_AD5444] = {
118 .bits = 12,
119 .storagebits = 16,
120 .left_shift = 2,
121 .store_sample = ad5446_store_sample,
122 },
123 [ID_AD5446] = {
124 .bits = 14,
125 .storagebits = 16,
126 .left_shift = 0,
127 .store_sample = ad5446_store_sample,
128 },
129 [ID_AD5542A] = {
130 .bits = 16,
131 .storagebits = 16,
132 .left_shift = 0,
133 .store_sample = ad5542_store_sample,
134 },
135 [ID_AD5543] = {
136 .bits = 16,
137 .storagebits = 16,
138 .left_shift = 0,
139 .store_sample = ad5542_store_sample,
140 },
141 [ID_AD5512A] = {
142 .bits = 12,
143 .storagebits = 16,
144 .left_shift = 4,
145 .store_sample = ad5542_store_sample,
146 },
147 [ID_AD5553] = {
148 .bits = 14,
149 .storagebits = 16,
150 .left_shift = 0,
151 .store_sample = ad5542_store_sample,
152 },
153 [ID_AD5620_2500] = {
154 .bits = 12,
155 .storagebits = 16,
156 .left_shift = 2,
157 .int_vref_mv = 2500,
158 .store_sample = ad5620_store_sample,
159 },
160 [ID_AD5620_1250] = {
161 .bits = 12,
162 .storagebits = 16,
163 .left_shift = 2,
164 .int_vref_mv = 1250,
165 .store_sample = ad5620_store_sample,
166 },
167 [ID_AD5640_2500] = {
168 .bits = 14,
169 .storagebits = 16,
170 .left_shift = 0,
171 .int_vref_mv = 2500,
172 .store_sample = ad5620_store_sample,
173 },
174 [ID_AD5640_1250] = {
175 .bits = 14,
176 .storagebits = 16,
177 .left_shift = 0,
178 .int_vref_mv = 1250,
179 .store_sample = ad5620_store_sample,
180 },
181 [ID_AD5660_2500] = {
182 .bits = 16,
183 .storagebits = 24,
184 .left_shift = 0,
185 .int_vref_mv = 2500,
186 .store_sample = ad5660_store_sample,
187 },
188 [ID_AD5660_1250] = {
189 .bits = 16,
190 .storagebits = 24,
191 .left_shift = 0,
192 .int_vref_mv = 1250,
193 .store_sample = ad5660_store_sample,
194 },
195 };
196
197 static int __devinit ad5446_probe(struct spi_device *spi)
198 {
199 struct ad5446_state *st;
200 int ret, voltage_uv = 0;
201
202 st = kzalloc(sizeof(*st), GFP_KERNEL);
203 if (st == NULL) {
204 ret = -ENOMEM;
205 goto error_ret;
206 }
207
208 st->reg = regulator_get(&spi->dev, "vcc");
209 if (!IS_ERR(st->reg)) {
210 ret = regulator_enable(st->reg);
211 if (ret)
212 goto error_put_reg;
213
214 voltage_uv = regulator_get_voltage(st->reg);
215 }
216
217 st->chip_info =
218 &ad5446_chip_info_tbl[spi_get_device_id(spi)->driver_data];
219
220 spi_set_drvdata(spi, st);
221
222 st->spi = spi;
223
224 st->indio_dev = iio_allocate_device();
225 if (st->indio_dev == NULL) {
226 ret = -ENOMEM;
227 goto error_disable_reg;
228 }
229
230 /* Estabilish that the iio_dev is a child of the spi device */
231 st->indio_dev->dev.parent = &spi->dev;
232 st->indio_dev->attrs = &ad5446_attribute_group;
233 st->indio_dev->dev_data = (void *)(st);
234 st->indio_dev->driver_module = THIS_MODULE;
235 st->indio_dev->modes = INDIO_DIRECT_MODE;
236
237 /* Setup default message */
238
239 st->xfer.tx_buf = &st->data;
240 st->xfer.len = st->chip_info->storagebits / 8;
241
242 spi_message_init(&st->msg);
243 spi_message_add_tail(&st->xfer, &st->msg);
244
245 switch (spi_get_device_id(spi)->driver_data) {
246 case ID_AD5620_2500:
247 case ID_AD5620_1250:
248 case ID_AD5640_2500:
249 case ID_AD5640_1250:
250 case ID_AD5660_2500:
251 case ID_AD5660_1250:
252 st->vref_mv = st->chip_info->int_vref_mv;
253 break;
254 default:
255 if (voltage_uv)
256 st->vref_mv = voltage_uv / 1000;
257 else
258 dev_warn(&spi->dev,
259 "reference voltage unspecified\n");
260 }
261
262 ret = iio_device_register(st->indio_dev);
263 if (ret)
264 goto error_free_device;
265
266 return 0;
267
268 error_free_device:
269 iio_free_device(st->indio_dev);
270 error_disable_reg:
271 if (!IS_ERR(st->reg))
272 regulator_disable(st->reg);
273 error_put_reg:
274 if (!IS_ERR(st->reg))
275 regulator_put(st->reg);
276 kfree(st);
277 error_ret:
278 return ret;
279 }
280
281 static int ad5446_remove(struct spi_device *spi)
282 {
283 struct ad5446_state *st = spi_get_drvdata(spi);
284 struct iio_dev *indio_dev = st->indio_dev;
285
286 iio_device_unregister(indio_dev);
287 if (!IS_ERR(st->reg)) {
288 regulator_disable(st->reg);
289 regulator_put(st->reg);
290 }
291 kfree(st);
292 return 0;
293 }
294
295 static const struct spi_device_id ad5446_id[] = {
296 {"ad5444", ID_AD5444},
297 {"ad5446", ID_AD5446},
298 {"ad5542a", ID_AD5542A},
299 {"ad5512a", ID_AD5512A},
300 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
301 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
302 {"ad5640-2500", ID_AD5640_2500},
303 {"ad5640-1250", ID_AD5640_1250},
304 {"ad5660-2500", ID_AD5660_2500},
305 {"ad5660-1250", ID_AD5660_1250},
306 {}
307 };
308
309 static struct spi_driver ad5446_driver = {
310 .driver = {
311 .name = "ad5446",
312 .bus = &spi_bus_type,
313 .owner = THIS_MODULE,
314 },
315 .probe = ad5446_probe,
316 .remove = __devexit_p(ad5446_remove),
317 .id_table = ad5446_id,
318 };
319
320 static int __init ad5446_init(void)
321 {
322 return spi_register_driver(&ad5446_driver);
323 }
324 module_init(ad5446_init);
325
326 static void __exit ad5446_exit(void)
327 {
328 spi_unregister_driver(&ad5446_driver);
329 }
330 module_exit(ad5446_exit);
331
332 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
333 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
334 MODULE_LICENSE("GPL v2");
335 MODULE_ALIAS("spi:ad5446");
This page took 0.036643 seconds and 5 git commands to generate.