1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2022 VK
4 * Author: Ivan Vozvakhov <i.vozvakhov@vk.team>
15 #define LEDS_PWM_DRIVER_NAME "led_pwm"
19 uint period; /* period in ns */
20 uint duty; /* duty cycle in ns */
21 uint channel; /* pwm channel number */
22 bool active_low; /* pwm polarity */
26 static int led_pwm_enable(struct udevice *dev)
28 struct led_pwm_priv *priv = dev_get_priv(dev);
31 ret = pwm_set_invert(priv->pwm, priv->channel, priv->active_low);
35 ret = pwm_set_config(priv->pwm, priv->channel, priv->period, priv->duty);
39 ret = pwm_set_enable(priv->pwm, priv->channel, true);
48 static int led_pwm_disable(struct udevice *dev)
50 struct led_pwm_priv *priv = dev_get_priv(dev);
53 ret = pwm_set_config(priv->pwm, priv->channel, priv->period, 0);
57 ret = pwm_set_enable(priv->pwm, priv->channel, false);
61 priv->enabled = false;
66 static int led_pwm_set_state(struct udevice *dev, enum led_state_t state)
68 struct led_pwm_priv *priv = dev_get_priv(dev);
73 ret = led_pwm_disable(dev);
76 ret = led_pwm_enable(dev);
79 ret = (priv->enabled) ? led_pwm_disable(dev) : led_pwm_enable(dev);
88 static enum led_state_t led_pwm_get_state(struct udevice *dev)
90 struct led_pwm_priv *priv = dev_get_priv(dev);
92 return (priv->enabled) ? LEDST_ON : LEDST_OFF;
95 static int led_pwm_probe(struct udevice *dev)
97 struct led_pwm_priv *priv = dev_get_priv(dev);
98 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
100 /* Ignore the top-level LED node */
104 return led_pwm_set_state(dev, (priv->enabled) ? LEDST_ON : LEDST_OFF);
107 static int led_pwm_of_to_plat(struct udevice *dev)
109 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
110 struct led_pwm_priv *priv = dev_get_priv(dev);
111 struct ofnode_phandle_args args;
112 uint def_brightness, max_brightness;
115 /* Ignore the top-level LED node */
119 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
123 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
127 priv->channel = args.args[0];
128 priv->period = args.args[1];
129 priv->active_low = dev_read_bool(dev, "active-low");
131 def_brightness = dev_read_u32_default(dev, "u-boot,default-brightness", 0);
132 max_brightness = dev_read_u32_default(dev, "max-brightness", 255);
133 priv->enabled = !!def_brightness;
136 * No need to handle pwm iverted case (active_low)
137 * because of pwm_set_invert function
139 if (def_brightness < max_brightness)
140 priv->duty = priv->period * def_brightness / max_brightness;
142 priv->duty = priv->period;
147 static int led_pwm_bind(struct udevice *parent)
153 dev_for_each_subnode(node, parent) {
154 struct led_uc_plat *uc_plat;
157 label = ofnode_read_string(node, "label");
159 label = ofnode_get_name(node);
161 ret = device_bind_driver_to_node(parent, LEDS_PWM_DRIVER_NAME,
162 ofnode_get_name(node),
167 uc_plat = dev_get_uclass_plat(dev);
168 uc_plat->label = label;
173 static const struct led_ops led_pwm_ops = {
174 .set_state = led_pwm_set_state,
175 .get_state = led_pwm_get_state,
178 static const struct udevice_id led_pwm_ids[] = {
179 { .compatible = "pwm-leds" },
183 U_BOOT_DRIVER(led_pwm) = {
184 .name = LEDS_PWM_DRIVER_NAME,
186 .of_match = led_pwm_ids,
188 .priv_auto = sizeof(struct led_pwm_priv),
189 .bind = led_pwm_bind,
190 .probe = led_pwm_probe,
191 .of_to_plat = led_pwm_of_to_plat,