developer | e194781 | 2019-09-25 17:45:26 +0800 | [diff] [blame] | 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 | |
developer | e194781 | 2019-09-25 17:45:26 +0800 | [diff] [blame] | 8 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
developer | e194781 | 2019-09-25 17:45:26 +0800 | [diff] [blame] | 10 | #include <dm/pinctrl.h> |
| 11 | #include <linux/io.h> |
| 12 | |
| 13 | #include "pinctrl-mtmips-common.h" |
| 14 | |
developer | 6d1a630 | 2022-05-20 11:22:49 +0800 | [diff] [blame] | 15 | void mtmips_pinctrl_reg_set(struct mtmips_pinctrl_priv *priv, |
| 16 | u32 reg, u32 shift, u32 mask, u32 value) |
developer | e194781 | 2019-09-25 17:45:26 +0800 | [diff] [blame] | 17 | { |
| 18 | u32 val; |
| 19 | |
| 20 | val = readl(priv->base + reg); |
| 21 | val &= ~(mask << shift); |
| 22 | val |= value << shift; |
| 23 | writel(val, priv->base + reg); |
| 24 | } |
| 25 | |
| 26 | int mtmips_get_functions_count(struct udevice *dev) |
| 27 | { |
| 28 | struct mtmips_pinctrl_priv *priv = dev_get_priv(dev); |
| 29 | |
| 30 | return priv->nfuncs; |
| 31 | } |
| 32 | |
| 33 | const char *mtmips_get_function_name(struct udevice *dev, unsigned int selector) |
| 34 | { |
| 35 | struct mtmips_pinctrl_priv *priv = dev_get_priv(dev); |
| 36 | |
| 37 | return priv->funcs[selector]->name; |
| 38 | } |
| 39 | |
| 40 | int mtmips_pinmux_group_set(struct udevice *dev, unsigned int group_selector, |
| 41 | unsigned int func_selector) |
| 42 | { |
| 43 | struct mtmips_pinctrl_priv *priv = dev_get_priv(dev); |
| 44 | const struct mtmips_pmx_group *grp = &priv->groups[group_selector]; |
| 45 | const struct mtmips_pmx_func *func = priv->funcs[func_selector]; |
| 46 | int i; |
| 47 | |
| 48 | if (!grp->nfuncs) |
| 49 | return 0; |
| 50 | |
| 51 | for (i = 0; i < grp->nfuncs; i++) { |
| 52 | if (!strcmp(grp->funcs[i].name, func->name)) { |
| 53 | mtmips_pinctrl_reg_set(priv, grp->reg, grp->shift, |
| 54 | grp->mask, grp->funcs[i].value); |
| 55 | return 0; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | int mtmips_pinctrl_probe(struct mtmips_pinctrl_priv *priv, u32 ngroups, |
| 63 | const struct mtmips_pmx_group *groups) |
| 64 | { |
| 65 | int i, j, n; |
| 66 | |
| 67 | priv->ngroups = ngroups; |
| 68 | priv->groups = groups; |
| 69 | |
| 70 | priv->nfuncs = 0; |
| 71 | |
| 72 | for (i = 0; i < ngroups; i++) |
| 73 | priv->nfuncs += groups[i].nfuncs; |
| 74 | |
| 75 | priv->funcs = malloc(priv->nfuncs * sizeof(*priv->funcs)); |
| 76 | if (!priv->funcs) |
| 77 | return -ENOMEM; |
| 78 | |
| 79 | n = 0; |
| 80 | |
| 81 | for (i = 0; i < ngroups; i++) { |
| 82 | for (j = 0; j < groups[i].nfuncs; j++) |
| 83 | priv->funcs[n++] = &groups[i].funcs[j]; |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |