Module Name:    src
Committed By:   thorpej
Date:           Sat May  8 02:44:22 UTC 2021

Modified Files:
        src/sys/dev/fdt [thorpej-i2c-spi-conf]: i2cmux_fdt.c
        src/sys/dev/i2c [thorpej-i2c-spi-conf]: i2cmux.c i2cmuxvar.h
            pcai2cmux.c

Log Message:
Update the i2c mux code for the new enumeration methods.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.10.4.1 src/sys/dev/fdt/i2cmux_fdt.c
cvs rdiff -u -r1.5 -r1.5.2.1 src/sys/dev/i2c/i2cmux.c
cvs rdiff -u -r1.3 -r1.3.4.1 src/sys/dev/i2c/i2cmuxvar.h
cvs rdiff -u -r1.8 -r1.8.4.1 src/sys/dev/i2c/pcai2cmux.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/fdt/i2cmux_fdt.c
diff -u src/sys/dev/fdt/i2cmux_fdt.c:1.10 src/sys/dev/fdt/i2cmux_fdt.c:1.10.4.1
--- src/sys/dev/fdt/i2cmux_fdt.c:1.10	Wed Jan 27 03:10:21 2021
+++ src/sys/dev/fdt/i2cmux_fdt.c	Sat May  8 02:44:22 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2cmux_fdt.c,v 1.10 2021/01/27 03:10:21 thorpej Exp $	*/
+/*	$NetBSD: i2cmux_fdt.c,v 1.10.4.1 2021/05/08 02:44:22 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: i2cmux_fdt.c,v 1.10 2021/01/27 03:10:21 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2cmux_fdt.c,v 1.10.4.1 2021/05/08 02:44:22 thorpej Exp $");
 
 #include <sys/types.h>
 #include <sys/device.h>
@@ -59,11 +59,12 @@ static void *
 iicmux_gpio_get_mux_info(struct iicmux_softc * const sc)
 {
 	struct mux_info_gpio *mux_data;
+	const int phandle = devhandle_to_of(device_handle(sc->sc_dev));
 	int i;
 
 	mux_data = kmem_zalloc(sizeof(*mux_data), KM_SLEEP);
 
-	mux_data->npins = fdtbus_gpio_count(sc->sc_handle, "mux-gpios");
+	mux_data->npins = fdtbus_gpio_count(phandle, "mux-gpios");
 	if (mux_data->npins == 0) {
 		aprint_error_dev(sc->sc_dev,
 		    "unable to get mux-gpios property\n");
@@ -73,7 +74,7 @@ iicmux_gpio_get_mux_info(struct iicmux_s
 	mux_data->pins =
 	    kmem_zalloc(sizeof(*mux_data->pins) * mux_data->npins, KM_SLEEP);
 	for (i = 0; i < mux_data->npins; i++) {
-		mux_data->pins[i] = fdtbus_gpio_acquire_index(sc->sc_handle,
+		mux_data->pins[i] = fdtbus_gpio_acquire_index(phandle,
 		    "mux-gpios", i, GPIO_PIN_OUTPUT);
 		if (mux_data->pins[i] == NULL) {
 			aprint_error_dev(sc->sc_dev,
@@ -83,7 +84,7 @@ iicmux_gpio_get_mux_info(struct iicmux_s
 	}
 
 	mux_data->has_idle_value =
-	    of_getprop_uint32(sc->sc_handle, "idle-state",
+	    of_getprop_uint32(phandle, "idle-state",
 			      &mux_data->idle_value) == 0;
 
 	return mux_data;
@@ -102,12 +103,13 @@ static void *
 iicmux_gpio_get_bus_info(struct iicmux_bus * const bus)
 {
 	struct iicmux_softc * const sc = bus->mux;
+	const int phandle = devhandle_to_of(bus->devhandle);
 	struct bus_info_gpio *bus_info;
 	int error;
 
 	bus_info = kmem_zalloc(sizeof(*bus_info), KM_SLEEP);
 
-	error = fdtbus_get_reg(bus->handle, 0, &bus_info->value, NULL);
+	error = fdtbus_get_reg(phandle, 0, &bus_info->value, NULL);
 	if (error) {
 		aprint_error_dev(sc->sc_dev,
 		    "unable to get reg property for bus %d\n", bus->busidx);
@@ -172,12 +174,13 @@ struct bus_info_pinctrl {
 static void *
 iicmux_pinctrl_get_mux_info(struct iicmux_softc * const sc)
 {
+	const int phandle = devhandle_to_of(device_handle(sc->sc_dev));
 	struct mux_info_pinctrl *mux_info;
 
 	mux_info = kmem_alloc(sizeof(*mux_info), KM_SLEEP);
 
 	mux_info->has_idle_idx =
-	    fdtbus_get_index(sc->sc_handle, "pinctrl-names", "idle",
+	    fdtbus_get_index(phandle, "pinctrl-names", "idle",
 			     &mux_info->idle_idx) == 0;
 
 	return mux_info;
@@ -187,12 +190,13 @@ static void *
 iicmux_pinctrl_get_bus_info(struct iicmux_bus * const bus)
 {
 	struct iicmux_softc * const sc = bus->mux;
+	const int phandle = devhandle_to_of(bus->devhandle);
 	struct bus_info_pinctrl *bus_info;
 	int error;
 
 	bus_info = kmem_alloc(sizeof(*bus_info), KM_SLEEP);
 
-	error = fdtbus_get_reg(bus->handle, 0, &bus_info->idx, NULL);
+	error = fdtbus_get_reg(phandle, 0, &bus_info->idx, NULL);
 	if (error) {
 		aprint_error_dev(sc->sc_dev,
 		    "unable to get reg property for bus %d\n", bus->busidx);
@@ -208,9 +212,10 @@ iicmux_pinctrl_acquire_bus(struct iicmux
     int const flags __unused)
 {
 	struct iicmux_softc * const sc = bus->mux;
+	const int phandle = devhandle_to_of(device_handle(sc->sc_dev));
 	struct bus_info_pinctrl * const bus_info = bus->bus_data;
 
-	return fdtbus_pinctrl_set_config_index(sc->sc_handle, bus_info->idx);
+	return fdtbus_pinctrl_set_config_index(phandle, bus_info->idx);
 }
 
 static void
@@ -218,10 +223,11 @@ iicmux_pinctrl_release_bus(struct iicmux
     int const flags __unused)
 {
 	struct iicmux_softc * const sc = bus->mux;
+	const int phandle = devhandle_to_of(device_handle(sc->sc_dev));
 	struct mux_info_pinctrl * const mux_info = sc->sc_mux_data;
 
 	if (mux_info->has_idle_idx) {
-		(void) fdtbus_pinctrl_set_config_index(sc->sc_handle,
+		(void) fdtbus_pinctrl_set_config_index(phandle,
 		    mux_info->idle_idx);
 	}
 }
@@ -261,13 +267,13 @@ iicmux_fdt_attach(device_t const parent,
 	struct fdt_attach_args * const faa = aux;
 
 	sc->sc_dev = self;
-	sc->sc_handle = faa->faa_phandle;
-	sc->sc_config = of_compatible_lookup(sc->sc_handle, compat_data)->data;
+	sc->sc_config =
+	    of_compatible_lookup(faa->faa_phandle, compat_data)->data;
 
 	aprint_naive("\n");
 	aprint_normal(": %s I2C mux\n", sc->sc_config->desc);
 
-	sc->sc_i2c_parent = fdtbus_i2c_acquire(sc->sc_handle, "i2c-parent");
+	sc->sc_i2c_parent = fdtbus_i2c_acquire(faa->faa_phandle, "i2c-parent");
 	if (sc->sc_i2c_parent == NULL) {
 		aprint_error_dev(sc->sc_dev, "unable to acquire i2c-parent\n");
 		return;

Index: src/sys/dev/i2c/i2cmux.c
diff -u src/sys/dev/i2c/i2cmux.c:1.5 src/sys/dev/i2c/i2cmux.c:1.5.2.1
--- src/sys/dev/i2c/i2cmux.c:1.5	Sat Apr 24 23:36:54 2021
+++ src/sys/dev/i2c/i2cmux.c	Sat May  8 02:44:22 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2cmux.c,v 1.5 2021/04/24 23:36:54 thorpej Exp $	*/
+/*	$NetBSD: i2cmux.c,v 1.5.2.1 2021/05/08 02:44:22 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -29,27 +29,17 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
-#include "acpica.h"
-#endif
-
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5 2021/04/24 23:36:54 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i2cmux.c,v 1.5.2.1 2021/05/08 02:44:22 thorpej Exp $");
 
 #include <sys/types.h>
 #include <sys/device.h>
 #include <sys/kernel.h>
 #include <sys/kmem.h>
 
-#include <dev/fdt/fdtvar.h>
 #include <dev/i2c/i2cvar.h>
 #include <dev/i2c/i2cmuxvar.h>
 
-#if NACPICA > 0
-#include <dev/acpi/acpivar.h>
-#include <dev/acpi/acpi_i2c.h>
-#endif
-
 /*
  * i2c mux
  *
@@ -120,33 +110,6 @@ iicmux_exec(void * const v, i2c_op_t con
 
 /*****************************************************************************/
 
