[U-Boot] [PATCH v2 09/26] dm: core: Add a post_bind method for parents

Simon Glass sjg at chromium.org
Tue Jan 20 04:12:38 CET 2015


Allow parent drivers to be called when a new child is bound to them. This
allows a bus to set up information it needs for that child.

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

Changes in v2: None

 drivers/core/device.c | 12 ++++++++++++
 include/dm/device.h   |  2 ++
 test/dm/bus.c         | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index 8791688..aa54575 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -111,12 +111,24 @@ int device_bind(struct udevice *parent, struct driver *drv, const char *name,
 		if (ret)
 			goto fail_bind;
 	}
+	if (parent && parent->driver->child_post_bind) {
+		ret = parent->driver->child_post_bind(dev);
+		if (ret)
+			goto fail_child_post_bind;
+	}
+
 	if (parent)
 		dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
 	*devp = dev;
 
 	return 0;
 
+fail_child_post_bind:
+	if (drv->unbind && drv->unbind(dev)) {
+		dm_warn("unbind() method failed on dev '%s' on error path\n",
+			dev->name);
+	}
+
 fail_bind:
 	if (uclass_unbind_device(dev)) {
 		dm_warn("Failed to unbind dev '%s' on error path\n",
diff --git a/include/dm/device.h b/include/dm/device.h
index 096d84b..50f1b4f 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -132,6 +132,7 @@ struct udevice_id {
  * @remove: Called to remove a device, i.e. de-activate it
  * @unbind: Called to unbind a device from its driver
  * @ofdata_to_platdata: Called before probe to decode device tree data
+ * @child_post_bind: Called after a new child has been bound
  * @child_pre_probe: Called before a child device is probed. The device has
  * memory allocated but it has not yet been probed.
  * @child_post_remove: Called after a child device is removed. The device
@@ -168,6 +169,7 @@ struct driver {
 	int (*remove)(struct udevice *dev);
 	int (*unbind)(struct udevice *dev);
 	int (*ofdata_to_platdata)(struct udevice *dev);
+	int (*child_post_bind)(struct udevice *dev);
 	int (*child_pre_probe)(struct udevice *dev);
 	int (*child_post_remove)(struct udevice *dev);
 	int priv_auto_alloc_size;
diff --git a/test/dm/bus.c b/test/dm/bus.c
index 26b8293..e18a6f7 100644
--- a/test/dm/bus.c
+++ b/test/dm/bus.c
@@ -17,6 +17,7 @@ DECLARE_GLOBAL_DATA_PTR;
 
 struct dm_test_parent_platdata {
 	int count;
+	int bind_flag;
 };
 
 enum {
@@ -31,6 +32,16 @@ static int testbus_drv_probe(struct udevice *dev)
 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
 }
 
+static int testbus_child_post_bind(struct udevice *dev)
+{
+	struct dm_test_parent_platdata *plat;
+
+	plat = dev_get_parent_platdata(dev);
+	plat->bind_flag = 1;
+
+	return 0;
+}
+
 static int testbus_child_pre_probe(struct udevice *dev)
 {
 	struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
@@ -64,6 +75,7 @@ U_BOOT_DRIVER(testbus_drv) = {
 	.of_match	= testbus_ids,
 	.id	= UCLASS_TEST_BUS,
 	.probe	= testbus_drv_probe,
+	.child_post_bind = testbus_child_post_bind,
 	.priv_auto_alloc_size = sizeof(struct dm_test_priv),
 	.platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
 	.per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
@@ -380,3 +392,26 @@ static int dm_test_bus_parent_platdata_uclass(struct dm_test_state *dms)
 }
 DM_TEST(dm_test_bus_parent_platdata_uclass,
 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+/* Test that the child post_bind method is called */
+static int dm_test_bus_child_post_bind(struct dm_test_state *dms)
+{
+	struct dm_test_parent_platdata *plat;
+	struct udevice *bus, *dev;
+	int child_count;
+
+	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
+	for (device_find_first_child(bus, &dev), child_count = 0;
+	     dev;
+	     device_find_next_child(&dev)) {
+		/* Check that platform data is allocated */
+		plat = dev_get_parent_platdata(dev);
+		ut_assert(plat != NULL);
+		ut_asserteq(1, plat->bind_flag);
+		child_count++;
+	}
+	ut_asserteq(3, child_count);
+
+	return 0;
+}
+DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.2.0.rc0.207.ga3a616c



More information about the U-Boot mailing list