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/of.h> 14 #include <linux/property.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/spi/spi.h> 17 18 #include <drm/clients/drm_client_setup.h> 19 #include <drm/drm_atomic_helper.h> 20 #include <drm/drm_drv.h> 21 #include <drm/drm_fbdev_dma.h> 22 #include <drm/drm_gem_atomic_helper.h> 23 #include <drm/drm_gem_dma_helper.h> 24 #include <drm/drm_managed.h> 25 #include <drm/drm_mipi_dbi.h> 26 #include <drm/drm_modes.h> 27 #include <drm/drm_modeset_helper.h> 28 #include <drm/drm_print.h> 29 30 #include <video/mipi_display.h> 31 32 struct panel_mipi_dbi_format { 33 const char *name; 34 u32 fourcc; 35 unsigned int bpp; 36 }; 37 38 static const struct panel_mipi_dbi_format panel_mipi_dbi_formats[] = { 39 { "r5g6b5", DRM_FORMAT_RGB565, 16 }, 40 { "b6x2g6x2r6x2", DRM_FORMAT_RGB888, 24 }, 41 }; 42 43 static int panel_mipi_dbi_get_format(struct device *dev, u32 *formats, unsigned int *bpp) 44 { 45 const char *format_name; 46 unsigned int i; 47 int ret; 48 49 formats[1] = DRM_FORMAT_XRGB8888; 50 51 ret = device_property_read_string(dev, "format", &format_name); 52 if (ret) { 53 /* Old Device Trees don't have this property */ 54 formats[0] = DRM_FORMAT_RGB565; 55 *bpp = 16; 56 return 0; 57 } 58 59 for (i = 0; i < ARRAY_SIZE(panel_mipi_dbi_formats); i++) { 60 const struct panel_mipi_dbi_format *format = &panel_mipi_dbi_formats[i]; 61 62 if (strcmp(format_name, format->name)) 63 continue; 64 65 formats[0] = format->fourcc; 66 *bpp = format->bpp; 67 return 0; 68 } 69 70 dev_err(dev, "Pixel format is not supported: '%s'\n", format_name); 71 72 return -EINVAL; 73 } 74 75 static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I', 76 0, 0, 0, 0, 0, 0, 0 }; 77 78 /* 79 * The display controller configuration is stored in a firmware file. 80 * The Device Tree 'compatible' property value with a '.bin' suffix is passed 81 * to request_firmware() to fetch this file. 82 */ 83 struct panel_mipi_dbi_config { 84 /* Magic string: panel_mipi_dbi_magic */ 85 u8 magic[15]; 86 87 /* Config file format version */ 88 u8 file_format_version; 89 90 /* 91 * MIPI commands to execute when the display pipeline is enabled. 92 * This is used to configure the display controller. 93 * 94 * The commands are stored in a byte array with the format: 95 * command, num_parameters, [ parameter, ...], command, ... 96 * 97 * Some commands require a pause before the next command can be received. 98 * Inserting a delay in the command sequence is done by using the NOP command with one 99 * parameter: delay in miliseconds (the No Operation command is part of the MIPI Display 100 * Command Set where it has no parameters). 101 * 102 * Example: 103 * command 0x11 104 * sleep 120ms 105 * command 0xb1 parameters 0x01, 0x2c, 0x2d 106 * command 0x29 107 * 108 * Byte sequence: 109 * 0x11 0x00 110 * 0x00 0x01 0x78 111 * 0xb1 0x03 0x01 0x2c 0x2d 112 * 0x29 0x00 113 */ 114 u8 commands[]; 115 }; 116 117 struct panel_mipi_dbi_commands { 118 const u8 *buf; 119 size_t len; 120 }; 121 122 static struct panel_mipi_dbi_commands * 123 panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw) 124 { 125 const struct panel_mipi_dbi_config *config = (struct panel_mipi_dbi_config *)fw->data; 126 struct panel_mipi_dbi_commands *commands; 127 size_t size = fw->size, commands_len; 128 unsigned int i = 0; 129 130 if (size < sizeof(*config) + 2) { /* At least 1 command */ 131 dev_err(dev, "config: file size=%zu is too small\n", size); 132 return ERR_PTR(-EINVAL); 133 } 134 135 if (memcmp(config->magic, panel_mipi_dbi_magic, sizeof(config->magic))) { 136 dev_err(dev, "config: Bad magic: %15ph\n", config->magic); 137 return ERR_PTR(-EINVAL); 138 } 139 140 if (config->file_format_version != 1) { 141 dev_err(dev, "config: version=%u is not supported\n", config->file_format_version); 142 return ERR_PTR(-EINVAL); 143 } 144 145 drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version); 146 147 commands_len = size - sizeof(*config); 148 149 while ((i + 1) < commands_len) { 150 u8 command = config->commands[i++]; 151 u8 num_parameters = config->commands[i++]; 152 const u8 *parameters = &config->commands[i]; 153 154 i += num_parameters; 155 if (i > commands_len) { 156 dev_err(dev, "config: command=0x%02x num_parameters=%u overflows\n", 157 command, num_parameters); 158 return ERR_PTR(-EINVAL); 159 } 160 161 if (command == 0x00 && num_parameters == 1) 162 drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]); 163 else 164 drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n", 165 command, num_parameters, parameters); 166 } 167 168 if (i != commands_len) { 169 dev_err(dev, "config: malformed command array\n"); 170 return ERR_PTR(-EINVAL); 171 } 172 173 commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL); 174 if (!commands) 175 return ERR_PTR(-ENOMEM); 176 177 commands->len = commands_len; 178 commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL); 179 if (!commands->buf) 180 return ERR_PTR(-ENOMEM); 181 182 return commands; 183 } 184 185 static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev) 186 { 187 struct panel_mipi_dbi_commands *commands; 188 const struct firmware *fw; 189 const char *compatible; 190 char fw_name[40]; 191 int ret; 192 193 ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible); 194 if (ret) 195 return ERR_PTR(ret); 196 197 snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible); 198 ret = request_firmware(&fw, fw_name, dev); 199 if (ret) { 200 dev_err(dev, "No config file found for compatible '%s' (error=%d)\n", 201 compatible, ret); 202 203 return ERR_PTR(ret); 204 } 205 206 commands = panel_mipi_dbi_check_commands(dev, fw); 207 release_firmware(fw); 208 209 return commands; 210 } 211 212 static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi, 213 struct panel_mipi_dbi_commands *commands) 214 { 215 unsigned int i = 0; 216 217 if (!commands) 218 return; 219 220 while (i < commands->len) { 221 u8 command = commands->buf[i++]; 222 u8 num_parameters = commands->buf[i++]; 223 const u8 *parameters = &commands->buf[i]; 224 225 if (command == 0x00 && num_parameters == 1) 226 msleep(parameters[0]); 227 else if (num_parameters) 228 mipi_dbi_command_stackbuf(dbi, command, parameters, num_parameters); 229 else 230 mipi_dbi_command(dbi, command); 231 232 i += num_parameters; 233 } 234 } 235 236 struct panel_mipi_dbi_device { 237 struct mipi_dbi_dev dbidev; 238 239 struct drm_plane plane; 240 struct drm_crtc crtc; 241 struct drm_encoder encoder; 242 struct drm_connector connector; 243 }; 244 245 static struct panel_mipi_dbi_device *to_panel_mipi_dbi_device(struct drm_device *dev) 246 { 247 return container_of(drm_to_mipi_dbi_dev(dev), struct panel_mipi_dbi_device, dbidev); 248 } 249 250 static const u64 panel_mipi_dbi_plane_format_modifiers[] = { 251 DRM_MIPI_DBI_PLANE_FORMAT_MODIFIERS, 252 }; 253 254 static const struct drm_plane_helper_funcs panel_mipi_dbi_plane_helper_funcs = { 255 DRM_MIPI_DBI_PLANE_HELPER_FUNCS, 256 }; 257 258 static const struct drm_plane_funcs panel_mipi_dbi_plane_funcs = { 259 DRM_MIPI_DBI_PLANE_FUNCS, 260 .destroy = drm_plane_cleanup, 261 }; 262 263 static void panel_mipi_dbi_crtc_helper_atomic_enable(struct drm_crtc *crtc, 264 struct drm_atomic_state *state) 265 { 266 struct drm_device *drm = crtc->dev; 267 struct panel_mipi_dbi_device *panel_mipi_dbi = to_panel_mipi_dbi_device(drm); 268 struct mipi_dbi_dev *dbidev = &panel_mipi_dbi->dbidev; 269 struct mipi_dbi *dbi = &dbidev->dbi; 270 int ret, idx; 271 272 if (!drm_dev_enter(drm, &idx)) 273 return; 274 275 drm_dbg(drm, "\n"); 276 277 ret = mipi_dbi_poweron_conditional_reset(dbidev); 278 if (ret < 0) 279 goto out_exit; 280 if (!ret) 281 panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private); 282 283 backlight_enable(dbidev->backlight); 284 out_exit: 285 drm_dev_exit(idx); 286 } 287 288 static const struct drm_crtc_helper_funcs panel_mipi_dbi_crtc_helper_funcs = { 289 DRM_MIPI_DBI_CRTC_HELPER_FUNCS, 290 .atomic_enable = panel_mipi_dbi_crtc_helper_atomic_enable, 291 }; 292 293 static const struct drm_crtc_funcs panel_mipi_dbi_crtc_funcs = { 294 DRM_MIPI_DBI_CRTC_FUNCS, 295 .destroy = drm_crtc_cleanup, 296 }; 297 298 static const struct drm_encoder_funcs panel_mipi_dbi_encoder_funcs = { 299 .destroy = drm_encoder_cleanup, 300 }; 301 302 static const struct drm_connector_helper_funcs panel_mipi_dbi_connector_helper_funcs = { 303 DRM_MIPI_DBI_CONNECTOR_HELPER_FUNCS, 304 }; 305 306 static const struct drm_connector_funcs panel_mipi_dbi_connector_funcs = { 307 DRM_MIPI_DBI_CONNECTOR_FUNCS, 308 .destroy = drm_connector_cleanup, 309 }; 310 311 static const struct drm_mode_config_helper_funcs panel_mipi_dbi_mode_config_helper_funcs = { 312 DRM_MIPI_DBI_MODE_CONFIG_HELPER_FUNCS, 313 }; 314 315 static const struct drm_mode_config_funcs panel_mipi_dbi_mode_config_funcs = { 316 DRM_MIPI_DBI_MODE_CONFIG_FUNCS, 317 }; 318 319 DEFINE_DRM_GEM_DMA_FOPS(panel_mipi_dbi_fops); 320 321 static const struct drm_driver panel_mipi_dbi_driver = { 322 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 323 .fops = &panel_mipi_dbi_fops, 324 DRM_GEM_DMA_DRIVER_OPS_VMAP, 325 DRM_FBDEV_DMA_DRIVER_OPS, 326 .debugfs_init = mipi_dbi_debugfs_init, 327 .name = "panel-mipi-dbi", 328 .desc = "MIPI DBI compatible display panel", 329 .major = 1, 330 .minor = 0, 331 }; 332 333 static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode) 334 { 335 struct device *dev = dbidev->drm.dev; 336 u16 hback_porch, vback_porch; 337 int ret; 338 339 ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL); 340 if (ret) { 341 dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret); 342 return ret; 343 } 344 345 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 346 347 hback_porch = mode->htotal - mode->hsync_end; 348 vback_porch = mode->vtotal - mode->vsync_end; 349 350 /* 351 * Make sure width and height are set and that only back porch and 352 * pixelclock are set in the other timing values. Also check that 353 * width and height don't exceed the 16-bit value specified by MIPI DCS. 354 */ 355 if (!mode->hdisplay || !mode->vdisplay || mode->flags || 356 mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff || 357 mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) { 358 dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node); 359 return -EINVAL; 360 } 361 362 /* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */ 363 if (!mode->clock) 364 mode->clock = mode->htotal * mode->vtotal * 60 / 1000; 365 366 dbidev->top_offset = vback_porch; 367 dbidev->left_offset = hback_porch; 368 369 return 0; 370 } 371 372 static int panel_mipi_dbi_spi_probe(struct spi_device *spi) 373 { 374 struct device *dev = &spi->dev; 375 struct panel_mipi_dbi_device *panel_mipi_dbi; 376 struct mipi_dbi_dev *dbidev; 377 struct drm_device *drm; 378 struct drm_display_mode mode; 379 struct mipi_dbi *dbi; 380 struct gpio_desc *dc; 381 unsigned int bpp; 382 size_t buf_size; 383 u32 formats[2]; 384 struct drm_plane *plane; 385 struct drm_crtc *crtc; 386 struct drm_encoder *encoder; 387 struct drm_connector *connector; 388 int ret; 389 390 panel_mipi_dbi = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, 391 struct panel_mipi_dbi_device, 392 dbidev.drm); 393 if (IS_ERR(panel_mipi_dbi)) 394 return PTR_ERR(panel_mipi_dbi); 395 dbidev = &panel_mipi_dbi->dbidev; 396 dbi = &dbidev->dbi; 397 drm = &dbidev->drm; 398 399 ret = panel_mipi_dbi_get_mode(dbidev, &mode); 400 if (ret) 401 return ret; 402 403 dbidev->regulator = devm_regulator_get(dev, "power"); 404 if (IS_ERR(dbidev->regulator)) 405 return dev_err_probe(dev, PTR_ERR(dbidev->regulator), 406 "Failed to get regulator 'power'\n"); 407 408 dbidev->io_regulator = devm_regulator_get(dev, "io"); 409 if (IS_ERR(dbidev->io_regulator)) 410 return dev_err_probe(dev, PTR_ERR(dbidev->io_regulator), 411 "Failed to get regulator 'io'\n"); 412 413 dbidev->backlight = devm_of_find_backlight(dev); 414 if (IS_ERR(dbidev->backlight)) 415 return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n"); 416 417 dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 418 if (IS_ERR(dbi->reset)) 419 return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n"); 420 421 /* Multiple panels can share the "dc" GPIO, but only if they are on the same SPI bus! */ 422 dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE); 423 if (IS_ERR(dc)) 424 return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n"); 425 426 ret = mipi_dbi_spi_init(spi, dbi, dc); 427 if (ret) 428 return ret; 429 430 if (device_property_present(dev, "write-only")) 431 dbi->read_commands = NULL; 432 433 dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev); 434 if (IS_ERR(dbidev->driver_private)) 435 return PTR_ERR(dbidev->driver_private); 436 437 ret = panel_mipi_dbi_get_format(dev, formats, &bpp); 438 if (ret) 439 return ret; 440 441 buf_size = DIV_ROUND_UP(mode.hdisplay * mode.vdisplay * bpp, 8); 442 ret = drm_mipi_dbi_dev_init(dbidev, &mode, formats[0], 0, buf_size); 443 if (ret) 444 return ret; 445 446 ret = drmm_mode_config_init(drm); 447 if (ret) 448 return ret; 449 450 drm->mode_config.min_width = dbidev->mode.hdisplay; 451 drm->mode_config.max_width = dbidev->mode.hdisplay; 452 drm->mode_config.min_height = dbidev->mode.vdisplay; 453 drm->mode_config.max_height = dbidev->mode.vdisplay; 454 drm->mode_config.funcs = &panel_mipi_dbi_mode_config_funcs; 455 drm->mode_config.preferred_depth = bpp; 456 drm->mode_config.helper_private = &panel_mipi_dbi_mode_config_helper_funcs; 457 458 plane = &panel_mipi_dbi->plane; 459 ret = drm_universal_plane_init(drm, plane, 0, &panel_mipi_dbi_plane_funcs, 460 formats, ARRAY_SIZE(formats), 461 panel_mipi_dbi_plane_format_modifiers, 462 DRM_PLANE_TYPE_PRIMARY, NULL); 463 if (ret) 464 return ret; 465 drm_plane_helper_add(plane, &panel_mipi_dbi_plane_helper_funcs); 466 drm_plane_enable_fb_damage_clips(plane); 467 468 crtc = &panel_mipi_dbi->crtc; 469 ret = drm_crtc_init_with_planes(drm, crtc, plane, NULL, &panel_mipi_dbi_crtc_funcs, 470 NULL); 471 if (ret) 472 return ret; 473 drm_crtc_helper_add(crtc, &panel_mipi_dbi_crtc_helper_funcs); 474 475 encoder = &panel_mipi_dbi->encoder; 476 ret = drm_encoder_init(drm, encoder, &panel_mipi_dbi_encoder_funcs, 477 DRM_MODE_ENCODER_NONE, NULL); 478 if (ret) 479 return ret; 480 encoder->possible_crtcs = drm_crtc_mask(crtc); 481 482 connector = &panel_mipi_dbi->connector; 483 ret = drm_connector_init(drm, connector, &panel_mipi_dbi_connector_funcs, 484 DRM_MODE_CONNECTOR_SPI); 485 if (ret) 486 return ret; 487 drm_connector_helper_add(connector, &panel_mipi_dbi_connector_helper_funcs); 488 489 ret = drm_connector_attach_encoder(connector, encoder); 490 if (ret) 491 return ret; 492 493 drm_mode_config_reset(drm); 494 495 ret = drm_dev_register(drm, 0); 496 if (ret) 497 return ret; 498 499 spi_set_drvdata(spi, drm); 500 501 if (bpp == 16) 502 drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB565); 503 else 504 drm_client_setup_with_fourcc(drm, DRM_FORMAT_RGB888); 505 506 return 0; 507 } 508 509 static void panel_mipi_dbi_spi_remove(struct spi_device *spi) 510 { 511 struct drm_device *drm = spi_get_drvdata(spi); 512 513 drm_dev_unplug(drm); 514 drm_atomic_helper_shutdown(drm); 515 } 516 517 static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi) 518 { 519 drm_atomic_helper_shutdown(spi_get_drvdata(spi)); 520 } 521 522 static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev) 523 { 524 return drm_mode_config_helper_suspend(dev_get_drvdata(dev)); 525 } 526 527 static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev) 528 { 529 drm_mode_config_helper_resume(dev_get_drvdata(dev)); 530 531 return 0; 532 } 533 534 static const struct dev_pm_ops panel_mipi_dbi_pm_ops = { 535 SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume) 536 }; 537 538 static const struct of_device_id panel_mipi_dbi_spi_of_match[] = { 539 { .compatible = "panel-mipi-dbi-spi" }, 540 {}, 541 }; 542 MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match); 543 544 static const struct spi_device_id panel_mipi_dbi_spi_id[] = { 545 { "panel-mipi-dbi-spi", 0 }, 546 { }, 547 }; 548 MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id); 549 550 static struct spi_driver panel_mipi_dbi_spi_driver = { 551 .driver = { 552 .name = "panel-mipi-dbi-spi", 553 .of_match_table = panel_mipi_dbi_spi_of_match, 554 .pm = &panel_mipi_dbi_pm_ops, 555 }, 556 .id_table = panel_mipi_dbi_spi_id, 557 .probe = panel_mipi_dbi_spi_probe, 558 .remove = panel_mipi_dbi_spi_remove, 559 .shutdown = panel_mipi_dbi_spi_shutdown, 560 }; 561 module_spi_driver(panel_mipi_dbi_spi_driver); 562 563 MODULE_DESCRIPTION("MIPI DBI compatible display panel driver"); 564 MODULE_AUTHOR("Noralf Trønnes"); 565 MODULE_LICENSE("GPL"); 566