0075-phy-starfive-Add-mipi-dphy-rx-support.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. From f6fbb431f9e3ac5c9144edf05340db9a96dffa59 Mon Sep 17 00:00:00 2001
  2. From: Changhuang Liang <[email protected]>
  3. Date: Mon, 29 May 2023 05:15:02 -0700
  4. Subject: [PATCH 075/122] phy: starfive: Add mipi dphy rx support
  5. Add mipi dphy rx support for the StarFive JH7110 SoC. It is used to
  6. transfer CSI camera data.
  7. Signed-off-by: Changhuang Liang <[email protected]>
  8. ---
  9. drivers/phy/Kconfig | 1 +
  10. drivers/phy/Makefile | 1 +
  11. drivers/phy/starfive/Kconfig | 13 +
  12. drivers/phy/starfive/Makefile | 2 +
  13. drivers/phy/starfive/phy-starfive-dphy-rx.c | 301 ++++++++++++++++++++
  14. 5 files changed, 318 insertions(+)
  15. create mode 100644 drivers/phy/starfive/Kconfig
  16. create mode 100644 drivers/phy/starfive/Makefile
  17. create mode 100644 drivers/phy/starfive/phy-starfive-dphy-rx.c
  18. --- a/drivers/phy/Kconfig
  19. +++ b/drivers/phy/Kconfig
  20. @@ -91,6 +91,7 @@ source "drivers/phy/rockchip/Kconfig"
  21. source "drivers/phy/samsung/Kconfig"
  22. source "drivers/phy/socionext/Kconfig"
  23. source "drivers/phy/st/Kconfig"
  24. +source "drivers/phy/starfive/Kconfig"
  25. source "drivers/phy/sunplus/Kconfig"
  26. source "drivers/phy/tegra/Kconfig"
  27. source "drivers/phy/ti/Kconfig"
  28. --- a/drivers/phy/Makefile
  29. +++ b/drivers/phy/Makefile
  30. @@ -31,6 +31,7 @@ obj-y += allwinner/ \
  31. samsung/ \
  32. socionext/ \
  33. st/ \
  34. + starfive/ \
  35. sunplus/ \
  36. tegra/ \
  37. ti/ \
  38. --- /dev/null
  39. +++ b/drivers/phy/starfive/Kconfig
  40. @@ -0,0 +1,13 @@
  41. +# SPDX-License-Identifier: GPL-2.0-only
  42. +#
  43. +# Phy drivers for StarFive platforms
  44. +#
  45. +
  46. +config PHY_STARFIVE_DPHY_RX
  47. + tristate "StarFive D-PHY RX Support"
  48. + select GENERIC_PHY
  49. + select GENERIC_PHY_MIPI_DPHY
  50. + help
  51. + Choose this option if you have a StarFive D-PHY in your
  52. + system. If M is selected, the module will be called
  53. + phy-starfive-dphy-rx.
  54. --- /dev/null
  55. +++ b/drivers/phy/starfive/Makefile
  56. @@ -0,0 +1,2 @@
  57. +# SPDX-License-Identifier: GPL-2.0
  58. +obj-$(CONFIG_PHY_STARFIVE_DPHY_RX) += phy-starfive-dphy-rx.o
  59. --- /dev/null
  60. +++ b/drivers/phy/starfive/phy-starfive-dphy-rx.c
  61. @@ -0,0 +1,301 @@
  62. +// SPDX-License-Identifier: GPL-2.0+
  63. +/*
  64. + * DPHY driver for the StarFive JH7110 SoC
  65. + *
  66. + * Copyright (C) 2023 StarFive Technology Co., Ltd.
  67. + */
  68. +
  69. +#include <linux/bitfield.h>
  70. +#include <linux/bitops.h>
  71. +#include <linux/clk.h>
  72. +#include <linux/io.h>
  73. +#include <linux/module.h>
  74. +#include <linux/of.h>
  75. +#include <linux/of_device.h>
  76. +#include <linux/phy/phy.h>
  77. +#include <linux/platform_device.h>
  78. +#include <linux/pm_runtime.h>
  79. +#include <linux/reset.h>
  80. +
  81. +#define STF_DPHY_APBCFGSAIF_SYSCFG(x) (x)
  82. +
  83. +#define STF_DPHY_DA_CDPHY_R100_CTRL0_2D1C_EFUSE_EN BIT(6)
  84. +#define STF_DPHY_DA_CDPHY_R100_CTRL0_2D1C_EFUSE_IN GENMASK(12, 7)
  85. +#define STF_DPHY_DA_CDPHY_R100_CTRL1_2D1C_EFUSE_EN BIT(19)
  86. +#define STF_DPHY_DA_CDPHY_R100_CTRL1_2D1C_EFUSE_IN GENMASK(25, 20)
  87. +
  88. +#define STF_DPHY_DATA_BUS16_8 BIT(8)
  89. +#define STF_DPHY_DEBUG_MODE_SEL GENMASK(15, 9)
  90. +
  91. +#define STF_DPHY_ENABLE_CLK BIT(6)
  92. +#define STF_DPHY_ENABLE_CLK1 BIT(7)
  93. +#define STF_DPHY_ENABLE_LAN0 BIT(8)
  94. +#define STF_DPHY_ENABLE_LAN1 BIT(9)
  95. +#define STF_DPHY_ENABLE_LAN2 BIT(10)
  96. +#define STF_DPHY_ENABLE_LAN3 BIT(11)
  97. +#define STF_DPHY_GPI_EN GENMASK(17, 12)
  98. +#define STF_DPHY_HS_FREQ_CHANGE_CLK BIT(18)
  99. +#define STF_DPHY_HS_FREQ_CHANGE_CLK1 BIT(19)
  100. +#define STF_DPHY_LANE_SWAP_CLK GENMASK(22, 20)
  101. +#define STF_DPHY_LANE_SWAP_CLK1 GENMASK(25, 23)
  102. +#define STF_DPHY_LANE_SWAP_LAN0 GENMASK(28, 26)
  103. +#define STF_DPHY_LANE_SWAP_LAN1 GENMASK(31, 29)
  104. +
  105. +#define STF_DPHY_LANE_SWAP_LAN2 GENMASK(2, 0)
  106. +#define STF_DPHY_LANE_SWAP_LAN3 GENMASK(5, 3)
  107. +#define STF_DPHY_MP_TEST_EN BIT(6)
  108. +#define STF_DPHY_MP_TEST_MODE_SEL GENMASK(11, 7)
  109. +#define STF_DPHY_PLL_CLK_SEL GENMASK(21, 12)
  110. +#define STF_DPHY_PRECOUNTER_IN_CLK GENMASK(29, 22)
  111. +
  112. +#define STF_DPHY_PRECOUNTER_IN_CLK1 GENMASK(7, 0)
  113. +#define STF_DPHY_PRECOUNTER_IN_LAN0 GENMASK(15, 8)
  114. +#define STF_DPHY_PRECOUNTER_IN_LAN1 GENMASK(23, 16)
  115. +#define STF_DPHY_PRECOUNTER_IN_LAN2 GENMASK(31, 24)
  116. +
  117. +#define STF_DPHY_PRECOUNTER_IN_LAN3 GENMASK(7, 0)
  118. +#define STF_DPHY_RX_1C2C_SEL BIT(8)
  119. +
  120. +#define STF_MAP_LANES_NUM 6
  121. +
  122. +struct regval {
  123. + u32 addr;
  124. + u32 val;
  125. +};
  126. +
  127. +struct stf_dphy_info {
  128. + /**
  129. + * @maps:
  130. + *
  131. + * Physical lanes and logic lanes mapping table.
  132. + *
  133. + * The default order is:
  134. + * [clk lane0, data lane 0, data lane 1, data lane 2, date lane 3, clk lane 1]
  135. + */
  136. + u8 maps[STF_MAP_LANES_NUM];
  137. +};
  138. +
  139. +struct stf_dphy {
  140. + struct device *dev;
  141. + void __iomem *regs;
  142. + struct clk *cfg_clk;
  143. + struct clk *ref_clk;
  144. + struct clk *tx_clk;
  145. + struct reset_control *rstc;
  146. + struct regulator *mipi_0p9;
  147. + struct phy *phy;
  148. + const struct stf_dphy_info *info;
  149. +};
  150. +
  151. +static const struct regval stf_dphy_init_list[] = {
  152. + { STF_DPHY_APBCFGSAIF_SYSCFG(4), 0x00000000 },
  153. + { STF_DPHY_APBCFGSAIF_SYSCFG(8), 0x00000000 },
  154. + { STF_DPHY_APBCFGSAIF_SYSCFG(12), 0x0000fff0 },
  155. + { STF_DPHY_APBCFGSAIF_SYSCFG(16), 0x00000000 },
  156. + { STF_DPHY_APBCFGSAIF_SYSCFG(20), 0x00000000 },
  157. + { STF_DPHY_APBCFGSAIF_SYSCFG(24), 0x00000000 },
  158. + { STF_DPHY_APBCFGSAIF_SYSCFG(28), 0x00000000 },
  159. + { STF_DPHY_APBCFGSAIF_SYSCFG(32), 0x00000000 },
  160. + { STF_DPHY_APBCFGSAIF_SYSCFG(36), 0x00000000 },
  161. + { STF_DPHY_APBCFGSAIF_SYSCFG(40), 0x00000000 },
  162. + { STF_DPHY_APBCFGSAIF_SYSCFG(40), 0x00000000 },
  163. + { STF_DPHY_APBCFGSAIF_SYSCFG(48), 0x24000000 },
  164. + { STF_DPHY_APBCFGSAIF_SYSCFG(52), 0x00000000 },
  165. + { STF_DPHY_APBCFGSAIF_SYSCFG(56), 0x00000000 },
  166. + { STF_DPHY_APBCFGSAIF_SYSCFG(60), 0x00000000 },
  167. + { STF_DPHY_APBCFGSAIF_SYSCFG(64), 0x00000000 },
  168. + { STF_DPHY_APBCFGSAIF_SYSCFG(68), 0x00000000 },
  169. + { STF_DPHY_APBCFGSAIF_SYSCFG(72), 0x00000000 },
  170. + { STF_DPHY_APBCFGSAIF_SYSCFG(76), 0x00000000 },
  171. + { STF_DPHY_APBCFGSAIF_SYSCFG(80), 0x00000000 },
  172. + { STF_DPHY_APBCFGSAIF_SYSCFG(84), 0x00000000 },
  173. + { STF_DPHY_APBCFGSAIF_SYSCFG(88), 0x00000000 },
  174. + { STF_DPHY_APBCFGSAIF_SYSCFG(92), 0x00000000 },
  175. + { STF_DPHY_APBCFGSAIF_SYSCFG(96), 0x00000000 },
  176. + { STF_DPHY_APBCFGSAIF_SYSCFG(100), 0x02000000 },
  177. + { STF_DPHY_APBCFGSAIF_SYSCFG(104), 0x00000000 },
  178. + { STF_DPHY_APBCFGSAIF_SYSCFG(108), 0x00000000 },
  179. + { STF_DPHY_APBCFGSAIF_SYSCFG(112), 0x00000000 },
  180. + { STF_DPHY_APBCFGSAIF_SYSCFG(116), 0x00000000 },
  181. + { STF_DPHY_APBCFGSAIF_SYSCFG(120), 0x00000000 },
  182. + { STF_DPHY_APBCFGSAIF_SYSCFG(124), 0x0000000c },
  183. + { STF_DPHY_APBCFGSAIF_SYSCFG(128), 0x00000000 },
  184. + { STF_DPHY_APBCFGSAIF_SYSCFG(132), 0xcc500000 },
  185. + { STF_DPHY_APBCFGSAIF_SYSCFG(136), 0x000000cc },
  186. + { STF_DPHY_APBCFGSAIF_SYSCFG(140), 0x00000000 },
  187. + { STF_DPHY_APBCFGSAIF_SYSCFG(144), 0x00000000 },
  188. +};
  189. +
  190. +static int stf_dphy_configure(struct phy *phy, union phy_configure_opts *opts)
  191. +{
  192. + struct stf_dphy *dphy = phy_get_drvdata(phy);
  193. + const struct stf_dphy_info *info = dphy->info;
  194. + int i;
  195. +
  196. + for (i = 0; i < ARRAY_SIZE(stf_dphy_init_list); i++)
  197. + writel(stf_dphy_init_list[i].val,
  198. + dphy->regs + stf_dphy_init_list[i].addr);
  199. +
  200. + writel(FIELD_PREP(STF_DPHY_DA_CDPHY_R100_CTRL0_2D1C_EFUSE_EN, 1) |
  201. + FIELD_PREP(STF_DPHY_DA_CDPHY_R100_CTRL0_2D1C_EFUSE_IN, 0x1b) |
  202. + FIELD_PREP(STF_DPHY_DA_CDPHY_R100_CTRL1_2D1C_EFUSE_EN, 1) |
  203. + FIELD_PREP(STF_DPHY_DA_CDPHY_R100_CTRL1_2D1C_EFUSE_IN, 0x1b),
  204. + dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(0));
  205. +
  206. + writel(FIELD_PREP(STF_DPHY_DATA_BUS16_8, 0) |
  207. + FIELD_PREP(STF_DPHY_DEBUG_MODE_SEL, 0x5a),
  208. + dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(184));
  209. +
  210. + writel(FIELD_PREP(STF_DPHY_ENABLE_CLK, 1) |
  211. + FIELD_PREP(STF_DPHY_ENABLE_CLK1, 1) |
  212. + FIELD_PREP(STF_DPHY_ENABLE_LAN0, 1) |
  213. + FIELD_PREP(STF_DPHY_ENABLE_LAN1, 1) |
  214. + FIELD_PREP(STF_DPHY_ENABLE_LAN2, 1) |
  215. + FIELD_PREP(STF_DPHY_ENABLE_LAN3, 1) |
  216. + FIELD_PREP(STF_DPHY_GPI_EN, 0) |
  217. + FIELD_PREP(STF_DPHY_HS_FREQ_CHANGE_CLK, 0) |
  218. + FIELD_PREP(STF_DPHY_HS_FREQ_CHANGE_CLK1, 0) |
  219. + FIELD_PREP(STF_DPHY_LANE_SWAP_CLK, info->maps[0]) |
  220. + FIELD_PREP(STF_DPHY_LANE_SWAP_CLK1, info->maps[5]) |
  221. + FIELD_PREP(STF_DPHY_LANE_SWAP_LAN0, info->maps[1]) |
  222. + FIELD_PREP(STF_DPHY_LANE_SWAP_LAN1, info->maps[2]),
  223. + dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(188));
  224. +
  225. + writel(FIELD_PREP(STF_DPHY_LANE_SWAP_LAN2, info->maps[3]) |
  226. + FIELD_PREP(STF_DPHY_LANE_SWAP_LAN3, info->maps[4]) |
  227. + FIELD_PREP(STF_DPHY_MP_TEST_EN, 0) |
  228. + FIELD_PREP(STF_DPHY_MP_TEST_MODE_SEL, 0) |
  229. + FIELD_PREP(STF_DPHY_PLL_CLK_SEL, 0x37c) |
  230. + FIELD_PREP(STF_DPHY_PRECOUNTER_IN_CLK, 8),
  231. + dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(192));
  232. +
  233. + writel(FIELD_PREP(STF_DPHY_PRECOUNTER_IN_CLK1, 8) |
  234. + FIELD_PREP(STF_DPHY_PRECOUNTER_IN_LAN0, 7) |
  235. + FIELD_PREP(STF_DPHY_PRECOUNTER_IN_LAN1, 7) |
  236. + FIELD_PREP(STF_DPHY_PRECOUNTER_IN_LAN2, 7),
  237. + dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(196));
  238. +
  239. + writel(FIELD_PREP(STF_DPHY_PRECOUNTER_IN_LAN3, 7) |
  240. + FIELD_PREP(STF_DPHY_RX_1C2C_SEL, 0),
  241. + dphy->regs + STF_DPHY_APBCFGSAIF_SYSCFG(200));
  242. +
  243. + return 0;
  244. +}
  245. +
  246. +static int stf_dphy_power_on(struct phy *phy)
  247. +{
  248. + struct stf_dphy *dphy = phy_get_drvdata(phy);
  249. + int ret;
  250. +
  251. + pm_runtime_get_sync(dphy->dev);
  252. +
  253. + ret = regulator_enable(dphy->mipi_0p9);
  254. + if (ret)
  255. + return ret;
  256. +
  257. + clk_set_rate(dphy->cfg_clk, 99000000);
  258. + clk_set_rate(dphy->ref_clk, 49500000);
  259. + clk_set_rate(dphy->tx_clk, 19800000);
  260. + reset_control_deassert(dphy->rstc);
  261. +
  262. + return 0;
  263. +}
  264. +
  265. +static int stf_dphy_power_off(struct phy *phy)
  266. +{
  267. + struct stf_dphy *dphy = phy_get_drvdata(phy);
  268. +
  269. + reset_control_assert(dphy->rstc);
  270. +
  271. + regulator_disable(dphy->mipi_0p9);
  272. +
  273. + pm_runtime_put_sync(dphy->dev);
  274. +
  275. + return 0;
  276. +}
  277. +
  278. +static const struct phy_ops stf_dphy_ops = {
  279. + .configure = stf_dphy_configure,
  280. + .power_on = stf_dphy_power_on,
  281. + .power_off = stf_dphy_power_off,
  282. +};
  283. +
  284. +static int stf_dphy_probe(struct platform_device *pdev)
  285. +{
  286. + struct phy_provider *phy_provider;
  287. + struct stf_dphy *dphy;
  288. +
  289. + dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL);
  290. + if (!dphy)
  291. + return -ENOMEM;
  292. +
  293. + dphy->info = of_device_get_match_data(&pdev->dev);
  294. +
  295. + dev_set_drvdata(&pdev->dev, dphy);
  296. + dphy->dev = &pdev->dev;
  297. +
  298. + dphy->regs = devm_platform_ioremap_resource(pdev, 0);
  299. + if (IS_ERR(dphy->regs))
  300. + return PTR_ERR(dphy->regs);
  301. +
  302. + dphy->cfg_clk = devm_clk_get(&pdev->dev, "cfg");
  303. + if (IS_ERR(dphy->cfg_clk))
  304. + return PTR_ERR(dphy->cfg_clk);
  305. +
  306. + dphy->ref_clk = devm_clk_get(&pdev->dev, "ref");
  307. + if (IS_ERR(dphy->ref_clk))
  308. + return PTR_ERR(dphy->ref_clk);
  309. +
  310. + dphy->tx_clk = devm_clk_get(&pdev->dev, "tx");
  311. + if (IS_ERR(dphy->tx_clk))
  312. + return PTR_ERR(dphy->tx_clk);
  313. +
  314. + dphy->rstc = devm_reset_control_array_get_exclusive(&pdev->dev);
  315. + if (IS_ERR(dphy->rstc))
  316. + return PTR_ERR(dphy->rstc);
  317. +
  318. + dphy->mipi_0p9 = devm_regulator_get(&pdev->dev, "mipi_0p9");
  319. + if (IS_ERR(dphy->mipi_0p9))
  320. + return PTR_ERR(dphy->mipi_0p9);
  321. +
  322. + dphy->phy = devm_phy_create(&pdev->dev, NULL, &stf_dphy_ops);
  323. + if (IS_ERR(dphy->phy)) {
  324. + dev_err(&pdev->dev, "Failed to create PHY\n");
  325. + return PTR_ERR(dphy->phy);
  326. + }
  327. +
  328. + pm_runtime_enable(&pdev->dev);
  329. +
  330. + phy_set_drvdata(dphy->phy, dphy);
  331. + phy_provider = devm_of_phy_provider_register(&pdev->dev,
  332. + of_phy_simple_xlate);
  333. +
  334. + return PTR_ERR_OR_ZERO(phy_provider);
  335. +}
  336. +
  337. +static const struct stf_dphy_info starfive_dphy_info = {
  338. + .maps = {4, 0, 1, 2, 3, 5},
  339. +};
  340. +
  341. +static const struct of_device_id stf_dphy_dt_ids[] = {
  342. + {
  343. + .compatible = "starfive,jh7110-dphy-rx",
  344. + .data = &starfive_dphy_info,
  345. + },
  346. + { /* sentinel */ },
  347. +};
  348. +MODULE_DEVICE_TABLE(of, stf_dphy_dt_ids);
  349. +
  350. +static struct platform_driver stf_dphy_driver = {
  351. + .probe = stf_dphy_probe,
  352. + .driver = {
  353. + .name = "starfive-dphy-rx",
  354. + .of_match_table = stf_dphy_dt_ids,
  355. + },
  356. +};
  357. +module_platform_driver(stf_dphy_driver);
  358. +
  359. +MODULE_AUTHOR("Jack Zhu <[email protected]>");
  360. +MODULE_AUTHOR("Changhuang Liang <[email protected]>");
  361. +MODULE_DESCRIPTION("StarFive DPHY RX driver");
  362. +MODULE_LICENSE("GPL");