Skip to contentSkip to conversation

clk: Allow clk_get_by_name() with NULL name

PATCH Applied Samuel Holland 3 participants 6 messages Jan 22, 2023 – Feb 12, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 clk: Allow clk_get_by_name() with NULL name

This allows devm_clock_get(dev, NULL) to work and get the first clock, which is common in code ported from Linux.

---

Changes in v2:
- Move index error check inside if statement
- Update function comment
- Add unit test

 drivers/clk/clk-uclass.c | 12 +++++++-----
 include/clk.h            |  4 ++--
 test/dm/clk.c            |  5 +++++
 3 files changed, 14 insertions(+), 7 deletions(-)
drivers/clk/clk-uclass.c +7 -5
index 2f9635524c..dc3e9d6a26 100644
@@ -399,16 +399,18 @@ int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
 
 int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
 {
-	int index;
+	int index = 0;
 
 	debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
 		ofnode_get_name(node), name, clk);
 	clk->dev = NULL;
 
-	index = ofnode_stringlist_search(node, "clock-names", name);
-	if (index < 0) {
-		debug("fdt_stringlist_search() failed: %d\n", index);
-		return index;
+	if (name) {
+		index = ofnode_stringlist_search(node, "clock-names", name);
+		if (index < 0) {
+			debug("fdt_stringlist_search() failed: %d\n", index);
+			return index;
+		}
 	}
 
 	return clk_get_by_index_nodev(node, index, clk);
include/clk.h +2 -2
index 138766bd49..d91285235f 100644
@@ -167,7 +167,7 @@ int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
  * clk_get_by_name() - Get/request a clock by name.
  * @dev:	The client device.
  * @name:	The name of the clock to request, within the client's list of
- *		clocks.
+ *		clocks, or NULL to request the first clock in the list.
  * @clk:	A pointer to a clock struct to initialize.
  *
  * This looks up and requests a clock. The name is relative to the client
@@ -184,7 +184,7 @@ int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
  * clk_get_by_name_nodev - Get/request a clock by name without a device.
  * @node:	The client ofnode.
  * @name:	The name of the clock to request, within the client's list of
- *		clocks.
+ *		clocks, or NULL to request the first clock in the list.
  * @clk:	A pointer to a clock struct to initialize.
  *
  * Return: 0 if OK, or a negative error code.
test/dm/clk.c +5 -0
index 21997ed892..f48de05436 100644
@@ -26,6 +26,11 @@ static int dm_test_clk_base(struct unit_test_state *uts)
 	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", &dev));
 
 	/* Get the same clk port in 2 different ways and compare */
+	ut_assertok(clk_get_by_index(dev, 0, &clk_method1));
+	ut_assertok(clk_get_by_name(dev, NULL, &clk_method2));
+	ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true);
+	ut_asserteq(clk_method1.id, clk_method2.id);
+
 	ut_assertok(clk_get_by_index(dev, 1, &clk_method1));
 	ut_assertok(clk_get_by_index_nodev(dev_ofnode(dev), 1, &clk_method2));
 	ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true);
Signature · 1 line
2.37.4
#1 lore raw

PATCH v2 2/2 reset: Allow reset_get_by_name() with NULL name

This allows devm_reset_control_get(dev, NULL) to work and get the first reset control, which is common in code ported from Linux.

---

Changes in v2:
- Move index error check inside if statement
- Update function comment
- Add unit test

 drivers/reset/reset-uclass.c | 12 +++++++-----
 include/reset.h              |  3 ++-
 test/dm/reset.c              |  3 +--
 3 files changed, 10 insertions(+), 8 deletions(-)
drivers/reset/reset-uclass.c +7 -5
index bcef46039c..b972faf013 100644
@@ -147,16 +147,18 @@ int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
 int reset_get_by_name(struct udevice *dev, const char *name,
 		     struct reset_ctl *reset_ctl)
 {
-	int index;
+	int index = 0;
 
 	debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
 	      reset_ctl);
 	reset_ctl->dev = NULL;
 
-	index = dev_read_stringlist_search(dev, "reset-names", name);
-	if (index < 0) {
-		debug("fdt_stringlist_search() failed: %d\n", index);
-		return index;
+	if (name) {
+		index = dev_read_stringlist_search(dev, "reset-names", name);
+		if (index < 0) {
+			debug("fdt_stringlist_search() failed: %d\n", index);
+			return index;
+		}
 	}
 
 	return reset_get_by_index(dev, index, reset_ctl);
include/reset.h +2 -1
index 965f02e0ce..036a786d2a 100644
@@ -238,7 +238,8 @@ int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk);
  *
  * @dev:	The client device.
  * @name:	The name of the reset signal to request, within the client's
- *		list of reset signals.
+ *		list of reset signals, or NULL to request the first reset
+ *		signal in the list.
  * @reset_ctl:	A pointer to a reset control struct to initialize.
  * Return: 0 if OK, or a negative error code.
  */
test/dm/reset.c +1 -2
index 9c00452336..e2d6f45623 100644
@@ -35,8 +35,7 @@ static int dm_test_reset_base(struct unit_test_state *uts)
 
 	/* Get the same reset port in 2 different ways and compare */
 	ut_assertok(reset_get_by_index(dev, 0, &reset_method1));
