dsi_phy.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/platform_device.h>
  7. #include <dt-bindings/phy/phy.h>
  8. #include "dsi_phy.h"
  9. #define S_DIV_ROUND_UP(n, d) \
  10. (((n) >= 0) ? (((n) + (d) - 1) / (d)) : (((n) - (d) + 1) / (d)))
  11. static inline s32 linear_inter(s32 tmax, s32 tmin, s32 percent,
  12. s32 min_result, bool even)
  13. {
  14. s32 v;
  15. v = (tmax - tmin) * percent;
  16. v = S_DIV_ROUND_UP(v, 100) + tmin;
  17. if (even && (v & 0x1))
  18. return max_t(s32, min_result, v - 1);
  19. else
  20. return max_t(s32, min_result, v);
  21. }
  22. static void dsi_dphy_timing_calc_clk_zero(struct msm_dsi_dphy_timing *timing,
  23. s32 ui, s32 coeff, s32 pcnt)
  24. {
  25. s32 tmax, tmin, clk_z;
  26. s32 temp;
  27. /* reset */
  28. temp = 300 * coeff - ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  29. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  30. if (tmin > 255) {
  31. tmax = 511;
  32. clk_z = linear_inter(2 * tmin, tmin, pcnt, 0, true);
  33. } else {
  34. tmax = 255;
  35. clk_z = linear_inter(tmax, tmin, pcnt, 0, true);
  36. }
  37. /* adjust */
  38. temp = (timing->hs_rqst + timing->clk_prepare + clk_z) & 0x7;
  39. timing->clk_zero = clk_z + 8 - temp;
  40. }
  41. int msm_dsi_dphy_timing_calc(struct msm_dsi_dphy_timing *timing,
  42. struct msm_dsi_phy_clk_request *clk_req)
  43. {
  44. const unsigned long bit_rate = clk_req->bitclk_rate;
  45. const unsigned long esc_rate = clk_req->escclk_rate;
  46. s32 ui, lpx;
  47. s32 tmax, tmin;
  48. s32 pcnt0 = 10;
  49. s32 pcnt1 = (bit_rate > 1200000000) ? 15 : 10;
  50. s32 pcnt2 = 10;
  51. s32 pcnt3 = (bit_rate > 180000000) ? 10 : 40;
  52. s32 coeff = 1000; /* Precision, should avoid overflow */
  53. s32 temp;
  54. if (!bit_rate || !esc_rate)
  55. return -EINVAL;
  56. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  57. lpx = mult_frac(NSEC_PER_MSEC, coeff, esc_rate / 1000);
  58. tmax = S_DIV_ROUND_UP(95 * coeff, ui) - 2;
  59. tmin = S_DIV_ROUND_UP(38 * coeff, ui) - 2;
  60. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, true);
  61. temp = lpx / ui;
  62. if (temp & 0x1)
  63. timing->hs_rqst = temp;
  64. else
  65. timing->hs_rqst = max_t(s32, 0, temp - 2);
  66. /* Calculate clk_zero after clk_prepare and hs_rqst */
  67. dsi_dphy_timing_calc_clk_zero(timing, ui, coeff, pcnt2);
  68. temp = 105 * coeff + 12 * ui - 20 * coeff;
  69. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  70. tmin = S_DIV_ROUND_UP(60 * coeff, ui) - 2;
  71. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  72. temp = 85 * coeff + 6 * ui;
  73. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  74. temp = 40 * coeff + 4 * ui;
  75. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  76. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, true);
  77. tmax = 255;
  78. temp = ((timing->hs_prepare >> 1) + 1) * 2 * ui + 2 * ui;
  79. temp = 145 * coeff + 10 * ui - temp;
  80. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  81. timing->hs_zero = linear_inter(tmax, tmin, pcnt2, 24, true);
  82. temp = 105 * coeff + 12 * ui - 20 * coeff;
  83. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  84. temp = 60 * coeff + 4 * ui;
  85. tmin = DIV_ROUND_UP(temp, ui) - 2;
  86. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  87. tmax = 255;
  88. tmin = S_DIV_ROUND_UP(100 * coeff, ui) - 2;
  89. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, true);
  90. tmax = 63;
  91. temp = ((timing->hs_exit >> 1) + 1) * 2 * ui;
  92. temp = 60 * coeff + 52 * ui - 24 * ui - temp;
  93. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  94. timing->shared_timings.clk_post = linear_inter(tmax, tmin, pcnt2, 0,
  95. false);
  96. tmax = 63;
  97. temp = ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  98. temp += ((timing->clk_zero >> 1) + 1) * 2 * ui;
  99. temp += 8 * ui + lpx;
  100. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  101. if (tmin > tmax) {
  102. temp = linear_inter(2 * tmax, tmin, pcnt2, 0, false);
  103. timing->shared_timings.clk_pre = temp >> 1;
  104. timing->shared_timings.clk_pre_inc_by_2 = true;
  105. } else {
  106. timing->shared_timings.clk_pre =
  107. linear_inter(tmax, tmin, pcnt2, 0, false);
  108. timing->shared_timings.clk_pre_inc_by_2 = false;
  109. }
  110. timing->ta_go = 3;
  111. timing->ta_sure = 0;
  112. timing->ta_get = 4;
  113. DBG("PHY timings: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  114. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  115. timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
  116. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  117. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  118. timing->hs_rqst);
  119. return 0;
  120. }
  121. int msm_dsi_dphy_timing_calc_v2(struct msm_dsi_dphy_timing *timing,
  122. struct msm_dsi_phy_clk_request *clk_req)
  123. {
  124. const unsigned long bit_rate = clk_req->bitclk_rate;
  125. const unsigned long esc_rate = clk_req->escclk_rate;
  126. s32 ui, ui_x8;
  127. s32 tmax, tmin;
  128. s32 pcnt0 = 50;
  129. s32 pcnt1 = 50;
  130. s32 pcnt2 = 10;
  131. s32 pcnt3 = 30;
  132. s32 pcnt4 = 10;
  133. s32 pcnt5 = 2;
  134. s32 coeff = 1000; /* Precision, should avoid overflow */
  135. s32 hb_en, hb_en_ckln, pd_ckln, pd;
  136. s32 val, val_ckln;
  137. s32 temp;
  138. if (!bit_rate || !esc_rate)
  139. return -EINVAL;
  140. timing->hs_halfbyte_en = 0;
  141. hb_en = 0;
  142. timing->hs_halfbyte_en_ckln = 0;
  143. hb_en_ckln = 0;
  144. timing->hs_prep_dly_ckln = (bit_rate > 100000000) ? 0 : 3;
  145. pd_ckln = timing->hs_prep_dly_ckln;
  146. timing->hs_prep_dly = (bit_rate > 120000000) ? 0 : 1;
  147. pd = timing->hs_prep_dly;
  148. val = (hb_en << 2) + (pd << 1);
  149. val_ckln = (hb_en_ckln << 2) + (pd_ckln << 1);
  150. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  151. ui_x8 = ui << 3;
  152. temp = S_DIV_ROUND_UP(38 * coeff - val_ckln * ui, ui_x8);
  153. tmin = max_t(s32, temp, 0);
  154. temp = (95 * coeff - val_ckln * ui) / ui_x8;
  155. tmax = max_t(s32, temp, 0);
  156. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, false);
  157. temp = 300 * coeff - ((timing->clk_prepare << 3) + val_ckln) * ui;
  158. tmin = S_DIV_ROUND_UP(temp - 11 * ui, ui_x8) - 3;
  159. tmax = (tmin > 255) ? 511 : 255;
  160. timing->clk_zero = linear_inter(tmax, tmin, pcnt5, 0, false);
  161. tmin = DIV_ROUND_UP(60 * coeff + 3 * ui, ui_x8);
  162. temp = 105 * coeff + 12 * ui - 20 * coeff;
  163. tmax = (temp + 3 * ui) / ui_x8;
  164. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
  165. temp = S_DIV_ROUND_UP(40 * coeff + 4 * ui - val * ui, ui_x8);
  166. tmin = max_t(s32, temp, 0);
  167. temp = (85 * coeff + 6 * ui - val * ui) / ui_x8;
  168. tmax = max_t(s32, temp, 0);
  169. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, false);
  170. temp = 145 * coeff + 10 * ui - ((timing->hs_prepare << 3) + val) * ui;
  171. tmin = S_DIV_ROUND_UP(temp - 11 * ui, ui_x8) - 3;
  172. tmax = 255;
  173. timing->hs_zero = linear_inter(tmax, tmin, pcnt4, 0, false);
  174. tmin = DIV_ROUND_UP(60 * coeff + 4 * ui + 3 * ui, ui_x8);
  175. temp = 105 * coeff + 12 * ui - 20 * coeff;
  176. tmax = (temp + 3 * ui) / ui_x8;
  177. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
  178. temp = 50 * coeff + ((hb_en << 2) - 8) * ui;
  179. timing->hs_rqst = S_DIV_ROUND_UP(temp, ui_x8);
  180. tmin = DIV_ROUND_UP(100 * coeff, ui_x8) - 1;
  181. tmax = 255;
  182. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, false);
  183. temp = 50 * coeff + ((hb_en_ckln << 2) - 8) * ui;
  184. timing->hs_rqst_ckln = S_DIV_ROUND_UP(temp, ui_x8);
  185. temp = 60 * coeff + 52 * ui - 43 * ui;
  186. tmin = DIV_ROUND_UP(temp, ui_x8) - 1;
  187. tmax = 63;
  188. timing->shared_timings.clk_post =
  189. linear_inter(tmax, tmin, pcnt2, 0, false);
  190. temp = 8 * ui + ((timing->clk_prepare << 3) + val_ckln) * ui;
  191. temp += (((timing->clk_zero + 3) << 3) + 11 - (pd_ckln << 1)) * ui;
  192. temp += hb_en_ckln ? (((timing->hs_rqst_ckln << 3) + 4) * ui) :
  193. (((timing->hs_rqst_ckln << 3) + 8) * ui);
  194. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  195. tmax = 63;
  196. if (tmin > tmax) {
  197. temp = linear_inter(tmax << 1, tmin, pcnt2, 0, false);
  198. timing->shared_timings.clk_pre = temp >> 1;
  199. timing->shared_timings.clk_pre_inc_by_2 = 1;
  200. } else {
  201. timing->shared_timings.clk_pre =
  202. linear_inter(tmax, tmin, pcnt2, 0, false);
  203. timing->shared_timings.clk_pre_inc_by_2 = 0;
  204. }
  205. timing->ta_go = 3;
  206. timing->ta_sure = 0;
  207. timing->ta_get = 4;
  208. DBG("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  209. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  210. timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
  211. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  212. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  213. timing->hs_rqst, timing->hs_rqst_ckln, timing->hs_halfbyte_en,
  214. timing->hs_halfbyte_en_ckln, timing->hs_prep_dly,
  215. timing->hs_prep_dly_ckln);
  216. return 0;
  217. }
  218. int msm_dsi_dphy_timing_calc_v3(struct msm_dsi_dphy_timing *timing,
  219. struct msm_dsi_phy_clk_request *clk_req)
  220. {
  221. const unsigned long bit_rate = clk_req->bitclk_rate;
  222. const unsigned long esc_rate = clk_req->escclk_rate;
  223. s32 ui, ui_x8;
  224. s32 tmax, tmin;
  225. s32 pcnt0 = 50;
  226. s32 pcnt1 = 50;
  227. s32 pcnt2 = 10;
  228. s32 pcnt3 = 30;
  229. s32 pcnt4 = 10;
  230. s32 pcnt5 = 2;
  231. s32 coeff = 1000; /* Precision, should avoid overflow */
  232. s32 hb_en, hb_en_ckln;
  233. s32 temp;
  234. if (!bit_rate || !esc_rate)
  235. return -EINVAL;
  236. timing->hs_halfbyte_en = 0;
  237. hb_en = 0;
  238. timing->hs_halfbyte_en_ckln = 0;
  239. hb_en_ckln = 0;
  240. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  241. ui_x8 = ui << 3;
  242. temp = S_DIV_ROUND_UP(38 * coeff, ui_x8);
  243. tmin = max_t(s32, temp, 0);
  244. temp = (95 * coeff) / ui_x8;
  245. tmax = max_t(s32, temp, 0);
  246. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, false);
  247. temp = 300 * coeff - (timing->clk_prepare << 3) * ui;
  248. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  249. tmax = (tmin > 255) ? 511 : 255;
  250. timing->clk_zero = linear_inter(tmax, tmin, pcnt5, 0, false);
  251. tmin = DIV_ROUND_UP(60 * coeff + 3 * ui, ui_x8);
  252. temp = 105 * coeff + 12 * ui - 20 * coeff;
  253. tmax = (temp + 3 * ui) / ui_x8;
  254. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
  255. temp = S_DIV_ROUND_UP(40 * coeff + 4 * ui, ui_x8);
  256. tmin = max_t(s32, temp, 0);
  257. temp = (85 * coeff + 6 * ui) / ui_x8;
  258. tmax = max_t(s32, temp, 0);
  259. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, false);
  260. temp = 145 * coeff + 10 * ui - (timing->hs_prepare << 3) * ui;
  261. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  262. tmax = 255;
  263. timing->hs_zero = linear_inter(tmax, tmin, pcnt4, 0, false);
  264. tmin = DIV_ROUND_UP(60 * coeff + 4 * ui, ui_x8) - 1;
  265. temp = 105 * coeff + 12 * ui - 20 * coeff;
  266. tmax = (temp / ui_x8) - 1;
  267. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
  268. temp = 50 * coeff + ((hb_en << 2) - 8) * ui;
  269. timing->hs_rqst = S_DIV_ROUND_UP(temp, ui_x8);
  270. tmin = DIV_ROUND_UP(100 * coeff, ui_x8) - 1;
  271. tmax = 255;
  272. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, false);
  273. temp = 50 * coeff + ((hb_en_ckln << 2) - 8) * ui;
  274. timing->hs_rqst_ckln = S_DIV_ROUND_UP(temp, ui_x8);
  275. temp = 60 * coeff + 52 * ui - 43 * ui;
  276. tmin = DIV_ROUND_UP(temp, ui_x8) - 1;
  277. tmax = 63;
  278. timing->shared_timings.clk_post =
  279. linear_inter(tmax, tmin, pcnt2, 0, false);
  280. temp = 8 * ui + (timing->clk_prepare << 3) * ui;
  281. temp += (((timing->clk_zero + 3) << 3) + 11) * ui;
  282. temp += hb_en_ckln ? (((timing->hs_rqst_ckln << 3) + 4) * ui) :
  283. (((timing->hs_rqst_ckln << 3) + 8) * ui);
  284. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  285. tmax = 63;
  286. if (tmin > tmax) {
  287. temp = linear_inter(tmax << 1, tmin, pcnt2, 0, false);
  288. timing->shared_timings.clk_pre = temp >> 1;
  289. timing->shared_timings.clk_pre_inc_by_2 = 1;
  290. } else {
  291. timing->shared_timings.clk_pre =
  292. linear_inter(tmax, tmin, pcnt2, 0, false);
  293. timing->shared_timings.clk_pre_inc_by_2 = 0;
  294. }
  295. timing->shared_timings.byte_intf_clk_div_2 = true;
  296. timing->ta_go = 3;
  297. timing->ta_sure = 0;
  298. timing->ta_get = 4;
  299. DBG("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  300. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  301. timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
  302. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  303. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  304. timing->hs_rqst, timing->hs_rqst_ckln, timing->hs_halfbyte_en,
  305. timing->hs_halfbyte_en_ckln, timing->hs_prep_dly,
  306. timing->hs_prep_dly_ckln);
  307. return 0;
  308. }
  309. int msm_dsi_dphy_timing_calc_v4(struct msm_dsi_dphy_timing *timing,
  310. struct msm_dsi_phy_clk_request *clk_req)
  311. {
  312. const unsigned long bit_rate = clk_req->bitclk_rate;
  313. const unsigned long esc_rate = clk_req->escclk_rate;
  314. s32 ui, ui_x8;
  315. s32 tmax, tmin;
  316. s32 pcnt_clk_prep = 50;
  317. s32 pcnt_clk_zero = 2;
  318. s32 pcnt_clk_trail = 30;
  319. s32 pcnt_hs_prep = 50;
  320. s32 pcnt_hs_zero = 10;
  321. s32 pcnt_hs_trail = 30;
  322. s32 pcnt_hs_exit = 10;
  323. s32 coeff = 1000; /* Precision, should avoid overflow */
  324. s32 hb_en;
  325. s32 temp;
  326. if (!bit_rate || !esc_rate)
  327. return -EINVAL;
  328. hb_en = 0;
  329. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  330. ui_x8 = ui << 3;
  331. /* TODO: verify these calculations against latest downstream driver
  332. * everything except clk_post/clk_pre uses calculations from v3 based
  333. * on the downstream driver having the same calculations for v3 and v4
  334. */
  335. temp = S_DIV_ROUND_UP(38 * coeff, ui_x8);
  336. tmin = max_t(s32, temp, 0);
  337. temp = (95 * coeff) / ui_x8;
  338. tmax = max_t(s32, temp, 0);
  339. timing->clk_prepare = linear_inter(tmax, tmin, pcnt_clk_prep, 0, false);
  340. temp = 300 * coeff - (timing->clk_prepare << 3) * ui;
  341. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  342. tmax = (tmin > 255) ? 511 : 255;
  343. timing->clk_zero = linear_inter(tmax, tmin, pcnt_clk_zero, 0, false);
  344. tmin = DIV_ROUND_UP(60 * coeff + 3 * ui, ui_x8);
  345. temp = 105 * coeff + 12 * ui - 20 * coeff;
  346. tmax = (temp + 3 * ui) / ui_x8;
  347. timing->clk_trail = linear_inter(tmax, tmin, pcnt_clk_trail, 0, false);
  348. temp = S_DIV_ROUND_UP(40 * coeff + 4 * ui, ui_x8);
  349. tmin = max_t(s32, temp, 0);
  350. temp = (85 * coeff + 6 * ui) / ui_x8;
  351. tmax = max_t(s32, temp, 0);
  352. timing->hs_prepare = linear_inter(tmax, tmin, pcnt_hs_prep, 0, false);
  353. temp = 145 * coeff + 10 * ui - (timing->hs_prepare << 3) * ui;
  354. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  355. tmax = 255;
  356. timing->hs_zero = linear_inter(tmax, tmin, pcnt_hs_zero, 0, false);
  357. tmin = DIV_ROUND_UP(60 * coeff + 4 * ui, ui_x8) - 1;
  358. temp = 105 * coeff + 12 * ui - 20 * coeff;
  359. tmax = (temp / ui_x8) - 1;
  360. timing->hs_trail = linear_inter(tmax, tmin, pcnt_hs_trail, 0, false);
  361. temp = 50 * coeff + ((hb_en << 2) - 8) * ui;
  362. timing->hs_rqst = S_DIV_ROUND_UP(temp, ui_x8);
  363. tmin = DIV_ROUND_UP(100 * coeff, ui_x8) - 1;
  364. tmax = 255;
  365. timing->hs_exit = linear_inter(tmax, tmin, pcnt_hs_exit, 0, false);
  366. /* recommended min
  367. * = roundup((mipi_min_ns + t_hs_trail_ns)/(16*bit_clk_ns), 0) - 1
  368. */
  369. temp = 60 * coeff + 52 * ui + + (timing->hs_trail + 1) * ui_x8;
  370. tmin = DIV_ROUND_UP(temp, 16 * ui) - 1;
  371. tmax = 255;
  372. timing->shared_timings.clk_post = linear_inter(tmax, tmin, 5, 0, false);
  373. /* recommended min
  374. * val1 = (tlpx_ns + clk_prepare_ns + clk_zero_ns + hs_rqst_ns)
  375. * val2 = (16 * bit_clk_ns)
  376. * final = roundup(val1/val2, 0) - 1
  377. */
  378. temp = 52 * coeff + (timing->clk_prepare + timing->clk_zero + 1) * ui_x8 + 54 * coeff;
  379. tmin = DIV_ROUND_UP(temp, 16 * ui) - 1;
  380. tmax = 255;
  381. timing->shared_timings.clk_pre = DIV_ROUND_UP((tmax - tmin) * 125, 10000) + tmin;
  382. timing->shared_timings.byte_intf_clk_div_2 = true;
  383. DBG("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  384. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  385. timing->clk_zero, timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  386. timing->hs_zero, timing->hs_prepare, timing->hs_trail, timing->hs_rqst);
  387. return 0;
  388. }
  389. int msm_dsi_cphy_timing_calc_v4(struct msm_dsi_dphy_timing *timing,
  390. struct msm_dsi_phy_clk_request *clk_req)
  391. {
  392. const unsigned long bit_rate = clk_req->bitclk_rate;
  393. const unsigned long esc_rate = clk_req->escclk_rate;
  394. s32 ui, ui_x7;
  395. s32 tmax, tmin;
  396. s32 coeff = 1000; /* Precision, should avoid overflow */
  397. s32 temp;
  398. if (!bit_rate || !esc_rate)
  399. return -EINVAL;
  400. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  401. ui_x7 = ui * 7;
  402. temp = S_DIV_ROUND_UP(38 * coeff, ui_x7);
  403. tmin = max_t(s32, temp, 0);
  404. temp = (95 * coeff) / ui_x7;
  405. tmax = max_t(s32, temp, 0);
  406. timing->clk_prepare = linear_inter(tmax, tmin, 50, 0, false);
  407. tmin = DIV_ROUND_UP(50 * coeff, ui_x7);
  408. tmax = 255;
  409. timing->hs_rqst = linear_inter(tmax, tmin, 1, 0, false);
  410. tmin = DIV_ROUND_UP(100 * coeff, ui_x7) - 1;
  411. tmax = 255;
  412. timing->hs_exit = linear_inter(tmax, tmin, 10, 0, false);
  413. tmin = 1;
  414. tmax = 32;
  415. timing->shared_timings.clk_post = linear_inter(tmax, tmin, 80, 0, false);
  416. tmin = min_t(s32, 64, S_DIV_ROUND_UP(262 * coeff, ui_x7) - 1);
  417. tmax = 64;
  418. timing->shared_timings.clk_pre = linear_inter(tmax, tmin, 20, 0, false);
  419. DBG("%d, %d, %d, %d, %d",
  420. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  421. timing->clk_prepare, timing->hs_exit, timing->hs_rqst);
  422. return 0;
  423. }
  424. static int dsi_phy_enable_resource(struct msm_dsi_phy *phy)
  425. {
  426. struct device *dev = &phy->pdev->dev;
  427. int ret;
  428. ret = pm_runtime_resume_and_get(dev);
  429. if (ret)
  430. return ret;
  431. ret = clk_prepare_enable(phy->ahb_clk);
  432. if (ret) {
  433. DRM_DEV_ERROR(dev, "%s: can't enable ahb clk, %d\n", __func__, ret);
  434. pm_runtime_put_sync(dev);
  435. }
  436. return ret;
  437. }
  438. static void dsi_phy_disable_resource(struct msm_dsi_phy *phy)
  439. {
  440. clk_disable_unprepare(phy->ahb_clk);
  441. pm_runtime_put(&phy->pdev->dev);
  442. }
  443. static const struct of_device_id dsi_phy_dt_match[] = {
  444. #ifdef CONFIG_DRM_MSM_DSI_28NM_PHY
  445. { .compatible = "qcom,dsi-phy-28nm-hpm",
  446. .data = &dsi_phy_28nm_hpm_cfgs },
  447. { .compatible = "qcom,dsi-phy-28nm-hpm-fam-b",
  448. .data = &dsi_phy_28nm_hpm_famb_cfgs },
  449. { .compatible = "qcom,dsi-phy-28nm-lp",
  450. .data = &dsi_phy_28nm_lp_cfgs },
  451. { .compatible = "qcom,dsi-phy-28nm-8226",
  452. .data = &dsi_phy_28nm_8226_cfgs },
  453. { .compatible = "qcom,dsi-phy-28nm-8937",
  454. .data = &dsi_phy_28nm_8937_cfgs },
  455. #endif
  456. #ifdef CONFIG_DRM_MSM_DSI_20NM_PHY
  457. { .compatible = "qcom,dsi-phy-20nm",
  458. .data = &dsi_phy_20nm_cfgs },
  459. #endif
  460. #ifdef CONFIG_DRM_MSM_DSI_28NM_8960_PHY
  461. { .compatible = "qcom,dsi-phy-28nm-8960",
  462. .data = &dsi_phy_28nm_8960_cfgs },
  463. #endif
  464. #ifdef CONFIG_DRM_MSM_DSI_14NM_PHY
  465. { .compatible = "qcom,dsi-phy-14nm",
  466. .data = &dsi_phy_14nm_cfgs },
  467. { .compatible = "qcom,dsi-phy-14nm-2290",
  468. .data = &dsi_phy_14nm_2290_cfgs },
  469. { .compatible = "qcom,dsi-phy-14nm-660",
  470. .data = &dsi_phy_14nm_660_cfgs },
  471. { .compatible = "qcom,dsi-phy-14nm-8953",
  472. .data = &dsi_phy_14nm_8953_cfgs },
  473. { .compatible = "qcom,sm6125-dsi-phy-14nm",
  474. .data = &dsi_phy_14nm_2290_cfgs },
  475. #endif
  476. #ifdef CONFIG_DRM_MSM_DSI_10NM_PHY
  477. { .compatible = "qcom,dsi-phy-10nm",
  478. .data = &dsi_phy_10nm_cfgs },
  479. { .compatible = "qcom,dsi-phy-10nm-8998",
  480. .data = &dsi_phy_10nm_8998_cfgs },
  481. #endif
  482. #ifdef CONFIG_DRM_MSM_DSI_7NM_PHY
  483. { .compatible = "qcom,dsi-phy-7nm",
  484. .data = &dsi_phy_7nm_cfgs },
  485. { .compatible = "qcom,dsi-phy-7nm-8150",
  486. .data = &dsi_phy_7nm_8150_cfgs },
  487. { .compatible = "qcom,sc7280-dsi-phy-7nm",
  488. .data = &dsi_phy_7nm_7280_cfgs },
  489. { .compatible = "qcom,sm6375-dsi-phy-7nm",
  490. .data = &dsi_phy_7nm_6375_cfgs },
  491. { .compatible = "qcom,sm8350-dsi-phy-5nm",
  492. .data = &dsi_phy_5nm_8350_cfgs },
  493. { .compatible = "qcom,sm8450-dsi-phy-5nm",
  494. .data = &dsi_phy_5nm_8450_cfgs },
  495. { .compatible = "qcom,sm8550-dsi-phy-4nm",
  496. .data = &dsi_phy_4nm_8550_cfgs },
  497. { .compatible = "qcom,sm8650-dsi-phy-4nm",
  498. .data = &dsi_phy_4nm_8650_cfgs },
  499. #endif
  500. {}
  501. };
  502. /*
  503. * Currently, we only support one SoC for each PHY type. When we have multiple
  504. * SoCs for the same PHY, we can try to make the index searching a bit more
  505. * clever.
  506. */
  507. static int dsi_phy_get_id(struct msm_dsi_phy *phy)
  508. {
  509. struct platform_device *pdev = phy->pdev;
  510. const struct msm_dsi_phy_cfg *cfg = phy->cfg;
  511. struct resource *res;
  512. int i;
  513. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_phy");
  514. if (!res)
  515. return -EINVAL;
  516. for (i = 0; i < cfg->num_dsi_phy; i++) {
  517. if (cfg->io_start[i] == res->start)
  518. return i;
  519. }
  520. return -EINVAL;
  521. }
  522. static int dsi_phy_driver_probe(struct platform_device *pdev)
  523. {
  524. struct msm_dsi_phy *phy;
  525. struct device *dev = &pdev->dev;
  526. u32 phy_type;
  527. int ret;
  528. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  529. if (!phy)
  530. return -ENOMEM;
  531. phy->provided_clocks = devm_kzalloc(dev,
  532. struct_size(phy->provided_clocks, hws, NUM_PROVIDED_CLKS),
  533. GFP_KERNEL);
  534. if (!phy->provided_clocks)
  535. return -ENOMEM;
  536. phy->provided_clocks->num = NUM_PROVIDED_CLKS;
  537. phy->cfg = of_device_get_match_data(&pdev->dev);
  538. if (!phy->cfg)
  539. return -ENODEV;
  540. phy->pdev = pdev;
  541. phy->id = dsi_phy_get_id(phy);
  542. if (phy->id < 0)
  543. return dev_err_probe(dev, phy->id,
  544. "Couldn't identify PHY index\n");
  545. phy->regulator_ldo_mode = of_property_read_bool(dev->of_node,
  546. "qcom,dsi-phy-regulator-ldo-mode");
  547. if (!of_property_read_u32(dev->of_node, "phy-type", &phy_type))
  548. phy->cphy_mode = (phy_type == PHY_TYPE_CPHY);
  549. phy->base = msm_ioremap_size(pdev, "dsi_phy", &phy->base_size);
  550. if (IS_ERR(phy->base))
  551. return dev_err_probe(dev, PTR_ERR(phy->base),
  552. "Failed to map phy base\n");
  553. phy->pll_base = msm_ioremap_size(pdev, "dsi_pll", &phy->pll_size);
  554. if (IS_ERR(phy->pll_base))
  555. return dev_err_probe(dev, PTR_ERR(phy->pll_base),
  556. "Failed to map pll base\n");
  557. if (phy->cfg->has_phy_lane) {
  558. phy->lane_base = msm_ioremap_size(pdev, "dsi_phy_lane", &phy->lane_size);
  559. if (IS_ERR(phy->lane_base))
  560. return dev_err_probe(dev, PTR_ERR(phy->lane_base),
  561. "Failed to map phy lane base\n");
  562. }
  563. if (phy->cfg->has_phy_regulator) {
  564. phy->reg_base = msm_ioremap_size(pdev, "dsi_phy_regulator", &phy->reg_size);
  565. if (IS_ERR(phy->reg_base))
  566. return dev_err_probe(dev, PTR_ERR(phy->reg_base),
  567. "Failed to map phy regulator base\n");
  568. }
  569. if (phy->cfg->ops.parse_dt_properties) {
  570. ret = phy->cfg->ops.parse_dt_properties(phy);
  571. if (ret)
  572. return ret;
  573. }
  574. ret = devm_regulator_bulk_get_const(dev, phy->cfg->num_regulators,
  575. phy->cfg->regulator_data,
  576. &phy->supplies);
  577. if (ret)
  578. return ret;
  579. phy->ahb_clk = msm_clk_get(pdev, "iface");
  580. if (IS_ERR(phy->ahb_clk))
  581. return dev_err_probe(dev, PTR_ERR(phy->ahb_clk),
  582. "Unable to get ahb clk\n");
  583. ret = devm_pm_runtime_enable(&pdev->dev);
  584. if (ret)
  585. return ret;
  586. /* PLL init will call into clk_register which requires
  587. * register access, so we need to enable power and ahb clock.
  588. */
  589. ret = dsi_phy_enable_resource(phy);
  590. if (ret)
  591. return ret;
  592. if (phy->cfg->ops.pll_init) {
  593. ret = phy->cfg->ops.pll_init(phy);
  594. if (ret)
  595. return dev_err_probe(dev, ret,
  596. "PLL init failed; need separate clk driver\n");
  597. }
  598. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
  599. phy->provided_clocks);
  600. if (ret)
  601. return dev_err_probe(dev, ret,
  602. "Failed to register clk provider\n");
  603. dsi_phy_disable_resource(phy);
  604. platform_set_drvdata(pdev, phy);
  605. return 0;
  606. }
  607. static struct platform_driver dsi_phy_platform_driver = {
  608. .probe = dsi_phy_driver_probe,
  609. .driver = {
  610. .name = "msm_dsi_phy",
  611. .of_match_table = dsi_phy_dt_match,
  612. },
  613. };
  614. void __init msm_dsi_phy_driver_register(void)
  615. {
  616. platform_driver_register(&dsi_phy_platform_driver);
  617. }
  618. void __exit msm_dsi_phy_driver_unregister(void)
  619. {
  620. platform_driver_unregister(&dsi_phy_platform_driver);
  621. }
  622. int msm_dsi_phy_enable(struct msm_dsi_phy *phy,
  623. struct msm_dsi_phy_clk_request *clk_req,
  624. struct msm_dsi_phy_shared_timings *shared_timings)
  625. {
  626. struct device *dev;
  627. int ret;
  628. if (!phy || !phy->cfg->ops.enable)
  629. return -EINVAL;
  630. dev = &phy->pdev->dev;
  631. ret = dsi_phy_enable_resource(phy);
  632. if (ret) {
  633. DRM_DEV_ERROR(dev, "%s: resource enable failed, %d\n",
  634. __func__, ret);
  635. goto res_en_fail;
  636. }
  637. ret = regulator_bulk_enable(phy->cfg->num_regulators, phy->supplies);
  638. if (ret) {
  639. DRM_DEV_ERROR(dev, "%s: regulator enable failed, %d\n",
  640. __func__, ret);
  641. goto reg_en_fail;
  642. }
  643. ret = phy->cfg->ops.enable(phy, clk_req);
  644. if (ret) {
  645. DRM_DEV_ERROR(dev, "%s: phy enable failed, %d\n", __func__, ret);
  646. goto phy_en_fail;
  647. }
  648. memcpy(shared_timings, &phy->timing.shared_timings,
  649. sizeof(*shared_timings));
  650. /*
  651. * Resetting DSI PHY silently changes its PLL registers to reset status,
  652. * which will confuse clock driver and result in wrong output rate of
  653. * link clocks. Restore PLL status if its PLL is being used as clock
  654. * source.
  655. */
  656. if (phy->usecase != MSM_DSI_PHY_SLAVE) {
  657. ret = msm_dsi_phy_pll_restore_state(phy);
  658. if (ret) {
  659. DRM_DEV_ERROR(dev, "%s: failed to restore phy state, %d\n",
  660. __func__, ret);
  661. goto pll_restor_fail;
  662. }
  663. }
  664. return 0;
  665. pll_restor_fail:
  666. if (phy->cfg->ops.disable)
  667. phy->cfg->ops.disable(phy);
  668. phy_en_fail:
  669. regulator_bulk_disable(phy->cfg->num_regulators, phy->supplies);
  670. reg_en_fail:
  671. dsi_phy_disable_resource(phy);
  672. res_en_fail:
  673. return ret;
  674. }
  675. void msm_dsi_phy_disable(struct msm_dsi_phy *phy)
  676. {
  677. if (!phy || !phy->cfg->ops.disable)
  678. return;
  679. phy->cfg->ops.disable(phy);
  680. regulator_bulk_disable(phy->cfg->num_regulators, phy->supplies);
  681. dsi_phy_disable_resource(phy);
  682. }
  683. void msm_dsi_phy_set_usecase(struct msm_dsi_phy *phy,
  684. enum msm_dsi_phy_usecase uc)
  685. {
  686. if (phy)
  687. phy->usecase = uc;
  688. }
  689. /* Returns true if we have to clear DSI_LANE_CTRL.HS_REQ_SEL_PHY */
  690. bool msm_dsi_phy_set_continuous_clock(struct msm_dsi_phy *phy, bool enable)
  691. {
  692. if (!phy || !phy->cfg->ops.set_continuous_clock)
  693. return false;
  694. return phy->cfg->ops.set_continuous_clock(phy, enable);
  695. }
  696. void msm_dsi_phy_pll_save_state(struct msm_dsi_phy *phy)
  697. {
  698. if (phy->cfg->ops.save_pll_state) {
  699. phy->cfg->ops.save_pll_state(phy);
  700. phy->state_saved = true;
  701. }
  702. }
  703. int msm_dsi_phy_pll_restore_state(struct msm_dsi_phy *phy)
  704. {
  705. int ret;
  706. if (phy->cfg->ops.restore_pll_state && phy->state_saved) {
  707. ret = phy->cfg->ops.restore_pll_state(phy);
  708. if (ret)
  709. return ret;
  710. phy->state_saved = false;
  711. }
  712. return 0;
  713. }
  714. void msm_dsi_phy_snapshot(struct msm_disp_state *disp_state, struct msm_dsi_phy *phy)
  715. {
  716. msm_disp_snapshot_add_block(disp_state,
  717. phy->base_size, phy->base,
  718. "dsi%d_phy", phy->id);
  719. /* Do not try accessing PLL registers if it is switched off */
  720. if (phy->pll_on)
  721. msm_disp_snapshot_add_block(disp_state,
  722. phy->pll_size, phy->pll_base,
  723. "dsi%d_pll", phy->id);
  724. if (phy->lane_base)
  725. msm_disp_snapshot_add_block(disp_state,
  726. phy->lane_size, phy->lane_base,
  727. "dsi%d_lane", phy->id);
  728. if (phy->reg_base)
  729. msm_disp_snapshot_add_block(disp_state,
  730. phy->reg_size, phy->reg_base,
  731. "dsi%d_reg", phy->id);
  732. }