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>
21 #define DRV_NAME "ad9852"
23 #define addr_phaad1 0x0
24 #define addr_phaad2 0x1
25 #define addr_fretu1 0x2
26 #define addr_fretu2 0x3
27 #define addr_delfre 0x4
28 #define addr_updclk 0x5
29 #define addr_ramclk 0x6
30 #define addr_contrl 0x7
31 #define addr_optskm 0x8
32 #define addr_optskr 0xa
33 #define addr_dacctl 0xb
35 #define COMPPD (1 << 4)
36 #define REFMULT2 (1 << 2)
37 #define BYPPLL (1 << 5)
38 #define PLLRANG (1 << 6)
40 #define OSKEN (1 << 5)
42 #define read_bit (1 << 7)
44 /* Register format: 1 byte addr + value */
45 struct ad9852_config
{
61 struct spi_device
*sdev
;
64 static ssize_t
ad9852_set_parameter(struct device
*dev
,
65 struct device_attribute
*attr
,
69 struct spi_message msg
;
70 struct spi_transfer xfer
;
72 struct ad9852_config
*config
= (struct ad9852_config
*)buf
;
73 struct iio_dev
*idev
= dev_get_drvdata(dev
);
74 struct ad9852_state
*st
= iio_priv(idev
);
77 xfer
.tx_buf
= &config
->phajst0
[0];
78 mutex_lock(&st
->lock
);
80 spi_message_init(&msg
);
81 spi_message_add_tail(&xfer
, &msg
);
82 ret
= spi_sync(st
->sdev
, &msg
);
87 xfer
.tx_buf
= &config
->phajst1
[0];
89 spi_message_init(&msg
);
90 spi_message_add_tail(&xfer
, &msg
);
91 ret
= spi_sync(st
->sdev
, &msg
);
96 xfer
.tx_buf
= &config
->fretun1
[0];
98 spi_message_init(&msg
);
99 spi_message_add_tail(&xfer
, &msg
);
100 ret
= spi_sync(st
->sdev
, &msg
);
105 xfer
.tx_buf
= &config
->fretun2
[0];
107 spi_message_init(&msg
);
108 spi_message_add_tail(&xfer
, &msg
);
109 ret
= spi_sync(st
->sdev
, &msg
);
114 xfer
.tx_buf
= &config
->dltafre
[0];
116 spi_message_init(&msg
);
117 spi_message_add_tail(&xfer
, &msg
);
118 ret
= spi_sync(st
->sdev
, &msg
);
123 xfer
.tx_buf
= &config
->updtclk
[0];
125 spi_message_init(&msg
);
126 spi_message_add_tail(&xfer
, &msg
);
127 ret
= spi_sync(st
->sdev
, &msg
);
132 xfer
.tx_buf
= &config
->ramprat
[0];
134 spi_message_init(&msg
);
135 spi_message_add_tail(&xfer
, &msg
);
136 ret
= spi_sync(st
->sdev
, &msg
);
141 xfer
.tx_buf
= &config
->control
[0];
143 spi_message_init(&msg
);
144 spi_message_add_tail(&xfer
, &msg
);
145 ret
= spi_sync(st
->sdev
, &msg
);
150 xfer
.tx_buf
= &config
->outpskm
[0];
152 spi_message_init(&msg
);
153 spi_message_add_tail(&xfer
, &msg
);
154 ret
= spi_sync(st
->sdev
, &msg
);
159 xfer
.tx_buf
= &config
->outpskr
[0];
161 spi_message_init(&msg
);
162 spi_message_add_tail(&xfer
, &msg
);
163 ret
= spi_sync(st
->sdev
, &msg
);
168 xfer
.tx_buf
= &config
->daccntl
[0];
170 spi_message_init(&msg
);
171 spi_message_add_tail(&xfer
, &msg
);
172 ret
= spi_sync(st
->sdev
, &msg
);
176 mutex_unlock(&st
->lock
);
178 return ret
? ret
: len
;
181 static IIO_DEVICE_ATTR(dds
, S_IWUSR
, NULL
, ad9852_set_parameter
, 0);
183 static void ad9852_init(struct ad9852_state
*st
)
185 struct spi_message msg
;
186 struct spi_transfer xfer
;
190 config
[0] = addr_contrl
;
192 config
[2] = REFMULT2
| BYPPLL
| PLLRANG
;
196 mutex_lock(&st
->lock
);
199 xfer
.tx_buf
= &config
;
201 spi_message_init(&msg
);
202 spi_message_add_tail(&xfer
, &msg
);
203 ret
= spi_sync(st
->sdev
, &msg
);
208 mutex_unlock(&st
->lock
);
214 static struct attribute
*ad9852_attributes
[] = {
215 &iio_dev_attr_dds
.dev_attr
.attr
,
219 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
));
274 static struct spi_driver ad9852_driver
= {
277 .owner
= THIS_MODULE
,
279 .probe
= ad9852_probe
,
280 .remove
= __devexit_p(ad9852_remove
),
283 static __init
int ad9852_spi_init(void)
285 return spi_register_driver(&ad9852_driver
);
287 module_init(ad9852_spi_init
);
289 static __exit
void ad9852_spi_exit(void)
291 spi_unregister_driver(&ad9852_driver
);
293 module_exit(ad9852_spi_exit
);
295 MODULE_AUTHOR("Cliff Cai");
296 MODULE_DESCRIPTION("Analog Devices ad9852 driver");
297 MODULE_LICENSE("GPL v2");