首页 > 解决方案 > 如何使用 devm_regulator_get 处理错误

问题描述

我正在尝试处理devm_regulator_get找不到匹配调节器的情况。我在内核 4.9.30 上编程

从 linux 内核源码中我们可以看到如下描述 [ drivers/regulator/devres.c / drivers/regulator/core.c ]

驱动程序/调节器/devres.c

/**
 * devm_regulator_get - Resource managed regulator_get()
 * @dev: device for regulator "consumer"
 * @id: Supply name or regulator ID.
 *
 * Managed regulator_get(). Regulators returned from this function are
 * automatically regulator_put() on driver detach. See regulator_get() for more
 * information.
 */
struct regulator *devm_regulator_get(struct device *dev, const char *id)
{
    return _devm_regulator_get(dev, id, NORMAL_GET);
}
EXPORT_SYMBOL_GPL(devm_regulator_get);

驱动程序/调节器/core.c

/**
 * regulator_get - lookup and obtain a reference to a regulator.
 * @dev: device for regulator "consumer"
 * @id: Supply name or regulator ID.
 *
 * Returns a struct regulator corresponding to the regulator producer,
 * or IS_ERR() condition containing errno.
 *
 * Use of supply names configured via regulator_set_device_supply() is
 * strongly encouraged.  It is recommended that the supply name used
 * should match the name used for the supply and/or the relevant
 * device pins in the datasheet.
 */
struct regulator *regulator_get(struct device *dev, const char *id)
{
    return _regulator_get(dev, id, false, true);
}
EXPORT_SYMBOL_GPL(regulator_get);

这是我的代码

struct regulator *power_regulator = devm_regulator_get(&pdev->dev, "zdfhkfgv");
if(IS_ERR(power_regulator))
{
    ERROR("Invalid power regulator\r\n");
    return -EINVAL;
}

但是,即使找不到调节器,我也不会陷入错误情况。

这是内核输出:

# insmod mod.ko 
module : Init driver
module : Device allocated and initialized
module supply zdfhkfgv not found, using dummy regulator
module : device is not powered
[...]

所以我可以看到devm_regulator_get未能找到监管机构,但我没有陷入错误情况。这很烦人,因为当调节器未设置时,我将返回。取而代之的是,代码继续在未通电的设备上运行。

我在这里做错了什么?

标签: linuxlinux-kernellinux-device-driver

解决方案


如果您不想依赖虚拟调节器,则应该使用 devm_regulator_get_optional()。然后它将返回 -ENODEV 。


推荐阅读