Linux内核LCD驱动分析(二)

上一节我们分析了framebuffer驱动的核心部分,下面我们就以Atmel的lcd驱动作为例子简单的分析一下驱动的实现过程,也就是注册fb_info的过程。
LCD的驱动位于内核源码的drivers/video/fbdev中, Atmel的驱动源码是atmel_lcdfb.c。驱动的入口是

module_platform_driver_probe(atmel_lcdfb_driver, atmel_lcdfb_probe);

是的,只有一个宏。看看atmel_lcdfb_driver这个结构体。

static struct platform_driver atmel_lcdfb_driver = {
	.remove		= __exit_p(atmel_lcdfb_remove),
	.suspend	= atmel_lcdfb_suspend,
	.resume		= atmel_lcdfb_resume,
	.id_table	= atmel_lcdfb_devtypes,
	.driver		= {
		.name	= "atmel_lcdfb",
		.of_match_table	= of_match_ptr(atmel_lcdfb_dt_ids),
	},
};

这里涉及到了内核的总线驱动模型,我们暂时不去分析它的实现原理。当某个程序使用module_platform_driver_probe枚举某一个结构体的时候,它会在内核中寻找一个和它同名的设备,如果能找到这个设备,它就认为是对的,就调用自己的probe函数,也就是atmel_lcdfb_probe。还有另一种方式就是设备树的方式,当设备树中存在atmel_lcdfb_devtypes中列出的设备的时候,就会调用atmel_lcdfb_probe函数。

在Atmel中有相关的设备树文件,我们以at91sam9261为例,在arch/arm/boot/dts/at91sam9261.dtsi中存在以下的设备树节点。

fb0: fb@0x00600000 {
    compatible = "atmel,at91sam9261-lcdc";
    reg = <0x00600000 0x1000>;
	interrupts = <21 IRQ_TYPE_LEVEL_HIGH 3>;
	pinctrl-names = "default";
	pinctrl-0 = <&pinctrl_fb>;
	clocks = <&lcd_clk>, <&hclk1>;
	clock-names = "lcdc_clk", "hclk";
	status = "disabled";
};

在arch/arm/boot/dts/at91sam9261ek.dts中,存在以下的设备树节点。

fb0: fb@0x00600000 {
    display = <&display0>;
    atmel,power-control-gpio = <&pioA 12 GPIO_ACTIVE_LOW>;
    status = "okay";

    display0: display {
        bits-per-pixel = <16>;
        atmel,lcdcon-backlight;
        atmel,dmacon = <0x1>;
        atmel,lcdcon2 = <0x80008002>;
        atmel,guard-time = <1>;
        atmel,lcd-wiring-mode = "BRG";

        display-timings {
            native-mode = <&timing0>;
            timing0: timing0 {
                clock-frequency = <4965000>;
                hactive = <240>;
                vactive = <320>;
                hback-porch = <1>;
                hfront-porch = <33>;
                vback-porch = <1>;
                vfront-porch = <0>;
                hsync-len = <5>;
                vsync-len = <1>;
                hsync-active = <1>;
                vsync-active = <1>;
            };
        };
    };
};


这里假设内核中已经存在了名字为"atmel_lcdfb"的“设备”或者设备树中已经定义了相关的id,即这里的设备树中存在compatible = "atmel,at91sam9261-lcdc"属性, 则atmel_lcdfb_probe会被调用, 然而,这个函数非常的长。。。。

