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,
385 static int ltk500hd1829_get_modes(struct drm_panel
*panel
,
386 struct drm_connector
*connector
)
388 struct ltk500hd1829
*ctx
= panel_to_ltk500hd1829(panel
);
389 struct drm_display_mode
*mode
;
391 mode
= drm_mode_duplicate(connector
->dev
, &default_mode
);
393 DRM_DEV_ERROR(ctx
->dev
, "failed to add mode %ux%ux@%u\n",
394 default_mode
.hdisplay
, default_mode
.vdisplay
,
395 default_mode
.vrefresh
);
399 drm_mode_set_name(mode
);
401 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
402 connector
->display_info
.width_mm
= mode
->width_mm
;
403 connector
->display_info
.height_mm
= mode
->height_mm
;
404 drm_mode_probed_add(connector
, mode
);
409 static const struct drm_panel_funcs ltk500hd1829_funcs
= {
410 .unprepare
= ltk500hd1829_unprepare
,
411 .prepare
= ltk500hd1829_prepare
,
412 .get_modes
= ltk500hd1829_get_modes
,
415 static int ltk500hd1829_probe(struct mipi_dsi_device
*dsi
)
417 struct ltk500hd1829
*ctx
;
418 struct device
*dev
= &dsi
->dev
;
421 ctx
= devm_kzalloc(&dsi
->dev
, sizeof(*ctx
), GFP_KERNEL
);
425 ctx
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
426 if (IS_ERR(ctx
->reset_gpio
)) {
427 DRM_DEV_ERROR(dev
, "cannot get reset gpio\n");
428 return PTR_ERR(ctx
->reset_gpio
);
431 ctx
->vcc
= devm_regulator_get(dev
, "vcc");
432 if (IS_ERR(ctx
->vcc
)) {
433 ret
= PTR_ERR(ctx
->vcc
);
434 if (ret
!= -EPROBE_DEFER
)
436 "Failed to request vcc regulator: %d\n",
441 ctx
->iovcc
= devm_regulator_get(dev
, "iovcc");
442 if (IS_ERR(ctx
->iovcc
)) {
443 ret
= PTR_ERR(ctx
->iovcc
);
444 if (ret
!= -EPROBE_DEFER
)
446 "Failed to request iovcc regulator: %d\n",
451 mipi_dsi_set_drvdata(dsi
, ctx
);
456 dsi
->format
= MIPI_DSI_FMT_RGB888
;
457 dsi
->mode_flags
= MIPI_DSI_MODE_VIDEO
| MIPI_DSI_MODE_VIDEO_BURST
|
458 MIPI_DSI_MODE_LPM
| MIPI_DSI_MODE_EOT_PACKET
;
460 drm_panel_init(&ctx
->panel
, &dsi
->dev
, <k500hd1829_funcs
,
461 DRM_MODE_CONNECTOR_DSI
);
463 ret
= drm_panel_of_backlight(&ctx
->panel
);
467 drm_panel_add(&ctx
->panel
);
469 ret
= mipi_dsi_attach(dsi
);
471 DRM_DEV_ERROR(dev
, "mipi_dsi_attach failed: %d\n", ret
);
472 drm_panel_remove(&ctx
->panel
);
479 static void ltk500hd1829_shutdown(struct mipi_dsi_device
*dsi
)
481 struct ltk500hd1829
*ctx
= mipi_dsi_get_drvdata(dsi
);
484 ret
= drm_panel_unprepare(&ctx
->panel
);
486 DRM_DEV_ERROR(&dsi
->dev
, "Failed to unprepare panel: %d\n",
489 ret
= drm_panel_disable(&ctx
->panel
);
491 DRM_DEV_ERROR(&dsi
->dev
, "Failed to disable panel: %d\n",
495 static int ltk500hd1829_remove(struct mipi_dsi_device
*dsi
)
497 struct ltk500hd1829
*ctx
= mipi_dsi_get_drvdata(dsi
);
500 ltk500hd1829_shutdown(dsi
);
502 ret
= mipi_dsi_detach(dsi
);
504 DRM_DEV_ERROR(&dsi
->dev
, "failed to detach from DSI host: %d\n",
507 drm_panel_remove(&ctx
->panel
);
512 static const struct of_device_id ltk500hd1829_of_match
[] = {
513 { .compatible
= "leadtek,ltk500hd1829", },
516 MODULE_DEVICE_TABLE(of
, ltk500hd1829_of_match
);
518 static struct mipi_dsi_driver ltk500hd1829_driver
= {
520 .name
= "panel-leadtek-ltk500hd1829",
521 .of_match_table
= ltk500hd1829_of_match
,
523 .probe
= ltk500hd1829_probe
,
524 .remove
= ltk500hd1829_remove
,
525 .shutdown
= ltk500hd1829_shutdown
,
527 module_mipi_dsi_driver(ltk500hd1829_driver
);
529 MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@theobroma-systems.com>");
530 MODULE_DESCRIPTION("Leadtek LTK500HD1829 panel driver");
531 MODULE_LICENSE("GPL v2");