[PATCH] i3c: master: cdns: Simplify handling clocks in probe()

Krzysztof Kozlowski posted 1 patch 2 months, 3 weeks ago
drivers/i3c/master/i3c-master-cdns.c | 51 +++++++---------------------
1 file changed, 12 insertions(+), 39 deletions(-)
[PATCH] i3c: master: cdns: Simplify handling clocks in probe()
Posted by Krzysztof Kozlowski 2 months, 3 weeks ago
The two clocks, driver is getting, are not being disabled/re-enabled
during runtime of the device.  Eliminate one variable in state struct,
all error paths and a lot of code from probe() and remove() by using
devm_clk_get_enabled().

Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
---
 drivers/i3c/master/i3c-master-cdns.c | 51 +++++++---------------------
 1 file changed, 12 insertions(+), 39 deletions(-)

diff --git a/drivers/i3c/master/i3c-master-cdns.c b/drivers/i3c/master/i3c-master-cdns.c
index 449e85d7ba87..cc504b58013a 100644
--- a/drivers/i3c/master/i3c-master-cdns.c
+++ b/drivers/i3c/master/i3c-master-cdns.c
@@ -412,7 +412,6 @@ struct cdns_i3c_master {
 	} xferqueue;
 	void __iomem *regs;
 	struct clk *sysclk;
-	struct clk *pclk;
 	struct cdns_i3c_master_caps caps;
 	unsigned long i3c_scl_lim;
 	const struct cdns_i3c_data *devdata;
@@ -1566,6 +1565,7 @@ MODULE_DEVICE_TABLE(of, cdns_i3c_master_of_ids);
 static int cdns_i3c_master_probe(struct platform_device *pdev)
 {
 	struct cdns_i3c_master *master;
+	struct clk *pclk;
 	int ret, irq;
 	u32 val;
 
@@ -1581,11 +1581,11 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
 	if (IS_ERR(master->regs))
 		return PTR_ERR(master->regs);
 
-	master->pclk = devm_clk_get(&pdev->dev, "pclk");
-	if (IS_ERR(master->pclk))
-		return PTR_ERR(master->pclk);
+	pclk = devm_clk_get_enabled(&pdev->dev, "pclk");
+	if (IS_ERR(pclk))
+		return PTR_ERR(pclk);
 
-	master->sysclk = devm_clk_get(&pdev->dev, "sysclk");
+	master->sysclk = devm_clk_get_enabled(&pdev->dev, "sysclk");
 	if (IS_ERR(master->sysclk))
 		return PTR_ERR(master->sysclk);
 
@@ -1593,18 +1593,8 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
 	if (irq < 0)
 		return irq;
 
-	ret = clk_prepare_enable(master->pclk);
-	if (ret)
-		return ret;
-
-	ret = clk_prepare_enable(master->sysclk);
-	if (ret)
-		goto err_disable_pclk;
-
-	if (readl(master->regs + DEV_ID) != DEV_ID_I3C_MASTER) {
-		ret = -EINVAL;
-		goto err_disable_sysclk;
-	}
+	if (readl(master->regs + DEV_ID) != DEV_ID_I3C_MASTER)
+		return -EINVAL;
 
 	spin_lock_init(&master->xferqueue.lock);
 	INIT_LIST_HEAD(&master->xferqueue.list);
@@ -1615,7 +1605,7 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
 	ret = devm_request_irq(&pdev->dev, irq, cdns_i3c_master_interrupt, 0,
 			       dev_name(&pdev->dev), master);
 	if (ret)
-		goto err_disable_sysclk;
+		return ret;
 
 	platform_set_drvdata(pdev, master);
 
@@ -1637,29 +1627,15 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
 	master->ibi.slots = devm_kcalloc(&pdev->dev, master->ibi.num_slots,
 					 sizeof(*master->ibi.slots),
 					 GFP_KERNEL);
-	if (!master->ibi.slots) {
-		ret = -ENOMEM;
-		goto err_disable_sysclk;
-	}
+	if (!master->ibi.slots)
+		return -ENOMEM;
 
 	writel(IBIR_THR(1), master->regs + CMD_IBI_THR_CTRL);
 	writel(MST_INT_IBIR_THR, master->regs + MST_IER);
 	writel(DEVS_CTRL_DEV_CLR_ALL, master->regs + DEVS_CTRL);
 
-	ret = i3c_master_register(&master->base, &pdev->dev,
-				  &cdns_i3c_master_ops, false);
-	if (ret)
-		goto err_disable_sysclk;
-
-	return 0;
-
-err_disable_sysclk:
-	clk_disable_unprepare(master->sysclk);
-
-err_disable_pclk:
-	clk_disable_unprepare(master->pclk);
-
-	return ret;
+	return i3c_master_register(&master->base, &pdev->dev,
+				   &cdns_i3c_master_ops, false);
 }
 
 static void cdns_i3c_master_remove(struct platform_device *pdev)