static int __init atmel_lcdfb_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct fb_info *info;
	struct atmel_lcdfb_info *sinfo;
	struct atmel_lcdfb_pdata *pdata = NULL;
	struct resource *regs = NULL;
	struct resource *map = NULL;
	struct fb_modelist *modelist;
	int ret;

	dev_dbg(dev, "%s BEGIN\n", __func__);

	ret = -ENOMEM;
	info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);// 分配一个fb_info,分配了strcut atmel_lcdfb_info这么大
	if (!info) {
		dev_err(dev, "cannot allocate memory\n");
		goto out;
	}

	sinfo = info->par;
	sinfo->pdev = pdev;
	sinfo->info = info; // 将sinfo->info指针指向刚刚分配的fb_info

	INIT_LIST_HEAD(&info->modelist);

	if (pdev->dev.of_node) {  //如果存在设备树节点,从设备树中初始化sinfo结构体
		ret = atmel_lcdfb_of_init(sinfo);
		if (ret)
			goto free_info;
	} else if (dev_get_platdata(dev)) { //否则从平台设备中初始化sinfo,也就是刚刚说在内核中存在的"atmel_lcdfb"的“设备”
		struct fb_monspecs *monspecs;
		int i;

		pdata = dev_get_platdata(dev);
		monspecs = pdata->default_monspecs;
		sinfo->pdata = *pdata;

		for (i = 0; i < monspecs->modedb_len; i++)
			fb_add_videomode(&monspecs->modedb[i], &info->modelist);

		sinfo->config = atmel_lcdfb_get_config(pdev);// 从平台设备中获取一个atmel_lcdfb_config结构体

		info->var.bits_per_pixel = pdata->default_bpp ? pdata->default_bpp : 16;  //设置fb_info结构体,是否指定默认的颜色深度,没有就使用16位
		memcpy(&info->monspecs, pdata->default_monspecs, sizeof(info->monspecs));
	} else {
		dev_err(dev, "cannot get default configuration\n");
		goto free_info;
	}

	if (!sinfo->config)
		goto free_info;

	sinfo->reg_lcd = devm_regulator_get(&pdev->dev, "lcd");
	if (IS_ERR(sinfo->reg_lcd))
		sinfo->reg_lcd = NULL;

	info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
	info->pseudo_palette = sinfo->pseudo_palette;
	info->fbops = &atmel_lcdfb_ops;  //设置fb_info结构体, 指定lcd的操作函数

	info->fix = atmel_lcdfb_fix; //设置fb_info结构体的固定参数
	strcpy(info->fix.id, sinfo->pdev->name); // 设置id为设备的名字

	/* Enable LCDC Clocks */
	sinfo->bus_clk = clk_get(dev, "hclk");    //获取hclk
	if (IS_ERR(sinfo->bus_clk)) {
		ret = PTR_ERR(sinfo->bus_clk);
		goto free_info;
	}
	sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");  //获取lcdc_clk
	if (IS_ERR(sinfo->lcdc_clk)) {
		ret = PTR_ERR(sinfo->lcdc_clk);
		goto put_bus_clk;
	}
	atmel_lcdfb_start_clock(sinfo); // 设置打开上面两个时钟

	modelist = list_first_entry(&info->modelist,
			struct fb_modelist, list);
	fb_videomode_to_var(&info->var, &modelist->mode); //从fb_videomode中取出参数,并设置fb_info的可变参数

	atmel_lcdfb_check_var(&info->var, info);// 检验可变参数的合法性

	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);  //从平台设备中获取第0个IO资源
	if (!regs) {
		dev_err(dev, "resources unusable\n");
		ret = -ENXIO;
		goto stop_clk;
	}

	sinfo->irq_base = platform_get_irq(pdev, 0);  // 从平台设备中获取中断资源
	if (sinfo->irq_base < 0) {
		dev_err(dev, "unable to get irq\n");
		ret = sinfo->irq_base;
		goto stop_clk;
	}

	/* Initialize video memory */
	map = platform_get_resource(pdev, IORESOURCE_MEM, 1); //从平台设备中获取第1个IO资源
	if (map) { // 如果存在平台设备的资源
		/* use a pre-allocated memory buffer */
		info->fix.smem_start = map->start;   //设置固定参数的内存起始地址,物理地址
		info->fix.smem_len = resource_size(map);// 设置固定参数的内存大小
		if (!request_mem_region(info->fix.smem_start,          //申请内存资源
					info->fix.smem_len, pdev->name)) {
			ret = -EBUSY;
			goto stop_clk;
		}

		info->screen_base = ioremap_wc(info->fix.smem_start,    //内存做ioremap内存映射,返回虚拟地址,并设置info->screen_base
					       info->fix.smem_len);
		if (!info->screen_base) {
			ret = -ENOMEM;
			goto release_intmem;
		}

		/*
		 * Don't clear the framebuffer -- someone may have set
		 * up a splash image.
		 */
	} else { //如果不存在平台设备资源,则自己分配一个buffer
		/* allocate memory buffer */
		ret = atmel_lcdfb_alloc_video_memory(sinfo); // 使用dma_alloc_wc分配一个buffer,并设置了info->screen_base
		if (ret < 0) {
			dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
			goto stop_clk;
		}
	}

	/* LCDC registers */
	info->fix.mmio_start = regs->start;  //设置固定参数mmio_start,即Start of Memory Mapped I/O 
	info->fix.mmio_len = resource_size(regs);

	if (!request_mem_region(info->fix.mmio_start,    //申请资源
				info->fix.mmio_len, pdev->name)) {
		ret = -EBUSY;
		goto free_fb;
	}

	sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);  //对IO资源进行内存io映射
	if (!sinfo->mmio) {
		dev_err(dev, "cannot map LCDC registers\n");
		ret = -ENOMEM;
		goto release_mem;
	}

	/* Initialize PWM for contrast or backlight ("off") 初始化PWM*/
	init_contrast(sinfo);

	/* interrupt 申请中断,并设置中断服务函数*/
	ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
	if (ret) {
		dev_err(dev, "request_irq failed: %d\n", ret);
		goto unmap_mmio;
	}

	/* Some operations on the LCDC might sleep and
	 * require a preemptible task context */
	INIT_WORK(&sinfo->task, atmel_lcdfb_task);

	ret = atmel_lcdfb_init_fbinfo(sinfo); // 设置var.activate,colormap等参数
	if (ret < 0) {
		dev_err(dev, "init fbinfo failed: %d\n", ret);
		goto unregister_irqs;
	}

	ret = atmel_lcdfb_set_par(info);// 设置fix.visual,fix.line_length,使能DMA,设置像素时钟等一系列操作
	if (ret < 0) {
		dev_err(dev, "set par failed: %d\n", ret);
		goto unregister_irqs;
	}

	dev_set_drvdata(dev, info);

	/*
	 * Tell the world that we're ready to go
	 */
	ret = register_framebuffer(info);  //注册fb_info结构体
	if (ret < 0) {
		dev_err(dev, "failed to register framebuffer device: %d\n", ret);
		goto reset_drvdata;
	}

	/* Power up the LCDC screen */
	atmel_lcdfb_power_control(sinfo, 1);//点亮LCD

	dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n",
		       info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);

	return 0;

