blob: ae6de3087ab7754a22949decdb2488be670e1096 [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
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <led.h>
11#include <malloc.h>
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +030012#include <pwm.h>
13
14#define LEDS_PWM_DRIVER_NAME "led_pwm"
15
16struct led_pwm_priv {
17 struct udevice *pwm;
18 uint period; /* period in ns */
19 uint duty; /* duty cycle in ns */
20 uint channel; /* pwm channel number */
21 bool active_low; /* pwm polarity */
22 bool enabled;
23};
24
25static int led_pwm_enable(struct udevice *dev)
26{
27 struct led_pwm_priv *priv = dev_get_priv(dev);
28 int ret;
29
30 ret = pwm_set_invert(priv->pwm, priv->channel, priv->active_low);
31 if (ret)
32 return ret;
33
34 ret = pwm_set_config(priv->pwm, priv->channel, priv->period, priv->duty);
35 if (ret)
36 return ret;
37
38 ret = pwm_set_enable(priv->pwm, priv->channel, true);
39 if (ret)
40 return ret;
41
42 priv->enabled = true;
43
44 return 0;
45}
46
47static int led_pwm_disable(struct udevice *dev)
48{
49 struct led_pwm_priv *priv = dev_get_priv(dev);
50 int ret;
51
52 ret = pwm_set_config(priv->pwm, priv->channel, priv->period, 0);
53 if (ret)
54 return ret;
55
56 ret = pwm_set_enable(priv->pwm, priv->channel, false);
57 if (ret)
58 return ret;
59
60 priv->enabled = false;
61
62 return 0;
63}
64
65static int led_pwm_set_state(struct udevice *dev, enum led_state_t state)
66{
67 struct led_pwm_priv *priv = dev_get_priv(dev);
68 int ret;
69
70 switch (state) {
71 case LEDST_OFF:
72 ret = led_pwm_disable(dev);
73 break;
74 case LEDST_ON:
75 ret = led_pwm_enable(dev);
76 break;
77 case LEDST_TOGGLE:
78 ret = (priv->enabled) ? led_pwm_disable(dev) : led_pwm_enable(dev);
79 break;
80 default:
81 ret = -ENOSYS;
82 }
83
84 return ret;
85}
86
87static enum led_state_t led_pwm_get_state(struct udevice *dev)
88{
89 struct led_pwm_priv *priv = dev_get_priv(dev);
90
91 return (priv->enabled) ? LEDST_ON : LEDST_OFF;
92}
93
94static int led_pwm_probe(struct udevice *dev)
95{
96 struct led_pwm_priv *priv = dev_get_priv(dev);
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +030097
98 return led_pwm_set_state(dev, (priv->enabled) ? LEDST_ON : LEDST_OFF);
99}
100
101static int led_pwm_of_to_plat(struct udevice *dev)
102{
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300103 struct led_pwm_priv *priv = dev_get_priv(dev);
104 struct ofnode_phandle_args args;
105 uint def_brightness, max_brightness;
106 int ret;
107
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300108 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args);
109 if (ret)
110 return ret;
111
112 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
113 if (ret)
114 return ret;
115
116 priv->channel = args.args[0];
117 priv->period = args.args[1];
118 priv->active_low = dev_read_bool(dev, "active-low");
119
120 def_brightness = dev_read_u32_default(dev, "u-boot,default-brightness", 0);
121 max_brightness = dev_read_u32_default(dev, "max-brightness", 255);
122 priv->enabled = !!def_brightness;
123
124 /*
Nylon Chen50806322022-10-27 14:25:37 +0800125 * No need to handle pwm inverted case (active_low)
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300126 * because of pwm_set_invert function
127 */
128 if (def_brightness < max_brightness)
129 priv->duty = priv->period * def_brightness / max_brightness;
130 else
131 priv->duty = priv->period;
132
133 return 0;
134}
135
136static int led_pwm_bind(struct udevice *parent)
137{
Rasmus Villemoes073475a2023-11-17 12:38:10 +0100138 return led_bind_generic(parent, LEDS_PWM_DRIVER_NAME);
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300139}
140
141static const struct led_ops led_pwm_ops = {
142 .set_state = led_pwm_set_state,
143 .get_state = led_pwm_get_state,
144};
145
146static const struct udevice_id led_pwm_ids[] = {
147 { .compatible = "pwm-leds" },
148 { }
149};
150
151U_BOOT_DRIVER(led_pwm) = {
152 .name = LEDS_PWM_DRIVER_NAME,
153 .id = UCLASS_LED,
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300154 .ops = &led_pwm_ops,
155 .priv_auto = sizeof(struct led_pwm_priv),
Ivan Vozvakhov78a18f62022-03-12 13:03:14 +0300156 .probe = led_pwm_probe,
157 .of_to_plat = led_pwm_of_to_plat,
158};
Stefan Herbrechtsmeier18abf792022-06-23 11:25:29 +0200159
160U_BOOT_DRIVER(led_pwm_wrap) = {
161 .name = LEDS_PWM_DRIVER_NAME "_wrap",
162 .id = UCLASS_NOP,
163 .of_match = led_pwm_ids,
164 .bind = led_pwm_bind,
165};