| .. | .. |
|---|
| 8 | 8 | |
|---|
| 9 | 9 | #include <linux/clk.h> |
|---|
| 10 | 10 | #include <linux/iopoll.h> |
|---|
| 11 | +#include <linux/mod_devicetable.h> |
|---|
| 11 | 12 | #include <linux/module.h> |
|---|
| 12 | | -#include <drm/drmP.h> |
|---|
| 13 | | -#include <drm/drm_mipi_dsi.h> |
|---|
| 14 | | -#include <drm/bridge/dw_mipi_dsi.h> |
|---|
| 13 | +#include <linux/platform_device.h> |
|---|
| 14 | +#include <linux/regulator/consumer.h> |
|---|
| 15 | + |
|---|
| 15 | 16 | #include <video/mipi_display.h> |
|---|
| 17 | + |
|---|
| 18 | +#include <drm/bridge/dw_mipi_dsi.h> |
|---|
| 19 | +#include <drm/drm_mipi_dsi.h> |
|---|
| 20 | +#include <drm/drm_print.h> |
|---|
| 16 | 21 | |
|---|
| 17 | 22 | #define HWVER_130 0x31333000 /* IP version 1.30 */ |
|---|
| 18 | 23 | #define HWVER_131 0x31333100 /* IP version 1.31 */ |
|---|
| .. | .. |
|---|
| 76 | 81 | u32 hw_version; |
|---|
| 77 | 82 | int lane_min_kbps; |
|---|
| 78 | 83 | int lane_max_kbps; |
|---|
| 84 | + struct regulator *vdd_supply; |
|---|
| 79 | 85 | }; |
|---|
| 80 | 86 | |
|---|
| 81 | 87 | static inline void dsi_write(struct dw_mipi_dsi_stm *dsi, u32 reg, u32 val) |
|---|
| .. | .. |
|---|
| 208 | 214 | if (ret) |
|---|
| 209 | 215 | DRM_DEBUG_DRIVER("!TIMEOUT! waiting PLL, let's continue\n"); |
|---|
| 210 | 216 | |
|---|
| 211 | | - /* Enable the DSI wrapper */ |
|---|
| 212 | | - dsi_set(dsi, DSI_WCR, WCR_DSIEN); |
|---|
| 213 | | - |
|---|
| 214 | 217 | return 0; |
|---|
| 215 | 218 | } |
|---|
| 216 | 219 | |
|---|
| 220 | +static void dw_mipi_dsi_phy_power_on(void *priv_data) |
|---|
| 221 | +{ |
|---|
| 222 | + struct dw_mipi_dsi_stm *dsi = priv_data; |
|---|
| 223 | + |
|---|
| 224 | + DRM_DEBUG_DRIVER("\n"); |
|---|
| 225 | + |
|---|
| 226 | + /* Enable the DSI wrapper */ |
|---|
| 227 | + dsi_set(dsi, DSI_WCR, WCR_DSIEN); |
|---|
| 228 | +} |
|---|
| 229 | + |
|---|
| 230 | +static void dw_mipi_dsi_phy_power_off(void *priv_data) |
|---|
| 231 | +{ |
|---|
| 232 | + struct dw_mipi_dsi_stm *dsi = priv_data; |
|---|
| 233 | + |
|---|
| 234 | + DRM_DEBUG_DRIVER("\n"); |
|---|
| 235 | + |
|---|
| 236 | + /* Disable the DSI wrapper */ |
|---|
| 237 | + dsi_clear(dsi, DSI_WCR, WCR_DSIEN); |
|---|
| 238 | +} |
|---|
| 239 | + |
|---|
| 217 | 240 | static int |
|---|
| 218 | | -dw_mipi_dsi_get_lane_mbps(void *priv_data, struct drm_display_mode *mode, |
|---|
| 241 | +dw_mipi_dsi_get_lane_mbps(void *priv_data, const struct drm_display_mode *mode, |
|---|
| 219 | 242 | unsigned long mode_flags, u32 lanes, u32 format, |
|---|
| 220 | 243 | unsigned int *lane_mbps) |
|---|
| 221 | 244 | { |
|---|
| .. | .. |
|---|
| 225 | 248 | u32 val; |
|---|
| 226 | 249 | |
|---|
| 227 | 250 | /* Update lane capabilities according to hw version */ |
|---|
| 228 | | - dsi->hw_version = dsi_read(dsi, DSI_VERSION) & VERSION; |
|---|
| 229 | 251 | dsi->lane_min_kbps = LANE_MIN_KBPS; |
|---|
| 230 | 252 | dsi->lane_max_kbps = LANE_MAX_KBPS; |
|---|
| 231 | 253 | if (dsi->hw_version == HWVER_131) { |
|---|
| .. | .. |
|---|
| 238 | 260 | /* Compute requested pll out */ |
|---|
| 239 | 261 | bpp = mipi_dsi_pixel_format_to_bpp(format); |
|---|
| 240 | 262 | pll_out_khz = mode->clock * bpp / lanes; |
|---|
| 263 | + |
|---|
| 241 | 264 | /* Add 20% to pll out to be higher than pixel bw (burst mode only) */ |
|---|
| 242 | | - pll_out_khz = (pll_out_khz * 12) / 10; |
|---|
| 265 | + if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST) |
|---|
| 266 | + pll_out_khz = (pll_out_khz * 12) / 10; |
|---|
| 267 | + |
|---|
| 243 | 268 | if (pll_out_khz > dsi->lane_max_kbps) { |
|---|
| 244 | 269 | pll_out_khz = dsi->lane_max_kbps; |
|---|
| 245 | 270 | DRM_WARN("Warning max phy mbps is used\n"); |
|---|
| .. | .. |
|---|
| 284 | 309 | return 0; |
|---|
| 285 | 310 | } |
|---|
| 286 | 311 | |
|---|
| 312 | +static int |
|---|
| 313 | +dw_mipi_dsi_phy_get_timing(void *priv_data, unsigned int lane_mbps, |
|---|
| 314 | + struct dw_mipi_dsi_dphy_timing *timing) |
|---|
| 315 | +{ |
|---|
| 316 | + timing->clk_hs2lp = 0x40; |
|---|
| 317 | + timing->clk_lp2hs = 0x40; |
|---|
| 318 | + timing->data_hs2lp = 0x40; |
|---|
| 319 | + timing->data_lp2hs = 0x40; |
|---|
| 320 | + |
|---|
| 321 | + return 0; |
|---|
| 322 | +} |
|---|
| 323 | + |
|---|
| 287 | 324 | static const struct dw_mipi_dsi_phy_ops dw_mipi_dsi_stm_phy_ops = { |
|---|
| 288 | 325 | .init = dw_mipi_dsi_phy_init, |
|---|
| 326 | + .power_on = dw_mipi_dsi_phy_power_on, |
|---|
| 327 | + .power_off = dw_mipi_dsi_phy_power_off, |
|---|
| 289 | 328 | .get_lane_mbps = dw_mipi_dsi_get_lane_mbps, |
|---|
| 329 | + .get_timing = dw_mipi_dsi_phy_get_timing, |
|---|
| 290 | 330 | }; |
|---|
| 291 | 331 | |
|---|
| 292 | 332 | static struct dw_mipi_dsi_plat_data dw_mipi_dsi_stm_plat_data = { |
|---|
| .. | .. |
|---|
| 304 | 344 | { |
|---|
| 305 | 345 | struct device *dev = &pdev->dev; |
|---|
| 306 | 346 | struct dw_mipi_dsi_stm *dsi; |
|---|
| 347 | + struct clk *pclk; |
|---|
| 307 | 348 | struct resource *res; |
|---|
| 308 | 349 | int ret; |
|---|
| 309 | 350 | |
|---|
| .. | .. |
|---|
| 314 | 355 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 315 | 356 | dsi->base = devm_ioremap_resource(dev, res); |
|---|
| 316 | 357 | if (IS_ERR(dsi->base)) { |
|---|
| 317 | | - DRM_ERROR("Unable to get dsi registers\n"); |
|---|
| 318 | | - return PTR_ERR(dsi->base); |
|---|
| 358 | + ret = PTR_ERR(dsi->base); |
|---|
| 359 | + DRM_ERROR("Unable to get dsi registers %d\n", ret); |
|---|
| 360 | + return ret; |
|---|
| 361 | + } |
|---|
| 362 | + |
|---|
| 363 | + dsi->vdd_supply = devm_regulator_get(dev, "phy-dsi"); |
|---|
| 364 | + if (IS_ERR(dsi->vdd_supply)) { |
|---|
| 365 | + ret = PTR_ERR(dsi->vdd_supply); |
|---|
| 366 | + if (ret != -EPROBE_DEFER) |
|---|
| 367 | + DRM_ERROR("Failed to request regulator: %d\n", ret); |
|---|
| 368 | + return ret; |
|---|
| 369 | + } |
|---|
| 370 | + |
|---|
| 371 | + ret = regulator_enable(dsi->vdd_supply); |
|---|
| 372 | + if (ret) { |
|---|
| 373 | + DRM_ERROR("Failed to enable regulator: %d\n", ret); |
|---|
| 374 | + return ret; |
|---|
| 319 | 375 | } |
|---|
| 320 | 376 | |
|---|
| 321 | 377 | dsi->pllref_clk = devm_clk_get(dev, "ref"); |
|---|
| 322 | 378 | if (IS_ERR(dsi->pllref_clk)) { |
|---|
| 323 | 379 | ret = PTR_ERR(dsi->pllref_clk); |
|---|
| 324 | | - dev_err(dev, "Unable to get pll reference clock: %d\n", ret); |
|---|
| 325 | | - return ret; |
|---|
| 380 | + if (ret != -EPROBE_DEFER) |
|---|
| 381 | + DRM_ERROR("Unable to get pll reference clock: %d\n", |
|---|
| 382 | + ret); |
|---|
| 383 | + goto err_clk_get; |
|---|
| 326 | 384 | } |
|---|
| 327 | 385 | |
|---|
| 328 | 386 | ret = clk_prepare_enable(dsi->pllref_clk); |
|---|
| 329 | 387 | if (ret) { |
|---|
| 330 | | - dev_err(dev, "%s: Failed to enable pllref_clk\n", __func__); |
|---|
| 331 | | - return ret; |
|---|
| 388 | + DRM_ERROR("Failed to enable pllref_clk: %d\n", ret); |
|---|
| 389 | + goto err_clk_get; |
|---|
| 390 | + } |
|---|
| 391 | + |
|---|
| 392 | + pclk = devm_clk_get(dev, "pclk"); |
|---|
| 393 | + if (IS_ERR(pclk)) { |
|---|
| 394 | + ret = PTR_ERR(pclk); |
|---|
| 395 | + DRM_ERROR("Unable to get peripheral clock: %d\n", ret); |
|---|
| 396 | + goto err_dsi_probe; |
|---|
| 397 | + } |
|---|
| 398 | + |
|---|
| 399 | + ret = clk_prepare_enable(pclk); |
|---|
| 400 | + if (ret) { |
|---|
| 401 | + DRM_ERROR("%s: Failed to enable peripheral clk\n", __func__); |
|---|
| 402 | + goto err_dsi_probe; |
|---|
| 403 | + } |
|---|
| 404 | + |
|---|
| 405 | + dsi->hw_version = dsi_read(dsi, DSI_VERSION) & VERSION; |
|---|
| 406 | + clk_disable_unprepare(pclk); |
|---|
| 407 | + |
|---|
| 408 | + if (dsi->hw_version != HWVER_130 && dsi->hw_version != HWVER_131) { |
|---|
| 409 | + ret = -ENODEV; |
|---|
| 410 | + DRM_ERROR("bad dsi hardware version\n"); |
|---|
| 411 | + goto err_dsi_probe; |
|---|
| 332 | 412 | } |
|---|
| 333 | 413 | |
|---|
| 334 | 414 | dw_mipi_dsi_stm_plat_data.base = dsi->base; |
|---|
| .. | .. |
|---|
| 338 | 418 | |
|---|
| 339 | 419 | dsi->dsi = dw_mipi_dsi_probe(pdev, &dw_mipi_dsi_stm_plat_data); |
|---|
| 340 | 420 | if (IS_ERR(dsi->dsi)) { |
|---|
| 341 | | - DRM_ERROR("Failed to initialize mipi dsi host\n"); |
|---|
| 342 | | - clk_disable_unprepare(dsi->pllref_clk); |
|---|
| 343 | | - return PTR_ERR(dsi->dsi); |
|---|
| 421 | + ret = PTR_ERR(dsi->dsi); |
|---|
| 422 | + DRM_ERROR("Failed to initialize mipi dsi host: %d\n", ret); |
|---|
| 423 | + goto err_dsi_probe; |
|---|
| 344 | 424 | } |
|---|
| 345 | 425 | |
|---|
| 346 | 426 | return 0; |
|---|
| 427 | + |
|---|
| 428 | +err_dsi_probe: |
|---|
| 429 | + clk_disable_unprepare(dsi->pllref_clk); |
|---|
| 430 | +err_clk_get: |
|---|
| 431 | + regulator_disable(dsi->vdd_supply); |
|---|
| 432 | + |
|---|
| 433 | + return ret; |
|---|
| 347 | 434 | } |
|---|
| 348 | 435 | |
|---|
| 349 | 436 | static int dw_mipi_dsi_stm_remove(struct platform_device *pdev) |
|---|
| 350 | 437 | { |
|---|
| 351 | 438 | struct dw_mipi_dsi_stm *dsi = platform_get_drvdata(pdev); |
|---|
| 352 | 439 | |
|---|
| 353 | | - clk_disable_unprepare(dsi->pllref_clk); |
|---|
| 354 | 440 | dw_mipi_dsi_remove(dsi->dsi); |
|---|
| 441 | + clk_disable_unprepare(dsi->pllref_clk); |
|---|
| 442 | + regulator_disable(dsi->vdd_supply); |
|---|
| 355 | 443 | |
|---|
| 356 | 444 | return 0; |
|---|
| 357 | 445 | } |
|---|
| 446 | + |
|---|
| 447 | +static int __maybe_unused dw_mipi_dsi_stm_suspend(struct device *dev) |
|---|
| 448 | +{ |
|---|
| 449 | + struct dw_mipi_dsi_stm *dsi = dw_mipi_dsi_stm_plat_data.priv_data; |
|---|
| 450 | + |
|---|
| 451 | + DRM_DEBUG_DRIVER("\n"); |
|---|
| 452 | + |
|---|
| 453 | + clk_disable_unprepare(dsi->pllref_clk); |
|---|
| 454 | + regulator_disable(dsi->vdd_supply); |
|---|
| 455 | + |
|---|
| 456 | + return 0; |
|---|
| 457 | +} |
|---|
| 458 | + |
|---|
| 459 | +static int __maybe_unused dw_mipi_dsi_stm_resume(struct device *dev) |
|---|
| 460 | +{ |
|---|
| 461 | + struct dw_mipi_dsi_stm *dsi = dw_mipi_dsi_stm_plat_data.priv_data; |
|---|
| 462 | + int ret; |
|---|
| 463 | + |
|---|
| 464 | + DRM_DEBUG_DRIVER("\n"); |
|---|
| 465 | + |
|---|
| 466 | + ret = regulator_enable(dsi->vdd_supply); |
|---|
| 467 | + if (ret) { |
|---|
| 468 | + DRM_ERROR("Failed to enable regulator: %d\n", ret); |
|---|
| 469 | + return ret; |
|---|
| 470 | + } |
|---|
| 471 | + |
|---|
| 472 | + ret = clk_prepare_enable(dsi->pllref_clk); |
|---|
| 473 | + if (ret) { |
|---|
| 474 | + regulator_disable(dsi->vdd_supply); |
|---|
| 475 | + DRM_ERROR("Failed to enable pllref_clk: %d\n", ret); |
|---|
| 476 | + return ret; |
|---|
| 477 | + } |
|---|
| 478 | + |
|---|
| 479 | + return 0; |
|---|
| 480 | +} |
|---|
| 481 | + |
|---|
| 482 | +static const struct dev_pm_ops dw_mipi_dsi_stm_pm_ops = { |
|---|
| 483 | + SET_SYSTEM_SLEEP_PM_OPS(dw_mipi_dsi_stm_suspend, |
|---|
| 484 | + dw_mipi_dsi_stm_resume) |
|---|
| 485 | +}; |
|---|
| 358 | 486 | |
|---|
| 359 | 487 | static struct platform_driver dw_mipi_dsi_stm_driver = { |
|---|
| 360 | 488 | .probe = dw_mipi_dsi_stm_probe, |
|---|
| .. | .. |
|---|
| 362 | 490 | .driver = { |
|---|
| 363 | 491 | .of_match_table = dw_mipi_dsi_stm_dt_ids, |
|---|
| 364 | 492 | .name = "stm32-display-dsi", |
|---|
| 493 | + .pm = &dw_mipi_dsi_stm_pm_ops, |
|---|
| 365 | 494 | }, |
|---|
| 366 | 495 | }; |
|---|
| 367 | 496 | |
|---|