Skip to content

Commit 36ecbca

Browse files
shubhrajyoti1Wolfram Sang
authored andcommitted
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 <[email protected]> Acked-by: Rob Herring <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent 33f5ccc commit 36ecbca

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

Documentation/devicetree/bindings/i2c/i2c-xiic.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ Required properties:
66
- interrupts : IIC controller unterrupt
77
- #address-cells = <1>
88
- #size-cells = <0>
9+
- clocks: Input clock specifier. Refer to common clock bindings.
910

1011
Optional properties:
1112
- Child nodes conforming to i2c bus binding
13+
- clock-names: Input clock name, should be 'pclk'.
1214

1315
Example:
1416

1517
axi_iic_0: i2c@40800000 {
1618
compatible = "xlnx,xps-iic-2.00.a";
19+
clocks = <&clkc 15>;
1720
interrupts = < 1 2 >;
1821
reg = < 0x40800000 0x10000 >;
1922

drivers/i2c/busses/i2c-xiic.c

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include <linux/io.h>
3838
#include <linux/slab.h>
3939
#include <linux/of.h>
40+
#include <linux/clk.h>
41+
#include <linux/pm_runtime.h>
4042

4143
#define DRIVER_NAME "xiic-i2c"
4244

@@ -66,6 +68,7 @@ enum xiic_endian {
6668
* @endianness: big/little-endian byte order
6769
*/
6870
struct xiic_i2c {
71+
struct device *dev;
6972
void __iomem *base;
7073
wait_queue_head_t wait;
7174
struct i2c_adapter adap;
@@ -77,6 +80,7 @@ struct xiic_i2c {
7780
struct i2c_msg *rx_msg;
7881
int rx_pos;
7982
enum xiic_endian endianness;
83+
struct clk *clk;
8084
};
8185

8286

@@ -164,6 +168,7 @@ struct xiic_i2c {
164168

165169
#define XIIC_RESET_MASK 0xAUL
166170

171+
#define XIIC_PM_TIMEOUT 1000 /* ms */
167172
/*
168173
* The following constant is used for the device global interrupt enable
169174
* register, to enable all interrupts for the device, this is the only bit
@@ -676,24 +681,34 @@ static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
676681
dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,
677682
xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));
678683

684+
err = pm_runtime_get_sync(i2c->dev);
685+
if (err < 0)
686+
return err;
687+
679688
err = xiic_busy(i2c);
680689
if (err)
681-
return err;
690+
goto out;
682691

683692
i2c->tx_msg = msgs;
684693
i2c->nmsgs = num;
685694

686695
xiic_start_xfer(i2c);
687696

688697
if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
689-
(i2c->state == STATE_DONE), HZ))
690-
return (i2c->state == STATE_DONE) ? num : -EIO;
691-
else {
698+
(i2c->state == STATE_DONE), HZ)) {
699+
err = (i2c->state == STATE_DONE) ? num : -EIO;
700+
goto out;
701+
} else {
692702
i2c->tx_msg = NULL;
693703
i2c->rx_msg = NULL;
694704
i2c->nmsgs = 0;
695-
return -ETIMEDOUT;
705+
err = -ETIMEDOUT;
706+
goto out;
696707
}
708+
out:
709+
pm_runtime_mark_last_busy(i2c->dev);
710+
pm_runtime_put_autosuspend(i2c->dev);
711+
return err;
697712
}
698713

699714
static u32 xiic_func(struct i2c_adapter *adap)
@@ -748,13 +763,28 @@ static int xiic_i2c_probe(struct platform_device *pdev)
748763
mutex_init(&i2c->lock);
749764
init_waitqueue_head(&i2c->wait);
750765

766+
i2c->clk = devm_clk_get(&pdev->dev, NULL);
767+
if (IS_ERR(i2c->clk)) {
768+
dev_err(&pdev->dev, "input clock not found.\n");
769+
return PTR_ERR(i2c->clk);
770+
}
771+
ret = clk_prepare_enable(i2c->clk);
772+
if (ret) {
773+
dev_err(&pdev->dev, "Unable to enable clock.\n");
774+
return ret;
775+
}
776+
i2c->dev = &pdev->dev;
777+
pm_runtime_enable(i2c->dev);
778+
pm_runtime_set_autosuspend_delay(i2c->dev, XIIC_PM_TIMEOUT);
779+
pm_runtime_use_autosuspend(i2c->dev);
780+
pm_runtime_set_active(i2c->dev);
751781
ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr,
752782
xiic_process, IRQF_ONESHOT,
753783
pdev->name, i2c);
754784

755785
if (ret < 0) {
756786
dev_err(&pdev->dev, "Cannot claim IRQ\n");
757-
return ret;
787+
goto err_clk_dis;
758788
}
759789

760790
/*
@@ -776,7 +806,7 @@ static int xiic_i2c_probe(struct platform_device *pdev)
776806
if (ret) {
777807
dev_err(&pdev->dev, "Failed to add adapter\n");
778808
xiic_deinit(i2c);
779-
return ret;
809+
goto err_clk_dis;
780810
}
781811

782812
if (pdata) {
@@ -786,16 +816,30 @@ static int xiic_i2c_probe(struct platform_device *pdev)
786816
}
787817

788818
return 0;
819+
820+
err_clk_dis:
821+
pm_runtime_set_suspended(&pdev->dev);
822+
pm_runtime_disable(&pdev->dev);
823+
clk_disable_unprepare(i2c->clk);
824+
return ret;
789825
}
790826

791827
static int xiic_i2c_remove(struct platform_device *pdev)
792828
{
793829
struct xiic_i2c *i2c = platform_get_drvdata(pdev);
830+
int ret;
794831

795832
/* remove adapter & data */
796833
i2c_del_adapter(&i2c->adap);
797834

835+
ret = clk_prepare_enable(i2c->clk);
836+
if (ret) {
837+
dev_err(&pdev->dev, "Unable to enable clock.\n");
838+
return ret;
839+
}
798840
xiic_deinit(i2c);
841+
clk_disable_unprepare(i2c->clk);
842+
pm_runtime_disable(&pdev->dev);
799843

800844
return 0;
801845
}
@@ -808,12 +852,42 @@ static const struct of_device_id xiic_of_match[] = {
808852
MODULE_DEVICE_TABLE(of, xiic_of_match);
809853
#endif
810854

855+
static int __maybe_unused cdns_i2c_runtime_suspend(struct device *dev)
856+
{
857+
struct platform_device *pdev = to_platform_device(dev);
858+
struct xiic_i2c *i2c = platform_get_drvdata(pdev);
859+
860+
clk_disable(i2c->clk);
861+
862+
return 0;
863+
}
864+
865+
static int __maybe_unused cdns_i2c_runtime_resume(struct device *dev)
866+
{
867+
struct platform_device *pdev = to_platform_device(dev);
868+
struct xiic_i2c *i2c = platform_get_drvdata(pdev);
869+
int ret;
870+
871+
ret = clk_enable(i2c->clk);
872+
if (ret) {
873+
dev_err(dev, "Cannot enable clock.\n");
874+
return ret;
875+
}
876+
877+
return 0;
878+
}
879+
880+
static const struct dev_pm_ops xiic_dev_pm_ops = {
881+
SET_RUNTIME_PM_OPS(cdns_i2c_runtime_suspend,
882+
cdns_i2c_runtime_resume, NULL)
883+
};
811884
static struct platform_driver xiic_i2c_driver = {
812885
.probe = xiic_i2c_probe,
813886
.remove = xiic_i2c_remove,
814887
.driver = {
815888
.name = DRIVER_NAME,
816889
.of_match_table = of_match_ptr(xiic_of_match),
890+
.pm = &xiic_dev_pm_ops,
817891
},
818892
};
819893

0 commit comments

Comments
 (0)