aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/staging/iio/imu/Kconfig10
-rw-r--r--drivers/staging/iio/imu/Makefile4
-rw-r--r--drivers/staging/iio/imu/adis16400.h238
-rw-r--r--drivers/staging/iio/imu/adis16400_core.c849
-rw-r--r--drivers/staging/iio/imu/adis16400_ring.c245
-rw-r--r--drivers/staging/iio/imu/adis16400_trigger.c127
-rw-r--r--drivers/staging/iio/magnetometer/magnet.h31
7 files changed, 1504 insertions, 0 deletions
diff --git a/drivers/staging/iio/imu/Kconfig b/drivers/staging/iio/imu/Kconfig
index 3fe47161e76..411804f11a7 100644
--- a/drivers/staging/iio/imu/Kconfig
+++ b/drivers/staging/iio/imu/Kconfig
@@ -12,3 +12,13 @@ config ADIS16300
12 help 12 help
13 Say yes here to build support for Analog Devices adis16300 four degrees 13 Say yes here to build support for Analog Devices adis16300 four degrees
14 of freedom inertial sensor. 14 of freedom inertial sensor.
15
16config ADIS16400
17 tristate "Analog Devices ADIS16400/5 IMU SPI driver"
18 depends on SPI
19 select IIO_SW_RING
20 select IIO_RING_BUFFER
21 select IIO_TRIGGER
22 help
23 Say yes here to build support for Analog Devices adis16400/5 triaxial
24 inertial sensor with Magnetometer. \ No newline at end of file
diff --git a/drivers/staging/iio/imu/Makefile b/drivers/staging/iio/imu/Makefile
index 4cd5984137b..de454dd08c6 100644
--- a/drivers/staging/iio/imu/Makefile
+++ b/drivers/staging/iio/imu/Makefile
@@ -4,3 +4,7 @@
4adis16300-y := adis16300_core.o 4adis16300-y := adis16300_core.o
5adis16300-$(CONFIG_IIO_RING_BUFFER) += adis16300_ring.o adis16300_trigger.o 5adis16300-$(CONFIG_IIO_RING_BUFFER) += adis16300_ring.o adis16300_trigger.o
6obj-$(CONFIG_ADIS16300) += adis16300.o 6obj-$(CONFIG_ADIS16300) += adis16300.o
7
8adis16400-y := adis16400_core.o
9adis16400-$(CONFIG_IIO_RING_BUFFER) += adis16400_ring.o adis16400_trigger.o
10obj-$(CONFIG_ADIS16400) += adis16400.o \ No newline at end of file
diff --git a/drivers/staging/iio/imu/adis16400.h b/drivers/staging/iio/imu/adis16400.h
new file mode 100644
index 00000000000..a8062f60579
--- /dev/null
+++ b/drivers/staging/iio/imu/adis16400.h
@@ -0,0 +1,238 @@
1/*
2 * adis16400.h support Analog Devices ADIS16400
3 * 3d 18g accelerometers,
4 * 3d gyroscopes,
5 * 3d 2.5gauss magnetometers via SPI
6 *
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@cam.ac.uk>
9 *
10 * Loosely based upon lis3l02dq.h
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#ifndef SPI_ADIS16400_H_
18#define SPI_ADIS16400_H_
19
20#define ADIS16400_STARTUP_DELAY 220 /* ms */
21
22#define ADIS16400_READ_REG(a) a
23#define ADIS16400_WRITE_REG(a) ((a) | 0x80)
24
25#define ADIS16400_FLASH_CNT 0x00 /* Flash memory write count */
26#define ADIS16400_SUPPLY_OUT 0x02 /* Power supply measurement */
27#define ADIS16400_XGYRO_OUT 0x04 /* X-axis gyroscope output */
28#define ADIS16400_YGYRO_OUT 0x06 /* Y-axis gyroscope output */
29#define ADIS16400_ZGYRO_OUT 0x08 /* Z-axis gyroscope output */
30#define ADIS16400_XACCL_OUT 0x0A /* X-axis accelerometer output */
31#define ADIS16400_YACCL_OUT 0x0C /* Y-axis accelerometer output */
32#define ADIS16400_ZACCL_OUT 0x0E /* Z-axis accelerometer output */
33#define ADIS16400_XMAGN_OUT 0x10 /* X-axis magnetometer measurement */
34#define ADIS16400_YMAGN_OUT 0x12 /* Y-axis magnetometer measurement */
35#define ADIS16400_ZMAGN_OUT 0x14 /* Z-axis magnetometer measurement */
36#define ADIS16400_TEMP_OUT 0x16 /* Temperature output */
37#define ADIS16400_AUX_ADC 0x18 /* Auxiliary ADC measurement */
38
39/* Calibration parameters */
40#define ADIS16400_XGYRO_OFF 0x1A /* X-axis gyroscope bias offset factor */
41#define ADIS16400_YGYRO_OFF 0x1C /* Y-axis gyroscope bias offset factor */
42#define ADIS16400_ZGYRO_OFF 0x1E /* Z-axis gyroscope bias offset factor */
43#define ADIS16400_XACCL_OFF 0x20 /* X-axis acceleration bias offset factor */
44#define ADIS16400_YACCL_OFF 0x22 /* Y-axis acceleration bias offset factor */
45#define ADIS16400_ZACCL_OFF 0x24 /* Z-axis acceleration bias offset factor */
46#define ADIS16400_XMAGN_HIF 0x26 /* X-axis magnetometer, hard-iron factor */
47#define ADIS16400_YMAGN_HIF 0x28 /* Y-axis magnetometer, hard-iron factor */
48#define ADIS16400_ZMAGN_HIF 0x2A /* Z-axis magnetometer, hard-iron factor */
49#define ADIS16400_XMAGN_SIF 0x2C /* X-axis magnetometer, soft-iron factor */
50#define ADIS16400_YMAGN_SIF 0x2E /* Y-axis magnetometer, soft-iron factor */
51#define ADIS16400_ZMAGN_SIF 0x30 /* Z-axis magnetometer, soft-iron factor */
52
53#define ADIS16400_GPIO_CTRL 0x32 /* Auxiliary digital input/output control */
54#define ADIS16400_MSC_CTRL 0x34 /* Miscellaneous control */
55#define ADIS16400_SMPL_PRD 0x36 /* Internal sample period (rate) control */
56#define ADIS16400_SENS_AVG 0x38 /* Dynamic range and digital filter control */
57#define ADIS16400_SLP_CNT 0x3A /* Sleep mode control */
58#define ADIS16400_DIAG_STAT 0x3C /* System status */
59
60/* Alarm functions */
61#define ADIS16400_GLOB_CMD 0x3E /* System command */
62#define ADIS16400_ALM_MAG1 0x40 /* Alarm 1 amplitude threshold */
63#define ADIS16400_ALM_MAG2 0x42 /* Alarm 2 amplitude threshold */
64#define ADIS16400_ALM_SMPL1 0x44 /* Alarm 1 sample size */
65#define ADIS16400_ALM_SMPL2 0x46 /* Alarm 2 sample size */
66#define ADIS16400_ALM_CTRL 0x48 /* Alarm control */
67#define ADIS16400_AUX_DAC 0x4A /* Auxiliary DAC data */
68
69#define ADIS16400_PRODUCT_ID 0x56 /* Product identifier */
70#define ADIS16400_PRODUCT_ID_DEFAULT 0x4015 /* Datasheet says 0x4105, I get 0x4015 */
71
72#define ADIS16400_ERROR_ACTIVE (1<<14)
73#define ADIS16400_NEW_DATA (1<<14)
74
75/* MSC_CTRL */
76#define ADIS16400_MSC_CTRL_MEM_TEST (1<<11)
77#define ADIS16400_MSC_CTRL_INT_SELF_TEST (1<<10)
78#define ADIS16400_MSC_CTRL_NEG_SELF_TEST (1<<9)
79#define ADIS16400_MSC_CTRL_POS_SELF_TEST (1<<8)
80#define ADIS16400_MSC_CTRL_GYRO_BIAS (1<<7)
81#define ADIS16400_MSC_CTRL_ACCL_ALIGN (1<<6)
82#define ADIS16400_MSC_CTRL_DATA_RDY_EN (1<<2)
83#define ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH (1<<1)
84#define ADIS16400_MSC_CTRL_DATA_RDY_DIO2 (1<<0)
85
86/* SMPL_PRD */
87#define ADIS16400_SMPL_PRD_TIME_BASE (1<<7)
88#define ADIS16400_SMPL_PRD_DIV_MASK 0x7F
89
90/* DIAG_STAT */
91#define ADIS16400_DIAG_STAT_ZACCL_FAIL (1<<15)
92#define ADIS16400_DIAG_STAT_YACCL_FAIL (1<<14)
93#define ADIS16400_DIAG_STAT_XACCL_FAIL (1<<13)
94#define ADIS16400_DIAG_STAT_XGYRO_FAIL (1<<12)
95#define ADIS16400_DIAG_STAT_YGYRO_FAIL (1<<11)
96#define ADIS16400_DIAG_STAT_ZGYRO_FAIL (1<<10)
97#define ADIS16400_DIAG_STAT_ALARM2 (1<<9)
98#define ADIS16400_DIAG_STAT_ALARM1 (1<<8)
99#define ADIS16400_DIAG_STAT_FLASH_CHK (1<<6)
100#define ADIS16400_DIAG_STAT_SELF_TEST (1<<5)
101#define ADIS16400_DIAG_STAT_OVERFLOW (1<<4)
102#define ADIS16400_DIAG_STAT_SPI_FAIL (1<<3)
103#define ADIS16400_DIAG_STAT_FLASH_UPT (1<<2)
104#define ADIS16400_DIAG_STAT_POWER_HIGH (1<<1)
105#define ADIS16400_DIAG_STAT_POWER_LOW (1<<0)
106
107/* GLOB_CMD */
108#define ADIS16400_GLOB_CMD_SW_RESET (1<<7)
109#define ADIS16400_GLOB_CMD_P_AUTO_NULL (1<<4)
110#define ADIS16400_GLOB_CMD_FLASH_UPD (1<<3)
111#define ADIS16400_GLOB_CMD_DAC_LATCH (1<<2)
112#define ADIS16400_GLOB_CMD_FAC_CALIB (1<<1)
113#define ADIS16400_GLOB_CMD_AUTO_NULL (1<<0)
114
115/* SLP_CNT */
116#define ADIS16400_SLP_CNT_POWER_OFF (1<<8)
117
118#define ADIS16400_MAX_TX 24
119#define ADIS16400_MAX_RX 24
120
121#define ADIS16400_SPI_SLOW (u32)(300 * 1000)
122#define ADIS16400_SPI_BURST (u32)(1000 * 1000)
123#define ADIS16400_SPI_FAST (u32)(2000 * 1000)
124
125/**
126 * struct adis16400_state - device instance specific data
127 * @us: actual spi_device
128 * @work_trigger_to_ring: bh for triggered event handling
129 * @work_cont_thresh: CLEAN
130 * @inter: used to check if new interrupt has been triggered
131 * @last_timestamp: passing timestamp from th to bh of interrupt handler
132 * @indio_dev: industrial I/O device structure
133 * @trig: data ready trigger registered with iio
134 * @tx: transmit buffer
135 * @rx: recieve buffer
136 * @buf_lock: mutex to protect tx and rx
137 **/
138struct adis16400_state {
139 struct spi_device *us;
140 struct work_struct work_trigger_to_ring;
141 struct iio_work_cont work_cont_thresh;
142 s64 last_timestamp;
143 struct iio_dev *indio_dev;
144 struct iio_trigger *trig;
145 u8 *tx;
146 u8 *rx;
147 struct mutex buf_lock;
148};
149
150int adis16400_spi_write_reg_8(struct device *dev,
151 u8 reg_address,
152 u8 val);
153
154int adis16400_spi_read_burst(struct device *dev, u8 *rx);
155
156int adis16400_spi_read_sequence(struct device *dev,
157 u8 *tx, u8 *rx, int num);
158
159int adis16400_set_irq(struct device *dev, bool enable);
160
161int adis16400_reset(struct device *dev);
162
163int adis16400_stop_device(struct device *dev);
164
165int adis16400_check_status(struct device *dev);
166
167#ifdef CONFIG_IIO_RING_BUFFER
168/* At the moment triggers are only used for ring buffer
169 * filling. This may change!
170 */
171
172enum adis16400_scan {
173 ADIS16400_SCAN_SUPPLY,
174 ADIS16400_SCAN_GYRO_X,
175 ADIS16400_SCAN_GYRO_Y,
176 ADIS16400_SCAN_GYRO_Z,
177 ADIS16400_SCAN_ACC_X,
178 ADIS16400_SCAN_ACC_Y,
179 ADIS16400_SCAN_ACC_Z,
180 ADIS16400_SCAN_MAGN_X,
181 ADIS16400_SCAN_MAGN_Y,
182 ADIS16400_SCAN_MAGN_Z,
183 ADIS16400_SCAN_TEMP,
184 ADIS16400_SCAN_ADC_0
185};
186
187void adis16400_remove_trigger(struct iio_dev *indio_dev);
188int adis16400_probe_trigger(struct iio_dev *indio_dev);
189
190ssize_t adis16400_read_data_from_ring(struct device *dev,
191 struct device_attribute *attr,
192 char *buf);
193
194
195int adis16400_configure_ring(struct iio_dev *indio_dev);
196void adis16400_unconfigure_ring(struct iio_dev *indio_dev);
197
198int adis16400_initialize_ring(struct iio_ring_buffer *ring);
199void adis16400_uninitialize_ring(struct iio_ring_buffer *ring);
200#else /* CONFIG_IIO_RING_BUFFER */
201
202static inline void adis16400_remove_trigger(struct iio_dev *indio_dev)
203{
204}
205
206static inline int adis16400_probe_trigger(struct iio_dev *indio_dev)
207{
208 return 0;
209}
210
211static inline ssize_t
212adis16400_read_data_from_ring(struct device *dev,
213 struct device_attribute *attr,
214 char *buf)
215{
216 return 0;
217}
218
219static int adis16400_configure_ring(struct iio_dev *indio_dev)
220{
221 return 0;
222}
223
224static inline void adis16400_unconfigure_ring(struct iio_dev *indio_dev)
225{
226}
227
228static inline int adis16400_initialize_ring(struct iio_ring_buffer *ring)
229{
230 return 0;
231}
232
233static inline void adis16400_uninitialize_ring(struct iio_ring_buffer *ring)
234{
235}
236
237#endif /* CONFIG_IIO_RING_BUFFER */
238#endif /* SPI_ADIS16400_H_ */
diff --git a/drivers/staging/iio/imu/adis16400_core.c b/drivers/staging/iio/imu/adis16400_core.c
new file mode 100644
index 00000000000..27e4a734807
--- /dev/null
+++ b/drivers/staging/iio/imu/adis16400_core.c
@@ -0,0 +1,849 @@
1/*
2 * adis16400.c support Analog Devices ADIS16400/5
3 * 3d 2g Linear Accelerometers,
4 * 3d Gyroscopes,
5 * 3d Magnetometers via SPI
6 *
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@cam.ac.uk>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/gpio.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/spi/spi.h>
24
25#include <linux/sysfs.h>
26#include <linux/list.h>
27
28#include "../iio.h"
29#include "../sysfs.h"
30#include "../accel/accel.h"
31#include "../adc/adc.h"
32#include "../gyro/gyro.h"
33#include "../magnetometer/magnet.h"
34
35#include "adis16400.h"
36
37#define DRIVER_NAME "adis16400"
38
39/* At the moment the spi framework doesn't allow global setting of cs_change.
40 * It's in the likely to be added comment at the top of spi.h.
41 * This means that use cannot be made of spi_write etc.
42 */
43
44/**
45 * adis16400_spi_write_reg_8() - write single byte to a register
46 * @dev: device associated with child of actual device (iio_dev or iio_trig)
47 * @reg_address: the address of the register to be written
48 * @val: the value to write
49 **/
50int adis16400_spi_write_reg_8(struct device *dev,
51 u8 reg_address,
52 u8 val)
53{
54 int ret;
55 struct iio_dev *indio_dev = dev_get_drvdata(dev);
56 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
57
58 mutex_lock(&st->buf_lock);
59 st->tx[0] = ADIS16400_WRITE_REG(reg_address);
60 st->tx[1] = val;
61
62 ret = spi_write(st->us, st->tx, 2);
63 mutex_unlock(&st->buf_lock);
64
65 return ret;
66}
67
68/**
69 * adis16400_spi_write_reg_16() - write 2 bytes to a pair of registers
70 * @dev: device associated with child of actual device (iio_dev or iio_trig)
71 * @reg_address: the address of the lower of the two registers. Second register
72 * is assumed to have address one greater.
73 * @val: value to be written
74 **/
75static int adis16400_spi_write_reg_16(struct device *dev,
76 u8 lower_reg_address,
77 u16 value)
78{
79 int ret;
80 struct spi_message msg;
81 struct iio_dev *indio_dev = dev_get_drvdata(dev);
82 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
83 struct spi_transfer xfers[] = {
84 {
85 .tx_buf = st->tx,
86 .bits_per_word = 8,
87 .len = 2,
88 .cs_change = 1,
89 }, {
90 .tx_buf = st->tx + 2,
91 .bits_per_word = 8,
92 .len = 2,
93 .cs_change = 1,
94 },
95 };
96
97 mutex_lock(&st->buf_lock);
98 st->tx[0] = ADIS16400_WRITE_REG(lower_reg_address);
99 st->tx[1] = value & 0xFF;
100 st->tx[2] = ADIS16400_WRITE_REG(lower_reg_address + 1);
101 st->tx[3] = (value >> 8) & 0xFF;
102
103 spi_message_init(&msg);
104 spi_message_add_tail(&xfers[0], &msg);
105 spi_message_add_tail(&xfers[1], &msg);
106 ret = spi_sync(st->us, &msg);
107 mutex_unlock(&st->buf_lock);
108
109 return ret;
110}
111
112/**
113 * adis16400_spi_read_reg_16() - read 2 bytes from a 16-bit register
114 * @dev: device associated with child of actual device (iio_dev or iio_trig)
115 * @reg_address: the address of the lower of the two registers. Second register
116 * is assumed to have address one greater.
117 * @val: somewhere to pass back the value read
118 **/
119static int adis16400_spi_read_reg_16(struct device *dev,
120 u8 lower_reg_address,
121 u16 *val)
122{
123 struct spi_message msg;
124 struct iio_dev *indio_dev = dev_get_drvdata(dev);
125 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
126 int ret;
127 struct spi_transfer xfers[] = {
128 {
129 .tx_buf = st->tx,
130 .bits_per_word = 8,
131 .len = 2,
132 .cs_change = 1,
133 }, {
134 .rx_buf = st->rx,
135 .bits_per_word = 8,
136 .len = 2,
137 .cs_change = 1,
138 },
139 };
140
141 mutex_lock(&st->buf_lock);
142 st->tx[0] = ADIS16400_READ_REG(lower_reg_address);
143 st->tx[1] = 0;
144 st->tx[2] = 0;
145 st->tx[3] = 0;
146
147 spi_message_init(&msg);
148 spi_message_add_tail(&xfers[0], &msg);
149 spi_message_add_tail(&xfers[1], &msg);
150 ret = spi_sync(st->us, &msg);
151 if (ret) {
152 dev_err(&st->us->dev,
153 "problem when reading 16 bit register 0x%02X",
154 lower_reg_address);
155 goto error_ret;
156 }
157 *val = (st->rx[0] << 8) | st->rx[1];
158
159error_ret:
160 mutex_unlock(&st->buf_lock);
161 return ret;
162}
163
164/**
165 * adis16400_spi_read_burst() - read all data registers
166 * @dev: device associated with child of actual device (iio_dev or iio_trig)
167 * @rx: somewhere to pass back the value read (min size is 24 bytes)
168 **/
169int adis16400_spi_read_burst(struct device *dev, u8 *rx)
170{
171 struct spi_message msg;
172 struct iio_dev *indio_dev = dev_get_drvdata(dev);
173 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
174 u32 old_speed_hz = st->us->max_speed_hz;
175 int ret;
176
177 struct spi_transfer xfers[] = {
178 {
179 .tx_buf = st->tx,
180 .bits_per_word = 8,
181 .len = 2,
182 .cs_change = 0,
183 }, {
184 .rx_buf = rx,
185 .bits_per_word = 8,
186 .len = 24,
187 .cs_change = 1,
188 },
189 };
190
191 mutex_lock(&st->buf_lock);
192 st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD);
193 st->tx[1] = 0;
194
195 spi_message_init(&msg);
196 spi_message_add_tail(&xfers[0], &msg);
197 spi_message_add_tail(&xfers[1], &msg);
198
199 st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz);
200 spi_setup(st->us);
201
202 ret = spi_sync(st->us, &msg);
203 if (ret)
204 dev_err(&st->us->dev, "problem when burst reading");
205
206 st->us->max_speed_hz = old_speed_hz;
207 spi_setup(st->us);
208 mutex_unlock(&st->buf_lock);
209 return ret;
210}
211
212/**
213 * adis16400_spi_read_sequence() - read a sequence of 16-bit registers
214 * @dev: device associated with child of actual device (iio_dev or iio_trig)
215 * @tx: register addresses in bytes 0,2,4,6... (min size is 2*num bytes)
216 * @rx: somewhere to pass back the value read (min size is 2*num bytes)
217 **/
218int adis16400_spi_read_sequence(struct device *dev,
219 u8 *tx, u8 *rx, int num)
220{
221 struct spi_message msg;
222 struct spi_transfer *xfers;
223 struct iio_dev *indio_dev = dev_get_drvdata(dev);
224 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
225 int ret, i;
226
227 xfers = kzalloc(num + 1, GFP_KERNEL);
228 if (xfers == NULL) {
229 dev_err(&st->us->dev, "memory alloc failed");
230 ret = -ENOMEM;
231 goto error_ret;
232 }
233
234 /* tx: |add1|addr2|addr3|...|addrN |zero|
235 * rx: |zero|res1 |res2 |...|resN-1|resN| */
236 spi_message_init(&msg);
237 for (i = 0; i < num + 1; i++) {
238 if (i > 0)
239 xfers[i].rx_buf = st->rx + 2*(i - 1);
240 if (i < num)
241 xfers[i].tx_buf = st->tx + 2*i;
242 xfers[i].bits_per_word = 8;
243 xfers[i].len = 2;
244 xfers[i].cs_change = 1;
245 spi_message_add_tail(&xfers[i], &msg);
246 }
247
248 mutex_lock(&st->buf_lock);
249
250 ret = spi_sync(st->us, &msg);
251 if (ret)
252 dev_err(&st->us->dev, "problem when reading sequence");
253
254 mutex_unlock(&st->buf_lock);
255 kfree(xfers);
256
257error_ret:
258 return ret;
259}
260
261static ssize_t adis16400_spi_read_signed(struct device *dev,
262 struct device_attribute *attr,
263 char *buf,
264 unsigned bits)
265{
266 int ret;
267 s16 val = 0;
268 unsigned shift = 16 - bits;
269 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
270
271 ret = adis16400_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
272 if (ret)
273 return ret;
274
275 if (val & ADIS16400_ERROR_ACTIVE)
276 adis16400_check_status(dev);
277 val = ((s16)(val << shift) >> shift);
278 return sprintf(buf, "%d\n", val);
279}
280
281static ssize_t adis16400_read_12bit_unsigned(struct device *dev,
282 struct device_attribute *attr,
283 char *buf)
284{
285 int ret;
286 u16 val = 0;
287 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
288
289 ret = adis16400_spi_read_reg_16(dev, this_attr->address, &val);
290 if (ret)
291 return ret;
292
293 if (val & ADIS16400_ERROR_ACTIVE)
294 adis16400_check_status(dev);
295
296 return sprintf(buf, "%u\n", val & 0x0FFF);
297}
298
299static ssize_t adis16400_read_14bit_signed(struct device *dev,
300 struct device_attribute *attr,
301 char *buf)
302{
303 struct iio_dev *indio_dev = dev_get_drvdata(dev);
304 ssize_t ret;
305
306 /* Take the iio_dev status lock */
307 mutex_lock(&indio_dev->mlock);
308 ret = adis16400_spi_read_signed(dev, attr, buf, 14);
309 mutex_unlock(&indio_dev->mlock);
310
311 return ret;
312}
313
314static ssize_t adis16400_read_12bit_signed(struct device *dev,
315 struct device_attribute *attr,
316 char *buf)
317{
318 struct iio_dev *indio_dev = dev_get_drvdata(dev);
319 ssize_t ret;
320
321 /* Take the iio_dev status lock */
322 mutex_lock(&indio_dev->mlock);
323 ret = adis16400_spi_read_signed(dev, attr, buf, 12);
324 mutex_unlock(&indio_dev->mlock);
325
326 return ret;
327}
328
329
330static ssize_t adis16400_write_16bit(struct device *dev,
331 struct device_attribute *attr,
332 const char *buf,
333 size_t len)
334{
335 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
336 int ret;
337 long val;
338
339 ret = strict_strtol(buf, 10, &val);
340 if (ret)
341 goto error_ret;
342 ret = adis16400_spi_write_reg_16(dev, this_attr->address, val);
343
344error_ret:
345 return ret ? ret : len;
346}
347
348static ssize_t adis16400_read_frequency(struct device *dev,
349 struct device_attribute *attr,
350 char *buf)
351{
352 int ret, len = 0;
353 u16 t;
354 int sps;
355 ret = adis16400_spi_read_reg_16(dev,
356 ADIS16400_SMPL_PRD,
357 &t);
358 if (ret)
359 return ret;
360 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 53 : 1638;
361 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
362 len = sprintf(buf, "%d SPS\n", sps);
363 return len;
364}
365
366static ssize_t adis16400_write_frequency(struct device *dev,
367 struct device_attribute *attr,
368 const char *buf,
369 size_t len)
370{
371 struct iio_dev *indio_dev = dev_get_drvdata(dev);
372 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
373 long val;
374 int ret;
375 u8 t;
376
377 ret = strict_strtol(buf, 10, &val);
378 if (ret)
379 return ret;
380
381 mutex_lock(&indio_dev->mlock);
382
383 t = (1638 / val);
384 if (t > 0)
385 t--;
386 t &= ADIS16400_SMPL_PRD_DIV_MASK;
387 if ((t & ADIS16400_SMPL_PRD_DIV_MASK) >= 0x0A)
388 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
389 else
390 st->us->max_speed_hz = ADIS16400_SPI_FAST;
391
392 ret = adis16400_spi_write_reg_8(dev,
393 ADIS16400_SMPL_PRD,
394 t);
395
396 mutex_unlock(&indio_dev->mlock);
397
398 return ret ? ret : len;
399}
400
401static ssize_t adis16400_write_reset(struct device *dev,
402 struct device_attribute *attr,
403 const char *buf, size_t len)
404{
405 if (len < 1)
406 return -1;
407 switch (buf[0]) {
408 case '1':
409 case 'y':
410 case 'Y':
411 return adis16400_reset(dev);
412 }
413 return -1;
414}
415
416
417
418int adis16400_set_irq(struct device *dev, bool enable)
419{
420 int ret;
421 u16 msc;
422 ret = adis16400_spi_read_reg_16(dev, ADIS16400_MSC_CTRL, &msc);
423 if (ret)
424 goto error_ret;
425
426 msc |= ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH;
427 if (enable)
428 msc |= ADIS16400_MSC_CTRL_DATA_RDY_EN;
429 else
430 msc &= ~ADIS16400_MSC_CTRL_DATA_RDY_EN;
431
432 ret = adis16400_spi_write_reg_16(dev, ADIS16400_MSC_CTRL, msc);
433 if (ret)
434 goto error_ret;
435
436error_ret:
437 return ret;
438}
439
440int adis16400_reset(struct device *dev)
441{
442 int ret;
443 ret = adis16400_spi_write_reg_8(dev,
444 ADIS16400_GLOB_CMD,
445 ADIS16400_GLOB_CMD_SW_RESET);
446 if (ret)
447 dev_err(dev, "problem resetting device");
448
449 return ret;
450}
451
452/* Power down the device */
453int adis16400_stop_device(struct device *dev)
454{
455 int ret;
456 u16 val = ADIS16400_SLP_CNT_POWER_OFF;
457
458 ret = adis16400_spi_write_reg_16(dev, ADIS16400_SLP_CNT, val);
459 if (ret)
460 dev_err(dev, "problem with turning device off: SLP_CNT");
461
462 return ret;
463}
464
465int adis16400_self_test(struct device *dev)
466{
467 int ret;
468 ret = adis16400_spi_write_reg_16(dev,
469 ADIS16400_MSC_CTRL,
470 ADIS16400_MSC_CTRL_MEM_TEST);
471 if (ret) {
472 dev_err(dev, "problem starting self test");
473 goto err_ret;
474 }
475
476 adis16400_check_status(dev);
477
478err_ret:
479 return ret;
480}
481
482int adis16400_check_status(struct device *dev)
483{
484 u16 status;
485 int ret;
486
487 ret = adis16400_spi_read_reg_16(dev, ADIS16400_DIAG_STAT, &status);
488
489 if (ret < 0) {
490 dev_err(dev, "Reading status failed\n");
491 goto error_ret;
492 }
493 ret = status;
494 if (status & ADIS16400_DIAG_STAT_ZACCL_FAIL)
495 dev_err(dev, "Z-axis accelerometer self-test failure\n");
496 if (status & ADIS16400_DIAG_STAT_YACCL_FAIL)
497 dev_err(dev, "Y-axis accelerometer self-test failure\n");
498 if (status & ADIS16400_DIAG_STAT_XACCL_FAIL)
499 dev_err(dev, "X-axis accelerometer self-test failure\n");
500 if (status & ADIS16400_DIAG_STAT_XGYRO_FAIL)
501 dev_err(dev, "X-axis gyroscope self-test failure\n");
502 if (status & ADIS16400_DIAG_STAT_YGYRO_FAIL)
503 dev_err(dev, "Y-axis gyroscope self-test failure\n");
504 if (status & ADIS16400_DIAG_STAT_ZGYRO_FAIL)
505 dev_err(dev, "Z-axis gyroscope self-test failure\n");
506 if (status & ADIS16400_DIAG_STAT_ALARM2)
507 dev_err(dev, "Alarm 2 active\n");
508 if (status & ADIS16400_DIAG_STAT_ALARM1)
509 dev_err(dev, "Alarm 1 active\n");
510 if (status & ADIS16400_DIAG_STAT_FLASH_CHK)
511 dev_err(dev, "Flash checksum error\n");
512 if (status & ADIS16400_DIAG_STAT_SELF_TEST)
513 dev_err(dev, "Self test error\n");
514 if (status & ADIS16400_DIAG_STAT_OVERFLOW)
515 dev_err(dev, "Sensor overrange\n");
516 if (status & ADIS16400_DIAG_STAT_SPI_FAIL)
517 dev_err(dev, "SPI failure\n");
518 if (status & ADIS16400_DIAG_STAT_FLASH_UPT)
519 dev_err(dev, "Flash update failed\n");
520 if (status & ADIS16400_DIAG_STAT_POWER_HIGH)
521 dev_err(dev, "Power supply above 5.25V\n");
522 if (status & ADIS16400_DIAG_STAT_POWER_LOW)
523 dev_err(dev, "Power supply below 4.75V\n");
524
525error_ret:
526 return ret;
527}
528
529static int adis16400_initial_setup(struct adis16400_state *st)
530{
531 int ret;
532 u16 prod_id, smp_prd;
533 struct device *dev = &st->indio_dev->dev;
534
535 /* use low spi speed for init */
536 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
537 st->us->mode = SPI_MODE_3;
538 spi_setup(st->us);
539
540 /* Disable IRQ */
541 ret = adis16400_set_irq(dev, false);
542 if (ret) {
543 dev_err(dev, "disable irq failed");
544 goto err_ret;
545 }
546
547 /* Do self test */
548
549 /* Read status register to check the result */
550 ret = adis16400_check_status(dev);
551 if (ret) {
552 adis16400_reset(dev);
553 dev_err(dev, "device not playing ball -> reset");
554 msleep(ADIS16400_STARTUP_DELAY);
555 ret = adis16400_check_status(dev);
556 if (ret) {
557 dev_err(dev, "giving up");
558 goto err_ret;
559 }
560 }
561
562 ret = adis16400_spi_read_reg_16(dev, ADIS16400_PRODUCT_ID, &prod_id);
563 if (ret)
564 goto err_ret;
565
566 if (prod_id != ADIS16400_PRODUCT_ID_DEFAULT)
567 dev_warn(dev, "unknown product id");
568
569 printk(KERN_INFO DRIVER_NAME ": prod_id 0x%04x at CS%d (irq %d)\n",
570 prod_id, st->us->chip_select, st->us->irq);
571
572 /* use high spi speed if possible */
573 ret = adis16400_spi_read_reg_16(dev, ADIS16400_SMPL_PRD, &smp_prd);
574 if (!ret && (smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
575 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
576 spi_setup(st->us);
577 }
578
579
580err_ret:
581
582 return ret;
583}
584
585static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
586 adis16400_read_12bit_signed,
587 adis16400_write_16bit,
588 ADIS16400_XACCL_OFF);
589
590static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
591 adis16400_read_12bit_signed,
592 adis16400_write_16bit,
593 ADIS16400_YACCL_OFF);
594
595static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO,
596 adis16400_read_12bit_signed,
597 adis16400_write_16bit,
598 ADIS16400_ZACCL_OFF);
599
600static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16400_read_14bit_signed,
601 ADIS16400_SUPPLY_OUT);
602static IIO_CONST_ATTR(in_supply_scale, "0.002418");
603
604static IIO_DEV_ATTR_GYRO_X(adis16400_read_14bit_signed,
605 ADIS16400_XGYRO_OUT);
606static IIO_DEV_ATTR_GYRO_Y(adis16400_read_14bit_signed,
607 ADIS16400_YGYRO_OUT);
608static IIO_DEV_ATTR_GYRO_Z(adis16400_read_14bit_signed,
609 ADIS16400_ZGYRO_OUT);
610static IIO_CONST_ATTR(gyro_scale, "0.05 deg/s");
611
612static IIO_DEV_ATTR_ACCEL_X(adis16400_read_14bit_signed,
613 ADIS16400_XACCL_OUT);
614static IIO_DEV_ATTR_ACCEL_Y(adis16400_read_14bit_signed,
615 ADIS16400_YACCL_OUT);
616static IIO_DEV_ATTR_ACCEL_Z(adis16400_read_14bit_signed,
617 ADIS16400_ZACCL_OUT);
618static IIO_CONST_ATTR(accel_scale, "0.00333 g");
619
620static IIO_DEV_ATTR_MAGN_X(adis16400_read_14bit_signed,
621 ADIS16400_XMAGN_OUT);
622static IIO_DEV_ATTR_MAGN_Y(adis16400_read_14bit_signed,
623 ADIS16400_YMAGN_OUT);
624static IIO_DEV_ATTR_MAGN_Z(adis16400_read_14bit_signed,
625 ADIS16400_ZMAGN_OUT);
626static IIO_CONST_ATTR(magn_scale, "0.0005 Gs");
627
628
629static IIO_DEV_ATTR_TEMP(adis16400_read_12bit_signed);
630static IIO_CONST_ATTR(temp_offset, "198.16 K");
631static IIO_CONST_ATTR(temp_scale, "0.14 K");
632
633static IIO_DEV_ATTR_IN_RAW(0, adis16400_read_12bit_unsigned,
634 ADIS16400_AUX_ADC);
635static IIO_CONST_ATTR(in0_scale, "0.000806");
636
637static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
638 adis16400_read_frequency,
639 adis16400_write_frequency);
640
641static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16400_write_reset, 0);
642
643static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638");
644
645static IIO_CONST_ATTR(name, "adis16400");
646
647static struct attribute *adis16400_event_attributes[] = {
648 NULL
649};
650
651static struct attribute_group adis16400_event_attribute_group = {
652 .attrs = adis16400_event_attributes,
653};
654
655static struct attribute *adis16400_attributes[] = {
656 &iio_dev_attr_accel_x_offset.dev_attr.attr,
657 &iio_dev_attr_accel_y_offset.dev_attr.attr,
658 &iio_dev_attr_accel_z_offset.dev_attr.attr,
659 &iio_dev_attr_in_supply_raw.dev_attr.attr,
660 &iio_const_attr_in_supply_scale.dev_attr.attr,
661 &iio_dev_attr_gyro_x.dev_attr.attr,
662 &iio_dev_attr_gyro_y.dev_attr.attr,
663 &iio_dev_attr_gyro_z.dev_attr.attr,
664 &iio_const_attr_gyro_scale.dev_attr.attr,
665 &iio_dev_attr_accel_x_raw.dev_attr.attr,
666 &iio_dev_attr_accel_y_raw.dev_attr.attr,
667 &iio_dev_attr_accel_z_raw.dev_attr.attr,
668 &iio_const_attr_accel_scale.dev_attr.attr,
669 &iio_dev_attr_magn_x.dev_attr.attr,
670 &iio_dev_attr_magn_y.dev_attr.attr,
671 &iio_dev_attr_magn_z.dev_attr.attr,
672 &iio_const_attr_magn_scale.dev_attr.attr,
673 &iio_dev_attr_temp.dev_attr.attr,
674 &iio_const_attr_temp_offset.dev_attr.attr,
675 &iio_const_attr_temp_scale.dev_attr.attr,
676 &iio_dev_attr_in0_raw.dev_attr.attr,
677 &iio_const_attr_in0_scale.dev_attr.attr,
678 &iio_dev_attr_sampling_frequency.dev_attr.attr,
679 &iio_const_attr_available_sampling_frequency.dev_attr.attr,
680 &iio_dev_attr_reset.dev_attr.attr,
681 &iio_const_attr_name.dev_attr.attr,
682 NULL
683};
684
685static const struct attribute_group adis16400_attribute_group = {
686 .attrs = adis16400_attributes,
687};
688
689static int __devinit adis16400_probe(struct spi_device *spi)
690{
691 int ret, regdone = 0;
692 struct adis16400_state *st = kzalloc(sizeof *st, GFP_KERNEL);
693 if (!st) {
694 ret = -ENOMEM;
695 goto error_ret;
696 }
697 /* this is only used for removal purposes */
698 spi_set_drvdata(spi, st);
699
700 /* Allocate the comms buffers */
701 st->rx = kzalloc(sizeof(*st->rx)*ADIS16400_MAX_RX, GFP_KERNEL);
702 if (st->rx == NULL) {
703 ret = -ENOMEM;
704 goto error_free_st;
705 }
706 st->tx = kzalloc(sizeof(*st->tx)*ADIS16400_MAX_TX, GFP_KERNEL);
707 if (st->tx == NULL) {
708 ret = -ENOMEM;
709 goto error_free_rx;
710 }
711 st->us = spi;
712 mutex_init(&st->buf_lock);
713 /* setup the industrialio driver allocated elements */
714 st->indio_dev = iio_allocate_device();
715 if (st->indio_dev == NULL) {
716 ret = -ENOMEM;
717 goto error_free_tx;
718 }
719
720 st->indio_dev->dev.parent = &spi->dev;
721 st->indio_dev->num_interrupt_lines = 1;
722 st->indio_dev->event_attrs = &adis16400_event_attribute_group;
723 st->indio_dev->attrs = &adis16400_attribute_group;
724 st->indio_dev->dev_data = (void *)(st);
725 st->indio_dev->driver_module = THIS_MODULE;
726 st->indio_dev->modes = INDIO_DIRECT_MODE;
727
728 ret = adis16400_configure_ring(st->indio_dev);
729 if (ret)
730 goto error_free_dev;
731
732 ret = iio_device_register(st->indio_dev);
733 if (ret)
734 goto error_unreg_ring_funcs;
735 regdone = 1;
736
737 ret = adis16400_initialize_ring(st->indio_dev->ring);
738 if (ret) {
739 printk(KERN_ERR "failed to initialize the ring\n");
740 goto error_unreg_ring_funcs;
741 }
742
743 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
744#if 0 /* fixme: here we should support */
745 iio_init_work_cont(&st->work_cont_thresh,
746 NULL,
747 adis16400_thresh_handler_bh_no_check,
748 0,
749 0,
750 st);
751#endif
752 ret = iio_register_interrupt_line(spi->irq,
753 st->indio_dev,
754 0,
755 IRQF_TRIGGER_RISING,
756 "adis16400");
757 if (ret)
758 goto error_uninitialize_ring;
759
760 ret = adis16400_probe_trigger(st->indio_dev);
761 if (ret)
762 goto error_unregister_line;
763 }
764
765 /* Get the device into a sane initial state */
766 ret = adis16400_initial_setup(st);
767 if (ret)
768 goto error_remove_trigger;
769 return 0;
770
771error_remove_trigger:
772 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
773 adis16400_remove_trigger(st->indio_dev);
774error_unregister_line:
775 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
776 iio_unregister_interrupt_line(st->indio_dev, 0);
777error_uninitialize_ring:
778 adis16400_uninitialize_ring(st->indio_dev->ring);
779error_unreg_ring_funcs:
780 adis16400_unconfigure_ring(st->indio_dev);
781error_free_dev:
782 if (regdone)
783 iio_device_unregister(st->indio_dev);
784 else
785 iio_free_device(st->indio_dev);
786error_free_tx:
787 kfree(st->tx);
788error_free_rx:
789 kfree(st->rx);
790error_free_st:
791 kfree(st);
792error_ret:
793 return ret;
794}
795
796/* fixme, confirm ordering in this function */
797static int adis16400_remove(struct spi_device *spi)
798{
799 int ret;
800 struct adis16400_state *st = spi_get_drvdata(spi);
801 struct iio_dev *indio_dev = st->indio_dev;
802
803 ret = adis16400_stop_device(&(indio_dev->dev));
804 if (ret)
805 goto err_ret;
806
807 flush_scheduled_work();
808
809 adis16400_remove_trigger(indio_dev);
810 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
811 iio_unregister_interrupt_line(indio_dev, 0);
812
813 adis16400_uninitialize_ring(indio_dev->ring);
814 adis16400_unconfigure_ring(indio_dev);
815 iio_device_unregister(indio_dev);
816 kfree(st->tx);
817 kfree(st->rx);
818 kfree(st);
819
820 return 0;
821
822err_ret:
823 return ret;
824}
825
826static struct spi_driver adis16400_driver = {
827 .driver = {
828 .name = "adis16400",
829 .owner = THIS_MODULE,
830 },
831 .probe = adis16400_probe,
832 .remove = __devexit_p(adis16400_remove),
833};
834
835static __init int adis16400_init(void)
836{
837 return spi_register_driver(&adis16400_driver);
838}
839module_init(adis16400_init);
840
841static __exit void adis16400_exit(void)
842{
843 spi_unregister_driver(&adis16400_driver);
844}
845module_exit(adis16400_exit);
846
847MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
848MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
849MODULE_LICENSE("GPL v2");
diff --git a/drivers/staging/iio/imu/adis16400_ring.c b/drivers/staging/iio/imu/adis16400_ring.c
new file mode 100644
index 00000000000..5529b32bd2e
--- /dev/null
+++ b/drivers/staging/iio/imu/adis16400_ring.c
@@ -0,0 +1,245 @@
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/sysfs.h>
10#include <linux/list.h>
11
12#include "../iio.h"
13#include "../sysfs.h"
14#include "../ring_sw.h"
15#include "../accel/accel.h"
16#include "../trigger.h"
17#include "adis16400.h"
18
19/**
20 * combine_8_to_16() utility function to munge to u8s into u16
21 **/
22static inline u16 combine_8_to_16(u8 lower, u8 upper)
23{
24 u16 _lower = lower;
25 u16 _upper = upper;
26 return _lower | (_upper << 8);
27}
28
29static IIO_SCAN_EL_C(supply, ADIS16400_SCAN_SUPPLY, IIO_SIGNED(14),
30 ADIS16400_SUPPLY_OUT, NULL);
31
32static IIO_SCAN_EL_C(gyro_x, ADIS16400_SCAN_GYRO_X, IIO_SIGNED(14),
33 ADIS16400_XGYRO_OUT, NULL);
34static IIO_SCAN_EL_C(gyro_y, ADIS16400_SCAN_GYRO_Y, IIO_SIGNED(14),
35 ADIS16400_YGYRO_OUT, NULL);
36static IIO_SCAN_EL_C(gyro_z, ADIS16400_SCAN_GYRO_Z, IIO_SIGNED(14),
37 ADIS16400_ZGYRO_OUT, NULL);
38
39static IIO_SCAN_EL_C(accel_x, ADIS16400_SCAN_ACC_X, IIO_SIGNED(14),
40 ADIS16400_XACCL_OUT, NULL);
41static IIO_SCAN_EL_C(accel_y, ADIS16400_SCAN_ACC_Y, IIO_SIGNED(14),
42 ADIS16400_YACCL_OUT, NULL);
43static IIO_SCAN_EL_C(accel_z, ADIS16400_SCAN_ACC_Z, IIO_SIGNED(14),
44 ADIS16400_ZACCL_OUT, NULL);
45
46static IIO_SCAN_EL_C(magn_x, ADIS16400_SCAN_MAGN_X, IIO_SIGNED(14),
47 ADIS16400_XMAGN_OUT, NULL);
48static IIO_SCAN_EL_C(magn_y, ADIS16400_SCAN_MAGN_Y, IIO_SIGNED(14),
49 ADIS16400_YMAGN_OUT, NULL);
50static IIO_SCAN_EL_C(magn_z, ADIS16400_SCAN_MAGN_Z, IIO_SIGNED(14),
51 ADIS16400_ZMAGN_OUT, NULL);
52
53static IIO_SCAN_EL_C(temp, ADIS16400_SCAN_TEMP, IIO_SIGNED(12),
54 ADIS16400_TEMP_OUT, NULL);
55static IIO_SCAN_EL_C(adc_0, ADIS16400_SCAN_ADC_0, IIO_SIGNED(12),
56 ADIS16400_AUX_ADC, NULL);
57
58static IIO_SCAN_EL_TIMESTAMP(12);
59
60static struct attribute *adis16400_scan_el_attrs[] = {
61 &iio_scan_el_supply.dev_attr.attr,
62 &iio_scan_el_gyro_x.dev_attr.attr,
63 &iio_scan_el_gyro_y.dev_attr.attr,
64 &iio_scan_el_gyro_z.dev_attr.attr,
65 &iio_scan_el_accel_x.dev_attr.attr,
66 &iio_scan_el_accel_y.dev_attr.attr,
67 &iio_scan_el_accel_z.dev_attr.attr,
68 &iio_scan_el_magn_x.dev_attr.attr,
69 &iio_scan_el_magn_y.dev_attr.attr,
70 &iio_scan_el_magn_z.dev_attr.attr,
71 &iio_scan_el_temp.dev_attr.attr,
72 &iio_scan_el_adc_0.dev_attr.attr,
73 &iio_scan_el_timestamp.dev_attr.attr,
74 NULL,
75};
76
77static struct attribute_group adis16400_scan_el_group = {
78 .attrs = adis16400_scan_el_attrs,
79 .name = "scan_elements",
80};
81
82/**
83 * adis16400_poll_func_th() top half interrupt handler called by trigger
84 * @private_data: iio_dev
85 **/
86static void adis16400_poll_func_th(struct iio_dev *indio_dev)
87{
88 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
89 st->last_timestamp = indio_dev->trig->timestamp;
90 schedule_work(&st->work_trigger_to_ring);
91 /* Indicate that this interrupt is being handled */
92
93 /* Technically this is trigger related, but without this
94 * handler running there is currently no way for the interrupt
95 * to clear.
96 */
97}
98
99/* Whilst this makes a lot of calls to iio_sw_ring functions - it is to device
100 * specific to be rolled into the core.
101 */
102static void adis16400_trigger_bh_to_ring(struct work_struct *work_s)
103{
104 struct adis16400_state *st
105 = container_of(work_s, struct adis16400_state,
106 work_trigger_to_ring);
107
108 int i = 0;
109 s16 *data;
110 size_t datasize = st->indio_dev
111 ->ring->access.get_bpd(st->indio_dev->ring);
112
113 data = kmalloc(datasize , GFP_KERNEL);
114 if (data == NULL) {
115 dev_err(&st->us->dev, "memory alloc failed in ring bh");
116 return;
117 }
118
119 if (st->indio_dev->scan_count)
120 if (adis16400_spi_read_burst(&st->indio_dev->dev, st->rx) >= 0)
121 for (; i < st->indio_dev->scan_count; i++) {
122 data[i] = combine_8_to_16(st->rx[i*2+1],
123 st->rx[i*2]);
124 }
125
126 /* Guaranteed to be aligned with 8 byte boundary */
127 if (st->indio_dev->scan_timestamp)
128 *((s64 *)(data + ((i + 3)/4)*4)) = st->last_timestamp;
129
130 st->indio_dev->ring->access.store_to(st->indio_dev->ring,
131 (u8 *)data,
132 st->last_timestamp);
133
134 iio_trigger_notify_done(st->indio_dev->trig);
135 kfree(data);
136
137 return;
138}
139/* in these circumstances is it better to go with unaligned packing and
140 * deal with the cost?*/
141static int adis16400_data_rdy_ring_preenable(struct iio_dev *indio_dev)
142{
143 size_t size;
144 dev_dbg(&indio_dev->dev, "%s\n", __func__);
145 /* Check if there are any scan elements enabled, if not fail*/
146 if (!(indio_dev->scan_count || indio_dev->scan_timestamp))
147 return -EINVAL;
148
149 if (indio_dev->ring->access.set_bpd) {
150 if (indio_dev->scan_timestamp)
151 if (indio_dev->scan_count) /* Timestamp and data */
152 size = 6*sizeof(s64);
153 else /* Timestamp only */
154 size = sizeof(s64);
155 else /* Data only */
156 size = indio_dev->scan_count*sizeof(s16);
157 indio_dev->ring->access.set_bpd(indio_dev->ring, size);
158 }
159
160 return 0;
161}
162
163static int adis16400_data_rdy_ring_postenable(struct iio_dev *indio_dev)
164{
165 return indio_dev->trig
166 ? iio_trigger_attach_poll_func(indio_dev->trig,
167 indio_dev->pollfunc)
168 : 0;
169}
170
171static int adis16400_data_rdy_ring_predisable(struct iio_dev *indio_dev)
172{
173 return indio_dev->trig
174 ? iio_trigger_dettach_poll_func(indio_dev->trig,
175 indio_dev->pollfunc)
176 : 0;
177}
178
179void adis16400_unconfigure_ring(struct iio_dev *indio_dev)
180{
181 kfree(indio_dev->pollfunc);
182 iio_sw_rb_free(indio_dev->ring);
183}
184
185int adis16400_configure_ring(struct iio_dev *indio_dev)
186{
187 int ret = 0;
188 struct adis16400_state *st = indio_dev->dev_data;
189 struct iio_ring_buffer *ring;
190 INIT_WORK(&st->work_trigger_to_ring, adis16400_trigger_bh_to_ring);
191 /* Set default scan mode */
192
193 iio_scan_mask_set(indio_dev, iio_scan_el_supply.number);
194 iio_scan_mask_set(indio_dev, iio_scan_el_gyro_x.number);
195 iio_scan_mask_set(indio_dev, iio_scan_el_gyro_y.number);
196 iio_scan_mask_set(indio_dev, iio_scan_el_gyro_z.number);
197 iio_scan_mask_set(indio_dev, iio_scan_el_accel_x.number);
198 iio_scan_mask_set(indio_dev, iio_scan_el_accel_y.number);
199 iio_scan_mask_set(indio_dev, iio_scan_el_accel_z.number);
200 iio_scan_mask_set(indio_dev, iio_scan_el_magn_x.number);
201 iio_scan_mask_set(indio_dev, iio_scan_el_magn_y.number);
202 iio_scan_mask_set(indio_dev, iio_scan_el_magn_z.number);
203 iio_scan_mask_set(indio_dev, iio_scan_el_temp.number);
204 iio_scan_mask_set(indio_dev, iio_scan_el_adc_0.number);
205 indio_dev->scan_timestamp = true;
206
207 indio_dev->scan_el_attrs = &adis16400_scan_el_group;
208
209 ring = iio_sw_rb_allocate(indio_dev);
210 if (!ring) {
211 ret = -ENOMEM;
212 return ret;
213 }
214 indio_dev->ring = ring;
215 /* Effectively select the ring buffer implementation */
216 iio_ring_sw_register_funcs(&ring->access);
217 ring->preenable = &adis16400_data_rdy_ring_preenable;
218 ring->postenable = &adis16400_data_rdy_ring_postenable;
219 ring->predisable = &adis16400_data_rdy_ring_predisable;
220 ring->owner = THIS_MODULE;
221
222 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
223 if (indio_dev->pollfunc == NULL) {
224 ret = -ENOMEM;
225 goto error_iio_sw_rb_free;;
226 }
227 indio_dev->pollfunc->poll_func_main = &adis16400_poll_func_th;
228 indio_dev->pollfunc->private_data = indio_dev;
229 indio_dev->modes |= INDIO_RING_TRIGGERED;
230 return 0;
231
232error_iio_sw_rb_free:
233 iio_sw_rb_free(indio_dev->ring);
234 return ret;
235}
236
237int adis16400_initialize_ring(struct iio_ring_buffer *ring)
238{
239 return iio_ring_buffer_register(ring, 0);
240}
241
242void adis16400_uninitialize_ring(struct iio_ring_buffer *ring)
243{
244 iio_ring_buffer_unregister(ring);
245}
diff --git a/drivers/staging/iio/imu/adis16400_trigger.c b/drivers/staging/iio/imu/adis16400_trigger.c
new file mode 100644
index 00000000000..3b3250ac768
--- /dev/null
+++ b/drivers/staging/iio/imu/adis16400_trigger.c
@@ -0,0 +1,127 @@
1#include <linux/interrupt.h>
2#include <linux/irq.h>
3#include <linux/mutex.h>
4#include <linux/device.h>
5#include <linux/kernel.h>
6#include <linux/sysfs.h>
7#include <linux/list.h>
8#include <linux/spi/spi.h>
9
10#include "../iio.h"
11#include "../sysfs.h"
12#include "../trigger.h"
13#include "adis16400.h"
14
15/**
16 * adis16400_data_rdy_trig_poll() the event handler for the data rdy trig
17 **/
18static int adis16400_data_rdy_trig_poll(struct iio_dev *dev_info,
19 int index,
20 s64 timestamp,
21 int no_test)
22{
23 struct adis16400_state *st = iio_dev_get_devdata(dev_info);
24 struct iio_trigger *trig = st->trig;
25
26 trig->timestamp = timestamp;
27 iio_trigger_poll(trig);
28
29 return IRQ_HANDLED;
30}
31
32IIO_EVENT_SH(data_rdy_trig, &adis16400_data_rdy_trig_poll);
33
34static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
35
36static struct attribute *adis16400_trigger_attrs[] = {
37 &dev_attr_name.attr,
38 NULL,
39};
40
41static const struct attribute_group adis16400_trigger_attr_group = {
42 .attrs = adis16400_trigger_attrs,
43};
44
45/**
46 * adis16400_data_rdy_trigger_set_state() set datardy interrupt state
47 **/
48static int adis16400_data_rdy_trigger_set_state(struct iio_trigger *trig,
49 bool state)
50{
51 struct adis16400_state *st = trig->private_data;
52 struct iio_dev *indio_dev = st->indio_dev;
53 int ret = 0;
54
55 dev_dbg(&indio_dev->dev, "%s (%d)\n", __func__, state);
56 ret = adis16400_set_irq(&st->indio_dev->dev, state);
57 if (state == false) {
58 iio_remove_event_from_list(&iio_event_data_rdy_trig,
59 &indio_dev->interrupts[0]
60 ->ev_list);
61 /* possible quirk with handler currently worked around
62 by ensuring the work queue is empty */
63 flush_scheduled_work();
64 } else {
65 iio_add_event_to_list(&iio_event_data_rdy_trig,
66 &indio_dev->interrupts[0]->ev_list);
67 }
68 return ret;
69}
70
71/**
72 * adis16400_trig_try_reen() try renabling irq for data rdy trigger
73 * @trig: the datardy trigger
74 **/
75static int adis16400_trig_try_reen(struct iio_trigger *trig)
76{
77 struct adis16400_state *st = trig->private_data;
78 enable_irq(st->us->irq);
79 /* irq reenabled so success! */
80 return 0;
81}
82
83int adis16400_probe_trigger(struct iio_dev *indio_dev)
84{
85 int ret;
86 struct adis16400_state *st = indio_dev->dev_data;
87
88 st->trig = iio_allocate_trigger();
89 st->trig->name = kmalloc(IIO_TRIGGER_NAME_LENGTH, GFP_KERNEL);
90 if (!st->trig->name) {
91 ret = -ENOMEM;
92 goto error_free_trig;
93 }
94 snprintf((char *)st->trig->name,
95 IIO_TRIGGER_NAME_LENGTH,
96 "adis16400-dev%d", indio_dev->id);
97 st->trig->dev.parent = &st->us->dev;
98 st->trig->owner = THIS_MODULE;
99 st->trig->private_data = st;
100 st->trig->set_trigger_state = &adis16400_data_rdy_trigger_set_state;
101 st->trig->try_reenable = &adis16400_trig_try_reen;
102 st->trig->control_attrs = &adis16400_trigger_attr_group;
103 ret = iio_trigger_register(st->trig);
104
105 /* select default trigger */
106 indio_dev->trig = st->trig;
107 if (ret)
108 goto error_free_trig_name;
109
110 return 0;
111
112error_free_trig_name:
113 kfree(st->trig->name);
114error_free_trig:
115 iio_free_trigger(st->trig);
116
117 return ret;
118}
119
120void adis16400_remove_trigger(struct iio_dev *indio_dev)
121{
122 struct adis16400_state *state = indio_dev->dev_data;
123
124 iio_trigger_unregister(state->trig);
125 kfree(state->trig->name);
126 iio_free_trigger(state->trig);
127}
diff --git a/drivers/staging/iio/magnetometer/magnet.h b/drivers/staging/iio/magnetometer/magnet.h
new file mode 100644
index 00000000000..6f6c6ed91fa
--- /dev/null
+++ b/drivers/staging/iio/magnetometer/magnet.h
@@ -0,0 +1,31 @@
1
2#include "../sysfs.h"
3
4/* Magnetometer types of attribute */
5
6#define IIO_DEV_ATTR_MAGN_X_OFFSET(_mode, _show, _store, _addr) \
7 IIO_DEVICE_ATTR(magn_x_offset, _mode, _show, _store, _addr)
8
9#define IIO_DEV_ATTR_MAGN_Y_OFFSET(_mode, _show, _store, _addr) \
10 IIO_DEVICE_ATTR(magn_y_offset, _mode, _show, _store, _addr)
11
12#define IIO_DEV_ATTR_MAGN_Z_OFFSET(_mode, _show, _store, _addr) \
13 IIO_DEVICE_ATTR(magn_z_offset, _mode, _show, _store, _addr)
14
15#define IIO_DEV_ATTR_MAGN_X_GAIN(_mode, _show, _store, _addr) \
16 IIO_DEVICE_ATTR(magn_x_gain, _mode, _show, _store, _addr)
17
18#define IIO_DEV_ATTR_MAGN_Y_GAIN(_mode, _show, _store, _addr) \
19 IIO_DEVICE_ATTR(magn_y_gain, _mode, _show, _store, _addr)
20
21#define IIO_DEV_ATTR_MAGN_Z_GAIN(_mode, _show, _store, _addr) \
22 IIO_DEVICE_ATTR(magn_z_gain, _mode, _show, _store, _addr)
23
24#define IIO_DEV_ATTR_MAGN_X(_show, _addr) \
25 IIO_DEVICE_ATTR(magn_x, S_IRUGO, _show, NULL, _addr)
26
27#define IIO_DEV_ATTR_MAGN_Y(_show, _addr) \
28 IIO_DEVICE_ATTR(magn_y, S_IRUGO, _show, NULL, _addr)
29
30#define IIO_DEV_ATTR_MAGN_Z(_show, _addr) \
31 IIO_DEVICE_ATTR(magn_z, S_IRUGO, _show, NULL, _addr)