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/property.h> 14 #include <linux/regulator/consumer.h> 15 #include <linux/spi/spi.h> 16 17 #include <drm/clients/drm_client_setup.h> 18 #include <drm/drm_atomic_helper.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> 27 28 #include <video/mipi_display.h> 29 30 struct panel_mipi_dbi_format { 31 const char *name; 32 u32 fourcc; 33 unsigned int bpp; 34 }; 35 36 static const struct panel_mipi_dbi_format panel_mipi_dbi_formats[] = { 37 { "r5g6b5", DRM_FORMAT_RGB565, 16 }, 38 { "b6x2g6x2r6x2", DRM_FORMAT_RGB888, 24 }, 39 }; 40 41 static int panel_mipi_dbi_get_format(struct device *dev, u32 *formats, unsigned int *bpp) 42 { 43 const char *format_name; 44 unsigned int i; 45 int ret; 46 47 formats[1] = DRM_FORMAT_XRGB8888; 48 49 ret = device_property_read_string(dev, "format", &format_name); 50 if (ret) { 51 /* Old Device Trees don't have this property */ 52 formats[0] = DRM_FORMAT_RGB565; 53 *bpp = 16; 54 return 0; 55 } 56 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]; 59 60 if (strcmp(format_name, format->name)) 61 continue; 62 63 formats[0] = format->fourcc; 64 *bpp = format->bpp; 65 return 0; 66 } 67 68 dev_err(dev, "Pixel format is not supported: '%s'\n", format_name); 69 70 return -EINVAL; 71 } 72 73 static const u8 panel_mipi_dbi_magic[15] = { 'M', 'I', 'P', 'I', ' ', 'D', 'B', 'I', 74 0, 0, 0, 0, 0, 0, 0 }; 75 76 /* 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. 80 */ 81 struct panel_mipi_dbi_config { 82 /* Magic string: panel_mipi_dbi_magic */ 83 u8 magic[15]; 84 85 /* Config file format version */ 86 u8 file_format_version; 87 88 /* 89 * MIPI commands to execute when the display pipeline is enabled. 90 * This is used to configure the display controller. 91 * 92 * The commands are stored in a byte array with the format: 93 * command, num_parameters, [ parameter, ...], command, ... 94 * 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). 99 * 100 * Example: 101 * command 0x11 102 * sleep 120ms 103 * command 0xb1 parameters 0x01, 0x2c, 0x2d 104 * command 0x29 105 * 106 * Byte sequence: 107 * 0x11 0x00 108 * 0x00 0x01 0x78 109 * 0xb1 0x03 0x01 0x2c 0x2d 110 * 0x29 0x00 111 */ 112 u8 commands[]; 113 }; 114 115 struct panel_mipi_dbi_commands { 116 const u8 *buf; 117 size_t len; 118 }; 119 120 static struct panel_mipi_dbi_commands * 121 panel_mipi_dbi_check_commands(struct device *dev, const struct firmware *fw) 122 { 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; 126 unsigned int i = 0; 127 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); 131 } 132 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); 136 } 137 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); 141 } 142 143 drm_dev_dbg(dev, DRM_UT_DRIVER, "size=%zu version=%u\n", size, config->file_format_version); 144 145 commands_len = size - sizeof(*config); 146 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]; 151 152 i += num_parameters; 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); 157 } 158 159 if (command == 0x00 && num_parameters == 1) 160 drm_dev_dbg(dev, DRM_UT_DRIVER, "sleep %ums\n", parameters[0]); 161 else 162 drm_dev_dbg(dev, DRM_UT_DRIVER, "command %02x %*ph\n", 163 command, num_parameters, parameters); 164 } 165 166 if (i != commands_len) { 167 dev_err(dev, "config: malformed command array\n"); 168 return ERR_PTR(-EINVAL); 169 } 170 171 commands = devm_kzalloc(dev, sizeof(*commands), GFP_KERNEL); 172 if (!commands) 173 return ERR_PTR(-ENOMEM); 174 175 commands->len = commands_len; 176 commands->buf = devm_kmemdup(dev, config->commands, commands->len, GFP_KERNEL); 177 if (!commands->buf) 178 return ERR_PTR(-ENOMEM); 179 180 return commands; 181 } 182 183 static struct panel_mipi_dbi_commands *panel_mipi_dbi_commands_from_fw(struct device *dev) 184 { 185 struct panel_mipi_dbi_commands *commands; 186 const struct firmware *fw; 187 const char *compatible; 188 char fw_name[40]; 189 int ret; 190 191 ret = of_property_read_string_index(dev->of_node, "compatible", 0, &compatible); 192 if (ret) 193 return ERR_PTR(ret); 194 195 snprintf(fw_name, sizeof(fw_name), "%s.bin", compatible); 196 ret = request_firmware(&fw, fw_name, dev); 197 if (ret) { 198 dev_err(dev, "No config file found for compatible '%s' (error=%d)\n", 199 compatible, ret); 200 201 return ERR_PTR(ret); 202 } 203 204 commands = panel_mipi_dbi_check_commands(dev, fw); 205 release_firmware(fw); 206 207 return commands; 208 } 209 210 static void panel_mipi_dbi_commands_execute(struct mipi_dbi *dbi, 211 struct panel_mipi_dbi_commands *commands) 212 { 213 unsigned int i = 0; 214 215 if (!commands) 216 return; 217 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]; 222 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); 227 else 228 mipi_dbi_command(dbi, command); 229 230 i += num_parameters; 231 } 232 } 233 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) 237 { 238 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev); 239 struct mipi_dbi *dbi = &dbidev->dbi; 240 int ret, idx; 241 242 if (!drm_dev_enter(pipe->crtc.dev, &idx)) 243 return; 244 245 drm_dbg(pipe->crtc.dev, "\n"); 246 247 ret = mipi_dbi_poweron_conditional_reset(dbidev); 248 if (ret < 0) 249 goto out_exit; 250 if (!ret) 251 panel_mipi_dbi_commands_execute(dbi, dbidev->driver_private); 252 253 mipi_dbi_enable_flush(dbidev, crtc_state, plane_state); 254 out_exit: 255 drm_dev_exit(idx); 256 } 257 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), 260 }; 261 262 DEFINE_DRM_GEM_DMA_FOPS(panel_mipi_dbi_fops); 263 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", 272 .major = 1, 273 .minor = 0, 274 }; 275 276 static int panel_mipi_dbi_get_mode(struct mipi_dbi_dev *dbidev, struct drm_display_mode *mode) 277 { 278 struct device *dev = dbidev->drm.dev; 279 u16 hback_porch, vback_porch; 280 int ret; 281 282 ret = of_get_drm_panel_display_mode(dev->of_node, mode, NULL); 283 if (ret) { 284 dev_err(dev, "%pOF: failed to get panel-timing (error=%d)\n", dev->of_node, ret); 285 return ret; 286 } 287 288 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 289 290 hback_porch = mode->htotal - mode->hsync_end; 291 vback_porch = mode->vtotal - mode->vsync_end; 292 293 /* 294 * Make sure width and height are set and that only back porch and 295 * pixelclock are set in the other timing values. Also check that 296 * width and height don't exceed the 16-bit value specified by MIPI DCS. 297 */ 298 if (!mode->hdisplay || !mode->vdisplay || mode->flags || 299 mode->hsync_end > mode->hdisplay || (hback_porch + mode->hdisplay) > 0xffff || 300 mode->vsync_end > mode->vdisplay || (vback_porch + mode->vdisplay) > 0xffff) { 301 dev_err(dev, "%pOF: panel-timing out of bounds\n", dev->of_node); 302 return -EINVAL; 303 } 304 305 /* The driver doesn't use the pixel clock but it is mandatory so fake one if not set */ 306 if (!mode->clock) 307 mode->clock = mode->htotal * mode->vtotal * 60 / 1000; 308 309 dbidev->top_offset = vback_porch; 310 dbidev->left_offset = hback_porch; 311 312 return 0; 313 } 314 315 static int panel_mipi_dbi_spi_probe(struct spi_device *spi) 316 { 317 struct device *dev = &spi->dev; 318 struct drm_display_mode mode; 319 struct mipi_dbi_dev *dbidev; 320 struct drm_device *drm; 321 struct mipi_dbi *dbi; 322 struct gpio_desc *dc; 323 unsigned int bpp; 324 size_t buf_size; 325 u32 formats[2]; 326 int ret; 327 328 dbidev = devm_drm_dev_alloc(dev, &panel_mipi_dbi_driver, struct mipi_dbi_dev, drm); 329 if (IS_ERR(dbidev)) 330 return PTR_ERR(dbidev); 331 332 dbi = &dbidev->dbi; 333 drm = &dbidev->drm; 334 335 ret = panel_mipi_dbi_get_mode(dbidev, &mode); 336 if (ret) 337 return ret; 338 339 dbidev->regulator = devm_regulator_get(dev, "power"); 340 if (IS_ERR(dbidev->regulator)) 341 return dev_err_probe(dev, PTR_ERR(dbidev->regulator), 342 "Failed to get regulator 'power'\n"); 343 344 dbidev->io_regulator = devm_regulator_get(dev, "io"); 345 if (IS_ERR(dbidev->io_regulator)) 346 return dev_err_probe(dev, PTR_ERR(dbidev->io_regulator), 347 "Failed to get regulator 'io'\n"); 348 349 dbidev->backlight = devm_of_find_backlight(dev); 350 if (IS_ERR(dbidev->backlight)) 351 return dev_err_probe(dev, PTR_ERR(dbidev->backlight), "Failed to get backlight\n"); 352 353 dbi->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 354 if (IS_ERR(dbi->reset)) 355 return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n"); 356 357 /* Multiple panels can share the "dc" GPIO, but only if they are on the same SPI bus! */ 358 dc = devm_gpiod_get_optional(dev, "dc", GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE); 359 if (IS_ERR(dc)) 360 return dev_err_probe(dev, PTR_ERR(dc), "Failed to get GPIO 'dc'\n"); 361 362 ret = mipi_dbi_spi_init(spi, dbi, dc); 363 if (ret) 364 return ret; 365 366 if (device_property_present(dev, "write-only")) 367 dbi->read_commands = NULL; 368 369 dbidev->driver_private = panel_mipi_dbi_commands_from_fw(dev); 370 if (IS_ERR(dbidev->driver_private)) 371 return PTR_ERR(dbidev->driver_private); 372 373 ret = panel_mipi_dbi_get_format(dev, formats, &bpp); 374 if (ret) 375 return ret; 376 377 buf_size = DIV_ROUND_UP(mode.hdisplay * mode.vdisplay * bpp, 8); 378 ret = mipi_dbi_dev_init_with_formats(dbidev, &panel_mipi_dbi_pipe_funcs, 379 formats, ARRAY_SIZE(formats), 380 &mode, 0, buf_size); 381 if (ret) 382 return ret; 383 384 drm_mode_config_reset(drm); 385 386 ret = drm_dev_register(drm, 0); 387 if (ret) 388 return ret; 389 390 spi_set_drvdata(spi, drm); 391 392 drm_client_setup(drm, NULL); 393 394 return 0; 395 } 396 397 static void panel_mipi_dbi_spi_remove(struct spi_device *spi) 398 { 399 struct drm_device *drm = spi_get_drvdata(spi); 400 401 drm_dev_unplug(drm); 402 drm_atomic_helper_shutdown(drm); 403 } 404 405 static void panel_mipi_dbi_spi_shutdown(struct spi_device *spi) 406 { 407 drm_atomic_helper_shutdown(spi_get_drvdata(spi)); 408 } 409 410 static int __maybe_unused panel_mipi_dbi_pm_suspend(struct device *dev) 411 { 412 return drm_mode_config_helper_suspend(dev_get_drvdata(dev)); 413 } 414 415 static int __maybe_unused panel_mipi_dbi_pm_resume(struct device *dev) 416 { 417 drm_mode_config_helper_resume(dev_get_drvdata(dev)); 418 419 return 0; 420 } 421 422 static const struct dev_pm_ops panel_mipi_dbi_pm_ops = { 423 SET_SYSTEM_SLEEP_PM_OPS(panel_mipi_dbi_pm_suspend, panel_mipi_dbi_pm_resume) 424 }; 425 426 static const struct of_device_id panel_mipi_dbi_spi_of_match[] = { 427 { .compatible = "panel-mipi-dbi-spi" }, 428 {}, 429 }; 430 MODULE_DEVICE_TABLE(of, panel_mipi_dbi_spi_of_match); 431 432 static const struct spi_device_id panel_mipi_dbi_spi_id[] = { 433 { "panel-mipi-dbi-spi", 0 }, 434 { }, 435 }; 436 MODULE_DEVICE_TABLE(spi, panel_mipi_dbi_spi_id); 437 438 static struct spi_driver panel_mipi_dbi_spi_driver = { 439 .driver = { 440 .name = "panel-mipi-dbi-spi", 441 .of_match_table = panel_mipi_dbi_spi_of_match, 442 .pm = &panel_mipi_dbi_pm_ops, 443 }, 444 .id_table = panel_mipi_dbi_spi_id, 445 .probe = panel_mipi_dbi_spi_probe, 446 .remove = panel_mipi_dbi_spi_remove, 447 .shutdown = panel_mipi_dbi_spi_shutdown, 448 }; 449 module_spi_driver(panel_mipi_dbi_spi_driver); 450 451 MODULE_DESCRIPTION("MIPI DBI compatible display panel driver"); 452 MODULE_AUTHOR("Noralf Trønnes"); 453 MODULE_LICENSE("GPL"); 454