sun6i_mipi_dphy.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/of_address.h>
  11. #include <linux/regmap.h>
  12. #include <linux/reset.h>
  13. #include "sun6i_mipi_dsi.h"
  14. #define SUN6I_DPHY_GCTL_REG 0x00
  15. #define SUN6I_DPHY_GCTL_LANE_NUM(n) ((((n) - 1) & 3) << 4)
  16. #define SUN6I_DPHY_GCTL_EN BIT(0)
  17. #define SUN6I_DPHY_TX_CTL_REG 0x04
  18. #define SUN6I_DPHY_TX_CTL_HS_TX_CLK_CONT BIT(28)
  19. #define SUN6I_DPHY_TX_TIME0_REG 0x10
  20. #define SUN6I_DPHY_TX_TIME0_HS_TRAIL(n) (((n) & 0xff) << 24)
  21. #define SUN6I_DPHY_TX_TIME0_HS_PREPARE(n) (((n) & 0xff) << 16)
  22. #define SUN6I_DPHY_TX_TIME0_LP_CLK_DIV(n) ((n) & 0xff)
  23. #define SUN6I_DPHY_TX_TIME1_REG 0x14
  24. #define SUN6I_DPHY_TX_TIME1_CLK_POST(n) (((n) & 0xff) << 24)
  25. #define SUN6I_DPHY_TX_TIME1_CLK_PRE(n) (((n) & 0xff) << 16)
  26. #define SUN6I_DPHY_TX_TIME1_CLK_ZERO(n) (((n) & 0xff) << 8)
  27. #define SUN6I_DPHY_TX_TIME1_CLK_PREPARE(n) ((n) & 0xff)
  28. #define SUN6I_DPHY_TX_TIME2_REG 0x18
  29. #define SUN6I_DPHY_TX_TIME2_CLK_TRAIL(n) ((n) & 0xff)
  30. #define SUN6I_DPHY_TX_TIME3_REG 0x1c
  31. #define SUN6I_DPHY_TX_TIME4_REG 0x20
  32. #define SUN6I_DPHY_TX_TIME4_HS_TX_ANA1(n) (((n) & 0xff) << 8)
  33. #define SUN6I_DPHY_TX_TIME4_HS_TX_ANA0(n) ((n) & 0xff)
  34. #define SUN6I_DPHY_ANA0_REG 0x4c
  35. #define SUN6I_DPHY_ANA0_REG_PWS BIT(31)
  36. #define SUN6I_DPHY_ANA0_REG_DMPC BIT(28)
  37. #define SUN6I_DPHY_ANA0_REG_DMPD(n) (((n) & 0xf) << 24)
  38. #define SUN6I_DPHY_ANA0_REG_SLV(n) (((n) & 7) << 12)
  39. #define SUN6I_DPHY_ANA0_REG_DEN(n) (((n) & 0xf) << 8)
  40. #define SUN6I_DPHY_ANA1_REG 0x50
  41. #define SUN6I_DPHY_ANA1_REG_VTTMODE BIT(31)
  42. #define SUN6I_DPHY_ANA1_REG_CSMPS(n) (((n) & 3) << 28)
  43. #define SUN6I_DPHY_ANA1_REG_SVTT(n) (((n) & 0xf) << 24)
  44. #define SUN6I_DPHY_ANA2_REG 0x54
  45. #define SUN6I_DPHY_ANA2_EN_P2S_CPU(n) (((n) & 0xf) << 24)
  46. #define SUN6I_DPHY_ANA2_EN_P2S_CPU_MASK GENMASK(27, 24)
  47. #define SUN6I_DPHY_ANA2_EN_CK_CPU BIT(4)
  48. #define SUN6I_DPHY_ANA2_REG_ENIB BIT(1)
  49. #define SUN6I_DPHY_ANA3_REG 0x58
  50. #define SUN6I_DPHY_ANA3_EN_VTTD(n) (((n) & 0xf) << 28)
  51. #define SUN6I_DPHY_ANA3_EN_VTTD_MASK GENMASK(31, 28)
  52. #define SUN6I_DPHY_ANA3_EN_VTTC BIT(27)
  53. #define SUN6I_DPHY_ANA3_EN_DIV BIT(26)
  54. #define SUN6I_DPHY_ANA3_EN_LDOC BIT(25)
  55. #define SUN6I_DPHY_ANA3_EN_LDOD BIT(24)
  56. #define SUN6I_DPHY_ANA3_EN_LDOR BIT(18)
  57. #define SUN6I_DPHY_ANA4_REG 0x5c
  58. #define SUN6I_DPHY_ANA4_REG_DMPLVC BIT(24)
  59. #define SUN6I_DPHY_ANA4_REG_DMPLVD(n) (((n) & 0xf) << 20)
  60. #define SUN6I_DPHY_ANA4_REG_CKDV(n) (((n) & 0x1f) << 12)
  61. #define SUN6I_DPHY_ANA4_REG_TMSC(n) (((n) & 3) << 10)
  62. #define SUN6I_DPHY_ANA4_REG_TMSD(n) (((n) & 3) << 8)
  63. #define SUN6I_DPHY_ANA4_REG_TXDNSC(n) (((n) & 3) << 6)
  64. #define SUN6I_DPHY_ANA4_REG_TXDNSD(n) (((n) & 3) << 4)
  65. #define SUN6I_DPHY_ANA4_REG_TXPUSC(n) (((n) & 3) << 2)
  66. #define SUN6I_DPHY_ANA4_REG_TXPUSD(n) ((n) & 3)
  67. #define SUN6I_DPHY_DBG5_REG 0xf4
  68. int sun6i_dphy_init(struct sun6i_dphy *dphy, unsigned int lanes)
  69. {
  70. reset_control_deassert(dphy->reset);
  71. clk_prepare_enable(dphy->mod_clk);
  72. clk_set_rate_exclusive(dphy->mod_clk, 150000000);
  73. regmap_write(dphy->regs, SUN6I_DPHY_TX_CTL_REG,
  74. SUN6I_DPHY_TX_CTL_HS_TX_CLK_CONT);
  75. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME0_REG,
  76. SUN6I_DPHY_TX_TIME0_LP_CLK_DIV(14) |
  77. SUN6I_DPHY_TX_TIME0_HS_PREPARE(6) |
  78. SUN6I_DPHY_TX_TIME0_HS_TRAIL(10));
  79. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME1_REG,
  80. SUN6I_DPHY_TX_TIME1_CLK_PREPARE(7) |
  81. SUN6I_DPHY_TX_TIME1_CLK_ZERO(50) |
  82. SUN6I_DPHY_TX_TIME1_CLK_PRE(3) |
  83. SUN6I_DPHY_TX_TIME1_CLK_POST(10));
  84. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME2_REG,
  85. SUN6I_DPHY_TX_TIME2_CLK_TRAIL(30));
  86. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME3_REG, 0);
  87. regmap_write(dphy->regs, SUN6I_DPHY_TX_TIME4_REG,
  88. SUN6I_DPHY_TX_TIME4_HS_TX_ANA0(3) |
  89. SUN6I_DPHY_TX_TIME4_HS_TX_ANA1(3));
  90. regmap_write(dphy->regs, SUN6I_DPHY_GCTL_REG,
  91. SUN6I_DPHY_GCTL_LANE_NUM(lanes) |
  92. SUN6I_DPHY_GCTL_EN);
  93. return 0;
  94. }
  95. int sun6i_dphy_power_on(struct sun6i_dphy *dphy, unsigned int lanes)
  96. {
  97. u8 lanes_mask = GENMASK(lanes - 1, 0);
  98. regmap_write(dphy->regs, SUN6I_DPHY_ANA0_REG,
  99. SUN6I_DPHY_ANA0_REG_PWS |
  100. SUN6I_DPHY_ANA0_REG_DMPC |
  101. SUN6I_DPHY_ANA0_REG_SLV(7) |
  102. SUN6I_DPHY_ANA0_REG_DMPD(lanes_mask) |
  103. SUN6I_DPHY_ANA0_REG_DEN(lanes_mask));
  104. regmap_write(dphy->regs, SUN6I_DPHY_ANA1_REG,
  105. SUN6I_DPHY_ANA1_REG_CSMPS(1) |
  106. SUN6I_DPHY_ANA1_REG_SVTT(7));
  107. regmap_write(dphy->regs, SUN6I_DPHY_ANA4_REG,
  108. SUN6I_DPHY_ANA4_REG_CKDV(1) |
  109. SUN6I_DPHY_ANA4_REG_TMSC(1) |
  110. SUN6I_DPHY_ANA4_REG_TMSD(1) |
  111. SUN6I_DPHY_ANA4_REG_TXDNSC(1) |
  112. SUN6I_DPHY_ANA4_REG_TXDNSD(1) |
  113. SUN6I_DPHY_ANA4_REG_TXPUSC(1) |
  114. SUN6I_DPHY_ANA4_REG_TXPUSD(1) |
  115. SUN6I_DPHY_ANA4_REG_DMPLVC |
  116. SUN6I_DPHY_ANA4_REG_DMPLVD(lanes_mask));
  117. regmap_write(dphy->regs, SUN6I_DPHY_ANA2_REG,
  118. SUN6I_DPHY_ANA2_REG_ENIB);
  119. udelay(5);
  120. regmap_write(dphy->regs, SUN6I_DPHY_ANA3_REG,
  121. SUN6I_DPHY_ANA3_EN_LDOR |
  122. SUN6I_DPHY_ANA3_EN_LDOC |
  123. SUN6I_DPHY_ANA3_EN_LDOD);
  124. udelay(1);
  125. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA3_REG,
  126. SUN6I_DPHY_ANA3_EN_VTTC |
  127. SUN6I_DPHY_ANA3_EN_VTTD_MASK,
  128. SUN6I_DPHY_ANA3_EN_VTTC |
  129. SUN6I_DPHY_ANA3_EN_VTTD(lanes_mask));
  130. udelay(1);
  131. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA3_REG,
  132. SUN6I_DPHY_ANA3_EN_DIV,
  133. SUN6I_DPHY_ANA3_EN_DIV);
  134. udelay(1);
  135. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
  136. SUN6I_DPHY_ANA2_EN_CK_CPU,
  137. SUN6I_DPHY_ANA2_EN_CK_CPU);
  138. udelay(1);
  139. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA1_REG,
  140. SUN6I_DPHY_ANA1_REG_VTTMODE,
  141. SUN6I_DPHY_ANA1_REG_VTTMODE);
  142. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA2_REG,
  143. SUN6I_DPHY_ANA2_EN_P2S_CPU_MASK,
  144. SUN6I_DPHY_ANA2_EN_P2S_CPU(lanes_mask));
  145. return 0;
  146. }
  147. int sun6i_dphy_power_off(struct sun6i_dphy *dphy)
  148. {
  149. regmap_update_bits(dphy->regs, SUN6I_DPHY_ANA1_REG,
  150. SUN6I_DPHY_ANA1_REG_VTTMODE, 0);
  151. return 0;
  152. }
  153. int sun6i_dphy_exit(struct sun6i_dphy *dphy)
  154. {
  155. clk_rate_exclusive_put(dphy->mod_clk);
  156. clk_disable_unprepare(dphy->mod_clk);
  157. reset_control_assert(dphy->reset);
  158. return 0;
  159. }
  160. static struct regmap_config sun6i_dphy_regmap_config = {
  161. .reg_bits = 32,
  162. .val_bits = 32,
  163. .reg_stride = 4,
  164. .max_register = SUN6I_DPHY_DBG5_REG,
  165. .name = "mipi-dphy",
  166. };
  167. static const struct of_device_id sun6i_dphy_of_table[] = {
  168. { .compatible = "allwinner,sun6i-a31-mipi-dphy" },
  169. { }
  170. };
  171. int sun6i_dphy_probe(struct sun6i_dsi *dsi, struct device_node *node)
  172. {
  173. struct sun6i_dphy *dphy;
  174. struct resource res;
  175. void __iomem *regs;
  176. int ret;
  177. if (!of_match_node(sun6i_dphy_of_table, node)) {
  178. dev_err(dsi->dev, "Incompatible D-PHY\n");
  179. return -EINVAL;
  180. }
  181. dphy = devm_kzalloc(dsi->dev, sizeof(*dphy), GFP_KERNEL);
  182. if (!dphy)
  183. return -ENOMEM;
  184. ret = of_address_to_resource(node, 0, &res);
  185. if (ret) {
  186. dev_err(dsi->dev, "phy: Couldn't get our resources\n");
  187. return ret;
  188. }
  189. regs = devm_ioremap_resource(dsi->dev, &res);
  190. if (IS_ERR(regs)) {
  191. dev_err(dsi->dev, "Couldn't map the DPHY encoder registers\n");
  192. return PTR_ERR(regs);
  193. }
  194. dphy->regs = devm_regmap_init_mmio(dsi->dev, regs,
  195. &sun6i_dphy_regmap_config);
  196. if (IS_ERR(dphy->regs)) {
  197. dev_err(dsi->dev, "Couldn't create the DPHY encoder regmap\n");
  198. return PTR_ERR(dphy->regs);
  199. }
  200. dphy->reset = of_reset_control_get_shared(node, NULL);
  201. if (IS_ERR(dphy->reset)) {
  202. dev_err(dsi->dev, "Couldn't get our reset line\n");
  203. return PTR_ERR(dphy->reset);
  204. }
  205. dphy->bus_clk = of_clk_get_by_name(node, "bus");
  206. if (IS_ERR(dphy->bus_clk)) {
  207. dev_err(dsi->dev, "Couldn't get the DPHY bus clock\n");
  208. ret = PTR_ERR(dphy->bus_clk);
  209. goto err_free_reset;
  210. }
  211. regmap_mmio_attach_clk(dphy->regs, dphy->bus_clk);
  212. dphy->mod_clk = of_clk_get_by_name(node, "mod");
  213. if (IS_ERR(dphy->mod_clk)) {
  214. dev_err(dsi->dev, "Couldn't get the DPHY mod clock\n");
  215. ret = PTR_ERR(dphy->mod_clk);
  216. goto err_free_bus;
  217. }
  218. dsi->dphy = dphy;
  219. return 0;
  220. err_free_bus:
  221. regmap_mmio_detach_clk(dphy->regs);
  222. clk_put(dphy->bus_clk);
  223. err_free_reset:
  224. reset_control_put(dphy->reset);
  225. return ret;
  226. }
  227. int sun6i_dphy_remove(struct sun6i_dsi *dsi)
  228. {
  229. struct sun6i_dphy *dphy = dsi->dphy;
  230. regmap_mmio_detach_clk(dphy->regs);
  231. clk_put(dphy->mod_clk);
  232. clk_put(dphy->bus_clk);
  233. reset_control_put(dphy->reset);
  234. return 0;
  235. }