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>
23 #define DRV_NAME "ad2s120x"
25 /* input pin sample and rdvel is controlled by driver */
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
{
36 struct spi_device
*sdev
;
37 unsigned short sample
;
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
;
53 struct iio_dev
*idev
= dev_get_drvdata(dev
);
54 struct ad2s120x_state
*st
= idev
->dev_data
;
59 mutex_lock(&st
->lock
);
61 gpio_set_value(st
->sample
, 0);
62 /* delay (6 * AD2S120X_TSCLK + 20) nano seconds */
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
);
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');
82 gpio_set_value(st
->rdvel
, 0);
85 spi_message_init(&msg
);
86 spi_message_add_tail(&xfer
, &msg
);
87 ret
= spi_sync(st
->sdev
, &msg
);
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');
99 gpio_set_value(st
->rdvel
, 1);
100 /* delay (2 * AD2S120X_TSCLK + 20) ns for sample pulse */
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
;
116 struct iio_dev
*idev
= dev_get_drvdata(dev
);
117 struct ad2s120x_state
*st
= idev
->dev_data
;
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 */
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
);
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');
143 /* delay (2 * AD2S120X_TSCLK + 20) ns for sample pulse */
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
;
159 struct iio_dev
*idev
= dev_get_drvdata(dev
);
160 struct ad2s120x_state
*st
= idev
->dev_data
;
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 */
170 gpio_set_value(st
->sample
, 1);
172 gpio_set_value(st
->rdvel
, 0);
175 spi_message_init(&msg
);
176 spi_message_add_tail(&xfer
, &msg
);
177 ret
= spi_sync(st
->sdev
, &msg
);
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');
189 gpio_set_value(st
->rdvel
, 1);
190 /* delay (2 * AD2S120X_TSCLK + 20) ns for sample pulse */
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
,
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
;
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",
232 gpio_direction_output(pins
[pn
], 1);
235 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
240 spi_set_drvdata(spi
, st
);
242 mutex_init(&st
->lock
);
244 st
->sample
= pins
[0];
247 st
->idev
= iio_allocate_device(0);
248 if (st
->idev
== NULL
) {
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
);
262 spi
->max_speed_hz
= AD2S120X_HZ
;
263 spi
->mode
= SPI_MODE_3
;
269 iio_free_device(st
->idev
);
273 for (--pn
; pn
>= 0; pn
--)
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
);
288 static struct spi_driver ad2s120x_driver
= {
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");