[PATCH v2 06/12] w1: replace dt detection by automatic detection

Kory Maincent kory.maincent at bootlin.com
Tue Mar 2 10:58:07 CET 2021


This patch changes the functioning of the detection of w1 devices.
The old way was a comparison between detected w1 and the ones described in
the device tree. Now it will just look for the right driver matching the
family id of the w1 detected.

The patch is inspired from Maxime Ripart code.

Signed-off-by: Kory Maincent <kory.maincent at bootlin.com>
---
 drivers/w1-eeprom/ds24xxx.c          | 20 ++-----------
 drivers/w1-eeprom/ds2502.c           | 18 ++---------
 drivers/w1-eeprom/w1-eeprom-uclass.c | 45 ++++++++++++++++------------
 drivers/w1/w1-uclass.c               |  2 +-
 include/w1-eeprom.h                  |  2 +-
 include/w1.h                         | 17 +++++++++++
 6 files changed, 50 insertions(+), 54 deletions(-)

diff --git a/drivers/w1-eeprom/ds24xxx.c b/drivers/w1-eeprom/ds24xxx.c
index 0967accdf0..7c245fbd71 100644
--- a/drivers/w1-eeprom/ds24xxx.c
+++ b/drivers/w1-eeprom/ds24xxx.c
@@ -27,29 +27,15 @@ static int ds24xxx_read_buf(struct udevice *dev, unsigned int offset,
 	return w1_read_buf(dev, buf, count);
 }
 
-static int ds24xxx_probe(struct udevice *dev)
-{
-	struct w1_device *w1;
-
-	w1 = dev_get_parent_platdata(dev);
-	w1->id = 0;
-	return 0;
-}
-
 static const struct w1_eeprom_ops ds24xxx_ops = {
 	.read_buf	= ds24xxx_read_buf,
 };
 
-static const struct udevice_id ds24xxx_id[] = {
-	{ .compatible = "maxim,ds24b33", .data = W1_FAMILY_DS24B33 },
-	{ .compatible = "maxim,ds2431", .data = W1_FAMILY_DS2431 },
-	{ },
-};
-
 U_BOOT_DRIVER(ds24xxx) = {
 	.name		= "ds24xxx",
 	.id		= UCLASS_W1_EEPROM,
-	.of_match	= ds24xxx_id,
 	.ops		= &ds24xxx_ops,
-	.probe		= ds24xxx_probe,
 };
+
+U_BOOT_W1_DEVICE(ds24b33, ds24xxx, W1_FAMILY_DS24B33);
+U_BOOT_W1_DEVICE(ds2431, ds24xxx, W1_FAMILY_DS2431);
diff --git a/drivers/w1-eeprom/ds2502.c b/drivers/w1-eeprom/ds2502.c
index 19ee4b17ea..73e1cc023a 100644
--- a/drivers/w1-eeprom/ds2502.c
+++ b/drivers/w1-eeprom/ds2502.c
@@ -218,28 +218,14 @@ static int ds2502_read_buf(struct udevice *dev, unsigned int offset,
 	return 0;
 }
 
-static int ds2502_probe(struct udevice *dev)
-{
-	struct w1_device *w1;
-
-	w1 = dev_get_parent_platdata(dev);
-	w1->id = 0;
-	return 0;
-}
-
 static const struct w1_eeprom_ops ds2502_ops = {
 	.read_buf	= ds2502_read_buf,
 };
 
-static const struct udevice_id ds2502_id[] = {
-	{ .compatible = "maxim,ds2502", .data = W1_FAMILY_DS2502 },
-	{ },
-};
-
 U_BOOT_DRIVER(ds2502) = {
 	.name		= "ds2502",
 	.id		= UCLASS_W1_EEPROM,
-	.of_match	= ds2502_id,
 	.ops		= &ds2502_ops,
-	.probe		= ds2502_probe,
 };
+
+U_BOOT_W1_DEVICE(ds2502, ds2502, W1_FAMILY_DS2502);
diff --git a/drivers/w1-eeprom/w1-eeprom-uclass.c b/drivers/w1-eeprom/w1-eeprom-uclass.c
index d515b4c536..9edce7f6c5 100644
--- a/drivers/w1-eeprom/w1-eeprom-uclass.c
+++ b/drivers/w1-eeprom/w1-eeprom-uclass.c
@@ -37,30 +37,37 @@ int w1_eeprom_read_buf(struct udevice *dev, unsigned int offset,
 	return ops->read_buf(dev, offset, buf, count);
 }
 
