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