2022-10-21 linux 使用device_for_each_child_node 遍历dts里面的所有结点node,用gpio-leds驱动分析

一、gpio-leds驱动里面的dts配置是这样

二、驱动解析dts leds结点的代码如下,使用device_for_each_child_node遍历所有的node

static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct fwnode_handle *child;
	struct gpio_leds_priv *priv;
	int count, ret;
	struct device_node *np;

	count = device_get_child_node_count(dev);
	if (!count)
		return ERR_PTR(-ENODEV);

	priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL);
	if (!priv)
		return ERR_PTR(-ENOMEM);

	//获取设备子节点的个数
	printk("child node count : %d ", device_get_child_node_count(&pdev->dev));
	//获取设备属性autorepeat的值
	printk("%d ", device_property_read_bool(&pdev->dev, "autorepeat"));

	device_for_each_child_node(dev, child) {
		struct gpio_led led = {};
		const char *state = NULL;
        printk("parse a new child node\n");
		led.gpiod = devm_get_gpiod_from_child(dev, NULL, child);
		if (IS_ERR(led.gpiod)) {
			fwnode_handle_put(child);
			ret = PTR_ERR(led.gpiod);
			goto err;
		}

		np = to_of_node(child);

		if (fwnode_property_present(child, "label")) {
			fwnode_property_read_string(child, "label", &led.name);
		} else {
			if (IS_ENABLED(CONFIG_OF) && !led.name && np)
				led.name = np->name;
			if (!led.name) {
				ret = -EINVAL;
				goto err;
			}
		}
		printk("led.name = %s \n",led.name);
		fwnode_property_read_string(child, "linux,default-trigger",
					    &led.default_trigger);
        printk("led.default_trigger = %s \n",led.default_trigger);
		if (!fwnode_property_read_string(child, "default-state",
						 &state)) {
			if (!strcmp(state, "keep"))
				led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
			else if (!strcmp(state, "on"))
				led.default_state = LEDS_GPIO_DEFSTATE_ON;
			else
				led.default_state = LEDS_GPIO_DEFSTATE_OFF;
		}
        printk("default-state = %s \n",state);
		if (fwnode_property_present(child, "retain-state-suspended"))
			led.retain_state_suspended = 1;

		ret = create_gpio_led(&led, &priv->leds[priv->num_leds],
				      dev, NULL);
		if (ret < 0) {
			fwnode_handle_put(child);
			goto err;
		}
		priv->num_leds++;
	}

	return priv;

err:
	for (count = priv->num_leds - 1; count >= 0; count--)
		delete_gpio_led(&priv->leds[count]);
	return ERR_PTR(ret);
}

 三、查看打印信息,解析dts里面的leds结点

 四、参考文章

08 在设备树里描述platform_device【转】 - 走看看 (zoukankan.com)icon-default.png?t=M85Bhttp://t.zoukankan.com/sky-heaven-p-11840017.html

猜你喜欢

转载自blog.csdn.net/qq_37858386/article/details/127449229
今日推荐