dm: Introduce per-child data for devices

Some device types can have child devices and want to store information
about them. For example a USB flash stick attached to a USB host
controller would likely use this space. The controller can hold
information about the USB state of each of its children.

The data is stored attached to the child device in the 'parent_priv'
member. It can be auto-allocated by dm when the child is probed. To
do this, add a per_child_auto_alloc_size value to the parent driver.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/include/dm/device.h b/include/dm/device.h
index 3f0f711..20207ce 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -51,6 +51,7 @@
  * @priv: Private data for this device
  * @uclass: Pointer to uclass for this device
  * @uclass_priv: The uclass's private data for this device
+ * @parent_priv: The parent's private data for this device
  * @uclass_node: Used by uclass to link its devices
  * @child_head: List of children of this device
  * @sibling_node: Next device in list of all devices
@@ -67,6 +68,7 @@
 	void *priv;
 	struct uclass *uclass;
 	void *uclass_priv;
+	void *parent_priv;
 	struct list_head uclass_node;
 	struct list_head child_head;
 	struct list_head sibling_node;
@@ -124,6 +126,9 @@
  * This is typically only useful for device-tree-aware drivers (those with
  * an of_match), since drivers which use platdata will have the data
  * provided in the U_BOOT_DEVICE() instantiation.
+ * @per_child_auto_alloc_size: Each device can hold private data owned by
+ * its parent. If required this will be automatically allocated if this
+ * value is non-zero.
  * @ops: Driver-specific operations. This is typically a list of function
  * pointers defined by the driver, to implement driver functions required by
  * the uclass.
@@ -140,6 +145,7 @@
 	int (*ofdata_to_platdata)(struct udevice *dev);
 	int priv_auto_alloc_size;
 	int platdata_auto_alloc_size;
+	int per_child_auto_alloc_size;
 	const void *ops;	/* driver-specific operations */
 	uint32_t flags;
 };
@@ -159,6 +165,20 @@
 void *dev_get_platdata(struct udevice *dev);
 
 /**
+ * dev_get_parentdata() - Get the parent data for a device
+ *
+ * The parent data is data stored in the device but owned by the parent.
+ * For example, a USB device may have parent data which contains information
+ * about how to talk to the device over USB.
+ *
+ * This checks that dev is not NULL, but no other checks for now
+ *
+ * @dev		Device to check
+ * @return parent data, or NULL if none
+ */
+void *dev_get_parentdata(struct udevice *dev);
+
+/**
  * dev_get_priv() - Get the private data for a device
  *
  * This checks that dev is not NULL, but no other checks for now
diff --git a/include/dm/test.h b/include/dm/test.h
index e8e1c0b..7b04850 100644
--- a/include/dm/test.h
+++ b/include/dm/test.h
@@ -82,6 +82,15 @@
 	int total_add;
 };
 
+/**
+ * struct dm_test_parent_data - parent's information on each child
+ *
+ * @sum: Test value used to check parent data works correctly
+ */
+struct dm_test_parent_data {
+	int sum;
+};
+
 /*
  * Operation counts for the test driver, used to check that each method is
  * called correctly