dsi_phy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/platform_device.h>
  14. #include "dsi_phy.h"
  15. #define S_DIV_ROUND_UP(n, d) \
  16. (((n) >= 0) ? (((n) + (d) - 1) / (d)) : (((n) - (d) + 1) / (d)))
  17. static inline s32 linear_inter(s32 tmax, s32 tmin, s32 percent,
  18. s32 min_result, bool even)
  19. {
  20. s32 v;
  21. v = (tmax - tmin) * percent;
  22. v = S_DIV_ROUND_UP(v, 100) + tmin;
  23. if (even && (v & 0x1))
  24. return max_t(s32, min_result, v - 1);
  25. else
  26. return max_t(s32, min_result, v);
  27. }
  28. static void dsi_dphy_timing_calc_clk_zero(struct msm_dsi_dphy_timing *timing,
  29. s32 ui, s32 coeff, s32 pcnt)
  30. {
  31. s32 tmax, tmin, clk_z;
  32. s32 temp;
  33. /* reset */
  34. temp = 300 * coeff - ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  35. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  36. if (tmin > 255) {
  37. tmax = 511;
  38. clk_z = linear_inter(2 * tmin, tmin, pcnt, 0, true);
  39. } else {
  40. tmax = 255;
  41. clk_z = linear_inter(tmax, tmin, pcnt, 0, true);
  42. }
  43. /* adjust */
  44. temp = (timing->hs_rqst + timing->clk_prepare + clk_z) & 0x7;
  45. timing->clk_zero = clk_z + 8 - temp;
  46. }
  47. int msm_dsi_dphy_timing_calc(struct msm_dsi_dphy_timing *timing,
  48. struct msm_dsi_phy_clk_request *clk_req)
  49. {
  50. const unsigned long bit_rate = clk_req->bitclk_rate;
  51. const unsigned long esc_rate = clk_req->escclk_rate;
  52. s32 ui, lpx;
  53. s32 tmax, tmin;
  54. s32 pcnt0 = 10;
  55. s32 pcnt1 = (bit_rate > 1200000000) ? 15 : 10;
  56. s32 pcnt2 = 10;
  57. s32 pcnt3 = (bit_rate > 180000000) ? 10 : 40;
  58. s32 coeff = 1000; /* Precision, should avoid overflow */
  59. s32 temp;
  60. if (!bit_rate || !esc_rate)
  61. return -EINVAL;
  62. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  63. lpx = mult_frac(NSEC_PER_MSEC, coeff, esc_rate / 1000);
  64. tmax = S_DIV_ROUND_UP(95 * coeff, ui) - 2;
  65. tmin = S_DIV_ROUND_UP(38 * coeff, ui) - 2;
  66. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, true);
  67. temp = lpx / ui;
  68. if (temp & 0x1)
  69. timing->hs_rqst = temp;
  70. else
  71. timing->hs_rqst = max_t(s32, 0, temp - 2);
  72. /* Calculate clk_zero after clk_prepare and hs_rqst */
  73. dsi_dphy_timing_calc_clk_zero(timing, ui, coeff, pcnt2);
  74. temp = 105 * coeff + 12 * ui - 20 * coeff;
  75. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  76. tmin = S_DIV_ROUND_UP(60 * coeff, ui) - 2;
  77. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  78. temp = 85 * coeff + 6 * ui;
  79. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  80. temp = 40 * coeff + 4 * ui;
  81. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  82. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, true);
  83. tmax = 255;
  84. temp = ((timing->hs_prepare >> 1) + 1) * 2 * ui + 2 * ui;
  85. temp = 145 * coeff + 10 * ui - temp;
  86. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  87. timing->hs_zero = linear_inter(tmax, tmin, pcnt2, 24, true);
  88. temp = 105 * coeff + 12 * ui - 20 * coeff;
  89. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  90. temp = 60 * coeff + 4 * ui;
  91. tmin = DIV_ROUND_UP(temp, ui) - 2;
  92. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  93. tmax = 255;
  94. tmin = S_DIV_ROUND_UP(100 * coeff, ui) - 2;
  95. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, true);
  96. tmax = 63;
  97. temp = ((timing->hs_exit >> 1) + 1) * 2 * ui;
  98. temp = 60 * coeff + 52 * ui - 24 * ui - temp;
  99. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  100. timing->shared_timings.clk_post = linear_inter(tmax, tmin, pcnt2, 0,
  101. false);
  102. tmax = 63;
  103. temp = ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  104. temp += ((timing->clk_zero >> 1) + 1) * 2 * ui;
  105. temp += 8 * ui + lpx;
  106. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  107. if (tmin > tmax) {
  108. temp = linear_inter(2 * tmax, tmin, pcnt2, 0, false);
  109. timing->shared_timings.clk_pre = temp >> 1;
  110. timing->shared_timings.clk_pre_inc_by_2 = true;
  111. } else {
  112. timing->shared_timings.clk_pre =
  113. linear_inter(tmax, tmin, pcnt2, 0, false);
  114. timing->shared_timings.clk_pre_inc_by_2 = false;
  115. }
  116. timing->ta_go = 3;
  117. timing->ta_sure = 0;
  118. timing->ta_get = 4;
  119. DBG("PHY timings: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  120. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  121. timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
  122. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  123. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  124. timing->hs_rqst);
  125. return 0;
  126. }
  127. int msm_dsi_dphy_timing_calc_v2(struct msm_dsi_dphy_timing *timing,
  128. struct msm_dsi_phy_clk_request *clk_req)
  129. {
  130. const unsigned long bit_rate = clk_req->bitclk_rate;
  131. const unsigned long esc_rate = clk_req->escclk_rate;
  132. s32 ui, ui_x8, lpx;
  133. s32 tmax, tmin;
  134. s32 pcnt0 = 50;
  135. s32 pcnt1 = 50;
  136. s32 pcnt2 = 10;
  137. s32 pcnt3 = 30;
  138. s32 pcnt4 = 10;
  139. s32 pcnt5 = 2;
  140. s32 coeff = 1000; /* Precision, should avoid overflow */
  141. s32 hb_en, hb_en_ckln, pd_ckln, pd;
  142. s32 val, val_ckln;
  143. s32 temp;
  144. if (!bit_rate || !esc_rate)
  145. return -EINVAL;
  146. timing->hs_halfbyte_en = 0;
  147. hb_en = 0;
  148. timing->hs_halfbyte_en_ckln = 0;
  149. hb_en_ckln = 0;
  150. timing->hs_prep_dly_ckln = (bit_rate > 100000000) ? 0 : 3;
  151. pd_ckln = timing->hs_prep_dly_ckln;
  152. timing->hs_prep_dly = (bit_rate > 120000000) ? 0 : 1;
  153. pd = timing->hs_prep_dly;
  154. val = (hb_en << 2) + (pd << 1);
  155. val_ckln = (hb_en_ckln << 2) + (pd_ckln << 1);
  156. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  157. ui_x8 = ui << 3;
  158. lpx = mult_frac(NSEC_PER_MSEC, coeff, esc_rate / 1000);
  159. temp = S_DIV_ROUND_UP(38 * coeff - val_ckln * ui, ui_x8);
  160. tmin = max_t(s32, temp, 0);
  161. temp = (95 * coeff - val_ckln * ui) / ui_x8;
  162. tmax = max_t(s32, temp, 0);
  163. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, false);
  164. temp = 300 * coeff - ((timing->clk_prepare << 3) + val_ckln) * ui;
  165. tmin = S_DIV_ROUND_UP(temp - 11 * ui, ui_x8) - 3;
  166. tmax = (tmin > 255) ? 511 : 255;
  167. timing->clk_zero = linear_inter(tmax, tmin, pcnt5, 0, false);
  168. tmin = DIV_ROUND_UP(60 * coeff + 3 * ui, ui_x8);
  169. temp = 105 * coeff + 12 * ui - 20 * coeff;
  170. tmax = (temp + 3 * ui) / ui_x8;
  171. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
  172. temp = S_DIV_ROUND_UP(40 * coeff + 4 * ui - val * ui, ui_x8);
  173. tmin = max_t(s32, temp, 0);
  174. temp = (85 * coeff + 6 * ui - val * ui) / ui_x8;
  175. tmax = max_t(s32, temp, 0);
  176. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, false);
  177. temp = 145 * coeff + 10 * ui - ((timing->hs_prepare << 3) + val) * ui;
  178. tmin = S_DIV_ROUND_UP(temp - 11 * ui, ui_x8) - 3;
  179. tmax = 255;
  180. timing->hs_zero = linear_inter(tmax, tmin, pcnt4, 0, false);
  181. tmin = DIV_ROUND_UP(60 * coeff + 4 * ui + 3 * ui, ui_x8);
  182. temp = 105 * coeff + 12 * ui - 20 * coeff;
  183. tmax = (temp + 3 * ui) / ui_x8;
  184. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
  185. temp = 50 * coeff + ((hb_en << 2) - 8) * ui;
  186. timing->hs_rqst = S_DIV_ROUND_UP(temp, ui_x8);
  187. tmin = DIV_ROUND_UP(100 * coeff, ui_x8) - 1;
  188. tmax = 255;
  189. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, false);
  190. temp = 50 * coeff + ((hb_en_ckln << 2) - 8) * ui;
  191. timing->hs_rqst_ckln = S_DIV_ROUND_UP(temp, ui_x8);
  192. temp = 60 * coeff + 52 * ui - 43 * ui;
  193. tmin = DIV_ROUND_UP(temp, ui_x8) - 1;
  194. tmax = 63;
  195. timing->shared_timings.clk_post =
  196. linear_inter(tmax, tmin, pcnt2, 0, false);
  197. temp = 8 * ui + ((timing->clk_prepare << 3) + val_ckln) * ui;
  198. temp += (((timing->clk_zero + 3) << 3) + 11 - (pd_ckln << 1)) * ui;
  199. temp += hb_en_ckln ? (((timing->hs_rqst_ckln << 3) + 4) * ui) :
  200. (((timing->hs_rqst_ckln << 3) + 8) * ui);
  201. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  202. tmax = 63;
  203. if (tmin > tmax) {
  204. temp = linear_inter(tmax << 1, tmin, pcnt2, 0, false);
  205. timing->shared_timings.clk_pre = temp >> 1;
  206. timing->shared_timings.clk_pre_inc_by_2 = 1;
  207. } else {
  208. timing->shared_timings.clk_pre =
  209. linear_inter(tmax, tmin, pcnt2, 0, false);
  210. timing->shared_timings.clk_pre_inc_by_2 = 0;
  211. }
  212. timing->ta_go = 3;
  213. timing->ta_sure = 0;
  214. timing->ta_get = 4;
  215. DBG("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  216. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  217. timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
  218. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  219. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  220. timing->hs_rqst, timing->hs_rqst_ckln, timing->hs_halfbyte_en,
  221. timing->hs_halfbyte_en_ckln, timing->hs_prep_dly,
  222. timing->hs_prep_dly_ckln);
  223. return 0;
  224. }
  225. int msm_dsi_dphy_timing_calc_v3(struct msm_dsi_dphy_timing *timing,
  226. struct msm_dsi_phy_clk_request *clk_req)
  227. {
  228. const unsigned long bit_rate = clk_req->bitclk_rate;
  229. const unsigned long esc_rate = clk_req->escclk_rate;
  230. s32 ui, ui_x8, lpx;
  231. s32 tmax, tmin;
  232. s32 pcnt0 = 50;
  233. s32 pcnt1 = 50;
  234. s32 pcnt2 = 10;
  235. s32 pcnt3 = 30;
  236. s32 pcnt4 = 10;
  237. s32 pcnt5 = 2;
  238. s32 coeff = 1000; /* Precision, should avoid overflow */
  239. s32 hb_en, hb_en_ckln;
  240. s32 temp;
  241. if (!bit_rate || !esc_rate)
  242. return -EINVAL;
  243. timing->hs_halfbyte_en = 0;
  244. hb_en = 0;
  245. timing->hs_halfbyte_en_ckln = 0;
  246. hb_en_ckln = 0;
  247. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  248. ui_x8 = ui << 3;
  249. lpx = mult_frac(NSEC_PER_MSEC, coeff, esc_rate / 1000);
  250. temp = S_DIV_ROUND_UP(38 * coeff, ui_x8);
  251. tmin = max_t(s32, temp, 0);
  252. temp = (95 * coeff) / ui_x8;
  253. tmax = max_t(s32, temp, 0);
  254. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, false);
  255. temp = 300 * coeff - (timing->clk_prepare << 3) * ui;
  256. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  257. tmax = (tmin > 255) ? 511 : 255;
  258. timing->clk_zero = linear_inter(tmax, tmin, pcnt5, 0, false);
  259. tmin = DIV_ROUND_UP(60 * coeff + 3 * ui, ui_x8);
  260. temp = 105 * coeff + 12 * ui - 20 * coeff;
  261. tmax = (temp + 3 * ui) / ui_x8;
  262. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
  263. temp = S_DIV_ROUND_UP(40 * coeff + 4 * ui, ui_x8);
  264. tmin = max_t(s32, temp, 0);
  265. temp = (85 * coeff + 6 * ui) / ui_x8;
  266. tmax = max_t(s32, temp, 0);
  267. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, false);
  268. temp = 145 * coeff + 10 * ui - (timing->hs_prepare << 3) * ui;
  269. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  270. tmax = 255;
  271. timing->hs_zero = linear_inter(tmax, tmin, pcnt4, 0, false);
  272. tmin = DIV_ROUND_UP(60 * coeff + 4 * ui, ui_x8) - 1;
  273. temp = 105 * coeff + 12 * ui - 20 * coeff;
  274. tmax = (temp / ui_x8) - 1;
  275. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, false);
  276. temp = 50 * coeff + ((hb_en << 2) - 8) * ui;
  277. timing->hs_rqst = S_DIV_ROUND_UP(temp, ui_x8);
  278. tmin = DIV_ROUND_UP(100 * coeff, ui_x8) - 1;
  279. tmax = 255;
  280. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, false);
  281. temp = 50 * coeff + ((hb_en_ckln << 2) - 8) * ui;
  282. timing->hs_rqst_ckln = S_DIV_ROUND_UP(temp, ui_x8);
  283. temp = 60 * coeff + 52 * ui - 43 * ui;
  284. tmin = DIV_ROUND_UP(temp, ui_x8) - 1;
  285. tmax = 63;
  286. timing->shared_timings.clk_post =
  287. linear_inter(tmax, tmin, pcnt2, 0, false);
  288. temp = 8 * ui + (timing->clk_prepare << 3) * ui;
  289. temp += (((timing->clk_zero + 3) << 3) + 11) * ui;
  290. temp += hb_en_ckln ? (((timing->hs_rqst_ckln << 3) + 4) * ui) :
  291. (((timing->hs_rqst_ckln << 3) + 8) * ui);
  292. tmin = S_DIV_ROUND_UP(temp, ui_x8) - 1;
  293. tmax = 63;
  294. if (tmin > tmax) {
  295. temp = linear_inter(tmax << 1, tmin, pcnt2, 0, false);
  296. timing->shared_timings.clk_pre = temp >> 1;
  297. timing->shared_timings.clk_pre_inc_by_2 = 1;
  298. } else {
  299. timing->shared_timings.clk_pre =
  300. linear_inter(tmax, tmin, pcnt2, 0, false);
  301. timing->shared_timings.clk_pre_inc_by_2 = 0;
  302. }
  303. timing->ta_go = 3;
  304. timing->ta_sure = 0;
  305. timing->ta_get = 4;
  306. DBG("%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  307. timing->shared_timings.clk_pre, timing->shared_timings.clk_post,
  308. timing->shared_timings.clk_pre_inc_by_2, timing->clk_zero,
  309. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  310. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  311. timing->hs_rqst, timing->hs_rqst_ckln, timing->hs_halfbyte_en,
  312. timing->hs_halfbyte_en_ckln, timing->hs_prep_dly,
  313. timing->hs_prep_dly_ckln);
  314. return 0;
  315. }
  316. void msm_dsi_phy_set_src_pll(struct msm_dsi_phy *phy, int pll_id, u32 reg,
  317. u32 bit_mask)
  318. {
  319. int phy_id = phy->id;
  320. u32 val;
  321. if ((phy_id >= DSI_MAX) || (pll_id >= DSI_MAX))
  322. return;
  323. val = dsi_phy_read(phy->base + reg);
  324. if (phy->cfg->src_pll_truthtable[phy_id][pll_id])
  325. dsi_phy_write(phy->base + reg, val | bit_mask);
  326. else
  327. dsi_phy_write(phy->base + reg, val & (~bit_mask));
  328. }
  329. static int dsi_phy_regulator_init(struct msm_dsi_phy *phy)
  330. {
  331. struct regulator_bulk_data *s = phy->supplies;
  332. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  333. struct device *dev = &phy->pdev->dev;
  334. int num = phy->cfg->reg_cfg.num;
  335. int i, ret;
  336. for (i = 0; i < num; i++)
  337. s[i].supply = regs[i].name;
  338. ret = devm_regulator_bulk_get(dev, num, s);
  339. if (ret < 0) {
  340. dev_err(dev, "%s: failed to init regulator, ret=%d\n",
  341. __func__, ret);
  342. return ret;
  343. }
  344. return 0;
  345. }
  346. static void dsi_phy_regulator_disable(struct msm_dsi_phy *phy)
  347. {
  348. struct regulator_bulk_data *s = phy->supplies;
  349. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  350. int num = phy->cfg->reg_cfg.num;
  351. int i;
  352. DBG("");
  353. for (i = num - 1; i >= 0; i--)
  354. if (regs[i].disable_load >= 0)
  355. regulator_set_load(s[i].consumer, regs[i].disable_load);
  356. regulator_bulk_disable(num, s);
  357. }
  358. static int dsi_phy_regulator_enable(struct msm_dsi_phy *phy)
  359. {
  360. struct regulator_bulk_data *s = phy->supplies;
  361. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  362. struct device *dev = &phy->pdev->dev;
  363. int num = phy->cfg->reg_cfg.num;
  364. int ret, i;
  365. DBG("");
  366. for (i = 0; i < num; i++) {
  367. if (regs[i].enable_load >= 0) {
  368. ret = regulator_set_load(s[i].consumer,
  369. regs[i].enable_load);
  370. if (ret < 0) {
  371. dev_err(dev,
  372. "regulator %d set op mode failed, %d\n",
  373. i, ret);
  374. goto fail;
  375. }
  376. }
  377. }
  378. ret = regulator_bulk_enable(num, s);
  379. if (ret < 0) {
  380. dev_err(dev, "regulator enable failed, %d\n", ret);
  381. goto fail;
  382. }
  383. return 0;
  384. fail:
  385. for (i--; i >= 0; i--)
  386. regulator_set_load(s[i].consumer, regs[i].disable_load);
  387. return ret;
  388. }
  389. static int dsi_phy_enable_resource(struct msm_dsi_phy *phy)
  390. {
  391. struct device *dev = &phy->pdev->dev;
  392. int ret;
  393. pm_runtime_get_sync(dev);
  394. ret = clk_prepare_enable(phy->ahb_clk);
  395. if (ret) {
  396. dev_err(dev, "%s: can't enable ahb clk, %d\n", __func__, ret);
  397. pm_runtime_put_sync(dev);
  398. }
  399. return ret;
  400. }
  401. static void dsi_phy_disable_resource(struct msm_dsi_phy *phy)
  402. {
  403. clk_disable_unprepare(phy->ahb_clk);
  404. pm_runtime_put_autosuspend(&phy->pdev->dev);
  405. }
  406. static const struct of_device_id dsi_phy_dt_match[] = {
  407. #ifdef CONFIG_DRM_MSM_DSI_28NM_PHY
  408. { .compatible = "qcom,dsi-phy-28nm-hpm",
  409. .data = &dsi_phy_28nm_hpm_cfgs },
  410. { .compatible = "qcom,dsi-phy-28nm-lp",
  411. .data = &dsi_phy_28nm_lp_cfgs },
  412. #endif
  413. #ifdef CONFIG_DRM_MSM_DSI_20NM_PHY
  414. { .compatible = "qcom,dsi-phy-20nm",
  415. .data = &dsi_phy_20nm_cfgs },
  416. #endif
  417. #ifdef CONFIG_DRM_MSM_DSI_28NM_8960_PHY
  418. { .compatible = "qcom,dsi-phy-28nm-8960",
  419. .data = &dsi_phy_28nm_8960_cfgs },
  420. #endif
  421. #ifdef CONFIG_DRM_MSM_DSI_14NM_PHY
  422. { .compatible = "qcom,dsi-phy-14nm",
  423. .data = &dsi_phy_14nm_cfgs },
  424. #endif
  425. #ifdef CONFIG_DRM_MSM_DSI_10NM_PHY
  426. { .compatible = "qcom,dsi-phy-10nm",
  427. .data = &dsi_phy_10nm_cfgs },
  428. #endif
  429. {}
  430. };
  431. /*
  432. * Currently, we only support one SoC for each PHY type. When we have multiple
  433. * SoCs for the same PHY, we can try to make the index searching a bit more
  434. * clever.
  435. */
  436. static int dsi_phy_get_id(struct msm_dsi_phy *phy)
  437. {
  438. struct platform_device *pdev = phy->pdev;
  439. const struct msm_dsi_phy_cfg *cfg = phy->cfg;
  440. struct resource *res;
  441. int i;
  442. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_phy");
  443. if (!res)
  444. return -EINVAL;
  445. for (i = 0; i < cfg->num_dsi_phy; i++) {
  446. if (cfg->io_start[i] == res->start)
  447. return i;
  448. }
  449. return -EINVAL;
  450. }
  451. int msm_dsi_phy_init_common(struct msm_dsi_phy *phy)
  452. {
  453. struct platform_device *pdev = phy->pdev;
  454. int ret = 0;
  455. phy->reg_base = msm_ioremap(pdev, "dsi_phy_regulator",
  456. "DSI_PHY_REG");
  457. if (IS_ERR(phy->reg_base)) {
  458. dev_err(&pdev->dev, "%s: failed to map phy regulator base\n",
  459. __func__);
  460. ret = -ENOMEM;
  461. goto fail;
  462. }
  463. fail:
  464. return ret;
  465. }
  466. static int dsi_phy_driver_probe(struct platform_device *pdev)
  467. {
  468. struct msm_dsi_phy *phy;
  469. struct device *dev = &pdev->dev;
  470. const struct of_device_id *match;
  471. int ret;
  472. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  473. if (!phy)
  474. return -ENOMEM;
  475. match = of_match_node(dsi_phy_dt_match, dev->of_node);
  476. if (!match)
  477. return -ENODEV;
  478. phy->cfg = match->data;
  479. phy->pdev = pdev;
  480. phy->id = dsi_phy_get_id(phy);
  481. if (phy->id < 0) {
  482. ret = phy->id;
  483. dev_err(dev, "%s: couldn't identify PHY index, %d\n",
  484. __func__, ret);
  485. goto fail;
  486. }
  487. phy->regulator_ldo_mode = of_property_read_bool(dev->of_node,
  488. "qcom,dsi-phy-regulator-ldo-mode");
  489. phy->base = msm_ioremap(pdev, "dsi_phy", "DSI_PHY");
  490. if (IS_ERR(phy->base)) {
  491. dev_err(dev, "%s: failed to map phy base\n", __func__);
  492. ret = -ENOMEM;
  493. goto fail;
  494. }
  495. ret = dsi_phy_regulator_init(phy);
  496. if (ret) {
  497. dev_err(dev, "%s: failed to init regulator\n", __func__);
  498. goto fail;
  499. }
  500. phy->ahb_clk = msm_clk_get(pdev, "iface");
  501. if (IS_ERR(phy->ahb_clk)) {
  502. dev_err(dev, "%s: Unable to get ahb clk\n", __func__);
  503. ret = PTR_ERR(phy->ahb_clk);
  504. goto fail;
  505. }
  506. if (phy->cfg->ops.init) {
  507. ret = phy->cfg->ops.init(phy);
  508. if (ret)
  509. goto fail;
  510. }
  511. /* PLL init will call into clk_register which requires
  512. * register access, so we need to enable power and ahb clock.
  513. */
  514. ret = dsi_phy_enable_resource(phy);
  515. if (ret)
  516. goto fail;
  517. phy->pll = msm_dsi_pll_init(pdev, phy->cfg->type, phy->id);
  518. if (IS_ERR_OR_NULL(phy->pll))
  519. dev_info(dev,
  520. "%s: pll init failed: %ld, need separate pll clk driver\n",
  521. __func__, PTR_ERR(phy->pll));
  522. dsi_phy_disable_resource(phy);
  523. platform_set_drvdata(pdev, phy);
  524. return 0;
  525. fail:
  526. return ret;
  527. }
  528. static int dsi_phy_driver_remove(struct platform_device *pdev)
  529. {
  530. struct msm_dsi_phy *phy = platform_get_drvdata(pdev);
  531. if (phy && phy->pll) {
  532. msm_dsi_pll_destroy(phy->pll);
  533. phy->pll = NULL;
  534. }
  535. platform_set_drvdata(pdev, NULL);
  536. return 0;
  537. }
  538. static struct platform_driver dsi_phy_platform_driver = {
  539. .probe = dsi_phy_driver_probe,
  540. .remove = dsi_phy_driver_remove,
  541. .driver = {
  542. .name = "msm_dsi_phy",
  543. .of_match_table = dsi_phy_dt_match,
  544. },
  545. };
  546. void __init msm_dsi_phy_driver_register(void)
  547. {
  548. platform_driver_register(&dsi_phy_platform_driver);
  549. }
  550. void __exit msm_dsi_phy_driver_unregister(void)
  551. {
  552. platform_driver_unregister(&dsi_phy_platform_driver);
  553. }
  554. int msm_dsi_phy_enable(struct msm_dsi_phy *phy, int src_pll_id,
  555. struct msm_dsi_phy_clk_request *clk_req)
  556. {
  557. struct device *dev = &phy->pdev->dev;
  558. int ret;
  559. if (!phy || !phy->cfg->ops.enable)
  560. return -EINVAL;
  561. ret = dsi_phy_enable_resource(phy);
  562. if (ret) {
  563. dev_err(dev, "%s: resource enable failed, %d\n",
  564. __func__, ret);
  565. goto res_en_fail;
  566. }
  567. ret = dsi_phy_regulator_enable(phy);
  568. if (ret) {
  569. dev_err(dev, "%s: regulator enable failed, %d\n",
  570. __func__, ret);
  571. goto reg_en_fail;
  572. }
  573. ret = phy->cfg->ops.enable(phy, src_pll_id, clk_req);
  574. if (ret) {
  575. dev_err(dev, "%s: phy enable failed, %d\n", __func__, ret);
  576. goto phy_en_fail;
  577. }
  578. /*
  579. * Resetting DSI PHY silently changes its PLL registers to reset status,
  580. * which will confuse clock driver and result in wrong output rate of
  581. * link clocks. Restore PLL status if its PLL is being used as clock
  582. * source.
  583. */
  584. if (phy->usecase != MSM_DSI_PHY_SLAVE) {
  585. ret = msm_dsi_pll_restore_state(phy->pll);
  586. if (ret) {
  587. dev_err(dev, "%s: failed to restore pll state, %d\n",
  588. __func__, ret);
  589. goto pll_restor_fail;
  590. }
  591. }
  592. return 0;
  593. pll_restor_fail:
  594. if (phy->cfg->ops.disable)
  595. phy->cfg->ops.disable(phy);
  596. phy_en_fail:
  597. dsi_phy_regulator_disable(phy);
  598. reg_en_fail:
  599. dsi_phy_disable_resource(phy);
  600. res_en_fail:
  601. return ret;
  602. }
  603. void msm_dsi_phy_disable(struct msm_dsi_phy *phy)
  604. {
  605. if (!phy || !phy->cfg->ops.disable)
  606. return;
  607. /* Save PLL status if it is a clock source */
  608. if (phy->usecase != MSM_DSI_PHY_SLAVE)
  609. msm_dsi_pll_save_state(phy->pll);
  610. phy->cfg->ops.disable(phy);
  611. dsi_phy_regulator_disable(phy);
  612. dsi_phy_disable_resource(phy);
  613. }
  614. void msm_dsi_phy_get_shared_timings(struct msm_dsi_phy *phy,
  615. struct msm_dsi_phy_shared_timings *shared_timings)
  616. {
  617. memcpy(shared_timings, &phy->timing.shared_timings,
  618. sizeof(*shared_timings));
  619. }
  620. struct msm_dsi_pll *msm_dsi_phy_get_pll(struct msm_dsi_phy *phy)
  621. {
  622. if (!phy)
  623. return NULL;
  624. return phy->pll;
  625. }
  626. void msm_dsi_phy_set_usecase(struct msm_dsi_phy *phy,
  627. enum msm_dsi_phy_usecase uc)
  628. {
  629. if (phy)
  630. phy->usecase = uc;
  631. }