28
28
#include <linux/regmap.h>
29
29
#include <linux/regulator/of_regulator.h>
30
30
#include <linux/regulator/consumer.h>
31
+ #include <linux/regulator/coupler.h>
31
32
#include <linux/regulator/driver.h>
32
33
#include <linux/regulator/machine.h>
33
34
#include <linux/module.h>
@@ -55,6 +56,7 @@ static DEFINE_MUTEX(regulator_list_mutex);
55
56
static LIST_HEAD (regulator_map_list );
56
57
static LIST_HEAD (regulator_ena_gpio_list );
57
58
static LIST_HEAD (regulator_supply_alias_list );
59
+ static LIST_HEAD (regulator_coupler_list );
58
60
static bool has_full_constraints ;
59
61
60
62
static struct dentry * debugfs_root ;
@@ -3439,11 +3441,10 @@ static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3439
3441
struct coupling_desc * c_desc = & rdev -> coupling_desc ;
3440
3442
struct regulator_dev * * c_rdevs = c_desc -> coupled_rdevs ;
3441
3443
struct regulation_constraints * constraints = rdev -> constraints ;
3442
- int max_spread = constraints -> max_spread ;
3443
3444
int desired_min_uV = 0 , desired_max_uV = INT_MAX ;
3444
3445
int max_current_uV = 0 , min_current_uV = INT_MAX ;
3445
3446
int highest_min_uV = 0 , target_uV , possible_uV ;
3446
- int i , ret ;
3447
+ int i , ret , max_spread ;
3447
3448
bool done ;
3448
3449
3449
3450
* current_uV = -1 ;
@@ -3497,6 +3498,8 @@ static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3497
3498
}
3498
3499
}
3499
3500
3501
+ max_spread = constraints -> max_spread [0 ];
3502
+
3500
3503
/*
3501
3504
* Let target_uV be equal to the desired one if possible.
3502
3505
* If not, set it to minimum voltage, allowed by other coupled
@@ -3578,9 +3581,11 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
3578
3581
struct regulator_dev * * c_rdevs ;
3579
3582
struct regulator_dev * best_rdev ;
3580
3583
struct coupling_desc * c_desc = & rdev -> coupling_desc ;
3584
+ struct regulator_coupler * coupler = c_desc -> coupler ;
3581
3585
int i , ret , n_coupled , best_min_uV , best_max_uV , best_c_rdev ;
3582
- bool best_c_rdev_done , c_rdev_done [MAX_COUPLED ];
3583
3586
unsigned int delta , best_delta ;
3587
+ unsigned long c_rdev_done = 0 ;
3588
+ bool best_c_rdev_done ;
3584
3589
3585
3590
c_rdevs = c_desc -> coupled_rdevs ;
3586
3591
n_coupled = c_desc -> n_coupled ;
@@ -3597,8 +3602,9 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
3597
3602
return - EPERM ;
3598
3603
}
3599
3604
3600
- for (i = 0 ; i < n_coupled ; i ++ )
3601
- c_rdev_done [i ] = false;
3605
+ /* Invoke custom balancer for customized couplers */
3606
+ if (coupler && coupler -> balance_voltage )
3607
+ return coupler -> balance_voltage (coupler , rdev , state );
3602
3608
3603
3609
/*
3604
3610
* Find the best possible voltage change on each loop. Leave the loop
@@ -3625,7 +3631,7 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
3625
3631
*/
3626
3632
int optimal_uV = 0 , optimal_max_uV = 0 , current_uV = 0 ;
3627
3633
3628
- if (c_rdev_done [ i ] )
3634
+ if (test_bit ( i , & c_rdev_done ) )
3629
3635
continue ;
3630
3636
3631
3637
ret = regulator_get_optimal_voltage (c_rdevs [i ],
@@ -3660,7 +3666,8 @@ static int regulator_balance_voltage(struct regulator_dev *rdev,
3660
3666
if (ret < 0 )
3661
3667
goto out ;
3662
3668
3663
- c_rdev_done [best_c_rdev ] = best_c_rdev_done ;
3669
+ if (best_c_rdev_done )
3670
+ set_bit (best_c_rdev , & c_rdev_done );
3664
3671
3665
3672
} while (n_coupled > 1 );
3666
3673
@@ -4712,8 +4719,60 @@ static int regulator_register_resolve_supply(struct device *dev, void *data)
4712
4719
return 0 ;
4713
4720
}
4714
4721
4722
+ int regulator_coupler_register (struct regulator_coupler * coupler )
4723
+ {
4724
+ mutex_lock (& regulator_list_mutex );
4725
+ list_add_tail (& coupler -> list , & regulator_coupler_list );
4726
+ mutex_unlock (& regulator_list_mutex );
4727
+
4728
+ return 0 ;
4729
+ }
4730
+
4731
+ static struct regulator_coupler *
4732
+ regulator_find_coupler (struct regulator_dev * rdev )
4733
+ {
4734
+ struct regulator_coupler * coupler ;
4735
+ int err ;
4736
+
4737
+ /*
4738
+ * Note that regulators are appended to the list and the generic
4739
+ * coupler is registered first, hence it will be attached at last
4740
+ * if nobody cared.
4741
+ */
4742
+ list_for_each_entry_reverse (coupler , & regulator_coupler_list , list ) {
4743
+ err = coupler -> attach_regulator (coupler , rdev );
4744
+ if (!err ) {
4745
+ if (!coupler -> balance_voltage &&
4746
+ rdev -> coupling_desc .n_coupled > 2 )
4747
+ goto err_unsupported ;
4748
+
4749
+ return coupler ;
4750
+ }
4751
+
4752
+ if (err < 0 )
4753
+ return ERR_PTR (err );
4754
+
4755
+ if (err == 1 )
4756
+ continue ;
4757
+
4758
+ break ;
4759
+ }
4760
+
4761
+ return ERR_PTR (- EINVAL );
4762
+
4763
+ err_unsupported :
4764
+ if (coupler -> detach_regulator )
4765
+ coupler -> detach_regulator (coupler , rdev );
4766
+
4767
+ rdev_err (rdev ,
4768
+ "Voltage balancing for multiple regulator couples is unimplemented\n" );
4769
+
4770
+ return ERR_PTR (- EPERM );
4771
+ }
4772
+
4715
4773
static void regulator_resolve_coupling (struct regulator_dev * rdev )
4716
4774
{
4775
+ struct regulator_coupler * coupler = rdev -> coupling_desc .coupler ;
4717
4776
struct coupling_desc * c_desc = & rdev -> coupling_desc ;
4718
4777
int n_coupled = c_desc -> n_coupled ;
4719
4778
struct regulator_dev * c_rdev ;
@@ -4729,6 +4788,12 @@ static void regulator_resolve_coupling(struct regulator_dev *rdev)
4729
4788
if (!c_rdev )
4730
4789
continue ;
4731
4790
4791
+ if (c_rdev -> coupling_desc .coupler != coupler ) {
4792
+ rdev_err (rdev , "coupler mismatch with %s\n" ,
4793
+ rdev_get_name (c_rdev ));
4794
+ return ;
4795
+ }
4796
+
4732
4797
regulator_lock (c_rdev );
4733
4798
4734
4799
c_desc -> coupled_rdevs [i ] = c_rdev ;
@@ -4742,10 +4807,12 @@ static void regulator_resolve_coupling(struct regulator_dev *rdev)
4742
4807
4743
4808
static void regulator_remove_coupling (struct regulator_dev * rdev )
4744
4809
{
4810
+ struct regulator_coupler * coupler = rdev -> coupling_desc .coupler ;
4745
4811
struct coupling_desc * __c_desc , * c_desc = & rdev -> coupling_desc ;
4746
4812
struct regulator_dev * __c_rdev , * c_rdev ;
4747
4813
unsigned int __n_coupled , n_coupled ;
4748
4814
int i , k ;
4815
+ int err ;
4749
4816
4750
4817
n_coupled = c_desc -> n_coupled ;
4751
4818
@@ -4775,21 +4842,33 @@ static void regulator_remove_coupling(struct regulator_dev *rdev)
4775
4842
c_desc -> coupled_rdevs [i ] = NULL ;
4776
4843
c_desc -> n_resolved -- ;
4777
4844
}
4845
+
4846
+ if (coupler && coupler -> detach_regulator ) {
4847
+ err = coupler -> detach_regulator (coupler , rdev );
4848
+ if (err )
4849
+ rdev_err (rdev , "failed to detach from coupler: %d\n" ,
4850
+ err );
4851
+ }
4852
+
4853
+ kfree (rdev -> coupling_desc .coupled_rdevs );
4854
+ rdev -> coupling_desc .coupled_rdevs = NULL ;
4778
4855
}
4779
4856
4780
4857
static int regulator_init_coupling (struct regulator_dev * rdev )
4781
4858
{
4782
- int n_phandles ;
4859
+ int err , n_phandles ;
4860
+ size_t alloc_size ;
4783
4861
4784
4862
if (!IS_ENABLED (CONFIG_OF ))
4785
4863
n_phandles = 0 ;
4786
4864
else
4787
4865
n_phandles = of_get_n_coupled (rdev );
4788
4866
4789
- if (n_phandles + 1 > MAX_COUPLED ) {
4790
- rdev_err (rdev , "too many regulators coupled\n" );
4791
- return - EPERM ;
4792
- }
4867
+ alloc_size = sizeof (* rdev ) * (n_phandles + 1 );
4868
+
4869
+ rdev -> coupling_desc .coupled_rdevs = kzalloc (alloc_size , GFP_KERNEL );
4870
+ if (!rdev -> coupling_desc .coupled_rdevs )
4871
+ return - ENOMEM ;
4793
4872
4794
4873
/*
4795
4874
* Every regulator should always have coupling descriptor filled with
@@ -4803,23 +4882,35 @@ static int regulator_init_coupling(struct regulator_dev *rdev)
4803
4882
if (n_phandles == 0 )
4804
4883
return 0 ;
4805
4884
4806
- /* regulator, which can't change its voltage, can't be coupled */
4807
- if (!regulator_ops_is_valid (rdev , REGULATOR_CHANGE_VOLTAGE )) {
4808
- rdev_err (rdev , "voltage operation not allowed\n" );
4885
+ if (!of_check_coupling_data (rdev ))
4809
4886
return - EPERM ;
4810
- }
4811
4887
4812
- if (rdev -> constraints -> max_spread <= 0 ) {
4813
- rdev_err (rdev , "wrong max_spread value\n" );
4814
- return - EPERM ;
4888
+ rdev -> coupling_desc .coupler = regulator_find_coupler (rdev );
4889
+ if (IS_ERR (rdev -> coupling_desc .coupler )) {
4890
+ err = PTR_ERR (rdev -> coupling_desc .coupler );
4891
+ rdev_err (rdev , "failed to get coupler: %d\n" , err );
4892
+ return err ;
4815
4893
}
4816
4894
4817
- if (!of_check_coupling_data (rdev ))
4895
+ return 0 ;
4896
+ }
4897
+
4898
+ static int generic_coupler_attach (struct regulator_coupler * coupler ,
4899
+ struct regulator_dev * rdev )
4900
+ {
4901
+ if (rdev -> coupling_desc .n_coupled > 2 ) {
4902
+ rdev_err (rdev ,
4903
+ "Voltage balancing for multiple regulator couples is unimplemented\n" );
4818
4904
return - EPERM ;
4905
+ }
4819
4906
4820
4907
return 0 ;
4821
4908
}
4822
4909
4910
+ static struct regulator_coupler generic_regulator_coupler = {
4911
+ .attach_regulator = generic_coupler_attach ,
4912
+ };
4913
+
4823
4914
/**
4824
4915
* regulator_register - register regulator
4825
4916
* @regulator_desc: regulator to register
@@ -4981,7 +5072,9 @@ regulator_register(const struct regulator_desc *regulator_desc,
4981
5072
if (ret < 0 )
4982
5073
goto wash ;
4983
5074
5075
+ mutex_lock (& regulator_list_mutex );
4984
5076
ret = regulator_init_coupling (rdev );
5077
+ mutex_unlock (& regulator_list_mutex );
4985
5078
if (ret < 0 )
4986
5079
goto wash ;
4987
5080
@@ -5030,6 +5123,7 @@ regulator_register(const struct regulator_desc *regulator_desc,
5030
5123
unset_supplies :
5031
5124
mutex_lock (& regulator_list_mutex );
5032
5125
unset_regulator_supplies (rdev );
5126
+ regulator_remove_coupling (rdev );
5033
5127
mutex_unlock (& regulator_list_mutex );
5034
5128
wash :
5035
5129
kfree (rdev -> constraints );
@@ -5485,6 +5579,8 @@ static int __init regulator_init(void)
5485
5579
#endif
5486
5580
regulator_dummy_init ();
5487
5581
5582
+ regulator_coupler_register (& generic_regulator_coupler );
5583
+
5488
5584
return ret ;
5489
5585
}
5490
5586
0 commit comments