1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2019 MediaTek Inc. All Rights Reserved.
4 *
5 * Author: Weijie Gao <weijie.gao@mediatek.com>
6 */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <malloc.h>
11 #include <dm/pinctrl.h>
12 #include <linux/io.h>
13
14 #include "pinctrl-mtmips-common.h"
15
mtmips_pinctrl_reg_set(struct mtmips_pinctrl_priv * priv,u32 reg,u32 shift,u32 mask,u32 value)16 static void mtmips_pinctrl_reg_set(struct mtmips_pinctrl_priv *priv,
17 u32 reg, u32 shift, u32 mask, u32 value)
18 {
19 u32 val;
20
21 val = readl(priv->base + reg);
22 val &= ~(mask << shift);
23 val |= value << shift;
24 writel(val, priv->base + reg);
25 }
26
mtmips_get_functions_count(struct udevice * dev)27 int mtmips_get_functions_count(struct udevice *dev)
28 {
29 struct mtmips_pinctrl_priv *priv = dev_get_priv(dev);
30
31 return priv->nfuncs;
32 }
33
mtmips_get_function_name(struct udevice * dev,unsigned int selector)34 const char *mtmips_get_function_name(struct udevice *dev, unsigned int selector)
35 {
36 struct mtmips_pinctrl_priv *priv = dev_get_priv(dev);
37
38 return priv->funcs[selector]->name;
39 }
40
mtmips_pinmux_group_set(struct udevice * dev,unsigned int group_selector,unsigned int func_selector)41 int mtmips_pinmux_group_set(struct udevice *dev, unsigned int group_selector,
42 unsigned int func_selector)
43 {
44 struct mtmips_pinctrl_priv *priv = dev_get_priv(dev);
45 const struct mtmips_pmx_group *grp = &priv->groups[group_selector];
46 const struct mtmips_pmx_func *func = priv->funcs[func_selector];
47 int i;
48
49 if (!grp->nfuncs)
50 return 0;
51
52 for (i = 0; i < grp->nfuncs; i++) {
53 if (!strcmp(grp->funcs[i].name, func->name)) {
54 mtmips_pinctrl_reg_set(priv, grp->reg, grp->shift,
55 grp->mask, grp->funcs[i].value);
56 return 0;
57 }
58 }
59
60 return -EINVAL;
61 }
62
mtmips_pinctrl_probe(struct mtmips_pinctrl_priv * priv,u32 ngroups,const struct mtmips_pmx_group * groups)63 int mtmips_pinctrl_probe(struct mtmips_pinctrl_priv *priv, u32 ngroups,
64 const struct mtmips_pmx_group *groups)
65 {
66 int i, j, n;
67
68 priv->ngroups = ngroups;
69 priv->groups = groups;
70
71 priv->nfuncs = 0;
72
73 for (i = 0; i < ngroups; i++)
74 priv->nfuncs += groups[i].nfuncs;
75
76 priv->funcs = malloc(priv->nfuncs * sizeof(*priv->funcs));
77 if (!priv->funcs)
78 return -ENOMEM;
79
80 n = 0;
81
82 for (i = 0; i < ngroups; i++) {
83 for (j = 0; j < groups[i].nfuncs; j++)
84 priv->funcs[n++] = &groups[i].funcs[j];
85 }
86
87 return 0;
88 }
89