phy-fsl-imx8qm-lvds-phy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2017-2020,2022 NXP
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bits.h>
  7. #include <linux/clk.h>
  8. #include <linux/mfd/syscon.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/phy/phy.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/regmap.h>
  15. #include <linux/units.h>
  16. #define REG_SET 0x4
  17. #define REG_CLR 0x8
  18. #define PHY_CTRL 0x0
  19. #define M_MASK GENMASK(18, 17)
  20. #define M(n) FIELD_PREP(M_MASK, (n))
  21. #define CCM_MASK GENMASK(16, 14)
  22. #define CCM(n) FIELD_PREP(CCM_MASK, (n))
  23. #define CA_MASK GENMASK(13, 11)
  24. #define CA(n) FIELD_PREP(CA_MASK, (n))
  25. #define TST_MASK GENMASK(10, 5)
  26. #define TST(n) FIELD_PREP(TST_MASK, (n))
  27. #define CH_EN(id) BIT(3 + (id))
  28. #define NB BIT(2)
  29. #define RFB BIT(1)
  30. #define PD BIT(0)
  31. /* Power On Reset(POR) value */
  32. #define CTRL_RESET_VAL (M(0x0) | CCM(0x4) | CA(0x4) | TST(0x25))
  33. /* PHY initialization value and mask */
  34. #define CTRL_INIT_MASK (M_MASK | CCM_MASK | CA_MASK | TST_MASK | NB | RFB)
  35. #define CTRL_INIT_VAL (M(0x0) | CCM(0x5) | CA(0x4) | TST(0x25) | RFB)
  36. #define PHY_STATUS 0x10
  37. #define LOCK BIT(0)
  38. #define PHY_NUM 2
  39. #define MIN_CLKIN_FREQ (25 * MEGA)
  40. #define MAX_CLKIN_FREQ (165 * MEGA)
  41. #define PLL_LOCK_SLEEP 10
  42. #define PLL_LOCK_TIMEOUT 1000
  43. struct mixel_lvds_phy {
  44. struct phy *phy;
  45. struct phy_configure_opts_lvds cfg;
  46. unsigned int id;
  47. };
  48. struct mixel_lvds_phy_priv {
  49. struct regmap *regmap;
  50. struct mutex lock; /* protect remap access and cfg of our own */
  51. struct clk *phy_ref_clk;
  52. struct mixel_lvds_phy *phys[PHY_NUM];
  53. };
  54. static int mixel_lvds_phy_init(struct phy *phy)
  55. {
  56. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
  57. mutex_lock(&priv->lock);
  58. regmap_update_bits(priv->regmap,
  59. PHY_CTRL, CTRL_INIT_MASK, CTRL_INIT_VAL);
  60. mutex_unlock(&priv->lock);
  61. return 0;
  62. }
  63. static int mixel_lvds_phy_power_on(struct phy *phy)
  64. {
  65. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
  66. struct mixel_lvds_phy *lvds_phy = phy_get_drvdata(phy);
  67. struct mixel_lvds_phy *companion = priv->phys[lvds_phy->id ^ 1];
  68. struct phy_configure_opts_lvds *cfg = &lvds_phy->cfg;
  69. u32 val = 0;
  70. u32 locked;
  71. int ret;
  72. /* The master PHY would power on the slave PHY. */
  73. if (cfg->is_slave)
  74. return 0;
  75. ret = clk_prepare_enable(priv->phy_ref_clk);
  76. if (ret < 0) {
  77. dev_err(&phy->dev,
  78. "failed to enable PHY reference clock: %d\n", ret);
  79. return ret;
  80. }
  81. mutex_lock(&priv->lock);
  82. if (cfg->bits_per_lane_and_dclk_cycle == 7) {
  83. if (cfg->differential_clk_rate < 44000000)
  84. val |= M(0x2);
  85. else if (cfg->differential_clk_rate < 90000000)
  86. val |= M(0x1);
  87. else
  88. val |= M(0x0);
  89. } else {
  90. val = NB;
  91. if (cfg->differential_clk_rate < 32000000)
  92. val |= M(0x2);
  93. else if (cfg->differential_clk_rate < 63000000)
  94. val |= M(0x1);
  95. else
  96. val |= M(0x0);
  97. }
  98. regmap_update_bits(priv->regmap, PHY_CTRL, M_MASK | NB, val);
  99. /*
  100. * Enable two channels synchronously,
  101. * if the companion PHY is a slave PHY.
  102. */
  103. if (companion->cfg.is_slave)
  104. val = CH_EN(0) | CH_EN(1);
  105. else
  106. val = CH_EN(lvds_phy->id);
  107. regmap_write(priv->regmap, PHY_CTRL + REG_SET, val);
  108. ret = regmap_read_poll_timeout(priv->regmap, PHY_STATUS, locked,
  109. locked, PLL_LOCK_SLEEP,
  110. PLL_LOCK_TIMEOUT);
  111. if (ret < 0) {
  112. dev_err(&phy->dev, "failed to get PHY lock: %d\n", ret);
  113. clk_disable_unprepare(priv->phy_ref_clk);
  114. }
  115. mutex_unlock(&priv->lock);
  116. return ret;
  117. }
  118. static int mixel_lvds_phy_power_off(struct phy *phy)
  119. {
  120. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
  121. struct mixel_lvds_phy *lvds_phy = phy_get_drvdata(phy);
  122. struct mixel_lvds_phy *companion = priv->phys[lvds_phy->id ^ 1];
  123. struct phy_configure_opts_lvds *cfg = &lvds_phy->cfg;
  124. /* The master PHY would power off the slave PHY. */
  125. if (cfg->is_slave)
  126. return 0;
  127. mutex_lock(&priv->lock);
  128. if (companion->cfg.is_slave)
  129. regmap_write(priv->regmap, PHY_CTRL + REG_CLR,
  130. CH_EN(0) | CH_EN(1));
  131. else
  132. regmap_write(priv->regmap, PHY_CTRL + REG_CLR,
  133. CH_EN(lvds_phy->id));
  134. mutex_unlock(&priv->lock);
  135. clk_disable_unprepare(priv->phy_ref_clk);
  136. return 0;
  137. }
  138. static int mixel_lvds_phy_configure(struct phy *phy,
  139. union phy_configure_opts *opts)
  140. {
  141. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
  142. struct phy_configure_opts_lvds *cfg = &opts->lvds;
  143. int ret;
  144. ret = clk_set_rate(priv->phy_ref_clk, cfg->differential_clk_rate);
  145. if (ret)
  146. dev_err(&phy->dev, "failed to set PHY reference clock rate(%lu): %d\n",
  147. cfg->differential_clk_rate, ret);
  148. return ret;
  149. }
  150. /* Assume the master PHY's configuration set is cached first. */
  151. static int mixel_lvds_phy_check_slave(struct phy *slave_phy)
  152. {
  153. struct device *dev = &slave_phy->dev;
  154. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev->parent);
  155. struct mixel_lvds_phy *slv = phy_get_drvdata(slave_phy);
  156. struct mixel_lvds_phy *mst = priv->phys[slv->id ^ 1];
  157. struct phy_configure_opts_lvds *mst_cfg = &mst->cfg;
  158. struct phy_configure_opts_lvds *slv_cfg = &slv->cfg;
  159. if (mst_cfg->bits_per_lane_and_dclk_cycle !=
  160. slv_cfg->bits_per_lane_and_dclk_cycle) {
  161. dev_err(dev, "number bits mismatch(mst: %u vs slv: %u)\n",
  162. mst_cfg->bits_per_lane_and_dclk_cycle,
  163. slv_cfg->bits_per_lane_and_dclk_cycle);
  164. return -EINVAL;
  165. }
  166. if (mst_cfg->differential_clk_rate !=
  167. slv_cfg->differential_clk_rate) {
  168. dev_err(dev, "dclk rate mismatch(mst: %lu vs slv: %lu)\n",
  169. mst_cfg->differential_clk_rate,
  170. slv_cfg->differential_clk_rate);
  171. return -EINVAL;
  172. }
  173. if (mst_cfg->lanes != slv_cfg->lanes) {
  174. dev_err(dev, "lanes mismatch(mst: %u vs slv: %u)\n",
  175. mst_cfg->lanes, slv_cfg->lanes);
  176. return -EINVAL;
  177. }
  178. if (mst_cfg->is_slave == slv_cfg->is_slave) {
  179. dev_err(dev, "master PHY is not found\n");
  180. return -EINVAL;
  181. }
  182. return 0;
  183. }
  184. static int mixel_lvds_phy_validate(struct phy *phy, enum phy_mode mode,
  185. int submode, union phy_configure_opts *opts)
  186. {
  187. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(phy->dev.parent);
  188. struct mixel_lvds_phy *lvds_phy = phy_get_drvdata(phy);
  189. struct phy_configure_opts_lvds *cfg = &opts->lvds;
  190. int ret = 0;
  191. if (mode != PHY_MODE_LVDS) {
  192. dev_err(&phy->dev, "invalid PHY mode(%d)\n", mode);
  193. return -EINVAL;
  194. }
  195. if (cfg->bits_per_lane_and_dclk_cycle != 7 &&
  196. cfg->bits_per_lane_and_dclk_cycle != 10) {
  197. dev_err(&phy->dev, "invalid bits per data lane(%u)\n",
  198. cfg->bits_per_lane_and_dclk_cycle);
  199. return -EINVAL;
  200. }
  201. if (cfg->lanes != 4 && cfg->lanes != 3) {
  202. dev_err(&phy->dev, "invalid data lanes(%u)\n", cfg->lanes);
  203. return -EINVAL;
  204. }
  205. if (cfg->differential_clk_rate < MIN_CLKIN_FREQ ||
  206. cfg->differential_clk_rate > MAX_CLKIN_FREQ) {
  207. dev_err(&phy->dev, "invalid differential clock rate(%lu)\n",
  208. cfg->differential_clk_rate);
  209. return -EINVAL;
  210. }
  211. mutex_lock(&priv->lock);
  212. /* cache configuration set of our own for check */
  213. memcpy(&lvds_phy->cfg, cfg, sizeof(*cfg));
  214. if (cfg->is_slave) {
  215. ret = mixel_lvds_phy_check_slave(phy);
  216. if (ret)
  217. dev_err(&phy->dev, "failed to check slave PHY: %d\n", ret);
  218. }
  219. mutex_unlock(&priv->lock);
  220. return ret;
  221. }
  222. static const struct phy_ops mixel_lvds_phy_ops = {
  223. .init = mixel_lvds_phy_init,
  224. .power_on = mixel_lvds_phy_power_on,
  225. .power_off = mixel_lvds_phy_power_off,
  226. .configure = mixel_lvds_phy_configure,
  227. .validate = mixel_lvds_phy_validate,
  228. .owner = THIS_MODULE,
  229. };
  230. static int mixel_lvds_phy_reset(struct device *dev)
  231. {
  232. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev);
  233. int ret;
  234. ret = pm_runtime_resume_and_get(dev);
  235. if (ret < 0) {
  236. dev_err(dev, "failed to get PM runtime: %d\n", ret);
  237. return ret;
  238. }
  239. regmap_write(priv->regmap, PHY_CTRL, CTRL_RESET_VAL);
  240. ret = pm_runtime_put(dev);
  241. if (ret < 0)
  242. dev_err(dev, "failed to put PM runtime: %d\n", ret);
  243. return ret;
  244. }
  245. static struct phy *mixel_lvds_phy_xlate(struct device *dev,
  246. struct of_phandle_args *args)
  247. {
  248. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev);
  249. unsigned int phy_id;
  250. if (args->args_count != 1) {
  251. dev_err(dev,
  252. "invalid argument number(%d) for 'phys' property\n",
  253. args->args_count);
  254. return ERR_PTR(-EINVAL);
  255. }
  256. phy_id = args->args[0];
  257. if (phy_id >= PHY_NUM) {
  258. dev_err(dev, "invalid PHY index(%d)\n", phy_id);
  259. return ERR_PTR(-ENODEV);
  260. }
  261. return priv->phys[phy_id]->phy;
  262. }
  263. static int mixel_lvds_phy_probe(struct platform_device *pdev)
  264. {
  265. struct device *dev = &pdev->dev;
  266. struct phy_provider *phy_provider;
  267. struct mixel_lvds_phy_priv *priv;
  268. struct mixel_lvds_phy *lvds_phy;
  269. struct phy *phy;
  270. int i;
  271. int ret;
  272. if (!dev->of_node)
  273. return -ENODEV;
  274. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  275. if (!priv)
  276. return -ENOMEM;
  277. priv->regmap = syscon_node_to_regmap(dev->of_node->parent);
  278. if (IS_ERR(priv->regmap))
  279. return dev_err_probe(dev, PTR_ERR(priv->regmap),
  280. "failed to get regmap\n");
  281. priv->phy_ref_clk = devm_clk_get(dev, NULL);
  282. if (IS_ERR(priv->phy_ref_clk))
  283. return dev_err_probe(dev, PTR_ERR(priv->phy_ref_clk),
  284. "failed to get PHY reference clock\n");
  285. mutex_init(&priv->lock);
  286. dev_set_drvdata(dev, priv);
  287. pm_runtime_enable(dev);
  288. ret = mixel_lvds_phy_reset(dev);
  289. if (ret) {
  290. dev_err(dev, "failed to do POR reset: %d\n", ret);
  291. return ret;
  292. }
  293. for (i = 0; i < PHY_NUM; i++) {
  294. lvds_phy = devm_kzalloc(dev, sizeof(*lvds_phy), GFP_KERNEL);
  295. if (!lvds_phy) {
  296. ret = -ENOMEM;
  297. goto err;
  298. }
  299. phy = devm_phy_create(dev, NULL, &mixel_lvds_phy_ops);
  300. if (IS_ERR(phy)) {
  301. ret = PTR_ERR(phy);
  302. dev_err(dev, "failed to create PHY for channel%d: %d\n",
  303. i, ret);
  304. goto err;
  305. }
  306. lvds_phy->phy = phy;
  307. lvds_phy->id = i;
  308. priv->phys[i] = lvds_phy;
  309. phy_set_drvdata(phy, lvds_phy);
  310. }
  311. phy_provider = devm_of_phy_provider_register(dev, mixel_lvds_phy_xlate);
  312. if (IS_ERR(phy_provider)) {
  313. ret = PTR_ERR(phy_provider);
  314. dev_err(dev, "failed to register PHY provider: %d\n", ret);
  315. goto err;
  316. }
  317. return 0;
  318. err:
  319. pm_runtime_disable(dev);
  320. return ret;
  321. }
  322. static int mixel_lvds_phy_remove(struct platform_device *pdev)
  323. {
  324. pm_runtime_disable(&pdev->dev);
  325. return 0;
  326. }
  327. static int __maybe_unused mixel_lvds_phy_runtime_suspend(struct device *dev)
  328. {
  329. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev);
  330. /* power down */
  331. mutex_lock(&priv->lock);
  332. regmap_write(priv->regmap, PHY_CTRL + REG_SET, PD);
  333. mutex_unlock(&priv->lock);
  334. return 0;
  335. }
  336. static int __maybe_unused mixel_lvds_phy_runtime_resume(struct device *dev)
  337. {
  338. struct mixel_lvds_phy_priv *priv = dev_get_drvdata(dev);
  339. /* power up + control initialization */
  340. mutex_lock(&priv->lock);
  341. regmap_update_bits(priv->regmap, PHY_CTRL,
  342. CTRL_INIT_MASK | PD, CTRL_INIT_VAL);
  343. mutex_unlock(&priv->lock);
  344. return 0;
  345. }
  346. static const struct dev_pm_ops mixel_lvds_phy_pm_ops = {
  347. SET_RUNTIME_PM_OPS(mixel_lvds_phy_runtime_suspend,
  348. mixel_lvds_phy_runtime_resume, NULL)
  349. };
  350. static const struct of_device_id mixel_lvds_phy_of_match[] = {
  351. { .compatible = "fsl,imx8qm-lvds-phy" },
  352. { /* sentinel */ }
  353. };
  354. MODULE_DEVICE_TABLE(of, mixel_lvds_phy_of_match);
  355. static struct platform_driver mixel_lvds_phy_driver = {
  356. .probe = mixel_lvds_phy_probe,
  357. .remove = mixel_lvds_phy_remove,
  358. .driver = {
  359. .pm = &mixel_lvds_phy_pm_ops,
  360. .name = "mixel-lvds-phy",
  361. .of_match_table = mixel_lvds_phy_of_match,
  362. }
  363. };
  364. module_platform_driver(mixel_lvds_phy_driver);
  365. MODULE_DESCRIPTION("Mixel LVDS PHY driver");
  366. MODULE_AUTHOR("Liu Ying <[email protected]>");
  367. MODULE_LICENSE("GPL");