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