通过设备节点获取已注册的 i2c client
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 代码分析
前言
另一个驱动通过设备节点 获取已注册的i2c client
代码分析
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/module.h>
#include <linux/pinctrl/consumer.h>
#include <linux/err.h>
#include <linux/pwm.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/i2c.h>struct i2c_client *g_client = NULL;//遍历时,都会执行这个func来判断是否符合要求
static int compare_i2c_device_node(struct device *dev, void *path_node)
{//判断已经注册进adapter 中 的 I2C设备节点 是否与设备树中的节点 一致//如果一致,说明这个I2C设备已经被注册进对应的adapter中,是可以使用的if (dev->of_node == path_node) {g_client = to_i2c_client(dev);printk("%s find the node correspond device: %s , will break \n", __func__, g_client->name);return 1;} else {g_client = to_i2c_client(dev);printk("%s find the node no correspond client: %s , will break \n", __func__, g_client->name); } return 0;
}//通过I2C总线 num_id 与 设备树节点路径 查询在对应bus_num上的 client是否已经注册进对应adapter
static int m_find_i2c_client(int bus_num, const char *path)
{struct device_node *node;struct i2c_adapter *adapter;struct device *adapter_dev;int ret = 0;//通过完整路径查找I2C设备节点node = of_find_node_by_path(path);if (!node) {pr_err("Failed to find I2C device node\n");return ret;}//通过I2C 的bus_num获取I2C的适配器(一般是i2c0/1/2/3/4/5...)adapter = i2c_get_adapter(bus_num);//获取适配器的设备节点adapter_dev = &adapter->dev;if (!adapter_dev->p)return ret;//遍历adapter所有子设备ret = device_for_each_child(adapter_dev, node, &compare_i2c_device_node);if (ret) {printk("%s find the correspond client : %s \n", __func__, g_client->name);} else {printk("%s find the no correspond client : %s \n", __func__, g_client->name);}i2c_put_adapter(adapter);of_node_put(node); return ret;
}static int __init kk_dirver_init(void)
{int ret = 0;//i2c@1101a000是i2c6ret = m_find_i2c_client(6, "/soc/i2c@1101a000/kk@38");if (ret) {printk("%s emdoor get kk@38 client succeed \n", __func__);} return 0;
}module_init(kk_dirver_init);