Merge branch 'ino-alloc' of git://repo.or.cz/linux-btrfs-devel into inode_numbers
[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 void ad5620_store_pwr_down(struct ad5446_state *st, unsigned mode)
52 {
53 st->data.d16 = cpu_to_be16(mode << 14);
54 }
55
56 static void ad5660_store_pwr_down(struct ad5446_state *st, unsigned mode)
57 {
58 unsigned val = mode << 16;
59
60 st->data.d24[0] = (val >> 16) & 0xFF;
61 st->data.d24[1] = (val >> 8) & 0xFF;
62 st->data.d24[2] = val & 0xFF;
63 }
64
65 static ssize_t ad5446_write(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf,
68 size_t len)
69 {
70 struct iio_dev *dev_info = dev_get_drvdata(dev);
71 struct ad5446_state *st = dev_info->dev_data;
72 int ret;
73 long val;
74
75 ret = strict_strtol(buf, 10, &val);
76 if (ret)
77 goto error_ret;
78
79 if (val > RES_MASK(st->chip_info->bits)) {
80 ret = -EINVAL;
81 goto error_ret;
82 }
83
84 mutex_lock(&dev_info->mlock);
85 st->cached_val = val;
86 st->chip_info->store_sample(st, val);
87 ret = spi_sync(st->spi, &st->msg);
88 mutex_unlock(&dev_info->mlock);
89
90 error_ret:
91 return ret ? ret : len;
92 }
93
94 static IIO_DEV_ATTR_OUT_RAW(0, ad5446_write, 0);
95
96 static ssize_t ad5446_show_scale(struct device *dev,
97 struct device_attribute *attr,
98 char *buf)
99 {
100 struct iio_dev *dev_info = dev_get_drvdata(dev);
101 struct ad5446_state *st = iio_dev_get_devdata(dev_info);
102 /* Corresponds to Vref / 2^(bits) */
103 unsigned int scale_uv = (st->vref_mv * 1000) >> st->chip_info->bits;
104
105 return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
106 }
107 static IIO_DEVICE_ATTR(out_scale, S_IRUGO, ad5446_show_scale, NULL, 0);
108
109 static ssize_t ad5446_show_name(struct device *dev,
110 struct device_attribute *attr,
111 char *buf)
112 {
113 struct iio_dev *dev_info = dev_get_drvdata(dev);
114 struct ad5446_state *st = iio_dev_get_devdata(dev_info);
115
116 return sprintf(buf, "%s\n", spi_get_device_id(st->spi)->name);
117 }
118 static IIO_DEVICE_ATTR(name, S_IRUGO, ad5446_show_name, NULL, 0);
119
120 static ssize_t ad5446_write_powerdown_mode(struct device *dev,
121 struct device_attribute *attr,
122 const char *buf, size_t len)
123 {
124 struct iio_dev *dev_info = dev_get_drvdata(dev);
125 struct ad5446_state *st = dev_info->dev_data;
126
127 if (sysfs_streq(buf, "1kohm_to_gnd"))
128 st->pwr_down_mode = MODE_PWRDWN_1k;
129 else if (sysfs_streq(buf, "100kohm_to_gnd"))
130 st->pwr_down_mode = MODE_PWRDWN_100k;
131 else if (sysfs_streq(buf, "three_state"))
132 st->pwr_down_mode = MODE_PWRDWN_TRISTATE;
133 else
134 return -EINVAL;
135
136 return len;
137 }
138
139 static ssize_t ad5446_read_powerdown_mode(struct device *dev,
140 struct device_attribute *attr, char *buf)
141 {
142 struct iio_dev *dev_info = dev_get_drvdata(dev);
143 struct ad5446_state *st = dev_info->dev_data;
144
145 char mode[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
146
147 return sprintf(buf, "%s\n", mode[st->pwr_down_mode]);
148 }
149
150 static ssize_t ad5446_read_dac_powerdown(struct device *dev,
151 struct device_attribute *attr,
152 char *buf)
153 {
154 struct iio_dev *dev_info = dev_get_drvdata(dev);
155 struct ad5446_state *st = dev_info->dev_data;
156
157 return sprintf(buf, "%d\n", st->pwr_down);
158 }
159
160 static ssize_t ad5446_write_dac_powerdown(struct device *dev,
161 struct device_attribute *attr,
162 const char *buf, size_t len)
163 {
164 struct iio_dev *dev_info = dev_get_drvdata(dev);
165 struct ad5446_state *st = dev_info->dev_data;
166 unsigned long readin;
167 int ret;
168
169 ret = strict_strtol(buf, 10, &readin);
170 if (ret)
171 return ret;
172
173 if (readin > 1)
174 ret = -EINVAL;
175
176 mutex_lock(&dev_info->mlock);
177 st->pwr_down = readin;
178
179 if (st->pwr_down)
180 st->chip_info->store_pwr_down(st, st->pwr_down_mode);
181 else
182 st->chip_info->store_sample(st, st->cached_val);
183
184 ret = spi_sync(st->spi, &st->msg);
185 mutex_unlock(&dev_info->mlock);
186
187 return ret ? ret : len;
188 }
189
190 static IIO_DEVICE_ATTR(out_powerdown_mode, S_IRUGO | S_IWUSR,
191 ad5446_read_powerdown_mode,
192 ad5446_write_powerdown_mode, 0);
193
194 static IIO_CONST_ATTR(out_powerdown_mode_available,
195 "1kohm_to_gnd 100kohm_to_gnd three_state");
196
197 static IIO_DEVICE_ATTR(out0_powerdown, S_IRUGO | S_IWUSR,
198 ad5446_read_dac_powerdown,
199 ad5446_write_dac_powerdown, 0);
200
201 static struct attribute *ad5446_attributes[] = {
202 &iio_dev_attr_out0_raw.dev_attr.attr,
203 &iio_dev_attr_out_scale.dev_attr.attr,
204 &iio_dev_attr_out0_powerdown.dev_attr.attr,
205 &iio_dev_attr_out_powerdown_mode.dev_attr.attr,
206 &iio_const_attr_out_powerdown_mode_available.dev_attr.attr,
207 &iio_dev_attr_name.dev_attr.attr,
208 NULL,
209 };
210
211 static mode_t ad5446_attr_is_visible(struct kobject *kobj,
212 struct attribute *attr, int n)
213 {
214 struct device *dev = container_of(kobj, struct device, kobj);
215 struct iio_dev *dev_info = dev_get_drvdata(dev);
216 struct ad5446_state *st = iio_dev_get_devdata(dev_info);
217
218 mode_t mode = attr->mode;
219
220 if (!st->chip_info->store_pwr_down &&
221 (attr == &iio_dev_attr_out0_powerdown.dev_attr.attr ||
222 attr == &iio_dev_attr_out_powerdown_mode.dev_attr.attr ||
223 attr ==
224 &iio_const_attr_out_powerdown_mode_available.dev_attr.attr))
225 mode = 0;
226
227 return mode;
228 }
229
230 static const struct attribute_group ad5446_attribute_group = {
231 .attrs = ad5446_attributes,
232 .is_visible = ad5446_attr_is_visible,
233 };
234
235 static const struct ad5446_chip_info ad5446_chip_info_tbl[] = {
236 [ID_AD5444] = {
237 .bits = 12,
238 .storagebits = 16,
239 .left_shift = 2,
240 .store_sample = ad5446_store_sample,
241 },
242 [ID_AD5446] = {
243 .bits = 14,
244 .storagebits = 16,
245 .left_shift = 0,
246 .store_sample = ad5446_store_sample,
247 },
248 [ID_AD5542A] = {
249 .bits = 16,
250 .storagebits = 16,
251 .left_shift = 0,
252 .store_sample = ad5542_store_sample,
253 },
254 [ID_AD5543] = {
255 .bits = 16,
256 .storagebits = 16,
257 .left_shift = 0,
258 .store_sample = ad5542_store_sample,
259 },
260 [ID_AD5512A] = {
261 .bits = 12,
262 .storagebits = 16,
263 .left_shift = 4,
264 .store_sample = ad5542_store_sample,
265 },
266 [ID_AD5553] = {
267 .bits = 14,
268 .storagebits = 16,
269 .left_shift = 0,
270 .store_sample = ad5542_store_sample,
271 },
272 [ID_AD5601] = {
273 .bits = 8,
274 .storagebits = 16,
275 .left_shift = 6,
276 .store_sample = ad5542_store_sample,
277 .store_pwr_down = ad5620_store_pwr_down,
278 },
279 [ID_AD5611] = {
280 .bits = 10,
281 .storagebits = 16,
282 .left_shift = 4,
283 .store_sample = ad5542_store_sample,
284 .store_pwr_down = ad5620_store_pwr_down,
285 },
286 [ID_AD5621] = {
287 .bits = 12,
288 .storagebits = 16,
289 .left_shift = 2,
290 .store_sample = ad5542_store_sample,
291 .store_pwr_down = ad5620_store_pwr_down,
292 },
293 [ID_AD5620_2500] = {
294 .bits = 12,
295 .storagebits = 16,
296 .left_shift = 2,
297 .int_vref_mv = 2500,
298 .store_sample = ad5620_store_sample,
299 .store_pwr_down = ad5620_store_pwr_down,
300 },
301 [ID_AD5620_1250] = {
302 .bits = 12,
303 .storagebits = 16,
304 .left_shift = 2,
305 .int_vref_mv = 1250,
306 .store_sample = ad5620_store_sample,
307 .store_pwr_down = ad5620_store_pwr_down,
308 },
309 [ID_AD5640_2500] = {
310 .bits = 14,
311 .storagebits = 16,
312 .left_shift = 0,
313 .int_vref_mv = 2500,
314 .store_sample = ad5620_store_sample,
315 .store_pwr_down = ad5620_store_pwr_down,
316 },
317 [ID_AD5640_1250] = {
318 .bits = 14,
319 .storagebits = 16,
320 .left_shift = 0,
321 .int_vref_mv = 1250,
322 .store_sample = ad5620_store_sample,
323 .store_pwr_down = ad5620_store_pwr_down,
324 },
325 [ID_AD5660_2500] = {
326 .bits = 16,
327 .storagebits = 24,
328 .left_shift = 0,
329 .int_vref_mv = 2500,
330 .store_sample = ad5660_store_sample,
331 .store_pwr_down = ad5660_store_pwr_down,
332 },
333 [ID_AD5660_1250] = {
334 .bits = 16,
335 .storagebits = 24,
336 .left_shift = 0,
337 .int_vref_mv = 1250,
338 .store_sample = ad5660_store_sample,
339 .store_pwr_down = ad5660_store_pwr_down,
340 },
341 };
342
343 static int __devinit ad5446_probe(struct spi_device *spi)
344 {
345 struct ad5446_state *st;
346 int ret, voltage_uv = 0;
347
348 st = kzalloc(sizeof(*st), GFP_KERNEL);
349 if (st == NULL) {
350 ret = -ENOMEM;
351 goto error_ret;
352 }
353
354 st->reg = regulator_get(&spi->dev, "vcc");
355 if (!IS_ERR(st->reg)) {
356 ret = regulator_enable(st->reg);
357 if (ret)
358 goto error_put_reg;
359
360 voltage_uv = regulator_get_voltage(st->reg);
361 }
362
363 st->chip_info =
364 &ad5446_chip_info_tbl[spi_get_device_id(spi)->driver_data];
365
366 spi_set_drvdata(spi, st);
367
368 st->spi = spi;
369
370 st->indio_dev = iio_allocate_device();
371 if (st->indio_dev == NULL) {
372 ret = -ENOMEM;
373 goto error_disable_reg;
374 }
375
376 /* Estabilish that the iio_dev is a child of the spi device */
377 st->indio_dev->dev.parent = &spi->dev;
378 st->indio_dev->attrs = &ad5446_attribute_group;
379 st->indio_dev->dev_data = (void *)(st);
380 st->indio_dev->driver_module = THIS_MODULE;
381 st->indio_dev->modes = INDIO_DIRECT_MODE;
382
383 /* Setup default message */
384
385 st->xfer.tx_buf = &st->data;
386 st->xfer.len = st->chip_info->storagebits / 8;
387
388 spi_message_init(&st->msg);
389 spi_message_add_tail(&st->xfer, &st->msg);
390
391 switch (spi_get_device_id(spi)->driver_data) {
392 case ID_AD5620_2500:
393 case ID_AD5620_1250:
394 case ID_AD5640_2500:
395 case ID_AD5640_1250:
396 case ID_AD5660_2500:
397 case ID_AD5660_1250:
398 st->vref_mv = st->chip_info->int_vref_mv;
399 break;
400 default:
401 if (voltage_uv)
402 st->vref_mv = voltage_uv / 1000;
403 else
404 dev_warn(&spi->dev,
405 "reference voltage unspecified\n");
406 }
407
408 ret = iio_device_register(st->indio_dev);
409 if (ret)
410 goto error_free_device;
411
412 return 0;
413
414 error_free_device:
415 iio_free_device(st->indio_dev);
416 error_disable_reg:
417 if (!IS_ERR(st->reg))
418 regulator_disable(st->reg);
419 error_put_reg:
420 if (!IS_ERR(st->reg))
421 regulator_put(st->reg);
422 kfree(st);
423 error_ret:
424 return ret;
425 }
426
427 static int ad5446_remove(struct spi_device *spi)
428 {
429 struct ad5446_state *st = spi_get_drvdata(spi);
430 struct iio_dev *indio_dev = st->indio_dev;
431
432 iio_device_unregister(indio_dev);
433 if (!IS_ERR(st->reg)) {
434 regulator_disable(st->reg);
435 regulator_put(st->reg);
436 }
437 kfree(st);
438 return 0;
439 }
440
441 static const struct spi_device_id ad5446_id[] = {
442 {"ad5444", ID_AD5444},
443 {"ad5446", ID_AD5446},
444 {"ad5512a", ID_AD5512A},
445 {"ad5542a", ID_AD5542A},
446 {"ad5543", ID_AD5543},
447 {"ad5553", ID_AD5553},
448 {"ad5601", ID_AD5601},
449 {"ad5611", ID_AD5611},
450 {"ad5621", ID_AD5621},
451 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
452 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
453 {"ad5640-2500", ID_AD5640_2500},
454 {"ad5640-1250", ID_AD5640_1250},
455 {"ad5660-2500", ID_AD5660_2500},
456 {"ad5660-1250", ID_AD5660_1250},
457 {}
458 };
459
460 static struct spi_driver ad5446_driver = {
461 .driver = {
462 .name = "ad5446",
463 .bus = &spi_bus_type,
464 .owner = THIS_MODULE,
465 },
466 .probe = ad5446_probe,
467 .remove = __devexit_p(ad5446_remove),
468 .id_table = ad5446_id,
469 };
470
471 static int __init ad5446_init(void)
472 {
473 return spi_register_driver(&ad5446_driver);
474 }
475 module_init(ad5446_init);
476
477 static void __exit ad5446_exit(void)
478 {
479 spi_unregister_driver(&ad5446_driver);
480 }
481 module_exit(ad5446_exit);
482
483 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
484 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
485 MODULE_LICENSE("GPL v2");
486 MODULE_ALIAS("spi:ad5446");
This page took 0.040717 seconds and 6 git commands to generate.