phy-sun6i-mipi-dphy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2016 Allwinnertech Co., Ltd.
  4. * Copyright (C) 2017-2018 Bootlin
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/module.h>
  11. #include <linux/of_address.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/reset.h>
  15. #include <linux/phy/phy.h>
  16. #include <linux/phy/phy-mipi-dphy.h>
  17. #define SUN6I_DPHY_GCTL_REG 0x00
  18. #define SUN6I_DPHY_GCTL_LANE_NUM(n) ((((n) - 1) & 3) << 4)
  19. #define SUN6I_DPHY_GCTL_EN BIT(0)
  20. #define SUN6I_DPHY_TX_CTL_REG 0x04
  21. #define SUN6I_DPHY_TX_CTL_HS_TX_CLK_CONT BIT(28)
  22. #define SUN6I_DPHY_RX_CTL_REG 0x08
  23. #define SUN6I_DPHY_RX_CTL_EN_DBC BIT(31)
  24. #define SUN6I_DPHY_RX_CTL_RX_CLK_FORCE BIT(24)
  25. #define SUN6I_DPHY_RX_CTL_RX_D3_FORCE BIT(23)
  26. #define SUN6I_DPHY_RX_CTL_RX_D2_FORCE BIT(22)
  27. #define SUN6I_DPHY_RX_CTL_RX_D1_FORCE BIT(21)
  28. #define SUN6I_DPHY_RX_CTL_RX_D0_FORCE BIT(20)
  29. #define SUN6I_DPHY_TX_TIME0_REG 0x10
  30. #define SUN6I_DPHY_TX_TIME0_HS_TRAIL(n) (((n) & 0xff) << 24)
  31. #define SUN6I_DPHY_TX_TIME0_HS_PREPARE(n) (((n) & 0xff) << 16)
  32. #define SUN6I_DPHY_TX_TIME0_LP_CLK_DIV(n) ((n) & 0xff)
  33. #define SUN6I_DPHY_TX_TIME1_REG 0x14
  34. #define SUN6I_DPHY_TX_TIME1_CLK_POST(n) (((n) & 0xff) << 24)
  35. #define SUN6I_DPHY_TX_TIME1_CLK_PRE(n) (((n) & 0xff) << 16)
  36. #define SUN6I_DPHY_TX_TIME1_CLK_ZERO(n) (((n) & 0xff) << 8)
  37. #define SUN6I_DPHY_TX_TIME1_CLK_PREPARE(n) ((n) & 0xff)
  38. #define SUN6I_DPHY_TX_TIME2_REG 0x18
  39. #define SUN6I_DPHY_TX_TIME2_CLK_TRAIL(n) ((n) & 0xff)
  40. #define SUN6I_DPHY_TX_TIME3_REG 0x1c
  41. #define SUN6I_DPHY_TX_TIME4_REG 0x20
  42. #define SUN6I_DPHY_TX_TIME4_HS_TX_ANA1(n) (((n) & 0xff) << 8)
  43. #define SUN6I_DPHY_TX_TIME4_HS_TX_ANA0(n) ((n) & 0xff)
  44. #define SUN6I_DPHY_RX_TIME0_REG 0x30
  45. #define SUN6I_DPHY_RX_TIME0_HS_RX_SYNC(n) (((n) & 0xff) << 24)
  46. #define SUN6I_DPHY_RX_TIME0_HS_RX_CLK_MISS(n) (((n) & 0xff) << 16)
  47. #define SUN6I_DPHY_RX_TIME0_LP_RX(n) (((n) & 0xff) << 8)
  48. #define SUN6I_DPHY_RX_TIME1_REG 0x34
  49. #define SUN6I_DPHY_RX_TIME1_RX_DLY(n) (((n) & 0xfff) << 20)
  50. #define SUN6I_DPHY_RX_TIME1_LP_RX_ULPS_WP(n) ((n) & 0xfffff)
  51. #define SUN6I_DPHY_RX_TIME2_REG 0x38
  52. #define SUN6I_DPHY_RX_TIME2_HS_RX_ANA1(n) (((n) & 0xff) << 8)
  53. #define SUN6I_DPHY_RX_TIME2_HS_RX_ANA0(n) ((n) & 0xff)
  54. #define SUN6I_DPHY_RX_TIME3_REG 0x40
  55. #define SUN6I_DPHY_RX_TIME3_LPRST_DLY(n) (((n) & 0xffff) << 16)
  56. #define SUN6I_DPHY_ANA0_REG 0x4c
  57. #define SUN6I_DPHY_ANA0_REG_PWS BIT(31)
  58. #define SUN6I_DPHY_ANA0_REG_PWEND BIT(30)
  59. #define SUN6I_DPHY_ANA0_REG_PWENC BIT(29)
  60. #define SUN6I_DPHY_ANA0_REG_DMPC BIT(28)
  61. #define SUN6I_DPHY_ANA0_REG_DMPD(n) (((n) & 0xf) << 24)
  62. #define SUN6I_DPHY_ANA0_REG_SRXDT(n) (((n) & 0xf) << 20)
  63. #define SUN6I_DPHY_ANA0_REG_SRXCK(n) (((n) & 0xf) << 16)
  64. #define SUN6I_DPHY_ANA0_REG_SDIV2 BIT(15)
  65. #define SUN6I_DPHY_ANA0_REG_SLV(n) (((n) & 7) << 12)
  66. #define SUN6I_DPHY_ANA0_REG_DEN(n) (((n) & 0xf) << 8)
  67. #define SUN6I_DPHY_ANA0_REG_PLR(n) (((n) & 0xf) << 4)
  68. #define SUN6I_DPHY_ANA0_REG_SFB(n) (((n) & 3) << 2)
  69. #define SUN6I_DPHY_ANA0_REG_RSD BIT(1)
  70. #define SUN6I_DPHY_ANA0_REG_SELSCK BIT(0)
  71. #define SUN6I_DPHY_ANA1_REG 0x50
  72. #define SUN6I_DPHY_ANA1_REG_VTTMODE BIT(31)
  73. #define SUN6I_DPHY_ANA1_REG_CSMPS(n) (((n) & 3) << 28)
  74. #define SUN6I_DPHY_ANA1_REG_SVTT(n) (((n) & 0xf) << 24)
  75. #define SUN6I_DPHY_ANA2_REG 0x54
  76. #define SUN6I_DPHY_ANA2_EN_P2S_CPU(n) (((n) & 0xf) << 24)
  77. #define SUN6I_DPHY_ANA2_EN_P2S_CPU_MASK GENMASK(27, 24)
  78. #define SUN6I_DPHY_ANA2_EN_CK_CPU BIT(4)
  79. #define SUN6I_DPHY_ANA2_REG_ENIB BIT(1)
  80. #define SUN6I_DPHY_ANA3_REG 0x58
  81. #define SUN6I_DPHY_ANA3_EN_VTTD(n) (((n) & 0xf) << 28)
  82. #define SUN6I_DPHY_ANA3_EN_VTTD_MASK GENMASK(31, 28)
  83. #define SUN6I_DPHY_ANA3_EN_VTTC BIT(27)
  84. #define SUN6I_DPHY_ANA3_EN_DIV BIT(26)
  85. #define SUN6I_DPHY_ANA3_EN_LDOC BIT(25)
  86. #define SUN6I_DPHY_ANA3_EN_LDOD BIT(24)
  87. #define SUN6I_DPHY_ANA3_EN_LDOR BIT(18)
  88. #define SUN6I_DPHY_ANA4_REG 0x5c
  89. #define SUN6I_DPHY_ANA4_REG_EN_MIPI BIT(31)
  90. #define SUN6I_DPHY_ANA4_REG_EN_COMTEST BIT(30)
  91. #define SUN6I_DPHY_ANA4_REG_COMTEST(n) (((n) & 3) << 28)
  92. #define SUN6I_DPHY_ANA4_REG_IB(n) (((n) & 3) << 25)
  93. #define SUN6I_DPHY_ANA4_REG_DMPLVC BIT(24)
  94. #define SUN6I_DPHY_ANA4_REG_DMPLVD(n) (((n) & 0xf) << 20)
  95. #define SUN6I_DPHY_ANA4_REG_VTT_SET(n) (((n) & 0x7) << 17)
  96. #define SUN6I_DPHY_ANA4_REG_CKDV(n) (((n) & 0x1f) << 12)
  97. #define SUN6I_DPHY_ANA4_REG_TMSC(n) (((n) & 3) << 10)
  98. #define SUN6I_DPHY_ANA4_REG_TMSD(n) (((n) & 3) << 8)
  99. #define SUN6I_DPHY_ANA4_REG_TXDNSC(n) (((n) & 3) << 6)
  100. #define SUN6I_DPHY_ANA4_REG_TXDNSD(n) (((n) & 3) << 4)
  101. #define SUN6I_DPHY_ANA4_REG_TXPUSC(n) (((n) & 3) << 2)
  102. #define SUN6I_DPHY_ANA4_REG_TXPUSD(n) ((n) & 3)
  103. #define SUN6I_DPHY_DBG5_REG 0xf4
  104. #define SUN50I_DPHY_TX_SLEW_REG0 0xf8
  105. #define SUN50I_DPHY_TX_SLEW_REG1 0xfc
  106. #define SUN50I_DPHY_TX_SLEW_REG2 0x100
  107. #define SUN50I_DPHY_PLL_REG0 0x104
  108. #define SUN50I_DPHY_PLL_REG0_CP36_EN BIT(23)
  109. #define SUN50I_DPHY_PLL_REG0_LDO_EN BIT(22)
  110. #define SUN50I_DPHY_PLL_REG0_EN_LVS BIT(21)
  111. #define SUN50I_DPHY_PLL_REG0_PLL_EN BIT(20)
  112. #define SUN50I_DPHY_PLL_REG0_P(n) (((n) & 0xf) << 16)
  113. #define SUN50I_DPHY_PLL_REG0_N(n) (((n) & 0xff) << 8)
  114. #define SUN50I_DPHY_PLL_REG0_NDET BIT(7)
  115. #define SUN50I_DPHY_PLL_REG0_TDIV BIT(6)
  116. #define SUN50I_DPHY_PLL_REG0_M0(n) (((n) & 3) << 4)
  117. #define SUN50I_DPHY_PLL_REG0_M1(n) ((n) & 0xf)
  118. #define SUN50I_DPHY_PLL_REG1 0x108
  119. #define SUN50I_DPHY_PLL_REG1_UNLOCK_MDSEL(n) (((n) & 3) << 14)
  120. #define SUN50I_DPHY_PLL_REG1_LOCKMDSEL BIT(13)
  121. #define SUN50I_DPHY_PLL_REG1_LOCKDET_EN BIT(12)
  122. #define SUN50I_DPHY_PLL_REG1_VSETA(n) (((n) & 0x7) << 9)
  123. #define SUN50I_DPHY_PLL_REG1_VSETD(n) (((n) & 0x7) << 6)
  124. #define SUN50I_DPHY_PLL_REG1_LPF_SW BIT(5)
  125. #define SUN50I_DPHY_PLL_REG1_ICP_SEL(n) (((n) & 3) << 3)
  126. #define SUN50I_DPHY_PLL_REG1_ATEST_SEL(n) (((n) & 3) << 1)
  127. #define SUN50I_DPHY_PLL_REG1_TEST_EN BIT(0)
  128. #define SUN50I_DPHY_PLL_REG2 0x10c
  129. #define SUN50I_DPHY_PLL_REG2_SDM_EN BIT(31)
  130. #define SUN50I_DPHY_PLL_REG2_FF_EN BIT(30)
  131. #define SUN50I_DPHY_PLL_REG2_SS_EN BIT(29)
  132. #define SUN50I_DPHY_PLL_REG2_SS_FRAC(n) (((n) & 0x1ff) << 20)
  133. #define SUN50I_DPHY_PLL_REG2_SS_INT(n) (((n) & 0xff) << 12)
  134. #define SUN50I_DPHY_PLL_REG2_FRAC(n) ((n) & 0xfff)
  135. #define SUN50I_COMBO_PHY_REG0 0x110
  136. #define SUN50I_COMBO_PHY_REG0_EN_TEST_COMBOLDO BIT(5)
  137. #define SUN50I_COMBO_PHY_REG0_EN_TEST_0P8 BIT(4)
  138. #define SUN50I_COMBO_PHY_REG0_EN_MIPI BIT(3)
  139. #define SUN50I_COMBO_PHY_REG0_EN_LVDS BIT(2)
  140. #define SUN50I_COMBO_PHY_REG0_EN_COMBOLDO BIT(1)
  141. #define SUN50I_COMBO_PHY_REG0_EN_CP BIT(0)
  142. #define SUN50I_COMBO_PHY_REG1 0x114
  143. #define SUN50I_COMBO_PHY_REG2_REG_VREF1P6(n) (((n) & 0x7) << 4)
  144. #define SUN50I_COMBO_PHY_REG2_REG_VREF0P8(n) ((n) & 0x7)
  145. #define SUN50I_COMBO_PHY_REG2 0x118
  146. #define SUN50I_COMBO_PHY_REG2_HS_STOP_DLY(n) ((n) & 0xff)
  147. enum sun6i_dphy_direction {
  148. SUN6I_DPHY_DIRECTION_TX,
  149. SUN6I_DPHY_DIRECTION_RX,
  150. };
  151. struct sun6i_dphy;
  152. struct sun6i_dphy_variant {
  153. void (*tx_power_on)(struct sun6i_dphy *dphy);
  154. bool rx_supported;
  155. };
  156. struct sun6i_dphy {
  157. struct clk *bus_clk;
  158. struct clk *mod_clk;
  159. struct regmap *regs;
  160. struct reset_control *reset;
  161. struct phy *phy;
  162. struct phy_configure_opts_mipi_dphy config;
  163. const struct sun6i_dphy_variant *variant;
  164. enum sun6i_dphy_direction direction;
  165. };
  166. static int sun6i_dphy_init(struct phy *phy)
  167. {
  168. struct sun6i_dphy *dphy = phy_get_drvdata(phy);
  169. reset_control_deassert(dphy->reset);
  170. clk_prepare_enable(dphy->mod_clk);
  171. clk_set_rate_exclusive(dphy->mod_clk, 150000000);
  172. return 0;
  173. }
  174. static int sun6i_dphy_configure(struct phy *phy, union phy_configure_opts *opts)
  175. {
  176. struct sun6i_dphy *dphy = phy_get_drvdata(phy);
  177. int ret;
  178. ret = phy_mipi_dphy_config_validate(&opts->mipi_dphy);
  179. if (ret)
  180. return ret;
  181. memcpy(&dphy->config, opts, sizeof(dphy->config));
  182. return 0;
  183. }
  184. static void sun6i_a31_mipi_dphy_tx_power_on(struct sun6i_dphy *dphy)
  185. {
  186. u8 lanes_mask = GENMASK(dphy->config.lanes - 1, 0);
  187. regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG,
  188. SUN6I_DPHY_ANA0_REG_PWS |
  189. SUN6I_DPHY_ANA0_REG_DMPC |
  190. SUN6I_DPHY_ANA0_REG_SLV(7) |
  191. SUN6I_DPHY_ANA0_REG_DMPD(lanes_mask) |
  192. SUN6I_DPHY_ANA0_REG_DEN(lanes_mask));
  193. regmap_write(dphy->regs, SUN6I_DPHY_ANA1_REG,
  194. SUN6I_DPHY_ANA1_REG_CSMPS(1) |
  195. SUN6I_DPHY_ANA1_REG_SVTT(7));
  196. regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG,
  197. SUN6I_DPHY_ANA4_REG_CKDV(1) |
  198. SUN6I_DPHY_ANA4_REG_TMSC(1) |
  199. SUN6I_DPHY_ANA4_REG_TMSD(1) |
  200. SUN6I_DPHY_ANA4_REG_TXDNSC(1) |
  201. SUN6I_DPHY_ANA4_REG_TXDNSD(1) |
  202. SUN6I_DPHY_ANA4_REG_TXPUSC(1) |
  203. SUN6I_DPHY_ANA4_REG_TXPUSD(1) |
  204. SUN6I_DPHY_ANA4_REG_DMPLVC |
  205. SUN6I_DPHY_ANA4_REG_DMPLVD(lanes_mask));
  206. regmap_write(dphy->regs, SUN6I_DPHY_ANA2_REG,
  207. SUN6I_DPHY_ANA2_REG_ENIB);
  208. udelay(5);
  209. regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG,
  210. SUN6I_DPHY_ANA3_EN_LDOR |
  211. SUN6I_DPHY_ANA3_EN_LDOC |
  212. SUN6I_DPHY_ANA3_EN_LDOD);
  213. udelay(1);
  214. }
  215. static void sun50i_a100_mipi_dphy_tx_power_on(struct sun6i_dphy *dphy)
  216. {
  217. unsigned long mipi_symbol_rate = dphy->config.hs_clk_rate;
  218. unsigned int div, n;
  219. regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG,
  220. SUN6I_DPHY_ANA4_REG_IB(2) |
  221. SUN6I_DPHY_ANA4_REG_DMPLVD(4) |
  222. SUN6I_DPHY_ANA4_REG_VTT_SET(3) |
  223. SUN6I_DPHY_ANA4_REG_CKDV(3) |
  224. SUN6I_DPHY_ANA4_REG_TMSD(1) |
  225. SUN6I_DPHY_ANA4_REG_TMSC(1) |
  226. SUN6I_DPHY_ANA4_REG_TXPUSD(2) |
  227. SUN6I_DPHY_ANA4_REG_TXPUSC(3) |
  228. SUN6I_DPHY_ANA4_REG_TXDNSD(2) |
  229. SUN6I_DPHY_ANA4_REG_TXDNSC(3));
  230. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
  231. SUN6I_DPHY_ANA2_EN_CK_CPU,
  232. SUN6I_DPHY_ANA2_EN_CK_CPU);
  233. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
  234. SUN6I_DPHY_ANA2_REG_ENIB,
  235. SUN6I_DPHY_ANA2_REG_ENIB);
  236. regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG,
  237. SUN6I_DPHY_ANA3_EN_LDOR |
  238. SUN6I_DPHY_ANA3_EN_LDOC |
  239. SUN6I_DPHY_ANA3_EN_LDOD);
  240. regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG,
  241. SUN6I_DPHY_ANA0_REG_PLR(4) |
  242. SUN6I_DPHY_ANA0_REG_SFB(1));
  243. regmap_write(dphy->regs, SUN50I_COMBO_PHY_REG0,
  244. SUN50I_COMBO_PHY_REG0_EN_CP);
  245. /* Choose a divider to limit the VCO frequency to around 2 GHz. */
  246. div = 16 >> order_base_2(DIV_ROUND_UP(mipi_symbol_rate, 264000000));
  247. n = mipi_symbol_rate * div / 24000000;
  248. regmap_write(dphy->regs, SUN50I_DPHY_PLL_REG0,
  249. SUN50I_DPHY_PLL_REG0_CP36_EN |
  250. SUN50I_DPHY_PLL_REG0_LDO_EN |
  251. SUN50I_DPHY_PLL_REG0_EN_LVS |
  252. SUN50I_DPHY_PLL_REG0_PLL_EN |
  253. SUN50I_DPHY_PLL_REG0_NDET |
  254. SUN50I_DPHY_PLL_REG0_P((div - 1) % 8) |
  255. SUN50I_DPHY_PLL_REG0_N(n) |
  256. SUN50I_DPHY_PLL_REG0_M0((div - 1) / 8) |
  257. SUN50I_DPHY_PLL_REG0_M1(2));
  258. /* Disable sigma-delta modulation. */
  259. regmap_write(dphy->regs, SUN50I_DPHY_PLL_REG2, 0);
  260. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA4_REG,
  261. SUN6I_DPHY_ANA4_REG_EN_MIPI,
  262. SUN6I_DPHY_ANA4_REG_EN_MIPI);
  263. regmap_update_bits(dphy->regs, SUN50I_COMBO_PHY_REG0,
  264. SUN50I_COMBO_PHY_REG0_EN_MIPI |
  265. SUN50I_COMBO_PHY_REG0_EN_COMBOLDO,
  266. SUN50I_COMBO_PHY_REG0_EN_MIPI |
  267. SUN50I_COMBO_PHY_REG0_EN_COMBOLDO);
  268. regmap_write(dphy->regs, SUN50I_COMBO_PHY_REG2,
  269. SUN50I_COMBO_PHY_REG2_HS_STOP_DLY(20));
  270. udelay(1);
  271. }
  272. static int sun6i_dphy_tx_power_on(struct sun6i_dphy *dphy)
  273. {
  274. u8 lanes_mask = GENMASK(dphy->config.lanes - 1, 0);
  275. regmap_write(dphy->regs, SUN6I_DPHY_TX_CTL_REG,
  276. SUN6I_DPHY_TX_CTL_HS_TX_CLK_CONT);
  277. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME0_REG,
  278. SUN6I_DPHY_TX_TIME0_LP_CLK_DIV(14) |
  279. SUN6I_DPHY_TX_TIME0_HS_PREPARE(6) |
  280. SUN6I_DPHY_TX_TIME0_HS_TRAIL(10));
  281. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME1_REG,
  282. SUN6I_DPHY_TX_TIME1_CLK_PREPARE(7) |
  283. SUN6I_DPHY_TX_TIME1_CLK_ZERO(50) |
  284. SUN6I_DPHY_TX_TIME1_CLK_PRE(3) |
  285. SUN6I_DPHY_TX_TIME1_CLK_POST(10));
  286. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME2_REG,
  287. SUN6I_DPHY_TX_TIME2_CLK_TRAIL(30));
  288. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME3_REG, 0);
  289. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME4_REG,
  290. SUN6I_DPHY_TX_TIME4_HS_TX_ANA0(3) |
  291. SUN6I_DPHY_TX_TIME4_HS_TX_ANA1(3));
  292. dphy->variant->tx_power_on(dphy);
  293. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA3_REG,
  294. SUN6I_DPHY_ANA3_EN_VTTC |
  295. SUN6I_DPHY_ANA3_EN_VTTD_MASK,
  296. SUN6I_DPHY_ANA3_EN_VTTC |
  297. SUN6I_DPHY_ANA3_EN_VTTD(lanes_mask));
  298. udelay(1);
  299. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA3_REG,
  300. SUN6I_DPHY_ANA3_EN_DIV,
  301. SUN6I_DPHY_ANA3_EN_DIV);
  302. udelay(1);
  303. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
  304. SUN6I_DPHY_ANA2_EN_CK_CPU,
  305. SUN6I_DPHY_ANA2_EN_CK_CPU);
  306. udelay(1);
  307. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA1_REG,
  308. SUN6I_DPHY_ANA1_REG_VTTMODE,
  309. SUN6I_DPHY_ANA1_REG_VTTMODE);
  310. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
  311. SUN6I_DPHY_ANA2_EN_P2S_CPU_MASK,
  312. SUN6I_DPHY_ANA2_EN_P2S_CPU(lanes_mask));
  313. regmap_write(dphy->regs, SUN6I_DPHY_GCTL_REG,
  314. SUN6I_DPHY_GCTL_LANE_NUM(dphy->config.lanes) |
  315. SUN6I_DPHY_GCTL_EN);
  316. return 0;
  317. }
  318. static int sun6i_dphy_rx_power_on(struct sun6i_dphy *dphy)
  319. {
  320. /* Physical clock rate is actually half of symbol rate with DDR. */
  321. unsigned long mipi_symbol_rate = dphy->config.hs_clk_rate;
  322. unsigned long dphy_clk_rate;
  323. unsigned int rx_dly;
  324. unsigned int lprst_dly;
  325. u32 value;
  326. dphy_clk_rate = clk_get_rate(dphy->mod_clk);
  327. if (!dphy_clk_rate)
  328. return -EINVAL;
  329. /* Hardcoded timing parameters from the Allwinner BSP. */
  330. regmap_write(dphy->regs, SUN6I_DPHY_RX_TIME0_REG,
  331. SUN6I_DPHY_RX_TIME0_HS_RX_SYNC(255) |
  332. SUN6I_DPHY_RX_TIME0_HS_RX_CLK_MISS(255) |
  333. SUN6I_DPHY_RX_TIME0_LP_RX(255));
  334. /*
  335. * Formula from the Allwinner BSP, with hardcoded coefficients
  336. * (probably internal divider/multiplier).
  337. */
  338. rx_dly = 8 * (unsigned int)(dphy_clk_rate / (mipi_symbol_rate / 8));
  339. /*
  340. * The Allwinner BSP has an alternative formula for LP_RX_ULPS_WP:
  341. * lp_ulps_wp_cnt = lp_ulps_wp_ms * lp_clk / 1000
  342. * but does not use it and hardcodes 255 instead.
  343. */
  344. regmap_write(dphy->regs, SUN6I_DPHY_RX_TIME1_REG,
  345. SUN6I_DPHY_RX_TIME1_RX_DLY(rx_dly) |
  346. SUN6I_DPHY_RX_TIME1_LP_RX_ULPS_WP(255));
  347. /* HS_RX_ANA0 value is hardcoded in the Allwinner BSP. */
  348. regmap_write(dphy->regs, SUN6I_DPHY_RX_TIME2_REG,
  349. SUN6I_DPHY_RX_TIME2_HS_RX_ANA0(4));
  350. /*
  351. * Formula from the Allwinner BSP, with hardcoded coefficients
  352. * (probably internal divider/multiplier).
  353. */
  354. lprst_dly = 4 * (unsigned int)(dphy_clk_rate / (mipi_symbol_rate / 2));
  355. regmap_write(dphy->regs, SUN6I_DPHY_RX_TIME3_REG,
  356. SUN6I_DPHY_RX_TIME3_LPRST_DLY(lprst_dly));
  357. /* Analog parameters are hardcoded in the Allwinner BSP. */
  358. regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG,
  359. SUN6I_DPHY_ANA0_REG_PWS |
  360. SUN6I_DPHY_ANA0_REG_SLV(7) |
  361. SUN6I_DPHY_ANA0_REG_SFB(2));
  362. regmap_write(dphy->regs, SUN6I_DPHY_ANA1_REG,
  363. SUN6I_DPHY_ANA1_REG_SVTT(4));
  364. regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG,
  365. SUN6I_DPHY_ANA4_REG_DMPLVC |
  366. SUN6I_DPHY_ANA4_REG_DMPLVD(1));
  367. regmap_write(dphy->regs, SUN6I_DPHY_ANA2_REG,
  368. SUN6I_DPHY_ANA2_REG_ENIB);
  369. regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG,
  370. SUN6I_DPHY_ANA3_EN_LDOR |
  371. SUN6I_DPHY_ANA3_EN_LDOC |
  372. SUN6I_DPHY_ANA3_EN_LDOD);
  373. /*
  374. * Delay comes from the Allwinner BSP, likely for internal regulator
  375. * ramp-up.
  376. */
  377. udelay(3);
  378. value = SUN6I_DPHY_RX_CTL_EN_DBC | SUN6I_DPHY_RX_CTL_RX_CLK_FORCE;
  379. /*
  380. * Rx data lane force-enable bits are used as regular RX enable by the
  381. * Allwinner BSP.
  382. */
  383. if (dphy->config.lanes >= 1)
  384. value |= SUN6I_DPHY_RX_CTL_RX_D0_FORCE;
  385. if (dphy->config.lanes >= 2)
  386. value |= SUN6I_DPHY_RX_CTL_RX_D1_FORCE;
  387. if (dphy->config.lanes >= 3)
  388. value |= SUN6I_DPHY_RX_CTL_RX_D2_FORCE;
  389. if (dphy->config.lanes == 4)
  390. value |= SUN6I_DPHY_RX_CTL_RX_D3_FORCE;
  391. regmap_write(dphy->regs, SUN6I_DPHY_RX_CTL_REG, value);
  392. regmap_write(dphy->regs, SUN6I_DPHY_GCTL_REG,
  393. SUN6I_DPHY_GCTL_LANE_NUM(dphy->config.lanes) |
  394. SUN6I_DPHY_GCTL_EN);
  395. return 0;
  396. }
  397. static int sun6i_dphy_power_on(struct phy *phy)
  398. {
  399. struct sun6i_dphy *dphy = phy_get_drvdata(phy);
  400. switch (dphy->direction) {
  401. case SUN6I_DPHY_DIRECTION_TX:
  402. return sun6i_dphy_tx_power_on(dphy);
  403. case SUN6I_DPHY_DIRECTION_RX:
  404. return sun6i_dphy_rx_power_on(dphy);
  405. default:
  406. return -EINVAL;
  407. }
  408. }
  409. static int sun6i_dphy_power_off(struct phy *phy)
  410. {
  411. struct sun6i_dphy *dphy = phy_get_drvdata(phy);
  412. regmap_write(dphy->regs, SUN6I_DPHY_GCTL_REG, 0);
  413. regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG, 0);
  414. regmap_write(dphy->regs, SUN6I_DPHY_ANA1_REG, 0);
  415. regmap_write(dphy->regs, SUN6I_DPHY_ANA2_REG, 0);
  416. regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG, 0);
  417. regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG, 0);
  418. return 0;
  419. }
  420. static int sun6i_dphy_exit(struct phy *phy)
  421. {
  422. struct sun6i_dphy *dphy = phy_get_drvdata(phy);
  423. clk_rate_exclusive_put(dphy->mod_clk);
  424. clk_disable_unprepare(dphy->mod_clk);
  425. reset_control_assert(dphy->reset);
  426. return 0;
  427. }
  428. static const struct phy_ops sun6i_dphy_ops = {
  429. .configure = sun6i_dphy_configure,
  430. .power_on = sun6i_dphy_power_on,
  431. .power_off = sun6i_dphy_power_off,
  432. .init = sun6i_dphy_init,
  433. .exit = sun6i_dphy_exit,
  434. };
  435. static const struct regmap_config sun6i_dphy_regmap_config = {
  436. .reg_bits = 32,
  437. .val_bits = 32,
  438. .reg_stride = 4,
  439. .max_register = SUN50I_COMBO_PHY_REG2,
  440. .name = "mipi-dphy",
  441. };
  442. static int sun6i_dphy_probe(struct platform_device *pdev)
  443. {
  444. struct phy_provider *phy_provider;
  445. struct sun6i_dphy *dphy;
  446. const char *direction;
  447. void __iomem *regs;
  448. int ret;
  449. dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL);
  450. if (!dphy)
  451. return -ENOMEM;
  452. dphy->variant = device_get_match_data(&pdev->dev);
  453. if (!dphy->variant)
  454. return -EINVAL;
  455. regs = devm_platform_ioremap_resource(pdev, 0);
  456. if (IS_ERR(regs)) {
  457. dev_err(&pdev->dev, "Couldn't map the DPHY encoder registers\n");
  458. return PTR_ERR(regs);
  459. }
  460. dphy->regs = devm_regmap_init_mmio_clk(&pdev->dev, "bus",
  461. regs, &sun6i_dphy_regmap_config);
  462. if (IS_ERR(dphy->regs)) {
  463. dev_err(&pdev->dev, "Couldn't create the DPHY encoder regmap\n");
  464. return PTR_ERR(dphy->regs);
  465. }
  466. dphy->reset = devm_reset_control_get_shared(&pdev->dev, NULL);
  467. if (IS_ERR(dphy->reset)) {
  468. dev_err(&pdev->dev, "Couldn't get our reset line\n");
  469. return PTR_ERR(dphy->reset);
  470. }
  471. dphy->mod_clk = devm_clk_get(&pdev->dev, "mod");
  472. if (IS_ERR(dphy->mod_clk)) {
  473. dev_err(&pdev->dev, "Couldn't get the DPHY mod clock\n");
  474. return PTR_ERR(dphy->mod_clk);
  475. }
  476. dphy->phy = devm_phy_create(&pdev->dev, NULL, &sun6i_dphy_ops);
  477. if (IS_ERR(dphy->phy)) {
  478. dev_err(&pdev->dev, "failed to create PHY\n");
  479. return PTR_ERR(dphy->phy);
  480. }
  481. dphy->direction = SUN6I_DPHY_DIRECTION_TX;
  482. ret = of_property_read_string(pdev->dev.of_node, "allwinner,direction",
  483. &direction);
  484. if (!ret && !strncmp(direction, "rx", 2)) {
  485. if (!dphy->variant->rx_supported) {
  486. dev_err(&pdev->dev, "RX not supported on this variant\n");
  487. return -EOPNOTSUPP;
  488. }
  489. dphy->direction = SUN6I_DPHY_DIRECTION_RX;
  490. }
  491. phy_set_drvdata(dphy->phy, dphy);
  492. phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
  493. return PTR_ERR_OR_ZERO(phy_provider);
  494. }
  495. static const struct sun6i_dphy_variant sun6i_a31_mipi_dphy_variant = {
  496. .tx_power_on = sun6i_a31_mipi_dphy_tx_power_on,
  497. .rx_supported = true,
  498. };
  499. static const struct sun6i_dphy_variant sun50i_a100_mipi_dphy_variant = {
  500. .tx_power_on = sun50i_a100_mipi_dphy_tx_power_on,
  501. };
  502. static const struct of_device_id sun6i_dphy_of_table[] = {
  503. {
  504. .compatible = "allwinner,sun6i-a31-mipi-dphy",
  505. .data = &sun6i_a31_mipi_dphy_variant,
  506. },
  507. {
  508. .compatible = "allwinner,sun50i-a100-mipi-dphy",
  509. .data = &sun50i_a100_mipi_dphy_variant,
  510. },
  511. { }
  512. };
  513. MODULE_DEVICE_TABLE(of, sun6i_dphy_of_table);
  514. static struct platform_driver sun6i_dphy_platform_driver = {
  515. .probe = sun6i_dphy_probe,
  516. .driver = {
  517. .name = "sun6i-mipi-dphy",
  518. .of_match_table = sun6i_dphy_of_table,
  519. },
  520. };
  521. module_platform_driver(sun6i_dphy_platform_driver);
  522. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin>");
  523. MODULE_DESCRIPTION("Allwinner A31 MIPI D-PHY Driver");
  524. MODULE_LICENSE("GPL");