blob: 15dd836509b8b9b1f94b60195c9ce64b658b107d [file] [log] [blame]
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2022 VK
4 * Author: Ivan Vozvakhov <i.vozvakhov@vk.team>
5 */
6
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +03007#include <dm.h>
8#include <errno.h>
9#include <led.h>
10#include <malloc.h>
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +030011#include <pwm.h>
12
13#define LEDS_PWM_DRIVER_NAME "led_pwm"
14
15struct led_pwm_priv {
16 struct udevice *pwm;
17 uint period; /* period in ns */
18 uint duty; /* duty cycle in ns */
19 uint channel; /* pwm channel number */
20 bool active_low; /* pwm polarity */
21 bool enabled;
22};
23
24static int led_pwm_enable(struct udevice *dev)
25{
26 struct led_pwm_priv *priv = dev_get_priv(dev);
27 int ret;
28
29 ret = pwm_set_invert(priv->pwm, priv->channel, priv->active_low);
30 if (ret)
31 return ret;
32
33 ret = pwm_set_config(priv->pwm, priv->channel, priv->period, priv->duty);
34 if (ret)
35 return ret;
36
37 ret = pwm_set_enable(priv->pwm, priv->channel, true);
38 if (ret)
39 return ret;
40
41 priv->enabled = true;
42
43 return 0;
44}
45
46static int led_pwm_disable(struct udevice *dev)
47{
48 struct led_pwm_priv *priv = dev_get_priv(dev);
49 int ret;
50
51 ret = pwm_set_config(priv->pwm, priv->channel, priv->period, 0);
52 if (ret)
53 return ret;
54
55 ret = pwm_set_enable(priv->pwm, priv->channel, false);
56 if (ret)
57 return ret;
58
59 priv->enabled = false;
60
61 return 0;
62}
63
64static int led_pwm_set_state(struct udevice *dev, enum led_state_t state)
65{
66 struct led_pwm_priv *priv = dev_get_priv(dev);
67 int ret;
68
69 switch (state) {
70 case LEDST_OFF:
71 ret = led_pwm_disable(dev);
72 break;
73 case LEDST_ON:
74 ret = led_pwm_enable(dev);
75 break;
76 case LEDST_TOGGLE:
77 ret = (priv->enabled) ? led_pwm_disable(dev) : led_pwm_enable(dev);
78 break;
79 default:
80 ret = -ENOSYS;
81 }
82
83 return ret;
84}
85
86static enum led_state_t led_pwm_get_state(struct udevice *dev)
87{
88 struct led_pwm_priv *priv = dev_get_priv(dev);
89
90 return (priv->enabled) ? LEDST_ON : LEDST_OFF;
91}
92
93static int led_pwm_probe(struct udevice *dev)
94{
95 struct led_pwm_priv *priv = dev_get_priv(dev);
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +030096
97 return led_pwm_set_state(dev, (priv->enabled) ? LEDST_ON : LEDST_OFF);
98}
99
100static int led_pwm_of_to_plat(struct udevice *dev)
101{
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300102 struct led_pwm_priv *priv = dev_get_priv(dev);
103 struct ofnode_phandle_args args;
104 uint def_brightness, max_brightness;
105 int ret;
106
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300107 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
108 if (ret)
109 return ret;
110
111 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
112 if (ret)
113 return ret;
114
115 priv->channel = args.args[0];
116 priv->period = args.args[1];
117 priv->active_low = dev_read_bool(dev, "active-low");
118
119 def_brightness = dev_read_u32_default(dev, "u-boot,default-brightness", 0);
120 max_brightness = dev_read_u32_default(dev, "max-brightness", 255);
121 priv->enabled = !!def_brightness;
122
123 /*
Nylon Chen50806322022-10-27 14:25:37 +0800124 * No need to handle pwm inverted case (active_low)
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300125 * because of pwm_set_invert function
126 */
127 if (def_brightness < max_brightness)
128 priv->duty = priv->period * def_brightness / max_brightness;
129 else
130 priv->duty = priv->period;
131
132 return 0;
133}
134
135static int led_pwm_bind(struct udevice *parent)
136{
Rasmus Villemoes073475a2023-11-17 12:38:10 +0100137 return led_bind_generic(parent, LEDS_PWM_DRIVER_NAME);
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300138}
139
140static const struct led_ops led_pwm_ops = {
141 .set_state = led_pwm_set_state,
142 .get_state = led_pwm_get_state,
143};
144
145static const struct udevice_id led_pwm_ids[] = {
146 { .compatible = "pwm-leds" },
147 { }
148};
149
150U_BOOT_DRIVER(led_pwm) = {
151 .name = LEDS_PWM_DRIVER_NAME,
152 .id = UCLASS_LED,
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300153 .ops = &led_pwm_ops,
154 .priv_auto = sizeof(struct led_pwm_priv),
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300155 .probe = led_pwm_probe,
156 .of_to_plat = led_pwm_of_to_plat,
157};
Stefan Herbrechtsmeier18abf792022-06-23 11:25:29 +0200158
159U_BOOT_DRIVER(led_pwm_wrap) = {
160 .name = LEDS_PWM_DRIVER_NAME "_wrap",
161 .id = UCLASS_NOP,
162 .of_match = led_pwm_ids,
163 .bind = led_pwm_bind,
164};