aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpiolib.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpio/gpiolib.c')
-rw-r--r--drivers/gpio/gpiolib.c251
1 files changed, 137 insertions, 114 deletions
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index c8a7c810bade..50c4922fe53a 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -10,12 +10,13 @@
10#include <linux/seq_file.h> 10#include <linux/seq_file.h>
11#include <linux/gpio.h> 11#include <linux/gpio.h>
12#include <linux/of_gpio.h> 12#include <linux/of_gpio.h>
13#include <linux/acpi_gpio.h>
14#include <linux/idr.h> 13#include <linux/idr.h>
15#include <linux/slab.h> 14#include <linux/slab.h>
16#include <linux/acpi.h> 15#include <linux/acpi.h>
17#include <linux/gpio/driver.h> 16#include <linux/gpio/driver.h>
18 17
18#include "gpiolib.h"
19
19#define CREATE_TRACE_POINTS 20#define CREATE_TRACE_POINTS
20#include <trace/events/gpio.h> 21#include <trace/events/gpio.h>
21 22
@@ -84,40 +85,57 @@ static DEFINE_IDR(dirent_idr);
84static int gpiod_request(struct gpio_desc *desc, const char *label); 85static int gpiod_request(struct gpio_desc *desc, const char *label);
85static void gpiod_free(struct gpio_desc *desc); 86static void gpiod_free(struct gpio_desc *desc);
86 87
88/* With descriptor prefix */
89
87#ifdef CONFIG_DEBUG_FS 90#ifdef CONFIG_DEBUG_FS
88#define gpiod_emerg(desc, fmt, ...) \ 91#define gpiod_emerg(desc, fmt, ...) \
89 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ 92 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
90 ##__VA_ARGS__) 93 ##__VA_ARGS__)
91#define gpiod_crit(desc, fmt, ...) \ 94#define gpiod_crit(desc, fmt, ...) \
92 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ 95 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
93 ##__VA_ARGS__) 96 ##__VA_ARGS__)
94#define gpiod_err(desc, fmt, ...) \ 97#define gpiod_err(desc, fmt, ...) \
95 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ 98 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
96 ##__VA_ARGS__) 99 ##__VA_ARGS__)
97#define gpiod_warn(desc, fmt, ...) \ 100#define gpiod_warn(desc, fmt, ...) \
98 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ 101 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
99 ##__VA_ARGS__) 102 ##__VA_ARGS__)
100#define gpiod_info(desc, fmt, ...) \ 103#define gpiod_info(desc, fmt, ...) \
101 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ 104 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
102 ##__VA_ARGS__) 105 ##__VA_ARGS__)
103#define gpiod_dbg(desc, fmt, ...) \ 106#define gpiod_dbg(desc, fmt, ...) \
104 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label, \ 107 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
105 ##__VA_ARGS__) 108 ##__VA_ARGS__)
106#else 109#else
107#define gpiod_emerg(desc, fmt, ...) \ 110#define gpiod_emerg(desc, fmt, ...) \
108 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) 111 pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
109#define gpiod_crit(desc, fmt, ...) \ 112#define gpiod_crit(desc, fmt, ...) \
110 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) 113 pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
111#define gpiod_err(desc, fmt, ...) \ 114#define gpiod_err(desc, fmt, ...) \
112 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) 115 pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
113#define gpiod_warn(desc, fmt, ...) \ 116#define gpiod_warn(desc, fmt, ...) \
114 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) 117 pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
115#define gpiod_info(desc, fmt, ...) \ 118#define gpiod_info(desc, fmt, ...) \
116 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) 119 pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
117#define gpiod_dbg(desc, fmt, ...) \ 120#define gpiod_dbg(desc, fmt, ...) \
118 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) 121 pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__)
119#endif 122#endif
120 123
124/* With chip prefix */
125
126#define chip_emerg(chip, fmt, ...) \
127 pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
128#define chip_crit(chip, fmt, ...) \
129 pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
130#define chip_err(chip, fmt, ...) \
131 pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
132#define chip_warn(chip, fmt, ...) \
133 pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
134#define chip_info(chip, fmt, ...) \
135 pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
136#define chip_dbg(chip, fmt, ...) \
137 pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__)
138
121static inline void desc_set_label(struct gpio_desc *d, const char *label) 139static inline void desc_set_label(struct gpio_desc *d, const char *label)
122{ 140{
123#ifdef CONFIG_DEBUG_FS 141#ifdef CONFIG_DEBUG_FS
@@ -151,9 +169,10 @@ EXPORT_SYMBOL_GPL(gpio_to_desc);
151static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip, 169static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip,
152 unsigned int offset) 170 unsigned int offset)
153{ 171{
154 unsigned int gpio = chip->base + offset; 172 if (offset >= chip->ngpio)
173 return ERR_PTR(-EINVAL);
155 174
156 return gpio_to_desc(gpio); 175 return &chip->desc[offset];
157} 176}
158 177
159/** 178/**
@@ -187,7 +206,8 @@ static int gpio_ensure_requested(struct gpio_desc *desc)
187 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, 206 if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0,
188 "autorequest GPIO-%d\n", gpio)) { 207 "autorequest GPIO-%d\n", gpio)) {
189 if (!try_module_get(chip->owner)) { 208 if (!try_module_get(chip->owner)) {
190 pr_err("GPIO-%d: module can't be gotten \n", gpio); 209 gpiod_err(desc, "%s: module can't be gotten\n",
210 __func__);
191 clear_bit(FLAG_REQUESTED, &desc->flags); 211 clear_bit(FLAG_REQUESTED, &desc->flags);
192 /* lose */ 212 /* lose */
193 return -EIO; 213 return -EIO;
@@ -808,8 +828,8 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
808 if (!test_bit(FLAG_REQUESTED, &desc->flags) || 828 if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
809 test_bit(FLAG_EXPORT, &desc->flags)) { 829 test_bit(FLAG_EXPORT, &desc->flags)) {
810 spin_unlock_irqrestore(&gpio_lock, flags); 830 spin_unlock_irqrestore(&gpio_lock, flags);
811 pr_debug("%s: gpio %d unavailable (requested=%d, exported=%d)\n", 831 gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
812 __func__, desc_to_gpio(desc), 832 __func__,
813 test_bit(FLAG_REQUESTED, &desc->flags), 833 test_bit(FLAG_REQUESTED, &desc->flags),
814 test_bit(FLAG_EXPORT, &desc->flags)); 834 test_bit(FLAG_EXPORT, &desc->flags));
815 status = -EPERM; 835 status = -EPERM;
@@ -857,8 +877,7 @@ fail_unregister_device:
857 device_unregister(dev); 877 device_unregister(dev);
858fail_unlock: 878fail_unlock:
859 mutex_unlock(&sysfs_lock); 879 mutex_unlock(&sysfs_lock);
860 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 880 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
861 status);
862 return status; 881 return status;
863} 882}
864EXPORT_SYMBOL_GPL(gpiod_export); 883EXPORT_SYMBOL_GPL(gpiod_export);
@@ -906,8 +925,7 @@ int gpiod_export_link(struct device *dev, const char *name,
906 mutex_unlock(&sysfs_lock); 925 mutex_unlock(&sysfs_lock);
907 926
908 if (status) 927 if (status)
909 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 928 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
910 status);
911 929
912 return status; 930 return status;
913} 931}
@@ -951,8 +969,7 @@ unlock:
951 mutex_unlock(&sysfs_lock); 969 mutex_unlock(&sysfs_lock);
952 970
953 if (status) 971 if (status)
954 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 972 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
955 status);
956 973
957 return status; 974 return status;
958} 975}
@@ -994,8 +1011,7 @@ void gpiod_unexport(struct gpio_desc *desc)
994 } 1011 }
995 1012
996 if (status) 1013 if (status)
997 pr_debug("%s: gpio%d status %d\n", __func__, desc_to_gpio(desc), 1014 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
998 status);
999} 1015}
1000EXPORT_SYMBOL_GPL(gpiod_unexport); 1016EXPORT_SYMBOL_GPL(gpiod_unexport);
1001 1017
@@ -1034,8 +1050,7 @@ static int gpiochip_export(struct gpio_chip *chip)
1034 chip->desc[gpio++].chip = NULL; 1050 chip->desc[gpio++].chip = NULL;
1035 spin_unlock_irqrestore(&gpio_lock, flags); 1051 spin_unlock_irqrestore(&gpio_lock, flags);
1036 1052
1037 pr_debug("%s: chip %s status %d\n", __func__, 1053 chip_dbg(chip, "%s: status %d\n", __func__, status);
1038 chip->label, status);
1039 } 1054 }
1040 1055
1041 return status; 1056 return status;
@@ -1051,15 +1066,14 @@ static void gpiochip_unexport(struct gpio_chip *chip)
1051 if (dev) { 1066 if (dev) {
1052 put_device(dev); 1067 put_device(dev);
1053 device_unregister(dev); 1068 device_unregister(dev);
1054 chip->exported = 0; 1069 chip->exported = false;
1055 status = 0; 1070 status = 0;
1056 } else 1071 } else
1057 status = -ENODEV; 1072 status = -ENODEV;
1058 mutex_unlock(&sysfs_lock); 1073 mutex_unlock(&sysfs_lock);
1059 1074
1060 if (status) 1075 if (status)
1061 pr_debug("%s: chip %s status %d\n", __func__, 1076 chip_dbg(chip, "%s: status %d\n", __func__, status);
1062 chip->label, status);
1063} 1077}
1064 1078
1065static int __init gpiolib_sysfs_init(void) 1079static int __init gpiolib_sysfs_init(void)
@@ -1213,6 +1227,7 @@ int gpiochip_add(struct gpio_chip *chip)
1213#endif 1227#endif
1214 1228
1215 of_gpiochip_add(chip); 1229 of_gpiochip_add(chip);
1230 acpi_gpiochip_add(chip);
1216 1231
1217 if (status) 1232 if (status)
1218 goto fail; 1233 goto fail;
@@ -1221,7 +1236,7 @@ int gpiochip_add(struct gpio_chip *chip)
1221 if (status) 1236 if (status)
1222 goto fail; 1237 goto fail;
1223 1238
1224 pr_debug("gpiochip_add: registered GPIOs %d to %d on device: %s\n", 1239 pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__,
1225 chip->base, chip->base + chip->ngpio - 1, 1240 chip->base, chip->base + chip->ngpio - 1,
1226 chip->label ? : "generic"); 1241 chip->label ? : "generic");
1227 1242
@@ -1231,7 +1246,7 @@ unlock:
1231 spin_unlock_irqrestore(&gpio_lock, flags); 1246 spin_unlock_irqrestore(&gpio_lock, flags);
1232fail: 1247fail:
1233 /* failures here can mean systems won't boot... */ 1248 /* failures here can mean systems won't boot... */
1234 pr_err("gpiochip_add: gpios %d..%d (%s) failed to register\n", 1249 pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__,
1235 chip->base, chip->base + chip->ngpio - 1, 1250 chip->base, chip->base + chip->ngpio - 1,
1236 chip->label ? : "generic"); 1251 chip->label ? : "generic");
1237 return status; 1252 return status;
@@ -1254,6 +1269,7 @@ int gpiochip_remove(struct gpio_chip *chip)
1254 1269
1255 gpiochip_remove_pin_ranges(chip); 1270 gpiochip_remove_pin_ranges(chip);
1256 of_gpiochip_remove(chip); 1271 of_gpiochip_remove(chip);
1272 acpi_gpiochip_remove(chip);
1257 1273
1258 for (id = 0; id < chip->ngpio; id++) { 1274 for (id = 0; id < chip->ngpio; id++) {
1259 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) { 1275 if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) {
@@ -1339,8 +1355,7 @@ int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1339 1355
1340 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); 1356 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1341 if (!pin_range) { 1357 if (!pin_range) {
1342 pr_err("%s: GPIO chip: failed to allocate pin ranges\n", 1358 chip_err(chip, "failed to allocate pin ranges\n");
1343 chip->label);
1344 return -ENOMEM; 1359 return -ENOMEM;
1345 } 1360 }
1346 1361
@@ -1361,9 +1376,8 @@ int gpiochip_add_pingroup_range(struct gpio_chip *chip,
1361 1376
1362 pinctrl_add_gpio_range(pctldev, &pin_range->range); 1377 pinctrl_add_gpio_range(pctldev, &pin_range->range);
1363 1378
1364 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n", 1379 chip_dbg(chip, "created GPIO range %d->%d ==> %s PINGRP %s\n",
1365 chip->label, gpio_offset, 1380 gpio_offset, gpio_offset + pin_range->range.npins - 1,
1366 gpio_offset + pin_range->range.npins - 1,
1367 pinctrl_dev_get_devname(pctldev), pin_group); 1381 pinctrl_dev_get_devname(pctldev), pin_group);
1368 1382
1369 list_add_tail(&pin_range->node, &chip->pin_ranges); 1383 list_add_tail(&pin_range->node, &chip->pin_ranges);
@@ -1390,8 +1404,7 @@ int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1390 1404
1391 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); 1405 pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL);
1392 if (!pin_range) { 1406 if (!pin_range) {
1393 pr_err("%s: GPIO chip: failed to allocate pin ranges\n", 1407 chip_err(chip, "failed to allocate pin ranges\n");
1394 chip->label);
1395 return -ENOMEM; 1408 return -ENOMEM;
1396 } 1409 }
1397 1410
@@ -1406,13 +1419,12 @@ int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name,
1406 &pin_range->range); 1419 &pin_range->range);
1407 if (IS_ERR(pin_range->pctldev)) { 1420 if (IS_ERR(pin_range->pctldev)) {
1408 ret = PTR_ERR(pin_range->pctldev); 1421 ret = PTR_ERR(pin_range->pctldev);
1409 pr_err("%s: GPIO chip: could not create pin range\n", 1422 chip_err(chip, "could not create pin range\n");
1410 chip->label);
1411 kfree(pin_range); 1423 kfree(pin_range);
1412 return ret; 1424 return ret;
1413 } 1425 }
1414 pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n", 1426 chip_dbg(chip, "created GPIO range %d->%d ==> %s PIN %d->%d\n",
1415 chip->label, gpio_offset, gpio_offset + npins - 1, 1427 gpio_offset, gpio_offset + npins - 1,
1416 pinctl_name, 1428 pinctl_name,
1417 pin_offset, pin_offset + npins - 1); 1429 pin_offset, pin_offset + npins - 1);
1418 1430
@@ -1499,8 +1511,7 @@ static int gpiod_request(struct gpio_desc *desc, const char *label)
1499 } 1511 }
1500done: 1512done:
1501 if (status) 1513 if (status)
1502 pr_debug("_gpio_request: gpio-%d (%s) status %d\n", 1514 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1503 desc_to_gpio(desc), label ? : "?", status);
1504 spin_unlock_irqrestore(&gpio_lock, flags); 1515 spin_unlock_irqrestore(&gpio_lock, flags);
1505 return status; 1516 return status;
1506} 1517}
@@ -1701,7 +1712,7 @@ int gpiod_direction_input(struct gpio_desc *desc)
1701 if (!chip->get || !chip->direction_input) { 1712 if (!chip->get || !chip->direction_input) {
1702 gpiod_warn(desc, 1713 gpiod_warn(desc,
1703 "%s: missing get() or direction_input() operations\n", 1714 "%s: missing get() or direction_input() operations\n",
1704 __func__); 1715 __func__);
1705 return -EIO; 1716 return -EIO;
1706 } 1717 }
1707 1718
@@ -1721,7 +1732,8 @@ int gpiod_direction_input(struct gpio_desc *desc)
1721 if (status) { 1732 if (status) {
1722 status = chip->request(chip, offset); 1733 status = chip->request(chip, offset);
1723 if (status < 0) { 1734 if (status < 0) {
1724 gpiod_dbg(desc, "chip request fail, %d\n", status); 1735 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1736 __func__, status);
1725 /* and it's not available to anyone else ... 1737 /* and it's not available to anyone else ...
1726 * gpio_request() is the fully clean solution. 1738 * gpio_request() is the fully clean solution.
1727 */ 1739 */
@@ -1739,7 +1751,7 @@ lose:
1739fail: 1751fail:
1740 spin_unlock_irqrestore(&gpio_lock, flags); 1752 spin_unlock_irqrestore(&gpio_lock, flags);
1741 if (status) 1753 if (status)
1742 gpiod_dbg(desc, "%s status %d\n", __func__, status); 1754 gpiod_dbg(desc, "%s: status %d\n", __func__, status);
1743 return status; 1755 return status;
1744} 1756}
1745EXPORT_SYMBOL_GPL(gpiod_direction_input); 1757EXPORT_SYMBOL_GPL(gpiod_direction_input);
@@ -1806,7 +1818,8 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
1806 if (status) { 1818 if (status) {
1807 status = chip->request(chip, offset); 1819 status = chip->request(chip, offset);
1808 if (status < 0) { 1820 if (status < 0) {
1809 gpiod_dbg(desc, "chip request fail, %d\n", status); 1821 gpiod_dbg(desc, "%s: chip request fail, %d\n",
1822 __func__, status);
1810 /* and it's not available to anyone else ... 1823 /* and it's not available to anyone else ...
1811 * gpio_request() is the fully clean solution. 1824 * gpio_request() is the fully clean solution.
1812 */ 1825 */
@@ -2259,18 +2272,14 @@ void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)
2259EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); 2272EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep);
2260 2273
2261/** 2274/**
2262 * gpiod_add_table() - register GPIO device consumers 2275 * gpiod_add_lookup_table() - register GPIO device consumers
2263 * @table: array of consumers to register 2276 * @table: table of consumers to register
2264 * @num: number of consumers in table
2265 */ 2277 */
2266void gpiod_add_table(struct gpiod_lookup *table, size_t size) 2278void gpiod_add_lookup_table(struct gpiod_lookup_table *table)
2267{ 2279{
2268 mutex_lock(&gpio_lookup_lock); 2280 mutex_lock(&gpio_lookup_lock);
2269 2281
2270 while (size--) { 2282 list_add_tail(&table->list, &gpio_lookup_list);
2271 list_add_tail(&table->list, &gpio_lookup_list);
2272 table++;
2273 }
2274 2283
2275 mutex_unlock(&gpio_lookup_lock); 2284 mutex_unlock(&gpio_lookup_lock);
2276} 2285}
@@ -2326,76 +2335,92 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
2326 return desc; 2335 return desc;
2327} 2336}
2328 2337
2329static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, 2338static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
2330 unsigned int idx,
2331 enum gpio_lookup_flags *flags)
2332{ 2339{
2333 const char *dev_id = dev ? dev_name(dev) : NULL; 2340 const char *dev_id = dev ? dev_name(dev) : NULL;
2334 struct gpio_desc *desc = ERR_PTR(-ENODEV); 2341 struct gpiod_lookup_table *table;
2335 unsigned int match, best = 0;
2336 struct gpiod_lookup *p;
2337 2342
2338 mutex_lock(&gpio_lookup_lock); 2343 mutex_lock(&gpio_lookup_lock);
2339 2344
2340 list_for_each_entry(p, &gpio_lookup_list, list) { 2345 list_for_each_entry(table, &gpio_lookup_list, list) {
2341 match = 0; 2346 if (table->dev_id && dev_id) {
2347 /*
2348 * Valid strings on both ends, must be identical to have
2349 * a match
2350 */
2351 if (!strcmp(table->dev_id, dev_id))
2352 goto found;
2353 } else {
2354 /*
2355 * One of the pointers is NULL, so both must be to have
2356 * a match
2357 */
2358 if (dev_id == table->dev_id)
2359 goto found;
2360 }
2361 }
2362 table = NULL;
2342 2363
2343 if (p->dev_id) { 2364found:
2344 if (!dev_id || strcmp(p->dev_id, dev_id)) 2365 mutex_unlock(&gpio_lookup_lock);
2345 continue; 2366 return table;
2367}
2346 2368
2347 match += 2; 2369static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
2348 } 2370 unsigned int idx,
2371 enum gpio_lookup_flags *flags)
2372{
2373 struct gpio_desc *desc = ERR_PTR(-ENOENT);
2374 struct gpiod_lookup_table *table;
2375 struct gpiod_lookup *p;
2349 2376
2350 if (p->con_id) { 2377 table = gpiod_find_lookup_table(dev);
2351 if (!con_id || strcmp(p->con_id, con_id)) 2378 if (!table)
2352 continue; 2379 return desc;
2353 2380
2354 match += 1; 2381 for (p = &table->table[0]; p->chip_label; p++) {
2355 } 2382 struct gpio_chip *chip;
2356 2383
2384 /* idx must always match exactly */
2357 if (p->idx != idx) 2385 if (p->idx != idx)
2358 continue; 2386 continue;
2359 2387
2360 if (match > best) { 2388 /* If the lookup entry has a con_id, require exact match */
2361 struct gpio_chip *chip; 2389 if (p->con_id && (!con_id || strcmp(p->con_id, con_id)))
2390 continue;
2362 2391
2363 chip = find_chip_by_name(p->chip_label); 2392 chip = find_chip_by_name(p->chip_label);
2364 2393
2365 if (!chip) { 2394 if (!chip) {
2366 dev_warn(dev, "cannot find GPIO chip %s\n", 2395 dev_err(dev, "cannot find GPIO chip %s\n",
2367 p->chip_label); 2396 p->chip_label);
2368 continue; 2397 return ERR_PTR(-ENODEV);
2369 } 2398 }
2370 2399
2371 if (chip->ngpio <= p->chip_hwnum) { 2400 if (chip->ngpio <= p->chip_hwnum) {
2372 dev_warn(dev, "GPIO chip %s has %d GPIOs\n", 2401 dev_err(dev,
2373 chip->label, chip->ngpio); 2402 "requested GPIO %d is out of range [0..%d] for chip %s\n",
2374 continue; 2403 idx, chip->ngpio, chip->label);
2375 } 2404 return ERR_PTR(-EINVAL);
2405 }
2376 2406
2377 desc = gpio_to_desc(chip->base + p->chip_hwnum); 2407 desc = gpiochip_offset_to_desc(chip, p->chip_hwnum);
2378 *flags = p->flags; 2408 *flags = p->flags;
2379 2409
2380 if (match != 3) 2410 return desc;
2381 best = match;
2382 else
2383 break;
2384 }
2385 } 2411 }
2386 2412
2387 mutex_unlock(&gpio_lookup_lock);
2388
2389 return desc; 2413 return desc;
2390} 2414}
2391 2415
2392/** 2416/**
2393 * gpio_get - obtain a GPIO for a given GPIO function 2417 * gpio_get - obtain a GPIO for a given GPIO function
2394 * @dev: GPIO consumer 2418 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2395 * @con_id: function within the GPIO consumer 2419 * @con_id: function within the GPIO consumer
2396 * 2420 *
2397 * Return the GPIO descriptor corresponding to the function con_id of device 2421 * Return the GPIO descriptor corresponding to the function con_id of device
2398 * dev, or an IS_ERR() condition if an error occured. 2422 * dev, -ENOENT if no GPIO has been assigned to the requested function, or
2423 * another IS_ERR() code if an error occured while trying to acquire the GPIO.
2399 */ 2424 */
2400struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id) 2425struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id)
2401{ 2426{
@@ -2405,14 +2430,16 @@ EXPORT_SYMBOL_GPL(gpiod_get);
2405 2430
2406/** 2431/**
2407 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function 2432 * gpiod_get_index - obtain a GPIO from a multi-index GPIO function
2408 * @dev: GPIO consumer 2433 * @dev: GPIO consumer, can be NULL for system-global GPIOs
2409 * @con_id: function within the GPIO consumer 2434 * @con_id: function within the GPIO consumer
2410 * @idx: index of the GPIO to obtain in the consumer 2435 * @idx: index of the GPIO to obtain in the consumer
2411 * 2436 *
2412 * This variant of gpiod_get() allows to access GPIOs other than the first 2437 * This variant of gpiod_get() allows to access GPIOs other than the first
2413 * defined one for functions that define several GPIOs. 2438 * defined one for functions that define several GPIOs.
2414 * 2439 *
2415 * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error. 2440 * Return a valid GPIO descriptor, -ENOENT if no GPIO has been assigned to the
2441 * requested function and/or index, or another IS_ERR() code if an error
2442 * occured while trying to acquire the GPIO.
2416 */ 2443 */
2417struct gpio_desc *__must_check gpiod_get_index(struct device *dev, 2444struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2418 const char *con_id, 2445 const char *con_id,
@@ -2437,13 +2464,9 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
2437 * Either we are not using DT or ACPI, or their lookup did not return 2464 * Either we are not using DT or ACPI, or their lookup did not return
2438 * a result. In that case, use platform lookup as a fallback. 2465 * a result. In that case, use platform lookup as a fallback.
2439 */ 2466 */
2440 if (!desc || IS_ERR(desc)) { 2467 if (!desc || desc == ERR_PTR(-ENOENT)) {
2441 struct gpio_desc *pdesc;
2442 dev_dbg(dev, "using lookup tables for GPIO lookup"); 2468 dev_dbg(dev, "using lookup tables for GPIO lookup");
2443 pdesc = gpiod_find(dev, con_id, idx, &flags); 2469 desc = gpiod_find(dev, con_id, idx, &flags);
2444 /* If used as fallback, do not replace the previous error */
2445 if (!IS_ERR(pdesc) || !desc)
2446 desc = pdesc;
2447 } 2470 }
2448 2471
2449 if (IS_ERR(desc)) { 2472 if (IS_ERR(desc)) {