Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 | // SPDX-License-Identifier: GPL-2.0 /* * R-Car MIPI DSI Encoder * * Copyright (C) 2020 Renesas Electronics Corporation */ #include <linux/clk.h> #include <linux/delay.h> #include <linux/io.h> #include <linux/iopoll.h> #include <linux/math64.h> #include <linux/module.h> #include <linux/of.h> #include <linux/of_device.h> #include <linux/of_graph.h> #include <linux/platform_device.h> #include <linux/reset.h> #include <linux/slab.h> #include <drm/drm_atomic.h> #include <drm/drm_atomic_helper.h> #include <drm/drm_bridge.h> #include <drm/drm_mipi_dsi.h> #include <drm/drm_of.h> #include <drm/drm_panel.h> #include <drm/drm_probe_helper.h> #include "rcar_mipi_dsi.h" #include "rcar_mipi_dsi_regs.h" #define MHZ(v) ((u32)((v) * 1000000U)) enum rcar_mipi_dsi_hw_model { RCAR_DSI_V3U, RCAR_DSI_V4H, }; struct rcar_mipi_dsi_device_info { enum rcar_mipi_dsi_hw_model model; const struct dsi_clk_config *clk_cfg; u8 clockset2_m_offset; u8 n_min; u8 n_max; u8 n_mul; unsigned long fpfd_min; unsigned long fpfd_max; u16 m_min; u16 m_max; unsigned long fout_min; unsigned long fout_max; }; struct rcar_mipi_dsi { struct device *dev; const struct rcar_mipi_dsi_device_info *info; struct reset_control *rstc; struct mipi_dsi_host host; struct drm_bridge bridge; struct drm_bridge *next_bridge; struct drm_connector connector; void __iomem *mmio; struct { struct clk *mod; struct clk *pll; struct clk *dsi; } clocks; enum mipi_dsi_pixel_format format; unsigned int num_data_lanes; unsigned int lanes; }; struct dsi_setup_info { unsigned long hsfreq; u16 hsfreqrange; unsigned long fout; u16 m; u16 n; u16 vclk_divider; const struct dsi_clk_config *clkset; }; static inline struct rcar_mipi_dsi * bridge_to_rcar_mipi_dsi(struct drm_bridge *bridge) { return container_of(bridge, struct rcar_mipi_dsi, bridge); } static inline struct rcar_mipi_dsi * host_to_rcar_mipi_dsi(struct mipi_dsi_host *host) { return container_of(host, struct rcar_mipi_dsi, host); } static const u32 hsfreqrange_table[][2] = { { MHZ(80), 0x00 }, { MHZ(90), 0x10 }, { MHZ(100), 0x20 }, { MHZ(110), 0x30 }, { MHZ(120), 0x01 }, { MHZ(130), 0x11 }, { MHZ(140), 0x21 }, { MHZ(150), 0x31 }, { MHZ(160), 0x02 }, { MHZ(170), 0x12 }, { MHZ(180), 0x22 }, { MHZ(190), 0x32 }, { MHZ(205), 0x03 }, { MHZ(220), 0x13 }, { MHZ(235), 0x23 }, { MHZ(250), 0x33 }, { MHZ(275), 0x04 }, { MHZ(300), 0x14 }, { MHZ(325), 0x25 }, { MHZ(350), 0x35 }, { MHZ(400), 0x05 }, { MHZ(450), 0x16 }, { MHZ(500), 0x26 }, { MHZ(550), 0x37 }, { MHZ(600), 0x07 }, { MHZ(650), 0x18 }, { MHZ(700), 0x28 }, { MHZ(750), 0x39 }, { MHZ(800), 0x09 }, { MHZ(850), 0x19 }, { MHZ(900), 0x29 }, { MHZ(950), 0x3a }, { MHZ(1000), 0x0a }, { MHZ(1050), 0x1a }, { MHZ(1100), 0x2a }, { MHZ(1150), 0x3b }, { MHZ(1200), 0x0b }, { MHZ(1250), 0x1b }, { MHZ(1300), 0x2b }, { MHZ(1350), 0x3c }, { MHZ(1400), 0x0c }, { MHZ(1450), 0x1c }, { MHZ(1500), 0x2c }, { MHZ(1550), 0x3d }, { MHZ(1600), 0x0d }, { MHZ(1650), 0x1d }, { MHZ(1700), 0x2e }, { MHZ(1750), 0x3e }, { MHZ(1800), 0x0e }, { MHZ(1850), 0x1e }, { MHZ(1900), 0x2f }, { MHZ(1950), 0x3f }, { MHZ(2000), 0x0f }, { MHZ(2050), 0x40 }, { MHZ(2100), 0x41 }, { MHZ(2150), 0x42 }, { MHZ(2200), 0x43 }, { MHZ(2250), 0x44 }, { MHZ(2300), 0x45 }, { MHZ(2350), 0x46 }, { MHZ(2400), 0x47 }, { MHZ(2450), 0x48 }, { MHZ(2500), 0x49 }, { /* sentinel */ }, }; struct dsi_clk_config { u32 min_freq; u32 max_freq; u8 vco_cntrl; u8 cpbias_cntrl; u8 gmp_cntrl; u8 int_cntrl; u8 prop_cntrl; }; static const struct dsi_clk_config dsi_clk_cfg_v3u[] = { { MHZ(40), MHZ(55), 0x3f, 0x10, 0x01, 0x00, 0x0b }, { MHZ(52.5), MHZ(80), 0x39, 0x10, 0x01, 0x00, 0x0b }, { MHZ(80), MHZ(110), 0x2f, 0x10, 0x01, 0x00, 0x0b }, { MHZ(105), MHZ(160), 0x29, 0x10, 0x01, 0x00, 0x0b }, { MHZ(160), MHZ(220), 0x1f, 0x10, 0x01, 0x00, 0x0b }, { MHZ(210), MHZ(320), 0x19, 0x10, 0x01, 0x00, 0x0b }, { MHZ(320), MHZ(440), 0x0f, 0x10, 0x01, 0x00, 0x0b }, { MHZ(420), MHZ(660), 0x09, 0x10, 0x01, 0x00, 0x0b }, { MHZ(630), MHZ(1149), 0x03, 0x10, 0x01, 0x00, 0x0b }, { MHZ(1100), MHZ(1152), 0x01, 0x10, 0x01, 0x00, 0x0b }, { MHZ(1150), MHZ(1250), 0x01, 0x10, 0x01, 0x00, 0x0c }, { /* sentinel */ }, }; static const struct dsi_clk_config dsi_clk_cfg_v4h[] = { { MHZ(40), MHZ(45.31), 0x2b, 0x00, 0x00, 0x08, 0x0a }, { MHZ(45.31), MHZ(54.66), 0x28, 0x00, 0x00, 0x08, 0x0a }, { MHZ(54.66), MHZ(62.5), 0x28, 0x00, 0x00, 0x08, 0x0a }, { MHZ(62.5), MHZ(75), 0x27, 0x00, 0x00, 0x08, 0x0a }, { MHZ(75), MHZ(90.63), 0x23, 0x00, 0x00, 0x08, 0x0a }, { MHZ(90.63), MHZ(109.37), 0x20, 0x00, 0x00, 0x08, 0x0a }, { MHZ(109.37), MHZ(125), 0x20, 0x00, 0x00, 0x08, 0x0a }, { MHZ(125), MHZ(150), 0x1f, 0x00, 0x00, 0x08, 0x0a }, { MHZ(150), MHZ(181.25), 0x1b, 0x00, 0x00, 0x08, 0x0a }, { MHZ(181.25), MHZ(218.75), 0x18, 0x00, 0x00, 0x08, 0x0a }, { MHZ(218.75), MHZ(250), 0x18, 0x00, 0x00, 0x08, 0x0a }, { MHZ(250), MHZ(300), 0x17, 0x00, 0x00, 0x08, 0x0a }, { MHZ(300), MHZ(362.5), 0x13, 0x00, 0x00, 0x08, 0x0a }, { MHZ(362.5), MHZ(455.48), 0x10, 0x00, 0x00, 0x08, 0x0a }, { MHZ(455.48), MHZ(500), 0x10, 0x00, 0x00, 0x08, 0x0a }, { MHZ(500), MHZ(600), 0x0f, 0x00, 0x00, 0x08, 0x0a }, { MHZ(600), MHZ(725), 0x0b, 0x00, 0x00, 0x08, 0x0a }, { MHZ(725), MHZ(875), 0x08, 0x00, 0x00, 0x08, 0x0a }, { MHZ(875), MHZ(1000), 0x08, 0x00, 0x00, 0x08, 0x0a }, { MHZ(1000), MHZ(1200), 0x07, 0x00, 0x00, 0x08, 0x0a }, { MHZ(1200), MHZ(1250), 0x03, 0x00, 0x00, 0x08, 0x0a }, { /* sentinel */ }, }; static void rcar_mipi_dsi_write(struct rcar_mipi_dsi *dsi, u32 reg, u32 data) { iowrite32(data, dsi->mmio + reg); } static u32 rcar_mipi_dsi_read(struct rcar_mipi_dsi *dsi, u32 reg) { return ioread32(dsi->mmio + reg); } static void rcar_mipi_dsi_clr(struct rcar_mipi_dsi *dsi, u32 reg, u32 clr) { rcar_mipi_dsi_write(dsi, reg, rcar_mipi_dsi_read(dsi, reg) & ~clr); } static void rcar_mipi_dsi_set(struct rcar_mipi_dsi *dsi, u32 reg, u32 set) { rcar_mipi_dsi_write(dsi, reg, rcar_mipi_dsi_read(dsi, reg) | set); } static int rcar_mipi_dsi_write_phtw(struct rcar_mipi_dsi *dsi, u32 phtw) { u32 status; int ret; rcar_mipi_dsi_write(dsi, PHTW, phtw); ret = read_poll_timeout(rcar_mipi_dsi_read, status, !(status & (PHTW_DWEN | PHTW_CWEN)), 2000, 10000, false, dsi, PHTW); if (ret < 0) { dev_err(dsi->dev, "PHY test interface write timeout (0x%08x)\n", phtw); return ret; } return ret; } static int rcar_mipi_dsi_write_phtw_arr(struct rcar_mipi_dsi *dsi, const u32 *phtw, unsigned int size) { for (unsigned int i = 0; i < size; i++) { int ret = rcar_mipi_dsi_write_phtw(dsi, phtw[i]); if (ret < 0) return ret; } return 0; } #define WRITE_PHTW(...) \ ({ \ static const u32 phtw[] = { __VA_ARGS__ }; \ int ret; \ ret = rcar_mipi_dsi_write_phtw_arr(dsi, phtw, \ ARRAY_SIZE(phtw)); \ ret; \ }) static int rcar_mipi_dsi_init_phtw_v3u(struct rcar_mipi_dsi *dsi) { return WRITE_PHTW(0x01020114, 0x01600115, 0x01030116, 0x0102011d, 0x011101a4, 0x018601a4, 0x014201a0, 0x010001a3, 0x0101011f); } static int rcar_mipi_dsi_post_init_phtw_v3u(struct rcar_mipi_dsi *dsi) { return WRITE_PHTW(0x010c0130, 0x010c0140, 0x010c0150, 0x010c0180, 0x010c0190, 0x010a0160, 0x010a0170, 0x01800164, 0x01800174); } static int rcar_mipi_dsi_init_phtw_v4h(struct rcar_mipi_dsi *dsi, const struct dsi_setup_info *setup_info) { int ret; if (setup_info->hsfreq < MHZ(450)) { ret = WRITE_PHTW(0x01010100, 0x011b01ac); if (ret) return ret; } ret = WRITE_PHTW(0x01010100, 0x01030173, 0x01000174, 0x01500175, 0x01030176, 0x01040166, 0x010201ad); if (ret) return ret; if (setup_info->hsfreq <= MHZ(1000)) ret = WRITE_PHTW(0x01020100, 0x01910170, 0x01020171, 0x01110172); else if (setup_info->hsfreq <= MHZ(1500)) ret = WRITE_PHTW(0x01020100, 0x01980170, 0x01030171, 0x01100172); else if (setup_info->hsfreq <= MHZ(2500)) ret = WRITE_PHTW(0x01020100, 0x0144016b, 0x01000172); else return -EINVAL; if (ret) return ret; if (dsi->lanes <= 1) { ret = WRITE_PHTW(0x01070100, 0x010e010b); if (ret) return ret; } if (dsi->lanes <= 2) { ret = WRITE_PHTW(0x01090100, 0x010e010b); if (ret) return ret; } if (dsi->lanes <= 3) { ret = WRITE_PHTW(0x010b0100, 0x010e010b); if (ret) return ret; } if (setup_info->hsfreq <= MHZ(1500)) { ret = WRITE_PHTW(0x01010100, 0x01c0016e); if (ret) return ret; } return 0; } static int rcar_mipi_dsi_post_init_phtw_v4h(struct rcar_mipi_dsi *dsi, const struct dsi_setup_info *setup_info) { u32 status; int ret; if (setup_info->hsfreq <= MHZ(1500)) { WRITE_PHTW(0x01020100, 0x00000180); ret = read_poll_timeout(rcar_mipi_dsi_read, status, status & PHTR_TEST, 2000, 10000, false, dsi, PHTR); if (ret < 0) { dev_err(dsi->dev, "failed to test PHTR\n"); return ret; } WRITE_PHTW(0x01010100, 0x0100016e); } return 0; } /* ----------------------------------------------------------------------------- * Hardware Setup */ static void rcar_mipi_dsi_pll_calc(struct rcar_mipi_dsi *dsi, unsigned long fin_rate, unsigned long fout_target, struct dsi_setup_info *setup_info) { unsigned int best_err = -1; const struct rcar_mipi_dsi_device_info *info = dsi->info; for (unsigned int n = info->n_min; n <= info->n_max; n++) { unsigned long fpfd; fpfd = fin_rate / n; if (fpfd < info->fpfd_min || fpfd > info->fpfd_max) continue; for (unsigned int m = info->m_min; m <= info->m_max; m++) { unsigned int err; u64 fout; fout = div64_u64((u64)fpfd * m, dsi->info->n_mul); if (fout < info->fout_min || fout > info->fout_max) continue; fout = div64_u64(fout, setup_info->vclk_divider); if (fout < setup_info->clkset->min_freq || fout > setup_info->clkset->max_freq) continue; err = abs((long)(fout - fout_target) * 10000 / (long)fout_target); if (err < best_err) { setup_info->m = m; setup_info->n = n; setup_info->fout = (unsigned long)fout; best_err = err; if (err == 0) return; } } } } static void rcar_mipi_dsi_parameters_calc(struct rcar_mipi_dsi *dsi, struct clk *clk, unsigned long target, struct dsi_setup_info *setup_info) { const struct dsi_clk_config *clk_cfg; unsigned long fout_target; unsigned long fin_rate; unsigned int i; unsigned int err; /* * Calculate Fout = dot clock * ColorDepth / (2 * Lane Count) * The range out Fout is [40 - 1250] Mhz */ fout_target = target * mipi_dsi_pixel_format_to_bpp(dsi->format) / (2 * dsi->lanes); if (fout_target < MHZ(40) || fout_target > MHZ(1250)) return; /* Find PLL settings */ for (clk_cfg = dsi->info->clk_cfg; clk_cfg->min_freq != 0; clk_cfg++) { if (fout_target > clk_cfg->min_freq && fout_target <= clk_cfg->max_freq) { setup_info->clkset = clk_cfg; break; } } fin_rate = clk_get_rate(clk); switch (dsi->info->model) { case RCAR_DSI_V3U: default: setup_info->vclk_divider = 1 << ((clk_cfg->vco_cntrl >> 4) & 0x3); break; case RCAR_DSI_V4H: setup_info->vclk_divider = 1 << (((clk_cfg->vco_cntrl >> 3) & 0x7) + 1); break; } rcar_mipi_dsi_pll_calc(dsi, fin_rate, fout_target, setup_info); /* Find hsfreqrange */ setup_info->hsfreq = setup_info->fout * 2; for (i = 0; i < ARRAY_SIZE(hsfreqrange_table); i++) { if (hsfreqrange_table[i][0] >= setup_info->hsfreq) { setup_info->hsfreqrange = hsfreqrange_table[i][1]; break; } } err = abs((long)(setup_info->fout - fout_target) * 10000 / (long)fout_target); dev_dbg(dsi->dev, "Fout = %u * %lu / (%u * %u * %u) = %lu (target %lu Hz, error %d.%02u%%)\n", setup_info->m, fin_rate, dsi->info->n_mul, setup_info->n, setup_info->vclk_divider, setup_info->fout, fout_target, err / 100, err % 100); dev_dbg(dsi->dev, "vco_cntrl = 0x%x\tprop_cntrl = 0x%x\thsfreqrange = 0x%x\n", clk_cfg->vco_cntrl, clk_cfg->prop_cntrl, setup_info->hsfreqrange); } static void rcar_mipi_dsi_set_display_timing(struct rcar_mipi_dsi *dsi, const struct drm_display_mode *mode) { u32 setr; u32 vprmset0r; u32 vprmset1r; u32 vprmset2r; u32 vprmset3r; u32 vprmset4r; /* Configuration for Pixel Stream and Packet Header */ if (mipi_dsi_pixel_format_to_bpp(dsi->format) == 24) rcar_mipi_dsi_write(dsi, TXVMPSPHSETR, TXVMPSPHSETR_DT_RGB24); else if (mipi_dsi_pixel_format_to_bpp(dsi->format) == 18) rcar_mipi_dsi_write(dsi, TXVMPSPHSETR, TXVMPSPHSETR_DT_RGB18); else if (mipi_dsi_pixel_format_to_bpp(dsi->format) == 16) rcar_mipi_dsi_write(dsi, TXVMPSPHSETR, TXVMPSPHSETR_DT_RGB16); else { dev_warn(dsi->dev, "unsupported format"); return; } /* Configuration for Blanking sequence and Input Pixel */ setr = TXVMSETR_HSABPEN_EN | TXVMSETR_HBPBPEN_EN | TXVMSETR_HFPBPEN_EN | TXVMSETR_SYNSEQ_PULSES | TXVMSETR_PIXWDTH | TXVMSETR_VSTPM; rcar_mipi_dsi_write(dsi, TXVMSETR, setr); /* Configuration for Video Parameters */ vprmset0r = (mode->flags & DRM_MODE_FLAG_PVSYNC ? TXVMVPRMSET0R_VSPOL_HIG : TXVMVPRMSET0R_VSPOL_LOW) | (mode->flags & DRM_MODE_FLAG_PHSYNC ? TXVMVPRMSET0R_HSPOL_HIG : TXVMVPRMSET0R_HSPOL_LOW) | TXVMVPRMSET0R_CSPC_RGB | TXVMVPRMSET0R_BPP_24; vprmset1r = TXVMVPRMSET1R_VACTIVE(mode->vdisplay) | TXVMVPRMSET1R_VSA(mode->vsync_end - mode->vsync_start); vprmset2r = TXVMVPRMSET2R_VFP(mode->vsync_start - mode->vdisplay) | TXVMVPRMSET2R_VBP(mode->vtotal - mode->vsync_end); vprmset3r = TXVMVPRMSET3R_HACTIVE(mode->hdisplay) | TXVMVPRMSET3R_HSA(mode->hsync_end - mode->hsync_start); vprmset4r = TXVMVPRMSET4R_HFP(mode->hsync_start - mode->hdisplay) | TXVMVPRMSET4R_HBP(mode->htotal - mode->hsync_end); rcar_mipi_dsi_write(dsi, TXVMVPRMSET0R, vprmset0r); rcar_mipi_dsi_write(dsi, TXVMVPRMSET1R, vprmset1r); rcar_mipi_dsi_write(dsi, TXVMVPRMSET2R, vprmset2r); rcar_mipi_dsi_write(dsi, TXVMVPRMSET3R, vprmset3r); rcar_mipi_dsi_write(dsi, TXVMVPRMSET4R, vprmset4r); } static int rcar_mipi_dsi_startup(struct rcar_mipi_dsi *dsi, const struct drm_display_mode *mode) { struct dsi_setup_info setup_info = {}; unsigned int timeout; int ret; int dsi_format; u32 phy_setup; u32 clockset2, clockset3; u32 ppisetr; u32 vclkset; /* Checking valid format */ dsi_format = mipi_dsi_pixel_format_to_bpp(dsi->format); if (dsi_format < 0) { dev_warn(dsi->dev, "invalid format"); return -EINVAL; } /* Parameters Calculation */ rcar_mipi_dsi_parameters_calc(dsi, dsi->clocks.pll, mode->clock * 1000, &setup_info); /* LPCLK enable */ rcar_mipi_dsi_set(dsi, LPCLKSET, LPCLKSET_CKEN); /* CFGCLK enabled */ rcar_mipi_dsi_set(dsi, CFGCLKSET, CFGCLKSET_CKEN); rcar_mipi_dsi_clr(dsi, PHYSETUP, PHYSETUP_RSTZ); rcar_mipi_dsi_clr(dsi, PHYSETUP, PHYSETUP_SHUTDOWNZ); rcar_mipi_dsi_set(dsi, PHTC, PHTC_TESTCLR); rcar_mipi_dsi_clr(dsi, PHTC, PHTC_TESTCLR); /* PHY setting */ phy_setup = rcar_mipi_dsi_read(dsi, PHYSETUP); phy_setup &= ~PHYSETUP_HSFREQRANGE_MASK; phy_setup |= PHYSETUP_HSFREQRANGE(setup_info.hsfreqrange); rcar_mipi_dsi_write(dsi, PHYSETUP, phy_setup); switch (dsi->info->model) { case RCAR_DSI_V3U: default: ret = rcar_mipi_dsi_init_phtw_v3u(dsi); if (ret < 0) return ret; break; case RCAR_DSI_V4H: ret = rcar_mipi_dsi_init_phtw_v4h(dsi, &setup_info); if (ret < 0) return ret; break; } /* PLL Clock Setting */ rcar_mipi_dsi_clr(dsi, CLOCKSET1, CLOCKSET1_SHADOW_CLEAR); rcar_mipi_dsi_set(dsi, CLOCKSET1, CLOCKSET1_SHADOW_CLEAR); rcar_mipi_dsi_clr(dsi, CLOCKSET1, CLOCKSET1_SHADOW_CLEAR); clockset2 = CLOCKSET2_M(setup_info.m - dsi->info->clockset2_m_offset) | CLOCKSET2_N(setup_info.n - 1) | CLOCKSET2_VCO_CNTRL(setup_info.clkset->vco_cntrl); clockset3 = CLOCKSET3_PROP_CNTRL(setup_info.clkset->prop_cntrl) | CLOCKSET3_INT_CNTRL(setup_info.clkset->int_cntrl) | CLOCKSET3_CPBIAS_CNTRL(setup_info.clkset->cpbias_cntrl) | CLOCKSET3_GMP_CNTRL(setup_info.clkset->gmp_cntrl); rcar_mipi_dsi_write(dsi, CLOCKSET2, clockset2); rcar_mipi_dsi_write(dsi, CLOCKSET3, clockset3); rcar_mipi_dsi_clr(dsi, CLOCKSET1, CLOCKSET1_UPDATEPLL); rcar_mipi_dsi_set(dsi, CLOCKSET1, CLOCKSET1_UPDATEPLL); udelay(10); rcar_mipi_dsi_clr(dsi, CLOCKSET1, CLOCKSET1_UPDATEPLL); ppisetr = PPISETR_DLEN_3 | PPISETR_CLEN; rcar_mipi_dsi_write(dsi, PPISETR, ppisetr); rcar_mipi_dsi_set(dsi, PHYSETUP, PHYSETUP_SHUTDOWNZ); rcar_mipi_dsi_set(dsi, PHYSETUP, PHYSETUP_RSTZ); usleep_range(400, 500); /* Checking PPI clock status register */ for (timeout = 10; timeout > 0; --timeout) { if ((rcar_mipi_dsi_read(dsi, PPICLSR) & PPICLSR_STPST) && (rcar_mipi_dsi_read(dsi, PPIDLSR) & PPIDLSR_STPST) && (rcar_mipi_dsi_read(dsi, CLOCKSET1) & CLOCKSET1_LOCK)) break; usleep_range(1000, 2000); } if (!timeout) { dev_err(dsi->dev, "failed to enable PPI clock\n"); return -ETIMEDOUT; } switch (dsi->info->model) { case RCAR_DSI_V3U: default: ret = rcar_mipi_dsi_post_init_phtw_v3u(dsi); if (ret < 0) return ret; break; case RCAR_DSI_V4H: ret = rcar_mipi_dsi_post_init_phtw_v4h(dsi, &setup_info); if (ret < 0) return ret; break; } /* Enable DOT clock */ vclkset = VCLKSET_CKEN; rcar_mipi_dsi_write(dsi, VCLKSET, vclkset); if (dsi_format == 24) vclkset |= VCLKSET_BPP_24; else if (dsi_format == 18) vclkset |= VCLKSET_BPP_18; else if (dsi_format == 16) vclkset |= VCLKSET_BPP_16; else { dev_warn(dsi->dev, "unsupported format"); return -EINVAL; } vclkset |= VCLKSET_COLOR_RGB | VCLKSET_LANE(dsi->lanes - 1); switch (dsi->info->model) { case RCAR_DSI_V3U: default: vclkset |= VCLKSET_DIV_V3U(__ffs(setup_info.vclk_divider)); break; case RCAR_DSI_V4H: vclkset |= VCLKSET_DIV_V4H(__ffs(setup_info.vclk_divider) - 1); break; } rcar_mipi_dsi_write(dsi, VCLKSET, vclkset); /* After setting VCLKSET register, enable VCLKEN */ rcar_mipi_dsi_set(dsi, VCLKEN, VCLKEN_CKEN); dev_dbg(dsi->dev, "DSI device is started\n"); return 0; } static void rcar_mipi_dsi_shutdown(struct rcar_mipi_dsi *dsi) { /* Disable VCLKEN */ rcar_mipi_dsi_write(dsi, VCLKSET, 0); /* Disable DOT clock */ rcar_mipi_dsi_write(dsi, VCLKSET, 0); rcar_mipi_dsi_clr(dsi, PHYSETUP, PHYSETUP_RSTZ); rcar_mipi_dsi_clr(dsi, PHYSETUP, PHYSETUP_SHUTDOWNZ); /* CFGCLK disable */ rcar_mipi_dsi_clr(dsi, CFGCLKSET, CFGCLKSET_CKEN); /* LPCLK disable */ rcar_mipi_dsi_clr(dsi, LPCLKSET, LPCLKSET_CKEN); dev_dbg(dsi->dev, "DSI device is shutdown\n"); } static int rcar_mipi_dsi_clk_enable(struct rcar_mipi_dsi *dsi) { int ret; reset_control_deassert(dsi->rstc); ret = clk_prepare_enable(dsi->clocks.mod); if (ret < 0) goto err_reset; ret = clk_prepare_enable(dsi->clocks.dsi); if (ret < 0) goto err_clock; return 0; err_clock: clk_disable_unprepare(dsi->clocks.mod); err_reset: reset_control_assert(dsi->rstc); return ret; } static void rcar_mipi_dsi_clk_disable(struct rcar_mipi_dsi *dsi) { clk_disable_unprepare(dsi->clocks.dsi); clk_disable_unprepare(dsi->clocks.mod); reset_control_assert(dsi->rstc); } static int rcar_mipi_dsi_start_hs_clock(struct rcar_mipi_dsi *dsi) { /* * In HW manual, we need to check TxDDRClkHS-Q Stable? but it dont * write how to check. So we skip this check in this patch */ u32 status; int ret; /* Start HS clock. */ rcar_mipi_dsi_set(dsi, PPICLCR, PPICLCR_TXREQHS); ret = read_poll_timeout(rcar_mipi_dsi_read, status, status & PPICLSR_TOHS, 2000, 10000, false, dsi, PPICLSR); if (ret < 0) { dev_err(dsi->dev, "failed to enable HS clock\n"); return ret; } rcar_mipi_dsi_set(dsi, PPICLSCR, PPICLSCR_TOHS); return 0; } static int rcar_mipi_dsi_start_video(struct rcar_mipi_dsi *dsi) { u32 status; int ret; /* Wait for the link to be ready. */ ret = read_poll_timeout(rcar_mipi_dsi_read, status, !(status & (LINKSR_LPBUSY | LINKSR_HSBUSY)), 2000, 10000, false, dsi, LINKSR); if (ret < 0) { dev_err(dsi->dev, "Link failed to become ready\n"); return ret; } /* De-assert video FIFO clear. */ rcar_mipi_dsi_clr(dsi, TXVMCR, TXVMCR_VFCLR); ret = read_poll_timeout(rcar_mipi_dsi_read, status, status & TXVMSR_VFRDY, 2000, 10000, false, dsi, TXVMSR); if (ret < 0) { dev_err(dsi->dev, "Failed to de-assert video FIFO clear\n"); return ret; } /* Enable transmission in video mode. */ rcar_mipi_dsi_set(dsi, TXVMCR, TXVMCR_EN_VIDEO); ret = read_poll_timeout(rcar_mipi_dsi_read, status, status & TXVMSR_RDY, 2000, 10000, false, dsi, TXVMSR); if (ret < 0) { dev_err(dsi->dev, "Failed to enable video transmission\n"); return ret; } return 0; } static void rcar_mipi_dsi_stop_video(struct rcar_mipi_dsi *dsi) { u32 status; int ret; /* Disable transmission in video mode. */ rcar_mipi_dsi_clr(dsi, TXVMCR, TXVMCR_EN_VIDEO); ret = read_poll_timeout(rcar_mipi_dsi_read, status, !(status & TXVMSR_ACT), 2000, 100000, false, dsi, TXVMSR); if (ret < 0) { dev_err(dsi->dev, "Failed to disable video transmission\n"); return; } /* Assert video FIFO clear. */ rcar_mipi_dsi_set(dsi, TXVMCR, TXVMCR_VFCLR); ret = read_poll_timeout(rcar_mipi_dsi_read, status, !(status & TXVMSR_VFRDY), 2000, 100000, false, dsi, TXVMSR); if (ret < 0) { dev_err(dsi->dev, "Failed to assert video FIFO clear\n"); return; } } /* ----------------------------------------------------------------------------- * Bridge */ static int rcar_mipi_dsi_attach(struct drm_bridge *bridge, enum drm_bridge_attach_flags flags) { struct rcar_mipi_dsi *dsi = bridge_to_rcar_mipi_dsi(bridge); return drm_bridge_attach(bridge->encoder, dsi->next_bridge, bridge, flags); } static void rcar_mipi_dsi_atomic_enable(struct drm_bridge *bridge, struct drm_bridge_state *old_bridge_state) { struct rcar_mipi_dsi *dsi = bridge_to_rcar_mipi_dsi(bridge); rcar_mipi_dsi_start_video(dsi); } static void rcar_mipi_dsi_atomic_disable(struct drm_bridge *bridge, struct drm_bridge_state *old_bridge_state) { struct rcar_mipi_dsi *dsi = bridge_to_rcar_mipi_dsi(bridge); rcar_mipi_dsi_stop_video(dsi); } void rcar_mipi_dsi_pclk_enable(struct drm_bridge *bridge, struct drm_atomic_state *state) { struct rcar_mipi_dsi *dsi = bridge_to_rcar_mipi_dsi(bridge); const struct drm_display_mode *mode; struct drm_connector *connector; struct drm_crtc *crtc; int ret; connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder); crtc = drm_atomic_get_new_connector_state(state, connector)->crtc; mode = &drm_atomic_get_new_crtc_state(state, crtc)->adjusted_mode; ret = rcar_mipi_dsi_clk_enable(dsi); if (ret < 0) { dev_err(dsi->dev, "failed to enable DSI clocks\n"); return; } ret = rcar_mipi_dsi_startup(dsi, mode); if (ret < 0) goto err_dsi_startup; rcar_mipi_dsi_set_display_timing(dsi, mode); ret = rcar_mipi_dsi_start_hs_clock(dsi); if (ret < 0) goto err_dsi_start_hs; return; err_dsi_start_hs: rcar_mipi_dsi_shutdown(dsi); err_dsi_startup: rcar_mipi_dsi_clk_disable(dsi); } EXPORT_SYMBOL_GPL(rcar_mipi_dsi_pclk_enable); void rcar_mipi_dsi_pclk_disable(struct drm_bridge *bridge) { struct rcar_mipi_dsi *dsi = bridge_to_rcar_mipi_dsi(bridge); rcar_mipi_dsi_shutdown(dsi); rcar_mipi_dsi_clk_disable(dsi); } EXPORT_SYMBOL_GPL(rcar_mipi_dsi_pclk_disable); static enum drm_mode_status rcar_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge, const struct drm_display_info *info, const struct drm_display_mode *mode) { if (mode->clock > 297000) return MODE_CLOCK_HIGH; return MODE_OK; } static const struct drm_bridge_funcs rcar_mipi_dsi_bridge_ops = { .attach = rcar_mipi_dsi_attach, .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, .atomic_reset = drm_atomic_helper_bridge_reset, .atomic_enable = rcar_mipi_dsi_atomic_enable, .atomic_disable = rcar_mipi_dsi_atomic_disable, .mode_valid = rcar_mipi_dsi_bridge_mode_valid, }; /* ----------------------------------------------------------------------------- * Host setting */ static int rcar_mipi_dsi_host_attach(struct mipi_dsi_host *host, struct mipi_dsi_device *device) { struct rcar_mipi_dsi *dsi = host_to_rcar_mipi_dsi(host); int ret; if (device->lanes > dsi->num_data_lanes) return -EINVAL; dsi->lanes = device->lanes; dsi->format = device->format; dsi->next_bridge = devm_drm_of_get_bridge(dsi->dev, dsi->dev->of_node, 1, 0); if (IS_ERR(dsi->next_bridge)) { ret = PTR_ERR(dsi->next_bridge); dev_err(dsi->dev, "failed to get next bridge: %d\n", ret); return ret; } /* Initialize the DRM bridge. */ dsi->bridge.funcs = &rcar_mipi_dsi_bridge_ops; dsi->bridge.of_node = dsi->dev->of_node; drm_bridge_add(&dsi->bridge); return 0; } static int rcar_mipi_dsi_host_detach(struct mipi_dsi_host *host, struct mipi_dsi_device *device) { struct rcar_mipi_dsi *dsi = host_to_rcar_mipi_dsi(host); drm_bridge_remove(&dsi->bridge); return 0; } static const struct mipi_dsi_host_ops rcar_mipi_dsi_host_ops = { .attach = rcar_mipi_dsi_host_attach, .detach = rcar_mipi_dsi_host_detach, }; /* ----------------------------------------------------------------------------- * Probe & Remove */ static int rcar_mipi_dsi_parse_dt(struct rcar_mipi_dsi *dsi) { int ret; ret = drm_of_get_data_lanes_count_ep(dsi->dev->of_node, 1, 0, 1, 4); if (ret < 0) { dev_err(dsi->dev, "missing or invalid data-lanes property\n"); return ret; } dsi->num_data_lanes = ret; return 0; } static struct clk *rcar_mipi_dsi_get_clock(struct rcar_mipi_dsi *dsi, const char *name, bool optional) { struct clk *clk; clk = devm_clk_get(dsi->dev, name); if (!IS_ERR(clk)) return clk; if (PTR_ERR(clk) == -ENOENT && optional) return NULL; dev_err_probe(dsi->dev, PTR_ERR(clk), "failed to get %s clock\n", name ? name : "module"); return clk; } static int rcar_mipi_dsi_get_clocks(struct rcar_mipi_dsi *dsi) { dsi->clocks.mod = rcar_mipi_dsi_get_clock(dsi, NULL, false); if (IS_ERR(dsi->clocks.mod)) return PTR_ERR(dsi->clocks.mod); dsi->clocks.pll = rcar_mipi_dsi_get_clock(dsi, "pll", true); if (IS_ERR(dsi->clocks.pll)) return PTR_ERR(dsi->clocks.pll); dsi->clocks.dsi = rcar_mipi_dsi_get_clock(dsi, "dsi", true); if (IS_ERR(dsi->clocks.dsi)) return PTR_ERR(dsi->clocks.dsi); if (!dsi->clocks.pll && !dsi->clocks.dsi) { dev_err(dsi->dev, "no input clock (pll, dsi)\n"); return -EINVAL; } return 0; } static int rcar_mipi_dsi_probe(struct platform_device *pdev) { struct rcar_mipi_dsi *dsi; struct resource *mem; int ret; dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL); if (dsi == NULL) return -ENOMEM; platform_set_drvdata(pdev, dsi); dsi->dev = &pdev->dev; dsi->info = of_device_get_match_data(&pdev->dev); ret = rcar_mipi_dsi_parse_dt(dsi); if (ret < 0) return ret; /* Acquire resources. */ mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); dsi->mmio = devm_ioremap_resource(dsi->dev, mem); if (IS_ERR(dsi->mmio)) return PTR_ERR(dsi->mmio); ret = rcar_mipi_dsi_get_clocks(dsi); if (ret < 0) return ret; dsi->rstc = devm_reset_control_get(dsi->dev, NULL); if (IS_ERR(dsi->rstc)) { dev_err(dsi->dev, "failed to get cpg reset\n"); return PTR_ERR(dsi->rstc); } /* Initialize the DSI host. */ dsi->host.dev = dsi->dev; dsi->host.ops = &rcar_mipi_dsi_host_ops; ret = mipi_dsi_host_register(&dsi->host); if (ret < 0) return ret; return 0; } static int rcar_mipi_dsi_remove(struct platform_device *pdev) { struct rcar_mipi_dsi *dsi = platform_get_drvdata(pdev); mipi_dsi_host_unregister(&dsi->host); return 0; } static const struct rcar_mipi_dsi_device_info v3u_data = { .model = RCAR_DSI_V3U, .clk_cfg = dsi_clk_cfg_v3u, .clockset2_m_offset = 2, .n_min = 3, .n_max = 8, .n_mul = 1, .fpfd_min = MHZ(2), .fpfd_max = MHZ(8), .m_min = 64, .m_max = 625, .fout_min = MHZ(320), .fout_max = MHZ(1250), }; static const struct rcar_mipi_dsi_device_info v4h_data = { .model = RCAR_DSI_V4H, .clk_cfg = dsi_clk_cfg_v4h, .clockset2_m_offset = 0, .n_min = 1, .n_max = 8, .n_mul = 2, .fpfd_min = MHZ(8), .fpfd_max = MHZ(24), .m_min = 167, .m_max = 1000, .fout_min = MHZ(2000), .fout_max = MHZ(4000), }; static const struct of_device_id rcar_mipi_dsi_of_table[] = { { .compatible = "renesas,r8a779a0-dsi-csi2-tx", .data = &v3u_data }, { .compatible = "renesas,r8a779g0-dsi-csi2-tx", .data = &v4h_data }, { } }; MODULE_DEVICE_TABLE(of, rcar_mipi_dsi_of_table); static struct platform_driver rcar_mipi_dsi_platform_driver = { .probe = rcar_mipi_dsi_probe, .remove = rcar_mipi_dsi_remove, .driver = { .name = "rcar-mipi-dsi", .of_match_table = rcar_mipi_dsi_of_table, }, }; module_platform_driver(rcar_mipi_dsi_platform_driver); MODULE_DESCRIPTION("Renesas R-Car MIPI DSI Encoder Driver"); MODULE_LICENSE("GPL"); |