183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
265fba592SSimon Glass /*
365fba592SSimon Glass * Copyright (c) 2016 Google, Inc
465fba592SSimon Glass * Written by Simon Glass <sjg@chromium.org>
565fba592SSimon Glass */
665fba592SSimon Glass
7a4f737a9SSimon Glass #define LOG_CATEGORY UCLASS_PANEL_BACKLIGHT
8a4f737a9SSimon Glass
965fba592SSimon Glass #include <common.h>
1065fba592SSimon Glass #include <dm.h>
1165fba592SSimon Glass #include <backlight.h>
1265fba592SSimon Glass #include <pwm.h>
1365fba592SSimon Glass #include <asm/gpio.h>
1465fba592SSimon Glass #include <power/regulator.h>
1565fba592SSimon Glass
16a4f737a9SSimon Glass /**
17a4f737a9SSimon Glass * Private information for the PWM backlight
18a4f737a9SSimon Glass *
19a4f737a9SSimon Glass * If @num_levels is 0 then the levels are simple values with the backlight
20a4f737a9SSimon Glass * value going between the minimum (default 0) and the maximum (default 255).
21a4f737a9SSimon Glass * Otherwise the levels are an index into @levels (0..n-1).
22a4f737a9SSimon Glass *
23a4f737a9SSimon Glass * @reg: Regulator to enable to turn the backlight on (NULL if none)
24a4f737a9SSimon Glass * @enable, GPIO to set to enable the backlight (can be missing)
25a4f737a9SSimon Glass * @pwm: PWM to use to change the backlight brightness
26a4f737a9SSimon Glass * @channel: PWM channel to use
27a4f737a9SSimon Glass * @period_ns: Period of the backlight in nanoseconds
28a4f737a9SSimon Glass * @levels: Levels for the backlight, or NULL if not using indexed levels
29a4f737a9SSimon Glass * @num_levels: Number of levels
30a4f737a9SSimon Glass * @cur_level: Current level for the backlight (index or value)
31a4f737a9SSimon Glass * @default_level: Default level for the backlight (index or value)
32a4f737a9SSimon Glass * @min_level: Minimum level of the backlight (full off)
33a4f737a9SSimon Glass * @min_level: Maximum level of the backlight (full on)
34a4f737a9SSimon Glass * @enabled: true if backlight is enabled
35a4f737a9SSimon Glass */
3665fba592SSimon Glass struct pwm_backlight_priv {
3765fba592SSimon Glass struct udevice *reg;
3865fba592SSimon Glass struct gpio_desc enable;
3965fba592SSimon Glass struct udevice *pwm;
4065fba592SSimon Glass uint channel;
4165fba592SSimon Glass uint period_ns;
42a4f737a9SSimon Glass u32 *levels;
43a4f737a9SSimon Glass int num_levels;
4465fba592SSimon Glass uint default_level;
45a4f737a9SSimon Glass int cur_level;
4665fba592SSimon Glass uint min_level;
4765fba592SSimon Glass uint max_level;
48a4f737a9SSimon Glass bool enabled;
4965fba592SSimon Glass };
5065fba592SSimon Glass
set_pwm(struct pwm_backlight_priv * priv)51a4f737a9SSimon Glass static int set_pwm(struct pwm_backlight_priv *priv)
52a4f737a9SSimon Glass {
53a4f737a9SSimon Glass uint duty_cycle;
54a4f737a9SSimon Glass int ret;
55a4f737a9SSimon Glass
56a4f737a9SSimon Glass duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) /
57a4f737a9SSimon Glass (priv->max_level - priv->min_level + 1);
58a4f737a9SSimon Glass ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
59a4f737a9SSimon Glass duty_cycle);
60a4f737a9SSimon Glass
61a4f737a9SSimon Glass return log_ret(ret);
62a4f737a9SSimon Glass }
63a4f737a9SSimon Glass
enable_sequence(struct udevice * dev,int seq)64a4f737a9SSimon Glass static int enable_sequence(struct udevice *dev, int seq)
65a4f737a9SSimon Glass {
66a4f737a9SSimon Glass struct pwm_backlight_priv *priv = dev_get_priv(dev);
67a4f737a9SSimon Glass int ret;
68a4f737a9SSimon Glass
69a4f737a9SSimon Glass switch (seq) {
70a4f737a9SSimon Glass case 0:
71a4f737a9SSimon Glass if (priv->reg) {
72a4f737a9SSimon Glass __maybe_unused struct dm_regulator_uclass_platdata
73a4f737a9SSimon Glass *plat;
74a4f737a9SSimon Glass
75a4f737a9SSimon Glass plat = dev_get_uclass_platdata(priv->reg);
76a4f737a9SSimon Glass log_debug("Enable '%s', regulator '%s'/'%s'\n",
77a4f737a9SSimon Glass dev->name, priv->reg->name, plat->name);
78a4f737a9SSimon Glass ret = regulator_set_enable(priv->reg, true);
79a4f737a9SSimon Glass if (ret) {
80a4f737a9SSimon Glass log_debug("Cannot enable regulator for PWM '%s'\n",
81*4a978e86SSimon Glass dev->name);
82a4f737a9SSimon Glass return log_ret(ret);
83a4f737a9SSimon Glass }
84a4f737a9SSimon Glass mdelay(120);
85a4f737a9SSimon Glass }
86a4f737a9SSimon Glass break;
87a4f737a9SSimon Glass case 1:
88a4f737a9SSimon Glass mdelay(10);
89a4f737a9SSimon Glass dm_gpio_set_value(&priv->enable, 1);
90a4f737a9SSimon Glass break;
91a4f737a9SSimon Glass }
92a4f737a9SSimon Glass
93a4f737a9SSimon Glass return 0;
94a4f737a9SSimon Glass }
95a4f737a9SSimon Glass
pwm_backlight_enable(struct udevice * dev)9665fba592SSimon Glass static int pwm_backlight_enable(struct udevice *dev)
9765fba592SSimon Glass {
9865fba592SSimon Glass struct pwm_backlight_priv *priv = dev_get_priv(dev);
9965fba592SSimon Glass int ret;
10065fba592SSimon Glass
101a4f737a9SSimon Glass ret = enable_sequence(dev, 0);
10265fba592SSimon Glass if (ret)
103a4f737a9SSimon Glass return log_ret(ret);
104a4f737a9SSimon Glass ret = set_pwm(priv);
105a4f737a9SSimon Glass if (ret)
106a4f737a9SSimon Glass return log_ret(ret);
10765fba592SSimon Glass ret = pwm_set_enable(priv->pwm, priv->channel, true);
10865fba592SSimon Glass if (ret)
109a4f737a9SSimon Glass return log_ret(ret);
110a4f737a9SSimon Glass ret = enable_sequence(dev, 1);
111a4f737a9SSimon Glass if (ret)
112a4f737a9SSimon Glass return log_ret(ret);
113a4f737a9SSimon Glass priv->enabled = true;
114a4f737a9SSimon Glass
115a4f737a9SSimon Glass return 0;
116a4f737a9SSimon Glass }
117a4f737a9SSimon Glass
pwm_backlight_set_brightness(struct udevice * dev,int percent)118a4f737a9SSimon Glass static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
119a4f737a9SSimon Glass {
120a4f737a9SSimon Glass struct pwm_backlight_priv *priv = dev_get_priv(dev);
121a4f737a9SSimon Glass bool disable = false;
122a4f737a9SSimon Glass int level;
123a4f737a9SSimon Glass int ret;
124a4f737a9SSimon Glass
125a4f737a9SSimon Glass if (!priv->enabled) {
126a4f737a9SSimon Glass ret = enable_sequence(dev, 0);
127a4f737a9SSimon Glass if (ret)
128a4f737a9SSimon Glass return log_ret(ret);
129a4f737a9SSimon Glass }
130a4f737a9SSimon Glass if (percent == BACKLIGHT_OFF) {
131a4f737a9SSimon Glass disable = true;
132a4f737a9SSimon Glass percent = 0;
133a4f737a9SSimon Glass }
134a4f737a9SSimon Glass if (percent == BACKLIGHT_DEFAULT) {
135a4f737a9SSimon Glass level = priv->default_level;
136a4f737a9SSimon Glass } else {
137a4f737a9SSimon Glass if (priv->levels) {
138a4f737a9SSimon Glass level = priv->levels[percent * (priv->num_levels - 1)
139a4f737a9SSimon Glass / 100];
140a4f737a9SSimon Glass } else {
141a4f737a9SSimon Glass level = priv->min_level +
142a4f737a9SSimon Glass (priv->max_level - priv->min_level) *
143a4f737a9SSimon Glass percent / 100;
144a4f737a9SSimon Glass }
145a4f737a9SSimon Glass }
146a4f737a9SSimon Glass priv->cur_level = level;
147a4f737a9SSimon Glass
148a4f737a9SSimon Glass ret = set_pwm(priv);
149a4f737a9SSimon Glass if (ret)
150a4f737a9SSimon Glass return log_ret(ret);
151a4f737a9SSimon Glass if (!priv->enabled) {
152a4f737a9SSimon Glass ret = enable_sequence(dev, 1);
153a4f737a9SSimon Glass if (ret)
154a4f737a9SSimon Glass return log_ret(ret);
155a4f737a9SSimon Glass priv->enabled = true;
156a4f737a9SSimon Glass }
157a4f737a9SSimon Glass if (disable) {
158a4f737a9SSimon Glass dm_gpio_set_value(&priv->enable, 0);
159a4f737a9SSimon Glass if (priv->reg) {
160a4f737a9SSimon Glass ret = regulator_set_enable(priv->reg, false);
161a4f737a9SSimon Glass if (ret)
162a4f737a9SSimon Glass return log_ret(ret);
163a4f737a9SSimon Glass }
164a4f737a9SSimon Glass priv->enabled = false;
165a4f737a9SSimon Glass }
16665fba592SSimon Glass
16765fba592SSimon Glass return 0;
16865fba592SSimon Glass }
16965fba592SSimon Glass
pwm_backlight_ofdata_to_platdata(struct udevice * dev)17065fba592SSimon Glass static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
17165fba592SSimon Glass {
17265fba592SSimon Glass struct pwm_backlight_priv *priv = dev_get_priv(dev);
1737cf208d7SSimon Glass struct ofnode_phandle_args args;
17465fba592SSimon Glass int index, ret, count, len;
17565fba592SSimon Glass const u32 *cell;
17665fba592SSimon Glass
177a4f737a9SSimon Glass log_debug("start\n");
17865fba592SSimon Glass ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
17965fba592SSimon Glass "power-supply", &priv->reg);
18019f124d8SKever Yang if (ret)
181a4f737a9SSimon Glass log_debug("Cannot get power supply: ret=%d\n", ret);
18265fba592SSimon Glass ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
18365fba592SSimon Glass GPIOD_IS_OUT);
18465fba592SSimon Glass if (ret) {
185a4f737a9SSimon Glass log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
18665fba592SSimon Glass if (ret != -ENOENT)
187a4f737a9SSimon Glass return log_ret(ret);
18865fba592SSimon Glass }
1897cf208d7SSimon Glass ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
1907cf208d7SSimon Glass &args);
19165fba592SSimon Glass if (ret) {
192a4f737a9SSimon Glass log_debug("Cannot get PWM phandle: ret=%d\n", ret);
193a4f737a9SSimon Glass return log_ret(ret);
19465fba592SSimon Glass }
19565fba592SSimon Glass
1967cf208d7SSimon Glass ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
19765fba592SSimon Glass if (ret) {
198a4f737a9SSimon Glass log_debug("Cannot get PWM: ret=%d\n", ret);
199a4f737a9SSimon Glass return log_ret(ret);
20065fba592SSimon Glass }
201a4f737a9SSimon Glass if (args.args_count < 2)
202a4f737a9SSimon Glass return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
20365fba592SSimon Glass priv->channel = args.args[0];
20465fba592SSimon Glass priv->period_ns = args.args[1];
20565fba592SSimon Glass
2067cf208d7SSimon Glass index = dev_read_u32_default(dev, "default-brightness-level", 255);
2077cf208d7SSimon Glass cell = dev_read_prop(dev, "brightness-levels", &len);
20865fba592SSimon Glass count = len / sizeof(u32);
20965fba592SSimon Glass if (cell && count > index) {
210a4f737a9SSimon Glass priv->levels = malloc(len);
211a4f737a9SSimon Glass if (!priv->levels)
212a4f737a9SSimon Glass return log_ret(-ENOMEM);
213a4f737a9SSimon Glass dev_read_u32_array(dev, "brightness-levels", priv->levels,
214a4f737a9SSimon Glass count);
215a4f737a9SSimon Glass priv->num_levels = count;
216a4f737a9SSimon Glass priv->default_level = priv->levels[index];
217a4f737a9SSimon Glass priv->max_level = priv->levels[count - 1];
21865fba592SSimon Glass } else {
21965fba592SSimon Glass priv->default_level = index;
22065fba592SSimon Glass priv->max_level = 255;
22165fba592SSimon Glass }
222a4f737a9SSimon Glass priv->cur_level = priv->default_level;
223a4f737a9SSimon Glass log_debug("done\n");
22465fba592SSimon Glass
22565fba592SSimon Glass
22665fba592SSimon Glass return 0;
22765fba592SSimon Glass }
22865fba592SSimon Glass
pwm_backlight_probe(struct udevice * dev)22965fba592SSimon Glass static int pwm_backlight_probe(struct udevice *dev)
23065fba592SSimon Glass {
23165fba592SSimon Glass return 0;
23265fba592SSimon Glass }
23365fba592SSimon Glass
23465fba592SSimon Glass static const struct backlight_ops pwm_backlight_ops = {
23565fba592SSimon Glass .enable = pwm_backlight_enable,
236a4f737a9SSimon Glass .set_brightness = pwm_backlight_set_brightness,
23765fba592SSimon Glass };
23865fba592SSimon Glass
23965fba592SSimon Glass static const struct udevice_id pwm_backlight_ids[] = {
24065fba592SSimon Glass { .compatible = "pwm-backlight" },
24165fba592SSimon Glass { }
24265fba592SSimon Glass };
24365fba592SSimon Glass
24465fba592SSimon Glass U_BOOT_DRIVER(pwm_backlight) = {
24565fba592SSimon Glass .name = "pwm_backlight",
24665fba592SSimon Glass .id = UCLASS_PANEL_BACKLIGHT,
24765fba592SSimon Glass .of_match = pwm_backlight_ids,
24865fba592SSimon Glass .ops = &pwm_backlight_ops,
24965fba592SSimon Glass .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata,
25065fba592SSimon Glass .probe = pwm_backlight_probe,
25165fba592SSimon Glass .priv_auto_alloc_size = sizeof(struct pwm_backlight_priv),
25265fba592SSimon Glass };
253