• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/staging/iio/gyro/
1/*
2 * ADIS16260 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/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>
17#include <linux/slab.h>
18#include <linux/sysfs.h>
19#include <linux/list.h>
20
21#include "../iio.h"
22#include "../sysfs.h"
23#include "../ring_generic.h"
24#include "../adc/adc.h"
25#include "gyro.h"
26
27#include "adis16260.h"
28
29#define DRIVER_NAME		"adis16260"
30
31static 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 **/
39static 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 **/
64static 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 **/
110static 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;
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
152error_ret:
153	mutex_unlock(&st->buf_lock);
154	return ret;
155}
156
157static ssize_t adis16260_spi_read_signed(struct device *dev,
158		struct device_attribute *attr,
159		char *buf,
160		unsigned bits)
161{
162	int ret;
163	s16 val = 0;
164	unsigned shift = 16 - bits;
165	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
166
167	ret = adis16260_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
168	if (ret)
169		return ret;
170
171	if (val & ADIS16260_ERROR_ACTIVE)
172		adis16260_check_status(dev);
173	val = ((s16)(val << shift) >> shift);
174	return sprintf(buf, "%d\n", val);
175}
176
177static ssize_t adis16260_read_12bit_unsigned(struct device *dev,
178		struct device_attribute *attr,
179		char *buf)
180{
181	int ret;
182	u16 val = 0;
183	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
184
185	ret = adis16260_spi_read_reg_16(dev, this_attr->address, &val);
186	if (ret)
187		return ret;
188
189	if (val & ADIS16260_ERROR_ACTIVE)
190		adis16260_check_status(dev);
191
192	return sprintf(buf, "%u\n", val & 0x0FFF);
193}
194
195static ssize_t adis16260_read_12bit_signed(struct device *dev,
196		struct device_attribute *attr,
197		char *buf)
198{
199	struct iio_dev *indio_dev = dev_get_drvdata(dev);
200	ssize_t ret;
201
202	/* Take the iio_dev status lock */
203	mutex_lock(&indio_dev->mlock);
204	ret =  adis16260_spi_read_signed(dev, attr, buf, 12);
205	mutex_unlock(&indio_dev->mlock);
206
207	return ret;
208}
209
210static ssize_t adis16260_read_14bit_signed(struct device *dev,
211		struct device_attribute *attr,
212		char *buf)
213{
214	struct iio_dev *indio_dev = dev_get_drvdata(dev);
215	ssize_t ret;
216
217	/* Take the iio_dev status lock */
218	mutex_lock(&indio_dev->mlock);
219	ret =  adis16260_spi_read_signed(dev, attr, buf, 14);
220	mutex_unlock(&indio_dev->mlock);
221
222	return ret;
223}
224
225static ssize_t adis16260_write_16bit(struct device *dev,
226		struct device_attribute *attr,
227		const char *buf,
228		size_t len)
229{
230	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
231	int ret;
232	long val;
233
234	ret = strict_strtol(buf, 10, &val);
235	if (ret)
236		goto error_ret;
237	ret = adis16260_spi_write_reg_16(dev, this_attr->address, val);
238
239error_ret:
240	return ret ? ret : len;
241}
242
243static ssize_t adis16260_read_frequency(struct device *dev,
244		struct device_attribute *attr,
245		char *buf)
246{
247	int ret, len = 0;
248	u16 t;
249	int sps;
250	ret = adis16260_spi_read_reg_16(dev,
251			ADIS16260_SMPL_PRD,
252			&t);
253	if (ret)
254		return ret;
255	sps =  (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048;
256	sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
257	len = sprintf(buf, "%d SPS\n", sps);
258	return len;
259}
260
261static ssize_t adis16260_write_frequency(struct device *dev,
262		struct device_attribute *attr,
263		const char *buf,
264		size_t len)
265{
266	struct iio_dev *indio_dev = dev_get_drvdata(dev);
267	struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
268	long val;
269	int ret;
270	u8 t;
271
272	ret = strict_strtol(buf, 10, &val);
273	if (ret)
274		return ret;
275
276	mutex_lock(&indio_dev->mlock);
277
278	t = (2048 / val);
279	if (t > 0)
280		t--;
281	t &= ADIS16260_SMPL_PRD_DIV_MASK;
282	if ((t & ADIS16260_SMPL_PRD_DIV_MASK) >= 0x0A)
283		st->us->max_speed_hz = ADIS16260_SPI_SLOW;
284	else
285		st->us->max_speed_hz = ADIS16260_SPI_FAST;
286
287	ret = adis16260_spi_write_reg_8(dev,
288			ADIS16260_SMPL_PRD,
289			t);
290
291	mutex_unlock(&indio_dev->mlock);
292
293	return ret ? ret : len;
294}
295
296static int adis16260_reset(struct device *dev)
297{
298	int ret;
299	ret = adis16260_spi_write_reg_8(dev,
300			ADIS16260_GLOB_CMD,
301			ADIS16260_GLOB_CMD_SW_RESET);
302	if (ret)
303		dev_err(dev, "problem resetting device");
304
305	return ret;
306}
307
308static ssize_t adis16260_write_reset(struct device *dev,
309		struct device_attribute *attr,
310		const char *buf, size_t len)
311{
312	if (len < 1)
313		return -EINVAL;
314	switch (buf[0]) {
315	case '1':
316	case 'y':
317	case 'Y':
318		return adis16260_reset(dev);
319	}
320	return -EINVAL;
321}
322
323int adis16260_set_irq(struct device *dev, bool enable)
324{
325	int ret;
326	u16 msc;
327	ret = adis16260_spi_read_reg_16(dev, ADIS16260_MSC_CTRL, &msc);
328	if (ret)
329		goto error_ret;
330
331	msc |= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH;
332	if (enable)
333		msc |= ADIS16260_MSC_CTRL_DATA_RDY_EN;
334	else
335		msc &= ~ADIS16260_MSC_CTRL_DATA_RDY_EN;
336
337	ret = adis16260_spi_write_reg_16(dev, ADIS16260_MSC_CTRL, msc);
338	if (ret)
339		goto error_ret;
340
341error_ret:
342	return ret;
343}
344
345/* Power down the device */
346static int adis16260_stop_device(struct device *dev)
347{
348	int ret;
349	u16 val = ADIS16260_SLP_CNT_POWER_OFF;
350
351	ret = adis16260_spi_write_reg_16(dev, ADIS16260_SLP_CNT, val);
352	if (ret)
353		dev_err(dev, "problem with turning device off: SLP_CNT");
354
355	return ret;
356}
357
358static int adis16260_self_test(struct device *dev)
359{
360	int ret;
361	ret = adis16260_spi_write_reg_16(dev,
362			ADIS16260_MSC_CTRL,
363			ADIS16260_MSC_CTRL_MEM_TEST);
364	if (ret) {
365		dev_err(dev, "problem starting self test");
366		goto err_ret;
367	}
368
369	adis16260_check_status(dev);
370
371err_ret:
372	return ret;
373}
374
375static int adis16260_check_status(struct device *dev)
376{
377	u16 status;
378	int ret;
379
380	ret = adis16260_spi_read_reg_16(dev, ADIS16260_DIAG_STAT, &status);
381
382	if (ret < 0) {
383		dev_err(dev, "Reading status failed\n");
384		goto error_ret;
385	}
386	ret = status & 0x7F;
387	if (status & ADIS16260_DIAG_STAT_FLASH_CHK)
388		dev_err(dev, "Flash checksum error\n");
389	if (status & ADIS16260_DIAG_STAT_SELF_TEST)
390		dev_err(dev, "Self test error\n");
391	if (status & ADIS16260_DIAG_STAT_OVERFLOW)
392		dev_err(dev, "Sensor overrange\n");
393	if (status & ADIS16260_DIAG_STAT_SPI_FAIL)
394		dev_err(dev, "SPI failure\n");
395	if (status & ADIS16260_DIAG_STAT_FLASH_UPT)
396		dev_err(dev, "Flash update failed\n");
397	if (status & ADIS16260_DIAG_STAT_POWER_HIGH)
398		dev_err(dev, "Power supply above 5.25V\n");
399	if (status & ADIS16260_DIAG_STAT_POWER_LOW)
400		dev_err(dev, "Power supply below 4.75V\n");
401
402error_ret:
403	return ret;
404}
405
406static int adis16260_initial_setup(struct adis16260_state *st)
407{
408	int ret;
409	struct device *dev = &st->indio_dev->dev;
410
411	/* Disable IRQ */
412	ret = adis16260_set_irq(dev, false);
413	if (ret) {
414		dev_err(dev, "disable irq failed");
415		goto err_ret;
416	}
417
418	/* Do self test */
419	ret = adis16260_self_test(dev);
420	if (ret) {
421		dev_err(dev, "self test failure");
422		goto err_ret;
423	}
424
425	/* Read status register to check the result */
426	ret = adis16260_check_status(dev);
427	if (ret) {
428		adis16260_reset(dev);
429		dev_err(dev, "device not playing ball -> reset");
430		msleep(ADIS16260_STARTUP_DELAY);
431		ret = adis16260_check_status(dev);
432		if (ret) {
433			dev_err(dev, "giving up");
434			goto err_ret;
435		}
436	}
437
438	printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
439			st->us->chip_select, st->us->irq);
440
441err_ret:
442	return ret;
443}
444
445static IIO_DEV_ATTR_IN_NAMED_RAW(supply,
446				adis16260_read_12bit_unsigned,
447				ADIS16260_SUPPLY_OUT);
448static IIO_CONST_ATTR(in_supply_scale, "0.0018315");
449
450static IIO_DEV_ATTR_GYRO(adis16260_read_14bit_signed,
451		ADIS16260_GYRO_OUT);
452static IIO_DEV_ATTR_GYRO_SCALE(S_IWUSR | S_IRUGO,
453		adis16260_read_14bit_signed,
454		adis16260_write_16bit,
455		ADIS16260_GYRO_SCALE);
456static IIO_DEV_ATTR_GYRO_OFFSET(S_IWUSR | S_IRUGO,
457		adis16260_read_12bit_signed,
458		adis16260_write_16bit,
459		ADIS16260_GYRO_OFF);
460
461static IIO_DEV_ATTR_TEMP_RAW(adis16260_read_12bit_unsigned);
462static IIO_CONST_ATTR(temp_offset, "25");
463static IIO_CONST_ATTR(temp_scale, "0.1453");
464
465static IIO_DEV_ATTR_IN_RAW(0, adis16260_read_12bit_unsigned,
466		ADIS16260_AUX_ADC);
467static IIO_CONST_ATTR(in0_scale, "0.0006105");
468
469static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
470		adis16260_read_frequency,
471		adis16260_write_frequency);
472static IIO_DEV_ATTR_ANGL(adis16260_read_14bit_signed,
473		ADIS16260_ANGL_OUT);
474
475static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16260_write_reset, 0);
476
477static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("256 2048");
478
479static IIO_CONST_ATTR(name, "adis16260");
480
481static struct attribute *adis16260_event_attributes[] = {
482	NULL
483};
484
485static struct attribute_group adis16260_event_attribute_group = {
486	.attrs = adis16260_event_attributes,
487};
488
489static struct attribute *adis16260_attributes[] = {
490	&iio_dev_attr_in_supply_raw.dev_attr.attr,
491	&iio_const_attr_in_supply_scale.dev_attr.attr,
492	&iio_dev_attr_gyro_raw.dev_attr.attr,
493	&iio_dev_attr_gyro_scale.dev_attr.attr,
494	&iio_dev_attr_gyro_offset.dev_attr.attr,
495	&iio_dev_attr_angl_raw.dev_attr.attr,
496	&iio_dev_attr_temp_raw.dev_attr.attr,
497	&iio_const_attr_temp_offset.dev_attr.attr,
498	&iio_const_attr_temp_scale.dev_attr.attr,
499	&iio_dev_attr_in0_raw.dev_attr.attr,
500	&iio_const_attr_in0_scale.dev_attr.attr,
501	&iio_dev_attr_sampling_frequency.dev_attr.attr,
502	&iio_const_attr_available_sampling_frequency.dev_attr.attr,
503	&iio_dev_attr_reset.dev_attr.attr,
504	&iio_const_attr_name.dev_attr.attr,
505	NULL
506};
507
508static const struct attribute_group adis16260_attribute_group = {
509	.attrs = adis16260_attributes,
510};
511
512static int __devinit adis16260_probe(struct spi_device *spi)
513{
514	int ret, regdone = 0;
515	struct adis16260_state *st = kzalloc(sizeof *st, GFP_KERNEL);
516	if (!st) {
517		ret =  -ENOMEM;
518		goto error_ret;
519	}
520	/* this is only used for removal purposes */
521	spi_set_drvdata(spi, st);
522
523	/* Allocate the comms buffers */
524	st->rx = kzalloc(sizeof(*st->rx)*ADIS16260_MAX_RX, GFP_KERNEL);
525	if (st->rx == NULL) {
526		ret = -ENOMEM;
527		goto error_free_st;
528	}
529	st->tx = kzalloc(sizeof(*st->tx)*ADIS16260_MAX_TX, GFP_KERNEL);
530	if (st->tx == NULL) {
531		ret = -ENOMEM;
532		goto error_free_rx;
533	}
534	st->us = spi;
535	mutex_init(&st->buf_lock);
536	/* setup the industrialio driver allocated elements */
537	st->indio_dev = iio_allocate_device();
538	if (st->indio_dev == NULL) {
539		ret = -ENOMEM;
540		goto error_free_tx;
541	}
542
543	st->indio_dev->dev.parent = &spi->dev;
544	st->indio_dev->num_interrupt_lines = 1;
545	st->indio_dev->event_attrs = &adis16260_event_attribute_group;
546	st->indio_dev->attrs = &adis16260_attribute_group;
547	st->indio_dev->dev_data = (void *)(st);
548	st->indio_dev->driver_module = THIS_MODULE;
549	st->indio_dev->modes = INDIO_DIRECT_MODE;
550
551	ret = adis16260_configure_ring(st->indio_dev);
552	if (ret)
553		goto error_free_dev;
554
555	ret = iio_device_register(st->indio_dev);
556	if (ret)
557		goto error_unreg_ring_funcs;
558	regdone = 1;
559	ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
560	if (ret) {
561		printk(KERN_ERR "failed to initialize the ring\n");
562		goto error_unreg_ring_funcs;
563	}
564
565	if (spi->irq) {
566		ret = iio_register_interrupt_line(spi->irq,
567				st->indio_dev,
568				0,
569				IRQF_TRIGGER_RISING,
570				"adis16260");
571		if (ret)
572			goto error_uninitialize_ring;
573
574		ret = adis16260_probe_trigger(st->indio_dev);
575		if (ret)
576			goto error_unregister_line;
577	}
578
579	/* Get the device into a sane initial state */
580	ret = adis16260_initial_setup(st);
581	if (ret)
582		goto error_remove_trigger;
583	return 0;
584
585error_remove_trigger:
586	adis16260_remove_trigger(st->indio_dev);
587error_unregister_line:
588	if (spi->irq)
589		iio_unregister_interrupt_line(st->indio_dev, 0);
590error_uninitialize_ring:
591	iio_ring_buffer_unregister(st->indio_dev->ring);
592error_unreg_ring_funcs:
593	adis16260_unconfigure_ring(st->indio_dev);
594error_free_dev:
595	if (regdone)
596		iio_device_unregister(st->indio_dev);
597	else
598		iio_free_device(st->indio_dev);
599error_free_tx:
600	kfree(st->tx);
601error_free_rx:
602	kfree(st->rx);
603error_free_st:
604	kfree(st);
605error_ret:
606	return ret;
607}
608
609static int adis16260_remove(struct spi_device *spi)
610{
611	int ret;
612	struct adis16260_state *st = spi_get_drvdata(spi);
613	struct iio_dev *indio_dev = st->indio_dev;
614
615	ret = adis16260_stop_device(&(indio_dev->dev));
616	if (ret)
617		goto err_ret;
618
619	flush_scheduled_work();
620
621	adis16260_remove_trigger(indio_dev);
622	if (spi->irq)
623		iio_unregister_interrupt_line(indio_dev, 0);
624
625	iio_ring_buffer_unregister(st->indio_dev->ring);
626	iio_device_unregister(indio_dev);
627	adis16260_unconfigure_ring(indio_dev);
628	kfree(st->tx);
629	kfree(st->rx);
630	kfree(st);
631
632err_ret:
633	return ret;
634}
635
636static struct spi_driver adis16260_driver = {
637	.driver = {
638		.name = "adis16260",
639		.owner = THIS_MODULE,
640	},
641	.probe = adis16260_probe,
642	.remove = __devexit_p(adis16260_remove),
643};
644
645static __init int adis16260_init(void)
646{
647	return spi_register_driver(&adis16260_driver);
648}
649module_init(adis16260_init);
650
651static __exit void adis16260_exit(void)
652{
653	spi_unregister_driver(&adis16260_driver);
654}
655module_exit(adis16260_exit);
656
657MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
658MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
659MODULE_LICENSE("GPL v2");
660