1ba99b756SJianlong Huang // SPDX-License-Identifier: GPL-2.0
2ba99b756SJianlong Huang /*
3ba99b756SJianlong Huang * Pinctrl / GPIO driver for StarFive JH7100 SoC
4ba99b756SJianlong Huang *
5ba99b756SJianlong Huang * Copyright (C) 2020 Shanghai StarFive Technology Co., Ltd.
6ba99b756SJianlong Huang * Copyright (C) 2021 Emil Renner Berthing <kernel@esmil.dk>
7ba99b756SJianlong Huang */
8ba99b756SJianlong Huang
9ba99b756SJianlong Huang #include <linux/bits.h>
10ba99b756SJianlong Huang #include <linux/clk.h>
11ba99b756SJianlong Huang #include <linux/gpio/driver.h>
12ba99b756SJianlong Huang #include <linux/io.h>
13ba99b756SJianlong Huang #include <linux/mod_devicetable.h>
14ba99b756SJianlong Huang #include <linux/module.h>
15ba99b756SJianlong Huang #include <linux/of.h>
16ba99b756SJianlong Huang #include <linux/platform_device.h>
17ba99b756SJianlong Huang #include <linux/reset.h>
18042b93c9SAndy Shevchenko #include <linux/seq_file.h>
19ba99b756SJianlong Huang #include <linux/spinlock.h>
20ba99b756SJianlong Huang
21042b93c9SAndy Shevchenko #include <linux/pinctrl/consumer.h>
22042b93c9SAndy Shevchenko #include <linux/pinctrl/pinconf.h>
23ba99b756SJianlong Huang #include <linux/pinctrl/pinctrl.h>
24ba99b756SJianlong Huang #include <linux/pinctrl/pinmux.h>
25ba99b756SJianlong Huang
26ba99b756SJianlong Huang #include <dt-bindings/pinctrl/pinctrl-starfive-jh7100.h>
27ba99b756SJianlong Huang
28ba99b756SJianlong Huang #include "../core.h"
29ba99b756SJianlong Huang #include "../pinctrl-utils.h"
30ba99b756SJianlong Huang #include "../pinmux.h"
31ba99b756SJianlong Huang #include "../pinconf.h"
32ba99b756SJianlong Huang
33ba99b756SJianlong Huang #define DRIVER_NAME "pinctrl-starfive"
34ba99b756SJianlong Huang
35ba99b756SJianlong Huang /*
36ba99b756SJianlong Huang * Refer to Section 12. GPIO Registers in the JH7100 data sheet:
37ba99b756SJianlong Huang * https://github.com/starfive-tech/JH7100_Docs
38ba99b756SJianlong Huang */
39ba99b756SJianlong Huang #define NR_GPIOS 64
40ba99b756SJianlong Huang
41ba99b756SJianlong Huang /*
42ba99b756SJianlong Huang * Global enable for GPIO interrupts. If bit 0 is set to 1 the GPIO interrupts
43ba99b756SJianlong Huang * are enabled. If set to 0 the GPIO interrupts are disabled.
44ba99b756SJianlong Huang */
45ba99b756SJianlong Huang #define GPIOEN 0x000
46ba99b756SJianlong Huang
47ba99b756SJianlong Huang /*
48ba99b756SJianlong Huang * The following 32-bit registers come in pairs, but only the offset of the
49ba99b756SJianlong Huang * first register is defined. The first controls (interrupts for) GPIO 0-31 and
50ba99b756SJianlong Huang * the second GPIO 32-63.
51ba99b756SJianlong Huang */
52ba99b756SJianlong Huang
53ba99b756SJianlong Huang /*
54ba99b756SJianlong Huang * Interrupt Type. If set to 1 the interrupt is edge-triggered. If set to 0 the
55ba99b756SJianlong Huang * interrupt is level-triggered.
56ba99b756SJianlong Huang */
57ba99b756SJianlong Huang #define GPIOIS 0x010
58ba99b756SJianlong Huang
59ba99b756SJianlong Huang /*
60ba99b756SJianlong Huang * Edge-Trigger Interrupt Type. If set to 1 the interrupt gets triggered on
61ba99b756SJianlong Huang * both positive and negative edges. If set to 0 the interrupt is triggered by a
62ba99b756SJianlong Huang * single edge.
63ba99b756SJianlong Huang */
64ba99b756SJianlong Huang #define GPIOIBE 0x018
65ba99b756SJianlong Huang
66ba99b756SJianlong Huang /*
67ba99b756SJianlong Huang * Interrupt Trigger Polarity. If set to 1 the interrupt is triggered on a
68ba99b756SJianlong Huang * rising edge (edge-triggered) or high level (level-triggered). If set to 0 the
69ba99b756SJianlong Huang * interrupt is triggered on a falling edge (edge-triggered) or low level
70ba99b756SJianlong Huang * (level-triggered).
71ba99b756SJianlong Huang */
72ba99b756SJianlong Huang #define GPIOIEV 0x020
73ba99b756SJianlong Huang
74ba99b756SJianlong Huang /*
75ba99b756SJianlong Huang * Interrupt Mask. If set to 1 the interrupt is enabled (unmasked). If set to 0
76ba99b756SJianlong Huang * the interrupt is disabled (masked). Note that the current documentation is
77ba99b756SJianlong Huang * wrong and says the exct opposite of this.
78ba99b756SJianlong Huang */
79ba99b756SJianlong Huang #define GPIOIE 0x028
80ba99b756SJianlong Huang
81ba99b756SJianlong Huang /*
82ba99b756SJianlong Huang * Clear Edge-Triggered Interrupts. Write a 1 to clear the edge-triggered
83ba99b756SJianlong Huang * interrupt.
84ba99b756SJianlong Huang */
85ba99b756SJianlong Huang #define GPIOIC 0x030
86ba99b756SJianlong Huang
87ba99b756SJianlong Huang /*
88ba99b756SJianlong Huang * Edge-Triggered Interrupt Status. A 1 means the configured edge was detected.
89ba99b756SJianlong Huang */
90ba99b756SJianlong Huang #define GPIORIS 0x038
91ba99b756SJianlong Huang
92ba99b756SJianlong Huang /*
93ba99b756SJianlong Huang * Interrupt Status after Masking. A 1 means the configured edge or level was
94ba99b756SJianlong Huang * detected and not masked.
95ba99b756SJianlong Huang */
96ba99b756SJianlong Huang #define GPIOMIS 0x040
97ba99b756SJianlong Huang
98ba99b756SJianlong Huang /*
99ba99b756SJianlong Huang * Data Value. Dynamically reflects the value of the GPIO pin. If 1 the pin is
100ba99b756SJianlong Huang * a digital 1 and if 0 the pin is a digital 0.
101ba99b756SJianlong Huang */
102ba99b756SJianlong Huang #define GPIODIN 0x048
103ba99b756SJianlong Huang
104ba99b756SJianlong Huang /*
105ba99b756SJianlong Huang * From the data sheet section 12.2, there are 64 32-bit output data registers
106ba99b756SJianlong Huang * and 64 output enable registers. Output data and output enable registers for
107ba99b756SJianlong Huang * a given GPIO are contiguous. Eg. GPO0_DOUT_CFG is 0x50 and GPO0_DOEN_CFG is
108ba99b756SJianlong Huang * 0x54 while GPO1_DOUT_CFG is 0x58 and GPO1_DOEN_CFG is 0x5c. The stride
109ba99b756SJianlong Huang * between GPIO registers is effectively 8, thus: GPOn_DOUT_CFG is 0x50 + 8n
110ba99b756SJianlong Huang * and GPOn_DOEN_CFG is 0x54 + 8n.
111ba99b756SJianlong Huang */
112ba99b756SJianlong Huang #define GPON_DOUT_CFG 0x050
113ba99b756SJianlong Huang #define GPON_DOEN_CFG 0x054
114ba99b756SJianlong Huang
115ba99b756SJianlong Huang /*
116ba99b756SJianlong Huang * From Section 12.3, there are 75 input signal configuration registers which
117ba99b756SJianlong Huang * are 4 bytes wide starting with GPI_CPU_JTAG_TCK_CFG at 0x250 and ending with
118ba99b756SJianlong Huang * GPI_USB_OVER_CURRENT_CFG 0x378
119ba99b756SJianlong Huang */
120ba99b756SJianlong Huang #define GPI_CFG_OFFSET 0x250
121ba99b756SJianlong Huang
122ba99b756SJianlong Huang /*
123ba99b756SJianlong Huang * Pad Control Bits. There are 16 pad control bits for each pin located in 103
124ba99b756SJianlong Huang * 32-bit registers controlling PAD_GPIO[0] to PAD_GPIO[63] followed by
125ba99b756SJianlong Huang * PAD_FUNC_SHARE[0] to PAD_FUNC_SHARE[141]. Odd numbered pins use the upper 16
126ba99b756SJianlong Huang * bit of each register.
127ba99b756SJianlong Huang */
128ba99b756SJianlong Huang #define PAD_SLEW_RATE_MASK GENMASK(11, 9)
129ba99b756SJianlong Huang #define PAD_SLEW_RATE_POS 9
130ba99b756SJianlong Huang #define PAD_BIAS_STRONG_PULL_UP BIT(8)
131ba99b756SJianlong Huang #define PAD_INPUT_ENABLE BIT(7)
132ba99b756SJianlong Huang #define PAD_INPUT_SCHMITT_ENABLE BIT(6)
133ba99b756SJianlong Huang #define PAD_BIAS_DISABLE BIT(5)
134ba99b756SJianlong Huang #define PAD_BIAS_PULL_DOWN BIT(4)
135ba99b756SJianlong Huang #define PAD_BIAS_MASK \
136ba99b756SJianlong Huang (PAD_BIAS_STRONG_PULL_UP | \
137ba99b756SJianlong Huang PAD_BIAS_DISABLE | \
138ba99b756SJianlong Huang PAD_BIAS_PULL_DOWN)
139ba99b756SJianlong Huang #define PAD_DRIVE_STRENGTH_MASK GENMASK(3, 0)
140ba99b756SJianlong Huang #define PAD_DRIVE_STRENGTH_POS 0
141ba99b756SJianlong Huang
142ba99b756SJianlong Huang /*
143ba99b756SJianlong Huang * From Section 11, the IO_PADSHARE_SEL register can be programmed to select
144ba99b756SJianlong Huang * one of seven pre-defined multiplexed signal groups on PAD_FUNC_SHARE and
145ba99b756SJianlong Huang * PAD_GPIO pads. This is a global setting.
146ba99b756SJianlong Huang */
147ba99b756SJianlong Huang #define IO_PADSHARE_SEL 0x1a0
148ba99b756SJianlong Huang
149ba99b756SJianlong Huang /*
150ba99b756SJianlong Huang * This just needs to be some number such that when
151ba99b756SJianlong Huang * sfp->gpio.pin_base = PAD_INVALID_GPIO then
152ba99b756SJianlong Huang * starfive_pin_to_gpio(sfp, validpin) is never a valid GPIO number.
153ba99b756SJianlong Huang * That is it should underflow and return something >= NR_GPIOS.
154ba99b756SJianlong Huang */
155ba99b756SJianlong Huang #define PAD_INVALID_GPIO 0x10000
156ba99b756SJianlong Huang
157ba99b756SJianlong Huang /*
158ba99b756SJianlong Huang * The packed pinmux values from the device tree look like this:
159ba99b756SJianlong Huang *
160ba99b756SJianlong Huang * | 31 - 24 | 23 - 16 | 15 - 8 | 7 | 6 | 5 - 0 |
161ba99b756SJianlong Huang * | dout | doen | din | dout rev | doen rev | gpio nr |
162ba99b756SJianlong Huang *
163ba99b756SJianlong Huang * ..but the GPOn_DOUT_CFG and GPOn_DOEN_CFG registers look like this:
164ba99b756SJianlong Huang *
165ba99b756SJianlong Huang * | 31 | 30 - 8 | 7 - 0 |
166ba99b756SJianlong Huang * | dout/doen rev | unused | dout/doen |
167ba99b756SJianlong Huang */
starfive_pinmux_to_gpio(u32 v)168ba99b756SJianlong Huang static unsigned int starfive_pinmux_to_gpio(u32 v)
169ba99b756SJianlong Huang {
170ba99b756SJianlong Huang return v & (NR_GPIOS - 1);
171ba99b756SJianlong Huang }
172ba99b756SJianlong Huang
starfive_pinmux_to_dout(u32 v)173ba99b756SJianlong Huang static u32 starfive_pinmux_to_dout(u32 v)
174ba99b756SJianlong Huang {
175ba99b756SJianlong Huang return ((v & BIT(7)) << (31 - 7)) | ((v >> 24) & GENMASK(7, 0));
176ba99b756SJianlong Huang }
177ba99b756SJianlong Huang
starfive_pinmux_to_doen(u32 v)178ba99b756SJianlong Huang static u32 starfive_pinmux_to_doen(u32 v)
179ba99b756SJianlong Huang {
180ba99b756SJianlong Huang return ((v & BIT(6)) << (31 - 6)) | ((v >> 16) & GENMASK(7, 0));
181ba99b756SJianlong Huang }
182ba99b756SJianlong Huang
starfive_pinmux_to_din(u32 v)183ba99b756SJianlong Huang static u32 starfive_pinmux_to_din(u32 v)
184ba99b756SJianlong Huang {
185ba99b756SJianlong Huang return (v >> 8) & GENMASK(7, 0);
186ba99b756SJianlong Huang }
187ba99b756SJianlong Huang
188ba99b756SJianlong Huang /*
189ba99b756SJianlong Huang * The maximum GPIO output current depends on the chosen drive strength:
190ba99b756SJianlong Huang *
191ba99b756SJianlong Huang * DS: 0 1 2 3 4 5 6 7
192ba99b756SJianlong Huang * mA: 14.2 21.2 28.2 35.2 42.2 49.1 56.0 62.8
193ba99b756SJianlong Huang *
194ba99b756SJianlong Huang * After rounding that is 7*DS + 14 mA
195ba99b756SJianlong Huang */
starfive_drive_strength_to_max_mA(u16 ds)196ba99b756SJianlong Huang static u32 starfive_drive_strength_to_max_mA(u16 ds)
197ba99b756SJianlong Huang {
198ba99b756SJianlong Huang return 7 * ds + 14;
199ba99b756SJianlong Huang }
200ba99b756SJianlong Huang
starfive_drive_strength_from_max_mA(u32 i)201ba99b756SJianlong Huang static u16 starfive_drive_strength_from_max_mA(u32 i)
202ba99b756SJianlong Huang {
203ba99b756SJianlong Huang return (clamp(i, 14U, 63U) - 14) / 7;
204ba99b756SJianlong Huang }
205ba99b756SJianlong Huang
206ba99b756SJianlong Huang struct starfive_pinctrl {
207ba99b756SJianlong Huang struct gpio_chip gc;
208ba99b756SJianlong Huang struct pinctrl_gpio_range gpios;
209ba99b756SJianlong Huang raw_spinlock_t lock;
210ba99b756SJianlong Huang void __iomem *base;
211ba99b756SJianlong Huang void __iomem *padctl;
212ba99b756SJianlong Huang struct pinctrl_dev *pctl;
213ba99b756SJianlong Huang struct mutex mutex; /* serialize adding groups and functions */
214ba99b756SJianlong Huang };
215ba99b756SJianlong Huang
starfive_pin_to_gpio(const struct starfive_pinctrl * sfp,unsigned int pin)216ba99b756SJianlong Huang static inline unsigned int starfive_pin_to_gpio(const struct starfive_pinctrl *sfp,
217ba99b756SJianlong Huang unsigned int pin)
218ba99b756SJianlong Huang {
219ba99b756SJianlong Huang return pin - sfp->gpios.pin_base;
220ba99b756SJianlong Huang }
221ba99b756SJianlong Huang
starfive_gpio_to_pin(const struct starfive_pinctrl * sfp,unsigned int gpio)222ba99b756SJianlong Huang static inline unsigned int starfive_gpio_to_pin(const struct starfive_pinctrl *sfp,
223ba99b756SJianlong Huang unsigned int gpio)
224ba99b756SJianlong Huang {
225ba99b756SJianlong Huang return sfp->gpios.pin_base + gpio;
226ba99b756SJianlong Huang }
227ba99b756SJianlong Huang
starfive_from_irq_data(struct irq_data * d)228ba99b756SJianlong Huang static struct starfive_pinctrl *starfive_from_irq_data(struct irq_data *d)
229ba99b756SJianlong Huang {
230ba99b756SJianlong Huang struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
231ba99b756SJianlong Huang
232ba99b756SJianlong Huang return container_of(gc, struct starfive_pinctrl, gc);
233ba99b756SJianlong Huang }
234ba99b756SJianlong Huang
starfive_from_irq_desc(struct irq_desc * desc)235ba99b756SJianlong Huang static struct starfive_pinctrl *starfive_from_irq_desc(struct irq_desc *desc)
236ba99b756SJianlong Huang {
237ba99b756SJianlong Huang struct gpio_chip *gc = irq_desc_get_handler_data(desc);
238ba99b756SJianlong Huang
239ba99b756SJianlong Huang return container_of(gc, struct starfive_pinctrl, gc);
240ba99b756SJianlong Huang }
241ba99b756SJianlong Huang
242ba99b756SJianlong Huang static const struct pinctrl_pin_desc starfive_pins[] = {
243ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(0), "GPIO[0]"),
244ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(1), "GPIO[1]"),
245ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(2), "GPIO[2]"),
246ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(3), "GPIO[3]"),
247ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(4), "GPIO[4]"),
248ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(5), "GPIO[5]"),
249ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(6), "GPIO[6]"),
250ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(7), "GPIO[7]"),
251ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(8), "GPIO[8]"),
252ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(9), "GPIO[9]"),
253ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(10), "GPIO[10]"),
254ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(11), "GPIO[11]"),
255ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(12), "GPIO[12]"),
256ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(13), "GPIO[13]"),
257ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(14), "GPIO[14]"),
258ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(15), "GPIO[15]"),
259ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(16), "GPIO[16]"),
260ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(17), "GPIO[17]"),
261ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(18), "GPIO[18]"),
262ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(19), "GPIO[19]"),
263ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(20), "GPIO[20]"),
264ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(21), "GPIO[21]"),
265ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(22), "GPIO[22]"),
266ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(23), "GPIO[23]"),
267ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(24), "GPIO[24]"),
268ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(25), "GPIO[25]"),
269ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(26), "GPIO[26]"),
270ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(27), "GPIO[27]"),
271ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(28), "GPIO[28]"),
272ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(29), "GPIO[29]"),
273ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(30), "GPIO[30]"),
274ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(31), "GPIO[31]"),
275ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(32), "GPIO[32]"),
276ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(33), "GPIO[33]"),
277ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(34), "GPIO[34]"),
278ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(35), "GPIO[35]"),
279ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(36), "GPIO[36]"),
280ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(37), "GPIO[37]"),
281ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(38), "GPIO[38]"),
282ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(39), "GPIO[39]"),
283ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(40), "GPIO[40]"),
284ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(41), "GPIO[41]"),
285ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(42), "GPIO[42]"),
286ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(43), "GPIO[43]"),
287ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(44), "GPIO[44]"),
288ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(45), "GPIO[45]"),
289ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(46), "GPIO[46]"),
290ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(47), "GPIO[47]"),
291ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(48), "GPIO[48]"),
292ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(49), "GPIO[49]"),
293ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(50), "GPIO[50]"),
294ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(51), "GPIO[51]"),
295ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(52), "GPIO[52]"),
296ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(53), "GPIO[53]"),
297ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(54), "GPIO[54]"),
298ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(55), "GPIO[55]"),
299ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(56), "GPIO[56]"),
300ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(57), "GPIO[57]"),
301ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(58), "GPIO[58]"),
302ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(59), "GPIO[59]"),
303ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(60), "GPIO[60]"),
304ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(61), "GPIO[61]"),
305ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(62), "GPIO[62]"),
306ba99b756SJianlong Huang PINCTRL_PIN(PAD_GPIO(63), "GPIO[63]"),
307ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(0), "FUNC_SHARE[0]"),
308ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(1), "FUNC_SHARE[1]"),
309ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(2), "FUNC_SHARE[2]"),
310ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(3), "FUNC_SHARE[3]"),
311ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(4), "FUNC_SHARE[4]"),
312ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(5), "FUNC_SHARE[5]"),
313ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(6), "FUNC_SHARE[6]"),
314ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(7), "FUNC_SHARE[7]"),
315ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(8), "FUNC_SHARE[8]"),
316ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(9), "FUNC_SHARE[9]"),
317ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(10), "FUNC_SHARE[10]"),
318ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(11), "FUNC_SHARE[11]"),
319ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(12), "FUNC_SHARE[12]"),
320ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(13), "FUNC_SHARE[13]"),
321ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(14), "FUNC_SHARE[14]"),
322ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(15), "FUNC_SHARE[15]"),
323ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(16), "FUNC_SHARE[16]"),
324ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(17), "FUNC_SHARE[17]"),
325ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(18), "FUNC_SHARE[18]"),
326ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(19), "FUNC_SHARE[19]"),
327ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(20), "FUNC_SHARE[20]"),
328ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(21), "FUNC_SHARE[21]"),
329ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(22), "FUNC_SHARE[22]"),
330ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(23), "FUNC_SHARE[23]"),
331ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(24), "FUNC_SHARE[24]"),
332ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(25), "FUNC_SHARE[25]"),
333ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(26), "FUNC_SHARE[26]"),
334ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(27), "FUNC_SHARE[27]"),
335ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(28), "FUNC_SHARE[28]"),
336ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(29), "FUNC_SHARE[29]"),
337ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(30), "FUNC_SHARE[30]"),
338ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(31), "FUNC_SHARE[31]"),
339ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(32), "FUNC_SHARE[32]"),
340ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(33), "FUNC_SHARE[33]"),
341ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(34), "FUNC_SHARE[34]"),
342ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(35), "FUNC_SHARE[35]"),
343ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(36), "FUNC_SHARE[36]"),
344ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(37), "FUNC_SHARE[37]"),
345ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(38), "FUNC_SHARE[38]"),
346ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(39), "FUNC_SHARE[39]"),
347ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(40), "FUNC_SHARE[40]"),
348ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(41), "FUNC_SHARE[41]"),
349ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(42), "FUNC_SHARE[42]"),
350ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(43), "FUNC_SHARE[43]"),
351ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(44), "FUNC_SHARE[44]"),
352ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(45), "FUNC_SHARE[45]"),
353ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(46), "FUNC_SHARE[46]"),
354ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(47), "FUNC_SHARE[47]"),
355ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(48), "FUNC_SHARE[48]"),
356ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(49), "FUNC_SHARE[49]"),
357ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(50), "FUNC_SHARE[50]"),
358ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(51), "FUNC_SHARE[51]"),
359ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(52), "FUNC_SHARE[52]"),
360ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(53), "FUNC_SHARE[53]"),
361ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(54), "FUNC_SHARE[54]"),
362ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(55), "FUNC_SHARE[55]"),
363ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(56), "FUNC_SHARE[56]"),
364ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(57), "FUNC_SHARE[57]"),
365ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(58), "FUNC_SHARE[58]"),
366ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(59), "FUNC_SHARE[59]"),
367ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(60), "FUNC_SHARE[60]"),
368ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(61), "FUNC_SHARE[61]"),
369ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(62), "FUNC_SHARE[62]"),
370ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(63), "FUNC_SHARE[63]"),
371ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(64), "FUNC_SHARE[64]"),
372ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(65), "FUNC_SHARE[65]"),
373ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(66), "FUNC_SHARE[66]"),
374ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(67), "FUNC_SHARE[67]"),
375ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(68), "FUNC_SHARE[68]"),
376ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(69), "FUNC_SHARE[69]"),
377ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(70), "FUNC_SHARE[70]"),
378ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(71), "FUNC_SHARE[71]"),
379ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(72), "FUNC_SHARE[72]"),
380ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(73), "FUNC_SHARE[73]"),
381ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(74), "FUNC_SHARE[74]"),
382ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(75), "FUNC_SHARE[75]"),
383ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(76), "FUNC_SHARE[76]"),
384ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(77), "FUNC_SHARE[77]"),
385ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(78), "FUNC_SHARE[78]"),
386ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(79), "FUNC_SHARE[79]"),
387ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(80), "FUNC_SHARE[80]"),
388ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(81), "FUNC_SHARE[81]"),
389ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(82), "FUNC_SHARE[82]"),
390ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(83), "FUNC_SHARE[83]"),
391ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(84), "FUNC_SHARE[84]"),
392ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(85), "FUNC_SHARE[85]"),
393ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(86), "FUNC_SHARE[86]"),
394ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(87), "FUNC_SHARE[87]"),
395ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(88), "FUNC_SHARE[88]"),
396ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(89), "FUNC_SHARE[89]"),
397ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(90), "FUNC_SHARE[90]"),
398ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(91), "FUNC_SHARE[91]"),
399ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(92), "FUNC_SHARE[92]"),
400ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(93), "FUNC_SHARE[93]"),
401ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(94), "FUNC_SHARE[94]"),
402ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(95), "FUNC_SHARE[95]"),
403ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(96), "FUNC_SHARE[96]"),
404ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(97), "FUNC_SHARE[97]"),
405ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(98), "FUNC_SHARE[98]"),
406ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(99), "FUNC_SHARE[99]"),
407ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(100), "FUNC_SHARE[100]"),
408ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(101), "FUNC_SHARE[101]"),
409ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(102), "FUNC_SHARE[102]"),
410ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(103), "FUNC_SHARE[103]"),
411ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(104), "FUNC_SHARE[104]"),
412ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(105), "FUNC_SHARE[105]"),
413ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(106), "FUNC_SHARE[106]"),
414ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(107), "FUNC_SHARE[107]"),
415ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(108), "FUNC_SHARE[108]"),
416ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(109), "FUNC_SHARE[109]"),
417ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(110), "FUNC_SHARE[110]"),
418ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(111), "FUNC_SHARE[111]"),
419ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(112), "FUNC_SHARE[112]"),
420ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(113), "FUNC_SHARE[113]"),
421ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(114), "FUNC_SHARE[114]"),
422ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(115), "FUNC_SHARE[115]"),
423ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(116), "FUNC_SHARE[116]"),
424ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(117), "FUNC_SHARE[117]"),
425ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(118), "FUNC_SHARE[118]"),
426ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(119), "FUNC_SHARE[119]"),
427ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(120), "FUNC_SHARE[120]"),
428ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(121), "FUNC_SHARE[121]"),
429ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(122), "FUNC_SHARE[122]"),
430ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(123), "FUNC_SHARE[123]"),
431ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(124), "FUNC_SHARE[124]"),
432ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(125), "FUNC_SHARE[125]"),
433ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(126), "FUNC_SHARE[126]"),
434ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(127), "FUNC_SHARE[127]"),
435ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(128), "FUNC_SHARE[128]"),
436ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(129), "FUNC_SHARE[129]"),
437ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(130), "FUNC_SHARE[130]"),
438ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(131), "FUNC_SHARE[131]"),
439ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(132), "FUNC_SHARE[132]"),
440ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(133), "FUNC_SHARE[133]"),
441ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(134), "FUNC_SHARE[134]"),
442ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(135), "FUNC_SHARE[135]"),
443ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(136), "FUNC_SHARE[136]"),
444ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(137), "FUNC_SHARE[137]"),
445ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(138), "FUNC_SHARE[138]"),
446ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(139), "FUNC_SHARE[139]"),
447ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(140), "FUNC_SHARE[140]"),
448ba99b756SJianlong Huang PINCTRL_PIN(PAD_FUNC_SHARE(141), "FUNC_SHARE[141]"),
449ba99b756SJianlong Huang };
450ba99b756SJianlong Huang
451ba99b756SJianlong Huang #ifdef CONFIG_DEBUG_FS
starfive_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned int pin)452ba99b756SJianlong Huang static void starfive_pin_dbg_show(struct pinctrl_dev *pctldev,
453ba99b756SJianlong Huang struct seq_file *s,
454ba99b756SJianlong Huang unsigned int pin)
455ba99b756SJianlong Huang {
456ba99b756SJianlong Huang struct starfive_pinctrl *sfp = pinctrl_dev_get_drvdata(pctldev);
457ba99b756SJianlong Huang unsigned int gpio = starfive_pin_to_gpio(sfp, pin);
458ba99b756SJianlong Huang void __iomem *reg;
459ba99b756SJianlong Huang u32 dout, doen;
460ba99b756SJianlong Huang
461ba99b756SJianlong Huang if (gpio >= NR_GPIOS)
462ba99b756SJianlong Huang return;
463ba99b756SJianlong Huang
464ba99b756SJianlong Huang reg = sfp->base + GPON_DOUT_CFG + 8 * gpio;
465ba99b756SJianlong Huang dout = readl_relaxed(reg + 0x000);
466ba99b756SJianlong Huang doen = readl_relaxed(reg + 0x004);
467ba99b756SJianlong Huang
468ba99b756SJianlong Huang seq_printf(s, "dout=%lu%s doen=%lu%s",
469ba99b756SJianlong Huang dout & GENMASK(7, 0), (dout & BIT(31)) ? "r" : "",
470ba99b756SJianlong Huang doen & GENMASK(7, 0), (doen & BIT(31)) ? "r" : "");
471ba99b756SJianlong Huang }
472ba99b756SJianlong Huang #else
473ba99b756SJianlong Huang #define starfive_pin_dbg_show NULL
474ba99b756SJianlong Huang #endif
475ba99b756SJianlong Huang
starfive_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** maps,unsigned int * num_maps)476ba99b756SJianlong Huang static int starfive_dt_node_to_map(struct pinctrl_dev *pctldev,
477ba99b756SJianlong Huang struct device_node *np,
478ba99b756SJianlong Huang struct pinctrl_map **maps,
479ba99b756SJianlong Huang unsigned int *num_maps)
480ba99b756SJianlong Huang {
481ba99b756SJianlong Huang struct starfive_pinctrl *sfp = pinctrl_dev_get_drvdata(pctldev);
482ba99b756SJianlong Huang struct device *dev = sfp->gc.parent;
483ba99b756SJianlong Huang struct device_node *child;
484ba99b756SJianlong Huang struct pinctrl_map *map;
485ba99b756SJianlong Huang const char **pgnames;
486ba99b756SJianlong Huang const char *grpname;
487ba99b756SJianlong Huang u32 *pinmux;
488ba99b756SJianlong Huang int ngroups;
489ba99b756SJianlong Huang int *pins;
490ba99b756SJianlong Huang int nmaps;
491ba99b756SJianlong Huang int ret;
492ba99b756SJianlong Huang
493ba99b756SJianlong Huang nmaps = 0;
494ba99b756SJianlong Huang ngroups = 0;
495*6e827b18SNam Cao for_each_available_child_of_node(np, child) {
496ba99b756SJianlong Huang int npinmux = of_property_count_u32_elems(child, "pinmux");
497ba99b756SJianlong Huang int npins = of_property_count_u32_elems(child, "pins");
498ba99b756SJianlong Huang
499ba99b756SJianlong Huang if (npinmux > 0 && npins > 0) {
500ba99b756SJianlong Huang dev_err(dev, "invalid pinctrl group %pOFn.%pOFn: both pinmux and pins set\n",
501ba99b756SJianlong Huang np, child);
502ba99b756SJianlong Huang of_node_put(child);
503ba99b756SJianlong Huang return -EINVAL;
504ba99b756SJianlong Huang }
505ba99b756SJianlong Huang if (npinmux == 0 && npins == 0) {
506ba99b756SJianlong Huang dev_err(dev, "invalid pinctrl group %pOFn.%pOFn: neither pinmux nor pins set\n",
507ba99b756SJianlong Huang np, child);
508ba99b756SJianlong Huang of_node_put(child);
509ba99b756SJianlong Huang return -EINVAL;
510ba99b756SJianlong Huang }
511ba99b756SJianlong Huang
512ba99b756SJianlong Huang if (npinmux > 0)
513ba99b756SJianlong Huang nmaps += 2;
514ba99b756SJianlong Huang else
515ba99b756SJianlong Huang nmaps += 1;
516ba99b756SJianlong Huang ngroups += 1;
517ba99b756SJianlong Huang }
518ba99b756SJianlong Huang
519ba99b756SJianlong Huang pgnames = devm_kcalloc(dev, ngroups, sizeof(*pgnames), GFP_KERNEL);
520ba99b756SJianlong Huang if (!pgnames)
521ba99b756SJianlong Huang return -ENOMEM;
522ba99b756SJianlong Huang
523ba99b756SJianlong Huang map = kcalloc(nmaps, sizeof(*map), GFP_KERNEL);
524ba99b756SJianlong Huang if (!map)
525ba99b756SJianlong Huang return -ENOMEM;
526ba99b756SJianlong Huang
527ba99b756SJianlong Huang nmaps = 0;
528ba99b756SJianlong Huang ngroups = 0;
529ba99b756SJianlong Huang mutex_lock(&sfp->mutex);
530*6e827b18SNam Cao for_each_available_child_of_node(np, child) {
531ba99b756SJianlong Huang int npins;
532ba99b756SJianlong Huang int i;
533ba99b756SJianlong Huang
534ba99b756SJianlong Huang grpname = devm_kasprintf(dev, GFP_KERNEL, "%pOFn.%pOFn", np, child);
535ba99b756SJianlong Huang if (!grpname) {
536ba99b756SJianlong Huang ret = -ENOMEM;
537ba99b756SJianlong Huang goto put_child;
538ba99b756SJianlong Huang }
539ba99b756SJianlong Huang
540ba99b756SJianlong Huang pgnames[ngroups++] = grpname;
541ba99b756SJianlong Huang
542ba99b756SJianlong Huang if ((npins = of_property_count_u32_elems(child, "pinmux")) > 0) {
543ba99b756SJianlong Huang pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL);
544ba99b756SJianlong Huang if (!pins) {
545ba99b756SJianlong Huang ret = -ENOMEM;
546ba99b756SJianlong Huang goto put_child;
547ba99b756SJianlong Huang }
548ba99b756SJianlong Huang
549ba99b756SJianlong Huang pinmux = devm_kcalloc(dev, npins, sizeof(*pinmux), GFP_KERNEL);
550ba99b756SJianlong Huang if (!pinmux) {
551ba99b756SJianlong Huang ret = -ENOMEM;
552ba99b756SJianlong Huang goto put_child;
553ba99b756SJianlong Huang }
554ba99b756SJianlong Huang
555ba99b756SJianlong Huang ret = of_property_read_u32_array(child, "pinmux", pinmux, npins);
556ba99b756SJianlong Huang if (ret)
557ba99b756SJianlong Huang goto put_child;
558ba99b756SJianlong Huang
559ba99b756SJianlong Huang for (i = 0; i < npins; i++) {
560ba99b756SJianlong Huang unsigned int gpio = starfive_pinmux_to_gpio(pinmux[i]);
561ba99b756SJianlong Huang
562ba99b756SJianlong Huang pins[i] = starfive_gpio_to_pin(sfp, gpio);
563ba99b756SJianlong Huang }
564ba99b756SJianlong Huang
565ba99b756SJianlong Huang map[nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
566ba99b756SJianlong Huang map[nmaps].data.mux.function = np->name;
567ba99b756SJianlong Huang map[nmaps].data.mux.group = grpname;
568ba99b756SJianlong Huang nmaps += 1;
569ba99b756SJianlong Huang } else if ((npins = of_property_count_u32_elems(child, "pins")) > 0) {
570ba99b756SJianlong Huang pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL);
571ba99b756SJianlong Huang if (!pins) {
572ba99b756SJianlong Huang ret = -ENOMEM;
573ba99b756SJianlong Huang goto put_child;
574ba99b756SJianlong Huang }
575ba99b756SJianlong Huang
576ba99b756SJianlong Huang pinmux = NULL;
577ba99b756SJianlong Huang
578ba99b756SJianlong Huang for (i = 0; i < npins; i++) {
579ba99b756SJianlong Huang u32 v;
580ba99b756SJianlong Huang
581ba99b756SJianlong Huang ret = of_property_read_u32_index(child, "pins", i, &v);
582ba99b756SJianlong Huang if (ret)
583ba99b756SJianlong Huang goto put_child;
584ba99b756SJianlong Huang pins[i] = v;
585ba99b756SJianlong Huang }
586ba99b756SJianlong Huang } else {
587ba99b756SJianlong Huang ret = -EINVAL;
588ba99b756SJianlong Huang goto put_child;
589ba99b756SJianlong Huang }
590ba99b756SJianlong Huang
591ba99b756SJianlong Huang ret = pinctrl_generic_add_group(pctldev, grpname, pins, npins, pinmux);
592ba99b756SJianlong Huang if (ret < 0) {
593ba99b756SJianlong Huang dev_err(dev, "error adding group %s: %d\n", grpname, ret);
594ba99b756SJianlong Huang goto put_child;
595ba99b756SJianlong Huang }
596ba99b756SJianlong Huang
597ba99b756SJianlong Huang ret = pinconf_generic_parse_dt_config(child, pctldev,
598ba99b756SJianlong Huang &map[nmaps].data.configs.configs,
599ba99b756SJianlong Huang &map[nmaps].data.configs.num_configs);
600ba99b756SJianlong Huang if (ret) {
601ba99b756SJianlong Huang dev_err(dev, "error parsing pin config of group %s: %d\n",
602ba99b756SJianlong Huang grpname, ret);
603ba99b756SJianlong Huang goto put_child;
604ba99b756SJianlong Huang }
605ba99b756SJianlong Huang
606ba99b756SJianlong Huang /* don't create a map if there are no pinconf settings */
607ba99b756SJianlong Huang if (map[nmaps].data.configs.num_configs == 0)
608ba99b756SJianlong Huang continue;
609ba99b756SJianlong Huang
610ba99b756SJianlong Huang map[nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
611ba99b756SJianlong Huang map[nmaps].data.configs.group_or_pin = grpname;
612ba99b756SJianlong Huang nmaps += 1;
613ba99b756SJianlong Huang }
614ba99b756SJianlong Huang
615ba99b756SJianlong Huang ret = pinmux_generic_add_function(pctldev, np->name, pgnames, ngroups, NULL);
616ba99b756SJianlong Huang if (ret < 0) {
617ba99b756SJianlong Huang dev_err(dev, "error adding function %s: %d\n", np->name, ret);
618ba99b756SJianlong Huang goto free_map;
619ba99b756SJianlong Huang }
620ba99b756SJianlong Huang
621ba99b756SJianlong Huang *maps = map;
622ba99b756SJianlong Huang *num_maps = nmaps;
623ba99b756SJianlong Huang mutex_unlock(&sfp->mutex);
624ba99b756SJianlong Huang return 0;
625ba99b756SJianlong Huang
626ba99b756SJianlong Huang put_child:
627ba99b756SJianlong Huang of_node_put(child);
628ba99b756SJianlong Huang free_map:
629ba99b756SJianlong Huang pinctrl_utils_free_map(pctldev, map, nmaps);
630ba99b756SJianlong Huang mutex_unlock(&sfp->mutex);
631ba99b756SJianlong Huang return ret;
632ba99b756SJianlong Huang }
633ba99b756SJianlong Huang
634ba99b756SJianlong Huang static const struct pinctrl_ops starfive_pinctrl_ops = {
635ba99b756SJianlong Huang .get_groups_count = pinctrl_generic_get_group_count,
636ba99b756SJianlong Huang .get_group_name = pinctrl_generic_get_group_name,
637ba99b756SJianlong Huang .get_group_pins = pinctrl_generic_get_group_pins,
638ba99b756SJianlong Huang .pin_dbg_show = starfive_pin_dbg_show,
639ba99b756SJianlong Huang .dt_node_to_map = starfive_dt_node_to_map,
640ba99b756SJianlong Huang .dt_free_map = pinctrl_utils_free_map,
641ba99b756SJianlong Huang };
642ba99b756SJianlong Huang
starfive_set_mux(struct pinctrl_dev * pctldev,unsigned int fsel,unsigned int gsel)643ba99b756SJianlong Huang static int starfive_set_mux(struct pinctrl_dev *pctldev,
644ba99b756SJianlong Huang unsigned int fsel, unsigned int gsel)
645ba99b756SJianlong Huang {
646ba99b756SJianlong Huang struct starfive_pinctrl *sfp = pinctrl_dev_get_drvdata(pctldev);
647ba99b756SJianlong Huang struct device *dev = sfp->gc.parent;
648ba99b756SJianlong Huang const struct group_desc *group;
649ba99b756SJianlong Huang const u32 *pinmux;
650ba99b756SJianlong Huang unsigned int i;
651ba99b756SJianlong Huang
652ba99b756SJianlong Huang group = pinctrl_generic_get_group(pctldev, gsel);
653ba99b756SJianlong Huang if (!group)
654ba99b756SJianlong Huang return -EINVAL;
655ba99b756SJianlong Huang
656ba99b756SJianlong Huang pinmux = group->data;
657ba99b756SJianlong Huang for (i = 0; i < group->num_pins; i++) {
658ba99b756SJianlong Huang u32 v = pinmux[i];
659ba99b756SJianlong Huang unsigned int gpio = starfive_pinmux_to_gpio(v);
660ba99b756SJianlong Huang u32 dout = starfive_pinmux_to_dout(v);
661ba99b756SJianlong Huang u32 doen = starfive_pinmux_to_doen(v);
662ba99b756SJianlong Huang u32 din = starfive_pinmux_to_din(v);
663ba99b756SJianlong Huang void __iomem *reg_dout;
664ba99b756SJianlong Huang void __iomem *reg_doen;
665ba99b756SJianlong Huang void __iomem *reg_din;
666ba99b756SJianlong Huang unsigned long flags;
667ba99b756SJianlong Huang
668ba99b756SJianlong Huang dev_dbg(dev, "GPIO%u: dout=0x%x doen=0x%x din=0x%x\n",
669ba99b756SJianlong Huang gpio, dout, doen, din);
670ba99b756SJianlong Huang
671ba99b756SJianlong Huang reg_dout = sfp->base + GPON_DOUT_CFG + 8 * gpio;
672ba99b756SJianlong Huang reg_doen = sfp->base + GPON_DOEN_CFG + 8 * gpio;
673ba99b756SJianlong Huang if (din != GPI_NONE)
674ba99b756SJianlong Huang reg_din = sfp->base + GPI_CFG_OFFSET + 4 * din;
675ba99b756SJianlong Huang else
676ba99b756SJianlong Huang reg_din = NULL;
677ba99b756SJianlong Huang
678ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
679ba99b756SJianlong Huang writel_relaxed(dout, reg_dout);
680ba99b756SJianlong Huang writel_relaxed(doen, reg_doen);
681ba99b756SJianlong Huang if (reg_din)
682ba99b756SJianlong Huang writel_relaxed(gpio + 2, reg_din);
683ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
684ba99b756SJianlong Huang }
685ba99b756SJianlong Huang
686ba99b756SJianlong Huang return 0;
687ba99b756SJianlong Huang }
688ba99b756SJianlong Huang
689ba99b756SJianlong Huang static const struct pinmux_ops starfive_pinmux_ops = {
690ba99b756SJianlong Huang .get_functions_count = pinmux_generic_get_function_count,
691ba99b756SJianlong Huang .get_function_name = pinmux_generic_get_function_name,
692ba99b756SJianlong Huang .get_function_groups = pinmux_generic_get_function_groups,
693ba99b756SJianlong Huang .set_mux = starfive_set_mux,
694ba99b756SJianlong Huang .strict = true,
695ba99b756SJianlong Huang };
696ba99b756SJianlong Huang
starfive_padctl_get(struct starfive_pinctrl * sfp,unsigned int pin)697ba99b756SJianlong Huang static u16 starfive_padctl_get(struct starfive_pinctrl *sfp,
698ba99b756SJianlong Huang unsigned int pin)
699ba99b756SJianlong Huang {
700ba99b756SJianlong Huang void __iomem *reg = sfp->padctl + 4 * (pin / 2);
701ba99b756SJianlong Huang int shift = 16 * (pin % 2);
702ba99b756SJianlong Huang
703ba99b756SJianlong Huang return readl_relaxed(reg) >> shift;
704ba99b756SJianlong Huang }
705ba99b756SJianlong Huang
starfive_padctl_rmw(struct starfive_pinctrl * sfp,unsigned int pin,u16 _mask,u16 _value)706ba99b756SJianlong Huang static void starfive_padctl_rmw(struct starfive_pinctrl *sfp,
707ba99b756SJianlong Huang unsigned int pin,
708ba99b756SJianlong Huang u16 _mask, u16 _value)
709ba99b756SJianlong Huang {
710ba99b756SJianlong Huang void __iomem *reg = sfp->padctl + 4 * (pin / 2);
711ba99b756SJianlong Huang int shift = 16 * (pin % 2);
712ba99b756SJianlong Huang u32 mask = (u32)_mask << shift;
713ba99b756SJianlong Huang u32 value = (u32)_value << shift;
714ba99b756SJianlong Huang unsigned long flags;
715ba99b756SJianlong Huang
716ba99b756SJianlong Huang dev_dbg(sfp->gc.parent, "padctl_rmw(%u, 0x%03x, 0x%03x)\n", pin, _mask, _value);
717ba99b756SJianlong Huang
718ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
719ba99b756SJianlong Huang value |= readl_relaxed(reg) & ~mask;
720ba99b756SJianlong Huang writel_relaxed(value, reg);
721ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
722ba99b756SJianlong Huang }
723ba99b756SJianlong Huang
724ba99b756SJianlong Huang #define PIN_CONFIG_STARFIVE_STRONG_PULL_UP (PIN_CONFIG_END + 1)
725ba99b756SJianlong Huang
726ba99b756SJianlong Huang static const struct pinconf_generic_params starfive_pinconf_custom_params[] = {
727ba99b756SJianlong Huang { "starfive,strong-pull-up", PIN_CONFIG_STARFIVE_STRONG_PULL_UP, 1 },
728ba99b756SJianlong Huang };
729ba99b756SJianlong Huang
730ba99b756SJianlong Huang #ifdef CONFIG_DEBUG_FS
731ba99b756SJianlong Huang static const struct pin_config_item starfive_pinconf_custom_conf_items[] = {
732ba99b756SJianlong Huang PCONFDUMP(PIN_CONFIG_STARFIVE_STRONG_PULL_UP, "input bias strong pull-up", NULL, false),
733ba99b756SJianlong Huang };
734ba99b756SJianlong Huang
735ba99b756SJianlong Huang static_assert(ARRAY_SIZE(starfive_pinconf_custom_conf_items) ==
736ba99b756SJianlong Huang ARRAY_SIZE(starfive_pinconf_custom_params));
737ba99b756SJianlong Huang #else
738ba99b756SJianlong Huang #define starfive_pinconf_custom_conf_items NULL
739ba99b756SJianlong Huang #endif
740ba99b756SJianlong Huang
starfive_pinconf_get(struct pinctrl_dev * pctldev,unsigned int pin,unsigned long * config)741ba99b756SJianlong Huang static int starfive_pinconf_get(struct pinctrl_dev *pctldev,
742ba99b756SJianlong Huang unsigned int pin, unsigned long *config)
743ba99b756SJianlong Huang {
744ba99b756SJianlong Huang struct starfive_pinctrl *sfp = pinctrl_dev_get_drvdata(pctldev);
745ba99b756SJianlong Huang int param = pinconf_to_config_param(*config);
746ba99b756SJianlong Huang u16 value = starfive_padctl_get(sfp, pin);
747ba99b756SJianlong Huang bool enabled;
748ba99b756SJianlong Huang u32 arg;
749ba99b756SJianlong Huang
750ba99b756SJianlong Huang switch (param) {
751ba99b756SJianlong Huang case PIN_CONFIG_BIAS_DISABLE:
752ba99b756SJianlong Huang enabled = value & PAD_BIAS_DISABLE;
753ba99b756SJianlong Huang arg = 0;
754ba99b756SJianlong Huang break;
755ba99b756SJianlong Huang case PIN_CONFIG_BIAS_PULL_DOWN:
756ba99b756SJianlong Huang enabled = value & PAD_BIAS_PULL_DOWN;
757ba99b756SJianlong Huang arg = 1;
758ba99b756SJianlong Huang break;
759ba99b756SJianlong Huang case PIN_CONFIG_BIAS_PULL_UP:
760ba99b756SJianlong Huang enabled = !(value & PAD_BIAS_MASK);
761ba99b756SJianlong Huang arg = 1;
762ba99b756SJianlong Huang break;
763ba99b756SJianlong Huang case PIN_CONFIG_DRIVE_STRENGTH:
764ba99b756SJianlong Huang enabled = value & PAD_DRIVE_STRENGTH_MASK;
765ba99b756SJianlong Huang arg = starfive_drive_strength_to_max_mA(value & PAD_DRIVE_STRENGTH_MASK);
766ba99b756SJianlong Huang break;
767ba99b756SJianlong Huang case PIN_CONFIG_INPUT_ENABLE:
768ba99b756SJianlong Huang enabled = value & PAD_INPUT_ENABLE;
769ba99b756SJianlong Huang arg = enabled;
770ba99b756SJianlong Huang break;
771ba99b756SJianlong Huang case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
772ba99b756SJianlong Huang enabled = value & PAD_INPUT_SCHMITT_ENABLE;
773ba99b756SJianlong Huang arg = enabled;
774ba99b756SJianlong Huang break;
775ba99b756SJianlong Huang case PIN_CONFIG_SLEW_RATE:
776ba99b756SJianlong Huang enabled = value & PAD_SLEW_RATE_MASK;
777ba99b756SJianlong Huang arg = (value & PAD_SLEW_RATE_MASK) >> PAD_SLEW_RATE_POS;
778ba99b756SJianlong Huang break;
779ba99b756SJianlong Huang case PIN_CONFIG_STARFIVE_STRONG_PULL_UP:
780ba99b756SJianlong Huang enabled = value & PAD_BIAS_STRONG_PULL_UP;
781ba99b756SJianlong Huang arg = enabled;
782ba99b756SJianlong Huang break;
783ba99b756SJianlong Huang default:
784ba99b756SJianlong Huang return -ENOTSUPP;
785ba99b756SJianlong Huang }
786ba99b756SJianlong Huang
787ba99b756SJianlong Huang *config = pinconf_to_config_packed(param, arg);
788ba99b756SJianlong Huang return enabled ? 0 : -EINVAL;
789ba99b756SJianlong Huang }
790ba99b756SJianlong Huang
starfive_pinconf_group_get(struct pinctrl_dev * pctldev,unsigned int gsel,unsigned long * config)791ba99b756SJianlong Huang static int starfive_pinconf_group_get(struct pinctrl_dev *pctldev,
792ba99b756SJianlong Huang unsigned int gsel, unsigned long *config)
793ba99b756SJianlong Huang {
794ba99b756SJianlong Huang const struct group_desc *group;
795ba99b756SJianlong Huang
796ba99b756SJianlong Huang group = pinctrl_generic_get_group(pctldev, gsel);
797ba99b756SJianlong Huang if (!group)
798ba99b756SJianlong Huang return -EINVAL;
799ba99b756SJianlong Huang
800ba99b756SJianlong Huang return starfive_pinconf_get(pctldev, group->pins[0], config);
801ba99b756SJianlong Huang }
802ba99b756SJianlong Huang
starfive_pinconf_group_set(struct pinctrl_dev * pctldev,unsigned int gsel,unsigned long * configs,unsigned int num_configs)803ba99b756SJianlong Huang static int starfive_pinconf_group_set(struct pinctrl_dev *pctldev,
804ba99b756SJianlong Huang unsigned int gsel,
805ba99b756SJianlong Huang unsigned long *configs,
806ba99b756SJianlong Huang unsigned int num_configs)
807ba99b756SJianlong Huang {
808ba99b756SJianlong Huang struct starfive_pinctrl *sfp = pinctrl_dev_get_drvdata(pctldev);
809ba99b756SJianlong Huang const struct group_desc *group;
810ba99b756SJianlong Huang u16 mask, value;
811ba99b756SJianlong Huang int i;
812ba99b756SJianlong Huang
813ba99b756SJianlong Huang group = pinctrl_generic_get_group(pctldev, gsel);
814ba99b756SJianlong Huang if (!group)
815ba99b756SJianlong Huang return -EINVAL;
816ba99b756SJianlong Huang
817ba99b756SJianlong Huang mask = 0;
818ba99b756SJianlong Huang value = 0;
819ba99b756SJianlong Huang for (i = 0; i < num_configs; i++) {
820ba99b756SJianlong Huang int param = pinconf_to_config_param(configs[i]);
821ba99b756SJianlong Huang u32 arg = pinconf_to_config_argument(configs[i]);
822ba99b756SJianlong Huang
823ba99b756SJianlong Huang switch (param) {
824ba99b756SJianlong Huang case PIN_CONFIG_BIAS_DISABLE:
825ba99b756SJianlong Huang mask |= PAD_BIAS_MASK;
826ba99b756SJianlong Huang value = (value & ~PAD_BIAS_MASK) | PAD_BIAS_DISABLE;
827ba99b756SJianlong Huang break;
828ba99b756SJianlong Huang case PIN_CONFIG_BIAS_PULL_DOWN:
829ba99b756SJianlong Huang if (arg == 0)
830ba99b756SJianlong Huang return -ENOTSUPP;
831ba99b756SJianlong Huang mask |= PAD_BIAS_MASK;
832ba99b756SJianlong Huang value = (value & ~PAD_BIAS_MASK) | PAD_BIAS_PULL_DOWN;
833ba99b756SJianlong Huang break;
834ba99b756SJianlong Huang case PIN_CONFIG_BIAS_PULL_UP:
835ba99b756SJianlong Huang if (arg == 0)
836ba99b756SJianlong Huang return -ENOTSUPP;
837ba99b756SJianlong Huang mask |= PAD_BIAS_MASK;
838ba99b756SJianlong Huang value = value & ~PAD_BIAS_MASK;
839ba99b756SJianlong Huang break;
840ba99b756SJianlong Huang case PIN_CONFIG_DRIVE_STRENGTH:
841ba99b756SJianlong Huang mask |= PAD_DRIVE_STRENGTH_MASK;
842ba99b756SJianlong Huang value = (value & ~PAD_DRIVE_STRENGTH_MASK) |
843ba99b756SJianlong Huang starfive_drive_strength_from_max_mA(arg);
844ba99b756SJianlong Huang break;
845ba99b756SJianlong Huang case PIN_CONFIG_INPUT_ENABLE:
846ba99b756SJianlong Huang mask |= PAD_INPUT_ENABLE;
847ba99b756SJianlong Huang if (arg)
848ba99b756SJianlong Huang value |= PAD_INPUT_ENABLE;
849ba99b756SJianlong Huang else
850ba99b756SJianlong Huang value &= ~PAD_INPUT_ENABLE;
851ba99b756SJianlong Huang break;
852ba99b756SJianlong Huang case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
853ba99b756SJianlong Huang mask |= PAD_INPUT_SCHMITT_ENABLE;
854ba99b756SJianlong Huang if (arg)
855ba99b756SJianlong Huang value |= PAD_INPUT_SCHMITT_ENABLE;
856ba99b756SJianlong Huang else
857ba99b756SJianlong Huang value &= ~PAD_INPUT_SCHMITT_ENABLE;
858ba99b756SJianlong Huang break;
859ba99b756SJianlong Huang case PIN_CONFIG_SLEW_RATE:
860ba99b756SJianlong Huang mask |= PAD_SLEW_RATE_MASK;
861ba99b756SJianlong Huang value = (value & ~PAD_SLEW_RATE_MASK) |
862ba99b756SJianlong Huang ((arg << PAD_SLEW_RATE_POS) & PAD_SLEW_RATE_MASK);
863ba99b756SJianlong Huang break;
864ba99b756SJianlong Huang case PIN_CONFIG_STARFIVE_STRONG_PULL_UP:
865ba99b756SJianlong Huang if (arg) {
866ba99b756SJianlong Huang mask |= PAD_BIAS_MASK;
867ba99b756SJianlong Huang value = (value & ~PAD_BIAS_MASK) |
868ba99b756SJianlong Huang PAD_BIAS_STRONG_PULL_UP;
869ba99b756SJianlong Huang } else {
870ba99b756SJianlong Huang mask |= PAD_BIAS_STRONG_PULL_UP;
871ba99b756SJianlong Huang value = value & ~PAD_BIAS_STRONG_PULL_UP;
872ba99b756SJianlong Huang }
873ba99b756SJianlong Huang break;
874ba99b756SJianlong Huang default:
875ba99b756SJianlong Huang return -ENOTSUPP;
876ba99b756SJianlong Huang }
877ba99b756SJianlong Huang }
878ba99b756SJianlong Huang
879ba99b756SJianlong Huang for (i = 0; i < group->num_pins; i++)
880ba99b756SJianlong Huang starfive_padctl_rmw(sfp, group->pins[i], mask, value);
881ba99b756SJianlong Huang
882ba99b756SJianlong Huang return 0;
883ba99b756SJianlong Huang }
884ba99b756SJianlong Huang
885ba99b756SJianlong Huang #ifdef CONFIG_DEBUG_FS
starfive_pinconf_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned int pin)886ba99b756SJianlong Huang static void starfive_pinconf_dbg_show(struct pinctrl_dev *pctldev,
887ba99b756SJianlong Huang struct seq_file *s, unsigned int pin)
888ba99b756SJianlong Huang {
889ba99b756SJianlong Huang struct starfive_pinctrl *sfp = pinctrl_dev_get_drvdata(pctldev);
890ba99b756SJianlong Huang u16 value = starfive_padctl_get(sfp, pin);
891ba99b756SJianlong Huang
892ba99b756SJianlong Huang seq_printf(s, " (0x%03x)", value);
893ba99b756SJianlong Huang }
894ba99b756SJianlong Huang #else
895ba99b756SJianlong Huang #define starfive_pinconf_dbg_show NULL
896ba99b756SJianlong Huang #endif
897ba99b756SJianlong Huang
898ba99b756SJianlong Huang static const struct pinconf_ops starfive_pinconf_ops = {
899ba99b756SJianlong Huang .pin_config_get = starfive_pinconf_get,
900ba99b756SJianlong Huang .pin_config_group_get = starfive_pinconf_group_get,
901ba99b756SJianlong Huang .pin_config_group_set = starfive_pinconf_group_set,
902ba99b756SJianlong Huang .pin_config_dbg_show = starfive_pinconf_dbg_show,
903ba99b756SJianlong Huang .is_generic = true,
904ba99b756SJianlong Huang };
905ba99b756SJianlong Huang
906ba99b756SJianlong Huang static struct pinctrl_desc starfive_desc = {
907ba99b756SJianlong Huang .name = DRIVER_NAME,
908ba99b756SJianlong Huang .pins = starfive_pins,
909ba99b756SJianlong Huang .npins = ARRAY_SIZE(starfive_pins),
910ba99b756SJianlong Huang .pctlops = &starfive_pinctrl_ops,
911ba99b756SJianlong Huang .pmxops = &starfive_pinmux_ops,
912ba99b756SJianlong Huang .confops = &starfive_pinconf_ops,
913ba99b756SJianlong Huang .owner = THIS_MODULE,
914ba99b756SJianlong Huang .num_custom_params = ARRAY_SIZE(starfive_pinconf_custom_params),
915ba99b756SJianlong Huang .custom_params = starfive_pinconf_custom_params,
916ba99b756SJianlong Huang .custom_conf_items = starfive_pinconf_custom_conf_items,
917ba99b756SJianlong Huang };
918ba99b756SJianlong Huang
starfive_gpio_request(struct gpio_chip * gc,unsigned int gpio)919ba99b756SJianlong Huang static int starfive_gpio_request(struct gpio_chip *gc, unsigned int gpio)
920ba99b756SJianlong Huang {
921ba99b756SJianlong Huang return pinctrl_gpio_request(gc->base + gpio);
922ba99b756SJianlong Huang }
923ba99b756SJianlong Huang
starfive_gpio_free(struct gpio_chip * gc,unsigned int gpio)924ba99b756SJianlong Huang static void starfive_gpio_free(struct gpio_chip *gc, unsigned int gpio)
925ba99b756SJianlong Huang {
926ba99b756SJianlong Huang pinctrl_gpio_free(gc->base + gpio);
927ba99b756SJianlong Huang }
928ba99b756SJianlong Huang
starfive_gpio_get_direction(struct gpio_chip * gc,unsigned int gpio)929ba99b756SJianlong Huang static int starfive_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
930ba99b756SJianlong Huang {
931ba99b756SJianlong Huang struct starfive_pinctrl *sfp = container_of(gc, struct starfive_pinctrl, gc);
932ba99b756SJianlong Huang void __iomem *doen = sfp->base + GPON_DOEN_CFG + 8 * gpio;
933ba99b756SJianlong Huang
934ba99b756SJianlong Huang if (readl_relaxed(doen) == GPO_ENABLE)
935ba99b756SJianlong Huang return GPIO_LINE_DIRECTION_OUT;
936ba99b756SJianlong Huang
937ba99b756SJianlong Huang return GPIO_LINE_DIRECTION_IN;
938ba99b756SJianlong Huang }
939ba99b756SJianlong Huang
starfive_gpio_direction_input(struct gpio_chip * gc,unsigned int gpio)940ba99b756SJianlong Huang static int starfive_gpio_direction_input(struct gpio_chip *gc,
941ba99b756SJianlong Huang unsigned int gpio)
942ba99b756SJianlong Huang {
943ba99b756SJianlong Huang struct starfive_pinctrl *sfp = container_of(gc, struct starfive_pinctrl, gc);
944ba99b756SJianlong Huang void __iomem *doen = sfp->base + GPON_DOEN_CFG + 8 * gpio;
945ba99b756SJianlong Huang unsigned long flags;
946ba99b756SJianlong Huang
947ba99b756SJianlong Huang /* enable input and schmitt trigger */
948ba99b756SJianlong Huang starfive_padctl_rmw(sfp, starfive_gpio_to_pin(sfp, gpio),
949ba99b756SJianlong Huang PAD_INPUT_ENABLE | PAD_INPUT_SCHMITT_ENABLE,
950ba99b756SJianlong Huang PAD_INPUT_ENABLE | PAD_INPUT_SCHMITT_ENABLE);
951ba99b756SJianlong Huang
952ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
953ba99b756SJianlong Huang writel_relaxed(GPO_DISABLE, doen);
954ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
955ba99b756SJianlong Huang return 0;
956ba99b756SJianlong Huang }
957ba99b756SJianlong Huang
starfive_gpio_direction_output(struct gpio_chip * gc,unsigned int gpio,int value)958ba99b756SJianlong Huang static int starfive_gpio_direction_output(struct gpio_chip *gc,
959ba99b756SJianlong Huang unsigned int gpio, int value)
960ba99b756SJianlong Huang {
961ba99b756SJianlong Huang struct starfive_pinctrl *sfp = container_of(gc, struct starfive_pinctrl, gc);
962ba99b756SJianlong Huang void __iomem *dout = sfp->base + GPON_DOUT_CFG + 8 * gpio;
963ba99b756SJianlong Huang void __iomem *doen = sfp->base + GPON_DOEN_CFG + 8 * gpio;
964ba99b756SJianlong Huang unsigned long flags;
965ba99b756SJianlong Huang
966ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
967ba99b756SJianlong Huang writel_relaxed(value, dout);
968ba99b756SJianlong Huang writel_relaxed(GPO_ENABLE, doen);
969ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
970ba99b756SJianlong Huang
971ba99b756SJianlong Huang /* disable input, schmitt trigger and bias */
972ba99b756SJianlong Huang starfive_padctl_rmw(sfp, starfive_gpio_to_pin(sfp, gpio),
973ba99b756SJianlong Huang PAD_BIAS_MASK | PAD_INPUT_ENABLE | PAD_INPUT_SCHMITT_ENABLE,
974ba99b756SJianlong Huang PAD_BIAS_DISABLE);
975ba99b756SJianlong Huang
976ba99b756SJianlong Huang return 0;
977ba99b756SJianlong Huang }
978ba99b756SJianlong Huang
starfive_gpio_get(struct gpio_chip * gc,unsigned int gpio)979ba99b756SJianlong Huang static int starfive_gpio_get(struct gpio_chip *gc, unsigned int gpio)
980ba99b756SJianlong Huang {
981ba99b756SJianlong Huang struct starfive_pinctrl *sfp = container_of(gc, struct starfive_pinctrl, gc);
982ba99b756SJianlong Huang void __iomem *din = sfp->base + GPIODIN + 4 * (gpio / 32);
983ba99b756SJianlong Huang
984ba99b756SJianlong Huang return !!(readl_relaxed(din) & BIT(gpio % 32));
985ba99b756SJianlong Huang }
986ba99b756SJianlong Huang
starfive_gpio_set(struct gpio_chip * gc,unsigned int gpio,int value)987ba99b756SJianlong Huang static void starfive_gpio_set(struct gpio_chip *gc, unsigned int gpio,
988ba99b756SJianlong Huang int value)
989ba99b756SJianlong Huang {
990ba99b756SJianlong Huang struct starfive_pinctrl *sfp = container_of(gc, struct starfive_pinctrl, gc);
991ba99b756SJianlong Huang void __iomem *dout = sfp->base + GPON_DOUT_CFG + 8 * gpio;
992ba99b756SJianlong Huang unsigned long flags;
993ba99b756SJianlong Huang
994ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
995ba99b756SJianlong Huang writel_relaxed(value, dout);
996ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
997ba99b756SJianlong Huang }
998ba99b756SJianlong Huang
starfive_gpio_set_config(struct gpio_chip * gc,unsigned int gpio,unsigned long config)999ba99b756SJianlong Huang static int starfive_gpio_set_config(struct gpio_chip *gc, unsigned int gpio,
1000ba99b756SJianlong Huang unsigned long config)
1001ba99b756SJianlong Huang {
1002ba99b756SJianlong Huang struct starfive_pinctrl *sfp = container_of(gc, struct starfive_pinctrl, gc);
1003ba99b756SJianlong Huang u32 arg = pinconf_to_config_argument(config);
1004ba99b756SJianlong Huang u16 value;
1005ba99b756SJianlong Huang u16 mask;
1006ba99b756SJianlong Huang
1007ba99b756SJianlong Huang switch (pinconf_to_config_param(config)) {
1008ba99b756SJianlong Huang case PIN_CONFIG_BIAS_DISABLE:
1009ba99b756SJianlong Huang mask = PAD_BIAS_MASK;
1010ba99b756SJianlong Huang value = PAD_BIAS_DISABLE;
1011ba99b756SJianlong Huang break;
1012ba99b756SJianlong Huang case PIN_CONFIG_BIAS_PULL_DOWN:
1013ba99b756SJianlong Huang if (arg == 0)
1014ba99b756SJianlong Huang return -ENOTSUPP;
1015ba99b756SJianlong Huang mask = PAD_BIAS_MASK;
1016ba99b756SJianlong Huang value = PAD_BIAS_PULL_DOWN;
1017ba99b756SJianlong Huang break;
1018ba99b756SJianlong Huang case PIN_CONFIG_BIAS_PULL_UP:
1019ba99b756SJianlong Huang if (arg == 0)
1020ba99b756SJianlong Huang return -ENOTSUPP;
1021ba99b756SJianlong Huang mask = PAD_BIAS_MASK;
1022ba99b756SJianlong Huang value = 0;
1023ba99b756SJianlong Huang break;
1024ba99b756SJianlong Huang case PIN_CONFIG_DRIVE_PUSH_PULL:
1025ba99b756SJianlong Huang return 0;
1026ba99b756SJianlong Huang case PIN_CONFIG_INPUT_ENABLE:
1027ba99b756SJianlong Huang mask = PAD_INPUT_ENABLE;
1028ba99b756SJianlong Huang value = arg ? PAD_INPUT_ENABLE : 0;
1029ba99b756SJianlong Huang break;
1030ba99b756SJianlong Huang case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1031ba99b756SJianlong Huang mask = PAD_INPUT_SCHMITT_ENABLE;
1032ba99b756SJianlong Huang value = arg ? PAD_INPUT_SCHMITT_ENABLE : 0;
1033ba99b756SJianlong Huang break;
1034ba99b756SJianlong Huang default:
1035ba99b756SJianlong Huang return -ENOTSUPP;
1036ba99b756SJianlong Huang }
1037ba99b756SJianlong Huang
1038ba99b756SJianlong Huang starfive_padctl_rmw(sfp, starfive_gpio_to_pin(sfp, gpio), mask, value);
1039ba99b756SJianlong Huang return 0;
1040ba99b756SJianlong Huang }
1041ba99b756SJianlong Huang
starfive_gpio_add_pin_ranges(struct gpio_chip * gc)1042ba99b756SJianlong Huang static int starfive_gpio_add_pin_ranges(struct gpio_chip *gc)
1043ba99b756SJianlong Huang {
1044ba99b756SJianlong Huang struct starfive_pinctrl *sfp = container_of(gc, struct starfive_pinctrl, gc);
1045ba99b756SJianlong Huang
1046ba99b756SJianlong Huang sfp->gpios.name = sfp->gc.label;
1047ba99b756SJianlong Huang sfp->gpios.base = sfp->gc.base;
1048ba99b756SJianlong Huang /*
1049ba99b756SJianlong Huang * sfp->gpios.pin_base depends on the chosen signal group
1050ba99b756SJianlong Huang * and is set in starfive_probe()
1051ba99b756SJianlong Huang */
1052ba99b756SJianlong Huang sfp->gpios.npins = NR_GPIOS;
1053ba99b756SJianlong Huang sfp->gpios.gc = &sfp->gc;
1054ba99b756SJianlong Huang pinctrl_add_gpio_range(sfp->pctl, &sfp->gpios);
1055ba99b756SJianlong Huang return 0;
1056ba99b756SJianlong Huang }
1057ba99b756SJianlong Huang
starfive_irq_ack(struct irq_data * d)1058ba99b756SJianlong Huang static void starfive_irq_ack(struct irq_data *d)
1059ba99b756SJianlong Huang {
1060ba99b756SJianlong Huang struct starfive_pinctrl *sfp = starfive_from_irq_data(d);
1061ba99b756SJianlong Huang irq_hw_number_t gpio = irqd_to_hwirq(d);
1062ba99b756SJianlong Huang void __iomem *ic = sfp->base + GPIOIC + 4 * (gpio / 32);
1063ba99b756SJianlong Huang u32 mask = BIT(gpio % 32);
1064ba99b756SJianlong Huang unsigned long flags;
1065ba99b756SJianlong Huang
1066ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
1067ba99b756SJianlong Huang writel_relaxed(mask, ic);
1068ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
1069ba99b756SJianlong Huang }
1070ba99b756SJianlong Huang
starfive_irq_mask(struct irq_data * d)1071ba99b756SJianlong Huang static void starfive_irq_mask(struct irq_data *d)
1072ba99b756SJianlong Huang {
1073ba99b756SJianlong Huang struct starfive_pinctrl *sfp = starfive_from_irq_data(d);
1074ba99b756SJianlong Huang irq_hw_number_t gpio = irqd_to_hwirq(d);
1075ba99b756SJianlong Huang void __iomem *ie = sfp->base + GPIOIE + 4 * (gpio / 32);
1076ba99b756SJianlong Huang u32 mask = BIT(gpio % 32);
1077ba99b756SJianlong Huang unsigned long flags;
1078ba99b756SJianlong Huang u32 value;
1079ba99b756SJianlong Huang
1080ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
1081ba99b756SJianlong Huang value = readl_relaxed(ie) & ~mask;
1082ba99b756SJianlong Huang writel_relaxed(value, ie);
1083ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
1084ba99b756SJianlong Huang
1085ac8a616cSGeert Uytterhoeven gpiochip_disable_irq(&sfp->gc, gpio);
1086ba99b756SJianlong Huang }
1087ba99b756SJianlong Huang
starfive_irq_mask_ack(struct irq_data * d)1088ba99b756SJianlong Huang static void starfive_irq_mask_ack(struct irq_data *d)
1089ba99b756SJianlong Huang {
1090ba99b756SJianlong Huang struct starfive_pinctrl *sfp = starfive_from_irq_data(d);
1091ba99b756SJianlong Huang irq_hw_number_t gpio = irqd_to_hwirq(d);
1092ba99b756SJianlong Huang void __iomem *ie = sfp->base + GPIOIE + 4 * (gpio / 32);
1093ba99b756SJianlong Huang void __iomem *ic = sfp->base + GPIOIC + 4 * (gpio / 32);
1094ba99b756SJianlong Huang u32 mask = BIT(gpio % 32);
1095ba99b756SJianlong Huang unsigned long flags;
1096ba99b756SJianlong Huang u32 value;
1097ba99b756SJianlong Huang
1098ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
1099ba99b756SJianlong Huang value = readl_relaxed(ie) & ~mask;
1100ba99b756SJianlong Huang writel_relaxed(value, ie);
1101ba99b756SJianlong Huang writel_relaxed(mask, ic);
1102ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
1103ba99b756SJianlong Huang }
1104ba99b756SJianlong Huang
starfive_irq_unmask(struct irq_data * d)1105ba99b756SJianlong Huang static void starfive_irq_unmask(struct irq_data *d)
1106ba99b756SJianlong Huang {
1107ba99b756SJianlong Huang struct starfive_pinctrl *sfp = starfive_from_irq_data(d);
1108ba99b756SJianlong Huang irq_hw_number_t gpio = irqd_to_hwirq(d);
1109ba99b756SJianlong Huang void __iomem *ie = sfp->base + GPIOIE + 4 * (gpio / 32);
1110ba99b756SJianlong Huang u32 mask = BIT(gpio % 32);
1111ba99b756SJianlong Huang unsigned long flags;
1112ba99b756SJianlong Huang u32 value;
1113ba99b756SJianlong Huang
1114ac8a616cSGeert Uytterhoeven gpiochip_enable_irq(&sfp->gc, gpio);
1115ba99b756SJianlong Huang
1116ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
1117ba99b756SJianlong Huang value = readl_relaxed(ie) | mask;
1118ba99b756SJianlong Huang writel_relaxed(value, ie);
1119ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
1120ba99b756SJianlong Huang }
1121ba99b756SJianlong Huang
starfive_irq_set_type(struct irq_data * d,unsigned int trigger)1122ba99b756SJianlong Huang static int starfive_irq_set_type(struct irq_data *d, unsigned int trigger)
1123ba99b756SJianlong Huang {
1124ba99b756SJianlong Huang struct starfive_pinctrl *sfp = starfive_from_irq_data(d);
1125ba99b756SJianlong Huang irq_hw_number_t gpio = irqd_to_hwirq(d);
1126ba99b756SJianlong Huang void __iomem *base = sfp->base + 4 * (gpio / 32);
1127ba99b756SJianlong Huang u32 mask = BIT(gpio % 32);
1128ba99b756SJianlong Huang u32 irq_type, edge_both, polarity;
1129ba99b756SJianlong Huang unsigned long flags;
1130ba99b756SJianlong Huang
1131ba99b756SJianlong Huang switch (trigger) {
1132ba99b756SJianlong Huang case IRQ_TYPE_EDGE_RISING:
1133ba99b756SJianlong Huang irq_type = mask; /* 1: edge triggered */
1134ba99b756SJianlong Huang edge_both = 0; /* 0: single edge */
1135ba99b756SJianlong Huang polarity = mask; /* 1: rising edge */
1136ba99b756SJianlong Huang break;
1137ba99b756SJianlong Huang case IRQ_TYPE_EDGE_FALLING:
1138ba99b756SJianlong Huang irq_type = mask; /* 1: edge triggered */
1139ba99b756SJianlong Huang edge_both = 0; /* 0: single edge */
1140ba99b756SJianlong Huang polarity = 0; /* 0: falling edge */
1141ba99b756SJianlong Huang break;
1142ba99b756SJianlong Huang case IRQ_TYPE_EDGE_BOTH:
1143ba99b756SJianlong Huang irq_type = mask; /* 1: edge triggered */
1144ba99b756SJianlong Huang edge_both = mask; /* 1: both edges */
1145ba99b756SJianlong Huang polarity = 0; /* 0: ignored */
1146ba99b756SJianlong Huang break;
1147ba99b756SJianlong Huang case IRQ_TYPE_LEVEL_HIGH:
1148ba99b756SJianlong Huang irq_type = 0; /* 0: level triggered */
1149ba99b756SJianlong Huang edge_both = 0; /* 0: ignored */
1150ba99b756SJianlong Huang polarity = mask; /* 1: high level */
1151ba99b756SJianlong Huang break;
1152ba99b756SJianlong Huang case IRQ_TYPE_LEVEL_LOW:
1153ba99b756SJianlong Huang irq_type = 0; /* 0: level triggered */
1154ba99b756SJianlong Huang edge_both = 0; /* 0: ignored */
1155ba99b756SJianlong Huang polarity = 0; /* 0: low level */
1156ba99b756SJianlong Huang break;
1157ba99b756SJianlong Huang default:
1158ba99b756SJianlong Huang return -EINVAL;
1159ba99b756SJianlong Huang }
1160ba99b756SJianlong Huang
1161ba99b756SJianlong Huang if (trigger & IRQ_TYPE_EDGE_BOTH)
1162ba99b756SJianlong Huang irq_set_handler_locked(d, handle_edge_irq);
1163ba99b756SJianlong Huang else
1164ba99b756SJianlong Huang irq_set_handler_locked(d, handle_level_irq);
1165ba99b756SJianlong Huang
1166ba99b756SJianlong Huang raw_spin_lock_irqsave(&sfp->lock, flags);
1167ba99b756SJianlong Huang irq_type |= readl_relaxed(base + GPIOIS) & ~mask;
1168ba99b756SJianlong Huang writel_relaxed(irq_type, base + GPIOIS);
1169ba99b756SJianlong Huang edge_both |= readl_relaxed(base + GPIOIBE) & ~mask;
1170ba99b756SJianlong Huang writel_relaxed(edge_both, base + GPIOIBE);
1171ba99b756SJianlong Huang polarity |= readl_relaxed(base + GPIOIEV) & ~mask;
1172ba99b756SJianlong Huang writel_relaxed(polarity, base + GPIOIEV);
1173ba99b756SJianlong Huang raw_spin_unlock_irqrestore(&sfp->lock, flags);
1174ba99b756SJianlong Huang return 0;
1175ba99b756SJianlong Huang }
1176ba99b756SJianlong Huang
1177ba99b756SJianlong Huang static const struct irq_chip starfive_irq_chip = {
1178ba99b756SJianlong Huang .name = "StarFive GPIO",
1179ba99b756SJianlong Huang .irq_ack = starfive_irq_ack,
1180ba99b756SJianlong Huang .irq_mask = starfive_irq_mask,
1181ba99b756SJianlong Huang .irq_mask_ack = starfive_irq_mask_ack,
1182ba99b756SJianlong Huang .irq_unmask = starfive_irq_unmask,
1183ba99b756SJianlong Huang .irq_set_type = starfive_irq_set_type,
1184ba99b756SJianlong Huang .flags = IRQCHIP_IMMUTABLE | IRQCHIP_SET_TYPE_MASKED,
1185ba99b756SJianlong Huang GPIOCHIP_IRQ_RESOURCE_HELPERS,
1186ba99b756SJianlong Huang };
1187ba99b756SJianlong Huang
starfive_gpio_irq_handler(struct irq_desc * desc)1188ba99b756SJianlong Huang static void starfive_gpio_irq_handler(struct irq_desc *desc)
1189ba99b756SJianlong Huang {
1190ba99b756SJianlong Huang struct starfive_pinctrl *sfp = starfive_from_irq_desc(desc);
1191ba99b756SJianlong Huang struct irq_chip *chip = irq_desc_get_chip(desc);
1192ba99b756SJianlong Huang unsigned long mis;
1193ba99b756SJianlong Huang unsigned int pin;
1194ba99b756SJianlong Huang
1195ba99b756SJianlong Huang chained_irq_enter(chip, desc);
1196ba99b756SJianlong Huang
1197ba99b756SJianlong Huang mis = readl_relaxed(sfp->base + GPIOMIS + 0);
1198ba99b756SJianlong Huang for_each_set_bit(pin, &mis, 32)
1199ba99b756SJianlong Huang generic_handle_domain_irq(sfp->gc.irq.domain, pin);
1200ba99b756SJianlong Huang
1201ba99b756SJianlong Huang mis = readl_relaxed(sfp->base + GPIOMIS + 4);
1202ba99b756SJianlong Huang for_each_set_bit(pin, &mis, 32)
1203ba99b756SJianlong Huang generic_handle_domain_irq(sfp->gc.irq.domain, pin + 32);
1204ba99b756SJianlong Huang
1205ba99b756SJianlong Huang chained_irq_exit(chip, desc);
1206ba99b756SJianlong Huang }
1207ba99b756SJianlong Huang
starfive_gpio_init_hw(struct gpio_chip * gc)1208ba99b756SJianlong Huang static int starfive_gpio_init_hw(struct gpio_chip *gc)
1209ba99b756SJianlong Huang {
1210ba99b756SJianlong Huang struct starfive_pinctrl *sfp = container_of(gc, struct starfive_pinctrl, gc);
1211ba99b756SJianlong Huang
1212ba99b756SJianlong Huang /* mask all GPIO interrupts */
1213ba99b756SJianlong Huang writel(0, sfp->base + GPIOIE + 0);
1214ba99b756SJianlong Huang writel(0, sfp->base + GPIOIE + 4);
1215ba99b756SJianlong Huang /* clear edge interrupt flags */
1216ba99b756SJianlong Huang writel(~0U, sfp->base + GPIOIC + 0);
1217ba99b756SJianlong Huang writel(~0U, sfp->base + GPIOIC + 4);
1218ba99b756SJianlong Huang /* enable GPIO interrupts */
1219ba99b756SJianlong Huang writel(1, sfp->base + GPIOEN);
1220ba99b756SJianlong Huang return 0;
1221ba99b756SJianlong Huang }
1222ba99b756SJianlong Huang
starfive_disable_clock(void * data)1223ba99b756SJianlong Huang static void starfive_disable_clock(void *data)
1224ba99b756SJianlong Huang {
1225ba99b756SJianlong Huang clk_disable_unprepare(data);
1226ba99b756SJianlong Huang }
1227ba99b756SJianlong Huang
starfive_probe(struct platform_device * pdev)1228ba99b756SJianlong Huang static int starfive_probe(struct platform_device *pdev)
1229ba99b756SJianlong Huang {
1230ba99b756SJianlong Huang struct device *dev = &pdev->dev;
1231ba99b756SJianlong Huang struct starfive_pinctrl *sfp;
1232ba99b756SJianlong Huang struct reset_control *rst;
1233ba99b756SJianlong Huang struct clk *clk;
1234ba99b756SJianlong Huang u32 value;
1235ba99b756SJianlong Huang int ret;
1236ba99b756SJianlong Huang
1237ba99b756SJianlong Huang sfp = devm_kzalloc(dev, sizeof(*sfp), GFP_KERNEL);
1238ba99b756SJianlong Huang if (!sfp)
1239ba99b756SJianlong Huang return -ENOMEM;
1240ba99b756SJianlong Huang
1241ba99b756SJianlong Huang sfp->base = devm_platform_ioremap_resource_byname(pdev, "gpio");
1242ba99b756SJianlong Huang if (IS_ERR(sfp->base))
1243ba99b756SJianlong Huang return PTR_ERR(sfp->base);
1244ba99b756SJianlong Huang
1245ba99b756SJianlong Huang sfp->padctl = devm_platform_ioremap_resource_byname(pdev, "padctl");
1246ba99b756SJianlong Huang if (IS_ERR(sfp->padctl))
1247ba99b756SJianlong Huang return PTR_ERR(sfp->padctl);
1248ba99b756SJianlong Huang
1249ba99b756SJianlong Huang clk = devm_clk_get(dev, NULL);
1250ba99b756SJianlong Huang if (IS_ERR(clk))
1251ba99b756SJianlong Huang return dev_err_probe(dev, PTR_ERR(clk), "could not get clock\n");
1252ba99b756SJianlong Huang
1253ba99b756SJianlong Huang rst = devm_reset_control_get_exclusive(dev, NULL);
1254ba99b756SJianlong Huang if (IS_ERR(rst))
1255ba99b756SJianlong Huang return dev_err_probe(dev, PTR_ERR(rst), "could not get reset\n");
1256ba99b756SJianlong Huang
1257ba99b756SJianlong Huang ret = clk_prepare_enable(clk);
1258ba99b756SJianlong Huang if (ret)
1259ba99b756SJianlong Huang return dev_err_probe(dev, ret, "could not enable clock\n");
1260ba99b756SJianlong Huang
1261ba99b756SJianlong Huang ret = devm_add_action_or_reset(dev, starfive_disable_clock, clk);
1262ba99b756SJianlong Huang if (ret)
1263ba99b756SJianlong Huang return ret;
1264ba99b756SJianlong Huang
1265ba99b756SJianlong Huang /*
1266ba99b756SJianlong Huang * We don't want to assert reset and risk undoing pin muxing for the
1267ba99b756SJianlong Huang * early boot serial console, but let's make sure the reset line is
1268ba99b756SJianlong Huang * deasserted in case someone runs a really minimal bootloader.
1269ba99b756SJianlong Huang */
1270ba99b756SJianlong Huang ret = reset_control_deassert(rst);
1271ba99b756SJianlong Huang if (ret)
1272ba99b756SJianlong Huang return dev_err_probe(dev, ret, "could not deassert reset\n");
1273ba99b756SJianlong Huang
1274ba99b756SJianlong Huang platform_set_drvdata(pdev, sfp);
1275ba99b756SJianlong Huang sfp->gc.parent = dev;
1276ba99b756SJianlong Huang raw_spin_lock_init(&sfp->lock);
1277ba99b756SJianlong Huang mutex_init(&sfp->mutex);
1278ba99b756SJianlong Huang
1279ba99b756SJianlong Huang ret = devm_pinctrl_register_and_init(dev, &starfive_desc, sfp, &sfp->pctl);
1280ba99b756SJianlong Huang if (ret)
1281ba99b756SJianlong Huang return dev_err_probe(dev, ret, "could not register pinctrl driver\n");
1282ba99b756SJianlong Huang
1283ba99b756SJianlong Huang if (!of_property_read_u32(dev->of_node, "starfive,signal-group", &value)) {
1284ba99b756SJianlong Huang if (value > 6)
1285ba99b756SJianlong Huang return dev_err_probe(dev, -EINVAL, "invalid signal group %u\n", value);
1286ba99b756SJianlong Huang writel(value, sfp->padctl + IO_PADSHARE_SEL);
1287ba99b756SJianlong Huang }
1288ba99b756SJianlong Huang
1289ba99b756SJianlong Huang value = readl(sfp->padctl + IO_PADSHARE_SEL);
1290ba99b756SJianlong Huang switch (value) {
1291ba99b756SJianlong Huang case 0:
1292ba99b756SJianlong Huang sfp->gpios.pin_base = PAD_INVALID_GPIO;
1293ba99b756SJianlong Huang goto out_pinctrl_enable;
1294ba99b756SJianlong Huang case 1:
1295ba99b756SJianlong Huang sfp->gpios.pin_base = PAD_GPIO(0);
1296ba99b756SJianlong Huang break;
1297ba99b756SJianlong Huang case 2:
1298ba99b756SJianlong Huang sfp->gpios.pin_base = PAD_FUNC_SHARE(72);
1299ba99b756SJianlong Huang break;
1300ba99b756SJianlong Huang case 3:
1301ba99b756SJianlong Huang sfp->gpios.pin_base = PAD_FUNC_SHARE(70);
1302ba99b756SJianlong Huang break;
1303ba99b756SJianlong Huang case 4: case 5: case 6:
1304ba99b756SJianlong Huang sfp->gpios.pin_base = PAD_FUNC_SHARE(0);
1305ba99b756SJianlong Huang break;
1306ba99b756SJianlong Huang default:
1307ba99b756SJianlong Huang return dev_err_probe(dev, -EINVAL, "invalid signal group %u\n", value);
1308ba99b756SJianlong Huang }
1309ba99b756SJianlong Huang
1310ba99b756SJianlong Huang sfp->gc.label = dev_name(dev);
1311ba99b756SJianlong Huang sfp->gc.owner = THIS_MODULE;
1312ba99b756SJianlong Huang sfp->gc.request = starfive_gpio_request;
1313ba99b756SJianlong Huang sfp->gc.free = starfive_gpio_free;
1314ba99b756SJianlong Huang sfp->gc.get_direction = starfive_gpio_get_direction;
1315ba99b756SJianlong Huang sfp->gc.direction_input = starfive_gpio_direction_input;
1316ba99b756SJianlong Huang sfp->gc.direction_output = starfive_gpio_direction_output;
1317ba99b756SJianlong Huang sfp->gc.get = starfive_gpio_get;
1318ba99b756SJianlong Huang sfp->gc.set = starfive_gpio_set;
1319ba99b756SJianlong Huang sfp->gc.set_config = starfive_gpio_set_config;
1320ba99b756SJianlong Huang sfp->gc.add_pin_ranges = starfive_gpio_add_pin_ranges;
1321ba99b756SJianlong Huang sfp->gc.base = -1;
1322ba99b756SJianlong Huang sfp->gc.ngpio = NR_GPIOS;
1323ba99b756SJianlong Huang
1324ba99b756SJianlong Huang gpio_irq_chip_set_chip(&sfp->gc.irq, &starfive_irq_chip);
1325ba99b756SJianlong Huang sfp->gc.irq.parent_handler = starfive_gpio_irq_handler;
1326ba99b756SJianlong Huang sfp->gc.irq.num_parents = 1;
1327ba99b756SJianlong Huang sfp->gc.irq.parents = devm_kcalloc(dev, sfp->gc.irq.num_parents,
1328ba99b756SJianlong Huang sizeof(*sfp->gc.irq.parents), GFP_KERNEL);
1329ba99b756SJianlong Huang if (!sfp->gc.irq.parents)
1330ba99b756SJianlong Huang return -ENOMEM;
1331ba99b756SJianlong Huang sfp->gc.irq.default_type = IRQ_TYPE_NONE;
1332ba99b756SJianlong Huang sfp->gc.irq.handler = handle_bad_irq;
1333ba99b756SJianlong Huang sfp->gc.irq.init_hw = starfive_gpio_init_hw;
1334ba99b756SJianlong Huang
1335ba99b756SJianlong Huang ret = platform_get_irq(pdev, 0);
1336ba99b756SJianlong Huang if (ret < 0)
1337ba99b756SJianlong Huang return ret;
1338ba99b756SJianlong Huang sfp->gc.irq.parents[0] = ret;
1339ba99b756SJianlong Huang
1340ba99b756SJianlong Huang ret = devm_gpiochip_add_data(dev, &sfp->gc, sfp);
1341ba99b756SJianlong Huang if (ret)
1342ba99b756SJianlong Huang return dev_err_probe(dev, ret, "could not register gpiochip\n");
1343ba99b756SJianlong Huang
1344ba99b756SJianlong Huang irq_domain_set_pm_device(sfp->gc.irq.domain, dev);
1345ba99b756SJianlong Huang
1346ba99b756SJianlong Huang out_pinctrl_enable:
1347ba99b756SJianlong Huang return pinctrl_enable(sfp->pctl);
1348ba99b756SJianlong Huang }
1349ba99b756SJianlong Huang
1350ba99b756SJianlong Huang static const struct of_device_id starfive_of_match[] = {
1351ba99b756SJianlong Huang { .compatible = "starfive,jh7100-pinctrl" },
1352ba99b756SJianlong Huang { /* sentinel */ }
1353ba99b756SJianlong Huang };
1354ba99b756SJianlong Huang MODULE_DEVICE_TABLE(of, starfive_of_match);
1355ba99b756SJianlong Huang
1356ba99b756SJianlong Huang static struct platform_driver starfive_pinctrl_driver = {
1357ba99b756SJianlong Huang .probe = starfive_probe,
1358ba99b756SJianlong Huang .driver = {
1359ba99b756SJianlong Huang .name = DRIVER_NAME,
1360ba99b756SJianlong Huang .of_match_table = starfive_of_match,
1361ba99b756SJianlong Huang },
1362ba99b756SJianlong Huang };
1363ba99b756SJianlong Huang module_platform_driver(starfive_pinctrl_driver);
1364ba99b756SJianlong Huang
1365ba99b756SJianlong Huang MODULE_DESCRIPTION("Pinctrl driver for StarFive SoCs");
1366ba99b756SJianlong Huang MODULE_AUTHOR("Emil Renner Berthing <kernel@esmil.dk>");
1367ba99b756SJianlong Huang MODULE_LICENSE("GPL v2");
1368