@@ -123,6 +123,8 @@ static void regcache_hw_exit(struct regmap *map)
123123
124124int regcache_init (struct regmap * map , const struct regmap_config * config )
125125{
126+ bool sort_defaults = false;
127+ unsigned int reg_prev = 0 ;
126128 int count = 0 ;
127129 int ret ;
128130 int i ;
@@ -149,10 +151,16 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
149151 return - EINVAL ;
150152 }
151153
152- for (i = 0 ; i < config -> num_reg_defaults ; i ++ )
154+ for (i = 0 ; i < config -> num_reg_defaults ; i ++ ) {
153155 if (config -> reg_defaults [i ].reg % map -> reg_stride )
154156 return - EINVAL ;
155157
158+ if (reg_prev > config -> reg_defaults [i ].reg )
159+ sort_defaults = true;
160+
161+ reg_prev = config -> reg_defaults [i ].reg ;
162+ }
163+
156164 for (i = 0 ; i < ARRAY_SIZE (cache_types ); i ++ )
157165 if (cache_types [i ]-> type == map -> cache_type )
158166 break ;
@@ -186,6 +194,13 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
186194 sizeof (* map -> reg_defaults ), GFP_KERNEL );
187195 if (!tmp_buf )
188196 return - ENOMEM ;
197+
198+ /* regcache_lookup_reg() bsearch()es this array */
199+ if (sort_defaults ) {
200+ dev_warn (map -> dev ,
201+ "Driver needs fixing: Unsorted reg_defaults, sorting the copy\n" );
202+ regcache_sort_defaults (tmp_buf , map -> num_reg_defaults );
203+ }
189204 map -> reg_defaults = tmp_buf ;
190205 } else if (map -> num_reg_defaults_raw ) {
191206 count = regcache_count_cacheable_registers (map );
@@ -397,11 +412,16 @@ static int rbtree_all(const void *key, const struct rb_node *node)
397412 * volatile. In general drivers can choose not to use the provided
398413 * syncing functionality if they so require.
399414 *
415+ * This pushes cached changes made while cache_only (e.g. suspend) down
416+ * to hardware. The caller must disable cache_only before calling this
417+ * function.
418+ *
400419 * Return a negative value on failure, 0 on success.
401420 */
402421int regcache_sync (struct regmap * map )
403422{
404- int ret = 0 ;
423+ int sync_ret = 0 ;
424+ int selector_ret = 0 ;
405425 unsigned int i ;
406426 const char * name ;
407427 bool bypass ;
@@ -413,6 +433,12 @@ int regcache_sync(struct regmap *map)
413433 BUG_ON (!map -> cache_ops );
414434
415435 map -> lock (map -> lock_arg );
436+
437+ if (WARN_ON (map -> cache_only )) {
438+ map -> unlock (map -> lock_arg );
439+ return - EINVAL ;
440+ }
441+
416442 /* Remember the initial bypass state */
417443 bypass = map -> cache_bypass ;
418444 dev_dbg (map -> dev , "Syncing %s cache\n" ,
@@ -426,21 +452,21 @@ int regcache_sync(struct regmap *map)
426452 /* Apply any patch first */
427453 map -> cache_bypass = true;
428454 for (i = 0 ; i < map -> patch_regs ; i ++ ) {
429- ret = _regmap_write (map , map -> patch [i ].reg , map -> patch [i ].def );
430- if (ret != 0 ) {
455+ sync_ret = _regmap_write (map , map -> patch [i ].reg , map -> patch [i ].def );
456+ if (sync_ret != 0 ) {
431457 dev_err (map -> dev , "Failed to write %x = %x: %d\n" ,
432- map -> patch [i ].reg , map -> patch [i ].def , ret );
458+ map -> patch [i ].reg , map -> patch [i ].def , sync_ret );
433459 goto out ;
434460 }
435461 }
436462 map -> cache_bypass = false;
437463
438464 if (map -> cache_ops -> sync )
439- ret = map -> cache_ops -> sync (map , 0 , map -> max_register );
465+ sync_ret = map -> cache_ops -> sync (map , 0 , map -> max_register );
440466 else
441- ret = regcache_default_sync (map , 0 , map -> max_register );
467+ sync_ret = regcache_default_sync (map , 0 , map -> max_register );
442468
443- if (ret == 0 )
469+ if (sync_ret == 0 )
444470 map -> cache_dirty = false;
445471
446472out :
@@ -462,10 +488,11 @@ int regcache_sync(struct regmap *map)
462488 if (regcache_read (map , this -> selector_reg , & i ) != 0 )
463489 continue ;
464490
465- ret = _regmap_write (map , this -> selector_reg , i );
466- if (ret != 0 ) {
491+ selector_ret = _regmap_write (map , this -> selector_reg , i );
492+ if (selector_ret != 0 ) {
493+ map -> cache_dirty = true;
467494 dev_err (map -> dev , "Failed to write %x = %x: %d\n" ,
468- this -> selector_reg , i , ret );
495+ this -> selector_reg , i , selector_ret );
469496 break ;
470497 }
471498 }
@@ -476,7 +503,7 @@ int regcache_sync(struct regmap *map)
476503
477504 trace_regcache_sync (map , name , "stop" );
478505
479- return ret ;
506+ return sync_ret ? sync_ret : selector_ret ;
480507}
481508EXPORT_SYMBOL_GPL (regcache_sync );
482509
@@ -506,6 +533,10 @@ int regcache_sync_region(struct regmap *map, unsigned int min,
506533
507534 map -> lock (map -> lock_arg );
508535
536+ if (WARN_ON (map -> cache_only )) {
537+ map -> unlock (map -> lock_arg );
538+ return - EINVAL ;
539+ }
509540 /* Remember the initial bypass state */
510541 bypass = map -> cache_bypass ;
511542
@@ -727,14 +758,6 @@ unsigned int regcache_get_val(struct regmap *map, const void *base,
727758 return -1 ;
728759}
729760
730- static int regcache_default_cmp (const void * a , const void * b )
731- {
732- const struct reg_default * _a = a ;
733- const struct reg_default * _b = b ;
734-
735- return _a -> reg - _b -> reg ;
736- }
737-
738761int regcache_lookup_reg (struct regmap * map , unsigned int reg )
739762{
740763 struct reg_default key ;
@@ -744,7 +767,7 @@ int regcache_lookup_reg(struct regmap *map, unsigned int reg)
744767 key .def = 0 ;
745768
746769 r = bsearch (& key , map -> reg_defaults , map -> num_reg_defaults ,
747- sizeof (struct reg_default ), regcache_default_cmp );
770+ sizeof (struct reg_default ), regcache_defaults_cmp );
748771
749772 if (r )
750773 return r - map -> reg_defaults ;
0 commit comments