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