设备树下的platform总线-21

本文介绍Linux环境下设备树中platform总线的应用,重点解析of_iomap函数用于内存映射的过程。通过具体实例rk3399 beep_driver.c演示如何在设备驱动中使用该函数,并展示完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设备树下的platform总线

of _iomap 函数

作用:of iomap函数用于直接内存映射,以前我们会通过ioremap函数来完成物理地址到虚拟地址的映射。
函数原型:

#include <linux/of_address.h>
void iomem *of_iomap(struct device_node *np,int index)

参数:

  • np:设备节点。
  • index: reg属性中要完成内存映射的段,如果reg属性只有一段的话index就设置0。返回值:经过内存映射后的虚拟内存首地址,如果为NULL 的话表示内存映射失败

代码 rk3399 为例

  • beep_driver.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>


struct device_node *test_device_node;
u32 out_value[2] = {0};
unsigned int *vir_gpio_dr;

struct of_device_id of_match_table[] = {

    {.compatible = "test1234"},
    {}

};


int misc_open(struct inode * inode, struct file * file){
    printk("hello misc_open \n");
    return 0;  
}

int misc_release(struct inode * inode, struct file * file){
    printk("hello misc_release bye bye \n");
    return 0;
}

int misc_read(struct file* file, char __user * ubuf, size_t size, loff_t * loff_t){

    char kbuf[64] = "heheh_read";

    printk("hello misc_read \n");

    if(copy_to_user(ubuf, kbuf, strlen(kbuf)) != 0 ){
        printk("copy_to_user error \n");
        return -1;
    }
    
    return 0;

}

int misc_write(struct file * file, const char __user * ubuf, size_t size, loff_t * loff_t){
    char kbuf[64] = {0};
    printk("hello misc_write \n");

    if (copy_from_user(kbuf, ubuf, size) != 0)
    {

        printk("copy_from_to_user error \n");
        return -1;
    
    }

    printk("kbuf is %s \n", kbuf);


    if (kbuf[0] == 1){
        *vir_gpio_dr = 0x4a400000;
    }else if (kbuf[0] == 0)
    {
    
        *vir_gpio_dr = 0x4a000000;
    }
    
    return 0;

}

struct file_operations misc_fops =
{
    .owner = THIS_MODULE,
    .open = misc_open,
    .release = misc_release,
    .read = misc_read,
    .write = misc_write
};


struct miscdevice misc_dev = 
{
    .minor = MISC_DYNAMIC_MINOR,
    .name = "hello_misc",
    .fops = &misc_fops
};




int beep_probe(struct platform_device *pdev){

    int ret = 0;

    printk("beep_probe \n");
    printk("node name is %s \n", pdev->dev.of_node->name);

#if 0
    //使用这个获取device_node 节点
    test_device_node = of_find_node_by_path("/test");
    if (test_device_node == NULL)
    {
        printk("of_find_node_by_path is error \n");
        return -1;
    }
#endif

    test_device_node = pdev->dev.of_node;

    ret = of_property_read_u32_array(test_device_node, "reg", out_value, 2);
    if ( ret < 0)
    {
       printk("of_property_read_u32_array is error \n");
       return -1;
    }

    printk("out_value[0] = 0x%08x \n", out_value[0]);
    printk("out_value[1] = 0x%08x \n", out_value[1]);


    vir_gpio_dr = of_iomap(test_device_node, 0);

    if(vir_gpio_dr == NULL){
        printk("of_iomap is error \n");
        return -1;
    }
    printk("of_iomap is success \n");


    // vir_gpio_dr = ioremap(out_value[1], 1);
    // if (vir_gpio_dr == NULL)
    // {
    //     printk("GPIO5_DR ioremap error \n");
    //     return -1;
    // }

    // printk("GPIO5_DR ioremap ok \n");


    ret = misc_register(&misc_dev);

    if (ret <0)
    {
        printk("misc_registe is error \n");
    }

    return 0;
}

int beep_remove(struct platform_device *pdev){
    printk("beep_remove \n");
    return 0;
}

const struct platform_device_id  beep_id_table = {
    .name = "beep_test"
};

struct platform_driver beep_device =
{
    .probe = beep_probe,
    .remove = beep_remove,
    .driver = {
        .name = "123",
        .owner = THIS_MODULE,
        .of_match_table = of_match_table
    },
    .id_table = &beep_id_table
};

static int beep_driver_init(void){
    int ret = 0;
    printk(KERN_EMERG "hello world enter \n");
    ret = platform_driver_register(&beep_device);
    if (ret < 0)
    {
        printk("platform_driver_register 失败\n");
    }

    printk("platform_driver_register ok\n");
    
    return 0;
}

static void beep_driver_exit(void){
    printk(KERN_EMERG "hello world exit! \n");
    platform_driver_unregister(&beep_device);
}

module_init(beep_driver_init);
module_exit(beep_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("LIYU");
  • app.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char const *argv[]){
    int fd;
    char buf[64] = {0};
    char buf_write[64] = "write aaaaaaaaa";
    fd = open("/dev/hello_misc",O_RDWR);
    if (fd < 0)
    {
        printf("open error \n");
        return fd;
    }
    printf("open success \n");
    
    //字符串转化为整型
    buf[0] = atoi(argv[1]);
    printf("buf[0]  %d \n", buf[0]);
    write(fd,buf,sizeof(buf));
    close(fd);
    return 0;
}
  • 设备树
test1: test {
		#address-cells = <1>;
		#size-cells = <1>;

		compatible = "test";
		reg = <4 0xff790000 4 0xff790004>;
	};
	
	
&test1 {
	compatible = "test1234";
	status = "okay";
};
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值