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

gpiolib: provide provision to register pin ranges

pinctrl subsystem needs gpio chip base to prepare set of gpio
pin ranges, which a given pinctrl driver can handle. This is
important to handle pinctrl gpio request calls in order to
program a given pin properly for gpio operation.

As gpio base is allocated dynamically during gpiochip
registration, presently there exists no clean way to pass this
information to the pinctrl subsystem.

After few discussions from [1], it was concluded that may be
gpio controller reporting the pin range it supports, is a
better way than pinctrl subsystem directly registering it.

[1] http://comments.gmane.org/gmane.linux.ports.arm.kernel/184816

Cc: Grant Likely <grant.likely@secretlab.ca>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Shiraz Hashim <shiraz.hashim@st.com>
[Edited documentation a bit]
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Shiraz Hashim and committed by
Linus Walleij
f23f1516 7e10ee68

+251
+36
Documentation/devicetree/bindings/gpio/gpio.txt
··· 75 75 gpio-controller; 76 76 }; 77 77 78 + 2.1) gpio-controller and pinctrl subsystem 79 + ------------------------------------------ 78 80 81 + gpio-controller on a SOC might be tightly coupled with the pinctrl 82 + subsystem, in the sense that the pins can be used by other functions 83 + together with optional gpio feature. 84 + 85 + While the pin allocation is totally managed by the pin ctrl subsystem, 86 + gpio (under gpiolib) is still maintained by gpio drivers. It may happen 87 + that different pin ranges in a SoC is managed by different gpio drivers. 88 + 89 + This makes it logical to let gpio drivers announce their pin ranges to 90 + the pin ctrl subsystem and call 'pinctrl_request_gpio' in order to 91 + request the corresponding pin before any gpio usage. 92 + 93 + For this, the gpio controller can use a pinctrl phandle and pins to 94 + announce the pinrange to the pin ctrl subsystem. For example, 95 + 96 + qe_pio_e: gpio-controller@1460 { 97 + #gpio-cells = <2>; 98 + compatible = "fsl,qe-pario-bank-e", "fsl,qe-pario-bank"; 99 + reg = <0x1460 0x18>; 100 + gpio-controller; 101 + gpio-ranges = <&pinctrl1 20 10>, <&pinctrl2 50 20>; 102 + 103 + } 104 + 105 + where, 106 + &pinctrl1 and &pinctrl2 is the phandle to the pinctrl DT node. 107 + 108 + Next values specify the base pin and number of pins for the range 109 + handled by 'qe_pio_e' gpio. In the given example from base pin 20 to 110 + pin 29 under pinctrl1 and pin 50 to pin 69 under pinctrl2 is handled 111 + by this gpio controller. 112 + 113 + The pinctrl node must have "#gpio-range-cells" property to show number of 114 + arguments to pass with phandle from gpio controllers node.
+42
Documentation/gpio.txt
··· 439 439 signaling rate accordingly. 440 440 441 441 442 + GPIO controllers and the pinctrl subsystem 443 + ------------------------------------------ 444 + 445 + A GPIO controller on a SOC might be tightly coupled with the pinctrl 446 + subsystem, in the sense that the pins can be used by other functions 447 + together with an optional gpio feature. We have already covered the 448 + case where e.g. a GPIO controller need to reserve a pin or set the 449 + direction of a pin by calling any of: 450 + 451 + pinctrl_request_gpio() 452 + pinctrl_free_gpio() 453 + pinctrl_gpio_direction_input() 454 + pinctrl_gpio_direction_output() 455 + 456 + But how does the pin control subsystem cross-correlate the GPIO 457 + numbers (which are a global business) to a certain pin on a certain 458 + pin controller? 459 + 460 + This is done by registering "ranges" of pins, which are essentially 461 + cross-reference tables. These are described in 462 + Documentation/pinctrl.txt 463 + 464 + While the pin allocation is totally managed by the pinctrl subsystem, 465 + gpio (under gpiolib) is still maintained by gpio drivers. It may happen 466 + that different pin ranges in a SoC is managed by different gpio drivers. 467 + 468 + This makes it logical to let gpio drivers announce their pin ranges to 469 + the pin ctrl subsystem before it will call 'pinctrl_request_gpio' in order 470 + to request the corresponding pin to be prepared by the pinctrl subsystem 471 + before any gpio usage. 472 + 473 + For this, the gpio controller can register its pin range with pinctrl 474 + subsystem. There are two ways of doing it currently: with or without DT. 475 + 476 + For with DT support refer to Documentation/devicetree/bindings/gpio/gpio.txt. 477 + 478 + For non-DT support, user can call gpiochip_add_pin_range() with appropriate 479 + parameters to register a range of gpio pins with a pinctrl driver. For this 480 + exact name string of pinctrl device has to be passed as one of the 481 + argument to this routine. 482 + 483 + 442 484 What do these conventions omit? 443 485 =============================== 444 486 One of the biggest things these conventions omit is pin multiplexing, since
+3
Documentation/pinctrl.txt
··· 364 364 the range ID value, so that the pin controller knows which range it should 365 365 deal with. 366 366 367 + Calling pinctrl_add_gpio_range from pinctrl driver is DEPRECATED. Please see 368 + section 2.1 of Documentation/devicetree/bindings/gpio/gpio.txt on how to bind 369 + pinctrl and gpio drivers. 367 370 368 371 PINMUX interfaces 369 372 =================
+56
drivers/gpio/gpiolib-of.c
··· 19 19 #include <linux/of.h> 20 20 #include <linux/of_address.h> 21 21 #include <linux/of_gpio.h> 22 + #include <linux/pinctrl/pinctrl.h> 22 23 #include <linux/slab.h> 23 24 24 25 /* Private data structure for of_gpiochip_find_and_xlate */ ··· 217 216 } 218 217 EXPORT_SYMBOL(of_mm_gpiochip_add); 219 218 219 + #ifdef CONFIG_PINCTRL 220 + void of_gpiochip_add_pin_range(struct gpio_chip *chip) 221 + { 222 + struct device_node *np = chip->of_node; 223 + struct gpio_pin_range *pin_range; 224 + struct of_phandle_args pinspec; 225 + int index = 0, ret; 226 + 227 + if (!np) 228 + return; 229 + 230 + do { 231 + ret = of_parse_phandle_with_args(np, "gpio-ranges", 232 + "#gpio-range-cells", index, &pinspec); 233 + if (ret) 234 + break; 235 + 236 + pin_range = devm_kzalloc(chip->dev, sizeof(*pin_range), 237 + GFP_KERNEL); 238 + if (!pin_range) { 239 + pr_err("%s: GPIO chip: failed to allocate pin ranges\n", 240 + chip->label); 241 + break; 242 + } 243 + 244 + pin_range->range.name = chip->label; 245 + pin_range->range.base = chip->base; 246 + pin_range->range.pin_base = pinspec.args[0]; 247 + pin_range->range.npins = pinspec.args[1]; 248 + pin_range->pctldev = of_pinctrl_add_gpio_range(pinspec.np, 249 + &pin_range->range); 250 + 251 + list_add_tail(&pin_range->node, &chip->pin_ranges); 252 + 253 + } while (index++); 254 + } 255 + 256 + void of_gpiochip_remove_pin_range(struct gpio_chip *chip) 257 + { 258 + struct gpio_pin_range *pin_range, *tmp; 259 + 260 + list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { 261 + list_del(&pin_range->node); 262 + pinctrl_remove_gpio_range(pin_range->pctldev, 263 + &pin_range->range); 264 + } 265 + } 266 + #else 267 + void of_gpiochip_add_pin_range(struct gpio_chip *chip) {} 268 + void of_gpiochip_remove_pin_range(struct gpio_chip *chip) {} 269 + #endif 270 + 220 271 void of_gpiochip_add(struct gpio_chip *chip) 221 272 { 222 273 if ((!chip->of_node) && (chip->dev)) ··· 282 229 chip->of_xlate = of_gpio_simple_xlate; 283 230 } 284 231 232 + of_gpiochip_add_pin_range(chip); 285 233 of_node_get(chip->of_node); 286 234 } 287 235 288 236 void of_gpiochip_remove(struct gpio_chip *chip) 289 237 { 238 + of_gpiochip_remove_pin_range(chip); 239 + 290 240 if (chip->of_node) 291 241 of_node_put(chip->of_node); 292 242 }
+43
drivers/gpio/gpiolib.c
··· 1083 1083 } 1084 1084 } 1085 1085 1086 + #ifdef CONFIG_PINCTRL 1087 + INIT_LIST_HEAD(&chip->pin_ranges); 1088 + #endif 1089 + 1086 1090 of_gpiochip_add(chip); 1087 1091 1088 1092 unlock: ··· 1183 1179 return chip; 1184 1180 } 1185 1181 EXPORT_SYMBOL_GPL(gpiochip_find); 1182 + 1183 + #ifdef CONFIG_PINCTRL 1184 + void gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, 1185 + unsigned int pin_base, unsigned int npins) 1186 + { 1187 + struct gpio_pin_range *pin_range; 1188 + 1189 + pin_range = devm_kzalloc(chip->dev, sizeof(*pin_range), GFP_KERNEL); 1190 + if (!pin_range) { 1191 + pr_err("%s: GPIO chip: failed to allocate pin ranges\n", 1192 + chip->label); 1193 + return; 1194 + } 1195 + 1196 + pin_range->range.name = chip->label; 1197 + pin_range->range.base = chip->base; 1198 + pin_range->range.pin_base = pin_base; 1199 + pin_range->range.npins = npins; 1200 + pin_range->pctldev = find_pinctrl_and_add_gpio_range(pinctl_name, 1201 + &pin_range->range); 1202 + 1203 + list_add_tail(&pin_range->node, &chip->pin_ranges); 1204 + } 1205 + 1206 + void gpiochip_remove_pin_ranges(struct gpio_chip *chip) 1207 + { 1208 + struct gpio_pin_range *pin_range, *tmp; 1209 + 1210 + list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { 1211 + list_del(&pin_range->node); 1212 + pinctrl_remove_gpio_range(pin_range->pctldev, 1213 + &pin_range->range); 1214 + } 1215 + } 1216 + #else 1217 + void gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, 1218 + unsigned int pin_base, unsigned int npins) {} 1219 + void gpiochip_remove_pin_ranges(struct gpio_chip *chip) {} 1220 + #endif 1186 1221 1187 1222 /* These "optional" allocation calls help prevent drivers from stomping 1188 1223 * on each other, and help provide better diagnostics in debugfs.
+13
drivers/pinctrl/core.c
··· 345 345 } 346 346 EXPORT_SYMBOL_GPL(pinctrl_add_gpio_ranges); 347 347 348 + struct pinctrl_dev *find_pinctrl_and_add_gpio_range(const char *devname, 349 + struct pinctrl_gpio_range *range) 350 + { 351 + struct pinctrl_dev *pctldev = get_pinctrl_dev_from_devname(devname); 352 + 353 + if (!pctldev) 354 + return NULL; 355 + 356 + pinctrl_add_gpio_range(pctldev, range); 357 + return pctldev; 358 + } 359 + EXPORT_SYMBOL_GPL(find_pinctrl_and_add_gpio_range); 360 + 348 361 /** 349 362 * pinctrl_remove_gpio_range() - remove a range of GPIOs fro a pin controller 350 363 * @pctldev: pin controller device to remove the range from
+13
drivers/pinctrl/devicetree.c
··· 106 106 return NULL; 107 107 } 108 108 109 + struct pinctrl_dev *of_pinctrl_add_gpio_range(struct device_node *np, 110 + struct pinctrl_gpio_range *range) 111 + { 112 + struct pinctrl_dev *pctldev; 113 + 114 + pctldev = find_pinctrl_by_of_node(np); 115 + if (!pctldev) 116 + return NULL; 117 + 118 + pinctrl_add_gpio_range(pctldev, range); 119 + return pctldev; 120 + } 121 + 109 122 static int dt_to_map_one_config(struct pinctrl *p, const char *statename, 110 123 struct device_node *np_config) 111 124 {
+25
include/asm-generic/gpio.h
··· 5 5 #include <linux/types.h> 6 6 #include <linux/errno.h> 7 7 #include <linux/of.h> 8 + #include <linux/pinctrl/pinctrl.h> 8 9 9 10 #ifdef CONFIG_GPIOLIB 10 11 ··· 47 46 struct seq_file; 48 47 struct module; 49 48 struct device_node; 49 + 50 + #ifdef CONFIG_PINCTRL 51 + /** 52 + * struct gpio_pin_range - pin range controlled by a gpio chip 53 + * @head: list for maintaining set of pin ranges, used internally 54 + * @pctldev: pinctrl device which handles corresponding pins 55 + * @range: actual range of pins controlled by a gpio controller 56 + */ 57 + 58 + struct gpio_pin_range { 59 + struct list_head node; 60 + struct pinctrl_dev *pctldev; 61 + struct pinctrl_gpio_range range; 62 + }; 63 + #endif 50 64 51 65 /** 52 66 * struct gpio_chip - abstract a GPIO controller ··· 149 133 int of_gpio_n_cells; 150 134 int (*of_xlate)(struct gpio_chip *gc, 151 135 const struct of_phandle_args *gpiospec, u32 *flags); 136 + #endif 137 + #ifdef CONFIG_PINCTRL 138 + /* 139 + * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally 140 + * describe the actual pin range which they serve in an SoC. This 141 + * information would be used by pinctrl subsystem to configure 142 + * corresponding pins for gpio usage. 143 + */ 144 + struct list_head pin_ranges; 152 145 #endif 153 146 }; 154 147
+3
include/linux/gpio.h
··· 231 231 return -EINVAL; 232 232 } 233 233 234 + void gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, 235 + unsigned int pin_base, unsigned int npins); 236 + void gpiochip_remove_pin_ranges(struct gpio_chip *chip); 234 237 #endif 235 238 236 239 #endif /* __LINUX_GPIO_H */
+17
include/linux/pinctrl/pinctrl.h
··· 136 136 unsigned nranges); 137 137 extern void pinctrl_remove_gpio_range(struct pinctrl_dev *pctldev, 138 138 struct pinctrl_gpio_range *range); 139 + 140 + extern struct pinctrl_dev *find_pinctrl_and_add_gpio_range(const char *devname, 141 + struct pinctrl_gpio_range *range); 142 + 143 + #ifdef CONFIG_OF 144 + extern struct pinctrl_dev *of_pinctrl_add_gpio_range(struct device_node *np, 145 + struct pinctrl_gpio_range *range); 146 + #else 147 + static inline 148 + struct pinctrl_dev *of_pinctrl_add_gpio_range(struct device_node *np, 149 + struct pinctrl_gpio_range *range) 150 + { 151 + return NULL; 152 + } 153 + 154 + #endif /* CONFIG_OF */ 155 + 139 156 extern const char *pinctrl_dev_get_name(struct pinctrl_dev *pctldev); 140 157 extern void *pinctrl_dev_get_drvdata(struct pinctrl_dev *pctldev); 141 158 #else