Module Name: src Committed By: jmcneill Date: Sun Aug 13 18:27:11 UTC 2017
Modified Files: src/sys/dev/fdt: fdt_gpio.c fdtvar.h Log Message: Add fdtbus_gpio_acquire_index for accessing multi-xref gpios properties. To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 src/sys/dev/fdt/fdt_gpio.c cvs rdiff -u -r1.24 -r1.25 src/sys/dev/fdt/fdtvar.h 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/fdt_gpio.c diff -u src/sys/dev/fdt/fdt_gpio.c:1.4 src/sys/dev/fdt/fdt_gpio.c:1.5 --- src/sys/dev/fdt/fdt_gpio.c:1.4 Sat Oct 15 08:30:42 2016 +++ src/sys/dev/fdt/fdt_gpio.c Sun Aug 13 18:27:11 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: fdt_gpio.c,v 1.4 2016/10/15 08:30:42 maxv Exp $ */ +/* $NetBSD: fdt_gpio.c,v 1.5 2017/08/13 18:27:11 jmcneill Exp $ */ /*- * Copyright (c) 2015 Jared D. McNeill <jmcne...@invisible.ca> @@ -27,7 +27,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: fdt_gpio.c,v 1.4 2016/10/15 08:30:42 maxv Exp $"); +__KERNEL_RCSID(0, "$NetBSD: fdt_gpio.c,v 1.5 2017/08/13 18:27:11 jmcneill Exp $"); #include <sys/param.h> #include <sys/bus.h> @@ -80,39 +80,45 @@ fdtbus_get_gpio_controller(int phandle) struct fdtbus_gpio_pin * fdtbus_gpio_acquire(int phandle, const char *prop, int flags) { - struct fdtbus_gpio_controller *gc; - struct fdtbus_gpio_pin *gp; - int gpio_phandle, len; - u_int *data; - - gpio_phandle = fdtbus_get_phandle(phandle, prop); - if (gpio_phandle == -1) { - return NULL; - } - - gc = fdtbus_get_gpio_controller(gpio_phandle); - if (gc == NULL) { - return NULL; - } + return fdtbus_gpio_acquire_index(phandle, prop, 0, flags); +} - len = OF_getproplen(phandle, prop); - if (len < 4) { - return NULL; - } +struct fdtbus_gpio_pin * +fdtbus_gpio_acquire_index(int phandle, const char *prop, + int index, int flags) +{ + struct fdtbus_gpio_controller *gc; + struct fdtbus_gpio_pin *gp = NULL; + const uint32_t *gpios, *p; + u_int n, gpio_cells; + int len, resid; - data = kmem_alloc(len, KM_SLEEP); - if (OF_getprop(phandle, prop, data, len) != len) { - kmem_free(data, len); + gpios = fdtbus_get_prop(phandle, prop, &len); + if (gpios == NULL) return NULL; - } - gp = kmem_alloc(sizeof(*gp), KM_SLEEP); - gp->gp_gc = gc; - gp->gp_priv = gc->gc_funcs->acquire(gc->gc_dev, data, len, flags); - if (gp->gp_priv == NULL) { - kmem_free(data, len); - kmem_free(gp, sizeof(*gp)); - return NULL; + p = gpios; + for (n = 0, resid = len; resid > 0; n++) { + const int gc_phandle = + fdtbus_get_phandle_from_native(be32toh(p[0])); + if (of_getprop_uint32(gc_phandle, "#gpio-cells", &gpio_cells)) + break; + if (n == index) { + gc = fdtbus_get_gpio_controller(gc_phandle); + if (gc == NULL) + return NULL; + gp = kmem_alloc(sizeof(*gp), KM_SLEEP); + gp->gp_gc = gc; + gp->gp_priv = gc->gc_funcs->acquire(gc->gc_dev, + &p[0], (gpio_cells + 1) * 4, flags); + if (gp->gp_priv == NULL) { + kmem_free(gp, sizeof(*gp)); + return NULL; + } + break; + } + resid -= (gpio_cells + 1) * 4; + p += gpio_cells + 1; } return gp; Index: src/sys/dev/fdt/fdtvar.h diff -u src/sys/dev/fdt/fdtvar.h:1.24 src/sys/dev/fdt/fdtvar.h:1.25 --- src/sys/dev/fdt/fdtvar.h:1.24 Sat Jul 8 12:36:51 2017 +++ src/sys/dev/fdt/fdtvar.h Sun Aug 13 18:27:11 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: fdtvar.h,v 1.24 2017/07/08 12:36:51 jmcneill Exp $ */ +/* $NetBSD: fdtvar.h,v 1.25 2017/08/13 18:27:11 jmcneill Exp $ */ /*- * Copyright (c) 2015 Jared D. McNeill <jmcne...@invisible.ca> @@ -236,6 +236,7 @@ void * fdtbus_intr_establish(int, u_int void fdtbus_intr_disestablish(int, void *); bool fdtbus_intr_str(int, u_int, char *, size_t); struct fdtbus_gpio_pin *fdtbus_gpio_acquire(int, const char *, int); +struct fdtbus_gpio_pin *fdtbus_gpio_acquire_index(int, const char *, int, int); void fdtbus_gpio_release(struct fdtbus_gpio_pin *); int fdtbus_gpio_read(struct fdtbus_gpio_pin *); void fdtbus_gpio_write(struct fdtbus_gpio_pin *, int);