ti-opp-supply.c 12 KB

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