[] Eighteen Embedded Linux driver development, Linux kernel comes with the LED driver exploration

  Your life only lasts as electro-optic, fooling people injured.
  Whenever SUCCESS Wei is, everyone in a single day when a pair.
  Hardships and effort into the fold and move forward to the front.
  Use good every minute slot, before grain rice product into bins.


A, Linux kernel carrying LED drive enable

  Linux kernel source into the root directory, the input pattern make menuconfig open configuration interface, the LED driving open configuration item following path: Device Drivers -> LED Support (NEW_LEDS [=y]) -> LED Support for GPIO connected LEDsAfter the configuration, as shown below:

Here Insert Picture Description

  Linux can choose how to compile the LED lamp driver according to the value of CONFIG_LEDS_GPIO, if 'y' which will be compiled into a Linux kernel. Linux kernel configured after exiting the configuration interface, open the .config file, it will find "CONFIG_LEDS_GPIO = y" this line, as shown:

Here Insert Picture Description

  Recompile the Linux kernel, and then use a new translation of the image zImage start the development board. [Alternative method, not here in the repeat]

Two, Linux kernel comes with LED Driver Analysis

  LED lamp driver file for the /drivers/leds/leds-gpio.cfiles, open the file you see below

......
static const struct of_device_id of_gpio_leds_match[] = {
	{ .compatible = "gpio-leds", },
	{},
};
......
static struct platform_driver gpio_led_driver = {
	.probe		= gpio_led_probe,
	.shutdown	= gpio_led_shutdown,
	.driver		= {
		.name	= "leds-gpio",
		.of_match_table = of_gpio_leds_match,
	},
};

  According to the above code, the LED driver can see the match table, compatible contents "gpio-leds", thus the attribute value compatible LED lamp device in the device tree node is also "gpio-leds".

  Further, driver gpio_led_probe main function is a function of acquiring device information of the LED lamp, and then to initialize the corresponding IO based on the information, to output. Therefore, the device tree without re-multiplexing and electrical properties is provided, i.e. no longer set pinctrl.

Third, the writing device tree node

  Documentation Documentation/devicetree/bindings/leds/leds-gpio.txtexplains in detail how to drive Linux comes with the corresponding device tree node of the write, when we write device node to note the following:

  • ①, create a node represents an LED lamp device, such dtsleds, if there are a plurality of LED lamps it lights each LED on the board as a child node of dtsleds.
  • ②, compatible attribute values ​​dtsleds node must be as "gpio-leds".
  • ③, set the label property, this property is optional, each child node has a label property label attribute indicates the name of the general LED lamp, such as color-coded word is red, green and the like.
  • ④, each child node must be set gpios property value, this LED indicates the GPIO pins used!
  • ⑤, you can set "linux, default-trigger" property value, which is the default setting LED light function, you can review Documentation/devicetree/bindings/leds/common.txtthe document to see options such as:
    • backlight: LED lamp as a backlight.
    • default-on: LED light on
    • heartbeat: LED indicator lights as a heartbeat, the warning lamp may be used as the system is running.
    • ide-disk: LED indicator lamp as a disk activity.
    • timer: LED lamp blinks periodically driven by a timer, flicker frequency can be modified
  • ⑥, can set "default-state" attribute value, it can be set to on, off, or keep, is on by default when the LED lamp, the LED lamp is off, then turned off by default, to keep holding the current mode, then the LED lamp.

Example code for the device node

dtsleds {
	compatible = "gpio-leds";
	
	led0 {
		label = "red";
		gpios = <&gpio5 3 GPIO_ACTIVE_LOW>;
		default-state = "off";
	};
};

Fourth, run the program

  Development of new start board equipment zImage and tree file later view the start / sys/bus/platform/devices/dtsledsthe directory exists, but can also ls view the contents directory under the LEDs, as shown in FIG:

Here Insert Picture Description

  In leds have a directory called "red" sub-directory, subdirectory name is the label attribute values ​​that we set in the device tree! Then, it enters the command test LED blinking:

echo 1 > /sys/class/leds/red/brightness //打开 LED0
echo 0 > /sys/class/leds/red/brightness //关闭 LED0

  If it can turn on and off the normal LED lights, then that means we Linux kernel driver that comes with LED lights work properly. We typically use an LED as a system indicator , the system is running normal, then the LED indicator will be flashing. Setting method, by adding "linux, default-trigger" to the attribute information, the attribute value "heartbeat" in this dtsleds node device, the node content dtsleds modified after completion of the following:

dtsleds {
	compatible = "gpio-leds";
	
	led0 {
		label = "red";
		gpios = <&gpio5 3 GPIO_ACTIVE_LOW>;
		linux,default-trigger = "heartbeat";
		default-state = "on";
	};
};

Again recompiled replacement device tree, running board, can see the LED blinks!

We published 722 original articles · won praise 1207 · Views 860,000 +

Guess you like

Origin blog.csdn.net/ReCclay/article/details/105376106