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))
85 * ti_padconf_devmap - Array of pins, should be defined one per SoC
87 * This array is typically defined in one of the targeted *_scm_pinumx.c
88 * files and is specific to the given SoC platform. Each entry in the array
89 * corresponds to an individual pin.
91 static const struct ti_pinmux_device *ti_pinmux_dev;
95 * ti_pinmux_padconf_from_name - searches the list of pads and returns entry
96 * with matching ball name.
97 * @ballname: the name of the ball
100 * A pointer to the matching padconf or NULL if the ball wasn't found.
102 static const struct ti_pinmux_padconf*
103 ti_pinmux_padconf_from_name(const char *ballname)
105 const struct ti_pinmux_padconf *padconf;
107 padconf = ti_pinmux_dev->padconf;
108 while (padconf->ballname != NULL) {
109 if (strcmp(ballname, padconf->ballname) == 0)
118 * ti_pinmux_padconf_set_internal - sets the muxmode and state for a pad/pin
119 * @padconf: pointer to the pad structure
120 * @muxmode: the name of the mode to use for the pin, i.e. "uart1_rx"
121 * @state: the state to put the pad/pin in, i.e. PADCONF_PIN_???
125 * Internally locks it's own context.
129 * EINVAL if pin requested is outside valid range or already in use.
132 ti_pinmux_padconf_set_internal(struct ti_pinmux_softc *sc,
133 const struct ti_pinmux_padconf *padconf,
134 const char *muxmode, unsigned int state)
139 /* populate the new value for the PADCONF register */
140 reg_val = (uint16_t)(state & ti_pinmux_dev->padconf_sate_mask);
142 /* find the new mode requested */
143 for (mode = 0; mode < 8; mode++) {
144 if ((padconf->muxmodes[mode] != NULL) &&
145 (strcmp(padconf->muxmodes[mode], muxmode) == 0)) {
150 /* couldn't find the mux mode */
152 printf("Invalid mode \"%s\"\n", muxmode);
156 /* set the mux mode */
157 reg_val |= (uint16_t)(mode & ti_pinmux_dev->padconf_muxmode_mask);
160 device_printf(sc->sc_dev, "setting internal %x for %s\n",
162 /* write the register value (16-bit writes) */
163 ti_pinmux_write_2(sc, padconf->reg_off, reg_val);
169 * ti_pinmux_padconf_set - sets the muxmode and state for a pad/pin
170 * @padname: the name of the pad, i.e. "c12"
171 * @muxmode: the name of the mode to use for the pin, i.e. "uart1_rx"
172 * @state: the state to put the pad/pin in, i.e. PADCONF_PIN_???
176 * Internally locks it's own context.
180 * EINVAL if pin requested is outside valid range or already in use.
183 ti_pinmux_padconf_set(const char *padname, const char *muxmode, unsigned int state)
185 const struct ti_pinmux_padconf *padconf;
190 /* find the pin in the devmap */
191 padconf = ti_pinmux_padconf_from_name(padname);
195 return (ti_pinmux_padconf_set_internal(ti_pinmux_sc, padconf, muxmode, state));
199 * ti_pinmux_padconf_get - gets the muxmode and state for a pad/pin
200 * @padname: the name of the pad, i.e. "c12"
201 * @muxmode: upon return will contain the name of the muxmode of the pin
202 * @state: upon return will contain the state of the pad/pin
206 * Internally locks it's own context.
210 * EINVAL if pin requested is outside valid range or already in use.
213 ti_pinmux_padconf_get(const char *padname, const char **muxmode,
216 const struct ti_pinmux_padconf *padconf;
222 /* find the pin in the devmap */
223 padconf = ti_pinmux_padconf_from_name(padname);
227 /* read the register value (16-bit reads) */
228 reg_val = ti_pinmux_read_2(ti_pinmux_sc, padconf->reg_off);
232 *state = (reg_val & ti_pinmux_dev->padconf_sate_mask);
236 *muxmode = padconf->muxmodes[(reg_val & ti_pinmux_dev->padconf_muxmode_mask)];
242 * ti_pinmux_padconf_set_gpiomode - converts a pad to GPIO mode.
243 * @gpio: the GPIO pin number (0-195)
244 * @state: the state to put the pad/pin in, i.e. PADCONF_PIN_???
249 * Internally locks it's own context.
253 * EINVAL if pin requested is outside valid range or already in use.
256 ti_pinmux_padconf_set_gpiomode(uint32_t gpio, unsigned int state)
258 const struct ti_pinmux_padconf *padconf;
264 /* find the gpio pin in the padconf array */
265 padconf = ti_pinmux_dev->padconf;
266 while (padconf->ballname != NULL) {
267 if (padconf->gpio_pin == gpio)
271 if (padconf->ballname == NULL)
274 /* populate the new value for the PADCONF register */
275 reg_val = (uint16_t)(state & ti_pinmux_dev->padconf_sate_mask);
277 /* set the mux mode */
278 reg_val |= (uint16_t)(padconf->gpio_mode & ti_pinmux_dev->padconf_muxmode_mask);
280 /* write the register value (16-bit writes) */
281 ti_pinmux_write_2(ti_pinmux_sc, padconf->reg_off, reg_val);
287 * ti_pinmux_padconf_get_gpiomode - gets the current GPIO mode of the pin
288 * @gpio: the GPIO pin number (0-195)
289 * @state: upon return will contain the state
294 * Internally locks it's own context.
298 * EINVAL if pin requested is outside valid range or not configured as GPIO.
301 ti_pinmux_padconf_get_gpiomode(uint32_t gpio, unsigned int *state)
303 const struct ti_pinmux_padconf *padconf;
309 /* find the gpio pin in the padconf array */
310 padconf = ti_pinmux_dev->padconf;
311 while (padconf->ballname != NULL) {
312 if (padconf->gpio_pin == gpio)
316 if (padconf->ballname == NULL)
319 /* read the current register settings */
320 reg_val = ti_pinmux_read_2(ti_pinmux_sc, padconf->reg_off);
322 /* check to make sure the pins is configured as GPIO in the first state */
323 if ((reg_val & ti_pinmux_dev->padconf_muxmode_mask) != padconf->gpio_mode)
326 /* read and store the reset of the state, i.e. pull-up, pull-down, etc */
328 *state = (reg_val & ti_pinmux_dev->padconf_sate_mask);
334 ti_pinmux_configure_pins(device_t dev, phandle_t cfgxref)
336 struct pincfg *cfgtuples, *cfg;
339 static struct ti_pinmux_softc *sc;
341 sc = device_get_softc(dev);
342 cfgnode = OF_node_from_xref(cfgxref);
343 ntuples = OF_getencprop_alloc(cfgnode, "pinctrl-single,pins", sizeof(*cfgtuples),
344 (void **)&cfgtuples);
350 return (0); /* Empty property is not an error. */
352 for (i = 0, cfg = cfgtuples; i < ntuples; i++, cfg++) {
355 OF_getprop(cfgnode, "name", &name, sizeof(name));
356 printf("%16s: muxreg 0x%04x muxval 0x%02x\n",
357 name, cfg->reg, cfg->conf);
360 /* write the register value (16-bit writes) */
361 ti_pinmux_write_2(sc, cfg->reg, cfg->conf);
364 OF_prop_free(cfgtuples);
370 * Device part of OMAP SCM driver
374 ti_pinmux_probe(device_t dev)
376 if (!ofw_bus_status_okay(dev))
379 if (!ofw_bus_is_compatible(dev, "pinctrl-single"))
383 printf("%s: multiple pinctrl modules in device tree data, ignoring\n",
390 ti_pinmux_dev = &omap4_pinmux_dev;
395 ti_pinmux_dev = &ti_am335x_pinmux_dev;
399 printf("Unknown CPU in pinmux\n");
404 device_set_desc(dev, "TI Pinmux Module");
405 return (BUS_PROBE_DEFAULT);
409 * ti_pinmux_attach - attaches the pinmux to the simplebus
413 * Zero on success or ENXIO if an error occuried.
416 ti_pinmux_attach(device_t dev)
418 struct ti_pinmux_softc *sc = device_get_softc(dev);
427 if (bus_alloc_resources(dev, ti_pinmux_res_spec, sc->sc_res)) {
428 device_printf(dev, "could not allocate resources\n");
432 sc->sc_bst = rman_get_bustag(sc->sc_res[0]);
433 sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]);
435 if (ti_pinmux_sc == NULL)
438 fdt_pinctrl_register(dev, "pinctrl-single,pins");
439 fdt_pinctrl_configure_tree(dev);
444 static device_method_t ti_pinmux_methods[] = {
445 DEVMETHOD(device_probe, ti_pinmux_probe),
446 DEVMETHOD(device_attach, ti_pinmux_attach),
448 /* fdt_pinctrl interface */
449 DEVMETHOD(fdt_pinctrl_configure, ti_pinmux_configure_pins),
453 static driver_t ti_pinmux_driver = {
456 sizeof(struct ti_pinmux_softc),
459 static devclass_t ti_pinmux_devclass;
461 DRIVER_MODULE(ti_pinmux, simplebus, ti_pinmux_driver, ti_pinmux_devclass, 0, 0);