1 // SPDX-License-Identifier: GPL-2.0 2 /* 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> 6 * 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 9 * SmartReflex Class0. 10 */ 11 #include <linux/clk.h> 12 #include <linux/cpufreq.h> 13 #include <linux/device.h> 14 #include <linux/io.h> 15 #include <linux/module.h> 16 #include <linux/notifier.h> 17 #include <linux/of_device.h> 18 #include <linux/of.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> 24 25 /** 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 29 */ 30 struct ti_opp_supply_optimum_voltage_table { 31 unsigned int reference_uv; 32 unsigned int optimized_uv; 33 }; 34 35 /** 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. 42 */ 43 struct ti_opp_supply_data { 44 struct ti_opp_supply_optimum_voltage_table *vdd_table; 45 u32 num_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]; 49 }; 50 51 static struct ti_opp_supply_data opp_data; 52 53 /** 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 58 * milli-volts. 59 */ 60 struct ti_opp_supply_of_data { 61 #define OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE BIT(1) 62 #define OPPDM_HAS_NO_ABB BIT(2) 63 const u8 flags; 64 const u32 efuse_voltage_mask; 65 const bool efuse_voltage_uv; 66 }; 67 68 /** 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 72 * 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. 75 * 76 * Return: If successful, 0, else appropriate error value. 77 */ 78 static int _store_optimized_voltages(struct device *dev, 79 struct ti_opp_supply_data *data) 80 { 81 void __iomem *base; 82 struct property *prop; 83 struct resource *res; 84 const __be32 *val; 85 int proplen, i; 86 int ret = 0; 87 struct ti_opp_supply_optimum_voltage_table *table; 88 const struct ti_opp_supply_of_data *of_data = dev_get_drvdata(dev); 89 90 /* pick up Efuse based voltages */ 91 res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 0); 92 if (!res) { 93 dev_err(dev, "Unable to get IO resource\n"); 94 ret = -ENODEV; 95 goto out_map; 96 } 97 98 base = ioremap(res->start, resource_size(res)); 99 if (!base) { 100 dev_err(dev, "Unable to map Efuse registers\n"); 101 ret = -ENOMEM; 102 goto out_map; 103 } 104 105 /* Fetch efuse-settings. */ 106 prop = of_find_property(dev->of_node, "ti,efuse-settings", NULL); 107 if (!prop) { 108 dev_err(dev, "No 'ti,efuse-settings' property found\n"); 109 ret = -EINVAL; 110 goto out; 111 } 112 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"); 118 ret = -EINVAL; 119 goto out; 120 } 121 122 ret = of_property_read_u32(dev->of_node, "ti,absolute-max-voltage-uv", 123 &data->vdd_absolute_max_voltage_uv); 124 if (ret) { 125 dev_err(dev, "ti,absolute-max-voltage-uv is missing\n"); 126 ret = -EINVAL; 127 goto out; 128 } 129 130 table = kzalloc_objs(*data->vdd_table, data->num_vdd_table); 131 if (!table) { 132 ret = -ENOMEM; 133 goto out; 134 } 135 data->vdd_table = table; 136 137 val = prop->value; 138 for (i = 0; i < data->num_vdd_table; i++, table++) { 139 u32 efuse_offset; 140 u32 tmp; 141 142 table->reference_uv = be32_to_cpup(val++); 143 efuse_offset = be32_to_cpup(val++); 144 145 tmp = readl(base + efuse_offset); 146 tmp &= of_data->efuse_voltage_mask; 147 tmp >>= __ffs(of_data->efuse_voltage_mask); 148 149 table->optimized_uv = of_data->efuse_voltage_uv ? tmp : 150 tmp * 1000; 151 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); 155 156 /* 157 * Some older samples might not have optimized efuse 158 * Use reference voltage for those - just add debug message 159 * for them. 160 */ 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; 165 } 166 } 167 out: 168 iounmap(base); 169 out_map: 170 return ret; 171 } 172 173 /** 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 177 */ 178 static void _free_optimized_voltages(struct device *dev, 179 struct ti_opp_supply_data *data) 180 { 181 kfree(data->vdd_table); 182 data->vdd_table = NULL; 183 data->num_vdd_table = 0; 184 } 185 186 /** 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 191 * 192 * Return: if a match is found, return optimized voltage, else return 193 * reference_uv, also return reference_uv if no optimization is needed. 194 */ 195 static int _get_optimal_vdd_voltage(struct device *dev, 196 struct ti_opp_supply_data *data, 197 int reference_uv) 198 { 199 int i; 200 struct ti_opp_supply_optimum_voltage_table *table; 201 202 if (!data->num_vdd_table) 203 return reference_uv; 204 205 table = data->vdd_table; 206 if (!table) 207 return -EINVAL; 208 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; 213 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); 217 return reference_uv; 218 } 219 220 static int _opp_set_voltage(struct device *dev, 221 struct dev_pm_opp_supply *supply, 222 int new_target_uv, struct regulator *reg, 223 char *reg_name) 224 { 225 int ret; 226 unsigned long vdd_uv, uv_max; 227 228 if (new_target_uv) 229 vdd_uv = new_target_uv; 230 else 231 vdd_uv = supply->u_volt; 232 233 /* 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 240 */ 241 if (opp_data.vdd_absolute_max_voltage_uv) 242 uv_max = opp_data.vdd_absolute_max_voltage_uv; 243 else 244 uv_max = supply->u_volt_max; 245 246 if (vdd_uv > uv_max || 247 vdd_uv < supply->u_volt_min || 248 supply->u_volt_min > uv_max) { 249 dev_warn(dev, 250 "Invalid range voltages [Min:%lu target:%lu Max:%lu]\n", 251 supply->u_volt_min, vdd_uv, uv_max); 252 return -EINVAL; 253 } 254 255 dev_dbg(dev, "%s scaling to %luuV[min %luuV max %luuV]\n", reg_name, 256 vdd_uv, supply->u_volt_min, 257 uv_max); 258 259 ret = regulator_set_voltage_triplet(reg, 260 supply->u_volt_min, 261 vdd_uv, 262 uv_max); 263 if (ret) { 264 dev_err(dev, "%s failed for %luuV[min %luuV max %luuV]\n", 265 reg_name, vdd_uv, supply->u_volt_min, 266 uv_max); 267 return ret; 268 } 269 270 return 0; 271 } 272 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) 277 { 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; 285 int vdd_uv; 286 int ret; 287 288 /* We must have two regulators here */ 289 WARN_ON(count != 2); 290 291 /* Fetch supplies and freq information from OPP core */ 292 ret = dev_pm_opp_get_supplies(new_opp, opp_data.new_supplies); 293 WARN_ON(ret); 294 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); 298 299 vdd_uv = _get_optimal_vdd_voltage(dev, &opp_data, 300 new_supply_vdd->u_volt); 301 302 if (new_supply_vdd->u_volt_min < vdd_uv) 303 new_supply_vdd->u_volt_min = vdd_uv; 304 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, 308 "vdd"); 309 if (ret) 310 goto restore_voltage; 311 312 ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb"); 313 if (ret) 314 goto restore_voltage; 315 } else { 316 ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb"); 317 if (ret) 318 goto restore_voltage; 319 320 ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg, 321 "vdd"); 322 if (ret) 323 goto restore_voltage; 324 } 325 326 return 0; 327 328 restore_voltage: 329 /* Fetch old supplies information only if required */ 330 ret = dev_pm_opp_get_supplies(old_opp, opp_data.old_supplies); 331 WARN_ON(ret); 332 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"); 336 if (ret) 337 return ret; 338 339 ret = _opp_set_voltage(dev, old_supply_vdd, 0, vdd_reg, 340 "vdd"); 341 if (ret) 342 return ret; 343 } 344 345 return ret; 346 } 347 348 static const struct ti_opp_supply_of_data omap_generic_of_data = { 349 }; 350 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, 355 }; 356 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, 361 }; 362 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}, 368 {}, 369 }; 370 MODULE_DEVICE_TABLE(of, ti_opp_supply_of_match); 371 372 static int ti_opp_supply_probe(struct platform_device *pdev) 373 { 374 struct device *dev = &pdev->dev; 375 struct device *cpu_dev = get_cpu_device(0); 376 const struct ti_opp_supply_of_data *of_data; 377 int ret = 0; 378 379 of_data = device_get_match_data(dev); 380 if (!of_data) { 381 /* Again, unlikely.. but mistakes do happen */ 382 dev_err(dev, "%s: Bad data in match\n", __func__); 383 return -EINVAL; 384 } 385 dev_set_drvdata(dev, (void *)of_data); 386 387 /* If we need optimized voltage */ 388 if (of_data->flags & OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE) { 389 ret = _store_optimized_voltages(dev, &opp_data); 390 if (ret) 391 return ret; 392 } 393 394 ret = dev_pm_opp_set_config_regulators(cpu_dev, ti_opp_config_regulators); 395 if (ret < 0) { 396 _free_optimized_voltages(dev, &opp_data); 397 return ret; 398 } 399 400 return 0; 401 } 402 403 static struct platform_driver ti_opp_supply_driver = { 404 .probe = ti_opp_supply_probe, 405 .driver = { 406 .name = "ti_opp_supply", 407 .of_match_table = ti_opp_supply_of_match, 408 }, 409 }; 410 module_platform_driver(ti_opp_supply_driver); 411 412 MODULE_DESCRIPTION("Texas Instruments OMAP OPP Supply driver"); 413 MODULE_AUTHOR("Texas Instruments Inc."); 414 MODULE_LICENSE("GPL v2"); 415