blob: d09a726beece5d7d99ba2352df9903fe8bae688c [file] [log] [blame]
Zhiyong Tao80525092018-09-08 19:07:33 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * MediaTek Pinctrl Paris Driver, which implement the vendor per-pin
4 * bindings for MediaTek SoC.
5 *
6 * Copyright (C) 2018 MediaTek Inc.
7 * Author: Sean Wang <sean.wang@mediatek.com>
8 * Zhiyong Tao <zhiyong.tao@mediatek.com>
9 * Hongzhou.Yang <hongzhou.yang@mediatek.com>
10 */
11
Linus Walleij22d7fe42018-09-18 15:03:13 -070012#include <linux/gpio/driver.h>
Zhiyong Tao80525092018-09-08 19:07:33 +080013#include <dt-bindings/pinctrl/mt65xx.h>
14#include "pinctrl-paris.h"
15
16#define PINCTRL_PINCTRL_DEV KBUILD_MODNAME
17
18/* Custom pinconf parameters */
19#define MTK_PIN_CONFIG_TDSEL (PIN_CONFIG_END + 1)
20#define MTK_PIN_CONFIG_RDSEL (PIN_CONFIG_END + 2)
21#define MTK_PIN_CONFIG_PU_ADV (PIN_CONFIG_END + 3)
22#define MTK_PIN_CONFIG_PD_ADV (PIN_CONFIG_END + 4)
Zhiyong Tao5e73de32019-04-01 11:35:35 +080023#define MTK_PIN_CONFIG_DRV_ADV (PIN_CONFIG_END + 5)
Zhiyong Tao80525092018-09-08 19:07:33 +080024
25static const struct pinconf_generic_params mtk_custom_bindings[] = {
26 {"mediatek,tdsel", MTK_PIN_CONFIG_TDSEL, 0},
27 {"mediatek,rdsel", MTK_PIN_CONFIG_RDSEL, 0},
28 {"mediatek,pull-up-adv", MTK_PIN_CONFIG_PU_ADV, 1},
29 {"mediatek,pull-down-adv", MTK_PIN_CONFIG_PD_ADV, 1},
Zhiyong Tao5e73de32019-04-01 11:35:35 +080030 {"mediatek,drive-strength-adv", MTK_PIN_CONFIG_DRV_ADV, 2},
Zhiyong Tao80525092018-09-08 19:07:33 +080031};
32
33#ifdef CONFIG_DEBUG_FS
34static const struct pin_config_item mtk_conf_items[] = {
35 PCONFDUMP(MTK_PIN_CONFIG_TDSEL, "tdsel", NULL, true),
36 PCONFDUMP(MTK_PIN_CONFIG_RDSEL, "rdsel", NULL, true),
37 PCONFDUMP(MTK_PIN_CONFIG_PU_ADV, "pu-adv", NULL, true),
38 PCONFDUMP(MTK_PIN_CONFIG_PD_ADV, "pd-adv", NULL, true),
Zhiyong Tao5e73de32019-04-01 11:35:35 +080039 PCONFDUMP(MTK_PIN_CONFIG_DRV_ADV, "drive-strength-adv", NULL, true),
Zhiyong Tao80525092018-09-08 19:07:33 +080040};
41#endif
42
43static const char * const mtk_gpio_functions[] = {
44 "func0", "func1", "func2", "func3",
45 "func4", "func5", "func6", "func7",
46 "func8", "func9", "func10", "func11",
47 "func12", "func13", "func14", "func15",
48};
49
50static int mtk_pinmux_gpio_request_enable(struct pinctrl_dev *pctldev,
51 struct pinctrl_gpio_range *range,
52 unsigned int pin)
53{
54 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
55 const struct mtk_pin_desc *desc;
56
57 desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
58
59 return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE,
60 hw->soc->gpio_m);
61}
62
63static int mtk_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
64 struct pinctrl_gpio_range *range,
65 unsigned int pin, bool input)
66{
67 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
68 const struct mtk_pin_desc *desc;
69
70 desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
71
72 /* hardware would take 0 as input direction */
73 return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, !input);
74}
75
76static int mtk_pinconf_get(struct pinctrl_dev *pctldev,
77 unsigned int pin, unsigned long *config)
78{
79 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
80 u32 param = pinconf_to_config_param(*config);
Light Hsieh1bea6af2020-01-22 14:53:12 +080081 int err, reg, ret = 1;
Zhiyong Tao80525092018-09-08 19:07:33 +080082 const struct mtk_pin_desc *desc;
83
Light Hsieh3599cc522020-01-22 14:53:11 +080084 if (pin >= hw->soc->npins) {
85 err = -EINVAL;
86 goto out;
87 }
Zhiyong Tao80525092018-09-08 19:07:33 +080088 desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
89
90 switch (param) {
91 case PIN_CONFIG_BIAS_DISABLE:
Light Hsieh3599cc522020-01-22 14:53:11 +080092 if (hw->soc->bias_disable_get)
Zhiyong Tao80525092018-09-08 19:07:33 +080093 err = hw->soc->bias_disable_get(hw, desc, &ret);
Light Hsieh3599cc522020-01-22 14:53:11 +080094 else
95 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +080096 break;
97 case PIN_CONFIG_BIAS_PULL_UP:
Light Hsieh3599cc522020-01-22 14:53:11 +080098 if (hw->soc->bias_get)
Zhiyong Tao80525092018-09-08 19:07:33 +080099 err = hw->soc->bias_get(hw, desc, 1, &ret);
Light Hsieh3599cc522020-01-22 14:53:11 +0800100 else
101 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800102 break;
103 case PIN_CONFIG_BIAS_PULL_DOWN:
Light Hsieh3599cc522020-01-22 14:53:11 +0800104 if (hw->soc->bias_get)
Zhiyong Tao80525092018-09-08 19:07:33 +0800105 err = hw->soc->bias_get(hw, desc, 0, &ret);
Light Hsieh3599cc522020-01-22 14:53:11 +0800106 else
107 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800108 break;
109 case PIN_CONFIG_SLEW_RATE:
Light Hsieh1bea6af2020-01-22 14:53:12 +0800110 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_SR, &ret);
Zhiyong Tao80525092018-09-08 19:07:33 +0800111 break;
112 case PIN_CONFIG_INPUT_ENABLE:
113 case PIN_CONFIG_OUTPUT_ENABLE:
Light Hsieh1bea6af2020-01-22 14:53:12 +0800114 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &ret);
Zhiyong Tao80525092018-09-08 19:07:33 +0800115 if (err)
Light Hsieh3599cc522020-01-22 14:53:11 +0800116 goto out;
117 /* CONFIG Current direction return value
118 * ------------- ----------------- ----------------------
119 * OUTPUT_ENABLE output 1 (= HW value)
120 * input 0 (= HW value)
121 * INPUT_ENABLE output 0 (= reverse HW value)
122 * input 1 (= reverse HW value)
123 */
124 if (param == PIN_CONFIG_INPUT_ENABLE)
Light Hsieh1bea6af2020-01-22 14:53:12 +0800125 ret = !ret;
Zhiyong Tao80525092018-09-08 19:07:33 +0800126
127 break;
128 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
Light Hsieh1bea6af2020-01-22 14:53:12 +0800129 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &ret);
Zhiyong Tao80525092018-09-08 19:07:33 +0800130 if (err)
Light Hsieh1bea6af2020-01-22 14:53:12 +0800131 goto out;
132 /* return error when in output mode
133 * because schmitt trigger only work in input mode
134 */
135 if (ret) {
136 err = -EINVAL;
137 goto out;
138 }
Zhiyong Tao80525092018-09-08 19:07:33 +0800139
Light Hsieh1bea6af2020-01-22 14:53:12 +0800140 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_SMT, &ret);
Zhiyong Tao80525092018-09-08 19:07:33 +0800141
142 break;
143 case PIN_CONFIG_DRIVE_STRENGTH:
Light Hsieh3599cc522020-01-22 14:53:11 +0800144 if (hw->soc->drive_get)
Zhiyong Tao80525092018-09-08 19:07:33 +0800145 err = hw->soc->drive_get(hw, desc, &ret);
Light Hsieh3599cc522020-01-22 14:53:11 +0800146 else
Zhiyong Tao80525092018-09-08 19:07:33 +0800147 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800148 break;
149 case MTK_PIN_CONFIG_TDSEL:
150 case MTK_PIN_CONFIG_RDSEL:
151 reg = (param == MTK_PIN_CONFIG_TDSEL) ?
152 PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
Light Hsieh1bea6af2020-01-22 14:53:12 +0800153 err = mtk_hw_get_value(hw, desc, reg, &ret);
Zhiyong Tao80525092018-09-08 19:07:33 +0800154 break;
155 case MTK_PIN_CONFIG_PU_ADV:
156 case MTK_PIN_CONFIG_PD_ADV:
157 if (hw->soc->adv_pull_get) {
158 bool pullup;
159
160 pullup = param == MTK_PIN_CONFIG_PU_ADV;
161 err = hw->soc->adv_pull_get(hw, desc, pullup, &ret);
Light Hsieh3599cc522020-01-22 14:53:11 +0800162 } else
163 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800164 break;
Zhiyong Tao5e73de32019-04-01 11:35:35 +0800165 case MTK_PIN_CONFIG_DRV_ADV:
Light Hsieh3599cc522020-01-22 14:53:11 +0800166 if (hw->soc->adv_drive_get)
Zhiyong Tao5e73de32019-04-01 11:35:35 +0800167 err = hw->soc->adv_drive_get(hw, desc, &ret);
Light Hsieh3599cc522020-01-22 14:53:11 +0800168 else
169 err = -ENOTSUPP;
Zhiyong Tao5e73de32019-04-01 11:35:35 +0800170 break;
Zhiyong Tao80525092018-09-08 19:07:33 +0800171 default:
Light Hsieh3599cc522020-01-22 14:53:11 +0800172 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800173 }
174
Light Hsieh3599cc522020-01-22 14:53:11 +0800175out:
176 if (!err)
177 *config = pinconf_to_config_packed(param, ret);
Zhiyong Tao80525092018-09-08 19:07:33 +0800178
Light Hsieh3599cc522020-01-22 14:53:11 +0800179 return err;
Zhiyong Tao80525092018-09-08 19:07:33 +0800180}
181
182static int mtk_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
183 enum pin_config_param param,
184 enum pin_config_param arg)
185{
186 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
187 const struct mtk_pin_desc *desc;
188 int err = 0;
189 u32 reg;
190
Light Hsieh3de7dee2020-01-22 14:53:09 +0800191 if (pin >= hw->soc->npins) {
192 err = -EINVAL;
193 goto err;
194 }
Zhiyong Tao80525092018-09-08 19:07:33 +0800195 desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
196
197 switch ((u32)param) {
198 case PIN_CONFIG_BIAS_DISABLE:
Light Hsieh3599cc522020-01-22 14:53:11 +0800199 if (hw->soc->bias_disable_set)
Zhiyong Tao80525092018-09-08 19:07:33 +0800200 err = hw->soc->bias_disable_set(hw, desc);
Light Hsieh3599cc522020-01-22 14:53:11 +0800201 else
202 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800203 break;
204 case PIN_CONFIG_BIAS_PULL_UP:
Light Hsieh3599cc522020-01-22 14:53:11 +0800205 if (hw->soc->bias_set)
Zhiyong Tao80525092018-09-08 19:07:33 +0800206 err = hw->soc->bias_set(hw, desc, 1);
Light Hsieh3599cc522020-01-22 14:53:11 +0800207 else
208 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800209 break;
210 case PIN_CONFIG_BIAS_PULL_DOWN:
Light Hsieh3599cc522020-01-22 14:53:11 +0800211 if (hw->soc->bias_set)
Zhiyong Tao80525092018-09-08 19:07:33 +0800212 err = hw->soc->bias_set(hw, desc, 0);
Light Hsieh3599cc522020-01-22 14:53:11 +0800213 else
214 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800215 break;
216 case PIN_CONFIG_OUTPUT_ENABLE:
217 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT,
218 MTK_DISABLE);
Light Hsieh3599cc522020-01-22 14:53:11 +0800219 /* Keep set direction to consider the case that a GPIO pin
220 * does not have SMT control
221 */
222 if (err != -ENOTSUPP)
Zhiyong Tao80525092018-09-08 19:07:33 +0800223 goto err;
224
225 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
226 MTK_OUTPUT);
Zhiyong Tao80525092018-09-08 19:07:33 +0800227 break;
228 case PIN_CONFIG_INPUT_ENABLE:
Light Hsieh3599cc522020-01-22 14:53:11 +0800229 /* regard all non-zero value as enable */
230 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_IES, !!arg);
231 if (err)
232 goto err;
Zhiyong Tao80525092018-09-08 19:07:33 +0800233
234 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
235 MTK_INPUT);
Zhiyong Tao80525092018-09-08 19:07:33 +0800236 break;
237 case PIN_CONFIG_SLEW_RATE:
Light Hsieh3599cc522020-01-22 14:53:11 +0800238 /* regard all non-zero value as enable */
239 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SR, !!arg);
Zhiyong Tao80525092018-09-08 19:07:33 +0800240 break;
241 case PIN_CONFIG_OUTPUT:
242 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
243 MTK_OUTPUT);
244 if (err)
245 goto err;
246
247 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DO,
248 arg);
Zhiyong Tao80525092018-09-08 19:07:33 +0800249 break;
Light Hsieh3599cc522020-01-22 14:53:11 +0800250 case PIN_CONFIG_INPUT_SCHMITT:
Zhiyong Tao80525092018-09-08 19:07:33 +0800251 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
252 /* arg = 1: Input mode & SMT enable ;
253 * arg = 0: Output mode & SMT disable
254 */
Light Hsieh3599cc522020-01-22 14:53:11 +0800255 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, !arg);
Zhiyong Tao80525092018-09-08 19:07:33 +0800256 if (err)
257 goto err;
258
Light Hsieh3599cc522020-01-22 14:53:11 +0800259 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, !!arg);
Zhiyong Tao80525092018-09-08 19:07:33 +0800260 break;
261 case PIN_CONFIG_DRIVE_STRENGTH:
Light Hsieh3599cc522020-01-22 14:53:11 +0800262 if (hw->soc->drive_set)
Zhiyong Tao80525092018-09-08 19:07:33 +0800263 err = hw->soc->drive_set(hw, desc, arg);
Light Hsieh3599cc522020-01-22 14:53:11 +0800264 else
265 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800266 break;
267 case MTK_PIN_CONFIG_TDSEL:
268 case MTK_PIN_CONFIG_RDSEL:
269 reg = (param == MTK_PIN_CONFIG_TDSEL) ?
270 PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
Zhiyong Tao80525092018-09-08 19:07:33 +0800271 err = mtk_hw_set_value(hw, desc, reg, arg);
Zhiyong Tao80525092018-09-08 19:07:33 +0800272 break;
273 case MTK_PIN_CONFIG_PU_ADV:
274 case MTK_PIN_CONFIG_PD_ADV:
275 if (hw->soc->adv_pull_set) {
276 bool pullup;
277
278 pullup = param == MTK_PIN_CONFIG_PU_ADV;
279 err = hw->soc->adv_pull_set(hw, desc, pullup,
280 arg);
Light Hsieh3599cc522020-01-22 14:53:11 +0800281 } else
282 err = -ENOTSUPP;
Zhiyong Tao80525092018-09-08 19:07:33 +0800283 break;
Zhiyong Tao5e73de32019-04-01 11:35:35 +0800284 case MTK_PIN_CONFIG_DRV_ADV:
Light Hsieh3599cc522020-01-22 14:53:11 +0800285 if (hw->soc->adv_drive_set)
Zhiyong Tao5e73de32019-04-01 11:35:35 +0800286 err = hw->soc->adv_drive_set(hw, desc, arg);
Light Hsieh3599cc522020-01-22 14:53:11 +0800287 else
288 err = -ENOTSUPP;
Zhiyong Tao5e73de32019-04-01 11:35:35 +0800289 break;
Zhiyong Tao80525092018-09-08 19:07:33 +0800290 default:
291 err = -ENOTSUPP;
292 }
293
294err:
295 return err;
296}
297
298static struct mtk_pinctrl_group *
299mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *hw, u32 pin)
300{
301 int i;
302
303 for (i = 0; i < hw->soc->ngrps; i++) {
304 struct mtk_pinctrl_group *grp = hw->groups + i;
305
306 if (grp->pin == pin)
307 return grp;
308 }
309
310 return NULL;
311}
312
313static const struct mtk_func_desc *
314mtk_pctrl_find_function_by_pin(struct mtk_pinctrl *hw, u32 pin_num, u32 fnum)
315{
316 const struct mtk_pin_desc *pin = hw->soc->pins + pin_num;
317 const struct mtk_func_desc *func = pin->funcs;
318
319 while (func && func->name) {
320 if (func->muxval == fnum)
321 return func;
322 func++;
323 }
324
325 return NULL;
326}
327
328static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *hw, u32 pin_num,
329 u32 fnum)
330{
331 int i;
332
333 for (i = 0; i < hw->soc->npins; i++) {
334 const struct mtk_pin_desc *pin = hw->soc->pins + i;
335
336 if (pin->number == pin_num) {
337 const struct mtk_func_desc *func = pin->funcs;
338
339 while (func && func->name) {
340 if (func->muxval == fnum)
341 return true;
342 func++;
343 }
344
345 break;
346 }
347 }
348
349 return false;
350}
351
352static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
353 u32 pin, u32 fnum,
354 struct mtk_pinctrl_group *grp,
355 struct pinctrl_map **map,
356 unsigned *reserved_maps,
357 unsigned *num_maps)
358{
359 bool ret;
360
361 if (*num_maps == *reserved_maps)
362 return -ENOSPC;
363
364 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
365 (*map)[*num_maps].data.mux.group = grp->name;
366
367 ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
368 if (!ret) {
369 dev_err(pctl->dev, "invalid function %d on pin %d .\n",
370 fnum, pin);
371 return -EINVAL;
372 }
373
374 (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
375 (*num_maps)++;
376
377 return 0;
378}
379
380static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
381 struct device_node *node,
382 struct pinctrl_map **map,
383 unsigned *reserved_maps,
384 unsigned *num_maps)
385{
386 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
387 int num_pins, num_funcs, maps_per_pin, i, err;
388 struct mtk_pinctrl_group *grp;
389 unsigned int num_configs;
390 bool has_config = false;
391 unsigned long *configs;
392 u32 pinfunc, pin, func;
393 struct property *pins;
394 unsigned reserve = 0;
395
396 pins = of_find_property(node, "pinmux", NULL);
397 if (!pins) {
Rob Herring9ede2a72018-11-16 16:05:40 -0600398 dev_err(hw->dev, "missing pins property in node %pOFn .\n",
399 node);
Zhiyong Tao80525092018-09-08 19:07:33 +0800400 return -EINVAL;
401 }
402
403 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
404 &num_configs);
405 if (err)
406 return err;
407
408 if (num_configs)
409 has_config = true;
410
411 num_pins = pins->length / sizeof(u32);
412 num_funcs = num_pins;
413 maps_per_pin = 0;
414 if (num_funcs)
415 maps_per_pin++;
416 if (has_config && num_pins >= 1)
417 maps_per_pin++;
418
419 if (!num_pins || !maps_per_pin) {
420 err = -EINVAL;
421 goto exit;
422 }
423
424 reserve = num_pins * maps_per_pin;
425
426 err = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps,
427 reserve);
428 if (err < 0)
429 goto exit;
430
431 for (i = 0; i < num_pins; i++) {
432 err = of_property_read_u32_index(node, "pinmux", i, &pinfunc);
433 if (err)
434 goto exit;
435
436 pin = MTK_GET_PIN_NO(pinfunc);
437 func = MTK_GET_PIN_FUNC(pinfunc);
438
439 if (pin >= hw->soc->npins ||
440 func >= ARRAY_SIZE(mtk_gpio_functions)) {
441 dev_err(hw->dev, "invalid pins value.\n");
442 err = -EINVAL;
443 goto exit;
444 }
445
446 grp = mtk_pctrl_find_group_by_pin(hw, pin);
447 if (!grp) {
448 dev_err(hw->dev, "unable to match pin %d to group\n",
449 pin);
450 err = -EINVAL;
451 goto exit;
452 }
453
454 err = mtk_pctrl_dt_node_to_map_func(hw, pin, func, grp, map,
455 reserved_maps, num_maps);
456 if (err < 0)
457 goto exit;
458
459 if (has_config) {
460 err = pinctrl_utils_add_map_configs(pctldev, map,
461 reserved_maps,
462 num_maps,
463 grp->name,
464 configs,
465 num_configs,
466 PIN_MAP_TYPE_CONFIGS_GROUP);
467 if (err < 0)
468 goto exit;
469 }
470 }
471
472 err = 0;
473
474exit:
475 kfree(configs);
476 return err;
477}
478
479static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
480 struct device_node *np_config,
481 struct pinctrl_map **map,
482 unsigned *num_maps)
483{
484 struct device_node *np;
485 unsigned reserved_maps;
486 int ret;
487
488 *map = NULL;
489 *num_maps = 0;
490 reserved_maps = 0;
491
492 for_each_child_of_node(np_config, np) {
493 ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
494 &reserved_maps,
495 num_maps);
496 if (ret < 0) {
497 pinctrl_utils_free_map(pctldev, *map, *num_maps);
498 of_node_put(np);
499 return ret;
500 }
501 }
502
503 return 0;
504}
505
506static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
507{
508 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
509
510 return hw->soc->ngrps;
511}
512
513static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
514 unsigned group)
515{
516 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
517
518 return hw->groups[group].name;
519}
520
521static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
522 unsigned group, const unsigned **pins,
523 unsigned *num_pins)
524{
525 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
526
527 *pins = (unsigned *)&hw->groups[group].pin;
528 *num_pins = 1;
529
530 return 0;
531}
532
533static const struct pinctrl_ops mtk_pctlops = {
534 .dt_node_to_map = mtk_pctrl_dt_node_to_map,
535 .dt_free_map = pinctrl_utils_free_map,
536 .get_groups_count = mtk_pctrl_get_groups_count,
537 .get_group_name = mtk_pctrl_get_group_name,
538 .get_group_pins = mtk_pctrl_get_group_pins,
539};
540
541static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
542{
543 return ARRAY_SIZE(mtk_gpio_functions);
544}
545
546static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
547 unsigned selector)
548{
549 return mtk_gpio_functions[selector];
550}
551
552static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
553 unsigned function,
554 const char * const **groups,
555 unsigned * const num_groups)
556{
557 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
558
559 *groups = hw->grp_names;
560 *num_groups = hw->soc->ngrps;
561
562 return 0;
563}
564
565static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
566 unsigned function,
567 unsigned group)
568{
569 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
570 struct mtk_pinctrl_group *grp = hw->groups + group;
571 const struct mtk_func_desc *desc_func;
572 const struct mtk_pin_desc *desc;
573 bool ret;
574
575 ret = mtk_pctrl_is_function_valid(hw, grp->pin, function);
576 if (!ret) {
577 dev_err(hw->dev, "invalid function %d on group %d .\n",
578 function, group);
579 return -EINVAL;
580 }
581
582 desc_func = mtk_pctrl_find_function_by_pin(hw, grp->pin, function);
583 if (!desc_func)
584 return -EINVAL;
585
586 desc = (const struct mtk_pin_desc *)&hw->soc->pins[grp->pin];
587 mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE, desc_func->muxval);
588
589 return 0;
590}
591
592static const struct pinmux_ops mtk_pmxops = {
593 .get_functions_count = mtk_pmx_get_funcs_cnt,
594 .get_function_name = mtk_pmx_get_func_name,
595 .get_function_groups = mtk_pmx_get_func_groups,
596 .set_mux = mtk_pmx_set_mux,
597 .gpio_set_direction = mtk_pinmux_gpio_set_direction,
598 .gpio_request_enable = mtk_pinmux_gpio_request_enable,
599};
600
601static int mtk_pconf_group_get(struct pinctrl_dev *pctldev, unsigned group,
602 unsigned long *config)
603{
604 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
605
606 *config = hw->groups[group].config;
607
608 return 0;
609}
610
611static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
612 unsigned long *configs, unsigned num_configs)
613{
614 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
615 struct mtk_pinctrl_group *grp = &hw->groups[group];
616 int i, ret;
617
618 for (i = 0; i < num_configs; i++) {
619 ret = mtk_pinconf_set(pctldev, grp->pin,
620 pinconf_to_config_param(configs[i]),
621 pinconf_to_config_argument(configs[i]));
622 if (ret < 0)
623 return ret;
624
625 grp->config = configs[i];
626 }
627
628 return 0;
629}
630
631static const struct pinconf_ops mtk_confops = {
632 .pin_config_get = mtk_pinconf_get,
633 .pin_config_group_get = mtk_pconf_group_get,
634 .pin_config_group_set = mtk_pconf_group_set,
635};
636
637static struct pinctrl_desc mtk_desc = {
638 .name = PINCTRL_PINCTRL_DEV,
639 .pctlops = &mtk_pctlops,
640 .pmxops = &mtk_pmxops,
641 .confops = &mtk_confops,
642 .owner = THIS_MODULE,
643};
644
645static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
646{
647 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
648 const struct mtk_pin_desc *desc;
649 int value, err;
650
Light Hsieh3de7dee2020-01-22 14:53:09 +0800651 if (gpio > hw->soc->npins)
652 return -EINVAL;
653
Zhiyong Tao80525092018-09-08 19:07:33 +0800654 desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio];
655
656 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &value);
657 if (err)
658 return err;
659
660 return !value;
661}
662
663static int mtk_gpio_get(struct gpio_chip *chip, unsigned int gpio)
664{
665 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
666 const struct mtk_pin_desc *desc;
667 int value, err;
668
Light Hsieh3de7dee2020-01-22 14:53:09 +0800669 if (gpio > hw->soc->npins)
670 return -EINVAL;
671
Zhiyong Tao80525092018-09-08 19:07:33 +0800672 desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio];
673
674 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value);
675 if (err)
676 return err;
677
678 return !!value;
679}
680
681static void mtk_gpio_set(struct gpio_chip *chip, unsigned int gpio, int value)
682{
683 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
684 const struct mtk_pin_desc *desc;
685
Light Hsieh3de7dee2020-01-22 14:53:09 +0800686 if (gpio > hw->soc->npins)
687 return;
688
Zhiyong Tao80525092018-09-08 19:07:33 +0800689 desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio];
690
691 mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DO, !!value);
692}
693
694static int mtk_gpio_direction_input(struct gpio_chip *chip, unsigned int gpio)
695{
Light Hsieh3de7dee2020-01-22 14:53:09 +0800696 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
697
698 if (gpio > hw->soc->npins)
699 return -EINVAL;
700
Zhiyong Tao80525092018-09-08 19:07:33 +0800701 return pinctrl_gpio_direction_input(chip->base + gpio);
702}
703
704static int mtk_gpio_direction_output(struct gpio_chip *chip, unsigned int gpio,
705 int value)
706{
Light Hsieh3de7dee2020-01-22 14:53:09 +0800707 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
708
709 if (gpio > hw->soc->npins)
710 return -EINVAL;
711
Zhiyong Tao80525092018-09-08 19:07:33 +0800712 mtk_gpio_set(chip, gpio, value);
713
714 return pinctrl_gpio_direction_output(chip->base + gpio);
715}
716
Sean Wang65618592018-09-08 19:07:38 +0800717static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
718{
719 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
720 const struct mtk_pin_desc *desc;
721
722 if (!hw->eint)
723 return -ENOTSUPP;
724
725 desc = (const struct mtk_pin_desc *)&hw->soc->pins[offset];
726
727 if (desc->eint.eint_n == EINT_NA)
728 return -ENOTSUPP;
729
730 return mtk_eint_find_irq(hw->eint, desc->eint.eint_n);
731}
732
Zhiyong Tao80525092018-09-08 19:07:33 +0800733static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
734 unsigned long config)
735{
736 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
737 const struct mtk_pin_desc *desc;
738 u32 debounce;
739
740 desc = (const struct mtk_pin_desc *)&hw->soc->pins[offset];
741
742 if (!hw->eint ||
743 pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE ||
744 desc->eint.eint_n == EINT_NA)
745 return -ENOTSUPP;
746
747 debounce = pinconf_to_config_argument(config);
748
749 return mtk_eint_set_debounce(hw->eint, desc->eint.eint_n, debounce);
750}
751
752static int mtk_build_gpiochip(struct mtk_pinctrl *hw, struct device_node *np)
753{
754 struct gpio_chip *chip = &hw->chip;
755 int ret;
756
757 chip->label = PINCTRL_PINCTRL_DEV;
758 chip->parent = hw->dev;
759 chip->request = gpiochip_generic_request;
760 chip->free = gpiochip_generic_free;
761 chip->get_direction = mtk_gpio_get_direction;
762 chip->direction_input = mtk_gpio_direction_input;
763 chip->direction_output = mtk_gpio_direction_output;
764 chip->get = mtk_gpio_get;
765 chip->set = mtk_gpio_set;
Sean Wang65618592018-09-08 19:07:38 +0800766 chip->to_irq = mtk_gpio_to_irq,
Zhiyong Tao80525092018-09-08 19:07:33 +0800767 chip->set_config = mtk_gpio_set_config,
768 chip->base = -1;
769 chip->ngpio = hw->soc->npins;
770 chip->of_node = np;
771 chip->of_gpio_n_cells = 2;
772
773 ret = gpiochip_add_data(chip, hw);
774 if (ret < 0)
775 return ret;
776
777 return 0;
778}
779
780static int mtk_pctrl_build_state(struct platform_device *pdev)
781{
782 struct mtk_pinctrl *hw = platform_get_drvdata(pdev);
783 int i;
784
785 /* Allocate groups */
786 hw->groups = devm_kmalloc_array(&pdev->dev, hw->soc->ngrps,
787 sizeof(*hw->groups), GFP_KERNEL);
788 if (!hw->groups)
789 return -ENOMEM;
790
791 /* We assume that one pin is one group, use pin name as group name. */
792 hw->grp_names = devm_kmalloc_array(&pdev->dev, hw->soc->ngrps,
793 sizeof(*hw->grp_names), GFP_KERNEL);
794 if (!hw->grp_names)
795 return -ENOMEM;
796
797 for (i = 0; i < hw->soc->npins; i++) {
798 const struct mtk_pin_desc *pin = hw->soc->pins + i;
799 struct mtk_pinctrl_group *group = hw->groups + i;
800
801 group->name = pin->name;
802 group->pin = pin->number;
803
804 hw->grp_names[i] = pin->name;
805 }
806
807 return 0;
808}
809
810int mtk_paris_pinctrl_probe(struct platform_device *pdev,
811 const struct mtk_pin_soc *soc)
812{
813 struct pinctrl_pin_desc *pins;
814 struct mtk_pinctrl *hw;
815 struct resource *res;
816 int err, i;
817
818 hw = devm_kzalloc(&pdev->dev, sizeof(*hw), GFP_KERNEL);
819 if (!hw)
820 return -ENOMEM;
821
822 platform_set_drvdata(pdev, hw);
823 hw->soc = soc;
824 hw->dev = &pdev->dev;
825
826 if (!hw->soc->nbase_names) {
827 dev_err(&pdev->dev,
828 "SoC should be assigned at least one register base\n");
829 return -EINVAL;
830 }
831
832 hw->base = devm_kmalloc_array(&pdev->dev, hw->soc->nbase_names,
833 sizeof(*hw->base), GFP_KERNEL);
Wei Yongjun184744e2018-09-20 06:21:28 +0000834 if (!hw->base)
835 return -ENOMEM;
Zhiyong Tao80525092018-09-08 19:07:33 +0800836
837 for (i = 0; i < hw->soc->nbase_names; i++) {
838 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
839 hw->soc->base_names[i]);
840 if (!res) {
841 dev_err(&pdev->dev, "missing IO resource\n");
842 return -ENXIO;
843 }
844
845 hw->base[i] = devm_ioremap_resource(&pdev->dev, res);
846 if (IS_ERR(hw->base[i]))
847 return PTR_ERR(hw->base[i]);
848 }
849
850 hw->nbase = hw->soc->nbase_names;
851
852 err = mtk_pctrl_build_state(pdev);
853 if (err) {
854 dev_err(&pdev->dev, "build state failed: %d\n", err);
855 return -EINVAL;
856 }
857
858 /* Copy from internal struct mtk_pin_desc to register to the core */
859 pins = devm_kmalloc_array(&pdev->dev, hw->soc->npins, sizeof(*pins),
860 GFP_KERNEL);
Wei Yongjun184744e2018-09-20 06:21:28 +0000861 if (!pins)
862 return -ENOMEM;
Zhiyong Tao80525092018-09-08 19:07:33 +0800863
864 for (i = 0; i < hw->soc->npins; i++) {
865 pins[i].number = hw->soc->pins[i].number;
866 pins[i].name = hw->soc->pins[i].name;
867 }
868
869 /* Setup pins descriptions per SoC types */
870 mtk_desc.pins = (const struct pinctrl_pin_desc *)pins;
871 mtk_desc.npins = hw->soc->npins;
872 mtk_desc.num_custom_params = ARRAY_SIZE(mtk_custom_bindings);
873 mtk_desc.custom_params = mtk_custom_bindings;
874#ifdef CONFIG_DEBUG_FS
875 mtk_desc.custom_conf_items = mtk_conf_items;
876#endif
877
878 err = devm_pinctrl_register_and_init(&pdev->dev, &mtk_desc, hw,
879 &hw->pctrl);
880 if (err)
881 return err;
882
883 err = pinctrl_enable(hw->pctrl);
884 if (err)
885 return err;
886
Sean Wang65618592018-09-08 19:07:38 +0800887 err = mtk_build_eint(hw, pdev);
888 if (err)
889 dev_warn(&pdev->dev,
890 "Failed to add EINT, but pinctrl still can work\n");
891
Zhiyong Tao80525092018-09-08 19:07:33 +0800892 /* Build gpiochip should be after pinctrl_enable is done */
893 err = mtk_build_gpiochip(hw, pdev->dev.of_node);
894 if (err) {
895 dev_err(&pdev->dev, "Failed to add gpio_chip\n");
896 return err;
897 }
898
899 platform_set_drvdata(pdev, hw);
900
901 return 0;
902}
Nicolas Boichat5c090442019-05-08 15:33:30 +0800903
904static int mtk_paris_pinctrl_suspend(struct device *device)
905{
906 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
907
908 return mtk_eint_do_suspend(pctl->eint);
909}
910
911static int mtk_paris_pinctrl_resume(struct device *device)
912{
913 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
914
915 return mtk_eint_do_resume(pctl->eint);
916}
917
918const struct dev_pm_ops mtk_paris_pinctrl_pm_ops = {
919 .suspend_noirq = mtk_paris_pinctrl_suspend,
920 .resume_noirq = mtk_paris_pinctrl_resume,
921};