hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/include/linux/iio/imu/adis.h
....@@ -1,10 +1,9 @@
1
+/* SPDX-License-Identifier: GPL-2.0-or-later */
12 /*
23 * Common library for ADIS16XXX devices
34 *
45 * Copyright 2012 Analog Devices Inc.
56 * Author: Lars-Peter Clausen <lars@metafoo.de>
6
- *
7
- * Licensed under the GPL-2 or later.
87 */
98
109 #ifndef __IIO_ADIS_H__
....@@ -23,59 +22,254 @@
2322 struct adis;
2423
2524 /**
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
+/**
2637 * struct adis_data - ADIS chip variant specific data
2738 * @read_delay: SPI delay for read operations in us
2839 * @write_delay: SPI delay for write operations in us
40
+ * @cs_change_delay: SPI delay between CS changes in us
2941 * @glob_cmd_reg: Register address of the GLOB_CMD register
3042 * @msc_ctrl_reg: Register address of the MSC_CTRL register
3143 * @diag_stat_reg: Register address of the DIAG_STAT register
32
- * @status_error_msgs: Array of error messgaes
33
- * @status_error_mask:
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
3460 */
3561 struct adis_data {
3662 unsigned int read_delay;
3763 unsigned int write_delay;
64
+ unsigned int cs_change_delay;
3865
3966 unsigned int glob_cmd_reg;
4067 unsigned int msc_ctrl_reg;
4168 unsigned int diag_stat_reg;
69
+ unsigned int prod_id_reg;
70
+
71
+ unsigned int prod_id;
4272
4373 unsigned int self_test_mask;
74
+ unsigned int self_test_reg;
4475 bool self_test_no_autoclear;
45
- unsigned int startup_delay;
76
+ const struct adis_timeout *timeouts;
4677
4778 const char * const *status_error_msgs;
4879 unsigned int status_error_mask;
4980
5081 int (*enable_irq)(struct adis *adis, bool enable);
82
+ bool unmasked_drdy;
5183
5284 bool has_paging;
85
+
86
+ unsigned int burst_reg_cmd;
87
+ unsigned int burst_len;
88
+ unsigned int burst_max_len;
5389 };
5490
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
+ */
55108 struct adis {
56109 struct spi_device *spi;
57110 struct iio_trigger *trig;
58111
59112 const struct adis_data *data;
60
-
61
- struct mutex txrx_lock;
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;
62126 struct spi_message msg;
63127 struct spi_transfer *xfer;
64128 unsigned int current_page;
129
+ unsigned long irq_flag;
65130 void *buffer;
66131
67
- uint8_t tx[10] ____cacheline_aligned;
68
- uint8_t rx[4];
132
+ u8 tx[10] ____cacheline_aligned;
133
+ u8 rx[4];
69134 };
70135
71136 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
72
- struct spi_device *spi, const struct adis_data *data);
73
-int adis_reset(struct adis *adis);
137
+ struct spi_device *spi, const struct adis_data *data);
138
+int __adis_reset(struct adis *adis);
74139
75
-int adis_write_reg(struct adis *adis, unsigned int reg,
76
- unsigned int val, unsigned int size);
77
-int adis_read_reg(struct adis *adis, unsigned int reg,
78
- unsigned int *val, unsigned int size);
140
+/**
141
+ * adis_reset() - Reset the device
142
+ * @adis: The adis device
143
+ *
144
+ * Returns 0 on success, a negative error code otherwise
145
+ */
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
+ */
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
+ */
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
+ */
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
+ */
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
+ */
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
+ */
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
+ */
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
+}
79273
80274 /**
81275 * adis_write_reg_8() - Write single byte to a register
....@@ -84,7 +278,7 @@
84278 * @value: The value to write
85279 */
86280 static inline int adis_write_reg_8(struct adis *adis, unsigned int reg,
87
- uint8_t val)
281
+ u8 val)
88282 {
89283 return adis_write_reg(adis, reg, val, 1);
90284 }
....@@ -96,7 +290,7 @@
96290 * @value: Value to be written
97291 */
98292 static inline int adis_write_reg_16(struct adis *adis, unsigned int reg,
99
- uint16_t val)
293
+ u16 val)
100294 {
101295 return adis_write_reg(adis, reg, val, 2);
102296 }
....@@ -108,7 +302,7 @@
108302 * @value: Value to be written
109303 */
110304 static inline int adis_write_reg_32(struct adis *adis, unsigned int reg,
111
- uint32_t val)
305
+ u32 val)
112306 {
113307 return adis_write_reg(adis, reg, val, 4);
114308 }
....@@ -120,13 +314,14 @@
120314 * @val: The value read back from the device
121315 */
122316 static inline int adis_read_reg_16(struct adis *adis, unsigned int reg,
123
- uint16_t *val)
317
+ u16 *val)
124318 {
125319 unsigned int tmp;
126320 int ret;
127321
128322 ret = adis_read_reg(adis, reg, &tmp, 2);
129
- *val = tmp;
323
+ if (ret == 0)
324
+ *val = tmp;
130325
131326 return ret;
132327 }
....@@ -138,25 +333,118 @@
138333 * @val: The value read back from the device
139334 */
140335 static inline int adis_read_reg_32(struct adis *adis, unsigned int reg,
141
- uint32_t *val)
336
+ u32 *val)
142337 {
143338 unsigned int tmp;
144339 int ret;
145340
146341 ret = adis_read_reg(adis, reg, &tmp, 4);
147
- *val = tmp;
342
+ if (ret == 0)
343
+ *val = tmp;
148344
149345 return ret;
150346 }
151347
152
-int adis_enable_irq(struct adis *adis, bool enable);
153
-int adis_check_status(struct adis *adis);
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
+ */
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;
154364
155
-int adis_initial_startup(struct adis *adis);
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
+
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
+
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() */
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
+}
156444
157445 int adis_single_conversion(struct iio_dev *indio_dev,
158
- const struct iio_chan_spec *chan, unsigned int error_mask,
159
- int *val);
446
+ const struct iio_chan_spec *chan,
447
+ unsigned int error_mask, int *val);
160448
161449 #define ADIS_VOLTAGE_CHAN(addr, si, chan, name, info_all, bits) { \
162450 .type = IIO_VOLTAGE, \
....@@ -205,7 +493,7 @@
205493 .modified = 1, \
206494 .channel2 = IIO_MOD_ ## mod, \
207495 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
208
- info_sep, \
496
+ (info_sep), \
209497 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
210498 .info_mask_shared_by_all = info_all, \
211499 .address = (addr), \
....@@ -232,38 +520,28 @@
232520
233521 #ifdef CONFIG_IIO_ADIS_LIB_BUFFER
234522
235
-int adis_setup_buffer_and_trigger(struct adis *adis,
236
- struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *));
237
-void adis_cleanup_buffer_and_trigger(struct adis *adis,
238
- struct iio_dev *indio_dev);
523
+int
524
+devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
525
+ irq_handler_t trigger_handler);
239526
240
-int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
241
-void adis_remove_trigger(struct adis *adis);
527
+int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev);
242528
243529 int adis_update_scan_mode(struct iio_dev *indio_dev,
244
- const unsigned long *scan_mask);
530
+ const unsigned long *scan_mask);
245531
246532 #else /* CONFIG_IIO_BUFFER */
247533
248
-static inline int adis_setup_buffer_and_trigger(struct adis *adis,
249
- struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *))
534
+static inline int
535
+devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev,
536
+ irq_handler_t trigger_handler)
250537 {
251538 return 0;
252539 }
253540
254
-static inline void adis_cleanup_buffer_and_trigger(struct adis *adis,
255
- struct iio_dev *indio_dev)
256
-{
257
-}
258
-
259
-static inline int adis_probe_trigger(struct adis *adis,
260
- struct iio_dev *indio_dev)
541
+static inline int devm_adis_probe_trigger(struct adis *adis,
542
+ struct iio_dev *indio_dev)
261543 {
262544 return 0;
263
-}
264
-
265
-static inline void adis_remove_trigger(struct adis *adis)
266
-{
267545 }
268546
269547 #define adis_update_scan_mode NULL
....@@ -273,7 +551,8 @@
273551 #ifdef CONFIG_DEBUG_FS
274552
275553 int adis_debugfs_reg_access(struct iio_dev *indio_dev,
276
- unsigned int reg, unsigned int writeval, unsigned int *readval);
554
+ unsigned int reg, unsigned int writeval,
555
+ unsigned int *readval);
277556
278557 #else
279558