Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
0004  *        http://www.samsung.com/
0005  * Author: Marek Szyprowski <m.szyprowski@samsung.com>
0006  *
0007  * Simplified generic voltage coupler from regulator core.c
0008  * The main difference is that it keeps current regulator voltage
0009  * if consumers didn't apply their constraints yet.
0010  */
0011 
0012 #include <linux/init.h>
0013 #include <linux/kernel.h>
0014 #include <linux/of.h>
0015 #include <linux/regulator/coupler.h>
0016 #include <linux/regulator/driver.h>
0017 #include <linux/regulator/machine.h>
0018 
0019 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
0020                      int *current_uV,
0021                      int *min_uV, int *max_uV,
0022                      suspend_state_t state)
0023 {
0024     struct coupling_desc *c_desc = &rdev->coupling_desc;
0025     struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
0026     struct regulation_constraints *constraints = rdev->constraints;
0027     int desired_min_uV = 0, desired_max_uV = INT_MAX;
0028     int max_current_uV = 0, min_current_uV = INT_MAX;
0029     int highest_min_uV = 0, target_uV, possible_uV;
0030     int i, ret, max_spread, n_coupled = c_desc->n_coupled;
0031     bool done;
0032 
0033     *current_uV = -1;
0034 
0035     /* Find highest min desired voltage */
0036     for (i = 0; i < n_coupled; i++) {
0037         int tmp_min = 0;
0038         int tmp_max = INT_MAX;
0039 
0040         lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
0041 
0042         ret = regulator_check_consumers(c_rdevs[i],
0043                         &tmp_min,
0044                         &tmp_max, state);
0045         if (ret < 0)
0046             return ret;
0047 
0048         if (tmp_min == 0) {
0049             ret = regulator_get_voltage_rdev(c_rdevs[i]);
0050             if (ret < 0)
0051                 return ret;
0052             tmp_min = ret;
0053         }
0054 
0055         /* apply constraints */
0056         ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
0057         if (ret < 0)
0058             return ret;
0059 
0060         highest_min_uV = max(highest_min_uV, tmp_min);
0061 
0062         if (i == 0) {
0063             desired_min_uV = tmp_min;
0064             desired_max_uV = tmp_max;
0065         }
0066     }
0067 
0068     max_spread = constraints->max_spread[0];
0069 
0070     /*
0071      * Let target_uV be equal to the desired one if possible.
0072      * If not, set it to minimum voltage, allowed by other coupled
0073      * regulators.
0074      */
0075     target_uV = max(desired_min_uV, highest_min_uV - max_spread);
0076 
0077     /*
0078      * Find min and max voltages, which currently aren't violating
0079      * max_spread.
0080      */
0081     for (i = 1; i < n_coupled; i++) {
0082         int tmp_act;
0083 
0084         tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
0085         if (tmp_act < 0)
0086             return tmp_act;
0087 
0088         min_current_uV = min(tmp_act, min_current_uV);
0089         max_current_uV = max(tmp_act, max_current_uV);
0090     }
0091 
0092     /*
0093      * Correct target voltage, so as it currently isn't
0094      * violating max_spread
0095      */
0096     possible_uV = max(target_uV, max_current_uV - max_spread);
0097     possible_uV = min(possible_uV, min_current_uV + max_spread);
0098 
0099     if (possible_uV > desired_max_uV)
0100         return -EINVAL;
0101 
0102     done = (possible_uV == target_uV);
0103     desired_min_uV = possible_uV;
0104 
0105     /* Set current_uV if wasn't done earlier in the code and if necessary */
0106     if (*current_uV == -1) {
0107         ret = regulator_get_voltage_rdev(rdev);
0108         if (ret < 0)
0109             return ret;
0110         *current_uV = ret;
0111     }
0112 
0113     *min_uV = desired_min_uV;
0114     *max_uV = desired_max_uV;
0115 
0116     return done;
0117 }
0118 
0119 static int exynos_coupler_balance_voltage(struct regulator_coupler *coupler,
0120                       struct regulator_dev *rdev,
0121                       suspend_state_t state)
0122 {
0123     struct regulator_dev **c_rdevs;
0124     struct regulator_dev *best_rdev;
0125     struct coupling_desc *c_desc = &rdev->coupling_desc;
0126     int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
0127     unsigned int delta, best_delta;
0128     unsigned long c_rdev_done = 0;
0129     bool best_c_rdev_done;
0130 
0131     c_rdevs = c_desc->coupled_rdevs;
0132     n_coupled = c_desc->n_coupled;
0133 
0134     /*
0135      * Find the best possible voltage change on each loop. Leave the loop
0136      * if there isn't any possible change.
0137      */
0138     do {
0139         best_c_rdev_done = false;
0140         best_delta = 0;
0141         best_min_uV = 0;
0142         best_max_uV = 0;
0143         best_c_rdev = 0;
0144         best_rdev = NULL;
0145 
0146         /*
0147          * Find highest difference between optimal voltage
0148          * and current voltage.
0149          */
0150         for (i = 0; i < n_coupled; i++) {
0151             /*
0152              * optimal_uV is the best voltage that can be set for
0153              * i-th regulator at the moment without violating
0154              * max_spread constraint in order to balance
0155              * the coupled voltages.
0156              */
0157             int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
0158 
0159             if (test_bit(i, &c_rdev_done))
0160                 continue;
0161 
0162             ret = regulator_get_optimal_voltage(c_rdevs[i],
0163                                 &current_uV,
0164                                 &optimal_uV,
0165                                 &optimal_max_uV,
0166                                 state);
0167             if (ret < 0)
0168                 goto out;
0169 
0170             delta = abs(optimal_uV - current_uV);
0171 
0172             if (delta && best_delta <= delta) {
0173                 best_c_rdev_done = ret;
0174                 best_delta = delta;
0175                 best_rdev = c_rdevs[i];
0176                 best_min_uV = optimal_uV;
0177                 best_max_uV = optimal_max_uV;
0178                 best_c_rdev = i;
0179             }
0180         }
0181 
0182         /* Nothing to change, return successfully */
0183         if (!best_rdev) {
0184             ret = 0;
0185             goto out;
0186         }
0187 
0188         ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
0189                          best_max_uV, state);
0190 
0191         if (ret < 0)
0192             goto out;
0193 
0194         if (best_c_rdev_done)
0195             set_bit(best_c_rdev, &c_rdev_done);
0196 
0197     } while (n_coupled > 1);
0198 
0199 out:
0200     return ret;
0201 }
0202 
0203 static int exynos_coupler_attach(struct regulator_coupler *coupler,
0204                  struct regulator_dev *rdev)
0205 {
0206     return 0;
0207 }
0208 
0209 static struct regulator_coupler exynos_coupler = {
0210     .attach_regulator = exynos_coupler_attach,
0211     .balance_voltage  = exynos_coupler_balance_voltage,
0212 };
0213 
0214 static int __init exynos_coupler_init(void)
0215 {
0216     if (!of_machine_is_compatible("samsung,exynos5800"))
0217         return 0;
0218 
0219     return regulator_coupler_register(&exynos_coupler);
0220 }
0221 arch_initcall(exynos_coupler_init);