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>
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_get_drvdata(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 (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_get_drvdata(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 (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_get_drvdata(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(dds0_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_get_drvdata(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(dds0_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_dds0_freq0
.dev_attr
.attr
,
267 &iio_dev_attr_dds0_freq1
.dev_attr
.attr
,
268 &iio_const_attr_dds0_freq_scale
.dev_attr
.attr
,
269 &iio_dev_attr_dds0_phase0
.dev_attr
.attr
,
270 &iio_dev_attr_dds0_phase1
.dev_attr
.attr
,
271 &iio_const_attr_dds0_phase_scale
.dev_attr
.attr
,
272 &iio_dev_attr_dds0_pincontrol_en
.dev_attr
.attr
,
273 &iio_dev_attr_dds0_freqsymbol
.dev_attr
.attr
,
274 &iio_dev_attr_dds0_phasesymbol
.dev_attr
.attr
,
275 &iio_dev_attr_dds0_out_enable
.dev_attr
.attr
,
276 &iio_dev_attr_dds0_out1_enable
.dev_attr
.attr
,
277 &iio_dev_attr_dds0_out0_wavetype
.dev_attr
.attr
,
278 &iio_dev_attr_dds0_out1_wavetype
.dev_attr
.attr
,
279 &iio_dev_attr_dds0_out0_wavetype_available
.dev_attr
.attr
,
280 &iio_dev_attr_dds0_out1_wavetype_available
.dev_attr
.attr
,
284 static mode_t
ad9834_attr_is_visible(struct kobject
*kobj
,
285 struct attribute
*attr
, int n
)
287 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
288 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
289 struct ad9834_state
*st
= iio_priv(indio_dev
);
291 mode_t mode
= attr
->mode
;
293 if (((st
->devid
== ID_AD9833
) || (st
->devid
== ID_AD9837
)) &&
294 ((attr
== &iio_dev_attr_dds0_out1_enable
.dev_attr
.attr
) ||
295 (attr
== &iio_dev_attr_dds0_out1_wavetype
.dev_attr
.attr
) ||
297 &iio_dev_attr_dds0_out1_wavetype_available
.dev_attr
.attr
) ||
298 (attr
== &iio_dev_attr_dds0_pincontrol_en
.dev_attr
.attr
)))
304 static const struct attribute_group ad9834_attribute_group
= {
305 .attrs
= ad9834_attributes
,
306 .is_visible
= ad9834_attr_is_visible
,
309 static const struct iio_info ad9834_info
= {
310 .attrs
= &ad9834_attribute_group
,
311 .driver_module
= THIS_MODULE
,
314 static int __devinit
ad9834_probe(struct spi_device
*spi
)
316 struct ad9834_platform_data
*pdata
= spi
->dev
.platform_data
;
317 struct ad9834_state
*st
;
318 struct iio_dev
*indio_dev
;
319 struct regulator
*reg
;
323 dev_dbg(&spi
->dev
, "no platform data?\n");
327 reg
= regulator_get(&spi
->dev
, "vcc");
329 ret
= regulator_enable(reg
);
334 indio_dev
= iio_allocate_device(sizeof(*st
));
335 if (indio_dev
== NULL
) {
337 goto error_disable_reg
;
339 spi_set_drvdata(spi
, indio_dev
);
340 st
= iio_priv(indio_dev
);
341 st
->mclk
= pdata
->mclk
;
343 st
->devid
= spi_get_device_id(spi
)->driver_data
;
345 indio_dev
->dev
.parent
= &spi
->dev
;
346 indio_dev
->name
= spi_get_device_id(spi
)->name
;
347 indio_dev
->info
= &ad9834_info
;
348 indio_dev
->modes
= INDIO_DIRECT_MODE
;
350 /* Setup default messages */
352 st
->xfer
.tx_buf
= &st
->data
;
355 spi_message_init(&st
->msg
);
356 spi_message_add_tail(&st
->xfer
, &st
->msg
);
358 st
->freq_xfer
[0].tx_buf
= &st
->freq_data
[0];
359 st
->freq_xfer
[0].len
= 2;
360 st
->freq_xfer
[0].cs_change
= 1;
361 st
->freq_xfer
[1].tx_buf
= &st
->freq_data
[1];
362 st
->freq_xfer
[1].len
= 2;
364 spi_message_init(&st
->freq_msg
);
365 spi_message_add_tail(&st
->freq_xfer
[0], &st
->freq_msg
);
366 spi_message_add_tail(&st
->freq_xfer
[1], &st
->freq_msg
);
368 st
->control
= AD9834_B28
| AD9834_RESET
;
371 st
->control
|= AD9834_DIV2
;
373 if (!pdata
->en_signbit_msb_out
&& (st
->devid
== ID_AD9834
))
374 st
->control
|= AD9834_SIGN_PIB
;
376 st
->data
= cpu_to_be16(AD9834_REG_CMD
| st
->control
);
377 ret
= spi_sync(st
->spi
, &st
->msg
);
379 dev_err(&spi
->dev
, "device init failed\n");
380 goto error_free_device
;
383 ret
= ad9834_write_frequency(st
, AD9834_REG_FREQ0
, pdata
->freq0
);
385 goto error_free_device
;
387 ret
= ad9834_write_frequency(st
, AD9834_REG_FREQ1
, pdata
->freq1
);
389 goto error_free_device
;
391 ret
= ad9834_write_phase(st
, AD9834_REG_PHASE0
, pdata
->phase0
);
393 goto error_free_device
;
395 ret
= ad9834_write_phase(st
, AD9834_REG_PHASE1
, pdata
->phase1
);
397 goto error_free_device
;
399 ret
= iio_device_register(indio_dev
);
401 goto error_free_device
;
406 iio_free_device(indio_dev
);
409 regulator_disable(reg
);
416 static int __devexit
ad9834_remove(struct spi_device
*spi
)
418 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
419 struct ad9834_state
*st
= iio_priv(indio_dev
);
421 iio_device_unregister(indio_dev
);
422 if (!IS_ERR(st
->reg
)) {
423 regulator_disable(st
->reg
);
424 regulator_put(st
->reg
);
426 iio_free_device(indio_dev
);
431 static const struct spi_device_id ad9834_id
[] = {
432 {"ad9833", ID_AD9833
},
433 {"ad9834", ID_AD9834
},
434 {"ad9837", ID_AD9837
},
435 {"ad9838", ID_AD9838
},
439 static struct spi_driver ad9834_driver
= {
442 .bus
= &spi_bus_type
,
443 .owner
= THIS_MODULE
,
445 .probe
= ad9834_probe
,
446 .remove
= __devexit_p(ad9834_remove
),
447 .id_table
= ad9834_id
,
450 static int __init
ad9834_init(void)
452 return spi_register_driver(&ad9834_driver
);
454 module_init(ad9834_init
);
456 static void __exit
ad9834_exit(void)
458 spi_unregister_driver(&ad9834_driver
);
460 module_exit(ad9834_exit
);
462 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
463 MODULE_DESCRIPTION("Analog Devices AD9833/AD9834/AD9837/AD9838 DDS");
464 MODULE_LICENSE("GPL v2");
465 MODULE_ALIAS("spi:ad9834");