phy-core-mipi-dphy.c 4.3 KB

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