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>
19 #include <linux/module.h>
24 #define DRV_NAME "ad2s120x"
26 /* input pin sample and rdvel is controlled by driver */
29 /* input clock on serial interface */
30 #define AD2S120X_HZ 8192000
31 /* clock period in nano second */
32 #define AD2S120X_TSCLK (1000000000/AD2S120X_HZ)
34 struct ad2s120x_state {
36 struct spi_device *sdev;
39 u8 rx[2] ____cacheline_aligned;
42 static ssize_t ad2s120x_show_val(struct device *dev,
43 struct device_attribute *attr, char *buf)
50 struct ad2s120x_state *st = iio_priv(dev_get_drvdata(dev));
51 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
53 mutex_lock(&st->lock);
55 gpio_set_value(st->sample, 0);
56 /* delay (6 * AD2S120X_TSCLK + 20) nano seconds */
58 gpio_set_value(st->sample, 1);
59 gpio_set_value(st->rdvel, iattr->address);
60 ret = spi_read(st->sdev, st->rx, 2);
65 pos = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
67 vel = (st->rx[0] & 0x80) ? 0xf000 : 0;
68 vel |= (((s16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
70 len = sprintf(buf, "%d %c%c%c%c ", iattr->address ? pos : vel,
71 (status & 0x8) ? 'P' : 'V',
72 (status & 0x4) ? 'd' : '_',
73 (status & 0x2) ? 'l' : '_',
74 (status & 0x1) ? '1' : '0');
76 /* delay (2 * AD2S120X_TSCLK + 20) ns for sample pulse */
78 mutex_unlock(&st->lock);
80 return ret ? ret : len;
83 static IIO_DEVICE_ATTR(pos, S_IRUGO, ad2s120x_show_val, NULL, 1);
84 static IIO_DEVICE_ATTR(vel, S_IRUGO, ad2s120x_show_val, NULL, 0);
86 static struct attribute *ad2s120x_attributes[] = {
87 &iio_dev_attr_pos.dev_attr.attr,
88 &iio_dev_attr_vel.dev_attr.attr,
92 static const struct attribute_group ad2s120x_attribute_group = {
93 .attrs = ad2s120x_attributes,
96 static const struct iio_info ad2s120x_info = {
97 .attrs = &ad2s120x_attribute_group,
98 .driver_module = THIS_MODULE,
101 static int __devinit ad2s120x_probe(struct spi_device *spi)
103 struct ad2s120x_state *st;
104 struct iio_dev *indio_dev;
106 unsigned short *pins = spi->dev.platform_data;
108 for (pn = 0; pn < AD2S120X_PN; pn++)
109 if (gpio_request_one(pins[pn], GPIOF_DIR_OUT, DRV_NAME)) {
110 pr_err("%s: request gpio pin %d failed\n",
114 indio_dev = iio_allocate_device(sizeof(*st));
115 if (indio_dev == NULL) {
119 spi_set_drvdata(spi, indio_dev);
120 st = iio_priv(indio_dev);
121 mutex_init(&st->lock);
123 st->sample = pins[0];
126 indio_dev->dev.parent = &spi->dev;
127 indio_dev->info = &ad2s120x_info;
128 indio_dev->modes = INDIO_DIRECT_MODE;
130 ret = iio_device_register(indio_dev);
134 spi->max_speed_hz = AD2S120X_HZ;
135 spi->mode = SPI_MODE_3;
141 iio_free_device(indio_dev);
143 for (--pn; pn >= 0; pn--)
148 static int __devexit ad2s120x_remove(struct spi_device *spi)
150 iio_device_unregister(spi_get_drvdata(spi));
155 static struct spi_driver ad2s120x_driver = {
158 .owner = THIS_MODULE,
160 .probe = ad2s120x_probe,
161 .remove = __devexit_p(ad2s120x_remove),
164 static __init int ad2s120x_spi_init(void)
166 return spi_register_driver(&ad2s120x_driver);
168 module_init(ad2s120x_spi_init);
170 static __exit void ad2s120x_spi_exit(void)
172 spi_unregister_driver(&ad2s120x_driver);
174 module_exit(ad2s120x_spi_exit);
176 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
177 MODULE_DESCRIPTION("Analog Devices AD2S1200/1205 Resolver to Digital SPI driver");
178 MODULE_LICENSE("GPL v2");