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_drv.h>
19 #include <drm/drm_fbdev_generic.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>
27 #include <video/mipi_display.h>
29 static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
30 0, 0, 0, 0, 0, 0, 0 };
33 * The display controller configuration is stored in a firmware file.
34 * The Device Tree 'compatible' property value with a '.bin' suffix is passed
35 * to request_firmware() to fetch this file.
37 struct panel_mipi_dbi_config {
38 /* Magic string: panel_mipi_dbi_magic */
41 /* Config file format version */
42 u8 file_format_version;
45 * MIPI commands to execute when the display pipeline is enabled.
46 * This is used to configure the display controller.
48 * The commands are stored in a byte array with the format:
49 * command, num_parameters, [ parameter, ...], command, ...
51 * Some commands require a pause before the next command can be received.
52 * Inserting a delay in the command sequence is done by using the NOP command with one
53 * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display
54 * Command Set where it has no parameters).
59 * command 0xb1 parameters 0x01, 0x2c, 0x2d
65 * 0xb1 0x03 0x01 0x2c 0x2d
71 struct panel_mipi_dbi_commands {
76 static struct panel_mipi_dbi_commands *
77 panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
79 const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data;
80 struct panel_mipi_dbi_commands *commands;
81 size_t size = fw->size, commands_len;
84 if (size < sizeof(*config) + 2) { /* At least 1 command */
85 dev_err(dev, "config: file size=%zu is too small\n", size);
86 return ERR_PTR(-EINVAL);
89 if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) {
90 dev_err(dev, "config: Bad magic: %15ph\n", config->magic);
91 return ERR_PTR(-EINVAL);
94 if (config->file_format_version != 1) {
95 dev_err(dev, "config: version=%u is not supported\n", config->file_format_version);
96 return ERR_PTR(-EINVAL);
99 drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
101 commands_len = size - sizeof(*config);
103 while ((i + 1) < commands_len) {
104 u8 command = config->commands[i++];
105 u8 num_parameters = config->commands[i++];
106 const u8 *parameters = &config->commands[i];
109 if (i > commands_len) {
110 dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n",
111 command, num_parameters);
112 return ERR_PTR(-EINVAL);
115 if (command == 0x00 && num_parameters == 1)
116 drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
118 drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
119 command, num_parameters, parameters);
122 if (i != commands_len) {
123 dev_err(dev, "config: malformed command array\n");
124 return ERR_PTR(-EINVAL);
127 commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
129 return ERR_PTR(-ENOMEM);
131 commands->len = commands_len;
132 commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL);
134 return ERR_PTR(-ENOMEM);
139 static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
141 struct panel_mipi_dbi_commands *commands;
142 const struct firmware *fw;
143 const char *compatible;
147 ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
151 snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
152 ret = request_firmware(&fw, fw_name, dev);
154 dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
160 commands = panel_mipi_dbi_check_commands(dev, fw);
161 release_firmware(fw);
166 static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
167 struct panel_mipi_dbi_commands *commands)
174 while (i < commands->len) {
175 u8 command = commands->buf[i++];
176 u8 num_parameters = commands->buf[i++];
177 const u8 *parameters = &commands->buf[i];
179 if (command == 0x00 && num_parameters == 1)
180 msleep(parameters[0]);
181 else if (num_parameters)
182 mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters);
184 mipi_dbi_command(dbi, command);
190 static void panel_mipi_dbi_enable(struct drm_simple_display_pipe *pipe,
191 struct drm_crtc_state *crtc_state,
192 struct drm_plane_state *plane_state)
194 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
195 struct mipi_dbi *dbi = &dbidev->dbi;
198 if (!drm_dev_enter(pipe->crtc.dev, &idx))
201 drm_dbg(pipe->crtc.dev, "\n");
203 ret = mipi_dbi_poweron_conditional_reset(dbidev);
207 panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
209 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
214 static const struct drm_simple_display_pipe_funcs panel_mipi_dbi_pipe_funcs = {
215 DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(panel_mipi_dbi_enable),
218 DEFINE_DRM_GEM_DMA_FOPS(panel_mipi_dbi_fops);
220 static const struct drm_driver panel_mipi_dbi_driver = {
221 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
222 .fops = &panel_mipi_dbi_fops,
223 DRM_GEM_DMA_DRIVER_OPS_VMAP,
224 .debugfs_init = mipi_dbi_debugfs_init,
225 .name = "panel-mipi-dbi",
226 .desc = "MIPI DBI compatible display panel",
232 static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
234 struct device *dev = dbidev->drm.dev;
235 u16 hback_porch, vback_porch;
238 ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
240 dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
244 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
246 hback_porch = mode->htotal - mode->hsync_end;
247 vback_porch = mode->vtotal - mode->vsync_end;
250 * Make sure width and height are set and that only back porch and
251 * pixelclock are set in the other timing values. Also check that
252 * width and height don't exceed the 16-bit value specified by MIPI DCS.
254 if (!mode->hdisplay || !mode->vdisplay || mode->flags ||
255 mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff ||
256 mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) {
257 dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node);
261 /* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
263 mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
265 dbidev->top_offset = vback_porch;
266 dbidev->left_offset = hback_porch;
271 static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
273 struct device *dev = &spi->dev;
274 struct drm_display_mode mode;
275 struct mipi_dbi_dev *dbidev;
276 struct drm_device *drm;
277 struct mipi_dbi *dbi;
278 struct gpio_desc *dc;
281 dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm);
283 return PTR_ERR(dbidev);
288 ret = panel_mipi_dbi_get_mode(dbidev, &mode);
292 dbidev->regulator = devm_regulator_get(dev, "power");
293 if (IS_ERR(dbidev->regulator))
294 return dev_err_probe(dev, PTR_ERR(dbidev->regulator),
295 "Failed to get regulator 'power'\n");
297 dbidev->io_regulator = devm_regulator_get(dev, "io");
298 if (IS_ERR(dbidev->io_regulator))
299 return dev_err_probe(dev, PTR_ERR(dbidev->io_regulator),
300 "Failed to get regulator 'io'\n");
302 dbidev->backlight = devm_of_find_backlight(dev);
303 if (IS_ERR(dbidev->backlight))
304 return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n");
306 dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
307 if (IS_ERR(dbi->reset))
308 return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
310 dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
312 return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
314 ret = mipi_dbi_spi_init(spi, dbi, dc);
318 if (device_property_present(dev, "write-only"))
319 dbi->read_commands = NULL;
321 dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev);
322 if (IS_ERR(dbidev->driver_private))
323 return PTR_ERR(dbidev->driver_private);
325 ret = mipi_dbi_dev_init(dbidev, &panel_mipi_dbi_pipe_funcs, &mode, 0);
329 drm_mode_config_reset(drm);
331 ret = drm_dev_register(drm, 0);
335 spi_set_drvdata(spi, drm);
337 drm_fbdev_generic_setup(drm, 0);
342 static void panel_mipi_dbi_spi_remove(struct spi_device *spi)
344 struct drm_device *drm = spi_get_drvdata(spi);
347 drm_atomic_helper_shutdown(drm);
350 static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
352 drm_atomic_helper_shutdown(spi_get_drvdata(spi));
355 static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
357 return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
360 static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
362 drm_mode_config_helper_resume(dev_get_drvdata(dev));
367 static const struct dev_pm_ops panel_mipi_dbi_pm_ops = {
368 SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume)
371 static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
372 { .compatible = "panel-mipi-dbi-spi" },
375 MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
377 static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
378 { "panel-mipi-dbi-spi", 0 },
381 MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
383 static struct spi_driver panel_mipi_dbi_spi_driver = {
385 .name = "panel-mipi-dbi-spi",
386 .owner = THIS_MODULE,
387 .of_match_table = panel_mipi_dbi_spi_of_match,
388 .pm = &panel_mipi_dbi_pm_ops,
390 .id_table = panel_mipi_dbi_spi_id,
391 .probe = panel_mipi_dbi_spi_probe,
392 .remove = panel_mipi_dbi_spi_remove,
393 .shutdown = panel_mipi_dbi_spi_shutdown,
395 module_spi_driver(panel_mipi_dbi_spi_driver);
397 MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
398 MODULE_AUTHOR("Noralf Trønnes");
399 MODULE_LICENSE("GPL");