当前位置:网站首页>LCD notes (5) LCD driver framework_ Use device tree
LCD notes (5) LCD driver framework_ Use device tree
2022-07-26 12:32:00 【Cui Guo broke her mouth】
Linux The driver = Driver framework + Hardware programming . Based on QEMU Write the LCD The driver , Yes LCD The framework of the driver has been analyzed clearly . The core is :
Distribute fb_info
Set up fb_info
register fb_info
Hardware related settings
We write drivers based on the device tree .
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/init.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/cpufreq.h>
#include <linux/io.h>
#include <asm/div64.h>
#include <asm/mach/map.h>
struct lcd_regs {
volatile unsigned int fb_base_phys;
volatile unsigned int fb_xres;
volatile unsigned int fb_yres;
volatile unsigned int fb_bpp;
};
static struct lcd_regs *mylcd_regs;
static struct fb_info *myfb_info;
static unsigned int pseudo_palette[16];
/* from pxafb.c */
static inline unsigned int chan_to_field(unsigned int chan,
struct fb_bitfield *bf)
{
chan &= 0xffff;
chan >>= 16 - bf->length;
return chan << bf->offset;
}
static int mylcd_setcolreg(unsigned regno,
unsigned red, unsigned green, unsigned blue,
unsigned transp, struct fb_info *info)
{
unsigned int val;
/* dprintk("setcol: regno=%d, rgb=%d,%d,%d\n",
regno, red, green, blue); */
switch (info->fix.visual) {
case FB_VISUAL_TRUECOLOR:
/* true-colour, use pseudo-palette */
if (regno < 16) {
u32 *pal = info->pseudo_palette;
val = chan_to_field(red, &info->var.red);
val |= chan_to_field(green, &info->var.green);
val |= chan_to_field(blue, &info->var.blue);
pal[regno] = val;
}
break;
default:
return 1; /* unknown type */
}
return 0;
}
static struct fb_ops myfb_ops = {
.owner = THIS_MODULE,
.fb_setcolreg = mylcd_setcolreg,
.fb_fillrect = cfb_fillrect,
.fb_copyarea = cfb_copyarea,
.fb_imageblit = cfb_imageblit,
};
static int mylcd_probe(struct platform_device *pdev)
{
dma_addr_t phy_addr;
/* 1.1 Distribute fb_info */
myfb_info = framebuffer_alloc(0, NULL);
/* 1.2 Set up fb_info */ // The pin settings will be put in the device tree
/* a. var : LCD The resolution of the 、 Color format */
myfb_info->var.xres_virtual = myfb_info->var.xres = 500;
myfb_info->var.yres_virtual = myfb_info->var.yres = 300;
myfb_info->var.bits_per_pixel = 16; /* rgb565 */
myfb_info->var.red.offset = 11;
myfb_info->var.red.length = 5;
myfb_info->var.green.offset = 5;
myfb_info->var.green.length = 6;
myfb_info->var.blue.offset = 0;
myfb_info->var.blue.length = 5;
/* b. fix */
strcpy(myfb_info->fix.id, "100ask_lcd");
myfb_info->fix.smem_len = myfb_info->var.xres * myfb_info->var.yres * myfb_info->var.bits_per_pixel / 8;
if (myfb_info->var.bits_per_pixel == 24)
myfb_info->fix.smem_len = myfb_info->var.xres * myfb_info->var.yres * 4;
/* fb The virtual address of */
myfb_info->screen_base = dma_alloc_wc(NULL, myfb_info->fix.smem_len, &phy_addr,
GFP_KERNEL);
myfb_info->fix.smem_start = phy_addr; /* fb The physical address of */
myfb_info->fix.type = FB_TYPE_PACKED_PIXELS;
myfb_info->fix.visual = FB_VISUAL_TRUECOLOR;
myfb_info->fix.line_length = myfb_info->var.xres * myfb_info->var.bits_per_pixel / 8;
if (myfb_info->var.bits_per_pixel == 24)
myfb_info->fix.line_length = myfb_info->var.xres * 4;
/* c. fbops */
myfb_info->fbops = &myfb_ops;
myfb_info->pseudo_palette = pseudo_palette;
/* 1.3 register fb_info */
register_framebuffer(myfb_info);
/* 1.4 Hardware operation */
mylcd_regs = ioremap(0x021C8000, sizeof(struct lcd_regs));
mylcd_regs->fb_base_phys = phy_addr;
mylcd_regs->fb_xres = 500;
mylcd_regs->fb_yres = 300;
mylcd_regs->fb_bpp = 16;
return 0;
}
static int mylcd_remove(struct platform_device *pdev)
{
/* Reverse operation */
/* 2.1 Anti registration fb_info */
unregister_framebuffer(myfb_info);
/* 2.2 Release fb_info */
framebuffer_release(myfb_info);
iounmap(mylcd_regs);
return 0;
}
static const struct of_device_id mylcd_of_match[] = {
{ .compatible = "100ask,lcd_drv", }, // Find... In the device tree 100ask,lcd_drv This attribute , You can use this driver
{ },
};
MODULE_DEVICE_TABLE(of, simplefb_of_match);
static struct platform_driver mylcd_driver = {
.driver = {
.name = "mylcd",
.of_match_table = mylcd_of_match, // Define an array , Which device can use this driver
},
.probe = mylcd_probe,
.remove = mylcd_remove,
};
static int __init lcd_drv_init(void)
{
int ret;
struct device_node *np;
ret = platform_driver_register(&mylcd_driver);
if (ret)
return ret;
return 0;
}
/* 2. exit */
static void __exit lcd_drv_exit(void)
{
platform_driver_unregister(&mylcd_driver);
}
module_init(lcd_drv_init);
module_exit(lcd_drv_exit);
MODULE_AUTHOR("www.100ask.net");
MODULE_DESCRIPTION("Framebuffer driver for the linux");
MODULE_LICENSE("GPL");
2. Entry function registration platform_driver
3. The device tree has corresponding nodes
framebuffer-mylcd {
compatible = "100ask,lcd_drv";
};4. To write probe function
Distribute fb_info
Set up fb_info
register fb_info
Hardware related settings
Pin settings
The clock is set
LCD Controller settings
边栏推荐
- 回溯——46. 全排列
- Why is redis so fast? Redis threading model and redis multithreading
- 连锁店收银系统如何帮助鞋店管理好分店?
- 一款超好用的神器Apifox,甩 Swagger 几条街...(荣耀典藏版)
- Pytoch deep learning quick start tutorial -- mound tutorial notes (II)
- Do you need to launch MES system? What are you struggling with?
- The difference between JVM memory overflow and memory leak
- Backtracking - 491. Incremental subsequence
- 回溯——第51题. N皇后——必须攻克的经典回溯难题
- 海外APP推送(下篇):海外厂商通道集成指南
猜你喜欢

