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 9 #include <linux/clk.h> 10 #include <linux/iopoll.h> 11 #include <linux/module.h> 12 #include <linux/regulator/consumer.h> 13 #include <drm/drmP.h> 14 #include <drm/drm_mipi_dsi.h> 15 #include <drm/bridge/dw_mipi_dsi.h> 16 #include <video/mipi_display.h> 17 18 #define HWVER_130 0x31333000 /* IP version 1.30 */ 19 #define HWVER_131 0x31333100 /* IP version 1.31 */ 20 21 /* DSI digital registers & bit definitions */ 22 #define DSI_VERSION 0x00 23 #define VERSION GENMASK(31, 8) 24 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 */ 30 31 #define DSI_WCR 0x0404 /* Wrapper Control Reg */ 32 #define WCR_DSIEN BIT(3) /* DSI ENable */ 33 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 */ 37 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 */ 41 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 */ 49 #define IDF_MIN 1 50 #define IDF_MAX 7 51 #define NDIV_MIN 10 52 #define NDIV_MAX 125 53 #define ODF_MIN 1 54 #define ODF_MAX 8 55 56 /* dsi color format coding according to the datasheet */ 57 enum dsi_color { 58 DSI_RGB565_CONF1, 59 DSI_RGB565_CONF2, 60 DSI_RGB565_CONF3, 61 DSI_RGB666_CONF1, 62 DSI_RGB666_CONF2, 63 DSI_RGB888, 64 }; 65 66 #define LANE_MIN_KBPS 31250 67 #define LANE_MAX_KBPS 500000 68 69 /* Sleep & timeout for regulator on/off, pll lock/unlock & fifo empty */ 70 #define SLEEP_US 1000 71 #define TIMEOUT_US 200000 72 73 struct dw_mipi_dsi_stm { 74 void __iomem *base; 75 struct clk *pllref_clk; 76 struct dw_mipi_dsi *dsi; 77 u32 hw_version; 78 int lane_min_kbps; 79 int lane_max_kbps; 80 struct regulator *vdd_supply; 81 }; 82 83 static inline void dsi_write(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 val) 84 { 85 writel(val, dsi->base + reg); 86 } 87 88 static inline u32 dsi_read(struct dw_mipi_dsi_stm *dsi, u32 reg) 89 { 90 return readl(dsi->base + reg); 91 } 92 93 static inline void dsi_set(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 mask) 94 { 95 dsi_write(dsi, reg, dsi_read(dsi, reg) | mask); 96 } 97 98 static inline void dsi_clear(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 mask) 99 { 100 dsi_write(dsi, reg, dsi_read(dsi, reg) & ~mask); 101 } 102 103 static inline void dsi_update_bits(struct dw_mipi_dsi_stm *dsi, u32 reg, 104 u32 mask, u32 val) 105 { 106 dsi_write(dsi, reg, (dsi_read(dsi, reg) & ~mask) | val); 107 } 108 109 static enum dsi_color dsi_color_from_mipi(enum mipi_dsi_pixel_format fmt) 110 { 111 switch (fmt) { 112 case MIPI_DSI_FMT_RGB888: 113 return DSI_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; 120 default: 121 DRM_DEBUG_DRIVER("MIPI color invalid, so we use rgb888\n"); 122 } 123 return DSI_RGB888; 124 } 125 126 static int dsi_pll_get_clkout_khz(int clkin_khz, int idf, int ndiv, int odf) 127 { 128 int divisor = idf * odf; 129 130 /* prevent from division by 0 */ 131 if (!divisor) 132 return 0; 133 134 return DIV_ROUND_CLOSEST(clkin_khz * ndiv, divisor); 135 } 136 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) 140 { 141 int i, o, n, n_min, n_max; 142 int fvco_min, fvco_max, delta, best_delta; /* all in khz */ 143 144 /* Early checks preventing division by 0 & odd results */ 145 if (clkin_khz <= 0 || clkout_khz <= 0) 146 return -EINVAL; 147 148 fvco_min = dsi->lane_min_kbps * 2 * ODF_MAX; 149 fvco_max = dsi->lane_max_kbps * 2 * ODF_MIN; 150 151 best_delta = 1000000; /* big started value (1000000khz) */ 152 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); 157 158 /* No need to continue idf loop if we reach ndiv max */ 159 if (n_min >= NDIV_MAX) 160 break; 161 162 /* Clamp ndiv to valid values */ 163 if (n_min < NDIV_MIN) 164 n_min = NDIV_MIN; 165 if (n_max > NDIV_MAX) 166 n_max = NDIV_MAX; 167 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) 172 continue; 173 /* Check if new delta is better & saves parameters */ 174 delta = dsi_pll_get_clkout_khz(clkin_khz, i, n, o) - 175 clkout_khz; 176 if (delta < 0) 177 delta = -delta; 178 if (delta < best_delta) { 179 *idf = i; 180 *ndiv = n; 181 *odf = o; 182 best_delta = delta; 183 } 184 /* fast return in case of "perfect result" */ 185 if (!delta) 186 return 0; 187 } 188 } 189 190 return 0; 191 } 192 193 static int dw_mipi_dsi_phy_init(void *priv_data) 194 { 195 struct dw_mipi_dsi_stm *dsi = priv_data; 196 u32 val; 197 int ret; 198 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); 203 if (ret) 204 DRM_DEBUG_DRIVER("!TIMEOUT! waiting REGU, let's continue\n"); 205 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); 210 if (ret) 211 DRM_DEBUG_DRIVER("!TIMEOUT! waiting PLL, let's continue\n"); 212 213 return 0; 214 } 215 216 static void dw_mipi_dsi_phy_power_on(void *priv_data) 217 { 218 struct dw_mipi_dsi_stm *dsi = priv_data; 219 220 DRM_DEBUG_DRIVER("\n"); 221 222 /* Enable the DSI wrapper */ 223 dsi_set(dsi, DSI_WCR, WCR_DSIEN); 224 } 225 226 static void dw_mipi_dsi_phy_power_off(void *priv_data) 227 { 228 struct dw_mipi_dsi_stm *dsi = priv_data; 229 230 DRM_DEBUG_DRIVER("\n"); 231 232 /* Disable the DSI wrapper */ 233 dsi_clear(dsi, DSI_WCR, WCR_DSIEN); 234 } 235 236 static int 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) 240 { 241 struct dw_mipi_dsi_stm *dsi = priv_data; 242 unsigned int idf, ndiv, odf, pll_in_khz, pll_out_khz; 243 int ret, bpp; 244 u32 val; 245 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; 252 } 253 254 pll_in_khz = (unsigned int)(clk_get_rate(dsi->pllref_clk) / 1000); 255 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"); 264 } 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"); 268 } 269 270 /* Compute best pll parameters */ 271 idf = 0; 272 ndiv = 0; 273 odf = 0; 274 ret = dsi_pll_get_params(dsi, pll_in_khz, pll_out_khz, 275 &idf, &ndiv, &odf); 276 if (ret) 277 DRM_WARN("Warning dsi_pll_get_params(): bad params\n"); 278 279 /* Get the adjusted pll out value */ 280 pll_out_khz = dsi_pll_get_clkout_khz(pll_in_khz, idf, ndiv, odf); 281 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)); 285 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); 289 290 /* Select video mode by resetting DSIM bit */ 291 dsi_clear(dsi, DSI_WCFGR, WCFGR_DSIM); 292 293 /* Select the color coding */ 294 dsi_update_bits(dsi, DSI_WCFGR, WCFGR_COLMUX, 295 dsi_color_from_mipi(format) << 1); 296 297 *lane_mbps = pll_out_khz / 1000; 298 299 DRM_DEBUG_DRIVER("pll_in %ukHz pll_out %ukHz lane_mbps %uMHz\n", 300 pll_in_khz, pll_out_khz, *lane_mbps); 301 302 return 0; 303 } 304 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, 310 }; 311 312 static struct dw_mipi_dsi_plat_data dw_mipi_dsi_stm_plat_data = { 313 .max_data_lanes = 2, 314 .phy_ops = &dw_mipi_dsi_stm_phy_ops, 315 }; 316 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, }, 319 { }, 320 }; 321 MODULE_DEVICE_TABLE(of, dw_mipi_dsi_stm_dt_ids); 322 323 static int dw_mipi_dsi_stm_probe(struct platform_device *pdev) 324 { 325 struct device *dev = &pdev->dev; 326 struct dw_mipi_dsi_stm *dsi; 327 struct clk *pclk; 328 struct resource *res; 329 int ret; 330 331 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL); 332 if (!dsi) 333 return -ENOMEM; 334 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); 340 return ret; 341 } 342 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); 348 return ret; 349 } 350 351 ret = regulator_enable(dsi->vdd_supply); 352 if (ret) { 353 DRM_ERROR("Failed to enable regulator: %d\n", ret); 354 return ret; 355 } 356 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); 361 goto err_clk_get; 362 } 363 364 ret = clk_prepare_enable(dsi->pllref_clk); 365 if (ret) { 366 DRM_ERROR("Failed to enable pllref_clk: %d\n", ret); 367 goto err_clk_get; 368 } 369 370 pclk = devm_clk_get(dev, "pclk"); 371 if (IS_ERR(pclk)) { 372 ret = PTR_ERR(pclk); 373 DRM_ERROR("Unable to get peripheral clock: %d\n", ret); 374 goto err_dsi_probe; 375 } 376 377 ret = clk_prepare_enable(pclk); 378 if (ret) { 379 DRM_ERROR("%s: Failed to enable peripheral clk\n", __func__); 380 goto err_dsi_probe; 381 } 382 383 dsi->hw_version = dsi_read(dsi, DSI_VERSION) & VERSION; 384 clk_disable_unprepare(pclk); 385 386 if (dsi->hw_version != HWVER_130 && dsi->hw_version != HWVER_131) { 387 ret = -ENODEV; 388 DRM_ERROR("bad dsi hardware version\n"); 389 goto err_dsi_probe; 390 } 391 392 dw_mipi_dsi_stm_plat_data.base = dsi->base; 393 dw_mipi_dsi_stm_plat_data.priv_data = dsi; 394 395 platform_set_drvdata(pdev, dsi); 396 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); 401 goto err_dsi_probe; 402 } 403 404 return 0; 405 406 err_dsi_probe: 407 clk_disable_unprepare(dsi->pllref_clk); 408 err_clk_get: 409 regulator_disable(dsi->vdd_supply); 410 411 return ret; 412 } 413 414 static int dw_mipi_dsi_stm_remove(struct platform_device *pdev) 415 { 416 struct dw_mipi_dsi_stm *dsi = platform_get_drvdata(pdev); 417 418 dw_mipi_dsi_remove(dsi->dsi); 419 clk_disable_unprepare(dsi->pllref_clk); 420 regulator_disable(dsi->vdd_supply); 421 422 return 0; 423 } 424 425 static int __maybe_unused dw_mipi_dsi_stm_suspend(struct device *dev) 426 { 427 struct dw_mipi_dsi_stm *dsi = dw_mipi_dsi_stm_plat_data.priv_data; 428 429 DRM_DEBUG_DRIVER("\n"); 430 431 clk_disable_unprepare(dsi->pllref_clk); 432 regulator_disable(dsi->vdd_supply); 433 434 return 0; 435 } 436 437 static int __maybe_unused dw_mipi_dsi_stm_resume(struct device *dev) 438 { 439 struct dw_mipi_dsi_stm *dsi = dw_mipi_dsi_stm_plat_data.priv_data; 440 int ret; 441 442 DRM_DEBUG_DRIVER("\n"); 443 444 ret = regulator_enable(dsi->vdd_supply); 445 if (ret) { 446 DRM_ERROR("Failed to enable regulator: %d\n", ret); 447 return ret; 448 } 449 450 ret = clk_prepare_enable(dsi->pllref_clk); 451 if (ret) { 452 regulator_disable(dsi->vdd_supply); 453 DRM_ERROR("Failed to enable pllref_clk: %d\n", ret); 454 return ret; 455 } 456 457 return 0; 458 } 459 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) 463 }; 464 465 static struct platform_driver dw_mipi_dsi_stm_driver = { 466 .probe = dw_mipi_dsi_stm_probe, 467 .remove = dw_mipi_dsi_stm_remove, 468 .driver = { 469 .of_match_table = dw_mipi_dsi_stm_dt_ids, 470 .name = "stm32-display-dsi", 471 .pm = &dw_mipi_dsi_stm_pm_ops, 472 }, 473 }; 474 475 module_platform_driver(dw_mipi_dsi_stm_driver); 476 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"); 481