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