reset_drvdata:
	dev_set_drvdata(dev, NULL);
	fb_dealloc_cmap(&info->cmap);
unregister_irqs:
	cancel_work_sync(&sinfo->task);
	free_irq(sinfo->irq_base, info);
unmap_mmio:
	exit_backlight(sinfo);
	iounmap(sinfo->mmio);
release_mem:
 	release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
free_fb:
	if (map)
		iounmap(info->screen_base);
	else
		atmel_lcdfb_free_video_memory(sinfo);

release_intmem:
	if (map)
		release_mem_region(info->fix.smem_start, info->fix.smem_len);
stop_clk:
	atmel_lcdfb_stop_clock(sinfo);
	clk_put(sinfo->lcdc_clk);
put_bus_clk:
	clk_put(sinfo->bus_clk);
free_info:
	framebuffer_release(info);
out:
	dev_dbg(dev, "%s FAILED\n", __func__);
	return ret;
}

在atmel_lcdfb_of_init函数中,驱动通过很多次调用of_property_read_u32来获取设备树中的各种属性,包括设置可变参数的bpp值,lcd寄存器的配置值,dma的配置,电源IO等等。

驱动还会设置info->fbops = &atmel_lcdfb_ops属性,就是上一节中提到的fbops操作函数。atmel_lcdfb_ops的内容如下所示。

static struct fb_ops atmel_lcdfb_ops = {
	.owner		= THIS_MODULE,
	.fb_check_var	= atmel_lcdfb_check_var,
	.fb_set_par	= atmel_lcdfb_set_par,
	.fb_setcolreg	= atmel_lcdfb_setcolreg,
	.fb_blank	= atmel_lcdfb_blank,
	.fb_pan_display	= atmel_lcdfb_pan_display,
	.fb_fillrect	= cfb_fillrect,
	.fb_copyarea	= cfb_copyarea,
	.fb_imageblit	= cfb_imageblit,
};

这里会发现,驱动中并没有指定read/write/mmap对应的fb_read/fb_write/fb_mmap操作函数,所以在应用程序调用对应的系统调用接口的时候,使用的是fb_mem提供的读写函数。

然后驱动设置了atmel_lcdfb_fix固定参数,并使能了相关的系统时钟。

驱动中调用了platform_get_resource来获取了对应的IO资源。

regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!regs) {
	dev_err(dev, "resources unusable\n");
	ret = -ENXIO;
	goto stop_clk;
}

这里的资源对应着设备树中的reg属性,代表着lcd控制器的IO地址空间,即:

reg = <0x00600000 0x1000>;

驱动后面会对这一片内存空间进行IO地址映射,即:

info->fix.mmio_start = regs->start;
info->fix.mmio_len = resource_size(regs);

if (!request_mem_region(info->fix.mmio_start, info->fix.mmio_len, pdev->name)) {
    ret = -EBUSY;
    goto free_fb;
}

sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
if (!sinfo->mmio) {
    dev_err(dev, "cannot map LCDC registers\n");
    ret = -ENOMEM;
    goto release_mem;
}

因为设备树中reg属性只有一个,所以当驱动程序通过调用platform_get_resource获取第二个资源的时候,会返回NULL,即:

map = platform_get_resource(pdev, IORESOURCE_MEM, 1);

会返回NULL,所以这个时候,驱动会调用atmel_lcdfb_alloc_video_memory自己分配一段内存空间,实现如下:

