Skip to contentSkip to conversation

staging: iio: frequency: ad9833: Get frequency value statically

PATCH In review Beniamin Bia 3 participants 5 messages Feb 1, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 staging: iio: frequency: ad9833: Get frequency value statically

The value of frequency is taken from ad9834.c instead of platform data

---
 drivers/staging/iio/frequency/ad9834.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/staging/iio/frequency/ad9834.c +12 -5
index 995acdd7c942..d92d4bf71261 100644
@@ -99,6 +99,16 @@ enum ad9834_supported_device_ids {
 	ID_AD9838,
 };
 
+static struct ad9834_platform_data default_config = {
+	.mclk = 25000000,
+	.freq0 = 1000000,
+	.freq1 = 5000000,
+	.phase0 = 512,
+	.phase1 = 1024,
+	.en_div2 = false,
+	.en_signbit_msb_out = false,
+};
+
 static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
 {
 	unsigned long long freqreg = (u64)fout * (u64)BIT(AD9834_FREQ_BITS);
@@ -391,16 +401,13 @@ static const struct iio_info ad9833_info = {
 
 static int ad9834_probe(struct spi_device *spi)
 {
-	struct ad9834_platform_data *pdata = dev_get_platdata(&spi->dev);
+	struct ad9834_platform_data *pdata;
 	struct ad9834_state *st;
 	struct iio_dev *indio_dev;
 	struct regulator *reg;
 	int ret;
 
-	if (!pdata) {
-		dev_dbg(&spi->dev, "no platform data?\n");
-		return -ENODEV;
-	}
+	pdata = &default_config;
 
 	reg = devm_regulator_get(&spi->dev, "avdd");
 	if (IS_ERR(reg))
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 staging: iio: frequency: ad9833: Load clock using clock framework

The clock frequency is loaded from device-tree using clock framework instead of old platform data structure. 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.

---
 drivers/staging/iio/frequency/ad9834.c | 24 +++++++++++++++++++-----
 drivers/staging/iio/frequency/ad9834.h |  2 --
 2 files changed, 19 insertions(+), 7 deletions(-)
drivers/staging/iio/frequency/ad9834.c +19 -5
index d92d4bf71261..3b25065c1a72 100644
@@ -6,6 +6,7 @@
  * Licensed under the GPL-2.
  */
 
+#include <linux/clk.h>
 #include <linux/interrupt.h>
 #include <linux/workqueue.h>
 #include <linux/device.h>
@@ -71,7 +72,7 @@
 struct ad9834_state {
 	struct spi_device		*spi;
 	struct regulator		*reg;
-	unsigned int			mclk;
+	struct clk			*mclk;
 	unsigned short			control;
 	unsigned short			devid;
 	struct spi_transfer		xfer;
@@ -100,7 +101,6 @@ enum ad9834_supported_device_ids {
 };
 
 static struct ad9834_platform_data default_config = {
-	.mclk = 25000000,
 	.freq0 = 1000000,
 	.freq1 = 5000000,
 	.phase0 = 512,
@@ -120,12 +120,15 @@ static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
 static int ad9834_write_frequency(struct ad9834_state *st,
 				  unsigned long addr, unsigned long fout)
 {
+	unsigned long clk_freq;
 	unsigned long regval;
 
-	if (fout > (st->mclk / 2))
+	clk_freq = clk_get_rate(st->mclk);
+
+	if (fout > (clk_freq / 2))
 		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 &
 				       RES_MASK(AD9834_FREQ_BITS / 2)));
@@ -405,6 +408,7 @@ static int ad9834_probe(struct spi_device *spi)
 	struct ad9834_state *st;
 	struct iio_dev *indio_dev;
 	struct regulator *reg;
+	struct clk *clk;
 	int ret;
 
 	pdata = &default_config;
@@ -419,6 +423,14 @@ static int ad9834_probe(struct spi_device *spi)
 		return ret;
 	}
 
+	clk = devm_clk_get(&spi->dev, NULL);
+
+	ret = clk_prepare_enable(clk);
+	if (ret) {
+		dev_err(&spi->dev, "Failed to enable master clock\n");
+		return ret;
+	}
+
 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
 	if (!indio_dev) {
 		ret = -ENOMEM;
@@ -427,7 +439,7 @@ static int ad9834_probe(struct spi_device *spi)
 	spi_set_drvdata(spi, indio_dev);
 	st = iio_priv(indio_dev);
 	mutex_init(&st->lock);
-	st->mclk = pdata->mclk;
+	st->mclk = clk;
 	st->spi = spi;
 	st->devid = spi_get_device_id(spi)->driver_data;
 	st->reg = reg;
@@ -501,6 +513,7 @@ static int ad9834_probe(struct spi_device *spi)
 
 error_disable_reg:
 	regulator_disable(reg);
+	clk_disable_unprepare(st->mclk);
 
 	return ret;
 }
@@ -512,6 +525,7 @@ static int ad9834_remove(struct spi_device *spi)
 
 	iio_device_unregister(indio_dev);
 	regulator_disable(st->reg);
+	clk_disable_unprepare(st->mclk);
 
 	return 0;
 }
drivers/staging/iio/frequency/ad9834.h +0 -2
index ae620f38eb49..16562dc10812 100644
@@ -14,7 +14,6 @@
 
 /**
  * struct ad9834_platform_data - platform specific information
- * @mclk:		master clock in Hz
  * @freq0:		power up freq0 tuning word in Hz
  * @freq1:		power up freq1 tuning word in Hz
  * @phase0:		power up phase0 value [0..4095] correlates with 0..2PI
@@ -27,7 +26,6 @@
  */
 
 struct ad9834_platform_data {
-	unsigned int		mclk;
 	unsigned int		freq0;
 	unsigned int		freq1;
 	unsigned short		phase0;
Signature · 1 line
2.17.1
#2 lore raw
In reply to Beniamin Bia
Quoted 1 line · On Fri, 1 Feb 2019 11:36:37 +0200 Beniamin Bia <[email protected]> wrote:

The value of frequency is taken from ad9834.c instead of platform data

Why? I would rather see this move over to DT than take aways the flexibility that was previously there.

Jonathan

Quoted 46 lines
---
 drivers/staging/iio/frequency/ad9834.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/staging/iio/frequency/ad9834.c +12 -5
index 995acdd7c942..d92d4bf71261 100644
@@ -99,6 +99,16 @@ enum ad9834_supported_device_ids {
 	ID_AD9838,
 };
 
+static struct ad9834_platform_data default_config = {
+	.mclk = 25000000,
+	.freq0 = 1000000,
+	.freq1 = 5000000,
+	.phase0 = 512,
+	.phase1 = 1024,
+	.en_div2 = false,
+	.en_signbit_msb_out = false,
+};
+
 static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
 {
 	unsigned long long freqreg = (u64)fout * (u64)BIT(AD9834_FREQ_BITS);
@@ -391,16 +401,13 @@ static const struct iio_info ad9833_info = {
 
 static int ad9834_probe(struct spi_device *spi)
 {
-	struct ad9834_platform_data *pdata = dev_get_platdata(&spi->dev);
+	struct ad9834_platform_data *pdata;
 	struct ad9834_state *st;
 	struct iio_dev *indio_dev;
 	struct regulator *reg;
 	int ret;
 
-	if (!pdata) {
-		dev_dbg(&spi->dev, "no platform data?\n");
-		return -ENODEV;
-	}
+	pdata = &default_config;
 
 	reg = devm_regulator_get(&spi->dev, "avdd");
 	if (IS_ERR(reg))
#3 lore raw

The mclk is loaded from device tree in the next patch. This was just a intermediate step. But what should i do with the rest of the fields?
Frequency and phase are the default configuration of the device and they don't belong in device tree. They could also be modified from sysfs.
Should i remove the struct and write directly the value in probe function?

Ben
________________________________________
From: Jonathan Cameron [[email protected]]
Sent: Friday, February 01, 2019 1:24 PM
To: Beniamin Bia
Cc: [email protected]; Bia, Beniamin
Subject: Re: [PATCH 1/2] staging: iio: frequency: ad9833: Get frequency value statically

[External]

Quoted 1 line · On Fri, 1 Feb 2019 11:36:37 +0200 Beniamin Bia <[email protected]> wrote:

The value of frequency is taken from ad9834.c instead of platform data

Why? I would rather see this move over to DT than take aways the flexibility that was previously there.

Jonathan

Quoted 46 lines
---
 drivers/staging/iio/frequency/ad9834.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/staging/iio/frequency/ad9834.c +12 -5
index 995acdd7c942..d92d4bf71261 100644
@@ -99,6 +99,16 @@ enum ad9834_supported_device_ids {
      ID_AD9838,
 };
 
+static struct ad9834_platform_data default_config = {
+     .mclk = 25000000,
+     .freq0 = 1000000,
+     .freq1 = 5000000,
+     .phase0 = 512,
+     .phase1 = 1024,
+     .en_div2 = false,
+     .en_signbit_msb_out = false,
+};
+
 static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
 {
      unsigned long long freqreg = (u64)fout * (u64)BIT(AD9834_FREQ_BITS);
@@ -391,16 +401,13 @@ static const struct iio_info ad9833_info = {
 
 static int ad9834_probe(struct spi_device *spi)
 {
-     struct ad9834_platform_data *pdata = dev_get_platdata(&spi->dev);
+     struct ad9834_platform_data *pdata;
      struct ad9834_state *st;
      struct iio_dev *indio_dev;
      struct regulator *reg;
      int ret;
 
-     if (!pdata) {
-             dev_dbg(&spi->dev, "no platform data?\n");
-             return -ENODEV;
-     }
+     pdata = &default_config;
 
      reg = devm_regulator_get(&spi->dev, "avdd");
      if (IS_ERR(reg))
#4 lore raw
Quoted 5 lines · Beniamin

The mclk is loaded from device tree in the next patch. This was just a intermediate step. But what should i do with the rest of the fields?
Frequency and phase are the default configuration of the device and they don't belong in device tree. They could also be modified from sysfs.
Should i remove the struct and write directly the value in probe function?

Yes, that would make sense.

Jonathan

Quoted 70 lines

Ben
________________________________________
From: Jonathan Cameron [[email protected]]
Sent: Friday, February 01, 2019 1:24 PM
To: Beniamin Bia
Cc: [email protected]; Bia, Beniamin
Subject: Re: [PATCH 1/2] staging: iio: frequency: ad9833: Get frequency value statically

[External]

Quoted 1 line · On Fri, 1 Feb 2019 11:36:37 +0200 Beniamin Bia <[email protected]> wrote:

The value of frequency is taken from ad9834.c instead of platform data

Why? I would rather see this move over to DT than take aways the flexibility that was previously there.

Jonathan

Quoted 46 lines
---
 drivers/staging/iio/frequency/ad9834.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/staging/iio/frequency/ad9834.c +12 -5
index 995acdd7c942..d92d4bf71261 100644
@@ -99,6 +99,16 @@ enum ad9834_supported_device_ids {
      ID_AD9838,
 };
 
+static struct ad9834_platform_data default_config = {
+     .mclk = 25000000,
+     .freq0 = 1000000,
+     .freq1 = 5000000,
+     .phase0 = 512,
+     .phase1 = 1024,
+     .en_div2 = false,
+     .en_signbit_msb_out = false,
+};
+
 static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
 {
      unsigned long long freqreg = (u64)fout * (u64)BIT(AD9834_FREQ_BITS);
@@ -391,16 +401,13 @@ static const struct iio_info ad9833_info = {
 
 static int ad9834_probe(struct spi_device *spi)
 {
-     struct ad9834_platform_data *pdata = dev_get_platdata(&spi->dev);
+     struct ad9834_platform_data *pdata;
      struct ad9834_state *st;
      struct iio_dev *indio_dev;
      struct regulator *reg;
      int ret;
 
-     if (!pdata) {
-             dev_dbg(&spi->dev, "no platform data?\n");
-             return -ENODEV;
-     }
+     pdata = &default_config;
 
      reg = devm_regulator_get(&spi->dev, "avdd");
      if (IS_ERR(reg))