-int w1_eeprom_register_new_device(u64 id)
+int w1_eeprom_register_new_device(u64 id, struct udevice *bus)
 {
 	u8 family = id & 0xff;
-	int ret;
+	int n_ents, ret;
 	struct udevice *dev;
 
-	for (ret = uclass_first_device(UCLASS_W1_EEPROM, &dev);
-	     !ret && dev;
-	     uclass_next_device(&dev)) {
-		if (ret || !dev) {
-			debug("cannot find w1 eeprom dev\n");
+	struct w1_driver_entry *start, *entry;
+
+	start = ll_entry_start(struct w1_driver_entry, w1_driver_entry);
+	n_ents = ll_entry_count(struct w1_driver_entry, w1_driver_entry);
+
+	for (entry = start; entry != start + n_ents; entry++) {
+		const struct driver *drv;
+		struct w1_device *w1;
+		struct udevice *dev;
+
+		if (entry->family != family)
+			continue;
+
+		drv = entry->driver;
+		ret = device_bind(bus, drv, drv->name, NULL, -1,
+				  &dev);
+		if (ret)
 			return ret;
-		}
-		if (dev_get_driver_data(dev) == family) {
-			struct w1_device *w1;
-
-			w1 = dev_get_parent_platdata(dev);
-			if (w1->id) /* device already in use */
-				continue;
-			w1->id = id;
-			debug("%s: Match found: %s:%s %llx\n", __func__,
-			      dev->name, dev->driver->name, id);
-			return 0;
-		}
+
+		debug("%s: Match found: %s\n", __func__, drv->name);
+
+		w1 = dev_get_parent_platdata(dev);
+		w1->id = id;
+
+		return 0;
 	}
 
 	debug("%s: No matches found: error %d\n", __func__, ret);
diff --git a/drivers/w1/w1-uclass.c b/drivers/w1/w1-uclass.c
index 9478c93e9b..004d1098c8 100644
--- a/drivers/w1/w1-uclass.c
+++ b/drivers/w1/w1-uclass.c
@@ -98,7 +98,7 @@ static int w1_enumerate(struct udevice *bus)
 			      bus->name, rn, (u8)(rn & 0xff));
 
 			/* attempt to register as w1-eeprom device */
-			w1_eeprom_register_new_device(rn);
+			w1_eeprom_register_new_device(rn, bus);
 		}
 	}
 
diff --git a/include/w1-eeprom.h b/include/w1-eeprom.h
index 22337368b4..6d6a088e1b 100644
--- a/include/w1-eeprom.h
+++ b/include/w1-eeprom.h
@@ -27,7 +27,7 @@ int w1_eeprom_read_buf(struct udevice *dev, unsigned int offset,
 
 int w1_eeprom_dm_init(void);
 
-int w1_eeprom_register_new_device(u64 id);
+int w1_eeprom_register_new_device(u64 id, struct udevice *bus);
 
 int w1_eeprom_get_id(struct udevice *dev, u64 *id);
 #endif
diff --git a/include/w1.h b/include/w1.h
index 77f439e587..11506a6c9f 100644
--- a/include/w1.h
+++ b/include/w1.h
@@ -15,6 +15,23 @@ struct udevice;
 #define W1_FAMILY_DS2502	0x09
 #define W1_FAMILY_EEP_SANDBOX	0xfe
 
+struct w1_driver_entry {
+	struct driver	*driver;
+	u8		family;
+};
+
+/* U_BOOT_W1_DEVICE() tells U-Boot to create a one-wire device.
+ *
+ * @__name: Device name (C identifier, not a string. E.g. gpio7_at_ff7e0000)
+ * @__driver: Driver name (C identifier, not a string. E.g. gpio7_at_ff7e0000)
+ * @__family: Family code number of the one-wire
+ */
+#define U_BOOT_W1_DEVICE(__name, __driver, __family)				\
+	ll_entry_declare(struct w1_driver_entry, __name, w1_driver_entry) = { \
+		.driver = llsym(struct driver, __driver, driver),		\
+		.family = __family,					\
+	}
+
 struct w1_device {
 	u64	id;
 };
-- 
2.17.1



More information about the U-Boot mailing list