treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / pinctrl / mediatek / pinctrl-mtk-common.c
blob67f8444f7a0cb6a5eb725c24ca90b0a8241850ca
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * mt65xx pinctrl driver based on Allwinner A1X pinctrl driver.
4 * Copyright (c) 2014 MediaTek Inc.
5 * Author: Hongzhou.Yang <hongzhou.yang@mediatek.com>
6 */
8 #include <linux/io.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/of.h>
11 #include <linux/of_address.h>
12 #include <linux/of_device.h>
13 #include <linux/of_irq.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/pinctrl/machine.h>
16 #include <linux/pinctrl/pinconf.h>
17 #include <linux/pinctrl/pinconf-generic.h>
18 #include <linux/pinctrl/pinctrl.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/bitops.h>
23 #include <linux/regmap.h>
24 #include <linux/mfd/syscon.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/pm.h>
28 #include <dt-bindings/pinctrl/mt65xx.h>
30 #include "../core.h"
31 #include "../pinconf.h"
32 #include "../pinctrl-utils.h"
33 #include "mtk-eint.h"
34 #include "pinctrl-mtk-common.h"
36 #define MAX_GPIO_MODE_PER_REG 5
37 #define GPIO_MODE_BITS 3
38 #define GPIO_MODE_PREFIX "GPIO"
40 static const char * const mtk_gpio_functions[] = {
41 "func0", "func1", "func2", "func3",
42 "func4", "func5", "func6", "func7",
43 "func8", "func9", "func10", "func11",
44 "func12", "func13", "func14", "func15",
48 * There are two base address for pull related configuration
49 * in mt8135, and different GPIO pins use different base address.
50 * When pin number greater than type1_start and less than type1_end,
51 * should use the second base address.
53 static struct regmap *mtk_get_regmap(struct mtk_pinctrl *pctl,
54 unsigned long pin)
56 if (pin >= pctl->devdata->type1_start && pin < pctl->devdata->type1_end)
57 return pctl->regmap2;
58 return pctl->regmap1;
61 static unsigned int mtk_get_port(struct mtk_pinctrl *pctl, unsigned long pin)
63 /* Different SoC has different mask and port shift. */
64 return ((pin >> 4) & pctl->devdata->port_mask)
65 << pctl->devdata->port_shf;
68 static int mtk_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
69 struct pinctrl_gpio_range *range, unsigned offset,
70 bool input)
72 unsigned int reg_addr;
73 unsigned int bit;
74 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
76 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
77 bit = BIT(offset & 0xf);
79 if (pctl->devdata->spec_dir_set)
80 pctl->devdata->spec_dir_set(&reg_addr, offset);
82 if (input)
83 /* Different SoC has different alignment offset. */
84 reg_addr = CLR_ADDR(reg_addr, pctl);
85 else
86 reg_addr = SET_ADDR(reg_addr, pctl);
88 regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
89 return 0;
92 static void mtk_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
94 unsigned int reg_addr;
95 unsigned int bit;
96 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
98 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dout_offset;
99 bit = BIT(offset & 0xf);
101 if (value)
102 reg_addr = SET_ADDR(reg_addr, pctl);
103 else
104 reg_addr = CLR_ADDR(reg_addr, pctl);
106 regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
109 static int mtk_pconf_set_ies_smt(struct mtk_pinctrl *pctl, unsigned pin,
110 int value, enum pin_config_param arg)
112 unsigned int reg_addr, offset;
113 unsigned int bit;
116 * Due to some soc are not support ies/smt config, add this special
117 * control to handle it.
119 if (!pctl->devdata->spec_ies_smt_set &&
120 pctl->devdata->ies_offset == MTK_PINCTRL_NOT_SUPPORT &&
121 arg == PIN_CONFIG_INPUT_ENABLE)
122 return -EINVAL;
124 if (!pctl->devdata->spec_ies_smt_set &&
125 pctl->devdata->smt_offset == MTK_PINCTRL_NOT_SUPPORT &&
126 arg == PIN_CONFIG_INPUT_SCHMITT_ENABLE)
127 return -EINVAL;
130 * Due to some pins are irregular, their input enable and smt
131 * control register are discontinuous, so we need this special handle.
133 if (pctl->devdata->spec_ies_smt_set) {
134 return pctl->devdata->spec_ies_smt_set(mtk_get_regmap(pctl, pin),
135 pin, pctl->devdata->port_align, value, arg);
138 bit = BIT(pin & 0xf);
140 if (arg == PIN_CONFIG_INPUT_ENABLE)
141 offset = pctl->devdata->ies_offset;
142 else
143 offset = pctl->devdata->smt_offset;
145 if (value)
146 reg_addr = SET_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
147 else
148 reg_addr = CLR_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
150 regmap_write(mtk_get_regmap(pctl, pin), reg_addr, bit);
151 return 0;
154 int mtk_pconf_spec_set_ies_smt_range(struct regmap *regmap,
155 const struct mtk_pin_ies_smt_set *ies_smt_infos, unsigned int info_num,
156 unsigned int pin, unsigned char align, int value)
158 unsigned int i, reg_addr, bit;
160 for (i = 0; i < info_num; i++) {
161 if (pin >= ies_smt_infos[i].start &&
162 pin <= ies_smt_infos[i].end) {
163 break;
167 if (i == info_num)
168 return -EINVAL;
170 if (value)
171 reg_addr = ies_smt_infos[i].offset + align;
172 else
173 reg_addr = ies_smt_infos[i].offset + (align << 1);
175 bit = BIT(ies_smt_infos[i].bit);
176 regmap_write(regmap, reg_addr, bit);
177 return 0;
180 static const struct mtk_pin_drv_grp *mtk_find_pin_drv_grp_by_pin(
181 struct mtk_pinctrl *pctl, unsigned long pin) {
182 int i;
184 for (i = 0; i < pctl->devdata->n_pin_drv_grps; i++) {
185 const struct mtk_pin_drv_grp *pin_drv =
186 pctl->devdata->pin_drv_grp + i;
187 if (pin == pin_drv->pin)
188 return pin_drv;
191 return NULL;
194 static int mtk_pconf_set_driving(struct mtk_pinctrl *pctl,
195 unsigned int pin, unsigned char driving)
197 const struct mtk_pin_drv_grp *pin_drv;
198 unsigned int val;
199 unsigned int bits, mask, shift;
200 const struct mtk_drv_group_desc *drv_grp;
202 if (pin >= pctl->devdata->npins)
203 return -EINVAL;
205 pin_drv = mtk_find_pin_drv_grp_by_pin(pctl, pin);
206 if (!pin_drv || pin_drv->grp > pctl->devdata->n_grp_cls)
207 return -EINVAL;
209 drv_grp = pctl->devdata->grp_desc + pin_drv->grp;
210 if (driving >= drv_grp->min_drv && driving <= drv_grp->max_drv
211 && !(driving % drv_grp->step)) {
212 val = driving / drv_grp->step - 1;
213 bits = drv_grp->high_bit - drv_grp->low_bit + 1;
214 mask = BIT(bits) - 1;
215 shift = pin_drv->bit + drv_grp->low_bit;
216 mask <<= shift;
217 val <<= shift;
218 return regmap_update_bits(mtk_get_regmap(pctl, pin),
219 pin_drv->offset, mask, val);
222 return -EINVAL;
225 int mtk_pctrl_spec_pull_set_samereg(struct regmap *regmap,
226 const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
227 unsigned int info_num, unsigned int pin,
228 unsigned char align, bool isup, unsigned int r1r0)
230 unsigned int i;
231 unsigned int reg_pupd, reg_set, reg_rst;
232 unsigned int bit_pupd, bit_r0, bit_r1;
233 const struct mtk_pin_spec_pupd_set_samereg *spec_pupd_pin;
234 bool find = false;
236 for (i = 0; i < info_num; i++) {
237 if (pin == pupd_infos[i].pin) {
238 find = true;
239 break;
243 if (!find)
244 return -EINVAL;
246 spec_pupd_pin = pupd_infos + i;
247 reg_set = spec_pupd_pin->offset + align;
248 reg_rst = spec_pupd_pin->offset + (align << 1);
250 if (isup)
251 reg_pupd = reg_rst;
252 else
253 reg_pupd = reg_set;
255 bit_pupd = BIT(spec_pupd_pin->pupd_bit);
256 regmap_write(regmap, reg_pupd, bit_pupd);
258 bit_r0 = BIT(spec_pupd_pin->r0_bit);
259 bit_r1 = BIT(spec_pupd_pin->r1_bit);
261 switch (r1r0) {
262 case MTK_PUPD_SET_R1R0_00:
263 regmap_write(regmap, reg_rst, bit_r0);
264 regmap_write(regmap, reg_rst, bit_r1);
265 break;
266 case MTK_PUPD_SET_R1R0_01:
267 regmap_write(regmap, reg_set, bit_r0);
268 regmap_write(regmap, reg_rst, bit_r1);
269 break;
270 case MTK_PUPD_SET_R1R0_10:
271 regmap_write(regmap, reg_rst, bit_r0);
272 regmap_write(regmap, reg_set, bit_r1);
273 break;
274 case MTK_PUPD_SET_R1R0_11:
275 regmap_write(regmap, reg_set, bit_r0);
276 regmap_write(regmap, reg_set, bit_r1);
277 break;
278 default:
279 return -EINVAL;
282 return 0;
285 static int mtk_pconf_set_pull_select(struct mtk_pinctrl *pctl,
286 unsigned int pin, bool enable, bool isup, unsigned int arg)
288 unsigned int bit;
289 unsigned int reg_pullen, reg_pullsel, r1r0;
290 int ret;
292 /* Some pins' pull setting are very different,
293 * they have separate pull up/down bit, R0 and R1
294 * resistor bit, so we need this special handle.
296 if (pctl->devdata->spec_pull_set) {
297 /* For special pins, bias-disable is set by R1R0,
298 * the parameter should be "MTK_PUPD_SET_R1R0_00".
300 r1r0 = enable ? arg : MTK_PUPD_SET_R1R0_00;
301 ret = pctl->devdata->spec_pull_set(mtk_get_regmap(pctl, pin),
302 pin, pctl->devdata->port_align, isup, r1r0);
303 if (!ret)
304 return 0;
307 /* For generic pull config, default arg value should be 0 or 1. */
308 if (arg != 0 && arg != 1) {
309 dev_err(pctl->dev, "invalid pull-up argument %d on pin %d .\n",
310 arg, pin);
311 return -EINVAL;
314 bit = BIT(pin & 0xf);
315 if (enable)
316 reg_pullen = SET_ADDR(mtk_get_port(pctl, pin) +
317 pctl->devdata->pullen_offset, pctl);
318 else
319 reg_pullen = CLR_ADDR(mtk_get_port(pctl, pin) +
320 pctl->devdata->pullen_offset, pctl);
322 if (isup)
323 reg_pullsel = SET_ADDR(mtk_get_port(pctl, pin) +
324 pctl->devdata->pullsel_offset, pctl);
325 else
326 reg_pullsel = CLR_ADDR(mtk_get_port(pctl, pin) +
327 pctl->devdata->pullsel_offset, pctl);
329 regmap_write(mtk_get_regmap(pctl, pin), reg_pullen, bit);
330 regmap_write(mtk_get_regmap(pctl, pin), reg_pullsel, bit);
331 return 0;
334 static int mtk_pconf_parse_conf(struct pinctrl_dev *pctldev,
335 unsigned int pin, enum pin_config_param param,
336 enum pin_config_param arg)
338 int ret = 0;
339 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
341 switch (param) {
342 case PIN_CONFIG_BIAS_DISABLE:
343 ret = mtk_pconf_set_pull_select(pctl, pin, false, false, arg);
344 break;
345 case PIN_CONFIG_BIAS_PULL_UP:
346 ret = mtk_pconf_set_pull_select(pctl, pin, true, true, arg);
347 break;
348 case PIN_CONFIG_BIAS_PULL_DOWN:
349 ret = mtk_pconf_set_pull_select(pctl, pin, true, false, arg);
350 break;
351 case PIN_CONFIG_INPUT_ENABLE:
352 mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
353 ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
354 break;
355 case PIN_CONFIG_OUTPUT:
356 mtk_gpio_set(pctl->chip, pin, arg);
357 ret = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
358 break;
359 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
360 mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
361 ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
362 break;
363 case PIN_CONFIG_DRIVE_STRENGTH:
364 ret = mtk_pconf_set_driving(pctl, pin, arg);
365 break;
366 default:
367 ret = -EINVAL;
370 return ret;
373 static int mtk_pconf_group_get(struct pinctrl_dev *pctldev,
374 unsigned group,
375 unsigned long *config)
377 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
379 *config = pctl->groups[group].config;
381 return 0;
384 static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
385 unsigned long *configs, unsigned num_configs)
387 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
388 struct mtk_pinctrl_group *g = &pctl->groups[group];
389 int i, ret;
391 for (i = 0; i < num_configs; i++) {
392 ret = mtk_pconf_parse_conf(pctldev, g->pin,
393 pinconf_to_config_param(configs[i]),
394 pinconf_to_config_argument(configs[i]));
395 if (ret < 0)
396 return ret;
398 g->config = configs[i];
401 return 0;
404 static const struct pinconf_ops mtk_pconf_ops = {
405 .pin_config_group_get = mtk_pconf_group_get,
406 .pin_config_group_set = mtk_pconf_group_set,
409 static struct mtk_pinctrl_group *
410 mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *pctl, u32 pin)
412 int i;
414 for (i = 0; i < pctl->ngroups; i++) {
415 struct mtk_pinctrl_group *grp = pctl->groups + i;
417 if (grp->pin == pin)
418 return grp;
421 return NULL;
424 static const struct mtk_desc_function *mtk_pctrl_find_function_by_pin(
425 struct mtk_pinctrl *pctl, u32 pin_num, u32 fnum)
427 const struct mtk_desc_pin *pin = pctl->devdata->pins + pin_num;
428 const struct mtk_desc_function *func = pin->functions;
430 while (func && func->name) {
431 if (func->muxval == fnum)
432 return func;
433 func++;
436 return NULL;
439 static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *pctl,
440 u32 pin_num, u32 fnum)
442 int i;
444 for (i = 0; i < pctl->devdata->npins; i++) {
445 const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
447 if (pin->pin.number == pin_num) {
448 const struct mtk_desc_function *func =
449 pin->functions;
451 while (func && func->name) {
452 if (func->muxval == fnum)
453 return true;
454 func++;
457 break;
461 return false;
464 static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
465 u32 pin, u32 fnum, struct mtk_pinctrl_group *grp,
466 struct pinctrl_map **map, unsigned *reserved_maps,
467 unsigned *num_maps)
469 bool ret;
471 if (*num_maps == *reserved_maps)
472 return -ENOSPC;
474 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
475 (*map)[*num_maps].data.mux.group = grp->name;
477 ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
478 if (!ret) {
479 dev_err(pctl->dev, "invalid function %d on pin %d .\n",
480 fnum, pin);
481 return -EINVAL;
484 (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
485 (*num_maps)++;
487 return 0;
490 static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
491 struct device_node *node,
492 struct pinctrl_map **map,
493 unsigned *reserved_maps,
494 unsigned *num_maps)
496 struct property *pins;
497 u32 pinfunc, pin, func;
498 int num_pins, num_funcs, maps_per_pin;
499 unsigned long *configs;
500 unsigned int num_configs;
501 bool has_config = false;
502 int i, err;
503 unsigned reserve = 0;
504 struct mtk_pinctrl_group *grp;
505 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
507 pins = of_find_property(node, "pinmux", NULL);
508 if (!pins) {
509 dev_err(pctl->dev, "missing pins property in node %pOFn .\n",
510 node);
511 return -EINVAL;
514 err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
515 &num_configs);
516 if (err)
517 return err;
519 if (num_configs)
520 has_config = true;
522 num_pins = pins->length / sizeof(u32);
523 num_funcs = num_pins;
524 maps_per_pin = 0;
525 if (num_funcs)
526 maps_per_pin++;
527 if (has_config && num_pins >= 1)
528 maps_per_pin++;
530 if (!num_pins || !maps_per_pin) {
531 err = -EINVAL;
532 goto exit;
535 reserve = num_pins * maps_per_pin;
537 err = pinctrl_utils_reserve_map(pctldev, map,
538 reserved_maps, num_maps, reserve);
539 if (err < 0)
540 goto exit;
542 for (i = 0; i < num_pins; i++) {
543 err = of_property_read_u32_index(node, "pinmux",
544 i, &pinfunc);
545 if (err)
546 goto exit;
548 pin = MTK_GET_PIN_NO(pinfunc);
549 func = MTK_GET_PIN_FUNC(pinfunc);
551 if (pin >= pctl->devdata->npins ||
552 func >= ARRAY_SIZE(mtk_gpio_functions)) {
553 dev_err(pctl->dev, "invalid pins value.\n");
554 err = -EINVAL;
555 goto exit;
558 grp = mtk_pctrl_find_group_by_pin(pctl, pin);
559 if (!grp) {
560 dev_err(pctl->dev, "unable to match pin %d to group\n",
561 pin);
562 err = -EINVAL;
563 goto exit;
566 err = mtk_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
567 reserved_maps, num_maps);
568 if (err < 0)
569 goto exit;
571 if (has_config) {
572 err = pinctrl_utils_add_map_configs(pctldev, map,
573 reserved_maps, num_maps, grp->name,
574 configs, num_configs,
575 PIN_MAP_TYPE_CONFIGS_GROUP);
576 if (err < 0)
577 goto exit;
581 err = 0;
583 exit:
584 kfree(configs);
585 return err;
588 static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
589 struct device_node *np_config,
590 struct pinctrl_map **map, unsigned *num_maps)
592 struct device_node *np;
593 unsigned reserved_maps;
594 int ret;
596 *map = NULL;
597 *num_maps = 0;
598 reserved_maps = 0;
600 for_each_child_of_node(np_config, np) {
601 ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
602 &reserved_maps, num_maps);
603 if (ret < 0) {
604 pinctrl_utils_free_map(pctldev, *map, *num_maps);
605 of_node_put(np);
606 return ret;
610 return 0;
613 static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
615 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
617 return pctl->ngroups;
620 static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
621 unsigned group)
623 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
625 return pctl->groups[group].name;
628 static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
629 unsigned group,
630 const unsigned **pins,
631 unsigned *num_pins)
633 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
635 *pins = (unsigned *)&pctl->groups[group].pin;
636 *num_pins = 1;
638 return 0;
641 static const struct pinctrl_ops mtk_pctrl_ops = {
642 .dt_node_to_map = mtk_pctrl_dt_node_to_map,
643 .dt_free_map = pinctrl_utils_free_map,
644 .get_groups_count = mtk_pctrl_get_groups_count,
645 .get_group_name = mtk_pctrl_get_group_name,
646 .get_group_pins = mtk_pctrl_get_group_pins,
649 static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
651 return ARRAY_SIZE(mtk_gpio_functions);
654 static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
655 unsigned selector)
657 return mtk_gpio_functions[selector];
660 static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
661 unsigned function,
662 const char * const **groups,
663 unsigned * const num_groups)
665 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
667 *groups = pctl->grp_names;
668 *num_groups = pctl->ngroups;
670 return 0;
673 static int mtk_pmx_set_mode(struct pinctrl_dev *pctldev,
674 unsigned long pin, unsigned long mode)
676 unsigned int reg_addr;
677 unsigned char bit;
678 unsigned int val;
679 unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
680 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
682 if (pctl->devdata->spec_pinmux_set)
683 pctl->devdata->spec_pinmux_set(mtk_get_regmap(pctl, pin),
684 pin, mode);
686 reg_addr = ((pin / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
687 + pctl->devdata->pinmux_offset;
689 mode &= mask;
690 bit = pin % MAX_GPIO_MODE_PER_REG;
691 mask <<= (GPIO_MODE_BITS * bit);
692 val = (mode << (GPIO_MODE_BITS * bit));
693 return regmap_update_bits(mtk_get_regmap(pctl, pin),
694 reg_addr, mask, val);
697 static const struct mtk_desc_pin *
698 mtk_find_pin_by_eint_num(struct mtk_pinctrl *pctl, unsigned int eint_num)
700 int i;
701 const struct mtk_desc_pin *pin;
703 for (i = 0; i < pctl->devdata->npins; i++) {
704 pin = pctl->devdata->pins + i;
705 if (pin->eint.eintnum == eint_num)
706 return pin;
709 return NULL;
712 static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
713 unsigned function,
714 unsigned group)
716 bool ret;
717 const struct mtk_desc_function *desc;
718 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
719 struct mtk_pinctrl_group *g = pctl->groups + group;
721 ret = mtk_pctrl_is_function_valid(pctl, g->pin, function);
722 if (!ret) {
723 dev_err(pctl->dev, "invalid function %d on group %d .\n",
724 function, group);
725 return -EINVAL;
728 desc = mtk_pctrl_find_function_by_pin(pctl, g->pin, function);
729 if (!desc)
730 return -EINVAL;
731 mtk_pmx_set_mode(pctldev, g->pin, desc->muxval);
732 return 0;
735 static int mtk_pmx_find_gpio_mode(struct mtk_pinctrl *pctl,
736 unsigned offset)
738 const struct mtk_desc_pin *pin = pctl->devdata->pins + offset;
739 const struct mtk_desc_function *func = pin->functions;
741 while (func && func->name) {
742 if (!strncmp(func->name, GPIO_MODE_PREFIX,
743 sizeof(GPIO_MODE_PREFIX)-1))
744 return func->muxval;
745 func++;
747 return -EINVAL;
750 static int mtk_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
751 struct pinctrl_gpio_range *range,
752 unsigned offset)
754 int muxval;
755 struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
757 muxval = mtk_pmx_find_gpio_mode(pctl, offset);
759 if (muxval < 0) {
760 dev_err(pctl->dev, "invalid gpio pin %d.\n", offset);
761 return -EINVAL;
764 mtk_pmx_set_mode(pctldev, offset, muxval);
765 mtk_pconf_set_ies_smt(pctl, offset, 1, PIN_CONFIG_INPUT_ENABLE);
767 return 0;
770 static const struct pinmux_ops mtk_pmx_ops = {
771 .get_functions_count = mtk_pmx_get_funcs_cnt,
772 .get_function_name = mtk_pmx_get_func_name,
773 .get_function_groups = mtk_pmx_get_func_groups,
774 .set_mux = mtk_pmx_set_mux,
775 .gpio_set_direction = mtk_pmx_gpio_set_direction,
776 .gpio_request_enable = mtk_pmx_gpio_request_enable,
779 static int mtk_gpio_direction_input(struct gpio_chip *chip,
780 unsigned offset)
782 return pinctrl_gpio_direction_input(chip->base + offset);
785 static int mtk_gpio_direction_output(struct gpio_chip *chip,
786 unsigned offset, int value)
788 mtk_gpio_set(chip, offset, value);
789 return pinctrl_gpio_direction_output(chip->base + offset);
792 static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
794 unsigned int reg_addr;
795 unsigned int bit;
796 unsigned int read_val = 0;
798 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
800 reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
801 bit = BIT(offset & 0xf);
803 if (pctl->devdata->spec_dir_set)
804 pctl->devdata->spec_dir_set(&reg_addr, offset);
806 regmap_read(pctl->regmap1, reg_addr, &read_val);
807 return !(read_val & bit);
810 static int mtk_gpio_get(struct gpio_chip *chip, unsigned offset)
812 unsigned int reg_addr;
813 unsigned int bit;
814 unsigned int read_val = 0;
815 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
817 reg_addr = mtk_get_port(pctl, offset) +
818 pctl->devdata->din_offset;
820 bit = BIT(offset & 0xf);
821 regmap_read(pctl->regmap1, reg_addr, &read_val);
822 return !!(read_val & bit);
825 static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
827 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
828 const struct mtk_desc_pin *pin;
829 unsigned long eint_n;
831 pin = pctl->devdata->pins + offset;
832 if (pin->eint.eintnum == NO_EINT_SUPPORT)
833 return -EINVAL;
835 eint_n = pin->eint.eintnum;
837 return mtk_eint_find_irq(pctl->eint, eint_n);
840 static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned offset,
841 unsigned long config)
843 struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
844 const struct mtk_desc_pin *pin;
845 unsigned long eint_n;
846 u32 debounce;
848 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
849 return -ENOTSUPP;
851 pin = pctl->devdata->pins + offset;
852 if (pin->eint.eintnum == NO_EINT_SUPPORT)
853 return -EINVAL;
855 debounce = pinconf_to_config_argument(config);
856 eint_n = pin->eint.eintnum;
858 return mtk_eint_set_debounce(pctl->eint, eint_n, debounce);
861 static const struct gpio_chip mtk_gpio_chip = {
862 .owner = THIS_MODULE,
863 .request = gpiochip_generic_request,
864 .free = gpiochip_generic_free,
865 .get_direction = mtk_gpio_get_direction,
866 .direction_input = mtk_gpio_direction_input,
867 .direction_output = mtk_gpio_direction_output,
868 .get = mtk_gpio_get,
869 .set = mtk_gpio_set,
870 .to_irq = mtk_gpio_to_irq,
871 .set_config = mtk_gpio_set_config,
872 .of_gpio_n_cells = 2,
875 static int mtk_eint_suspend(struct device *device)
877 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
879 return mtk_eint_do_suspend(pctl->eint);
882 static int mtk_eint_resume(struct device *device)
884 struct mtk_pinctrl *pctl = dev_get_drvdata(device);
886 return mtk_eint_do_resume(pctl->eint);
889 const struct dev_pm_ops mtk_eint_pm_ops = {
890 .suspend_noirq = mtk_eint_suspend,
891 .resume_noirq = mtk_eint_resume,
894 static int mtk_pctrl_build_state(struct platform_device *pdev)
896 struct mtk_pinctrl *pctl = platform_get_drvdata(pdev);
897 int i;
899 pctl->ngroups = pctl->devdata->npins;
901 /* Allocate groups */
902 pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
903 sizeof(*pctl->groups), GFP_KERNEL);
904 if (!pctl->groups)
905 return -ENOMEM;
907 /* We assume that one pin is one group, use pin name as group name. */
908 pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
909 sizeof(*pctl->grp_names), GFP_KERNEL);
910 if (!pctl->grp_names)
911 return -ENOMEM;
913 for (i = 0; i < pctl->devdata->npins; i++) {
914 const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
915 struct mtk_pinctrl_group *group = pctl->groups + i;
917 group->name = pin->pin.name;
918 group->pin = pin->pin.number;
920 pctl->grp_names[i] = pin->pin.name;
923 return 0;
926 static int
927 mtk_xt_get_gpio_n(void *data, unsigned long eint_n, unsigned int *gpio_n,
928 struct gpio_chip **gpio_chip)
930 struct mtk_pinctrl *pctl = (struct mtk_pinctrl *)data;
931 const struct mtk_desc_pin *pin;
933 pin = mtk_find_pin_by_eint_num(pctl, eint_n);
934 if (!pin)
935 return -EINVAL;
937 *gpio_chip = pctl->chip;
938 *gpio_n = pin->pin.number;
940 return 0;
943 static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n)
945 struct mtk_pinctrl *pctl = (struct mtk_pinctrl *)data;
946 const struct mtk_desc_pin *pin;
948 pin = mtk_find_pin_by_eint_num(pctl, eint_n);
949 if (!pin)
950 return -EINVAL;
952 return mtk_gpio_get(pctl->chip, pin->pin.number);
955 static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n)
957 struct mtk_pinctrl *pctl = (struct mtk_pinctrl *)data;
958 const struct mtk_desc_pin *pin;
960 pin = mtk_find_pin_by_eint_num(pctl, eint_n);
961 if (!pin)
962 return -EINVAL;
964 /* set mux to INT mode */
965 mtk_pmx_set_mode(pctl->pctl_dev, pin->pin.number, pin->eint.eintmux);
966 /* set gpio direction to input */
967 mtk_pmx_gpio_set_direction(pctl->pctl_dev, NULL, pin->pin.number,
968 true);
969 /* set input-enable */
970 mtk_pconf_set_ies_smt(pctl, pin->pin.number, 1,
971 PIN_CONFIG_INPUT_ENABLE);
973 return 0;
976 static const struct mtk_eint_xt mtk_eint_xt = {
977 .get_gpio_n = mtk_xt_get_gpio_n,
978 .get_gpio_state = mtk_xt_get_gpio_state,
979 .set_gpio_as_eint = mtk_xt_set_gpio_as_eint,
982 static int mtk_eint_init(struct mtk_pinctrl *pctl, struct platform_device *pdev)
984 struct device_node *np = pdev->dev.of_node;
986 if (!of_property_read_bool(np, "interrupt-controller"))
987 return -ENODEV;
989 pctl->eint = devm_kzalloc(pctl->dev, sizeof(*pctl->eint), GFP_KERNEL);
990 if (!pctl->eint)
991 return -ENOMEM;
993 pctl->eint->base = devm_platform_ioremap_resource(pdev, 0);
994 if (IS_ERR(pctl->eint->base))
995 return PTR_ERR(pctl->eint->base);
997 pctl->eint->irq = irq_of_parse_and_map(np, 0);
998 if (!pctl->eint->irq)
999 return -EINVAL;
1001 pctl->eint->dev = &pdev->dev;
1003 * If pctl->eint->regs == NULL, it would fall back into using a generic
1004 * register map in mtk_eint_do_init calls.
1006 pctl->eint->regs = pctl->devdata->eint_regs;
1007 pctl->eint->hw = &pctl->devdata->eint_hw;
1008 pctl->eint->pctl = pctl;
1009 pctl->eint->gpio_xlate = &mtk_eint_xt;
1011 return mtk_eint_do_init(pctl->eint);
1014 int mtk_pctrl_init(struct platform_device *pdev,
1015 const struct mtk_pinctrl_devdata *data,
1016 struct regmap *regmap)
1018 struct pinctrl_pin_desc *pins;
1019 struct mtk_pinctrl *pctl;
1020 struct device_node *np = pdev->dev.of_node, *node;
1021 struct property *prop;
1022 int ret, i;
1024 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
1025 if (!pctl)
1026 return -ENOMEM;
1028 platform_set_drvdata(pdev, pctl);
1030 prop = of_find_property(np, "pins-are-numbered", NULL);
1031 if (!prop) {
1032 dev_err(&pdev->dev, "only support pins-are-numbered format\n");
1033 return -EINVAL;
1036 node = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
1037 if (node) {
1038 pctl->regmap1 = syscon_node_to_regmap(node);
1039 if (IS_ERR(pctl->regmap1))
1040 return PTR_ERR(pctl->regmap1);
1041 } else if (regmap) {
1042 pctl->regmap1 = regmap;
1043 } else {
1044 dev_err(&pdev->dev, "Pinctrl node has not register regmap.\n");
1045 return -EINVAL;
1048 /* Only 8135 has two base addr, other SoCs have only one. */
1049 node = of_parse_phandle(np, "mediatek,pctl-regmap", 1);
1050 if (node) {
1051 pctl->regmap2 = syscon_node_to_regmap(node);
1052 if (IS_ERR(pctl->regmap2))
1053 return PTR_ERR(pctl->regmap2);
1056 pctl->devdata = data;
1057 ret = mtk_pctrl_build_state(pdev);
1058 if (ret) {
1059 dev_err(&pdev->dev, "build state failed: %d\n", ret);
1060 return -EINVAL;
1063 pins = devm_kcalloc(&pdev->dev, pctl->devdata->npins, sizeof(*pins),
1064 GFP_KERNEL);
1065 if (!pins)
1066 return -ENOMEM;
1068 for (i = 0; i < pctl->devdata->npins; i++)
1069 pins[i] = pctl->devdata->pins[i].pin;
1071 pctl->pctl_desc.name = dev_name(&pdev->dev);
1072 pctl->pctl_desc.owner = THIS_MODULE;
1073 pctl->pctl_desc.pins = pins;
1074 pctl->pctl_desc.npins = pctl->devdata->npins;
1075 pctl->pctl_desc.confops = &mtk_pconf_ops;
1076 pctl->pctl_desc.pctlops = &mtk_pctrl_ops;
1077 pctl->pctl_desc.pmxops = &mtk_pmx_ops;
1078 pctl->dev = &pdev->dev;
1080 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->pctl_desc,
1081 pctl);
1082 if (IS_ERR(pctl->pctl_dev)) {
1083 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
1084 return PTR_ERR(pctl->pctl_dev);
1087 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
1088 if (!pctl->chip)
1089 return -ENOMEM;
1091 *pctl->chip = mtk_gpio_chip;
1092 pctl->chip->ngpio = pctl->devdata->npins;
1093 pctl->chip->label = dev_name(&pdev->dev);
1094 pctl->chip->parent = &pdev->dev;
1095 pctl->chip->base = -1;
1097 ret = gpiochip_add_data(pctl->chip, pctl);
1098 if (ret)
1099 return -EINVAL;
1101 /* Register the GPIO to pin mappings. */
1102 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
1103 0, 0, pctl->devdata->npins);
1104 if (ret) {
1105 ret = -EINVAL;
1106 goto chip_error;
1109 ret = mtk_eint_init(pctl, pdev);
1110 if (ret)
1111 goto chip_error;
1113 return 0;
1115 chip_error:
1116 gpiochip_remove(pctl->chip);
1117 return ret;