1515#include <linux/property.h>
1616#include <linux/regmap.h>
1717
18+ struct mux_mmio {
19+ struct regmap_field * * fields ;
20+ unsigned int * hardware_states ;
21+ };
22+
23+ static int mux_mmio_get (struct mux_control * mux , int * state )
24+ {
25+ struct mux_mmio * mux_mmio = mux_chip_priv (mux -> chip );
26+ unsigned int index = mux_control_get_index (mux );
27+
28+ return regmap_field_read (mux_mmio -> fields [index ], state );
29+ }
30+
1831static int mux_mmio_set (struct mux_control * mux , int state )
1932{
20- struct regmap_field * * fields = mux_chip_priv (mux -> chip );
33+ struct mux_mmio * mux_mmio = mux_chip_priv (mux -> chip );
34+ unsigned int index = mux_control_get_index (mux );
2135
22- return regmap_field_write (fields [mux_control_get_index ( mux ) ], state );
36+ return regmap_field_write (mux_mmio -> fields [index ], state );
2337}
2438
2539static const struct mux_control_ops mux_mmio_ops = {
@@ -43,8 +57,8 @@ static int mux_mmio_probe(struct platform_device *pdev)
4357{
4458 struct device * dev = & pdev -> dev ;
4559 struct device_node * np = dev -> of_node ;
46- struct regmap_field * * fields ;
4760 struct mux_chip * mux_chip ;
61+ struct mux_mmio * mux_mmio ;
4862 struct regmap * regmap ;
4963 void __iomem * base ;
5064 int num_fields ;
@@ -80,12 +94,20 @@ static int mux_mmio_probe(struct platform_device *pdev)
8094 }
8195 num_fields = ret / 2 ;
8296
83- mux_chip = devm_mux_chip_alloc (dev , num_fields , num_fields *
84- sizeof (* fields ));
97+ mux_chip = devm_mux_chip_alloc (dev , num_fields , sizeof (struct mux_mmio ));
8598 if (IS_ERR (mux_chip ))
8699 return PTR_ERR (mux_chip );
87100
88- fields = mux_chip_priv (mux_chip );
101+ mux_mmio = mux_chip_priv (mux_chip );
102+
103+ mux_mmio -> fields = devm_kmalloc (dev , num_fields * sizeof (* mux_mmio -> fields ), GFP_KERNEL );
104+ if (IS_ERR (mux_mmio -> fields ))
105+ return PTR_ERR (mux_mmio -> fields );
106+
107+ mux_mmio -> hardware_states = devm_kmalloc (dev , num_fields *
108+ sizeof (* mux_mmio -> hardware_states ), GFP_KERNEL );
109+ if (IS_ERR (mux_mmio -> hardware_states ))
110+ return PTR_ERR (mux_mmio -> hardware_states );
89111
90112 for (i = 0 ; i < num_fields ; i ++ ) {
91113 struct mux_control * mux = & mux_chip -> mux [i ];
@@ -115,9 +137,9 @@ static int mux_mmio_probe(struct platform_device *pdev)
115137 return - EINVAL ;
116138 }
117139
118- fields [i ] = devm_regmap_field_alloc (dev , regmap , field );
119- if (IS_ERR (fields [i ])) {
120- ret = PTR_ERR (fields [i ]);
140+ mux_mmio -> fields [i ] = devm_regmap_field_alloc (dev , regmap , field );
141+ if (IS_ERR (mux_mmio -> fields [i ])) {
142+ ret = PTR_ERR (mux_mmio -> fields [i ]);
121143 dev_err (dev , "bitfield %d: failed to allocate: %d\n" ,
122144 i , ret );
123145 return ret ;
@@ -141,13 +163,55 @@ static int mux_mmio_probe(struct platform_device *pdev)
141163
142164 mux_chip -> ops = & mux_mmio_ops ;
143165
166+ dev_set_drvdata (dev , mux_chip );
167+
144168 return devm_mux_chip_register (dev , mux_chip );
145169}
146170
171+ static int mux_mmio_suspend_noirq (struct device * dev )
172+ {
173+ struct mux_chip * mux_chip = dev_get_drvdata (dev );
174+ struct mux_mmio * mux_mmio = mux_chip_priv (mux_chip );
175+ unsigned int state ;
176+ int ret , i ;
177+
178+ for (i = 0 ; i < mux_chip -> controllers ; i ++ ) {
179+ ret = mux_mmio_get (& mux_chip -> mux [i ], & state );
180+ if (ret ) {
181+ dev_err (dev , "control %u: error saving mux: %d\n" , i , ret );
182+ return ret ;
183+ }
184+
185+ mux_mmio -> hardware_states [i ] = state ;
186+ }
187+
188+ return 0 ;
189+ }
190+
191+ static int mux_mmio_resume_noirq (struct device * dev )
192+ {
193+ struct mux_chip * mux_chip = dev_get_drvdata (dev );
194+ struct mux_mmio * mux_mmio = mux_chip_priv (mux_chip );
195+ int ret , i ;
196+
197+ for (i = 0 ; i < mux_chip -> controllers ; i ++ ) {
198+ ret = mux_mmio_set (& mux_chip -> mux [i ], mux_mmio -> hardware_states [i ]);
199+ if (ret ) {
200+ dev_err (dev , "control %u: error restoring mux: %d\n" , i , ret );
201+ return ret ;
202+ }
203+ }
204+
205+ return 0 ;
206+ }
207+
208+ static DEFINE_NOIRQ_DEV_PM_OPS (mux_mmio_pm_ops , mux_mmio_suspend_noirq , mux_mmio_resume_noirq ) ;
209+
147210static struct platform_driver mux_mmio_driver = {
148211 .driver = {
149212 .name = "mmio-mux" ,
150213 .of_match_table = mux_mmio_dt_ids ,
214+ .pm = pm_sleep_ptr (& mux_mmio_pm_ops ),
151215 },
152216 .probe = mux_mmio_probe ,
153217};
0 commit comments