]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
w1: replace dt detection by automatic detection
authorKory Maincent <kory.maincent@bootlin.com>
Tue, 4 May 2021 17:31:26 +0000 (19:31 +0200)
committerTom Rini <trini@konsulko.com>
Thu, 13 May 2021 17:09:09 +0000 (13:09 -0400)
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 driver matching the family
id of the w1 detected.

The patch is inspired from Maxime Ripard code.

Signed-off-by: Kory Maincent <kory.maincent@bootlin.com>
Reviewed-by: Maxime Ripard <maxime@cerno.tech>
drivers/w1-eeprom/ds24xxx.c
drivers/w1-eeprom/ds2502.c
drivers/w1-eeprom/w1-eeprom-uclass.c
drivers/w1/w1-uclass.c
include/w1-eeprom.h
include/w1.h

index d12fd5754e39996b68433dd48876c345c468b493..4be378b43d03540342cf1e0342b18ecac5abc7e5 100644 (file)
@@ -53,3 +53,10 @@ U_BOOT_DRIVER(ds24xxx) = {
        .ops            = &ds24xxx_ops,
        .probe          = ds24xxx_probe,
 };
+
+u8 family_supported[] = {
+       W1_FAMILY_DS24B33,
+       W1_FAMILY_DS2431,
+};
+
+U_BOOT_W1_DEVICE(ds24xxx, family_supported);
index b3d68d7f058f85cecdbdd7d487f990d91b3f4ed2..a67f5edd0febb010c4315a55eab9eb28fe5831fd 100644 (file)
@@ -243,3 +243,9 @@ U_BOOT_DRIVER(ds2502) = {
        .ops            = &ds2502_ops,
        .probe          = ds2502_probe,
 };
+
+u8 family_supported[] = {
+       W1_FAMILY_DS2502,
+};
+
+U_BOOT_W1_DEVICE(ds2502, family_supported);
index 97a9d43b03a4164a42656cfec64f0ec57bcd7bc7..7a02af3dd6d35f6f743b4749fe84a71cc51927c7 100644 (file)
@@ -37,37 +37,6 @@ 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)
-{
-       u8 family = id & 0xff;
-       int 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");
-                       return ret;
-               }
-               if (dev_get_driver_data(dev) == family) {
-                       struct w1_device *w1;
-
-                       w1 = dev_get_parent_plat(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: No matches found: error %d\n", __func__, ret);
-
-       return ret;
-}
-
 int w1_eeprom_get_id(struct udevice *dev, u64 *id)
 {
        struct w1_device *w1 = dev_get_parent_plat(dev);
index 8bc6cb13f49c3a82a1c65afc88e997f5b61b0ffc..b98927389f39b58fd69f354174a442d4f4285e0a 100644 (file)
@@ -4,9 +4,11 @@
  * Copyright (c) 2015 Free Electrons
  * Copyright (c) 2015 NextThing Co.
  * Copyright (c) 2018 Microchip Technology, Inc.
+ * Copyright (c) 2021 Bootlin
  *
  * Maxime Ripard <maxime.ripard@free-electrons.com>
  * Eugen Hristev <eugen.hristev@microchip.com>
+ * Kory Maincent <kory.maincent@bootlin.com>
  *
  */
 
@@ -26,6 +28,76 @@ struct w1_bus {
        u64     search_id;
 };
 
+int w1_bus_find_dev(const struct udevice *bus, u64 id, struct udevice
+**devp)
+{
+       struct udevice *dev;
+       u8 family = id & 0xff;
+       int ret;
+
+       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");
+                       return -ENODEV;
+               }
+
+               if (dev_get_driver_data(dev) == family) {
+                       *devp = dev;
+                       return 0;
+               }
+       }
+
+       return -ENODEV;
+}
+
+int w1_register_new_device(u64 id, struct udevice *bus)
+{
+       u8 family = id & 0xff;
+       int n_ents, ret = 0;
+       struct udevice *dev;
+
+       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 u8 *match_family;
+               const struct driver *drv;
+               struct w1_device *w1;
+
+               for (match_family = entry->family; match_family;
+                    match_family++) {
+                       if (*match_family != family)
+                               continue;
+
+                       ret = w1_bus_find_dev(bus, id, &dev);
+
+                       /* If nothing in the device tree, bind a device */
+                       if (ret == -ENODEV) {
+                               drv = entry->driver;
+                               ret = device_bind(bus, drv, drv->name,
+                                                 NULL, ofnode_null(), &dev);
+                               if (ret)
+                                       return ret;
+                       }
+
+                       device_probe(dev);
+
+                       w1 = dev_get_parent_plat(dev);
+                       w1->id = id;
+
+                       return 0;
+               }
+       }
+
+       debug("%s: No matches found: error %d\n", __func__, ret);
+
+       return ret;
+}
+
 static int w1_enumerate(struct udevice *bus)
 {
        const struct w1_ops *ops = device_get_ops(bus);
@@ -97,8 +169,8 @@ static int w1_enumerate(struct udevice *bus)
                        debug("%s: Detected new device 0x%llx (family 0x%x)\n",
                              bus->name, rn, (u8)(rn & 0xff));
 
-                       /* attempt to register as w1-eeprom device */
-                       w1_eeprom_register_new_device(rn);
+                       /* attempt to register as w1 device */
+                       w1_register_new_device(rn, bus);
                }
        }
 
index 22337368b4b556bf67be975315798699bfd112cd..b3cf77a81ef670f261878483850490eea2d6d52a 100644 (file)
@@ -27,7 +27,5 @@ 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_get_id(struct udevice *dev, u64 *id);
 #endif
index 77f439e587f76e1e13e35311c91b7b508d5d141e..b18078ba152ddc1b479b17a6a7e186fa9b6c61d2 100644 (file)
@@ -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, __family)                             \
+       ll_entry_declare(struct w1_driver_entry, __name, w1_driver_entry) = { \
+               .driver = llsym(struct driver, __name, driver),         \
+               .family = __family,                                     \
+       }
+
 struct w1_device {
        u64     id;
 };