Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / dds / ad9834.c
blobe6454d58fe41cb855b1347638ec979146a986cae
1 /*
2 * AD9833/AD9834/AD9837/AD9838 SPI DDS driver
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
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>
19 #include <asm/div64.h>
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "dds.h"
25 #include "ad9834.h"
27 static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
29 unsigned long long freqreg = (u64) fout * (u64) (1 << AD9834_FREQ_BITS);
30 do_div(freqreg, mclk);
31 return freqreg;
34 static int ad9834_write_frequency(struct ad9834_state *st,
35 unsigned long addr, unsigned long fout)
37 unsigned long regval;
39 if (fout > (st->mclk / 2))
40 return -EINVAL;
42 regval = ad9834_calc_freqreg(st->mclk, fout);
44 st->freq_data[0] = cpu_to_be16(addr | (regval &
45 RES_MASK(AD9834_FREQ_BITS / 2)));
46 st->freq_data[1] = cpu_to_be16(addr | ((regval >>
47 (AD9834_FREQ_BITS / 2)) &
48 RES_MASK(AD9834_FREQ_BITS / 2)));
50 return spi_sync(st->spi, &st->freq_msg);
53 static int ad9834_write_phase(struct ad9834_state *st,
54 unsigned long addr, unsigned long phase)
56 if (phase > (1 << AD9834_PHASE_BITS))
57 return -EINVAL;
58 st->data = cpu_to_be16(addr | phase);
60 return spi_sync(st->spi, &st->msg);
63 static ssize_t ad9834_write(struct device *dev,
64 struct device_attribute *attr,
65 const char *buf,
66 size_t len)
68 struct iio_dev *dev_info = dev_get_drvdata(dev);
69 struct ad9834_state *st = iio_priv(dev_info);
70 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
71 int ret;
72 long val;
74 ret = strict_strtoul(buf, 10, &val);
75 if (ret)
76 goto error_ret;
78 mutex_lock(&dev_info->mlock);
79 switch (this_attr->address) {
80 case AD9834_REG_FREQ0:
81 case AD9834_REG_FREQ1:
82 ret = ad9834_write_frequency(st, this_attr->address, val);
83 break;
84 case AD9834_REG_PHASE0:
85 case AD9834_REG_PHASE1:
86 ret = ad9834_write_phase(st, this_attr->address, val);
87 break;
88 case AD9834_OPBITEN:
89 if (st->control & AD9834_MODE) {
90 ret = -EINVAL; /* AD9843 reserved mode */
91 break;
94 if (val)
95 st->control |= AD9834_OPBITEN;
96 else
97 st->control &= ~AD9834_OPBITEN;
99 st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
100 ret = spi_sync(st->spi, &st->msg);
101 break;
102 case AD9834_PIN_SW:
103 if (val)
104 st->control |= AD9834_PIN_SW;
105 else
106 st->control &= ~AD9834_PIN_SW;
107 st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
108 ret = spi_sync(st->spi, &st->msg);
109 break;
110 case AD9834_FSEL:
111 case AD9834_PSEL:
112 if (val == 0)
113 st->control &= ~(this_attr->address | AD9834_PIN_SW);
114 else if (val == 1) {
115 st->control |= this_attr->address;
116 st->control &= ~AD9834_PIN_SW;
117 } else {
118 ret = -EINVAL;
119 break;
121 st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
122 ret = spi_sync(st->spi, &st->msg);
123 break;
124 case AD9834_RESET:
125 if (val)
126 st->control &= ~AD9834_RESET;
127 else
128 st->control |= AD9834_RESET;
130 st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
131 ret = spi_sync(st->spi, &st->msg);
132 break;
133 default:
134 ret = -ENODEV;
136 mutex_unlock(&dev_info->mlock);
138 error_ret:
139 return ret ? ret : len;
142 static ssize_t ad9834_store_wavetype(struct device *dev,
143 struct device_attribute *attr,
144 const char *buf,
145 size_t len)
147 struct iio_dev *dev_info = dev_get_drvdata(dev);
148 struct ad9834_state *st = iio_priv(dev_info);
149 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
150 int ret = 0;
151 bool is_ad9833_7 = (st->devid == ID_AD9833) || (st->devid == ID_AD9837);
153 mutex_lock(&dev_info->mlock);
155 switch (this_attr->address) {
156 case 0:
157 if (sysfs_streq(buf, "sine")) {
158 st->control &= ~AD9834_MODE;
159 if (is_ad9833_7)
160 st->control &= ~AD9834_OPBITEN;
161 } else if (sysfs_streq(buf, "triangle")) {
162 if (is_ad9833_7) {
163 st->control &= ~AD9834_OPBITEN;
164 st->control |= AD9834_MODE;
165 } else if (st->control & AD9834_OPBITEN) {
166 ret = -EINVAL; /* AD9843 reserved mode */
167 } else {
168 st->control |= AD9834_MODE;
170 } else if (is_ad9833_7 && sysfs_streq(buf, "square")) {
171 st->control &= ~AD9834_MODE;
172 st->control |= AD9834_OPBITEN;
173 } else {
174 ret = -EINVAL;
177 break;
178 case 1:
179 if (sysfs_streq(buf, "square") &&
180 !(st->control & AD9834_MODE)) {
181 st->control &= ~AD9834_MODE;
182 st->control |= AD9834_OPBITEN;
183 } else {
184 ret = -EINVAL;
186 break;
187 default:
188 ret = -EINVAL;
189 break;
192 if (!ret) {
193 st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
194 ret = spi_sync(st->spi, &st->msg);
196 mutex_unlock(&dev_info->mlock);
198 return ret ? ret : len;
201 static ssize_t ad9834_show_out0_wavetype_available(struct device *dev,
202 struct device_attribute *attr,
203 char *buf)
205 struct iio_dev *dev_info = dev_get_drvdata(dev);
206 struct ad9834_state *st = iio_priv(dev_info);
207 char *str;
209 if ((st->devid == ID_AD9833) || (st->devid == ID_AD9837))
210 str = "sine triangle square";
211 else if (st->control & AD9834_OPBITEN)
212 str = "sine";
213 else
214 str = "sine triangle";
216 return sprintf(buf, "%s\n", str);
220 static IIO_DEVICE_ATTR(dds0_out0_wavetype_available, S_IRUGO,
221 ad9834_show_out0_wavetype_available, NULL, 0);
223 static ssize_t ad9834_show_out1_wavetype_available(struct device *dev,
224 struct device_attribute *attr,
225 char *buf)
227 struct iio_dev *dev_info = dev_get_drvdata(dev);
228 struct ad9834_state *st = iio_priv(dev_info);
229 char *str;
231 if (st->control & AD9834_MODE)
232 str = "";
233 else
234 str = "square";
236 return sprintf(buf, "%s\n", str);
239 static IIO_DEVICE_ATTR(dds0_out1_wavetype_available, S_IRUGO,
240 ad9834_show_out1_wavetype_available, NULL, 0);
243 * see dds.h for further information
246 static IIO_DEV_ATTR_FREQ(0, 0, S_IWUSR, NULL, ad9834_write, AD9834_REG_FREQ0);
247 static IIO_DEV_ATTR_FREQ(0, 1, S_IWUSR, NULL, ad9834_write, AD9834_REG_FREQ1);
248 static IIO_DEV_ATTR_FREQSYMBOL(0, S_IWUSR, NULL, ad9834_write, AD9834_FSEL);
249 static IIO_CONST_ATTR_FREQ_SCALE(0, "1"); /* 1Hz */
251 static IIO_DEV_ATTR_PHASE(0, 0, S_IWUSR, NULL, ad9834_write, AD9834_REG_PHASE0);
252 static IIO_DEV_ATTR_PHASE(0, 1, S_IWUSR, NULL, ad9834_write, AD9834_REG_PHASE1);
253 static IIO_DEV_ATTR_PHASESYMBOL(0, S_IWUSR, NULL, ad9834_write, AD9834_PSEL);
254 static IIO_CONST_ATTR_PHASE_SCALE(0, "0.0015339808"); /* 2PI/2^12 rad*/
256 static IIO_DEV_ATTR_PINCONTROL_EN(0, S_IWUSR, NULL,
257 ad9834_write, AD9834_PIN_SW);
258 static IIO_DEV_ATTR_OUT_ENABLE(0, S_IWUSR, NULL, ad9834_write, AD9834_RESET);
259 static IIO_DEV_ATTR_OUTY_ENABLE(0, 1, S_IWUSR, NULL,
260 ad9834_write, AD9834_OPBITEN);
261 static IIO_DEV_ATTR_OUT_WAVETYPE(0, 0, ad9834_store_wavetype, 0);
262 static IIO_DEV_ATTR_OUT_WAVETYPE(0, 1, ad9834_store_wavetype, 1);
264 static struct attribute *ad9834_attributes[] = {
265 &iio_dev_attr_dds0_freq0.dev_attr.attr,
266 &iio_dev_attr_dds0_freq1.dev_attr.attr,
267 &iio_const_attr_dds0_freq_scale.dev_attr.attr,
268 &iio_dev_attr_dds0_phase0.dev_attr.attr,
269 &iio_dev_attr_dds0_phase1.dev_attr.attr,
270 &iio_const_attr_dds0_phase_scale.dev_attr.attr,
271 &iio_dev_attr_dds0_pincontrol_en.dev_attr.attr,
272 &iio_dev_attr_dds0_freqsymbol.dev_attr.attr,
273 &iio_dev_attr_dds0_phasesymbol.dev_attr.attr,
274 &iio_dev_attr_dds0_out_enable.dev_attr.attr,
275 &iio_dev_attr_dds0_out1_enable.dev_attr.attr,
276 &iio_dev_attr_dds0_out0_wavetype.dev_attr.attr,
277 &iio_dev_attr_dds0_out1_wavetype.dev_attr.attr,
278 &iio_dev_attr_dds0_out0_wavetype_available.dev_attr.attr,
279 &iio_dev_attr_dds0_out1_wavetype_available.dev_attr.attr,
280 NULL,
283 static mode_t ad9834_attr_is_visible(struct kobject *kobj,
284 struct attribute *attr, int n)
286 struct device *dev = container_of(kobj, struct device, kobj);
287 struct iio_dev *dev_info = dev_get_drvdata(dev);
288 struct ad9834_state *st = iio_priv(dev_info);
290 mode_t mode = attr->mode;
292 if (((st->devid == ID_AD9833) || (st->devid == ID_AD9837)) &&
293 ((attr == &iio_dev_attr_dds0_out1_enable.dev_attr.attr) ||
294 (attr == &iio_dev_attr_dds0_out1_wavetype.dev_attr.attr) ||
295 (attr ==
296 &iio_dev_attr_dds0_out1_wavetype_available.dev_attr.attr) ||
297 (attr == &iio_dev_attr_dds0_pincontrol_en.dev_attr.attr)))
298 mode = 0;
300 return mode;
303 static const struct attribute_group ad9834_attribute_group = {
304 .attrs = ad9834_attributes,
305 .is_visible = ad9834_attr_is_visible,
308 static const struct iio_info ad9834_info = {
309 .attrs = &ad9834_attribute_group,
310 .driver_module = THIS_MODULE,
313 static int __devinit ad9834_probe(struct spi_device *spi)
315 struct ad9834_platform_data *pdata = spi->dev.platform_data;
316 struct ad9834_state *st;
317 struct iio_dev *indio_dev;
318 struct regulator *reg;
319 int ret;
321 if (!pdata) {
322 dev_dbg(&spi->dev, "no platform data?\n");
323 return -ENODEV;
326 reg = regulator_get(&spi->dev, "vcc");
327 if (!IS_ERR(reg)) {
328 ret = regulator_enable(reg);
329 if (ret)
330 goto error_put_reg;
333 indio_dev = iio_allocate_device(sizeof(*st));
334 if (indio_dev == NULL) {
335 ret = -ENOMEM;
336 goto error_disable_reg;
338 spi_set_drvdata(spi, indio_dev);
339 st = iio_priv(indio_dev);
340 st->mclk = pdata->mclk;
341 st->spi = spi;
342 st->devid = spi_get_device_id(spi)->driver_data;
343 st->reg = reg;
344 indio_dev->dev.parent = &spi->dev;
345 indio_dev->name = spi_get_device_id(spi)->name;
346 indio_dev->info = &ad9834_info;
347 indio_dev->modes = INDIO_DIRECT_MODE;
349 /* Setup default messages */
351 st->xfer.tx_buf = &st->data;
352 st->xfer.len = 2;
354 spi_message_init(&st->msg);
355 spi_message_add_tail(&st->xfer, &st->msg);
357 st->freq_xfer[0].tx_buf = &st->freq_data[0];
358 st->freq_xfer[0].len = 2;
359 st->freq_xfer[0].cs_change = 1;
360 st->freq_xfer[1].tx_buf = &st->freq_data[1];
361 st->freq_xfer[1].len = 2;
363 spi_message_init(&st->freq_msg);
364 spi_message_add_tail(&st->freq_xfer[0], &st->freq_msg);
365 spi_message_add_tail(&st->freq_xfer[1], &st->freq_msg);
367 st->control = AD9834_B28 | AD9834_RESET;
369 if (!pdata->en_div2)
370 st->control |= AD9834_DIV2;
372 if (!pdata->en_signbit_msb_out && (st->devid == ID_AD9834))
373 st->control |= AD9834_SIGN_PIB;
375 st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
376 ret = spi_sync(st->spi, &st->msg);
377 if (ret) {
378 dev_err(&spi->dev, "device init failed\n");
379 goto error_free_device;
382 ret = ad9834_write_frequency(st, AD9834_REG_FREQ0, pdata->freq0);
383 if (ret)
384 goto error_free_device;
386 ret = ad9834_write_frequency(st, AD9834_REG_FREQ1, pdata->freq1);
387 if (ret)
388 goto error_free_device;
390 ret = ad9834_write_phase(st, AD9834_REG_PHASE0, pdata->phase0);
391 if (ret)
392 goto error_free_device;
394 ret = ad9834_write_phase(st, AD9834_REG_PHASE1, pdata->phase1);
395 if (ret)
396 goto error_free_device;
398 ret = iio_device_register(indio_dev);
399 if (ret)
400 goto error_free_device;
402 return 0;
404 error_free_device:
405 iio_free_device(indio_dev);
406 error_disable_reg:
407 if (!IS_ERR(reg))
408 regulator_disable(reg);
409 error_put_reg:
410 if (!IS_ERR(reg))
411 regulator_put(reg);
412 return ret;
415 static int __devexit ad9834_remove(struct spi_device *spi)
417 struct iio_dev *indio_dev = spi_get_drvdata(spi);
418 struct ad9834_state *st = iio_priv(indio_dev);
419 struct regulator *reg = st->reg;
421 iio_device_unregister(indio_dev);
422 if (!IS_ERR(reg)) {
423 regulator_disable(reg);
424 regulator_put(reg);
427 return 0;
430 static const struct spi_device_id ad9834_id[] = {
431 {"ad9833", ID_AD9833},
432 {"ad9834", ID_AD9834},
433 {"ad9837", ID_AD9837},
434 {"ad9838", ID_AD9838},
438 static struct spi_driver ad9834_driver = {
439 .driver = {
440 .name = "ad9834",
441 .bus = &spi_bus_type,
442 .owner = THIS_MODULE,
444 .probe = ad9834_probe,
445 .remove = __devexit_p(ad9834_remove),
446 .id_table = ad9834_id,
449 static int __init ad9834_init(void)
451 return spi_register_driver(&ad9834_driver);
453 module_init(ad9834_init);
455 static void __exit ad9834_exit(void)
457 spi_unregister_driver(&ad9834_driver);
459 module_exit(ad9834_exit);
461 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
462 MODULE_DESCRIPTION("Analog Devices AD9833/AD9834/AD9837/AD9838 DDS");
463 MODULE_LICENSE("GPL v2");
464 MODULE_ALIAS("spi:ad9834");