#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
int led[3] = {0};
char *led_name[3] = {"myled1","myled2","myled3"};
struct device_node *node;
int i;
struct timer_list mytimer;
void mytimer_function(struct timer_list *timer)
{
for(i=0;i<3;i++)
{
gpio_set_value(led[i],!gpiod_get_value(led[i]));
}
mod_timer(&mytimer,jiffies + HZ);
}
static int __init mycdev_init(void)
{
node = of_find_node_by_name(NULL,"myleds");
if(node == NULL)
{
printk("设备树节点解析失败\n");
return -1;
}
for(i=0;i<3;i++)
{
led[i] = of_get_named_gpio(node,led_name[i],0);
if(led[i] < 0)
{
printk("解析gpio编号失败\n");
return -1;
}
printk("解析gpio编号成功%d\n",led[i]);
if(gpio_request(led[i],NULL))
{
printk("申请gpio编号失败\n");
return -1;
}
printk("ok\n");
gpio_direction_output(led[i],0);
}
mytimer.expires = jiffies+HZ;
timer_setup(&mytimer,mytimer_function,0);
add_timer(&mytimer);
return 0;
}
static void __exit mycdev_exit(void)
{
del_timer(&mytimer);
for(i=0;i<3;i++)
{
gpio_set_value(led[i],0);
gpio_free(led[i]);
}
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
呃呃