Skip to content

Commit 9404fa1

Browse files
mhennerichjic23
authored andcommitted
iio: frequency: adf4350: Add support for clock consumer framework
Preferably get clkin (PLL reference clock) from clock framework Signed-off-by: Michael Hennerich <michael.hennerich@analog.com> Reviewed-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 5f43264 commit 9404fa1

1 file changed

Lines changed: 49 additions & 9 deletions

File tree

‎drivers/iio/frequency/adf4350.c‎

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* ADF4350/ADF4351 SPI Wideband Synthesizer driver
33
*
4-
* Copyright 2012 Analog Devices Inc.
4+
* Copyright 2012-2013 Analog Devices Inc.
55
*
66
* Licensed under the GPL-2.
77
*/
@@ -17,6 +17,7 @@
1717
#include <linux/gcd.h>
1818
#include <linux/gpio.h>
1919
#include <asm/div64.h>
20+
#include <linux/clk.h>
2021

2122
#include <linux/iio/iio.h>
2223
#include <linux/iio/sysfs.h>
@@ -33,6 +34,7 @@ struct adf4350_state {
3334
struct spi_device *spi;
3435
struct regulator *reg;
3536
struct adf4350_platform_data *pdata;
37+
struct clk *clk;
3638
unsigned long clkin;
3739
unsigned long chspc; /* Channel Spacing */
3840
unsigned long fpfd; /* Phase Frequency Detector */
@@ -43,7 +45,7 @@ struct adf4350_state {
4345
unsigned r4_rf_div_sel;
4446
unsigned long regs[6];
4547
unsigned long regs_hw[6];
46-
48+
unsigned long long freq_req;
4749
/*
4850
* DMA (thus cache coherency maintenance) requires the
4951
* transfer buffers to live in their own cache lines.
@@ -52,7 +54,6 @@ struct adf4350_state {
5254
};
5355

5456
static struct adf4350_platform_data default_pdata = {
55-
.clkin = 122880000,
5657
.channel_spacing = 10000,
5758
.r2_user_settings = ADF4350_REG2_PD_POLARITY_POS |
5859
ADF4350_REG2_CHARGE_PUMP_CURR_uA(2500),
@@ -235,6 +236,7 @@ static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq)
235236
ADF4350_REG4_MUTE_TILL_LOCK_EN));
236237

237238
st->regs[ADF4350_REG5] = ADF4350_REG5_LD_PIN_MODE_DIGITAL;
239+
st->freq_req = freq;
238240

239241
return adf4350_sync_config(st);
240242
}
@@ -246,6 +248,7 @@ static ssize_t adf4350_write(struct iio_dev *indio_dev,
246248
{
247249
struct adf4350_state *st = iio_priv(indio_dev);
248250
unsigned long long readin;
251+
unsigned long tmp;
249252
int ret;
250253

251254
ret = kstrtoull(buf, 10, &readin);
@@ -258,10 +261,23 @@ static ssize_t adf4350_write(struct iio_dev *indio_dev,
258261
ret = adf4350_set_freq(st, readin);
259262
break;
260263
case ADF4350_FREQ_REFIN:
261-
if (readin > ADF4350_MAX_FREQ_REFIN)
264+
if (readin > ADF4350_MAX_FREQ_REFIN) {
262265
ret = -EINVAL;
263-
else
264-
st->clkin = readin;
266+
break;
267+
}
268+
269+
if (st->clk) {
270+
tmp = clk_round_rate(st->clk, readin);
271+
if (tmp != readin) {
272+
ret = -EINVAL;
273+
break;
274+
}
275+
ret = clk_set_rate(st->clk, tmp);
276+
if (ret < 0)
277+
break;
278+
}
279+
st->clkin = readin;
280+
ret = adf4350_set_freq(st, st->freq_req);
265281
break;
266282
case ADF4350_FREQ_RESOLUTION:
267283
if (readin == 0)
@@ -308,6 +324,9 @@ static ssize_t adf4350_read(struct iio_dev *indio_dev,
308324
}
309325
break;
310326
case ADF4350_FREQ_REFIN:
327+
if (st->clk)
328+
st->clkin = clk_get_rate(st->clk);
329+
311330
val = st->clkin;
312331
break;
313332
case ADF4350_FREQ_RESOLUTION:
@@ -318,6 +337,7 @@ static ssize_t adf4350_read(struct iio_dev *indio_dev,
318337
break;
319338
default:
320339
ret = -EINVAL;
340+
val = 0;
321341
}
322342
mutex_unlock(&indio_dev->mlock);
323343

@@ -360,14 +380,24 @@ static int adf4350_probe(struct spi_device *spi)
360380
struct adf4350_platform_data *pdata = spi->dev.platform_data;
361381
struct iio_dev *indio_dev;
362382
struct adf4350_state *st;
383+
struct clk *clk = NULL;
363384
int ret;
364385

365386
if (!pdata) {
366387
dev_warn(&spi->dev, "no platform data? using default\n");
367-
368388
pdata = &default_pdata;
369389
}
370390

391+
if (!pdata->clkin) {
392+
clk = clk_get(&spi->dev, "clkin");
393+
if (IS_ERR(clk))
394+
return -EPROBE_DEFER;
395+
396+
ret = clk_prepare_enable(clk);
397+
if (ret < 0)
398+
return ret;
399+
}
400+
371401
indio_dev = iio_device_alloc(sizeof(*st));
372402
if (indio_dev == NULL)
373403
return -ENOMEM;
@@ -395,7 +425,12 @@ static int adf4350_probe(struct spi_device *spi)
395425
indio_dev->num_channels = 1;
396426

397427
st->chspc = pdata->channel_spacing;
398-
st->clkin = pdata->clkin;
428+
if (clk) {
429+
st->clk = clk;
430+
st->clkin = clk_get_rate(clk);
431+
} else {
432+
st->clkin = pdata->clkin;
433+
}
399434

400435
st->min_out_freq = spi_get_device_id(spi)->driver_data == 4351 ?
401436
ADF4351_MIN_OUT_FREQ : ADF4350_MIN_OUT_FREQ;
@@ -435,6 +470,8 @@ static int adf4350_probe(struct spi_device *spi)
435470
if (!IS_ERR(st->reg))
436471
regulator_put(st->reg);
437472

473+
if (clk)
474+
clk_disable_unprepare(clk);
438475
iio_device_free(indio_dev);
439476

440477
return ret;
@@ -451,6 +488,9 @@ static int adf4350_remove(struct spi_device *spi)
451488

452489
iio_device_unregister(indio_dev);
453490

491+
if (st->clk)
492+
clk_disable_unprepare(st->clk);
493+
454494
if (!IS_ERR(reg)) {
455495
regulator_disable(reg);
456496
regulator_put(reg);
@@ -481,6 +521,6 @@ static struct spi_driver adf4350_driver = {
481521
};
482522
module_spi_driver(adf4350_driver);
483523

484-
MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
524+
MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
485525
MODULE_DESCRIPTION("Analog Devices ADF4350/ADF4351 PLL");
486526
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)