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