@@ -178,6 +178,15 @@ static void regmap_unlock_spinlock(struct regmap *map)
178178 spin_unlock (& map -> spinlock );
179179}
180180
181+ static void dev_get_regmap_release (struct device * dev , void * res )
182+ {
183+ /*
184+ * We don't actually have anything to do here; the goal here
185+ * is not to manage the regmap but to provide a simple way to
186+ * get the regmap back given a struct device.
187+ */
188+ }
189+
181190/**
182191 * regmap_init(): Initialise register map
183192 *
@@ -195,7 +204,7 @@ struct regmap *regmap_init(struct device *dev,
195204 void * bus_context ,
196205 const struct regmap_config * config )
197206{
198- struct regmap * map ;
207+ struct regmap * map , * * m ;
199208 int ret = - EINVAL ;
200209
201210 if (!bus || !config )
@@ -230,6 +239,7 @@ struct regmap *regmap_init(struct device *dev,
230239 map -> volatile_reg = config -> volatile_reg ;
231240 map -> precious_reg = config -> precious_reg ;
232241 map -> cache_type = config -> cache_type ;
242+ map -> name = config -> name ;
233243
234244 if (config -> read_flag_mask || config -> write_flag_mask ) {
235245 map -> read_flag_mask = config -> read_flag_mask ;
@@ -326,8 +336,19 @@ struct regmap *regmap_init(struct device *dev,
326336 if (ret < 0 )
327337 goto err_free_workbuf ;
328338
339+ /* Add a devres resource for dev_get_regmap() */
340+ m = devres_alloc (dev_get_regmap_release , sizeof (* m ), GFP_KERNEL );
341+ if (!m ) {
342+ ret = - ENOMEM ;
343+ goto err_cache ;
344+ }
345+ * m = map ;
346+ devres_add (dev , m );
347+
329348 return map ;
330349
350+ err_cache :
351+ regcache_exit (map );
331352err_free_workbuf :
332353 kfree (map -> work_buf );
333354err_map :
@@ -431,6 +452,44 @@ void regmap_exit(struct regmap *map)
431452}
432453EXPORT_SYMBOL_GPL (regmap_exit );
433454
455+ static int dev_get_regmap_match (struct device * dev , void * res , void * data )
456+ {
457+ struct regmap * * r = res ;
458+ if (!r || !* r ) {
459+ WARN_ON (!r || !* r );
460+ return 0 ;
461+ }
462+
463+ /* If the user didn't specify a name match any */
464+ if (data )
465+ return (* r )-> name == data ;
466+ else
467+ return 1 ;
468+ }
469+
470+ /**
471+ * dev_get_regmap(): Obtain the regmap (if any) for a device
472+ *
473+ * @dev: Device to retrieve the map for
474+ * @name: Optional name for the register map, usually NULL.
475+ *
476+ * Returns the regmap for the device if one is present, or NULL. If
477+ * name is specified then it must match the name specified when
478+ * registering the device, if it is NULL then the first regmap found
479+ * will be used. Devices with multiple register maps are very rare,
480+ * generic code should normally not need to specify a name.
481+ */
482+ struct regmap * dev_get_regmap (struct device * dev , const char * name )
483+ {
484+ struct regmap * * r = devres_find (dev , dev_get_regmap_release ,
485+ dev_get_regmap_match , (void * )name );
486+
487+ if (!r )
488+ return NULL ;
489+ return * r ;
490+ }
491+ EXPORT_SYMBOL_GPL (dev_get_regmap );
492+
434493static int _regmap_raw_write (struct regmap * map , unsigned int reg ,
435494 const void * val , size_t val_len )
436495{
0 commit comments