1 // SPDX-License-Identifier: GPL-2.0
3 * Samsung S6D7AA0 MIPI-DSI TFT LCD controller drm_panel driver.
5 * Copyright (C) 2022 Artur Weber <aweber.kernel@gmail.com>
8 #include <linux/backlight.h>
9 #include <linux/delay.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/module.h>
12 #include <linux/regulator/consumer.h>
15 #include <video/mipi_display.h>
16 #include <drm/drm_mipi_dsi.h>
17 #include <drm/drm_modes.h>
18 #include <drm/drm_panel.h>
20 /* Manufacturer command set */
21 #define MCS_BL_CTL 0xc3
22 #define MCS_OTP_RELOAD 0xd0
23 #define MCS_PASSWD1 0xf0
24 #define MCS_PASSWD2 0xf1
25 #define MCS_PASSWD3 0xfc
28 struct drm_panel panel;
29 struct mipi_dsi_device *dsi;
30 struct gpio_desc *reset_gpio;
31 struct regulator_bulk_data supplies[2];
32 const struct s6d7aa0_panel_desc *desc;
35 struct s6d7aa0_panel_desc {
36 unsigned int panel_type;
37 int (*init_func)(struct s6d7aa0 *ctx);
38 int (*off_func)(struct s6d7aa0 *ctx);
39 const struct drm_display_mode *drm_mode;
40 unsigned long mode_flags;
47 S6D7AA0_PANEL_LSL080AL02,
48 S6D7AA0_PANEL_LSL080AL03,
49 S6D7AA0_PANEL_LTL101AT01,
52 static inline struct s6d7aa0 *panel_to_s6d7aa0(struct drm_panel *panel)
54 return container_of(panel, struct s6d7aa0, panel);
57 static void s6d7aa0_reset(struct s6d7aa0 *ctx)
59 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
61 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
65 static int s6d7aa0_lock(struct s6d7aa0 *ctx, bool lock)
67 struct mipi_dsi_device *dsi = ctx->dsi;
70 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD1, 0xa5, 0xa5);
71 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD2, 0xa5, 0xa5);
72 if (ctx->desc->use_passwd3)
73 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD3, 0x5a, 0x5a);
75 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD1, 0x5a, 0x5a);
76 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD2, 0x5a, 0x5a);
77 if (ctx->desc->use_passwd3)
78 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD3, 0xa5, 0xa5);
84 static int s6d7aa0_on(struct s6d7aa0 *ctx)
86 struct mipi_dsi_device *dsi = ctx->dsi;
87 struct device *dev = &dsi->dev;
90 ret = ctx->desc->init_func(ctx);
92 dev_err(dev, "Failed to initialize panel: %d\n", ret);
93 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
97 ret = mipi_dsi_dcs_set_display_on(dsi);
99 dev_err(dev, "Failed to set display on: %d\n", ret);
106 static int s6d7aa0_off(struct s6d7aa0 *ctx)
108 struct mipi_dsi_device *dsi = ctx->dsi;
109 struct device *dev = &dsi->dev;
112 ret = ctx->desc->off_func(ctx);
114 dev_err(dev, "Panel-specific off function failed: %d\n", ret);
118 ret = mipi_dsi_dcs_set_display_off(dsi);
120 dev_err(dev, "Failed to set display off: %d\n", ret);
125 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
127 dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
135 static int s6d7aa0_prepare(struct drm_panel *panel)
137 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel);
138 struct device *dev = &ctx->dsi->dev;
141 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
143 dev_err(dev, "Failed to enable regulators: %d\n", ret);
149 ret = s6d7aa0_on(ctx);
151 dev_err(dev, "Failed to initialize panel: %d\n", ret);
152 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
159 static int s6d7aa0_disable(struct drm_panel *panel)
161 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel);
162 struct device *dev = &ctx->dsi->dev;
165 ret = s6d7aa0_off(ctx);
167 dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
172 static int s6d7aa0_unprepare(struct drm_panel *panel)
174 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel);
176 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
177 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
182 /* Backlight control code */
184 static int s6d7aa0_bl_update_status(struct backlight_device *bl)
186 struct mipi_dsi_device *dsi = bl_get_data(bl);
187 u16 brightness = backlight_get_brightness(bl);
190 ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
197 static int s6d7aa0_bl_get_brightness(struct backlight_device *bl)
199 struct mipi_dsi_device *dsi = bl_get_data(bl);
203 ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
207 return brightness & 0xff;
210 static const struct backlight_ops s6d7aa0_bl_ops = {
211 .update_status = s6d7aa0_bl_update_status,
212 .get_brightness = s6d7aa0_bl_get_brightness,
215 static struct backlight_device *
216 s6d7aa0_create_backlight(struct mipi_dsi_device *dsi)
218 struct device *dev = &dsi->dev;
219 const struct backlight_properties props = {
220 .type = BACKLIGHT_RAW,
222 .max_brightness = 255,
225 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
226 &s6d7aa0_bl_ops, &props);
229 /* Initialization code and structures for LSL080AL02 panel */
231 static int s6d7aa0_lsl080al02_init(struct s6d7aa0 *ctx)
233 struct mipi_dsi_device *dsi = ctx->dsi;
234 struct device *dev = &dsi->dev;
237 usleep_range(20000, 25000);
239 ret = s6d7aa0_lock(ctx, false);
241 dev_err(dev, "Failed to unlock registers: %d\n", ret);
245 mipi_dsi_dcs_write_seq(dsi, MCS_OTP_RELOAD, 0x00, 0x10);
246 usleep_range(1000, 1500);
248 /* SEQ_B6_PARAM_8_R01 */
249 mipi_dsi_dcs_write_seq(dsi, 0xb6, 0x10);
252 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x28);
254 usleep_range(5000, 6000);
256 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x04);
258 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
260 dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
265 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
267 ret = s6d7aa0_lock(ctx, true);
269 dev_err(dev, "Failed to lock registers: %d\n", ret);
273 ret = mipi_dsi_dcs_set_display_on(dsi);
275 dev_err(dev, "Failed to set display on: %d\n", ret);
282 static int s6d7aa0_lsl080al02_off(struct s6d7aa0 *ctx)
284 struct mipi_dsi_device *dsi = ctx->dsi;
287 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x20);
292 static const struct drm_display_mode s6d7aa0_lsl080al02_mode = {
293 .clock = (800 + 16 + 4 + 140) * (1280 + 8 + 4 + 4) * 60 / 1000,
295 .hsync_start = 800 + 16,
296 .hsync_end = 800 + 16 + 4,
297 .htotal = 800 + 16 + 4 + 140,
299 .vsync_start = 1280 + 8,
300 .vsync_end = 1280 + 8 + 4,
301 .vtotal = 1280 + 8 + 4 + 4,
306 static const struct s6d7aa0_panel_desc s6d7aa0_lsl080al02_desc = {
307 .panel_type = S6D7AA0_PANEL_LSL080AL02,
308 .init_func = s6d7aa0_lsl080al02_init,
309 .off_func = s6d7aa0_lsl080al02_off,
310 .drm_mode = &s6d7aa0_lsl080al02_mode,
311 .mode_flags = MIPI_DSI_MODE_VSYNC_FLUSH | MIPI_DSI_MODE_VIDEO_NO_HFP,
314 .has_backlight = false,
315 .use_passwd3 = false,
318 /* Initialization code and structures for LSL080AL03 panel */
320 static int s6d7aa0_lsl080al03_init(struct s6d7aa0 *ctx)
322 struct mipi_dsi_device *dsi = ctx->dsi;
323 struct device *dev = &dsi->dev;
326 usleep_range(20000, 25000);
328 ret = s6d7aa0_lock(ctx, false);
330 dev_err(dev, "Failed to unlock registers: %d\n", ret);
334 if (ctx->desc->panel_type == S6D7AA0_PANEL_LSL080AL03) {
335 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0xc7, 0x00, 0x29);
336 mipi_dsi_dcs_write_seq(dsi, 0xbc, 0x01, 0x4e, 0xa0);
337 mipi_dsi_dcs_write_seq(dsi, 0xfd, 0x16, 0x10, 0x11, 0x23,
339 mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x00, 0x02, 0x03, 0x21,
341 } else if (ctx->desc->panel_type == S6D7AA0_PANEL_LTL101AT01) {
342 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x08);
343 mipi_dsi_dcs_write_seq(dsi, 0xbc, 0x01, 0x4e, 0x0b);
344 mipi_dsi_dcs_write_seq(dsi, 0xfd, 0x16, 0x10, 0x11, 0x23,
346 mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x00, 0x02, 0x03, 0x21,
350 mipi_dsi_dcs_write_seq(dsi, 0xb3, 0x51);
351 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24);
352 mipi_dsi_dcs_write_seq(dsi, 0xf2, 0x02, 0x08, 0x08);
354 usleep_range(10000, 11000);
356 mipi_dsi_dcs_write_seq(dsi, 0xc0, 0x80, 0x80, 0x30);
357 mipi_dsi_dcs_write_seq(dsi, 0xcd,
358 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
359 0x2e, 0x2e, 0x2e, 0x2e, 0x2e);
360 mipi_dsi_dcs_write_seq(dsi, 0xce,
361 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
362 0x00, 0x00, 0x00, 0x00, 0x00);
363 mipi_dsi_dcs_write_seq(dsi, 0xc1, 0x03);
365 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
367 dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
371 ret = s6d7aa0_lock(ctx, true);
373 dev_err(dev, "Failed to lock registers: %d\n", ret);
377 ret = mipi_dsi_dcs_set_display_on(dsi);
379 dev_err(dev, "Failed to set display on: %d\n", ret);
386 static int s6d7aa0_lsl080al03_off(struct s6d7aa0 *ctx)
388 struct mipi_dsi_device *dsi = ctx->dsi;
390 mipi_dsi_dcs_write_seq(dsi, 0x22, 0x00);
395 static const struct drm_display_mode s6d7aa0_lsl080al03_mode = {
396 .clock = (768 + 18 + 16 + 126) * (1024 + 8 + 2 + 6) * 60 / 1000,
398 .hsync_start = 768 + 18,
399 .hsync_end = 768 + 18 + 16,
400 .htotal = 768 + 18 + 16 + 126,
402 .vsync_start = 1024 + 8,
403 .vsync_end = 1024 + 8 + 2,
404 .vtotal = 1024 + 8 + 2 + 6,
409 static const struct s6d7aa0_panel_desc s6d7aa0_lsl080al03_desc = {
410 .panel_type = S6D7AA0_PANEL_LSL080AL03,
411 .init_func = s6d7aa0_lsl080al03_init,
412 .off_func = s6d7aa0_lsl080al03_off,
413 .drm_mode = &s6d7aa0_lsl080al03_mode,
414 .mode_flags = MIPI_DSI_MODE_NO_EOT_PACKET,
417 .has_backlight = true,
421 /* Initialization structures for LTL101AT01 panel */
423 static const struct drm_display_mode s6d7aa0_ltl101at01_mode = {
424 .clock = (768 + 96 + 16 + 184) * (1024 + 8 + 2 + 6) * 60 / 1000,
426 .hsync_start = 768 + 96,
427 .hsync_end = 768 + 96 + 16,
428 .htotal = 768 + 96 + 16 + 184,
430 .vsync_start = 1024 + 8,
431 .vsync_end = 1024 + 8 + 2,
432 .vtotal = 1024 + 8 + 2 + 6,
437 static const struct s6d7aa0_panel_desc s6d7aa0_ltl101at01_desc = {
438 .panel_type = S6D7AA0_PANEL_LTL101AT01,
439 .init_func = s6d7aa0_lsl080al03_init, /* Similar init to LSL080AL03 */
440 .off_func = s6d7aa0_lsl080al03_off,
441 .drm_mode = &s6d7aa0_ltl101at01_mode,
442 .mode_flags = MIPI_DSI_MODE_NO_EOT_PACKET,
445 .has_backlight = true,
449 static int s6d7aa0_get_modes(struct drm_panel *panel,
450 struct drm_connector *connector)
452 struct drm_display_mode *mode;
455 ctx = container_of(panel, struct s6d7aa0, panel);
459 mode = drm_mode_duplicate(connector->dev, ctx->desc->drm_mode);
463 drm_mode_set_name(mode);
465 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
466 connector->display_info.width_mm = mode->width_mm;
467 connector->display_info.height_mm = mode->height_mm;
468 connector->display_info.bus_flags = ctx->desc->bus_flags;
469 drm_mode_probed_add(connector, mode);
474 static const struct drm_panel_funcs s6d7aa0_panel_funcs = {
475 .disable = s6d7aa0_disable,
476 .prepare = s6d7aa0_prepare,
477 .unprepare = s6d7aa0_unprepare,
478 .get_modes = s6d7aa0_get_modes,
481 static int s6d7aa0_probe(struct mipi_dsi_device *dsi)
483 struct device *dev = &dsi->dev;
487 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
491 ctx->desc = of_device_get_match_data(dev);
495 ctx->supplies[0].supply = "power";
496 ctx->supplies[1].supply = "vmipi";
497 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
500 return dev_err_probe(dev, ret, "Failed to get regulators\n");
502 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
503 if (IS_ERR(ctx->reset_gpio))
504 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
505 "Failed to get reset-gpios\n");
508 mipi_dsi_set_drvdata(dsi, ctx);
511 dsi->format = MIPI_DSI_FMT_RGB888;
512 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST
513 | ctx->desc->mode_flags;
515 drm_panel_init(&ctx->panel, dev, &s6d7aa0_panel_funcs,
516 DRM_MODE_CONNECTOR_DSI);
517 ctx->panel.prepare_prev_first = true;
519 ret = drm_panel_of_backlight(&ctx->panel);
521 return dev_err_probe(dev, ret, "Failed to get backlight\n");
523 /* Use DSI-based backlight as fallback if available */
524 if (ctx->desc->has_backlight && !ctx->panel.backlight) {
525 ctx->panel.backlight = s6d7aa0_create_backlight(dsi);
526 if (IS_ERR(ctx->panel.backlight))
527 return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
528 "Failed to create backlight\n");
531 drm_panel_add(&ctx->panel);
533 ret = mipi_dsi_attach(dsi);
535 dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
536 drm_panel_remove(&ctx->panel);
543 static void s6d7aa0_remove(struct mipi_dsi_device *dsi)
545 struct s6d7aa0 *ctx = mipi_dsi_get_drvdata(dsi);
548 ret = mipi_dsi_detach(dsi);
550 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
552 drm_panel_remove(&ctx->panel);
555 static const struct of_device_id s6d7aa0_of_match[] = {
557 .compatible = "samsung,lsl080al02",
558 .data = &s6d7aa0_lsl080al02_desc
561 .compatible = "samsung,lsl080al03",
562 .data = &s6d7aa0_lsl080al03_desc
565 .compatible = "samsung,ltl101at01",
566 .data = &s6d7aa0_ltl101at01_desc
570 MODULE_DEVICE_TABLE(of, s6d7aa0_of_match);
572 static struct mipi_dsi_driver s6d7aa0_driver = {
573 .probe = s6d7aa0_probe,
574 .remove = s6d7aa0_remove,
576 .name = "panel-samsung-s6d7aa0",
577 .of_match_table = s6d7aa0_of_match,
580 module_mipi_dsi_driver(s6d7aa0_driver);
582 MODULE_AUTHOR("Artur Weber <aweber.kernel@gmail.com>");
583 MODULE_DESCRIPTION("Samsung S6D7AA0 MIPI-DSI LCD controller driver");
584 MODULE_LICENSE("GPL");