设备树下的platform总线-21
Posted 杨斌并
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设备树下的platform总线-21相关的知识,希望对你有一定的参考价值。
设备树下的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";
};
以上是关于设备树下的platform总线-21的主要内容,如果未能解决你的问题,请参考以下文章
Linux——Linux驱动之设备树下platform总线驱动编写实战(手把手教你设备树下platform总线利用GPIO控制蜂鸣器完整实现过程)
Linux——Linux驱动之玩转I2C(中)自己实现一个I2C总线client设备(Linux下I2C驱动框架非设备树下的client设备树下添加I2C设备节点)