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

了解linux-5.4.31/drivers/gpio/gpiolib-devres.c中的devm_gpiod_get_optional()函数

1、打开“drivers/gpio/gpiolib-devres.c

/**

获取GPIO线的索引,查找“设备资源”,分配设备资源数据”,注册“设备资源”;

 * devm_gpiod_get_optional - Resource-managed gpiod_get_optional()

 * @dev: GPIO consumer

 * @con_id: function within the GPIO consumer

 * @flags: optional GPIO initialization flags

 *

 * Managed gpiod_get_optional(). GPIO descriptors returned from this function

 * are automatically disposed on driver detach. See gpiod_get_optional() for

 * detailed information about behavior and return values.

 */

//sii902x->reset_gpio = devm_gpiod_get_optional(dev, "reset",GPIOD_OUT_LOW);

struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,

       const char *con_id,

       enum gpiod_flags flags)

{

return devm_gpiod_get_index_optional(dev, con_id, 0, flags);

}

/**

获取GPIO线的索引,查找“设备资源”,分配设备资源数据”,注册“设备资源”;

 * devm_gpiod_get_index_optional - Resource-managed gpiod_get_index_optional()

 * @dev: GPIO consumer

 * @con_id: function within the GPIO consumer

 * @index: index of the GPIO to obtain in the consumer

 * @flags: optional GPIO initialization flags

 *

 * Managed gpiod_get_index_optional(). GPIO descriptors returned from this

 * function are automatically disposed on driver detach. See

 * gpiod_get_index_optional() for detailed information about behavior and

 * return values.

 */

struct gpio_desc *__must_check devm_gpiod_get_index_optional(struct device *dev,

     const char *con_id,

     unsigned int index,

     enum gpiod_flags flags)

{

struct gpio_desc *desc;

desc = devm_gpiod_get_index(dev, con_id, index, flags);

//获取GPIO线的索引,查找“设备资源”,分配设备资源数据”,注册“设备资源”;

if (IS_ERR(desc)) {

if (PTR_ERR(desc) == -ENOENT)

return NULL;

}

return desc;

}

/**

获取GPIO线的索引,查找“设备资源”,分配设备资源数据”,注册“设备资源”;

 * devm_gpiod_get_index - Resource-managed gpiod_get_index()

 * @dev: GPIO consumer

 * @con_id: function within the GPIO consumer

 * @idx: index of the GPIO to obtain in the consumer

 * @flags: optional GPIO initialization flags

 *

 * Managed gpiod_get_index(). GPIO descriptors returned from this function are

 * automatically disposed on driver detach. See gpiod_get_index() for detailed

 * information about behavior and return values.

 */

struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,

    const char *con_id,

    unsigned int idx,

    enum gpiod_flags flags)

{

struct gpio_desc **dr;

struct gpio_desc *desc;

desc = gpiod_get_index(dev, con_id, idx, flags);//用于获取GPIO线的索引

if (IS_ERR(desc))

return desc;

/*

 * For non-exclusive GPIO descriptors, check if this descriptor is

 * already under resource management by this device.

 */

if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {

struct devres *dres;

dres = devres_find(dev, devm_gpiod_release,devm_gpiod_match, &desc);

//查找设备资源

if (dres) return desc;

}

dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *),GFP_KERNEL);

//分配设备资源数据

if (!dr) {

gpiod_put(desc);

//释放一个已经申请的GPIO资源,释放后该GPIO可以被其他设备或应用程序使用;

return ERR_PTR(-ENOMEM);

}

*dr = desc;

devres_add(dev, dr);//注册设备资源

return desc;

}

/**

 * devres_find - 查找设备资源Find device resource

 * @dev: Device to lookup resource from

 * @release: Look for resources associated with this release function

 * @match: Match function (optional)

 * @match_data: Data for the match function

 *

 * Find the latest devres of @dev which is associated with @release

 * and for which @match returns 1.  If @match is NULL, it's considered

 * to match all.

 *

 * RETURNS:

 * Pointer to found devres, NULL if not found.

 */

void * devres_find(struct device *dev, dr_release_t release,

   dr_match_t match, void *match_data)

{

struct devres *dr;

unsigned long flags;

spin_lock_irqsave(&dev->devres_lock, flags);

dr = find_dr(dev, release, match, match_data);

spin_unlock_irqrestore(&dev->devres_lock, flags);

if (dr)

return dr->data;

return NULL;

}

2、打开“drivers/gpio/gpiolib.c”

/**

 * gpiod_put - 处理一个GPIO描述符,dispose of a GPIO descriptor

 * @desc: GPIO descriptor to dispose of

 *

 * No descriptor can be used after gpiod_put() has been called on it.

 */

void gpiod_put(struct gpio_desc *desc)

{

if (desc)

gpiod_free(desc);

//用于释放一个已经申请的GPIO资源,释放后该GPIO可以被其他设备或应用程序使用;

}

3、打开“include/linux/device.h”

#define devres_alloc(release, size, gfp) \

__devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)

4、打开“drivers/base/devres.c”

/**

 * devres_alloc - 分配设备资源数据Allocate device resource data

 * @release: Release function devres will be associated with

 * @size: Allocation size

 * @gfp: Allocation flags

 * @nid: NUMA node

 *

 * Allocate devres of @size bytes.  The allocated area is zeroed, then

 * associated with @release.  The returned pointer can be passed to

 * other devres_*() functions.

 *

 * RETURNS:

 * Pointer to allocated devres on success, NULL on failure.

 */

void * devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp, int nid)

{

struct devres *dr;

dr = alloc_dr(release, size, gfp | __GFP_ZERO, nid);

if (unlikely(!dr))

return NULL;

return dr->data;

}

static __always_inline struct devres * alloc_dr(dr_release_t release,

size_t size, gfp_t gfp, int nid)

{

size_t tot_size;

struct devres *dr;

/* We must catch any near-SIZE_MAX cases that could overflow. */

if (unlikely(check_add_overflow(sizeof(struct devres), size,

&tot_size)))

return NULL;

dr = kmalloc_node_track_caller(tot_size, gfp, nid);

if (unlikely(!dr))

return NULL;

memset(dr, 0, offsetof(struct devres, data));

INIT_LIST_HEAD(&dr->node.entry);

dr->node.release = release;

return dr;

}

/**

 * devres_add - 注册设备资源,Register device resource

 * @dev: Device to add resource to

 * @res: Resource to register

 *

 * Register devres @res to @dev.  @res should have been allocated

 * using devres_alloc().  On driver detach, the associated release

 * function will be invoked and devres will be freed automatically.

 */

void devres_add(struct device *dev, void *res)

{

struct devres *dr = container_of(res, struct devres, data);

unsigned long flags;

spin_lock_irqsave(&dev->devres_lock, flags);

add_dr(dev, &dr->node);

spin_unlock_irqrestore(&dev->devres_lock, flags);

}


http://www.kler.cn/a/534599.html

相关文章:

  • axios如何利用promise无痛刷新token
  • [数据结构] 线性表和顺序表
  • 基于Springboot+vue的租车网站系统
  • 视频融合平台EasyCVR无人机场景视频压缩及录像方案
  • Java进阶笔记(中级)
  • 如何自定义软件安装路径及Scoop包管理器使用全攻略
  • MD5 简介 以及 C# 和 js 实现【加密知多少系列_1】
  • 新版AndroidStudio 修改 jdk版本
  • 8. k8s二进制集群之Kubectl部署
  • http状态码:504 Gateway Timeout(网关超时)的原有以及排查问题的思路
  • 【Uniapp-Vue3】创建DB schema数据表结构
  • VMware下Linux和macOS遇到的一些问题总结
  • android 自定义通话录音
  • 【PostgreSQL内核学习 —— (WindowAgg(二))】
  • Docker 国内最新可用镜像源20250205
  • Solidity08 Solidity 函数
  • 机器学习8-卷积和卷积核1
  • 【AI 语音】实时语音交互优化全解析:从 RTC 技术到双讲处理
  • Java常见的技术场景面试题
  • python-leetcode-岛屿数量
  • 设备通过国标GB28181接入EasyCVR,显示在线但视频无法播放的原因排查
  • Racecar Gym
  • 【B站保姆级视频教程:Jetson配置YOLOv11环境(七)Ultralytics YOLOv11配置】
  • .NET 中实现生产者-消费者模型,BlockingCollection<T> 和 Channel<T>使用示例
  • 大模型Dense、MoE 与 Hybrid-MoE 架构的比较
  • 从java角度对比nodejs、fastapi,同步和异步区别