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>
14 #include <power/regulator.h>
17 * Private information for the PWM backlight
19 * If @num_levels is 0 then the levels are simple values with the backlight
20 * value going between the minimum (default 0) and the maximum (default 255).
21 * Otherwise the levels are an index into @levels (0..n-1).
23 * @reg: Regulator to enable to turn the backlight on (NULL if none)
24 * @enable, GPIO to set to enable the backlight (can be missing)
25 * @pwm: PWM to use to change the backlight brightness
26 * @channel: PWM channel to use
27 * @period_ns: Period of the backlight in nanoseconds
28 * @levels: Levels for the backlight, or NULL if not using indexed levels
29 * @num_levels: Number of levels
30 * @cur_level: Current level for the backlight (index or value)
31 * @default_level: Default level for the backlight (index or value)
32 * @min_level: Minimum level of the backlight (full off)
33 * @min_level: Maximum level of the backlight (full on)
34 * @enabled: true if backlight is enabled
36 struct pwm_backlight_priv {
38 struct gpio_desc enable;
51 static int set_pwm(struct pwm_backlight_priv *priv)
56 duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) /
57 (priv->max_level - priv->min_level + 1);
58 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
64 static int enable_sequence(struct udevice *dev, int seq)
66 struct pwm_backlight_priv *priv = dev_get_priv(dev);
72 __maybe_unused struct dm_regulator_uclass_platdata
75 plat = dev_get_uclass_platdata(priv->reg);
76 log_debug("Enable '%s', regulator '%s'/'%s'\n",
77 dev->name, priv->reg->name, plat->name);
78 ret = regulator_set_enable(priv->reg, true);
80 log_debug("Cannot enable regulator for PWM '%s'\n",
89 dm_gpio_set_value(&priv->enable, 1);
96 static int pwm_backlight_enable(struct udevice *dev)
98 struct pwm_backlight_priv *priv = dev_get_priv(dev);
101 ret = enable_sequence(dev, 0);
107 ret = pwm_set_enable(priv->pwm, priv->channel, true);
110 ret = enable_sequence(dev, 1);
113 priv->enabled = true;
118 static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
120 struct pwm_backlight_priv *priv = dev_get_priv(dev);
121 bool disable = false;
125 if (!priv->enabled) {
126 ret = enable_sequence(dev, 0);
130 if (percent == BACKLIGHT_OFF) {
134 if (percent == BACKLIGHT_DEFAULT) {
135 level = priv->default_level;
138 level = priv->levels[percent * (priv->num_levels - 1)
141 level = priv->min_level +
142 (priv->max_level - priv->min_level) *
146 priv->cur_level = level;
151 if (!priv->enabled) {
152 ret = enable_sequence(dev, 1);
155 priv->enabled = true;
158 dm_gpio_set_value(&priv->enable, 0);
160 ret = regulator_set_enable(priv->reg, false);
164 priv->enabled = false;
170 static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
172 struct pwm_backlight_priv *priv = dev_get_priv(dev);
173 struct ofnode_phandle_args args;
174 int index, ret, count, len;
177 log_debug("start\n");
178 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
179 "power-supply", &priv->reg);
181 log_debug("Cannot get power supply: ret=%d\n", ret);
182 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
185 log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
189 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
192 log_debug("Cannot get PWM phandle: ret=%d\n", ret);
196 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
198 log_debug("Cannot get PWM: ret=%d\n", ret);
201 if (args.args_count < 2)
202 return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
203 priv->channel = args.args[0];
204 priv->period_ns = args.args[1];
206 index = dev_read_u32_default(dev, "default-brightness-level", 255);
207 cell = dev_read_prop(dev, "brightness-levels", &len);
208 count = len / sizeof(u32);
209 if (cell && count > index) {
210 priv->levels = malloc(len);
212 return log_ret(-ENOMEM);
213 dev_read_u32_array(dev, "brightness-levels", priv->levels,
215 priv->num_levels = count;
216 priv->default_level = priv->levels[index];
217 priv->max_level = priv->levels[count - 1];
219 priv->default_level = index;
220 priv->max_level = 255;
222 priv->cur_level = priv->default_level;
229 static int pwm_backlight_probe(struct udevice *dev)
234 static const struct backlight_ops pwm_backlight_ops = {
235 .enable = pwm_backlight_enable,
236 .set_brightness = pwm_backlight_set_brightness,
239 static const struct udevice_id pwm_backlight_ids[] = {
240 { .compatible = "pwm-backlight" },
244 U_BOOT_DRIVER(pwm_backlight) = {
245 .name = "pwm_backlight",
246 .id = UCLASS_PANEL_BACKLIGHT,
247 .of_match = pwm_backlight_ids,
248 .ops = &pwm_backlight_ops,
249 .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata,
250 .probe = pwm_backlight_probe,
251 .priv_auto_alloc_size = sizeof(struct pwm_backlight_priv),