1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd 4 */ 5 6 #include <linux/delay.h> 7 #include <linux/gpio/consumer.h> 8 #include <linux/module.h> 9 #include <linux/of.h> 10 #include <linux/of_device.h> 11 #include <linux/regulator/consumer.h> 12 13 #include <video/mipi_display.h> 14 15 #include <drm/drm_crtc.h> 16 #include <drm/drm_device.h> 17 #include <drm/drm_mipi_dsi.h> 18 #include <drm/drm_modes.h> 19 #include <drm/drm_panel.h> 20 #include <drm/drm_print.h> 21 22 struct panel_init_cmd { 23 size_t len; 24 const char *data; 25 }; 26 27 #define _INIT_CMD(...) { \ 28 .len = sizeof((char[]){__VA_ARGS__}), \ 29 .data = (char[]){__VA_ARGS__} } 30 31 struct panel_desc { 32 const struct drm_display_mode *mode; 33 unsigned int bpc; 34 struct { 35 unsigned int width; 36 unsigned int height; 37 } size; 38 39 unsigned long flags; 40 enum mipi_dsi_pixel_format format; 41 const struct panel_init_cmd *init_cmds; 42 unsigned int lanes; 43 const char * const *supply_names; 44 unsigned int num_supplies; 45 unsigned int sleep_mode_delay; 46 unsigned int power_down_delay; 47 }; 48 49 struct innolux_panel { 50 struct drm_panel base; 51 struct mipi_dsi_device *link; 52 const struct panel_desc *desc; 53 54 struct regulator_bulk_data *supplies; 55 struct gpio_desc *enable_gpio; 56 57 bool prepared; 58 bool enabled; 59 }; 60 61 static inline struct innolux_panel *to_innolux_panel(struct drm_panel *panel) 62 { 63 return container_of(panel, struct innolux_panel, base); 64 } 65 66 static int innolux_panel_disable(struct drm_panel *panel) 67 { 68 struct innolux_panel *innolux = to_innolux_panel(panel); 69 70 if (!innolux->enabled) 71 return 0; 72 73 innolux->enabled = false; 74 75 return 0; 76 } 77 78 static int innolux_panel_unprepare(struct drm_panel *panel) 79 { 80 struct innolux_panel *innolux = to_innolux_panel(panel); 81 int err; 82 83 if (!innolux->prepared) 84 return 0; 85 86 err = mipi_dsi_dcs_set_display_off(innolux->link); 87 if (err < 0) 88 DRM_DEV_ERROR(panel->dev, "failed to set display off: %d\n", 89 err); 90 91 err = mipi_dsi_dcs_enter_sleep_mode(innolux->link); 92 if (err < 0) { 93 DRM_DEV_ERROR(panel->dev, "failed to enter sleep mode: %d\n", 94 err); 95 return err; 96 } 97 98 if (innolux->desc->sleep_mode_delay) 99 msleep(innolux->desc->sleep_mode_delay); 100 101 gpiod_set_value_cansleep(innolux->enable_gpio, 0); 102 103 if (innolux->desc->power_down_delay) 104 msleep(innolux->desc->power_down_delay); 105 106 err = regulator_bulk_disable(innolux->desc->num_supplies, 107 innolux->supplies); 108 if (err < 0) 109 return err; 110 111 innolux->prepared = false; 112 113 return 0; 114 } 115 116 static int innolux_panel_prepare(struct drm_panel *panel) 117 { 118 struct innolux_panel *innolux = to_innolux_panel(panel); 119 int err; 120 121 if (innolux->prepared) 122 return 0; 123 124 gpiod_set_value_cansleep(innolux->enable_gpio, 0); 125 126 err = regulator_bulk_enable(innolux->desc->num_supplies, 127 innolux->supplies); 128 if (err < 0) 129 return err; 130 131 /* p079zca: t2 (20ms), p097pfg: t4 (15ms) */ 132 usleep_range(20000, 21000); 133 134 gpiod_set_value_cansleep(innolux->enable_gpio, 1); 135 136 /* p079zca: t4, p097pfg: t5 */ 137 usleep_range(20000, 21000); 138 139 if (innolux->desc->init_cmds) { 140 const struct panel_init_cmd *cmds = 141 innolux->desc->init_cmds; 142 unsigned int i; 143 144 for (i = 0; cmds[i].len != 0; i++) { 145 const struct panel_init_cmd *cmd = &cmds[i]; 146 147 err = mipi_dsi_generic_write(innolux->link, cmd->data, 148 cmd->len); 149 if (err < 0) { 150 dev_err(panel->dev, 151 "failed to write command %u\n", i); 152 goto poweroff; 153 } 154 155 /* 156 * Included by random guessing, because without this 157 * (or at least, some delay), the panel sometimes 158 * didn't appear to pick up the command sequence. 159 */ 160 err = mipi_dsi_dcs_nop(innolux->link); 161 if (err < 0) { 162 dev_err(panel->dev, 163 "failed to send DCS nop: %d\n", err); 164 goto poweroff; 165 } 166 } 167 } 168 169 err = mipi_dsi_dcs_exit_sleep_mode(innolux->link); 170 if (err < 0) { 171 DRM_DEV_ERROR(panel->dev, "failed to exit sleep mode: %d\n", 172 err); 173 goto poweroff; 174 } 175 176 /* T6: 120ms - 1000ms*/ 177 msleep(120); 178 179 err = mipi_dsi_dcs_set_display_on(innolux->link); 180 if (err < 0) { 181 DRM_DEV_ERROR(panel->dev, "failed to set display on: %d\n", 182 err); 183 goto poweroff; 184 } 185 186 /* T7: 5ms */ 187 usleep_range(5000, 6000); 188 189 innolux->prepared = true; 190 191 return 0; 192 193 poweroff: 194 gpiod_set_value_cansleep(innolux->enable_gpio, 0); 195 regulator_bulk_disable(innolux->desc->num_supplies, innolux->supplies); 196 197 return err; 198 } 199 200 static int innolux_panel_enable(struct drm_panel *panel) 201 { 202 struct innolux_panel *innolux = to_innolux_panel(panel); 203 204 if (innolux->enabled) 205 return 0; 206 207 innolux->enabled = true; 208 209 return 0; 210 } 211 212 static const char * const innolux_p079zca_supply_names[] = { 213 "power", 214 }; 215 216 static const struct drm_display_mode innolux_p079zca_mode = { 217 .clock = 56900, 218 .hdisplay = 768, 219 .hsync_start = 768 + 40, 220 .hsync_end = 768 + 40 + 40, 221 .htotal = 768 + 40 + 40 + 40, 222 .vdisplay = 1024, 223 .vsync_start = 1024 + 20, 224 .vsync_end = 1024 + 20 + 4, 225 .vtotal = 1024 + 20 + 4 + 20, 226 }; 227 228 static const struct panel_desc innolux_p079zca_panel_desc = { 229 .mode = &innolux_p079zca_mode, 230 .bpc = 8, 231 .size = { 232 .width = 120, 233 .height = 160, 234 }, 235 .flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 236 MIPI_DSI_MODE_LPM, 237 .format = MIPI_DSI_FMT_RGB888, 238 .lanes = 4, 239 .supply_names = innolux_p079zca_supply_names, 240 .num_supplies = ARRAY_SIZE(innolux_p079zca_supply_names), 241 .power_down_delay = 80, /* T8: 80ms - 1000ms */ 242 }; 243 244 static const char * const innolux_p097pfg_supply_names[] = { 245 "avdd", 246 "avee", 247 }; 248 249 static const struct drm_display_mode innolux_p097pfg_mode = { 250 .clock = 229000, 251 .hdisplay = 1536, 252 .hsync_start = 1536 + 100, 253 .hsync_end = 1536 + 100 + 24, 254 .htotal = 1536 + 100 + 24 + 100, 255 .vdisplay = 2048, 256 .vsync_start = 2048 + 100, 257 .vsync_end = 2048 + 100 + 2, 258 .vtotal = 2048 + 100 + 2 + 18, 259 }; 260 261 /* 262 * Display manufacturer failed to provide init sequencing according to 263 * https://chromium-review.googlesource.com/c/chromiumos/third_party/coreboot/+/892065/ 264 * so the init sequence stems from a register dump of a working panel. 265 */ 266 static const struct panel_init_cmd innolux_p097pfg_init_cmds[] = { 267 /* page 0 */ 268 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x00), 269 _INIT_CMD(0xB1, 0xE8, 0x11), 270 _INIT_CMD(0xB2, 0x25, 0x02), 271 _INIT_CMD(0xB5, 0x08, 0x00), 272 _INIT_CMD(0xBC, 0x0F, 0x00), 273 _INIT_CMD(0xB8, 0x03, 0x06, 0x00, 0x00), 274 _INIT_CMD(0xBD, 0x01, 0x90, 0x14, 0x14), 275 _INIT_CMD(0x6F, 0x01), 276 _INIT_CMD(0xC0, 0x03), 277 _INIT_CMD(0x6F, 0x02), 278 _INIT_CMD(0xC1, 0x0D), 279 _INIT_CMD(0xD9, 0x01, 0x09, 0x70), 280 _INIT_CMD(0xC5, 0x12, 0x21, 0x00), 281 _INIT_CMD(0xBB, 0x93, 0x93), 282 283 /* page 1 */ 284 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x01), 285 _INIT_CMD(0xB3, 0x3C, 0x3C), 286 _INIT_CMD(0xB4, 0x0F, 0x0F), 287 _INIT_CMD(0xB9, 0x45, 0x45), 288 _INIT_CMD(0xBA, 0x14, 0x14), 289 _INIT_CMD(0xCA, 0x02), 290 _INIT_CMD(0xCE, 0x04), 291 _INIT_CMD(0xC3, 0x9B, 0x9B), 292 _INIT_CMD(0xD8, 0xC0, 0x03), 293 _INIT_CMD(0xBC, 0x82, 0x01), 294 _INIT_CMD(0xBD, 0x9E, 0x01), 295 296 /* page 2 */ 297 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x02), 298 _INIT_CMD(0xB0, 0x82), 299 _INIT_CMD(0xD1, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x82, 0x00, 0xA5, 300 0x00, 0xC1, 0x00, 0xEA, 0x01, 0x0D, 0x01, 0x40), 301 _INIT_CMD(0xD2, 0x01, 0x6A, 0x01, 0xA8, 0x01, 0xDC, 0x02, 0x29, 302 0x02, 0x67, 0x02, 0x68, 0x02, 0xA8, 0x02, 0xF0), 303 _INIT_CMD(0xD3, 0x03, 0x19, 0x03, 0x49, 0x03, 0x67, 0x03, 0x8C, 304 0x03, 0xA6, 0x03, 0xC7, 0x03, 0xDE, 0x03, 0xEC), 305 _INIT_CMD(0xD4, 0x03, 0xFF, 0x03, 0xFF), 306 _INIT_CMD(0xE0, 0x00, 0x00, 0x00, 0x86, 0x00, 0xC5, 0x00, 0xE5, 307 0x00, 0xFF, 0x01, 0x26, 0x01, 0x45, 0x01, 0x75), 308 _INIT_CMD(0xE1, 0x01, 0x9C, 0x01, 0xD5, 0x02, 0x05, 0x02, 0x4D, 309 0x02, 0x86, 0x02, 0x87, 0x02, 0xC3, 0x03, 0x03), 310 _INIT_CMD(0xE2, 0x03, 0x2A, 0x03, 0x56, 0x03, 0x72, 0x03, 0x94, 311 0x03, 0xAC, 0x03, 0xCB, 0x03, 0xE0, 0x03, 0xED), 312 _INIT_CMD(0xE3, 0x03, 0xFF, 0x03, 0xFF), 313 314 /* page 3 */ 315 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x03), 316 _INIT_CMD(0xB0, 0x00, 0x00, 0x00, 0x00), 317 _INIT_CMD(0xB1, 0x00, 0x00, 0x00, 0x00), 318 _INIT_CMD(0xB2, 0x00, 0x00, 0x06, 0x04, 0x01, 0x40, 0x85), 319 _INIT_CMD(0xB3, 0x10, 0x07, 0xFC, 0x04, 0x01, 0x40, 0x80), 320 _INIT_CMD(0xB6, 0xF0, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 321 0x40, 0x80), 322 _INIT_CMD(0xBA, 0xC5, 0x07, 0x00, 0x04, 0x11, 0x25, 0x8C), 323 _INIT_CMD(0xBB, 0xC5, 0x07, 0x00, 0x03, 0x11, 0x25, 0x8C), 324 _INIT_CMD(0xC0, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x80, 0x80), 325 _INIT_CMD(0xC1, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x80, 0x80), 326 _INIT_CMD(0xC4, 0x00, 0x00), 327 _INIT_CMD(0xEF, 0x41), 328 329 /* page 4 */ 330 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x04), 331 _INIT_CMD(0xEC, 0x4C), 332 333 /* page 5 */ 334 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x05), 335 _INIT_CMD(0xB0, 0x13, 0x03, 0x03, 0x01), 336 _INIT_CMD(0xB1, 0x30, 0x00), 337 _INIT_CMD(0xB2, 0x02, 0x02, 0x00), 338 _INIT_CMD(0xB3, 0x82, 0x23, 0x82, 0x9D), 339 _INIT_CMD(0xB4, 0xC5, 0x75, 0x24, 0x57), 340 _INIT_CMD(0xB5, 0x00, 0xD4, 0x72, 0x11, 0x11, 0xAB, 0x0A), 341 _INIT_CMD(0xB6, 0x00, 0x00, 0xD5, 0x72, 0x24, 0x56), 342 _INIT_CMD(0xB7, 0x5C, 0xDC, 0x5C, 0x5C), 343 _INIT_CMD(0xB9, 0x0C, 0x00, 0x00, 0x01, 0x00), 344 _INIT_CMD(0xC0, 0x75, 0x11, 0x11, 0x54, 0x05), 345 _INIT_CMD(0xC6, 0x00, 0x00, 0x00, 0x00), 346 _INIT_CMD(0xD0, 0x00, 0x48, 0x08, 0x00, 0x00), 347 _INIT_CMD(0xD1, 0x00, 0x48, 0x09, 0x00, 0x00), 348 349 /* page 6 */ 350 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x06), 351 _INIT_CMD(0xB0, 0x02, 0x32, 0x32, 0x08, 0x2F), 352 _INIT_CMD(0xB1, 0x2E, 0x15, 0x14, 0x13, 0x12), 353 _INIT_CMD(0xB2, 0x11, 0x10, 0x00, 0x3D, 0x3D), 354 _INIT_CMD(0xB3, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D), 355 _INIT_CMD(0xB4, 0x3D, 0x32), 356 _INIT_CMD(0xB5, 0x03, 0x32, 0x32, 0x09, 0x2F), 357 _INIT_CMD(0xB6, 0x2E, 0x1B, 0x1A, 0x19, 0x18), 358 _INIT_CMD(0xB7, 0x17, 0x16, 0x01, 0x3D, 0x3D), 359 _INIT_CMD(0xB8, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D), 360 _INIT_CMD(0xB9, 0x3D, 0x32), 361 _INIT_CMD(0xC0, 0x01, 0x32, 0x32, 0x09, 0x2F), 362 _INIT_CMD(0xC1, 0x2E, 0x1A, 0x1B, 0x16, 0x17), 363 _INIT_CMD(0xC2, 0x18, 0x19, 0x03, 0x3D, 0x3D), 364 _INIT_CMD(0xC3, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D), 365 _INIT_CMD(0xC4, 0x3D, 0x32), 366 _INIT_CMD(0xC5, 0x00, 0x32, 0x32, 0x08, 0x2F), 367 _INIT_CMD(0xC6, 0x2E, 0x14, 0x15, 0x10, 0x11), 368 _INIT_CMD(0xC7, 0x12, 0x13, 0x02, 0x3D, 0x3D), 369 _INIT_CMD(0xC8, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D), 370 _INIT_CMD(0xC9, 0x3D, 0x32), 371 372 {}, 373 }; 374 375 static const struct panel_desc innolux_p097pfg_panel_desc = { 376 .mode = &innolux_p097pfg_mode, 377 .bpc = 8, 378 .size = { 379 .width = 147, 380 .height = 196, 381 }, 382 .flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 383 MIPI_DSI_MODE_LPM, 384 .format = MIPI_DSI_FMT_RGB888, 385 .init_cmds = innolux_p097pfg_init_cmds, 386 .lanes = 4, 387 .supply_names = innolux_p097pfg_supply_names, 388 .num_supplies = ARRAY_SIZE(innolux_p097pfg_supply_names), 389 .sleep_mode_delay = 100, /* T15 */ 390 }; 391 392 static int innolux_panel_get_modes(struct drm_panel *panel, 393 struct drm_connector *connector) 394 { 395 struct innolux_panel *innolux = to_innolux_panel(panel); 396 const struct drm_display_mode *m = innolux->desc->mode; 397 struct drm_display_mode *mode; 398 399 mode = drm_mode_duplicate(connector->dev, m); 400 if (!mode) { 401 DRM_DEV_ERROR(panel->dev, "failed to add mode %ux%ux@%u\n", 402 m->hdisplay, m->vdisplay, drm_mode_vrefresh(m)); 403 return -ENOMEM; 404 } 405 406 drm_mode_set_name(mode); 407 408 drm_mode_probed_add(connector, mode); 409 410 connector->display_info.width_mm = innolux->desc->size.width; 411 connector->display_info.height_mm = innolux->desc->size.height; 412 connector->display_info.bpc = innolux->desc->bpc; 413 414 return 1; 415 } 416 417 static const struct drm_panel_funcs innolux_panel_funcs = { 418 .disable = innolux_panel_disable, 419 .unprepare = innolux_panel_unprepare, 420 .prepare = innolux_panel_prepare, 421 .enable = innolux_panel_enable, 422 .get_modes = innolux_panel_get_modes, 423 }; 424 425 static const struct of_device_id innolux_of_match[] = { 426 { .compatible = "innolux,p079zca", 427 .data = &innolux_p079zca_panel_desc 428 }, 429 { .compatible = "innolux,p097pfg", 430 .data = &innolux_p097pfg_panel_desc 431 }, 432 { } 433 }; 434 MODULE_DEVICE_TABLE(of, innolux_of_match); 435 436 static int innolux_panel_add(struct mipi_dsi_device *dsi, 437 const struct panel_desc *desc) 438 { 439 struct innolux_panel *innolux; 440 struct device *dev = &dsi->dev; 441 int err, i; 442 443 innolux = devm_kzalloc(dev, sizeof(*innolux), GFP_KERNEL); 444 if (!innolux) 445 return -ENOMEM; 446 447 innolux->desc = desc; 448 449 innolux->supplies = devm_kcalloc(dev, desc->num_supplies, 450 sizeof(*innolux->supplies), 451 GFP_KERNEL); 452 if (!innolux->supplies) 453 return -ENOMEM; 454 455 for (i = 0; i < desc->num_supplies; i++) 456 innolux->supplies[i].supply = desc->supply_names[i]; 457 458 err = devm_regulator_bulk_get(dev, desc->num_supplies, 459 innolux->supplies); 460 if (err < 0) 461 return err; 462 463 innolux->enable_gpio = devm_gpiod_get_optional(dev, "enable", 464 GPIOD_OUT_HIGH); 465 if (IS_ERR(innolux->enable_gpio)) { 466 err = PTR_ERR(innolux->enable_gpio); 467 dev_dbg(dev, "failed to get enable gpio: %d\n", err); 468 innolux->enable_gpio = NULL; 469 } 470 471 drm_panel_init(&innolux->base, dev, &innolux_panel_funcs, 472 DRM_MODE_CONNECTOR_DSI); 473 474 err = drm_panel_of_backlight(&innolux->base); 475 if (err) 476 return err; 477 478 err = drm_panel_add(&innolux->base); 479 if (err < 0) 480 return err; 481 482 mipi_dsi_set_drvdata(dsi, innolux); 483 innolux->link = dsi; 484 485 return 0; 486 } 487 488 static void innolux_panel_del(struct innolux_panel *innolux) 489 { 490 drm_panel_remove(&innolux->base); 491 } 492 493 static int innolux_panel_probe(struct mipi_dsi_device *dsi) 494 { 495 const struct panel_desc *desc; 496 int err; 497 498 desc = of_device_get_match_data(&dsi->dev); 499 dsi->mode_flags = desc->flags; 500 dsi->format = desc->format; 501 dsi->lanes = desc->lanes; 502 503 err = innolux_panel_add(dsi, desc); 504 if (err < 0) 505 return err; 506 507 return mipi_dsi_attach(dsi); 508 } 509 510 static int innolux_panel_remove(struct mipi_dsi_device *dsi) 511 { 512 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi); 513 int err; 514 515 err = drm_panel_unprepare(&innolux->base); 516 if (err < 0) 517 DRM_DEV_ERROR(&dsi->dev, "failed to unprepare panel: %d\n", 518 err); 519 520 err = drm_panel_disable(&innolux->base); 521 if (err < 0) 522 DRM_DEV_ERROR(&dsi->dev, "failed to disable panel: %d\n", err); 523 524 err = mipi_dsi_detach(dsi); 525 if (err < 0) 526 DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n", 527 err); 528 529 innolux_panel_del(innolux); 530 531 return 0; 532 } 533 534 static void innolux_panel_shutdown(struct mipi_dsi_device *dsi) 535 { 536 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi); 537 538 drm_panel_unprepare(&innolux->base); 539 drm_panel_disable(&innolux->base); 540 } 541 542 static struct mipi_dsi_driver innolux_panel_driver = { 543 .driver = { 544 .name = "panel-innolux-p079zca", 545 .of_match_table = innolux_of_match, 546 }, 547 .probe = innolux_panel_probe, 548 .remove = innolux_panel_remove, 549 .shutdown = innolux_panel_shutdown, 550 }; 551 module_mipi_dsi_driver(innolux_panel_driver); 552 553 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>"); 554 MODULE_AUTHOR("Lin Huang <hl@rock-chips.com>"); 555 MODULE_DESCRIPTION("Innolux P079ZCA panel driver"); 556 MODULE_LICENSE("GPL v2"); 557