1 // SPDX-License-Identifier: GPL-2.0
3 * Samsung S6D7AA0 MIPI-DSI TFT LCD controller drm_panel driver.
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>
14 #include <linux/of_device.h>
16 #include <video/mipi_display.h>
17 #include <drm/drm_mipi_dsi.h>
18 #include <drm/drm_modes.h>
19 #include <drm/drm_panel.h>
21 /* Manufacturer command set */
22 #define MCS_BL_CTL 0xc3
23 #define MCS_OTP_RELOAD 0xd0
24 #define MCS_PASSWD1 0xf0
25 #define MCS_PASSWD2 0xf1
26 #define MCS_PASSWD3 0xfc
29 struct drm_panel panel;
30 struct mipi_dsi_device *dsi;
31 struct gpio_desc *reset_gpio;
32 struct regulator_bulk_data supplies[2];
33 const struct s6d7aa0_panel_desc *desc;
36 struct s6d7aa0_panel_desc {
37 unsigned int panel_type;
38 int (*init_func)(struct s6d7aa0 *ctx);
39 int (*off_func)(struct s6d7aa0 *ctx);
40 const struct drm_display_mode *drm_mode;
41 unsigned long mode_flags;
48 S6D7AA0_PANEL_LSL080AL02,
49 S6D7AA0_PANEL_LSL080AL03,
50 S6D7AA0_PANEL_LTL101AT01,
53 static inline struct s6d7aa0 *panel_to_s6d7aa0(struct drm_panel *panel)
55 return container_of(panel, struct s6d7aa0, panel);
58 static void s6d7aa0_reset(struct s6d7aa0 *ctx)
60 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
62 gpiod_set_value_cansleep(ctx->reset_gpio, 0);
66 static int s6d7aa0_lock(struct s6d7aa0 *ctx, bool lock)
68 struct mipi_dsi_device *dsi = ctx->dsi;
72 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD1, 0xa5, 0xa5);
73 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD2, 0xa5, 0xa5);
74 if (ctx->desc->use_passwd3)
75 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD3, 0x5a, 0x5a);
77 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD1, 0x5a, 0x5a);
78 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD2, 0x5a, 0x5a);
79 if (ctx->desc->use_passwd3)
80 mipi_dsi_dcs_write_seq(dsi, MCS_PASSWD3, 0xa5, 0xa5);
86 static int s6d7aa0_on(struct s6d7aa0 *ctx)
88 struct mipi_dsi_device *dsi = ctx->dsi;
89 struct device *dev = &dsi->dev;
92 ret = ctx->desc->init_func(ctx);
94 dev_err(dev, "Failed to initialize panel: %d\n", ret);
95 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
99 ret = mipi_dsi_dcs_set_display_on(dsi);
101 dev_err(dev, "Failed to set display on: %d\n", ret);
108 static int s6d7aa0_off(struct s6d7aa0 *ctx)
110 struct mipi_dsi_device *dsi = ctx->dsi;
111 struct device *dev = &dsi->dev;
114 ret = ctx->desc->off_func(ctx);
116 dev_err(dev, "Panel-specific off function failed: %d\n", ret);
120 ret = mipi_dsi_dcs_set_display_off(dsi);
122 dev_err(dev, "Failed to set display off: %d\n", ret);
127 ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
129 dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
137 static int s6d7aa0_prepare(struct drm_panel *panel)
139 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel);
140 struct device *dev = &ctx->dsi->dev;
143 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
145 dev_err(dev, "Failed to enable regulators: %d\n", ret);
151 ret = s6d7aa0_on(ctx);
153 dev_err(dev, "Failed to initialize panel: %d\n", ret);
154 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
161 static int s6d7aa0_disable(struct drm_panel *panel)
163 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel);
164 struct device *dev = &ctx->dsi->dev;
167 ret = s6d7aa0_off(ctx);
169 dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
174 static int s6d7aa0_unprepare(struct drm_panel *panel)
176 struct s6d7aa0 *ctx = panel_to_s6d7aa0(panel);
178 gpiod_set_value_cansleep(ctx->reset_gpio, 1);
179 regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
184 /* Backlight control code */
186 static int s6d7aa0_bl_update_status(struct backlight_device *bl)
188 struct mipi_dsi_device *dsi = bl_get_data(bl);
189 u16 brightness = backlight_get_brightness(bl);
192 ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
199 static int s6d7aa0_bl_get_brightness(struct backlight_device *bl)
201 struct mipi_dsi_device *dsi = bl_get_data(bl);
205 ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
209 return brightness & 0xff;
212 static const struct backlight_ops s6d7aa0_bl_ops = {
213 .update_status = s6d7aa0_bl_update_status,
214 .get_brightness = s6d7aa0_bl_get_brightness,
217 static struct backlight_device *
218 s6d7aa0_create_backlight(struct mipi_dsi_device *dsi)
220 struct device *dev = &dsi->dev;
221 const struct backlight_properties props = {
222 .type = BACKLIGHT_RAW,
224 .max_brightness = 255,
227 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
228 &s6d7aa0_bl_ops, &props);
231 /* Initialization code and structures for LSL080AL02 panel */
233 static int s6d7aa0_lsl080al02_init(struct s6d7aa0 *ctx)
235 struct mipi_dsi_device *dsi = ctx->dsi;
236 struct device *dev = &dsi->dev;
239 usleep_range(20000, 25000);
241 ret = s6d7aa0_lock(ctx, false);
243 dev_err(dev, "Failed to unlock registers: %d\n", ret);
247 mipi_dsi_dcs_write_seq(dsi, MCS_OTP_RELOAD, 0x00, 0x10);
248 usleep_range(1000, 1500);
250 /* SEQ_B6_PARAM_8_R01 */
251 mipi_dsi_dcs_write_seq(dsi, 0xb6, 0x10);
254 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x28);
256 usleep_range(5000, 6000);
258 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x04);
260 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
262 dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
267 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_SET_ADDRESS_MODE, 0x00);
269 ret = s6d7aa0_lock(ctx, true);
271 dev_err(dev, "Failed to lock registers: %d\n", ret);
275 ret = mipi_dsi_dcs_set_display_on(dsi);
277 dev_err(dev, "Failed to set display on: %d\n", ret);
284 static int s6d7aa0_lsl080al02_off(struct s6d7aa0 *ctx)
286 struct mipi_dsi_device *dsi = ctx->dsi;
289 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x20);
294 static const struct drm_display_mode s6d7aa0_lsl080al02_mode = {
295 .clock = (800 + 16 + 4 + 140) * (1280 + 8 + 4 + 4) * 60 / 1000,
297 .hsync_start = 800 + 16,
298 .hsync_end = 800 + 16 + 4,
299 .htotal = 800 + 16 + 4 + 140,
301 .vsync_start = 1280 + 8,
302 .vsync_end = 1280 + 8 + 4,
303 .vtotal = 1280 + 8 + 4 + 4,
308 static const struct s6d7aa0_panel_desc s6d7aa0_lsl080al02_desc = {
309 .panel_type = S6D7AA0_PANEL_LSL080AL02,
310 .init_func = s6d7aa0_lsl080al02_init,
311 .off_func = s6d7aa0_lsl080al02_off,
312 .drm_mode = &s6d7aa0_lsl080al02_mode,
313 .mode_flags = MIPI_DSI_MODE_VSYNC_FLUSH | MIPI_DSI_MODE_VIDEO_NO_HFP,
314 .bus_flags = DRM_BUS_FLAG_DE_HIGH,
316 .has_backlight = false,
317 .use_passwd3 = false,
320 /* Initialization code and structures for LSL080AL03 panel */
322 static int s6d7aa0_lsl080al03_init(struct s6d7aa0 *ctx)
324 struct mipi_dsi_device *dsi = ctx->dsi;
325 struct device *dev = &dsi->dev;
328 usleep_range(20000, 25000);
330 ret = s6d7aa0_lock(ctx, false);
332 dev_err(dev, "Failed to unlock registers: %d\n", ret);
336 if (ctx->desc->panel_type == S6D7AA0_PANEL_LSL080AL03) {
337 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0xc7, 0x00, 0x29);
338 mipi_dsi_dcs_write_seq(dsi, 0xbc, 0x01, 0x4e, 0xa0);
339 mipi_dsi_dcs_write_seq(dsi, 0xfd, 0x16, 0x10, 0x11, 0x23,
341 mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x00, 0x02, 0x03, 0x21,
343 } else if (ctx->desc->panel_type == S6D7AA0_PANEL_LTL101AT01) {
344 mipi_dsi_dcs_write_seq(dsi, MCS_BL_CTL, 0x40, 0x00, 0x08);
345 mipi_dsi_dcs_write_seq(dsi, 0xbc, 0x01, 0x4e, 0x0b);
346 mipi_dsi_dcs_write_seq(dsi, 0xfd, 0x16, 0x10, 0x11, 0x23,
348 mipi_dsi_dcs_write_seq(dsi, 0xfe, 0x00, 0x02, 0x03, 0x21,
352 mipi_dsi_dcs_write_seq(dsi, 0xb3, 0x51);
353 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24);
354 mipi_dsi_dcs_write_seq(dsi, 0xf2, 0x02, 0x08, 0x08);
356 usleep_range(10000, 11000);
358 mipi_dsi_dcs_write_seq(dsi, 0xc0, 0x80, 0x80, 0x30);
359 mipi_dsi_dcs_write_seq(dsi, 0xcd,
360 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
361 0x2e, 0x2e, 0x2e, 0x2e, 0x2e);
362 mipi_dsi_dcs_write_seq(dsi, 0xce,
363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
364 0x00, 0x00, 0x00, 0x00, 0x00);
365 mipi_dsi_dcs_write_seq(dsi, 0xc1, 0x03);
367 ret = mipi_dsi_dcs_exit_sleep_mode(dsi);
369 dev_err(dev, "Failed to exit sleep mode: %d\n", ret);
373 ret = s6d7aa0_lock(ctx, true);
375 dev_err(dev, "Failed to lock registers: %d\n", ret);
379 ret = mipi_dsi_dcs_set_display_on(dsi);
381 dev_err(dev, "Failed to set display on: %d\n", ret);
388 static int s6d7aa0_lsl080al03_off(struct s6d7aa0 *ctx)
390 struct mipi_dsi_device *dsi = ctx->dsi;
392 mipi_dsi_dcs_write_seq(dsi, 0x22, 0x00);
397 static const struct drm_display_mode s6d7aa0_lsl080al03_mode = {
398 .clock = (768 + 18 + 16 + 126) * (1024 + 8 + 2 + 6) * 60 / 1000,
400 .hsync_start = 768 + 18,
401 .hsync_end = 768 + 18 + 16,
402 .htotal = 768 + 18 + 16 + 126,
404 .vsync_start = 1024 + 8,
405 .vsync_end = 1024 + 8 + 2,
406 .vtotal = 1024 + 8 + 2 + 6,
411 static const struct s6d7aa0_panel_desc s6d7aa0_lsl080al03_desc = {
412 .panel_type = S6D7AA0_PANEL_LSL080AL03,
413 .init_func = s6d7aa0_lsl080al03_init,
414 .off_func = s6d7aa0_lsl080al03_off,
415 .drm_mode = &s6d7aa0_lsl080al03_mode,
416 .mode_flags = MIPI_DSI_MODE_NO_EOT_PACKET,
419 .has_backlight = true,
423 /* Initialization structures for LTL101AT01 panel */
425 static const struct drm_display_mode s6d7aa0_ltl101at01_mode = {
426 .clock = (768 + 96 + 16 + 184) * (1024 + 8 + 2 + 6) * 60 / 1000,
428 .hsync_start = 768 + 96,
429 .hsync_end = 768 + 96 + 16,
430 .htotal = 768 + 96 + 16 + 184,
432 .vsync_start = 1024 + 8,
433 .vsync_end = 1024 + 8 + 2,
434 .vtotal = 1024 + 8 + 2 + 6,
439 static const struct s6d7aa0_panel_desc s6d7aa0_ltl101at01_desc = {
440 .panel_type = S6D7AA0_PANEL_LTL101AT01,
441 .init_func = s6d7aa0_lsl080al03_init, /* Similar init to LSL080AL03 */
442 .off_func = s6d7aa0_lsl080al03_off,
443 .drm_mode = &s6d7aa0_ltl101at01_mode,
444 .mode_flags = MIPI_DSI_MODE_NO_EOT_PACKET,
447 .has_backlight = true,
451 static int s6d7aa0_get_modes(struct drm_panel *panel,
452 struct drm_connector *connector)
454 struct drm_display_mode *mode;
457 ctx = container_of(panel, struct s6d7aa0, panel);
461 mode = drm_mode_duplicate(connector->dev, ctx->desc->drm_mode);
465 drm_mode_set_name(mode);
467 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
468 connector->display_info.width_mm = mode->width_mm;
469 connector->display_info.height_mm = mode->height_mm;
470 connector->display_info.bus_flags = ctx->desc->bus_flags;
471 drm_mode_probed_add(connector, mode);
476 static const struct drm_panel_funcs s6d7aa0_panel_funcs = {
477 .disable = s6d7aa0_disable,
478 .prepare = s6d7aa0_prepare,
479 .unprepare = s6d7aa0_unprepare,
480 .get_modes = s6d7aa0_get_modes,
483 static int s6d7aa0_probe(struct mipi_dsi_device *dsi)
485 struct device *dev = &dsi->dev;
489 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
493 ctx->desc = of_device_get_match_data(dev);
497 ctx->supplies[0].supply = "power";
498 ctx->supplies[1].supply = "vmipi";
499 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
502 return dev_err_probe(dev, ret, "Failed to get regulators\n");
504 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
505 if (IS_ERR(ctx->reset_gpio))
506 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio),
507 "Failed to get reset-gpios\n");
510 mipi_dsi_set_drvdata(dsi, ctx);
513 dsi->format = MIPI_DSI_FMT_RGB888;
514 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST
515 | ctx->desc->mode_flags;
517 drm_panel_init(&ctx->panel, dev, &s6d7aa0_panel_funcs,
518 DRM_MODE_CONNECTOR_DSI);
519 ctx->panel.prepare_prev_first = true;
521 ret = drm_panel_of_backlight(&ctx->panel);
523 return dev_err_probe(dev, ret, "Failed to get backlight\n");
525 /* Use DSI-based backlight as fallback if available */
526 if (ctx->desc->has_backlight && !ctx->panel.backlight) {
527 ctx->panel.backlight = s6d7aa0_create_backlight(dsi);
528 if (IS_ERR(ctx->panel.backlight))
529 return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight),
530 "Failed to create backlight\n");
533 drm_panel_add(&ctx->panel);
535 ret = mipi_dsi_attach(dsi);
537 dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
538 drm_panel_remove(&ctx->panel);
545 static void s6d7aa0_remove(struct mipi_dsi_device *dsi)
547 struct s6d7aa0 *ctx = mipi_dsi_get_drvdata(dsi);
550 ret = mipi_dsi_detach(dsi);
552 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret);
554 drm_panel_remove(&ctx->panel);
557 static const struct of_device_id s6d7aa0_of_match[] = {
559 .compatible = "samsung,lsl080al02",
560 .data = &s6d7aa0_lsl080al02_desc
563 .compatible = "samsung,lsl080al03",
564 .data = &s6d7aa0_lsl080al03_desc
567 .compatible = "samsung,ltl101at01",
568 .data = &s6d7aa0_ltl101at01_desc
573 static struct mipi_dsi_driver s6d7aa0_driver = {
574 .probe = s6d7aa0_probe,
575 .remove = s6d7aa0_remove,
577 .name = "panel-samsung-s6d7aa0",
578 .of_match_table = s6d7aa0_of_match,
581 module_mipi_dsi_driver(s6d7aa0_driver);
584 MODULE_DESCRIPTION("Samsung S6D7AA0 MIPI-DSI LCD controller driver");
585 MODULE_LICENSE("GPL");