1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016-2017 Texas Instruments Incorporated - https://www.ti.com/
4 * Nishanth Menon <nm@ti.com>
5 * Dave Gerlach <d-gerlach@ti.com>
7 * TI OPP supply driver that provides override into the regulator control
8 * for generic opp core to handle devices with ABB regulator and/or
11 #include <linux/clk.h>
12 #include <linux/cpufreq.h>
13 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/notifier.h>
17 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_opp.h>
21 #include <linux/property.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
26 * struct ti_opp_supply_optimum_voltage_table - optimized voltage table
27 * @reference_uv: reference voltage (usually Nominal voltage)
28 * @optimized_uv: Optimized voltage from efuse
30 struct ti_opp_supply_optimum_voltage_table {
31 unsigned int reference_uv;
32 unsigned int optimized_uv;
36 * struct ti_opp_supply_data - OMAP specific opp supply data
37 * @vdd_table: Optimized voltage mapping table
38 * @num_vdd_table: number of entries in vdd_table
39 * @vdd_absolute_max_voltage_uv: absolute maximum voltage in UV for the supply
40 * @old_supplies: Placeholder for supplies information for old OPP.
41 * @new_supplies: Placeholder for supplies information for new OPP.
43 struct ti_opp_supply_data {
44 struct ti_opp_supply_optimum_voltage_table *vdd_table;
46 u32 vdd_absolute_max_voltage_uv;
47 struct dev_pm_opp_supply old_supplies[2];
48 struct dev_pm_opp_supply new_supplies[2];
51 static struct ti_opp_supply_data opp_data;
54 * struct ti_opp_supply_of_data - device tree match data
55 * @flags: specific type of opp supply
56 * @efuse_voltage_mask: mask required for efuse register representing voltage
57 * @efuse_voltage_uv: Are the efuse entries in micro-volts? if not, assume
60 struct ti_opp_supply_of_data {
61 #define OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE BIT(1)
62 #define OPPDM_HAS_NO_ABB BIT(2)
64 const u32 efuse_voltage_mask;
65 const bool efuse_voltage_uv;
69 * _store_optimized_voltages() - store optimized voltages
70 * @dev: ti opp supply device for which we need to store info
71 * @data: data specific to the device
73 * Picks up efuse based optimized voltages for VDD unique per device and
74 * stores it in internal data structure for use during transition requests.
76 * Return: If successful, 0, else appropriate error value.
78 static int _store_optimized_voltages(struct device *dev,
79 struct ti_opp_supply_data *data)
82 struct property *prop;
87 struct ti_opp_supply_optimum_voltage_table *table;
88 const struct ti_opp_supply_of_data *of_data = dev_get_drvdata(dev);
90 /* pick up Efuse based voltages */
91 res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 0);
93 dev_err(dev, "Unable to get IO resource\n");
98 base = ioremap(res->start, resource_size(res));
100 dev_err(dev, "Unable to map Efuse registers\n");
105 /* Fetch efuse-settings. */
106 prop = of_find_property(dev->of_node, "ti,efuse-settings", NULL);
108 dev_err(dev, "No 'ti,efuse-settings' property found\n");
113 proplen = prop->length / sizeof(int);
114 data->num_vdd_table = proplen / 2;
115 /* Verify for corrupted OPP entries in dt */
116 if (data->num_vdd_table * 2 * sizeof(int) != prop->length) {
117 dev_err(dev, "Invalid 'ti,efuse-settings'\n");
122 ret = of_property_read_u32(dev->of_node, "ti,absolute-max-voltage-uv",
123 &data->vdd_absolute_max_voltage_uv);
125 dev_err(dev, "ti,absolute-max-voltage-uv is missing\n");
130 table = kcalloc(data->num_vdd_table, sizeof(*data->vdd_table),
136 data->vdd_table = table;
139 for (i = 0; i < data->num_vdd_table; i++, table++) {
143 table->reference_uv = be32_to_cpup(val++);
144 efuse_offset = be32_to_cpup(val++);
146 tmp = readl(base + efuse_offset);
147 tmp &= of_data->efuse_voltage_mask;
148 tmp >>= __ffs(of_data->efuse_voltage_mask);
150 table->optimized_uv = of_data->efuse_voltage_uv ? tmp :
153 dev_dbg(dev, "[%d] efuse=0x%08x volt_table=%d vset=%d\n",
154 i, efuse_offset, table->reference_uv,
155 table->optimized_uv);
158 * Some older samples might not have optimized efuse
159 * Use reference voltage for those - just add debug message
162 if (!table->optimized_uv) {
163 dev_dbg(dev, "[%d] efuse=0x%08x volt_table=%d:vset0\n",
164 i, efuse_offset, table->reference_uv);
165 table->optimized_uv = table->reference_uv;
175 * _free_optimized_voltages() - free resources for optvoltages
176 * @dev: device for which we need to free info
177 * @data: data specific to the device
179 static void _free_optimized_voltages(struct device *dev,
180 struct ti_opp_supply_data *data)
182 kfree(data->vdd_table);
183 data->vdd_table = NULL;
184 data->num_vdd_table = 0;
188 * _get_optimal_vdd_voltage() - Finds optimal voltage for the supply
189 * @dev: device for which we need to find info
190 * @data: data specific to the device
191 * @reference_uv: reference voltage (OPP voltage) for which we need value
193 * Return: if a match is found, return optimized voltage, else return
194 * reference_uv, also return reference_uv if no optimization is needed.
196 static int _get_optimal_vdd_voltage(struct device *dev,
197 struct ti_opp_supply_data *data,
201 struct ti_opp_supply_optimum_voltage_table *table;
203 if (!data->num_vdd_table)
206 table = data->vdd_table;
210 /* Find a exact match - this list is usually very small */
211 for (i = 0; i < data->num_vdd_table; i++, table++)
212 if (table->reference_uv == reference_uv)
213 return table->optimized_uv;
215 /* IF things are screwed up, we'd make a mess on console.. ratelimit */
216 dev_err_ratelimited(dev, "%s: Failed optimized voltage match for %d\n",
217 __func__, reference_uv);
221 static int _opp_set_voltage(struct device *dev,
222 struct dev_pm_opp_supply *supply,
223 int new_target_uv, struct regulator *reg,
227 unsigned long vdd_uv, uv_max;
230 vdd_uv = new_target_uv;
232 vdd_uv = supply->u_volt;
235 * If we do have an absolute max voltage specified, then we should
236 * use that voltage instead to allow for cases where the voltage rails
237 * are ganged (example if we set the max for an opp as 1.12v, and
238 * the absolute max is 1.5v, for another rail to get 1.25v, it cannot
239 * be achieved if the regulator is constrainted to max of 1.12v, even
240 * if it can function at 1.25v
242 if (opp_data.vdd_absolute_max_voltage_uv)
243 uv_max = opp_data.vdd_absolute_max_voltage_uv;
245 uv_max = supply->u_volt_max;
247 if (vdd_uv > uv_max ||
248 vdd_uv < supply->u_volt_min ||
249 supply->u_volt_min > uv_max) {
251 "Invalid range voltages [Min:%lu target:%lu Max:%lu]\n",
252 supply->u_volt_min, vdd_uv, uv_max);
256 dev_dbg(dev, "%s scaling to %luuV[min %luuV max %luuV]\n", reg_name,
257 vdd_uv, supply->u_volt_min,
260 ret = regulator_set_voltage_triplet(reg,
265 dev_err(dev, "%s failed for %luuV[min %luuV max %luuV]\n",
266 reg_name, vdd_uv, supply->u_volt_min,
274 /* Do the opp supply transition */
275 static int ti_opp_config_regulators(struct device *dev,
276 struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
277 struct regulator **regulators, unsigned int count)
279 struct dev_pm_opp_supply *old_supply_vdd = &opp_data.old_supplies[0];
280 struct dev_pm_opp_supply *old_supply_vbb = &opp_data.old_supplies[1];
281 struct dev_pm_opp_supply *new_supply_vdd = &opp_data.new_supplies[0];
282 struct dev_pm_opp_supply *new_supply_vbb = &opp_data.new_supplies[1];
283 struct regulator *vdd_reg = regulators[0];
284 struct regulator *vbb_reg = regulators[1];
285 unsigned long old_freq, freq;
289 /* We must have two regulators here */
292 /* Fetch supplies and freq information from OPP core */
293 ret = dev_pm_opp_get_supplies(new_opp, opp_data.new_supplies);
296 old_freq = dev_pm_opp_get_freq(old_opp);
297 freq = dev_pm_opp_get_freq(new_opp);
298 WARN_ON(!old_freq || !freq);
300 vdd_uv = _get_optimal_vdd_voltage(dev, &opp_data,
301 new_supply_vdd->u_volt);
303 if (new_supply_vdd->u_volt_min < vdd_uv)
304 new_supply_vdd->u_volt_min = vdd_uv;
306 /* Scaling up? Scale voltage before frequency */
307 if (freq > old_freq) {
308 ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg,
311 goto restore_voltage;
313 ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb");
315 goto restore_voltage;
317 ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb");
319 goto restore_voltage;
321 ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg,
324 goto restore_voltage;
330 /* Fetch old supplies information only if required */
331 ret = dev_pm_opp_get_supplies(old_opp, opp_data.old_supplies);
334 /* This shouldn't harm even if the voltages weren't updated earlier */
335 if (old_supply_vdd->u_volt) {
336 ret = _opp_set_voltage(dev, old_supply_vbb, 0, vbb_reg, "vbb");
340 ret = _opp_set_voltage(dev, old_supply_vdd, 0, vdd_reg,
349 static const struct ti_opp_supply_of_data omap_generic_of_data = {
352 static const struct ti_opp_supply_of_data omap_omap5_of_data = {
353 .flags = OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE,
354 .efuse_voltage_mask = 0xFFF,
355 .efuse_voltage_uv = false,
358 static const struct ti_opp_supply_of_data omap_omap5core_of_data = {
359 .flags = OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE | OPPDM_HAS_NO_ABB,
360 .efuse_voltage_mask = 0xFFF,
361 .efuse_voltage_uv = false,
364 static const struct of_device_id ti_opp_supply_of_match[] = {
365 {.compatible = "ti,omap-opp-supply", .data = &omap_generic_of_data},
366 {.compatible = "ti,omap5-opp-supply", .data = &omap_omap5_of_data},
367 {.compatible = "ti,omap5-core-opp-supply",
368 .data = &omap_omap5core_of_data},
371 MODULE_DEVICE_TABLE(of, ti_opp_supply_of_match);
373 static int ti_opp_supply_probe(struct platform_device *pdev)
375 struct device *dev = &pdev->dev;
376 struct device *cpu_dev = get_cpu_device(0);
377 const struct ti_opp_supply_of_data *of_data;
380 of_data = device_get_match_data(dev);
382 /* Again, unlikely.. but mistakes do happen */
383 dev_err(dev, "%s: Bad data in match\n", __func__);
386 dev_set_drvdata(dev, (void *)of_data);
388 /* If we need optimized voltage */
389 if (of_data->flags & OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE) {
390 ret = _store_optimized_voltages(dev, &opp_data);
395 ret = dev_pm_opp_set_config_regulators(cpu_dev, ti_opp_config_regulators);
397 _free_optimized_voltages(dev, &opp_data);
402 static struct platform_driver ti_opp_supply_driver = {
403 .probe = ti_opp_supply_probe,
405 .name = "ti_opp_supply",
406 .of_match_table = of_match_ptr(ti_opp_supply_of_match),
409 module_platform_driver(ti_opp_supply_driver);
411 MODULE_DESCRIPTION("Texas Instruments OMAP OPP Supply driver");
412 MODULE_AUTHOR("Texas Instruments Inc.");
413 MODULE_LICENSE("GPL v2");