1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Samsung S6D7AA0 MIPI-DSI TFT LCD controller drm_panel driver. 4 * 5 * Copyright (C) 2022 Artur Weber <aweber.kernel@gmail.com> 6 */ 7 8 #include <linux/backlight.h> 9 #include <linux/delay.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/module.h> 12 #include <linux/regulator/consumer.h> 13 #include <linux/of.h> 14 #include <linux/of_device.h> 15 16 #include <video/mipi_display.h> 17 #include <drm/drm_mipi_dsi.h> 18 #include <drm/drm_modes.h> 19 #include <drm/drm_panel.h> 20 21 /* Manufacturer command set */ 22 #define MCS_BL_CTL 0xc3 23 #define MCS_OTP_RELOAD 0xd0 24 #define MCS_PASSWD1 0xf0 25 #define MCS_PASSWD2 0xf1 26 #define MCS_PASSWD3 0xfc 27 28 struct s6d7aa0 { 29 struct drm_panel panel; 30 struct mipi_dsi_device *dsi; 31 struct gpio_desc *reset_gpio; 32 struct regulator_bulk_data supplies[2]; 33 const struct s6d7aa0_panel_desc *desc; 34 }; 35 36 struct s6d7aa0_panel_desc { 37 unsigned int panel_type; 38 int (*init_func)(struct s6d7aa0 *ctx); 39 int (*off_func)(struct s6d7aa0 *ctx); 40 const struct drm_display_mode *drm_mode; 41 unsigned long mode_flags; 42 u32 bus_flags; 43 bool has_backlight; 44 bool use_passwd3; 45 }; 46 47 enum s6d7aa0_panels { 48 S6D7AA0_PANEL_LSL080AL02, 49 S6D7AA0_PANEL_LSL080AL03, 50 S6D7AA0_PANEL_LTL101AT01, 51 }; 52 53 static inline struct s6d7aa0 *panel_to_s6d7aa0(struct drm_panel *panel) 54 { 55 return container_of(panel, struct s6d7aa0, panel); 56 } 57 58 static void s6d7aa0_reset(struct s6d7aa0 *ctx) 59 { 60 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 61 msleep(50); 62 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 63 msleep(50); 64 } 65 66 static int s6d7aa0_lock(struct s6d7aa0 *ctx, bool lock) 67 { 68 struct mipi_dsi_device *dsi = ctx->dsi; 69 70 if (lock) { 71 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD1, 0xa5, 0xa5); 72 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD2, 0xa5, 0xa5); 73 if (ctx->desc->use_passwd3) 74 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD3, 0x5a, 0x5a); 75 } else { 76 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD1, 0x5a, 0x5a); 77 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD2, 0x5a, 0x5a); 78 if (ctx->desc->use_passwd3) 79 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD3, 0xa5, 0xa5); 80 } 81 82 return 0; 83 } 84 85 static int s6d7aa0_on(struct s6d7aa0 *ctx) 86 { 87 struct mipi_dsi_device *dsi = ctx->dsi; 88 struct device *dev = &dsi->dev; 89 int ret; 90 91 ret = ctx->desc->init_func(ctx); 92 if (ret < 0) { 93 dev_err(dev, "Failed to initialize panel: %d\n", ret); 94 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 95 return ret; 96 } 97 98 ret = mipi_dsi_dcs_set_display_on(dsi); 99 if (ret < 0) { 100 dev_err(dev, "Failed to set display on: %d\n", ret); 101 return ret; 102 } 103 104 return 0; 105 } 106 107 static int s6d7aa0_off(struct s6d7aa0 *ctx) 108 { 109 struct mipi_dsi_device *dsi = ctx->dsi; 110 struct device *dev = &dsi->dev; 111 int ret; 112 113 ret = ctx->desc->off_func(ctx); 114 if (ret < 0) { 115 dev_err(dev, "Panel-specific off function failed: %d\n", ret); 116 return ret; 117 } 118 119 ret = mipi_dsi_dcs_set_display_off(dsi); 120 if (ret < 0) { 121 dev_err(dev, "Failed to set display off: %d\n", ret); 122 return ret; 123 } 124 msleep(64); 125 126 ret = mipi_dsi_dcs_enter_sleep_mode(dsi); 127 if (ret < 0) { 128 dev_err(dev, "Failed to enter sleep mode: %d\n", ret); 129 return ret; 130 } 131 msleep(120); 132 133 return 0; 134 } 135 136 static int s6d7aa0_prepare(struct drm_panel *panel) 137 { 138 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel); 139 struct device *dev = &ctx->dsi->dev; 140 int ret; 141 142 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies); 143 if (ret < 0) { 144 dev_err(dev, "Failed to enable regulators: %d\n", ret); 145 return ret; 146 } 147 148 s6d7aa0_reset(ctx); 149 150 ret = s6d7aa0_on(ctx); 151 if (ret < 0) { 152 dev_err(dev, "Failed to initialize panel: %d\n", ret); 153 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 154 return ret; 155 } 156 157 return 0; 158 } 159 160 static int s6d7aa0_disable(struct drm_panel *panel) 161 { 162 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel); 163 struct device *dev = &ctx->dsi->dev; 164 int ret; 165 166 ret = s6d7aa0_off(ctx); 167 if (ret < 0) 168 dev_err(dev, "Failed to un-initialize panel: %d\n", ret); 169 170 return 0; 171 } 172 173 static int s6d7aa0_unprepare(struct drm_panel *panel) 174 { 175 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel); 176 177 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 178 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies); 179 180 return 0; 181 } 182 183 /* Backlight control code */ 184 185 static int s6d7aa0_bl_update_status(struct backlight_device *bl) 186 { 187 struct mipi_dsi_device *dsi = bl_get_data(bl); 188 u16 brightness = backlight_get_brightness(bl); 189 int ret; 190 191 ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness); 192 if (ret < 0) 193 return ret; 194 195 return 0; 196 } 197 198 static int s6d7aa0_bl_get_brightness(struct backlight_device *bl) 199 { 200 struct mipi_dsi_device *dsi = bl_get_data(bl); 201 u16 brightness; 202 int ret; 203 204 ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness); 205 if (ret < 0) 206 return ret; 207 208 return brightness & 0xff; 209 } 210 211 static const struct backlight_ops s6d7aa0_bl_ops = { 212 .update_status = s6d7aa0_bl_update_status, 213 .get_brightness = s6d7aa0_bl_get_brightness, 214 }; 215 216 static struct backlight_device * 217 s6d7aa0_create_backlight(struct mipi_dsi_device *dsi) 218 { 219 struct device *dev = &dsi->dev; 220 const struct backlight_properties props = { 221 .type = BACKLIGHT_RAW, 222 .brightness = 255, 223 .max_brightness = 255, 224 }; 225 226 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi, 227 &s6d7aa0_bl_ops, &props); 228 } 229 230 /* Initialization code and structures for LSL080AL02 panel */ 231 232 static int s6d7aa0_lsl080al02_init(struct s6d7aa0 *ctx) 233 { 234 struct mipi_dsi_device *dsi = ctx->dsi; 235 struct device *dev = &dsi->dev; 236 int ret; 237 238 usleep_range(20000, 25000); 239 240 ret = s6d7aa0_lock(ctx, false); 241 if (ret < 0) { 242 dev_err(dev, "Failed to unlock registers: %d\n", ret); 243 return ret; 244 } 245 246 mipi_dsi_dcs_write_seq(dsi, MCS_OTP_RELOAD, 0x00, 0x10); 247 usleep_range(1000, 1500); 248 249 /* SEQ_B6_PARAM_8_R01 */ 250 mipi_dsi_dcs_write_seq(dsi, 0xb6, 0x10); 251 252 /* BL_CTL_ON */ 253 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x28); 254 255 usleep_range(5000, 6000); 256 257 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x04); 258 259 ret = mipi_dsi_dcs_exit_sleep_mode(dsi); 260 if (ret < 0) { 261 dev_err(dev, "Failed to exit sleep mode: %d\n", ret); 262 return ret; 263 } 264 265 msleep(120); 266 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x00); 267 268 ret = s6d7aa0_lock(ctx, true); 269 if (ret < 0) { 270 dev_err(dev, "Failed to lock registers: %d\n", ret); 271 return ret; 272 } 273 274 ret = mipi_dsi_dcs_set_display_on(dsi); 275 if (ret < 0) { 276 dev_err(dev, "Failed to set display on: %d\n", ret); 277 return ret; 278 } 279 280 return 0; 281 } 282 283 static int s6d7aa0_lsl080al02_off(struct s6d7aa0 *ctx) 284 { 285 struct mipi_dsi_device *dsi = ctx->dsi; 286 287 /* BL_CTL_OFF */ 288 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x20); 289 290 return 0; 291 } 292 293 static const struct drm_display_mode s6d7aa0_lsl080al02_mode = { 294 .clock = (800 + 16 + 4 + 140) * (1280 + 8 + 4 + 4) * 60 / 1000, 295 .hdisplay = 800, 296 .hsync_start = 800 + 16, 297 .hsync_end = 800 + 16 + 4, 298 .htotal = 800 + 16 + 4 + 140, 299 .vdisplay = 1280, 300 .vsync_start = 1280 + 8, 301 .vsync_end = 1280 + 8 + 4, 302 .vtotal = 1280 + 8 + 4 + 4, 303 .width_mm = 108, 304 .height_mm = 173, 305 }; 306 307 static const struct s6d7aa0_panel_desc s6d7aa0_lsl080al02_desc = { 308 .panel_type = S6D7AA0_PANEL_LSL080AL02, 309 .init_func = s6d7aa0_lsl080al02_init, 310 .off_func = s6d7aa0_lsl080al02_off, 311 .drm_mode = &s6d7aa0_lsl080al02_mode, 312 .mode_flags = MIPI_DSI_MODE_VSYNC_FLUSH | MIPI_DSI_MODE_VIDEO_NO_HFP, 313 .bus_flags = DRM_BUS_FLAG_DE_HIGH, 314 315 .has_backlight = false, 316 .use_passwd3 = false, 317 }; 318 319 /* Initialization code and structures for LSL080AL03 panel */ 320 321 static int s6d7aa0_lsl080al03_init(struct s6d7aa0 *ctx) 322 { 323 struct mipi_dsi_device *dsi = ctx->dsi; 324 struct device *dev = &dsi->dev; 325 int ret; 326 327 usleep_range(20000, 25000); 328 329 ret = s6d7aa0_lock(ctx, false); 330 if (ret < 0) { 331 dev_err(dev, "Failed to unlock registers: %d\n", ret); 332 return ret; 333 } 334 335 if (ctx->desc->panel_type == S6D7AA0_PANEL_LSL080AL03) { 336 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0xc7, 0x00, 0x29); 337 mipi_dsi_dcs_write_seq(dsi, 0xbc, 0x01, 0x4e, 0xa0); 338 mipi_dsi_dcs_write_seq(dsi, 0xfd, 0x16, 0x10, 0x11, 0x23, 339 0x09); 340 mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x00, 0x02, 0x03, 0x21, 341 0x80, 0x78); 342 } else if (ctx->desc->panel_type == S6D7AA0_PANEL_LTL101AT01) { 343 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x08); 344 mipi_dsi_dcs_write_seq(dsi, 0xbc, 0x01, 0x4e, 0x0b); 345 mipi_dsi_dcs_write_seq(dsi, 0xfd, 0x16, 0x10, 0x11, 0x23, 346 0x09); 347 mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x00, 0x02, 0x03, 0x21, 348 0x80, 0x68); 349 } 350 351 mipi_dsi_dcs_write_seq(dsi, 0xb3, 0x51); 352 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24); 353 mipi_dsi_dcs_write_seq(dsi, 0xf2, 0x02, 0x08, 0x08); 354 355 usleep_range(10000, 11000); 356 357 mipi_dsi_dcs_write_seq(dsi, 0xc0, 0x80, 0x80, 0x30); 358 mipi_dsi_dcs_write_seq(dsi, 0xcd, 359 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 360 0x2e, 0x2e, 0x2e, 0x2e, 0x2e); 361 mipi_dsi_dcs_write_seq(dsi, 0xce, 362 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 363 0x00, 0x00, 0x00, 0x00, 0x00); 364 mipi_dsi_dcs_write_seq(dsi, 0xc1, 0x03); 365 366 ret = mipi_dsi_dcs_exit_sleep_mode(dsi); 367 if (ret < 0) { 368 dev_err(dev, "Failed to exit sleep mode: %d\n", ret); 369 return ret; 370 } 371 372 ret = s6d7aa0_lock(ctx, true); 373 if (ret < 0) { 374 dev_err(dev, "Failed to lock registers: %d\n", ret); 375 return ret; 376 } 377 378 ret = mipi_dsi_dcs_set_display_on(dsi); 379 if (ret < 0) { 380 dev_err(dev, "Failed to set display on: %d\n", ret); 381 return ret; 382 } 383 384 return 0; 385 } 386 387 static int s6d7aa0_lsl080al03_off(struct s6d7aa0 *ctx) 388 { 389 struct mipi_dsi_device *dsi = ctx->dsi; 390 391 mipi_dsi_dcs_write_seq(dsi, 0x22, 0x00); 392 393 return 0; 394 } 395 396 static const struct drm_display_mode s6d7aa0_lsl080al03_mode = { 397 .clock = (768 + 18 + 16 + 126) * (1024 + 8 + 2 + 6) * 60 / 1000, 398 .hdisplay = 768, 399 .hsync_start = 768 + 18, 400 .hsync_end = 768 + 18 + 16, 401 .htotal = 768 + 18 + 16 + 126, 402 .vdisplay = 1024, 403 .vsync_start = 1024 + 8, 404 .vsync_end = 1024 + 8 + 2, 405 .vtotal = 1024 + 8 + 2 + 6, 406 .width_mm = 122, 407 .height_mm = 163, 408 }; 409 410 static const struct s6d7aa0_panel_desc s6d7aa0_lsl080al03_desc = { 411 .panel_type = S6D7AA0_PANEL_LSL080AL03, 412 .init_func = s6d7aa0_lsl080al03_init, 413 .off_func = s6d7aa0_lsl080al03_off, 414 .drm_mode = &s6d7aa0_lsl080al03_mode, 415 .mode_flags = MIPI_DSI_MODE_NO_EOT_PACKET, 416 .bus_flags = 0, 417 418 .has_backlight = true, 419 .use_passwd3 = true, 420 }; 421 422 /* Initialization structures for LTL101AT01 panel */ 423 424 static const struct drm_display_mode s6d7aa0_ltl101at01_mode = { 425 .clock = (768 + 96 + 16 + 184) * (1024 + 8 + 2 + 6) * 60 / 1000, 426 .hdisplay = 768, 427 .hsync_start = 768 + 96, 428 .hsync_end = 768 + 96 + 16, 429 .htotal = 768 + 96 + 16 + 184, 430 .vdisplay = 1024, 431 .vsync_start = 1024 + 8, 432 .vsync_end = 1024 + 8 + 2, 433 .vtotal = 1024 + 8 + 2 + 6, 434 .width_mm = 148, 435 .height_mm = 197, 436 }; 437 438 static const struct s6d7aa0_panel_desc s6d7aa0_ltl101at01_desc = { 439 .panel_type = S6D7AA0_PANEL_LTL101AT01, 440 .init_func = s6d7aa0_lsl080al03_init, /* Similar init to LSL080AL03 */ 441 .off_func = s6d7aa0_lsl080al03_off, 442 .drm_mode = &s6d7aa0_ltl101at01_mode, 443 .mode_flags = MIPI_DSI_MODE_NO_EOT_PACKET, 444 .bus_flags = 0, 445 446 .has_backlight = true, 447 .use_passwd3 = true, 448 }; 449 450 static int s6d7aa0_get_modes(struct drm_panel *panel, 451 struct drm_connector *connector) 452 { 453 struct drm_display_mode *mode; 454 struct s6d7aa0 *ctx; 455 456 ctx = container_of(panel, struct s6d7aa0, panel); 457 if (!ctx) 458 return -EINVAL; 459 460 mode = drm_mode_duplicate(connector->dev, ctx->desc->drm_mode); 461 if (!mode) 462 return -ENOMEM; 463 464 drm_mode_set_name(mode); 465 466 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 467 connector->display_info.width_mm = mode->width_mm; 468 connector->display_info.height_mm = mode->height_mm; 469 connector->display_info.bus_flags = ctx->desc->bus_flags; 470 drm_mode_probed_add(connector, mode); 471 472 return 1; 473 } 474 475 static const struct drm_panel_funcs s6d7aa0_panel_funcs = { 476 .disable = s6d7aa0_disable, 477 .prepare = s6d7aa0_prepare, 478 .unprepare = s6d7aa0_unprepare, 479 .get_modes = s6d7aa0_get_modes, 480 }; 481 482 static int s6d7aa0_probe(struct mipi_dsi_device *dsi) 483 { 484 struct device *dev = &dsi->dev; 485 struct s6d7aa0 *ctx; 486 int ret; 487 488 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 489 if (!ctx) 490 return -ENOMEM; 491 492 ctx->desc = of_device_get_match_data(dev); 493 if (!ctx->desc) 494 return -ENODEV; 495 496 ctx->supplies[0].supply = "power"; 497 ctx->supplies[1].supply = "vmipi"; 498 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies), 499 ctx->supplies); 500 if (ret < 0) 501 return dev_err_probe(dev, ret, "Failed to get regulators\n"); 502 503 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 504 if (IS_ERR(ctx->reset_gpio)) 505 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio), 506 "Failed to get reset-gpios\n"); 507 508 ctx->dsi = dsi; 509 mipi_dsi_set_drvdata(dsi, ctx); 510 511 dsi->lanes = 4; 512 dsi->format = MIPI_DSI_FMT_RGB888; 513 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST 514 | ctx->desc->mode_flags; 515 516 drm_panel_init(&ctx->panel, dev, &s6d7aa0_panel_funcs, 517 DRM_MODE_CONNECTOR_DSI); 518 ctx->panel.prepare_prev_first = true; 519 520 ret = drm_panel_of_backlight(&ctx->panel); 521 if (ret) 522 return dev_err_probe(dev, ret, "Failed to get backlight\n"); 523 524 /* Use DSI-based backlight as fallback if available */ 525 if (ctx->desc->has_backlight && !ctx->panel.backlight) { 526 ctx->panel.backlight = s6d7aa0_create_backlight(dsi); 527 if (IS_ERR(ctx->panel.backlight)) 528 return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight), 529 "Failed to create backlight\n"); 530 } 531 532 drm_panel_add(&ctx->panel); 533 534 ret = mipi_dsi_attach(dsi); 535 if (ret < 0) { 536 dev_err(dev, "Failed to attach to DSI host: %d\n", ret); 537 drm_panel_remove(&ctx->panel); 538 return ret; 539 } 540 541 return 0; 542 } 543 544 static void s6d7aa0_remove(struct mipi_dsi_device *dsi) 545 { 546 struct s6d7aa0 *ctx = mipi_dsi_get_drvdata(dsi); 547 int ret; 548 549 ret = mipi_dsi_detach(dsi); 550 if (ret < 0) 551 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret); 552 553 drm_panel_remove(&ctx->panel); 554 } 555 556 static const struct of_device_id s6d7aa0_of_match[] = { 557 { 558 .compatible = "samsung,lsl080al02", 559 .data = &s6d7aa0_lsl080al02_desc 560 }, 561 { 562 .compatible = "samsung,lsl080al03", 563 .data = &s6d7aa0_lsl080al03_desc 564 }, 565 { 566 .compatible = "samsung,ltl101at01", 567 .data = &s6d7aa0_ltl101at01_desc 568 }, 569 { /* sentinel */ } 570 }; 571 572 static struct mipi_dsi_driver s6d7aa0_driver = { 573 .probe = s6d7aa0_probe, 574 .remove = s6d7aa0_remove, 575 .driver = { 576 .name = "panel-samsung-s6d7aa0", 577 .of_match_table = s6d7aa0_of_match, 578 }, 579 }; 580 module_mipi_dsi_driver(s6d7aa0_driver); 581 582 MODULE_AUTHOR("Artur Weber <aweber.kernel@gmail.com>"); 583 MODULE_DESCRIPTION("Samsung S6D7AA0 MIPI-DSI LCD controller driver"); 584 MODULE_LICENSE("GPL"); 585