blob: c7351f32bb60d60c1644f6795a881db6666e1051 [file] [log] [blame]
developer84c7a632018-11-15 10:07:58 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2018 MediaTek Inc.
4 * Author: Ryder Lee <ryder.lee@mediatek.com>
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <dm/device-internal.h>
10#include <dm/lists.h>
11#include <dm/pinctrl.h>
12#include <asm/io.h>
13#include <asm-generic/gpio.h>
14
15#include "pinctrl-mtk-common.h"
16
Fabien Parent105f6c82019-07-18 19:08:08 +020017#if CONFIG_IS_ENABLED(PINCONF)
developer84c7a632018-11-15 10:07:58 +080018/**
19 * struct mtk_drive_desc - the structure that holds the information
20 * of the driving current
21 * @min: the minimum current of this group
22 * @max: the maximum current of this group
23 * @step: the step current of this group
24 * @scal: the weight factor
25 *
26 * formula: output = ((input) / step - 1) * scal
27 */
28struct mtk_drive_desc {
29 u8 min;
30 u8 max;
31 u8 step;
32 u8 scal;
33};
34
35/* The groups of drive strength */
36static const struct mtk_drive_desc mtk_drive[] = {
37 [DRV_GRP0] = { 4, 16, 4, 1 },
38 [DRV_GRP1] = { 4, 16, 4, 2 },
39 [DRV_GRP2] = { 2, 8, 2, 1 },
40 [DRV_GRP3] = { 2, 8, 2, 2 },
41 [DRV_GRP4] = { 2, 16, 2, 1 },
42};
Fabien Parent105f6c82019-07-18 19:08:08 +020043#endif
developer84c7a632018-11-15 10:07:58 +080044
45static const char *mtk_pinctrl_dummy_name = "_dummy";
46
47static void mtk_w32(struct udevice *dev, u32 reg, u32 val)
48{
49 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
50
51 __raw_writel(val, priv->base + reg);
52}
53
54static u32 mtk_r32(struct udevice *dev, u32 reg)
55{
56 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
57
58 return __raw_readl(priv->base + reg);
59}
60
61static inline int get_count_order(unsigned int count)
62{
63 int order;
64
65 order = fls(count) - 1;
66 if (count & (count - 1))
67 order++;
68 return order;
69}
70
71void mtk_rmw(struct udevice *dev, u32 reg, u32 mask, u32 set)
72{
73 u32 val;
74
75 val = mtk_r32(dev, reg);
76 val &= ~mask;
77 val |= set;
78 mtk_w32(dev, reg, val);
79}
80
81static int mtk_hw_pin_field_lookup(struct udevice *dev, int pin,
82 const struct mtk_pin_reg_calc *rc,
83 struct mtk_pin_field *pfd)
84{
85 const struct mtk_pin_field_calc *c, *e;
86 u32 bits;
87
88 c = rc->range;
89 e = c + rc->nranges;
90
91 while (c < e) {
92 if (pin >= c->s_pin && pin <= c->e_pin)
93 break;
94 c++;
95 }
96
97 if (c >= e)
98 return -EINVAL;
99
100 /* Calculated bits as the overall offset the pin is located at,
101 * if c->fixed is held, that determines the all the pins in the
102 * range use the same field with the s_pin.
103 */
104 bits = c->fixed ? c->s_bit : c->s_bit + (pin - c->s_pin) * (c->x_bits);
105
106 /* Fill pfd from bits. For example 32-bit register applied is assumed
107 * when c->sz_reg is equal to 32.
108 */
109 pfd->offset = c->s_addr + c->x_addrs * (bits / c->sz_reg);
110 pfd->bitpos = bits % c->sz_reg;
111 pfd->mask = (1 << c->x_bits) - 1;
112
113 /* pfd->next is used for indicating that bit wrapping-around happens
114 * which requires the manipulation for bit 0 starting in the next
115 * register to form the complete field read/write.
116 */
117 pfd->next = pfd->bitpos + c->x_bits > c->sz_reg ? c->x_addrs : 0;
118
119 return 0;
120}
121
122static int mtk_hw_pin_field_get(struct udevice *dev, int pin,
123 int field, struct mtk_pin_field *pfd)
124{
125 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
126 const struct mtk_pin_reg_calc *rc;
127
128 if (field < 0 || field >= PINCTRL_PIN_REG_MAX)
129 return -EINVAL;
130
131 if (priv->soc->reg_cal && priv->soc->reg_cal[field].range)
132 rc = &priv->soc->reg_cal[field];
133 else
134 return -EINVAL;
135
136 return mtk_hw_pin_field_lookup(dev, pin, rc, pfd);
137}
138
139static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
140{
141 *l = 32 - pf->bitpos;
142 *h = get_count_order(pf->mask) - *l;
143}
144
145static void mtk_hw_write_cross_field(struct udevice *dev,
146 struct mtk_pin_field *pf, int value)
147{
148 int nbits_l, nbits_h;
149
150 mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
151
152 mtk_rmw(dev, pf->offset, pf->mask << pf->bitpos,
153 (value & pf->mask) << pf->bitpos);
154
155 mtk_rmw(dev, pf->offset + pf->next, BIT(nbits_h) - 1,
156 (value & pf->mask) >> nbits_l);
157}
158
159static void mtk_hw_read_cross_field(struct udevice *dev,
160 struct mtk_pin_field *pf, int *value)
161{
162 int nbits_l, nbits_h, h, l;
163
164 mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
165
166 l = (mtk_r32(dev, pf->offset) >> pf->bitpos) & (BIT(nbits_l) - 1);
167 h = (mtk_r32(dev, pf->offset + pf->next)) & (BIT(nbits_h) - 1);
168
169 *value = (h << nbits_l) | l;
170}
171
172static int mtk_hw_set_value(struct udevice *dev, int pin, int field,
173 int value)
174{
175 struct mtk_pin_field pf;
176 int err;
177
178 err = mtk_hw_pin_field_get(dev, pin, field, &pf);
179 if (err)
180 return err;
181
182 if (!pf.next)
183 mtk_rmw(dev, pf.offset, pf.mask << pf.bitpos,
184 (value & pf.mask) << pf.bitpos);
185 else
186 mtk_hw_write_cross_field(dev, &pf, value);
187
188 return 0;
189}
190
191static int mtk_hw_get_value(struct udevice *dev, int pin, int field,
192 int *value)
193{
194 struct mtk_pin_field pf;
195 int err;
196
197 err = mtk_hw_pin_field_get(dev, pin, field, &pf);
198 if (err)
199 return err;
200
201 if (!pf.next)
202 *value = (mtk_r32(dev, pf.offset) >> pf.bitpos) & pf.mask;
203 else
204 mtk_hw_read_cross_field(dev, &pf, value);
205
206 return 0;
207}
208
209static int mtk_get_groups_count(struct udevice *dev)
210{
211 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
212
213 return priv->soc->ngrps;
214}
215
216static const char *mtk_get_pin_name(struct udevice *dev,
217 unsigned int selector)
218{
219 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
220
221 if (!priv->soc->grps[selector].name)
222 return mtk_pinctrl_dummy_name;
223
224 return priv->soc->pins[selector].name;
225}
226
227static int mtk_get_pins_count(struct udevice *dev)
228{
229 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
230
231 return priv->soc->npins;
232}
233
234static const char *mtk_get_group_name(struct udevice *dev,
235 unsigned int selector)
236{
237 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
238
239 if (!priv->soc->grps[selector].name)
240 return mtk_pinctrl_dummy_name;
241
242 return priv->soc->grps[selector].name;
243}
244
245static int mtk_get_functions_count(struct udevice *dev)
246{
247 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
248
249 return priv->soc->nfuncs;
250}
251
252static const char *mtk_get_function_name(struct udevice *dev,
253 unsigned int selector)
254{
255 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
256
257 if (!priv->soc->funcs[selector].name)
258 return mtk_pinctrl_dummy_name;
259
260 return priv->soc->funcs[selector].name;
261}
262
263static int mtk_pinmux_group_set(struct udevice *dev,
264 unsigned int group_selector,
265 unsigned int func_selector)
266{
267 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
268 const struct mtk_group_desc *grp =
269 &priv->soc->grps[group_selector];
270 int i;
271
272 for (i = 0; i < grp->num_pins; i++) {
273 int *pin_modes = grp->data;
274
275 mtk_hw_set_value(dev, grp->pins[i], PINCTRL_PIN_REG_MODE,
276 pin_modes[i]);
277 }
278
279 return 0;
280}
281
282#if CONFIG_IS_ENABLED(PINCONF)
283static const struct pinconf_param mtk_conf_params[] = {
284 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
285 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
286 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
287 { "input-schmitt-enable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 1 },
288 { "input-schmitt-disable", PIN_CONFIG_INPUT_SCHMITT_ENABLE, 0 },
289 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
290 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
291 { "output-enable", PIN_CONFIG_OUTPUT_ENABLE, 1 },
292 { "output-high", PIN_CONFIG_OUTPUT, 1, },
293 { "output-low", PIN_CONFIG_OUTPUT, 0, },
294 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
295};
296
developer74d69012020-01-10 16:30:28 +0800297
298int mtk_pinconf_bias_set_v0(struct udevice *dev, u32 pin, u32 arg)
299{
300 int err, disable, pullup;
301
302 disable = (arg == PIN_CONFIG_BIAS_DISABLE);
303 pullup = (arg == PIN_CONFIG_BIAS_PULL_UP);
304
305 if (disable) {
306 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PU, 0);
307 if (err)
308 return err;
309 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PD, 0);
310 if (err)
311 return err;
312
313 } else {
314 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PU, pullup);
315 if (err)
316 return err;
317 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PD, !pullup);
318 if (err)
319 return err;
320 }
321
322 return 0;
323}
324
325int mtk_pinconf_bias_set_v1(struct udevice *dev, u32 pin, u32 arg)
326{
327 int err, disable, pullup;
328
329 disable = (arg == PIN_CONFIG_BIAS_DISABLE);
330 pullup = (arg == PIN_CONFIG_BIAS_PULL_UP);
331
332 if (disable) {
333 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLEN, 0);
334 if (err)
335 return err;
336 } else {
337 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLEN, 1);
338 if (err)
339 return err;
340 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_PULLSEL,
341 pullup);
342 if (err)
343 return err;
344 }
345
346 return 0;
347}
348
349int mtk_pinconf_input_enable_v1(struct udevice *dev, u32 pin, u32 arg)
350{
351 int err;
352
353 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_IES, 1);
354 if (err)
355 return err;
356 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 0);
357 if (err)
358 return err;
359 return 0;
360}
361
362int mtk_pinconf_drive_set_v0(struct udevice *dev, u32 pin, u32 arg)
developer84c7a632018-11-15 10:07:58 +0800363{
364 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
365 const struct mtk_pin_desc *desc = &priv->soc->pins[pin];
366 const struct mtk_drive_desc *tb;
367 int err = -ENOTSUPP;
368
369 tb = &mtk_drive[desc->drv_n];
370 /* 4mA when (e8, e4) = (0, 0)
371 * 8mA when (e8, e4) = (0, 1)
372 * 12mA when (e8, e4) = (1, 0)
373 * 16mA when (e8, e4) = (1, 1)
374 */
375 if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
376 arg = (arg / tb->step - 1) * tb->scal;
developer74d69012020-01-10 16:30:28 +0800377 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_E4,
378 arg & 0x1);
379 if (err)
380 return err;
381 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_E8,
382 (arg & 0x2) >> 1);
383 if (err)
384 return err;
385 }
386
387 return 0;
388}
developer84c7a632018-11-15 10:07:58 +0800389
developer74d69012020-01-10 16:30:28 +0800390
391int mtk_pinconf_drive_set_v1(struct udevice *dev, u32 pin, u32 arg)
392{
393 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
394 const struct mtk_pin_desc *desc = &priv->soc->pins[pin];
395 const struct mtk_drive_desc *tb;
396 int err = -ENOTSUPP;
397
398 tb = &mtk_drive[desc->drv_n];
399 if ((arg >= tb->min && arg <= tb->max) && !(arg % tb->step)) {
400 arg = (arg / tb->step - 1) * tb->scal;
developer84c7a632018-11-15 10:07:58 +0800401 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DRV, arg);
402 if (err)
403 return err;
404 }
405
406 return 0;
407}
408
409static int mtk_pinconf_set(struct udevice *dev, unsigned int pin,
410 unsigned int param, unsigned int arg)
411{
412 int err = 0;
developer74d69012020-01-10 16:30:28 +0800413 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
414 int rev = priv->soc->rev;
developer84c7a632018-11-15 10:07:58 +0800415
416 switch (param) {
417 case PIN_CONFIG_BIAS_DISABLE:
418 case PIN_CONFIG_BIAS_PULL_UP:
419 case PIN_CONFIG_BIAS_PULL_DOWN:
developer74d69012020-01-10 16:30:28 +0800420 if (rev == MTK_PINCTRL_V0)
421 err = mtk_pinconf_bias_set_v0(dev, pin, param);
422 else
423 err = mtk_pinconf_bias_set_v1(dev, pin, param);
developer84c7a632018-11-15 10:07:58 +0800424 if (err)
425 goto err;
426 break;
427 case PIN_CONFIG_OUTPUT_ENABLE:
428 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_SMT, 0);
429 if (err)
430 goto err;
431 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 1);
432 if (err)
433 goto err;
434 break;
435 case PIN_CONFIG_INPUT_ENABLE:
developer74d69012020-01-10 16:30:28 +0800436 if (rev == MTK_PINCTRL_V1)
437 err = mtk_pinconf_input_enable_v1(dev, pin, param);
developer84c7a632018-11-15 10:07:58 +0800438 if (err)
439 goto err;
440 break;
441 case PIN_CONFIG_OUTPUT:
442 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR, 1);
443 if (err)
444 goto err;
445
446 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DO, arg);
447 if (err)
448 goto err;
449 break;
450 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
451 /* arg = 1: Input mode & SMT enable ;
452 * arg = 0: Output mode & SMT disable
453 */
454 arg = arg ? 2 : 1;
455 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_DIR,
456 arg & 1);
457 if (err)
458 goto err;
459
460 err = mtk_hw_set_value(dev, pin, PINCTRL_PIN_REG_SMT,
461 !!(arg & 2));
462 if (err)
463 goto err;
464 break;
465 case PIN_CONFIG_DRIVE_STRENGTH:
developer74d69012020-01-10 16:30:28 +0800466 if (rev == MTK_PINCTRL_V0)
467 err = mtk_pinconf_drive_set_v0(dev, pin, arg);
468 else
469 err = mtk_pinconf_drive_set_v1(dev, pin, arg);
developer84c7a632018-11-15 10:07:58 +0800470 if (err)
471 goto err;
472 break;
473
474 default:
475 err = -ENOTSUPP;
476 }
477
478err:
479
480 return err;
481}
482
483static int mtk_pinconf_group_set(struct udevice *dev,
484 unsigned int group_selector,
485 unsigned int param, unsigned int arg)
486{
487 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
488 const struct mtk_group_desc *grp =
489 &priv->soc->grps[group_selector];
490 int i, ret;
491
492 for (i = 0; i < grp->num_pins; i++) {
493 ret = mtk_pinconf_set(dev, grp->pins[i], param, arg);
494 if (ret)
495 return ret;
496 }
497
498 return 0;
499}
500#endif
501
502const struct pinctrl_ops mtk_pinctrl_ops = {
503 .get_pins_count = mtk_get_pins_count,
504 .get_pin_name = mtk_get_pin_name,
505 .get_groups_count = mtk_get_groups_count,
506 .get_group_name = mtk_get_group_name,
507 .get_functions_count = mtk_get_functions_count,
508 .get_function_name = mtk_get_function_name,
509 .pinmux_group_set = mtk_pinmux_group_set,
510#if CONFIG_IS_ENABLED(PINCONF)
511 .pinconf_num_params = ARRAY_SIZE(mtk_conf_params),
512 .pinconf_params = mtk_conf_params,
513 .pinconf_set = mtk_pinconf_set,
514 .pinconf_group_set = mtk_pinconf_group_set,
515#endif
516 .set_state = pinctrl_generic_set_state,
517};
518
519static int mtk_gpio_get(struct udevice *dev, unsigned int off)
520{
521 int val, err;
522
523 err = mtk_hw_get_value(dev->parent, off, PINCTRL_PIN_REG_DI, &val);
524 if (err)
525 return err;
526
527 return !!val;
528}
529
530static int mtk_gpio_set(struct udevice *dev, unsigned int off, int val)
531{
532 return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DO, !!val);
533}
534
535static int mtk_gpio_get_direction(struct udevice *dev, unsigned int off)
536{
537 int val, err;
538
539 err = mtk_hw_get_value(dev->parent, off, PINCTRL_PIN_REG_DIR, &val);
540 if (err)
541 return err;
542
543 return val ? GPIOF_OUTPUT : GPIOF_INPUT;
544}
545
546static int mtk_gpio_direction_input(struct udevice *dev, unsigned int off)
547{
548 return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DIR, 0);
549}
550
551static int mtk_gpio_direction_output(struct udevice *dev,
552 unsigned int off, int val)
553{
554 mtk_gpio_set(dev, off, val);
555
556 /* And set the requested value */
557 return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_DIR, 1);
558}
559
560static int mtk_gpio_request(struct udevice *dev, unsigned int off,
561 const char *label)
562{
developer74d69012020-01-10 16:30:28 +0800563 struct mtk_pinctrl_priv *priv = dev_get_priv(dev->parent);
564
565 return mtk_hw_set_value(dev->parent, off, PINCTRL_PIN_REG_MODE,
566 priv->soc->gpio_mode);
developer84c7a632018-11-15 10:07:58 +0800567}
568
569static int mtk_gpio_probe(struct udevice *dev)
570{
571 struct mtk_pinctrl_priv *priv = dev_get_priv(dev->parent);
572 struct gpio_dev_priv *uc_priv;
573
574 uc_priv = dev_get_uclass_priv(dev);
575 uc_priv->bank_name = priv->soc->name;
576 uc_priv->gpio_count = priv->soc->npins;
577
578 return 0;
579}
580
581static const struct dm_gpio_ops mtk_gpio_ops = {
582 .request = mtk_gpio_request,
583 .set_value = mtk_gpio_set,
584 .get_value = mtk_gpio_get,
585 .get_function = mtk_gpio_get_direction,
586 .direction_input = mtk_gpio_direction_input,
587 .direction_output = mtk_gpio_direction_output,
588};
589
590static struct driver mtk_gpio_driver = {
591 .name = "mediatek_gpio",
592 .id = UCLASS_GPIO,
593 .probe = mtk_gpio_probe,
594 .ops = &mtk_gpio_ops,
595};
596
597static int mtk_gpiochip_register(struct udevice *parent)
598{
599 struct uclass_driver *drv;
600 struct udevice *dev;
601 int ret;
602 ofnode node;
603
604 drv = lists_uclass_lookup(UCLASS_GPIO);
605 if (!drv)
606 return -ENOENT;
607
608 dev_for_each_subnode(node, parent)
609 if (ofnode_read_bool(node, "gpio-controller")) {
610 ret = 0;
611 break;
612 }
613
614 if (ret)
615 return ret;
616
617 ret = device_bind_with_driver_data(parent, &mtk_gpio_driver,
618 "mediatek_gpio", 0, node,
619 &dev);
620 if (ret)
621 return ret;
622
623 return 0;
624}
625
626int mtk_pinctrl_common_probe(struct udevice *dev,
627 struct mtk_pinctrl_soc *soc)
628{
629 struct mtk_pinctrl_priv *priv = dev_get_priv(dev);
630 int ret;
631
632 priv->base = dev_read_addr_ptr(dev);
633 if (priv->base == (void *)FDT_ADDR_T_NONE)
634 return -EINVAL;
635
636 priv->soc = soc;
637
638 ret = mtk_gpiochip_register(dev);
639 if (ret)
640 return ret;
641
642 return 0;
643}