linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 1/2] dt-bindings: iio: adf4350: add clk provider prop
@ 2024-06-18  8:20 Antoniu Miclaus
  2024-06-18  8:20 ` [PATCH v5 2/2] iio: frequency: adf4350: add clk provider Antoniu Miclaus
  0 siblings, 1 reply; 3+ messages in thread
From: Antoniu Miclaus @ 2024-06-18  8:20 UTC (permalink / raw)
  To: Michael Turquette, Stephen Boyd, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Lars-Peter Clausen, Michael Hennerich,
	Jonathan Cameron, linux-clk, devicetree, linux-kernel, linux-iio
  Cc: Antoniu Miclaus, Conor Dooley

Add properties required for providing clock to other consumers.

Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
Acked-by: Conor Dooley <conor.dooley@microchip.com>
---
 .../devicetree/bindings/iio/frequency/adi,adf4350.yaml      | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/Documentation/devicetree/bindings/iio/frequency/adi,adf4350.yaml b/Documentation/devicetree/bindings/iio/frequency/adi,adf4350.yaml
index 43cbf27114c7..d1d1311332f8 100644
--- a/Documentation/devicetree/bindings/iio/frequency/adi,adf4350.yaml
+++ b/Documentation/devicetree/bindings/iio/frequency/adi,adf4350.yaml
@@ -28,6 +28,12 @@ properties:
   clock-names:
     const: clkin
 
+  '#clock-cells':
+    const: 0
+
+  clock-output-names:
+    maxItems: 1
+
   gpios:
     maxItems: 1
     description: Lock detect GPIO.
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v5 2/2] iio: frequency: adf4350: add clk provider
  2024-06-18  8:20 [PATCH v5 1/2] dt-bindings: iio: adf4350: add clk provider prop Antoniu Miclaus
@ 2024-06-18  8:20 ` Antoniu Miclaus
  2024-06-18  9:37   ` Nuno Sá
  0 siblings, 1 reply; 3+ messages in thread
From: Antoniu Miclaus @ 2024-06-18  8:20 UTC (permalink / raw)
  To: Michael Turquette, Stephen Boyd, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Lars-Peter Clausen, Michael Hennerich,
	Jonathan Cameron, linux-clk, devicetree, linux-kernel, linux-iio
  Cc: Antoniu Miclaus

Add clk provider feature for the adf4350.

Even though the driver was sent as an IIO driver in most cases the
device is actually seen as a clock provider.

This patch aims to cover actual usecases requested by users in order to
completely control the output frequencies from userspace.

Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
---
changes in v5:
 - add error handling for dt properties parse.
 - drop all IIO channels if driver acts as clock provider.
 drivers/iio/frequency/adf4350.c | 129 +++++++++++++++++++++++++++++++-
 1 file changed, 127 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/frequency/adf4350.c b/drivers/iio/frequency/adf4350.c
index 4abf80f75ef5..d0791a21e65c 100644
--- a/drivers/iio/frequency/adf4350.c
+++ b/drivers/iio/frequency/adf4350.c
@@ -19,6 +19,7 @@
 #include <linux/gpio/consumer.h>
 #include <asm/div64.h>
 #include <linux/clk.h>
+#include <linux/clk-provider.h>
 
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -36,6 +37,9 @@ struct adf4350_state {
 	struct gpio_desc		*lock_detect_gpiod;
 	struct adf4350_platform_data	*pdata;
 	struct clk			*clk;
+	struct clk			*clkout;
+	const char			*clk_out_name;
+	struct clk_hw			hw;
 	unsigned long			clkin;
 	unsigned long			chspc; /* Channel Spacing */
 	unsigned long			fpfd; /* Phase Frequency Detector */
@@ -61,6 +65,8 @@ struct adf4350_state {
 	__be32				val __aligned(IIO_DMA_MINALIGN);
 };
 
+#define to_adf4350_state(_hw) container_of(_hw, struct adf4350_state, hw)
+
 static struct adf4350_platform_data default_pdata = {
 	.channel_spacing = 10000,
 	.r2_user_settings = ADF4350_REG2_PD_POLARITY_POS |
@@ -381,6 +387,115 @@ static const struct iio_info adf4350_info = {
 	.debugfs_reg_access = &adf4350_reg_access,
 };
 
+static void adf4350_clk_del_provider(void *data)
+{
+	struct adf4350_state *st = data;
+
+	of_clk_del_provider(st->spi->dev.of_node);
+}
+
+static unsigned long adf4350_clk_recalc_rate(struct clk_hw *hw,
+					     unsigned long parent_rate)
+{
+	struct adf4350_state *st = to_adf4350_state(hw);
+	unsigned long long tmp;
+
+	tmp = (u64)(st->r0_int * st->r1_mod + st->r0_fract) * st->fpfd;
+	do_div(tmp, st->r1_mod * (1 << st->r4_rf_div_sel));
+
+	return tmp;
+}
+
+static int adf4350_clk_set_rate(struct clk_hw *hw,
+				unsigned long rate,
+				unsigned long parent_rate)
+{
+	struct adf4350_state *st = to_adf4350_state(hw);
+
+	if (parent_rate == 0 || parent_rate > ADF4350_MAX_FREQ_REFIN)
+		return -EINVAL;
+
+	st->clkin = parent_rate;
+
+	return adf4350_set_freq(st, rate);
+}
+
+static int adf4350_clk_prepare(struct clk_hw *hw)
+{
+	struct adf4350_state *st = to_adf4350_state(hw);
+
+	st->regs[ADF4350_REG2] &= ~ADF4350_REG2_POWER_DOWN_EN;
+
+	return adf4350_sync_config(st);
+}
+
+static void adf4350_clk_unprepare(struct clk_hw *hw)
+{
+	struct adf4350_state *st = to_adf4350_state(hw);
+
+	st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN;
+
+	adf4350_sync_config(st);
+}
+
+static int adf4350_clk_is_enabled(struct clk_hw *hw)
+{
+	struct adf4350_state *st = to_adf4350_state(hw);
+
+	return (st->regs[ADF4350_REG2] & ADF4350_REG2_POWER_DOWN_EN);
+}
+
+static const struct clk_ops adf4350_clk_ops = {
+	.recalc_rate = adf4350_clk_recalc_rate,
+	.set_rate = adf4350_clk_set_rate,
+	.prepare = adf4350_clk_prepare,
+	.unprepare = adf4350_clk_unprepare,
+	.is_enabled = adf4350_clk_is_enabled,
+};
+
+static int adf4350_clk_register(struct adf4350_state *st)
+{
+	struct spi_device *spi = st->spi;
+	struct clk_init_data init;
+	struct clk *clk;
+	const char *parent_name;
+	int ret;
+
+	if (!device_property_present(&spi->dev, "#clock-cells"))
+		return 0;
+
+	init.name = devm_kasprintf(&spi->dev, GFP_KERNEL, "%s-clk",
+				   fwnode_get_name(dev_fwnode(&spi->dev)));
+	if (!init.name)
+		return -ENOMEM;
+
+	if (device_property_read_string(&spi->dev, "clock-output-names",
+					&init.name))
+		init.name = spi->dev.of_node->name;
+
+	parent_name = of_clk_get_parent_name(spi->dev.of_node, 0);
+	if (!parent_name)
+		return -EINVAL;
+
+	init.ops = &adf4350_clk_ops;
+	init.parent_names = &parent_name;
+	init.num_parents = 1;
+	init.flags = CLK_SET_RATE_PARENT;
+
+	st->hw.init = &init;
+	clk = devm_clk_register(&spi->dev, &st->hw);
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
+
+	ret = of_clk_add_provider(spi->dev.of_node, of_clk_src_simple_get, clk);
+	if (ret)
+		return ret;
+
+	st->clkout = clk;
+
+	return devm_add_action_or_reset(&spi->dev, adf4350_clk_del_provider, st);
+}
+
 static struct adf4350_platform_data *adf4350_parse_dt(struct device *dev)
 {
 	struct adf4350_platform_data *pdata;
@@ -522,8 +637,6 @@ static int adf4350_probe(struct spi_device *spi)
 
 	indio_dev->info = &adf4350_info;
 	indio_dev->modes = INDIO_DIRECT_MODE;
-	indio_dev->channels = &adf4350_chan;
-	indio_dev->num_channels = 1;
 
 	mutex_init(&st->lock);
 
@@ -551,6 +664,18 @@ static int adf4350_probe(struct spi_device *spi)
 			return ret;
 	}
 
+	ret = adf4350_clk_register(st);
+	if (ret)
+		return ret;
+
+	if (st->clkout) {
+		indio_dev->channels = NULL;
+		indio_dev->num_channels = 0;
+	} else {
+		indio_dev->channels = &adf4350_chan;
+		indio_dev->num_channels = 1;
+	}
+
 	ret = devm_add_action_or_reset(&spi->dev, adf4350_power_down, indio_dev);
 	if (ret)
 		return dev_err_probe(&spi->dev, ret,
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v5 2/2] iio: frequency: adf4350: add clk provider
  2024-06-18  8:20 ` [PATCH v5 2/2] iio: frequency: adf4350: add clk provider Antoniu Miclaus
