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