2 * Common library for ADIS16XXX devices
4 * Copyright 2012 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 * Licensed under the GPL-2 or later.
10 #ifndef __IIO_ADIS_H__
11 #define __IIO_ADIS_H__
13 #include <linux/spi/spi.h>
14 #include <linux/interrupt.h>
15 #include <linux/iio/types.h>
17 #define ADIS_WRITE_REG(reg) ((0x80 | (reg)))
18 #define ADIS_READ_REG(reg) ((reg) & 0x7f)
20 #define ADIS_PAGE_SIZE 0x80
21 #define ADIS_REG_PAGE_ID 0x00
24 * struct adis_data - ADIS chip variant specific data
25 * @read_delay: SPI delay for read operations in us
26 * @write_delay: SPI delay for write operations in us
27 * @glob_cmd_reg: Register address of the GLOB_CMD register
28 * @msc_ctrl_reg: Register address of the MSC_CTRL register
29 * @diag_stat_reg: Register address of the DIAG_STAT register
30 * @status_error_msgs: Array of error messgaes
34 unsigned int read_delay
;
35 unsigned int write_delay
;
37 unsigned int glob_cmd_reg
;
38 unsigned int msc_ctrl_reg
;
39 unsigned int diag_stat_reg
;
41 unsigned int self_test_mask
;
42 unsigned int startup_delay
;
44 const char * const *status_error_msgs
;
45 unsigned int status_error_mask
;
51 struct spi_device
*spi
;
52 struct iio_trigger
*trig
;
54 const struct adis_data
*data
;
56 struct mutex txrx_lock
;
57 struct spi_message msg
;
58 struct spi_transfer
*xfer
;
59 unsigned int current_page
;
62 uint8_t tx
[10] ____cacheline_aligned
;
66 int adis_init(struct adis
*adis
, struct iio_dev
*indio_dev
,
67 struct spi_device
*spi
, const struct adis_data
*data
);
68 int adis_reset(struct adis
*adis
);
70 int adis_write_reg(struct adis
*adis
, unsigned int reg
,
71 unsigned int val
, unsigned int size
);
72 int adis_read_reg(struct adis
*adis
, unsigned int reg
,
73 unsigned int *val
, unsigned int size
);
76 * adis_write_reg_8() - Write single byte to a register
77 * @adis: The adis device
78 * @reg: The address of the register to be written
79 * @value: The value to write
81 static inline int adis_write_reg_8(struct adis
*adis
, unsigned int reg
,
84 return adis_write_reg(adis
, reg
, val
, 1);
88 * adis_write_reg_16() - Write 2 bytes to a pair of registers
89 * @adis: The adis device
90 * @reg: The address of the lower of the two registers
91 * @value: Value to be written
93 static inline int adis_write_reg_16(struct adis
*adis
, unsigned int reg
,
96 return adis_write_reg(adis
, reg
, val
, 2);
100 * adis_write_reg_32() - write 4 bytes to four registers
101 * @adis: The adis device
102 * @reg: The address of the lower of the four register
103 * @value: Value to be written
105 static inline int adis_write_reg_32(struct adis
*adis
, unsigned int reg
,
108 return adis_write_reg(adis
, reg
, val
, 4);
112 * adis_read_reg_16() - read 2 bytes from a 16-bit register
113 * @adis: The adis device
114 * @reg: The address of the lower of the two registers
115 * @val: The value read back from the device
117 static inline int adis_read_reg_16(struct adis
*adis
, unsigned int reg
,
123 ret
= adis_read_reg(adis
, reg
, &tmp
, 2);
130 * adis_read_reg_32() - read 4 bytes from a 32-bit register
131 * @adis: The adis device
132 * @reg: The address of the lower of the two registers
133 * @val: The value read back from the device
135 static inline int adis_read_reg_32(struct adis
*adis
, unsigned int reg
,
141 ret
= adis_read_reg(adis
, reg
, &tmp
, 4);
147 int adis_enable_irq(struct adis
*adis
, bool enable
);
148 int adis_check_status(struct adis
*adis
);
150 int adis_initial_startup(struct adis
*adis
);
152 int adis_single_conversion(struct iio_dev
*indio_dev
,
153 const struct iio_chan_spec
*chan
, unsigned int error_mask
,
156 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, bits) { \
157 .type = IIO_VOLTAGE, \
160 .extend_name = name, \
161 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
162 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
164 .scan_index = (si), \
167 .realbits = (bits), \
169 .endianness = IIO_BE, \
173 #define ADIS_SUPPLY_CHAN(addr, si, bits) \
174 ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", bits)
176 #define ADIS_AUX_ADC_CHAN(addr, si, bits) \
177 ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, bits)
179 #define ADIS_TEMP_CHAN(addr, si, bits) { \
183 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
184 IIO_CHAN_INFO_SCALE_SEPARATE_BIT | \
185 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT, \
187 .scan_index = (si), \
190 .realbits = (bits), \
192 .endianness = IIO_BE, \
196 #define ADIS_MOD_CHAN(_type, mod, addr, si, info, bits) { \
199 .channel2 = IIO_MOD_ ## mod, \
200 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
201 IIO_CHAN_INFO_SCALE_SHARED_BIT | \
204 .scan_index = (si), \
207 .realbits = (bits), \
209 .endianness = IIO_BE, \
213 #define ADIS_ACCEL_CHAN(mod, addr, si, info, bits) \
214 ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info, bits)
216 #define ADIS_GYRO_CHAN(mod, addr, si, info, bits) \
217 ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info, bits)
219 #define ADIS_INCLI_CHAN(mod, addr, si, info, bits) \
220 ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info, bits)
222 #define ADIS_ROT_CHAN(mod, addr, si, info, bits) \
223 ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info, bits)
225 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
227 int adis_setup_buffer_and_trigger(struct adis
*adis
,
228 struct iio_dev
*indio_dev
, irqreturn_t (*trigger_handler
)(int, void *));
229 void adis_cleanup_buffer_and_trigger(struct adis
*adis
,
230 struct iio_dev
*indio_dev
);
232 int adis_probe_trigger(struct adis
*adis
, struct iio_dev
*indio_dev
);
233 void adis_remove_trigger(struct adis
*adis
);
235 int adis_update_scan_mode(struct iio_dev
*indio_dev
,
236 const unsigned long *scan_mask
);
238 #else /* CONFIG_IIO_BUFFER */
240 static inline int adis_setup_buffer_and_trigger(struct adis
*adis
,
241 struct iio_dev
*indio_dev
, irqreturn_t (*trigger_handler
)(int, void *))
246 static inline void adis_cleanup_buffer_and_trigger(struct adis
*adis
,
247 struct iio_dev
*indio_dev
)
251 static inline int adis_probe_trigger(struct adis
*adis
,
252 struct iio_dev
*indio_dev
)
257 static inline void adis_remove_trigger(struct adis
*adis
)
261 #define adis_update_scan_mode NULL
263 #endif /* CONFIG_IIO_BUFFER */
265 #ifdef CONFIG_DEBUG_FS
267 int adis_debugfs_reg_access(struct iio_dev
*indio_dev
,
268 unsigned int reg
, unsigned int writeval
, unsigned int *readval
);
272 #define adis_debugfs_reg_access NULL