1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Pinctrl driver for Rockchip RK805/RK806 PMIC
5 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
6 * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
8 * Author: Joseph Chen <chenjh@rock-chips.com>
9 * Author: Xu Shengfei <xsf@rock-chips.com>
11 * Based on the pinctrl-as3722 driver
14 #include <linux/gpio/driver.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mfd/rk808.h>
18 #include <linux/platform_device.h>
20 #include <linux/property.h>
21 #include <linux/slab.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pinctrl/machine.h>
25 #include <linux/pinctrl/pinctrl.h>
26 #include <linux/pinctrl/pinconf-generic.h>
27 #include <linux/pinctrl/pinconf.h>
28 #include <linux/pinctrl/pinmux.h>
32 #include "pinctrl-utils.h"
34 struct rk805_pin_function
{
36 const char *const *groups
;
41 struct rk805_pin_group
{
43 const unsigned int pins
[1];
48 * @reg: gpio setting register;
49 * @fun_reg: functions select register;
50 * @fun_mask: functions select mask value, when set is gpio;
51 * @dir_mask: input or output mask value, when set is output, otherwise input;
52 * @val_mask: gpio set value, when set is level high, otherwise low;
54 * Different PMIC has different pin features, belowing 3 mask members are not
55 * all necessary for every PMIC. For example, RK805 has 2 pins that can be used
56 * as output only GPIOs, so func_mask and dir_mask are not needed. RK816 has 1
57 * pin that can be used as TS/GPIO, so fun_mask, dir_mask and val_mask are all
60 struct rk805_pin_config
{
68 struct rk805_pctrl_info
{
71 struct pinctrl_dev
*pctl
;
72 struct gpio_chip gpio_chip
;
73 struct pinctrl_desc pinctrl_desc
;
74 const struct rk805_pin_function
*functions
;
75 unsigned int num_functions
;
76 const struct rk805_pin_group
*groups
;
78 const struct pinctrl_pin_desc
*pins
;
79 unsigned int num_pins
;
80 const struct rk805_pin_config
*pin_cfg
;
83 enum rk805_pinmux_option
{
87 enum rk806_pinmux_option
{
88 RK806_PINMUX_FUN0
= 0,
96 enum rk816_pinmux_option
{
97 RK816_PINMUX_THERMISTOR
,
116 static const char *const rk805_gpio_groups
[] = {
121 static const char *const rk806_gpio_groups
[] = {
127 static const char *const rk816_gpio_groups
[] = {
131 /* RK805: 2 output only GPIOs */
132 static const struct pinctrl_pin_desc rk805_pins_desc
[] = {
133 PINCTRL_PIN(RK805_GPIO0
, "gpio0"),
134 PINCTRL_PIN(RK805_GPIO1
, "gpio1"),
138 static const struct pinctrl_pin_desc rk806_pins_desc
[] = {
139 PINCTRL_PIN(RK806_GPIO_DVS1
, "gpio_pwrctrl1"),
140 PINCTRL_PIN(RK806_GPIO_DVS2
, "gpio_pwrctrl2"),
141 PINCTRL_PIN(RK806_GPIO_DVS3
, "gpio_pwrctrl3"),
145 static const struct pinctrl_pin_desc rk816_pins_desc
[] = {
146 PINCTRL_PIN(RK816_GPIO0
, "gpio0"),
149 static const struct rk805_pin_function rk805_pin_functions
[] = {
152 .groups
= rk805_gpio_groups
,
153 .ngroups
= ARRAY_SIZE(rk805_gpio_groups
),
154 .mux_option
= RK805_PINMUX_GPIO
,
158 static const struct rk805_pin_function rk806_pin_functions
[] = {
161 .groups
= rk806_gpio_groups
,
162 .ngroups
= ARRAY_SIZE(rk806_gpio_groups
),
163 .mux_option
= RK806_PINMUX_FUN0
,
167 .groups
= rk806_gpio_groups
,
168 .ngroups
= ARRAY_SIZE(rk806_gpio_groups
),
169 .mux_option
= RK806_PINMUX_FUN1
,
173 .groups
= rk806_gpio_groups
,
174 .ngroups
= ARRAY_SIZE(rk806_gpio_groups
),
175 .mux_option
= RK806_PINMUX_FUN2
,
179 .groups
= rk806_gpio_groups
,
180 .ngroups
= ARRAY_SIZE(rk806_gpio_groups
),
181 .mux_option
= RK806_PINMUX_FUN3
,
185 .groups
= rk806_gpio_groups
,
186 .ngroups
= ARRAY_SIZE(rk806_gpio_groups
),
187 .mux_option
= RK806_PINMUX_FUN4
,
191 .groups
= rk806_gpio_groups
,
192 .ngroups
= ARRAY_SIZE(rk806_gpio_groups
),
193 .mux_option
= RK806_PINMUX_FUN5
,
197 static const struct rk805_pin_function rk816_pin_functions
[] = {
200 .groups
= rk816_gpio_groups
,
201 .ngroups
= ARRAY_SIZE(rk816_gpio_groups
),
202 .mux_option
= RK816_PINMUX_GPIO
,
205 .name
= "thermistor",
206 .groups
= rk816_gpio_groups
,
207 .ngroups
= ARRAY_SIZE(rk816_gpio_groups
),
208 .mux_option
= RK816_PINMUX_THERMISTOR
,
212 static const struct rk805_pin_group rk805_pin_groups
[] = {
215 .pins
= { RK805_GPIO0
},
220 .pins
= { RK805_GPIO1
},
225 static const struct rk805_pin_group rk806_pin_groups
[] = {
227 .name
= "gpio_pwrctrl1",
228 .pins
= { RK806_GPIO_DVS1
},
232 .name
= "gpio_pwrctrl2",
233 .pins
= { RK806_GPIO_DVS2
},
237 .name
= "gpio_pwrctrl3",
238 .pins
= { RK806_GPIO_DVS3
},
243 static const struct rk805_pin_group rk816_pin_groups
[] = {
246 .pins
= { RK816_GPIO0
},
251 #define RK805_GPIO0_VAL_MSK BIT(0)
252 #define RK805_GPIO1_VAL_MSK BIT(1)
254 static const struct rk805_pin_config rk805_gpio_cfgs
[] = {
256 .reg
= RK805_OUT_REG
,
257 .val_msk
= RK805_GPIO0_VAL_MSK
,
260 .reg
= RK805_OUT_REG
,
261 .val_msk
= RK805_GPIO1_VAL_MSK
,
265 #define RK806_PWRCTRL1_DR BIT(0)
266 #define RK806_PWRCTRL2_DR BIT(1)
267 #define RK806_PWRCTRL3_DR BIT(2)
268 #define RK806_PWRCTRL1_DATA BIT(4)
269 #define RK806_PWRCTRL2_DATA BIT(5)
270 #define RK806_PWRCTRL3_DATA BIT(6)
271 #define RK806_PWRCTRL1_FUN GENMASK(2, 0)
272 #define RK806_PWRCTRL2_FUN GENMASK(6, 4)
273 #define RK806_PWRCTRL3_FUN GENMASK(2, 0)
275 static struct rk805_pin_config rk806_gpio_cfgs
[] = {
277 .fun_reg
= RK806_SLEEP_CONFIG0
,
278 .fun_msk
= RK806_PWRCTRL1_FUN
,
279 .reg
= RK806_SLEEP_GPIO
,
280 .val_msk
= RK806_PWRCTRL1_DATA
,
281 .dir_msk
= RK806_PWRCTRL1_DR
,
284 .fun_reg
= RK806_SLEEP_CONFIG0
,
285 .fun_msk
= RK806_PWRCTRL2_FUN
,
286 .reg
= RK806_SLEEP_GPIO
,
287 .val_msk
= RK806_PWRCTRL2_DATA
,
288 .dir_msk
= RK806_PWRCTRL2_DR
,
291 .fun_reg
= RK806_SLEEP_CONFIG1
,
292 .fun_msk
= RK806_PWRCTRL3_FUN
,
293 .reg
= RK806_SLEEP_GPIO
,
294 .val_msk
= RK806_PWRCTRL3_DATA
,
295 .dir_msk
= RK806_PWRCTRL3_DR
,
299 #define RK816_FUN_MASK BIT(2)
300 #define RK816_VAL_MASK BIT(3)
301 #define RK816_DIR_MASK BIT(4)
303 static struct rk805_pin_config rk816_gpio_cfgs
[] = {
305 .fun_reg
= RK818_IO_POL_REG
,
306 .fun_msk
= RK816_FUN_MASK
,
307 .reg
= RK818_IO_POL_REG
,
308 .val_msk
= RK816_VAL_MASK
,
309 .dir_msk
= RK816_DIR_MASK
,
313 /* generic gpio chip */
314 static int rk805_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
316 struct rk805_pctrl_info
*pci
= gpiochip_get_data(chip
);
319 ret
= regmap_read(pci
->rk808
->regmap
, pci
->pin_cfg
[offset
].reg
, &val
);
321 dev_err(pci
->dev
, "get gpio%d value failed\n", offset
);
325 return !!(val
& pci
->pin_cfg
[offset
].val_msk
);
328 static void rk805_gpio_set(struct gpio_chip
*chip
,
332 struct rk805_pctrl_info
*pci
= gpiochip_get_data(chip
);
335 ret
= regmap_update_bits(pci
->rk808
->regmap
,
336 pci
->pin_cfg
[offset
].reg
,
337 pci
->pin_cfg
[offset
].val_msk
,
338 value
? pci
->pin_cfg
[offset
].val_msk
: 0);
340 dev_err(pci
->dev
, "set gpio%d value %d failed\n",
344 static int rk805_gpio_direction_output(struct gpio_chip
*chip
,
345 unsigned int offset
, int value
)
347 rk805_gpio_set(chip
, offset
, value
);
348 return pinctrl_gpio_direction_output(chip
, offset
);
351 static int rk805_gpio_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
353 struct rk805_pctrl_info
*pci
= gpiochip_get_data(chip
);
358 if (!pci
->pin_cfg
[offset
].dir_msk
)
359 return GPIO_LINE_DIRECTION_OUT
;
361 ret
= regmap_read(pci
->rk808
->regmap
,
362 pci
->pin_cfg
[offset
].reg
,
365 dev_err(pci
->dev
, "get gpio%d direction failed\n", offset
);
369 if (val
& pci
->pin_cfg
[offset
].dir_msk
)
370 return GPIO_LINE_DIRECTION_OUT
;
372 return GPIO_LINE_DIRECTION_IN
;
375 static const struct gpio_chip rk805_gpio_chip
= {
376 .label
= "rk805-gpio",
377 .request
= gpiochip_generic_request
,
378 .free
= gpiochip_generic_free
,
379 .get_direction
= rk805_gpio_get_direction
,
380 .get
= rk805_gpio_get
,
381 .set
= rk805_gpio_set
,
382 .direction_input
= pinctrl_gpio_direction_input
,
383 .direction_output
= rk805_gpio_direction_output
,
386 .owner
= THIS_MODULE
,
389 /* generic pinctrl */
390 static int rk805_pinctrl_get_groups_count(struct pinctrl_dev
*pctldev
)
392 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
394 return pci
->num_pin_groups
;
397 static const char *rk805_pinctrl_get_group_name(struct pinctrl_dev
*pctldev
,
400 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
402 return pci
->groups
[group
].name
;
405 static int rk805_pinctrl_get_group_pins(struct pinctrl_dev
*pctldev
,
407 const unsigned int **pins
,
408 unsigned int *num_pins
)
410 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
412 *pins
= pci
->groups
[group
].pins
;
413 *num_pins
= pci
->groups
[group
].npins
;
418 static const struct pinctrl_ops rk805_pinctrl_ops
= {
419 .get_groups_count
= rk805_pinctrl_get_groups_count
,
420 .get_group_name
= rk805_pinctrl_get_group_name
,
421 .get_group_pins
= rk805_pinctrl_get_group_pins
,
422 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
423 .dt_free_map
= pinctrl_utils_free_map
,
426 static int rk805_pinctrl_get_funcs_count(struct pinctrl_dev
*pctldev
)
428 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
430 return pci
->num_functions
;
433 static const char *rk805_pinctrl_get_func_name(struct pinctrl_dev
*pctldev
,
434 unsigned int function
)
436 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
438 return pci
->functions
[function
].name
;
441 static int rk805_pinctrl_get_func_groups(struct pinctrl_dev
*pctldev
,
442 unsigned int function
,
443 const char *const **groups
,
444 unsigned int *const num_groups
)
446 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
448 *groups
= pci
->functions
[function
].groups
;
449 *num_groups
= pci
->functions
[function
].ngroups
;
454 static int _rk805_pinctrl_set_mux(struct pinctrl_dev
*pctldev
,
458 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
461 if (!pci
->pin_cfg
[offset
].fun_msk
)
464 mux
<<= ffs(pci
->pin_cfg
[offset
].fun_msk
) - 1;
465 ret
= regmap_update_bits(pci
->rk808
->regmap
,
466 pci
->pin_cfg
[offset
].fun_reg
,
467 pci
->pin_cfg
[offset
].fun_msk
, mux
);
470 dev_err(pci
->dev
, "set gpio%d func%d failed\n", offset
, mux
);
475 static int rk805_pinctrl_set_mux(struct pinctrl_dev
*pctldev
,
476 unsigned int function
,
479 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
480 int mux
= pci
->functions
[function
].mux_option
;
483 return _rk805_pinctrl_set_mux(pctldev
, offset
, mux
);
486 static int rk805_pinctrl_gpio_request_enable(struct pinctrl_dev
*pctldev
,
487 struct pinctrl_gpio_range
*range
,
490 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
492 switch (pci
->rk808
->variant
) {
494 return _rk805_pinctrl_set_mux(pctldev
, offset
, RK805_PINMUX_GPIO
);
496 return _rk805_pinctrl_set_mux(pctldev
, offset
, RK806_PINMUX_FUN5
);
498 return _rk805_pinctrl_set_mux(pctldev
, offset
, RK816_PINMUX_GPIO
);
504 static int rk805_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
505 struct pinctrl_gpio_range
*range
,
506 unsigned int offset
, bool input
)
508 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
512 if (!pci
->pin_cfg
[offset
].dir_msk
)
515 ret
= regmap_update_bits(pci
->rk808
->regmap
,
516 pci
->pin_cfg
[offset
].reg
,
517 pci
->pin_cfg
[offset
].dir_msk
,
518 input
? 0 : pci
->pin_cfg
[offset
].dir_msk
);
520 dev_err(pci
->dev
, "set gpio%d direction failed\n", offset
);
527 static const struct pinmux_ops rk805_pinmux_ops
= {
528 .get_functions_count
= rk805_pinctrl_get_funcs_count
,
529 .get_function_name
= rk805_pinctrl_get_func_name
,
530 .get_function_groups
= rk805_pinctrl_get_func_groups
,
531 .set_mux
= rk805_pinctrl_set_mux
,
532 .gpio_request_enable
= rk805_pinctrl_gpio_request_enable
,
533 .gpio_set_direction
= rk805_pmx_gpio_set_direction
,
536 static int rk805_pinconf_get(struct pinctrl_dev
*pctldev
,
537 unsigned int pin
, unsigned long *config
)
539 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
540 enum pin_config_param param
= pinconf_to_config_param(*config
);
544 case PIN_CONFIG_OUTPUT
:
545 case PIN_CONFIG_INPUT_ENABLE
:
546 arg
= rk805_gpio_get(&pci
->gpio_chip
, pin
);
549 dev_err(pci
->dev
, "Properties not supported\n");
553 *config
= pinconf_to_config_packed(param
, (u16
)arg
);
558 static int rk805_pinconf_set(struct pinctrl_dev
*pctldev
,
559 unsigned int pin
, unsigned long *configs
,
560 unsigned int num_configs
)
562 struct rk805_pctrl_info
*pci
= pinctrl_dev_get_drvdata(pctldev
);
563 enum pin_config_param param
;
566 for (i
= 0; i
< num_configs
; i
++) {
567 param
= pinconf_to_config_param(configs
[i
]);
568 arg
= pinconf_to_config_argument(configs
[i
]);
571 case PIN_CONFIG_OUTPUT
:
572 rk805_gpio_set(&pci
->gpio_chip
, pin
, arg
);
573 rk805_pmx_gpio_set_direction(pctldev
, NULL
, pin
, false);
575 case PIN_CONFIG_INPUT_ENABLE
:
576 if (pci
->rk808
->variant
!= RK805_ID
&& arg
) {
577 rk805_pmx_gpio_set_direction(pctldev
, NULL
, pin
, true);
582 dev_err(pci
->dev
, "Properties not supported\n");
590 static const struct pinconf_ops rk805_pinconf_ops
= {
591 .pin_config_get
= rk805_pinconf_get
,
592 .pin_config_set
= rk805_pinconf_set
,
595 static const struct pinctrl_desc rk805_pinctrl_desc
= {
596 .name
= "rk805-pinctrl",
597 .pctlops
= &rk805_pinctrl_ops
,
598 .pmxops
= &rk805_pinmux_ops
,
599 .confops
= &rk805_pinconf_ops
,
600 .owner
= THIS_MODULE
,
603 static int rk805_pinctrl_probe(struct platform_device
*pdev
)
605 struct rk805_pctrl_info
*pci
;
608 device_set_node(&pdev
->dev
, dev_fwnode(pdev
->dev
.parent
));
610 pci
= devm_kzalloc(&pdev
->dev
, sizeof(*pci
), GFP_KERNEL
);
614 pci
->dev
= &pdev
->dev
;
615 pci
->rk808
= dev_get_drvdata(pdev
->dev
.parent
);
617 pci
->pinctrl_desc
= rk805_pinctrl_desc
;
618 pci
->gpio_chip
= rk805_gpio_chip
;
619 pci
->gpio_chip
.parent
= &pdev
->dev
;
621 platform_set_drvdata(pdev
, pci
);
623 switch (pci
->rk808
->variant
) {
625 pci
->pins
= rk805_pins_desc
;
626 pci
->num_pins
= ARRAY_SIZE(rk805_pins_desc
);
627 pci
->functions
= rk805_pin_functions
;
628 pci
->num_functions
= ARRAY_SIZE(rk805_pin_functions
);
629 pci
->groups
= rk805_pin_groups
;
630 pci
->num_pin_groups
= ARRAY_SIZE(rk805_pin_groups
);
631 pci
->pinctrl_desc
.pins
= rk805_pins_desc
;
632 pci
->pinctrl_desc
.npins
= ARRAY_SIZE(rk805_pins_desc
);
633 pci
->pin_cfg
= rk805_gpio_cfgs
;
634 pci
->gpio_chip
.ngpio
= ARRAY_SIZE(rk805_gpio_cfgs
);
637 pci
->pins
= rk806_pins_desc
;
638 pci
->num_pins
= ARRAY_SIZE(rk806_pins_desc
);
639 pci
->functions
= rk806_pin_functions
;
640 pci
->num_functions
= ARRAY_SIZE(rk806_pin_functions
);
641 pci
->groups
= rk806_pin_groups
;
642 pci
->num_pin_groups
= ARRAY_SIZE(rk806_pin_groups
);
643 pci
->pinctrl_desc
.pins
= rk806_pins_desc
;
644 pci
->pinctrl_desc
.npins
= ARRAY_SIZE(rk806_pins_desc
);
645 pci
->pin_cfg
= rk806_gpio_cfgs
;
646 pci
->gpio_chip
.ngpio
= ARRAY_SIZE(rk806_gpio_cfgs
);
649 pci
->pins
= rk816_pins_desc
;
650 pci
->num_pins
= ARRAY_SIZE(rk816_pins_desc
);
651 pci
->functions
= rk816_pin_functions
;
652 pci
->num_functions
= ARRAY_SIZE(rk816_pin_functions
);
653 pci
->groups
= rk816_pin_groups
;
654 pci
->num_pin_groups
= ARRAY_SIZE(rk816_pin_groups
);
655 pci
->pinctrl_desc
.pins
= rk816_pins_desc
;
656 pci
->pinctrl_desc
.npins
= ARRAY_SIZE(rk816_pins_desc
);
657 pci
->pin_cfg
= rk816_gpio_cfgs
;
658 pci
->gpio_chip
.ngpio
= ARRAY_SIZE(rk816_gpio_cfgs
);
661 dev_err(&pdev
->dev
, "unsupported RK805 ID %lu\n",
662 pci
->rk808
->variant
);
667 ret
= devm_gpiochip_add_data(&pdev
->dev
, &pci
->gpio_chip
, pci
);
669 dev_err(&pdev
->dev
, "Couldn't add gpiochip\n");
674 pci
->pctl
= devm_pinctrl_register(&pdev
->dev
, &pci
->pinctrl_desc
, pci
);
675 if (IS_ERR(pci
->pctl
)) {
676 dev_err(&pdev
->dev
, "Couldn't add pinctrl\n");
677 return PTR_ERR(pci
->pctl
);
681 ret
= gpiochip_add_pin_range(&pci
->gpio_chip
, dev_name(&pdev
->dev
),
682 0, 0, pci
->gpio_chip
.ngpio
);
684 dev_err(&pdev
->dev
, "Couldn't add gpiochip pin range\n");
691 static struct platform_driver rk805_pinctrl_driver
= {
692 .probe
= rk805_pinctrl_probe
,
694 .name
= "rk805-pinctrl",
697 module_platform_driver(rk805_pinctrl_driver
);
699 MODULE_DESCRIPTION("RK805 pin control and GPIO driver");
700 MODULE_AUTHOR("Xu Shengfei <xsf@rock-chips.com>");
701 MODULE_AUTHOR("Joseph Chen <chenjh@rock-chips.com>");
702 MODULE_LICENSE("GPL v2");