dsi_phy.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "msm-dsi-phy:[%s] " fmt, __func__
  6. #include <linux/of_device.h>
  7. #include <linux/err.h>
  8. #include <linux/regulator/consumer.h>
  9. #include <linux/clk.h>
  10. #include <linux/msm-bus.h>
  11. #include <linux/list.h>
  12. #include "msm_drv.h"
  13. #include "msm_kms.h"
  14. #include "dsi_phy.h"
  15. #include "dsi_phy_hw.h"
  16. #include "dsi_clk.h"
  17. #include "dsi_pwr.h"
  18. #include "dsi_catalog.h"
  19. #include "sde_dbg.h"
  20. #define DSI_PHY_DEFAULT_LABEL "MDSS PHY CTRL"
  21. #define BITS_PER_BYTE 8
  22. struct dsi_phy_list_item {
  23. struct msm_dsi_phy *phy;
  24. struct list_head list;
  25. };
  26. static LIST_HEAD(dsi_phy_list);
  27. static DEFINE_MUTEX(dsi_phy_list_lock);
  28. static const struct dsi_ver_spec_info dsi_phy_v0_0_hpm = {
  29. .version = DSI_PHY_VERSION_0_0_HPM,
  30. .lane_cfg_count = 4,
  31. .strength_cfg_count = 2,
  32. .regulator_cfg_count = 1,
  33. .timing_cfg_count = 8,
  34. };
  35. static const struct dsi_ver_spec_info dsi_phy_v0_0_lpm = {
  36. .version = DSI_PHY_VERSION_0_0_LPM,
  37. .lane_cfg_count = 4,
  38. .strength_cfg_count = 2,
  39. .regulator_cfg_count = 1,
  40. .timing_cfg_count = 8,
  41. };
  42. static const struct dsi_ver_spec_info dsi_phy_v1_0 = {
  43. .version = DSI_PHY_VERSION_1_0,
  44. .lane_cfg_count = 4,
  45. .strength_cfg_count = 2,
  46. .regulator_cfg_count = 1,
  47. .timing_cfg_count = 8,
  48. };
  49. static const struct dsi_ver_spec_info dsi_phy_v2_0 = {
  50. .version = DSI_PHY_VERSION_2_0,
  51. .lane_cfg_count = 4,
  52. .strength_cfg_count = 2,
  53. .regulator_cfg_count = 1,
  54. .timing_cfg_count = 8,
  55. };
  56. static const struct dsi_ver_spec_info dsi_phy_v3_0 = {
  57. .version = DSI_PHY_VERSION_3_0,
  58. .lane_cfg_count = 4,
  59. .strength_cfg_count = 2,
  60. .regulator_cfg_count = 0,
  61. .timing_cfg_count = 12,
  62. };
  63. static const struct dsi_ver_spec_info dsi_phy_v4_0 = {
  64. .version = DSI_PHY_VERSION_4_0,
  65. .lane_cfg_count = 4,
  66. .strength_cfg_count = 2,
  67. .regulator_cfg_count = 0,
  68. .timing_cfg_count = 14,
  69. };
  70. static const struct dsi_ver_spec_info dsi_phy_v4_1 = {
  71. .version = DSI_PHY_VERSION_4_1,
  72. .lane_cfg_count = 4,
  73. .strength_cfg_count = 2,
  74. .regulator_cfg_count = 0,
  75. .timing_cfg_count = 14,
  76. };
  77. static const struct of_device_id msm_dsi_phy_of_match[] = {
  78. { .compatible = "qcom,dsi-phy-v0.0-hpm",
  79. .data = &dsi_phy_v0_0_hpm,},
  80. { .compatible = "qcom,dsi-phy-v0.0-lpm",
  81. .data = &dsi_phy_v0_0_lpm,},
  82. { .compatible = "qcom,dsi-phy-v1.0",
  83. .data = &dsi_phy_v1_0,},
  84. { .compatible = "qcom,dsi-phy-v2.0",
  85. .data = &dsi_phy_v2_0,},
  86. { .compatible = "qcom,dsi-phy-v3.0",
  87. .data = &dsi_phy_v3_0,},
  88. { .compatible = "qcom,dsi-phy-v4.0",
  89. .data = &dsi_phy_v4_0,},
  90. { .compatible = "qcom,dsi-phy-v4.1",
  91. .data = &dsi_phy_v4_1,},
  92. {}
  93. };
  94. static int dsi_phy_regmap_init(struct platform_device *pdev,
  95. struct msm_dsi_phy *phy)
  96. {
  97. int rc = 0;
  98. void __iomem *ptr;
  99. ptr = msm_ioremap(pdev, "dsi_phy", phy->name);
  100. if (IS_ERR(ptr)) {
  101. rc = PTR_ERR(ptr);
  102. return rc;
  103. }
  104. phy->hw.base = ptr;
  105. pr_debug("[%s] map dsi_phy registers to %pK\n",
  106. phy->name, phy->hw.base);
  107. switch (phy->ver_info->version) {
  108. case DSI_PHY_VERSION_2_0:
  109. ptr = msm_ioremap(pdev, "phy_clamp_base", phy->name);
  110. if (IS_ERR(ptr))
  111. phy->hw.phy_clamp_base = NULL;
  112. else
  113. phy->hw.phy_clamp_base = ptr;
  114. break;
  115. default:
  116. break;
  117. }
  118. return rc;
  119. }
  120. static int dsi_phy_regmap_deinit(struct msm_dsi_phy *phy)
  121. {
  122. pr_debug("[%s] unmap registers\n", phy->name);
  123. return 0;
  124. }
  125. static int dsi_phy_supplies_init(struct platform_device *pdev,
  126. struct msm_dsi_phy *phy)
  127. {
  128. int rc = 0;
  129. int i = 0;
  130. struct dsi_regulator_info *regs;
  131. struct regulator *vreg = NULL;
  132. regs = &phy->pwr_info.digital;
  133. regs->vregs = devm_kzalloc(&pdev->dev, sizeof(struct dsi_vreg),
  134. GFP_KERNEL);
  135. if (!regs->vregs)
  136. goto error;
  137. regs->count = 1;
  138. snprintf(regs->vregs->vreg_name,
  139. ARRAY_SIZE(regs->vregs[i].vreg_name),
  140. "%s", "gdsc");
  141. rc = dsi_pwr_get_dt_vreg_data(&pdev->dev,
  142. &phy->pwr_info.phy_pwr,
  143. "qcom,phy-supply-entries");
  144. if (rc) {
  145. pr_err("failed to get host power supplies, rc = %d\n", rc);
  146. goto error_digital;
  147. }
  148. regs = &phy->pwr_info.digital;
  149. for (i = 0; i < regs->count; i++) {
  150. vreg = devm_regulator_get(&pdev->dev, regs->vregs[i].vreg_name);
  151. rc = PTR_RET(vreg);
  152. if (rc) {
  153. pr_err("failed to get %s regulator\n",
  154. regs->vregs[i].vreg_name);
  155. goto error_host_pwr;
  156. }
  157. regs->vregs[i].vreg = vreg;
  158. }
  159. regs = &phy->pwr_info.phy_pwr;
  160. for (i = 0; i < regs->count; i++) {
  161. vreg = devm_regulator_get(&pdev->dev, regs->vregs[i].vreg_name);
  162. rc = PTR_RET(vreg);
  163. if (rc) {
  164. pr_err("failed to get %s regulator\n",
  165. regs->vregs[i].vreg_name);
  166. for (--i; i >= 0; i--)
  167. devm_regulator_put(regs->vregs[i].vreg);
  168. goto error_digital_put;
  169. }
  170. regs->vregs[i].vreg = vreg;
  171. }
  172. return rc;
  173. error_digital_put:
  174. regs = &phy->pwr_info.digital;
  175. for (i = 0; i < regs->count; i++)
  176. devm_regulator_put(regs->vregs[i].vreg);
  177. error_host_pwr:
  178. devm_kfree(&pdev->dev, phy->pwr_info.phy_pwr.vregs);
  179. phy->pwr_info.phy_pwr.vregs = NULL;
  180. phy->pwr_info.phy_pwr.count = 0;
  181. error_digital:
  182. devm_kfree(&pdev->dev, phy->pwr_info.digital.vregs);
  183. phy->pwr_info.digital.vregs = NULL;
  184. phy->pwr_info.digital.count = 0;
  185. error:
  186. return rc;
  187. }
  188. static int dsi_phy_supplies_deinit(struct msm_dsi_phy *phy)
  189. {
  190. int i = 0;
  191. int rc = 0;
  192. struct dsi_regulator_info *regs;
  193. regs = &phy->pwr_info.digital;
  194. for (i = 0; i < regs->count; i++) {
  195. if (!regs->vregs[i].vreg)
  196. pr_err("vreg is NULL, should not reach here\n");
  197. else
  198. devm_regulator_put(regs->vregs[i].vreg);
  199. }
  200. regs = &phy->pwr_info.phy_pwr;
  201. for (i = 0; i < regs->count; i++) {
  202. if (!regs->vregs[i].vreg)
  203. pr_err("vreg is NULL, should not reach here\n");
  204. else
  205. devm_regulator_put(regs->vregs[i].vreg);
  206. }
  207. if (phy->pwr_info.phy_pwr.vregs) {
  208. devm_kfree(&phy->pdev->dev, phy->pwr_info.phy_pwr.vregs);
  209. phy->pwr_info.phy_pwr.vregs = NULL;
  210. phy->pwr_info.phy_pwr.count = 0;
  211. }
  212. if (phy->pwr_info.digital.vregs) {
  213. devm_kfree(&phy->pdev->dev, phy->pwr_info.digital.vregs);
  214. phy->pwr_info.digital.vregs = NULL;
  215. phy->pwr_info.digital.count = 0;
  216. }
  217. return rc;
  218. }
  219. static int dsi_phy_parse_dt_per_lane_cfgs(struct platform_device *pdev,
  220. struct dsi_phy_per_lane_cfgs *cfg,
  221. char *property)
  222. {
  223. int rc = 0, i = 0, j = 0;
  224. const u8 *data;
  225. u32 len = 0;
  226. data = of_get_property(pdev->dev.of_node, property, &len);
  227. if (!data) {
  228. pr_err("Unable to read Phy %s settings\n", property);
  229. return -EINVAL;
  230. }
  231. if (len != DSI_LANE_MAX * cfg->count_per_lane) {
  232. pr_err("incorrect phy %s settings, exp=%d, act=%d\n",
  233. property, (DSI_LANE_MAX * cfg->count_per_lane), len);
  234. return -EINVAL;
  235. }
  236. for (i = DSI_LOGICAL_LANE_0; i < DSI_LANE_MAX; i++) {
  237. for (j = 0; j < cfg->count_per_lane; j++) {
  238. cfg->lane[i][j] = *data;
  239. data++;
  240. }
  241. }
  242. return rc;
  243. }
  244. static int dsi_phy_settings_init(struct platform_device *pdev,
  245. struct msm_dsi_phy *phy)
  246. {
  247. int rc = 0;
  248. struct dsi_phy_per_lane_cfgs *lane = &phy->cfg.lanecfg;
  249. struct dsi_phy_per_lane_cfgs *strength = &phy->cfg.strength;
  250. struct dsi_phy_per_lane_cfgs *timing = &phy->cfg.timing;
  251. struct dsi_phy_per_lane_cfgs *regs = &phy->cfg.regulators;
  252. lane->count_per_lane = phy->ver_info->lane_cfg_count;
  253. rc = dsi_phy_parse_dt_per_lane_cfgs(pdev, lane,
  254. "qcom,platform-lane-config");
  255. if (rc) {
  256. pr_err("failed to parse lane cfgs, rc=%d\n", rc);
  257. goto err;
  258. }
  259. strength->count_per_lane = phy->ver_info->strength_cfg_count;
  260. rc = dsi_phy_parse_dt_per_lane_cfgs(pdev, strength,
  261. "qcom,platform-strength-ctrl");
  262. if (rc) {
  263. pr_err("failed to parse lane cfgs, rc=%d\n", rc);
  264. goto err;
  265. }
  266. regs->count_per_lane = phy->ver_info->regulator_cfg_count;
  267. if (regs->count_per_lane > 0) {
  268. rc = dsi_phy_parse_dt_per_lane_cfgs(pdev, regs,
  269. "qcom,platform-regulator-settings");
  270. if (rc) {
  271. pr_err("failed to parse lane cfgs, rc=%d\n", rc);
  272. goto err;
  273. }
  274. }
  275. /* Actual timing values are dependent on panel */
  276. timing->count_per_lane = phy->ver_info->timing_cfg_count;
  277. phy->allow_phy_power_off = of_property_read_bool(pdev->dev.of_node,
  278. "qcom,panel-allow-phy-poweroff");
  279. of_property_read_u32(pdev->dev.of_node,
  280. "qcom,dsi-phy-regulator-min-datarate-bps",
  281. &phy->regulator_min_datarate_bps);
  282. phy->cfg.force_clk_lane_hs = of_property_read_bool(pdev->dev.of_node,
  283. "qcom,panel-force-clock-lane-hs");
  284. return 0;
  285. err:
  286. lane->count_per_lane = 0;
  287. strength->count_per_lane = 0;
  288. regs->count_per_lane = 0;
  289. timing->count_per_lane = 0;
  290. return rc;
  291. }
  292. static int dsi_phy_settings_deinit(struct msm_dsi_phy *phy)
  293. {
  294. memset(&phy->cfg.lanecfg, 0x0, sizeof(phy->cfg.lanecfg));
  295. memset(&phy->cfg.strength, 0x0, sizeof(phy->cfg.strength));
  296. memset(&phy->cfg.timing, 0x0, sizeof(phy->cfg.timing));
  297. memset(&phy->cfg.regulators, 0x0, sizeof(phy->cfg.regulators));
  298. return 0;
  299. }
  300. static int dsi_phy_driver_probe(struct platform_device *pdev)
  301. {
  302. struct msm_dsi_phy *dsi_phy;
  303. struct dsi_phy_list_item *item;
  304. const struct of_device_id *id;
  305. const struct dsi_ver_spec_info *ver_info;
  306. int rc = 0;
  307. u32 index = 0;
  308. if (!pdev || !pdev->dev.of_node) {
  309. pr_err("pdev not found\n");
  310. return -ENODEV;
  311. }
  312. id = of_match_node(msm_dsi_phy_of_match, pdev->dev.of_node);
  313. if (!id)
  314. return -ENODEV;
  315. ver_info = id->data;
  316. item = devm_kzalloc(&pdev->dev, sizeof(*item), GFP_KERNEL);
  317. if (!item)
  318. return -ENOMEM;
  319. dsi_phy = devm_kzalloc(&pdev->dev, sizeof(*dsi_phy), GFP_KERNEL);
  320. if (!dsi_phy) {
  321. devm_kfree(&pdev->dev, item);
  322. return -ENOMEM;
  323. }
  324. rc = of_property_read_u32(pdev->dev.of_node, "cell-index", &index);
  325. if (rc) {
  326. pr_debug("cell index not set, default to 0\n");
  327. index = 0;
  328. }
  329. dsi_phy->index = index;
  330. dsi_phy->name = of_get_property(pdev->dev.of_node, "label", NULL);
  331. if (!dsi_phy->name)
  332. dsi_phy->name = DSI_PHY_DEFAULT_LABEL;
  333. pr_debug("Probing %s device\n", dsi_phy->name);
  334. dsi_phy->ver_info = ver_info;
  335. rc = dsi_phy_regmap_init(pdev, dsi_phy);
  336. if (rc) {
  337. pr_err("Failed to parse register information, rc=%d\n", rc);
  338. goto fail;
  339. }
  340. rc = dsi_phy_supplies_init(pdev, dsi_phy);
  341. if (rc) {
  342. pr_err("failed to parse voltage supplies, rc = %d\n", rc);
  343. goto fail_regmap;
  344. }
  345. rc = dsi_catalog_phy_setup(&dsi_phy->hw, ver_info->version,
  346. dsi_phy->index);
  347. if (rc) {
  348. pr_err("Catalog does not support version (%d)\n",
  349. ver_info->version);
  350. goto fail_supplies;
  351. }
  352. rc = dsi_phy_settings_init(pdev, dsi_phy);
  353. if (rc) {
  354. pr_err("Failed to parse phy setting, rc=%d\n", rc);
  355. goto fail_supplies;
  356. }
  357. item->phy = dsi_phy;
  358. mutex_lock(&dsi_phy_list_lock);
  359. list_add(&item->list, &dsi_phy_list);
  360. mutex_unlock(&dsi_phy_list_lock);
  361. mutex_init(&dsi_phy->phy_lock);
  362. /** TODO: initialize debugfs */
  363. dsi_phy->pdev = pdev;
  364. platform_set_drvdata(pdev, dsi_phy);
  365. pr_info("Probe successful for %s\n", dsi_phy->name);
  366. return 0;
  367. fail_supplies:
  368. (void)dsi_phy_supplies_deinit(dsi_phy);
  369. fail_regmap:
  370. (void)dsi_phy_regmap_deinit(dsi_phy);
  371. fail:
  372. devm_kfree(&pdev->dev, dsi_phy);
  373. devm_kfree(&pdev->dev, item);
  374. return rc;
  375. }
  376. static int dsi_phy_driver_remove(struct platform_device *pdev)
  377. {
  378. int rc = 0;
  379. struct msm_dsi_phy *phy = platform_get_drvdata(pdev);
  380. struct list_head *pos, *tmp;
  381. if (!pdev || !phy) {
  382. pr_err("Invalid device\n");
  383. return -EINVAL;
  384. }
  385. mutex_lock(&dsi_phy_list_lock);
  386. list_for_each_safe(pos, tmp, &dsi_phy_list) {
  387. struct dsi_phy_list_item *n;
  388. n = list_entry(pos, struct dsi_phy_list_item, list);
  389. if (n->phy == phy) {
  390. list_del(&n->list);
  391. devm_kfree(&pdev->dev, n);
  392. break;
  393. }
  394. }
  395. mutex_unlock(&dsi_phy_list_lock);
  396. mutex_lock(&phy->phy_lock);
  397. rc = dsi_phy_settings_deinit(phy);
  398. if (rc)
  399. pr_err("failed to deinitialize phy settings, rc=%d\n", rc);
  400. rc = dsi_phy_supplies_deinit(phy);
  401. if (rc)
  402. pr_err("failed to deinitialize voltage supplies, rc=%d\n", rc);
  403. rc = dsi_phy_regmap_deinit(phy);
  404. if (rc)
  405. pr_err("failed to deinitialize regmap, rc=%d\n", rc);
  406. mutex_unlock(&phy->phy_lock);
  407. mutex_destroy(&phy->phy_lock);
  408. devm_kfree(&pdev->dev, phy);
  409. platform_set_drvdata(pdev, NULL);
  410. return 0;
  411. }
  412. static struct platform_driver dsi_phy_platform_driver = {
  413. .probe = dsi_phy_driver_probe,
  414. .remove = dsi_phy_driver_remove,
  415. .driver = {
  416. .name = "dsi_phy",
  417. .of_match_table = msm_dsi_phy_of_match,
  418. },
  419. };
  420. static void dsi_phy_enable_hw(struct msm_dsi_phy *phy)
  421. {
  422. if (phy->hw.ops.regulator_enable)
  423. phy->hw.ops.regulator_enable(&phy->hw, &phy->cfg.regulators);
  424. if (phy->hw.ops.enable)
  425. phy->hw.ops.enable(&phy->hw, &phy->cfg);
  426. }
  427. static void dsi_phy_disable_hw(struct msm_dsi_phy *phy)
  428. {
  429. if (phy->hw.ops.disable)
  430. phy->hw.ops.disable(&phy->hw, &phy->cfg);
  431. if (phy->hw.ops.regulator_disable)
  432. phy->hw.ops.regulator_disable(&phy->hw);
  433. }
  434. /**
  435. * dsi_phy_get() - get a dsi phy handle from device node
  436. * @of_node: device node for dsi phy controller
  437. *
  438. * Gets the DSI PHY handle for the corresponding of_node. The ref count is
  439. * incremented to one all subsequents get will fail until the original client
  440. * calls a put.
  441. *
  442. * Return: DSI PHY handle or an error code.
  443. */
  444. struct msm_dsi_phy *dsi_phy_get(struct device_node *of_node)
  445. {
  446. struct list_head *pos, *tmp;
  447. struct msm_dsi_phy *phy = NULL;
  448. mutex_lock(&dsi_phy_list_lock);
  449. list_for_each_safe(pos, tmp, &dsi_phy_list) {
  450. struct dsi_phy_list_item *n;
  451. n = list_entry(pos, struct dsi_phy_list_item, list);
  452. if (n->phy->pdev->dev.of_node == of_node) {
  453. phy = n->phy;
  454. break;
  455. }
  456. }
  457. mutex_unlock(&dsi_phy_list_lock);
  458. if (!phy) {
  459. pr_err("Device with of node not found\n");
  460. phy = ERR_PTR(-EPROBE_DEFER);
  461. return phy;
  462. }
  463. mutex_lock(&phy->phy_lock);
  464. if (phy->refcount > 0) {
  465. pr_err("[PHY_%d] Device under use\n", phy->index);
  466. phy = ERR_PTR(-EINVAL);
  467. } else {
  468. phy->refcount++;
  469. }
  470. mutex_unlock(&phy->phy_lock);
  471. return phy;
  472. }
  473. /**
  474. * dsi_phy_put() - release dsi phy handle
  475. * @dsi_phy: DSI PHY handle.
  476. *
  477. * Release the DSI PHY hardware. Driver will clean up all resources and puts
  478. * back the DSI PHY into reset state.
  479. */
  480. void dsi_phy_put(struct msm_dsi_phy *dsi_phy)
  481. {
  482. mutex_lock(&dsi_phy->phy_lock);
  483. if (dsi_phy->refcount == 0)
  484. pr_err("Unbalanced %s call\n", __func__);
  485. else
  486. dsi_phy->refcount--;
  487. mutex_unlock(&dsi_phy->phy_lock);
  488. }
  489. /**
  490. * dsi_phy_drv_init() - initialize dsi phy driver
  491. * @dsi_phy: DSI PHY handle.
  492. *
  493. * Initializes DSI PHY driver. Should be called after dsi_phy_get().
  494. *
  495. * Return: error code.
  496. */
  497. int dsi_phy_drv_init(struct msm_dsi_phy *dsi_phy)
  498. {
  499. char dbg_name[DSI_DEBUG_NAME_LEN];
  500. snprintf(dbg_name, DSI_DEBUG_NAME_LEN, "dsi%d_phy", dsi_phy->index);
  501. sde_dbg_reg_register_base(dbg_name, dsi_phy->hw.base,
  502. msm_iomap_size(dsi_phy->pdev, "dsi_phy"));
  503. sde_dbg_reg_register_dump_range(dbg_name, dbg_name, 0,
  504. msm_iomap_size(dsi_phy->pdev, "dsi_phy"), 0);
  505. return 0;
  506. }
  507. /**
  508. * dsi_phy_drv_deinit() - de-initialize dsi phy driver
  509. * @dsi_phy: DSI PHY handle.
  510. *
  511. * Release all resources acquired by dsi_phy_drv_init().
  512. *
  513. * Return: error code.
  514. */
  515. int dsi_phy_drv_deinit(struct msm_dsi_phy *dsi_phy)
  516. {
  517. return 0;
  518. }
  519. int dsi_phy_clk_cb_register(struct msm_dsi_phy *dsi_phy,
  520. struct clk_ctrl_cb *clk_cb)
  521. {
  522. if (!dsi_phy || !clk_cb) {
  523. pr_err("Invalid params\n");
  524. return -EINVAL;
  525. }
  526. dsi_phy->clk_cb.priv = clk_cb->priv;
  527. dsi_phy->clk_cb.dsi_clk_cb = clk_cb->dsi_clk_cb;
  528. return 0;
  529. }
  530. /**
  531. * dsi_phy_validate_mode() - validate a display mode
  532. * @dsi_phy: DSI PHY handle.
  533. * @mode: Mode information.
  534. *
  535. * Validation will fail if the mode cannot be supported by the PHY driver or
  536. * hardware.
  537. *
  538. * Return: error code.
  539. */
  540. int dsi_phy_validate_mode(struct msm_dsi_phy *dsi_phy,
  541. struct dsi_mode_info *mode)
  542. {
  543. int rc = 0;
  544. if (!dsi_phy || !mode) {
  545. pr_err("Invalid params\n");
  546. return -EINVAL;
  547. }
  548. mutex_lock(&dsi_phy->phy_lock);
  549. pr_debug("[PHY_%d] Skipping validation\n", dsi_phy->index);
  550. mutex_unlock(&dsi_phy->phy_lock);
  551. return rc;
  552. }
  553. /**
  554. * dsi_phy_set_power_state() - enable/disable dsi phy power supplies
  555. * @dsi_phy: DSI PHY handle.
  556. * @enable: Boolean flag to enable/disable.
  557. *
  558. * Return: error code.
  559. */
  560. int dsi_phy_set_power_state(struct msm_dsi_phy *dsi_phy, bool enable)
  561. {
  562. int rc = 0;
  563. if (!dsi_phy) {
  564. pr_err("Invalid params\n");
  565. return -EINVAL;
  566. }
  567. mutex_lock(&dsi_phy->phy_lock);
  568. if (enable == dsi_phy->power_state) {
  569. pr_err("[PHY_%d] No state change\n", dsi_phy->index);
  570. goto error;
  571. }
  572. if (enable) {
  573. rc = dsi_pwr_enable_regulator(&dsi_phy->pwr_info.digital, true);
  574. if (rc) {
  575. pr_err("failed to enable digital regulator\n");
  576. goto error;
  577. }
  578. if (dsi_phy->dsi_phy_state == DSI_PHY_ENGINE_OFF &&
  579. dsi_phy->regulator_required) {
  580. rc = dsi_pwr_enable_regulator(
  581. &dsi_phy->pwr_info.phy_pwr, true);
  582. if (rc) {
  583. pr_err("failed to enable phy power\n");
  584. (void)dsi_pwr_enable_regulator(
  585. &dsi_phy->pwr_info.digital, false);
  586. goto error;
  587. }
  588. }
  589. } else {
  590. if (dsi_phy->dsi_phy_state == DSI_PHY_ENGINE_OFF &&
  591. dsi_phy->regulator_required) {
  592. rc = dsi_pwr_enable_regulator(
  593. &dsi_phy->pwr_info.phy_pwr, false);
  594. if (rc) {
  595. pr_err("failed to enable digital regulator\n");
  596. goto error;
  597. }
  598. }
  599. rc = dsi_pwr_enable_regulator(&dsi_phy->pwr_info.digital,
  600. false);
  601. if (rc) {
  602. pr_err("failed to enable phy power\n");
  603. goto error;
  604. }
  605. }
  606. dsi_phy->power_state = enable;
  607. error:
  608. mutex_unlock(&dsi_phy->phy_lock);
  609. return rc;
  610. }
  611. static int dsi_phy_enable_ulps(struct msm_dsi_phy *phy,
  612. struct dsi_host_config *config, bool clamp_enabled)
  613. {
  614. int rc = 0;
  615. u32 lanes = 0;
  616. u32 ulps_lanes;
  617. lanes = config->common_config.data_lanes;
  618. lanes |= DSI_CLOCK_LANE;
  619. /*
  620. * If DSI clamps are enabled, it means that the DSI lanes are
  621. * already in idle state. Checking for lanes to be in idle state
  622. * should be skipped during ULPS entry programming while coming
  623. * out of idle screen.
  624. */
  625. if (!clamp_enabled) {
  626. rc = phy->hw.ops.ulps_ops.wait_for_lane_idle(&phy->hw, lanes);
  627. if (rc) {
  628. pr_err("lanes not entering idle, skip ULPS\n");
  629. return rc;
  630. }
  631. }
  632. phy->hw.ops.ulps_ops.ulps_request(&phy->hw, &phy->cfg, lanes);
  633. ulps_lanes = phy->hw.ops.ulps_ops.get_lanes_in_ulps(&phy->hw);
  634. if (!phy->hw.ops.ulps_ops.is_lanes_in_ulps(lanes, ulps_lanes)) {
  635. pr_err("Failed to enter ULPS, request=0x%x, actual=0x%x\n",
  636. lanes, ulps_lanes);
  637. rc = -EIO;
  638. }
  639. return rc;
  640. }
  641. static int dsi_phy_disable_ulps(struct msm_dsi_phy *phy,
  642. struct dsi_host_config *config)
  643. {
  644. u32 ulps_lanes, lanes = 0;
  645. lanes = config->common_config.data_lanes;
  646. lanes |= DSI_CLOCK_LANE;
  647. ulps_lanes = phy->hw.ops.ulps_ops.get_lanes_in_ulps(&phy->hw);
  648. if (!phy->hw.ops.ulps_ops.is_lanes_in_ulps(lanes, ulps_lanes)) {
  649. pr_err("Mismatch in ULPS: lanes:%d, ulps_lanes:%d\n",
  650. lanes, ulps_lanes);
  651. return -EIO;
  652. }
  653. phy->hw.ops.ulps_ops.ulps_exit(&phy->hw, &phy->cfg, lanes);
  654. ulps_lanes = phy->hw.ops.ulps_ops.get_lanes_in_ulps(&phy->hw);
  655. if (phy->hw.ops.ulps_ops.is_lanes_in_ulps(lanes, ulps_lanes)) {
  656. pr_err("Lanes (0x%x) stuck in ULPS\n", ulps_lanes);
  657. return -EIO;
  658. }
  659. return 0;
  660. }
  661. void dsi_phy_toggle_resync_fifo(struct msm_dsi_phy *phy)
  662. {
  663. if (!phy)
  664. return;
  665. if (!phy->hw.ops.toggle_resync_fifo)
  666. return;
  667. phy->hw.ops.toggle_resync_fifo(&phy->hw);
  668. }
  669. void dsi_phy_reset_clk_en_sel(struct msm_dsi_phy *phy)
  670. {
  671. if (!phy)
  672. return;
  673. if (!phy->hw.ops.reset_clk_en_sel)
  674. return;
  675. phy->hw.ops.reset_clk_en_sel(&phy->hw);
  676. }
  677. int dsi_phy_set_ulps(struct msm_dsi_phy *phy, struct dsi_host_config *config,
  678. bool enable, bool clamp_enabled)
  679. {
  680. int rc = 0;
  681. if (!phy) {
  682. pr_err("Invalid params\n");
  683. return DSI_PHY_ULPS_ERROR;
  684. }
  685. if (!phy->hw.ops.ulps_ops.ulps_request ||
  686. !phy->hw.ops.ulps_ops.ulps_exit ||
  687. !phy->hw.ops.ulps_ops.get_lanes_in_ulps ||
  688. !phy->hw.ops.ulps_ops.is_lanes_in_ulps ||
  689. !phy->hw.ops.ulps_ops.wait_for_lane_idle) {
  690. pr_debug("DSI PHY ULPS ops not present\n");
  691. return DSI_PHY_ULPS_NOT_HANDLED;
  692. }
  693. mutex_lock(&phy->phy_lock);
  694. if (enable)
  695. rc = dsi_phy_enable_ulps(phy, config, clamp_enabled);
  696. else
  697. rc = dsi_phy_disable_ulps(phy, config);
  698. if (rc) {
  699. pr_err("[DSI_PHY%d] Ulps state change(%d) failed, rc=%d\n",
  700. phy->index, enable, rc);
  701. rc = DSI_PHY_ULPS_ERROR;
  702. goto error;
  703. }
  704. pr_debug("[DSI_PHY%d] ULPS state = %d\n", phy->index, enable);
  705. error:
  706. mutex_unlock(&phy->phy_lock);
  707. return rc;
  708. }
  709. /**
  710. * dsi_phy_enable() - enable DSI PHY hardware
  711. * @dsi_phy: DSI PHY handle.
  712. * @config: DSI host configuration.
  713. * @pll_source: Source PLL for PHY clock.
  714. * @skip_validation: Validation will not be performed on parameters.
  715. * @is_cont_splash_enabled: check whether continuous splash enabled.
  716. *
  717. * Validates and enables DSI PHY.
  718. *
  719. * Return: error code.
  720. */
  721. int dsi_phy_enable(struct msm_dsi_phy *phy,
  722. struct dsi_host_config *config,
  723. enum dsi_phy_pll_source pll_source,
  724. bool skip_validation,
  725. bool is_cont_splash_enabled)
  726. {
  727. int rc = 0;
  728. if (!phy || !config) {
  729. pr_err("Invalid params\n");
  730. return -EINVAL;
  731. }
  732. mutex_lock(&phy->phy_lock);
  733. if (!skip_validation)
  734. pr_debug("[PHY_%d] TODO: perform validation\n", phy->index);
  735. memcpy(&phy->mode, &config->video_timing, sizeof(phy->mode));
  736. memcpy(&phy->cfg.lane_map, &config->lane_map, sizeof(config->lane_map));
  737. phy->data_lanes = config->common_config.data_lanes;
  738. phy->dst_format = config->common_config.dst_format;
  739. phy->cfg.pll_source = pll_source;
  740. phy->cfg.bit_clk_rate_hz = config->bit_clk_rate_hz;
  741. /**
  742. * If PHY timing parameters are not present in panel dtsi file,
  743. * then calculate them in the driver
  744. */
  745. if (!phy->cfg.is_phy_timing_present)
  746. rc = phy->hw.ops.calculate_timing_params(&phy->hw,
  747. &phy->mode,
  748. &config->common_config,
  749. &phy->cfg.timing);
  750. if (rc) {
  751. pr_err("[%s] failed to set timing, rc=%d\n", phy->name, rc);
  752. goto error;
  753. }
  754. if (!is_cont_splash_enabled) {
  755. dsi_phy_enable_hw(phy);
  756. pr_debug("cont splash not enabled, phy enable required\n");
  757. }
  758. phy->dsi_phy_state = DSI_PHY_ENGINE_ON;
  759. error:
  760. mutex_unlock(&phy->phy_lock);
  761. return rc;
  762. }
  763. int dsi_phy_lane_reset(struct msm_dsi_phy *phy)
  764. {
  765. int ret = 0;
  766. if (!phy)
  767. return ret;
  768. mutex_lock(&phy->phy_lock);
  769. if (phy->hw.ops.phy_lane_reset)
  770. ret = phy->hw.ops.phy_lane_reset(&phy->hw);
  771. mutex_unlock(&phy->phy_lock);
  772. return ret;
  773. }
  774. /**
  775. * dsi_phy_disable() - disable DSI PHY hardware.
  776. * @phy: DSI PHY handle.
  777. *
  778. * Return: error code.
  779. */
  780. int dsi_phy_disable(struct msm_dsi_phy *phy)
  781. {
  782. int rc = 0;
  783. if (!phy) {
  784. pr_err("Invalid params\n");
  785. return -EINVAL;
  786. }
  787. mutex_lock(&phy->phy_lock);
  788. dsi_phy_disable_hw(phy);
  789. phy->dsi_phy_state = DSI_PHY_ENGINE_OFF;
  790. mutex_unlock(&phy->phy_lock);
  791. return rc;
  792. }
  793. /**
  794. * dsi_phy_set_clamp_state() - configure clamps for DSI lanes
  795. * @phy: DSI PHY handle.
  796. * @enable: boolean to specify clamp enable/disable.
  797. *
  798. * Return: error code.
  799. */
  800. int dsi_phy_set_clamp_state(struct msm_dsi_phy *phy, bool enable)
  801. {
  802. if (!phy)
  803. return -EINVAL;
  804. pr_debug("[%s] enable=%d\n", phy->name, enable);
  805. if (phy->hw.ops.clamp_ctrl)
  806. phy->hw.ops.clamp_ctrl(&phy->hw, enable);
  807. return 0;
  808. }
  809. /**
  810. * dsi_phy_idle_ctrl() - enable/disable DSI PHY during idle screen
  811. * @phy: DSI PHY handle
  812. * @enable: boolean to specify PHY enable/disable.
  813. *
  814. * Return: error code.
  815. */
  816. int dsi_phy_idle_ctrl(struct msm_dsi_phy *phy, bool enable)
  817. {
  818. if (!phy) {
  819. pr_err("Invalid params\n");
  820. return -EINVAL;
  821. }
  822. pr_debug("[%s] enable=%d\n", phy->name, enable);
  823. mutex_lock(&phy->phy_lock);
  824. if (enable) {
  825. if (phy->hw.ops.phy_idle_on)
  826. phy->hw.ops.phy_idle_on(&phy->hw, &phy->cfg);
  827. if (phy->hw.ops.regulator_enable)
  828. phy->hw.ops.regulator_enable(&phy->hw,
  829. &phy->cfg.regulators);
  830. if (phy->hw.ops.enable)
  831. phy->hw.ops.enable(&phy->hw, &phy->cfg);
  832. phy->dsi_phy_state = DSI_PHY_ENGINE_ON;
  833. } else {
  834. phy->dsi_phy_state = DSI_PHY_ENGINE_OFF;
  835. if (phy->hw.ops.disable)
  836. phy->hw.ops.disable(&phy->hw, &phy->cfg);
  837. if (phy->hw.ops.phy_idle_off)
  838. phy->hw.ops.phy_idle_off(&phy->hw);
  839. }
  840. mutex_unlock(&phy->phy_lock);
  841. return 0;
  842. }
  843. /**
  844. * dsi_phy_set_clk_freq() - set DSI PHY clock frequency setting
  845. * @phy: DSI PHY handle
  846. * @clk_freq: link clock frequency
  847. *
  848. * Return: error code.
  849. */
  850. int dsi_phy_set_clk_freq(struct msm_dsi_phy *phy,
  851. struct link_clk_freq *clk_freq)
  852. {
  853. if (!phy || !clk_freq) {
  854. pr_err("Invalid params\n");
  855. return -EINVAL;
  856. }
  857. phy->regulator_required = clk_freq->byte_clk_rate >
  858. (phy->regulator_min_datarate_bps / BITS_PER_BYTE);
  859. /*
  860. * DSI PLL needs 0p9 LDO1A for Powering DSI PLL block.
  861. * PLL driver can vote for this regulator in PLL driver file, but for
  862. * the usecase where we come out of idle(static screen), if PLL and
  863. * PHY vote for regulator ,there will be performance delays as both
  864. * votes go through RPM to enable regulators.
  865. */
  866. phy->regulator_required = true;
  867. pr_debug("[%s] lane_datarate=%u min_datarate=%u required=%d\n",
  868. phy->name,
  869. clk_freq->byte_clk_rate * BITS_PER_BYTE,
  870. phy->regulator_min_datarate_bps,
  871. phy->regulator_required);
  872. return 0;
  873. }
  874. /**
  875. * dsi_phy_set_timing_params() - timing parameters for the panel
  876. * @phy: DSI PHY handle
  877. * @timing: array holding timing params.
  878. * @size: size of the array.
  879. *
  880. * When PHY timing calculator is not implemented, this array will be used to
  881. * pass PHY timing information.
  882. *
  883. * Return: error code.
  884. */
  885. int dsi_phy_set_timing_params(struct msm_dsi_phy *phy,
  886. u32 *timing, u32 size)
  887. {
  888. int rc = 0;
  889. if (!phy || !timing || !size) {
  890. pr_err("Invalid params\n");
  891. return -EINVAL;
  892. }
  893. mutex_lock(&phy->phy_lock);
  894. if (phy->hw.ops.phy_timing_val)
  895. rc = phy->hw.ops.phy_timing_val(&phy->cfg.timing, timing, size);
  896. if (!rc)
  897. phy->cfg.is_phy_timing_present = true;
  898. mutex_unlock(&phy->phy_lock);
  899. return rc;
  900. }
  901. /**
  902. * dsi_phy_conv_phy_to_logical_lane() - Convert physical to logical lane
  903. * @lane_map: logical lane
  904. * @phy_lane: physical lane
  905. *
  906. * Return: Error code on failure. Lane number on success.
  907. */
  908. int dsi_phy_conv_phy_to_logical_lane(
  909. struct dsi_lane_map *lane_map, enum dsi_phy_data_lanes phy_lane)
  910. {
  911. int i = 0;
  912. if (phy_lane > DSI_PHYSICAL_LANE_3)
  913. return -EINVAL;
  914. for (i = DSI_LOGICAL_LANE_0; i < (DSI_LANE_MAX - 1); i++) {
  915. if (lane_map->lane_map_v2[i] == phy_lane)
  916. break;
  917. }
  918. return i;
  919. }
  920. /**
  921. * dsi_phy_conv_logical_to_phy_lane() - Convert logical to physical lane
  922. * @lane_map: physical lane
  923. * @lane: logical lane
  924. *
  925. * Return: Error code on failure. Lane number on success.
  926. */
  927. int dsi_phy_conv_logical_to_phy_lane(
  928. struct dsi_lane_map *lane_map, enum dsi_logical_lane lane)
  929. {
  930. int i = 0;
  931. if (lane > (DSI_LANE_MAX - 1))
  932. return -EINVAL;
  933. for (i = DSI_LOGICAL_LANE_0; i < (DSI_LANE_MAX - 1); i++) {
  934. if (BIT(i) == lane_map->lane_map_v2[lane])
  935. break;
  936. }
  937. return i;
  938. }
  939. void dsi_phy_drv_register(void)
  940. {
  941. platform_driver_register(&dsi_phy_platform_driver);
  942. }
  943. void dsi_phy_drv_unregister(void)
  944. {
  945. platform_driver_unregister(&dsi_phy_platform_driver);
  946. }