xref: /openbsd/sys/dev/ofw/ofw_pinctrl.c (revision a7702439)
1 /*	$OpenBSD: ofw_pinctrl.c,v 1.3 2020/06/06 16:59:43 patrick 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
pinctrl_register(int node,int (* pinctrl)(uint32_t,void *),void * cookie)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
pinctrl_register_child(int node,int (* pinctrl)(uint32_t,void *),void * cookie)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
pinctrl_byphandle(uint32_t phandle)64 pinctrl_byphandle(uint32_t phandle)
65 {
66 	struct pinctrl *pc;
67 
68 	if (phandle == 0)
69 		return -1;
70 
71 	LIST_FOREACH(pc, &pinctrls, pc_list) {
72 		if (pc->pc_phandle == phandle)
73 			return pc->pc_pinctrl(pc->pc_phandle, pc->pc_cookie);
74 	}
75 
76 	return -1;
77 }
78 
79 int
pinctrl_byid(int node,int id)80 pinctrl_byid(int node, int id)
81 {
82 	char pinctrl[32];
83 	uint32_t *phandles;
84 	int len, i;
85 
86 	snprintf(pinctrl, sizeof(pinctrl), "pinctrl-%d", id);
87 	len = OF_getproplen(node, pinctrl);
88 	if (len <= 0)
89 		return -1;
90 
91 	phandles = malloc(len, M_TEMP, M_WAITOK);
92 	OF_getpropintarray(node, pinctrl, phandles, len);
93 	for (i = 0; i < len / sizeof(uint32_t); i++)
94 		pinctrl_byphandle(phandles[i]);
95 	free(phandles, M_TEMP, len);
96 	return 0;
97 }
98 
99 int
pinctrl_byname(int node,const char * config)100 pinctrl_byname(int node, const char *config)
101 {
102 	int id;
103 
104 	id = OF_getindex(node, config, "pinctrl-names");
105 	if (id < 0)
106 		return -1;
107 
108 	return pinctrl_byid(node, id);
109 }
110