phy-rockchip-inno-csidphy.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Rockchip MIPI RX Innosilicon DPHY driver
  4. *
  5. * Copyright (C) 2021 Fuzhou Rockchip Electronics Co., Ltd.
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/io.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/phy/phy.h>
  16. #include <linux/phy/phy-mipi-dphy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/regmap.h>
  20. #include <linux/reset.h>
  21. /* GRF */
  22. #define RK1808_GRF_PD_VI_CON_OFFSET 0x0430
  23. #define RK3326_GRF_PD_VI_CON_OFFSET 0x0430
  24. #define RK3368_GRF_SOC_CON6_OFFSET 0x0418
  25. #define RK3568_GRF_VI_CON0 0x0340
  26. #define RK3568_GRF_VI_CON1 0x0344
  27. /* PHY */
  28. #define CSIDPHY_CTRL_LANE_ENABLE 0x00
  29. #define CSIDPHY_CTRL_LANE_ENABLE_CK BIT(6)
  30. #define CSIDPHY_CTRL_LANE_ENABLE_MASK GENMASK(5, 2)
  31. #define CSIDPHY_CTRL_LANE_ENABLE_UNDEFINED BIT(0)
  32. /* not present on all variants */
  33. #define CSIDPHY_CTRL_PWRCTL 0x04
  34. #define CSIDPHY_CTRL_PWRCTL_UNDEFINED GENMASK(7, 5)
  35. #define CSIDPHY_CTRL_PWRCTL_SYNCRST BIT(2)
  36. #define CSIDPHY_CTRL_PWRCTL_LDO_PD BIT(1)
  37. #define CSIDPHY_CTRL_PWRCTL_PLL_PD BIT(0)
  38. #define CSIDPHY_CTRL_DIG_RST 0x80
  39. #define CSIDPHY_CTRL_DIG_RST_UNDEFINED 0x1e
  40. #define CSIDPHY_CTRL_DIG_RST_RESET BIT(0)
  41. /* offset after ths_settle_offset */
  42. #define CSIDPHY_CLK_THS_SETTLE 0
  43. #define CSIDPHY_LANE_THS_SETTLE(n) (((n) + 1) * 0x80)
  44. #define CSIDPHY_THS_SETTLE_MASK GENMASK(6, 0)
  45. /* offset after calib_offset */
  46. #define CSIDPHY_CLK_CALIB_EN 0
  47. #define CSIDPHY_LANE_CALIB_EN(n) (((n) + 1) * 0x80)
  48. #define CSIDPHY_CALIB_EN BIT(7)
  49. /* Configure the count time of the THS-SETTLE by protocol. */
  50. #define RK1808_CSIDPHY_CLK_WR_THS_SETTLE 0x160
  51. #define RK3326_CSIDPHY_CLK_WR_THS_SETTLE 0x100
  52. #define RK3368_CSIDPHY_CLK_WR_THS_SETTLE 0x100
  53. #define RK3568_CSIDPHY_CLK_WR_THS_SETTLE 0x160
  54. /* Calibration reception enable */
  55. #define RK1808_CSIDPHY_CLK_CALIB_EN 0x168
  56. #define RK3568_CSIDPHY_CLK_CALIB_EN 0x168
  57. /*
  58. * The higher 16-bit of this register is used for write protection
  59. * only if BIT(x + 16) set to 1 the BIT(x) can be written.
  60. */
  61. #define HIWORD_UPDATE(val, mask, shift) \
  62. ((val) << (shift) | (mask) << ((shift) + 16))
  63. #define HZ_TO_MHZ(freq) div_u64(freq, 1000 * 1000)
  64. enum dphy_reg_id {
  65. /* rk1808 & rk3326 */
  66. GRF_DPHY_CSIPHY_FORCERXMODE,
  67. GRF_DPHY_CSIPHY_CLKLANE_EN,
  68. GRF_DPHY_CSIPHY_DATALANE_EN,
  69. };
  70. struct dphy_reg {
  71. u32 offset;
  72. u32 mask;
  73. u32 shift;
  74. };
  75. #define PHY_REG(_offset, _width, _shift) \
  76. { .offset = _offset, .mask = BIT(_width) - 1, .shift = _shift, }
  77. static const struct dphy_reg rk1808_grf_dphy_regs[] = {
  78. [GRF_DPHY_CSIPHY_FORCERXMODE] = PHY_REG(RK1808_GRF_PD_VI_CON_OFFSET, 4, 0),
  79. [GRF_DPHY_CSIPHY_CLKLANE_EN] = PHY_REG(RK1808_GRF_PD_VI_CON_OFFSET, 1, 8),
  80. [GRF_DPHY_CSIPHY_DATALANE_EN] = PHY_REG(RK1808_GRF_PD_VI_CON_OFFSET, 4, 4),
  81. };
  82. static const struct dphy_reg rk3326_grf_dphy_regs[] = {
  83. [GRF_DPHY_CSIPHY_FORCERXMODE] = PHY_REG(RK3326_GRF_PD_VI_CON_OFFSET, 4, 0),
  84. [GRF_DPHY_CSIPHY_CLKLANE_EN] = PHY_REG(RK3326_GRF_PD_VI_CON_OFFSET, 1, 8),
  85. [GRF_DPHY_CSIPHY_DATALANE_EN] = PHY_REG(RK3326_GRF_PD_VI_CON_OFFSET, 4, 4),
  86. };
  87. static const struct dphy_reg rk3368_grf_dphy_regs[] = {
  88. [GRF_DPHY_CSIPHY_FORCERXMODE] = PHY_REG(RK3368_GRF_SOC_CON6_OFFSET, 4, 8),
  89. };
  90. static const struct dphy_reg rk3568_grf_dphy_regs[] = {
  91. [GRF_DPHY_CSIPHY_FORCERXMODE] = PHY_REG(RK3568_GRF_VI_CON0, 4, 0),
  92. [GRF_DPHY_CSIPHY_DATALANE_EN] = PHY_REG(RK3568_GRF_VI_CON0, 4, 4),
  93. [GRF_DPHY_CSIPHY_CLKLANE_EN] = PHY_REG(RK3568_GRF_VI_CON0, 1, 8),
  94. };
  95. struct hsfreq_range {
  96. u32 range_h;
  97. u8 cfg_bit;
  98. };
  99. struct dphy_drv_data {
  100. int pwrctl_offset;
  101. int ths_settle_offset;
  102. int calib_offset;
  103. const struct hsfreq_range *hsfreq_ranges;
  104. int num_hsfreq_ranges;
  105. const struct dphy_reg *grf_regs;
  106. };
  107. struct rockchip_inno_csidphy {
  108. struct device *dev;
  109. void __iomem *phy_base;
  110. struct clk *pclk;
  111. struct regmap *grf;
  112. struct reset_control *rst;
  113. const struct dphy_drv_data *drv_data;
  114. struct phy_configure_opts_mipi_dphy config;
  115. u8 hsfreq;
  116. };
  117. static inline void write_grf_reg(struct rockchip_inno_csidphy *priv,
  118. int index, u8 value)
  119. {
  120. const struct dphy_drv_data *drv_data = priv->drv_data;
  121. const struct dphy_reg *reg = &drv_data->grf_regs[index];
  122. if (reg->offset)
  123. regmap_write(priv->grf, reg->offset,
  124. HIWORD_UPDATE(value, reg->mask, reg->shift));
  125. }
  126. /* These tables must be sorted by .range_h ascending. */
  127. static const struct hsfreq_range rk1808_mipidphy_hsfreq_ranges[] = {
  128. { 109, 0x02}, { 149, 0x03}, { 199, 0x06}, { 249, 0x06},
  129. { 299, 0x06}, { 399, 0x08}, { 499, 0x0b}, { 599, 0x0e},
  130. { 699, 0x10}, { 799, 0x12}, { 999, 0x16}, {1199, 0x1e},
  131. {1399, 0x23}, {1599, 0x2d}, {1799, 0x32}, {1999, 0x37},
  132. {2199, 0x3c}, {2399, 0x41}, {2499, 0x46}
  133. };
  134. static const struct hsfreq_range rk3326_mipidphy_hsfreq_ranges[] = {
  135. { 109, 0x00}, { 149, 0x01}, { 199, 0x02}, { 249, 0x03},
  136. { 299, 0x04}, { 399, 0x05}, { 499, 0x06}, { 599, 0x07},
  137. { 699, 0x08}, { 799, 0x09}, { 899, 0x0a}, {1099, 0x0b},
  138. {1249, 0x0c}, {1349, 0x0d}, {1500, 0x0e}
  139. };
  140. static const struct hsfreq_range rk3368_mipidphy_hsfreq_ranges[] = {
  141. { 109, 0x00}, { 149, 0x01}, { 199, 0x02}, { 249, 0x03},
  142. { 299, 0x04}, { 399, 0x05}, { 499, 0x06}, { 599, 0x07},
  143. { 699, 0x08}, { 799, 0x09}, { 899, 0x0a}, {1099, 0x0b},
  144. {1249, 0x0c}, {1349, 0x0d}, {1500, 0x0e}
  145. };
  146. static void rockchip_inno_csidphy_ths_settle(struct rockchip_inno_csidphy *priv,
  147. int hsfreq, int offset)
  148. {
  149. const struct dphy_drv_data *drv_data = priv->drv_data;
  150. u32 val;
  151. val = readl(priv->phy_base + drv_data->ths_settle_offset + offset);
  152. val &= ~CSIDPHY_THS_SETTLE_MASK;
  153. val |= hsfreq;
  154. writel(val, priv->phy_base + drv_data->ths_settle_offset + offset);
  155. }
  156. static int rockchip_inno_csidphy_configure(struct phy *phy,
  157. union phy_configure_opts *opts)
  158. {
  159. struct rockchip_inno_csidphy *priv = phy_get_drvdata(phy);
  160. const struct dphy_drv_data *drv_data = priv->drv_data;
  161. struct phy_configure_opts_mipi_dphy *config = &opts->mipi_dphy;
  162. unsigned int hsfreq = 0;
  163. unsigned int i;
  164. u64 data_rate_mbps;
  165. int ret;
  166. /* pass with phy_mipi_dphy_get_default_config (with pixel rate?) */
  167. ret = phy_mipi_dphy_config_validate(config);
  168. if (ret)
  169. return ret;
  170. data_rate_mbps = HZ_TO_MHZ(config->hs_clk_rate);
  171. dev_dbg(priv->dev, "lanes %d - data_rate_mbps %llu\n",
  172. config->lanes, data_rate_mbps);
  173. for (i = 0; i < drv_data->num_hsfreq_ranges; i++) {
  174. if (drv_data->hsfreq_ranges[i].range_h >= data_rate_mbps) {
  175. hsfreq = drv_data->hsfreq_ranges[i].cfg_bit;
  176. break;
  177. }
  178. }
  179. if (!hsfreq)
  180. return -EINVAL;
  181. priv->hsfreq = hsfreq;
  182. priv->config = *config;
  183. return 0;
  184. }
  185. static int rockchip_inno_csidphy_power_on(struct phy *phy)
  186. {
  187. struct rockchip_inno_csidphy *priv = phy_get_drvdata(phy);
  188. const struct dphy_drv_data *drv_data = priv->drv_data;
  189. u64 data_rate_mbps = HZ_TO_MHZ(priv->config.hs_clk_rate);
  190. u32 val;
  191. int ret, i;
  192. ret = clk_enable(priv->pclk);
  193. if (ret < 0)
  194. return ret;
  195. ret = pm_runtime_resume_and_get(priv->dev);
  196. if (ret < 0) {
  197. clk_disable(priv->pclk);
  198. return ret;
  199. }
  200. /* phy start */
  201. if (drv_data->pwrctl_offset >= 0)
  202. writel(CSIDPHY_CTRL_PWRCTL_UNDEFINED |
  203. CSIDPHY_CTRL_PWRCTL_SYNCRST,
  204. priv->phy_base + drv_data->pwrctl_offset);
  205. /* set data lane num and enable clock lane */
  206. val = FIELD_PREP(CSIDPHY_CTRL_LANE_ENABLE_MASK, GENMASK(priv->config.lanes - 1, 0)) |
  207. FIELD_PREP(CSIDPHY_CTRL_LANE_ENABLE_CK, 1) |
  208. FIELD_PREP(CSIDPHY_CTRL_LANE_ENABLE_UNDEFINED, 1);
  209. writel(val, priv->phy_base + CSIDPHY_CTRL_LANE_ENABLE);
  210. /* Reset dphy analog part */
  211. if (drv_data->pwrctl_offset >= 0)
  212. writel(CSIDPHY_CTRL_PWRCTL_UNDEFINED,
  213. priv->phy_base + drv_data->pwrctl_offset);
  214. usleep_range(500, 1000);
  215. /* Reset dphy digital part */
  216. writel(CSIDPHY_CTRL_DIG_RST_UNDEFINED,
  217. priv->phy_base + CSIDPHY_CTRL_DIG_RST);
  218. writel(CSIDPHY_CTRL_DIG_RST_UNDEFINED + CSIDPHY_CTRL_DIG_RST_RESET,
  219. priv->phy_base + CSIDPHY_CTRL_DIG_RST);
  220. /* not into receive mode/wait stopstate */
  221. write_grf_reg(priv, GRF_DPHY_CSIPHY_FORCERXMODE, 0x0);
  222. /* enable calibration */
  223. if (data_rate_mbps > 1500 && drv_data->calib_offset >= 0) {
  224. writel(CSIDPHY_CALIB_EN,
  225. priv->phy_base + drv_data->calib_offset +
  226. CSIDPHY_CLK_CALIB_EN);
  227. for (i = 0; i < priv->config.lanes; i++)
  228. writel(CSIDPHY_CALIB_EN,
  229. priv->phy_base + drv_data->calib_offset +
  230. CSIDPHY_LANE_CALIB_EN(i));
  231. }
  232. rockchip_inno_csidphy_ths_settle(priv, priv->hsfreq,
  233. CSIDPHY_CLK_THS_SETTLE);
  234. for (i = 0; i < priv->config.lanes; i++)
  235. rockchip_inno_csidphy_ths_settle(priv, priv->hsfreq,
  236. CSIDPHY_LANE_THS_SETTLE(i));
  237. write_grf_reg(priv, GRF_DPHY_CSIPHY_CLKLANE_EN, 0x1);
  238. write_grf_reg(priv, GRF_DPHY_CSIPHY_DATALANE_EN,
  239. GENMASK(priv->config.lanes - 1, 0));
  240. return 0;
  241. }
  242. static int rockchip_inno_csidphy_power_off(struct phy *phy)
  243. {
  244. struct rockchip_inno_csidphy *priv = phy_get_drvdata(phy);
  245. const struct dphy_drv_data *drv_data = priv->drv_data;
  246. /* disable all lanes */
  247. writel(CSIDPHY_CTRL_LANE_ENABLE_UNDEFINED,
  248. priv->phy_base + CSIDPHY_CTRL_LANE_ENABLE);
  249. /* disable pll and ldo */
  250. if (drv_data->pwrctl_offset >= 0)
  251. writel(CSIDPHY_CTRL_PWRCTL_UNDEFINED |
  252. CSIDPHY_CTRL_PWRCTL_LDO_PD |
  253. CSIDPHY_CTRL_PWRCTL_PLL_PD,
  254. priv->phy_base + drv_data->pwrctl_offset);
  255. usleep_range(500, 1000);
  256. pm_runtime_put(priv->dev);
  257. clk_disable(priv->pclk);
  258. return 0;
  259. }
  260. static int rockchip_inno_csidphy_init(struct phy *phy)
  261. {
  262. struct rockchip_inno_csidphy *priv = phy_get_drvdata(phy);
  263. return clk_prepare(priv->pclk);
  264. }
  265. static int rockchip_inno_csidphy_exit(struct phy *phy)
  266. {
  267. struct rockchip_inno_csidphy *priv = phy_get_drvdata(phy);
  268. clk_unprepare(priv->pclk);
  269. return 0;
  270. }
  271. static const struct phy_ops rockchip_inno_csidphy_ops = {
  272. .power_on = rockchip_inno_csidphy_power_on,
  273. .power_off = rockchip_inno_csidphy_power_off,
  274. .init = rockchip_inno_csidphy_init,
  275. .exit = rockchip_inno_csidphy_exit,
  276. .configure = rockchip_inno_csidphy_configure,
  277. .owner = THIS_MODULE,
  278. };
  279. static const struct dphy_drv_data rk1808_mipidphy_drv_data = {
  280. .pwrctl_offset = -1,
  281. .ths_settle_offset = RK1808_CSIDPHY_CLK_WR_THS_SETTLE,
  282. .calib_offset = RK1808_CSIDPHY_CLK_CALIB_EN,
  283. .hsfreq_ranges = rk1808_mipidphy_hsfreq_ranges,
  284. .num_hsfreq_ranges = ARRAY_SIZE(rk1808_mipidphy_hsfreq_ranges),
  285. .grf_regs = rk1808_grf_dphy_regs,
  286. };
  287. static const struct dphy_drv_data rk3326_mipidphy_drv_data = {
  288. .pwrctl_offset = CSIDPHY_CTRL_PWRCTL,
  289. .ths_settle_offset = RK3326_CSIDPHY_CLK_WR_THS_SETTLE,
  290. .calib_offset = -1,
  291. .hsfreq_ranges = rk3326_mipidphy_hsfreq_ranges,
  292. .num_hsfreq_ranges = ARRAY_SIZE(rk3326_mipidphy_hsfreq_ranges),
  293. .grf_regs = rk3326_grf_dphy_regs,
  294. };
  295. static const struct dphy_drv_data rk3368_mipidphy_drv_data = {
  296. .pwrctl_offset = CSIDPHY_CTRL_PWRCTL,
  297. .ths_settle_offset = RK3368_CSIDPHY_CLK_WR_THS_SETTLE,
  298. .calib_offset = -1,
  299. .hsfreq_ranges = rk3368_mipidphy_hsfreq_ranges,
  300. .num_hsfreq_ranges = ARRAY_SIZE(rk3368_mipidphy_hsfreq_ranges),
  301. .grf_regs = rk3368_grf_dphy_regs,
  302. };
  303. static const struct dphy_drv_data rk3568_mipidphy_drv_data = {
  304. .pwrctl_offset = -1,
  305. .ths_settle_offset = RK3568_CSIDPHY_CLK_WR_THS_SETTLE,
  306. .calib_offset = RK3568_CSIDPHY_CLK_CALIB_EN,
  307. .hsfreq_ranges = rk1808_mipidphy_hsfreq_ranges,
  308. .num_hsfreq_ranges = ARRAY_SIZE(rk1808_mipidphy_hsfreq_ranges),
  309. .grf_regs = rk3568_grf_dphy_regs,
  310. };
  311. static const struct of_device_id rockchip_inno_csidphy_match_id[] = {
  312. {
  313. .compatible = "rockchip,px30-csi-dphy",
  314. .data = &rk3326_mipidphy_drv_data,
  315. },
  316. {
  317. .compatible = "rockchip,rk1808-csi-dphy",
  318. .data = &rk1808_mipidphy_drv_data,
  319. },
  320. {
  321. .compatible = "rockchip,rk3326-csi-dphy",
  322. .data = &rk3326_mipidphy_drv_data,
  323. },
  324. {
  325. .compatible = "rockchip,rk3368-csi-dphy",
  326. .data = &rk3368_mipidphy_drv_data,
  327. },
  328. {
  329. .compatible = "rockchip,rk3568-csi-dphy",
  330. .data = &rk3568_mipidphy_drv_data,
  331. },
  332. {}
  333. };
  334. MODULE_DEVICE_TABLE(of, rockchip_inno_csidphy_match_id);
  335. static int rockchip_inno_csidphy_probe(struct platform_device *pdev)
  336. {
  337. struct rockchip_inno_csidphy *priv;
  338. struct device *dev = &pdev->dev;
  339. struct phy_provider *phy_provider;
  340. struct phy *phy;
  341. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  342. if (!priv)
  343. return -ENOMEM;
  344. priv->dev = dev;
  345. platform_set_drvdata(pdev, priv);
  346. priv->drv_data = of_device_get_match_data(dev);
  347. if (!priv->drv_data) {
  348. dev_err(dev, "Can't find device data\n");
  349. return -ENODEV;
  350. }
  351. priv->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
  352. "rockchip,grf");
  353. if (IS_ERR(priv->grf)) {
  354. dev_err(dev, "Can't find GRF syscon\n");
  355. return PTR_ERR(priv->grf);
  356. }
  357. priv->phy_base = devm_platform_ioremap_resource(pdev, 0);
  358. if (IS_ERR(priv->phy_base))
  359. return PTR_ERR(priv->phy_base);
  360. priv->pclk = devm_clk_get(dev, "pclk");
  361. if (IS_ERR(priv->pclk)) {
  362. dev_err(dev, "failed to get pclk\n");
  363. return PTR_ERR(priv->pclk);
  364. }
  365. priv->rst = devm_reset_control_get(dev, "apb");
  366. if (IS_ERR(priv->rst)) {
  367. dev_err(dev, "failed to get system reset control\n");
  368. return PTR_ERR(priv->rst);
  369. }
  370. phy = devm_phy_create(dev, NULL, &rockchip_inno_csidphy_ops);
  371. if (IS_ERR(phy)) {
  372. dev_err(dev, "failed to create phy\n");
  373. return PTR_ERR(phy);
  374. }
  375. phy_set_drvdata(phy, priv);
  376. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  377. if (IS_ERR(phy_provider)) {
  378. dev_err(dev, "failed to register phy provider\n");
  379. return PTR_ERR(phy_provider);
  380. }
  381. pm_runtime_enable(dev);
  382. return 0;
  383. }
  384. static int rockchip_inno_csidphy_remove(struct platform_device *pdev)
  385. {
  386. struct rockchip_inno_csidphy *priv = platform_get_drvdata(pdev);
  387. pm_runtime_disable(priv->dev);
  388. return 0;
  389. }
  390. static struct platform_driver rockchip_inno_csidphy_driver = {
  391. .driver = {
  392. .name = "rockchip-inno-csidphy",
  393. .of_match_table = rockchip_inno_csidphy_match_id,
  394. },
  395. .probe = rockchip_inno_csidphy_probe,
  396. .remove = rockchip_inno_csidphy_remove,
  397. };
  398. module_platform_driver(rockchip_inno_csidphy_driver);
  399. MODULE_AUTHOR("Heiko Stuebner <[email protected]>");
  400. MODULE_DESCRIPTION("Rockchip MIPI Innosilicon CSI-DPHY driver");
  401. MODULE_LICENSE("GPL v2");