@@ -178,6 +178,15 @@ static void regmap_unlock_spinlock(struct regmap *map)
178
178
spin_unlock (& map -> spinlock );
179
179
}
180
180
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
+
181
190
/**
182
191
* regmap_init(): Initialise register map
183
192
*
@@ -195,7 +204,7 @@ struct regmap *regmap_init(struct device *dev,
195
204
void * bus_context ,
196
205
const struct regmap_config * config )
197
206
{
198
- struct regmap * map ;
207
+ struct regmap * map , * * m ;
199
208
int ret = - EINVAL ;
200
209
201
210
if (!bus || !config )
@@ -230,6 +239,7 @@ struct regmap *regmap_init(struct device *dev,
230
239
map -> volatile_reg = config -> volatile_reg ;
231
240
map -> precious_reg = config -> precious_reg ;
232
241
map -> cache_type = config -> cache_type ;
242
+ map -> name = config -> name ;
233
243
234
244
if (config -> read_flag_mask || config -> write_flag_mask ) {
235
245
map -> read_flag_mask = config -> read_flag_mask ;
@@ -326,8 +336,19 @@ struct regmap *regmap_init(struct device *dev,
326
336
if (ret < 0 )
327
337
goto err_free_workbuf ;
328
338
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
+
329
348
return map ;
330
349
350
+ err_cache :
351
+ regcache_exit (map );
331
352
err_free_workbuf :
332
353
kfree (map -> work_buf );
333
354
err_map :
@@ -431,6 +452,44 @@ void regmap_exit(struct regmap *map)
431
452
}
432
453
EXPORT_SYMBOL_GPL (regmap_exit );
433
454
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
+
434
493
static int _regmap_raw_write (struct regmap * map , unsigned int reg ,
435
494
const void * val , size_t val_len )
436
495
{
0 commit comments