@@ -236,19 +236,22 @@ static bool regulator_supply_is_couple(struct regulator_dev *rdev)
236
236
static void regulator_unlock_recursive (struct regulator_dev * rdev ,
237
237
unsigned int n_coupled )
238
238
{
239
- struct regulator_dev * c_rdev ;
240
- int i ;
239
+ struct regulator_dev * c_rdev , * supply_rdev ;
240
+ int i , supply_n_coupled ;
241
241
242
242
for (i = n_coupled ; i > 0 ; i -- ) {
243
243
c_rdev = rdev -> coupling_desc .coupled_rdevs [i - 1 ];
244
244
245
245
if (!c_rdev )
246
246
continue ;
247
247
248
- if (c_rdev -> supply && !regulator_supply_is_couple (c_rdev ))
249
- regulator_unlock_recursive (
250
- c_rdev -> supply -> rdev ,
251
- c_rdev -> coupling_desc .n_coupled );
248
+ if (c_rdev -> supply && !regulator_supply_is_couple (c_rdev )) {
249
+ supply_rdev = c_rdev -> supply -> rdev ;
250
+ supply_n_coupled = supply_rdev -> coupling_desc .n_coupled ;
251
+
252
+ regulator_unlock_recursive (supply_rdev ,
253
+ supply_n_coupled );
254
+ }
252
255
253
256
regulator_unlock (c_rdev );
254
257
}
@@ -1461,7 +1464,7 @@ static int set_consumer_device_supply(struct regulator_dev *rdev,
1461
1464
const char * consumer_dev_name ,
1462
1465
const char * supply )
1463
1466
{
1464
- struct regulator_map * node ;
1467
+ struct regulator_map * node , * new_node ;
1465
1468
int has_dev ;
1466
1469
1467
1470
if (supply == NULL )
@@ -1472,6 +1475,22 @@ static int set_consumer_device_supply(struct regulator_dev *rdev,
1472
1475
else
1473
1476
has_dev = 0 ;
1474
1477
1478
+ new_node = kzalloc (sizeof (struct regulator_map ), GFP_KERNEL );
1479
+ if (new_node == NULL )
1480
+ return - ENOMEM ;
1481
+
1482
+ new_node -> regulator = rdev ;
1483
+ new_node -> supply = supply ;
1484
+
1485
+ if (has_dev ) {
1486
+ new_node -> dev_name = kstrdup (consumer_dev_name , GFP_KERNEL );
1487
+ if (new_node -> dev_name == NULL ) {
1488
+ kfree (new_node );
1489
+ return - ENOMEM ;
1490
+ }
1491
+ }
1492
+
1493
+ mutex_lock (& regulator_list_mutex );
1475
1494
list_for_each_entry (node , & regulator_map_list , list ) {
1476
1495
if (node -> dev_name && consumer_dev_name ) {
1477
1496
if (strcmp (node -> dev_name , consumer_dev_name ) != 0 )
@@ -1489,26 +1508,19 @@ static int set_consumer_device_supply(struct regulator_dev *rdev,
1489
1508
node -> regulator -> desc -> name ,
1490
1509
supply ,
1491
1510
dev_name (& rdev -> dev ), rdev_get_name (rdev ));
1492
- return - EBUSY ;
1511
+ goto fail ;
1493
1512
}
1494
1513
1495
- node = kzalloc (sizeof (struct regulator_map ), GFP_KERNEL );
1496
- if (node == NULL )
1497
- return - ENOMEM ;
1498
-
1499
- node -> regulator = rdev ;
1500
- node -> supply = supply ;
1501
-
1502
- if (has_dev ) {
1503
- node -> dev_name = kstrdup (consumer_dev_name , GFP_KERNEL );
1504
- if (node -> dev_name == NULL ) {
1505
- kfree (node );
1506
- return - ENOMEM ;
1507
- }
1508
- }
1514
+ list_add (& new_node -> list , & regulator_map_list );
1515
+ mutex_unlock (& regulator_list_mutex );
1509
1516
1510
- list_add (& node -> list , & regulator_map_list );
1511
1517
return 0 ;
1518
+
1519
+ fail :
1520
+ mutex_unlock (& regulator_list_mutex );
1521
+ kfree (new_node -> dev_name );
1522
+ kfree (new_node );
1523
+ return - EBUSY ;
1512
1524
}
1513
1525
1514
1526
static void unset_regulator_supplies (struct regulator_dev * rdev )
@@ -1580,44 +1592,53 @@ static struct regulator *create_regulator(struct regulator_dev *rdev,
1580
1592
const char * supply_name )
1581
1593
{
1582
1594
struct regulator * regulator ;
1583
- char buf [REG_STR_SIZE ];
1584
- int err , size ;
1595
+ int err ;
1596
+
1597
+ if (dev ) {
1598
+ char buf [REG_STR_SIZE ];
1599
+ int size ;
1600
+
1601
+ size = snprintf (buf , REG_STR_SIZE , "%s-%s" ,
1602
+ dev -> kobj .name , supply_name );
1603
+ if (size >= REG_STR_SIZE )
1604
+ return NULL ;
1605
+
1606
+ supply_name = kstrdup (buf , GFP_KERNEL );
1607
+ if (supply_name == NULL )
1608
+ return NULL ;
1609
+ } else {
1610
+ supply_name = kstrdup_const (supply_name , GFP_KERNEL );
1611
+ if (supply_name == NULL )
1612
+ return NULL ;
1613
+ }
1585
1614
1586
1615
regulator = kzalloc (sizeof (* regulator ), GFP_KERNEL );
1587
- if (regulator == NULL )
1616
+ if (regulator == NULL ) {
1617
+ kfree (supply_name );
1588
1618
return NULL ;
1619
+ }
1589
1620
1590
- regulator_lock (rdev );
1591
1621
regulator -> rdev = rdev ;
1622
+ regulator -> supply_name = supply_name ;
1623
+
1624
+ regulator_lock (rdev );
1592
1625
list_add (& regulator -> list , & rdev -> consumer_list );
1626
+ regulator_unlock (rdev );
1593
1627
1594
1628
if (dev ) {
1595
1629
regulator -> dev = dev ;
1596
1630
1597
1631
/* Add a link to the device sysfs entry */
1598
- size = snprintf (buf , REG_STR_SIZE , "%s-%s" ,
1599
- dev -> kobj .name , supply_name );
1600
- if (size >= REG_STR_SIZE )
1601
- goto overflow_err ;
1602
-
1603
- regulator -> supply_name = kstrdup (buf , GFP_KERNEL );
1604
- if (regulator -> supply_name == NULL )
1605
- goto overflow_err ;
1606
-
1607
1632
err = sysfs_create_link_nowarn (& rdev -> dev .kobj , & dev -> kobj ,
1608
- buf );
1633
+ supply_name );
1609
1634
if (err ) {
1610
1635
rdev_dbg (rdev , "could not add device link %s err %d\n" ,
1611
1636
dev -> kobj .name , err );
1612
1637
/* non-fatal */
1613
1638
}
1614
- } else {
1615
- regulator -> supply_name = kstrdup_const (supply_name , GFP_KERNEL );
1616
- if (regulator -> supply_name == NULL )
1617
- goto overflow_err ;
1618
1639
}
1619
1640
1620
- regulator -> debugfs = debugfs_create_dir (regulator -> supply_name ,
1641
+ regulator -> debugfs = debugfs_create_dir (supply_name ,
1621
1642
rdev -> debugfs );
1622
1643
if (!regulator -> debugfs ) {
1623
1644
rdev_dbg (rdev , "Failed to create debugfs directory\n" );
@@ -1642,13 +1663,7 @@ static struct regulator *create_regulator(struct regulator_dev *rdev,
1642
1663
_regulator_is_enabled (rdev ))
1643
1664
regulator -> always_on = true;
1644
1665
1645
- regulator_unlock (rdev );
1646
1666
return regulator ;
1647
- overflow_err :
1648
- list_del (& regulator -> list );
1649
- kfree (regulator );
1650
- regulator_unlock (rdev );
1651
- return NULL ;
1652
1667
}
1653
1668
1654
1669
static int _regulator_get_enable_time (struct regulator_dev * rdev )
@@ -2230,10 +2245,13 @@ EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2230
2245
static int regulator_ena_gpio_request (struct regulator_dev * rdev ,
2231
2246
const struct regulator_config * config )
2232
2247
{
2233
- struct regulator_enable_gpio * pin ;
2248
+ struct regulator_enable_gpio * pin , * new_pin ;
2234
2249
struct gpio_desc * gpiod ;
2235
2250
2236
2251
gpiod = config -> ena_gpiod ;
2252
+ new_pin = kzalloc (sizeof (* new_pin ), GFP_KERNEL );
2253
+
2254
+ mutex_lock (& regulator_list_mutex );
2237
2255
2238
2256
list_for_each_entry (pin , & regulator_ena_gpio_list , list ) {
2239
2257
if (pin -> gpiod == gpiod ) {
@@ -2242,16 +2260,24 @@ static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2242
2260
}
2243
2261
}
2244
2262
2245
- pin = kzalloc ( sizeof ( struct regulator_enable_gpio ), GFP_KERNEL );
2246
- if ( pin == NULL )
2263
+ if ( new_pin == NULL ) {
2264
+ mutex_unlock ( & regulator_list_mutex );
2247
2265
return - ENOMEM ;
2266
+ }
2267
+
2268
+ pin = new_pin ;
2269
+ new_pin = NULL ;
2248
2270
2249
2271
pin -> gpiod = gpiod ;
2250
2272
list_add (& pin -> list , & regulator_ena_gpio_list );
2251
2273
2252
2274
update_ena_gpio_to_rdev :
2253
2275
pin -> request_count ++ ;
2254
2276
rdev -> ena_pin = pin ;
2277
+
2278
+ mutex_unlock (& regulator_list_mutex );
2279
+ kfree (new_pin );
2280
+
2255
2281
return 0 ;
2256
2282
}
2257
2283
@@ -2264,19 +2290,19 @@ static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2264
2290
2265
2291
/* Free the GPIO only in case of no use */
2266
2292
list_for_each_entry_safe (pin , n , & regulator_ena_gpio_list , list ) {
2267
- if (pin -> gpiod == rdev -> ena_pin -> gpiod ) {
2268
- if (pin -> request_count <= 1 ) {
2269
- pin -> request_count = 0 ;
2270
- gpiod_put (pin -> gpiod );
2271
- list_del (& pin -> list );
2272
- kfree (pin );
2273
- rdev -> ena_pin = NULL ;
2274
- return ;
2275
- } else {
2276
- pin -> request_count -- ;
2277
- }
2278
- }
2293
+ if (pin != rdev -> ena_pin )
2294
+ continue ;
2295
+
2296
+ if (-- pin -> request_count )
2297
+ break ;
2298
+
2299
+ gpiod_put (pin -> gpiod );
2300
+ list_del (& pin -> list );
2301
+ kfree (pin );
2302
+ break ;
2279
2303
}
2304
+
2305
+ rdev -> ena_pin = NULL ;
2280
2306
}
2281
2307
2282
2308
/**
@@ -4949,13 +4975,9 @@ static void regulator_resolve_coupling(struct regulator_dev *rdev)
4949
4975
return ;
4950
4976
}
4951
4977
4952
- regulator_lock (c_rdev );
4953
-
4954
4978
c_desc -> coupled_rdevs [i ] = c_rdev ;
4955
4979
c_desc -> n_resolved ++ ;
4956
4980
4957
- regulator_unlock (c_rdev );
4958
-
4959
4981
regulator_resolve_coupling (c_rdev );
4960
4982
}
4961
4983
}
@@ -5040,7 +5062,10 @@ static int regulator_init_coupling(struct regulator_dev *rdev)
5040
5062
if (!of_check_coupling_data (rdev ))
5041
5063
return - EPERM ;
5042
5064
5065
+ mutex_lock (& regulator_list_mutex );
5043
5066
rdev -> coupling_desc .coupler = regulator_find_coupler (rdev );
5067
+ mutex_unlock (& regulator_list_mutex );
5068
+
5044
5069
if (IS_ERR (rdev -> coupling_desc .coupler )) {
5045
5070
err = PTR_ERR (rdev -> coupling_desc .coupler );
5046
5071
rdev_err (rdev , "failed to get coupler: %d\n" , err );
@@ -5141,16 +5166,16 @@ regulator_register(const struct regulator_desc *regulator_desc,
5141
5166
ret = - ENOMEM ;
5142
5167
goto rinse ;
5143
5168
}
5169
+ device_initialize (& rdev -> dev );
5144
5170
5145
5171
/*
5146
5172
* Duplicate the config so the driver could override it after
5147
5173
* parsing init data.
5148
5174
*/
5149
5175
config = kmemdup (cfg , sizeof (* cfg ), GFP_KERNEL );
5150
5176
if (config == NULL ) {
5151
- kfree (rdev );
5152
5177
ret = - ENOMEM ;
5153
- goto rinse ;
5178
+ goto clean ;
5154
5179
}
5155
5180
5156
5181
init_data = regulator_of_get_init_data (dev , regulator_desc , config ,
@@ -5162,10 +5187,8 @@ regulator_register(const struct regulator_desc *regulator_desc,
5162
5187
* from a gpio extender or something else.
5163
5188
*/
5164
5189
if (PTR_ERR (init_data ) == - EPROBE_DEFER ) {
5165
- kfree (config );
5166
- kfree (rdev );
5167
5190
ret = - EPROBE_DEFER ;
5168
- goto rinse ;
5191
+ goto clean ;
5169
5192
}
5170
5193
5171
5194
/*
@@ -5206,9 +5229,7 @@ regulator_register(const struct regulator_desc *regulator_desc,
5206
5229
}
5207
5230
5208
5231
if (config -> ena_gpiod ) {
5209
- mutex_lock (& regulator_list_mutex );
5210
5232
ret = regulator_ena_gpio_request (rdev , config );
5211
- mutex_unlock (& regulator_list_mutex );
5212
5233
if (ret != 0 ) {
5213
5234
rdev_err (rdev , "Failed to request enable GPIO: %d\n" ,
5214
5235
ret );
@@ -5220,7 +5241,6 @@ regulator_register(const struct regulator_desc *regulator_desc,
5220
5241
}
5221
5242
5222
5243
/* register with sysfs */
5223
- device_initialize (& rdev -> dev );
5224
5244
rdev -> dev .class = & regulator_class ;
5225
5245
rdev -> dev .parent = dev ;
5226
5246
dev_set_name (& rdev -> dev , "regulator.%lu" ,
@@ -5248,27 +5268,22 @@ regulator_register(const struct regulator_desc *regulator_desc,
5248
5268
if (ret < 0 )
5249
5269
goto wash ;
5250
5270
5251
- mutex_lock (& regulator_list_mutex );
5252
5271
ret = regulator_init_coupling (rdev );
5253
- mutex_unlock (& regulator_list_mutex );
5254
5272
if (ret < 0 )
5255
5273
goto wash ;
5256
5274
5257
5275
/* add consumers devices */
5258
5276
if (init_data ) {
5259
- mutex_lock (& regulator_list_mutex );
5260
5277
for (i = 0 ; i < init_data -> num_consumer_supplies ; i ++ ) {
5261
5278
ret = set_consumer_device_supply (rdev ,
5262
5279
init_data -> consumer_supplies [i ].dev_name ,
5263
5280
init_data -> consumer_supplies [i ].supply );
5264
5281
if (ret < 0 ) {
5265
- mutex_unlock (& regulator_list_mutex );
5266
5282
dev_err (dev , "Failed to set supply %s\n" ,
5267
5283
init_data -> consumer_supplies [i ].supply );
5268
5284
goto unset_supplies ;
5269
5285
}
5270
5286
}
5271
- mutex_unlock (& regulator_list_mutex );
5272
5287
}
5273
5288
5274
5289
if (!rdev -> desc -> ops -> get_voltage &&
@@ -5303,13 +5318,11 @@ regulator_register(const struct regulator_desc *regulator_desc,
5303
5318
mutex_lock (& regulator_list_mutex );
5304
5319
regulator_ena_gpio_free (rdev );
5305
5320
mutex_unlock (& regulator_list_mutex );
5306
- put_device (& rdev -> dev );
5307
- rdev = NULL ;
5308
5321
clean :
5309
5322
if (dangling_of_gpiod )
5310
5323
gpiod_put (config -> ena_gpiod );
5311
- kfree (rdev );
5312
5324
kfree (config );
5325
+ put_device (& rdev -> dev );
5313
5326
rinse :
5314
5327
if (dangling_cfg_gpiod )
5315
5328
gpiod_put (cfg -> ena_gpiod );
0 commit comments