Home | History | Annotate | Line # | Download | only in fdt
fdt_pinctrl.c revision 1.1
      1  1.1  marty /* $NetBSD: fdt_pinctrl.c,v 1.1 2015/12/30 04:23:39 marty Exp $ */
      2  1.1  marty 
      3  1.1  marty /*-
      4  1.1  marty  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  marty  * All rights reserved.
      6  1.1  marty  *
      7  1.1  marty  * Redistribution and use in source and binary forms, with or without
      8  1.1  marty  * modification, are permitted provided that the following conditions
      9  1.1  marty  * are met:
     10  1.1  marty  * 1. Redistributions of source code must retain the above copyright
     11  1.1  marty  *    notice, this list of conditions and the following disclaimer.
     12  1.1  marty  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  marty  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  marty  *    documentation and/or other materials provided with the distribution.
     15  1.1  marty  *
     16  1.1  marty  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  marty  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  marty  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  marty  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  marty  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  marty  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  marty  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  marty  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  marty  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  marty  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  marty  * SUCH DAMAGE.
     27  1.1  marty  */
     28  1.1  marty 
     29  1.1  marty #include <sys/cdefs.h>
     30  1.1  marty __KERNEL_RCSID(0, "$NetBSD: fdt_pinctrl.c,v 1.1 2015/12/30 04:23:39 marty Exp $");
     31  1.1  marty 
     32  1.1  marty #include <sys/param.h>
     33  1.1  marty #include <sys/bus.h>
     34  1.1  marty #include <sys/kmem.h>
     35  1.1  marty 
     36  1.1  marty #include <libfdt.h>
     37  1.1  marty #include <dev/fdt/fdtvar.h>
     38  1.1  marty 
     39  1.1  marty struct fdtbus_pinctrl_controller {
     40  1.1  marty 	device_t pc_dev;
     41  1.1  marty 	int pc_phandle;
     42  1.1  marty 	const struct fdtbus_pinctrl_controller_func *pc_funcs;
     43  1.1  marty 
     44  1.1  marty 	struct fdtbus_pinctrl_controller *pc_next;
     45  1.1  marty };
     46  1.1  marty 
     47  1.1  marty static struct fdtbus_pinctrl_controller *fdtbus_pc = NULL;
     48  1.1  marty 
     49  1.1  marty int
     50  1.1  marty fdtbus_register_pinctrl_controller(device_t dev, int phandle,
     51  1.1  marty     const struct fdtbus_pinctrl_controller_func *funcs)
     52  1.1  marty {
     53  1.1  marty 	struct fdtbus_pinctrl_controller *pc;
     54  1.1  marty 
     55  1.1  marty 	pc = kmem_alloc(sizeof(*pc), KM_SLEEP);
     56  1.1  marty 	pc->pc_dev = dev;
     57  1.1  marty 	pc->pc_phandle = phandle;
     58  1.1  marty 	pc->pc_funcs = funcs;
     59  1.1  marty 
     60  1.1  marty 	pc->pc_next = fdtbus_pc;
     61  1.1  marty 	fdtbus_pc = pc;
     62  1.1  marty 
     63  1.1  marty 	return 0;
     64  1.1  marty }
     65  1.1  marty 
     66  1.1  marty struct fdtbus_pinctrl_pin *
     67  1.1  marty fdtbus_pinctrl_acquire(int phandle, const char *prop)
     68  1.1  marty {
     69  1.1  marty 	struct fdtbus_pinctrl_controller *pc;
     70  1.1  marty 	struct fdtbus_pinctrl_pin *gp;
     71  1.1  marty 
     72  1.1  marty 	gp = kmem_alloc(sizeof(*gp), KM_SLEEP);
     73  1.1  marty 	for (pc = fdtbus_pc; pc; pc = pc->pc_next) {
     74  1.1  marty 		gp->pp_pc = pc;
     75  1.1  marty 		gp->pp_priv = pc->pc_funcs->acquire(pc->pc_dev, prop);
     76  1.1  marty 		if (gp->pp_priv != NULL)
     77  1.1  marty 			break;
     78  1.1  marty 	}
     79  1.1  marty 
     80  1.1  marty 	if (gp->pp_priv == NULL) {
     81  1.1  marty 		kmem_free(gp, sizeof(*gp));
     82  1.1  marty 		return NULL;
     83  1.1  marty 	}
     84  1.1  marty 
     85  1.1  marty 	return gp;
     86  1.1  marty }
     87  1.1  marty 
     88  1.1  marty void
     89  1.1  marty fdtbus_pinctrl_release(struct fdtbus_pinctrl_pin *gp)
     90  1.1  marty {
     91  1.1  marty 	struct fdtbus_pinctrl_controller *pc = gp->pp_pc;
     92  1.1  marty 
     93  1.1  marty 	pc->pc_funcs->release(pc->pc_dev, gp->pp_priv);
     94  1.1  marty 	kmem_free(gp, sizeof(*gp));
     95  1.1  marty }
     96  1.1  marty 
     97  1.1  marty void
     98  1.1  marty fdtbus_pinctrl_get_cfg(struct fdtbus_pinctrl_pin *gp, void *cookie)
     99  1.1  marty {
    100  1.1  marty 	struct fdtbus_pinctrl_controller *pc = gp->pp_pc;
    101  1.1  marty 
    102  1.1  marty 	pc->pc_funcs->get(gp, cookie);
    103  1.1  marty }
    104  1.1  marty 
    105  1.1  marty void
    106  1.1  marty fdtbus_pinctrl_set_cfg(struct fdtbus_pinctrl_pin *gp, void *cookie)
    107  1.1  marty {
    108  1.1  marty 	struct fdtbus_pinctrl_controller *pc = gp->pp_pc;
    109  1.1  marty 
    110  1.1  marty 	pc->pc_funcs->set(gp, cookie);
    111  1.1  marty }
    112