Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gpio: of: Break out OF-only code

The core gpiolib should not contain any OF/device tree-only
code. Try to break out the main part of it and push it down
into the optional gpiolib-of.c part of the library.

Create a local gpiolib-of.h header and move stuff around a
bit to get a clean cut.

Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20190717071001.3858-1-linus.walleij@linaro.org

+167 -132
+114
drivers/gpio/gpiolib-of.c
··· 21 21 #include <linux/gpio/machine.h> 22 22 23 23 #include "gpiolib.h" 24 + #include "gpiolib-of.h" 25 + 26 + /* 27 + * This is used by external users of of_gpio_count() from <linux/of_gpio.h> 28 + * 29 + * FIXME: get rid of those external users by converting them to GPIO 30 + * descriptors and let them all use gpiod_get_count() 31 + */ 32 + int of_gpio_get_count(struct device *dev, const char *con_id) 33 + { 34 + int ret; 35 + char propname[32]; 36 + unsigned int i; 37 + 38 + for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 39 + if (con_id) 40 + snprintf(propname, sizeof(propname), "%s-%s", 41 + con_id, gpio_suffixes[i]); 42 + else 43 + snprintf(propname, sizeof(propname), "%s", 44 + gpio_suffixes[i]); 45 + 46 + ret = of_gpio_named_count(dev->of_node, propname); 47 + if (ret > 0) 48 + break; 49 + } 50 + return ret ? ret : -ENOENT; 51 + } 24 52 25 53 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) 26 54 { ··· 79 51 return ERR_PTR(ret); 80 52 81 53 return gpiochip_get_desc(chip, ret); 54 + } 55 + 56 + /** 57 + * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs 58 + * to set the .valid_mask 59 + * @dev: the device for the GPIO provider 60 + * @return: true if the valid mask needs to be set 61 + */ 62 + bool of_gpio_need_valid_mask(struct gpio_chip *gc) 63 + { 64 + int size; 65 + struct device_node *np = gc->of_node; 66 + 67 + size = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 68 + if (size > 0 && size % 2 == 0) 69 + return true; 70 + return false; 82 71 } 83 72 84 73 static void of_gpio_flags_quirks(struct device_node *np, ··· 275 230 return desc_to_gpio(desc); 276 231 } 277 232 EXPORT_SYMBOL(of_get_named_gpio_flags); 233 + 234 + /** 235 + * gpiod_get_from_of_node() - obtain a GPIO from an OF node 236 + * @node: handle of the OF node 237 + * @propname: name of the DT property representing the GPIO 238 + * @index: index of the GPIO to obtain for the consumer 239 + * @dflags: GPIO initialization flags 240 + * @label: label to attach to the requested GPIO 241 + * 242 + * Returns: 243 + * On successful request the GPIO pin is configured in accordance with 244 + * provided @dflags. 245 + * 246 + * In case of error an ERR_PTR() is returned. 247 + */ 248 + struct gpio_desc *gpiod_get_from_of_node(struct device_node *node, 249 + const char *propname, int index, 250 + enum gpiod_flags dflags, 251 + const char *label) 252 + { 253 + unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 254 + struct gpio_desc *desc; 255 + enum of_gpio_flags flags; 256 + bool active_low = false; 257 + bool single_ended = false; 258 + bool open_drain = false; 259 + bool transitory = false; 260 + int ret; 261 + 262 + desc = of_get_named_gpiod_flags(node, propname, 263 + index, &flags); 264 + 265 + if (!desc || IS_ERR(desc)) { 266 + return desc; 267 + } 268 + 269 + active_low = flags & OF_GPIO_ACTIVE_LOW; 270 + single_ended = flags & OF_GPIO_SINGLE_ENDED; 271 + open_drain = flags & OF_GPIO_OPEN_DRAIN; 272 + transitory = flags & OF_GPIO_TRANSITORY; 273 + 274 + ret = gpiod_request(desc, label); 275 + if (ret == -EBUSY && (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) 276 + return desc; 277 + if (ret) 278 + return ERR_PTR(ret); 279 + 280 + if (active_low) 281 + lflags |= GPIO_ACTIVE_LOW; 282 + 283 + if (single_ended) { 284 + if (open_drain) 285 + lflags |= GPIO_OPEN_DRAIN; 286 + else 287 + lflags |= GPIO_OPEN_SOURCE; 288 + } 289 + 290 + if (transitory) 291 + lflags |= GPIO_TRANSITORY; 292 + 293 + ret = gpiod_configure_flags(desc, propname, lflags, dflags); 294 + if (ret < 0) { 295 + gpiod_put(desc); 296 + return ERR_PTR(ret); 297 + } 298 + 299 + return desc; 300 + } 301 + EXPORT_SYMBOL(gpiod_get_from_of_node); 278 302 279 303 /* 280 304 * The SPI GPIO bindings happened before we managed to establish that GPIO
+45
drivers/gpio/gpiolib-of.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + 3 + #ifndef GPIOLIB_OF_H 4 + #define GPIOLIB_OF_H 5 + 6 + struct gpio_chip; 7 + enum of_gpio_flags; 8 + 9 + #ifdef CONFIG_OF_GPIO 10 + struct gpio_desc *of_find_gpio(struct device *dev, 11 + const char *con_id, 12 + unsigned int idx, 13 + unsigned long *lookupflags); 14 + struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 15 + const char *list_name, int index, enum of_gpio_flags *flags); 16 + int of_gpiochip_add(struct gpio_chip *gc); 17 + void of_gpiochip_remove(struct gpio_chip *gc); 18 + int of_gpio_get_count(struct device *dev, const char *con_id); 19 + bool of_gpio_need_valid_mask(struct gpio_chip *gc); 20 + #else 21 + static inline struct gpio_desc *of_find_gpio(struct device *dev, 22 + const char *con_id, 23 + unsigned int idx, 24 + unsigned long *lookupflags) 25 + { 26 + return ERR_PTR(-ENOENT); 27 + } 28 + static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 29 + const char *list_name, int index, enum of_gpio_flags *flags) 30 + { 31 + return ERR_PTR(-ENOENT); 32 + } 33 + static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; } 34 + static inline void of_gpiochip_remove(struct gpio_chip *gc) { } 35 + static inline int of_gpio_get_count(struct device *dev, const char *con_id) 36 + { 37 + return 0; 38 + } 39 + static inline bool of_gpio_need_valid_mask(struct gpio_chip *gc) 40 + { 41 + return false; 42 + } 43 + #endif /* CONFIG_OF_GPIO */ 44 + 45 + #endif /* GPIOLIB_OF_H */
+8 -105
drivers/gpio/gpiolib.c
··· 11 11 #include <linux/debugfs.h> 12 12 #include <linux/seq_file.h> 13 13 #include <linux/gpio.h> 14 - #include <linux/of_gpio.h> 15 14 #include <linux/idr.h> 16 15 #include <linux/slab.h> 17 16 #include <linux/acpi.h> ··· 29 30 #include <uapi/linux/gpio.h> 30 31 31 32 #include "gpiolib.h" 33 + #include "gpiolib-of.h" 32 34 33 35 #define CREATE_TRACE_POINTS 34 36 #include <trace/events/gpio.h> ··· 360 360 return p; 361 361 } 362 362 363 - static int gpiochip_alloc_valid_mask(struct gpio_chip *gpiochip) 363 + static int gpiochip_alloc_valid_mask(struct gpio_chip *gc) 364 364 { 365 - #ifdef CONFIG_OF_GPIO 366 - int size; 367 - struct device_node *np = gpiochip->of_node; 368 - 369 - size = of_property_count_u32_elems(np, "gpio-reserved-ranges"); 370 - if (size > 0 && size % 2 == 0) 371 - gpiochip->need_valid_mask = true; 372 - #endif 373 - 374 - if (!gpiochip->need_valid_mask) 365 + if (IS_ENABLED(CONFIG_OF_GPIO)) 366 + gc->need_valid_mask = of_gpio_need_valid_mask(gc); 367 + if (!gc->need_valid_mask) 375 368 return 0; 376 369 377 - gpiochip->valid_mask = gpiochip_allocate_mask(gpiochip); 378 - if (!gpiochip->valid_mask) 370 + gc->valid_mask = gpiochip_allocate_mask(gc); 371 + if (!gc->valid_mask) 379 372 return -ENOMEM; 380 373 381 374 return 0; ··· 3986 3993 return desc; 3987 3994 } 3988 3995 3989 - static int dt_gpio_count(struct device *dev, const char *con_id) 3990 - { 3991 - int ret; 3992 - char propname[32]; 3993 - unsigned int i; 3994 - 3995 - for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { 3996 - if (con_id) 3997 - snprintf(propname, sizeof(propname), "%s-%s", 3998 - con_id, gpio_suffixes[i]); 3999 - else 4000 - snprintf(propname, sizeof(propname), "%s", 4001 - gpio_suffixes[i]); 4002 - 4003 - ret = of_gpio_named_count(dev->of_node, propname); 4004 - if (ret > 0) 4005 - break; 4006 - } 4007 - return ret ? ret : -ENOENT; 4008 - } 4009 - 4010 3996 static int platform_gpio_count(struct device *dev, const char *con_id) 4011 3997 { 4012 3998 struct gpiod_lookup_table *table; ··· 4018 4046 int count = -ENOENT; 4019 4047 4020 4048 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) 4021 - count = dt_gpio_count(dev, con_id); 4049 + count = of_gpio_get_count(dev, con_id); 4022 4050 else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) 4023 4051 count = acpi_gpio_count(dev, con_id); 4024 4052 ··· 4218 4246 return desc; 4219 4247 } 4220 4248 EXPORT_SYMBOL_GPL(gpiod_get_index); 4221 - 4222 - /** 4223 - * gpiod_get_from_of_node() - obtain a GPIO from an OF node 4224 - * @node: handle of the OF node 4225 - * @propname: name of the DT property representing the GPIO 4226 - * @index: index of the GPIO to obtain for the consumer 4227 - * @dflags: GPIO initialization flags 4228 - * @label: label to attach to the requested GPIO 4229 - * 4230 - * Returns: 4231 - * On successful request the GPIO pin is configured in accordance with 4232 - * provided @dflags. 4233 - * 4234 - * In case of error an ERR_PTR() is returned. 4235 - */ 4236 - struct gpio_desc *gpiod_get_from_of_node(struct device_node *node, 4237 - const char *propname, int index, 4238 - enum gpiod_flags dflags, 4239 - const char *label) 4240 - { 4241 - unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; 4242 - struct gpio_desc *desc; 4243 - enum of_gpio_flags flags; 4244 - bool active_low = false; 4245 - bool single_ended = false; 4246 - bool open_drain = false; 4247 - bool transitory = false; 4248 - int ret; 4249 - 4250 - desc = of_get_named_gpiod_flags(node, propname, 4251 - index, &flags); 4252 - 4253 - if (!desc || IS_ERR(desc)) { 4254 - return desc; 4255 - } 4256 - 4257 - active_low = flags & OF_GPIO_ACTIVE_LOW; 4258 - single_ended = flags & OF_GPIO_SINGLE_ENDED; 4259 - open_drain = flags & OF_GPIO_OPEN_DRAIN; 4260 - transitory = flags & OF_GPIO_TRANSITORY; 4261 - 4262 - ret = gpiod_request(desc, label); 4263 - if (ret == -EBUSY && (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) 4264 - return desc; 4265 - if (ret) 4266 - return ERR_PTR(ret); 4267 - 4268 - if (active_low) 4269 - lflags |= GPIO_ACTIVE_LOW; 4270 - 4271 - if (single_ended) { 4272 - if (open_drain) 4273 - lflags |= GPIO_OPEN_DRAIN; 4274 - else 4275 - lflags |= GPIO_OPEN_SOURCE; 4276 - } 4277 - 4278 - if (transitory) 4279 - lflags |= GPIO_TRANSITORY; 4280 - 4281 - ret = gpiod_configure_flags(desc, propname, lflags, dflags); 4282 - if (ret < 0) { 4283 - gpiod_put(desc); 4284 - return ERR_PTR(ret); 4285 - } 4286 - 4287 - return desc; 4288 - } 4289 - EXPORT_SYMBOL(gpiod_get_from_of_node); 4290 4249 4291 4250 /** 4292 4251 * fwnode_get_named_gpiod - obtain a GPIO from firmware node
-27
drivers/gpio/gpiolib.h
··· 16 16 #include <linux/module.h> 17 17 #include <linux/cdev.h> 18 18 19 - enum of_gpio_flags; 20 19 struct acpi_device; 21 20 22 21 /** ··· 90 91 91 92 /* gpio suffixes used for ACPI and device tree lookup */ 92 93 static __maybe_unused const char * const gpio_suffixes[] = { "gpios", "gpio" }; 93 - 94 - #ifdef CONFIG_OF_GPIO 95 - struct gpio_desc *of_find_gpio(struct device *dev, 96 - const char *con_id, 97 - unsigned int idx, 98 - unsigned long *lookupflags); 99 - struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 100 - const char *list_name, int index, enum of_gpio_flags *flags); 101 - int of_gpiochip_add(struct gpio_chip *gc); 102 - void of_gpiochip_remove(struct gpio_chip *gc); 103 - #else 104 - static inline struct gpio_desc *of_find_gpio(struct device *dev, 105 - const char *con_id, 106 - unsigned int idx, 107 - unsigned long *lookupflags) 108 - { 109 - return ERR_PTR(-ENOENT); 110 - } 111 - static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 112 - const char *list_name, int index, enum of_gpio_flags *flags) 113 - { 114 - return ERR_PTR(-ENOENT); 115 - } 116 - static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; } 117 - static inline void of_gpiochip_remove(struct gpio_chip *gc) { } 118 - #endif /* CONFIG_OF_GPIO */ 119 94 120 95 #ifdef CONFIG_ACPI 121 96 void acpi_gpiochip_add(struct gpio_chip *chip);