Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_PANEL_BACKLIGHT |
| 8 | |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <backlight.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 14 | #include <pwm.h> |
| 15 | #include <asm/gpio.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
Alvaro Fernando García | be732a5 | 2023-08-03 21:35:38 -0300 | [diff] [blame] | 17 | #include <linux/math64.h> |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 18 | #include <power/regulator.h> |
| 19 | |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 20 | /** |
| 21 | * Private information for the PWM backlight |
| 22 | * |
| 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). |
| 26 | * |
| 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) |
Dario Binacchi | 4d57087 | 2020-10-11 14:28:03 +0200 | [diff] [blame] | 37 | * @max_level: Maximum level of the backlight (full on) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 38 | * @enabled: true if backlight is enabled |
| 39 | */ |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 40 | struct pwm_backlight_priv { |
| 41 | struct udevice *reg; |
| 42 | struct gpio_desc enable; |
| 43 | struct udevice *pwm; |
| 44 | uint channel; |
| 45 | uint period_ns; |
Stefan Mavrodiev | a7bec41 | 2019-04-12 08:56:27 +0300 | [diff] [blame] | 46 | /* |
| 47 | * the polarity of one PWM |
| 48 | * 0: normal polarity |
| 49 | * 1: inverted polarity |
| 50 | */ |
| 51 | bool polarity; |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 52 | u32 *levels; |
| 53 | int num_levels; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 54 | uint default_level; |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 55 | int cur_level; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 56 | uint min_level; |
| 57 | uint max_level; |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 58 | bool enabled; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 61 | static int set_pwm(struct pwm_backlight_priv *priv) |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 62 | { |
Alvaro Fernando García | be732a5 | 2023-08-03 21:35:38 -0300 | [diff] [blame] | 63 | u64 width; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 64 | uint duty_cycle; |
| 65 | int ret; |
| 66 | |
Alper Nebi Yasak | 4692746 | 2020-10-22 23:49:26 +0300 | [diff] [blame] | 67 | if (priv->period_ns) { |
Alvaro Fernando García | be732a5 | 2023-08-03 21:35:38 -0300 | [diff] [blame] | 68 | width = priv->period_ns * (priv->cur_level - priv->min_level); |
| 69 | duty_cycle = div_u64(width, |
| 70 | (priv->max_level - priv->min_level)); |
Alper Nebi Yasak | 4692746 | 2020-10-22 23:49:26 +0300 | [diff] [blame] | 71 | ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns, |
| 72 | duty_cycle); |
| 73 | } else { |
| 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); |
| 78 | } |
Stefan Mavrodiev | a7bec41 | 2019-04-12 08:56:27 +0300 | [diff] [blame] | 79 | if (ret) |
| 80 | return log_ret(ret); |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 81 | |
Stefan Mavrodiev | a7bec41 | 2019-04-12 08:56:27 +0300 | [diff] [blame] | 82 | ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity); |
Marc Dietrich | bc59fb4 | 2019-07-02 22:08:33 +0200 | [diff] [blame] | 83 | if (ret == -ENOSYS && !priv->polarity) |
| 84 | ret = 0; |
| 85 | |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 86 | return log_ret(ret); |
| 87 | } |
| 88 | |
| 89 | static int enable_sequence(struct udevice *dev, int seq) |
| 90 | { |
| 91 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
| 92 | int ret; |
| 93 | |
| 94 | switch (seq) { |
| 95 | case 0: |
| 96 | if (priv->reg) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 97 | __maybe_unused struct dm_regulator_uclass_plat |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 98 | *plat; |
| 99 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 100 | plat = dev_get_uclass_plat(priv->reg); |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 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); |
| 104 | if (ret) { |
| 105 | log_debug("Cannot enable regulator for PWM '%s'\n", |
Simon Glass | a119b90 | 2018-11-23 21:29:39 -0700 | [diff] [blame] | 106 | dev->name); |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 107 | return log_ret(ret); |
| 108 | } |
| 109 | mdelay(120); |
| 110 | } |
| 111 | break; |
| 112 | case 1: |
| 113 | mdelay(10); |
| 114 | dm_gpio_set_value(&priv->enable, 1); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int pwm_backlight_enable(struct udevice *dev) |
| 122 | { |
| 123 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
| 124 | int ret; |
| 125 | |
| 126 | ret = enable_sequence(dev, 0); |
| 127 | if (ret) |
| 128 | return log_ret(ret); |
| 129 | ret = set_pwm(priv); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 130 | if (ret) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 131 | return log_ret(ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 132 | ret = pwm_set_enable(priv->pwm, priv->channel, true); |
| 133 | if (ret) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 134 | return log_ret(ret); |
| 135 | ret = enable_sequence(dev, 1); |
| 136 | if (ret) |
| 137 | return log_ret(ret); |
| 138 | priv->enabled = true; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int pwm_backlight_set_brightness(struct udevice *dev, int percent) |
| 144 | { |
| 145 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
| 146 | bool disable = false; |
| 147 | int level; |
| 148 | int ret; |
| 149 | |
| 150 | if (!priv->enabled) { |
| 151 | ret = enable_sequence(dev, 0); |
| 152 | if (ret) |
| 153 | return log_ret(ret); |
| 154 | } |
| 155 | if (percent == BACKLIGHT_OFF) { |
| 156 | disable = true; |
| 157 | percent = 0; |
| 158 | } |
| 159 | if (percent == BACKLIGHT_DEFAULT) { |
| 160 | level = priv->default_level; |
| 161 | } else { |
| 162 | if (priv->levels) { |
| 163 | level = priv->levels[percent * (priv->num_levels - 1) |
| 164 | / 100]; |
| 165 | } else { |
| 166 | level = priv->min_level + |
| 167 | (priv->max_level - priv->min_level) * |
| 168 | percent / 100; |
| 169 | } |
| 170 | } |
| 171 | priv->cur_level = level; |
| 172 | |
| 173 | ret = set_pwm(priv); |
| 174 | if (ret) |
| 175 | return log_ret(ret); |
| 176 | if (!priv->enabled) { |
| 177 | ret = enable_sequence(dev, 1); |
| 178 | if (ret) |
| 179 | return log_ret(ret); |
| 180 | priv->enabled = true; |
| 181 | } |
| 182 | if (disable) { |
| 183 | dm_gpio_set_value(&priv->enable, 0); |
| 184 | if (priv->reg) { |
| 185 | ret = regulator_set_enable(priv->reg, false); |
| 186 | if (ret) |
| 187 | return log_ret(ret); |
| 188 | } |
| 189 | priv->enabled = false; |
| 190 | } |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 195 | static int pwm_backlight_of_to_plat(struct udevice *dev) |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 196 | { |
| 197 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
Simon Glass | e247e5c | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 198 | struct ofnode_phandle_args args; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 199 | int index, ret, count, len; |
| 200 | const u32 *cell; |
| 201 | |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 202 | log_debug("start\n"); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 203 | ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev, |
| 204 | "power-supply", &priv->reg); |
Kever Yang | 2dae581 | 2018-02-09 10:45:12 +0800 | [diff] [blame] | 205 | if (ret) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 206 | log_debug("Cannot get power supply: ret=%d\n", ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 207 | ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable, |
| 208 | GPIOD_IS_OUT); |
| 209 | if (ret) { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 210 | log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 211 | if (ret != -ENOENT) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 212 | return log_ret(ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 213 | } |
Simon Glass | e247e5c | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 214 | ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, |
| 215 | &args); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 216 | if (ret) { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 217 | log_debug("Cannot get PWM phandle: ret=%d\n", ret); |
| 218 | return log_ret(ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Simon Glass | e247e5c | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 221 | ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 222 | if (ret) { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 223 | log_debug("Cannot get PWM: ret=%d\n", ret); |
| 224 | return log_ret(ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 225 | } |
Alper Nebi Yasak | 4692746 | 2020-10-22 23:49:26 +0300 | [diff] [blame] | 226 | if (args.args_count < 1) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 227 | return log_msg_ret("Not enough arguments to pwm\n", -EINVAL); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 228 | priv->channel = args.args[0]; |
Alper Nebi Yasak | 4692746 | 2020-10-22 23:49:26 +0300 | [diff] [blame] | 229 | if (args.args_count > 1) |
| 230 | priv->period_ns = args.args[1]; |
Stefan Mavrodiev | a7bec41 | 2019-04-12 08:56:27 +0300 | [diff] [blame] | 231 | if (args.args_count > 2) |
| 232 | priv->polarity = args.args[2]; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 233 | |
Simon Glass | e247e5c | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 234 | index = dev_read_u32_default(dev, "default-brightness-level", 255); |
| 235 | cell = dev_read_prop(dev, "brightness-levels", &len); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 236 | count = len / sizeof(u32); |
| 237 | if (cell && count > index) { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 238 | priv->levels = malloc(len); |
| 239 | if (!priv->levels) |
| 240 | return log_ret(-ENOMEM); |
Simon Glass | 5a56994 | 2021-05-13 19:39:18 -0600 | [diff] [blame] | 241 | ret = dev_read_u32_array(dev, "brightness-levels", priv->levels, |
| 242 | count); |
| 243 | if (ret) |
| 244 | return log_msg_ret("levels", ret); |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 245 | priv->num_levels = count; |
| 246 | priv->default_level = priv->levels[index]; |
| 247 | priv->max_level = priv->levels[count - 1]; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 248 | } else { |
| 249 | priv->default_level = index; |
| 250 | priv->max_level = 255; |
| 251 | } |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 252 | priv->cur_level = priv->default_level; |
| 253 | log_debug("done\n"); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 254 | |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int pwm_backlight_probe(struct udevice *dev) |
| 260 | { |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static const struct backlight_ops pwm_backlight_ops = { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 265 | .enable = pwm_backlight_enable, |
| 266 | .set_brightness = pwm_backlight_set_brightness, |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | static const struct udevice_id pwm_backlight_ids[] = { |
| 270 | { .compatible = "pwm-backlight" }, |
| 271 | { } |
| 272 | }; |
| 273 | |
| 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, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 279 | .of_to_plat = pwm_backlight_of_to_plat, |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 280 | .probe = pwm_backlight_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 281 | .priv_auto = sizeof(struct pwm_backlight_priv), |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 282 | }; |