2012年(1008)
分类:
2012-08-01 11:04:47
/************************************************
LCD背光的驱动,在TX2440A开发板上做测试
维护记录: 2009-10-30 V1.0
linux内核:2.6.31.4
硬件接法:
GPG4 -- LCD_PWREN
驱动用法:
设备名称:TX2440-backlight
*************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "TX2440-backlight"
static int BACKLIGHT_Major = 0; /* 主设备号 */
static unsigned int bl_state;
static inline void set_bl(int state)
{
bl_state = !!state;
s3c2410_gpio_setpin(S3C2410_GPG(4), bl_state);
}
static inline unsigned int get_bl(void)
{
return bl_state;
}
static int TX2440_backlight_open(struct inode *inode, struct file *file)
{
printk("TX2440-BACKLIGHT Driver Open Called!\n");
return 0;
}
static int TX2440_backlight_release(struct inode *inode, struct file *file)
{
printk("TX2440-BACKLIGHT Driver Release Called!\n");
return 0;
}
static ssize_t TX2440_backlight_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)
{
unsigned char ch;
int ret;
if (count == 0)
{
return count;
}
ret = copy_from_user(&ch, buffer, sizeof ch) ? -EFAULT : 0;
if (ret)
{
return ret;
}
ch &= 0x01;
set_bl(ch);
return count;
}
static ssize_t TX2440_backlight_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{
int ret;
unsigned char str[] = {'0', '1' };
if (count == 0)
{
return 0;
}
ret = copy_to_user(buffer, str + get_bl(), sizeof(unsigned char) ) ? -EFAULT : 0;
if (ret)
{
return ret;
}
return sizeof(unsigned char);
}
static struct file_operations TX2440_backlight_fops =
{
.owner = THIS_MODULE,
.open = TX2440_backlight_open,
.release = TX2440_backlight_release,
.read = TX2440_backlight_read,
.write = TX2440_backlight_write,
};
static struct class *backlight_class;
static int __init TX2440_backlight_init(void)
{
printk("TX2440 BACKLIGHT DRIVER MODULE INIT\n");
s3c2410_gpio_cfgpin(S3C2410_GPG(4), S3C2410_GPIO_OUTPUT);
set_bl(1);
BACKLIGHT_Major = register_chrdev(0, DEVICE_NAME, &TX2440_backlight_fops);
if (BACKLIGHT_Major < 0)
{
printk(DEVICE_NAME " can't register major number\n");
return BACKLIGHT_Major;
}
printk("register TX2440-BACKLIGHT Driver OK! Major = %d\n", BACKLIGHT_Major);
backlight_class = class_create(THIS_MODULE, DEVICE_NAME);
if(IS_ERR(backlight_class))
{
printk("Err: failed in TX2440-BACKLIGHT class. \n");
return -1;
}
device_create(backlight_class, NULL, MKDEV(BACKLIGHT_Major, 0), NULL, DEVICE_NAME);
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit TX2440_backlight_exit(void)
{
printk("TX2440 BACKLIGHT DRIVER MODULE EXIT\n");
unregister_chrdev(BACKLIGHT_Major, DEVICE_NAME);
device_destroy(backlight_class, MKDEV(BACKLIGHT_Major, 0));
class_destroy(backlight_class);
}
module_init(TX2440_backlight_init);
module_exit(TX2440_backlight_exit);
MODULE_AUTHOR("www.txmcu.com");
MODULE_DESCRIPTION("TX2440 BACKLIGHT Driver");
MODULE_LICENSE("GPL");
源代码: TX2440_backlight.rar