1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
4  */
5 
6 /* #define DEBUG */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <dm/pinctrl.h>
11 
12 static const char * const sandbox_pins[] = {
13 	"SCL",
14 	"SDA",
15 	"TX",
16 	"RX",
17 	"W1"
18 };
19 
20 static const char * const sandbox_groups[] = {
21 	"i2c",
22 	"serial_a",
23 	"serial_b",
24 	"spi",
25 	"w1",
26 };
27 
28 static const char * const sandbox_functions[] = {
29 	"i2c",
30 	"serial",
31 	"spi",
32 	"w1",
33 };
34 
35 static const struct pinconf_param sandbox_conf_params[] = {
36 	{ "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
37 	{ "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
38 	{ "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
39 	{ "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
40 	{ "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
41 	{ "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
42 	{ "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
43 	{ "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
44 	{ "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
45 	{ "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
46 	{ "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
47 };
48 
49 static int sandbox_get_pins_count(struct udevice *dev)
50 {
51 	return ARRAY_SIZE(sandbox_pins);
52 }
53 
54 static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
55 {
56 	return sandbox_pins[selector];
57 }
58 
59 static int sandbox_get_groups_count(struct udevice *dev)
60 {
61 	return ARRAY_SIZE(sandbox_groups);
62 }
63 
64 static const char *sandbox_get_group_name(struct udevice *dev,
65 					  unsigned selector)
66 {
67 	return sandbox_groups[selector];
68 }
69 
70 static int sandbox_get_functions_count(struct udevice *dev)
71 {
72 	return ARRAY_SIZE(sandbox_functions);
73 }
74 
75 static const char *sandbox_get_function_name(struct udevice *dev,
76 					     unsigned selector)
77 {
78 	return sandbox_functions[selector];
79 }
80 
81 static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
82 			      unsigned func_selector)
83 {
84 	debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
85 	      pin_selector, sandbox_get_pin_name(dev, pin_selector),
86 	      func_selector, sandbox_get_function_name(dev, func_selector));
87 
88 	return 0;
89 }
90 
91 static int sandbox_pinmux_group_set(struct udevice *dev,
92 				    unsigned group_selector,
93 				    unsigned func_selector)
94 {
95 	debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
96 	      group_selector, sandbox_get_group_name(dev, group_selector),
97 	      func_selector, sandbox_get_function_name(dev, func_selector));
98 
99 	return 0;
100 }
101 
102 static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
103 			       unsigned param, unsigned argument)
104 {
105 	debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
106 	      pin_selector, sandbox_get_pin_name(dev, pin_selector),
107 	      param, argument);
108 
109 	return 0;
110 }
111 
112 static int sandbox_pinconf_group_set(struct udevice *dev,
113 				     unsigned group_selector,
114 				     unsigned param, unsigned argument)
115 {
116 	debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
117 	      group_selector, sandbox_get_group_name(dev, group_selector),
118 	      param, argument);
119 
120 	return 0;
121 }
122 
123 const struct pinctrl_ops sandbox_pinctrl_ops = {
124 	.get_pins_count = sandbox_get_pins_count,
125 	.get_pin_name = sandbox_get_pin_name,
126 	.get_groups_count = sandbox_get_groups_count,
127 	.get_group_name = sandbox_get_group_name,
128 	.get_functions_count = sandbox_get_functions_count,
129 	.get_function_name = sandbox_get_function_name,
130 	.pinmux_set = sandbox_pinmux_set,
131 	.pinmux_group_set = sandbox_pinmux_group_set,
132 	.pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
133 	.pinconf_params = sandbox_conf_params,
134 	.pinconf_set = sandbox_pinconf_set,
135 	.pinconf_group_set = sandbox_pinconf_group_set,
136 	.set_state = pinctrl_generic_set_state,
137 };
138 
139 static const struct udevice_id sandbox_pinctrl_match[] = {
140 	{ .compatible = "sandbox,pinctrl" },
141 	{ /* sentinel */ }
142 };
143 
144 U_BOOT_DRIVER(sandbox_pinctrl) = {
145 	.name = "sandbox_pinctrl",
146 	.id = UCLASS_PINCTRL,
147 	.of_match = sandbox_pinctrl_match,
148 	.ops = &sandbox_pinctrl_ops,
149 };
150