phy-qcom-ufs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * Copyright (c) 2013-2015, 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. */
  14. #include "phy-qcom-ufs-i.h"
  15. #define MAX_PROP_NAME 32
  16. #define VDDA_PHY_MIN_UV 1000000
  17. #define VDDA_PHY_MAX_UV 1000000
  18. #define VDDA_PLL_MIN_UV 1800000
  19. #define VDDA_PLL_MAX_UV 1800000
  20. #define VDDP_REF_CLK_MIN_UV 1200000
  21. #define VDDP_REF_CLK_MAX_UV 1200000
  22. int ufs_qcom_phy_calibrate(struct ufs_qcom_phy *ufs_qcom_phy,
  23. struct ufs_qcom_phy_calibration *tbl_A,
  24. int tbl_size_A,
  25. struct ufs_qcom_phy_calibration *tbl_B,
  26. int tbl_size_B, bool is_rate_B)
  27. {
  28. int i;
  29. int ret = 0;
  30. if (!tbl_A) {
  31. dev_err(ufs_qcom_phy->dev, "%s: tbl_A is NULL", __func__);
  32. ret = EINVAL;
  33. goto out;
  34. }
  35. for (i = 0; i < tbl_size_A; i++)
  36. writel_relaxed(tbl_A[i].cfg_value,
  37. ufs_qcom_phy->mmio + tbl_A[i].reg_offset);
  38. /*
  39. * In case we would like to work in rate B, we need
  40. * to override a registers that were configured in rate A table
  41. * with registers of rate B table.
  42. * table.
  43. */
  44. if (is_rate_B) {
  45. if (!tbl_B) {
  46. dev_err(ufs_qcom_phy->dev, "%s: tbl_B is NULL",
  47. __func__);
  48. ret = EINVAL;
  49. goto out;
  50. }
  51. for (i = 0; i < tbl_size_B; i++)
  52. writel_relaxed(tbl_B[i].cfg_value,
  53. ufs_qcom_phy->mmio + tbl_B[i].reg_offset);
  54. }
  55. /* flush buffered writes */
  56. mb();
  57. out:
  58. return ret;
  59. }
  60. EXPORT_SYMBOL_GPL(ufs_qcom_phy_calibrate);
  61. /*
  62. * This assumes the embedded phy structure inside generic_phy is of type
  63. * struct ufs_qcom_phy. In order to function properly it's crucial
  64. * to keep the embedded struct "struct ufs_qcom_phy common_cfg"
  65. * as the first inside generic_phy.
  66. */
  67. struct ufs_qcom_phy *get_ufs_qcom_phy(struct phy *generic_phy)
  68. {
  69. return (struct ufs_qcom_phy *)phy_get_drvdata(generic_phy);
  70. }
  71. EXPORT_SYMBOL_GPL(get_ufs_qcom_phy);
  72. static
  73. int ufs_qcom_phy_base_init(struct platform_device *pdev,
  74. struct ufs_qcom_phy *phy_common)
  75. {
  76. struct device *dev = &pdev->dev;
  77. struct resource *res;
  78. int err = 0;
  79. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_mem");
  80. phy_common->mmio = devm_ioremap_resource(dev, res);
  81. if (IS_ERR((void const *)phy_common->mmio)) {
  82. err = PTR_ERR((void const *)phy_common->mmio);
  83. phy_common->mmio = NULL;
  84. dev_err(dev, "%s: ioremap for phy_mem resource failed %d\n",
  85. __func__, err);
  86. return err;
  87. }
  88. /* "dev_ref_clk_ctrl_mem" is optional resource */
  89. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  90. "dev_ref_clk_ctrl_mem");
  91. phy_common->dev_ref_clk_ctrl_mmio = devm_ioremap_resource(dev, res);
  92. if (IS_ERR((void const *)phy_common->dev_ref_clk_ctrl_mmio))
  93. phy_common->dev_ref_clk_ctrl_mmio = NULL;
  94. return 0;
  95. }
  96. struct phy *ufs_qcom_phy_generic_probe(struct platform_device *pdev,
  97. struct ufs_qcom_phy *common_cfg,
  98. const struct phy_ops *ufs_qcom_phy_gen_ops,
  99. struct ufs_qcom_phy_specific_ops *phy_spec_ops)
  100. {
  101. int err;
  102. struct device *dev = &pdev->dev;
  103. struct phy *generic_phy = NULL;
  104. struct phy_provider *phy_provider;
  105. err = ufs_qcom_phy_base_init(pdev, common_cfg);
  106. if (err) {
  107. dev_err(dev, "%s: phy base init failed %d\n", __func__, err);
  108. goto out;
  109. }
  110. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  111. if (IS_ERR(phy_provider)) {
  112. err = PTR_ERR(phy_provider);
  113. dev_err(dev, "%s: failed to register phy %d\n", __func__, err);
  114. goto out;
  115. }
  116. generic_phy = devm_phy_create(dev, NULL, ufs_qcom_phy_gen_ops);
  117. if (IS_ERR(generic_phy)) {
  118. err = PTR_ERR(generic_phy);
  119. dev_err(dev, "%s: failed to create phy %d\n", __func__, err);
  120. generic_phy = NULL;
  121. goto out;
  122. }
  123. common_cfg->phy_spec_ops = phy_spec_ops;
  124. common_cfg->dev = dev;
  125. out:
  126. return generic_phy;
  127. }
  128. EXPORT_SYMBOL_GPL(ufs_qcom_phy_generic_probe);
  129. static int __ufs_qcom_phy_clk_get(struct device *dev,
  130. const char *name, struct clk **clk_out, bool err_print)
  131. {
  132. struct clk *clk;
  133. int err = 0;
  134. clk = devm_clk_get(dev, name);
  135. if (IS_ERR(clk)) {
  136. err = PTR_ERR(clk);
  137. if (err_print)
  138. dev_err(dev, "failed to get %s err %d", name, err);
  139. } else {
  140. *clk_out = clk;
  141. }
  142. return err;
  143. }
  144. static int ufs_qcom_phy_clk_get(struct device *dev,
  145. const char *name, struct clk **clk_out)
  146. {
  147. return __ufs_qcom_phy_clk_get(dev, name, clk_out, true);
  148. }
  149. int ufs_qcom_phy_init_clks(struct ufs_qcom_phy *phy_common)
  150. {
  151. int err;
  152. if (of_device_is_compatible(phy_common->dev->of_node,
  153. "qcom,msm8996-ufs-phy-qmp-14nm"))
  154. goto skip_txrx_clk;
  155. err = ufs_qcom_phy_clk_get(phy_common->dev, "tx_iface_clk",
  156. &phy_common->tx_iface_clk);
  157. if (err)
  158. goto out;
  159. err = ufs_qcom_phy_clk_get(phy_common->dev, "rx_iface_clk",
  160. &phy_common->rx_iface_clk);
  161. if (err)
  162. goto out;
  163. skip_txrx_clk:
  164. err = ufs_qcom_phy_clk_get(phy_common->dev, "ref_clk_src",
  165. &phy_common->ref_clk_src);
  166. if (err)
  167. goto out;
  168. /*
  169. * "ref_clk_parent" is optional hence don't abort init if it's not
  170. * found.
  171. */
  172. __ufs_qcom_phy_clk_get(phy_common->dev, "ref_clk_parent",
  173. &phy_common->ref_clk_parent, false);
  174. err = ufs_qcom_phy_clk_get(phy_common->dev, "ref_clk",
  175. &phy_common->ref_clk);
  176. out:
  177. return err;
  178. }
  179. EXPORT_SYMBOL_GPL(ufs_qcom_phy_init_clks);
  180. static int ufs_qcom_phy_init_vreg(struct device *dev,
  181. struct ufs_qcom_phy_vreg *vreg,
  182. const char *name)
  183. {
  184. int err = 0;
  185. char prop_name[MAX_PROP_NAME];
  186. vreg->name = name;
  187. vreg->reg = devm_regulator_get(dev, name);
  188. if (IS_ERR(vreg->reg)) {
  189. err = PTR_ERR(vreg->reg);
  190. dev_err(dev, "failed to get %s, %d\n", name, err);
  191. goto out;
  192. }
  193. if (dev->of_node) {
  194. snprintf(prop_name, MAX_PROP_NAME, "%s-max-microamp", name);
  195. err = of_property_read_u32(dev->of_node,
  196. prop_name, &vreg->max_uA);
  197. if (err && err != -EINVAL) {
  198. dev_err(dev, "%s: failed to read %s\n",
  199. __func__, prop_name);
  200. goto out;
  201. } else if (err == -EINVAL || !vreg->max_uA) {
  202. if (regulator_count_voltages(vreg->reg) > 0) {
  203. dev_err(dev, "%s: %s is mandatory\n",
  204. __func__, prop_name);
  205. goto out;
  206. }
  207. err = 0;
  208. }
  209. }
  210. if (!strcmp(name, "vdda-pll")) {
  211. vreg->max_uV = VDDA_PLL_MAX_UV;
  212. vreg->min_uV = VDDA_PLL_MIN_UV;
  213. } else if (!strcmp(name, "vdda-phy")) {
  214. vreg->max_uV = VDDA_PHY_MAX_UV;
  215. vreg->min_uV = VDDA_PHY_MIN_UV;
  216. } else if (!strcmp(name, "vddp-ref-clk")) {
  217. vreg->max_uV = VDDP_REF_CLK_MAX_UV;
  218. vreg->min_uV = VDDP_REF_CLK_MIN_UV;
  219. }
  220. out:
  221. return err;
  222. }
  223. int ufs_qcom_phy_init_vregulators(struct ufs_qcom_phy *phy_common)
  224. {
  225. int err;
  226. err = ufs_qcom_phy_init_vreg(phy_common->dev, &phy_common->vdda_pll,
  227. "vdda-pll");
  228. if (err)
  229. goto out;
  230. err = ufs_qcom_phy_init_vreg(phy_common->dev, &phy_common->vdda_phy,
  231. "vdda-phy");
  232. if (err)
  233. goto out;
  234. err = ufs_qcom_phy_init_vreg(phy_common->dev, &phy_common->vddp_ref_clk,
  235. "vddp-ref-clk");
  236. out:
  237. return err;
  238. }
  239. EXPORT_SYMBOL_GPL(ufs_qcom_phy_init_vregulators);
  240. static int ufs_qcom_phy_cfg_vreg(struct device *dev,
  241. struct ufs_qcom_phy_vreg *vreg, bool on)
  242. {
  243. int ret = 0;
  244. struct regulator *reg = vreg->reg;
  245. const char *name = vreg->name;
  246. int min_uV;
  247. int uA_load;
  248. if (regulator_count_voltages(reg) > 0) {
  249. min_uV = on ? vreg->min_uV : 0;
  250. ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
  251. if (ret) {
  252. dev_err(dev, "%s: %s set voltage failed, err=%d\n",
  253. __func__, name, ret);
  254. goto out;
  255. }
  256. uA_load = on ? vreg->max_uA : 0;
  257. ret = regulator_set_load(reg, uA_load);
  258. if (ret >= 0) {
  259. /*
  260. * regulator_set_load() returns new regulator
  261. * mode upon success.
  262. */
  263. ret = 0;
  264. } else {
  265. dev_err(dev, "%s: %s set optimum mode(uA_load=%d) failed, err=%d\n",
  266. __func__, name, uA_load, ret);
  267. goto out;
  268. }
  269. }
  270. out:
  271. return ret;
  272. }
  273. static int ufs_qcom_phy_enable_vreg(struct device *dev,
  274. struct ufs_qcom_phy_vreg *vreg)
  275. {
  276. int ret = 0;
  277. if (!vreg || vreg->enabled)
  278. goto out;
  279. ret = ufs_qcom_phy_cfg_vreg(dev, vreg, true);
  280. if (ret) {
  281. dev_err(dev, "%s: ufs_qcom_phy_cfg_vreg() failed, err=%d\n",
  282. __func__, ret);
  283. goto out;
  284. }
  285. ret = regulator_enable(vreg->reg);
  286. if (ret) {
  287. dev_err(dev, "%s: enable failed, err=%d\n",
  288. __func__, ret);
  289. goto out;
  290. }
  291. vreg->enabled = true;
  292. out:
  293. return ret;
  294. }
  295. static int ufs_qcom_phy_enable_ref_clk(struct ufs_qcom_phy *phy)
  296. {
  297. int ret = 0;
  298. if (phy->is_ref_clk_enabled)
  299. goto out;
  300. /*
  301. * reference clock is propagated in a daisy-chained manner from
  302. * source to phy, so ungate them at each stage.
  303. */
  304. ret = clk_prepare_enable(phy->ref_clk_src);
  305. if (ret) {
  306. dev_err(phy->dev, "%s: ref_clk_src enable failed %d\n",
  307. __func__, ret);
  308. goto out;
  309. }
  310. /*
  311. * "ref_clk_parent" is optional clock hence make sure that clk reference
  312. * is available before trying to enable the clock.
  313. */
  314. if (phy->ref_clk_parent) {
  315. ret = clk_prepare_enable(phy->ref_clk_parent);
  316. if (ret) {
  317. dev_err(phy->dev, "%s: ref_clk_parent enable failed %d\n",
  318. __func__, ret);
  319. goto out_disable_src;
  320. }
  321. }
  322. ret = clk_prepare_enable(phy->ref_clk);
  323. if (ret) {
  324. dev_err(phy->dev, "%s: ref_clk enable failed %d\n",
  325. __func__, ret);
  326. goto out_disable_parent;
  327. }
  328. phy->is_ref_clk_enabled = true;
  329. goto out;
  330. out_disable_parent:
  331. if (phy->ref_clk_parent)
  332. clk_disable_unprepare(phy->ref_clk_parent);
  333. out_disable_src:
  334. clk_disable_unprepare(phy->ref_clk_src);
  335. out:
  336. return ret;
  337. }
  338. static int ufs_qcom_phy_disable_vreg(struct device *dev,
  339. struct ufs_qcom_phy_vreg *vreg)
  340. {
  341. int ret = 0;
  342. if (!vreg || !vreg->enabled)
  343. goto out;
  344. ret = regulator_disable(vreg->reg);
  345. if (!ret) {
  346. /* ignore errors on applying disable config */
  347. ufs_qcom_phy_cfg_vreg(dev, vreg, false);
  348. vreg->enabled = false;
  349. } else {
  350. dev_err(dev, "%s: %s disable failed, err=%d\n",
  351. __func__, vreg->name, ret);
  352. }
  353. out:
  354. return ret;
  355. }
  356. static void ufs_qcom_phy_disable_ref_clk(struct ufs_qcom_phy *phy)
  357. {
  358. if (phy->is_ref_clk_enabled) {
  359. clk_disable_unprepare(phy->ref_clk);
  360. /*
  361. * "ref_clk_parent" is optional clock hence make sure that clk
  362. * reference is available before trying to disable the clock.
  363. */
  364. if (phy->ref_clk_parent)
  365. clk_disable_unprepare(phy->ref_clk_parent);
  366. clk_disable_unprepare(phy->ref_clk_src);
  367. phy->is_ref_clk_enabled = false;
  368. }
  369. }
  370. #define UFS_REF_CLK_EN (1 << 5)
  371. static void ufs_qcom_phy_dev_ref_clk_ctrl(struct phy *generic_phy, bool enable)
  372. {
  373. struct ufs_qcom_phy *phy = get_ufs_qcom_phy(generic_phy);
  374. if (phy->dev_ref_clk_ctrl_mmio &&
  375. (enable ^ phy->is_dev_ref_clk_enabled)) {
  376. u32 temp = readl_relaxed(phy->dev_ref_clk_ctrl_mmio);
  377. if (enable)
  378. temp |= UFS_REF_CLK_EN;
  379. else
  380. temp &= ~UFS_REF_CLK_EN;
  381. /*
  382. * If we are here to disable this clock immediately after
  383. * entering into hibern8, we need to make sure that device
  384. * ref_clk is active atleast 1us after the hibern8 enter.
  385. */
  386. if (!enable)
  387. udelay(1);
  388. writel_relaxed(temp, phy->dev_ref_clk_ctrl_mmio);
  389. /* ensure that ref_clk is enabled/disabled before we return */
  390. wmb();
  391. /*
  392. * If we call hibern8 exit after this, we need to make sure that
  393. * device ref_clk is stable for atleast 1us before the hibern8
  394. * exit command.
  395. */
  396. if (enable)
  397. udelay(1);
  398. phy->is_dev_ref_clk_enabled = enable;
  399. }
  400. }
  401. void ufs_qcom_phy_enable_dev_ref_clk(struct phy *generic_phy)
  402. {
  403. ufs_qcom_phy_dev_ref_clk_ctrl(generic_phy, true);
  404. }
  405. EXPORT_SYMBOL_GPL(ufs_qcom_phy_enable_dev_ref_clk);
  406. void ufs_qcom_phy_disable_dev_ref_clk(struct phy *generic_phy)
  407. {
  408. ufs_qcom_phy_dev_ref_clk_ctrl(generic_phy, false);
  409. }
  410. EXPORT_SYMBOL_GPL(ufs_qcom_phy_disable_dev_ref_clk);
  411. /* Turn ON M-PHY RMMI interface clocks */
  412. static int ufs_qcom_phy_enable_iface_clk(struct ufs_qcom_phy *phy)
  413. {
  414. int ret = 0;
  415. if (phy->is_iface_clk_enabled)
  416. goto out;
  417. ret = clk_prepare_enable(phy->tx_iface_clk);
  418. if (ret) {
  419. dev_err(phy->dev, "%s: tx_iface_clk enable failed %d\n",
  420. __func__, ret);
  421. goto out;
  422. }
  423. ret = clk_prepare_enable(phy->rx_iface_clk);
  424. if (ret) {
  425. clk_disable_unprepare(phy->tx_iface_clk);
  426. dev_err(phy->dev, "%s: rx_iface_clk enable failed %d. disabling also tx_iface_clk\n",
  427. __func__, ret);
  428. goto out;
  429. }
  430. phy->is_iface_clk_enabled = true;
  431. out:
  432. return ret;
  433. }
  434. /* Turn OFF M-PHY RMMI interface clocks */
  435. void ufs_qcom_phy_disable_iface_clk(struct ufs_qcom_phy *phy)
  436. {
  437. if (phy->is_iface_clk_enabled) {
  438. clk_disable_unprepare(phy->tx_iface_clk);
  439. clk_disable_unprepare(phy->rx_iface_clk);
  440. phy->is_iface_clk_enabled = false;
  441. }
  442. }
  443. int ufs_qcom_phy_start_serdes(struct phy *generic_phy)
  444. {
  445. struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
  446. int ret = 0;
  447. if (!ufs_qcom_phy->phy_spec_ops->start_serdes) {
  448. dev_err(ufs_qcom_phy->dev, "%s: start_serdes() callback is not supported\n",
  449. __func__);
  450. ret = -ENOTSUPP;
  451. } else {
  452. ufs_qcom_phy->phy_spec_ops->start_serdes(ufs_qcom_phy);
  453. }
  454. return ret;
  455. }
  456. EXPORT_SYMBOL_GPL(ufs_qcom_phy_start_serdes);
  457. int ufs_qcom_phy_set_tx_lane_enable(struct phy *generic_phy, u32 tx_lanes)
  458. {
  459. struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
  460. int ret = 0;
  461. if (!ufs_qcom_phy->phy_spec_ops->set_tx_lane_enable) {
  462. dev_err(ufs_qcom_phy->dev, "%s: set_tx_lane_enable() callback is not supported\n",
  463. __func__);
  464. ret = -ENOTSUPP;
  465. } else {
  466. ufs_qcom_phy->phy_spec_ops->set_tx_lane_enable(ufs_qcom_phy,
  467. tx_lanes);
  468. }
  469. return ret;
  470. }
  471. EXPORT_SYMBOL_GPL(ufs_qcom_phy_set_tx_lane_enable);
  472. void ufs_qcom_phy_save_controller_version(struct phy *generic_phy,
  473. u8 major, u16 minor, u16 step)
  474. {
  475. struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
  476. ufs_qcom_phy->host_ctrl_rev_major = major;
  477. ufs_qcom_phy->host_ctrl_rev_minor = minor;
  478. ufs_qcom_phy->host_ctrl_rev_step = step;
  479. }
  480. EXPORT_SYMBOL_GPL(ufs_qcom_phy_save_controller_version);
  481. int ufs_qcom_phy_calibrate_phy(struct phy *generic_phy, bool is_rate_B)
  482. {
  483. struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
  484. int ret = 0;
  485. if (!ufs_qcom_phy->phy_spec_ops->calibrate_phy) {
  486. dev_err(ufs_qcom_phy->dev, "%s: calibrate_phy() callback is not supported\n",
  487. __func__);
  488. ret = -ENOTSUPP;
  489. } else {
  490. ret = ufs_qcom_phy->phy_spec_ops->
  491. calibrate_phy(ufs_qcom_phy, is_rate_B);
  492. if (ret)
  493. dev_err(ufs_qcom_phy->dev, "%s: calibrate_phy() failed %d\n",
  494. __func__, ret);
  495. }
  496. return ret;
  497. }
  498. EXPORT_SYMBOL_GPL(ufs_qcom_phy_calibrate_phy);
  499. int ufs_qcom_phy_is_pcs_ready(struct phy *generic_phy)
  500. {
  501. struct ufs_qcom_phy *ufs_qcom_phy = get_ufs_qcom_phy(generic_phy);
  502. if (!ufs_qcom_phy->phy_spec_ops->is_physical_coding_sublayer_ready) {
  503. dev_err(ufs_qcom_phy->dev, "%s: is_physical_coding_sublayer_ready() callback is not supported\n",
  504. __func__);
  505. return -ENOTSUPP;
  506. }
  507. return ufs_qcom_phy->phy_spec_ops->
  508. is_physical_coding_sublayer_ready(ufs_qcom_phy);
  509. }
  510. EXPORT_SYMBOL_GPL(ufs_qcom_phy_is_pcs_ready);
  511. int ufs_qcom_phy_power_on(struct phy *generic_phy)
  512. {
  513. struct ufs_qcom_phy *phy_common = get_ufs_qcom_phy(generic_phy);
  514. struct device *dev = phy_common->dev;
  515. int err;
  516. if (phy_common->is_powered_on)
  517. return 0;
  518. err = ufs_qcom_phy_enable_vreg(dev, &phy_common->vdda_phy);
  519. if (err) {
  520. dev_err(dev, "%s enable vdda_phy failed, err=%d\n",
  521. __func__, err);
  522. goto out;
  523. }
  524. phy_common->phy_spec_ops->power_control(phy_common, true);
  525. /* vdda_pll also enables ref clock LDOs so enable it first */
  526. err = ufs_qcom_phy_enable_vreg(dev, &phy_common->vdda_pll);
  527. if (err) {
  528. dev_err(dev, "%s enable vdda_pll failed, err=%d\n",
  529. __func__, err);
  530. goto out_disable_phy;
  531. }
  532. err = ufs_qcom_phy_enable_iface_clk(phy_common);
  533. if (err) {
  534. dev_err(dev, "%s enable phy iface clock failed, err=%d\n",
  535. __func__, err);
  536. goto out_disable_pll;
  537. }
  538. err = ufs_qcom_phy_enable_ref_clk(phy_common);
  539. if (err) {
  540. dev_err(dev, "%s enable phy ref clock failed, err=%d\n",
  541. __func__, err);
  542. goto out_disable_iface_clk;
  543. }
  544. /* enable device PHY ref_clk pad rail */
  545. if (phy_common->vddp_ref_clk.reg) {
  546. err = ufs_qcom_phy_enable_vreg(dev,
  547. &phy_common->vddp_ref_clk);
  548. if (err) {
  549. dev_err(dev, "%s enable vddp_ref_clk failed, err=%d\n",
  550. __func__, err);
  551. goto out_disable_ref_clk;
  552. }
  553. }
  554. phy_common->is_powered_on = true;
  555. goto out;
  556. out_disable_ref_clk:
  557. ufs_qcom_phy_disable_ref_clk(phy_common);
  558. out_disable_iface_clk:
  559. ufs_qcom_phy_disable_iface_clk(phy_common);
  560. out_disable_pll:
  561. ufs_qcom_phy_disable_vreg(dev, &phy_common->vdda_pll);
  562. out_disable_phy:
  563. ufs_qcom_phy_disable_vreg(dev, &phy_common->vdda_phy);
  564. out:
  565. return err;
  566. }
  567. EXPORT_SYMBOL_GPL(ufs_qcom_phy_power_on);
  568. int ufs_qcom_phy_power_off(struct phy *generic_phy)
  569. {
  570. struct ufs_qcom_phy *phy_common = get_ufs_qcom_phy(generic_phy);
  571. if (!phy_common->is_powered_on)
  572. return 0;
  573. phy_common->phy_spec_ops->power_control(phy_common, false);
  574. if (phy_common->vddp_ref_clk.reg)
  575. ufs_qcom_phy_disable_vreg(phy_common->dev,
  576. &phy_common->vddp_ref_clk);
  577. ufs_qcom_phy_disable_ref_clk(phy_common);
  578. ufs_qcom_phy_disable_iface_clk(phy_common);
  579. ufs_qcom_phy_disable_vreg(phy_common->dev, &phy_common->vdda_pll);
  580. ufs_qcom_phy_disable_vreg(phy_common->dev, &phy_common->vdda_phy);
  581. phy_common->is_powered_on = false;
  582. return 0;
  583. }
  584. EXPORT_SYMBOL_GPL(ufs_qcom_phy_power_off);
  585. MODULE_AUTHOR("Yaniv Gardi <ygardi@codeaurora.org>");
  586. MODULE_AUTHOR("Vivek Gautam <vivek.gautam@codeaurora.org>");
  587. MODULE_DESCRIPTION("Universal Flash Storage (UFS) QCOM PHY");
  588. MODULE_LICENSE("GPL v2");