1 From e73c6348ebeeb60bc92b316904c7ae5ca7b68fe1 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= <noralf@tronnes.org>
3 Date: Sun, 27 Feb 2022 13:47:13 +0100
4 Subject: [PATCH] drm/tiny: Add MIPI DBI compatible SPI driver
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 commit 0e65e2e upstream.
11 Add a driver that will work with most MIPI DBI compatible SPI panels.
12 This avoids adding a driver for every new MIPI DBI compatible controller
13 that is to be used by Linux. The 'compatible' Device Tree property with
14 a '.bin' suffix will be used to load a firmware file that contains the
15 controller configuration.
17 Example (driver will load sainsmart18.bin):
20 compatible = "sainsmart18", "panel-mipi-dbi-spi";
25 - kconfig: s/DRM_KMS_CMA_HELPER/DRM_GEM_CMA_HELPER/ (Sam)
26 - kconfig: Add select VIDEOMODE_HELPERS (Sam)
27 - kconfig: Add wiki url in the description (Sam)
28 - Split out and use of_get_drm_panel_display_mode()(Sam)
29 - Only use the first compatible to look for a firmware file since the
30 binding mandates 2 compatibles.
31 - Make having a firmware file mandatory so we can print an error
32 message if it's missing to improve the user experience. It's very
33 unlikely that a controller doesn't need to be initialized and if
34 it doesn't, it's possible to have a firmware file containing only
38 - Move driver to drm/tiny where the other drivers of its kind are located.
39 The driver module will not be shared with a future DPI driver after all.
42 - Move properties to DT (Maxime)
43 - The MIPI DPI spec has optional support for DPI where the controller is
44 configured over DBI. Rework the command functions so they can be moved
45 to drm_mipi_dbi and shared with a future panel-mipi-dpi-spi driver
48 - Drop model property and use compatible instead (Rob)
49 - Add wiki entry in MAINTAINERS
51 Acked-by: Maxime Ripard <maxime@cerno.tech>
52 Reviewed-by: Sam Ravnborg <sam@ravnborg.org>
53 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
54 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
55 Link: https://patchwork.freedesktop.org/patch/msgid/20220227124713.39766-6-noralf@tronnes.org
58 drivers/gpu/drm/tiny/Kconfig | 15 +
59 drivers/gpu/drm/tiny/Makefile | 1 +
60 drivers/gpu/drm/tiny/panel-mipi-dbi.c | 398 ++++++++++++++++++++++++++
61 4 files changed, 422 insertions(+)
62 create mode 100644 drivers/gpu/drm/tiny/panel-mipi-dbi.c
66 @@ -5981,6 +5981,14 @@ T: git git://anongit.freedesktop.org/drm
67 F: Documentation/devicetree/bindings/display/multi-inno,mi0283qt.txt
68 F: drivers/gpu/drm/tiny/mi0283qt.c
70 +DRM DRIVER FOR MIPI DBI compatible panels
71 +M: Noralf Trønnes <noralf@tronnes.org>
73 +W: https://github.com/notro/panel-mipi-dbi/wiki
74 +T: git git://anongit.freedesktop.org/drm/drm-misc
75 +F: Documentation/devicetree/bindings/display/panel/panel-mipi-dbi-spi.yaml
76 +F: drivers/gpu/drm/tiny/panel-mipi-dbi.c
78 DRM DRIVER FOR MSM ADRENO GPU
79 M: Rob Clark <robdclark@gmail.com>
80 M: Sean Paul <sean@poorly.run>
81 --- a/drivers/gpu/drm/tiny/Kconfig
82 +++ b/drivers/gpu/drm/tiny/Kconfig
83 @@ -51,6 +51,21 @@ config DRM_GM12U320
84 This is a KMS driver for projectors which use the GM12U320 chipset
85 for video transfer over USB2/3, such as the Acer C120 mini projector.
87 +config DRM_PANEL_MIPI_DBI
88 + tristate "DRM support for MIPI DBI compatible panels"
89 + depends on DRM && SPI
90 + select DRM_KMS_HELPER
91 + select DRM_GEM_CMA_HELPER
93 + select BACKLIGHT_CLASS_DEVICE
94 + select VIDEOMODE_HELPERS
96 + Say Y here if you want to enable support for MIPI DBI compatible
97 + panels. The controller command setup can be provided using a
98 + firmware file. For more information see
99 + https://github.com/notro/panel-mipi-dbi/wiki.
100 + To compile this driver as a module, choose M here.
103 tristate "Simple framebuffer driver"
105 --- a/drivers/gpu/drm/tiny/Makefile
106 +++ b/drivers/gpu/drm/tiny/Makefile
107 @@ -4,6 +4,7 @@ obj-$(CONFIG_DRM_ARCPGU) += arcpgu.o
108 obj-$(CONFIG_DRM_BOCHS) += bochs.o
109 obj-$(CONFIG_DRM_CIRRUS_QEMU) += cirrus.o
110 obj-$(CONFIG_DRM_GM12U320) += gm12u320.o
111 +obj-$(CONFIG_DRM_PANEL_MIPI_DBI) += panel-mipi-dbi.o
112 obj-$(CONFIG_DRM_SIMPLEDRM) += simpledrm.o
113 obj-$(CONFIG_TINYDRM_HX8357D) += hx8357d.o
114 obj-$(CONFIG_TINYDRM_ILI9225) += ili9225.o
116 +++ b/drivers/gpu/drm/tiny/panel-mipi-dbi.c
118 +// SPDX-License-Identifier: GPL-2.0
120 + * DRM driver for MIPI DBI compatible display panels
122 + * Copyright 2022 Noralf Trønnes
125 +#include <linux/backlight.h>
126 +#include <linux/delay.h>
127 +#include <linux/firmware.h>
128 +#include <linux/gpio/consumer.h>
129 +#include <linux/module.h>
130 +#include <linux/property.h>
131 +#include <linux/regulator/consumer.h>
132 +#include <linux/spi/spi.h>
134 +#include <drm/drm_atomic_helper.h>
135 +#include <drm/drm_drv.h>
136 +#include <drm/drm_fb_helper.h>
137 +#include <drm/drm_gem_atomic_helper.h>
138 +#include <drm/drm_gem_cma_helper.h>
139 +#include <drm/drm_managed.h>
140 +#include <drm/drm_mipi_dbi.h>
141 +#include <drm/drm_modes.h>
142 +#include <drm/drm_modeset_helper.h>
144 +#include <video/mipi_display.h>
146 +static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
147 + 0, 0, 0, 0, 0, 0, 0 };
150 + * The display controller configuration is stored in a firmware file.
151 + * The Device Tree 'compatible' property value with a '.bin' suffix is passed
152 + * to request_firmware() to fetch this file.
154 +struct panel_mipi_dbi_config {
155 + /* Magic string: panel_mipi_dbi_magic */
158 + /* Config file format version */
159 + u8 file_format_version;
162 + * MIPI commands to execute when the display pipeline is enabled.
163 + * This is used to configure the display controller.
165 + * The commands are stored in a byte array with the format:
166 + * command, num_parameters, [ parameter, ...], command, ...
168 + * Some commands require a pause before the next command can be received.
169 + * Inserting a delay in the command sequence is done by using the NOP command with one
170 + * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display
171 + * Command Set where it has no parameters).
176 + * command 0xb1 parameters 0x01, 0x2c, 0x2d
182 + * 0xb1 0x03 0x01 0x2c 0x2d
188 +struct panel_mipi_dbi_commands {
193 +static struct panel_mipi_dbi_commands *
194 +panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
196 + const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data;
197 + struct panel_mipi_dbi_commands *commands;
198 + size_t size = fw->size, commands_len;
199 + unsigned int i = 0;
201 + if (size < sizeof(*config) + 2) { /* At least 1 command */
202 + dev_err(dev, "config: file size=%zu is too small\n", size);
203 + return ERR_PTR(-EINVAL);
206 + if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) {
207 + dev_err(dev, "config: Bad magic: %15ph\n", config->magic);
208 + return ERR_PTR(-EINVAL);
211 + if (config->file_format_version != 1) {
212 + dev_err(dev, "config: version=%u is not supported\n", config->file_format_version);
213 + return ERR_PTR(-EINVAL);
216 + drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
218 + commands_len = size - sizeof(*config);
220 + while ((i + 1) < commands_len) {
221 + u8 command = config->commands[i++];
222 + u8 num_parameters = config->commands[i++];
223 + const u8 *parameters = &config->commands[i];
225 + i += num_parameters;
226 + if (i > commands_len) {
227 + dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n",
228 + command, num_parameters);
229 + return ERR_PTR(-EINVAL);
232 + if (command == 0x00 && num_parameters == 1)
233 + drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
235 + drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
236 + command, num_parameters, parameters);
239 + if (i != commands_len) {
240 + dev_err(dev, "config: malformed command array\n");
241 + return ERR_PTR(-EINVAL);
244 + commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
246 + return ERR_PTR(-ENOMEM);
248 + commands->len = commands_len;
249 + commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL);
250 + if (!commands->buf)
251 + return ERR_PTR(-ENOMEM);
256 +static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
258 + struct panel_mipi_dbi_commands *commands;
259 + const struct firmware *fw;
260 + const char *compatible;
264 + ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
266 + return ERR_PTR(ret);
268 + snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
269 + ret = request_firmware(&fw, fw_name, dev);
271 + dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
274 + return ERR_PTR(ret);
277 + commands = panel_mipi_dbi_check_commands(dev, fw);
278 + release_firmware(fw);
283 +static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
284 + struct panel_mipi_dbi_commands *commands)
286 + unsigned int i = 0;
291 + while (i < commands->len) {
292 + u8 command = commands->buf[i++];
293 + u8 num_parameters = commands->buf[i++];
294 + const u8 *parameters = &commands->buf[i];
296 + if (command == 0x00 && num_parameters == 1)
297 + msleep(parameters[0]);
298 + else if (num_parameters)
299 + mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters);
301 + mipi_dbi_command(dbi, command);
303 + i += num_parameters;
307 +static void panel_mipi_dbi_enable(struct drm_simple_display_pipe *pipe,
308 + struct drm_crtc_state *crtc_state,
309 + struct drm_plane_state *plane_state)
311 + struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
312 + struct mipi_dbi *dbi = &dbidev->dbi;
315 + if (!drm_dev_enter(pipe->crtc.dev, &idx))
318 + drm_dbg(pipe->crtc.dev, "\n");
320 + ret = mipi_dbi_poweron_conditional_reset(dbidev);
324 + panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
326 + mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
331 +static const struct drm_simple_display_pipe_funcs panel_mipi_dbi_pipe_funcs = {
332 + .enable = panel_mipi_dbi_enable,
333 + .disable = mipi_dbi_pipe_disable,
334 + .update = mipi_dbi_pipe_update,
337 +DEFINE_DRM_GEM_CMA_FOPS(panel_mipi_dbi_fops);
339 +static const struct drm_driver panel_mipi_dbi_driver = {
340 + .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
341 + .fops = &panel_mipi_dbi_fops,
342 + DRM_GEM_CMA_DRIVER_OPS_VMAP,
343 + .debugfs_init = mipi_dbi_debugfs_init,
344 + .name = "panel-mipi-dbi",
345 + .desc = "MIPI DBI compatible display panel",
346 + .date = "20220103",
351 +static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
353 + struct device *dev = dbidev->drm.dev;
354 + u16 hback_porch, vback_porch;
357 + ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
359 + dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
363 + mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
365 + hback_porch = mode->htotal - mode->hsync_end;
366 + vback_porch = mode->vtotal - mode->vsync_end;
369 + * Make sure width and height are set and that only back porch and
370 + * pixelclock are set in the other timing values. Also check that
371 + * width and height don't exceed the 16-bit value specified by MIPI DCS.
373 + if (!mode->hdisplay || !mode->vdisplay || mode->flags ||
374 + mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff ||
375 + mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) {
376 + dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node);
380 + /* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
382 + mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
384 + dbidev->top_offset = vback_porch;
385 + dbidev->left_offset = hback_porch;
390 +static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
392 + struct device *dev = &spi->dev;
393 + struct drm_display_mode mode;
394 + struct mipi_dbi_dev *dbidev;
395 + struct drm_device *drm;
396 + struct mipi_dbi *dbi;
397 + struct gpio_desc *dc;
400 + dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm);
401 + if (IS_ERR(dbidev))
402 + return PTR_ERR(dbidev);
404 + dbi = &dbidev->dbi;
405 + drm = &dbidev->drm;
407 + ret = panel_mipi_dbi_get_mode(dbidev, &mode);
411 + dbidev->regulator = devm_regulator_get(dev, "power");
412 + if (IS_ERR(dbidev->regulator))
413 + return dev_err_probe(dev, PTR_ERR(dbidev->regulator),
414 + "Failed to get regulator 'power'\n");
416 + dbidev->backlight = devm_of_find_backlight(dev);
417 + if (IS_ERR(dbidev->backlight))
418 + return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n");
420 + dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
421 + if (IS_ERR(dbi->reset))
422 + return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n");
424 + dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
426 + return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
428 + ret = mipi_dbi_spi_init(spi, dbi, dc);
432 + if (device_property_present(dev, "write-only"))
433 + dbi->read_commands = NULL;
435 + dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev);
436 + if (IS_ERR(dbidev->driver_private))
437 + return PTR_ERR(dbidev->driver_private);
439 + ret = mipi_dbi_dev_init(dbidev, &panel_mipi_dbi_pipe_funcs, &mode, 0);
443 + drm_mode_config_reset(drm);
445 + ret = drm_dev_register(drm, 0);
449 + spi_set_drvdata(spi, drm);
451 + drm_fbdev_generic_setup(drm, 0);
456 +static int panel_mipi_dbi_spi_remove(struct spi_device *spi)
458 + struct drm_device *drm = spi_get_drvdata(spi);
460 + drm_dev_unplug(drm);
461 + drm_atomic_helper_shutdown(drm);
466 +static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
468 + drm_atomic_helper_shutdown(spi_get_drvdata(spi));
471 +static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
473 + return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
476 +static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
478 + drm_mode_config_helper_resume(dev_get_drvdata(dev));
483 +static const struct dev_pm_ops panel_mipi_dbi_pm_ops = {
484 + SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume)
487 +static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
488 + { .compatible = "panel-mipi-dbi-spi" },
491 +MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
493 +static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
494 + { "panel-mipi-dbi-spi", 0 },
497 +MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
499 +static struct spi_driver panel_mipi_dbi_spi_driver = {
501 + .name = "panel-mipi-dbi-spi",
502 + .owner = THIS_MODULE,
503 + .of_match_table = panel_mipi_dbi_spi_of_match,
504 + .pm = &panel_mipi_dbi_pm_ops,
506 + .id_table = panel_mipi_dbi_spi_id,
507 + .probe = panel_mipi_dbi_spi_probe,
508 + .remove = panel_mipi_dbi_spi_remove,
509 + .shutdown = panel_mipi_dbi_spi_shutdown,
511 +module_spi_driver(panel_mipi_dbi_spi_driver);
513 +MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
514 +MODULE_AUTHOR("Noralf Trønnes");
515 +MODULE_LICENSE("GPL");