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>
26 struct drm_panel panel
;
27 struct gpio_desc
*reset_gpio
;
28 struct regulator
*vcc
;
29 struct regulator
*iovcc
;
33 struct ltk500hd1829_cmd
{
39 * There is no description in the Reference Manual about these commands.
40 * We received them from the vendor, so just use them as is.
42 static const struct ltk500hd1829_cmd init_code
[] = {
264 struct ltk500hd1829
*panel_to_ltk500hd1829(struct drm_panel
*panel
)
266 return container_of(panel
, struct ltk500hd1829
, panel
);
269 static int ltk500hd1829_unprepare(struct drm_panel
*panel
)
271 struct ltk500hd1829
*ctx
= panel_to_ltk500hd1829(panel
);
272 struct mipi_dsi_device
*dsi
= to_mipi_dsi_device(ctx
->dev
);
278 ret
= mipi_dsi_dcs_set_display_off(dsi
);
280 dev_err(panel
->dev
, "failed to set display off: %d\n", ret
);
282 ret
= mipi_dsi_dcs_enter_sleep_mode(dsi
);
284 dev_err(panel
->dev
, "failed to enter sleep mode: %d\n", ret
);
287 /* 120ms to enter sleep mode */
290 regulator_disable(ctx
->iovcc
);
291 regulator_disable(ctx
->vcc
);
293 ctx
->prepared
= false;
298 static int ltk500hd1829_prepare(struct drm_panel
*panel
)
300 struct ltk500hd1829
*ctx
= panel_to_ltk500hd1829(panel
);
301 struct mipi_dsi_device
*dsi
= to_mipi_dsi_device(ctx
->dev
);
308 ret
= regulator_enable(ctx
->vcc
);
310 dev_err(ctx
->dev
, "Failed to enable vci supply: %d\n", ret
);
313 ret
= regulator_enable(ctx
->iovcc
);
315 dev_err(ctx
->dev
, "Failed to enable iovcc supply: %d\n", ret
);
319 gpiod_set_value_cansleep(ctx
->reset_gpio
, 1);
321 usleep_range(10, 20);
322 gpiod_set_value_cansleep(ctx
->reset_gpio
, 0);
325 usleep_range(5000, 6000);
327 for (i
= 0; i
< ARRAY_SIZE(init_code
); i
++) {
328 ret
= mipi_dsi_generic_write(dsi
, &init_code
[i
],
329 sizeof(struct ltk500hd1829_cmd
));
331 dev_err(panel
->dev
, "failed to write init cmds: %d\n", ret
);
336 ret
= mipi_dsi_dcs_exit_sleep_mode(dsi
);
338 dev_err(panel
->dev
, "failed to exit sleep mode: %d\n", ret
);
342 /* 120ms to exit sleep mode */
345 ret
= mipi_dsi_dcs_set_display_on(dsi
);
347 dev_err(panel
->dev
, "failed to set display on: %d\n", ret
);
351 ctx
->prepared
= true;
356 regulator_disable(ctx
->iovcc
);
358 regulator_disable(ctx
->vcc
);
362 static const struct drm_display_mode default_mode
= {
364 .hsync_start
= 720 + 50,
365 .hsync_end
= 720 + 50 + 50,
366 .htotal
= 720 + 50 + 50 + 50,
368 .vsync_start
= 1280 + 30,
369 .vsync_end
= 1280 + 30 + 4,
370 .vtotal
= 1280 + 30 + 4 + 12,
376 static int ltk500hd1829_get_modes(struct drm_panel
*panel
,
377 struct drm_connector
*connector
)
379 struct ltk500hd1829
*ctx
= panel_to_ltk500hd1829(panel
);
380 struct drm_display_mode
*mode
;
382 mode
= drm_mode_duplicate(connector
->dev
, &default_mode
);
384 dev_err(ctx
->dev
, "failed to add mode %ux%u@%u\n",
385 default_mode
.hdisplay
, default_mode
.vdisplay
,
386 drm_mode_vrefresh(&default_mode
));
390 drm_mode_set_name(mode
);
392 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
393 connector
->display_info
.width_mm
= mode
->width_mm
;
394 connector
->display_info
.height_mm
= mode
->height_mm
;
395 drm_mode_probed_add(connector
, mode
);
400 static const struct drm_panel_funcs ltk500hd1829_funcs
= {
401 .unprepare
= ltk500hd1829_unprepare
,
402 .prepare
= ltk500hd1829_prepare
,
403 .get_modes
= ltk500hd1829_get_modes
,
406 static int ltk500hd1829_probe(struct mipi_dsi_device
*dsi
)
408 struct ltk500hd1829
*ctx
;
409 struct device
*dev
= &dsi
->dev
;
412 ctx
= devm_kzalloc(&dsi
->dev
, sizeof(*ctx
), GFP_KERNEL
);
416 ctx
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
417 if (IS_ERR(ctx
->reset_gpio
)) {
418 dev_err(dev
, "cannot get reset gpio\n");
419 return PTR_ERR(ctx
->reset_gpio
);
422 ctx
->vcc
= devm_regulator_get(dev
, "vcc");
423 if (IS_ERR(ctx
->vcc
)) {
424 ret
= PTR_ERR(ctx
->vcc
);
425 if (ret
!= -EPROBE_DEFER
)
426 dev_err(dev
, "Failed to request vcc regulator: %d\n", ret
);
430 ctx
->iovcc
= devm_regulator_get(dev
, "iovcc");
431 if (IS_ERR(ctx
->iovcc
)) {
432 ret
= PTR_ERR(ctx
->iovcc
);
433 if (ret
!= -EPROBE_DEFER
)
434 dev_err(dev
, "Failed to request iovcc regulator: %d\n", ret
);
438 mipi_dsi_set_drvdata(dsi
, ctx
);
443 dsi
->format
= MIPI_DSI_FMT_RGB888
;
444 dsi
->mode_flags
= MIPI_DSI_MODE_VIDEO
| MIPI_DSI_MODE_VIDEO_BURST
|
445 MIPI_DSI_MODE_LPM
| MIPI_DSI_MODE_EOT_PACKET
;
447 drm_panel_init(&ctx
->panel
, &dsi
->dev
, <k500hd1829_funcs
,
448 DRM_MODE_CONNECTOR_DSI
);
450 ret
= drm_panel_of_backlight(&ctx
->panel
);
454 drm_panel_add(&ctx
->panel
);
456 ret
= mipi_dsi_attach(dsi
);
458 dev_err(dev
, "mipi_dsi_attach failed: %d\n", ret
);
459 drm_panel_remove(&ctx
->panel
);
466 static void ltk500hd1829_shutdown(struct mipi_dsi_device
*dsi
)
468 struct ltk500hd1829
*ctx
= mipi_dsi_get_drvdata(dsi
);
471 ret
= drm_panel_unprepare(&ctx
->panel
);
473 dev_err(&dsi
->dev
, "Failed to unprepare panel: %d\n", ret
);
475 ret
= drm_panel_disable(&ctx
->panel
);
477 dev_err(&dsi
->dev
, "Failed to disable panel: %d\n", ret
);
480 static int ltk500hd1829_remove(struct mipi_dsi_device
*dsi
)
482 struct ltk500hd1829
*ctx
= mipi_dsi_get_drvdata(dsi
);
485 ltk500hd1829_shutdown(dsi
);
487 ret
= mipi_dsi_detach(dsi
);
489 dev_err(&dsi
->dev
, "failed to detach from DSI host: %d\n", ret
);
491 drm_panel_remove(&ctx
->panel
);
496 static const struct of_device_id ltk500hd1829_of_match
[] = {
497 { .compatible
= "leadtek,ltk500hd1829", },
500 MODULE_DEVICE_TABLE(of
, ltk500hd1829_of_match
);
502 static struct mipi_dsi_driver ltk500hd1829_driver
= {
504 .name
= "panel-leadtek-ltk500hd1829",
505 .of_match_table
= ltk500hd1829_of_match
,
507 .probe
= ltk500hd1829_probe
,
508 .remove
= ltk500hd1829_remove
,
509 .shutdown
= ltk500hd1829_shutdown
,
511 module_mipi_dsi_driver(ltk500hd1829_driver
);
513 MODULE_AUTHOR("Heiko Stuebner <heiko.stuebner@theobroma-systems.com>");
514 MODULE_DESCRIPTION("Leadtek LTK500HD1829 panel driver");
515 MODULE_LICENSE("GPL v2");