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/regulator/consumer.h>
16 #include <video/mipi_display.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_mipi_dsi.h>
21 #include <drm/drm_modes.h>
22 #include <drm/drm_panel.h>
23 #include <drm/drm_print.h>
27 struct drm_panel panel;
28 struct gpio_desc *reset_gpio;
29 struct regulator *vcc;
30 struct regulator *iovcc;
34 struct ltk500hd1829_cmd {
40 * There is no description in the Reference Manual about these commands.
41 * We received them from the vendor, so just use them as is.
43 static const struct ltk500hd1829_cmd init_code[] = {
265 struct ltk500hd1829 *panel_to_ltk500hd1829(struct drm_panel *panel)
267 return container_of(panel, struct ltk500hd1829, panel);
270 static int ltk500hd1829_unprepare(struct drm_panel *panel)
272 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
273 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
279 ret = mipi_dsi_dcs_set_display_off(dsi);
281 DRM_DEV_ERROR(panel->dev, "failed to set display off: %d\n",
284 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
286 DRM_DEV_ERROR(panel->dev, "failed to enter sleep mode: %d\n",
290 /* 120ms to enter sleep mode */
293 regulator_disable(ctx->iovcc);
294 regulator_disable(ctx->vcc);
296 ctx->prepared = false;
301 static int ltk500hd1829_prepare(struct drm_panel *panel)
303 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
304 struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
311 ret = regulator_enable(ctx->vcc);
313 DRM_DEV_ERROR(ctx->dev,
314 "Failed to enable vci supply: %d\n", ret);
317 ret = regulator_enable(ctx->iovcc);
319 DRM_DEV_ERROR(ctx->dev,
320 "Failed to enable iovcc supply: %d\n", ret);
324 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
326 usleep_range(10, 20);
327 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
330 usleep_range(5000, 6000);
332 for (i = 0; i < ARRAY_SIZE(init_code); i++) {
333 ret = mipi_dsi_generic_write(dsi, &init_code[i],
334 sizeof(struct ltk500hd1829_cmd));
336 DRM_DEV_ERROR(panel->dev,
337 "failed to write init cmds: %d\n", ret);
342 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
344 DRM_DEV_ERROR(panel->dev, "failed to exit sleep mode: %d\n",
349 /* 120ms to exit sleep mode */
352 ret = mipi_dsi_dcs_set_display_on(dsi);
354 DRM_DEV_ERROR(panel->dev, "failed to set display on: %d\n",
359 ctx->prepared = true;
364 regulator_disable(ctx->iovcc);
366 regulator_disable(ctx->vcc);
370 static const struct drm_display_mode default_mode = {
372 .hsync_start = 720 + 50,
373 .hsync_end = 720 + 50 + 50,
374 .htotal = 720 + 50 + 50 + 50,
376 .vsync_start = 1280 + 30,
377 .vsync_end = 1280 + 30 + 4,
378 .vtotal = 1280 + 30 + 4 + 12,
384 static int ltk500hd1829_get_modes(struct drm_panel *panel,
385 struct drm_connector *connector)
387 struct ltk500hd1829 *ctx = panel_to_ltk500hd1829(panel);
388 struct drm_display_mode *mode;
390 mode = drm_mode_duplicate(connector->dev, &default_mode);
392 DRM_DEV_ERROR(ctx->dev, "failed to add mode %ux%ux@%u\n",
393 default_mode.hdisplay, default_mode.vdisplay,
394 drm_mode_vrefresh(&default_mode));
398 drm_mode_set_name(mode);
400 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
401 connector->display_info.width_mm = mode->width_mm;
402 connector->display_info.height_mm = mode->height_mm;
403 drm_mode_probed_add(connector, mode);
408 static const struct drm_panel_funcs ltk500hd1829_funcs = {
409 .unprepare = ltk500hd1829_unprepare,
410 .prepare = ltk500hd1829_prepare,
411 .get_modes = ltk500hd1829_get_modes,
414 static int ltk500hd1829_probe(struct mipi_dsi_device *dsi)
416 struct ltk500hd1829 *ctx;
417 struct device *dev = &dsi->dev;
420 ctx = devm_kzalloc(&dsi->dev, sizeof(*ctx), GFP_KERNEL);
424 ctx->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
425 if (IS_ERR(ctx->reset_gpio)) {
426 DRM_DEV_ERROR(dev, "cannot get reset gpio\n");
427 return PTR_ERR(ctx->reset_gpio);
430 ctx->vcc = devm_regulator_get(dev, "vcc");
431 if (IS_ERR(ctx->vcc)) {
432 ret = PTR_ERR(ctx->vcc);
433 if (ret != -EPROBE_DEFER)
435 "Failed to request vcc regulator: %d\n",
440 ctx->iovcc = devm_regulator_get(dev, "iovcc");
441 if (IS_ERR(ctx->iovcc)) {
442 ret = PTR_ERR(ctx->iovcc);
443 if (ret != -EPROBE_DEFER)
445 "Failed to request iovcc regulator: %d\n",
450 mipi_dsi_set_drvdata(dsi, ctx);
455 dsi->format = MIPI_DSI_FMT_RGB888;
456 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
457 MIPI_DSI_MODE_LPM | MIPI_DSI_MODE_EOT_PACKET;
459 drm_panel_init(&ctx->panel, &dsi->dev, <k500hd1829_funcs,
460 DRM_MODE_CONNECTOR_DSI);
462 ret = drm_panel_of_backlight(&ctx->panel);
466 drm_panel_add(&ctx->panel);
468 ret = mipi_dsi_attach(dsi);
470 DRM_DEV_ERROR(dev, "mipi_dsi_attach failed: %d\n", ret);
471 drm_panel_remove(&ctx->panel);
478 static void ltk500hd1829_shutdown(struct mipi_dsi_device *dsi)
480 struct ltk500hd1829 *ctx = mipi_dsi_get_drvdata(dsi);
483 ret = drm_panel_unprepare(&ctx->panel);
485 DRM_DEV_ERROR(&dsi->dev, "Failed to unprepare panel: %d\n",
488 ret = drm_panel_disable(&ctx->panel);
490 DRM_DEV_ERROR(&dsi->dev, "Failed to disable panel: %d\n",
494 static int ltk500hd1829_remove(struct mipi_dsi_device *dsi)
496 struct ltk500hd1829 *ctx = mipi_dsi_get_drvdata(dsi);
499 ltk500hd1829_shutdown(dsi);
501 ret = mipi_dsi_detach(dsi);
503 DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n",
506 drm_panel_remove(&ctx->panel);
511 static const struct of_device_id ltk500hd1829_of_match[] = {
512 { .compatible = "leadtek,ltk500hd1829", },
515 MODULE_DEVICE_TABLE(of, ltk500hd1829_of_match);
517 static struct mipi_dsi_driver ltk500hd1829_driver = {
519 .name = "panel-leadtek-ltk500hd1829",
520 .of_match_table = ltk500hd1829_of_match,
522 .probe = ltk500hd1829_probe,
523 .remove = ltk500hd1829_remove,
524 .shutdown = ltk500hd1829_shutdown,
526 module_mipi_dsi_driver(ltk500hd1829_driver);
529 MODULE_DESCRIPTION("Leadtek LTK500HD1829 panel driver");
530 MODULE_LICENSE("GPL v2");