phy-meson-axg-mipi-dphy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Meson AXG MIPI DPHY driver
  4. *
  5. * Copyright (C) 2018 Amlogic, Inc. All rights reserved
  6. * Copyright (C) 2020 BayLibre, SAS
  7. * Author: Neil Armstrong <[email protected]>
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bitops.h>
  11. #include <linux/bits.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/reset.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. /* [31] soft reset for the phy.
  22. * 1: reset. 0: dessert the reset.
  23. * [30] clock lane soft reset.
  24. * [29] data byte lane 3 soft reset.
  25. * [28] data byte lane 2 soft reset.
  26. * [27] data byte lane 1 soft reset.
  27. * [26] data byte lane 0 soft reset.
  28. * [25] mipi dsi pll clock selection.
  29. * 1: clock from fixed 850Mhz clock source. 0: from VID2 PLL.
  30. * [12] mipi HSbyteclk enable.
  31. * [11] mipi divider clk selection.
  32. * 1: select the mipi DDRCLKHS from clock divider.
  33. * 0: from PLL clock.
  34. * [10] mipi clock divider control.
  35. * 1: /4. 0: /2.
  36. * [9] mipi divider output enable.
  37. * [8] mipi divider counter enable.
  38. * [7] PLL clock enable.
  39. * [5] LPDT data endian.
  40. * 1 = transfer the high bit first. 0 : transfer the low bit first.
  41. * [4] HS data endian.
  42. * [3] force data byte lane in stop mode.
  43. * [2] force data byte lane 0 in receiver mode.
  44. * [1] write 1 to sync the txclkesc input. the internal logic have to
  45. * use txclkesc to decide Txvalid and Txready.
  46. * [0] enalbe the MIPI DPHY TxDDRClk.
  47. */
  48. #define MIPI_DSI_PHY_CTRL 0x0
  49. /* [31] clk lane tx_hs_en control selection.
  50. * 1: from register. 0: use clk lane state machine.
  51. * [30] register bit for clock lane tx_hs_en.
  52. * [29] clk lane tx_lp_en contrl selection.
  53. * 1: from register. 0: from clk lane state machine.
  54. * [28] register bit for clock lane tx_lp_en.
  55. * [27] chan0 tx_hs_en control selection.
  56. * 1: from register. 0: from chan0 state machine.
  57. * [26] register bit for chan0 tx_hs_en.
  58. * [25] chan0 tx_lp_en control selection.
  59. * 1: from register. 0: from chan0 state machine.
  60. * [24] register bit from chan0 tx_lp_en.
  61. * [23] chan0 rx_lp_en control selection.
  62. * 1: from register. 0: from chan0 state machine.
  63. * [22] register bit from chan0 rx_lp_en.
  64. * [21] chan0 contention detection enable control selection.
  65. * 1: from register. 0: from chan0 state machine.
  66. * [20] register bit from chan0 contention dectection enable.
  67. * [19] chan1 tx_hs_en control selection.
  68. * 1: from register. 0: from chan0 state machine.
  69. * [18] register bit for chan1 tx_hs_en.
  70. * [17] chan1 tx_lp_en control selection.
  71. * 1: from register. 0: from chan0 state machine.
  72. * [16] register bit from chan1 tx_lp_en.
  73. * [15] chan2 tx_hs_en control selection.
  74. * 1: from register. 0: from chan0 state machine.
  75. * [14] register bit for chan2 tx_hs_en.
  76. * [13] chan2 tx_lp_en control selection.
  77. * 1: from register. 0: from chan0 state machine.
  78. * [12] register bit from chan2 tx_lp_en.
  79. * [11] chan3 tx_hs_en control selection.
  80. * 1: from register. 0: from chan0 state machine.
  81. * [10] register bit for chan3 tx_hs_en.
  82. * [9] chan3 tx_lp_en control selection.
  83. * 1: from register. 0: from chan0 state machine.
  84. * [8] register bit from chan3 tx_lp_en.
  85. * [4] clk chan power down. this bit is also used as the power down
  86. * of the whole MIPI_DSI_PHY.
  87. * [3] chan3 power down.
  88. * [2] chan2 power down.
  89. * [1] chan1 power down.
  90. * [0] chan0 power down.
  91. */
  92. #define MIPI_DSI_CHAN_CTRL 0x4
  93. /* [24] rx turn watch dog triggered.
  94. * [23] rx esc watchdog triggered.
  95. * [22] mbias ready.
  96. * [21] txclkesc synced and ready.
  97. * [20:17] clk lane state. {mbias_ready, tx_stop, tx_ulps, tx_hs_active}
  98. * [16:13] chan3 state{0, tx_stop, tx_ulps, tx_hs_active}
  99. * [12:9] chan2 state.{0, tx_stop, tx_ulps, tx_hs_active}
  100. * [8:5] chan1 state. {0, tx_stop, tx_ulps, tx_hs_active}
  101. * [4:0] chan0 state. {TX_STOP, tx_ULPS, hs_active, direction, rxulpsesc}
  102. */
  103. #define MIPI_DSI_CHAN_STS 0x8
  104. /* [31:24] TCLK_PREPARE.
  105. * [23:16] TCLK_ZERO.
  106. * [15:8] TCLK_POST.
  107. * [7:0] TCLK_TRAIL.
  108. */
  109. #define MIPI_DSI_CLK_TIM 0xc
  110. /* [31:24] THS_PREPARE.
  111. * [23:16] THS_ZERO.
  112. * [15:8] THS_TRAIL.
  113. * [7:0] THS_EXIT.
  114. */
  115. #define MIPI_DSI_HS_TIM 0x10
  116. /* [31:24] tTA_GET.
  117. * [23:16] tTA_GO.
  118. * [15:8] tTA_SURE.
  119. * [7:0] tLPX.
  120. */
  121. #define MIPI_DSI_LP_TIM 0x14
  122. /* wait time to MIPI DIS analog ready. */
  123. #define MIPI_DSI_ANA_UP_TIM 0x18
  124. /* TINIT. */
  125. #define MIPI_DSI_INIT_TIM 0x1c
  126. /* TWAKEUP. */
  127. #define MIPI_DSI_WAKEUP_TIM 0x20
  128. /* when in RxULPS check state, after the logic enable the analog,
  129. * how long we should wait to check the lP state .
  130. */
  131. #define MIPI_DSI_LPOK_TIM 0x24
  132. /* Watchdog for RX low power state no finished. */
  133. #define MIPI_DSI_LP_WCHDOG 0x28
  134. /* tMBIAS, after send power up signals to analog,
  135. * how long we should wait for analog powered up.
  136. */
  137. #define MIPI_DSI_ANA_CTRL 0x2c
  138. /* [31:8] reserved for future.
  139. * [7:0] tCLK_PRE.
  140. */
  141. #define MIPI_DSI_CLK_TIM1 0x30
  142. /* watchdog for turn around waiting time. */
  143. #define MIPI_DSI_TURN_WCHDOG 0x34
  144. /* When in RxULPS state, how frequency we should to check
  145. * if the TX side out of ULPS state.
  146. */
  147. #define MIPI_DSI_ULPS_CHECK 0x38
  148. #define MIPI_DSI_TEST_CTRL0 0x3c
  149. #define MIPI_DSI_TEST_CTRL1 0x40
  150. struct phy_meson_axg_mipi_dphy_priv {
  151. struct device *dev;
  152. struct regmap *regmap;
  153. struct clk *clk;
  154. struct reset_control *reset;
  155. struct phy *analog;
  156. struct phy_configure_opts_mipi_dphy config;
  157. };
  158. static const struct regmap_config phy_meson_axg_mipi_dphy_regmap_conf = {
  159. .reg_bits = 8,
  160. .val_bits = 32,
  161. .reg_stride = 4,
  162. .max_register = MIPI_DSI_TEST_CTRL1,
  163. };
  164. static int phy_meson_axg_mipi_dphy_init(struct phy *phy)
  165. {
  166. struct phy_meson_axg_mipi_dphy_priv *priv = phy_get_drvdata(phy);
  167. int ret;
  168. ret = phy_init(priv->analog);
  169. if (ret)
  170. return ret;
  171. ret = reset_control_reset(priv->reset);
  172. if (ret)
  173. return ret;
  174. return 0;
  175. }
  176. static int phy_meson_axg_mipi_dphy_configure(struct phy *phy,
  177. union phy_configure_opts *opts)
  178. {
  179. struct phy_meson_axg_mipi_dphy_priv *priv = phy_get_drvdata(phy);
  180. int ret;
  181. ret = phy_mipi_dphy_config_validate(&opts->mipi_dphy);
  182. if (ret)
  183. return ret;
  184. ret = phy_configure(priv->analog, opts);
  185. if (ret)
  186. return ret;
  187. memcpy(&priv->config, opts, sizeof(priv->config));
  188. return 0;
  189. }
  190. static int phy_meson_axg_mipi_dphy_power_on(struct phy *phy)
  191. {
  192. struct phy_meson_axg_mipi_dphy_priv *priv = phy_get_drvdata(phy);
  193. int ret;
  194. unsigned long temp;
  195. ret = phy_power_on(priv->analog);
  196. if (ret)
  197. return ret;
  198. /* enable phy clock */
  199. regmap_write(priv->regmap, MIPI_DSI_PHY_CTRL, 0x1);
  200. regmap_write(priv->regmap, MIPI_DSI_PHY_CTRL,
  201. BIT(0) | /* enable the DSI PLL clock . */
  202. BIT(7) | /* enable pll clock which connected to DDR clock path */
  203. BIT(8)); /* enable the clock divider counter */
  204. /* enable the divider clock out */
  205. regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(9), BIT(9));
  206. /* enable the byte clock generation. */
  207. regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(12), BIT(12));
  208. regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(31), BIT(31));
  209. regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(31), 0);
  210. /* Calculate lanebyteclk period in ps */
  211. temp = (1000000 * 100) / (priv->config.hs_clk_rate / 1000);
  212. temp = temp * 8 * 10;
  213. regmap_write(priv->regmap, MIPI_DSI_CLK_TIM,
  214. DIV_ROUND_UP(priv->config.clk_trail, temp) |
  215. (DIV_ROUND_UP(priv->config.clk_post +
  216. priv->config.hs_trail, temp) << 8) |
  217. (DIV_ROUND_UP(priv->config.clk_zero, temp) << 16) |
  218. (DIV_ROUND_UP(priv->config.clk_prepare, temp) << 24));
  219. regmap_write(priv->regmap, MIPI_DSI_CLK_TIM1,
  220. DIV_ROUND_UP(priv->config.clk_pre, BITS_PER_BYTE));
  221. regmap_write(priv->regmap, MIPI_DSI_HS_TIM,
  222. DIV_ROUND_UP(priv->config.hs_exit, temp) |
  223. (DIV_ROUND_UP(priv->config.hs_trail, temp) << 8) |
  224. (DIV_ROUND_UP(priv->config.hs_zero, temp) << 16) |
  225. (DIV_ROUND_UP(priv->config.hs_prepare, temp) << 24));
  226. regmap_write(priv->regmap, MIPI_DSI_LP_TIM,
  227. DIV_ROUND_UP(priv->config.lpx, temp) |
  228. (DIV_ROUND_UP(priv->config.ta_sure, temp) << 8) |
  229. (DIV_ROUND_UP(priv->config.ta_go, temp) << 16) |
  230. (DIV_ROUND_UP(priv->config.ta_get, temp) << 24));
  231. regmap_write(priv->regmap, MIPI_DSI_ANA_UP_TIM, 0x0100);
  232. regmap_write(priv->regmap, MIPI_DSI_INIT_TIM,
  233. DIV_ROUND_UP(priv->config.init * NSEC_PER_MSEC, temp));
  234. regmap_write(priv->regmap, MIPI_DSI_WAKEUP_TIM,
  235. DIV_ROUND_UP(priv->config.wakeup * NSEC_PER_MSEC, temp));
  236. regmap_write(priv->regmap, MIPI_DSI_LPOK_TIM, 0x7C);
  237. regmap_write(priv->regmap, MIPI_DSI_ULPS_CHECK, 0x927C);
  238. regmap_write(priv->regmap, MIPI_DSI_LP_WCHDOG, 0x1000);
  239. regmap_write(priv->regmap, MIPI_DSI_TURN_WCHDOG, 0x1000);
  240. /* Powerup the analog circuit */
  241. switch (priv->config.lanes) {
  242. case 1:
  243. regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0xe);
  244. break;
  245. case 2:
  246. regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0xc);
  247. break;
  248. case 3:
  249. regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0x8);
  250. break;
  251. case 4:
  252. default:
  253. regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0);
  254. break;
  255. }
  256. /* Trigger a sync active for esc_clk */
  257. regmap_update_bits(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(1), BIT(1));
  258. return 0;
  259. }
  260. static int phy_meson_axg_mipi_dphy_power_off(struct phy *phy)
  261. {
  262. struct phy_meson_axg_mipi_dphy_priv *priv = phy_get_drvdata(phy);
  263. regmap_write(priv->regmap, MIPI_DSI_CHAN_CTRL, 0xf);
  264. regmap_write(priv->regmap, MIPI_DSI_PHY_CTRL, BIT(31));
  265. phy_power_off(priv->analog);
  266. return 0;
  267. }
  268. static int phy_meson_axg_mipi_dphy_exit(struct phy *phy)
  269. {
  270. struct phy_meson_axg_mipi_dphy_priv *priv = phy_get_drvdata(phy);
  271. int ret;
  272. ret = phy_exit(priv->analog);
  273. if (ret)
  274. return ret;
  275. return reset_control_reset(priv->reset);
  276. }
  277. static const struct phy_ops phy_meson_axg_mipi_dphy_ops = {
  278. .configure = phy_meson_axg_mipi_dphy_configure,
  279. .init = phy_meson_axg_mipi_dphy_init,
  280. .exit = phy_meson_axg_mipi_dphy_exit,
  281. .power_on = phy_meson_axg_mipi_dphy_power_on,
  282. .power_off = phy_meson_axg_mipi_dphy_power_off,
  283. .owner = THIS_MODULE,
  284. };
  285. static int phy_meson_axg_mipi_dphy_probe(struct platform_device *pdev)
  286. {
  287. struct device *dev = &pdev->dev;
  288. struct phy_provider *phy_provider;
  289. struct resource *res;
  290. struct phy_meson_axg_mipi_dphy_priv *priv;
  291. struct phy *phy;
  292. void __iomem *base;
  293. int ret;
  294. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  295. if (!priv)
  296. return -ENOMEM;
  297. priv->dev = dev;
  298. platform_set_drvdata(pdev, priv);
  299. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  300. base = devm_ioremap_resource(dev, res);
  301. if (IS_ERR(base))
  302. return PTR_ERR(base);
  303. priv->regmap = devm_regmap_init_mmio(dev, base,
  304. &phy_meson_axg_mipi_dphy_regmap_conf);
  305. if (IS_ERR(priv->regmap))
  306. return PTR_ERR(priv->regmap);
  307. priv->clk = devm_clk_get(dev, "pclk");
  308. if (IS_ERR(priv->clk))
  309. return PTR_ERR(priv->clk);
  310. priv->reset = devm_reset_control_get(dev, "phy");
  311. if (IS_ERR(priv->reset))
  312. return PTR_ERR(priv->reset);
  313. priv->analog = devm_phy_get(dev, "analog");
  314. if (IS_ERR(priv->analog))
  315. return PTR_ERR(priv->analog);
  316. ret = clk_prepare_enable(priv->clk);
  317. if (ret)
  318. return ret;
  319. ret = reset_control_deassert(priv->reset);
  320. if (ret)
  321. return ret;
  322. phy = devm_phy_create(dev, NULL, &phy_meson_axg_mipi_dphy_ops);
  323. if (IS_ERR(phy)) {
  324. ret = PTR_ERR(phy);
  325. if (ret != -EPROBE_DEFER)
  326. dev_err(dev, "failed to create PHY\n");
  327. return ret;
  328. }
  329. phy_set_drvdata(phy, priv);
  330. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  331. return PTR_ERR_OR_ZERO(phy_provider);
  332. }
  333. static const struct of_device_id phy_meson_axg_mipi_dphy_of_match[] = {
  334. { .compatible = "amlogic,axg-mipi-dphy", },
  335. { },
  336. };
  337. MODULE_DEVICE_TABLE(of, phy_meson_axg_mipi_dphy_of_match);
  338. static struct platform_driver phy_meson_axg_mipi_dphy_driver = {
  339. .probe = phy_meson_axg_mipi_dphy_probe,
  340. .driver = {
  341. .name = "phy-meson-axg-mipi-dphy",
  342. .of_match_table = phy_meson_axg_mipi_dphy_of_match,
  343. },
  344. };
  345. module_platform_driver(phy_meson_axg_mipi_dphy_driver);
  346. MODULE_AUTHOR("Neil Armstrong <[email protected]>");
  347. MODULE_DESCRIPTION("Meson AXG MIPI DPHY driver");
  348. MODULE_LICENSE("GPL v2");