18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Voltage regulators coupler for NVIDIA Tegra20
48c2ecf20Sopenharmony_ci * Copyright (C) 2019 GRATE-DRIVER project
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Voltage constraints borrowed from downstream kernel sources
78c2ecf20Sopenharmony_ci * Copyright (C) 2010-2011 NVIDIA Corporation
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define pr_fmt(fmt)	"tegra voltage-coupler: " fmt
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include <linux/regulator/coupler.h>
168c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
178c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct tegra_regulator_coupler {
208c2ecf20Sopenharmony_ci	struct regulator_coupler coupler;
218c2ecf20Sopenharmony_ci	struct regulator_dev *core_rdev;
228c2ecf20Sopenharmony_ci	struct regulator_dev *cpu_rdev;
238c2ecf20Sopenharmony_ci	struct regulator_dev *rtc_rdev;
248c2ecf20Sopenharmony_ci	int core_min_uV;
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic inline struct tegra_regulator_coupler *
288c2ecf20Sopenharmony_cito_tegra_coupler(struct regulator_coupler *coupler)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	return container_of(coupler, struct tegra_regulator_coupler, coupler);
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic int tegra20_core_limit(struct tegra_regulator_coupler *tegra,
348c2ecf20Sopenharmony_ci			      struct regulator_dev *core_rdev)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	int core_min_uV = 0;
378c2ecf20Sopenharmony_ci	int core_max_uV;
388c2ecf20Sopenharmony_ci	int core_cur_uV;
398c2ecf20Sopenharmony_ci	int err;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	if (tegra->core_min_uV > 0)
428c2ecf20Sopenharmony_ci		return tegra->core_min_uV;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	core_cur_uV = regulator_get_voltage_rdev(core_rdev);
458c2ecf20Sopenharmony_ci	if (core_cur_uV < 0)
468c2ecf20Sopenharmony_ci		return core_cur_uV;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	core_max_uV = max(core_cur_uV, 1200000);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	err = regulator_check_voltage(core_rdev, &core_min_uV, &core_max_uV);
518c2ecf20Sopenharmony_ci	if (err)
528c2ecf20Sopenharmony_ci		return err;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	/*
558c2ecf20Sopenharmony_ci	 * Limit minimum CORE voltage to a value left from bootloader or,
568c2ecf20Sopenharmony_ci	 * if it's unreasonably low value, to the most common 1.2v or to
578c2ecf20Sopenharmony_ci	 * whatever maximum value defined via board's device-tree.
588c2ecf20Sopenharmony_ci	 */
598c2ecf20Sopenharmony_ci	tegra->core_min_uV = core_max_uV;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	pr_info("core minimum voltage limited to %duV\n", tegra->core_min_uV);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return tegra->core_min_uV;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int tegra20_core_rtc_max_spread(struct regulator_dev *core_rdev,
678c2ecf20Sopenharmony_ci				       struct regulator_dev *rtc_rdev)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct coupling_desc *c_desc = &core_rdev->coupling_desc;
708c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
718c2ecf20Sopenharmony_ci	int max_spread;
728c2ecf20Sopenharmony_ci	unsigned int i;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	for (i = 1; i < c_desc->n_coupled; i++) {
758c2ecf20Sopenharmony_ci		max_spread = core_rdev->constraints->max_spread[i - 1];
768c2ecf20Sopenharmony_ci		rdev = c_desc->coupled_rdevs[i];
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci		if (rdev == rtc_rdev && max_spread)
798c2ecf20Sopenharmony_ci			return max_spread;
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	pr_err_once("rtc-core max-spread is undefined in device-tree\n");
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return 150000;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int tegra20_core_rtc_update(struct tegra_regulator_coupler *tegra,
888c2ecf20Sopenharmony_ci				   struct regulator_dev *core_rdev,
898c2ecf20Sopenharmony_ci				   struct regulator_dev *rtc_rdev,
908c2ecf20Sopenharmony_ci				   int cpu_uV, int cpu_min_uV)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	int core_min_uV, core_max_uV = INT_MAX;
938c2ecf20Sopenharmony_ci	int rtc_min_uV, rtc_max_uV = INT_MAX;
948c2ecf20Sopenharmony_ci	int core_target_uV;
958c2ecf20Sopenharmony_ci	int rtc_target_uV;
968c2ecf20Sopenharmony_ci	int max_spread;
978c2ecf20Sopenharmony_ci	int core_uV;
988c2ecf20Sopenharmony_ci	int rtc_uV;
998c2ecf20Sopenharmony_ci	int err;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/*
1028c2ecf20Sopenharmony_ci	 * RTC and CORE voltages should be no more than 170mV from each other,
1038c2ecf20Sopenharmony_ci	 * CPU should be below RTC and CORE by at least 120mV. This applies
1048c2ecf20Sopenharmony_ci	 * to all Tegra20 SoC's.
1058c2ecf20Sopenharmony_ci	 */
1068c2ecf20Sopenharmony_ci	max_spread = tegra20_core_rtc_max_spread(core_rdev, rtc_rdev);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/*
1098c2ecf20Sopenharmony_ci	 * The core voltage scaling is currently not hooked up in drivers,
1108c2ecf20Sopenharmony_ci	 * hence we will limit the minimum core voltage to a reasonable value.
1118c2ecf20Sopenharmony_ci	 * This should be good enough for the time being.
1128c2ecf20Sopenharmony_ci	 */
1138c2ecf20Sopenharmony_ci	core_min_uV = tegra20_core_limit(tegra, core_rdev);
1148c2ecf20Sopenharmony_ci	if (core_min_uV < 0)
1158c2ecf20Sopenharmony_ci		return core_min_uV;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	err = regulator_check_voltage(core_rdev, &core_min_uV, &core_max_uV);
1188c2ecf20Sopenharmony_ci	if (err)
1198c2ecf20Sopenharmony_ci		return err;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	err = regulator_check_consumers(core_rdev, &core_min_uV, &core_max_uV,
1228c2ecf20Sopenharmony_ci					PM_SUSPEND_ON);
1238c2ecf20Sopenharmony_ci	if (err)
1248c2ecf20Sopenharmony_ci		return err;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	core_uV = regulator_get_voltage_rdev(core_rdev);
1278c2ecf20Sopenharmony_ci	if (core_uV < 0)
1288c2ecf20Sopenharmony_ci		return core_uV;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	core_min_uV = max(cpu_min_uV + 125000, core_min_uV);
1318c2ecf20Sopenharmony_ci	if (core_min_uV > core_max_uV)
1328c2ecf20Sopenharmony_ci		return -EINVAL;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	if (cpu_uV + 120000 > core_uV)
1358c2ecf20Sopenharmony_ci		pr_err("core-cpu voltage constraint violated: %d %d\n",
1368c2ecf20Sopenharmony_ci		       core_uV, cpu_uV + 120000);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	rtc_uV = regulator_get_voltage_rdev(rtc_rdev);
1398c2ecf20Sopenharmony_ci	if (rtc_uV < 0)
1408c2ecf20Sopenharmony_ci		return rtc_uV;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (cpu_uV + 120000 > rtc_uV)
1438c2ecf20Sopenharmony_ci		pr_err("rtc-cpu voltage constraint violated: %d %d\n",
1448c2ecf20Sopenharmony_ci		       rtc_uV, cpu_uV + 120000);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	if (abs(core_uV - rtc_uV) > 170000)
1478c2ecf20Sopenharmony_ci		pr_err("core-rtc voltage constraint violated: %d %d\n",
1488c2ecf20Sopenharmony_ci		       core_uV, rtc_uV);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	rtc_min_uV = max(cpu_min_uV + 125000, core_min_uV - max_spread);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	err = regulator_check_voltage(rtc_rdev, &rtc_min_uV, &rtc_max_uV);
1538c2ecf20Sopenharmony_ci	if (err)
1548c2ecf20Sopenharmony_ci		return err;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	while (core_uV != core_min_uV || rtc_uV != rtc_min_uV) {
1578c2ecf20Sopenharmony_ci		if (core_uV < core_min_uV) {
1588c2ecf20Sopenharmony_ci			core_target_uV = min(core_uV + max_spread, core_min_uV);
1598c2ecf20Sopenharmony_ci			core_target_uV = min(rtc_uV + max_spread, core_target_uV);
1608c2ecf20Sopenharmony_ci		} else {
1618c2ecf20Sopenharmony_ci			core_target_uV = max(core_uV - max_spread, core_min_uV);
1628c2ecf20Sopenharmony_ci			core_target_uV = max(rtc_uV - max_spread, core_target_uV);
1638c2ecf20Sopenharmony_ci		}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		if (core_uV == core_target_uV)
1668c2ecf20Sopenharmony_ci			goto update_rtc;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		err = regulator_set_voltage_rdev(core_rdev,
1698c2ecf20Sopenharmony_ci						 core_target_uV,
1708c2ecf20Sopenharmony_ci						 core_max_uV,
1718c2ecf20Sopenharmony_ci						 PM_SUSPEND_ON);
1728c2ecf20Sopenharmony_ci		if (err)
1738c2ecf20Sopenharmony_ci			return err;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		core_uV = core_target_uV;
1768c2ecf20Sopenharmony_ciupdate_rtc:
1778c2ecf20Sopenharmony_ci		if (rtc_uV < rtc_min_uV) {
1788c2ecf20Sopenharmony_ci			rtc_target_uV = min(rtc_uV + max_spread, rtc_min_uV);
1798c2ecf20Sopenharmony_ci			rtc_target_uV = min(core_uV + max_spread, rtc_target_uV);
1808c2ecf20Sopenharmony_ci		} else {
1818c2ecf20Sopenharmony_ci			rtc_target_uV = max(rtc_uV - max_spread, rtc_min_uV);
1828c2ecf20Sopenharmony_ci			rtc_target_uV = max(core_uV - max_spread, rtc_target_uV);
1838c2ecf20Sopenharmony_ci		}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci		if (rtc_uV == rtc_target_uV)
1868c2ecf20Sopenharmony_ci			continue;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci		err = regulator_set_voltage_rdev(rtc_rdev,
1898c2ecf20Sopenharmony_ci						 rtc_target_uV,
1908c2ecf20Sopenharmony_ci						 rtc_max_uV,
1918c2ecf20Sopenharmony_ci						 PM_SUSPEND_ON);
1928c2ecf20Sopenharmony_ci		if (err)
1938c2ecf20Sopenharmony_ci			return err;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		rtc_uV = rtc_target_uV;
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	return 0;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int tegra20_core_voltage_update(struct tegra_regulator_coupler *tegra,
2028c2ecf20Sopenharmony_ci				       struct regulator_dev *cpu_rdev,
2038c2ecf20Sopenharmony_ci				       struct regulator_dev *core_rdev,
2048c2ecf20Sopenharmony_ci				       struct regulator_dev *rtc_rdev)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	int cpu_uV;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	cpu_uV = regulator_get_voltage_rdev(cpu_rdev);
2098c2ecf20Sopenharmony_ci	if (cpu_uV < 0)
2108c2ecf20Sopenharmony_ci		return cpu_uV;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return tegra20_core_rtc_update(tegra, core_rdev, rtc_rdev,
2138c2ecf20Sopenharmony_ci				       cpu_uV, cpu_uV);
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic int tegra20_cpu_voltage_update(struct tegra_regulator_coupler *tegra,
2178c2ecf20Sopenharmony_ci				      struct regulator_dev *cpu_rdev,
2188c2ecf20Sopenharmony_ci				      struct regulator_dev *core_rdev,
2198c2ecf20Sopenharmony_ci				      struct regulator_dev *rtc_rdev)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	int cpu_min_uV_consumers = 0;
2228c2ecf20Sopenharmony_ci	int cpu_max_uV = INT_MAX;
2238c2ecf20Sopenharmony_ci	int cpu_min_uV = 0;
2248c2ecf20Sopenharmony_ci	int cpu_uV;
2258c2ecf20Sopenharmony_ci	int err;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	err = regulator_check_voltage(cpu_rdev, &cpu_min_uV, &cpu_max_uV);
2288c2ecf20Sopenharmony_ci	if (err)
2298c2ecf20Sopenharmony_ci		return err;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	err = regulator_check_consumers(cpu_rdev, &cpu_min_uV, &cpu_max_uV,
2328c2ecf20Sopenharmony_ci					PM_SUSPEND_ON);
2338c2ecf20Sopenharmony_ci	if (err)
2348c2ecf20Sopenharmony_ci		return err;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	err = regulator_check_consumers(cpu_rdev, &cpu_min_uV_consumers,
2378c2ecf20Sopenharmony_ci					&cpu_max_uV, PM_SUSPEND_ON);
2388c2ecf20Sopenharmony_ci	if (err)
2398c2ecf20Sopenharmony_ci		return err;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	cpu_uV = regulator_get_voltage_rdev(cpu_rdev);
2428c2ecf20Sopenharmony_ci	if (cpu_uV < 0)
2438c2ecf20Sopenharmony_ci		return cpu_uV;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	/*
2468c2ecf20Sopenharmony_ci	 * CPU's regulator may not have any consumers, hence the voltage
2478c2ecf20Sopenharmony_ci	 * must not be changed in that case because CPU simply won't
2488c2ecf20Sopenharmony_ci	 * survive the voltage drop if it's running on a higher frequency.
2498c2ecf20Sopenharmony_ci	 */
2508c2ecf20Sopenharmony_ci	if (!cpu_min_uV_consumers)
2518c2ecf20Sopenharmony_ci		cpu_min_uV = cpu_uV;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (cpu_min_uV > cpu_uV) {
2548c2ecf20Sopenharmony_ci		err = tegra20_core_rtc_update(tegra, core_rdev, rtc_rdev,
2558c2ecf20Sopenharmony_ci					      cpu_uV, cpu_min_uV);
2568c2ecf20Sopenharmony_ci		if (err)
2578c2ecf20Sopenharmony_ci			return err;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci		err = regulator_set_voltage_rdev(cpu_rdev, cpu_min_uV,
2608c2ecf20Sopenharmony_ci						 cpu_max_uV, PM_SUSPEND_ON);
2618c2ecf20Sopenharmony_ci		if (err)
2628c2ecf20Sopenharmony_ci			return err;
2638c2ecf20Sopenharmony_ci	} else if (cpu_min_uV < cpu_uV)  {
2648c2ecf20Sopenharmony_ci		err = regulator_set_voltage_rdev(cpu_rdev, cpu_min_uV,
2658c2ecf20Sopenharmony_ci						 cpu_max_uV, PM_SUSPEND_ON);
2668c2ecf20Sopenharmony_ci		if (err)
2678c2ecf20Sopenharmony_ci			return err;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci		err = tegra20_core_rtc_update(tegra, core_rdev, rtc_rdev,
2708c2ecf20Sopenharmony_ci					      cpu_uV, cpu_min_uV);
2718c2ecf20Sopenharmony_ci		if (err)
2728c2ecf20Sopenharmony_ci			return err;
2738c2ecf20Sopenharmony_ci	}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	return 0;
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic int tegra20_regulator_balance_voltage(struct regulator_coupler *coupler,
2798c2ecf20Sopenharmony_ci					     struct regulator_dev *rdev,
2808c2ecf20Sopenharmony_ci					     suspend_state_t state)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
2838c2ecf20Sopenharmony_ci	struct regulator_dev *core_rdev = tegra->core_rdev;
2848c2ecf20Sopenharmony_ci	struct regulator_dev *cpu_rdev = tegra->cpu_rdev;
2858c2ecf20Sopenharmony_ci	struct regulator_dev *rtc_rdev = tegra->rtc_rdev;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if ((core_rdev != rdev && cpu_rdev != rdev && rtc_rdev != rdev) ||
2888c2ecf20Sopenharmony_ci	    state != PM_SUSPEND_ON) {
2898c2ecf20Sopenharmony_ci		pr_err("regulators are not coupled properly\n");
2908c2ecf20Sopenharmony_ci		return -EINVAL;
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (rdev == cpu_rdev)
2948c2ecf20Sopenharmony_ci		return tegra20_cpu_voltage_update(tegra, cpu_rdev,
2958c2ecf20Sopenharmony_ci						  core_rdev, rtc_rdev);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if (rdev == core_rdev)
2988c2ecf20Sopenharmony_ci		return tegra20_core_voltage_update(tegra, cpu_rdev,
2998c2ecf20Sopenharmony_ci						   core_rdev, rtc_rdev);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	pr_err("changing %s voltage not permitted\n", rdev_get_name(rtc_rdev));
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	return -EPERM;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic int tegra20_regulator_attach(struct regulator_coupler *coupler,
3078c2ecf20Sopenharmony_ci				    struct regulator_dev *rdev)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
3108c2ecf20Sopenharmony_ci	struct device_node *np = rdev->dev.of_node;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "nvidia,tegra-core-regulator") &&
3138c2ecf20Sopenharmony_ci	    !tegra->core_rdev) {
3148c2ecf20Sopenharmony_ci		tegra->core_rdev = rdev;
3158c2ecf20Sopenharmony_ci		return 0;
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "nvidia,tegra-rtc-regulator") &&
3198c2ecf20Sopenharmony_ci	    !tegra->rtc_rdev) {
3208c2ecf20Sopenharmony_ci		tegra->rtc_rdev = rdev;
3218c2ecf20Sopenharmony_ci		return 0;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "nvidia,tegra-cpu-regulator") &&
3258c2ecf20Sopenharmony_ci	    !tegra->cpu_rdev) {
3268c2ecf20Sopenharmony_ci		tegra->cpu_rdev = rdev;
3278c2ecf20Sopenharmony_ci		return 0;
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	return -EINVAL;
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic int tegra20_regulator_detach(struct regulator_coupler *coupler,
3348c2ecf20Sopenharmony_ci				    struct regulator_dev *rdev)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	struct tegra_regulator_coupler *tegra = to_tegra_coupler(coupler);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	if (tegra->core_rdev == rdev) {
3398c2ecf20Sopenharmony_ci		tegra->core_rdev = NULL;
3408c2ecf20Sopenharmony_ci		return 0;
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (tegra->rtc_rdev == rdev) {
3448c2ecf20Sopenharmony_ci		tegra->rtc_rdev = NULL;
3458c2ecf20Sopenharmony_ci		return 0;
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (tegra->cpu_rdev == rdev) {
3498c2ecf20Sopenharmony_ci		tegra->cpu_rdev = NULL;
3508c2ecf20Sopenharmony_ci		return 0;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return -EINVAL;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic struct tegra_regulator_coupler tegra20_coupler = {
3578c2ecf20Sopenharmony_ci	.coupler = {
3588c2ecf20Sopenharmony_ci		.attach_regulator = tegra20_regulator_attach,
3598c2ecf20Sopenharmony_ci		.detach_regulator = tegra20_regulator_detach,
3608c2ecf20Sopenharmony_ci		.balance_voltage = tegra20_regulator_balance_voltage,
3618c2ecf20Sopenharmony_ci	},
3628c2ecf20Sopenharmony_ci};
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic int __init tegra_regulator_coupler_init(void)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	if (!of_machine_is_compatible("nvidia,tegra20"))
3678c2ecf20Sopenharmony_ci		return 0;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	return regulator_coupler_register(&tegra20_coupler.coupler);
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ciarch_initcall(tegra_regulator_coupler_init);
372