xref: /netbsd-src/sys/dev/fdt/pinctrl_single.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $NetBSD: pinctrl_single.c,v 1.1 2019/10/27 15:31:15 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pinctrl_single.c,v 1.1 2019/10/27 15:31:15 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/gpio.h>
37 
38 #include <dev/fdt/fdtvar.h>
39 
40 #define	PINCTRL_FLAG_PINCONF	__BIT(0)	/* supports generic pinconf */
41 
42 struct pinctrl_single_config {
43 	uint32_t	flags;
44 };
45 
46 static const struct pinctrl_single_config pinctrl_config = {
47 };
48 
49 static const struct pinctrl_single_config pinconf_config = {
50 	.flags = PINCTRL_FLAG_PINCONF
51 };
52 
53 static const struct of_compat_data compat_data[] = {
54 	{ "pinctrl-single",		(uintptr_t)&pinctrl_config },
55 	{ "pinconf-single",		(uintptr_t)&pinconf_config },
56 	{ NULL }
57 };
58 
59 struct pinctrl_single_softc {
60 	device_t			sc_dev;
61 	int				sc_phandle;
62 	bus_space_tag_t			sc_bst;
63 	bus_space_handle_t		sc_bsh;
64 	uint32_t			sc_flags;
65 	u_int				sc_regwidth;
66 	u_int				sc_funcmask;
67 };
68 
69 static void
70 pinctrl_single_pins_write(struct pinctrl_single_softc *sc, u_int off, u_int val)
71 {
72 	union {
73 		uint32_t reg32;
74 		uint16_t reg16;
75 		uint8_t reg8;
76 	} u;
77 
78 	aprint_debug_dev(sc->sc_dev, "writing %#x with %#x\n", off, val);
79 
80 	switch (sc->sc_regwidth) {
81 	case 8:
82 		u.reg8 = bus_space_read_1(sc->sc_bst, sc->sc_bsh, off);
83 		u.reg8 &= ~sc->sc_funcmask;
84 		u.reg8 |= val;
85 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, u.reg8);
86 		break;
87 	case 16:
88 		u.reg16 = bus_space_read_2(sc->sc_bst, sc->sc_bsh, off);
89 		u.reg16 &= ~sc->sc_funcmask;
90 		u.reg16 |= val;
91 		bus_space_write_2(sc->sc_bst, sc->sc_bsh, off, u.reg16);
92 		break;
93 	case 32:
94 		u.reg32 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, off);
95 		u.reg32 &= ~sc->sc_funcmask;
96 		u.reg32 |= val;
97 		bus_space_write_4(sc->sc_bst, sc->sc_bsh, off, u.reg32);
98 		break;
99 	default:
100 		device_printf(sc->sc_dev, "%s: unsupported reg width %d\n",
101 		    __func__, sc->sc_regwidth);
102 		break;
103 	}
104 }
105 
106 static int
107 pinctrl_single_pins_set_config(device_t dev, const void *data, size_t len)
108 {
109 	struct pinctrl_single_softc * const sc = device_private(dev);
110 	const u_int *pins;
111 	int pinslen;
112 
113 	if (len != 4)
114 		return -1;
115 
116 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
117 
118 	pins = fdtbus_get_prop(phandle, "pinctrl-single,pins", &pinslen);
119 	if (pins == NULL)
120 		return -1;
121 
122 	while (pinslen >= 8) {
123 		const int off = be32toh(pins[0]);
124 		const int val = be32toh(pins[1]);
125 
126 		pinctrl_single_pins_write(sc, off, val);
127 		pins += 2;
128 		pinslen -= 8;
129 	}
130 
131 	return 0;
132 }
133 
134 static struct fdtbus_pinctrl_controller_func pinctrl_single_pins_funcs = {
135 	.set_config = pinctrl_single_pins_set_config,
136 };
137 
138 static int
139 pinctrl_single_match(device_t parent, cfdata_t cf, void *aux)
140 {
141 	struct fdt_attach_args * const faa = aux;
142 
143 	return of_match_compat_data(faa->faa_phandle, compat_data);
144 }
145 
146 static void
147 pinctrl_single_attach(device_t parent, device_t self, void *aux)
148 {
149 	struct pinctrl_single_softc * const sc = device_private(self);
150 	struct fdt_attach_args * const faa = aux;
151 	const int phandle = faa->faa_phandle;
152 	const struct pinctrl_single_config *conf;
153 	bus_addr_t addr;
154 	bus_size_t size;
155 	int child;
156 
157 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
158 		aprint_error(": couldn't get registers\n");
159 		return;
160 	}
161 
162 	conf = (const void *)of_search_compatible(phandle, compat_data)->data;
163 
164 	sc->sc_dev = self;
165 	sc->sc_phandle = phandle;
166 	sc->sc_bst = faa->faa_bst;
167 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
168 		aprint_error(": couldn't map registers\n");
169 		return;
170 	}
171 	sc->sc_flags = conf->flags;
172 
173 	if (of_getprop_uint32(phandle, "pinctrl-single,register-width", &sc->sc_regwidth) != 0) {
174 		aprint_error(": missing 'pinctrl-single,register-width' property\n");
175 		return;
176 	}
177 	if (of_getprop_uint32(phandle, "pinctrl-single,function-mask", &sc->sc_funcmask) != 0) {
178 		aprint_error(": missing 'pinctrl-single,function-mask' property\n");
179 		return;
180 	}
181 
182 	switch (sc->sc_regwidth) {
183 	case 8:
184 	case 16:
185 	case 32:
186 		break;
187 	default:
188 		aprint_error(": register width %d not supported\n", sc->sc_regwidth);
189 		return;
190 	}
191 
192 	aprint_naive("\n");
193 	aprint_normal("\n");
194 
195 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
196 		if (of_hasprop(child, "pinctrl-single,pins"))
197 			fdtbus_register_pinctrl_config(self, child, &pinctrl_single_pins_funcs);
198 	}
199 
200 	fdtbus_pinctrl_set_config(phandle, "default");
201 }
202 
203 CFATTACH_DECL_NEW(pinctrl_single, sizeof(struct pinctrl_single_softc),
204 	pinctrl_single_match, pinctrl_single_attach, NULL, NULL);
205