• 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.36/drivers/staging/iio/imu/
1#include <linux/interrupt.h>
2#include <linux/irq.h>
3#include <linux/gpio.h>
4#include <linux/workqueue.h>
5#include <linux/mutex.h>
6#include <linux/device.h>
7#include <linux/kernel.h>
8#include <linux/spi/spi.h>
9#include <linux/slab.h>
10#include <linux/sysfs.h>
11#include <linux/list.h>
12
13#include "../iio.h"
14#include "../sysfs.h"
15#include "../ring_sw.h"
16#include "../accel/accel.h"
17#include "../trigger.h"
18#include "adis16350.h"
19
20static IIO_SCAN_EL_C(supply, ADIS16350_SCAN_SUPPLY, IIO_UNSIGNED(12),
21		ADIS16350_SUPPLY_OUT, NULL);
22
23static IIO_SCAN_EL_C(gyro_x, ADIS16350_SCAN_GYRO_X, IIO_SIGNED(14),
24		ADIS16350_XGYRO_OUT, NULL);
25static IIO_SCAN_EL_C(gyro_y, ADIS16350_SCAN_GYRO_Y, IIO_SIGNED(14),
26		ADIS16350_YGYRO_OUT, NULL);
27static IIO_SCAN_EL_C(gyro_z, ADIS16350_SCAN_GYRO_Z, IIO_SIGNED(14),
28		ADIS16350_ZGYRO_OUT, NULL);
29
30static IIO_SCAN_EL_C(accel_x, ADIS16350_SCAN_ACC_X, IIO_SIGNED(14),
31		ADIS16350_XACCL_OUT, NULL);
32static IIO_SCAN_EL_C(accel_y, ADIS16350_SCAN_ACC_Y, IIO_SIGNED(14),
33		ADIS16350_YACCL_OUT, NULL);
34static IIO_SCAN_EL_C(accel_z, ADIS16350_SCAN_ACC_Z, IIO_SIGNED(14),
35		ADIS16350_ZACCL_OUT, NULL);
36
37static IIO_SCAN_EL_C(temp_x, ADIS16350_SCAN_TEMP_X, IIO_SIGNED(12),
38		ADIS16350_XTEMP_OUT, NULL);
39static IIO_SCAN_EL_C(temp_y, ADIS16350_SCAN_TEMP_Y, IIO_SIGNED(12),
40		ADIS16350_YTEMP_OUT, NULL);
41static IIO_SCAN_EL_C(temp_z, ADIS16350_SCAN_TEMP_Z, IIO_SIGNED(12),
42		ADIS16350_ZTEMP_OUT, NULL);
43
44static IIO_SCAN_EL_C(adc_0, ADIS16350_SCAN_ADC_0, IIO_UNSIGNED(12),
45		ADIS16350_AUX_ADC, NULL);
46
47static IIO_SCAN_EL_TIMESTAMP(11);
48
49static struct attribute *adis16350_scan_el_attrs[] = {
50	&iio_scan_el_supply.dev_attr.attr,
51	&iio_scan_el_gyro_x.dev_attr.attr,
52	&iio_scan_el_gyro_y.dev_attr.attr,
53	&iio_scan_el_gyro_z.dev_attr.attr,
54	&iio_scan_el_accel_x.dev_attr.attr,
55	&iio_scan_el_accel_y.dev_attr.attr,
56	&iio_scan_el_accel_z.dev_attr.attr,
57	&iio_scan_el_temp_x.dev_attr.attr,
58	&iio_scan_el_temp_y.dev_attr.attr,
59	&iio_scan_el_temp_z.dev_attr.attr,
60	&iio_scan_el_adc_0.dev_attr.attr,
61	&iio_scan_el_timestamp.dev_attr.attr,
62	NULL,
63};
64
65static struct attribute_group adis16350_scan_el_group = {
66	.attrs = adis16350_scan_el_attrs,
67	.name = "scan_elements",
68};
69
70/**
71 * adis16350_poll_func_th() top half interrupt handler called by trigger
72 * @private_data:	iio_dev
73 **/
74static void adis16350_poll_func_th(struct iio_dev *indio_dev, s64 time)
75{
76	struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
77	st->last_timestamp = time;
78	schedule_work(&st->work_trigger_to_ring);
79}
80
81/**
82 * adis16350_spi_read_burst() - read all data registers
83 * @dev: device associated with child of actual device (iio_dev or iio_trig)
84 * @rx: somewhere to pass back the value read (min size is 24 bytes)
85 **/
86static int adis16350_spi_read_burst(struct device *dev, u8 *rx)
87{
88	struct spi_message msg;
89	struct iio_dev *indio_dev = dev_get_drvdata(dev);
90	struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
91	u32 old_speed_hz = st->us->max_speed_hz;
92	int ret;
93
94	struct spi_transfer xfers[] = {
95		{
96			.tx_buf = st->tx,
97			.bits_per_word = 8,
98			.len = 2,
99			.cs_change = 0,
100		}, {
101			.rx_buf = rx,
102			.bits_per_word = 8,
103			.len = 22,
104			.cs_change = 0,
105		},
106	};
107
108	mutex_lock(&st->buf_lock);
109	st->tx[0] = ADIS16350_READ_REG(ADIS16350_GLOB_CMD);
110	st->tx[1] = 0;
111
112	spi_message_init(&msg);
113	spi_message_add_tail(&xfers[0], &msg);
114	spi_message_add_tail(&xfers[1], &msg);
115
116	st->us->max_speed_hz = ADIS16350_SPI_BURST;
117	spi_setup(st->us);
118
119	ret = spi_sync(st->us, &msg);
120	if (ret)
121		dev_err(&st->us->dev, "problem when burst reading");
122
123	st->us->max_speed_hz = old_speed_hz;
124	spi_setup(st->us);
125	mutex_unlock(&st->buf_lock);
126	return ret;
127}
128
129/* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
130 * specific to be rolled into the core.
131 */
132static void adis16350_trigger_bh_to_ring(struct work_struct *work_s)
133{
134	struct adis16350_state *st
135		= container_of(work_s, struct adis16350_state,
136			       work_trigger_to_ring);
137
138	int i = 0;
139	s16 *data;
140	size_t datasize = st->indio_dev
141		->ring->access.get_bpd(st->indio_dev->ring);
142
143	data = kmalloc(datasize , GFP_KERNEL);
144	if (data == NULL) {
145		dev_err(&st->us->dev, "memory alloc failed in ring bh");
146		return;
147	}
148
149	if (st->indio_dev->scan_count)
150		if (adis16350_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
151			for (; i < st->indio_dev->scan_count; i++)
152				data[i] = be16_to_cpup(
153					(__be16 *)&(st->rx[i*2]));
154
155	/* Guaranteed to be aligned with 8 byte boundary */
156	if (st->indio_dev->scan_timestamp)
157		*((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
158
159	st->indio_dev->ring->access.store_to(st->indio_dev->ring,
160					    (u8 *)data,
161					    st->last_timestamp);
162
163	iio_trigger_notify_done(st->indio_dev->trig);
164	kfree(data);
165
166	return;
167}
168
169void adis16350_unconfigure_ring(struct iio_dev *indio_dev)
170{
171	kfree(indio_dev->pollfunc);
172	iio_sw_rb_free(indio_dev->ring);
173}
174
175int adis16350_configure_ring(struct iio_dev *indio_dev)
176{
177	int ret = 0;
178	struct adis16350_state *st = indio_dev->dev_data;
179	struct iio_ring_buffer *ring;
180	INIT_WORK(&st->work_trigger_to_ring, adis16350_trigger_bh_to_ring);
181	/* Set default scan mode */
182
183	iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
184	iio_scan_mask_set(indio_dev, iio_scan_el_gyro_x.number);
185	iio_scan_mask_set(indio_dev, iio_scan_el_gyro_y.number);
186	iio_scan_mask_set(indio_dev, iio_scan_el_gyro_z.number);
187	iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
188	iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
189	iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
190	iio_scan_mask_set(indio_dev, iio_scan_el_temp_x.number);
191	iio_scan_mask_set(indio_dev, iio_scan_el_temp_y.number);
192	iio_scan_mask_set(indio_dev, iio_scan_el_temp_z.number);
193	iio_scan_mask_set(indio_dev, iio_scan_el_adc_0.number);
194	indio_dev->scan_timestamp = true;
195
196	indio_dev->scan_el_attrs = &adis16350_scan_el_group;
197
198	ring = iio_sw_rb_allocate(indio_dev);
199	if (!ring) {
200		ret = -ENOMEM;
201		return ret;
202	}
203	indio_dev->ring = ring;
204	/* Effectively select the ring buffer implementation */
205	iio_ring_sw_register_funcs(&ring->access);
206	ring->bpe = 2;
207	ring->preenable = &iio_sw_ring_preenable;
208	ring->postenable = &iio_triggered_ring_postenable;
209	ring->predisable = &iio_triggered_ring_predisable;
210	ring->owner = THIS_MODULE;
211
212	ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16350_poll_func_th);
213	if (ret)
214		goto error_iio_sw_rb_free;
215
216	indio_dev->modes |= INDIO_RING_TRIGGERED;
217	return 0;
218
219error_iio_sw_rb_free:
220	iio_sw_rb_free(indio_dev->ring);
221	return ret;
222}
223