Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/thorpej-i2c-spi-conf]: src/sys/dev/i2c Use device_enumerate_children() t...



details:   https://anonhg.NetBSD.org/src/rev/8dfd5627444d
branches:  thorpej-i2c-spi-conf
changeset: 1020787:8dfd5627444d
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sat May 08 15:10:44 2021 +0000

description:
Use device_enumerate_children() to eliminate all of the ACPI-specific
code and most of the FDT-specific code from the iicmux driver.

diffstat:

 sys/dev/i2c/i2cmux.c    |  165 ++++++++++++++++++-----------------------------
 sys/dev/i2c/i2cmuxvar.h |   24 +------
 2 files changed, 64 insertions(+), 125 deletions(-)

diffs (271 lines):

diff -r 43ee7d28c91a -r 8dfd5627444d sys/dev/i2c/i2cmux.c
--- a/sys/dev/i2c/i2cmux.c      Sat May 08 14:23:15 2021 +0000
+++ b/sys/dev/i2c/i2cmux.c      Sat May 08 15:10:44 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: i2cmux.c,v 1.5.2.2 2021/05/08 14:23:15 thorpej Exp $   */
+/*     $NetBSD: i2cmux.c,v 1.5.2.3 2021/05/08 15:10:44 thorpej Exp $   */
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5.2.2 2021/05/08 14:23:15 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5.2.3 2021/05/08 15:10:44 thorpej Exp $");
 
 #include <sys/types.h>
 #include <sys/device.h>
@@ -157,98 +157,53 @@
            CFARG_EOL);
 }
 
-#if defined(I2CMUX_USE_FDT)
-static int
-iicmux_fdt_count_children(struct iicmux_softc * const sc)
+static bool
+iicmux_count_busses_callback(device_t self, devhandle_t devhandle,
+    void *v __unused)
 {
-       int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
-       char name[32];
-       int child, count;
+       struct iicmux_softc *sc = device_private(self);
 
- restart:
-       for (child = OF_child(phandle), count = 0; child;
-            child = OF_peer(child)) {
-               if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
-                       continue;
+#if defined(I2CMUX_USE_FDT)
+       if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
+               char name[32];
+
+               if (OF_getprop(devhandle_to_of(devhandle), "name", name,
+                              sizeof(name)) <= 0) {
+                       /* Skip this DT node (shouldn't happen). */
+                       return true;    /* keep enumerating */
                }
                if (strcmp(name, "i2c-mux") == 0) {
-                       phandle = child;
-                       goto restart;
+                       /*
+                        * This DT node is the actual mux node; reset the
+                        * our devhandle and restart enumeration.
+                        */
+                       device_set_handle(self, devhandle);
+                       sc->sc_nbusses = -1;
+                       return false;   /* stop enumerating */
                }
-               count++;
        }
+#endif /* I2CMUX_USE_FDT */
 
-       /* phandle may have changed. */
-       sc->sc_i2c_mux_devhandle = devhandle_from_of(phandle);
-       return count;
+       sc->sc_nbusses++;
+       return true;                    /* keep enumerating */
 }
 
-static void
-iicmux_attach_fdt(struct iicmux_softc * const sc)
-{
-       int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
-
-       sc->sc_nbusses = iicmux_fdt_count_children(sc);
-       if (sc->sc_nbusses == 0) {
-               return;
-       }
-
-       /* sc_i2c_mux_devhandle may have changed. */
-       phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
-
-       sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
-           KM_SLEEP);
-
-       int child, idx;
-       for (child = OF_child(phandle), idx = 0; child;
-            child = OF_peer(child), idx++) {
-               KASSERT(idx < sc->sc_nbusses);
-               iicmux_attach_bus(sc, devhandle_from_of(child), idx);
-       }
-}
-#endif /* I2CMUX_USE_FDT */
-
-#if defined(I2CMUX_USE_ACPI)
-static void
-iicmux_attach_acpi(struct iicmux_softc * const sc)
+static bool
+iicmux_attach_busses_callback(device_t self, devhandle_t devhandle, void *v)
 {
-       ACPI_HANDLE hdl = devhandle_to_acpi(sc->sc_i2c_mux_devhandle);
-       struct acpi_devnode *devnode, *ad;
-       int idx;
-
-       devnode = acpi_match_node(hdl);
-       KASSERT(devnode != NULL);
+       struct iicmux_softc *sc = device_private(self);
+       int * const idxp = v;
 
-       /* Count child busses */
-       sc->sc_nbusses = 0;
-       SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
-               if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
-                   !acpi_device_present(ad->ad_handle)) {
-                       continue;
-               }
-               sc->sc_nbusses++;
-       }
-
-       sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
-           KM_SLEEP);
+       KASSERT(*idxp < sc->sc_nbusses);
+       iicmux_attach_bus(sc, devhandle, (*idxp)++);
 
