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