phy: zynqmp: Handle the clock enable/disable properly
PATCH 1/1 phy: zynqmp: Handle the clock enable/disable properly
The current driver is not handling the clock enable/disable operations properly. The clocks need to be handled correctly by enabling or disabling at appropriate places. This patch adds code to handle the same.
--- drivers/phy/xilinx/phy-zynqmp.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-)
drivers/phy/xilinx/phy-zynqmp.c +37 -3
@@ -219,6 +219,7 @@ struct xpsgtr_dev {struct mutex gtr_mutex;/* mutex for locking */ struct xpsgtr_phy phys[NUM_LANES ];const struct xpsgtr_ssc *refclk_sscs[NUM_LANES ]; +struct clk *clk[NUM_LANES ];bool tx_term_fix;unsigned int saved_icm_cfg0;unsigned int saved_icm_cfg1;
@@ -818,11 +819,15 @@ static struct phy *xpsgtr_xlate(struct device *dev,static int __maybe_unused xpsgtr_suspend (struct device *dev) {struct xpsgtr_dev *gtr_dev =dev_get_drvdata (dev); +int i;/* Save the snapshot ICM_CFG registers. */ gtr_dev->saved_icm_cfg0 =xpsgtr_read (gtr_dev,ICM_CFG0 ); gtr_dev->saved_icm_cfg1 =xpsgtr_read (gtr_dev,ICM_CFG1 ); +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) +clk_disable (gtr_dev->clk[i]); +return 0 ; }
@@ -832,6 +837,13 @@ static int __maybe_unused xpsgtr_resume(struct device *dev)unsigned int icm_cfg0, icm_cfg1;unsigned int i;bool skip_phy_init; +int err; + +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) { + err =clk_enable (gtr_dev->clk[i]); +if (err) +return err; + } icm_cfg0 =xpsgtr_read (gtr_dev,ICM_CFG0 ); icm_cfg1 =xpsgtr_read (gtr_dev,ICM_CFG1 );
@@ -865,6 +877,7 @@ static const struct dev_pm_ops xpsgtr_pm_ops = {static int xpsgtr_get_ref_clocks (struct xpsgtr_dev *gtr_dev) {unsigned int refclk; +int ret;for (refclk =0 ; refclk <ARRAY_SIZE (gtr_dev->refclk_sscs); ++refclk) {unsigned long rate;
@@ -882,6 +895,12 @@ static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev)if (!clk)continue ; + gtr_dev->clk[refclk] = clk; + + ret =clk_prepare_enable (gtr_dev->clk[refclk]); +if (ret) +return ret; +/* * Get the spread spectrum (SSC) settings for the reference * clock rate.
@@ -899,11 +918,17 @@ static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev)dev_err (gtr_dev->dev,"Invalid rate %lu for reference clock %u\n" , rate, refclk); -return -EINVAL ; + goto err_clk_put; } }return 0 ; + +err_clk_put: +for (refclk =0 ; refclk <ARRAY_SIZE (gtr_dev->clk); refclk++) +clk_disable_unprepare (gtr_dev->clk[refclk]); + +return -EINVAL ; }static int xpsgtr_probe (struct platform_device *pdev)
@@ -913,6 +938,7 @@ static int xpsgtr_probe(struct platform_device *pdev)struct phy_provider *provider;unsigned int port;int ret; +int i; gtr_dev =devm_kzalloc (&pdev->dev,sizeof (*gtr_dev),GFP_KERNEL );if (!gtr_dev)
@@ -951,7 +977,8 @@ static int xpsgtr_probe(struct platform_device *pdev) phy =devm_phy_create (&pdev->dev, np, &xpsgtr_phyops);if (IS_ERR (phy)) {dev_err (&pdev->dev,"failed to create PHY\n" ); -return PTR_ERR (phy); + ret =PTR_ERR (phy); + goto err_clk_put; } gtr_phy->phy = phy;
@@ -962,9 +989,16 @@ static int xpsgtr_probe(struct platform_device *pdev) provider =devm_of_phy_provider_register (&pdev->dev, xpsgtr_xlate);if (IS_ERR (provider)) {dev_err (&pdev->dev,"registering provider failed\n" ); -return PTR_ERR (provider); + ret =PTR_ERR (provider); + goto err_clk_put; }return 0 ; + +err_clk_put: +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) +clk_disable_unprepare (gtr_dev->clk[i]); + +return ret; }static const struct of_device_id xpsgtr_of_match[] = {
Signature · 1 line
2.1.1
Hi Manish,
Thank you for the patch.
Quoted 23 lines · Manish Narani
The current driver is not handling the clock enable/disable operations properly. The clocks need to be handled correctly by enabling or disabling at appropriate places. This patch adds code to handle the same.
Signed-off-by Manish Narani <[email protected]>--- drivers/phy/xilinx/phy-zynqmp.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-)drivers/phy/xilinx/phy-zynqmp.c +1
-0@@ -219,6 +219,7 @@ struct xpsgtr_dev {struct mutex gtr_mutex;/* mutex for locking */ struct xpsgtr_phy phys[NUM_LANES ];const struct xpsgtr_ssc *refclk_sscs[NUM_LANES ]; +struct clk *clk[NUM_LANES ];bool tx_term_fix;unsigned int saved_icm_cfg0;unsigned int saved_icm_cfg1;
drivers/phy/xilinx/phy-zynqmp.c static struct phy *xpsgtr_xlate(struct device *dev, · line 822
Quoted 4 lines
drivers/phy/xilinx/phy-zynqmp.c +1
-0@@ -818,11 +819,15 @@ static struct phy *xpsgtr_xlate(struct device *dev,static int __maybe_unused xpsgtr_suspend (struct device *dev) {struct xpsgtr_dev *gtr_dev =dev_get_drvdata (dev); +int i;
i is never negative, so you can make it an unsigned int.
drivers/phy/xilinx/phy-zynqmp.c static struct phy *xpsgtr_xlate(struct device *dev, · line 829
Quoted 7 lines
drivers/phy/xilinx/phy-zynqmp.c +2
-0/* Save the snapshot ICM_CFG registers. */ gtr_dev->saved_icm_cfg0 =xpsgtr_read (gtr_dev,ICM_CFG0 ); gtr_dev->saved_icm_cfg1 =xpsgtr_read (gtr_dev,ICM_CFG1 ); +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) +clk_disable (gtr_dev->clk[i]);
Why is this only clk_disable(), and not clk_disable_unprepare() ? Same question for xpsgtr_resume().
Quoted 5 lines
drivers/phy/xilinx/phy-zynqmp.c +1
-0+return 0 ; }
drivers/phy/xilinx/phy-zynqmp.c static int __maybe_unused xpsgtr_resume(struct device *dev) · line 845
Quoted 9 lines
drivers/phy/xilinx/phy-zynqmp.c +6
-0@@ -832,6 +837,13 @@ static int __maybe_unused xpsgtr_resume(struct device *dev)unsigned int icm_cfg0, icm_cfg1;unsigned int i;bool skip_phy_init; +int err; + +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) { + err =clk_enable (gtr_dev->clk[i]); +if (err) +return err;
In case of error we need to disable the clocks that have been successfully enabled already.
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev)
Quoted 13 lines
drivers/phy/xilinx/phy-zynqmp.c +2
-0+ } icm_cfg0 =xpsgtr_read (gtr_dev,ICM_CFG0 ); icm_cfg1 =xpsgtr_read (gtr_dev,ICM_CFG1 );@@ -865,6 +877,7 @@ static const struct dev_pm_ops xpsgtr_pm_ops = {static int xpsgtr_get_ref_clocks (struct xpsgtr_dev *gtr_dev) {unsigned int refclk; +int ret;for (refclk =0 ; refclk <ARRAY_SIZE (gtr_dev->refclk_sscs); ++refclk) {unsigned long rate;@@ -882,6 +895,12 @@ static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev)
There's an error check above that needs to jump to err_clk_put too.
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev) · line 902
Quoted 8 lines
drivers/phy/xilinx/phy-zynqmp.c +5
-0if (!clk)continue ; + gtr_dev->clk[refclk] = clk; + + ret =clk_prepare_enable (gtr_dev->clk[refclk]); +if (ret) +return ret;
It would be nice to move the driver to runtime PM to keep the clocks disabled when the PHY isn't in use. It can be done in a separate patch.
Quoted 24 lines
drivers/phy/xilinx/phy-zynqmp.c +8
-1+/* * Get the spread spectrum (SSC) settings for the reference * clock rate. @@ -899,11 +918,17 @@ static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev)dev_err (gtr_dev->dev,"Invalid rate %lu for reference clock %u\n" , rate, refclk); -return -EINVAL ; + goto err_clk_put; } }return 0 ; + +err_clk_put: +for (refclk =0 ; refclk <ARRAY_SIZE (gtr_dev->clk); refclk++) +clk_disable_unprepare (gtr_dev->clk[refclk]); + +return -EINVAL ; }static int xpsgtr_probe (struct platform_device *pdev)
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_probe(struct platform_device *pdev) · line 941
Quoted 4 lines
drivers/phy/xilinx/phy-zynqmp.c +1
-0@@ -913,6 +938,7 @@ static int xpsgtr_probe(struct platform_device *pdev)struct phy_provider *provider;unsigned int port;int ret; +int i;
unsigned int here too.
Quoted 31 lines
drivers/phy/xilinx/phy-zynqmp.c +10
-2gtr_dev =devm_kzalloc (&pdev->dev,sizeof (*gtr_dev),GFP_KERNEL );if (!gtr_dev)@@ -951,7 +977,8 @@ static int xpsgtr_probe(struct platform_device *pdev) phy =devm_phy_create (&pdev->dev, np, &xpsgtr_phyops);if (IS_ERR (phy)) {dev_err (&pdev->dev,"failed to create PHY\n" ); -return PTR_ERR (phy); + ret =PTR_ERR (phy); + goto err_clk_put; } gtr_phy->phy = phy;@@ -962,9 +989,16 @@ static int xpsgtr_probe(struct platform_device *pdev) provider =devm_of_phy_provider_register (&pdev->dev, xpsgtr_xlate);if (IS_ERR (provider)) {dev_err (&pdev->dev,"registering provider failed\n" ); -return PTR_ERR (provider); + ret =PTR_ERR (provider); + goto err_clk_put; }return 0 ; + +err_clk_put: +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) +clk_disable_unprepare (gtr_dev->clk[i]); + +return ret; }static const struct of_device_id xpsgtr_of_match[] = {
Signature · 3 lines
Regards, Laurent Pinchart
Hi Laurent,
Thank you for the review.
Quoted 37 lines
-----Original Message-----
From: Laurent Pinchart <[email protected]>
Sent: Tuesday, March 9, 2021 1:37 AM
To: Manish Narani <[email protected]>
Cc: Anurag Kumar Vulisha <[email protected]>; [email protected]; [email protected]; Michal Simek <[email protected]>; linux- [email protected]; [email protected]
Subject: Re: [PATCH] phy: zynqmp: Handle the clock enable/disable properlyHi Manish,
Thank you for the patch.
Quoted 8 lines · Manish Narani
The current driver is not handling the clock enable/disable operations properly. The clocks need to be handled correctly by enabling or disabling at appropriate places. This patch adds code to handle the same.
Signed-off-by Manish Narani <[email protected]>--- drivers/phy/xilinx/phy-zynqmp.c | 40+++++++++++++++++++++++++++++++++++++---
drivers/phy/xilinx/phy-
Quoted 3 lines
1 file changed, 37 insertions(+), 3 deletions(-)drivers/phy/xilinx/phy- renamed from drivers/phy/xilinx/phy-zynqmp.c +0
-0
zynqmp.cQuoted 10 lines
index 2b65f84..0ec534e 100644
drivers/phy/xilinx/phy-zynqmp.c +1
-0@@ -219,6 +219,7 @@ struct xpsgtr_dev {struct mutex gtr_mutex;/* mutex for locking */ struct xpsgtr_phy phys[NUM_LANES ];const struct xpsgtr_ssc *refclk_sscs[NUM_LANES ]; +struct clk *clk[NUM_LANES ];bool tx_term_fix;unsigned int saved_icm_cfg0;
Quoted 8 lines
drivers/phy/xilinx/phy-zynqmp.c static struct phy *xpsgtr_xlate(struct device *dev, · line 822
Quoted 6 lines
drivers/phy/xilinx/phy-zynqmp.c +1
-0unsigned int saved_icm_cfg1;@@ -818,11 +819,15 @@ static struct phy *xpsgtr_xlate(struct device *dev,static int __maybe_unused xpsgtr_suspend (struct device *dev) {struct xpsgtr_dev *gtr_dev =dev_get_drvdata (dev); +int i;i is never negative, so you can make it an unsigned int.
OK. Will update in v2.
Quoted 11 lines
drivers/phy/xilinx/phy-zynqmp.c static struct phy *xpsgtr_xlate(struct device *dev, · line 829
Quoted 8 lines
drivers/phy/xilinx/phy-zynqmp.c +2
-0/* Save the snapshot ICM_CFG registers. */ gtr_dev->saved_icm_cfg0 =xpsgtr_read (gtr_dev,ICM_CFG0 ); gtr_dev->saved_icm_cfg1 =xpsgtr_read (gtr_dev,ICM_CFG1 ); +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) +clk_disable (gtr_dev->clk[i]);Why is this only
clk_disable(), and notclk_disable_unprepare()? Same question forxpsgtr_resume().
It can be clk_disable_unprepare() and clk_prepare_enable in suspend() and resume() respectively, will update in v2.
Quoted 7 lines
drivers/phy/xilinx/phy-zynqmp.c static int __maybe_unused xpsgtr_resume(struct
Quoted 6 lines
drivers/phy/xilinx/phy-zynqmp.c +1
-0+return 0 ; }@@ -832,6 +837,13 @@ static int __maybe_unused xpsgtr_resume(structdevice *dev)
Quoted 12 lines
drivers/phy/xilinx/phy-zynqmp.c static int __maybe_unused xpsgtr_resume(struct device *dev) · line 845
Quoted 9 lines
drivers/phy/xilinx/phy-zynqmp.c +6
-0unsigned int icm_cfg0, icm_cfg1;unsigned int i;bool skip_phy_init; +int err; + +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) { + err =clk_enable (gtr_dev->clk[i]); +if (err) +return err;In case of error we need to disable the clocks that have been successfully enabled already.
Thanks for bringing this. Will be updated in v2.
Quoted 21 lines
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_get_ref_clocks(struct xpsgtr_dev
Quoted 14 lines
drivers/phy/xilinx/phy-zynqmp.c +2
-0+ } icm_cfg0 =xpsgtr_read (gtr_dev,ICM_CFG0 ); icm_cfg1 =xpsgtr_read (gtr_dev,ICM_CFG1 );@@ -865,6 +877,7 @@ static const struct dev_pm_ops xpsgtr_pm_ops = {static int xpsgtr_get_ref_clocks (struct xpsgtr_dev *gtr_dev) {unsigned int refclk; +int ret;for (refclk =0 ; refclk <ARRAY_SIZE (gtr_dev->refclk_sscs); ++refclk) {unsigned long rate;@@ -882,6 +895,12 @@ static int xpsgtr_get_ref_clocks(struct xpsgtr_dev
*gtr_dev)There's an error check above that needs to jump to
err_clk_puttoo.Quoted 3 lines
drivers/phy/xilinx/phy-zynqmp.c +0
-0if (!clk)continue ;
Quoted 8 lines
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_get_ref_clocks(struct xpsgtr_dev *gtr_dev) · line 902
Quoted 5 lines
drivers/phy/xilinx/phy-zynqmp.c +5
-0+ gtr_dev->clk[refclk] = clk; + + ret =clk_prepare_enable (gtr_dev->clk[refclk]); +if (ret) +return ret;It would be nice to move the driver to runtime PM to keep the clocks disabled when the PHY isn't in use. It can be done in a separate patch.
Sure thanks. I will plan for that as well.
Thanks,
Manish
Quoted 73 lines
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_get_ref_clocks(struct xpsgtr_dev
Quoted 6 lines
drivers/phy/xilinx/phy-zynqmp.c +1
-0+/* * Get the spread spectrum (SSC) settings for the reference * clock rate. @@ -899,11 +918,17 @@ static int xpsgtr_get_ref_clocks(struct xpsgtr_dev
*gtr_dev)drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_probe(struct platform_device
Quoted 19 lines
drivers/phy/xilinx/phy-zynqmp.c +7
-1dev_err (gtr_dev->dev,"Invalid rate %lu for reference clock %u\n" , rate, refclk); -return -EINVAL ; + goto err_clk_put; } }return 0 ; + +err_clk_put: +for (refclk =0 ; refclk <ARRAY_SIZE (gtr_dev->clk); refclk++) +clk_disable_unprepare (gtr_dev->clk[refclk]); + +return -EINVAL ; }static int xpsgtr_probe (struct platform_device *pdev)@@ -913,6 +938,7 @@ static int xpsgtr_probe(struct platform_device*pdev)
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_probe(struct platform_device *pdev) · line 941
Quoted 4 lines
drivers/phy/xilinx/phy-zynqmp.c +1
-0struct phy_provider *provider;unsigned int port;int ret; +int i;unsigned int here too.
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_probe(struct platform_device
Quoted 4 lines
drivers/phy/xilinx/phy-zynqmp.c +0
-0gtr_dev =devm_kzalloc (&pdev->dev,sizeof (*gtr_dev),GFP_KERNEL );if (!gtr_dev)@@ -951,7 +977,8 @@ static int xpsgtr_probe(struct platform_device*pdev)
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_probe(struct platform_device
Quoted 10 lines
drivers/phy/xilinx/phy-zynqmp.c +2
-1phy =devm_phy_create (&pdev->dev, np, &xpsgtr_phyops);if (IS_ERR (phy)) {dev_err (&pdev->dev,"failed to create PHY\n" ); -return PTR_ERR (phy); + ret =PTR_ERR (phy); + goto err_clk_put; } gtr_phy->phy = phy;@@ -962,9 +989,16 @@ static int xpsgtr_probe(struct platform_device*pdev)
drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_probe(struct platform_device · line 989
Quoted 1 line
drivers/phy/xilinx/phy-zynqmp.c +0
-0provider = devm_of_phy_provider_register(&pdev->dev,
xpsgtr_xlate);Quoted 6 lines
drivers/phy/xilinx/phy-zynqmp.c +2
-1if (IS_ERR (provider)) {dev_err (&pdev->dev,"registering provider failed\n" ); -return PTR_ERR (provider); + ret =PTR_ERR (provider); + goto err_clk_put; }drivers/phy/xilinx/phy-zynqmp.c static int xpsgtr_probe(struct platform_device *pdev) · line 1004
Quoted 10 lines
drivers/phy/xilinx/phy-zynqmp.c +6
-0return 0 ; + +err_clk_put: +for (i =0 ; i <ARRAY_SIZE (gtr_dev->clk); i++) +clk_disable_unprepare (gtr_dev->clk[i]); + +return ret; }static const struct of_device_id xpsgtr_of_match[] = {--
Regards,Laurent Pinchart