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 state_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);
79 * adis_reset() - Reset the device
80 * @adis: The adis device
82 * Returns 0 on success, a negative error code otherwise
84 static inline int adis_reset(struct adis *adis)
88 mutex_lock(&adis->state_lock);
89 ret = __adis_reset(adis);
90 mutex_unlock(&adis->state_lock);
95 int __adis_write_reg(struct adis *adis, unsigned int reg,
96 unsigned int val, unsigned int size);
97 int __adis_read_reg(struct adis *adis, unsigned int reg,
98 unsigned int *val, unsigned int size);
101 * __adis_write_reg_8() - Write single byte to a register (unlocked)
102 * @adis: The adis device
103 * @reg: The address of the register to be written
104 * @value: The value to write
106 static inline int __adis_write_reg_8(struct adis *adis, unsigned int reg,
109 return __adis_write_reg(adis, reg, val, 1);
113 * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
114 * @adis: The adis device
115 * @reg: The address of the lower of the two registers
116 * @value: Value to be written
118 static inline int __adis_write_reg_16(struct adis *adis, unsigned int reg,
121 return __adis_write_reg(adis, reg, val, 2);
125 * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
126 * @adis: The adis device
127 * @reg: The address of the lower of the four register
128 * @value: Value to be written
130 static inline int __adis_write_reg_32(struct adis *adis, unsigned int reg,
133 return __adis_write_reg(adis, reg, val, 4);
137 * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
138 * @adis: The adis device
139 * @reg: The address of the lower of the two registers
140 * @val: The value read back from the device
142 static inline int __adis_read_reg_16(struct adis *adis, unsigned int reg,
148 ret = __adis_read_reg(adis, reg, &tmp, 2);
156 * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
157 * @adis: The adis device
158 * @reg: The address of the lower of the two registers
159 * @val: The value read back from the device
161 static inline int __adis_read_reg_32(struct adis *adis, unsigned int reg,
167 ret = __adis_read_reg(adis, reg, &tmp, 4);
175 * adis_write_reg() - write N bytes to register
176 * @adis: The adis device
177 * @reg: The address of the lower of the two registers
178 * @value: The value to write to device (up to 4 bytes)
179 * @size: The size of the @value (in bytes)
181 static inline int adis_write_reg(struct adis *adis, unsigned int reg,
182 unsigned int val, unsigned int size)
186 mutex_lock(&adis->state_lock);
187 ret = __adis_write_reg(adis, reg, val, size);
188 mutex_unlock(&adis->state_lock);
194 * adis_read_reg() - read N bytes from register
195 * @adis: The adis device
196 * @reg: The address of the lower of the two registers
197 * @val: The value read back from the device
198 * @size: The size of the @val buffer
200 static int adis_read_reg(struct adis *adis, unsigned int reg,
201 unsigned int *val, unsigned int size)
205 mutex_lock(&adis->state_lock);
206 ret = __adis_read_reg(adis, reg, val, size);
207 mutex_unlock(&adis->state_lock);
213 * adis_write_reg_8() - Write single byte to a register
214 * @adis: The adis device
215 * @reg: The address of the register to be written
216 * @value: The value to write
218 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
221 return adis_write_reg(adis, reg, val, 1);
225 * adis_write_reg_16() - Write 2 bytes to a pair of registers
226 * @adis: The adis device
227 * @reg: The address of the lower of the two registers
228 * @value: Value to be written
230 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
233 return adis_write_reg(adis, reg, val, 2);
237 * adis_write_reg_32() - write 4 bytes to four registers
238 * @adis: The adis device
239 * @reg: The address of the lower of the four register
240 * @value: Value to be written
242 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
245 return adis_write_reg(adis, reg, val, 4);
249 * adis_read_reg_16() - read 2 bytes from a 16-bit register
250 * @adis: The adis device
251 * @reg: The address of the lower of the two registers
252 * @val: The value read back from the device
254 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
260 ret = adis_read_reg(adis, reg, &tmp, 2);
268 * adis_read_reg_32() - read 4 bytes from a 32-bit register
269 * @adis: The adis device
270 * @reg: The address of the lower of the two registers
271 * @val: The value read back from the device
273 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
279 ret = adis_read_reg(adis, reg, &tmp, 4);
286 int adis_enable_irq(struct adis *adis, bool enable);
287 int __adis_check_status(struct adis *adis);
289 static inline int adis_check_status(struct adis *adis)
293 mutex_lock(&adis->state_lock);
294 ret = __adis_check_status(adis);
295 mutex_unlock(&adis->state_lock);
300 int adis_initial_startup(struct adis *adis);
302 int adis_single_conversion(struct iio_dev *indio_dev,
303 const struct iio_chan_spec *chan, unsigned int error_mask,
306 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
307 .type = IIO_VOLTAGE, \
310 .extend_name = name, \
311 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
312 BIT(IIO_CHAN_INFO_SCALE), \
313 .info_mask_shared_by_all = info_all, \
315 .scan_index = (si), \
318 .realbits = (bits), \
320 .endianness = IIO_BE, \
324 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
325 ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
327 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
328 ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
330 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
334 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
335 BIT(IIO_CHAN_INFO_SCALE) | \
336 BIT(IIO_CHAN_INFO_OFFSET), \
337 .info_mask_shared_by_all = info_all, \
339 .scan_index = (si), \
342 .realbits = (bits), \
344 .endianness = IIO_BE, \
348 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
351 .channel2 = IIO_MOD_ ## mod, \
352 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
354 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
355 .info_mask_shared_by_all = info_all, \
357 .scan_index = (si), \
360 .realbits = (bits), \
362 .endianness = IIO_BE, \
366 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
367 ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
369 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \
370 ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
372 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
373 ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
375 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
376 ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
378 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
381 * struct adis_burst - ADIS data for burst transfers
382 * @en burst mode enabled
383 * @reg_cmd register command that triggers burst
384 * @extra_len extra length to account in the SPI RX buffer
388 unsigned int reg_cmd;
389 unsigned int extra_len;
392 int adis_setup_buffer_and_trigger(struct adis *adis,
393 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
394 void adis_cleanup_buffer_and_trigger(struct adis *adis,
395 struct iio_dev *indio_dev);
397 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
398 void adis_remove_trigger(struct adis *adis);
400 int adis_update_scan_mode(struct iio_dev *indio_dev,
401 const unsigned long *scan_mask);
403 #else /* CONFIG_IIO_BUFFER */
405 static inline int adis_setup_buffer_and_trigger(struct adis *adis,
406 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
411 static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
412 struct iio_dev *indio_dev)
416 static inline int adis_probe_trigger(struct adis *adis,
417 struct iio_dev *indio_dev)
422 static inline void adis_remove_trigger(struct adis *adis)
426 #define adis_update_scan_mode NULL
428 #endif /* CONFIG_IIO_BUFFER */
430 #ifdef CONFIG_DEBUG_FS
432 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
433 unsigned int reg, unsigned int writeval, unsigned int *readval);
437 #define adis_debugfs_reg_access NULL