blob: 757ab7106ee33d3fe03b9d3345fc4d9be8c60c52 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass96495d92014-02-26 15:59:24 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass96495d92014-02-26 15:59:24 -07004 */
5
6#include <common.h>
7#include <dm.h>
Patrick Delaunay4292fb12020-01-13 11:35:04 +01008#include <dm/device_compat.h>
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02009#include <dm/device-internal.h>
10#include <dm/lists.h>
11#include <dm/uclass-internal.h>
Eric Nelson6c880b72016-04-24 16:32:40 -070012#include <dt-bindings/gpio/gpio.h>
Simon Glass96495d92014-02-26 15:59:24 -070013#include <errno.h>
Simon Glass0dac4d52015-01-05 20:05:28 -070014#include <fdtdec.h>
Simon Glassb892d122014-10-04 11:29:42 -060015#include <malloc.h>
Simon Glass96495d92014-02-26 15:59:24 -070016#include <asm/gpio.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090017#include <linux/bug.h>
Simon Glassfe1ef502014-10-22 21:37:01 -060018#include <linux/ctype.h>
Simon Glass96495d92014-02-26 15:59:24 -070019
Simon Glass3669e0e2015-01-05 20:05:29 -070020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass96495d92014-02-26 15:59:24 -070022/**
Patrick Delaunay9f2b0662020-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 Glass96495d92014-02-26 15:59:24 -070039 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glass96495d92014-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 Glassae7123f2015-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 Glass96495d92014-02-26 15:59:24 -070048 */
Simon Glassae7123f2015-01-05 20:05:27 -070049static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070050{
51 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +020052 struct udevice *dev;
Simon Glass96495d92014-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 Glasse564f052015-03-05 12:25:20 -070058 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -070059 if (gpio >= uc_priv->gpio_base &&
60 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay9f2b0662020-01-13 11:35:01 +010061 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glass96495d92014-02-26 15:59:24 -070062 return 0;
63 }
64 }
65
66 /* No such GPIO */
Simon Glassae7123f2015-01-05 20:05:27 -070067 return ret ? ret : -ENOENT;
Simon Glass96495d92014-02-26 15:59:24 -070068}
69
Simon Glass32ec1592015-06-23 15:38:40 -060070int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070071{
Simon Glassfe1ef502014-10-22 21:37:01 -060072 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocher54c5d082014-05-22 12:43:05 +020073 struct udevice *dev;
Simon Glassfe1ef502014-10-22 21:37:01 -060074 ulong offset;
75 int numeric;
Simon Glass96495d92014-02-26 15:59:24 -070076 int ret;
77
Simon Glassfe1ef502014-10-22 21:37:01 -060078 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glass96495d92014-02-26 15:59:24 -070079 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
80 dev;
81 ret = uclass_next_device(&dev)) {
Simon Glass96495d92014-02-26 15:59:24 -070082 int len;
83
Simon Glasse564f052015-03-05 12:25:20 -070084 uc_priv = dev_get_uclass_priv(dev);
Simon Glassfe1ef502014-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 Rini75897912017-05-10 15:20:15 -040088 if (offset < uc_priv->gpio_count)
Simon Glassfe1ef502014-10-22 21:37:01 -060089 break;
90 }
91
Simon Glass96495d92014-02-26 15:59:24 -070092 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
93
Simon Glass939cda52014-06-11 23:29:47 -060094 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glassfe1ef502014-10-22 21:37:01 -060095 if (!strict_strtoul(name + len, 10, &offset))
96 break;
Simon Glass96495d92014-02-26 15:59:24 -070097 }
98 }
99
Simon Glassfe1ef502014-10-22 21:37:01 -0600100 if (!dev)
101 return ret ? ret : -EINVAL;
102
Patrick Delaunay9f2b0662020-01-13 11:35:01 +0100103 gpio_desc_init(desc, dev, offset);
Simon Glass32ec1592015-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
Simon Glassfe1ef502014-10-22 21:37:01 -0600114 if (devp)
Simon Glass32ec1592015-06-23 15:38:40 -0600115 *devp = NULL;
116 ret = dm_gpio_lookup_name(name, &desc);
117 if (ret)
118 return ret;
119
120 if (devp)
121 *devp = desc.dev;
Simon Glassfe1ef502014-10-22 21:37:01 -0600122 if (offsetp)
Simon Glass32ec1592015-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 Glassfe1ef502014-10-22 21:37:01 -0600129
130 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700131}
132
Simon Glass3a571232017-05-18 20:09:18 -0600133int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
134 struct ofnode_phandle_args *args)
Eric Nelson6c880b72016-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 Delaunay695e5fd2020-01-13 11:35:06 +0100144 desc->flags = 0;
Eric Nelson6c880b72016-04-24 16:32:40 -0700145 if (args->args[1] & GPIO_ACTIVE_LOW)
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100146 desc->flags |= GPIOD_ACTIVE_LOW;
Eric Nelson6c880b72016-04-24 16:32:40 -0700147
Patrick Delaunay477ca572020-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 Nelson6c880b72016-04-24 16:32:40 -0700166 return 0;
167}
168
Simon Glass3669e0e2015-01-05 20:05:29 -0700169static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600170 struct ofnode_phandle_args *args)
Simon Glass0dac4d52015-01-05 20:05:28 -0700171{
172 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
173
Eric Nelson6c880b72016-04-24 16:32:40 -0700174 if (ops->xlate)
175 return ops->xlate(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700176 else
Eric Nelson6c880b72016-04-24 16:32:40 -0700177 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700178}
179
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200180#if defined(CONFIG_GPIO_HOG)
Heiko Schocher5fc7cf82019-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 Schocher49b10cb2019-07-17 06:59:51 +0200217 if (nodename)
218 device_set_name(dev, nodename);
Heiko Schocher5fc7cf82019-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 Schocher49b10cb2019-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 Schocher5fc7cf82019-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 Schocher49b10cb2019-07-17 06:59:51 +0200254 int retval = 0;
Heiko Schocher5fc7cf82019-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 Schocher49b10cb2019-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 Schocher5fc7cf82019-06-12 06:11:46 +0200266 }
267 }
268
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200269 return retval;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200270}
271
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200272int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200273{
274 struct udevice *dev;
275
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200276 *desc = NULL;
Heiko Schocher5fc7cf82019-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 Schocher49b10cb2019-07-17 06:59:51 +0200281 *desc = &priv->gpiod;
282 return 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200283 }
284
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200285 return -ENODEV;
Heiko Schocher5fc7cf82019-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 Schocher49b10cb2019-07-17 06:59:51 +0200297int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200298{
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200299 return 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200300}
301#endif
302
Simon Glassefa677f2015-06-23 15:38:41 -0600303int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassae7123f2015-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 Glasse564f052015-03-05 12:25:20 -0700310 uc_priv = dev_get_uclass_priv(dev);
Simon Glassae7123f2015-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 Glass3669e0e2015-01-05 20:05:29 -0700328static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
329{
Simon Glass27084c02019-09-25 08:56:27 -0600330#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass3669e0e2015-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 Glass4dc52592015-12-29 05:22:48 -0700338#else
339 return dm_gpio_request(desc, fmt);
340#endif
Simon Glass3669e0e2015-01-05 20:05:29 -0700341}
342
Simon Glass96495d92014-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 Glassb892d122014-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 Glass96495d92014-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 Glassae7123f2015-01-05 20:05:27 -0700357 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700358 int ret;
359
Simon Glassae7123f2015-01-05 20:05:27 -0700360 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700361 if (ret)
362 return ret;
363
Simon Glassae7123f2015-01-05 20:05:27 -0700364 return dm_gpio_request(&desc, label);
Simon Glass96495d92014-02-26 15:59:24 -0700365}
366
367/**
Simon Glassd44f5972014-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 Glass27084c02019-09-25 08:56:27 -0600379#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glassd44f5972014-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 Glass4dc52592015-12-29 05:22:48 -0700387#else
388 return gpio_request(gpio, fmt);
389#endif
Simon Glassd44f5972014-10-04 11:29:49 -0600390}
391
Simon Glassae7123f2015-01-05 20:05:27 -0700392int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass96495d92014-02-26 15:59:24 -0700393{
Simon Glassb892d122014-10-04 11:29:42 -0600394 struct gpio_dev_priv *uc_priv;
Simon Glass96495d92014-02-26 15:59:24 -0700395 int ret;
396
Simon Glasse564f052015-03-05 12:25:20 -0700397 uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600398 if (!uc_priv->name[offset])
399 return -ENXIO;
Simon Glass093152f2020-02-04 20:15:17 -0700400 if (gpio_get_ops(dev)->rfree) {
401 ret = gpio_get_ops(dev)->rfree(dev, offset);
Simon Glassb892d122014-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 Glassae7123f2015-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)
Simon Glassb892d122014-10-04 11:29:42 -0600421{
Simon Glassae7123f2015-01-05 20:05:27 -0700422 struct gpio_desc desc;
423 int ret;
Simon Glassb892d122014-10-04 11:29:42 -0600424
Simon Glassae7123f2015-01-05 20:05:27 -0700425 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 Glass17c43f12016-03-06 19:27:51 -0700432static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glassae7123f2015-01-05 20:05:27 -0700433{
Simon Glasseca48662015-07-02 18:16:16 -0600434 struct gpio_dev_priv *uc_priv;
Simon Glassae7123f2015-01-05 20:05:27 -0700435
Simon Glasseca48662015-07-02 18:16:16 -0600436 if (!dm_gpio_is_valid(desc))
437 return -ENOENT;
438
439 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700440 if (!uc_priv->name[desc->offset]) {
Simon Glassb892d122014-10-04 11:29:42 -0600441 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassae7123f2015-01-05 20:05:27 -0700442 desc->dev->name, func,
443 uc_priv->bank_name ? uc_priv->bank_name : "",
444 desc->offset);
Simon Glassb892d122014-10-04 11:29:42 -0600445 return -EBUSY;
446 }
447
448 return 0;
Simon Glass96495d92014-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 Glassae7123f2015-01-05 20:05:27 -0700461 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700462 int ret;
463
Simon Glassae7123f2015-01-05 20:05:27 -0700464 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700465 if (ret)
466 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700467 ret = check_reserved(&desc, "dir_input");
468 if (ret)
469 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700470
Simon Glassae7123f2015-01-05 20:05:27 -0700471 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glass96495d92014-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 Glassae7123f2015-01-05 20:05:27 -0700485 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700486 int ret;
487
Simon Glassae7123f2015-01-05 20:05:27 -0700488 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700489 if (ret)
490 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700491 ret = check_reserved(&desc, "dir_output");
492 if (ret)
493 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700494
Simon Glassae7123f2015-01-05 20:05:27 -0700495 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
496 desc.offset, value);
497}
498
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100499static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassae7123f2015-01-05 20:05:27 -0700500{
501 int value;
Patrick Delaunay8a9140c2020-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 Glassae7123f2015-01-05 20:05:27 -0700510 int ret;
511
512 ret = check_reserved(desc, "get_value");
513 if (ret)
514 return ret;
515
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100516 return _gpio_get_value(desc);
Simon Glassae7123f2015-01-05 20:05:27 -0700517}
518
Simon Glass17c43f12016-03-06 19:27:51 -0700519int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassae7123f2015-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;
529 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
530 return 0;
531}
532
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100533/* check dir flags invalid configuration */
534static int check_dir_flags(ulong flags)
535{
536 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
537 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
538 __func__, flags);
539 return -EINVAL;
540 }
541
Patrick Delaunay477ca572020-01-13 11:35:07 +0100542 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
543 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
544 __func__, flags);
545 return -EINVAL;
546 }
547
548 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
549 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
550 __func__, flags);
551 return -EINVAL;
552 }
553
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100554 return 0;
555}
556
Patrick Delaunay788ea832020-01-13 11:35:03 +0100557static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
Simon Glassae7123f2015-01-05 20:05:27 -0700558{
559 struct udevice *dev = desc->dev;
560 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100561 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay788ea832020-01-13 11:35:03 +0100562 int ret = 0;
Simon Glassae7123f2015-01-05 20:05:27 -0700563
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100564 ret = check_dir_flags(flags);
565 if (ret) {
566 dev_dbg(dev,
567 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
568 desc->dev->name,
569 uc_priv->bank_name ? uc_priv->bank_name : "",
570 desc->offset, flags);
571
572 return ret;
573 }
574
Patrick Delaunay8fd9daf2020-01-13 11:35:09 +0100575 /* GPIOD_ are directly managed by driver in set_dir_flags*/
576 if (ops->set_dir_flags) {
577 ret = ops->set_dir_flags(dev, desc->offset, flags);
578 } else {
579 if (flags & GPIOD_IS_OUT) {
580 ret = ops->direction_output(dev, desc->offset,
581 GPIOD_FLAGS_OUTPUT(flags));
582 } else if (flags & GPIOD_IS_IN) {
583 ret = ops->direction_input(dev, desc->offset);
584 }
Simon Glassae7123f2015-01-05 20:05:27 -0700585 }
Patrick Delaunay788ea832020-01-13 11:35:03 +0100586
587 return ret;
588}
589
590int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
591{
592 int ret;
593
594 ret = check_reserved(desc, "set_dir_flags");
Simon Glassae7123f2015-01-05 20:05:27 -0700595 if (ret)
596 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700597
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100598 /* combine the requested flags (for IN/OUT) and the descriptor flags */
599 flags |= desc->flags;
Patrick Delaunay788ea832020-01-13 11:35:03 +0100600 ret = _dm_gpio_set_dir_flags(desc, flags);
601
602 /* update the descriptor flags */
603 if (ret)
604 desc->flags = flags;
605
606 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700607}
608
609int dm_gpio_set_dir(struct gpio_desc *desc)
610{
Patrick Delaunay788ea832020-01-13 11:35:03 +0100611 int ret;
612
613 ret = check_reserved(desc, "set_dir");
614 if (ret)
615 return ret;
616
617 return _dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700618}
619
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100620int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
621{
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100622 struct udevice *dev = desc->dev;
623 int ret, value;
624 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100625 ulong dir_flags;
626
627 ret = check_reserved(desc, "get_dir_flags");
628 if (ret)
629 return ret;
630
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100631 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
632 if (ops->get_dir_flags) {
633 ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
634 if (ret)
635 return ret;
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100636
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100637 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
638 value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
639 if (desc->flags & GPIOD_ACTIVE_LOW)
640 value = !value;
641 dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
642 dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
643 if (value)
644 dir_flags |= GPIOD_IS_OUT_ACTIVE;
645 } else {
646 dir_flags = desc->flags;
647 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
648 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
649 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
650 dir_flags |= GPIOD_IS_OUT_ACTIVE;
651 }
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100652 *flags = dir_flags;
653
654 return 0;
655}
656
Simon Glass96495d92014-02-26 15:59:24 -0700657/**
658 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
659 * gpio: GPIO number
660 *
661 * This function implements the API that's compatible with current
662 * GPIO API used in U-Boot. The request is forwarded to particular
663 * GPIO driver. Returns the value of the GPIO pin, or negative value
664 * on error.
665 */
666int gpio_get_value(unsigned gpio)
667{
Simon Glass96495d92014-02-26 15:59:24 -0700668 int ret;
669
Simon Glassae7123f2015-01-05 20:05:27 -0700670 struct gpio_desc desc;
671
672 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700673 if (ret)
674 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700675 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700676}
677
678/**
679 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
680 * gpio: GPIO number
681 * value: Logical value to be set on the GPIO pin.
682 *
683 * This function implements the API that's compatible with current
684 * GPIO API used in U-Boot. The request is forwarded to particular
685 * GPIO driver. Returns 0 on success, negative value on error.
686 */
687int gpio_set_value(unsigned gpio, int value)
688{
Simon Glassae7123f2015-01-05 20:05:27 -0700689 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700690 int ret;
691
Simon Glassae7123f2015-01-05 20:05:27 -0700692 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700693 if (ret)
694 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700695 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700696}
697
Heiko Schocher54c5d082014-05-22 12:43:05 +0200698const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700699{
700 struct gpio_dev_priv *priv;
701
702 /* Must be called on an active device */
Simon Glasse564f052015-03-05 12:25:20 -0700703 priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700704 assert(priv);
705
706 *bit_count = priv->gpio_count;
707 return priv->bank_name;
708}
709
Simon Glass6449a502014-10-04 11:29:43 -0600710static const char * const gpio_function[GPIOF_COUNT] = {
711 "input",
712 "output",
713 "unused",
714 "unknown",
715 "func",
716};
717
Masahiro Yamadafb07f972017-06-22 16:50:25 +0900718static int get_function(struct udevice *dev, int offset, bool skip_unused,
719 const char **namep)
Simon Glass6449a502014-10-04 11:29:43 -0600720{
Simon Glasse564f052015-03-05 12:25:20 -0700721 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6449a502014-10-04 11:29:43 -0600722 struct dm_gpio_ops *ops = gpio_get_ops(dev);
723
724 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
725 if (!device_active(dev))
726 return -ENODEV;
727 if (offset < 0 || offset >= uc_priv->gpio_count)
728 return -EINVAL;
729 if (namep)
730 *namep = uc_priv->name[offset];
731 if (skip_unused && !uc_priv->name[offset])
732 return GPIOF_UNUSED;
733 if (ops->get_function) {
734 int ret;
735
736 ret = ops->get_function(dev, offset);
737 if (ret < 0)
738 return ret;
739 if (ret >= ARRAY_SIZE(gpio_function))
740 return -ENODATA;
741 return ret;
742 }
743
744 return GPIOF_UNKNOWN;
745}
746
747int gpio_get_function(struct udevice *dev, int offset, const char **namep)
748{
749 return get_function(dev, offset, true, namep);
750}
751
752int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
753{
754 return get_function(dev, offset, false, namep);
755}
756
Simon Glass07575352014-10-04 11:29:44 -0600757int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
758{
759 struct dm_gpio_ops *ops = gpio_get_ops(dev);
760 struct gpio_dev_priv *priv;
761 char *str = buf;
762 int func;
763 int ret;
764 int len;
765
766 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
767
768 *buf = 0;
Simon Glasse564f052015-03-05 12:25:20 -0700769 priv = dev_get_uclass_priv(dev);
Simon Glass07575352014-10-04 11:29:44 -0600770 ret = gpio_get_raw_function(dev, offset, NULL);
771 if (ret < 0)
772 return ret;
773 func = ret;
774 len = snprintf(str, buffsize, "%s%d: %s",
775 priv->bank_name ? priv->bank_name : "",
776 offset, gpio_function[func]);
777 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
778 func == GPIOF_UNUSED) {
779 const char *label;
780 bool used;
781
782 ret = ops->get_value(dev, offset);
783 if (ret < 0)
784 return ret;
785 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
786 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
787 ret,
788 used ? 'x' : ' ',
789 used ? " " : "",
790 label ? label : "");
791 }
792
793 return 0;
794}
795
Simon Glass962f5ca2015-04-14 21:03:20 -0600796int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
797{
798 int i, ret;
799 int gpio;
800
801 for (i = 0; i < 32; i++) {
802 gpio = gpio_num_array[i];
803 if (gpio == -1)
804 break;
805 ret = gpio_requestf(gpio, fmt, i);
806 if (ret)
807 goto err;
808 ret = gpio_direction_input(gpio);
809 if (ret) {
810 gpio_free(gpio);
811 goto err;
812 }
813 }
814
815 return 0;
816err:
817 for (i--; i >= 0; i--)
818 gpio_free(gpio_num_array[i]);
819
820 return ret;
821}
822
Simon Glasse5901c92014-11-10 18:00:21 -0700823/*
824 * get a number comprised of multiple GPIO values. gpio_num_array points to
825 * the array of gpio pin numbers to scan, terminated by -1.
826 */
Simon Glass962f5ca2015-04-14 21:03:20 -0600827int gpio_get_values_as_int(const int *gpio_list)
Simon Glasse5901c92014-11-10 18:00:21 -0700828{
829 int gpio;
830 unsigned bitmask = 1;
831 unsigned vector = 0;
Simon Glass962f5ca2015-04-14 21:03:20 -0600832 int ret;
Simon Glasse5901c92014-11-10 18:00:21 -0700833
834 while (bitmask &&
Simon Glass962f5ca2015-04-14 21:03:20 -0600835 ((gpio = *gpio_list++) != -1)) {
836 ret = gpio_get_value(gpio);
837 if (ret < 0)
838 return ret;
839 else if (ret)
Simon Glasse5901c92014-11-10 18:00:21 -0700840 vector |= bitmask;
841 bitmask <<= 1;
842 }
Simon Glass962f5ca2015-04-14 21:03:20 -0600843
Simon Glasse5901c92014-11-10 18:00:21 -0700844 return vector;
845}
846
Simon Glass17c43f12016-03-06 19:27:51 -0700847int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassbbf24782016-03-06 19:27:50 -0700848{
849 unsigned bitmask = 1;
850 unsigned vector = 0;
851 int ret, i;
852
853 for (i = 0; i < count; i++) {
854 ret = dm_gpio_get_value(&desc_list[i]);
855 if (ret < 0)
856 return ret;
857 else if (ret)
858 vector |= bitmask;
859 bitmask <<= 1;
860 }
861
862 return vector;
863}
864
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200865/**
866 * gpio_request_tail: common work for requesting a gpio.
867 *
868 * ret: return value from previous work in function which calls
869 * this function.
870 * This seems bogus (why calling this function instead not
871 * calling it and end caller function instead?).
872 * Because on error in caller function we want to set some
873 * default values in gpio desc and have a common error
874 * debug message, which provides this function.
875 * nodename: Name of node for which gpio gets requested
876 * used for gpio label name.
877 * args: pointer to output arguments structure
878 * list_name: Name of GPIO list
879 * used for gpio label name.
880 * index: gpio index in gpio list
881 * used for gpio label name.
882 * desc: pointer to gpio descriptor, filled from this
883 * function.
884 * flags: gpio flags to use.
885 * add_index: should index added to gpio label name
886 * gpio_dev: pointer to gpio device from which the gpio
887 * will be requested. If NULL try to get the
888 * gpio device with uclass_get_device_by_ofnode()
889 *
890 * return: In error case this function sets default values in
891 * gpio descriptor, also emmits a debug message.
892 * On success it returns 0 else the error code from
893 * function calls, or the error code passed through
894 * ret to this function.
895 *
896 */
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200897static int gpio_request_tail(int ret, const char *nodename,
Simon Glass3a571232017-05-18 20:09:18 -0600898 struct ofnode_phandle_args *args,
899 const char *list_name, int index,
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200900 struct gpio_desc *desc, int flags,
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200901 bool add_index, struct udevice *gpio_dev)
Simon Glass3669e0e2015-01-05 20:05:29 -0700902{
Patrick Delaunay9f2b0662020-01-13 11:35:01 +0100903 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass3a571232017-05-18 20:09:18 -0600904 if (ret)
Simon Glass3669e0e2015-01-05 20:05:29 -0700905 goto err;
Simon Glass3669e0e2015-01-05 20:05:29 -0700906
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200907 if (!desc->dev) {
908 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
909 &desc->dev);
910 if (ret) {
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200911 debug("%s: uclass_get_device_by_ofnode failed\n",
912 __func__);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200913 goto err;
914 }
Simon Glass3669e0e2015-01-05 20:05:29 -0700915 }
Simon Glass3a571232017-05-18 20:09:18 -0600916 ret = gpio_find_and_xlate(desc, args);
Simon Glass3669e0e2015-01-05 20:05:29 -0700917 if (ret) {
918 debug("%s: gpio_find_and_xlate failed\n", __func__);
919 goto err;
920 }
921 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200922 nodename, list_name, index);
Simon Glass3669e0e2015-01-05 20:05:29 -0700923 if (ret) {
924 debug("%s: dm_gpio_requestf failed\n", __func__);
925 goto err;
926 }
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100927 ret = dm_gpio_set_dir_flags(desc, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -0700928 if (ret) {
929 debug("%s: dm_gpio_set_dir failed\n", __func__);
930 goto err;
931 }
932
933 return 0;
934err:
935 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200936 __func__, nodename, list_name, index, ret);
Simon Glass3669e0e2015-01-05 20:05:29 -0700937 return ret;
938}
939
Simon Glass150c5af2017-05-30 21:47:09 -0600940static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
941 int index, struct gpio_desc *desc,
942 int flags, bool add_index)
Simon Glass3a571232017-05-18 20:09:18 -0600943{
944 struct ofnode_phandle_args args;
945 int ret;
946
Simon Glass150c5af2017-05-30 21:47:09 -0600947 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
948 index, &args);
Simon Glass3a571232017-05-18 20:09:18 -0600949
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200950 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
951 index, desc, flags, add_index, NULL);
Simon Glass3a571232017-05-18 20:09:18 -0600952}
953
Simon Glass150c5af2017-05-30 21:47:09 -0600954int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -0700955 struct gpio_desc *desc, int flags)
956{
Simon Glass150c5af2017-05-30 21:47:09 -0600957 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
958 index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -0700959}
960
Simon Glass150c5af2017-05-30 21:47:09 -0600961int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -0700962 struct gpio_desc *desc, int flags)
963{
Simon Glass150c5af2017-05-30 21:47:09 -0600964 struct ofnode_phandle_args args;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200965 ofnode node;
Simon Glass150c5af2017-05-30 21:47:09 -0600966 int ret;
967
968 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
969 index, &args);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200970 node = dev_ofnode(dev);
971 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
972 index, desc, flags, index > 0, NULL);
Simon Glass3669e0e2015-01-05 20:05:29 -0700973}
974
Simon Glass150c5af2017-05-30 21:47:09 -0600975int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass3669e0e2015-01-05 20:05:29 -0700976 struct gpio_desc *desc, int max_count,
977 int flags)
978{
979 int count;
980 int ret;
981
Przemyslaw Marczak2984e7a2015-03-31 18:57:16 +0200982 for (count = 0; count < max_count; count++) {
Simon Glass150c5af2017-05-30 21:47:09 -0600983 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass3669e0e2015-01-05 20:05:29 -0700984 &desc[count], flags, true);
985 if (ret == -ENOENT)
986 break;
987 else if (ret)
988 goto err;
989 }
990
991 /* We ran out of GPIOs in the list */
992 return count;
993
994err:
995 gpio_free_list_nodev(desc, count - 1);
996
997 return ret;
998}
999
1000int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1001 struct gpio_desc *desc, int max_count,
1002 int flags)
1003{
1004 /*
1005 * This isn't ideal since we don't use dev->name in the debug()
1006 * calls in gpio_request_by_name(), but we can do this until
1007 * gpio_request_list_by_name_nodev() can be dropped.
1008 */
Simon Glass150c5af2017-05-30 21:47:09 -06001009 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1010 max_count, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -07001011}
1012
1013int gpio_get_list_count(struct udevice *dev, const char *list_name)
1014{
1015 int ret;
1016
Simon Glasse160f7d2017-01-17 16:52:55 -07001017 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass3669e0e2015-01-05 20:05:29 -07001018 list_name, "#gpio-cells", 0, -1,
1019 NULL);
1020 if (ret) {
1021 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1022 __func__, dev->name, list_name, ret);
1023 }
1024
1025 return ret;
1026}
1027
1028int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1029{
1030 /* For now, we don't do any checking of dev */
1031 return _dm_gpio_free(desc->dev, desc->offset);
1032}
1033
1034int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1035{
1036 int i;
1037
1038 /* For now, we don't do any checking of dev */
1039 for (i = 0; i < count; i++)
1040 dm_gpio_free(dev, &desc[i]);
1041
1042 return 0;
1043}
1044
1045int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1046{
1047 return gpio_free_list(NULL, desc, count);
1048}
1049
Simon Glass96495d92014-02-26 15:59:24 -07001050/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -06001051static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -07001052{
1053 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +02001054 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -07001055 struct uclass *uc;
1056 unsigned base;
1057 int ret;
1058
1059 ret = uclass_get(UCLASS_GPIO, &uc);
1060 if (ret)
1061 return ret;
1062
1063 /* Ensure that we have a base for each bank */
1064 base = 0;
1065 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -06001066 if (device_active(dev) && dev != removed_dev) {
Simon Glasse564f052015-03-05 12:25:20 -07001067 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001068 uc_priv->gpio_base = base;
1069 base += uc_priv->gpio_count;
1070 }
1071 }
1072
1073 return 0;
1074}
1075
Simon Glass17c43f12016-03-06 19:27:51 -07001076int gpio_get_number(const struct gpio_desc *desc)
Simon Glass56a71f82015-03-25 12:21:58 -06001077{
1078 struct udevice *dev = desc->dev;
1079 struct gpio_dev_priv *uc_priv;
1080
1081 if (!dev)
1082 return -1;
1083 uc_priv = dev->uclass_priv;
1084
1085 return uc_priv->gpio_base + desc->offset;
1086}
1087
Heiko Schocher54c5d082014-05-22 12:43:05 +02001088static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001089{
Simon Glasse564f052015-03-05 12:25:20 -07001090 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001091
1092 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1093 if (!uc_priv->name)
1094 return -ENOMEM;
1095
1096 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -07001097}
1098
Heiko Schocher54c5d082014-05-22 12:43:05 +02001099static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001100{
Simon Glasse564f052015-03-05 12:25:20 -07001101 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001102 int i;
1103
1104 for (i = 0; i < uc_priv->gpio_count; i++) {
1105 if (uc_priv->name[i])
1106 free(uc_priv->name[i]);
1107 }
1108 free(uc_priv->name);
1109
1110 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001111}
1112
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001113int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1114 char *list_name, int index, int flags,
1115 int dtflags, struct gpio_desc *desc)
1116{
1117 struct ofnode_phandle_args args;
1118
1119 args.node = ofnode_null();
1120 args.args_count = 2;
1121 args.args[0] = index;
1122 args.args[1] = dtflags;
1123
1124 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1125 flags, 0, dev);
1126}
1127
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001128static int gpio_post_bind(struct udevice *dev)
1129{
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001130 struct udevice *child;
1131 ofnode node;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001132
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001133#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1134 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1135 static int reloc_done;
1136
1137 if (!reloc_done) {
1138 if (ops->request)
1139 ops->request += gd->reloc_off;
Simon Glass093152f2020-02-04 20:15:17 -07001140 if (ops->rfree)
1141 ops->rfree += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001142 if (ops->direction_input)
1143 ops->direction_input += gd->reloc_off;
1144 if (ops->direction_output)
1145 ops->direction_output += gd->reloc_off;
1146 if (ops->get_value)
1147 ops->get_value += gd->reloc_off;
1148 if (ops->set_value)
1149 ops->set_value += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001150 if (ops->get_function)
1151 ops->get_function += gd->reloc_off;
1152 if (ops->xlate)
1153 ops->xlate += gd->reloc_off;
Patrick Delaunay8fd9daf2020-01-13 11:35:09 +01001154 if (ops->set_dir_flags)
1155 ops->set_dir_flags += gd->reloc_off;
Patrick Delaunayd2c07e52020-01-13 11:35:08 +01001156 if (ops->get_dir_flags)
1157 ops->get_dir_flags += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001158
1159 reloc_done++;
1160 }
1161#endif
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001162
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001163 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1164 dev_for_each_subnode(node, dev) {
1165 if (ofnode_read_bool(node, "gpio-hog")) {
1166 const char *name = ofnode_get_name(node);
1167 int ret;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001168
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001169 ret = device_bind_driver_to_node(dev,
1170 "gpio_hog",
1171 name, node,
1172 &child);
1173 if (ret)
1174 return ret;
1175 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001176 }
1177 }
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001178 return 0;
1179}
1180
Simon Glass96495d92014-02-26 15:59:24 -07001181UCLASS_DRIVER(gpio) = {
1182 .id = UCLASS_GPIO,
1183 .name = "gpio",
Bhuvanchandra DVae89bb02015-06-01 18:37:15 +05301184 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass96495d92014-02-26 15:59:24 -07001185 .post_probe = gpio_post_probe,
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001186 .post_bind = gpio_post_bind,
Simon Glass96495d92014-02-26 15:59:24 -07001187 .pre_remove = gpio_pre_remove,
1188 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1189};