1 /*
2  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <linux/libfdt.h>
9 #include <linux/err.h>
10 #include <linux/list.h>
11 #include <dm.h>
12 #include <dm/lists.h>
13 #include <dm/pinctrl.h>
14 #include <dm/util.h>
15 #include <dm/of_access.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 int pinctrl_decode_pin_config(const void *blob, int node)
20 {
21 	int flags = 0;
22 
23 	if (fdtdec_get_bool(blob, node, "bias-pull-up"))
24 		flags |= 1 << PIN_CONFIG_BIAS_PULL_UP;
25 	else if (fdtdec_get_bool(blob, node, "bias-pull-down"))
26 		flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
27 
28 	return flags;
29 }
30 
31 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
32 /**
33  * pinctrl_config_one() - apply pinctrl settings for a single node
34  *
35  * @config: pin configuration node
36  * @return: 0 on success, or negative error code on failure
37  */
38 static int pinctrl_config_one(struct udevice *config)
39 {
40 	struct udevice *pctldev;
41 	const struct pinctrl_ops *ops;
42 
43 	pctldev = config;
44 	for (;;) {
45 		pctldev = dev_get_parent(pctldev);
46 		if (!pctldev) {
47 			dev_err(config, "could not find pctldev\n");
48 			return -EINVAL;
49 		}
50 		if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
51 			break;
52 	}
53 
54 	ops = pinctrl_get_ops(pctldev);
55 	return ops->set_state(pctldev, config);
56 }
57 
58 /**
59  * pinctrl_select_state_full() - full implementation of pinctrl_select_state
60  *
61  * @dev: peripheral device
62  * @statename: state name, like "default"
63  * @return: 0 on success, or negative error code on failure
64  */
65 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
66 {
67 	char propname[32]; /* long enough */
68 	const fdt32_t *list;
69 	uint32_t phandle;
70 	struct udevice *config;
71 	int state, size, i, ret;
72 
73 	state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
74 	if (state < 0) {
75 		char *end;
76 		/*
77 		 * If statename is not found in "pinctrl-names",
78 		 * assume statename is just the integer state ID.
79 		 */
80 		state = simple_strtoul(statename, &end, 10);
81 		if (*end)
82 			return -EINVAL;
83 	}
84 
85 	snprintf(propname, sizeof(propname), "pinctrl-%d", state);
86 	list = dev_read_prop(dev, propname, &size);
87 	if (!list)
88 		return -EINVAL;
89 
90 	size /= sizeof(*list);
91 	for (i = 0; i < size; i++) {
92 		phandle = fdt32_to_cpu(*list++);
93 		ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
94 						      &config);
95 		if (ret)
96 			return ret;
97 
98 		ret = pinctrl_config_one(config);
99 		if (ret)
100 			return ret;
101 	}
102 
103 	return 0;
104 }
105 
106 /**
107  * pinconfig_post_bind() - post binding for PINCONFIG uclass
108  * Recursively bind its children as pinconfig devices.
109  *
110  * @dev: pinconfig device
111  * @return: 0 on success, or negative error code on failure
112  */
113 static int pinconfig_post_bind(struct udevice *dev)
114 {
115 	bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
116 	const char *name;
117 	ofnode node;
118 	int ret;
119 
120 	dev_for_each_subnode(node, dev) {
121 		if (pre_reloc_only &&
122 		    !ofnode_pre_reloc(node))
123 			continue;
124 		/*
125 		 * If this node has "compatible" property, this is not
126 		 * a pin configuration node, but a normal device. skip.
127 		 */
128 		ofnode_get_property(node, "compatible", &ret);
129 		if (ret >= 0)
130 			continue;
131 
132 		if (ret != -FDT_ERR_NOTFOUND)
133 			return ret;
134 
135 		name = ofnode_get_name(node);
136 		if (!name)
137 			return -EINVAL;
138 		ret = device_bind_driver_to_node(dev, "pinconfig", name,
139 						 node, NULL);
140 		if (ret)
141 			return ret;
142 	}
143 
144 	return 0;
145 }
146 
147 UCLASS_DRIVER(pinconfig) = {
148 	.id = UCLASS_PINCONFIG,
149 	.post_bind = pinconfig_post_bind,
150 	.name = "pinconfig",
151 };
152 
153 U_BOOT_DRIVER(pinconfig_generic) = {
154 	.name = "pinconfig",
155 	.id = UCLASS_PINCONFIG,
156 };
157 
158 #else
159 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
160 {
161 	return -ENODEV;
162 }
163 
164 static int pinconfig_post_bind(struct udevice *dev)
165 {
166 	return 0;
167 }
168 #endif
169 
170 /**
171  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
172  *
173  * @dev: peripheral device
174  * @return: 0 on success, or negative error code on failure
175  */
176 static int pinctrl_select_state_simple(struct udevice *dev)
177 {
178 	struct udevice *pctldev;
179 	struct pinctrl_ops *ops;
180 	int ret;
181 
182 	/*
183 	 * For simplicity, assume the first device of PINCTRL uclass
184 	 * is the correct one.  This is most likely OK as there is
185 	 * usually only one pinctrl device on the system.
186 	 */
187 	ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
188 	if (ret)
189 		return ret;
190 
191 	ops = pinctrl_get_ops(pctldev);
192 	if (!ops->set_state_simple) {
193 		dev_dbg(dev, "set_state_simple op missing\n");
194 		return -ENOSYS;
195 	}
196 
197 	return ops->set_state_simple(pctldev, dev);
198 }
199 
200 int pinctrl_select_state(struct udevice *dev, const char *statename)
201 {
202 	/*
203 	 * Try full-implemented pinctrl first.
204 	 * If it fails or is not implemented, try simple one.
205 	 */
206 	if (pinctrl_select_state_full(dev, statename))
207 		return pinctrl_select_state_simple(dev);
208 
209 	return 0;
210 }
211 
212 int pinctrl_request(struct udevice *dev, int func, int flags)
213 {
214 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
215 
216 	if (!ops->request)
217 		return -ENOSYS;
218 
219 	return ops->request(dev, func, flags);
220 }
221 
222 int pinctrl_request_noflags(struct udevice *dev, int func)
223 {
224 	return pinctrl_request(dev, func, 0);
225 }
226 
227 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
228 {
229 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
230 
231 	if (!ops->get_periph_id)
232 		return -ENOSYS;
233 
234 	return ops->get_periph_id(dev, periph);
235 }
236 
237 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
238 {
239 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
240 
241 	if (!ops->get_gpio_mux)
242 		return -ENOSYS;
243 
244 	return ops->get_gpio_mux(dev, banknum, index);
245 }
246 
247 /**
248  * pinconfig_post_bind() - post binding for PINCTRL uclass
249  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
250  *
251  * @dev: pinctrl device
252  * @return: 0 on success, or negative error code on failure
253  */
254 static int pinctrl_post_bind(struct udevice *dev)
255 {
256 	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
257 
258 	if (!ops) {
259 		dev_dbg(dev, "ops is not set.  Do not bind.\n");
260 		return -EINVAL;
261 	}
262 
263 	/*
264 	 * If set_state callback is set, we assume this pinctrl driver is the
265 	 * full implementation.  In this case, its child nodes should be bound
266 	 * so that peripheral devices can easily search in parent devices
267 	 * during later DT-parsing.
268 	 */
269 	if (ops->set_state)
270 		return pinconfig_post_bind(dev);
271 
272 	return 0;
273 }
274 
275 UCLASS_DRIVER(pinctrl) = {
276 	.id = UCLASS_PINCTRL,
277 	.post_bind = pinctrl_post_bind,
278 	.flags = DM_UC_FLAG_SEQ_ALIAS,
279 	.name = "pinctrl",
280 };
281