blob: 9e32bc47cffbf979fd4f160fa458c39947866eb1 [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
65 duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) /
Dario Binacchi76c2ff32020-10-11 14:28:04 +020066 (priv->max_level - priv->min_level);
Simon Glassa4f737a2018-10-01 12:22:41 -060067 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
68 duty_cycle);
Stefan Mavrodiev57e77752019-04-12 08:56:27 +030069 if (ret)
70 return log_ret(ret);
Simon Glassa4f737a2018-10-01 12:22:41 -060071
Stefan Mavrodiev57e77752019-04-12 08:56:27 +030072 ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity);
Marc Dietricha2c4ef02019-07-02 22:08:33 +020073 if (ret == -ENOSYS && !priv->polarity)
74 ret = 0;
75
Simon Glassa4f737a2018-10-01 12:22:41 -060076 return log_ret(ret);
77}
78
79static 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 Glasscaa4daa2020-12-03 16:55:18 -070087 __maybe_unused struct dm_regulator_uclass_plat
Simon Glassa4f737a2018-10-01 12:22:41 -060088 *plat;
89
Simon Glasscaa4daa2020-12-03 16:55:18 -070090 plat = dev_get_uclass_plat(priv->reg);
Simon Glassa4f737a2018-10-01 12:22:41 -060091 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 Glass4a978e82018-11-23 21:29:39 -070096 dev->name);
Simon Glassa4f737a2018-10-01 12:22:41 -060097 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
Simon Glass65fba592016-01-21 19:44:57 -0700111static int pwm_backlight_enable(struct udevice *dev)
112{
113 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass65fba592016-01-21 19:44:57 -0700114 int ret;
115
Simon Glassa4f737a2018-10-01 12:22:41 -0600116 ret = enable_sequence(dev, 0);
Simon Glass65fba592016-01-21 19:44:57 -0700117 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600118 return log_ret(ret);
119 ret = set_pwm(priv);
120 if (ret)
121 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700122 ret = pwm_set_enable(priv->pwm, priv->channel, true);
123 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600124 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
133static 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 Glass65fba592016-01-21 19:44:57 -0700181
182 return 0;
183}
184
Simon Glassd1998a92020-12-03 16:55:21 -0700185static int pwm_backlight_of_to_plat(struct udevice *dev)
Simon Glass65fba592016-01-21 19:44:57 -0700186{
187 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -0600188 struct ofnode_phandle_args args;
Simon Glass65fba592016-01-21 19:44:57 -0700189 int index, ret, count, len;
190 const u32 *cell;
191
Simon Glassa4f737a2018-10-01 12:22:41 -0600192 log_debug("start\n");
Simon Glass65fba592016-01-21 19:44:57 -0700193 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
194 "power-supply", &priv->reg);
Kever Yang19f124d2018-02-09 10:45:12 +0800195 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600196 log_debug("Cannot get power supply: ret=%d\n", ret);
Simon Glass65fba592016-01-21 19:44:57 -0700197 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
198 GPIOD_IS_OUT);
199 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600200 log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
Simon Glass65fba592016-01-21 19:44:57 -0700201 if (ret != -ENOENT)
Simon Glassa4f737a2018-10-01 12:22:41 -0600202 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700203 }
Simon Glass7cf208d2017-06-12 06:21:37 -0600204 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
205 &args);
Simon Glass65fba592016-01-21 19:44:57 -0700206 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600207 log_debug("Cannot get PWM phandle: ret=%d\n", ret);
208 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700209 }
210
Simon Glass7cf208d2017-06-12 06:21:37 -0600211 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
Simon Glass65fba592016-01-21 19:44:57 -0700212 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600213 log_debug("Cannot get PWM: ret=%d\n", ret);
214 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700215 }
Simon Glassa4f737a2018-10-01 12:22:41 -0600216 if (args.args_count < 2)
217 return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
Simon Glass65fba592016-01-21 19:44:57 -0700218 priv->channel = args.args[0];
219 priv->period_ns = args.args[1];
Stefan Mavrodiev57e77752019-04-12 08:56:27 +0300220 if (args.args_count > 2)
221 priv->polarity = args.args[2];
Simon Glass65fba592016-01-21 19:44:57 -0700222
Simon Glass7cf208d2017-06-12 06:21:37 -0600223 index = dev_read_u32_default(dev, "default-brightness-level", 255);
224 cell = dev_read_prop(dev, "brightness-levels", &len);
Simon Glass65fba592016-01-21 19:44:57 -0700225 count = len / sizeof(u32);
226 if (cell && count > index) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600227 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 Glass65fba592016-01-21 19:44:57 -0700235 } else {
236 priv->default_level = index;
237 priv->max_level = 255;
238 }
Simon Glassa4f737a2018-10-01 12:22:41 -0600239 priv->cur_level = priv->default_level;
240 log_debug("done\n");
Simon Glass65fba592016-01-21 19:44:57 -0700241
242
243 return 0;
244}
245
246static int pwm_backlight_probe(struct udevice *dev)
247{
248 return 0;
249}
250
251static const struct backlight_ops pwm_backlight_ops = {
Simon Glassa4f737a2018-10-01 12:22:41 -0600252 .enable = pwm_backlight_enable,
253 .set_brightness = pwm_backlight_set_brightness,
Simon Glass65fba592016-01-21 19:44:57 -0700254};
255
256static const struct udevice_id pwm_backlight_ids[] = {
257 { .compatible = "pwm-backlight" },
258 { }
259};
260
261U_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 Glassd1998a92020-12-03 16:55:21 -0700266 .of_to_plat = pwm_backlight_of_to_plat,
Simon Glass65fba592016-01-21 19:44:57 -0700267 .probe = pwm_backlight_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700268 .priv_auto = sizeof(struct pwm_backlight_priv),
Simon Glass65fba592016-01-21 19:44:57 -0700269};