1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018 MediaTek Inc.
4  *
5  * Author: Sean Wang <[email protected]>
6  *
7  */
8 
9 #include <dt-bindings/pinctrl/mt65xx.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/platform_device.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of_irq.h>
17 
18 #include "mtk-eint.h"
19 #include "pinctrl-mtk-common-v2.h"
20 
21 /**
22  * struct mtk_drive_desc - the structure that holds the information
23  *			    of the driving current
24  * @min:	the minimum current of this group
25  * @max:	the maximum current of this group
26  * @step:	the step current of this group
27  * @scal:	the weight factor
28  *
29  * formula: output = ((input) / step - 1) * scal
30  */
31 struct mtk_drive_desc {
32 	u8 min;
33 	u8 max;
34 	u8 step;
35 	u8 scal;
36 };
37 
38 /* The groups of drive strength */
39 static const struct mtk_drive_desc mtk_drive[] = {
40 	[DRV_GRP0] = { 4, 16, 4, 1 },
41 	[DRV_GRP1] = { 4, 16, 4, 2 },
42 	[DRV_GRP2] = { 2, 8, 2, 1 },
43 	[DRV_GRP3] = { 2, 8, 2, 2 },
44 	[DRV_GRP4] = { 2, 16, 2, 1 },
45 };
46 
mtk_w32(struct mtk_pinctrl * pctl,u8 i,u32 reg,u32 val)47 static void mtk_w32(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 val)
48 {
49 	writel_relaxed(val, pctl->base[i] + reg);
50 }
51 
mtk_r32(struct mtk_pinctrl * pctl,u8 i,u32 reg)52 static u32 mtk_r32(struct mtk_pinctrl *pctl, u8 i, u32 reg)
53 {
54 	return readl_relaxed(pctl->base[i] + reg);
55 }
56 
mtk_rmw(struct mtk_pinctrl * pctl,u8 i,u32 reg,u32 mask,u32 set)57 void mtk_rmw(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 mask, u32 set)
58 {
59 	u32 val;
60 	unsigned long flags;
61 
62 	spin_lock_irqsave(&pctl->lock, flags);
63 
64 	val = mtk_r32(pctl, i, reg);
65 	val &= ~mask;
66 	val |= set;
67 	mtk_w32(pctl, i, reg, val);
68 
69 	spin_unlock_irqrestore(&pctl->lock, flags);
70 }
71 
mtk_hw_pin_field_lookup(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int field,struct mtk_pin_field * pfd)72 static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw,
73 				   const struct mtk_pin_desc *desc,
74 				   int field, struct mtk_pin_field *pfd)
75 {
76 	const struct mtk_pin_field_calc *c;
77 	const struct mtk_pin_reg_calc *rc;
78 	int start = 0, end, check;
79 	bool found = false;
80 	u32 bits;
81 
82 	if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) {
83 		rc = &hw->soc->reg_cal[field];
84 	} else {
85 		dev_dbg(hw->dev,
86 			"Not support field %d for this soc\n", field);
87 		return -ENOTSUPP;
88 	}
89 
90 	end = rc->nranges - 1;
91 
92 	while (start <= end) {
93 		check = (start + end) >> 1;
94 		if (desc->number >= rc->range[check].s_pin
95 		 && desc->number <= rc->range[check].e_pin) {
96 			found = true;
97 			break;
98 		} else if (start == end)
99 			break;
100 		else if (desc->number < rc->range[check].s_pin)
101 			end = check - 1;
102 		else
103 			start = check + 1;
104 	}
105 
106 	if (!found) {
107 		dev_dbg(hw->dev, "Not support field %d for pin = %d (%s)\n",
108 			field, desc->number, desc->name);
109 		return -ENOTSUPP;
110 	}
111 
112 	c = rc->range + check;
113 
114 	if (c->i_base > hw->nbase - 1) {
115 		dev_err(hw->dev,
116 			"Invalid base for field %d for pin = %d (%s)\n",
117 			field, desc->number, desc->name);
118 		return -EINVAL;
119 	}
120 
121 	/* Calculated bits as the overall offset the pin is located at,
122 	 * if c->fixed is held, that determines the all the pins in the
123 	 * range use the same field with the s_pin.
124 	 */
125 	bits = c->fixed ? c->s_bit : c->s_bit +
126 	       (desc->number - c->s_pin) * (c->x_bits);
127 
128 	/* Fill pfd from bits. For example 32-bit register applied is assumed
129 	 * when c->sz_reg is equal to 32.
130 	 */
131 	pfd->index = c->i_base;
132 	pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg);
133 	pfd->bitpos = bits % c->sz_reg;
134 	pfd->mask = (1 << c->x_bits) - 1;
135 
136 	/* pfd->next is used for indicating that bit wrapping-around happens
137 	 * which requires the manipulation for bit 0 starting in the next
138 	 * register to form the complete field read/write.
139 	 */
140 	pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0;
141 
142 	return 0;
143 }
144 
mtk_hw_pin_field_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int field,struct mtk_pin_field * pfd)145 static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw,
146 				const struct mtk_pin_desc *desc,
147 				int field, struct mtk_pin_field *pfd)
148 {
149 	if (field < 0 || field >= PINCTRL_PIN_REG_MAX) {
150 		dev_err(hw->dev, "Invalid Field %d\n", field);
151 		return -EINVAL;
152 	}
153 
154 	return mtk_hw_pin_field_lookup(hw, desc, field, pfd);
155 }
156 
mtk_hw_bits_part(struct mtk_pin_field * pf,int * h,int * l)157 static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
158 {
159 	*l = 32 - pf->bitpos;
160 	*h = get_count_order(pf->mask) - *l;
161 }
162 
mtk_hw_write_cross_field(struct mtk_pinctrl * hw,struct mtk_pin_field * pf,int value)163 static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw,
164 				     struct mtk_pin_field *pf, int value)
165 {
166 	int nbits_l, nbits_h;
167 
168 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
169 
170 	mtk_rmw(hw, pf->index, pf->offset, pf->mask << pf->bitpos,
171 		(value & pf->mask) << pf->bitpos);
172 
173 	mtk_rmw(hw, pf->index, pf->offset + pf->next, BIT(nbits_h) - 1,
174 		(value & pf->mask) >> nbits_l);
175 }
176 
mtk_hw_read_cross_field(struct mtk_pinctrl * hw,struct mtk_pin_field * pf,int * value)177 static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw,
178 				    struct mtk_pin_field *pf, int *value)
179 {
180 	int nbits_l, nbits_h, h, l;
181 
182 	mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
183 
184 	l  = (mtk_r32(hw, pf->index, pf->offset)
185 	      >> pf->bitpos) & (BIT(nbits_l) - 1);
186 	h  = (mtk_r32(hw, pf->index, pf->offset + pf->next))
187 	      & (BIT(nbits_h) - 1);
188 
189 	*value = (h << nbits_l) | l;
190 }
191 
mtk_hw_set_value(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int field,int value)192 int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
193 		     int field, int value)
194 {
195 	struct mtk_pin_field pf;
196 	int err;
197 
198 	err = mtk_hw_pin_field_get(hw, desc, field, &pf);
199 	if (err)
200 		return err;
201 
202 	if (value < 0 || value > pf.mask)
203 		return -EINVAL;
204 
205 	if (!pf.next)
206 		mtk_rmw(hw, pf.index, pf.offset, pf.mask << pf.bitpos,
207 			(value & pf.mask) << pf.bitpos);
208 	else
209 		mtk_hw_write_cross_field(hw, &pf, value);
210 
211 	return 0;
212 }
213 EXPORT_SYMBOL_GPL(mtk_hw_set_value);
214 
mtk_hw_get_value(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int field,int * value)215 int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
216 		     int field, int *value)
217 {
218 	struct mtk_pin_field pf;
219 	int err;
220 
221 	err = mtk_hw_pin_field_get(hw, desc, field, &pf);
222 	if (err)
223 		return err;
224 
225 	if (!pf.next)
226 		*value = (mtk_r32(hw, pf.index, pf.offset)
227 			  >> pf.bitpos) & pf.mask;
228 	else
229 		mtk_hw_read_cross_field(hw, &pf, value);
230 
231 	return 0;
232 }
233 EXPORT_SYMBOL_GPL(mtk_hw_get_value);
234 
mtk_xt_find_eint_num(struct mtk_pinctrl * hw,unsigned long eint_n)235 static int mtk_xt_find_eint_num(struct mtk_pinctrl *hw, unsigned long eint_n)
236 {
237 	const struct mtk_pin_desc *desc;
238 	int i = 0;
239 
240 	desc = (const struct mtk_pin_desc *)hw->soc->pins;
241 
242 	while (i < hw->soc->npins) {
243 		if (desc[i].eint.eint_n == eint_n)
244 			return desc[i].number;
245 		i++;
246 	}
247 
248 	return EINT_NA;
249 }
250 
251 /*
252  * Virtual GPIO only used inside SOC and not being exported to outside SOC.
253  * Some modules use virtual GPIO as eint (e.g. pmif or usb).
254  * In MTK platform, external interrupt (EINT) and GPIO is 1-1 mapping
255  * and we can set GPIO as eint.
256  * But some modules use specific eint which doesn't have real GPIO pin.
257  * So we use virtual GPIO to map it.
258  */
259 
mtk_is_virt_gpio(struct mtk_pinctrl * hw,unsigned int gpio_n)260 bool mtk_is_virt_gpio(struct mtk_pinctrl *hw, unsigned int gpio_n)
261 {
262 	const struct mtk_pin_desc *desc;
263 	bool virt_gpio = false;
264 
265 	desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
266 
267 	/* if the GPIO is not supported for eint mode */
268 	if (desc->eint.eint_m == NO_EINT_SUPPORT)
269 		return virt_gpio;
270 
271 	if (desc->funcs && !desc->funcs[desc->eint.eint_m].name)
272 		virt_gpio = true;
273 
274 	return virt_gpio;
275 }
276 EXPORT_SYMBOL_GPL(mtk_is_virt_gpio);
277 
mtk_xt_get_gpio_n(void * data,unsigned long eint_n,unsigned int * gpio_n,struct gpio_chip ** gpio_chip)278 static int mtk_xt_get_gpio_n(void *data, unsigned long eint_n,
279 			     unsigned int *gpio_n,
280 			     struct gpio_chip **gpio_chip)
281 {
282 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
283 	const struct mtk_pin_desc *desc;
284 
285 	desc = (const struct mtk_pin_desc *)hw->soc->pins;
286 	*gpio_chip = &hw->chip;
287 
288 	/*
289 	 * Be greedy to guess first gpio_n is equal to eint_n.
290 	 * Only eint virtual eint number is greater than gpio number.
291 	 */
292 	if (hw->soc->npins > eint_n &&
293 	    desc[eint_n].eint.eint_n == eint_n)
294 		*gpio_n = eint_n;
295 	else
296 		*gpio_n = mtk_xt_find_eint_num(hw, eint_n);
297 
298 	return *gpio_n == EINT_NA ? -EINVAL : 0;
299 }
300 
mtk_xt_get_gpio_state(void * data,unsigned long eint_n)301 static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n)
302 {
303 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
304 	const struct mtk_pin_desc *desc;
305 	struct gpio_chip *gpio_chip;
306 	unsigned int gpio_n;
307 	int value, err;
308 
309 	err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
310 	if (err)
311 		return err;
312 
313 	desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
314 
315 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value);
316 	if (err)
317 		return err;
318 
319 	return !!value;
320 }
321 
mtk_xt_set_gpio_as_eint(void * data,unsigned long eint_n)322 static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n)
323 {
324 	struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
325 	const struct mtk_pin_desc *desc;
326 	struct gpio_chip *gpio_chip;
327 	unsigned int gpio_n;
328 	int err;
329 
330 	err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
331 	if (err)
332 		return err;
333 
334 	if (mtk_is_virt_gpio(hw, gpio_n))
335 		return 0;
336 
337 	desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
338 
339 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE,
340 			       desc->eint.eint_m);
341 	if (err)
342 		return err;
343 
344 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, MTK_INPUT);
345 	if (err)
346 		return err;
347 
348 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, MTK_ENABLE);
349 	/* SMT is supposed to be supported by every real GPIO and doesn't
350 	 * support virtual GPIOs, so the extra condition err != -ENOTSUPP
351 	 * is just for adding EINT support to these virtual GPIOs. It should
352 	 * add an extra flag in the pin descriptor when more pins with
353 	 * distinctive characteristic come out.
354 	 */
355 	if (err && err != -ENOTSUPP)
356 		return err;
357 
358 	return 0;
359 }
360 
361 static const struct mtk_eint_xt mtk_eint_xt = {
362 	.get_gpio_n = mtk_xt_get_gpio_n,
363 	.get_gpio_state = mtk_xt_get_gpio_state,
364 	.set_gpio_as_eint = mtk_xt_set_gpio_as_eint,
365 };
366 
mtk_build_eint(struct mtk_pinctrl * hw,struct platform_device * pdev)367 int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev)
368 {
369 	struct device_node *np = pdev->dev.of_node;
370 	int ret;
371 
372 	if (!IS_ENABLED(CONFIG_EINT_MTK))
373 		return 0;
374 
375 	if (!of_property_read_bool(np, "interrupt-controller"))
376 		return -ENODEV;
377 
378 	hw->eint = devm_kzalloc(hw->dev, sizeof(*hw->eint), GFP_KERNEL);
379 	if (!hw->eint)
380 		return -ENOMEM;
381 
382 	hw->eint->base = devm_platform_ioremap_resource_byname(pdev, "eint");
383 	if (IS_ERR(hw->eint->base)) {
384 		ret = PTR_ERR(hw->eint->base);
385 		goto err_free_eint;
386 	}
387 
388 	hw->eint->irq = irq_of_parse_and_map(np, 0);
389 	if (!hw->eint->irq) {
390 		ret = -EINVAL;
391 		goto err_free_eint;
392 	}
393 
394 	if (!hw->soc->eint_hw) {
395 		ret = -ENODEV;
396 		goto err_free_eint;
397 	}
398 
399 	hw->eint->dev = &pdev->dev;
400 	hw->eint->hw = hw->soc->eint_hw;
401 	hw->eint->pctl = hw;
402 	hw->eint->gpio_xlate = &mtk_eint_xt;
403 
404 	return mtk_eint_do_init(hw->eint);
405 
406 err_free_eint:
407 	devm_kfree(hw->dev, hw->eint);
408 	hw->eint = NULL;
409 	return ret;
410 }
411 EXPORT_SYMBOL_GPL(mtk_build_eint);
412 
413 /* Revision 0 */
mtk_pinconf_bias_disable_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc)414 int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
415 				 const struct mtk_pin_desc *desc)
416 {
417 	int err;
418 
419 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU,
420 			       MTK_DISABLE);
421 	if (err)
422 		return err;
423 
424 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
425 			       MTK_DISABLE);
426 	if (err)
427 		return err;
428 
429 	return 0;
430 }
431 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_disable_set);
432 
mtk_pinconf_bias_disable_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * res)433 int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
434 				 const struct mtk_pin_desc *desc, int *res)
435 {
436 	int v, v2;
437 	int err;
438 
439 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &v);
440 	if (err)
441 		return err;
442 
443 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &v2);
444 	if (err)
445 		return err;
446 
447 	if (v == MTK_ENABLE || v2 == MTK_ENABLE)
448 		return -EINVAL;
449 
450 	*res = 1;
451 
452 	return 0;
453 }
454 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_disable_get);
455 
mtk_pinconf_bias_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup)456 int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
457 			 const struct mtk_pin_desc *desc, bool pullup)
458 {
459 	int err, arg;
460 
461 	arg = pullup ? 1 : 2;
462 
463 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU, arg & 1);
464 	if (err)
465 		return err;
466 
467 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
468 			       !!(arg & 2));
469 	if (err)
470 		return err;
471 
472 	return 0;
473 }
474 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_set);
475 
mtk_pinconf_bias_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup,int * res)476 int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
477 			 const struct mtk_pin_desc *desc, bool pullup, int *res)
478 {
479 	int reg, err, v;
480 
481 	reg = pullup ? PINCTRL_PIN_REG_PU : PINCTRL_PIN_REG_PD;
482 
483 	err = mtk_hw_get_value(hw, desc, reg, &v);
484 	if (err)
485 		return err;
486 
487 	if (!v)
488 		return -EINVAL;
489 
490 	*res = 1;
491 
492 	return 0;
493 }
494 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_get);
495 
496 /* Revision 1 */
mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc)497 int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
498 				      const struct mtk_pin_desc *desc)
499 {
500 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
501 				MTK_DISABLE);
502 }
503 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_disable_set_rev1);
504 
mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * res)505 int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
506 				      const struct mtk_pin_desc *desc, int *res)
507 {
508 	int v, err;
509 
510 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
511 	if (err)
512 		return err;
513 
514 	if (v == MTK_ENABLE)
515 		return -EINVAL;
516 
517 	*res = 1;
518 
519 	return 0;
520 }
521 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_disable_get_rev1);
522 
mtk_pinconf_bias_set_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup)523 int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
524 			      const struct mtk_pin_desc *desc, bool pullup)
525 {
526 	int err, arg;
527 
528 	arg = pullup ? MTK_PULLUP : MTK_PULLDOWN;
529 
530 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
531 			       MTK_ENABLE);
532 	if (err)
533 		return err;
534 
535 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, arg);
536 	if (err)
537 		return err;
538 
539 	return 0;
540 }
541 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_set_rev1);
542 
mtk_pinconf_bias_get_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup,int * res)543 int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
544 			      const struct mtk_pin_desc *desc, bool pullup,
545 			      int *res)
546 {
547 	int err, v;
548 
549 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
550 	if (err)
551 		return err;
552 
553 	if (v == MTK_DISABLE)
554 		return -EINVAL;
555 
556 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, &v);
557 	if (err)
558 		return err;
559 
560 	if (pullup ^ (v == MTK_PULLUP))
561 		return -EINVAL;
562 
563 	*res = 1;
564 
565 	return 0;
566 }
567 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_get_rev1);
568 
569 /* Combo for the following pull register type:
570  * 1. PU + PD
571  * 2. PULLSEL + PULLEN
572  * 3. PUPD + R0 + R1
573  */
mtk_pinconf_bias_set_pu_pd(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg,bool pd_only)574 static int mtk_pinconf_bias_set_pu_pd(struct mtk_pinctrl *hw,
575 				const struct mtk_pin_desc *desc,
576 				u32 pullup, u32 arg, bool pd_only)
577 {
578 	int err, pu, pd;
579 
580 	if (arg == MTK_DISABLE) {
581 		pu = 0;
582 		pd = 0;
583 	} else if ((arg == MTK_ENABLE) && pullup) {
584 		pu = 1;
585 		pd = 0;
586 	} else if ((arg == MTK_ENABLE) && !pullup) {
587 		pu = 0;
588 		pd = 1;
589 	} else {
590 		return -EINVAL;
591 	}
592 
593 	if (!pd_only) {
594 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU, pu);
595 		if (err)
596 			return err;
597 	}
598 
599 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD, pd);
600 }
601 
mtk_pinconf_bias_set_pullsel_pullen(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)602 static int mtk_pinconf_bias_set_pullsel_pullen(struct mtk_pinctrl *hw,
603 				const struct mtk_pin_desc *desc,
604 				u32 pullup, u32 arg)
605 {
606 	int err, enable;
607 
608 	if (arg == MTK_DISABLE)
609 		enable = 0;
610 	else if (arg == MTK_ENABLE)
611 		enable = 1;
612 	else {
613 		err = -EINVAL;
614 		goto out;
615 	}
616 
617 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN, enable);
618 	if (err)
619 		goto out;
620 
621 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, pullup);
622 
623 out:
624 	return err;
625 }
626 
mtk_pinconf_bias_set_pupd_r1_r0(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)627 static int mtk_pinconf_bias_set_pupd_r1_r0(struct mtk_pinctrl *hw,
628 				const struct mtk_pin_desc *desc,
629 				u32 pullup, u32 arg)
630 {
631 	int err, r0, r1;
632 
633 	if ((arg == MTK_DISABLE) || (arg == MTK_PUPD_SET_R1R0_00)) {
634 		pullup = 0;
635 		r0 = 0;
636 		r1 = 0;
637 	} else if (arg == MTK_PUPD_SET_R1R0_01) {
638 		r0 = 1;
639 		r1 = 0;
640 	} else if (arg == MTK_PUPD_SET_R1R0_10) {
641 		r0 = 0;
642 		r1 = 1;
643 	} else if (arg == MTK_PUPD_SET_R1R0_11) {
644 		r0 = 1;
645 		r1 = 1;
646 	} else {
647 		err = -EINVAL;
648 		goto out;
649 	}
650 
651 	/* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
652 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PUPD, !pullup);
653 	if (err)
654 		goto out;
655 
656 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R0, r0);
657 	if (err)
658 		goto out;
659 
660 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R1, r1);
661 
662 out:
663 	return err;
664 }
665 
mtk_hw_pin_rsel_lookup(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg,u32 * rsel_val)666 static int mtk_hw_pin_rsel_lookup(struct mtk_pinctrl *hw,
667 				  const struct mtk_pin_desc *desc,
668 				  u32 pullup, u32 arg, u32 *rsel_val)
669 {
670 	const struct mtk_pin_rsel *rsel;
671 	int check;
672 	bool found = false;
673 
674 	rsel = hw->soc->pin_rsel;
675 
676 	for (check = 0; check <= hw->soc->npin_rsel - 1; check++) {
677 		if (desc->number >= rsel[check].s_pin &&
678 		    desc->number <= rsel[check].e_pin) {
679 			if (pullup) {
680 				if (rsel[check].up_rsel == arg) {
681 					found = true;
682 					*rsel_val = rsel[check].rsel_index;
683 					break;
684 				}
685 			} else {
686 				if (rsel[check].down_rsel == arg) {
687 					found = true;
688 					*rsel_val = rsel[check].rsel_index;
689 					break;
690 				}
691 			}
692 		}
693 	}
694 
695 	if (!found) {
696 		dev_err(hw->dev, "Not support rsel value %d Ohm for pin = %d (%s)\n",
697 			arg, desc->number, desc->name);
698 		return -ENOTSUPP;
699 	}
700 
701 	return 0;
702 }
703 
mtk_pinconf_bias_set_rsel(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)704 static int mtk_pinconf_bias_set_rsel(struct mtk_pinctrl *hw,
705 				     const struct mtk_pin_desc *desc,
706 				     u32 pullup, u32 arg)
707 {
708 	int err, rsel_val;
709 
710 	if (hw->rsel_si_unit) {
711 		/* find pin rsel_index from pin_rsel array*/
712 		err = mtk_hw_pin_rsel_lookup(hw, desc, pullup, arg, &rsel_val);
713 		if (err)
714 			return err;
715 	} else {
716 		if (arg < MTK_PULL_SET_RSEL_000 || arg > MTK_PULL_SET_RSEL_111)
717 			return -EINVAL;
718 
719 		rsel_val = arg - MTK_PULL_SET_RSEL_000;
720 	}
721 
722 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_RSEL, rsel_val);
723 }
724 
mtk_pinconf_bias_set_pu_pd_rsel(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)725 static int mtk_pinconf_bias_set_pu_pd_rsel(struct mtk_pinctrl *hw,
726 					   const struct mtk_pin_desc *desc,
727 					   u32 pullup, u32 arg)
728 {
729 	u32 enable = arg == MTK_DISABLE ? MTK_DISABLE : MTK_ENABLE;
730 	int err;
731 
732 	if (arg != MTK_DISABLE) {
733 		err = mtk_pinconf_bias_set_rsel(hw, desc, pullup, arg);
734 		if (err)
735 			return err;
736 	}
737 
738 	return mtk_pinconf_bias_set_pu_pd(hw, desc, pullup, enable, false);
739 }
740 
mtk_pinconf_bias_set_combo(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 arg)741 int mtk_pinconf_bias_set_combo(struct mtk_pinctrl *hw,
742 			       const struct mtk_pin_desc *desc,
743 			       u32 pullup, u32 arg)
744 {
745 	int err = -ENOTSUPP;
746 	u32 try_all_type;
747 
748 	if (hw->soc->pull_type)
749 		try_all_type = hw->soc->pull_type[desc->number];
750 	else
751 		try_all_type = MTK_PULL_TYPE_MASK;
752 
753 	if (try_all_type & MTK_PULL_RSEL_TYPE) {
754 		err = mtk_pinconf_bias_set_pu_pd_rsel(hw, desc, pullup, arg);
755 		if (!err)
756 			return 0;
757 	}
758 
759 	if (try_all_type & MTK_PULL_PD_TYPE) {
760 		err = mtk_pinconf_bias_set_pu_pd(hw, desc, pullup, arg, true);
761 		if (!err)
762 			return err;
763 	}
764 
765 	if (try_all_type & MTK_PULL_PU_PD_TYPE) {
766 		err = mtk_pinconf_bias_set_pu_pd(hw, desc, pullup, arg, false);
767 		if (!err)
768 			return 0;
769 	}
770 
771 	if (try_all_type & MTK_PULL_PULLSEL_TYPE) {
772 		err = mtk_pinconf_bias_set_pullsel_pullen(hw, desc,
773 							  pullup, arg);
774 		if (!err)
775 			return 0;
776 	}
777 
778 	if (try_all_type & MTK_PULL_PUPD_R1R0_TYPE)
779 		err = mtk_pinconf_bias_set_pupd_r1_r0(hw, desc, pullup, arg);
780 
781 	if (err)
782 		dev_err(hw->dev, "Invalid pull argument\n");
783 
784 	return err;
785 }
786 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_set_combo);
787 
mtk_rsel_get_si_unit(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 pullup,u32 rsel_val,u32 * si_unit)788 static int mtk_rsel_get_si_unit(struct mtk_pinctrl *hw,
789 				const struct mtk_pin_desc *desc,
790 				u32 pullup, u32 rsel_val, u32 *si_unit)
791 {
792 	const struct mtk_pin_rsel *rsel;
793 	int check;
794 
795 	rsel = hw->soc->pin_rsel;
796 
797 	for (check = 0; check <= hw->soc->npin_rsel - 1; check++) {
798 		if (desc->number >= rsel[check].s_pin &&
799 		    desc->number <= rsel[check].e_pin) {
800 			if (rsel_val == rsel[check].rsel_index) {
801 				if (pullup)
802 					*si_unit = rsel[check].up_rsel;
803 				else
804 					*si_unit = rsel[check].down_rsel;
805 				break;
806 			}
807 		}
808 	}
809 
810 	return 0;
811 }
812 
mtk_pinconf_bias_get_pu_pd_rsel(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)813 static int mtk_pinconf_bias_get_pu_pd_rsel(struct mtk_pinctrl *hw,
814 					   const struct mtk_pin_desc *desc,
815 					   u32 *pullup, u32 *enable)
816 {
817 	int pu, pd, rsel, err;
818 
819 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_RSEL, &rsel);
820 	if (err)
821 		goto out;
822 
823 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &pu);
824 	if (err)
825 		goto out;
826 
827 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &pd);
828 	if (err)
829 		goto out;
830 
831 	if (pu == 0 && pd == 0) {
832 		*pullup = 0;
833 		*enable = MTK_DISABLE;
834 	} else if (pu == 1 && pd == 0) {
835 		*pullup = 1;
836 		if (hw->rsel_si_unit)
837 			mtk_rsel_get_si_unit(hw, desc, *pullup, rsel, enable);
838 		else
839 			*enable = rsel + MTK_PULL_SET_RSEL_000;
840 	} else if (pu == 0 && pd == 1) {
841 		*pullup = 0;
842 		if (hw->rsel_si_unit)
843 			mtk_rsel_get_si_unit(hw, desc, *pullup, rsel, enable);
844 		else
845 			*enable = rsel + MTK_PULL_SET_RSEL_000;
846 	} else {
847 		err = -EINVAL;
848 		goto out;
849 	}
850 
851 out:
852 	return err;
853 }
854 
mtk_pinconf_bias_get_pu_pd(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)855 static int mtk_pinconf_bias_get_pu_pd(struct mtk_pinctrl *hw,
856 				const struct mtk_pin_desc *desc,
857 				u32 *pullup, u32 *enable)
858 {
859 	int err, pu, pd;
860 
861 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &pu);
862 	if (err)
863 		goto out;
864 
865 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &pd);
866 	if (err)
867 		goto out;
868 
869 	if (pu == 0 && pd == 0) {
870 		*pullup = 0;
871 		*enable = MTK_DISABLE;
872 	} else if (pu == 1 && pd == 0) {
873 		*pullup = 1;
874 		*enable = MTK_ENABLE;
875 	} else if (pu == 0 && pd == 1) {
876 		*pullup = 0;
877 		*enable = MTK_ENABLE;
878 	} else
879 		err = -EINVAL;
880 
881 out:
882 	return err;
883 }
884 
mtk_pinconf_bias_get_pd(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)885 static int mtk_pinconf_bias_get_pd(struct mtk_pinctrl *hw,
886 				const struct mtk_pin_desc *desc,
887 				u32 *pullup, u32 *enable)
888 {
889 	int err, pd;
890 
891 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &pd);
892 	if (err)
893 		goto out;
894 
895 	if (pd == 0) {
896 		*pullup = 0;
897 		*enable = MTK_DISABLE;
898 	} else if (pd == 1) {
899 		*pullup = 0;
900 		*enable = MTK_ENABLE;
901 	} else
902 		err = -EINVAL;
903 
904 out:
905 	return err;
906 }
907 
mtk_pinconf_bias_get_pullsel_pullen(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)908 static int mtk_pinconf_bias_get_pullsel_pullen(struct mtk_pinctrl *hw,
909 				const struct mtk_pin_desc *desc,
910 				u32 *pullup, u32 *enable)
911 {
912 	int err;
913 
914 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, pullup);
915 	if (err)
916 		goto out;
917 
918 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, enable);
919 
920 out:
921 	return err;
922 }
923 
mtk_pinconf_bias_get_pupd_r1_r0(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)924 static int mtk_pinconf_bias_get_pupd_r1_r0(struct mtk_pinctrl *hw,
925 				const struct mtk_pin_desc *desc,
926 				u32 *pullup, u32 *enable)
927 {
928 	int err, r0, r1;
929 
930 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PUPD, pullup);
931 	if (err)
932 		goto out;
933 	/* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
934 	*pullup = !(*pullup);
935 
936 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R0, &r0);
937 	if (err)
938 		goto out;
939 
940 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R1, &r1);
941 	if (err)
942 		goto out;
943 
944 	if ((r1 == 0) && (r0 == 0))
945 		*enable = MTK_PUPD_SET_R1R0_00;
946 	else if ((r1 == 0) && (r0 == 1))
947 		*enable = MTK_PUPD_SET_R1R0_01;
948 	else if ((r1 == 1) && (r0 == 0))
949 		*enable = MTK_PUPD_SET_R1R0_10;
950 	else if ((r1 == 1) && (r0 == 1))
951 		*enable = MTK_PUPD_SET_R1R0_11;
952 	else
953 		err = -EINVAL;
954 
955 out:
956 	return err;
957 }
958 
mtk_pinconf_bias_get_combo(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * pullup,u32 * enable)959 int mtk_pinconf_bias_get_combo(struct mtk_pinctrl *hw,
960 			      const struct mtk_pin_desc *desc,
961 			      u32 *pullup, u32 *enable)
962 {
963 	int err = -ENOTSUPP;
964 	u32 try_all_type;
965 
966 	if (hw->soc->pull_type)
967 		try_all_type = hw->soc->pull_type[desc->number];
968 	else
969 		try_all_type = MTK_PULL_TYPE_MASK;
970 
971 	if (try_all_type & MTK_PULL_RSEL_TYPE) {
972 		err = mtk_pinconf_bias_get_pu_pd_rsel(hw, desc, pullup, enable);
973 		if (!err)
974 			return 0;
975 	}
976 
977 	if (try_all_type & MTK_PULL_PD_TYPE) {
978 		err = mtk_pinconf_bias_get_pd(hw, desc, pullup, enable);
979 		if (!err)
980 			return err;
981 	}
982 
983 	if (try_all_type & MTK_PULL_PU_PD_TYPE) {
984 		err = mtk_pinconf_bias_get_pu_pd(hw, desc, pullup, enable);
985 		if (!err)
986 			return 0;
987 	}
988 
989 	if (try_all_type & MTK_PULL_PULLSEL_TYPE) {
990 		err = mtk_pinconf_bias_get_pullsel_pullen(hw, desc,
991 							  pullup, enable);
992 		if (!err)
993 			return 0;
994 	}
995 
996 	if (try_all_type & MTK_PULL_PUPD_R1R0_TYPE)
997 		err = mtk_pinconf_bias_get_pupd_r1_r0(hw, desc, pullup, enable);
998 
999 	return err;
1000 }
1001 EXPORT_SYMBOL_GPL(mtk_pinconf_bias_get_combo);
1002 
1003 /* Revision 0 */
mtk_pinconf_drive_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)1004 int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
1005 			  const struct mtk_pin_desc *desc, u32 arg)
1006 {
1007 	const struct mtk_drive_desc *tb;
1008 	int err = -ENOTSUPP;
1009 
1010 	tb = &mtk_drive[desc->drv_n];
1011 	/* 4mA when (e8, e4) = (0, 0)
1012 	 * 8mA when (e8, e4) = (0, 1)
1013 	 * 12mA when (e8, e4) = (1, 0)
1014 	 * 16mA when (e8, e4) = (1, 1)
1015 	 */
1016 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
1017 		arg = (arg / tb->step - 1) * tb->scal;
1018 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E4,
1019 				       arg & 0x1);
1020 		if (err)
1021 			return err;
1022 
1023 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E8,
1024 				       (arg & 0x2) >> 1);
1025 		if (err)
1026 			return err;
1027 	}
1028 
1029 	return err;
1030 }
1031 EXPORT_SYMBOL_GPL(mtk_pinconf_drive_set);
1032 
mtk_pinconf_drive_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * val)1033 int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
1034 			  const struct mtk_pin_desc *desc, int *val)
1035 {
1036 	const struct mtk_drive_desc *tb;
1037 	int err, val1, val2;
1038 
1039 	tb = &mtk_drive[desc->drv_n];
1040 
1041 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E4, &val1);
1042 	if (err)
1043 		return err;
1044 
1045 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E8, &val2);
1046 	if (err)
1047 		return err;
1048 
1049 	/* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1)
1050 	 * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1)
1051 	 */
1052 	*val = (((val2 << 1) + val1) / tb->scal + 1) * tb->step;
1053 
1054 	return 0;
1055 }
1056 EXPORT_SYMBOL_GPL(mtk_pinconf_drive_get);
1057 
1058 /* Revision 1 */
mtk_pinconf_drive_set_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)1059 int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
1060 			       const struct mtk_pin_desc *desc, u32 arg)
1061 {
1062 	const struct mtk_drive_desc *tb;
1063 	int err = -ENOTSUPP;
1064 
1065 	tb = &mtk_drive[desc->drv_n];
1066 
1067 	if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
1068 		arg = (arg / tb->step - 1) * tb->scal;
1069 
1070 		err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV,
1071 				       arg);
1072 		if (err)
1073 			return err;
1074 	}
1075 
1076 	return err;
1077 }
1078 EXPORT_SYMBOL_GPL(mtk_pinconf_drive_set_rev1);
1079 
mtk_pinconf_drive_get_rev1(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * val)1080 int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
1081 			       const struct mtk_pin_desc *desc, int *val)
1082 {
1083 	const struct mtk_drive_desc *tb;
1084 	int err, val1;
1085 
1086 	tb = &mtk_drive[desc->drv_n];
1087 
1088 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, &val1);
1089 	if (err)
1090 		return err;
1091 
1092 	*val = ((val1 & 0x7) / tb->scal + 1) * tb->step;
1093 
1094 	return 0;
1095 }
1096 EXPORT_SYMBOL_GPL(mtk_pinconf_drive_get_rev1);
1097 
mtk_pinconf_drive_set_raw(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)1098 int mtk_pinconf_drive_set_raw(struct mtk_pinctrl *hw,
1099 			       const struct mtk_pin_desc *desc, u32 arg)
1100 {
1101 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV, arg);
1102 }
1103 EXPORT_SYMBOL_GPL(mtk_pinconf_drive_set_raw);
1104 
mtk_pinconf_drive_get_raw(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,int * val)1105 int mtk_pinconf_drive_get_raw(struct mtk_pinctrl *hw,
1106 			       const struct mtk_pin_desc *desc, int *val)
1107 {
1108 	return mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, val);
1109 }
1110 EXPORT_SYMBOL_GPL(mtk_pinconf_drive_get_raw);
1111 
mtk_pinconf_adv_pull_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup,u32 arg)1112 int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
1113 			     const struct mtk_pin_desc *desc, bool pullup,
1114 			     u32 arg)
1115 {
1116 	int err;
1117 
1118 	/* 10K off & 50K (75K) off, when (R0, R1) = (0, 0);
1119 	 * 10K off & 50K (75K) on, when (R0, R1) = (0, 1);
1120 	 * 10K on & 50K (75K) off, when (R0, R1) = (1, 0);
1121 	 * 10K on & 50K (75K) on, when (R0, R1) = (1, 1)
1122 	 */
1123 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R0, arg & 1);
1124 	if (err)
1125 		return 0;
1126 
1127 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R1,
1128 			       !!(arg & 2));
1129 	if (err)
1130 		return 0;
1131 
1132 	arg = pullup ? 0 : 1;
1133 
1134 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PUPD, arg);
1135 
1136 	/* If PUPD register is not supported for that pin, let's fallback to
1137 	 * general bias control.
1138 	 */
1139 	if (err == -ENOTSUPP) {
1140 		if (hw->soc->bias_set) {
1141 			err = hw->soc->bias_set(hw, desc, pullup);
1142 			if (err)
1143 				return err;
1144 		} else {
1145 			err = mtk_pinconf_bias_set_rev1(hw, desc, pullup);
1146 			if (err)
1147 				err = mtk_pinconf_bias_set(hw, desc, pullup);
1148 		}
1149 	}
1150 
1151 	return err;
1152 }
1153 EXPORT_SYMBOL_GPL(mtk_pinconf_adv_pull_set);
1154 
mtk_pinconf_adv_pull_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,bool pullup,u32 * val)1155 int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
1156 			     const struct mtk_pin_desc *desc, bool pullup,
1157 			     u32 *val)
1158 {
1159 	u32 t, t2;
1160 	int err;
1161 
1162 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PUPD, &t);
1163 
1164 	/* If PUPD register is not supported for that pin, let's fallback to
1165 	 * general bias control.
1166 	 */
1167 	if (err == -ENOTSUPP) {
1168 		if (hw->soc->bias_get) {
1169 			err = hw->soc->bias_get(hw, desc, pullup, val);
1170 			if (err)
1171 				return err;
1172 		} else {
1173 			return -ENOTSUPP;
1174 		}
1175 	} else {
1176 		/* t == 0 supposes PULLUP for the customized PULL setup */
1177 		if (err)
1178 			return err;
1179 
1180 		if (pullup ^ !t)
1181 			return -EINVAL;
1182 	}
1183 
1184 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R0, &t);
1185 	if (err)
1186 		return err;
1187 
1188 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R1, &t2);
1189 	if (err)
1190 		return err;
1191 
1192 	*val = (t | t2 << 1) & 0x7;
1193 
1194 	return 0;
1195 }
1196 EXPORT_SYMBOL_GPL(mtk_pinconf_adv_pull_get);
1197 
mtk_pinconf_adv_drive_set(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)1198 int mtk_pinconf_adv_drive_set(struct mtk_pinctrl *hw,
1199 			      const struct mtk_pin_desc *desc, u32 arg)
1200 {
1201 	int err;
1202 	int en = arg & 1;
1203 	int e0 = !!(arg & 2);
1204 	int e1 = !!(arg & 4);
1205 
1206 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_EN, en);
1207 	if (err)
1208 		return err;
1209 
1210 	if (!en)
1211 		return err;
1212 
1213 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_E0, e0);
1214 	if (err)
1215 		return err;
1216 
1217 	err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_E1, e1);
1218 	if (err)
1219 		return err;
1220 
1221 	return err;
1222 }
1223 EXPORT_SYMBOL_GPL(mtk_pinconf_adv_drive_set);
1224 
mtk_pinconf_adv_drive_get(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * val)1225 int mtk_pinconf_adv_drive_get(struct mtk_pinctrl *hw,
1226 			      const struct mtk_pin_desc *desc, u32 *val)
1227 {
1228 	u32 en, e0, e1;
1229 	int err;
1230 
1231 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_EN, &en);
1232 	if (err)
1233 		return err;
1234 
1235 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_E0, &e0);
1236 	if (err)
1237 		return err;
1238 
1239 	err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_E1, &e1);
1240 	if (err)
1241 		return err;
1242 
1243 	*val = (en | e0 << 1 | e1 << 2) & 0x7;
1244 
1245 	return 0;
1246 }
1247 EXPORT_SYMBOL_GPL(mtk_pinconf_adv_drive_get);
1248 
mtk_pinconf_adv_drive_set_raw(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 arg)1249 int mtk_pinconf_adv_drive_set_raw(struct mtk_pinctrl *hw,
1250 				  const struct mtk_pin_desc *desc, u32 arg)
1251 {
1252 	return mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV_ADV, arg);
1253 }
1254 EXPORT_SYMBOL_GPL(mtk_pinconf_adv_drive_set_raw);
1255 
mtk_pinconf_adv_drive_get_raw(struct mtk_pinctrl * hw,const struct mtk_pin_desc * desc,u32 * val)1256 int mtk_pinconf_adv_drive_get_raw(struct mtk_pinctrl *hw,
1257 				  const struct mtk_pin_desc *desc, u32 *val)
1258 {
1259 	return mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV_ADV, val);
1260 }
1261 EXPORT_SYMBOL_GPL(mtk_pinconf_adv_drive_get_raw);
1262 
1263 MODULE_LICENSE("GPL v2");
1264 MODULE_AUTHOR("Sean Wang <[email protected]>");
1265 MODULE_DESCRIPTION("Pin configuration library module for mediatek SoCs");
1266