diff mbox

[U-Boot,1/4] dm: clk: Add a way to find a clock by its driver

Message ID 1468790598-21133-1-git-send-email-sjg@chromium.org
State Accepted
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass July 17, 2016, 9:23 p.m. UTC
Some SoCs have a single clock device. Provide a way to find it given its
driver name. This is handled by the linker so will fail if the name is not
found, avoiding strange errors when names change and do not match. It is
also faster than a string comparison.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/core/uclass.c | 20 ++++++++++++++++++++
 include/dm/device.h   |  4 ++++
 include/dm/uclass.h   | 17 +++++++++++++++++
 3 files changed, 41 insertions(+)

Comments

Simon Glass July 18, 2016, 12:16 p.m. UTC | #1
On 17 July 2016 at 15:23, Simon Glass <sjg@chromium.org> wrote:
> Some SoCs have a single clock device. Provide a way to find it given its
> driver name. This is handled by the linker so will fail if the name is not
> found, avoiding strange errors when names change and do not match. It is
> also faster than a string comparison.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  drivers/core/uclass.c | 20 ++++++++++++++++++++
>  include/dm/device.h   |  4 ++++
>  include/dm/uclass.h   | 17 +++++++++++++++++
>  3 files changed, 41 insertions(+)

Applied to u-boot-rockchip.
Stephen Warren July 18, 2016, 4:23 p.m. UTC | #2
On 07/17/2016 03:23 PM, Simon Glass wrote:
> Some SoCs have a single clock device. Provide a way to find it given its
> driver name. This is handled by the linker so will fail if the name is not
> found, avoiding strange errors when names change and do not match. It is
> also faster than a string comparison.

The code looks plausible, but the commit subject and description imply 
this has something to do with clocks, whereas it doesn't; it seems to be 
a generic/core DM function.
Simon Glass Aug. 1, 2016, 1:01 a.m. UTC | #3
Hi Stephen,

On 18 July 2016 at 10:23, Stephen Warren <swarren@wwwdotorg.org> wrote:
> On 07/17/2016 03:23 PM, Simon Glass wrote:
>>
>> Some SoCs have a single clock device. Provide a way to find it given its
>> driver name. This is handled by the linker so will fail if the name is not
>> found, avoiding strange errors when names change and do not match. It is
>> also faster than a string comparison.
>
>
> The code looks plausible, but the commit subject and description imply this
> has something to do with clocks, whereas it doesn't; it seems to be a
> generic/core DM function.

Yes you are correct. I updated the commit subject when I applied it:

c57f806 dm: core: Add a way to find a device by its driver

Regards,
Simon
diff mbox

Patch

diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index 1141ce1..de602ae 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -311,6 +311,26 @@  static int uclass_find_device_by_phandle(enum uclass_id id,
 }
 #endif
 
+int uclass_get_device_by_driver(enum uclass_id id,
+				const struct driver *find_drv,
+				struct udevice **devp)
+{
+	struct udevice *dev;
+	struct uclass *uc;
+	int ret;
+
+	ret = uclass_get(id, &uc);
+	if (ret)
+		return ret;
+
+	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
+		if (dev->driver == find_drv)
+			return uclass_get_device_tail(dev, 0, devp);
+	}
+
+	return -ENODEV;
+}
+
 int uclass_get_device_tail(struct udevice *dev, int ret,
 				  struct udevice **devp)
 {
diff --git a/include/dm/device.h b/include/dm/device.h
index c825d47..705849b 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -207,6 +207,10 @@  struct driver {
 #define U_BOOT_DRIVER(__name)						\
 	ll_entry_declare(struct driver, __name, driver)
 
+/* Get a pointer to a given driver */
+#define DM_GET_DRIVER(__name)						\
+	ll_entry_get(struct driver, __name, driver)
+
 /**
  * dev_get_platdata() - Get the platform data for a device
  *
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index fd368b6..e49fbed 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -194,6 +194,23 @@  int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
 				 const char *name, struct udevice **devp);
 
 /**
+ * uclass_get_device_by_driver() - Get a uclass device for a driver
+ *
+ * This searches the devices in the uclass for one that uses the given
+ * driver. Use DM_GET_DRIVER(name) for the @drv argument, where 'name' is
+ * the driver name - as used in U_BOOT_DRIVER(name).
+ *
+ * The device is probed to activate it ready for use.
+ *
+ * @id: ID to look up
+ * @drv: Driver to look for
+ * @devp: Returns pointer to the first device with that driver
+ * @return 0 if OK, -ve on error
+ */
+int uclass_get_device_by_driver(enum uclass_id id, const struct driver *drv,
+				struct udevice **devp);
+
+/**
  * uclass_first_device() - Get the first device in a uclass
  *
  * The device returned is probed if necessary, and ready for use