am18xx_pupd.c revision 1.1 1 /* $NetBSD: am18xx_pupd.c,v 1.1 2026/09/27 19:55:02 yurix Exp $ */
2
3 /*-
4 * Copyright (c) 2026 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Yuri Honegger.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Pull-up/pull-down pin configuration on the TI AM18XX SoC.
34 */
35
36 #include <sys/param.h>
37 #include <sys/cdefs.h>
38 #include <sys/device.h>
39 #include <sys/mutex.h>
40
41 #include <dev/fdt/fdtvar.h>
42
43 struct am18xx_pupd_softc {
44 bus_space_tag_t sc_bst;
45 bus_space_handle_t sc_bsh;
46 kmutex_t sc_lock;
47 };
48
49 static int am18xx_pupd_match(device_t, cfdata_t, void *);
50 static void am18xx_pupd_attach(device_t, device_t, void *);
51 static int am18xx_pupd_set_config(device_t, const void *, size_t);
52 static void am18xx_pupd_configure_group(struct am18xx_pupd_softc *, int);
53
54 CFATTACH_DECL_NEW(am18xxpupd, sizeof(struct am18xx_pupd_softc),
55 am18xx_pupd_match, am18xx_pupd_attach, NULL, NULL);
56
57 #define AM18XX_PUPD_ENA 0
58 #define AM18XX_PUPD_SEL 4
59
60 #define PUPD_READ(sc, reg) \
61 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, reg)
62 #define PUPD_WRITE(sc, reg, val) \
63 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, reg, val)
64
65 static const struct device_compatible_entry compat_data[] = {
66 { .compat = "ti,da850-pupd" },
67 DEVICE_COMPAT_EOL
68 };
69
70 static struct fdtbus_pinctrl_controller_func am18xx_pupd_pinctrl_funcs = {
71 .set_config = am18xx_pupd_set_config,
72 };
73
74 static int
75 am18xx_pupd_set_config(device_t dev, const void *data, size_t len)
76 {
77 struct am18xx_pupd_softc * const sc = device_private(dev);
78
79 if (len != 4) {
80 return -1;
81 }
82
83 const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
84
85 for (int child = OF_child(phandle); child; child = OF_peer(child)) {
86 am18xx_pupd_configure_group(sc, child);
87 }
88
89 return 0;
90 }
91
92 static void
93 am18xx_pupd_configure_group(struct am18xx_pupd_softc *sc, int phandle)
94 {
95 int groups_len;
96 const char *groups = fdtbus_pinctrl_parse_groups(phandle, &groups_len);
97
98 if (groups == NULL) {
99 return;
100 }
101
102 uint32_t active_groups = 0;
103 while (groups_len > 0) {
104 for (int j = 0; j < 32; j++) {
105 char buf[16];
106 snprintf(buf, sizeof(buf), "cp%d", j);
107 if (strcmp(groups, buf) == 0) {
108 active_groups |= __BIT(j);
109 }
110 }
111 int len = strlen(groups) + 1;
112 groups += len;
113 groups_len -= len;
114 }
115
116 int pull_strength;
117 int bias = fdtbus_pinctrl_parse_bias(phandle, &pull_strength);
118
119 mutex_enter(&sc->sc_lock);
120
121 if (bias == 0) {
122 /* no bias */
123 uint32_t ena = PUPD_READ(sc, AM18XX_PUPD_ENA);
124 ena &= ~active_groups;
125 PUPD_WRITE(sc, AM18XX_PUPD_ENA, ena);
126 } else if (bias == GPIO_PIN_PULLDOWN) {
127 uint32_t sel = PUPD_READ(sc, AM18XX_PUPD_SEL);
128 uint32_t ena = PUPD_READ(sc, AM18XX_PUPD_ENA);
129 sel &= ~active_groups;
130 ena |= active_groups;
131 PUPD_WRITE(sc, AM18XX_PUPD_SEL, sel);
132 PUPD_WRITE(sc, AM18XX_PUPD_ENA, ena);
133 } else if (bias == GPIO_PIN_PULLUP) {
134 uint32_t sel = PUPD_READ(sc, AM18XX_PUPD_SEL);
135 uint32_t ena = PUPD_READ(sc, AM18XX_PUPD_ENA);
136 sel |= active_groups;
137 ena |= active_groups;
138 PUPD_WRITE(sc, AM18XX_PUPD_SEL, sel);
139 PUPD_WRITE(sc, AM18XX_PUPD_ENA, ena);
140 }
141
142 mutex_exit(&sc->sc_lock);
143 }
144
145 int
146 am18xx_pupd_match(device_t parent, cfdata_t cf, void *aux)
147 {
148 struct fdt_attach_args * const faa = aux;
149
150 return of_compatible_match(faa->faa_phandle, compat_data);
151 }
152
153 void
154 am18xx_pupd_attach(device_t parent, device_t self, void *aux)
155 {
156 struct am18xx_pupd_softc * const sc = device_private(self);
157 struct fdt_attach_args * const faa = aux;
158 const int phandle = faa->faa_phandle;
159 bus_addr_t addr;
160 bus_size_t size;
161
162 sc->sc_bst = faa->faa_bst;
163
164 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
165
166 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
167 aprint_error(": couldn't get registers\n");
168 return;
169 }
170 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh)) {
171 aprint_error(": couldn't map registers\n");
172 return;
173 }
174
175 for (int child = OF_child(phandle); child; child = OF_peer(child)) {
176 fdtbus_register_pinctrl_config(self, child,
177 &am18xx_pupd_pinctrl_funcs);
178 }
179
180 aprint_naive("\n");
181 aprint_normal("\n");
182 }
183
184