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_pins_muxing[] = {
21 	"I2C SCL",
22 	"I2C SDA",
23 	"Uart TX",
24 	"Uart RX",
25 	"1-wire gpio",
26 };
27 
28 static const char * const sandbox_groups[] = {
29 	"i2c",
30 	"serial_a",
31 	"serial_b",
32 	"spi",
33 	"w1",
34 };
35 
36 static const char * const sandbox_functions[] = {
37 	"i2c",
38 	"serial",
39 	"spi",
40 	"w1",
41 };
42 
43 static const struct pinconf_param sandbox_conf_params[] = {
44 	{ "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
45 	{ "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
46 	{ "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
47 	{ "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
48 	{ "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
49 	{ "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
50 	{ "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
51 	{ "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
52 	{ "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
53 	{ "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
54 	{ "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
55 };
56 
sandbox_get_pins_count(struct udevice * dev)57 static int sandbox_get_pins_count(struct udevice *dev)
58 {
59 	return ARRAY_SIZE(sandbox_pins);
60 }
61 
sandbox_get_pin_name(struct udevice * dev,unsigned selector)62 static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
63 {
64 	return sandbox_pins[selector];
65 }
66 
sandbox_get_pin_muxing(struct udevice * dev,unsigned int selector,char * buf,int size)67 static int sandbox_get_pin_muxing(struct udevice *dev,
68 				  unsigned int selector,
69 				  char *buf, int size)
70 {
71 	snprintf(buf, size, "%s", sandbox_pins_muxing[selector]);
72 
73 	return 0;
74 }
75 
sandbox_get_groups_count(struct udevice * dev)76 static int sandbox_get_groups_count(struct udevice *dev)
77 {
78 	return ARRAY_SIZE(sandbox_groups);
79 }
80 
sandbox_get_group_name(struct udevice * dev,unsigned selector)81 static const char *sandbox_get_group_name(struct udevice *dev,
82 					  unsigned selector)
83 {
84 	return sandbox_groups[selector];
85 }
86 
sandbox_get_functions_count(struct udevice * dev)87 static int sandbox_get_functions_count(struct udevice *dev)
88 {
89 	return ARRAY_SIZE(sandbox_functions);
90 }
91 
sandbox_get_function_name(struct udevice * dev,unsigned selector)92 static const char *sandbox_get_function_name(struct udevice *dev,
93 					     unsigned selector)
94 {
95 	return sandbox_functions[selector];
96 }
97 
sandbox_pinmux_set(struct udevice * dev,unsigned pin_selector,unsigned func_selector)98 static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
99 			      unsigned func_selector)
100 {
101 	debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
102 	      pin_selector, sandbox_get_pin_name(dev, pin_selector),
103 	      func_selector, sandbox_get_function_name(dev, func_selector));
104 
105 	return 0;
106 }
107 
sandbox_pinmux_group_set(struct udevice * dev,unsigned group_selector,unsigned func_selector)108 static int sandbox_pinmux_group_set(struct udevice *dev,
109 				    unsigned group_selector,
110 				    unsigned func_selector)
111 {
112 	debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
113 	      group_selector, sandbox_get_group_name(dev, group_selector),
114 	      func_selector, sandbox_get_function_name(dev, func_selector));
115 
116 	return 0;
117 }
118 
sandbox_pinconf_set(struct udevice * dev,unsigned pin_selector,unsigned param,unsigned argument)119 static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
120 			       unsigned param, unsigned argument)
121 {
122 	debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
123 	      pin_selector, sandbox_get_pin_name(dev, pin_selector),
124 	      param, argument);
125 
126 	return 0;
127 }
128 
sandbox_pinconf_group_set(struct udevice * dev,unsigned group_selector,unsigned param,unsigned argument)129 static int sandbox_pinconf_group_set(struct udevice *dev,
130 				     unsigned group_selector,
131 				     unsigned param, unsigned argument)
132 {
133 	debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
134 	      group_selector, sandbox_get_group_name(dev, group_selector),
135 	      param, argument);
136 
137 	return 0;
138 }
139 
140 const struct pinctrl_ops sandbox_pinctrl_ops = {
141 	.get_pins_count = sandbox_get_pins_count,
142 	.get_pin_name = sandbox_get_pin_name,
143 	.get_pin_muxing = sandbox_get_pin_muxing,
144 	.get_groups_count = sandbox_get_groups_count,
145 	.get_group_name = sandbox_get_group_name,
146 	.get_functions_count = sandbox_get_functions_count,
147 	.get_function_name = sandbox_get_function_name,
148 	.pinmux_set = sandbox_pinmux_set,
149 	.pinmux_group_set = sandbox_pinmux_group_set,
150 	.pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
151 	.pinconf_params = sandbox_conf_params,
152 	.pinconf_set = sandbox_pinconf_set,
153 	.pinconf_group_set = sandbox_pinconf_group_set,
154 	.set_state = pinctrl_generic_set_state,
155 };
156 
157 static const struct udevice_id sandbox_pinctrl_match[] = {
158 	{ .compatible = "sandbox,pinctrl" },
159 	{ /* sentinel */ }
160 };
161 
162 U_BOOT_DRIVER(sandbox_pinctrl) = {
163 	.name = "sandbox_pinctrl",
164 	.id = UCLASS_PINCTRL,
165 	.of_match = sandbox_pinctrl_match,
166 	.ops = &sandbox_pinctrl_ops,
167 };
168