@ 2024-06-18  9:37   ` Nuno Sá
  0 siblings, 0 replies; 3+ messages in thread
From: Nuno Sá @ 2024-06-18  9:37 UTC (permalink / raw)
  To: Antoniu Miclaus, Michael Turquette, Stephen Boyd, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Lars-Peter Clausen,
	Michael Hennerich, Jonathan Cameron, linux-clk, devicetree,
	linux-kernel, linux-iio

On Tue, 2024-06-18 at 11:20 +0300, Antoniu Miclaus wrote:
> Add clk provider feature for the adf4350.
> 
> Even though the driver was sent as an IIO driver in most cases the
> device is actually seen as a clock provider.
> 
> This patch aims to cover actual usecases requested by users in order to
> completely control the output frequencies from userspace.
> 
> Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
> ---

Hi Antoniu,

For some reason your DT patch is missing (or still did not arrived)... Some
comments below.

...
> 
> +static int adf4350_clk_register(struct adf4350_state *st)
> +{
> +	struct spi_device *spi = st->spi;
> +	struct clk_init_data init;
> +	struct clk *clk;
> +	const char *parent_name;
> +	int ret;
> +
> +	if (!device_property_present(&spi->dev, "#clock-cells"))
> +		return 0;
> +
> +	init.name = devm_kasprintf(&spi->dev, GFP_KERNEL, "%s-clk",
> +				   fwnode_get_name(dev_fwnode(&spi->dev)));
> +	if (!init.name)
> +		return -ENOMEM;
> +
> +	if (device_property_read_string(&spi->dev, "clock-output-names",
> +					&init.name))
> +		init.name = spi->dev.of_node->name;

So this means the first devm_kasprintf() is useless :). I believe this should be
	
	init.name = devm_kasprintf(&spi->dev, GFP_KERNEL, "%s-clk",
				   fwnode_get_name(dev_fwnode(&spi->dev)));
	if (!init.name)
		return -ENOMEM;


> +
> +	parent_name = of_clk_get_parent_name(spi->dev.of_node, 0);
> +	if (!parent_name)
> +		return -EINVAL;
> +
> +	init.ops = &adf4350_clk_ops;
> +	init.parent_names = &parent_name;
> +	init.num_parents = 1;
> +	init.flags = CLK_SET_RATE_PARENT;
> +
> +	st->hw.init = &init;
> +	clk = devm_clk_register(&spi->dev, &st->hw);
> +	if (IS_ERR(clk))
> +		return PTR_ERR(clk);
> +
> +	ret = of_clk_add_provider(spi->dev.of_node, of_clk_src_simple_get,
> clk);
> +	if (ret)
> +		return ret;
> +
> +	st->clkout = clk;
> +
> +	return devm_add_action_or_reset(&spi->dev, adf4350_clk_del_provider,
> st);
> +}
> +
>  static struct adf4350_platform_data *adf4350_parse_dt(struct device *dev)
>  {
>  	struct adf4350_platform_data *pdata;
> @@ -522,8 +637,6 @@ static int adf4350_probe(struct spi_device *spi)
>  
>  	indio_dev->info = &adf4350_info;
>  	indio_dev->modes = INDIO_DIRECT_MODE;
> -	indio_dev->channels = &adf4350_chan;
> -	indio_dev->num_channels = 1;
>  
>  	mutex_init(&st->lock);
>  
> @@ -551,6 +664,18 @@ static int adf4350_probe(struct spi_device *spi)
>  			return ret;
>  	}
>  
> +	ret = adf4350_clk_register(st);
> +	if (ret)
> +		return ret;
> +
> +	if (st->clkout) {
> +		indio_dev->channels = NULL;
> +		indio_dev->num_channels = 0;
> +	} else {
> +		indio_dev->channels = &adf4350_chan;
> +		indio_dev->num_channels = 1;
> +	}

nit: The above could be simplied to
if (!st->clkout) {
	indio_dev->channels = &adf4350_chan;
	indio_dev->num_channels = 1;
}

- Nuno Sá


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-06-18  9:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-18  8:20 [PATCH v5 1/2] dt-bindings: iio: adf4350: add clk provider prop Antoniu Miclaus
2024-06-18  8:20 ` [PATCH v5 2/2] iio: frequency: adf4350: add clk provider Antoniu Miclaus
2024-06-18  9:37   ` Nuno Sá

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).