Commit | Line | Data |
---|---|---|
089a4198 | 1 | /* |
f733d02a | 2 | * ADIS16260/ADIS16265 Programmable Digital Gyroscope Sensor Driver |
089a4198 BS |
3 | * |
4 | * Copyright 2010 Analog Devices Inc. | |
5 | * | |
6 | * Licensed under the GPL-2 or later. | |
7 | */ | |
8 | ||
9 | #include <linux/interrupt.h> | |
10 | #include <linux/irq.h> | |
11 | #include <linux/gpio.h> | |
12 | #include <linux/delay.h> | |
13 | #include <linux/mutex.h> | |
14 | #include <linux/device.h> | |
15 | #include <linux/kernel.h> | |
16 | #include <linux/spi/spi.h> | |
1cb6c1f5 | 17 | #include <linux/slab.h> |
089a4198 BS |
18 | #include <linux/sysfs.h> |
19 | #include <linux/list.h> | |
20 | ||
21 | #include "../iio.h" | |
22 | #include "../sysfs.h" | |
2662051e | 23 | #include "../ring_generic.h" |
089a4198 BS |
24 | #include "../adc/adc.h" |
25 | #include "gyro.h" | |
26 | ||
27 | #include "adis16260.h" | |
28 | ||
29 | #define DRIVER_NAME "adis16260" | |
30 | ||
31 | static int adis16260_check_status(struct device *dev); | |
32 | ||
33 | /** | |
34 | * adis16260_spi_write_reg_8() - write single byte to a register | |
35 | * @dev: device associated with child of actual device (iio_dev or iio_trig) | |
36 | * @reg_address: the address of the register to be written | |
37 | * @val: the value to write | |
38 | **/ | |
39 | static int adis16260_spi_write_reg_8(struct device *dev, | |
40 | u8 reg_address, | |
41 | u8 val) | |
42 | { | |
43 | int ret; | |
44 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
45 | struct adis16260_state *st = iio_dev_get_devdata(indio_dev); | |
46 | ||
47 | mutex_lock(&st->buf_lock); | |
48 | st->tx[0] = ADIS16260_WRITE_REG(reg_address); | |
49 | st->tx[1] = val; | |
50 | ||
51 | ret = spi_write(st->us, st->tx, 2); | |
52 | mutex_unlock(&st->buf_lock); | |
53 | ||
54 | return ret; | |
55 | } | |
56 | ||
57 | /** | |
58 | * adis16260_spi_write_reg_16() - write 2 bytes to a pair of registers | |
59 | * @dev: device associated with child of actual device (iio_dev or iio_trig) | |
60 | * @reg_address: the address of the lower of the two registers. Second register | |
61 | * is assumed to have address one greater. | |
62 | * @val: value to be written | |
63 | **/ | |
64 | static int adis16260_spi_write_reg_16(struct device *dev, | |
65 | u8 lower_reg_address, | |
66 | u16 value) | |
67 | { | |
68 | int ret; | |
69 | struct spi_message msg; | |
70 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
71 | struct adis16260_state *st = iio_dev_get_devdata(indio_dev); | |
72 | struct spi_transfer xfers[] = { | |
73 | { | |
74 | .tx_buf = st->tx, | |
75 | .bits_per_word = 8, | |
76 | .len = 2, | |
77 | .cs_change = 1, | |
78 | .delay_usecs = 20, | |
79 | }, { | |
80 | .tx_buf = st->tx + 2, | |
81 | .bits_per_word = 8, | |
82 | .len = 2, | |
83 | .cs_change = 1, | |
84 | .delay_usecs = 20, | |
85 | }, | |
86 | }; | |
87 | ||
88 | mutex_lock(&st->buf_lock); | |
89 | st->tx[0] = ADIS16260_WRITE_REG(lower_reg_address); | |
90 | st->tx[1] = value & 0xFF; | |
91 | st->tx[2] = ADIS16260_WRITE_REG(lower_reg_address + 1); | |
92 | st->tx[3] = (value >> 8) & 0xFF; | |
93 | ||
94 | spi_message_init(&msg); | |
95 | spi_message_add_tail(&xfers[0], &msg); | |
96 | spi_message_add_tail(&xfers[1], &msg); | |
97 | ret = spi_sync(st->us, &msg); | |
98 | mutex_unlock(&st->buf_lock); | |
99 | ||
100 | return ret; | |
101 | } | |
102 | ||
103 | /** | |
104 | * adis16260_spi_read_reg_16() - read 2 bytes from a 16-bit register | |
105 | * @dev: device associated with child of actual device (iio_dev or iio_trig) | |
106 | * @reg_address: the address of the lower of the two registers. Second register | |
107 | * is assumed to have address one greater. | |
108 | * @val: somewhere to pass back the value read | |
109 | **/ | |
110 | static int adis16260_spi_read_reg_16(struct device *dev, | |
111 | u8 lower_reg_address, | |
112 | u16 *val) | |
113 | { | |
114 | struct spi_message msg; | |
115 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
116 | struct adis16260_state *st = iio_dev_get_devdata(indio_dev); | |
117 | int ret; | |
118 | struct spi_transfer xfers[] = { | |
119 | { | |
120 | .tx_buf = st->tx, | |
121 | .bits_per_word = 8, | |
122 | .len = 2, | |
123 | .cs_change = 1, | |
124 | .delay_usecs = 30, | |
125 | }, { | |
126 | .rx_buf = st->rx, | |
127 | .bits_per_word = 8, | |
128 | .len = 2, | |
129 | .cs_change = 1, | |
130 | .delay_usecs = 30, | |
131 | }, | |
132 | }; | |
133 | ||
134 | mutex_lock(&st->buf_lock); | |
135 | st->tx[0] = ADIS16260_READ_REG(lower_reg_address); | |
136 | st->tx[1] = 0; | |
089a4198 BS |
137 | |
138 | spi_message_init(&msg); | |
139 | spi_message_add_tail(&xfers[0], &msg); | |
140 | spi_message_add_tail(&xfers[1], &msg); | |
141 | ret = spi_sync(st->us, &msg); | |
142 | if (ret) { | |
143 | dev_err(&st->us->dev, | |
144 | "problem when reading 16 bit register 0x%02X", | |
145 | lower_reg_address); | |
146 | goto error_ret; | |
147 | } | |
148 | *val = (st->rx[0] << 8) | st->rx[1]; | |
149 | ||
150 | error_ret: | |
151 | mutex_unlock(&st->buf_lock); | |
152 | return ret; | |
153 | } | |
154 | ||
155 | static ssize_t adis16260_spi_read_signed(struct device *dev, | |
156 | struct device_attribute *attr, | |
157 | char *buf, | |
158 | unsigned bits) | |
159 | { | |
160 | int ret; | |
161 | s16 val = 0; | |
162 | unsigned shift = 16 - bits; | |
163 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
164 | ||
165 | ret = adis16260_spi_read_reg_16(dev, this_attr->address, (u16 *)&val); | |
166 | if (ret) | |
167 | return ret; | |
168 | ||
169 | if (val & ADIS16260_ERROR_ACTIVE) | |
170 | adis16260_check_status(dev); | |
171 | val = ((s16)(val << shift) >> shift); | |
172 | return sprintf(buf, "%d\n", val); | |
173 | } | |
174 | ||
175 | static ssize_t adis16260_read_12bit_unsigned(struct device *dev, | |
176 | struct device_attribute *attr, | |
177 | char *buf) | |
178 | { | |
179 | int ret; | |
180 | u16 val = 0; | |
181 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
182 | ||
183 | ret = adis16260_spi_read_reg_16(dev, this_attr->address, &val); | |
184 | if (ret) | |
185 | return ret; | |
186 | ||
187 | if (val & ADIS16260_ERROR_ACTIVE) | |
188 | adis16260_check_status(dev); | |
189 | ||
190 | return sprintf(buf, "%u\n", val & 0x0FFF); | |
191 | } | |
192 | ||
193 | static ssize_t adis16260_read_12bit_signed(struct device *dev, | |
194 | struct device_attribute *attr, | |
195 | char *buf) | |
196 | { | |
197 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
198 | ssize_t ret; | |
199 | ||
200 | /* Take the iio_dev status lock */ | |
201 | mutex_lock(&indio_dev->mlock); | |
202 | ret = adis16260_spi_read_signed(dev, attr, buf, 12); | |
203 | mutex_unlock(&indio_dev->mlock); | |
204 | ||
205 | return ret; | |
206 | } | |
207 | ||
208 | static ssize_t adis16260_read_14bit_signed(struct device *dev, | |
209 | struct device_attribute *attr, | |
210 | char *buf) | |
211 | { | |
212 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
213 | ssize_t ret; | |
214 | ||
215 | /* Take the iio_dev status lock */ | |
216 | mutex_lock(&indio_dev->mlock); | |
217 | ret = adis16260_spi_read_signed(dev, attr, buf, 14); | |
218 | mutex_unlock(&indio_dev->mlock); | |
219 | ||
220 | return ret; | |
221 | } | |
222 | ||
223 | static ssize_t adis16260_write_16bit(struct device *dev, | |
224 | struct device_attribute *attr, | |
225 | const char *buf, | |
226 | size_t len) | |
227 | { | |
228 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
229 | int ret; | |
230 | long val; | |
231 | ||
232 | ret = strict_strtol(buf, 10, &val); | |
233 | if (ret) | |
234 | goto error_ret; | |
235 | ret = adis16260_spi_write_reg_16(dev, this_attr->address, val); | |
236 | ||
237 | error_ret: | |
238 | return ret ? ret : len; | |
239 | } | |
240 | ||
03d1b7d3 JC |
241 | static ssize_t adis16260_read_frequency_available(struct device *dev, |
242 | struct device_attribute *attr, | |
243 | char *buf) | |
244 | { | |
245 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
246 | struct adis16260_state *st = iio_dev_get_devdata(indio_dev); | |
247 | if (spi_get_device_id(st->us)->driver_data) | |
248 | return sprintf(buf, "%s\n", "0.129 ~ 256"); | |
249 | else | |
250 | return sprintf(buf, "%s\n", "256 2048"); | |
251 | } | |
252 | ||
089a4198 BS |
253 | static ssize_t adis16260_read_frequency(struct device *dev, |
254 | struct device_attribute *attr, | |
255 | char *buf) | |
256 | { | |
03d1b7d3 JC |
257 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
258 | struct adis16260_state *st = iio_dev_get_devdata(indio_dev); | |
089a4198 BS |
259 | int ret, len = 0; |
260 | u16 t; | |
261 | int sps; | |
262 | ret = adis16260_spi_read_reg_16(dev, | |
263 | ADIS16260_SMPL_PRD, | |
264 | &t); | |
265 | if (ret) | |
266 | return ret; | |
03d1b7d3 JC |
267 | |
268 | if (spi_get_device_id(st->us)->driver_data) /* If an adis16251 */ | |
269 | sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 8 : 256; | |
270 | else | |
271 | sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048; | |
089a4198 BS |
272 | sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1; |
273 | len = sprintf(buf, "%d SPS\n", sps); | |
274 | return len; | |
275 | } | |
276 | ||
277 | static ssize_t adis16260_write_frequency(struct device *dev, | |
278 | struct device_attribute *attr, | |
279 | const char *buf, | |
280 | size_t len) | |
281 | { | |
282 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
283 | struct adis16260_state *st = iio_dev_get_devdata(indio_dev); | |
284 | long val; | |
285 | int ret; | |
286 | u8 t; | |
287 | ||
288 | ret = strict_strtol(buf, 10, &val); | |
289 | if (ret) | |
290 | return ret; | |
291 | ||
292 | mutex_lock(&indio_dev->mlock); | |
03d1b7d3 JC |
293 | if (spi_get_device_id(st->us)) { |
294 | t = (256 / val); | |
295 | if (t > 0) | |
296 | t--; | |
297 | t &= ADIS16260_SMPL_PRD_DIV_MASK; | |
298 | } else { | |
299 | t = (2048 / val); | |
300 | if (t > 0) | |
301 | t--; | |
302 | t &= ADIS16260_SMPL_PRD_DIV_MASK; | |
303 | } | |
089a4198 BS |
304 | if ((t & ADIS16260_SMPL_PRD_DIV_MASK) >= 0x0A) |
305 | st->us->max_speed_hz = ADIS16260_SPI_SLOW; | |
306 | else | |
307 | st->us->max_speed_hz = ADIS16260_SPI_FAST; | |
089a4198 BS |
308 | ret = adis16260_spi_write_reg_8(dev, |
309 | ADIS16260_SMPL_PRD, | |
310 | t); | |
311 | ||
312 | mutex_unlock(&indio_dev->mlock); | |
313 | ||
314 | return ret ? ret : len; | |
315 | } | |
316 | ||
fe346048 JC |
317 | static ssize_t adis16260_read_gyro_scale(struct device *dev, |
318 | struct device_attribute *attr, | |
319 | char *buf) | |
320 | { | |
321 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
322 | struct adis16260_state *st = iio_dev_get_devdata(indio_dev); | |
323 | ssize_t ret = 0; | |
324 | ||
325 | if (st->negate) | |
326 | ret = sprintf(buf, "-"); | |
327 | /* Take the iio_dev status lock */ | |
03d1b7d3 JC |
328 | if (spi_get_device_id(st->us)->driver_data) |
329 | ret += sprintf(buf + ret, "%s\n", "0.00031974432"); | |
330 | else | |
331 | ret += sprintf(buf + ret, "%s\n", "0.00127862821"); | |
fe346048 JC |
332 | |
333 | return ret; | |
334 | } | |
335 | ||
089a4198 BS |
336 | static int adis16260_reset(struct device *dev) |
337 | { | |
338 | int ret; | |
339 | ret = adis16260_spi_write_reg_8(dev, | |
340 | ADIS16260_GLOB_CMD, | |
341 | ADIS16260_GLOB_CMD_SW_RESET); | |
342 | if (ret) | |
343 | dev_err(dev, "problem resetting device"); | |
344 | ||
345 | return ret; | |
346 | } | |
347 | ||
348 | static ssize_t adis16260_write_reset(struct device *dev, | |
349 | struct device_attribute *attr, | |
350 | const char *buf, size_t len) | |
351 | { | |
352 | if (len < 1) | |
353 | return -EINVAL; | |
354 | switch (buf[0]) { | |
355 | case '1': | |
356 | case 'y': | |
357 | case 'Y': | |
358 | return adis16260_reset(dev); | |
359 | } | |
360 | return -EINVAL; | |
361 | } | |
362 | ||
363 | int adis16260_set_irq(struct device *dev, bool enable) | |
364 | { | |
365 | int ret; | |
366 | u16 msc; | |
367 | ret = adis16260_spi_read_reg_16(dev, ADIS16260_MSC_CTRL, &msc); | |
368 | if (ret) | |
369 | goto error_ret; | |
370 | ||
371 | msc |= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH; | |
372 | if (enable) | |
373 | msc |= ADIS16260_MSC_CTRL_DATA_RDY_EN; | |
374 | else | |
375 | msc &= ~ADIS16260_MSC_CTRL_DATA_RDY_EN; | |
376 | ||
377 | ret = adis16260_spi_write_reg_16(dev, ADIS16260_MSC_CTRL, msc); | |
378 | if (ret) | |
379 | goto error_ret; | |
380 | ||
381 | error_ret: | |
382 | return ret; | |
383 | } | |
384 | ||
385 | /* Power down the device */ | |
386 | static int adis16260_stop_device(struct device *dev) | |
387 | { | |
388 | int ret; | |
389 | u16 val = ADIS16260_SLP_CNT_POWER_OFF; | |
390 | ||
391 | ret = adis16260_spi_write_reg_16(dev, ADIS16260_SLP_CNT, val); | |
392 | if (ret) | |
393 | dev_err(dev, "problem with turning device off: SLP_CNT"); | |
394 | ||
395 | return ret; | |
396 | } | |
397 | ||
398 | static int adis16260_self_test(struct device *dev) | |
399 | { | |
400 | int ret; | |
401 | ret = adis16260_spi_write_reg_16(dev, | |
402 | ADIS16260_MSC_CTRL, | |
403 | ADIS16260_MSC_CTRL_MEM_TEST); | |
404 | if (ret) { | |
405 | dev_err(dev, "problem starting self test"); | |
406 | goto err_ret; | |
407 | } | |
408 | ||
409 | adis16260_check_status(dev); | |
410 | ||
411 | err_ret: | |
412 | return ret; | |
413 | } | |
414 | ||
415 | static int adis16260_check_status(struct device *dev) | |
416 | { | |
417 | u16 status; | |
418 | int ret; | |
419 | ||
420 | ret = adis16260_spi_read_reg_16(dev, ADIS16260_DIAG_STAT, &status); | |
421 | ||
422 | if (ret < 0) { | |
423 | dev_err(dev, "Reading status failed\n"); | |
424 | goto error_ret; | |
425 | } | |
426 | ret = status & 0x7F; | |
427 | if (status & ADIS16260_DIAG_STAT_FLASH_CHK) | |
428 | dev_err(dev, "Flash checksum error\n"); | |
429 | if (status & ADIS16260_DIAG_STAT_SELF_TEST) | |
430 | dev_err(dev, "Self test error\n"); | |
431 | if (status & ADIS16260_DIAG_STAT_OVERFLOW) | |
432 | dev_err(dev, "Sensor overrange\n"); | |
433 | if (status & ADIS16260_DIAG_STAT_SPI_FAIL) | |
434 | dev_err(dev, "SPI failure\n"); | |
435 | if (status & ADIS16260_DIAG_STAT_FLASH_UPT) | |
436 | dev_err(dev, "Flash update failed\n"); | |
437 | if (status & ADIS16260_DIAG_STAT_POWER_HIGH) | |
438 | dev_err(dev, "Power supply above 5.25V\n"); | |
439 | if (status & ADIS16260_DIAG_STAT_POWER_LOW) | |
440 | dev_err(dev, "Power supply below 4.75V\n"); | |
441 | ||
442 | error_ret: | |
443 | return ret; | |
444 | } | |
445 | ||
446 | static int adis16260_initial_setup(struct adis16260_state *st) | |
447 | { | |
448 | int ret; | |
449 | struct device *dev = &st->indio_dev->dev; | |
450 | ||
451 | /* Disable IRQ */ | |
452 | ret = adis16260_set_irq(dev, false); | |
453 | if (ret) { | |
454 | dev_err(dev, "disable irq failed"); | |
455 | goto err_ret; | |
456 | } | |
457 | ||
458 | /* Do self test */ | |
459 | ret = adis16260_self_test(dev); | |
460 | if (ret) { | |
461 | dev_err(dev, "self test failure"); | |
462 | goto err_ret; | |
463 | } | |
464 | ||
465 | /* Read status register to check the result */ | |
466 | ret = adis16260_check_status(dev); | |
467 | if (ret) { | |
468 | adis16260_reset(dev); | |
469 | dev_err(dev, "device not playing ball -> reset"); | |
470 | msleep(ADIS16260_STARTUP_DELAY); | |
471 | ret = adis16260_check_status(dev); | |
472 | if (ret) { | |
473 | dev_err(dev, "giving up"); | |
474 | goto err_ret; | |
475 | } | |
476 | } | |
477 | ||
478 | printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n", | |
479 | st->us->chip_select, st->us->irq); | |
480 | ||
481 | err_ret: | |
482 | return ret; | |
483 | } | |
484 | ||
1755b0ae | 485 | static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, |
089a4198 BS |
486 | adis16260_read_12bit_unsigned, |
487 | ADIS16260_SUPPLY_OUT); | |
1755b0ae | 488 | static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "0.0018315"); |
089a4198 | 489 | |
089a4198 | 490 | static IIO_DEV_ATTR_TEMP_RAW(adis16260_read_12bit_unsigned); |
51a0a5b0 MS |
491 | static IIO_CONST_ATTR_TEMP_OFFSET("25"); |
492 | static IIO_CONST_ATTR_TEMP_SCALE("0.1453"); | |
089a4198 | 493 | |
1755b0ae | 494 | static IIO_DEV_ATTR_IN_RAW(1, adis16260_read_12bit_unsigned, |
089a4198 | 495 | ADIS16260_AUX_ADC); |
1755b0ae | 496 | static IIO_CONST_ATTR(in1_scale, "0.0006105"); |
089a4198 BS |
497 | |
498 | static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, | |
499 | adis16260_read_frequency, | |
500 | adis16260_write_frequency); | |
089a4198 BS |
501 | |
502 | static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16260_write_reset, 0); | |
503 | ||
03d1b7d3 JC |
504 | |
505 | static IIO_DEVICE_ATTR(sampling_frequency_available, | |
506 | S_IRUGO, adis16260_read_frequency_available, NULL, 0); | |
089a4198 | 507 | |
51a0a5b0 | 508 | static IIO_CONST_ATTR_NAME("adis16260"); |
089a4198 BS |
509 | |
510 | static struct attribute *adis16260_event_attributes[] = { | |
511 | NULL | |
512 | }; | |
513 | ||
514 | static struct attribute_group adis16260_event_attribute_group = { | |
515 | .attrs = adis16260_event_attributes, | |
516 | }; | |
517 | ||
fe346048 JC |
518 | #define ADIS16260_GYRO_ATTR_SET(axis) \ |
519 | IIO_DEV_ATTR_GYRO##axis(adis16260_read_14bit_signed, \ | |
520 | ADIS16260_GYRO_OUT); \ | |
521 | static IIO_DEV_ATTR_GYRO##axis##_SCALE(S_IRUGO, \ | |
522 | adis16260_read_gyro_scale, \ | |
523 | NULL, \ | |
524 | 0); \ | |
525 | static IIO_DEV_ATTR_GYRO##axis##_CALIBSCALE(S_IRUGO | S_IWUSR, \ | |
526 | adis16260_read_12bit_unsigned, \ | |
527 | adis16260_write_16bit, \ | |
528 | ADIS16260_GYRO_SCALE); \ | |
529 | static IIO_DEV_ATTR_GYRO##axis##_CALIBBIAS(S_IWUSR | S_IRUGO, \ | |
530 | adis16260_read_12bit_signed, \ | |
531 | adis16260_write_16bit, \ | |
532 | ADIS16260_GYRO_OFF); \ | |
533 | static IIO_DEV_ATTR_ANGL##axis(adis16260_read_14bit_signed, \ | |
534 | ADIS16260_ANGL_OUT); | |
535 | ||
536 | static ADIS16260_GYRO_ATTR_SET(); | |
537 | static ADIS16260_GYRO_ATTR_SET(_X); | |
538 | static ADIS16260_GYRO_ATTR_SET(_Y); | |
539 | static ADIS16260_GYRO_ATTR_SET(_Z); | |
540 | ||
541 | #define ADIS16260_ATTR_GROUP(axis) \ | |
542 | struct attribute *adis16260_attributes##axis[] = { \ | |
543 | &iio_dev_attr_in0_supply_raw.dev_attr.attr, \ | |
544 | &iio_const_attr_in0_supply_scale.dev_attr.attr, \ | |
545 | &iio_dev_attr_gyro##axis##_raw.dev_attr.attr, \ | |
546 | &iio_dev_attr_gyro##axis##_scale.dev_attr.attr, \ | |
547 | &iio_dev_attr_gyro##axis##_calibscale.dev_attr.attr, \ | |
548 | &iio_dev_attr_gyro##axis##_calibbias.dev_attr.attr, \ | |
549 | &iio_dev_attr_angl##axis##_raw.dev_attr.attr, \ | |
550 | &iio_dev_attr_temp_raw.dev_attr.attr, \ | |
551 | &iio_const_attr_temp_offset.dev_attr.attr, \ | |
552 | &iio_const_attr_temp_scale.dev_attr.attr, \ | |
553 | &iio_dev_attr_in1_raw.dev_attr.attr, \ | |
554 | &iio_const_attr_in1_scale.dev_attr.attr, \ | |
555 | &iio_dev_attr_sampling_frequency.dev_attr.attr, \ | |
03d1b7d3 | 556 | &iio_dev_attr_sampling_frequency_available.dev_attr.attr, \ |
fe346048 JC |
557 | &iio_dev_attr_reset.dev_attr.attr, \ |
558 | &iio_const_attr_name.dev_attr.attr, \ | |
559 | NULL \ | |
560 | }; \ | |
561 | static const struct attribute_group adis16260_attribute_group##axis \ | |
562 | = { \ | |
563 | .attrs = adis16260_attributes##axis, \ | |
564 | }; | |
089a4198 | 565 | |
fe346048 JC |
566 | static ADIS16260_ATTR_GROUP(); |
567 | static ADIS16260_ATTR_GROUP(_x); | |
568 | static ADIS16260_ATTR_GROUP(_y); | |
569 | static ADIS16260_ATTR_GROUP(_z); | |
089a4198 BS |
570 | |
571 | static int __devinit adis16260_probe(struct spi_device *spi) | |
572 | { | |
573 | int ret, regdone = 0; | |
fe346048 | 574 | struct adis16260_platform_data *pd = spi->dev.platform_data; |
089a4198 BS |
575 | struct adis16260_state *st = kzalloc(sizeof *st, GFP_KERNEL); |
576 | if (!st) { | |
577 | ret = -ENOMEM; | |
578 | goto error_ret; | |
579 | } | |
fe346048 JC |
580 | if (pd) |
581 | st->negate = pd->negate; | |
089a4198 BS |
582 | /* this is only used for removal purposes */ |
583 | spi_set_drvdata(spi, st); | |
584 | ||
585 | /* Allocate the comms buffers */ | |
586 | st->rx = kzalloc(sizeof(*st->rx)*ADIS16260_MAX_RX, GFP_KERNEL); | |
587 | if (st->rx == NULL) { | |
588 | ret = -ENOMEM; | |
589 | goto error_free_st; | |
590 | } | |
591 | st->tx = kzalloc(sizeof(*st->tx)*ADIS16260_MAX_TX, GFP_KERNEL); | |
592 | if (st->tx == NULL) { | |
593 | ret = -ENOMEM; | |
594 | goto error_free_rx; | |
595 | } | |
596 | st->us = spi; | |
597 | mutex_init(&st->buf_lock); | |
598 | /* setup the industrialio driver allocated elements */ | |
6f7c8ee5 | 599 | st->indio_dev = iio_allocate_device(0); |
089a4198 BS |
600 | if (st->indio_dev == NULL) { |
601 | ret = -ENOMEM; | |
602 | goto error_free_tx; | |
603 | } | |
604 | ||
605 | st->indio_dev->dev.parent = &spi->dev; | |
606 | st->indio_dev->num_interrupt_lines = 1; | |
607 | st->indio_dev->event_attrs = &adis16260_event_attribute_group; | |
fe346048 JC |
608 | if (pd && pd->direction) |
609 | switch (pd->direction) { | |
610 | case 'x': | |
611 | st->indio_dev->attrs = &adis16260_attribute_group_x; | |
612 | break; | |
613 | case 'y': | |
614 | st->indio_dev->attrs = &adis16260_attribute_group_y; | |
615 | break; | |
616 | case 'z': | |
617 | st->indio_dev->attrs = &adis16260_attribute_group_z; | |
618 | break; | |
619 | default: | |
620 | st->indio_dev->attrs = &adis16260_attribute_group; | |
621 | break; | |
622 | } | |
623 | else | |
624 | st->indio_dev->attrs = &adis16260_attribute_group; | |
625 | ||
089a4198 BS |
626 | st->indio_dev->dev_data = (void *)(st); |
627 | st->indio_dev->driver_module = THIS_MODULE; | |
628 | st->indio_dev->modes = INDIO_DIRECT_MODE; | |
629 | ||
630 | ret = adis16260_configure_ring(st->indio_dev); | |
631 | if (ret) | |
632 | goto error_free_dev; | |
633 | ||
634 | ret = iio_device_register(st->indio_dev); | |
635 | if (ret) | |
636 | goto error_unreg_ring_funcs; | |
637 | regdone = 1; | |
2662051e | 638 | ret = iio_ring_buffer_register(st->indio_dev->ring, 0); |
089a4198 BS |
639 | if (ret) { |
640 | printk(KERN_ERR "failed to initialize the ring\n"); | |
641 | goto error_unreg_ring_funcs; | |
642 | } | |
643 | ||
644 | if (spi->irq) { | |
645 | ret = iio_register_interrupt_line(spi->irq, | |
646 | st->indio_dev, | |
647 | 0, | |
648 | IRQF_TRIGGER_RISING, | |
649 | "adis16260"); | |
650 | if (ret) | |
651 | goto error_uninitialize_ring; | |
652 | ||
653 | ret = adis16260_probe_trigger(st->indio_dev); | |
654 | if (ret) | |
655 | goto error_unregister_line; | |
656 | } | |
657 | ||
658 | /* Get the device into a sane initial state */ | |
659 | ret = adis16260_initial_setup(st); | |
660 | if (ret) | |
661 | goto error_remove_trigger; | |
662 | return 0; | |
663 | ||
664 | error_remove_trigger: | |
665 | adis16260_remove_trigger(st->indio_dev); | |
666 | error_unregister_line: | |
667 | if (spi->irq) | |
668 | iio_unregister_interrupt_line(st->indio_dev, 0); | |
669 | error_uninitialize_ring: | |
2662051e | 670 | iio_ring_buffer_unregister(st->indio_dev->ring); |
089a4198 BS |
671 | error_unreg_ring_funcs: |
672 | adis16260_unconfigure_ring(st->indio_dev); | |
673 | error_free_dev: | |
674 | if (regdone) | |
675 | iio_device_unregister(st->indio_dev); | |
676 | else | |
677 | iio_free_device(st->indio_dev); | |
678 | error_free_tx: | |
679 | kfree(st->tx); | |
680 | error_free_rx: | |
681 | kfree(st->rx); | |
682 | error_free_st: | |
683 | kfree(st); | |
684 | error_ret: | |
685 | return ret; | |
686 | } | |
687 | ||
688 | static int adis16260_remove(struct spi_device *spi) | |
689 | { | |
690 | int ret; | |
691 | struct adis16260_state *st = spi_get_drvdata(spi); | |
692 | struct iio_dev *indio_dev = st->indio_dev; | |
693 | ||
694 | ret = adis16260_stop_device(&(indio_dev->dev)); | |
695 | if (ret) | |
696 | goto err_ret; | |
697 | ||
698 | flush_scheduled_work(); | |
699 | ||
700 | adis16260_remove_trigger(indio_dev); | |
701 | if (spi->irq) | |
702 | iio_unregister_interrupt_line(indio_dev, 0); | |
703 | ||
2662051e | 704 | iio_ring_buffer_unregister(st->indio_dev->ring); |
089a4198 BS |
705 | iio_device_unregister(indio_dev); |
706 | adis16260_unconfigure_ring(indio_dev); | |
707 | kfree(st->tx); | |
708 | kfree(st->rx); | |
709 | kfree(st); | |
710 | ||
089a4198 BS |
711 | err_ret: |
712 | return ret; | |
713 | } | |
714 | ||
fe346048 JC |
715 | /* |
716 | * These parts do not need to be differentiated until someone adds | |
717 | * support for the on chip filtering. | |
718 | */ | |
a9672951 JC |
719 | static const struct spi_device_id adis16260_id[] = { |
720 | {"adis16260", 0}, | |
721 | {"adis16265", 0}, | |
fe346048 JC |
722 | {"adis16250", 0}, |
723 | {"adis16255", 0}, | |
03d1b7d3 | 724 | {"adis16251", 1}, |
a9672951 JC |
725 | {} |
726 | }; | |
727 | ||
089a4198 BS |
728 | static struct spi_driver adis16260_driver = { |
729 | .driver = { | |
730 | .name = "adis16260", | |
731 | .owner = THIS_MODULE, | |
732 | }, | |
733 | .probe = adis16260_probe, | |
734 | .remove = __devexit_p(adis16260_remove), | |
a9672951 | 735 | .id_table = adis16260_id, |
089a4198 BS |
736 | }; |
737 | ||
738 | static __init int adis16260_init(void) | |
739 | { | |
740 | return spi_register_driver(&adis16260_driver); | |
741 | } | |
742 | module_init(adis16260_init); | |
743 | ||
744 | static __exit void adis16260_exit(void) | |
745 | { | |
746 | spi_unregister_driver(&adis16260_driver); | |
747 | } | |
748 | module_exit(adis16260_exit); | |
749 | ||
750 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | |
751 | MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor"); | |
752 | MODULE_LICENSE("GPL v2"); |