Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Common library for ADIS16XXX devices
4 *
5 * Copyright 2012 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 */
8
9#ifndef __IIO_ADIS_H__
10#define __IIO_ADIS_H__
11
12#include <linux/spi/spi.h>
13#include <linux/interrupt.h>
14#include <linux/iio/types.h>
15
16#define ADIS_WRITE_REG(reg) ((0x80 | (reg)))
17#define ADIS_READ_REG(reg) ((reg) & 0x7f)
18
19#define ADIS_PAGE_SIZE 0x80
20#define ADIS_REG_PAGE_ID 0x00
21
22struct adis;
23struct adis_burst;
24
25/**
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
30 */
31struct adis_timeout {
32 u16 reset_ms;
33 u16 sw_reset_ms;
34 u16 self_test_ms;
35};
36/**
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 * @prod_id_reg: Register address of the PROD_ID register
45 * @prod_id: Product ID code that should be expected when reading @prod_id_reg
46 * @self_test_mask: Bitmask of supported self-test operations
47 * @self_test_reg: Register address to request self test command
48 * @self_test_no_autoclear: True if device's self-test needs clear of ctrl reg
49 * @status_error_msgs: Array of error messgaes
50 * @status_error_mask: Bitmask of errors supported by the device
51 * @timeouts: Chip specific delays
52 * @enable_irq: Hook for ADIS devices that have a special IRQ enable/disable
53 * @has_paging: True if ADIS device has paged registers
54 */
55struct adis_data {
56 unsigned int read_delay;
57 unsigned int write_delay;
58 unsigned int cs_change_delay;
59
60 unsigned int glob_cmd_reg;
61 unsigned int msc_ctrl_reg;
62 unsigned int diag_stat_reg;
63 unsigned int prod_id_reg;
64
65 unsigned int prod_id;
66
67 unsigned int self_test_mask;
68 unsigned int self_test_reg;
69 bool self_test_no_autoclear;
70 const struct adis_timeout *timeouts;
71
72 const char * const *status_error_msgs;
73 unsigned int status_error_mask;
74
75 int (*enable_irq)(struct adis *adis, bool enable);
76
77 bool has_paging;
78};
79
80/**
81 * struct adis - ADIS device instance data
82 * @spi: Reference to SPI device which owns this ADIS IIO device
83 * @trig: IIO trigger object data
84 * @data: ADIS chip variant specific data
85 * @burst: ADIS burst transfer information
86 * @burst_extra_len: Burst extra length. Should only be used by devices that can
87 * dynamically change their burst mode length.
88 * @state_lock: Lock used by the device to protect state
89 * @msg: SPI message object
90 * @xfer: SPI transfer objects to be used for a @msg
91 * @current_page: Some ADIS devices have registers, this selects current page
92 * @irq_flag: IRQ handling flags as passed to request_irq()
93 * @buffer: Data buffer for information read from the device
94 * @tx: DMA safe TX buffer for SPI transfers
95 * @rx: DMA safe RX buffer for SPI transfers
96 */
97struct adis {
98 struct spi_device *spi;
99 struct iio_trigger *trig;
100
101 const struct adis_data *data;
102 struct adis_burst *burst;
103 unsigned int burst_extra_len;
104 /**
105 * The state_lock is meant to be used during operations that require
106 * a sequence of SPI R/W in order to protect the SPI transfer
107 * information (fields 'xfer', 'msg' & 'current_page') between
108 * potential concurrent accesses.
109 * This lock is used by all "adis_{functions}" that have to read/write
110 * registers. These functions also have unlocked variants
111 * (see "__adis_{functions}"), which don't hold this lock.
112 * This allows users of the ADIS library to group SPI R/W into
113 * the drivers, but they also must manage this lock themselves.
114 */
115 struct mutex state_lock;
116 struct spi_message msg;
117 struct spi_transfer *xfer;
118 unsigned int current_page;
119 unsigned long irq_flag;
120 void *buffer;
121
122 uint8_t tx[10] ____cacheline_aligned;
123 uint8_t rx[4];
124};
125
126int adis_init(struct adis *adis, struct iio_dev *indio_dev,
127 struct spi_device *spi, const struct adis_data *data);
128int __adis_reset(struct adis *adis);
129
130/**
131 * adis_reset() - Reset the device
132 * @adis: The adis device
133 *
134 * Returns 0 on success, a negative error code otherwise
135 */
136static inline int adis_reset(struct adis *adis)
137{
138 int ret;
139
140 mutex_lock(&adis->state_lock);
141 ret = __adis_reset(adis);
142 mutex_unlock(&adis->state_lock);
143
144 return ret;
145}
146
147int __adis_write_reg(struct adis *adis, unsigned int reg,
148 unsigned int val, unsigned int size);
149int __adis_read_reg(struct adis *adis, unsigned int reg,
150 unsigned int *val, unsigned int size);
151
152/**
153 * __adis_write_reg_8() - Write single byte to a register (unlocked)
154 * @adis: The adis device
155 * @reg: The address of the register to be written
156 * @value: The value to write
157 */
158static inline int __adis_write_reg_8(struct adis *adis, unsigned int reg,
159 uint8_t val)
160{
161 return __adis_write_reg(adis, reg, val, 1);
162}
163
164/**
165 * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
166 * @adis: The adis device
167 * @reg: The address of the lower of the two registers
168 * @value: Value to be written
169 */
170static inline int __adis_write_reg_16(struct adis *adis, unsigned int reg,
171 uint16_t val)
172{
173 return __adis_write_reg(adis, reg, val, 2);
174}
175
176/**
177 * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
178 * @adis: The adis device
179 * @reg: The address of the lower of the four register
180 * @value: Value to be written
181 */
182static inline int __adis_write_reg_32(struct adis *adis, unsigned int reg,
183 uint32_t val)
184{
185 return __adis_write_reg(adis, reg, val, 4);
186}
187
188/**
189 * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
190 * @adis: The adis device
191 * @reg: The address of the lower of the two registers
192 * @val: The value read back from the device
193 */
194static inline int __adis_read_reg_16(struct adis *adis, unsigned int reg,
195 uint16_t *val)
196{
197 unsigned int tmp;
198 int ret;
199
200 ret = __adis_read_reg(adis, reg, &tmp, 2);
201 if (ret == 0)
202 *val = tmp;
203
204 return ret;
205}
206
207/**
208 * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
209 * @adis: The adis device
210 * @reg: The address of the lower of the two registers
211 * @val: The value read back from the device
212 */
213static inline int __adis_read_reg_32(struct adis *adis, unsigned int reg,
214 uint32_t *val)
215{
216 unsigned int tmp;
217 int ret;
218
219 ret = __adis_read_reg(adis, reg, &tmp, 4);
220 if (ret == 0)
221 *val = tmp;
222
223 return ret;
224}
225
226/**
227 * adis_write_reg() - write N bytes to register
228 * @adis: The adis device
229 * @reg: The address of the lower of the two registers
230 * @value: The value to write to device (up to 4 bytes)
231 * @size: The size of the @value (in bytes)
232 */
233static inline int adis_write_reg(struct adis *adis, unsigned int reg,
234 unsigned int val, unsigned int size)
235{
236 int ret;
237
238 mutex_lock(&adis->state_lock);
239 ret = __adis_write_reg(adis, reg, val, size);
240 mutex_unlock(&adis->state_lock);
241
242 return ret;
243}
244
245/**
246 * adis_read_reg() - read N bytes from register
247 * @adis: The adis device
248 * @reg: The address of the lower of the two registers
249 * @val: The value read back from the device
250 * @size: The size of the @val buffer
251 */
252static int adis_read_reg(struct adis *adis, unsigned int reg,
253 unsigned int *val, unsigned int size)
254{
255 int ret;
256
257 mutex_lock(&adis->state_lock);
258 ret = __adis_read_reg(adis, reg, val, size);
259 mutex_unlock(&adis->state_lock);
260
261 return ret;
262}
263
264/**
265 * adis_write_reg_8() - Write single byte to a register
266 * @adis: The adis device
267 * @reg: The address of the register to be written
268 * @value: The value to write
269 */
270static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
271 uint8_t val)
272{
273 return adis_write_reg(adis, reg, val, 1);
274}
275
276/**
277 * adis_write_reg_16() - Write 2 bytes to a pair of registers
278 * @adis: The adis device
279 * @reg: The address of the lower of the two registers
280 * @value: Value to be written
281 */
282static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
283 uint16_t val)
284{
285 return adis_write_reg(adis, reg, val, 2);
286}
287
288/**
289 * adis_write_reg_32() - write 4 bytes to four registers
290 * @adis: The adis device
291 * @reg: The address of the lower of the four register
292 * @value: Value to be written
293 */
294static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
295 uint32_t val)
296{
297 return adis_write_reg(adis, reg, val, 4);
298}
299
300/**
301 * adis_read_reg_16() - read 2 bytes from a 16-bit register
302 * @adis: The adis device
303 * @reg: The address of the lower of the two registers
304 * @val: The value read back from the device
305 */
306static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
307 uint16_t *val)
308{
309 unsigned int tmp;
310 int ret;
311
312 ret = adis_read_reg(adis, reg, &tmp, 2);
313 if (ret == 0)
314 *val = tmp;
315
316 return ret;
317}
318
319/**
320 * adis_read_reg_32() - read 4 bytes from a 32-bit register
321 * @adis: The adis device
322 * @reg: The address of the lower of the two registers
323 * @val: The value read back from the device
324 */
325static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
326 uint32_t *val)
327{
328 unsigned int tmp;
329 int ret;
330
331 ret = adis_read_reg(adis, reg, &tmp, 4);
332 if (ret == 0)
333 *val = tmp;
334
335 return ret;
336}
337
338int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
339 const u32 val, u8 size);
340/**
341 * adis_update_bits_base() - ADIS Update bits function - Locked version
342 * @adis: The adis device
343 * @reg: The address of the lower of the two registers
344 * @mask: Bitmask to change
345 * @val: Value to be written
346 * @size: Size of the register to update
347 *
348 * Updates the desired bits of @reg in accordance with @mask and @val.
349 */
350static inline int adis_update_bits_base(struct adis *adis, unsigned int reg,
351 const u32 mask, const u32 val, u8 size)
352{
353 int ret;
354
355 mutex_lock(&adis->state_lock);
356 ret = __adis_update_bits_base(adis, reg, mask, val, size);
357 mutex_unlock(&adis->state_lock);
358 return ret;
359}
360
361/**
362 * adis_update_bits() - Wrapper macro for adis_update_bits_base - Locked version
363 * @adis: The adis device
364 * @reg: The address of the lower of the two registers
365 * @mask: Bitmask to change
366 * @val: Value to be written
367 *
368 * This macro evaluates the sizeof of @val at compile time and calls
369 * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
370 * @val can lead to undesired behavior if the register to update is 16bit.
371 */
372#define adis_update_bits(adis, reg, mask, val) ({ \
373 BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8); \
374 __builtin_choose_expr(sizeof(val) == 4, \
375 adis_update_bits_base(adis, reg, mask, val, 4), \
376 adis_update_bits_base(adis, reg, mask, val, 2)); \
377})
378
379/**
380 * adis_update_bits() - Wrapper macro for adis_update_bits_base
381 * @adis: The adis device
382 * @reg: The address of the lower of the two registers
383 * @mask: Bitmask to change
384 * @val: Value to be written
385 *
386 * This macro evaluates the sizeof of @val at compile time and calls
387 * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
388 * @val can lead to undesired behavior if the register to update is 16bit.
389 */
390#define __adis_update_bits(adis, reg, mask, val) ({ \
391 BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8); \
392 __builtin_choose_expr(sizeof(val) == 4, \
393 __adis_update_bits_base(adis, reg, mask, val, 4), \
394 __adis_update_bits_base(adis, reg, mask, val, 2)); \
395})
396
397int adis_enable_irq(struct adis *adis, bool enable);
398int __adis_check_status(struct adis *adis);
399int __adis_initial_startup(struct adis *adis);
400
401static inline int adis_check_status(struct adis *adis)
402{
403 int ret;
404
405 mutex_lock(&adis->state_lock);
406 ret = __adis_check_status(adis);
407 mutex_unlock(&adis->state_lock);
408
409 return ret;
410}
411
412/* locked version of __adis_initial_startup() */
413static inline int adis_initial_startup(struct adis *adis)
414{
415 int ret;
416
417 mutex_lock(&adis->state_lock);
418 ret = __adis_initial_startup(adis);
419 mutex_unlock(&adis->state_lock);
420
421 return ret;
422}
423
424int adis_single_conversion(struct iio_dev *indio_dev,
425 const struct iio_chan_spec *chan, unsigned int error_mask,
426 int *val);
427
428#define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
429 .type = IIO_VOLTAGE, \
430 .indexed = 1, \
431 .channel = (chan), \
432 .extend_name = name, \
433 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
434 BIT(IIO_CHAN_INFO_SCALE), \
435 .info_mask_shared_by_all = info_all, \
436 .address = (addr), \
437 .scan_index = (si), \
438 .scan_type = { \
439 .sign = 'u', \
440 .realbits = (bits), \
441 .storagebits = 16, \
442 .endianness = IIO_BE, \
443 }, \
444}
445
446#define ADIS_SUPPLY_CHAN(addr, si, info_all, bits) \
447 ADIS_VOLTAGE_CHAN(addr, si, 0, "supply", info_all, bits)
448
449#define ADIS_AUX_ADC_CHAN(addr, si, info_all, bits) \
450 ADIS_VOLTAGE_CHAN(addr, si, 1, NULL, info_all, bits)
451
452#define ADIS_TEMP_CHAN(addr, si, info_all, bits) { \
453 .type = IIO_TEMP, \
454 .indexed = 1, \
455 .channel = 0, \
456 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
457 BIT(IIO_CHAN_INFO_SCALE) | \
458 BIT(IIO_CHAN_INFO_OFFSET), \
459 .info_mask_shared_by_all = info_all, \
460 .address = (addr), \
461 .scan_index = (si), \
462 .scan_type = { \
463 .sign = 'u', \
464 .realbits = (bits), \
465 .storagebits = 16, \
466 .endianness = IIO_BE, \
467 }, \
468}
469
470#define ADIS_MOD_CHAN(_type, mod, addr, si, info_sep, info_all, bits) { \
471 .type = (_type), \
472 .modified = 1, \
473 .channel2 = IIO_MOD_ ## mod, \
474 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
475 info_sep, \
476 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
477 .info_mask_shared_by_all = info_all, \
478 .address = (addr), \
479 .scan_index = (si), \
480 .scan_type = { \
481 .sign = 's', \
482 .realbits = (bits), \
483 .storagebits = 16, \
484 .endianness = IIO_BE, \
485 }, \
486}
487
488#define ADIS_ACCEL_CHAN(mod, addr, si, info_sep, info_all, bits) \
489 ADIS_MOD_CHAN(IIO_ACCEL, mod, addr, si, info_sep, info_all, bits)
490
491#define ADIS_GYRO_CHAN(mod, addr, si, info_sep, info_all, bits) \
492 ADIS_MOD_CHAN(IIO_ANGL_VEL, mod, addr, si, info_sep, info_all, bits)
493
494#define ADIS_INCLI_CHAN(mod, addr, si, info_sep, info_all, bits) \
495 ADIS_MOD_CHAN(IIO_INCLI, mod, addr, si, info_sep, info_all, bits)
496
497#define ADIS_ROT_CHAN(mod, addr, si, info_sep, info_all, bits) \
498 ADIS_MOD_CHAN(IIO_ROT, mod, addr, si, info_sep, info_all, bits)
499
500#ifdef CONFIG_IIO_ADIS_LIB_BUFFER
501
502/**
503 * struct adis_burst - ADIS data for burst transfers
504 * @en burst mode enabled
505 * @reg_cmd register command that triggers burst
506 * @extra_len extra length to account in the SPI RX buffer
507 * @burst_max_len holds the maximum burst size when the device supports
508 * more than one burst mode with different sizes
509 */
510struct adis_burst {
511 bool en;
512 unsigned int reg_cmd;
513 const u32 extra_len;
514 const u32 burst_max_len;
515};
516
517int
518devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
519 irq_handler_t trigger_handler);
520int adis_setup_buffer_and_trigger(struct adis *adis,
521 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
522void adis_cleanup_buffer_and_trigger(struct adis *adis,
523 struct iio_dev *indio_dev);
524
525int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
526int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
527void adis_remove_trigger(struct adis *adis);
528
529int adis_update_scan_mode(struct iio_dev *indio_dev,
530 const unsigned long *scan_mask);
531
532#else /* CONFIG_IIO_BUFFER */
533
534static inline int
535devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
536 irq_handler_t trigger_handler)
537{
538 return 0;
539}
540
541static inline int adis_setup_buffer_and_trigger(struct adis *adis,
542 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
543{
544 return 0;
545}
546
547static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
548 struct iio_dev *indio_dev)
549{
550}
551
552static inline int devm_adis_probe_trigger(struct adis *adis,
553 struct iio_dev *indio_dev)
554{
555 return 0;
556}
557
558static inline int adis_probe_trigger(struct adis *adis,
559 struct iio_dev *indio_dev)
560{
561 return 0;
562}
563
564static inline void adis_remove_trigger(struct adis *adis)
565{
566}
567
568#define adis_update_scan_mode NULL
569
570#endif /* CONFIG_IIO_BUFFER */
571
572#ifdef CONFIG_DEBUG_FS
573
574int adis_debugfs_reg_access(struct iio_dev *indio_dev,
575 unsigned int reg, unsigned int writeval, unsigned int *readval);
576
577#else
578
579#define adis_debugfs_reg_access NULL
580
581#endif
582
583#endif