phy-core-mipi-dphy.c 3.7 KB

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