@@ -42,28 +42,29 @@ void regcache_sort_defaults(struct reg_default *defaults, unsigned int ndefaults
4242}
4343EXPORT_SYMBOL_GPL (regcache_sort_defaults );
4444
45- static int regcache_hw_init (struct regmap * map )
45+ static int regcache_count_cacheable_registers (struct regmap * map )
4646{
47- int i , j ;
48- int ret ;
49- int count ;
50- unsigned int reg , val ;
51- void * tmp_buf ;
52-
53- if (!map -> num_reg_defaults_raw )
54- return - EINVAL ;
47+ unsigned int count ;
5548
5649 /* calculate the size of reg_defaults */
57- for (count = 0 , i = 0 ; i < map -> num_reg_defaults_raw ; i ++ )
50+ count = 0 ;
51+ for (unsigned int i = 0 ; i < map -> num_reg_defaults_raw ; i ++ )
5852 if (regmap_readable (map , i * map -> reg_stride ) &&
5953 !regmap_volatile (map , i * map -> reg_stride ))
6054 count ++ ;
6155
6256 /* all registers are unreadable or volatile, so just bypass */
63- if (!count ) {
57+ if (!count )
6458 map -> cache_bypass = true;
65- return 0 ;
66- }
59+
60+ return count ;
61+ }
62+
63+ static int regcache_hw_init (struct regmap * map , int count )
64+ {
65+ int ret ;
66+ unsigned int reg , val ;
67+ void * tmp_buf ;
6768
6869 map -> num_reg_defaults = count ;
6970 map -> reg_defaults = kmalloc_objs (struct reg_default , count );
@@ -93,7 +94,7 @@ static int regcache_hw_init(struct regmap *map)
9394 }
9495
9596 /* fill the reg_defaults */
96- for (i = 0 , j = 0 ; i < map -> num_reg_defaults_raw ; i ++ ) {
97+ for (unsigned int i = 0 , j = 0 ; i < map -> num_reg_defaults_raw ; i ++ ) {
9798 reg = i * map -> reg_stride ;
9899
99100 if (!regmap_readable (map , reg ))
@@ -111,7 +112,7 @@ static int regcache_hw_init(struct regmap *map)
111112 ret = regmap_read (map , reg , & val );
112113 map -> cache_bypass = cache_bypass ;
113114 if (ret != 0 ) {
114- dev_err (map -> dev , "Failed to read %d : %d\n" ,
115+ dev_err (map -> dev , "Failed to read %x : %d\n" ,
115116 reg , ret );
116117 goto err_free ;
117118 }
@@ -130,8 +131,16 @@ static int regcache_hw_init(struct regmap *map)
130131 return ret ;
131132}
132133
134+ static void regcache_hw_exit (struct regmap * map )
135+ {
136+ kfree (map -> reg_defaults );
137+ if (map -> cache_free )
138+ kfree (map -> reg_defaults_raw );
139+ }
140+
133141int regcache_init (struct regmap * map , const struct regmap_config * config )
134142{
143+ int count = 0 ;
135144 int ret ;
136145 int i ;
137146 void * tmp_buf ;
@@ -196,15 +205,17 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
196205 return - ENOMEM ;
197206 map -> reg_defaults = tmp_buf ;
198207 } else if (map -> num_reg_defaults_raw ) {
208+ count = regcache_count_cacheable_registers (map );
209+ if (map -> cache_bypass )
210+ return 0 ;
211+
199212 /* Some devices such as PMICs don't have cache defaults,
200213 * we cope with this by reading back the HW registers and
201214 * crafting the cache defaults by hand.
202215 */
203- ret = regcache_hw_init (map );
216+ ret = regcache_hw_init (map , count );
204217 if (ret < 0 )
205218 return ret ;
206- if (map -> cache_bypass )
207- return 0 ;
208219 }
209220
210221 if (!map -> max_register_is_set && map -> num_reg_defaults_raw ) {
@@ -241,9 +252,7 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
241252 map -> unlock (map -> lock_arg );
242253 }
243254err_free :
244- kfree (map -> reg_defaults );
245- if (map -> cache_free )
246- kfree (map -> reg_defaults_raw );
255+ regcache_hw_exit (map );
247256
248257 return ret ;
249258}
@@ -255,9 +264,7 @@ void regcache_exit(struct regmap *map)
255264
256265 BUG_ON (!map -> cache_ops );
257266
258- kfree (map -> reg_defaults );
259- if (map -> cache_free )
260- kfree (map -> reg_defaults_raw );
267+ regcache_hw_exit (map );
261268
262269 if (map -> cache_ops -> exit ) {
263270 dev_dbg (map -> dev , "Destroying %s cache\n" ,
@@ -504,7 +511,7 @@ int regcache_sync_region(struct regmap *map, unsigned int min,
504511 bypass = map -> cache_bypass ;
505512
506513 name = map -> cache_ops -> name ;
507- dev_dbg (map -> dev , "Syncing %s cache from %d-%d \n" , name , min , max );
514+ dev_dbg (map -> dev , "Syncing %s cache from %#x-%#x \n" , name , min , max );
508515
509516 trace_regcache_sync (map , name , "start region" );
510517
@@ -835,13 +842,13 @@ static int regcache_sync_block_raw(struct regmap *map, void *block,
835842 unsigned int block_base , unsigned int start ,
836843 unsigned int end )
837844{
838- unsigned int i , val ;
839845 unsigned int regtmp = 0 ;
840846 unsigned int base = 0 ;
841847 const void * data = NULL ;
848+ unsigned int val ;
842849 int ret ;
843850
844- for (i = start ; i < end ; i ++ ) {
851+ for (unsigned int i = start ; i < end ; i ++ ) {
845852 regtmp = block_base + (i * map -> reg_stride );
846853
847854 if (!regcache_reg_present (cache_present , i ) ||
0 commit comments