1 // SPDX-License-Identifier: GPL-2.0-only
3 * mt65xx pinctrl driver based on Allwinner A1X pinctrl driver.
4 * Copyright (c) 2014 MediaTek Inc.
5 * Author: Hongzhou.Yang <hongzhou.yang@mediatek.com>
9 #include <linux/gpio/driver.h>
11 #include <linux/of_address.h>
12 #include <linux/of_device.h>
13 #include <linux/of_irq.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/pinctrl/machine.h>
16 #include <linux/pinctrl/pinconf.h>
17 #include <linux/pinctrl/pinconf-generic.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/bitops.h>
23 #include <linux/regmap.h>
24 #include <linux/mfd/syscon.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
28 #include <dt-bindings/pinctrl/mt65xx.h>
31 #include "../pinconf.h"
32 #include "../pinctrl-utils.h"
34 #include "pinctrl-mtk-common.h"
36 #define MAX_GPIO_MODE_PER_REG 5
37 #define GPIO_MODE_BITS 3
38 #define GPIO_MODE_PREFIX "GPIO"
40 static const char * const mtk_gpio_functions
[] = {
41 "func0", "func1", "func2", "func3",
42 "func4", "func5", "func6", "func7",
43 "func8", "func9", "func10", "func11",
44 "func12", "func13", "func14", "func15",
48 * There are two base address for pull related configuration
49 * in mt8135, and different GPIO pins use different base address.
50 * When pin number greater than type1_start and less than type1_end,
51 * should use the second base address.
53 static struct regmap
*mtk_get_regmap(struct mtk_pinctrl
*pctl
,
56 if (pin
>= pctl
->devdata
->type1_start
&& pin
< pctl
->devdata
->type1_end
)
61 static unsigned int mtk_get_port(struct mtk_pinctrl
*pctl
, unsigned long pin
)
63 /* Different SoC has different mask and port shift. */
64 return ((pin
>> 4) & pctl
->devdata
->port_mask
)
65 << pctl
->devdata
->port_shf
;
68 static int mtk_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
69 struct pinctrl_gpio_range
*range
, unsigned offset
,
72 unsigned int reg_addr
;
74 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
76 reg_addr
= mtk_get_port(pctl
, offset
) + pctl
->devdata
->dir_offset
;
77 bit
= BIT(offset
& 0xf);
79 if (pctl
->devdata
->spec_dir_set
)
80 pctl
->devdata
->spec_dir_set(®_addr
, offset
);
83 /* Different SoC has different alignment offset. */
84 reg_addr
= CLR_ADDR(reg_addr
, pctl
);
86 reg_addr
= SET_ADDR(reg_addr
, pctl
);
88 regmap_write(mtk_get_regmap(pctl
, offset
), reg_addr
, bit
);
92 static void mtk_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
94 unsigned int reg_addr
;
96 struct mtk_pinctrl
*pctl
= gpiochip_get_data(chip
);
98 reg_addr
= mtk_get_port(pctl
, offset
) + pctl
->devdata
->dout_offset
;
99 bit
= BIT(offset
& 0xf);
102 reg_addr
= SET_ADDR(reg_addr
, pctl
);
104 reg_addr
= CLR_ADDR(reg_addr
, pctl
);
106 regmap_write(mtk_get_regmap(pctl
, offset
), reg_addr
, bit
);
109 static int mtk_pconf_set_ies_smt(struct mtk_pinctrl
*pctl
, unsigned pin
,
110 int value
, enum pin_config_param arg
)
112 unsigned int reg_addr
, offset
;
116 * Due to some soc are not support ies/smt config, add this special
117 * control to handle it.
119 if (!pctl
->devdata
->spec_ies_smt_set
&&
120 pctl
->devdata
->ies_offset
== MTK_PINCTRL_NOT_SUPPORT
&&
121 arg
== PIN_CONFIG_INPUT_ENABLE
)
124 if (!pctl
->devdata
->spec_ies_smt_set
&&
125 pctl
->devdata
->smt_offset
== MTK_PINCTRL_NOT_SUPPORT
&&
126 arg
== PIN_CONFIG_INPUT_SCHMITT_ENABLE
)
130 * Due to some pins are irregular, their input enable and smt
131 * control register are discontinuous, so we need this special handle.
133 if (pctl
->devdata
->spec_ies_smt_set
) {
134 return pctl
->devdata
->spec_ies_smt_set(mtk_get_regmap(pctl
, pin
),
135 pin
, pctl
->devdata
->port_align
, value
, arg
);
138 bit
= BIT(pin
& 0xf);
140 if (arg
== PIN_CONFIG_INPUT_ENABLE
)
141 offset
= pctl
->devdata
->ies_offset
;
143 offset
= pctl
->devdata
->smt_offset
;
146 reg_addr
= SET_ADDR(mtk_get_port(pctl
, pin
) + offset
, pctl
);
148 reg_addr
= CLR_ADDR(mtk_get_port(pctl
, pin
) + offset
, pctl
);
150 regmap_write(mtk_get_regmap(pctl
, pin
), reg_addr
, bit
);
154 int mtk_pconf_spec_set_ies_smt_range(struct regmap
*regmap
,
155 const struct mtk_pin_ies_smt_set
*ies_smt_infos
, unsigned int info_num
,
156 unsigned int pin
, unsigned char align
, int value
)
158 unsigned int i
, reg_addr
, bit
;
160 for (i
= 0; i
< info_num
; i
++) {
161 if (pin
>= ies_smt_infos
[i
].start
&&
162 pin
<= ies_smt_infos
[i
].end
) {
171 reg_addr
= ies_smt_infos
[i
].offset
+ align
;
173 reg_addr
= ies_smt_infos
[i
].offset
+ (align
<< 1);
175 bit
= BIT(ies_smt_infos
[i
].bit
);
176 regmap_write(regmap
, reg_addr
, bit
);
180 static const struct mtk_pin_drv_grp
*mtk_find_pin_drv_grp_by_pin(
181 struct mtk_pinctrl
*pctl
, unsigned long pin
) {
184 for (i
= 0; i
< pctl
->devdata
->n_pin_drv_grps
; i
++) {
185 const struct mtk_pin_drv_grp
*pin_drv
=
186 pctl
->devdata
->pin_drv_grp
+ i
;
187 if (pin
== pin_drv
->pin
)
194 static int mtk_pconf_set_driving(struct mtk_pinctrl
*pctl
,
195 unsigned int pin
, unsigned char driving
)
197 const struct mtk_pin_drv_grp
*pin_drv
;
199 unsigned int bits
, mask
, shift
;
200 const struct mtk_drv_group_desc
*drv_grp
;
202 if (pin
>= pctl
->devdata
->npins
)
205 pin_drv
= mtk_find_pin_drv_grp_by_pin(pctl
, pin
);
206 if (!pin_drv
|| pin_drv
->grp
> pctl
->devdata
->n_grp_cls
)
209 drv_grp
= pctl
->devdata
->grp_desc
+ pin_drv
->grp
;
210 if (driving
>= drv_grp
->min_drv
&& driving
<= drv_grp
->max_drv
211 && !(driving
% drv_grp
->step
)) {
212 val
= driving
/ drv_grp
->step
- 1;
213 bits
= drv_grp
->high_bit
- drv_grp
->low_bit
+ 1;
214 mask
= BIT(bits
) - 1;
215 shift
= pin_drv
->bit
+ drv_grp
->low_bit
;
218 return regmap_update_bits(mtk_get_regmap(pctl
, pin
),
219 pin_drv
->offset
, mask
, val
);
225 int mtk_pctrl_spec_pull_set_samereg(struct regmap
*regmap
,
226 const struct mtk_pin_spec_pupd_set_samereg
*pupd_infos
,
227 unsigned int info_num
, unsigned int pin
,
228 unsigned char align
, bool isup
, unsigned int r1r0
)
231 unsigned int reg_pupd
, reg_set
, reg_rst
;
232 unsigned int bit_pupd
, bit_r0
, bit_r1
;
233 const struct mtk_pin_spec_pupd_set_samereg
*spec_pupd_pin
;
236 for (i
= 0; i
< info_num
; i
++) {
237 if (pin
== pupd_infos
[i
].pin
) {
246 spec_pupd_pin
= pupd_infos
+ i
;
247 reg_set
= spec_pupd_pin
->offset
+ align
;
248 reg_rst
= spec_pupd_pin
->offset
+ (align
<< 1);
255 bit_pupd
= BIT(spec_pupd_pin
->pupd_bit
);
256 regmap_write(regmap
, reg_pupd
, bit_pupd
);
258 bit_r0
= BIT(spec_pupd_pin
->r0_bit
);
259 bit_r1
= BIT(spec_pupd_pin
->r1_bit
);
262 case MTK_PUPD_SET_R1R0_00
:
263 regmap_write(regmap
, reg_rst
, bit_r0
);
264 regmap_write(regmap
, reg_rst
, bit_r1
);
266 case MTK_PUPD_SET_R1R0_01
:
267 regmap_write(regmap
, reg_set
, bit_r0
);
268 regmap_write(regmap
, reg_rst
, bit_r1
);
270 case MTK_PUPD_SET_R1R0_10
:
271 regmap_write(regmap
, reg_rst
, bit_r0
);
272 regmap_write(regmap
, reg_set
, bit_r1
);
274 case MTK_PUPD_SET_R1R0_11
:
275 regmap_write(regmap
, reg_set
, bit_r0
);
276 regmap_write(regmap
, reg_set
, bit_r1
);
285 static int mtk_pconf_set_pull_select(struct mtk_pinctrl
*pctl
,
286 unsigned int pin
, bool enable
, bool isup
, unsigned int arg
)
289 unsigned int reg_pullen
, reg_pullsel
, r1r0
;
292 /* Some pins' pull setting are very different,
293 * they have separate pull up/down bit, R0 and R1
294 * resistor bit, so we need this special handle.
296 if (pctl
->devdata
->spec_pull_set
) {
297 /* For special pins, bias-disable is set by R1R0,
298 * the parameter should be "MTK_PUPD_SET_R1R0_00".
300 r1r0
= enable
? arg
: MTK_PUPD_SET_R1R0_00
;
301 ret
= pctl
->devdata
->spec_pull_set(mtk_get_regmap(pctl
, pin
),
302 pin
, pctl
->devdata
->port_align
, isup
, r1r0
);
307 /* For generic pull config, default arg value should be 0 or 1. */
308 if (arg
!= 0 && arg
!= 1) {
309 dev_err(pctl
->dev
, "invalid pull-up argument %d on pin %d .\n",
314 bit
= BIT(pin
& 0xf);
316 reg_pullen
= SET_ADDR(mtk_get_port(pctl
, pin
) +
317 pctl
->devdata
->pullen_offset
, pctl
);
319 reg_pullen
= CLR_ADDR(mtk_get_port(pctl
, pin
) +
320 pctl
->devdata
->pullen_offset
, pctl
);
323 reg_pullsel
= SET_ADDR(mtk_get_port(pctl
, pin
) +
324 pctl
->devdata
->pullsel_offset
, pctl
);
326 reg_pullsel
= CLR_ADDR(mtk_get_port(pctl
, pin
) +
327 pctl
->devdata
->pullsel_offset
, pctl
);
329 regmap_write(mtk_get_regmap(pctl
, pin
), reg_pullen
, bit
);
330 regmap_write(mtk_get_regmap(pctl
, pin
), reg_pullsel
, bit
);
334 static int mtk_pconf_parse_conf(struct pinctrl_dev
*pctldev
,
335 unsigned int pin
, enum pin_config_param param
,
336 enum pin_config_param arg
)
339 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
342 case PIN_CONFIG_BIAS_DISABLE
:
343 ret
= mtk_pconf_set_pull_select(pctl
, pin
, false, false, arg
);
345 case PIN_CONFIG_BIAS_PULL_UP
:
346 ret
= mtk_pconf_set_pull_select(pctl
, pin
, true, true, arg
);
348 case PIN_CONFIG_BIAS_PULL_DOWN
:
349 ret
= mtk_pconf_set_pull_select(pctl
, pin
, true, false, arg
);
351 case PIN_CONFIG_INPUT_ENABLE
:
352 mtk_pmx_gpio_set_direction(pctldev
, NULL
, pin
, true);
353 ret
= mtk_pconf_set_ies_smt(pctl
, pin
, arg
, param
);
355 case PIN_CONFIG_OUTPUT
:
356 mtk_gpio_set(pctl
->chip
, pin
, arg
);
357 ret
= mtk_pmx_gpio_set_direction(pctldev
, NULL
, pin
, false);
359 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
360 mtk_pmx_gpio_set_direction(pctldev
, NULL
, pin
, true);
361 ret
= mtk_pconf_set_ies_smt(pctl
, pin
, arg
, param
);
363 case PIN_CONFIG_DRIVE_STRENGTH
:
364 ret
= mtk_pconf_set_driving(pctl
, pin
, arg
);
373 static int mtk_pconf_group_get(struct pinctrl_dev
*pctldev
,
375 unsigned long *config
)
377 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
379 *config
= pctl
->groups
[group
].config
;
384 static int mtk_pconf_group_set(struct pinctrl_dev
*pctldev
, unsigned group
,
385 unsigned long *configs
, unsigned num_configs
)
387 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
388 struct mtk_pinctrl_group
*g
= &pctl
->groups
[group
];
391 for (i
= 0; i
< num_configs
; i
++) {
392 ret
= mtk_pconf_parse_conf(pctldev
, g
->pin
,
393 pinconf_to_config_param(configs
[i
]),
394 pinconf_to_config_argument(configs
[i
]));
398 g
->config
= configs
[i
];
404 static const struct pinconf_ops mtk_pconf_ops
= {
405 .pin_config_group_get
= mtk_pconf_group_get
,
406 .pin_config_group_set
= mtk_pconf_group_set
,
409 static struct mtk_pinctrl_group
*
410 mtk_pctrl_find_group_by_pin(struct mtk_pinctrl
*pctl
, u32 pin
)
414 for (i
= 0; i
< pctl
->ngroups
; i
++) {
415 struct mtk_pinctrl_group
*grp
= pctl
->groups
+ i
;
424 static const struct mtk_desc_function
*mtk_pctrl_find_function_by_pin(
425 struct mtk_pinctrl
*pctl
, u32 pin_num
, u32 fnum
)
427 const struct mtk_desc_pin
*pin
= pctl
->devdata
->pins
+ pin_num
;
428 const struct mtk_desc_function
*func
= pin
->functions
;
430 while (func
&& func
->name
) {
431 if (func
->muxval
== fnum
)
439 static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl
*pctl
,
440 u32 pin_num
, u32 fnum
)
444 for (i
= 0; i
< pctl
->devdata
->npins
; i
++) {
445 const struct mtk_desc_pin
*pin
= pctl
->devdata
->pins
+ i
;
447 if (pin
->pin
.number
== pin_num
) {
448 const struct mtk_desc_function
*func
=
451 while (func
&& func
->name
) {
452 if (func
->muxval
== fnum
)
464 static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl
*pctl
,
465 u32 pin
, u32 fnum
, struct mtk_pinctrl_group
*grp
,
466 struct pinctrl_map
**map
, unsigned *reserved_maps
,
471 if (*num_maps
== *reserved_maps
)
474 (*map
)[*num_maps
].type
= PIN_MAP_TYPE_MUX_GROUP
;
475 (*map
)[*num_maps
].data
.mux
.group
= grp
->name
;
477 ret
= mtk_pctrl_is_function_valid(pctl
, pin
, fnum
);
479 dev_err(pctl
->dev
, "invalid function %d on pin %d .\n",
484 (*map
)[*num_maps
].data
.mux
.function
= mtk_gpio_functions
[fnum
];
490 static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
491 struct device_node
*node
,
492 struct pinctrl_map
**map
,
493 unsigned *reserved_maps
,
496 struct property
*pins
;
497 u32 pinfunc
, pin
, func
;
498 int num_pins
, num_funcs
, maps_per_pin
;
499 unsigned long *configs
;
500 unsigned int num_configs
;
501 bool has_config
= false;
503 unsigned reserve
= 0;
504 struct mtk_pinctrl_group
*grp
;
505 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
507 pins
= of_find_property(node
, "pinmux", NULL
);
509 dev_err(pctl
->dev
, "missing pins property in node %pOFn .\n",
514 err
= pinconf_generic_parse_dt_config(node
, pctldev
, &configs
,
522 num_pins
= pins
->length
/ sizeof(u32
);
523 num_funcs
= num_pins
;
527 if (has_config
&& num_pins
>= 1)
530 if (!num_pins
|| !maps_per_pin
) {
535 reserve
= num_pins
* maps_per_pin
;
537 err
= pinctrl_utils_reserve_map(pctldev
, map
,
538 reserved_maps
, num_maps
, reserve
);
542 for (i
= 0; i
< num_pins
; i
++) {
543 err
= of_property_read_u32_index(node
, "pinmux",
548 pin
= MTK_GET_PIN_NO(pinfunc
);
549 func
= MTK_GET_PIN_FUNC(pinfunc
);
551 if (pin
>= pctl
->devdata
->npins
||
552 func
>= ARRAY_SIZE(mtk_gpio_functions
)) {
553 dev_err(pctl
->dev
, "invalid pins value.\n");
558 grp
= mtk_pctrl_find_group_by_pin(pctl
, pin
);
560 dev_err(pctl
->dev
, "unable to match pin %d to group\n",
566 err
= mtk_pctrl_dt_node_to_map_func(pctl
, pin
, func
, grp
, map
,
567 reserved_maps
, num_maps
);
572 err
= pinctrl_utils_add_map_configs(pctldev
, map
,
573 reserved_maps
, num_maps
, grp
->name
,
574 configs
, num_configs
,
575 PIN_MAP_TYPE_CONFIGS_GROUP
);
588 static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
589 struct device_node
*np_config
,
590 struct pinctrl_map
**map
, unsigned *num_maps
)
592 struct device_node
*np
;
593 unsigned reserved_maps
;
600 for_each_child_of_node(np_config
, np
) {
601 ret
= mtk_pctrl_dt_subnode_to_map(pctldev
, np
, map
,
602 &reserved_maps
, num_maps
);
604 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
613 static int mtk_pctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
615 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
617 return pctl
->ngroups
;
620 static const char *mtk_pctrl_get_group_name(struct pinctrl_dev
*pctldev
,
623 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
625 return pctl
->groups
[group
].name
;
628 static int mtk_pctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
630 const unsigned **pins
,
633 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
635 *pins
= (unsigned *)&pctl
->groups
[group
].pin
;
641 static const struct pinctrl_ops mtk_pctrl_ops
= {
642 .dt_node_to_map
= mtk_pctrl_dt_node_to_map
,
643 .dt_free_map
= pinctrl_utils_free_map
,
644 .get_groups_count
= mtk_pctrl_get_groups_count
,
645 .get_group_name
= mtk_pctrl_get_group_name
,
646 .get_group_pins
= mtk_pctrl_get_group_pins
,
649 static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev
*pctldev
)
651 return ARRAY_SIZE(mtk_gpio_functions
);
654 static const char *mtk_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
657 return mtk_gpio_functions
[selector
];
660 static int mtk_pmx_get_func_groups(struct pinctrl_dev
*pctldev
,
662 const char * const **groups
,
663 unsigned * const num_groups
)
665 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
667 *groups
= pctl
->grp_names
;
668 *num_groups
= pctl
->ngroups
;
673 static int mtk_pmx_set_mode(struct pinctrl_dev
*pctldev
,
674 unsigned long pin
, unsigned long mode
)
676 unsigned int reg_addr
;
679 unsigned int mask
= (1L << GPIO_MODE_BITS
) - 1;
680 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
682 if (pctl
->devdata
->spec_pinmux_set
)
683 pctl
->devdata
->spec_pinmux_set(mtk_get_regmap(pctl
, pin
),
686 reg_addr
= ((pin
/ MAX_GPIO_MODE_PER_REG
) << pctl
->devdata
->port_shf
)
687 + pctl
->devdata
->pinmux_offset
;
690 bit
= pin
% MAX_GPIO_MODE_PER_REG
;
691 mask
<<= (GPIO_MODE_BITS
* bit
);
692 val
= (mode
<< (GPIO_MODE_BITS
* bit
));
693 return regmap_update_bits(mtk_get_regmap(pctl
, pin
),
694 reg_addr
, mask
, val
);
697 static const struct mtk_desc_pin
*
698 mtk_find_pin_by_eint_num(struct mtk_pinctrl
*pctl
, unsigned int eint_num
)
701 const struct mtk_desc_pin
*pin
;
703 for (i
= 0; i
< pctl
->devdata
->npins
; i
++) {
704 pin
= pctl
->devdata
->pins
+ i
;
705 if (pin
->eint
.eintnum
== eint_num
)
712 static int mtk_pmx_set_mux(struct pinctrl_dev
*pctldev
,
717 const struct mtk_desc_function
*desc
;
718 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
719 struct mtk_pinctrl_group
*g
= pctl
->groups
+ group
;
721 ret
= mtk_pctrl_is_function_valid(pctl
, g
->pin
, function
);
723 dev_err(pctl
->dev
, "invalid function %d on group %d .\n",
728 desc
= mtk_pctrl_find_function_by_pin(pctl
, g
->pin
, function
);
731 mtk_pmx_set_mode(pctldev
, g
->pin
, desc
->muxval
);
735 static int mtk_pmx_find_gpio_mode(struct mtk_pinctrl
*pctl
,
738 const struct mtk_desc_pin
*pin
= pctl
->devdata
->pins
+ offset
;
739 const struct mtk_desc_function
*func
= pin
->functions
;
741 while (func
&& func
->name
) {
742 if (!strncmp(func
->name
, GPIO_MODE_PREFIX
,
743 sizeof(GPIO_MODE_PREFIX
)-1))
750 static int mtk_pmx_gpio_request_enable(struct pinctrl_dev
*pctldev
,
751 struct pinctrl_gpio_range
*range
,
755 struct mtk_pinctrl
*pctl
= pinctrl_dev_get_drvdata(pctldev
);
757 muxval
= mtk_pmx_find_gpio_mode(pctl
, offset
);
760 dev_err(pctl
->dev
, "invalid gpio pin %d.\n", offset
);
764 mtk_pmx_set_mode(pctldev
, offset
, muxval
);
765 mtk_pconf_set_ies_smt(pctl
, offset
, 1, PIN_CONFIG_INPUT_ENABLE
);
770 static const struct pinmux_ops mtk_pmx_ops
= {
771 .get_functions_count
= mtk_pmx_get_funcs_cnt
,
772 .get_function_name
= mtk_pmx_get_func_name
,
773 .get_function_groups
= mtk_pmx_get_func_groups
,
774 .set_mux
= mtk_pmx_set_mux
,
775 .gpio_set_direction
= mtk_pmx_gpio_set_direction
,
776 .gpio_request_enable
= mtk_pmx_gpio_request_enable
,
779 static int mtk_gpio_direction_input(struct gpio_chip
*chip
,
782 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
785 static int mtk_gpio_direction_output(struct gpio_chip
*chip
,
786 unsigned offset
, int value
)
788 mtk_gpio_set(chip
, offset
, value
);
789 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
792 static int mtk_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
794 unsigned int reg_addr
;
796 unsigned int read_val
= 0;
798 struct mtk_pinctrl
*pctl
= gpiochip_get_data(chip
);
800 reg_addr
= mtk_get_port(pctl
, offset
) + pctl
->devdata
->dir_offset
;
801 bit
= BIT(offset
& 0xf);
803 if (pctl
->devdata
->spec_dir_set
)
804 pctl
->devdata
->spec_dir_set(®_addr
, offset
);
806 regmap_read(pctl
->regmap1
, reg_addr
, &read_val
);
807 return !(read_val
& bit
);
810 static int mtk_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
812 unsigned int reg_addr
;
814 unsigned int read_val
= 0;
815 struct mtk_pinctrl
*pctl
= gpiochip_get_data(chip
);
817 reg_addr
= mtk_get_port(pctl
, offset
) +
818 pctl
->devdata
->din_offset
;
820 bit
= BIT(offset
& 0xf);
821 regmap_read(pctl
->regmap1
, reg_addr
, &read_val
);
822 return !!(read_val
& bit
);
825 static int mtk_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
827 struct mtk_pinctrl
*pctl
= gpiochip_get_data(chip
);
828 const struct mtk_desc_pin
*pin
;
829 unsigned long eint_n
;
831 pin
= pctl
->devdata
->pins
+ offset
;
832 if (pin
->eint
.eintnum
== NO_EINT_SUPPORT
)
835 eint_n
= pin
->eint
.eintnum
;
837 return mtk_eint_find_irq(pctl
->eint
, eint_n
);
840 static int mtk_gpio_set_config(struct gpio_chip
*chip
, unsigned offset
,
841 unsigned long config
)
843 struct mtk_pinctrl
*pctl
= gpiochip_get_data(chip
);
844 const struct mtk_desc_pin
*pin
;
845 unsigned long eint_n
;
848 if (pinconf_to_config_param(config
) != PIN_CONFIG_INPUT_DEBOUNCE
)
851 pin
= pctl
->devdata
->pins
+ offset
;
852 if (pin
->eint
.eintnum
== NO_EINT_SUPPORT
)
855 debounce
= pinconf_to_config_argument(config
);
856 eint_n
= pin
->eint
.eintnum
;
858 return mtk_eint_set_debounce(pctl
->eint
, eint_n
, debounce
);
861 static const struct gpio_chip mtk_gpio_chip
= {
862 .owner
= THIS_MODULE
,
863 .request
= gpiochip_generic_request
,
864 .free
= gpiochip_generic_free
,
865 .get_direction
= mtk_gpio_get_direction
,
866 .direction_input
= mtk_gpio_direction_input
,
867 .direction_output
= mtk_gpio_direction_output
,
870 .to_irq
= mtk_gpio_to_irq
,
871 .set_config
= mtk_gpio_set_config
,
872 .of_gpio_n_cells
= 2,
875 static int mtk_eint_suspend(struct device
*device
)
877 struct mtk_pinctrl
*pctl
= dev_get_drvdata(device
);
879 return mtk_eint_do_suspend(pctl
->eint
);
882 static int mtk_eint_resume(struct device
*device
)
884 struct mtk_pinctrl
*pctl
= dev_get_drvdata(device
);
886 return mtk_eint_do_resume(pctl
->eint
);
889 const struct dev_pm_ops mtk_eint_pm_ops
= {
890 .suspend_noirq
= mtk_eint_suspend
,
891 .resume_noirq
= mtk_eint_resume
,
894 static int mtk_pctrl_build_state(struct platform_device
*pdev
)
896 struct mtk_pinctrl
*pctl
= platform_get_drvdata(pdev
);
899 pctl
->ngroups
= pctl
->devdata
->npins
;
901 /* Allocate groups */
902 pctl
->groups
= devm_kcalloc(&pdev
->dev
, pctl
->ngroups
,
903 sizeof(*pctl
->groups
), GFP_KERNEL
);
907 /* We assume that one pin is one group, use pin name as group name. */
908 pctl
->grp_names
= devm_kcalloc(&pdev
->dev
, pctl
->ngroups
,
909 sizeof(*pctl
->grp_names
), GFP_KERNEL
);
910 if (!pctl
->grp_names
)
913 for (i
= 0; i
< pctl
->devdata
->npins
; i
++) {
914 const struct mtk_desc_pin
*pin
= pctl
->devdata
->pins
+ i
;
915 struct mtk_pinctrl_group
*group
= pctl
->groups
+ i
;
917 group
->name
= pin
->pin
.name
;
918 group
->pin
= pin
->pin
.number
;
920 pctl
->grp_names
[i
] = pin
->pin
.name
;
927 mtk_xt_get_gpio_n(void *data
, unsigned long eint_n
, unsigned int *gpio_n
,
928 struct gpio_chip
**gpio_chip
)
930 struct mtk_pinctrl
*pctl
= (struct mtk_pinctrl
*)data
;
931 const struct mtk_desc_pin
*pin
;
933 pin
= mtk_find_pin_by_eint_num(pctl
, eint_n
);
937 *gpio_chip
= pctl
->chip
;
938 *gpio_n
= pin
->pin
.number
;
943 static int mtk_xt_get_gpio_state(void *data
, unsigned long eint_n
)
945 struct mtk_pinctrl
*pctl
= (struct mtk_pinctrl
*)data
;
946 const struct mtk_desc_pin
*pin
;
948 pin
= mtk_find_pin_by_eint_num(pctl
, eint_n
);
952 return mtk_gpio_get(pctl
->chip
, pin
->pin
.number
);
955 static int mtk_xt_set_gpio_as_eint(void *data
, unsigned long eint_n
)
957 struct mtk_pinctrl
*pctl
= (struct mtk_pinctrl
*)data
;
958 const struct mtk_desc_pin
*pin
;
960 pin
= mtk_find_pin_by_eint_num(pctl
, eint_n
);
964 /* set mux to INT mode */
965 mtk_pmx_set_mode(pctl
->pctl_dev
, pin
->pin
.number
, pin
->eint
.eintmux
);
966 /* set gpio direction to input */
967 mtk_pmx_gpio_set_direction(pctl
->pctl_dev
, NULL
, pin
->pin
.number
,
969 /* set input-enable */
970 mtk_pconf_set_ies_smt(pctl
, pin
->pin
.number
, 1,
971 PIN_CONFIG_INPUT_ENABLE
);
976 static const struct mtk_eint_xt mtk_eint_xt
= {
977 .get_gpio_n
= mtk_xt_get_gpio_n
,
978 .get_gpio_state
= mtk_xt_get_gpio_state
,
979 .set_gpio_as_eint
= mtk_xt_set_gpio_as_eint
,
982 static int mtk_eint_init(struct mtk_pinctrl
*pctl
, struct platform_device
*pdev
)
984 struct device_node
*np
= pdev
->dev
.of_node
;
986 if (!of_property_read_bool(np
, "interrupt-controller"))
989 pctl
->eint
= devm_kzalloc(pctl
->dev
, sizeof(*pctl
->eint
), GFP_KERNEL
);
993 pctl
->eint
->base
= devm_platform_ioremap_resource(pdev
, 0);
994 if (IS_ERR(pctl
->eint
->base
))
995 return PTR_ERR(pctl
->eint
->base
);
997 pctl
->eint
->irq
= irq_of_parse_and_map(np
, 0);
998 if (!pctl
->eint
->irq
)
1001 pctl
->eint
->dev
= &pdev
->dev
;
1003 * If pctl->eint->regs == NULL, it would fall back into using a generic
1004 * register map in mtk_eint_do_init calls.
1006 pctl
->eint
->regs
= pctl
->devdata
->eint_regs
;
1007 pctl
->eint
->hw
= &pctl
->devdata
->eint_hw
;
1008 pctl
->eint
->pctl
= pctl
;
1009 pctl
->eint
->gpio_xlate
= &mtk_eint_xt
;
1011 return mtk_eint_do_init(pctl
->eint
);
1014 int mtk_pctrl_init(struct platform_device
*pdev
,
1015 const struct mtk_pinctrl_devdata
*data
,
1016 struct regmap
*regmap
)
1018 struct pinctrl_pin_desc
*pins
;
1019 struct mtk_pinctrl
*pctl
;
1020 struct device_node
*np
= pdev
->dev
.of_node
, *node
;
1021 struct property
*prop
;
1024 pctl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
), GFP_KERNEL
);
1028 platform_set_drvdata(pdev
, pctl
);
1030 prop
= of_find_property(np
, "pins-are-numbered", NULL
);
1032 dev_err(&pdev
->dev
, "only support pins-are-numbered format\n");
1036 node
= of_parse_phandle(np
, "mediatek,pctl-regmap", 0);
1038 pctl
->regmap1
= syscon_node_to_regmap(node
);
1039 if (IS_ERR(pctl
->regmap1
))
1040 return PTR_ERR(pctl
->regmap1
);
1041 } else if (regmap
) {
1042 pctl
->regmap1
= regmap
;
1044 dev_err(&pdev
->dev
, "Pinctrl node has not register regmap.\n");
1048 /* Only 8135 has two base addr, other SoCs have only one. */
1049 node
= of_parse_phandle(np
, "mediatek,pctl-regmap", 1);
1051 pctl
->regmap2
= syscon_node_to_regmap(node
);
1052 if (IS_ERR(pctl
->regmap2
))
1053 return PTR_ERR(pctl
->regmap2
);
1056 pctl
->devdata
= data
;
1057 ret
= mtk_pctrl_build_state(pdev
);
1059 dev_err(&pdev
->dev
, "build state failed: %d\n", ret
);
1063 pins
= devm_kcalloc(&pdev
->dev
, pctl
->devdata
->npins
, sizeof(*pins
),
1068 for (i
= 0; i
< pctl
->devdata
->npins
; i
++)
1069 pins
[i
] = pctl
->devdata
->pins
[i
].pin
;
1071 pctl
->pctl_desc
.name
= dev_name(&pdev
->dev
);
1072 pctl
->pctl_desc
.owner
= THIS_MODULE
;
1073 pctl
->pctl_desc
.pins
= pins
;
1074 pctl
->pctl_desc
.npins
= pctl
->devdata
->npins
;
1075 pctl
->pctl_desc
.confops
= &mtk_pconf_ops
;
1076 pctl
->pctl_desc
.pctlops
= &mtk_pctrl_ops
;
1077 pctl
->pctl_desc
.pmxops
= &mtk_pmx_ops
;
1078 pctl
->dev
= &pdev
->dev
;
1080 pctl
->pctl_dev
= devm_pinctrl_register(&pdev
->dev
, &pctl
->pctl_desc
,
1082 if (IS_ERR(pctl
->pctl_dev
)) {
1083 dev_err(&pdev
->dev
, "couldn't register pinctrl driver\n");
1084 return PTR_ERR(pctl
->pctl_dev
);
1087 pctl
->chip
= devm_kzalloc(&pdev
->dev
, sizeof(*pctl
->chip
), GFP_KERNEL
);
1091 *pctl
->chip
= mtk_gpio_chip
;
1092 pctl
->chip
->ngpio
= pctl
->devdata
->npins
;
1093 pctl
->chip
->label
= dev_name(&pdev
->dev
);
1094 pctl
->chip
->parent
= &pdev
->dev
;
1095 pctl
->chip
->base
= -1;
1097 ret
= gpiochip_add_data(pctl
->chip
, pctl
);
1101 /* Register the GPIO to pin mappings. */
1102 ret
= gpiochip_add_pin_range(pctl
->chip
, dev_name(&pdev
->dev
),
1103 0, 0, pctl
->devdata
->npins
);
1109 ret
= mtk_eint_init(pctl
, pdev
);
1116 gpiochip_remove(pctl
->chip
);