]>
Commit | Line | Data |
---|---|---|
5763dcab BS |
1 | /* |
2 | * ADIS16350/54/55/60/62/64/65 high precision tri-axis inertial sensor | |
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> |
5763dcab BS |
18 | #include <linux/sysfs.h> |
19 | #include <linux/list.h> | |
20 | ||
21 | #include "../iio.h" | |
22 | #include "../sysfs.h" | |
23 | #include "../accel/accel.h" | |
24 | #include "../adc/adc.h" | |
25 | #include "../gyro/gyro.h" | |
26 | ||
27 | #include "adis16350.h" | |
28 | ||
29 | #define DRIVER_NAME "adis16350" | |
30 | ||
31 | static int adis16350_check_status(struct device *dev); | |
32 | ||
33 | /** | |
34 | * adis16350_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 adis16350_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 adis16350_state *st = iio_dev_get_devdata(indio_dev); | |
46 | ||
47 | mutex_lock(&st->buf_lock); | |
48 | st->tx[0] = ADIS16350_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 | * adis16350_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 adis16350_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 adis16350_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, | |
00ae7946 | 78 | .delay_usecs = 35, |
5763dcab BS |
79 | }, { |
80 | .tx_buf = st->tx + 2, | |
81 | .bits_per_word = 8, | |
82 | .len = 2, | |
83 | .cs_change = 1, | |
00ae7946 | 84 | .delay_usecs = 35, |
5763dcab BS |
85 | }, |
86 | }; | |
87 | ||
88 | mutex_lock(&st->buf_lock); | |
89 | st->tx[0] = ADIS16350_WRITE_REG(lower_reg_address); | |
90 | st->tx[1] = value & 0xFF; | |
91 | st->tx[2] = ADIS16350_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 | * adis16350_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 adis16350_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 adis16350_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, | |
00ae7946 | 124 | .delay_usecs = 35, |
5763dcab BS |
125 | }, { |
126 | .rx_buf = st->rx, | |
127 | .bits_per_word = 8, | |
128 | .len = 2, | |
129 | .cs_change = 1, | |
00ae7946 | 130 | .delay_usecs = 35, |
5763dcab BS |
131 | }, |
132 | }; | |
133 | ||
134 | mutex_lock(&st->buf_lock); | |
135 | st->tx[0] = ADIS16350_READ_REG(lower_reg_address); | |
136 | st->tx[1] = 0; | |
137 | st->tx[2] = 0; | |
138 | st->tx[3] = 0; | |
139 | ||
140 | spi_message_init(&msg); | |
141 | spi_message_add_tail(&xfers[0], &msg); | |
142 | spi_message_add_tail(&xfers[1], &msg); | |
143 | ret = spi_sync(st->us, &msg); | |
144 | if (ret) { | |
145 | dev_err(&st->us->dev, | |
146 | "problem when reading 16 bit register 0x%02X", | |
147 | lower_reg_address); | |
148 | goto error_ret; | |
149 | } | |
150 | *val = (st->rx[0] << 8) | st->rx[1]; | |
151 | ||
152 | error_ret: | |
153 | mutex_unlock(&st->buf_lock); | |
154 | return ret; | |
155 | } | |
156 | ||
157 | ||
158 | static ssize_t adis16350_spi_read_signed(struct device *dev, | |
159 | struct device_attribute *attr, | |
160 | char *buf, | |
161 | unsigned bits) | |
162 | { | |
163 | int ret; | |
164 | s16 val = 0; | |
165 | unsigned shift = 16 - bits; | |
166 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
167 | ||
168 | ret = adis16350_spi_read_reg_16(dev, this_attr->address, (u16 *)&val); | |
169 | if (ret) | |
170 | return ret; | |
171 | ||
172 | if (val & ADIS16350_ERROR_ACTIVE) | |
173 | adis16350_check_status(dev); | |
174 | val = ((s16)(val << shift) >> shift); | |
175 | return sprintf(buf, "%d\n", val); | |
176 | } | |
177 | ||
178 | static ssize_t adis16350_read_12bit_unsigned(struct device *dev, | |
179 | struct device_attribute *attr, | |
180 | char *buf) | |
181 | { | |
182 | int ret; | |
183 | u16 val = 0; | |
184 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
185 | ||
186 | ret = adis16350_spi_read_reg_16(dev, this_attr->address, &val); | |
187 | if (ret) | |
188 | return ret; | |
189 | ||
190 | if (val & ADIS16350_ERROR_ACTIVE) | |
191 | adis16350_check_status(dev); | |
192 | ||
193 | return sprintf(buf, "%u\n", val & 0x0FFF); | |
194 | } | |
195 | ||
196 | static ssize_t adis16350_read_14bit_signed(struct device *dev, | |
197 | struct device_attribute *attr, | |
198 | char *buf) | |
199 | { | |
200 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
201 | ssize_t ret; | |
202 | ||
203 | /* Take the iio_dev status lock */ | |
204 | mutex_lock(&indio_dev->mlock); | |
205 | ret = adis16350_spi_read_signed(dev, attr, buf, 14); | |
206 | mutex_unlock(&indio_dev->mlock); | |
207 | ||
208 | return ret; | |
209 | } | |
210 | ||
211 | static ssize_t adis16350_read_12bit_signed(struct device *dev, | |
212 | struct device_attribute *attr, | |
213 | char *buf) | |
214 | { | |
215 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
216 | ssize_t ret; | |
217 | ||
218 | /* Take the iio_dev status lock */ | |
219 | mutex_lock(&indio_dev->mlock); | |
220 | ret = adis16350_spi_read_signed(dev, attr, buf, 12); | |
221 | mutex_unlock(&indio_dev->mlock); | |
222 | ||
223 | return ret; | |
224 | } | |
225 | ||
226 | static ssize_t adis16350_write_16bit(struct device *dev, | |
227 | struct device_attribute *attr, | |
228 | const char *buf, | |
229 | size_t len) | |
230 | { | |
231 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
232 | int ret; | |
233 | long val; | |
234 | ||
235 | ret = strict_strtol(buf, 10, &val); | |
236 | if (ret) | |
237 | goto error_ret; | |
238 | ret = adis16350_spi_write_reg_16(dev, this_attr->address, val); | |
239 | ||
240 | error_ret: | |
241 | return ret ? ret : len; | |
242 | } | |
243 | ||
244 | static ssize_t adis16350_read_frequency(struct device *dev, | |
245 | struct device_attribute *attr, | |
246 | char *buf) | |
247 | { | |
248 | int ret, len = 0; | |
249 | u16 t; | |
250 | int sps; | |
251 | ret = adis16350_spi_read_reg_16(dev, | |
252 | ADIS16350_SMPL_PRD, | |
253 | &t); | |
254 | if (ret) | |
255 | return ret; | |
256 | sps = (t & ADIS16350_SMPL_PRD_TIME_BASE) ? 53 : 1638; | |
257 | sps /= (t & ADIS16350_SMPL_PRD_DIV_MASK) + 1; | |
258 | len = sprintf(buf, "%d SPS\n", sps); | |
259 | return len; | |
260 | } | |
261 | ||
262 | static ssize_t adis16350_write_frequency(struct device *dev, | |
263 | struct device_attribute *attr, | |
264 | const char *buf, | |
265 | size_t len) | |
266 | { | |
267 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
268 | struct adis16350_state *st = iio_dev_get_devdata(indio_dev); | |
269 | long val; | |
270 | int ret; | |
271 | u8 t; | |
272 | ||
273 | ret = strict_strtol(buf, 10, &val); | |
274 | if (ret) | |
275 | return ret; | |
276 | ||
277 | mutex_lock(&indio_dev->mlock); | |
278 | ||
279 | t = (1638 / val); | |
280 | if (t > 0) | |
281 | t--; | |
282 | t &= ADIS16350_SMPL_PRD_DIV_MASK; | |
283 | if ((t & ADIS16350_SMPL_PRD_DIV_MASK) >= 0x0A) | |
284 | st->us->max_speed_hz = ADIS16350_SPI_SLOW; | |
285 | else | |
286 | st->us->max_speed_hz = ADIS16350_SPI_FAST; | |
287 | ||
288 | ret = adis16350_spi_write_reg_8(dev, | |
289 | ADIS16350_SMPL_PRD, | |
290 | t); | |
291 | ||
292 | mutex_unlock(&indio_dev->mlock); | |
293 | ||
294 | return ret ? ret : len; | |
295 | } | |
296 | ||
297 | static int adis16350_reset(struct device *dev) | |
298 | { | |
299 | int ret; | |
300 | ret = adis16350_spi_write_reg_8(dev, | |
301 | ADIS16350_GLOB_CMD, | |
302 | ADIS16350_GLOB_CMD_SW_RESET); | |
303 | if (ret) | |
304 | dev_err(dev, "problem resetting device"); | |
305 | ||
306 | return ret; | |
307 | } | |
308 | ||
309 | static ssize_t adis16350_write_reset(struct device *dev, | |
310 | struct device_attribute *attr, | |
311 | const char *buf, size_t len) | |
312 | { | |
313 | if (len < 1) | |
314 | return -1; | |
315 | switch (buf[0]) { | |
316 | case '1': | |
317 | case 'y': | |
318 | case 'Y': | |
319 | return adis16350_reset(dev); | |
320 | } | |
321 | return -1; | |
322 | } | |
323 | ||
324 | int adis16350_set_irq(struct device *dev, bool enable) | |
325 | { | |
326 | int ret; | |
327 | u16 msc; | |
328 | ret = adis16350_spi_read_reg_16(dev, ADIS16350_MSC_CTRL, &msc); | |
329 | if (ret) | |
330 | goto error_ret; | |
331 | ||
332 | msc |= ADIS16350_MSC_CTRL_DATA_RDY_POL_HIGH; | |
333 | msc &= ~ADIS16350_MSC_CTRL_DATA_RDY_DIO2; | |
334 | ||
335 | if (enable) | |
336 | msc |= ADIS16350_MSC_CTRL_DATA_RDY_EN; | |
337 | else | |
338 | msc &= ~ADIS16350_MSC_CTRL_DATA_RDY_EN; | |
339 | ||
340 | ret = adis16350_spi_write_reg_16(dev, ADIS16350_MSC_CTRL, msc); | |
341 | if (ret) | |
342 | goto error_ret; | |
343 | ||
344 | error_ret: | |
345 | return ret; | |
346 | } | |
347 | ||
348 | /* Power down the device */ | |
349 | static int adis16350_stop_device(struct device *dev) | |
350 | { | |
351 | int ret; | |
352 | u16 val = ADIS16350_SLP_CNT_POWER_OFF; | |
353 | ||
354 | ret = adis16350_spi_write_reg_16(dev, ADIS16350_SLP_CNT, val); | |
355 | if (ret) | |
356 | dev_err(dev, "problem with turning device off: SLP_CNT"); | |
357 | ||
358 | return ret; | |
359 | } | |
360 | ||
361 | static int adis16350_self_test(struct device *dev) | |
362 | { | |
363 | int ret; | |
364 | ret = adis16350_spi_write_reg_16(dev, | |
365 | ADIS16350_MSC_CTRL, | |
366 | ADIS16350_MSC_CTRL_MEM_TEST); | |
367 | if (ret) { | |
368 | dev_err(dev, "problem starting self test"); | |
369 | goto err_ret; | |
370 | } | |
371 | ||
372 | adis16350_check_status(dev); | |
373 | ||
374 | err_ret: | |
375 | return ret; | |
376 | } | |
377 | ||
378 | static int adis16350_check_status(struct device *dev) | |
379 | { | |
380 | u16 status; | |
381 | int ret; | |
382 | ||
383 | ret = adis16350_spi_read_reg_16(dev, ADIS16350_DIAG_STAT, &status); | |
384 | ||
385 | if (ret < 0) { | |
386 | dev_err(dev, "Reading status failed\n"); | |
387 | goto error_ret; | |
388 | } | |
389 | ret = status; | |
390 | if (status & ADIS16350_DIAG_STAT_ZACCL_FAIL) | |
391 | dev_err(dev, "Z-axis accelerometer self-test failure\n"); | |
392 | if (status & ADIS16350_DIAG_STAT_YACCL_FAIL) | |
393 | dev_err(dev, "Y-axis accelerometer self-test failure\n"); | |
394 | if (status & ADIS16350_DIAG_STAT_XACCL_FAIL) | |
395 | dev_err(dev, "X-axis accelerometer self-test failure\n"); | |
396 | if (status & ADIS16350_DIAG_STAT_XGYRO_FAIL) | |
397 | dev_err(dev, "X-axis gyroscope self-test failure\n"); | |
398 | if (status & ADIS16350_DIAG_STAT_YGYRO_FAIL) | |
399 | dev_err(dev, "Y-axis gyroscope self-test failure\n"); | |
400 | if (status & ADIS16350_DIAG_STAT_ZGYRO_FAIL) | |
401 | dev_err(dev, "Z-axis gyroscope self-test failure\n"); | |
402 | if (status & ADIS16350_DIAG_STAT_ALARM2) | |
403 | dev_err(dev, "Alarm 2 active\n"); | |
404 | if (status & ADIS16350_DIAG_STAT_ALARM1) | |
405 | dev_err(dev, "Alarm 1 active\n"); | |
406 | if (status & ADIS16350_DIAG_STAT_FLASH_CHK) | |
407 | dev_err(dev, "Flash checksum error\n"); | |
408 | if (status & ADIS16350_DIAG_STAT_SELF_TEST) | |
409 | dev_err(dev, "Self test error\n"); | |
410 | if (status & ADIS16350_DIAG_STAT_OVERFLOW) | |
411 | dev_err(dev, "Sensor overrange\n"); | |
412 | if (status & ADIS16350_DIAG_STAT_SPI_FAIL) | |
413 | dev_err(dev, "SPI failure\n"); | |
414 | if (status & ADIS16350_DIAG_STAT_FLASH_UPT) | |
415 | dev_err(dev, "Flash update failed\n"); | |
416 | if (status & ADIS16350_DIAG_STAT_POWER_HIGH) | |
417 | dev_err(dev, "Power supply above 5.25V\n"); | |
418 | if (status & ADIS16350_DIAG_STAT_POWER_LOW) | |
419 | dev_err(dev, "Power supply below 4.75V\n"); | |
420 | ||
421 | error_ret: | |
422 | return ret; | |
423 | } | |
424 | ||
425 | static int adis16350_initial_setup(struct adis16350_state *st) | |
426 | { | |
427 | int ret; | |
428 | u16 smp_prd; | |
429 | struct device *dev = &st->indio_dev->dev; | |
430 | ||
431 | /* use low spi speed for init */ | |
432 | st->us->max_speed_hz = ADIS16350_SPI_SLOW; | |
433 | st->us->mode = SPI_MODE_3; | |
434 | spi_setup(st->us); | |
435 | ||
436 | /* Disable IRQ */ | |
437 | ret = adis16350_set_irq(dev, false); | |
438 | if (ret) { | |
439 | dev_err(dev, "disable irq failed"); | |
440 | goto err_ret; | |
441 | } | |
442 | ||
443 | /* Do self test */ | |
444 | ret = adis16350_self_test(dev); | |
445 | if (ret) { | |
446 | dev_err(dev, "self test failure"); | |
447 | goto err_ret; | |
448 | } | |
449 | ||
450 | /* Read status register to check the result */ | |
451 | ret = adis16350_check_status(dev); | |
452 | if (ret) { | |
453 | adis16350_reset(dev); | |
454 | dev_err(dev, "device not playing ball -> reset"); | |
455 | msleep(ADIS16350_STARTUP_DELAY); | |
456 | ret = adis16350_check_status(dev); | |
457 | if (ret) { | |
458 | dev_err(dev, "giving up"); | |
459 | goto err_ret; | |
460 | } | |
461 | } | |
462 | ||
463 | printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n", | |
464 | st->us->chip_select, st->us->irq); | |
465 | ||
466 | /* use high spi speed if possible */ | |
467 | ret = adis16350_spi_read_reg_16(dev, ADIS16350_SMPL_PRD, &smp_prd); | |
468 | if (!ret && (smp_prd & ADIS16350_SMPL_PRD_DIV_MASK) < 0x0A) { | |
469 | st->us->max_speed_hz = ADIS16350_SPI_SLOW; | |
470 | spi_setup(st->us); | |
471 | } | |
472 | ||
473 | err_ret: | |
474 | return ret; | |
475 | } | |
476 | ||
477 | static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO, | |
478 | adis16350_read_12bit_signed, | |
479 | adis16350_write_16bit, | |
480 | ADIS16350_XACCL_OFF); | |
481 | ||
482 | static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO, | |
483 | adis16350_read_12bit_signed, | |
484 | adis16350_write_16bit, | |
485 | ADIS16350_YACCL_OFF); | |
486 | ||
487 | static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO, | |
488 | adis16350_read_12bit_signed, | |
489 | adis16350_write_16bit, | |
490 | ADIS16350_ZACCL_OFF); | |
491 | ||
492 | static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16350_read_12bit_unsigned, | |
493 | ADIS16350_SUPPLY_OUT); | |
494 | static IIO_CONST_ATTR(in_supply_scale, "0.002418"); | |
495 | ||
496 | static IIO_DEV_ATTR_GYRO_X(adis16350_read_14bit_signed, | |
497 | ADIS16350_XGYRO_OUT); | |
498 | static IIO_DEV_ATTR_GYRO_Y(adis16350_read_14bit_signed, | |
499 | ADIS16350_YGYRO_OUT); | |
500 | static IIO_DEV_ATTR_GYRO_Z(adis16350_read_14bit_signed, | |
501 | ADIS16350_ZGYRO_OUT); | |
502 | static IIO_CONST_ATTR(gyro_scale, "0.05"); | |
503 | ||
504 | static IIO_DEV_ATTR_ACCEL_X(adis16350_read_14bit_signed, | |
505 | ADIS16350_XACCL_OUT); | |
506 | static IIO_DEV_ATTR_ACCEL_Y(adis16350_read_14bit_signed, | |
507 | ADIS16350_YACCL_OUT); | |
508 | static IIO_DEV_ATTR_ACCEL_Z(adis16350_read_14bit_signed, | |
509 | ADIS16350_ZACCL_OUT); | |
510 | static IIO_CONST_ATTR(accel_scale, "0.00333"); | |
511 | ||
512 | static IIO_DEVICE_ATTR(temp_x_raw, S_IRUGO, adis16350_read_12bit_signed, | |
513 | NULL, ADIS16350_XTEMP_OUT); | |
514 | static IIO_DEVICE_ATTR(temp_y_raw, S_IRUGO, adis16350_read_12bit_signed, | |
515 | NULL, ADIS16350_YTEMP_OUT); | |
516 | static IIO_DEVICE_ATTR(temp_z_raw, S_IRUGO, adis16350_read_12bit_signed, | |
517 | NULL, ADIS16350_ZTEMP_OUT); | |
518 | static IIO_CONST_ATTR(temp_scale, "0.0005"); | |
519 | ||
520 | static IIO_DEV_ATTR_IN_RAW(0, adis16350_read_12bit_unsigned, | |
521 | ADIS16350_AUX_ADC); | |
522 | static IIO_CONST_ATTR(in0_scale, "0.000806"); | |
523 | ||
524 | static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, | |
525 | adis16350_read_frequency, | |
526 | adis16350_write_frequency); | |
527 | ||
528 | static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, | |
529 | adis16350_write_reset, 0); | |
530 | ||
531 | static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638"); | |
532 | ||
533 | static IIO_CONST_ATTR(name, "adis16350"); | |
534 | ||
535 | static struct attribute *adis16350_attributes[] = { | |
536 | &iio_dev_attr_accel_x_offset.dev_attr.attr, | |
537 | &iio_dev_attr_accel_y_offset.dev_attr.attr, | |
538 | &iio_dev_attr_accel_z_offset.dev_attr.attr, | |
539 | &iio_dev_attr_in_supply_raw.dev_attr.attr, | |
540 | &iio_const_attr_in_supply_scale.dev_attr.attr, | |
541 | &iio_dev_attr_gyro_x_raw.dev_attr.attr, | |
542 | &iio_dev_attr_gyro_y_raw.dev_attr.attr, | |
543 | &iio_dev_attr_gyro_z_raw.dev_attr.attr, | |
544 | &iio_const_attr_gyro_scale.dev_attr.attr, | |
545 | &iio_dev_attr_accel_x_raw.dev_attr.attr, | |
546 | &iio_dev_attr_accel_y_raw.dev_attr.attr, | |
547 | &iio_dev_attr_accel_z_raw.dev_attr.attr, | |
548 | &iio_const_attr_accel_scale.dev_attr.attr, | |
549 | &iio_dev_attr_temp_x_raw.dev_attr.attr, | |
550 | &iio_dev_attr_temp_y_raw.dev_attr.attr, | |
551 | &iio_dev_attr_temp_z_raw.dev_attr.attr, | |
552 | &iio_const_attr_temp_scale.dev_attr.attr, | |
553 | &iio_dev_attr_in0_raw.dev_attr.attr, | |
554 | &iio_const_attr_in0_scale.dev_attr.attr, | |
555 | &iio_dev_attr_sampling_frequency.dev_attr.attr, | |
556 | &iio_const_attr_available_sampling_frequency.dev_attr.attr, | |
557 | &iio_dev_attr_reset.dev_attr.attr, | |
558 | &iio_const_attr_name.dev_attr.attr, | |
559 | NULL | |
560 | }; | |
561 | ||
562 | static const struct attribute_group adis16350_attribute_group = { | |
563 | .attrs = adis16350_attributes, | |
564 | }; | |
565 | ||
566 | static struct attribute *adis16350_event_attributes[] = { | |
567 | NULL, | |
568 | }; | |
569 | ||
570 | static struct attribute_group adis16350_event_attribute_group = { | |
571 | .attrs = adis16350_event_attributes, | |
572 | }; | |
573 | ||
574 | static int __devinit adis16350_probe(struct spi_device *spi) | |
575 | { | |
576 | int ret, regdone = 0; | |
577 | struct adis16350_state *st = kzalloc(sizeof *st, GFP_KERNEL); | |
578 | if (!st) { | |
579 | ret = -ENOMEM; | |
580 | goto error_ret; | |
581 | } | |
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)*ADIS16350_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)*ADIS16350_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 */ | |
599 | st->indio_dev = iio_allocate_device(); | |
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 = &adis16350_event_attribute_group; | |
608 | st->indio_dev->attrs = &adis16350_attribute_group; | |
609 | st->indio_dev->dev_data = (void *)(st); | |
610 | st->indio_dev->driver_module = THIS_MODULE; | |
611 | st->indio_dev->modes = INDIO_DIRECT_MODE; | |
612 | ||
613 | ret = adis16350_configure_ring(st->indio_dev); | |
614 | if (ret) | |
615 | goto error_free_dev; | |
616 | ||
617 | ret = iio_device_register(st->indio_dev); | |
618 | if (ret) | |
619 | goto error_unreg_ring_funcs; | |
620 | regdone = 1; | |
621 | ||
622 | ret = adis16350_initialize_ring(st->indio_dev->ring); | |
623 | if (ret) { | |
624 | printk(KERN_ERR "failed to initialize the ring\n"); | |
625 | goto error_unreg_ring_funcs; | |
626 | } | |
627 | ||
628 | if (spi->irq) { | |
629 | ret = iio_register_interrupt_line(spi->irq, | |
630 | st->indio_dev, | |
631 | 0, | |
632 | IRQF_TRIGGER_RISING, | |
633 | "adis16350"); | |
634 | if (ret) | |
635 | goto error_uninitialize_ring; | |
636 | ||
637 | ret = adis16350_probe_trigger(st->indio_dev); | |
638 | if (ret) | |
639 | goto error_unregister_line; | |
640 | } | |
641 | ||
642 | /* Get the device into a sane initial state */ | |
643 | ret = adis16350_initial_setup(st); | |
644 | if (ret) | |
645 | goto error_remove_trigger; | |
646 | return 0; | |
647 | ||
648 | error_remove_trigger: | |
649 | adis16350_remove_trigger(st->indio_dev); | |
650 | error_unregister_line: | |
651 | if (spi->irq) | |
652 | iio_unregister_interrupt_line(st->indio_dev, 0); | |
653 | error_uninitialize_ring: | |
654 | adis16350_uninitialize_ring(st->indio_dev->ring); | |
655 | error_unreg_ring_funcs: | |
656 | adis16350_unconfigure_ring(st->indio_dev); | |
657 | error_free_dev: | |
658 | if (regdone) | |
659 | iio_device_unregister(st->indio_dev); | |
660 | else | |
661 | iio_free_device(st->indio_dev); | |
662 | error_free_tx: | |
663 | kfree(st->tx); | |
664 | error_free_rx: | |
665 | kfree(st->rx); | |
666 | error_free_st: | |
667 | kfree(st); | |
668 | error_ret: | |
669 | return ret; | |
670 | } | |
671 | ||
672 | static int adis16350_remove(struct spi_device *spi) | |
673 | { | |
674 | int ret; | |
675 | struct adis16350_state *st = spi_get_drvdata(spi); | |
676 | struct iio_dev *indio_dev = st->indio_dev; | |
677 | ||
678 | ret = adis16350_stop_device(&(indio_dev->dev)); | |
679 | if (ret) | |
680 | goto err_ret; | |
681 | ||
682 | flush_scheduled_work(); | |
683 | ||
684 | adis16350_remove_trigger(indio_dev); | |
685 | if (spi->irq) | |
686 | iio_unregister_interrupt_line(indio_dev, 0); | |
687 | ||
688 | adis16350_uninitialize_ring(indio_dev->ring); | |
689 | iio_device_unregister(indio_dev); | |
690 | adis16350_unconfigure_ring(indio_dev); | |
691 | kfree(st->tx); | |
692 | kfree(st->rx); | |
693 | kfree(st); | |
694 | ||
695 | return 0; | |
696 | ||
697 | err_ret: | |
698 | return ret; | |
699 | } | |
700 | ||
701 | static const struct spi_device_id adis16350_id[] = { | |
702 | {"adis16350", 0}, | |
703 | {"adis16354", 0}, | |
704 | {"adis16355", 0}, | |
705 | {"adis16360", 0}, | |
706 | {"adis16362", 0}, | |
707 | {"adis16364", 0}, | |
708 | {"adis16365", 0}, | |
709 | {} | |
710 | }; | |
711 | ||
712 | static struct spi_driver adis16350_driver = { | |
713 | .driver = { | |
714 | .name = "adis16350", | |
715 | .owner = THIS_MODULE, | |
716 | }, | |
717 | .probe = adis16350_probe, | |
718 | .remove = __devexit_p(adis16350_remove), | |
719 | .id_table = adis16350_id, | |
720 | }; | |
721 | ||
722 | static __init int adis16350_init(void) | |
723 | { | |
724 | return spi_register_driver(&adis16350_driver); | |
725 | } | |
726 | module_init(adis16350_init); | |
727 | ||
728 | static __exit void adis16350_exit(void) | |
729 | { | |
730 | spi_unregister_driver(&adis16350_driver); | |
731 | } | |
732 | module_exit(adis16350_exit); | |
733 | ||
734 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | |
735 | MODULE_DESCRIPTION("Analog Devices ADIS16350/54/55/60/62/64/65 IMU SPI driver"); | |
736 | MODULE_LICENSE("GPL v2"); |