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