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 <asm/div64.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
);
34 static int ad9834_write_frequency(struct ad9834_state
*st
,
35 unsigned long addr
, unsigned long fout
)
39 if (fout
> (st
->mclk
/ 2))
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
))
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
,
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
);
74 ret
= strict_strtoul(buf
, 10, &val
);
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
);
84 case AD9834_REG_PHASE0
:
85 case AD9834_REG_PHASE1
:
86 ret
= ad9834_write_phase(st
, this_attr
->address
, val
);
89 if (st
->control
& AD9834_MODE
) {
90 ret
= -EINVAL
; /* AD9843 reserved mode */
95 st
->control
|= AD9834_OPBITEN
;
97 st
->control
&= ~AD9834_OPBITEN
;
99 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
100 ret
= spi_sync(st
->spi
, &st
->msg
);
104 st
->control
|= AD9834_PIN_SW
;
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
);
113 st
->control
&= ~(this_attr
->address
| AD9834_PIN_SW
);
115 st
->control
|= this_attr
->address
;
116 st
->control
&= ~AD9834_PIN_SW
;
121 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
122 ret
= spi_sync(st
->spi
, &st
->msg
);
126 st
->control
&= ~AD9834_RESET
;
128 st
->control
|= AD9834_RESET
;
130 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
131 ret
= spi_sync(st
->spi
, &st
->msg
);
136 mutex_unlock(&dev_info
->mlock
);
139 return ret
? ret
: len
;
142 static ssize_t
ad9834_store_wavetype(struct device
*dev
,
143 struct device_attribute
*attr
,
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
);
151 bool is_ad9833_7
= (st
->devid
== ID_AD9833
) || (st
->devid
== ID_AD9837
);
153 mutex_lock(&dev_info
->mlock
);
155 switch (this_attr
->address
) {
157 if (sysfs_streq(buf
, "sine")) {
158 st
->control
&= ~AD9834_MODE
;
160 st
->control
&= ~AD9834_OPBITEN
;
161 } else if (sysfs_streq(buf
, "triangle")) {
163 st
->control
&= ~AD9834_OPBITEN
;
164 st
->control
|= AD9834_MODE
;
165 } else if (st
->control
& AD9834_OPBITEN
) {
166 ret
= -EINVAL
; /* AD9843 reserved mode */
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
;
179 if (sysfs_streq(buf
, "square") &&
180 !(st
->control
& AD9834_MODE
)) {
181 st
->control
&= ~AD9834_MODE
;
182 st
->control
|= AD9834_OPBITEN
;
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
,
205 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
206 struct ad9834_state
*st
= iio_priv(dev_info
);
209 if ((st
->devid
== ID_AD9833
) || (st
->devid
== ID_AD9837
))
210 str
= "sine triangle square";
211 else if (st
->control
& AD9834_OPBITEN
)
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
,
227 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
228 struct ad9834_state
*st
= iio_priv(dev_info
);
231 if (st
->control
& AD9834_MODE
)
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
,
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
) ||
296 &iio_dev_attr_dds0_out1_wavetype_available
.dev_attr
.attr
) ||
297 (attr
== &iio_dev_attr_dds0_pincontrol_en
.dev_attr
.attr
)))
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
;
322 dev_dbg(&spi
->dev
, "no platform data?\n");
326 reg
= regulator_get(&spi
->dev
, "vcc");
328 ret
= regulator_enable(reg
);
333 indio_dev
= iio_allocate_device(sizeof(*st
));
334 if (indio_dev
== NULL
) {
336 goto error_disable_reg
;
338 spi_set_drvdata(spi
, indio_dev
);
339 st
= iio_priv(indio_dev
);
340 st
->mclk
= pdata
->mclk
;
342 st
->devid
= spi_get_device_id(spi
)->driver_data
;
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
;
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
;
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
);
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
);
384 goto error_free_device
;
386 ret
= ad9834_write_frequency(st
, AD9834_REG_FREQ1
, pdata
->freq1
);
388 goto error_free_device
;
390 ret
= ad9834_write_phase(st
, AD9834_REG_PHASE0
, pdata
->phase0
);
392 goto error_free_device
;
394 ret
= ad9834_write_phase(st
, AD9834_REG_PHASE1
, pdata
->phase1
);
396 goto error_free_device
;
398 ret
= iio_device_register(indio_dev
);
400 goto error_free_device
;
405 iio_free_device(indio_dev
);
408 regulator_disable(reg
);
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
);
423 regulator_disable(reg
);
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
= {
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");