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> |
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 | { |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 62 | uint duty_cycle; |
| 63 | int ret; |
| 64 | |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 65 | duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) / |
Dario Binacchi | 753251d | 2020-10-11 14:28:04 +0200 | [diff] [blame] | 66 | (priv->max_level - priv->min_level); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 67 | ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns, |
| 68 | duty_cycle); |
Stefan Mavrodiev | a7bec41 | 2019-04-12 08:56:27 +0300 | [diff] [blame] | 69 | if (ret) |
| 70 | return log_ret(ret); |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 71 | |
Stefan Mavrodiev | a7bec41 | 2019-04-12 08:56:27 +0300 | [diff] [blame] | 72 | ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity); |
Marc Dietrich | bc59fb4 | 2019-07-02 22:08:33 +0200 | [diff] [blame] | 73 | if (ret == -ENOSYS && !priv->polarity) |
| 74 | ret = 0; |
| 75 | |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 76 | return log_ret(ret); |
| 77 | } |
| 78 | |
| 79 | static int enable_sequence(struct udevice *dev, int seq) |
| 80 | { |
| 81 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
| 82 | int ret; |
| 83 | |
| 84 | switch (seq) { |
| 85 | case 0: |
| 86 | if (priv->reg) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 87 | __maybe_unused struct dm_regulator_uclass_plat |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 88 | *plat; |
| 89 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 90 | plat = dev_get_uclass_plat(priv->reg); |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 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); |
| 94 | if (ret) { |
| 95 | log_debug("Cannot enable regulator for PWM '%s'\n", |
Simon Glass | a119b90 | 2018-11-23 21:29:39 -0700 | [diff] [blame] | 96 | dev->name); |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 97 | return log_ret(ret); |
| 98 | } |
| 99 | mdelay(120); |
| 100 | } |
| 101 | break; |
| 102 | case 1: |
| 103 | mdelay(10); |
| 104 | dm_gpio_set_value(&priv->enable, 1); |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int pwm_backlight_enable(struct udevice *dev) |
| 112 | { |
| 113 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
| 114 | int ret; |
| 115 | |
| 116 | ret = enable_sequence(dev, 0); |
| 117 | if (ret) |
| 118 | return log_ret(ret); |
| 119 | ret = set_pwm(priv); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 120 | if (ret) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 121 | return log_ret(ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 122 | ret = pwm_set_enable(priv->pwm, priv->channel, true); |
| 123 | if (ret) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 124 | return log_ret(ret); |
| 125 | ret = enable_sequence(dev, 1); |
| 126 | if (ret) |
| 127 | return log_ret(ret); |
| 128 | priv->enabled = true; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int pwm_backlight_set_brightness(struct udevice *dev, int percent) |
| 134 | { |
| 135 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
| 136 | bool disable = false; |
| 137 | int level; |
| 138 | int ret; |
| 139 | |
| 140 | if (!priv->enabled) { |
| 141 | ret = enable_sequence(dev, 0); |
| 142 | if (ret) |
| 143 | return log_ret(ret); |
| 144 | } |
| 145 | if (percent == BACKLIGHT_OFF) { |
| 146 | disable = true; |
| 147 | percent = 0; |
| 148 | } |
| 149 | if (percent == BACKLIGHT_DEFAULT) { |
| 150 | level = priv->default_level; |
| 151 | } else { |
| 152 | if (priv->levels) { |
| 153 | level = priv->levels[percent * (priv->num_levels - 1) |
| 154 | / 100]; |
| 155 | } else { |
| 156 | level = priv->min_level + |
| 157 | (priv->max_level - priv->min_level) * |
| 158 | percent / 100; |
| 159 | } |
| 160 | } |
| 161 | priv->cur_level = level; |
| 162 | |
| 163 | ret = set_pwm(priv); |
| 164 | if (ret) |
| 165 | return log_ret(ret); |
| 166 | if (!priv->enabled) { |
| 167 | ret = enable_sequence(dev, 1); |
| 168 | if (ret) |
| 169 | return log_ret(ret); |
| 170 | priv->enabled = true; |
| 171 | } |
| 172 | if (disable) { |
| 173 | dm_gpio_set_value(&priv->enable, 0); |
| 174 | if (priv->reg) { |
| 175 | ret = regulator_set_enable(priv->reg, false); |
| 176 | if (ret) |
| 177 | return log_ret(ret); |
| 178 | } |
| 179 | priv->enabled = false; |
| 180 | } |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 185 | static int pwm_backlight_of_to_plat(struct udevice *dev) |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 186 | { |
| 187 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
Simon Glass | e247e5c | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 188 | struct ofnode_phandle_args args; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 189 | int index, ret, count, len; |
| 190 | const u32 *cell; |
| 191 | |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 192 | log_debug("start\n"); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 193 | ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev, |
| 194 | "power-supply", &priv->reg); |
Kever Yang | 2dae581 | 2018-02-09 10:45:12 +0800 | [diff] [blame] | 195 | if (ret) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 196 | log_debug("Cannot get power supply: ret=%d\n", ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 197 | ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable, |
| 198 | GPIOD_IS_OUT); |
| 199 | if (ret) { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 200 | log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 201 | if (ret != -ENOENT) |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 202 | return log_ret(ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 203 | } |
Simon Glass | e247e5c | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 204 | ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, |
| 205 | &args); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 206 | if (ret) { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 207 | log_debug("Cannot get PWM phandle: ret=%d\n", ret); |
| 208 | return log_ret(ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Simon Glass | e247e5c | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 211 | ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 212 | if (ret) { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 213 | log_debug("Cannot get PWM: ret=%d\n", ret); |
| 214 | return log_ret(ret); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 215 | } |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 216 | if (args.args_count < 2) |
| 217 | return log_msg_ret("Not enough arguments to pwm\n", -EINVAL); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 218 | priv->channel = args.args[0]; |
| 219 | priv->period_ns = args.args[1]; |
Stefan Mavrodiev | a7bec41 | 2019-04-12 08:56:27 +0300 | [diff] [blame] | 220 | if (args.args_count > 2) |
| 221 | priv->polarity = args.args[2]; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 222 | |
Simon Glass | e247e5c | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 223 | index = dev_read_u32_default(dev, "default-brightness-level", 255); |
| 224 | cell = dev_read_prop(dev, "brightness-levels", &len); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 225 | count = len / sizeof(u32); |
| 226 | if (cell && count > index) { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 227 | priv->levels = malloc(len); |
| 228 | if (!priv->levels) |
| 229 | return log_ret(-ENOMEM); |
| 230 | dev_read_u32_array(dev, "brightness-levels", priv->levels, |
| 231 | count); |
| 232 | priv->num_levels = count; |
| 233 | priv->default_level = priv->levels[index]; |
| 234 | priv->max_level = priv->levels[count - 1]; |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 235 | } else { |
| 236 | priv->default_level = index; |
| 237 | priv->max_level = 255; |
| 238 | } |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 239 | priv->cur_level = priv->default_level; |
| 240 | log_debug("done\n"); |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 241 | |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int pwm_backlight_probe(struct udevice *dev) |
| 247 | { |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static const struct backlight_ops pwm_backlight_ops = { |
Simon Glass | fe68a45 | 2018-10-01 12:22:41 -0600 | [diff] [blame] | 252 | .enable = pwm_backlight_enable, |
| 253 | .set_brightness = pwm_backlight_set_brightness, |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | static const struct udevice_id pwm_backlight_ids[] = { |
| 257 | { .compatible = "pwm-backlight" }, |
| 258 | { } |
| 259 | }; |
| 260 | |
| 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, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 266 | .of_to_plat = pwm_backlight_of_to_plat, |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 267 | .probe = pwm_backlight_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 268 | .priv_auto = sizeof(struct pwm_backlight_priv), |
Simon Glass | 37211bf | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 269 | }; |