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.

setting leds in linux kernel

Hello

I want to set leds ON during u-boot and linux initialization.

In U-boot i called     gpio_direction_output( 76, 0); in boeard.c : that is OK

But when linux launch itself, the led progammation is then not OK

I called also in am335x_evm_init() function :       

gpio_direction_output(GPIO_TO_PIN(2, 12), 0);
gpio_free(GPIO_TO_PIN(2, 12));

But in my appliation i then can't open these GPIO using gpio_set from the following file :

4338.gpio-sysfs.c


The other IO are OK. 
THe gpio_free function is not enough to be able to use the IO in an aplication using the gpio_set function ?

best regards

  • Hi Trichet,

    Have you tried to control the GPIO through commands "SYSFS"  instead of trying through "C" app ?

    http://processors.wiki.ti.com/index.php/Linux_PSP_GPIO_Driver_Guide#User_Space_-_Sysfs_control

    Try the below commands and update me the results.

    Note:

    Mention your appropriate GPIO no instead 45.

    echo 45 > /sys/class/gpio/export

    echo "out" > /sys/class/gpio/gpio45/direction

    echo 0 > /sys/class/gpio/gpio45/value

    echo 1 > /sys/class/gpio/gpio45/value

  • Hello

    thanks for your reply

    1) I added  at the end of am335x_evm_setup the following code

        int status = gpio_request(GPIO_TO_PIN(2, 12),"DEF");
        if (status < 0)
            pr_err("Failed to request GPIO DEF");
        else
        {
            gpio_direction_output(GPIO_TO_PIN(2, 12), 0);
            gpio_free(GPIO_TO_PIN(2, 12));
        }

    with the free function i get no error using sysfs command (echo 45 > /sys/class/gpio/export)

    But with this function the gpio doesn't stay to 0. How can i impose a value on an IO, and then use it with sysfs command in an application ?

    Thanks

  • Hi Trichet,

    What is the output of "GPIO_TO_PIN(2, 12)" ie GPIO pin no ?

    Please make sure that you are not used the GPIO for any other peripheral.

    Like LCD , GPMC etc.,

    Have you configured the "LCD_DATA6" pin as MODE7 in linux source.

  • Hello

    thanks for the reply

    In U-boot/MLO the GPIO is cleared using : gpio_direction_output( 76, 0); it's OK

    But when linux is launched this IO returns to 1

    This IO is number 76, it is not re-configured in the following file (the code used is under #ifdef UCR_BOARD) :

    8461.board-am335xevm.c

    Has the pinumx is not re-defined in this file i shouldn't have problem ?

    Thanks

  • Hi Trichet,

    Try the below code in your board file and update the results with logs and confirm the LED status too ie whether the LEDs are blinking or not.

    Include "#include <linux/delay.h>" in board file.

    static void gpio_led_init(int evm_id, int profile)
    {
    	int status;
    #if 0
    	int err;
    
    	setup_pin_mux(gpio_led_mux);
    	err = platform_device_register(&leds_gpio);
    	if (err)
    		pr_err("failed to register gpio led device\n");
    #endif
    
    #ifdef UCR_BOARD
    //V1.2
    	pr_info("Configure led INIT and DEF!!!!!!!!!!!!!!!!!!!!!!!!!!!...\n");
    		status = gpio_request(GPIO_TO_PIN(2, 12),NULL);
    		printk("GPIO76 REQUEST STATUS is %p\n",status);
    		gpio_direction_output(GPIO_TO_PIN(2, 12), 0);
    		mdelay(2000)	//2 sec delay so that we can see that LED blinking
    		gpio_set_value(GPIO_TO_PIN(2, 12),1);	//OFF
    		mdelay(2000)	//2 sec delay so that we can see that LED blinking
    		gpio_set_value(GPIO_TO_PIN(2, 12),0);	//ON
    
    		gpio_free(GPIO_TO_PIN(2, 13));
    	pr_info("DONE !!!\n");
    #endif
    
    }
    

  • Hello

    thanks for the reply, that OK i can see the blinking done by gpio_led_init() function.

    By under linux prompt i don't have access to that IO :

    root@am335x-evm:~# echo 76 > /sys/class/gpio/export                             
    -sh: echo: write error: Device or resource busy                                 
    s/class/gpio/gpio76/value                                                       
    root@am335x-evm:~# echo "out" > /sys/class/gpio/gpio76/direction                
    -sh: /sys/class/gpio/gpio76/direction: No such file or directory                
    root@am335x-evm:~# echo 0 > /sys/class/gpio/gpio76/value                        
    -sh: /sys/class/gpio/gpio76/value: No such file or directory  

    Is there a way to unexport in IO ?

    The test was done using the code :

    static struct pinmux_config gpio_led_mux[] = {
    //V1.2
    #ifdef UCR_BOARD
        {"lcd_data6.gpio2_12", OMAP_MUX_MODE7 | AM33XX_PIN_OUTPUT},
        {"lcd_data7.gpio2_13", OMAP_MUX_MODE7 | AM33XX_PIN_OUTPUT},
        {NULL, 0},
    #else
        {"gpmc_ad4.gpio1_4", OMAP_MUX_MODE7 | AM33XX_PIN_INPUT},
        {"gpmc_ad5.gpio1_5", OMAP_MUX_MODE7 | AM33XX_PIN_INPUT},
        {"gpmc_ad6.gpio1_6", OMAP_MUX_MODE7 | AM33XX_PIN_INPUT},
        {"gpmc_ad7.gpio1_7", OMAP_MUX_MODE7 | AM33XX_PIN_INPUT},
        {NULL, 0},
    #endif
    };

    static struct gpio_led gpio_leds[] = {
    //V1.2
    #ifdef UCR_BOARD
        {
            .name            = "INIT",
            .gpio            = GPIO_TO_PIN(2, 12),    
        },
        {
            .name            = "DEF",
            .gpio            = GPIO_TO_PIN(2, 13),    
        },
    #else
        {
            .name            = "am335x:EVM_SK:usr0",
            .gpio            = GPIO_TO_PIN(1, 4),    /* D1 */
        },
        {
            .name            = "am335x:EVM_SK:usr1",
            .gpio            = GPIO_TO_PIN(1, 5),    /* D2 */
        },
        {
            .name            = "am335x:EVM_SK:mmc0",
            .gpio            = GPIO_TO_PIN(1, 7),    /* D3 */
            .default_trigger    = "mmc0",
        },
        {
            .name            = "am335x:EVM_SK:heartbeat",
            .gpio            = GPIO_TO_PIN(1, 6),    /* D4 */
            .default_trigger    = "heartbeat",
        },
    #endif
    };

    static void gpio_led_init(int evm_id, int profile)
    {
        int err;

        setup_pin_mux(gpio_led_mux);
        err = platform_device_register(&leds_gpio);
        if (err)
            pr_err("failed to register gpio led device\n");


        pr_info("Configure led INIT and DEF!!!!!!!!!!!!!!!!!!!!!!!!!!!...\n");
            int status = gpio_request(GPIO_TO_PIN(2, 12),NULL);
            printk("GPIO76 REQUEST STATUS is %p\n",status);
            gpio_direction_output(GPIO_TO_PIN(2, 12), 0);
            mdelay(2000);    //2 sec delay so that we can see that LED blinking
            gpio_set_value(GPIO_TO_PIN(2, 12),1);    //OFF
            mdelay(2000);    //2 sec delay so that we can see that LED blinking
            gpio_set_value(GPIO_TO_PIN(2, 12),0);    //ON

            gpio_free(GPIO_TO_PIN(2, 13));
        pr_info("DONE !!!\n");




    }

  • Hi Trichet,

    Hello

    thanks for the reply, that OK i can see the blinking done by gpio_led_init() function.

    By under linux prompt i don't have access to that IO :

    root@am335x-evm:~# echo 76 > /sys/class/gpio/export                             
    -sh: echo: write error: Device or resource busy                                 
    s/class/gpio/gpio76/value                                              

    I've done a mistake and sorry for that.

    Just I've used your code and modified slightly.

    I freed the GPIO2_13 but not GPIO2_12 right ?

    Now I've freed the GPIO2_12 and have a try.

    static void gpio_led_init(int evm_id, int profile)
    {
        int status;
    #if 0
        int err;
        setup_pin_mux(gpio_led_mux);
        err = platform_device_register(&leds_gpio);
        if (err)
            pr_err("failed to register gpio led device\n");
    #endif
    #ifdef UCR_BOARD
    //V1.2
        pr_info("Configure led INIT and DEF!!!!!!!!!!!!!!!!!!!!!!!!!!!...\n");
            status = gpio_request(GPIO_TO_PIN(2, 12),NULL);
            printk("GPIO76 REQUEST STATUS is %p\n",status);
            gpio_direction_output(GPIO_TO_PIN(2, 12), 0);
            mdelay(2000)    //2 sec delay so that we can see the LED blinking
            gpio_set_value(GPIO_TO_PIN(2, 12),1);   //OFF
            mdelay(2000)    //2 sec delay so that we can see the LED blinking
            gpio_set_value(GPIO_TO_PIN(2, 12),0);   //ON
            gpio_free(GPIO_TO_PIN(2, 12));
        pr_info("DONE !!!\n");
    #endif
    }

  • Hello

    thanks for the reply

    i have tried the free(76), with no more success :

               
    [    0.965393] Configure led INIT and DEF!!!!!!!!!!!!!!!!!!!!!!!!!!!...         
    [    0.972045] GPIO76 REQUEST STATUS is   (null)                                
    [    4.974182] DONE !!!                                                         
    [

    root@am335x-evm:~# echo 76 > /sys/class/gpio/export                             
    -sh: echo: write error: Device or resource busy                                 
    lass/gpio/gpio76/value                                                          
    root@am335x-evm:~# echo "out" > /sys/class/gpio/gpio76/direction                
    -sh: /sys/class/gpio/gpio76/direction: No such file or directory                
    root@am335x-evm:~# echo 0 > /sys/class/gpio/gpio76/value                        
    -sh: /sys/class/gpio/gpio76/value: No such file or directory

    Shouldn't we use the function

    
    
         int gpiod_export(struct gpio_desc *desc, bool direction_may_change);

    /* export the GPIO to userspace */

    Have you any idea ?

    best regards

  • Hi Trichet,

    You can also export the GPIO to userspace through linux kernel code.

    gpio_export(76,true);

    or

    gpio_export(GPIO_TO_PIN(2, 12),true);

  • Thank for the reply

    now that is OK

    The only problem is a de-programmation of about 1s when linux boots. I put my code

            int status = gpio_request(GPIO_TO_PIN(2, 12),NULL);
            //printk("GPIO76 REQUEST STATUS is %p\n",status);
            gpio_direction_output(GPIO_TO_PIN(2, 12), 0);
            gpio_set_value(GPIO_TO_PIN(2, 12),0);    //ON
            gpio_export(GPIO_TO_PIN(2, 12),true);

    in am335x_evm_init , am335x_evm_setup, gpio_led_init functions to force to 0 the GPIO

    I don't undertsand why this IO is momentanuously de-programmed

    Any idea

    Regards

  • Hi Trichet,

    Currently what problem are you facing ?

    Not able to control the GPIO from user space using "echo 0 > /sys/class/gpio/gpio76/value" ?

  • Hello

    thanks for the reply.

    No the problem is that i set gpio76 to 0 in u-boot, but it returns to 1 when linux is lauched, for 60ms, before i put it again to 0 in gpio_led_init().

    I' m wondering where in linux the GPIO maybe modified

    Regards

  • Hi Trichet,

    Remove this "lcdc_pin_mux" declaration in your board file and try.

    Please ensure that you have not used "LCD_D6" data line which is pinmuxed with 'GPIO2_12' in board file or dts file.

  • Hello

    thanks for the reply. I have patched the lcdc_pin_mux with no success (lcdc_init() function was not called anyway)

    7585.board-am335xevm.c

    What do you mean by dts file ?

    Thanks

  • Hi Trichet,

    'dts' means "device tree source"

    Nowadays we are using "dts" support in linux for configuring what board file does.

    arch/arm/boot/dts/

  • Hello

    Thanks for the reply

    i have no device tree for that linux distribution.

    The GPIO 76 is set to 1 when linux starts; it is done before we enter the  __init am335x_evm_init() function.

    What function may change u-boot IO configuration ?

    Thanks