1 // SPDX-License-Identifier: GPL-2.0
3 * MediaTek Pinctrl Paris Driver, which implement the vendor per-pin
4 * bindings for MediaTek SoC.
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>
12 #include <linux/gpio/driver.h>
13 #include <dt-bindings/pinctrl/mt65xx.h>
14 #include "pinctrl-paris.h"
16 #define PINCTRL_PINCTRL_DEV KBUILD_MODNAME
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)
23 #define MTK_PIN_CONFIG_DRV_ADV (PIN_CONFIG_END + 5)
25 static 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},
30 {"mediatek,drive-strength-adv", MTK_PIN_CONFIG_DRV_ADV
, 2},
33 #ifdef CONFIG_DEBUG_FS
34 static 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),
39 PCONFDUMP(MTK_PIN_CONFIG_DRV_ADV
, "drive-strength-adv", NULL
, true),
43 static 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",
50 static int mtk_pinmux_gpio_request_enable(struct pinctrl_dev
*pctldev
,
51 struct pinctrl_gpio_range
*range
,
54 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
55 const struct mtk_pin_desc
*desc
;
57 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[pin
];
59 return mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_MODE
,
63 static int mtk_pinmux_gpio_set_direction(struct pinctrl_dev
*pctldev
,
64 struct pinctrl_gpio_range
*range
,
65 unsigned int pin
, bool input
)
67 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
68 const struct mtk_pin_desc
*desc
;
70 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[pin
];
72 /* hardware would take 0 as input direction */
73 return mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_DIR
, !input
);
76 static int mtk_pinconf_get(struct pinctrl_dev
*pctldev
,
77 unsigned int pin
, unsigned long *config
)
79 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
80 u32 param
= pinconf_to_config_param(*config
);
81 int pullup
, err
, reg
, ret
= 1;
82 const struct mtk_pin_desc
*desc
;
84 if (pin
>= hw
->soc
->npins
) {
88 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[pin
];
91 case PIN_CONFIG_BIAS_DISABLE
:
92 case PIN_CONFIG_BIAS_PULL_UP
:
93 case PIN_CONFIG_BIAS_PULL_DOWN
:
94 if (hw
->soc
->bias_get_combo
) {
95 err
= hw
->soc
->bias_get_combo(hw
, desc
, &pullup
, &ret
);
98 if (param
== PIN_CONFIG_BIAS_DISABLE
) {
99 if (ret
== MTK_PUPD_SET_R1R0_00
)
101 } else if (param
== PIN_CONFIG_BIAS_PULL_UP
) {
102 /* When desire to get pull-up value, return
103 * error if current setting is pull-down
107 } else if (param
== PIN_CONFIG_BIAS_PULL_DOWN
) {
108 /* When desire to get pull-down value, return
109 * error if current setting is pull-up
118 case PIN_CONFIG_SLEW_RATE
:
119 err
= mtk_hw_get_value(hw
, desc
, PINCTRL_PIN_REG_SR
, &ret
);
121 case PIN_CONFIG_INPUT_ENABLE
:
122 case PIN_CONFIG_OUTPUT_ENABLE
:
123 err
= mtk_hw_get_value(hw
, desc
, PINCTRL_PIN_REG_DIR
, &ret
);
126 /* CONFIG Current direction return value
127 * ------------- ----------------- ----------------------
128 * OUTPUT_ENABLE output 1 (= HW value)
129 * input 0 (= HW value)
130 * INPUT_ENABLE output 0 (= reverse HW value)
131 * input 1 (= reverse HW value)
133 if (param
== PIN_CONFIG_INPUT_ENABLE
)
137 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
138 err
= mtk_hw_get_value(hw
, desc
, PINCTRL_PIN_REG_DIR
, &ret
);
141 /* return error when in output mode
142 * because schmitt trigger only work in input mode
149 err
= mtk_hw_get_value(hw
, desc
, PINCTRL_PIN_REG_SMT
, &ret
);
152 case PIN_CONFIG_DRIVE_STRENGTH
:
153 if (hw
->soc
->drive_get
)
154 err
= hw
->soc
->drive_get(hw
, desc
, &ret
);
158 case MTK_PIN_CONFIG_TDSEL
:
159 case MTK_PIN_CONFIG_RDSEL
:
160 reg
= (param
== MTK_PIN_CONFIG_TDSEL
) ?
161 PINCTRL_PIN_REG_TDSEL
: PINCTRL_PIN_REG_RDSEL
;
162 err
= mtk_hw_get_value(hw
, desc
, reg
, &ret
);
164 case MTK_PIN_CONFIG_PU_ADV
:
165 case MTK_PIN_CONFIG_PD_ADV
:
166 if (hw
->soc
->adv_pull_get
) {
169 pullup
= param
== MTK_PIN_CONFIG_PU_ADV
;
170 err
= hw
->soc
->adv_pull_get(hw
, desc
, pullup
, &ret
);
174 case MTK_PIN_CONFIG_DRV_ADV
:
175 if (hw
->soc
->adv_drive_get
)
176 err
= hw
->soc
->adv_drive_get(hw
, desc
, &ret
);
186 *config
= pinconf_to_config_packed(param
, ret
);
191 static int mtk_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
192 enum pin_config_param param
,
193 enum pin_config_param arg
)
195 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
196 const struct mtk_pin_desc
*desc
;
200 if (pin
>= hw
->soc
->npins
) {
204 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[pin
];
206 switch ((u32
)param
) {
207 case PIN_CONFIG_BIAS_DISABLE
:
208 if (hw
->soc
->bias_set_combo
)
209 err
= hw
->soc
->bias_set_combo(hw
, desc
, 0, MTK_DISABLE
);
213 case PIN_CONFIG_BIAS_PULL_UP
:
214 if (hw
->soc
->bias_set_combo
)
215 err
= hw
->soc
->bias_set_combo(hw
, desc
, 1, arg
);
219 case PIN_CONFIG_BIAS_PULL_DOWN
:
220 if (hw
->soc
->bias_set_combo
)
221 err
= hw
->soc
->bias_set_combo(hw
, desc
, 0, arg
);
225 case PIN_CONFIG_OUTPUT_ENABLE
:
226 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_SMT
,
228 /* Keep set direction to consider the case that a GPIO pin
229 * does not have SMT control
231 if (err
!= -ENOTSUPP
)
234 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_DIR
,
237 case PIN_CONFIG_INPUT_ENABLE
:
238 /* regard all non-zero value as enable */
239 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_IES
, !!arg
);
243 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_DIR
,
246 case PIN_CONFIG_SLEW_RATE
:
247 /* regard all non-zero value as enable */
248 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_SR
, !!arg
);
250 case PIN_CONFIG_OUTPUT
:
251 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_DIR
,
256 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_DO
,
259 case PIN_CONFIG_INPUT_SCHMITT
:
260 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
261 /* arg = 1: Input mode & SMT enable ;
262 * arg = 0: Output mode & SMT disable
264 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_DIR
, !arg
);
268 err
= mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_SMT
, !!arg
);
270 case PIN_CONFIG_DRIVE_STRENGTH
:
271 if (hw
->soc
->drive_set
)
272 err
= hw
->soc
->drive_set(hw
, desc
, arg
);
276 case MTK_PIN_CONFIG_TDSEL
:
277 case MTK_PIN_CONFIG_RDSEL
:
278 reg
= (param
== MTK_PIN_CONFIG_TDSEL
) ?
279 PINCTRL_PIN_REG_TDSEL
: PINCTRL_PIN_REG_RDSEL
;
280 err
= mtk_hw_set_value(hw
, desc
, reg
, arg
);
282 case MTK_PIN_CONFIG_PU_ADV
:
283 case MTK_PIN_CONFIG_PD_ADV
:
284 if (hw
->soc
->adv_pull_set
) {
287 pullup
= param
== MTK_PIN_CONFIG_PU_ADV
;
288 err
= hw
->soc
->adv_pull_set(hw
, desc
, pullup
,
293 case MTK_PIN_CONFIG_DRV_ADV
:
294 if (hw
->soc
->adv_drive_set
)
295 err
= hw
->soc
->adv_drive_set(hw
, desc
, arg
);
307 static struct mtk_pinctrl_group
*
308 mtk_pctrl_find_group_by_pin(struct mtk_pinctrl
*hw
, u32 pin
)
312 for (i
= 0; i
< hw
->soc
->ngrps
; i
++) {
313 struct mtk_pinctrl_group
*grp
= hw
->groups
+ i
;
322 static const struct mtk_func_desc
*
323 mtk_pctrl_find_function_by_pin(struct mtk_pinctrl
*hw
, u32 pin_num
, u32 fnum
)
325 const struct mtk_pin_desc
*pin
= hw
->soc
->pins
+ pin_num
;
326 const struct mtk_func_desc
*func
= pin
->funcs
;
328 while (func
&& func
->name
) {
329 if (func
->muxval
== fnum
)
337 static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl
*hw
, u32 pin_num
,
342 for (i
= 0; i
< hw
->soc
->npins
; i
++) {
343 const struct mtk_pin_desc
*pin
= hw
->soc
->pins
+ i
;
345 if (pin
->number
== pin_num
) {
346 const struct mtk_func_desc
*func
= pin
->funcs
;
348 while (func
&& func
->name
) {
349 if (func
->muxval
== fnum
)
361 static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl
*pctl
,
363 struct mtk_pinctrl_group
*grp
,
364 struct pinctrl_map
**map
,
365 unsigned *reserved_maps
,
370 if (*num_maps
== *reserved_maps
)
373 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
374 (*map
)[*num_maps
].data
.mux
.group
= grp
->name
;
376 ret
= mtk_pctrl_is_function_valid(pctl
, pin
, fnum
);
378 dev_err(pctl
->dev
, "invalid function %d on pin %d .\n",
383 (*map
)[*num_maps
].data
.mux
.function
= mtk_gpio_functions
[fnum
];
389 static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
390 struct device_node
*node
,
391 struct pinctrl_map
**map
,
392 unsigned *reserved_maps
,
395 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
396 int num_pins
, num_funcs
, maps_per_pin
, i
, err
;
397 struct mtk_pinctrl_group
*grp
;
398 unsigned int num_configs
;
399 bool has_config
= false;
400 unsigned long *configs
;
401 u32 pinfunc
, pin
, func
;
402 struct property
*pins
;
403 unsigned reserve
= 0;
405 pins
= of_find_property(node
, "pinmux", NULL
);
407 dev_err(hw
->dev
, "missing pins property in node %pOFn .\n",
412 err
= pinconf_generic_parse_dt_config(node
, pctldev
, &configs
,
420 num_pins
= pins
->length
/ sizeof(u32
);
421 num_funcs
= num_pins
;
425 if (has_config
&& num_pins
>= 1)
428 if (!num_pins
|| !maps_per_pin
) {
433 reserve
= num_pins
* maps_per_pin
;
435 err
= pinctrl_utils_reserve_map(pctldev
, map
, reserved_maps
, num_maps
,
440 for (i
= 0; i
< num_pins
; i
++) {
441 err
= of_property_read_u32_index(node
, "pinmux", i
, &pinfunc
);
445 pin
= MTK_GET_PIN_NO(pinfunc
);
446 func
= MTK_GET_PIN_FUNC(pinfunc
);
448 if (pin
>= hw
->soc
->npins
||
449 func
>= ARRAY_SIZE(mtk_gpio_functions
)) {
450 dev_err(hw
->dev
, "invalid pins value.\n");
455 grp
= mtk_pctrl_find_group_by_pin(hw
, pin
);
457 dev_err(hw
->dev
, "unable to match pin %d to group\n",
463 err
= mtk_pctrl_dt_node_to_map_func(hw
, pin
, func
, grp
, map
,
464 reserved_maps
, num_maps
);
469 err
= pinctrl_utils_add_map_configs(pctldev
, map
,
475 PIN_MAP_TYPE_CONFIGS_GROUP
);
488 static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
489 struct device_node
*np_config
,
490 struct pinctrl_map
**map
,
493 struct device_node
*np
;
494 unsigned reserved_maps
;
501 for_each_child_of_node(np_config
, np
) {
502 ret
= mtk_pctrl_dt_subnode_to_map(pctldev
, np
, map
,
506 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
515 static int mtk_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
517 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
519 return hw
->soc
->ngrps
;
522 static const char *mtk_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
525 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
527 return hw
->groups
[group
].name
;
530 static int mtk_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
531 unsigned group
, const unsigned **pins
,
534 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
536 *pins
= (unsigned *)&hw
->groups
[group
].pin
;
542 static int mtk_hw_get_value_wrap(struct mtk_pinctrl
*hw
, unsigned int gpio
, int field
)
544 const struct mtk_pin_desc
*desc
;
547 if (gpio
>= hw
->soc
->npins
)
550 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[gpio
];
552 err
= mtk_hw_get_value(hw
, desc
, field
, &value
);
559 #define mtk_pctrl_get_pinmux(hw, gpio) \
560 mtk_hw_get_value_wrap(hw, gpio, PINCTRL_PIN_REG_MODE)
562 #define mtk_pctrl_get_direction(hw, gpio) \
563 mtk_hw_get_value_wrap(hw, gpio, PINCTRL_PIN_REG_DIR)
565 #define mtk_pctrl_get_out(hw, gpio) \
566 mtk_hw_get_value_wrap(hw, gpio, PINCTRL_PIN_REG_DO)
568 #define mtk_pctrl_get_in(hw, gpio) \
569 mtk_hw_get_value_wrap(hw, gpio, PINCTRL_PIN_REG_DI)
571 #define mtk_pctrl_get_smt(hw, gpio) \
572 mtk_hw_get_value_wrap(hw, gpio, PINCTRL_PIN_REG_SMT)
574 #define mtk_pctrl_get_ies(hw, gpio) \
575 mtk_hw_get_value_wrap(hw, gpio, PINCTRL_PIN_REG_IES)
577 #define mtk_pctrl_get_driving(hw, gpio) \
578 mtk_hw_get_value_wrap(hw, gpio, PINCTRL_PIN_REG_DRV)
580 ssize_t
mtk_pctrl_show_one_pin(struct mtk_pinctrl
*hw
,
581 unsigned int gpio
, char *buf
, unsigned int bufLen
)
583 int pinmux
, pullup
, pullen
, len
= 0, r1
= -1, r0
= -1;
584 const struct mtk_pin_desc
*desc
;
586 if (gpio
>= hw
->soc
->npins
)
589 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[gpio
];
590 pinmux
= mtk_pctrl_get_pinmux(hw
, gpio
);
591 if (pinmux
>= hw
->soc
->nfuncs
)
592 pinmux
-= hw
->soc
->nfuncs
;
594 mtk_pinconf_bias_get_combo(hw
, desc
, &pullup
, &pullen
);
595 if (pullen
== MTK_PUPD_SET_R1R0_00
) {
599 } else if (pullen
== MTK_PUPD_SET_R1R0_01
) {
603 } else if (pullen
== MTK_PUPD_SET_R1R0_10
) {
607 } else if (pullen
== MTK_PUPD_SET_R1R0_11
) {
611 } else if (pullen
!= MTK_DISABLE
&& pullen
!= MTK_ENABLE
) {
614 len
+= scnprintf(buf
+ len
, bufLen
- len
,
615 "%03d: %1d%1d%1d%1d%02d%1d%1d%1d%1d",
618 mtk_pctrl_get_direction(hw
, gpio
),
619 mtk_pctrl_get_out(hw
, gpio
),
620 mtk_pctrl_get_in(hw
, gpio
),
621 mtk_pctrl_get_driving(hw
, gpio
),
622 mtk_pctrl_get_smt(hw
, gpio
),
623 mtk_pctrl_get_ies(hw
, gpio
),
628 len
+= scnprintf(buf
+ len
, bufLen
- len
, " (%1d %1d)\n",
631 len
+= scnprintf(buf
+ len
, bufLen
- len
, "\n");
637 #define PIN_DBG_BUF_SZ 96
638 static void mtk_pctrl_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
641 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
642 char buf
[PIN_DBG_BUF_SZ
];
644 (void)mtk_pctrl_show_one_pin(hw
, gpio
, buf
, PIN_DBG_BUF_SZ
);
646 seq_printf(s
, "%s", buf
);
649 static const struct pinctrl_ops mtk_pctlops
= {
650 .dt_node_to_map
= mtk_pctrl_dt_node_to_map
,
651 .dt_free_map
= pinctrl_utils_free_map
,
652 .get_groups_count
= mtk_pctrl_get_groups_count
,
653 .get_group_name
= mtk_pctrl_get_group_name
,
654 .get_group_pins
= mtk_pctrl_get_group_pins
,
655 .pin_dbg_show
= mtk_pctrl_dbg_show
,
658 static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
660 return ARRAY_SIZE(mtk_gpio_functions
);
663 static const char *mtk_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
666 return mtk_gpio_functions
[selector
];
669 static int mtk_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
671 const char * const **groups
,
672 unsigned * const num_groups
)
674 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
676 *groups
= hw
->grp_names
;
677 *num_groups
= hw
->soc
->ngrps
;
682 static int mtk_pmx_set_mux(struct pinctrl_dev
*pctldev
,
686 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
687 struct mtk_pinctrl_group
*grp
= hw
->groups
+ group
;
688 const struct mtk_func_desc
*desc_func
;
689 const struct mtk_pin_desc
*desc
;
692 ret
= mtk_pctrl_is_function_valid(hw
, grp
->pin
, function
);
694 dev_err(hw
->dev
, "invalid function %d on group %d .\n",
699 desc_func
= mtk_pctrl_find_function_by_pin(hw
, grp
->pin
, function
);
703 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[grp
->pin
];
704 mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_MODE
, desc_func
->muxval
);
709 static const struct pinmux_ops mtk_pmxops
= {
710 .get_functions_count
= mtk_pmx_get_funcs_cnt
,
711 .get_function_name
= mtk_pmx_get_func_name
,
712 .get_function_groups
= mtk_pmx_get_func_groups
,
713 .set_mux
= mtk_pmx_set_mux
,
714 .gpio_set_direction
= mtk_pinmux_gpio_set_direction
,
715 .gpio_request_enable
= mtk_pinmux_gpio_request_enable
,
718 static int mtk_pconf_group_get(struct pinctrl_dev
*pctldev
, unsigned group
,
719 unsigned long *config
)
721 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
723 *config
= hw
->groups
[group
].config
;
728 static int mtk_pconf_group_set(struct pinctrl_dev
*pctldev
, unsigned group
,
729 unsigned long *configs
, unsigned num_configs
)
731 struct mtk_pinctrl
*hw
= pinctrl_dev_get_drvdata(pctldev
);
732 struct mtk_pinctrl_group
*grp
= &hw
->groups
[group
];
735 for (i
= 0; i
< num_configs
; i
++) {
736 ret
= mtk_pinconf_set(pctldev
, grp
->pin
,
737 pinconf_to_config_param(configs
[i
]),
738 pinconf_to_config_argument(configs
[i
]));
742 grp
->config
= configs
[i
];
748 static const struct pinconf_ops mtk_confops
= {
749 .pin_config_get
= mtk_pinconf_get
,
750 .pin_config_group_get
= mtk_pconf_group_get
,
751 .pin_config_group_set
= mtk_pconf_group_set
,
755 static struct pinctrl_desc mtk_desc
= {
756 .name
= PINCTRL_PINCTRL_DEV
,
757 .pctlops
= &mtk_pctlops
,
758 .pmxops
= &mtk_pmxops
,
759 .confops
= &mtk_confops
,
760 .owner
= THIS_MODULE
,
763 static int mtk_gpio_get_direction(struct gpio_chip
*chip
, unsigned int gpio
)
765 struct mtk_pinctrl
*hw
= gpiochip_get_data(chip
);
766 const struct mtk_pin_desc
*desc
;
769 if (gpio
>= hw
->soc
->npins
)
772 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[gpio
];
774 err
= mtk_hw_get_value(hw
, desc
, PINCTRL_PIN_REG_DIR
, &value
);
779 return GPIO_LINE_DIRECTION_OUT
;
781 return GPIO_LINE_DIRECTION_IN
;
784 static int mtk_gpio_get(struct gpio_chip
*chip
, unsigned int gpio
)
786 struct mtk_pinctrl
*hw
= gpiochip_get_data(chip
);
787 const struct mtk_pin_desc
*desc
;
790 if (gpio
>= hw
->soc
->npins
)
793 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[gpio
];
795 err
= mtk_hw_get_value(hw
, desc
, PINCTRL_PIN_REG_DI
, &value
);
802 static void mtk_gpio_set(struct gpio_chip
*chip
, unsigned int gpio
, int value
)
804 struct mtk_pinctrl
*hw
= gpiochip_get_data(chip
);
805 const struct mtk_pin_desc
*desc
;
807 if (gpio
>= hw
->soc
->npins
)
810 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[gpio
];
812 mtk_hw_set_value(hw
, desc
, PINCTRL_PIN_REG_DO
, !!value
);
815 static int mtk_gpio_direction_input(struct gpio_chip
*chip
, unsigned int gpio
)
817 struct mtk_pinctrl
*hw
= gpiochip_get_data(chip
);
819 if (gpio
>= hw
->soc
->npins
)
822 return pinctrl_gpio_direction_input(chip
->base
+ gpio
);
825 static int mtk_gpio_direction_output(struct gpio_chip
*chip
, unsigned int gpio
,
828 struct mtk_pinctrl
*hw
= gpiochip_get_data(chip
);
830 if (gpio
>= hw
->soc
->npins
)
833 mtk_gpio_set(chip
, gpio
, value
);
835 return pinctrl_gpio_direction_output(chip
->base
+ gpio
);
838 static int mtk_gpio_to_irq(struct gpio_chip
*chip
, unsigned int offset
)
840 struct mtk_pinctrl
*hw
= gpiochip_get_data(chip
);
841 const struct mtk_pin_desc
*desc
;
846 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[offset
];
848 if (desc
->eint
.eint_n
== EINT_NA
)
851 return mtk_eint_find_irq(hw
->eint
, desc
->eint
.eint_n
);
854 static int mtk_gpio_set_config(struct gpio_chip
*chip
, unsigned int offset
,
855 unsigned long config
)
857 struct mtk_pinctrl
*hw
= gpiochip_get_data(chip
);
858 const struct mtk_pin_desc
*desc
;
861 desc
= (const struct mtk_pin_desc
*)&hw
->soc
->pins
[offset
];
864 pinconf_to_config_param(config
) != PIN_CONFIG_INPUT_DEBOUNCE
||
865 desc
->eint
.eint_n
== EINT_NA
)
868 debounce
= pinconf_to_config_argument(config
);
870 return mtk_eint_set_debounce(hw
->eint
, desc
->eint
.eint_n
, debounce
);
873 static int mtk_build_gpiochip(struct mtk_pinctrl
*hw
, struct device_node
*np
)
875 struct gpio_chip
*chip
= &hw
->chip
;
878 chip
->label
= PINCTRL_PINCTRL_DEV
;
879 chip
->parent
= hw
->dev
;
880 chip
->request
= gpiochip_generic_request
;
881 chip
->free
= gpiochip_generic_free
;
882 chip
->get_direction
= mtk_gpio_get_direction
;
883 chip
->direction_input
= mtk_gpio_direction_input
;
884 chip
->direction_output
= mtk_gpio_direction_output
;
885 chip
->get
= mtk_gpio_get
;
886 chip
->set
= mtk_gpio_set
;
887 chip
->to_irq
= mtk_gpio_to_irq
,
888 chip
->set_config
= mtk_gpio_set_config
,
890 chip
->ngpio
= hw
->soc
->npins
;
892 chip
->of_gpio_n_cells
= 2;
894 ret
= gpiochip_add_data(chip
, hw
);
901 static int mtk_pctrl_build_state(struct platform_device
*pdev
)
903 struct mtk_pinctrl
*hw
= platform_get_drvdata(pdev
);
906 /* Allocate groups */
907 hw
->groups
= devm_kmalloc_array(&pdev
->dev
, hw
->soc
->ngrps
,
908 sizeof(*hw
->groups
), GFP_KERNEL
);
912 /* We assume that one pin is one group, use pin name as group name. */
913 hw
->grp_names
= devm_kmalloc_array(&pdev
->dev
, hw
->soc
->ngrps
,
914 sizeof(*hw
->grp_names
), GFP_KERNEL
);
918 for (i
= 0; i
< hw
->soc
->npins
; i
++) {
919 const struct mtk_pin_desc
*pin
= hw
->soc
->pins
+ i
;
920 struct mtk_pinctrl_group
*group
= hw
->groups
+ i
;
922 group
->name
= pin
->name
;
923 group
->pin
= pin
->number
;
925 hw
->grp_names
[i
] = pin
->name
;
931 int mtk_paris_pinctrl_probe(struct platform_device
*pdev
,
932 const struct mtk_pin_soc
*soc
)
934 struct pinctrl_pin_desc
*pins
;
935 struct mtk_pinctrl
*hw
;
936 struct resource
*res
;
939 hw
= devm_kzalloc(&pdev
->dev
, sizeof(*hw
), GFP_KERNEL
);
943 platform_set_drvdata(pdev
, hw
);
945 hw
->dev
= &pdev
->dev
;
947 if (!hw
->soc
->nbase_names
) {
949 "SoC should be assigned at least one register base\n");
953 hw
->base
= devm_kmalloc_array(&pdev
->dev
, hw
->soc
->nbase_names
,
954 sizeof(*hw
->base
), GFP_KERNEL
);
958 for (i
= 0; i
< hw
->soc
->nbase_names
; i
++) {
959 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
960 hw
->soc
->base_names
[i
]);
962 dev_err(&pdev
->dev
, "missing IO resource\n");
966 hw
->base
[i
] = devm_ioremap_resource(&pdev
->dev
, res
);
967 if (IS_ERR(hw
->base
[i
]))
968 return PTR_ERR(hw
->base
[i
]);
971 hw
->nbase
= hw
->soc
->nbase_names
;
973 err
= mtk_pctrl_build_state(pdev
);
975 dev_err(&pdev
->dev
, "build state failed: %d\n", err
);
979 /* Copy from internal struct mtk_pin_desc to register to the core */
980 pins
= devm_kmalloc_array(&pdev
->dev
, hw
->soc
->npins
, sizeof(*pins
),
985 for (i
= 0; i
< hw
->soc
->npins
; i
++) {
986 pins
[i
].number
= hw
->soc
->pins
[i
].number
;
987 pins
[i
].name
= hw
->soc
->pins
[i
].name
;
990 /* Setup pins descriptions per SoC types */
991 mtk_desc
.pins
= (const struct pinctrl_pin_desc
*)pins
;
992 mtk_desc
.npins
= hw
->soc
->npins
;
993 mtk_desc
.num_custom_params
= ARRAY_SIZE(mtk_custom_bindings
);
994 mtk_desc
.custom_params
= mtk_custom_bindings
;
995 #ifdef CONFIG_DEBUG_FS
996 mtk_desc
.custom_conf_items
= mtk_conf_items
;
999 err
= devm_pinctrl_register_and_init(&pdev
->dev
, &mtk_desc
, hw
,
1004 err
= pinctrl_enable(hw
->pctrl
);
1008 err
= mtk_build_eint(hw
, pdev
);
1010 dev_warn(&pdev
->dev
,
1011 "Failed to add EINT, but pinctrl still can work\n");
1013 /* Build gpiochip should be after pinctrl_enable is done */
1014 err
= mtk_build_gpiochip(hw
, pdev
->dev
.of_node
);
1016 dev_err(&pdev
->dev
, "Failed to add gpio_chip\n");
1020 platform_set_drvdata(pdev
, hw
);
1025 static int mtk_paris_pinctrl_suspend(struct device
*device
)
1027 struct mtk_pinctrl
*pctl
= dev_get_drvdata(device
);
1029 return mtk_eint_do_suspend(pctl
->eint
);
1032 static int mtk_paris_pinctrl_resume(struct device
*device
)
1034 struct mtk_pinctrl
*pctl
= dev_get_drvdata(device
);
1036 return mtk_eint_do_resume(pctl
->eint
);
1039 const struct dev_pm_ops mtk_paris_pinctrl_pm_ops
= {
1040 .suspend_noirq
= mtk_paris_pinctrl_suspend
,
1041 .resume_noirq
= mtk_paris_pinctrl_resume
,