blob: f40e57bb8e6841aa9ec7718e31c69ea73b4bdc8e [file] [log] [blame]
Simon Glass65fba592016-01-21 19:44:57 -07001/*
2 * Copyright (c) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <backlight.h>
11#include <pwm.h>
12#include <asm/gpio.h>
13#include <power/regulator.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17struct pwm_backlight_priv {
18 struct udevice *reg;
19 struct gpio_desc enable;
20 struct udevice *pwm;
21 uint channel;
22 uint period_ns;
23 uint default_level;
24 uint min_level;
25 uint max_level;
26};
27
28static int pwm_backlight_enable(struct udevice *dev)
29{
30 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -060031 struct dm_regulator_uclass_platdata *plat;
Simon Glass65fba592016-01-21 19:44:57 -070032 uint duty_cycle;
33 int ret;
34
Kever Yang19f124d2018-02-09 10:45:12 +080035 if (priv->reg) {
36 plat = dev_get_uclass_platdata(priv->reg);
37 debug("%s: Enable '%s', regulator '%s'/'%s'\n", __func__,
38 dev->name, priv->reg->name, plat->name);
39 ret = regulator_set_enable(priv->reg, true);
40 if (ret) {
41 debug("%s: Cannot enable regulator for PWM '%s'\n",
42 __func__, dev->name);
43 return ret;
44 }
45 mdelay(120);
Simon Glass65fba592016-01-21 19:44:57 -070046 }
Simon Glass65fba592016-01-21 19:44:57 -070047
48 duty_cycle = priv->period_ns * (priv->default_level - priv->min_level) /
49 (priv->max_level - priv->min_level + 1);
50 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
51 duty_cycle);
52 if (ret)
53 return ret;
54 ret = pwm_set_enable(priv->pwm, priv->channel, true);
55 if (ret)
56 return ret;
57 mdelay(10);
58 dm_gpio_set_value(&priv->enable, 1);
59
60 return 0;
61}
62
63static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
64{
65 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -060066 struct ofnode_phandle_args args;
Simon Glass65fba592016-01-21 19:44:57 -070067 int index, ret, count, len;
68 const u32 *cell;
69
Simon Glass7cf208d2017-06-12 06:21:37 -060070 debug("%s: start\n", __func__);
Simon Glass65fba592016-01-21 19:44:57 -070071 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
72 "power-supply", &priv->reg);
Kever Yang19f124d2018-02-09 10:45:12 +080073 if (ret)
Simon Glass65fba592016-01-21 19:44:57 -070074 debug("%s: Cannot get power supply: ret=%d\n", __func__, ret);
Simon Glass65fba592016-01-21 19:44:57 -070075 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
76 GPIOD_IS_OUT);
77 if (ret) {
78 debug("%s: Warning: cannot get enable GPIO: ret=%d\n",
79 __func__, ret);
80 if (ret != -ENOENT)
81 return ret;
82 }
Simon Glass7cf208d2017-06-12 06:21:37 -060083 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
84 &args);
Simon Glass65fba592016-01-21 19:44:57 -070085 if (ret) {
86 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
87 return ret;
88 }
89
Simon Glass7cf208d2017-06-12 06:21:37 -060090 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
Simon Glass65fba592016-01-21 19:44:57 -070091 if (ret) {
92 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
93 return ret;
94 }
95 priv->channel = args.args[0];
96 priv->period_ns = args.args[1];
97
Simon Glass7cf208d2017-06-12 06:21:37 -060098 index = dev_read_u32_default(dev, "default-brightness-level", 255);
99 cell = dev_read_prop(dev, "brightness-levels", &len);
Simon Glass65fba592016-01-21 19:44:57 -0700100 count = len / sizeof(u32);
101 if (cell && count > index) {
102 priv->default_level = fdt32_to_cpu(cell[index]);
103 priv->max_level = fdt32_to_cpu(cell[count - 1]);
104 } else {
105 priv->default_level = index;
106 priv->max_level = 255;
107 }
Simon Glass7cf208d2017-06-12 06:21:37 -0600108 debug("%s: done\n", __func__);
Simon Glass65fba592016-01-21 19:44:57 -0700109
110
111 return 0;
112}
113
114static int pwm_backlight_probe(struct udevice *dev)
115{
116 return 0;
117}
118
119static const struct backlight_ops pwm_backlight_ops = {
120 .enable = pwm_backlight_enable,
121};
122
123static const struct udevice_id pwm_backlight_ids[] = {
124 { .compatible = "pwm-backlight" },
125 { }
126};
127
128U_BOOT_DRIVER(pwm_backlight) = {
129 .name = "pwm_backlight",
130 .id = UCLASS_PANEL_BACKLIGHT,
131 .of_match = pwm_backlight_ids,
132 .ops = &pwm_backlight_ops,
133 .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata,
134 .probe = pwm_backlight_probe,
135 .priv_auto_alloc_size = sizeof(struct pwm_backlight_priv),
136};