blob: d7c096923b37288adc6ed450e036fe456ae4b07b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass65fba592016-01-21 19:44:57 -07002/*
3 * Copyright (c) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass65fba592016-01-21 19:44:57 -07005 */
6
Simon Glassa4f737a2018-10-01 12:22:41 -06007#define LOG_CATEGORY UCLASS_PANEL_BACKLIGHT
8
Simon Glass65fba592016-01-21 19:44:57 -07009#include <common.h>
10#include <dm.h>
11#include <backlight.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass65fba592016-01-21 19:44:57 -070014#include <pwm.h>
15#include <asm/gpio.h>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
Simon Glass65fba592016-01-21 19:44:57 -070017#include <power/regulator.h>
18
Simon Glassa4f737a2018-10-01 12:22:41 -060019/**
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 Binacchif9b94052020-10-11 14:28:03 +020036 * @max_level: Maximum level of the backlight (full on)
Simon Glassa4f737a2018-10-01 12:22:41 -060037 * @enabled: true if backlight is enabled
38 */
Simon Glass65fba592016-01-21 19:44:57 -070039struct pwm_backlight_priv {
40 struct udevice *reg;
41 struct gpio_desc enable;
42 struct udevice *pwm;
43 uint channel;
44 uint period_ns;
Stefan Mavrodiev57e77752019-04-12 08:56:27 +030045 /*
46 * the polarity of one PWM
47 * 0: normal polarity
48 * 1: inverted polarity
49 */
50 bool polarity;
Simon Glassa4f737a2018-10-01 12:22:41 -060051 u32 *levels;
52 int num_levels;
Simon Glass65fba592016-01-21 19:44:57 -070053 uint default_level;
Simon Glassa4f737a2018-10-01 12:22:41 -060054 int cur_level;
Simon Glass65fba592016-01-21 19:44:57 -070055 uint min_level;
56 uint max_level;
Simon Glassa4f737a2018-10-01 12:22:41 -060057 bool enabled;
Simon Glass65fba592016-01-21 19:44:57 -070058};
59
Simon Glassa4f737a2018-10-01 12:22:41 -060060static int set_pwm(struct pwm_backlight_priv *priv)
61{
62 uint duty_cycle;
63 int ret;
64
Alper Nebi Yasakfefa7132020-10-22 23:49:26 +030065 if (priv->period_ns) {
66 duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) /
67 (priv->max_level - priv->min_level);
68 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
69 duty_cycle);
70 } else {
71 /* PWM driver will internally scale these like the above. */
72 ret = pwm_set_config(priv->pwm, priv->channel,
73 priv->max_level - priv->min_level,
74 priv->cur_level - priv->min_level);
75 }
Stefan Mavrodiev57e77752019-04-12 08:56:27 +030076 if (ret)
77 return log_ret(ret);
Simon Glassa4f737a2018-10-01 12:22:41 -060078
Stefan Mavrodiev57e77752019-04-12 08:56:27 +030079 ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity);
Marc Dietricha2c4ef02019-07-02 22:08:33 +020080 if (ret == -ENOSYS && !priv->polarity)
81 ret = 0;
82
Simon Glassa4f737a2018-10-01 12:22:41 -060083 return log_ret(ret);
84}
85
86static int enable_sequence(struct udevice *dev, int seq)
87{
88 struct pwm_backlight_priv *priv = dev_get_priv(dev);
89 int ret;
90
91 switch (seq) {
92 case 0:
93 if (priv->reg) {
Simon Glasscaa4daa2020-12-03 16:55:18 -070094 __maybe_unused struct dm_regulator_uclass_plat
Simon Glassa4f737a2018-10-01 12:22:41 -060095 *plat;
96
Simon Glasscaa4daa2020-12-03 16:55:18 -070097 plat = dev_get_uclass_plat(priv->reg);
Simon Glassa4f737a2018-10-01 12:22:41 -060098 log_debug("Enable '%s', regulator '%s'/'%s'\n",
99 dev->name, priv->reg->name, plat->name);
100 ret = regulator_set_enable(priv->reg, true);
101 if (ret) {
102 log_debug("Cannot enable regulator for PWM '%s'\n",
Simon Glass4a978e82018-11-23 21:29:39 -0700103 dev->name);
Simon Glassa4f737a2018-10-01 12:22:41 -0600104 return log_ret(ret);
105 }
106 mdelay(120);
107 }
108 break;
109 case 1:
110 mdelay(10);
111 dm_gpio_set_value(&priv->enable, 1);
112 break;
113 }
114
115 return 0;
116}
117
Simon Glass65fba592016-01-21 19:44:57 -0700118static int pwm_backlight_enable(struct udevice *dev)
119{
120 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass65fba592016-01-21 19:44:57 -0700121 int ret;
122
Simon Glassa4f737a2018-10-01 12:22:41 -0600123 ret = enable_sequence(dev, 0);
Simon Glass65fba592016-01-21 19:44:57 -0700124 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600125 return log_ret(ret);
126 ret = set_pwm(priv);
127 if (ret)
128 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700129 ret = pwm_set_enable(priv->pwm, priv->channel, true);
130 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600131 return log_ret(ret);
132 ret = enable_sequence(dev, 1);
133 if (ret)
134 return log_ret(ret);
135 priv->enabled = true;
136
137 return 0;
138}
139
140static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
141{
142 struct pwm_backlight_priv *priv = dev_get_priv(dev);
143 bool disable = false;
144 int level;
145 int ret;
146
147 if (!priv->enabled) {
148 ret = enable_sequence(dev, 0);
149 if (ret)
150 return log_ret(ret);
151 }
152 if (percent == BACKLIGHT_OFF) {
153 disable = true;
154 percent = 0;
155 }
156 if (percent == BACKLIGHT_DEFAULT) {
157 level = priv->default_level;
158 } else {
159 if (priv->levels) {
160 level = priv->levels[percent * (priv->num_levels - 1)
161 / 100];
162 } else {
163 level = priv->min_level +
164 (priv->max_level - priv->min_level) *
165 percent / 100;
166 }
167 }
168 priv->cur_level = level;
169
170 ret = set_pwm(priv);
171 if (ret)
172 return log_ret(ret);
173 if (!priv->enabled) {
174 ret = enable_sequence(dev, 1);
175 if (ret)
176 return log_ret(ret);
177 priv->enabled = true;
178 }
179 if (disable) {
180 dm_gpio_set_value(&priv->enable, 0);
181 if (priv->reg) {
182 ret = regulator_set_enable(priv->reg, false);
183 if (ret)
184 return log_ret(ret);
185 }
186 priv->enabled = false;
187 }
Simon Glass65fba592016-01-21 19:44:57 -0700188
189 return 0;
190}
191
Simon Glassd1998a92020-12-03 16:55:21 -0700192static int pwm_backlight_of_to_plat(struct udevice *dev)
Simon Glass65fba592016-01-21 19:44:57 -0700193{
194 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -0600195 struct ofnode_phandle_args args;
Simon Glass65fba592016-01-21 19:44:57 -0700196 int index, ret, count, len;
197 const u32 *cell;
198
Simon Glassa4f737a2018-10-01 12:22:41 -0600199 log_debug("start\n");
Simon Glass65fba592016-01-21 19:44:57 -0700200 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
201 "power-supply", &priv->reg);
Kever Yang19f124d2018-02-09 10:45:12 +0800202 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600203 log_debug("Cannot get power supply: ret=%d\n", ret);
Simon Glass65fba592016-01-21 19:44:57 -0700204 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
205 GPIOD_IS_OUT);
206 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600207 log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
Simon Glass65fba592016-01-21 19:44:57 -0700208 if (ret != -ENOENT)
Simon Glassa4f737a2018-10-01 12:22:41 -0600209 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700210 }
Simon Glass7cf208d2017-06-12 06:21:37 -0600211 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
212 &args);
Simon Glass65fba592016-01-21 19:44:57 -0700213 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600214 log_debug("Cannot get PWM phandle: ret=%d\n", ret);
215 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700216 }
217
Simon Glass7cf208d2017-06-12 06:21:37 -0600218 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
Simon Glass65fba592016-01-21 19:44:57 -0700219 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600220 log_debug("Cannot get PWM: ret=%d\n", ret);
221 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700222 }
Alper Nebi Yasakfefa7132020-10-22 23:49:26 +0300223 if (args.args_count < 1)
Simon Glassa4f737a2018-10-01 12:22:41 -0600224 return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
Simon Glass65fba592016-01-21 19:44:57 -0700225 priv->channel = args.args[0];
Alper Nebi Yasakfefa7132020-10-22 23:49:26 +0300226 if (args.args_count > 1)
227 priv->period_ns = args.args[1];
Stefan Mavrodiev57e77752019-04-12 08:56:27 +0300228 if (args.args_count > 2)
229 priv->polarity = args.args[2];
Simon Glass65fba592016-01-21 19:44:57 -0700230
Simon Glass7cf208d2017-06-12 06:21:37 -0600231 index = dev_read_u32_default(dev, "default-brightness-level", 255);
232 cell = dev_read_prop(dev, "brightness-levels", &len);
Simon Glass65fba592016-01-21 19:44:57 -0700233 count = len / sizeof(u32);
234 if (cell && count > index) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600235 priv->levels = malloc(len);
236 if (!priv->levels)
237 return log_ret(-ENOMEM);
Simon Glass92598bd2021-05-13 19:39:18 -0600238 ret = dev_read_u32_array(dev, "brightness-levels", priv->levels,
239 count);
240 if (ret)
241 return log_msg_ret("levels", ret);
Simon Glassa4f737a2018-10-01 12:22:41 -0600242 priv->num_levels = count;
243 priv->default_level = priv->levels[index];
244 priv->max_level = priv->levels[count - 1];
Simon Glass65fba592016-01-21 19:44:57 -0700245 } else {
246 priv->default_level = index;
247 priv->max_level = 255;
248 }
Simon Glassa4f737a2018-10-01 12:22:41 -0600249 priv->cur_level = priv->default_level;
250 log_debug("done\n");
Simon Glass65fba592016-01-21 19:44:57 -0700251
252
253 return 0;
254}
255
256static int pwm_backlight_probe(struct udevice *dev)
257{
258 return 0;
259}
260
261static const struct backlight_ops pwm_backlight_ops = {
Simon Glassa4f737a2018-10-01 12:22:41 -0600262 .enable = pwm_backlight_enable,
263 .set_brightness = pwm_backlight_set_brightness,
Simon Glass65fba592016-01-21 19:44:57 -0700264};
265
266static const struct udevice_id pwm_backlight_ids[] = {
267 { .compatible = "pwm-backlight" },
268 { }
269};
270
271U_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 Glassd1998a92020-12-03 16:55:21 -0700276 .of_to_plat = pwm_backlight_of_to_plat,
Simon Glass65fba592016-01-21 19:44:57 -0700277 .probe = pwm_backlight_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700278 .priv_auto = sizeof(struct pwm_backlight_priv),
Simon Glass65fba592016-01-21 19:44:57 -0700279};