1 /*
2 * ad2s120x.c simple support for the ADI Resolver to Digital Converters: AD2S1200/1205
3 *
4 * Copyright (c) 2010-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 #include <linux/delay.h>
18 #include <linux/gpio.h>
19
20 #include "../iio.h"
21 #include "../sysfs.h"
22
23 #define DRV_NAME "ad2s120x"
24
25 /* input pin sample and rdvel is controlled by driver */
26 #define AD2S120X_PN 2
27
28 /* input clock on serial interface */
29 #define AD2S120X_HZ 8192000
30 /* clock period in nano second */
31 #define AD2S120X_TSCLK (1000000000/AD2S120X_HZ)
32
33 struct ad2s120x_state {
34 struct mutex lock;
35 struct iio_dev *idev;
36 struct spi_device *sdev;
37 unsigned short sample;
38 unsigned short rdvel;
39 u8 rx[2];
40 u8 tx[2];
41 };
42
ad2s120x_show_pos_vel(struct device * dev,struct device_attribute * attr,char * buf)43 static ssize_t ad2s120x_show_pos_vel(struct device *dev,
44 struct device_attribute *attr, char *buf)
45 {
46 struct spi_message msg;
47 struct spi_transfer xfer;
48 int ret = 0;
49 ssize_t len = 0;
50 u16 pos;
51 s16 vel;
52 u8 status;
53 struct iio_dev *idev = dev_get_drvdata(dev);
54 struct ad2s120x_state *st = idev->dev_data;
55
56 xfer.len = 1;
57 xfer.tx_buf = st->tx;
58 xfer.rx_buf = st->rx;
59 mutex_lock(&st->lock);
60
61 gpio_set_value(st->sample, 0);
62 /* delay (6 * AD2S120X_TSCLK + 20) nano seconds */
63 udelay(1);
64 gpio_set_value(st->sample, 1);
65
66 spi_message_init(&msg);
67 spi_message_add_tail(&xfer, &msg);
68 ret = spi_sync(st->sdev, &msg);
69 if (ret)
70 goto error_ret;
71 status = st->rx[1];
72 pos = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
73 len = sprintf(buf, "%d %c%c%c%c ", pos,
74 (status & 0x8) ? 'P' : 'V',
75 (status & 0x4) ? 'd' : '_',
76 (status & 0x2) ? 'l' : '_',
77 (status & 0x1) ? '1' : '0');
78
79 /* delay 18 ns */
80 /* ndelay(18); */
81
82 gpio_set_value(st->rdvel, 0);
83 /* ndelay(5);*/
84
85 spi_message_init(&msg);
86 spi_message_add_tail(&xfer, &msg);
87 ret = spi_sync(st->sdev, &msg);
88 if (ret)
89 goto error_ret;
90 status = st->rx[1];
91 vel = (st->rx[0] & 0x80) ? 0xf000 : 0;
92 vel |= (((s16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
93 len += sprintf(buf + len, "%d %c%c%c%c\n", vel,
94 (status & 0x8) ? 'P' : 'V',
95 (status & 0x4) ? 'd' : '_',
96 (status & 0x2) ? 'l' : '_',
97 (status & 0x1) ? '1' : '0');
98 error_ret:
99 gpio_set_value(st->rdvel, 1);
100 /* delay (2 * AD2S120X_TSCLK + 20) ns for sample pulse */
101 udelay(1);
102 mutex_unlock(&st->lock);
103
104 return ret ? ret : len;
105 }
106
ad2s120x_show_pos(struct device * dev,struct device_attribute * attr,char * buf)107 static ssize_t ad2s120x_show_pos(struct device *dev,
108 struct device_attribute *attr, char *buf)
109 {
110 struct spi_message msg;
111 struct spi_transfer xfer;
112 int ret = 0;
113 ssize_t len = 0;
114 u16 pos;
115 u8 status;
116 struct iio_dev *idev = dev_get_drvdata(dev);
117 struct ad2s120x_state *st = idev->dev_data;
118
119 xfer.len = 1;
120 xfer.tx_buf = st->tx;
121 xfer.rx_buf = st->rx;
122 mutex_lock(&st->lock);
123
124 gpio_set_value(st->sample, 0);
125 /* delay (6 * AD2S120X_TSCLK + 20) nano seconds */
126 udelay(1);
127 gpio_set_value(st->sample, 1);
128 gpio_set_value(st->rdvel, 1);
129
130 spi_message_init(&msg);
131 spi_message_add_tail(&xfer, &msg);
132 ret = spi_sync(st->sdev, &msg);
133 if (ret)
134 goto error_ret;
135 status = st->rx[1];
136 pos = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
137 len = sprintf(buf, "%d %c%c%c%c ", pos,
138 (status & 0x8) ? 'P' : 'V',
139 (status & 0x4) ? 'd' : '_',
140 (status & 0x2) ? 'l' : '_',
141 (status & 0x1) ? '1' : '0');
142 error_ret:
143 /* delay (2 * AD2S120X_TSCLK + 20) ns for sample pulse */
144 udelay(1);
145 mutex_unlock(&st->lock);
146
147 return ret ? ret : len;
148 }
149
ad2s120x_show_vel(struct device * dev,struct device_attribute * attr,char * buf)150 static ssize_t ad2s120x_show_vel(struct device *dev,
151 struct device_attribute *attr, char *buf)
152 {
153 struct spi_message msg;
154 struct spi_transfer xfer;
155 int ret = 0;
156 ssize_t len = 0;
157 s16 vel;
158 u8 status;
159 struct iio_dev *idev = dev_get_drvdata(dev);
160 struct ad2s120x_state *st = idev->dev_data;
161
162 xfer.len = 1;
163 xfer.tx_buf = st->tx;
164 xfer.rx_buf = st->rx;
165 mutex_lock(&st->lock);
166
167 gpio_set_value(st->sample, 0);
168 /* delay (6 * AD2S120X_TSCLK + 20) nano seconds */
169 udelay(1);
170 gpio_set_value(st->sample, 1);
171
172 gpio_set_value(st->rdvel, 0);
173 /* ndelay(5);*/
174
175 spi_message_init(&msg);
176 spi_message_add_tail(&xfer, &msg);
177 ret = spi_sync(st->sdev, &msg);
178 if (ret)
179 goto error_ret;
180 status = st->rx[1];
181 vel = (st->rx[0] & 0x80) ? 0xf000 : 0;
182 vel |= (((s16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
183 len += sprintf(buf + len, "%d %c%c%c%c\n", vel,
184 (status & 0x8) ? 'P' : 'V',
185 (status & 0x4) ? 'd' : '_',
186 (status & 0x2) ? 'l' : '_',
187 (status & 0x1) ? '1' : '0');
188 error_ret:
189 gpio_set_value(st->rdvel, 1);
190 /* delay (2 * AD2S120X_TSCLK + 20) ns for sample pulse */
191 udelay(1);
192 mutex_unlock(&st->lock);
193
194 return ret ? ret : len;
195 }
196
197 static IIO_CONST_ATTR(description,
198 "12-Bit R/D Converter with Reference Oscillator");
199 static IIO_DEVICE_ATTR(pos_vel, S_IRUGO, ad2s120x_show_pos_vel, NULL, 0);
200 static IIO_DEVICE_ATTR(pos, S_IRUGO, ad2s120x_show_pos, NULL, 0);
201 static IIO_DEVICE_ATTR(vel, S_IRUGO, ad2s120x_show_vel, NULL, 0);
202
203 static struct attribute *ad2s120x_attributes[] = {
204 &iio_const_attr_description.dev_attr.attr,
205 &iio_dev_attr_pos_vel.dev_attr.attr,
206 &iio_dev_attr_pos.dev_attr.attr,
207 &iio_dev_attr_vel.dev_attr.attr,
208 NULL,
209 };
210
211 static const struct attribute_group ad2s120x_attribute_group = {
212 .name = DRV_NAME,
213 .attrs = ad2s120x_attributes,
214 };
215
ad2s120x_probe(struct spi_device * spi)216 static int __devinit ad2s120x_probe(struct spi_device *spi)
217 {
218 struct ad2s120x_state *st;
219 int pn, ret = 0;
220 unsigned short *pins = spi->dev.platform_data;
221
222 for (pn = 0; pn < AD2S120X_PN; pn++) {
223 if (gpio_request(pins[pn], DRV_NAME)) {
224 pr_err("%s: request gpio pin %d failed\n",
225 DRV_NAME, pins[pn]);
226 goto error_ret;
227 }
228 gpio_direction_output(pins[pn], 1);
229 }
230
231 st = kzalloc(sizeof(*st), GFP_KERNEL);
232 if (st == NULL) {
233 ret = -ENOMEM;
234 goto error_ret;
235 }
236 spi_set_drvdata(spi, st);
237
238 mutex_init(&st->lock);
239 st->sdev = spi;
240 st->sample = pins[0];
241 st->rdvel = pins[1];
242
243 st->idev = iio_allocate_device();
244 if (st->idev == NULL) {
245 ret = -ENOMEM;
246 goto error_free_st;
247 }
248 st->idev->dev.parent = &spi->dev;
249 st->idev->num_interrupt_lines = 0;
250 st->idev->event_attrs = NULL;
251
252 st->idev->attrs = &ad2s120x_attribute_group;
253 st->idev->dev_data = (void *)(st);
254 st->idev->driver_module = THIS_MODULE;
255 st->idev->modes = INDIO_DIRECT_MODE;
256
257 ret = iio_device_register(st->idev);
258 if (ret)
259 goto error_free_dev;
260
261 spi->max_speed_hz = AD2S120X_HZ;
262 spi->mode = SPI_MODE_3;
263 spi_setup(spi);
264
265 return 0;
266
267 error_free_dev:
268 iio_free_device(st->idev);
269 error_free_st:
270 kfree(st);
271 error_ret:
272 for (--pn; pn >= 0; pn--)
273 gpio_free(pins[pn]);
274 return ret;
275 }
276
ad2s120x_remove(struct spi_device * spi)277 static int __devexit ad2s120x_remove(struct spi_device *spi)
278 {
279 struct ad2s120x_state *st = spi_get_drvdata(spi);
280
281 iio_device_unregister(st->idev);
282 kfree(st);
283
284 return 0;
285 }
286
287 static struct spi_driver ad2s120x_driver = {
288 .driver = {
289 .name = DRV_NAME,
290 .owner = THIS_MODULE,
291 },
292 .probe = ad2s120x_probe,
293 .remove = __devexit_p(ad2s120x_remove),
294 };
295
ad2s120x_spi_init(void)296 static __init int ad2s120x_spi_init(void)
297 {
298 return spi_register_driver(&ad2s120x_driver);
299 }
300 module_init(ad2s120x_spi_init);
301
ad2s120x_spi_exit(void)302 static __exit void ad2s120x_spi_exit(void)
303 {
304 spi_unregister_driver(&ad2s120x_driver);
305 }
306 module_exit(ad2s120x_spi_exit);
307
308 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
309 MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
310 MODULE_LICENSE("GPL v2");
311