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 <backlight.h>
8 #include <dm.h>
9 #include <edid.h>
10 #include <i2c.h>
11 #include <log.h>
12 #include <mipi_dsi.h>
13 #include <panel.h>
14 #include <asm/gpio.h>
15 #include <power/regulator.h>
16 
17 #define EDID_I2C_ADDR	0x50
18 
19 struct simple_panel_priv {
20 	struct udevice *reg;
21 	struct udevice *backlight;
22 	struct gpio_desc enable;
23 };
24 
simple_panel_enable_backlight(struct udevice * dev)25 static int simple_panel_enable_backlight(struct udevice *dev)
26 {
27 	struct simple_panel_priv *priv = dev_get_priv(dev);
28 	int ret;
29 
30 	debug("%s: start, backlight = '%s'\n", __func__, priv->backlight->name);
31 	dm_gpio_set_value(&priv->enable, 1);
32 	ret = backlight_enable(priv->backlight);
33 	debug("%s: done, ret = %d\n", __func__, ret);
34 	if (ret)
35 		return ret;
36 
37 	return 0;
38 }
39 
simple_panel_set_backlight(struct udevice * dev,int percent)40 static int simple_panel_set_backlight(struct udevice *dev, int percent)
41 {
42 	struct simple_panel_priv *priv = dev_get_priv(dev);
43 	int ret;
44 
45 	debug("%s: start, backlight = '%s'\n", __func__, priv->backlight->name);
46 	dm_gpio_set_value(&priv->enable, 1);
47 	ret = backlight_set_brightness(priv->backlight, percent);
48 	debug("%s: done, ret = %d\n", __func__, ret);
49 	if (ret)
50 		return ret;
51 
52 	return 0;
53 }
54 
55 #if CONFIG_IS_ENABLED(I2C_EDID) && CONFIG_IS_ENABLED(DM_I2C)
simple_panel_get_edid_timing(struct udevice * dev,struct display_timing * timings)56 static int simple_panel_get_edid_timing(struct udevice *dev,
57 					struct display_timing *timings)
58 {
59 	struct udevice *panel_ddc, *panel_edid;
60 	struct display_timing edid_timing;
61 	u8 edid_buf[EDID_SIZE] = { 0 };
62 	int ret, bpc;
63 	/* Check for DDC i2c if no timings are provided */
64 	ret = uclass_get_device_by_phandle(UCLASS_I2C, dev,
65 					   "ddc-i2c-bus",
66 					   &panel_ddc);
67 	if (ret) {
68 		log_debug("%s: cannot get DDC i2c bus: error %d\n",
69 			  __func__, ret);
70 		return ret;
71 	}
72 
73 	ret = dm_i2c_probe(panel_ddc, EDID_I2C_ADDR, 0, &panel_edid);
74 	if (ret) {
75 		log_debug("%s: cannot probe EDID: error %d\n",
76 			  __func__, ret);
77 		return ret;
78 	}
79 
80 	ret = dm_i2c_read(panel_edid, 0, edid_buf, sizeof(edid_buf));
81 	if (ret) {
82 		log_debug("%s: cannot dump EDID buffer: error %d\n",
83 			  __func__, ret);
84 		return ret;
85 	}
86 
87 	ret = edid_get_timing(edid_buf, sizeof(edid_buf),
88 			      &edid_timing, &bpc);
89 	if (ret) {
90 		log_debug("%s: cannot decode EDID info: error %d\n",
91 			  __func__, ret);
92 		return ret;
93 	}
94 
95 	memcpy(timings, &edid_timing, sizeof(*timings));
96 
97 	return 0;
98 }
99 #else
simple_panel_get_edid_timing(struct udevice * dev,struct display_timing * timings)100 static int simple_panel_get_edid_timing(struct udevice *dev,
101 					struct display_timing *timings)
102 {
103 	return -ENOTSUPP;
104 }
105 #endif
106 
simple_panel_get_display_timing(struct udevice * dev,struct display_timing * timings)107 static int simple_panel_get_display_timing(struct udevice *dev,
108 					   struct display_timing *timings)
109 {
110 	const void *blob = gd->fdt_blob;
111 	int ret;
112 
113 	/* Check for timing subnode if panel node first */
114 	ret = fdtdec_decode_display_timing(blob, dev_of_offset(dev),
115 					   0, timings);
116 	if (!ret)
117 		return ret;
118 
119 	return simple_panel_get_edid_timing(dev, timings);
120 }
121 
simple_panel_of_to_plat(struct udevice * dev)122 static int simple_panel_of_to_plat(struct udevice *dev)
123 {
124 	struct simple_panel_priv *priv = dev_get_priv(dev);
125 	int ret;
126 
127 	if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
128 		ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
129 						   "power-supply", &priv->reg);
130 		if (ret) {
131 			debug("%s: Warning: cannot get power supply: ret=%d\n",
132 			      __func__, ret);
133 			if (ret != -ENOENT)
134 				return ret;
135 		}
136 	}
137 
138 	ret = uclass_get_device_by_phandle(UCLASS_PANEL_BACKLIGHT, dev,
139 						   "backlight", &priv->backlight);
140 	if (ret) {
141 		debug("%s: Cannot get backlight: ret=%d\n", __func__, ret);
142 		if (ret != -ENOENT)
143 			return log_ret(ret);
144 	}
145 
146 	ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
147 				   GPIOD_IS_OUT);
148 	if (ret) {
149 		debug("%s: Warning: cannot get enable GPIO: ret=%d\n",
150 		      __func__, ret);
151 		if (ret != -ENOENT)
152 			return log_ret(ret);
153 	}
154 
155 	return 0;
156 }
157 
simple_panel_probe(struct udevice * dev)158 static int simple_panel_probe(struct udevice *dev)
159 {
160 	struct simple_panel_priv *priv = dev_get_priv(dev);
161 	struct mipi_dsi_panel_plat *plat = dev_get_plat(dev);
162 	struct mipi_dsi_panel_plat *dsi_data =
163 		(struct mipi_dsi_panel_plat *)dev_get_driver_data(dev);
164 	int ret;
165 
166 	ret = regulator_set_enable_if_allowed(priv->reg, true);
167 	if (ret && ret != -ENOSYS) {
168 		debug("%s: failed to enable regulator '%s' %d\n",
169 		      __func__, priv->reg->name, ret);
170 		return ret;
171 	}
172 
173 	if (dsi_data)
174 		memcpy(plat, dsi_data, sizeof(struct mipi_dsi_panel_plat));
175 
176 	return 0;
177 }
178 
179 static const struct panel_ops simple_panel_ops = {
180 	.enable_backlight	= simple_panel_enable_backlight,
181 	.set_backlight		= simple_panel_set_backlight,
182 	.get_display_timing	= simple_panel_get_display_timing,
183 };
184 
185 static const struct mipi_dsi_panel_plat panasonic_vvx10f004b00 = {
186 	.mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
187 		      MIPI_DSI_CLOCK_NON_CONTINUOUS,
188 	.format = MIPI_DSI_FMT_RGB888,
189 	.lanes = 4,
190 };
191 
192 static const struct udevice_id simple_panel_ids[] = {
193 	{ .compatible = "simple-panel" },
194 	{ .compatible = "auo,b133xtn01" },
195 	{ .compatible = "auo,b116xw03" },
196 	{ .compatible = "auo,b133htn01" },
197 	{ .compatible = "boe,nv140fhmn49" },
198 	{ .compatible = "lg,lb070wv8" },
199 	{ .compatible = "sharp,lq123p1jx31" },
200 	{ .compatible = "boe,nv101wxmn51" },
201 	{ .compatible = "panasonic,vvx10f004b00",
202 	  .data = (ulong)&panasonic_vvx10f004b00 },
203 	{ }
204 };
205 
206 U_BOOT_DRIVER(simple_panel) = {
207 	.name		= "simple_panel",
208 	.id		= UCLASS_PANEL,
209 	.of_match	= simple_panel_ids,
210 	.ops		= &simple_panel_ops,
211 	.of_to_plat	= simple_panel_of_to_plat,
212 	.probe		= simple_panel_probe,
213 	.priv_auto	= sizeof(struct simple_panel_priv),
214 	.plat_auto	= sizeof(struct mipi_dsi_panel_plat),
215 };
216