21
21
#include <linux/pm_runtime.h>
22
22
#include <linux/property.h>
23
23
#include <linux/regmap.h>
24
+ #include <linux/regulator/consumer.h>
24
25
#include <linux/slab.h>
25
26
26
27
/* Address pointer is 16 bit. */
@@ -87,6 +88,7 @@ struct at24_data {
87
88
u8 flags ;
88
89
89
90
struct nvmem_device * nvmem ;
91
+ struct regulator * vcc_reg ;
90
92
91
93
/*
92
94
* Some chips tie up multiple I2C addresses; dummy devices reserve
@@ -656,6 +658,9 @@ static int at24_probe(struct i2c_client *client)
656
658
at24 -> client [0 ].client = client ;
657
659
at24 -> client [0 ].regmap = regmap ;
658
660
661
+ at24 -> vcc_reg = devm_regulator_get (dev , "vcc" );
662
+ if (IS_ERR (at24 -> vcc_reg ))
663
+ return PTR_ERR (at24 -> vcc_reg );
659
664
660
665
writable = !(flags & AT24_FLAG_READONLY );
661
666
if (writable ) {
@@ -692,6 +697,12 @@ static int at24_probe(struct i2c_client *client)
692
697
693
698
i2c_set_clientdata (client , at24 );
694
699
700
+ err = regulator_enable (at24 -> vcc_reg );
701
+ if (err ) {
702
+ dev_err (dev , "Failed to enable vcc regulator\n" );
703
+ return err ;
704
+ }
705
+
695
706
/* enable runtime pm */
696
707
pm_runtime_set_active (dev );
697
708
pm_runtime_enable (dev );
@@ -704,6 +715,7 @@ static int at24_probe(struct i2c_client *client)
704
715
pm_runtime_idle (dev );
705
716
if (err ) {
706
717
pm_runtime_disable (dev );
718
+ regulator_disable (at24 -> vcc_reg );
707
719
return - ENODEV ;
708
720
}
709
721
@@ -719,15 +731,42 @@ static int at24_probe(struct i2c_client *client)
719
731
720
732
static int at24_remove (struct i2c_client * client )
721
733
{
734
+ struct at24_data * at24 = i2c_get_clientdata (client );
735
+
722
736
pm_runtime_disable (& client -> dev );
737
+ if (!pm_runtime_status_suspended (& client -> dev ))
738
+ regulator_disable (at24 -> vcc_reg );
723
739
pm_runtime_set_suspended (& client -> dev );
724
740
725
741
return 0 ;
726
742
}
727
743
744
+ static int __maybe_unused at24_suspend (struct device * dev )
745
+ {
746
+ struct i2c_client * client = to_i2c_client (dev );
747
+ struct at24_data * at24 = i2c_get_clientdata (client );
748
+
749
+ return regulator_disable (at24 -> vcc_reg );
750
+ }
751
+
752
+ static int __maybe_unused at24_resume (struct device * dev )
753
+ {
754
+ struct i2c_client * client = to_i2c_client (dev );
755
+ struct at24_data * at24 = i2c_get_clientdata (client );
756
+
757
+ return regulator_enable (at24 -> vcc_reg );
758
+ }
759
+
760
+ static const struct dev_pm_ops at24_pm_ops = {
761
+ SET_SYSTEM_SLEEP_PM_OPS (pm_runtime_force_suspend ,
762
+ pm_runtime_force_resume )
763
+ SET_RUNTIME_PM_OPS (at24_suspend , at24_resume , NULL )
764
+ };
765
+
728
766
static struct i2c_driver at24_driver = {
729
767
.driver = {
730
768
.name = "at24" ,
769
+ .pm = & at24_pm_ops ,
731
770
.of_match_table = at24_of_match ,
732
771
.acpi_match_table = ACPI_PTR (at24_acpi_ids ),
733
772
},
0 commit comments