1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
7 #define LOG_CATEGORY UCLASS_PANEL_BACKLIGHT
11 #include <backlight.h>
16 #include <linux/delay.h>
17 #include <linux/math64.h>
18 #include <power/regulator.h>
21 * Private information for the PWM backlight
23 * If @num_levels is 0 then the levels are simple values with the backlight
24 * value going between the minimum (default 0) and the maximum (default 255).
25 * Otherwise the levels are an index into @levels (0..n-1).
27 * @reg: Regulator to enable to turn the backlight on (NULL if none)
28 * @enable, GPIO to set to enable the backlight (can be missing)
29 * @pwm: PWM to use to change the backlight brightness
30 * @channel: PWM channel to use
31 * @period_ns: Period of the backlight in nanoseconds
32 * @levels: Levels for the backlight, or NULL if not using indexed levels
33 * @num_levels: Number of levels
34 * @cur_level: Current level for the backlight (index or value)
35 * @default_level: Default level for the backlight (index or value)
36 * @min_level: Minimum level of the backlight (full off)
37 * @max_level: Maximum level of the backlight (full on)
38 * @enabled: true if backlight is enabled
40 struct pwm_backlight_priv {
42 struct gpio_desc enable;
47 * the polarity of one PWM
49 * 1: inverted polarity
61 static int set_pwm(struct pwm_backlight_priv *priv)
67 if (priv->period_ns) {
68 width = priv->period_ns * (priv->cur_level - priv->min_level);
69 duty_cycle = div_u64(width,
70 (priv->max_level - priv->min_level));
71 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
74 /* PWM driver will internally scale these like the above. */
75 ret = pwm_set_config(priv->pwm, priv->channel,
76 priv->max_level - priv->min_level,
77 priv->cur_level - priv->min_level);
82 ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity);
83 if (ret == -ENOSYS && !priv->polarity)
89 static int enable_sequence(struct udevice *dev, int seq)
91 struct pwm_backlight_priv *priv = dev_get_priv(dev);
97 __maybe_unused struct dm_regulator_uclass_plat
100 plat = dev_get_uclass_plat(priv->reg);
101 log_debug("Enable '%s', regulator '%s'/'%s'\n",
102 dev->name, priv->reg->name, plat->name);
103 ret = regulator_set_enable(priv->reg, true);
105 log_debug("Cannot enable regulator for PWM '%s'\n",
114 dm_gpio_set_value(&priv->enable, 1);
121 static int pwm_backlight_enable(struct udevice *dev)
123 struct pwm_backlight_priv *priv = dev_get_priv(dev);
126 ret = enable_sequence(dev, 0);
132 ret = pwm_set_enable(priv->pwm, priv->channel, true);
135 ret = enable_sequence(dev, 1);
138 priv->enabled = true;
143 static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
145 struct pwm_backlight_priv *priv = dev_get_priv(dev);
146 bool disable = false;
150 if (!priv->enabled) {
151 ret = enable_sequence(dev, 0);
155 if (percent == BACKLIGHT_OFF) {
159 if (percent == BACKLIGHT_DEFAULT) {
160 level = priv->default_level;
163 level = priv->levels[percent * (priv->num_levels - 1)
166 level = priv->min_level +
167 (priv->max_level - priv->min_level) *
171 priv->cur_level = level;
176 if (!priv->enabled) {
177 ret = enable_sequence(dev, 1);
180 priv->enabled = true;
183 dm_gpio_set_value(&priv->enable, 0);
185 ret = regulator_set_enable(priv->reg, false);
189 priv->enabled = false;
195 static int pwm_backlight_of_to_plat(struct udevice *dev)
197 struct pwm_backlight_priv *priv = dev_get_priv(dev);
198 struct ofnode_phandle_args args;
199 int index, ret, count, len;
202 log_debug("start\n");
203 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
204 "power-supply", &priv->reg);
206 log_debug("Cannot get power supply: ret=%d\n", ret);
207 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
210 log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
214 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
217 log_debug("Cannot get PWM phandle: ret=%d\n", ret);
221 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
223 log_debug("Cannot get PWM: ret=%d\n", ret);
226 if (args.args_count < 1)
227 return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
228 priv->channel = args.args[0];
229 if (args.args_count > 1)
230 priv->period_ns = args.args[1];
231 if (args.args_count > 2)
232 priv->polarity = args.args[2];
234 index = dev_read_u32_default(dev, "default-brightness-level", 255);
235 cell = dev_read_prop(dev, "brightness-levels", &len);
236 count = len / sizeof(u32);
237 if (cell && count > index) {
238 priv->levels = malloc(len);
240 return log_ret(-ENOMEM);
241 ret = dev_read_u32_array(dev, "brightness-levels", priv->levels,
244 return log_msg_ret("levels", ret);
245 priv->num_levels = count;
246 priv->default_level = priv->levels[index];
247 priv->max_level = priv->levels[count - 1];
249 priv->default_level = index;
250 priv->max_level = 255;
252 priv->cur_level = priv->default_level;
259 static int pwm_backlight_probe(struct udevice *dev)
264 static const struct backlight_ops pwm_backlight_ops = {
265 .enable = pwm_backlight_enable,
266 .set_brightness = pwm_backlight_set_brightness,
269 static const struct udevice_id pwm_backlight_ids[] = {
270 { .compatible = "pwm-backlight" },
274 U_BOOT_DRIVER(pwm_backlight) = {
275 .name = "pwm_backlight",
276 .id = UCLASS_PANEL_BACKLIGHT,
277 .of_match = pwm_backlight_ids,
278 .ops = &pwm_backlight_ops,
279 .of_to_plat = pwm_backlight_of_to_plat,
280 .probe = pwm_backlight_probe,
281 .priv_auto = sizeof(struct pwm_backlight_priv),