1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 MediaTek Inc.
4 * Author: Ryder Lee <ryder.lee@mediatek.com>
9 #include <dm/device-internal.h>
11 #include <dm/pinctrl.h>
13 #include <asm-generic/gpio.h>
14 #include <linux/bitops.h>
16 #include "pinctrl-mtk-common.h"
18 #if CONFIG_IS_ENABLED(PINCONF)
20 * struct mtk_drive_desc - the structure that holds the information
21 * of the driving current
22 * @min: the minimum current of this group
23 * @max: the maximum current of this group
24 * @step: the step current of this group
25 * @scal: the weight factor
27 * formula: output = ((input) / step - 1) * scal
29 struct mtk_drive_desc {
36 /* The groups of drive strength */
37 static const struct mtk_drive_desc mtk_drive[] = {
38 [DRV_GRP0] = { 4, 16, 4, 1 },
39 [DRV_GRP1] = { 4, 16, 4, 2 },
40 [DRV_GRP2] = { 2, 8, 2, 1 },
41 [DRV_GRP3] = { 2, 8, 2, 2 },
42 [DRV_GRP4] = { 2, 16, 2, 1 },
46 static const char *mtk_pinctrl_dummy_name = "_dummy";
48 static void mtk_w32(struct udevice *dev, u8 i, u32 reg, u32 val)
50 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
52 __raw_writel(val, priv->base[i] + reg);
55 static u32 mtk_r32(struct udevice *dev, u8 i, u32 reg)
57 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
59 return __raw_readl(priv->base[i] + reg);
62 static inline int get_count_order(unsigned int count)
66 order = fls(count) - 1;
67 if (count & (count - 1))
72 void mtk_rmw(struct udevice *dev, u32 reg, u32 mask, u32 set)
74 return mtk_i_rmw(dev, 0, reg, mask, set);
77 void mtk_i_rmw(struct udevice *dev, u8 i, u32 reg, u32 mask, u32 set)
81 val = mtk_r32(dev, i, reg);
84 mtk_w32(dev, i, reg, val);
87 static int mtk_hw_pin_field_lookup(struct udevice *dev, int pin,
88 const struct mtk_pin_reg_calc *rc,
89 struct mtk_pin_field *pfd)
91 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
92 const struct mtk_pin_field_calc *c, *e;
94 u32 base_calc = priv->soc->base_calc;
100 if (pin >= c->s_pin && pin <= c->e_pin)
108 /* Calculated bits as the overall offset the pin is located at,
109 * if c->fixed is held, that determines the all the pins in the
110 * range use the same field with the s_pin.
112 bits = c->fixed ? c->s_bit : c->s_bit + (pin - c->s_pin) * (c->x_bits);
114 /* Fill pfd from bits. For example 32-bit register applied is assumed
115 * when c->sz_reg is equal to 32.
117 pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg);
118 pfd->bitpos = bits % c->sz_reg;
119 pfd->mask = (1 << c->x_bits) - 1;
122 pfd->index = c->i_base;
126 /* pfd->next is used for indicating that bit wrapping-around happens
127 * which requires the manipulation for bit 0 starting in the next
128 * register to form the complete field read/write.
130 pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0;
135 static int mtk_hw_pin_field_get(struct udevice *dev, int pin,
136 int field, struct mtk_pin_field *pfd)
138 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
139 const struct mtk_pin_reg_calc *rc;
141 if (field < 0 || field >= PINCTRL_PIN_REG_MAX)
144 if (priv->soc->reg_cal && priv->soc->reg_cal[field].range)
145 rc = &priv->soc->reg_cal[field];
149 return mtk_hw_pin_field_lookup(dev, pin, rc, pfd);
152 static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
154 *l = 32 - pf->bitpos;
155 *h = get_count_order(pf->mask) - *l;
158 static void mtk_hw_write_cross_field(struct udevice *dev,
159 struct mtk_pin_field *pf, int value)
161 int nbits_l, nbits_h;
163 mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
165 mtk_i_rmw(dev, pf->index, pf->offset, pf->mask << pf->bitpos,
166 (value & pf->mask) << pf->bitpos);
168 mtk_i_rmw(dev, pf->index, pf->offset + pf->next, BIT(nbits_h) - 1,
169 (value & pf->mask) >> nbits_l);
172 static void mtk_hw_read_cross_field(struct udevice *dev,
173 struct mtk_pin_field *pf, int *value)
175 int nbits_l, nbits_h, h, l;
177 mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
179 l = (mtk_r32(dev, pf->index, pf->offset) >> pf->bitpos) & (BIT(nbits_l) - 1);
180 h = (mtk_r32(dev, pf->index, pf->offset + pf->next)) & (BIT(nbits_h) - 1);
182 *value = (h << nbits_l) | l;
185 static int mtk_hw_set_value(struct udevice *dev, int pin, int field,
188 struct mtk_pin_field pf;
191 err = mtk_hw_pin_field_get(dev, pin, field, &pf);
196 mtk_i_rmw(dev, pf.index, pf.offset, pf.mask << pf.bitpos,
197 (value & pf.mask) << pf.bitpos);
199 mtk_hw_write_cross_field(dev, &pf, value);
204 static int mtk_hw_get_value(struct udevice *dev, int pin, int field,
207 struct mtk_pin_field pf;
210 err = mtk_hw_pin_field_get(dev, pin, field, &pf);
215 *value = (mtk_r32(dev, pf.index, pf.offset) >> pf.bitpos) & pf.mask;
217 mtk_hw_read_cross_field(dev, &pf, value);
222 #if CONFIG_IS_ENABLED(PINCONF)
223 static int mtk_get_pin_io_type(struct udevice *dev, int pin,
224 struct mtk_io_type_desc *io_type)
226 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
227 u8 io_n = priv->soc->pins[pin].io_n;
229 if (io_n >= priv->soc->ntype)
232 io_type->name = priv->soc->io_type[io_n].name;
233 io_type->bias_set = priv->soc->io_type[io_n].bias_set;
234 io_type->drive_set = priv->soc->io_type[io_n].drive_set;
235 io_type->input_enable = priv->soc->io_type[io_n].input_enable;
241 static int mtk_get_groups_count(struct udevice *dev)
243 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
245 return priv->soc->ngrps;
248 static const char *mtk_get_pin_name(struct udevice *dev,
249 unsigned int selector)
251 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
253 if (!priv->soc->pins[selector].name)
254 return mtk_pinctrl_dummy_name;
256 return priv->soc->pins[selector].name;
259 static int mtk_get_pins_count(struct udevice *dev)
261 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
263 return priv->soc->npins;
266 static int mtk_get_pin_muxing(struct udevice *dev, unsigned int selector,
270 err = mtk_hw_get_value(dev, selector, PINCTRL_PIN_REG_MODE, &val);
274 snprintf(buf, size, "Aux Func.%d", val);
278 static const char *mtk_get_group_name(struct udevice *dev,
279 unsigned int selector)
281 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
283 if (!priv->soc->grps[selector].name)
284 return mtk_pinctrl_dummy_name;
286 return priv->soc->grps[selector].name;
289 static int mtk_get_functions_count(struct udevice *dev)
291 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
293 return priv->soc->nfuncs;
296 static const char *mtk_get_function_name(struct udevice *dev,
297 unsigned int selector)
299 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
301 if (!priv->soc->funcs[selector].name)
302 return mtk_pinctrl_dummy_name;
304 return priv->soc->funcs[selector].name;
307 static int mtk_pinmux_set(struct udevice *dev, unsigned int pin_selector,
308 unsigned int func_selector)
312 err = mtk_hw_set_value(dev, pin_selector, PINCTRL_PIN_REG_MODE,
320 static int mtk_pinmux_group_set(struct udevice *dev,
321 unsigned int group_selector,
322 unsigned int func_selector)
324 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
325 const struct mtk_group_desc *grp =
326 &priv->soc->grps[group_selector];
329 for (i = 0; i < grp->num_pins; i++) {
330 const int *pin_modes = grp->data;
332 mtk_hw_set_value(dev, grp->pins[i], PINCTRL_PIN_REG_MODE,
339 #if CONFIG_IS_ENABLED(PINCONF)
340 static const struct pinconf_param mtk_conf_params[] = {
341 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
342 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
343 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
344 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
345 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
346 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
347 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
348 { "output-enable", PIN_CONFIG_OUTPUT_ENABLE, 1 },
349 { "output-high", PIN_CONFIG_OUTPUT, 1, },
350 { "output-low", PIN_CONFIG_OUTPUT, 0, },
351 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
354 int mtk_pinconf_bias_set_v0(struct udevice *dev, u32 pin, bool disable,
355 bool pullup, u32 val)
357 return mtk_pinconf_bias_set_pu_pd(dev, pin, disable, pullup, val);
360 int mtk_pinconf_bias_set_v1(struct udevice *dev, u32 pin, bool disable,
361 bool pullup, u32 val)
365 /* set pupd_r1_r0 if pullen_pullsel succeeded */
366 err = mtk_pinconf_bias_set_pullen_pullsel(dev, pin, disable, pullup,
369 return mtk_pinconf_bias_set_pupd_r1_r0(dev, pin, disable,
375 int mtk_pinconf_bias_set_pu_pd(struct udevice *dev, u32 pin, bool disable,
376 bool pullup, u32 val)
381 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PU, 0);
384 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PD, 0);
388 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PU, pullup);
391 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PD, !pullup);
399 int mtk_pinconf_bias_set_pullen_pullsel(struct udevice *dev, u32 pin,
400 bool disable, bool pullup, u32 val)
405 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLEN, 0);
409 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLEN, 1);
412 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLSEL,
421 int mtk_pinconf_bias_set_pupd_r1_r0(struct udevice *dev, u32 pin, bool disable,
422 bool pullup, u32 val)
435 /* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
436 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PUPD, !pullup);
440 /* Also set PUPD/R0/R1 if the pin has them */
441 mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_R0, r0);
442 mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_R1, r1);
447 int mtk_pinconf_bias_set(struct udevice *dev, u32 pin, u32 arg, u32 val)
450 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
451 struct mtk_io_type_desc io_type;
452 int rev = priv->soc->rev;
453 bool disable, pullup;
455 disable = (arg == PIN_CONFIG_BIAS_DISABLE);
456 pullup = (arg == PIN_CONFIG_BIAS_PULL_UP);
458 if (!mtk_get_pin_io_type(dev, pin, &io_type)) {
459 if (io_type.bias_set)
460 err = io_type.bias_set(dev, pin, disable, pullup,
465 } else if (rev == MTK_PINCTRL_V0) {
466 err = mtk_pinconf_bias_set_v0(dev, pin, disable, pullup, val);
468 err = mtk_pinconf_bias_set_v1(dev, pin, disable, pullup, val);
474 int mtk_pinconf_input_enable_v1(struct udevice *dev, u32 pin, u32 arg)
478 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_IES, 1);
481 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 0);
488 int mtk_pinconf_input_enable(struct udevice *dev, u32 pin, u32 arg)
490 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
491 struct mtk_io_type_desc io_type;
493 int rev = priv->soc->rev;
495 if (!mtk_get_pin_io_type(dev, pin, &io_type))
496 if (io_type.input_enable)
497 return io_type.input_enable(dev, pin, arg);
498 if (rev == MTK_PINCTRL_V1)
499 return mtk_pinconf_input_enable_v1(dev, pin, arg);
504 int mtk_pinconf_drive_set_v0(struct udevice *dev, u32 pin, u32 arg)
506 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
507 const struct mtk_pin_desc *desc = &priv->soc->pins[pin];
508 const struct mtk_drive_desc *tb;
511 tb = &mtk_drive[desc->drv_n];
512 /* 4mA when (e8, e4) = (0, 0)
513 * 8mA when (e8, e4) = (0, 1)
514 * 12mA when (e8, e4) = (1, 0)
515 * 16mA when (e8, e4) = (1, 1)
517 if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
518 arg = (arg / tb->step - 1) * tb->scal;
519 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_E4,
523 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_E8,
532 int mtk_pinconf_drive_set_v1(struct udevice *dev, u32 pin, u32 arg)
534 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
535 const struct mtk_pin_desc *desc = &priv->soc->pins[pin];
536 const struct mtk_drive_desc *tb;
539 tb = &mtk_drive[desc->drv_n];
540 if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
541 arg = (arg / tb->step - 1) * tb->scal;
542 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DRV, arg);
550 int mtk_pinconf_drive_set(struct udevice *dev, u32 pin, u32 arg)
553 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
554 struct mtk_io_type_desc io_type;
555 int rev = priv->soc->rev;
557 if (!mtk_get_pin_io_type(dev, pin, &io_type)) {
558 if (io_type.drive_set)
559 err = io_type.drive_set(dev, pin, arg);
562 } else if (rev == MTK_PINCTRL_V0) {
563 err = mtk_pinconf_drive_set_v0(dev, pin, arg);
565 err = mtk_pinconf_drive_set_v1(dev, pin, arg);
571 static int mtk_pinconf_set(struct udevice *dev, unsigned int pin,
572 unsigned int param, unsigned int arg)
577 case PIN_CONFIG_BIAS_DISABLE:
578 case PIN_CONFIG_BIAS_PULL_UP:
579 case PIN_CONFIG_BIAS_PULL_DOWN:
580 err = mtk_pinconf_bias_set(dev, pin, param, arg);
584 case PIN_CONFIG_OUTPUT_ENABLE:
585 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_SMT, 0);
588 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 1);
592 case PIN_CONFIG_INPUT_ENABLE:
593 err = mtk_pinconf_input_enable(dev, pin, param);
597 case PIN_CONFIG_OUTPUT:
598 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 1);
602 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DO, arg);
606 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
607 /* arg = 1: Input mode & SMT enable ;
608 * arg = 0: Output mode & SMT disable
611 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR,
616 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_SMT,
621 case PIN_CONFIG_DRIVE_STRENGTH:
622 err = mtk_pinconf_drive_set(dev, pin, arg);
636 static int mtk_pinconf_group_set(struct udevice *dev,
637 unsigned int group_selector,
638 unsigned int param, unsigned int arg)
640 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
641 const struct mtk_group_desc *grp =
642 &priv->soc->grps[group_selector];
645 for (i = 0; i < grp->num_pins; i++) {
646 ret = mtk_pinconf_set(dev, grp->pins[i], param, arg);
655 const struct pinctrl_ops mtk_pinctrl_ops = {
656 .get_pins_count = mtk_get_pins_count,
657 .get_pin_name = mtk_get_pin_name,
658 .get_pin_muxing = mtk_get_pin_muxing,
659 .get_groups_count = mtk_get_groups_count,
660 .get_group_name = mtk_get_group_name,
661 .get_functions_count = mtk_get_functions_count,
662 .get_function_name = mtk_get_function_name,
663 .pinmux_set = mtk_pinmux_set,
664 .pinmux_group_set = mtk_pinmux_group_set,
665 #if CONFIG_IS_ENABLED(PINCONF)
666 .pinconf_num_params = ARRAY_SIZE(mtk_conf_params),
667 .pinconf_params = mtk_conf_params,
668 .pinconf_set = mtk_pinconf_set,
669 .pinconf_group_set = mtk_pinconf_group_set,
671 .set_state = pinctrl_generic_set_state,
674 #if CONFIG_IS_ENABLED(DM_GPIO) || \
675 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO))
676 static int mtk_gpio_get(struct udevice *dev, unsigned int off)
680 err = mtk_hw_get_value(dev->parent, off, PINCTRL_PIN_REG_DI, &val);
687 static int mtk_gpio_set(struct udevice *dev, unsigned int off, int val)
689 return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DO, !!val);
692 static int mtk_gpio_get_direction(struct udevice *dev, unsigned int off)
696 err = mtk_hw_get_value(dev->parent, off, PINCTRL_PIN_REG_DIR, &val);
700 return val ? GPIOF_OUTPUT : GPIOF_INPUT;
703 static int mtk_gpio_direction_input(struct udevice *dev, unsigned int off)
705 return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DIR, 0);
708 static int mtk_gpio_direction_output(struct udevice *dev,
709 unsigned int off, int val)
711 mtk_gpio_set(dev, off, val);
713 /* And set the requested value */
714 return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DIR, 1);
717 static int mtk_gpio_request(struct udevice *dev, unsigned int off,
720 struct mtk_pinctrl_priv *priv = dev_get_priv(dev->parent);
722 return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_MODE,
723 priv->soc->gpio_mode);
726 static int mtk_gpio_probe(struct udevice *dev)
728 struct mtk_pinctrl_priv *priv = dev_get_priv(dev->parent);
729 struct gpio_dev_priv *uc_priv;
731 uc_priv = dev_get_uclass_priv(dev);
732 uc_priv->bank_name = priv->soc->name;
733 uc_priv->gpio_count = priv->soc->npins;
738 static const struct dm_gpio_ops mtk_gpio_ops = {
739 .request = mtk_gpio_request,
740 .set_value = mtk_gpio_set,
741 .get_value = mtk_gpio_get,
742 .get_function = mtk_gpio_get_direction,
743 .direction_input = mtk_gpio_direction_input,
744 .direction_output = mtk_gpio_direction_output,
747 static struct driver mtk_gpio_driver = {
748 .name = "mediatek_gpio",
750 .probe = mtk_gpio_probe,
751 .ops = &mtk_gpio_ops,
754 static int mtk_gpiochip_register(struct udevice *parent)
756 struct uclass_driver *drv;
761 drv = lists_uclass_lookup(UCLASS_GPIO);
766 dev_for_each_subnode(node, parent)
767 if (ofnode_read_bool(node, "gpio-controller")) {
775 ret = device_bind_with_driver_data(parent, &mtk_gpio_driver,
776 "mediatek_gpio", 0, node,
785 int mtk_pinctrl_common_probe(struct udevice *dev,
786 const struct mtk_pinctrl_soc *soc)
788 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
792 u32 base_calc = soc->base_calc;
793 u32 nbase_names = soc->nbase_names;
800 for (i = 0; i < nbase_names; i++) {
801 addr = devfdt_get_addr_index(dev, i);
802 if (addr == FDT_ADDR_T_NONE)
804 priv->base[i] = (void __iomem *)addr;
807 #if CONFIG_IS_ENABLED(DM_GPIO) || \
808 (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_GPIO))
809 ret = mtk_gpiochip_register(dev);