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