当前位置:网站首页>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
边栏推荐
- Redis master-slave replication principle
- Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)
- 结合环境光、接近传感以及红外测距的光距感芯片4530A
- Hit the blackboard and draw the key points: a detailed explanation of seven common "distributed transactions"
- Flutter's learning path
- 一款超好用的神器Apifox,甩 Swagger 几条街...(荣耀典藏版)
- Shell变量和引用
- 如何组装一个注册中心?
- Flutter JNI confusion introduction.So file release package flash back
- 羽毛球馆的两个基础设施你了解多少?
猜你喜欢

CVPR 2022 new SOTA for monocular depth estimation new CRFs: neural window fullyconnected CRFs

HTAP是有代价的

详解勒让德变换与共轭函数

How much do you know about the two infrastructures of the badminton stadium?

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

一款超好用的神器Apifox,甩 Swagger 几条街...(荣耀典藏版)

Pytorch深度学习快速入门教程 -- 土堆教程笔记(二)

连锁店收银系统如何帮助鞋店管理好分店?

3D point cloud course (VIII) -- feature point matching

What is a callback function? Understanding of the word "back"
随机推荐
【微信小程序】一文读懂,数据请求
Redis为什么这么快?Redis的线程模型与Redis多线程
The.Net webapi uses groupname to group controllers to render the swagger UI
14.2 byte stream learning
MySQL之数据查询(聚合函数)
El form displays two columns per row, with the bottom button centered
扫雷小游戏——轻松玩上瘾(C语言版)
Ubenwa, a start-up under Mila, received an investment of US $2.5 million to study the AI diagnosis of infant health
Uniapp H5, APP references external online JS
Ssj-21b time relay
Redis master-slave replication principle
Optical distance sensing chip 4530a combining ambient light, proximity sensing and infrared ranging
RFID的工作原理
14.2字节流学习
Backtracking - question 51 Queen n -- a classic backtracking problem that must be overcome
10. 509. Introduction to PKCs file format
数据库组成存储过程和函数
yolov7训练危险品识别 pytorch
回溯——46. 全排列
使用fastJson中的JSONObject对象简化POST请求传参