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/regulator/consumer.h>
22 #include <linux/slab.h>
25 * struct ti_opp_supply_optimum_voltage_table - optimized voltage table
26 * @reference_uv: reference voltage (usually Nominal voltage)
27 * @optimized_uv: Optimized voltage from efuse
29 struct ti_opp_supply_optimum_voltage_table {
30 unsigned int reference_uv;
31 unsigned int optimized_uv;
35 * struct ti_opp_supply_data - OMAP specific opp supply data
36 * @vdd_table: Optimized voltage mapping table
37 * @num_vdd_table: number of entries in vdd_table
38 * @vdd_absolute_max_voltage_uv: absolute maximum voltage in UV for the supply
39 * @old_supplies: Placeholder for supplies information for old OPP.
40 * @new_supplies: Placeholder for supplies information for new OPP.
42 struct ti_opp_supply_data {
43 struct ti_opp_supply_optimum_voltage_table *vdd_table;
45 u32 vdd_absolute_max_voltage_uv;
46 struct dev_pm_opp_supply old_supplies[2];
47 struct dev_pm_opp_supply new_supplies[2];
50 static struct ti_opp_supply_data opp_data;
53 * struct ti_opp_supply_of_data - device tree match data
54 * @flags: specific type of opp supply
55 * @efuse_voltage_mask: mask required for efuse register representing voltage
56 * @efuse_voltage_uv: Are the efuse entries in micro-volts? if not, assume
59 struct ti_opp_supply_of_data {
60 #define OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE BIT(1)
61 #define OPPDM_HAS_NO_ABB BIT(2)
63 const u32 efuse_voltage_mask;
64 const bool efuse_voltage_uv;
68 * _store_optimized_voltages() - store optimized voltages
69 * @dev: ti opp supply device for which we need to store info
70 * @data: data specific to the device
72 * Picks up efuse based optimized voltages for VDD unique per device and
73 * stores it in internal data structure for use during transition requests.
75 * Return: If successful, 0, else appropriate error value.
77 static int _store_optimized_voltages(struct device *dev,
78 struct ti_opp_supply_data *data)
81 struct property *prop;
86 struct ti_opp_supply_optimum_voltage_table *table;
87 const struct ti_opp_supply_of_data *of_data = dev_get_drvdata(dev);
89 /* pick up Efuse based voltages */
90 res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 0);
92 dev_err(dev, "Unable to get IO resource\n");
97 base = ioremap(res->start, resource_size(res));
99 dev_err(dev, "Unable to map Efuse registers\n");
104 /* Fetch efuse-settings. */
105 prop = of_find_property(dev->of_node, "ti,efuse-settings", NULL);
107 dev_err(dev, "No 'ti,efuse-settings' property found\n");
112 proplen = prop->length / sizeof(int);
113 data->num_vdd_table = proplen / 2;
114 /* Verify for corrupted OPP entries in dt */
115 if (data->num_vdd_table * 2 * sizeof(int) != prop->length) {
116 dev_err(dev, "Invalid 'ti,efuse-settings'\n");
121 ret = of_property_read_u32(dev->of_node, "ti,absolute-max-voltage-uv",
122 &data->vdd_absolute_max_voltage_uv);
124 dev_err(dev, "ti,absolute-max-voltage-uv is missing\n");
129 table = kcalloc(data->num_vdd_table, sizeof(*data->vdd_table),
135 data->vdd_table = table;
138 for (i = 0; i < data->num_vdd_table; i++, table++) {
142 table->reference_uv = be32_to_cpup(val++);
143 efuse_offset = be32_to_cpup(val++);
145 tmp = readl(base + efuse_offset);
146 tmp &= of_data->efuse_voltage_mask;
147 tmp >>= __ffs(of_data->efuse_voltage_mask);
149 table->optimized_uv = of_data->efuse_voltage_uv ? tmp :
152 dev_dbg(dev, "[%d] efuse=0x%08x volt_table=%d vset=%d\n",
153 i, efuse_offset, table->reference_uv,
154 table->optimized_uv);
157 * Some older samples might not have optimized efuse
158 * Use reference voltage for those - just add debug message
161 if (!table->optimized_uv) {
162 dev_dbg(dev, "[%d] efuse=0x%08x volt_table=%d:vset0\n",
163 i, efuse_offset, table->reference_uv);
164 table->optimized_uv = table->reference_uv;
174 * _free_optimized_voltages() - free resources for optvoltages
175 * @dev: device for which we need to free info
176 * @data: data specific to the device
178 static void _free_optimized_voltages(struct device *dev,
179 struct ti_opp_supply_data *data)
181 kfree(data->vdd_table);
182 data->vdd_table = NULL;
183 data->num_vdd_table = 0;
187 * _get_optimal_vdd_voltage() - Finds optimal voltage for the supply
188 * @dev: device for which we need to find info
189 * @data: data specific to the device
190 * @reference_uv: reference voltage (OPP voltage) for which we need value
192 * Return: if a match is found, return optimized voltage, else return
193 * reference_uv, also return reference_uv if no optimization is needed.
195 static int _get_optimal_vdd_voltage(struct device *dev,
196 struct ti_opp_supply_data *data,
200 struct ti_opp_supply_optimum_voltage_table *table;
202 if (!data->num_vdd_table)
205 table = data->vdd_table;
209 /* Find a exact match - this list is usually very small */
210 for (i = 0; i < data->num_vdd_table; i++, table++)
211 if (table->reference_uv == reference_uv)
212 return table->optimized_uv;
214 /* IF things are screwed up, we'd make a mess on console.. ratelimit */
215 dev_err_ratelimited(dev, "%s: Failed optimized voltage match for %d\n",
216 __func__, reference_uv);
220 static int _opp_set_voltage(struct device *dev,
221 struct dev_pm_opp_supply *supply,
222 int new_target_uv, struct regulator *reg,
226 unsigned long vdd_uv, uv_max;
229 vdd_uv = new_target_uv;
231 vdd_uv = supply->u_volt;
234 * If we do have an absolute max voltage specified, then we should
235 * use that voltage instead to allow for cases where the voltage rails
236 * are ganged (example if we set the max for an opp as 1.12v, and
237 * the absolute max is 1.5v, for another rail to get 1.25v, it cannot
238 * be achieved if the regulator is constrainted to max of 1.12v, even
239 * if it can function at 1.25v
241 if (opp_data.vdd_absolute_max_voltage_uv)
242 uv_max = opp_data.vdd_absolute_max_voltage_uv;
244 uv_max = supply->u_volt_max;
246 if (vdd_uv > uv_max ||
247 vdd_uv < supply->u_volt_min ||
248 supply->u_volt_min > uv_max) {
250 "Invalid range voltages [Min:%lu target:%lu Max:%lu]\n",
251 supply->u_volt_min, vdd_uv, uv_max);
255 dev_dbg(dev, "%s scaling to %luuV[min %luuV max %luuV]\n", reg_name,
256 vdd_uv, supply->u_volt_min,
259 ret = regulator_set_voltage_triplet(reg,
264 dev_err(dev, "%s failed for %luuV[min %luuV max %luuV]\n",
265 reg_name, vdd_uv, supply->u_volt_min,
273 /* Do the opp supply transition */
274 static int ti_opp_config_regulators(struct device *dev,
275 struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
276 struct regulator **regulators, unsigned int count)
278 struct dev_pm_opp_supply *old_supply_vdd = &opp_data.old_supplies[0];
279 struct dev_pm_opp_supply *old_supply_vbb = &opp_data.old_supplies[1];
280 struct dev_pm_opp_supply *new_supply_vdd = &opp_data.new_supplies[0];
281 struct dev_pm_opp_supply *new_supply_vbb = &opp_data.new_supplies[1];
282 struct regulator *vdd_reg = regulators[0];
283 struct regulator *vbb_reg = regulators[1];
284 unsigned long old_freq, freq;
288 /* We must have two regulators here */
291 /* Fetch supplies and freq information from OPP core */
292 ret = dev_pm_opp_get_supplies(new_opp, opp_data.new_supplies);
295 old_freq = dev_pm_opp_get_freq(old_opp);
296 freq = dev_pm_opp_get_freq(new_opp);
297 WARN_ON(!old_freq || !freq);
299 vdd_uv = _get_optimal_vdd_voltage(dev, &opp_data,
300 new_supply_vdd->u_volt);
302 if (new_supply_vdd->u_volt_min < vdd_uv)
303 new_supply_vdd->u_volt_min = vdd_uv;
305 /* Scaling up? Scale voltage before frequency */
306 if (freq > old_freq) {
307 ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg,
310 goto restore_voltage;
312 ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb");
314 goto restore_voltage;
316 ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb");
318 goto restore_voltage;
320 ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg,
323 goto restore_voltage;
329 /* Fetch old supplies information only if required */
330 ret = dev_pm_opp_get_supplies(old_opp, opp_data.old_supplies);
333 /* This shouldn't harm even if the voltages weren't updated earlier */
334 if (old_supply_vdd->u_volt) {
335 ret = _opp_set_voltage(dev, old_supply_vbb, 0, vbb_reg, "vbb");
339 ret = _opp_set_voltage(dev, old_supply_vdd, 0, vdd_reg,
348 static const struct ti_opp_supply_of_data omap_generic_of_data = {
351 static const struct ti_opp_supply_of_data omap_omap5_of_data = {
352 .flags = OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE,
353 .efuse_voltage_mask = 0xFFF,
354 .efuse_voltage_uv = false,
357 static const struct ti_opp_supply_of_data omap_omap5core_of_data = {
358 .flags = OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE | OPPDM_HAS_NO_ABB,
359 .efuse_voltage_mask = 0xFFF,
360 .efuse_voltage_uv = false,
363 static const struct of_device_id ti_opp_supply_of_match[] = {
364 {.compatible = "ti,omap-opp-supply", .data = &omap_generic_of_data},
365 {.compatible = "ti,omap5-opp-supply", .data = &omap_omap5_of_data},
366 {.compatible = "ti,omap5-core-opp-supply",
367 .data = &omap_omap5core_of_data},
370 MODULE_DEVICE_TABLE(of, ti_opp_supply_of_match);
372 static int ti_opp_supply_probe(struct platform_device *pdev)
374 struct device *dev = &pdev->dev;
375 struct device *cpu_dev = get_cpu_device(0);
376 const struct of_device_id *match;
377 const struct ti_opp_supply_of_data *of_data;
380 match = of_match_device(ti_opp_supply_of_match, dev);
382 /* We do not expect this to happen */
383 dev_err(dev, "%s: Unable to match device\n", __func__);
387 /* Again, unlikely.. but mistakes do happen */
388 dev_err(dev, "%s: Bad data in match\n", __func__);
391 of_data = match->data;
393 dev_set_drvdata(dev, (void *)of_data);
395 /* If we need optimized voltage */
396 if (of_data->flags & OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE) {
397 ret = _store_optimized_voltages(dev, &opp_data);
402 ret = dev_pm_opp_set_config_regulators(cpu_dev, ti_opp_config_regulators);
404 _free_optimized_voltages(dev, &opp_data);
409 static struct platform_driver ti_opp_supply_driver = {
410 .probe = ti_opp_supply_probe,
412 .name = "ti_opp_supply",
413 .of_match_table = of_match_ptr(ti_opp_supply_of_match),
416 module_platform_driver(ti_opp_supply_driver);
418 MODULE_DESCRIPTION("Texas Instruments OMAP OPP Supply driver");
419 MODULE_AUTHOR("Texas Instruments Inc.");
420 MODULE_LICENSE("GPL v2");