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

of/gpio: Implement of_get_gpio_flags()

This adds a new function, of_get_gpio_flags, which is like
of_get_gpio(), but accepts a new "flags" argument. This new function
will be used by the drivers that need to retrieve additional GPIO
information, such as active-low flag.

Also, this changes the default ("simple") .xlate routine to warn about
bogus (< 2) #gpio-cells usage: the second cell should always be present
for GPIO flags.

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
Signed-off-by: Paul Mackerras <paulus@samba.org>

authored by

Anton Vorontsov and committed by
Paul Mackerras
b908b53d 2fd091f3

+63 -11
+29 -7
drivers/of/gpio.c
··· 19 19 #include <asm/prom.h> 20 20 21 21 /** 22 - * of_get_gpio - Get a GPIO number from the device tree to use with GPIO API 22 + * of_get_gpio_flags - Get a GPIO number and flags to use with GPIO API 23 23 * @np: device node to get GPIO from 24 24 * @index: index of the GPIO 25 + * @flags: a flags pointer to fill in 25 26 * 26 27 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 27 - * value on the error condition. 28 + * value on the error condition. If @flags is not NULL the function also fills 29 + * in flags for the GPIO. 28 30 */ 29 - int of_get_gpio(struct device_node *np, int index) 31 + int of_get_gpio_flags(struct device_node *np, int index, 32 + enum of_gpio_flags *flags) 30 33 { 31 34 int ret; 32 35 struct device_node *gc; ··· 62 59 goto err1; 63 60 } 64 61 65 - ret = of_gc->xlate(of_gc, np, gpio_spec); 62 + /* .xlate might decide to not fill in the flags, so clear it. */ 63 + if (flags) 64 + *flags = 0; 65 + 66 + ret = of_gc->xlate(of_gc, np, gpio_spec, flags); 66 67 if (ret < 0) 67 68 goto err1; 68 69 ··· 77 70 pr_debug("%s exited with status %d\n", __func__, ret); 78 71 return ret; 79 72 } 80 - EXPORT_SYMBOL(of_get_gpio); 73 + EXPORT_SYMBOL(of_get_gpio_flags); 81 74 82 75 /** 83 - * of_gpio_simple_xlate - translate gpio_spec to the GPIO number 76 + * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags 84 77 * @of_gc: pointer to the of_gpio_chip structure 85 78 * @np: device node of the GPIO chip 86 79 * @gpio_spec: gpio specifier as found in the device tree 80 + * @flags: a flags pointer to fill in 87 81 * 88 82 * This is simple translation function, suitable for the most 1:1 mapped 89 83 * gpio chips. This function performs only one sanity check: whether gpio 90 84 * is less than ngpios (that is specified in the gpio_chip). 91 85 */ 92 86 int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np, 93 - const void *gpio_spec) 87 + const void *gpio_spec, enum of_gpio_flags *flags) 94 88 { 95 89 const u32 *gpio = gpio_spec; 96 90 91 + /* 92 + * We're discouraging gpio_cells < 2, since that way you'll have to 93 + * write your own xlate function (that will have to retrive the GPIO 94 + * number and the flags from a single gpio cell -- this is possible, 95 + * but not recommended). 96 + */ 97 + if (of_gc->gpio_cells < 2) { 98 + WARN_ON(1); 99 + return -EINVAL; 100 + } 101 + 97 102 if (*gpio > of_gc->gc.ngpio) 98 103 return -EINVAL; 104 + 105 + if (flags) 106 + *flags = gpio[1]; 99 107 100 108 return *gpio; 101 109 }
+34 -4
include/linux/of_gpio.h
··· 14 14 #ifndef __LINUX_OF_GPIO_H 15 15 #define __LINUX_OF_GPIO_H 16 16 17 + #include <linux/compiler.h> 18 + #include <linux/kernel.h> 17 19 #include <linux/errno.h> 18 20 #include <linux/gpio.h> 21 + 22 + struct device_node; 23 + 24 + /* 25 + * This is Linux-specific flags. By default controllers' and Linux' mapping 26 + * match, but GPIO controllers are free to translate their own flags to 27 + * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended. 28 + */ 29 + enum of_gpio_flags { 30 + OF_GPIO_ACTIVE_LOW = 0x1, 31 + }; 19 32 20 33 #ifdef CONFIG_OF_GPIO 21 34 ··· 39 26 struct gpio_chip gc; 40 27 int gpio_cells; 41 28 int (*xlate)(struct of_gpio_chip *of_gc, struct device_node *np, 42 - const void *gpio_spec); 29 + const void *gpio_spec, enum of_gpio_flags *flags); 43 30 }; 44 31 45 32 static inline struct of_gpio_chip *to_of_gpio_chip(struct gpio_chip *gc) ··· 63 50 return container_of(of_gc, struct of_mm_gpio_chip, of_gc); 64 51 } 65 52 66 - extern int of_get_gpio(struct device_node *np, int index); 53 + extern int of_get_gpio_flags(struct device_node *np, int index, 54 + enum of_gpio_flags *flags); 55 + 67 56 extern int of_mm_gpiochip_add(struct device_node *np, 68 57 struct of_mm_gpio_chip *mm_gc); 69 58 extern int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, 70 59 struct device_node *np, 71 - const void *gpio_spec); 60 + const void *gpio_spec, 61 + enum of_gpio_flags *flags); 72 62 #else 73 63 74 64 /* Drivers may not strictly depend on the GPIO support, so let them link. */ 75 - static inline int of_get_gpio(struct device_node *np, int index) 65 + static inline int of_get_gpio_flags(struct device_node *np, int index, 66 + enum of_gpio_flags *flags) 76 67 { 77 68 return -ENOSYS; 78 69 } 79 70 80 71 #endif /* CONFIG_OF_GPIO */ 72 + 73 + /** 74 + * of_get_gpio - Get a GPIO number to use with GPIO API 75 + * @np: device node to get GPIO from 76 + * @index: index of the GPIO 77 + * 78 + * Returns GPIO number to use with Linux generic GPIO API, or one of the errno 79 + * value on the error condition. 80 + */ 81 + static inline int of_get_gpio(struct device_node *np, int index) 82 + { 83 + return of_get_gpio_flags(np, index, NULL); 84 + } 81 85 82 86 #endif /* __LINUX_OF_GPIO_H */