1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3  * Microsemi SoCs pinctrl driver
4  *
5  * Author: <gregory.clement@bootlin.com>
6  * License: Dual MIT/GPL
7  * Copyright (c) 2018 Microsemi Corporation
8  */
9 
10 #include <config.h>
11 #include <dm.h>
12 #include <dm/device-internal.h>
13 #include <dm/lists.h>
14 #include <dm/pinctrl.h>
15 #include <dm/root.h>
16 #include <errno.h>
17 #include <fdtdec.h>
18 #include <linux/io.h>
19 #include <asm/gpio.h>
20 #include <asm/system.h>
21 #include "mscc-common.h"
22 
23 enum {
24 	FUNC_NONE,
25 	FUNC_GPIO,
26 	FUNC_SIO,
27 	FUNC_TACHO,
28 	FUNC_TWI,
29 	FUNC_PHY_LED,
30 	FUNC_EXT_IRQ,
31 	FUNC_SFP,
32 	FUNC_SI,
33 	FUNC_PWM,
34 	FUNC_UART,
35 	FUNC_MAX
36 };
37 
38 static char * const luton_function_names[] = {
39 	[FUNC_NONE]		= "none",
40 	[FUNC_GPIO]		= "gpio",
41 	[FUNC_SIO]		= "sio",
42 	[FUNC_TACHO]		= "tacho",
43 	[FUNC_TWI]		= "twi",
44 	[FUNC_PHY_LED]		= "phy_led",
45 	[FUNC_EXT_IRQ]		= "ext_irq",
46 	[FUNC_SFP]		= "sfp",
47 	[FUNC_SI]		= "si",
48 	[FUNC_PWM]		= "pwm",
49 	[FUNC_UART]		= "uart",
50 };
51 
52 MSCC_P(0,  SIO,       NONE,      NONE);
53 MSCC_P(1,  SIO,       NONE,      NONE);
54 MSCC_P(2,  SIO,       NONE,      NONE);
55 MSCC_P(3,  SIO,       NONE,      NONE);
56 MSCC_P(4,  TACHO,     NONE,      NONE);
57 MSCC_P(5,  TWI,       PHY_LED,   NONE);
58 MSCC_P(6,  TWI,       PHY_LED,   NONE);
59 MSCC_P(7,  NONE,      PHY_LED,   NONE);
60 MSCC_P(8,  EXT_IRQ,   PHY_LED,   NONE);
61 MSCC_P(9,  EXT_IRQ,   PHY_LED,   NONE);
62 MSCC_P(10, SFP,       PHY_LED,   NONE);
63 MSCC_P(11, SFP,       PHY_LED,   NONE);
64 MSCC_P(12, SFP,       PHY_LED,   NONE);
65 MSCC_P(13, SFP,       PHY_LED,   NONE);
66 MSCC_P(14, SI,        PHY_LED,   NONE);
67 MSCC_P(15, SI,        PHY_LED,   NONE);
68 MSCC_P(16, SI,        PHY_LED,   NONE);
69 MSCC_P(17, SFP,       PHY_LED,   NONE);
70 MSCC_P(18, SFP,       PHY_LED,   NONE);
71 MSCC_P(19, SFP,       PHY_LED,   NONE);
72 MSCC_P(20, SFP,       PHY_LED,   NONE);
73 MSCC_P(21, SFP,       PHY_LED,   NONE);
74 MSCC_P(22, SFP,       PHY_LED,   NONE);
75 MSCC_P(23, SFP,       PHY_LED,   NONE);
76 MSCC_P(24, SFP,       PHY_LED,   NONE);
77 MSCC_P(25, SFP,       PHY_LED,   NONE);
78 MSCC_P(26, SFP,       PHY_LED,   NONE);
79 MSCC_P(27, SFP,       PHY_LED,   NONE);
80 MSCC_P(28, SFP,       PHY_LED,   NONE);
81 MSCC_P(29, PWM,       NONE,      NONE);
82 MSCC_P(30, UART,      NONE,      NONE);
83 MSCC_P(31, UART,      NONE,      NONE);
84 
85 #define LUTON_PIN(n) {						\
86 	.name = "GPIO_"#n,					\
87 	.drv_data = &mscc_pin_##n				\
88 }
89 
90 static const struct mscc_pin_data luton_pins[] = {
91 	LUTON_PIN(0),
92 	LUTON_PIN(1),
93 	LUTON_PIN(2),
94 	LUTON_PIN(3),
95 	LUTON_PIN(4),
96 	LUTON_PIN(5),
97 	LUTON_PIN(6),
98 	LUTON_PIN(7),
99 	LUTON_PIN(8),
100 	LUTON_PIN(9),
101 	LUTON_PIN(10),
102 	LUTON_PIN(11),
103 	LUTON_PIN(12),
104 	LUTON_PIN(13),
105 	LUTON_PIN(14),
106 	LUTON_PIN(15),
107 	LUTON_PIN(16),
108 	LUTON_PIN(17),
109 	LUTON_PIN(18),
110 	LUTON_PIN(19),
111 	LUTON_PIN(20),
112 	LUTON_PIN(21),
113 	LUTON_PIN(22),
114 	LUTON_PIN(23),
115 	LUTON_PIN(24),
116 	LUTON_PIN(25),
117 	LUTON_PIN(26),
118 	LUTON_PIN(27),
119 	LUTON_PIN(28),
120 	LUTON_PIN(29),
121 	LUTON_PIN(30),
122 	LUTON_PIN(31),
123 };
124 
125 static const unsigned long luton_gpios[] = {
126 	[MSCC_GPIO_OUT_SET] = 0x00,
127 	[MSCC_GPIO_OUT_CLR] = 0x04,
128 	[MSCC_GPIO_OUT] = 0x08,
129 	[MSCC_GPIO_IN] = 0x0c,
130 	[MSCC_GPIO_OE] = 0x10,
131 	[MSCC_GPIO_INTR] = 0x14,
132 	[MSCC_GPIO_INTR_ENA] = 0x18,
133 	[MSCC_GPIO_INTR_IDENT] = 0x1c,
134 	[MSCC_GPIO_ALT0] = 0x20,
135 	[MSCC_GPIO_ALT1] = 0x24,
136 };
137 
luton_gpio_probe(struct udevice * dev)138 static int luton_gpio_probe(struct udevice *dev)
139 {
140 	struct gpio_dev_priv *uc_priv;
141 
142 	uc_priv = dev_get_uclass_priv(dev);
143 	uc_priv->bank_name = "luton-gpio";
144 	uc_priv->gpio_count = ARRAY_SIZE(luton_pins);
145 
146 	return 0;
147 }
148 
149 static struct driver luton_gpio_driver = {
150 	.name	= "luton-gpio",
151 	.id	= UCLASS_GPIO,
152 	.probe	= luton_gpio_probe,
153 	.ops	= &mscc_gpio_ops,
154 };
155 
luton_pinctrl_probe(struct udevice * dev)156 int luton_pinctrl_probe(struct udevice *dev)
157 {
158 	int ret;
159 
160 	ret = mscc_pinctrl_probe(dev, FUNC_MAX, luton_pins,
161 				 ARRAY_SIZE(luton_pins), luton_function_names,
162 				 luton_gpios);
163 
164 	if (ret)
165 		return ret;
166 
167 	ret = device_bind(dev, &luton_gpio_driver, "luton-gpio", NULL,
168 			  dev_ofnode(dev), NULL);
169 
170 	return 0;
171 }
172 
173 static const struct udevice_id luton_pinctrl_of_match[] = {
174 	{.compatible = "mscc,luton-pinctrl"},
175 	{},
176 };
177 
178 U_BOOT_DRIVER(luton_pinctrl) = {
179 	.name = "luton-pinctrl",
180 	.id = UCLASS_PINCTRL,
181 	.of_match = of_match_ptr(luton_pinctrl_of_match),
182 	.probe = luton_pinctrl_probe,
183 	.priv_auto	= sizeof(struct mscc_pinctrl),
184 	.ops = &mscc_pinctrl_ops,
185 };
186