.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * ADF4350/ADF4351 SPI Wideband Synthesizer driver |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright 2012-2013 Analog Devices Inc. |
---|
5 | | - * |
---|
6 | | - * Licensed under the GPL-2. |
---|
7 | 6 | */ |
---|
8 | 7 | |
---|
9 | 8 | #include <linux/device.h> |
---|
.. | .. |
---|
15 | 14 | #include <linux/err.h> |
---|
16 | 15 | #include <linux/module.h> |
---|
17 | 16 | #include <linux/gcd.h> |
---|
18 | | -#include <linux/gpio.h> |
---|
| 17 | +#include <linux/gpio/consumer.h> |
---|
19 | 18 | #include <asm/div64.h> |
---|
20 | 19 | #include <linux/clk.h> |
---|
21 | 20 | #include <linux/of.h> |
---|
22 | | -#include <linux/of_gpio.h> |
---|
23 | 21 | |
---|
24 | 22 | #include <linux/iio/iio.h> |
---|
25 | 23 | #include <linux/iio/sysfs.h> |
---|
.. | .. |
---|
35 | 33 | struct adf4350_state { |
---|
36 | 34 | struct spi_device *spi; |
---|
37 | 35 | struct regulator *reg; |
---|
| 36 | + struct gpio_desc *lock_detect_gpiod; |
---|
38 | 37 | struct adf4350_platform_data *pdata; |
---|
39 | 38 | struct clk *clk; |
---|
40 | 39 | unsigned long clkin; |
---|
.. | .. |
---|
49 | 48 | unsigned long regs_hw[6]; |
---|
50 | 49 | unsigned long long freq_req; |
---|
51 | 50 | /* |
---|
| 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 | + /* |
---|
52 | 58 | * DMA (thus cache coherency maintenance) requires the |
---|
53 | 59 | * transfer buffers to live in their own cache lines. |
---|
54 | 60 | */ |
---|
.. | .. |
---|
62 | 68 | .r3_user_settings = ADF4350_REG3_12BIT_CLKDIV_MODE(0), |
---|
63 | 69 | .r4_user_settings = ADF4350_REG4_OUTPUT_PWR(3) | |
---|
64 | 70 | ADF4350_REG4_MUTE_TILL_LOCK_EN, |
---|
65 | | - .gpio_lock_detect = -1, |
---|
66 | 71 | }; |
---|
67 | 72 | |
---|
68 | 73 | static int adf4350_sync_config(struct adf4350_state *st) |
---|
.. | .. |
---|
101 | 106 | if (reg > ADF4350_REG5) |
---|
102 | 107 | return -EINVAL; |
---|
103 | 108 | |
---|
104 | | - mutex_lock(&indio_dev->mlock); |
---|
| 109 | + mutex_lock(&st->lock); |
---|
105 | 110 | if (readval == NULL) { |
---|
106 | 111 | st->regs[reg] = writeval & ~(BIT(0) | BIT(1) | BIT(2)); |
---|
107 | 112 | ret = adf4350_sync_config(st); |
---|
.. | .. |
---|
109 | 114 | *readval = st->regs_hw[reg]; |
---|
110 | 115 | ret = 0; |
---|
111 | 116 | } |
---|
112 | | - mutex_unlock(&indio_dev->mlock); |
---|
| 117 | + mutex_unlock(&st->lock); |
---|
113 | 118 | |
---|
114 | 119 | return ret; |
---|
115 | 120 | } |
---|
.. | .. |
---|
256 | 261 | if (ret) |
---|
257 | 262 | return ret; |
---|
258 | 263 | |
---|
259 | | - mutex_lock(&indio_dev->mlock); |
---|
| 264 | + mutex_lock(&st->lock); |
---|
260 | 265 | switch ((u32)private) { |
---|
261 | 266 | case ADF4350_FREQ: |
---|
262 | 267 | ret = adf4350_set_freq(st, readin); |
---|
.. | .. |
---|
297 | 302 | default: |
---|
298 | 303 | ret = -EINVAL; |
---|
299 | 304 | } |
---|
300 | | - mutex_unlock(&indio_dev->mlock); |
---|
| 305 | + mutex_unlock(&st->lock); |
---|
301 | 306 | |
---|
302 | 307 | return ret ? ret : len; |
---|
303 | 308 | } |
---|
.. | .. |
---|
311 | 316 | unsigned long long val; |
---|
312 | 317 | int ret = 0; |
---|
313 | 318 | |
---|
314 | | - mutex_lock(&indio_dev->mlock); |
---|
| 319 | + mutex_lock(&st->lock); |
---|
315 | 320 | switch ((u32)private) { |
---|
316 | 321 | case ADF4350_FREQ: |
---|
317 | 322 | val = (u64)((st->r0_int * st->r1_mod) + st->r0_fract) * |
---|
318 | 323 | (u64)st->fpfd; |
---|
319 | 324 | do_div(val, st->r1_mod * (1 << st->r4_rf_div_sel)); |
---|
320 | 325 | /* 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)) { |
---|
323 | 328 | dev_dbg(&st->spi->dev, "PLL un-locked\n"); |
---|
324 | 329 | ret = -EBUSY; |
---|
325 | 330 | } |
---|
.. | .. |
---|
340 | 345 | ret = -EINVAL; |
---|
341 | 346 | val = 0; |
---|
342 | 347 | } |
---|
343 | | - mutex_unlock(&indio_dev->mlock); |
---|
| 348 | + mutex_unlock(&st->lock); |
---|
344 | 349 | |
---|
345 | 350 | return ret < 0 ? ret : sprintf(buf, "%llu\n", val); |
---|
346 | 351 | } |
---|
.. | .. |
---|
382 | 387 | struct device_node *np = dev->of_node; |
---|
383 | 388 | struct adf4350_platform_data *pdata; |
---|
384 | 389 | unsigned int tmp; |
---|
385 | | - int ret; |
---|
386 | 390 | |
---|
387 | 391 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
---|
388 | 392 | if (!pdata) |
---|
389 | 393 | return NULL; |
---|
390 | 394 | |
---|
391 | | - strncpy(&pdata->name[0], np->name, SPI_NAME_SIZE - 1); |
---|
| 395 | + snprintf(&pdata->name[0], SPI_NAME_SIZE - 1, "%pOFn", np); |
---|
392 | 396 | |
---|
393 | 397 | tmp = 10000; |
---|
394 | 398 | of_property_read_u32(np, "adi,channel-spacing", &tmp); |
---|
.. | .. |
---|
401 | 405 | tmp = 0; |
---|
402 | 406 | of_property_read_u32(np, "adi,reference-div-factor", &tmp); |
---|
403 | 407 | 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; |
---|
410 | 408 | |
---|
411 | 409 | pdata->ref_doubler_en = of_property_read_bool(np, |
---|
412 | 410 | "adi,reference-doubler-enable"); |
---|
.. | .. |
---|
540 | 538 | st->spi = spi; |
---|
541 | 539 | st->pdata = pdata; |
---|
542 | 540 | |
---|
543 | | - indio_dev->dev.parent = &spi->dev; |
---|
544 | 541 | indio_dev->name = (pdata->name[0] != 0) ? pdata->name : |
---|
545 | 542 | spi_get_device_id(spi)->name; |
---|
546 | 543 | |
---|
.. | .. |
---|
548 | 545 | indio_dev->modes = INDIO_DIRECT_MODE; |
---|
549 | 546 | indio_dev->channels = &adf4350_chan; |
---|
550 | 547 | indio_dev->num_channels = 1; |
---|
| 548 | + |
---|
| 549 | + mutex_init(&st->lock); |
---|
551 | 550 | |
---|
552 | 551 | st->chspc = pdata->channel_spacing; |
---|
553 | 552 | if (clk) { |
---|
.. | .. |
---|
562 | 561 | |
---|
563 | 562 | memset(st->regs_hw, 0xFF, sizeof(st->regs_hw)); |
---|
564 | 563 | |
---|
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; |
---|
574 | 569 | } |
---|
575 | 570 | |
---|
576 | 571 | if (pdata->power_up_frequency) { |
---|