1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Common library for ADIS16XXX devices
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
10 #define __IIO_ADIS_H__
12 #include <linux/spi/spi.h>
13 #include <linux/interrupt.h>
14 #include <linux/iio/types.h>
16 #define ADIS_WRITE_REG(reg) ((0x80 | (reg)))
17 #define ADIS_READ_REG(reg) ((reg) & 0x7f)
19 #define ADIS_PAGE_SIZE 0x80
20 #define ADIS_REG_PAGE_ID 0x00
26 * struct adis_data - ADIS chip variant specific data
27 * @read_delay: SPI delay for read operations in us
28 * @write_delay: SPI delay for write operations in us
29 * @cs_change_delay: SPI delay between CS changes in us
30 * @glob_cmd_reg: Register address of the GLOB_CMD register
31 * @msc_ctrl_reg: Register address of the MSC_CTRL register
32 * @diag_stat_reg: Register address of the DIAG_STAT register
33 * @status_error_msgs: Array of error messgaes
37 unsigned int read_delay;
38 unsigned int write_delay;
39 unsigned int cs_change_delay;
41 unsigned int glob_cmd_reg;
42 unsigned int msc_ctrl_reg;
43 unsigned int diag_stat_reg;
45 unsigned int self_test_mask;
46 bool self_test_no_autoclear;
47 unsigned int startup_delay;
49 const char * const *status_error_msgs;
50 unsigned int status_error_mask;
52 int (*enable_irq)(struct adis *adis, bool enable);
58 struct spi_device *spi;
59 struct iio_trigger *trig;
61 const struct adis_data *data;
62 struct adis_burst *burst;
64 struct mutex txrx_lock;
65 struct spi_message msg;
66 struct spi_transfer *xfer;
67 unsigned int current_page;
70 uint8_t tx[10] ____cacheline_aligned;
74 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
75 struct spi_device *spi, const struct adis_data *data);
76 int adis_reset(struct adis *adis);
78 int adis_write_reg(struct adis *adis, unsigned int reg,
79 unsigned int val, unsigned int size);
80 int adis_read_reg(struct adis *adis, unsigned int reg,
81 unsigned int *val, unsigned int size);
84 * adis_write_reg_8() - Write single byte to a register
85 * @adis: The adis device
86 * @reg: The address of the register to be written
87 * @value: The value to write
89 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
92 return adis_write_reg(adis, reg, val, 1);
96 * adis_write_reg_16() - Write 2 bytes to a pair of registers
97 * @adis: The adis device
98 * @reg: The address of the lower of the two registers
99 * @value: Value to be written
101 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
104 return adis_write_reg(adis, reg, val, 2);
108 * adis_write_reg_32() - write 4 bytes to four registers
109 * @adis: The adis device
110 * @reg: The address of the lower of the four register
111 * @value: Value to be written
113 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
116 return adis_write_reg(adis, reg, val, 4);
120 * adis_read_reg_16() - read 2 bytes from a 16-bit register
121 * @adis: The adis device
122 * @reg: The address of the lower of the two registers
123 * @val: The value read back from the device
125 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
131 ret = adis_read_reg(adis, reg, &tmp, 2);
139 * adis_read_reg_32() - read 4 bytes from a 32-bit register
140 * @adis: The adis device
141 * @reg: The address of the lower of the two registers
142 * @val: The value read back from the device
144 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
150 ret = adis_read_reg(adis, reg, &tmp, 4);
157 int adis_enable_irq(struct adis *adis, bool enable);
158 int adis_check_status(struct adis *adis);
160 int adis_initial_startup(struct adis *adis);
162 int adis_single_conversion(struct iio_dev *indio_dev,
163 const struct iio_chan_spec *chan, unsigned int error_mask,
166 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
167 .type = IIO_VOLTAGE, \
170 .extend_name = name, \
171 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
172 BIT(IIO_CHAN_INFO_SCALE), \
173 .info_mask_shared_by_all = info_all, \
175 .scan_index = (si), \
178 .realbits = (bits), \
180 .endianness = IIO_BE, \
184 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
185 ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
187 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
188 ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
190 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
194 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
195 BIT(IIO_CHAN_INFO_SCALE) | \
196 BIT(IIO_CHAN_INFO_OFFSET), \
197 .info_mask_shared_by_all = info_all, \
199 .scan_index = (si), \
202 .realbits = (bits), \
204 .endianness = IIO_BE, \
208 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
211 .channel2 = IIO_MOD_ ## mod, \
212 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
214 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
215 .info_mask_shared_by_all = info_all, \
217 .scan_index = (si), \
220 .realbits = (bits), \
222 .endianness = IIO_BE, \
226 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
227 ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
229 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \
230 ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
232 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
233 ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
235 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
236 ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
238 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
241 * struct adis_burst - ADIS data for burst transfers
242 * @en burst mode enabled
243 * @reg_cmd register command that triggers burst
244 * @extra_len extra length to account in the SPI RX buffer
248 unsigned int reg_cmd;
249 unsigned int extra_len;
252 int adis_setup_buffer_and_trigger(struct adis *adis,
253 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
254 void adis_cleanup_buffer_and_trigger(struct adis *adis,
255 struct iio_dev *indio_dev);
257 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
258 void adis_remove_trigger(struct adis *adis);
260 int adis_update_scan_mode(struct iio_dev *indio_dev,
261 const unsigned long *scan_mask);
263 #else /* CONFIG_IIO_BUFFER */
265 static inline int adis_setup_buffer_and_trigger(struct adis *adis,
266 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
271 static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
272 struct iio_dev *indio_dev)
276 static inline int adis_probe_trigger(struct adis *adis,
277 struct iio_dev *indio_dev)
282 static inline void adis_remove_trigger(struct adis *adis)
286 #define adis_update_scan_mode NULL
288 #endif /* CONFIG_IIO_BUFFER */
290 #ifdef CONFIG_DEBUG_FS
292 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
293 unsigned int reg, unsigned int writeval, unsigned int *readval);
297 #define adis_debugfs_reg_access NULL