1*b3f8e88fSPhilippe Reynes // SPDX-License-Identifier: GPL-2.0
2*b3f8e88fSPhilippe Reynes
3*b3f8e88fSPhilippe Reynes #include <common.h>
4*b3f8e88fSPhilippe Reynes #include <dm.h>
5*b3f8e88fSPhilippe Reynes #include <regmap.h>
6*b3f8e88fSPhilippe Reynes #include <syscon.h>
7*b3f8e88fSPhilippe Reynes #include <dm/pinctrl.h>
8*b3f8e88fSPhilippe Reynes
9*b3f8e88fSPhilippe Reynes #define BCM6838_CMD_LOAD_MUX 0x21
10*b3f8e88fSPhilippe Reynes
11*b3f8e88fSPhilippe Reynes #define BCM6838_FUNC_OFFS 12
12*b3f8e88fSPhilippe Reynes #define BCM6838_FUNC_MASK (0x37 << BCM6838_FUNC_OFFS)
13*b3f8e88fSPhilippe Reynes #define BCM6838_PIN_OFFS 0
14*b3f8e88fSPhilippe Reynes #define BCM6838_PIN_MASK (0xfff << BCM6838_PIN_OFFS)
15*b3f8e88fSPhilippe Reynes
16*b3f8e88fSPhilippe Reynes #define BCM6838_MAX_PIN_NAME_LEN 8
17*b3f8e88fSPhilippe Reynes static char bcm6838_pin_name[BCM6838_MAX_PIN_NAME_LEN];
18*b3f8e88fSPhilippe Reynes
19*b3f8e88fSPhilippe Reynes #define BCM6838_MAX_FUNC_NAME_LEN 8
20*b3f8e88fSPhilippe Reynes static char bcm6838_func_name[BCM6838_MAX_FUNC_NAME_LEN];
21*b3f8e88fSPhilippe Reynes
22*b3f8e88fSPhilippe Reynes struct bcm6838_test_port_hw {
23*b3f8e88fSPhilippe Reynes unsigned long port_blk_data1;
24*b3f8e88fSPhilippe Reynes unsigned long port_blk_data2;
25*b3f8e88fSPhilippe Reynes unsigned long port_command;
26*b3f8e88fSPhilippe Reynes };
27*b3f8e88fSPhilippe Reynes
28*b3f8e88fSPhilippe Reynes static const struct bcm6838_test_port_hw bcm6838_hw = {
29*b3f8e88fSPhilippe Reynes .port_blk_data1 = 0x10,
30*b3f8e88fSPhilippe Reynes .port_blk_data2 = 0x14,
31*b3f8e88fSPhilippe Reynes .port_command = 0x18
32*b3f8e88fSPhilippe Reynes };
33*b3f8e88fSPhilippe Reynes
34*b3f8e88fSPhilippe Reynes struct bcm6838_pinctrl_priv {
35*b3f8e88fSPhilippe Reynes const struct bcm6838_test_port_hw *hw;
36*b3f8e88fSPhilippe Reynes struct regmap *regmap;
37*b3f8e88fSPhilippe Reynes u32 pins_count;
38*b3f8e88fSPhilippe Reynes u32 functions_count;
39*b3f8e88fSPhilippe Reynes };
40*b3f8e88fSPhilippe Reynes
bcm6838_pinctrl_get_pins_count(struct udevice * dev)41*b3f8e88fSPhilippe Reynes int bcm6838_pinctrl_get_pins_count(struct udevice *dev)
42*b3f8e88fSPhilippe Reynes {
43*b3f8e88fSPhilippe Reynes struct bcm6838_pinctrl_priv *priv = dev_get_priv(dev);
44*b3f8e88fSPhilippe Reynes
45*b3f8e88fSPhilippe Reynes return priv->pins_count;
46*b3f8e88fSPhilippe Reynes }
47*b3f8e88fSPhilippe Reynes
bcm6838_pinctrl_get_pin_name(struct udevice * dev,unsigned int selector)48*b3f8e88fSPhilippe Reynes const char *bcm6838_pinctrl_get_pin_name(struct udevice *dev,
49*b3f8e88fSPhilippe Reynes unsigned int selector)
50*b3f8e88fSPhilippe Reynes {
51*b3f8e88fSPhilippe Reynes snprintf(bcm6838_pin_name, BCM6838_MAX_PIN_NAME_LEN, "%u", selector);
52*b3f8e88fSPhilippe Reynes return bcm6838_pin_name;
53*b3f8e88fSPhilippe Reynes }
54*b3f8e88fSPhilippe Reynes
bcm6838_pinctrl_get_functions_count(struct udevice * dev)55*b3f8e88fSPhilippe Reynes int bcm6838_pinctrl_get_functions_count(struct udevice *dev)
56*b3f8e88fSPhilippe Reynes {
57*b3f8e88fSPhilippe Reynes struct bcm6838_pinctrl_priv *priv = dev_get_priv(dev);
58*b3f8e88fSPhilippe Reynes
59*b3f8e88fSPhilippe Reynes return priv->functions_count;
60*b3f8e88fSPhilippe Reynes }
61*b3f8e88fSPhilippe Reynes
bcm6838_pinctrl_get_function_name(struct udevice * dev,unsigned int selector)62*b3f8e88fSPhilippe Reynes const char *bcm6838_pinctrl_get_function_name(struct udevice *dev,
63*b3f8e88fSPhilippe Reynes unsigned int selector)
64*b3f8e88fSPhilippe Reynes {
65*b3f8e88fSPhilippe Reynes snprintf(bcm6838_func_name, BCM6838_MAX_FUNC_NAME_LEN, "%u", selector);
66*b3f8e88fSPhilippe Reynes return bcm6838_func_name;
67*b3f8e88fSPhilippe Reynes }
68*b3f8e88fSPhilippe Reynes
bcm6838_pinctrl_pinmux_set(struct udevice * dev,unsigned int pin_selector,unsigned int func_selector)69*b3f8e88fSPhilippe Reynes int bcm6838_pinctrl_pinmux_set(struct udevice *dev,
70*b3f8e88fSPhilippe Reynes unsigned int pin_selector,
71*b3f8e88fSPhilippe Reynes unsigned int func_selector)
72*b3f8e88fSPhilippe Reynes {
73*b3f8e88fSPhilippe Reynes struct bcm6838_pinctrl_priv *priv = dev_get_priv(dev);
74*b3f8e88fSPhilippe Reynes const struct bcm6838_test_port_hw *hw = priv->hw;
75*b3f8e88fSPhilippe Reynes unsigned int data;
76*b3f8e88fSPhilippe Reynes
77*b3f8e88fSPhilippe Reynes regmap_write(priv->regmap, hw->port_blk_data1, 0);
78*b3f8e88fSPhilippe Reynes data = (func_selector << BCM6838_FUNC_OFFS) & BCM6838_FUNC_MASK;
79*b3f8e88fSPhilippe Reynes data |= (pin_selector << BCM6838_PIN_OFFS) & BCM6838_PIN_MASK;
80*b3f8e88fSPhilippe Reynes regmap_write(priv->regmap, hw->port_blk_data2, data);
81*b3f8e88fSPhilippe Reynes regmap_write(priv->regmap, hw->port_command, BCM6838_CMD_LOAD_MUX);
82*b3f8e88fSPhilippe Reynes
83*b3f8e88fSPhilippe Reynes return 0;
84*b3f8e88fSPhilippe Reynes }
85*b3f8e88fSPhilippe Reynes
bcm6838_pinctrl_probe(struct udevice * dev)86*b3f8e88fSPhilippe Reynes int bcm6838_pinctrl_probe(struct udevice *dev)
87*b3f8e88fSPhilippe Reynes {
88*b3f8e88fSPhilippe Reynes struct bcm6838_pinctrl_priv *priv = dev_get_priv(dev);
89*b3f8e88fSPhilippe Reynes const struct bcm6838_test_port_hw *hw =
90*b3f8e88fSPhilippe Reynes (const struct bcm6838_test_port_hw *)dev_get_driver_data(dev);
91*b3f8e88fSPhilippe Reynes int err;
92*b3f8e88fSPhilippe Reynes u32 phandle;
93*b3f8e88fSPhilippe Reynes ofnode node;
94*b3f8e88fSPhilippe Reynes
95*b3f8e88fSPhilippe Reynes err = ofnode_read_u32(dev_ofnode(dev), "regmap", &phandle);
96*b3f8e88fSPhilippe Reynes if (err) {
97*b3f8e88fSPhilippe Reynes dev_err(dev, "%s: unable to read regmap\n", __func__);
98*b3f8e88fSPhilippe Reynes goto out;
99*b3f8e88fSPhilippe Reynes }
100*b3f8e88fSPhilippe Reynes
101*b3f8e88fSPhilippe Reynes node = ofnode_get_by_phandle(phandle);
102*b3f8e88fSPhilippe Reynes if (!ofnode_valid(node)) {
103*b3f8e88fSPhilippe Reynes dev_err(dev, "%s: unable to find node\n", __func__);
104*b3f8e88fSPhilippe Reynes err = -EINVAL;
105*b3f8e88fSPhilippe Reynes goto out;
106*b3f8e88fSPhilippe Reynes }
107*b3f8e88fSPhilippe Reynes
108*b3f8e88fSPhilippe Reynes priv->regmap = syscon_node_to_regmap(node);
109*b3f8e88fSPhilippe Reynes if (!priv->regmap) {
110*b3f8e88fSPhilippe Reynes dev_err(dev, "%s: unable to find regmap\n", __func__);
111*b3f8e88fSPhilippe Reynes err = -ENODEV;
112*b3f8e88fSPhilippe Reynes goto out;
113*b3f8e88fSPhilippe Reynes }
114*b3f8e88fSPhilippe Reynes
115*b3f8e88fSPhilippe Reynes err = ofnode_read_u32(dev_ofnode(dev), "brcm,pins-count",
116*b3f8e88fSPhilippe Reynes &priv->pins_count);
117*b3f8e88fSPhilippe Reynes if (err) {
118*b3f8e88fSPhilippe Reynes dev_err(dev, "%s: unable to read brcm,pins-count\n",
119*b3f8e88fSPhilippe Reynes __func__);
120*b3f8e88fSPhilippe Reynes goto out;
121*b3f8e88fSPhilippe Reynes }
122*b3f8e88fSPhilippe Reynes
123*b3f8e88fSPhilippe Reynes err = ofnode_read_u32(dev_ofnode(dev), "brcm,functions-count",
124*b3f8e88fSPhilippe Reynes &priv->functions_count);
125*b3f8e88fSPhilippe Reynes if (err) {
126*b3f8e88fSPhilippe Reynes dev_err(dev, "%s: unable to read brcm,functions-count\n",
127*b3f8e88fSPhilippe Reynes __func__);
128*b3f8e88fSPhilippe Reynes goto out;
129*b3f8e88fSPhilippe Reynes }
130*b3f8e88fSPhilippe Reynes
131*b3f8e88fSPhilippe Reynes priv->hw = hw;
132*b3f8e88fSPhilippe Reynes
133*b3f8e88fSPhilippe Reynes out:
134*b3f8e88fSPhilippe Reynes return err;
135*b3f8e88fSPhilippe Reynes }
136*b3f8e88fSPhilippe Reynes
137*b3f8e88fSPhilippe Reynes const struct pinctrl_ops bcm6838_pinctrl_ops = {
138*b3f8e88fSPhilippe Reynes .set_state = pinctrl_generic_set_state,
139*b3f8e88fSPhilippe Reynes .get_pins_count = bcm6838_pinctrl_get_pins_count,
140*b3f8e88fSPhilippe Reynes .get_pin_name = bcm6838_pinctrl_get_pin_name,
141*b3f8e88fSPhilippe Reynes .get_functions_count = bcm6838_pinctrl_get_functions_count,
142*b3f8e88fSPhilippe Reynes .get_function_name = bcm6838_pinctrl_get_function_name,
143*b3f8e88fSPhilippe Reynes .pinmux_set = bcm6838_pinctrl_pinmux_set,
144*b3f8e88fSPhilippe Reynes };
145*b3f8e88fSPhilippe Reynes
146*b3f8e88fSPhilippe Reynes static const struct udevice_id bcm6838_pinctrl_match[] = {
147*b3f8e88fSPhilippe Reynes {
148*b3f8e88fSPhilippe Reynes .compatible = "brcm,bcm6838-pinctrl",
149*b3f8e88fSPhilippe Reynes .data = (ulong)&bcm6838_hw,
150*b3f8e88fSPhilippe Reynes },
151*b3f8e88fSPhilippe Reynes { /* sentinel */ }
152*b3f8e88fSPhilippe Reynes };
153*b3f8e88fSPhilippe Reynes
154*b3f8e88fSPhilippe Reynes U_BOOT_DRIVER(bcm6838_pinctrl) = {
155*b3f8e88fSPhilippe Reynes .name = "bcm6838_pinctrl",
156*b3f8e88fSPhilippe Reynes .id = UCLASS_PINCTRL,
157*b3f8e88fSPhilippe Reynes .of_match = bcm6838_pinctrl_match,
158*b3f8e88fSPhilippe Reynes .ops = &bcm6838_pinctrl_ops,
159*b3f8e88fSPhilippe Reynes .priv_auto_alloc_size = sizeof(struct bcm6838_pinctrl_priv),
160*b3f8e88fSPhilippe Reynes .probe = bcm6838_pinctrl_probe,
161*b3f8e88fSPhilippe Reynes };
162