xref: /linux/drivers/gpu/drm/panel/panel-raydium-rm69380.c (revision 3f41368fbfe1b3d5922d317fe1a0a0cab6846802)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree.
4  * Copyright (c) 2024 David Wronek <david@mainlining.org>
5  */
6 
7 #include <linux/backlight.h>
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_device.h>
13 #include <linux/of_graph.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include <video/mipi_display.h>
17 
18 #include <drm/drm_mipi_dsi.h>
19 #include <drm/drm_modes.h>
20 #include <drm/drm_panel.h>
21 #include <drm/drm_probe_helper.h>
22 
23 struct rm69380_panel {
24 	struct drm_panel panel;
25 	struct mipi_dsi_device *dsi[2];
26 	struct regulator_bulk_data supplies[2];
27 	struct gpio_desc *reset_gpio;
28 };
29 
30 static inline
31 struct rm69380_panel *to_rm69380_panel(struct drm_panel *panel)
32 {
33 	return container_of(panel, struct rm69380_panel, panel);
34 }
35 
36 static void rm69380_reset(struct rm69380_panel *ctx)
37 {
38 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
39 	usleep_range(15000, 16000);
40 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
41 	usleep_range(10000, 11000);
42 	gpiod_set_value_cansleep(ctx->reset_gpio, 0);
43 	msleep(30);
44 }
45 
46 static int rm69380_on(struct rm69380_panel *ctx)
47 {
48 	struct mipi_dsi_device *dsi = ctx->dsi[0];
49 	struct device *dev = &dsi->dev;
50 	int ret;
51 
52 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
53 	if (ctx->dsi[1])
54 		ctx->dsi[1]->mode_flags |= MIPI_DSI_MODE_LPM;
55 
56 	mipi_dsi_dcs_write_seq(dsi, 0xfe, 0xd4);
57 	mipi_dsi_dcs_write_seq(dsi, 0x00, 0x80);
58 	mipi_dsi_dcs_write_seq(dsi, 0xfe, 0xd0);
59 	mipi_dsi_dcs_write_seq(dsi, 0x48, 0x00);
60 	mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x26);
61 	mipi_dsi_dcs_write_seq(dsi, 0x75, 0x3f);
62 	mipi_dsi_dcs_write_seq(dsi, 0x1d, 0x1a);
63 	mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x00);
64 	mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x28);
65 	mipi_dsi_dcs_write_seq(dsi, 0xc2, 0x08);
66 
67 	ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK);
68 	if (ret < 0) {
69 		dev_err(dev, "Failed to set tear on: %d\n", ret);
70 		return ret;
71 	}
72 
73 	ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
74 	if (ret < 0) {
75 		dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
76 		return ret;
77 	}
78 	msleep(20);
79 
80 	ret = mipi_dsi_dcs_set_display_on(dsi);
81 	if (ret < 0) {
82 		dev_err(dev, "Failed to set display on: %d\n", ret);
83 		return ret;
84 	}
85 	msleep(36);
86 
87 	return 0;
88 }
89 
90 static int rm69380_off(struct rm69380_panel *ctx)
91 {
92 	struct mipi_dsi_device *dsi = ctx->dsi[0];
93 	struct device *dev = &dsi->dev;
94 	int ret;
95 
96 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
97 	if (ctx->dsi[1])
98 		ctx->dsi[1]->mode_flags &= ~MIPI_DSI_MODE_LPM;
99 
100 	ret = mipi_dsi_dcs_set_display_off(dsi);
101 	if (ret < 0) {
102 		dev_err(dev, "Failed to set display off: %d\n", ret);
103 		return ret;
104 	}
105 	msleep(35);
106 
107 	ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
108 	if (ret < 0) {
109 		dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
110 		return ret;
111 	}
112 	msleep(20);
113 
114 	return 0;
115 }
116 
117 static int rm69380_prepare(struct drm_panel *panel)
118 {
119 	struct rm69380_panel *ctx = to_rm69380_panel(panel);
120 	struct device *dev = &ctx->dsi[0]->dev;
121 	int ret;
122 
123 	ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
124 	if (ret < 0) {
125 		dev_err(dev, "Failed to enable regulators: %d\n", ret);
126 		return ret;
127 	}
128 
129 	rm69380_reset(ctx);
130 
131 	ret = rm69380_on(ctx);
132 	if (ret < 0) {
133 		dev_err(dev, "Failed to initialize panel: %d\n", ret);
134 		gpiod_set_value_cansleep(ctx->reset_gpio, 1);
135 		regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
136 		return ret;
137 	}
138 
139 	return 0;
140 }
141 
142 static int rm69380_unprepare(struct drm_panel *panel)
143 {
144 	struct rm69380_panel *ctx = to_rm69380_panel(panel);
145 	struct device *dev = &ctx->dsi[0]->dev;
146 	int ret;
147 
148 	ret = rm69380_off(ctx);
149 	if (ret < 0)
150 		dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
151 
152 	gpiod_set_value_cansleep(ctx->reset_gpio, 1);
153 	regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
154 
155 	return 0;
156 }
157 
158 static const struct drm_display_mode rm69380_mode = {
159 	.clock = (2560 + 32 + 12 + 38) * (1600 + 20 + 4 + 8) * 90 / 1000,
160 	.hdisplay = 2560,
161 	.hsync_start = 2560 + 32,
162 	.hsync_end = 2560 + 32 + 12,
163 	.htotal = 2560 + 32 + 12 + 38,
164 	.vdisplay = 1600,
165 	.vsync_start = 1600 + 20,
166 	.vsync_end = 1600 + 20 + 4,
167 	.vtotal = 1600 + 20 + 4 + 8,
168 	.width_mm = 248,
169 	.height_mm = 155,
170 	.type = DRM_MODE_TYPE_DRIVER,
171 };
172 
173 static int rm69380_get_modes(struct drm_panel *panel,
174 					struct drm_connector *connector)
175 {
176 	return drm_connector_helper_get_modes_fixed(connector, &rm69380_mode);
177 }
178 
179 static const struct drm_panel_funcs rm69380_panel_funcs = {
180 	.prepare = rm69380_prepare,
181 	.unprepare = rm69380_unprepare,
182 	.get_modes = rm69380_get_modes,
183 };
184 
185 static int rm69380_bl_update_status(struct backlight_device *bl)
186 {
187 	struct mipi_dsi_device *dsi = bl_get_data(bl);
188 	u16 brightness = backlight_get_brightness(bl);
189 	int ret;
190 
191 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
192 
193 	ret = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness);
194 	if (ret < 0)
195 		return ret;
196 
197 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
198 
199 	return 0;
200 }
201 
202 static int rm69380_bl_get_brightness(struct backlight_device *bl)
203 {
204 	struct mipi_dsi_device *dsi = bl_get_data(bl);
205 	u16 brightness;
206 	int ret;
207 
208 	dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
209 
210 	ret = mipi_dsi_dcs_get_display_brightness_large(dsi, &brightness);
211 	if (ret < 0)
212 		return ret;
213 
214 	dsi->mode_flags |= MIPI_DSI_MODE_LPM;
215 
216 	return brightness;
217 }
218 
219 static const struct backlight_ops rm69380_bl_ops = {
220 	.update_status = rm69380_bl_update_status,
221 	.get_brightness = rm69380_bl_get_brightness,
222 };
223 
224 static struct backlight_device *
225 rm69380_create_backlight(struct mipi_dsi_device *dsi)
226 {
227 	struct device *dev = &dsi->dev;
228 	const struct backlight_properties props = {
229 		.type = BACKLIGHT_RAW,
230 		.brightness = 511,
231 		.max_brightness = 2047,
232 	};
233 
234 	return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
235 					      &rm69380_bl_ops, &props);
236 }
237 
238 static int rm69380_probe(struct mipi_dsi_device *dsi)
239 {
240 	struct mipi_dsi_host *dsi_sec_host;
241 	struct rm69380_panel *ctx;
242 	struct device *dev = &dsi->dev;
243 	struct device_node *dsi_sec;
244 	int ret, i;
245 
246 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
247 	if (!ctx)
248 		return -ENOMEM;
249 
250 	ctx->supplies[0].supply = "vddio";
251 	ctx->supplies[1].supply = "avdd";
252 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
253 				      ctx->supplies);
254 	if (ret < 0)
255 		return dev_err_probe(dev, ret, "Failed to get regulators\n");
256 
257 	ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
258 	if (IS_ERR(ctx->reset_gpio))
259 		return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
260 				     "Failed to get reset-gpios\n");
261 
262 	dsi_sec = of_graph_get_remote_node(dsi->dev.of_node, 1, -1);
263 
264 	if (dsi_sec) {
265 		const struct mipi_dsi_device_info info = { "RM69380 DSI1", 0,
266 							   dsi_sec };
267 
268 		dsi_sec_host = of_find_mipi_dsi_host_by_node(dsi_sec);
269 		of_node_put(dsi_sec);
270 		if (!dsi_sec_host)
271 			return dev_err_probe(dev, -EPROBE_DEFER,
272 					     "Cannot get secondary DSI host\n");
273 
274 		ctx->dsi[1] =
275 			devm_mipi_dsi_device_register_full(dev, dsi_sec_host, &info);
276 		if (IS_ERR(ctx->dsi[1]))
277 			return dev_err_probe(dev, PTR_ERR(ctx->dsi[1]),
278 					     "Cannot get secondary DSI node\n");
279 
280 		mipi_dsi_set_drvdata(ctx->dsi[1], ctx);
281 	}
282 
283 	ctx->dsi[0] = dsi;
284 	mipi_dsi_set_drvdata(dsi, ctx);
285 
286 	drm_panel_init(&ctx->panel, dev, &rm69380_panel_funcs,
287 		       DRM_MODE_CONNECTOR_DSI);
288 	ctx->panel.prepare_prev_first = true;
289 
290 	ctx->panel.backlight = rm69380_create_backlight(dsi);
291 	if (IS_ERR(ctx->panel.backlight))
292 		return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
293 				     "Failed to create backlight\n");
294 
295 	drm_panel_add(&ctx->panel);
296 
297 	for (i = 0; i < ARRAY_SIZE(ctx->dsi); i++) {
298 		if (!ctx->dsi[i])
299 			continue;
300 
301 		dev_dbg(&ctx->dsi[i]->dev, "Binding DSI %d\n", i);
302 
303 		ctx->dsi[i]->lanes = 4;
304 		ctx->dsi[i]->format = MIPI_DSI_FMT_RGB888;
305 		ctx->dsi[i]->mode_flags = MIPI_DSI_MODE_VIDEO_BURST |
306 					  MIPI_DSI_CLOCK_NON_CONTINUOUS;
307 
308 		ret = devm_mipi_dsi_attach(dev, ctx->dsi[i]);
309 		if (ret < 0) {
310 			drm_panel_remove(&ctx->panel);
311 			return dev_err_probe(dev, ret,
312 					     "Failed to attach to DSI%d\n", i);
313 		}
314 	}
315 
316 	return 0;
317 }
318 
319 static void rm69380_remove(struct mipi_dsi_device *dsi)
320 {
321 	struct rm69380_panel *ctx = mipi_dsi_get_drvdata(dsi);
322 
323 	drm_panel_remove(&ctx->panel);
324 }
325 
326 static const struct of_device_id rm69380_of_match[] = {
327 	{ .compatible = "lenovo,j716f-edo-rm69380" },
328 	{ /* sentinel */ }
329 };
330 MODULE_DEVICE_TABLE(of, rm69380_of_match);
331 
332 static struct mipi_dsi_driver rm69380_panel_driver = {
333 	.probe = rm69380_probe,
334 	.remove = rm69380_remove,
335 	.driver = {
336 		.name = "panel-raydium-rm69380",
337 		.of_match_table = rm69380_of_match,
338 	},
339 };
340 module_mipi_dsi_driver(rm69380_panel_driver);
341 
342 MODULE_AUTHOR("David Wronek <david@mainlining.org");
343 MODULE_DESCRIPTION("DRM driver for Raydium RM69380-equipped DSI panels");
344 MODULE_LICENSE("GPL");
345