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