dw_mipi_dsi-stm.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2017
  4. *
  5. * Authors: Philippe Cornu <philippe.cornu@st.com>
  6. * Yannick Fertre <yannick.fertre@st.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/module.h>
  11. #include <drm/drmP.h>
  12. #include <drm/drm_mipi_dsi.h>
  13. #include <drm/bridge/dw_mipi_dsi.h>
  14. #include <video/mipi_display.h>
  15. #define HWVER_130 0x31333000 /* IP version 1.30 */
  16. #define HWVER_131 0x31333100 /* IP version 1.31 */
  17. /* DSI digital registers & bit definitions */
  18. #define DSI_VERSION 0x00
  19. #define VERSION GENMASK(31, 8)
  20. /* DSI wrapper registers & bit definitions */
  21. /* Note: registers are named as in the Reference Manual */
  22. #define DSI_WCFGR 0x0400 /* Wrapper ConFiGuration Reg */
  23. #define WCFGR_DSIM BIT(0) /* DSI Mode */
  24. #define WCFGR_COLMUX GENMASK(3, 1) /* COLor MUltipleXing */
  25. #define DSI_WCR 0x0404 /* Wrapper Control Reg */
  26. #define WCR_DSIEN BIT(3) /* DSI ENable */
  27. #define DSI_WISR 0x040C /* Wrapper Interrupt and Status Reg */
  28. #define WISR_PLLLS BIT(8) /* PLL Lock Status */
  29. #define WISR_RRS BIT(12) /* Regulator Ready Status */
  30. #define DSI_WPCR0 0x0418 /* Wrapper Phy Conf Reg 0 */
  31. #define WPCR0_UIX4 GENMASK(5, 0) /* Unit Interval X 4 */
  32. #define WPCR0_TDDL BIT(16) /* Turn Disable Data Lanes */
  33. #define DSI_WRPCR 0x0430 /* Wrapper Regulator & Pll Ctrl Reg */
  34. #define WRPCR_PLLEN BIT(0) /* PLL ENable */
  35. #define WRPCR_NDIV GENMASK(8, 2) /* pll loop DIVision Factor */
  36. #define WRPCR_IDF GENMASK(14, 11) /* pll Input Division Factor */
  37. #define WRPCR_ODF GENMASK(17, 16) /* pll Output Division Factor */
  38. #define WRPCR_REGEN BIT(24) /* REGulator ENable */
  39. #define WRPCR_BGREN BIT(28) /* BandGap Reference ENable */
  40. #define IDF_MIN 1
  41. #define IDF_MAX 7
  42. #define NDIV_MIN 10
  43. #define NDIV_MAX 125
  44. #define ODF_MIN 1
  45. #define ODF_MAX 8
  46. /* dsi color format coding according to the datasheet */
  47. enum dsi_color {
  48. DSI_RGB565_CONF1,
  49. DSI_RGB565_CONF2,
  50. DSI_RGB565_CONF3,
  51. DSI_RGB666_CONF1,
  52. DSI_RGB666_CONF2,
  53. DSI_RGB888,
  54. };
  55. #define LANE_MIN_KBPS 31250
  56. #define LANE_MAX_KBPS 500000
  57. /* Sleep & timeout for regulator on/off, pll lock/unlock & fifo empty */
  58. #define SLEEP_US 1000
  59. #define TIMEOUT_US 200000
  60. struct dw_mipi_dsi_stm {
  61. void __iomem *base;
  62. struct clk *pllref_clk;
  63. struct dw_mipi_dsi *dsi;
  64. u32 hw_version;
  65. int lane_min_kbps;
  66. int lane_max_kbps;
  67. };
  68. static inline void dsi_write(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 val)
  69. {
  70. writel(val, dsi->base + reg);
  71. }
  72. static inline u32 dsi_read(struct dw_mipi_dsi_stm *dsi, u32 reg)
  73. {
  74. return readl(dsi->base + reg);
  75. }
  76. static inline void dsi_set(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 mask)
  77. {
  78. dsi_write(dsi, reg, dsi_read(dsi, reg) | mask);
  79. }
  80. static inline void dsi_clear(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 mask)
  81. {
  82. dsi_write(dsi, reg, dsi_read(dsi, reg) & ~mask);
  83. }
  84. static inline void dsi_update_bits(struct dw_mipi_dsi_stm *dsi, u32 reg,
  85. u32 mask, u32 val)
  86. {
  87. dsi_write(dsi, reg, (dsi_read(dsi, reg) & ~mask) | val);
  88. }
  89. static enum dsi_color dsi_color_from_mipi(enum mipi_dsi_pixel_format fmt)
  90. {
  91. switch (fmt) {
  92. case MIPI_DSI_FMT_RGB888:
  93. return DSI_RGB888;
  94. case MIPI_DSI_FMT_RGB666:
  95. return DSI_RGB666_CONF2;
  96. case MIPI_DSI_FMT_RGB666_PACKED:
  97. return DSI_RGB666_CONF1;
  98. case MIPI_DSI_FMT_RGB565:
  99. return DSI_RGB565_CONF1;
  100. default:
  101. DRM_DEBUG_DRIVER("MIPI color invalid, so we use rgb888\n");
  102. }
  103. return DSI_RGB888;
  104. }
  105. static int dsi_pll_get_clkout_khz(int clkin_khz, int idf, int ndiv, int odf)
  106. {
  107. int divisor = idf * odf;
  108. /* prevent from division by 0 */
  109. if (!divisor)
  110. return 0;
  111. return DIV_ROUND_CLOSEST(clkin_khz * ndiv, divisor);
  112. }
  113. static int dsi_pll_get_params(struct dw_mipi_dsi_stm *dsi,
  114. int clkin_khz, int clkout_khz,
  115. int *idf, int *ndiv, int *odf)
  116. {
  117. int i, o, n, n_min, n_max;
  118. int fvco_min, fvco_max, delta, best_delta; /* all in khz */
  119. /* Early checks preventing division by 0 & odd results */
  120. if (clkin_khz <= 0 || clkout_khz <= 0)
  121. return -EINVAL;
  122. fvco_min = dsi->lane_min_kbps * 2 * ODF_MAX;
  123. fvco_max = dsi->lane_max_kbps * 2 * ODF_MIN;
  124. best_delta = 1000000; /* big started value (1000000khz) */
  125. for (i = IDF_MIN; i <= IDF_MAX; i++) {
  126. /* Compute ndiv range according to Fvco */
  127. n_min = ((fvco_min * i) / (2 * clkin_khz)) + 1;
  128. n_max = (fvco_max * i) / (2 * clkin_khz);
  129. /* No need to continue idf loop if we reach ndiv max */
  130. if (n_min >= NDIV_MAX)
  131. break;
  132. /* Clamp ndiv to valid values */
  133. if (n_min < NDIV_MIN)
  134. n_min = NDIV_MIN;
  135. if (n_max > NDIV_MAX)
  136. n_max = NDIV_MAX;
  137. for (o = ODF_MIN; o <= ODF_MAX; o *= 2) {
  138. n = DIV_ROUND_CLOSEST(i * o * clkout_khz, clkin_khz);
  139. /* Check ndiv according to vco range */
  140. if (n < n_min || n > n_max)
  141. continue;
  142. /* Check if new delta is better & saves parameters */
  143. delta = dsi_pll_get_clkout_khz(clkin_khz, i, n, o) -
  144. clkout_khz;
  145. if (delta < 0)
  146. delta = -delta;
  147. if (delta < best_delta) {
  148. *idf = i;
  149. *ndiv = n;
  150. *odf = o;
  151. best_delta = delta;
  152. }
  153. /* fast return in case of "perfect result" */
  154. if (!delta)
  155. return 0;
  156. }
  157. }
  158. return 0;
  159. }
  160. static int dw_mipi_dsi_phy_init(void *priv_data)
  161. {
  162. struct dw_mipi_dsi_stm *dsi = priv_data;
  163. u32 val;
  164. int ret;
  165. /* Enable the regulator */
  166. dsi_set(dsi, DSI_WRPCR, WRPCR_REGEN | WRPCR_BGREN);
  167. ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_RRS,
  168. SLEEP_US, TIMEOUT_US);
  169. if (ret)
  170. DRM_DEBUG_DRIVER("!TIMEOUT! waiting REGU, let's continue\n");
  171. /* Enable the DSI PLL & wait for its lock */
  172. dsi_set(dsi, DSI_WRPCR, WRPCR_PLLEN);
  173. ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_PLLLS,
  174. SLEEP_US, TIMEOUT_US);
  175. if (ret)
  176. DRM_DEBUG_DRIVER("!TIMEOUT! waiting PLL, let's continue\n");
  177. /* Enable the DSI wrapper */
  178. dsi_set(dsi, DSI_WCR, WCR_DSIEN);
  179. return 0;
  180. }
  181. static int
  182. dw_mipi_dsi_get_lane_mbps(void *priv_data, struct drm_display_mode *mode,
  183. unsigned long mode_flags, u32 lanes, u32 format,
  184. unsigned int *lane_mbps)
  185. {
  186. struct dw_mipi_dsi_stm *dsi = priv_data;
  187. unsigned int idf, ndiv, odf, pll_in_khz, pll_out_khz;
  188. int ret, bpp;
  189. u32 val;
  190. /* Update lane capabilities according to hw version */
  191. dsi->hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
  192. dsi->lane_min_kbps = LANE_MIN_KBPS;
  193. dsi->lane_max_kbps = LANE_MAX_KBPS;
  194. if (dsi->hw_version == HWVER_131) {
  195. dsi->lane_min_kbps *= 2;
  196. dsi->lane_max_kbps *= 2;
  197. }
  198. pll_in_khz = (unsigned int)(clk_get_rate(dsi->pllref_clk) / 1000);
  199. /* Compute requested pll out */
  200. bpp = mipi_dsi_pixel_format_to_bpp(format);
  201. pll_out_khz = mode->clock * bpp / lanes;
  202. /* Add 20% to pll out to be higher than pixel bw (burst mode only) */
  203. pll_out_khz = (pll_out_khz * 12) / 10;
  204. if (pll_out_khz > dsi->lane_max_kbps) {
  205. pll_out_khz = dsi->lane_max_kbps;
  206. DRM_WARN("Warning max phy mbps is used\n");
  207. }
  208. if (pll_out_khz < dsi->lane_min_kbps) {
  209. pll_out_khz = dsi->lane_min_kbps;
  210. DRM_WARN("Warning min phy mbps is used\n");
  211. }
  212. /* Compute best pll parameters */
  213. idf = 0;
  214. ndiv = 0;
  215. odf = 0;
  216. ret = dsi_pll_get_params(dsi, pll_in_khz, pll_out_khz,
  217. &idf, &ndiv, &odf);
  218. if (ret)
  219. DRM_WARN("Warning dsi_pll_get_params(): bad params\n");
  220. /* Get the adjusted pll out value */
  221. pll_out_khz = dsi_pll_get_clkout_khz(pll_in_khz, idf, ndiv, odf);
  222. /* Set the PLL division factors */
  223. dsi_update_bits(dsi, DSI_WRPCR, WRPCR_NDIV | WRPCR_IDF | WRPCR_ODF,
  224. (ndiv << 2) | (idf << 11) | ((ffs(odf) - 1) << 16));
  225. /* Compute uix4 & set the bit period in high-speed mode */
  226. val = 4000000 / pll_out_khz;
  227. dsi_update_bits(dsi, DSI_WPCR0, WPCR0_UIX4, val);
  228. /* Select video mode by resetting DSIM bit */
  229. dsi_clear(dsi, DSI_WCFGR, WCFGR_DSIM);
  230. /* Select the color coding */
  231. dsi_update_bits(dsi, DSI_WCFGR, WCFGR_COLMUX,
  232. dsi_color_from_mipi(format) << 1);
  233. *lane_mbps = pll_out_khz / 1000;
  234. DRM_DEBUG_DRIVER("pll_in %ukHz pll_out %ukHz lane_mbps %uMHz\n",
  235. pll_in_khz, pll_out_khz, *lane_mbps);
  236. return 0;
  237. }
  238. static const struct dw_mipi_dsi_phy_ops dw_mipi_dsi_stm_phy_ops = {
  239. .init = dw_mipi_dsi_phy_init,
  240. .get_lane_mbps = dw_mipi_dsi_get_lane_mbps,
  241. };
  242. static struct dw_mipi_dsi_plat_data dw_mipi_dsi_stm_plat_data = {
  243. .max_data_lanes = 2,
  244. .phy_ops = &dw_mipi_dsi_stm_phy_ops,
  245. };
  246. static const struct of_device_id dw_mipi_dsi_stm_dt_ids[] = {
  247. { .compatible = "st,stm32-dsi", .data = &dw_mipi_dsi_stm_plat_data, },
  248. { },
  249. };
  250. MODULE_DEVICE_TABLE(of, dw_mipi_dsi_stm_dt_ids);
  251. static int dw_mipi_dsi_stm_probe(struct platform_device *pdev)
  252. {
  253. struct device *dev = &pdev->dev;
  254. struct dw_mipi_dsi_stm *dsi;
  255. struct resource *res;
  256. int ret;
  257. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  258. if (!dsi)
  259. return -ENOMEM;
  260. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  261. dsi->base = devm_ioremap_resource(dev, res);
  262. if (IS_ERR(dsi->base)) {
  263. DRM_ERROR("Unable to get dsi registers\n");
  264. return PTR_ERR(dsi->base);
  265. }
  266. dsi->pllref_clk = devm_clk_get(dev, "ref");
  267. if (IS_ERR(dsi->pllref_clk)) {
  268. ret = PTR_ERR(dsi->pllref_clk);
  269. dev_err(dev, "Unable to get pll reference clock: %d\n", ret);
  270. return ret;
  271. }
  272. ret = clk_prepare_enable(dsi->pllref_clk);
  273. if (ret) {
  274. dev_err(dev, "%s: Failed to enable pllref_clk\n", __func__);
  275. return ret;
  276. }
  277. dw_mipi_dsi_stm_plat_data.base = dsi->base;
  278. dw_mipi_dsi_stm_plat_data.priv_data = dsi;
  279. platform_set_drvdata(pdev, dsi);
  280. dsi->dsi = dw_mipi_dsi_probe(pdev, &dw_mipi_dsi_stm_plat_data);
  281. if (IS_ERR(dsi->dsi)) {
  282. DRM_ERROR("Failed to initialize mipi dsi host\n");
  283. clk_disable_unprepare(dsi->pllref_clk);
  284. return PTR_ERR(dsi->dsi);
  285. }
  286. return 0;
  287. }
  288. static int dw_mipi_dsi_stm_remove(struct platform_device *pdev)
  289. {
  290. struct dw_mipi_dsi_stm *dsi = platform_get_drvdata(pdev);
  291. clk_disable_unprepare(dsi->pllref_clk);
  292. dw_mipi_dsi_remove(dsi->dsi);
  293. return 0;
  294. }
  295. static struct platform_driver dw_mipi_dsi_stm_driver = {
  296. .probe = dw_mipi_dsi_stm_probe,
  297. .remove = dw_mipi_dsi_stm_remove,
  298. .driver = {
  299. .of_match_table = dw_mipi_dsi_stm_dt_ids,
  300. .name = "stm32-display-dsi",
  301. },
  302. };
  303. module_platform_driver(dw_mipi_dsi_stm_driver);
  304. MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
  305. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  306. MODULE_DESCRIPTION("STMicroelectronics DW MIPI DSI host controller driver");
  307. MODULE_LICENSE("GPL v2");