blob: fde046e8df83f9c98a5d0392b455450829c7258b [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
Simon Glasse43eae22021-02-04 21:22:08 -07006#define LOG_CATEGORY UCLASS_GPIO
7
Simon Glasse821d182014-02-26 15:59:24 -07008#include <common.h>
9#include <dm.h>
Simon Glass2149e112021-08-07 07:24:12 -060010#include <dt-structs.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +053012#include <dm/devres.h>
13#include <dm/device_compat.h>
Heiko Schocher39cb3402019-06-12 06:11:46 +020014#include <dm/device-internal.h>
15#include <dm/lists.h>
16#include <dm/uclass-internal.h>
Eric Nelson786e98d2016-04-24 16:32:40 -070017#include <dt-bindings/gpio/gpio.h>
Simon Glasse821d182014-02-26 15:59:24 -070018#include <errno.h>
Simon Glassd3322bb2015-01-05 20:05:28 -070019#include <fdtdec.h>
Simon Glass0f4517d2014-10-04 11:29:42 -060020#include <malloc.h>
Simon Glass3176b6c2020-07-07 13:11:44 -060021#include <acpi/acpi_device.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Simon Glasse821d182014-02-26 15:59:24 -070023#include <asm/gpio.h>
Simon Glass0f2af882020-05-10 11:40:05 -060024#include <dm/device_compat.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090025#include <linux/bug.h>
Simon Glass43b2e1a2014-10-22 21:37:01 -060026#include <linux/ctype.h>
Simon Glass247ccf22021-02-04 21:22:09 -070027#include <linux/delay.h>
Simon Glasse821d182014-02-26 15:59:24 -070028
Simon Glass16e10402015-01-05 20:05:29 -070029DECLARE_GLOBAL_DATA_PTR;
30
Simon Glasse821d182014-02-26 15:59:24 -070031/**
Patrick Delaunay758bba92020-01-13 11:35:01 +010032 * gpio_desc_init() - Initialize the GPIO descriptor
33 *
34 * @desc: GPIO descriptor to initialize
35 * @dev: GPIO device
36 * @offset: Offset of device GPIO
37 */
38static void gpio_desc_init(struct gpio_desc *desc,
39 struct udevice *dev,
40 uint offset)
41{
42 desc->dev = dev;
43 desc->offset = offset;
44 desc->flags = 0;
45}
46
47/**
Simon Glasse821d182014-02-26 15:59:24 -070048 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070049 *
50 * Convert the GPIO number to an entry in the list of GPIOs
51 * or GPIO blocks registered with the GPIO controller. Returns
52 * entry on success, NULL on error.
Simon Glassce555292015-01-05 20:05:27 -070053 *
54 * @gpio: The numeric representation of the GPIO
55 * @desc: Returns description (desc->flags will always be 0)
56 * @return 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070057 */
Simon Glassce555292015-01-05 20:05:27 -070058static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070059{
60 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020061 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070062 int ret;
63
64 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65 dev;
66 ret = uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070067 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070068 if (gpio >= uc_priv->gpio_base &&
69 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay758bba92020-01-13 11:35:01 +010070 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glasse821d182014-02-26 15:59:24 -070071 return 0;
72 }
73 }
74
75 /* No such GPIO */
Simon Glassce555292015-01-05 20:05:27 -070076 return ret ? ret : -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070077}
78
Heiko Schochera3e793c2020-05-22 11:08:59 +020079#if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
80/**
81 * dm_gpio_lookup_label() - look for name in gpio device
82 *
83 * search in uc_priv, if there is a gpio with labelname same
84 * as name.
85 *
86 * @name: name which is searched
87 * @uc_priv: gpio_dev_priv pointer.
88 * @offset: gpio offset within the device
89 * @return: 0 if found, -ENOENT if not.
90 */
91static int dm_gpio_lookup_label(const char *name,
92 struct gpio_dev_priv *uc_priv, ulong *offset)
93{
94 int len;
95 int i;
96
97 *offset = -1;
98 len = strlen(name);
99 for (i = 0; i < uc_priv->gpio_count; i++) {
100 if (!uc_priv->name[i])
101 continue;
102 if (!strncmp(name, uc_priv->name[i], len)) {
103 *offset = i;
104 return 0;
105 }
106 }
107 return -ENOENT;
108}
109#else
110static int
111dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
112 ulong *offset)
113{
114 return -ENOENT;
115}
116#endif
117
Simon Glass215bcc72015-06-23 15:38:40 -0600118int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -0700119{
Simon Glass43b2e1a2014-10-22 21:37:01 -0600120 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200121 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600122 ulong offset;
123 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -0700124 int ret;
125
Simon Glassff9b9032021-07-24 09:03:30 -0600126 numeric = isdigit(*name) ? dectoul(name, NULL) : -1;
Simon Glasse821d182014-02-26 15:59:24 -0700127 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
128 dev;
129 ret = uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -0700130 int len;
131
Simon Glassde0977b2015-03-05 12:25:20 -0700132 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -0600133 if (numeric != -1) {
134 offset = numeric - uc_priv->gpio_base;
135 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -0400136 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -0600137 break;
138 }
139
Simon Glasse821d182014-02-26 15:59:24 -0700140 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
141
Simon Glassd4acf632014-06-11 23:29:47 -0600142 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -0600143 if (!strict_strtoul(name + len, 10, &offset))
Samuel Holland8f53a332021-09-11 17:05:51 -0500144 if (offset < uc_priv->gpio_count)
145 break;
Simon Glasse821d182014-02-26 15:59:24 -0700146 }
Heiko Schochera3e793c2020-05-22 11:08:59 +0200147
148 /*
149 * if we did not found a gpio through its bank
150 * name, we search for a valid gpio label.
151 */
152 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
153 break;
Simon Glasse821d182014-02-26 15:59:24 -0700154 }
155
Simon Glass43b2e1a2014-10-22 21:37:01 -0600156 if (!dev)
157 return ret ? ret : -EINVAL;
158
Patrick Delaunay758bba92020-01-13 11:35:01 +0100159 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600160
161 return 0;
162}
163
164int gpio_lookup_name(const char *name, struct udevice **devp,
165 unsigned int *offsetp, unsigned int *gpiop)
166{
167 struct gpio_desc desc;
168 int ret;
169
170 if (devp)
171 *devp = NULL;
172 ret = dm_gpio_lookup_name(name, &desc);
173 if (ret)
174 return ret;
175
Simon Glass43b2e1a2014-10-22 21:37:01 -0600176 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600177 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600178 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600179 *offsetp = desc.offset;
180 if (gpiop) {
181 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
182
183 *gpiop = uc_priv->gpio_base + desc.offset;
184 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600185
186 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700187}
188
Simon Glass12faa022017-05-18 20:09:18 -0600189int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
190 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700191{
Samuel Holland78acb922021-09-11 17:05:52 -0500192 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
193
Eric Nelson786e98d2016-04-24 16:32:40 -0700194 if (args->args_count < 1)
195 return -EINVAL;
196
197 desc->offset = args->args[0];
Samuel Holland78acb922021-09-11 17:05:52 -0500198 if (desc->offset >= uc_priv->gpio_count)
199 return -EINVAL;
Eric Nelson786e98d2016-04-24 16:32:40 -0700200
201 if (args->args_count < 2)
202 return 0;
203
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100204 desc->flags = 0;
Eric Nelson786e98d2016-04-24 16:32:40 -0700205 if (args->args[1] & GPIO_ACTIVE_LOW)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100206 desc->flags |= GPIOD_ACTIVE_LOW;
Eric Nelson786e98d2016-04-24 16:32:40 -0700207
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100208 /*
209 * need to test 2 bits for gpio output binding:
210 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
211 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
212 */
213 if (args->args[1] & GPIO_SINGLE_ENDED) {
214 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
215 desc->flags |= GPIOD_OPEN_DRAIN;
216 else
217 desc->flags |= GPIOD_OPEN_SOURCE;
218 }
219
220 if (args->args[1] & GPIO_PULL_UP)
221 desc->flags |= GPIOD_PULL_UP;
222
223 if (args->args[1] & GPIO_PULL_DOWN)
224 desc->flags |= GPIOD_PULL_DOWN;
225
Eric Nelson786e98d2016-04-24 16:32:40 -0700226 return 0;
227}
228
Simon Glass16e10402015-01-05 20:05:29 -0700229static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600230 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700231{
Simon Glass49f315a2021-02-04 21:22:05 -0700232 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassd3322bb2015-01-05 20:05:28 -0700233
Eric Nelson786e98d2016-04-24 16:32:40 -0700234 if (ops->xlate)
235 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700236 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700237 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700238}
239
Simon Glass2149e112021-08-07 07:24:12 -0600240#if CONFIG_IS_ENABLED(GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200241
242struct gpio_hog_priv {
243 struct gpio_desc gpiod;
244};
245
246struct gpio_hog_data {
247 int gpiod_flags;
248 int value;
249 u32 val[2];
250};
251
Simon Glassaad29ae2020-12-03 16:55:21 -0700252static int gpio_hog_of_to_plat(struct udevice *dev)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200253{
Simon Glassfa20e932020-12-03 16:55:20 -0700254 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200255 const char *nodename;
256 int ret;
257
258 plat->value = 0;
259 if (dev_read_bool(dev, "input")) {
260 plat->gpiod_flags = GPIOD_IS_IN;
261 } else if (dev_read_bool(dev, "output-high")) {
262 plat->value = 1;
263 plat->gpiod_flags = GPIOD_IS_OUT;
264 } else if (dev_read_bool(dev, "output-low")) {
265 plat->gpiod_flags = GPIOD_IS_OUT;
266 } else {
267 printf("%s: missing gpio-hog state.\n", __func__);
268 return -EINVAL;
269 }
270 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
271 if (ret) {
272 printf("%s: wrong gpios property, 2 values needed %d\n",
273 __func__, ret);
274 return ret;
275 }
276 nodename = dev_read_string(dev, "line-name");
Heiko Schocher58e4c382019-07-17 06:59:51 +0200277 if (nodename)
278 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200279
280 return 0;
281}
282
283static int gpio_hog_probe(struct udevice *dev)
284{
Simon Glassfa20e932020-12-03 16:55:20 -0700285 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200286 struct gpio_hog_priv *priv = dev_get_priv(dev);
287 int ret;
288
289 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
290 plat->val[0], plat->gpiod_flags,
291 plat->val[1], &priv->gpiod);
292 if (ret < 0) {
293 debug("%s: node %s could not get gpio.\n", __func__,
294 dev->name);
295 return ret;
296 }
Heiko Schocher58e4c382019-07-17 06:59:51 +0200297
298 if (plat->gpiod_flags == GPIOD_IS_OUT) {
299 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
300 if (ret < 0) {
301 debug("%s: node %s could not set gpio.\n", __func__,
302 dev->name);
303 return ret;
304 }
305 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200306
307 return 0;
308}
309
310int gpio_hog_probe_all(void)
311{
312 struct udevice *dev;
313 int ret;
Heiko Schocher58e4c382019-07-17 06:59:51 +0200314 int retval = 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200315
316 for (uclass_first_device(UCLASS_NOP, &dev);
317 dev;
318 uclass_find_next_device(&dev)) {
Simon Glass65130cd2020-12-28 20:34:56 -0700319 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
Heiko Schocher39cb3402019-06-12 06:11:46 +0200320 ret = device_probe(dev);
Heiko Schocher58e4c382019-07-17 06:59:51 +0200321 if (ret) {
322 printf("Failed to probe device %s err: %d\n",
323 dev->name, ret);
324 retval = ret;
325 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200326 }
327 }
328
Heiko Schocher58e4c382019-07-17 06:59:51 +0200329 return retval;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200330}
331
Heiko Schocher58e4c382019-07-17 06:59:51 +0200332int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200333{
334 struct udevice *dev;
335
Heiko Schocher58e4c382019-07-17 06:59:51 +0200336 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200337 gpio_hog_probe_all();
338 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
339 struct gpio_hog_priv *priv = dev_get_priv(dev);
340
Heiko Schocher58e4c382019-07-17 06:59:51 +0200341 *desc = &priv->gpiod;
342 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200343 }
344
Heiko Schocher58e4c382019-07-17 06:59:51 +0200345 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200346}
347
348U_BOOT_DRIVER(gpio_hog) = {
349 .name = "gpio_hog",
350 .id = UCLASS_NOP,
Simon Glassaad29ae2020-12-03 16:55:21 -0700351 .of_to_plat = gpio_hog_of_to_plat,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200352 .probe = gpio_hog_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700353 .priv_auto = sizeof(struct gpio_hog_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700354 .plat_auto = sizeof(struct gpio_hog_data),
Heiko Schocher39cb3402019-06-12 06:11:46 +0200355};
356#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200357int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200358{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200359 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200360}
361#endif
362
Simon Glass047cdb32015-06-23 15:38:41 -0600363int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700364{
Simon Glass49f315a2021-02-04 21:22:05 -0700365 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700366 struct udevice *dev = desc->dev;
367 struct gpio_dev_priv *uc_priv;
368 char *str;
369 int ret;
370
Simon Glassde0977b2015-03-05 12:25:20 -0700371 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700372 if (uc_priv->name[desc->offset])
373 return -EBUSY;
374 str = strdup(label);
375 if (!str)
376 return -ENOMEM;
Simon Glass49f315a2021-02-04 21:22:05 -0700377 if (ops->request) {
378 ret = ops->request(dev, desc->offset, label);
Simon Glassce555292015-01-05 20:05:27 -0700379 if (ret) {
380 free(str);
381 return ret;
382 }
383 }
384 uc_priv->name[desc->offset] = str;
385
386 return 0;
387}
388
Simon Glass16e10402015-01-05 20:05:29 -0700389static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
390{
Simon Glass7611ac62019-09-25 08:56:27 -0600391#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700392 va_list args;
393 char buf[40];
394
395 va_start(args, fmt);
396 vscnprintf(buf, sizeof(buf), fmt, args);
397 va_end(args);
398 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700399#else
400 return dm_gpio_request(desc, fmt);
401#endif
Simon Glass16e10402015-01-05 20:05:29 -0700402}
403
Simon Glasse821d182014-02-26 15:59:24 -0700404/**
405 * gpio_request() - [COMPAT] Request GPIO
406 * gpio: GPIO number
407 * label: Name for the requested GPIO
408 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600409 * The label is copied and allocated so the caller does not need to keep
410 * the pointer around.
411 *
Simon Glasse821d182014-02-26 15:59:24 -0700412 * This function implements the API that's compatible with current
413 * GPIO API used in U-Boot. The request is forwarded to particular
414 * GPIO driver. Returns 0 on success, negative value on error.
415 */
416int gpio_request(unsigned gpio, const char *label)
417{
Simon Glassce555292015-01-05 20:05:27 -0700418 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700419 int ret;
420
Simon Glassce555292015-01-05 20:05:27 -0700421 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700422 if (ret)
423 return ret;
424
Simon Glassce555292015-01-05 20:05:27 -0700425 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700426}
427
428/**
Simon Glass1b27d602014-10-04 11:29:49 -0600429 * gpio_requestf() - [COMPAT] Request GPIO
430 * @gpio: GPIO number
431 * @fmt: Format string for the requested GPIO
432 * @...: Arguments for the printf() format string
433 *
434 * This function implements the API that's compatible with current
435 * GPIO API used in U-Boot. The request is forwarded to particular
436 * GPIO driver. Returns 0 on success, negative value on error.
437 */
438int gpio_requestf(unsigned gpio, const char *fmt, ...)
439{
Simon Glass7611ac62019-09-25 08:56:27 -0600440#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600441 va_list args;
442 char buf[40];
443
444 va_start(args, fmt);
445 vscnprintf(buf, sizeof(buf), fmt, args);
446 va_end(args);
447 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700448#else
449 return gpio_request(gpio, fmt);
450#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600451}
452
Simon Glassce555292015-01-05 20:05:27 -0700453int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700454{
Simon Glass49f315a2021-02-04 21:22:05 -0700455 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600456 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700457 int ret;
458
Simon Glassde0977b2015-03-05 12:25:20 -0700459 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600460 if (!uc_priv->name[offset])
461 return -ENXIO;
Simon Glass49f315a2021-02-04 21:22:05 -0700462 if (ops->rfree) {
463 ret = ops->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600464 if (ret)
465 return ret;
466 }
467
468 free(uc_priv->name[offset]);
469 uc_priv->name[offset] = NULL;
470
471 return 0;
472}
473
Simon Glassce555292015-01-05 20:05:27 -0700474/**
475 * gpio_free() - [COMPAT] Relinquish GPIO
476 * gpio: GPIO number
477 *
478 * This function implements the API that's compatible with current
479 * GPIO API used in U-Boot. The request is forwarded to particular
480 * GPIO driver. Returns 0 on success, negative value on error.
481 */
482int gpio_free(unsigned gpio)
483{
484 struct gpio_desc desc;
485 int ret;
486
487 ret = gpio_to_device(gpio, &desc);
488 if (ret)
489 return ret;
490
491 return _dm_gpio_free(desc.dev, desc.offset);
492}
493
Simon Glassfd838972016-03-06 19:27:51 -0700494static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600495{
Simon Glass230c1432015-07-02 18:16:16 -0600496 struct gpio_dev_priv *uc_priv;
497
498 if (!dm_gpio_is_valid(desc))
499 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600500
Simon Glass230c1432015-07-02 18:16:16 -0600501 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700502 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600503 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700504 desc->dev->name, func,
505 uc_priv->bank_name ? uc_priv->bank_name : "",
506 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600507 return -EBUSY;
508 }
509
510 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700511}
512
513/**
514 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
515 * gpio: GPIO number
516 *
517 * This function implements the API that's compatible with current
518 * GPIO API used in U-Boot. The request is forwarded to particular
519 * GPIO driver. Returns 0 on success, negative value on error.
520 */
521int gpio_direction_input(unsigned gpio)
522{
Simon Glassce555292015-01-05 20:05:27 -0700523 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700524 int ret;
525
Simon Glassce555292015-01-05 20:05:27 -0700526 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700527 if (ret)
528 return ret;
529
Simon Glass722f9682021-02-04 21:22:04 -0700530 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
Simon Glasse821d182014-02-26 15:59:24 -0700531}
532
533/**
534 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
535 * gpio: GPIO number
536 * value: Logical value to be set on the GPIO pin
537 *
538 * This function implements the API that's compatible with current
539 * GPIO API used in U-Boot. The request is forwarded to particular
540 * GPIO driver. Returns 0 on success, negative value on error.
541 */
542int gpio_direction_output(unsigned gpio, int value)
543{
Simon Glassce555292015-01-05 20:05:27 -0700544 struct gpio_desc desc;
Simon Glass722f9682021-02-04 21:22:04 -0700545 ulong flags;
Simon Glassce555292015-01-05 20:05:27 -0700546 int ret;
547
548 ret = gpio_to_device(gpio, &desc);
549 if (ret)
550 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700551
Simon Glass722f9682021-02-04 21:22:04 -0700552 flags = GPIOD_IS_OUT;
553 if (value)
554 flags |= GPIOD_IS_OUT_ACTIVE;
555 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700556}
557
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100558static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700559{
Simon Glass49f315a2021-02-04 21:22:05 -0700560 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700561 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100562
Simon Glass49f315a2021-02-04 21:22:05 -0700563 value = ops->get_value(desc->dev, desc->offset);
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100564
565 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
566}
567
568int dm_gpio_get_value(const struct gpio_desc *desc)
569{
Simon Glassce555292015-01-05 20:05:27 -0700570 int ret;
571
572 ret = check_reserved(desc, "get_value");
573 if (ret)
574 return ret;
575
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100576 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700577}
578
Simon Glassfd838972016-03-06 19:27:51 -0700579int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700580{
Simon Glass7b893f92021-02-04 21:22:03 -0700581 const struct dm_gpio_ops *ops;
Simon Glassce555292015-01-05 20:05:27 -0700582 int ret;
583
584 ret = check_reserved(desc, "set_value");
585 if (ret)
586 return ret;
587
588 if (desc->flags & GPIOD_ACTIVE_LOW)
589 value = !value;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200590
Simon Glass7b893f92021-02-04 21:22:03 -0700591 /* GPIOD_ are directly managed by driver in set_flags */
592 ops = gpio_get_ops(desc->dev);
593 if (ops->set_flags) {
594 ulong flags = desc->flags;
595
596 if (value)
597 flags |= GPIOD_IS_OUT_ACTIVE;
598 else
599 flags &= ~GPIOD_IS_OUT_ACTIVE;
600 return ops->set_flags(desc->dev, desc->offset, flags);
601 }
602
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200603 /*
604 * Emulate open drain by not actively driving the line high or
605 * Emulate open source by not actively driving the line low
606 */
607 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
608 (desc->flags & GPIOD_OPEN_SOURCE && !value))
Simon Glass7b893f92021-02-04 21:22:03 -0700609 return ops->direction_input(desc->dev, desc->offset);
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200610 else if (desc->flags & GPIOD_OPEN_DRAIN ||
611 desc->flags & GPIOD_OPEN_SOURCE)
Simon Glass7b893f92021-02-04 21:22:03 -0700612 return ops->direction_output(desc->dev, desc->offset, value);
613
614 ret = ops->set_value(desc->dev, desc->offset, value);
615 if (ret)
616 return ret;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200617
Simon Glassce555292015-01-05 20:05:27 -0700618 return 0;
619}
620
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100621/* check dir flags invalid configuration */
622static int check_dir_flags(ulong flags)
623{
624 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
625 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
626 __func__, flags);
627 return -EINVAL;
628 }
629
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100630 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
631 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
632 __func__, flags);
633 return -EINVAL;
634 }
635
636 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
637 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
638 __func__, flags);
639 return -EINVAL;
640 }
641
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100642 return 0;
643}
644
Simon Glass7b893f92021-02-04 21:22:03 -0700645/**
646 * _dm_gpio_set_flags() - Send flags to the driver
647 *
648 * This uses the best available method to send the given flags to the driver.
649 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
650 * of GPIOD_IS_OUT_ACTIVE.
651 *
652 * @desc: GPIO description
653 * @flags: flags value to set
654 * @return 0 if OK, -ve on error
655 */
Simon Glass54befdd2021-02-04 21:21:55 -0700656static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700657{
658 struct udevice *dev = desc->dev;
Simon Glass49f315a2021-02-04 21:22:05 -0700659 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100660 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100661 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700662
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100663 ret = check_dir_flags(flags);
664 if (ret) {
665 dev_dbg(dev,
666 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
667 desc->dev->name,
668 uc_priv->bank_name ? uc_priv->bank_name : "",
669 desc->offset, flags);
670
671 return ret;
672 }
673
Simon Glass7b893f92021-02-04 21:22:03 -0700674 /* If active low, invert the output state */
675 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
676 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
677 flags ^= GPIOD_IS_OUT_ACTIVE;
678
Simon Glass54befdd2021-02-04 21:21:55 -0700679 /* GPIOD_ are directly managed by driver in set_flags */
680 if (ops->set_flags) {
681 ret = ops->set_flags(dev, desc->offset, flags);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100682 } else {
683 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700684 bool value = flags & GPIOD_IS_OUT_ACTIVE;
685
686 ret = ops->direction_output(dev, desc->offset, value);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100687 } else if (flags & GPIOD_IS_IN) {
688 ret = ops->direction_input(dev, desc->offset);
689 }
Simon Glassce555292015-01-05 20:05:27 -0700690 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100691
692 return ret;
693}
694
Simon Glass7b893f92021-02-04 21:22:03 -0700695int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100696{
Simon Glass7b893f92021-02-04 21:22:03 -0700697 ulong flags;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100698 int ret;
699
700 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700701 if (ret)
702 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100703
Simon Glass7b893f92021-02-04 21:22:03 -0700704 flags = (desc->flags & ~clr) | set;
705
Simon Glass54befdd2021-02-04 21:21:55 -0700706 ret = _dm_gpio_set_flags(desc, flags);
Simon Glass7b893f92021-02-04 21:22:03 -0700707 if (ret)
708 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700709
Simon Glass7b893f92021-02-04 21:22:03 -0700710 /* save the flags also in descriptor */
711 desc->flags = flags;
712
713 return 0;
714}
715
716int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
717{
718 /* combine the requested flags (for IN/OUT) and the descriptor flags */
719 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700720}
721
Simon Glass247ccf22021-02-04 21:22:09 -0700722int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
723 ulong set)
724{
725 int ret;
726 int i;
727
728 for (i = 0; i < count; i++) {
729 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
730 if (ret)
731 return log_ret(ret);
732 }
733
734 return 0;
735}
736
Simon Glass909bee32021-02-04 21:21:57 -0700737int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100738{
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100739 struct udevice *dev = desc->dev;
740 int ret, value;
Simon Glass49f315a2021-02-04 21:22:05 -0700741 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glassd063ce92021-02-04 21:21:56 -0700742 ulong flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100743
Simon Glassd063ce92021-02-04 21:21:56 -0700744 ret = check_reserved(desc, "get_flags");
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100745 if (ret)
746 return ret;
747
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100748 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
Simon Glassd063ce92021-02-04 21:21:56 -0700749 if (ops->get_flags) {
750 ret = ops->get_flags(dev, desc->offset, &flags);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100751 if (ret)
752 return ret;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100753
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100754 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
Simon Glassd063ce92021-02-04 21:21:56 -0700755 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100756 if (desc->flags & GPIOD_ACTIVE_LOW)
757 value = !value;
Simon Glassd063ce92021-02-04 21:21:56 -0700758 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
759 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100760 if (value)
Simon Glassd063ce92021-02-04 21:21:56 -0700761 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100762 } else {
Simon Glassd063ce92021-02-04 21:21:56 -0700763 flags = desc->flags;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100764 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
Simon Glassd063ce92021-02-04 21:21:56 -0700765 flags &= ~GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100766 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
Simon Glassd063ce92021-02-04 21:21:56 -0700767 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100768 }
Simon Glassd063ce92021-02-04 21:21:56 -0700769 *flagsp = flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100770
771 return 0;
772}
773
Simon Glasse821d182014-02-26 15:59:24 -0700774/**
775 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
776 * gpio: GPIO number
777 *
778 * This function implements the API that's compatible with current
779 * GPIO API used in U-Boot. The request is forwarded to particular
780 * GPIO driver. Returns the value of the GPIO pin, or negative value
781 * on error.
782 */
783int gpio_get_value(unsigned gpio)
784{
Simon Glasse821d182014-02-26 15:59:24 -0700785 int ret;
786
Simon Glassce555292015-01-05 20:05:27 -0700787 struct gpio_desc desc;
788
789 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700790 if (ret)
791 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700792 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700793}
794
795/**
796 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
797 * gpio: GPIO number
798 * value: Logical value to be set on the GPIO pin.
799 *
800 * This function implements the API that's compatible with current
801 * GPIO API used in U-Boot. The request is forwarded to particular
802 * GPIO driver. Returns 0 on success, negative value on error.
803 */
804int gpio_set_value(unsigned gpio, int value)
805{
Simon Glassce555292015-01-05 20:05:27 -0700806 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700807 int ret;
808
Simon Glassce555292015-01-05 20:05:27 -0700809 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700810 if (ret)
811 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700812 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700813}
814
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200815const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700816{
817 struct gpio_dev_priv *priv;
818
819 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700820 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700821 assert(priv);
822
823 *bit_count = priv->gpio_count;
824 return priv->bank_name;
825}
826
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600827static const char * const gpio_function[GPIOF_COUNT] = {
828 "input",
829 "output",
830 "unused",
831 "unknown",
832 "func",
833};
834
Masahiro Yamada286c2522017-06-22 16:50:25 +0900835static int get_function(struct udevice *dev, int offset, bool skip_unused,
836 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600837{
Simon Glassde0977b2015-03-05 12:25:20 -0700838 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass49f315a2021-02-04 21:22:05 -0700839 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600840
841 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
842 if (!device_active(dev))
843 return -ENODEV;
844 if (offset < 0 || offset >= uc_priv->gpio_count)
845 return -EINVAL;
846 if (namep)
847 *namep = uc_priv->name[offset];
848 if (skip_unused && !uc_priv->name[offset])
849 return GPIOF_UNUSED;
850 if (ops->get_function) {
851 int ret;
852
853 ret = ops->get_function(dev, offset);
854 if (ret < 0)
855 return ret;
856 if (ret >= ARRAY_SIZE(gpio_function))
857 return -ENODATA;
858 return ret;
859 }
860
861 return GPIOF_UNKNOWN;
862}
863
864int gpio_get_function(struct udevice *dev, int offset, const char **namep)
865{
866 return get_function(dev, offset, true, namep);
867}
868
869int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
870{
871 return get_function(dev, offset, false, namep);
872}
873
Simon Glass6b1ef592014-10-04 11:29:44 -0600874int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
875{
Simon Glass49f315a2021-02-04 21:22:05 -0700876 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600877 struct gpio_dev_priv *priv;
878 char *str = buf;
879 int func;
880 int ret;
881 int len;
882
883 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
884
885 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700886 priv = dev_get_uclass_priv(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600887 ret = gpio_get_raw_function(dev, offset, NULL);
888 if (ret < 0)
889 return ret;
890 func = ret;
891 len = snprintf(str, buffsize, "%s%d: %s",
892 priv->bank_name ? priv->bank_name : "",
893 offset, gpio_function[func]);
894 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
895 func == GPIOF_UNUSED) {
896 const char *label;
897 bool used;
898
899 ret = ops->get_value(dev, offset);
900 if (ret < 0)
901 return ret;
902 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
903 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
904 ret,
905 used ? 'x' : ' ',
906 used ? " " : "",
907 label ? label : "");
908 }
909
910 return 0;
911}
912
Simon Glass3176b6c2020-07-07 13:11:44 -0600913#if CONFIG_IS_ENABLED(ACPIGEN)
914int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
915{
Simon Glass49f315a2021-02-04 21:22:05 -0700916 const struct dm_gpio_ops *ops;
Simon Glass3176b6c2020-07-07 13:11:44 -0600917
918 memset(gpio, '\0', sizeof(*gpio));
919 if (!dm_gpio_is_valid(desc)) {
920 /* Indicate that the GPIO is not valid */
921 gpio->pin_count = 0;
922 gpio->pins[0] = 0;
923 return -EINVAL;
924 }
925
926 ops = gpio_get_ops(desc->dev);
927 if (!ops->get_acpi)
928 return -ENOSYS;
929
930 return ops->get_acpi(desc, gpio);
931}
932#endif
933
Simon Glassbef54db2015-04-14 21:03:20 -0600934int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
935{
936 int i, ret;
937 int gpio;
938
939 for (i = 0; i < 32; i++) {
940 gpio = gpio_num_array[i];
941 if (gpio == -1)
942 break;
943 ret = gpio_requestf(gpio, fmt, i);
944 if (ret)
945 goto err;
946 ret = gpio_direction_input(gpio);
947 if (ret) {
948 gpio_free(gpio);
949 goto err;
950 }
951 }
952
953 return 0;
954err:
955 for (i--; i >= 0; i--)
956 gpio_free(gpio_num_array[i]);
957
958 return ret;
959}
960
Simon Glass2c97a8f2014-11-10 18:00:21 -0700961/*
962 * get a number comprised of multiple GPIO values. gpio_num_array points to
963 * the array of gpio pin numbers to scan, terminated by -1.
964 */
Simon Glassbef54db2015-04-14 21:03:20 -0600965int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700966{
967 int gpio;
968 unsigned bitmask = 1;
969 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600970 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700971
972 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600973 ((gpio = *gpio_list++) != -1)) {
974 ret = gpio_get_value(gpio);
975 if (ret < 0)
976 return ret;
977 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700978 vector |= bitmask;
979 bitmask <<= 1;
980 }
Simon Glassbef54db2015-04-14 21:03:20 -0600981
Simon Glass2c97a8f2014-11-10 18:00:21 -0700982 return vector;
983}
984
Simon Glassfd838972016-03-06 19:27:51 -0700985int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -0700986{
987 unsigned bitmask = 1;
988 unsigned vector = 0;
989 int ret, i;
990
991 for (i = 0; i < count; i++) {
992 ret = dm_gpio_get_value(&desc_list[i]);
993 if (ret < 0)
994 return ret;
995 else if (ret)
996 vector |= bitmask;
997 bitmask <<= 1;
998 }
Simon Glass247ccf22021-02-04 21:22:09 -0700999
1000 return vector;
1001}
1002
1003int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1004 int count)
1005{
1006 static const char tristate[] = "01z";
1007 enum {
1008 PULLUP,
1009 PULLDOWN,
1010
1011 NUM_OPTIONS,
1012 };
1013 int vals[NUM_OPTIONS];
1014 uint mask;
1015 uint vector = 0;
1016 int ret, i;
1017
1018 /*
1019 * Limit to 19 digits which should be plenty. This avoids overflow of a
1020 * 32-bit int
1021 */
1022 assert(count < 20);
1023
1024 for (i = 0; i < NUM_OPTIONS; i++) {
1025 uint flags = GPIOD_IS_IN;
1026
1027 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1028 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1029 flags);
1030 if (ret)
1031 return log_msg_ret("pu", ret);
1032
1033 /* Give the lines time to settle */
1034 udelay(10);
1035
1036 ret = dm_gpio_get_values_as_int(desc_list, count);
1037 if (ret < 0)
1038 return log_msg_ret("get1", ret);
1039 vals[i] = ret;
1040 }
1041
1042 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1043 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1044 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1045 uint pu = vals[PULLUP] & mask ? 1 : 0;
1046 uint digit;
1047
1048 /*
1049 * Get value with internal pulldown active. If this is 1 then
1050 * there is a stronger external pullup, which we call 1. If not
1051 * then call it 0.
1052 *
1053 * If the values differ then the pin is floating so we call
1054 * this a 2.
1055 */
1056 if (pu == pd)
1057 digit = pd;
1058 else
1059 digit = 2;
1060 log_debug("%c ", tristate[digit]);
1061 vector = 3 * vector + digit;
1062 }
1063 log_debug("vector=%d\n", vector);
Simon Glassdf1687d2016-03-06 19:27:50 -07001064
1065 return vector;
1066}
1067
Heiko Schocher58e4c382019-07-17 06:59:51 +02001068/**
1069 * gpio_request_tail: common work for requesting a gpio.
1070 *
1071 * ret: return value from previous work in function which calls
1072 * this function.
1073 * This seems bogus (why calling this function instead not
1074 * calling it and end caller function instead?).
1075 * Because on error in caller function we want to set some
1076 * default values in gpio desc and have a common error
1077 * debug message, which provides this function.
1078 * nodename: Name of node for which gpio gets requested
1079 * used for gpio label name.
1080 * args: pointer to output arguments structure
1081 * list_name: Name of GPIO list
1082 * used for gpio label name.
1083 * index: gpio index in gpio list
1084 * used for gpio label name.
1085 * desc: pointer to gpio descriptor, filled from this
1086 * function.
1087 * flags: gpio flags to use.
1088 * add_index: should index added to gpio label name
1089 * gpio_dev: pointer to gpio device from which the gpio
1090 * will be requested. If NULL try to get the
1091 * gpio device with uclass_get_device_by_ofnode()
1092 *
1093 * return: In error case this function sets default values in
1094 * gpio descriptor, also emmits a debug message.
1095 * On success it returns 0 else the error code from
1096 * function calls, or the error code passed through
1097 * ret to this function.
1098 *
1099 */
Heiko Schocher39cb3402019-06-12 06:11:46 +02001100static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -06001101 struct ofnode_phandle_args *args,
1102 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +02001103 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +02001104 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -07001105{
Patrick Delaunay758bba92020-01-13 11:35:01 +01001106 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -06001107 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -07001108 goto err;
Simon Glass16e10402015-01-05 20:05:29 -07001109
Heiko Schocher39cb3402019-06-12 06:11:46 +02001110 if (!desc->dev) {
1111 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1112 &desc->dev);
1113 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +02001114 debug("%s: uclass_get_device_by_ofnode failed\n",
1115 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001116 goto err;
1117 }
Simon Glass16e10402015-01-05 20:05:29 -07001118 }
Simon Glass12faa022017-05-18 20:09:18 -06001119 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -07001120 if (ret) {
1121 debug("%s: gpio_find_and_xlate failed\n", __func__);
1122 goto err;
1123 }
1124 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001125 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -07001126 if (ret) {
1127 debug("%s: dm_gpio_requestf failed\n", __func__);
1128 goto err;
1129 }
Simon Glass7b893f92021-02-04 21:22:03 -07001130
1131 /* Keep any direction flags provided by the devicetree */
1132 ret = dm_gpio_set_dir_flags(desc,
1133 flags | (desc->flags & GPIOD_MASK_DIR));
Simon Glass16e10402015-01-05 20:05:29 -07001134 if (ret) {
1135 debug("%s: dm_gpio_set_dir failed\n", __func__);
1136 goto err;
1137 }
1138
1139 return 0;
1140err:
1141 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001142 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -07001143 return ret;
1144}
1145
Simon Glass92882652021-08-07 07:24:04 -06001146#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass1d9af1f2017-05-30 21:47:09 -06001147static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1148 int index, struct gpio_desc *desc,
1149 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -06001150{
1151 struct ofnode_phandle_args args;
1152 int ret;
1153
Simon Glass1d9af1f2017-05-30 21:47:09 -06001154 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1155 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -06001156
Heiko Schocher39cb3402019-06-12 06:11:46 +02001157 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1158 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -06001159}
1160
Simon Glass1d9af1f2017-05-30 21:47:09 -06001161int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001162 struct gpio_desc *desc, int flags)
1163{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001164 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1165 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -07001166}
1167
Simon Glass1d9af1f2017-05-30 21:47:09 -06001168int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001169 struct gpio_desc *desc, int flags)
1170{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001171 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001172 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -06001173 int ret;
1174
1175 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1176 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001177 node = dev_ofnode(dev);
1178 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1179 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -07001180}
1181
Simon Glass1d9af1f2017-05-30 21:47:09 -06001182int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -07001183 struct gpio_desc *desc, int max_count,
1184 int flags)
1185{
1186 int count;
1187 int ret;
1188
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +02001189 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -06001190 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -07001191 &desc[count], flags, true);
1192 if (ret == -ENOENT)
1193 break;
1194 else if (ret)
1195 goto err;
1196 }
1197
1198 /* We ran out of GPIOs in the list */
1199 return count;
1200
1201err:
1202 gpio_free_list_nodev(desc, count - 1);
1203
1204 return ret;
1205}
1206
1207int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1208 struct gpio_desc *desc, int max_count,
1209 int flags)
1210{
1211 /*
1212 * This isn't ideal since we don't use dev->name in the debug()
1213 * calls in gpio_request_by_name(), but we can do this until
1214 * gpio_request_list_by_name_nodev() can be dropped.
1215 */
Simon Glass1d9af1f2017-05-30 21:47:09 -06001216 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1217 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -07001218}
1219
1220int gpio_get_list_count(struct udevice *dev, const char *list_name)
1221{
1222 int ret;
1223
Sean Anderson8ad66c52021-04-20 10:50:54 -04001224 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1225 -ENOENT);
1226 if (ret < 0) {
Simon Glass16e10402015-01-05 20:05:29 -07001227 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1228 __func__, dev->name, list_name, ret);
1229 }
1230
1231 return ret;
1232}
Simon Glassea383722021-02-04 21:21:54 -07001233#endif /* OF_PLATDATA */
Simon Glass16e10402015-01-05 20:05:29 -07001234
Simon Glass2149e112021-08-07 07:24:12 -06001235#if CONFIG_IS_ENABLED(OF_PLATDATA)
1236int gpio_request_by_phandle(struct udevice *dev,
1237 const struct phandle_2_arg *cells,
1238 struct gpio_desc *desc, int flags)
1239{
1240 struct ofnode_phandle_args args;
1241 struct udevice *gpio_dev;
1242 const int index = 0;
1243 int ret;
1244
1245 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1246 if (ret)
1247 return ret;
1248 args.args[0] = cells->arg[0];
1249 args.args[1] = cells->arg[1];
1250
1251 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1252 index > 0, gpio_dev);
1253}
1254#endif
1255
Simon Glass16e10402015-01-05 20:05:29 -07001256int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1257{
1258 /* For now, we don't do any checking of dev */
1259 return _dm_gpio_free(desc->dev, desc->offset);
1260}
1261
1262int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1263{
1264 int i;
1265
1266 /* For now, we don't do any checking of dev */
1267 for (i = 0; i < count; i++)
1268 dm_gpio_free(dev, &desc[i]);
1269
1270 return 0;
1271}
1272
1273int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1274{
1275 return gpio_free_list(NULL, desc, count);
1276}
1277
Simon Glasse821d182014-02-26 15:59:24 -07001278/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -06001279static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -07001280{
1281 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001282 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -07001283 struct uclass *uc;
1284 unsigned base;
1285 int ret;
1286
1287 ret = uclass_get(UCLASS_GPIO, &uc);
1288 if (ret)
1289 return ret;
1290
1291 /* Ensure that we have a base for each bank */
1292 base = 0;
1293 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -06001294 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -07001295 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001296 uc_priv->gpio_base = base;
1297 base += uc_priv->gpio_count;
1298 }
1299 }
1300
1301 return 0;
1302}
1303
Simon Glassfd838972016-03-06 19:27:51 -07001304int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -06001305{
1306 struct udevice *dev = desc->dev;
1307 struct gpio_dev_priv *uc_priv;
1308
1309 if (!dev)
1310 return -1;
Simon Glass95588622020-12-22 19:30:28 -07001311 uc_priv = dev_get_uclass_priv(dev);
Simon Glass94f54d12015-03-25 12:21:58 -06001312
1313 return uc_priv->gpio_base + desc->offset;
1314}
1315
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001316static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001317{
Simon Glassde0977b2015-03-05 12:25:20 -07001318 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001319
1320 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1321 if (!uc_priv->name)
1322 return -ENOMEM;
1323
1324 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001325}
1326
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001327static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001328{
Simon Glassde0977b2015-03-05 12:25:20 -07001329 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001330 int i;
1331
1332 for (i = 0; i < uc_priv->gpio_count; i++) {
1333 if (uc_priv->name[i])
1334 free(uc_priv->name[i]);
1335 }
1336 free(uc_priv->name);
1337
1338 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001339}
1340
Heiko Schocher39cb3402019-06-12 06:11:46 +02001341int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1342 char *list_name, int index, int flags,
1343 int dtflags, struct gpio_desc *desc)
1344{
1345 struct ofnode_phandle_args args;
1346
1347 args.node = ofnode_null();
1348 args.args_count = 2;
1349 args.args[0] = index;
1350 args.args[1] = dtflags;
1351
1352 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1353 flags, 0, dev);
1354}
1355
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +05301356static void devm_gpiod_release(struct udevice *dev, void *res)
1357{
1358 dm_gpio_free(dev, res);
1359}
1360
1361static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1362{
1363 return res == data;
1364}
1365
1366struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1367 unsigned int index, int flags)
1368{
1369 int rc;
1370 struct gpio_desc *desc;
1371 char *propname;
1372 static const char suffix[] = "-gpios";
1373
1374 propname = malloc(strlen(id) + sizeof(suffix));
1375 if (!propname) {
1376 rc = -ENOMEM;
1377 goto end;
1378 }
1379
1380 strcpy(propname, id);
1381 strcat(propname, suffix);
1382
1383 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1384 __GFP_ZERO);
1385 if (unlikely(!desc)) {
1386 rc = -ENOMEM;
1387 goto end;
1388 }
1389
1390 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1391
1392end:
1393 if (propname)
1394 free(propname);
1395
1396 if (rc)
1397 return ERR_PTR(rc);
1398
1399 devres_add(dev, desc);
1400
1401 return desc;
1402}
1403
1404struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1405 const char *id,
1406 unsigned int index,
1407 int flags)
1408{
1409 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1410
1411 if (IS_ERR(desc))
1412 return NULL;
1413
1414 return desc;
1415}
1416
1417void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1418{
1419 int rc;
1420
1421 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1422 WARN_ON(rc);
1423}
1424
Michal Simek5f7202c2018-07-12 12:42:27 +02001425static int gpio_post_bind(struct udevice *dev)
1426{
Heiko Schocher39cb3402019-06-12 06:11:46 +02001427 struct udevice *child;
1428 ofnode node;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001429
Michal Simek5f7202c2018-07-12 12:42:27 +02001430#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1431 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1432 static int reloc_done;
1433
1434 if (!reloc_done) {
1435 if (ops->request)
1436 ops->request += gd->reloc_off;
Simon Glassb3a47542020-02-04 20:15:17 -07001437 if (ops->rfree)
1438 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001439 if (ops->direction_input)
1440 ops->direction_input += gd->reloc_off;
1441 if (ops->direction_output)
1442 ops->direction_output += gd->reloc_off;
1443 if (ops->get_value)
1444 ops->get_value += gd->reloc_off;
1445 if (ops->set_value)
1446 ops->set_value += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001447 if (ops->get_function)
1448 ops->get_function += gd->reloc_off;
1449 if (ops->xlate)
1450 ops->xlate += gd->reloc_off;
Simon Glass54befdd2021-02-04 21:21:55 -07001451 if (ops->set_flags)
1452 ops->set_flags += gd->reloc_off;
Simon Glassd063ce92021-02-04 21:21:56 -07001453 if (ops->get_flags)
1454 ops->get_flags += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001455
1456 reloc_done++;
1457 }
1458#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001459
Simon Glass2149e112021-08-07 07:24:12 -06001460 if (CONFIG_IS_ENABLED(OF_REAL) && IS_ENABLED(CONFIG_GPIO_HOG)) {
Heiko Schocher58e4c382019-07-17 06:59:51 +02001461 dev_for_each_subnode(node, dev) {
1462 if (ofnode_read_bool(node, "gpio-hog")) {
1463 const char *name = ofnode_get_name(node);
1464 int ret;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001465
Heiko Schocher58e4c382019-07-17 06:59:51 +02001466 ret = device_bind_driver_to_node(dev,
1467 "gpio_hog",
1468 name, node,
1469 &child);
1470 if (ret)
1471 return ret;
1472 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001473 }
1474 }
Michal Simek5f7202c2018-07-12 12:42:27 +02001475 return 0;
1476}
1477
Simon Glasse821d182014-02-26 15:59:24 -07001478UCLASS_DRIVER(gpio) = {
1479 .id = UCLASS_GPIO,
1480 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301481 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001482 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001483 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001484 .pre_remove = gpio_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001485 .per_device_auto = sizeof(struct gpio_dev_priv),
Simon Glasse821d182014-02-26 15:59:24 -07001486};