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;
45 static const struct ltk500hd1829_cmd ltk101b4029w_init[] = {
52 { 0x80, 0x03 }, /* 0X03:4-LANE; 0X02:3-LANE; 0X01:2-LANE */
58 /* Set Gamma Power, VGMP,VGMN,VGSP,VGSN */
60 { 0x18, 0xAF }, /* 4.3V */
61 { 0x19, 0x01 }, /* 0.3V */
63 { 0x1B, 0xAF }, /* 4.3V */
64 { 0x1C, 0x01 }, /* 0.3V */
66 { 0x1F, 0x3E }, /* VGH_R = 15V */
67 { 0x20, 0x28 }, /* VGL_R = -12V */
68 { 0x21, 0x28 }, /* VGL_R2 = -12V */
82 { 0x40, 0x06 }, /* RSO = 800 RGB */
83 { 0x41, 0xA0 }, /* LN = 640->1280 line */
85 { 0x43, 0x08 }, /* VFP = 8 */
86 { 0x44, 0x0B }, /* VBP = 12 */
87 { 0x45, 0x28 }, /* HBP = 40 */
89 { 0x55, 0x0F }, /* DCDCM = 0001, JD PWR_IC */
91 { 0x59, 0x0A }, /* VCL = -2.9V */
92 { 0x5A, 0x28 }, /* VGH = 15V */
93 { 0x5B, 0x14 }, /* VGL = -11V */
135 /* GIP_L Pin mapping */
158 /* GIP_R Pin mapping */
217 static const struct drm_display_mode ltk101b4029w_mode = {
219 .hsync_start = 800 + 18,
220 .hsync_end = 800 + 18 + 18,
221 .htotal = 800 + 18 + 18 + 18,
223 .vsync_start = 1280 + 24,
224 .vsync_end = 1280 + 24 + 4,
225 .vtotal = 1280 + 24 + 4 + 8,
231 static const struct ltk500hd1829_desc ltk101b4029w_data = {
232 .mode = <k101b4029w_mode,
233 .init = ltk101b4029w_init,
234 .num_init = ARRAY_SIZE(ltk101b4029w_init),
238 * There is no description in the Reference Manual about these commands.
239 * We received them from the vendor, so just use them as is.
241 static const struct ltk500hd1829_cmd ltk500hd1829_init[] = {
462 static const struct drm_display_mode ltk500hd1829_mode = {
464 .hsync_start = 720 + 50,
465 .hsync_end = 720 + 50 + 50,
466 .htotal = 720 + 50 + 50 + 50,
468 .vsync_start = 1280 + 30,
469 .vsync_end = 1280 + 30 + 4,
470 .vtotal = 1280 + 30 + 4 + 12,
476 static const struct ltk500hd1829_desc ltk500hd1829_data = {
477 .mode = <k500hd1829_mode,
478 .init = ltk500hd1829_init,
479 .num_init = ARRAY_SIZE(ltk500hd1829_init),
483 struct ltk500hd1829 *panel_to_ltk500hd1829(struct drm_panel *panel)
485 return container_of(panel, struct ltk500hd1829, panel);
488 static int ltk500hd1829_unprepare(struct drm_panel *panel)
490 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
491 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
494 ret = mipi_dsi_dcs_set_display_off(dsi);
496 dev_err(panel->dev, "failed to set display off: %d\n", ret);
498 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
500 dev_err(panel->dev, "failed to enter sleep mode: %d\n", ret);
503 /* 120ms to enter sleep mode */
506 regulator_disable(ctx->iovcc);
507 regulator_disable(ctx->vcc);
512 static int ltk500hd1829_prepare(struct drm_panel *panel)
514 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
515 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
519 ret = regulator_enable(ctx->vcc);
521 dev_err(ctx->dev, "Failed to enable vci supply: %d\n", ret);
524 ret = regulator_enable(ctx->iovcc);
526 dev_err(ctx->dev, "Failed to enable iovcc supply: %d\n", ret);
530 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
532 usleep_range(10, 20);
533 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
536 usleep_range(5000, 6000);
538 for (i = 0; i < ctx->panel_desc->num_init; i++) {
539 ret = mipi_dsi_generic_write(dsi, &ctx->panel_desc->init[i],
540 sizeof(struct ltk500hd1829_cmd));
542 dev_err(panel->dev, "failed to write init cmds: %d\n", ret);
547 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
549 dev_err(panel->dev, "failed to exit sleep mode: %d\n", ret);
553 /* 120ms to exit sleep mode */
556 ret = mipi_dsi_dcs_set_display_on(dsi);
558 dev_err(panel->dev, "failed to set display on: %d\n", ret);
565 regulator_disable(ctx->iovcc);
567 regulator_disable(ctx->vcc);
571 static int ltk500hd1829_get_modes(struct drm_panel *panel,
572 struct drm_connector *connector)
574 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
575 struct drm_display_mode *mode;
577 mode = drm_mode_duplicate(connector->dev, ctx->panel_desc->mode);
579 dev_err(ctx->dev, "failed to add mode %ux%u@%u\n",
580 ctx->panel_desc->mode->hdisplay, ctx->panel_desc->mode->vdisplay,
581 drm_mode_vrefresh(ctx->panel_desc->mode));
585 drm_mode_set_name(mode);
587 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
588 connector->display_info.width_mm = mode->width_mm;
589 connector->display_info.height_mm = mode->height_mm;
590 drm_mode_probed_add(connector, mode);
595 static const struct drm_panel_funcs ltk500hd1829_funcs = {
596 .unprepare = ltk500hd1829_unprepare,
597 .prepare = ltk500hd1829_prepare,
598 .get_modes = ltk500hd1829_get_modes,
601 static int ltk500hd1829_probe(struct mipi_dsi_device *dsi)
603 struct ltk500hd1829 *ctx;
604 struct device *dev = &dsi->dev;
607 ctx = devm_kzalloc(&dsi->dev, sizeof(*ctx), GFP_KERNEL);
611 ctx->panel_desc = of_device_get_match_data(dev);
612 if (!ctx->panel_desc)
615 ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
616 if (IS_ERR(ctx->reset_gpio)) {
617 dev_err(dev, "cannot get reset gpio\n");
618 return PTR_ERR(ctx->reset_gpio);
621 ctx->vcc = devm_regulator_get(dev, "vcc");
622 if (IS_ERR(ctx->vcc)) {
623 ret = PTR_ERR(ctx->vcc);
624 if (ret != -EPROBE_DEFER)
625 dev_err(dev, "Failed to request vcc regulator: %d\n", ret);
629 ctx->iovcc = devm_regulator_get(dev, "iovcc");
630 if (IS_ERR(ctx->iovcc)) {
631 ret = PTR_ERR(ctx->iovcc);
632 if (ret != -EPROBE_DEFER)
633 dev_err(dev, "Failed to request iovcc regulator: %d\n", ret);
637 mipi_dsi_set_drvdata(dsi, ctx);
642 dsi->format = MIPI_DSI_FMT_RGB888;
643 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
644 MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_NO_EOT_PACKET;
646 drm_panel_init(&ctx->panel, &dsi->dev, <k500hd1829_funcs,
647 DRM_MODE_CONNECTOR_DSI);
649 ret = drm_panel_of_backlight(&ctx->panel);
653 drm_panel_add(&ctx->panel);
655 ret = mipi_dsi_attach(dsi);
657 dev_err(dev, "mipi_dsi_attach failed: %d\n", ret);
658 drm_panel_remove(&ctx->panel);
665 static void ltk500hd1829_remove(struct mipi_dsi_device *dsi)
667 struct ltk500hd1829 *ctx = mipi_dsi_get_drvdata(dsi);
670 ret = mipi_dsi_detach(dsi);
672 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
674 drm_panel_remove(&ctx->panel);
677 static const struct of_device_id ltk500hd1829_of_match[] = {
679 .compatible = "leadtek,ltk101b4029w",
680 .data = <k101b4029w_data,
683 .compatible = "leadtek,ltk500hd1829",
684 .data = <k500hd1829_data,
688 MODULE_DEVICE_TABLE(of, ltk500hd1829_of_match);
690 static struct mipi_dsi_driver ltk500hd1829_driver = {
692 .name = "panel-leadtek-ltk500hd1829",
693 .of_match_table = ltk500hd1829_of_match,
695 .probe = ltk500hd1829_probe,
696 .remove = ltk500hd1829_remove,
698 module_mipi_dsi_driver(ltk500hd1829_driver);
701 MODULE_DESCRIPTION("Leadtek LTK500HD1829 panel driver");
702 MODULE_LICENSE("GPL v2");