xref: /openbmc/linux/drivers/opp/ti-opp-supply.c (revision 622fecbccfe86a8052dea6b512d3821dcce29994)
19a835fa6SDave Gerlach // SPDX-License-Identifier: GPL-2.0
29a835fa6SDave Gerlach /*
39a835fa6SDave Gerlach  * Copyright (C) 2016-2017 Texas Instruments Incorporated - http://www.ti.com/
49a835fa6SDave Gerlach  *	Nishanth Menon <nm@ti.com>
59a835fa6SDave Gerlach  *	Dave Gerlach <d-gerlach@ti.com>
69a835fa6SDave Gerlach  *
79a835fa6SDave Gerlach  * TI OPP supply driver that provides override into the regulator control
89a835fa6SDave Gerlach  * for generic opp core to handle devices with ABB regulator and/or
99a835fa6SDave Gerlach  * SmartReflex Class0.
109a835fa6SDave Gerlach  */
119a835fa6SDave Gerlach #include <linux/clk.h>
129a835fa6SDave Gerlach #include <linux/cpufreq.h>
139a835fa6SDave Gerlach #include <linux/device.h>
149a835fa6SDave Gerlach #include <linux/io.h>
159a835fa6SDave Gerlach #include <linux/module.h>
169a835fa6SDave Gerlach #include <linux/notifier.h>
179a835fa6SDave Gerlach #include <linux/of_device.h>
189a835fa6SDave Gerlach #include <linux/of.h>
199a835fa6SDave Gerlach #include <linux/platform_device.h>
209a835fa6SDave Gerlach #include <linux/pm_opp.h>
219a835fa6SDave Gerlach #include <linux/regulator/consumer.h>
229a835fa6SDave Gerlach #include <linux/slab.h>
239a835fa6SDave Gerlach 
249a835fa6SDave Gerlach /**
259a835fa6SDave Gerlach  * struct ti_opp_supply_optimum_voltage_table - optimized voltage table
269a835fa6SDave Gerlach  * @reference_uv:	reference voltage (usually Nominal voltage)
279a835fa6SDave Gerlach  * @optimized_uv:	Optimized voltage from efuse
289a835fa6SDave Gerlach  */
299a835fa6SDave Gerlach struct ti_opp_supply_optimum_voltage_table {
309a835fa6SDave Gerlach 	unsigned int reference_uv;
319a835fa6SDave Gerlach 	unsigned int optimized_uv;
329a835fa6SDave Gerlach };
339a835fa6SDave Gerlach 
349a835fa6SDave Gerlach /**
359a835fa6SDave Gerlach  * struct ti_opp_supply_data - OMAP specific opp supply data
369a835fa6SDave Gerlach  * @vdd_table:	Optimized voltage mapping table
379a835fa6SDave Gerlach  * @num_vdd_table: number of entries in vdd_table
389a835fa6SDave Gerlach  * @vdd_absolute_max_voltage_uv: absolute maximum voltage in UV for the supply
399a835fa6SDave Gerlach  */
409a835fa6SDave Gerlach struct ti_opp_supply_data {
419a835fa6SDave Gerlach 	struct ti_opp_supply_optimum_voltage_table *vdd_table;
429a835fa6SDave Gerlach 	u32 num_vdd_table;
439a835fa6SDave Gerlach 	u32 vdd_absolute_max_voltage_uv;
449a835fa6SDave Gerlach };
459a835fa6SDave Gerlach 
469a835fa6SDave Gerlach static struct ti_opp_supply_data opp_data;
479a835fa6SDave Gerlach 
489a835fa6SDave Gerlach /**
499a835fa6SDave Gerlach  * struct ti_opp_supply_of_data - device tree match data
509a835fa6SDave Gerlach  * @flags:	specific type of opp supply
519a835fa6SDave Gerlach  * @efuse_voltage_mask: mask required for efuse register representing voltage
529a835fa6SDave Gerlach  * @efuse_voltage_uv: Are the efuse entries in micro-volts? if not, assume
539a835fa6SDave Gerlach  *		milli-volts.
549a835fa6SDave Gerlach  */
559a835fa6SDave Gerlach struct ti_opp_supply_of_data {
569a835fa6SDave Gerlach #define OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE	BIT(1)
579a835fa6SDave Gerlach #define OPPDM_HAS_NO_ABB			BIT(2)
589a835fa6SDave Gerlach 	const u8 flags;
599a835fa6SDave Gerlach 	const u32 efuse_voltage_mask;
609a835fa6SDave Gerlach 	const bool efuse_voltage_uv;
619a835fa6SDave Gerlach };
629a835fa6SDave Gerlach 
639a835fa6SDave Gerlach /**
649a835fa6SDave Gerlach  * _store_optimized_voltages() - store optimized voltages
659a835fa6SDave Gerlach  * @dev:	ti opp supply device for which we need to store info
669a835fa6SDave Gerlach  * @data:	data specific to the device
679a835fa6SDave Gerlach  *
689a835fa6SDave Gerlach  * Picks up efuse based optimized voltages for VDD unique per device and
699a835fa6SDave Gerlach  * stores it in internal data structure for use during transition requests.
709a835fa6SDave Gerlach  *
719a835fa6SDave Gerlach  * Return: If successful, 0, else appropriate error value.
729a835fa6SDave Gerlach  */
739a835fa6SDave Gerlach static int _store_optimized_voltages(struct device *dev,
749a835fa6SDave Gerlach 				     struct ti_opp_supply_data *data)
759a835fa6SDave Gerlach {
769a835fa6SDave Gerlach 	void __iomem *base;
779a835fa6SDave Gerlach 	struct property *prop;
789a835fa6SDave Gerlach 	struct resource *res;
799a835fa6SDave Gerlach 	const __be32 *val;
809a835fa6SDave Gerlach 	int proplen, i;
819a835fa6SDave Gerlach 	int ret = 0;
829a835fa6SDave Gerlach 	struct ti_opp_supply_optimum_voltage_table *table;
839a835fa6SDave Gerlach 	const struct ti_opp_supply_of_data *of_data = dev_get_drvdata(dev);
849a835fa6SDave Gerlach 
859a835fa6SDave Gerlach 	/* pick up Efuse based voltages */
869a835fa6SDave Gerlach 	res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 0);
879a835fa6SDave Gerlach 	if (!res) {
889a835fa6SDave Gerlach 		dev_err(dev, "Unable to get IO resource\n");
899a835fa6SDave Gerlach 		ret = -ENODEV;
909a835fa6SDave Gerlach 		goto out_map;
919a835fa6SDave Gerlach 	}
929a835fa6SDave Gerlach 
939a835fa6SDave Gerlach 	base = ioremap_nocache(res->start, resource_size(res));
949a835fa6SDave Gerlach 	if (!base) {
959a835fa6SDave Gerlach 		dev_err(dev, "Unable to map Efuse registers\n");
969a835fa6SDave Gerlach 		ret = -ENOMEM;
979a835fa6SDave Gerlach 		goto out_map;
989a835fa6SDave Gerlach 	}
999a835fa6SDave Gerlach 
1009a835fa6SDave Gerlach 	/* Fetch efuse-settings. */
1019a835fa6SDave Gerlach 	prop = of_find_property(dev->of_node, "ti,efuse-settings", NULL);
1029a835fa6SDave Gerlach 	if (!prop) {
1039a835fa6SDave Gerlach 		dev_err(dev, "No 'ti,efuse-settings' property found\n");
1049a835fa6SDave Gerlach 		ret = -EINVAL;
1059a835fa6SDave Gerlach 		goto out;
1069a835fa6SDave Gerlach 	}
1079a835fa6SDave Gerlach 
1089a835fa6SDave Gerlach 	proplen = prop->length / sizeof(int);
1099a835fa6SDave Gerlach 	data->num_vdd_table = proplen / 2;
1109a835fa6SDave Gerlach 	/* Verify for corrupted OPP entries in dt */
1119a835fa6SDave Gerlach 	if (data->num_vdd_table * 2 * sizeof(int) != prop->length) {
1129a835fa6SDave Gerlach 		dev_err(dev, "Invalid 'ti,efuse-settings'\n");
1139a835fa6SDave Gerlach 		ret = -EINVAL;
1149a835fa6SDave Gerlach 		goto out;
1159a835fa6SDave Gerlach 	}
1169a835fa6SDave Gerlach 
1179a835fa6SDave Gerlach 	ret = of_property_read_u32(dev->of_node, "ti,absolute-max-voltage-uv",
1189a835fa6SDave Gerlach 				   &data->vdd_absolute_max_voltage_uv);
1199a835fa6SDave Gerlach 	if (ret) {
1209a835fa6SDave Gerlach 		dev_err(dev, "ti,absolute-max-voltage-uv is missing\n");
1219a835fa6SDave Gerlach 		ret = -EINVAL;
1229a835fa6SDave Gerlach 		goto out;
1239a835fa6SDave Gerlach 	}
1249a835fa6SDave Gerlach 
1256396bb22SKees Cook 	table = kcalloc(data->num_vdd_table, sizeof(*data->vdd_table),
1266396bb22SKees Cook 			GFP_KERNEL);
1279a835fa6SDave Gerlach 	if (!table) {
1289a835fa6SDave Gerlach 		ret = -ENOMEM;
1299a835fa6SDave Gerlach 		goto out;
1309a835fa6SDave Gerlach 	}
1319a835fa6SDave Gerlach 	data->vdd_table = table;
1329a835fa6SDave Gerlach 
1339a835fa6SDave Gerlach 	val = prop->value;
1349a835fa6SDave Gerlach 	for (i = 0; i < data->num_vdd_table; i++, table++) {
1359a835fa6SDave Gerlach 		u32 efuse_offset;
1369a835fa6SDave Gerlach 		u32 tmp;
1379a835fa6SDave Gerlach 
1389a835fa6SDave Gerlach 		table->reference_uv = be32_to_cpup(val++);
1399a835fa6SDave Gerlach 		efuse_offset = be32_to_cpup(val++);
1409a835fa6SDave Gerlach 
1419a835fa6SDave Gerlach 		tmp = readl(base + efuse_offset);
1429a835fa6SDave Gerlach 		tmp &= of_data->efuse_voltage_mask;
1439a835fa6SDave Gerlach 		tmp >>= __ffs(of_data->efuse_voltage_mask);
1449a835fa6SDave Gerlach 
1459a835fa6SDave Gerlach 		table->optimized_uv = of_data->efuse_voltage_uv ? tmp :
1469a835fa6SDave Gerlach 					tmp * 1000;
1479a835fa6SDave Gerlach 
1489a835fa6SDave Gerlach 		dev_dbg(dev, "[%d] efuse=0x%08x volt_table=%d vset=%d\n",
1499a835fa6SDave Gerlach 			i, efuse_offset, table->reference_uv,
1509a835fa6SDave Gerlach 			table->optimized_uv);
1519a835fa6SDave Gerlach 
1529a835fa6SDave Gerlach 		/*
1539a835fa6SDave Gerlach 		 * Some older samples might not have optimized efuse
1549a835fa6SDave Gerlach 		 * Use reference voltage for those - just add debug message
1559a835fa6SDave Gerlach 		 * for them.
1569a835fa6SDave Gerlach 		 */
1579a835fa6SDave Gerlach 		if (!table->optimized_uv) {
1589a835fa6SDave Gerlach 			dev_dbg(dev, "[%d] efuse=0x%08x volt_table=%d:vset0\n",
1599a835fa6SDave Gerlach 				i, efuse_offset, table->reference_uv);
1609a835fa6SDave Gerlach 			table->optimized_uv = table->reference_uv;
1619a835fa6SDave Gerlach 		}
1629a835fa6SDave Gerlach 	}
1639a835fa6SDave Gerlach out:
1649a835fa6SDave Gerlach 	iounmap(base);
1659a835fa6SDave Gerlach out_map:
1669a835fa6SDave Gerlach 	return ret;
1679a835fa6SDave Gerlach }
1689a835fa6SDave Gerlach 
1699a835fa6SDave Gerlach /**
1709a835fa6SDave Gerlach  * _free_optimized_voltages() - free resources for optvoltages
1719a835fa6SDave Gerlach  * @dev:	device for which we need to free info
1729a835fa6SDave Gerlach  * @data:	data specific to the device
1739a835fa6SDave Gerlach  */
1749a835fa6SDave Gerlach static void _free_optimized_voltages(struct device *dev,
1759a835fa6SDave Gerlach 				     struct ti_opp_supply_data *data)
1769a835fa6SDave Gerlach {
1779a835fa6SDave Gerlach 	kfree(data->vdd_table);
1789a835fa6SDave Gerlach 	data->vdd_table = NULL;
1799a835fa6SDave Gerlach 	data->num_vdd_table = 0;
1809a835fa6SDave Gerlach }
1819a835fa6SDave Gerlach 
1829a835fa6SDave Gerlach /**
1839a835fa6SDave Gerlach  * _get_optimal_vdd_voltage() - Finds optimal voltage for the supply
1849a835fa6SDave Gerlach  * @dev:	device for which we need to find info
1859a835fa6SDave Gerlach  * @data:	data specific to the device
1869a835fa6SDave Gerlach  * @reference_uv:	reference voltage (OPP voltage) for which we need value
1879a835fa6SDave Gerlach  *
1889a835fa6SDave Gerlach  * Return: if a match is found, return optimized voltage, else return
1899a835fa6SDave Gerlach  * reference_uv, also return reference_uv if no optimization is needed.
1909a835fa6SDave Gerlach  */
1919a835fa6SDave Gerlach static int _get_optimal_vdd_voltage(struct device *dev,
1929a835fa6SDave Gerlach 				    struct ti_opp_supply_data *data,
1939a835fa6SDave Gerlach 				    int reference_uv)
1949a835fa6SDave Gerlach {
1959a835fa6SDave Gerlach 	int i;
1969a835fa6SDave Gerlach 	struct ti_opp_supply_optimum_voltage_table *table;
1979a835fa6SDave Gerlach 
1989a835fa6SDave Gerlach 	if (!data->num_vdd_table)
1999a835fa6SDave Gerlach 		return reference_uv;
2009a835fa6SDave Gerlach 
2019a835fa6SDave Gerlach 	table = data->vdd_table;
2029a835fa6SDave Gerlach 	if (!table)
2039a835fa6SDave Gerlach 		return -EINVAL;
2049a835fa6SDave Gerlach 
2059a835fa6SDave Gerlach 	/* Find a exact match - this list is usually very small */
2069a835fa6SDave Gerlach 	for (i = 0; i < data->num_vdd_table; i++, table++)
2079a835fa6SDave Gerlach 		if (table->reference_uv == reference_uv)
2089a835fa6SDave Gerlach 			return table->optimized_uv;
2099a835fa6SDave Gerlach 
2109a835fa6SDave Gerlach 	/* IF things are screwed up, we'd make a mess on console.. ratelimit */
2119a835fa6SDave Gerlach 	dev_err_ratelimited(dev, "%s: Failed optimized voltage match for %d\n",
2129a835fa6SDave Gerlach 			    __func__, reference_uv);
2139a835fa6SDave Gerlach 	return reference_uv;
2149a835fa6SDave Gerlach }
2159a835fa6SDave Gerlach 
2169a835fa6SDave Gerlach static int _opp_set_voltage(struct device *dev,
2179a835fa6SDave Gerlach 			    struct dev_pm_opp_supply *supply,
2189a835fa6SDave Gerlach 			    int new_target_uv, struct regulator *reg,
2199a835fa6SDave Gerlach 			    char *reg_name)
2209a835fa6SDave Gerlach {
2219a835fa6SDave Gerlach 	int ret;
2229a835fa6SDave Gerlach 	unsigned long vdd_uv, uv_max;
2239a835fa6SDave Gerlach 
2249a835fa6SDave Gerlach 	if (new_target_uv)
2259a835fa6SDave Gerlach 		vdd_uv = new_target_uv;
2269a835fa6SDave Gerlach 	else
2279a835fa6SDave Gerlach 		vdd_uv = supply->u_volt;
2289a835fa6SDave Gerlach 
2299a835fa6SDave Gerlach 	/*
2309a835fa6SDave Gerlach 	 * If we do have an absolute max voltage specified, then we should
2319a835fa6SDave Gerlach 	 * use that voltage instead to allow for cases where the voltage rails
2329a835fa6SDave Gerlach 	 * are ganged (example if we set the max for an opp as 1.12v, and
2339a835fa6SDave Gerlach 	 * the absolute max is 1.5v, for another rail to get 1.25v, it cannot
2349a835fa6SDave Gerlach 	 * be achieved if the regulator is constrainted to max of 1.12v, even
2359a835fa6SDave Gerlach 	 * if it can function at 1.25v
2369a835fa6SDave Gerlach 	 */
2379a835fa6SDave Gerlach 	if (opp_data.vdd_absolute_max_voltage_uv)
2389a835fa6SDave Gerlach 		uv_max = opp_data.vdd_absolute_max_voltage_uv;
2399a835fa6SDave Gerlach 	else
2409a835fa6SDave Gerlach 		uv_max = supply->u_volt_max;
2419a835fa6SDave Gerlach 
2429a835fa6SDave Gerlach 	if (vdd_uv > uv_max ||
2439a835fa6SDave Gerlach 	    vdd_uv < supply->u_volt_min ||
2449a835fa6SDave Gerlach 	    supply->u_volt_min > uv_max) {
2459a835fa6SDave Gerlach 		dev_warn(dev,
2469a835fa6SDave Gerlach 			 "Invalid range voltages [Min:%lu target:%lu Max:%lu]\n",
2479a835fa6SDave Gerlach 			 supply->u_volt_min, vdd_uv, uv_max);
2489a835fa6SDave Gerlach 		return -EINVAL;
2499a835fa6SDave Gerlach 	}
2509a835fa6SDave Gerlach 
2519a835fa6SDave Gerlach 	dev_dbg(dev, "%s scaling to %luuV[min %luuV max %luuV]\n", reg_name,
2529a835fa6SDave Gerlach 		vdd_uv, supply->u_volt_min,
2539a835fa6SDave Gerlach 		uv_max);
2549a835fa6SDave Gerlach 
2559a835fa6SDave Gerlach 	ret = regulator_set_voltage_triplet(reg,
2569a835fa6SDave Gerlach 					    supply->u_volt_min,
2579a835fa6SDave Gerlach 					    vdd_uv,
2589a835fa6SDave Gerlach 					    uv_max);
2599a835fa6SDave Gerlach 	if (ret) {
2609a835fa6SDave Gerlach 		dev_err(dev, "%s failed for %luuV[min %luuV max %luuV]\n",
2619a835fa6SDave Gerlach 			reg_name, vdd_uv, supply->u_volt_min,
2629a835fa6SDave Gerlach 			uv_max);
2639a835fa6SDave Gerlach 		return ret;
2649a835fa6SDave Gerlach 	}
2659a835fa6SDave Gerlach 
2669a835fa6SDave Gerlach 	return 0;
2679a835fa6SDave Gerlach }
2689a835fa6SDave Gerlach 
2699a835fa6SDave Gerlach /**
2709a835fa6SDave Gerlach  * ti_opp_supply_set_opp() - do the opp supply transition
2719a835fa6SDave Gerlach  * @data:	information on regulators and new and old opps provided by
2729a835fa6SDave Gerlach  *		opp core to use in transition
2739a835fa6SDave Gerlach  *
2749a835fa6SDave Gerlach  * Return: If successful, 0, else appropriate error value.
2759a835fa6SDave Gerlach  */
2763eff5f67SWei Yongjun static int ti_opp_supply_set_opp(struct dev_pm_set_opp_data *data)
2779a835fa6SDave Gerlach {
2789a835fa6SDave Gerlach 	struct dev_pm_opp_supply *old_supply_vdd = &data->old_opp.supplies[0];
2799a835fa6SDave Gerlach 	struct dev_pm_opp_supply *old_supply_vbb = &data->old_opp.supplies[1];
2809a835fa6SDave Gerlach 	struct dev_pm_opp_supply *new_supply_vdd = &data->new_opp.supplies[0];
2819a835fa6SDave Gerlach 	struct dev_pm_opp_supply *new_supply_vbb = &data->new_opp.supplies[1];
2829a835fa6SDave Gerlach 	struct device *dev = data->dev;
2839a835fa6SDave Gerlach 	unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate;
2849a835fa6SDave Gerlach 	struct clk *clk = data->clk;
2859a835fa6SDave Gerlach 	struct regulator *vdd_reg = data->regulators[0];
2869a835fa6SDave Gerlach 	struct regulator *vbb_reg = data->regulators[1];
2879a835fa6SDave Gerlach 	int vdd_uv;
2889a835fa6SDave Gerlach 	int ret;
2899a835fa6SDave Gerlach 
2909a835fa6SDave Gerlach 	vdd_uv = _get_optimal_vdd_voltage(dev, &opp_data,
291*622fecbcSKeerthy 					  new_supply_vdd->u_volt);
2929a835fa6SDave Gerlach 
293ba038546SKeerthy 	if (new_supply_vdd->u_volt_min < vdd_uv)
294ba038546SKeerthy 		new_supply_vdd->u_volt_min = vdd_uv;
295ba038546SKeerthy 
2969a835fa6SDave Gerlach 	/* Scaling up? Scale voltage before frequency */
2979a835fa6SDave Gerlach 	if (freq > old_freq) {
2989a835fa6SDave Gerlach 		ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg,
2999a835fa6SDave Gerlach 				       "vdd");
3009a835fa6SDave Gerlach 		if (ret)
3019a835fa6SDave Gerlach 			goto restore_voltage;
3029a835fa6SDave Gerlach 
3039a835fa6SDave Gerlach 		ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb");
3049a835fa6SDave Gerlach 		if (ret)
3059a835fa6SDave Gerlach 			goto restore_voltage;
3069a835fa6SDave Gerlach 	}
3079a835fa6SDave Gerlach 
3089a835fa6SDave Gerlach 	/* Change frequency */
3099a835fa6SDave Gerlach 	dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n",
3109a835fa6SDave Gerlach 		__func__, old_freq, freq);
3119a835fa6SDave Gerlach 
3129a835fa6SDave Gerlach 	ret = clk_set_rate(clk, freq);
3139a835fa6SDave Gerlach 	if (ret) {
3149a835fa6SDave Gerlach 		dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
3159a835fa6SDave Gerlach 			ret);
3169a835fa6SDave Gerlach 		goto restore_voltage;
3179a835fa6SDave Gerlach 	}
3189a835fa6SDave Gerlach 
3199a835fa6SDave Gerlach 	/* Scaling down? Scale voltage after frequency */
3209a835fa6SDave Gerlach 	if (freq < old_freq) {
3219a835fa6SDave Gerlach 		ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb");
3229a835fa6SDave Gerlach 		if (ret)
3239a835fa6SDave Gerlach 			goto restore_freq;
3249a835fa6SDave Gerlach 
3259a835fa6SDave Gerlach 		ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg,
3269a835fa6SDave Gerlach 				       "vdd");
3279a835fa6SDave Gerlach 		if (ret)
3289a835fa6SDave Gerlach 			goto restore_freq;
3299a835fa6SDave Gerlach 	}
3309a835fa6SDave Gerlach 
3319a835fa6SDave Gerlach 	return 0;
3329a835fa6SDave Gerlach 
3339a835fa6SDave Gerlach restore_freq:
3349a835fa6SDave Gerlach 	ret = clk_set_rate(clk, old_freq);
3359a835fa6SDave Gerlach 	if (ret)
3369a835fa6SDave Gerlach 		dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
3379a835fa6SDave Gerlach 			__func__, old_freq);
3389a835fa6SDave Gerlach restore_voltage:
3399a835fa6SDave Gerlach 	/* This shouldn't harm even if the voltages weren't updated earlier */
3409a835fa6SDave Gerlach 	if (old_supply_vdd->u_volt) {
3419a835fa6SDave Gerlach 		ret = _opp_set_voltage(dev, old_supply_vbb, 0, vbb_reg, "vbb");
3429a835fa6SDave Gerlach 		if (ret)
3439a835fa6SDave Gerlach 			return ret;
3449a835fa6SDave Gerlach 
3459a835fa6SDave Gerlach 		ret = _opp_set_voltage(dev, old_supply_vdd, 0, vdd_reg,
3469a835fa6SDave Gerlach 				       "vdd");
3479a835fa6SDave Gerlach 		if (ret)
3489a835fa6SDave Gerlach 			return ret;
3499a835fa6SDave Gerlach 	}
3509a835fa6SDave Gerlach 
3519a835fa6SDave Gerlach 	return ret;
3529a835fa6SDave Gerlach }
3539a835fa6SDave Gerlach 
3549a835fa6SDave Gerlach static const struct ti_opp_supply_of_data omap_generic_of_data = {
3559a835fa6SDave Gerlach };
3569a835fa6SDave Gerlach 
3579a835fa6SDave Gerlach static const struct ti_opp_supply_of_data omap_omap5_of_data = {
3589a835fa6SDave Gerlach 	.flags = OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE,
3599a835fa6SDave Gerlach 	.efuse_voltage_mask = 0xFFF,
3609a835fa6SDave Gerlach 	.efuse_voltage_uv = false,
3619a835fa6SDave Gerlach };
3629a835fa6SDave Gerlach 
3639a835fa6SDave Gerlach static const struct ti_opp_supply_of_data omap_omap5core_of_data = {
3649a835fa6SDave Gerlach 	.flags = OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE | OPPDM_HAS_NO_ABB,
3659a835fa6SDave Gerlach 	.efuse_voltage_mask = 0xFFF,
3669a835fa6SDave Gerlach 	.efuse_voltage_uv = false,
3679a835fa6SDave Gerlach };
3689a835fa6SDave Gerlach 
3699a835fa6SDave Gerlach static const struct of_device_id ti_opp_supply_of_match[] = {
3709a835fa6SDave Gerlach 	{.compatible = "ti,omap-opp-supply", .data = &omap_generic_of_data},
3719a835fa6SDave Gerlach 	{.compatible = "ti,omap5-opp-supply", .data = &omap_omap5_of_data},
3729a835fa6SDave Gerlach 	{.compatible = "ti,omap5-core-opp-supply",
3739a835fa6SDave Gerlach 	 .data = &omap_omap5core_of_data},
3749a835fa6SDave Gerlach 	{},
3759a835fa6SDave Gerlach };
3769a835fa6SDave Gerlach MODULE_DEVICE_TABLE(of, ti_opp_supply_of_match);
3779a835fa6SDave Gerlach 
3789a835fa6SDave Gerlach static int ti_opp_supply_probe(struct platform_device *pdev)
3799a835fa6SDave Gerlach {
3809a835fa6SDave Gerlach 	struct device *dev = &pdev->dev;
3819a835fa6SDave Gerlach 	struct device *cpu_dev = get_cpu_device(0);
3829a835fa6SDave Gerlach 	const struct of_device_id *match;
3839a835fa6SDave Gerlach 	const struct ti_opp_supply_of_data *of_data;
3849a835fa6SDave Gerlach 	int ret = 0;
3859a835fa6SDave Gerlach 
3869a835fa6SDave Gerlach 	match = of_match_device(ti_opp_supply_of_match, dev);
3879a835fa6SDave Gerlach 	if (!match) {
3889a835fa6SDave Gerlach 		/* We do not expect this to happen */
3899a835fa6SDave Gerlach 		dev_err(dev, "%s: Unable to match device\n", __func__);
3909a835fa6SDave Gerlach 		return -ENODEV;
3919a835fa6SDave Gerlach 	}
3929a835fa6SDave Gerlach 	if (!match->data) {
3939a835fa6SDave Gerlach 		/* Again, unlikely.. but mistakes do happen */
3949a835fa6SDave Gerlach 		dev_err(dev, "%s: Bad data in match\n", __func__);
3959a835fa6SDave Gerlach 		return -EINVAL;
3969a835fa6SDave Gerlach 	}
3979a835fa6SDave Gerlach 	of_data = match->data;
3989a835fa6SDave Gerlach 
3999a835fa6SDave Gerlach 	dev_set_drvdata(dev, (void *)of_data);
4009a835fa6SDave Gerlach 
4019a835fa6SDave Gerlach 	/* If we need optimized voltage */
4029a835fa6SDave Gerlach 	if (of_data->flags & OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE) {
4039a835fa6SDave Gerlach 		ret = _store_optimized_voltages(dev, &opp_data);
4049a835fa6SDave Gerlach 		if (ret)
4059a835fa6SDave Gerlach 			return ret;
4069a835fa6SDave Gerlach 	}
4079a835fa6SDave Gerlach 
4089a835fa6SDave Gerlach 	ret = PTR_ERR_OR_ZERO(dev_pm_opp_register_set_opp_helper(cpu_dev,
4099a835fa6SDave Gerlach 								 ti_opp_supply_set_opp));
4109a835fa6SDave Gerlach 	if (ret)
4119a835fa6SDave Gerlach 		_free_optimized_voltages(dev, &opp_data);
4129a835fa6SDave Gerlach 
4139a835fa6SDave Gerlach 	return ret;
4149a835fa6SDave Gerlach }
4159a835fa6SDave Gerlach 
4169a835fa6SDave Gerlach static struct platform_driver ti_opp_supply_driver = {
4179a835fa6SDave Gerlach 	.probe = ti_opp_supply_probe,
4189a835fa6SDave Gerlach 	.driver = {
4199a835fa6SDave Gerlach 		   .name = "ti_opp_supply",
4209a835fa6SDave Gerlach 		   .owner = THIS_MODULE,
4219a835fa6SDave Gerlach 		   .of_match_table = of_match_ptr(ti_opp_supply_of_match),
4229a835fa6SDave Gerlach 		   },
4239a835fa6SDave Gerlach };
4249a835fa6SDave Gerlach module_platform_driver(ti_opp_supply_driver);
4259a835fa6SDave Gerlach 
4269a835fa6SDave Gerlach MODULE_DESCRIPTION("Texas Instruments OMAP OPP Supply driver");
4279a835fa6SDave Gerlach MODULE_AUTHOR("Texas Instruments Inc.");
4289a835fa6SDave Gerlach MODULE_LICENSE("GPL v2");
429