linux设备树读取属性函数理解纠正

1.of_property_count_u32_elems/of_property_read_u32_index(),读取属性32位数据个数/读取属性32位数据值,原型如下

/**
 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
 *
 * @np:        device node from which the property value is to be read.
 * @propname:    name of the property to be searched.
 * @index:    index of the u32 in the list of values
 * @out_value:    pointer to return value, modified only if no error.
 *
 * Search for a property in a device node and read nth 32-bit value from
 * it. Returns 0 on success, -EINVAL if the property does not exist,
 * -ENODATA if property does not have a value, and -EOVERFLOW if the
 * property data isn't large enough.
 *
 * The out_value is modified only if a valid u32 value can be decoded.
 */
int of_property_read_u32_index(const struct device_node *np,
                       const char *propname,
                       u32 index, u32 *out_value)
{
    const u32 *val = of_find_property_value_of_size(np, propname,
                    ((index + 1) * sizeof(*out_value)),
                    0,
                    NULL);

    if (IS_ERR(val))
        return PTR_ERR(val);

    *out_value = be32_to_cpup(((__be32 *)val) + index);
    return 0;
}
EXPORT_SYMBOL_GPL(of_property_read_u32_index);

在gpio-regulator.c里面有如下代码

    mmciv: gpio-regulator {
        compatible = "regulator-gpio";

        regulator-name = "mmci-gpio-supply";
        regulator-min-microvolt = <1800000>;
        regulator-max-microvolt = <2600000>;
        regulator-boot-on;

        enable-gpio = <&gpio0 23 0x4>;
        gpios = <&gpio0 24 0x4
             &gpio0 25 0x4>;
        states = <1800000 0x3
              2200000 0x2
              2600000 0x1
              2900000 0x0>;

        startup-delay-us = <100000>;
        enable-active-high;
    };

    /* Fetch states. */
    proplen = of_property_count_u32_elems(np, "states");
    if (proplen < 0) {
        dev_err(dev, "No 'states' property found\n");
        return ERR_PTR(-EINVAL);
    }

    config->states = devm_kzalloc(dev,
                sizeof(struct gpio_regulator_state)
                * (proplen / 2),
                GFP_KERNEL);
    if (!config->states)
        return ERR_PTR(-ENOMEM);

    for (i = 0; i < proplen / 2; i++) {
        of_property_read_u32_index(np, "states", i * 2,
                       &config->states[i].value);
        of_property_read_u32_index(np, "states", i * 2 + 1,
                       &config->states[i].gpios);
    }

之前错误认为proplen为4,实际为8,因为在states属性里面共有8个值,虽然成对出现,但在属性里都是32bit数据,计数为8个值而不是4个.

顺便说一下gpio-regulator设备树几个属性的理解

a.enable-gpio属性是使能regulator电压的(控制输出高低电平)

b.gpios属性是调节电压输出大小的(从regulator-min-microvolt到regulator-max-microvolt)

c.states属性是gpios属性调节电压大小的值(比如想调节电压输出1.8/2.2/2.6/2.9这四个值,则可以将这几个值写到states属性里)

d.gpios-states是描述gpios属性初始状态的(比如想要调节电压输出的初始值是高电平则将如下写法(gpios-states = <1>;),他和gpios属性个数一致,即上述;例子gpios有两个24/25,则gpios-states也必须是两个gpios-states = <1 1>;或gpios-states = <1 0>;或gpios-states = <0 1>;或gpios-states = <0 0>;)

如果只输出高低电平而不用调节范围,则可以用enable-gpio或者gpios(用gpios-states控制输出高低电平).但是如果选择了enable-gpio属性,则states属性也必须写到设备树了里,因为他是必须属性,不然gpio-regulator.c源代码会找不到states属性而出错退出不能注册成功.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值