Home | History | Annotate | Line # | Download | only in fdt
fdt_pinctrl.c revision 1.2.4.1
      1  1.2.4.1  pgoyette /* $NetBSD: fdt_pinctrl.c,v 1.2.4.1 2016/11/04 14:49:08 pgoyette Exp $ */
      2      1.1     marty 
      3      1.1     marty /*-
      4      1.2     marty  * Copyright (c) 2015 Martin Fouts
      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.2.4.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: fdt_pinctrl.c,v 1.2.4.1 2016/11/04 14:49:08 pgoyette 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 	int pc_phandle;
     41      1.2     marty 	void *pc_cookie;
     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.2     marty fdtbus_register_pinctrl_config(void *cookie, 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.2     marty 	pc->pc_cookie = cookie;
     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.2     marty static struct fdtbus_pinctrl_controller *
     67      1.2     marty fdtbus_pinctrl_lookup(int phandle)
     68      1.1     marty {
     69      1.1     marty 	struct fdtbus_pinctrl_controller *pc;
     70      1.1     marty 
     71      1.2     marty 	for (pc = fdtbus_pc; pc; pc = pc->pc_next)
     72      1.2     marty 		if (pc->pc_phandle == phandle)
     73      1.2     marty 			return pc;
     74      1.1     marty 
     75      1.2     marty 	return NULL;
     76      1.1     marty }
     77      1.1     marty 
     78      1.2     marty int
     79      1.2     marty fdtbus_pinctrl_set_config_index(int phandle, u_int index)
     80      1.1     marty {
     81      1.2     marty 	char buf[80];
     82      1.2     marty 	int len, handle;
     83      1.2     marty 	struct fdtbus_pinctrl_controller *pc;
     84      1.1     marty 
     85      1.2     marty 	snprintf(buf, 80, "pinctrl-%d", index);
     86      1.1     marty 
     87      1.2     marty 	len = OF_getprop(phandle, buf, (char *)&handle,
     88      1.2     marty                         sizeof(handle));
     89      1.2     marty 	if (len != sizeof(int)) {
     90      1.2     marty 		printf("%s: couldn't get %s.\n", __func__, buf);
     91      1.2     marty                return -1;
     92      1.2     marty        }
     93      1.2     marty 
     94      1.2     marty 	handle = fdtbus_get_phandle_from_native(be32toh(handle));
     95      1.2     marty 
     96      1.2     marty 	pc = fdtbus_pinctrl_lookup(handle);
     97      1.2     marty 	if (!pc) {
     98      1.2     marty 		printf("%s: Couldn't get handle %d for %s\n", __func__, handle,
     99      1.2     marty 		       buf);
    100      1.2     marty 		return -1;
    101      1.2     marty 	}
    102      1.1     marty 
    103      1.2     marty 	return pc->pc_funcs->set_config(pc->pc_cookie);
    104      1.1     marty }
    105      1.1     marty 
    106      1.2     marty int
    107      1.2     marty fdtbus_pinctrl_set_config(int phandle, const char *cfgname)
    108      1.1     marty {
    109      1.2     marty 	int index = 0;
    110      1.2     marty 	int len;
    111      1.2     marty 	char *result;
    112      1.2     marty 	char *next;
    113      1.2     marty 
    114      1.2     marty 	len = OF_getproplen(phandle, "pinctrl-names");
    115      1.2     marty 	if (len <= 0)
    116      1.2     marty 		return -1;
    117      1.2     marty 	result = kmem_zalloc(len, KM_SLEEP);
    118      1.2     marty 	OF_getprop(phandle, "pinctrl-names", result, len);
    119      1.2     marty 
    120      1.2     marty 	next = result;
    121      1.2     marty 	while (next - result < len) {
    122      1.2     marty 		if (!strcmp(next, cfgname)) {
    123  1.2.4.1  pgoyette 			kmem_free(result, len);
    124      1.2     marty 			return fdtbus_pinctrl_set_config_index(phandle, index);
    125      1.2     marty 		}
    126      1.2     marty 		index++;
    127      1.2     marty 		while (*next)
    128      1.2     marty 			next++;
    129      1.2     marty 		next++;
    130      1.2     marty 	}
    131      1.1     marty 
    132  1.2.4.1  pgoyette 	kmem_free(result, len);
    133      1.2     marty 	return -1;
    134      1.1     marty }
    135