blob: fbd7bf783891655f3bedc10333d7f978ce0cdd0f [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
Simon Glass7cf208d2017-06-12 06:21:37 -060035 plat = dev_get_uclass_platdata(priv->reg);
36 debug("%s: Enable '%s', regulator '%s'/'%s'\n", __func__, dev->name,
37 priv->reg->name, plat->name);
Simon Glass65fba592016-01-21 19:44:57 -070038 ret = regulator_set_enable(priv->reg, true);
39 if (ret) {
40 debug("%s: Cannot enable regulator for PWM '%s'\n", __func__,
41 dev->name);
42 return ret;
43 }
44 mdelay(120);
45
46 duty_cycle = priv->period_ns * (priv->default_level - priv->min_level) /
47 (priv->max_level - priv->min_level + 1);
48 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
49 duty_cycle);
50 if (ret)
51 return ret;
52 ret = pwm_set_enable(priv->pwm, priv->channel, true);
53 if (ret)
54 return ret;
55 mdelay(10);
56 dm_gpio_set_value(&priv->enable, 1);
57
58 return 0;
59}
60
61static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
62{
63 struct pwm_backlight_priv *priv = dev_get_priv(dev);
Simon Glass7cf208d2017-06-12 06:21:37 -060064 struct ofnode_phandle_args args;
Simon Glass65fba592016-01-21 19:44:57 -070065 int index, ret, count, len;
66 const u32 *cell;
67
Simon Glass7cf208d2017-06-12 06:21:37 -060068 debug("%s: start\n", __func__);
Simon Glass65fba592016-01-21 19:44:57 -070069 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
70 "power-supply", &priv->reg);
71 if (ret) {
72 debug("%s: Cannot get power supply: ret=%d\n", __func__, ret);
73 return ret;
74 }
75 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};