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> | |
14f98326 | 16 | |
e071f6b8 BS |
17 | |
18 | #include "../iio.h" | |
19 | #include "../sysfs.h" | |
20 | #include "gyro.h" | |
21 | #include "../adc/adc.h" | |
22 | ||
14f98326 JC |
23 | #define ADIS16060_GYRO 0x20 /* Measure Angular Rate (Gyro) */ |
24 | #define ADIS16060_TEMP_OUT 0x10 /* Measure Temperature */ | |
25 | #define ADIS16060_AIN2 0x80 /* Measure AIN2 */ | |
26 | #define ADIS16060_AIN1 0x40 /* Measure AIN1 */ | |
27 | ||
28 | /** | |
29 | * struct adis16060_state - device instance specific data | |
30 | * @us_w: actual spi_device to write config | |
31 | * @us_r: actual spi_device to read back data | |
32 | * @indio_dev: industrial I/O device structure | |
25985edc | 33 | * @buf: transmit or receive buffer |
14f98326 JC |
34 | * @buf_lock: mutex to protect tx and rx |
35 | **/ | |
36 | struct adis16060_state { | |
37 | struct spi_device *us_w; | |
38 | struct spi_device *us_r; | |
39 | struct iio_dev *indio_dev; | |
40 | struct mutex buf_lock; | |
41 | ||
42 | u8 buf[3] ____cacheline_aligned; | |
43 | }; | |
e071f6b8 | 44 | |
d14ae859 | 45 | static struct adis16060_state *adis16060_st; |
e071f6b8 | 46 | |
d14ae859 | 47 | static int adis16060_spi_write(struct device *dev, |
e071f6b8 BS |
48 | u8 val) |
49 | { | |
50 | int ret; | |
51 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
52 | struct adis16060_state *st = iio_dev_get_devdata(indio_dev); | |
53 | ||
54 | mutex_lock(&st->buf_lock); | |
14f98326 JC |
55 | st->buf[2] = val; /* The last 8 bits clocked in are latched */ |
56 | ret = spi_write(st->us_w, st->buf, 3); | |
e071f6b8 BS |
57 | mutex_unlock(&st->buf_lock); |
58 | ||
59 | return ret; | |
60 | } | |
61 | ||
d14ae859 | 62 | static int adis16060_spi_read(struct device *dev, |
e071f6b8 BS |
63 | u16 *val) |
64 | { | |
65 | int ret; | |
66 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
67 | struct adis16060_state *st = iio_dev_get_devdata(indio_dev); | |
68 | ||
69 | mutex_lock(&st->buf_lock); | |
70 | ||
14f98326 | 71 | ret = spi_read(st->us_r, st->buf, 3); |
e071f6b8 | 72 | |
d14ae859 JC |
73 | /* The internal successive approximation ADC begins the |
74 | * conversion process on the falling edge of MSEL1 and | |
75 | * starts to place data MSB first on the DOUT line at | |
76 | * the 6th falling edge of SCLK | |
e071f6b8 BS |
77 | */ |
78 | if (ret == 0) | |
14f98326 JC |
79 | *val = ((st->buf[0] & 0x3) << 12) | |
80 | (st->buf[1] << 4) | | |
81 | ((st->buf[2] >> 4) & 0xF); | |
e071f6b8 BS |
82 | mutex_unlock(&st->buf_lock); |
83 | ||
84 | return ret; | |
85 | } | |
86 | ||
87 | static ssize_t adis16060_read(struct device *dev, | |
88 | struct device_attribute *attr, | |
89 | char *buf) | |
90 | { | |
14f98326 | 91 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
e071f6b8 | 92 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
d14ae859 | 93 | u16 val = 0; |
e071f6b8 BS |
94 | ssize_t ret; |
95 | ||
96 | /* Take the iio_dev status lock */ | |
97 | mutex_lock(&indio_dev->mlock); | |
14f98326 JC |
98 | |
99 | ret = adis16060_spi_write(dev, this_attr->address); | |
100 | if (ret < 0) | |
101 | goto error_ret; | |
102 | ret = adis16060_spi_read(dev, &val); | |
103 | error_ret: | |
e071f6b8 BS |
104 | mutex_unlock(&indio_dev->mlock); |
105 | ||
106 | if (ret == 0) | |
107 | return sprintf(buf, "%d\n", val); | |
108 | else | |
109 | return ret; | |
110 | } | |
111 | ||
14f98326 JC |
112 | static IIO_DEV_ATTR_GYRO_Z(adis16060_read, ADIS16060_GYRO); |
113 | static IIO_DEVICE_ATTR(temp_raw, S_IRUGO, adis16060_read, NULL, | |
114 | ADIS16060_TEMP_OUT); | |
115 | static IIO_CONST_ATTR_TEMP_SCALE("34"); /* Milli degrees C */ | |
116 | static IIO_CONST_ATTR_TEMP_OFFSET("-7461.117"); /* Milli degrees C */ | |
117 | static IIO_DEV_ATTR_IN_RAW(0, adis16060_read, ADIS16060_AIN1); | |
118 | static IIO_DEV_ATTR_IN_RAW(1, adis16060_read, ADIS16060_AIN2); | |
e071f6b8 BS |
119 | static IIO_CONST_ATTR(name, "adis16060"); |
120 | ||
e071f6b8 | 121 | static struct attribute *adis16060_attributes[] = { |
14f98326 JC |
122 | &iio_dev_attr_gyro_z_raw.dev_attr.attr, |
123 | &iio_dev_attr_temp_raw.dev_attr.attr, | |
124 | &iio_const_attr_temp_scale.dev_attr.attr, | |
125 | &iio_const_attr_temp_offset.dev_attr.attr, | |
126 | &iio_dev_attr_in0_raw.dev_attr.attr, | |
127 | &iio_dev_attr_in1_raw.dev_attr.attr, | |
e071f6b8 BS |
128 | &iio_const_attr_name.dev_attr.attr, |
129 | NULL | |
130 | }; | |
131 | ||
132 | static const struct attribute_group adis16060_attribute_group = { | |
133 | .attrs = adis16060_attributes, | |
134 | }; | |
135 | ||
136 | static int __devinit adis16060_r_probe(struct spi_device *spi) | |
137 | { | |
138 | int ret, regdone = 0; | |
139 | struct adis16060_state *st = kzalloc(sizeof *st, GFP_KERNEL); | |
140 | if (!st) { | |
141 | ret = -ENOMEM; | |
142 | goto error_ret; | |
143 | } | |
144 | /* this is only used for removal purposes */ | |
145 | spi_set_drvdata(spi, st); | |
146 | ||
e071f6b8 BS |
147 | st->us_r = spi; |
148 | mutex_init(&st->buf_lock); | |
149 | /* setup the industrialio driver allocated elements */ | |
150 | st->indio_dev = iio_allocate_device(); | |
151 | if (st->indio_dev == NULL) { | |
152 | ret = -ENOMEM; | |
14f98326 | 153 | goto error_free_st; |
e071f6b8 BS |
154 | } |
155 | ||
156 | st->indio_dev->dev.parent = &spi->dev; | |
e071f6b8 BS |
157 | st->indio_dev->attrs = &adis16060_attribute_group; |
158 | st->indio_dev->dev_data = (void *)(st); | |
159 | st->indio_dev->driver_module = THIS_MODULE; | |
160 | st->indio_dev->modes = INDIO_DIRECT_MODE; | |
161 | ||
e071f6b8 BS |
162 | ret = iio_device_register(st->indio_dev); |
163 | if (ret) | |
d14ae859 | 164 | goto error_free_dev; |
e071f6b8 BS |
165 | regdone = 1; |
166 | ||
e071f6b8 BS |
167 | adis16060_st = st; |
168 | return 0; | |
169 | ||
e071f6b8 BS |
170 | error_free_dev: |
171 | if (regdone) | |
172 | iio_device_unregister(st->indio_dev); | |
173 | else | |
174 | iio_free_device(st->indio_dev); | |
e071f6b8 BS |
175 | error_free_st: |
176 | kfree(st); | |
177 | error_ret: | |
178 | return ret; | |
179 | } | |
180 | ||
181 | /* fixme, confirm ordering in this function */ | |
182 | static int adis16060_r_remove(struct spi_device *spi) | |
183 | { | |
184 | struct adis16060_state *st = spi_get_drvdata(spi); | |
185 | struct iio_dev *indio_dev = st->indio_dev; | |
186 | ||
e071f6b8 | 187 | iio_device_unregister(indio_dev); |
e071f6b8 BS |
188 | kfree(st); |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
193 | static int __devinit adis16060_w_probe(struct spi_device *spi) | |
194 | { | |
195 | int ret; | |
196 | struct adis16060_state *st = adis16060_st; | |
197 | if (!st) { | |
198 | ret = -ENODEV; | |
199 | goto error_ret; | |
200 | } | |
201 | spi_set_drvdata(spi, st); | |
202 | st->us_w = spi; | |
203 | return 0; | |
204 | ||
205 | error_ret: | |
206 | return ret; | |
207 | } | |
208 | ||
209 | static int adis16060_w_remove(struct spi_device *spi) | |
210 | { | |
211 | return 0; | |
212 | } | |
213 | ||
214 | static struct spi_driver adis16060_r_driver = { | |
215 | .driver = { | |
216 | .name = "adis16060_r", | |
217 | .owner = THIS_MODULE, | |
218 | }, | |
219 | .probe = adis16060_r_probe, | |
220 | .remove = __devexit_p(adis16060_r_remove), | |
221 | }; | |
222 | ||
223 | static struct spi_driver adis16060_w_driver = { | |
224 | .driver = { | |
225 | .name = "adis16060_w", | |
226 | .owner = THIS_MODULE, | |
227 | }, | |
228 | .probe = adis16060_w_probe, | |
229 | .remove = __devexit_p(adis16060_w_remove), | |
230 | }; | |
231 | ||
232 | static __init int adis16060_init(void) | |
233 | { | |
234 | int ret; | |
235 | ||
236 | ret = spi_register_driver(&adis16060_r_driver); | |
237 | if (ret < 0) | |
238 | return ret; | |
239 | ||
240 | ret = spi_register_driver(&adis16060_w_driver); | |
241 | if (ret < 0) { | |
242 | spi_unregister_driver(&adis16060_r_driver); | |
243 | return ret; | |
244 | } | |
245 | ||
246 | return 0; | |
247 | } | |
248 | module_init(adis16060_init); | |
249 | ||
250 | static __exit void adis16060_exit(void) | |
251 | { | |
252 | spi_unregister_driver(&adis16060_w_driver); | |
253 | spi_unregister_driver(&adis16060_r_driver); | |
254 | } | |
255 | module_exit(adis16060_exit); | |
256 | ||
257 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | |
d14ae859 | 258 | MODULE_DESCRIPTION("Analog Devices ADIS16060 Yaw Rate Gyroscope Driver"); |
e071f6b8 | 259 | MODULE_LICENSE("GPL v2"); |