staging:iio: ring core cleanups + check if read_last available in lis3l02dq
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / dac / ad5446.c
blob22646a617806c1db00f0184b9261160a551b5fd1
1 /*
2 * AD5446 SPI DAC driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "dac.h"
24 #include "ad5446.h"
26 static void ad5446_store_sample(struct ad5446_state *st, unsigned val)
28 st->data.d16 = cpu_to_be16(AD5446_LOAD |
29 (val << st->chip_info->left_shift));
32 static void ad5542_store_sample(struct ad5446_state *st, unsigned val)
34 st->data.d16 = cpu_to_be16(val << st->chip_info->left_shift);
37 static void ad5620_store_sample(struct ad5446_state *st, unsigned val)
39 st->data.d16 = cpu_to_be16(AD5620_LOAD |
40 (val << st->chip_info->left_shift));
43 static void ad5660_store_sample(struct ad5446_state *st, unsigned val)
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;
51 static void ad5620_store_pwr_down(struct ad5446_state *st, unsigned mode)
53 st->data.d16 = cpu_to_be16(mode << 14);
56 static void ad5660_store_pwr_down(struct ad5446_state *st, unsigned mode)
58 unsigned val = mode << 16;
60 st->data.d24[0] = (val >> 16) & 0xFF;
61 st->data.d24[1] = (val >> 8) & 0xFF;
62 st->data.d24[2] = val & 0xFF;
65 static ssize_t ad5446_write(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf,
68 size_t len)
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;
75 ret = strict_strtol(buf, 10, &val);
76 if (ret)
77 goto error_ret;
79 if (val > RES_MASK(st->chip_info->bits)) {
80 ret = -EINVAL;
81 goto error_ret;
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);
90 error_ret:
91 return ret ? ret : len;
94 static IIO_DEV_ATTR_OUT_RAW(0, ad5446_write, 0);
96 static ssize_t ad5446_show_scale(struct device *dev,
97 struct device_attribute *attr,
98 char *buf)
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;
105 return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
107 static IIO_DEVICE_ATTR(out_scale, S_IRUGO, ad5446_show_scale, NULL, 0);
109 static ssize_t ad5446_write_powerdown_mode(struct device *dev,
110 struct device_attribute *attr,
111 const char *buf, size_t len)
113 struct iio_dev *dev_info = dev_get_drvdata(dev);
114 struct ad5446_state *st = dev_info->dev_data;
116 if (sysfs_streq(buf, "1kohm_to_gnd"))
117 st->pwr_down_mode = MODE_PWRDWN_1k;
118 else if (sysfs_streq(buf, "100kohm_to_gnd"))
119 st->pwr_down_mode = MODE_PWRDWN_100k;
120 else if (sysfs_streq(buf, "three_state"))
121 st->pwr_down_mode = MODE_PWRDWN_TRISTATE;
122 else
123 return -EINVAL;
125 return len;
128 static ssize_t ad5446_read_powerdown_mode(struct device *dev,
129 struct device_attribute *attr, char *buf)
131 struct iio_dev *dev_info = dev_get_drvdata(dev);
132 struct ad5446_state *st = dev_info->dev_data;
134 char mode[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
136 return sprintf(buf, "%s\n", mode[st->pwr_down_mode]);
139 static ssize_t ad5446_read_dac_powerdown(struct device *dev,
140 struct device_attribute *attr,
141 char *buf)
143 struct iio_dev *dev_info = dev_get_drvdata(dev);
144 struct ad5446_state *st = dev_info->dev_data;
146 return sprintf(buf, "%d\n", st->pwr_down);
149 static ssize_t ad5446_write_dac_powerdown(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t len)
153 struct iio_dev *dev_info = dev_get_drvdata(dev);
154 struct ad5446_state *st = dev_info->dev_data;
155 unsigned long readin;
156 int ret;
158 ret = strict_strtol(buf, 10, &readin);
159 if (ret)
160 return ret;
162 if (readin > 1)
163 ret = -EINVAL;
165 mutex_lock(&dev_info->mlock);
166 st->pwr_down = readin;
168 if (st->pwr_down)
169 st->chip_info->store_pwr_down(st, st->pwr_down_mode);
170 else
171 st->chip_info->store_sample(st, st->cached_val);
173 ret = spi_sync(st->spi, &st->msg);
174 mutex_unlock(&dev_info->mlock);
176 return ret ? ret : len;
179 static IIO_DEVICE_ATTR(out_powerdown_mode, S_IRUGO | S_IWUSR,
180 ad5446_read_powerdown_mode,
181 ad5446_write_powerdown_mode, 0);
183 static IIO_CONST_ATTR(out_powerdown_mode_available,
184 "1kohm_to_gnd 100kohm_to_gnd three_state");
186 static IIO_DEVICE_ATTR(out0_powerdown, S_IRUGO | S_IWUSR,
187 ad5446_read_dac_powerdown,
188 ad5446_write_dac_powerdown, 0);
190 static struct attribute *ad5446_attributes[] = {
191 &iio_dev_attr_out0_raw.dev_attr.attr,
192 &iio_dev_attr_out_scale.dev_attr.attr,
193 &iio_dev_attr_out0_powerdown.dev_attr.attr,
194 &iio_dev_attr_out_powerdown_mode.dev_attr.attr,
195 &iio_const_attr_out_powerdown_mode_available.dev_attr.attr,
196 NULL,
199 static mode_t ad5446_attr_is_visible(struct kobject *kobj,
200 struct attribute *attr, int n)
202 struct device *dev = container_of(kobj, struct device, kobj);
203 struct iio_dev *dev_info = dev_get_drvdata(dev);
204 struct ad5446_state *st = iio_dev_get_devdata(dev_info);
206 mode_t mode = attr->mode;
208 if (!st->chip_info->store_pwr_down &&
209 (attr == &iio_dev_attr_out0_powerdown.dev_attr.attr ||
210 attr == &iio_dev_attr_out_powerdown_mode.dev_attr.attr ||
211 attr ==
212 &iio_const_attr_out_powerdown_mode_available.dev_attr.attr))
213 mode = 0;
215 return mode;
218 static const struct attribute_group ad5446_attribute_group = {
219 .attrs = ad5446_attributes,
220 .is_visible = ad5446_attr_is_visible,
223 static const struct ad5446_chip_info ad5446_chip_info_tbl[] = {
224 [ID_AD5444] = {
225 .bits = 12,
226 .storagebits = 16,
227 .left_shift = 2,
228 .store_sample = ad5446_store_sample,
230 [ID_AD5446] = {
231 .bits = 14,
232 .storagebits = 16,
233 .left_shift = 0,
234 .store_sample = ad5446_store_sample,
236 [ID_AD5541A] = {
237 .bits = 16,
238 .storagebits = 16,
239 .left_shift = 0,
240 .store_sample = ad5542_store_sample,
242 [ID_AD5542A] = {
243 .bits = 16,
244 .storagebits = 16,
245 .left_shift = 0,
246 .store_sample = ad5542_store_sample,
248 [ID_AD5543] = {
249 .bits = 16,
250 .storagebits = 16,
251 .left_shift = 0,
252 .store_sample = ad5542_store_sample,
254 [ID_AD5512A] = {
255 .bits = 12,
256 .storagebits = 16,
257 .left_shift = 4,
258 .store_sample = ad5542_store_sample,
260 [ID_AD5553] = {
261 .bits = 14,
262 .storagebits = 16,
263 .left_shift = 0,
264 .store_sample = ad5542_store_sample,
266 [ID_AD5601] = {
267 .bits = 8,
268 .storagebits = 16,
269 .left_shift = 6,
270 .store_sample = ad5542_store_sample,
271 .store_pwr_down = ad5620_store_pwr_down,
273 [ID_AD5611] = {
274 .bits = 10,
275 .storagebits = 16,
276 .left_shift = 4,
277 .store_sample = ad5542_store_sample,
278 .store_pwr_down = ad5620_store_pwr_down,
280 [ID_AD5621] = {
281 .bits = 12,
282 .storagebits = 16,
283 .left_shift = 2,
284 .store_sample = ad5542_store_sample,
285 .store_pwr_down = ad5620_store_pwr_down,
287 [ID_AD5620_2500] = {
288 .bits = 12,
289 .storagebits = 16,
290 .left_shift = 2,
291 .int_vref_mv = 2500,
292 .store_sample = ad5620_store_sample,
293 .store_pwr_down = ad5620_store_pwr_down,
295 [ID_AD5620_1250] = {
296 .bits = 12,
297 .storagebits = 16,
298 .left_shift = 2,
299 .int_vref_mv = 1250,
300 .store_sample = ad5620_store_sample,
301 .store_pwr_down = ad5620_store_pwr_down,
303 [ID_AD5640_2500] = {
304 .bits = 14,
305 .storagebits = 16,
306 .left_shift = 0,
307 .int_vref_mv = 2500,
308 .store_sample = ad5620_store_sample,
309 .store_pwr_down = ad5620_store_pwr_down,
311 [ID_AD5640_1250] = {
312 .bits = 14,
313 .storagebits = 16,
314 .left_shift = 0,
315 .int_vref_mv = 1250,
316 .store_sample = ad5620_store_sample,
317 .store_pwr_down = ad5620_store_pwr_down,
319 [ID_AD5660_2500] = {
320 .bits = 16,
321 .storagebits = 24,
322 .left_shift = 0,
323 .int_vref_mv = 2500,
324 .store_sample = ad5660_store_sample,
325 .store_pwr_down = ad5660_store_pwr_down,
327 [ID_AD5660_1250] = {
328 .bits = 16,
329 .storagebits = 24,
330 .left_shift = 0,
331 .int_vref_mv = 1250,
332 .store_sample = ad5660_store_sample,
333 .store_pwr_down = ad5660_store_pwr_down,
337 static int __devinit ad5446_probe(struct spi_device *spi)
339 struct ad5446_state *st;
340 int ret, voltage_uv = 0;
342 st = kzalloc(sizeof(*st), GFP_KERNEL);
343 if (st == NULL) {
344 ret = -ENOMEM;
345 goto error_ret;
348 st->reg = regulator_get(&spi->dev, "vcc");
349 if (!IS_ERR(st->reg)) {
350 ret = regulator_enable(st->reg);
351 if (ret)
352 goto error_put_reg;
354 voltage_uv = regulator_get_voltage(st->reg);
357 st->chip_info =
358 &ad5446_chip_info_tbl[spi_get_device_id(spi)->driver_data];
360 spi_set_drvdata(spi, st);
362 st->spi = spi;
364 st->indio_dev = iio_allocate_device(0);
365 if (st->indio_dev == NULL) {
366 ret = -ENOMEM;
367 goto error_disable_reg;
370 /* Estabilish that the iio_dev is a child of the spi device */
371 st->indio_dev->dev.parent = &spi->dev;
372 st->indio_dev->name = spi_get_device_id(spi)->name;
373 st->indio_dev->attrs = &ad5446_attribute_group;
374 st->indio_dev->dev_data = (void *)(st);
375 st->indio_dev->driver_module = THIS_MODULE;
376 st->indio_dev->modes = INDIO_DIRECT_MODE;
378 /* Setup default message */
380 st->xfer.tx_buf = &st->data;
381 st->xfer.len = st->chip_info->storagebits / 8;
383 spi_message_init(&st->msg);
384 spi_message_add_tail(&st->xfer, &st->msg);
386 switch (spi_get_device_id(spi)->driver_data) {
387 case ID_AD5620_2500:
388 case ID_AD5620_1250:
389 case ID_AD5640_2500:
390 case ID_AD5640_1250:
391 case ID_AD5660_2500:
392 case ID_AD5660_1250:
393 st->vref_mv = st->chip_info->int_vref_mv;
394 break;
395 default:
396 if (voltage_uv)
397 st->vref_mv = voltage_uv / 1000;
398 else
399 dev_warn(&spi->dev,
400 "reference voltage unspecified\n");
403 ret = iio_device_register(st->indio_dev);
404 if (ret)
405 goto error_free_device;
407 return 0;
409 error_free_device:
410 iio_free_device(st->indio_dev);
411 error_disable_reg:
412 if (!IS_ERR(st->reg))
413 regulator_disable(st->reg);
414 error_put_reg:
415 if (!IS_ERR(st->reg))
416 regulator_put(st->reg);
417 kfree(st);
418 error_ret:
419 return ret;
422 static int ad5446_remove(struct spi_device *spi)
424 struct ad5446_state *st = spi_get_drvdata(spi);
425 struct iio_dev *indio_dev = st->indio_dev;
427 iio_device_unregister(indio_dev);
428 if (!IS_ERR(st->reg)) {
429 regulator_disable(st->reg);
430 regulator_put(st->reg);
432 kfree(st);
433 return 0;
436 static const struct spi_device_id ad5446_id[] = {
437 {"ad5444", ID_AD5444},
438 {"ad5446", ID_AD5446},
439 {"ad5512a", ID_AD5512A},
440 {"ad5541a", ID_AD5541A},
441 {"ad5542a", ID_AD5542A},
442 {"ad5543", ID_AD5543},
443 {"ad5553", ID_AD5553},
444 {"ad5601", ID_AD5601},
445 {"ad5611", ID_AD5611},
446 {"ad5621", ID_AD5621},
447 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */
448 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */
449 {"ad5640-2500", ID_AD5640_2500},
450 {"ad5640-1250", ID_AD5640_1250},
451 {"ad5660-2500", ID_AD5660_2500},
452 {"ad5660-1250", ID_AD5660_1250},
456 static struct spi_driver ad5446_driver = {
457 .driver = {
458 .name = "ad5446",
459 .bus = &spi_bus_type,
460 .owner = THIS_MODULE,
462 .probe = ad5446_probe,
463 .remove = __devexit_p(ad5446_remove),
464 .id_table = ad5446_id,
467 static int __init ad5446_init(void)
469 return spi_register_driver(&ad5446_driver);
471 module_init(ad5446_init);
473 static void __exit ad5446_exit(void)
475 spi_unregister_driver(&ad5446_driver);
477 module_exit(ad5446_exit);
479 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
480 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
481 MODULE_LICENSE("GPL v2");
482 MODULE_ALIAS("spi:ad5446");