-       /* Attach child busses */
-       idx = 0;
-       SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
-               if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE ||
-                   !acpi_device_present(ad->ad_handle)) {
-                       continue;
-               }
-               iicmux_attach_bus(sc, devhandle_from_acpi(ad->ad_handle), idx);
-               idx++;
-       }
+       return true;                    /* keep enumerating */
 }
-#endif /* I2CMUX_USE_ACPI */
 
 void
 iicmux_attach(struct iicmux_softc * const sc)
 {
-       devhandle_t devhandle = device_handle(sc->sc_dev);
+       int error, idx;
 
        /*
         * We expect sc->sc_config and sc->sc_i2c_parent to be initialized
@@ -258,14 +213,6 @@
        KASSERT(sc->sc_i2c_parent != NULL);
 
        /*
-        * We start out assuming that the i2c bus nodes are children of
-        * our own node.  We'll adjust later if we encounter a subnode
-        * while enumerating our child busses that itself is defined as
-        * the mux node according to our device tree bindings.
-        */
-       sc->sc_i2c_mux_devhandle = devhandle;
-
-       /*
         * Gather up all of the various bits of information needed
         * for this particular type of i2c mux.
         */
@@ -276,25 +223,39 @@
        }
 
        /*
-        * Do configuration method (OF, ACPI) specific setup.
+        * Count the number of busses behind this mux.
         */
-       switch (devhandle_type(devhandle)) {
-#if defined(I2CMUX_USE_FDT)
-       case DEVHANDLE_TYPE_OF:
-               iicmux_attach_fdt(sc);
-               break;
-#endif /* I2CMUX_USE_FDT */
+ count_again:
+       error = device_enumerate_children(sc->sc_dev,
+           iicmux_count_busses_callback, NULL);
+       if (error) {
+               aprint_error_dev(sc->sc_dev,
+                   "unable to enumerate busses to count, error = %d\n", error);
+               return;
+       }
+       if (sc->sc_nbusses == -1) {
+               /*
+                * We had to reset our devhandle to a different device
+                * tree node.  We need to try counting again.
+                */
+               sc->sc_nbusses = 0;
+               goto count_again;
+       }
+       if (sc->sc_nbusses == 0) {
+               /* No busses; no more work to do. */
+               return;
+       }
 
-#if defined(I2CMUX_USE_ACPI)
-       case DEVHANDLE_TYPE_ACPI:
-               iicmux_attach_acpi(sc);
-               break;
-#endif /* I2CMUX_USE_ACPI */
+       sc->sc_busses = kmem_zalloc(sizeof(*sc->sc_busses) * sc->sc_nbusses,
+           KM_SLEEP);
 
-       default:
-               aprint_error_dev(sc->sc_dev, "could not configure mux: "
-                   "handle type %d not supported\n",
-                   devhandle_type(devhandle));
-               break;
+       /* Now attach them. */
+       idx = 0;
+       error = device_enumerate_children(sc->sc_dev,
+           iicmux_attach_busses_callback, &idx);
+       if (error) {
+               aprint_error_dev(sc->sc_dev,
+                   "unable to enumerate busses to attach, error = %d\n",
+                   error);
        }
 }
diff -r 43ee7d28c91a -r 8dfd5627444d sys/dev/i2c/i2cmuxvar.h
--- a/sys/dev/i2c/i2cmuxvar.h   Sat May 08 14:23:15 2021 +0000
+++ b/sys/dev/i2c/i2cmuxvar.h   Sat May 08 15:10:44 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: i2cmuxvar.h,v 1.3.4.1 2021/05/08 02:44:22 thorpej Exp $        */
+/*     $NetBSD: i2cmuxvar.h,v 1.3.4.2 2021/05/08 15:10:44 thorpej Exp $        */
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -36,23 +36,6 @@
 
 /* XXX This is not ideal, but... XXX */
 
-#if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
-
-#ifdef _KERNEL_OPT
-#include "acpica.h"
-
-#if NACPICA > 0
-#define        I2CMUX_USE_ACPI
-#endif
-
-#else /* ! _KERNEL_OPT */
-
-#define        I2CMUX_USE_ACPI
-
-#endif /* _KERNEL_OPT */
-
-#endif /* __i386__ || __amd64__ || __aarch64__ */
-
 #if defined(__arm__) || defined(__aarch64__)
 
 #ifdef _KERNEL_OPT
@@ -72,10 +55,6 @@
 
 /* XXX ^^^ XXX */
 
-#if defined(I2CMUX_USE_ACPI)
-#include <dev/acpi/acpivar.h>
-#endif
-
 #if defined(I2CMUX_USE_FDT)
 #include <dev/fdt/fdtvar.h>
 #endif
@@ -101,7 +80,6 @@
 
 struct iicmux_softc {
        device_t                        sc_dev;
-       devhandle_t                     sc_i2c_mux_devhandle;
        const struct iicmux_config *    sc_config;
        i2c_tag_t                       sc_i2c_parent;
        struct iicmux_bus *             sc_busses;



Home | Main Index | Thread Index | Old Index