exynos_pinctrl.c revision 1.8 1 1.8 marty /* $NetBSD: exynos_pinctrl.c,v 1.8 2015/12/30 04:30:27 marty Exp $ */
2 1.1 marty
3 1.1 marty /*-
4 1.1 marty * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 marty * All rights reserved.
6 1.1 marty *
7 1.1 marty * This code is derived from software contributed to The NetBSD Foundation
8 1.1 marty * by Marty Fouts
9 1.1 marty *
10 1.1 marty * Redistribution and use in source and binary forms, with or without
11 1.1 marty * modification, are permitted provided that the following conditions
12 1.1 marty * are met:
13 1.1 marty * 1. Redistributions of source code must retain the above copyright
14 1.1 marty * notice, this list of conditions and the following disclaimer.
15 1.1 marty * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 marty * notice, this list of conditions and the following disclaimer in the
17 1.1 marty * documentation and/or other materials provided with the distribution.
18 1.1 marty *
19 1.1 marty * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 marty * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 marty * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 marty * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 marty * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 marty * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 marty * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 marty * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 marty * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 marty * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 marty * POSSIBILITY OF SUCH DAMAGE.
30 1.1 marty */
31 1.1 marty
32 1.1 marty #include "opt_exynos.h"
33 1.1 marty #include "opt_arm_debug.h"
34 1.1 marty #include "gpio.h"
35 1.1 marty
36 1.1 marty #include <sys/cdefs.h>
37 1.8 marty __KERNEL_RCSID(1, "$NetBSD: exynos_pinctrl.c,v 1.8 2015/12/30 04:30:27 marty Exp $");
38 1.1 marty
39 1.1 marty #include <sys/param.h>
40 1.1 marty #include <sys/bus.h>
41 1.1 marty #include <sys/device.h>
42 1.1 marty #include <sys/intr.h>
43 1.1 marty #include <sys/systm.h>
44 1.1 marty #include <sys/kmem.h>
45 1.1 marty #include <sys/gpio.h>
46 1.1 marty
47 1.1 marty #include <dev/gpio/gpiovar.h>
48 1.1 marty
49 1.1 marty #include <arm/samsung/exynos_reg.h>
50 1.3 marty #include <arm/samsung/exynos_var.h>
51 1.1 marty #include <arm/samsung/exynos_intr.h>
52 1.1 marty #include <arm/samsung/exynos_pinctrl.h>
53 1.1 marty
54 1.1 marty #include <dev/fdt/fdtvar.h>
55 1.1 marty
56 1.1 marty static int exynos_pinctrl_match(device_t, cfdata_t, void *);
57 1.1 marty static void exynos_pinctrl_attach(device_t, device_t, void *);
58 1.1 marty
59 1.8 marty static void *exynos_pinctrl_acquire(device_t, const char *);
60 1.8 marty static void exynos_pinctrl_release(device_t, void *);
61 1.8 marty static void exynos_pinctrl_get_cfg(struct fdtbus_pinctrl_pin *, void *);
62 1.8 marty static void exynos_pinctrl_set_cfg(struct fdtbus_pinctrl_pin *, void *);
63 1.8 marty
64 1.8 marty static struct fdtbus_pinctrl_controller_func exynos_pinctrl_controller_func = {
65 1.8 marty .acquire = exynos_pinctrl_acquire,
66 1.8 marty .release = exynos_pinctrl_release,
67 1.8 marty .get = exynos_pinctrl_get_cfg,
68 1.8 marty .set = exynos_pinctrl_set_cfg,
69 1.8 marty };
70 1.8 marty
71 1.1 marty CFATTACH_DECL_NEW(exynos_pinctrl, sizeof(struct exynos_pinctrl_softc),
72 1.1 marty exynos_pinctrl_match, exynos_pinctrl_attach, NULL, NULL);
73 1.1 marty
74 1.1 marty static int
75 1.1 marty exynos_pinctrl_match(device_t parent, cfdata_t cf, void *aux)
76 1.1 marty {
77 1.7 jmcneill const char * const compatible[] = { "samsung,exynos5420-pinctrl",
78 1.1 marty NULL };
79 1.1 marty struct fdt_attach_args * const faa = aux;
80 1.1 marty return of_match_compatible(faa->faa_phandle, compatible);
81 1.1 marty }
82 1.1 marty
83 1.1 marty static void
84 1.1 marty exynos_pinctrl_attach(device_t parent, device_t self, void *aux)
85 1.1 marty {
86 1.1 marty struct exynos_pinctrl_softc * const sc
87 1.1 marty = kmem_zalloc(sizeof(*sc), KM_SLEEP);
88 1.1 marty struct fdt_attach_args * const faa = aux;
89 1.8 marty struct exynos_gpio_softc *child_sc;
90 1.1 marty bus_addr_t addr;
91 1.1 marty bus_size_t size;
92 1.1 marty int error;
93 1.1 marty int child;
94 1.1 marty
95 1.1 marty if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
96 1.1 marty aprint_error(": couldn't get registers\n");
97 1.1 marty return;
98 1.1 marty }
99 1.1 marty
100 1.4 marty aprint_normal(" pinctl @ 0x%08x ", (uint)addr);
101 1.1 marty sc->sc_dev = self;
102 1.1 marty sc->sc_bst = faa->faa_bst;
103 1.1 marty error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
104 1.1 marty if (error) {
105 1.1 marty aprint_error(": couldn't map %#llx: %d",
106 1.1 marty (uint64_t)addr, error);
107 1.1 marty return;
108 1.1 marty }
109 1.1 marty
110 1.7 jmcneill aprint_naive("\n");
111 1.7 jmcneill aprint_normal("\n");
112 1.7 jmcneill
113 1.1 marty for (child = OF_child(faa->faa_phandle); child;
114 1.1 marty child = OF_peer(child)) {
115 1.5 jmcneill if (of_getprop_bool(child, "gpio-controller") == false)
116 1.1 marty continue;
117 1.8 marty child_sc = exynos_gpio_bank_config(sc, faa, child);
118 1.8 marty fdtbus_register_pinctrl_controller(child_sc->sc_dev, child,
119 1.8 marty &exynos_pinctrl_controller_func);
120 1.1 marty }
121 1.1 marty }
122 1.8 marty
123 1.8 marty
124 1.8 marty static void *exynos_pinctrl_acquire(device_t self, const char *name)
125 1.8 marty {
126 1.8 marty return exynos_gpio_bank_lookup(name);
127 1.8 marty }
128 1.8 marty
129 1.8 marty static void exynos_pinctrl_release(device_t self, void *cookie)
130 1.8 marty {
131 1.8 marty }
132 1.8 marty
133 1.8 marty static void exynos_pinctrl_get_cfg(struct fdtbus_pinctrl_pin *pin,
134 1.8 marty void *cookie)
135 1.8 marty {
136 1.8 marty struct exynos_gpio_bank *bank = pin->pp_priv;
137 1.8 marty struct exynos_gpio_pin_cfg *cfg = cookie;
138 1.8 marty struct exynos_gpio_pin_cfg **cfgp = &cfg;
139 1.8 marty struct exynos_gpio_pin_cfg *newcfg = kmem_zalloc(sizeof(*newcfg),
140 1.8 marty KM_SLEEP);
141 1.8 marty if (newcfg == NULL)
142 1.8 marty return;
143 1.8 marty exynos_gpio_pin_ctl_read(bank, newcfg);
144 1.8 marty *cfgp = newcfg;
145 1.8 marty return;
146 1.8 marty }
147 1.8 marty
148 1.8 marty static void exynos_pinctrl_set_cfg(struct fdtbus_pinctrl_pin *pin,
149 1.8 marty void *cookie)
150 1.8 marty {
151 1.8 marty struct exynos_gpio_bank *bank = pin->pp_priv;
152 1.8 marty struct exynos_gpio_pin_cfg *cfg = cookie;
153 1.8 marty exynos_gpio_pin_ctl_write(bank, cfg);
154 1.8 marty }
155