This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

setup gpio output in device tree and userspace

I'm working on a custom board that has several GPIO pins that I would like be able to read and write from user space. 

I'm having trouble configuring a pin as a generic output. 

I can do it via the gpio-led driver and the device tree like so:

am33xx_pinmux: pinmux@44e10800 {
		pinctrl-names = "default";
		pinctrl-0 = <&system_ctrl &board_id &ac_pwr_supply_status &ir_remote_in &panel_pwr_ctrl &clkout2_pin>;

status_leds: status_leds {
		
			pinctrl-single,pins = <
				/* Stand By LEDs */
				0xC8 (PIN_OUTPUT | MUX_MODE7)  /* STBY_LED_ENA_0: U3 lcd_data10.gpio2_16 */
				0xCC (PIN_OUTPUT | MUX_MODE7)  /* STBY_LED_ENA_1: U4 lcd_data11.gpio2_17 */
				/* DEV LEDs */
				0xAC (PIN_OUTPUT | MUX_MODE7)  /* LED_0: R4 lcd_data3.gpio2_9 */
				0xA8 (PIN_OUTPUT | MUX_MODE7)  /* LED_1: R3 lcd_data2.gpio2_8 */
				0xA4 (PIN_OUTPUT | MUX_MODE7)  /* LED_2: R2 lcd_data1.gpio2_7 */
				0xA0 (PIN_OUTPUT | MUX_MODE7)  /* LED_3: R2 lcd_data0.gpio2_6 */
				
                /* System communications enable/isolation, set to high to disable
                 * most of the dock, Mstar, i2c, uart, gpio interfaces. */
                0x88 (PIN_OUTPUT | MUX_MODE7)  /* UART_GATE_ENA: T13 gpmc_csn3.gpio2_0 */
				
			>;
		};

...

ocp {
        /**/
        gpio_leds: status_leds@0{
            compatible = "gpio-leds";
            pinctrl-names = "default";
            pinctrl-0 = <&status_leds>;

...
sys-com-en { label = "sys-com-en"; gpios = <&gpio2 0 GPIO_ACTIVE_LOW>; default-sate = "off"; }; }; /*end gpio_leds*/

I can then change the state of sys-com-en from user space via the command:
$echo "1" > /sys/class/leds/sys-con-en/brightness

However this pin is not an LED and it seems like I should be able set it up as a more generic GPIO that is exported under /sys/class/gpio but I can't get it to work, I must be missing something in the device tree. 

I've tried putting the pin in it's own group in the pinmux@44e10800 part of the tree and then exporting it via: 
$ echo "34" > /sys/class/gpio/export

The pin gets exported I don't appear to have any control over the physical state of the pin. It seems like I'm missing something in the ocp section of the device tree. I've seen plenty of examples of how to do this with device tree overlays using the cape manager and the bone-pinmux-helper but I don't intend to use overlays or the cape manager

Any enlightenment here would be very much appreciated. 

Thanks,
Matt S. 

  • Hi Matt,

    Still if you are looking for the solution, please refer to the following post.

    http://e2e.ti.com/support/embedded/linux/f/354/t/383391.aspx

  • Hi Titus,

    Thanks for responding, from reading the thread that you referenced it sounds like the OP's problem had to do with incorrectly issuing the command to export the gpio from the shell (i.e. echo "44" > export). I don't think that is my problem. 

    When I export a pin via the command echo "34" > /sys/class/gpio/export the appropriate gio directory appears (e.g /sys/class/gpio/gpio34 )

    However, when I attempt to change the direction of the pin (i.e. echo "out" > /sys/class/gpio/gpio34/direction) and the value echo "1" > /sys/class/gpio/gpio34/value I see no corresponding change in the electrical state of the pin. 

    I've been able to make output pins work if I designated them as gpio-leds but that's not really what I want, I would like them to be regular gpio's and I will have some input gpio's that obviously won't work as LED's.  

    Thanks,
    Matt S. 

  • Ok so I figured this out, and I feel kind of dumb.

    I had everything correct except I was using the wrong pin number.
    I was using the pin number reported in /sys/kernel/debug/pinctrl/44e10800.pinmux/pins
    Which is not what you want.
    You need to calculate the pin number from the GPIO bank number
    GPIO BANK 0 - Pins 0 -31
    GPIO Bank 1 - Pins 32-64
    etc.