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

pinctrl: GPIO direction support for muxing

When requesting a single GPIO pin to be muxed in, some controllers
will need to poke a different value into the control register
depending on whether the pin will be used for GPIO output or GPIO
input. So create pinmux counterparts to gpio_direction_[input|output]
in the pinctrl framework.

ChangeLog v1->v2:
- This also amends the documentation to make it clear the this
function and associated machinery is *ONLY* intended as a backend
to gpiolib machinery, not for everyone and his dog to start playing
around with pins.
ChangeLog v2->v3:
- Don't pass an argument to the common request function, instead
provide pinmux_* counterparts to the gpio_direction_[input|output]
calls, simpler and anyone can understand it.
ChangeLog v3->v4:
- Fix numerous spelling mistakes and dangling text in documentation.
Add Ack and Rewewed-by.

Cc: Igor Grinberg <grinberg@compulab.co.il>
Acked-by: Stephen Warren <swarren@nvidia.com>
Reviewed-by: Thomas Abraham <thomas.abraham@linaro.org>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

+108 -9
+24 -8
Documentation/pinctrl.txt
··· 645 645 Pinmux interaction with the GPIO subsystem 646 646 ========================================== 647 647 648 + The public pinmux API contains two functions named pinmux_request_gpio() 649 + and pinmux_free_gpio(). These two functions shall *ONLY* be called from 650 + gpiolib-based drivers as part of their gpio_request() and 651 + gpio_free() semantics. Likewise the pinmux_gpio_direction_[input|output] 652 + shall only be called from within respective gpio_direction_[input|output] 653 + gpiolib implementation. 654 + 655 + NOTE that platforms and individual drivers shall *NOT* request GPIO pins to be 656 + muxed in. Instead, implement a proper gpiolib driver and have that driver 657 + request proper muxing for its pins. 658 + 648 659 The function list could become long, especially if you can convert every 649 660 individual pin into a GPIO pin independent of any other pins, and then try 650 661 the approach to define every pin as a function. ··· 663 652 In this case, the function array would become 64 entries for each GPIO 664 653 setting and then the device functions. 665 654 666 - For this reason there is an additional function a pinmux driver can implement 667 - to enable only GPIO on an individual pin: .gpio_request_enable(). The same 668 - .free() function as for other functions is assumed to be usable also for 669 - GPIO pins. 655 + For this reason there are two functions a pinmux driver can implement 656 + to enable only GPIO on an individual pin: .gpio_request_enable() and 657 + .gpio_disable_free(). 670 658 671 659 This function will pass in the affected GPIO range identified by the pin 672 660 controller core, so you know which GPIO pins are being affected by the request 673 661 operation. 674 662 675 - Alternatively it is fully allowed to use named functions for each GPIO 676 - pin, the pinmux_request_gpio() will attempt to obtain the function "gpioN" 677 - where "N" is the global GPIO pin number if no special GPIO-handler is 678 - registered. 663 + If your driver needs to have an indication from the framework of whether the 664 + GPIO pin shall be used for input or output you can implement the 665 + .gpio_set_direction() function. As described this shall be called from the 666 + gpiolib driver and the affected GPIO range, pin offset and desired direction 667 + will be passed along to this function. 668 + 669 + Alternatively to using these special functions, it is fully allowed to use 670 + named functions for each GPIO pin, the pinmux_request_gpio() will attempt to 671 + obtain the function "gpioN" where "N" is the global GPIO pin number if no 672 + special GPIO-handler is registered. 679 673 680 674 681 675 Pinmux board/machine configuration
+61
drivers/pinctrl/pinmux.c
··· 214 214 /** 215 215 * pinmux_request_gpio() - request a single pin to be muxed in as GPIO 216 216 * @gpio: the GPIO pin number from the GPIO subsystem number space 217 + * 218 + * This function should *ONLY* be used from gpiolib-based GPIO drivers, 219 + * as part of their gpio_request() semantics, platforms and individual drivers 220 + * shall *NOT* request GPIO pins to be muxed in. 217 221 */ 218 222 int pinmux_request_gpio(unsigned gpio) 219 223 { ··· 253 249 /** 254 250 * pinmux_free_gpio() - free a single pin, currently used as GPIO 255 251 * @gpio: the GPIO pin number from the GPIO subsystem number space 252 + * 253 + * This function should *ONLY* be used from gpiolib-based GPIO drivers, 254 + * as part of their gpio_free() semantics, platforms and individual drivers 255 + * shall *NOT* request GPIO pins to be muxed out. 256 256 */ 257 257 void pinmux_free_gpio(unsigned gpio) 258 258 { ··· 277 269 kfree(func); 278 270 } 279 271 EXPORT_SYMBOL_GPL(pinmux_free_gpio); 272 + 273 + static int pinmux_gpio_direction(unsigned gpio, bool input) 274 + { 275 + struct pinctrl_dev *pctldev; 276 + struct pinctrl_gpio_range *range; 277 + const struct pinmux_ops *ops; 278 + int ret; 279 + int pin; 280 + 281 + ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range); 282 + if (ret) 283 + return ret; 284 + 285 + ops = pctldev->desc->pmxops; 286 + 287 + /* Convert to the pin controllers number space */ 288 + pin = gpio - range->base + range->pin_base; 289 + 290 + if (ops->gpio_set_direction) 291 + ret = ops->gpio_set_direction(pctldev, range, pin, input); 292 + else 293 + ret = 0; 294 + 295 + return ret; 296 + } 297 + 298 + /** 299 + * pinmux_gpio_direction_input() - request a GPIO pin to go into input mode 300 + * @gpio: the GPIO pin number from the GPIO subsystem number space 301 + * 302 + * This function should *ONLY* be used from gpiolib-based GPIO drivers, 303 + * as part of their gpio_direction_input() semantics, platforms and individual 304 + * drivers shall *NOT* touch pinmux GPIO calls. 305 + */ 306 + int pinmux_gpio_direction_input(unsigned gpio) 307 + { 308 + return pinmux_gpio_direction(gpio, true); 309 + } 310 + EXPORT_SYMBOL_GPL(pinmux_gpio_direction_input); 311 + 312 + /** 313 + * pinmux_gpio_direction_output() - request a GPIO pin to go into output mode 314 + * @gpio: the GPIO pin number from the GPIO subsystem number space 315 + * 316 + * This function should *ONLY* be used from gpiolib-based GPIO drivers, 317 + * as part of their gpio_direction_output() semantics, platforms and individual 318 + * drivers shall *NOT* touch pinmux GPIO calls. 319 + */ 320 + int pinmux_gpio_direction_output(unsigned gpio) 321 + { 322 + return pinmux_gpio_direction(gpio, false); 323 + } 324 + EXPORT_SYMBOL_GPL(pinmux_gpio_direction_output); 280 325 281 326 /** 282 327 * pinmux_register_mappings() - register a set of pinmux mappings
+23 -1
include/linux/pinctrl/pinmux.h
··· 54 54 * Implement this only if you can mux every pin individually as GPIO. The 55 55 * affected GPIO range is passed along with an offset(pin number) into that 56 56 * specific GPIO range - function selectors and pin groups are orthogonal 57 - * to this, the core will however make sure the pins do not collide 57 + * to this, the core will however make sure the pins do not collide. 58 + * @gpio_disable_free: free up GPIO muxing on a certain pin, the reverse of 59 + * @gpio_request_enable 60 + * @gpio_set_direction: Since controllers may need different configurations 61 + * depending on whether the GPIO is configured as input or output, 62 + * a direction selector function may be implemented as a backing 63 + * to the GPIO controllers that need pin muxing. 58 64 */ 59 65 struct pinmux_ops { 60 66 int (*request) (struct pinctrl_dev *pctldev, unsigned offset); ··· 82 76 void (*gpio_disable_free) (struct pinctrl_dev *pctldev, 83 77 struct pinctrl_gpio_range *range, 84 78 unsigned offset); 79 + int (*gpio_set_direction) (struct pinctrl_dev *pctldev, 80 + struct pinctrl_gpio_range *range, 81 + unsigned offset, 82 + bool input); 85 83 }; 86 84 87 85 /* External interface to pinmux */ 88 86 extern int pinmux_request_gpio(unsigned gpio); 89 87 extern void pinmux_free_gpio(unsigned gpio); 88 + extern int pinmux_gpio_direction_input(unsigned gpio); 89 + extern int pinmux_gpio_direction_output(unsigned gpio); 90 90 extern struct pinmux * __must_check pinmux_get(struct device *dev, const char *name); 91 91 extern void pinmux_put(struct pinmux *pmx); 92 92 extern int pinmux_enable(struct pinmux *pmx); ··· 107 95 108 96 static inline void pinmux_free_gpio(unsigned gpio) 109 97 { 98 + } 99 + 100 + static inline int pinmux_gpio_direction_input(unsigned gpio) 101 + { 102 + return 0; 103 + } 104 + 105 + static inline int pinmux_gpio_direction_output(unsigned gpio) 106 + { 107 + return 0; 110 108 } 111 109 112 110 static inline struct pinmux * __must_check pinmux_get(struct device *dev, const char *name)