• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
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 messages
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  * @unmasked_drdy: True for devices that cannot mask/unmask the data ready pin
54  * @has_paging: True if ADIS device has paged registers
55  * @burst_reg_cmd:	Register command that triggers burst
56  * @burst_len:		Burst size in the SPI RX buffer. If @burst_max_len is defined,
57  *			this should be the minimum size supported by the device.
58  * @burst_max_len:	Holds the maximum burst size when the device supports
59  *			more than one burst mode with different sizes
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 };
90 
91 /**
92  * struct adis - ADIS device instance data
93  * @spi: Reference to SPI device which owns this ADIS IIO device
94  * @trig: IIO trigger object data
95  * @data: ADIS chip variant specific data
96  * @burst: ADIS burst transfer information
97  * @burst_extra_len: Burst extra length. Should only be used by devices that can
98  *		     dynamically change their burst mode length.
99  * @state_lock: Lock used by the device to protect state
100  * @msg: SPI message object
101  * @xfer: SPI transfer objects to be used for a @msg
102  * @current_page: Some ADIS devices have registers, this selects current page
103  * @irq_flag: IRQ handling flags as passed to request_irq()
104  * @buffer: Data buffer for information read from the device
105  * @tx: DMA safe TX buffer for SPI transfers
106  * @rx: DMA safe RX buffer for SPI transfers
107  */
108 struct adis {
109 	struct spi_device	*spi;
110 	struct iio_trigger	*trig;
111 
112 	const struct adis_data	*data;
113 	unsigned int		burst_extra_len;
114 	/**
115 	 * The state_lock is meant to be used during operations that require
116 	 * a sequence of SPI R/W in order to protect the SPI transfer
117 	 * information (fields 'xfer', 'msg' & 'current_page') between
118 	 * potential concurrent accesses.
119 	 * This lock is used by all "adis_{functions}" that have to read/write
120 	 * registers. These functions also have unlocked variants
121 	 * (see "__adis_{functions}"), which don't hold this lock.
122 	 * This allows users of the ADIS library to group SPI R/W into
123 	 * the drivers, but they also must manage this lock themselves.
124 	 */
125 	struct mutex		state_lock;
126 	struct spi_message	msg;
127 	struct spi_transfer	*xfer;
128 	unsigned int		current_page;
129 	unsigned long		irq_flag;
130 	void			*buffer;
131 
132 	u8			tx[10] ____cacheline_aligned;
133 	u8			rx[4];
134 };
135 
136 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
137 	      struct spi_device *spi, const struct adis_data *data);
138 int __adis_reset(struct adis *adis);
139 
140 /**
141  * adis_reset() - Reset the device
142  * @adis: The adis device
143  *
144  * Returns 0 on success, a negative error code otherwise
145  */
adis_reset(struct adis * adis)146 static inline int adis_reset(struct adis *adis)
147 {
148 	int ret;
149 
150 	mutex_lock(&adis->state_lock);
151 	ret = __adis_reset(adis);
152 	mutex_unlock(&adis->state_lock);
153 
154 	return ret;
155 }
156 
157 int __adis_write_reg(struct adis *adis, unsigned int reg,
158 		     unsigned int val, unsigned int size);
159 int __adis_read_reg(struct adis *adis, unsigned int reg,
160 		    unsigned int *val, unsigned int size);
161 
162 /**
163  * __adis_write_reg_8() - Write single byte to a register (unlocked)
164  * @adis: The adis device
165  * @reg: The address of the register to be written
166  * @value: The value to write
167  */
__adis_write_reg_8(struct adis * adis,unsigned int reg,u8 val)168 static inline int __adis_write_reg_8(struct adis *adis, unsigned int reg,
169 				     u8 val)
170 {
171 	return __adis_write_reg(adis, reg, val, 1);
172 }
173 
174 /**
175  * __adis_write_reg_16() - Write 2 bytes to a pair of registers (unlocked)
176  * @adis: The adis device
177  * @reg: The address of the lower of the two registers
178  * @value: Value to be written
179  */
__adis_write_reg_16(struct adis * adis,unsigned int reg,u16 val)180 static inline int __adis_write_reg_16(struct adis *adis, unsigned int reg,
181 				      u16 val)
182 {
183 	return __adis_write_reg(adis, reg, val, 2);
184 }
185 
186 /**
187  * __adis_write_reg_32() - write 4 bytes to four registers (unlocked)
188  * @adis: The adis device
189  * @reg: The address of the lower of the four register
190  * @value: Value to be written
191  */
__adis_write_reg_32(struct adis * adis,unsigned int reg,u32 val)192 static inline int __adis_write_reg_32(struct adis *adis, unsigned int reg,
193 				      u32 val)
194 {
195 	return __adis_write_reg(adis, reg, val, 4);
196 }
197 
198 /**
199  * __adis_read_reg_16() - read 2 bytes from a 16-bit register (unlocked)
200  * @adis: The adis device
201  * @reg: The address of the lower of the two registers
202  * @val: The value read back from the device
203  */
__adis_read_reg_16(struct adis * adis,unsigned int reg,u16 * val)204 static inline int __adis_read_reg_16(struct adis *adis, unsigned int reg,
205 				     u16 *val)
206 {
207 	unsigned int tmp;
208 	int ret;
209 
210 	ret = __adis_read_reg(adis, reg, &tmp, 2);
211 	if (ret == 0)
212 		*val = tmp;
213 
214 	return ret;
215 }
216 
217 /**
218  * __adis_read_reg_32() - read 4 bytes from a 32-bit register (unlocked)
219  * @adis: The adis device
220  * @reg: The address of the lower of the two registers
221  * @val: The value read back from the device
222  */
__adis_read_reg_32(struct adis * adis,unsigned int reg,u32 * val)223 static inline int __adis_read_reg_32(struct adis *adis, unsigned int reg,
224 				     u32 *val)
225 {
226 	unsigned int tmp;
227 	int ret;
228 
229 	ret = __adis_read_reg(adis, reg, &tmp, 4);
230 	if (ret == 0)
231 		*val = tmp;
232 
233 	return ret;
234 }
235 
236 /**
237  * adis_write_reg() - write N bytes to register
238  * @adis: The adis device
239  * @reg: The address of the lower of the two registers
240  * @value: The value to write to device (up to 4 bytes)
241  * @size: The size of the @value (in bytes)
242  */
adis_write_reg(struct adis * adis,unsigned int reg,unsigned int val,unsigned int size)243 static inline int adis_write_reg(struct adis *adis, unsigned int reg,
244 				 unsigned int val, unsigned int size)
245 {
246 	int ret;
247 
248 	mutex_lock(&adis->state_lock);
249 	ret = __adis_write_reg(adis, reg, val, size);
250 	mutex_unlock(&adis->state_lock);
251 
252 	return ret;
253 }
254 
255 /**
256  * adis_read_reg() - read N bytes from register
257  * @adis: The adis device
258  * @reg: The address of the lower of the two registers
259  * @val: The value read back from the device
260  * @size: The size of the @val buffer
261  */
adis_read_reg(struct adis * adis,unsigned int reg,unsigned int * val,unsigned int size)262 static int adis_read_reg(struct adis *adis, unsigned int reg,
263 			 unsigned int *val, unsigned int size)
264 {
265 	int ret;
266 
267 	mutex_lock(&adis->state_lock);
268 	ret = __adis_read_reg(adis, reg, val, size);
269 	mutex_unlock(&adis->state_lock);
270 
271 	return ret;
272 }
273 
274 /**
275  * adis_write_reg_8() - Write single byte to a register
276  * @adis: The adis device
277  * @reg: The address of the register to be written
278  * @value: The value to write
279  */
adis_write_reg_8(struct adis * adis,unsigned int reg,u8 val)280 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
281 				   u8 val)
282 {
283 	return adis_write_reg(adis, reg, val, 1);
284 }
285 
286 /**
287  * adis_write_reg_16() - Write 2 bytes to a pair of registers
288  * @adis: The adis device
289  * @reg: The address of the lower of the two registers
290  * @value: Value to be written
291  */
adis_write_reg_16(struct adis * adis,unsigned int reg,u16 val)292 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
293 				    u16 val)
294 {
295 	return adis_write_reg(adis, reg, val, 2);
296 }
297 
298 /**
299  * adis_write_reg_32() - write 4 bytes to four registers
300  * @adis: The adis device
301  * @reg: The address of the lower of the four register
302  * @value: Value to be written
303  */
adis_write_reg_32(struct adis * adis,unsigned int reg,u32 val)304 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
305 				    u32 val)
306 {
307 	return adis_write_reg(adis, reg, val, 4);
308 }
309 
310 /**
311  * adis_read_reg_16() - read 2 bytes from a 16-bit register
312  * @adis: The adis device
313  * @reg: The address of the lower of the two registers
314  * @val: The value read back from the device
315  */
adis_read_reg_16(struct adis * adis,unsigned int reg,u16 * val)316 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
317 				   u16 *val)
318 {
319 	unsigned int tmp;
320 	int ret;
321 
322 	ret = adis_read_reg(adis, reg, &tmp, 2);
323 	if (ret == 0)
324 		*val = tmp;
325 
326 	return ret;
327 }
328 
329 /**
330  * adis_read_reg_32() - read 4 bytes from a 32-bit register
331  * @adis: The adis device
332  * @reg: The address of the lower of the two registers
333  * @val: The value read back from the device
334  */
adis_read_reg_32(struct adis * adis,unsigned int reg,u32 * val)335 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
336 				   u32 *val)
337 {
338 	unsigned int tmp;
339 	int ret;
340 
341 	ret = adis_read_reg(adis, reg, &tmp, 4);
342 	if (ret == 0)
343 		*val = tmp;
344 
345 	return ret;
346 }
347 
348 int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
349 			    const u32 val, u8 size);
350 /**
351  * adis_update_bits_base() - ADIS Update bits function - Locked version
352  * @adis: The adis device
353  * @reg: The address of the lower of the two registers
354  * @mask: Bitmask to change
355  * @val: Value to be written
356  * @size: Size of the register to update
357  *
358  * Updates the desired bits of @reg in accordance with @mask and @val.
359  */
adis_update_bits_base(struct adis * adis,unsigned int reg,const u32 mask,const u32 val,u8 size)360 static inline int adis_update_bits_base(struct adis *adis, unsigned int reg,
361 					const u32 mask, const u32 val, u8 size)
362 {
363 	int ret;
364 
365 	mutex_lock(&adis->state_lock);
366 	ret = __adis_update_bits_base(adis, reg, mask, val, size);
367 	mutex_unlock(&adis->state_lock);
368 	return ret;
369 }
370 
371 /**
372  * adis_update_bits() - Wrapper macro for adis_update_bits_base - Locked version
373  * @adis: The adis device
374  * @reg: The address of the lower of the two registers
375  * @mask: Bitmask to change
376  * @val: Value to be written
377  *
378  * This macro evaluates the sizeof of @val at compile time and calls
379  * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
380  * @val can lead to undesired behavior if the register to update is 16bit.
381  */
382 #define adis_update_bits(adis, reg, mask, val) ({			\
383 	BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8);		\
384 	__builtin_choose_expr(sizeof(val) == 4,				\
385 		adis_update_bits_base(adis, reg, mask, val, 4),         \
386 		adis_update_bits_base(adis, reg, mask, val, 2));	\
387 })
388 
389 /**
390  * adis_update_bits() - Wrapper macro for adis_update_bits_base
391  * @adis: The adis device
392  * @reg: The address of the lower of the two registers
393  * @mask: Bitmask to change
394  * @val: Value to be written
395  *
396  * This macro evaluates the sizeof of @val at compile time and calls
397  * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for
398  * @val can lead to undesired behavior if the register to update is 16bit.
399  */
400 #define __adis_update_bits(adis, reg, mask, val) ({			\
401 	BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8);		\
402 	__builtin_choose_expr(sizeof(val) == 4,				\
403 		__adis_update_bits_base(adis, reg, mask, val, 4),	\
404 		__adis_update_bits_base(adis, reg, mask, val, 2));	\
405 })
406 
407 int __adis_check_status(struct adis *adis);
408 int __adis_initial_startup(struct adis *adis);
409 int __adis_enable_irq(struct adis *adis, bool enable);
410 
adis_enable_irq(struct adis * adis,bool enable)411 static inline int adis_enable_irq(struct adis *adis, bool enable)
412 {
413 	int ret;
414 
415 	mutex_lock(&adis->state_lock);
416 	ret = __adis_enable_irq(adis, enable);
417 	mutex_unlock(&adis->state_lock);
418 
419 	return ret;
420 }
421 
adis_check_status(struct adis * adis)422 static inline int adis_check_status(struct adis *adis)
423 {
424 	int ret;
425 
426 	mutex_lock(&adis->state_lock);
427 	ret = __adis_check_status(adis);
428 	mutex_unlock(&adis->state_lock);
429 
430 	return ret;
431 }
432 
433 /* locked version of __adis_initial_startup() */
adis_initial_startup(struct adis * adis)434 static inline int adis_initial_startup(struct adis *adis)
435 {
436 	int ret;
437 
438 	mutex_lock(&adis->state_lock);
439 	ret = __adis_initial_startup(adis);
440 	mutex_unlock(&adis->state_lock);
441 
442 	return ret;
443 }
444 
445 int adis_single_conversion(struct iio_dev *indio_dev,
446 			   const struct iio_chan_spec *chan,
447 			   unsigned int error_mask, 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,
555 			    unsigned int *readval);
556 
557 #else
558 
559 #define adis_debugfs_reg_access NULL
560 
561 #endif
562 
563 #endif
564