xref: /linux/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c (revision 41594663c3fb565940ec2b947bd7e623e3f0e600)
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 
9cafe19dbSLight 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>
158174a851SLight Hsieh #include <linux/module.h>
1689132dd8SSean Wang #include <linux/of_irq.h>
17a1a503a8SSean Wang 
1889132dd8SSean Wang #include "mtk-eint.h"
19a1a503a8SSean Wang #include "pinctrl-mtk-common-v2.h"
20a1a503a8SSean Wang 
21c2832197SSean Wang /**
22c2832197SSean Wang  * struct mtk_drive_desc - the structure that holds the information
23c2832197SSean Wang  *			    of the driving current
24c2832197SSean Wang  * @min:	the minimum current of this group
25c2832197SSean Wang  * @max:	the maximum current of this group
26c2832197SSean Wang  * @step:	the step current of this group
27c2832197SSean Wang  * @scal:	the weight factor
28c2832197SSean Wang  *
29c2832197SSean Wang  * formula: output = ((input) / step - 1) * scal
30c2832197SSean Wang  */
31c2832197SSean Wang struct mtk_drive_desc {
32c2832197SSean Wang 	u8 min;
33c2832197SSean Wang 	u8 max;
34c2832197SSean Wang 	u8 step;
35c2832197SSean Wang 	u8 scal;
36c2832197SSean Wang };
37c2832197SSean Wang 
38c2832197SSean Wang /* The groups of drive strength */
3907c6b037SWei Yongjun static const struct mtk_drive_desc mtk_drive[] = {
40c2832197SSean Wang 	[DRV_GRP0] = { 4, 16, 4, 1 },
41c2832197SSean Wang 	[DRV_GRP1] = { 4, 16, 4, 2 },
42c2832197SSean Wang 	[DRV_GRP2] = { 2, 8, 2, 1 },
43c2832197SSean Wang 	[DRV_GRP3] = { 2, 8, 2, 2 },
44c2832197SSean Wang 	[DRV_GRP4] = { 2, 16, 2, 1 },
45c2832197SSean Wang };
46c2832197SSean Wang 
mtk_w32(struct mtk_pinctrl * pctl,u8 i,u32 reg,u32 val)472bc47dfeSSean Wang static void mtk_w32(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 val)
48a1a503a8SSean Wang {
492bc47dfeSSean Wang 	writel_relaxed(val, pctl->base[i] + reg);
50a1a503a8SSean Wang }
51a1a503a8SSean Wang 
mtk_r32(struct mtk_pinctrl * pctl,u8 i,u32 reg)522bc47dfeSSean Wang static u32 mtk_r32(struct mtk_pinctrl *pctl, u8 i, u32 reg)
53a1a503a8SSean Wang {
542bc47dfeSSean Wang 	return readl_relaxed(pctl->base[i] + reg);
55a1a503a8SSean Wang }
56a1a503a8SSean Wang 
mtk_rmw(struct mtk_pinctrl * pctl,u8 i,u32 reg,u32 mask,u32 set)572bc47dfeSSean Wang void mtk_rmw(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 mask, u32 set)
58a1a503a8SSean Wang {
59a1a503a8SSean Wang 	u32 val;
6056ab29ecSTzung-Bi Shih 	unsigned long flags;
61a1a503a8SSean Wang 
6256ab29ecSTzung-Bi Shih 	spin_lock_irqsave(&pctl->lock, flags);
6342a46434SZhiyong Tao 
642bc47dfeSSean Wang 	val = mtk_r32(pctl, i, reg);
65a1a503a8SSean Wang 	val &= ~mask;
66a1a503a8SSean Wang 	val |= set;
672bc47dfeSSean Wang 	mtk_w32(pctl, i, reg, val);
6842a46434SZhiyong Tao 
6956ab29ecSTzung-Bi Shih 	spin_unlock_irqrestore(&pctl->lock, flags);
70a1a503a8SSean Wang }
71a1a503a8SSean Wang 
mtk_hw_pin_field_lookup(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int field,struct mtk_pin_field * pfd)72ea051eb3SSean Wang static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw,
73ea051eb3SSean Wang 				   const struct mtk_pin_desc *desc,
749d9b171cSSean Wang 				   int field, struct mtk_pin_field *pfd)
75a1a503a8SSean Wang {
7686ecb7d6SYueHaibing 	const struct mtk_pin_field_calc *c;
779d9b171cSSean Wang 	const struct mtk_pin_reg_calc *rc;
783de7deefSLight Hsieh 	int start = 0, end, check;
793de7deefSLight Hsieh 	bool found = false;
80a1a503a8SSean Wang 	u32 bits;
81a1a503a8SSean Wang 
829d9b171cSSean Wang 	if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) {
839d9b171cSSean Wang 		rc = &hw->soc->reg_cal[field];
849d9b171cSSean Wang 	} else {
859d9b171cSSean Wang 		dev_dbg(hw->dev,
863de7deefSLight Hsieh 			"Not support field %d for this soc\n", field);
879d9b171cSSean Wang 		return -ENOTSUPP;
889d9b171cSSean Wang 	}
899d9b171cSSean Wang 
903de7deefSLight Hsieh 	end = rc->nranges - 1;
91a1a503a8SSean Wang 
923de7deefSLight Hsieh 	while (start <= end) {
933de7deefSLight Hsieh 		check = (start + end) >> 1;
943de7deefSLight Hsieh 		if (desc->number >= rc->range[check].s_pin
953de7deefSLight Hsieh 		 && desc->number <= rc->range[check].e_pin) {
963de7deefSLight Hsieh 			found = true;
97a1a503a8SSean Wang 			break;
983de7deefSLight Hsieh 		} else if (start == end)
993de7deefSLight Hsieh 			break;
1003de7deefSLight Hsieh 		else if (desc->number < rc->range[check].s_pin)
1013de7deefSLight Hsieh 			end = check - 1;
1023de7deefSLight Hsieh 		else
1033de7deefSLight Hsieh 			start = check + 1;
104a1a503a8SSean Wang 	}
105a1a503a8SSean Wang 
1063de7deefSLight Hsieh 	if (!found) {
1079d9b171cSSean Wang 		dev_dbg(hw->dev, "Not support field %d for pin = %d (%s)\n",
1089d9b171cSSean Wang 			field, desc->number, desc->name);
1099d9b171cSSean Wang 		return -ENOTSUPP;
110a1a503a8SSean Wang 	}
111a1a503a8SSean Wang 
1123de7deefSLight Hsieh 	c = rc->range + check;
1133de7deefSLight Hsieh 
1142bc47dfeSSean Wang 	if (c->i_base > hw->nbase - 1) {
1159d9b171cSSean Wang 		dev_err(hw->dev,
1169d9b171cSSean Wang 			"Invalid base for field %d for pin = %d (%s)\n",
1179d9b171cSSean Wang 			field, desc->number, desc->name);
1182bc47dfeSSean Wang 		return -EINVAL;
1192bc47dfeSSean Wang 	}
1202bc47dfeSSean Wang 
121b906faf7SSean Wang 	/* Calculated bits as the overall offset the pin is located at,
122b906faf7SSean Wang 	 * if c->fixed is held, that determines the all the pins in the
123b906faf7SSean Wang 	 * range use the same field with the s_pin.
124b906faf7SSean Wang 	 */
125ea051eb3SSean Wang 	bits = c->fixed ? c->s_bit : c->s_bit +
126ea051eb3SSean Wang 	       (desc->number - c->s_pin) * (c->x_bits);
127a1a503a8SSean Wang 
128b906faf7SSean Wang 	/* Fill pfd from bits. For example 32-bit register applied is assumed
129b906faf7SSean Wang 	 * when c->sz_reg is equal to 32.
130b906faf7SSean Wang 	 */
1312bc47dfeSSean Wang 	pfd->index = c->i_base;
132b906faf7SSean Wang 	pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg);
133b906faf7SSean Wang 	pfd->bitpos = bits % c->sz_reg;
134a1a503a8SSean Wang 	pfd->mask = (1 << c->x_bits) - 1;
135a1a503a8SSean Wang 
136a1a503a8SSean Wang 	/* pfd->next is used for indicating that bit wrapping-around happens
137a1a503a8SSean Wang 	 * which requires the manipulation for bit 0 starting in the next
138a1a503a8SSean Wang 	 * register to form the complete field read/write.
139a1a503a8SSean Wang 	 */
140b906faf7SSean Wang 	pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0;
141a1a503a8SSean Wang 
142a1a503a8SSean Wang 	return 0;
143a1a503a8SSean Wang }
144a1a503a8SSean Wang 
mtk_hw_pin_field_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int field,struct mtk_pin_field * pfd)145ea051eb3SSean Wang static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw,
146ea051eb3SSean Wang 				const struct mtk_pin_desc *desc,
147a1a503a8SSean Wang 				int field, struct mtk_pin_field *pfd)
148a1a503a8SSean Wang {
149a1a503a8SSean Wang 	if (field < 0 || field >= PINCTRL_PIN_REG_MAX) {
150a1a503a8SSean Wang 		dev_err(hw->dev, "Invalid Field %d\n", field);
151a1a503a8SSean Wang 		return -EINVAL;
152a1a503a8SSean Wang 	}
153a1a503a8SSean Wang 
1549d9b171cSSean Wang 	return mtk_hw_pin_field_lookup(hw, desc, field, pfd);
155a1a503a8SSean Wang }
156a1a503a8SSean Wang 
mtk_hw_bits_part(struct mtk_pin_field * pf,int * h,int * l)157a1a503a8SSean Wang static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
158a1a503a8SSean Wang {
159a1a503a8SSean Wang 	*l = 32 - pf->bitpos;
160a1a503a8SSean Wang 	*h = get_count_order(pf->mask) - *l;
161a1a503a8SSean Wang }
162a1a503a8SSean Wang 
mtk_hw_write_cross_field(struct mtk_pinctrl * hw,struct mtk_pin_field * pf,int value)163a1a503a8SSean Wang static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw,
164a1a503a8SSean Wang 				     struct mtk_pin_field *pf, int value)
165a1a503a8SSean Wang {
166a1a503a8SSean Wang 	int nbits_l, nbits_h;
167a1a503a8SSean Wang 
168a1a503a8SSean Wang 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
169a1a503a8SSean Wang 
1702bc47dfeSSean Wang 	mtk_rmw(hw, pf->index, pf->offset, pf->mask << pf->bitpos,
171a1a503a8SSean Wang 		(value & pf->mask) << pf->bitpos);
172a1a503a8SSean Wang 
1732bc47dfeSSean Wang 	mtk_rmw(hw, pf->index, pf->offset + pf->next, BIT(nbits_h) - 1,
174a1a503a8SSean Wang 		(value & pf->mask) >> nbits_l);
175a1a503a8SSean Wang }
176a1a503a8SSean Wang 
mtk_hw_read_cross_field(struct mtk_pinctrl * hw,struct mtk_pin_field * pf,int * value)177a1a503a8SSean Wang static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw,
178a1a503a8SSean Wang 				    struct mtk_pin_field *pf, int *value)
179a1a503a8SSean Wang {
180a1a503a8SSean Wang 	int nbits_l, nbits_h, h, l;
181a1a503a8SSean Wang 
182a1a503a8SSean Wang 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
183a1a503a8SSean Wang 
1842bc47dfeSSean Wang 	l  = (mtk_r32(hw, pf->index, pf->offset)
1852bc47dfeSSean Wang 	      >> pf->bitpos) & (BIT(nbits_l) - 1);
1862bc47dfeSSean Wang 	h  = (mtk_r32(hw, pf->index, pf->offset + pf->next))
1872bc47dfeSSean Wang 	      & (BIT(nbits_h) - 1);
188a1a503a8SSean Wang 
189a1a503a8SSean Wang 	*value = (h << nbits_l) | l;
190a1a503a8SSean Wang }
191a1a503a8SSean Wang 
mtk_hw_set_value(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int field,int value)192ea051eb3SSean Wang int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
193ea051eb3SSean Wang 		     int field, int value)
194a1a503a8SSean Wang {
195a1a503a8SSean Wang 	struct mtk_pin_field pf;
196a1a503a8SSean Wang 	int err;
197a1a503a8SSean Wang 
198ea051eb3SSean Wang 	err = mtk_hw_pin_field_get(hw, desc, field, &pf);
199a1a503a8SSean Wang 	if (err)
200a1a503a8SSean Wang 		return err;
201a1a503a8SSean Wang 
2023de7deefSLight Hsieh 	if (value < 0 || value > pf.mask)
2033de7deefSLight Hsieh 		return -EINVAL;
2043de7deefSLight Hsieh 
205a1a503a8SSean Wang 	if (!pf.next)
2062bc47dfeSSean Wang 		mtk_rmw(hw, pf.index, pf.offset, pf.mask << pf.bitpos,
207a1a503a8SSean Wang 			(value & pf.mask) << pf.bitpos);
208a1a503a8SSean Wang 	else
209a1a503a8SSean Wang 		mtk_hw_write_cross_field(hw, &pf, value);
210a1a503a8SSean Wang 
211a1a503a8SSean Wang 	return 0;
212a1a503a8SSean Wang }
2138174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_hw_set_value);
214a1a503a8SSean Wang 
mtk_hw_get_value(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int field,int * value)215ea051eb3SSean Wang int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
216ea051eb3SSean Wang 		     int field, int *value)
217a1a503a8SSean Wang {
218a1a503a8SSean Wang 	struct mtk_pin_field pf;
219a1a503a8SSean Wang 	int err;
220a1a503a8SSean Wang 
221ea051eb3SSean Wang 	err = mtk_hw_pin_field_get(hw, desc, field, &pf);
222a1a503a8SSean Wang 	if (err)
223a1a503a8SSean Wang 		return err;
224a1a503a8SSean Wang 
225a1a503a8SSean Wang 	if (!pf.next)
2262bc47dfeSSean Wang 		*value = (mtk_r32(hw, pf.index, pf.offset)
2272bc47dfeSSean Wang 			  >> pf.bitpos) & pf.mask;
228a1a503a8SSean Wang 	else
229a1a503a8SSean Wang 		mtk_hw_read_cross_field(hw, &pf, value);
230a1a503a8SSean Wang 
231a1a503a8SSean Wang 	return 0;
232a1a503a8SSean Wang }
2338174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_hw_get_value);
234c2832197SSean Wang 
mtk_xt_find_eint_num(struct mtk_pinctrl * hw,unsigned long eint_n)23589132dd8SSean Wang static int mtk_xt_find_eint_num(struct mtk_pinctrl *hw, unsigned long eint_n)
23689132dd8SSean Wang {
23789132dd8SSean Wang 	const struct mtk_pin_desc *desc;
23889132dd8SSean Wang 	int i = 0;
23989132dd8SSean Wang 
24089132dd8SSean Wang 	desc = (const struct mtk_pin_desc *)hw->soc->pins;
24189132dd8SSean Wang 
24289132dd8SSean Wang 	while (i < hw->soc->npins) {
24389132dd8SSean Wang 		if (desc[i].eint.eint_n == eint_n)
24489132dd8SSean Wang 			return desc[i].number;
24589132dd8SSean Wang 		i++;
24689132dd8SSean Wang 	}
24789132dd8SSean Wang 
24889132dd8SSean Wang 	return EINT_NA;
24989132dd8SSean Wang }
25089132dd8SSean Wang 
251edd54646SHanks Chen /*
252edd54646SHanks Chen  * Virtual GPIO only used inside SOC and not being exported to outside SOC.
253edd54646SHanks Chen  * Some modules use virtual GPIO as eint (e.g. pmif or usb).
254edd54646SHanks Chen  * In MTK platform, external interrupt (EINT) and GPIO is 1-1 mapping
255edd54646SHanks Chen  * and we can set GPIO as eint.
256edd54646SHanks Chen  * But some modules use specific eint which doesn't have real GPIO pin.
257edd54646SHanks Chen  * So we use virtual GPIO to map it.
258edd54646SHanks Chen  */
259edd54646SHanks Chen 
mtk_is_virt_gpio(struct mtk_pinctrl * hw,unsigned int gpio_n)260edd54646SHanks Chen bool mtk_is_virt_gpio(struct mtk_pinctrl *hw, unsigned int gpio_n)
261edd54646SHanks Chen {
262edd54646SHanks Chen 	const struct mtk_pin_desc *desc;
263edd54646SHanks Chen 	bool virt_gpio = false;
264edd54646SHanks Chen 
265edd54646SHanks Chen 	desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
266edd54646SHanks Chen 
26739c4dbe4SHanks Chen 	/* if the GPIO is not supported for eint mode */
26839c4dbe4SHanks Chen 	if (desc->eint.eint_m == NO_EINT_SUPPORT)
26939c4dbe4SHanks Chen 		return virt_gpio;
27039c4dbe4SHanks Chen 
271edd54646SHanks Chen 	if (desc->funcs && !desc->funcs[desc->eint.eint_m].name)
272edd54646SHanks Chen 		virt_gpio = true;
273edd54646SHanks Chen 
274edd54646SHanks Chen 	return virt_gpio;
275edd54646SHanks Chen }
276047cd9a6SRandy Dunlap EXPORT_SYMBOL_GPL(mtk_is_virt_gpio);
277edd54646SHanks Chen 
mtk_xt_get_gpio_n(void * data,unsigned long eint_n,unsigned int * gpio_n,struct gpio_chip ** gpio_chip)27889132dd8SSean Wang static int mtk_xt_get_gpio_n(void *data, unsigned long eint_n,
27989132dd8SSean Wang 			     unsigned int *gpio_n,
28089132dd8SSean Wang 			     struct gpio_chip **gpio_chip)
28189132dd8SSean Wang {
28289132dd8SSean Wang 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
28389132dd8SSean Wang 	const struct mtk_pin_desc *desc;
28489132dd8SSean Wang 
28589132dd8SSean Wang 	desc = (const struct mtk_pin_desc *)hw->soc->pins;
28689132dd8SSean Wang 	*gpio_chip = &hw->chip;
28789132dd8SSean Wang 
2882d5446daSGuodong Liu 	/*
2892d5446daSGuodong Liu 	 * Be greedy to guess first gpio_n is equal to eint_n.
2902d5446daSGuodong Liu 	 * Only eint virtual eint number is greater than gpio number.
2912d5446daSGuodong Liu 	 */
2922d5446daSGuodong Liu 	if (hw->soc->npins > eint_n &&
2932d5446daSGuodong Liu 	    desc[eint_n].eint.eint_n == eint_n)
29489132dd8SSean Wang 		*gpio_n = eint_n;
29589132dd8SSean Wang 	else
29689132dd8SSean Wang 		*gpio_n = mtk_xt_find_eint_num(hw, eint_n);
29789132dd8SSean Wang 
29889132dd8SSean Wang 	return *gpio_n == EINT_NA ? -EINVAL : 0;
29989132dd8SSean Wang }
30089132dd8SSean Wang 
mtk_xt_get_gpio_state(void * data,unsigned long eint_n)30189132dd8SSean Wang static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n)
30289132dd8SSean Wang {
30389132dd8SSean Wang 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
30489132dd8SSean Wang 	const struct mtk_pin_desc *desc;
30589132dd8SSean Wang 	struct gpio_chip *gpio_chip;
30689132dd8SSean Wang 	unsigned int gpio_n;
30789132dd8SSean Wang 	int value, err;
30889132dd8SSean Wang 
30989132dd8SSean Wang 	err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
31089132dd8SSean Wang 	if (err)
31189132dd8SSean Wang 		return err;
31289132dd8SSean Wang 
31389132dd8SSean Wang 	desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
31489132dd8SSean Wang 
31589132dd8SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value);
31689132dd8SSean Wang 	if (err)
31789132dd8SSean Wang 		return err;
31889132dd8SSean Wang 
31989132dd8SSean Wang 	return !!value;
32089132dd8SSean Wang }
32189132dd8SSean Wang 
mtk_xt_set_gpio_as_eint(void * data,unsigned long eint_n)32289132dd8SSean Wang static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n)
32389132dd8SSean Wang {
32489132dd8SSean Wang 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
32589132dd8SSean Wang 	const struct mtk_pin_desc *desc;
32689132dd8SSean Wang 	struct gpio_chip *gpio_chip;
32789132dd8SSean Wang 	unsigned int gpio_n;
32889132dd8SSean Wang 	int err;
32989132dd8SSean Wang 
33089132dd8SSean Wang 	err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
33189132dd8SSean Wang 	if (err)
33289132dd8SSean Wang 		return err;
33389132dd8SSean Wang 
334edd54646SHanks Chen 	if (mtk_is_virt_gpio(hw, gpio_n))
335edd54646SHanks Chen 		return 0;
336edd54646SHanks Chen 
33789132dd8SSean Wang 	desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
33889132dd8SSean Wang 
33989132dd8SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE,
34089132dd8SSean Wang 			       desc->eint.eint_m);
34189132dd8SSean Wang 	if (err)
34289132dd8SSean Wang 		return err;
34389132dd8SSean Wang 
34489132dd8SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, MTK_INPUT);
34589132dd8SSean Wang 	if (err)
34689132dd8SSean Wang 		return err;
34789132dd8SSean Wang 
34889132dd8SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, MTK_ENABLE);
3496e737a4eSchuanjia.liu 	/* SMT is supposed to be supported by every real GPIO and doesn't
3506e737a4eSchuanjia.liu 	 * support virtual GPIOs, so the extra condition err != -ENOTSUPP
3516e737a4eSchuanjia.liu 	 * is just for adding EINT support to these virtual GPIOs. It should
3526e737a4eSchuanjia.liu 	 * add an extra flag in the pin descriptor when more pins with
3536e737a4eSchuanjia.liu 	 * distinctive characteristic come out.
3546e737a4eSchuanjia.liu 	 */
3556e737a4eSchuanjia.liu 	if (err && err != -ENOTSUPP)
35689132dd8SSean Wang 		return err;
35789132dd8SSean Wang 
35889132dd8SSean Wang 	return 0;
35989132dd8SSean Wang }
36089132dd8SSean Wang 
36189132dd8SSean Wang static const struct mtk_eint_xt mtk_eint_xt = {
36289132dd8SSean Wang 	.get_gpio_n = mtk_xt_get_gpio_n,
36389132dd8SSean Wang 	.get_gpio_state = mtk_xt_get_gpio_state,
36489132dd8SSean Wang 	.set_gpio_as_eint = mtk_xt_set_gpio_as_eint,
36589132dd8SSean Wang };
36689132dd8SSean Wang 
mtk_build_eint(struct mtk_pinctrl * hw,struct platform_device * pdev)36789132dd8SSean Wang int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev)
36889132dd8SSean Wang {
36989132dd8SSean Wang 	struct device_node *np = pdev->dev.of_node;
370db751578SEnric Balletbo i Serra 	int ret;
37189132dd8SSean Wang 
37289132dd8SSean Wang 	if (!IS_ENABLED(CONFIG_EINT_MTK))
37389132dd8SSean Wang 		return 0;
37489132dd8SSean Wang 
37589132dd8SSean Wang 	if (!of_property_read_bool(np, "interrupt-controller"))
37689132dd8SSean Wang 		return -ENODEV;
37789132dd8SSean Wang 
37889132dd8SSean Wang 	hw->eint = devm_kzalloc(hw->dev, sizeof(*hw->eint), GFP_KERNEL);
37989132dd8SSean Wang 	if (!hw->eint)
38089132dd8SSean Wang 		return -ENOMEM;
38189132dd8SSean Wang 
38248548c78SWang Xiaojun 	hw->eint->base = devm_platform_ioremap_resource_byname(pdev, "eint");
383db751578SEnric Balletbo i Serra 	if (IS_ERR(hw->eint->base)) {
384db751578SEnric Balletbo i Serra 		ret = PTR_ERR(hw->eint->base);
385db751578SEnric Balletbo i Serra 		goto err_free_eint;
38689132dd8SSean Wang 	}
38789132dd8SSean Wang 
38889132dd8SSean Wang 	hw->eint->irq = irq_of_parse_and_map(np, 0);
389db751578SEnric Balletbo i Serra 	if (!hw->eint->irq) {
390db751578SEnric Balletbo i Serra 		ret = -EINVAL;
391db751578SEnric Balletbo i Serra 		goto err_free_eint;
392db751578SEnric Balletbo i Serra 	}
39389132dd8SSean Wang 
394db751578SEnric Balletbo i Serra 	if (!hw->soc->eint_hw) {
395db751578SEnric Balletbo i Serra 		ret = -ENODEV;
396db751578SEnric Balletbo i Serra 		goto err_free_eint;
397db751578SEnric Balletbo i Serra 	}
39889132dd8SSean Wang 
39989132dd8SSean Wang 	hw->eint->dev = &pdev->dev;
40089132dd8SSean Wang 	hw->eint->hw = hw->soc->eint_hw;
40189132dd8SSean Wang 	hw->eint->pctl = hw;
40289132dd8SSean Wang 	hw->eint->gpio_xlate = &mtk_eint_xt;
40389132dd8SSean Wang 
40489132dd8SSean Wang 	return mtk_eint_do_init(hw->eint);
405db751578SEnric Balletbo i Serra 
406db751578SEnric Balletbo i Serra err_free_eint:
407db751578SEnric Balletbo i Serra 	devm_kfree(hw->dev, hw->eint);
408db751578SEnric Balletbo i Serra 	hw->eint = NULL;
409db751578SEnric Balletbo i Serra 	return ret;
41089132dd8SSean Wang }
4118174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_build_eint);
41289132dd8SSean Wang 
4139afc305bSSean Wang /* Revision 0 */
mtk_pinconf_bias_disable_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc)41485430152SSean Wang int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
41585430152SSean Wang 				 const struct mtk_pin_desc *desc)
41685430152SSean Wang {
41785430152SSean Wang 	int err;
41885430152SSean Wang 
419ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU,
42085430152SSean Wang 			       MTK_DISABLE);
42185430152SSean Wang 	if (err)
42285430152SSean Wang 		return err;
42385430152SSean Wang 
424ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
42585430152SSean Wang 			       MTK_DISABLE);
42685430152SSean Wang 	if (err)
42785430152SSean Wang 		return err;
42885430152SSean Wang 
42985430152SSean Wang 	return 0;
43085430152SSean Wang }
4318174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_disable_set);
43285430152SSean Wang 
mtk_pinconf_bias_disable_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * res)43385430152SSean Wang int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
43485430152SSean Wang 				 const struct mtk_pin_desc *desc, int *res)
43585430152SSean Wang {
43685430152SSean Wang 	int v, v2;
43785430152SSean Wang 	int err;
43885430152SSean Wang 
439ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &v);
44085430152SSean Wang 	if (err)
44185430152SSean Wang 		return err;
44285430152SSean Wang 
443ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &v2);
44485430152SSean Wang 	if (err)
44585430152SSean Wang 		return err;
44685430152SSean Wang 
44785430152SSean Wang 	if (v == MTK_ENABLE || v2 == MTK_ENABLE)
44885430152SSean Wang 		return -EINVAL;
44985430152SSean Wang 
45085430152SSean Wang 	*res = 1;
45185430152SSean Wang 
45285430152SSean Wang 	return 0;
45385430152SSean Wang }
4548174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_disable_get);
45585430152SSean Wang 
mtk_pinconf_bias_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup)45685430152SSean Wang int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
45785430152SSean Wang 			 const struct mtk_pin_desc *desc, bool pullup)
45885430152SSean Wang {
45985430152SSean Wang 	int err, arg;
46085430152SSean Wang 
46185430152SSean Wang 	arg = pullup ? 1 : 2;
46285430152SSean Wang 
463ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU, arg & 1);
46485430152SSean Wang 	if (err)
46585430152SSean Wang 		return err;
46685430152SSean Wang 
467ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
46885430152SSean Wang 			       !!(arg & 2));
46985430152SSean Wang 	if (err)
47085430152SSean Wang 		return err;
47185430152SSean Wang 
47285430152SSean Wang 	return 0;
47385430152SSean Wang }
4748174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_set);
47585430152SSean Wang 
mtk_pinconf_bias_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup,int * res)47685430152SSean Wang int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
47785430152SSean Wang 			 const struct mtk_pin_desc *desc, bool pullup, int *res)
47885430152SSean Wang {
47985430152SSean Wang 	int reg, err, v;
48085430152SSean Wang 
48185430152SSean Wang 	reg = pullup ? PINCTRL_PIN_REG_PU : PINCTRL_PIN_REG_PD;
48285430152SSean Wang 
483ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, reg, &v);
48485430152SSean Wang 	if (err)
48585430152SSean Wang 		return err;
48685430152SSean Wang 
48785430152SSean Wang 	if (!v)
48885430152SSean Wang 		return -EINVAL;
48985430152SSean Wang 
49085430152SSean Wang 	*res = 1;
49185430152SSean Wang 
49285430152SSean Wang 	return 0;
49385430152SSean Wang }
4948174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_get);
49585430152SSean Wang 
4969afc305bSSean Wang /* Revision 1 */
mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc)4979afc305bSSean Wang int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
4989afc305bSSean Wang 				      const struct mtk_pin_desc *desc)
4999afc305bSSean Wang {
500a15f8596SZheng Yongjun 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
5019afc305bSSean Wang 				MTK_DISABLE);
5029afc305bSSean Wang }
5038174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_disable_set_rev1);
5049afc305bSSean Wang 
mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * res)5059afc305bSSean Wang int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
5069afc305bSSean Wang 				      const struct mtk_pin_desc *desc, int *res)
5079afc305bSSean Wang {
5089afc305bSSean Wang 	int v, err;
5099afc305bSSean Wang 
510ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
5119afc305bSSean Wang 	if (err)
5129afc305bSSean Wang 		return err;
5139afc305bSSean Wang 
5149afc305bSSean Wang 	if (v == MTK_ENABLE)
5159afc305bSSean Wang 		return -EINVAL;
5169afc305bSSean Wang 
5179afc305bSSean Wang 	*res = 1;
5189afc305bSSean Wang 
5199afc305bSSean Wang 	return 0;
5209afc305bSSean Wang }
5218174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_disable_get_rev1);
5229afc305bSSean Wang 
mtk_pinconf_bias_set_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup)5239afc305bSSean Wang int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
5249afc305bSSean Wang 			      const struct mtk_pin_desc *desc, bool pullup)
5259afc305bSSean Wang {
5269afc305bSSean Wang 	int err, arg;
5279afc305bSSean Wang 
5289afc305bSSean Wang 	arg = pullup ? MTK_PULLUP : MTK_PULLDOWN;
5299afc305bSSean Wang 
530ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
5319afc305bSSean Wang 			       MTK_ENABLE);
5329afc305bSSean Wang 	if (err)
5339afc305bSSean Wang 		return err;
5349afc305bSSean Wang 
535ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, arg);
5369afc305bSSean Wang 	if (err)
5379afc305bSSean Wang 		return err;
5389afc305bSSean Wang 
5399afc305bSSean Wang 	return 0;
5409afc305bSSean Wang }
5418174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_set_rev1);
5429afc305bSSean Wang 
mtk_pinconf_bias_get_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup,int * res)5439afc305bSSean Wang int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
5449afc305bSSean Wang 			      const struct mtk_pin_desc *desc, bool pullup,
5459afc305bSSean Wang 			      int *res)
5469afc305bSSean Wang {
5479afc305bSSean Wang 	int err, v;
5489afc305bSSean Wang 
549ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
5509afc305bSSean Wang 	if (err)
5519afc305bSSean Wang 		return err;
5529afc305bSSean Wang 
5539afc305bSSean Wang 	if (v == MTK_DISABLE)
5549afc305bSSean Wang 		return -EINVAL;
5559afc305bSSean Wang 
556ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, &v);
5579afc305bSSean Wang 	if (err)
5589afc305bSSean Wang 		return err;
5599afc305bSSean Wang 
5609afc305bSSean Wang 	if (pullup ^ (v == MTK_PULLUP))
5619afc305bSSean Wang 		return -EINVAL;
5629afc305bSSean Wang 
5639afc305bSSean Wang 	*res = 1;
5649afc305bSSean Wang 
5659afc305bSSean Wang 	return 0;
5669afc305bSSean Wang }
5678174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_get_rev1);
5689afc305bSSean Wang 
569cafe19dbSLight Hsieh /* Combo for the following pull register type:
570cafe19dbSLight Hsieh  * 1. PU + PD
571cafe19dbSLight Hsieh  * 2. PULLSEL + PULLEN
572cafe19dbSLight Hsieh  * 3. PUPD + R0 + R1
573cafe19dbSLight Hsieh  */
mtk_pinconf_bias_set_pu_pd(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)574cafe19dbSLight Hsieh static int mtk_pinconf_bias_set_pu_pd(struct mtk_pinctrl *hw,
575cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
576cafe19dbSLight Hsieh 				u32 pullup, u32 arg)
577cafe19dbSLight Hsieh {
578cafe19dbSLight Hsieh 	int err, pu, pd;
579cafe19dbSLight Hsieh 
580cafe19dbSLight Hsieh 	if (arg == MTK_DISABLE) {
581cafe19dbSLight Hsieh 		pu = 0;
582cafe19dbSLight Hsieh 		pd = 0;
583cafe19dbSLight Hsieh 	} else if ((arg == MTK_ENABLE) && pullup) {
584cafe19dbSLight Hsieh 		pu = 1;
585cafe19dbSLight Hsieh 		pd = 0;
586cafe19dbSLight Hsieh 	} else if ((arg == MTK_ENABLE) && !pullup) {
587cafe19dbSLight Hsieh 		pu = 0;
588cafe19dbSLight Hsieh 		pd = 1;
589cafe19dbSLight Hsieh 	} else {
590cafe19dbSLight Hsieh 		err = -EINVAL;
591cafe19dbSLight Hsieh 		goto out;
592cafe19dbSLight Hsieh 	}
593cafe19dbSLight Hsieh 
594cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU, pu);
595cafe19dbSLight Hsieh 	if (err)
596cafe19dbSLight Hsieh 		goto out;
597cafe19dbSLight Hsieh 
598cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD, pd);
599cafe19dbSLight Hsieh 
600cafe19dbSLight Hsieh out:
601cafe19dbSLight Hsieh 	return err;
602cafe19dbSLight Hsieh }
603cafe19dbSLight Hsieh 
mtk_pinconf_bias_set_pullsel_pullen(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)604cafe19dbSLight Hsieh static int mtk_pinconf_bias_set_pullsel_pullen(struct mtk_pinctrl *hw,
605cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
606cafe19dbSLight Hsieh 				u32 pullup, u32 arg)
607cafe19dbSLight Hsieh {
608cafe19dbSLight Hsieh 	int err, enable;
609cafe19dbSLight Hsieh 
610cafe19dbSLight Hsieh 	if (arg == MTK_DISABLE)
611cafe19dbSLight Hsieh 		enable = 0;
612cafe19dbSLight Hsieh 	else if (arg == MTK_ENABLE)
613cafe19dbSLight Hsieh 		enable = 1;
614cafe19dbSLight Hsieh 	else {
615cafe19dbSLight Hsieh 		err = -EINVAL;
616cafe19dbSLight Hsieh 		goto out;
617cafe19dbSLight Hsieh 	}
618cafe19dbSLight Hsieh 
619cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN, enable);
620cafe19dbSLight Hsieh 	if (err)
621cafe19dbSLight Hsieh 		goto out;
622cafe19dbSLight Hsieh 
623cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, pullup);
624cafe19dbSLight Hsieh 
625cafe19dbSLight Hsieh out:
626cafe19dbSLight Hsieh 	return err;
627cafe19dbSLight Hsieh }
628cafe19dbSLight Hsieh 
mtk_pinconf_bias_set_pupd_r1_r0(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)629cafe19dbSLight Hsieh static int mtk_pinconf_bias_set_pupd_r1_r0(struct mtk_pinctrl *hw,
630cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
631cafe19dbSLight Hsieh 				u32 pullup, u32 arg)
632cafe19dbSLight Hsieh {
633cafe19dbSLight Hsieh 	int err, r0, r1;
634cafe19dbSLight Hsieh 
635cafe19dbSLight Hsieh 	if ((arg == MTK_DISABLE) || (arg == MTK_PUPD_SET_R1R0_00)) {
636cafe19dbSLight Hsieh 		pullup = 0;
637cafe19dbSLight Hsieh 		r0 = 0;
638cafe19dbSLight Hsieh 		r1 = 0;
639cafe19dbSLight Hsieh 	} else if (arg == MTK_PUPD_SET_R1R0_01) {
640cafe19dbSLight Hsieh 		r0 = 1;
641cafe19dbSLight Hsieh 		r1 = 0;
642cafe19dbSLight Hsieh 	} else if (arg == MTK_PUPD_SET_R1R0_10) {
643cafe19dbSLight Hsieh 		r0 = 0;
644cafe19dbSLight Hsieh 		r1 = 1;
645cafe19dbSLight Hsieh 	} else if (arg == MTK_PUPD_SET_R1R0_11) {
646cafe19dbSLight Hsieh 		r0 = 1;
647cafe19dbSLight Hsieh 		r1 = 1;
648cafe19dbSLight Hsieh 	} else {
649cafe19dbSLight Hsieh 		err = -EINVAL;
650cafe19dbSLight Hsieh 		goto out;
651cafe19dbSLight Hsieh 	}
652cafe19dbSLight Hsieh 
653cafe19dbSLight Hsieh 	/* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
654cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PUPD, !pullup);
655cafe19dbSLight Hsieh 	if (err)
656cafe19dbSLight Hsieh 		goto out;
657cafe19dbSLight Hsieh 
658cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R0, r0);
659cafe19dbSLight Hsieh 	if (err)
660cafe19dbSLight Hsieh 		goto out;
661cafe19dbSLight Hsieh 
662cafe19dbSLight Hsieh 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R1, r1);
663cafe19dbSLight Hsieh 
664cafe19dbSLight Hsieh out:
665cafe19dbSLight Hsieh 	return err;
666cafe19dbSLight Hsieh }
667cafe19dbSLight Hsieh 
mtk_hw_pin_rsel_lookup(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg,u32 * rsel_val)668fb34a9aeSZhiyong Tao static int mtk_hw_pin_rsel_lookup(struct mtk_pinctrl *hw,
669fb34a9aeSZhiyong Tao 				  const struct mtk_pin_desc *desc,
670fb34a9aeSZhiyong Tao 				  u32 pullup, u32 arg, u32 *rsel_val)
671fb34a9aeSZhiyong Tao {
672fb34a9aeSZhiyong Tao 	const struct mtk_pin_rsel *rsel;
673fb34a9aeSZhiyong Tao 	int check;
674fb34a9aeSZhiyong Tao 	bool found = false;
675fb34a9aeSZhiyong Tao 
676fb34a9aeSZhiyong Tao 	rsel = hw->soc->pin_rsel;
677fb34a9aeSZhiyong Tao 
678fb34a9aeSZhiyong Tao 	for (check = 0; check <= hw->soc->npin_rsel - 1; check++) {
679fb34a9aeSZhiyong Tao 		if (desc->number >= rsel[check].s_pin &&
680fb34a9aeSZhiyong Tao 		    desc->number <= rsel[check].e_pin) {
681fb34a9aeSZhiyong Tao 			if (pullup) {
682fb34a9aeSZhiyong Tao 				if (rsel[check].up_rsel == arg) {
683fb34a9aeSZhiyong Tao 					found = true;
684fb34a9aeSZhiyong Tao 					*rsel_val = rsel[check].rsel_index;
685fb34a9aeSZhiyong Tao 					break;
686fb34a9aeSZhiyong Tao 				}
687fb34a9aeSZhiyong Tao 			} else {
688fb34a9aeSZhiyong Tao 				if (rsel[check].down_rsel == arg) {
689fb34a9aeSZhiyong Tao 					found = true;
690fb34a9aeSZhiyong Tao 					*rsel_val = rsel[check].rsel_index;
691fb34a9aeSZhiyong Tao 					break;
692fb34a9aeSZhiyong Tao 				}
693fb34a9aeSZhiyong Tao 			}
694fb34a9aeSZhiyong Tao 		}
695fb34a9aeSZhiyong Tao 	}
696fb34a9aeSZhiyong Tao 
697fb34a9aeSZhiyong Tao 	if (!found) {
698fb34a9aeSZhiyong Tao 		dev_err(hw->dev, "Not support rsel value %d Ohm for pin = %d (%s)\n",
699fb34a9aeSZhiyong Tao 			arg, desc->number, desc->name);
700fb34a9aeSZhiyong Tao 		return -ENOTSUPP;
701fb34a9aeSZhiyong Tao 	}
702fb34a9aeSZhiyong Tao 
703fb34a9aeSZhiyong Tao 	return 0;
704fb34a9aeSZhiyong Tao }
705fb34a9aeSZhiyong Tao 
mtk_pinconf_bias_set_rsel(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)706fb34a9aeSZhiyong Tao static int mtk_pinconf_bias_set_rsel(struct mtk_pinctrl *hw,
707fb34a9aeSZhiyong Tao 				     const struct mtk_pin_desc *desc,
708fb34a9aeSZhiyong Tao 				     u32 pullup, u32 arg)
709fb34a9aeSZhiyong Tao {
710fb34a9aeSZhiyong Tao 	int err, rsel_val;
711fb34a9aeSZhiyong Tao 
712fb34a9aeSZhiyong Tao 	if (hw->rsel_si_unit) {
713fb34a9aeSZhiyong Tao 		/* find pin rsel_index from pin_rsel array*/
714fb34a9aeSZhiyong Tao 		err = mtk_hw_pin_rsel_lookup(hw, desc, pullup, arg, &rsel_val);
715fb34a9aeSZhiyong Tao 		if (err)
716*166bf8afSNícolas F. R. A. Prado 			return err;
717fb34a9aeSZhiyong Tao 	} else {
718*166bf8afSNícolas F. R. A. Prado 		if (arg < MTK_PULL_SET_RSEL_000 || arg > MTK_PULL_SET_RSEL_111)
719*166bf8afSNícolas F. R. A. Prado 			return -EINVAL;
720fb34a9aeSZhiyong Tao 
721fb34a9aeSZhiyong Tao 		rsel_val = arg - MTK_PULL_SET_RSEL_000;
722fb34a9aeSZhiyong Tao 	}
723fb34a9aeSZhiyong Tao 
724*166bf8afSNícolas F. R. A. Prado 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_RSEL, rsel_val);
725*166bf8afSNícolas F. R. A. Prado }
726*166bf8afSNícolas F. R. A. Prado 
mtk_pinconf_bias_set_pu_pd_rsel(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)727*166bf8afSNícolas F. R. A. Prado static int mtk_pinconf_bias_set_pu_pd_rsel(struct mtk_pinctrl *hw,
728*166bf8afSNícolas F. R. A. Prado 					   const struct mtk_pin_desc *desc,
729*166bf8afSNícolas F. R. A. Prado 					   u32 pullup, u32 arg)
730*166bf8afSNícolas F. R. A. Prado {
731*166bf8afSNícolas F. R. A. Prado 	u32 enable = arg == MTK_DISABLE ? MTK_DISABLE : MTK_ENABLE;
732*166bf8afSNícolas F. R. A. Prado 	int err;
733*166bf8afSNícolas F. R. A. Prado 
734*166bf8afSNícolas F. R. A. Prado 	if (arg != MTK_DISABLE) {
735*166bf8afSNícolas F. R. A. Prado 		err = mtk_pinconf_bias_set_rsel(hw, desc, pullup, arg);
736fb34a9aeSZhiyong Tao 		if (err)
737fb34a9aeSZhiyong Tao 			return err;
738fb34a9aeSZhiyong Tao 	}
739fb34a9aeSZhiyong Tao 
740*166bf8afSNícolas F. R. A. Prado 	return mtk_pinconf_bias_set_pu_pd(hw, desc, pullup, enable);
741*166bf8afSNícolas F. R. A. Prado }
742*166bf8afSNícolas F. R. A. Prado 
mtk_pinconf_bias_set_combo(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)743fb34a9aeSZhiyong Tao int mtk_pinconf_bias_set_combo(struct mtk_pinctrl *hw,
744fb34a9aeSZhiyong Tao 			       const struct mtk_pin_desc *desc,
745fb34a9aeSZhiyong Tao 			       u32 pullup, u32 arg)
746fb34a9aeSZhiyong Tao {
747fb34a9aeSZhiyong Tao 	int err = -ENOTSUPP;
748fb34a9aeSZhiyong Tao 	u32 try_all_type;
749fb34a9aeSZhiyong Tao 
750fb34a9aeSZhiyong Tao 	if (hw->soc->pull_type)
751fb34a9aeSZhiyong Tao 		try_all_type = hw->soc->pull_type[desc->number];
752fb34a9aeSZhiyong Tao 	else
753fb34a9aeSZhiyong Tao 		try_all_type = MTK_PULL_TYPE_MASK;
754fb34a9aeSZhiyong Tao 
755fb34a9aeSZhiyong Tao 	if (try_all_type & MTK_PULL_RSEL_TYPE) {
756*166bf8afSNícolas F. R. A. Prado 		err = mtk_pinconf_bias_set_pu_pd_rsel(hw, desc, pullup, arg);
757fb34a9aeSZhiyong Tao 		if (!err)
758*166bf8afSNícolas F. R. A. Prado 			return 0;
759fb34a9aeSZhiyong Tao 	}
760fb34a9aeSZhiyong Tao 
761fb34a9aeSZhiyong Tao 	if (try_all_type & MTK_PULL_PU_PD_TYPE) {
762fb34a9aeSZhiyong Tao 		err = mtk_pinconf_bias_set_pu_pd(hw, desc, pullup, arg);
763fb34a9aeSZhiyong Tao 		if (!err)
764*166bf8afSNícolas F. R. A. Prado 			return 0;
765fb34a9aeSZhiyong Tao 	}
766fb34a9aeSZhiyong Tao 
767fb34a9aeSZhiyong Tao 	if (try_all_type & MTK_PULL_PULLSEL_TYPE) {
768fb34a9aeSZhiyong Tao 		err = mtk_pinconf_bias_set_pullsel_pullen(hw, desc,
769fb34a9aeSZhiyong Tao 							  pullup, arg);
770fb34a9aeSZhiyong Tao 		if (!err)
771*166bf8afSNícolas F. R. A. Prado 			return 0;
772fb34a9aeSZhiyong Tao 	}
773fb34a9aeSZhiyong Tao 
774fb34a9aeSZhiyong Tao 	if (try_all_type & MTK_PULL_PUPD_R1R0_TYPE)
775fb34a9aeSZhiyong Tao 		err = mtk_pinconf_bias_set_pupd_r1_r0(hw, desc, pullup, arg);
776fb34a9aeSZhiyong Tao 
777fb34a9aeSZhiyong Tao 	if (err)
778fb34a9aeSZhiyong Tao 		dev_err(hw->dev, "Invalid pull argument\n");
779fb34a9aeSZhiyong Tao 
780fb34a9aeSZhiyong Tao 	return err;
781fb34a9aeSZhiyong Tao }
782fb34a9aeSZhiyong Tao EXPORT_SYMBOL_GPL(mtk_pinconf_bias_set_combo);
783fb34a9aeSZhiyong Tao 
mtk_rsel_get_si_unit(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 rsel_val,u32 * si_unit)784fb34a9aeSZhiyong Tao static int mtk_rsel_get_si_unit(struct mtk_pinctrl *hw,
785fb34a9aeSZhiyong Tao 				const struct mtk_pin_desc *desc,
786fb34a9aeSZhiyong Tao 				u32 pullup, u32 rsel_val, u32 *si_unit)
787fb34a9aeSZhiyong Tao {
788fb34a9aeSZhiyong Tao 	const struct mtk_pin_rsel *rsel;
789fb34a9aeSZhiyong Tao 	int check;
790fb34a9aeSZhiyong Tao 
791fb34a9aeSZhiyong Tao 	rsel = hw->soc->pin_rsel;
792fb34a9aeSZhiyong Tao 
793fb34a9aeSZhiyong Tao 	for (check = 0; check <= hw->soc->npin_rsel - 1; check++) {
794fb34a9aeSZhiyong Tao 		if (desc->number >= rsel[check].s_pin &&
795fb34a9aeSZhiyong Tao 		    desc->number <= rsel[check].e_pin) {
796fb34a9aeSZhiyong Tao 			if (rsel_val == rsel[check].rsel_index) {
797fb34a9aeSZhiyong Tao 				if (pullup)
798fb34a9aeSZhiyong Tao 					*si_unit = rsel[check].up_rsel;
799fb34a9aeSZhiyong Tao 				else
800fb34a9aeSZhiyong Tao 					*si_unit = rsel[check].down_rsel;
801fb34a9aeSZhiyong Tao 				break;
802fb34a9aeSZhiyong Tao 			}
803fb34a9aeSZhiyong Tao 		}
804fb34a9aeSZhiyong Tao 	}
805fb34a9aeSZhiyong Tao 
806fb34a9aeSZhiyong Tao 	return 0;
807fb34a9aeSZhiyong Tao }
808fb34a9aeSZhiyong Tao 
mtk_pinconf_bias_get_pu_pd_rsel(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)809*166bf8afSNícolas F. R. A. Prado static int mtk_pinconf_bias_get_pu_pd_rsel(struct mtk_pinctrl *hw,
810fb34a9aeSZhiyong Tao 					   const struct mtk_pin_desc *desc,
811fb34a9aeSZhiyong Tao 					   u32 *pullup, u32 *enable)
812fb34a9aeSZhiyong Tao {
813fb34a9aeSZhiyong Tao 	int pu, pd, rsel, err;
814fb34a9aeSZhiyong Tao 
815fb34a9aeSZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_RSEL, &rsel);
816fb34a9aeSZhiyong Tao 	if (err)
817fb34a9aeSZhiyong Tao 		goto out;
818fb34a9aeSZhiyong Tao 
819fb34a9aeSZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &pu);
820fb34a9aeSZhiyong Tao 	if (err)
821fb34a9aeSZhiyong Tao 		goto out;
822fb34a9aeSZhiyong Tao 
823fb34a9aeSZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &pd);
8249f9d17c2SDan Carpenter 	if (err)
8259f9d17c2SDan Carpenter 		goto out;
826fb34a9aeSZhiyong Tao 
827fb34a9aeSZhiyong Tao 	if (pu == 0 && pd == 0) {
828fb34a9aeSZhiyong Tao 		*pullup = 0;
829fb34a9aeSZhiyong Tao 		*enable = MTK_DISABLE;
830fb34a9aeSZhiyong Tao 	} else if (pu == 1 && pd == 0) {
831fb34a9aeSZhiyong Tao 		*pullup = 1;
832fb34a9aeSZhiyong Tao 		if (hw->rsel_si_unit)
833fb34a9aeSZhiyong Tao 			mtk_rsel_get_si_unit(hw, desc, *pullup, rsel, enable);
834fb34a9aeSZhiyong Tao 		else
835fb34a9aeSZhiyong Tao 			*enable = rsel + MTK_PULL_SET_RSEL_000;
836fb34a9aeSZhiyong Tao 	} else if (pu == 0 && pd == 1) {
837fb34a9aeSZhiyong Tao 		*pullup = 0;
838fb34a9aeSZhiyong Tao 		if (hw->rsel_si_unit)
839fb34a9aeSZhiyong Tao 			mtk_rsel_get_si_unit(hw, desc, *pullup, rsel, enable);
840fb34a9aeSZhiyong Tao 		else
841fb34a9aeSZhiyong Tao 			*enable = rsel + MTK_PULL_SET_RSEL_000;
842fb34a9aeSZhiyong Tao 	} else {
843fb34a9aeSZhiyong Tao 		err = -EINVAL;
844fb34a9aeSZhiyong Tao 		goto out;
845fb34a9aeSZhiyong Tao 	}
846fb34a9aeSZhiyong Tao 
847fb34a9aeSZhiyong Tao out:
848fb34a9aeSZhiyong Tao 	return err;
849fb34a9aeSZhiyong Tao }
850fb34a9aeSZhiyong Tao 
mtk_pinconf_bias_get_pu_pd(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)851cafe19dbSLight Hsieh static int mtk_pinconf_bias_get_pu_pd(struct mtk_pinctrl *hw,
852cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
853cafe19dbSLight Hsieh 				u32 *pullup, u32 *enable)
854cafe19dbSLight Hsieh {
855cafe19dbSLight Hsieh 	int err, pu, pd;
856cafe19dbSLight Hsieh 
857cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &pu);
858cafe19dbSLight Hsieh 	if (err)
859cafe19dbSLight Hsieh 		goto out;
860cafe19dbSLight Hsieh 
861cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &pd);
862cafe19dbSLight Hsieh 	if (err)
863cafe19dbSLight Hsieh 		goto out;
864cafe19dbSLight Hsieh 
865cafe19dbSLight Hsieh 	if (pu == 0 && pd == 0) {
866cafe19dbSLight Hsieh 		*pullup = 0;
867cafe19dbSLight Hsieh 		*enable = MTK_DISABLE;
868cafe19dbSLight Hsieh 	} else if (pu == 1 && pd == 0) {
869cafe19dbSLight Hsieh 		*pullup = 1;
870cafe19dbSLight Hsieh 		*enable = MTK_ENABLE;
871cafe19dbSLight Hsieh 	} else if (pu == 0 && pd == 1) {
872cafe19dbSLight Hsieh 		*pullup = 0;
873cafe19dbSLight Hsieh 		*enable = MTK_ENABLE;
874cafe19dbSLight Hsieh 	} else
875cafe19dbSLight Hsieh 		err = -EINVAL;
876cafe19dbSLight Hsieh 
877cafe19dbSLight Hsieh out:
878cafe19dbSLight Hsieh 	return err;
879cafe19dbSLight Hsieh }
880cafe19dbSLight Hsieh 
mtk_pinconf_bias_get_pullsel_pullen(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)881cafe19dbSLight Hsieh static int mtk_pinconf_bias_get_pullsel_pullen(struct mtk_pinctrl *hw,
882cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
883cafe19dbSLight Hsieh 				u32 *pullup, u32 *enable)
884cafe19dbSLight Hsieh {
885cafe19dbSLight Hsieh 	int err;
886cafe19dbSLight Hsieh 
887cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, pullup);
888cafe19dbSLight Hsieh 	if (err)
889cafe19dbSLight Hsieh 		goto out;
890cafe19dbSLight Hsieh 
891cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, enable);
892cafe19dbSLight Hsieh 
893cafe19dbSLight Hsieh out:
894cafe19dbSLight Hsieh 	return err;
895cafe19dbSLight Hsieh }
896cafe19dbSLight Hsieh 
mtk_pinconf_bias_get_pupd_r1_r0(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)897cafe19dbSLight Hsieh static int mtk_pinconf_bias_get_pupd_r1_r0(struct mtk_pinctrl *hw,
898cafe19dbSLight Hsieh 				const struct mtk_pin_desc *desc,
899cafe19dbSLight Hsieh 				u32 *pullup, u32 *enable)
900cafe19dbSLight Hsieh {
901cafe19dbSLight Hsieh 	int err, r0, r1;
902cafe19dbSLight Hsieh 
903cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PUPD, pullup);
904cafe19dbSLight Hsieh 	if (err)
905cafe19dbSLight Hsieh 		goto out;
906cafe19dbSLight Hsieh 	/* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
907cafe19dbSLight Hsieh 	*pullup = !(*pullup);
908cafe19dbSLight Hsieh 
909cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R0, &r0);
910cafe19dbSLight Hsieh 	if (err)
911cafe19dbSLight Hsieh 		goto out;
912cafe19dbSLight Hsieh 
913cafe19dbSLight Hsieh 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R1, &r1);
914cafe19dbSLight Hsieh 	if (err)
915cafe19dbSLight Hsieh 		goto out;
916cafe19dbSLight Hsieh 
917cafe19dbSLight Hsieh 	if ((r1 == 0) && (r0 == 0))
918cafe19dbSLight Hsieh 		*enable = MTK_PUPD_SET_R1R0_00;
919cafe19dbSLight Hsieh 	else if ((r1 == 0) && (r0 == 1))
920cafe19dbSLight Hsieh 		*enable = MTK_PUPD_SET_R1R0_01;
921cafe19dbSLight Hsieh 	else if ((r1 == 1) && (r0 == 0))
922cafe19dbSLight Hsieh 		*enable = MTK_PUPD_SET_R1R0_10;
923cafe19dbSLight Hsieh 	else if ((r1 == 1) && (r0 == 1))
924cafe19dbSLight Hsieh 		*enable = MTK_PUPD_SET_R1R0_11;
925cafe19dbSLight Hsieh 	else
926cafe19dbSLight Hsieh 		err = -EINVAL;
927cafe19dbSLight Hsieh 
928cafe19dbSLight Hsieh out:
929cafe19dbSLight Hsieh 	return err;
930cafe19dbSLight Hsieh }
931cafe19dbSLight Hsieh 
mtk_pinconf_bias_get_combo(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)932cafe19dbSLight Hsieh int mtk_pinconf_bias_get_combo(struct mtk_pinctrl *hw,
933cafe19dbSLight Hsieh 			      const struct mtk_pin_desc *desc,
934cafe19dbSLight Hsieh 			      u32 *pullup, u32 *enable)
935cafe19dbSLight Hsieh {
936fb34a9aeSZhiyong Tao 	int err = -ENOTSUPP;
937fb34a9aeSZhiyong Tao 	u32 try_all_type;
938cafe19dbSLight Hsieh 
939fb34a9aeSZhiyong Tao 	if (hw->soc->pull_type)
940fb34a9aeSZhiyong Tao 		try_all_type = hw->soc->pull_type[desc->number];
941fb34a9aeSZhiyong Tao 	else
942fb34a9aeSZhiyong Tao 		try_all_type = MTK_PULL_TYPE_MASK;
943fb34a9aeSZhiyong Tao 
944fb34a9aeSZhiyong Tao 	if (try_all_type & MTK_PULL_RSEL_TYPE) {
945*166bf8afSNícolas F. R. A. Prado 		err = mtk_pinconf_bias_get_pu_pd_rsel(hw, desc, pullup, enable);
946fb34a9aeSZhiyong Tao 		if (!err)
947*166bf8afSNícolas F. R. A. Prado 			return 0;
948fb34a9aeSZhiyong Tao 	}
949fb34a9aeSZhiyong Tao 
950fb34a9aeSZhiyong Tao 	if (try_all_type & MTK_PULL_PU_PD_TYPE) {
951cafe19dbSLight Hsieh 		err = mtk_pinconf_bias_get_pu_pd(hw, desc, pullup, enable);
952cafe19dbSLight Hsieh 		if (!err)
953*166bf8afSNícolas F. R. A. Prado 			return 0;
954fb34a9aeSZhiyong Tao 	}
955cafe19dbSLight Hsieh 
956fb34a9aeSZhiyong Tao 	if (try_all_type & MTK_PULL_PULLSEL_TYPE) {
957fb34a9aeSZhiyong Tao 		err = mtk_pinconf_bias_get_pullsel_pullen(hw, desc,
958fb34a9aeSZhiyong Tao 							  pullup, enable);
959cafe19dbSLight Hsieh 		if (!err)
960*166bf8afSNícolas F. R. A. Prado 			return 0;
961fb34a9aeSZhiyong Tao 	}
962cafe19dbSLight Hsieh 
963fb34a9aeSZhiyong Tao 	if (try_all_type & MTK_PULL_PUPD_R1R0_TYPE)
964cafe19dbSLight Hsieh 		err = mtk_pinconf_bias_get_pupd_r1_r0(hw, desc, pullup, enable);
965cafe19dbSLight Hsieh 
966cafe19dbSLight Hsieh 	return err;
967cafe19dbSLight Hsieh }
9688174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_bias_get_combo);
969cafe19dbSLight Hsieh 
970c2832197SSean Wang /* Revision 0 */
mtk_pinconf_drive_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)971c2832197SSean Wang int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
972c2832197SSean Wang 			  const struct mtk_pin_desc *desc, u32 arg)
973c2832197SSean Wang {
974c2832197SSean Wang 	const struct mtk_drive_desc *tb;
975c2832197SSean Wang 	int err = -ENOTSUPP;
976c2832197SSean Wang 
977c2832197SSean Wang 	tb = &mtk_drive[desc->drv_n];
978c2832197SSean Wang 	/* 4mA when (e8, e4) = (0, 0)
979c2832197SSean Wang 	 * 8mA when (e8, e4) = (0, 1)
980c2832197SSean Wang 	 * 12mA when (e8, e4) = (1, 0)
981c2832197SSean Wang 	 * 16mA when (e8, e4) = (1, 1)
982c2832197SSean Wang 	 */
983c2832197SSean Wang 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
984c2832197SSean Wang 		arg = (arg / tb->step - 1) * tb->scal;
985ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E4,
986c2832197SSean Wang 				       arg & 0x1);
987c2832197SSean Wang 		if (err)
988c2832197SSean Wang 			return err;
989c2832197SSean Wang 
990ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E8,
991c2832197SSean Wang 				       (arg & 0x2) >> 1);
992c2832197SSean Wang 		if (err)
993c2832197SSean Wang 			return err;
994c2832197SSean Wang 	}
995c2832197SSean Wang 
996c2832197SSean Wang 	return err;
997c2832197SSean Wang }
9988174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_drive_set);
999c2832197SSean Wang 
mtk_pinconf_drive_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * val)1000c2832197SSean Wang int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
1001c2832197SSean Wang 			  const struct mtk_pin_desc *desc, int *val)
1002c2832197SSean Wang {
1003c2832197SSean Wang 	const struct mtk_drive_desc *tb;
1004c2832197SSean Wang 	int err, val1, val2;
1005c2832197SSean Wang 
1006c2832197SSean Wang 	tb = &mtk_drive[desc->drv_n];
1007c2832197SSean Wang 
1008ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E4, &val1);
1009c2832197SSean Wang 	if (err)
1010c2832197SSean Wang 		return err;
1011c2832197SSean Wang 
1012ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E8, &val2);
1013c2832197SSean Wang 	if (err)
1014c2832197SSean Wang 		return err;
1015c2832197SSean Wang 
1016c2832197SSean Wang 	/* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1)
1017c2832197SSean Wang 	 * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1)
1018c2832197SSean Wang 	 */
1019c2832197SSean Wang 	*val = (((val2 << 1) + val1) / tb->scal + 1) * tb->step;
1020c2832197SSean Wang 
1021c2832197SSean Wang 	return 0;
1022c2832197SSean Wang }
10238174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_drive_get);
10243ad38a14SSean Wang 
10253ad38a14SSean Wang /* Revision 1 */
mtk_pinconf_drive_set_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)10263ad38a14SSean Wang int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
10273ad38a14SSean Wang 			       const struct mtk_pin_desc *desc, u32 arg)
10283ad38a14SSean Wang {
10293ad38a14SSean Wang 	const struct mtk_drive_desc *tb;
10303ad38a14SSean Wang 	int err = -ENOTSUPP;
10313ad38a14SSean Wang 
10323ad38a14SSean Wang 	tb = &mtk_drive[desc->drv_n];
10333ad38a14SSean Wang 
10343ad38a14SSean Wang 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
10353ad38a14SSean Wang 		arg = (arg / tb->step - 1) * tb->scal;
10363ad38a14SSean Wang 
1037ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV,
10383ad38a14SSean Wang 				       arg);
10393ad38a14SSean Wang 		if (err)
10403ad38a14SSean Wang 			return err;
10413ad38a14SSean Wang 	}
10423ad38a14SSean Wang 
10433ad38a14SSean Wang 	return err;
10443ad38a14SSean Wang }
10458174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_drive_set_rev1);
10463ad38a14SSean Wang 
mtk_pinconf_drive_get_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * val)10473ad38a14SSean Wang int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
10483ad38a14SSean Wang 			       const struct mtk_pin_desc *desc, int *val)
10493ad38a14SSean Wang {
10503ad38a14SSean Wang 	const struct mtk_drive_desc *tb;
10513ad38a14SSean Wang 	int err, val1;
10523ad38a14SSean Wang 
10533ad38a14SSean Wang 	tb = &mtk_drive[desc->drv_n];
10543ad38a14SSean Wang 
1055ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, &val1);
10563ad38a14SSean Wang 	if (err)
10573ad38a14SSean Wang 		return err;
10583ad38a14SSean Wang 
10593ad38a14SSean Wang 	*val = ((val1 & 0x7) / tb->scal + 1) * tb->step;
10603ad38a14SSean Wang 
10613ad38a14SSean Wang 	return 0;
10623ad38a14SSean Wang }
10638174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_drive_get_rev1);
10640d7ca772SSean Wang 
mtk_pinconf_drive_set_raw(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)10655f755e1fSLight Hsieh int mtk_pinconf_drive_set_raw(struct mtk_pinctrl *hw,
10665f755e1fSLight Hsieh 			       const struct mtk_pin_desc *desc, u32 arg)
10675f755e1fSLight Hsieh {
10685f755e1fSLight Hsieh 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV, arg);
10695f755e1fSLight Hsieh }
10708174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_drive_set_raw);
10715f755e1fSLight Hsieh 
mtk_pinconf_drive_get_raw(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * val)10725f755e1fSLight Hsieh int mtk_pinconf_drive_get_raw(struct mtk_pinctrl *hw,
10735f755e1fSLight Hsieh 			       const struct mtk_pin_desc *desc, int *val)
10745f755e1fSLight Hsieh {
10755f755e1fSLight Hsieh 	return mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, val);
10765f755e1fSLight Hsieh }
10778174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_drive_get_raw);
10785f755e1fSLight Hsieh 
mtk_pinconf_adv_pull_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup,u32 arg)10790d7ca772SSean Wang int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
10800d7ca772SSean Wang 			     const struct mtk_pin_desc *desc, bool pullup,
10810d7ca772SSean Wang 			     u32 arg)
10820d7ca772SSean Wang {
10830d7ca772SSean Wang 	int err;
10840d7ca772SSean Wang 
10850d7ca772SSean Wang 	/* 10K off & 50K (75K) off, when (R0, R1) = (0, 0);
10860d7ca772SSean Wang 	 * 10K off & 50K (75K) on, when (R0, R1) = (0, 1);
10870d7ca772SSean Wang 	 * 10K on & 50K (75K) off, when (R0, R1) = (1, 0);
10880d7ca772SSean Wang 	 * 10K on & 50K (75K) on, when (R0, R1) = (1, 1)
10890d7ca772SSean Wang 	 */
1090ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R0, arg & 1);
10910d7ca772SSean Wang 	if (err)
10920d7ca772SSean Wang 		return 0;
10930d7ca772SSean Wang 
1094ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R1,
10950d7ca772SSean Wang 			       !!(arg & 2));
10960d7ca772SSean Wang 	if (err)
10970d7ca772SSean Wang 		return 0;
10980d7ca772SSean Wang 
10990d7ca772SSean Wang 	arg = pullup ? 0 : 1;
11000d7ca772SSean Wang 
1101ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PUPD, arg);
11020d7ca772SSean Wang 
110379348f6fSSean Wang 	/* If PUPD register is not supported for that pin, let's fallback to
110479348f6fSSean Wang 	 * general bias control.
110579348f6fSSean Wang 	 */
110679348f6fSSean Wang 	if (err == -ENOTSUPP) {
110779348f6fSSean Wang 		if (hw->soc->bias_set) {
110879348f6fSSean Wang 			err = hw->soc->bias_set(hw, desc, pullup);
110979348f6fSSean Wang 			if (err)
111079348f6fSSean Wang 				return err;
111179348f6fSSean Wang 		} else {
1112798a315fSHsin-Yi Wang 			err = mtk_pinconf_bias_set_rev1(hw, desc, pullup);
1113798a315fSHsin-Yi Wang 			if (err)
1114798a315fSHsin-Yi Wang 				err = mtk_pinconf_bias_set(hw, desc, pullup);
111579348f6fSSean Wang 		}
111679348f6fSSean Wang 	}
111779348f6fSSean Wang 
11180d7ca772SSean Wang 	return err;
11190d7ca772SSean Wang }
11208174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_adv_pull_set);
11210d7ca772SSean Wang 
mtk_pinconf_adv_pull_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup,u32 * val)11220d7ca772SSean Wang int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
11230d7ca772SSean Wang 			     const struct mtk_pin_desc *desc, bool pullup,
11240d7ca772SSean Wang 			     u32 *val)
11250d7ca772SSean Wang {
11260d7ca772SSean Wang 	u32 t, t2;
11270d7ca772SSean Wang 	int err;
11280d7ca772SSean Wang 
1129ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PUPD, &t);
113079348f6fSSean Wang 
113179348f6fSSean Wang 	/* If PUPD register is not supported for that pin, let's fallback to
113279348f6fSSean Wang 	 * general bias control.
113379348f6fSSean Wang 	 */
113479348f6fSSean Wang 	if (err == -ENOTSUPP) {
113579348f6fSSean Wang 		if (hw->soc->bias_get) {
113679348f6fSSean Wang 			err = hw->soc->bias_get(hw, desc, pullup, val);
113779348f6fSSean Wang 			if (err)
113879348f6fSSean Wang 				return err;
113979348f6fSSean Wang 		} else {
114079348f6fSSean Wang 			return -ENOTSUPP;
114179348f6fSSean Wang 		}
114279348f6fSSean Wang 	} else {
114379348f6fSSean Wang 		/* t == 0 supposes PULLUP for the customized PULL setup */
11440d7ca772SSean Wang 		if (err)
11450d7ca772SSean Wang 			return err;
11460d7ca772SSean Wang 
11470d7ca772SSean Wang 		if (pullup ^ !t)
11480d7ca772SSean Wang 			return -EINVAL;
114979348f6fSSean Wang 	}
11500d7ca772SSean Wang 
1151ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R0, &t);
11520d7ca772SSean Wang 	if (err)
11530d7ca772SSean Wang 		return err;
11540d7ca772SSean Wang 
1155ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R1, &t2);
11560d7ca772SSean Wang 	if (err)
11570d7ca772SSean Wang 		return err;
11580d7ca772SSean Wang 
11590d7ca772SSean Wang 	*val = (t | t2 << 1) & 0x7;
11600d7ca772SSean Wang 
11610d7ca772SSean Wang 	return 0;
11620d7ca772SSean Wang }
11638174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_adv_pull_get);
11645e73de34SZhiyong Tao 
mtk_pinconf_adv_drive_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)11655e73de34SZhiyong Tao int mtk_pinconf_adv_drive_set(struct mtk_pinctrl *hw,
11665e73de34SZhiyong Tao 			      const struct mtk_pin_desc *desc, u32 arg)
11675e73de34SZhiyong Tao {
11685e73de34SZhiyong Tao 	int err;
11695e73de34SZhiyong Tao 	int en = arg & 1;
11705e73de34SZhiyong Tao 	int e0 = !!(arg & 2);
11715e73de34SZhiyong Tao 	int e1 = !!(arg & 4);
11725e73de34SZhiyong Tao 
11735e73de34SZhiyong Tao 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_EN, en);
11745e73de34SZhiyong Tao 	if (err)
11755e73de34SZhiyong Tao 		return err;
11765e73de34SZhiyong Tao 
11775e73de34SZhiyong Tao 	if (!en)
11785e73de34SZhiyong Tao 		return err;
11795e73de34SZhiyong Tao 
11805e73de34SZhiyong Tao 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_E0, e0);
11815e73de34SZhiyong Tao 	if (err)
11825e73de34SZhiyong Tao 		return err;
11835e73de34SZhiyong Tao 
11845e73de34SZhiyong Tao 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_E1, e1);
11855e73de34SZhiyong Tao 	if (err)
11865e73de34SZhiyong Tao 		return err;
11875e73de34SZhiyong Tao 
11885e73de34SZhiyong Tao 	return err;
11895e73de34SZhiyong Tao }
11908174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_adv_drive_set);
11915e73de34SZhiyong Tao 
mtk_pinconf_adv_drive_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * val)11925e73de34SZhiyong Tao int mtk_pinconf_adv_drive_get(struct mtk_pinctrl *hw,
11935e73de34SZhiyong Tao 			      const struct mtk_pin_desc *desc, u32 *val)
11945e73de34SZhiyong Tao {
11955e73de34SZhiyong Tao 	u32 en, e0, e1;
11965e73de34SZhiyong Tao 	int err;
11975e73de34SZhiyong Tao 
11985e73de34SZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_EN, &en);
11995e73de34SZhiyong Tao 	if (err)
12005e73de34SZhiyong Tao 		return err;
12015e73de34SZhiyong Tao 
12025e73de34SZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_E0, &e0);
12035e73de34SZhiyong Tao 	if (err)
12045e73de34SZhiyong Tao 		return err;
12055e73de34SZhiyong Tao 
12065e73de34SZhiyong Tao 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_E1, &e1);
12075e73de34SZhiyong Tao 	if (err)
12085e73de34SZhiyong Tao 		return err;
12095e73de34SZhiyong Tao 
12105e73de34SZhiyong Tao 	*val = (en | e0 << 1 | e1 << 2) & 0x7;
12115e73de34SZhiyong Tao 
12125e73de34SZhiyong Tao 	return 0;
12135e73de34SZhiyong Tao }
12148174a851SLight Hsieh EXPORT_SYMBOL_GPL(mtk_pinconf_adv_drive_get);
1215c7acd6feSArnd Bergmann 
mtk_pinconf_adv_drive_set_raw(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)1216ea9d2ed4SZhiyong Tao int mtk_pinconf_adv_drive_set_raw(struct mtk_pinctrl *hw,
1217ea9d2ed4SZhiyong Tao 				  const struct mtk_pin_desc *desc, u32 arg)
1218ea9d2ed4SZhiyong Tao {
1219ea9d2ed4SZhiyong Tao 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_ADV, arg);
1220ea9d2ed4SZhiyong Tao }
1221ea9d2ed4SZhiyong Tao EXPORT_SYMBOL_GPL(mtk_pinconf_adv_drive_set_raw);
1222ea9d2ed4SZhiyong Tao 
mtk_pinconf_adv_drive_get_raw(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * val)1223ea9d2ed4SZhiyong Tao int mtk_pinconf_adv_drive_get_raw(struct mtk_pinctrl *hw,
1224ea9d2ed4SZhiyong Tao 				  const struct mtk_pin_desc *desc, u32 *val)
1225ea9d2ed4SZhiyong Tao {
1226ea9d2ed4SZhiyong Tao 	return mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_ADV, val);
1227ea9d2ed4SZhiyong Tao }
1228ea9d2ed4SZhiyong Tao EXPORT_SYMBOL_GPL(mtk_pinconf_adv_drive_get_raw);
1229ea9d2ed4SZhiyong Tao 
1230c7acd6feSArnd Bergmann MODULE_LICENSE("GPL v2");
1231c7acd6feSArnd Bergmann MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1232c7acd6feSArnd Bergmann MODULE_DESCRIPTION("Pin configuration library module for mediatek SoCs");
1233