1
2
3
4
5
6
7
8
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
26#define AD2S120X_PN 2
27
28
29#define AD2S120X_HZ 8192000
30
31#define AD2S120X_TSCLK (1000000000/AD2S120X_HZ)
32
33struct 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
43static 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
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
80
81
82 gpio_set_value(st->rdvel, 0);
83
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');
98error_ret:
99 gpio_set_value(st->rdvel, 1);
100
101 udelay(1);
102 mutex_unlock(&st->lock);
103
104 return ret ? ret : len;
105}
106
107static 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
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');
142error_ret:
143
144 udelay(1);
145 mutex_unlock(&st->lock);
146
147 return ret ? ret : len;
148}
149
150static 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
169 udelay(1);
170 gpio_set_value(st->sample, 1);
171
172 gpio_set_value(st->rdvel, 0);
173
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');
188error_ret:
189 gpio_set_value(st->rdvel, 1);
190
191 udelay(1);
192 mutex_unlock(&st->lock);
193
194 return ret ? ret : len;
195}
196
197static IIO_CONST_ATTR(description,
198 "12-Bit R/D Converter with Reference Oscillator");
199static IIO_DEVICE_ATTR(pos_vel, S_IRUGO, ad2s120x_show_pos_vel, NULL, 0);
200static IIO_DEVICE_ATTR(pos, S_IRUGO, ad2s120x_show_pos, NULL, 0);
201static IIO_DEVICE_ATTR(vel, S_IRUGO, ad2s120x_show_vel, NULL, 0);
202
203static 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
211static const struct attribute_group ad2s120x_attribute_group = {
212 .attrs = ad2s120x_attributes,
213};
214
215static const struct iio_info ad2s120x_info = {
216 .attrs = &ad2s120x_attribute_group,
217 .driver_module = THIS_MODULE,
218};
219
220static int __devinit ad2s120x_probe(struct spi_device *spi)
221{
222 struct ad2s120x_state *st;
223 int pn, ret = 0;
224 unsigned short *pins = spi->dev.platform_data;
225
226 for (pn = 0; pn < AD2S120X_PN; pn++) {
227 if (gpio_request(pins[pn], DRV_NAME)) {
228 pr_err("%s: request gpio pin %d failed\n",
229 DRV_NAME, pins[pn]);
230 goto error_ret;
231 }
232 gpio_direction_output(pins[pn], 1);
233 }
234
235 st = kzalloc(sizeof(*st), GFP_KERNEL);
236 if (st == NULL) {
237 ret = -ENOMEM;
238 goto error_ret;
239 }
240 spi_set_drvdata(spi, st);
241
242 mutex_init(&st->lock);
243 st->sdev = spi;
244 st->sample = pins[0];
245 st->rdvel = pins[1];
246
247 st->idev = iio_allocate_device(0);
248 if (st->idev == NULL) {
249 ret = -ENOMEM;
250 goto error_free_st;
251 }
252 st->idev->dev.parent = &spi->dev;
253
254 st->idev->info = &ad2s120x_info;
255 st->idev->dev_data = (void *)(st);
256 st->idev->modes = INDIO_DIRECT_MODE;
257
258 ret = iio_device_register(st->idev);
259 if (ret)
260 goto error_free_dev;
261
262 spi->max_speed_hz = AD2S120X_HZ;
263 spi->mode = SPI_MODE_3;
264 spi_setup(spi);
265
266 return 0;
267
268error_free_dev:
269 iio_free_device(st->idev);
270error_free_st:
271 kfree(st);
272error_ret:
273 for (--pn; pn >= 0; pn--)
274 gpio_free(pins[pn]);
275 return ret;
276}
277
278static int __devexit ad2s120x_remove(struct spi_device *spi)
279{
280 struct ad2s120x_state *st = spi_get_drvdata(spi);
281
282 iio_device_unregister(st->idev);
283 kfree(st);
284
285 return 0;
286}
287
288static struct spi_driver ad2s120x_driver = {
289 .driver = {
290 .name = DRV_NAME,
291 .owner = THIS_MODULE,
292 },
293 .probe = ad2s120x_probe,
294 .remove = __devexit_p(ad2s120x_remove),
295};
296
297static __init int ad2s120x_spi_init(void)
298{
299 return spi_register_driver(&ad2s120x_driver);
300}
301module_init(ad2s120x_spi_init);
302
303static __exit void ad2s120x_spi_exit(void)
304{
305 spi_unregister_driver(&ad2s120x_driver);
306}
307module_exit(ad2s120x_spi_exit);
308
309MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
310MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
311MODULE_LICENSE("GPL v2");
312