blob: 49a66ef8caaae7ab44891025ca85e4643ff071ca [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +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 <linux/device.h>
10#include <linux/err.h>
11#include <linux/gpio/driver.h>
12#include <linux/platform_device.h>
13#include <linux/io.h>
14#include <linux/of_irq.h>
15
16#include <dt-bindings/pinctrl/mt65xx.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 */
31struct mtk_drive_desc {
32 u8 min;
33 u8 max;
34 u8 step;
35 u8 scal;
36};
37
38/* The groups of drive strength */
39static 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
47static void mtk_w32(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 val)
48{
49 writel_relaxed(val, pctl->base[i] + reg);
50}
51
52static u32 mtk_r32(struct mtk_pinctrl *pctl, u8 i, u32 reg)
53{
54 return readl_relaxed(pctl->base[i] + reg);
55}
56
57void mtk_rmw(struct mtk_pinctrl *pctl, u8 i, u32 reg, u32 mask, u32 set)
58{
59 u32 val;
60
61 val = mtk_r32(pctl, i, reg);
62 val &= ~mask;
63 val |= set;
64 mtk_w32(pctl, i, reg, val);
65}
66
67static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw,
68 const struct mtk_pin_desc *desc,
69 int field, struct mtk_pin_field *pfd)
70{
71 const struct mtk_pin_field_calc *c, *e;
72 const struct mtk_pin_reg_calc *rc;
73 u32 bits;
74
75 if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) {
76 rc = &hw->soc->reg_cal[field];
77 } else {
78 dev_dbg(hw->dev,
79 "Not support field %d for pin %d (%s)\n",
80 field, desc->number, desc->name);
81 return -ENOTSUPP;
82 }
83
84 c = rc->range;
85 e = c + rc->nranges;
86
87 while (c < e) {
88 if (desc->number >= c->s_pin && desc->number <= c->e_pin)
89 break;
90 c++;
91 }
92
93 if (c >= e) {
94 dev_dbg(hw->dev, "Not support field %d for pin = %d (%s)\n",
95 field, desc->number, desc->name);
96 return -ENOTSUPP;
97 }
98
99 if (c->i_base > hw->nbase - 1) {
100 dev_err(hw->dev,
101 "Invalid base for field %d for pin = %d (%s)\n",
102 field, desc->number, desc->name);
103 return -EINVAL;
104 }
105
106 /* Calculated bits as the overall offset the pin is located at,
107 * if c->fixed is held, that determines the all the pins in the
108 * range use the same field with the s_pin.
109 */
110 bits = c->fixed ? c->s_bit : c->s_bit +
111 (desc->number - c->s_pin) * (c->x_bits);
112
113 /* Fill pfd from bits. For example 32-bit register applied is assumed
114 * when c->sz_reg is equal to 32.
115 */
116 pfd->index = c->i_base;
117 pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg);
118 pfd->bitpos = bits % c->sz_reg;
119 pfd->mask = (1 << c->x_bits) - 1;
120
121 /* pfd->next is used for indicating that bit wrapping-around happens
122 * which requires the manipulation for bit 0 starting in the next
123 * register to form the complete field read/write.
124 */
125 pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0;
126
127 return 0;
128}
129
130static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw,
131 const struct mtk_pin_desc *desc,
132 int field, struct mtk_pin_field *pfd)
133{
134 if (field < 0 || field >= PINCTRL_PIN_REG_MAX) {
135 dev_err(hw->dev, "Invalid Field %d\n", field);
136 return -EINVAL;
137 }
138
139 return mtk_hw_pin_field_lookup(hw, desc, field, pfd);
140}
141
142static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
143{
144 *l = 32 - pf->bitpos;
145 *h = get_count_order(pf->mask) - *l;
146}
147
148static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw,
149 struct mtk_pin_field *pf, int value)
150{
151 int nbits_l, nbits_h;
152
153 mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
154
155 mtk_rmw(hw, pf->index, pf->offset, pf->mask << pf->bitpos,
156 (value & pf->mask) << pf->bitpos);
157
158 mtk_rmw(hw, pf->index, pf->offset + pf->next, BIT(nbits_h) - 1,
159 (value & pf->mask) >> nbits_l);
160}
161
162static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw,
163 struct mtk_pin_field *pf, int *value)
164{
165 int nbits_l, nbits_h, h, l;
166
167 mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
168
169 l = (mtk_r32(hw, pf->index, pf->offset)
170 >> pf->bitpos) & (BIT(nbits_l) - 1);
171 h = (mtk_r32(hw, pf->index, pf->offset + pf->next))
172 & (BIT(nbits_h) - 1);
173
174 *value = (h << nbits_l) | l;
175}
176
177int mtk_hw_set_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
178 int field, int value)
179{
180 struct mtk_pin_field pf;
181 int err;
182
183 err = mtk_hw_pin_field_get(hw, desc, field, &pf);
184 if (err)
185 return err;
186
187 if (!pf.next)
188 mtk_rmw(hw, pf.index, pf.offset, pf.mask << pf.bitpos,
189 (value & pf.mask) << pf.bitpos);
190 else
191 mtk_hw_write_cross_field(hw, &pf, value);
192
193 return 0;
194}
195
196void mtk_hw_set_value_no_lookup(struct mtk_pinctrl *hw,
197 const struct mtk_pin_desc *desc,
198 int value, struct mtk_pin_field *pf)
199{
200 if (value < 0 || value > pf->mask)
201 return;
202
203 if (!pf->next)
204 mtk_rmw(hw, pf->index, pf->offset, pf->mask << pf->bitpos,
205 (value & pf->mask) << pf->bitpos);
206 else
207 mtk_hw_write_cross_field(hw, pf, value);
208}
209
210int mtk_hw_get_value(struct mtk_pinctrl *hw, const struct mtk_pin_desc *desc,
211 int field, int *value)
212{
213 struct mtk_pin_field pf;
214 int err;
215
216 err = mtk_hw_pin_field_get(hw, desc, field, &pf);
217 if (err)
218 return err;
219
220 if (!pf.next)
221 *value = (mtk_r32(hw, pf.index, pf.offset)
222 >> pf.bitpos) & pf.mask;
223 else
224 mtk_hw_read_cross_field(hw, &pf, value);
225
226 return 0;
227}
228
229void mtk_hw_get_value_no_lookup(struct mtk_pinctrl *hw,
230 const struct mtk_pin_desc *desc,
231 int *value, struct mtk_pin_field *pf)
232{
233 if (!pf->next)
234 *value = (mtk_r32(hw, pf->index, pf->offset)
235 >> pf->bitpos) & pf->mask;
236 else
237 mtk_hw_read_cross_field(hw, pf, value);
238}
239
240static int mtk_xt_find_eint_num(struct mtk_pinctrl *hw, unsigned long eint_n,
241 unsigned int *virt_gpio)
242{
243 const struct mtk_pin_desc *desc;
244 int i = 0;
245
246 desc = (const struct mtk_pin_desc *)hw->soc->pins;
247
248 while (i < hw->soc->npins) {
249 if (desc[i].eint.eint_n == eint_n) {
250 if (desc[i].funcs[desc[i].eint.eint_m].name == 0)
251 *virt_gpio = 1;
252 return desc[i].number;
253 }
254 i++;
255 }
256
257 return EINT_NA;
258}
259
260static int mtk_xt_get_gpio_n(void *data, unsigned long eint_n,
261 unsigned int *gpio_n,
262 struct gpio_chip **gpio_chip)
263{
264 struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
265 const struct mtk_pin_desc *desc;
266 unsigned int virt_gpio = 0;
267
268 desc = (const struct mtk_pin_desc *)hw->soc->pins;
269 *gpio_chip = &hw->chip;
270
271 /* Be greedy to guess first gpio_n is equal to eint_n */
272 if (desc[eint_n].eint.eint_n == eint_n)
273 *gpio_n = eint_n;
274 else
275 *gpio_n = mtk_xt_find_eint_num(hw, eint_n, &virt_gpio);
276
277 if (virt_gpio)
278 return EINT_NO_GPIO;
279
280 return *gpio_n == EINT_NA ? -EINVAL : 0;
281}
282
283static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n)
284{
285 struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
286 const struct mtk_pin_desc *desc;
287 struct gpio_chip *gpio_chip;
288 unsigned int gpio_n;
289 int value, err;
290
291 err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
292 if (err)
293 return err;
294
295 desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
296
297 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DI, &value);
298 if (err)
299 return err;
300
301 return !!value;
302}
303
304static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n)
305{
306 struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
307 const struct mtk_pin_desc *desc;
308 struct gpio_chip *gpio_chip;
309 unsigned int gpio_n;
310 int err;
311
312 err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
313 if (err)
314 return err;
315
316 if (err == EINT_NO_GPIO)
317 return 0;
318
319 desc = (const struct mtk_pin_desc *)&hw->soc->pins[gpio_n];
320
321 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_MODE,
322 desc->eint.eint_m);
323 if (err)
324 return err;
325
326 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DIR, MTK_INPUT);
327 if (err)
328 return err;
329
330 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_SMT, MTK_ENABLE);
331 /* SMT is supposed to be supported by every real GPIO and doesn't
332 * support virtual GPIOs, so the extra condition err != -ENOTSUPP
333 * is just for adding EINT support to these virtual GPIOs. It should
334 * add an extra flag in the pin descriptor when more pins with
335 * distinctive characteristic come out.
336 */
337 if (err && err != -ENOTSUPP)
338 return err;
339
340 return 0;
341}
342
343static const struct mtk_eint_xt mtk_eint_xt = {
344 .get_gpio_n = mtk_xt_get_gpio_n,
345 .get_gpio_state = mtk_xt_get_gpio_state,
346 .set_gpio_as_eint = mtk_xt_set_gpio_as_eint,
347};
348
349int mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev)
350{
351 struct device_node *np = pdev->dev.of_node;
352 struct resource *res;
353
354 if (!IS_ENABLED(CONFIG_EINT_MTK))
355 return 0;
356
357 if (!of_property_read_bool(np, "interrupt-controller"))
358 return -ENODEV;
359
360 hw->eint = devm_kzalloc(hw->dev, sizeof(*hw->eint), GFP_KERNEL);
361 if (!hw->eint)
362 return -ENOMEM;
363
364 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eint");
365 if (!res) {
366 dev_err(&pdev->dev, "Unable to get eint resource\n");
367 return -ENODEV;
368 }
369
370 hw->eint->base = devm_ioremap_resource(&pdev->dev, res);
371 if (IS_ERR(hw->eint->base))
372 return PTR_ERR(hw->eint->base);
373
374 hw->eint->irq = irq_of_parse_and_map(np, 0);
375 if (!hw->eint->irq)
376 return -EINVAL;
377
378 if (!hw->soc->eint_hw)
379 return -ENODEV;
380
381 hw->eint->dev = &pdev->dev;
382 hw->eint->hw = hw->soc->eint_hw;
383 hw->eint->pctl = hw;
384 hw->eint->gpio_xlate = &mtk_eint_xt;
385
386 return mtk_eint_do_init(hw->eint);
387}
388
389/* Revision 0 */
390int mtk_pinconf_bias_disable_set(struct mtk_pinctrl *hw,
391 const struct mtk_pin_desc *desc)
392{
393 int err;
394
395 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU,
396 MTK_DISABLE);
397 if (err)
398 return err;
399
400 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
401 MTK_DISABLE);
402 if (err)
403 return err;
404
405 return 0;
406}
407
408int mtk_pinconf_bias_disable_get(struct mtk_pinctrl *hw,
409 const struct mtk_pin_desc *desc, int *res)
410{
411 int v, v2;
412 int err;
413
414 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PU, &v);
415 if (err)
416 return err;
417
418 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PD, &v2);
419 if (err)
420 return err;
421
422 if (v == MTK_ENABLE || v2 == MTK_ENABLE)
423 return -EINVAL;
424
425 *res = 1;
426
427 return 0;
428}
429
430int mtk_pinconf_bias_set(struct mtk_pinctrl *hw,
431 const struct mtk_pin_desc *desc, bool pullup)
432{
433 int err, arg;
434
435 arg = pullup ? 1 : 2;
436
437 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PU, arg & 1);
438 if (err)
439 return err;
440
441 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PD,
442 !!(arg & 2));
443 if (err)
444 return err;
445
446 return 0;
447}
448
449int mtk_pinconf_bias_get(struct mtk_pinctrl *hw,
450 const struct mtk_pin_desc *desc, bool pullup, int *res)
451{
452 int reg, err, v;
453
454 reg = pullup ? PINCTRL_PIN_REG_PU : PINCTRL_PIN_REG_PD;
455
456 err = mtk_hw_get_value(hw, desc, reg, &v);
457 if (err)
458 return err;
459
460 if (!v)
461 return -EINVAL;
462
463 *res = 1;
464
465 return 0;
466}
467
468/* Revision 1 */
469int mtk_pinconf_bias_disable_set_rev1(struct mtk_pinctrl *hw,
470 const struct mtk_pin_desc *desc)
471{
472 int err;
473
474 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
475 MTK_DISABLE);
476 if (err)
477 return err;
478
479 return 0;
480}
481
482int mtk_pinconf_bias_disable_get_rev1(struct mtk_pinctrl *hw,
483 const struct mtk_pin_desc *desc, int *res)
484{
485 int v, err;
486
487 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
488 if (err)
489 return err;
490
491 if (v == MTK_ENABLE)
492 return -EINVAL;
493
494 *res = 1;
495
496 return 0;
497}
498
499int mtk_pinconf_bias_get_rev1(struct mtk_pinctrl *hw,
500 const struct mtk_pin_desc *desc, bool pullup,
501 int *res)
502{
503 int err, v;
504
505 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLEN, &v);
506 if (err)
507 return err;
508
509 if (v == MTK_DISABLE)
510 return -EINVAL;
511
512 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, &v);
513 if (err)
514 return err;
515
516 if (pullup ^ (v == MTK_PULLUP))
517 return -EINVAL;
518
519 *res = 1;
520
521 return 0;
522}
523
524/* Combo for the following pull register type:
525 * 1. PU + PD
526 * 2. PULLSEL + PULLEN
527 * 3. PUPD + R0 + R1
528 */
529int mtk_pinconf_bias_set_pu_pd(struct mtk_pinctrl *hw,
530 const struct mtk_pin_desc *desc,
531 u32 pullup, u32 arg)
532{
533 struct mtk_pin_field pf;
534 int err = -EINVAL;
535 int pu, pd;
536
537 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PU, &pf);
538 if (err)
539 goto out;
540
541 if (arg == MTK_DISABLE) {
542 pu = 0;
543 pd = 0;
544 } else if ((arg == MTK_ENABLE) && pullup) {
545 pu = 1;
546 pd = 0;
547 } else if ((arg == MTK_ENABLE) && !pullup) {
548 pu = 0;
549 pd = 1;
550 } else {
551 goto out;
552 }
553
554 mtk_hw_set_value_no_lookup(hw, desc, pu, &pf);
555
556 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PD, &pf);
557 if (err)
558 goto out;
559
560 mtk_hw_set_value_no_lookup(hw, desc, pd, &pf);
561
562out:
563 return err;
564}
565
566int mtk_pinconf_bias_set_pullsel_pullen(struct mtk_pinctrl *hw,
567 const struct mtk_pin_desc *desc,
568 u32 pullup, u32 arg)
569{
570 struct mtk_pin_field pf;
571 int err = -EINVAL, enable;
572
573 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PULLEN, &pf);
574 if (err)
575 goto out;
576
577 if (arg == MTK_DISABLE)
578 enable = 0;
579 else if (arg == MTK_ENABLE)
580 enable = 1;
581 else
582 goto out;
583
584 mtk_hw_set_value_no_lookup(hw, desc, enable, &pf);
585
586 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PULLSEL, &pf);
587 if (err)
588 goto out;
589 mtk_hw_set_value_no_lookup(hw, desc, pullup, &pf);
590
591out:
592 return err;
593}
594
595int mtk_pinconf_bias_set_pupd_r1_r0(struct mtk_pinctrl *hw,
596 const struct mtk_pin_desc *desc,
597 u32 pullup, u32 arg)
598{
599 struct mtk_pin_field pf;
600 int err = -EINVAL;
601 int r0, r1;
602
603 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PUPD, &pf);
604 if (err)
605 goto out;
606
607 if ((arg == MTK_DISABLE) || (arg == MTK_PUPD_SET_R1R0_00)) {
608 pullup = 0;
609 r0 = 0;
610 r1 = 0;
611 } else if (arg == MTK_PUPD_SET_R1R0_01) {
612 r0 = 1;
613 r1 = 0;
614 } else if (arg == MTK_PUPD_SET_R1R0_10) {
615 r0 = 0;
616 r1 = 1;
617 } else if (arg == MTK_PUPD_SET_R1R0_11) {
618 r0 = 1;
619 r1 = 1;
620 } else
621 goto out;
622
623 /* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
624 mtk_hw_set_value_no_lookup(hw, desc, !pullup, &pf);
625
626 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_R0, &pf);
627 if (err)
628 goto out;
629 mtk_hw_set_value_no_lookup(hw, desc, r0, &pf);
630
631 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_R1, &pf);
632 if (err)
633 goto out;
634 mtk_hw_set_value_no_lookup(hw, desc, r1, &pf);
635
636out:
637 return err;
638}
639
640int mtk_pinconf_bias_get_pu_pd(struct mtk_pinctrl *hw,
641 const struct mtk_pin_desc *desc,
642 u32 *pullup, u32 *enable)
643{
644 struct mtk_pin_field pf;
645 int err = -EINVAL;
646 int pu, pd;
647
648 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PU, &pf);
649 if (err)
650 goto out;
651
652 mtk_hw_get_value_no_lookup(hw, desc, &pu, &pf);
653
654 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PD, &pf);
655 if (err)
656 goto out;
657
658 mtk_hw_get_value_no_lookup(hw, desc, &pd, &pf);
659
660 if (pu == 0 && pd == 0) {
661 *pullup = 0;
662 *enable = MTK_DISABLE;
663 } else if (pu == 1 && pd == 0) {
664 *pullup = 1;
665 *enable = MTK_ENABLE;
666 } else if (pu == 0 && pd == 1) {
667 *pullup = 0;
668 *enable = MTK_ENABLE;
669 } else {
670 err = -EINVAL;
671 goto out;
672 }
673
674out:
675 return err;
676}
677
678int mtk_pinconf_bias_get_pullsel_pullen(struct mtk_pinctrl *hw,
679 const struct mtk_pin_desc *desc,
680 u32 *pullup, u32 *enable)
681{
682 struct mtk_pin_field pf;
683 int err = -EINVAL;
684
685 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PULLSEL, &pf);
686 if (err)
687 goto out;
688
689 mtk_hw_get_value_no_lookup(hw, desc, pullup, &pf);
690
691 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PULLEN, &pf);
692 if (err)
693 goto out;
694
695 mtk_hw_get_value_no_lookup(hw, desc, enable, &pf);
696
697out:
698 return err;
699}
700
701int mtk_pinconf_bias_get_pupd_r1_r0(struct mtk_pinctrl *hw,
702 const struct mtk_pin_desc *desc,
703 u32 *pullup, u32 *enable)
704{
705 struct mtk_pin_field pf;
706 int err = -EINVAL;
707 int r0, r1;
708
709 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_PUPD, &pf);
710 if (err)
711 goto out;
712
713 /* MTK HW PUPD bit: 1 for pull-down, 0 for pull-up */
714 mtk_hw_get_value_no_lookup(hw, desc, pullup, &pf);
715 *pullup = !(*pullup);
716
717 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_R0, &pf);
718 if (err)
719 goto out;
720 mtk_hw_get_value_no_lookup(hw, desc, &r0, &pf);
721
722 err = mtk_hw_pin_field_lookup(hw, desc, PINCTRL_PIN_REG_R1, &pf);
723 if (err)
724 goto out;
725 mtk_hw_get_value_no_lookup(hw, desc, &r1, &pf);
726
727 if ((r1 == 0) && (r0 == 0))
728 *enable = MTK_PUPD_SET_R1R0_00;
729 else if ((r1 == 0) && (r0 == 1))
730 *enable = MTK_PUPD_SET_R1R0_01;
731 else if ((r1 == 1) && (r0 == 0))
732 *enable = MTK_PUPD_SET_R1R0_10;
733 else if ((r1 == 1) && (r0 == 1))
734 *enable = MTK_PUPD_SET_R1R0_11;
735 else
736 goto out;
737
738out:
739 return err;
740}
741
742int mtk_pinconf_bias_set_combo(struct mtk_pinctrl *hw,
743 const struct mtk_pin_desc *desc,
744 u32 pullup, u32 arg)
745{
746 int err;
747
748 err = mtk_pinconf_bias_set_pu_pd(hw, desc, pullup, arg);
749 if (!err)
750 goto out;
751
752 err = mtk_pinconf_bias_set_pullsel_pullen(hw, desc, pullup, arg);
753 if (!err)
754 goto out;
755
756 err = mtk_pinconf_bias_set_pupd_r1_r0(hw, desc, pullup, arg);
757
758out:
759 return err;
760}
761
762int mtk_pinconf_bias_get_combo(struct mtk_pinctrl *hw,
763 const struct mtk_pin_desc *desc,
764 u32 *pullup, u32 *enable)
765{
766 int err;
767
768 err = mtk_pinconf_bias_get_pu_pd(hw, desc, pullup, enable);
769 if (!err)
770 goto out;
771
772 err = mtk_pinconf_bias_get_pullsel_pullen(hw, desc, pullup, enable);
773 if (!err)
774 goto out;
775
776 err = mtk_pinconf_bias_get_pupd_r1_r0(hw, desc, pullup, enable);
777
778out:
779 return err;
780}
781
782/* Revision 0 */
783int mtk_pinconf_drive_set(struct mtk_pinctrl *hw,
784 const struct mtk_pin_desc *desc, u32 arg)
785{
786 const struct mtk_drive_desc *tb;
787 int err = -ENOTSUPP;
788
789 tb = &mtk_drive[desc->drv_n];
790 /* 4mA when (e8, e4) = (0, 0)
791 * 8mA when (e8, e4) = (0, 1)
792 * 12mA when (e8, e4) = (1, 0)
793 * 16mA when (e8, e4) = (1, 1)
794 */
795 if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
796 arg = (arg / tb->step - 1) * tb->scal;
797 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E4,
798 arg & 0x1);
799 if (err)
800 return err;
801
802 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_E8,
803 (arg & 0x2) >> 1);
804 if (err)
805 return err;
806 }
807
808 return err;
809}
810
811int mtk_pinconf_bias_set_rev1(struct mtk_pinctrl *hw,
812 const struct mtk_pin_desc *desc, bool pullup)
813{
814 int err, arg;
815
816 arg = pullup ? MTK_PULLUP : MTK_PULLDOWN;
817
818 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLEN,
819 MTK_ENABLE);
820 if (err)
821 return err;
822
823 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PULLSEL, arg);
824 if (err)
825 return err;
826
827 return 0;
828}
829
830int mtk_pinconf_drive_get(struct mtk_pinctrl *hw,
831 const struct mtk_pin_desc *desc, int *val)
832{
833 const struct mtk_drive_desc *tb;
834 int err, val1, val2;
835
836 tb = &mtk_drive[desc->drv_n];
837
838 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E4, &val1);
839 if (err)
840 return err;
841
842 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_E8, &val2);
843 if (err)
844 return err;
845
846 /* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1)
847 * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1)
848 */
849 *val = (((val2 << 1) + val1) / tb->scal + 1) * tb->step;
850
851 return 0;
852}
853
854/* Revision 1 */
855int mtk_pinconf_drive_set_rev1(struct mtk_pinctrl *hw,
856 const struct mtk_pin_desc *desc, u32 arg)
857{
858 const struct mtk_drive_desc *tb;
859 int err = -ENOTSUPP;
860
861 tb = &mtk_drive[desc->drv_n];
862
863 if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
864 arg = (arg / tb->step - 1) * tb->scal;
865
866 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV,
867 arg);
868 if (err)
869 return err;
870 }
871
872 return err;
873}
874
875int mtk_pinconf_drive_get_rev1(struct mtk_pinctrl *hw,
876 const struct mtk_pin_desc *desc, int *val)
877{
878 const struct mtk_drive_desc *tb;
879 int err, val1;
880
881 tb = &mtk_drive[desc->drv_n];
882
883 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, &val1);
884 if (err)
885 return err;
886
887 *val = ((val1 & 0x7) / tb->scal + 1) * tb->step;
888
889 return 0;
890}
891
892/* Revision direct value */
893int mtk_pinconf_drive_set_direct_val(struct mtk_pinctrl *hw,
894 const struct mtk_pin_desc *desc, u32 arg)
895{
896 int err;
897
898 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_DRV, arg);
899
900 return err;
901}
902
903int mtk_pinconf_drive_get_direct_val(struct mtk_pinctrl *hw,
904 const struct mtk_pin_desc *desc, int *val)
905{
906 int err;
907
908 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_DRV, val);
909
910 return err;
911}
912
913int mtk_pinconf_adv_pull_set(struct mtk_pinctrl *hw,
914 const struct mtk_pin_desc *desc, bool pullup,
915 u32 arg)
916{
917 int err;
918
919 /* 10K off & 50K (75K) off, when (R0, R1) = (0, 0);
920 * 10K off & 50K (75K) on, when (R0, R1) = (0, 1);
921 * 10K on & 50K (75K) off, when (R0, R1) = (1, 0);
922 * 10K on & 50K (75K) on, when (R0, R1) = (1, 1)
923 */
924 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R0, arg & 1);
925 if (err)
926 return 0;
927
928 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_R1,
929 !!(arg & 2));
930 if (err)
931 return 0;
932
933 arg = pullup ? 0 : 1;
934
935 err = mtk_hw_set_value(hw, desc, PINCTRL_PIN_REG_PUPD, arg);
936
937 /* If PUPD register is not supported for that pin, let's fallback to
938 * general bias control.
939 */
940 if (err == -ENOTSUPP) {
941 if (hw->soc->bias_set) {
942 err = hw->soc->bias_set(hw, desc, pullup);
943 if (err)
944 return err;
945 } else {
946 return -ENOTSUPP;
947 }
948 }
949
950 return err;
951}
952
953int mtk_pinconf_adv_pull_get(struct mtk_pinctrl *hw,
954 const struct mtk_pin_desc *desc, bool pullup,
955 u32 *val)
956{
957 u32 t, t2;
958 int err;
959
960 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_PUPD, &t);
961
962 /* If PUPD register is not supported for that pin, let's fallback to
963 * general bias control.
964 */
965 if (err == -ENOTSUPP) {
966 if (hw->soc->bias_get) {
967 err = hw->soc->bias_get(hw, desc, pullup, val);
968 if (err)
969 return err;
970 } else {
971 return -ENOTSUPP;
972 }
973 } else {
974 /* t == 0 supposes PULLUP for the customized PULL setup */
975 if (err)
976 return err;
977
978 if (pullup ^ !t)
979 return -EINVAL;
980 }
981
982 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R0, &t);
983 if (err)
984 return err;
985
986 err = mtk_hw_get_value(hw, desc, PINCTRL_PIN_REG_R1, &t2);
987 if (err)
988 return err;
989
990 *val = (t | t2 << 1) & 0x7;
991
992 return 0;
993}