Skip to contentSkip to conversation

i2c: hisi: Add support to get clock frequency from clock property

PATCH Superseded Weilong Chen 2 participants 2 messages Sep 21, 2022 – Sep 22, 2022

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: hisi: Add support to get clock frequency from clock property

Support the driver to obtain clock information by clk_rate or clock property. Find clock first, if not, fall back to clk_rate.

---
 drivers/i2c/busses/i2c-hisi.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-hisi.c +20 -5
index 67031024217c..5e48d4ee0c6d 100644
@@ -8,6 +8,7 @@
 #include <linux/acpi.h>
 #include <linux/bits.h>
 #include <linux/bitfield.h>
+#include <linux/clk.h>
 #include <linux/completion.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>
@@ -91,6 +92,7 @@ struct hisi_i2c_controller {
 	void __iomem *iobase;
 	struct device *dev;
 	int irq;
+	struct clk *clk;
 
 	/* Intermediates for recording the transfer process */
 	struct completion *completion;
@@ -456,10 +458,21 @@ static int hisi_i2c_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	ret = device_property_read_u64(dev, "clk_rate", &clk_rate_hz);
-	if (ret) {
-		dev_err(dev, "failed to get clock frequency, ret = %d\n", ret);
-		return ret;
+	ctlr->clk = devm_clk_get_optional(&pdev->dev, NULL);
+	if (IS_ERR(ctlr->clk)) {
+		ret = device_property_read_u64(dev, "clk_rate", &clk_rate_hz);
+		if (ret) {
+			dev_err(dev, "failed to get clock frequency, ret = %d\n", ret);
+			return ret;
+		}
+	} else {
+		ret = clk_prepare_enable(ctlr->clk);
+		if (ret) {
+			dev_err(dev, "failed to enable clock, ret = %d\n", ret);
+			return ret;
+		}
+
+		clk_rate_hz = clk_get_rate(ctlr->clk);
 	}
 
 	ctlr->clk_rate_khz = DIV_ROUND_UP_ULL(clk_rate_hz, HZ_PER_KHZ);
@@ -475,8 +488,10 @@ static int hisi_i2c_probe(struct platform_device *pdev)
 	i2c_set_adapdata(adapter, ctlr);
 
 	ret = devm_i2c_add_adapter(dev, adapter);
-	if (ret)
+	if (ret) {
+		clk_disable_unprepare(ctlr->clk);
 		return ret;
+	}
 
 	hw_version = readl(ctlr->iobase + HISI_I2C_VERSION);
 	dev_info(ctlr->dev, "speed mode is %s. hw version 0x%x\n",
Signature · 1 line
2.31.GIT
#1 lore raw

Hi Weilong,

Quoted 21 lines · Weilong Chen

Support the driver to obtain clock information by clk_rate or clock property. Find clock first, if not, fall back to clk_rate.

---
 drivers/i2c/busses/i2c-hisi.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-hisi.c +1 -0
index 67031024217c..5e48d4ee0c6d 100644
@@ -8,6 +8,7 @@
 #include <linux/acpi.h>
 #include <linux/bits.h>
 #include <linux/bitfield.h>
+#include <linux/clk.h>
 #include <linux/completion.h>
 #include <linux/i2c.h>
 #include <linux/interrupt.h>

drivers/i2c/busses/i2c-hisi.c struct hisi_i2c_controller { · line 96

Quoted 5 lines
drivers/i2c/busses/i2c-hisi.c +1 -0
@@ -91,6 +92,7 @@ struct hisi_i2c_controller {
 	void __iomem *iobase;
 	struct device *dev;
 	int irq;
+	struct clk *clk;
 

Prefer to make this field between @irq and @dev, to make these fields ordered in an inverted triangle.

drivers/i2c/busses/i2c-hisi.c static int hisi_i2c_probe(struct platform_device *pdev) · line 462

Quoted 12 lines
drivers/i2c/busses/i2c-hisi.c +2 -4
 	/* Intermediates for recording the transfer process */
 	struct completion *completion;
@@ -456,10 +458,21 @@ static int hisi_i2c_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	ret = device_property_read_u64(dev, "clk_rate", &clk_rate_hz);
-	if (ret) {
-		dev_err(dev, "failed to get clock frequency, ret = %d\n", ret);
-		return ret;
+	ctlr->clk = devm_clk_get_optional(&pdev->dev, NULL);
+	if (IS_ERR(ctlr->clk)) {

Use IS_ERR_OR_NULL(), otherwise we cannot get the clock rate.

drivers/i2c/busses/i2c-hisi.c static int hisi_i2c_probe(struct platform_device *pdev) · line 469

Quoted 7 lines
drivers/i2c/busses/i2c-hisi.c +7 -0
+		ret = device_property_read_u64(dev, "clk_rate", &clk_rate_hz);
+		if (ret) {
+			dev_err(dev, "failed to get clock frequency, ret = %d\n", ret);
+			return ret;
+		}
+	} else {
+		ret = clk_prepare_enable(ctlr->clk);

You didn't disable and unprepare the clock on removal.

Use devm_clk_get_optional_enabled() instead then it's managed by devres and the error path below is unnecessary as well.

BTW, do we really need to prepare and enable the clock? I checked designware-i2c seems it only gets the clock rate and don't do the prepare and enable operations. I may miss something but please have a check.

Thanks,
Yicong

Quoted 22 lines
drivers/i2c/busses/i2c-hisi.c +9 -1
+		if (ret) {
+			dev_err(dev, "failed to enable clock, ret = %d\n", ret);
+			return ret;
+		}
+
+		clk_rate_hz = clk_get_rate(ctlr->clk);
 	}
 
 	ctlr->clk_rate_khz = DIV_ROUND_UP_ULL(clk_rate_hz, HZ_PER_KHZ);
@@ -475,8 +488,10 @@ static int hisi_i2c_probe(struct platform_device *pdev)
 	i2c_set_adapdata(adapter, ctlr);
 
 	ret = devm_i2c_add_adapter(dev, adapter);
-	if (ret)
+	if (ret) {
+		clk_disable_unprepare(ctlr->clk);
 		return ret;
+	}
 
 	hw_version = readl(ctlr->iobase + HISI_I2C_VERSION);
 	dev_info(ctlr->dev, "speed mode is %s. hw version 0x%x\n",