Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / dds / ad9852.c
blob0184585425d82c7a5ed22b9ada934ac43fa9c007
1 /*
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>
18 #include "../iio.h"
19 #include "../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)
39 #define IEUPCLK (1)
40 #define OSKEN (1 << 5)
42 #define read_bit (1 << 7)
44 /* Register format: 1 byte addr + value */
45 struct ad9852_config {
46 u8 phajst0[3];
47 u8 phajst1[3];
48 u8 fretun1[6];
49 u8 fretun2[6];
50 u8 dltafre[6];
51 u8 updtclk[5];
52 u8 ramprat[4];
53 u8 control[5];
54 u8 outpskm[3];
55 u8 outpskr[2];
56 u8 daccntl[3];
59 struct ad9852_state {
60 struct mutex lock;
61 struct spi_device *sdev;
64 static ssize_t ad9852_set_parameter(struct device *dev,
65 struct device_attribute *attr,
66 const char *buf,
67 size_t len)
69 struct spi_message msg;
70 struct spi_transfer xfer;
71 int ret;
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);
76 xfer.len = 3;
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);
83 if (ret)
84 goto error_ret;
86 xfer.len = 3;
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);
92 if (ret)
93 goto error_ret;
95 xfer.len = 6;
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);
101 if (ret)
102 goto error_ret;
104 xfer.len = 6;
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);
110 if (ret)
111 goto error_ret;
113 xfer.len = 6;
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);
119 if (ret)
120 goto error_ret;
122 xfer.len = 5;
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);
128 if (ret)
129 goto error_ret;
131 xfer.len = 4;
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);
137 if (ret)
138 goto error_ret;
140 xfer.len = 5;
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);
146 if (ret)
147 goto error_ret;
149 xfer.len = 3;
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);
155 if (ret)
156 goto error_ret;
158 xfer.len = 2;
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);
164 if (ret)
165 goto error_ret;
167 xfer.len = 3;
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);
173 if (ret)
174 goto error_ret;
175 error_ret:
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;
187 int ret;
188 u8 config[5];
190 config[0] = addr_contrl;
191 config[1] = COMPPD;
192 config[2] = REFMULT2 | BYPPLL | PLLRANG;
193 config[3] = IEUPCLK;
194 config[4] = OSKEN;
196 mutex_lock(&st->lock);
198 xfer.len = 5;
199 xfer.tx_buf = &config;
201 spi_message_init(&msg);
202 spi_message_add_tail(&xfer, &msg);
203 ret = spi_sync(st->sdev, &msg);
204 if (ret)
205 goto error_ret;
207 error_ret:
208 mutex_unlock(&st->lock);
214 static struct attribute *ad9852_attributes[] = {
215 &iio_dev_attr_dds.dev_attr.attr,
216 NULL,
219 static const struct attribute_group ad9852_attribute_group = {
220 .name = DRV_NAME,
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;
233 int ret = 0;
235 idev = iio_allocate_device(sizeof(*st));
236 if (idev == NULL) {
237 ret = -ENOMEM;
238 goto error_ret;
240 st = iio_priv(idev);
241 spi_set_drvdata(spi, idev);
242 mutex_init(&st->lock);
243 st->sdev = spi;
245 idev->dev.parent = &spi->dev;
246 idev->info = &ad9852_info;
247 idev->modes = INDIO_DIRECT_MODE;
249 ret = iio_device_register(idev);
250 if (ret)
251 goto error_free_dev;
252 spi->max_speed_hz = 2000000;
253 spi->mode = SPI_MODE_3;
254 spi->bits_per_word = 8;
255 spi_setup(spi);
256 ad9852_init(st);
258 return 0;
260 error_free_dev:
261 iio_free_device(idev);
263 error_ret:
264 return ret;
267 static int __devexit ad9852_remove(struct spi_device *spi)
269 iio_device_unregister(spi_get_drvdata(spi));
271 return 0;
274 static struct spi_driver ad9852_driver = {
275 .driver = {
276 .name = DRV_NAME,
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");