1
2
3
4
5
6#include <drm/drmP.h>
7#include <drm/drm_panel.h>
8#include <drm/drm_mipi_dsi.h>
9
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>
15
16#include <video/mipi_display.h>
17
18static const char * const regulator_names[] = {
19 "vdda",
20 "vdispp",
21 "vdispn",
22};
23
24static unsigned long const regulator_enable_loads[] = {
25 62000,
26 100000,
27 100000,
28};
29
30static unsigned long const regulator_disable_loads[] = {
31 80,
32 100,
33 100,
34};
35
36struct cmd_set {
37 u8 commands[4];
38 u8 size;
39};
40
41struct nt35597_config {
42 u32 width_mm;
43 u32 height_mm;
44 const char *panel_name;
45 const struct cmd_set *panel_on_cmds;
46 u32 num_on_cmds;
47 const struct drm_display_mode *dm;
48};
49
50struct truly_nt35597 {
51 struct device *dev;
52 struct drm_panel panel;
53
54 struct regulator_bulk_data supplies[ARRAY_SIZE(regulator_names)];
55
56 struct gpio_desc *reset_gpio;
57 struct gpio_desc *mode_gpio;
58
59 struct backlight_device *backlight;
60
61 struct mipi_dsi_device *dsi[2];
62
63 const struct nt35597_config *config;
64 bool prepared;
65 bool enabled;
66};
67
68static inline struct truly_nt35597 *panel_to_ctx(struct drm_panel *panel)
69{
70 return container_of(panel, struct truly_nt35597, panel);
71}
72
73static const struct cmd_set qcom_2k_panel_magic_cmds[] = {
74
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 },
99
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 },
134
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 },
143
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 },
152
153 { { 0x29, 0xD8 }, 2 },
154 { { 0x2A, 0x2A }, 2 },
155
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 },
169
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
179 { { 0x72, 0x02 }, 2 },
180
181 { { 0x7A, 0x80 }, 2 },
182 { { 0x7B, 0x91 }, 2 },
183 { { 0x7C, 0xD8 }, 2 },
184 { { 0x7D, 0x60 }, 2 },
185 { { 0x7F, 0x15 }, 2 },
186 { { 0x75, 0x15 }, 2 },
187
188 { { 0xB3, 0xC0 }, 2 },
189 { { 0xB4, 0x00 }, 2 },
190 { { 0xB5, 0x00 }, 2 },
191
192 { { 0x78, 0x00 }, 2 },
193 { { 0x79, 0x00 }, 2 },
194 { { 0x80, 0x00 }, 2 },
195 { { 0x83, 0x00 }, 2 },
196
197 { { 0x93, 0x0A }, 2 },
198 { { 0x94, 0x0A }, 2 },
199
200 { { 0x8A, 0x00 }, 2 },
201 { { 0x9B, 0xFF }, 2 },
202
203 { { 0x9D, 0xB0 }, 2 },
204 { { 0x9F, 0x63 }, 2 },
205 { { 0x98, 0x10 }, 2 },
206
207 { { 0xEC, 0x00 }, 2 },
208
209 { { 0xFF, 0x10 }, 2 },
210
211 { { 0x3B, 0x03, 0x0A, 0x0A }, 4 },
212
213 { { 0x35, 0x00 }, 2 },
214
215 { { 0xE5, 0x01 }, 2 },
216
217 { { 0xBB, 0x03 }, 2 },
218
219 { { 0xFB, 0x01 }, 2 },
220};
221
222static int truly_dcs_write(struct drm_panel *panel, u32 command)
223{
224 struct truly_nt35597 *ctx = panel_to_ctx(panel);
225 int i, ret;
226
227 for (i = 0; i < ARRAY_SIZE(ctx->dsi); i++) {
228 ret = mipi_dsi_dcs_write(ctx->dsi[i], command, NULL, 0);
229 if (ret < 0) {
230 DRM_DEV_ERROR(ctx->dev,
231 "cmd 0x%x failed for dsi = %d\n",
232 command, i);
233 }
234 }
235
236 return ret;
237}
238
239static int truly_dcs_write_buf(struct drm_panel *panel,
240 u32 size, const u8 *buf)
241{
242 struct truly_nt35597 *ctx = panel_to_ctx(panel);
243 int ret = 0;
244 int i;
245
246 for (i = 0; i < ARRAY_SIZE(ctx->dsi); i++) {
247 ret = mipi_dsi_dcs_write_buffer(ctx->dsi[i], buf, size);
248 if (ret < 0) {
249 DRM_DEV_ERROR(ctx->dev,
250 "failed to tx cmd [%d], err: %d\n", i, ret);
251 return ret;
252 }
253 }
254
255 return ret;
256}
257
258static int truly_35597_power_on(struct truly_nt35597 *ctx)
259{
260 int ret, i;
261
262 for (i = 0; i < ARRAY_SIZE(ctx->supplies); i++) {
263 ret = regulator_set_load(ctx->supplies[i].consumer,
264 regulator_enable_loads[i]);
265 if (ret)
266 return ret;
267 }
268
269 ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
270 if (ret < 0)
271 return ret;
272
273
274
275
276
277
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);
283
284 return 0;
285}
286
287static int truly_nt35597_power_off(struct truly_nt35597 *ctx)
288{
289 int ret = 0;
290 int i;
291
292 gpiod_set_value(ctx->reset_gpio, 1);
293
294 for (i = 0; i < ARRAY_SIZE(ctx->supplies); i++) {
295 ret = regulator_set_load(ctx->supplies[i].consumer,
296 regulator_disable_loads[i]);
297 if (ret) {
298 DRM_DEV_ERROR(ctx->dev,
299 "regulator_set_load failed %d\n", ret);
300 return ret;
301 }
302 }
303
304 ret = regulator_bulk_disable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
305 if (ret) {
306 DRM_DEV_ERROR(ctx->dev,
307 "regulator_bulk_disable failed %d\n", ret);
308 }
309 return ret;
310}
311
312static int truly_nt35597_disable(struct drm_panel *panel)
313{
314 struct truly_nt35597 *ctx = panel_to_ctx(panel);
315 int ret;
316
317 if (!ctx->enabled)
318 return 0;
319
320 if (ctx->backlight) {
321 ret = backlight_disable(ctx->backlight);
322 if (ret < 0)
323 DRM_DEV_ERROR(ctx->dev, "backlight disable failed %d\n",
324 ret);
325 }
326
327 ctx->enabled = false;
328 return 0;
329}
330
331static int truly_nt35597_unprepare(struct drm_panel *panel)
332{
333 struct truly_nt35597 *ctx = panel_to_ctx(panel);
334 int ret = 0;
335
336 if (!ctx->prepared)
337 return 0;
338
339 ctx->dsi[0]->mode_flags = 0;
340 ctx->dsi[1]->mode_flags = 0;
341
342 ret = truly_dcs_write(panel, MIPI_DCS_SET_DISPLAY_OFF);
343 if (ret < 0) {
344 DRM_DEV_ERROR(ctx->dev,
345 "set_display_off cmd failed ret = %d\n",
346 ret);
347 }
348
349
350 msleep(120);
351
352 ret = truly_dcs_write(panel, MIPI_DCS_ENTER_SLEEP_MODE);
353 if (ret < 0) {
354 DRM_DEV_ERROR(ctx->dev,
355 "enter_sleep cmd failed ret = %d\n", ret);
356 }
357
358 ret = truly_nt35597_power_off(ctx);
359 if (ret < 0)
360 DRM_DEV_ERROR(ctx->dev, "power_off failed ret = %d\n", ret);
361
362 ctx->prepared = false;
363 return ret;
364}
365
366static int truly_nt35597_prepare(struct drm_panel *panel)
367{
368 struct truly_nt35597 *ctx = panel_to_ctx(panel);
369 int ret;
370 int i;
371 const struct cmd_set *panel_on_cmds;
372 const struct nt35597_config *config;
373 u32 num_cmds;
374
375 if (ctx->prepared)
376 return 0;
377
378 ret = truly_35597_power_on(ctx);
379 if (ret < 0)
380 return ret;
381
382 ctx->dsi[0]->mode_flags |= MIPI_DSI_MODE_LPM;
383 ctx->dsi[1]->mode_flags |= MIPI_DSI_MODE_LPM;
384
385 config = ctx->config;
386 panel_on_cmds = config->panel_on_cmds;
387 num_cmds = config->num_on_cmds;
388
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);
393 if (ret < 0) {
394 DRM_DEV_ERROR(ctx->dev,
395 "cmd set tx failed i = %d ret = %d\n",
396 i, ret);
397 goto power_off;
398 }
399 }
400
401 ret = truly_dcs_write(panel, MIPI_DCS_EXIT_SLEEP_MODE);
402 if (ret < 0) {
403 DRM_DEV_ERROR(ctx->dev,
404 "exit_sleep_mode cmd failed ret = %d\n",
405 ret);
406 goto power_off;
407 }
408
409
410 msleep(120);
411
412 ret = truly_dcs_write(panel, MIPI_DCS_SET_DISPLAY_ON);
413 if (ret < 0) {
414 DRM_DEV_ERROR(ctx->dev,
415 "set_display_on cmd failed ret = %d\n", ret);
416 goto power_off;
417 }
418
419
420 msleep(120);
421
422 ctx->prepared = true;
423
424 return 0;
425
426power_off:
427 if (truly_nt35597_power_off(ctx))
428 DRM_DEV_ERROR(ctx->dev, "power_off failed\n");
429 return ret;
430}
431
432static int truly_nt35597_enable(struct drm_panel *panel)
433{
434 struct truly_nt35597 *ctx = panel_to_ctx(panel);
435 int ret;
436
437 if (ctx->enabled)
438 return 0;
439
440 if (ctx->backlight) {
441 ret = backlight_enable(ctx->backlight);
442 if (ret < 0)
443 DRM_DEV_ERROR(ctx->dev, "backlight enable failed %d\n",
444 ret);
445 }
446
447 ctx->enabled = true;
448
449 return 0;
450}
451
452static int truly_nt35597_get_modes(struct drm_panel *panel)
453{
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;
458
459 config = ctx->config;
460 mode = drm_mode_create(connector->dev);
461 if (!mode) {
462 DRM_DEV_ERROR(ctx->dev,
463 "failed to create a new display mode\n");
464 return 0;
465 }
466
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);
472
473 return 1;
474}
475
476static 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,
482};
483
484static int truly_nt35597_panel_add(struct truly_nt35597 *ctx)
485{
486 struct device *dev = ctx->dev;
487 int ret, i;
488 const struct nt35597_config *config;
489
490 config = ctx->config;
491 for (i = 0; i < ARRAY_SIZE(ctx->supplies); i++)
492 ctx->supplies[i].supply = regulator_names[i];
493
494 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
495 ctx->supplies);
496 if (ret < 0)
497 return ret;
498
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);
504 }
505
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);
511 }
512
513
514 gpiod_set_value(ctx->mode_gpio, 0);
515
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);
520
521 return 0;
522}
523
524static const struct drm_display_mode qcom_sdm845_mtp_2k_mode = {
525 .name = "1440x2560",
526 .clock = 268316,
527 .hdisplay = 1440,
528 .hsync_start = 1440 + 200,
529 .hsync_end = 1440 + 200 + 32,
530 .htotal = 1440 + 200 + 32 + 64,
531 .vdisplay = 2560,
532 .vsync_start = 2560 + 8,
533 .vsync_end = 2560 + 8 + 1,
534 .vtotal = 2560 + 8 + 1 + 7,
535 .vrefresh = 60,
536 .flags = 0,
537};
538
539static const struct nt35597_config nt35597_dir = {
540 .width_mm = 74,
541 .height_mm = 131,
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),
546};
547
548static int truly_nt35597_probe(struct mipi_dsi_device *dsi)
549{
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;
556 int ret = 0;
557 int i;
558
559 const struct mipi_dsi_device_info info = {
560 .type = "trulynt35597",
561 .channel = 0,
562 .node = NULL,
563 };
564
565 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
566
567 if (!ctx)
568 return -ENOMEM;
569
570
571
572
573
574
575
576
577 ctx->config = of_device_get_match_data(dev);
578
579 if (!ctx->config) {
580 dev_err(dev, "missing device configuration\n");
581 return -ENODEV;
582 }
583
584 dsi1 = of_graph_get_remote_node(dsi->dev.of_node, 1, -1);
585 if (!dsi1) {
586 DRM_DEV_ERROR(dev,
587 "failed to get remote node for dsi1_device\n");
588 return -ENODEV;
589 }
590
591 dsi1_host = of_find_mipi_dsi_host_by_node(dsi1);
592 of_node_put(dsi1);
593 if (!dsi1_host) {
594 DRM_DEV_ERROR(dev, "failed to find dsi host\n");
595 return -EPROBE_DEFER;
596 }
597
598
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);
603 }
604
605 mipi_dsi_set_drvdata(dsi, ctx);
606
607 ctx->dev = dev;
608 ctx->dsi[0] = dsi;
609 ctx->dsi[1] = dsi1_device;
610
611 ret = truly_nt35597_panel_add(ctx);
612 if (ret) {
613 DRM_DEV_ERROR(dev, "failed to add panel\n");
614 goto err_panel_add;
615 }
616
617 for (i = 0; i < ARRAY_SIZE(ctx->dsi); i++) {
618 dsi_dev = ctx->dsi[i];
619 dsi_dev->lanes = 4;
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);
624 if (ret < 0) {
625 DRM_DEV_ERROR(dev,
626 "dsi attach failed i = %d\n", i);
627 goto err_dsi_attach;
628 }
629 }
630
631 return 0;
632
633err_dsi_attach:
634 drm_panel_remove(&ctx->panel);
635err_panel_add:
636 mipi_dsi_device_unregister(dsi1_device);
637 return ret;
638}
639
640static int truly_nt35597_remove(struct mipi_dsi_device *dsi)
641{
642 struct truly_nt35597 *ctx = mipi_dsi_get_drvdata(dsi);
643
644 if (ctx->dsi[0])
645 mipi_dsi_detach(ctx->dsi[0]);
646 if (ctx->dsi[1]) {
647 mipi_dsi_detach(ctx->dsi[1]);
648 mipi_dsi_device_unregister(ctx->dsi[1]);
649 }
650
651 drm_panel_remove(&ctx->panel);
652 return 0;
653}
654
655static const struct of_device_id truly_nt35597_of_match[] = {
656 {
657 .compatible = "truly,nt35597-2K-display",
658 .data = &nt35597_dir,
659 },
660 { }
661};
662MODULE_DEVICE_TABLE(of, truly_nt35597_of_match);
663
664static struct mipi_dsi_driver truly_nt35597_driver = {
665 .driver = {
666 .name = "panel-truly-nt35597",
667 .of_match_table = truly_nt35597_of_match,
668 },
669 .probe = truly_nt35597_probe,
670 .remove = truly_nt35597_remove,
671};
672module_mipi_dsi_driver(truly_nt35597_driver);
673
674MODULE_DESCRIPTION("Truly NT35597 DSI Panel Driver");
675MODULE_LICENSE("GPL v2");
676