Android TP驱动之(二)设备树解析
来源:互联网 发布:淘宝优惠券群是真的吗 编辑:程序博客网 时间:2024/04/30 16:47
AndroidTP驱动之(二)设备树解析
0. 前情提要
上文中我们已经初步搭好了TP驱动的框架。当然我们还缺了重要的部分input子系统,这个不急,我们下一篇文章再叙。
本文我们来完成TP driver中对设备树的解析。
1. probe函数
我们知道驱动匹配有四种方式:设备树匹配、设备名称匹配、设备ID匹配、ACPI方式匹配。
这里我们只用设备树匹配,这也是Android驱动中最常用的匹配方式。
我们知道当驱动和设备树匹配的时候,probe函数就会被执行。
这时一般首先进行设备树的解析,从设备树中获取设备的硬件信息。
struct mytp_platform_data *pdata; int err; if (client->dev.of_node) { pdata = devm_kzalloc(&client->dev, sizeof(struct mytp_platform_data), GFP_KERNEL); if (!pdata) { PRINT_INFO("[MEMORY]Failed to allocate memory"); return -ENOMEM; } err = mytp_parse_dt(&client->dev, pdata); if (err) { PRINT_INFO("[DTS]DT parsing failed"); } } else { return -1; } if (!pdata) { PRINT_INFO("Invalid pdata"); return -EINVAL; }
从代码中我们很清晰的看到,我们先是定义了一个结构体指针pdata。这是用来存放平台数据。
接着,我们为这结构体申请了内存,并做异常处理。
之后调用mytp_parse_dt函数对设备树进行解析。也做了相应的异常处理。
1.1 mytp_platform_data结构体
struct mytp_platform_data{ u32 irq_gpio; u32 irq_gpio_flags; u32 reset_gpio; u32 reset_gpio_flags; bool have_key; u32 key_number; u32 keys[4]; u32 key_y_coord; u32 key_x_coords[4]; u32 x_max; u32 y_max; u32 x_min; u32 y_min; u32 max_touch_number;};
头四个结构体成员变量,分别是两个gpio所使用的,一个是irq中断,一个是TP的reset复位引脚。
之后的bool变量,是TP按键的标志,表示是否支持TP按键。
紧接着的四个key_打头的变量是提供给TP按键处理函数用的。
x_max、x_min、y_max、y_min限定了tp的触摸范围。
最后的一个变量限定了tp支持的最大多点触控数目。
1.2 mytp_parse_dt函数
这个函数就是用来解析设备树的。
其中的mytp_get_dt_coords函数是自己实现的,用来解析设备树中的数组的。具体实现见1.3小节。
static int mytp_parse_dt(struct device *dev, struct mytp_platform_data *pdata){ int rc; struct device_node *np = dev->of_node; u32 temp_val; rc = mytp_get_dt_coords(dev, "focaltech,display-coords", pdata); if (rc) PRINT_INFO("Unable to get display-coords"); /* key */ pdata->have_key = of_property_read_bool(np, "focaltech,have-key"); if (pdata->have_key) { rc = of_property_read_u32(np, "focaltech,key-number", &pdata->key_number); if (rc) { PRINT_INFO("Key number undefined!"); } rc = of_property_read_u32_array(np, "focaltech,keys", pdata->keys, pdata->key_number); if (rc) { PRINT_INFO("Keys undefined!"); } rc = of_property_read_u32(np, "focaltech,key-y-coord", &pdata->key_y_coord); if (rc) { PRINT_INFO("Key Y Coord undefined!"); } rc = of_property_read_u32_array(np, "focaltech,key-x-coords", pdata->key_x_coords, pdata->key_number); if (rc) { PRINT_INFO("Key X Coords undefined!"); } PRINT_INFO("%d: (%d, %d, %d), [%d, %d, %d][%d]", pdata->key_number, pdata->keys[0], pdata->keys[1], pdata->keys[2], pdata->key_x_coords[0], pdata->key_x_coords[1], pdata->key_x_coords[2], pdata->key_y_coord); } /* reset, irq gpio info */ pdata->reset_gpio = of_get_named_gpio_flags(np, "focaltech,reset-gpio", 0, &pdata->reset_gpio_flags); if (pdata->reset_gpio < 0) { PRINT_INFO("Unable to get reset_gpio"); } pdata->irq_gpio = of_get_named_gpio_flags(np, "focaltech,irq-gpio", 0, &pdata->irq_gpio_flags); if (pdata->irq_gpio < 0) { PRINT_INFO("Unable to get irq_gpio"); } rc = of_property_read_u32(np, "focaltech,max-touch-number", &temp_val); if (!rc) { pdata->max_touch_number = temp_val; PRINT_INFO("max_touch_number=%d", pdata->max_touch_number); } else { PRINT_INFO("Unable to get max-touch-number"); pdata->max_touch_number = MYTP_MAX_POINTS; } return 0;}
1.3 mytp_get_dt_coords函数
static int mytp_get_dt_coords(struct device *dev, char *name, struct mytp_platform_data *pdata){ u32 coords[MYTP_COORDS_ARR_SIZE]; struct property *prop; struct device_node *np = dev->of_node; int coords_size, rc; prop = of_find_property(np, name, NULL); if (!prop) return -EINVAL; if (!prop->value) return -ENODATA; coords_size = prop->length / sizeof(u32); if (coords_size != MYTP_COORDS_ARR_SIZE) { PRINT_INFO("invalid %s", name); return -EINVAL; } rc = of_property_read_u32_array(np, name, coords, coords_size); if (rc && (rc != -EINVAL)) { PRINT_INFO("Unable to read %s", name); return rc; } if (!strcmp(name, "focaltech,display-coords")) { pdata->x_min = coords[0]; pdata->y_min = coords[1]; pdata->x_max = coords[2]; pdata->y_max = coords[3]; } else { PRINT_INFO("unsupported property %s", name); return -EINVAL; } return 0;}
End
至此我们完成了对设备树的解析。
阅读全文
1 0
- Android TP驱动之(二)设备树解析
- Android TP 驱动之(一)I2C
- Android TP驱动之(三)input
- Android 块设备驱动之二
- Android TP(二)
- linux设备驱动(二)---字符设备之按键驱动
- 设备驱动之二----字符设备驱动
- 过 DNF TP 驱动保护(二)
- 过 DNF TP 驱动保护(二)
- 过 DNF TP 驱动保护(二)
- 过 DNF TP 驱动保护(二)
- 过 DNF TP 驱动保护(二)
- Linux设备驱动之字符设备(二)
- linux设备驱动篇之LED驱动(二)
- BACnet(二)MS/TP AutoMAC解析
- 块设备驱动之二
- 设备模型之二 总线设备驱动
- Linux 设备驱动之字符设备(二)
- ZJOI2005独木舟上的旅行
- c++用户界面编程
- Cmake如何编译Qt程序
- Spring Bean的作用域
- 在浏览器中输入www.baidu.com后执行的全部过程
- Android TP驱动之(二)设备树解析
- 概念笔记
- linux centos下 手动编译安装memcache 扩展
- NDK程序开发流程
- 【项目记录】山东大学场馆管理系统之——器材管理
- Python图像处理库(1)
- 【项目记录】山东大学场馆管理系统之——财务管理
- 强题!dft!
- 铺砖问题(状态压缩DP)