About LED support in linux

LED blinking support is integrated in the Linux kernel, that allows to simplify working with the LEDs connected to any free gpio.

The kernel can assume an independent function for displaying via led, for example the hdd activity or processor activity etc. All available options for using LEDs using the kernel driver can be found in the Internet. Here is the description how it all can be configured.

How to let the led to blink using the linux kernel.

The led support is to be activated in the kernel LED: CONFIG_LEDS_CLASS, CONFIG_LEDS_GPIO, all options CONFIG_LEDS_TRIGGERS.

In the configuration file of the target platform for the kernel building (for processors from kirkwood family this file can be a file /arch/arm/mach-kirkwood/db88f6281-bp-setup.c) we should create some structs as follows:

static struct gpio_led tr628m1_led_pins[] = {
	{
		.name		= "tr628m1:green:Rs232Act",
		.gpio		= 47,
		.active_low	= 1,
		.default_trigger = "oneshot"
	}
};

static struct gpio_led_platform_data tr628m1_data = {
	.leds		= tr628m1_led_pins,
	.num_leds	= ARRAY_SIZE(tr628m1_led_pins),
};

static struct platform_device tr628m1_leds = {
	.name	= "leds-gpio",
	.id	= -1,
	.dev	= {
		.platform_data	= &tr628m1_data,
	},
};

Then call out the function:

platform_device_register(&tr628m1_leds);

After the kernel boot process and after starting of the file system we should initialize gpio which will control the led:

echo 47 > /sys/class/gpio/export

Then the interface will appears in the file system /sys for control the led.

# ls -l /sys/class/leds/
lrwxrwxrwx    1 root     root             0 Jan  1  1970 tr628m1:green:Rs232Act -> ../../devices/platform/leds-gpio/leds/tr628m1:green:Rs232Act

It should be noted that there is a set of triggers using which we can manipulate the led. Depending on which of the triggers is active now these or other properties become available.

For example, in this case the trigger named "oneshot" is active and the set of available options for it looks like this:

# ls -l /sys/class/leds/tr628m1\:green\:Rs232Act/
-rw-r--r--    1 root     root          4096 Apr 19 01:20 brightness
-rw-r--r--    1 root     root          4096 Sep 29  2015 delay_off
-rw-r--r--    1 root     root          4096 Sep 29  2015 delay_on
lrwxrwxrwx    1 root     root             0 Apr 19 01:20 device -> ../../../leds-gpio
-rw-r--r--    1 root     root          4096 Apr 19 01:20 invert
-r--r--r--    1 root     root          4096 Apr 19 01:20 max_brightness
--w-------    1 root     root          4096 Apr 19 01:20 shot
lrwxrwxrwx    1 root     root             0 Apr 19 01:20 subsystem -> ../../../../../class/leds
-rw-r--r--    1 root     root          4096 Apr 19 01:20 trigger
-rw-r--r--    1 root     root          4096 Apr 19 01:20 uevent

Therefore to make the trigger "oneshot" work during the wanted time period we should configure the following properties:

echo 50  >  /sys/class/leds/tr628m1\:green\:Rs232Act/delay_off 
echo 50  >  /sys/class/leds/tr628m1\:green\:Rs232Act/delay_on

For activation of the oneshot trigger we should run the following program from the console:

echo 1  >  /sys/class/leds/tr628m1\:green\:Rs232Act/shot

or to run the following function from the programm:

FILE *file; 
char *fname = "/sys/class/leds/tr628m1:green:Rs232Act/shot"; 

file = fopen(fname,"w"); 
    if (file) { 
        fputc ('1', file); 
        fclose (file); 
    } 

The list of available triggers can be seen as follows (the brackets indicates the active trigger):

# cat /sys/class/leds/tr628m1\:green\:Rs232Act/trigger 
none timer [oneshot] default-on netdev 

To change the trigger type we can run the command:

echo timer > /sys/class/leds/tr628m1\:green\:Rs232Act/trigger