xref: /linux/drivers/pinctrl/mediatek/pinctrl-mtk-common-v2.c (revision ea051eb384139bd183757761aa83362f2290996d)
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 
62*ea051eb3SSean Wang static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw,
63*ea051eb3SSean Wang 				   const struct mtk_pin_desc *desc,
64a1a503a8SSean Wang 				   const struct mtk_pin_reg_calc *rc,
65a1a503a8SSean Wang 				   struct mtk_pin_field *pfd)
66a1a503a8SSean Wang {
67a1a503a8SSean Wang 	const struct mtk_pin_field_calc *c, *e;
68a1a503a8SSean Wang 	u32 bits;
69a1a503a8SSean Wang 
70a1a503a8SSean Wang 	c = rc->range;
71a1a503a8SSean Wang 	e = c + rc->nranges;
72a1a503a8SSean Wang 
73a1a503a8SSean Wang 	while (c < e) {
74*ea051eb3SSean Wang 		if (desc->number >= c->s_pin && desc->number <= c->e_pin)
75a1a503a8SSean Wang 			break;
76a1a503a8SSean Wang 		c++;
77a1a503a8SSean Wang 	}
78a1a503a8SSean Wang 
79a1a503a8SSean Wang 	if (c >= e) {
80*ea051eb3SSean Wang 		dev_err(hw->dev, "Out of range for pin = %d (%s)\n",
81*ea051eb3SSean Wang 			desc->number, desc->name);
82a1a503a8SSean Wang 		return -EINVAL;
83a1a503a8SSean Wang 	}
84a1a503a8SSean Wang 
85b906faf7SSean Wang 	/* Calculated bits as the overall offset the pin is located at,
86b906faf7SSean Wang 	 * if c->fixed is held, that determines the all the pins in the
87b906faf7SSean Wang 	 * range use the same field with the s_pin.
88b906faf7SSean Wang 	 */
89*ea051eb3SSean Wang 	bits = c->fixed ? c->s_bit : c->s_bit +
90*ea051eb3SSean Wang 	       (desc->number - c->s_pin) * (c->x_bits);
91a1a503a8SSean Wang 
92b906faf7SSean Wang 	/* Fill pfd from bits. For example 32-bit register applied is assumed
93b906faf7SSean Wang 	 * when c->sz_reg is equal to 32.
94b906faf7SSean Wang 	 */
95b906faf7SSean Wang 	pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg);
96b906faf7SSean Wang 	pfd->bitpos = bits % c->sz_reg;
97a1a503a8SSean Wang 	pfd->mask = (1 << c->x_bits) - 1;
98a1a503a8SSean Wang 
99a1a503a8SSean Wang 	/* pfd->next is used for indicating that bit wrapping-around happens
100a1a503a8SSean Wang 	 * which requires the manipulation for bit 0 starting in the next
101a1a503a8SSean Wang 	 * register to form the complete field read/write.
102a1a503a8SSean Wang 	 */
103b906faf7SSean Wang 	pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0;
104a1a503a8SSean Wang 
105a1a503a8SSean Wang 	return 0;
106a1a503a8SSean Wang }
107a1a503a8SSean Wang 
108*ea051eb3SSean Wang static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw,
109*ea051eb3SSean Wang 				const struct mtk_pin_desc *desc,
110a1a503a8SSean Wang 				int field, struct mtk_pin_field *pfd)
111a1a503a8SSean Wang {
112a1a503a8SSean Wang 	const struct mtk_pin_reg_calc *rc;
113a1a503a8SSean Wang 
114a1a503a8SSean Wang 	if (field < 0 || field >= PINCTRL_PIN_REG_MAX) {
115a1a503a8SSean Wang 		dev_err(hw->dev, "Invalid Field %d\n", field);
116a1a503a8SSean Wang 		return -EINVAL;
117a1a503a8SSean Wang 	}
118a1a503a8SSean Wang 
119a1a503a8SSean Wang 	if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) {
120a1a503a8SSean Wang 		rc = &hw->soc->reg_cal[field];
121a1a503a8SSean Wang 	} else {
122a1a503a8SSean Wang 		dev_err(hw->dev, "Undefined range for field %d\n", field);
123a1a503a8SSean Wang 		return -EINVAL;
124a1a503a8SSean Wang 	}
125a1a503a8SSean Wang 
126*ea051eb3SSean Wang 	return mtk_hw_pin_field_lookup(hw, desc, rc, pfd);
127a1a503a8SSean Wang }
128a1a503a8SSean Wang 
129a1a503a8SSean Wang static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
130a1a503a8SSean Wang {
131a1a503a8SSean Wang 	*l = 32 - pf->bitpos;
132a1a503a8SSean Wang 	*h = get_count_order(pf->mask) - *l;
133a1a503a8SSean Wang }
134a1a503a8SSean Wang 
135a1a503a8SSean Wang static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw,
136a1a503a8SSean Wang 				     struct mtk_pin_field *pf, int value)
137a1a503a8SSean Wang {
138a1a503a8SSean Wang 	int nbits_l, nbits_h;
139a1a503a8SSean Wang 
140a1a503a8SSean Wang 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
141a1a503a8SSean Wang 
142a1a503a8SSean Wang 	mtk_rmw(hw, pf->offset, pf->mask << pf->bitpos,
143a1a503a8SSean Wang 		(value & pf->mask) << pf->bitpos);
144a1a503a8SSean Wang 
145a1a503a8SSean Wang 	mtk_rmw(hw, pf->offset + pf->next, BIT(nbits_h) - 1,
146a1a503a8SSean Wang 		(value & pf->mask) >> nbits_l);
147a1a503a8SSean Wang }
148a1a503a8SSean Wang 
149a1a503a8SSean Wang static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw,
150a1a503a8SSean Wang 				    struct mtk_pin_field *pf, int *value)
151a1a503a8SSean Wang {
152a1a503a8SSean Wang 	int nbits_l, nbits_h, h, l;
153a1a503a8SSean Wang 
154a1a503a8SSean Wang 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
155a1a503a8SSean Wang 
156a1a503a8SSean Wang 	l  = (mtk_r32(hw, pf->offset) >> pf->bitpos) & (BIT(nbits_l) - 1);
157a1a503a8SSean Wang 	h  = (mtk_r32(hw, pf->offset + pf->next)) & (BIT(nbits_h) - 1);
158a1a503a8SSean Wang 
159a1a503a8SSean Wang 	*value = (h << nbits_l) | l;
160a1a503a8SSean Wang }
161a1a503a8SSean Wang 
162*ea051eb3SSean Wang int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
163*ea051eb3SSean Wang 		     int field, int value)
164a1a503a8SSean Wang {
165a1a503a8SSean Wang 	struct mtk_pin_field pf;
166a1a503a8SSean Wang 	int err;
167a1a503a8SSean Wang 
168*ea051eb3SSean Wang 	err = mtk_hw_pin_field_get(hw, desc, field, &pf);
169a1a503a8SSean Wang 	if (err)
170a1a503a8SSean Wang 		return err;
171a1a503a8SSean Wang 
172a1a503a8SSean Wang 	if (!pf.next)
173a1a503a8SSean Wang 		mtk_rmw(hw, pf.offset, pf.mask << pf.bitpos,
174a1a503a8SSean Wang 			(value & pf.mask) << pf.bitpos);
175a1a503a8SSean Wang 	else
176a1a503a8SSean Wang 		mtk_hw_write_cross_field(hw, &pf, value);
177a1a503a8SSean Wang 
178a1a503a8SSean Wang 	return 0;
179a1a503a8SSean Wang }
180a1a503a8SSean Wang 
181*ea051eb3SSean Wang int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
182*ea051eb3SSean Wang 		     int field, int *value)
183a1a503a8SSean Wang {
184a1a503a8SSean Wang 	struct mtk_pin_field pf;
185a1a503a8SSean Wang 	int err;
186a1a503a8SSean Wang 
187*ea051eb3SSean Wang 	err = mtk_hw_pin_field_get(hw, desc, field, &pf);
188a1a503a8SSean Wang 	if (err)
189a1a503a8SSean Wang 		return err;
190a1a503a8SSean Wang 
191a1a503a8SSean Wang 	if (!pf.next)
192a1a503a8SSean Wang 		*value = (mtk_r32(hw, pf.offset) >> pf.bitpos) & pf.mask;
193a1a503a8SSean Wang 	else
194a1a503a8SSean Wang 		mtk_hw_read_cross_field(hw, &pf, value);
195a1a503a8SSean Wang 
196a1a503a8SSean Wang 	return 0;
197a1a503a8SSean Wang }
198c2832197SSean Wang 
1999afc305bSSean Wang /* Revision 0 */
20085430152SSean Wang int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
20185430152SSean Wang 				 const struct mtk_pin_desc *desc)
20285430152SSean Wang {
20385430152SSean Wang 	int err;
20485430152SSean Wang 
205*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU,
20685430152SSean Wang 			       MTK_DISABLE);
20785430152SSean Wang 	if (err)
20885430152SSean Wang 		return err;
20985430152SSean Wang 
210*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
21185430152SSean Wang 			       MTK_DISABLE);
21285430152SSean Wang 	if (err)
21385430152SSean Wang 		return err;
21485430152SSean Wang 
21585430152SSean Wang 	return 0;
21685430152SSean Wang }
21785430152SSean Wang 
21885430152SSean Wang int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
21985430152SSean Wang 				 const struct mtk_pin_desc *desc, int *res)
22085430152SSean Wang {
22185430152SSean Wang 	int v, v2;
22285430152SSean Wang 	int err;
22385430152SSean Wang 
224*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &v);
22585430152SSean Wang 	if (err)
22685430152SSean Wang 		return err;
22785430152SSean Wang 
228*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &v2);
22985430152SSean Wang 	if (err)
23085430152SSean Wang 		return err;
23185430152SSean Wang 
23285430152SSean Wang 	if (v == MTK_ENABLE || v2 == MTK_ENABLE)
23385430152SSean Wang 		return -EINVAL;
23485430152SSean Wang 
23585430152SSean Wang 	*res = 1;
23685430152SSean Wang 
23785430152SSean Wang 	return 0;
23885430152SSean Wang }
23985430152SSean Wang 
24085430152SSean Wang int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
24185430152SSean Wang 			 const struct mtk_pin_desc *desc, bool pullup)
24285430152SSean Wang {
24385430152SSean Wang 	int err, arg;
24485430152SSean Wang 
24585430152SSean Wang 	arg = pullup ? 1 : 2;
24685430152SSean Wang 
247*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU, arg & 1);
24885430152SSean Wang 	if (err)
24985430152SSean Wang 		return err;
25085430152SSean Wang 
251*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
25285430152SSean Wang 			       !!(arg & 2));
25385430152SSean Wang 	if (err)
25485430152SSean Wang 		return err;
25585430152SSean Wang 
25685430152SSean Wang 	return 0;
25785430152SSean Wang }
25885430152SSean Wang 
25985430152SSean Wang int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
26085430152SSean Wang 			 const struct mtk_pin_desc *desc, bool pullup, int *res)
26185430152SSean Wang {
26285430152SSean Wang 	int reg, err, v;
26385430152SSean Wang 
26485430152SSean Wang 	reg = pullup ? PINCTRL_PIN_REG_PU : PINCTRL_PIN_REG_PD;
26585430152SSean Wang 
266*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, reg, &v);
26785430152SSean Wang 	if (err)
26885430152SSean Wang 		return err;
26985430152SSean Wang 
27085430152SSean Wang 	if (!v)
27185430152SSean Wang 		return -EINVAL;
27285430152SSean Wang 
27385430152SSean Wang 	*res = 1;
27485430152SSean Wang 
27585430152SSean Wang 	return 0;
27685430152SSean Wang }
27785430152SSean Wang 
2789afc305bSSean Wang /* Revision 1 */
2799afc305bSSean Wang int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
2809afc305bSSean Wang 				      const struct mtk_pin_desc *desc)
2819afc305bSSean Wang {
2829afc305bSSean Wang 	int err;
2839afc305bSSean Wang 
284*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
2859afc305bSSean Wang 			       MTK_DISABLE);
2869afc305bSSean Wang 	if (err)
2879afc305bSSean Wang 		return err;
2889afc305bSSean Wang 
2899afc305bSSean Wang 	return 0;
2909afc305bSSean Wang }
2919afc305bSSean Wang 
2929afc305bSSean Wang int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
2939afc305bSSean Wang 				      const struct mtk_pin_desc *desc, int *res)
2949afc305bSSean Wang {
2959afc305bSSean Wang 	int v, err;
2969afc305bSSean Wang 
297*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
2989afc305bSSean Wang 	if (err)
2999afc305bSSean Wang 		return err;
3009afc305bSSean Wang 
3019afc305bSSean Wang 	if (v == MTK_ENABLE)
3029afc305bSSean Wang 		return -EINVAL;
3039afc305bSSean Wang 
3049afc305bSSean Wang 	*res = 1;
3059afc305bSSean Wang 
3069afc305bSSean Wang 	return 0;
3079afc305bSSean Wang }
3089afc305bSSean Wang 
3099afc305bSSean Wang int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
3109afc305bSSean Wang 			      const struct mtk_pin_desc *desc, bool pullup)
3119afc305bSSean Wang {
3129afc305bSSean Wang 	int err, arg;
3139afc305bSSean Wang 
3149afc305bSSean Wang 	arg = pullup ? MTK_PULLUP : MTK_PULLDOWN;
3159afc305bSSean Wang 
316*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
3179afc305bSSean Wang 			       MTK_ENABLE);
3189afc305bSSean Wang 	if (err)
3199afc305bSSean Wang 		return err;
3209afc305bSSean Wang 
321*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, arg);
3229afc305bSSean Wang 	if (err)
3239afc305bSSean Wang 		return err;
3249afc305bSSean Wang 
3259afc305bSSean Wang 	return 0;
3269afc305bSSean Wang }
3279afc305bSSean Wang 
3289afc305bSSean Wang int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
3299afc305bSSean Wang 			      const struct mtk_pin_desc *desc, bool pullup,
3309afc305bSSean Wang 			      int *res)
3319afc305bSSean Wang {
3329afc305bSSean Wang 	int err, v;
3339afc305bSSean Wang 
334*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
3359afc305bSSean Wang 	if (err)
3369afc305bSSean Wang 		return err;
3379afc305bSSean Wang 
3389afc305bSSean Wang 	if (v == MTK_DISABLE)
3399afc305bSSean Wang 		return -EINVAL;
3409afc305bSSean Wang 
341*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, &v);
3429afc305bSSean Wang 	if (err)
3439afc305bSSean Wang 		return err;
3449afc305bSSean Wang 
3459afc305bSSean Wang 	if (pullup ^ (v == MTK_PULLUP))
3469afc305bSSean Wang 		return -EINVAL;
3479afc305bSSean Wang 
3489afc305bSSean Wang 	*res = 1;
3499afc305bSSean Wang 
3509afc305bSSean Wang 	return 0;
3519afc305bSSean Wang }
3529afc305bSSean Wang 
353c2832197SSean Wang /* Revision 0 */
354c2832197SSean Wang int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
355c2832197SSean Wang 			  const struct mtk_pin_desc *desc, u32 arg)
356c2832197SSean Wang {
357c2832197SSean Wang 	const struct mtk_drive_desc *tb;
358c2832197SSean Wang 	int err = -ENOTSUPP;
359c2832197SSean Wang 
360c2832197SSean Wang 	tb = &mtk_drive[desc->drv_n];
361c2832197SSean Wang 	/* 4mA when (e8, e4) = (0, 0)
362c2832197SSean Wang 	 * 8mA when (e8, e4) = (0, 1)
363c2832197SSean Wang 	 * 12mA when (e8, e4) = (1, 0)
364c2832197SSean Wang 	 * 16mA when (e8, e4) = (1, 1)
365c2832197SSean Wang 	 */
366c2832197SSean Wang 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
367c2832197SSean Wang 		arg = (arg / tb->step - 1) * tb->scal;
368*ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E4,
369c2832197SSean Wang 				       arg & 0x1);
370c2832197SSean Wang 		if (err)
371c2832197SSean Wang 			return err;
372c2832197SSean Wang 
373*ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E8,
374c2832197SSean Wang 				       (arg & 0x2) >> 1);
375c2832197SSean Wang 		if (err)
376c2832197SSean Wang 			return err;
377c2832197SSean Wang 	}
378c2832197SSean Wang 
379c2832197SSean Wang 	return err;
380c2832197SSean Wang }
381c2832197SSean Wang 
382c2832197SSean Wang int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
383c2832197SSean Wang 			  const struct mtk_pin_desc *desc, int *val)
384c2832197SSean Wang {
385c2832197SSean Wang 	const struct mtk_drive_desc *tb;
386c2832197SSean Wang 	int err, val1, val2;
387c2832197SSean Wang 
388c2832197SSean Wang 	tb = &mtk_drive[desc->drv_n];
389c2832197SSean Wang 
390*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E4, &val1);
391c2832197SSean Wang 	if (err)
392c2832197SSean Wang 		return err;
393c2832197SSean Wang 
394*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E8, &val2);
395c2832197SSean Wang 	if (err)
396c2832197SSean Wang 		return err;
397c2832197SSean Wang 
398c2832197SSean Wang 	/* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1)
399c2832197SSean Wang 	 * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1)
400c2832197SSean Wang 	 */
401c2832197SSean Wang 	*val = (((val2 << 1) + val1) / tb->scal + 1) * tb->step;
402c2832197SSean Wang 
403c2832197SSean Wang 	return 0;
404c2832197SSean Wang }
4053ad38a14SSean Wang 
4063ad38a14SSean Wang /* Revision 1 */
4073ad38a14SSean Wang int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
4083ad38a14SSean Wang 			       const struct mtk_pin_desc *desc, u32 arg)
4093ad38a14SSean Wang {
4103ad38a14SSean Wang 	const struct mtk_drive_desc *tb;
4113ad38a14SSean Wang 	int err = -ENOTSUPP;
4123ad38a14SSean Wang 
4133ad38a14SSean Wang 	tb = &mtk_drive[desc->drv_n];
4143ad38a14SSean Wang 
4153ad38a14SSean Wang 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
4163ad38a14SSean Wang 		arg = (arg / tb->step - 1) * tb->scal;
4173ad38a14SSean Wang 
418*ea051eb3SSean Wang 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV,
4193ad38a14SSean Wang 				       arg);
4203ad38a14SSean Wang 		if (err)
4213ad38a14SSean Wang 			return err;
4223ad38a14SSean Wang 	}
4233ad38a14SSean Wang 
4243ad38a14SSean Wang 	return err;
4253ad38a14SSean Wang }
4263ad38a14SSean Wang 
4273ad38a14SSean Wang int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
4283ad38a14SSean Wang 			       const struct mtk_pin_desc *desc, int *val)
4293ad38a14SSean Wang {
4303ad38a14SSean Wang 	const struct mtk_drive_desc *tb;
4313ad38a14SSean Wang 	int err, val1;
4323ad38a14SSean Wang 
4333ad38a14SSean Wang 	tb = &mtk_drive[desc->drv_n];
4343ad38a14SSean Wang 
435*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, &val1);
4363ad38a14SSean Wang 	if (err)
4373ad38a14SSean Wang 		return err;
4383ad38a14SSean Wang 
4393ad38a14SSean Wang 	*val = ((val1 & 0x7) / tb->scal + 1) * tb->step;
4403ad38a14SSean Wang 
4413ad38a14SSean Wang 	return 0;
4423ad38a14SSean Wang }
4430d7ca772SSean Wang 
4440d7ca772SSean Wang int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
4450d7ca772SSean Wang 			     const struct mtk_pin_desc *desc, bool pullup,
4460d7ca772SSean Wang 			     u32 arg)
4470d7ca772SSean Wang {
4480d7ca772SSean Wang 	int err;
4490d7ca772SSean Wang 
4500d7ca772SSean Wang 	/* 10K off & 50K (75K) off, when (R0, R1) = (0, 0);
4510d7ca772SSean Wang 	 * 10K off & 50K (75K) on, when (R0, R1) = (0, 1);
4520d7ca772SSean Wang 	 * 10K on & 50K (75K) off, when (R0, R1) = (1, 0);
4530d7ca772SSean Wang 	 * 10K on & 50K (75K) on, when (R0, R1) = (1, 1)
4540d7ca772SSean Wang 	 */
455*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R0, arg & 1);
4560d7ca772SSean Wang 	if (err)
4570d7ca772SSean Wang 		return 0;
4580d7ca772SSean Wang 
459*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R1,
4600d7ca772SSean Wang 			       !!(arg & 2));
4610d7ca772SSean Wang 	if (err)
4620d7ca772SSean Wang 		return 0;
4630d7ca772SSean Wang 
4640d7ca772SSean Wang 	arg = pullup ? 0 : 1;
4650d7ca772SSean Wang 
466*ea051eb3SSean Wang 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PUPD, arg);
4670d7ca772SSean Wang 
4680d7ca772SSean Wang 	return err;
4690d7ca772SSean Wang }
4700d7ca772SSean Wang 
4710d7ca772SSean Wang int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
4720d7ca772SSean Wang 			     const struct mtk_pin_desc *desc, bool pullup,
4730d7ca772SSean Wang 			     u32 *val)
4740d7ca772SSean Wang {
4750d7ca772SSean Wang 	u32 t, t2;
4760d7ca772SSean Wang 	int err;
4770d7ca772SSean Wang 
478*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PUPD, &t);
4790d7ca772SSean Wang 	if (err)
4800d7ca772SSean Wang 		return err;
4810d7ca772SSean Wang 
4820d7ca772SSean Wang 	/* t == 0 supposes PULLUP for the customized PULL setup */
4830d7ca772SSean Wang 	if (pullup ^ !t)
4840d7ca772SSean Wang 		return -EINVAL;
4850d7ca772SSean Wang 
486*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R0, &t);
4870d7ca772SSean Wang 	if (err)
4880d7ca772SSean Wang 		return err;
4890d7ca772SSean Wang 
490*ea051eb3SSean Wang 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R1, &t2);
4910d7ca772SSean Wang 	if (err)
4920d7ca772SSean Wang 		return err;
4930d7ca772SSean Wang 
4940d7ca772SSean Wang 	*val = (t | t2 << 1) & 0x7;
4950d7ca772SSean Wang 
4960d7ca772SSean Wang 	return 0;
4970d7ca772SSean Wang }
498