2 * Copyright 2015 Alexander Kabaev <kan@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Ingenic JZ4780 pinctrl driver.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/queue.h>
44 #include <sys/resource.h>
47 #include <machine/bus.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/fdt/fdt_common.h>
53 #include <dev/fdt/fdt_pinctrl.h>
54 #include <dev/fdt/simplebus.h>
56 #include <mips/ingenic/jz4780_regs.h>
58 #include "jz4780_gpio_if.h"
60 struct jz4780_pinctrl_softc {
61 struct simplebus_softc ssc;
65 #define CHIP_REG_STRIDE 256
66 #define CHIP_REG_OFFSET(base, chip) ((base) + (chip) * CHIP_REG_STRIDE)
69 jz4780_pinctrl_probe(device_t dev)
72 if (!ofw_bus_status_okay(dev))
75 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-pinctrl"))
78 device_set_desc(dev, "Ingenic JZ4780 GPIO");
80 return (BUS_PROBE_DEFAULT);
84 jz4780_pinctrl_attach(device_t dev)
86 struct jz4780_pinctrl_softc *sc;
87 struct resource_list *rs;
88 struct resource_list_entry *re;
89 phandle_t dt_parent, dt_child;
92 sc = device_get_softc(dev);
96 * Fetch our own resource list to dole memory between children
98 rs = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev);
101 re = resource_list_find(rs, SYS_RES_MEMORY, 0);
105 simplebus_init(dev, 0);
107 /* Iterate over this node children, looking for pin controllers */
108 dt_parent = ofw_bus_get_node(dev);
110 for (dt_child = OF_child(dt_parent); dt_child != 0;
111 dt_child = OF_peer(dt_child)) {
112 struct simplebus_devinfo *ndi;
117 /* Add gpio controller child */
118 if (!OF_hasprop(dt_child, "gpio-controller"))
120 child = simplebus_add_device(dev, dt_child, 0, NULL, -1, NULL);
123 /* Setup child resources */
124 phys = CHIP_REG_OFFSET(re->start, i);
125 size = CHIP_REG_STRIDE;
126 if (phys + size - 1 <= re->end) {
127 ndi = device_get_ivars(child);
128 resource_list_add(&ndi->rl, SYS_RES_MEMORY, 0,
129 phys, phys + size - 1, size);
134 ret = bus_generic_attach(dev);
136 fdt_pinctrl_register(dev, "ingenic,pins");
137 fdt_pinctrl_configure_tree(dev);
143 jz4780_pinctrl_detach(device_t dev)
146 bus_generic_detach(dev);
150 struct jx4780_bias_prop {
155 static struct jx4780_bias_prop jx4780_bias_table[] = {
156 { "bias-disable", 0 },
157 { "bias-pull-up", GPIO_PIN_PULLUP },
158 { "bias-pull-down", GPIO_PIN_PULLDOWN },
162 jz4780_pinctrl_parse_pincfg(phandle_t pincfgxref, uint32_t *bias_value)
164 phandle_t pincfg_node;
167 pincfg_node = OF_node_from_xref(pincfgxref);
168 for (i = 0; i < nitems(jx4780_bias_table); i++) {
169 if (OF_hasprop(pincfg_node, jx4780_bias_table[i].name)) {
170 *bias_value = jx4780_bias_table[i].bias;
179 jz4780_pinctrl_chip_lookup(struct jz4780_pinctrl_softc *sc, phandle_t chipxref)
183 chipdev = OF_device_from_xref(chipxref);
188 jz4780_pinctrl_configure_pins(device_t dev, phandle_t cfgxref)
190 struct jz4780_pinctrl_softc *sc = device_get_softc(dev);
194 uint32_t *value, *pconf;
197 node = OF_node_from_xref(cfgxref);
199 len = OF_getencprop_alloc_multi(node, "ingenic,pins",
200 sizeof(uint32_t) * 4, (void **)&value);
203 "missing ingenic,pins attribute in FDT\n");
209 for (i = 0; i < len; i++, pconf += 4) {
212 /* Lookup the chip that handles this configuration */
213 chip = jz4780_pinctrl_chip_lookup(sc, pconf[0]);
216 "invalid gpio controller reference in FDT\n");
220 if (jz4780_pinctrl_parse_pincfg(pconf[3], &bias) != 0) {
222 "invalid pin bias for pin %u on %s in FDT\n",
223 pconf[1], ofw_bus_get_name(chip));
227 result = JZ4780_GPIO_CONFIGURE_PIN(chip, pconf[1], pconf[2],
231 "failed to configure pin %u on %s\n", pconf[1],
232 ofw_bus_get_name(chip));
239 free(value, M_OFWPROP);
244 static device_method_t jz4780_pinctrl_methods[] = {
245 /* Device interface */
246 DEVMETHOD(device_probe, jz4780_pinctrl_probe),
247 DEVMETHOD(device_attach, jz4780_pinctrl_attach),
248 DEVMETHOD(device_detach, jz4780_pinctrl_detach),
250 /* fdt_pinctrl interface */
251 DEVMETHOD(fdt_pinctrl_configure, jz4780_pinctrl_configure_pins),
256 static devclass_t jz4780_pinctrl_devclass;
257 DEFINE_CLASS_1(pinctrl, jz4780_pinctrl_driver, jz4780_pinctrl_methods,
258 sizeof(struct jz4780_pinctrl_softc), simplebus_driver);
259 EARLY_DRIVER_MODULE(pinctrl, simplebus, jz4780_pinctrl_driver,
260 jz4780_pinctrl_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);