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屬性而出錯退出不能註冊成功.