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);
99 return led_pwm_set_state(dev, (priv->enabled) ? LEDST_ON : LEDST_OFF);
102 static int led_pwm_of_to_plat(struct udevice *dev)
104 struct led_pwm_priv *priv = dev_get_priv(dev);
105 struct ofnode_phandle_args args;
106 uint def_brightness, max_brightness;
109 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
113 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
117 priv->channel = args.args[0];
118 priv->period = args.args[1];
119 priv->active_low = dev_read_bool(dev, "active-low");
121 def_brightness = dev_read_u32_default(dev, "u-boot,default-brightness", 0);
122 max_brightness = dev_read_u32_default(dev, "max-brightness", 255);
123 priv->enabled = !!def_brightness;
126 * No need to handle pwm iverted case (active_low)
127 * because of pwm_set_invert function
129 if (def_brightness < max_brightness)
130 priv->duty = priv->period * def_brightness / max_brightness;
132 priv->duty = priv->period;
137 static int led_pwm_bind(struct udevice *parent)
143 dev_for_each_subnode(node, parent) {
144 ret = device_bind_driver_to_node(parent, LEDS_PWM_DRIVER_NAME,
145 ofnode_get_name(node),
153 static const struct led_ops led_pwm_ops = {
154 .set_state = led_pwm_set_state,
155 .get_state = led_pwm_get_state,
158 static const struct udevice_id led_pwm_ids[] = {
159 { .compatible = "pwm-leds" },
163 U_BOOT_DRIVER(led_pwm) = {
164 .name = LEDS_PWM_DRIVER_NAME,
167 .priv_auto = sizeof(struct led_pwm_priv),
168 .probe = led_pwm_probe,
169 .of_to_plat = led_pwm_of_to_plat,
172 U_BOOT_DRIVER(led_pwm_wrap) = {
173 .name = LEDS_PWM_DRIVER_NAME "_wrap",
175 .of_match = led_pwm_ids,
176 .bind = led_pwm_bind,