blob: 53953179bfbdf93aa4d87ab415661eb4620669a5 [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
7#include <common.h>
8#include <dm.h>
9#include <backlight.h>
10#include <pwm.h>
11#include <asm/gpio.h>
12#include <power/regulator.h>
13
Simon Glass65fba592016-01-21 19:44:57 -070014struct pwm_backlight_priv {
15 struct udevice *reg;
16 struct gpio_desc enable;
17 struct udevice *pwm;
18 uint channel;
19 uint period_ns;
20 uint default_level;
21 uint min_level;
22 uint max_level;
23};
24
25static int pwm_backlight_enable(struct udevice *dev)
26{
27 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -060028 struct dm_regulator_uclass_platdata *plat;
Simon Glass65fba592016-01-21 19:44:57 -070029 uint duty_cycle;
30 int ret;
31
Kever Yang19f124d2018-02-09 10:45:12 +080032 if (priv->reg) {
33 plat = dev_get_uclass_platdata(priv->reg);
34 debug("%s: Enable '%s', regulator '%s'/'%s'\n", __func__,
35 dev->name, priv->reg->name, plat->name);
36 ret = regulator_set_enable(priv->reg, true);
37 if (ret) {
38 debug("%s: Cannot enable regulator for PWM '%s'\n",
39 __func__, dev->name);
40 return ret;
41 }
42 mdelay(120);
Simon Glass65fba592016-01-21 19:44:57 -070043 }
Simon Glass65fba592016-01-21 19:44:57 -070044
45 duty_cycle = priv->period_ns * (priv->default_level - priv->min_level) /
46 (priv->max_level - priv->min_level + 1);
47 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
48 duty_cycle);
49 if (ret)
50 return ret;
51 ret = pwm_set_enable(priv->pwm, priv->channel, true);
52 if (ret)
53 return ret;
54 mdelay(10);
55 dm_gpio_set_value(&priv->enable, 1);
56
57 return 0;
58}
59
60static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
61{
62 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -060063 struct ofnode_phandle_args args;
Simon Glass65fba592016-01-21 19:44:57 -070064 int index, ret, count, len;
65 const u32 *cell;
66
Simon Glass7cf208d2017-06-12 06:21:37 -060067 debug("%s: start\n", __func__);
Simon Glass65fba592016-01-21 19:44:57 -070068 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
69 "power-supply", &priv->reg);
Kever Yang19f124d2018-02-09 10:45:12 +080070 if (ret)
Simon Glass65fba592016-01-21 19:44:57 -070071 debug("%s: Cannot get power supply: ret=%d\n", __func__, ret);
Simon Glass65fba592016-01-21 19:44:57 -070072 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
73 GPIOD_IS_OUT);
74 if (ret) {
75 debug("%s: Warning: cannot get enable GPIO: ret=%d\n",
76 __func__, ret);
77 if (ret != -ENOENT)
78 return ret;
79 }
Simon Glass7cf208d2017-06-12 06:21:37 -060080 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
81 &args);
Simon Glass65fba592016-01-21 19:44:57 -070082 if (ret) {
83 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
84 return ret;
85 }
86
Simon Glass7cf208d2017-06-12 06:21:37 -060087 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
Simon Glass65fba592016-01-21 19:44:57 -070088 if (ret) {
89 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
90 return ret;
91 }
92 priv->channel = args.args[0];
93 priv->period_ns = args.args[1];
94
Simon Glass7cf208d2017-06-12 06:21:37 -060095 index = dev_read_u32_default(dev, "default-brightness-level", 255);
96 cell = dev_read_prop(dev, "brightness-levels", &len);
Simon Glass65fba592016-01-21 19:44:57 -070097 count = len / sizeof(u32);
98 if (cell && count > index) {
99 priv->default_level = fdt32_to_cpu(cell[index]);
100 priv->max_level = fdt32_to_cpu(cell[count - 1]);
101 } else {
102 priv->default_level = index;
103 priv->max_level = 255;
104 }
Simon Glass7cf208d2017-06-12 06:21:37 -0600105 debug("%s: done\n", __func__);
Simon Glass65fba592016-01-21 19:44:57 -0700106
107
108 return 0;
109}
110
111static int pwm_backlight_probe(struct udevice *dev)
112{
113 return 0;
114}
115
116static const struct backlight_ops pwm_backlight_ops = {
117 .enable = pwm_backlight_enable,
118};
119
120static const struct udevice_id pwm_backlight_ids[] = {
121 { .compatible = "pwm-backlight" },
122 { }
123};
124
125U_BOOT_DRIVER(pwm_backlight) = {
126 .name = "pwm_backlight",
127 .id = UCLASS_PANEL_BACKLIGHT,
128 .of_match = pwm_backlight_ids,
129 .ops = &pwm_backlight_ops,
130 .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata,
131 .probe = pwm_backlight_probe,
132 .priv_auto_alloc_size = sizeof(struct pwm_backlight_priv),
133};