驱动测试代码:
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
//#include <linux/platform_device.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#define GPIO_LOW 0
#define GPIO_HIGH 1
int gpio;
int major;
static struct class *cls;
static int led_open(struct inode *inode, struct file *file)
{
printk(KERN_EMERG "%s-%d: enter\n",__FUNCTION__,__LINE__);
return 0;
}
static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
int val;
int ret;
printk(KERN_EMERG "%s-%d: enter\n",__FUNCTION__,__LINE__);
ret = copy_from_user(&val, buf, count); // copy_to_user();
if (val == 1)
{
gpio_set_value(gpio,GPIO_LOW);
}
else
{
gpio_set_value(gpio,GPIO_HIGH);
}
return 0;
}
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
};
static int led_probe(struct platform_device *pdev)
{
int ret ;
int i;
enum of_gpio_flags flag;
struct device_node *led_node = pdev->dev.of_node;
struct device_node *child;
printk(KERN_EMERG "%s-%d: enter\n",__FUNCTION__,__LINE__);
for_each_child_of_node(led_node, child)
{
printk(KERN_ERR"child->name=%s,child->full_name=%s,%p\n",child->name,child->full_name,child);
gpio = of_get_named_gpio_flags(child,"gpios", 0,&flag);
if (!gpio_is_valid(gpio)){
printk(KERN_INFO "hello: invalid gpio : %d\n",gpio);
return -1;
}
ret = gpio_request(gpio, "led");
if (ret) {
gpio_free(gpio);
return -EIO;
}
gpio_direction_output(gpio, GPIO_HIGH);
gpio_set_value(gpio,GPIO_LOW);
}
for(i=0; i < 10; i++)
{
gpio_set_value(gpio,GPIO_LOW);
mdelay(500);
gpio_set_value(gpio,GPIO_HIGH);
mdelay(500);
}
major = register_chrdev(0, "myled", &led_fops);
cls = class_create(THIS_MODULE, "myled");
device_create(cls, NULL, MKDEV(major, 0), NULL, "led");
gpio_set_value(gpio,GPIO_LOW);
printk(KERN_INFO "%s-%d: exit\n",__FUNCTION__,__LINE__);
return 0; //return Ok
}
static int led_remove(struct platform_device *pdev)
{
printk(KERN_INFO "Enter %s\n", __FUNCTION__);
gpio_free(gpio);
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "myled");
return 0;
}
static const struct of_device_id of_led_match[] = {
{ .compatible = "gpio-leds" },
{ /* Sentinel */ }
};
static struct platform_driver led_driver = {
.probe = led_probe,
.remove = led_remove,
.driver = {
.name = "led",
.owner = THIS_MODULE,
.of_match_table = of_led_match,
},
};
static int __init led_init(void)
{
printk(KERN_INFO "Enter %s\n", __FUNCTION__);
return platform_driver_register(&led_driver);
return 0;
}
static void __exit led_exit(void)
{
platform_driver_unregister(&led_driver);
printk(KERN_INFO "Exit Hello world\n");
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
设备树文件信息:
leds {
compatible = "gpio-leds";
led@1 {
pinctrl-names = "default";
pinctrl-0 = <&led1_ctl>;
label = "led1";
gpios = <&gpio1 1 GPIO_ACTIVE_LOW>;
linux,default-trigger = "heartbeat";
default-state = "off";
};
led@2 {
pinctrl-names = "default";
pinctrl-0 = <&led2_ctl>;
label = "led2";
gpios = <&gpio0 8 GPIO_ACTIVE_LOW>;
default-state = "off";
};
led@3 {
pinctrl-names = "default";
pinctrl-0 = <&led3_ctl>;
label = "led3";
gpios = <&gpio1 23 GPIO_ACTIVE_LOW>;
default-state = "on";
};
led@4 {
pinctrl-names = "default";
pinctrl-0 = <&led4_ctl>;
label = "led4";
gpios = <&gpio0 12 GPIO_ACTIVE_LOW>;
default-state = "on";
};
};
结果:
四个led全亮,程序结束