]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/iio/dds/ad9832.c
a4bb0482ed5b8bc489b2dc570721ae9a27fddb10
[mv-sheeva.git] / drivers / staging / iio / dds / ad9832.c
1 /*
2  * Driver for ADI Direct Digital Synthesis ad9832
3  *
4  * Copyright (c) 2010 Analog Devices Inc.
5  *
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.
9  *
10  */
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
18 #include "../iio.h"
19 #include "../sysfs.h"
20
21 #define DRV_NAME "ad9832"
22
23 #define value_mask (u16)0xf000
24 #define cmd_shift 12
25 #define add_shift 8
26 #define AD9832_SYNC (1 << 13)
27 #define AD9832_SELSRC (1 << 12)
28 #define AD9832_SLEEP (1 << 13)
29 #define AD9832_RESET (1 << 12)
30 #define AD9832_CLR (1 << 11)
31
32 #define ADD_FREQ0LL 0x0
33 #define ADD_FREQ0HL 0x1
34 #define ADD_FREQ0LM 0x2
35 #define ADD_FREQ0HM 0x3
36 #define ADD_FREQ1LL 0x4
37 #define ADD_FREQ1HL 0x5
38 #define ADD_FREQ1LM 0x6
39 #define ADD_FREQ1HM 0x7
40 #define ADD_PHASE0L 0x8
41 #define ADD_PHASE0H 0x9
42 #define ADD_PHASE1L 0xa
43 #define ADD_PHASE1H 0xb
44 #define ADD_PHASE2L 0xc
45 #define ADD_PHASE2H 0xd
46 #define ADD_PHASE3L 0xe
47 #define ADD_PHASE3H 0xf
48
49 #define CMD_PHA8BITSW 0x1
50 #define CMD_PHA16BITSW 0x0
51 #define CMD_FRE8BITSW 0x3
52 #define CMD_FRE16BITSW 0x2
53 #define CMD_SELBITSCTL 0x6
54
55 struct ad9832_setting {
56         u16 freq0[4];
57         u16 freq1[4];
58         u16 phase0[2];
59         u16 phase1[2];
60         u16 phase2[2];
61         u16 phase3[2];
62 };
63
64 struct ad9832_state {
65         struct mutex lock;
66         struct iio_dev *idev;
67         struct spi_device *sdev;
68 };
69
70 static ssize_t ad9832_set_parameter(struct device *dev,
71                                         struct device_attribute *attr,
72                                         const char *buf,
73                                         size_t len)
74 {
75         struct spi_message msg;
76         struct spi_transfer xfer;
77         int ret;
78         struct ad9832_setting config;
79         struct iio_dev *idev = dev_get_drvdata(dev);
80         struct ad9832_state *st = idev->dev_data;
81
82         config.freq0[0] = (CMD_FRE8BITSW << add_shift | ADD_FREQ0LL << add_shift | buf[0]);
83         config.freq0[1] = (CMD_FRE16BITSW << add_shift | ADD_FREQ0HL << add_shift | buf[1]);
84         config.freq0[2] = (CMD_FRE8BITSW << add_shift | ADD_FREQ0LM << add_shift | buf[2]);
85         config.freq0[3] = (CMD_FRE16BITSW << add_shift | ADD_FREQ0HM << add_shift | buf[3]);
86         config.freq1[0] = (CMD_FRE8BITSW << add_shift | ADD_FREQ1LL << add_shift | buf[4]);
87         config.freq1[1] = (CMD_FRE16BITSW << add_shift | ADD_FREQ1HL << add_shift | buf[5]);
88         config.freq1[2] = (CMD_FRE8BITSW << add_shift | ADD_FREQ1LM << add_shift | buf[6]);
89         config.freq1[3] = (CMD_FRE16BITSW << add_shift | ADD_FREQ1HM << add_shift | buf[7]);
90
91         config.phase0[0] = (CMD_PHA8BITSW << add_shift | ADD_PHASE0L << add_shift | buf[9]);
92         config.phase0[1] = (CMD_PHA16BITSW << add_shift | ADD_PHASE0H << add_shift | buf[10]);
93         config.phase1[0] = (CMD_PHA8BITSW << add_shift | ADD_PHASE1L << add_shift | buf[11]);
94         config.phase1[1] = (CMD_PHA16BITSW << add_shift | ADD_PHASE1H << add_shift | buf[12]);
95         config.phase2[0] = (CMD_PHA8BITSW << add_shift | ADD_PHASE2L << add_shift | buf[13]);
96         config.phase2[1] = (CMD_PHA16BITSW << add_shift | ADD_PHASE2H << add_shift | buf[14]);
97         config.phase3[0] = (CMD_PHA8BITSW << add_shift | ADD_PHASE3L << add_shift | buf[15]);
98         config.phase3[1] = (CMD_PHA16BITSW << add_shift | ADD_PHASE3H << add_shift | buf[16]);
99
100         xfer.len = 2 * len;
101         xfer.tx_buf = &config;
102         mutex_lock(&st->lock);
103
104         spi_message_init(&msg);
105         spi_message_add_tail(&xfer, &msg);
106         ret = spi_sync(st->sdev, &msg);
107         if (ret)
108                 goto error_ret;
109 error_ret:
110         mutex_unlock(&st->lock);
111
112         return ret ? ret : len;
113 }
114
115 static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9832_set_parameter, 0);
116
117 static struct attribute *ad9832_attributes[] = {
118         &iio_dev_attr_dds.dev_attr.attr,
119         NULL,
120 };
121
122 static const struct attribute_group ad9832_attribute_group = {
123         .name = DRV_NAME,
124         .attrs = ad9832_attributes,
125 };
126
127 static void ad9832_init(struct ad9832_state *st)
128 {
129         struct spi_message msg;
130         struct spi_transfer xfer;
131         int ret;
132         u16 config = 0;
133
134         config = 0x3 << 14 | AD9832_SLEEP | AD9832_RESET | AD9832_CLR;
135
136         mutex_lock(&st->lock);
137
138         xfer.len = 2;
139         xfer.tx_buf = &config;
140
141         spi_message_init(&msg);
142         spi_message_add_tail(&xfer, &msg);
143         ret = spi_sync(st->sdev, &msg);
144         if (ret)
145                 goto error_ret;
146
147         config = 0x2 << 14 | AD9832_SYNC | AD9832_SELSRC;
148         xfer.len = 2;
149         xfer.tx_buf = &config;
150
151         spi_message_init(&msg);
152         spi_message_add_tail(&xfer, &msg);
153         ret = spi_sync(st->sdev, &msg);
154         if (ret)
155                 goto error_ret;
156
157         config = CMD_SELBITSCTL << cmd_shift;
158         xfer.len = 2;
159         xfer.tx_buf = &config;
160
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;
166
167         config = 0x3 << 14;
168
169         mutex_lock(&st->lock);
170
171         xfer.len = 2;
172         xfer.tx_buf = &config;
173
174         spi_message_init(&msg);
175         spi_message_add_tail(&xfer, &msg);
176         ret = spi_sync(st->sdev, &msg);
177         if (ret)
178                 goto error_ret;
179 error_ret:
180         mutex_unlock(&st->lock);
181
182
183
184 }
185
186 static int __devinit ad9832_probe(struct spi_device *spi)
187 {
188         struct ad9832_state *st;
189         int ret = 0;
190
191         st = kzalloc(sizeof(*st), GFP_KERNEL);
192         if (st == NULL) {
193                 ret = -ENOMEM;
194                 goto error_ret;
195         }
196         spi_set_drvdata(spi, st);
197
198         mutex_init(&st->lock);
199         st->sdev = spi;
200
201         st->idev = iio_allocate_device();
202         if (st->idev == NULL) {
203                 ret = -ENOMEM;
204                 goto error_free_st;
205         }
206         st->idev->dev.parent = &spi->dev;
207         st->idev->num_interrupt_lines = 0;
208         st->idev->event_attrs = NULL;
209
210         st->idev->attrs = &ad9832_attribute_group;
211         st->idev->dev_data = (void *)(st);
212         st->idev->driver_module = THIS_MODULE;
213         st->idev->modes = INDIO_DIRECT_MODE;
214
215         ret = iio_device_register(st->idev);
216         if (ret)
217                 goto error_free_dev;
218         spi->max_speed_hz = 2000000;
219         spi->mode = SPI_MODE_3;
220         spi->bits_per_word = 16;
221         spi_setup(spi);
222         ad9832_init(st);
223         return 0;
224
225 error_free_dev:
226         iio_free_device(st->idev);
227 error_free_st:
228         kfree(st);
229 error_ret:
230         return ret;
231 }
232
233 static int __devexit ad9832_remove(struct spi_device *spi)
234 {
235         struct ad9832_state *st = spi_get_drvdata(spi);
236
237         iio_device_unregister(st->idev);
238         kfree(st);
239
240         return 0;
241 }
242
243 static struct spi_driver ad9832_driver = {
244         .driver = {
245                 .name = DRV_NAME,
246                 .owner = THIS_MODULE,
247         },
248         .probe = ad9832_probe,
249         .remove = __devexit_p(ad9832_remove),
250 };
251
252 static __init int ad9832_spi_init(void)
253 {
254         return spi_register_driver(&ad9832_driver);
255 }
256 module_init(ad9832_spi_init);
257
258 static __exit void ad9832_spi_exit(void)
259 {
260         spi_unregister_driver(&ad9832_driver);
261 }
262 module_exit(ad9832_spi_exit);
263
264 MODULE_AUTHOR("Cliff Cai");
265 MODULE_DESCRIPTION("Analog Devices ad9832 driver");
266 MODULE_LICENSE("GPL v2");