blob: c13a90770909651f12d1c7bbbd5446f144b2d040 [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>
12#include <pwm.h>
13#include <asm/gpio.h>
14#include <power/regulator.h>
15
Simon Glassa4f737a2018-10-01 12:22:41 -060016/**
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 Glass65fba592016-01-21 19:44:57 -070036struct pwm_backlight_priv {
37 struct udevice *reg;
38 struct gpio_desc enable;
39 struct udevice *pwm;
40 uint channel;
41 uint period_ns;
Simon Glassa4f737a2018-10-01 12:22:41 -060042 u32 *levels;
43 int num_levels;
Simon Glass65fba592016-01-21 19:44:57 -070044 uint default_level;
Simon Glassa4f737a2018-10-01 12:22:41 -060045 int cur_level;
Simon Glass65fba592016-01-21 19:44:57 -070046 uint min_level;
47 uint max_level;
Simon Glassa4f737a2018-10-01 12:22:41 -060048 bool enabled;
Simon Glass65fba592016-01-21 19:44:57 -070049};
50
Simon Glassa4f737a2018-10-01 12:22:41 -060051static int set_pwm(struct pwm_backlight_priv *priv)
52{
53 uint duty_cycle;
54 int ret;
55
56 duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) /
57 (priv->max_level - priv->min_level + 1);
58 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
59 duty_cycle);
60
61 return log_ret(ret);
62}
63
64static int enable_sequence(struct udevice *dev, int seq)
65{
66 struct pwm_backlight_priv *priv = dev_get_priv(dev);
67 int ret;
68
69 switch (seq) {
70 case 0:
71 if (priv->reg) {
72 __maybe_unused struct dm_regulator_uclass_platdata
73 *plat;
74
75 plat = dev_get_uclass_platdata(priv->reg);
76 log_debug("Enable '%s', regulator '%s'/'%s'\n",
77 dev->name, priv->reg->name, plat->name);
78 ret = regulator_set_enable(priv->reg, true);
79 if (ret) {
80 log_debug("Cannot enable regulator for PWM '%s'\n",
81 __func__, dev->name);
82 return log_ret(ret);
83 }
84 mdelay(120);
85 }
86 break;
87 case 1:
88 mdelay(10);
89 dm_gpio_set_value(&priv->enable, 1);
90 break;
91 }
92
93 return 0;
94}
95
Simon Glass65fba592016-01-21 19:44:57 -070096static int pwm_backlight_enable(struct udevice *dev)
97{
98 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass65fba592016-01-21 19:44:57 -070099 int ret;
100
Simon Glassa4f737a2018-10-01 12:22:41 -0600101 ret = enable_sequence(dev, 0);
Simon Glass65fba592016-01-21 19:44:57 -0700102 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600103 return log_ret(ret);
104 ret = set_pwm(priv);
105 if (ret)
106 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700107 ret = pwm_set_enable(priv->pwm, priv->channel, true);
108 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600109 return log_ret(ret);
110 ret = enable_sequence(dev, 1);
111 if (ret)
112 return log_ret(ret);
113 priv->enabled = true;
114
115 return 0;
116}
117
118static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
119{
120 struct pwm_backlight_priv *priv = dev_get_priv(dev);
121 bool disable = false;
122 int level;
123 int ret;
124
125 if (!priv->enabled) {
126 ret = enable_sequence(dev, 0);
127 if (ret)
128 return log_ret(ret);
129 }
130 if (percent == BACKLIGHT_OFF) {
131 disable = true;
132 percent = 0;
133 }
134 if (percent == BACKLIGHT_DEFAULT) {
135 level = priv->default_level;
136 } else {
137 if (priv->levels) {
138 level = priv->levels[percent * (priv->num_levels - 1)
139 / 100];
140 } else {
141 level = priv->min_level +
142 (priv->max_level - priv->min_level) *
143 percent / 100;
144 }
145 }
146 priv->cur_level = level;
147
148 ret = set_pwm(priv);
149 if (ret)
150 return log_ret(ret);
151 if (!priv->enabled) {
152 ret = enable_sequence(dev, 1);
153 if (ret)
154 return log_ret(ret);
155 priv->enabled = true;
156 }
157 if (disable) {
158 dm_gpio_set_value(&priv->enable, 0);
159 if (priv->reg) {
160 ret = regulator_set_enable(priv->reg, false);
161 if (ret)
162 return log_ret(ret);
163 }
164 priv->enabled = false;
165 }
Simon Glass65fba592016-01-21 19:44:57 -0700166
167 return 0;
168}
169
170static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
171{
172 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -0600173 struct ofnode_phandle_args args;
Simon Glass65fba592016-01-21 19:44:57 -0700174 int index, ret, count, len;
175 const u32 *cell;
176
Simon Glassa4f737a2018-10-01 12:22:41 -0600177 log_debug("start\n");
Simon Glass65fba592016-01-21 19:44:57 -0700178 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
179 "power-supply", &priv->reg);
Kever Yang19f124d2018-02-09 10:45:12 +0800180 if (ret)
Simon Glassa4f737a2018-10-01 12:22:41 -0600181 log_debug("Cannot get power supply: ret=%d\n", ret);
Simon Glass65fba592016-01-21 19:44:57 -0700182 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
183 GPIOD_IS_OUT);
184 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600185 log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
Simon Glass65fba592016-01-21 19:44:57 -0700186 if (ret != -ENOENT)
Simon Glassa4f737a2018-10-01 12:22:41 -0600187 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700188 }
Simon Glass7cf208d2017-06-12 06:21:37 -0600189 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
190 &args);
Simon Glass65fba592016-01-21 19:44:57 -0700191 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600192 log_debug("Cannot get PWM phandle: ret=%d\n", ret);
193 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700194 }
195
Simon Glass7cf208d2017-06-12 06:21:37 -0600196 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
Simon Glass65fba592016-01-21 19:44:57 -0700197 if (ret) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600198 log_debug("Cannot get PWM: ret=%d\n", ret);
199 return log_ret(ret);
Simon Glass65fba592016-01-21 19:44:57 -0700200 }
Simon Glassa4f737a2018-10-01 12:22:41 -0600201 if (args.args_count < 2)
202 return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
Simon Glass65fba592016-01-21 19:44:57 -0700203 priv->channel = args.args[0];
204 priv->period_ns = args.args[1];
205
Simon Glass7cf208d2017-06-12 06:21:37 -0600206 index = dev_read_u32_default(dev, "default-brightness-level", 255);
207 cell = dev_read_prop(dev, "brightness-levels", &len);
Simon Glass65fba592016-01-21 19:44:57 -0700208 count = len / sizeof(u32);
209 if (cell && count > index) {
Simon Glassa4f737a2018-10-01 12:22:41 -0600210 priv->levels = malloc(len);
211 if (!priv->levels)
212 return log_ret(-ENOMEM);
213 dev_read_u32_array(dev, "brightness-levels", priv->levels,
214 count);
215 priv->num_levels = count;
216 priv->default_level = priv->levels[index];
217 priv->max_level = priv->levels[count - 1];
Simon Glass65fba592016-01-21 19:44:57 -0700218 } else {
219 priv->default_level = index;
220 priv->max_level = 255;
221 }
Simon Glassa4f737a2018-10-01 12:22:41 -0600222 priv->cur_level = priv->default_level;
223 log_debug("done\n");
Simon Glass65fba592016-01-21 19:44:57 -0700224
225
226 return 0;
227}
228
229static int pwm_backlight_probe(struct udevice *dev)
230{
231 return 0;
232}
233
234static const struct backlight_ops pwm_backlight_ops = {
Simon Glassa4f737a2018-10-01 12:22:41 -0600235 .enable = pwm_backlight_enable,
236 .set_brightness = pwm_backlight_set_brightness,
Simon Glass65fba592016-01-21 19:44:57 -0700237};
238
239static const struct udevice_id pwm_backlight_ids[] = {
240 { .compatible = "pwm-backlight" },
241 { }
242};
243
244U_BOOT_DRIVER(pwm_backlight) = {
245 .name = "pwm_backlight",
246 .id = UCLASS_PANEL_BACKLIGHT,
247 .of_match = pwm_backlight_ids,
248 .ops = &pwm_backlight_ops,
249 .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata,
250 .probe = pwm_backlight_probe,
251 .priv_auto_alloc_size = sizeof(struct pwm_backlight_priv),
252};