Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *	      http://www.samsung.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Author: Marek Szyprowski <m.szyprowski@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Simplified generic voltage coupler from regulator core.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * The main difference is that it keeps current regulator voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * if consumers didn't apply their constraints yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/regulator/coupler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/regulator/machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 					 int *current_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 					 int *min_uV, int *max_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 					 suspend_state_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct coupling_desc *c_desc = &rdev->coupling_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct regulation_constraints *constraints = rdev->constraints;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	int desired_min_uV = 0, desired_max_uV = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	int max_current_uV = 0, min_current_uV = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	int highest_min_uV = 0, target_uV, possible_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	int i, ret, max_spread, n_coupled = c_desc->n_coupled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	bool done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	*current_uV = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	/* Find highest min desired voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	for (i = 0; i < n_coupled; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		int tmp_min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		int tmp_max = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		ret = regulator_check_consumers(c_rdevs[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 						&tmp_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 						&tmp_max, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		if (tmp_min == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			ret = regulator_get_voltage_rdev(c_rdevs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			tmp_min = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		/* apply constraints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		highest_min_uV = max(highest_min_uV, tmp_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		if (i == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			desired_min_uV = tmp_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			desired_max_uV = tmp_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	max_spread = constraints->max_spread[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	 * Let target_uV be equal to the desired one if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	 * If not, set it to minimum voltage, allowed by other coupled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	 * regulators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	target_uV = max(desired_min_uV, highest_min_uV - max_spread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	 * Find min and max voltages, which currently aren't violating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	 * max_spread.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	for (i = 1; i < n_coupled; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		int tmp_act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		if (tmp_act < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			return tmp_act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		min_current_uV = min(tmp_act, min_current_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		max_current_uV = max(tmp_act, max_current_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	 * Correct target voltage, so as it currently isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * violating max_spread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	possible_uV = max(target_uV, max_current_uV - max_spread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	possible_uV = min(possible_uV, min_current_uV + max_spread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (possible_uV > desired_max_uV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	done = (possible_uV == target_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	desired_min_uV = possible_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	/* Set current_uV if wasn't done earlier in the code and if necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (*current_uV == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		ret = regulator_get_voltage_rdev(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		*current_uV = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	*min_uV = desired_min_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	*max_uV = desired_max_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int exynos_coupler_balance_voltage(struct regulator_coupler *coupler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					  struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 					  suspend_state_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct regulator_dev **c_rdevs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct regulator_dev *best_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct coupling_desc *c_desc = &rdev->coupling_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	unsigned int delta, best_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	unsigned long c_rdev_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	bool best_c_rdev_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	c_rdevs = c_desc->coupled_rdevs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	n_coupled = c_desc->n_coupled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * Find the best possible voltage change on each loop. Leave the loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * if there isn't any possible change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		best_c_rdev_done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		best_delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		best_min_uV = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		best_max_uV = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		best_c_rdev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		best_rdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		 * Find highest difference between optimal voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		 * and current voltage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		for (i = 0; i < n_coupled; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			 * optimal_uV is the best voltage that can be set for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			 * i-th regulator at the moment without violating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			 * max_spread constraint in order to balance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			 * the coupled voltages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			if (test_bit(i, &c_rdev_done))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			ret = regulator_get_optimal_voltage(c_rdevs[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 							    &current_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 							    &optimal_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 							    &optimal_max_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 							    state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			delta = abs(optimal_uV - current_uV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			if (delta && best_delta <= delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				best_c_rdev_done = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				best_delta = delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				best_rdev = c_rdevs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 				best_min_uV = optimal_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				best_max_uV = optimal_max_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				best_c_rdev = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		/* Nothing to change, return successfully */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		if (!best_rdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 						 best_max_uV, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		if (best_c_rdev_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			set_bit(best_c_rdev, &c_rdev_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	} while (n_coupled > 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int exynos_coupler_attach(struct regulator_coupler *coupler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 				 struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static struct regulator_coupler exynos_coupler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	.attach_regulator = exynos_coupler_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	.balance_voltage  = exynos_coupler_balance_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int __init exynos_coupler_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (!of_machine_is_compatible("samsung,exynos5800"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return regulator_coupler_register(&exynos_coupler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) arch_initcall(exynos_coupler_init);