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