Commit | Line | Data |
---|---|---|
e071f6b8 BS |
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 | ||
45296236 | 9 | #include <linux/module.h> |
e071f6b8 BS |
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> | |
99c97852 | 17 | #include <linux/module.h> |
14f98326 | 18 | |
e071f6b8 BS |
19 | #include "../iio.h" |
20 | #include "../sysfs.h" | |
e071f6b8 | 21 | |
14f98326 JC |
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 | |
25985edc | 31 | * @buf: transmit or receive buffer |
14f98326 JC |
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; | |
14f98326 JC |
37 | struct mutex buf_lock; |
38 | ||
39 | u8 buf[3] ____cacheline_aligned; | |
40 | }; | |
e071f6b8 | 41 | |
3a5952f9 | 42 | static struct iio_dev *adis16060_iio_dev; |
e071f6b8 | 43 | |
4f2ca080 | 44 | static int adis16060_spi_write(struct iio_dev *indio_dev, u8 val) |
e071f6b8 BS |
45 | { |
46 | int ret; | |
3a5952f9 | 47 | struct adis16060_state *st = iio_priv(indio_dev); |
e071f6b8 BS |
48 | |
49 | mutex_lock(&st->buf_lock); | |
14f98326 JC |
50 | st->buf[2] = val; /* The last 8 bits clocked in are latched */ |
51 | ret = spi_write(st->us_w, st->buf, 3); | |
e071f6b8 BS |
52 | mutex_unlock(&st->buf_lock); |
53 | ||
54 | return ret; | |
55 | } | |
56 | ||
4f2ca080 | 57 | static int adis16060_spi_read(struct iio_dev *indio_dev, u16 *val) |
e071f6b8 BS |
58 | { |
59 | int ret; | |
3a5952f9 | 60 | struct adis16060_state *st = iio_priv(indio_dev); |
e071f6b8 BS |
61 | |
62 | mutex_lock(&st->buf_lock); | |
63 | ||
14f98326 | 64 | ret = spi_read(st->us_r, st->buf, 3); |
e071f6b8 | 65 | |
d14ae859 JC |
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 | |
e071f6b8 BS |
70 | */ |
71 | if (ret == 0) | |
14f98326 JC |
72 | *val = ((st->buf[0] & 0x3) << 12) | |
73 | (st->buf[1] << 4) | | |
74 | ((st->buf[2] >> 4) & 0xF); | |
e071f6b8 BS |
75 | mutex_unlock(&st->buf_lock); |
76 | ||
77 | return ret; | |
78 | } | |
79 | ||
4f2ca080 JC |
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) | |
e071f6b8 | 84 | { |
4f2ca080 JC |
85 | u16 tval = 0; |
86 | int ret; | |
14f98326 | 87 | |
4f2ca080 JC |
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; | |
c8a9f805 | 101 | case IIO_CHAN_INFO_OFFSET: |
4f2ca080 JC |
102 | *val = -7; |
103 | *val2 = 461117; | |
104 | return IIO_VAL_INT_PLUS_MICRO; | |
c8a9f805 | 105 | case IIO_CHAN_INFO_SCALE: |
4f2ca080 JC |
106 | *val = 0; |
107 | *val2 = 34000; | |
108 | return IIO_VAL_INT_PLUS_MICRO; | |
109 | } | |
e071f6b8 | 110 | |
4f2ca080 | 111 | return -EINVAL; |
e071f6b8 BS |
112 | } |
113 | ||
6fe8135f | 114 | static const struct iio_info adis16060_info = { |
4f2ca080 | 115 | .read_raw = &adis16060_read_raw, |
6fe8135f JC |
116 | .driver_module = THIS_MODULE, |
117 | }; | |
118 | ||
4f2ca080 JC |
119 | static const struct iio_chan_spec adis16060_channels[] = { |
120 | { | |
41ea040c | 121 | .type = IIO_ANGL_VEL, |
4f2ca080 JC |
122 | .modified = 1, |
123 | .channel2 = IIO_MOD_Z, | |
124 | .address = ADIS16060_GYRO, | |
125 | }, { | |
6835cb6b | 126 | .type = IIO_VOLTAGE, |
4f2ca080 JC |
127 | .indexed = 1, |
128 | .channel = 0, | |
129 | .address = ADIS16060_AIN1, | |
130 | }, { | |
6835cb6b | 131 | .type = IIO_VOLTAGE, |
4f2ca080 JC |
132 | .indexed = 1, |
133 | .channel = 1, | |
134 | .address = ADIS16060_AIN2, | |
135 | }, { | |
136 | .type = IIO_TEMP, | |
137 | .indexed = 1, | |
138 | .channel = 0, | |
c8a9f805 JC |
139 | .info_mask = IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | |
140 | IIO_CHAN_INFO_SCALE_SEPARATE_BIT, | |
4f2ca080 JC |
141 | .address = ADIS16060_TEMP_OUT, |
142 | } | |
143 | }; | |
144 | ||
e071f6b8 BS |
145 | static int __devinit adis16060_r_probe(struct spi_device *spi) |
146 | { | |
26d25ae3 | 147 | int ret; |
3a5952f9 JC |
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; | |
e071f6b8 BS |
155 | goto error_ret; |
156 | } | |
157 | /* this is only used for removal purposes */ | |
3a5952f9 JC |
158 | spi_set_drvdata(spi, indio_dev); |
159 | st = iio_priv(indio_dev); | |
e071f6b8 BS |
160 | st->us_r = spi; |
161 | mutex_init(&st->buf_lock); | |
e071f6b8 | 162 | |
4f2ca080 | 163 | indio_dev->name = spi->dev.driver->name; |
3a5952f9 JC |
164 | indio_dev->dev.parent = &spi->dev; |
165 | indio_dev->info = &adis16060_info; | |
166 | indio_dev->modes = INDIO_DIRECT_MODE; | |
4f2ca080 JC |
167 | indio_dev->channels = adis16060_channels; |
168 | indio_dev->num_channels = ARRAY_SIZE(adis16060_channels); | |
e071f6b8 | 169 | |
3a5952f9 | 170 | ret = iio_device_register(indio_dev); |
e071f6b8 | 171 | if (ret) |
d14ae859 | 172 | goto error_free_dev; |
e071f6b8 | 173 | |
3a5952f9 | 174 | adis16060_iio_dev = indio_dev; |
e071f6b8 BS |
175 | return 0; |
176 | ||
e071f6b8 | 177 | error_free_dev: |
26d25ae3 | 178 | iio_free_device(indio_dev); |
e071f6b8 BS |
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 | { | |
3a5952f9 | 186 | iio_device_unregister(spi_get_drvdata(spi)); |
d2fffd6c | 187 | iio_free_device(spi_get_drvdata(spi)); |
e071f6b8 BS |
188 | |
189 | return 0; | |
190 | } | |
191 | ||
192 | static int __devinit adis16060_w_probe(struct spi_device *spi) | |
193 | { | |
194 | int ret; | |
3a5952f9 JC |
195 | struct iio_dev *indio_dev = adis16060_iio_dev; |
196 | struct adis16060_state *st; | |
197 | if (!indio_dev) { | |
e071f6b8 BS |
198 | ret = -ENODEV; |
199 | goto error_ret; | |
200 | } | |
3a5952f9 JC |
201 | st = iio_priv(indio_dev); |
202 | spi_set_drvdata(spi, indio_dev); | |
e071f6b8 BS |
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>"); | |
d14ae859 | 259 | MODULE_DESCRIPTION("Analog Devices ADIS16060 Yaw Rate Gyroscope Driver"); |
e071f6b8 | 260 | MODULE_LICENSE("GPL v2"); |