dsi_phy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 <linux/regulator/consumer.h>
  15. #include "dsi.h"
  16. #include "dsi.xml.h"
  17. #define dsi_phy_read(offset) msm_readl((offset))
  18. #define dsi_phy_write(offset, data) msm_writel((data), (offset))
  19. struct dsi_phy_ops {
  20. int (*enable)(struct msm_dsi_phy *phy, int src_pll_id,
  21. const unsigned long bit_rate, const unsigned long esc_rate);
  22. int (*disable)(struct msm_dsi_phy *phy);
  23. };
  24. struct dsi_phy_cfg {
  25. enum msm_dsi_phy_type type;
  26. struct dsi_reg_config reg_cfg;
  27. struct dsi_phy_ops ops;
  28. /* Each cell {phy_id, pll_id} of the truth table indicates
  29. * if the source PLL is on the right side of the PHY.
  30. * Fill default H/W values in illegal cells, eg. cell {0, 1}.
  31. */
  32. bool src_pll_truthtable[DSI_MAX][DSI_MAX];
  33. };
  34. struct dsi_dphy_timing {
  35. u32 clk_pre;
  36. u32 clk_post;
  37. u32 clk_zero;
  38. u32 clk_trail;
  39. u32 clk_prepare;
  40. u32 hs_exit;
  41. u32 hs_zero;
  42. u32 hs_prepare;
  43. u32 hs_trail;
  44. u32 hs_rqst;
  45. u32 ta_go;
  46. u32 ta_sure;
  47. u32 ta_get;
  48. };
  49. struct msm_dsi_phy {
  50. struct platform_device *pdev;
  51. void __iomem *base;
  52. void __iomem *reg_base;
  53. int id;
  54. struct clk *ahb_clk;
  55. struct regulator_bulk_data supplies[DSI_DEV_REGULATOR_MAX];
  56. struct dsi_dphy_timing timing;
  57. const struct dsi_phy_cfg *cfg;
  58. bool regulator_ldo_mode;
  59. struct msm_dsi_pll *pll;
  60. };
  61. static int dsi_phy_regulator_init(struct msm_dsi_phy *phy)
  62. {
  63. struct regulator_bulk_data *s = phy->supplies;
  64. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  65. struct device *dev = &phy->pdev->dev;
  66. int num = phy->cfg->reg_cfg.num;
  67. int i, ret;
  68. for (i = 0; i < num; i++)
  69. s[i].supply = regs[i].name;
  70. ret = devm_regulator_bulk_get(&phy->pdev->dev, num, s);
  71. if (ret < 0) {
  72. dev_err(dev, "%s: failed to init regulator, ret=%d\n",
  73. __func__, ret);
  74. return ret;
  75. }
  76. for (i = 0; i < num; i++) {
  77. if ((regs[i].min_voltage >= 0) && (regs[i].max_voltage >= 0)) {
  78. ret = regulator_set_voltage(s[i].consumer,
  79. regs[i].min_voltage, regs[i].max_voltage);
  80. if (ret < 0) {
  81. dev_err(dev,
  82. "regulator %d set voltage failed, %d\n",
  83. i, ret);
  84. return ret;
  85. }
  86. }
  87. }
  88. return 0;
  89. }
  90. static void dsi_phy_regulator_disable(struct msm_dsi_phy *phy)
  91. {
  92. struct regulator_bulk_data *s = phy->supplies;
  93. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  94. int num = phy->cfg->reg_cfg.num;
  95. int i;
  96. DBG("");
  97. for (i = num - 1; i >= 0; i--)
  98. if (regs[i].disable_load >= 0)
  99. regulator_set_load(s[i].consumer,
  100. regs[i].disable_load);
  101. regulator_bulk_disable(num, s);
  102. }
  103. static int dsi_phy_regulator_enable(struct msm_dsi_phy *phy)
  104. {
  105. struct regulator_bulk_data *s = phy->supplies;
  106. const struct dsi_reg_entry *regs = phy->cfg->reg_cfg.regs;
  107. struct device *dev = &phy->pdev->dev;
  108. int num = phy->cfg->reg_cfg.num;
  109. int ret, i;
  110. DBG("");
  111. for (i = 0; i < num; i++) {
  112. if (regs[i].enable_load >= 0) {
  113. ret = regulator_set_load(s[i].consumer,
  114. regs[i].enable_load);
  115. if (ret < 0) {
  116. dev_err(dev,
  117. "regulator %d set op mode failed, %d\n",
  118. i, ret);
  119. goto fail;
  120. }
  121. }
  122. }
  123. ret = regulator_bulk_enable(num, s);
  124. if (ret < 0) {
  125. dev_err(dev, "regulator enable failed, %d\n", ret);
  126. goto fail;
  127. }
  128. return 0;
  129. fail:
  130. for (i--; i >= 0; i--)
  131. regulator_set_load(s[i].consumer, regs[i].disable_load);
  132. return ret;
  133. }
  134. static void dsi_phy_set_src_pll(struct msm_dsi_phy *phy, int pll_id, u32 reg,
  135. u32 bit_mask)
  136. {
  137. int phy_id = phy->id;
  138. u32 val;
  139. if ((phy_id >= DSI_MAX) || (pll_id >= DSI_MAX))
  140. return;
  141. val = dsi_phy_read(phy->base + reg);
  142. if (phy->cfg->src_pll_truthtable[phy_id][pll_id])
  143. dsi_phy_write(phy->base + reg, val | bit_mask);
  144. else
  145. dsi_phy_write(phy->base + reg, val & (~bit_mask));
  146. }
  147. #define S_DIV_ROUND_UP(n, d) \
  148. (((n) >= 0) ? (((n) + (d) - 1) / (d)) : (((n) - (d) + 1) / (d)))
  149. static inline s32 linear_inter(s32 tmax, s32 tmin, s32 percent,
  150. s32 min_result, bool even)
  151. {
  152. s32 v;
  153. v = (tmax - tmin) * percent;
  154. v = S_DIV_ROUND_UP(v, 100) + tmin;
  155. if (even && (v & 0x1))
  156. return max_t(s32, min_result, v - 1);
  157. else
  158. return max_t(s32, min_result, v);
  159. }
  160. static void dsi_dphy_timing_calc_clk_zero(struct dsi_dphy_timing *timing,
  161. s32 ui, s32 coeff, s32 pcnt)
  162. {
  163. s32 tmax, tmin, clk_z;
  164. s32 temp;
  165. /* reset */
  166. temp = 300 * coeff - ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  167. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  168. if (tmin > 255) {
  169. tmax = 511;
  170. clk_z = linear_inter(2 * tmin, tmin, pcnt, 0, true);
  171. } else {
  172. tmax = 255;
  173. clk_z = linear_inter(tmax, tmin, pcnt, 0, true);
  174. }
  175. /* adjust */
  176. temp = (timing->hs_rqst + timing->clk_prepare + clk_z) & 0x7;
  177. timing->clk_zero = clk_z + 8 - temp;
  178. }
  179. static int dsi_dphy_timing_calc(struct dsi_dphy_timing *timing,
  180. const unsigned long bit_rate, const unsigned long esc_rate)
  181. {
  182. s32 ui, lpx;
  183. s32 tmax, tmin;
  184. s32 pcnt0 = 10;
  185. s32 pcnt1 = (bit_rate > 1200000000) ? 15 : 10;
  186. s32 pcnt2 = 10;
  187. s32 pcnt3 = (bit_rate > 180000000) ? 10 : 40;
  188. s32 coeff = 1000; /* Precision, should avoid overflow */
  189. s32 temp;
  190. if (!bit_rate || !esc_rate)
  191. return -EINVAL;
  192. ui = mult_frac(NSEC_PER_MSEC, coeff, bit_rate / 1000);
  193. lpx = mult_frac(NSEC_PER_MSEC, coeff, esc_rate / 1000);
  194. tmax = S_DIV_ROUND_UP(95 * coeff, ui) - 2;
  195. tmin = S_DIV_ROUND_UP(38 * coeff, ui) - 2;
  196. timing->clk_prepare = linear_inter(tmax, tmin, pcnt0, 0, true);
  197. temp = lpx / ui;
  198. if (temp & 0x1)
  199. timing->hs_rqst = temp;
  200. else
  201. timing->hs_rqst = max_t(s32, 0, temp - 2);
  202. /* Calculate clk_zero after clk_prepare and hs_rqst */
  203. dsi_dphy_timing_calc_clk_zero(timing, ui, coeff, pcnt2);
  204. temp = 105 * coeff + 12 * ui - 20 * coeff;
  205. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  206. tmin = S_DIV_ROUND_UP(60 * coeff, ui) - 2;
  207. timing->clk_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  208. temp = 85 * coeff + 6 * ui;
  209. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  210. temp = 40 * coeff + 4 * ui;
  211. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  212. timing->hs_prepare = linear_inter(tmax, tmin, pcnt1, 0, true);
  213. tmax = 255;
  214. temp = ((timing->hs_prepare >> 1) + 1) * 2 * ui + 2 * ui;
  215. temp = 145 * coeff + 10 * ui - temp;
  216. tmin = S_DIV_ROUND_UP(temp, ui) - 2;
  217. timing->hs_zero = linear_inter(tmax, tmin, pcnt2, 24, true);
  218. temp = 105 * coeff + 12 * ui - 20 * coeff;
  219. tmax = S_DIV_ROUND_UP(temp, ui) - 2;
  220. temp = 60 * coeff + 4 * ui;
  221. tmin = DIV_ROUND_UP(temp, ui) - 2;
  222. timing->hs_trail = linear_inter(tmax, tmin, pcnt3, 0, true);
  223. tmax = 255;
  224. tmin = S_DIV_ROUND_UP(100 * coeff, ui) - 2;
  225. timing->hs_exit = linear_inter(tmax, tmin, pcnt2, 0, true);
  226. tmax = 63;
  227. temp = ((timing->hs_exit >> 1) + 1) * 2 * ui;
  228. temp = 60 * coeff + 52 * ui - 24 * ui - temp;
  229. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  230. timing->clk_post = linear_inter(tmax, tmin, pcnt2, 0, false);
  231. tmax = 63;
  232. temp = ((timing->clk_prepare >> 1) + 1) * 2 * ui;
  233. temp += ((timing->clk_zero >> 1) + 1) * 2 * ui;
  234. temp += 8 * ui + lpx;
  235. tmin = S_DIV_ROUND_UP(temp, 8 * ui) - 1;
  236. if (tmin > tmax) {
  237. temp = linear_inter(2 * tmax, tmin, pcnt2, 0, false) >> 1;
  238. timing->clk_pre = temp >> 1;
  239. temp = (2 * tmax - tmin) * pcnt2;
  240. } else {
  241. timing->clk_pre = linear_inter(tmax, tmin, pcnt2, 0, false);
  242. }
  243. timing->ta_go = 3;
  244. timing->ta_sure = 0;
  245. timing->ta_get = 4;
  246. DBG("PHY timings: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",
  247. timing->clk_pre, timing->clk_post, timing->clk_zero,
  248. timing->clk_trail, timing->clk_prepare, timing->hs_exit,
  249. timing->hs_zero, timing->hs_prepare, timing->hs_trail,
  250. timing->hs_rqst);
  251. return 0;
  252. }
  253. static void dsi_28nm_phy_regulator_ctrl(struct msm_dsi_phy *phy, bool enable)
  254. {
  255. void __iomem *base = phy->reg_base;
  256. if (!enable) {
  257. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CAL_PWR_CFG, 0);
  258. return;
  259. }
  260. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CTRL_0, 0x0);
  261. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CAL_PWR_CFG, 1);
  262. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CTRL_5, 0);
  263. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CTRL_3, 0);
  264. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CTRL_2, 0x3);
  265. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CTRL_1, 0x9);
  266. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CTRL_0, 0x7);
  267. dsi_phy_write(base + REG_DSI_28nm_PHY_REGULATOR_CTRL_4, 0x20);
  268. }
  269. static int dsi_28nm_phy_enable(struct msm_dsi_phy *phy, int src_pll_id,
  270. const unsigned long bit_rate, const unsigned long esc_rate)
  271. {
  272. struct dsi_dphy_timing *timing = &phy->timing;
  273. int i;
  274. void __iomem *base = phy->base;
  275. DBG("");
  276. if (dsi_dphy_timing_calc(timing, bit_rate, esc_rate)) {
  277. pr_err("%s: D-PHY timing calculation failed\n", __func__);
  278. return -EINVAL;
  279. }
  280. dsi_phy_write(base + REG_DSI_28nm_PHY_STRENGTH_0, 0xff);
  281. dsi_28nm_phy_regulator_ctrl(phy, true);
  282. dsi_phy_write(base + REG_DSI_28nm_PHY_LDO_CNTRL, 0x00);
  283. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_0,
  284. DSI_28nm_PHY_TIMING_CTRL_0_CLK_ZERO(timing->clk_zero));
  285. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_1,
  286. DSI_28nm_PHY_TIMING_CTRL_1_CLK_TRAIL(timing->clk_trail));
  287. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_2,
  288. DSI_28nm_PHY_TIMING_CTRL_2_CLK_PREPARE(timing->clk_prepare));
  289. if (timing->clk_zero & BIT(8))
  290. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_3,
  291. DSI_28nm_PHY_TIMING_CTRL_3_CLK_ZERO_8);
  292. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_4,
  293. DSI_28nm_PHY_TIMING_CTRL_4_HS_EXIT(timing->hs_exit));
  294. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_5,
  295. DSI_28nm_PHY_TIMING_CTRL_5_HS_ZERO(timing->hs_zero));
  296. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_6,
  297. DSI_28nm_PHY_TIMING_CTRL_6_HS_PREPARE(timing->hs_prepare));
  298. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_7,
  299. DSI_28nm_PHY_TIMING_CTRL_7_HS_TRAIL(timing->hs_trail));
  300. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_8,
  301. DSI_28nm_PHY_TIMING_CTRL_8_HS_RQST(timing->hs_rqst));
  302. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_9,
  303. DSI_28nm_PHY_TIMING_CTRL_9_TA_GO(timing->ta_go) |
  304. DSI_28nm_PHY_TIMING_CTRL_9_TA_SURE(timing->ta_sure));
  305. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_10,
  306. DSI_28nm_PHY_TIMING_CTRL_10_TA_GET(timing->ta_get));
  307. dsi_phy_write(base + REG_DSI_28nm_PHY_TIMING_CTRL_11,
  308. DSI_28nm_PHY_TIMING_CTRL_11_TRIG3_CMD(0));
  309. dsi_phy_write(base + REG_DSI_28nm_PHY_CTRL_1, 0x00);
  310. dsi_phy_write(base + REG_DSI_28nm_PHY_CTRL_0, 0x5f);
  311. dsi_phy_write(base + REG_DSI_28nm_PHY_STRENGTH_1, 0x6);
  312. for (i = 0; i < 4; i++) {
  313. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_CFG_0(i), 0);
  314. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_CFG_1(i), 0);
  315. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_CFG_2(i), 0);
  316. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_CFG_3(i), 0);
  317. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_TEST_DATAPATH(i), 0);
  318. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_DEBUG_SEL(i), 0);
  319. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_TEST_STR_0(i), 0x1);
  320. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_TEST_STR_1(i), 0x97);
  321. }
  322. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_CFG_4(0), 0);
  323. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_CFG_4(1), 0x5);
  324. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_CFG_4(2), 0xa);
  325. dsi_phy_write(base + REG_DSI_28nm_PHY_LN_CFG_4(3), 0xf);
  326. dsi_phy_write(base + REG_DSI_28nm_PHY_LNCK_CFG_1, 0xc0);
  327. dsi_phy_write(base + REG_DSI_28nm_PHY_LNCK_TEST_STR0, 0x1);
  328. dsi_phy_write(base + REG_DSI_28nm_PHY_LNCK_TEST_STR1, 0xbb);
  329. dsi_phy_write(base + REG_DSI_28nm_PHY_CTRL_0, 0x5f);
  330. dsi_phy_set_src_pll(phy, src_pll_id, REG_DSI_28nm_PHY_GLBL_TEST_CTRL,
  331. DSI_28nm_PHY_GLBL_TEST_CTRL_BITCLK_HS_SEL);
  332. return 0;
  333. }
  334. static int dsi_28nm_phy_disable(struct msm_dsi_phy *phy)
  335. {
  336. dsi_phy_write(phy->base + REG_DSI_28nm_PHY_CTRL_0, 0);
  337. dsi_28nm_phy_regulator_ctrl(phy, false);
  338. /*
  339. * Wait for the registers writes to complete in order to
  340. * ensure that the phy is completely disabled
  341. */
  342. wmb();
  343. return 0;
  344. }
  345. static void dsi_20nm_phy_regulator_ctrl(struct msm_dsi_phy *phy, bool enable)
  346. {
  347. void __iomem *base = phy->reg_base;
  348. if (!enable) {
  349. dsi_phy_write(base + REG_DSI_20nm_PHY_REGULATOR_CAL_PWR_CFG, 0);
  350. return;
  351. }
  352. if (phy->regulator_ldo_mode) {
  353. dsi_phy_write(phy->base + REG_DSI_20nm_PHY_LDO_CNTRL, 0x1d);
  354. return;
  355. }
  356. /* non LDO mode */
  357. dsi_phy_write(base + REG_DSI_20nm_PHY_REGULATOR_CTRL_1, 0x03);
  358. dsi_phy_write(base + REG_DSI_20nm_PHY_REGULATOR_CTRL_2, 0x03);
  359. dsi_phy_write(base + REG_DSI_20nm_PHY_REGULATOR_CTRL_3, 0x00);
  360. dsi_phy_write(base + REG_DSI_20nm_PHY_REGULATOR_CTRL_4, 0x20);
  361. dsi_phy_write(base + REG_DSI_20nm_PHY_REGULATOR_CAL_PWR_CFG, 0x01);
  362. dsi_phy_write(phy->base + REG_DSI_20nm_PHY_LDO_CNTRL, 0x00);
  363. dsi_phy_write(base + REG_DSI_20nm_PHY_REGULATOR_CTRL_0, 0x03);
  364. }
  365. static int dsi_20nm_phy_enable(struct msm_dsi_phy *phy, int src_pll_id,
  366. const unsigned long bit_rate, const unsigned long esc_rate)
  367. {
  368. struct dsi_dphy_timing *timing = &phy->timing;
  369. int i;
  370. void __iomem *base = phy->base;
  371. u32 cfg_4[4] = {0x20, 0x40, 0x20, 0x00};
  372. DBG("");
  373. if (dsi_dphy_timing_calc(timing, bit_rate, esc_rate)) {
  374. pr_err("%s: D-PHY timing calculation failed\n", __func__);
  375. return -EINVAL;
  376. }
  377. dsi_20nm_phy_regulator_ctrl(phy, true);
  378. dsi_phy_write(base + REG_DSI_20nm_PHY_STRENGTH_0, 0xff);
  379. dsi_phy_set_src_pll(phy, src_pll_id, REG_DSI_20nm_PHY_GLBL_TEST_CTRL,
  380. DSI_20nm_PHY_GLBL_TEST_CTRL_BITCLK_HS_SEL);
  381. for (i = 0; i < 4; i++) {
  382. dsi_phy_write(base + REG_DSI_20nm_PHY_LN_CFG_3(i),
  383. (i >> 1) * 0x40);
  384. dsi_phy_write(base + REG_DSI_20nm_PHY_LN_TEST_STR_0(i), 0x01);
  385. dsi_phy_write(base + REG_DSI_20nm_PHY_LN_TEST_STR_1(i), 0x46);
  386. dsi_phy_write(base + REG_DSI_20nm_PHY_LN_CFG_0(i), 0x02);
  387. dsi_phy_write(base + REG_DSI_20nm_PHY_LN_CFG_1(i), 0xa0);
  388. dsi_phy_write(base + REG_DSI_20nm_PHY_LN_CFG_4(i), cfg_4[i]);
  389. }
  390. dsi_phy_write(base + REG_DSI_20nm_PHY_LNCK_CFG_3, 0x80);
  391. dsi_phy_write(base + REG_DSI_20nm_PHY_LNCK_TEST_STR0, 0x01);
  392. dsi_phy_write(base + REG_DSI_20nm_PHY_LNCK_TEST_STR1, 0x46);
  393. dsi_phy_write(base + REG_DSI_20nm_PHY_LNCK_CFG_0, 0x00);
  394. dsi_phy_write(base + REG_DSI_20nm_PHY_LNCK_CFG_1, 0xa0);
  395. dsi_phy_write(base + REG_DSI_20nm_PHY_LNCK_CFG_2, 0x00);
  396. dsi_phy_write(base + REG_DSI_20nm_PHY_LNCK_CFG_4, 0x00);
  397. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_0,
  398. DSI_20nm_PHY_TIMING_CTRL_0_CLK_ZERO(timing->clk_zero));
  399. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_1,
  400. DSI_20nm_PHY_TIMING_CTRL_1_CLK_TRAIL(timing->clk_trail));
  401. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_2,
  402. DSI_20nm_PHY_TIMING_CTRL_2_CLK_PREPARE(timing->clk_prepare));
  403. if (timing->clk_zero & BIT(8))
  404. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_3,
  405. DSI_20nm_PHY_TIMING_CTRL_3_CLK_ZERO_8);
  406. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_4,
  407. DSI_20nm_PHY_TIMING_CTRL_4_HS_EXIT(timing->hs_exit));
  408. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_5,
  409. DSI_20nm_PHY_TIMING_CTRL_5_HS_ZERO(timing->hs_zero));
  410. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_6,
  411. DSI_20nm_PHY_TIMING_CTRL_6_HS_PREPARE(timing->hs_prepare));
  412. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_7,
  413. DSI_20nm_PHY_TIMING_CTRL_7_HS_TRAIL(timing->hs_trail));
  414. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_8,
  415. DSI_20nm_PHY_TIMING_CTRL_8_HS_RQST(timing->hs_rqst));
  416. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_9,
  417. DSI_20nm_PHY_TIMING_CTRL_9_TA_GO(timing->ta_go) |
  418. DSI_20nm_PHY_TIMING_CTRL_9_TA_SURE(timing->ta_sure));
  419. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_10,
  420. DSI_20nm_PHY_TIMING_CTRL_10_TA_GET(timing->ta_get));
  421. dsi_phy_write(base + REG_DSI_20nm_PHY_TIMING_CTRL_11,
  422. DSI_20nm_PHY_TIMING_CTRL_11_TRIG3_CMD(0));
  423. dsi_phy_write(base + REG_DSI_20nm_PHY_CTRL_1, 0x00);
  424. dsi_phy_write(base + REG_DSI_20nm_PHY_STRENGTH_1, 0x06);
  425. /* make sure everything is written before enable */
  426. wmb();
  427. dsi_phy_write(base + REG_DSI_20nm_PHY_CTRL_0, 0x7f);
  428. return 0;
  429. }
  430. static int dsi_20nm_phy_disable(struct msm_dsi_phy *phy)
  431. {
  432. dsi_phy_write(phy->base + REG_DSI_20nm_PHY_CTRL_0, 0);
  433. dsi_20nm_phy_regulator_ctrl(phy, false);
  434. return 0;
  435. }
  436. static int dsi_phy_enable_resource(struct msm_dsi_phy *phy)
  437. {
  438. int ret;
  439. pm_runtime_get_sync(&phy->pdev->dev);
  440. ret = clk_prepare_enable(phy->ahb_clk);
  441. if (ret) {
  442. pr_err("%s: can't enable ahb clk, %d\n", __func__, ret);
  443. pm_runtime_put_sync(&phy->pdev->dev);
  444. }
  445. return ret;
  446. }
  447. static void dsi_phy_disable_resource(struct msm_dsi_phy *phy)
  448. {
  449. clk_disable_unprepare(phy->ahb_clk);
  450. pm_runtime_put_sync(&phy->pdev->dev);
  451. }
  452. static const struct dsi_phy_cfg dsi_phy_cfgs[MSM_DSI_PHY_MAX] = {
  453. [MSM_DSI_PHY_28NM_HPM] = {
  454. .type = MSM_DSI_PHY_28NM_HPM,
  455. .src_pll_truthtable = { {true, true}, {false, true} },
  456. .reg_cfg = {
  457. .num = 1,
  458. .regs = {
  459. {"vddio", 1800000, 1800000, 100000, 100},
  460. },
  461. },
  462. .ops = {
  463. .enable = dsi_28nm_phy_enable,
  464. .disable = dsi_28nm_phy_disable,
  465. }
  466. },
  467. [MSM_DSI_PHY_28NM_LP] = {
  468. .type = MSM_DSI_PHY_28NM_LP,
  469. .src_pll_truthtable = { {true, true}, {true, true} },
  470. .reg_cfg = {
  471. .num = 1,
  472. .regs = {
  473. {"vddio", 1800000, 1800000, 100000, 100},
  474. },
  475. },
  476. .ops = {
  477. .enable = dsi_28nm_phy_enable,
  478. .disable = dsi_28nm_phy_disable,
  479. }
  480. },
  481. [MSM_DSI_PHY_20NM] = {
  482. .type = MSM_DSI_PHY_20NM,
  483. .src_pll_truthtable = { {false, true}, {false, true} },
  484. .reg_cfg = {
  485. .num = 2,
  486. .regs = {
  487. {"vddio", 1800000, 1800000, 100000, 100},
  488. {"vcca", 1000000, 1000000, 10000, 100},
  489. },
  490. },
  491. .ops = {
  492. .enable = dsi_20nm_phy_enable,
  493. .disable = dsi_20nm_phy_disable,
  494. }
  495. },
  496. };
  497. static const struct of_device_id dsi_phy_dt_match[] = {
  498. { .compatible = "qcom,dsi-phy-28nm-hpm",
  499. .data = &dsi_phy_cfgs[MSM_DSI_PHY_28NM_HPM],},
  500. { .compatible = "qcom,dsi-phy-28nm-lp",
  501. .data = &dsi_phy_cfgs[MSM_DSI_PHY_28NM_LP],},
  502. { .compatible = "qcom,dsi-phy-20nm",
  503. .data = &dsi_phy_cfgs[MSM_DSI_PHY_20NM],},
  504. {}
  505. };
  506. static int dsi_phy_driver_probe(struct platform_device *pdev)
  507. {
  508. struct msm_dsi_phy *phy;
  509. const struct of_device_id *match;
  510. int ret;
  511. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  512. if (!phy)
  513. return -ENOMEM;
  514. match = of_match_node(dsi_phy_dt_match, pdev->dev.of_node);
  515. if (!match)
  516. return -ENODEV;
  517. phy->cfg = match->data;
  518. phy->pdev = pdev;
  519. ret = of_property_read_u32(pdev->dev.of_node,
  520. "qcom,dsi-phy-index", &phy->id);
  521. if (ret) {
  522. dev_err(&pdev->dev,
  523. "%s: PHY index not specified, ret=%d\n",
  524. __func__, ret);
  525. goto fail;
  526. }
  527. phy->regulator_ldo_mode = of_property_read_bool(pdev->dev.of_node,
  528. "qcom,dsi-phy-regulator-ldo-mode");
  529. phy->base = msm_ioremap(pdev, "dsi_phy", "DSI_PHY");
  530. if (IS_ERR(phy->base)) {
  531. dev_err(&pdev->dev, "%s: failed to map phy base\n", __func__);
  532. ret = -ENOMEM;
  533. goto fail;
  534. }
  535. phy->reg_base = msm_ioremap(pdev, "dsi_phy_regulator", "DSI_PHY_REG");
  536. if (IS_ERR(phy->reg_base)) {
  537. dev_err(&pdev->dev,
  538. "%s: failed to map phy regulator base\n", __func__);
  539. ret = -ENOMEM;
  540. goto fail;
  541. }
  542. ret = dsi_phy_regulator_init(phy);
  543. if (ret) {
  544. dev_err(&pdev->dev, "%s: failed to init regulator\n", __func__);
  545. goto fail;
  546. }
  547. phy->ahb_clk = devm_clk_get(&pdev->dev, "iface_clk");
  548. if (IS_ERR(phy->ahb_clk)) {
  549. pr_err("%s: Unable to get ahb clk\n", __func__);
  550. ret = PTR_ERR(phy->ahb_clk);
  551. goto fail;
  552. }
  553. /* PLL init will call into clk_register which requires
  554. * register access, so we need to enable power and ahb clock.
  555. */
  556. ret = dsi_phy_enable_resource(phy);
  557. if (ret)
  558. goto fail;
  559. phy->pll = msm_dsi_pll_init(pdev, phy->cfg->type, phy->id);
  560. if (!phy->pll)
  561. dev_info(&pdev->dev,
  562. "%s: pll init failed, need separate pll clk driver\n",
  563. __func__);
  564. dsi_phy_disable_resource(phy);
  565. platform_set_drvdata(pdev, phy);
  566. return 0;
  567. fail:
  568. return ret;
  569. }
  570. static int dsi_phy_driver_remove(struct platform_device *pdev)
  571. {
  572. struct msm_dsi_phy *phy = platform_get_drvdata(pdev);
  573. if (phy && phy->pll) {
  574. msm_dsi_pll_destroy(phy->pll);
  575. phy->pll = NULL;
  576. }
  577. platform_set_drvdata(pdev, NULL);
  578. return 0;
  579. }
  580. static struct platform_driver dsi_phy_platform_driver = {
  581. .probe = dsi_phy_driver_probe,
  582. .remove = dsi_phy_driver_remove,
  583. .driver = {
  584. .name = "msm_dsi_phy",
  585. .of_match_table = dsi_phy_dt_match,
  586. },
  587. };
  588. void __init msm_dsi_phy_driver_register(void)
  589. {
  590. platform_driver_register(&dsi_phy_platform_driver);
  591. }
  592. void __exit msm_dsi_phy_driver_unregister(void)
  593. {
  594. platform_driver_unregister(&dsi_phy_platform_driver);
  595. }
  596. int msm_dsi_phy_enable(struct msm_dsi_phy *phy, int src_pll_id,
  597. const unsigned long bit_rate, const unsigned long esc_rate)
  598. {
  599. int ret;
  600. if (!phy || !phy->cfg->ops.enable)
  601. return -EINVAL;
  602. ret = dsi_phy_regulator_enable(phy);
  603. if (ret) {
  604. dev_err(&phy->pdev->dev, "%s: regulator enable failed, %d\n",
  605. __func__, ret);
  606. return ret;
  607. }
  608. return phy->cfg->ops.enable(phy, src_pll_id, bit_rate, esc_rate);
  609. }
  610. int msm_dsi_phy_disable(struct msm_dsi_phy *phy)
  611. {
  612. if (!phy || !phy->cfg->ops.disable)
  613. return -EINVAL;
  614. phy->cfg->ops.disable(phy);
  615. dsi_phy_regulator_disable(phy);
  616. return 0;
  617. }
  618. void msm_dsi_phy_get_clk_pre_post(struct msm_dsi_phy *phy,
  619. u32 *clk_pre, u32 *clk_post)
  620. {
  621. if (!phy)
  622. return;
  623. if (clk_pre)
  624. *clk_pre = phy->timing.clk_pre;
  625. if (clk_post)
  626. *clk_post = phy->timing.clk_post;
  627. }
  628. struct msm_dsi_pll *msm_dsi_phy_get_pll(struct msm_dsi_phy *phy)
  629. {
  630. if (!phy)
  631. return NULL;
  632. return phy->pll;
  633. }