1. 程式人生 > >linux裝置樹之gpio

linux裝置樹之gpio

吐舌頭
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/platform_device.h>
#include <asm/io.h>
/* DTS
	myled{
		compatible = "led";
		/* led2-5: gpx2_7 gpx1_0 gpf3_4 gpf3_5 *//*
		gpios = <&gpx2 7 0>, <&gpx1 0 0>, <&gpf3 4 0>, <&gpf3 5 0>;
		
	};
*/
MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("a simple driver example!");

//create a platform driver
struct resource *res;

int led_pin;

int led_probe(struct platform_device *pdev)
{
	int i = 0; 
	char name_buf[10] = {0};
	printk("probe !\n");
	
	// 獲取DTS:gpios = <&gpx2 7 0>, <&gpx1 0 0>, <&gpf3 4 0>, <&gpf3 5 0>;(正式)
	for(i = 0; i < 4; i++) {
		// 從DTB解析管腳
		led_pin = of_get_gpio(pdev->dev.of_node, i);
		printk("led-gpio: %d\n", led_pin);
		sprintf(name_buf, "led%d-pin", i);
		devm_gpio_request(&pdev->dev, led_pin, name_buf);
		// gpio_request(led_pin, name_buf);
		gpio_direction_output(led_pin, 1);
		gpio_set_value(led_pin, 1);
	}

#if 0 
	// 獲取DTS:reg = <0x11000c40 4>;
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	printk("baseaddr: %#x\n", res->start);
	// 獲取DTS:pin = <7>; (非正式)
	res = platform_get_resource_byname(pdev, 0, "pin");
	printk("pinnum: %#x\n", res->start);
#endif	

	return 0;
}

int led_remove(struct platform_device *pdev)
{
	printk("remove !\n");
	return 0;
}

struct of_device_id led_table[] = {
	{.compatible = "led"},
	{}
};

struct platform_driver led_driver = {
	.probe = led_probe,
	.remove = led_remove,
	.driver = {
		.name = "11000c40.led_node",
		.of_match_table = led_table
	}
};

static int led_init(void)
{
	printk("module install\n");
	
	//add into platform bus
	platform_driver_register(&led_driver);

	return 0;
}

static void led_exit(void)
{
	printk("module release\n");
	//del from platform bus
	platform_driver_unregister(&led_driver);
}


module_init(led_init);
module_exit(led_exit);