• 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 "adis16400.h"
19
20static IIO_SCAN_EL_C(supply, ADIS16400_SCAN_SUPPLY, IIO_SIGNED(14),
21		     ADIS16400_SUPPLY_OUT, NULL);
22
23static IIO_SCAN_EL_C(gyro_x, ADIS16400_SCAN_GYRO_X, IIO_SIGNED(14),
24		     ADIS16400_XGYRO_OUT, NULL);
25static IIO_SCAN_EL_C(gyro_y, ADIS16400_SCAN_GYRO_Y, IIO_SIGNED(14),
26		     ADIS16400_YGYRO_OUT, NULL);
27static IIO_SCAN_EL_C(gyro_z, ADIS16400_SCAN_GYRO_Z, IIO_SIGNED(14),
28		     ADIS16400_ZGYRO_OUT, NULL);
29
30static IIO_SCAN_EL_C(accel_x, ADIS16400_SCAN_ACC_X, IIO_SIGNED(14),
31		     ADIS16400_XACCL_OUT, NULL);
32static IIO_SCAN_EL_C(accel_y, ADIS16400_SCAN_ACC_Y, IIO_SIGNED(14),
33		     ADIS16400_YACCL_OUT, NULL);
34static IIO_SCAN_EL_C(accel_z, ADIS16400_SCAN_ACC_Z, IIO_SIGNED(14),
35		     ADIS16400_ZACCL_OUT, NULL);
36
37static IIO_SCAN_EL_C(magn_x, ADIS16400_SCAN_MAGN_X, IIO_SIGNED(14),
38		     ADIS16400_XMAGN_OUT, NULL);
39static IIO_SCAN_EL_C(magn_y, ADIS16400_SCAN_MAGN_Y, IIO_SIGNED(14),
40		     ADIS16400_YMAGN_OUT, NULL);
41static IIO_SCAN_EL_C(magn_z, ADIS16400_SCAN_MAGN_Z, IIO_SIGNED(14),
42		     ADIS16400_ZMAGN_OUT, NULL);
43
44static IIO_SCAN_EL_C(temp, ADIS16400_SCAN_TEMP, IIO_SIGNED(12),
45		     ADIS16400_TEMP_OUT, NULL);
46static IIO_SCAN_EL_C(adc_0, ADIS16400_SCAN_ADC_0, IIO_SIGNED(12),
47		     ADIS16400_AUX_ADC, NULL);
48
49static IIO_SCAN_EL_TIMESTAMP(12);
50
51static struct attribute *adis16400_scan_el_attrs[] = {
52	&iio_scan_el_supply.dev_attr.attr,
53	&iio_scan_el_gyro_x.dev_attr.attr,
54	&iio_scan_el_gyro_y.dev_attr.attr,
55	&iio_scan_el_gyro_z.dev_attr.attr,
56	&iio_scan_el_accel_x.dev_attr.attr,
57	&iio_scan_el_accel_y.dev_attr.attr,
58	&iio_scan_el_accel_z.dev_attr.attr,
59	&iio_scan_el_magn_x.dev_attr.attr,
60	&iio_scan_el_magn_y.dev_attr.attr,
61	&iio_scan_el_magn_z.dev_attr.attr,
62	&iio_scan_el_temp.dev_attr.attr,
63	&iio_scan_el_adc_0.dev_attr.attr,
64	&iio_scan_el_timestamp.dev_attr.attr,
65	NULL,
66};
67
68static struct attribute_group adis16400_scan_el_group = {
69	.attrs = adis16400_scan_el_attrs,
70	.name = "scan_elements",
71};
72
73/**
74 * adis16400_poll_func_th() top half interrupt handler called by trigger
75 * @private_data:	iio_dev
76 **/
77static void adis16400_poll_func_th(struct iio_dev *indio_dev, s64 time)
78{
79	struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
80	st->last_timestamp = time;
81	schedule_work(&st->work_trigger_to_ring);
82	/* Indicate that this interrupt is being handled */
83
84	/* Technically this is trigger related, but without this
85	 * handler running there is currently no way for the interrupt
86	 * to clear.
87	 */
88}
89
90/**
91 * adis16400_spi_read_burst() - read all data registers
92 * @dev: device associated with child of actual device (iio_dev or iio_trig)
93 * @rx: somewhere to pass back the value read (min size is 24 bytes)
94 **/
95static int adis16400_spi_read_burst(struct device *dev, u8 *rx)
96{
97	struct spi_message msg;
98	struct iio_dev *indio_dev = dev_get_drvdata(dev);
99	struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
100	u32 old_speed_hz = st->us->max_speed_hz;
101	int ret;
102
103	struct spi_transfer xfers[] = {
104		{
105			.tx_buf = st->tx,
106			.bits_per_word = 8,
107			.len = 2,
108			.cs_change = 0,
109		}, {
110			.rx_buf = rx,
111			.bits_per_word = 8,
112			.len = 24,
113			.cs_change = 1,
114		},
115	};
116
117	mutex_lock(&st->buf_lock);
118	st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD);
119	st->tx[1] = 0;
120
121	spi_message_init(&msg);
122	spi_message_add_tail(&xfers[0], &msg);
123	spi_message_add_tail(&xfers[1], &msg);
124
125	st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz);
126	spi_setup(st->us);
127
128	ret = spi_sync(st->us, &msg);
129	if (ret)
130		dev_err(&st->us->dev, "problem when burst reading");
131
132	st->us->max_speed_hz = old_speed_hz;
133	spi_setup(st->us);
134	mutex_unlock(&st->buf_lock);
135	return ret;
136}
137
138/* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
139 * specific to be rolled into the core.
140 */
141static void adis16400_trigger_bh_to_ring(struct work_struct *work_s)
142{
143	struct adis16400_state *st
144		= container_of(work_s, struct adis16400_state,
145			       work_trigger_to_ring);
146
147	int i = 0;
148	s16 *data;
149	size_t datasize = st->indio_dev
150		->ring->access.get_bpd(st->indio_dev->ring);
151
152	data = kmalloc(datasize , GFP_KERNEL);
153	if (data == NULL) {
154		dev_err(&st->us->dev, "memory alloc failed in ring bh");
155		return;
156	}
157
158	if (st->indio_dev->scan_count)
159		if (adis16400_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
160			for (; i < st->indio_dev->scan_count; i++)
161				data[i]	= be16_to_cpup(
162					(__be16 *)&(st->rx[i*2]));
163
164	/* Guaranteed to be aligned with 8 byte boundary */
165	if (st->indio_dev->scan_timestamp)
166		*((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
167
168	st->indio_dev->ring->access.store_to(st->indio_dev->ring,
169					    (u8 *)data,
170					    st->last_timestamp);
171
172	iio_trigger_notify_done(st->indio_dev->trig);
173	kfree(data);
174
175	return;
176}
177
178void adis16400_unconfigure_ring(struct iio_dev *indio_dev)
179{
180	kfree(indio_dev->pollfunc);
181	iio_sw_rb_free(indio_dev->ring);
182}
183
184int adis16400_configure_ring(struct iio_dev *indio_dev)
185{
186	int ret = 0;
187	struct adis16400_state *st = indio_dev->dev_data;
188	struct iio_ring_buffer *ring;
189	INIT_WORK(&st->work_trigger_to_ring, adis16400_trigger_bh_to_ring);
190	/* Set default scan mode */
191
192	iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
193	iio_scan_mask_set(indio_dev, iio_scan_el_gyro_x.number);
194	iio_scan_mask_set(indio_dev, iio_scan_el_gyro_y.number);
195	iio_scan_mask_set(indio_dev, iio_scan_el_gyro_z.number);
196	iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
197	iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
198	iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
199	iio_scan_mask_set(indio_dev, iio_scan_el_magn_x.number);
200	iio_scan_mask_set(indio_dev, iio_scan_el_magn_y.number);
201	iio_scan_mask_set(indio_dev, iio_scan_el_magn_z.number);
202	iio_scan_mask_set(indio_dev, iio_scan_el_temp.number);
203	iio_scan_mask_set(indio_dev, iio_scan_el_adc_0.number);
204	indio_dev->scan_timestamp = true;
205
206	indio_dev->scan_el_attrs = &adis16400_scan_el_group;
207
208	ring = iio_sw_rb_allocate(indio_dev);
209	if (!ring) {
210		ret = -ENOMEM;
211		return ret;
212	}
213	indio_dev->ring = ring;
214	/* Effectively select the ring buffer implementation */
215	iio_ring_sw_register_funcs(&ring->access);
216	ring->bpe = 2;
217	ring->preenable = &iio_sw_ring_preenable;
218	ring->postenable = &iio_triggered_ring_postenable;
219	ring->predisable = &iio_triggered_ring_predisable;
220	ring->owner = THIS_MODULE;
221
222	ret = iio_alloc_pollfunc(indio_dev, NULL, &adis16400_poll_func_th);
223	if (ret)
224		goto error_iio_sw_rb_free;
225
226	indio_dev->modes |= INDIO_RING_TRIGGERED;
227	return 0;
228
229error_iio_sw_rb_free:
230	iio_sw_rb_free(indio_dev->ring);
231	return ret;
232}
233