1 /*
2  * Copyright 2017 Google, Inc
3  *
4  * SPDX-License-Identifier: GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <asm/io.h>
11 #include <asm/arch/pinctrl.h>
12 #include <asm/arch/scu_ast2500.h>
13 #include <dm/pinctrl.h>
14 
15 /*
16  * This driver works with very simple configuration that has the same name
17  * for group and function. This way it is compatible with the Linux Kernel
18  * driver.
19  */
20 
21 struct ast2500_pinctrl_priv {
22 	struct ast2500_scu *scu;
23 };
24 
25 static int ast2500_pinctrl_probe(struct udevice *dev)
26 {
27 	struct ast2500_pinctrl_priv *priv = dev_get_priv(dev);
28 
29 	priv->scu = ast_get_scu();
30 
31 	return 0;
32 }
33 
34 struct ast2500_group_config {
35 	char *group_name;
36 	/* Control register number (1-10) */
37 	unsigned reg_num;
38 	/* The mask of control bits in the register */
39 	u32 ctrl_bit_mask;
40 };
41 
42 static const struct ast2500_group_config ast2500_groups[] = {
43 	{ "I2C1", 8, (1 << 13) | (1 << 12) },
44 	{ "I2C2", 8, (1 << 15) | (1 << 14) },
45 	{ "I2C3", 8, (1 << 16) },
46 	{ "I2C4", 5, (1 << 17) },
47 	{ "I2C4", 5, (1 << 17) },
48 	{ "I2C5", 5, (1 << 18) },
49 	{ "I2C6", 5, (1 << 19) },
50 	{ "I2C7", 5, (1 << 20) },
51 	{ "I2C8", 5, (1 << 21) },
52 	{ "I2C9", 5, (1 << 22) },
53 	{ "I2C10", 5, (1 << 23) },
54 	{ "I2C11", 5, (1 << 24) },
55 	{ "I2C12", 5, (1 << 25) },
56 	{ "I2C13", 5, (1 << 26) },
57 	{ "I2C14", 5, (1 << 27) },
58 	{ "MAC1LINK", 1, (1 << 0) },
59 	{ "MDIO1", 3, (1 << 31) | (1 << 30) },
60 	{ "MAC2LINK", 1, (1 << 1) },
61 	{ "MDIO2", 5, (1 << 2) },
62 };
63 
64 static int ast2500_pinctrl_get_groups_count(struct udevice *dev)
65 {
66 	debug("PINCTRL: get_(functions/groups)_count\n");
67 
68 	return ARRAY_SIZE(ast2500_groups);
69 }
70 
71 static const char *ast2500_pinctrl_get_group_name(struct udevice *dev,
72 						  unsigned selector)
73 {
74 	debug("PINCTRL: get_(function/group)_name %u\n", selector);
75 
76 	return ast2500_groups[selector].group_name;
77 }
78 
79 static int ast2500_pinctrl_group_set(struct udevice *dev, unsigned selector,
80 				     unsigned func_selector)
81 {
82 	struct ast2500_pinctrl_priv *priv = dev_get_priv(dev);
83 	const struct ast2500_group_config *config;
84 	u32 *ctrl_reg;
85 
86 	debug("PINCTRL: group_set <%u, %u>\n", selector, func_selector);
87 	if (selector >= ARRAY_SIZE(ast2500_groups))
88 		return -EINVAL;
89 
90 	config = &ast2500_groups[selector];
91 	if (config->reg_num > 6)
92 		ctrl_reg = &priv->scu->pinmux_ctrl1[config->reg_num - 7];
93 	else
94 		ctrl_reg = &priv->scu->pinmux_ctrl[config->reg_num - 1];
95 
96 	ast_scu_unlock(priv->scu);
97 	setbits_le32(ctrl_reg, config->ctrl_bit_mask);
98 	ast_scu_lock(priv->scu);
99 
100 	return 0;
101 }
102 
103 static struct pinctrl_ops ast2500_pinctrl_ops = {
104 	.set_state = pinctrl_generic_set_state,
105 	.get_groups_count = ast2500_pinctrl_get_groups_count,
106 	.get_group_name = ast2500_pinctrl_get_group_name,
107 	.get_functions_count = ast2500_pinctrl_get_groups_count,
108 	.get_function_name = ast2500_pinctrl_get_group_name,
109 	.pinmux_group_set = ast2500_pinctrl_group_set,
110 };
111 
112 static const struct udevice_id ast2500_pinctrl_ids[] = {
113 	{ .compatible = "aspeed,ast2500-pinctrl" },
114 	{ .compatible = "aspeed,g5-pinctrl" },
115 	{ }
116 };
117 
118 U_BOOT_DRIVER(pinctrl_ast2500) = {
119 	.name = "aspeed_ast2500_pinctrl",
120 	.id = UCLASS_PINCTRL,
121 	.of_match = ast2500_pinctrl_ids,
122 	.priv_auto_alloc_size = sizeof(struct ast2500_pinctrl_priv),
123 	.ops = &ast2500_pinctrl_ops,
124 	.probe = ast2500_pinctrl_probe,
125 };
126