您的位置:首页 > 运维架构 > Linux

linux2.6.32.2 mini2440平台移植--LCD 背光驱动

2013-05-16 21:16 525 查看
1.3.1 LCD 背光控制原理

到目前为止,我们一直都在命令行下移植,查看结果,在 mini2440/micro2440 开发板中,LCD 背光是通过 CPU 的 LCD_PWR 引脚来控制的,从原理图中可以看出,它对应于 GPG4 。

当 LCD_PWR 输出为高电平"1"时,将打开背光;当输出为低电平"0"时,将关闭背光(注意:这里只是打开和关闭背光,而并没有背光亮度的调节作用)。

1.3.2 在内核中添加背光驱动程序

现在,我们需要增加一个简单的背光驱动,以便能够通过软件便可简单的控制背光的开关。我们要达到的目的是:在命令终端通过向背光设备发送偶数比如"0"便可关闭背光,发送奇数比如"1"便可打开背光,这样使用起来就方便多了,而不需要专门的应用程序控制它,提示:LCD 背光设备文件:/dev/backlight

在命令行种输入:echo 0 > /dev/backlight 可以关闭 LCD 背光。

在命令行种输入:echo 1 > /dev/backlight 可以打开 LCD 背光。

为了实现这点,我们在 linux-2.6.32.2/drivers/video 目录增加一个 mini2440_backlight.c

文件,内容如下:

//以下头文件可能并不是每一个都必须的,但多余的并不会影响驱动程序的内容

#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 <linux/gpio.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>

#undef DEBUG

//#define DEBUG

#ifdef DEBUG

#define DPRINTK(x...) {printk(__FUNCTION__"(%d): ",__LINE__);printk(##x);}

#else

#define DPRINTK(x...) (void)(0)

#endif

//定义背光驱动的名称为 backligh,将会出现在/dev/backlight

#define DEVICE_NAME "backlight"

//定义背光变量 bl_state,以记录背光的开关状态

static unsigned int bl_state;

//设置背光开关的函数,主要是翻转背光变量 bl_state

static inline void set_bl(int state)

{

bl_state = !!state; //翻转 bl_state 变量

s3c2410_gpio_setpin(S3C2410_GPG(4), bl_state); //把结果写入背光所用的寄存器 GPG4

}

//获取背光状态

static inline unsigned int get_bl(void)

{

return bl_state;

}

//从应用程序读取参数,并传递到内核中

static ssize_t dev_write(struct file *file, const char *buffer, size_t count, loff_t * ppos)

{

unsigned char ch;

int ret;

if (count == 0) {

return count;

}

//使用 copy_from_user 函数从用户层/应用层读取参数

ret = copy_from_user(&ch, buffer, sizeof ch) ? -EFAULT : 0;

if (ret) {

return ret;

}

ch &= 0x01; //判断奇数还是偶数

set_bl(ch); //设置背光状态

return count;

}

//把内核参数传递给用户层/应用层的读函数

static ssize_t dev_read(struct file *filp, char *buffer, size_t count, loff_t *ppos)

{

int ret;

unsigned char str[] = {'0', '1' };

if (count == 0) {

return 0;

}

//使用 copy_to_user 函数把内核参数传递到用户层/应用层

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 dev_fops = {

owner: THIS_MODULE,

read:dev_read,

write: dev_write,

};

static struct miscdevice misc = {

.minor = MISC_DYNAMIC_MINOR,

.name = DEVICE_NAME,

.fops = &dev_fops,

};

//设备初始化,内核启动时就有效

static int __init dev_init(void)

{

int ret;

ret = misc_register(&misc);

printk (DEVICE_NAME"\tinitialized\n");

//初始化背光所用的端口 GPG4 为输出

s3c2410_gpio_cfgpin(S3C2410_GPG(4), S3C2410_GPIO_OUTPUT);

//启动内核时打开背光

set_bl(1);

return ret;

}

static void __exit dev_exit(void)

{

misc_deregister(&misc);

}

module_init(dev_init); //注册背光驱动模块

module_exit(dev_exit); //卸载背光驱动模块

MODULE_LICENSE("GPL");

MODULE_AUTHOR("FriendlyARM Inc.");

然后把背光配置选项加入内核配置菜单,打开 linux-2.6.32.2/drivers/video/Kconfig,在如下位置加入:

config FB_S3C2410_DEBUG

bool "S3C2410 lcd debug messages"

depends on FB_S3C2410

help

Turn on debugging messages. Note that you can set/unset at run time

through sysfs

//在里加入 MINI2440 的背光驱动配置

config BACKLIGHT_MINI2440

tristate "Backlight support for mini2440 from FriendlyARM"

depends on MACH_MINI2440 && FB_S3C2410

help

backlight driver for MINI2440 from FriendlyARM

config FB_SM501

tristate "Silicon Motion SM501 framebuffer support"

depends on FB && MFD_SM501

select FB_CFB_FILLRECT

select FB_CFB_COPYAREA

select FB_CFB_IMAGEBLIT

再打开 linux-2.6.32.2/drivers/video/Makefile,根据配置定义加入驱动目标文件,如下:

# the test framebuffer is last

obj-$(CONFIG_FB_VIRTUAL)

#video output switch sysfs driver

+= vfb.o

obj-$(CONFIG_VIDEO_OUTPUT_CONTROL) += output.o

obj-$(CONFIG_BACKLIGHT_MINI2440) += mini2440_backlight.o

这样,我们就在内核中移植好了 mini2440 的背光驱动,在内核源代码根目录执行:

make menuconfig,依次选择如下子菜单:

Device Drivers --->

Graphics support --->

<*> Support for frame buffer devices --->

就可以找到该配置选项,在这里,按空格选中我们刚刚加入的 mini2440 配置项,然后退出保存内核配置菜单,在命令行执行:make zImage

将生成 arch/arm/boot/zImage,使用 supervivi 的"k"功能把它烧写到开发板中,可以在启动时看到如图所示的企鹅图像,这说明我们已经点亮了背光,只不过LCD 驱动还有些问题,下一节我们将会详细的介绍如何移植 LCD 驱动。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: