欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 了解linux-5.4.31/drivers/gpio/gpiolib-devres.c中的devm_gpiod_get_optional()函数

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

2025/2/6 10:02:43 来源:https://blog.csdn.net/weixin_42550185/article/details/145452285  浏览:    关键词:了解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);

}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com