mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] clk: Add clk_bulk_[get|put]_all()
@ 2020-09-23  9:03 Sascha Hauer
  2020-09-23  9:03 ` [PATCH 2/2] usb: dwc3-of-simple: Use clk_bulk API Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2020-09-23  9:03 UTC (permalink / raw)
  To: Barebox List

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/clk/clk-bulk.c | 80 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/clk.h    | 42 ++++++++++++++++++++++
 2 files changed, 122 insertions(+)

diff --git a/drivers/clk/clk-bulk.c b/drivers/clk/clk-bulk.c
index ddbe32f9c2..b8db60dcbc 100644
--- a/drivers/clk/clk-bulk.c
+++ b/drivers/clk/clk-bulk.c
@@ -53,6 +53,86 @@ err:
 }
 EXPORT_SYMBOL(clk_bulk_get);
 
+static int __must_check of_clk_bulk_get(struct device_node *np, int num_clks,
+					struct clk_bulk_data *clks)
+{
+	int ret;
+	int i;
+
+	for (i = 0; i < num_clks; i++) {
+		clks[i].id = NULL;
+		clks[i].clk = NULL;
+	}
+
+	for (i = 0; i < num_clks; i++) {
+		of_property_read_string_index(np, "clock-names", i, &clks[i].id);
+		clks[i].clk = of_clk_get(np, i);
+		if (IS_ERR(clks[i].clk)) {
+			ret = PTR_ERR(clks[i].clk);
+			pr_err("%pOF: Failed to get clk index: %d ret: %d\n",
+				np, i, ret);
+			clks[i].clk = NULL;
+			goto err;
+		}
+	}
+
+	return 0;
+
+err:
+	clk_bulk_put(i, clks);
+
+	return ret;
+}
+
+static int __must_check of_clk_bulk_get_all(struct device_node *np,
+					    struct clk_bulk_data **clks)
+{
+	struct clk_bulk_data *clk_bulk;
+	int num_clks;
+	int ret;
+
+	num_clks = of_clk_get_parent_count(np);
+	if (!num_clks)
+		return 0;
+
+	clk_bulk = kmalloc_array(num_clks, sizeof(*clk_bulk), GFP_KERNEL);
+	if (!clk_bulk)
+		return -ENOMEM;
+
+	ret = of_clk_bulk_get(np, num_clks, clk_bulk);
+	if (ret) {
+		kfree(clk_bulk);
+		return ret;
+	}
+
+	*clks = clk_bulk;
+
+	return num_clks;
+}
+
+void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks)
+{
+	if (IS_ERR_OR_NULL(clks))
+		return;
+
+	clk_bulk_put(num_clks, clks);
+
+	kfree(clks);
+}
+EXPORT_SYMBOL(clk_bulk_put_all);
+
+int __must_check clk_bulk_get_all(struct device_d *dev,
+				  struct clk_bulk_data **clks)
+{
+	struct device_node *np = dev->device_node;
+
+	if (!np)
+		return 0;
+
+	return of_clk_bulk_get_all(np, clks);
+}
+EXPORT_SYMBOL(clk_bulk_get_all);
+
 /**
  * clk_bulk_disable - gate a set of clocks
  * @num_clks: the number of clk_bulk_data
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 3d66343e8d..86056d1c0f 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -82,6 +82,27 @@ struct clk *clk_get(struct device_d *dev, const char *id);
 int __must_check clk_bulk_get(struct device_d *dev, int num_clks,
 			      struct clk_bulk_data *clks);
 
+/**
+ * clk_bulk_get_all - lookup and obtain all available references to clock
+ *                    producer.
+ * @dev: device for clock "consumer"
+ * @clks: pointer to the clk_bulk_data table of consumer
+ *
+ * This helper function allows drivers to get all clk consumers in one
+ * operation. If any of the clk cannot be acquired then any clks
+ * that were obtained will be freed before returning to the caller.
+ *
+ * Returns a positive value for the number of clocks obtained while the
+ * clock references are stored in the clk_bulk_data table in @clks field.
+ * Returns 0 if there're none and a negative value if something failed.
+ *
+ * Drivers must assume that the clock source is not enabled.
+ *
+ * clk_bulk_get should not be called from within interrupt context.
+ */
+int __must_check clk_bulk_get_all(struct device_d *dev,
+				  struct clk_bulk_data **clks);
+
 /**
  * clk_enable - inform the system when the clock source should be running.
  * @clk: clock source
@@ -156,6 +177,19 @@ unsigned long clk_get_rate(struct clk *clk);
  */
 void clk_bulk_put(int num_clks, struct clk_bulk_data *clks);
 
+/**
+ * clk_bulk_put_all - "free" all the clock source
+ * @num_clks: the number of clk_bulk_data
+ * @clks: the clk_bulk_data table of consumer
+ *
+ * Note: drivers must ensure that all clk_bulk_enable calls made on this
+ * clock source are balanced by clk_bulk_disable calls prior to calling
+ * this function.
+ *
+ * clk_bulk_put_all should not be called from within interrupt context.
+ */
+void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks);
+
 /*
  * The remaining APIs are optional for machine class support.
  */
@@ -240,8 +274,16 @@ static inline int __must_check clk_bulk_get(struct device_d *dev, int num_clks,
 	return 0;
 }
 
+static inline int __must_check clk_bulk_get_all(struct device *dev,
+						struct clk_bulk_data **clks)
+{
+	return 0;
+}
+
 static inline void clk_bulk_put(int num_clks, struct clk_bulk_data *clks) {}
 
+static inline void clk_bulk_put_all(int num_clks, struct clk_bulk_data *clks) {}
+
 static inline int clk_enable(struct clk *clk)
 {
 	return 0;
-- 
2.28.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* [PATCH 2/2] usb: dwc3-of-simple: Use clk_bulk API
  2020-09-23  9:03 [PATCH 1/2] clk: Add clk_bulk_[get|put]_all() Sascha Hauer
@ 2020-09-23  9:03 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2020-09-23  9:03 UTC (permalink / raw)
  To: Barebox List

Use clk_bulk_get_all() to retrieve all clocks rather than open code
this. Also actually enable the clocks, previously they had been disabled
in the error path, but never enabled before. Also this fixes a memory
corruption: The driver populated an array of clks, but only allocated
space for a single entry.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/usb/dwc3/dwc3-of-simple.c | 58 ++++++-------------------------
 1 file changed, 10 insertions(+), 48 deletions(-)

diff --git a/drivers/usb/dwc3/dwc3-of-simple.c b/drivers/usb/dwc3/dwc3-of-simple.c
index a0aab4f114..ac16d22624 100644
--- a/drivers/usb/dwc3/dwc3-of-simple.c
+++ b/drivers/usb/dwc3/dwc3-of-simple.c
@@ -21,50 +21,16 @@
 
 struct dwc3_of_simple {
 	struct device_d		*dev;
-	struct clk		**clks;
+	struct clk_bulk_data	*clks;
 	int			num_clocks;
 };
 
-static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
-{
-	struct device_d		*dev = simple->dev;
-	struct device_node	*np = dev->device_node;
-	int			i;
-
-	simple->num_clocks = count;
-
-	if (!count)
-		return 0;
-
-	simple->clks = xzalloc(sizeof(struct clk *));
-	if (!simple->clks)
-		return -ENOMEM;
-
-	for (i = 0; i < simple->num_clocks; i++) {
-		struct clk	*clk;
-
-		clk = of_clk_get(np, i);
-		if (IS_ERR(clk)) {
-			while (--i >= 0) {
-				clk_disable(simple->clks[i]);
-				clk_put(simple->clks[i]);
-			}
-			return PTR_ERR(clk);
-		}
-
-		simple->clks[i] = clk;
-	}
-
-	return 0;
-}
-
 static int dwc3_of_simple_probe(struct device_d *dev)
 {
 	struct dwc3_of_simple	*simple;
 	struct device_node	*np = dev->device_node;
 
 	int			ret;
-	int			i;
 
 	simple = xzalloc(sizeof(*simple));
 	if (!simple)
@@ -73,17 +39,18 @@ static int dwc3_of_simple_probe(struct device_d *dev)
 	dev->priv = simple;
 	simple->dev = dev;
 
-	ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
-						"clocks", "#clock-cells"));
+	ret = clk_bulk_get_all(simple->dev, &simple->clks);
+	if (ret < 0)
+		return ret;
+
+	simple->num_clocks = ret;
+	ret = clk_bulk_enable(simple->num_clocks, simple->clks);
 	if (ret)
 		return ret;
 
-	ret = of_platform_populate(np, NULL, dev);
+        ret = of_platform_populate(np, NULL, dev);
 	if (ret) {
-		for (i = 0; i < simple->num_clocks; i++) {
-			clk_disable(simple->clks[i]);
-			clk_put(simple->clks[i]);
-		}
+		clk_bulk_disable(simple->num_clocks, simple->clks);
 		return ret;
 	}
 
@@ -93,13 +60,8 @@ static int dwc3_of_simple_probe(struct device_d *dev)
 static void dwc3_of_simple_remove(struct device_d *dev)
 {
 	struct dwc3_of_simple	*simple = dev->priv;
-	int			i;
 
-	for (i = 0; i < simple->num_clocks; i++) {
-		clk_disable(simple->clks[i]);
-		clk_put(simple->clks[i]);
-	}
-	simple->num_clocks = 0;
+	clk_bulk_disable(simple->num_clocks, simple->clks);
 }
 
 static const struct of_device_id of_dwc3_simple_match[] = {
-- 
2.28.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

end of thread, other threads:[~2020-09-23  9:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-23  9:03 [PATCH 1/2] clk: Add clk_bulk_[get|put]_all() Sascha Hauer
2020-09-23  9:03 ` [PATCH 2/2] usb: dwc3-of-simple: Use clk_bulk API Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox