1*50ad5627Sriastradh /* $NetBSD: exynos_pinctrl.c,v 1.22 2022/02/11 23:48:41 riastradh Exp $ */
2645518d8Smarty
3645518d8Smarty /*-
4f45e91efSskrll * Copyright (c) 2015, 2020 The NetBSD Foundation, Inc.
5645518d8Smarty * All rights reserved.
6645518d8Smarty *
7645518d8Smarty * This code is derived from software contributed to The NetBSD Foundation
8f45e91efSskrll * by Marty Fouts, and by Nick Hudson
9645518d8Smarty *
10645518d8Smarty * Redistribution and use in source and binary forms, with or without
11645518d8Smarty * modification, are permitted provided that the following conditions
12645518d8Smarty * are met:
13645518d8Smarty * 1. Redistributions of source code must retain the above copyright
14645518d8Smarty * notice, this list of conditions and the following disclaimer.
15645518d8Smarty * 2. Redistributions in binary form must reproduce the above copyright
16645518d8Smarty * notice, this list of conditions and the following disclaimer in the
17645518d8Smarty * documentation and/or other materials provided with the distribution.
18645518d8Smarty *
19645518d8Smarty * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20645518d8Smarty * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21645518d8Smarty * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22645518d8Smarty * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23645518d8Smarty * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24645518d8Smarty * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25645518d8Smarty * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26645518d8Smarty * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27645518d8Smarty * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28645518d8Smarty * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29645518d8Smarty * POSSIBILITY OF SUCH DAMAGE.
30645518d8Smarty */
31645518d8Smarty
32645518d8Smarty #include "opt_exynos.h"
33645518d8Smarty #include "opt_arm_debug.h"
34645518d8Smarty #include "gpio.h"
35645518d8Smarty
36645518d8Smarty #include <sys/cdefs.h>
37*50ad5627Sriastradh __KERNEL_RCSID(1, "$NetBSD: exynos_pinctrl.c,v 1.22 2022/02/11 23:48:41 riastradh Exp $");
38645518d8Smarty
39645518d8Smarty #include <sys/param.h>
40645518d8Smarty #include <sys/bus.h>
41645518d8Smarty #include <sys/device.h>
42645518d8Smarty #include <sys/intr.h>
43645518d8Smarty #include <sys/systm.h>
44645518d8Smarty #include <sys/gpio.h>
45645518d8Smarty
46645518d8Smarty #include <dev/gpio/gpiovar.h>
47645518d8Smarty
48645518d8Smarty #include <arm/samsung/exynos_reg.h>
494abb0952Smarty #include <arm/samsung/exynos_var.h>
50645518d8Smarty #include <arm/samsung/exynos_intr.h>
51645518d8Smarty #include <arm/samsung/exynos_pinctrl.h>
52645518d8Smarty
53645518d8Smarty #include <dev/fdt/fdtvar.h>
54645518d8Smarty
55aab26c17Smarty struct exynos_pinctrl_config {
56aab26c17Smarty int pc_phandle;
577fa4406cSjmcneill struct exynos_gpio_pin_cfg pc_pincfg;
58aab26c17Smarty struct exynos_pinctrl_softc *pc_sc;
59aab26c17Smarty };
60aab26c17Smarty
61645518d8Smarty static int exynos_pinctrl_match(device_t, cfdata_t, void *);
62645518d8Smarty static void exynos_pinctrl_attach(device_t, device_t, void *);
63645518d8Smarty
647fa4406cSjmcneill static int exynos_pinctrl_set_cfg(device_t, const void *, size_t);
657fa4406cSjmcneill static void exynos_parse_config(int, struct exynos_gpio_pin_cfg *);
669c676a63Smarty
679c676a63Smarty static struct fdtbus_pinctrl_controller_func exynos_pinctrl_controller_func = {
68aab26c17Smarty .set_config = exynos_pinctrl_set_cfg
699c676a63Smarty };
709c676a63Smarty
71645518d8Smarty CFATTACH_DECL_NEW(exynos_pinctrl, sizeof(struct exynos_pinctrl_softc),
72645518d8Smarty exynos_pinctrl_match, exynos_pinctrl_attach, NULL, NULL);
73645518d8Smarty
74f45e91efSskrll
75646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
76646c0f59Sthorpej { .compat = "samsung,exynos5410-pinctrl",
77646c0f59Sthorpej .data = &exynos5410_pinctrl_banks },
78646c0f59Sthorpej { .compat = "samsung,exynos5420-pinctrl",
79646c0f59Sthorpej .data = &exynos5420_pinctrl_banks },
80646c0f59Sthorpej
81ec189949Sthorpej DEVICE_COMPAT_EOL
82f45e91efSskrll };
83f45e91efSskrll
84645518d8Smarty static int
exynos_pinctrl_match(device_t parent,cfdata_t cf,void * aux)85645518d8Smarty exynos_pinctrl_match(device_t parent, cfdata_t cf, void *aux)
86645518d8Smarty {
87645518d8Smarty struct fdt_attach_args * const faa = aux;
88f45e91efSskrll
896e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
90645518d8Smarty }
91645518d8Smarty
92645518d8Smarty static void
exynos_pinctrl_attach(device_t parent,device_t self,void * aux)93645518d8Smarty exynos_pinctrl_attach(device_t parent, device_t self, void *aux)
94645518d8Smarty {
95*50ad5627Sriastradh struct exynos_pinctrl_softc * const sc = device_private(self);
96645518d8Smarty struct fdt_attach_args * const faa = aux;
97645518d8Smarty bus_addr_t addr;
98645518d8Smarty bus_size_t size;
99645518d8Smarty int error;
100645518d8Smarty int child;
101645518d8Smarty
102645518d8Smarty if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
103645518d8Smarty aprint_error(": couldn't get registers\n");
104645518d8Smarty return;
105645518d8Smarty }
106645518d8Smarty
107f45e91efSskrll aprint_normal(" pinctrl @ 0x%08x ", (uint)addr);
108645518d8Smarty sc->sc_dev = self;
109645518d8Smarty sc->sc_bst = faa->faa_bst;
1106e54367aSthorpej sc->sc_epb = of_compatible_lookup(faa->faa_phandle, compat_data)->data;
111f45e91efSskrll
112645518d8Smarty error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
113645518d8Smarty if (error) {
114d3f45d9fSskrll aprint_error(": couldn't map %#" PRIxBUSADDR ": %d",
115d3f45d9fSskrll addr, error);
116645518d8Smarty return;
117645518d8Smarty }
118645518d8Smarty
119e2450495Sjmcneill aprint_naive("\n");
120e2450495Sjmcneill aprint_normal("\n");
121e2450495Sjmcneill
122645518d8Smarty for (child = OF_child(faa->faa_phandle); child;
123645518d8Smarty child = OF_peer(child)) {
124aab26c17Smarty
1256f008dd0Sjmcneill if (of_hasprop(child, "gpio-controller")) {
126aab26c17Smarty exynos_gpio_bank_config(sc, faa, child);
127aab26c17Smarty }
128aab26c17Smarty
1296f008dd0Sjmcneill if (of_hasprop(child, "samsung,pins")) {
1307fa4406cSjmcneill fdtbus_register_pinctrl_config(self, child,
1319c676a63Smarty &exynos_pinctrl_controller_func);
132645518d8Smarty }
133645518d8Smarty }
1349c676a63Smarty }
1359c676a63Smarty
1367fa4406cSjmcneill static void
exynos_parse_config(int phandle,struct exynos_gpio_pin_cfg * gc)1377fa4406cSjmcneill exynos_parse_config(int phandle, struct exynos_gpio_pin_cfg *gc)
1389c676a63Smarty {
1396f008dd0Sjmcneill gc->cfg_valid = of_getprop_uint32(phandle, "samsung,pin-function", &gc->cfg) == 0;
1406f008dd0Sjmcneill gc->pud_valid = of_getprop_uint32(phandle, "samsung,pin-pud", &gc->pud) == 0;
1416f008dd0Sjmcneill gc->drv_valid = of_getprop_uint32(phandle, "samsung,pin-drv", &gc->drv) == 0;
1426f008dd0Sjmcneill gc->conpwd_valid = of_getprop_uint32(phandle, "samsung,pin-conpwd", &gc->conpwd) == 0;
1436f008dd0Sjmcneill gc->pudpwd_valid = of_getprop_uint32(phandle, "samsung,pin-pudpwd", &gc->pudpwd) == 0;
1446f008dd0Sjmcneill }
1456f008dd0Sjmcneill
1466f008dd0Sjmcneill static int
exynos_parse_pin(const char * pinname)1476f008dd0Sjmcneill exynos_parse_pin(const char *pinname)
1486f008dd0Sjmcneill {
1496f008dd0Sjmcneill
1506f008dd0Sjmcneill const int len = strlen(pinname);
1516f008dd0Sjmcneill
1526f008dd0Sjmcneill if (len == 0)
1536f008dd0Sjmcneill return -1;
1546f008dd0Sjmcneill
1556f008dd0Sjmcneill if (pinname[len - 1] < '0' || pinname[len - 1] > '9')
1566f008dd0Sjmcneill return -1;
1576f008dd0Sjmcneill
1586f008dd0Sjmcneill return pinname[len - 1] - '0';
159aab26c17Smarty }
160aab26c17Smarty
161aab26c17Smarty static int
exynos_do_config(struct exynos_pinctrl_config * pc)162aab26c17Smarty exynos_do_config(struct exynos_pinctrl_config *pc)
1639c676a63Smarty {
1647fa4406cSjmcneill struct exynos_gpio_pin_cfg *gc = &pc->pc_pincfg;
165646c0f59Sthorpej const struct exynos_pinctrl_banks *epb = pc->pc_sc->sc_epb;
166aab26c17Smarty struct exynos_gpio_bank *bank;
1677fa4406cSjmcneill const char *pins;
1686f008dd0Sjmcneill int pin;
169aab26c17Smarty
1707fa4406cSjmcneill int pins_len = OF_getproplen(pc->pc_phandle, "samsung,pins");
1717fa4406cSjmcneill if (pins_len <= 0)
172aab26c17Smarty return -1;
173aab26c17Smarty
1747fa4406cSjmcneill for (pins = fdtbus_get_string(pc->pc_phandle, "samsung,pins");
1757fa4406cSjmcneill pins_len > 0;
1767fa4406cSjmcneill pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
177f45e91efSskrll bank = exynos_gpio_bank_lookup(epb, pins);
1786f008dd0Sjmcneill pin = exynos_parse_pin(pins);
1797fa4406cSjmcneill if (bank == NULL) {
1807fa4406cSjmcneill aprint_error_dev(pc->pc_sc->sc_dev,
1817fa4406cSjmcneill "unknown pin name '%s'\n", pins);
1827fa4406cSjmcneill continue;
183aab26c17Smarty }
1846f008dd0Sjmcneill exynos_gpio_pin_ctl_write(bank, gc, pin);
1857fa4406cSjmcneill }
1867fa4406cSjmcneill
187aab26c17Smarty return 0;
188aab26c17Smarty }
189aab26c17Smarty
190aab26c17Smarty static int
exynos_pinctrl_set_cfg(device_t dev,const void * data,size_t len)1917fa4406cSjmcneill exynos_pinctrl_set_cfg(device_t dev, const void *data, size_t len)
192aab26c17Smarty {
1937fa4406cSjmcneill struct exynos_pinctrl_config pc;
1947fa4406cSjmcneill
1957fa4406cSjmcneill if (len != 4)
1967fa4406cSjmcneill return -1;
1977fa4406cSjmcneill
1987fa4406cSjmcneill pc.pc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
1997fa4406cSjmcneill pc.pc_sc = device_private(dev);
2007fa4406cSjmcneill exynos_parse_config(pc.pc_phandle, &pc.pc_pincfg);
2017fa4406cSjmcneill
2027fa4406cSjmcneill return exynos_do_config(&pc);
2039c676a63Smarty }
204