dm: core: Add a new flag to track platform data

We want to avoid allocating platform data twice. This could happen if
device_probe() is called after device_ofdata_to_platdata() for the same
device.

Add a flag to track whether device_ofdata_to_platdata() has been called on
a device. Check the flag to make sure it doesn't happen twice, and clear
the flag when the data is freed.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c
index 5c8dc4a..444e34b 100644
--- a/drivers/core/device-remove.c
+++ b/drivers/core/device-remove.c
@@ -140,6 +140,7 @@
 			dev->parent_priv = NULL;
 		}
 	}
+	dev->flags &= ~DM_FLAG_PLATDATA_VALID;
 
 	devres_release_probe(dev);
 }
diff --git a/drivers/core/device.c b/drivers/core/device.c
index 9506c7d..9f39218 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -320,7 +320,7 @@
 	if (!dev)
 		return -EINVAL;
 
-	if (dev->flags & DM_FLAG_ACTIVATED)
+	if (dev->flags & DM_FLAG_PLATDATA_VALID)
 		return 0;
 
 	drv = dev->driver;
@@ -368,6 +368,8 @@
 			goto fail;
 	}
 
+	dev->flags |= DM_FLAG_PLATDATA_VALID;
+
 	return 0;
 fail:
 	device_free(dev);
diff --git a/include/dm/device.h b/include/dm/device.h
index 2177470..9ebfd0a 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -65,6 +65,9 @@
 /* DM does not enable/disable the power domains corresponding to this device */
 #define DM_FLAG_DEFAULT_PD_CTRL_OFF	(1 << 11)
 
+/* Driver platdata has been read. Cleared when the device is removed */
+#define DM_FLAG_PLATDATA_VALID		(1 << 12)
+
 /*
  * One or multiple of these flags are passed to device_remove() so that
  * a selective device removal as specified by the remove-stage and the