led 驱动,lcd背光驱动,仿照杨创提供的驱动,写成misc 驱动。lcd 背光驱动的本质上也是个led 驱动。
/* * LED interface driver for utu2440 * * This file is subject to the terms and conditions of the GNU General Public * License. See the file "COPYING" in the main directory of this archive * for more details. * * [email protected] */ #include <linux/types.h> #include <linux/ioport.h> #include <linux/fcntl.h> #include <linux/miscdevice.h> #include <linux/sched.h> #include <linux/spinlock.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/input.h> #include <linux/init.h> #include <linux/delay.h> #include <linux/interrupt.h> #include <linux/device.h> #include <asm/uaccess.h> #include <asm/system.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/arch/regs-gpio.h> #define LED_DRIVER "utu2440 LED Driver v1.00" static unsigned long led_table [] = { S3C2410_GPF4, S3C2410_GPF5, S3C2410_GPF6, S3C2410_GPF7, }; static int led_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg); static int led_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { switch (cmd) { case 0: case 1: if (arg > 4) { return -EINVAL; } s3c2410_gpio_setpin(led_table[arg], !cmd); default: return -EINVAL; } return 0; } static struct file_operations led_fops = { .ioctl = led_ioctl, }; static struct miscdevice led_dev = { MISC_DYNAMIC_MINOR, "myled", &led_fops }; static int led_init(void) { pr_info("%s\n", LED_DRIVER); misc_register(&led_dev); return 0; } static void __exit led_exit(void) { misc_deregister(&led_dev); } module_init(led_init); module_exit(led_exit); MODULE_AUTHOR("[email protected]"); MODULE_LICENSE("GPL");
#include <linux/errno.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/input.h> #include <linux/init.h> #include <linux/serio.h> #include <linux/delay.h> #include <linux/clk.h> #include <linux/miscdevice.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/uaccess.h> #include <mach/regs-clock.h> #include <plat/regs-timer.h> #include <mach/regs-gpio.h> #include <linux/cdev.h> #define DEVICE_NAME "backlight" //设备名称 #define DEVICE_MINOR 5 //次设备号,这里我们将设备注册为misc设备,这种设备的主设备号都为10 static int my2440_backlight_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { switch(cmd) { case 0: //当接收的命令为0时,就将GPG4引脚设为低电平,关闭背光 s3c2410_gpio_setpin(S3C2410_GPG4, 0); printk(DEVICE_NAME " turn off!\n"); return 0; case 1: //当接收的命令为1时,就将GPG4引脚设为高电平,开启背光 s3c2410_gpio_setpin(S3C2410_GPG4, 1); printk(DEVICE_NAME " turn on!\n"); return 0; default: return -EINVAL; } } static struct file_operations dev_fops = { .owner = THIS_MODULE, .ioctl = my2440_backlight_ioctl, //这里只使用控制IO口的方式来控制背光 }; static struct miscdevice misc = { .minor = DEVICE_MINOR, .name = DEVICE_NAME, .fops = &dev_fops, }; static int __init dev_init(void) { int ret; ret = misc_register(&misc); //注册成misc设备 if(ret < 0) { printk("Register misc device fiald!"); return ret; } //将GPG4口配置成输出口 s3c2410_gpio_cfgpin(S3C2410_GPG4, S3C2410_GPG4_OUTP); return ret; } static void __exit dev_exit(void) { misc_deregister(&misc); //注销该misc设备 } module_init(dev_init); module_exit(dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("xzf"); MODULE_DESCRIPTION("Backlight control for my2440");
分析如下
1.数据结构
a.miscdevice,混杂设备,里面做了很多标准字符设备的封装
这里直接申明led_dev使用,填充三个数据,次设备号,设备名称,驱动操作
b.led 信息,这里用到了一个数组,与平台相关。
c.lcd 背光,直接使用接口操作
2.驱动分析
a.入口,出口。module_init(led_init);module_exit(led_exit);
led_init做了一件事:将led_dev注册成misc
led_exit做了一件事:将led_dev从misc注销
b.标准文件驱动编写
1.文件操作指针结构赋值
2.完成相应操作