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 <power/regulator.h>
19 * Private information for the PWM backlight
21 * If @num_levels is 0 then the levels are simple values with the backlight
22 * value going between the minimum (default 0) and the maximum (default 255).
23 * Otherwise the levels are an index into @levels (0..n-1).
25 * @reg: Regulator to enable to turn the backlight on (NULL if none)
26 * @enable, GPIO to set to enable the backlight (can be missing)
27 * @pwm: PWM to use to change the backlight brightness
28 * @channel: PWM channel to use
29 * @period_ns: Period of the backlight in nanoseconds
30 * @levels: Levels for the backlight, or NULL if not using indexed levels
31 * @num_levels: Number of levels
32 * @cur_level: Current level for the backlight (index or value)
33 * @default_level: Default level for the backlight (index or value)
34 * @min_level: Minimum level of the backlight (full off)
35 * @min_level: Maximum level of the backlight (full on)
36 * @enabled: true if backlight is enabled
38 struct pwm_backlight_priv {
40 struct gpio_desc enable;
45 * the polarity of one PWM
47 * 1: inverted polarity
59 static int set_pwm(struct pwm_backlight_priv *priv)
64 duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) /
65 (priv->max_level - priv->min_level + 1);
66 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
71 ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity);
72 if (ret == -ENOSYS && !priv->polarity)
78 static int enable_sequence(struct udevice *dev, int seq)
80 struct pwm_backlight_priv *priv = dev_get_priv(dev);
86 __maybe_unused struct dm_regulator_uclass_platdata
89 plat = dev_get_uclass_platdata(priv->reg);
90 log_debug("Enable '%s', regulator '%s'/'%s'\n",
91 dev->name, priv->reg->name, plat->name);
92 ret = regulator_set_enable(priv->reg, true);
94 log_debug("Cannot enable regulator for PWM '%s'\n",
103 dm_gpio_set_value(&priv->enable, 1);
110 static int pwm_backlight_enable(struct udevice *dev)
112 struct pwm_backlight_priv *priv = dev_get_priv(dev);
115 ret = enable_sequence(dev, 0);
121 ret = pwm_set_enable(priv->pwm, priv->channel, true);
124 ret = enable_sequence(dev, 1);
127 priv->enabled = true;
132 static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
134 struct pwm_backlight_priv *priv = dev_get_priv(dev);
135 bool disable = false;
139 if (!priv->enabled) {
140 ret = enable_sequence(dev, 0);
144 if (percent == BACKLIGHT_OFF) {
148 if (percent == BACKLIGHT_DEFAULT) {
149 level = priv->default_level;
152 level = priv->levels[percent * (priv->num_levels - 1)
155 level = priv->min_level +
156 (priv->max_level - priv->min_level) *
160 priv->cur_level = level;
165 if (!priv->enabled) {
166 ret = enable_sequence(dev, 1);
169 priv->enabled = true;
172 dm_gpio_set_value(&priv->enable, 0);
174 ret = regulator_set_enable(priv->reg, false);
178 priv->enabled = false;
184 static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
186 struct pwm_backlight_priv *priv = dev_get_priv(dev);
187 struct ofnode_phandle_args args;
188 int index, ret, count, len;
191 log_debug("start\n");
192 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
193 "power-supply", &priv->reg);
195 log_debug("Cannot get power supply: ret=%d\n", ret);
196 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
199 log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
203 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
206 log_debug("Cannot get PWM phandle: ret=%d\n", ret);
210 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
212 log_debug("Cannot get PWM: ret=%d\n", ret);
215 if (args.args_count < 2)
216 return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
217 priv->channel = args.args[0];
218 priv->period_ns = args.args[1];
219 if (args.args_count > 2)
220 priv->polarity = args.args[2];
222 index = dev_read_u32_default(dev, "default-brightness-level", 255);
223 cell = dev_read_prop(dev, "brightness-levels", &len);
224 count = len / sizeof(u32);
225 if (cell && count > index) {
226 priv->levels = malloc(len);
228 return log_ret(-ENOMEM);
229 dev_read_u32_array(dev, "brightness-levels", priv->levels,
231 priv->num_levels = count;
232 priv->default_level = priv->levels[index];
233 priv->max_level = priv->levels[count - 1];
235 priv->default_level = index;
236 priv->max_level = 255;
238 priv->cur_level = priv->default_level;
245 static int pwm_backlight_probe(struct udevice *dev)
250 static const struct backlight_ops pwm_backlight_ops = {
251 .enable = pwm_backlight_enable,
252 .set_brightness = pwm_backlight_set_brightness,
255 static const struct udevice_id pwm_backlight_ids[] = {
256 { .compatible = "pwm-backlight" },
260 U_BOOT_DRIVER(pwm_backlight) = {
261 .name = "pwm_backlight",
262 .id = UCLASS_PANEL_BACKLIGHT,
263 .of_match = pwm_backlight_ids,
264 .ops = &pwm_backlight_ops,
265 .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata,
266 .probe = pwm_backlight_probe,
267 .priv_auto_alloc_size = sizeof(struct pwm_backlight_priv),