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
@@ -66,6 +68,7 @@ enum xiic_endian {
66
68
* @endianness: big/little-endian byte order
67
69
*/
68
70
struct xiic_i2c {
71
+ struct device * dev ;
69
72
void __iomem * base ;
70
73
wait_queue_head_t wait ;
71
74
struct i2c_adapter adap ;
@@ -77,6 +80,7 @@ struct xiic_i2c {
77
80
struct i2c_msg * rx_msg ;
78
81
int rx_pos ;
79
82
enum xiic_endian endianness ;
83
+ struct clk * clk ;
80
84
};
81
85
82
86
@@ -164,6 +168,7 @@ struct xiic_i2c {
164
168
165
169
#define XIIC_RESET_MASK 0xAUL
166
170
171
+ #define XIIC_PM_TIMEOUT 1000 /* ms */
167
172
/*
168
173
* The following constant is used for the device global interrupt enable
169
174
* 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)
676
681
dev_dbg (adap -> dev .parent , "%s entry SR: 0x%x\n" , __func__ ,
677
682
xiic_getreg8 (i2c , XIIC_SR_REG_OFFSET ));
678
683
684
+ err = pm_runtime_get_sync (i2c -> dev );
685
+ if (err < 0 )
686
+ return err ;
687
+
679
688
err = xiic_busy (i2c );
680
689
if (err )
681
- return err ;
690
+ goto out ;
682
691
683
692
i2c -> tx_msg = msgs ;
684
693
i2c -> nmsgs = num ;
685
694
686
695
xiic_start_xfer (i2c );
687
696
688
697
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 {
692
702
i2c -> tx_msg = NULL ;
693
703
i2c -> rx_msg = NULL ;
694
704
i2c -> nmsgs = 0 ;
695
- return - ETIMEDOUT ;
705
+ err = - ETIMEDOUT ;
706
+ goto out ;
696
707
}
708
+ out :
709
+ pm_runtime_mark_last_busy (i2c -> dev );
710
+ pm_runtime_put_autosuspend (i2c -> dev );
711
+ return err ;
697
712
}
698
713
699
714
static u32 xiic_func (struct i2c_adapter * adap )
@@ -748,13 +763,28 @@ static int xiic_i2c_probe(struct platform_device *pdev)
748
763
mutex_init (& i2c -> lock );
749
764
init_waitqueue_head (& i2c -> wait );
750
765
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 );
751
781
ret = devm_request_threaded_irq (& pdev -> dev , irq , xiic_isr ,
752
782
xiic_process , IRQF_ONESHOT ,
753
783
pdev -> name , i2c );
754
784
755
785
if (ret < 0 ) {
756
786
dev_err (& pdev -> dev , "Cannot claim IRQ\n" );
757
- return ret ;
787
+ goto err_clk_dis ;
758
788
}
759
789
760
790
/*
@@ -776,7 +806,7 @@ static int xiic_i2c_probe(struct platform_device *pdev)
776
806
if (ret ) {
777
807
dev_err (& pdev -> dev , "Failed to add adapter\n" );
778
808
xiic_deinit (i2c );
779
- return ret ;
809
+ goto err_clk_dis ;
780
810
}
781
811
782
812
if (pdata ) {
@@ -786,16 +816,30 @@ static int xiic_i2c_probe(struct platform_device *pdev)
786
816
}
787
817
788
818
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 ;
789
825
}
790
826
791
827
static int xiic_i2c_remove (struct platform_device * pdev )
792
828
{
793
829
struct xiic_i2c * i2c = platform_get_drvdata (pdev );
830
+ int ret ;
794
831
795
832
/* remove adapter & data */
796
833
i2c_del_adapter (& i2c -> adap );
797
834
835
+ ret = clk_prepare_enable (i2c -> clk );
836
+ if (ret ) {
837
+ dev_err (& pdev -> dev , "Unable to enable clock.\n" );
838
+ return ret ;
839
+ }
798
840
xiic_deinit (i2c );
841
+ clk_disable_unprepare (i2c -> clk );
842
+ pm_runtime_disable (& pdev -> dev );
799
843
800
844
return 0 ;
801
845
}
@@ -808,12 +852,42 @@ static const struct of_device_id xiic_of_match[] = {
808
852
MODULE_DEVICE_TABLE (of , xiic_of_match );
809
853
#endif
810
854
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
+ };
811
884
static struct platform_driver xiic_i2c_driver = {
812
885
.probe = xiic_i2c_probe ,
813
886
.remove = xiic_i2c_remove ,
814
887
.driver = {
815
888
.name = DRIVER_NAME ,
816
889
.of_match_table = of_match_ptr (xiic_of_match ),
890
+ .pm = & xiic_dev_pm_ops ,
817
891
},
818
892
};
819
893
0 commit comments