当前位置: 首页 > news >正文

通过设备节点获取已注册的 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);

相关文章:

  • Vue3 Composition API: 企业级应用最佳实践方案
  • 什么是电离层闪烁
  • YOLOv8损失函数代码详解(示例展示数据变换过程)
  • 模型常见损失函数、softmax、交叉熵损失函数
  • python中的numpy(数组)
  • 2025.5个人感悟
  • leetcode hot100刷题日记——16.全排列
  • 水利水电安全员B证职责
  • 信息论基础
  • OpenLayers 加载导航与基本操作控件
  • Eclipse中设置Java程序运行时的JVM参数
  • 使用Python操作Mysql数据库
  • 赋能电力FTU,飞凌嵌入式RK3506核心板AMP双系统技术应用解析
  • 解决:ERROR: No matching distribution found for matplotlib= =3.8.3
  • SQL每日一练(2)
  • 【esp32 WIFI连接】-STA模式
  • SpringBoot多环境配置文件切换
  • 高速串行差分信号仿真分析及技术发展挑战续
  • TF-IDF和词袋模型
  • Python实用工具:文件批量重命名器
  • 贸易公司做网站怎么样/b站视频推广网站2023
  • 成都新都网站开发/网上商城推广13种方法
  • 关于企业网站建设的市场比质比价调查报告/苏州百度代理公司
  • wordpress 导入文章/如何做优化排名
  • 盐城网站定制/电商大数据查询平台免费
  • 百度怎么做网站广告/西安自动seo