2 * AD9833/AD9834/AD9837/AD9838 SPI DDS driver
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
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 <linux/module.h>
20 #include <asm/div64.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
28 static unsigned int ad9834_calc_freqreg(unsigned long mclk
, unsigned long fout
)
30 unsigned long long freqreg
= (u64
) fout
* (u64
) (1 << AD9834_FREQ_BITS
);
31 do_div(freqreg
, mclk
);
35 static int ad9834_write_frequency(struct ad9834_state
*st
,
36 unsigned long addr
, unsigned long fout
)
40 if (fout
> (st
->mclk
/ 2))
43 regval
= ad9834_calc_freqreg(st
->mclk
, fout
);
45 st
->freq_data
[0] = cpu_to_be16(addr
| (regval
&
46 RES_MASK(AD9834_FREQ_BITS
/ 2)));
47 st
->freq_data
[1] = cpu_to_be16(addr
| ((regval
>>
48 (AD9834_FREQ_BITS
/ 2)) &
49 RES_MASK(AD9834_FREQ_BITS
/ 2)));
51 return spi_sync(st
->spi
, &st
->freq_msg
);
54 static int ad9834_write_phase(struct ad9834_state
*st
,
55 unsigned long addr
, unsigned long phase
)
57 if (phase
> (1 << AD9834_PHASE_BITS
))
59 st
->data
= cpu_to_be16(addr
| phase
);
61 return spi_sync(st
->spi
, &st
->msg
);
64 static ssize_t
ad9834_write(struct device
*dev
,
65 struct device_attribute
*attr
,
69 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
70 struct ad9834_state
*st
= iio_priv(indio_dev
);
71 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
75 ret
= strict_strtoul(buf
, 10, &val
);
79 mutex_lock(&indio_dev
->mlock
);
80 switch ((u32
) this_attr
->address
) {
81 case AD9834_REG_FREQ0
:
82 case AD9834_REG_FREQ1
:
83 ret
= ad9834_write_frequency(st
, this_attr
->address
, val
);
85 case AD9834_REG_PHASE0
:
86 case AD9834_REG_PHASE1
:
87 ret
= ad9834_write_phase(st
, this_attr
->address
, val
);
90 if (st
->control
& AD9834_MODE
) {
91 ret
= -EINVAL
; /* AD9843 reserved mode */
96 st
->control
|= AD9834_OPBITEN
;
98 st
->control
&= ~AD9834_OPBITEN
;
100 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
101 ret
= spi_sync(st
->spi
, &st
->msg
);
105 st
->control
|= AD9834_PIN_SW
;
107 st
->control
&= ~AD9834_PIN_SW
;
108 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
109 ret
= spi_sync(st
->spi
, &st
->msg
);
114 st
->control
&= ~(this_attr
->address
| AD9834_PIN_SW
);
116 st
->control
|= this_attr
->address
;
117 st
->control
&= ~AD9834_PIN_SW
;
122 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
123 ret
= spi_sync(st
->spi
, &st
->msg
);
127 st
->control
&= ~AD9834_RESET
;
129 st
->control
|= AD9834_RESET
;
131 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
132 ret
= spi_sync(st
->spi
, &st
->msg
);
137 mutex_unlock(&indio_dev
->mlock
);
140 return ret
? ret
: len
;
143 static ssize_t
ad9834_store_wavetype(struct device
*dev
,
144 struct device_attribute
*attr
,
148 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
149 struct ad9834_state
*st
= iio_priv(indio_dev
);
150 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
152 bool is_ad9833_7
= (st
->devid
== ID_AD9833
) || (st
->devid
== ID_AD9837
);
154 mutex_lock(&indio_dev
->mlock
);
156 switch ((u32
) this_attr
->address
) {
158 if (sysfs_streq(buf
, "sine")) {
159 st
->control
&= ~AD9834_MODE
;
161 st
->control
&= ~AD9834_OPBITEN
;
162 } else if (sysfs_streq(buf
, "triangle")) {
164 st
->control
&= ~AD9834_OPBITEN
;
165 st
->control
|= AD9834_MODE
;
166 } else if (st
->control
& AD9834_OPBITEN
) {
167 ret
= -EINVAL
; /* AD9843 reserved mode */
169 st
->control
|= AD9834_MODE
;
171 } else if (is_ad9833_7
&& sysfs_streq(buf
, "square")) {
172 st
->control
&= ~AD9834_MODE
;
173 st
->control
|= AD9834_OPBITEN
;
180 if (sysfs_streq(buf
, "square") &&
181 !(st
->control
& AD9834_MODE
)) {
182 st
->control
&= ~AD9834_MODE
;
183 st
->control
|= AD9834_OPBITEN
;
194 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
195 ret
= spi_sync(st
->spi
, &st
->msg
);
197 mutex_unlock(&indio_dev
->mlock
);
199 return ret
? ret
: len
;
202 static ssize_t
ad9834_show_out0_wavetype_available(struct device
*dev
,
203 struct device_attribute
*attr
,
206 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
207 struct ad9834_state
*st
= iio_priv(indio_dev
);
210 if ((st
->devid
== ID_AD9833
) || (st
->devid
== ID_AD9837
))
211 str
= "sine triangle square";
212 else if (st
->control
& AD9834_OPBITEN
)
215 str
= "sine triangle";
217 return sprintf(buf
, "%s\n", str
);
221 static IIO_DEVICE_ATTR(out_altvoltage0_out0_wavetype_available
, S_IRUGO
,
222 ad9834_show_out0_wavetype_available
, NULL
, 0);
224 static ssize_t
ad9834_show_out1_wavetype_available(struct device
*dev
,
225 struct device_attribute
*attr
,
228 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
229 struct ad9834_state
*st
= iio_priv(indio_dev
);
232 if (st
->control
& AD9834_MODE
)
237 return sprintf(buf
, "%s\n", str
);
240 static IIO_DEVICE_ATTR(out_altvoltage0_out1_wavetype_available
, S_IRUGO
,
241 ad9834_show_out1_wavetype_available
, NULL
, 0);
244 * see dds.h for further information
247 static IIO_DEV_ATTR_FREQ(0, 0, S_IWUSR
, NULL
, ad9834_write
, AD9834_REG_FREQ0
);
248 static IIO_DEV_ATTR_FREQ(0, 1, S_IWUSR
, NULL
, ad9834_write
, AD9834_REG_FREQ1
);
249 static IIO_DEV_ATTR_FREQSYMBOL(0, S_IWUSR
, NULL
, ad9834_write
, AD9834_FSEL
);
250 static IIO_CONST_ATTR_FREQ_SCALE(0, "1"); /* 1Hz */
252 static IIO_DEV_ATTR_PHASE(0, 0, S_IWUSR
, NULL
, ad9834_write
, AD9834_REG_PHASE0
);
253 static IIO_DEV_ATTR_PHASE(0, 1, S_IWUSR
, NULL
, ad9834_write
, AD9834_REG_PHASE1
);
254 static IIO_DEV_ATTR_PHASESYMBOL(0, S_IWUSR
, NULL
, ad9834_write
, AD9834_PSEL
);
255 static IIO_CONST_ATTR_PHASE_SCALE(0, "0.0015339808"); /* 2PI/2^12 rad*/
257 static IIO_DEV_ATTR_PINCONTROL_EN(0, S_IWUSR
, NULL
,
258 ad9834_write
, AD9834_PIN_SW
);
259 static IIO_DEV_ATTR_OUT_ENABLE(0, S_IWUSR
, NULL
, ad9834_write
, AD9834_RESET
);
260 static IIO_DEV_ATTR_OUTY_ENABLE(0, 1, S_IWUSR
, NULL
,
261 ad9834_write
, AD9834_OPBITEN
);
262 static IIO_DEV_ATTR_OUT_WAVETYPE(0, 0, ad9834_store_wavetype
, 0);
263 static IIO_DEV_ATTR_OUT_WAVETYPE(0, 1, ad9834_store_wavetype
, 1);
265 static struct attribute
*ad9834_attributes
[] = {
266 &iio_dev_attr_out_altvoltage0_frequency0
.dev_attr
.attr
,
267 &iio_dev_attr_out_altvoltage0_frequency1
.dev_attr
.attr
,
268 &iio_const_attr_out_altvoltage0_frequency_scale
.dev_attr
.attr
,
269 &iio_dev_attr_out_altvoltage0_phase0
.dev_attr
.attr
,
270 &iio_dev_attr_out_altvoltage0_phase1
.dev_attr
.attr
,
271 &iio_const_attr_out_altvoltage0_phase_scale
.dev_attr
.attr
,
272 &iio_dev_attr_out_altvoltage0_pincontrol_en
.dev_attr
.attr
,
273 &iio_dev_attr_out_altvoltage0_frequencysymbol
.dev_attr
.attr
,
274 &iio_dev_attr_out_altvoltage0_phasesymbol
.dev_attr
.attr
,
275 &iio_dev_attr_out_altvoltage0_out_enable
.dev_attr
.attr
,
276 &iio_dev_attr_out_altvoltage0_out1_enable
.dev_attr
.attr
,
277 &iio_dev_attr_out_altvoltage0_out0_wavetype
.dev_attr
.attr
,
278 &iio_dev_attr_out_altvoltage0_out1_wavetype
.dev_attr
.attr
,
279 &iio_dev_attr_out_altvoltage0_out0_wavetype_available
.dev_attr
.attr
,
280 &iio_dev_attr_out_altvoltage0_out1_wavetype_available
.dev_attr
.attr
,
284 static struct attribute
*ad9833_attributes
[] = {
285 &iio_dev_attr_out_altvoltage0_frequency0
.dev_attr
.attr
,
286 &iio_dev_attr_out_altvoltage0_frequency1
.dev_attr
.attr
,
287 &iio_const_attr_out_altvoltage0_frequency_scale
.dev_attr
.attr
,
288 &iio_dev_attr_out_altvoltage0_phase0
.dev_attr
.attr
,
289 &iio_dev_attr_out_altvoltage0_phase1
.dev_attr
.attr
,
290 &iio_const_attr_out_altvoltage0_phase_scale
.dev_attr
.attr
,
291 &iio_dev_attr_out_altvoltage0_frequencysymbol
.dev_attr
.attr
,
292 &iio_dev_attr_out_altvoltage0_phasesymbol
.dev_attr
.attr
,
293 &iio_dev_attr_out_altvoltage0_out_enable
.dev_attr
.attr
,
294 &iio_dev_attr_out_altvoltage0_out0_wavetype
.dev_attr
.attr
,
295 &iio_dev_attr_out_altvoltage0_out0_wavetype_available
.dev_attr
.attr
,
299 static const struct attribute_group ad9834_attribute_group
= {
300 .attrs
= ad9834_attributes
,
303 static const struct attribute_group ad9833_attribute_group
= {
304 .attrs
= ad9833_attributes
,
307 static const struct iio_info ad9834_info
= {
308 .attrs
= &ad9834_attribute_group
,
309 .driver_module
= THIS_MODULE
,
312 static const struct iio_info ad9833_info
= {
313 .attrs
= &ad9833_attribute_group
,
314 .driver_module
= THIS_MODULE
,
317 static int __devinit
ad9834_probe(struct spi_device
*spi
)
319 struct ad9834_platform_data
*pdata
= spi
->dev
.platform_data
;
320 struct ad9834_state
*st
;
321 struct iio_dev
*indio_dev
;
322 struct regulator
*reg
;
326 dev_dbg(&spi
->dev
, "no platform data?\n");
330 reg
= regulator_get(&spi
->dev
, "vcc");
332 ret
= regulator_enable(reg
);
337 indio_dev
= iio_device_alloc(sizeof(*st
));
338 if (indio_dev
== NULL
) {
340 goto error_disable_reg
;
342 spi_set_drvdata(spi
, indio_dev
);
343 st
= iio_priv(indio_dev
);
344 st
->mclk
= pdata
->mclk
;
346 st
->devid
= spi_get_device_id(spi
)->driver_data
;
348 indio_dev
->dev
.parent
= &spi
->dev
;
349 indio_dev
->name
= spi_get_device_id(spi
)->name
;
353 indio_dev
->info
= &ad9833_info
;
356 indio_dev
->info
= &ad9834_info
;
359 indio_dev
->modes
= INDIO_DIRECT_MODE
;
361 /* Setup default messages */
363 st
->xfer
.tx_buf
= &st
->data
;
366 spi_message_init(&st
->msg
);
367 spi_message_add_tail(&st
->xfer
, &st
->msg
);
369 st
->freq_xfer
[0].tx_buf
= &st
->freq_data
[0];
370 st
->freq_xfer
[0].len
= 2;
371 st
->freq_xfer
[0].cs_change
= 1;
372 st
->freq_xfer
[1].tx_buf
= &st
->freq_data
[1];
373 st
->freq_xfer
[1].len
= 2;
375 spi_message_init(&st
->freq_msg
);
376 spi_message_add_tail(&st
->freq_xfer
[0], &st
->freq_msg
);
377 spi_message_add_tail(&st
->freq_xfer
[1], &st
->freq_msg
);
379 st
->control
= AD9834_B28
| AD9834_RESET
;
382 st
->control
|= AD9834_DIV2
;
384 if (!pdata
->en_signbit_msb_out
&& (st
->devid
== ID_AD9834
))
385 st
->control
|= AD9834_SIGN_PIB
;
387 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
388 ret
= spi_sync(st
->spi
, &st
->msg
);
390 dev_err(&spi
->dev
, "device init failed\n");
391 goto error_free_device
;
394 ret
= ad9834_write_frequency(st
, AD9834_REG_FREQ0
, pdata
->freq0
);
396 goto error_free_device
;
398 ret
= ad9834_write_frequency(st
, AD9834_REG_FREQ1
, pdata
->freq1
);
400 goto error_free_device
;
402 ret
= ad9834_write_phase(st
, AD9834_REG_PHASE0
, pdata
->phase0
);
404 goto error_free_device
;
406 ret
= ad9834_write_phase(st
, AD9834_REG_PHASE1
, pdata
->phase1
);
408 goto error_free_device
;
410 ret
= iio_device_register(indio_dev
);
412 goto error_free_device
;
417 iio_device_free(indio_dev
);
420 regulator_disable(reg
);
427 static int __devexit
ad9834_remove(struct spi_device
*spi
)
429 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
430 struct ad9834_state
*st
= iio_priv(indio_dev
);
432 iio_device_unregister(indio_dev
);
433 if (!IS_ERR(st
->reg
)) {
434 regulator_disable(st
->reg
);
435 regulator_put(st
->reg
);
437 iio_device_free(indio_dev
);
442 static const struct spi_device_id ad9834_id
[] = {
443 {"ad9833", ID_AD9833
},
444 {"ad9834", ID_AD9834
},
445 {"ad9837", ID_AD9837
},
446 {"ad9838", ID_AD9838
},
449 MODULE_DEVICE_TABLE(spi
, ad9834_id
);
451 static struct spi_driver ad9834_driver
= {
454 .owner
= THIS_MODULE
,
456 .probe
= ad9834_probe
,
457 .remove
= __devexit_p(ad9834_remove
),
458 .id_table
= ad9834_id
,
460 module_spi_driver(ad9834_driver
);
462 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
463 MODULE_DESCRIPTION("Analog Devices AD9833/AD9834/AD9837/AD9838 DDS");
464 MODULE_LICENSE("GPL v2");