Add fdtbus_gpio_acquire_index for accessing multi-xref gpios properties.

This commit is contained in:
jmcneill 2017-08-13 18:27:11 +00:00
parent 77a9c1779f
commit 97d3260c03
2 changed files with 39 additions and 32 deletions

View File

@ -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 <jmcneill@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>
@ -79,40 +79,46 @@ fdtbus_get_gpio_controller(int phandle)
struct fdtbus_gpio_pin *
fdtbus_gpio_acquire(int phandle, const char *prop, int flags)
{
return fdtbus_gpio_acquire_index(phandle, prop, 0, flags);
}
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;
int gpio_phandle, len;
u_int *data;
struct fdtbus_gpio_pin *gp = NULL;
const uint32_t *gpios, *p;
u_int n, gpio_cells;
int len, resid;
gpio_phandle = fdtbus_get_phandle(phandle, prop);
if (gpio_phandle == -1) {
gpios = fdtbus_get_prop(phandle, prop, &len);
if (gpios == NULL)
return NULL;
}
gc = fdtbus_get_gpio_controller(gpio_phandle);
if (gc == NULL) {
return NULL;
}
len = OF_getproplen(phandle, prop);
if (len < 4) {
return NULL;
}
data = kmem_alloc(len, KM_SLEEP);
if (OF_getprop(phandle, prop, data, len) != len) {
kmem_free(data, len);
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;

View File

@ -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 <jmcneill@invisible.ca>
@ -236,6 +236,7 @@ void * fdtbus_intr_establish(int, u_int, int, 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);