core.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /*
  2. * Generic OPP Interface
  3. *
  4. * Copyright (C) 2009-2010 Texas Instruments Incorporated.
  5. * Nishanth Menon
  6. * Romit Dasgupta
  7. * Kevin Hilman
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/clk.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/device.h>
  19. #include <linux/export.h>
  20. #include <linux/pm_domain.h>
  21. #include <linux/regulator/consumer.h>
  22. #include "opp.h"
  23. /*
  24. * The root of the list of all opp-tables. All opp_table structures branch off
  25. * from here, with each opp_table containing the list of opps it supports in
  26. * various states of availability.
  27. */
  28. LIST_HEAD(opp_tables);
  29. /* Lock to allow exclusive modification to the device and opp lists */
  30. DEFINE_MUTEX(opp_table_lock);
  31. static struct opp_device *_find_opp_dev(const struct device *dev,
  32. struct opp_table *opp_table)
  33. {
  34. struct opp_device *opp_dev;
  35. list_for_each_entry(opp_dev, &opp_table->dev_list, node)
  36. if (opp_dev->dev == dev)
  37. return opp_dev;
  38. return NULL;
  39. }
  40. static struct opp_table *_find_opp_table_unlocked(struct device *dev)
  41. {
  42. struct opp_table *opp_table;
  43. list_for_each_entry(opp_table, &opp_tables, node) {
  44. if (_find_opp_dev(dev, opp_table)) {
  45. _get_opp_table_kref(opp_table);
  46. return opp_table;
  47. }
  48. }
  49. return ERR_PTR(-ENODEV);
  50. }
  51. /**
  52. * _find_opp_table() - find opp_table struct using device pointer
  53. * @dev: device pointer used to lookup OPP table
  54. *
  55. * Search OPP table for one containing matching device.
  56. *
  57. * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
  58. * -EINVAL based on type of error.
  59. *
  60. * The callers must call dev_pm_opp_put_opp_table() after the table is used.
  61. */
  62. struct opp_table *_find_opp_table(struct device *dev)
  63. {
  64. struct opp_table *opp_table;
  65. if (IS_ERR_OR_NULL(dev)) {
  66. pr_err("%s: Invalid parameters\n", __func__);
  67. return ERR_PTR(-EINVAL);
  68. }
  69. mutex_lock(&opp_table_lock);
  70. opp_table = _find_opp_table_unlocked(dev);
  71. mutex_unlock(&opp_table_lock);
  72. return opp_table;
  73. }
  74. /**
  75. * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
  76. * @opp: opp for which voltage has to be returned for
  77. *
  78. * Return: voltage in micro volt corresponding to the opp, else
  79. * return 0
  80. *
  81. * This is useful only for devices with single power supply.
  82. */
  83. unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
  84. {
  85. if (IS_ERR_OR_NULL(opp)) {
  86. pr_err("%s: Invalid parameters\n", __func__);
  87. return 0;
  88. }
  89. return opp->supplies[0].u_volt;
  90. }
  91. EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
  92. /**
  93. * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
  94. * @opp: opp for which frequency has to be returned for
  95. *
  96. * Return: frequency in hertz corresponding to the opp, else
  97. * return 0
  98. */
  99. unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
  100. {
  101. if (IS_ERR_OR_NULL(opp) || !opp->available) {
  102. pr_err("%s: Invalid parameters\n", __func__);
  103. return 0;
  104. }
  105. return opp->rate;
  106. }
  107. EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
  108. /**
  109. * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
  110. * @opp: opp for which turbo mode is being verified
  111. *
  112. * Turbo OPPs are not for normal use, and can be enabled (under certain
  113. * conditions) for short duration of times to finish high throughput work
  114. * quickly. Running on them for longer times may overheat the chip.
  115. *
  116. * Return: true if opp is turbo opp, else false.
  117. */
  118. bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
  119. {
  120. if (IS_ERR_OR_NULL(opp) || !opp->available) {
  121. pr_err("%s: Invalid parameters\n", __func__);
  122. return false;
  123. }
  124. return opp->turbo;
  125. }
  126. EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
  127. /**
  128. * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
  129. * @dev: device for which we do this operation
  130. *
  131. * Return: This function returns the max clock latency in nanoseconds.
  132. */
  133. unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
  134. {
  135. struct opp_table *opp_table;
  136. unsigned long clock_latency_ns;
  137. opp_table = _find_opp_table(dev);
  138. if (IS_ERR(opp_table))
  139. return 0;
  140. clock_latency_ns = opp_table->clock_latency_ns_max;
  141. dev_pm_opp_put_opp_table(opp_table);
  142. return clock_latency_ns;
  143. }
  144. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
  145. /**
  146. * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
  147. * @dev: device for which we do this operation
  148. *
  149. * Return: This function returns the max voltage latency in nanoseconds.
  150. */
  151. unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
  152. {
  153. struct opp_table *opp_table;
  154. struct dev_pm_opp *opp;
  155. struct regulator *reg;
  156. unsigned long latency_ns = 0;
  157. int ret, i, count;
  158. struct {
  159. unsigned long min;
  160. unsigned long max;
  161. } *uV;
  162. opp_table = _find_opp_table(dev);
  163. if (IS_ERR(opp_table))
  164. return 0;
  165. /* Regulator may not be required for the device */
  166. if (!opp_table->regulators)
  167. goto put_opp_table;
  168. count = opp_table->regulator_count;
  169. uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
  170. if (!uV)
  171. goto put_opp_table;
  172. mutex_lock(&opp_table->lock);
  173. for (i = 0; i < count; i++) {
  174. uV[i].min = ~0;
  175. uV[i].max = 0;
  176. list_for_each_entry(opp, &opp_table->opp_list, node) {
  177. if (!opp->available)
  178. continue;
  179. if (opp->supplies[i].u_volt_min < uV[i].min)
  180. uV[i].min = opp->supplies[i].u_volt_min;
  181. if (opp->supplies[i].u_volt_max > uV[i].max)
  182. uV[i].max = opp->supplies[i].u_volt_max;
  183. }
  184. }
  185. mutex_unlock(&opp_table->lock);
  186. /*
  187. * The caller needs to ensure that opp_table (and hence the regulator)
  188. * isn't freed, while we are executing this routine.
  189. */
  190. for (i = 0; i < count; i++) {
  191. reg = opp_table->regulators[i];
  192. ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
  193. if (ret > 0)
  194. latency_ns += ret * 1000;
  195. }
  196. kfree(uV);
  197. put_opp_table:
  198. dev_pm_opp_put_opp_table(opp_table);
  199. return latency_ns;
  200. }
  201. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
  202. /**
  203. * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
  204. * nanoseconds
  205. * @dev: device for which we do this operation
  206. *
  207. * Return: This function returns the max transition latency, in nanoseconds, to
  208. * switch from one OPP to other.
  209. */
  210. unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
  211. {
  212. return dev_pm_opp_get_max_volt_latency(dev) +
  213. dev_pm_opp_get_max_clock_latency(dev);
  214. }
  215. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
  216. /**
  217. * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
  218. * @dev: device for which we do this operation
  219. *
  220. * Return: This function returns the frequency of the OPP marked as suspend_opp
  221. * if one is available, else returns 0;
  222. */
  223. unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
  224. {
  225. struct opp_table *opp_table;
  226. unsigned long freq = 0;
  227. opp_table = _find_opp_table(dev);
  228. if (IS_ERR(opp_table))
  229. return 0;
  230. if (opp_table->suspend_opp && opp_table->suspend_opp->available)
  231. freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
  232. dev_pm_opp_put_opp_table(opp_table);
  233. return freq;
  234. }
  235. EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
  236. int _get_opp_count(struct opp_table *opp_table)
  237. {
  238. struct dev_pm_opp *opp;
  239. int count = 0;
  240. mutex_lock(&opp_table->lock);
  241. list_for_each_entry(opp, &opp_table->opp_list, node) {
  242. if (opp->available)
  243. count++;
  244. }
  245. mutex_unlock(&opp_table->lock);
  246. return count;
  247. }
  248. /**
  249. * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
  250. * @dev: device for which we do this operation
  251. *
  252. * Return: This function returns the number of available opps if there are any,
  253. * else returns 0 if none or the corresponding error value.
  254. */
  255. int dev_pm_opp_get_opp_count(struct device *dev)
  256. {
  257. struct opp_table *opp_table;
  258. int count;
  259. opp_table = _find_opp_table(dev);
  260. if (IS_ERR(opp_table)) {
  261. count = PTR_ERR(opp_table);
  262. dev_dbg(dev, "%s: OPP table not found (%d)\n",
  263. __func__, count);
  264. return count;
  265. }
  266. count = _get_opp_count(opp_table);
  267. dev_pm_opp_put_opp_table(opp_table);
  268. return count;
  269. }
  270. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
  271. /**
  272. * dev_pm_opp_find_freq_exact() - search for an exact frequency
  273. * @dev: device for which we do this operation
  274. * @freq: frequency to search for
  275. * @available: true/false - match for available opp
  276. *
  277. * Return: Searches for exact match in the opp table and returns pointer to the
  278. * matching opp if found, else returns ERR_PTR in case of error and should
  279. * be handled using IS_ERR. Error return values can be:
  280. * EINVAL: for bad pointer
  281. * ERANGE: no match found for search
  282. * ENODEV: if device not found in list of registered devices
  283. *
  284. * Note: available is a modifier for the search. if available=true, then the
  285. * match is for exact matching frequency and is available in the stored OPP
  286. * table. if false, the match is for exact frequency which is not available.
  287. *
  288. * This provides a mechanism to enable an opp which is not available currently
  289. * or the opposite as well.
  290. *
  291. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  292. * use.
  293. */
  294. struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
  295. unsigned long freq,
  296. bool available)
  297. {
  298. struct opp_table *opp_table;
  299. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  300. opp_table = _find_opp_table(dev);
  301. if (IS_ERR(opp_table)) {
  302. int r = PTR_ERR(opp_table);
  303. dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
  304. return ERR_PTR(r);
  305. }
  306. mutex_lock(&opp_table->lock);
  307. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  308. if (temp_opp->available == available &&
  309. temp_opp->rate == freq) {
  310. opp = temp_opp;
  311. /* Increment the reference count of OPP */
  312. dev_pm_opp_get(opp);
  313. break;
  314. }
  315. }
  316. mutex_unlock(&opp_table->lock);
  317. dev_pm_opp_put_opp_table(opp_table);
  318. return opp;
  319. }
  320. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
  321. static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
  322. unsigned long *freq)
  323. {
  324. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  325. mutex_lock(&opp_table->lock);
  326. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  327. if (temp_opp->available && temp_opp->rate >= *freq) {
  328. opp = temp_opp;
  329. *freq = opp->rate;
  330. /* Increment the reference count of OPP */
  331. dev_pm_opp_get(opp);
  332. break;
  333. }
  334. }
  335. mutex_unlock(&opp_table->lock);
  336. return opp;
  337. }
  338. /**
  339. * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
  340. * @dev: device for which we do this operation
  341. * @freq: Start frequency
  342. *
  343. * Search for the matching ceil *available* OPP from a starting freq
  344. * for a device.
  345. *
  346. * Return: matching *opp and refreshes *freq accordingly, else returns
  347. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  348. * values can be:
  349. * EINVAL: for bad pointer
  350. * ERANGE: no match found for search
  351. * ENODEV: if device not found in list of registered devices
  352. *
  353. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  354. * use.
  355. */
  356. struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
  357. unsigned long *freq)
  358. {
  359. struct opp_table *opp_table;
  360. struct dev_pm_opp *opp;
  361. if (!dev || !freq) {
  362. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  363. return ERR_PTR(-EINVAL);
  364. }
  365. opp_table = _find_opp_table(dev);
  366. if (IS_ERR(opp_table))
  367. return ERR_CAST(opp_table);
  368. opp = _find_freq_ceil(opp_table, freq);
  369. dev_pm_opp_put_opp_table(opp_table);
  370. return opp;
  371. }
  372. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
  373. /**
  374. * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
  375. * @dev: device for which we do this operation
  376. * @freq: Start frequency
  377. *
  378. * Search for the matching floor *available* OPP from a starting freq
  379. * for a device.
  380. *
  381. * Return: matching *opp and refreshes *freq accordingly, else returns
  382. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  383. * values can be:
  384. * EINVAL: for bad pointer
  385. * ERANGE: no match found for search
  386. * ENODEV: if device not found in list of registered devices
  387. *
  388. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  389. * use.
  390. */
  391. struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
  392. unsigned long *freq)
  393. {
  394. struct opp_table *opp_table;
  395. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  396. if (!dev || !freq) {
  397. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  398. return ERR_PTR(-EINVAL);
  399. }
  400. opp_table = _find_opp_table(dev);
  401. if (IS_ERR(opp_table))
  402. return ERR_CAST(opp_table);
  403. mutex_lock(&opp_table->lock);
  404. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  405. if (temp_opp->available) {
  406. /* go to the next node, before choosing prev */
  407. if (temp_opp->rate > *freq)
  408. break;
  409. else
  410. opp = temp_opp;
  411. }
  412. }
  413. /* Increment the reference count of OPP */
  414. if (!IS_ERR(opp))
  415. dev_pm_opp_get(opp);
  416. mutex_unlock(&opp_table->lock);
  417. dev_pm_opp_put_opp_table(opp_table);
  418. if (!IS_ERR(opp))
  419. *freq = opp->rate;
  420. return opp;
  421. }
  422. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
  423. static int _set_opp_voltage(struct device *dev, struct regulator *reg,
  424. struct dev_pm_opp_supply *supply)
  425. {
  426. int ret;
  427. /* Regulator not available for device */
  428. if (IS_ERR(reg)) {
  429. dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
  430. PTR_ERR(reg));
  431. return 0;
  432. }
  433. dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
  434. supply->u_volt_min, supply->u_volt, supply->u_volt_max);
  435. ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
  436. supply->u_volt, supply->u_volt_max);
  437. if (ret)
  438. dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
  439. __func__, supply->u_volt_min, supply->u_volt,
  440. supply->u_volt_max, ret);
  441. return ret;
  442. }
  443. static inline int
  444. _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
  445. unsigned long old_freq, unsigned long freq)
  446. {
  447. int ret;
  448. ret = clk_set_rate(clk, freq);
  449. if (ret) {
  450. dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
  451. ret);
  452. }
  453. return ret;
  454. }
  455. static inline int
  456. _generic_set_opp_domain(struct device *dev, struct clk *clk,
  457. unsigned long old_freq, unsigned long freq,
  458. unsigned int old_pstate, unsigned int new_pstate)
  459. {
  460. int ret;
  461. /* Scaling up? Scale domain performance state before frequency */
  462. if (freq > old_freq) {
  463. ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
  464. if (ret)
  465. return ret;
  466. }
  467. ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
  468. if (ret)
  469. goto restore_domain_state;
  470. /* Scaling down? Scale domain performance state after frequency */
  471. if (freq < old_freq) {
  472. ret = dev_pm_genpd_set_performance_state(dev, new_pstate);
  473. if (ret)
  474. goto restore_freq;
  475. }
  476. return 0;
  477. restore_freq:
  478. if (_generic_set_opp_clk_only(dev, clk, freq, old_freq))
  479. dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
  480. __func__, old_freq);
  481. restore_domain_state:
  482. if (freq > old_freq)
  483. dev_pm_genpd_set_performance_state(dev, old_pstate);
  484. return ret;
  485. }
  486. static int _generic_set_opp_regulator(const struct opp_table *opp_table,
  487. struct device *dev,
  488. unsigned long old_freq,
  489. unsigned long freq,
  490. struct dev_pm_opp_supply *old_supply,
  491. struct dev_pm_opp_supply *new_supply)
  492. {
  493. struct regulator *reg = opp_table->regulators[0];
  494. int ret;
  495. /* This function only supports single regulator per device */
  496. if (WARN_ON(opp_table->regulator_count > 1)) {
  497. dev_err(dev, "multiple regulators are not supported\n");
  498. return -EINVAL;
  499. }
  500. /* Scaling up? Scale voltage before frequency */
  501. if (freq >= old_freq) {
  502. ret = _set_opp_voltage(dev, reg, new_supply);
  503. if (ret)
  504. goto restore_voltage;
  505. }
  506. /* Change frequency */
  507. ret = _generic_set_opp_clk_only(dev, opp_table->clk, old_freq, freq);
  508. if (ret)
  509. goto restore_voltage;
  510. /* Scaling down? Scale voltage after frequency */
  511. if (freq < old_freq) {
  512. ret = _set_opp_voltage(dev, reg, new_supply);
  513. if (ret)
  514. goto restore_freq;
  515. }
  516. return 0;
  517. restore_freq:
  518. if (_generic_set_opp_clk_only(dev, opp_table->clk, freq, old_freq))
  519. dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
  520. __func__, old_freq);
  521. restore_voltage:
  522. /* This shouldn't harm even if the voltages weren't updated earlier */
  523. if (old_supply)
  524. _set_opp_voltage(dev, reg, old_supply);
  525. return ret;
  526. }
  527. /**
  528. * dev_pm_opp_set_rate() - Configure new OPP based on frequency
  529. * @dev: device for which we do this operation
  530. * @target_freq: frequency to achieve
  531. *
  532. * This configures the power-supplies and clock source to the levels specified
  533. * by the OPP corresponding to the target_freq.
  534. */
  535. int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
  536. {
  537. struct opp_table *opp_table;
  538. unsigned long freq, old_freq;
  539. struct dev_pm_opp *old_opp, *opp;
  540. struct clk *clk;
  541. int ret, size;
  542. if (unlikely(!target_freq)) {
  543. dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
  544. target_freq);
  545. return -EINVAL;
  546. }
  547. opp_table = _find_opp_table(dev);
  548. if (IS_ERR(opp_table)) {
  549. dev_err(dev, "%s: device opp doesn't exist\n", __func__);
  550. return PTR_ERR(opp_table);
  551. }
  552. clk = opp_table->clk;
  553. if (IS_ERR(clk)) {
  554. dev_err(dev, "%s: No clock available for the device\n",
  555. __func__);
  556. ret = PTR_ERR(clk);
  557. goto put_opp_table;
  558. }
  559. freq = clk_round_rate(clk, target_freq);
  560. if ((long)freq <= 0)
  561. freq = target_freq;
  562. old_freq = clk_get_rate(clk);
  563. /* Return early if nothing to do */
  564. if (old_freq == freq) {
  565. dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
  566. __func__, freq);
  567. ret = 0;
  568. goto put_opp_table;
  569. }
  570. old_opp = _find_freq_ceil(opp_table, &old_freq);
  571. if (IS_ERR(old_opp)) {
  572. dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
  573. __func__, old_freq, PTR_ERR(old_opp));
  574. }
  575. opp = _find_freq_ceil(opp_table, &freq);
  576. if (IS_ERR(opp)) {
  577. ret = PTR_ERR(opp);
  578. dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
  579. __func__, freq, ret);
  580. goto put_old_opp;
  581. }
  582. dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
  583. old_freq, freq);
  584. /* Only frequency scaling */
  585. if (!opp_table->regulators) {
  586. /*
  587. * We don't support devices with both regulator and
  588. * domain performance-state for now.
  589. */
  590. if (opp_table->genpd_performance_state)
  591. ret = _generic_set_opp_domain(dev, clk, old_freq, freq,
  592. IS_ERR(old_opp) ? 0 : old_opp->pstate,
  593. opp->pstate);
  594. else
  595. ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
  596. } else if (!opp_table->set_opp) {
  597. ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
  598. IS_ERR(old_opp) ? NULL : old_opp->supplies,
  599. opp->supplies);
  600. } else {
  601. struct dev_pm_set_opp_data *data;
  602. data = opp_table->set_opp_data;
  603. data->regulators = opp_table->regulators;
  604. data->regulator_count = opp_table->regulator_count;
  605. data->clk = clk;
  606. data->dev = dev;
  607. data->old_opp.rate = old_freq;
  608. size = sizeof(*opp->supplies) * opp_table->regulator_count;
  609. if (IS_ERR(old_opp))
  610. memset(data->old_opp.supplies, 0, size);
  611. else
  612. memcpy(data->old_opp.supplies, old_opp->supplies, size);
  613. data->new_opp.rate = freq;
  614. memcpy(data->new_opp.supplies, opp->supplies, size);
  615. ret = opp_table->set_opp(data);
  616. }
  617. dev_pm_opp_put(opp);
  618. put_old_opp:
  619. if (!IS_ERR(old_opp))
  620. dev_pm_opp_put(old_opp);
  621. put_opp_table:
  622. dev_pm_opp_put_opp_table(opp_table);
  623. return ret;
  624. }
  625. EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
  626. /* OPP-dev Helpers */
  627. static void _remove_opp_dev(struct opp_device *opp_dev,
  628. struct opp_table *opp_table)
  629. {
  630. opp_debug_unregister(opp_dev, opp_table);
  631. list_del(&opp_dev->node);
  632. kfree(opp_dev);
  633. }
  634. struct opp_device *_add_opp_dev(const struct device *dev,
  635. struct opp_table *opp_table)
  636. {
  637. struct opp_device *opp_dev;
  638. int ret;
  639. opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
  640. if (!opp_dev)
  641. return NULL;
  642. /* Initialize opp-dev */
  643. opp_dev->dev = dev;
  644. list_add(&opp_dev->node, &opp_table->dev_list);
  645. /* Create debugfs entries for the opp_table */
  646. ret = opp_debug_register(opp_dev, opp_table);
  647. if (ret)
  648. dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
  649. __func__, ret);
  650. return opp_dev;
  651. }
  652. static struct opp_table *_allocate_opp_table(struct device *dev)
  653. {
  654. struct opp_table *opp_table;
  655. struct opp_device *opp_dev;
  656. int ret;
  657. /*
  658. * Allocate a new OPP table. In the infrequent case where a new
  659. * device is needed to be added, we pay this penalty.
  660. */
  661. opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
  662. if (!opp_table)
  663. return NULL;
  664. INIT_LIST_HEAD(&opp_table->dev_list);
  665. /* Mark regulator count uninitialized */
  666. opp_table->regulator_count = -1;
  667. opp_dev = _add_opp_dev(dev, opp_table);
  668. if (!opp_dev) {
  669. kfree(opp_table);
  670. return NULL;
  671. }
  672. _of_init_opp_table(opp_table, dev);
  673. /* Find clk for the device */
  674. opp_table->clk = clk_get(dev, NULL);
  675. if (IS_ERR(opp_table->clk)) {
  676. ret = PTR_ERR(opp_table->clk);
  677. if (ret != -EPROBE_DEFER)
  678. dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
  679. ret);
  680. }
  681. BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
  682. INIT_LIST_HEAD(&opp_table->opp_list);
  683. mutex_init(&opp_table->lock);
  684. kref_init(&opp_table->kref);
  685. /* Secure the device table modification */
  686. list_add(&opp_table->node, &opp_tables);
  687. return opp_table;
  688. }
  689. void _get_opp_table_kref(struct opp_table *opp_table)
  690. {
  691. kref_get(&opp_table->kref);
  692. }
  693. struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
  694. {
  695. struct opp_table *opp_table;
  696. /* Hold our table modification lock here */
  697. mutex_lock(&opp_table_lock);
  698. opp_table = _find_opp_table_unlocked(dev);
  699. if (!IS_ERR(opp_table))
  700. goto unlock;
  701. opp_table = _allocate_opp_table(dev);
  702. unlock:
  703. mutex_unlock(&opp_table_lock);
  704. return opp_table;
  705. }
  706. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
  707. static void _opp_table_kref_release(struct kref *kref)
  708. {
  709. struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
  710. struct opp_device *opp_dev;
  711. /* Release clk */
  712. if (!IS_ERR(opp_table->clk))
  713. clk_put(opp_table->clk);
  714. opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
  715. node);
  716. _remove_opp_dev(opp_dev, opp_table);
  717. /* dev_list must be empty now */
  718. WARN_ON(!list_empty(&opp_table->dev_list));
  719. mutex_destroy(&opp_table->lock);
  720. list_del(&opp_table->node);
  721. kfree(opp_table);
  722. mutex_unlock(&opp_table_lock);
  723. }
  724. void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
  725. {
  726. kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
  727. &opp_table_lock);
  728. }
  729. EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
  730. void _opp_free(struct dev_pm_opp *opp)
  731. {
  732. kfree(opp);
  733. }
  734. static void _opp_kref_release(struct kref *kref)
  735. {
  736. struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
  737. struct opp_table *opp_table = opp->opp_table;
  738. /*
  739. * Notify the changes in the availability of the operable
  740. * frequency/voltage list.
  741. */
  742. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
  743. opp_debug_remove_one(opp);
  744. list_del(&opp->node);
  745. kfree(opp);
  746. mutex_unlock(&opp_table->lock);
  747. dev_pm_opp_put_opp_table(opp_table);
  748. }
  749. void dev_pm_opp_get(struct dev_pm_opp *opp)
  750. {
  751. kref_get(&opp->kref);
  752. }
  753. void dev_pm_opp_put(struct dev_pm_opp *opp)
  754. {
  755. kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
  756. }
  757. EXPORT_SYMBOL_GPL(dev_pm_opp_put);
  758. /**
  759. * dev_pm_opp_remove() - Remove an OPP from OPP table
  760. * @dev: device for which we do this operation
  761. * @freq: OPP to remove with matching 'freq'
  762. *
  763. * This function removes an opp from the opp table.
  764. */
  765. void dev_pm_opp_remove(struct device *dev, unsigned long freq)
  766. {
  767. struct dev_pm_opp *opp;
  768. struct opp_table *opp_table;
  769. bool found = false;
  770. opp_table = _find_opp_table(dev);
  771. if (IS_ERR(opp_table))
  772. return;
  773. mutex_lock(&opp_table->lock);
  774. list_for_each_entry(opp, &opp_table->opp_list, node) {
  775. if (opp->rate == freq) {
  776. found = true;
  777. break;
  778. }
  779. }
  780. mutex_unlock(&opp_table->lock);
  781. if (found) {
  782. dev_pm_opp_put(opp);
  783. } else {
  784. dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
  785. __func__, freq);
  786. }
  787. dev_pm_opp_put_opp_table(opp_table);
  788. }
  789. EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
  790. struct dev_pm_opp *_opp_allocate(struct opp_table *table)
  791. {
  792. struct dev_pm_opp *opp;
  793. int count, supply_size;
  794. /* Allocate space for at least one supply */
  795. count = table->regulator_count > 0 ? table->regulator_count : 1;
  796. supply_size = sizeof(*opp->supplies) * count;
  797. /* allocate new OPP node and supplies structures */
  798. opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
  799. if (!opp)
  800. return NULL;
  801. /* Put the supplies at the end of the OPP structure as an empty array */
  802. opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
  803. INIT_LIST_HEAD(&opp->node);
  804. return opp;
  805. }
  806. static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
  807. struct opp_table *opp_table)
  808. {
  809. struct regulator *reg;
  810. int i;
  811. if (!opp_table->regulators)
  812. return true;
  813. for (i = 0; i < opp_table->regulator_count; i++) {
  814. reg = opp_table->regulators[i];
  815. if (!regulator_is_supported_voltage(reg,
  816. opp->supplies[i].u_volt_min,
  817. opp->supplies[i].u_volt_max)) {
  818. pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
  819. __func__, opp->supplies[i].u_volt_min,
  820. opp->supplies[i].u_volt_max);
  821. return false;
  822. }
  823. }
  824. return true;
  825. }
  826. static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
  827. struct opp_table *opp_table,
  828. struct list_head **head)
  829. {
  830. struct dev_pm_opp *opp;
  831. /*
  832. * Insert new OPP in order of increasing frequency and discard if
  833. * already present.
  834. *
  835. * Need to use &opp_table->opp_list in the condition part of the 'for'
  836. * loop, don't replace it with head otherwise it will become an infinite
  837. * loop.
  838. */
  839. list_for_each_entry(opp, &opp_table->opp_list, node) {
  840. if (new_opp->rate > opp->rate) {
  841. *head = &opp->node;
  842. continue;
  843. }
  844. if (new_opp->rate < opp->rate)
  845. return 0;
  846. /* Duplicate OPPs */
  847. dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
  848. __func__, opp->rate, opp->supplies[0].u_volt,
  849. opp->available, new_opp->rate,
  850. new_opp->supplies[0].u_volt, new_opp->available);
  851. /* Should we compare voltages for all regulators here ? */
  852. return opp->available &&
  853. new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
  854. }
  855. return 0;
  856. }
  857. /*
  858. * Returns:
  859. * 0: On success. And appropriate error message for duplicate OPPs.
  860. * -EBUSY: For OPP with same freq/volt and is available. The callers of
  861. * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
  862. * sure we don't print error messages unnecessarily if different parts of
  863. * kernel try to initialize the OPP table.
  864. * -EEXIST: For OPP with same freq but different volt or is unavailable. This
  865. * should be considered an error by the callers of _opp_add().
  866. */
  867. int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
  868. struct opp_table *opp_table, bool rate_not_available)
  869. {
  870. struct list_head *head;
  871. int ret;
  872. mutex_lock(&opp_table->lock);
  873. head = &opp_table->opp_list;
  874. if (likely(!rate_not_available)) {
  875. ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
  876. if (ret) {
  877. mutex_unlock(&opp_table->lock);
  878. return ret;
  879. }
  880. }
  881. list_add(&new_opp->node, head);
  882. mutex_unlock(&opp_table->lock);
  883. new_opp->opp_table = opp_table;
  884. kref_init(&new_opp->kref);
  885. /* Get a reference to the OPP table */
  886. _get_opp_table_kref(opp_table);
  887. ret = opp_debug_create_one(new_opp, opp_table);
  888. if (ret)
  889. dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
  890. __func__, ret);
  891. if (!_opp_supported_by_regulators(new_opp, opp_table)) {
  892. new_opp->available = false;
  893. dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
  894. __func__, new_opp->rate);
  895. }
  896. return 0;
  897. }
  898. /**
  899. * _opp_add_v1() - Allocate a OPP based on v1 bindings.
  900. * @opp_table: OPP table
  901. * @dev: device for which we do this operation
  902. * @freq: Frequency in Hz for this OPP
  903. * @u_volt: Voltage in uVolts for this OPP
  904. * @dynamic: Dynamically added OPPs.
  905. *
  906. * This function adds an opp definition to the opp table and returns status.
  907. * The opp is made available by default and it can be controlled using
  908. * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
  909. *
  910. * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
  911. * and freed by dev_pm_opp_of_remove_table.
  912. *
  913. * Return:
  914. * 0 On success OR
  915. * Duplicate OPPs (both freq and volt are same) and opp->available
  916. * -EEXIST Freq are same and volt are different OR
  917. * Duplicate OPPs (both freq and volt are same) and !opp->available
  918. * -ENOMEM Memory allocation failure
  919. */
  920. int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
  921. unsigned long freq, long u_volt, bool dynamic)
  922. {
  923. struct dev_pm_opp *new_opp;
  924. unsigned long tol;
  925. int ret;
  926. new_opp = _opp_allocate(opp_table);
  927. if (!new_opp)
  928. return -ENOMEM;
  929. /* populate the opp table */
  930. new_opp->rate = freq;
  931. tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
  932. new_opp->supplies[0].u_volt = u_volt;
  933. new_opp->supplies[0].u_volt_min = u_volt - tol;
  934. new_opp->supplies[0].u_volt_max = u_volt + tol;
  935. new_opp->available = true;
  936. new_opp->dynamic = dynamic;
  937. ret = _opp_add(dev, new_opp, opp_table, false);
  938. if (ret) {
  939. /* Don't return error for duplicate OPPs */
  940. if (ret == -EBUSY)
  941. ret = 0;
  942. goto free_opp;
  943. }
  944. /*
  945. * Notify the changes in the availability of the operable
  946. * frequency/voltage list.
  947. */
  948. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
  949. return 0;
  950. free_opp:
  951. _opp_free(new_opp);
  952. return ret;
  953. }
  954. /**
  955. * dev_pm_opp_set_supported_hw() - Set supported platforms
  956. * @dev: Device for which supported-hw has to be set.
  957. * @versions: Array of hierarchy of versions to match.
  958. * @count: Number of elements in the array.
  959. *
  960. * This is required only for the V2 bindings, and it enables a platform to
  961. * specify the hierarchy of versions it supports. OPP layer will then enable
  962. * OPPs, which are available for those versions, based on its 'opp-supported-hw'
  963. * property.
  964. */
  965. struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
  966. const u32 *versions, unsigned int count)
  967. {
  968. struct opp_table *opp_table;
  969. opp_table = dev_pm_opp_get_opp_table(dev);
  970. if (!opp_table)
  971. return ERR_PTR(-ENOMEM);
  972. /* Make sure there are no concurrent readers while updating opp_table */
  973. WARN_ON(!list_empty(&opp_table->opp_list));
  974. /* Another CPU that shares the OPP table has set the property ? */
  975. if (opp_table->supported_hw)
  976. return opp_table;
  977. opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
  978. GFP_KERNEL);
  979. if (!opp_table->supported_hw) {
  980. dev_pm_opp_put_opp_table(opp_table);
  981. return ERR_PTR(-ENOMEM);
  982. }
  983. opp_table->supported_hw_count = count;
  984. return opp_table;
  985. }
  986. EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
  987. /**
  988. * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
  989. * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
  990. *
  991. * This is required only for the V2 bindings, and is called for a matching
  992. * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
  993. * will not be freed.
  994. */
  995. void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
  996. {
  997. /* Make sure there are no concurrent readers while updating opp_table */
  998. WARN_ON(!list_empty(&opp_table->opp_list));
  999. kfree(opp_table->supported_hw);
  1000. opp_table->supported_hw = NULL;
  1001. opp_table->supported_hw_count = 0;
  1002. dev_pm_opp_put_opp_table(opp_table);
  1003. }
  1004. EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
  1005. /**
  1006. * dev_pm_opp_set_prop_name() - Set prop-extn name
  1007. * @dev: Device for which the prop-name has to be set.
  1008. * @name: name to postfix to properties.
  1009. *
  1010. * This is required only for the V2 bindings, and it enables a platform to
  1011. * specify the extn to be used for certain property names. The properties to
  1012. * which the extension will apply are opp-microvolt and opp-microamp. OPP core
  1013. * should postfix the property name with -<name> while looking for them.
  1014. */
  1015. struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
  1016. {
  1017. struct opp_table *opp_table;
  1018. opp_table = dev_pm_opp_get_opp_table(dev);
  1019. if (!opp_table)
  1020. return ERR_PTR(-ENOMEM);
  1021. /* Make sure there are no concurrent readers while updating opp_table */
  1022. WARN_ON(!list_empty(&opp_table->opp_list));
  1023. /* Another CPU that shares the OPP table has set the property ? */
  1024. if (opp_table->prop_name)
  1025. return opp_table;
  1026. opp_table->prop_name = kstrdup(name, GFP_KERNEL);
  1027. if (!opp_table->prop_name) {
  1028. dev_pm_opp_put_opp_table(opp_table);
  1029. return ERR_PTR(-ENOMEM);
  1030. }
  1031. return opp_table;
  1032. }
  1033. EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
  1034. /**
  1035. * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
  1036. * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
  1037. *
  1038. * This is required only for the V2 bindings, and is called for a matching
  1039. * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
  1040. * will not be freed.
  1041. */
  1042. void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
  1043. {
  1044. /* Make sure there are no concurrent readers while updating opp_table */
  1045. WARN_ON(!list_empty(&opp_table->opp_list));
  1046. kfree(opp_table->prop_name);
  1047. opp_table->prop_name = NULL;
  1048. dev_pm_opp_put_opp_table(opp_table);
  1049. }
  1050. EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
  1051. static int _allocate_set_opp_data(struct opp_table *opp_table)
  1052. {
  1053. struct dev_pm_set_opp_data *data;
  1054. int len, count = opp_table->regulator_count;
  1055. if (WARN_ON(!opp_table->regulators))
  1056. return -EINVAL;
  1057. /* space for set_opp_data */
  1058. len = sizeof(*data);
  1059. /* space for old_opp.supplies and new_opp.supplies */
  1060. len += 2 * sizeof(struct dev_pm_opp_supply) * count;
  1061. data = kzalloc(len, GFP_KERNEL);
  1062. if (!data)
  1063. return -ENOMEM;
  1064. data->old_opp.supplies = (void *)(data + 1);
  1065. data->new_opp.supplies = data->old_opp.supplies + count;
  1066. opp_table->set_opp_data = data;
  1067. return 0;
  1068. }
  1069. static void _free_set_opp_data(struct opp_table *opp_table)
  1070. {
  1071. kfree(opp_table->set_opp_data);
  1072. opp_table->set_opp_data = NULL;
  1073. }
  1074. /**
  1075. * dev_pm_opp_set_regulators() - Set regulator names for the device
  1076. * @dev: Device for which regulator name is being set.
  1077. * @names: Array of pointers to the names of the regulator.
  1078. * @count: Number of regulators.
  1079. *
  1080. * In order to support OPP switching, OPP layer needs to know the name of the
  1081. * device's regulators, as the core would be required to switch voltages as
  1082. * well.
  1083. *
  1084. * This must be called before any OPPs are initialized for the device.
  1085. */
  1086. struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
  1087. const char * const names[],
  1088. unsigned int count)
  1089. {
  1090. struct opp_table *opp_table;
  1091. struct regulator *reg;
  1092. int ret, i;
  1093. opp_table = dev_pm_opp_get_opp_table(dev);
  1094. if (!opp_table)
  1095. return ERR_PTR(-ENOMEM);
  1096. /* This should be called before OPPs are initialized */
  1097. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  1098. ret = -EBUSY;
  1099. goto err;
  1100. }
  1101. /* Another CPU that shares the OPP table has set the regulators ? */
  1102. if (opp_table->regulators)
  1103. return opp_table;
  1104. opp_table->regulators = kmalloc_array(count,
  1105. sizeof(*opp_table->regulators),
  1106. GFP_KERNEL);
  1107. if (!opp_table->regulators) {
  1108. ret = -ENOMEM;
  1109. goto err;
  1110. }
  1111. for (i = 0; i < count; i++) {
  1112. reg = regulator_get_optional(dev, names[i]);
  1113. if (IS_ERR(reg)) {
  1114. ret = PTR_ERR(reg);
  1115. if (ret != -EPROBE_DEFER)
  1116. dev_err(dev, "%s: no regulator (%s) found: %d\n",
  1117. __func__, names[i], ret);
  1118. goto free_regulators;
  1119. }
  1120. opp_table->regulators[i] = reg;
  1121. }
  1122. opp_table->regulator_count = count;
  1123. /* Allocate block only once to pass to set_opp() routines */
  1124. ret = _allocate_set_opp_data(opp_table);
  1125. if (ret)
  1126. goto free_regulators;
  1127. return opp_table;
  1128. free_regulators:
  1129. while (i != 0)
  1130. regulator_put(opp_table->regulators[--i]);
  1131. kfree(opp_table->regulators);
  1132. opp_table->regulators = NULL;
  1133. opp_table->regulator_count = -1;
  1134. err:
  1135. dev_pm_opp_put_opp_table(opp_table);
  1136. return ERR_PTR(ret);
  1137. }
  1138. EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
  1139. /**
  1140. * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
  1141. * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
  1142. */
  1143. void dev_pm_opp_put_regulators(struct opp_table *opp_table)
  1144. {
  1145. int i;
  1146. if (!opp_table->regulators)
  1147. goto put_opp_table;
  1148. /* Make sure there are no concurrent readers while updating opp_table */
  1149. WARN_ON(!list_empty(&opp_table->opp_list));
  1150. for (i = opp_table->regulator_count - 1; i >= 0; i--)
  1151. regulator_put(opp_table->regulators[i]);
  1152. _free_set_opp_data(opp_table);
  1153. kfree(opp_table->regulators);
  1154. opp_table->regulators = NULL;
  1155. opp_table->regulator_count = -1;
  1156. put_opp_table:
  1157. dev_pm_opp_put_opp_table(opp_table);
  1158. }
  1159. EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
  1160. /**
  1161. * dev_pm_opp_set_clkname() - Set clk name for the device
  1162. * @dev: Device for which clk name is being set.
  1163. * @name: Clk name.
  1164. *
  1165. * In order to support OPP switching, OPP layer needs to get pointer to the
  1166. * clock for the device. Simple cases work fine without using this routine (i.e.
  1167. * by passing connection-id as NULL), but for a device with multiple clocks
  1168. * available, the OPP core needs to know the exact name of the clk to use.
  1169. *
  1170. * This must be called before any OPPs are initialized for the device.
  1171. */
  1172. struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
  1173. {
  1174. struct opp_table *opp_table;
  1175. int ret;
  1176. opp_table = dev_pm_opp_get_opp_table(dev);
  1177. if (!opp_table)
  1178. return ERR_PTR(-ENOMEM);
  1179. /* This should be called before OPPs are initialized */
  1180. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  1181. ret = -EBUSY;
  1182. goto err;
  1183. }
  1184. /* Already have default clk set, free it */
  1185. if (!IS_ERR(opp_table->clk))
  1186. clk_put(opp_table->clk);
  1187. /* Find clk for the device */
  1188. opp_table->clk = clk_get(dev, name);
  1189. if (IS_ERR(opp_table->clk)) {
  1190. ret = PTR_ERR(opp_table->clk);
  1191. if (ret != -EPROBE_DEFER) {
  1192. dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
  1193. ret);
  1194. }
  1195. goto err;
  1196. }
  1197. return opp_table;
  1198. err:
  1199. dev_pm_opp_put_opp_table(opp_table);
  1200. return ERR_PTR(ret);
  1201. }
  1202. EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
  1203. /**
  1204. * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
  1205. * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
  1206. */
  1207. void dev_pm_opp_put_clkname(struct opp_table *opp_table)
  1208. {
  1209. /* Make sure there are no concurrent readers while updating opp_table */
  1210. WARN_ON(!list_empty(&opp_table->opp_list));
  1211. clk_put(opp_table->clk);
  1212. opp_table->clk = ERR_PTR(-EINVAL);
  1213. dev_pm_opp_put_opp_table(opp_table);
  1214. }
  1215. EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
  1216. /**
  1217. * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
  1218. * @dev: Device for which the helper is getting registered.
  1219. * @set_opp: Custom set OPP helper.
  1220. *
  1221. * This is useful to support complex platforms (like platforms with multiple
  1222. * regulators per device), instead of the generic OPP set rate helper.
  1223. *
  1224. * This must be called before any OPPs are initialized for the device.
  1225. */
  1226. struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
  1227. int (*set_opp)(struct dev_pm_set_opp_data *data))
  1228. {
  1229. struct opp_table *opp_table;
  1230. if (!set_opp)
  1231. return ERR_PTR(-EINVAL);
  1232. opp_table = dev_pm_opp_get_opp_table(dev);
  1233. if (!opp_table)
  1234. return ERR_PTR(-ENOMEM);
  1235. /* This should be called before OPPs are initialized */
  1236. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  1237. dev_pm_opp_put_opp_table(opp_table);
  1238. return ERR_PTR(-EBUSY);
  1239. }
  1240. /* Another CPU that shares the OPP table has set the helper ? */
  1241. if (!opp_table->set_opp)
  1242. opp_table->set_opp = set_opp;
  1243. return opp_table;
  1244. }
  1245. EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
  1246. /**
  1247. * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
  1248. * set_opp helper
  1249. * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
  1250. *
  1251. * Release resources blocked for platform specific set_opp helper.
  1252. */
  1253. void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
  1254. {
  1255. /* Make sure there are no concurrent readers while updating opp_table */
  1256. WARN_ON(!list_empty(&opp_table->opp_list));
  1257. opp_table->set_opp = NULL;
  1258. dev_pm_opp_put_opp_table(opp_table);
  1259. }
  1260. EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
  1261. /**
  1262. * dev_pm_opp_add() - Add an OPP table from a table definitions
  1263. * @dev: device for which we do this operation
  1264. * @freq: Frequency in Hz for this OPP
  1265. * @u_volt: Voltage in uVolts for this OPP
  1266. *
  1267. * This function adds an opp definition to the opp table and returns status.
  1268. * The opp is made available by default and it can be controlled using
  1269. * dev_pm_opp_enable/disable functions.
  1270. *
  1271. * Return:
  1272. * 0 On success OR
  1273. * Duplicate OPPs (both freq and volt are same) and opp->available
  1274. * -EEXIST Freq are same and volt are different OR
  1275. * Duplicate OPPs (both freq and volt are same) and !opp->available
  1276. * -ENOMEM Memory allocation failure
  1277. */
  1278. int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
  1279. {
  1280. struct opp_table *opp_table;
  1281. int ret;
  1282. opp_table = dev_pm_opp_get_opp_table(dev);
  1283. if (!opp_table)
  1284. return -ENOMEM;
  1285. /* Fix regulator count for dynamic OPPs */
  1286. opp_table->regulator_count = 1;
  1287. ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
  1288. dev_pm_opp_put_opp_table(opp_table);
  1289. return ret;
  1290. }
  1291. EXPORT_SYMBOL_GPL(dev_pm_opp_add);
  1292. /**
  1293. * _opp_set_availability() - helper to set the availability of an opp
  1294. * @dev: device for which we do this operation
  1295. * @freq: OPP frequency to modify availability
  1296. * @availability_req: availability status requested for this opp
  1297. *
  1298. * Set the availability of an OPP, opp_{enable,disable} share a common logic
  1299. * which is isolated here.
  1300. *
  1301. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1302. * copy operation, returns 0 if no modification was done OR modification was
  1303. * successful.
  1304. */
  1305. static int _opp_set_availability(struct device *dev, unsigned long freq,
  1306. bool availability_req)
  1307. {
  1308. struct opp_table *opp_table;
  1309. struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
  1310. int r = 0;
  1311. /* Find the opp_table */
  1312. opp_table = _find_opp_table(dev);
  1313. if (IS_ERR(opp_table)) {
  1314. r = PTR_ERR(opp_table);
  1315. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  1316. return r;
  1317. }
  1318. mutex_lock(&opp_table->lock);
  1319. /* Do we have the frequency? */
  1320. list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
  1321. if (tmp_opp->rate == freq) {
  1322. opp = tmp_opp;
  1323. break;
  1324. }
  1325. }
  1326. if (IS_ERR(opp)) {
  1327. r = PTR_ERR(opp);
  1328. goto unlock;
  1329. }
  1330. /* Is update really needed? */
  1331. if (opp->available == availability_req)
  1332. goto unlock;
  1333. opp->available = availability_req;
  1334. dev_pm_opp_get(opp);
  1335. mutex_unlock(&opp_table->lock);
  1336. /* Notify the change of the OPP availability */
  1337. if (availability_req)
  1338. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
  1339. opp);
  1340. else
  1341. blocking_notifier_call_chain(&opp_table->head,
  1342. OPP_EVENT_DISABLE, opp);
  1343. dev_pm_opp_put(opp);
  1344. goto put_table;
  1345. unlock:
  1346. mutex_unlock(&opp_table->lock);
  1347. put_table:
  1348. dev_pm_opp_put_opp_table(opp_table);
  1349. return r;
  1350. }
  1351. /**
  1352. * dev_pm_opp_enable() - Enable a specific OPP
  1353. * @dev: device for which we do this operation
  1354. * @freq: OPP frequency to enable
  1355. *
  1356. * Enables a provided opp. If the operation is valid, this returns 0, else the
  1357. * corresponding error value. It is meant to be used for users an OPP available
  1358. * after being temporarily made unavailable with dev_pm_opp_disable.
  1359. *
  1360. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1361. * copy operation, returns 0 if no modification was done OR modification was
  1362. * successful.
  1363. */
  1364. int dev_pm_opp_enable(struct device *dev, unsigned long freq)
  1365. {
  1366. return _opp_set_availability(dev, freq, true);
  1367. }
  1368. EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
  1369. /**
  1370. * dev_pm_opp_disable() - Disable a specific OPP
  1371. * @dev: device for which we do this operation
  1372. * @freq: OPP frequency to disable
  1373. *
  1374. * Disables a provided opp. If the operation is valid, this returns
  1375. * 0, else the corresponding error value. It is meant to be a temporary
  1376. * control by users to make this OPP not available until the circumstances are
  1377. * right to make it available again (with a call to dev_pm_opp_enable).
  1378. *
  1379. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1380. * copy operation, returns 0 if no modification was done OR modification was
  1381. * successful.
  1382. */
  1383. int dev_pm_opp_disable(struct device *dev, unsigned long freq)
  1384. {
  1385. return _opp_set_availability(dev, freq, false);
  1386. }
  1387. EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
  1388. /**
  1389. * dev_pm_opp_register_notifier() - Register OPP notifier for the device
  1390. * @dev: Device for which notifier needs to be registered
  1391. * @nb: Notifier block to be registered
  1392. *
  1393. * Return: 0 on success or a negative error value.
  1394. */
  1395. int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
  1396. {
  1397. struct opp_table *opp_table;
  1398. int ret;
  1399. opp_table = _find_opp_table(dev);
  1400. if (IS_ERR(opp_table))
  1401. return PTR_ERR(opp_table);
  1402. ret = blocking_notifier_chain_register(&opp_table->head, nb);
  1403. dev_pm_opp_put_opp_table(opp_table);
  1404. return ret;
  1405. }
  1406. EXPORT_SYMBOL(dev_pm_opp_register_notifier);
  1407. /**
  1408. * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
  1409. * @dev: Device for which notifier needs to be unregistered
  1410. * @nb: Notifier block to be unregistered
  1411. *
  1412. * Return: 0 on success or a negative error value.
  1413. */
  1414. int dev_pm_opp_unregister_notifier(struct device *dev,
  1415. struct notifier_block *nb)
  1416. {
  1417. struct opp_table *opp_table;
  1418. int ret;
  1419. opp_table = _find_opp_table(dev);
  1420. if (IS_ERR(opp_table))
  1421. return PTR_ERR(opp_table);
  1422. ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
  1423. dev_pm_opp_put_opp_table(opp_table);
  1424. return ret;
  1425. }
  1426. EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
  1427. /*
  1428. * Free OPPs either created using static entries present in DT or even the
  1429. * dynamically added entries based on remove_all param.
  1430. */
  1431. void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
  1432. bool remove_all)
  1433. {
  1434. struct dev_pm_opp *opp, *tmp;
  1435. /* Find if opp_table manages a single device */
  1436. if (list_is_singular(&opp_table->dev_list)) {
  1437. /* Free static OPPs */
  1438. list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
  1439. if (remove_all || !opp->dynamic)
  1440. dev_pm_opp_put(opp);
  1441. }
  1442. /*
  1443. * The OPP table is getting removed, drop the performance state
  1444. * constraints.
  1445. */
  1446. if (opp_table->genpd_performance_state)
  1447. dev_pm_genpd_set_performance_state(dev, 0);
  1448. } else {
  1449. _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
  1450. }
  1451. }
  1452. void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
  1453. {
  1454. struct opp_table *opp_table;
  1455. /* Check for existing table for 'dev' */
  1456. opp_table = _find_opp_table(dev);
  1457. if (IS_ERR(opp_table)) {
  1458. int error = PTR_ERR(opp_table);
  1459. if (error != -ENODEV)
  1460. WARN(1, "%s: opp_table: %d\n",
  1461. IS_ERR_OR_NULL(dev) ?
  1462. "Invalid device" : dev_name(dev),
  1463. error);
  1464. return;
  1465. }
  1466. _dev_pm_opp_remove_table(opp_table, dev, remove_all);
  1467. dev_pm_opp_put_opp_table(opp_table);
  1468. }
  1469. /**
  1470. * dev_pm_opp_remove_table() - Free all OPPs associated with the device
  1471. * @dev: device pointer used to lookup OPP table.
  1472. *
  1473. * Free both OPPs created using static entries present in DT and the
  1474. * dynamically added entries.
  1475. */
  1476. void dev_pm_opp_remove_table(struct device *dev)
  1477. {
  1478. _dev_pm_opp_find_and_remove_table(dev, true);
  1479. }
  1480. EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);