phy-core-mipi-dphy.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2013 NVIDIA Corporation
  4. * Copyright (C) 2018 Cadence Design Systems Inc.
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/export.h>
  8. #include <linux/kernel.h>
  9. #include <linux/time64.h>
  10. #include <linux/phy/phy.h>
  11. #include <linux/phy/phy-mipi-dphy.h>
  12. #define PSEC_PER_SEC 1000000000000LL
  13. /*
  14. * Minimum D-PHY timings based on MIPI D-PHY specification. Derived
  15. * from the valid ranges specified in Section 6.9, Table 14, Page 41
  16. * of the D-PHY specification (v2.1).
  17. */
  18. int phy_mipi_dphy_get_default_config(unsigned long pixel_clock,
  19. unsigned int bpp,
  20. unsigned int lanes,
  21. struct phy_configure_opts_mipi_dphy *cfg)
  22. {
  23. unsigned long long hs_clk_rate;
  24. unsigned long long ui;
  25. if (!cfg)
  26. return -EINVAL;
  27. hs_clk_rate = pixel_clock * bpp;
  28. do_div(hs_clk_rate, lanes);
  29. ui = ALIGN(PSEC_PER_SEC, hs_clk_rate);
  30. do_div(ui, hs_clk_rate);
  31. cfg->clk_miss = 0;
  32. cfg->clk_post = 60000 + 52 * ui;
  33. cfg->clk_pre = 8000;
  34. cfg->clk_prepare = 38000;
  35. cfg->clk_settle = 95000;
  36. cfg->clk_term_en = 0;
  37. cfg->clk_trail = 60000;
  38. cfg->clk_zero = 262000;
  39. cfg->d_term_en = 0;
  40. cfg->eot = 0;
  41. cfg->hs_exit = 100000;
  42. cfg->hs_prepare = 40000 + 4 * ui;
  43. cfg->hs_zero = 105000 + 6 * ui;
  44. cfg->hs_settle = 85000 + 6 * ui;
  45. cfg->hs_skip = 40000;
  46. /*
  47. * The MIPI D-PHY specification (Section 6.9, v1.2, Table 14, Page 40)
  48. * contains this formula as:
  49. *
  50. * T_HS-TRAIL = max(n * 8 * ui, 60 + n * 4 * ui)
  51. *
  52. * where n = 1 for forward-direction HS mode and n = 4 for reverse-
  53. * direction HS mode. There's only one setting and this function does
  54. * not parameterize on anything other that ui, so this code will
  55. * assumes that reverse-direction HS mode is supported and uses n = 4.
  56. */
  57. cfg->hs_trail = max(4 * 8 * ui, 60000 + 4 * 4 * ui);
  58. cfg->init = 100;
  59. cfg->lpx = 50000;
  60. cfg->ta_get = 5 * cfg->lpx;
  61. cfg->ta_go = 4 * cfg->lpx;
  62. cfg->ta_sure = cfg->lpx;
  63. cfg->wakeup = 1000;
  64. cfg->hs_clk_rate = hs_clk_rate;
  65. cfg->lanes = lanes;
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(phy_mipi_dphy_get_default_config);
  69. /*
  70. * Validate D-PHY configuration according to MIPI D-PHY specification
  71. * (v1.2, Section Section 6.9 "Global Operation Timing Parameters").
  72. */
  73. int phy_mipi_dphy_config_validate(struct phy_configure_opts_mipi_dphy *cfg)
  74. {
  75. unsigned long long ui;
  76. if (!cfg)
  77. return -EINVAL;
  78. ui = ALIGN(PSEC_PER_SEC, cfg->hs_clk_rate);
  79. do_div(ui, cfg->hs_clk_rate);
  80. if (cfg->clk_miss > 60000)
  81. return -EINVAL;
  82. if (cfg->clk_post < (60000 + 52 * ui))
  83. return -EINVAL;
  84. if (cfg->clk_pre < 8000)
  85. return -EINVAL;
  86. if (cfg->clk_prepare < 38000 || cfg->clk_prepare > 95000)
  87. return -EINVAL;
  88. if (cfg->clk_settle < 95000 || cfg->clk_settle > 300000)
  89. return -EINVAL;
  90. if (cfg->clk_term_en > 38000)
  91. return -EINVAL;
  92. if (cfg->clk_trail < 60000)
  93. return -EINVAL;
  94. if ((cfg->clk_prepare + cfg->clk_zero) < 300000)
  95. return -EINVAL;
  96. if (cfg->d_term_en > (35000 + 4 * ui))
  97. return -EINVAL;
  98. if (cfg->eot > (105000 + 12 * ui))
  99. return -EINVAL;
  100. if (cfg->hs_exit < 100000)
  101. return -EINVAL;
  102. if (cfg->hs_prepare < (40000 + 4 * ui) ||
  103. cfg->hs_prepare > (85000 + 6 * ui))
  104. return -EINVAL;
  105. if ((cfg->hs_prepare + cfg->hs_zero) < (145000 + 10 * ui))
  106. return -EINVAL;
  107. if ((cfg->hs_settle < (85000 + 6 * ui)) ||
  108. (cfg->hs_settle > (145000 + 10 * ui)))
  109. return -EINVAL;
  110. if (cfg->hs_skip < 40000 || cfg->hs_skip > (55000 + 4 * ui))
  111. return -EINVAL;
  112. if (cfg->hs_trail < max(8 * ui, 60000 + 4 * ui))
  113. return -EINVAL;
  114. if (cfg->init < 100)
  115. return -EINVAL;
  116. if (cfg->lpx < 50000)
  117. return -EINVAL;
  118. if (cfg->ta_get != (5 * cfg->lpx))
  119. return -EINVAL;
  120. if (cfg->ta_go != (4 * cfg->lpx))
  121. return -EINVAL;
  122. if (cfg->ta_sure < cfg->lpx || cfg->ta_sure > (2 * cfg->lpx))
  123. return -EINVAL;
  124. if (cfg->wakeup < 1000)
  125. return -EINVAL;
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(phy_mipi_dphy_config_validate);