1 /* $OpenBSD: ofw_pinctrl.c,v 1.1 2016/08/06 17:12:34 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2016 Mark Kettenis 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/systm.h> 20 #include <sys/malloc.h> 21 22 #include <dev/ofw/openfirm.h> 23 #include <dev/ofw/ofw_pinctrl.h> 24 25 struct pinctrl { 26 int pc_phandle; 27 int (*pc_pinctrl)(uint32_t, void *); 28 void *pc_cookie; 29 30 LIST_ENTRY(pinctrl) pc_list; 31 }; 32 33 void pinctrl_register_child(int, int (*)(uint32_t, void *), void *); 34 35 LIST_HEAD(, pinctrl) pinctrls = LIST_HEAD_INITIALIZER(pinctrl); 36 37 void 38 pinctrl_register(int node, int (*pinctrl)(uint32_t, void *), void *cookie) 39 { 40 for (node = OF_child(node); node; node = OF_peer(node)) 41 pinctrl_register_child(node, pinctrl, cookie); 42 } 43 44 void 45 pinctrl_register_child(int node, int (*pinctrl)(uint32_t, void *), void *cookie) 46 { 47 struct pinctrl *pc; 48 uint32_t phandle; 49 50 phandle = OF_getpropint(node, "phandle", 0); 51 if (phandle) { 52 pc = malloc(sizeof(struct pinctrl), M_DEVBUF, M_WAITOK); 53 pc->pc_phandle = phandle; 54 pc->pc_pinctrl = pinctrl; 55 pc->pc_cookie = cookie; 56 LIST_INSERT_HEAD(&pinctrls, pc, pc_list); 57 } 58 59 for (node = OF_child(node); node; node = OF_peer(node)) 60 pinctrl_register_child(node, pinctrl, cookie); 61 } 62 63 int 64 pinctrl_byphandle(uint32_t phandle) 65 { 66 struct pinctrl *pc; 67 68 LIST_FOREACH(pc, &pinctrls, pc_list) { 69 if (pc->pc_phandle == phandle) 70 return pc->pc_pinctrl(pc->pc_phandle, pc->pc_cookie); 71 } 72 73 return -1; 74 } 75 76 int 77 pinctrl_byid(int node, int id) 78 { 79 char pinctrl[32]; 80 uint32_t *phandles; 81 int len, i; 82 83 snprintf(pinctrl, sizeof(pinctrl), "pinctrl-%d", id); 84 len = OF_getproplen(node, pinctrl); 85 if (len <= 0) 86 return -1; 87 88 phandles = malloc(len, M_TEMP, M_WAITOK); 89 OF_getpropintarray(node, pinctrl, phandles, len); 90 for (i = 0; i < len / sizeof(uint32_t); i++) 91 pinctrl_byphandle(phandles[i]); 92 free(phandles, M_TEMP, len); 93 return 0; 94 } 95 96 int 97 pinctrl_byname(int node, const char *config) 98 { 99 char *names; 100 char *name; 101 char *end; 102 int id = 0; 103 int len; 104 105 len = OF_getproplen(node, "pinctrl-names"); 106 if (len <= 0) 107 return -1; 108 109 names = malloc(len, M_TEMP, M_WAITOK); 110 OF_getprop(node, "pinctrl-names", names, len); 111 end = names + len; 112 name = names; 113 while (name < end) { 114 if (strcmp(name, config) == 0) { 115 free(names, M_TEMP, len); 116 return pinctrl_byid(node, id); 117 } 118 name += strlen(name) + 1; 119 id++; 120 } 121 free(names, M_TEMP, len); 122 return -1; 123 } 124