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