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_timeouts - ADIS chip variant timeouts
27 * @reset_ms - Wait time after rst pin goes inactive
28 * @sw_reset_ms - Wait time after sw reset command
29 * @self_test_ms - Wait time after self test command
37 * struct adis_data - ADIS chip variant specific data
38 * @read_delay: SPI delay for read operations in us
39 * @write_delay: SPI delay for write operations in us
40 * @cs_change_delay: SPI delay between CS changes in us
41 * @glob_cmd_reg: Register address of the GLOB_CMD register
42 * @msc_ctrl_reg: Register address of the MSC_CTRL register
43 * @diag_stat_reg: Register address of the DIAG_STAT register
44 * @status_error_msgs: Array of error messgaes
46 * @timeouts: Chip specific delays
49 unsigned int read_delay;
50 unsigned int write_delay;
51 unsigned int cs_change_delay;
53 unsigned int glob_cmd_reg;
54 unsigned int msc_ctrl_reg;
55 unsigned int diag_stat_reg;
57 unsigned int self_test_mask;
58 bool self_test_no_autoclear;
59 const struct adis_timeout *timeouts;
61 const char * const *status_error_msgs;
62 unsigned int status_error_mask;
64 int (*enable_irq)(struct adis *adis, bool enable);
70 struct spi_device *spi;
71 struct iio_trigger *trig;
73 const struct adis_data *data;
74 struct adis_burst *burst;
76 struct mutex state_lock;
77 struct spi_message msg;
78 struct spi_transfer *xfer;
79 unsigned int current_page;
82 uint8_t tx[10] ____cacheline_aligned;
86 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
87 struct spi_device *spi, const struct adis_data *data);
88 int __adis_reset(struct adis *adis);
91 * adis_reset() - Reset the device
92 * @adis: The adis device
94 * Returns 0 on success, a negative error code otherwise
96 static inline int adis_reset(struct adis *adis)
100 mutex_lock(&adis->state_lock);
101 ret = __adis_reset(adis);
102 mutex_unlock(&adis->state_lock);
107 int __adis_write_reg(struct adis *adis, unsigned int reg,
108 unsigned int val, unsigned int size);
109 int __adis_read_reg(struct adis *adis, unsigned int reg,
110 unsigned int *val, unsigned int size);
113 * __adis_write_reg_8() - Write single byte to a register (unlocked)
114 * @adis: The adis device
115 * @reg: The address of the register to be written
116 * @value: The value to write
118 static inline int __adis_write_reg_8(struct adis *adis, unsigned int reg,
121 return __adis_write_reg(adis, reg, val, 1);
125 * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
126 * @adis: The adis device
127 * @reg: The address of the lower of the two registers
128 * @value: Value to be written
130 static inline int __adis_write_reg_16(struct adis *adis, unsigned int reg,
133 return __adis_write_reg(adis, reg, val, 2);
137 * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
138 * @adis: The adis device
139 * @reg: The address of the lower of the four register
140 * @value: Value to be written
142 static inline int __adis_write_reg_32(struct adis *adis, unsigned int reg,
145 return __adis_write_reg(adis, reg, val, 4);
149 * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
150 * @adis: The adis device
151 * @reg: The address of the lower of the two registers
152 * @val: The value read back from the device
154 static inline int __adis_read_reg_16(struct adis *adis, unsigned int reg,
160 ret = __adis_read_reg(adis, reg, &tmp, 2);
168 * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
169 * @adis: The adis device
170 * @reg: The address of the lower of the two registers
171 * @val: The value read back from the device
173 static inline int __adis_read_reg_32(struct adis *adis, unsigned int reg,
179 ret = __adis_read_reg(adis, reg, &tmp, 4);
187 * adis_write_reg() - write N bytes to register
188 * @adis: The adis device
189 * @reg: The address of the lower of the two registers
190 * @value: The value to write to device (up to 4 bytes)
191 * @size: The size of the @value (in bytes)
193 static inline int adis_write_reg(struct adis *adis, unsigned int reg,
194 unsigned int val, unsigned int size)
198 mutex_lock(&adis->state_lock);
199 ret = __adis_write_reg(adis, reg, val, size);
200 mutex_unlock(&adis->state_lock);
206 * adis_read_reg() - read N bytes from register
207 * @adis: The adis device
208 * @reg: The address of the lower of the two registers
209 * @val: The value read back from the device
210 * @size: The size of the @val buffer
212 static int adis_read_reg(struct adis *adis, unsigned int reg,
213 unsigned int *val, unsigned int size)
217 mutex_lock(&adis->state_lock);
218 ret = __adis_read_reg(adis, reg, val, size);
219 mutex_unlock(&adis->state_lock);
225 * adis_write_reg_8() - Write single byte to a register
226 * @adis: The adis device
227 * @reg: The address of the register to be written
228 * @value: The value to write
230 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
233 return adis_write_reg(adis, reg, val, 1);
237 * adis_write_reg_16() - Write 2 bytes to a pair of registers
238 * @adis: The adis device
239 * @reg: The address of the lower of the two registers
240 * @value: Value to be written
242 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
245 return adis_write_reg(adis, reg, val, 2);
249 * adis_write_reg_32() - write 4 bytes to four registers
250 * @adis: The adis device
251 * @reg: The address of the lower of the four register
252 * @value: Value to be written
254 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
257 return adis_write_reg(adis, reg, val, 4);
261 * adis_read_reg_16() - read 2 bytes from a 16-bit register
262 * @adis: The adis device
263 * @reg: The address of the lower of the two registers
264 * @val: The value read back from the device
266 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
272 ret = adis_read_reg(adis, reg, &tmp, 2);
280 * adis_read_reg_32() - read 4 bytes from a 32-bit register
281 * @adis: The adis device
282 * @reg: The address of the lower of the two registers
283 * @val: The value read back from the device
285 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
291 ret = adis_read_reg(adis, reg, &tmp, 4);
298 int adis_enable_irq(struct adis *adis, bool enable);
299 int __adis_check_status(struct adis *adis);
301 static inline int adis_check_status(struct adis *adis)
305 mutex_lock(&adis->state_lock);
306 ret = __adis_check_status(adis);
307 mutex_unlock(&adis->state_lock);
312 int adis_initial_startup(struct adis *adis);
314 int adis_single_conversion(struct iio_dev *indio_dev,
315 const struct iio_chan_spec *chan, unsigned int error_mask,
318 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
319 .type = IIO_VOLTAGE, \
322 .extend_name = name, \
323 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
324 BIT(IIO_CHAN_INFO_SCALE), \
325 .info_mask_shared_by_all = info_all, \
327 .scan_index = (si), \
330 .realbits = (bits), \
332 .endianness = IIO_BE, \
336 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
337 ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
339 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
340 ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
342 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
346 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
347 BIT(IIO_CHAN_INFO_SCALE) | \
348 BIT(IIO_CHAN_INFO_OFFSET), \
349 .info_mask_shared_by_all = info_all, \
351 .scan_index = (si), \
354 .realbits = (bits), \
356 .endianness = IIO_BE, \
360 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
363 .channel2 = IIO_MOD_ ## mod, \
364 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
366 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
367 .info_mask_shared_by_all = info_all, \
369 .scan_index = (si), \
372 .realbits = (bits), \
374 .endianness = IIO_BE, \
378 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
379 ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
381 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \
382 ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
384 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
385 ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
387 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
388 ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
390 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
393 * struct adis_burst - ADIS data for burst transfers
394 * @en burst mode enabled
395 * @reg_cmd register command that triggers burst
396 * @extra_len extra length to account in the SPI RX buffer
400 unsigned int reg_cmd;
401 unsigned int extra_len;
404 int adis_setup_buffer_and_trigger(struct adis *adis,
405 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
406 void adis_cleanup_buffer_and_trigger(struct adis *adis,
407 struct iio_dev *indio_dev);
409 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
410 void adis_remove_trigger(struct adis *adis);
412 int adis_update_scan_mode(struct iio_dev *indio_dev,
413 const unsigned long *scan_mask);
415 #else /* CONFIG_IIO_BUFFER */
417 static inline int adis_setup_buffer_and_trigger(struct adis *adis,
418 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
423 static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
424 struct iio_dev *indio_dev)
428 static inline int adis_probe_trigger(struct adis *adis,
429 struct iio_dev *indio_dev)
434 static inline void adis_remove_trigger(struct adis *adis)
438 #define adis_update_scan_mode NULL
440 #endif /* CONFIG_IIO_BUFFER */
442 #ifdef CONFIG_DEBUG_FS
444 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
445 unsigned int reg, unsigned int writeval, unsigned int *readval);
449 #define adis_debugfs_reg_access NULL