static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
{
	struct fb_info *info = sinfo->info;
	struct fb_var_screeninfo *var = &info->var;
	unsigned int smem_len;

	smem_len = (var->xres_virtual * var->yres_virtual
		    * ((var->bits_per_pixel + 7) / 8)); // 显存的字节数,加7是为了以字节对齐,不足一字节以一字节算。
	info->fix.smem_len = max(smem_len, sinfo->smem_len);

    //分配一段连续的内存空间, info->screen_base保存虚拟地址, info->fix.smem_start是物理地址
	info->screen_base = dma_alloc_wc(info->device, info->fix.smem_len,
					 (dma_addr_t *)&info->fix.smem_start,
					 GFP_KERNEL);

	if (!info->screen_base) {
		return -ENOMEM;
	}

	memset(info->screen_base, 0, info->fix.smem_len);//哈哈,这里会清空缓存,即LCD显示全黑

	return 0;
}

这么辛苦,终于把这个fb_info结构体设置得差不多了,最后,驱动程序会向fb_mem注册这个fb_info结构体。

    /*
    * Tell the world that we're ready to go
    */
    //上面这个注释真的是醉了。。。
    ret = register_framebuffer(info);
    if (ret < 0) {
        dev_err(dev, "failed to register framebuffer device: %d\n", ret);
        goto reset_drvdata;
    }

看看这个伟大得函数吧。

int
register_framebuffer(struct fb_info *fb_info)
{
	int ret;

	mutex_lock(®istration_lock); //上锁,防止同时注册
	ret = do_register_framebuffer(fb_info);
	mutex_unlock(®istration_lock); //解锁

	return ret;
}

继续看do_register_framebuffer这个函数。看看它都做了什么,虽然之前已经知道它会将fb_info结构体放进registered_fb[ ]数组中。

static int do_register_framebuffer(struct fb_info *fb_info)
{
	int i, ret;
	struct fb_event event;
	struct fb_videomode mode;

	if (fb_check_foreignness(fb_info))
		return -ENOSYS;

	ret = do_remove_conflicting_framebuffers(fb_info->apertures,
						 fb_info->fix.id,
						 fb_is_primary_device(fb_info));
	if (ret)
		return ret;

	if (num_registered_fb == FB_MAX) // 查询注册数量是否超过32个
		return -ENXIO;

	num_registered_fb++; //注册数量++
	for (i = 0 ; i < FB_MAX; i++)
		if (!registered_fb[i])  // 从数组中找出一个空项
			break;
	fb_info->node = i;
	atomic_set(&fb_info->count, 1); //设置打开计数
	mutex_init(&fb_info->lock);    //初始化一些锁
	mutex_init(&fb_info->mm_lock);

    //创建设备节点,对应于/dev/fbX
	fb_info->dev = device_create(fb_class, fb_info->device,
				     MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
	if (IS_ERR(fb_info->dev)) {
		/* Not fatal */
		printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
		fb_info->dev = NULL;
	} else
		fb_init_device(fb_info);

	if (fb_info->pixmap.addr == NULL) {
		fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
		if (fb_info->pixmap.addr) {
			fb_info->pixmap.size = FBPIXMAPSIZE;
			fb_info->pixmap.buf_align = 1;
			fb_info->pixmap.scan_align = 1;
			fb_info->pixmap.access_align = 32;
			fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
		}
	}	
	fb_info->pixmap.offset = 0;

	if (!fb_info->pixmap.blit_x)
		fb_info->pixmap.blit_x = ~(u32)0;

	if (!fb_info->pixmap.blit_y)
		fb_info->pixmap.blit_y = ~(u32)0;

	if (!fb_info->modelist.prev || !fb_info->modelist.next)
		INIT_LIST_HEAD(&fb_info->modelist);

	if (fb_info->skip_vt_switch)
		pm_vt_switch_required(fb_info->dev, false);
	else
		pm_vt_switch_required(fb_info->dev, true);

	fb_var_to_videomode(&mode, &fb_info->var);
	fb_add_videomode(&mode, &fb_info->modelist);
	registered_fb[i] = fb_info;  //添加到数组中

	event.info = fb_info;
	if (!lockless_register_fb)
		console_lock();
	if (!lock_fb_info(fb_info)) {
		if (!lockless_register_fb)
			console_unlock();
		return -ENODEV;
	}

    //通知大家我已经注册了一个fb设备
	fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
	unlock_fb_info(fb_info);
	if (!lockless_register_fb)
		console_unlock();
	return 0;
}

嗯,整个过程就是这样子了,Atmel设备树中的相关属性,会在驱动运行的probe函数中将一些参数设置到fb_info结构体中,在最后,会在register_framebuffer的时候将这个结构体注册到fb_mem的数组中,等到应用程序调用系统调用接口的时候,会根据次设备号从数组中取到对应的fb_info结构,然后根据结构中的内存地址信息去进行相关的操作。

 

你可能感兴趣的:(linux)