blob: bb2f23241edba54f6baddc051c6ad14ee9cf09e5 [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))
144 break;
Simon Glasse821d182014-02-26 15:59:24 -0700145 }
Heiko Schochera3e793c2020-05-22 11:08:59 +0200146
147 /*
148 * if we did not found a gpio through its bank
149 * name, we search for a valid gpio label.
150 */
151 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
152 break;
Simon Glasse821d182014-02-26 15:59:24 -0700153 }
154
Simon Glass43b2e1a2014-10-22 21:37:01 -0600155 if (!dev)
156 return ret ? ret : -EINVAL;
157
Patrick Delaunay758bba92020-01-13 11:35:01 +0100158 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600159
160 return 0;
161}
162
163int gpio_lookup_name(const char *name, struct udevice **devp,
164 unsigned int *offsetp, unsigned int *gpiop)
165{
166 struct gpio_desc desc;
167 int ret;
168
169 if (devp)
170 *devp = NULL;
171 ret = dm_gpio_lookup_name(name, &desc);
172 if (ret)
173 return ret;
174
Simon Glass43b2e1a2014-10-22 21:37:01 -0600175 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600176 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600177 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600178 *offsetp = desc.offset;
179 if (gpiop) {
180 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
181
182 *gpiop = uc_priv->gpio_base + desc.offset;
183 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600184
185 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700186}
187
Simon Glass12faa022017-05-18 20:09:18 -0600188int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
189 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700190{
191 if (args->args_count < 1)
192 return -EINVAL;
193
194 desc->offset = args->args[0];
195
196 if (args->args_count < 2)
197 return 0;
198
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100199 desc->flags = 0;
Eric Nelson786e98d2016-04-24 16:32:40 -0700200 if (args->args[1] & GPIO_ACTIVE_LOW)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100201 desc->flags |= GPIOD_ACTIVE_LOW;
Eric Nelson786e98d2016-04-24 16:32:40 -0700202
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100203 /*
204 * need to test 2 bits for gpio output binding:
205 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
206 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
207 */
208 if (args->args[1] & GPIO_SINGLE_ENDED) {
209 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
210 desc->flags |= GPIOD_OPEN_DRAIN;
211 else
212 desc->flags |= GPIOD_OPEN_SOURCE;
213 }
214
215 if (args->args[1] & GPIO_PULL_UP)
216 desc->flags |= GPIOD_PULL_UP;
217
218 if (args->args[1] & GPIO_PULL_DOWN)
219 desc->flags |= GPIOD_PULL_DOWN;
220
Eric Nelson786e98d2016-04-24 16:32:40 -0700221 return 0;
222}
223
Simon Glass16e10402015-01-05 20:05:29 -0700224static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600225 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700226{
Simon Glass49f315a2021-02-04 21:22:05 -0700227 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassd3322bb2015-01-05 20:05:28 -0700228
Eric Nelson786e98d2016-04-24 16:32:40 -0700229 if (ops->xlate)
230 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700231 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700232 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700233}
234
Simon Glass2149e112021-08-07 07:24:12 -0600235#if CONFIG_IS_ENABLED(GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200236
237struct gpio_hog_priv {
238 struct gpio_desc gpiod;
239};
240
241struct gpio_hog_data {
242 int gpiod_flags;
243 int value;
244 u32 val[2];
245};
246
Simon Glassaad29ae2020-12-03 16:55:21 -0700247static int gpio_hog_of_to_plat(struct udevice *dev)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200248{
Simon Glassfa20e932020-12-03 16:55:20 -0700249 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200250 const char *nodename;
251 int ret;
252
253 plat->value = 0;
254 if (dev_read_bool(dev, "input")) {
255 plat->gpiod_flags = GPIOD_IS_IN;
256 } else if (dev_read_bool(dev, "output-high")) {
257 plat->value = 1;
258 plat->gpiod_flags = GPIOD_IS_OUT;
259 } else if (dev_read_bool(dev, "output-low")) {
260 plat->gpiod_flags = GPIOD_IS_OUT;
261 } else {
262 printf("%s: missing gpio-hog state.\n", __func__);
263 return -EINVAL;
264 }
265 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
266 if (ret) {
267 printf("%s: wrong gpios property, 2 values needed %d\n",
268 __func__, ret);
269 return ret;
270 }
271 nodename = dev_read_string(dev, "line-name");
Heiko Schocher58e4c382019-07-17 06:59:51 +0200272 if (nodename)
273 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200274
275 return 0;
276}
277
278static int gpio_hog_probe(struct udevice *dev)
279{
Simon Glassfa20e932020-12-03 16:55:20 -0700280 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200281 struct gpio_hog_priv *priv = dev_get_priv(dev);
282 int ret;
283
284 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
285 plat->val[0], plat->gpiod_flags,
286 plat->val[1], &priv->gpiod);
287 if (ret < 0) {
288 debug("%s: node %s could not get gpio.\n", __func__,
289 dev->name);
290 return ret;
291 }
Heiko Schocher58e4c382019-07-17 06:59:51 +0200292
293 if (plat->gpiod_flags == GPIOD_IS_OUT) {
294 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
295 if (ret < 0) {
296 debug("%s: node %s could not set gpio.\n", __func__,
297 dev->name);
298 return ret;
299 }
300 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200301
302 return 0;
303}
304
305int gpio_hog_probe_all(void)
306{
307 struct udevice *dev;
308 int ret;
Heiko Schocher58e4c382019-07-17 06:59:51 +0200309 int retval = 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200310
311 for (uclass_first_device(UCLASS_NOP, &dev);
312 dev;
313 uclass_find_next_device(&dev)) {
Simon Glass65130cd2020-12-28 20:34:56 -0700314 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
Heiko Schocher39cb3402019-06-12 06:11:46 +0200315 ret = device_probe(dev);
Heiko Schocher58e4c382019-07-17 06:59:51 +0200316 if (ret) {
317 printf("Failed to probe device %s err: %d\n",
318 dev->name, ret);
319 retval = ret;
320 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200321 }
322 }
323
Heiko Schocher58e4c382019-07-17 06:59:51 +0200324 return retval;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200325}
326
Heiko Schocher58e4c382019-07-17 06:59:51 +0200327int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200328{
329 struct udevice *dev;
330
Heiko Schocher58e4c382019-07-17 06:59:51 +0200331 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200332 gpio_hog_probe_all();
333 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
334 struct gpio_hog_priv *priv = dev_get_priv(dev);
335
Heiko Schocher58e4c382019-07-17 06:59:51 +0200336 *desc = &priv->gpiod;
337 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200338 }
339
Heiko Schocher58e4c382019-07-17 06:59:51 +0200340 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200341}
342
343U_BOOT_DRIVER(gpio_hog) = {
344 .name = "gpio_hog",
345 .id = UCLASS_NOP,
Simon Glassaad29ae2020-12-03 16:55:21 -0700346 .of_to_plat = gpio_hog_of_to_plat,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200347 .probe = gpio_hog_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700348 .priv_auto = sizeof(struct gpio_hog_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700349 .plat_auto = sizeof(struct gpio_hog_data),
Heiko Schocher39cb3402019-06-12 06:11:46 +0200350};
351#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200352int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200353{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200354 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200355}
356#endif
357
Simon Glass047cdb32015-06-23 15:38:41 -0600358int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700359{
Simon Glass49f315a2021-02-04 21:22:05 -0700360 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700361 struct udevice *dev = desc->dev;
362 struct gpio_dev_priv *uc_priv;
363 char *str;
364 int ret;
365
Simon Glassde0977b2015-03-05 12:25:20 -0700366 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700367 if (uc_priv->name[desc->offset])
368 return -EBUSY;
369 str = strdup(label);
370 if (!str)
371 return -ENOMEM;
Simon Glass49f315a2021-02-04 21:22:05 -0700372 if (ops->request) {
373 ret = ops->request(dev, desc->offset, label);
Simon Glassce555292015-01-05 20:05:27 -0700374 if (ret) {
375 free(str);
376 return ret;
377 }
378 }
379 uc_priv->name[desc->offset] = str;
380
381 return 0;
382}
383
Simon Glass16e10402015-01-05 20:05:29 -0700384static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
385{
Simon Glass7611ac62019-09-25 08:56:27 -0600386#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700387 va_list args;
388 char buf[40];
389
390 va_start(args, fmt);
391 vscnprintf(buf, sizeof(buf), fmt, args);
392 va_end(args);
393 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700394#else
395 return dm_gpio_request(desc, fmt);
396#endif
Simon Glass16e10402015-01-05 20:05:29 -0700397}
398
Simon Glasse821d182014-02-26 15:59:24 -0700399/**
400 * gpio_request() - [COMPAT] Request GPIO
401 * gpio: GPIO number
402 * label: Name for the requested GPIO
403 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600404 * The label is copied and allocated so the caller does not need to keep
405 * the pointer around.
406 *
Simon Glasse821d182014-02-26 15:59:24 -0700407 * This function implements the API that's compatible with current
408 * GPIO API used in U-Boot. The request is forwarded to particular
409 * GPIO driver. Returns 0 on success, negative value on error.
410 */
411int gpio_request(unsigned gpio, const char *label)
412{
Simon Glassce555292015-01-05 20:05:27 -0700413 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700414 int ret;
415
Simon Glassce555292015-01-05 20:05:27 -0700416 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700417 if (ret)
418 return ret;
419
Simon Glassce555292015-01-05 20:05:27 -0700420 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700421}
422
423/**
Simon Glass1b27d602014-10-04 11:29:49 -0600424 * gpio_requestf() - [COMPAT] Request GPIO
425 * @gpio: GPIO number
426 * @fmt: Format string for the requested GPIO
427 * @...: Arguments for the printf() format string
428 *
429 * This function implements the API that's compatible with current
430 * GPIO API used in U-Boot. The request is forwarded to particular
431 * GPIO driver. Returns 0 on success, negative value on error.
432 */
433int gpio_requestf(unsigned gpio, const char *fmt, ...)
434{
Simon Glass7611ac62019-09-25 08:56:27 -0600435#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600436 va_list args;
437 char buf[40];
438
439 va_start(args, fmt);
440 vscnprintf(buf, sizeof(buf), fmt, args);
441 va_end(args);
442 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700443#else
444 return gpio_request(gpio, fmt);
445#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600446}
447
Simon Glassce555292015-01-05 20:05:27 -0700448int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700449{
Simon Glass49f315a2021-02-04 21:22:05 -0700450 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600451 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700452 int ret;
453
Simon Glassde0977b2015-03-05 12:25:20 -0700454 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600455 if (!uc_priv->name[offset])
456 return -ENXIO;
Simon Glass49f315a2021-02-04 21:22:05 -0700457 if (ops->rfree) {
458 ret = ops->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600459 if (ret)
460 return ret;
461 }
462
463 free(uc_priv->name[offset]);
464 uc_priv->name[offset] = NULL;
465
466 return 0;
467}
468
Simon Glassce555292015-01-05 20:05:27 -0700469/**
470 * gpio_free() - [COMPAT] Relinquish GPIO
471 * gpio: GPIO number
472 *
473 * This function implements the API that's compatible with current
474 * GPIO API used in U-Boot. The request is forwarded to particular
475 * GPIO driver. Returns 0 on success, negative value on error.
476 */
477int gpio_free(unsigned gpio)
478{
479 struct gpio_desc desc;
480 int ret;
481
482 ret = gpio_to_device(gpio, &desc);
483 if (ret)
484 return ret;
485
486 return _dm_gpio_free(desc.dev, desc.offset);
487}
488
Simon Glassfd838972016-03-06 19:27:51 -0700489static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600490{
Simon Glass230c1432015-07-02 18:16:16 -0600491 struct gpio_dev_priv *uc_priv;
492
493 if (!dm_gpio_is_valid(desc))
494 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600495
Simon Glass230c1432015-07-02 18:16:16 -0600496 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700497 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600498 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700499 desc->dev->name, func,
500 uc_priv->bank_name ? uc_priv->bank_name : "",
501 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600502 return -EBUSY;
503 }
504
505 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700506}
507
508/**
509 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
510 * gpio: GPIO number
511 *
512 * This function implements the API that's compatible with current
513 * GPIO API used in U-Boot. The request is forwarded to particular
514 * GPIO driver. Returns 0 on success, negative value on error.
515 */
516int gpio_direction_input(unsigned gpio)
517{
Simon Glassce555292015-01-05 20:05:27 -0700518 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700519 int ret;
520
Simon Glassce555292015-01-05 20:05:27 -0700521 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700522 if (ret)
523 return ret;
524
Simon Glass722f9682021-02-04 21:22:04 -0700525 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
Simon Glasse821d182014-02-26 15:59:24 -0700526}
527
528/**
529 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
530 * gpio: GPIO number
531 * value: Logical value to be set on the GPIO pin
532 *
533 * This function implements the API that's compatible with current
534 * GPIO API used in U-Boot. The request is forwarded to particular
535 * GPIO driver. Returns 0 on success, negative value on error.
536 */
537int gpio_direction_output(unsigned gpio, int value)
538{
Simon Glassce555292015-01-05 20:05:27 -0700539 struct gpio_desc desc;
Simon Glass722f9682021-02-04 21:22:04 -0700540 ulong flags;
Simon Glassce555292015-01-05 20:05:27 -0700541 int ret;
542
543 ret = gpio_to_device(gpio, &desc);
544 if (ret)
545 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700546
Simon Glass722f9682021-02-04 21:22:04 -0700547 flags = GPIOD_IS_OUT;
548 if (value)
549 flags |= GPIOD_IS_OUT_ACTIVE;
550 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700551}
552
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100553static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700554{
Simon Glass49f315a2021-02-04 21:22:05 -0700555 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700556 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100557
Simon Glass49f315a2021-02-04 21:22:05 -0700558 value = ops->get_value(desc->dev, desc->offset);
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100559
560 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
561}
562
563int dm_gpio_get_value(const struct gpio_desc *desc)
564{
Simon Glassce555292015-01-05 20:05:27 -0700565 int ret;
566
567 ret = check_reserved(desc, "get_value");
568 if (ret)
569 return ret;
570
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100571 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700572}
573
Simon Glassfd838972016-03-06 19:27:51 -0700574int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700575{
Simon Glass7b893f92021-02-04 21:22:03 -0700576 const struct dm_gpio_ops *ops;
Simon Glassce555292015-01-05 20:05:27 -0700577 int ret;
578
579 ret = check_reserved(desc, "set_value");
580 if (ret)
581 return ret;
582
583 if (desc->flags & GPIOD_ACTIVE_LOW)
584 value = !value;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200585
Simon Glass7b893f92021-02-04 21:22:03 -0700586 /* GPIOD_ are directly managed by driver in set_flags */
587 ops = gpio_get_ops(desc->dev);
588 if (ops->set_flags) {
589 ulong flags = desc->flags;
590
591 if (value)
592 flags |= GPIOD_IS_OUT_ACTIVE;
593 else
594 flags &= ~GPIOD_IS_OUT_ACTIVE;
595 return ops->set_flags(desc->dev, desc->offset, flags);
596 }
597
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200598 /*
599 * Emulate open drain by not actively driving the line high or
600 * Emulate open source by not actively driving the line low
601 */
602 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
603 (desc->flags & GPIOD_OPEN_SOURCE && !value))
Simon Glass7b893f92021-02-04 21:22:03 -0700604 return ops->direction_input(desc->dev, desc->offset);
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200605 else if (desc->flags & GPIOD_OPEN_DRAIN ||
606 desc->flags & GPIOD_OPEN_SOURCE)
Simon Glass7b893f92021-02-04 21:22:03 -0700607 return ops->direction_output(desc->dev, desc->offset, value);
608
609 ret = ops->set_value(desc->dev, desc->offset, value);
610 if (ret)
611 return ret;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200612
Simon Glassce555292015-01-05 20:05:27 -0700613 return 0;
614}
615
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100616/* check dir flags invalid configuration */
617static int check_dir_flags(ulong flags)
618{
619 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
620 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
621 __func__, flags);
622 return -EINVAL;
623 }
624
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100625 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
626 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
627 __func__, flags);
628 return -EINVAL;
629 }
630
631 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
632 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
633 __func__, flags);
634 return -EINVAL;
635 }
636
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100637 return 0;
638}
639
Simon Glass7b893f92021-02-04 21:22:03 -0700640/**
641 * _dm_gpio_set_flags() - Send flags to the driver
642 *
643 * This uses the best available method to send the given flags to the driver.
644 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
645 * of GPIOD_IS_OUT_ACTIVE.
646 *
647 * @desc: GPIO description
648 * @flags: flags value to set
649 * @return 0 if OK, -ve on error
650 */
Simon Glass54befdd2021-02-04 21:21:55 -0700651static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700652{
653 struct udevice *dev = desc->dev;
Simon Glass49f315a2021-02-04 21:22:05 -0700654 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100655 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100656 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700657
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100658 ret = check_dir_flags(flags);
659 if (ret) {
660 dev_dbg(dev,
661 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
662 desc->dev->name,
663 uc_priv->bank_name ? uc_priv->bank_name : "",
664 desc->offset, flags);
665
666 return ret;
667 }
668
Simon Glass7b893f92021-02-04 21:22:03 -0700669 /* If active low, invert the output state */
670 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
671 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
672 flags ^= GPIOD_IS_OUT_ACTIVE;
673
Simon Glass54befdd2021-02-04 21:21:55 -0700674 /* GPIOD_ are directly managed by driver in set_flags */
675 if (ops->set_flags) {
676 ret = ops->set_flags(dev, desc->offset, flags);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100677 } else {
678 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700679 bool value = flags & GPIOD_IS_OUT_ACTIVE;
680
681 ret = ops->direction_output(dev, desc->offset, value);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100682 } else if (flags & GPIOD_IS_IN) {
683 ret = ops->direction_input(dev, desc->offset);
684 }
Simon Glassce555292015-01-05 20:05:27 -0700685 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100686
687 return ret;
688}
689
Simon Glass7b893f92021-02-04 21:22:03 -0700690int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100691{
Simon Glass7b893f92021-02-04 21:22:03 -0700692 ulong flags;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100693 int ret;
694
695 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700696 if (ret)
697 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100698
Simon Glass7b893f92021-02-04 21:22:03 -0700699 flags = (desc->flags & ~clr) | set;
700
Simon Glass54befdd2021-02-04 21:21:55 -0700701 ret = _dm_gpio_set_flags(desc, flags);
Simon Glass7b893f92021-02-04 21:22:03 -0700702 if (ret)
703 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700704
Simon Glass7b893f92021-02-04 21:22:03 -0700705 /* save the flags also in descriptor */
706 desc->flags = flags;
707
708 return 0;
709}
710
711int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
712{
713 /* combine the requested flags (for IN/OUT) and the descriptor flags */
714 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700715}
716
Simon Glass247ccf22021-02-04 21:22:09 -0700717int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
718 ulong set)
719{
720 int ret;
721 int i;
722
723 for (i = 0; i < count; i++) {
724 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
725 if (ret)
726 return log_ret(ret);
727 }
728
729 return 0;
730}
731
Simon Glass909bee32021-02-04 21:21:57 -0700732int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100733{
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100734 struct udevice *dev = desc->dev;
735 int ret, value;
Simon Glass49f315a2021-02-04 21:22:05 -0700736 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glassd063ce92021-02-04 21:21:56 -0700737 ulong flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100738
Simon Glassd063ce92021-02-04 21:21:56 -0700739 ret = check_reserved(desc, "get_flags");
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100740 if (ret)
741 return ret;
742
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100743 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
Simon Glassd063ce92021-02-04 21:21:56 -0700744 if (ops->get_flags) {
745 ret = ops->get_flags(dev, desc->offset, &flags);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100746 if (ret)
747 return ret;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100748
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100749 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
Simon Glassd063ce92021-02-04 21:21:56 -0700750 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100751 if (desc->flags & GPIOD_ACTIVE_LOW)
752 value = !value;
Simon Glassd063ce92021-02-04 21:21:56 -0700753 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
754 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100755 if (value)
Simon Glassd063ce92021-02-04 21:21:56 -0700756 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100757 } else {
Simon Glassd063ce92021-02-04 21:21:56 -0700758 flags = desc->flags;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100759 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
Simon Glassd063ce92021-02-04 21:21:56 -0700760 flags &= ~GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100761 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
Simon Glassd063ce92021-02-04 21:21:56 -0700762 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100763 }
Simon Glassd063ce92021-02-04 21:21:56 -0700764 *flagsp = flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100765
766 return 0;
767}
768
Simon Glasse821d182014-02-26 15:59:24 -0700769/**
770 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
771 * gpio: GPIO number
772 *
773 * This function implements the API that's compatible with current
774 * GPIO API used in U-Boot. The request is forwarded to particular
775 * GPIO driver. Returns the value of the GPIO pin, or negative value
776 * on error.
777 */
778int gpio_get_value(unsigned gpio)
779{
Simon Glasse821d182014-02-26 15:59:24 -0700780 int ret;
781
Simon Glassce555292015-01-05 20:05:27 -0700782 struct gpio_desc desc;
783
784 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700785 if (ret)
786 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700787 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700788}
789
790/**
791 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
792 * gpio: GPIO number
793 * value: Logical value to be set on the GPIO pin.
794 *
795 * This function implements the API that's compatible with current
796 * GPIO API used in U-Boot. The request is forwarded to particular
797 * GPIO driver. Returns 0 on success, negative value on error.
798 */
799int gpio_set_value(unsigned gpio, int value)
800{
Simon Glassce555292015-01-05 20:05:27 -0700801 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700802 int ret;
803
Simon Glassce555292015-01-05 20:05:27 -0700804 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700805 if (ret)
806 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700807 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700808}
809
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200810const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700811{
812 struct gpio_dev_priv *priv;
813
814 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700815 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700816 assert(priv);
817
818 *bit_count = priv->gpio_count;
819 return priv->bank_name;
820}
821
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600822static const char * const gpio_function[GPIOF_COUNT] = {
823 "input",
824 "output",
825 "unused",
826 "unknown",
827 "func",
828};
829
Masahiro Yamada286c2522017-06-22 16:50:25 +0900830static int get_function(struct udevice *dev, int offset, bool skip_unused,
831 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600832{
Simon Glassde0977b2015-03-05 12:25:20 -0700833 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass49f315a2021-02-04 21:22:05 -0700834 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600835
836 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
837 if (!device_active(dev))
838 return -ENODEV;
839 if (offset < 0 || offset >= uc_priv->gpio_count)
840 return -EINVAL;
841 if (namep)
842 *namep = uc_priv->name[offset];
843 if (skip_unused && !uc_priv->name[offset])
844 return GPIOF_UNUSED;
845 if (ops->get_function) {
846 int ret;
847
848 ret = ops->get_function(dev, offset);
849 if (ret < 0)
850 return ret;
851 if (ret >= ARRAY_SIZE(gpio_function))
852 return -ENODATA;
853 return ret;
854 }
855
856 return GPIOF_UNKNOWN;
857}
858
859int gpio_get_function(struct udevice *dev, int offset, const char **namep)
860{
861 return get_function(dev, offset, true, namep);
862}
863
864int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
865{
866 return get_function(dev, offset, false, namep);
867}
868
Simon Glass6b1ef592014-10-04 11:29:44 -0600869int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
870{
Simon Glass49f315a2021-02-04 21:22:05 -0700871 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600872 struct gpio_dev_priv *priv;
873 char *str = buf;
874 int func;
875 int ret;
876 int len;
877
878 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
879
880 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700881 priv = dev_get_uclass_priv(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600882 ret = gpio_get_raw_function(dev, offset, NULL);
883 if (ret < 0)
884 return ret;
885 func = ret;
886 len = snprintf(str, buffsize, "%s%d: %s",
887 priv->bank_name ? priv->bank_name : "",
888 offset, gpio_function[func]);
889 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
890 func == GPIOF_UNUSED) {
891 const char *label;
892 bool used;
893
894 ret = ops->get_value(dev, offset);
895 if (ret < 0)
896 return ret;
897 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
898 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
899 ret,
900 used ? 'x' : ' ',
901 used ? " " : "",
902 label ? label : "");
903 }
904
905 return 0;
906}
907
Simon Glass3176b6c2020-07-07 13:11:44 -0600908#if CONFIG_IS_ENABLED(ACPIGEN)
909int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
910{
Simon Glass49f315a2021-02-04 21:22:05 -0700911 const struct dm_gpio_ops *ops;
Simon Glass3176b6c2020-07-07 13:11:44 -0600912
913 memset(gpio, '\0', sizeof(*gpio));
914 if (!dm_gpio_is_valid(desc)) {
915 /* Indicate that the GPIO is not valid */
916 gpio->pin_count = 0;
917 gpio->pins[0] = 0;
918 return -EINVAL;
919 }
920
921 ops = gpio_get_ops(desc->dev);
922 if (!ops->get_acpi)
923 return -ENOSYS;
924
925 return ops->get_acpi(desc, gpio);
926}
927#endif
928
Simon Glassbef54db2015-04-14 21:03:20 -0600929int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
930{
931 int i, ret;
932 int gpio;
933
934 for (i = 0; i < 32; i++) {
935 gpio = gpio_num_array[i];
936 if (gpio == -1)
937 break;
938 ret = gpio_requestf(gpio, fmt, i);
939 if (ret)
940 goto err;
941 ret = gpio_direction_input(gpio);
942 if (ret) {
943 gpio_free(gpio);
944 goto err;
945 }
946 }
947
948 return 0;
949err:
950 for (i--; i >= 0; i--)
951 gpio_free(gpio_num_array[i]);
952
953 return ret;
954}
955
Simon Glass2c97a8f2014-11-10 18:00:21 -0700956/*
957 * get a number comprised of multiple GPIO values. gpio_num_array points to
958 * the array of gpio pin numbers to scan, terminated by -1.
959 */
Simon Glassbef54db2015-04-14 21:03:20 -0600960int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700961{
962 int gpio;
963 unsigned bitmask = 1;
964 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600965 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700966
967 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600968 ((gpio = *gpio_list++) != -1)) {
969 ret = gpio_get_value(gpio);
970 if (ret < 0)
971 return ret;
972 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700973 vector |= bitmask;
974 bitmask <<= 1;
975 }
Simon Glassbef54db2015-04-14 21:03:20 -0600976
Simon Glass2c97a8f2014-11-10 18:00:21 -0700977 return vector;
978}
979
Simon Glassfd838972016-03-06 19:27:51 -0700980int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -0700981{
982 unsigned bitmask = 1;
983 unsigned vector = 0;
984 int ret, i;
985
986 for (i = 0; i < count; i++) {
987 ret = dm_gpio_get_value(&desc_list[i]);
988 if (ret < 0)
989 return ret;
990 else if (ret)
991 vector |= bitmask;
992 bitmask <<= 1;
993 }
Simon Glass247ccf22021-02-04 21:22:09 -0700994
995 return vector;
996}
997
998int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
999 int count)
1000{
1001 static const char tristate[] = "01z";
1002 enum {
1003 PULLUP,
1004 PULLDOWN,
1005
1006 NUM_OPTIONS,
1007 };
1008 int vals[NUM_OPTIONS];
1009 uint mask;
1010 uint vector = 0;
1011 int ret, i;
1012
1013 /*
1014 * Limit to 19 digits which should be plenty. This avoids overflow of a
1015 * 32-bit int
1016 */
1017 assert(count < 20);
1018
1019 for (i = 0; i < NUM_OPTIONS; i++) {
1020 uint flags = GPIOD_IS_IN;
1021
1022 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1023 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1024 flags);
1025 if (ret)
1026 return log_msg_ret("pu", ret);
1027
1028 /* Give the lines time to settle */
1029 udelay(10);
1030
1031 ret = dm_gpio_get_values_as_int(desc_list, count);
1032 if (ret < 0)
1033 return log_msg_ret("get1", ret);
1034 vals[i] = ret;
1035 }
1036
1037 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1038 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1039 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1040 uint pu = vals[PULLUP] & mask ? 1 : 0;
1041 uint digit;
1042
1043 /*
1044 * Get value with internal pulldown active. If this is 1 then
1045 * there is a stronger external pullup, which we call 1. If not
1046 * then call it 0.
1047 *
1048 * If the values differ then the pin is floating so we call
1049 * this a 2.
1050 */
1051 if (pu == pd)
1052 digit = pd;
1053 else
1054 digit = 2;
1055 log_debug("%c ", tristate[digit]);
1056 vector = 3 * vector + digit;
1057 }
1058 log_debug("vector=%d\n", vector);
Simon Glassdf1687d2016-03-06 19:27:50 -07001059
1060 return vector;
1061}
1062
Heiko Schocher58e4c382019-07-17 06:59:51 +02001063/**
1064 * gpio_request_tail: common work for requesting a gpio.
1065 *
1066 * ret: return value from previous work in function which calls
1067 * this function.
1068 * This seems bogus (why calling this function instead not
1069 * calling it and end caller function instead?).
1070 * Because on error in caller function we want to set some
1071 * default values in gpio desc and have a common error
1072 * debug message, which provides this function.
1073 * nodename: Name of node for which gpio gets requested
1074 * used for gpio label name.
1075 * args: pointer to output arguments structure
1076 * list_name: Name of GPIO list
1077 * used for gpio label name.
1078 * index: gpio index in gpio list
1079 * used for gpio label name.
1080 * desc: pointer to gpio descriptor, filled from this
1081 * function.
1082 * flags: gpio flags to use.
1083 * add_index: should index added to gpio label name
1084 * gpio_dev: pointer to gpio device from which the gpio
1085 * will be requested. If NULL try to get the
1086 * gpio device with uclass_get_device_by_ofnode()
1087 *
1088 * return: In error case this function sets default values in
1089 * gpio descriptor, also emmits a debug message.
1090 * On success it returns 0 else the error code from
1091 * function calls, or the error code passed through
1092 * ret to this function.
1093 *
1094 */
Heiko Schocher39cb3402019-06-12 06:11:46 +02001095static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -06001096 struct ofnode_phandle_args *args,
1097 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +02001098 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +02001099 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -07001100{
Patrick Delaunay758bba92020-01-13 11:35:01 +01001101 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -06001102 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -07001103 goto err;
Simon Glass16e10402015-01-05 20:05:29 -07001104
Heiko Schocher39cb3402019-06-12 06:11:46 +02001105 if (!desc->dev) {
1106 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1107 &desc->dev);
1108 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +02001109 debug("%s: uclass_get_device_by_ofnode failed\n",
1110 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001111 goto err;
1112 }
Simon Glass16e10402015-01-05 20:05:29 -07001113 }
Simon Glass12faa022017-05-18 20:09:18 -06001114 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -07001115 if (ret) {
1116 debug("%s: gpio_find_and_xlate failed\n", __func__);
1117 goto err;
1118 }
1119 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001120 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -07001121 if (ret) {
1122 debug("%s: dm_gpio_requestf failed\n", __func__);
1123 goto err;
1124 }
Simon Glass7b893f92021-02-04 21:22:03 -07001125
1126 /* Keep any direction flags provided by the devicetree */
1127 ret = dm_gpio_set_dir_flags(desc,
1128 flags | (desc->flags & GPIOD_MASK_DIR));
Simon Glass16e10402015-01-05 20:05:29 -07001129 if (ret) {
1130 debug("%s: dm_gpio_set_dir failed\n", __func__);
1131 goto err;
1132 }
1133
1134 return 0;
1135err:
1136 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001137 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -07001138 return ret;
1139}
1140
Simon Glass92882652021-08-07 07:24:04 -06001141#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass1d9af1f2017-05-30 21:47:09 -06001142static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1143 int index, struct gpio_desc *desc,
1144 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -06001145{
1146 struct ofnode_phandle_args args;
1147 int ret;
1148
Simon Glass1d9af1f2017-05-30 21:47:09 -06001149 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1150 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -06001151
Heiko Schocher39cb3402019-06-12 06:11:46 +02001152 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1153 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -06001154}
1155
Simon Glass1d9af1f2017-05-30 21:47:09 -06001156int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001157 struct gpio_desc *desc, int flags)
1158{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001159 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1160 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -07001161}
1162
Simon Glass1d9af1f2017-05-30 21:47:09 -06001163int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001164 struct gpio_desc *desc, int flags)
1165{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001166 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001167 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -06001168 int ret;
1169
1170 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1171 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001172 node = dev_ofnode(dev);
1173 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1174 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -07001175}
1176
Simon Glass1d9af1f2017-05-30 21:47:09 -06001177int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -07001178 struct gpio_desc *desc, int max_count,
1179 int flags)
1180{
1181 int count;
1182 int ret;
1183
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +02001184 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -06001185 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -07001186 &desc[count], flags, true);
1187 if (ret == -ENOENT)
1188 break;
1189 else if (ret)
1190 goto err;
1191 }
1192
1193 /* We ran out of GPIOs in the list */
1194 return count;
1195
1196err:
1197 gpio_free_list_nodev(desc, count - 1);
1198
1199 return ret;
1200}
1201
1202int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1203 struct gpio_desc *desc, int max_count,
1204 int flags)
1205{
1206 /*
1207 * This isn't ideal since we don't use dev->name in the debug()
1208 * calls in gpio_request_by_name(), but we can do this until
1209 * gpio_request_list_by_name_nodev() can be dropped.
1210 */
Simon Glass1d9af1f2017-05-30 21:47:09 -06001211 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1212 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -07001213}
1214
1215int gpio_get_list_count(struct udevice *dev, const char *list_name)
1216{
1217 int ret;
1218
Sean Anderson8ad66c52021-04-20 10:50:54 -04001219 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1220 -ENOENT);
1221 if (ret < 0) {
Simon Glass16e10402015-01-05 20:05:29 -07001222 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1223 __func__, dev->name, list_name, ret);
1224 }
1225
1226 return ret;
1227}
Simon Glassea383722021-02-04 21:21:54 -07001228#endif /* OF_PLATDATA */
Simon Glass16e10402015-01-05 20:05:29 -07001229
Simon Glass2149e112021-08-07 07:24:12 -06001230#if CONFIG_IS_ENABLED(OF_PLATDATA)
1231int gpio_request_by_phandle(struct udevice *dev,
1232 const struct phandle_2_arg *cells,
1233 struct gpio_desc *desc, int flags)
1234{
1235 struct ofnode_phandle_args args;
1236 struct udevice *gpio_dev;
1237 const int index = 0;
1238 int ret;
1239
1240 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1241 if (ret)
1242 return ret;
1243 args.args[0] = cells->arg[0];
1244 args.args[1] = cells->arg[1];
1245
1246 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1247 index > 0, gpio_dev);
1248}
1249#endif
1250
Simon Glass16e10402015-01-05 20:05:29 -07001251int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1252{
1253 /* For now, we don't do any checking of dev */
1254 return _dm_gpio_free(desc->dev, desc->offset);
1255}
1256
1257int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1258{
1259 int i;
1260
1261 /* For now, we don't do any checking of dev */
1262 for (i = 0; i < count; i++)
1263 dm_gpio_free(dev, &desc[i]);
1264
1265 return 0;
1266}
1267
1268int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1269{
1270 return gpio_free_list(NULL, desc, count);
1271}
1272
Simon Glasse821d182014-02-26 15:59:24 -07001273/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -06001274static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -07001275{
1276 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001277 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -07001278 struct uclass *uc;
1279 unsigned base;
1280 int ret;
1281
1282 ret = uclass_get(UCLASS_GPIO, &uc);
1283 if (ret)
1284 return ret;
1285
1286 /* Ensure that we have a base for each bank */
1287 base = 0;
1288 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -06001289 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -07001290 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001291 uc_priv->gpio_base = base;
1292 base += uc_priv->gpio_count;
1293 }
1294 }
1295
1296 return 0;
1297}
1298
Simon Glassfd838972016-03-06 19:27:51 -07001299int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -06001300{
1301 struct udevice *dev = desc->dev;
1302 struct gpio_dev_priv *uc_priv;
1303
1304 if (!dev)
1305 return -1;
Simon Glass95588622020-12-22 19:30:28 -07001306 uc_priv = dev_get_uclass_priv(dev);
Simon Glass94f54d12015-03-25 12:21:58 -06001307
1308 return uc_priv->gpio_base + desc->offset;
1309}
1310
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001311static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001312{
Simon Glassde0977b2015-03-05 12:25:20 -07001313 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001314
1315 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1316 if (!uc_priv->name)
1317 return -ENOMEM;
1318
1319 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001320}
1321
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001322static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001323{
Simon Glassde0977b2015-03-05 12:25:20 -07001324 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001325 int i;
1326
1327 for (i = 0; i < uc_priv->gpio_count; i++) {
1328 if (uc_priv->name[i])
1329 free(uc_priv->name[i]);
1330 }
1331 free(uc_priv->name);
1332
1333 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001334}
1335
Heiko Schocher39cb3402019-06-12 06:11:46 +02001336int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1337 char *list_name, int index, int flags,
1338 int dtflags, struct gpio_desc *desc)
1339{
1340 struct ofnode_phandle_args args;
1341
1342 args.node = ofnode_null();
1343 args.args_count = 2;
1344 args.args[0] = index;
1345 args.args[1] = dtflags;
1346
1347 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1348 flags, 0, dev);
1349}
1350
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +05301351static void devm_gpiod_release(struct udevice *dev, void *res)
1352{
1353 dm_gpio_free(dev, res);
1354}
1355
1356static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1357{
1358 return res == data;
1359}
1360
1361struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1362 unsigned int index, int flags)
1363{
1364 int rc;
1365 struct gpio_desc *desc;
1366 char *propname;
1367 static const char suffix[] = "-gpios";
1368
1369 propname = malloc(strlen(id) + sizeof(suffix));
1370 if (!propname) {
1371 rc = -ENOMEM;
1372 goto end;
1373 }
1374
1375 strcpy(propname, id);
1376 strcat(propname, suffix);
1377
1378 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1379 __GFP_ZERO);
1380 if (unlikely(!desc)) {
1381 rc = -ENOMEM;
1382 goto end;
1383 }
1384
1385 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1386
1387end:
1388 if (propname)
1389 free(propname);
1390
1391 if (rc)
1392 return ERR_PTR(rc);
1393
1394 devres_add(dev, desc);
1395
1396 return desc;
1397}
1398
1399struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1400 const char *id,
1401 unsigned int index,
1402 int flags)
1403{
1404 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1405
1406 if (IS_ERR(desc))
1407 return NULL;
1408
1409 return desc;
1410}
1411
1412void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1413{
1414 int rc;
1415
1416 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1417 WARN_ON(rc);
1418}
1419
Michal Simek5f7202c2018-07-12 12:42:27 +02001420static int gpio_post_bind(struct udevice *dev)
1421{
Heiko Schocher39cb3402019-06-12 06:11:46 +02001422 struct udevice *child;
1423 ofnode node;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001424
Michal Simek5f7202c2018-07-12 12:42:27 +02001425#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1426 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1427 static int reloc_done;
1428
1429 if (!reloc_done) {
1430 if (ops->request)
1431 ops->request += gd->reloc_off;
Simon Glassb3a47542020-02-04 20:15:17 -07001432 if (ops->rfree)
1433 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001434 if (ops->direction_input)
1435 ops->direction_input += gd->reloc_off;
1436 if (ops->direction_output)
1437 ops->direction_output += gd->reloc_off;
1438 if (ops->get_value)
1439 ops->get_value += gd->reloc_off;
1440 if (ops->set_value)
1441 ops->set_value += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001442 if (ops->get_function)
1443 ops->get_function += gd->reloc_off;
1444 if (ops->xlate)
1445 ops->xlate += gd->reloc_off;
Simon Glass54befdd2021-02-04 21:21:55 -07001446 if (ops->set_flags)
1447 ops->set_flags += gd->reloc_off;
Simon Glassd063ce92021-02-04 21:21:56 -07001448 if (ops->get_flags)
1449 ops->get_flags += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001450
1451 reloc_done++;
1452 }
1453#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001454
Simon Glass2149e112021-08-07 07:24:12 -06001455 if (CONFIG_IS_ENABLED(OF_REAL) && IS_ENABLED(CONFIG_GPIO_HOG)) {
Heiko Schocher58e4c382019-07-17 06:59:51 +02001456 dev_for_each_subnode(node, dev) {
1457 if (ofnode_read_bool(node, "gpio-hog")) {
1458 const char *name = ofnode_get_name(node);
1459 int ret;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001460
Heiko Schocher58e4c382019-07-17 06:59:51 +02001461 ret = device_bind_driver_to_node(dev,
1462 "gpio_hog",
1463 name, node,
1464 &child);
1465 if (ret)
1466 return ret;
1467 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001468 }
1469 }
Michal Simek5f7202c2018-07-12 12:42:27 +02001470 return 0;
1471}
1472
Simon Glasse821d182014-02-26 15:59:24 -07001473UCLASS_DRIVER(gpio) = {
1474 .id = UCLASS_GPIO,
1475 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301476 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001477 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001478 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001479 .pre_remove = gpio_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001480 .per_device_auto = sizeof(struct gpio_dev_priv),
Simon Glasse821d182014-02-26 15:59:24 -07001481};