-	ut_assertok(reset_get_by_index_nodev(dev_ofnode(dev), 0,
-					     &reset_method1_1));
+	ut_assertok(reset_get_by_name(dev, NULL, &reset_method1_1));
 	ut_assertok(reset_get_by_index(dev, 1, &reset_method2));
 	ut_assertok(reset_get_by_index_nodev(dev_ofnode(dev), 1,
 					     &reset_method2_1));
Signature · 1 line
2.37.4
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] reset: Allow reset_get_by_name() with NULL name

Quoted 12 lines · Samuel Holland

This allows devm_reset_control_get(dev, NULL) to work and get the first reset control, which is common in code ported from Linux.

---

Changes in v2:
- Move index error check inside if statement
- Update function comment
- Add unit test

Quoted 4 lines
 drivers/reset/reset-uclass.c | 12 +++++++-----
 include/reset.h              |  3 ++-
 test/dm/reset.c              |  3 +--
 3 files changed, 10 insertions(+), 8 deletions(-)
Reviewed-by on v2 1/2
#3 lore raw
In reply to Samuel Holland
Quoted 12 lines · Samuel Holland

This allows devm_clock_get(dev, NULL) to work and get the first clock, which is common in code ported from Linux.

---

Changes in v2:
- Move index error check inside if statement
- Update function comment
- Add unit test

Quoted 5 lines
 drivers/clk/clk-uclass.c | 12 +++++++-----
 include/clk.h            |  4 ++--
 test/dm/clk.c            |  5 +++++
 3 files changed, 14 insertions(+), 7 deletions(-)
Reviewed-by on v2 1/2
#4 lore raw
In reply to Samuel Holland
Quoted 72 lines · Samuel Holland

This allows devm_clock_get(dev, NULL) to work and get the first clock, which is common in code ported from Linux.

---

Changes in v2:
- Move index error check inside if statement
- Update function comment
- Add unit test

  drivers/clk/clk-uclass.c | 12 +++++++-----
  include/clk.h            |  4 ++--
  test/dm/clk.c            |  5 +++++
  3 files changed, 14 insertions(+), 7 deletions(-)
drivers/clk/clk-uclass.c +7 -5
index 2f9635524c..dc3e9d6a26 100644
@@ -399,16 +399,18 @@ int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
 
  int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
  {
-	int index;
+	int index = 0;
 
  	debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
  		ofnode_get_name(node), name, clk);
  	clk->dev = NULL;
 
-	index = ofnode_stringlist_search(node, "clock-names", name);
-	if (index < 0) {
-		debug("fdt_stringlist_search() failed: %d\n", index);
-		return index;
+	if (name) {
+		index = ofnode_stringlist_search(node, "clock-names", name);
+		if (index < 0) {
+			debug("fdt_stringlist_search() failed: %d\n", index);
+			return index;
+		}
  	}
 
  	return clk_get_by_index_nodev(node, index, clk);
include/clk.h +2 -2
index 138766bd49..d91285235f 100644
@@ -167,7 +167,7 @@ int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk);
   * clk_get_by_name() - Get/request a clock by name.
   * @dev:	The client device.
   * @name:	The name of the clock to request, within the client's list of
- *		clocks.
+ *		clocks, or NULL to request the first clock in the list.
   * @clk:	A pointer to a clock struct to initialize.
   *
   * This looks up and requests a clock. The name is relative to the client
@@ -184,7 +184,7 @@ int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk);
   * clk_get_by_name_nodev - Get/request a clock by name without a device.
   * @node:	The client ofnode.
   * @name:	The name of the clock to request, within the client's list of
- *		clocks.
+ *		clocks, or NULL to request the first clock in the list.
   * @clk:	A pointer to a clock struct to initialize.
   *
   * Return: 0 if OK, or a negative error code.
test/dm/clk.c +0 -0
index 21997ed892..f48de05436 100644
@@ -26,6 +26,11 @@ static int dm_test_clk_base(struct unit_test_state *uts)
  	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", &dev));

test/dm/clk.c static int dm_test_clk_base(struct unit_test_state *uts) · line 36

Quoted 10 lines
test/dm/clk.c +5 -0
 
  	/* Get the same clk port in 2 different ways and compare */
+	ut_assertok(clk_get_by_index(dev, 0, &clk_method1));
+	ut_assertok(clk_get_by_name(dev, NULL, &clk_method2));
+	ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true);
+	ut_asserteq(clk_method1.id, clk_method2.id);
+
  	ut_assertok(clk_get_by_index(dev, 1, &clk_method1));
  	ut_assertok(clk_get_by_index_nodev(dev_ofnode(dev), 1, &clk_method2));
  	ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true);
#5 lore raw
In reply to Samuel Holland
Quoted 4 lines · Samuel Holland

This allows devm_clock_get(dev, NULL) to work and get the first clock, which is common in code ported from Linux.

Applied, thanks!

[1/2] clk: Allow clk_get_by_name() with NULL name

      commit: 2050f824e1367cb227d8e13a91c98577987fe413
[2/2] reset: Allow reset_get_by_name() with NULL name
      commit: 5a675abfe7c3f12b475cc0a6c9074a9fbe0b6bb6

Best regards,

Signature · 1 line
Sean Anderson <[email protected]>