bcm27xx: switch to kernel v6.1
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.15 / 950-0795-drm-tiny-Add-MIPI-DBI-compatible-SPI-driver.patch
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 commit 0e65e2e upstream.
10
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.
16
17 Example (driver will load sainsmart18.bin):
18
19 display@0 {
20 compatible = "sainsmart18", "panel-mipi-dbi-spi";
21 ...
22 };
23
24 v5:
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
35 a DCS NOP.
36
37 v4:
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.
40
41 v3:
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
46
47 v2:
48 - Drop model property and use compatible instead (Rob)
49 - Add wiki entry in MAINTAINERS
50
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
56 ---
57 MAINTAINERS | 8 +
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
63
64 --- a/MAINTAINERS
65 +++ b/MAINTAINERS
66 @@ -5980,6 +5980,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
69
70 +DRM DRIVER FOR MIPI DBI compatible panels
71 +M: Noralf Trønnes <noralf@tronnes.org>
72 +S: Maintained
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
77 +
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.
86
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
92 + select DRM_MIPI_DBI
93 + select BACKLIGHT_CLASS_DEVICE
94 + select VIDEOMODE_HELPERS
95 + help
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.
101 +
102 config DRM_SIMPLEDRM
103 tristate "Simple framebuffer driver"
104 depends on DRM
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
115 --- /dev/null
116 +++ b/drivers/gpu/drm/tiny/panel-mipi-dbi.c
117 @@ -0,0 +1,398 @@
118 +// SPDX-License-Identifier: GPL-2.0
119 +/*
120 + * DRM driver for MIPI DBI compatible display panels
121 + *
122 + * Copyright 2022 Noralf Trønnes
123 + */
124 +
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>
133 +
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>
143 +
144 +#include <video/mipi_display.h>
145 +
146 +static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I',
147 + 0, 0, 0, 0, 0, 0, 0 };
148 +
149 +/*
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.
153 + */
154 +struct panel_mipi_dbi_config {
155 + /* Magic string: panel_mipi_dbi_magic */
156 + u8 magic[15];
157 +
158 + /* Config file format version */
159 + u8 file_format_version;
160 +
161 + /*
162 + * MIPI commands to execute when the display pipeline is enabled.
163 + * This is used to configure the display controller.
164 + *
165 + * The commands are stored in a byte array with the format:
166 + * command, num_parameters, [ parameter, ...], command, ...
167 + *
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).
172 + *
173 + * Example:
174 + * command 0x11
175 + * sleep 120ms
176 + * command 0xb1 parameters 0x01, 0x2c, 0x2d
177 + * command 0x29
178 + *
179 + * Byte sequence:
180 + * 0x11 0x00
181 + * 0x00 0x01 0x78
182 + * 0xb1 0x03 0x01 0x2c 0x2d
183 + * 0x29 0x00
184 + */
185 + u8 commands[];
186 +};
187 +
188 +struct panel_mipi_dbi_commands {
189 + const u8 *buf;
190 + size_t len;
191 +};
192 +
193 +static struct panel_mipi_dbi_commands *
194 +panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw)
195 +{
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;
200 +
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);
204 + }
205 +
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);
209 + }
210 +
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);
214 + }
215 +
216 + drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version);
217 +
218 + commands_len = size - sizeof(*config);
219 +
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];
224 +
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);
230 + }
231 +
232 + if (command == 0x00 && num_parameters == 1)
233 + drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]);
234 + else
235 + drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n",
236 + command, num_parameters, parameters);
237 + }
238 +
239 + if (i != commands_len) {
240 + dev_err(dev, "config: malformed command array\n");
241 + return ERR_PTR(-EINVAL);
242 + }
243 +
244 + commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL);
245 + if (!commands)
246 + return ERR_PTR(-ENOMEM);
247 +
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);
252 +
253 + return commands;
254 +}
255 +
256 +static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev)
257 +{
258 + struct panel_mipi_dbi_commands *commands;
259 + const struct firmware *fw;
260 + const char *compatible;
261 + char fw_name[40];
262 + int ret;
263 +
264 + ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible);
265 + if (ret)
266 + return ERR_PTR(ret);
267 +
268 + snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible);
269 + ret = request_firmware(&fw, fw_name, dev);
270 + if (ret) {
271 + dev_err(dev, "No config file found for compatible '%s' (error=%d)\n",
272 + compatible, ret);
273 +
274 + return ERR_PTR(ret);
275 + }
276 +
277 + commands = panel_mipi_dbi_check_commands(dev, fw);
278 + release_firmware(fw);
279 +
280 + return commands;
281 +}
282 +
283 +static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi,
284 + struct panel_mipi_dbi_commands *commands)
285 +{
286 + unsigned int i = 0;
287 +
288 + if (!commands)
289 + return;
290 +
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];
295 +
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);
300 + else
301 + mipi_dbi_command(dbi, command);
302 +
303 + i += num_parameters;
304 + }
305 +}
306 +
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)
310 +{
311 + struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev);
312 + struct mipi_dbi *dbi = &dbidev->dbi;
313 + int ret, idx;
314 +
315 + if (!drm_dev_enter(pipe->crtc.dev, &idx))
316 + return;
317 +
318 + drm_dbg(pipe->crtc.dev, "\n");
319 +
320 + ret = mipi_dbi_poweron_conditional_reset(dbidev);
321 + if (ret < 0)
322 + goto out_exit;
323 + if (!ret)
324 + panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private);
325 +
326 + mipi_dbi_enable_flush(dbidev, crtc_state, plane_state);
327 +out_exit:
328 + drm_dev_exit(idx);
329 +}
330 +
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,
335 +};
336 +
337 +DEFINE_DRM_GEM_CMA_FOPS(panel_mipi_dbi_fops);
338 +
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",
347 + .major = 1,
348 + .minor = 0,
349 +};
350 +
351 +static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode)
352 +{
353 + struct device *dev = dbidev->drm.dev;
354 + u16 hback_porch, vback_porch;
355 + int ret;
356 +
357 + ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL);
358 + if (ret) {
359 + dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret);
360 + return ret;
361 + }
362 +
363 + mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
364 +
365 + hback_porch = mode->htotal - mode->hsync_end;
366 + vback_porch = mode->vtotal - mode->vsync_end;
367 +
368 + /*
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.
372 + */
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);
377 + return -EINVAL;
378 + }
379 +
380 + /* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */
381 + if (!mode->clock)
382 + mode->clock = mode->htotal * mode->vtotal * 60 / 1000;
383 +
384 + dbidev->top_offset = vback_porch;
385 + dbidev->left_offset = hback_porch;
386 +
387 + return 0;
388 +}
389 +
390 +static int panel_mipi_dbi_spi_probe(struct spi_device *spi)
391 +{
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;
398 + int ret;
399 +
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);
403 +
404 + dbi = &dbidev->dbi;
405 + drm = &dbidev->drm;
406 +
407 + ret = panel_mipi_dbi_get_mode(dbidev, &mode);
408 + if (ret)
409 + return ret;
410 +
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");
415 +
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");
419 +
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");
423 +
424 + dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW);
425 + if (IS_ERR(dc))
426 + return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n");
427 +
428 + ret = mipi_dbi_spi_init(spi, dbi, dc);
429 + if (ret)
430 + return ret;
431 +
432 + if (device_property_present(dev, "write-only"))
433 + dbi->read_commands = NULL;
434 +
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);
438 +
439 + ret = mipi_dbi_dev_init(dbidev, &panel_mipi_dbi_pipe_funcs, &mode, 0);
440 + if (ret)
441 + return ret;
442 +
443 + drm_mode_config_reset(drm);
444 +
445 + ret = drm_dev_register(drm, 0);
446 + if (ret)
447 + return ret;
448 +
449 + spi_set_drvdata(spi, drm);
450 +
451 + drm_fbdev_generic_setup(drm, 0);
452 +
453 + return 0;
454 +}
455 +
456 +static int panel_mipi_dbi_spi_remove(struct spi_device *spi)
457 +{
458 + struct drm_device *drm = spi_get_drvdata(spi);
459 +
460 + drm_dev_unplug(drm);
461 + drm_atomic_helper_shutdown(drm);
462 +
463 + return 0;
464 +}
465 +
466 +static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi)
467 +{
468 + drm_atomic_helper_shutdown(spi_get_drvdata(spi));
469 +}
470 +
471 +static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev)
472 +{
473 + return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
474 +}
475 +
476 +static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev)
477 +{
478 + drm_mode_config_helper_resume(dev_get_drvdata(dev));
479 +
480 + return 0;
481 +}
482 +
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)
485 +};
486 +
487 +static const struct of_device_id panel_mipi_dbi_spi_of_match[] = {
488 + { .compatible = "panel-mipi-dbi-spi" },
489 + {},
490 +};
491 +MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match);
492 +
493 +static const struct spi_device_id panel_mipi_dbi_spi_id[] = {
494 + { "panel-mipi-dbi-spi", 0 },
495 + { },
496 +};
497 +MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id);
498 +
499 +static struct spi_driver panel_mipi_dbi_spi_driver = {
500 + .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,
505 + },
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,
510 +};
511 +module_spi_driver(panel_mipi_dbi_spi_driver);
512 +
513 +MODULE_DESCRIPTION("MIPI DBI compatible display panel driver");
514 +MODULE_AUTHOR("Noralf Trønnes");
515 +MODULE_LICENSE("GPL");