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/cleanup.h>
13 #include <linux/spi/spi.h>
14 #include <linux/interrupt.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/types.h>
18 #define ADIS_WRITE_REG(reg) ((0x80 | (reg)))
19 #define ADIS_READ_REG(reg) ((reg) & 0x7f)
21 #define ADIS_PAGE_SIZE 0x80
22 #define ADIS_REG_PAGE_ID 0x00
28 * struct adis_timeouts - ADIS chip variant timeouts
29 * @reset_ms - Wait time after rst pin goes inactive
30 * @sw_reset_ms - Wait time after sw reset command
31 * @self_test_ms - Wait time after self test command
40 * struct adis_data - ADIS chip variant specific data
41 * @read_delay: SPI delay for read operations in us
42 * @write_delay: SPI delay for write operations in us
43 * @cs_change_delay: SPI delay between CS changes in us
44 * @glob_cmd_reg: Register address of the GLOB_CMD register
45 * @msc_ctrl_reg: Register address of the MSC_CTRL register
46 * @diag_stat_reg: Register address of the DIAG_STAT register
47 * @prod_id_reg: Register address of the PROD_ID register
48 * @prod_id: Product ID code that should be expected when reading @prod_id_reg
49 * @self_test_mask: Bitmask of supported self-test operations
50 * @self_test_reg: Register address to request self test command
51 * @self_test_no_autoclear: True if device's self-test needs clear of ctrl reg
52 * @status_error_msgs: Array of error messages
53 * @status_error_mask: Bitmask of errors supported by the device
54 * @timeouts: Chip specific delays
55 * @enable_irq: Hook for ADIS devices that have a special IRQ enable/disable
56 * @unmasked_drdy: True for devices that cannot mask/unmask the data ready pin
57 * @has_paging: True if ADIS device has paged registers
58 * @burst_reg_cmd: Register command that triggers burst
59 * @burst_len: Burst size in the SPI RX buffer. If @burst_max_len is defined,
60 * this should be the minimum size supported by the device.
61 * @burst_max_len: Holds the maximum burst size when the device supports
62 * more than one burst mode with different sizes
63 * @burst_max_speed_hz: Maximum spi speed that can be used in burst mode
66 unsigned int read_delay
;
67 unsigned int write_delay
;
68 unsigned int cs_change_delay
;
70 unsigned int glob_cmd_reg
;
71 unsigned int msc_ctrl_reg
;
72 unsigned int diag_stat_reg
;
73 unsigned int prod_id_reg
;
77 unsigned int self_test_mask
;
78 unsigned int self_test_reg
;
79 bool self_test_no_autoclear
;
80 const struct adis_timeout
*timeouts
;
82 const char * const *status_error_msgs
;
83 unsigned int status_error_mask
;
85 int (*enable_irq
)(struct adis
*adis
, bool enable
);
91 unsigned int burst_reg_cmd
;
92 unsigned int burst_len
;
93 unsigned int burst_max_len
;
94 unsigned int burst_max_speed_hz
;
98 * struct adis - ADIS device instance data
99 * @spi: Reference to SPI device which owns this ADIS IIO device
100 * @trig: IIO trigger object data
101 * @data: ADIS chip variant specific data
102 * @burst: ADIS burst transfer information
103 * @burst_extra_len: Burst extra length. Should only be used by devices that can
104 * dynamically change their burst mode length.
105 * @state_lock: Lock used by the device to protect state
106 * @msg: SPI message object
107 * @xfer: SPI transfer objects to be used for a @msg
108 * @current_page: Some ADIS devices have registers, this selects current page
109 * @irq_flag: IRQ handling flags as passed to request_irq()
110 * @buffer: Data buffer for information read from the device
111 * @tx: DMA safe TX buffer for SPI transfers
112 * @rx: DMA safe RX buffer for SPI transfers
115 struct spi_device
*spi
;
116 struct iio_trigger
*trig
;
118 const struct adis_data
*data
;
119 unsigned int burst_extra_len
;
121 * The state_lock is meant to be used during operations that require
122 * a sequence of SPI R/W in order to protect the SPI transfer
123 * information (fields 'xfer', 'msg' & 'current_page') between
124 * potential concurrent accesses.
125 * This lock is used by all "adis_{functions}" that have to read/write
126 * registers. These functions also have unlocked variants
127 * (see "__adis_{functions}"), which don't hold this lock.
128 * This allows users of the ADIS library to group SPI R/W into
129 * the drivers, but they also must manage this lock themselves.
131 struct mutex state_lock
;
132 struct spi_message msg
;
133 struct spi_transfer
*xfer
;
134 unsigned int current_page
;
135 unsigned long irq_flag
;
138 u8 tx
[10] __aligned(IIO_DMA_MINALIGN
);
142 int adis_init(struct adis
*adis
, struct iio_dev
*indio_dev
,
143 struct spi_device
*spi
, const struct adis_data
*data
);
144 int __adis_reset(struct adis
*adis
);
147 * adis_reset() - Reset the device
148 * @adis: The adis device
150 * Returns 0 on success, a negative error code otherwise
152 static inline int adis_reset(struct adis
*adis
)
154 guard(mutex
)(&adis
->state_lock
);
155 return __adis_reset(adis
);
158 int __adis_write_reg(struct adis
*adis
, unsigned int reg
,
159 unsigned int val
, unsigned int size
);
160 int __adis_read_reg(struct adis
*adis
, unsigned int reg
,
161 unsigned int *val
, unsigned int size
);
164 * __adis_write_reg_8() - Write single byte to a register (unlocked)
165 * @adis: The adis device
166 * @reg: The address of the register to be written
167 * @value: The value to write
169 static inline int __adis_write_reg_8(struct adis
*adis
, unsigned int reg
,
172 return __adis_write_reg(adis
, reg
, val
, 1);
176 * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
177 * @adis: The adis device
178 * @reg: The address of the lower of the two registers
179 * @value: Value to be written
181 static inline int __adis_write_reg_16(struct adis
*adis
, unsigned int reg
,
184 return __adis_write_reg(adis
, reg
, val
, 2);
188 * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
189 * @adis: The adis device
190 * @reg: The address of the lower of the four register
191 * @value: Value to be written
193 static inline int __adis_write_reg_32(struct adis
*adis
, unsigned int reg
,
196 return __adis_write_reg(adis
, reg
, val
, 4);
200 * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
201 * @adis: The adis device
202 * @reg: The address of the lower of the two registers
203 * @val: The value read back from the device
205 static inline int __adis_read_reg_16(struct adis
*adis
, unsigned int reg
,
211 ret
= __adis_read_reg(adis
, reg
, &tmp
, 2);
219 * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
220 * @adis: The adis device
221 * @reg: The address of the lower of the two registers
222 * @val: The value read back from the device
224 static inline int __adis_read_reg_32(struct adis
*adis
, unsigned int reg
,
230 ret
= __adis_read_reg(adis
, reg
, &tmp
, 4);
238 * adis_write_reg() - write N bytes to register
239 * @adis: The adis device
240 * @reg: The address of the lower of the two registers
241 * @value: The value to write to device (up to 4 bytes)
242 * @size: The size of the @value (in bytes)
244 static inline int adis_write_reg(struct adis
*adis
, unsigned int reg
,
245 unsigned int val
, unsigned int size
)
247 guard(mutex
)(&adis
->state_lock
);
248 return __adis_write_reg(adis
, reg
, val
, size
);
252 * adis_read_reg() - read N bytes from register
253 * @adis: The adis device
254 * @reg: The address of the lower of the two registers
255 * @val: The value read back from the device
256 * @size: The size of the @val buffer
258 static int adis_read_reg(struct adis
*adis
, unsigned int reg
,
259 unsigned int *val
, unsigned int size
)
261 guard(mutex
)(&adis
->state_lock
);
262 return __adis_read_reg(adis
, reg
, val
, size
);
266 * adis_write_reg_8() - Write single byte to a register
267 * @adis: The adis device
268 * @reg: The address of the register to be written
269 * @value: The value to write
271 static inline int adis_write_reg_8(struct adis
*adis
, unsigned int reg
,
274 return adis_write_reg(adis
, reg
, val
, 1);
278 * adis_write_reg_16() - Write 2 bytes to a pair of registers
279 * @adis: The adis device
280 * @reg: The address of the lower of the two registers
281 * @value: Value to be written
283 static inline int adis_write_reg_16(struct adis
*adis
, unsigned int reg
,
286 return adis_write_reg(adis
, reg
, val
, 2);
290 * adis_write_reg_32() - write 4 bytes to four registers
291 * @adis: The adis device
292 * @reg: The address of the lower of the four register
293 * @value: Value to be written
295 static inline int adis_write_reg_32(struct adis
*adis
, unsigned int reg
,
298 return adis_write_reg(adis
, reg
, val
, 4);
302 * adis_read_reg_16() - read 2 bytes from a 16-bit register
303 * @adis: The adis device
304 * @reg: The address of the lower of the two registers
305 * @val: The value read back from the device
307 static inline int adis_read_reg_16(struct adis
*adis
, unsigned int reg
,
313 ret
= adis_read_reg(adis
, reg
, &tmp
, 2);
321 * adis_read_reg_32() - read 4 bytes from a 32-bit register
322 * @adis: The adis device
323 * @reg: The address of the lower of the two registers
324 * @val: The value read back from the device
326 static inline int adis_read_reg_32(struct adis
*adis
, unsigned int reg
,
332 ret
= adis_read_reg(adis
, reg
, &tmp
, 4);
339 int __adis_update_bits_base(struct adis
*adis
, unsigned int reg
, const u32 mask
,
340 const u32 val
, u8 size
);
342 * adis_update_bits_base() - ADIS Update bits function - Locked version
343 * @adis: The adis device
344 * @reg: The address of the lower of the two registers
345 * @mask: Bitmask to change
346 * @val: Value to be written
347 * @size: Size of the register to update
349 * Updates the desired bits of @reg in accordance with @mask and @val.
351 static inline int adis_update_bits_base(struct adis
*adis
, unsigned int reg
,
352 const u32 mask
, const u32 val
, u8 size
)
354 guard(mutex
)(&adis
->state_lock
);
355 return __adis_update_bits_base(adis
, reg
, mask
, val
, size
);
359 * adis_update_bits() - Wrapper macro for adis_update_bits_base - Locked version
360 * @adis: The adis device
361 * @reg: The address of the lower of the two registers
362 * @mask: Bitmask to change
363 * @val: Value to be written
365 * This macro evaluates the sizeof of @val at compile time and calls
366 * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
367 * @val can lead to undesired behavior if the register to update is 16bit.
369 #define adis_update_bits(adis, reg, mask, val) ({ \
370 BUILD_BUG_ON(sizeof(val) != 2 && sizeof(val) != 4); \
371 adis_update_bits_base(adis, reg, mask, val, sizeof(val)); \
375 * adis_update_bits() - Wrapper macro for adis_update_bits_base
376 * @adis: The adis device
377 * @reg: The address of the lower of the two registers
378 * @mask: Bitmask to change
379 * @val: Value to be written
381 * This macro evaluates the sizeof of @val at compile time and calls
382 * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
383 * @val can lead to undesired behavior if the register to update is 16bit.
385 #define __adis_update_bits(adis, reg, mask, val) ({ \
386 BUILD_BUG_ON(sizeof(val) != 2 && sizeof(val) != 4); \
387 __adis_update_bits_base(adis, reg, mask, val, sizeof(val)); \
390 int __adis_check_status(struct adis
*adis
);
391 int __adis_initial_startup(struct adis
*adis
);
392 int __adis_enable_irq(struct adis
*adis
, bool enable
);
394 static inline int adis_enable_irq(struct adis
*adis
, bool enable
)
396 guard(mutex
)(&adis
->state_lock
);
397 return __adis_enable_irq(adis
, enable
);
400 static inline int adis_check_status(struct adis
*adis
)
402 guard(mutex
)(&adis
->state_lock
);
403 return __adis_check_status(adis
);
406 #define adis_dev_auto_lock(adis) guard(mutex)(&(adis)->state_lock)
407 #define adis_dev_auto_scoped_lock(adis) \
408 scoped_guard(mutex, &(adis)->state_lock)
410 int adis_single_conversion(struct iio_dev
*indio_dev
,
411 const struct iio_chan_spec
*chan
,
412 unsigned int error_mask
, int *val
);
414 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
415 .type = IIO_VOLTAGE, \
418 .extend_name = name, \
419 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
420 BIT(IIO_CHAN_INFO_SCALE), \
421 .info_mask_shared_by_all = info_all, \
423 .scan_index = (si), \
426 .realbits = (bits), \
428 .endianness = IIO_BE, \
432 #define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
433 ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
435 #define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
436 ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
438 #define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
442 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
443 BIT(IIO_CHAN_INFO_SCALE) | \
444 BIT(IIO_CHAN_INFO_OFFSET), \
445 .info_mask_shared_by_all = info_all, \
447 .scan_index = (si), \
450 .realbits = (bits), \
452 .endianness = IIO_BE, \
456 #define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
459 .channel2 = IIO_MOD_ ## mod, \
460 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
462 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
463 .info_mask_shared_by_all = info_all, \
465 .scan_index = (si), \
468 .realbits = (bits), \
470 .endianness = IIO_BE, \
474 #define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
475 ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
477 #define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \
478 ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
480 #define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
481 ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
483 #define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
484 ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
486 #define devm_adis_setup_buffer_and_trigger(adis, indio_dev, trigger_handler) \
487 devm_adis_setup_buffer_and_trigger_with_attrs((adis), (indio_dev), \
488 (trigger_handler), NULL, \
491 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
494 devm_adis_setup_buffer_and_trigger_with_attrs(struct adis
*adis
,
495 struct iio_dev
*indio_dev
,
496 irq_handler_t trigger_handler
,
497 const struct iio_buffer_setup_ops
*ops
,
498 const struct iio_dev_attr
**buffer_attrs
);
500 int devm_adis_probe_trigger(struct adis
*adis
, struct iio_dev
*indio_dev
);
502 int adis_update_scan_mode(struct iio_dev
*indio_dev
,
503 const unsigned long *scan_mask
);
505 #else /* CONFIG_IIO_BUFFER */
508 devm_adis_setup_buffer_and_trigger_with_attrs(struct adis
*adis
,
509 struct iio_dev
*indio_dev
,
510 irq_handler_t trigger_handler
,
511 const struct iio_buffer_setup_ops
*ops
,
512 const struct iio_dev_attr
**buffer_attrs
)
517 static inline int devm_adis_probe_trigger(struct adis
*adis
,
518 struct iio_dev
*indio_dev
)
523 #define adis_update_scan_mode NULL
525 #endif /* CONFIG_IIO_BUFFER */
527 #ifdef CONFIG_DEBUG_FS
529 int adis_debugfs_reg_access(struct iio_dev
*indio_dev
,
530 unsigned int reg
, unsigned int writeval
,
531 unsigned int *readval
);
535 #define adis_debugfs_reg_access NULL