--- zzzz-none-000/linux-3.10.107/drivers/iio/frequency/adf4350.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/iio/frequency/adf4350.c 2021-02-04 17:41:59.000000000 +0000 @@ -1,7 +1,7 @@ /* * ADF4350/ADF4351 SPI Wideband Synthesizer driver * - * Copyright 2012 Analog Devices Inc. + * Copyright 2012-2013 Analog Devices Inc. * * Licensed under the GPL-2. */ @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include #include @@ -33,6 +36,7 @@ struct spi_device *spi; struct regulator *reg; struct adf4350_platform_data *pdata; + struct clk *clk; unsigned long clkin; unsigned long chspc; /* Channel Spacing */ unsigned long fpfd; /* Phase Frequency Detector */ @@ -43,7 +47,7 @@ unsigned r4_rf_div_sel; unsigned long regs[6]; unsigned long regs_hw[6]; - + unsigned long long freq_req; /* * DMA (thus cache coherency maintenance) requires the * transfer buffers to live in their own cache lines. @@ -52,7 +56,6 @@ }; static struct adf4350_platform_data default_pdata = { - .clkin = 122880000, .channel_spacing = 10000, .r2_user_settings = ADF4350_REG2_PD_POLARITY_POS | ADF4350_REG2_CHARGE_PUMP_CURR_uA(2500), @@ -69,7 +72,6 @@ for (i = ADF4350_REG5; i >= ADF4350_REG0; i--) { if ((st->regs_hw[i] != st->regs[i]) || ((i == ADF4350_REG0) && doublebuf)) { - switch (i) { case ADF4350_REG1: case ADF4350_REG4: @@ -235,6 +237,7 @@ ADF4350_REG4_MUTE_TILL_LOCK_EN)); st->regs[ADF4350_REG5] = ADF4350_REG5_LD_PIN_MODE_DIGITAL; + st->freq_req = freq; return adf4350_sync_config(st); } @@ -246,6 +249,7 @@ { struct adf4350_state *st = iio_priv(indio_dev); unsigned long long readin; + unsigned long tmp; int ret; ret = kstrtoull(buf, 10, &readin); @@ -258,10 +262,23 @@ ret = adf4350_set_freq(st, readin); break; case ADF4350_FREQ_REFIN: - if (readin > ADF4350_MAX_FREQ_REFIN) + if (readin > ADF4350_MAX_FREQ_REFIN) { ret = -EINVAL; - else - st->clkin = readin; + break; + } + + if (st->clk) { + tmp = clk_round_rate(st->clk, readin); + if (tmp != readin) { + ret = -EINVAL; + break; + } + ret = clk_set_rate(st->clk, tmp); + if (ret < 0) + break; + } + st->clkin = readin; + ret = adf4350_set_freq(st, st->freq_req); break; case ADF4350_FREQ_RESOLUTION: if (readin == 0) @@ -308,6 +325,9 @@ } break; case ADF4350_FREQ_REFIN: + if (st->clk) + st->clkin = clk_get_rate(st->clk); + val = st->clkin; break; case ADF4350_FREQ_RESOLUTION: @@ -318,6 +338,7 @@ break; default: ret = -EINVAL; + val = 0; } mutex_unlock(&indio_dev->mlock); @@ -329,6 +350,7 @@ .read = adf4350_read, \ .write = adf4350_write, \ .private = _ident, \ + .shared = IIO_SEPARATE, \ } static const struct iio_chan_spec_ext_info adf4350_ext_info[] = { @@ -355,30 +377,164 @@ .driver_module = THIS_MODULE, }; +#ifdef CONFIG_OF +static struct adf4350_platform_data *adf4350_parse_dt(struct device *dev) +{ + struct device_node *np = dev->of_node; + struct adf4350_platform_data *pdata; + unsigned int tmp; + int ret; + + pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); + if (!pdata) + return NULL; + + strncpy(&pdata->name[0], np->name, SPI_NAME_SIZE - 1); + + tmp = 10000; + of_property_read_u32(np, "adi,channel-spacing", &tmp); + pdata->channel_spacing = tmp; + + tmp = 0; + of_property_read_u32(np, "adi,power-up-frequency", &tmp); + pdata->power_up_frequency = tmp; + + tmp = 0; + of_property_read_u32(np, "adi,reference-div-factor", &tmp); + pdata->ref_div_factor = tmp; + + ret = of_get_gpio(np, 0); + if (ret < 0) + pdata->gpio_lock_detect = -1; + else + pdata->gpio_lock_detect = ret; + + pdata->ref_doubler_en = of_property_read_bool(np, + "adi,reference-doubler-enable"); + pdata->ref_div2_en = of_property_read_bool(np, + "adi,reference-div2-enable"); + + /* r2_user_settings */ + pdata->r2_user_settings = of_property_read_bool(np, + "adi,phase-detector-polarity-positive-enable") ? + ADF4350_REG2_PD_POLARITY_POS : 0; + pdata->r2_user_settings |= of_property_read_bool(np, + "adi,lock-detect-precision-6ns-enable") ? + ADF4350_REG2_LDP_6ns : 0; + pdata->r2_user_settings |= of_property_read_bool(np, + "adi,lock-detect-function-integer-n-enable") ? + ADF4350_REG2_LDF_INT_N : 0; + + tmp = 2500; + of_property_read_u32(np, "adi,charge-pump-current", &tmp); + pdata->r2_user_settings |= ADF4350_REG2_CHARGE_PUMP_CURR_uA(tmp); + + tmp = 0; + of_property_read_u32(np, "adi,muxout-select", &tmp); + pdata->r2_user_settings |= ADF4350_REG2_MUXOUT(tmp); + + pdata->r2_user_settings |= of_property_read_bool(np, + "adi,low-spur-mode-enable") ? + ADF4350_REG2_NOISE_MODE(0x3) : 0; + + /* r3_user_settings */ + + pdata->r3_user_settings = of_property_read_bool(np, + "adi,cycle-slip-reduction-enable") ? + ADF4350_REG3_12BIT_CSR_EN : 0; + pdata->r3_user_settings |= of_property_read_bool(np, + "adi,charge-cancellation-enable") ? + ADF4351_REG3_CHARGE_CANCELLATION_EN : 0; + + pdata->r3_user_settings |= of_property_read_bool(np, + "adi,anti-backlash-3ns-enable") ? + ADF4351_REG3_ANTI_BACKLASH_3ns_EN : 0; + pdata->r3_user_settings |= of_property_read_bool(np, + "adi,band-select-clock-mode-high-enable") ? + ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH : 0; + + tmp = 0; + of_property_read_u32(np, "adi,12bit-clk-divider", &tmp); + pdata->r3_user_settings |= ADF4350_REG3_12BIT_CLKDIV(tmp); + + tmp = 0; + of_property_read_u32(np, "adi,clk-divider-mode", &tmp); + pdata->r3_user_settings |= ADF4350_REG3_12BIT_CLKDIV_MODE(tmp); + + /* r4_user_settings */ + + pdata->r4_user_settings = of_property_read_bool(np, + "adi,aux-output-enable") ? + ADF4350_REG4_AUX_OUTPUT_EN : 0; + pdata->r4_user_settings |= of_property_read_bool(np, + "adi,aux-output-fundamental-enable") ? + ADF4350_REG4_AUX_OUTPUT_FUND : 0; + pdata->r4_user_settings |= of_property_read_bool(np, + "adi,mute-till-lock-enable") ? + ADF4350_REG4_MUTE_TILL_LOCK_EN : 0; + + tmp = 0; + of_property_read_u32(np, "adi,output-power", &tmp); + pdata->r4_user_settings |= ADF4350_REG4_OUTPUT_PWR(tmp); + + tmp = 0; + of_property_read_u32(np, "adi,aux-output-power", &tmp); + pdata->r4_user_settings |= ADF4350_REG4_AUX_OUTPUT_PWR(tmp); + + return pdata; +} +#else +static +struct adf4350_platform_data *adf4350_parse_dt(struct device *dev) +{ + return NULL; +} +#endif + static int adf4350_probe(struct spi_device *spi) { - struct adf4350_platform_data *pdata = spi->dev.platform_data; + struct adf4350_platform_data *pdata; struct iio_dev *indio_dev; struct adf4350_state *st; + struct clk *clk = NULL; int ret; + if (spi->dev.of_node) { + pdata = adf4350_parse_dt(&spi->dev); + if (pdata == NULL) + return -EINVAL; + } else { + pdata = spi->dev.platform_data; + } + if (!pdata) { dev_warn(&spi->dev, "no platform data? using default\n"); - pdata = &default_pdata; } - indio_dev = iio_device_alloc(sizeof(*st)); - if (indio_dev == NULL) - return -ENOMEM; + if (!pdata->clkin) { + clk = devm_clk_get(&spi->dev, "clkin"); + if (IS_ERR(clk)) + return -EPROBE_DEFER; + + ret = clk_prepare_enable(clk); + if (ret < 0) + return ret; + } + + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); + if (indio_dev == NULL) { + ret = -ENOMEM; + goto error_disable_clk; + } st = iio_priv(indio_dev); - st->reg = regulator_get(&spi->dev, "vcc"); + st->reg = devm_regulator_get(&spi->dev, "vcc"); if (!IS_ERR(st->reg)) { ret = regulator_enable(st->reg); if (ret) - goto error_put_reg; + goto error_disable_clk; } spi_set_drvdata(spi, indio_dev); @@ -395,7 +551,12 @@ indio_dev->num_channels = 1; st->chspc = pdata->channel_spacing; - st->clkin = pdata->clkin; + if (clk) { + st->clk = clk; + st->clkin = clk_get_rate(clk); + } else { + st->clkin = pdata->clkin; + } st->min_out_freq = spi_get_device_id(spi)->driver_data == 4351 ? ADF4351_MIN_OUT_FREQ : ADF4350_MIN_OUT_FREQ; @@ -403,7 +564,8 @@ memset(st->regs_hw, 0xFF, sizeof(st->regs_hw)); if (gpio_is_valid(pdata->gpio_lock_detect)) { - ret = gpio_request(pdata->gpio_lock_detect, indio_dev->name); + ret = devm_gpio_request(&spi->dev, pdata->gpio_lock_detect, + indio_dev->name); if (ret) { dev_err(&spi->dev, "fail to request lock detect GPIO-%d", pdata->gpio_lock_detect); @@ -415,27 +577,21 @@ if (pdata->power_up_frequency) { ret = adf4350_set_freq(st, pdata->power_up_frequency); if (ret) - goto error_free_gpio; + goto error_disable_reg; } ret = iio_device_register(indio_dev); if (ret) - goto error_free_gpio; + goto error_disable_reg; return 0; -error_free_gpio: - if (gpio_is_valid(pdata->gpio_lock_detect)) - gpio_free(pdata->gpio_lock_detect); - error_disable_reg: if (!IS_ERR(st->reg)) regulator_disable(st->reg); -error_put_reg: - if (!IS_ERR(st->reg)) - regulator_put(st->reg); - - iio_device_free(indio_dev); +error_disable_clk: + if (clk) + clk_disable_unprepare(clk); return ret; } @@ -451,29 +607,33 @@ iio_device_unregister(indio_dev); - if (!IS_ERR(reg)) { - regulator_disable(reg); - regulator_put(reg); - } + if (st->clk) + clk_disable_unprepare(st->clk); - if (gpio_is_valid(st->pdata->gpio_lock_detect)) - gpio_free(st->pdata->gpio_lock_detect); - - iio_device_free(indio_dev); + if (!IS_ERR(reg)) + regulator_disable(reg); return 0; } +static const struct of_device_id adf4350_of_match[] = { + { .compatible = "adi,adf4350", }, + { .compatible = "adi,adf4351", }, + { /* sentinel */ }, +}; +MODULE_DEVICE_TABLE(of, adf4350_of_match); + static const struct spi_device_id adf4350_id[] = { {"adf4350", 4350}, {"adf4351", 4351}, {} }; +MODULE_DEVICE_TABLE(spi, adf4350_id); static struct spi_driver adf4350_driver = { .driver = { .name = "adf4350", - .owner = THIS_MODULE, + .of_match_table = of_match_ptr(adf4350_of_match), }, .probe = adf4350_probe, .remove = adf4350_remove, @@ -481,6 +641,6 @@ }; module_spi_driver(adf4350_driver); -MODULE_AUTHOR("Michael Hennerich "); +MODULE_AUTHOR("Michael Hennerich "); MODULE_DESCRIPTION("Analog Devices ADF4350/ADF4351 PLL"); MODULE_LICENSE("GPL v2");