xref: /linux/drivers/gpu/drm/tiny/panel-mipi-dbi.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DRM driver for MIPI DBI compatible display panels
4  *
5  * Copyright 2022 Noralf Trønnes
6  */
7 
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/firmware.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/property.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/spi/spi.h>
17 
18 #include <drm/clients/drm_client_setup.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_drv.h>
21 #include <drm/drm_fbdev_dma.h>
22 #include <drm/drm_gem_atomic_helper.h>
23 #include <drm/drm_gem_dma_helper.h>
24 #include <drm/drm_managed.h>
25 #include <drm/drm_mipi_dbi.h>
26 #include <drm/drm_modes.h>
27 #include <drm/drm_modeset_helper.h>
28 #include <drm/drm_print.h>
29 
30 #include <video/mipi_display.h>
31 
32 struct panel_mipi_dbi_format {
33 	const char *name;
34 	u32 fourcc;
35 	unsigned int bpp;
36 };
37 
38 static const struct panel_mipi_dbi_format panel_mipi_dbi_formats[] = {
39 	{ "r5g6b5", DRM_FORMAT_RGB565, 16 },
40 	{ "b6x2g6x2r6x2", DRM_FORMAT_RGB888, 24 },
41 };
42 
43 static int panel_mipi_dbi_get_format(struct device *dev, u32 *formats, unsigned int *bpp)
44 {
45 	const char *format_name;
46 	unsigned int i;
47 	int ret;
48 
49 	formats[1] = DRM_FORMAT_XRGB8888;
50 
51 	ret = device_property_read_string(dev, "format", &format_name);
52 	if (ret) {
53 		/* Old Device Trees don't have this property */
54 		formats[0] = DRM_FORMAT_RGB565;
55 		*bpp = 16;
56 		return 0;
57 	}
58 
59 	for (i = 0; i < ARRAY_SIZE(panel_mipi_dbi_formats); i++) {
60 		const struct panel_mipi_dbi_format *format = &panel_mipi_dbi_formats[i];
61 
62 		if (strcmp(format_name, format->name))
63 			continue;
64 
65 		formats[0] = format->fourcc;
66 		*bpp = format->bpp;
67 		return 0;
68 	}
69 
70 	dev_err(dev, "Pixel format is not supported: '%s'\n", format_name);
71 
72 	return -EINVAL;
73 }
74 
75 static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
76 					     0, 0, 0, 0, 0, 0, 0 };
77 
78 /*
79  * The display controller configuration is stored in a firmware file.
80  * The Device Tree 'compatible' property value with a '.bin' suffix is passed
81  * to request_firmware() to fetch this file.
82  */
83 struct panel_mipi_dbi_config {
84 	/* Magic string: panel_mipi_dbi_magic */
85 	u8 magic[15];
86 
87 	/* Config file format version */
88 	u8 file_format_version;
89 
90 	/*
91 	 * MIPI commands to execute when the display pipeline is enabled.
92 	 * This is used to configure the display controller.
93 	 *
94 	 * The commands are stored in a byte array with the format:
95 	 *     command, num_parameters, [ parameter, ...], command, ...
96 	 *
97 	 * Some commands require a pause before the next command can be received.
98 	 * Inserting a delay in the command sequence is done by using the NOP command with one
99 	 * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display
100 	 * Command Set where it has no parameters).
101 	 *
102 	 * Example:
103 	 *     command 0x11
104 	 *     sleep 120ms
105 	 *     command 0xb1 parameters 0x01, 0x2c, 0x2d
106 	 *     command 0x29
107 	 *
108 	 * Byte sequence:
109 	 *     0x11 0x00
110 	 *     0x00 0x01 0x78
111 	 *     0xb1 0x03 0x01 0x2c 0x2d
112 	 *     0x29 0x00
113 	 */
114 	u8 commands[];
115 };
116 
117 struct panel_mipi_dbi_commands {
118 	const u8 *buf;
119 	size_t len;
120 };
121 
122 static struct panel_mipi_dbi_commands *
123 panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
124 {
125 	const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data;
126 	struct panel_mipi_dbi_commands *commands;
127 	size_t size = fw->size, commands_len;
128 	unsigned int i = 0;
129 
130 	if (size < sizeof(*config) + 2) { /* At least 1 command */
131 		dev_err(dev, "config: file size=%zu is too small\n", size);
132 		return ERR_PTR(-EINVAL);
133 	}
134 
135 	if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) {
136 		dev_err(dev, "config: Bad magic: %15ph\n", config->magic);
137 		return ERR_PTR(-EINVAL);
138 	}
139 
140 	if (config->file_format_version != 1) {
141 		dev_err(dev, "config: version=%u is not supported\n", config->file_format_version);
142 		return ERR_PTR(-EINVAL);
143 	}
144 
145 	drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
146 
147 	commands_len = size - sizeof(*config);
148 
149 	while ((i + 1) < commands_len) {
150 		u8 command = config->commands[i++];
151 		u8 num_parameters = config->commands[i++];
152 		const u8 *parameters = &config->commands[i];
153 
154 		i += num_parameters;
155 		if (i > commands_len) {
156 			dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n",
157 				command, num_parameters);
158 			return ERR_PTR(-EINVAL);
159 		}
160 
161 		if (command == 0x00 && num_parameters == 1)
162 			drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
163 		else
164 			drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
165 				    command, num_parameters, parameters);
166 	}
167 
168 	if (i != commands_len) {
169 		dev_err(dev, "config: malformed command array\n");
170 		return ERR_PTR(-EINVAL);
171 	}
172 
173 	commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
174 	if (!commands)
175 		return ERR_PTR(-ENOMEM);
176 
177 	commands->len = commands_len;
178 	commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL);
179 	if (!commands->buf)
180 		return ERR_PTR(-ENOMEM);
181 
182 	return commands;
183 }
184 
185 static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
186 {
187 	struct panel_mipi_dbi_commands *commands;
188 	const struct firmware *fw;
189 	const char *compatible;
190 	char fw_name[40];
191 	int ret;
192 
193 	ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
194 	if (ret)
195 		return ERR_PTR(ret);
196 
197 	snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
198 	ret = request_firmware(&fw, fw_name, dev);
199 	if (ret) {
200 		dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
201 			compatible, ret);
202 
203 		return ERR_PTR(ret);
204 	}
205 
206 	commands = panel_mipi_dbi_check_commands(dev, fw);
207 	release_firmware(fw);
208 
209 	return commands;
210 }
211 
212 static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
213 					    struct panel_mipi_dbi_commands *commands)
214 {
215 	unsigned int i = 0;
216 
217 	if (!commands)
218 		return;
219 
220 	while (i < commands->len) {
221 		u8 command = commands->buf[i++];
222 		u8 num_parameters = commands->buf[i++];
223 		const u8 *parameters = &commands->buf[i];
224 
225 		if (command == 0x00 && num_parameters == 1)
226 			msleep(parameters[0]);
227 		else if (num_parameters)
228 			mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters);
229 		else
230 			mipi_dbi_command(dbi, command);
231 
232 		i += num_parameters;
233 	}
234 }
235 
236 static void panel_mipi_dbi_enable(struct drm_simple_display_pipe *pipe,
237 				  struct drm_crtc_state *crtc_state,
238 				  struct drm_plane_state *plane_state)
239 {
240 	struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
241 	struct mipi_dbi *dbi = &dbidev->dbi;
242 	int ret, idx;
243 
244 	if (!drm_dev_enter(pipe->crtc.dev, &idx))
245 		return;
246 
247 	drm_dbg(pipe->crtc.dev, "\n");
248 
249 	ret = mipi_dbi_poweron_conditional_reset(dbidev);
250 	if (ret < 0)
251 		goto out_exit;
252 	if (!ret)
253 		panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
254 
255 	mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
256 out_exit:
257 	drm_dev_exit(idx);
258 }
259 
260 static const struct drm_simple_display_pipe_funcs panel_mipi_dbi_pipe_funcs = {
261 	DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(panel_mipi_dbi_enable),
262 };
263 
264 DEFINE_DRM_GEM_DMA_FOPS(panel_mipi_dbi_fops);
265 
266 static const struct drm_driver panel_mipi_dbi_driver = {
267 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
268 	.fops			= &panel_mipi_dbi_fops,
269 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
270 	DRM_FBDEV_DMA_DRIVER_OPS,
271 	.debugfs_init		= mipi_dbi_debugfs_init,
272 	.name			= "panel-mipi-dbi",
273 	.desc			= "MIPI DBI compatible display panel",
274 	.major			= 1,
275 	.minor			= 0,
276 };
277 
278 static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
279 {
280 	struct device *dev = dbidev->drm.dev;
281 	u16 hback_porch, vback_porch;
282 	int ret;
283 
284 	ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
285 	if (ret) {
286 		dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
287 		return ret;
288 	}
289 
290 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
291 
292 	hback_porch = mode->htotal - mode->hsync_end;
293 	vback_porch = mode->vtotal - mode->vsync_end;
294 
295 	/*
296 	 * Make sure width and height are set and that only back porch and
297 	 * pixelclock are set in the other timing values. Also check that
298 	 * width and height don't exceed the 16-bit value specified by MIPI DCS.
299 	 */
300 	if (!mode->hdisplay || !mode->vdisplay || mode->flags ||
301 	    mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff ||
302 	    mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) {
303 		dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node);
304 		return -EINVAL;
305 	}
306 
307 	/* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
308 	if (!mode->clock)
309 		mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
310 
311 	dbidev->top_offset = vback_porch;
312 	dbidev->left_offset = hback_porch;
313 
314 	return 0;
315 }
316 
317 static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
318 {
319 	struct device *dev = &spi->dev;
320 	struct drm_display_mode mode;
321 	struct mipi_dbi_dev *dbidev;
322 	struct drm_device *drm;
323 	struct mipi_dbi *dbi;
324 	struct gpio_desc *dc;
325 	unsigned int bpp;
326 	size_t buf_size;
327 	u32 formats[2];
328 	int ret;
329 
330 	dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm);
331 	if (IS_ERR(dbidev))
332 		return PTR_ERR(dbidev);
333 
334 	dbi = &dbidev->dbi;
335 	drm = &dbidev->drm;
336 
337 	ret = panel_mipi_dbi_get_mode(dbidev, &mode);
338 	if (ret)
339 		return ret;
340 
341 	dbidev->regulator = devm_regulator_get(dev, "power");
342 	if (IS_ERR(dbidev->regulator))
343 		return dev_err_probe(dev, PTR_ERR(dbidev->regulator),
344 				     "Failed to get regulator 'power'\n");
345 
346 	dbidev->io_regulator = devm_regulator_get(dev, "io");
347 	if (IS_ERR(dbidev->io_regulator))
348 		return dev_err_probe(dev, PTR_ERR(dbidev->io_regulator),
349 				     "Failed to get regulator 'io'\n");
350 
351 	dbidev->backlight = devm_of_find_backlight(dev);
352 	if (IS_ERR(dbidev->backlight))
353 		return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n");
354 
355 	dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
356 	if (IS_ERR(dbi->reset))
357 		return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
358 
359 	/* Multiple panels can share the "dc" GPIO, but only if they are on the same SPI bus! */
360 	dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
361 	if (IS_ERR(dc))
362 		return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
363 
364 	ret = mipi_dbi_spi_init(spi, dbi, dc);
365 	if (ret)
366 		return ret;
367 
368 	if (device_property_present(dev, "write-only"))
369 		dbi->read_commands = NULL;
370 
371 	dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev);
372 	if (IS_ERR(dbidev->driver_private))
373 		return PTR_ERR(dbidev->driver_private);
374 
375 	ret = panel_mipi_dbi_get_format(dev, formats, &bpp);
376 	if (ret)
377 		return ret;
378 
379 	buf_size = DIV_ROUND_UP(mode.hdisplay * mode.vdisplay * bpp, 8);
380 	ret = mipi_dbi_dev_init_with_formats(dbidev, &panel_mipi_dbi_pipe_funcs,
381 					     formats, ARRAY_SIZE(formats),
382 					     &mode, 0, buf_size);
383 	if (ret)
384 		return ret;
385 
386 	drm_mode_config_reset(drm);
387 
388 	ret = drm_dev_register(drm, 0);
389 	if (ret)
390 		return ret;
391 
392 	spi_set_drvdata(spi, drm);
393 
394 	if (bpp == 16)
395 		drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB565);
396 	else
397 		drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB888);
398 
399 	return 0;
400 }
401 
402 static void panel_mipi_dbi_spi_remove(struct spi_device *spi)
403 {
404 	struct drm_device *drm = spi_get_drvdata(spi);
405 
406 	drm_dev_unplug(drm);
407 	drm_atomic_helper_shutdown(drm);
408 }
409 
410 static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
411 {
412 	drm_atomic_helper_shutdown(spi_get_drvdata(spi));
413 }
414 
415 static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
416 {
417 	return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
418 }
419 
420 static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
421 {
422 	drm_mode_config_helper_resume(dev_get_drvdata(dev));
423 
424 	return 0;
425 }
426 
427 static const struct dev_pm_ops panel_mipi_dbi_pm_ops = {
428 	SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume)
429 };
430 
431 static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
432 	{ .compatible = "panel-mipi-dbi-spi" },
433 	{},
434 };
435 MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
436 
437 static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
438 	{ "panel-mipi-dbi-spi", 0 },
439 	{ },
440 };
441 MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
442 
443 static struct spi_driver panel_mipi_dbi_spi_driver = {
444 	.driver = {
445 		.name = "panel-mipi-dbi-spi",
446 		.of_match_table = panel_mipi_dbi_spi_of_match,
447 		.pm = &panel_mipi_dbi_pm_ops,
448 	},
449 	.id_table = panel_mipi_dbi_spi_id,
450 	.probe = panel_mipi_dbi_spi_probe,
451 	.remove = panel_mipi_dbi_spi_remove,
452 	.shutdown = panel_mipi_dbi_spi_shutdown,
453 };
454 module_spi_driver(panel_mipi_dbi_spi_driver);
455 
456 MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
457 MODULE_AUTHOR("Noralf Trønnes");
458 MODULE_LICENSE("GPL");
459