3 * Ben Gray <ben.r.gray@gmail.com>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Ben Gray.
17 * 4. The name of the company nor the name of the author may be used to
18 * endorse or promote products derived from this software without specific
19 * prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY BEN GRAY ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL BEN GRAY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Exposes pinmux module to pinctrl-compatible interface
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/resource.h>
47 #include <sys/mutex.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
52 #include <dev/ofw/openfirm.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/fdt/fdt_pinctrl.h>
57 #include <arm/ti/omap4/omap4_scm_padconf.h>
58 #include <arm/ti/am335x/am335x_scm_padconf.h>
59 #include <arm/ti/ti_cpuid.h>
60 #include "ti_pinmux.h"
67 static struct resource_spec ti_pinmux_res_spec[] = {
68 { SYS_RES_MEMORY, 0, RF_ACTIVE }, /* Control memory window */
72 static struct ti_pinmux_softc *ti_pinmux_sc;
74 #define ti_pinmux_read_2(sc, reg) \
75 bus_space_read_2((sc)->sc_bst, (sc)->sc_bsh, (reg))
76 #define ti_pinmux_write_2(sc, reg, val) \
77 bus_space_write_2((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
78 #define ti_pinmux_read_4(sc, reg) \
79 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
80 #define ti_pinmux_write_4(sc, reg, val) \
81 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
84 * ti_padconf_devmap - Array of pins, should be defined one per SoC
86 * This array is typically defined in one of the targeted *_scm_pinumx.c
87 * files and is specific to the given SoC platform. Each entry in the array
88 * corresponds to an individual pin.
90 static const struct ti_pinmux_device *ti_pinmux_dev;
93 * ti_pinmux_padconf_from_name - searches the list of pads and returns entry
94 * with matching ball name.
95 * @ballname: the name of the ball
98 * A pointer to the matching padconf or NULL if the ball wasn't found.
100 static const struct ti_pinmux_padconf*
101 ti_pinmux_padconf_from_name(const char *ballname)
103 const struct ti_pinmux_padconf *padconf;
105 padconf = ti_pinmux_dev->padconf;
106 while (padconf->ballname != NULL) {
107 if (strcmp(ballname, padconf->ballname) == 0)
116 * ti_pinmux_padconf_set_internal - sets the muxmode and state for a pad/pin
117 * @padconf: pointer to the pad structure
118 * @muxmode: the name of the mode to use for the pin, i.e. "uart1_rx"
119 * @state: the state to put the pad/pin in, i.e. PADCONF_PIN_???
123 * Internally locks it's own context.
127 * EINVAL if pin requested is outside valid range or already in use.
130 ti_pinmux_padconf_set_internal(struct ti_pinmux_softc *sc,
131 const struct ti_pinmux_padconf *padconf,
132 const char *muxmode, unsigned int state)
137 /* populate the new value for the PADCONF register */
138 reg_val = (uint16_t)(state & ti_pinmux_dev->padconf_sate_mask);
140 /* find the new mode requested */
141 for (mode = 0; mode < 8; mode++) {
142 if ((padconf->muxmodes[mode] != NULL) &&
143 (strcmp(padconf->muxmodes[mode], muxmode) == 0)) {
148 /* couldn't find the mux mode */
150 printf("Invalid mode \"%s\"\n", muxmode);
154 /* set the mux mode */
155 reg_val |= (uint16_t)(mode & ti_pinmux_dev->padconf_muxmode_mask);
158 device_printf(sc->sc_dev, "setting internal %x for %s\n",
160 /* write the register value (16-bit writes) */
161 ti_pinmux_write_2(sc, padconf->reg_off, reg_val);
167 * ti_pinmux_padconf_set - sets the muxmode and state for a pad/pin
168 * @padname: the name of the pad, i.e. "c12"
169 * @muxmode: the name of the mode to use for the pin, i.e. "uart1_rx"
170 * @state: the state to put the pad/pin in, i.e. PADCONF_PIN_???
174 * Internally locks it's own context.
178 * EINVAL if pin requested is outside valid range or already in use.
181 ti_pinmux_padconf_set(const char *padname, const char *muxmode, unsigned int state)
183 const struct ti_pinmux_padconf *padconf;
188 /* find the pin in the devmap */
189 padconf = ti_pinmux_padconf_from_name(padname);
193 return (ti_pinmux_padconf_set_internal(ti_pinmux_sc, padconf, muxmode, state));
197 * ti_pinmux_padconf_get - gets the muxmode and state for a pad/pin
198 * @padname: the name of the pad, i.e. "c12"
199 * @muxmode: upon return will contain the name of the muxmode of the pin
200 * @state: upon return will contain the state of the pad/pin
204 * Internally locks it's own context.
208 * EINVAL if pin requested is outside valid range or already in use.
211 ti_pinmux_padconf_get(const char *padname, const char **muxmode,
214 const struct ti_pinmux_padconf *padconf;
220 /* find the pin in the devmap */
221 padconf = ti_pinmux_padconf_from_name(padname);
225 /* read the register value (16-bit reads) */
226 reg_val = ti_pinmux_read_2(ti_pinmux_sc, padconf->reg_off);
230 *state = (reg_val & ti_pinmux_dev->padconf_sate_mask);
234 *muxmode = padconf->muxmodes[(reg_val & ti_pinmux_dev->padconf_muxmode_mask)];
240 * ti_pinmux_padconf_set_gpiomode - converts a pad to GPIO mode.
241 * @gpio: the GPIO pin number (0-195)
242 * @state: the state to put the pad/pin in, i.e. PADCONF_PIN_???
247 * Internally locks it's own context.
251 * EINVAL if pin requested is outside valid range or already in use.
254 ti_pinmux_padconf_set_gpiomode(uint32_t gpio, unsigned int state)
256 const struct ti_pinmux_padconf *padconf;
262 /* find the gpio pin in the padconf array */
263 padconf = ti_pinmux_dev->padconf;
264 while (padconf->ballname != NULL) {
265 if (padconf->gpio_pin == gpio)
269 if (padconf->ballname == NULL)
272 /* populate the new value for the PADCONF register */
273 reg_val = (uint16_t)(state & ti_pinmux_dev->padconf_sate_mask);
275 /* set the mux mode */
276 reg_val |= (uint16_t)(padconf->gpio_mode & ti_pinmux_dev->padconf_muxmode_mask);
278 /* write the register value (16-bit writes) */
279 ti_pinmux_write_2(ti_pinmux_sc, padconf->reg_off, reg_val);
285 * ti_pinmux_padconf_get_gpiomode - gets the current GPIO mode of the pin
286 * @gpio: the GPIO pin number (0-195)
287 * @state: upon return will contain the state
292 * Internally locks it's own context.
296 * EINVAL if pin requested is outside valid range or not configured as GPIO.
299 ti_pinmux_padconf_get_gpiomode(uint32_t gpio, unsigned int *state)
301 const struct ti_pinmux_padconf *padconf;
307 /* find the gpio pin in the padconf array */
308 padconf = ti_pinmux_dev->padconf;
309 while (padconf->ballname != NULL) {
310 if (padconf->gpio_pin == gpio)
314 if (padconf->ballname == NULL)
317 /* read the current register settings */
318 reg_val = ti_pinmux_read_2(ti_pinmux_sc, padconf->reg_off);
320 /* check to make sure the pins is configured as GPIO in the first state */
321 if ((reg_val & ti_pinmux_dev->padconf_muxmode_mask) != padconf->gpio_mode)
324 /* read and store the reset of the state, i.e. pull-up, pull-down, etc */
326 *state = (reg_val & ti_pinmux_dev->padconf_sate_mask);
332 ti_pinmux_configure_pins(device_t dev, phandle_t cfgxref)
334 struct pincfg *cfgtuples, *cfg;
337 static struct ti_pinmux_softc *sc;
339 sc = device_get_softc(dev);
340 cfgnode = OF_node_from_xref(cfgxref);
341 ntuples = OF_getencprop_alloc_multi(cfgnode, "pinctrl-single,pins",
342 sizeof(*cfgtuples), (void **)&cfgtuples);
348 return (0); /* Empty property is not an error. */
350 for (i = 0, cfg = cfgtuples; i < ntuples; i++, cfg++) {
353 OF_getprop(cfgnode, "name", &name, sizeof(name));
354 printf("%16s: muxreg 0x%04x muxval 0x%02x\n",
355 name, cfg->reg, cfg->conf);
358 /* write the register value (16-bit writes) */
359 ti_pinmux_write_2(sc, cfg->reg, cfg->conf);
362 OF_prop_free(cfgtuples);
368 * Device part of OMAP SCM driver
372 ti_pinmux_probe(device_t dev)
374 if (!ofw_bus_status_okay(dev))
377 if (!ofw_bus_is_compatible(dev, "pinctrl-single"))
381 printf("%s: multiple pinctrl modules in device tree data, ignoring\n",
388 ti_pinmux_dev = &omap4_pinmux_dev;
393 ti_pinmux_dev = &ti_am335x_pinmux_dev;
397 printf("Unknown CPU in pinmux\n");
401 device_set_desc(dev, "TI Pinmux Module");
402 return (BUS_PROBE_DEFAULT);
406 * ti_pinmux_attach - attaches the pinmux to the simplebus
410 * Zero on success or ENXIO if an error occuried.
413 ti_pinmux_attach(device_t dev)
415 struct ti_pinmux_softc *sc = device_get_softc(dev);
424 if (bus_alloc_resources(dev, ti_pinmux_res_spec, sc->sc_res)) {
425 device_printf(dev, "could not allocate resources\n");
429 sc->sc_bst = rman_get_bustag(sc->sc_res[0]);
430 sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]);
432 if (ti_pinmux_sc == NULL)
435 fdt_pinctrl_register(dev, "pinctrl-single,pins");
436 fdt_pinctrl_configure_tree(dev);
441 static device_method_t ti_pinmux_methods[] = {
442 DEVMETHOD(device_probe, ti_pinmux_probe),
443 DEVMETHOD(device_attach, ti_pinmux_attach),
445 /* fdt_pinctrl interface */
446 DEVMETHOD(fdt_pinctrl_configure, ti_pinmux_configure_pins),
450 static driver_t ti_pinmux_driver = {
453 sizeof(struct ti_pinmux_softc),
456 DRIVER_MODULE(ti_pinmux, simplebus, ti_pinmux_driver, 0, 0);
457 MODULE_VERSION(ti_pinmux, 1);
458 MODULE_DEPEND(ti_pinmux, ti_scm, 1, 1, 1);