1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2017
5 * Authors: Philippe Cornu <philippe.cornu@st.com>
6 * Yannick Fertre <yannick.fertre@st.com>
10 #include <linux/iopoll.h>
11 #include <linux/module.h>
12 #include <linux/regulator/consumer.h>
14 #include <drm/drm_mipi_dsi.h>
15 #include <drm/bridge/dw_mipi_dsi.h>
16 #include <video/mipi_display.h>
18 #define HWVER_130 0x31333000 /* IP version 1.30 */
19 #define HWVER_131 0x31333100 /* IP version 1.31 */
21 /* DSI digital registers & bit definitions */
22 #define DSI_VERSION 0x00
23 #define VERSION GENMASK(31, 8)
25 /* DSI wrapper registers & bit definitions */
26 /* Note: registers are named as in the Reference Manual */
27 #define DSI_WCFGR 0x0400 /* Wrapper ConFiGuration Reg */
28 #define WCFGR_DSIM BIT(0) /* DSI Mode */
29 #define WCFGR_COLMUX GENMASK(3, 1) /* COLor MUltipleXing */
31 #define DSI_WCR 0x0404 /* Wrapper Control Reg */
32 #define WCR_DSIEN BIT(3) /* DSI ENable */
34 #define DSI_WISR 0x040C /* Wrapper Interrupt and Status Reg */
35 #define WISR_PLLLS BIT(8) /* PLL Lock Status */
36 #define WISR_RRS BIT(12) /* Regulator Ready Status */
38 #define DSI_WPCR0 0x0418 /* Wrapper Phy Conf Reg 0 */
39 #define WPCR0_UIX4 GENMASK(5, 0) /* Unit Interval X 4 */
40 #define WPCR0_TDDL BIT(16) /* Turn Disable Data Lanes */
42 #define DSI_WRPCR 0x0430 /* Wrapper Regulator & Pll Ctrl Reg */
43 #define WRPCR_PLLEN BIT(0) /* PLL ENable */
44 #define WRPCR_NDIV GENMASK(8, 2) /* pll loop DIVision Factor */
45 #define WRPCR_IDF GENMASK(14, 11) /* pll Input Division Factor */
46 #define WRPCR_ODF GENMASK(17, 16) /* pll Output Division Factor */
47 #define WRPCR_REGEN BIT(24) /* REGulator ENable */
48 #define WRPCR_BGREN BIT(28) /* BandGap Reference ENable */
56 /* dsi color format coding according to the datasheet */
66 #define LANE_MIN_KBPS 31250
67 #define LANE_MAX_KBPS 500000
69 /* Sleep & timeout for regulator on/off, pll lock/unlock & fifo empty */
71 #define TIMEOUT_US 200000
73 struct dw_mipi_dsi_stm {
75 struct clk *pllref_clk;
76 struct dw_mipi_dsi *dsi;
80 struct regulator *vdd_supply;
83 static inline void dsi_write(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 val)
85 writel(val, dsi->base + reg);
88 static inline u32 dsi_read(struct dw_mipi_dsi_stm *dsi, u32 reg)
90 return readl(dsi->base + reg);
93 static inline void dsi_set(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 mask)
95 dsi_write(dsi, reg, dsi_read(dsi, reg) | mask);
98 static inline void dsi_clear(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 mask)
100 dsi_write(dsi, reg, dsi_read(dsi, reg) & ~mask);
103 static inline void dsi_update_bits(struct dw_mipi_dsi_stm *dsi, u32 reg,
106 dsi_write(dsi, reg, (dsi_read(dsi, reg) & ~mask) | val);
109 static enum dsi_color dsi_color_from_mipi(enum mipi_dsi_pixel_format fmt)
112 case MIPI_DSI_FMT_RGB888:
114 case MIPI_DSI_FMT_RGB666:
115 return DSI_RGB666_CONF2;
116 case MIPI_DSI_FMT_RGB666_PACKED:
117 return DSI_RGB666_CONF1;
118 case MIPI_DSI_FMT_RGB565:
119 return DSI_RGB565_CONF1;
121 DRM_DEBUG_DRIVER("MIPI color invalid, so we use rgb888\n");
126 static int dsi_pll_get_clkout_khz(int clkin_khz, int idf, int ndiv, int odf)
128 int divisor = idf * odf;
130 /* prevent from division by 0 */
134 return DIV_ROUND_CLOSEST(clkin_khz * ndiv, divisor);
137 static int dsi_pll_get_params(struct dw_mipi_dsi_stm *dsi,
138 int clkin_khz, int clkout_khz,
139 int *idf, int *ndiv, int *odf)
141 int i, o, n, n_min, n_max;
142 int fvco_min, fvco_max, delta, best_delta; /* all in khz */
144 /* Early checks preventing division by 0 & odd results */
145 if (clkin_khz <= 0 || clkout_khz <= 0)
148 fvco_min = dsi->lane_min_kbps * 2 * ODF_MAX;
149 fvco_max = dsi->lane_max_kbps * 2 * ODF_MIN;
151 best_delta = 1000000; /* big started value (1000000khz) */
153 for (i = IDF_MIN; i <= IDF_MAX; i++) {
154 /* Compute ndiv range according to Fvco */
155 n_min = ((fvco_min * i) / (2 * clkin_khz)) + 1;
156 n_max = (fvco_max * i) / (2 * clkin_khz);
158 /* No need to continue idf loop if we reach ndiv max */
159 if (n_min >= NDIV_MAX)
162 /* Clamp ndiv to valid values */
163 if (n_min < NDIV_MIN)
165 if (n_max > NDIV_MAX)
168 for (o = ODF_MIN; o <= ODF_MAX; o *= 2) {
169 n = DIV_ROUND_CLOSEST(i * o * clkout_khz, clkin_khz);
170 /* Check ndiv according to vco range */
171 if (n < n_min || n > n_max)
173 /* Check if new delta is better & saves parameters */
174 delta = dsi_pll_get_clkout_khz(clkin_khz, i, n, o) -
178 if (delta < best_delta) {
184 /* fast return in case of "perfect result" */
193 static int dw_mipi_dsi_phy_init(void *priv_data)
195 struct dw_mipi_dsi_stm *dsi = priv_data;
199 /* Enable the regulator */
200 dsi_set(dsi, DSI_WRPCR, WRPCR_REGEN | WRPCR_BGREN);
201 ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_RRS,
202 SLEEP_US, TIMEOUT_US);
204 DRM_DEBUG_DRIVER("!TIMEOUT! waiting REGU, let's continue\n");
206 /* Enable the DSI PLL & wait for its lock */
207 dsi_set(dsi, DSI_WRPCR, WRPCR_PLLEN);
208 ret = readl_poll_timeout(dsi->base + DSI_WISR, val, val & WISR_PLLLS,
209 SLEEP_US, TIMEOUT_US);
211 DRM_DEBUG_DRIVER("!TIMEOUT! waiting PLL, let's continue\n");
216 static void dw_mipi_dsi_phy_power_on(void *priv_data)
218 struct dw_mipi_dsi_stm *dsi = priv_data;
220 DRM_DEBUG_DRIVER("\n");
222 /* Enable the DSI wrapper */
223 dsi_set(dsi, DSI_WCR, WCR_DSIEN);
226 static void dw_mipi_dsi_phy_power_off(void *priv_data)
228 struct dw_mipi_dsi_stm *dsi = priv_data;
230 DRM_DEBUG_DRIVER("\n");
232 /* Disable the DSI wrapper */
233 dsi_clear(dsi, DSI_WCR, WCR_DSIEN);
237 dw_mipi_dsi_get_lane_mbps(void *priv_data, const struct drm_display_mode *mode,
238 unsigned long mode_flags, u32 lanes, u32 format,
239 unsigned int *lane_mbps)
241 struct dw_mipi_dsi_stm *dsi = priv_data;
242 unsigned int idf, ndiv, odf, pll_in_khz, pll_out_khz;
246 /* Update lane capabilities according to hw version */
247 dsi->lane_min_kbps = LANE_MIN_KBPS;
248 dsi->lane_max_kbps = LANE_MAX_KBPS;
249 if (dsi->hw_version == HWVER_131) {
250 dsi->lane_min_kbps *= 2;
251 dsi->lane_max_kbps *= 2;
254 pll_in_khz = (unsigned int)(clk_get_rate(dsi->pllref_clk) / 1000);
256 /* Compute requested pll out */
257 bpp = mipi_dsi_pixel_format_to_bpp(format);
258 pll_out_khz = mode->clock * bpp / lanes;
259 /* Add 20% to pll out to be higher than pixel bw (burst mode only) */
260 pll_out_khz = (pll_out_khz * 12) / 10;
261 if (pll_out_khz > dsi->lane_max_kbps) {
262 pll_out_khz = dsi->lane_max_kbps;
263 DRM_WARN("Warning max phy mbps is used\n");
265 if (pll_out_khz < dsi->lane_min_kbps) {
266 pll_out_khz = dsi->lane_min_kbps;
267 DRM_WARN("Warning min phy mbps is used\n");
270 /* Compute best pll parameters */
274 ret = dsi_pll_get_params(dsi, pll_in_khz, pll_out_khz,
277 DRM_WARN("Warning dsi_pll_get_params(): bad params\n");
279 /* Get the adjusted pll out value */
280 pll_out_khz = dsi_pll_get_clkout_khz(pll_in_khz, idf, ndiv, odf);
282 /* Set the PLL division factors */
283 dsi_update_bits(dsi, DSI_WRPCR, WRPCR_NDIV | WRPCR_IDF | WRPCR_ODF,
284 (ndiv << 2) | (idf << 11) | ((ffs(odf) - 1) << 16));
286 /* Compute uix4 & set the bit period in high-speed mode */
287 val = 4000000 / pll_out_khz;
288 dsi_update_bits(dsi, DSI_WPCR0, WPCR0_UIX4, val);
290 /* Select video mode by resetting DSIM bit */
291 dsi_clear(dsi, DSI_WCFGR, WCFGR_DSIM);
293 /* Select the color coding */
294 dsi_update_bits(dsi, DSI_WCFGR, WCFGR_COLMUX,
295 dsi_color_from_mipi(format) << 1);
297 *lane_mbps = pll_out_khz / 1000;
299 DRM_DEBUG_DRIVER("pll_in %ukHz pll_out %ukHz lane_mbps %uMHz\n",
300 pll_in_khz, pll_out_khz, *lane_mbps);
305 static const struct dw_mipi_dsi_phy_ops dw_mipi_dsi_stm_phy_ops = {
306 .init = dw_mipi_dsi_phy_init,
307 .power_on = dw_mipi_dsi_phy_power_on,
308 .power_off = dw_mipi_dsi_phy_power_off,
309 .get_lane_mbps = dw_mipi_dsi_get_lane_mbps,
312 static struct dw_mipi_dsi_plat_data dw_mipi_dsi_stm_plat_data = {
314 .phy_ops = &dw_mipi_dsi_stm_phy_ops,
317 static const struct of_device_id dw_mipi_dsi_stm_dt_ids[] = {
318 { .compatible = "st,stm32-dsi", .data = &dw_mipi_dsi_stm_plat_data, },
321 MODULE_DEVICE_TABLE(of, dw_mipi_dsi_stm_dt_ids);
323 static int dw_mipi_dsi_stm_probe(struct platform_device *pdev)
325 struct device *dev = &pdev->dev;
326 struct dw_mipi_dsi_stm *dsi;
328 struct resource *res;
331 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
335 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
336 dsi->base = devm_ioremap_resource(dev, res);
337 if (IS_ERR(dsi->base)) {
338 ret = PTR_ERR(dsi->base);
339 DRM_ERROR("Unable to get dsi registers %d\n", ret);
343 dsi->vdd_supply = devm_regulator_get(dev, "phy-dsi");
344 if (IS_ERR(dsi->vdd_supply)) {
345 ret = PTR_ERR(dsi->vdd_supply);
346 if (ret != -EPROBE_DEFER)
347 DRM_ERROR("Failed to request regulator: %d\n", ret);
351 ret = regulator_enable(dsi->vdd_supply);
353 DRM_ERROR("Failed to enable regulator: %d\n", ret);
357 dsi->pllref_clk = devm_clk_get(dev, "ref");
358 if (IS_ERR(dsi->pllref_clk)) {
359 ret = PTR_ERR(dsi->pllref_clk);
360 DRM_ERROR("Unable to get pll reference clock: %d\n", ret);
364 ret = clk_prepare_enable(dsi->pllref_clk);
366 DRM_ERROR("Failed to enable pllref_clk: %d\n", ret);
370 pclk = devm_clk_get(dev, "pclk");
373 DRM_ERROR("Unable to get peripheral clock: %d\n", ret);
377 ret = clk_prepare_enable(pclk);
379 DRM_ERROR("%s: Failed to enable peripheral clk\n", __func__);
383 dsi->hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
384 clk_disable_unprepare(pclk);
386 if (dsi->hw_version != HWVER_130 && dsi->hw_version != HWVER_131) {
388 DRM_ERROR("bad dsi hardware version\n");
392 dw_mipi_dsi_stm_plat_data.base = dsi->base;
393 dw_mipi_dsi_stm_plat_data.priv_data = dsi;
395 platform_set_drvdata(pdev, dsi);
397 dsi->dsi = dw_mipi_dsi_probe(pdev, &dw_mipi_dsi_stm_plat_data);
398 if (IS_ERR(dsi->dsi)) {
399 ret = PTR_ERR(dsi->dsi);
400 DRM_ERROR("Failed to initialize mipi dsi host: %d\n", ret);
407 clk_disable_unprepare(dsi->pllref_clk);
409 regulator_disable(dsi->vdd_supply);
414 static int dw_mipi_dsi_stm_remove(struct platform_device *pdev)
416 struct dw_mipi_dsi_stm *dsi = platform_get_drvdata(pdev);
418 dw_mipi_dsi_remove(dsi->dsi);
419 clk_disable_unprepare(dsi->pllref_clk);
420 regulator_disable(dsi->vdd_supply);
425 static int __maybe_unused dw_mipi_dsi_stm_suspend(struct device *dev)
427 struct dw_mipi_dsi_stm *dsi = dw_mipi_dsi_stm_plat_data.priv_data;
429 DRM_DEBUG_DRIVER("\n");
431 clk_disable_unprepare(dsi->pllref_clk);
432 regulator_disable(dsi->vdd_supply);
437 static int __maybe_unused dw_mipi_dsi_stm_resume(struct device *dev)
439 struct dw_mipi_dsi_stm *dsi = dw_mipi_dsi_stm_plat_data.priv_data;
442 DRM_DEBUG_DRIVER("\n");
444 ret = regulator_enable(dsi->vdd_supply);
446 DRM_ERROR("Failed to enable regulator: %d\n", ret);
450 ret = clk_prepare_enable(dsi->pllref_clk);
452 regulator_disable(dsi->vdd_supply);
453 DRM_ERROR("Failed to enable pllref_clk: %d\n", ret);
460 static const struct dev_pm_ops dw_mipi_dsi_stm_pm_ops = {
461 SET_SYSTEM_SLEEP_PM_OPS(dw_mipi_dsi_stm_suspend,
462 dw_mipi_dsi_stm_resume)
465 static struct platform_driver dw_mipi_dsi_stm_driver = {
466 .probe = dw_mipi_dsi_stm_probe,
467 .remove = dw_mipi_dsi_stm_remove,
469 .of_match_table = dw_mipi_dsi_stm_dt_ids,
470 .name = "stm32-display-dsi",
471 .pm = &dw_mipi_dsi_stm_pm_ops,
475 module_platform_driver(dw_mipi_dsi_stm_driver);
477 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
478 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
479 MODULE_DESCRIPTION("STMicroelectronics DW MIPI DSI host controller driver");
480 MODULE_LICENSE("GPL v2");