1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Pinctrl driver for Rockchip RK805 PMIC
5 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
7 * Author: Joseph Chen <chenjh@rock-chips.com>
9 * Based on the pinctrl-as3722 driver
12 #include <linux/gpio/driver.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mfd/rk808.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/pinctrl/machine.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinmux.h>
26 #include <linux/slab.h>
30 #include "pinctrl-utils.h"
32 struct rk805_pin_function {
34 const char *const *groups;
39 struct rk805_pin_group {
41 const unsigned int pins[1];
46 * @reg: gpio setting register;
47 * @fun_mask: functions select mask value, when set is gpio;
48 * @dir_mask: input or output mask value, when set is output, otherwise input;
49 * @val_mask: gpio set value, when set is level high, otherwise low;
51 * Different PMIC has different pin features, belowing 3 mask members are not
52 * all necessary for every PMIC. For example, RK805 has 2 pins that can be used
53 * as output only GPIOs, so func_mask and dir_mask are not needed. RK816 has 1
54 * pin that can be used as TS/GPIO, so fun_mask, dir_mask and val_mask are all
57 struct rk805_pin_config {
64 struct rk805_pctrl_info {
67 struct pinctrl_dev *pctl;
68 struct gpio_chip gpio_chip;
69 struct pinctrl_desc pinctrl_desc;
70 const struct rk805_pin_function *functions;
71 unsigned int num_functions;
72 const struct rk805_pin_group *groups;
74 const struct pinctrl_pin_desc *pins;
75 unsigned int num_pins;
76 const struct rk805_pin_config *pin_cfg;
79 enum rk805_pinmux_option {
88 static const char *const rk805_gpio_groups[] = {
93 /* RK805: 2 output only GPIOs */
94 static const struct pinctrl_pin_desc rk805_pins_desc[] = {
95 PINCTRL_PIN(RK805_GPIO0, "gpio0"),
96 PINCTRL_PIN(RK805_GPIO1, "gpio1"),
99 static const struct rk805_pin_function rk805_pin_functions[] = {
102 .groups = rk805_gpio_groups,
103 .ngroups = ARRAY_SIZE(rk805_gpio_groups),
104 .mux_option = RK805_PINMUX_GPIO,
108 static const struct rk805_pin_group rk805_pin_groups[] = {
111 .pins = { RK805_GPIO0 },
116 .pins = { RK805_GPIO1 },
121 #define RK805_GPIO0_VAL_MSK BIT(0)
122 #define RK805_GPIO1_VAL_MSK BIT(1)
124 static const struct rk805_pin_config rk805_gpio_cfgs[] = {
126 .reg = RK805_OUT_REG,
127 .val_msk = RK805_GPIO0_VAL_MSK,
130 .reg = RK805_OUT_REG,
131 .val_msk = RK805_GPIO1_VAL_MSK,
135 /* generic gpio chip */
136 static int rk805_gpio_get(struct gpio_chip *chip, unsigned int offset)
138 struct rk805_pctrl_info *pci = gpiochip_get_data(chip);
141 ret = regmap_read(pci->rk808->regmap, pci->pin_cfg[offset].reg, &val);
143 dev_err(pci->dev, "get gpio%d value failed\n", offset);
147 return !!(val & pci->pin_cfg[offset].val_msk);
150 static void rk805_gpio_set(struct gpio_chip *chip,
154 struct rk805_pctrl_info *pci = gpiochip_get_data(chip);
157 ret = regmap_update_bits(pci->rk808->regmap,
158 pci->pin_cfg[offset].reg,
159 pci->pin_cfg[offset].val_msk,
160 value ? pci->pin_cfg[offset].val_msk : 0);
162 dev_err(pci->dev, "set gpio%d value %d failed\n",
166 static int rk805_gpio_direction_input(struct gpio_chip *chip,
169 return pinctrl_gpio_direction_input(chip->base + offset);
172 static int rk805_gpio_direction_output(struct gpio_chip *chip,
173 unsigned int offset, int value)
175 rk805_gpio_set(chip, offset, value);
176 return pinctrl_gpio_direction_output(chip->base + offset);
179 static int rk805_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
181 struct rk805_pctrl_info *pci = gpiochip_get_data(chip);
186 if (!pci->pin_cfg[offset].dir_msk)
187 return GPIO_LINE_DIRECTION_OUT;
189 ret = regmap_read(pci->rk808->regmap,
190 pci->pin_cfg[offset].reg,
193 dev_err(pci->dev, "get gpio%d direction failed\n", offset);
197 if (val & pci->pin_cfg[offset].dir_msk)
198 return GPIO_LINE_DIRECTION_OUT;
200 return GPIO_LINE_DIRECTION_IN;
203 static const struct gpio_chip rk805_gpio_chip = {
204 .label = "rk805-gpio",
205 .request = gpiochip_generic_request,
206 .free = gpiochip_generic_free,
207 .get_direction = rk805_gpio_get_direction,
208 .get = rk805_gpio_get,
209 .set = rk805_gpio_set,
210 .direction_input = rk805_gpio_direction_input,
211 .direction_output = rk805_gpio_direction_output,
214 .owner = THIS_MODULE,
217 /* generic pinctrl */
218 static int rk805_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
220 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
222 return pci->num_pin_groups;
225 static const char *rk805_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
228 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
230 return pci->groups[group].name;
233 static int rk805_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
235 const unsigned int **pins,
236 unsigned int *num_pins)
238 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
240 *pins = pci->groups[group].pins;
241 *num_pins = pci->groups[group].npins;
246 static const struct pinctrl_ops rk805_pinctrl_ops = {
247 .get_groups_count = rk805_pinctrl_get_groups_count,
248 .get_group_name = rk805_pinctrl_get_group_name,
249 .get_group_pins = rk805_pinctrl_get_group_pins,
250 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
251 .dt_free_map = pinctrl_utils_free_map,
254 static int rk805_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
256 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
258 return pci->num_functions;
261 static const char *rk805_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
262 unsigned int function)
264 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
266 return pci->functions[function].name;
269 static int rk805_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
270 unsigned int function,
271 const char *const **groups,
272 unsigned int *const num_groups)
274 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
276 *groups = pci->functions[function].groups;
277 *num_groups = pci->functions[function].ngroups;
282 static int _rk805_pinctrl_set_mux(struct pinctrl_dev *pctldev,
286 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
289 if (!pci->pin_cfg[offset].fun_msk)
292 if (mux == RK805_PINMUX_GPIO) {
293 ret = regmap_update_bits(pci->rk808->regmap,
294 pci->pin_cfg[offset].reg,
295 pci->pin_cfg[offset].fun_msk,
296 pci->pin_cfg[offset].fun_msk);
298 dev_err(pci->dev, "set gpio%d GPIO failed\n", offset);
302 dev_err(pci->dev, "Couldn't find function mux %d\n", mux);
309 static int rk805_pinctrl_set_mux(struct pinctrl_dev *pctldev,
310 unsigned int function,
313 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
314 int mux = pci->functions[function].mux_option;
317 return _rk805_pinctrl_set_mux(pctldev, offset, mux);
320 static int rk805_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
321 struct pinctrl_gpio_range *range,
322 unsigned int offset, bool input)
324 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
327 /* switch to gpio function */
328 ret = _rk805_pinctrl_set_mux(pctldev, offset, RK805_PINMUX_GPIO);
330 dev_err(pci->dev, "set gpio%d mux failed\n", offset);
335 if (!pci->pin_cfg[offset].dir_msk)
338 ret = regmap_update_bits(pci->rk808->regmap,
339 pci->pin_cfg[offset].reg,
340 pci->pin_cfg[offset].dir_msk,
341 input ? 0 : pci->pin_cfg[offset].dir_msk);
343 dev_err(pci->dev, "set gpio%d direction failed\n", offset);
350 static const struct pinmux_ops rk805_pinmux_ops = {
351 .get_functions_count = rk805_pinctrl_get_funcs_count,
352 .get_function_name = rk805_pinctrl_get_func_name,
353 .get_function_groups = rk805_pinctrl_get_func_groups,
354 .set_mux = rk805_pinctrl_set_mux,
355 .gpio_set_direction = rk805_pmx_gpio_set_direction,
358 static int rk805_pinconf_get(struct pinctrl_dev *pctldev,
359 unsigned int pin, unsigned long *config)
361 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
362 enum pin_config_param param = pinconf_to_config_param(*config);
366 case PIN_CONFIG_OUTPUT:
367 arg = rk805_gpio_get(&pci->gpio_chip, pin);
370 dev_err(pci->dev, "Properties not supported\n");
374 *config = pinconf_to_config_packed(param, (u16)arg);
379 static int rk805_pinconf_set(struct pinctrl_dev *pctldev,
380 unsigned int pin, unsigned long *configs,
381 unsigned int num_configs)
383 struct rk805_pctrl_info *pci = pinctrl_dev_get_drvdata(pctldev);
384 enum pin_config_param param;
387 for (i = 0; i < num_configs; i++) {
388 param = pinconf_to_config_param(configs[i]);
389 arg = pinconf_to_config_argument(configs[i]);
392 case PIN_CONFIG_OUTPUT:
393 rk805_gpio_set(&pci->gpio_chip, pin, arg);
394 rk805_pmx_gpio_set_direction(pctldev, NULL, pin, false);
397 dev_err(pci->dev, "Properties not supported\n");
405 static const struct pinconf_ops rk805_pinconf_ops = {
406 .pin_config_get = rk805_pinconf_get,
407 .pin_config_set = rk805_pinconf_set,
410 static const struct pinctrl_desc rk805_pinctrl_desc = {
411 .name = "rk805-pinctrl",
412 .pctlops = &rk805_pinctrl_ops,
413 .pmxops = &rk805_pinmux_ops,
414 .confops = &rk805_pinconf_ops,
415 .owner = THIS_MODULE,
418 static int rk805_pinctrl_probe(struct platform_device *pdev)
420 struct rk805_pctrl_info *pci;
423 pci = devm_kzalloc(&pdev->dev, sizeof(*pci), GFP_KERNEL);
427 pci->dev = &pdev->dev;
428 pci->dev->of_node = pdev->dev.parent->of_node;
429 pci->rk808 = dev_get_drvdata(pdev->dev.parent);
431 pci->pinctrl_desc = rk805_pinctrl_desc;
432 pci->gpio_chip = rk805_gpio_chip;
433 pci->gpio_chip.parent = &pdev->dev;
434 pci->gpio_chip.of_node = pdev->dev.parent->of_node;
436 platform_set_drvdata(pdev, pci);
438 switch (pci->rk808->variant) {
440 pci->pins = rk805_pins_desc;
441 pci->num_pins = ARRAY_SIZE(rk805_pins_desc);
442 pci->functions = rk805_pin_functions;
443 pci->num_functions = ARRAY_SIZE(rk805_pin_functions);
444 pci->groups = rk805_pin_groups;
445 pci->num_pin_groups = ARRAY_SIZE(rk805_pin_groups);
446 pci->pinctrl_desc.pins = rk805_pins_desc;
447 pci->pinctrl_desc.npins = ARRAY_SIZE(rk805_pins_desc);
448 pci->pin_cfg = rk805_gpio_cfgs;
449 pci->gpio_chip.ngpio = ARRAY_SIZE(rk805_gpio_cfgs);
452 dev_err(&pdev->dev, "unsupported RK805 ID %lu\n",
453 pci->rk808->variant);
458 ret = devm_gpiochip_add_data(&pdev->dev, &pci->gpio_chip, pci);
460 dev_err(&pdev->dev, "Couldn't add gpiochip\n");
465 pci->pctl = devm_pinctrl_register(&pdev->dev, &pci->pinctrl_desc, pci);
466 if (IS_ERR(pci->pctl)) {
467 dev_err(&pdev->dev, "Couldn't add pinctrl\n");
468 return PTR_ERR(pci->pctl);
472 ret = gpiochip_add_pin_range(&pci->gpio_chip, dev_name(&pdev->dev),
473 0, 0, pci->gpio_chip.ngpio);
475 dev_err(&pdev->dev, "Couldn't add gpiochip pin range\n");
482 static struct platform_driver rk805_pinctrl_driver = {
483 .probe = rk805_pinctrl_probe,
485 .name = "rk805-pinctrl",
488 module_platform_driver(rk805_pinctrl_driver);
490 MODULE_DESCRIPTION("RK805 pin control and GPIO driver");
491 MODULE_AUTHOR("Joseph Chen <chenjh@rock-chips.com>");
492 MODULE_LICENSE("GPL v2");