ti-opp-supply.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016-2017 Texas Instruments Incorporated - https://www.ti.com/
  4. * Nishanth Menon <[email protected]>
  5. * Dave Gerlach <[email protected]>
  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/regulator/consumer.h>
  22. #include <linux/slab.h>
  23. /**
  24. * struct ti_opp_supply_optimum_voltage_table - optimized voltage table
  25. * @reference_uv: reference voltage (usually Nominal voltage)
  26. * @optimized_uv: Optimized voltage from efuse
  27. */
  28. struct ti_opp_supply_optimum_voltage_table {
  29. unsigned int reference_uv;
  30. unsigned int optimized_uv;
  31. };
  32. /**
  33. * struct ti_opp_supply_data - OMAP specific opp supply data
  34. * @vdd_table: Optimized voltage mapping table
  35. * @num_vdd_table: number of entries in vdd_table
  36. * @vdd_absolute_max_voltage_uv: absolute maximum voltage in UV for the supply
  37. * @old_supplies: Placeholder for supplies information for old OPP.
  38. * @new_supplies: Placeholder for supplies information for new OPP.
  39. */
  40. struct ti_opp_supply_data {
  41. struct ti_opp_supply_optimum_voltage_table *vdd_table;
  42. u32 num_vdd_table;
  43. u32 vdd_absolute_max_voltage_uv;
  44. struct dev_pm_opp_supply old_supplies[2];
  45. struct dev_pm_opp_supply new_supplies[2];
  46. };
  47. static struct ti_opp_supply_data opp_data;
  48. /**
  49. * struct ti_opp_supply_of_data - device tree match data
  50. * @flags: specific type of opp supply
  51. * @efuse_voltage_mask: mask required for efuse register representing voltage
  52. * @efuse_voltage_uv: Are the efuse entries in micro-volts? if not, assume
  53. * milli-volts.
  54. */
  55. struct ti_opp_supply_of_data {
  56. #define OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE BIT(1)
  57. #define OPPDM_HAS_NO_ABB BIT(2)
  58. const u8 flags;
  59. const u32 efuse_voltage_mask;
  60. const bool efuse_voltage_uv;
  61. };
  62. /**
  63. * _store_optimized_voltages() - store optimized voltages
  64. * @dev: ti opp supply device for which we need to store info
  65. * @data: data specific to the device
  66. *
  67. * Picks up efuse based optimized voltages for VDD unique per device and
  68. * stores it in internal data structure for use during transition requests.
  69. *
  70. * Return: If successful, 0, else appropriate error value.
  71. */
  72. static int _store_optimized_voltages(struct device *dev,
  73. struct ti_opp_supply_data *data)
  74. {
  75. void __iomem *base;
  76. struct property *prop;
  77. struct resource *res;
  78. const __be32 *val;
  79. int proplen, i;
  80. int ret = 0;
  81. struct ti_opp_supply_optimum_voltage_table *table;
  82. const struct ti_opp_supply_of_data *of_data = dev_get_drvdata(dev);
  83. /* pick up Efuse based voltages */
  84. res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 0);
  85. if (!res) {
  86. dev_err(dev, "Unable to get IO resource\n");
  87. ret = -ENODEV;
  88. goto out_map;
  89. }
  90. base = ioremap(res->start, resource_size(res));
  91. if (!base) {
  92. dev_err(dev, "Unable to map Efuse registers\n");
  93. ret = -ENOMEM;
  94. goto out_map;
  95. }
  96. /* Fetch efuse-settings. */
  97. prop = of_find_property(dev->of_node, "ti,efuse-settings", NULL);
  98. if (!prop) {
  99. dev_err(dev, "No 'ti,efuse-settings' property found\n");
  100. ret = -EINVAL;
  101. goto out;
  102. }
  103. proplen = prop->length / sizeof(int);
  104. data->num_vdd_table = proplen / 2;
  105. /* Verify for corrupted OPP entries in dt */
  106. if (data->num_vdd_table * 2 * sizeof(int) != prop->length) {
  107. dev_err(dev, "Invalid 'ti,efuse-settings'\n");
  108. ret = -EINVAL;
  109. goto out;
  110. }
  111. ret = of_property_read_u32(dev->of_node, "ti,absolute-max-voltage-uv",
  112. &data->vdd_absolute_max_voltage_uv);
  113. if (ret) {
  114. dev_err(dev, "ti,absolute-max-voltage-uv is missing\n");
  115. ret = -EINVAL;
  116. goto out;
  117. }
  118. table = kcalloc(data->num_vdd_table, sizeof(*data->vdd_table),
  119. GFP_KERNEL);
  120. if (!table) {
  121. ret = -ENOMEM;
  122. goto out;
  123. }
  124. data->vdd_table = table;
  125. val = prop->value;
  126. for (i = 0; i < data->num_vdd_table; i++, table++) {
  127. u32 efuse_offset;
  128. u32 tmp;
  129. table->reference_uv = be32_to_cpup(val++);
  130. efuse_offset = be32_to_cpup(val++);
  131. tmp = readl(base + efuse_offset);
  132. tmp &= of_data->efuse_voltage_mask;
  133. tmp >>= __ffs(of_data->efuse_voltage_mask);
  134. table->optimized_uv = of_data->efuse_voltage_uv ? tmp :
  135. tmp * 1000;
  136. dev_dbg(dev, "[%d] efuse=0x%08x volt_table=%d vset=%d\n",
  137. i, efuse_offset, table->reference_uv,
  138. table->optimized_uv);
  139. /*
  140. * Some older samples might not have optimized efuse
  141. * Use reference voltage for those - just add debug message
  142. * for them.
  143. */
  144. if (!table->optimized_uv) {
  145. dev_dbg(dev, "[%d] efuse=0x%08x volt_table=%d:vset0\n",
  146. i, efuse_offset, table->reference_uv);
  147. table->optimized_uv = table->reference_uv;
  148. }
  149. }
  150. out:
  151. iounmap(base);
  152. out_map:
  153. return ret;
  154. }
  155. /**
  156. * _free_optimized_voltages() - free resources for optvoltages
  157. * @dev: device for which we need to free info
  158. * @data: data specific to the device
  159. */
  160. static void _free_optimized_voltages(struct device *dev,
  161. struct ti_opp_supply_data *data)
  162. {
  163. kfree(data->vdd_table);
  164. data->vdd_table = NULL;
  165. data->num_vdd_table = 0;
  166. }
  167. /**
  168. * _get_optimal_vdd_voltage() - Finds optimal voltage for the supply
  169. * @dev: device for which we need to find info
  170. * @data: data specific to the device
  171. * @reference_uv: reference voltage (OPP voltage) for which we need value
  172. *
  173. * Return: if a match is found, return optimized voltage, else return
  174. * reference_uv, also return reference_uv if no optimization is needed.
  175. */
  176. static int _get_optimal_vdd_voltage(struct device *dev,
  177. struct ti_opp_supply_data *data,
  178. int reference_uv)
  179. {
  180. int i;
  181. struct ti_opp_supply_optimum_voltage_table *table;
  182. if (!data->num_vdd_table)
  183. return reference_uv;
  184. table = data->vdd_table;
  185. if (!table)
  186. return -EINVAL;
  187. /* Find a exact match - this list is usually very small */
  188. for (i = 0; i < data->num_vdd_table; i++, table++)
  189. if (table->reference_uv == reference_uv)
  190. return table->optimized_uv;
  191. /* IF things are screwed up, we'd make a mess on console.. ratelimit */
  192. dev_err_ratelimited(dev, "%s: Failed optimized voltage match for %d\n",
  193. __func__, reference_uv);
  194. return reference_uv;
  195. }
  196. static int _opp_set_voltage(struct device *dev,
  197. struct dev_pm_opp_supply *supply,
  198. int new_target_uv, struct regulator *reg,
  199. char *reg_name)
  200. {
  201. int ret;
  202. unsigned long vdd_uv, uv_max;
  203. if (new_target_uv)
  204. vdd_uv = new_target_uv;
  205. else
  206. vdd_uv = supply->u_volt;
  207. /*
  208. * If we do have an absolute max voltage specified, then we should
  209. * use that voltage instead to allow for cases where the voltage rails
  210. * are ganged (example if we set the max for an opp as 1.12v, and
  211. * the absolute max is 1.5v, for another rail to get 1.25v, it cannot
  212. * be achieved if the regulator is constrainted to max of 1.12v, even
  213. * if it can function at 1.25v
  214. */
  215. if (opp_data.vdd_absolute_max_voltage_uv)
  216. uv_max = opp_data.vdd_absolute_max_voltage_uv;
  217. else
  218. uv_max = supply->u_volt_max;
  219. if (vdd_uv > uv_max ||
  220. vdd_uv < supply->u_volt_min ||
  221. supply->u_volt_min > uv_max) {
  222. dev_warn(dev,
  223. "Invalid range voltages [Min:%lu target:%lu Max:%lu]\n",
  224. supply->u_volt_min, vdd_uv, uv_max);
  225. return -EINVAL;
  226. }
  227. dev_dbg(dev, "%s scaling to %luuV[min %luuV max %luuV]\n", reg_name,
  228. vdd_uv, supply->u_volt_min,
  229. uv_max);
  230. ret = regulator_set_voltage_triplet(reg,
  231. supply->u_volt_min,
  232. vdd_uv,
  233. uv_max);
  234. if (ret) {
  235. dev_err(dev, "%s failed for %luuV[min %luuV max %luuV]\n",
  236. reg_name, vdd_uv, supply->u_volt_min,
  237. uv_max);
  238. return ret;
  239. }
  240. return 0;
  241. }
  242. /* Do the opp supply transition */
  243. static int ti_opp_config_regulators(struct device *dev,
  244. struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
  245. struct regulator **regulators, unsigned int count)
  246. {
  247. struct dev_pm_opp_supply *old_supply_vdd = &opp_data.old_supplies[0];
  248. struct dev_pm_opp_supply *old_supply_vbb = &opp_data.old_supplies[1];
  249. struct dev_pm_opp_supply *new_supply_vdd = &opp_data.new_supplies[0];
  250. struct dev_pm_opp_supply *new_supply_vbb = &opp_data.new_supplies[1];
  251. struct regulator *vdd_reg = regulators[0];
  252. struct regulator *vbb_reg = regulators[1];
  253. unsigned long old_freq, freq;
  254. int vdd_uv;
  255. int ret;
  256. /* We must have two regulators here */
  257. WARN_ON(count != 2);
  258. /* Fetch supplies and freq information from OPP core */
  259. ret = dev_pm_opp_get_supplies(new_opp, opp_data.new_supplies);
  260. WARN_ON(ret);
  261. old_freq = dev_pm_opp_get_freq(old_opp);
  262. freq = dev_pm_opp_get_freq(new_opp);
  263. WARN_ON(!old_freq || !freq);
  264. vdd_uv = _get_optimal_vdd_voltage(dev, &opp_data,
  265. new_supply_vdd->u_volt);
  266. if (new_supply_vdd->u_volt_min < vdd_uv)
  267. new_supply_vdd->u_volt_min = vdd_uv;
  268. /* Scaling up? Scale voltage before frequency */
  269. if (freq > old_freq) {
  270. ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg,
  271. "vdd");
  272. if (ret)
  273. goto restore_voltage;
  274. ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb");
  275. if (ret)
  276. goto restore_voltage;
  277. } else {
  278. ret = _opp_set_voltage(dev, new_supply_vbb, 0, vbb_reg, "vbb");
  279. if (ret)
  280. goto restore_voltage;
  281. ret = _opp_set_voltage(dev, new_supply_vdd, vdd_uv, vdd_reg,
  282. "vdd");
  283. if (ret)
  284. goto restore_voltage;
  285. }
  286. return 0;
  287. restore_voltage:
  288. /* Fetch old supplies information only if required */
  289. ret = dev_pm_opp_get_supplies(old_opp, opp_data.old_supplies);
  290. WARN_ON(ret);
  291. /* This shouldn't harm even if the voltages weren't updated earlier */
  292. if (old_supply_vdd->u_volt) {
  293. ret = _opp_set_voltage(dev, old_supply_vbb, 0, vbb_reg, "vbb");
  294. if (ret)
  295. return ret;
  296. ret = _opp_set_voltage(dev, old_supply_vdd, 0, vdd_reg,
  297. "vdd");
  298. if (ret)
  299. return ret;
  300. }
  301. return ret;
  302. }
  303. static const struct ti_opp_supply_of_data omap_generic_of_data = {
  304. };
  305. static const struct ti_opp_supply_of_data omap_omap5_of_data = {
  306. .flags = OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE,
  307. .efuse_voltage_mask = 0xFFF,
  308. .efuse_voltage_uv = false,
  309. };
  310. static const struct ti_opp_supply_of_data omap_omap5core_of_data = {
  311. .flags = OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE | OPPDM_HAS_NO_ABB,
  312. .efuse_voltage_mask = 0xFFF,
  313. .efuse_voltage_uv = false,
  314. };
  315. static const struct of_device_id ti_opp_supply_of_match[] = {
  316. {.compatible = "ti,omap-opp-supply", .data = &omap_generic_of_data},
  317. {.compatible = "ti,omap5-opp-supply", .data = &omap_omap5_of_data},
  318. {.compatible = "ti,omap5-core-opp-supply",
  319. .data = &omap_omap5core_of_data},
  320. {},
  321. };
  322. MODULE_DEVICE_TABLE(of, ti_opp_supply_of_match);
  323. static int ti_opp_supply_probe(struct platform_device *pdev)
  324. {
  325. struct device *dev = &pdev->dev;
  326. struct device *cpu_dev = get_cpu_device(0);
  327. const struct of_device_id *match;
  328. const struct ti_opp_supply_of_data *of_data;
  329. int ret = 0;
  330. match = of_match_device(ti_opp_supply_of_match, dev);
  331. if (!match) {
  332. /* We do not expect this to happen */
  333. dev_err(dev, "%s: Unable to match device\n", __func__);
  334. return -ENODEV;
  335. }
  336. if (!match->data) {
  337. /* Again, unlikely.. but mistakes do happen */
  338. dev_err(dev, "%s: Bad data in match\n", __func__);
  339. return -EINVAL;
  340. }
  341. of_data = match->data;
  342. dev_set_drvdata(dev, (void *)of_data);
  343. /* If we need optimized voltage */
  344. if (of_data->flags & OPPDM_EFUSE_CLASS0_OPTIMIZED_VOLTAGE) {
  345. ret = _store_optimized_voltages(dev, &opp_data);
  346. if (ret)
  347. return ret;
  348. }
  349. ret = dev_pm_opp_set_config_regulators(cpu_dev, ti_opp_config_regulators);
  350. if (ret < 0)
  351. _free_optimized_voltages(dev, &opp_data);
  352. return ret;
  353. }
  354. static struct platform_driver ti_opp_supply_driver = {
  355. .probe = ti_opp_supply_probe,
  356. .driver = {
  357. .name = "ti_opp_supply",
  358. .of_match_table = of_match_ptr(ti_opp_supply_of_match),
  359. },
  360. };
  361. module_platform_driver(ti_opp_supply_driver);
  362. MODULE_DESCRIPTION("Texas Instruments OMAP OPP Supply driver");
  363. MODULE_AUTHOR("Texas Instruments Inc.");
  364. MODULE_LICENSE("GPL v2");