xref: /linux/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c (revision 9afc305bfad79351b0e1fc2bcb04fc9a0631f865)
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 
9a1a503a8SSean Wang #include <linux/device.h>
10a1a503a8SSean Wang #include <linux/err.h>
11a1a503a8SSean Wang #include <linux/gpio.h>
12a1a503a8SSean Wang #include <linux/io.h>
13a1a503a8SSean Wang 
14a1a503a8SSean Wang #include "pinctrl-mtk-common-v2.h"
15a1a503a8SSean Wang 
16c2832197SSean Wang /**
17c2832197SSean Wang  * struct mtk_drive_desc - the structure that holds the information
18c2832197SSean Wang  *			    of the driving current
19c2832197SSean Wang  * @min:	the minimum current of this group
20c2832197SSean Wang  * @max:	the maximum current of this group
21c2832197SSean Wang  * @step:	the step current of this group
22c2832197SSean Wang  * @scal:	the weight factor
23c2832197SSean Wang  *
24c2832197SSean Wang  * formula: output = ((input) / step - 1) * scal
25c2832197SSean Wang  */
26c2832197SSean Wang struct mtk_drive_desc {
27c2832197SSean Wang 	u8 min;
28c2832197SSean Wang 	u8 max;
29c2832197SSean Wang 	u8 step;
30c2832197SSean Wang 	u8 scal;
31c2832197SSean Wang };
32c2832197SSean Wang 
33c2832197SSean Wang /* The groups of drive strength */
34c2832197SSean Wang const struct mtk_drive_desc mtk_drive[] = {
35c2832197SSean Wang 	[DRV_GRP0] = { 4, 16, 4, 1 },
36c2832197SSean Wang 	[DRV_GRP1] = { 4, 16, 4, 2 },
37c2832197SSean Wang 	[DRV_GRP2] = { 2, 8, 2, 1 },
38c2832197SSean Wang 	[DRV_GRP3] = { 2, 8, 2, 2 },
39c2832197SSean Wang 	[DRV_GRP4] = { 2, 16, 2, 1 },
40c2832197SSean Wang };
41c2832197SSean Wang 
42a1a503a8SSean Wang static void mtk_w32(struct mtk_pinctrl *pctl, u32 reg, u32 val)
43a1a503a8SSean Wang {
44a1a503a8SSean Wang 	writel_relaxed(val, pctl->base + reg);
45a1a503a8SSean Wang }
46a1a503a8SSean Wang 
47a1a503a8SSean Wang static u32 mtk_r32(struct mtk_pinctrl *pctl, u32 reg)
48a1a503a8SSean Wang {
49a1a503a8SSean Wang 	return readl_relaxed(pctl->base + reg);
50a1a503a8SSean Wang }
51a1a503a8SSean Wang 
52a1a503a8SSean Wang void mtk_rmw(struct mtk_pinctrl *pctl, u32 reg, u32 mask, u32 set)
53a1a503a8SSean Wang {
54a1a503a8SSean Wang 	u32 val;
55a1a503a8SSean Wang 
56a1a503a8SSean Wang 	val = mtk_r32(pctl, reg);
57a1a503a8SSean Wang 	val &= ~mask;
58a1a503a8SSean Wang 	val |= set;
59a1a503a8SSean Wang 	mtk_w32(pctl, reg, val);
60a1a503a8SSean Wang }
61a1a503a8SSean Wang 
62a1a503a8SSean Wang static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw, int pin,
63a1a503a8SSean Wang 				   const struct mtk_pin_reg_calc *rc,
64a1a503a8SSean Wang 				   struct mtk_pin_field *pfd)
65a1a503a8SSean Wang {
66a1a503a8SSean Wang 	const struct mtk_pin_field_calc *c, *e;
67a1a503a8SSean Wang 	u32 bits;
68a1a503a8SSean Wang 
69a1a503a8SSean Wang 	c = rc->range;
70a1a503a8SSean Wang 	e = c + rc->nranges;
71a1a503a8SSean Wang 
72a1a503a8SSean Wang 	while (c < e) {
73a1a503a8SSean Wang 		if (pin >= c->s_pin && pin <= c->e_pin)
74a1a503a8SSean Wang 			break;
75a1a503a8SSean Wang 		c++;
76a1a503a8SSean Wang 	}
77a1a503a8SSean Wang 
78a1a503a8SSean Wang 	if (c >= e) {
79a1a503a8SSean Wang 		dev_err(hw->dev, "Out of range for pin = %d\n", pin);
80a1a503a8SSean Wang 		return -EINVAL;
81a1a503a8SSean Wang 	}
82a1a503a8SSean Wang 
83b906faf7SSean Wang 	/* Calculated bits as the overall offset the pin is located at,
84b906faf7SSean Wang 	 * if c->fixed is held, that determines the all the pins in the
85b906faf7SSean Wang 	 * range use the same field with the s_pin.
86b906faf7SSean Wang 	 */
87b906faf7SSean Wang 	bits = c->fixed ? c->s_bit : c->s_bit + (pin - c->s_pin) * (c->x_bits);
88a1a503a8SSean Wang 
89b906faf7SSean Wang 	/* Fill pfd from bits. For example 32-bit register applied is assumed
90b906faf7SSean Wang 	 * when c->sz_reg is equal to 32.
91b906faf7SSean Wang 	 */
92b906faf7SSean Wang 	pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg);
93b906faf7SSean Wang 	pfd->bitpos = bits % c->sz_reg;
94a1a503a8SSean Wang 	pfd->mask = (1 << c->x_bits) - 1;
95a1a503a8SSean Wang 
96a1a503a8SSean Wang 	/* pfd->next is used for indicating that bit wrapping-around happens
97a1a503a8SSean Wang 	 * which requires the manipulation for bit 0 starting in the next
98a1a503a8SSean Wang 	 * register to form the complete field read/write.
99a1a503a8SSean Wang 	 */
100b906faf7SSean Wang 	pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0;
101a1a503a8SSean Wang 
102a1a503a8SSean Wang 	return 0;
103a1a503a8SSean Wang }
104a1a503a8SSean Wang 
105a1a503a8SSean Wang static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw, int pin,
106a1a503a8SSean Wang 				int field, struct mtk_pin_field *pfd)
107a1a503a8SSean Wang {
108a1a503a8SSean Wang 	const struct mtk_pin_reg_calc *rc;
109a1a503a8SSean Wang 
110a1a503a8SSean Wang 	if (field < 0 || field >= PINCTRL_PIN_REG_MAX) {
111a1a503a8SSean Wang 		dev_err(hw->dev, "Invalid Field %d\n", field);
112a1a503a8SSean Wang 		return -EINVAL;
113a1a503a8SSean Wang 	}
114a1a503a8SSean Wang 
115a1a503a8SSean Wang 	if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) {
116a1a503a8SSean Wang 		rc = &hw->soc->reg_cal[field];
117a1a503a8SSean Wang 	} else {
118a1a503a8SSean Wang 		dev_err(hw->dev, "Undefined range for field %d\n", field);
119a1a503a8SSean Wang 		return -EINVAL;
120a1a503a8SSean Wang 	}
121a1a503a8SSean Wang 
122a1a503a8SSean Wang 	return mtk_hw_pin_field_lookup(hw, pin, rc, pfd);
123a1a503a8SSean Wang }
124a1a503a8SSean Wang 
125a1a503a8SSean Wang static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
126a1a503a8SSean Wang {
127a1a503a8SSean Wang 	*l = 32 - pf->bitpos;
128a1a503a8SSean Wang 	*h = get_count_order(pf->mask) - *l;
129a1a503a8SSean Wang }
130a1a503a8SSean Wang 
131a1a503a8SSean Wang static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw,
132a1a503a8SSean Wang 				     struct mtk_pin_field *pf, int value)
133a1a503a8SSean Wang {
134a1a503a8SSean Wang 	int nbits_l, nbits_h;
135a1a503a8SSean Wang 
136a1a503a8SSean Wang 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
137a1a503a8SSean Wang 
138a1a503a8SSean Wang 	mtk_rmw(hw, pf->offset, pf->mask << pf->bitpos,
139a1a503a8SSean Wang 		(value & pf->mask) << pf->bitpos);
140a1a503a8SSean Wang 
141a1a503a8SSean Wang 	mtk_rmw(hw, pf->offset + pf->next, BIT(nbits_h) - 1,
142a1a503a8SSean Wang 		(value & pf->mask) >> nbits_l);
143a1a503a8SSean Wang }
144a1a503a8SSean Wang 
145a1a503a8SSean Wang static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw,
146a1a503a8SSean Wang 				    struct mtk_pin_field *pf, int *value)
147a1a503a8SSean Wang {
148a1a503a8SSean Wang 	int nbits_l, nbits_h, h, l;
149a1a503a8SSean Wang 
150a1a503a8SSean Wang 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
151a1a503a8SSean Wang 
152a1a503a8SSean Wang 	l  = (mtk_r32(hw, pf->offset) >> pf->bitpos) & (BIT(nbits_l) - 1);
153a1a503a8SSean Wang 	h  = (mtk_r32(hw, pf->offset + pf->next)) & (BIT(nbits_h) - 1);
154a1a503a8SSean Wang 
155a1a503a8SSean Wang 	*value = (h << nbits_l) | l;
156a1a503a8SSean Wang }
157a1a503a8SSean Wang 
158a1a503a8SSean Wang int mtk_hw_set_value(struct mtk_pinctrl *hw, int pin, int field, int value)
159a1a503a8SSean Wang {
160a1a503a8SSean Wang 	struct mtk_pin_field pf;
161a1a503a8SSean Wang 	int err;
162a1a503a8SSean Wang 
163a1a503a8SSean Wang 	err = mtk_hw_pin_field_get(hw, pin, field, &pf);
164a1a503a8SSean Wang 	if (err)
165a1a503a8SSean Wang 		return err;
166a1a503a8SSean Wang 
167a1a503a8SSean Wang 	if (!pf.next)
168a1a503a8SSean Wang 		mtk_rmw(hw, pf.offset, pf.mask << pf.bitpos,
169a1a503a8SSean Wang 			(value & pf.mask) << pf.bitpos);
170a1a503a8SSean Wang 	else
171a1a503a8SSean Wang 		mtk_hw_write_cross_field(hw, &pf, value);
172a1a503a8SSean Wang 
173a1a503a8SSean Wang 	return 0;
174a1a503a8SSean Wang }
175a1a503a8SSean Wang 
176a1a503a8SSean Wang int mtk_hw_get_value(struct mtk_pinctrl *hw, int pin, int field, int *value)
177a1a503a8SSean Wang {
178a1a503a8SSean Wang 	struct mtk_pin_field pf;
179a1a503a8SSean Wang 	int err;
180a1a503a8SSean Wang 
181a1a503a8SSean Wang 	err = mtk_hw_pin_field_get(hw, pin, field, &pf);
182a1a503a8SSean Wang 	if (err)
183a1a503a8SSean Wang 		return err;
184a1a503a8SSean Wang 
185a1a503a8SSean Wang 	if (!pf.next)
186a1a503a8SSean Wang 		*value = (mtk_r32(hw, pf.offset) >> pf.bitpos) & pf.mask;
187a1a503a8SSean Wang 	else
188a1a503a8SSean Wang 		mtk_hw_read_cross_field(hw, &pf, value);
189a1a503a8SSean Wang 
190a1a503a8SSean Wang 	return 0;
191a1a503a8SSean Wang }
192c2832197SSean Wang 
193*9afc305bSSean Wang /* Revision 0 */
19485430152SSean Wang int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
19585430152SSean Wang 				 const struct mtk_pin_desc *desc)
19685430152SSean Wang {
19785430152SSean Wang 	int err;
19885430152SSean Wang 
19985430152SSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_PU,
20085430152SSean Wang 			       MTK_DISABLE);
20185430152SSean Wang 	if (err)
20285430152SSean Wang 		return err;
20385430152SSean Wang 
20485430152SSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_PD,
20585430152SSean Wang 			       MTK_DISABLE);
20685430152SSean Wang 	if (err)
20785430152SSean Wang 		return err;
20885430152SSean Wang 
20985430152SSean Wang 	return 0;
21085430152SSean Wang }
21185430152SSean Wang 
21285430152SSean Wang int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
21385430152SSean Wang 				 const struct mtk_pin_desc *desc, int *res)
21485430152SSean Wang {
21585430152SSean Wang 	int v, v2;
21685430152SSean Wang 	int err;
21785430152SSean Wang 
21885430152SSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_PU, &v);
21985430152SSean Wang 	if (err)
22085430152SSean Wang 		return err;
22185430152SSean Wang 
22285430152SSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_PD, &v2);
22385430152SSean Wang 	if (err)
22485430152SSean Wang 		return err;
22585430152SSean Wang 
22685430152SSean Wang 	if (v == MTK_ENABLE || v2 == MTK_ENABLE)
22785430152SSean Wang 		return -EINVAL;
22885430152SSean Wang 
22985430152SSean Wang 	*res = 1;
23085430152SSean Wang 
23185430152SSean Wang 	return 0;
23285430152SSean Wang }
23385430152SSean Wang 
23485430152SSean Wang int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
23585430152SSean Wang 			 const struct mtk_pin_desc *desc, bool pullup)
23685430152SSean Wang {
23785430152SSean Wang 	int err, arg;
23885430152SSean Wang 
23985430152SSean Wang 	arg = pullup ? 1 : 2;
24085430152SSean Wang 
24185430152SSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_PU, arg & 1);
24285430152SSean Wang 	if (err)
24385430152SSean Wang 		return err;
24485430152SSean Wang 
24585430152SSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_PD,
24685430152SSean Wang 			       !!(arg & 2));
24785430152SSean Wang 	if (err)
24885430152SSean Wang 		return err;
24985430152SSean Wang 
25085430152SSean Wang 	return 0;
25185430152SSean Wang }
25285430152SSean Wang 
25385430152SSean Wang int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
25485430152SSean Wang 			 const struct mtk_pin_desc *desc, bool pullup, int *res)
25585430152SSean Wang {
25685430152SSean Wang 	int reg, err, v;
25785430152SSean Wang 
25885430152SSean Wang 	reg = pullup ? PINCTRL_PIN_REG_PU : PINCTRL_PIN_REG_PD;
25985430152SSean Wang 
26085430152SSean Wang 	err = mtk_hw_get_value(hw, desc->number, reg, &v);
26185430152SSean Wang 	if (err)
26285430152SSean Wang 		return err;
26385430152SSean Wang 
26485430152SSean Wang 	if (!v)
26585430152SSean Wang 		return -EINVAL;
26685430152SSean Wang 
26785430152SSean Wang 	*res = 1;
26885430152SSean Wang 
26985430152SSean Wang 	return 0;
27085430152SSean Wang }
27185430152SSean Wang 
272*9afc305bSSean Wang /* Revision 1 */
273*9afc305bSSean Wang int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
274*9afc305bSSean Wang 				      const struct mtk_pin_desc *desc)
275*9afc305bSSean Wang {
276*9afc305bSSean Wang 	int err;
277*9afc305bSSean Wang 
278*9afc305bSSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_PULLEN,
279*9afc305bSSean Wang 			       MTK_DISABLE);
280*9afc305bSSean Wang 	if (err)
281*9afc305bSSean Wang 		return err;
282*9afc305bSSean Wang 
283*9afc305bSSean Wang 	return 0;
284*9afc305bSSean Wang }
285*9afc305bSSean Wang 
286*9afc305bSSean Wang int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
287*9afc305bSSean Wang 				      const struct mtk_pin_desc *desc, int *res)
288*9afc305bSSean Wang {
289*9afc305bSSean Wang 	int v, err;
290*9afc305bSSean Wang 
291*9afc305bSSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_PULLEN, &v);
292*9afc305bSSean Wang 	if (err)
293*9afc305bSSean Wang 		return err;
294*9afc305bSSean Wang 
295*9afc305bSSean Wang 	if (v == MTK_ENABLE)
296*9afc305bSSean Wang 		return -EINVAL;
297*9afc305bSSean Wang 
298*9afc305bSSean Wang 	*res = 1;
299*9afc305bSSean Wang 
300*9afc305bSSean Wang 	return 0;
301*9afc305bSSean Wang }
302*9afc305bSSean Wang 
303*9afc305bSSean Wang int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
304*9afc305bSSean Wang 			      const struct mtk_pin_desc *desc, bool pullup)
305*9afc305bSSean Wang {
306*9afc305bSSean Wang 	int err, arg;
307*9afc305bSSean Wang 
308*9afc305bSSean Wang 	arg = pullup ? MTK_PULLUP : MTK_PULLDOWN;
309*9afc305bSSean Wang 
310*9afc305bSSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_PULLEN,
311*9afc305bSSean Wang 			       MTK_ENABLE);
312*9afc305bSSean Wang 	if (err)
313*9afc305bSSean Wang 		return err;
314*9afc305bSSean Wang 
315*9afc305bSSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_PULLSEL, arg);
316*9afc305bSSean Wang 	if (err)
317*9afc305bSSean Wang 		return err;
318*9afc305bSSean Wang 
319*9afc305bSSean Wang 	return 0;
320*9afc305bSSean Wang }
321*9afc305bSSean Wang 
322*9afc305bSSean Wang int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
323*9afc305bSSean Wang 			      const struct mtk_pin_desc *desc, bool pullup,
324*9afc305bSSean Wang 			      int *res)
325*9afc305bSSean Wang {
326*9afc305bSSean Wang 	int err, v;
327*9afc305bSSean Wang 
328*9afc305bSSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_PULLEN, &v);
329*9afc305bSSean Wang 	if (err)
330*9afc305bSSean Wang 		return err;
331*9afc305bSSean Wang 
332*9afc305bSSean Wang 	if (v == MTK_DISABLE)
333*9afc305bSSean Wang 		return -EINVAL;
334*9afc305bSSean Wang 
335*9afc305bSSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_PULLSEL, &v);
336*9afc305bSSean Wang 	if (err)
337*9afc305bSSean Wang 		return err;
338*9afc305bSSean Wang 
339*9afc305bSSean Wang 	if (pullup ^ (v == MTK_PULLUP))
340*9afc305bSSean Wang 		return -EINVAL;
341*9afc305bSSean Wang 
342*9afc305bSSean Wang 	*res = 1;
343*9afc305bSSean Wang 
344*9afc305bSSean Wang 	return 0;
345*9afc305bSSean Wang }
346*9afc305bSSean Wang 
347c2832197SSean Wang /* Revision 0 */
348c2832197SSean Wang int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
349c2832197SSean Wang 			  const struct mtk_pin_desc *desc, u32 arg)
350c2832197SSean Wang {
351c2832197SSean Wang 	const struct mtk_drive_desc *tb;
352c2832197SSean Wang 	int err = -ENOTSUPP;
353c2832197SSean Wang 
354c2832197SSean Wang 	tb = &mtk_drive[desc->drv_n];
355c2832197SSean Wang 	/* 4mA when (e8, e4) = (0, 0)
356c2832197SSean Wang 	 * 8mA when (e8, e4) = (0, 1)
357c2832197SSean Wang 	 * 12mA when (e8, e4) = (1, 0)
358c2832197SSean Wang 	 * 16mA when (e8, e4) = (1, 1)
359c2832197SSean Wang 	 */
360c2832197SSean Wang 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
361c2832197SSean Wang 		arg = (arg / tb->step - 1) * tb->scal;
362c2832197SSean Wang 		err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_E4,
363c2832197SSean Wang 				       arg & 0x1);
364c2832197SSean Wang 		if (err)
365c2832197SSean Wang 			return err;
366c2832197SSean Wang 
367c2832197SSean Wang 		err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_E8,
368c2832197SSean Wang 				       (arg & 0x2) >> 1);
369c2832197SSean Wang 		if (err)
370c2832197SSean Wang 			return err;
371c2832197SSean Wang 	}
372c2832197SSean Wang 
373c2832197SSean Wang 	return err;
374c2832197SSean Wang }
375c2832197SSean Wang 
376c2832197SSean Wang int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
377c2832197SSean Wang 			  const struct mtk_pin_desc *desc, int *val)
378c2832197SSean Wang {
379c2832197SSean Wang 	const struct mtk_drive_desc *tb;
380c2832197SSean Wang 	int err, val1, val2;
381c2832197SSean Wang 
382c2832197SSean Wang 	tb = &mtk_drive[desc->drv_n];
383c2832197SSean Wang 
384c2832197SSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_E4, &val1);
385c2832197SSean Wang 	if (err)
386c2832197SSean Wang 		return err;
387c2832197SSean Wang 
388c2832197SSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_E8, &val2);
389c2832197SSean Wang 	if (err)
390c2832197SSean Wang 		return err;
391c2832197SSean Wang 
392c2832197SSean Wang 	/* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1)
393c2832197SSean Wang 	 * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1)
394c2832197SSean Wang 	 */
395c2832197SSean Wang 	*val = (((val2 << 1) + val1) / tb->scal + 1) * tb->step;
396c2832197SSean Wang 
397c2832197SSean Wang 	return 0;
398c2832197SSean Wang }
3993ad38a14SSean Wang 
4003ad38a14SSean Wang /* Revision 1 */
4013ad38a14SSean Wang int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
4023ad38a14SSean Wang 			       const struct mtk_pin_desc *desc, u32 arg)
4033ad38a14SSean Wang {
4043ad38a14SSean Wang 	const struct mtk_drive_desc *tb;
4053ad38a14SSean Wang 	int err = -ENOTSUPP;
4063ad38a14SSean Wang 
4073ad38a14SSean Wang 	tb = &mtk_drive[desc->drv_n];
4083ad38a14SSean Wang 
4093ad38a14SSean Wang 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
4103ad38a14SSean Wang 		arg = (arg / tb->step - 1) * tb->scal;
4113ad38a14SSean Wang 
4123ad38a14SSean Wang 		err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_DRV,
4133ad38a14SSean Wang 				       arg);
4143ad38a14SSean Wang 		if (err)
4153ad38a14SSean Wang 			return err;
4163ad38a14SSean Wang 	}
4173ad38a14SSean Wang 
4183ad38a14SSean Wang 	return err;
4193ad38a14SSean Wang }
4203ad38a14SSean Wang 
4213ad38a14SSean Wang int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
4223ad38a14SSean Wang 			       const struct mtk_pin_desc *desc, int *val)
4233ad38a14SSean Wang {
4243ad38a14SSean Wang 	const struct mtk_drive_desc *tb;
4253ad38a14SSean Wang 	int err, val1;
4263ad38a14SSean Wang 
4273ad38a14SSean Wang 	tb = &mtk_drive[desc->drv_n];
4283ad38a14SSean Wang 
4293ad38a14SSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_DRV, &val1);
4303ad38a14SSean Wang 	if (err)
4313ad38a14SSean Wang 		return err;
4323ad38a14SSean Wang 
4333ad38a14SSean Wang 	*val = ((val1 & 0x7) / tb->scal + 1) * tb->step;
4343ad38a14SSean Wang 
4353ad38a14SSean Wang 	return 0;
4363ad38a14SSean Wang }
4370d7ca772SSean Wang 
4380d7ca772SSean Wang int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
4390d7ca772SSean Wang 			     const struct mtk_pin_desc *desc, bool pullup,
4400d7ca772SSean Wang 			     u32 arg)
4410d7ca772SSean Wang {
4420d7ca772SSean Wang 	int err;
4430d7ca772SSean Wang 
4440d7ca772SSean Wang 	/* 10K off & 50K (75K) off, when (R0, R1) = (0, 0);
4450d7ca772SSean Wang 	 * 10K off & 50K (75K) on, when (R0, R1) = (0, 1);
4460d7ca772SSean Wang 	 * 10K on & 50K (75K) off, when (R0, R1) = (1, 0);
4470d7ca772SSean Wang 	 * 10K on & 50K (75K) on, when (R0, R1) = (1, 1)
4480d7ca772SSean Wang 	 */
4490d7ca772SSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_R0, arg & 1);
4500d7ca772SSean Wang 	if (err)
4510d7ca772SSean Wang 		return 0;
4520d7ca772SSean Wang 
4530d7ca772SSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_R1,
4540d7ca772SSean Wang 			       !!(arg & 2));
4550d7ca772SSean Wang 	if (err)
4560d7ca772SSean Wang 		return 0;
4570d7ca772SSean Wang 
4580d7ca772SSean Wang 	arg = pullup ? 0 : 1;
4590d7ca772SSean Wang 
4600d7ca772SSean Wang 	err = mtk_hw_set_value(hw, desc->number, PINCTRL_PIN_REG_PUPD, arg);
4610d7ca772SSean Wang 
4620d7ca772SSean Wang 	return err;
4630d7ca772SSean Wang }
4640d7ca772SSean Wang 
4650d7ca772SSean Wang int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
4660d7ca772SSean Wang 			     const struct mtk_pin_desc *desc, bool pullup,
4670d7ca772SSean Wang 			     u32 *val)
4680d7ca772SSean Wang {
4690d7ca772SSean Wang 	u32 t, t2;
4700d7ca772SSean Wang 	int err;
4710d7ca772SSean Wang 
4720d7ca772SSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_PUPD, &t);
4730d7ca772SSean Wang 	if (err)
4740d7ca772SSean Wang 		return err;
4750d7ca772SSean Wang 
4760d7ca772SSean Wang 	/* t == 0 supposes PULLUP for the customized PULL setup */
4770d7ca772SSean Wang 	if (pullup ^ !t)
4780d7ca772SSean Wang 		return -EINVAL;
4790d7ca772SSean Wang 
4800d7ca772SSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_R0, &t);
4810d7ca772SSean Wang 	if (err)
4820d7ca772SSean Wang 		return err;
4830d7ca772SSean Wang 
4840d7ca772SSean Wang 	err = mtk_hw_get_value(hw, desc->number, PINCTRL_PIN_REG_R1, &t2);
4850d7ca772SSean Wang 	if (err)
4860d7ca772SSean Wang 		return err;
4870d7ca772SSean Wang 
4880d7ca772SSean Wang 	*val = (t | t2 << 1) & 0x7;
4890d7ca772SSean Wang 
4900d7ca772SSean Wang 	return 0;
4910d7ca772SSean Wang }
492