1 // SPDX-License-Identifier: GPL-2.0-only
3 * MAX77620 pin control driver.
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
8 * Chaitanya Bandi <bandik@nvidia.com>
9 * Laxman Dewangan <ldewangan@nvidia.com>
12 #include <linux/mfd/max77620.h>
13 #include <linux/module.h>
15 #include <linux/pinctrl/pinctrl.h>
16 #include <linux/pinctrl/pinconf-generic.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinmux.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
24 #include "pinctrl-utils.h"
26 #define MAX77620_PIN_NUM 8
28 enum max77620_pin_ppdrv {
29 MAX77620_PIN_UNCONFIG_DRV,
34 #define MAX77620_ACTIVE_FPS_SOURCE (PIN_CONFIG_END + 1)
35 #define MAX77620_ACTIVE_FPS_POWER_ON_SLOTS (PIN_CONFIG_END + 2)
36 #define MAX77620_ACTIVE_FPS_POWER_DOWN_SLOTS (PIN_CONFIG_END + 3)
37 #define MAX77620_SUSPEND_FPS_SOURCE (PIN_CONFIG_END + 4)
38 #define MAX77620_SUSPEND_FPS_POWER_ON_SLOTS (PIN_CONFIG_END + 5)
39 #define MAX77620_SUSPEND_FPS_POWER_DOWN_SLOTS (PIN_CONFIG_END + 6)
41 struct max77620_pin_function {
43 const char * const *groups;
48 static const struct pinconf_generic_params max77620_cfg_params[] = {
50 .property = "maxim,active-fps-source",
51 .param = MAX77620_ACTIVE_FPS_SOURCE,
53 .property = "maxim,active-fps-power-up-slot",
54 .param = MAX77620_ACTIVE_FPS_POWER_ON_SLOTS,
56 .property = "maxim,active-fps-power-down-slot",
57 .param = MAX77620_ACTIVE_FPS_POWER_DOWN_SLOTS,
59 .property = "maxim,suspend-fps-source",
60 .param = MAX77620_SUSPEND_FPS_SOURCE,
62 .property = "maxim,suspend-fps-power-up-slot",
63 .param = MAX77620_SUSPEND_FPS_POWER_ON_SLOTS,
65 .property = "maxim,suspend-fps-power-down-slot",
66 .param = MAX77620_SUSPEND_FPS_POWER_DOWN_SLOTS,
70 enum max77620_alternate_pinmux_option {
71 MAX77620_PINMUX_GPIO = 0,
72 MAX77620_PINMUX_LOW_POWER_MODE_CONTROL_IN = 1,
73 MAX77620_PINMUX_FLEXIBLE_POWER_SEQUENCER_OUT = 2,
74 MAX77620_PINMUX_32K_OUT1 = 3,
75 MAX77620_PINMUX_SD0_DYNAMIC_VOLTAGE_SCALING_IN = 4,
76 MAX77620_PINMUX_SD1_DYNAMIC_VOLTAGE_SCALING_IN = 5,
77 MAX77620_PINMUX_REFERENCE_OUT = 6,
80 struct max77620_pingroup {
82 const unsigned int pins[1];
84 enum max77620_alternate_pinmux_option alt_option;
87 struct max77620_pin_info {
88 enum max77620_pin_ppdrv drv_type;
92 struct max77620_fps_config {
94 int active_power_up_slots;
95 int active_power_down_slots;
97 int suspend_power_up_slots;
98 int suspend_power_down_slots;
101 struct max77620_pctrl_info {
103 struct pinctrl_dev *pctl;
105 int pins_current_opt[MAX77620_GPIO_NR];
106 const struct max77620_pin_function *functions;
107 unsigned int num_functions;
108 const struct max77620_pingroup *pin_groups;
110 const struct pinctrl_pin_desc *pins;
111 unsigned int num_pins;
112 struct max77620_pin_info pin_info[MAX77620_PIN_NUM];
113 struct max77620_fps_config fps_config[MAX77620_PIN_NUM];
116 static const struct pinctrl_pin_desc max77620_pins_desc[] = {
117 PINCTRL_PIN(MAX77620_GPIO0, "gpio0"),
118 PINCTRL_PIN(MAX77620_GPIO1, "gpio1"),
119 PINCTRL_PIN(MAX77620_GPIO2, "gpio2"),
120 PINCTRL_PIN(MAX77620_GPIO3, "gpio3"),
121 PINCTRL_PIN(MAX77620_GPIO4, "gpio4"),
122 PINCTRL_PIN(MAX77620_GPIO5, "gpio5"),
123 PINCTRL_PIN(MAX77620_GPIO6, "gpio6"),
124 PINCTRL_PIN(MAX77620_GPIO7, "gpio7"),
127 static const char * const gpio_groups[] = {
138 #define FUNCTION_GROUP(fname, mux) \
141 .groups = gpio_groups, \
142 .ngroups = ARRAY_SIZE(gpio_groups), \
143 .mux_option = MAX77620_PINMUX_##mux, \
146 static const struct max77620_pin_function max77620_pin_function[] = {
147 FUNCTION_GROUP("gpio", GPIO),
148 FUNCTION_GROUP("lpm-control-in", LOW_POWER_MODE_CONTROL_IN),
149 FUNCTION_GROUP("fps-out", FLEXIBLE_POWER_SEQUENCER_OUT),
150 FUNCTION_GROUP("32k-out1", 32K_OUT1),
151 FUNCTION_GROUP("sd0-dvs-in", SD0_DYNAMIC_VOLTAGE_SCALING_IN),
152 FUNCTION_GROUP("sd1-dvs-in", SD1_DYNAMIC_VOLTAGE_SCALING_IN),
153 FUNCTION_GROUP("reference-out", REFERENCE_OUT),
156 #define MAX77620_PINGROUP(pg_name, pin_id, option) \
159 .pins = {MAX77620_##pin_id}, \
161 .alt_option = MAX77620_PINMUX_##option, \
164 static const struct max77620_pingroup max77620_pingroups[] = {
165 MAX77620_PINGROUP(gpio0, GPIO0, LOW_POWER_MODE_CONTROL_IN),
166 MAX77620_PINGROUP(gpio1, GPIO1, FLEXIBLE_POWER_SEQUENCER_OUT),
167 MAX77620_PINGROUP(gpio2, GPIO2, FLEXIBLE_POWER_SEQUENCER_OUT),
168 MAX77620_PINGROUP(gpio3, GPIO3, FLEXIBLE_POWER_SEQUENCER_OUT),
169 MAX77620_PINGROUP(gpio4, GPIO4, 32K_OUT1),
170 MAX77620_PINGROUP(gpio5, GPIO5, SD0_DYNAMIC_VOLTAGE_SCALING_IN),
171 MAX77620_PINGROUP(gpio6, GPIO6, SD1_DYNAMIC_VOLTAGE_SCALING_IN),
172 MAX77620_PINGROUP(gpio7, GPIO7, REFERENCE_OUT),
175 static int max77620_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
177 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
179 return mpci->num_pin_groups;
182 static const char *max77620_pinctrl_get_group_name(
183 struct pinctrl_dev *pctldev, unsigned int group)
185 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
187 return mpci->pin_groups[group].name;
190 static int max77620_pinctrl_get_group_pins(
191 struct pinctrl_dev *pctldev, unsigned int group,
192 const unsigned int **pins, unsigned int *num_pins)
194 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
196 *pins = mpci->pin_groups[group].pins;
197 *num_pins = mpci->pin_groups[group].npins;
202 static const struct pinctrl_ops max77620_pinctrl_ops = {
203 .get_groups_count = max77620_pinctrl_get_groups_count,
204 .get_group_name = max77620_pinctrl_get_group_name,
205 .get_group_pins = max77620_pinctrl_get_group_pins,
206 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
207 .dt_free_map = pinctrl_utils_free_map,
210 static int max77620_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
212 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
214 return mpci->num_functions;
217 static const char *max77620_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
218 unsigned int function)
220 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
222 return mpci->functions[function].name;
225 static int max77620_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
226 unsigned int function,
227 const char * const **groups,
228 unsigned int * const num_groups)
230 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
232 *groups = mpci->functions[function].groups;
233 *num_groups = mpci->functions[function].ngroups;
238 static int max77620_pinctrl_enable(struct pinctrl_dev *pctldev,
239 unsigned int function, unsigned int group)
241 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
245 if (function == MAX77620_PINMUX_GPIO) {
247 } else if (function == mpci->pin_groups[group].alt_option) {
250 dev_err(mpci->dev, "GPIO %u doesn't have function %u\n",
254 ret = regmap_update_bits(mpci->rmap, MAX77620_REG_AME_GPIO,
257 dev_err(mpci->dev, "REG AME GPIO update failed: %d\n", ret);
262 static const struct pinmux_ops max77620_pinmux_ops = {
263 .get_functions_count = max77620_pinctrl_get_funcs_count,
264 .get_function_name = max77620_pinctrl_get_func_name,
265 .get_function_groups = max77620_pinctrl_get_func_groups,
266 .set_mux = max77620_pinctrl_enable,
269 static int max77620_pinconf_get(struct pinctrl_dev *pctldev,
270 unsigned int pin, unsigned long *config)
272 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
273 struct device *dev = mpci->dev;
274 enum pin_config_param param = pinconf_to_config_param(*config);
280 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
281 if (mpci->pin_info[pin].drv_type == MAX77620_PIN_OD_DRV)
285 case PIN_CONFIG_DRIVE_PUSH_PULL:
286 if (mpci->pin_info[pin].drv_type == MAX77620_PIN_PP_DRV)
290 case PIN_CONFIG_BIAS_PULL_UP:
291 ret = regmap_read(mpci->rmap, MAX77620_REG_PUE_GPIO, &val);
293 dev_err(dev, "Reg PUE_GPIO read failed: %d\n", ret);
300 case PIN_CONFIG_BIAS_PULL_DOWN:
301 ret = regmap_read(mpci->rmap, MAX77620_REG_PDE_GPIO, &val);
303 dev_err(dev, "Reg PDE_GPIO read failed: %d\n", ret);
311 dev_err(dev, "Properties not supported\n");
315 *config = pinconf_to_config_packed(param, (u16)arg);
320 static int max77620_get_default_fps(struct max77620_pctrl_info *mpci,
326 ret = regmap_read(mpci->rmap, addr, &val);
328 dev_err(mpci->dev, "Reg PUE_GPIO read failed: %d\n", ret);
331 *fps = (val & MAX77620_FPS_SRC_MASK) >> MAX77620_FPS_SRC_SHIFT;
336 static int max77620_set_fps_param(struct max77620_pctrl_info *mpci,
339 struct max77620_fps_config *fps_config = &mpci->fps_config[pin];
344 if ((pin < MAX77620_GPIO1) || (pin > MAX77620_GPIO3))
347 addr = MAX77620_REG_FPS_GPIO1 + pin - 1;
349 case MAX77620_ACTIVE_FPS_SOURCE:
350 case MAX77620_SUSPEND_FPS_SOURCE:
351 mask = MAX77620_FPS_SRC_MASK;
352 shift = MAX77620_FPS_SRC_SHIFT;
353 param_val = fps_config->active_fps_src;
354 if (param == MAX77620_SUSPEND_FPS_SOURCE)
355 param_val = fps_config->suspend_fps_src;
358 case MAX77620_ACTIVE_FPS_POWER_ON_SLOTS:
359 case MAX77620_SUSPEND_FPS_POWER_ON_SLOTS:
360 mask = MAX77620_FPS_PU_PERIOD_MASK;
361 shift = MAX77620_FPS_PU_PERIOD_SHIFT;
362 param_val = fps_config->active_power_up_slots;
363 if (param == MAX77620_SUSPEND_FPS_POWER_ON_SLOTS)
364 param_val = fps_config->suspend_power_up_slots;
367 case MAX77620_ACTIVE_FPS_POWER_DOWN_SLOTS:
368 case MAX77620_SUSPEND_FPS_POWER_DOWN_SLOTS:
369 mask = MAX77620_FPS_PD_PERIOD_MASK;
370 shift = MAX77620_FPS_PD_PERIOD_SHIFT;
371 param_val = fps_config->active_power_down_slots;
372 if (param == MAX77620_SUSPEND_FPS_POWER_DOWN_SLOTS)
373 param_val = fps_config->suspend_power_down_slots;
377 dev_err(mpci->dev, "Invalid parameter %d for pin %d\n",
385 ret = regmap_update_bits(mpci->rmap, addr, mask, param_val << shift);
387 dev_err(mpci->dev, "Reg 0x%02x update failed %d\n", addr, ret);
392 static int max77620_pinconf_set(struct pinctrl_dev *pctldev,
393 unsigned int pin, unsigned long *configs,
394 unsigned int num_configs)
396 struct max77620_pctrl_info *mpci = pinctrl_dev_get_drvdata(pctldev);
397 struct device *dev = mpci->dev;
398 struct max77620_fps_config *fps_config;
407 for (i = 0; i < num_configs; i++) {
408 param = pinconf_to_config_param(configs[i]);
409 param_val = pinconf_to_config_argument(configs[i]);
412 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
413 val = param_val ? 0 : 1;
414 ret = regmap_update_bits(mpci->rmap,
415 MAX77620_REG_GPIO0 + pin,
416 MAX77620_CNFG_GPIO_DRV_MASK,
419 goto report_update_failure;
421 mpci->pin_info[pin].drv_type = val ?
422 MAX77620_PIN_PP_DRV : MAX77620_PIN_OD_DRV;
425 case PIN_CONFIG_DRIVE_PUSH_PULL:
426 val = param_val ? 1 : 0;
427 ret = regmap_update_bits(mpci->rmap,
428 MAX77620_REG_GPIO0 + pin,
429 MAX77620_CNFG_GPIO_DRV_MASK,
432 goto report_update_failure;
434 mpci->pin_info[pin].drv_type = val ?
435 MAX77620_PIN_PP_DRV : MAX77620_PIN_OD_DRV;
438 case MAX77620_ACTIVE_FPS_SOURCE:
439 case MAX77620_ACTIVE_FPS_POWER_ON_SLOTS:
440 case MAX77620_ACTIVE_FPS_POWER_DOWN_SLOTS:
441 if ((pin < MAX77620_GPIO1) || (pin > MAX77620_GPIO3))
444 fps_config = &mpci->fps_config[pin];
446 if ((param == MAX77620_ACTIVE_FPS_SOURCE) &&
447 (param_val == MAX77620_FPS_SRC_DEF)) {
448 addr = MAX77620_REG_FPS_GPIO1 + pin - 1;
449 ret = max77620_get_default_fps(
451 &fps_config->active_fps_src);
457 if (param == MAX77620_ACTIVE_FPS_SOURCE)
458 fps_config->active_fps_src = param_val;
459 else if (param == MAX77620_ACTIVE_FPS_POWER_ON_SLOTS)
460 fps_config->active_power_up_slots = param_val;
462 fps_config->active_power_down_slots = param_val;
464 ret = max77620_set_fps_param(mpci, pin, param);
469 case MAX77620_SUSPEND_FPS_SOURCE:
470 case MAX77620_SUSPEND_FPS_POWER_ON_SLOTS:
471 case MAX77620_SUSPEND_FPS_POWER_DOWN_SLOTS:
472 if ((pin < MAX77620_GPIO1) || (pin > MAX77620_GPIO3))
475 fps_config = &mpci->fps_config[pin];
477 if ((param == MAX77620_SUSPEND_FPS_SOURCE) &&
478 (param_val == MAX77620_FPS_SRC_DEF)) {
479 addr = MAX77620_REG_FPS_GPIO1 + pin - 1;
480 ret = max77620_get_default_fps(
482 &fps_config->suspend_fps_src);
488 if (param == MAX77620_SUSPEND_FPS_SOURCE)
489 fps_config->suspend_fps_src = param_val;
490 else if (param == MAX77620_SUSPEND_FPS_POWER_ON_SLOTS)
491 fps_config->suspend_power_up_slots = param_val;
493 fps_config->suspend_power_down_slots =
497 case PIN_CONFIG_BIAS_PULL_UP:
498 case PIN_CONFIG_BIAS_PULL_DOWN:
499 pu_val = (param == PIN_CONFIG_BIAS_PULL_UP) ?
501 pd_val = (param == PIN_CONFIG_BIAS_PULL_DOWN) ?
504 ret = regmap_update_bits(mpci->rmap,
505 MAX77620_REG_PUE_GPIO,
508 dev_err(dev, "PUE_GPIO update failed: %d\n",
513 ret = regmap_update_bits(mpci->rmap,
514 MAX77620_REG_PDE_GPIO,
517 dev_err(dev, "PDE_GPIO update failed: %d\n",
524 dev_err(dev, "Properties not supported\n");
531 report_update_failure:
532 dev_err(dev, "Reg 0x%02x update failed %d\n",
533 MAX77620_REG_GPIO0 + pin, ret);
537 static const struct pinconf_ops max77620_pinconf_ops = {
538 .pin_config_get = max77620_pinconf_get,
539 .pin_config_set = max77620_pinconf_set,
542 static struct pinctrl_desc max77620_pinctrl_desc = {
543 .pctlops = &max77620_pinctrl_ops,
544 .pmxops = &max77620_pinmux_ops,
545 .confops = &max77620_pinconf_ops,
548 static int max77620_pinctrl_probe(struct platform_device *pdev)
550 struct max77620_chip *max77620 = dev_get_drvdata(pdev->dev.parent);
551 struct max77620_pctrl_info *mpci;
554 mpci = devm_kzalloc(&pdev->dev, sizeof(*mpci), GFP_KERNEL);
558 mpci->dev = &pdev->dev;
559 mpci->dev->of_node = pdev->dev.parent->of_node;
560 mpci->rmap = max77620->rmap;
562 mpci->pins = max77620_pins_desc;
563 mpci->num_pins = ARRAY_SIZE(max77620_pins_desc);
564 mpci->functions = max77620_pin_function;
565 mpci->num_functions = ARRAY_SIZE(max77620_pin_function);
566 mpci->pin_groups = max77620_pingroups;
567 mpci->num_pin_groups = ARRAY_SIZE(max77620_pingroups);
568 platform_set_drvdata(pdev, mpci);
570 max77620_pinctrl_desc.name = dev_name(&pdev->dev);
571 max77620_pinctrl_desc.pins = max77620_pins_desc;
572 max77620_pinctrl_desc.npins = ARRAY_SIZE(max77620_pins_desc);
573 max77620_pinctrl_desc.num_custom_params =
574 ARRAY_SIZE(max77620_cfg_params);
575 max77620_pinctrl_desc.custom_params = max77620_cfg_params;
577 for (i = 0; i < MAX77620_PIN_NUM; ++i) {
578 mpci->fps_config[i].active_fps_src = -1;
579 mpci->fps_config[i].active_power_up_slots = -1;
580 mpci->fps_config[i].active_power_down_slots = -1;
581 mpci->fps_config[i].suspend_fps_src = -1;
582 mpci->fps_config[i].suspend_power_up_slots = -1;
583 mpci->fps_config[i].suspend_power_down_slots = -1;
586 mpci->pctl = devm_pinctrl_register(&pdev->dev, &max77620_pinctrl_desc,
588 if (IS_ERR(mpci->pctl)) {
589 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
590 return PTR_ERR(mpci->pctl);
596 #ifdef CONFIG_PM_SLEEP
597 static int max77620_suspend_fps_param[] = {
598 MAX77620_SUSPEND_FPS_SOURCE,
599 MAX77620_SUSPEND_FPS_POWER_ON_SLOTS,
600 MAX77620_SUSPEND_FPS_POWER_DOWN_SLOTS,
603 static int max77620_active_fps_param[] = {
604 MAX77620_ACTIVE_FPS_SOURCE,
605 MAX77620_ACTIVE_FPS_POWER_ON_SLOTS,
606 MAX77620_ACTIVE_FPS_POWER_DOWN_SLOTS,
609 static int max77620_pinctrl_suspend(struct device *dev)
611 struct max77620_pctrl_info *mpci = dev_get_drvdata(dev);
614 for (pin = 0; pin < MAX77620_PIN_NUM; ++pin) {
615 if ((pin < MAX77620_GPIO1) || (pin > MAX77620_GPIO3))
617 for (p = 0; p < 3; ++p)
618 max77620_set_fps_param(
619 mpci, pin, max77620_suspend_fps_param[p]);
625 static int max77620_pinctrl_resume(struct device *dev)
627 struct max77620_pctrl_info *mpci = dev_get_drvdata(dev);
630 for (pin = 0; pin < MAX77620_PIN_NUM; ++pin) {
631 if ((pin < MAX77620_GPIO1) || (pin > MAX77620_GPIO3))
633 for (p = 0; p < 3; ++p)
634 max77620_set_fps_param(
635 mpci, pin, max77620_active_fps_param[p]);
642 static const struct dev_pm_ops max77620_pinctrl_pm_ops = {
643 SET_SYSTEM_SLEEP_PM_OPS(
644 max77620_pinctrl_suspend, max77620_pinctrl_resume)
647 static const struct platform_device_id max77620_pinctrl_devtype[] = {
648 { .name = "max77620-pinctrl", },
649 { .name = "max20024-pinctrl", },
652 MODULE_DEVICE_TABLE(platform, max77620_pinctrl_devtype);
654 static struct platform_driver max77620_pinctrl_driver = {
656 .name = "max77620-pinctrl",
657 .pm = &max77620_pinctrl_pm_ops,
659 .probe = max77620_pinctrl_probe,
660 .id_table = max77620_pinctrl_devtype,
663 module_platform_driver(max77620_pinctrl_driver);
665 MODULE_DESCRIPTION("MAX77620/MAX20024 pin control driver");
666 MODULE_AUTHOR("Chaitanya Bandi<bandik@nvidia.com>");
667 MODULE_AUTHOR("Laxman Dewangan<ldewangan@nvidia.com>");
668 MODULE_ALIAS("platform:max77620-pinctrl");
669 MODULE_LICENSE("GPL v2");