forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/drivers/iio/imu/adis.c
....@@ -1,13 +1,13 @@
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 #include <linux/delay.h>
10
+#include <linux/gpio/consumer.h>
1111 #include <linux/mutex.h>
1212 #include <linux/device.h>
1313 #include <linux/kernel.h>
....@@ -27,7 +27,14 @@
2727 #define ADIS_MSC_CTRL_DATA_RDY_DIO2 BIT(0)
2828 #define ADIS_GLOB_CMD_SW_RESET BIT(7)
2929
30
-int adis_write_reg(struct adis *adis, unsigned int reg,
30
+/**
31
+ * __adis_write_reg() - write N bytes to register (unlocked version)
32
+ * @adis: The adis device
33
+ * @reg: The address of the lower of the two registers
34
+ * @value: The value to write to device (up to 4 bytes)
35
+ * @size: The size of the @value (in bytes)
36
+ */
37
+int __adis_write_reg(struct adis *adis, unsigned int reg,
3138 unsigned int value, unsigned int size)
3239 {
3340 unsigned int page = reg / ADIS_PAGE_SIZE;
....@@ -39,33 +46,42 @@
3946 .bits_per_word = 8,
4047 .len = 2,
4148 .cs_change = 1,
42
- .delay_usecs = adis->data->write_delay,
49
+ .delay.value = adis->data->write_delay,
50
+ .delay.unit = SPI_DELAY_UNIT_USECS,
51
+ .cs_change_delay.value = adis->data->cs_change_delay,
52
+ .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
4353 }, {
4454 .tx_buf = adis->tx + 2,
4555 .bits_per_word = 8,
4656 .len = 2,
4757 .cs_change = 1,
48
- .delay_usecs = adis->data->write_delay,
58
+ .delay.value = adis->data->write_delay,
59
+ .delay.unit = SPI_DELAY_UNIT_USECS,
60
+ .cs_change_delay.value = adis->data->cs_change_delay,
61
+ .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
4962 }, {
5063 .tx_buf = adis->tx + 4,
5164 .bits_per_word = 8,
5265 .len = 2,
5366 .cs_change = 1,
54
- .delay_usecs = adis->data->write_delay,
67
+ .delay.value = adis->data->write_delay,
68
+ .delay.unit = SPI_DELAY_UNIT_USECS,
69
+ .cs_change_delay.value = adis->data->cs_change_delay,
70
+ .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
5571 }, {
5672 .tx_buf = adis->tx + 6,
5773 .bits_per_word = 8,
5874 .len = 2,
59
- .delay_usecs = adis->data->write_delay,
75
+ .delay.value = adis->data->write_delay,
76
+ .delay.unit = SPI_DELAY_UNIT_USECS,
6077 }, {
6178 .tx_buf = adis->tx + 8,
6279 .bits_per_word = 8,
6380 .len = 2,
64
- .delay_usecs = adis->data->write_delay,
81
+ .delay.value = adis->data->write_delay,
82
+ .delay.unit = SPI_DELAY_UNIT_USECS,
6583 },
6684 };
67
-
68
- mutex_lock(&adis->txrx_lock);
6985
7086 spi_message_init(&msg);
7187
....@@ -81,18 +97,17 @@
8197 adis->tx[9] = (value >> 24) & 0xff;
8298 adis->tx[6] = ADIS_WRITE_REG(reg + 2);
8399 adis->tx[7] = (value >> 16) & 0xff;
84
- /* fall through */
100
+ fallthrough;
85101 case 2:
86102 adis->tx[4] = ADIS_WRITE_REG(reg + 1);
87103 adis->tx[5] = (value >> 8) & 0xff;
88
- /* fall through */
104
+ fallthrough;
89105 case 1:
90106 adis->tx[2] = ADIS_WRITE_REG(reg);
91107 adis->tx[3] = value & 0xff;
92108 break;
93109 default:
94
- ret = -EINVAL;
95
- goto out_unlock;
110
+ return -EINVAL;
96111 }
97112
98113 xfers[size].cs_change = 0;
....@@ -108,20 +123,18 @@
108123 adis->current_page = page;
109124 }
110125
111
-out_unlock:
112
- mutex_unlock(&adis->txrx_lock);
113
-
114126 return ret;
115127 }
116
-EXPORT_SYMBOL_GPL(adis_write_reg);
128
+EXPORT_SYMBOL_GPL(__adis_write_reg);
117129
118130 /**
119
- * adis_read_reg() - read 2 bytes from a 16-bit register
131
+ * __adis_read_reg() - read N bytes from register (unlocked version)
120132 * @adis: The adis device
121133 * @reg: The address of the lower of the two registers
122134 * @val: The value read back from the device
135
+ * @size: The size of the @val buffer
123136 */
124
-int adis_read_reg(struct adis *adis, unsigned int reg,
137
+int __adis_read_reg(struct adis *adis, unsigned int reg,
125138 unsigned int *val, unsigned int size)
126139 {
127140 unsigned int page = reg / ADIS_PAGE_SIZE;
....@@ -133,29 +146,38 @@
133146 .bits_per_word = 8,
134147 .len = 2,
135148 .cs_change = 1,
136
- .delay_usecs = adis->data->write_delay,
149
+ .delay.value = adis->data->write_delay,
150
+ .delay.unit = SPI_DELAY_UNIT_USECS,
151
+ .cs_change_delay.value = adis->data->cs_change_delay,
152
+ .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
137153 }, {
138154 .tx_buf = adis->tx + 2,
139155 .bits_per_word = 8,
140156 .len = 2,
141157 .cs_change = 1,
142
- .delay_usecs = adis->data->read_delay,
158
+ .delay.value = adis->data->read_delay,
159
+ .delay.unit = SPI_DELAY_UNIT_USECS,
160
+ .cs_change_delay.value = adis->data->cs_change_delay,
161
+ .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
143162 }, {
144163 .tx_buf = adis->tx + 4,
145164 .rx_buf = adis->rx,
146165 .bits_per_word = 8,
147166 .len = 2,
148167 .cs_change = 1,
149
- .delay_usecs = adis->data->read_delay,
168
+ .delay.value = adis->data->read_delay,
169
+ .delay.unit = SPI_DELAY_UNIT_USECS,
170
+ .cs_change_delay.value = adis->data->cs_change_delay,
171
+ .cs_change_delay.unit = SPI_DELAY_UNIT_USECS,
150172 }, {
151173 .rx_buf = adis->rx + 2,
152174 .bits_per_word = 8,
153175 .len = 2,
154
- .delay_usecs = adis->data->read_delay,
176
+ .delay.value = adis->data->read_delay,
177
+ .delay.unit = SPI_DELAY_UNIT_USECS,
155178 },
156179 };
157180
158
- mutex_lock(&adis->txrx_lock);
159181 spi_message_init(&msg);
160182
161183 if (adis->current_page != page) {
....@@ -169,7 +191,7 @@
169191 adis->tx[2] = ADIS_READ_REG(reg + 2);
170192 adis->tx[3] = 0;
171193 spi_message_add_tail(&xfers[1], &msg);
172
- /* fall through */
194
+ fallthrough;
173195 case 2:
174196 adis->tx[4] = ADIS_READ_REG(reg);
175197 adis->tx[5] = 0;
....@@ -177,15 +199,14 @@
177199 spi_message_add_tail(&xfers[3], &msg);
178200 break;
179201 default:
180
- ret = -EINVAL;
181
- goto out_unlock;
202
+ return -EINVAL;
182203 }
183204
184205 ret = spi_sync(adis->spi, &msg);
185206 if (ret) {
186207 dev_err(&adis->spi->dev, "Failed to read register 0x%02X: %d\n",
187208 reg, ret);
188
- goto out_unlock;
209
+ return ret;
189210 } else {
190211 adis->current_page = page;
191212 }
....@@ -199,12 +220,34 @@
199220 break;
200221 }
201222
202
-out_unlock:
203
- mutex_unlock(&adis->txrx_lock);
204
-
205223 return ret;
206224 }
207
-EXPORT_SYMBOL_GPL(adis_read_reg);
225
+EXPORT_SYMBOL_GPL(__adis_read_reg);
226
+/**
227
+ * __adis_update_bits_base() - ADIS Update bits function - Unlocked version
228
+ * @adis: The adis device
229
+ * @reg: The address of the lower of the two registers
230
+ * @mask: Bitmask to change
231
+ * @val: Value to be written
232
+ * @size: Size of the register to update
233
+ *
234
+ * Updates the desired bits of @reg in accordance with @mask and @val.
235
+ */
236
+int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask,
237
+ const u32 val, u8 size)
238
+{
239
+ int ret;
240
+ u32 __val;
241
+
242
+ ret = __adis_read_reg(adis, reg, &__val, size);
243
+ if (ret)
244
+ return ret;
245
+
246
+ __val = (__val & ~mask) | (val & mask);
247
+
248
+ return __adis_write_reg(adis, reg, __val, size);
249
+}
250
+EXPORT_SYMBOL_GPL(__adis_update_bits_base);
208251
209252 #ifdef CONFIG_DEBUG_FS
210253
....@@ -218,7 +261,8 @@
218261 int ret;
219262
220263 ret = adis_read_reg_16(adis, reg, &val16);
221
- *readval = val16;
264
+ if (ret == 0)
265
+ *readval = val16;
222266
223267 return ret;
224268 } else {
....@@ -241,12 +285,16 @@
241285 int ret = 0;
242286 uint16_t msc;
243287
244
- if (adis->data->enable_irq)
245
- return adis->data->enable_irq(adis, enable);
288
+ mutex_lock(&adis->state_lock);
246289
247
- ret = adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
290
+ if (adis->data->enable_irq) {
291
+ ret = adis->data->enable_irq(adis, enable);
292
+ goto out_unlock;
293
+ }
294
+
295
+ ret = __adis_read_reg_16(adis, adis->data->msc_ctrl_reg, &msc);
248296 if (ret)
249
- goto error_ret;
297
+ goto out_unlock;
250298
251299 msc |= ADIS_MSC_CTRL_DATA_RDY_POL_HIGH;
252300 msc &= ~ADIS_MSC_CTRL_DATA_RDY_DIO2;
....@@ -255,27 +303,28 @@
255303 else
256304 msc &= ~ADIS_MSC_CTRL_DATA_RDY_EN;
257305
258
- ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
306
+ ret = __adis_write_reg_16(adis, adis->data->msc_ctrl_reg, msc);
259307
260
-error_ret:
308
+out_unlock:
309
+ mutex_unlock(&adis->state_lock);
261310 return ret;
262311 }
263312 EXPORT_SYMBOL(adis_enable_irq);
264313
265314 /**
266
- * adis_check_status() - Check the device for error conditions
315
+ * __adis_check_status() - Check the device for error conditions (unlocked)
267316 * @adis: The adis device
268317 *
269318 * Returns 0 on success, a negative error code otherwise
270319 */
271
-int adis_check_status(struct adis *adis)
320
+int __adis_check_status(struct adis *adis)
272321 {
273322 uint16_t status;
274323 int ret;
275324 int i;
276325
277
- ret = adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
278
- if (ret < 0)
326
+ ret = __adis_read_reg_16(adis, adis->data->diag_stat_reg, &status);
327
+ if (ret)
279328 return ret;
280329
281330 status &= adis->data->status_error_mask;
....@@ -292,77 +341,116 @@
292341
293342 return -EIO;
294343 }
295
-EXPORT_SYMBOL_GPL(adis_check_status);
344
+EXPORT_SYMBOL_GPL(__adis_check_status);
296345
297346 /**
298
- * adis_reset() - Reset the device
347
+ * __adis_reset() - Reset the device (unlocked version)
299348 * @adis: The adis device
300349 *
301350 * Returns 0 on success, a negative error code otherwise
302351 */
303
-int adis_reset(struct adis *adis)
352
+int __adis_reset(struct adis *adis)
304353 {
305354 int ret;
355
+ const struct adis_timeout *timeouts = adis->data->timeouts;
306356
307
- ret = adis_write_reg_8(adis, adis->data->glob_cmd_reg,
357
+ ret = __adis_write_reg_8(adis, adis->data->glob_cmd_reg,
308358 ADIS_GLOB_CMD_SW_RESET);
309
- if (ret)
359
+ if (ret) {
310360 dev_err(&adis->spi->dev, "Failed to reset device: %d\n", ret);
361
+ return ret;
362
+ }
311363
312
- return ret;
364
+ msleep(timeouts->sw_reset_ms);
365
+
366
+ return 0;
313367 }
314
-EXPORT_SYMBOL_GPL(adis_reset);
368
+EXPORT_SYMBOL_GPL(__adis_reset);
315369
316370 static int adis_self_test(struct adis *adis)
317371 {
318372 int ret;
373
+ const struct adis_timeout *timeouts = adis->data->timeouts;
319374
320
- ret = adis_write_reg_16(adis, adis->data->msc_ctrl_reg,
321
- adis->data->self_test_mask);
375
+ ret = __adis_write_reg_16(adis, adis->data->self_test_reg,
376
+ adis->data->self_test_mask);
322377 if (ret) {
323378 dev_err(&adis->spi->dev, "Failed to initiate self test: %d\n",
324379 ret);
325380 return ret;
326381 }
327382
328
- msleep(adis->data->startup_delay);
383
+ msleep(timeouts->self_test_ms);
329384
330
- ret = adis_check_status(adis);
385
+ ret = __adis_check_status(adis);
331386
332387 if (adis->data->self_test_no_autoclear)
333
- adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00);
388
+ __adis_write_reg_16(adis, adis->data->self_test_reg, 0x00);
334389
335390 return ret;
336391 }
337392
338393 /**
339
- * adis_inital_startup() - Performs device self-test
394
+ * __adis_initial_startup() - Device initial setup
340395 * @adis: The adis device
396
+ *
397
+ * The function performs a HW reset via a reset pin that should be specified
398
+ * via GPIOLIB. If no pin is configured a SW reset will be performed.
399
+ * The RST pin for the ADIS devices should be configured as ACTIVE_LOW.
400
+ *
401
+ * After the self-test operation is performed, the function will also check
402
+ * that the product ID is as expected. This assumes that drivers providing
403
+ * 'prod_id_reg' will also provide the 'prod_id'.
341404 *
342405 * Returns 0 if the device is operational, a negative error code otherwise.
343406 *
344407 * This function should be called early on in the device initialization sequence
345408 * to ensure that the device is in a sane and known state and that it is usable.
346409 */
347
-int adis_initial_startup(struct adis *adis)
410
+int __adis_initial_startup(struct adis *adis)
348411 {
412
+ const struct adis_timeout *timeouts = adis->data->timeouts;
413
+ struct gpio_desc *gpio;
414
+ uint16_t prod_id;
349415 int ret;
350416
351
- ret = adis_self_test(adis);
352
- if (ret) {
353
- dev_err(&adis->spi->dev, "Self-test failed, trying reset.\n");
354
- adis_reset(adis);
355
- msleep(adis->data->startup_delay);
356
- ret = adis_self_test(adis);
357
- if (ret) {
358
- dev_err(&adis->spi->dev, "Second self-test failed, giving up.\n");
417
+ /* check if the device has rst pin low */
418
+ gpio = devm_gpiod_get_optional(&adis->spi->dev, "reset", GPIOD_OUT_HIGH);
419
+ if (IS_ERR(gpio))
420
+ return PTR_ERR(gpio);
421
+
422
+ if (gpio) {
423
+ msleep(10);
424
+ /* bring device out of reset */
425
+ gpiod_set_value_cansleep(gpio, 0);
426
+ msleep(timeouts->reset_ms);
427
+ } else {
428
+ ret = __adis_reset(adis);
429
+ if (ret)
359430 return ret;
360
- }
361431 }
432
+
433
+ ret = adis_self_test(adis);
434
+ if (ret)
435
+ return ret;
436
+
437
+ adis_enable_irq(adis, false);
438
+
439
+ if (!adis->data->prod_id_reg)
440
+ return 0;
441
+
442
+ ret = adis_read_reg_16(adis, adis->data->prod_id_reg, &prod_id);
443
+ if (ret)
444
+ return ret;
445
+
446
+ if (prod_id != adis->data->prod_id)
447
+ dev_warn(&adis->spi->dev,
448
+ "Device ID(%u) and product ID(%u) do not match.\n",
449
+ adis->data->prod_id, prod_id);
362450
363451 return 0;
364452 }
365
-EXPORT_SYMBOL_GPL(adis_initial_startup);
453
+EXPORT_SYMBOL_GPL(__adis_initial_startup);
366454
367455 /**
368456 * adis_single_conversion() - Performs a single sample conversion
....@@ -386,15 +474,15 @@
386474 unsigned int uval;
387475 int ret;
388476
389
- mutex_lock(&indio_dev->mlock);
477
+ mutex_lock(&adis->state_lock);
390478
391
- ret = adis_read_reg(adis, chan->address, &uval,
479
+ ret = __adis_read_reg(adis, chan->address, &uval,
392480 chan->scan_type.storagebits / 8);
393481 if (ret)
394482 goto err_unlock;
395483
396484 if (uval & error_mask) {
397
- ret = adis_check_status(adis);
485
+ ret = __adis_check_status(adis);
398486 if (ret)
399487 goto err_unlock;
400488 }
....@@ -406,7 +494,7 @@
406494
407495 ret = IIO_VAL_INT;
408496 err_unlock:
409
- mutex_unlock(&indio_dev->mlock);
497
+ mutex_unlock(&adis->state_lock);
410498 return ret;
411499 }
412500 EXPORT_SYMBOL_GPL(adis_single_conversion);
....@@ -426,7 +514,12 @@
426514 int adis_init(struct adis *adis, struct iio_dev *indio_dev,
427515 struct spi_device *spi, const struct adis_data *data)
428516 {
429
- mutex_init(&adis->txrx_lock);
517
+ if (!data || !data->timeouts) {
518
+ dev_err(&spi->dev, "No config data or timeouts not defined!\n");
519
+ return -EINVAL;
520
+ }
521
+
522
+ mutex_init(&adis->state_lock);
430523 adis->spi = spi;
431524 adis->data = data;
432525 iio_device_set_drvdata(indio_dev, adis);
....@@ -439,7 +532,7 @@
439532 adis->current_page = 0;
440533 }
441534
442
- return adis_enable_irq(adis, false);
535
+ return 0;
443536 }
444537 EXPORT_SYMBOL_GPL(adis_init);
445538