1 // SPDX-License-Identifier: GPL-2.0+
3 * Author: zhanghongchen <zhanghongchen@loongson.cn>
4 * Yinbo Zhu <zhuyinbo@loongson.cn>
5 * Copyright (C) 2022-2023 Loongson Technology Corporation Limited
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>
17 #include <linux/seq_file.h>
20 #include "pinctrl-utils.h"
22 #define PMX_GROUP(name, offset, bitv) \
24 .grp = PINCTRL_PINGROUP((#name), (name ## _pins), \
25 ARRAY_SIZE((name ## _pins))), \
30 #define SPECIFIC_GROUP(group) \
31 static const char * const group##_groups[] = { \
35 #define FUNCTION(fn) \
38 .groups = fn ## _groups, \
39 .num_groups = ARRAY_SIZE(fn ## _groups), \
42 struct loongson2_pinctrl
{
44 struct pinctrl_dev
*pcdev
;
45 struct pinctrl_desc desc
;
46 struct device_node
*of_node
;
48 void __iomem
*reg_base
;
51 struct loongson2_pmx_group
{
57 struct loongson2_pmx_func
{
59 const char * const *groups
;
60 unsigned int num_groups
;
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),
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,
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};
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),
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
);
138 static const char * const gpio_groups
[] = {
141 "pwm3", "pwm2", "pwm1", "pwm0",
149 static const struct loongson2_pmx_func loongson2_pmx_functions
[] = {
166 static int loongson2_get_groups_count(struct pinctrl_dev
*pcdev
)
168 return ARRAY_SIZE(loongson2_pmx_groups
);
171 static const char *loongson2_get_group_name(struct pinctrl_dev
*pcdev
,
172 unsigned int selector
)
174 return loongson2_pmx_groups
[selector
].grp
.name
;
177 static int loongson2_get_group_pins(struct pinctrl_dev
*pcdev
, unsigned int selector
,
178 const unsigned int **pins
, unsigned int *num_pins
)
180 *pins
= loongson2_pmx_groups
[selector
].grp
.pins
;
181 *num_pins
= loongson2_pmx_groups
[selector
].grp
.npins
;
186 static void loongson2_pin_dbg_show(struct pinctrl_dev
*pcdev
, struct seq_file
*s
,
189 seq_printf(s
, " %s", dev_name(pcdev
->dev
));
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
,
201 static int loongson2_pmx_set_mux(struct pinctrl_dev
*pcdev
, unsigned int func_num
,
202 unsigned int group_num
)
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
;
211 spin_lock_irqsave(&pctrl
->lock
, flags
);
214 val
&= ~BIT(mux_bit
);
218 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
223 static int loongson2_pmx_get_funcs_count(struct pinctrl_dev
*pcdev
)
225 return ARRAY_SIZE(loongson2_pmx_functions
);
228 static const char *loongson2_pmx_get_func_name(struct pinctrl_dev
*pcdev
,
229 unsigned int selector
)
231 return loongson2_pmx_functions
[selector
].name
;
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
)
239 *groups
= loongson2_pmx_functions
[selector
].groups
;
240 *num_groups
= loongson2_pmx_functions
[selector
].num_groups
;
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
,
252 static int loongson2_pinctrl_probe(struct platform_device
*pdev
)
254 struct device
*dev
= &pdev
->dev
;
255 struct loongson2_pinctrl
*pctrl
;
257 pctrl
= devm_kzalloc(dev
, sizeof(*pctrl
), GFP_KERNEL
);
261 pctrl
->reg_base
= devm_platform_ioremap_resource(pdev
, 0);
262 if (IS_ERR(pctrl
->reg_base
))
263 return PTR_ERR(pctrl
->reg_base
);
265 spin_lock_init(&pctrl
->lock
);
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
);
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");
283 static const struct of_device_id loongson2_pinctrl_dt_match
[] = {
285 .compatible
= "loongson,ls2k-pinctrl",
289 MODULE_DEVICE_TABLE(of
, loongson2_pinctrl_dt_match
);
291 static struct platform_driver loongson2_pinctrl_driver
= {
292 .probe
= loongson2_pinctrl_probe
,
294 .name
= "loongson2-pinctrl",
295 .of_match_table
= loongson2_pinctrl_dt_match
,
299 static int __init
loongson2_pinctrl_init(void)
301 return platform_driver_register(&loongson2_pinctrl_driver
);
303 arch_initcall(loongson2_pinctrl_init
);
305 static void __exit
loongson2_pinctrl_exit(void)
307 platform_driver_unregister(&loongson2_pinctrl_driver
);
309 module_exit(loongson2_pinctrl_exit
);
311 MODULE_DESCRIPTION("Loongson2 Pinctrl driver");
312 MODULE_LICENSE("GPL");