blob: e4e7f58c39a27513d47b6d501ad44f9b8d074368 [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 Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +053011#include <dm/devres.h>
12#include <dm/device_compat.h>
Heiko Schocher39cb3402019-06-12 06:11:46 +020013#include <dm/device-internal.h>
14#include <dm/lists.h>
15#include <dm/uclass-internal.h>
Eric Nelson786e98d2016-04-24 16:32:40 -070016#include <dt-bindings/gpio/gpio.h>
Simon Glasse821d182014-02-26 15:59:24 -070017#include <errno.h>
Simon Glassd3322bb2015-01-05 20:05:28 -070018#include <fdtdec.h>
Simon Glass0f4517d2014-10-04 11:29:42 -060019#include <malloc.h>
Simon Glass3176b6c2020-07-07 13:11:44 -060020#include <acpi/acpi_device.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glasse821d182014-02-26 15:59:24 -070022#include <asm/gpio.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <dm/device_compat.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090024#include <linux/bug.h>
Simon Glass43b2e1a2014-10-22 21:37:01 -060025#include <linux/ctype.h>
Simon Glass247ccf22021-02-04 21:22:09 -070026#include <linux/delay.h>
Simon Glasse821d182014-02-26 15:59:24 -070027
Simon Glass16e10402015-01-05 20:05:29 -070028DECLARE_GLOBAL_DATA_PTR;
29
Simon Glasse821d182014-02-26 15:59:24 -070030/**
Patrick Delaunay758bba92020-01-13 11:35:01 +010031 * gpio_desc_init() - Initialize the GPIO descriptor
32 *
33 * @desc: GPIO descriptor to initialize
34 * @dev: GPIO device
35 * @offset: Offset of device GPIO
36 */
37static void gpio_desc_init(struct gpio_desc *desc,
38 struct udevice *dev,
39 uint offset)
40{
41 desc->dev = dev;
42 desc->offset = offset;
43 desc->flags = 0;
44}
45
46/**
Simon Glasse821d182014-02-26 15:59:24 -070047 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070048 *
49 * Convert the GPIO number to an entry in the list of GPIOs
50 * or GPIO blocks registered with the GPIO controller. Returns
51 * entry on success, NULL on error.
Simon Glassce555292015-01-05 20:05:27 -070052 *
53 * @gpio: The numeric representation of the GPIO
54 * @desc: Returns description (desc->flags will always be 0)
55 * @return 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070056 */
Simon Glassce555292015-01-05 20:05:27 -070057static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070058{
59 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020060 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070061 int ret;
62
63 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
64 dev;
65 ret = uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070066 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070067 if (gpio >= uc_priv->gpio_base &&
68 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay758bba92020-01-13 11:35:01 +010069 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glasse821d182014-02-26 15:59:24 -070070 return 0;
71 }
72 }
73
74 /* No such GPIO */
Simon Glassce555292015-01-05 20:05:27 -070075 return ret ? ret : -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070076}
77
Heiko Schochera3e793c2020-05-22 11:08:59 +020078#if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
79/**
80 * dm_gpio_lookup_label() - look for name in gpio device
81 *
82 * search in uc_priv, if there is a gpio with labelname same
83 * as name.
84 *
85 * @name: name which is searched
86 * @uc_priv: gpio_dev_priv pointer.
87 * @offset: gpio offset within the device
88 * @return: 0 if found, -ENOENT if not.
89 */
90static int dm_gpio_lookup_label(const char *name,
91 struct gpio_dev_priv *uc_priv, ulong *offset)
92{
93 int len;
94 int i;
95
96 *offset = -1;
97 len = strlen(name);
98 for (i = 0; i < uc_priv->gpio_count; i++) {
99 if (!uc_priv->name[i])
100 continue;
101 if (!strncmp(name, uc_priv->name[i], len)) {
102 *offset = i;
103 return 0;
104 }
105 }
106 return -ENOENT;
107}
108#else
109static int
110dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
111 ulong *offset)
112{
113 return -ENOENT;
114}
115#endif
116
Simon Glass215bcc72015-06-23 15:38:40 -0600117int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -0700118{
Simon Glass43b2e1a2014-10-22 21:37:01 -0600119 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200120 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600121 ulong offset;
122 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -0700123 int ret;
124
Simon Glass43b2e1a2014-10-22 21:37:01 -0600125 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glasse821d182014-02-26 15:59:24 -0700126 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
127 dev;
128 ret = uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -0700129 int len;
130
Simon Glassde0977b2015-03-05 12:25:20 -0700131 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -0600132 if (numeric != -1) {
133 offset = numeric - uc_priv->gpio_base;
134 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -0400135 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -0600136 break;
137 }
138
Simon Glasse821d182014-02-26 15:59:24 -0700139 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
140
Simon Glassd4acf632014-06-11 23:29:47 -0600141 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -0600142 if (!strict_strtoul(name + len, 10, &offset))
143 break;
Simon Glasse821d182014-02-26 15:59:24 -0700144 }
Heiko Schochera3e793c2020-05-22 11:08:59 +0200145
146 /*
147 * if we did not found a gpio through its bank
148 * name, we search for a valid gpio label.
149 */
150 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
151 break;
Simon Glasse821d182014-02-26 15:59:24 -0700152 }
153
Simon Glass43b2e1a2014-10-22 21:37:01 -0600154 if (!dev)
155 return ret ? ret : -EINVAL;
156
Patrick Delaunay758bba92020-01-13 11:35:01 +0100157 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600158
159 return 0;
160}
161
162int gpio_lookup_name(const char *name, struct udevice **devp,
163 unsigned int *offsetp, unsigned int *gpiop)
164{
165 struct gpio_desc desc;
166 int ret;
167
168 if (devp)
169 *devp = NULL;
170 ret = dm_gpio_lookup_name(name, &desc);
171 if (ret)
172 return ret;
173
Simon Glass43b2e1a2014-10-22 21:37:01 -0600174 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600175 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600176 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600177 *offsetp = desc.offset;
178 if (gpiop) {
179 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
180
181 *gpiop = uc_priv->gpio_base + desc.offset;
182 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600183
184 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700185}
186
Simon Glass12faa022017-05-18 20:09:18 -0600187int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
188 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700189{
190 if (args->args_count < 1)
191 return -EINVAL;
192
193 desc->offset = args->args[0];
194
195 if (args->args_count < 2)
196 return 0;
197
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100198 desc->flags = 0;
Eric Nelson786e98d2016-04-24 16:32:40 -0700199 if (args->args[1] & GPIO_ACTIVE_LOW)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100200 desc->flags |= GPIOD_ACTIVE_LOW;
Eric Nelson786e98d2016-04-24 16:32:40 -0700201
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100202 /*
203 * need to test 2 bits for gpio output binding:
204 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
205 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
206 */
207 if (args->args[1] & GPIO_SINGLE_ENDED) {
208 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
209 desc->flags |= GPIOD_OPEN_DRAIN;
210 else
211 desc->flags |= GPIOD_OPEN_SOURCE;
212 }
213
214 if (args->args[1] & GPIO_PULL_UP)
215 desc->flags |= GPIOD_PULL_UP;
216
217 if (args->args[1] & GPIO_PULL_DOWN)
218 desc->flags |= GPIOD_PULL_DOWN;
219
Eric Nelson786e98d2016-04-24 16:32:40 -0700220 return 0;
221}
222
Simon Glass16e10402015-01-05 20:05:29 -0700223static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600224 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700225{
Simon Glass49f315a2021-02-04 21:22:05 -0700226 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassd3322bb2015-01-05 20:05:28 -0700227
Eric Nelson786e98d2016-04-24 16:32:40 -0700228 if (ops->xlate)
229 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700230 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700231 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700232}
233
Heiko Schocher58e4c382019-07-17 06:59:51 +0200234#if defined(CONFIG_GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200235
236struct gpio_hog_priv {
237 struct gpio_desc gpiod;
238};
239
240struct gpio_hog_data {
241 int gpiod_flags;
242 int value;
243 u32 val[2];
244};
245
Simon Glassaad29ae2020-12-03 16:55:21 -0700246static int gpio_hog_of_to_plat(struct udevice *dev)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200247{
Simon Glassfa20e932020-12-03 16:55:20 -0700248 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200249 const char *nodename;
250 int ret;
251
252 plat->value = 0;
253 if (dev_read_bool(dev, "input")) {
254 plat->gpiod_flags = GPIOD_IS_IN;
255 } else if (dev_read_bool(dev, "output-high")) {
256 plat->value = 1;
257 plat->gpiod_flags = GPIOD_IS_OUT;
258 } else if (dev_read_bool(dev, "output-low")) {
259 plat->gpiod_flags = GPIOD_IS_OUT;
260 } else {
261 printf("%s: missing gpio-hog state.\n", __func__);
262 return -EINVAL;
263 }
264 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
265 if (ret) {
266 printf("%s: wrong gpios property, 2 values needed %d\n",
267 __func__, ret);
268 return ret;
269 }
270 nodename = dev_read_string(dev, "line-name");
Heiko Schocher58e4c382019-07-17 06:59:51 +0200271 if (nodename)
272 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200273
274 return 0;
275}
276
277static int gpio_hog_probe(struct udevice *dev)
278{
Simon Glassfa20e932020-12-03 16:55:20 -0700279 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200280 struct gpio_hog_priv *priv = dev_get_priv(dev);
281 int ret;
282
283 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
284 plat->val[0], plat->gpiod_flags,
285 plat->val[1], &priv->gpiod);
286 if (ret < 0) {
287 debug("%s: node %s could not get gpio.\n", __func__,
288 dev->name);
289 return ret;
290 }
Heiko Schocher58e4c382019-07-17 06:59:51 +0200291
292 if (plat->gpiod_flags == GPIOD_IS_OUT) {
293 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
294 if (ret < 0) {
295 debug("%s: node %s could not set gpio.\n", __func__,
296 dev->name);
297 return ret;
298 }
299 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200300
301 return 0;
302}
303
304int gpio_hog_probe_all(void)
305{
306 struct udevice *dev;
307 int ret;
Heiko Schocher58e4c382019-07-17 06:59:51 +0200308 int retval = 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200309
310 for (uclass_first_device(UCLASS_NOP, &dev);
311 dev;
312 uclass_find_next_device(&dev)) {
Simon Glass65130cd2020-12-28 20:34:56 -0700313 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
Heiko Schocher39cb3402019-06-12 06:11:46 +0200314 ret = device_probe(dev);
Heiko Schocher58e4c382019-07-17 06:59:51 +0200315 if (ret) {
316 printf("Failed to probe device %s err: %d\n",
317 dev->name, ret);
318 retval = ret;
319 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200320 }
321 }
322
Heiko Schocher58e4c382019-07-17 06:59:51 +0200323 return retval;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200324}
325
Heiko Schocher58e4c382019-07-17 06:59:51 +0200326int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200327{
328 struct udevice *dev;
329
Heiko Schocher58e4c382019-07-17 06:59:51 +0200330 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200331 gpio_hog_probe_all();
332 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
333 struct gpio_hog_priv *priv = dev_get_priv(dev);
334
Heiko Schocher58e4c382019-07-17 06:59:51 +0200335 *desc = &priv->gpiod;
336 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200337 }
338
Heiko Schocher58e4c382019-07-17 06:59:51 +0200339 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200340}
341
342U_BOOT_DRIVER(gpio_hog) = {
343 .name = "gpio_hog",
344 .id = UCLASS_NOP,
Simon Glassaad29ae2020-12-03 16:55:21 -0700345 .of_to_plat = gpio_hog_of_to_plat,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200346 .probe = gpio_hog_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700347 .priv_auto = sizeof(struct gpio_hog_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700348 .plat_auto = sizeof(struct gpio_hog_data),
Heiko Schocher39cb3402019-06-12 06:11:46 +0200349};
350#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200351int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200352{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200353 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200354}
355#endif
356
Simon Glass047cdb32015-06-23 15:38:41 -0600357int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700358{
Simon Glass49f315a2021-02-04 21:22:05 -0700359 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700360 struct udevice *dev = desc->dev;
361 struct gpio_dev_priv *uc_priv;
362 char *str;
363 int ret;
364
Simon Glassde0977b2015-03-05 12:25:20 -0700365 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700366 if (uc_priv->name[desc->offset])
367 return -EBUSY;
368 str = strdup(label);
369 if (!str)
370 return -ENOMEM;
Simon Glass49f315a2021-02-04 21:22:05 -0700371 if (ops->request) {
372 ret = ops->request(dev, desc->offset, label);
Simon Glassce555292015-01-05 20:05:27 -0700373 if (ret) {
374 free(str);
375 return ret;
376 }
377 }
378 uc_priv->name[desc->offset] = str;
379
380 return 0;
381}
382
Simon Glass16e10402015-01-05 20:05:29 -0700383static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
384{
Simon Glass7611ac62019-09-25 08:56:27 -0600385#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700386 va_list args;
387 char buf[40];
388
389 va_start(args, fmt);
390 vscnprintf(buf, sizeof(buf), fmt, args);
391 va_end(args);
392 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700393#else
394 return dm_gpio_request(desc, fmt);
395#endif
Simon Glass16e10402015-01-05 20:05:29 -0700396}
397
Simon Glasse821d182014-02-26 15:59:24 -0700398/**
399 * gpio_request() - [COMPAT] Request GPIO
400 * gpio: GPIO number
401 * label: Name for the requested GPIO
402 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600403 * The label is copied and allocated so the caller does not need to keep
404 * the pointer around.
405 *
Simon Glasse821d182014-02-26 15:59:24 -0700406 * This function implements the API that's compatible with current
407 * GPIO API used in U-Boot. The request is forwarded to particular
408 * GPIO driver. Returns 0 on success, negative value on error.
409 */
410int gpio_request(unsigned gpio, const char *label)
411{
Simon Glassce555292015-01-05 20:05:27 -0700412 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700413 int ret;
414
Simon Glassce555292015-01-05 20:05:27 -0700415 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700416 if (ret)
417 return ret;
418
Simon Glassce555292015-01-05 20:05:27 -0700419 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700420}
421
422/**
Simon Glass1b27d602014-10-04 11:29:49 -0600423 * gpio_requestf() - [COMPAT] Request GPIO
424 * @gpio: GPIO number
425 * @fmt: Format string for the requested GPIO
426 * @...: Arguments for the printf() format string
427 *
428 * This function implements the API that's compatible with current
429 * GPIO API used in U-Boot. The request is forwarded to particular
430 * GPIO driver. Returns 0 on success, negative value on error.
431 */
432int gpio_requestf(unsigned gpio, const char *fmt, ...)
433{
Simon Glass7611ac62019-09-25 08:56:27 -0600434#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600435 va_list args;
436 char buf[40];
437
438 va_start(args, fmt);
439 vscnprintf(buf, sizeof(buf), fmt, args);
440 va_end(args);
441 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700442#else
443 return gpio_request(gpio, fmt);
444#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600445}
446
Simon Glassce555292015-01-05 20:05:27 -0700447int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700448{
Simon Glass49f315a2021-02-04 21:22:05 -0700449 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600450 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700451 int ret;
452
Simon Glassde0977b2015-03-05 12:25:20 -0700453 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600454 if (!uc_priv->name[offset])
455 return -ENXIO;
Simon Glass49f315a2021-02-04 21:22:05 -0700456 if (ops->rfree) {
457 ret = ops->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600458 if (ret)
459 return ret;
460 }
461
462 free(uc_priv->name[offset]);
463 uc_priv->name[offset] = NULL;
464
465 return 0;
466}
467
Simon Glassce555292015-01-05 20:05:27 -0700468/**
469 * gpio_free() - [COMPAT] Relinquish GPIO
470 * gpio: GPIO number
471 *
472 * This function implements the API that's compatible with current
473 * GPIO API used in U-Boot. The request is forwarded to particular
474 * GPIO driver. Returns 0 on success, negative value on error.
475 */
476int gpio_free(unsigned gpio)
477{
478 struct gpio_desc desc;
479 int ret;
480
481 ret = gpio_to_device(gpio, &desc);
482 if (ret)
483 return ret;
484
485 return _dm_gpio_free(desc.dev, desc.offset);
486}
487
Simon Glassfd838972016-03-06 19:27:51 -0700488static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600489{
Simon Glass230c1432015-07-02 18:16:16 -0600490 struct gpio_dev_priv *uc_priv;
491
492 if (!dm_gpio_is_valid(desc))
493 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600494
Simon Glass230c1432015-07-02 18:16:16 -0600495 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700496 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600497 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700498 desc->dev->name, func,
499 uc_priv->bank_name ? uc_priv->bank_name : "",
500 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600501 return -EBUSY;
502 }
503
504 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700505}
506
507/**
508 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
509 * gpio: GPIO number
510 *
511 * This function implements the API that's compatible with current
512 * GPIO API used in U-Boot. The request is forwarded to particular
513 * GPIO driver. Returns 0 on success, negative value on error.
514 */
515int gpio_direction_input(unsigned gpio)
516{
Simon Glassce555292015-01-05 20:05:27 -0700517 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700518 int ret;
519
Simon Glassce555292015-01-05 20:05:27 -0700520 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700521 if (ret)
522 return ret;
523
Simon Glass722f9682021-02-04 21:22:04 -0700524 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
Simon Glasse821d182014-02-26 15:59:24 -0700525}
526
527/**
528 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
529 * gpio: GPIO number
530 * value: Logical value to be set on the GPIO pin
531 *
532 * This function implements the API that's compatible with current
533 * GPIO API used in U-Boot. The request is forwarded to particular
534 * GPIO driver. Returns 0 on success, negative value on error.
535 */
536int gpio_direction_output(unsigned gpio, int value)
537{
Simon Glassce555292015-01-05 20:05:27 -0700538 struct gpio_desc desc;
Simon Glass722f9682021-02-04 21:22:04 -0700539 ulong flags;
Simon Glassce555292015-01-05 20:05:27 -0700540 int ret;
541
542 ret = gpio_to_device(gpio, &desc);
543 if (ret)
544 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700545
Simon Glass722f9682021-02-04 21:22:04 -0700546 flags = GPIOD_IS_OUT;
547 if (value)
548 flags |= GPIOD_IS_OUT_ACTIVE;
549 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700550}
551
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100552static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700553{
Simon Glass49f315a2021-02-04 21:22:05 -0700554 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700555 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100556
Simon Glass49f315a2021-02-04 21:22:05 -0700557 value = ops->get_value(desc->dev, desc->offset);
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100558
559 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
560}
561
562int dm_gpio_get_value(const struct gpio_desc *desc)
563{
Simon Glassce555292015-01-05 20:05:27 -0700564 int ret;
565
566 ret = check_reserved(desc, "get_value");
567 if (ret)
568 return ret;
569
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100570 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700571}
572
Simon Glassfd838972016-03-06 19:27:51 -0700573int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700574{
Simon Glass7b893f92021-02-04 21:22:03 -0700575 const struct dm_gpio_ops *ops;
Simon Glassce555292015-01-05 20:05:27 -0700576 int ret;
577
578 ret = check_reserved(desc, "set_value");
579 if (ret)
580 return ret;
581
582 if (desc->flags & GPIOD_ACTIVE_LOW)
583 value = !value;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200584
Simon Glass7b893f92021-02-04 21:22:03 -0700585 /* GPIOD_ are directly managed by driver in set_flags */
586 ops = gpio_get_ops(desc->dev);
587 if (ops->set_flags) {
588 ulong flags = desc->flags;
589
590 if (value)
591 flags |= GPIOD_IS_OUT_ACTIVE;
592 else
593 flags &= ~GPIOD_IS_OUT_ACTIVE;
594 return ops->set_flags(desc->dev, desc->offset, flags);
595 }
596
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200597 /*
598 * Emulate open drain by not actively driving the line high or
599 * Emulate open source by not actively driving the line low
600 */
601 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
602 (desc->flags & GPIOD_OPEN_SOURCE && !value))
Simon Glass7b893f92021-02-04 21:22:03 -0700603 return ops->direction_input(desc->dev, desc->offset);
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200604 else if (desc->flags & GPIOD_OPEN_DRAIN ||
605 desc->flags & GPIOD_OPEN_SOURCE)
Simon Glass7b893f92021-02-04 21:22:03 -0700606 return ops->direction_output(desc->dev, desc->offset, value);
607
608 ret = ops->set_value(desc->dev, desc->offset, value);
609 if (ret)
610 return ret;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200611
Simon Glassce555292015-01-05 20:05:27 -0700612 return 0;
613}
614
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100615/* check dir flags invalid configuration */
616static int check_dir_flags(ulong flags)
617{
618 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
619 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
620 __func__, flags);
621 return -EINVAL;
622 }
623
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100624 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
625 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
626 __func__, flags);
627 return -EINVAL;
628 }
629
630 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
631 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
632 __func__, flags);
633 return -EINVAL;
634 }
635
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100636 return 0;
637}
638
Simon Glass7b893f92021-02-04 21:22:03 -0700639/**
640 * _dm_gpio_set_flags() - Send flags to the driver
641 *
642 * This uses the best available method to send the given flags to the driver.
643 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
644 * of GPIOD_IS_OUT_ACTIVE.
645 *
646 * @desc: GPIO description
647 * @flags: flags value to set
648 * @return 0 if OK, -ve on error
649 */
Simon Glass54befdd2021-02-04 21:21:55 -0700650static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700651{
652 struct udevice *dev = desc->dev;
Simon Glass49f315a2021-02-04 21:22:05 -0700653 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100654 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100655 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700656
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100657 ret = check_dir_flags(flags);
658 if (ret) {
659 dev_dbg(dev,
660 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
661 desc->dev->name,
662 uc_priv->bank_name ? uc_priv->bank_name : "",
663 desc->offset, flags);
664
665 return ret;
666 }
667
Simon Glass7b893f92021-02-04 21:22:03 -0700668 /* If active low, invert the output state */
669 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
670 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
671 flags ^= GPIOD_IS_OUT_ACTIVE;
672
Simon Glass54befdd2021-02-04 21:21:55 -0700673 /* GPIOD_ are directly managed by driver in set_flags */
674 if (ops->set_flags) {
675 ret = ops->set_flags(dev, desc->offset, flags);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100676 } else {
677 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700678 bool value = flags & GPIOD_IS_OUT_ACTIVE;
679
680 ret = ops->direction_output(dev, desc->offset, value);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100681 } else if (flags & GPIOD_IS_IN) {
682 ret = ops->direction_input(dev, desc->offset);
683 }
Simon Glassce555292015-01-05 20:05:27 -0700684 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100685
686 return ret;
687}
688
Simon Glass7b893f92021-02-04 21:22:03 -0700689int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100690{
Simon Glass7b893f92021-02-04 21:22:03 -0700691 ulong flags;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100692 int ret;
693
694 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700695 if (ret)
696 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100697
Simon Glass7b893f92021-02-04 21:22:03 -0700698 flags = (desc->flags & ~clr) | set;
699
Simon Glass54befdd2021-02-04 21:21:55 -0700700 ret = _dm_gpio_set_flags(desc, flags);
Simon Glass7b893f92021-02-04 21:22:03 -0700701 if (ret)
702 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700703
Simon Glass7b893f92021-02-04 21:22:03 -0700704 /* save the flags also in descriptor */
705 desc->flags = flags;
706
707 return 0;
708}
709
710int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
711{
712 /* combine the requested flags (for IN/OUT) and the descriptor flags */
713 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700714}
715
Simon Glass247ccf22021-02-04 21:22:09 -0700716int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
717 ulong set)
718{
719 int ret;
720 int i;
721
722 for (i = 0; i < count; i++) {
723 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
724 if (ret)
725 return log_ret(ret);
726 }
727
728 return 0;
729}
730
Simon Glass909bee32021-02-04 21:21:57 -0700731int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100732{
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100733 struct udevice *dev = desc->dev;
734 int ret, value;
Simon Glass49f315a2021-02-04 21:22:05 -0700735 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glassd063ce92021-02-04 21:21:56 -0700736 ulong flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100737
Simon Glassd063ce92021-02-04 21:21:56 -0700738 ret = check_reserved(desc, "get_flags");
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100739 if (ret)
740 return ret;
741
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100742 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
Simon Glassd063ce92021-02-04 21:21:56 -0700743 if (ops->get_flags) {
744 ret = ops->get_flags(dev, desc->offset, &flags);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100745 if (ret)
746 return ret;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100747
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100748 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
Simon Glassd063ce92021-02-04 21:21:56 -0700749 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100750 if (desc->flags & GPIOD_ACTIVE_LOW)
751 value = !value;
Simon Glassd063ce92021-02-04 21:21:56 -0700752 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
753 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100754 if (value)
Simon Glassd063ce92021-02-04 21:21:56 -0700755 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100756 } else {
Simon Glassd063ce92021-02-04 21:21:56 -0700757 flags = desc->flags;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100758 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
Simon Glassd063ce92021-02-04 21:21:56 -0700759 flags &= ~GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100760 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
Simon Glassd063ce92021-02-04 21:21:56 -0700761 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100762 }
Simon Glassd063ce92021-02-04 21:21:56 -0700763 *flagsp = flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100764
765 return 0;
766}
767
Simon Glasse821d182014-02-26 15:59:24 -0700768/**
769 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
770 * gpio: GPIO number
771 *
772 * This function implements the API that's compatible with current
773 * GPIO API used in U-Boot. The request is forwarded to particular
774 * GPIO driver. Returns the value of the GPIO pin, or negative value
775 * on error.
776 */
777int gpio_get_value(unsigned gpio)
778{
Simon Glasse821d182014-02-26 15:59:24 -0700779 int ret;
780
Simon Glassce555292015-01-05 20:05:27 -0700781 struct gpio_desc desc;
782
783 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700784 if (ret)
785 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700786 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700787}
788
789/**
790 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
791 * gpio: GPIO number
792 * value: Logical value to be set on the GPIO pin.
793 *
794 * This function implements the API that's compatible with current
795 * GPIO API used in U-Boot. The request is forwarded to particular
796 * GPIO driver. Returns 0 on success, negative value on error.
797 */
798int gpio_set_value(unsigned gpio, int value)
799{
Simon Glassce555292015-01-05 20:05:27 -0700800 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700801 int ret;
802
Simon Glassce555292015-01-05 20:05:27 -0700803 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700804 if (ret)
805 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700806 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700807}
808
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200809const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700810{
811 struct gpio_dev_priv *priv;
812
813 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700814 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700815 assert(priv);
816
817 *bit_count = priv->gpio_count;
818 return priv->bank_name;
819}
820
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600821static const char * const gpio_function[GPIOF_COUNT] = {
822 "input",
823 "output",
824 "unused",
825 "unknown",
826 "func",
827};
828
Masahiro Yamada286c2522017-06-22 16:50:25 +0900829static int get_function(struct udevice *dev, int offset, bool skip_unused,
830 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600831{
Simon Glassde0977b2015-03-05 12:25:20 -0700832 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass49f315a2021-02-04 21:22:05 -0700833 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600834
835 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
836 if (!device_active(dev))
837 return -ENODEV;
838 if (offset < 0 || offset >= uc_priv->gpio_count)
839 return -EINVAL;
840 if (namep)
841 *namep = uc_priv->name[offset];
842 if (skip_unused && !uc_priv->name[offset])
843 return GPIOF_UNUSED;
844 if (ops->get_function) {
845 int ret;
846
847 ret = ops->get_function(dev, offset);
848 if (ret < 0)
849 return ret;
850 if (ret >= ARRAY_SIZE(gpio_function))
851 return -ENODATA;
852 return ret;
853 }
854
855 return GPIOF_UNKNOWN;
856}
857
858int gpio_get_function(struct udevice *dev, int offset, const char **namep)
859{
860 return get_function(dev, offset, true, namep);
861}
862
863int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
864{
865 return get_function(dev, offset, false, namep);
866}
867
Simon Glass6b1ef592014-10-04 11:29:44 -0600868int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
869{
Simon Glass49f315a2021-02-04 21:22:05 -0700870 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600871 struct gpio_dev_priv *priv;
872 char *str = buf;
873 int func;
874 int ret;
875 int len;
876
877 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
878
879 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700880 priv = dev_get_uclass_priv(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600881 ret = gpio_get_raw_function(dev, offset, NULL);
882 if (ret < 0)
883 return ret;
884 func = ret;
885 len = snprintf(str, buffsize, "%s%d: %s",
886 priv->bank_name ? priv->bank_name : "",
887 offset, gpio_function[func]);
888 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
889 func == GPIOF_UNUSED) {
890 const char *label;
891 bool used;
892
893 ret = ops->get_value(dev, offset);
894 if (ret < 0)
895 return ret;
896 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
897 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
898 ret,
899 used ? 'x' : ' ',
900 used ? " " : "",
901 label ? label : "");
902 }
903
904 return 0;
905}
906
Simon Glass3176b6c2020-07-07 13:11:44 -0600907#if CONFIG_IS_ENABLED(ACPIGEN)
908int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
909{
Simon Glass49f315a2021-02-04 21:22:05 -0700910 const struct dm_gpio_ops *ops;
Simon Glass3176b6c2020-07-07 13:11:44 -0600911
912 memset(gpio, '\0', sizeof(*gpio));
913 if (!dm_gpio_is_valid(desc)) {
914 /* Indicate that the GPIO is not valid */
915 gpio->pin_count = 0;
916 gpio->pins[0] = 0;
917 return -EINVAL;
918 }
919
920 ops = gpio_get_ops(desc->dev);
921 if (!ops->get_acpi)
922 return -ENOSYS;
923
924 return ops->get_acpi(desc, gpio);
925}
926#endif
927
Simon Glassbef54db2015-04-14 21:03:20 -0600928int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
929{
930 int i, ret;
931 int gpio;
932
933 for (i = 0; i < 32; i++) {
934 gpio = gpio_num_array[i];
935 if (gpio == -1)
936 break;
937 ret = gpio_requestf(gpio, fmt, i);
938 if (ret)
939 goto err;
940 ret = gpio_direction_input(gpio);
941 if (ret) {
942 gpio_free(gpio);
943 goto err;
944 }
945 }
946
947 return 0;
948err:
949 for (i--; i >= 0; i--)
950 gpio_free(gpio_num_array[i]);
951
952 return ret;
953}
954
Simon Glass2c97a8f2014-11-10 18:00:21 -0700955/*
956 * get a number comprised of multiple GPIO values. gpio_num_array points to
957 * the array of gpio pin numbers to scan, terminated by -1.
958 */
Simon Glassbef54db2015-04-14 21:03:20 -0600959int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700960{
961 int gpio;
962 unsigned bitmask = 1;
963 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600964 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700965
966 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600967 ((gpio = *gpio_list++) != -1)) {
968 ret = gpio_get_value(gpio);
969 if (ret < 0)
970 return ret;
971 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700972 vector |= bitmask;
973 bitmask <<= 1;
974 }
Simon Glassbef54db2015-04-14 21:03:20 -0600975
Simon Glass2c97a8f2014-11-10 18:00:21 -0700976 return vector;
977}
978
Simon Glassfd838972016-03-06 19:27:51 -0700979int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -0700980{
981 unsigned bitmask = 1;
982 unsigned vector = 0;
983 int ret, i;
984
985 for (i = 0; i < count; i++) {
986 ret = dm_gpio_get_value(&desc_list[i]);
987 if (ret < 0)
988 return ret;
989 else if (ret)
990 vector |= bitmask;
991 bitmask <<= 1;
992 }
Simon Glass247ccf22021-02-04 21:22:09 -0700993
994 return vector;
995}
996
997int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
998 int count)
999{
1000 static const char tristate[] = "01z";
1001 enum {
1002 PULLUP,
1003 PULLDOWN,
1004
1005 NUM_OPTIONS,
1006 };
1007 int vals[NUM_OPTIONS];
1008 uint mask;
1009 uint vector = 0;
1010 int ret, i;
1011
1012 /*
1013 * Limit to 19 digits which should be plenty. This avoids overflow of a
1014 * 32-bit int
1015 */
1016 assert(count < 20);
1017
1018 for (i = 0; i < NUM_OPTIONS; i++) {
1019 uint flags = GPIOD_IS_IN;
1020
1021 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1022 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1023 flags);
1024 if (ret)
1025 return log_msg_ret("pu", ret);
1026
1027 /* Give the lines time to settle */
1028 udelay(10);
1029
1030 ret = dm_gpio_get_values_as_int(desc_list, count);
1031 if (ret < 0)
1032 return log_msg_ret("get1", ret);
1033 vals[i] = ret;
1034 }
1035
1036 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1037 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1038 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1039 uint pu = vals[PULLUP] & mask ? 1 : 0;
1040 uint digit;
1041
1042 /*
1043 * Get value with internal pulldown active. If this is 1 then
1044 * there is a stronger external pullup, which we call 1. If not
1045 * then call it 0.
1046 *
1047 * If the values differ then the pin is floating so we call
1048 * this a 2.
1049 */
1050 if (pu == pd)
1051 digit = pd;
1052 else
1053 digit = 2;
1054 log_debug("%c ", tristate[digit]);
1055 vector = 3 * vector + digit;
1056 }
1057 log_debug("vector=%d\n", vector);
Simon Glassdf1687d2016-03-06 19:27:50 -07001058
1059 return vector;
1060}
1061
Heiko Schocher58e4c382019-07-17 06:59:51 +02001062/**
1063 * gpio_request_tail: common work for requesting a gpio.
1064 *
1065 * ret: return value from previous work in function which calls
1066 * this function.
1067 * This seems bogus (why calling this function instead not
1068 * calling it and end caller function instead?).
1069 * Because on error in caller function we want to set some
1070 * default values in gpio desc and have a common error
1071 * debug message, which provides this function.
1072 * nodename: Name of node for which gpio gets requested
1073 * used for gpio label name.
1074 * args: pointer to output arguments structure
1075 * list_name: Name of GPIO list
1076 * used for gpio label name.
1077 * index: gpio index in gpio list
1078 * used for gpio label name.
1079 * desc: pointer to gpio descriptor, filled from this
1080 * function.
1081 * flags: gpio flags to use.
1082 * add_index: should index added to gpio label name
1083 * gpio_dev: pointer to gpio device from which the gpio
1084 * will be requested. If NULL try to get the
1085 * gpio device with uclass_get_device_by_ofnode()
1086 *
1087 * return: In error case this function sets default values in
1088 * gpio descriptor, also emmits a debug message.
1089 * On success it returns 0 else the error code from
1090 * function calls, or the error code passed through
1091 * ret to this function.
1092 *
1093 */
Heiko Schocher39cb3402019-06-12 06:11:46 +02001094static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -06001095 struct ofnode_phandle_args *args,
1096 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +02001097 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +02001098 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -07001099{
Patrick Delaunay758bba92020-01-13 11:35:01 +01001100 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -06001101 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -07001102 goto err;
Simon Glass16e10402015-01-05 20:05:29 -07001103
Heiko Schocher39cb3402019-06-12 06:11:46 +02001104 if (!desc->dev) {
1105 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1106 &desc->dev);
1107 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +02001108 debug("%s: uclass_get_device_by_ofnode failed\n",
1109 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001110 goto err;
1111 }
Simon Glass16e10402015-01-05 20:05:29 -07001112 }
Simon Glass12faa022017-05-18 20:09:18 -06001113 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -07001114 if (ret) {
1115 debug("%s: gpio_find_and_xlate failed\n", __func__);
1116 goto err;
1117 }
1118 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001119 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -07001120 if (ret) {
1121 debug("%s: dm_gpio_requestf failed\n", __func__);
1122 goto err;
1123 }
Simon Glass7b893f92021-02-04 21:22:03 -07001124
1125 /* Keep any direction flags provided by the devicetree */
1126 ret = dm_gpio_set_dir_flags(desc,
1127 flags | (desc->flags & GPIOD_MASK_DIR));
Simon Glass16e10402015-01-05 20:05:29 -07001128 if (ret) {
1129 debug("%s: dm_gpio_set_dir failed\n", __func__);
1130 goto err;
1131 }
1132
1133 return 0;
1134err:
1135 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001136 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -07001137 return ret;
1138}
1139
Simon Glassea383722021-02-04 21:21:54 -07001140#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass1d9af1f2017-05-30 21:47:09 -06001141static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1142 int index, struct gpio_desc *desc,
1143 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -06001144{
1145 struct ofnode_phandle_args args;
1146 int ret;
1147
Simon Glass1d9af1f2017-05-30 21:47:09 -06001148 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1149 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -06001150
Heiko Schocher39cb3402019-06-12 06:11:46 +02001151 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1152 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -06001153}
1154
Simon Glass1d9af1f2017-05-30 21:47:09 -06001155int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001156 struct gpio_desc *desc, int flags)
1157{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001158 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1159 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -07001160}
1161
Simon Glass1d9af1f2017-05-30 21:47:09 -06001162int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001163 struct gpio_desc *desc, int flags)
1164{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001165 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001166 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -06001167 int ret;
1168
1169 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1170 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001171 node = dev_ofnode(dev);
1172 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1173 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -07001174}
1175
Simon Glass1d9af1f2017-05-30 21:47:09 -06001176int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -07001177 struct gpio_desc *desc, int max_count,
1178 int flags)
1179{
1180 int count;
1181 int ret;
1182
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +02001183 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -06001184 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -07001185 &desc[count], flags, true);
1186 if (ret == -ENOENT)
1187 break;
1188 else if (ret)
1189 goto err;
1190 }
1191
1192 /* We ran out of GPIOs in the list */
1193 return count;
1194
1195err:
1196 gpio_free_list_nodev(desc, count - 1);
1197
1198 return ret;
1199}
1200
1201int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1202 struct gpio_desc *desc, int max_count,
1203 int flags)
1204{
1205 /*
1206 * This isn't ideal since we don't use dev->name in the debug()
1207 * calls in gpio_request_by_name(), but we can do this until
1208 * gpio_request_list_by_name_nodev() can be dropped.
1209 */
Simon Glass1d9af1f2017-05-30 21:47:09 -06001210 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1211 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -07001212}
1213
1214int gpio_get_list_count(struct udevice *dev, const char *list_name)
1215{
1216 int ret;
1217
Patrick Delaunay75969032020-09-09 18:26:16 +02001218 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
1219 NULL);
Simon Glass16e10402015-01-05 20:05:29 -07001220 if (ret) {
1221 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1222 __func__, dev->name, list_name, ret);
1223 }
1224
1225 return ret;
1226}
Simon Glassea383722021-02-04 21:21:54 -07001227#endif /* OF_PLATDATA */
Simon Glass16e10402015-01-05 20:05:29 -07001228
1229int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1230{
1231 /* For now, we don't do any checking of dev */
1232 return _dm_gpio_free(desc->dev, desc->offset);
1233}
1234
1235int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1236{
1237 int i;
1238
1239 /* For now, we don't do any checking of dev */
1240 for (i = 0; i < count; i++)
1241 dm_gpio_free(dev, &desc[i]);
1242
1243 return 0;
1244}
1245
1246int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1247{
1248 return gpio_free_list(NULL, desc, count);
1249}
1250
Simon Glasse821d182014-02-26 15:59:24 -07001251/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -06001252static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -07001253{
1254 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001255 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -07001256 struct uclass *uc;
1257 unsigned base;
1258 int ret;
1259
1260 ret = uclass_get(UCLASS_GPIO, &uc);
1261 if (ret)
1262 return ret;
1263
1264 /* Ensure that we have a base for each bank */
1265 base = 0;
1266 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -06001267 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -07001268 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001269 uc_priv->gpio_base = base;
1270 base += uc_priv->gpio_count;
1271 }
1272 }
1273
1274 return 0;
1275}
1276
Simon Glassfd838972016-03-06 19:27:51 -07001277int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -06001278{
1279 struct udevice *dev = desc->dev;
1280 struct gpio_dev_priv *uc_priv;
1281
1282 if (!dev)
1283 return -1;
Simon Glass95588622020-12-22 19:30:28 -07001284 uc_priv = dev_get_uclass_priv(dev);
Simon Glass94f54d12015-03-25 12:21:58 -06001285
1286 return uc_priv->gpio_base + desc->offset;
1287}
1288
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001289static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001290{
Simon Glassde0977b2015-03-05 12:25:20 -07001291 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001292
1293 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1294 if (!uc_priv->name)
1295 return -ENOMEM;
1296
1297 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001298}
1299
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001300static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001301{
Simon Glassde0977b2015-03-05 12:25:20 -07001302 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001303 int i;
1304
1305 for (i = 0; i < uc_priv->gpio_count; i++) {
1306 if (uc_priv->name[i])
1307 free(uc_priv->name[i]);
1308 }
1309 free(uc_priv->name);
1310
1311 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001312}
1313
Heiko Schocher39cb3402019-06-12 06:11:46 +02001314int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1315 char *list_name, int index, int flags,
1316 int dtflags, struct gpio_desc *desc)
1317{
1318 struct ofnode_phandle_args args;
1319
1320 args.node = ofnode_null();
1321 args.args_count = 2;
1322 args.args[0] = index;
1323 args.args[1] = dtflags;
1324
1325 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1326 flags, 0, dev);
1327}
1328
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +05301329static void devm_gpiod_release(struct udevice *dev, void *res)
1330{
1331 dm_gpio_free(dev, res);
1332}
1333
1334static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1335{
1336 return res == data;
1337}
1338
1339struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1340 unsigned int index, int flags)
1341{
1342 int rc;
1343 struct gpio_desc *desc;
1344 char *propname;
1345 static const char suffix[] = "-gpios";
1346
1347 propname = malloc(strlen(id) + sizeof(suffix));
1348 if (!propname) {
1349 rc = -ENOMEM;
1350 goto end;
1351 }
1352
1353 strcpy(propname, id);
1354 strcat(propname, suffix);
1355
1356 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1357 __GFP_ZERO);
1358 if (unlikely(!desc)) {
1359 rc = -ENOMEM;
1360 goto end;
1361 }
1362
1363 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1364
1365end:
1366 if (propname)
1367 free(propname);
1368
1369 if (rc)
1370 return ERR_PTR(rc);
1371
1372 devres_add(dev, desc);
1373
1374 return desc;
1375}
1376
1377struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1378 const char *id,
1379 unsigned int index,
1380 int flags)
1381{
1382 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1383
1384 if (IS_ERR(desc))
1385 return NULL;
1386
1387 return desc;
1388}
1389
1390void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1391{
1392 int rc;
1393
1394 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1395 WARN_ON(rc);
1396}
1397
Michal Simek5f7202c2018-07-12 12:42:27 +02001398static int gpio_post_bind(struct udevice *dev)
1399{
Heiko Schocher39cb3402019-06-12 06:11:46 +02001400 struct udevice *child;
1401 ofnode node;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001402
Michal Simek5f7202c2018-07-12 12:42:27 +02001403#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1404 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1405 static int reloc_done;
1406
1407 if (!reloc_done) {
1408 if (ops->request)
1409 ops->request += gd->reloc_off;
Simon Glassb3a47542020-02-04 20:15:17 -07001410 if (ops->rfree)
1411 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001412 if (ops->direction_input)
1413 ops->direction_input += gd->reloc_off;
1414 if (ops->direction_output)
1415 ops->direction_output += gd->reloc_off;
1416 if (ops->get_value)
1417 ops->get_value += gd->reloc_off;
1418 if (ops->set_value)
1419 ops->set_value += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001420 if (ops->get_function)
1421 ops->get_function += gd->reloc_off;
1422 if (ops->xlate)
1423 ops->xlate += gd->reloc_off;
Simon Glass54befdd2021-02-04 21:21:55 -07001424 if (ops->set_flags)
1425 ops->set_flags += gd->reloc_off;
Simon Glassd063ce92021-02-04 21:21:56 -07001426 if (ops->get_flags)
1427 ops->get_flags += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001428
1429 reloc_done++;
1430 }
1431#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001432
Heiko Schocher58e4c382019-07-17 06:59:51 +02001433 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1434 dev_for_each_subnode(node, dev) {
1435 if (ofnode_read_bool(node, "gpio-hog")) {
1436 const char *name = ofnode_get_name(node);
1437 int ret;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001438
Heiko Schocher58e4c382019-07-17 06:59:51 +02001439 ret = device_bind_driver_to_node(dev,
1440 "gpio_hog",
1441 name, node,
1442 &child);
1443 if (ret)
1444 return ret;
1445 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001446 }
1447 }
Michal Simek5f7202c2018-07-12 12:42:27 +02001448 return 0;
1449}
1450
Simon Glasse821d182014-02-26 15:59:24 -07001451UCLASS_DRIVER(gpio) = {
1452 .id = UCLASS_GPIO,
1453 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301454 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001455 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001456 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001457 .pre_remove = gpio_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001458 .per_device_auto = sizeof(struct gpio_dev_priv),
Simon Glasse821d182014-02-26 15:59:24 -07001459};