1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
7 #include <drm/drm_panel.h>
8 #include <drm/drm_mipi_dsi.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/of_device.h>
12 #include <linux/of_graph.h>
13 #include <linux/pinctrl/consumer.h>
14 #include <linux/regulator/consumer.h>
16 #include <video/mipi_display.h>
18 static const char * const regulator_names
[] = {
24 static unsigned long const regulator_enable_loads
[] = {
30 static unsigned long const regulator_disable_loads
[] = {
41 struct nt35597_config
{
44 const char *panel_name
;
45 const struct cmd_set
*panel_on_cmds
;
47 const struct drm_display_mode
*dm
;
50 struct truly_nt35597
{
52 struct drm_panel panel
;
54 struct regulator_bulk_data supplies
[ARRAY_SIZE(regulator_names
)];
56 struct gpio_desc
*reset_gpio
;
57 struct gpio_desc
*mode_gpio
;
59 struct backlight_device
*backlight
;
61 struct mipi_dsi_device
*dsi
[2];
63 const struct nt35597_config
*config
;
68 static inline struct truly_nt35597
*panel_to_ctx(struct drm_panel
*panel
)
70 return container_of(panel
, struct truly_nt35597
, panel
);
73 static const struct cmd_set qcom_2k_panel_magic_cmds
[] = {
75 { { 0xff, 0x20 }, 2 },
76 { { 0xfb, 0x01 }, 2 },
77 { { 0x00, 0x01 }, 2 },
78 { { 0x01, 0x55 }, 2 },
79 { { 0x02, 0x45 }, 2 },
80 { { 0x05, 0x40 }, 2 },
81 { { 0x06, 0x19 }, 2 },
82 { { 0x07, 0x1e }, 2 },
83 { { 0x0b, 0x73 }, 2 },
84 { { 0x0c, 0x73 }, 2 },
85 { { 0x0e, 0xb0 }, 2 },
86 { { 0x0f, 0xae }, 2 },
87 { { 0x11, 0xb8 }, 2 },
88 { { 0x13, 0x00 }, 2 },
89 { { 0x58, 0x80 }, 2 },
90 { { 0x59, 0x01 }, 2 },
91 { { 0x5a, 0x00 }, 2 },
92 { { 0x5b, 0x01 }, 2 },
93 { { 0x5c, 0x80 }, 2 },
94 { { 0x5d, 0x81 }, 2 },
95 { { 0x5e, 0x00 }, 2 },
96 { { 0x5f, 0x01 }, 2 },
97 { { 0x72, 0x11 }, 2 },
98 { { 0x68, 0x03 }, 2 },
100 { { 0xFF, 0x24 }, 2 },
101 { { 0xFB, 0x01 }, 2 },
102 { { 0x00, 0x1C }, 2 },
103 { { 0x01, 0x0B }, 2 },
104 { { 0x02, 0x0C }, 2 },
105 { { 0x03, 0x01 }, 2 },
106 { { 0x04, 0x0F }, 2 },
107 { { 0x05, 0x10 }, 2 },
108 { { 0x06, 0x10 }, 2 },
109 { { 0x07, 0x10 }, 2 },
110 { { 0x08, 0x89 }, 2 },
111 { { 0x09, 0x8A }, 2 },
112 { { 0x0A, 0x13 }, 2 },
113 { { 0x0B, 0x13 }, 2 },
114 { { 0x0C, 0x15 }, 2 },
115 { { 0x0D, 0x15 }, 2 },
116 { { 0x0E, 0x17 }, 2 },
117 { { 0x0F, 0x17 }, 2 },
118 { { 0x10, 0x1C }, 2 },
119 { { 0x11, 0x0B }, 2 },
120 { { 0x12, 0x0C }, 2 },
121 { { 0x13, 0x01 }, 2 },
122 { { 0x14, 0x0F }, 2 },
123 { { 0x15, 0x10 }, 2 },
124 { { 0x16, 0x10 }, 2 },
125 { { 0x17, 0x10 }, 2 },
126 { { 0x18, 0x89 }, 2 },
127 { { 0x19, 0x8A }, 2 },
128 { { 0x1A, 0x13 }, 2 },
129 { { 0x1B, 0x13 }, 2 },
130 { { 0x1C, 0x15 }, 2 },
131 { { 0x1D, 0x15 }, 2 },
132 { { 0x1E, 0x17 }, 2 },
133 { { 0x1F, 0x17 }, 2 },
135 { { 0x20, 0x40 }, 2 },
136 { { 0x21, 0x01 }, 2 },
137 { { 0x22, 0x00 }, 2 },
138 { { 0x23, 0x40 }, 2 },
139 { { 0x24, 0x40 }, 2 },
140 { { 0x25, 0x6D }, 2 },
141 { { 0x26, 0x40 }, 2 },
142 { { 0x27, 0x40 }, 2 },
144 { { 0xE0, 0x00 }, 2 },
145 { { 0xDC, 0x21 }, 2 },
146 { { 0xDD, 0x22 }, 2 },
147 { { 0xDE, 0x07 }, 2 },
148 { { 0xDF, 0x07 }, 2 },
149 { { 0xE3, 0x6D }, 2 },
150 { { 0xE1, 0x07 }, 2 },
151 { { 0xE2, 0x07 }, 2 },
153 { { 0x29, 0xD8 }, 2 },
154 { { 0x2A, 0x2A }, 2 },
156 { { 0x4B, 0x03 }, 2 },
157 { { 0x4C, 0x11 }, 2 },
158 { { 0x4D, 0x10 }, 2 },
159 { { 0x4E, 0x01 }, 2 },
160 { { 0x4F, 0x01 }, 2 },
161 { { 0x50, 0x10 }, 2 },
162 { { 0x51, 0x00 }, 2 },
163 { { 0x52, 0x80 }, 2 },
164 { { 0x53, 0x00 }, 2 },
165 { { 0x56, 0x00 }, 2 },
166 { { 0x54, 0x07 }, 2 },
167 { { 0x58, 0x07 }, 2 },
168 { { 0x55, 0x25 }, 2 },
170 { { 0x5B, 0x43 }, 2 },
171 { { 0x5C, 0x00 }, 2 },
172 { { 0x5F, 0x73 }, 2 },
173 { { 0x60, 0x73 }, 2 },
174 { { 0x63, 0x22 }, 2 },
175 { { 0x64, 0x00 }, 2 },
176 { { 0x67, 0x08 }, 2 },
177 { { 0x68, 0x04 }, 2 },
178 /* Resolution:1440x2560 */
179 { { 0x72, 0x02 }, 2 },
181 { { 0x7A, 0x80 }, 2 },
182 { { 0x7B, 0x91 }, 2 },
183 { { 0x7C, 0xD8 }, 2 },
184 { { 0x7D, 0x60 }, 2 },
185 { { 0x7F, 0x15 }, 2 },
186 { { 0x75, 0x15 }, 2 },
188 { { 0xB3, 0xC0 }, 2 },
189 { { 0xB4, 0x00 }, 2 },
190 { { 0xB5, 0x00 }, 2 },
192 { { 0x78, 0x00 }, 2 },
193 { { 0x79, 0x00 }, 2 },
194 { { 0x80, 0x00 }, 2 },
195 { { 0x83, 0x00 }, 2 },
197 { { 0x93, 0x0A }, 2 },
198 { { 0x94, 0x0A }, 2 },
200 { { 0x8A, 0x00 }, 2 },
201 { { 0x9B, 0xFF }, 2 },
202 /* IMGSWAP =1 @PortSwap=1 */
203 { { 0x9D, 0xB0 }, 2 },
204 { { 0x9F, 0x63 }, 2 },
205 { { 0x98, 0x10 }, 2 },
207 { { 0xEC, 0x00 }, 2 },
209 { { 0xFF, 0x10 }, 2 },
210 /* VBP+VSA=,VFP = 10H */
211 { { 0x3B, 0x03, 0x0A, 0x0A }, 4 },
213 { { 0x35, 0x00 }, 2 },
214 /* EN_BK =1(auto black) */
215 { { 0xE5, 0x01 }, 2 },
216 /* CMD mode(10) VDO mode(03) */
217 { { 0xBB, 0x03 }, 2 },
219 { { 0xFB, 0x01 }, 2 },
222 static int truly_dcs_write(struct drm_panel
*panel
, u32 command
)
224 struct truly_nt35597
*ctx
= panel_to_ctx(panel
);
227 for (i
= 0; i
< ARRAY_SIZE(ctx
->dsi
); i
++) {
228 ret
= mipi_dsi_dcs_write(ctx
->dsi
[i
], command
, NULL
, 0);
230 DRM_DEV_ERROR(ctx
->dev
,
231 "cmd 0x%x failed for dsi = %d\n",
239 static int truly_dcs_write_buf(struct drm_panel
*panel
,
240 u32 size
, const u8
*buf
)
242 struct truly_nt35597
*ctx
= panel_to_ctx(panel
);
246 for (i
= 0; i
< ARRAY_SIZE(ctx
->dsi
); i
++) {
247 ret
= mipi_dsi_dcs_write_buffer(ctx
->dsi
[i
], buf
, size
);
249 DRM_DEV_ERROR(ctx
->dev
,
250 "failed to tx cmd [%d], err: %d\n", i
, ret
);
258 static int truly_35597_power_on(struct truly_nt35597
*ctx
)
262 for (i
= 0; i
< ARRAY_SIZE(ctx
->supplies
); i
++) {
263 ret
= regulator_set_load(ctx
->supplies
[i
].consumer
,
264 regulator_enable_loads
[i
]);
269 ret
= regulator_bulk_enable(ARRAY_SIZE(ctx
->supplies
), ctx
->supplies
);
274 * Reset sequence of truly panel requires the panel to be
275 * out of reset for 10ms, followed by being held in reset
276 * for 10ms and then out again
278 gpiod_set_value(ctx
->reset_gpio
, 0);
279 usleep_range(10000, 20000);
280 gpiod_set_value(ctx
->reset_gpio
, 1);
281 usleep_range(10000, 20000);
282 gpiod_set_value(ctx
->reset_gpio
, 0);
287 static int truly_nt35597_power_off(struct truly_nt35597
*ctx
)
292 gpiod_set_value(ctx
->reset_gpio
, 1);
294 for (i
= 0; i
< ARRAY_SIZE(ctx
->supplies
); i
++) {
295 ret
= regulator_set_load(ctx
->supplies
[i
].consumer
,
296 regulator_disable_loads
[i
]);
298 DRM_DEV_ERROR(ctx
->dev
,
299 "regulator_set_load failed %d\n", ret
);
304 ret
= regulator_bulk_disable(ARRAY_SIZE(ctx
->supplies
), ctx
->supplies
);
306 DRM_DEV_ERROR(ctx
->dev
,
307 "regulator_bulk_disable failed %d\n", ret
);
312 static int truly_nt35597_disable(struct drm_panel
*panel
)
314 struct truly_nt35597
*ctx
= panel_to_ctx(panel
);
320 if (ctx
->backlight
) {
321 ret
= backlight_disable(ctx
->backlight
);
323 DRM_DEV_ERROR(ctx
->dev
, "backlight disable failed %d\n",
327 ctx
->enabled
= false;
331 static int truly_nt35597_unprepare(struct drm_panel
*panel
)
333 struct truly_nt35597
*ctx
= panel_to_ctx(panel
);
339 ctx
->dsi
[0]->mode_flags
= 0;
340 ctx
->dsi
[1]->mode_flags
= 0;
342 ret
= truly_dcs_write(panel
, MIPI_DCS_SET_DISPLAY_OFF
);
344 DRM_DEV_ERROR(ctx
->dev
,
345 "set_display_off cmd failed ret = %d\n",
349 /* 120ms delay required here as per DCS spec */
352 ret
= truly_dcs_write(panel
, MIPI_DCS_ENTER_SLEEP_MODE
);
354 DRM_DEV_ERROR(ctx
->dev
,
355 "enter_sleep cmd failed ret = %d\n", ret
);
358 ret
= truly_nt35597_power_off(ctx
);
360 DRM_DEV_ERROR(ctx
->dev
, "power_off failed ret = %d\n", ret
);
362 ctx
->prepared
= false;
366 static int truly_nt35597_prepare(struct drm_panel
*panel
)
368 struct truly_nt35597
*ctx
= panel_to_ctx(panel
);
371 const struct cmd_set
*panel_on_cmds
;
372 const struct nt35597_config
*config
;
378 ret
= truly_35597_power_on(ctx
);
382 ctx
->dsi
[0]->mode_flags
|= MIPI_DSI_MODE_LPM
;
383 ctx
->dsi
[1]->mode_flags
|= MIPI_DSI_MODE_LPM
;
385 config
= ctx
->config
;
386 panel_on_cmds
= config
->panel_on_cmds
;
387 num_cmds
= config
->num_on_cmds
;
389 for (i
= 0; i
< num_cmds
; i
++) {
390 ret
= truly_dcs_write_buf(panel
,
391 panel_on_cmds
[i
].size
,
392 panel_on_cmds
[i
].commands
);
394 DRM_DEV_ERROR(ctx
->dev
,
395 "cmd set tx failed i = %d ret = %d\n",
401 ret
= truly_dcs_write(panel
, MIPI_DCS_EXIT_SLEEP_MODE
);
403 DRM_DEV_ERROR(ctx
->dev
,
404 "exit_sleep_mode cmd failed ret = %d\n",
409 /* Per DSI spec wait 120ms after sending exit sleep DCS command */
412 ret
= truly_dcs_write(panel
, MIPI_DCS_SET_DISPLAY_ON
);
414 DRM_DEV_ERROR(ctx
->dev
,
415 "set_display_on cmd failed ret = %d\n", ret
);
419 /* Per DSI spec wait 120ms after sending set_display_on DCS command */
422 ctx
->prepared
= true;
427 if (truly_nt35597_power_off(ctx
))
428 DRM_DEV_ERROR(ctx
->dev
, "power_off failed\n");
432 static int truly_nt35597_enable(struct drm_panel
*panel
)
434 struct truly_nt35597
*ctx
= panel_to_ctx(panel
);
440 if (ctx
->backlight
) {
441 ret
= backlight_enable(ctx
->backlight
);
443 DRM_DEV_ERROR(ctx
->dev
, "backlight enable failed %d\n",
452 static int truly_nt35597_get_modes(struct drm_panel
*panel
)
454 struct drm_connector
*connector
= panel
->connector
;
455 struct truly_nt35597
*ctx
= panel_to_ctx(panel
);
456 struct drm_display_mode
*mode
;
457 const struct nt35597_config
*config
;
459 config
= ctx
->config
;
460 mode
= drm_mode_create(connector
->dev
);
462 DRM_DEV_ERROR(ctx
->dev
,
463 "failed to create a new display mode\n");
467 connector
->display_info
.width_mm
= config
->width_mm
;
468 connector
->display_info
.height_mm
= config
->height_mm
;
469 drm_mode_copy(mode
, config
->dm
);
470 mode
->type
= DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
;
471 drm_mode_probed_add(connector
, mode
);
476 static const struct drm_panel_funcs truly_nt35597_drm_funcs
= {
477 .disable
= truly_nt35597_disable
,
478 .unprepare
= truly_nt35597_unprepare
,
479 .prepare
= truly_nt35597_prepare
,
480 .enable
= truly_nt35597_enable
,
481 .get_modes
= truly_nt35597_get_modes
,
484 static int truly_nt35597_panel_add(struct truly_nt35597
*ctx
)
486 struct device
*dev
= ctx
->dev
;
488 const struct nt35597_config
*config
;
490 config
= ctx
->config
;
491 for (i
= 0; i
< ARRAY_SIZE(ctx
->supplies
); i
++)
492 ctx
->supplies
[i
].supply
= regulator_names
[i
];
494 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(ctx
->supplies
),
499 ctx
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
500 if (IS_ERR(ctx
->reset_gpio
)) {
501 DRM_DEV_ERROR(dev
, "cannot get reset gpio %ld\n",
502 PTR_ERR(ctx
->reset_gpio
));
503 return PTR_ERR(ctx
->reset_gpio
);
506 ctx
->mode_gpio
= devm_gpiod_get(dev
, "mode", GPIOD_OUT_LOW
);
507 if (IS_ERR(ctx
->mode_gpio
)) {
508 DRM_DEV_ERROR(dev
, "cannot get mode gpio %ld\n",
509 PTR_ERR(ctx
->mode_gpio
));
510 return PTR_ERR(ctx
->mode_gpio
);
514 gpiod_set_value(ctx
->mode_gpio
, 0);
516 drm_panel_init(&ctx
->panel
);
517 ctx
->panel
.dev
= dev
;
518 ctx
->panel
.funcs
= &truly_nt35597_drm_funcs
;
519 drm_panel_add(&ctx
->panel
);
524 static const struct drm_display_mode qcom_sdm845_mtp_2k_mode
= {
528 .hsync_start
= 1440 + 200,
529 .hsync_end
= 1440 + 200 + 32,
530 .htotal
= 1440 + 200 + 32 + 64,
532 .vsync_start
= 2560 + 8,
533 .vsync_end
= 2560 + 8 + 1,
534 .vtotal
= 2560 + 8 + 1 + 7,
539 static const struct nt35597_config nt35597_dir
= {
542 .panel_name
= "qcom_sdm845_mtp_2k_panel",
543 .dm
= &qcom_sdm845_mtp_2k_mode
,
544 .panel_on_cmds
= qcom_2k_panel_magic_cmds
,
545 .num_on_cmds
= ARRAY_SIZE(qcom_2k_panel_magic_cmds
),
548 static int truly_nt35597_probe(struct mipi_dsi_device
*dsi
)
550 struct device
*dev
= &dsi
->dev
;
551 struct truly_nt35597
*ctx
;
552 struct mipi_dsi_device
*dsi1_device
;
553 struct device_node
*dsi1
;
554 struct mipi_dsi_host
*dsi1_host
;
555 struct mipi_dsi_device
*dsi_dev
;
559 const struct mipi_dsi_device_info info
= {
560 .type
= "trulynt35597",
565 ctx
= devm_kzalloc(dev
, sizeof(*ctx
), GFP_KERNEL
);
571 * This device represents itself as one with two input ports which are
572 * fed by the output ports of the two DSI controllers . The DSI0 is
573 * the master controller and has most of the panel related info in its
577 ctx
->config
= of_device_get_match_data(dev
);
580 dev_err(dev
, "missing device configuration\n");
584 dsi1
= of_graph_get_remote_node(dsi
->dev
.of_node
, 1, -1);
587 "failed to get remote node for dsi1_device\n");
591 dsi1_host
= of_find_mipi_dsi_host_by_node(dsi1
);
594 DRM_DEV_ERROR(dev
, "failed to find dsi host\n");
595 return -EPROBE_DEFER
;
598 /* register the second DSI device */
599 dsi1_device
= mipi_dsi_device_register_full(dsi1_host
, &info
);
600 if (IS_ERR(dsi1_device
)) {
601 DRM_DEV_ERROR(dev
, "failed to create dsi device\n");
602 return PTR_ERR(dsi1_device
);
605 mipi_dsi_set_drvdata(dsi
, ctx
);
609 ctx
->dsi
[1] = dsi1_device
;
611 ret
= truly_nt35597_panel_add(ctx
);
613 DRM_DEV_ERROR(dev
, "failed to add panel\n");
617 for (i
= 0; i
< ARRAY_SIZE(ctx
->dsi
); i
++) {
618 dsi_dev
= ctx
->dsi
[i
];
620 dsi_dev
->format
= MIPI_DSI_FMT_RGB888
;
621 dsi_dev
->mode_flags
= MIPI_DSI_MODE_VIDEO
| MIPI_DSI_MODE_LPM
|
622 MIPI_DSI_CLOCK_NON_CONTINUOUS
;
623 ret
= mipi_dsi_attach(dsi_dev
);
626 "dsi attach failed i = %d\n", i
);
634 drm_panel_remove(&ctx
->panel
);
636 mipi_dsi_device_unregister(dsi1_device
);
640 static int truly_nt35597_remove(struct mipi_dsi_device
*dsi
)
642 struct truly_nt35597
*ctx
= mipi_dsi_get_drvdata(dsi
);
645 mipi_dsi_detach(ctx
->dsi
[0]);
647 mipi_dsi_detach(ctx
->dsi
[1]);
648 mipi_dsi_device_unregister(ctx
->dsi
[1]);
651 drm_panel_remove(&ctx
->panel
);
655 static const struct of_device_id truly_nt35597_of_match
[] = {
657 .compatible
= "truly,nt35597-2K-display",
658 .data
= &nt35597_dir
,
662 MODULE_DEVICE_TABLE(of
, truly_nt35597_of_match
);
664 static struct mipi_dsi_driver truly_nt35597_driver
= {
666 .name
= "panel-truly-nt35597",
667 .of_match_table
= truly_nt35597_of_match
,
669 .probe
= truly_nt35597_probe
,
670 .remove
= truly_nt35597_remove
,
672 module_mipi_dsi_driver(truly_nt35597_driver
);
674 MODULE_DESCRIPTION("Truly NT35597 DSI Panel Driver");
675 MODULE_LICENSE("GPL v2");