phy-qcom-ufs.c 19 KB

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