dsi_phy.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. return 0;
  504. }
  505. /**
  506. * dsi_phy_drv_deinit() - de-initialize dsi phy driver
  507. * @dsi_phy: DSI PHY handle.
  508. *
  509. * Release all resources acquired by dsi_phy_drv_init().
  510. *
  511. * Return: error code.
  512. */
  513. int dsi_phy_drv_deinit(struct msm_dsi_phy *dsi_phy)
  514. {
  515. return 0;
  516. }
  517. int dsi_phy_clk_cb_register(struct msm_dsi_phy *dsi_phy,
  518. struct clk_ctrl_cb *clk_cb)
  519. {
  520. if (!dsi_phy || !clk_cb) {
  521. pr_err("Invalid params\n");
  522. return -EINVAL;
  523. }
  524. dsi_phy->clk_cb.priv = clk_cb->priv;
  525. dsi_phy->clk_cb.dsi_clk_cb = clk_cb->dsi_clk_cb;
  526. return 0;
  527. }
  528. /**
  529. * dsi_phy_validate_mode() - validate a display mode
  530. * @dsi_phy: DSI PHY handle.
  531. * @mode: Mode information.
  532. *
  533. * Validation will fail if the mode cannot be supported by the PHY driver or
  534. * hardware.
  535. *
  536. * Return: error code.
  537. */
  538. int dsi_phy_validate_mode(struct msm_dsi_phy *dsi_phy,
  539. struct dsi_mode_info *mode)
  540. {
  541. int rc = 0;
  542. if (!dsi_phy || !mode) {
  543. pr_err("Invalid params\n");
  544. return -EINVAL;
  545. }
  546. mutex_lock(&dsi_phy->phy_lock);
  547. pr_debug("[PHY_%d] Skipping validation\n", dsi_phy->index);
  548. mutex_unlock(&dsi_phy->phy_lock);
  549. return rc;
  550. }
  551. /**
  552. * dsi_phy_set_power_state() - enable/disable dsi phy power supplies
  553. * @dsi_phy: DSI PHY handle.
  554. * @enable: Boolean flag to enable/disable.
  555. *
  556. * Return: error code.
  557. */
  558. int dsi_phy_set_power_state(struct msm_dsi_phy *dsi_phy, bool enable)
  559. {
  560. int rc = 0;
  561. if (!dsi_phy) {
  562. pr_err("Invalid params\n");
  563. return -EINVAL;
  564. }
  565. mutex_lock(&dsi_phy->phy_lock);
  566. if (enable == dsi_phy->power_state) {
  567. pr_err("[PHY_%d] No state change\n", dsi_phy->index);
  568. goto error;
  569. }
  570. if (enable) {
  571. rc = dsi_pwr_enable_regulator(&dsi_phy->pwr_info.digital, true);
  572. if (rc) {
  573. pr_err("failed to enable digital regulator\n");
  574. goto error;
  575. }
  576. if (dsi_phy->dsi_phy_state == DSI_PHY_ENGINE_OFF &&
  577. dsi_phy->regulator_required) {
  578. rc = dsi_pwr_enable_regulator(
  579. &dsi_phy->pwr_info.phy_pwr, true);
  580. if (rc) {
  581. pr_err("failed to enable phy power\n");
  582. (void)dsi_pwr_enable_regulator(
  583. &dsi_phy->pwr_info.digital, false);
  584. goto error;
  585. }
  586. }
  587. } else {
  588. if (dsi_phy->dsi_phy_state == DSI_PHY_ENGINE_OFF &&
  589. dsi_phy->regulator_required) {
  590. rc = dsi_pwr_enable_regulator(
  591. &dsi_phy->pwr_info.phy_pwr, false);
  592. if (rc) {
  593. pr_err("failed to enable digital regulator\n");
  594. goto error;
  595. }
  596. }
  597. rc = dsi_pwr_enable_regulator(&dsi_phy->pwr_info.digital,
  598. false);
  599. if (rc) {
  600. pr_err("failed to enable phy power\n");
  601. goto error;
  602. }
  603. }
  604. dsi_phy->power_state = enable;
  605. error:
  606. mutex_unlock(&dsi_phy->phy_lock);
  607. return rc;
  608. }
  609. static int dsi_phy_enable_ulps(struct msm_dsi_phy *phy,
  610. struct dsi_host_config *config, bool clamp_enabled)
  611. {
  612. int rc = 0;
  613. u32 lanes = 0;
  614. u32 ulps_lanes;
  615. lanes = config->common_config.data_lanes;
  616. lanes |= DSI_CLOCK_LANE;
  617. /*
  618. * If DSI clamps are enabled, it means that the DSI lanes are
  619. * already in idle state. Checking for lanes to be in idle state
  620. * should be skipped during ULPS entry programming while coming
  621. * out of idle screen.
  622. */
  623. if (!clamp_enabled) {
  624. rc = phy->hw.ops.ulps_ops.wait_for_lane_idle(&phy->hw, lanes);
  625. if (rc) {
  626. pr_err("lanes not entering idle, skip ULPS\n");
  627. return rc;
  628. }
  629. }
  630. phy->hw.ops.ulps_ops.ulps_request(&phy->hw, &phy->cfg, lanes);
  631. ulps_lanes = phy->hw.ops.ulps_ops.get_lanes_in_ulps(&phy->hw);
  632. if (!phy->hw.ops.ulps_ops.is_lanes_in_ulps(lanes, ulps_lanes)) {
  633. pr_err("Failed to enter ULPS, request=0x%x, actual=0x%x\n",
  634. lanes, ulps_lanes);
  635. rc = -EIO;
  636. }
  637. return rc;
  638. }
  639. static int dsi_phy_disable_ulps(struct msm_dsi_phy *phy,
  640. struct dsi_host_config *config)
  641. {
  642. u32 ulps_lanes, lanes = 0;
  643. lanes = config->common_config.data_lanes;
  644. lanes |= DSI_CLOCK_LANE;
  645. ulps_lanes = phy->hw.ops.ulps_ops.get_lanes_in_ulps(&phy->hw);
  646. if (!phy->hw.ops.ulps_ops.is_lanes_in_ulps(lanes, ulps_lanes)) {
  647. pr_err("Mismatch in ULPS: lanes:%d, ulps_lanes:%d\n",
  648. lanes, ulps_lanes);
  649. return -EIO;
  650. }
  651. phy->hw.ops.ulps_ops.ulps_exit(&phy->hw, &phy->cfg, lanes);
  652. ulps_lanes = phy->hw.ops.ulps_ops.get_lanes_in_ulps(&phy->hw);
  653. if (phy->hw.ops.ulps_ops.is_lanes_in_ulps(lanes, ulps_lanes)) {
  654. pr_err("Lanes (0x%x) stuck in ULPS\n", ulps_lanes);
  655. return -EIO;
  656. }
  657. return 0;
  658. }
  659. void dsi_phy_toggle_resync_fifo(struct msm_dsi_phy *phy)
  660. {
  661. if (!phy)
  662. return;
  663. if (!phy->hw.ops.toggle_resync_fifo)
  664. return;
  665. phy->hw.ops.toggle_resync_fifo(&phy->hw);
  666. }
  667. void dsi_phy_reset_clk_en_sel(struct msm_dsi_phy *phy)
  668. {
  669. if (!phy)
  670. return;
  671. if (!phy->hw.ops.reset_clk_en_sel)
  672. return;
  673. phy->hw.ops.reset_clk_en_sel(&phy->hw);
  674. }
  675. int dsi_phy_set_ulps(struct msm_dsi_phy *phy, struct dsi_host_config *config,
  676. bool enable, bool clamp_enabled)
  677. {
  678. int rc = 0;
  679. if (!phy) {
  680. pr_err("Invalid params\n");
  681. return DSI_PHY_ULPS_ERROR;
  682. }
  683. if (!phy->hw.ops.ulps_ops.ulps_request ||
  684. !phy->hw.ops.ulps_ops.ulps_exit ||
  685. !phy->hw.ops.ulps_ops.get_lanes_in_ulps ||
  686. !phy->hw.ops.ulps_ops.is_lanes_in_ulps ||
  687. !phy->hw.ops.ulps_ops.wait_for_lane_idle) {
  688. pr_debug("DSI PHY ULPS ops not present\n");
  689. return DSI_PHY_ULPS_NOT_HANDLED;
  690. }
  691. mutex_lock(&phy->phy_lock);
  692. if (enable)
  693. rc = dsi_phy_enable_ulps(phy, config, clamp_enabled);
  694. else
  695. rc = dsi_phy_disable_ulps(phy, config);
  696. if (rc) {
  697. pr_err("[DSI_PHY%d] Ulps state change(%d) failed, rc=%d\n",
  698. phy->index, enable, rc);
  699. rc = DSI_PHY_ULPS_ERROR;
  700. goto error;
  701. }
  702. pr_debug("[DSI_PHY%d] ULPS state = %d\n", phy->index, enable);
  703. error:
  704. mutex_unlock(&phy->phy_lock);
  705. return rc;
  706. }
  707. /**
  708. * dsi_phy_enable() - enable DSI PHY hardware
  709. * @dsi_phy: DSI PHY handle.
  710. * @config: DSI host configuration.
  711. * @pll_source: Source PLL for PHY clock.
  712. * @skip_validation: Validation will not be performed on parameters.
  713. * @is_cont_splash_enabled: check whether continuous splash enabled.
  714. *
  715. * Validates and enables DSI PHY.
  716. *
  717. * Return: error code.
  718. */
  719. int dsi_phy_enable(struct msm_dsi_phy *phy,
  720. struct dsi_host_config *config,
  721. enum dsi_phy_pll_source pll_source,
  722. bool skip_validation,
  723. bool is_cont_splash_enabled)
  724. {
  725. int rc = 0;
  726. if (!phy || !config) {
  727. pr_err("Invalid params\n");
  728. return -EINVAL;
  729. }
  730. mutex_lock(&phy->phy_lock);
  731. if (!skip_validation)
  732. pr_debug("[PHY_%d] TODO: perform validation\n", phy->index);
  733. memcpy(&phy->mode, &config->video_timing, sizeof(phy->mode));
  734. memcpy(&phy->cfg.lane_map, &config->lane_map, sizeof(config->lane_map));
  735. phy->data_lanes = config->common_config.data_lanes;
  736. phy->dst_format = config->common_config.dst_format;
  737. phy->cfg.pll_source = pll_source;
  738. phy->cfg.bit_clk_rate_hz = config->bit_clk_rate_hz;
  739. /**
  740. * If PHY timing parameters are not present in panel dtsi file,
  741. * then calculate them in the driver
  742. */
  743. if (!phy->cfg.is_phy_timing_present)
  744. rc = phy->hw.ops.calculate_timing_params(&phy->hw,
  745. &phy->mode,
  746. &config->common_config,
  747. &phy->cfg.timing);
  748. if (rc) {
  749. pr_err("[%s] failed to set timing, rc=%d\n", phy->name, rc);
  750. goto error;
  751. }
  752. if (!is_cont_splash_enabled) {
  753. dsi_phy_enable_hw(phy);
  754. pr_debug("cont splash not enabled, phy enable required\n");
  755. }
  756. phy->dsi_phy_state = DSI_PHY_ENGINE_ON;
  757. error:
  758. mutex_unlock(&phy->phy_lock);
  759. return rc;
  760. }
  761. int dsi_phy_lane_reset(struct msm_dsi_phy *phy)
  762. {
  763. int ret = 0;
  764. if (!phy)
  765. return ret;
  766. mutex_lock(&phy->phy_lock);
  767. if (phy->hw.ops.phy_lane_reset)
  768. ret = phy->hw.ops.phy_lane_reset(&phy->hw);
  769. mutex_unlock(&phy->phy_lock);
  770. return ret;
  771. }
  772. /**
  773. * dsi_phy_disable() - disable DSI PHY hardware.
  774. * @phy: DSI PHY handle.
  775. *
  776. * Return: error code.
  777. */
  778. int dsi_phy_disable(struct msm_dsi_phy *phy)
  779. {
  780. int rc = 0;
  781. if (!phy) {
  782. pr_err("Invalid params\n");
  783. return -EINVAL;
  784. }
  785. mutex_lock(&phy->phy_lock);
  786. dsi_phy_disable_hw(phy);
  787. phy->dsi_phy_state = DSI_PHY_ENGINE_OFF;
  788. mutex_unlock(&phy->phy_lock);
  789. return rc;
  790. }
  791. /**
  792. * dsi_phy_set_clamp_state() - configure clamps for DSI lanes
  793. * @phy: DSI PHY handle.
  794. * @enable: boolean to specify clamp enable/disable.
  795. *
  796. * Return: error code.
  797. */
  798. int dsi_phy_set_clamp_state(struct msm_dsi_phy *phy, bool enable)
  799. {
  800. if (!phy)
  801. return -EINVAL;
  802. pr_debug("[%s] enable=%d\n", phy->name, enable);
  803. if (phy->hw.ops.clamp_ctrl)
  804. phy->hw.ops.clamp_ctrl(&phy->hw, enable);
  805. return 0;
  806. }
  807. /**
  808. * dsi_phy_idle_ctrl() - enable/disable DSI PHY during idle screen
  809. * @phy: DSI PHY handle
  810. * @enable: boolean to specify PHY enable/disable.
  811. *
  812. * Return: error code.
  813. */
  814. int dsi_phy_idle_ctrl(struct msm_dsi_phy *phy, bool enable)
  815. {
  816. if (!phy) {
  817. pr_err("Invalid params\n");
  818. return -EINVAL;
  819. }
  820. pr_debug("[%s] enable=%d\n", phy->name, enable);
  821. mutex_lock(&phy->phy_lock);
  822. if (enable) {
  823. if (phy->hw.ops.phy_idle_on)
  824. phy->hw.ops.phy_idle_on(&phy->hw, &phy->cfg);
  825. if (phy->hw.ops.regulator_enable)
  826. phy->hw.ops.regulator_enable(&phy->hw,
  827. &phy->cfg.regulators);
  828. if (phy->hw.ops.enable)
  829. phy->hw.ops.enable(&phy->hw, &phy->cfg);
  830. phy->dsi_phy_state = DSI_PHY_ENGINE_ON;
  831. } else {
  832. phy->dsi_phy_state = DSI_PHY_ENGINE_OFF;
  833. if (phy->hw.ops.disable)
  834. phy->hw.ops.disable(&phy->hw, &phy->cfg);
  835. if (phy->hw.ops.phy_idle_off)
  836. phy->hw.ops.phy_idle_off(&phy->hw);
  837. }
  838. mutex_unlock(&phy->phy_lock);
  839. return 0;
  840. }
  841. /**
  842. * dsi_phy_set_clk_freq() - set DSI PHY clock frequency setting
  843. * @phy: DSI PHY handle
  844. * @clk_freq: link clock frequency
  845. *
  846. * Return: error code.
  847. */
  848. int dsi_phy_set_clk_freq(struct msm_dsi_phy *phy,
  849. struct link_clk_freq *clk_freq)
  850. {
  851. if (!phy || !clk_freq) {
  852. pr_err("Invalid params\n");
  853. return -EINVAL;
  854. }
  855. phy->regulator_required = clk_freq->byte_clk_rate >
  856. (phy->regulator_min_datarate_bps / BITS_PER_BYTE);
  857. /*
  858. * DSI PLL needs 0p9 LDO1A for Powering DSI PLL block.
  859. * PLL driver can vote for this regulator in PLL driver file, but for
  860. * the usecase where we come out of idle(static screen), if PLL and
  861. * PHY vote for regulator ,there will be performance delays as both
  862. * votes go through RPM to enable regulators.
  863. */
  864. phy->regulator_required = true;
  865. pr_debug("[%s] lane_datarate=%u min_datarate=%u required=%d\n",
  866. phy->name,
  867. clk_freq->byte_clk_rate * BITS_PER_BYTE,
  868. phy->regulator_min_datarate_bps,
  869. phy->regulator_required);
  870. return 0;
  871. }
  872. /**
  873. * dsi_phy_set_timing_params() - timing parameters for the panel
  874. * @phy: DSI PHY handle
  875. * @timing: array holding timing params.
  876. * @size: size of the array.
  877. *
  878. * When PHY timing calculator is not implemented, this array will be used to
  879. * pass PHY timing information.
  880. *
  881. * Return: error code.
  882. */
  883. int dsi_phy_set_timing_params(struct msm_dsi_phy *phy,
  884. u32 *timing, u32 size)
  885. {
  886. int rc = 0;
  887. if (!phy || !timing || !size) {
  888. pr_err("Invalid params\n");
  889. return -EINVAL;
  890. }
  891. mutex_lock(&phy->phy_lock);
  892. if (phy->hw.ops.phy_timing_val)
  893. rc = phy->hw.ops.phy_timing_val(&phy->cfg.timing, timing, size);
  894. if (!rc)
  895. phy->cfg.is_phy_timing_present = true;
  896. mutex_unlock(&phy->phy_lock);
  897. return rc;
  898. }
  899. /**
  900. * dsi_phy_conv_phy_to_logical_lane() - Convert physical to logical lane
  901. * @lane_map: logical lane
  902. * @phy_lane: physical lane
  903. *
  904. * Return: Error code on failure. Lane number on success.
  905. */
  906. int dsi_phy_conv_phy_to_logical_lane(
  907. struct dsi_lane_map *lane_map, enum dsi_phy_data_lanes phy_lane)
  908. {
  909. int i = 0;
  910. if (phy_lane > DSI_PHYSICAL_LANE_3)
  911. return -EINVAL;
  912. for (i = DSI_LOGICAL_LANE_0; i < (DSI_LANE_MAX - 1); i++) {
  913. if (lane_map->lane_map_v2[i] == phy_lane)
  914. break;
  915. }
  916. return i;
  917. }
  918. /**
  919. * dsi_phy_conv_logical_to_phy_lane() - Convert logical to physical lane
  920. * @lane_map: physical lane
  921. * @lane: logical lane
  922. *
  923. * Return: Error code on failure. Lane number on success.
  924. */
  925. int dsi_phy_conv_logical_to_phy_lane(
  926. struct dsi_lane_map *lane_map, enum dsi_logical_lane lane)
  927. {
  928. int i = 0;
  929. if (lane > (DSI_LANE_MAX - 1))
  930. return -EINVAL;
  931. for (i = DSI_LOGICAL_LANE_0; i < (DSI_LANE_MAX - 1); i++) {
  932. if (BIT(i) == lane_map->lane_map_v2[lane])
  933. break;
  934. }
  935. return i;
  936. }
  937. void dsi_phy_drv_register(void)
  938. {
  939. platform_driver_register(&dsi_phy_platform_driver);
  940. }
  941. void dsi_phy_drv_unregister(void)
  942. {
  943. platform_driver_unregister(&dsi_phy_platform_driver);
  944. }