blob: d3cea11f76e5115513ed5661b45a578c4d2a705f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse821d182014-02-26 15:59:24 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glasse821d182014-02-26 15:59:24 -07004 */
5
6#include <common.h>
7#include <dm.h>
Patrick Delaunay187c45f2020-01-13 11:35:04 +01008#include <dm/device_compat.h>
Heiko Schocher39cb3402019-06-12 06:11:46 +02009#include <dm/device-internal.h>
10#include <dm/lists.h>
11#include <dm/uclass-internal.h>
Eric Nelson786e98d2016-04-24 16:32:40 -070012#include <dt-bindings/gpio/gpio.h>
Simon Glasse821d182014-02-26 15:59:24 -070013#include <errno.h>
Simon Glassd3322bb2015-01-05 20:05:28 -070014#include <fdtdec.h>
Simon Glass0f4517d2014-10-04 11:29:42 -060015#include <malloc.h>
Simon Glasse821d182014-02-26 15:59:24 -070016#include <asm/gpio.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090017#include <linux/bug.h>
Simon Glass43b2e1a2014-10-22 21:37:01 -060018#include <linux/ctype.h>
Simon Glasse821d182014-02-26 15:59:24 -070019
Simon Glass16e10402015-01-05 20:05:29 -070020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glasse821d182014-02-26 15:59:24 -070022/**
Patrick Delaunay758bba92020-01-13 11:35:01 +010023 * gpio_desc_init() - Initialize the GPIO descriptor
24 *
25 * @desc: GPIO descriptor to initialize
26 * @dev: GPIO device
27 * @offset: Offset of device GPIO
28 */
29static void gpio_desc_init(struct gpio_desc *desc,
30 struct udevice *dev,
31 uint offset)
32{
33 desc->dev = dev;
34 desc->offset = offset;
35 desc->flags = 0;
36}
37
38/**
Simon Glasse821d182014-02-26 15:59:24 -070039 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070040 *
41 * Convert the GPIO number to an entry in the list of GPIOs
42 * or GPIO blocks registered with the GPIO controller. Returns
43 * entry on success, NULL on error.
Simon Glassce555292015-01-05 20:05:27 -070044 *
45 * @gpio: The numeric representation of the GPIO
46 * @desc: Returns description (desc->flags will always be 0)
47 * @return 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070048 */
Simon Glassce555292015-01-05 20:05:27 -070049static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070050{
51 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020052 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070053 int ret;
54
55 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
56 dev;
57 ret = uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070058 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070059 if (gpio >= uc_priv->gpio_base &&
60 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay758bba92020-01-13 11:35:01 +010061 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glasse821d182014-02-26 15:59:24 -070062 return 0;
63 }
64 }
65
66 /* No such GPIO */
Simon Glassce555292015-01-05 20:05:27 -070067 return ret ? ret : -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070068}
69
Simon Glass215bcc72015-06-23 15:38:40 -060070int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070071{
Simon Glass43b2e1a2014-10-22 21:37:01 -060072 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020073 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -060074 ulong offset;
75 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -070076 int ret;
77
Simon Glass43b2e1a2014-10-22 21:37:01 -060078 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glasse821d182014-02-26 15:59:24 -070079 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
80 dev;
81 ret = uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -070082 int len;
83
Simon Glassde0977b2015-03-05 12:25:20 -070084 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -060085 if (numeric != -1) {
86 offset = numeric - uc_priv->gpio_base;
87 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -040088 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -060089 break;
90 }
91
Simon Glasse821d182014-02-26 15:59:24 -070092 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
93
Simon Glassd4acf632014-06-11 23:29:47 -060094 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -060095 if (!strict_strtoul(name + len, 10, &offset))
96 break;
Simon Glasse821d182014-02-26 15:59:24 -070097 }
98 }
99
Simon Glass43b2e1a2014-10-22 21:37:01 -0600100 if (!dev)
101 return ret ? ret : -EINVAL;
102
Patrick Delaunay758bba92020-01-13 11:35:01 +0100103 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600104
105 return 0;
106}
107
108int gpio_lookup_name(const char *name, struct udevice **devp,
109 unsigned int *offsetp, unsigned int *gpiop)
110{
111 struct gpio_desc desc;
112 int ret;
113
114 if (devp)
115 *devp = NULL;
116 ret = dm_gpio_lookup_name(name, &desc);
117 if (ret)
118 return ret;
119
Simon Glass43b2e1a2014-10-22 21:37:01 -0600120 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600121 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600122 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600123 *offsetp = desc.offset;
124 if (gpiop) {
125 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
126
127 *gpiop = uc_priv->gpio_base + desc.offset;
128 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600129
130 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700131}
132
Simon Glass12faa022017-05-18 20:09:18 -0600133int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
134 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700135{
136 if (args->args_count < 1)
137 return -EINVAL;
138
139 desc->offset = args->args[0];
140
141 if (args->args_count < 2)
142 return 0;
143
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100144 desc->flags = 0;
Eric Nelson786e98d2016-04-24 16:32:40 -0700145 if (args->args[1] & GPIO_ACTIVE_LOW)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100146 desc->flags |= GPIOD_ACTIVE_LOW;
Eric Nelson786e98d2016-04-24 16:32:40 -0700147
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100148 /*
149 * need to test 2 bits for gpio output binding:
150 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
151 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
152 */
153 if (args->args[1] & GPIO_SINGLE_ENDED) {
154 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
155 desc->flags |= GPIOD_OPEN_DRAIN;
156 else
157 desc->flags |= GPIOD_OPEN_SOURCE;
158 }
159
160 if (args->args[1] & GPIO_PULL_UP)
161 desc->flags |= GPIOD_PULL_UP;
162
163 if (args->args[1] & GPIO_PULL_DOWN)
164 desc->flags |= GPIOD_PULL_DOWN;
165
Eric Nelson786e98d2016-04-24 16:32:40 -0700166 return 0;
167}
168
Simon Glass16e10402015-01-05 20:05:29 -0700169static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600170 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700171{
172 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
173
Eric Nelson786e98d2016-04-24 16:32:40 -0700174 if (ops->xlate)
175 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700176 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700177 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700178}
179
Heiko Schocher58e4c382019-07-17 06:59:51 +0200180#if defined(CONFIG_GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200181
182struct gpio_hog_priv {
183 struct gpio_desc gpiod;
184};
185
186struct gpio_hog_data {
187 int gpiod_flags;
188 int value;
189 u32 val[2];
190};
191
192static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
193{
194 struct gpio_hog_data *plat = dev_get_platdata(dev);
195 const char *nodename;
196 int ret;
197
198 plat->value = 0;
199 if (dev_read_bool(dev, "input")) {
200 plat->gpiod_flags = GPIOD_IS_IN;
201 } else if (dev_read_bool(dev, "output-high")) {
202 plat->value = 1;
203 plat->gpiod_flags = GPIOD_IS_OUT;
204 } else if (dev_read_bool(dev, "output-low")) {
205 plat->gpiod_flags = GPIOD_IS_OUT;
206 } else {
207 printf("%s: missing gpio-hog state.\n", __func__);
208 return -EINVAL;
209 }
210 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
211 if (ret) {
212 printf("%s: wrong gpios property, 2 values needed %d\n",
213 __func__, ret);
214 return ret;
215 }
216 nodename = dev_read_string(dev, "line-name");
Heiko Schocher58e4c382019-07-17 06:59:51 +0200217 if (nodename)
218 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200219
220 return 0;
221}
222
223static int gpio_hog_probe(struct udevice *dev)
224{
225 struct gpio_hog_data *plat = dev_get_platdata(dev);
226 struct gpio_hog_priv *priv = dev_get_priv(dev);
227 int ret;
228
229 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
230 plat->val[0], plat->gpiod_flags,
231 plat->val[1], &priv->gpiod);
232 if (ret < 0) {
233 debug("%s: node %s could not get gpio.\n", __func__,
234 dev->name);
235 return ret;
236 }
Heiko Schocher58e4c382019-07-17 06:59:51 +0200237
238 if (plat->gpiod_flags == GPIOD_IS_OUT) {
239 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
240 if (ret < 0) {
241 debug("%s: node %s could not set gpio.\n", __func__,
242 dev->name);
243 return ret;
244 }
245 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200246
247 return 0;
248}
249
250int gpio_hog_probe_all(void)
251{
252 struct udevice *dev;
253 int ret;
Heiko Schocher58e4c382019-07-17 06:59:51 +0200254 int retval = 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200255
256 for (uclass_first_device(UCLASS_NOP, &dev);
257 dev;
258 uclass_find_next_device(&dev)) {
259 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
260 ret = device_probe(dev);
Heiko Schocher58e4c382019-07-17 06:59:51 +0200261 if (ret) {
262 printf("Failed to probe device %s err: %d\n",
263 dev->name, ret);
264 retval = ret;
265 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200266 }
267 }
268
Heiko Schocher58e4c382019-07-17 06:59:51 +0200269 return retval;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200270}
271
Heiko Schocher58e4c382019-07-17 06:59:51 +0200272int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200273{
274 struct udevice *dev;
275
Heiko Schocher58e4c382019-07-17 06:59:51 +0200276 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200277 gpio_hog_probe_all();
278 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
279 struct gpio_hog_priv *priv = dev_get_priv(dev);
280
Heiko Schocher58e4c382019-07-17 06:59:51 +0200281 *desc = &priv->gpiod;
282 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200283 }
284
Heiko Schocher58e4c382019-07-17 06:59:51 +0200285 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200286}
287
288U_BOOT_DRIVER(gpio_hog) = {
289 .name = "gpio_hog",
290 .id = UCLASS_NOP,
291 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
292 .probe = gpio_hog_probe,
293 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
294 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
295};
296#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200297int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200298{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200299 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200300}
301#endif
302
Simon Glass047cdb32015-06-23 15:38:41 -0600303int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700304{
305 struct udevice *dev = desc->dev;
306 struct gpio_dev_priv *uc_priv;
307 char *str;
308 int ret;
309
Simon Glassde0977b2015-03-05 12:25:20 -0700310 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700311 if (uc_priv->name[desc->offset])
312 return -EBUSY;
313 str = strdup(label);
314 if (!str)
315 return -ENOMEM;
316 if (gpio_get_ops(dev)->request) {
317 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
318 if (ret) {
319 free(str);
320 return ret;
321 }
322 }
323 uc_priv->name[desc->offset] = str;
324
325 return 0;
326}
327
Simon Glass16e10402015-01-05 20:05:29 -0700328static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
329{
Simon Glass7611ac62019-09-25 08:56:27 -0600330#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700331 va_list args;
332 char buf[40];
333
334 va_start(args, fmt);
335 vscnprintf(buf, sizeof(buf), fmt, args);
336 va_end(args);
337 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700338#else
339 return dm_gpio_request(desc, fmt);
340#endif
Simon Glass16e10402015-01-05 20:05:29 -0700341}
342
Simon Glasse821d182014-02-26 15:59:24 -0700343/**
344 * gpio_request() - [COMPAT] Request GPIO
345 * gpio: GPIO number
346 * label: Name for the requested GPIO
347 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600348 * The label is copied and allocated so the caller does not need to keep
349 * the pointer around.
350 *
Simon Glasse821d182014-02-26 15:59:24 -0700351 * This function implements the API that's compatible with current
352 * GPIO API used in U-Boot. The request is forwarded to particular
353 * GPIO driver. Returns 0 on success, negative value on error.
354 */
355int gpio_request(unsigned gpio, const char *label)
356{
Simon Glassce555292015-01-05 20:05:27 -0700357 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700358 int ret;
359
Simon Glassce555292015-01-05 20:05:27 -0700360 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700361 if (ret)
362 return ret;
363
Simon Glassce555292015-01-05 20:05:27 -0700364 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700365}
366
367/**
Simon Glass1b27d602014-10-04 11:29:49 -0600368 * gpio_requestf() - [COMPAT] Request GPIO
369 * @gpio: GPIO number
370 * @fmt: Format string for the requested GPIO
371 * @...: Arguments for the printf() format string
372 *
373 * This function implements the API that's compatible with current
374 * GPIO API used in U-Boot. The request is forwarded to particular
375 * GPIO driver. Returns 0 on success, negative value on error.
376 */
377int gpio_requestf(unsigned gpio, const char *fmt, ...)
378{
Simon Glass7611ac62019-09-25 08:56:27 -0600379#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600380 va_list args;
381 char buf[40];
382
383 va_start(args, fmt);
384 vscnprintf(buf, sizeof(buf), fmt, args);
385 va_end(args);
386 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700387#else
388 return gpio_request(gpio, fmt);
389#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600390}
391
Simon Glassce555292015-01-05 20:05:27 -0700392int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700393{
Simon Glass0f4517d2014-10-04 11:29:42 -0600394 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700395 int ret;
396
Simon Glassde0977b2015-03-05 12:25:20 -0700397 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600398 if (!uc_priv->name[offset])
399 return -ENXIO;
Simon Glassb3a47542020-02-04 20:15:17 -0700400 if (gpio_get_ops(dev)->rfree) {
401 ret = gpio_get_ops(dev)->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600402 if (ret)
403 return ret;
404 }
405
406 free(uc_priv->name[offset]);
407 uc_priv->name[offset] = NULL;
408
409 return 0;
410}
411
Simon Glassce555292015-01-05 20:05:27 -0700412/**
413 * gpio_free() - [COMPAT] Relinquish GPIO
414 * gpio: GPIO number
415 *
416 * This function implements the API that's compatible with current
417 * GPIO API used in U-Boot. The request is forwarded to particular
418 * GPIO driver. Returns 0 on success, negative value on error.
419 */
420int gpio_free(unsigned gpio)
421{
422 struct gpio_desc desc;
423 int ret;
424
425 ret = gpio_to_device(gpio, &desc);
426 if (ret)
427 return ret;
428
429 return _dm_gpio_free(desc.dev, desc.offset);
430}
431
Simon Glassfd838972016-03-06 19:27:51 -0700432static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600433{
Simon Glass230c1432015-07-02 18:16:16 -0600434 struct gpio_dev_priv *uc_priv;
435
436 if (!dm_gpio_is_valid(desc))
437 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600438
Simon Glass230c1432015-07-02 18:16:16 -0600439 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700440 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600441 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700442 desc->dev->name, func,
443 uc_priv->bank_name ? uc_priv->bank_name : "",
444 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600445 return -EBUSY;
446 }
447
448 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700449}
450
451/**
452 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
453 * gpio: GPIO number
454 *
455 * This function implements the API that's compatible with current
456 * GPIO API used in U-Boot. The request is forwarded to particular
457 * GPIO driver. Returns 0 on success, negative value on error.
458 */
459int gpio_direction_input(unsigned gpio)
460{
Simon Glassce555292015-01-05 20:05:27 -0700461 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700462 int ret;
463
Simon Glassce555292015-01-05 20:05:27 -0700464 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700465 if (ret)
466 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700467 ret = check_reserved(&desc, "dir_input");
468 if (ret)
469 return ret;
Simon Glasse821d182014-02-26 15:59:24 -0700470
Simon Glassce555292015-01-05 20:05:27 -0700471 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glasse821d182014-02-26 15:59:24 -0700472}
473
474/**
475 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
476 * gpio: GPIO number
477 * value: Logical value to be set on the GPIO pin
478 *
479 * This function implements the API that's compatible with current
480 * GPIO API used in U-Boot. The request is forwarded to particular
481 * GPIO driver. Returns 0 on success, negative value on error.
482 */
483int gpio_direction_output(unsigned gpio, int value)
484{
Simon Glassce555292015-01-05 20:05:27 -0700485 struct gpio_desc desc;
486 int ret;
487
488 ret = gpio_to_device(gpio, &desc);
489 if (ret)
490 return ret;
491 ret = check_reserved(&desc, "dir_output");
492 if (ret)
493 return ret;
494
495 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
496 desc.offset, value);
497}
498
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100499static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700500{
501 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100502
503 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
504
505 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
506}
507
508int dm_gpio_get_value(const struct gpio_desc *desc)
509{
Simon Glassce555292015-01-05 20:05:27 -0700510 int ret;
511
512 ret = check_reserved(desc, "get_value");
513 if (ret)
514 return ret;
515
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100516 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700517}
518
Simon Glassfd838972016-03-06 19:27:51 -0700519int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700520{
521 int ret;
522
523 ret = check_reserved(desc, "set_value");
524 if (ret)
525 return ret;
526
527 if (desc->flags & GPIOD_ACTIVE_LOW)
528 value = !value;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200529
530 /*
531 * Emulate open drain by not actively driving the line high or
532 * Emulate open source by not actively driving the line low
533 */
534 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
535 (desc->flags & GPIOD_OPEN_SOURCE && !value))
536 return gpio_get_ops(desc->dev)->direction_input(desc->dev,
537 desc->offset);
538 else if (desc->flags & GPIOD_OPEN_DRAIN ||
539 desc->flags & GPIOD_OPEN_SOURCE)
540 return gpio_get_ops(desc->dev)->direction_output(desc->dev,
541 desc->offset,
542 value);
543
Simon Glassce555292015-01-05 20:05:27 -0700544 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
545 return 0;
546}
547
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100548/* check dir flags invalid configuration */
549static int check_dir_flags(ulong flags)
550{
551 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
552 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
553 __func__, flags);
554 return -EINVAL;
555 }
556
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100557 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
558 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
559 __func__, flags);
560 return -EINVAL;
561 }
562
563 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
564 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
565 __func__, flags);
566 return -EINVAL;
567 }
568
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100569 return 0;
570}
571
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100572static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700573{
574 struct udevice *dev = desc->dev;
575 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100576 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100577 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700578
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100579 ret = check_dir_flags(flags);
580 if (ret) {
581 dev_dbg(dev,
582 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
583 desc->dev->name,
584 uc_priv->bank_name ? uc_priv->bank_name : "",
585 desc->offset, flags);
586
587 return ret;
588 }
589
Patrick Delaunay684326f2020-01-13 11:35:09 +0100590 /* GPIOD_ are directly managed by driver in set_dir_flags*/
591 if (ops->set_dir_flags) {
592 ret = ops->set_dir_flags(dev, desc->offset, flags);
593 } else {
594 if (flags & GPIOD_IS_OUT) {
595 ret = ops->direction_output(dev, desc->offset,
596 GPIOD_FLAGS_OUTPUT(flags));
597 } else if (flags & GPIOD_IS_IN) {
598 ret = ops->direction_input(dev, desc->offset);
599 }
Simon Glassce555292015-01-05 20:05:27 -0700600 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100601
602 return ret;
603}
604
605int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
606{
607 int ret;
608
609 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700610 if (ret)
611 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100612
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100613 /* combine the requested flags (for IN/OUT) and the descriptor flags */
614 flags |= desc->flags;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100615 ret = _dm_gpio_set_dir_flags(desc, flags);
Simon Glassce555292015-01-05 20:05:27 -0700616
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100617 /* update the descriptor flags */
618 if (ret)
619 desc->flags = flags;
620
621 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700622}
623
624int dm_gpio_set_dir(struct gpio_desc *desc)
625{
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100626 int ret;
627
628 ret = check_reserved(desc, "set_dir");
629 if (ret)
630 return ret;
631
632 return _dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glasse821d182014-02-26 15:59:24 -0700633}
634
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100635int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
636{
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100637 struct udevice *dev = desc->dev;
638 int ret, value;
639 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100640 ulong dir_flags;
641
642 ret = check_reserved(desc, "get_dir_flags");
643 if (ret)
644 return ret;
645
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100646 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
647 if (ops->get_dir_flags) {
648 ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
649 if (ret)
650 return ret;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100651
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100652 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
653 value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
654 if (desc->flags & GPIOD_ACTIVE_LOW)
655 value = !value;
656 dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
657 dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
658 if (value)
659 dir_flags |= GPIOD_IS_OUT_ACTIVE;
660 } else {
661 dir_flags = desc->flags;
662 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
663 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
664 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
665 dir_flags |= GPIOD_IS_OUT_ACTIVE;
666 }
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100667 *flags = dir_flags;
668
669 return 0;
670}
671
Simon Glasse821d182014-02-26 15:59:24 -0700672/**
673 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
674 * gpio: GPIO number
675 *
676 * This function implements the API that's compatible with current
677 * GPIO API used in U-Boot. The request is forwarded to particular
678 * GPIO driver. Returns the value of the GPIO pin, or negative value
679 * on error.
680 */
681int gpio_get_value(unsigned gpio)
682{
Simon Glasse821d182014-02-26 15:59:24 -0700683 int ret;
684
Simon Glassce555292015-01-05 20:05:27 -0700685 struct gpio_desc desc;
686
687 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700688 if (ret)
689 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700690 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700691}
692
693/**
694 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
695 * gpio: GPIO number
696 * value: Logical value to be set on the GPIO pin.
697 *
698 * This function implements the API that's compatible with current
699 * GPIO API used in U-Boot. The request is forwarded to particular
700 * GPIO driver. Returns 0 on success, negative value on error.
701 */
702int gpio_set_value(unsigned gpio, int value)
703{
Simon Glassce555292015-01-05 20:05:27 -0700704 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700705 int ret;
706
Simon Glassce555292015-01-05 20:05:27 -0700707 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700708 if (ret)
709 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700710 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700711}
712
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200713const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700714{
715 struct gpio_dev_priv *priv;
716
717 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700718 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700719 assert(priv);
720
721 *bit_count = priv->gpio_count;
722 return priv->bank_name;
723}
724
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600725static const char * const gpio_function[GPIOF_COUNT] = {
726 "input",
727 "output",
728 "unused",
729 "unknown",
730 "func",
731};
732
Masahiro Yamada286c2522017-06-22 16:50:25 +0900733static int get_function(struct udevice *dev, int offset, bool skip_unused,
734 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600735{
Simon Glassde0977b2015-03-05 12:25:20 -0700736 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600737 struct dm_gpio_ops *ops = gpio_get_ops(dev);
738
739 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
740 if (!device_active(dev))
741 return -ENODEV;
742 if (offset < 0 || offset >= uc_priv->gpio_count)
743 return -EINVAL;
744 if (namep)
745 *namep = uc_priv->name[offset];
746 if (skip_unused && !uc_priv->name[offset])
747 return GPIOF_UNUSED;
748 if (ops->get_function) {
749 int ret;
750
751 ret = ops->get_function(dev, offset);
752 if (ret < 0)
753 return ret;
754 if (ret >= ARRAY_SIZE(gpio_function))
755 return -ENODATA;
756 return ret;
757 }
758
759 return GPIOF_UNKNOWN;
760}
761
762int gpio_get_function(struct udevice *dev, int offset, const char **namep)
763{
764 return get_function(dev, offset, true, namep);
765}
766
767int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
768{
769 return get_function(dev, offset, false, namep);
770}
771
Simon Glass6b1ef592014-10-04 11:29:44 -0600772int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
773{
774 struct dm_gpio_ops *ops = gpio_get_ops(dev);
775 struct gpio_dev_priv *priv;
776 char *str = buf;
777 int func;
778 int ret;
779 int len;
780
781 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
782
783 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700784 priv = dev_get_uclass_priv(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600785 ret = gpio_get_raw_function(dev, offset, NULL);
786 if (ret < 0)
787 return ret;
788 func = ret;
789 len = snprintf(str, buffsize, "%s%d: %s",
790 priv->bank_name ? priv->bank_name : "",
791 offset, gpio_function[func]);
792 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
793 func == GPIOF_UNUSED) {
794 const char *label;
795 bool used;
796
797 ret = ops->get_value(dev, offset);
798 if (ret < 0)
799 return ret;
800 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
801 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
802 ret,
803 used ? 'x' : ' ',
804 used ? " " : "",
805 label ? label : "");
806 }
807
808 return 0;
809}
810
Simon Glassbef54db2015-04-14 21:03:20 -0600811int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
812{
813 int i, ret;
814 int gpio;
815
816 for (i = 0; i < 32; i++) {
817 gpio = gpio_num_array[i];
818 if (gpio == -1)
819 break;
820 ret = gpio_requestf(gpio, fmt, i);
821 if (ret)
822 goto err;
823 ret = gpio_direction_input(gpio);
824 if (ret) {
825 gpio_free(gpio);
826 goto err;
827 }
828 }
829
830 return 0;
831err:
832 for (i--; i >= 0; i--)
833 gpio_free(gpio_num_array[i]);
834
835 return ret;
836}
837
Simon Glass2c97a8f2014-11-10 18:00:21 -0700838/*
839 * get a number comprised of multiple GPIO values. gpio_num_array points to
840 * the array of gpio pin numbers to scan, terminated by -1.
841 */
Simon Glassbef54db2015-04-14 21:03:20 -0600842int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700843{
844 int gpio;
845 unsigned bitmask = 1;
846 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600847 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700848
849 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600850 ((gpio = *gpio_list++) != -1)) {
851 ret = gpio_get_value(gpio);
852 if (ret < 0)
853 return ret;
854 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700855 vector |= bitmask;
856 bitmask <<= 1;
857 }
Simon Glassbef54db2015-04-14 21:03:20 -0600858
Simon Glass2c97a8f2014-11-10 18:00:21 -0700859 return vector;
860}
861
Simon Glassfd838972016-03-06 19:27:51 -0700862int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -0700863{
864 unsigned bitmask = 1;
865 unsigned vector = 0;
866 int ret, i;
867
868 for (i = 0; i < count; i++) {
869 ret = dm_gpio_get_value(&desc_list[i]);
870 if (ret < 0)
871 return ret;
872 else if (ret)
873 vector |= bitmask;
874 bitmask <<= 1;
875 }
876
877 return vector;
878}
879
Heiko Schocher58e4c382019-07-17 06:59:51 +0200880/**
881 * gpio_request_tail: common work for requesting a gpio.
882 *
883 * ret: return value from previous work in function which calls
884 * this function.
885 * This seems bogus (why calling this function instead not
886 * calling it and end caller function instead?).
887 * Because on error in caller function we want to set some
888 * default values in gpio desc and have a common error
889 * debug message, which provides this function.
890 * nodename: Name of node for which gpio gets requested
891 * used for gpio label name.
892 * args: pointer to output arguments structure
893 * list_name: Name of GPIO list
894 * used for gpio label name.
895 * index: gpio index in gpio list
896 * used for gpio label name.
897 * desc: pointer to gpio descriptor, filled from this
898 * function.
899 * flags: gpio flags to use.
900 * add_index: should index added to gpio label name
901 * gpio_dev: pointer to gpio device from which the gpio
902 * will be requested. If NULL try to get the
903 * gpio device with uclass_get_device_by_ofnode()
904 *
905 * return: In error case this function sets default values in
906 * gpio descriptor, also emmits a debug message.
907 * On success it returns 0 else the error code from
908 * function calls, or the error code passed through
909 * ret to this function.
910 *
911 */
Heiko Schocher39cb3402019-06-12 06:11:46 +0200912static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -0600913 struct ofnode_phandle_args *args,
914 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200915 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +0200916 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -0700917{
Patrick Delaunay758bba92020-01-13 11:35:01 +0100918 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -0600919 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -0700920 goto err;
Simon Glass16e10402015-01-05 20:05:29 -0700921
Heiko Schocher39cb3402019-06-12 06:11:46 +0200922 if (!desc->dev) {
923 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
924 &desc->dev);
925 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +0200926 debug("%s: uclass_get_device_by_ofnode failed\n",
927 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200928 goto err;
929 }
Simon Glass16e10402015-01-05 20:05:29 -0700930 }
Simon Glass12faa022017-05-18 20:09:18 -0600931 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -0700932 if (ret) {
933 debug("%s: gpio_find_and_xlate failed\n", __func__);
934 goto err;
935 }
936 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +0200937 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -0700938 if (ret) {
939 debug("%s: dm_gpio_requestf failed\n", __func__);
940 goto err;
941 }
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100942 ret = dm_gpio_set_dir_flags(desc, flags);
Simon Glass16e10402015-01-05 20:05:29 -0700943 if (ret) {
944 debug("%s: dm_gpio_set_dir failed\n", __func__);
945 goto err;
946 }
947
948 return 0;
949err:
950 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +0200951 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -0700952 return ret;
953}
954
Simon Glass1d9af1f2017-05-30 21:47:09 -0600955static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
956 int index, struct gpio_desc *desc,
957 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -0600958{
959 struct ofnode_phandle_args args;
960 int ret;
961
Simon Glass1d9af1f2017-05-30 21:47:09 -0600962 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
963 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -0600964
Heiko Schocher39cb3402019-06-12 06:11:46 +0200965 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
966 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -0600967}
968
Simon Glass1d9af1f2017-05-30 21:47:09 -0600969int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -0700970 struct gpio_desc *desc, int flags)
971{
Simon Glass1d9af1f2017-05-30 21:47:09 -0600972 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
973 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -0700974}
975
Simon Glass1d9af1f2017-05-30 21:47:09 -0600976int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -0700977 struct gpio_desc *desc, int flags)
978{
Simon Glass1d9af1f2017-05-30 21:47:09 -0600979 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200980 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -0600981 int ret;
982
983 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
984 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200985 node = dev_ofnode(dev);
986 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
987 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -0700988}
989
Simon Glass1d9af1f2017-05-30 21:47:09 -0600990int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -0700991 struct gpio_desc *desc, int max_count,
992 int flags)
993{
994 int count;
995 int ret;
996
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +0200997 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -0600998 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -0700999 &desc[count], flags, true);
1000 if (ret == -ENOENT)
1001 break;
1002 else if (ret)
1003 goto err;
1004 }
1005
1006 /* We ran out of GPIOs in the list */
1007 return count;
1008
1009err:
1010 gpio_free_list_nodev(desc, count - 1);
1011
1012 return ret;
1013}
1014
1015int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1016 struct gpio_desc *desc, int max_count,
1017 int flags)
1018{
1019 /*
1020 * This isn't ideal since we don't use dev->name in the debug()
1021 * calls in gpio_request_by_name(), but we can do this until
1022 * gpio_request_list_by_name_nodev() can be dropped.
1023 */
Simon Glass1d9af1f2017-05-30 21:47:09 -06001024 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1025 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -07001026}
1027
1028int gpio_get_list_count(struct udevice *dev, const char *list_name)
1029{
1030 int ret;
1031
Simon Glassdd79d6e2017-01-17 16:52:55 -07001032 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass16e10402015-01-05 20:05:29 -07001033 list_name, "#gpio-cells", 0, -1,
1034 NULL);
1035 if (ret) {
1036 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1037 __func__, dev->name, list_name, ret);
1038 }
1039
1040 return ret;
1041}
1042
1043int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1044{
1045 /* For now, we don't do any checking of dev */
1046 return _dm_gpio_free(desc->dev, desc->offset);
1047}
1048
1049int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1050{
1051 int i;
1052
1053 /* For now, we don't do any checking of dev */
1054 for (i = 0; i < count; i++)
1055 dm_gpio_free(dev, &desc[i]);
1056
1057 return 0;
1058}
1059
1060int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1061{
1062 return gpio_free_list(NULL, desc, count);
1063}
1064
Simon Glasse821d182014-02-26 15:59:24 -07001065/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -06001066static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -07001067{
1068 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001069 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -07001070 struct uclass *uc;
1071 unsigned base;
1072 int ret;
1073
1074 ret = uclass_get(UCLASS_GPIO, &uc);
1075 if (ret)
1076 return ret;
1077
1078 /* Ensure that we have a base for each bank */
1079 base = 0;
1080 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -06001081 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -07001082 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001083 uc_priv->gpio_base = base;
1084 base += uc_priv->gpio_count;
1085 }
1086 }
1087
1088 return 0;
1089}
1090
Simon Glassfd838972016-03-06 19:27:51 -07001091int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -06001092{
1093 struct udevice *dev = desc->dev;
1094 struct gpio_dev_priv *uc_priv;
1095
1096 if (!dev)
1097 return -1;
1098 uc_priv = dev->uclass_priv;
1099
1100 return uc_priv->gpio_base + desc->offset;
1101}
1102
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001103static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001104{
Simon Glassde0977b2015-03-05 12:25:20 -07001105 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001106
1107 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1108 if (!uc_priv->name)
1109 return -ENOMEM;
1110
1111 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001112}
1113
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001114static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001115{
Simon Glassde0977b2015-03-05 12:25:20 -07001116 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001117 int i;
1118
1119 for (i = 0; i < uc_priv->gpio_count; i++) {
1120 if (uc_priv->name[i])
1121 free(uc_priv->name[i]);
1122 }
1123 free(uc_priv->name);
1124
1125 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001126}
1127
Heiko Schocher39cb3402019-06-12 06:11:46 +02001128int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1129 char *list_name, int index, int flags,
1130 int dtflags, struct gpio_desc *desc)
1131{
1132 struct ofnode_phandle_args args;
1133
1134 args.node = ofnode_null();
1135 args.args_count = 2;
1136 args.args[0] = index;
1137 args.args[1] = dtflags;
1138
1139 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1140 flags, 0, dev);
1141}
1142
Michal Simek5f7202c2018-07-12 12:42:27 +02001143static int gpio_post_bind(struct udevice *dev)
1144{
Heiko Schocher39cb3402019-06-12 06:11:46 +02001145 struct udevice *child;
1146 ofnode node;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001147
Michal Simek5f7202c2018-07-12 12:42:27 +02001148#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1149 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1150 static int reloc_done;
1151
1152 if (!reloc_done) {
1153 if (ops->request)
1154 ops->request += gd->reloc_off;
Simon Glassb3a47542020-02-04 20:15:17 -07001155 if (ops->rfree)
1156 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001157 if (ops->direction_input)
1158 ops->direction_input += gd->reloc_off;
1159 if (ops->direction_output)
1160 ops->direction_output += gd->reloc_off;
1161 if (ops->get_value)
1162 ops->get_value += gd->reloc_off;
1163 if (ops->set_value)
1164 ops->set_value += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001165 if (ops->get_function)
1166 ops->get_function += gd->reloc_off;
1167 if (ops->xlate)
1168 ops->xlate += gd->reloc_off;
Patrick Delaunay684326f2020-01-13 11:35:09 +01001169 if (ops->set_dir_flags)
1170 ops->set_dir_flags += gd->reloc_off;
Patrick Delaunayfbacd622020-01-13 11:35:08 +01001171 if (ops->get_dir_flags)
1172 ops->get_dir_flags += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001173
1174 reloc_done++;
1175 }
1176#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001177
Heiko Schocher58e4c382019-07-17 06:59:51 +02001178 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1179 dev_for_each_subnode(node, dev) {
1180 if (ofnode_read_bool(node, "gpio-hog")) {
1181 const char *name = ofnode_get_name(node);
1182 int ret;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001183
Heiko Schocher58e4c382019-07-17 06:59:51 +02001184 ret = device_bind_driver_to_node(dev,
1185 "gpio_hog",
1186 name, node,
1187 &child);
1188 if (ret)
1189 return ret;
1190 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001191 }
1192 }
Michal Simek5f7202c2018-07-12 12:42:27 +02001193 return 0;
1194}
1195
Simon Glasse821d182014-02-26 15:59:24 -07001196UCLASS_DRIVER(gpio) = {
1197 .id = UCLASS_GPIO,
1198 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301199 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001200 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001201 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001202 .pre_remove = gpio_pre_remove,
1203 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1204};