Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

i2c: imx-lpi2c: add runtime pm support

Add runtime pm support to dynamically manage the clock to avoid enable/disable
clock in frequently that can improve the i2c bus transfer performance.

And use pm_runtime_force_suspend/resume() instead of lpi2c_imx_suspend/resume().

Signed-off-by: Fugang Duan <fugang.duan@nxp.com>
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>

authored by

Fugang Duan and committed by
Wolfram Sang
13d6eb20 fe34fbf9

+51 -17
+51 -17
drivers/i2c/busses/i2c-imx-lpi2c.c
··· 30 30 #include <linux/of_device.h> 31 31 #include <linux/pinctrl/consumer.h> 32 32 #include <linux/platform_device.h> 33 + #include <linux/pm_runtime.h> 33 34 #include <linux/sched.h> 34 35 #include <linux/slab.h> 35 36 ··· 90 89 #define FAST_MAX_BITRATE 1000000 91 90 #define FAST_PLUS_MAX_BITRATE 3400000 92 91 #define HIGHSPEED_MAX_BITRATE 5000000 92 + 93 + #define I2C_PM_TIMEOUT 10 /* ms */ 93 94 94 95 enum lpi2c_imx_mode { 95 96 STANDARD, /* 100+Kbps */ ··· 277 274 unsigned int temp; 278 275 int ret; 279 276 280 - ret = clk_enable(lpi2c_imx->clk); 281 - if (ret) 277 + ret = pm_runtime_get_sync(lpi2c_imx->adapter.dev.parent); 278 + if (ret < 0) 282 279 return ret; 283 280 284 281 temp = MCR_RST; ··· 287 284 288 285 ret = lpi2c_imx_config(lpi2c_imx); 289 286 if (ret) 290 - goto clk_disable; 287 + goto rpm_put; 291 288 292 289 temp = readl(lpi2c_imx->base + LPI2C_MCR); 293 290 temp |= MCR_MEN; ··· 295 292 296 293 return 0; 297 294 298 - clk_disable: 299 - clk_disable(lpi2c_imx->clk); 295 + rpm_put: 296 + pm_runtime_mark_last_busy(lpi2c_imx->adapter.dev.parent); 297 + pm_runtime_put_autosuspend(lpi2c_imx->adapter.dev.parent); 300 298 301 299 return ret; 302 300 } ··· 310 306 temp &= ~MCR_MEN; 311 307 writel(temp, lpi2c_imx->base + LPI2C_MCR); 312 308 313 - clk_disable(lpi2c_imx->clk); 309 + pm_runtime_mark_last_busy(lpi2c_imx->adapter.dev.parent); 310 + pm_runtime_put_autosuspend(lpi2c_imx->adapter.dev.parent); 314 311 315 312 return 0; 316 313 } ··· 611 606 return ret; 612 607 } 613 608 609 + pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT); 610 + pm_runtime_use_autosuspend(&pdev->dev); 611 + pm_runtime_get_noresume(&pdev->dev); 612 + pm_runtime_set_active(&pdev->dev); 613 + pm_runtime_enable(&pdev->dev); 614 + 614 615 temp = readl(lpi2c_imx->base + LPI2C_PARAM); 615 616 lpi2c_imx->txfifosize = 1 << (temp & 0x0f); 616 617 lpi2c_imx->rxfifosize = 1 << ((temp >> 8) & 0x0f); 617 618 618 - clk_disable(lpi2c_imx->clk); 619 - 620 619 ret = i2c_add_adapter(&lpi2c_imx->adapter); 621 620 if (ret) 622 - goto clk_unprepare; 621 + goto rpm_disable; 622 + 623 + pm_runtime_mark_last_busy(&pdev->dev); 624 + pm_runtime_put_autosuspend(&pdev->dev); 623 625 624 626 dev_info(&lpi2c_imx->adapter.dev, "LPI2C adapter registered\n"); 625 627 626 628 return 0; 627 629 628 - clk_unprepare: 629 - clk_unprepare(lpi2c_imx->clk); 630 + rpm_disable: 631 + pm_runtime_put(&pdev->dev); 632 + pm_runtime_disable(&pdev->dev); 633 + pm_runtime_dont_use_autosuspend(&pdev->dev); 630 634 631 635 return ret; 632 636 } ··· 646 632 647 633 i2c_del_adapter(&lpi2c_imx->adapter); 648 634 649 - clk_unprepare(lpi2c_imx->clk); 635 + pm_runtime_disable(&pdev->dev); 636 + pm_runtime_dont_use_autosuspend(&pdev->dev); 650 637 651 638 return 0; 652 639 } 653 640 654 641 #ifdef CONFIG_PM_SLEEP 655 - static int lpi2c_imx_suspend(struct device *dev) 642 + static int lpi2c_runtime_suspend(struct device *dev) 656 643 { 644 + struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev); 645 + 646 + clk_disable_unprepare(lpi2c_imx->clk); 657 647 pinctrl_pm_select_sleep_state(dev); 658 648 659 649 return 0; 660 650 } 661 651 662 - static int lpi2c_imx_resume(struct device *dev) 652 + static int lpi2c_runtime_resume(struct device *dev) 663 653 { 654 + struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev); 655 + int ret; 656 + 664 657 pinctrl_pm_select_default_state(dev); 658 + ret = clk_prepare_enable(lpi2c_imx->clk); 659 + if (ret) { 660 + dev_err(dev, "failed to enable I2C clock, ret=%d\n", ret); 661 + return ret; 662 + } 665 663 666 664 return 0; 667 665 } 668 - #endif 669 666 670 - static SIMPLE_DEV_PM_OPS(imx_lpi2c_pm, lpi2c_imx_suspend, lpi2c_imx_resume); 667 + static const struct dev_pm_ops lpi2c_pm_ops = { 668 + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 669 + pm_runtime_force_resume) 670 + SET_RUNTIME_PM_OPS(lpi2c_runtime_suspend, 671 + lpi2c_runtime_resume, NULL) 672 + }; 673 + #define IMX_LPI2C_PM (&lpi2c_pm_ops) 674 + #else 675 + #define IMX_LPI2C_PM NULL 676 + #endif 671 677 672 678 static struct platform_driver lpi2c_imx_driver = { 673 679 .probe = lpi2c_imx_probe, ··· 695 661 .driver = { 696 662 .name = DRIVER_NAME, 697 663 .of_match_table = lpi2c_imx_of_match, 698 - .pm = &imx_lpi2c_pm, 664 + .pm = IMX_LPI2C_PM, 699 665 }, 700 666 }; 701 667