forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
kernel/drivers/iio/frequency/adf4350.c
....@@ -1,9 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * ADF4350/ADF4351 SPI Wideband Synthesizer driver
34 *
45 * Copyright 2012-2013 Analog Devices Inc.
5
- *
6
- * Licensed under the GPL-2.
76 */
87
98 #include <linux/device.h>
....@@ -15,11 +14,10 @@
1514 #include <linux/err.h>
1615 #include <linux/module.h>
1716 #include <linux/gcd.h>
18
-#include <linux/gpio.h>
17
+#include <linux/gpio/consumer.h>
1918 #include <asm/div64.h>
2019 #include <linux/clk.h>
2120 #include <linux/of.h>
22
-#include <linux/of_gpio.h>
2321
2422 #include <linux/iio/iio.h>
2523 #include <linux/iio/sysfs.h>
....@@ -35,6 +33,7 @@
3533 struct adf4350_state {
3634 struct spi_device *spi;
3735 struct regulator *reg;
36
+ struct gpio_desc *lock_detect_gpiod;
3837 struct adf4350_platform_data *pdata;
3938 struct clk *clk;
4039 unsigned long clkin;
....@@ -49,6 +48,13 @@
4948 unsigned long regs_hw[6];
5049 unsigned long long freq_req;
5150 /*
51
+ * Lock to protect the state of the device from potential concurrent
52
+ * writes. The device is configured via a sequence of SPI writes,
53
+ * and this lock is meant to prevent the start of another sequence
54
+ * before another one has finished.
55
+ */
56
+ struct mutex lock;
57
+ /*
5258 * DMA (thus cache coherency maintenance) requires the
5359 * transfer buffers to live in their own cache lines.
5460 */
....@@ -62,7 +68,6 @@
6268 .r3_user_settings = ADF4350_REG3_12BIT_CLKDIV_MODE(0),
6369 .r4_user_settings = ADF4350_REG4_OUTPUT_PWR(3) |
6470 ADF4350_REG4_MUTE_TILL_LOCK_EN,
65
- .gpio_lock_detect = -1,
6671 };
6772
6873 static int adf4350_sync_config(struct adf4350_state *st)
....@@ -101,7 +106,7 @@
101106 if (reg > ADF4350_REG5)
102107 return -EINVAL;
103108
104
- mutex_lock(&indio_dev->mlock);
109
+ mutex_lock(&st->lock);
105110 if (readval == NULL) {
106111 st->regs[reg] = writeval & ~(BIT(0) | BIT(1) | BIT(2));
107112 ret = adf4350_sync_config(st);
....@@ -109,7 +114,7 @@
109114 *readval = st->regs_hw[reg];
110115 ret = 0;
111116 }
112
- mutex_unlock(&indio_dev->mlock);
117
+ mutex_unlock(&st->lock);
113118
114119 return ret;
115120 }
....@@ -256,7 +261,7 @@
256261 if (ret)
257262 return ret;
258263
259
- mutex_lock(&indio_dev->mlock);
264
+ mutex_lock(&st->lock);
260265 switch ((u32)private) {
261266 case ADF4350_FREQ:
262267 ret = adf4350_set_freq(st, readin);
....@@ -297,7 +302,7 @@
297302 default:
298303 ret = -EINVAL;
299304 }
300
- mutex_unlock(&indio_dev->mlock);
305
+ mutex_unlock(&st->lock);
301306
302307 return ret ? ret : len;
303308 }
....@@ -311,15 +316,15 @@
311316 unsigned long long val;
312317 int ret = 0;
313318
314
- mutex_lock(&indio_dev->mlock);
319
+ mutex_lock(&st->lock);
315320 switch ((u32)private) {
316321 case ADF4350_FREQ:
317322 val = (u64)((st->r0_int * st->r1_mod) + st->r0_fract) *
318323 (u64)st->fpfd;
319324 do_div(val, st->r1_mod * (1 << st->r4_rf_div_sel));
320325 /* PLL unlocked? return error */
321
- if (gpio_is_valid(st->pdata->gpio_lock_detect))
322
- if (!gpio_get_value(st->pdata->gpio_lock_detect)) {
326
+ if (st->lock_detect_gpiod)
327
+ if (!gpiod_get_value(st->lock_detect_gpiod)) {
323328 dev_dbg(&st->spi->dev, "PLL un-locked\n");
324329 ret = -EBUSY;
325330 }
....@@ -340,7 +345,7 @@
340345 ret = -EINVAL;
341346 val = 0;
342347 }
343
- mutex_unlock(&indio_dev->mlock);
348
+ mutex_unlock(&st->lock);
344349
345350 return ret < 0 ? ret : sprintf(buf, "%llu\n", val);
346351 }
....@@ -382,13 +387,12 @@
382387 struct device_node *np = dev->of_node;
383388 struct adf4350_platform_data *pdata;
384389 unsigned int tmp;
385
- int ret;
386390
387391 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
388392 if (!pdata)
389393 return NULL;
390394
391
- strncpy(&pdata->name[0], np->name, SPI_NAME_SIZE - 1);
395
+ snprintf(&pdata->name[0], SPI_NAME_SIZE - 1, "%pOFn", np);
392396
393397 tmp = 10000;
394398 of_property_read_u32(np, "adi,channel-spacing", &tmp);
....@@ -401,12 +405,6 @@
401405 tmp = 0;
402406 of_property_read_u32(np, "adi,reference-div-factor", &tmp);
403407 pdata->ref_div_factor = tmp;
404
-
405
- ret = of_get_gpio(np, 0);
406
- if (ret < 0)
407
- pdata->gpio_lock_detect = -1;
408
- else
409
- pdata->gpio_lock_detect = ret;
410408
411409 pdata->ref_doubler_en = of_property_read_bool(np,
412410 "adi,reference-doubler-enable");
....@@ -540,7 +538,6 @@
540538 st->spi = spi;
541539 st->pdata = pdata;
542540
543
- indio_dev->dev.parent = &spi->dev;
544541 indio_dev->name = (pdata->name[0] != 0) ? pdata->name :
545542 spi_get_device_id(spi)->name;
546543
....@@ -548,6 +545,8 @@
548545 indio_dev->modes = INDIO_DIRECT_MODE;
549546 indio_dev->channels = &adf4350_chan;
550547 indio_dev->num_channels = 1;
548
+
549
+ mutex_init(&st->lock);
551550
552551 st->chspc = pdata->channel_spacing;
553552 if (clk) {
....@@ -562,15 +561,11 @@
562561
563562 memset(st->regs_hw, 0xFF, sizeof(st->regs_hw));
564563
565
- if (gpio_is_valid(pdata->gpio_lock_detect)) {
566
- ret = devm_gpio_request(&spi->dev, pdata->gpio_lock_detect,
567
- indio_dev->name);
568
- if (ret) {
569
- dev_err(&spi->dev, "fail to request lock detect GPIO-%d",
570
- pdata->gpio_lock_detect);
571
- goto error_disable_reg;
572
- }
573
- gpio_direction_input(pdata->gpio_lock_detect);
564
+ st->lock_detect_gpiod = devm_gpiod_get_optional(&spi->dev, NULL,
565
+ GPIOD_IN);
566
+ if (IS_ERR(st->lock_detect_gpiod)) {
567
+ ret = PTR_ERR(st->lock_detect_gpiod);
568
+ goto error_disable_reg;
574569 }
575570
576571 if (pdata->power_up_frequency) {