1 // SPDX-License-Identifier: GPL-2.0
3 * DRM driver for MIPI DBI compatible display panels
5 * Copyright 2022 Noralf Trønnes
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>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_client_setup.h>
19 #include <drm/drm_drv.h>
20 #include <drm/drm_fbdev_dma.h>
21 #include <drm/drm_gem_atomic_helper.h>
22 #include <drm/drm_gem_dma_helper.h>
23 #include <drm/drm_managed.h>
24 #include <drm/drm_mipi_dbi.h>
25 #include <drm/drm_modes.h>
26 #include <drm/drm_modeset_helper.h>
28 #include <video/mipi_display.h>
30 struct panel_mipi_dbi_format {
36 static const struct panel_mipi_dbi_format panel_mipi_dbi_formats[] = {
37 { "r5g6b5", DRM_FORMAT_RGB565, 16 },
38 { "b6x2g6x2r6x2", DRM_FORMAT_RGB888, 24 },
41 static int panel_mipi_dbi_get_format(struct device *dev, u32 *formats, unsigned int *bpp)
43 const char *format_name;
47 formats[1] = DRM_FORMAT_XRGB8888;
49 ret = device_property_read_string(dev, "format", &format_name);
51 /* Old Device Trees don't have this property */
52 formats[0] = DRM_FORMAT_RGB565;
57 for (i = 0; i < ARRAY_SIZE(panel_mipi_dbi_formats); i++) {
58 const struct panel_mipi_dbi_format *format = &panel_mipi_dbi_formats[i];
60 if (strcmp(format_name, format->name))
63 formats[0] = format->fourcc;
68 dev_err(dev, "Pixel format is not supported: '%s'\n", format_name);
73 static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
74 0, 0, 0, 0, 0, 0, 0 };
77 * The display controller configuration is stored in a firmware file.
78 * The Device Tree 'compatible' property value with a '.bin' suffix is passed
79 * to request_firmware() to fetch this file.
81 struct panel_mipi_dbi_config {
82 /* Magic string: panel_mipi_dbi_magic */
85 /* Config file format version */
86 u8 file_format_version;
89 * MIPI commands to execute when the display pipeline is enabled.
90 * This is used to configure the display controller.
92 * The commands are stored in a byte array with the format:
93 * command, num_parameters, [ parameter, ...], command, ...
95 * Some commands require a pause before the next command can be received.
96 * Inserting a delay in the command sequence is done by using the NOP command with one
97 * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display
98 * Command Set where it has no parameters).
103 * command 0xb1 parameters 0x01, 0x2c, 0x2d
109 * 0xb1 0x03 0x01 0x2c 0x2d
115 struct panel_mipi_dbi_commands {
120 static struct panel_mipi_dbi_commands *
121 panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
123 const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data;
124 struct panel_mipi_dbi_commands *commands;
125 size_t size = fw->size, commands_len;
128 if (size < sizeof(*config) + 2) { /* At least 1 command */
129 dev_err(dev, "config: file size=%zu is too small\n", size);
130 return ERR_PTR(-EINVAL);
133 if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) {
134 dev_err(dev, "config: Bad magic: %15ph\n", config->magic);
135 return ERR_PTR(-EINVAL);
138 if (config->file_format_version != 1) {
139 dev_err(dev, "config: version=%u is not supported\n", config->file_format_version);
140 return ERR_PTR(-EINVAL);
143 drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
145 commands_len = size - sizeof(*config);
147 while ((i + 1) < commands_len) {
148 u8 command = config->commands[i++];
149 u8 num_parameters = config->commands[i++];
150 const u8 *parameters = &config->commands[i];
153 if (i > commands_len) {
154 dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n",
155 command, num_parameters);
156 return ERR_PTR(-EINVAL);
159 if (command == 0x00 && num_parameters == 1)
160 drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
162 drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
163 command, num_parameters, parameters);
166 if (i != commands_len) {
167 dev_err(dev, "config: malformed command array\n");
168 return ERR_PTR(-EINVAL);
171 commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
173 return ERR_PTR(-ENOMEM);
175 commands->len = commands_len;
176 commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL);
178 return ERR_PTR(-ENOMEM);
183 static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
185 struct panel_mipi_dbi_commands *commands;
186 const struct firmware *fw;
187 const char *compatible;
191 ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
195 snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
196 ret = request_firmware(&fw, fw_name, dev);
198 dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
204 commands = panel_mipi_dbi_check_commands(dev, fw);
205 release_firmware(fw);
210 static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
211 struct panel_mipi_dbi_commands *commands)
218 while (i < commands->len) {
219 u8 command = commands->buf[i++];
220 u8 num_parameters = commands->buf[i++];
221 const u8 *parameters = &commands->buf[i];
223 if (command == 0x00 && num_parameters == 1)
224 msleep(parameters[0]);
225 else if (num_parameters)
226 mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters);
228 mipi_dbi_command(dbi, command);
234 static void panel_mipi_dbi_enable(struct drm_simple_display_pipe *pipe,
235 struct drm_crtc_state *crtc_state,
236 struct drm_plane_state *plane_state)
238 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
239 struct mipi_dbi *dbi = &dbidev->dbi;
242 if (!drm_dev_enter(pipe->crtc.dev, &idx))
245 drm_dbg(pipe->crtc.dev, "\n");
247 ret = mipi_dbi_poweron_conditional_reset(dbidev);
251 panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
253 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
258 static const struct drm_simple_display_pipe_funcs panel_mipi_dbi_pipe_funcs = {
259 DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(panel_mipi_dbi_enable),
262 DEFINE_DRM_GEM_DMA_FOPS(panel_mipi_dbi_fops);
264 static const struct drm_driver panel_mipi_dbi_driver = {
265 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
266 .fops = &panel_mipi_dbi_fops,
267 DRM_GEM_DMA_DRIVER_OPS_VMAP,
268 DRM_FBDEV_DMA_DRIVER_OPS,
269 .debugfs_init = mipi_dbi_debugfs_init,
270 .name = "panel-mipi-dbi",
271 .desc = "MIPI DBI compatible display panel",
277 static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
279 struct device *dev = dbidev->drm.dev;
280 u16 hback_porch, vback_porch;
283 ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
285 dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
289 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
291 hback_porch = mode->htotal - mode->hsync_end;
292 vback_porch = mode->vtotal - mode->vsync_end;
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.
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);
306 /* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
308 mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
310 dbidev->top_offset = vback_porch;
311 dbidev->left_offset = hback_porch;
316 static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
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;
329 dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm);
331 return PTR_ERR(dbidev);
336 ret = panel_mipi_dbi_get_mode(dbidev, &mode);
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");
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");
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");
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");
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);
361 return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
363 ret = mipi_dbi_spi_init(spi, dbi, dc);
367 if (device_property_present(dev, "write-only"))
368 dbi->read_commands = NULL;
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);
374 ret = panel_mipi_dbi_get_format(dev, formats, &bpp);
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),
385 drm_mode_config_reset(drm);
387 ret = drm_dev_register(drm, 0);
391 spi_set_drvdata(spi, drm);
393 drm_client_setup(drm, NULL);
398 static void panel_mipi_dbi_spi_remove(struct spi_device *spi)
400 struct drm_device *drm = spi_get_drvdata(spi);
403 drm_atomic_helper_shutdown(drm);
406 static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
408 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
411 static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
413 return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
416 static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
418 drm_mode_config_helper_resume(dev_get_drvdata(dev));
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)
427 static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
428 { .compatible = "panel-mipi-dbi-spi" },
431 MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
433 static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
434 { "panel-mipi-dbi-spi", 0 },
437 MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
439 static struct spi_driver panel_mipi_dbi_spi_driver = {
441 .name = "panel-mipi-dbi-spi",
442 .of_match_table = panel_mipi_dbi_spi_of_match,
443 .pm = &panel_mipi_dbi_pm_ops,
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,
450 module_spi_driver(panel_mipi_dbi_spi_driver);
452 MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
453 MODULE_AUTHOR("Noralf Trønnes");
454 MODULE_LICENSE("GPL");