1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 Theobroma Systems Design und Consulting GmbH
5 * base on panel-kingdisplay-kd097d04.c
6 * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd
9 #include <linux/backlight.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/regulator/consumer.h>
17 #include <video/mipi_display.h>
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_mipi_dsi.h>
22 #include <drm/drm_modes.h>
23 #include <drm/drm_panel.h>
25 struct ltk500hd1829_cmd {
30 struct ltk500hd1829_desc {
31 const struct drm_display_mode *mode;
32 const struct ltk500hd1829_cmd *init;
33 unsigned int num_init;
38 struct drm_panel panel;
39 struct gpio_desc *reset_gpio;
40 struct regulator *vcc;
41 struct regulator *iovcc;
42 const struct ltk500hd1829_desc *panel_desc;
46 static const struct ltk500hd1829_cmd ltk101b4029w_init[] = {
53 { 0x80, 0x03 }, /* 0X03:4-LANE; 0X02:3-LANE; 0X01:2-LANE */
59 /* Set Gamma Power, VGMP,VGMN,VGSP,VGSN */
61 { 0x18, 0xAF }, /* 4.3V */
62 { 0x19, 0x01 }, /* 0.3V */
64 { 0x1B, 0xAF }, /* 4.3V */
65 { 0x1C, 0x01 }, /* 0.3V */
67 { 0x1F, 0x3E }, /* VGH_R = 15V */
68 { 0x20, 0x28 }, /* VGL_R = -12V */
69 { 0x21, 0x28 }, /* VGL_R2 = -12V */
83 { 0x40, 0x06 }, /* RSO = 800 RGB */
84 { 0x41, 0xA0 }, /* LN = 640->1280 line */
86 { 0x43, 0x08 }, /* VFP = 8 */
87 { 0x44, 0x0B }, /* VBP = 12 */
88 { 0x45, 0x28 }, /* HBP = 40 */
90 { 0x55, 0x0F }, /* DCDCM = 0001, JD PWR_IC */
92 { 0x59, 0x0A }, /* VCL = -2.9V */
93 { 0x5A, 0x28 }, /* VGH = 15V */
94 { 0x5B, 0x14 }, /* VGL = -11V */
136 /* GIP_L Pin mapping */
159 /* GIP_R Pin mapping */
218 static const struct drm_display_mode ltk101b4029w_mode = {
220 .hsync_start = 800 + 18,
221 .hsync_end = 800 + 18 + 18,
222 .htotal = 800 + 18 + 18 + 18,
224 .vsync_start = 1280 + 24,
225 .vsync_end = 1280 + 24 + 4,
226 .vtotal = 1280 + 24 + 4 + 8,
232 static const struct ltk500hd1829_desc ltk101b4029w_data = {
233 .mode = <k101b4029w_mode,
234 .init = ltk101b4029w_init,
235 .num_init = ARRAY_SIZE(ltk101b4029w_init),
239 * There is no description in the Reference Manual about these commands.
240 * We received them from the vendor, so just use them as is.
242 static const struct ltk500hd1829_cmd ltk500hd1829_init[] = {
463 static const struct drm_display_mode ltk500hd1829_mode = {
465 .hsync_start = 720 + 50,
466 .hsync_end = 720 + 50 + 50,
467 .htotal = 720 + 50 + 50 + 50,
469 .vsync_start = 1280 + 30,
470 .vsync_end = 1280 + 30 + 4,
471 .vtotal = 1280 + 30 + 4 + 12,
477 static const struct ltk500hd1829_desc ltk500hd1829_data = {
478 .mode = <k500hd1829_mode,
479 .init = ltk500hd1829_init,
480 .num_init = ARRAY_SIZE(ltk500hd1829_init),
484 struct ltk500hd1829 *panel_to_ltk500hd1829(struct drm_panel *panel)
486 return container_of(panel, struct ltk500hd1829, panel);
489 static int ltk500hd1829_unprepare(struct drm_panel *panel)
491 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
492 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
498 ret = mipi_dsi_dcs_set_display_off(dsi);
500 dev_err(panel->dev, "failed to set display off: %d\n", ret);
502 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
504 dev_err(panel->dev, "failed to enter sleep mode: %d\n", ret);
507 /* 120ms to enter sleep mode */
510 regulator_disable(ctx->iovcc);
511 regulator_disable(ctx->vcc);
513 ctx->prepared = false;
518 static int ltk500hd1829_prepare(struct drm_panel *panel)
520 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
521 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
528 ret = regulator_enable(ctx->vcc);
530 dev_err(ctx->dev, "Failed to enable vci supply: %d\n", ret);
533 ret = regulator_enable(ctx->iovcc);
535 dev_err(ctx->dev, "Failed to enable iovcc supply: %d\n", ret);
539 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
541 usleep_range(10, 20);
542 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
545 usleep_range(5000, 6000);
547 for (i = 0; i < ctx->panel_desc->num_init; i++) {
548 ret = mipi_dsi_generic_write(dsi, &ctx->panel_desc->init[i],
549 sizeof(struct ltk500hd1829_cmd));
551 dev_err(panel->dev, "failed to write init cmds: %d\n", ret);
556 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
558 dev_err(panel->dev, "failed to exit sleep mode: %d\n", ret);
562 /* 120ms to exit sleep mode */
565 ret = mipi_dsi_dcs_set_display_on(dsi);
567 dev_err(panel->dev, "failed to set display on: %d\n", ret);
571 ctx->prepared = true;
576 regulator_disable(ctx->iovcc);
578 regulator_disable(ctx->vcc);
582 static int ltk500hd1829_get_modes(struct drm_panel *panel,
583 struct drm_connector *connector)
585 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
586 struct drm_display_mode *mode;
588 mode = drm_mode_duplicate(connector->dev, ctx->panel_desc->mode);
590 dev_err(ctx->dev, "failed to add mode %ux%u@%u\n",
591 ctx->panel_desc->mode->hdisplay, ctx->panel_desc->mode->vdisplay,
592 drm_mode_vrefresh(ctx->panel_desc->mode));
596 drm_mode_set_name(mode);
598 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
599 connector->display_info.width_mm = mode->width_mm;
600 connector->display_info.height_mm = mode->height_mm;
601 drm_mode_probed_add(connector, mode);
606 static const struct drm_panel_funcs ltk500hd1829_funcs = {
607 .unprepare = ltk500hd1829_unprepare,
608 .prepare = ltk500hd1829_prepare,
609 .get_modes = ltk500hd1829_get_modes,
612 static int ltk500hd1829_probe(struct mipi_dsi_device *dsi)
614 struct ltk500hd1829 *ctx;
615 struct device *dev = &dsi->dev;
618 ctx = devm_kzalloc(&dsi->dev, sizeof(*ctx), GFP_KERNEL);
622 ctx->panel_desc = of_device_get_match_data(dev);
623 if (!ctx->panel_desc)
626 ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
627 if (IS_ERR(ctx->reset_gpio)) {
628 dev_err(dev, "cannot get reset gpio\n");
629 return PTR_ERR(ctx->reset_gpio);
632 ctx->vcc = devm_regulator_get(dev, "vcc");
633 if (IS_ERR(ctx->vcc)) {
634 ret = PTR_ERR(ctx->vcc);
635 if (ret != -EPROBE_DEFER)
636 dev_err(dev, "Failed to request vcc regulator: %d\n", ret);
640 ctx->iovcc = devm_regulator_get(dev, "iovcc");
641 if (IS_ERR(ctx->iovcc)) {
642 ret = PTR_ERR(ctx->iovcc);
643 if (ret != -EPROBE_DEFER)
644 dev_err(dev, "Failed to request iovcc regulator: %d\n", ret);
648 mipi_dsi_set_drvdata(dsi, ctx);
653 dsi->format = MIPI_DSI_FMT_RGB888;
654 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
655 MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_NO_EOT_PACKET;
657 drm_panel_init(&ctx->panel, &dsi->dev, <k500hd1829_funcs,
658 DRM_MODE_CONNECTOR_DSI);
660 ret = drm_panel_of_backlight(&ctx->panel);
664 drm_panel_add(&ctx->panel);
666 ret = mipi_dsi_attach(dsi);
668 dev_err(dev, "mipi_dsi_attach failed: %d\n", ret);
669 drm_panel_remove(&ctx->panel);
676 static void ltk500hd1829_shutdown(struct mipi_dsi_device *dsi)
678 struct ltk500hd1829 *ctx = mipi_dsi_get_drvdata(dsi);
681 ret = drm_panel_unprepare(&ctx->panel);
683 dev_err(&dsi->dev, "Failed to unprepare panel: %d\n", ret);
685 ret = drm_panel_disable(&ctx->panel);
687 dev_err(&dsi->dev, "Failed to disable panel: %d\n", ret);
690 static void ltk500hd1829_remove(struct mipi_dsi_device *dsi)
692 struct ltk500hd1829 *ctx = mipi_dsi_get_drvdata(dsi);
695 ltk500hd1829_shutdown(dsi);
697 ret = mipi_dsi_detach(dsi);
699 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
701 drm_panel_remove(&ctx->panel);
704 static const struct of_device_id ltk500hd1829_of_match[] = {
706 .compatible = "leadtek,ltk101b4029w",
707 .data = <k101b4029w_data,
710 .compatible = "leadtek,ltk500hd1829",
711 .data = <k500hd1829_data,
715 MODULE_DEVICE_TABLE(of, ltk500hd1829_of_match);
717 static struct mipi_dsi_driver ltk500hd1829_driver = {
719 .name = "panel-leadtek-ltk500hd1829",
720 .of_match_table = ltk500hd1829_of_match,
722 .probe = ltk500hd1829_probe,
723 .remove = ltk500hd1829_remove,
724 .shutdown = ltk500hd1829_shutdown,
726 module_mipi_dsi_driver(ltk500hd1829_driver);
729 MODULE_DESCRIPTION("Leadtek LTK500HD1829 panel driver");
730 MODULE_LICENSE("GPL v2");