Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 1 | /* |
| 2 | * OF helpers for the GPIO API |
| 3 | * |
| 4 | * Copyright (c) 2007-2008 MontaVista Software, Inc. |
| 5 | * |
| 6 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 14 | #include <linux/device.h> |
Sachin Kamat | bea4dbe | 2014-01-27 12:15:08 +0530 | [diff] [blame] | 15 | #include <linux/err.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 16 | #include <linux/errno.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 17 | #include <linux/module.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 18 | #include <linux/io.h> |
Linus Walleij | 7d4defe | 2016-06-08 10:58:20 +0200 | [diff] [blame] | 19 | #include <linux/io-mapping.h> |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 20 | #include <linux/gpio/consumer.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 21 | #include <linux/of.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 22 | #include <linux/of_address.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 23 | #include <linux/of_gpio.h> |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 24 | #include <linux/pinctrl/pinctrl.h> |
Grant Likely | 2e13cba | 2010-07-05 16:11:55 -0600 | [diff] [blame] | 25 | #include <linux/slab.h> |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 26 | #include <linux/gpio/machine.h> |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 27 | |
Alexandre Courbot | 1bd6b60 | 2014-07-22 16:17:41 +0900 | [diff] [blame] | 28 | #include "gpiolib.h" |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 29 | |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 30 | static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 31 | { |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 32 | return chip->gpiodev->dev.of_node == data; |
| 33 | } |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 34 | |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 35 | static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np) |
| 36 | { |
| 37 | return gpiochip_find(np, of_gpiochip_match_node); |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 38 | } |
| 39 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 40 | static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, |
| 41 | struct of_phandle_args *gpiospec, |
| 42 | enum of_gpio_flags *flags) |
| 43 | { |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 44 | int ret; |
| 45 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 46 | if (chip->of_gpio_n_cells != gpiospec->args_count) |
| 47 | return ERR_PTR(-EINVAL); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 48 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 49 | ret = chip->of_xlate(chip, gpiospec, flags); |
| 50 | if (ret < 0) |
| 51 | return ERR_PTR(ret); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 52 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 53 | return gpiochip_get_desc(chip, ret); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 54 | } |
| 55 | |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 56 | /** |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 57 | * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 58 | * @np: device node to get GPIO from |
John Bonesio | a6b0919 | 2011-06-27 16:49:57 -0700 | [diff] [blame] | 59 | * @propname: property name containing gpio specifier(s) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 60 | * @index: index of the GPIO |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 61 | * @flags: a flags pointer to fill in |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 62 | * |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 63 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 64 | * value on the error condition. If @flags is not NULL the function also fills |
| 65 | * in flags for the GPIO. |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 66 | */ |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 67 | struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, |
| 68 | const char *propname, int index, enum of_gpio_flags *flags) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 69 | { |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 70 | struct of_phandle_args gpiospec; |
| 71 | struct gpio_chip *chip; |
| 72 | struct gpio_desc *desc; |
Anton Vorontsov | 64b60e0 | 2008-10-10 04:43:17 +0000 | [diff] [blame] | 73 | int ret; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 74 | |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 75 | ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index, |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 76 | &gpiospec); |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 77 | if (ret) { |
Tushar Behera | 85ea29a | 2014-07-04 15:22:09 +0530 | [diff] [blame] | 78 | pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n", |
| 79 | __func__, propname, np->full_name, index); |
Alexandre Courbot | af8b637 | 2013-10-17 10:21:37 -0700 | [diff] [blame] | 80 | return ERR_PTR(ret); |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 81 | } |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 82 | |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 83 | chip = of_find_gpiochip_by_node(gpiospec.np); |
| 84 | if (!chip) { |
| 85 | desc = ERR_PTR(-EPROBE_DEFER); |
| 86 | goto out; |
| 87 | } |
Grant Likely | 3d0f7cf | 2012-05-17 13:54:40 -0600 | [diff] [blame] | 88 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 89 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 90 | if (IS_ERR(desc)) |
| 91 | goto out; |
| 92 | |
Tushar Behera | 85ea29a | 2014-07-04 15:22:09 +0530 | [diff] [blame] | 93 | pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n", |
| 94 | __func__, propname, np->full_name, index, |
Masahiro Yamada | 762c2e4 | 2016-06-14 19:07:06 +0900 | [diff] [blame] | 95 | PTR_ERR_OR_ZERO(desc)); |
| 96 | |
| 97 | out: |
| 98 | of_node_put(gpiospec.np); |
| 99 | |
| 100 | return desc; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 101 | } |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 102 | |
Alexandre Courbot | f01d907 | 2014-05-17 14:54:50 +0900 | [diff] [blame] | 103 | int of_get_named_gpio_flags(struct device_node *np, const char *list_name, |
| 104 | int index, enum of_gpio_flags *flags) |
| 105 | { |
| 106 | struct gpio_desc *desc; |
| 107 | |
| 108 | desc = of_get_named_gpiod_flags(np, list_name, index, flags); |
| 109 | |
| 110 | if (IS_ERR(desc)) |
| 111 | return PTR_ERR(desc); |
| 112 | else |
| 113 | return desc_to_gpio(desc); |
| 114 | } |
| 115 | EXPORT_SYMBOL(of_get_named_gpio_flags); |
| 116 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 117 | /** |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 118 | * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 119 | * @np: device node to get GPIO from |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 120 | * @chip: GPIO chip whose hog is parsed |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 121 | * @name: GPIO line name |
| 122 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 123 | * of_parse_own_gpio() |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 124 | * @dflags: gpiod_flags - optional GPIO initialization flags |
| 125 | * |
| 126 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
| 127 | * value on the error condition. |
| 128 | */ |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 129 | static struct gpio_desc *of_parse_own_gpio(struct device_node *np, |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 130 | struct gpio_chip *chip, |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 131 | const char **name, |
| 132 | enum gpio_lookup_flags *lflags, |
| 133 | enum gpiod_flags *dflags) |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 134 | { |
| 135 | struct device_node *chip_np; |
| 136 | enum of_gpio_flags xlate_flags; |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 137 | struct of_phandle_args gpiospec; |
| 138 | struct gpio_desc *desc; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 139 | u32 tmp; |
Masahiro Yamada | 3f9547e | 2016-06-14 19:07:03 +0900 | [diff] [blame] | 140 | int ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 141 | |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 142 | chip_np = chip->of_node; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 143 | if (!chip_np) |
| 144 | return ERR_PTR(-EINVAL); |
| 145 | |
| 146 | xlate_flags = 0; |
| 147 | *lflags = 0; |
| 148 | *dflags = 0; |
| 149 | |
| 150 | ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); |
| 151 | if (ret) |
| 152 | return ERR_PTR(ret); |
| 153 | |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 154 | gpiospec.np = chip_np; |
| 155 | gpiospec.args_count = tmp; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 156 | |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 157 | ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp); |
Masahiro Yamada | 3f9547e | 2016-06-14 19:07:03 +0900 | [diff] [blame] | 158 | if (ret) |
| 159 | return ERR_PTR(ret); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 160 | |
Masahiro Yamada | 99468c1 | 2016-06-14 19:07:07 +0900 | [diff] [blame] | 161 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 162 | if (IS_ERR(desc)) |
| 163 | return desc; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 164 | |
| 165 | if (xlate_flags & OF_GPIO_ACTIVE_LOW) |
| 166 | *lflags |= GPIO_ACTIVE_LOW; |
| 167 | |
| 168 | if (of_property_read_bool(np, "input")) |
| 169 | *dflags |= GPIOD_IN; |
| 170 | else if (of_property_read_bool(np, "output-low")) |
| 171 | *dflags |= GPIOD_OUT_LOW; |
| 172 | else if (of_property_read_bool(np, "output-high")) |
| 173 | *dflags |= GPIOD_OUT_HIGH; |
| 174 | else { |
| 175 | pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n", |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 176 | desc_to_gpio(desc), np->name); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 177 | return ERR_PTR(-EINVAL); |
| 178 | } |
| 179 | |
| 180 | if (name && of_property_read_string(np, "line-name", name)) |
| 181 | *name = np->name; |
| 182 | |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 183 | return desc; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Linus Walleij | fd9c553 | 2016-04-19 15:26:26 +0200 | [diff] [blame] | 187 | * of_gpiochip_set_names() - set up the names of the lines |
| 188 | * @chip: GPIO chip whose lines should be named, if possible |
| 189 | */ |
| 190 | static void of_gpiochip_set_names(struct gpio_chip *gc) |
| 191 | { |
| 192 | struct gpio_device *gdev = gc->gpiodev; |
| 193 | struct device_node *np = gc->of_node; |
| 194 | int i; |
| 195 | int nstrings; |
| 196 | |
| 197 | nstrings = of_property_count_strings(np, "gpio-line-names"); |
| 198 | if (nstrings <= 0) |
| 199 | /* Lines names not present */ |
| 200 | return; |
| 201 | |
| 202 | /* This is normally not what you want */ |
| 203 | if (gdev->ngpio != nstrings) |
| 204 | dev_info(&gdev->dev, "gpio-line-names specifies %d line " |
| 205 | "names but there are %d lines on the chip\n", |
| 206 | nstrings, gdev->ngpio); |
| 207 | |
| 208 | /* |
| 209 | * Make sure to not index beyond the end of the number of descriptors |
| 210 | * of the GPIO device. |
| 211 | */ |
| 212 | for (i = 0; i < gdev->ngpio; i++) { |
| 213 | const char *name; |
| 214 | int ret; |
| 215 | |
| 216 | ret = of_property_read_string_index(np, |
| 217 | "gpio-line-names", |
| 218 | i, |
| 219 | &name); |
| 220 | if (ret) { |
| 221 | if (ret != -ENODATA) |
| 222 | dev_err(&gdev->dev, |
| 223 | "unable to name line %d: %d\n", |
| 224 | i, ret); |
| 225 | break; |
| 226 | } |
| 227 | gdev->descs[i].name = name; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
Markus Pargmann | fd7337f | 2015-08-14 16:10:58 +0200 | [diff] [blame] | 232 | * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 233 | * @chip: gpio chip to act on |
| 234 | * |
| 235 | * This is only used by of_gpiochip_add to request/set GPIO initial |
| 236 | * configuration. |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 237 | * It retures error if it fails otherwise 0 on success. |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 238 | */ |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 239 | static int of_gpiochip_scan_gpios(struct gpio_chip *chip) |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 240 | { |
| 241 | struct gpio_desc *desc = NULL; |
| 242 | struct device_node *np; |
| 243 | const char *name; |
| 244 | enum gpio_lookup_flags lflags; |
| 245 | enum gpiod_flags dflags; |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 246 | int ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 247 | |
Laxman Dewangan | d1279d9 | 2016-03-11 19:13:20 +0530 | [diff] [blame] | 248 | for_each_available_child_of_node(chip->of_node, np) { |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 249 | if (!of_property_read_bool(np, "gpio-hog")) |
| 250 | continue; |
| 251 | |
Masahiro Yamada | be71534 | 2016-06-14 19:07:04 +0900 | [diff] [blame] | 252 | desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 253 | if (IS_ERR(desc)) |
| 254 | continue; |
| 255 | |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 256 | ret = gpiod_hog(desc, name, lflags, dflags); |
| 257 | if (ret < 0) |
| 258 | return ret; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 259 | } |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 260 | |
| 261 | return 0; |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 265 | * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 266 | * @gc: pointer to the gpio_chip structure |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 267 | * @np: device node of the GPIO chip |
| 268 | * @gpio_spec: gpio specifier as found in the device tree |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 269 | * @flags: a flags pointer to fill in |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 270 | * |
| 271 | * This is simple translation function, suitable for the most 1:1 mapped |
| 272 | * gpio chips. This function performs only one sanity check: whether gpio |
| 273 | * is less than ngpios (that is specified in the gpio_chip). |
| 274 | */ |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 275 | int of_gpio_simple_xlate(struct gpio_chip *gc, |
| 276 | const struct of_phandle_args *gpiospec, u32 *flags) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 277 | { |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 278 | /* |
| 279 | * We're discouraging gpio_cells < 2, since that way you'll have to |
Colin Cronin | 20a8a96 | 2015-05-18 11:41:43 -0700 | [diff] [blame] | 280 | * write your own xlate function (that will have to retrieve the GPIO |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 281 | * number and the flags from a single gpio cell -- this is possible, |
| 282 | * but not recommended). |
| 283 | */ |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 284 | if (gc->of_gpio_n_cells < 2) { |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 285 | WARN_ON(1); |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 289 | if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) |
| 290 | return -EINVAL; |
| 291 | |
Roland Stigge | 6270d83 | 2012-04-04 02:02:58 +0200 | [diff] [blame] | 292 | if (gpiospec->args[0] >= gc->ngpio) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 295 | if (flags) |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 296 | *flags = gpiospec->args[1]; |
Anton Vorontsov | b908b53 | 2008-12-01 06:30:04 +0000 | [diff] [blame] | 297 | |
Grant Likely | 15c9a0a | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 298 | return gpiospec->args[0]; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 299 | } |
Jamie Iles | 3038bbd | 2011-07-28 16:25:41 +0100 | [diff] [blame] | 300 | EXPORT_SYMBOL(of_gpio_simple_xlate); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 301 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 302 | /** |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 303 | * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 304 | * @np: device node of the GPIO chip |
| 305 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 306 | * @data: driver data to store in the struct gpio_chip |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 307 | * |
| 308 | * To use this function you should allocate and fill mm_gc with: |
| 309 | * |
| 310 | * 1) In the gpio_chip structure: |
| 311 | * - all the callbacks |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 312 | * - of_gpio_n_cells |
| 313 | * - of_xlate callback (optional) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 314 | * |
| 315 | * 3) In the of_mm_gpio_chip structure: |
| 316 | * - save_regs callback (optional) |
| 317 | * |
| 318 | * If succeeded, this function will map bank's memory and will |
| 319 | * do all necessary work for you. Then you'll able to use .regs |
| 320 | * to manage GPIOs from the callbacks. |
| 321 | */ |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 322 | int of_mm_gpiochip_add_data(struct device_node *np, |
| 323 | struct of_mm_gpio_chip *mm_gc, |
| 324 | void *data) |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 325 | { |
| 326 | int ret = -ENOMEM; |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 327 | struct gpio_chip *gc = &mm_gc->gc; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 328 | |
| 329 | gc->label = kstrdup(np->full_name, GFP_KERNEL); |
| 330 | if (!gc->label) |
| 331 | goto err0; |
| 332 | |
| 333 | mm_gc->regs = of_iomap(np, 0); |
| 334 | if (!mm_gc->regs) |
| 335 | goto err1; |
| 336 | |
Anton Vorontsov | 2145115 | 2008-04-30 00:05:24 +1000 | [diff] [blame] | 337 | gc->base = -1; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 338 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 339 | if (mm_gc->save_regs) |
| 340 | mm_gc->save_regs(mm_gc); |
| 341 | |
Anton Vorontsov | a19e3da | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 342 | mm_gc->gc.of_node = np; |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 343 | |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 344 | ret = gpiochip_add_data(gc, data); |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 345 | if (ret) |
| 346 | goto err2; |
| 347 | |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 348 | return 0; |
| 349 | err2: |
Anton Vorontsov | 863fbf4 | 2008-04-11 23:06:45 +1000 | [diff] [blame] | 350 | iounmap(mm_gc->regs); |
| 351 | err1: |
| 352 | kfree(gc->label); |
| 353 | err0: |
| 354 | pr_err("%s: GPIO chip registration failed with status %d\n", |
| 355 | np->full_name, ret); |
| 356 | return ret; |
| 357 | } |
Linus Walleij | 3208b0f | 2015-12-04 15:13:53 +0100 | [diff] [blame] | 358 | EXPORT_SYMBOL(of_mm_gpiochip_add_data); |
Grant Likely | 594fa26 | 2010-06-08 07:48:16 -0600 | [diff] [blame] | 359 | |
Ricardo Ribalda Delgado | d621e8b | 2014-12-17 16:51:13 +0100 | [diff] [blame] | 360 | /** |
| 361 | * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) |
| 362 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure |
| 363 | */ |
| 364 | void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) |
| 365 | { |
| 366 | struct gpio_chip *gc = &mm_gc->gc; |
| 367 | |
| 368 | if (!mm_gc) |
| 369 | return; |
| 370 | |
| 371 | gpiochip_remove(gc); |
| 372 | iounmap(mm_gc->regs); |
| 373 | kfree(gc->label); |
| 374 | } |
| 375 | EXPORT_SYMBOL(of_mm_gpiochip_remove); |
| 376 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 377 | #ifdef CONFIG_PINCTRL |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 378 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 379 | { |
| 380 | struct device_node *np = chip->of_node; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 381 | struct of_phandle_args pinspec; |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 382 | struct pinctrl_dev *pctldev; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 383 | int index = 0, ret; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 384 | const char *name; |
| 385 | static const char group_names_propname[] = "gpio-ranges-group-names"; |
| 386 | struct property *group_names; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 387 | |
| 388 | if (!np) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 389 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 390 | |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 391 | group_names = of_find_property(np, group_names_propname, NULL); |
| 392 | |
Haojian Zhuang | ad4e1a7 | 2013-02-17 19:42:48 +0800 | [diff] [blame] | 393 | for (;; index++) { |
Stephen Warren | d9fe003 | 2013-08-14 15:27:12 -0600 | [diff] [blame] | 394 | ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, |
| 395 | index, &pinspec); |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 396 | if (ret) |
| 397 | break; |
| 398 | |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 399 | pctldev = of_pinctrl_get(pinspec.np); |
Masahiro Yamada | 602cf63 | 2016-05-23 10:52:09 +0900 | [diff] [blame] | 400 | of_node_put(pinspec.np); |
Linus Walleij | 1e63d7b | 2012-11-06 16:03:35 +0100 | [diff] [blame] | 401 | if (!pctldev) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 402 | return -EPROBE_DEFER; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 403 | |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 404 | if (pinspec.args[2]) { |
| 405 | if (group_names) { |
Laurent Navet | 7285860 | 2015-07-07 22:22:15 +0200 | [diff] [blame] | 406 | of_property_read_string_index(np, |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 407 | group_names_propname, |
| 408 | index, &name); |
| 409 | if (strlen(name)) { |
| 410 | pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n", |
| 411 | np->full_name); |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | /* npins != 0: linear range */ |
| 416 | ret = gpiochip_add_pin_range(chip, |
| 417 | pinctrl_dev_get_devname(pctldev), |
| 418 | pinspec.args[0], |
| 419 | pinspec.args[1], |
| 420 | pinspec.args[2]); |
| 421 | if (ret) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 422 | return ret; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 423 | } else { |
| 424 | /* npins == 0: special range */ |
| 425 | if (pinspec.args[1]) { |
| 426 | pr_err("%s: Illegal gpio-range format.\n", |
| 427 | np->full_name); |
| 428 | break; |
| 429 | } |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 430 | |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 431 | if (!group_names) { |
| 432 | pr_err("%s: GPIO group range requested but no %s property.\n", |
| 433 | np->full_name, group_names_propname); |
| 434 | break; |
| 435 | } |
| 436 | |
| 437 | ret = of_property_read_string_index(np, |
| 438 | group_names_propname, |
| 439 | index, &name); |
| 440 | if (ret) |
| 441 | break; |
| 442 | |
| 443 | if (!strlen(name)) { |
| 444 | pr_err("%s: Group name of GPIO group range cannot be the empty string.\n", |
| 445 | np->full_name); |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | ret = gpiochip_add_pingroup_range(chip, pctldev, |
| 450 | pinspec.args[0], name); |
| 451 | if (ret) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 452 | return ret; |
Christian Ruppert | 586a87e | 2013-10-15 15:37:54 +0200 | [diff] [blame] | 453 | } |
Haojian Zhuang | ad4e1a7 | 2013-02-17 19:42:48 +0800 | [diff] [blame] | 454 | } |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 455 | |
| 456 | return 0; |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 457 | } |
| 458 | |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 459 | #else |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 460 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } |
Shiraz Hashim | f23f151 | 2012-10-27 15:21:36 +0530 | [diff] [blame] | 461 | #endif |
| 462 | |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 463 | int of_gpiochip_add(struct gpio_chip *chip) |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 464 | { |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 465 | int status; |
| 466 | |
Linus Walleij | 58383c7 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 467 | if ((!chip->of_node) && (chip->parent)) |
| 468 | chip->of_node = chip->parent->of_node; |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 469 | |
| 470 | if (!chip->of_node) |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 471 | return 0; |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 472 | |
| 473 | if (!chip->of_xlate) { |
| 474 | chip->of_gpio_n_cells = 2; |
| 475 | chip->of_xlate = of_gpio_simple_xlate; |
| 476 | } |
| 477 | |
Masahiro Yamada | 1020dfd | 2016-06-14 19:07:05 +0900 | [diff] [blame] | 478 | if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) |
| 479 | return -EINVAL; |
| 480 | |
Tomeu Vizoso | 28355f8 | 2015-07-14 10:29:54 +0200 | [diff] [blame] | 481 | status = of_gpiochip_add_pin_range(chip); |
| 482 | if (status) |
| 483 | return status; |
| 484 | |
Linus Walleij | fd9c553 | 2016-04-19 15:26:26 +0200 | [diff] [blame] | 485 | /* If the chip defines names itself, these take precedence */ |
| 486 | if (!chip->names) |
| 487 | of_gpiochip_set_names(chip); |
| 488 | |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 489 | of_node_get(chip->of_node); |
Benoit Parrot | f625d46 | 2015-02-02 11:44:44 -0600 | [diff] [blame] | 490 | |
Laxman Dewangan | dfbd379 | 2016-03-11 19:13:22 +0530 | [diff] [blame] | 491 | return of_gpiochip_scan_gpios(chip); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void of_gpiochip_remove(struct gpio_chip *chip) |
| 495 | { |
Linus Walleij | e93fa3f | 2012-11-06 15:03:47 +0100 | [diff] [blame] | 496 | gpiochip_remove_pin_ranges(chip); |
Julia Lawall | 8a69155 | 2014-08-08 12:07:51 +0200 | [diff] [blame] | 497 | of_node_put(chip->of_node); |
Anton Vorontsov | 391c970 | 2010-06-08 07:48:17 -0600 | [diff] [blame] | 498 | } |