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 <power/regulator.h>
20 * Private information for the PWM backlight
22 * If @num_levels is 0 then the levels are simple values with the backlight
23 * value going between the minimum (default 0) and the maximum (default 255).
24 * Otherwise the levels are an index into @levels (0..n-1).
26 * @reg: Regulator to enable to turn the backlight on (NULL if none)
27 * @enable, GPIO to set to enable the backlight (can be missing)
28 * @pwm: PWM to use to change the backlight brightness
29 * @channel: PWM channel to use
30 * @period_ns: Period of the backlight in nanoseconds
31 * @levels: Levels for the backlight, or NULL if not using indexed levels
32 * @num_levels: Number of levels
33 * @cur_level: Current level for the backlight (index or value)
34 * @default_level: Default level for the backlight (index or value)
35 * @min_level: Minimum level of the backlight (full off)
36 * @max_level: Maximum level of the backlight (full on)
37 * @enabled: true if backlight is enabled
39 struct pwm_backlight_priv {
41 struct gpio_desc enable;
46 * the polarity of one PWM
48 * 1: inverted polarity
60 static int set_pwm(struct pwm_backlight_priv *priv)
65 duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) /
66 (priv->max_level - priv->min_level);
67 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
72 ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity);
73 if (ret == -ENOSYS && !priv->polarity)
79 static int enable_sequence(struct udevice *dev, int seq)
81 struct pwm_backlight_priv *priv = dev_get_priv(dev);
87 __maybe_unused struct dm_regulator_uclass_platdata
90 plat = dev_get_uclass_platdata(priv->reg);
91 log_debug("Enable '%s', regulator '%s'/'%s'\n",
92 dev->name, priv->reg->name, plat->name);
93 ret = regulator_set_enable(priv->reg, true);
95 log_debug("Cannot enable regulator for PWM '%s'\n",
104 dm_gpio_set_value(&priv->enable, 1);
111 static int pwm_backlight_enable(struct udevice *dev)
113 struct pwm_backlight_priv *priv = dev_get_priv(dev);
116 ret = enable_sequence(dev, 0);
122 ret = pwm_set_enable(priv->pwm, priv->channel, true);
125 ret = enable_sequence(dev, 1);
128 priv->enabled = true;
133 static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
135 struct pwm_backlight_priv *priv = dev_get_priv(dev);
136 bool disable = false;
140 if (!priv->enabled) {
141 ret = enable_sequence(dev, 0);
145 if (percent == BACKLIGHT_OFF) {
149 if (percent == BACKLIGHT_DEFAULT) {
150 level = priv->default_level;
153 level = priv->levels[percent * (priv->num_levels - 1)
156 level = priv->min_level +
157 (priv->max_level - priv->min_level) *
161 priv->cur_level = level;
166 if (!priv->enabled) {
167 ret = enable_sequence(dev, 1);
170 priv->enabled = true;
173 dm_gpio_set_value(&priv->enable, 0);
175 ret = regulator_set_enable(priv->reg, false);
179 priv->enabled = false;
185 static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
187 struct pwm_backlight_priv *priv = dev_get_priv(dev);
188 struct ofnode_phandle_args args;
189 int index, ret, count, len;
192 log_debug("start\n");
193 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
194 "power-supply", &priv->reg);
196 log_debug("Cannot get power supply: ret=%d\n", ret);
197 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
200 log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
204 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
207 log_debug("Cannot get PWM phandle: ret=%d\n", ret);
211 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
213 log_debug("Cannot get PWM: ret=%d\n", ret);
216 if (args.args_count < 2)
217 return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
218 priv->channel = args.args[0];
219 priv->period_ns = args.args[1];
220 if (args.args_count > 2)
221 priv->polarity = args.args[2];
223 index = dev_read_u32_default(dev, "default-brightness-level", 255);
224 cell = dev_read_prop(dev, "brightness-levels", &len);
225 count = len / sizeof(u32);
226 if (cell && count > index) {
227 priv->levels = malloc(len);
229 return log_ret(-ENOMEM);
230 dev_read_u32_array(dev, "brightness-levels", priv->levels,
232 priv->num_levels = count;
233 priv->default_level = priv->levels[index];
234 priv->max_level = priv->levels[count - 1];
236 priv->default_level = index;
237 priv->max_level = 255;
239 priv->cur_level = priv->default_level;
246 static int pwm_backlight_probe(struct udevice *dev)
251 static const struct backlight_ops pwm_backlight_ops = {
252 .enable = pwm_backlight_enable,
253 .set_brightness = pwm_backlight_set_brightness,
256 static const struct udevice_id pwm_backlight_ids[] = {
257 { .compatible = "pwm-backlight" },
261 U_BOOT_DRIVER(pwm_backlight) = {
262 .name = "pwm_backlight",
263 .id = UCLASS_PANEL_BACKLIGHT,
264 .of_match = pwm_backlight_ids,
265 .ops = &pwm_backlight_ops,
266 .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata,
267 .probe = pwm_backlight_probe,
268 .priv_auto = sizeof(struct pwm_backlight_priv),