xref: /linux/drivers/pinctrl/pinctrl-loongson2.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Author: zhanghongchen <zhanghongchen@loongson.cn>
4  *         Yinbo Zhu <zhuyinbo@loongson.cn>
5  * Copyright (C) 2022-2023 Loongson Technology Corporation Limited
6  */
7 
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/pinctrl/pinmux.h>
13 #include <linux/pinctrl/pinconf-generic.h>
14 #include <linux/pinctrl/pinctrl.h>
15 #include <linux/bitops.h>
16 #include <linux/io.h>
17 #include <linux/seq_file.h>
18 
19 #include "core.h"
20 #include "pinctrl-utils.h"
21 
22 #define PMX_GROUP(name, offset, bitv)					\
23 	{								\
24 		.grp = PINCTRL_PINGROUP((#name), (name ## _pins),	\
25 				ARRAY_SIZE((name ## _pins))),		\
26 		.reg = offset,						\
27 		.bit = bitv,						\
28 	}
29 
30 #define SPECIFIC_GROUP(group)						\
31 	static const char * const group##_groups[] = {			\
32 		#group							\
33 	}
34 
35 #define FUNCTION(fn)							\
36 	{								\
37 		.name = #fn,						\
38 		.groups = fn ## _groups,				\
39 		.num_groups = ARRAY_SIZE(fn ## _groups),		\
40 	}
41 
42 struct loongson2_pinctrl {
43 	struct device *dev;
44 	struct pinctrl_dev *pcdev;
45 	struct pinctrl_desc desc;
46 	struct device_node *of_node;
47 	spinlock_t lock;
48 	void __iomem *reg_base;
49 };
50 
51 struct loongson2_pmx_group {
52 	struct pingroup grp;
53 	unsigned int reg;
54 	unsigned int bit;
55 };
56 
57 struct loongson2_pmx_func {
58 	const char *name;
59 	const char * const *groups;
60 	unsigned int num_groups;
61 };
62 
63 #define LOONGSON2_PIN(x) PINCTRL_PIN(x, "gpio"#x)
64 static const struct pinctrl_pin_desc loongson2_pctrl_pins[] = {
65 	LOONGSON2_PIN(0),  LOONGSON2_PIN(1),  LOONGSON2_PIN(2),  LOONGSON2_PIN(3),
66 	LOONGSON2_PIN(4),  LOONGSON2_PIN(5),  LOONGSON2_PIN(6),  LOONGSON2_PIN(7),
67 	LOONGSON2_PIN(8),  LOONGSON2_PIN(9),  LOONGSON2_PIN(10), LOONGSON2_PIN(11),
68 	LOONGSON2_PIN(12), LOONGSON2_PIN(13), LOONGSON2_PIN(14),
69 	LOONGSON2_PIN(16), LOONGSON2_PIN(17), LOONGSON2_PIN(18), LOONGSON2_PIN(19),
70 	LOONGSON2_PIN(20), LOONGSON2_PIN(21), LOONGSON2_PIN(22), LOONGSON2_PIN(23),
71 	LOONGSON2_PIN(24), LOONGSON2_PIN(25), LOONGSON2_PIN(26), LOONGSON2_PIN(27),
72 	LOONGSON2_PIN(28), LOONGSON2_PIN(29), LOONGSON2_PIN(30),
73 	LOONGSON2_PIN(32), LOONGSON2_PIN(33), LOONGSON2_PIN(34), LOONGSON2_PIN(35),
74 	LOONGSON2_PIN(36), LOONGSON2_PIN(37), LOONGSON2_PIN(38), LOONGSON2_PIN(39),
75 	LOONGSON2_PIN(40), LOONGSON2_PIN(41),
76 	LOONGSON2_PIN(44), LOONGSON2_PIN(45), LOONGSON2_PIN(46), LOONGSON2_PIN(47),
77 	LOONGSON2_PIN(48), LOONGSON2_PIN(49), LOONGSON2_PIN(50), LOONGSON2_PIN(51),
78 	LOONGSON2_PIN(52), LOONGSON2_PIN(53), LOONGSON2_PIN(54), LOONGSON2_PIN(55),
79 	LOONGSON2_PIN(56), LOONGSON2_PIN(57), LOONGSON2_PIN(58), LOONGSON2_PIN(59),
80 	LOONGSON2_PIN(60), LOONGSON2_PIN(61), LOONGSON2_PIN(62), LOONGSON2_PIN(63),
81 };
82 
83 static const unsigned int gpio_pins[] = {0, 1, 2, 3, 4, 5, 6, 7,
84 					 8, 9, 10, 11, 12, 13, 14,
85 					 16, 17, 18, 19, 20, 21, 22, 23,
86 					 24, 25, 26, 27, 28, 29, 30,
87 					 32, 33, 34, 35, 36, 37, 38, 39,
88 					 40,         43, 44, 45, 46, 47,
89 					 48, 49, 50, 51, 52, 53, 46, 55,
90 					 56, 57, 58, 59, 60, 61, 62, 63};
91 static const unsigned int sdio_pins[] = {36, 37, 38, 39, 40, 41};
92 static const unsigned int can1_pins[] = {34, 35};
93 static const unsigned int can0_pins[] = {32, 33};
94 static const unsigned int pwm3_pins[] = {23};
95 static const unsigned int pwm2_pins[] = {22};
96 static const unsigned int pwm1_pins[] = {21};
97 static const unsigned int pwm0_pins[] = {20};
98 static const unsigned int i2c1_pins[] = {18, 19};
99 static const unsigned int i2c0_pins[] = {16, 17};
100 static const unsigned int nand_pins[] = {44, 45, 46, 47, 48, 49, 50, 51,
101 					 52, 53, 54, 55, 56, 57, 58, 59, 60,
102 					 61, 62, 63};
103 static const unsigned int sata_led_pins[] = {14};
104 static const unsigned int i2s_pins[]    = {24, 25, 26, 27, 28};
105 static const unsigned int hda_pins[]    = {24, 25, 26, 27, 28, 29, 30};
106 
107 static struct loongson2_pmx_group loongson2_pmx_groups[] = {
108 	PMX_GROUP(gpio, 0x0, 64),
109 	PMX_GROUP(sdio, 0x0, 20),
110 	PMX_GROUP(can1, 0x0, 17),
111 	PMX_GROUP(can0, 0x0, 16),
112 	PMX_GROUP(pwm3, 0x0, 15),
113 	PMX_GROUP(pwm2, 0x0, 14),
114 	PMX_GROUP(pwm1, 0x0, 13),
115 	PMX_GROUP(pwm0, 0x0, 12),
116 	PMX_GROUP(i2c1, 0x0, 11),
117 	PMX_GROUP(i2c0, 0x0, 10),
118 	PMX_GROUP(nand, 0x0, 9),
119 	PMX_GROUP(sata_led, 0x0, 8),
120 	PMX_GROUP(i2s, 0x0, 6),
121 	PMX_GROUP(hda, 0x0, 4),
122 };
123 
124 SPECIFIC_GROUP(sdio);
125 SPECIFIC_GROUP(can1);
126 SPECIFIC_GROUP(can0);
127 SPECIFIC_GROUP(pwm3);
128 SPECIFIC_GROUP(pwm2);
129 SPECIFIC_GROUP(pwm1);
130 SPECIFIC_GROUP(pwm0);
131 SPECIFIC_GROUP(i2c1);
132 SPECIFIC_GROUP(i2c0);
133 SPECIFIC_GROUP(nand);
134 SPECIFIC_GROUP(sata_led);
135 SPECIFIC_GROUP(i2s);
136 SPECIFIC_GROUP(hda);
137 
138 static const char * const gpio_groups[] = {
139 	"sdio",
140 	"can1", "can0",
141 	"pwm3", "pwm2", "pwm1", "pwm0",
142 	"i2c1", "i2c0",
143 	"nand",
144 	"sata_led",
145 	"i2s",
146 	"hda",
147 };
148 
149 static const struct loongson2_pmx_func loongson2_pmx_functions[] = {
150 	FUNCTION(gpio),
151 	FUNCTION(sdio),
152 	FUNCTION(can1),
153 	FUNCTION(can0),
154 	FUNCTION(pwm3),
155 	FUNCTION(pwm2),
156 	FUNCTION(pwm1),
157 	FUNCTION(pwm0),
158 	FUNCTION(i2c1),
159 	FUNCTION(i2c0),
160 	FUNCTION(nand),
161 	FUNCTION(sata_led),
162 	FUNCTION(i2s),
163 	FUNCTION(hda),
164 };
165 
166 static int loongson2_get_groups_count(struct pinctrl_dev *pcdev)
167 {
168 	return ARRAY_SIZE(loongson2_pmx_groups);
169 }
170 
171 static const char *loongson2_get_group_name(struct pinctrl_dev *pcdev,
172 					unsigned int selector)
173 {
174 	return loongson2_pmx_groups[selector].grp.name;
175 }
176 
177 static int loongson2_get_group_pins(struct pinctrl_dev *pcdev, unsigned int selector,
178 			const unsigned int **pins, unsigned int *num_pins)
179 {
180 	*pins = loongson2_pmx_groups[selector].grp.pins;
181 	*num_pins = loongson2_pmx_groups[selector].grp.npins;
182 
183 	return 0;
184 }
185 
186 static void loongson2_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s,
187 			       unsigned int offset)
188 {
189 	seq_printf(s, " %s", dev_name(pcdev->dev));
190 }
191 
192 static const struct pinctrl_ops loongson2_pctrl_ops = {
193 	.get_groups_count	= loongson2_get_groups_count,
194 	.get_group_name		= loongson2_get_group_name,
195 	.get_group_pins		= loongson2_get_group_pins,
196 	.dt_node_to_map		= pinconf_generic_dt_node_to_map_all,
197 	.dt_free_map		= pinctrl_utils_free_map,
198 	.pin_dbg_show		= loongson2_pin_dbg_show,
199 };
200 
201 static int loongson2_pmx_set_mux(struct pinctrl_dev *pcdev, unsigned int func_num,
202 			      unsigned int group_num)
203 {
204 	struct loongson2_pinctrl *pctrl = pinctrl_dev_get_drvdata(pcdev);
205 	void __iomem *reg = pctrl->reg_base +
206 				loongson2_pmx_groups[group_num].reg;
207 	unsigned int mux_bit = loongson2_pmx_groups[group_num].bit;
208 	unsigned int val;
209 	unsigned long flags;
210 
211 	spin_lock_irqsave(&pctrl->lock, flags);
212 	val = readl(reg);
213 	if (func_num == 0)
214 		val &= ~BIT(mux_bit);
215 	else
216 		val |= BIT(mux_bit);
217 	writel(val, reg);
218 	spin_unlock_irqrestore(&pctrl->lock, flags);
219 
220 	return 0;
221 }
222 
223 static int loongson2_pmx_get_funcs_count(struct pinctrl_dev *pcdev)
224 {
225 	return ARRAY_SIZE(loongson2_pmx_functions);
226 }
227 
228 static const char *loongson2_pmx_get_func_name(struct pinctrl_dev *pcdev,
229 				    unsigned int selector)
230 {
231 	return loongson2_pmx_functions[selector].name;
232 }
233 
234 static int loongson2_pmx_get_groups(struct pinctrl_dev *pcdev,
235 			 unsigned int selector,
236 			 const char * const **groups,
237 			 unsigned int * const num_groups)
238 {
239 	*groups = loongson2_pmx_functions[selector].groups;
240 	*num_groups = loongson2_pmx_functions[selector].num_groups;
241 
242 	return 0;
243 }
244 
245 static const struct pinmux_ops loongson2_pmx_ops = {
246 	.set_mux = loongson2_pmx_set_mux,
247 	.get_functions_count = loongson2_pmx_get_funcs_count,
248 	.get_function_name = loongson2_pmx_get_func_name,
249 	.get_function_groups = loongson2_pmx_get_groups,
250 };
251 
252 static int loongson2_pinctrl_probe(struct platform_device *pdev)
253 {
254 	struct device *dev = &pdev->dev;
255 	struct loongson2_pinctrl *pctrl;
256 
257 	pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
258 	if (!pctrl)
259 		return -ENOMEM;
260 
261 	pctrl->reg_base = devm_platform_ioremap_resource(pdev, 0);
262 	if (IS_ERR(pctrl->reg_base))
263 		return PTR_ERR(pctrl->reg_base);
264 
265 	spin_lock_init(&pctrl->lock);
266 
267 	pctrl->dev = dev;
268 	pctrl->desc.name	= "pinctrl-loongson2";
269 	pctrl->desc.owner	= THIS_MODULE;
270 	pctrl->desc.pctlops	= &loongson2_pctrl_ops;
271 	pctrl->desc.pmxops	= &loongson2_pmx_ops;
272 	pctrl->desc.pins	= loongson2_pctrl_pins;
273 	pctrl->desc.npins	= ARRAY_SIZE(loongson2_pctrl_pins);
274 
275 	pctrl->pcdev = devm_pinctrl_register(pctrl->dev, &pctrl->desc, pctrl);
276 	if (IS_ERR(pctrl->pcdev))
277 		return dev_err_probe(pctrl->dev, PTR_ERR(pctrl->pcdev),
278 				     "can't register pinctrl device");
279 
280 	return 0;
281 }
282 
283 static const struct of_device_id loongson2_pinctrl_dt_match[] = {
284 	{
285 		.compatible = "loongson,ls2k-pinctrl",
286 	},
287 	{ }
288 };
289 MODULE_DEVICE_TABLE(of, loongson2_pinctrl_dt_match);
290 
291 static struct platform_driver loongson2_pinctrl_driver = {
292 	.probe		= loongson2_pinctrl_probe,
293 	.driver = {
294 		.name	= "loongson2-pinctrl",
295 		.of_match_table = loongson2_pinctrl_dt_match,
296 	},
297 };
298 
299 static int __init loongson2_pinctrl_init(void)
300 {
301 	return platform_driver_register(&loongson2_pinctrl_driver);
302 }
303 arch_initcall(loongson2_pinctrl_init);
304 
305 static void __exit loongson2_pinctrl_exit(void)
306 {
307 	platform_driver_unregister(&loongson2_pinctrl_driver);
308 }
309 module_exit(loongson2_pinctrl_exit);
310 
311 MODULE_DESCRIPTION("Loongson2 Pinctrl driver");
312 MODULE_LICENSE("GPL");
313