diff options
Diffstat (limited to 'Documentation/pinctrl.txt')
-rw-r--r-- | Documentation/pinctrl.txt | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/Documentation/pinctrl.txt b/Documentation/pinctrl.txt index a2b57e0a1db0..447fd4cd54ec 100644 --- a/Documentation/pinctrl.txt +++ b/Documentation/pinctrl.txt | |||
@@ -736,6 +736,13 @@ All the above functions are mandatory to implement for a pinmux driver. | |||
736 | Pin control interaction with the GPIO subsystem | 736 | Pin control interaction with the GPIO subsystem |
737 | =============================================== | 737 | =============================================== |
738 | 738 | ||
739 | Note that the following implies that the use case is to use a certain pin | ||
740 | from the Linux kernel using the API in <linux/gpio.h> with gpio_request() | ||
741 | and similar functions. There are cases where you may be using something | ||
742 | that your datasheet calls "GPIO mode" but actually is just an electrical | ||
743 | configuration for a certain device. See the section below named | ||
744 | "GPIO mode pitfalls" for more details on this scenario. | ||
745 | |||
739 | The public pinmux API contains two functions named pinctrl_request_gpio() | 746 | The public pinmux API contains two functions named pinctrl_request_gpio() |
740 | and pinctrl_free_gpio(). These two functions shall *ONLY* be called from | 747 | and pinctrl_free_gpio(). These two functions shall *ONLY* be called from |
741 | gpiolib-based drivers as part of their gpio_request() and | 748 | gpiolib-based drivers as part of their gpio_request() and |
@@ -774,6 +781,111 @@ obtain the function "gpioN" where "N" is the global GPIO pin number if no | |||
774 | special GPIO-handler is registered. | 781 | special GPIO-handler is registered. |
775 | 782 | ||
776 | 783 | ||
784 | GPIO mode pitfalls | ||
785 | ================== | ||
786 | |||
787 | Sometime the developer may be confused by a datasheet talking about a pin | ||
788 | being possible to set into "GPIO mode". It appears that what hardware | ||
789 | engineers mean with "GPIO mode" is not necessarily the use case that is | ||
790 | implied in the kernel interface <linux/gpio.h>: a pin that you grab from | ||
791 | kernel code and then either listen for input or drive high/low to | ||
792 | assert/deassert some external line. | ||
793 | |||
794 | Rather hardware engineers think that "GPIO mode" means that you can | ||
795 | software-control a few electrical properties of the pin that you would | ||
796 | not be able to control if the pin was in some other mode, such as muxed in | ||
797 | for a device. | ||
798 | |||
799 | Example: a pin is usually muxed in to be used as a UART TX line. But during | ||
800 | system sleep, we need to put this pin into "GPIO mode" and ground it. | ||
801 | |||
802 | If you make a 1-to-1 map to the GPIO subsystem for this pin, you may start | ||
803 | to think that you need to come up with something real complex, that the | ||
804 | pin shall be used for UART TX and GPIO at the same time, that you will grab | ||
805 | a pin control handle and set it to a certain state to enable UART TX to be | ||
806 | muxed in, then twist it over to GPIO mode and use gpio_direction_output() | ||
807 | to drive it low during sleep, then mux it over to UART TX again when you | ||
808 | wake up and maybe even gpio_request/gpio_free as part of this cycle. This | ||
809 | all gets very complicated. | ||
810 | |||
811 | The solution is to not think that what the datasheet calls "GPIO mode" | ||
812 | has to be handled by the <linux/gpio.h> interface. Instead view this as | ||
813 | a certain pin config setting. Look in e.g. <linux/pinctrl/pinconf-generic.h> | ||
814 | and you find this in the documentation: | ||
815 | |||
816 | PIN_CONFIG_OUTPUT: this will configure the pin in output, use argument | ||
817 | 1 to indicate high level, argument 0 to indicate low level. | ||
818 | |||
819 | So it is perfectly possible to push a pin into "GPIO mode" and drive the | ||
820 | line low as part of the usual pin control map. So for example your UART | ||
821 | driver may look like this: | ||
822 | |||
823 | #include <linux/pinctrl/consumer.h> | ||
824 | |||
825 | struct pinctrl *pinctrl; | ||
826 | struct pinctrl_state *pins_default; | ||
827 | struct pinctrl_state *pins_sleep; | ||
828 | |||
829 | pins_default = pinctrl_lookup_state(uap->pinctrl, PINCTRL_STATE_DEFAULT); | ||
830 | pins_sleep = pinctrl_lookup_state(uap->pinctrl, PINCTRL_STATE_SLEEP); | ||
831 | |||
832 | /* Normal mode */ | ||
833 | retval = pinctrl_select_state(pinctrl, pins_default); | ||
834 | /* Sleep mode */ | ||
835 | retval = pinctrl_select_state(pinctrl, pins_sleep); | ||
836 | |||
837 | And your machine configuration may look like this: | ||
838 | -------------------------------------------------- | ||
839 | |||
840 | static unsigned long uart_default_mode[] = { | ||
841 | PIN_CONF_PACKED(PIN_CONFIG_DRIVE_PUSH_PULL, 0), | ||
842 | }; | ||
843 | |||
844 | static unsigned long uart_sleep_mode[] = { | ||
845 | PIN_CONF_PACKED(PIN_CONFIG_OUTPUT, 0), | ||
846 | }; | ||
847 | |||
848 | static struct pinctrl_map __initdata pinmap[] = { | ||
849 | PIN_MAP_MUX_GROUP("uart", PINCTRL_STATE_DEFAULT, "pinctrl-foo", | ||
850 | "u0_group", "u0"), | ||
851 | PIN_MAP_CONFIGS_PIN("uart", PINCTRL_STATE_DEFAULT, "pinctrl-foo", | ||
852 | "UART_TX_PIN", uart_default_mode), | ||
853 | PIN_MAP_MUX_GROUP("uart", PINCTRL_STATE_SLEEP, "pinctrl-foo", | ||
854 | "u0_group", "gpio-mode"), | ||
855 | PIN_MAP_CONFIGS_PIN("uart", PINCTRL_STATE_SLEEP, "pinctrl-foo", | ||
856 | "UART_TX_PIN", uart_sleep_mode), | ||
857 | }; | ||
858 | |||
859 | foo_init(void) { | ||
860 | pinctrl_register_mappings(pinmap, ARRAY_SIZE(pinmap)); | ||
861 | } | ||
862 | |||
863 | Here the pins we want to control are in the "u0_group" and there is some | ||
864 | function called "u0" that can be enabled on this group of pins, and then | ||
865 | everything is UART business as usual. But there is also some function | ||
866 | named "gpio-mode" that can be mapped onto the same pins to move them into | ||
867 | GPIO mode. | ||
868 | |||
869 | This will give the desired effect without any bogus interaction with the | ||
870 | GPIO subsystem. It is just an electrical configuration used by that device | ||
871 | when going to sleep, it might imply that the pin is set into something the | ||
872 | datasheet calls "GPIO mode" but that is not the point: it is still used | ||
873 | by that UART device to control the pins that pertain to that very UART | ||
874 | driver, putting them into modes needed by the UART. GPIO in the Linux | ||
875 | kernel sense are just some 1-bit line, and is a different use case. | ||
876 | |||
877 | How the registers are poked to attain the push/pull and output low | ||
878 | configuration and the muxing of the "u0" or "gpio-mode" group onto these | ||
879 | pins is a question for the driver. | ||
880 | |||
881 | Some datasheets will be more helpful and refer to the "GPIO mode" as | ||
882 | "low power mode" rather than anything to do with GPIO. This often means | ||
883 | the same thing electrically speaking, but in this latter case the | ||
884 | software engineers will usually quickly identify that this is some | ||
885 | specific muxing/configuration rather than anything related to the GPIO | ||
886 | API. | ||
887 | |||
888 | |||
777 | Board/machine configuration | 889 | Board/machine configuration |
778 | ================================== | 890 | ================================== |
779 | 891 | ||