1b1170c42SJianlong Huang // SPDX-License-Identifier: GPL-2.0
2b1170c42SJianlong Huang /*
3b1170c42SJianlong Huang * Pinctrl / GPIO driver for StarFive JH7110 SoC aon controller
4b1170c42SJianlong Huang *
5b1170c42SJianlong Huang * Copyright (C) 2022 StarFive Technology Co., Ltd.
6b1170c42SJianlong Huang */
7b1170c42SJianlong Huang
8b1170c42SJianlong Huang #include <linux/err.h>
9b1170c42SJianlong Huang #include <linux/gpio/driver.h>
10b1170c42SJianlong Huang #include <linux/init.h>
11b1170c42SJianlong Huang #include <linux/interrupt.h>
12b1170c42SJianlong Huang #include <linux/io.h>
13060f03e9SRob Herring #include <linux/mod_devicetable.h>
14b1170c42SJianlong Huang #include <linux/module.h>
15b1170c42SJianlong Huang #include <linux/pinctrl/pinconf.h>
16b1170c42SJianlong Huang #include <linux/pinctrl/pinconf-generic.h>
17b1170c42SJianlong Huang #include <linux/pinctrl/pinctrl.h>
18b1170c42SJianlong Huang #include <linux/pinctrl/pinmux.h>
19b1170c42SJianlong Huang #include <linux/platform_device.h>
20b1170c42SJianlong Huang #include <linux/pm_runtime.h>
21b1170c42SJianlong Huang #include <linux/regmap.h>
22b1170c42SJianlong Huang #include <linux/slab.h>
23b1170c42SJianlong Huang
24b1170c42SJianlong Huang #include <dt-bindings/pinctrl/starfive,jh7110-pinctrl.h>
25b1170c42SJianlong Huang
26b1170c42SJianlong Huang #include "../core.h"
27b1170c42SJianlong Huang #include "../pinconf.h"
28b1170c42SJianlong Huang #include "../pinmux.h"
29b1170c42SJianlong Huang #include "pinctrl-starfive-jh7110.h"
30b1170c42SJianlong Huang
31b1170c42SJianlong Huang #define JH7110_AON_NGPIO 4
32b1170c42SJianlong Huang #define JH7110_AON_GC_BASE 64
33b1170c42SJianlong Huang
34*64061b67SHal Feng #define JH7110_AON_REGS_NUM 37
35*64061b67SHal Feng
36b1170c42SJianlong Huang /* registers */
37b1170c42SJianlong Huang #define JH7110_AON_DOEN 0x0
38b1170c42SJianlong Huang #define JH7110_AON_DOUT 0x4
39b1170c42SJianlong Huang #define JH7110_AON_GPI 0x8
40b1170c42SJianlong Huang #define JH7110_AON_GPIOIN 0x2c
41b1170c42SJianlong Huang
42b1170c42SJianlong Huang #define JH7110_AON_GPIOEN 0xc
43b1170c42SJianlong Huang #define JH7110_AON_GPIOIS 0x10
44b1170c42SJianlong Huang #define JH7110_AON_GPIOIC 0x14
45b1170c42SJianlong Huang #define JH7110_AON_GPIOIBE 0x18
46b1170c42SJianlong Huang #define JH7110_AON_GPIOIEV 0x1c
47b1170c42SJianlong Huang #define JH7110_AON_GPIOIE 0x20
48b1170c42SJianlong Huang #define JH7110_AON_GPIORIS 0x28
49b1170c42SJianlong Huang #define JH7110_AON_GPIOMIS 0x28
50b1170c42SJianlong Huang
51b1170c42SJianlong Huang #define JH7110_AON_GPO_PDA_0_5_CFG 0x30
52b1170c42SJianlong Huang
53b1170c42SJianlong Huang static const struct pinctrl_pin_desc jh7110_aon_pins[] = {
54b1170c42SJianlong Huang PINCTRL_PIN(PAD_TESTEN, "TESTEN"),
55b1170c42SJianlong Huang PINCTRL_PIN(PAD_RGPIO0, "RGPIO0"),
56b1170c42SJianlong Huang PINCTRL_PIN(PAD_RGPIO1, "RGPIO1"),
57b1170c42SJianlong Huang PINCTRL_PIN(PAD_RGPIO2, "RGPIO2"),
58b1170c42SJianlong Huang PINCTRL_PIN(PAD_RGPIO3, "RGPIO3"),
59b1170c42SJianlong Huang PINCTRL_PIN(PAD_RSTN, "RSTN"),
60b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_MDC, "GMAC0_MDC"),
61b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_MDIO, "GMAC0_MDIO"),
62b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_RXD0, "GMAC0_RXD0"),
63b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_RXD1, "GMAC0_RXD1"),
64b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_RXD2, "GMAC0_RXD2"),
65b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_RXD3, "GMAC0_RXD3"),
66b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_RXDV, "GMAC0_RXDV"),
67b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_RXC, "GMAC0_RXC"),
68b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_TXD0, "GMAC0_TXD0"),
69b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_TXD1, "GMAC0_TXD1"),
70b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_TXD2, "GMAC0_TXD2"),
71b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_TXD3, "GMAC0_TXD3"),
72b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_TXEN, "GMAC0_TXEN"),
73b1170c42SJianlong Huang PINCTRL_PIN(PAD_GMAC0_TXC, "GMAC0_TXC"),
74b1170c42SJianlong Huang };
75b1170c42SJianlong Huang
jh7110_aon_set_one_pin_mux(struct jh7110_pinctrl * sfp,unsigned int pin,unsigned int din,u32 dout,u32 doen,u32 func)76b1170c42SJianlong Huang static int jh7110_aon_set_one_pin_mux(struct jh7110_pinctrl *sfp,
77b1170c42SJianlong Huang unsigned int pin,
78b1170c42SJianlong Huang unsigned int din, u32 dout,
79b1170c42SJianlong Huang u32 doen, u32 func)
80b1170c42SJianlong Huang {
81b1170c42SJianlong Huang if (pin < sfp->gc.ngpio && func == 0)
82b1170c42SJianlong Huang jh7110_set_gpiomux(sfp, pin, din, dout, doen);
83b1170c42SJianlong Huang
84b1170c42SJianlong Huang return 0;
85b1170c42SJianlong Huang }
86b1170c42SJianlong Huang
jh7110_aon_get_padcfg_base(struct jh7110_pinctrl * sfp,unsigned int pin)87b1170c42SJianlong Huang static int jh7110_aon_get_padcfg_base(struct jh7110_pinctrl *sfp,
88b1170c42SJianlong Huang unsigned int pin)
89b1170c42SJianlong Huang {
90b1170c42SJianlong Huang if (pin < PAD_GMAC0_MDC)
91b1170c42SJianlong Huang return JH7110_AON_GPO_PDA_0_5_CFG;
92b1170c42SJianlong Huang
93b1170c42SJianlong Huang return -1;
94b1170c42SJianlong Huang }
95b1170c42SJianlong Huang
jh7110_aon_irq_handler(struct irq_desc * desc)96b1170c42SJianlong Huang static void jh7110_aon_irq_handler(struct irq_desc *desc)
97b1170c42SJianlong Huang {
98b1170c42SJianlong Huang struct jh7110_pinctrl *sfp = jh7110_from_irq_desc(desc);
99b1170c42SJianlong Huang struct irq_chip *chip = irq_desc_get_chip(desc);
100b1170c42SJianlong Huang unsigned long mis;
101b1170c42SJianlong Huang unsigned int pin;
102b1170c42SJianlong Huang
103b1170c42SJianlong Huang chained_irq_enter(chip, desc);
104b1170c42SJianlong Huang
105b1170c42SJianlong Huang mis = readl_relaxed(sfp->base + JH7110_AON_GPIOMIS);
106b1170c42SJianlong Huang for_each_set_bit(pin, &mis, JH7110_AON_NGPIO)
107b1170c42SJianlong Huang generic_handle_domain_irq(sfp->gc.irq.domain, pin);
108b1170c42SJianlong Huang
109b1170c42SJianlong Huang chained_irq_exit(chip, desc);
110b1170c42SJianlong Huang }
111b1170c42SJianlong Huang
jh7110_aon_init_hw(struct gpio_chip * gc)112b1170c42SJianlong Huang static int jh7110_aon_init_hw(struct gpio_chip *gc)
113b1170c42SJianlong Huang {
114b1170c42SJianlong Huang struct jh7110_pinctrl *sfp = container_of(gc,
115b1170c42SJianlong Huang struct jh7110_pinctrl, gc);
116b1170c42SJianlong Huang
117b1170c42SJianlong Huang /* mask all GPIO interrupts */
118b1170c42SJianlong Huang writel_relaxed(0, sfp->base + JH7110_AON_GPIOIE);
119b1170c42SJianlong Huang /* clear edge interrupt flags */
120b1170c42SJianlong Huang writel_relaxed(0, sfp->base + JH7110_AON_GPIOIC);
121b1170c42SJianlong Huang writel_relaxed(0x0f, sfp->base + JH7110_AON_GPIOIC);
122b1170c42SJianlong Huang /* enable GPIO interrupts */
123b1170c42SJianlong Huang writel_relaxed(1, sfp->base + JH7110_AON_GPIOEN);
124b1170c42SJianlong Huang return 0;
125b1170c42SJianlong Huang }
126b1170c42SJianlong Huang
127b1170c42SJianlong Huang static const struct jh7110_gpio_irq_reg jh7110_aon_irq_reg = {
128b1170c42SJianlong Huang .is_reg_base = JH7110_AON_GPIOIS,
129b1170c42SJianlong Huang .ic_reg_base = JH7110_AON_GPIOIC,
130b1170c42SJianlong Huang .ibe_reg_base = JH7110_AON_GPIOIBE,
131b1170c42SJianlong Huang .iev_reg_base = JH7110_AON_GPIOIEV,
132b1170c42SJianlong Huang .ie_reg_base = JH7110_AON_GPIOIE,
133b1170c42SJianlong Huang .ris_reg_base = JH7110_AON_GPIORIS,
134b1170c42SJianlong Huang .mis_reg_base = JH7110_AON_GPIOMIS,
135b1170c42SJianlong Huang };
136b1170c42SJianlong Huang
137b1170c42SJianlong Huang static const struct jh7110_pinctrl_soc_info jh7110_aon_pinctrl_info = {
138b1170c42SJianlong Huang .pins = jh7110_aon_pins,
139b1170c42SJianlong Huang .npins = ARRAY_SIZE(jh7110_aon_pins),
140b1170c42SJianlong Huang .ngpios = JH7110_AON_NGPIO,
141b1170c42SJianlong Huang .gc_base = JH7110_AON_GC_BASE,
142b1170c42SJianlong Huang .dout_reg_base = JH7110_AON_DOUT,
143b1170c42SJianlong Huang .dout_mask = GENMASK(3, 0),
144b1170c42SJianlong Huang .doen_reg_base = JH7110_AON_DOEN,
145b1170c42SJianlong Huang .doen_mask = GENMASK(2, 0),
146b1170c42SJianlong Huang .gpi_reg_base = JH7110_AON_GPI,
147b1170c42SJianlong Huang .gpi_mask = GENMASK(3, 0),
148b1170c42SJianlong Huang .gpioin_reg_base = JH7110_AON_GPIOIN,
149b1170c42SJianlong Huang .irq_reg = &jh7110_aon_irq_reg,
150*64061b67SHal Feng .nsaved_regs = JH7110_AON_REGS_NUM,
151b1170c42SJianlong Huang .jh7110_set_one_pin_mux = jh7110_aon_set_one_pin_mux,
152b1170c42SJianlong Huang .jh7110_get_padcfg_base = jh7110_aon_get_padcfg_base,
153b1170c42SJianlong Huang .jh7110_gpio_irq_handler = jh7110_aon_irq_handler,
154b1170c42SJianlong Huang .jh7110_gpio_init_hw = jh7110_aon_init_hw,
155b1170c42SJianlong Huang };
156b1170c42SJianlong Huang
157b1170c42SJianlong Huang static const struct of_device_id jh7110_aon_pinctrl_of_match[] = {
158b1170c42SJianlong Huang {
159b1170c42SJianlong Huang .compatible = "starfive,jh7110-aon-pinctrl",
160b1170c42SJianlong Huang .data = &jh7110_aon_pinctrl_info,
161b1170c42SJianlong Huang },
162b1170c42SJianlong Huang { /* sentinel */ }
163b1170c42SJianlong Huang };
164b1170c42SJianlong Huang MODULE_DEVICE_TABLE(of, jh7110_aon_pinctrl_of_match);
165b1170c42SJianlong Huang
166b1170c42SJianlong Huang static struct platform_driver jh7110_aon_pinctrl_driver = {
167b1170c42SJianlong Huang .probe = jh7110_pinctrl_probe,
168b1170c42SJianlong Huang .driver = {
169b1170c42SJianlong Huang .name = "starfive-jh7110-aon-pinctrl",
170b1170c42SJianlong Huang .of_match_table = jh7110_aon_pinctrl_of_match,
171*64061b67SHal Feng .pm = pm_sleep_ptr(&jh7110_pinctrl_pm_ops),
172b1170c42SJianlong Huang },
173b1170c42SJianlong Huang };
174b1170c42SJianlong Huang module_platform_driver(jh7110_aon_pinctrl_driver);
175b1170c42SJianlong Huang
176b1170c42SJianlong Huang MODULE_DESCRIPTION("Pinctrl driver for the StarFive JH7110 SoC aon controller");
177b1170c42SJianlong Huang MODULE_AUTHOR("Jianlong Huang <jianlong.huang@starfivetech.com>");
178b1170c42SJianlong Huang MODULE_LICENSE("GPL");
179