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 .vrefresh = 60, 227 }; 228 229 static const struct panel_desc innolux_p079zca_panel_desc = { 230 .mode = &innolux_p079zca_mode, 231 .bpc = 8, 232 .size = { 233 .width = 120, 234 .height = 160, 235 }, 236 .flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 237 MIPI_DSI_MODE_LPM, 238 .format = MIPI_DSI_FMT_RGB888, 239 .lanes = 4, 240 .supply_names = innolux_p079zca_supply_names, 241 .num_supplies = ARRAY_SIZE(innolux_p079zca_supply_names), 242 .power_down_delay = 80, /* T8: 80ms - 1000ms */ 243 }; 244 245 static const char * const innolux_p097pfg_supply_names[] = { 246 "avdd", 247 "avee", 248 }; 249 250 static const struct drm_display_mode innolux_p097pfg_mode = { 251 .clock = 229000, 252 .hdisplay = 1536, 253 .hsync_start = 1536 + 100, 254 .hsync_end = 1536 + 100 + 24, 255 .htotal = 1536 + 100 + 24 + 100, 256 .vdisplay = 2048, 257 .vsync_start = 2048 + 100, 258 .vsync_end = 2048 + 100 + 2, 259 .vtotal = 2048 + 100 + 2 + 18, 260 .vrefresh = 60, 261 }; 262 263 /* 264 * Display manufacturer failed to provide init sequencing according to 265 * https://chromium-review.googlesource.com/c/chromiumos/third_party/coreboot/+/892065/ 266 * so the init sequence stems from a register dump of a working panel. 267 */ 268 static const struct panel_init_cmd innolux_p097pfg_init_cmds[] = { 269 /* page 0 */ 270 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x00), 271 _INIT_CMD(0xB1, 0xE8, 0x11), 272 _INIT_CMD(0xB2, 0x25, 0x02), 273 _INIT_CMD(0xB5, 0x08, 0x00), 274 _INIT_CMD(0xBC, 0x0F, 0x00), 275 _INIT_CMD(0xB8, 0x03, 0x06, 0x00, 0x00), 276 _INIT_CMD(0xBD, 0x01, 0x90, 0x14, 0x14), 277 _INIT_CMD(0x6F, 0x01), 278 _INIT_CMD(0xC0, 0x03), 279 _INIT_CMD(0x6F, 0x02), 280 _INIT_CMD(0xC1, 0x0D), 281 _INIT_CMD(0xD9, 0x01, 0x09, 0x70), 282 _INIT_CMD(0xC5, 0x12, 0x21, 0x00), 283 _INIT_CMD(0xBB, 0x93, 0x93), 284 285 /* page 1 */ 286 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x01), 287 _INIT_CMD(0xB3, 0x3C, 0x3C), 288 _INIT_CMD(0xB4, 0x0F, 0x0F), 289 _INIT_CMD(0xB9, 0x45, 0x45), 290 _INIT_CMD(0xBA, 0x14, 0x14), 291 _INIT_CMD(0xCA, 0x02), 292 _INIT_CMD(0xCE, 0x04), 293 _INIT_CMD(0xC3, 0x9B, 0x9B), 294 _INIT_CMD(0xD8, 0xC0, 0x03), 295 _INIT_CMD(0xBC, 0x82, 0x01), 296 _INIT_CMD(0xBD, 0x9E, 0x01), 297 298 /* page 2 */ 299 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x02), 300 _INIT_CMD(0xB0, 0x82), 301 _INIT_CMD(0xD1, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x82, 0x00, 0xA5, 302 0x00, 0xC1, 0x00, 0xEA, 0x01, 0x0D, 0x01, 0x40), 303 _INIT_CMD(0xD2, 0x01, 0x6A, 0x01, 0xA8, 0x01, 0xDC, 0x02, 0x29, 304 0x02, 0x67, 0x02, 0x68, 0x02, 0xA8, 0x02, 0xF0), 305 _INIT_CMD(0xD3, 0x03, 0x19, 0x03, 0x49, 0x03, 0x67, 0x03, 0x8C, 306 0x03, 0xA6, 0x03, 0xC7, 0x03, 0xDE, 0x03, 0xEC), 307 _INIT_CMD(0xD4, 0x03, 0xFF, 0x03, 0xFF), 308 _INIT_CMD(0xE0, 0x00, 0x00, 0x00, 0x86, 0x00, 0xC5, 0x00, 0xE5, 309 0x00, 0xFF, 0x01, 0x26, 0x01, 0x45, 0x01, 0x75), 310 _INIT_CMD(0xE1, 0x01, 0x9C, 0x01, 0xD5, 0x02, 0x05, 0x02, 0x4D, 311 0x02, 0x86, 0x02, 0x87, 0x02, 0xC3, 0x03, 0x03), 312 _INIT_CMD(0xE2, 0x03, 0x2A, 0x03, 0x56, 0x03, 0x72, 0x03, 0x94, 313 0x03, 0xAC, 0x03, 0xCB, 0x03, 0xE0, 0x03, 0xED), 314 _INIT_CMD(0xE3, 0x03, 0xFF, 0x03, 0xFF), 315 316 /* page 3 */ 317 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x03), 318 _INIT_CMD(0xB0, 0x00, 0x00, 0x00, 0x00), 319 _INIT_CMD(0xB1, 0x00, 0x00, 0x00, 0x00), 320 _INIT_CMD(0xB2, 0x00, 0x00, 0x06, 0x04, 0x01, 0x40, 0x85), 321 _INIT_CMD(0xB3, 0x10, 0x07, 0xFC, 0x04, 0x01, 0x40, 0x80), 322 _INIT_CMD(0xB6, 0xF0, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 323 0x40, 0x80), 324 _INIT_CMD(0xBA, 0xC5, 0x07, 0x00, 0x04, 0x11, 0x25, 0x8C), 325 _INIT_CMD(0xBB, 0xC5, 0x07, 0x00, 0x03, 0x11, 0x25, 0x8C), 326 _INIT_CMD(0xC0, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x80, 0x80), 327 _INIT_CMD(0xC1, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x80, 0x80), 328 _INIT_CMD(0xC4, 0x00, 0x00), 329 _INIT_CMD(0xEF, 0x41), 330 331 /* page 4 */ 332 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x04), 333 _INIT_CMD(0xEC, 0x4C), 334 335 /* page 5 */ 336 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x05), 337 _INIT_CMD(0xB0, 0x13, 0x03, 0x03, 0x01), 338 _INIT_CMD(0xB1, 0x30, 0x00), 339 _INIT_CMD(0xB2, 0x02, 0x02, 0x00), 340 _INIT_CMD(0xB3, 0x82, 0x23, 0x82, 0x9D), 341 _INIT_CMD(0xB4, 0xC5, 0x75, 0x24, 0x57), 342 _INIT_CMD(0xB5, 0x00, 0xD4, 0x72, 0x11, 0x11, 0xAB, 0x0A), 343 _INIT_CMD(0xB6, 0x00, 0x00, 0xD5, 0x72, 0x24, 0x56), 344 _INIT_CMD(0xB7, 0x5C, 0xDC, 0x5C, 0x5C), 345 _INIT_CMD(0xB9, 0x0C, 0x00, 0x00, 0x01, 0x00), 346 _INIT_CMD(0xC0, 0x75, 0x11, 0x11, 0x54, 0x05), 347 _INIT_CMD(0xC6, 0x00, 0x00, 0x00, 0x00), 348 _INIT_CMD(0xD0, 0x00, 0x48, 0x08, 0x00, 0x00), 349 _INIT_CMD(0xD1, 0x00, 0x48, 0x09, 0x00, 0x00), 350 351 /* page 6 */ 352 _INIT_CMD(0xF0, 0x55, 0xAA, 0x52, 0x08, 0x06), 353 _INIT_CMD(0xB0, 0x02, 0x32, 0x32, 0x08, 0x2F), 354 _INIT_CMD(0xB1, 0x2E, 0x15, 0x14, 0x13, 0x12), 355 _INIT_CMD(0xB2, 0x11, 0x10, 0x00, 0x3D, 0x3D), 356 _INIT_CMD(0xB3, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D), 357 _INIT_CMD(0xB4, 0x3D, 0x32), 358 _INIT_CMD(0xB5, 0x03, 0x32, 0x32, 0x09, 0x2F), 359 _INIT_CMD(0xB6, 0x2E, 0x1B, 0x1A, 0x19, 0x18), 360 _INIT_CMD(0xB7, 0x17, 0x16, 0x01, 0x3D, 0x3D), 361 _INIT_CMD(0xB8, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D), 362 _INIT_CMD(0xB9, 0x3D, 0x32), 363 _INIT_CMD(0xC0, 0x01, 0x32, 0x32, 0x09, 0x2F), 364 _INIT_CMD(0xC1, 0x2E, 0x1A, 0x1B, 0x16, 0x17), 365 _INIT_CMD(0xC2, 0x18, 0x19, 0x03, 0x3D, 0x3D), 366 _INIT_CMD(0xC3, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D), 367 _INIT_CMD(0xC4, 0x3D, 0x32), 368 _INIT_CMD(0xC5, 0x00, 0x32, 0x32, 0x08, 0x2F), 369 _INIT_CMD(0xC6, 0x2E, 0x14, 0x15, 0x10, 0x11), 370 _INIT_CMD(0xC7, 0x12, 0x13, 0x02, 0x3D, 0x3D), 371 _INIT_CMD(0xC8, 0x3D, 0x3D, 0x3D, 0x3D, 0x3D), 372 _INIT_CMD(0xC9, 0x3D, 0x32), 373 374 {}, 375 }; 376 377 static const struct panel_desc innolux_p097pfg_panel_desc = { 378 .mode = &innolux_p097pfg_mode, 379 .bpc = 8, 380 .size = { 381 .width = 147, 382 .height = 196, 383 }, 384 .flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 385 MIPI_DSI_MODE_LPM, 386 .format = MIPI_DSI_FMT_RGB888, 387 .init_cmds = innolux_p097pfg_init_cmds, 388 .lanes = 4, 389 .supply_names = innolux_p097pfg_supply_names, 390 .num_supplies = ARRAY_SIZE(innolux_p097pfg_supply_names), 391 .sleep_mode_delay = 100, /* T15 */ 392 }; 393 394 static int innolux_panel_get_modes(struct drm_panel *panel, 395 struct drm_connector *connector) 396 { 397 struct innolux_panel *innolux = to_innolux_panel(panel); 398 const struct drm_display_mode *m = innolux->desc->mode; 399 struct drm_display_mode *mode; 400 401 mode = drm_mode_duplicate(connector->dev, m); 402 if (!mode) { 403 DRM_DEV_ERROR(panel->dev, "failed to add mode %ux%ux@%u\n", 404 m->hdisplay, m->vdisplay, m->vrefresh); 405 return -ENOMEM; 406 } 407 408 drm_mode_set_name(mode); 409 410 drm_mode_probed_add(connector, mode); 411 412 connector->display_info.width_mm = innolux->desc->size.width; 413 connector->display_info.height_mm = innolux->desc->size.height; 414 connector->display_info.bpc = innolux->desc->bpc; 415 416 return 1; 417 } 418 419 static const struct drm_panel_funcs innolux_panel_funcs = { 420 .disable = innolux_panel_disable, 421 .unprepare = innolux_panel_unprepare, 422 .prepare = innolux_panel_prepare, 423 .enable = innolux_panel_enable, 424 .get_modes = innolux_panel_get_modes, 425 }; 426 427 static const struct of_device_id innolux_of_match[] = { 428 { .compatible = "innolux,p079zca", 429 .data = &innolux_p079zca_panel_desc 430 }, 431 { .compatible = "innolux,p097pfg", 432 .data = &innolux_p097pfg_panel_desc 433 }, 434 { } 435 }; 436 MODULE_DEVICE_TABLE(of, innolux_of_match); 437 438 static int innolux_panel_add(struct mipi_dsi_device *dsi, 439 const struct panel_desc *desc) 440 { 441 struct innolux_panel *innolux; 442 struct device *dev = &dsi->dev; 443 int err, i; 444 445 innolux = devm_kzalloc(dev, sizeof(*innolux), GFP_KERNEL); 446 if (!innolux) 447 return -ENOMEM; 448 449 innolux->desc = desc; 450 451 innolux->supplies = devm_kcalloc(dev, desc->num_supplies, 452 sizeof(*innolux->supplies), 453 GFP_KERNEL); 454 if (!innolux->supplies) 455 return -ENOMEM; 456 457 for (i = 0; i < desc->num_supplies; i++) 458 innolux->supplies[i].supply = desc->supply_names[i]; 459 460 err = devm_regulator_bulk_get(dev, desc->num_supplies, 461 innolux->supplies); 462 if (err < 0) 463 return err; 464 465 innolux->enable_gpio = devm_gpiod_get_optional(dev, "enable", 466 GPIOD_OUT_HIGH); 467 if (IS_ERR(innolux->enable_gpio)) { 468 err = PTR_ERR(innolux->enable_gpio); 469 dev_dbg(dev, "failed to get enable gpio: %d\n", err); 470 innolux->enable_gpio = NULL; 471 } 472 473 drm_panel_init(&innolux->base, dev, &innolux_panel_funcs, 474 DRM_MODE_CONNECTOR_DSI); 475 476 err = drm_panel_of_backlight(&innolux->base); 477 if (err) 478 return err; 479 480 err = drm_panel_add(&innolux->base); 481 if (err < 0) 482 return err; 483 484 mipi_dsi_set_drvdata(dsi, innolux); 485 innolux->link = dsi; 486 487 return 0; 488 } 489 490 static void innolux_panel_del(struct innolux_panel *innolux) 491 { 492 drm_panel_remove(&innolux->base); 493 } 494 495 static int innolux_panel_probe(struct mipi_dsi_device *dsi) 496 { 497 const struct panel_desc *desc; 498 int err; 499 500 desc = of_device_get_match_data(&dsi->dev); 501 dsi->mode_flags = desc->flags; 502 dsi->format = desc->format; 503 dsi->lanes = desc->lanes; 504 505 err = innolux_panel_add(dsi, desc); 506 if (err < 0) 507 return err; 508 509 return mipi_dsi_attach(dsi); 510 } 511 512 static int innolux_panel_remove(struct mipi_dsi_device *dsi) 513 { 514 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi); 515 int err; 516 517 err = drm_panel_unprepare(&innolux->base); 518 if (err < 0) 519 DRM_DEV_ERROR(&dsi->dev, "failed to unprepare panel: %d\n", 520 err); 521 522 err = drm_panel_disable(&innolux->base); 523 if (err < 0) 524 DRM_DEV_ERROR(&dsi->dev, "failed to disable panel: %d\n", err); 525 526 err = mipi_dsi_detach(dsi); 527 if (err < 0) 528 DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n", 529 err); 530 531 innolux_panel_del(innolux); 532 533 return 0; 534 } 535 536 static void innolux_panel_shutdown(struct mipi_dsi_device *dsi) 537 { 538 struct innolux_panel *innolux = mipi_dsi_get_drvdata(dsi); 539 540 drm_panel_unprepare(&innolux->base); 541 drm_panel_disable(&innolux->base); 542 } 543 544 static struct mipi_dsi_driver innolux_panel_driver = { 545 .driver = { 546 .name = "panel-innolux-p079zca", 547 .of_match_table = innolux_of_match, 548 }, 549 .probe = innolux_panel_probe, 550 .remove = innolux_panel_remove, 551 .shutdown = innolux_panel_shutdown, 552 }; 553 module_mipi_dsi_driver(innolux_panel_driver); 554 555 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>"); 556 MODULE_AUTHOR("Lin Huang <hl@rock-chips.com>"); 557 MODULE_DESCRIPTION("Innolux P079ZCA panel driver"); 558 MODULE_LICENSE("GPL v2"); 559