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