voltage.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * OMAP3/OMAP4 Voltage Management Routines
  3. *
  4. * Author: Thara Gopinath <thara@ti.com>
  5. *
  6. * Copyright (C) 2007 Texas Instruments, Inc.
  7. * Rajendra Nayak <rnayak@ti.com>
  8. * Lesly A M <x0080970@ti.com>
  9. *
  10. * Copyright (C) 2008, 2011 Nokia Corporation
  11. * Kalle Jokiniemi
  12. * Paul Walmsley
  13. *
  14. * Copyright (C) 2010 Texas Instruments, Inc.
  15. * Thara Gopinath <thara@ti.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/io.h>
  23. #include <linux/err.h>
  24. #include <linux/export.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/slab.h>
  27. #include <linux/clk.h>
  28. #include "common.h"
  29. #include "prm-regbits-34xx.h"
  30. #include "prm-regbits-44xx.h"
  31. #include "prm44xx.h"
  32. #include "prcm44xx.h"
  33. #include "prminst44xx.h"
  34. #include "control.h"
  35. #include "voltage.h"
  36. #include "powerdomain.h"
  37. #include "vc.h"
  38. #include "vp.h"
  39. static LIST_HEAD(voltdm_list);
  40. /* Public functions */
  41. /**
  42. * voltdm_get_voltage() - Gets the current non-auto-compensated voltage
  43. * @voltdm: pointer to the voltdm for which current voltage info is needed
  44. *
  45. * API to get the current non-auto-compensated voltage for a voltage domain.
  46. * Returns 0 in case of error else returns the current voltage.
  47. */
  48. unsigned long voltdm_get_voltage(struct voltagedomain *voltdm)
  49. {
  50. if (!voltdm || IS_ERR(voltdm)) {
  51. pr_warn("%s: VDD specified does not exist!\n", __func__);
  52. return 0;
  53. }
  54. return voltdm->nominal_volt;
  55. }
  56. /**
  57. * voltdm_scale() - API to scale voltage of a particular voltage domain.
  58. * @voltdm: pointer to the voltage domain which is to be scaled.
  59. * @target_volt: The target voltage of the voltage domain
  60. *
  61. * This API should be called by the kernel to do the voltage scaling
  62. * for a particular voltage domain during DVFS.
  63. */
  64. int voltdm_scale(struct voltagedomain *voltdm,
  65. unsigned long target_volt)
  66. {
  67. int ret, i;
  68. unsigned long volt = 0;
  69. if (!voltdm || IS_ERR(voltdm)) {
  70. pr_warn("%s: VDD specified does not exist!\n", __func__);
  71. return -EINVAL;
  72. }
  73. if (!voltdm->scale) {
  74. pr_err("%s: No voltage scale API registered for vdd_%s\n",
  75. __func__, voltdm->name);
  76. return -ENODATA;
  77. }
  78. if (!voltdm->volt_data) {
  79. pr_err("%s: No voltage data defined for vdd_%s\n",
  80. __func__, voltdm->name);
  81. return -ENODATA;
  82. }
  83. /* Adjust voltage to the exact voltage from the OPP table */
  84. for (i = 0; voltdm->volt_data[i].volt_nominal != 0; i++) {
  85. if (voltdm->volt_data[i].volt_nominal >= target_volt) {
  86. volt = voltdm->volt_data[i].volt_nominal;
  87. break;
  88. }
  89. }
  90. if (!volt) {
  91. pr_warn("%s: not scaling. OPP voltage for %lu, not found.\n",
  92. __func__, target_volt);
  93. return -EINVAL;
  94. }
  95. ret = voltdm->scale(voltdm, volt);
  96. if (!ret)
  97. voltdm->nominal_volt = volt;
  98. return ret;
  99. }
  100. /**
  101. * voltdm_reset() - Resets the voltage of a particular voltage domain
  102. * to that of the current OPP.
  103. * @voltdm: pointer to the voltage domain whose voltage is to be reset.
  104. *
  105. * This API finds out the correct voltage the voltage domain is supposed
  106. * to be at and resets the voltage to that level. Should be used especially
  107. * while disabling any voltage compensation modules.
  108. */
  109. void voltdm_reset(struct voltagedomain *voltdm)
  110. {
  111. unsigned long target_volt;
  112. if (!voltdm || IS_ERR(voltdm)) {
  113. pr_warn("%s: VDD specified does not exist!\n", __func__);
  114. return;
  115. }
  116. target_volt = voltdm_get_voltage(voltdm);
  117. if (!target_volt) {
  118. pr_err("%s: unable to find current voltage for vdd_%s\n",
  119. __func__, voltdm->name);
  120. return;
  121. }
  122. voltdm_scale(voltdm, target_volt);
  123. }
  124. /**
  125. * omap_voltage_get_volttable() - API to get the voltage table associated with a
  126. * particular voltage domain.
  127. * @voltdm: pointer to the VDD for which the voltage table is required
  128. * @volt_data: the voltage table for the particular vdd which is to be
  129. * populated by this API
  130. *
  131. * This API populates the voltage table associated with a VDD into the
  132. * passed parameter pointer. Returns the count of distinct voltages
  133. * supported by this vdd.
  134. *
  135. */
  136. void omap_voltage_get_volttable(struct voltagedomain *voltdm,
  137. struct omap_volt_data **volt_data)
  138. {
  139. if (!voltdm || IS_ERR(voltdm)) {
  140. pr_warn("%s: VDD specified does not exist!\n", __func__);
  141. return;
  142. }
  143. *volt_data = voltdm->volt_data;
  144. }
  145. /**
  146. * omap_voltage_get_voltdata() - API to get the voltage table entry for a
  147. * particular voltage
  148. * @voltdm: pointer to the VDD whose voltage table has to be searched
  149. * @volt: the voltage to be searched in the voltage table
  150. *
  151. * This API searches through the voltage table for the required voltage
  152. * domain and tries to find a matching entry for the passed voltage volt.
  153. * If a matching entry is found volt_data is populated with that entry.
  154. * This API searches only through the non-compensated voltages int the
  155. * voltage table.
  156. * Returns pointer to the voltage table entry corresponding to volt on
  157. * success. Returns -ENODATA if no voltage table exisits for the passed voltage
  158. * domain or if there is no matching entry.
  159. */
  160. struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
  161. unsigned long volt)
  162. {
  163. int i;
  164. if (!voltdm || IS_ERR(voltdm)) {
  165. pr_warn("%s: VDD specified does not exist!\n", __func__);
  166. return ERR_PTR(-EINVAL);
  167. }
  168. if (!voltdm->volt_data) {
  169. pr_warn("%s: voltage table does not exist for vdd_%s\n",
  170. __func__, voltdm->name);
  171. return ERR_PTR(-ENODATA);
  172. }
  173. for (i = 0; voltdm->volt_data[i].volt_nominal != 0; i++) {
  174. if (voltdm->volt_data[i].volt_nominal == volt)
  175. return &voltdm->volt_data[i];
  176. }
  177. pr_notice("%s: Unable to match the current voltage with the voltage table for vdd_%s\n",
  178. __func__, voltdm->name);
  179. return ERR_PTR(-ENODATA);
  180. }
  181. /**
  182. * omap_voltage_register_pmic() - API to register PMIC specific data
  183. * @voltdm: pointer to the VDD for which the PMIC specific data is
  184. * to be registered
  185. * @pmic: the structure containing pmic info
  186. *
  187. * This API is to be called by the SOC/PMIC file to specify the
  188. * pmic specific info as present in omap_voltdm_pmic structure.
  189. */
  190. int omap_voltage_register_pmic(struct voltagedomain *voltdm,
  191. struct omap_voltdm_pmic *pmic)
  192. {
  193. if (!voltdm || IS_ERR(voltdm)) {
  194. pr_warn("%s: VDD specified does not exist!\n", __func__);
  195. return -EINVAL;
  196. }
  197. voltdm->pmic = pmic;
  198. return 0;
  199. }
  200. /**
  201. * omap_voltage_late_init() - Init the various voltage parameters
  202. *
  203. * This API is to be called in the later stages of the
  204. * system boot to init the voltage controller and
  205. * voltage processors.
  206. */
  207. int __init omap_voltage_late_init(void)
  208. {
  209. struct voltagedomain *voltdm;
  210. if (list_empty(&voltdm_list)) {
  211. pr_err("%s: Voltage driver support not added\n",
  212. __func__);
  213. return -EINVAL;
  214. }
  215. list_for_each_entry(voltdm, &voltdm_list, node) {
  216. struct clk *sys_ck;
  217. if (!voltdm->scalable)
  218. continue;
  219. sys_ck = clk_get(NULL, voltdm->sys_clk.name);
  220. if (IS_ERR(sys_ck)) {
  221. pr_warn("%s: Could not get sys clk.\n", __func__);
  222. return -EINVAL;
  223. }
  224. voltdm->sys_clk.rate = clk_get_rate(sys_ck);
  225. WARN_ON(!voltdm->sys_clk.rate);
  226. clk_put(sys_ck);
  227. if (voltdm->vc) {
  228. voltdm->scale = omap_vc_bypass_scale;
  229. omap_vc_init_channel(voltdm);
  230. }
  231. if (voltdm->vp) {
  232. voltdm->scale = omap_vp_forceupdate_scale;
  233. omap_vp_init(voltdm);
  234. }
  235. }
  236. return 0;
  237. }
  238. static struct voltagedomain *_voltdm_lookup(const char *name)
  239. {
  240. struct voltagedomain *voltdm, *temp_voltdm;
  241. voltdm = NULL;
  242. list_for_each_entry(temp_voltdm, &voltdm_list, node) {
  243. if (!strcmp(name, temp_voltdm->name)) {
  244. voltdm = temp_voltdm;
  245. break;
  246. }
  247. }
  248. return voltdm;
  249. }
  250. static int _voltdm_register(struct voltagedomain *voltdm)
  251. {
  252. if (!voltdm || !voltdm->name)
  253. return -EINVAL;
  254. list_add(&voltdm->node, &voltdm_list);
  255. pr_debug("voltagedomain: registered %s\n", voltdm->name);
  256. return 0;
  257. }
  258. /**
  259. * voltdm_lookup - look up a voltagedomain by name, return a pointer
  260. * @name: name of voltagedomain
  261. *
  262. * Find a registered voltagedomain by its name @name. Returns a pointer
  263. * to the struct voltagedomain if found, or NULL otherwise.
  264. */
  265. struct voltagedomain *voltdm_lookup(const char *name)
  266. {
  267. struct voltagedomain *voltdm ;
  268. if (!name)
  269. return NULL;
  270. voltdm = _voltdm_lookup(name);
  271. return voltdm;
  272. }
  273. /**
  274. * voltdm_init - set up the voltagedomain layer
  275. * @voltdm_list: array of struct voltagedomain pointers to register
  276. *
  277. * Loop through the array of voltagedomains @voltdm_list, registering all
  278. * that are available on the current CPU. If voltdm_list is supplied
  279. * and not null, all of the referenced voltagedomains will be
  280. * registered. No return value.
  281. */
  282. void voltdm_init(struct voltagedomain **voltdms)
  283. {
  284. struct voltagedomain **v;
  285. if (voltdms) {
  286. for (v = voltdms; *v; v++)
  287. _voltdm_register(*v);
  288. }
  289. }