面试京东T5,被按在地上摩擦,鬼知道我经历了什么?

Emerging security providers to learn about in 2022

若有声明”int x=5,y=1;”,则表达式x<y?x++:y++的结果是:

Backtracking - question 51 Queen n -- a classic backtracking problem that must be overcome

自定义浏览器默认右击菜单栏

UE5 官方案例Lyra 全特性详解 7.资源管理

VS code 设置Ctrl+S保存,自动格式化的方法

The map function counts the number of occurrences of characters

STM32驱动HC05蓝牙串口通信模块

Microsoft has shut down two attack methods: Office macro and RDP brute force cracking
随机推荐
How does the chain store cashier system help shoe stores manage their branches?
数据库组成存储过程和函数
回溯——第51题. N皇后——必须攻克的经典回溯难题
How RFID works
Emerging security providers to learn about in 2022
Data query of MySQL (aggregate function)
Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (medium)
QT入门引导 及其 案例讲解
MATLAB中strjoin函数使用
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
动静态库的实现(打包动静态库供他人使用)
“2022华为开发者大赛中国区东部赛区开幕式”在福州成功举办
3D point cloud course (VIII) -- feature point matching
面试京东T5,被按在地上摩擦,鬼知道我经历了什么?
What is the Internet of things? The most comprehensive explanation of common IOT protocols
行业案例|指标中台如何助力银行业普惠金融可持续发展
基于STM32的SIM900A发送中文和英文短信
Redis主从复制原理
HCIP-9.OSPF的各种拓展
yolov7训练危险品识别 pytorch