sprayTerminalOperation.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_warning("%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_warning("%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. /* Adjust voltage to the exact voltage from the OPP table */
  79. for (i = 0; voltdm->volt_data[i].volt_nominal != 0; i++) {
  80. if (voltdm->volt_data[i].volt_nominal >= target_volt) {
  81. volt = voltdm->volt_data[i].volt_nominal;
  82. break;
  83. }
  84. }
  85. if (!volt) {
  86. pr_warning("%s: not scaling. OPP voltage for %lu, not found.\n",
  87. __func__, target_volt);
  88. return -EINVAL;
  89. }
  90. ret = voltdm->scale(voltdm, volt);
  91. if (!ret)
  92. voltdm->nominal_volt = volt;
  93. return ret;
  94. }
  95. /**
  96. * voltdm_reset() - Resets the voltage of a particular voltage domain
  97. * to that of the current OPP.
  98. * @voltdm: pointer to the voltage domain whose voltage is to be reset.
  99. *
  100. * This API finds out the correct voltage the voltage domain is supposed
  101. * to be at and resets the voltage to that level. Should be used especially
  102. * while disabling any voltage compensation modules.
  103. */
  104. void voltdm_reset(struct voltagedomain *voltdm)
  105. {
  106. unsigned long target_volt;
  107. if (!voltdm || IS_ERR(voltdm)) {
  108. pr_warning("%s: VDD specified does not exist!\n", __func__);
  109. return;
  110. }
  111. target_volt = voltdm_get_voltage(voltdm);
  112. if (!target_volt) {
  113. pr_err("%s: unable to find current voltage for vdd_%s\n",
  114. __func__, voltdm->name);
  115. return;
  116. }
  117. voltdm_scale(voltdm, target_volt);
  118. }
  119. /**
  120. * omap_voltage_get_volttable() - API to get the voltage table associated with a
  121. * particular voltage domain.
  122. * @voltdm: pointer to the VDD for which the voltage table is required
  123. * @volt_data: the voltage table for the particular vdd which is to be
  124. * populated by this API
  125. *
  126. * This API populates the voltage table associated with a VDD into the
  127. * passed parameter pointer. Returns the count of distinct voltages
  128. * supported by this vdd.
  129. *
  130. */
  131. void omap_voltage_get_volttable(struct voltagedomain *voltdm,
  132. struct omap_volt_data **volt_data)
  133. {
  134. if (!voltdm || IS_ERR(voltdm)) {
  135. pr_warning("%s: VDD specified does not exist!\n", __func__);
  136. return;
  137. }
  138. *volt_data = voltdm->volt_data;
  139. }
  140. /**
  141. * omap_voltage_get_voltdata() - API to get the voltage table entry for a
  142. * particular voltage
  143. * @voltdm: pointer to the VDD whose voltage table has to be searched
  144. * @volt: the voltage to be searched in the voltage table
  145. *
  146. * This API searches through the voltage table for the required voltage
  147. * domain and tries to find a matching entry for the passed voltage volt.
  148. * If a matching entry is found volt_data is populated with that entry.
  149. * This API searches only through the non-compensated voltages int the
  150. * voltage table.
  151. * Returns pointer to the voltage table entry corresponding to volt on
  152. * success. Returns -ENODATA if no voltage table exisits for the passed voltage
  153. * domain or if there is no matching entry.
  154. */
  155. struct omap_volt_data *omap_voltage_get_voltdata(struct voltagedomain *voltdm,
  156. unsigned long volt)
  157. {
  158. int i;
  159. if (!voltdm || IS_ERR(voltdm)) {
  160. pr_warning("%s: VDD specified does not exist!\n", __func__);
  161. return ERR_PTR(-EINVAL);
  162. }
  163. if (!voltdm->volt_data) {
  164. pr_warning("%s: voltage table does not exist for vdd_%s\n",
  165. __func__, voltdm->name);
  166. return ERR_PTR(-ENODATA);
  167. }
  168. for (i = 0; voltdm->volt_data[i].volt_nominal != 0; i++) {
  169. if (voltdm->volt_data[i].volt_nominal == volt)
  170. return &voltdm->volt_data[i];
  171. }
  172. pr_notice("%s: Unable to match the current voltage with the voltage table for vdd_%s\n",
  173. __func__, voltdm->name);
  174. return ERR_PTR(-ENODATA);
  175. }
  176. /**
  177. * omap_voltage_register_pmic() - API to register PMIC specific data
  178. * @voltdm: pointer to the VDD for which the PMIC specific data is
  179. * to be registered
  180. * @pmic: the structure containing pmic info
  181. *
  182. * This API is to be called by the SOC/PMIC file to specify the
  183. * pmic specific info as present in omap_voltdm_pmic structure.
  184. */
  185. int omap_voltage_register_pmic(struct voltagedomain *voltdm,
  186. struct omap_voltdm_pmic *pmic)
  187. {
  188. if (!voltdm || IS_ERR(voltdm)) {
  189. pr_warning("%s: VDD specified does not exist!\n", __func__);
  190. return -EINVAL;
  191. }
  192. voltdm->pmic = pmic;
  193. return 0;
  194. }
  195. /**
  196. * omap_change_voltscale_method() - API to change the voltage scaling method.
  197. * @voltdm: pointer to the VDD whose voltage scaling method
  198. * has to be changed.
  199. * @voltscale_method: the method to be used for voltage scaling.
  200. *
  201. * This API can be used by the board files to change the method of voltage
  202. * scaling between vpforceupdate and vcbypass. The parameter values are
  203. * defined in voltage.h
  204. */
  205. void omap_change_voltscale_method(struct voltagedomain *voltdm,
  206. int voltscale_method)
  207. {
  208. if (!voltdm || IS_ERR(voltdm)) {
  209. pr_warning("%s: VDD specified does not exist!\n", __func__);
  210. return;
  211. }
  212. switch (voltscale_method) {
  213. case VOLTSCALE_VPFORCEUPDATE:
  214. voltdm->scale = omap_vp_forceupdate_scale;
  215. return;
  216. case VOLTSCALE_VCBYPASS:
  217. voltdm->scale = omap_vc_bypass_scale;
  218. return;
  219. default:
  220. pr_warn("%s: Trying to change the method of voltage scaling to an unsupported one!\n",
  221. __func__);
  222. }
  223. }
  224. /**
  225. * omap_voltage_late_init() - Init the various voltage parameters
  226. *
  227. * This API is to be called in the later stages of the
  228. * system boot to init the voltage controller and
  229. * voltage processors.
  230. */
  231. int __init omap_voltage_late_init(void)
  232. {
  233. struct voltagedomain *voltdm;
  234. if (list_empty(&voltdm_list)) {
  235. pr_err("%s: Voltage driver support not added\n",
  236. __func__);
  237. return -EINVAL;
  238. }
  239. list_for_each_entry(voltdm, &voltdm_list, node) {
  240. struct clk *sys_ck;
  241. if (!voltdm->scalable)
  242. continue;
  243. sys_ck = clk_get(NULL, voltdm->sys_clk.name);
  244. if (IS_ERR(sys_ck)) {
  245. pr_warning("%s: Could not get sys clk.\n", __func__);
  246. return -EINVAL;
  247. }
  248. voltdm->sys_clk.rate = clk_get_rate(sys_ck);
  249. WARN_ON(!voltdm->sys_clk.rate);
  250. clk_put(sys_ck);
  251. if (voltdm->vc) {
  252. voltdm->scale = omap_vc_bypass_scale;
  253. omap_vc_init_channel(voltdm);
  254. }
  255. if (voltdm->vp) {
  256. voltdm->scale = omap_vp_forceupdate_scale;
  257. omap_vp_init(voltdm);
  258. }
  259. }
  260. return 0;
  261. }
  262. static struct voltagedomain *_voltdm_lookup(const char *name)
  263. {
  264. struct voltagedomain *voltdm, *temp_voltdm;
  265. voltdm = NULL;
  266. list_for_each_entry(temp_voltdm, &voltdm_list, node) {
  267. if (!strcmp(name, temp_voltdm->name)) {
  268. voltdm = temp_voltdm;
  269. break;
  270. }
  271. }
  272. return voltdm;
  273. }
  274. /**
  275. * voltdm_add_pwrdm - add a powerdomain to a voltagedomain
  276. * @voltdm: struct voltagedomain * to add the powerdomain to
  277. * @pwrdm: struct powerdomain * to associate with a voltagedomain
  278. *
  279. * Associate the powerdomain @pwrdm with a voltagedomain @voltdm. This
  280. * enables the use of voltdm_for_each_pwrdm(). Returns -EINVAL if
  281. * presented with invalid pointers; -ENOMEM if memory could not be allocated;
  282. * or 0 upon success.
  283. */
  284. int voltdm_add_pwrdm(struct voltagedomain *voltdm, struct powerdomain *pwrdm)
  285. {
  286. if (!voltdm || !pwrdm)
  287. return -EINVAL;
  288. pr_debug("voltagedomain: %s: associating powerdomain %s\n",
  289. voltdm->name, pwrdm->name);
  290. list_add(&pwrdm->voltdm_node, &voltdm->pwrdm_list);
  291. return 0;
  292. }
  293. /**
  294. * voltdm_for_each_pwrdm - call function for each pwrdm in a voltdm
  295. * @voltdm: struct voltagedomain * to iterate over
  296. * @fn: callback function *
  297. *
  298. * Call the supplied function @fn for each powerdomain in the
  299. * voltagedomain @voltdm. Returns -EINVAL if presented with invalid
  300. * pointers; or passes along the last return value of the callback
  301. * function, which should be 0 for success or anything else to
  302. * indicate failure.
  303. */
  304. int voltdm_for_each_pwrdm(struct voltagedomain *voltdm,
  305. int (*fn)(struct voltagedomain *voltdm,
  306. struct powerdomain *pwrdm))
  307. {
  308. struct powerdomain *pwrdm;
  309. int ret = 0;
  310. if (!fn)
  311. return -EINVAL;
  312. list_for_each_entry(pwrdm, &voltdm->pwrdm_list, voltdm_node)
  313. ret = (*fn)(voltdm, pwrdm);
  314. return ret;
  315. }
  316. /**
  317. * voltdm_for_each - call function on each registered voltagedomain
  318. * @fn: callback function *
  319. *
  320. * Call the supplied function @fn for each registered voltagedomain.
  321. * The callback function @fn can return anything but 0 to bail out
  322. * early from the iterator. Returns the last return value of the
  323. * callback function, which should be 0 for success or anything else
  324. * to indicate failure; or -EINVAL if the function pointer is null.
  325. */
  326. int voltdm_for_each(int (*fn)(struct voltagedomain *voltdm, void *user),
  327. void *user)
  328. {
  329. struct voltagedomain *temp_voltdm;
  330. int ret = 0;
  331. if (!fn)
  332. return -EINVAL;
  333. list_for_each_entry(temp_voltdm, &voltdm_list, node) {
  334. ret = (*fn)(temp_voltdm, user);
  335. if (ret)
  336. break;
  337. }
  338. return ret;
  339. }
  340. static int _voltdm_register(struct voltagedomain *voltdm)
  341. {
  342. if (!voltdm || !voltdm->name)
  343. return -EINVAL;
  344. INIT_LIST_HEAD(&voltdm->pwrdm_list);
  345. list_add(&voltdm->node, &voltdm_list);
  346. pr_debug("voltagedomain: registered %s\n", voltdm->name);
  347. return 0;
  348. }
  349. /**
  350. * voltdm_lookup - look up a voltagedomain by name, return a pointer
  351. * @name: name of voltagedomain
  352. *
  353. * Find a registered voltagedomain by its name @name. Returns a pointer
  354. * to the struct voltagedomain if found, or NULL otherwise.
  355. */
  356. struct voltagedomain *voltdm_lookup(const char *name)
  357. {
  358. struct voltagedomain *voltdm ;
  359. if (!name)
  360. return NULL;
  361. voltdm = _voltdm_lookup(name);
  362. return voltdm;
  363. }
  364. /**
  365. * voltdm_init - set up the voltagedomain layer
  366. * @voltdm_list: array of struct voltagedomain pointers to register
  367. *
  368. * Loop through the array of voltagedomains @voltdm_list, registering all
  369. * that are available on the current CPU. If voltdm_list is supplied
  370. * and not null, all of the referenced voltagedomains will be
  371. * registered. No return value.
  372. */
  373. void voltdm_init(struct voltagedomain **voltdms)
  374. {
  375. struct voltagedomain **v;
  376. if (voltdms) {
  377. for (v = voltdms; *v; v++)
  378. _voltdm_register(*v);
  379. }
  380. }