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,
107 static const char *const rk805_gpio_groups[] = {
112 static const char *const rk806_gpio_groups[] = {
118 /* RK805: 2 output only GPIOs */
119 static const struct pinctrl_pin_desc rk805_pins_desc[] = {
120 PINCTRL_PIN(RK805_GPIO0, "gpio0"),
121 PINCTRL_PIN(RK805_GPIO1, "gpio1"),
125 static const struct pinctrl_pin_desc rk806_pins_desc[] = {
126 PINCTRL_PIN(RK806_GPIO_DVS1, "gpio_pwrctrl1"),
127 PINCTRL_PIN(RK806_GPIO_DVS2, "gpio_pwrctrl2"),
128 PINCTRL_PIN(RK806_GPIO_DVS3, "gpio_pwrctrl3"),
131 static const struct rk805_pin_function rk805_pin_functions[] = {
134 .groups = rk805_gpio_groups,
135 .ngroups = ARRAY_SIZE(rk805_gpio_groups),
136 .mux_option = RK805_PINMUX_GPIO,
140 static const struct rk805_pin_function rk806_pin_functions[] = {
143 .groups = rk806_gpio_groups,
144 .ngroups = ARRAY_SIZE(rk806_gpio_groups),
145 .mux_option = RK806_PINMUX_FUN0,
149 .groups = rk806_gpio_groups,
150 .ngroups = ARRAY_SIZE(rk806_gpio_groups),
151 .mux_option = RK806_PINMUX_FUN1,
155 .groups = rk806_gpio_groups,
156 .ngroups = ARRAY_SIZE(rk806_gpio_groups),
157 .mux_option = RK806_PINMUX_FUN2,
161 .groups = rk806_gpio_groups,
162 .ngroups = ARRAY_SIZE(rk806_gpio_groups),
163 .mux_option = RK806_PINMUX_FUN3,
167 .groups = rk806_gpio_groups,
168 .ngroups = ARRAY_SIZE(rk806_gpio_groups),
169 .mux_option = RK806_PINMUX_FUN4,
173 .groups = rk806_gpio_groups,
174 .ngroups = ARRAY_SIZE(rk806_gpio_groups),
175 .mux_option = RK806_PINMUX_FUN5,
179 static const struct rk805_pin_group rk805_pin_groups[] = {
182 .pins = { RK805_GPIO0 },
187 .pins = { RK805_GPIO1 },
192 static const struct rk805_pin_group rk806_pin_groups[] = {
194 .name = "gpio_pwrctrl1",
195 .pins = { RK806_GPIO_DVS1 },
199 .name = "gpio_pwrctrl2",
200 .pins = { RK806_GPIO_DVS2 },
204 .name = "gpio_pwrctrl3",
205 .pins = { RK806_GPIO_DVS3 },
210 #define RK805_GPIO0_VAL_MSK BIT(0)
211 #define RK805_GPIO1_VAL_MSK BIT(1)
213 static const struct rk805_pin_config rk805_gpio_cfgs[] = {
215 .reg = RK805_OUT_REG,
216 .val_msk = RK805_GPIO0_VAL_MSK,
219 .reg = RK805_OUT_REG,
220 .val_msk = RK805_GPIO1_VAL_MSK,
224 #define RK806_PWRCTRL1_DR BIT(0)
225 #define RK806_PWRCTRL2_DR BIT(1)
226 #define RK806_PWRCTRL3_DR BIT(2)
227 #define RK806_PWRCTRL1_DATA BIT(4)
228 #define RK806_PWRCTRL2_DATA BIT(5)
229 #define RK806_PWRCTRL3_DATA BIT(6)
230 #define RK806_PWRCTRL1_FUN GENMASK(2, 0)
231 #define RK806_PWRCTRL2_FUN GENMASK(6, 4)
232 #define RK806_PWRCTRL3_FUN GENMASK(2, 0)
234 static struct rk805_pin_config rk806_gpio_cfgs[] = {
236 .fun_reg = RK806_SLEEP_CONFIG0,
237 .fun_msk = RK806_PWRCTRL1_FUN,
238 .reg = RK806_SLEEP_GPIO,
239 .val_msk = RK806_PWRCTRL1_DATA,
240 .dir_msk = RK806_PWRCTRL1_DR,
243 .fun_reg = RK806_SLEEP_CONFIG0,
244 .fun_msk = RK806_PWRCTRL2_FUN,
245 .reg = RK806_SLEEP_GPIO,
246 .val_msk = RK806_PWRCTRL2_DATA,
247 .dir_msk = RK806_PWRCTRL2_DR,
250 .fun_reg = RK806_SLEEP_CONFIG1,
251 .fun_msk = RK806_PWRCTRL3_FUN,
252 .reg = RK806_SLEEP_GPIO,
253 .val_msk = RK806_PWRCTRL3_DATA,
254 .dir_msk = RK806_PWRCTRL3_DR,
258 /* generic gpio chip */
259 static int rk805_gpio_get(struct gpio_chip *chip, unsigned int offset)
261 struct rk805_pctrl_info *pci = gpiochip_get_data(chip);
264 ret = regmap_read(pci->rk808->regmap, pci->pin_cfg[offset].reg, &val);
266 dev_err(pci->dev, "get gpio%d value failed\n", offset);
270 return !!(val & pci->pin_cfg[offset].val_msk);
273 static void rk805_gpio_set(struct gpio_chip *chip,
277 struct rk805_pctrl_info *pci = gpiochip_get_data(chip);
280 ret = regmap_update_bits(pci->rk808->regmap,
281 pci->pin_cfg[offset].reg,
282 pci->pin_cfg[offset].val_msk,
283 value ? pci->pin_cfg[offset].val_msk : 0);
285 dev_err(pci->dev, "set gpio%d value %d failed\n",
289 static int rk805_gpio_direction_input(struct gpio_chip *chip,
292 return pinctrl_gpio_direction_input(chip->base + offset);
295 static int rk805_gpio_direction_output(struct gpio_chip *chip,
296 unsigned int offset, int value)
298 rk805_gpio_set(chip, offset, value);
299 return pinctrl_gpio_direction_output(chip->base + offset);
302 static int rk805_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
304 struct rk805_pctrl_info *pci = gpiochip_get_data(chip);
309 if (!pci->pin_cfg[offset].dir_msk)
310 return GPIO_LINE_DIRECTION_OUT;
312 ret = regmap_read(pci->rk808->regmap,
313 pci->pin_cfg[offset].reg,
316 dev_err(pci->dev, "get gpio%d direction failed\n", offset);
320 if (val & pci->pin_cfg[offset].dir_msk)
321 return GPIO_LINE_DIRECTION_OUT;
323 return GPIO_LINE_DIRECTION_IN;
326 static const struct gpio_chip rk805_gpio_chip = {
327 .label = "rk805-gpio",
328 .request = gpiochip_generic_request,
329 .free = gpiochip_generic_free,
330 .get_direction = rk805_gpio_get_direction,
331 .get = rk805_gpio_get,
332 .set = rk805_gpio_set,
333 .direction_input = rk805_gpio_direction_input,
334 .direction_output = rk805_gpio_direction_output,
337 .owner = THIS_MODULE,
340 /* generic pinctrl */
341 static int rk805_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
343 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
345 return pci->num_pin_groups;
348 static const char *rk805_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
351 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
353 return pci->groups[group].name;
356 static int rk805_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
358 const unsigned int **pins,
359 unsigned int *num_pins)
361 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
363 *pins = pci->groups[group].pins;
364 *num_pins = pci->groups[group].npins;
369 static const struct pinctrl_ops rk805_pinctrl_ops = {
370 .get_groups_count = rk805_pinctrl_get_groups_count,
371 .get_group_name = rk805_pinctrl_get_group_name,
372 .get_group_pins = rk805_pinctrl_get_group_pins,
373 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
374 .dt_free_map = pinctrl_utils_free_map,
377 static int rk805_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
379 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
381 return pci->num_functions;
384 static const char *rk805_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
385 unsigned int function)
387 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
389 return pci->functions[function].name;
392 static int rk805_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
393 unsigned int function,
394 const char *const **groups,
395 unsigned int *const num_groups)
397 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
399 *groups = pci->functions[function].groups;
400 *num_groups = pci->functions[function].ngroups;
405 static int _rk805_pinctrl_set_mux(struct pinctrl_dev *pctldev,
409 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
412 if (!pci->pin_cfg[offset].fun_msk)
415 mux <<= ffs(pci->pin_cfg[offset].fun_msk) - 1;
416 ret = regmap_update_bits(pci->rk808->regmap,
417 pci->pin_cfg[offset].fun_reg,
418 pci->pin_cfg[offset].fun_msk, mux);
421 dev_err(pci->dev, "set gpio%d func%d failed\n", offset, mux);
426 static int rk805_pinctrl_set_mux(struct pinctrl_dev *pctldev,
427 unsigned int function,
430 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
431 int mux = pci->functions[function].mux_option;
434 return _rk805_pinctrl_set_mux(pctldev, offset, mux);
437 static int rk805_pinctrl_gpio_request_enable(struct pinctrl_dev *pctldev,
438 struct pinctrl_gpio_range *range,
441 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
443 switch (pci->rk808->variant) {
445 return _rk805_pinctrl_set_mux(pctldev, offset, RK805_PINMUX_GPIO);
447 return _rk805_pinctrl_set_mux(pctldev, offset, RK806_PINMUX_FUN5);
453 static int rk805_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
454 struct pinctrl_gpio_range *range,
455 unsigned int offset, bool input)
457 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
461 if (!pci->pin_cfg[offset].dir_msk)
464 ret = regmap_update_bits(pci->rk808->regmap,
465 pci->pin_cfg[offset].reg,
466 pci->pin_cfg[offset].dir_msk,
467 input ? 0 : pci->pin_cfg[offset].dir_msk);
469 dev_err(pci->dev, "set gpio%d direction failed\n", offset);
476 static const struct pinmux_ops rk805_pinmux_ops = {
477 .get_functions_count = rk805_pinctrl_get_funcs_count,
478 .get_function_name = rk805_pinctrl_get_func_name,
479 .get_function_groups = rk805_pinctrl_get_func_groups,
480 .set_mux = rk805_pinctrl_set_mux,
481 .gpio_request_enable = rk805_pinctrl_gpio_request_enable,
482 .gpio_set_direction = rk805_pmx_gpio_set_direction,
485 static int rk805_pinconf_get(struct pinctrl_dev *pctldev,
486 unsigned int pin, unsigned long *config)
488 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
489 enum pin_config_param param = pinconf_to_config_param(*config);
493 case PIN_CONFIG_OUTPUT:
494 case PIN_CONFIG_INPUT_ENABLE:
495 arg = rk805_gpio_get(&pci->gpio_chip, pin);
498 dev_err(pci->dev, "Properties not supported\n");
502 *config = pinconf_to_config_packed(param, (u16)arg);
507 static int rk805_pinconf_set(struct pinctrl_dev *pctldev,
508 unsigned int pin, unsigned long *configs,
509 unsigned int num_configs)
511 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
512 enum pin_config_param param;
515 for (i = 0; i < num_configs; i++) {
516 param = pinconf_to_config_param(configs[i]);
517 arg = pinconf_to_config_argument(configs[i]);
520 case PIN_CONFIG_OUTPUT:
521 rk805_gpio_set(&pci->gpio_chip, pin, arg);
522 rk805_pmx_gpio_set_direction(pctldev, NULL, pin, false);
524 case PIN_CONFIG_INPUT_ENABLE:
525 if (pci->rk808->variant != RK805_ID && arg) {
526 rk805_pmx_gpio_set_direction(pctldev, NULL, pin, true);
531 dev_err(pci->dev, "Properties not supported\n");
539 static const struct pinconf_ops rk805_pinconf_ops = {
540 .pin_config_get = rk805_pinconf_get,
541 .pin_config_set = rk805_pinconf_set,
544 static const struct pinctrl_desc rk805_pinctrl_desc = {
545 .name = "rk805-pinctrl",
546 .pctlops = &rk805_pinctrl_ops,
547 .pmxops = &rk805_pinmux_ops,
548 .confops = &rk805_pinconf_ops,
549 .owner = THIS_MODULE,
552 static int rk805_pinctrl_probe(struct platform_device *pdev)
554 struct rk805_pctrl_info *pci;
557 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
559 pci = devm_kzalloc(&pdev->dev, sizeof(*pci), GFP_KERNEL);
563 pci->dev = &pdev->dev;
564 pci->rk808 = dev_get_drvdata(pdev->dev.parent);
566 pci->pinctrl_desc = rk805_pinctrl_desc;
567 pci->gpio_chip = rk805_gpio_chip;
568 pci->gpio_chip.parent = &pdev->dev;
570 platform_set_drvdata(pdev, pci);
572 switch (pci->rk808->variant) {
574 pci->pins = rk805_pins_desc;
575 pci->num_pins = ARRAY_SIZE(rk805_pins_desc);
576 pci->functions = rk805_pin_functions;
577 pci->num_functions = ARRAY_SIZE(rk805_pin_functions);
578 pci->groups = rk805_pin_groups;
579 pci->num_pin_groups = ARRAY_SIZE(rk805_pin_groups);
580 pci->pinctrl_desc.pins = rk805_pins_desc;
581 pci->pinctrl_desc.npins = ARRAY_SIZE(rk805_pins_desc);
582 pci->pin_cfg = rk805_gpio_cfgs;
583 pci->gpio_chip.ngpio = ARRAY_SIZE(rk805_gpio_cfgs);
586 pci->pins = rk806_pins_desc;
587 pci->num_pins = ARRAY_SIZE(rk806_pins_desc);
588 pci->functions = rk806_pin_functions;
589 pci->num_functions = ARRAY_SIZE(rk806_pin_functions);
590 pci->groups = rk806_pin_groups;
591 pci->num_pin_groups = ARRAY_SIZE(rk806_pin_groups);
592 pci->pinctrl_desc.pins = rk806_pins_desc;
593 pci->pinctrl_desc.npins = ARRAY_SIZE(rk806_pins_desc);
594 pci->pin_cfg = rk806_gpio_cfgs;
595 pci->gpio_chip.ngpio = ARRAY_SIZE(rk806_gpio_cfgs);
598 dev_err(&pdev->dev, "unsupported RK805 ID %lu\n",
599 pci->rk808->variant);
604 ret = devm_gpiochip_add_data(&pdev->dev, &pci->gpio_chip, pci);
606 dev_err(&pdev->dev, "Couldn't add gpiochip\n");
611 pci->pctl = devm_pinctrl_register(&pdev->dev, &pci->pinctrl_desc, pci);
612 if (IS_ERR(pci->pctl)) {
613 dev_err(&pdev->dev, "Couldn't add pinctrl\n");
614 return PTR_ERR(pci->pctl);
618 ret = gpiochip_add_pin_range(&pci->gpio_chip, dev_name(&pdev->dev),
619 0, 0, pci->gpio_chip.ngpio);
621 dev_err(&pdev->dev, "Couldn't add gpiochip pin range\n");
628 static struct platform_driver rk805_pinctrl_driver = {
629 .probe = rk805_pinctrl_probe,
631 .name = "rk805-pinctrl",
634 module_platform_driver(rk805_pinctrl_driver);
636 MODULE_DESCRIPTION("RK805 pin control and GPIO driver");
637 MODULE_AUTHOR("Xu Shengfei <xsf@rock-chips.com>");
638 MODULE_AUTHOR("Joseph Chen <chenjh@rock-chips.com>");
639 MODULE_LICENSE("GPL v2");