xref: /linux/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c (revision cafe19db7751269bf6b4dd2148cbfa9fbe91d651)
1a1a503a8SSean Wang // SPDX-License-Identifier: GPL-2.0
2a1a503a8SSean Wang /*
3a1a503a8SSean Wang  * Copyright (C) 2018 MediaTek Inc.
4a1a503a8SSean Wang  *
5a1a503a8SSean Wang  * Author: Sean Wang <sean.wang@mediatek.com>
6a1a503a8SSean Wang  *
7a1a503a8SSean Wang  */
8a1a503a8SSean Wang 
9*cafe19dbSLight Hsieh #include <dt-bindings/pinctrl/mt65xx.h>
10a1a503a8SSean Wang #include <linux/device.h>
11a1a503a8SSean Wang #include <linux/err.h>
1222d7fe49SLinus Walleij #include <linux/gpio/driver.h>
1389132dd8SSean Wang #include <linux/platform_device.h>
14a1a503a8SSean Wang #include <linux/io.h>
1589132dd8SSean Wang #include <linux/of_irq.h>
16a1a503a8SSean Wang 
1789132dd8SSean Wang #include "mtk-eint.h"
18a1a503a8SSean Wang #include "pinctrl-mtk-common-v2.h"
19a1a503a8SSean Wang 
20c2832197SSean Wang /**
21c2832197SSean Wang  * struct mtk_drive_desc - the structure that holds the information
22c2832197SSean Wang  *			    of the driving current
23c2832197SSean Wang  * @min:	the minimum current of this group
24c2832197SSean Wang  * @max:	the maximum current of this group
25c2832197SSean Wang  * @step:	the step current of this group
26c2832197SSean Wang  * @scal:	the weight factor
27c2832197SSean Wang  *
28c2832197SSean Wang  * formula: output = ((input) / step - 1) * scal
29c2832197SSean Wang  */
30c2832197SSean Wang struct mtk_drive_desc {
31c2832197SSean Wang 	u8 min;
32c2832197SSean Wang 	u8 max;
33c2832197SSean Wang 	u8 step;
34c2832197SSean Wang 	u8 scal;
35c2832197SSean Wang };
36c2832197SSean Wang 
37c2832197SSean Wang /* The groups of drive strength */
3807c6b037SWei Yongjun static const struct mtk_drive_desc mtk_drive[] = {
39c2832197SSean Wang 	[DRV_GRP0] = { 4, 16, 4, 1 },
40c2832197SSean Wang 	[DRV_GRP1] = { 4, 16, 4, 2 },
41c2832197SSean Wang 	[DRV_GRP2] = { 2, 8, 2, 1 },
42c2832197SSean Wang 	[DRV_GRP3] = { 2, 8, 2, 2 },
43c2832197SSean Wang 	[DRV_GRP4] = { 2, 16, 2, 1 },
44c2832197SSean Wang };
45c2832197SSean Wang 
462bc47dfeSSean Wang static void mtk_w32(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 val)
47a1a503a8SSean Wang {
482bc47dfeSSean Wang 	writel_relaxed(val, pctl->base[i] + reg);
49a1a503a8SSean Wang }
50a1a503a8SSean Wang 
512bc47dfeSSean Wang static u32 mtk_r32(struct mtk_pinctrl *pctl, u8 i, u32 reg)
52a1a503a8SSean Wang {
532bc47dfeSSean Wang 	return readl_relaxed(pctl->base[i] + reg);
54a1a503a8SSean Wang }
55a1a503a8SSean Wang 
562bc47dfeSSean Wang void mtk_rmw(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 mask, u32 set)
57a1a503a8SSean Wang {
58a1a503a8SSean Wang 	u32 val;
59a1a503a8SSean Wang 
602bc47dfeSSean Wang 	val = mtk_r32(pctl, i, reg);
61a1a503a8SSean Wang 	val &= ~mask;
62a1a503a8SSean Wang 	val |= set;
632bc47dfeSSean Wang 	mtk_w32(pctl, i, reg, val);
64a1a503a8SSean Wang }
65a1a503a8SSean Wang 
66ea051eb3SSean Wang static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw,
67ea051eb3SSean Wang 				   const struct mtk_pin_desc *desc,
689d9b171cSSean Wang 				   int field, struct mtk_pin_field *pfd)
69a1a503a8SSean Wang {
70a1a503a8SSean Wang 	const struct mtk_pin_field_calc *c, *e;
719d9b171cSSean Wang 	const struct mtk_pin_reg_calc *rc;
723de7deefSLight Hsieh 	int start = 0, end, check;
733de7deefSLight Hsieh 	bool found = false;
74a1a503a8SSean Wang 	u32 bits;
75a1a503a8SSean Wang 
769d9b171cSSean Wang 	if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) {
779d9b171cSSean Wang 		rc = &hw->soc->reg_cal[field];
789d9b171cSSean Wang 	} else {
799d9b171cSSean Wang 		dev_dbg(hw->dev,
803de7deefSLight Hsieh 			"Not support field %d for this soc\n", field);
819d9b171cSSean Wang 		return -ENOTSUPP;
829d9b171cSSean Wang 	}
839d9b171cSSean Wang 
843de7deefSLight Hsieh 	end = rc->nranges - 1;
85a1a503a8SSean Wang 	c = rc->range;
86a1a503a8SSean Wang 	e = c + rc->nranges;
87a1a503a8SSean Wang 
883de7deefSLight Hsieh 	while (start <= end) {
893de7deefSLight Hsieh 		check = (start + end) >> 1;
903de7deefSLight Hsieh 		if (desc->number >= rc->range[check].s_pin
913de7deefSLight Hsieh 		 && desc->number <= rc->range[check].e_pin) {
923de7deefSLight Hsieh 			found = true;
93a1a503a8SSean Wang 			break;
943de7deefSLight Hsieh 		} else if (start == end)
953de7deefSLight Hsieh 			break;
963de7deefSLight Hsieh 		else if (desc->number < rc->range[check].s_pin)
973de7deefSLight Hsieh 			end = check - 1;
983de7deefSLight Hsieh 		else
993de7deefSLight Hsieh 			start = check + 1;
100a1a503a8SSean Wang 	}
101a1a503a8SSean Wang 
1023de7deefSLight Hsieh 	if (!found) {
1039d9b171cSSean Wang 		dev_dbg(hw->dev, "Not support field %d for pin = %d (%s)\n",
1049d9b171cSSean Wang 			field, desc->number, desc->name);
1059d9b171cSSean Wang 		return -ENOTSUPP;
106a1a503a8SSean Wang 	}
107a1a503a8SSean Wang 
1083de7deefSLight Hsieh 	c = rc->range + check;
1093de7deefSLight Hsieh 
1102bc47dfeSSean Wang 	if (c->i_base > hw->nbase - 1) {
1119d9b171cSSean Wang 		dev_err(hw->dev,
1129d9b171cSSean Wang 			"Invalid base for field %d for pin = %d (%s)\n",
1139d9b171cSSean Wang 			field, desc->number, desc->name);
1142bc47dfeSSean Wang 		return -EINVAL;
1152bc47dfeSSean Wang 	}
1162bc47dfeSSean Wang 
117b906faf7SSean Wang 	/* Calculated bits as the overall offset the pin is located at,
118b906faf7SSean Wang 	 * if c->fixed is held, that determines the all the pins in the
119b906faf7SSean Wang 	 * range use the same field with the s_pin.
120b906faf7SSean Wang 	 */
121ea051eb3SSean Wang 	bits = c->fixed ? c->s_bit : c->s_bit +
122ea051eb3SSean Wang 	       (desc->number - c->s_pin) * (c->x_bits);
123a1a503a8SSean Wang 
124b906faf7SSean Wang 	/* Fill pfd from bits. For example 32-bit register applied is assumed
125b906faf7SSean Wang 	 * when c->sz_reg is equal to 32.
126b906faf7SSean Wang 	 */
1272bc47dfeSSean Wang 	pfd->index = c->i_base;
128b906faf7SSean Wang 	pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg);
129b906faf7SSean Wang 	pfd->bitpos = bits % c->sz_reg;
130a1a503a8SSean Wang 	pfd->mask = (1 << c->x_bits) - 1;
131a1a503a8SSean Wang 
132a1a503a8SSean Wang 	/* pfd->next is used for indicating that bit wrapping-around happens
133a1a503a8SSean Wang 	 * which requires the manipulation for bit 0 starting in the next
134a1a503a8SSean Wang 	 * register to form the complete field read/write.
135a1a503a8SSean Wang 	 */
136b906faf7SSean Wang 	pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0;
137a1a503a8SSean Wang 
138a1a503a8SSean Wang 	return 0;
139a1a503a8SSean Wang }
140a1a503a8SSean Wang 
141ea051eb3SSean Wang static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw,
142ea051eb3SSean Wang 				const struct mtk_pin_desc *desc,
143a1a503a8SSean Wang 				int field, struct mtk_pin_field *pfd)
144a1a503a8SSean Wang {
145a1a503a8SSean Wang 	if (field < 0 || field >= PINCTRL_PIN_REG_MAX) {
146a1a503a8SSean Wang 		dev_err(hw->dev, "Invalid Field %d\n", field);
147a1a503a8SSean Wang 		return -EINVAL;
148a1a503a8SSean Wang 	}
149a1a503a8SSean Wang 
1509d9b171cSSean Wang 	return mtk_hw_pin_field_lookup(hw, desc, field, pfd);
151a1a503a8SSean Wang }
152a1a503a8SSean Wang 
153a1a503a8SSean Wang static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
154a1a503a8SSean Wang {
155a1a503a8SSean Wang 	*l = 32 - pf->bitpos;
156a1a503a8SSean Wang 	*h = get_count_order(pf->mask) - *l;
157a1a503a8SSean Wang }
158a1a503a8SSean Wang 
159a1a503a8SSean Wang static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw,
160a1a503a8SSean Wang 				     struct mtk_pin_field *pf, int value)
161a1a503a8SSean Wang {
162a1a503a8SSean Wang 	int nbits_l, nbits_h;
163a1a503a8SSean Wang 
164a1a503a8SSean Wang 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
165a1a503a8SSean Wang 
1662bc47dfeSSean Wang 	mtk_rmw(hw, pf->index, pf->offset, pf->mask << pf->bitpos,
167a1a503a8SSean Wang 		(value & pf->mask) << pf->bitpos);
168a1a503a8SSean Wang 
1692bc47dfeSSean Wang 	mtk_rmw(hw, pf->index, pf->offset + pf->next, BIT(nbits_h) - 1,
170a1a503a8SSean Wang 		(value & pf->mask) >> nbits_l);
171a1a503a8SSean Wang }
172a1a503a8SSean Wang 
173a1a503a8SSean Wang static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw,
174a1a503a8SSean Wang 				    struct mtk_pin_field *pf, int *value)
175a1a503a8SSean Wang {
176a1a503a8SSean Wang 	int nbits_l, nbits_h, h, l;
177a1a503a8SSean Wang 
178a1a503a8SSean Wang 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
179a1a503a8SSean Wang 
1802bc47dfeSSean Wang 	l  = (mtk_r32(hw, pf->index, pf->offset)
1812bc47dfeSSean Wang 	      >> pf->bitpos) & (BIT(nbits_l) - 1);
1822bc47dfeSSean Wang 	h  = (mtk_r32(hw, pf->index, pf->offset + pf->next))
1832bc47dfeSSean Wang 	      & (BIT(nbits_h) - 1);
184a1a503a8SSean Wang 
185a1a503a8SSean Wang 	*value = (h << nbits_l) | l;
186a1a503a8SSean Wang }
187a1a503a8SSean Wang 
188ea051eb3SSean Wang int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
189ea051eb3SSean Wang 		     int field, int value)
190a1a503a8SSean Wang {
191a1a503a8SSean Wang 	struct mtk_pin_field pf;
192a1a503a8SSean Wang 	int err;
193a1a503a8SSean Wang 
194ea051eb3SSean Wang 	err = mtk_hw_pin_field_get(hw, desc, field, &pf);
195a1a503a8SSean Wang 	if (err)
196a1a503a8SSean Wang 		return err;
197a1a503a8SSean Wang 
1983de7deefSLight Hsieh 	if (value < 0 || value > pf.mask)
1993de7deefSLight Hsieh 		return -EINVAL;
2003de7deefSLight Hsieh 
201a1a503a8SSean Wang 	if (!pf.next)
2022bc47dfeSSean Wang 		mtk_rmw(hw, pf.index, pf.offset, pf.mask << pf.bitpos,
203a1a503a8SSean Wang 			(value & pf.mask) << pf.bitpos);
204a1a503a8SSean Wang 	else
205a1a503a8SSean Wang 		mtk_hw_write_cross_field(hw, &pf, value);
206a1a503a8SSean Wang 
207a1a503a8SSean Wang 	return 0;
208a1a503a8SSean Wang }
209a1a503a8SSean Wang 
210ea051eb3SSean Wang int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
211ea051eb3SSean Wang 		     int field, int *value)
212a1a503a8SSean Wang {
213a1a503a8SSean Wang 	struct mtk_pin_field pf;
214a1a503a8SSean Wang 	int err;
215a1a503a8SSean Wang 
216ea051eb3SSean Wang 	err = mtk_hw_pin_field_get(hw, desc, field, &pf);
217a1a503a8SSean Wang 	if (err)
218a1a503a8SSean Wang 		return err;
219a1a503a8SSean Wang 
220a1a503a8SSean Wang 	if (!pf.next)
2212bc47dfeSSean Wang 		*value = (mtk_r32(hw, pf.index, pf.offset)
2222bc47dfeSSean Wang 			  >> pf.bitpos) & pf.mask;
223a1a503a8SSean Wang 	else
224a1a503a8SSean Wang 		mtk_hw_read_cross_field(hw, &pf, value);
225a1a503a8SSean Wang 
226a1a503a8SSean Wang 	return 0;
227a1a503a8SSean Wang }
228c2832197SSean Wang 
22989132dd8SSean Wang static int mtk_xt_find_eint_num(struct mtk_pinctrl *hw, unsigned long eint_n)
23089132dd8SSean Wang {
23189132dd8SSean Wang 	const struct mtk_pin_desc *desc;
23289132dd8SSean Wang 	int i = 0;
23389132dd8SSean Wang 
23489132dd8SSean Wang 	desc = (const struct mtk_pin_desc *)hw->soc->pins;
23589132dd8SSean Wang 
23689132dd8SSean Wang 	while (i < hw->soc->npins) {
23789132dd8SSean Wang 		if (desc[i].eint.eint_n == eint_n)
23889132dd8SSean Wang 			return desc[i].number;
23989132dd8SSean Wang 		i++;
24089132dd8SSean Wang 	}
24189132dd8SSean Wang 
24289132dd8SSean Wang 	return EINT_NA;
24389132dd8SSean Wang }
24489132dd8SSean Wang 
24589132dd8SSean Wang static int mtk_xt_get_gpio_n(void *data, unsigned long eint_n,
24689132dd8SSean Wang 			     unsigned int *gpio_n,
24789132dd8SSean Wang 			     struct gpio_chip **gpio_chip)
24889132dd8SSean Wang {
24989132dd8SSean Wang 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
25089132dd8SSean Wang 	const struct mtk_pin_desc *desc;
25189132dd8SSean Wang 
25289132dd8SSean Wang 	desc = (const struct mtk_pin_desc *)hw->soc->pins;
25389132dd8SSean Wang 	*gpio_chip = &hw->chip;
25489132dd8SSean Wang 
25589132dd8SSean Wang 	/* Be greedy to guess first gpio_n is equal to eint_n */
25689132dd8SSean Wang 	if (desc[eint_n].eint.eint_n == eint_n)
25789132dd8SSean Wang 		*gpio_n = eint_n;
25889132dd8SSean Wang 	else
25989132dd8SSean Wang 		*gpio_n = mtk_xt_find_eint_num(hw, eint_n);
26089132dd8SSean Wang 
26189132dd8SSean Wang 	return *gpio_n == EINT_NA ? -EINVAL : 0;
26289132dd8SSean Wang }
26389132dd8SSean Wang 
26489132dd8SSean Wang static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n)
26589132dd8SSean Wang {
26689132dd8SSean Wang 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
26789132dd8SSean Wang 	const struct mtk_pin_desc *desc;
26889132dd8SSean Wang 	struct gpio_chip *gpio_chip;
26989132dd8SSean Wang 	unsigned int gpio_n;
27089132dd8SSean Wang 	int value, err;
27189132dd8SSean Wang 
27289132dd8SSean Wang 	err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
27389132dd8SSean Wang 	if (err)
27489132dd8SSean Wang 		return err;
27589132dd8SSean Wang 
27689132dd8SSean Wang 	desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
27789132dd8SSean Wang 
27889132dd8SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value);
27989132dd8SSean Wang 	if (err)
28089132dd8SSean Wang 		return err;
28189132dd8SSean Wang 
28289132dd8SSean Wang 	return !!value;
28389132dd8SSean Wang }
28489132dd8SSean Wang 
28589132dd8SSean Wang static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n)
28689132dd8SSean Wang {
28789132dd8SSean Wang 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
28889132dd8SSean Wang 	const struct mtk_pin_desc *desc;
28989132dd8SSean Wang 	struct gpio_chip *gpio_chip;
29089132dd8SSean Wang 	unsigned int gpio_n;
29189132dd8SSean Wang 	int err;
29289132dd8SSean Wang 
29389132dd8SSean Wang 	err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
29489132dd8SSean Wang 	if (err)
29589132dd8SSean Wang 		return err;
29689132dd8SSean Wang 
29789132dd8SSean Wang 	desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
29889132dd8SSean Wang 
29989132dd8SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE,
30089132dd8SSean Wang 			       desc->eint.eint_m);
30189132dd8SSean Wang 	if (err)
30289132dd8SSean Wang 		return err;
30389132dd8SSean Wang 
30489132dd8SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, MTK_INPUT);
30589132dd8SSean Wang 	if (err)
30689132dd8SSean Wang 		return err;
30789132dd8SSean Wang 
30889132dd8SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, MTK_ENABLE);
3096e737a4eSchuanjia.liu 	/* SMT is supposed to be supported by every real GPIO and doesn't
3106e737a4eSchuanjia.liu 	 * support virtual GPIOs, so the extra condition err != -ENOTSUPP
3116e737a4eSchuanjia.liu 	 * is just for adding EINT support to these virtual GPIOs. It should
3126e737a4eSchuanjia.liu 	 * add an extra flag in the pin descriptor when more pins with
3136e737a4eSchuanjia.liu 	 * distinctive characteristic come out.
3146e737a4eSchuanjia.liu 	 */
3156e737a4eSchuanjia.liu 	if (err && err != -ENOTSUPP)
31689132dd8SSean Wang 		return err;
31789132dd8SSean Wang 
31889132dd8SSean Wang 	return 0;
31989132dd8SSean Wang }
32089132dd8SSean Wang 
32189132dd8SSean Wang static const struct mtk_eint_xt mtk_eint_xt = {
32289132dd8SSean Wang 	.get_gpio_n = mtk_xt_get_gpio_n,
32389132dd8SSean Wang 	.get_gpio_state = mtk_xt_get_gpio_state,
32489132dd8SSean Wang 	.set_gpio_as_eint = mtk_xt_set_gpio_as_eint,
32589132dd8SSean Wang };
32689132dd8SSean Wang 
32789132dd8SSean Wang int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev)
32889132dd8SSean Wang {
32989132dd8SSean Wang 	struct device_node *np = pdev->dev.of_node;
33089132dd8SSean Wang 	struct resource *res;
33189132dd8SSean Wang 
33289132dd8SSean Wang 	if (!IS_ENABLED(CONFIG_EINT_MTK))
33389132dd8SSean Wang 		return 0;
33489132dd8SSean Wang 
33589132dd8SSean Wang 	if (!of_property_read_bool(np, "interrupt-controller"))
33689132dd8SSean Wang 		return -ENODEV;
33789132dd8SSean Wang 
33889132dd8SSean Wang 	hw->eint = devm_kzalloc(hw->dev, sizeof(*hw->eint), GFP_KERNEL);
33989132dd8SSean Wang 	if (!hw->eint)
34089132dd8SSean Wang 		return -ENOMEM;
34189132dd8SSean Wang 
34289132dd8SSean Wang 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eint");
34389132dd8SSean Wang 	if (!res) {
34489132dd8SSean Wang 		dev_err(&pdev->dev, "Unable to get eint resource\n");
34589132dd8SSean Wang 		return -ENODEV;
34689132dd8SSean Wang 	}
34789132dd8SSean Wang 
34889132dd8SSean Wang 	hw->eint->base = devm_ioremap_resource(&pdev->dev, res);
34989132dd8SSean Wang 	if (IS_ERR(hw->eint->base))
35089132dd8SSean Wang 		return PTR_ERR(hw->eint->base);
35189132dd8SSean Wang 
35289132dd8SSean Wang 	hw->eint->irq = irq_of_parse_and_map(np, 0);
35389132dd8SSean Wang 	if (!hw->eint->irq)
35489132dd8SSean Wang 		return -EINVAL;
35589132dd8SSean Wang 
35689132dd8SSean Wang 	if (!hw->soc->eint_hw)
35789132dd8SSean Wang 		return -ENODEV;
35889132dd8SSean Wang 
35989132dd8SSean Wang 	hw->eint->dev = &pdev->dev;
36089132dd8SSean Wang 	hw->eint->hw = hw->soc->eint_hw;
36189132dd8SSean Wang 	hw->eint->pctl = hw;
36289132dd8SSean Wang 	hw->eint->gpio_xlate = &mtk_eint_xt;
36389132dd8SSean Wang 
36489132dd8SSean Wang 	return mtk_eint_do_init(hw->eint);
36589132dd8SSean Wang }
36689132dd8SSean Wang 
3679afc305bSSean Wang /* Revision 0 */
36885430152SSean Wang int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
36985430152SSean Wang 				 const struct mtk_pin_desc *desc)
37085430152SSean Wang {
37185430152SSean Wang 	int err;
37285430152SSean Wang 
373ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU,
37485430152SSean Wang 			       MTK_DISABLE);
37585430152SSean Wang 	if (err)
37685430152SSean Wang 		return err;
37785430152SSean Wang 
378ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
37985430152SSean Wang 			       MTK_DISABLE);
38085430152SSean Wang 	if (err)
38185430152SSean Wang 		return err;
38285430152SSean Wang 
38385430152SSean Wang 	return 0;
38485430152SSean Wang }
38585430152SSean Wang 
38685430152SSean Wang int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
38785430152SSean Wang 				 const struct mtk_pin_desc *desc, int *res)
38885430152SSean Wang {
38985430152SSean Wang 	int v, v2;
39085430152SSean Wang 	int err;
39185430152SSean Wang 
392ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &v);
39385430152SSean Wang 	if (err)
39485430152SSean Wang 		return err;
39585430152SSean Wang 
396ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &v2);
39785430152SSean Wang 	if (err)
39885430152SSean Wang 		return err;
39985430152SSean Wang 
40085430152SSean Wang 	if (v == MTK_ENABLE || v2 == MTK_ENABLE)
40185430152SSean Wang 		return -EINVAL;
40285430152SSean Wang 
40385430152SSean Wang 	*res = 1;
40485430152SSean Wang 
40585430152SSean Wang 	return 0;
40685430152SSean Wang }
40785430152SSean Wang 
40885430152SSean Wang int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
40985430152SSean Wang 			 const struct mtk_pin_desc *desc, bool pullup)
41085430152SSean Wang {
41185430152SSean Wang 	int err, arg;
41285430152SSean Wang 
41385430152SSean Wang 	arg = pullup ? 1 : 2;
41485430152SSean Wang 
415ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU, arg & 1);
41685430152SSean Wang 	if (err)
41785430152SSean Wang 		return err;
41885430152SSean Wang 
419ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
42085430152SSean Wang 			       !!(arg & 2));
42185430152SSean Wang 	if (err)
42285430152SSean Wang 		return err;
42385430152SSean Wang 
42485430152SSean Wang 	return 0;
42585430152SSean Wang }
42685430152SSean Wang 
42785430152SSean Wang int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
42885430152SSean Wang 			 const struct mtk_pin_desc *desc, bool pullup, int *res)
42985430152SSean Wang {
43085430152SSean Wang 	int reg, err, v;
43185430152SSean Wang 
43285430152SSean Wang 	reg = pullup ? PINCTRL_PIN_REG_PU : PINCTRL_PIN_REG_PD;
43385430152SSean Wang 
434ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, reg, &v);
43585430152SSean Wang 	if (err)
43685430152SSean Wang 		return err;
43785430152SSean Wang 
43885430152SSean Wang 	if (!v)
43985430152SSean Wang 		return -EINVAL;
44085430152SSean Wang 
44185430152SSean Wang 	*res = 1;
44285430152SSean Wang 
44385430152SSean Wang 	return 0;
44485430152SSean Wang }
44585430152SSean Wang 
4469afc305bSSean Wang /* Revision 1 */
4479afc305bSSean Wang int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
4489afc305bSSean Wang 				      const struct mtk_pin_desc *desc)
4499afc305bSSean Wang {
4509afc305bSSean Wang 	int err;
4519afc305bSSean Wang 
452ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
4539afc305bSSean Wang 			       MTK_DISABLE);
4549afc305bSSean Wang 	if (err)
4559afc305bSSean Wang 		return err;
4569afc305bSSean Wang 
4579afc305bSSean Wang 	return 0;
4589afc305bSSean Wang }
4599afc305bSSean Wang 
4609afc305bSSean Wang int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
4619afc305bSSean Wang 				      const struct mtk_pin_desc *desc, int *res)
4629afc305bSSean Wang {
4639afc305bSSean Wang 	int v, err;
4649afc305bSSean Wang 
465ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
4669afc305bSSean Wang 	if (err)
4679afc305bSSean Wang 		return err;
4689afc305bSSean Wang 
4699afc305bSSean Wang 	if (v == MTK_ENABLE)
4709afc305bSSean Wang 		return -EINVAL;
4719afc305bSSean Wang 
4729afc305bSSean Wang 	*res = 1;
4739afc305bSSean Wang 
4749afc305bSSean Wang 	return 0;
4759afc305bSSean Wang }
4769afc305bSSean Wang 
4779afc305bSSean Wang int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
4789afc305bSSean Wang 			      const struct mtk_pin_desc *desc, bool pullup)
4799afc305bSSean Wang {
4809afc305bSSean Wang 	int err, arg;
4819afc305bSSean Wang 
4829afc305bSSean Wang 	arg = pullup ? MTK_PULLUP : MTK_PULLDOWN;
4839afc305bSSean Wang 
484ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
4859afc305bSSean Wang 			       MTK_ENABLE);
4869afc305bSSean Wang 	if (err)
4879afc305bSSean Wang 		return err;
4889afc305bSSean Wang 
489ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, arg);
4909afc305bSSean Wang 	if (err)
4919afc305bSSean Wang 		return err;
4929afc305bSSean Wang 
4939afc305bSSean Wang 	return 0;
4949afc305bSSean Wang }
4959afc305bSSean Wang 
4969afc305bSSean Wang int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
4979afc305bSSean Wang 			      const struct mtk_pin_desc *desc, bool pullup,
4989afc305bSSean Wang 			      int *res)
4999afc305bSSean Wang {
5009afc305bSSean Wang 	int err, v;
5019afc305bSSean Wang 
502ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
5039afc305bSSean Wang 	if (err)
5049afc305bSSean Wang 		return err;
5059afc305bSSean Wang 
5069afc305bSSean Wang 	if (v == MTK_DISABLE)
5079afc305bSSean Wang 		return -EINVAL;
5089afc305bSSean Wang 
509ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, &v);
5109afc305bSSean Wang 	if (err)
5119afc305bSSean Wang 		return err;
5129afc305bSSean Wang 
5139afc305bSSean Wang 	if (pullup ^ (v == MTK_PULLUP))
5149afc305bSSean Wang 		return -EINVAL;
5159afc305bSSean Wang 
5169afc305bSSean Wang 	*res = 1;
5179afc305bSSean Wang 
5189afc305bSSean Wang 	return 0;
5199afc305bSSean Wang }
5209afc305bSSean Wang 
521*cafe19dbSLight Hsieh /* Combo for the following pull register type:
522*cafe19dbSLight Hsieh  * 1. PU + PD
523*cafe19dbSLight Hsieh  * 2. PULLSEL + PULLEN
524*cafe19dbSLight Hsieh  * 3. PUPD + R0 + R1
525*cafe19dbSLight Hsieh  */
526*cafe19dbSLight Hsieh static int mtk_pinconf_bias_set_pu_pd(struct mtk_pinctrl *hw,
527*cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
528*cafe19dbSLight Hsieh 				u32 pullup, u32 arg)
529*cafe19dbSLight Hsieh {
530*cafe19dbSLight Hsieh 	int err, pu, pd;
531*cafe19dbSLight Hsieh 
532*cafe19dbSLight Hsieh 	if (arg == MTK_DISABLE) {
533*cafe19dbSLight Hsieh 		pu = 0;
534*cafe19dbSLight Hsieh 		pd = 0;
535*cafe19dbSLight Hsieh 	} else if ((arg == MTK_ENABLE) && pullup) {
536*cafe19dbSLight Hsieh 		pu = 1;
537*cafe19dbSLight Hsieh 		pd = 0;
538*cafe19dbSLight Hsieh 	} else if ((arg == MTK_ENABLE) && !pullup) {
539*cafe19dbSLight Hsieh 		pu = 0;
540*cafe19dbSLight Hsieh 		pd = 1;
541*cafe19dbSLight Hsieh 	} else {
542*cafe19dbSLight Hsieh 		err = -EINVAL;
543*cafe19dbSLight Hsieh 		goto out;
544*cafe19dbSLight Hsieh 	}
545*cafe19dbSLight Hsieh 
546*cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU, pu);
547*cafe19dbSLight Hsieh 	if (err)
548*cafe19dbSLight Hsieh 		goto out;
549*cafe19dbSLight Hsieh 
550*cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD, pd);
551*cafe19dbSLight Hsieh 
552*cafe19dbSLight Hsieh out:
553*cafe19dbSLight Hsieh 	return err;
554*cafe19dbSLight Hsieh }
555*cafe19dbSLight Hsieh 
556*cafe19dbSLight Hsieh static int mtk_pinconf_bias_set_pullsel_pullen(struct mtk_pinctrl *hw,
557*cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
558*cafe19dbSLight Hsieh 				u32 pullup, u32 arg)
559*cafe19dbSLight Hsieh {
560*cafe19dbSLight Hsieh 	int err, enable;
561*cafe19dbSLight Hsieh 
562*cafe19dbSLight Hsieh 	if (arg == MTK_DISABLE)
563*cafe19dbSLight Hsieh 		enable = 0;
564*cafe19dbSLight Hsieh 	else if (arg == MTK_ENABLE)
565*cafe19dbSLight Hsieh 		enable = 1;
566*cafe19dbSLight Hsieh 	else {
567*cafe19dbSLight Hsieh 		err = -EINVAL;
568*cafe19dbSLight Hsieh 		goto out;
569*cafe19dbSLight Hsieh 	}
570*cafe19dbSLight Hsieh 
571*cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN, enable);
572*cafe19dbSLight Hsieh 	if (err)
573*cafe19dbSLight Hsieh 		goto out;
574*cafe19dbSLight Hsieh 
575*cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, pullup);
576*cafe19dbSLight Hsieh 
577*cafe19dbSLight Hsieh out:
578*cafe19dbSLight Hsieh 	return err;
579*cafe19dbSLight Hsieh }
580*cafe19dbSLight Hsieh 
581*cafe19dbSLight Hsieh static int mtk_pinconf_bias_set_pupd_r1_r0(struct mtk_pinctrl *hw,
582*cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
583*cafe19dbSLight Hsieh 				u32 pullup, u32 arg)
584*cafe19dbSLight Hsieh {
585*cafe19dbSLight Hsieh 	int err, r0, r1;
586*cafe19dbSLight Hsieh 
587*cafe19dbSLight Hsieh 	if ((arg == MTK_DISABLE) || (arg == MTK_PUPD_SET_R1R0_00)) {
588*cafe19dbSLight Hsieh 		pullup = 0;
589*cafe19dbSLight Hsieh 		r0 = 0;
590*cafe19dbSLight Hsieh 		r1 = 0;
591*cafe19dbSLight Hsieh 	} else if (arg == MTK_PUPD_SET_R1R0_01) {
592*cafe19dbSLight Hsieh 		r0 = 1;
593*cafe19dbSLight Hsieh 		r1 = 0;
594*cafe19dbSLight Hsieh 	} else if (arg == MTK_PUPD_SET_R1R0_10) {
595*cafe19dbSLight Hsieh 		r0 = 0;
596*cafe19dbSLight Hsieh 		r1 = 1;
597*cafe19dbSLight Hsieh 	} else if (arg == MTK_PUPD_SET_R1R0_11) {
598*cafe19dbSLight Hsieh 		r0 = 1;
599*cafe19dbSLight Hsieh 		r1 = 1;
600*cafe19dbSLight Hsieh 	} else {
601*cafe19dbSLight Hsieh 		err = -EINVAL;
602*cafe19dbSLight Hsieh 		goto out;
603*cafe19dbSLight Hsieh 	}
604*cafe19dbSLight Hsieh 
605*cafe19dbSLight Hsieh 	/* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
606*cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PUPD, !pullup);
607*cafe19dbSLight Hsieh 	if (err)
608*cafe19dbSLight Hsieh 		goto out;
609*cafe19dbSLight Hsieh 
610*cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R0, r0);
611*cafe19dbSLight Hsieh 	if (err)
612*cafe19dbSLight Hsieh 		goto out;
613*cafe19dbSLight Hsieh 
614*cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R1, r1);
615*cafe19dbSLight Hsieh 
616*cafe19dbSLight Hsieh out:
617*cafe19dbSLight Hsieh 	return err;
618*cafe19dbSLight Hsieh }
619*cafe19dbSLight Hsieh 
620*cafe19dbSLight Hsieh static int mtk_pinconf_bias_get_pu_pd(struct mtk_pinctrl *hw,
621*cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
622*cafe19dbSLight Hsieh 				u32 *pullup, u32 *enable)
623*cafe19dbSLight Hsieh {
624*cafe19dbSLight Hsieh 	int err, pu, pd;
625*cafe19dbSLight Hsieh 
626*cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &pu);
627*cafe19dbSLight Hsieh 	if (err)
628*cafe19dbSLight Hsieh 		goto out;
629*cafe19dbSLight Hsieh 
630*cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &pd);
631*cafe19dbSLight Hsieh 	if (err)
632*cafe19dbSLight Hsieh 		goto out;
633*cafe19dbSLight Hsieh 
634*cafe19dbSLight Hsieh 	if (pu == 0 && pd == 0) {
635*cafe19dbSLight Hsieh 		*pullup = 0;
636*cafe19dbSLight Hsieh 		*enable = MTK_DISABLE;
637*cafe19dbSLight Hsieh 	} else if (pu == 1 && pd == 0) {
638*cafe19dbSLight Hsieh 		*pullup = 1;
639*cafe19dbSLight Hsieh 		*enable = MTK_ENABLE;
640*cafe19dbSLight Hsieh 	} else if (pu == 0 && pd == 1) {
641*cafe19dbSLight Hsieh 		*pullup = 0;
642*cafe19dbSLight Hsieh 		*enable = MTK_ENABLE;
643*cafe19dbSLight Hsieh 	} else
644*cafe19dbSLight Hsieh 		err = -EINVAL;
645*cafe19dbSLight Hsieh 
646*cafe19dbSLight Hsieh out:
647*cafe19dbSLight Hsieh 	return err;
648*cafe19dbSLight Hsieh }
649*cafe19dbSLight Hsieh 
650*cafe19dbSLight Hsieh static int mtk_pinconf_bias_get_pullsel_pullen(struct mtk_pinctrl *hw,
651*cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
652*cafe19dbSLight Hsieh 				u32 *pullup, u32 *enable)
653*cafe19dbSLight Hsieh {
654*cafe19dbSLight Hsieh 	int err;
655*cafe19dbSLight Hsieh 
656*cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, pullup);
657*cafe19dbSLight Hsieh 	if (err)
658*cafe19dbSLight Hsieh 		goto out;
659*cafe19dbSLight Hsieh 
660*cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, enable);
661*cafe19dbSLight Hsieh 
662*cafe19dbSLight Hsieh out:
663*cafe19dbSLight Hsieh 	return err;
664*cafe19dbSLight Hsieh }
665*cafe19dbSLight Hsieh 
666*cafe19dbSLight Hsieh static int mtk_pinconf_bias_get_pupd_r1_r0(struct mtk_pinctrl *hw,
667*cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
668*cafe19dbSLight Hsieh 				u32 *pullup, u32 *enable)
669*cafe19dbSLight Hsieh {
670*cafe19dbSLight Hsieh 	int err, r0, r1;
671*cafe19dbSLight Hsieh 
672*cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PUPD, pullup);
673*cafe19dbSLight Hsieh 	if (err)
674*cafe19dbSLight Hsieh 		goto out;
675*cafe19dbSLight Hsieh 	/* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
676*cafe19dbSLight Hsieh 	*pullup = !(*pullup);
677*cafe19dbSLight Hsieh 
678*cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R0, &r0);
679*cafe19dbSLight Hsieh 	if (err)
680*cafe19dbSLight Hsieh 		goto out;
681*cafe19dbSLight Hsieh 
682*cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R1, &r1);
683*cafe19dbSLight Hsieh 	if (err)
684*cafe19dbSLight Hsieh 		goto out;
685*cafe19dbSLight Hsieh 
686*cafe19dbSLight Hsieh 	if ((r1 == 0) && (r0 == 0))
687*cafe19dbSLight Hsieh 		*enable = MTK_PUPD_SET_R1R0_00;
688*cafe19dbSLight Hsieh 	else if ((r1 == 0) && (r0 == 1))
689*cafe19dbSLight Hsieh 		*enable = MTK_PUPD_SET_R1R0_01;
690*cafe19dbSLight Hsieh 	else if ((r1 == 1) && (r0 == 0))
691*cafe19dbSLight Hsieh 		*enable = MTK_PUPD_SET_R1R0_10;
692*cafe19dbSLight Hsieh 	else if ((r1 == 1) && (r0 == 1))
693*cafe19dbSLight Hsieh 		*enable = MTK_PUPD_SET_R1R0_11;
694*cafe19dbSLight Hsieh 	else
695*cafe19dbSLight Hsieh 		err = -EINVAL;
696*cafe19dbSLight Hsieh 
697*cafe19dbSLight Hsieh out:
698*cafe19dbSLight Hsieh 	return err;
699*cafe19dbSLight Hsieh }
700*cafe19dbSLight Hsieh 
701*cafe19dbSLight Hsieh int mtk_pinconf_bias_set_combo(struct mtk_pinctrl *hw,
702*cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
703*cafe19dbSLight Hsieh 				u32 pullup, u32 arg)
704*cafe19dbSLight Hsieh {
705*cafe19dbSLight Hsieh 	int err;
706*cafe19dbSLight Hsieh 
707*cafe19dbSLight Hsieh 	err = mtk_pinconf_bias_set_pu_pd(hw, desc, pullup, arg);
708*cafe19dbSLight Hsieh 	if (!err)
709*cafe19dbSLight Hsieh 		goto out;
710*cafe19dbSLight Hsieh 
711*cafe19dbSLight Hsieh 	err = mtk_pinconf_bias_set_pullsel_pullen(hw, desc, pullup, arg);
712*cafe19dbSLight Hsieh 	if (!err)
713*cafe19dbSLight Hsieh 		goto out;
714*cafe19dbSLight Hsieh 
715*cafe19dbSLight Hsieh 	err = mtk_pinconf_bias_set_pupd_r1_r0(hw, desc, pullup, arg);
716*cafe19dbSLight Hsieh 
717*cafe19dbSLight Hsieh out:
718*cafe19dbSLight Hsieh 	return err;
719*cafe19dbSLight Hsieh }
720*cafe19dbSLight Hsieh 
721*cafe19dbSLight Hsieh int mtk_pinconf_bias_get_combo(struct mtk_pinctrl *hw,
722*cafe19dbSLight Hsieh 			      const struct mtk_pin_desc *desc,
723*cafe19dbSLight Hsieh 			      u32 *pullup, u32 *enable)
724*cafe19dbSLight Hsieh {
725*cafe19dbSLight Hsieh 	int err;
726*cafe19dbSLight Hsieh 
727*cafe19dbSLight Hsieh 	err = mtk_pinconf_bias_get_pu_pd(hw, desc, pullup, enable);
728*cafe19dbSLight Hsieh 	if (!err)
729*cafe19dbSLight Hsieh 		goto out;
730*cafe19dbSLight Hsieh 
731*cafe19dbSLight Hsieh 	err = mtk_pinconf_bias_get_pullsel_pullen(hw, desc, pullup, enable);
732*cafe19dbSLight Hsieh 	if (!err)
733*cafe19dbSLight Hsieh 		goto out;
734*cafe19dbSLight Hsieh 
735*cafe19dbSLight Hsieh 	err = mtk_pinconf_bias_get_pupd_r1_r0(hw, desc, pullup, enable);
736*cafe19dbSLight Hsieh 
737*cafe19dbSLight Hsieh out:
738*cafe19dbSLight Hsieh 	return err;
739*cafe19dbSLight Hsieh }
740*cafe19dbSLight Hsieh 
741c2832197SSean Wang /* Revision 0 */
742c2832197SSean Wang int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
743c2832197SSean Wang 			  const struct mtk_pin_desc *desc, u32 arg)
744c2832197SSean Wang {
745c2832197SSean Wang 	const struct mtk_drive_desc *tb;
746c2832197SSean Wang 	int err = -ENOTSUPP;
747c2832197SSean Wang 
748c2832197SSean Wang 	tb = &mtk_drive[desc->drv_n];
749c2832197SSean Wang 	/* 4mA when (e8, e4) = (0, 0)
750c2832197SSean Wang 	 * 8mA when (e8, e4) = (0, 1)
751c2832197SSean Wang 	 * 12mA when (e8, e4) = (1, 0)
752c2832197SSean Wang 	 * 16mA when (e8, e4) = (1, 1)
753c2832197SSean Wang 	 */
754c2832197SSean Wang 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
755c2832197SSean Wang 		arg = (arg / tb->step - 1) * tb->scal;
756ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E4,
757c2832197SSean Wang 				       arg & 0x1);
758c2832197SSean Wang 		if (err)
759c2832197SSean Wang 			return err;
760c2832197SSean Wang 
761ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E8,
762c2832197SSean Wang 				       (arg & 0x2) >> 1);
763c2832197SSean Wang 		if (err)
764c2832197SSean Wang 			return err;
765c2832197SSean Wang 	}
766c2832197SSean Wang 
767c2832197SSean Wang 	return err;
768c2832197SSean Wang }
769c2832197SSean Wang 
770c2832197SSean Wang int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
771c2832197SSean Wang 			  const struct mtk_pin_desc *desc, int *val)
772c2832197SSean Wang {
773c2832197SSean Wang 	const struct mtk_drive_desc *tb;
774c2832197SSean Wang 	int err, val1, val2;
775c2832197SSean Wang 
776c2832197SSean Wang 	tb = &mtk_drive[desc->drv_n];
777c2832197SSean Wang 
778ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E4, &val1);
779c2832197SSean Wang 	if (err)
780c2832197SSean Wang 		return err;
781c2832197SSean Wang 
782ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E8, &val2);
783c2832197SSean Wang 	if (err)
784c2832197SSean Wang 		return err;
785c2832197SSean Wang 
786c2832197SSean Wang 	/* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1)
787c2832197SSean Wang 	 * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1)
788c2832197SSean Wang 	 */
789c2832197SSean Wang 	*val = (((val2 << 1) + val1) / tb->scal + 1) * tb->step;
790c2832197SSean Wang 
791c2832197SSean Wang 	return 0;
792c2832197SSean Wang }
7933ad38a14SSean Wang 
7943ad38a14SSean Wang /* Revision 1 */
7953ad38a14SSean Wang int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
7963ad38a14SSean Wang 			       const struct mtk_pin_desc *desc, u32 arg)
7973ad38a14SSean Wang {
7983ad38a14SSean Wang 	const struct mtk_drive_desc *tb;
7993ad38a14SSean Wang 	int err = -ENOTSUPP;
8003ad38a14SSean Wang 
8013ad38a14SSean Wang 	tb = &mtk_drive[desc->drv_n];
8023ad38a14SSean Wang 
8033ad38a14SSean Wang 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
8043ad38a14SSean Wang 		arg = (arg / tb->step - 1) * tb->scal;
8053ad38a14SSean Wang 
806ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV,
8073ad38a14SSean Wang 				       arg);
8083ad38a14SSean Wang 		if (err)
8093ad38a14SSean Wang 			return err;
8103ad38a14SSean Wang 	}
8113ad38a14SSean Wang 
8123ad38a14SSean Wang 	return err;
8133ad38a14SSean Wang }
8143ad38a14SSean Wang 
8153ad38a14SSean Wang int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
8163ad38a14SSean Wang 			       const struct mtk_pin_desc *desc, int *val)
8173ad38a14SSean Wang {
8183ad38a14SSean Wang 	const struct mtk_drive_desc *tb;
8193ad38a14SSean Wang 	int err, val1;
8203ad38a14SSean Wang 
8213ad38a14SSean Wang 	tb = &mtk_drive[desc->drv_n];
8223ad38a14SSean Wang 
823ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, &val1);
8243ad38a14SSean Wang 	if (err)
8253ad38a14SSean Wang 		return err;
8263ad38a14SSean Wang 
8273ad38a14SSean Wang 	*val = ((val1 & 0x7) / tb->scal + 1) * tb->step;
8283ad38a14SSean Wang 
8293ad38a14SSean Wang 	return 0;
8303ad38a14SSean Wang }
8310d7ca772SSean Wang 
8325f755e1fSLight Hsieh int mtk_pinconf_drive_set_raw(struct mtk_pinctrl *hw,
8335f755e1fSLight Hsieh 			       const struct mtk_pin_desc *desc, u32 arg)
8345f755e1fSLight Hsieh {
8355f755e1fSLight Hsieh 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV, arg);
8365f755e1fSLight Hsieh }
8375f755e1fSLight Hsieh 
8385f755e1fSLight Hsieh int mtk_pinconf_drive_get_raw(struct mtk_pinctrl *hw,
8395f755e1fSLight Hsieh 			       const struct mtk_pin_desc *desc, int *val)
8405f755e1fSLight Hsieh {
8415f755e1fSLight Hsieh 	return mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, val);
8425f755e1fSLight Hsieh }
8435f755e1fSLight Hsieh 
8440d7ca772SSean Wang int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
8450d7ca772SSean Wang 			     const struct mtk_pin_desc *desc, bool pullup,
8460d7ca772SSean Wang 			     u32 arg)
8470d7ca772SSean Wang {
8480d7ca772SSean Wang 	int err;
8490d7ca772SSean Wang 
8500d7ca772SSean Wang 	/* 10K off & 50K (75K) off, when (R0, R1) = (0, 0);
8510d7ca772SSean Wang 	 * 10K off & 50K (75K) on, when (R0, R1) = (0, 1);
8520d7ca772SSean Wang 	 * 10K on & 50K (75K) off, when (R0, R1) = (1, 0);
8530d7ca772SSean Wang 	 * 10K on & 50K (75K) on, when (R0, R1) = (1, 1)
8540d7ca772SSean Wang 	 */
855ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R0, arg & 1);
8560d7ca772SSean Wang 	if (err)
8570d7ca772SSean Wang 		return 0;
8580d7ca772SSean Wang 
859ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R1,
8600d7ca772SSean Wang 			       !!(arg & 2));
8610d7ca772SSean Wang 	if (err)
8620d7ca772SSean Wang 		return 0;
8630d7ca772SSean Wang 
8640d7ca772SSean Wang 	arg = pullup ? 0 : 1;
8650d7ca772SSean Wang 
866ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PUPD, arg);
8670d7ca772SSean Wang 
86879348f6fSSean Wang 	/* If PUPD register is not supported for that pin, let's fallback to
86979348f6fSSean Wang 	 * general bias control.
87079348f6fSSean Wang 	 */
87179348f6fSSean Wang 	if (err == -ENOTSUPP) {
87279348f6fSSean Wang 		if (hw->soc->bias_set) {
87379348f6fSSean Wang 			err = hw->soc->bias_set(hw, desc, pullup);
87479348f6fSSean Wang 			if (err)
87579348f6fSSean Wang 				return err;
87679348f6fSSean Wang 		} else {
87779348f6fSSean Wang 			return -ENOTSUPP;
87879348f6fSSean Wang 		}
87979348f6fSSean Wang 	}
88079348f6fSSean Wang 
8810d7ca772SSean Wang 	return err;
8820d7ca772SSean Wang }
8830d7ca772SSean Wang 
8840d7ca772SSean Wang int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
8850d7ca772SSean Wang 			     const struct mtk_pin_desc *desc, bool pullup,
8860d7ca772SSean Wang 			     u32 *val)
8870d7ca772SSean Wang {
8880d7ca772SSean Wang 	u32 t, t2;
8890d7ca772SSean Wang 	int err;
8900d7ca772SSean Wang 
891ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PUPD, &t);
89279348f6fSSean Wang 
89379348f6fSSean Wang 	/* If PUPD register is not supported for that pin, let's fallback to
89479348f6fSSean Wang 	 * general bias control.
89579348f6fSSean Wang 	 */
89679348f6fSSean Wang 	if (err == -ENOTSUPP) {
89779348f6fSSean Wang 		if (hw->soc->bias_get) {
89879348f6fSSean Wang 			err = hw->soc->bias_get(hw, desc, pullup, val);
89979348f6fSSean Wang 			if (err)
90079348f6fSSean Wang 				return err;
90179348f6fSSean Wang 		} else {
90279348f6fSSean Wang 			return -ENOTSUPP;
90379348f6fSSean Wang 		}
90479348f6fSSean Wang 	} else {
90579348f6fSSean Wang 		/* t == 0 supposes PULLUP for the customized PULL setup */
9060d7ca772SSean Wang 		if (err)
9070d7ca772SSean Wang 			return err;
9080d7ca772SSean Wang 
9090d7ca772SSean Wang 		if (pullup ^ !t)
9100d7ca772SSean Wang 			return -EINVAL;
91179348f6fSSean Wang 	}
9120d7ca772SSean Wang 
913ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R0, &t);
9140d7ca772SSean Wang 	if (err)
9150d7ca772SSean Wang 		return err;
9160d7ca772SSean Wang 
917ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R1, &t2);
9180d7ca772SSean Wang 	if (err)
9190d7ca772SSean Wang 		return err;
9200d7ca772SSean Wang 
9210d7ca772SSean Wang 	*val = (t | t2 << 1) & 0x7;
9220d7ca772SSean Wang 
9230d7ca772SSean Wang 	return 0;
9240d7ca772SSean Wang }
9255e73de34SZhiyong Tao 
9265e73de34SZhiyong Tao int mtk_pinconf_adv_drive_set(struct mtk_pinctrl *hw,
9275e73de34SZhiyong Tao 			      const struct mtk_pin_desc *desc, u32 arg)
9285e73de34SZhiyong Tao {
9295e73de34SZhiyong Tao 	int err;
9305e73de34SZhiyong Tao 	int en = arg & 1;
9315e73de34SZhiyong Tao 	int e0 = !!(arg & 2);
9325e73de34SZhiyong Tao 	int e1 = !!(arg & 4);
9335e73de34SZhiyong Tao 
9345e73de34SZhiyong Tao 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_EN, en);
9355e73de34SZhiyong Tao 	if (err)
9365e73de34SZhiyong Tao 		return err;
9375e73de34SZhiyong Tao 
9385e73de34SZhiyong Tao 	if (!en)
9395e73de34SZhiyong Tao 		return err;
9405e73de34SZhiyong Tao 
9415e73de34SZhiyong Tao 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_E0, e0);
9425e73de34SZhiyong Tao 	if (err)
9435e73de34SZhiyong Tao 		return err;
9445e73de34SZhiyong Tao 
9455e73de34SZhiyong Tao 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_E1, e1);
9465e73de34SZhiyong Tao 	if (err)
9475e73de34SZhiyong Tao 		return err;
9485e73de34SZhiyong Tao 
9495e73de34SZhiyong Tao 	return err;
9505e73de34SZhiyong Tao }
9515e73de34SZhiyong Tao 
9525e73de34SZhiyong Tao int mtk_pinconf_adv_drive_get(struct mtk_pinctrl *hw,
9535e73de34SZhiyong Tao 			      const struct mtk_pin_desc *desc, u32 *val)
9545e73de34SZhiyong Tao {
9555e73de34SZhiyong Tao 	u32 en, e0, e1;
9565e73de34SZhiyong Tao 	int err;
9575e73de34SZhiyong Tao 
9585e73de34SZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_EN, &en);
9595e73de34SZhiyong Tao 	if (err)
9605e73de34SZhiyong Tao 		return err;
9615e73de34SZhiyong Tao 
9625e73de34SZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_E0, &e0);
9635e73de34SZhiyong Tao 	if (err)
9645e73de34SZhiyong Tao 		return err;
9655e73de34SZhiyong Tao 
9665e73de34SZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_E1, &e1);
9675e73de34SZhiyong Tao 	if (err)
9685e73de34SZhiyong Tao 		return err;
9695e73de34SZhiyong Tao 
9705e73de34SZhiyong Tao 	*val = (en | e0 << 1 | e1 << 2) & 0x7;
9715e73de34SZhiyong Tao 
9725e73de34SZhiyong Tao 	return 0;
9735e73de34SZhiyong Tao }
974