1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
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 
14 struct 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 
25 static int pwm_backlight_enable(struct udevice *dev)
26 {
27 	struct pwm_backlight_priv *priv = dev_get_priv(dev);
28 	struct dm_regulator_uclass_platdata *plat;
29 	uint duty_cycle;
30 	int ret;
31 
32 	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);
43 	}
44 
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 
60 static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
61 {
62 	struct pwm_backlight_priv *priv = dev_get_priv(dev);
63 	struct ofnode_phandle_args args;
64 	int index, ret, count, len;
65 	const u32 *cell;
66 
67 	debug("%s: start\n", __func__);
68 	ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
69 					   "power-supply", &priv->reg);
70 	if (ret)
71 		debug("%s: Cannot get power supply: ret=%d\n", __func__, ret);
72 	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 	}
80 	ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
81 					 &args);
82 	if (ret) {
83 		debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
84 		return ret;
85 	}
86 
87 	ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
88 	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 
95 	index = dev_read_u32_default(dev, "default-brightness-level", 255);
96 	cell = dev_read_prop(dev, "brightness-levels", &len);
97 	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 	}
105 	debug("%s: done\n", __func__);
106 
107 
108 	return 0;
109 }
110 
111 static int pwm_backlight_probe(struct udevice *dev)
112 {
113 	return 0;
114 }
115 
116 static const struct backlight_ops pwm_backlight_ops = {
117 	.enable	= pwm_backlight_enable,
118 };
119 
120 static const struct udevice_id pwm_backlight_ids[] = {
121 	{ .compatible = "pwm-backlight" },
122 	{ }
123 };
124 
125 U_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 };
134