treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / pinctrl / mediatek / pinctrl-paris.c
blob923264d0e9ef2c595b93d559bc04fb209e736045
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * MediaTek Pinctrl Paris Driver, which implement the vendor per-pin
4 * bindings for MediaTek SoC.
6 * Copyright (C) 2018 MediaTek Inc.
7 * Author: Sean Wang <sean.wang@mediatek.com>
8 * Zhiyong Tao <zhiyong.tao@mediatek.com>
9 * Hongzhou.Yang <hongzhou.yang@mediatek.com>
12 #include <linux/gpio/driver.h>
13 #include <dt-bindings/pinctrl/mt65xx.h>
14 #include "pinctrl-paris.h"
16 #define PINCTRL_PINCTRL_DEV KBUILD_MODNAME
18 /* Custom pinconf parameters */
19 #define MTK_PIN_CONFIG_TDSEL (PIN_CONFIG_END + 1)
20 #define MTK_PIN_CONFIG_RDSEL (PIN_CONFIG_END + 2)
21 #define MTK_PIN_CONFIG_PU_ADV (PIN_CONFIG_END + 3)
22 #define MTK_PIN_CONFIG_PD_ADV (PIN_CONFIG_END + 4)
23 #define MTK_PIN_CONFIG_DRV_ADV (PIN_CONFIG_END + 5)
25 static const struct pinconf_generic_params mtk_custom_bindings[] = {
26 {"mediatek,tdsel", MTK_PIN_CONFIG_TDSEL, 0},
27 {"mediatek,rdsel", MTK_PIN_CONFIG_RDSEL, 0},
28 {"mediatek,pull-up-adv", MTK_PIN_CONFIG_PU_ADV, 1},
29 {"mediatek,pull-down-adv", MTK_PIN_CONFIG_PD_ADV, 1},
30 {"mediatek,drive-strength-adv", MTK_PIN_CONFIG_DRV_ADV, 2},
33 #ifdef CONFIG_DEBUG_FS
34 static const struct pin_config_item mtk_conf_items[] = {
35 PCONFDUMP(MTK_PIN_CONFIG_TDSEL, "tdsel", NULL, true),
36 PCONFDUMP(MTK_PIN_CONFIG_RDSEL, "rdsel", NULL, true),
37 PCONFDUMP(MTK_PIN_CONFIG_PU_ADV, "pu-adv", NULL, true),
38 PCONFDUMP(MTK_PIN_CONFIG_PD_ADV, "pd-adv", NULL, true),
39 PCONFDUMP(MTK_PIN_CONFIG_DRV_ADV, "drive-strength-adv", NULL, true),
41 #endif
43 static const char * const mtk_gpio_functions[] = {
44 "func0", "func1", "func2", "func3",
45 "func4", "func5", "func6", "func7",
46 "func8", "func9", "func10", "func11",
47 "func12", "func13", "func14", "func15",
50 static int mtk_pinmux_gpio_request_enable(struct pinctrl_dev *pctldev,
51 struct pinctrl_gpio_range *range,
52 unsigned int pin)
54 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
55 const struct mtk_pin_desc *desc;
57 desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
59 return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE,
60 hw->soc->gpio_m);
63 static int mtk_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
64 struct pinctrl_gpio_range *range,
65 unsigned int pin, bool input)
67 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
68 const struct mtk_pin_desc *desc;
70 desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
72 /* hardware would take 0 as input direction */
73 return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, !input);
76 static int mtk_pinconf_get(struct pinctrl_dev *pctldev,
77 unsigned int pin, unsigned long *config)
79 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
80 u32 param = pinconf_to_config_param(*config);
81 int val, val2, err, reg, ret = 1;
82 const struct mtk_pin_desc *desc;
84 desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
86 switch (param) {
87 case PIN_CONFIG_BIAS_DISABLE:
88 if (hw->soc->bias_disable_get) {
89 err = hw->soc->bias_disable_get(hw, desc, &ret);
90 if (err)
91 return err;
92 } else {
93 return -ENOTSUPP;
95 break;
96 case PIN_CONFIG_BIAS_PULL_UP:
97 if (hw->soc->bias_get) {
98 err = hw->soc->bias_get(hw, desc, 1, &ret);
99 if (err)
100 return err;
101 } else {
102 return -ENOTSUPP;
104 break;
105 case PIN_CONFIG_BIAS_PULL_DOWN:
106 if (hw->soc->bias_get) {
107 err = hw->soc->bias_get(hw, desc, 0, &ret);
108 if (err)
109 return err;
110 } else {
111 return -ENOTSUPP;
113 break;
114 case PIN_CONFIG_SLEW_RATE:
115 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_SR, &val);
116 if (err)
117 return err;
119 if (!val)
120 return -EINVAL;
122 break;
123 case PIN_CONFIG_INPUT_ENABLE:
124 case PIN_CONFIG_OUTPUT_ENABLE:
125 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &val);
126 if (err)
127 return err;
129 /* HW takes input mode as zero; output mode as non-zero */
130 if ((val && param == PIN_CONFIG_INPUT_ENABLE) ||
131 (!val && param == PIN_CONFIG_OUTPUT_ENABLE))
132 return -EINVAL;
134 break;
135 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
136 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &val);
137 if (err)
138 return err;
140 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_SMT, &val2);
141 if (err)
142 return err;
144 if (val || !val2)
145 return -EINVAL;
147 break;
148 case PIN_CONFIG_DRIVE_STRENGTH:
149 if (hw->soc->drive_get) {
150 err = hw->soc->drive_get(hw, desc, &ret);
151 if (err)
152 return err;
153 } else {
154 err = -ENOTSUPP;
156 break;
157 case MTK_PIN_CONFIG_TDSEL:
158 case MTK_PIN_CONFIG_RDSEL:
159 reg = (param == MTK_PIN_CONFIG_TDSEL) ?
160 PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
162 err = mtk_hw_get_value(hw, desc, reg, &val);
163 if (err)
164 return err;
166 ret = val;
168 break;
169 case MTK_PIN_CONFIG_PU_ADV:
170 case MTK_PIN_CONFIG_PD_ADV:
171 if (hw->soc->adv_pull_get) {
172 bool pullup;
174 pullup = param == MTK_PIN_CONFIG_PU_ADV;
175 err = hw->soc->adv_pull_get(hw, desc, pullup, &ret);
176 if (err)
177 return err;
178 } else {
179 return -ENOTSUPP;
181 break;
182 case MTK_PIN_CONFIG_DRV_ADV:
183 if (hw->soc->adv_drive_get) {
184 err = hw->soc->adv_drive_get(hw, desc, &ret);
185 if (err)
186 return err;
187 } else {
188 return -ENOTSUPP;
190 break;
191 default:
192 return -ENOTSUPP;
195 *config = pinconf_to_config_packed(param, ret);
197 return 0;
200 static int mtk_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
201 enum pin_config_param param,
202 enum pin_config_param arg)
204 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
205 const struct mtk_pin_desc *desc;
206 int err = 0;
207 u32 reg;
209 desc = (const struct mtk_pin_desc *)&hw->soc->pins[pin];
211 switch ((u32)param) {
212 case PIN_CONFIG_BIAS_DISABLE:
213 if (hw->soc->bias_disable_set) {
214 err = hw->soc->bias_disable_set(hw, desc);
215 if (err)
216 return err;
217 } else {
218 return -ENOTSUPP;
220 break;
221 case PIN_CONFIG_BIAS_PULL_UP:
222 if (hw->soc->bias_set) {
223 err = hw->soc->bias_set(hw, desc, 1);
224 if (err)
225 return err;
226 } else {
227 return -ENOTSUPP;
229 break;
230 case PIN_CONFIG_BIAS_PULL_DOWN:
231 if (hw->soc->bias_set) {
232 err = hw->soc->bias_set(hw, desc, 0);
233 if (err)
234 return err;
235 } else {
236 return -ENOTSUPP;
238 break;
239 case PIN_CONFIG_OUTPUT_ENABLE:
240 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT,
241 MTK_DISABLE);
242 if (err)
243 goto err;
245 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
246 MTK_OUTPUT);
247 if (err)
248 goto err;
249 break;
250 case PIN_CONFIG_INPUT_ENABLE:
251 if (hw->soc->ies_present) {
252 mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_IES,
253 MTK_ENABLE);
256 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
257 MTK_INPUT);
258 if (err)
259 goto err;
260 break;
261 case PIN_CONFIG_SLEW_RATE:
262 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SR,
263 arg);
264 if (err)
265 goto err;
267 break;
268 case PIN_CONFIG_OUTPUT:
269 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
270 MTK_OUTPUT);
271 if (err)
272 goto err;
274 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DO,
275 arg);
276 if (err)
277 goto err;
278 break;
279 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
280 /* arg = 1: Input mode & SMT enable ;
281 * arg = 0: Output mode & SMT disable
283 arg = arg ? 2 : 1;
284 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR,
285 arg & 1);
286 if (err)
287 goto err;
289 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT,
290 !!(arg & 2));
291 if (err)
292 goto err;
293 break;
294 case PIN_CONFIG_DRIVE_STRENGTH:
295 if (hw->soc->drive_set) {
296 err = hw->soc->drive_set(hw, desc, arg);
297 if (err)
298 return err;
299 } else {
300 return -ENOTSUPP;
302 break;
303 case MTK_PIN_CONFIG_TDSEL:
304 case MTK_PIN_CONFIG_RDSEL:
305 reg = (param == MTK_PIN_CONFIG_TDSEL) ?
306 PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
308 err = mtk_hw_set_value(hw, desc, reg, arg);
309 if (err)
310 goto err;
311 break;
312 case MTK_PIN_CONFIG_PU_ADV:
313 case MTK_PIN_CONFIG_PD_ADV:
314 if (hw->soc->adv_pull_set) {
315 bool pullup;
317 pullup = param == MTK_PIN_CONFIG_PU_ADV;
318 err = hw->soc->adv_pull_set(hw, desc, pullup,
319 arg);
320 if (err)
321 return err;
322 } else {
323 return -ENOTSUPP;
325 break;
326 case MTK_PIN_CONFIG_DRV_ADV:
327 if (hw->soc->adv_drive_set) {
328 err = hw->soc->adv_drive_set(hw, desc, arg);
329 if (err)
330 return err;
331 } else {
332 return -ENOTSUPP;
334 break;
335 default:
336 err = -ENOTSUPP;
339 err:
340 return err;
343 static struct mtk_pinctrl_group *
344 mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *hw, u32 pin)
346 int i;
348 for (i = 0; i < hw->soc->ngrps; i++) {
349 struct mtk_pinctrl_group *grp = hw->groups + i;
351 if (grp->pin == pin)
352 return grp;
355 return NULL;
358 static const struct mtk_func_desc *
359 mtk_pctrl_find_function_by_pin(struct mtk_pinctrl *hw, u32 pin_num, u32 fnum)
361 const struct mtk_pin_desc *pin = hw->soc->pins + pin_num;
362 const struct mtk_func_desc *func = pin->funcs;
364 while (func && func->name) {
365 if (func->muxval == fnum)
366 return func;
367 func++;
370 return NULL;
373 static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *hw, u32 pin_num,
374 u32 fnum)
376 int i;
378 for (i = 0; i < hw->soc->npins; i++) {
379 const struct mtk_pin_desc *pin = hw->soc->pins + i;
381 if (pin->number == pin_num) {
382 const struct mtk_func_desc *func = pin->funcs;
384 while (func && func->name) {
385 if (func->muxval == fnum)
386 return true;
387 func++;
390 break;
394 return false;
397 static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
398 u32 pin, u32 fnum,
399 struct mtk_pinctrl_group *grp,
400 struct pinctrl_map **map,
401 unsigned *reserved_maps,
402 unsigned *num_maps)
404 bool ret;
406 if (*num_maps == *reserved_maps)
407 return -ENOSPC;
409 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
410 (*map)[*num_maps].data.mux.group = grp->name;
412 ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
413 if (!ret) {
414 dev_err(pctl->dev, "invalid function %d on pin %d .\n",
415 fnum, pin);
416 return -EINVAL;
419 (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
420 (*num_maps)++;
422 return 0;
425 static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
426 struct device_node *node,
427 struct pinctrl_map **map,
428 unsigned *reserved_maps,
429 unsigned *num_maps)
431 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
432 int num_pins, num_funcs, maps_per_pin, i, err;
433 struct mtk_pinctrl_group *grp;
434 unsigned int num_configs;
435 bool has_config = false;
436 unsigned long *configs;
437 u32 pinfunc, pin, func;
438 struct property *pins;
439 unsigned reserve = 0;
441 pins = of_find_property(node, "pinmux", NULL);
442 if (!pins) {
443 dev_err(hw->dev, "missing pins property in node %pOFn .\n",
444 node);
445 return -EINVAL;
448 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
449 &num_configs);
450 if (err)
451 return err;
453 if (num_configs)
454 has_config = true;
456 num_pins = pins->length / sizeof(u32);
457 num_funcs = num_pins;
458 maps_per_pin = 0;
459 if (num_funcs)
460 maps_per_pin++;
461 if (has_config && num_pins >= 1)
462 maps_per_pin++;
464 if (!num_pins || !maps_per_pin) {
465 err = -EINVAL;
466 goto exit;
469 reserve = num_pins * maps_per_pin;
471 err = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps,
472 reserve);
473 if (err < 0)
474 goto exit;
476 for (i = 0; i < num_pins; i++) {
477 err = of_property_read_u32_index(node, "pinmux", i, &pinfunc);
478 if (err)
479 goto exit;
481 pin = MTK_GET_PIN_NO(pinfunc);
482 func = MTK_GET_PIN_FUNC(pinfunc);
484 if (pin >= hw->soc->npins ||
485 func >= ARRAY_SIZE(mtk_gpio_functions)) {
486 dev_err(hw->dev, "invalid pins value.\n");
487 err = -EINVAL;
488 goto exit;
491 grp = mtk_pctrl_find_group_by_pin(hw, pin);
492 if (!grp) {
493 dev_err(hw->dev, "unable to match pin %d to group\n",
494 pin);
495 err = -EINVAL;
496 goto exit;
499 err = mtk_pctrl_dt_node_to_map_func(hw, pin, func, grp, map,
500 reserved_maps, num_maps);
501 if (err < 0)
502 goto exit;
504 if (has_config) {
505 err = pinctrl_utils_add_map_configs(pctldev, map,
506 reserved_maps,
507 num_maps,
508 grp->name,
509 configs,
510 num_configs,
511 PIN_MAP_TYPE_CONFIGS_GROUP);
512 if (err < 0)
513 goto exit;
517 err = 0;
519 exit:
520 kfree(configs);
521 return err;
524 static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
525 struct device_node *np_config,
526 struct pinctrl_map **map,
527 unsigned *num_maps)
529 struct device_node *np;
530 unsigned reserved_maps;
531 int ret;
533 *map = NULL;
534 *num_maps = 0;
535 reserved_maps = 0;
537 for_each_child_of_node(np_config, np) {
538 ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
539 &reserved_maps,
540 num_maps);
541 if (ret < 0) {
542 pinctrl_utils_free_map(pctldev, *map, *num_maps);
543 of_node_put(np);
544 return ret;
548 return 0;
551 static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
553 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
555 return hw->soc->ngrps;
558 static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
559 unsigned group)
561 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
563 return hw->groups[group].name;
566 static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
567 unsigned group, const unsigned **pins,
568 unsigned *num_pins)
570 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
572 *pins = (unsigned *)&hw->groups[group].pin;
573 *num_pins = 1;
575 return 0;
578 static const struct pinctrl_ops mtk_pctlops = {
579 .dt_node_to_map = mtk_pctrl_dt_node_to_map,
580 .dt_free_map = pinctrl_utils_free_map,
581 .get_groups_count = mtk_pctrl_get_groups_count,
582 .get_group_name = mtk_pctrl_get_group_name,
583 .get_group_pins = mtk_pctrl_get_group_pins,
586 static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
588 return ARRAY_SIZE(mtk_gpio_functions);
591 static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
592 unsigned selector)
594 return mtk_gpio_functions[selector];
597 static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
598 unsigned function,
599 const char * const **groups,
600 unsigned * const num_groups)
602 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
604 *groups = hw->grp_names;
605 *num_groups = hw->soc->ngrps;
607 return 0;
610 static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
611 unsigned function,
612 unsigned group)
614 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
615 struct mtk_pinctrl_group *grp = hw->groups + group;
616 const struct mtk_func_desc *desc_func;
617 const struct mtk_pin_desc *desc;
618 bool ret;
620 ret = mtk_pctrl_is_function_valid(hw, grp->pin, function);
621 if (!ret) {
622 dev_err(hw->dev, "invalid function %d on group %d .\n",
623 function, group);
624 return -EINVAL;
627 desc_func = mtk_pctrl_find_function_by_pin(hw, grp->pin, function);
628 if (!desc_func)
629 return -EINVAL;
631 desc = (const struct mtk_pin_desc *)&hw->soc->pins[grp->pin];
632 mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE, desc_func->muxval);
634 return 0;
637 static const struct pinmux_ops mtk_pmxops = {
638 .get_functions_count = mtk_pmx_get_funcs_cnt,
639 .get_function_name = mtk_pmx_get_func_name,
640 .get_function_groups = mtk_pmx_get_func_groups,
641 .set_mux = mtk_pmx_set_mux,
642 .gpio_set_direction = mtk_pinmux_gpio_set_direction,
643 .gpio_request_enable = mtk_pinmux_gpio_request_enable,
646 static int mtk_pconf_group_get(struct pinctrl_dev *pctldev, unsigned group,
647 unsigned long *config)
649 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
651 *config = hw->groups[group].config;
653 return 0;
656 static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
657 unsigned long *configs, unsigned num_configs)
659 struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
660 struct mtk_pinctrl_group *grp = &hw->groups[group];
661 int i, ret;
663 for (i = 0; i < num_configs; i++) {
664 ret = mtk_pinconf_set(pctldev, grp->pin,
665 pinconf_to_config_param(configs[i]),
666 pinconf_to_config_argument(configs[i]));
667 if (ret < 0)
668 return ret;
670 grp->config = configs[i];
673 return 0;
676 static const struct pinconf_ops mtk_confops = {
677 .pin_config_get = mtk_pinconf_get,
678 .pin_config_group_get = mtk_pconf_group_get,
679 .pin_config_group_set = mtk_pconf_group_set,
682 static struct pinctrl_desc mtk_desc = {
683 .name = PINCTRL_PINCTRL_DEV,
684 .pctlops = &mtk_pctlops,
685 .pmxops = &mtk_pmxops,
686 .confops = &mtk_confops,
687 .owner = THIS_MODULE,
690 static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
692 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
693 const struct mtk_pin_desc *desc;
694 int value, err;
696 desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio];
698 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DIR, &value);
699 if (err)
700 return err;
702 return !value;
705 static int mtk_gpio_get(struct gpio_chip *chip, unsigned int gpio)
707 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
708 const struct mtk_pin_desc *desc;
709 int value, err;
711 desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio];
713 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value);
714 if (err)
715 return err;
717 return !!value;
720 static void mtk_gpio_set(struct gpio_chip *chip, unsigned int gpio, int value)
722 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
723 const struct mtk_pin_desc *desc;
725 desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio];
727 mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DO, !!value);
730 static int mtk_gpio_direction_input(struct gpio_chip *chip, unsigned int gpio)
732 return pinctrl_gpio_direction_input(chip->base + gpio);
735 static int mtk_gpio_direction_output(struct gpio_chip *chip, unsigned int gpio,
736 int value)
738 mtk_gpio_set(chip, gpio, value);
740 return pinctrl_gpio_direction_output(chip->base + gpio);
743 static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
745 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
746 const struct mtk_pin_desc *desc;
748 if (!hw->eint)
749 return -ENOTSUPP;
751 desc = (const struct mtk_pin_desc *)&hw->soc->pins[offset];
753 if (desc->eint.eint_n == EINT_NA)
754 return -ENOTSUPP;
756 return mtk_eint_find_irq(hw->eint, desc->eint.eint_n);
759 static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
760 unsigned long config)
762 struct mtk_pinctrl *hw = gpiochip_get_data(chip);
763 const struct mtk_pin_desc *desc;
764 u32 debounce;
766 desc = (const struct mtk_pin_desc *)&hw->soc->pins[offset];
768 if (!hw->eint ||
769 pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE ||
770 desc->eint.eint_n == EINT_NA)
771 return -ENOTSUPP;
773 debounce = pinconf_to_config_argument(config);
775 return mtk_eint_set_debounce(hw->eint, desc->eint.eint_n, debounce);
778 static int mtk_build_gpiochip(struct mtk_pinctrl *hw, struct device_node *np)
780 struct gpio_chip *chip = &hw->chip;
781 int ret;
783 chip->label = PINCTRL_PINCTRL_DEV;
784 chip->parent = hw->dev;
785 chip->request = gpiochip_generic_request;
786 chip->free = gpiochip_generic_free;
787 chip->get_direction = mtk_gpio_get_direction;
788 chip->direction_input = mtk_gpio_direction_input;
789 chip->direction_output = mtk_gpio_direction_output;
790 chip->get = mtk_gpio_get;
791 chip->set = mtk_gpio_set;
792 chip->to_irq = mtk_gpio_to_irq,
793 chip->set_config = mtk_gpio_set_config,
794 chip->base = -1;
795 chip->ngpio = hw->soc->npins;
796 chip->of_node = np;
797 chip->of_gpio_n_cells = 2;
799 ret = gpiochip_add_data(chip, hw);
800 if (ret < 0)
801 return ret;
803 return 0;
806 static int mtk_pctrl_build_state(struct platform_device *pdev)
808 struct mtk_pinctrl *hw = platform_get_drvdata(pdev);
809 int i;
811 /* Allocate groups */
812 hw->groups = devm_kmalloc_array(&pdev->dev, hw->soc->ngrps,
813 sizeof(*hw->groups), GFP_KERNEL);
814 if (!hw->groups)
815 return -ENOMEM;
817 /* We assume that one pin is one group, use pin name as group name. */
818 hw->grp_names = devm_kmalloc_array(&pdev->dev, hw->soc->ngrps,
819 sizeof(*hw->grp_names), GFP_KERNEL);
820 if (!hw->grp_names)
821 return -ENOMEM;
823 for (i = 0; i < hw->soc->npins; i++) {
824 const struct mtk_pin_desc *pin = hw->soc->pins + i;
825 struct mtk_pinctrl_group *group = hw->groups + i;
827 group->name = pin->name;
828 group->pin = pin->number;
830 hw->grp_names[i] = pin->name;
833 return 0;
836 int mtk_paris_pinctrl_probe(struct platform_device *pdev,
837 const struct mtk_pin_soc *soc)
839 struct pinctrl_pin_desc *pins;
840 struct mtk_pinctrl *hw;
841 struct resource *res;
842 int err, i;
844 hw = devm_kzalloc(&pdev->dev, sizeof(*hw), GFP_KERNEL);
845 if (!hw)
846 return -ENOMEM;
848 platform_set_drvdata(pdev, hw);
849 hw->soc = soc;
850 hw->dev = &pdev->dev;
852 if (!hw->soc->nbase_names) {
853 dev_err(&pdev->dev,
854 "SoC should be assigned at least one register base\n");
855 return -EINVAL;
858 hw->base = devm_kmalloc_array(&pdev->dev, hw->soc->nbase_names,
859 sizeof(*hw->base), GFP_KERNEL);
860 if (!hw->base)
861 return -ENOMEM;
863 for (i = 0; i < hw->soc->nbase_names; i++) {
864 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
865 hw->soc->base_names[i]);
866 if (!res) {
867 dev_err(&pdev->dev, "missing IO resource\n");
868 return -ENXIO;
871 hw->base[i] = devm_ioremap_resource(&pdev->dev, res);
872 if (IS_ERR(hw->base[i]))
873 return PTR_ERR(hw->base[i]);
876 hw->nbase = hw->soc->nbase_names;
878 err = mtk_pctrl_build_state(pdev);
879 if (err) {
880 dev_err(&pdev->dev, "build state failed: %d\n", err);
881 return -EINVAL;
884 /* Copy from internal struct mtk_pin_desc to register to the core */
885 pins = devm_kmalloc_array(&pdev->dev, hw->soc->npins, sizeof(*pins),
886 GFP_KERNEL);
887 if (!pins)
888 return -ENOMEM;
890 for (i = 0; i < hw->soc->npins; i++) {
891 pins[i].number = hw->soc->pins[i].number;
892 pins[i].name = hw->soc->pins[i].name;
895 /* Setup pins descriptions per SoC types */
896 mtk_desc.pins = (const struct pinctrl_pin_desc *)pins;
897 mtk_desc.npins = hw->soc->npins;
898 mtk_desc.num_custom_params = ARRAY_SIZE(mtk_custom_bindings);
899 mtk_desc.custom_params = mtk_custom_bindings;
900 #ifdef CONFIG_DEBUG_FS
901 mtk_desc.custom_conf_items = mtk_conf_items;
902 #endif
904 err = devm_pinctrl_register_and_init(&pdev->dev, &mtk_desc, hw,
905 &hw->pctrl);
906 if (err)
907 return err;
909 err = pinctrl_enable(hw->pctrl);
910 if (err)
911 return err;
913 err = mtk_build_eint(hw, pdev);
914 if (err)
915 dev_warn(&pdev->dev,
916 "Failed to add EINT, but pinctrl still can work\n");
918 /* Build gpiochip should be after pinctrl_enable is done */
919 err = mtk_build_gpiochip(hw, pdev->dev.of_node);
920 if (err) {
921 dev_err(&pdev->dev, "Failed to add gpio_chip\n");
922 return err;
925 platform_set_drvdata(pdev, hw);
927 return 0;
930 static int mtk_paris_pinctrl_suspend(struct device *device)
932 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
934 return mtk_eint_do_suspend(pctl->eint);
937 static int mtk_paris_pinctrl_resume(struct device *device)
939 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
941 return mtk_eint_do_resume(pctl->eint);
944 const struct dev_pm_ops mtk_paris_pinctrl_pm_ops = {
945 .suspend_noirq = mtk_paris_pinctrl_suspend,
946 .resume_noirq = mtk_paris_pinctrl_resume,