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

i2c: xiic: Implement power management

Enable power management. This patch enables the clocks before transfer
and disables after the transfer. It also adds the clock description.

Signed-off-by: Shubhrajyoti Datta <shubhraj@xilinx.com>
Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>

authored by

Shubhrajyoti Datta and committed by
Wolfram Sang
36ecbcab 33f5ccc3

+84 -7
+3
Documentation/devicetree/bindings/i2c/i2c-xiic.txt
··· 6 6 - interrupts : IIC controller unterrupt 7 7 - #address-cells = <1> 8 8 - #size-cells = <0> 9 + - clocks: Input clock specifier. Refer to common clock bindings. 9 10 10 11 Optional properties: 11 12 - Child nodes conforming to i2c bus binding 13 + - clock-names: Input clock name, should be 'pclk'. 12 14 13 15 Example: 14 16 15 17 axi_iic_0: i2c@40800000 { 16 18 compatible = "xlnx,xps-iic-2.00.a"; 19 + clocks = <&clkc 15>; 17 20 interrupts = < 1 2 >; 18 21 reg = < 0x40800000 0x10000 >; 19 22
+81 -7
drivers/i2c/busses/i2c-xiic.c
··· 37 37 #include <linux/io.h> 38 38 #include <linux/slab.h> 39 39 #include <linux/of.h> 40 + #include <linux/clk.h> 41 + #include <linux/pm_runtime.h> 40 42 41 43 #define DRIVER_NAME "xiic-i2c" 42 44 ··· 68 66 * @endianness: big/little-endian byte order 69 67 */ 70 68 struct xiic_i2c { 69 + struct device *dev; 71 70 void __iomem *base; 72 71 wait_queue_head_t wait; 73 72 struct i2c_adapter adap; ··· 80 77 struct i2c_msg *rx_msg; 81 78 int rx_pos; 82 79 enum xiic_endian endianness; 80 + struct clk *clk; 83 81 }; 84 82 85 83 ··· 168 164 169 165 #define XIIC_RESET_MASK 0xAUL 170 166 167 + #define XIIC_PM_TIMEOUT 1000 /* ms */ 171 168 /* 172 169 * The following constant is used for the device global interrupt enable 173 170 * register, to enable all interrupts for the device, this is the only bit ··· 681 676 dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__, 682 677 xiic_getreg8(i2c, XIIC_SR_REG_OFFSET)); 683 678 679 + err = pm_runtime_get_sync(i2c->dev); 680 + if (err < 0) 681 + return err; 682 + 684 683 err = xiic_busy(i2c); 685 684 if (err) 686 - return err; 685 + goto out; 687 686 688 687 i2c->tx_msg = msgs; 689 688 i2c->nmsgs = num; ··· 695 686 xiic_start_xfer(i2c); 696 687 697 688 if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) || 698 - (i2c->state == STATE_DONE), HZ)) 699 - return (i2c->state == STATE_DONE) ? num : -EIO; 700 - else { 689 + (i2c->state == STATE_DONE), HZ)) { 690 + err = (i2c->state == STATE_DONE) ? num : -EIO; 691 + goto out; 692 + } else { 701 693 i2c->tx_msg = NULL; 702 694 i2c->rx_msg = NULL; 703 695 i2c->nmsgs = 0; 704 - return -ETIMEDOUT; 696 + err = -ETIMEDOUT; 697 + goto out; 705 698 } 699 + out: 700 + pm_runtime_mark_last_busy(i2c->dev); 701 + pm_runtime_put_autosuspend(i2c->dev); 702 + return err; 706 703 } 707 704 708 705 static u32 xiic_func(struct i2c_adapter *adap) ··· 763 748 mutex_init(&i2c->lock); 764 749 init_waitqueue_head(&i2c->wait); 765 750 751 + i2c->clk = devm_clk_get(&pdev->dev, NULL); 752 + if (IS_ERR(i2c->clk)) { 753 + dev_err(&pdev->dev, "input clock not found.\n"); 754 + return PTR_ERR(i2c->clk); 755 + } 756 + ret = clk_prepare_enable(i2c->clk); 757 + if (ret) { 758 + dev_err(&pdev->dev, "Unable to enable clock.\n"); 759 + return ret; 760 + } 761 + i2c->dev = &pdev->dev; 762 + pm_runtime_enable(i2c->dev); 763 + pm_runtime_set_autosuspend_delay(i2c->dev, XIIC_PM_TIMEOUT); 764 + pm_runtime_use_autosuspend(i2c->dev); 765 + pm_runtime_set_active(i2c->dev); 766 766 ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr, 767 767 xiic_process, IRQF_ONESHOT, 768 768 pdev->name, i2c); 769 769 770 770 if (ret < 0) { 771 771 dev_err(&pdev->dev, "Cannot claim IRQ\n"); 772 - return ret; 772 + goto err_clk_dis; 773 773 } 774 774 775 775 /* ··· 806 776 if (ret) { 807 777 dev_err(&pdev->dev, "Failed to add adapter\n"); 808 778 xiic_deinit(i2c); 809 - return ret; 779 + goto err_clk_dis; 810 780 } 811 781 812 782 if (pdata) { ··· 816 786 } 817 787 818 788 return 0; 789 + 790 + err_clk_dis: 791 + pm_runtime_set_suspended(&pdev->dev); 792 + pm_runtime_disable(&pdev->dev); 793 + clk_disable_unprepare(i2c->clk); 794 + return ret; 819 795 } 820 796 821 797 static int xiic_i2c_remove(struct platform_device *pdev) 822 798 { 823 799 struct xiic_i2c *i2c = platform_get_drvdata(pdev); 800 + int ret; 824 801 825 802 /* remove adapter & data */ 826 803 i2c_del_adapter(&i2c->adap); 827 804 805 + ret = clk_prepare_enable(i2c->clk); 806 + if (ret) { 807 + dev_err(&pdev->dev, "Unable to enable clock.\n"); 808 + return ret; 809 + } 828 810 xiic_deinit(i2c); 811 + clk_disable_unprepare(i2c->clk); 812 + pm_runtime_disable(&pdev->dev); 829 813 830 814 return 0; 831 815 } ··· 852 808 MODULE_DEVICE_TABLE(of, xiic_of_match); 853 809 #endif 854 810 811 + static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev) 812 + { 813 + struct platform_device *pdev = to_platform_device(dev); 814 + struct xiic_i2c *i2c = platform_get_drvdata(pdev); 815 + 816 + clk_disable(i2c->clk); 817 + 818 + return 0; 819 + } 820 + 821 + static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev) 822 + { 823 + struct platform_device *pdev = to_platform_device(dev); 824 + struct xiic_i2c *i2c = platform_get_drvdata(pdev); 825 + int ret; 826 + 827 + ret = clk_enable(i2c->clk); 828 + if (ret) { 829 + dev_err(dev, "Cannot enable clock.\n"); 830 + return ret; 831 + } 832 + 833 + return 0; 834 + } 835 + 836 + static const struct dev_pm_ops xiic_dev_pm_ops = { 837 + SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend, 838 + cdns_i2c_runtime_resume, NULL) 839 + }; 855 840 static struct platform_driver xiic_i2c_driver = { 856 841 .probe = xiic_i2c_probe, 857 842 .remove = xiic_i2c_remove, 858 843 .driver = { 859 844 .name = DRIVER_NAME, 860 845 .of_match_table = of_match_ptr(xiic_of_match), 846 + .pm = &xiic_dev_pm_ops, 861 847 }, 862 848 }; 863 849