blob: 1c747d98d7acfdd1d3024d3523af94e01fceff5c [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>
Alvaro Fernando García8300ceb2023-08-03 21:35:38 -030017#include <linux/math64.h>
Simon Glass65fba592016-01-21 19:44:57 -070018#include <power/regulator.h>
19
Simon Glassa4f737a2018-10-01 12:22:41 -060020/**
21 * Private information for the PWM backlight
22 *
23 * If @num_levels is 0 then the levels are simple values with the backlight
24 * value going between the minimum (default 0) and the maximum (default 255).
25 * Otherwise the levels are an index into @levels (0..n-1).
26 *
27 * @reg: Regulator to enable to turn the backlight on (NULL if none)
28 * @enable, GPIO to set to enable the backlight (can be missing)
29 * @pwm: PWM to use to change the backlight brightness
30 * @channel: PWM channel to use
31 * @period_ns: Period of the backlight in nanoseconds
32 * @levels: Levels for the backlight, or NULL if not using indexed levels
33 * @num_levels: Number of levels
34 * @cur_level: Current level for the backlight (index or value)
35 * @default_level: Default level for the backlight (index or value)
36 * @min_level: Minimum level of the backlight (full off)
Dario Binacchif9b94052020-10-11 14:28:03 +020037 * @max_level: Maximum level of the backlight (full on)
Simon Glassa4f737a2018-10-01 12:22:41 -060038 * @enabled: true if backlight is enabled
39 */
Simon Glass65fba592016-01-21 19:44:57 -070040struct pwm_backlight_priv {
41 struct udevice *reg;
42 struct gpio_desc enable;
43 struct udevice *pwm;
44 uint channel;
45 uint period_ns;
Stefan Mavrodiev57e77752019-04-12 08:56:27 +030046 /*
47 * the polarity of one PWM
48 * 0: normal polarity
49 * 1: inverted polarity
50 */
51 bool polarity;
Simon Glassa4f737a2018-10-01 12:22:41 -060052 u32 *levels;
53 int num_levels;
Simon Glass65fba592016-01-21 19:44:57 -070054 uint default_level;
Simon Glassa4f737a2018-10-01 12:22:41 -060055 int cur_level;
Simon Glass65fba592016-01-21 19:44:57 -070056 uint min_level;
57 uint max_level;
Simon Glassa4f737a2018-10-01 12:22:41 -060058 bool enabled;
Simon Glass65fba592016-01-21 19:44:57 -070059};
60
Simon Glassa4f737a2018-10-01 12:22:41 -060061static int set_pwm(struct pwm_backlight_priv *priv)
62{
Alvaro Fernando García8300ceb2023-08-03 21:35:38 -030063 u64 width;
Simon Glassa4f737a2018-10-01 12:22:41 -060064 uint duty_cycle;
65 int ret;
66
Alper Nebi Yasakfefa7132020-10-22 23:49:26 +030067 if (priv->period_ns) {
Alvaro Fernando García8300ceb2023-08-03 21:35:38 -030068 width = priv->period_ns * (priv->cur_level - priv->min_level);
69 duty_cycle = div_u64(width,
70 (priv->max_level - priv->min_level));
Alper Nebi Yasakfefa7132020-10-22 23:49:26 +030071 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
72 duty_cycle);
73 } else {
74 /* PWM driver will internally scale these like the above. */
75 ret = pwm_set_config(priv->pwm, priv->channel,
76 priv->max_level - priv->min_level,
77 priv->cur_level - priv->min_level);
78 }
Stefan Mavrodiev57e77752019-04-12 08:56:27 +030079 if (ret)
80 return log_ret(ret);
Simon Glassa4f737a2018-10-01 12:22:41 -060081
Stefan Mavrodiev57e77752019-04-12 08:56:27 +030082 ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity);
Marc Dietricha2c4ef02019-07-02 22:08:33 +020083 if (ret == -ENOSYS && !priv->polarity)
84 ret = 0;
85
Simon Glassa4f737a2018-10-01 12:22:41 -060086 return log_ret(ret);
87}
88
89static int enable_sequence(struct udevice *dev, int seq)
90{
91 struct pwm_backlight_priv *priv = dev_get_priv(dev);
92 int ret;
93
94 switch (seq) {
95 case 0:
96 if (priv->reg) {
Simon Glasscaa4daa2020-12-03 16:55:18 -070097 __maybe_unused struct dm_regulator_uclass_plat
Simon Glassa4f737a2018-10-01 12:22:41 -060098 *plat;
99
Simon Glasscaa4daa2020-12-03 16:55:18 -0700100 plat = dev_get_uclass_plat(priv->reg);
Simon Glassa4f737a2018-10-01 12:22:41 -0600101 log_debug("Enable '%s', regulator '%s'/'%s'\n",
102 dev->name, priv->reg->name, plat->name);
Svyatoslav Ryhel251ff342023-10-03 09:25:32 +0300103 ret = regulator_set_enable_if_allowed(priv->reg, true);
104 if (ret && ret != -ENOSYS) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600105 log_debug("Cannot enable regulator for PWM '%s'\n",
Simon Glass4a978e82018-11-23 21:29:39 -0700106 dev->name);
Simon Glassa4f737a2018-10-01 12:22:41 -0600107 return log_ret(ret);
108 }
109 mdelay(120);
110 }
111 break;
112 case 1:
113 mdelay(10);
114 dm_gpio_set_value(&priv->enable, 1);
115 break;
116 }
117
118 return 0;
119}
120
Simon Glass65fba592016-01-21 19:44:57 -0700121static int pwm_backlight_enable(struct udevice *dev)
122{
123 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass65fba592016-01-21 19:44:57 -0700124 int ret;
125
Simon Glassa4f737a2018-10-01 12:22:41 -0600126 ret = enable_sequence(dev, 0);
Simon Glass65fba592016-01-21 19:44:57 -0700127 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600128 return log_ret(ret);
129 ret = set_pwm(priv);
130 if (ret)
131 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700132 ret = pwm_set_enable(priv->pwm, priv->channel, true);
133 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600134 return log_ret(ret);
135 ret = enable_sequence(dev, 1);
136 if (ret)
137 return log_ret(ret);
138 priv->enabled = true;
139
140 return 0;
141}
142
143static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
144{
145 struct pwm_backlight_priv *priv = dev_get_priv(dev);
146 bool disable = false;
147 int level;
148 int ret;
149
150 if (!priv->enabled) {
151 ret = enable_sequence(dev, 0);
152 if (ret)
153 return log_ret(ret);
154 }
155 if (percent == BACKLIGHT_OFF) {
156 disable = true;
157 percent = 0;
158 }
159 if (percent == BACKLIGHT_DEFAULT) {
160 level = priv->default_level;
161 } else {
162 if (priv->levels) {
163 level = priv->levels[percent * (priv->num_levels - 1)
164 / 100];
165 } else {
166 level = priv->min_level +
167 (priv->max_level - priv->min_level) *
168 percent / 100;
169 }
170 }
171 priv->cur_level = level;
172
173 ret = set_pwm(priv);
174 if (ret)
175 return log_ret(ret);
176 if (!priv->enabled) {
177 ret = enable_sequence(dev, 1);
178 if (ret)
179 return log_ret(ret);
180 priv->enabled = true;
181 }
182 if (disable) {
183 dm_gpio_set_value(&priv->enable, 0);
Svyatoslav Ryhel251ff342023-10-03 09:25:32 +0300184 ret = regulator_set_enable_if_allowed(priv->reg, false);
185 if (ret && ret != -ENOSYS)
186 return log_ret(ret);
187
Simon Glassa4f737a2018-10-01 12:22:41 -0600188 priv->enabled = false;
189 }
Simon Glass65fba592016-01-21 19:44:57 -0700190
191 return 0;
192}
193
Simon Glassd1998a92020-12-03 16:55:21 -0700194static int pwm_backlight_of_to_plat(struct udevice *dev)
Simon Glass65fba592016-01-21 19:44:57 -0700195{
196 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -0600197 struct ofnode_phandle_args args;
Simon Glass65fba592016-01-21 19:44:57 -0700198 int index, ret, count, len;
199 const u32 *cell;
200
Simon Glassa4f737a2018-10-01 12:22:41 -0600201 log_debug("start\n");
Simon Glass65fba592016-01-21 19:44:57 -0700202 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
203 "power-supply", &priv->reg);
Kever Yang19f124d2018-02-09 10:45:12 +0800204 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600205 log_debug("Cannot get power supply: ret=%d\n", ret);
Simon Glass65fba592016-01-21 19:44:57 -0700206 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
207 GPIOD_IS_OUT);
208 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600209 log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
Simon Glass65fba592016-01-21 19:44:57 -0700210 if (ret != -ENOENT)
Simon Glassa4f737a2018-10-01 12:22:41 -0600211 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700212 }
Simon Glass7cf208d2017-06-12 06:21:37 -0600213 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
214 &args);
Simon Glass65fba592016-01-21 19:44:57 -0700215 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600216 log_debug("Cannot get PWM phandle: ret=%d\n", ret);
217 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700218 }
219
Simon Glass7cf208d2017-06-12 06:21:37 -0600220 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
Simon Glass65fba592016-01-21 19:44:57 -0700221 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600222 log_debug("Cannot get PWM: ret=%d\n", ret);
223 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700224 }
Alper Nebi Yasakfefa7132020-10-22 23:49:26 +0300225 if (args.args_count < 1)
Simon Glassa4f737a2018-10-01 12:22:41 -0600226 return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
Simon Glass65fba592016-01-21 19:44:57 -0700227 priv->channel = args.args[0];
Alper Nebi Yasakfefa7132020-10-22 23:49:26 +0300228 if (args.args_count > 1)
229 priv->period_ns = args.args[1];
Stefan Mavrodiev57e77752019-04-12 08:56:27 +0300230 if (args.args_count > 2)
231 priv->polarity = args.args[2];
Simon Glass65fba592016-01-21 19:44:57 -0700232
Simon Glass7cf208d2017-06-12 06:21:37 -0600233 index = dev_read_u32_default(dev, "default-brightness-level", 255);
234 cell = dev_read_prop(dev, "brightness-levels", &len);
Simon Glass65fba592016-01-21 19:44:57 -0700235 count = len / sizeof(u32);
236 if (cell && count > index) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600237 priv->levels = malloc(len);
238 if (!priv->levels)
239 return log_ret(-ENOMEM);
Simon Glass92598bd2021-05-13 19:39:18 -0600240 ret = dev_read_u32_array(dev, "brightness-levels", priv->levels,
241 count);
242 if (ret)
243 return log_msg_ret("levels", ret);
Simon Glassa4f737a2018-10-01 12:22:41 -0600244 priv->num_levels = count;
245 priv->default_level = priv->levels[index];
246 priv->max_level = priv->levels[count - 1];
Simon Glass65fba592016-01-21 19:44:57 -0700247 } else {
248 priv->default_level = index;
249 priv->max_level = 255;
250 }
Simon Glassa4f737a2018-10-01 12:22:41 -0600251 priv->cur_level = priv->default_level;
252 log_debug("done\n");
Simon Glass65fba592016-01-21 19:44:57 -0700253
254
255 return 0;
256}
257
258static int pwm_backlight_probe(struct udevice *dev)
259{
260 return 0;
261}
262
263static const struct backlight_ops pwm_backlight_ops = {
Simon Glassa4f737a2018-10-01 12:22:41 -0600264 .enable = pwm_backlight_enable,
265 .set_brightness = pwm_backlight_set_brightness,
Simon Glass65fba592016-01-21 19:44:57 -0700266};
267
268static const struct udevice_id pwm_backlight_ids[] = {
269 { .compatible = "pwm-backlight" },
270 { }
271};
272
273U_BOOT_DRIVER(pwm_backlight) = {
274 .name = "pwm_backlight",
275 .id = UCLASS_PANEL_BACKLIGHT,
276 .of_match = pwm_backlight_ids,
277 .ops = &pwm_backlight_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700278 .of_to_plat = pwm_backlight_of_to_plat,
Simon Glass65fba592016-01-21 19:44:57 -0700279 .probe = pwm_backlight_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700280 .priv_auto = sizeof(struct pwm_backlight_priv),
Simon Glass65fba592016-01-21 19:44:57 -0700281};