2 * Driver for ADI Direct Digital Synthesis ad9852
4 * Copyright (c) 2010 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/types.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/module.h>
22 #define DRV_NAME "ad9852"
24 #define addr_phaad1 0x0
25 #define addr_phaad2 0x1
26 #define addr_fretu1 0x2
27 #define addr_fretu2 0x3
28 #define addr_delfre 0x4
29 #define addr_updclk 0x5
30 #define addr_ramclk 0x6
31 #define addr_contrl 0x7
32 #define addr_optskm 0x8
33 #define addr_optskr 0xa
34 #define addr_dacctl 0xb
36 #define COMPPD (1 << 4)
37 #define REFMULT2 (1 << 2)
38 #define BYPPLL (1 << 5)
39 #define PLLRANG (1 << 6)
41 #define OSKEN (1 << 5)
43 #define read_bit (1 << 7)
45 /* Register format: 1 byte addr + value */
46 struct ad9852_config
{
62 struct spi_device
*sdev
;
65 static ssize_t
ad9852_set_parameter(struct device
*dev
,
66 struct device_attribute
*attr
,
70 struct spi_message msg
;
71 struct spi_transfer xfer
;
73 struct ad9852_config
*config
= (struct ad9852_config
*)buf
;
74 struct iio_dev
*idev
= dev_get_drvdata(dev
);
75 struct ad9852_state
*st
= iio_priv(idev
);
78 xfer
.tx_buf
= &config
->phajst0
[0];
79 mutex_lock(&st
->lock
);
81 spi_message_init(&msg
);
82 spi_message_add_tail(&xfer
, &msg
);
83 ret
= spi_sync(st
->sdev
, &msg
);
88 xfer
.tx_buf
= &config
->phajst1
[0];
90 spi_message_init(&msg
);
91 spi_message_add_tail(&xfer
, &msg
);
92 ret
= spi_sync(st
->sdev
, &msg
);
97 xfer
.tx_buf
= &config
->fretun1
[0];
99 spi_message_init(&msg
);
100 spi_message_add_tail(&xfer
, &msg
);
101 ret
= spi_sync(st
->sdev
, &msg
);
106 xfer
.tx_buf
= &config
->fretun2
[0];
108 spi_message_init(&msg
);
109 spi_message_add_tail(&xfer
, &msg
);
110 ret
= spi_sync(st
->sdev
, &msg
);
115 xfer
.tx_buf
= &config
->dltafre
[0];
117 spi_message_init(&msg
);
118 spi_message_add_tail(&xfer
, &msg
);
119 ret
= spi_sync(st
->sdev
, &msg
);
124 xfer
.tx_buf
= &config
->updtclk
[0];
126 spi_message_init(&msg
);
127 spi_message_add_tail(&xfer
, &msg
);
128 ret
= spi_sync(st
->sdev
, &msg
);
133 xfer
.tx_buf
= &config
->ramprat
[0];
135 spi_message_init(&msg
);
136 spi_message_add_tail(&xfer
, &msg
);
137 ret
= spi_sync(st
->sdev
, &msg
);
142 xfer
.tx_buf
= &config
->control
[0];
144 spi_message_init(&msg
);
145 spi_message_add_tail(&xfer
, &msg
);
146 ret
= spi_sync(st
->sdev
, &msg
);
151 xfer
.tx_buf
= &config
->outpskm
[0];
153 spi_message_init(&msg
);
154 spi_message_add_tail(&xfer
, &msg
);
155 ret
= spi_sync(st
->sdev
, &msg
);
160 xfer
.tx_buf
= &config
->outpskr
[0];
162 spi_message_init(&msg
);
163 spi_message_add_tail(&xfer
, &msg
);
164 ret
= spi_sync(st
->sdev
, &msg
);
169 xfer
.tx_buf
= &config
->daccntl
[0];
171 spi_message_init(&msg
);
172 spi_message_add_tail(&xfer
, &msg
);
173 ret
= spi_sync(st
->sdev
, &msg
);
177 mutex_unlock(&st
->lock
);
179 return ret
? ret
: len
;
182 static IIO_DEVICE_ATTR(dds
, S_IWUSR
, NULL
, ad9852_set_parameter
, 0);
184 static void ad9852_init(struct ad9852_state
*st
)
186 struct spi_message msg
;
187 struct spi_transfer xfer
;
191 config
[0] = addr_contrl
;
193 config
[2] = REFMULT2
| BYPPLL
| PLLRANG
;
197 mutex_lock(&st
->lock
);
200 xfer
.tx_buf
= &config
;
202 spi_message_init(&msg
);
203 spi_message_add_tail(&xfer
, &msg
);
204 ret
= spi_sync(st
->sdev
, &msg
);
209 mutex_unlock(&st
->lock
);
215 static struct attribute
*ad9852_attributes
[] = {
216 &iio_dev_attr_dds
.dev_attr
.attr
,
220 static const struct attribute_group ad9852_attribute_group
= {
221 .attrs
= ad9852_attributes
,
224 static const struct iio_info ad9852_info
= {
225 .attrs
= &ad9852_attribute_group
,
226 .driver_module
= THIS_MODULE
,
229 static int __devinit
ad9852_probe(struct spi_device
*spi
)
231 struct ad9852_state
*st
;
232 struct iio_dev
*idev
;
235 idev
= iio_allocate_device(sizeof(*st
));
241 spi_set_drvdata(spi
, idev
);
242 mutex_init(&st
->lock
);
245 idev
->dev
.parent
= &spi
->dev
;
246 idev
->info
= &ad9852_info
;
247 idev
->modes
= INDIO_DIRECT_MODE
;
249 ret
= iio_device_register(idev
);
252 spi
->max_speed_hz
= 2000000;
253 spi
->mode
= SPI_MODE_3
;
254 spi
->bits_per_word
= 8;
261 iio_free_device(idev
);
267 static int __devexit
ad9852_remove(struct spi_device
*spi
)
269 iio_device_unregister(spi_get_drvdata(spi
));
270 iio_free_device(spi_get_drvdata(spi
));
275 static struct spi_driver ad9852_driver
= {
278 .owner
= THIS_MODULE
,
280 .probe
= ad9852_probe
,
281 .remove
= __devexit_p(ad9852_remove
),
284 static __init
int ad9852_spi_init(void)
286 return spi_register_driver(&ad9852_driver
);
288 module_init(ad9852_spi_init
);
290 static __exit
void ad9852_spi_exit(void)
292 spi_unregister_driver(&ad9852_driver
);
294 module_exit(ad9852_spi_exit
);
296 MODULE_AUTHOR("Cliff Cai");
297 MODULE_DESCRIPTION("Analog Devices ad9852 driver");
298 MODULE_LICENSE("GPL v2");