ff1b5a82b3d62d022ccb2cb13b0a941dcaabee0c
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / gyro / adis16060_core.c
1 /*
2  * ADIS16060 Wide Bandwidth Yaw Rate Gyroscope with SPI driver
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/mutex.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/module.h>
18
19 #include "../iio.h"
20 #include "../sysfs.h"
21
22 #define ADIS16060_GYRO          0x20 /* Measure Angular Rate (Gyro) */
23 #define ADIS16060_TEMP_OUT      0x10 /* Measure Temperature */
24 #define ADIS16060_AIN2          0x80 /* Measure AIN2 */
25 #define ADIS16060_AIN1          0x40 /* Measure AIN1 */
26
27 /**
28  * struct adis16060_state - device instance specific data
29  * @us_w:               actual spi_device to write config
30  * @us_r:               actual spi_device to read back data
31  * @buf:                transmit or receive buffer
32  * @buf_lock:           mutex to protect tx and rx
33  **/
34 struct adis16060_state {
35         struct spi_device               *us_w;
36         struct spi_device               *us_r;
37         struct mutex                    buf_lock;
38
39         u8 buf[3] ____cacheline_aligned;
40 };
41
42 static struct iio_dev *adis16060_iio_dev;
43
44 static int adis16060_spi_write(struct iio_dev *indio_dev, u8 val)
45 {
46         int ret;
47         struct adis16060_state *st = iio_priv(indio_dev);
48
49         mutex_lock(&st->buf_lock);
50         st->buf[2] = val; /* The last 8 bits clocked in are latched */
51         ret = spi_write(st->us_w, st->buf, 3);
52         mutex_unlock(&st->buf_lock);
53
54         return ret;
55 }
56
57 static int adis16060_spi_read(struct iio_dev *indio_dev, u16 *val)
58 {
59         int ret;
60         struct adis16060_state *st = iio_priv(indio_dev);
61
62         mutex_lock(&st->buf_lock);
63
64         ret = spi_read(st->us_r, st->buf, 3);
65
66         /* The internal successive approximation ADC begins the
67          * conversion process on the falling edge of MSEL1 and
68          * starts to place data MSB first on the DOUT line at
69          * the 6th falling edge of SCLK
70          */
71         if (ret == 0)
72                 *val = ((st->buf[0] & 0x3) << 12) |
73                         (st->buf[1] << 4) |
74                         ((st->buf[2] >> 4) & 0xF);
75         mutex_unlock(&st->buf_lock);
76
77         return ret;
78 }
79
80 static int adis16060_read_raw(struct iio_dev *indio_dev,
81                               struct iio_chan_spec const *chan,
82                               int *val, int *val2,
83                               long mask)
84 {
85         u16 tval = 0;
86         int ret;
87
88         switch (mask) {
89         case 0:
90                 /* Take the iio_dev status lock */
91                 mutex_lock(&indio_dev->mlock);
92                 ret = adis16060_spi_write(indio_dev, chan->address);
93                 if (ret < 0) {
94                         mutex_unlock(&indio_dev->mlock);
95                         return ret;
96                 }
97                 ret = adis16060_spi_read(indio_dev, &tval);
98                 mutex_unlock(&indio_dev->mlock);
99                 *val = tval;
100                 return IIO_VAL_INT;
101         case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
102                 *val = -7;
103                 *val2 = 461117;
104                 return IIO_VAL_INT_PLUS_MICRO;
105         case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
106                 *val = 0;
107                 *val2 = 34000;
108                 return IIO_VAL_INT_PLUS_MICRO;
109         }
110
111         return -EINVAL;
112 }
113
114 static const struct iio_info adis16060_info = {
115         .read_raw = &adis16060_read_raw,
116         .driver_module = THIS_MODULE,
117 };
118
119 static const struct iio_chan_spec adis16060_channels[] = {
120         {
121                 .type = IIO_ANGL_VEL,
122                 .modified = 1,
123                 .channel2 = IIO_MOD_Z,
124                 .address = ADIS16060_GYRO,
125         }, {
126                 .type = IIO_VOLTAGE,
127                 .indexed = 1,
128                 .channel = 0,
129                 .address = ADIS16060_AIN1,
130         }, {
131                 .type = IIO_VOLTAGE,
132                 .indexed = 1,
133                 .channel = 1,
134                 .address = ADIS16060_AIN2,
135         }, {
136                 .type = IIO_TEMP,
137                 .indexed = 1,
138                 .channel = 0,
139                 .info_mask = (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
140                 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
141                 .address = ADIS16060_TEMP_OUT,
142         }
143 };
144
145 static int __devinit adis16060_r_probe(struct spi_device *spi)
146 {
147         int ret;
148         struct adis16060_state *st;
149         struct iio_dev *indio_dev;
150
151         /* setup the industrialio driver allocated elements */
152         indio_dev = iio_allocate_device(sizeof(*st));
153         if (indio_dev == NULL) {
154                 ret = -ENOMEM;
155                 goto error_ret;
156         }
157         /* this is only used for removal purposes */
158         spi_set_drvdata(spi, indio_dev);
159         st = iio_priv(indio_dev);
160         st->us_r = spi;
161         mutex_init(&st->buf_lock);
162
163         indio_dev->name = spi->dev.driver->name;
164         indio_dev->dev.parent = &spi->dev;
165         indio_dev->info = &adis16060_info;
166         indio_dev->modes = INDIO_DIRECT_MODE;
167         indio_dev->channels = adis16060_channels;
168         indio_dev->num_channels = ARRAY_SIZE(adis16060_channels);
169
170         ret = iio_device_register(indio_dev);
171         if (ret)
172                 goto error_free_dev;
173
174         adis16060_iio_dev = indio_dev;
175         return 0;
176
177 error_free_dev:
178         iio_free_device(indio_dev);
179 error_ret:
180         return ret;
181 }
182
183 /* fixme, confirm ordering in this function */
184 static int adis16060_r_remove(struct spi_device *spi)
185 {
186         iio_device_unregister(spi_get_drvdata(spi));
187         iio_free_device(spi_get_drvdata(spi));
188
189         return 0;
190 }
191
192 static int __devinit adis16060_w_probe(struct spi_device *spi)
193 {
194         int ret;
195         struct iio_dev *indio_dev = adis16060_iio_dev;
196         struct adis16060_state *st;
197         if (!indio_dev) {
198                 ret =  -ENODEV;
199                 goto error_ret;
200         }
201         st = iio_priv(indio_dev);
202         spi_set_drvdata(spi, indio_dev);
203         st->us_w = spi;
204         return 0;
205
206 error_ret:
207         return ret;
208 }
209
210 static int adis16060_w_remove(struct spi_device *spi)
211 {
212         return 0;
213 }
214
215 static struct spi_driver adis16060_r_driver = {
216         .driver = {
217                 .name = "adis16060_r",
218                 .owner = THIS_MODULE,
219         },
220         .probe = adis16060_r_probe,
221         .remove = __devexit_p(adis16060_r_remove),
222 };
223
224 static struct spi_driver adis16060_w_driver = {
225         .driver = {
226                 .name = "adis16060_w",
227                 .owner = THIS_MODULE,
228         },
229         .probe = adis16060_w_probe,
230         .remove = __devexit_p(adis16060_w_remove),
231 };
232
233 static __init int adis16060_init(void)
234 {
235         int ret;
236
237         ret = spi_register_driver(&adis16060_r_driver);
238         if (ret < 0)
239                 return ret;
240
241         ret = spi_register_driver(&adis16060_w_driver);
242         if (ret < 0) {
243                 spi_unregister_driver(&adis16060_r_driver);
244                 return ret;
245         }
246
247         return 0;
248 }
249 module_init(adis16060_init);
250
251 static __exit void adis16060_exit(void)
252 {
253         spi_unregister_driver(&adis16060_w_driver);
254         spi_unregister_driver(&adis16060_r_driver);
255 }
256 module_exit(adis16060_exit);
257
258 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
259 MODULE_DESCRIPTION("Analog Devices ADIS16060 Yaw Rate Gyroscope Driver");
260 MODULE_LICENSE("GPL v2");