1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 */
5
6 #include <common.h>
7 #include <malloc.h>
8 #include <asm/global_data.h>
9 #include <dm/device_compat.h>
10 #include <linux/libfdt.h>
11 #include <linux/err.h>
12 #include <linux/list.h>
13 #include <dm.h>
14 #include <dm/lists.h>
15 #include <dm/pinctrl.h>
16 #include <dm/util.h>
17 #include <dm/of_access.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
22 /**
23 * pinctrl_config_one() - apply pinctrl settings for a single node
24 *
25 * @config: pin configuration node
26 * @return: 0 on success, or negative error code on failure
27 */
pinctrl_config_one(struct udevice * config)28 static int pinctrl_config_one(struct udevice *config)
29 {
30 struct udevice *pctldev;
31 const struct pinctrl_ops *ops;
32
33 pctldev = config;
34 for (;;) {
35 pctldev = dev_get_parent(pctldev);
36 if (!pctldev) {
37 dev_err(config, "could not find pctldev\n");
38 return -EINVAL;
39 }
40 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
41 break;
42 }
43
44 ops = pinctrl_get_ops(pctldev);
45 return ops->set_state(pctldev, config);
46 }
47
48 /**
49 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
50 *
51 * @dev: peripheral device
52 * @statename: state name, like "default"
53 * @return: 0 on success, or negative error code on failure
54 */
pinctrl_select_state_full(struct udevice * dev,const char * statename)55 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
56 {
57 char propname[32]; /* long enough */
58 const fdt32_t *list;
59 uint32_t phandle;
60 struct udevice *config;
61 int state, size, i, ret;
62
63 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
64 if (state < 0) {
65 char *end;
66 /*
67 * If statename is not found in "pinctrl-names",
68 * assume statename is just the integer state ID.
69 */
70 state = simple_strtoul(statename, &end, 10);
71 if (*end)
72 return -EINVAL;
73 }
74
75 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
76 list = dev_read_prop(dev, propname, &size);
77 if (!list)
78 return -EINVAL;
79
80 size /= sizeof(*list);
81 for (i = 0; i < size; i++) {
82 phandle = fdt32_to_cpu(*list++);
83 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
84 &config);
85 if (ret) {
86 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
87 __func__, ret);
88 continue;
89 }
90
91 ret = pinctrl_config_one(config);
92 if (ret) {
93 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
94 __func__, ret);
95 continue;
96 }
97 }
98
99 return 0;
100 }
101
102 /**
103 * pinconfig_post_bind() - post binding for PINCONFIG uclass
104 * Recursively bind its children as pinconfig devices.
105 *
106 * @dev: pinconfig device
107 * @return: 0 on success, or negative error code on failure
108 */
pinconfig_post_bind(struct udevice * dev)109 static int pinconfig_post_bind(struct udevice *dev)
110 {
111 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
112 const char *name;
113 ofnode node;
114 int ret;
115
116 if (!dev_has_ofnode(dev))
117 return 0;
118
119 dev_for_each_subnode(node, dev) {
120 if (pre_reloc_only &&
121 !ofnode_pre_reloc(node))
122 continue;
123 /*
124 * If this node has "compatible" property, this is not
125 * a pin configuration node, but a normal device. skip.
126 */
127 ofnode_get_property(node, "compatible", &ret);
128 if (ret >= 0)
129 continue;
130 /* If this node has "gpio-controller" property, skip */
131 if (ofnode_read_bool(node, "gpio-controller"))
132 continue;
133
134 if (ret != -FDT_ERR_NOTFOUND)
135 return ret;
136
137 name = ofnode_get_name(node);
138 if (!name)
139 return -EINVAL;
140 ret = device_bind_driver_to_node(dev, "pinconfig", name,
141 node, NULL);
142 if (ret)
143 return ret;
144 }
145
146 return 0;
147 }
148
149 UCLASS_DRIVER(pinconfig) = {
150 .id = UCLASS_PINCONFIG,
151 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
152 .post_bind = pinconfig_post_bind,
153 #endif
154 .name = "pinconfig",
155 };
156
157 U_BOOT_DRIVER(pinconfig_generic) = {
158 .name = "pinconfig",
159 .id = UCLASS_PINCONFIG,
160 };
161
162 #else
pinctrl_select_state_full(struct udevice * dev,const char * statename)163 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
164 {
165 return -ENODEV;
166 }
167
pinconfig_post_bind(struct udevice * dev)168 static int pinconfig_post_bind(struct udevice *dev)
169 {
170 return 0;
171 }
172 #endif
173
174 static int
pinctrl_gpio_get_pinctrl_and_offset(struct udevice * dev,unsigned offset,struct udevice ** pctldev,unsigned int * pin_selector)175 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
176 struct udevice **pctldev,
177 unsigned int *pin_selector)
178 {
179 struct ofnode_phandle_args args;
180 unsigned gpio_offset, pfc_base, pfc_pins;
181 int ret;
182
183 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
184 0, &args);
185 if (ret) {
186 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
187 __func__, ret);
188 return ret;
189 }
190
191 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
192 args.node, pctldev);
193 if (ret) {
194 dev_dbg(dev,
195 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
196 __func__, ret);
197 return ret;
198 }
199
200 gpio_offset = args.args[0];
201 pfc_base = args.args[1];
202 pfc_pins = args.args[2];
203
204 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
205 dev_dbg(dev,
206 "%s: GPIO can not be mapped to pincontrol pin\n",
207 __func__);
208 return -EINVAL;
209 }
210
211 offset -= gpio_offset;
212 offset += pfc_base;
213 *pin_selector = offset;
214
215 return 0;
216 }
217
218 /**
219 * pinctrl_gpio_request() - request a single pin to be used as GPIO
220 *
221 * @dev: GPIO peripheral device
222 * @offset: the GPIO pin offset from the GPIO controller
223 * @return: 0 on success, or negative error code on failure
224 */
pinctrl_gpio_request(struct udevice * dev,unsigned offset)225 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
226 {
227 const struct pinctrl_ops *ops;
228 struct udevice *pctldev;
229 unsigned int pin_selector;
230 int ret;
231
232 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
233 &pctldev, &pin_selector);
234 if (ret)
235 return ret;
236
237 ops = pinctrl_get_ops(pctldev);
238 assert(ops);
239 if (!ops->gpio_request_enable)
240 return -ENOSYS;
241
242 return ops->gpio_request_enable(pctldev, pin_selector);
243 }
244
245 /**
246 * pinctrl_gpio_free() - free a single pin used as GPIO
247 *
248 * @dev: GPIO peripheral device
249 * @offset: the GPIO pin offset from the GPIO controller
250 * @return: 0 on success, or negative error code on failure
251 */
pinctrl_gpio_free(struct udevice * dev,unsigned offset)252 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
253 {
254 const struct pinctrl_ops *ops;
255 struct udevice *pctldev;
256 unsigned int pin_selector;
257 int ret;
258
259 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
260 &pctldev, &pin_selector);
261 if (ret)
262 return ret;
263
264 ops = pinctrl_get_ops(pctldev);
265 assert(ops);
266 if (!ops->gpio_disable_free)
267 return -ENOSYS;
268
269 return ops->gpio_disable_free(pctldev, pin_selector);
270 }
271
272 /**
273 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
274 *
275 * @dev: peripheral device
276 * @return: 0 on success, or negative error code on failure
277 */
pinctrl_select_state_simple(struct udevice * dev)278 static int pinctrl_select_state_simple(struct udevice *dev)
279 {
280 struct udevice *pctldev;
281 struct pinctrl_ops *ops;
282 int ret;
283
284 /*
285 * For most system, there is only one pincontroller device. But in
286 * case of multiple pincontroller devices, probe the one with sequence
287 * number 0 (defined by alias) to avoid race condition.
288 */
289 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
290 if (ret)
291 /* if not found, get the first one */
292 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
293 if (ret)
294 return ret;
295
296 ops = pinctrl_get_ops(pctldev);
297 if (!ops->set_state_simple) {
298 dev_dbg(dev, "set_state_simple op missing\n");
299 return -ENOSYS;
300 }
301
302 return ops->set_state_simple(pctldev, dev);
303 }
304
pinctrl_select_state(struct udevice * dev,const char * statename)305 int pinctrl_select_state(struct udevice *dev, const char *statename)
306 {
307 /*
308 * Some device which is logical like mmc.blk, do not have
309 * a valid ofnode.
310 */
311 if (!dev_has_ofnode(dev))
312 return 0;
313 /*
314 * Try full-implemented pinctrl first.
315 * If it fails or is not implemented, try simple one.
316 */
317 if (pinctrl_select_state_full(dev, statename))
318 return pinctrl_select_state_simple(dev);
319
320 return 0;
321 }
322
pinctrl_request(struct udevice * dev,int func,int flags)323 int pinctrl_request(struct udevice *dev, int func, int flags)
324 {
325 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
326
327 if (!ops->request)
328 return -ENOSYS;
329
330 return ops->request(dev, func, flags);
331 }
332
pinctrl_request_noflags(struct udevice * dev,int func)333 int pinctrl_request_noflags(struct udevice *dev, int func)
334 {
335 return pinctrl_request(dev, func, 0);
336 }
337
pinctrl_get_periph_id(struct udevice * dev,struct udevice * periph)338 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
339 {
340 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
341
342 if (!ops->get_periph_id)
343 return -ENOSYS;
344
345 return ops->get_periph_id(dev, periph);
346 }
347
pinctrl_get_gpio_mux(struct udevice * dev,int banknum,int index)348 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
349 {
350 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
351
352 if (!ops->get_gpio_mux)
353 return -ENOSYS;
354
355 return ops->get_gpio_mux(dev, banknum, index);
356 }
357
pinctrl_get_pins_count(struct udevice * dev)358 int pinctrl_get_pins_count(struct udevice *dev)
359 {
360 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
361
362 if (!ops->get_pins_count)
363 return -ENOSYS;
364
365 return ops->get_pins_count(dev);
366 }
367
pinctrl_get_pin_name(struct udevice * dev,int selector,char * buf,int size)368 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
369 int size)
370 {
371 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
372
373 if (!ops->get_pin_name)
374 return -ENOSYS;
375
376 snprintf(buf, size, ops->get_pin_name(dev, selector));
377
378 return 0;
379 }
380
pinctrl_get_pin_muxing(struct udevice * dev,int selector,char * buf,int size)381 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
382 int size)
383 {
384 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
385
386 if (!ops->get_pin_muxing)
387 return -ENOSYS;
388
389 return ops->get_pin_muxing(dev, selector, buf, size);
390 }
391
392 /**
393 * pinconfig_post_bind() - post binding for PINCTRL uclass
394 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
395 *
396 * @dev: pinctrl device
397 * @return: 0 on success, or negative error code on failure
398 */
pinctrl_post_bind(struct udevice * dev)399 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
400 {
401 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
402
403 if (!ops) {
404 dev_dbg(dev, "ops is not set. Do not bind.\n");
405 return -EINVAL;
406 }
407
408 /*
409 * If set_state callback is set, we assume this pinctrl driver is the
410 * full implementation. In this case, its child nodes should be bound
411 * so that peripheral devices can easily search in parent devices
412 * during later DT-parsing.
413 */
414 if (ops->set_state)
415 return pinconfig_post_bind(dev);
416
417 return 0;
418 }
419
420 UCLASS_DRIVER(pinctrl) = {
421 .id = UCLASS_PINCTRL,
422 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
423 .post_bind = pinctrl_post_bind,
424 #endif
425 .flags = DM_UC_FLAG_SEQ_ALIAS,
426 .name = "pinctrl",
427 };
428