staging:iio: implement an iio_info structure to take some of the constant elements...
[zen-stable.git] / drivers / staging / iio / resolver / ad2s120x.c
blobf83e1422fd2905d957516438fc54e1e6cb33aac0
1 /*
2 * ad2s120x.c simple support for the ADI Resolver to Digital Converters: AD2S1200/1205
4 * Copyright (c) 2010-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>
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
20 #include "../iio.h"
21 #include "../sysfs.h"
23 #define DRV_NAME "ad2s120x"
25 /* input pin sample and rdvel is controlled by driver */
26 #define AD2S120X_PN 2
28 /* input clock on serial interface */
29 #define AD2S120X_HZ 8192000
30 /* clock period in nano second */
31 #define AD2S120X_TSCLK (1000000000/AD2S120X_HZ)
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];
43 static ssize_t ad2s120x_show_pos_vel(struct device *dev,
44 struct device_attribute *attr, char *buf)
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;
56 xfer.len = 1;
57 xfer.tx_buf = st->tx;
58 xfer.rx_buf = st->rx;
59 mutex_lock(&st->lock);
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);
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');
79 /* delay 18 ns */
80 /* ndelay(18); */
82 gpio_set_value(st->rdvel, 0);
83 /* ndelay(5);*/
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);
104 return ret ? ret : len;
107 static ssize_t ad2s120x_show_pos(struct device *dev,
108 struct device_attribute *attr, char *buf)
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;
119 xfer.len = 1;
120 xfer.tx_buf = st->tx;
121 xfer.rx_buf = st->rx;
122 mutex_lock(&st->lock);
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);
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);
147 return ret ? ret : len;
150 static ssize_t ad2s120x_show_vel(struct device *dev,
151 struct device_attribute *attr, char *buf)
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;
162 xfer.len = 1;
163 xfer.tx_buf = st->tx;
164 xfer.rx_buf = st->rx;
165 mutex_lock(&st->lock);
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);
172 gpio_set_value(st->rdvel, 0);
173 /* ndelay(5);*/
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);
194 return ret ? ret : len;
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);
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,
211 static const struct attribute_group ad2s120x_attribute_group = {
212 .attrs = ad2s120x_attributes,
215 static const struct iio_info ad2s120x_info = {
216 .attrs = &ad2s120x_attribute_group,
217 .driver_module = THIS_MODULE,
220 static int __devinit ad2s120x_probe(struct spi_device *spi)
222 struct ad2s120x_state *st;
223 int pn, ret = 0;
224 unsigned short *pins = spi->dev.platform_data;
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;
232 gpio_direction_output(pins[pn], 1);
235 st = kzalloc(sizeof(*st), GFP_KERNEL);
236 if (st == NULL) {
237 ret = -ENOMEM;
238 goto error_ret;
240 spi_set_drvdata(spi, st);
242 mutex_init(&st->lock);
243 st->sdev = spi;
244 st->sample = pins[0];
245 st->rdvel = pins[1];
247 st->idev = iio_allocate_device(0);
248 if (st->idev == NULL) {
249 ret = -ENOMEM;
250 goto error_free_st;
252 st->idev->dev.parent = &spi->dev;
254 st->idev->info = &ad2s120x_info;
255 st->idev->dev_data = (void *)(st);
256 st->idev->modes = INDIO_DIRECT_MODE;
258 ret = iio_device_register(st->idev);
259 if (ret)
260 goto error_free_dev;
262 spi->max_speed_hz = AD2S120X_HZ;
263 spi->mode = SPI_MODE_3;
264 spi_setup(spi);
266 return 0;
268 error_free_dev:
269 iio_free_device(st->idev);
270 error_free_st:
271 kfree(st);
272 error_ret:
273 for (--pn; pn >= 0; pn--)
274 gpio_free(pins[pn]);
275 return ret;
278 static int __devexit ad2s120x_remove(struct spi_device *spi)
280 struct ad2s120x_state *st = spi_get_drvdata(spi);
282 iio_device_unregister(st->idev);
283 kfree(st);
285 return 0;
288 static struct spi_driver ad2s120x_driver = {
289 .driver = {
290 .name = DRV_NAME,
291 .owner = THIS_MODULE,
293 .probe = ad2s120x_probe,
294 .remove = __devexit_p(ad2s120x_remove),
297 static __init int ad2s120x_spi_init(void)
299 return spi_register_driver(&ad2s120x_driver);
301 module_init(ad2s120x_spi_init);
303 static __exit void ad2s120x_spi_exit(void)
305 spi_unregister_driver(&ad2s120x_driver);
307 module_exit(ad2s120x_spi_exit);
309 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
310 MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
311 MODULE_LICENSE("GPL v2");