ti-opp-supply.c 12 KB

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