ti-opp-supply.c 12 KB

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