staging: iio: frequency: ad9833: Load clock using clock framework

The clock frequency is loaded from device-tree using clock framework
instead of statically value. The change allow configuration of
the device via device-trees and better initialization sequence.
This is part of broader effort to add device-tree support to this driver
and take it out from staging.

Signed-off-by: Beniamin Bia <beniamin.bia@analog.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
Beniamin Bia 2019-02-01 17:01:38 +02:00 committed by Jonathan Cameron
parent 80109c3234
commit 8e8040c52e
1 changed files with 24 additions and 11 deletions

View File

@ -6,6 +6,7 @@
* Licensed under the GPL-2. * Licensed under the GPL-2.
*/ */
#include <linux/clk.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <linux/device.h> #include <linux/device.h>
@ -71,7 +72,7 @@
struct ad9834_state { struct ad9834_state {
struct spi_device *spi; struct spi_device *spi;
struct regulator *reg; struct regulator *reg;
unsigned int mclk; struct clk *mclk;
unsigned short control; unsigned short control;
unsigned short devid; unsigned short devid;
struct spi_transfer xfer; struct spi_transfer xfer;
@ -110,12 +111,15 @@ static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
static int ad9834_write_frequency(struct ad9834_state *st, static int ad9834_write_frequency(struct ad9834_state *st,
unsigned long addr, unsigned long fout) unsigned long addr, unsigned long fout)
{ {
unsigned long clk_freq;
unsigned long regval; unsigned long regval;
if (fout > (st->mclk / 2)) clk_freq = clk_get_rate(st->mclk);
if (fout > (clk_freq / 2))
return -EINVAL; return -EINVAL;
regval = ad9834_calc_freqreg(st->mclk, fout); regval = ad9834_calc_freqreg(clk_freq, fout);
st->freq_data[0] = cpu_to_be16(addr | (regval & st->freq_data[0] = cpu_to_be16(addr | (regval &
RES_MASK(AD9834_FREQ_BITS / 2))); RES_MASK(AD9834_FREQ_BITS / 2)));
@ -413,7 +417,14 @@ static int ad9834_probe(struct spi_device *spi)
spi_set_drvdata(spi, indio_dev); spi_set_drvdata(spi, indio_dev);
st = iio_priv(indio_dev); st = iio_priv(indio_dev);
mutex_init(&st->lock); mutex_init(&st->lock);
st->mclk = 25000000; st->mclk = devm_clk_get(&spi->dev, NULL);
ret = clk_prepare_enable(st->mclk);
if (ret) {
dev_err(&spi->dev, "Failed to enable master clock\n");
goto error_disable_reg;
}
st->spi = spi; st->spi = spi;
st->devid = spi_get_device_id(spi)->driver_data; st->devid = spi_get_device_id(spi)->driver_data;
st->reg = reg; st->reg = reg;
@ -458,31 +469,32 @@ static int ad9834_probe(struct spi_device *spi)
ret = spi_sync(st->spi, &st->msg); ret = spi_sync(st->spi, &st->msg);
if (ret) { if (ret) {
dev_err(&spi->dev, "device init failed\n"); dev_err(&spi->dev, "device init failed\n");
goto error_disable_reg; goto error_clock_unprepare;
} }
ret = ad9834_write_frequency(st, AD9834_REG_FREQ0, 1000000); ret = ad9834_write_frequency(st, AD9834_REG_FREQ0, 1000000);
if (ret) if (ret)
goto error_disable_reg; goto error_clock_unprepare;
ret = ad9834_write_frequency(st, AD9834_REG_FREQ1, 5000000); ret = ad9834_write_frequency(st, AD9834_REG_FREQ1, 5000000);
if (ret) if (ret)
goto error_disable_reg; goto error_clock_unprepare;
ret = ad9834_write_phase(st, AD9834_REG_PHASE0, 512); ret = ad9834_write_phase(st, AD9834_REG_PHASE0, 512);
if (ret) if (ret)
goto error_disable_reg; goto error_clock_unprepare;
ret = ad9834_write_phase(st, AD9834_REG_PHASE1, 1024); ret = ad9834_write_phase(st, AD9834_REG_PHASE1, 1024);
if (ret) if (ret)
goto error_disable_reg; goto error_clock_unprepare;
ret = iio_device_register(indio_dev); ret = iio_device_register(indio_dev);
if (ret) if (ret)
goto error_disable_reg; goto error_clock_unprepare;
return 0; return 0;
error_clock_unprepare:
clk_disable_unprepare(st->mclk);
error_disable_reg: error_disable_reg:
regulator_disable(reg); regulator_disable(reg);
@ -495,6 +507,7 @@ static int ad9834_remove(struct spi_device *spi)
struct ad9834_state *st = iio_priv(indio_dev); struct ad9834_state *st = iio_priv(indio_dev);
iio_device_unregister(indio_dev); iio_device_unregister(indio_dev);
clk_disable_unprepare(st->mclk);
regulator_disable(st->reg); regulator_disable(st->reg);
return 0; return 0;