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