@@ -1668,9 +1644,6 @@ static void cdns_i3c_master_remove(struct platform_device *pdev)
 
 	cancel_work_sync(&master->hj_work);
 	i3c_master_unregister(&master->base);
-
-	clk_disable_unprepare(master->sysclk);
-	clk_disable_unprepare(master->pclk);
 }
 
 static struct platform_driver cdns_i3c_master = {
-- 
2.43.0
Re: [PATCH] i3c: master: cdns: Simplify handling clocks in probe()
Posted by Alexandre Belloni 2 months, 1 week ago
On Sun, 13 Jul 2025 17:24:12 +0200, Krzysztof Kozlowski wrote:
> The two clocks, driver is getting, are not being disabled/re-enabled
> during runtime of the device.  Eliminate one variable in state struct,
> all error paths and a lot of code from probe() and remove() by using
> devm_clk_get_enabled().
> 
> 

Applied, thanks!

[1/1] i3c: master: cdns: Simplify handling clocks in probe()
      https://git.kernel.org/abelloni/c/da9b54708ddf

Best regards,

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Re: [PATCH] i3c: master: cdns: Simplify handling clocks in probe()
Posted by Frank Li 2 months, 3 weeks ago
On Sun, Jul 13, 2025 at 05:24:12PM +0200, Krzysztof Kozlowski wrote:
> The two clocks, driver is getting, are not being disabled/re-enabled
> during runtime of the device.  Eliminate one variable in state struct,
> all error paths and a lot of code from probe() and remove() by using
> devm_clk_get_enabled().
>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> ---
>  drivers/i3c/master/i3c-master-cdns.c | 51 +++++++---------------------
>  1 file changed, 12 insertions(+), 39 deletions(-)
>
> diff --git a/drivers/i3c/master/i3c-master-cdns.c b/drivers/i3c/master/i3c-master-cdns.c
> index 449e85d7ba87..cc504b58013a 100644
> --- a/drivers/i3c/master/i3c-master-cdns.c
> +++ b/drivers/i3c/master/i3c-master-cdns.c
> @@ -412,7 +412,6 @@ struct cdns_i3c_master {
>  	} xferqueue;
>  	void __iomem *regs;
>  	struct clk *sysclk;
> -	struct clk *pclk;
>  	struct cdns_i3c_master_caps caps;
>  	unsigned long i3c_scl_lim;
>  	const struct cdns_i3c_data *devdata;
> @@ -1566,6 +1565,7 @@ MODULE_DEVICE_TABLE(of, cdns_i3c_master_of_ids);
>  static int cdns_i3c_master_probe(struct platform_device *pdev)
>  {
>  	struct cdns_i3c_master *master;
> +	struct clk *pclk;
>  	int ret, irq;
>  	u32 val;
>
> @@ -1581,11 +1581,11 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
>  	if (IS_ERR(master->regs))
>  		return PTR_ERR(master->regs);
>
> -	master->pclk = devm_clk_get(&pdev->dev, "pclk");
> -	if (IS_ERR(master->pclk))
> -		return PTR_ERR(master->pclk);
> +	pclk = devm_clk_get_enabled(&pdev->dev, "pclk");
> +	if (IS_ERR(pclk))
> +		return PTR_ERR(pclk);
>
> -	master->sysclk = devm_clk_get(&pdev->dev, "sysclk");
> +	master->sysclk = devm_clk_get_enabled(&pdev->dev, "sysclk");

Can you use devm_clk_bulk_get_all_enabled() to simpilfy futher?

Frank

>  	if (IS_ERR(master->sysclk))
>  		return PTR_ERR(master->sysclk);
>
> @@ -1593,18 +1593,8 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
>  	if (irq < 0)
>  		return irq;
>
> -	ret = clk_prepare_enable(master->pclk);
> -	if (ret)
> -		return ret;
> -
> -	ret = clk_prepare_enable(master->sysclk);
> -	if (ret)
> -		goto err_disable_pclk;
> -
> -	if (readl(master->regs + DEV_ID) != DEV_ID_I3C_MASTER) {
> -		ret = -EINVAL;
> -		goto err_disable_sysclk;
> -	}
> +	if (readl(master->regs + DEV_ID) != DEV_ID_I3C_MASTER)
> +		return -EINVAL;
>
>  	spin_lock_init(&master->xferqueue.lock);
>  	INIT_LIST_HEAD(&master->xferqueue.list);
> @@ -1615,7 +1605,7 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
>  	ret = devm_request_irq(&pdev->dev, irq, cdns_i3c_master_interrupt, 0,
>  			       dev_name(&pdev->dev), master);
>  	if (ret)
> -		goto err_disable_sysclk;
> +		return ret;
>
>  	platform_set_drvdata(pdev, master);
>
> @@ -1637,29 +1627,15 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
>  	master->ibi.slots = devm_kcalloc(&pdev->dev, master->ibi.num_slots,
>  					 sizeof(*master->ibi.slots),
>  					 GFP_KERNEL);
> -	if (!master->ibi.slots) {
> -		ret = -ENOMEM;
> -		goto err_disable_sysclk;
> -	}
> +	if (!master->ibi.slots)
> +		return -ENOMEM;
>
>  	writel(IBIR_THR(1), master->regs + CMD_IBI_THR_CTRL);
>  	writel(MST_INT_IBIR_THR, master->regs + MST_IER);
>  	writel(DEVS_CTRL_DEV_CLR_ALL, master->regs + DEVS_CTRL);
>
> -	ret = i3c_master_register(&master->base, &pdev->dev,
> -				  &cdns_i3c_master_ops, false);
> -	if (ret)
> -		goto err_disable_sysclk;
> -
> -	return 0;
> -
> -err_disable_sysclk:
> -	clk_disable_unprepare(master->sysclk);
> -
> -err_disable_pclk:
> -	clk_disable_unprepare(master->pclk);
> -
> -	return ret;
> +	return i3c_master_register(&master->base, &pdev->dev,
> +				   &cdns_i3c_master_ops, false);
>  }
>
>  static void cdns_i3c_master_remove(struct platform_device *pdev)
> @@ -1668,9 +1644,6 @@ static void cdns_i3c_master_remove(struct platform_device *pdev)
>
>  	cancel_work_sync(&master->hj_work);
>  	i3c_master_unregister(&master->base);
> -
> -	clk_disable_unprepare(master->sysclk);
> -	clk_disable_unprepare(master->pclk);
>  }
>
>  static struct platform_driver cdns_i3c_master = {
> --
> 2.43.0
>
Re: [PATCH] i3c: master: cdns: Simplify handling clocks in probe()
Posted by Krzysztof Kozlowski 2 months, 3 weeks ago
On 14/07/2025 16:15, Frank Li wrote:
> On Sun, Jul 13, 2025 at 05:24:12PM +0200, Krzysztof Kozlowski wrote:
>> The two clocks, driver is getting, are not being disabled/re-enabled
>> during runtime of the device.  Eliminate one variable in state struct,
>> all error paths and a lot of code from probe() and remove() by using
>> devm_clk_get_enabled().
>>
>> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
>> ---
>>  drivers/i3c/master/i3c-master-cdns.c | 51 +++++++---------------------
>>  1 file changed, 12 insertions(+), 39 deletions(-)
>>
>> diff --git a/drivers/i3c/master/i3c-master-cdns.c b/drivers/i3c/master/i3c-master-cdns.c
>> index 449e85d7ba87..cc504b58013a 100644
>> --- a/drivers/i3c/master/i3c-master-cdns.c
>> +++ b/drivers/i3c/master/i3c-master-cdns.c
>> @@ -412,7 +412,6 @@ struct cdns_i3c_master {
>>  	} xferqueue;
>>  	void __iomem *regs;
>>  	struct clk *sysclk;
>> -	struct clk *pclk;
>>  	struct cdns_i3c_master_caps caps;
>>  	unsigned long i3c_scl_lim;
>>  	const struct cdns_i3c_data *devdata;
>> @@ -1566,6 +1565,7 @@ MODULE_DEVICE_TABLE(of, cdns_i3c_master_of_ids);
>>  static int cdns_i3c_master_probe(struct platform_device *pdev)
>>  {
>>  	struct cdns_i3c_master *master;
>> +	struct clk *pclk;
>>  	int ret, irq;
>>  	u32 val;
>>
>> @@ -1581,11 +1581,11 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
>>  	if (IS_ERR(master->regs))
>>  		return PTR_ERR(master->regs);
>>
>> -	master->pclk = devm_clk_get(&pdev->dev, "pclk");
>> -	if (IS_ERR(master->pclk))
>> -		return PTR_ERR(master->pclk);
>> +	pclk = devm_clk_get_enabled(&pdev->dev, "pclk");
>> +	if (IS_ERR(pclk))
>> +		return PTR_ERR(pclk);
>>
>> -	master->sysclk = devm_clk_get(&pdev->dev, "sysclk");
>> +	master->sysclk = devm_clk_get_enabled(&pdev->dev, "sysclk");
> 
> Can you use devm_clk_bulk_get_all_enabled() to simpilfy futher?

Instead of asking redundant question check yourself. On a first glance
it cannot, because it won't be simpler - you still need individual
clock. But if you find it possible which is not visible on first glance,
make a proposal instead of just random drive by comments.

Best regards,
Krzysztof
Re: [PATCH] i3c: master: cdns: Simplify handling clocks in probe()
Posted by Frank Li 2 months, 3 weeks ago
On Mon, Jul 14, 2025 at 04:40:53PM +0200, Krzysztof Kozlowski wrote:
> On 14/07/2025 16:15, Frank Li wrote:
> > On Sun, Jul 13, 2025 at 05:24:12PM +0200, Krzysztof Kozlowski wrote:
> >> The two clocks, driver is getting, are not being disabled/re-enabled
> >> during runtime of the device.  Eliminate one variable in state struct,
> >> all error paths and a lot of code from probe() and remove() by using
> >> devm_clk_get_enabled().
> >>
> >> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> >> ---
> >>  drivers/i3c/master/i3c-master-cdns.c | 51 +++++++---------------------
> >>  1 file changed, 12 insertions(+), 39 deletions(-)
> >>
> >> diff --git a/drivers/i3c/master/i3c-master-cdns.c b/drivers/i3c/master/i3c-master-cdns.c
> >> index 449e85d7ba87..cc504b58013a 100644
> >> --- a/drivers/i3c/master/i3c-master-cdns.c
> >> +++ b/drivers/i3c/master/i3c-master-cdns.c
> >> @@ -412,7 +412,6 @@ struct cdns_i3c_master {
> >>  	} xferqueue;
> >>  	void __iomem *regs;
> >>  	struct clk *sysclk;
> >> -	struct clk *pclk;
> >>  	struct cdns_i3c_master_caps caps;
> >>  	unsigned long i3c_scl_lim;
> >>  	const struct cdns_i3c_data *devdata;
> >> @@ -1566,6 +1565,7 @@ MODULE_DEVICE_TABLE(of, cdns_i3c_master_of_ids);
> >>  static int cdns_i3c_master_probe(struct platform_device *pdev)
> >>  {
> >>  	struct cdns_i3c_master *master;
> >> +	struct clk *pclk;
> >>  	int ret, irq;
> >>  	u32 val;
> >>
> >> @@ -1581,11 +1581,11 @@ static int cdns_i3c_master_probe(struct platform_device *pdev)
> >>  	if (IS_ERR(master->regs))
> >>  		return PTR_ERR(master->regs);
> >>
> >> -	master->pclk = devm_clk_get(&pdev->dev, "pclk");
> >> -	if (IS_ERR(master->pclk))
> >> -		return PTR_ERR(master->pclk);
> >> +	pclk = devm_clk_get_enabled(&pdev->dev, "pclk");
> >> +	if (IS_ERR(pclk))
> >> +		return PTR_ERR(pclk);
> >>
> >> -	master->sysclk = devm_clk_get(&pdev->dev, "sysclk");
> >> +	master->sysclk = devm_clk_get_enabled(&pdev->dev, "sysclk");
> >
> > Can you use devm_clk_bulk_get_all_enabled() to simpilfy futher?
>
> Instead of asking redundant question check yourself. On a first glance
> it cannot, because it won't be simpler - you still need individual
> clock. But if you find it possible which is not visible on first glance,
> make a proposal instead of just random drive by comments.

It is hard to find that sysclk is used at other place only from this patch
without check source code. I think you are expert. If it is feasible, a
simple reminder should be enough. If not, simple reply the reason/difficult.

In svc i3c driver, we use devm_clk_bulk_get_all_enabled(). And make code
simple because it use runtime pm.

In this case, using two dev_clk_get_enabled() is simplest. I think it should
be similar with svc i3c driver at first glance.

Reviewed-by: Frank Li <Frank.Li@nxp.com>

Frank

>
> Best regards,
> Krzysztof