驱动
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/leds.h>
#include <mach/regs-gpio.h>
#include <mach/gpio-bank.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <mach/gpio.h>
#define GPIO_LED1 S5PV210_GPJ0(3)
#define GPIO_LED2 S5PV210_GPJ0(4)
#define GPIO_LED3 S5PV210_GPJ0(5)
static struct led_classdev led_cdev1;
static struct led_classdev led_cdev2;
static struct led_classdev led_cdev3;
static void s5pv210_led_set1(struct led_classdev *led_cdev,enum led_brightness value)
{
if(value == LED_OFF)
gpio_set_value(GPIO_LED1,1);
else
gpio_set_value(GPIO_LED1,0);
printk(KERN_INFO "value = %d\n",value);
}
static void s5pv210_led_set2(struct led_classdev *led_cdev,enum led_brightness value)
{
if(value == LED_OFF)
gpio_set_value(GPIO_LED2,1);
else
gpio_set_value(GPIO_LED2,0);
printk(KERN_INFO "value = %d\n",value);
}
static void s5pv210_led_set3(struct led_classdev *led_cdev,enum led_brightness value)
{
if(value == LED_OFF)
gpio_set_value(GPIO_LED3,1);
else
gpio_set_value(GPIO_LED3,0);
printk(KERN_INFO "value = %d\n",value);
}
static int __init s5pv210_led_init(void)
{
int ret = -1;
ret = gpio_request(GPIO_LED1, "GPJ0");
if(ret)
printk("gpio_request1 fail\n");
gpio_direction_output(GPIO_LED1, 1);
ret = gpio_request(GPIO_LED2, "GPJ0");
if(ret)
printk("gpio_request2 fail\n");
gpio_direction_output(GPIO_LED2, 1);
ret = gpio_request(GPIO_LED3, "GPJ0");
if(ret)
printk("gpio_request3 fail\n");
gpio_direction_output(GPIO_LED3, 1);
led_cdev1.name = "led1";
led_cdev1.max_brightness = 1000;
led_cdev1.brightness = 0;
led_cdev1.brightness_set = s5pv210_led_set1;
ret = led_classdev_register(NULL, &led_cdev1);
if (ret < 0)
{
printk(KERN_ERR "led_classdev_register failed\n");
return ret;
}
led_cdev2.name = "led2";
led_cdev2.max_brightness = 1000;
led_cdev2.brightness = 0;
led_cdev2.brightness_set = s5pv210_led_set2;
ret = led_classdev_register(NULL, &led_cdev2);
if (ret < 0)
{
printk(KERN_ERR "led_classdev_register failed\n");
return ret;
}
led_cdev3.name = "led3";
led_cdev3.max_brightness = 1000;
led_cdev3.brightness = 0;
led_cdev3.brightness_set = s5pv210_led_set3;
ret = led_classdev_register(NULL, &led_cdev3);
if (ret < 0)
{
printk(KERN_ERR "led_classdev_register failed\n");
return ret;
}
return 0;
}
static void __exit s5pv210_led_exit(void)
{
led_classdev_unregister(&led_cdev1);
led_classdev_unregister(&led_cdev2);
led_classdev_unregister(&led_cdev3);
gpio_free(GPIO_LED1);
gpio_free(GPIO_LED2);
gpio_free(GPIO_LED3);
}
module_init(s5pv210_led_init);
module_exit(s5pv210_led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Xin-Ya Zhu<mhzhuxinya@outlook.com>");
MODULE_DESCRIPTION("s5pv210 led driver");
MODULE_ALIAS("s5pv210_leds");
应用
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILE "/sys/class/leds/led1/brightness"
#define LED_ON "1"
#define LED_OFF "0"
int main(void)
{
int fd = -1;
char buf[100] = {0};
fd = open(FILE,O_RDWR);
if(fd<0)
{
printf("open error.\n");
return -1;
}
write(fd,LED_ON,strlen(LED_ON));
sleep(1);
write(fd,LED_OFF,strlen(LED_OFF));
sleep(1);
write(fd,LED_ON,strlen(LED_ON));
sleep(1);
write(fd,LED_OFF,strlen(LED_OFF));
sleep(1);
close(fd);
return 0;
}