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