驱动中常用的获取devices tree属性的api

例如dts文件中设置如下:

	qcom,sw-jeita-enable;
	qcom,usb-icl-ua = <2500000>;
	qcom,thermal-mitigation
				= <1800000 1600000 1400000 1200000 1000000 1000000 1000000>;

1.获取是否存在该属性:

chg->sw_jeita_enabled = of_property_read_bool(node,"qcom,sw-jeita-enable");

2.获取属性的值

	rc = of_property_read_u32(node,"qcom,usb-icl-ua", &chip->dt.usb_icl_ua);
	if (rc < 0)
		chip->dt.usb_icl_ua = -EINVAL;		

获取属性数组的值

    //获取该数组的长度存放到byte_len中并且分配相应内存
	if (of_find_property(node, "qcom,thermal-mitigation", &byte_len)) {
		chg->thermal_mitigation = devm_kzalloc(chg->dev, byte_len,
			GFP_KERNEL);
        //内存不足
		if (chg->thermal_mitigation == NULL)
			return -ENOMEM;

        //属性是U32的,总字节数/sizeof(u32)就是数组元素个数
		chg->thermal_levels = byte_len / sizeof(u32);
		
		rc = of_property_read_u32_array(node,
				"qcom,thermal-mitigation",
				chg->thermal_mitigation,
				chg->thermal_levels);
		if (rc < 0) {
			dev_err(chg->dev,
				"Couldn't read threm limits rc = %d\n", rc);
			return rc;
		}
	}

你可能感兴趣的:(嵌入式Linux)