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