staging:iio:resolver:ad2s120x chan spec conversion
[pandora-kernel.git] / drivers / staging / iio / resolver / ad2s120x.c
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 #include <linux/module.h>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23
24 #define DRV_NAME "ad2s120x"
25
26 /* input pin sample and rdvel is controlled by driver */
27 #define AD2S120X_PN     2
28
29 /* input clock on serial interface */
30 #define AD2S120X_HZ     8192000
31 /* clock period in nano second */
32 #define AD2S120X_TSCLK  (1000000000/AD2S120X_HZ)
33
34 struct ad2s120x_state {
35         struct mutex lock;
36         struct spi_device *sdev;
37         int sample;
38         int rdvel;
39         u8 rx[2] ____cacheline_aligned;
40 };
41
42 static int ad2s1200_read_raw(struct iio_dev *indio_dev,
43                            struct iio_chan_spec const *chan,
44                            int *val,
45                            int *val2,
46                            long m)
47 {
48         int ret = 0;
49         s16 vel;
50         struct ad2s120x_state *st = iio_priv(indio_dev);
51
52         mutex_lock(&st->lock);
53         gpio_set_value(st->sample, 0);
54         /* delay (6 * AD2S120X_TSCLK + 20) nano seconds */
55         udelay(1);
56         gpio_set_value(st->sample, 1);
57         gpio_set_value(st->rdvel, !!(chan->type == IIO_ANGL));
58         ret = spi_read(st->sdev, st->rx, 2);
59         if (ret < 0) {
60                 mutex_unlock(&st->lock);
61                 return ret;
62         }
63
64         switch (chan->type) {
65         case IIO_ANGL:
66                 *val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
67                 break;
68         case IIO_ANGL_VEL:
69                 vel = (((s16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
70                 vel = (vel << 4) >> 4;
71                 *val = vel;
72         default:
73                 mutex_unlock(&st->lock);
74                 return -EINVAL;
75         }
76         /* delay (2 * AD2S120X_TSCLK + 20) ns for sample pulse */
77         udelay(1);
78         mutex_unlock(&st->lock);
79         return IIO_VAL_INT;
80 }
81
82 static const struct iio_chan_spec ad2s1200_channels[] = {
83         {
84                 .type = IIO_ANGL,
85                 .indexed = 1,
86                 .channel = 0,
87         }, {
88                 .type = IIO_ANGL_VEL,
89                 .indexed = 1,
90                 .channel = 0,
91         }
92 };
93
94 static const struct iio_info ad2s120x_info = {
95         .read_raw = &ad2s1200_read_raw,
96         .driver_module = THIS_MODULE,
97 };
98
99 static int __devinit ad2s120x_probe(struct spi_device *spi)
100 {
101         struct ad2s120x_state *st;
102         struct iio_dev *indio_dev;
103         int pn, ret = 0;
104         unsigned short *pins = spi->dev.platform_data;
105
106         for (pn = 0; pn < AD2S120X_PN; pn++)
107                 if (gpio_request_one(pins[pn], GPIOF_DIR_OUT, DRV_NAME)) {
108                         pr_err("%s: request gpio pin %d failed\n",
109                                                 DRV_NAME, pins[pn]);
110                         goto error_ret;
111                 }
112         indio_dev = iio_allocate_device(sizeof(*st));
113         if (indio_dev == NULL) {
114                 ret = -ENOMEM;
115                 goto error_ret;
116         }
117         spi_set_drvdata(spi, indio_dev);
118         st = iio_priv(indio_dev);
119         mutex_init(&st->lock);
120         st->sdev = spi;
121         st->sample = pins[0];
122         st->rdvel = pins[1];
123
124         indio_dev->dev.parent = &spi->dev;
125         indio_dev->info = &ad2s120x_info;
126         indio_dev->modes = INDIO_DIRECT_MODE;
127         indio_dev->channels = ad2s1200_channels;
128         indio_dev->num_channels = ARRAY_SIZE(ad2s1200_channels);
129
130         ret = iio_device_register(indio_dev);
131         if (ret)
132                 goto error_free_dev;
133
134         spi->max_speed_hz = AD2S120X_HZ;
135         spi->mode = SPI_MODE_3;
136         spi_setup(spi);
137
138         return 0;
139
140 error_free_dev:
141         iio_free_device(indio_dev);
142 error_ret:
143         for (--pn; pn >= 0; pn--)
144                 gpio_free(pins[pn]);
145         return ret;
146 }
147
148 static int __devexit ad2s120x_remove(struct spi_device *spi)
149 {
150         iio_device_unregister(spi_get_drvdata(spi));
151
152         return 0;
153 }
154
155 static struct spi_driver ad2s120x_driver = {
156         .driver = {
157                 .name = DRV_NAME,
158                 .owner = THIS_MODULE,
159         },
160         .probe = ad2s120x_probe,
161         .remove = __devexit_p(ad2s120x_remove),
162 };
163
164 static __init int ad2s120x_spi_init(void)
165 {
166         return spi_register_driver(&ad2s120x_driver);
167 }
168 module_init(ad2s120x_spi_init);
169
170 static __exit void ad2s120x_spi_exit(void)
171 {
172         spi_unregister_driver(&ad2s120x_driver);
173 }
174 module_exit(ad2s120x_spi_exit);
175
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");