-static int
-iicmux_count_children(struct iicmux_softc * const sc)
-{
-	char name[32];
-	int child, count;
-
- restart:
-	for (child = OF_child(sc->sc_i2c_mux_phandle), count = 0; child;
-	     child = OF_peer(child)) {
-		if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
-			continue;
-		}
-		if (strcmp(name, "i2c-mux") == 0) {
-			/*
-			 * The node we encountered is the acutal parent
-			 * of the i2c bus children.  Stash its phandle
-			 * and restart the enumeration.
-			 */
-			sc->sc_i2c_mux_phandle = child;
-			goto restart;
-		}
-		count++;
-	}
-
-	return count;
-}
-
 /* XXX iicbus_print() should be able to do this. */
 static int
 iicmux_print(void * const aux, const char * const pnp)
@@ -162,15 +125,14 @@ iicmux_print(void * const aux, const cha
 }
 
 static void
-iicmux_attach_bus(struct iicmux_softc * const sc,
-    uintptr_t const handle, enum i2c_cookie_type handletype, int const busidx)
+iicmux_attach_bus(struct iicmux_softc * const sc, devhandle_t devhandle,
+    int const busidx)
 {
 	struct iicmux_bus * const bus = &sc->sc_busses[busidx];
 
 	bus->mux = sc;
 	bus->busidx = busidx;
-	bus->handle = handle;
-	bus->handletype = handletype;
+	bus->devhandle = devhandle;
 
 	bus->bus_data = sc->sc_config->get_bus_info(bus);
 	if (bus->bus_data == NULL) {
@@ -185,63 +147,89 @@ iicmux_attach_bus(struct iicmux_softc * 
 	bus->controller.ic_release_bus = iicmux_release_bus;
 	bus->controller.ic_exec = iicmux_exec;
 
-	switch (handletype) {
-	case I2C_COOKIE_OF:
+	switch (devhandle_type(devhandle)) {
+#if defined(I2CMUX_USE_FDT)
+	case DEVHANDLE_TYPE_OF:
 		fdtbus_register_i2c_controller(&bus->controller,
-		    (int)bus->handle);
+		    devhandle_to_of(devhandle));
 
-		fdtbus_attach_i2cbus(sc->sc_dev, (int)bus->handle,
+		fdtbus_attach_i2cbus(sc->sc_dev, devhandle_to_of(devhandle),
 		    &bus->controller, iicmux_print);
 		break;
-#if NACPICA > 0
-	case I2C_COOKIE_ACPI: {
-		struct acpi_devnode *ad = acpi_match_node((ACPI_HANDLE)handle);
-		KASSERT(ad != NULL);
+#endif /* I2CMUX_USE_FDT */
+
+	case DEVHANDLE_TYPE_INVALID:
+		aprint_error_dev(sc->sc_dev, "invalid bus device handle\n");
+		return;
+
+	default: {
 		struct i2cbus_attach_args iba = {
 			.iba_tag = &bus->controller,
-			.iba_child_devices = acpi_enter_i2c_devs(NULL, ad)
 		};
-		config_found(sc->sc_dev, &iba, iicbus_print, CFARG_EOL);
-	}	break;
-#endif
-	default:
-		aprint_error_dev(sc->sc_dev, "unknown handle type\n");
+		config_found(sc->sc_dev, &iba, iicmux_print,
+		    CFARG_DEVHANDLE, devhandle,
+		    CFARG_EOL);
 		break;
+	    }
 	}
 }
 
+#if defined(I2CMUX_USE_FDT)
+static int
+iicmux_fdt_count_children(struct iicmux_softc * const sc)
+{
+	int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
+	char name[32];
+	int child, count;
+
+ restart:
+	for (child = OF_child(phandle), count = 0; child;
+	     child = OF_peer(child)) {
+		if (OF_getprop(child, "name", name, sizeof(name)) <= 0) {
+			continue;
+		}
+		if (strcmp(name, "i2c-mux") == 0) {
+			phandle = child;
+			goto restart;
+		}
+		count++;
+	}
+
+	/* phandle may have changed. */
+	sc->sc_i2c_mux_devhandle = devhandle_from_of(phandle);
+	return count;
+}
+
 static void
 iicmux_attach_fdt(struct iicmux_softc * const sc)
 {
-	/*
-	 * We start out assuming that the i2c bus nodes are children of
-	 * our own node.  We'll adjust later if we encounter an "i2c-mux"
-	 * node when counting our children.  If we encounter such a node,
-	 * then it's that node that is the parent of the i2c bus children.
-	 */
-	sc->sc_i2c_mux_phandle = (int)sc->sc_handle;
+	int phandle = devhandle_to_of(sc->sc_i2c_mux_devhandle);
 
-	sc->sc_nbusses = iicmux_count_children(sc);
+	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(sc->sc_i2c_mux_phandle), idx = 0; child;
+	for (child = OF_child(phandle), idx = 0; child;
 	     child = OF_peer(child), idx++) {
 		KASSERT(idx < sc->sc_nbusses);
-		iicmux_attach_bus(sc, child, I2C_COOKIE_OF, idx);
+		iicmux_attach_bus(sc, devhandle_from_of(child), idx);
 	}
 }
+#endif /* I2CMUX_USE_FDT */
 
-#if NACPICA > 0
+#if defined(I2CMUX_USE_ACPI)
 static void
 iicmux_attach_acpi(struct iicmux_softc * const sc)
 {
-	ACPI_HANDLE hdl = (ACPI_HANDLE)sc->sc_handle;
+	ACPI_HANDLE hdl = devhandle_to_acpi(sc->sc_i2c_mux_devhandle);
 	struct acpi_devnode *devnode, *ad;
 	int idx;
 
@@ -268,25 +256,33 @@ iicmux_attach_acpi(struct iicmux_softc *
 		    !acpi_device_present(ad->ad_handle)) {
 			continue;
 		}
-		iicmux_attach_bus(sc, (uintptr_t)ad->ad_handle,
-		    I2C_COOKIE_ACPI, idx);
+		iicmux_attach_bus(sc, devhandle_from_acpi(ad->ad_handle), idx);
 		idx++;
 	}
 }
-#endif
+#endif /* I2CMUX_USE_ACPI */
 
 void
 iicmux_attach(struct iicmux_softc * const sc)
 {
+	devhandle_t devhandle = device_handle(sc->sc_dev);
+
 	/*
-	 * We expect sc->sc_handle, sc->sc_config, and sc->sc_i2c_parent
-	 * to be initialized by the front-end.
+	 * We expect sc->sc_config and sc->sc_i2c_parent to be initialized
+	 * by the front-end.
 	 */
-	KASSERT(sc->sc_handle > 0);
 	KASSERT(sc->sc_config != NULL);
 	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.
 	 */
@@ -299,18 +295,23 @@ iicmux_attach(struct iicmux_softc * cons
 	/*
 	 * Do configuration method (OF, ACPI) specific setup.
 	 */
-	switch (sc->sc_handletype) {
-	case I2C_COOKIE_OF:
+	switch (devhandle_type(devhandle)) {
+#if defined(I2CMUX_USE_FDT)
+	case DEVHANDLE_TYPE_OF:
 		iicmux_attach_fdt(sc);
 		break;
-#if NACPICA > 0
-	case I2C_COOKIE_ACPI:
+#endif /* I2CMUX_USE_FDT */
+
+#if defined(I2CMUX_USE_ACPI)
+	case DEVHANDLE_TYPE_ACPI:
 		iicmux_attach_acpi(sc);
 		break;
-#endif
+#endif /* I2CMUX_USE_ACPI */
+
 	default:
 		aprint_error_dev(sc->sc_dev, "could not configure mux: "
-		    "handle type %u not supported\n", sc->sc_handletype);
+		    "handle type %d not supported\n",
+		    devhandle_type(devhandle));
 		break;
 	}
 }

Index: src/sys/dev/i2c/i2cmuxvar.h
diff -u src/sys/dev/i2c/i2cmuxvar.h:1.3 src/sys/dev/i2c/i2cmuxvar.h:1.3.4.1
--- src/sys/dev/i2c/i2cmuxvar.h:1.3	Mon Jan 25 12:18:18 2021
+++ src/sys/dev/i2c/i2cmuxvar.h	Sat May  8 02:44:22 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: i2cmuxvar.h,v 1.3 2021/01/25 12:18:18 jmcneill Exp $	*/
+/*	$NetBSD: i2cmuxvar.h,v 1.3.4.1 2021/05/08 02:44:22 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -34,6 +34,52 @@
 
 #include <dev/i2c/i2cvar.h>
 
+/* 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
+#include "opt_fdt.h"
+
+#if defined(FDT)
+#define	I2CMUX_USE_FDT
+#endif
+
+#else /* _KERNEL_OPT */
+
+#define	I2CMUX_USE_FDT
+
+#endif /* _KERNEL_OPT */
+
+#endif /* __arm__ || __aarch64__ */
+
+/* XXX ^^^ XXX */
+
+#if defined(I2CMUX_USE_ACPI)
+#include <dev/acpi/acpivar.h>
+#endif
+
+#if defined(I2CMUX_USE_FDT)
+#include <dev/fdt/fdtvar.h>
+#endif
+
 struct iicmux_softc;
 struct iicmux_bus;
 
@@ -48,17 +94,14 @@ struct iicmux_config {
 struct iicmux_bus {
 	struct i2c_controller controller;
 	struct iicmux_softc *mux;
-	uintptr_t handle;
-	enum i2c_cookie_type handletype;
+	devhandle_t devhandle;
 	int busidx;
 	void *bus_data;
 };
 
 struct iicmux_softc {
 	device_t			sc_dev;
-	enum i2c_cookie_type		sc_handletype;
-	uintptr_t			sc_handle;
-	int				sc_i2c_mux_phandle;
+	devhandle_t			sc_i2c_mux_devhandle;
 	const struct iicmux_config *	sc_config;
 	i2c_tag_t			sc_i2c_parent;
 	struct iicmux_bus *		sc_busses;

Index: src/sys/dev/i2c/pcai2cmux.c
diff -u src/sys/dev/i2c/pcai2cmux.c:1.8 src/sys/dev/i2c/pcai2cmux.c:1.8.4.1
--- src/sys/dev/i2c/pcai2cmux.c:1.8	Wed Jan 27 02:29:48 2021
+++ src/sys/dev/i2c/pcai2cmux.c	Sat May  8 02:44:22 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: pcai2cmux.c,v 1.8 2021/01/27 02:29:48 thorpej Exp $	*/
+/*	$NetBSD: pcai2cmux.c,v 1.8.4.1 2021/05/08 02:44:22 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -29,12 +29,8 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
-#include "acpica.h"
-#endif
-
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,v 1.8 2021/01/27 02:29:48 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,v 1.8.4.1 2021/05/08 02:44:22 thorpej Exp $");
 
 /*
  * Driver for NXP PCA954x / PCA984x I2C switches and multiplexers.
@@ -63,10 +59,6 @@ __KERNEL_RCSID(0, "$NetBSD: pcai2cmux.c,
 #include <dev/fdt/fdtvar.h>
 #include <dev/i2c/i2cmuxvar.h>
 
-#if NACPICA > 0
-#include <dev/acpi/acpivar.h>
-#endif
-
 /* There are a maximum of 8 busses supported. */
 #define	PCAIICMUX_MAX_BUSSES	8
 
@@ -166,7 +158,9 @@ struct pcaiicmux_softc {
 	int			sc_cur_value;
 
 	const struct pcaiicmux_type *sc_type;
+#if defined(I2CMUX_USE_FDT)
 	struct fdtbus_gpio_pin *sc_reset_gpio;
+#endif /* I2CMUX_USE_FDT */
 
 	bool			sc_idle_disconnect;
 
@@ -207,6 +201,7 @@ pcaiicmux_get_bus_info(struct iicmux_bus
 {
 	struct iicmux_softc * const iicmux = bus->mux;
 	struct pcaiicmux_softc * const sc = iicmux->sc_mux_data;
+	devhandle_t devhandle = bus->devhandle;
 	bus_addr_t addr;
 	int error;
 
@@ -220,9 +215,11 @@ pcaiicmux_get_bus_info(struct iicmux_bus
 	struct pcaiicmux_bus_info * const bus_info =
 	    &sc->sc_bus_info[bus->busidx];
 
-	switch (bus->handletype) {
-	case I2C_COOKIE_OF:
-		error = fdtbus_get_reg(bus->handle, 0, &addr, NULL);
+	switch (devhandle_type(devhandle)) {
+#if defined(I2CMUX_USE_FDT)
+	case DEVHANDLE_TYPE_OF:
+		error = fdtbus_get_reg(devhandle_to_of(devhandle),
+		    0, &addr, NULL);
 		if (error) {
 			aprint_error_dev(iicmux->sc_dev,
 			    "unable to get reg property for bus %d\n",
@@ -230,11 +227,14 @@ pcaiicmux_get_bus_info(struct iicmux_bus
 			return NULL;
 		}
 		break;
-#if NACPICA > 0
-	case I2C_COOKIE_ACPI: {
+#endif /* I2CMUX_USE_FDT */
+
+#if defined(I2CMUX_USE_ACPI)
+	case DEVHANDLE_TYPE_ACPI: {
 		ACPI_INTEGER val;
 		ACPI_STATUS rv;
-		rv = acpi_eval_integer((ACPI_HANDLE)bus->handle, "_ADR", &val);
+		rv = acpi_eval_integer(devhandle_to_acpi(devhandle),
+		    "_ADR", &val);
 		if (ACPI_FAILURE(rv)) {
 			aprint_error_dev(iicmux->sc_dev,
 			    "unable to evaluate _ADR for bus %d: %s\n",
@@ -243,7 +243,8 @@ pcaiicmux_get_bus_info(struct iicmux_bus
 		}
 		addr = (bus_addr_t)val;
 	}	break;
-#endif
+#endif /* I2CMUX_USE_ACPI */
+
 	default:
 		aprint_error_dev(iicmux->sc_dev, "unsupported handle type\n");
 		return NULL;
@@ -334,11 +335,12 @@ pcaiicmux_attach(device_t parent, device
 {
 	struct pcaiicmux_softc * const sc = device_private(self);
 	struct i2c_attach_args * const ia = aux;
+	devhandle_t devhandle = ia->ia_devhandle;
 	int error;
 
+	device_set_handle(self, devhandle);
+
 	sc->sc_iicmux.sc_dev = self;
-	sc->sc_iicmux.sc_handle = ia->ia_cookie;
-	sc->sc_iicmux.sc_handletype = ia->ia_cookietype;
 	sc->sc_iicmux.sc_config = &pcaiicmux_config;
 	sc->sc_iicmux.sc_i2c_parent = ia->ia_tag;
 	sc->sc_addr = ia->ia_addr;
@@ -350,8 +352,9 @@ pcaiicmux_attach(device_t parent, device
 	aprint_normal(": PCA954x I2C %s\n",
 	    sc->sc_type->enable_bit ? "mux" : "switch");
 
-	if (ia->ia_cookietype == I2C_COOKIE_OF) {
-		const int phandle = (int)ia->ia_cookie;
+#if defined(I2CMUX_USE_FDT)
+	if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
+		const int phandle = devhandle_to_of(devhandle);
 		if (of_hasprop(phandle, "i2c-mux-idle-disconnect")) {
 			sc->sc_idle_disconnect = true;
 		}
@@ -366,6 +369,7 @@ pcaiicmux_attach(device_t parent, device
 			delay(10);
 		}
 	}
+#endif /* I2CMUX_USE_FDT */
 
 	/* Force the mux into a disconnected state. */
 	sc->sc_cur_value = -1;

Reply via email to