core.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic OPP Interface
  4. *
  5. * Copyright (C) 2009-2010 Texas Instruments Incorporated.
  6. * Nishanth Menon
  7. * Romit Dasgupta
  8. * Kevin Hilman
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/clk.h>
  12. #include <linux/errno.h>
  13. #include <linux/err.h>
  14. #include <linux/device.h>
  15. #include <linux/export.h>
  16. #include <linux/pm_domain.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/slab.h>
  19. #include <linux/xarray.h>
  20. #include "opp.h"
  21. /*
  22. * The root of the list of all opp-tables. All opp_table structures branch off
  23. * from here, with each opp_table containing the list of opps it supports in
  24. * various states of availability.
  25. */
  26. LIST_HEAD(opp_tables);
  27. /* OPP tables with uninitialized required OPPs */
  28. LIST_HEAD(lazy_opp_tables);
  29. /* Lock to allow exclusive modification to the device and opp lists */
  30. DEFINE_MUTEX(opp_table_lock);
  31. /* Flag indicating that opp_tables list is being updated at the moment */
  32. static bool opp_tables_busy;
  33. /* OPP ID allocator */
  34. static DEFINE_XARRAY_ALLOC1(opp_configs);
  35. static bool _find_opp_dev(const struct device *dev, struct opp_table *opp_table)
  36. {
  37. struct opp_device *opp_dev;
  38. bool found = false;
  39. mutex_lock(&opp_table->lock);
  40. list_for_each_entry(opp_dev, &opp_table->dev_list, node)
  41. if (opp_dev->dev == dev) {
  42. found = true;
  43. break;
  44. }
  45. mutex_unlock(&opp_table->lock);
  46. return found;
  47. }
  48. static struct opp_table *_find_opp_table_unlocked(struct device *dev)
  49. {
  50. struct opp_table *opp_table;
  51. list_for_each_entry(opp_table, &opp_tables, node) {
  52. if (_find_opp_dev(dev, opp_table)) {
  53. _get_opp_table_kref(opp_table);
  54. return opp_table;
  55. }
  56. }
  57. return ERR_PTR(-ENODEV);
  58. }
  59. /**
  60. * _find_opp_table() - find opp_table struct using device pointer
  61. * @dev: device pointer used to lookup OPP table
  62. *
  63. * Search OPP table for one containing matching device.
  64. *
  65. * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
  66. * -EINVAL based on type of error.
  67. *
  68. * The callers must call dev_pm_opp_put_opp_table() after the table is used.
  69. */
  70. struct opp_table *_find_opp_table(struct device *dev)
  71. {
  72. struct opp_table *opp_table;
  73. if (IS_ERR_OR_NULL(dev)) {
  74. pr_err("%s: Invalid parameters\n", __func__);
  75. return ERR_PTR(-EINVAL);
  76. }
  77. mutex_lock(&opp_table_lock);
  78. opp_table = _find_opp_table_unlocked(dev);
  79. mutex_unlock(&opp_table_lock);
  80. return opp_table;
  81. }
  82. /*
  83. * Returns true if multiple clocks aren't there, else returns false with WARN.
  84. *
  85. * We don't force clk_count == 1 here as there are users who don't have a clock
  86. * representation in the OPP table and manage the clock configuration themselves
  87. * in an platform specific way.
  88. */
  89. static bool assert_single_clk(struct opp_table *opp_table)
  90. {
  91. return !WARN_ON(opp_table->clk_count > 1);
  92. }
  93. /**
  94. * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
  95. * @opp: opp for which voltage has to be returned for
  96. *
  97. * Return: voltage in micro volt corresponding to the opp, else
  98. * return 0
  99. *
  100. * This is useful only for devices with single power supply.
  101. */
  102. unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
  103. {
  104. if (IS_ERR_OR_NULL(opp)) {
  105. pr_err("%s: Invalid parameters\n", __func__);
  106. return 0;
  107. }
  108. return opp->supplies[0].u_volt;
  109. }
  110. EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
  111. /**
  112. * dev_pm_opp_get_supplies() - Gets the supply information corresponding to an opp
  113. * @opp: opp for which voltage has to be returned for
  114. * @supplies: Placeholder for copying the supply information.
  115. *
  116. * Return: negative error number on failure, 0 otherwise on success after
  117. * setting @supplies.
  118. *
  119. * This can be used for devices with any number of power supplies. The caller
  120. * must ensure the @supplies array must contain space for each regulator.
  121. */
  122. int dev_pm_opp_get_supplies(struct dev_pm_opp *opp,
  123. struct dev_pm_opp_supply *supplies)
  124. {
  125. if (IS_ERR_OR_NULL(opp) || !supplies) {
  126. pr_err("%s: Invalid parameters\n", __func__);
  127. return -EINVAL;
  128. }
  129. memcpy(supplies, opp->supplies,
  130. sizeof(*supplies) * opp->opp_table->regulator_count);
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(dev_pm_opp_get_supplies);
  134. /**
  135. * dev_pm_opp_get_power() - Gets the power corresponding to an opp
  136. * @opp: opp for which power has to be returned for
  137. *
  138. * Return: power in micro watt corresponding to the opp, else
  139. * return 0
  140. *
  141. * This is useful only for devices with single power supply.
  142. */
  143. unsigned long dev_pm_opp_get_power(struct dev_pm_opp *opp)
  144. {
  145. unsigned long opp_power = 0;
  146. int i;
  147. if (IS_ERR_OR_NULL(opp)) {
  148. pr_err("%s: Invalid parameters\n", __func__);
  149. return 0;
  150. }
  151. for (i = 0; i < opp->opp_table->regulator_count; i++)
  152. opp_power += opp->supplies[i].u_watt;
  153. return opp_power;
  154. }
  155. EXPORT_SYMBOL_GPL(dev_pm_opp_get_power);
  156. /**
  157. * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
  158. * @opp: opp for which frequency has to be returned for
  159. *
  160. * Return: frequency in hertz corresponding to the opp, else
  161. * return 0
  162. */
  163. unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
  164. {
  165. if (IS_ERR_OR_NULL(opp)) {
  166. pr_err("%s: Invalid parameters\n", __func__);
  167. return 0;
  168. }
  169. if (!assert_single_clk(opp->opp_table))
  170. return 0;
  171. return opp->rates[0];
  172. }
  173. EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
  174. /**
  175. * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
  176. * @opp: opp for which level value has to be returned for
  177. *
  178. * Return: level read from device tree corresponding to the opp, else
  179. * return 0.
  180. */
  181. unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
  182. {
  183. if (IS_ERR_OR_NULL(opp) || !opp->available) {
  184. pr_err("%s: Invalid parameters\n", __func__);
  185. return 0;
  186. }
  187. return opp->level;
  188. }
  189. EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
  190. /**
  191. * dev_pm_opp_get_required_pstate() - Gets the required performance state
  192. * corresponding to an available opp
  193. * @opp: opp for which performance state has to be returned for
  194. * @index: index of the required opp
  195. *
  196. * Return: performance state read from device tree corresponding to the
  197. * required opp, else return 0.
  198. */
  199. unsigned int dev_pm_opp_get_required_pstate(struct dev_pm_opp *opp,
  200. unsigned int index)
  201. {
  202. if (IS_ERR_OR_NULL(opp) || !opp->available ||
  203. index >= opp->opp_table->required_opp_count) {
  204. pr_err("%s: Invalid parameters\n", __func__);
  205. return 0;
  206. }
  207. /* required-opps not fully initialized yet */
  208. if (lazy_linking_pending(opp->opp_table))
  209. return 0;
  210. return opp->required_opps[index]->pstate;
  211. }
  212. EXPORT_SYMBOL_GPL(dev_pm_opp_get_required_pstate);
  213. /**
  214. * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
  215. * @opp: opp for which turbo mode is being verified
  216. *
  217. * Turbo OPPs are not for normal use, and can be enabled (under certain
  218. * conditions) for short duration of times to finish high throughput work
  219. * quickly. Running on them for longer times may overheat the chip.
  220. *
  221. * Return: true if opp is turbo opp, else false.
  222. */
  223. bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
  224. {
  225. if (IS_ERR_OR_NULL(opp) || !opp->available) {
  226. pr_err("%s: Invalid parameters\n", __func__);
  227. return false;
  228. }
  229. return opp->turbo;
  230. }
  231. EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
  232. /**
  233. * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
  234. * @dev: device for which we do this operation
  235. *
  236. * Return: This function returns the max clock latency in nanoseconds.
  237. */
  238. unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
  239. {
  240. struct opp_table *opp_table;
  241. unsigned long clock_latency_ns;
  242. opp_table = _find_opp_table(dev);
  243. if (IS_ERR(opp_table))
  244. return 0;
  245. clock_latency_ns = opp_table->clock_latency_ns_max;
  246. dev_pm_opp_put_opp_table(opp_table);
  247. return clock_latency_ns;
  248. }
  249. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
  250. /**
  251. * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
  252. * @dev: device for which we do this operation
  253. *
  254. * Return: This function returns the max voltage latency in nanoseconds.
  255. */
  256. unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
  257. {
  258. struct opp_table *opp_table;
  259. struct dev_pm_opp *opp;
  260. struct regulator *reg;
  261. unsigned long latency_ns = 0;
  262. int ret, i, count;
  263. struct {
  264. unsigned long min;
  265. unsigned long max;
  266. } *uV;
  267. opp_table = _find_opp_table(dev);
  268. if (IS_ERR(opp_table))
  269. return 0;
  270. /* Regulator may not be required for the device */
  271. if (!opp_table->regulators)
  272. goto put_opp_table;
  273. count = opp_table->regulator_count;
  274. uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
  275. if (!uV)
  276. goto put_opp_table;
  277. mutex_lock(&opp_table->lock);
  278. for (i = 0; i < count; i++) {
  279. uV[i].min = ~0;
  280. uV[i].max = 0;
  281. list_for_each_entry(opp, &opp_table->opp_list, node) {
  282. if (!opp->available)
  283. continue;
  284. if (opp->supplies[i].u_volt_min < uV[i].min)
  285. uV[i].min = opp->supplies[i].u_volt_min;
  286. if (opp->supplies[i].u_volt_max > uV[i].max)
  287. uV[i].max = opp->supplies[i].u_volt_max;
  288. }
  289. }
  290. mutex_unlock(&opp_table->lock);
  291. /*
  292. * The caller needs to ensure that opp_table (and hence the regulator)
  293. * isn't freed, while we are executing this routine.
  294. */
  295. for (i = 0; i < count; i++) {
  296. reg = opp_table->regulators[i];
  297. ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
  298. if (ret > 0)
  299. latency_ns += ret * 1000;
  300. }
  301. kfree(uV);
  302. put_opp_table:
  303. dev_pm_opp_put_opp_table(opp_table);
  304. return latency_ns;
  305. }
  306. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
  307. /**
  308. * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
  309. * nanoseconds
  310. * @dev: device for which we do this operation
  311. *
  312. * Return: This function returns the max transition latency, in nanoseconds, to
  313. * switch from one OPP to other.
  314. */
  315. unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
  316. {
  317. return dev_pm_opp_get_max_volt_latency(dev) +
  318. dev_pm_opp_get_max_clock_latency(dev);
  319. }
  320. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
  321. /**
  322. * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
  323. * @dev: device for which we do this operation
  324. *
  325. * Return: This function returns the frequency of the OPP marked as suspend_opp
  326. * if one is available, else returns 0;
  327. */
  328. unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
  329. {
  330. struct opp_table *opp_table;
  331. unsigned long freq = 0;
  332. opp_table = _find_opp_table(dev);
  333. if (IS_ERR(opp_table))
  334. return 0;
  335. if (opp_table->suspend_opp && opp_table->suspend_opp->available)
  336. freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
  337. dev_pm_opp_put_opp_table(opp_table);
  338. return freq;
  339. }
  340. EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
  341. int _get_opp_count(struct opp_table *opp_table)
  342. {
  343. struct dev_pm_opp *opp;
  344. int count = 0;
  345. mutex_lock(&opp_table->lock);
  346. list_for_each_entry(opp, &opp_table->opp_list, node) {
  347. if (opp->available)
  348. count++;
  349. }
  350. mutex_unlock(&opp_table->lock);
  351. return count;
  352. }
  353. /**
  354. * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
  355. * @dev: device for which we do this operation
  356. *
  357. * Return: This function returns the number of available opps if there are any,
  358. * else returns 0 if none or the corresponding error value.
  359. */
  360. int dev_pm_opp_get_opp_count(struct device *dev)
  361. {
  362. struct opp_table *opp_table;
  363. int count;
  364. opp_table = _find_opp_table(dev);
  365. if (IS_ERR(opp_table)) {
  366. count = PTR_ERR(opp_table);
  367. dev_dbg(dev, "%s: OPP table not found (%d)\n",
  368. __func__, count);
  369. return count;
  370. }
  371. count = _get_opp_count(opp_table);
  372. dev_pm_opp_put_opp_table(opp_table);
  373. return count;
  374. }
  375. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
  376. /* Helpers to read keys */
  377. static unsigned long _read_freq(struct dev_pm_opp *opp, int index)
  378. {
  379. return opp->rates[0];
  380. }
  381. static unsigned long _read_level(struct dev_pm_opp *opp, int index)
  382. {
  383. return opp->level;
  384. }
  385. static unsigned long _read_bw(struct dev_pm_opp *opp, int index)
  386. {
  387. return opp->bandwidth[index].peak;
  388. }
  389. /* Generic comparison helpers */
  390. static bool _compare_exact(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
  391. unsigned long opp_key, unsigned long key)
  392. {
  393. if (opp_key == key) {
  394. *opp = temp_opp;
  395. return true;
  396. }
  397. return false;
  398. }
  399. static bool _compare_ceil(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
  400. unsigned long opp_key, unsigned long key)
  401. {
  402. if (opp_key >= key) {
  403. *opp = temp_opp;
  404. return true;
  405. }
  406. return false;
  407. }
  408. static bool _compare_floor(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
  409. unsigned long opp_key, unsigned long key)
  410. {
  411. if (opp_key > key)
  412. return true;
  413. *opp = temp_opp;
  414. return false;
  415. }
  416. /* Generic key finding helpers */
  417. static struct dev_pm_opp *_opp_table_find_key(struct opp_table *opp_table,
  418. unsigned long *key, int index, bool available,
  419. unsigned long (*read)(struct dev_pm_opp *opp, int index),
  420. bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
  421. unsigned long opp_key, unsigned long key),
  422. bool (*assert)(struct opp_table *opp_table))
  423. {
  424. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  425. /* Assert that the requirement is met */
  426. if (assert && !assert(opp_table))
  427. return ERR_PTR(-EINVAL);
  428. mutex_lock(&opp_table->lock);
  429. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  430. if (temp_opp->available == available) {
  431. if (compare(&opp, temp_opp, read(temp_opp, index), *key))
  432. break;
  433. }
  434. }
  435. /* Increment the reference count of OPP */
  436. if (!IS_ERR(opp)) {
  437. *key = read(opp, index);
  438. dev_pm_opp_get(opp);
  439. }
  440. mutex_unlock(&opp_table->lock);
  441. return opp;
  442. }
  443. static struct dev_pm_opp *
  444. _find_key(struct device *dev, unsigned long *key, int index, bool available,
  445. unsigned long (*read)(struct dev_pm_opp *opp, int index),
  446. bool (*compare)(struct dev_pm_opp **opp, struct dev_pm_opp *temp_opp,
  447. unsigned long opp_key, unsigned long key),
  448. bool (*assert)(struct opp_table *opp_table))
  449. {
  450. struct opp_table *opp_table;
  451. struct dev_pm_opp *opp;
  452. opp_table = _find_opp_table(dev);
  453. if (IS_ERR(opp_table)) {
  454. dev_err(dev, "%s: OPP table not found (%ld)\n", __func__,
  455. PTR_ERR(opp_table));
  456. return ERR_CAST(opp_table);
  457. }
  458. opp = _opp_table_find_key(opp_table, key, index, available, read,
  459. compare, assert);
  460. dev_pm_opp_put_opp_table(opp_table);
  461. return opp;
  462. }
  463. static struct dev_pm_opp *_find_key_exact(struct device *dev,
  464. unsigned long key, int index, bool available,
  465. unsigned long (*read)(struct dev_pm_opp *opp, int index),
  466. bool (*assert)(struct opp_table *opp_table))
  467. {
  468. /*
  469. * The value of key will be updated here, but will be ignored as the
  470. * caller doesn't need it.
  471. */
  472. return _find_key(dev, &key, index, available, read, _compare_exact,
  473. assert);
  474. }
  475. static struct dev_pm_opp *_opp_table_find_key_ceil(struct opp_table *opp_table,
  476. unsigned long *key, int index, bool available,
  477. unsigned long (*read)(struct dev_pm_opp *opp, int index),
  478. bool (*assert)(struct opp_table *opp_table))
  479. {
  480. return _opp_table_find_key(opp_table, key, index, available, read,
  481. _compare_ceil, assert);
  482. }
  483. static struct dev_pm_opp *_find_key_ceil(struct device *dev, unsigned long *key,
  484. int index, bool available,
  485. unsigned long (*read)(struct dev_pm_opp *opp, int index),
  486. bool (*assert)(struct opp_table *opp_table))
  487. {
  488. return _find_key(dev, key, index, available, read, _compare_ceil,
  489. assert);
  490. }
  491. static struct dev_pm_opp *_find_key_floor(struct device *dev,
  492. unsigned long *key, int index, bool available,
  493. unsigned long (*read)(struct dev_pm_opp *opp, int index),
  494. bool (*assert)(struct opp_table *opp_table))
  495. {
  496. return _find_key(dev, key, index, available, read, _compare_floor,
  497. assert);
  498. }
  499. /**
  500. * dev_pm_opp_find_freq_exact() - search for an exact frequency
  501. * @dev: device for which we do this operation
  502. * @freq: frequency to search for
  503. * @available: true/false - match for available opp
  504. *
  505. * Return: Searches for exact match in the opp table and returns pointer to the
  506. * matching opp if found, else returns ERR_PTR in case of error and should
  507. * be handled using IS_ERR. Error return values can be:
  508. * EINVAL: for bad pointer
  509. * ERANGE: no match found for search
  510. * ENODEV: if device not found in list of registered devices
  511. *
  512. * Note: available is a modifier for the search. if available=true, then the
  513. * match is for exact matching frequency and is available in the stored OPP
  514. * table. if false, the match is for exact frequency which is not available.
  515. *
  516. * This provides a mechanism to enable an opp which is not available currently
  517. * or the opposite as well.
  518. *
  519. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  520. * use.
  521. */
  522. struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
  523. unsigned long freq, bool available)
  524. {
  525. return _find_key_exact(dev, freq, 0, available, _read_freq,
  526. assert_single_clk);
  527. }
  528. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
  529. static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
  530. unsigned long *freq)
  531. {
  532. return _opp_table_find_key_ceil(opp_table, freq, 0, true, _read_freq,
  533. assert_single_clk);
  534. }
  535. /**
  536. * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
  537. * @dev: device for which we do this operation
  538. * @freq: Start frequency
  539. *
  540. * Search for the matching ceil *available* OPP from a starting freq
  541. * for a device.
  542. *
  543. * Return: matching *opp and refreshes *freq accordingly, else returns
  544. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  545. * values can be:
  546. * EINVAL: for bad pointer
  547. * ERANGE: no match found for search
  548. * ENODEV: if device not found in list of registered devices
  549. *
  550. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  551. * use.
  552. */
  553. struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
  554. unsigned long *freq)
  555. {
  556. return _find_key_ceil(dev, freq, 0, true, _read_freq, assert_single_clk);
  557. }
  558. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
  559. /**
  560. * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
  561. * @dev: device for which we do this operation
  562. * @freq: Start frequency
  563. *
  564. * Search for the matching floor *available* OPP from a starting freq
  565. * for a device.
  566. *
  567. * Return: matching *opp and refreshes *freq accordingly, else returns
  568. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  569. * values can be:
  570. * EINVAL: for bad pointer
  571. * ERANGE: no match found for search
  572. * ENODEV: if device not found in list of registered devices
  573. *
  574. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  575. * use.
  576. */
  577. struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
  578. unsigned long *freq)
  579. {
  580. return _find_key_floor(dev, freq, 0, true, _read_freq, assert_single_clk);
  581. }
  582. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
  583. /**
  584. * dev_pm_opp_find_level_exact() - search for an exact level
  585. * @dev: device for which we do this operation
  586. * @level: level to search for
  587. *
  588. * Return: Searches for exact match in the opp table and returns pointer to the
  589. * matching opp if found, else returns ERR_PTR in case of error and should
  590. * be handled using IS_ERR. Error return values can be:
  591. * EINVAL: for bad pointer
  592. * ERANGE: no match found for search
  593. * ENODEV: if device not found in list of registered devices
  594. *
  595. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  596. * use.
  597. */
  598. struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
  599. unsigned int level)
  600. {
  601. return _find_key_exact(dev, level, 0, true, _read_level, NULL);
  602. }
  603. EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
  604. /**
  605. * dev_pm_opp_find_level_ceil() - search for an rounded up level
  606. * @dev: device for which we do this operation
  607. * @level: level to search for
  608. *
  609. * Return: Searches for rounded up match in the opp table and returns pointer
  610. * to the matching opp if found, else returns ERR_PTR in case of error and
  611. * should be handled using IS_ERR. Error return values can be:
  612. * EINVAL: for bad pointer
  613. * ERANGE: no match found for search
  614. * ENODEV: if device not found in list of registered devices
  615. *
  616. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  617. * use.
  618. */
  619. struct dev_pm_opp *dev_pm_opp_find_level_ceil(struct device *dev,
  620. unsigned int *level)
  621. {
  622. unsigned long temp = *level;
  623. struct dev_pm_opp *opp;
  624. opp = _find_key_ceil(dev, &temp, 0, true, _read_level, NULL);
  625. *level = temp;
  626. return opp;
  627. }
  628. EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_ceil);
  629. /**
  630. * dev_pm_opp_find_bw_ceil() - Search for a rounded ceil bandwidth
  631. * @dev: device for which we do this operation
  632. * @bw: start bandwidth
  633. * @index: which bandwidth to compare, in case of OPPs with several values
  634. *
  635. * Search for the matching floor *available* OPP from a starting bandwidth
  636. * for a device.
  637. *
  638. * Return: matching *opp and refreshes *bw accordingly, else returns
  639. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  640. * values can be:
  641. * EINVAL: for bad pointer
  642. * ERANGE: no match found for search
  643. * ENODEV: if device not found in list of registered devices
  644. *
  645. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  646. * use.
  647. */
  648. struct dev_pm_opp *dev_pm_opp_find_bw_ceil(struct device *dev, unsigned int *bw,
  649. int index)
  650. {
  651. unsigned long temp = *bw;
  652. struct dev_pm_opp *opp;
  653. opp = _find_key_ceil(dev, &temp, index, true, _read_bw, NULL);
  654. *bw = temp;
  655. return opp;
  656. }
  657. EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_ceil);
  658. /**
  659. * dev_pm_opp_find_bw_floor() - Search for a rounded floor bandwidth
  660. * @dev: device for which we do this operation
  661. * @bw: start bandwidth
  662. * @index: which bandwidth to compare, in case of OPPs with several values
  663. *
  664. * Search for the matching floor *available* OPP from a starting bandwidth
  665. * for a device.
  666. *
  667. * Return: matching *opp and refreshes *bw accordingly, else returns
  668. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  669. * values can be:
  670. * EINVAL: for bad pointer
  671. * ERANGE: no match found for search
  672. * ENODEV: if device not found in list of registered devices
  673. *
  674. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  675. * use.
  676. */
  677. struct dev_pm_opp *dev_pm_opp_find_bw_floor(struct device *dev,
  678. unsigned int *bw, int index)
  679. {
  680. unsigned long temp = *bw;
  681. struct dev_pm_opp *opp;
  682. opp = _find_key_floor(dev, &temp, index, true, _read_bw, NULL);
  683. *bw = temp;
  684. return opp;
  685. }
  686. EXPORT_SYMBOL_GPL(dev_pm_opp_find_bw_floor);
  687. static int _set_opp_voltage(struct device *dev, struct regulator *reg,
  688. struct dev_pm_opp_supply *supply)
  689. {
  690. int ret;
  691. /* Regulator not available for device */
  692. if (IS_ERR(reg)) {
  693. dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
  694. PTR_ERR(reg));
  695. return 0;
  696. }
  697. dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
  698. supply->u_volt_min, supply->u_volt, supply->u_volt_max);
  699. ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
  700. supply->u_volt, supply->u_volt_max);
  701. if (ret)
  702. dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
  703. __func__, supply->u_volt_min, supply->u_volt,
  704. supply->u_volt_max, ret);
  705. return ret;
  706. }
  707. static int
  708. _opp_config_clk_single(struct device *dev, struct opp_table *opp_table,
  709. struct dev_pm_opp *opp, void *data, bool scaling_down)
  710. {
  711. unsigned long *target = data;
  712. unsigned long freq;
  713. int ret;
  714. /* One of target and opp must be available */
  715. if (target) {
  716. freq = *target;
  717. } else if (opp) {
  718. freq = opp->rates[0];
  719. } else {
  720. WARN_ON(1);
  721. return -EINVAL;
  722. }
  723. ret = clk_set_rate(opp_table->clk, freq);
  724. if (ret) {
  725. dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
  726. ret);
  727. } else {
  728. opp_table->rate_clk_single = freq;
  729. }
  730. return ret;
  731. }
  732. /*
  733. * Simple implementation for configuring multiple clocks. Configure clocks in
  734. * the order in which they are present in the array while scaling up.
  735. */
  736. int dev_pm_opp_config_clks_simple(struct device *dev,
  737. struct opp_table *opp_table, struct dev_pm_opp *opp, void *data,
  738. bool scaling_down)
  739. {
  740. int ret, i;
  741. if (scaling_down) {
  742. for (i = opp_table->clk_count - 1; i >= 0; i--) {
  743. ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
  744. if (ret) {
  745. dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
  746. ret);
  747. return ret;
  748. }
  749. }
  750. } else {
  751. for (i = 0; i < opp_table->clk_count; i++) {
  752. ret = clk_set_rate(opp_table->clks[i], opp->rates[i]);
  753. if (ret) {
  754. dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
  755. ret);
  756. return ret;
  757. }
  758. }
  759. }
  760. return 0;
  761. }
  762. EXPORT_SYMBOL_GPL(dev_pm_opp_config_clks_simple);
  763. static int _opp_config_regulator_single(struct device *dev,
  764. struct dev_pm_opp *old_opp, struct dev_pm_opp *new_opp,
  765. struct regulator **regulators, unsigned int count)
  766. {
  767. struct regulator *reg = regulators[0];
  768. int ret;
  769. /* This function only supports single regulator per device */
  770. if (WARN_ON(count > 1)) {
  771. dev_err(dev, "multiple regulators are not supported\n");
  772. return -EINVAL;
  773. }
  774. ret = _set_opp_voltage(dev, reg, new_opp->supplies);
  775. if (ret)
  776. return ret;
  777. /*
  778. * Enable the regulator after setting its voltages, otherwise it breaks
  779. * some boot-enabled regulators.
  780. */
  781. if (unlikely(!new_opp->opp_table->enabled)) {
  782. ret = regulator_enable(reg);
  783. if (ret < 0)
  784. dev_warn(dev, "Failed to enable regulator: %d", ret);
  785. }
  786. return 0;
  787. }
  788. static int _set_opp_bw(const struct opp_table *opp_table,
  789. struct dev_pm_opp *opp, struct device *dev)
  790. {
  791. u32 avg, peak;
  792. int i, ret;
  793. if (!opp_table->paths)
  794. return 0;
  795. for (i = 0; i < opp_table->path_count; i++) {
  796. if (!opp) {
  797. avg = 0;
  798. peak = 0;
  799. } else {
  800. avg = opp->bandwidth[i].avg;
  801. peak = opp->bandwidth[i].peak;
  802. }
  803. ret = icc_set_bw(opp_table->paths[i], avg, peak);
  804. if (ret) {
  805. dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
  806. opp ? "set" : "remove", i, ret);
  807. return ret;
  808. }
  809. }
  810. return 0;
  811. }
  812. static int _set_required_opp(struct device *dev, struct device *pd_dev,
  813. struct dev_pm_opp *opp, int i)
  814. {
  815. unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
  816. int ret;
  817. if (!pd_dev)
  818. return 0;
  819. ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
  820. if (ret) {
  821. dev_err(dev, "Failed to set performance state of %s: %d (%d)\n",
  822. dev_name(pd_dev), pstate, ret);
  823. }
  824. return ret;
  825. }
  826. /* This is only called for PM domain for now */
  827. static int _set_required_opps(struct device *dev,
  828. struct opp_table *opp_table,
  829. struct dev_pm_opp *opp, bool up)
  830. {
  831. struct opp_table **required_opp_tables = opp_table->required_opp_tables;
  832. struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
  833. int i, ret = 0;
  834. if (!required_opp_tables)
  835. return 0;
  836. /* required-opps not fully initialized yet */
  837. if (lazy_linking_pending(opp_table))
  838. return -EBUSY;
  839. /*
  840. * We only support genpd's OPPs in the "required-opps" for now, as we
  841. * don't know much about other use cases. Error out if the required OPP
  842. * doesn't belong to a genpd.
  843. */
  844. if (unlikely(!required_opp_tables[0]->is_genpd)) {
  845. dev_err(dev, "required-opps don't belong to a genpd\n");
  846. return -ENOENT;
  847. }
  848. /* Single genpd case */
  849. if (!genpd_virt_devs)
  850. return _set_required_opp(dev, dev, opp, 0);
  851. /* Multiple genpd case */
  852. /*
  853. * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
  854. * after it is freed from another thread.
  855. */
  856. mutex_lock(&opp_table->genpd_virt_dev_lock);
  857. /* Scaling up? Set required OPPs in normal order, else reverse */
  858. if (up) {
  859. for (i = 0; i < opp_table->required_opp_count; i++) {
  860. ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
  861. if (ret)
  862. break;
  863. }
  864. } else {
  865. for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
  866. ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
  867. if (ret)
  868. break;
  869. }
  870. }
  871. mutex_unlock(&opp_table->genpd_virt_dev_lock);
  872. return ret;
  873. }
  874. static void _find_current_opp(struct device *dev, struct opp_table *opp_table)
  875. {
  876. struct dev_pm_opp *opp = ERR_PTR(-ENODEV);
  877. unsigned long freq;
  878. if (!IS_ERR(opp_table->clk)) {
  879. freq = clk_get_rate(opp_table->clk);
  880. opp = _find_freq_ceil(opp_table, &freq);
  881. }
  882. /*
  883. * Unable to find the current OPP ? Pick the first from the list since
  884. * it is in ascending order, otherwise rest of the code will need to
  885. * make special checks to validate current_opp.
  886. */
  887. if (IS_ERR(opp)) {
  888. mutex_lock(&opp_table->lock);
  889. opp = list_first_entry(&opp_table->opp_list, struct dev_pm_opp, node);
  890. dev_pm_opp_get(opp);
  891. mutex_unlock(&opp_table->lock);
  892. }
  893. opp_table->current_opp = opp;
  894. }
  895. static int _disable_opp_table(struct device *dev, struct opp_table *opp_table)
  896. {
  897. int ret;
  898. if (!opp_table->enabled)
  899. return 0;
  900. /*
  901. * Some drivers need to support cases where some platforms may
  902. * have OPP table for the device, while others don't and
  903. * opp_set_rate() just needs to behave like clk_set_rate().
  904. */
  905. if (!_get_opp_count(opp_table))
  906. return 0;
  907. ret = _set_opp_bw(opp_table, NULL, dev);
  908. if (ret)
  909. return ret;
  910. if (opp_table->regulators)
  911. regulator_disable(opp_table->regulators[0]);
  912. ret = _set_required_opps(dev, opp_table, NULL, false);
  913. opp_table->enabled = false;
  914. return ret;
  915. }
  916. static int _set_opp(struct device *dev, struct opp_table *opp_table,
  917. struct dev_pm_opp *opp, void *clk_data, bool forced)
  918. {
  919. struct dev_pm_opp *old_opp;
  920. int scaling_down, ret;
  921. if (unlikely(!opp))
  922. return _disable_opp_table(dev, opp_table);
  923. /* Find the currently set OPP if we don't know already */
  924. if (unlikely(!opp_table->current_opp))
  925. _find_current_opp(dev, opp_table);
  926. old_opp = opp_table->current_opp;
  927. /* Return early if nothing to do */
  928. if (!forced && old_opp == opp && opp_table->enabled) {
  929. dev_dbg(dev, "%s: OPPs are same, nothing to do\n", __func__);
  930. return 0;
  931. }
  932. dev_dbg(dev, "%s: switching OPP: Freq %lu -> %lu Hz, Level %u -> %u, Bw %u -> %u\n",
  933. __func__, old_opp->rates[0], opp->rates[0], old_opp->level,
  934. opp->level, old_opp->bandwidth ? old_opp->bandwidth[0].peak : 0,
  935. opp->bandwidth ? opp->bandwidth[0].peak : 0);
  936. scaling_down = _opp_compare_key(opp_table, old_opp, opp);
  937. if (scaling_down == -1)
  938. scaling_down = 0;
  939. /* Scaling up? Configure required OPPs before frequency */
  940. if (!scaling_down) {
  941. ret = _set_required_opps(dev, opp_table, opp, true);
  942. if (ret) {
  943. dev_err(dev, "Failed to set required opps: %d\n", ret);
  944. return ret;
  945. }
  946. ret = _set_opp_bw(opp_table, opp, dev);
  947. if (ret) {
  948. dev_err(dev, "Failed to set bw: %d\n", ret);
  949. return ret;
  950. }
  951. if (opp_table->config_regulators) {
  952. ret = opp_table->config_regulators(dev, old_opp, opp,
  953. opp_table->regulators,
  954. opp_table->regulator_count);
  955. if (ret) {
  956. dev_err(dev, "Failed to set regulator voltages: %d\n",
  957. ret);
  958. return ret;
  959. }
  960. }
  961. }
  962. if (opp_table->config_clks) {
  963. ret = opp_table->config_clks(dev, opp_table, opp, clk_data, scaling_down);
  964. if (ret)
  965. return ret;
  966. }
  967. /* Scaling down? Configure required OPPs after frequency */
  968. if (scaling_down) {
  969. if (opp_table->config_regulators) {
  970. ret = opp_table->config_regulators(dev, old_opp, opp,
  971. opp_table->regulators,
  972. opp_table->regulator_count);
  973. if (ret) {
  974. dev_err(dev, "Failed to set regulator voltages: %d\n",
  975. ret);
  976. return ret;
  977. }
  978. }
  979. ret = _set_opp_bw(opp_table, opp, dev);
  980. if (ret) {
  981. dev_err(dev, "Failed to set bw: %d\n", ret);
  982. return ret;
  983. }
  984. ret = _set_required_opps(dev, opp_table, opp, false);
  985. if (ret) {
  986. dev_err(dev, "Failed to set required opps: %d\n", ret);
  987. return ret;
  988. }
  989. }
  990. opp_table->enabled = true;
  991. dev_pm_opp_put(old_opp);
  992. /* Make sure current_opp doesn't get freed */
  993. dev_pm_opp_get(opp);
  994. opp_table->current_opp = opp;
  995. return ret;
  996. }
  997. /**
  998. * dev_pm_opp_set_rate() - Configure new OPP based on frequency
  999. * @dev: device for which we do this operation
  1000. * @target_freq: frequency to achieve
  1001. *
  1002. * This configures the power-supplies to the levels specified by the OPP
  1003. * corresponding to the target_freq, and programs the clock to a value <=
  1004. * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
  1005. * provided by the opp, should have already rounded to the target OPP's
  1006. * frequency.
  1007. */
  1008. int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
  1009. {
  1010. struct opp_table *opp_table;
  1011. unsigned long freq = 0, temp_freq;
  1012. struct dev_pm_opp *opp = NULL;
  1013. bool forced = false;
  1014. int ret;
  1015. opp_table = _find_opp_table(dev);
  1016. if (IS_ERR(opp_table)) {
  1017. dev_err(dev, "%s: device's opp table doesn't exist\n", __func__);
  1018. return PTR_ERR(opp_table);
  1019. }
  1020. if (target_freq) {
  1021. /*
  1022. * For IO devices which require an OPP on some platforms/SoCs
  1023. * while just needing to scale the clock on some others
  1024. * we look for empty OPP tables with just a clock handle and
  1025. * scale only the clk. This makes dev_pm_opp_set_rate()
  1026. * equivalent to a clk_set_rate()
  1027. */
  1028. if (!_get_opp_count(opp_table)) {
  1029. ret = opp_table->config_clks(dev, opp_table, NULL,
  1030. &target_freq, false);
  1031. goto put_opp_table;
  1032. }
  1033. freq = clk_round_rate(opp_table->clk, target_freq);
  1034. if ((long)freq <= 0)
  1035. freq = target_freq;
  1036. /*
  1037. * The clock driver may support finer resolution of the
  1038. * frequencies than the OPP table, don't update the frequency we
  1039. * pass to clk_set_rate() here.
  1040. */
  1041. temp_freq = freq;
  1042. opp = _find_freq_ceil(opp_table, &temp_freq);
  1043. if (IS_ERR(opp)) {
  1044. ret = PTR_ERR(opp);
  1045. dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
  1046. __func__, freq, ret);
  1047. goto put_opp_table;
  1048. }
  1049. /*
  1050. * An OPP entry specifies the highest frequency at which other
  1051. * properties of the OPP entry apply. Even if the new OPP is
  1052. * same as the old one, we may still reach here for a different
  1053. * value of the frequency. In such a case, do not abort but
  1054. * configure the hardware to the desired frequency forcefully.
  1055. */
  1056. forced = opp_table->rate_clk_single != target_freq;
  1057. }
  1058. ret = _set_opp(dev, opp_table, opp, &target_freq, forced);
  1059. if (target_freq)
  1060. dev_pm_opp_put(opp);
  1061. put_opp_table:
  1062. dev_pm_opp_put_opp_table(opp_table);
  1063. return ret;
  1064. }
  1065. EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
  1066. /**
  1067. * dev_pm_opp_set_opp() - Configure device for OPP
  1068. * @dev: device for which we do this operation
  1069. * @opp: OPP to set to
  1070. *
  1071. * This configures the device based on the properties of the OPP passed to this
  1072. * routine.
  1073. *
  1074. * Return: 0 on success, a negative error number otherwise.
  1075. */
  1076. int dev_pm_opp_set_opp(struct device *dev, struct dev_pm_opp *opp)
  1077. {
  1078. struct opp_table *opp_table;
  1079. int ret;
  1080. opp_table = _find_opp_table(dev);
  1081. if (IS_ERR(opp_table)) {
  1082. dev_err(dev, "%s: device opp doesn't exist\n", __func__);
  1083. return PTR_ERR(opp_table);
  1084. }
  1085. ret = _set_opp(dev, opp_table, opp, NULL, false);
  1086. dev_pm_opp_put_opp_table(opp_table);
  1087. return ret;
  1088. }
  1089. EXPORT_SYMBOL_GPL(dev_pm_opp_set_opp);
  1090. /* OPP-dev Helpers */
  1091. static void _remove_opp_dev(struct opp_device *opp_dev,
  1092. struct opp_table *opp_table)
  1093. {
  1094. opp_debug_unregister(opp_dev, opp_table);
  1095. list_del(&opp_dev->node);
  1096. kfree(opp_dev);
  1097. }
  1098. struct opp_device *_add_opp_dev(const struct device *dev,
  1099. struct opp_table *opp_table)
  1100. {
  1101. struct opp_device *opp_dev;
  1102. opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
  1103. if (!opp_dev)
  1104. return NULL;
  1105. /* Initialize opp-dev */
  1106. opp_dev->dev = dev;
  1107. mutex_lock(&opp_table->lock);
  1108. list_add(&opp_dev->node, &opp_table->dev_list);
  1109. mutex_unlock(&opp_table->lock);
  1110. /* Create debugfs entries for the opp_table */
  1111. opp_debug_register(opp_dev, opp_table);
  1112. return opp_dev;
  1113. }
  1114. static struct opp_table *_allocate_opp_table(struct device *dev, int index)
  1115. {
  1116. struct opp_table *opp_table;
  1117. struct opp_device *opp_dev;
  1118. int ret;
  1119. /*
  1120. * Allocate a new OPP table. In the infrequent case where a new
  1121. * device is needed to be added, we pay this penalty.
  1122. */
  1123. opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
  1124. if (!opp_table)
  1125. return ERR_PTR(-ENOMEM);
  1126. mutex_init(&opp_table->lock);
  1127. mutex_init(&opp_table->genpd_virt_dev_lock);
  1128. INIT_LIST_HEAD(&opp_table->dev_list);
  1129. INIT_LIST_HEAD(&opp_table->lazy);
  1130. opp_table->clk = ERR_PTR(-ENODEV);
  1131. /* Mark regulator count uninitialized */
  1132. opp_table->regulator_count = -1;
  1133. opp_dev = _add_opp_dev(dev, opp_table);
  1134. if (!opp_dev) {
  1135. ret = -ENOMEM;
  1136. goto err;
  1137. }
  1138. _of_init_opp_table(opp_table, dev, index);
  1139. /* Find interconnect path(s) for the device */
  1140. ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
  1141. if (ret) {
  1142. if (ret == -EPROBE_DEFER)
  1143. goto remove_opp_dev;
  1144. dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
  1145. __func__, ret);
  1146. }
  1147. BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
  1148. INIT_LIST_HEAD(&opp_table->opp_list);
  1149. kref_init(&opp_table->kref);
  1150. return opp_table;
  1151. remove_opp_dev:
  1152. _of_clear_opp_table(opp_table);
  1153. _remove_opp_dev(opp_dev, opp_table);
  1154. mutex_destroy(&opp_table->genpd_virt_dev_lock);
  1155. mutex_destroy(&opp_table->lock);
  1156. err:
  1157. kfree(opp_table);
  1158. return ERR_PTR(ret);
  1159. }
  1160. void _get_opp_table_kref(struct opp_table *opp_table)
  1161. {
  1162. kref_get(&opp_table->kref);
  1163. }
  1164. static struct opp_table *_update_opp_table_clk(struct device *dev,
  1165. struct opp_table *opp_table,
  1166. bool getclk)
  1167. {
  1168. int ret;
  1169. /*
  1170. * Return early if we don't need to get clk or we have already done it
  1171. * earlier.
  1172. */
  1173. if (!getclk || IS_ERR(opp_table) || !IS_ERR(opp_table->clk) ||
  1174. opp_table->clks)
  1175. return opp_table;
  1176. /* Find clk for the device */
  1177. opp_table->clk = clk_get(dev, NULL);
  1178. ret = PTR_ERR_OR_ZERO(opp_table->clk);
  1179. if (!ret) {
  1180. opp_table->config_clks = _opp_config_clk_single;
  1181. opp_table->clk_count = 1;
  1182. return opp_table;
  1183. }
  1184. if (ret == -ENOENT) {
  1185. /*
  1186. * There are few platforms which don't want the OPP core to
  1187. * manage device's clock settings. In such cases neither the
  1188. * platform provides the clks explicitly to us, nor the DT
  1189. * contains a valid clk entry. The OPP nodes in DT may still
  1190. * contain "opp-hz" property though, which we need to parse and
  1191. * allow the platform to find an OPP based on freq later on.
  1192. *
  1193. * This is a simple solution to take care of such corner cases,
  1194. * i.e. make the clk_count 1, which lets us allocate space for
  1195. * frequency in opp->rates and also parse the entries in DT.
  1196. */
  1197. opp_table->clk_count = 1;
  1198. dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
  1199. return opp_table;
  1200. }
  1201. dev_pm_opp_put_opp_table(opp_table);
  1202. dev_err_probe(dev, ret, "Couldn't find clock\n");
  1203. return ERR_PTR(ret);
  1204. }
  1205. /*
  1206. * We need to make sure that the OPP table for a device doesn't get added twice,
  1207. * if this routine gets called in parallel with the same device pointer.
  1208. *
  1209. * The simplest way to enforce that is to perform everything (find existing
  1210. * table and if not found, create a new one) under the opp_table_lock, so only
  1211. * one creator gets access to the same. But that expands the critical section
  1212. * under the lock and may end up causing circular dependencies with frameworks
  1213. * like debugfs, interconnect or clock framework as they may be direct or
  1214. * indirect users of OPP core.
  1215. *
  1216. * And for that reason we have to go for a bit tricky implementation here, which
  1217. * uses the opp_tables_busy flag to indicate if another creator is in the middle
  1218. * of adding an OPP table and others should wait for it to finish.
  1219. */
  1220. struct opp_table *_add_opp_table_indexed(struct device *dev, int index,
  1221. bool getclk)
  1222. {
  1223. struct opp_table *opp_table;
  1224. again:
  1225. mutex_lock(&opp_table_lock);
  1226. opp_table = _find_opp_table_unlocked(dev);
  1227. if (!IS_ERR(opp_table))
  1228. goto unlock;
  1229. /*
  1230. * The opp_tables list or an OPP table's dev_list is getting updated by
  1231. * another user, wait for it to finish.
  1232. */
  1233. if (unlikely(opp_tables_busy)) {
  1234. mutex_unlock(&opp_table_lock);
  1235. cpu_relax();
  1236. goto again;
  1237. }
  1238. opp_tables_busy = true;
  1239. opp_table = _managed_opp(dev, index);
  1240. /* Drop the lock to reduce the size of critical section */
  1241. mutex_unlock(&opp_table_lock);
  1242. if (opp_table) {
  1243. if (!_add_opp_dev(dev, opp_table)) {
  1244. dev_pm_opp_put_opp_table(opp_table);
  1245. opp_table = ERR_PTR(-ENOMEM);
  1246. }
  1247. mutex_lock(&opp_table_lock);
  1248. } else {
  1249. opp_table = _allocate_opp_table(dev, index);
  1250. mutex_lock(&opp_table_lock);
  1251. if (!IS_ERR(opp_table))
  1252. list_add(&opp_table->node, &opp_tables);
  1253. }
  1254. opp_tables_busy = false;
  1255. unlock:
  1256. mutex_unlock(&opp_table_lock);
  1257. return _update_opp_table_clk(dev, opp_table, getclk);
  1258. }
  1259. static struct opp_table *_add_opp_table(struct device *dev, bool getclk)
  1260. {
  1261. return _add_opp_table_indexed(dev, 0, getclk);
  1262. }
  1263. struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
  1264. {
  1265. return _find_opp_table(dev);
  1266. }
  1267. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
  1268. static void _opp_table_kref_release(struct kref *kref)
  1269. {
  1270. struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
  1271. struct opp_device *opp_dev, *temp;
  1272. int i;
  1273. /* Drop the lock as soon as we can */
  1274. list_del(&opp_table->node);
  1275. mutex_unlock(&opp_table_lock);
  1276. if (opp_table->current_opp)
  1277. dev_pm_opp_put(opp_table->current_opp);
  1278. _of_clear_opp_table(opp_table);
  1279. /* Release automatically acquired single clk */
  1280. if (!IS_ERR(opp_table->clk))
  1281. clk_put(opp_table->clk);
  1282. if (opp_table->paths) {
  1283. for (i = 0; i < opp_table->path_count; i++)
  1284. icc_put(opp_table->paths[i]);
  1285. kfree(opp_table->paths);
  1286. }
  1287. WARN_ON(!list_empty(&opp_table->opp_list));
  1288. list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
  1289. /*
  1290. * The OPP table is getting removed, drop the performance state
  1291. * constraints.
  1292. */
  1293. if (opp_table->genpd_performance_state)
  1294. dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
  1295. _remove_opp_dev(opp_dev, opp_table);
  1296. }
  1297. mutex_destroy(&opp_table->genpd_virt_dev_lock);
  1298. mutex_destroy(&opp_table->lock);
  1299. kfree(opp_table);
  1300. }
  1301. void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
  1302. {
  1303. kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
  1304. &opp_table_lock);
  1305. }
  1306. EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
  1307. void _opp_free(struct dev_pm_opp *opp)
  1308. {
  1309. kfree(opp);
  1310. }
  1311. static void _opp_kref_release(struct kref *kref)
  1312. {
  1313. struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
  1314. struct opp_table *opp_table = opp->opp_table;
  1315. list_del(&opp->node);
  1316. mutex_unlock(&opp_table->lock);
  1317. /*
  1318. * Notify the changes in the availability of the operable
  1319. * frequency/voltage list.
  1320. */
  1321. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
  1322. _of_clear_opp(opp_table, opp);
  1323. opp_debug_remove_one(opp);
  1324. kfree(opp);
  1325. }
  1326. void dev_pm_opp_get(struct dev_pm_opp *opp)
  1327. {
  1328. kref_get(&opp->kref);
  1329. }
  1330. void dev_pm_opp_put(struct dev_pm_opp *opp)
  1331. {
  1332. kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
  1333. }
  1334. EXPORT_SYMBOL_GPL(dev_pm_opp_put);
  1335. /**
  1336. * dev_pm_opp_remove() - Remove an OPP from OPP table
  1337. * @dev: device for which we do this operation
  1338. * @freq: OPP to remove with matching 'freq'
  1339. *
  1340. * This function removes an opp from the opp table.
  1341. */
  1342. void dev_pm_opp_remove(struct device *dev, unsigned long freq)
  1343. {
  1344. struct dev_pm_opp *opp = NULL, *iter;
  1345. struct opp_table *opp_table;
  1346. opp_table = _find_opp_table(dev);
  1347. if (IS_ERR(opp_table))
  1348. return;
  1349. if (!assert_single_clk(opp_table))
  1350. goto put_table;
  1351. mutex_lock(&opp_table->lock);
  1352. list_for_each_entry(iter, &opp_table->opp_list, node) {
  1353. if (iter->rates[0] == freq) {
  1354. opp = iter;
  1355. break;
  1356. }
  1357. }
  1358. mutex_unlock(&opp_table->lock);
  1359. if (opp) {
  1360. dev_pm_opp_put(opp);
  1361. /* Drop the reference taken by dev_pm_opp_add() */
  1362. dev_pm_opp_put_opp_table(opp_table);
  1363. } else {
  1364. dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
  1365. __func__, freq);
  1366. }
  1367. put_table:
  1368. /* Drop the reference taken by _find_opp_table() */
  1369. dev_pm_opp_put_opp_table(opp_table);
  1370. }
  1371. EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
  1372. static struct dev_pm_opp *_opp_get_next(struct opp_table *opp_table,
  1373. bool dynamic)
  1374. {
  1375. struct dev_pm_opp *opp = NULL, *temp;
  1376. mutex_lock(&opp_table->lock);
  1377. list_for_each_entry(temp, &opp_table->opp_list, node) {
  1378. /*
  1379. * Refcount must be dropped only once for each OPP by OPP core,
  1380. * do that with help of "removed" flag.
  1381. */
  1382. if (!temp->removed && dynamic == temp->dynamic) {
  1383. opp = temp;
  1384. break;
  1385. }
  1386. }
  1387. mutex_unlock(&opp_table->lock);
  1388. return opp;
  1389. }
  1390. /*
  1391. * Can't call dev_pm_opp_put() from under the lock as debugfs removal needs to
  1392. * happen lock less to avoid circular dependency issues. This routine must be
  1393. * called without the opp_table->lock held.
  1394. */
  1395. static void _opp_remove_all(struct opp_table *opp_table, bool dynamic)
  1396. {
  1397. struct dev_pm_opp *opp;
  1398. while ((opp = _opp_get_next(opp_table, dynamic))) {
  1399. opp->removed = true;
  1400. dev_pm_opp_put(opp);
  1401. /* Drop the references taken by dev_pm_opp_add() */
  1402. if (dynamic)
  1403. dev_pm_opp_put_opp_table(opp_table);
  1404. }
  1405. }
  1406. bool _opp_remove_all_static(struct opp_table *opp_table)
  1407. {
  1408. mutex_lock(&opp_table->lock);
  1409. if (!opp_table->parsed_static_opps) {
  1410. mutex_unlock(&opp_table->lock);
  1411. return false;
  1412. }
  1413. if (--opp_table->parsed_static_opps) {
  1414. mutex_unlock(&opp_table->lock);
  1415. return true;
  1416. }
  1417. mutex_unlock(&opp_table->lock);
  1418. _opp_remove_all(opp_table, false);
  1419. return true;
  1420. }
  1421. /**
  1422. * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
  1423. * @dev: device for which we do this operation
  1424. *
  1425. * This function removes all dynamically created OPPs from the opp table.
  1426. */
  1427. void dev_pm_opp_remove_all_dynamic(struct device *dev)
  1428. {
  1429. struct opp_table *opp_table;
  1430. opp_table = _find_opp_table(dev);
  1431. if (IS_ERR(opp_table))
  1432. return;
  1433. _opp_remove_all(opp_table, true);
  1434. /* Drop the reference taken by _find_opp_table() */
  1435. dev_pm_opp_put_opp_table(opp_table);
  1436. }
  1437. EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
  1438. struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table)
  1439. {
  1440. struct dev_pm_opp *opp;
  1441. int supply_count, supply_size, icc_size, clk_size;
  1442. /* Allocate space for at least one supply */
  1443. supply_count = opp_table->regulator_count > 0 ?
  1444. opp_table->regulator_count : 1;
  1445. supply_size = sizeof(*opp->supplies) * supply_count;
  1446. clk_size = sizeof(*opp->rates) * opp_table->clk_count;
  1447. icc_size = sizeof(*opp->bandwidth) * opp_table->path_count;
  1448. /* allocate new OPP node and supplies structures */
  1449. opp = kzalloc(sizeof(*opp) + supply_size + clk_size + icc_size, GFP_KERNEL);
  1450. if (!opp)
  1451. return NULL;
  1452. /* Put the supplies, bw and clock at the end of the OPP structure */
  1453. opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
  1454. opp->rates = (unsigned long *)(opp->supplies + supply_count);
  1455. if (icc_size)
  1456. opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->rates + opp_table->clk_count);
  1457. INIT_LIST_HEAD(&opp->node);
  1458. return opp;
  1459. }
  1460. static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
  1461. struct opp_table *opp_table)
  1462. {
  1463. struct regulator *reg;
  1464. int i;
  1465. if (!opp_table->regulators)
  1466. return true;
  1467. for (i = 0; i < opp_table->regulator_count; i++) {
  1468. reg = opp_table->regulators[i];
  1469. if (!regulator_is_supported_voltage(reg,
  1470. opp->supplies[i].u_volt_min,
  1471. opp->supplies[i].u_volt_max)) {
  1472. pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
  1473. __func__, opp->supplies[i].u_volt_min,
  1474. opp->supplies[i].u_volt_max);
  1475. return false;
  1476. }
  1477. }
  1478. return true;
  1479. }
  1480. static int _opp_compare_rate(struct opp_table *opp_table,
  1481. struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
  1482. {
  1483. int i;
  1484. for (i = 0; i < opp_table->clk_count; i++) {
  1485. if (opp1->rates[i] != opp2->rates[i])
  1486. return opp1->rates[i] < opp2->rates[i] ? -1 : 1;
  1487. }
  1488. /* Same rates for both OPPs */
  1489. return 0;
  1490. }
  1491. static int _opp_compare_bw(struct opp_table *opp_table, struct dev_pm_opp *opp1,
  1492. struct dev_pm_opp *opp2)
  1493. {
  1494. int i;
  1495. for (i = 0; i < opp_table->path_count; i++) {
  1496. if (opp1->bandwidth[i].peak != opp2->bandwidth[i].peak)
  1497. return opp1->bandwidth[i].peak < opp2->bandwidth[i].peak ? -1 : 1;
  1498. }
  1499. /* Same bw for both OPPs */
  1500. return 0;
  1501. }
  1502. /*
  1503. * Returns
  1504. * 0: opp1 == opp2
  1505. * 1: opp1 > opp2
  1506. * -1: opp1 < opp2
  1507. */
  1508. int _opp_compare_key(struct opp_table *opp_table, struct dev_pm_opp *opp1,
  1509. struct dev_pm_opp *opp2)
  1510. {
  1511. int ret;
  1512. ret = _opp_compare_rate(opp_table, opp1, opp2);
  1513. if (ret)
  1514. return ret;
  1515. ret = _opp_compare_bw(opp_table, opp1, opp2);
  1516. if (ret)
  1517. return ret;
  1518. if (opp1->level != opp2->level)
  1519. return opp1->level < opp2->level ? -1 : 1;
  1520. /* Duplicate OPPs */
  1521. return 0;
  1522. }
  1523. static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
  1524. struct opp_table *opp_table,
  1525. struct list_head **head)
  1526. {
  1527. struct dev_pm_opp *opp;
  1528. int opp_cmp;
  1529. /*
  1530. * Insert new OPP in order of increasing frequency and discard if
  1531. * already present.
  1532. *
  1533. * Need to use &opp_table->opp_list in the condition part of the 'for'
  1534. * loop, don't replace it with head otherwise it will become an infinite
  1535. * loop.
  1536. */
  1537. list_for_each_entry(opp, &opp_table->opp_list, node) {
  1538. opp_cmp = _opp_compare_key(opp_table, new_opp, opp);
  1539. if (opp_cmp > 0) {
  1540. *head = &opp->node;
  1541. continue;
  1542. }
  1543. if (opp_cmp < 0)
  1544. return 0;
  1545. /* Duplicate OPPs */
  1546. dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
  1547. __func__, opp->rates[0], opp->supplies[0].u_volt,
  1548. opp->available, new_opp->rates[0],
  1549. new_opp->supplies[0].u_volt, new_opp->available);
  1550. /* Should we compare voltages for all regulators here ? */
  1551. return opp->available &&
  1552. new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
  1553. }
  1554. return 0;
  1555. }
  1556. void _required_opps_available(struct dev_pm_opp *opp, int count)
  1557. {
  1558. int i;
  1559. for (i = 0; i < count; i++) {
  1560. if (opp->required_opps[i]->available)
  1561. continue;
  1562. opp->available = false;
  1563. pr_warn("%s: OPP not supported by required OPP %pOF (%lu)\n",
  1564. __func__, opp->required_opps[i]->np, opp->rates[0]);
  1565. return;
  1566. }
  1567. }
  1568. /*
  1569. * Returns:
  1570. * 0: On success. And appropriate error message for duplicate OPPs.
  1571. * -EBUSY: For OPP with same freq/volt and is available. The callers of
  1572. * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
  1573. * sure we don't print error messages unnecessarily if different parts of
  1574. * kernel try to initialize the OPP table.
  1575. * -EEXIST: For OPP with same freq but different volt or is unavailable. This
  1576. * should be considered an error by the callers of _opp_add().
  1577. */
  1578. int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
  1579. struct opp_table *opp_table)
  1580. {
  1581. struct list_head *head;
  1582. int ret;
  1583. mutex_lock(&opp_table->lock);
  1584. head = &opp_table->opp_list;
  1585. ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
  1586. if (ret) {
  1587. mutex_unlock(&opp_table->lock);
  1588. return ret;
  1589. }
  1590. list_add(&new_opp->node, head);
  1591. mutex_unlock(&opp_table->lock);
  1592. new_opp->opp_table = opp_table;
  1593. kref_init(&new_opp->kref);
  1594. opp_debug_create_one(new_opp, opp_table);
  1595. if (!_opp_supported_by_regulators(new_opp, opp_table)) {
  1596. new_opp->available = false;
  1597. dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
  1598. __func__, new_opp->rates[0]);
  1599. }
  1600. /* required-opps not fully initialized yet */
  1601. if (lazy_linking_pending(opp_table))
  1602. return 0;
  1603. _required_opps_available(new_opp, opp_table->required_opp_count);
  1604. return 0;
  1605. }
  1606. /**
  1607. * _opp_add_v1() - Allocate a OPP based on v1 bindings.
  1608. * @opp_table: OPP table
  1609. * @dev: device for which we do this operation
  1610. * @freq: Frequency in Hz for this OPP
  1611. * @u_volt: Voltage in uVolts for this OPP
  1612. * @dynamic: Dynamically added OPPs.
  1613. *
  1614. * This function adds an opp definition to the opp table and returns status.
  1615. * The opp is made available by default and it can be controlled using
  1616. * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
  1617. *
  1618. * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
  1619. * and freed by dev_pm_opp_of_remove_table.
  1620. *
  1621. * Return:
  1622. * 0 On success OR
  1623. * Duplicate OPPs (both freq and volt are same) and opp->available
  1624. * -EEXIST Freq are same and volt are different OR
  1625. * Duplicate OPPs (both freq and volt are same) and !opp->available
  1626. * -ENOMEM Memory allocation failure
  1627. */
  1628. int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
  1629. unsigned long freq, long u_volt, bool dynamic)
  1630. {
  1631. struct dev_pm_opp *new_opp;
  1632. unsigned long tol;
  1633. int ret;
  1634. if (!assert_single_clk(opp_table))
  1635. return -EINVAL;
  1636. new_opp = _opp_allocate(opp_table);
  1637. if (!new_opp)
  1638. return -ENOMEM;
  1639. /* populate the opp table */
  1640. new_opp->rates[0] = freq;
  1641. tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
  1642. new_opp->supplies[0].u_volt = u_volt;
  1643. new_opp->supplies[0].u_volt_min = u_volt - tol;
  1644. new_opp->supplies[0].u_volt_max = u_volt + tol;
  1645. new_opp->available = true;
  1646. new_opp->dynamic = dynamic;
  1647. ret = _opp_add(dev, new_opp, opp_table);
  1648. if (ret) {
  1649. /* Don't return error for duplicate OPPs */
  1650. if (ret == -EBUSY)
  1651. ret = 0;
  1652. goto free_opp;
  1653. }
  1654. /*
  1655. * Notify the changes in the availability of the operable
  1656. * frequency/voltage list.
  1657. */
  1658. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
  1659. return 0;
  1660. free_opp:
  1661. _opp_free(new_opp);
  1662. return ret;
  1663. }
  1664. /**
  1665. * _opp_set_supported_hw() - Set supported platforms
  1666. * @dev: Device for which supported-hw has to be set.
  1667. * @versions: Array of hierarchy of versions to match.
  1668. * @count: Number of elements in the array.
  1669. *
  1670. * This is required only for the V2 bindings, and it enables a platform to
  1671. * specify the hierarchy of versions it supports. OPP layer will then enable
  1672. * OPPs, which are available for those versions, based on its 'opp-supported-hw'
  1673. * property.
  1674. */
  1675. static int _opp_set_supported_hw(struct opp_table *opp_table,
  1676. const u32 *versions, unsigned int count)
  1677. {
  1678. /* Another CPU that shares the OPP table has set the property ? */
  1679. if (opp_table->supported_hw)
  1680. return 0;
  1681. opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
  1682. GFP_KERNEL);
  1683. if (!opp_table->supported_hw)
  1684. return -ENOMEM;
  1685. opp_table->supported_hw_count = count;
  1686. return 0;
  1687. }
  1688. /**
  1689. * _opp_put_supported_hw() - Releases resources blocked for supported hw
  1690. * @opp_table: OPP table returned by _opp_set_supported_hw().
  1691. *
  1692. * This is required only for the V2 bindings, and is called for a matching
  1693. * _opp_set_supported_hw(). Until this is called, the opp_table structure
  1694. * will not be freed.
  1695. */
  1696. static void _opp_put_supported_hw(struct opp_table *opp_table)
  1697. {
  1698. if (opp_table->supported_hw) {
  1699. kfree(opp_table->supported_hw);
  1700. opp_table->supported_hw = NULL;
  1701. opp_table->supported_hw_count = 0;
  1702. }
  1703. }
  1704. /**
  1705. * _opp_set_prop_name() - Set prop-extn name
  1706. * @dev: Device for which the prop-name has to be set.
  1707. * @name: name to postfix to properties.
  1708. *
  1709. * This is required only for the V2 bindings, and it enables a platform to
  1710. * specify the extn to be used for certain property names. The properties to
  1711. * which the extension will apply are opp-microvolt and opp-microamp. OPP core
  1712. * should postfix the property name with -<name> while looking for them.
  1713. */
  1714. static int _opp_set_prop_name(struct opp_table *opp_table, const char *name)
  1715. {
  1716. /* Another CPU that shares the OPP table has set the property ? */
  1717. if (!opp_table->prop_name) {
  1718. opp_table->prop_name = kstrdup(name, GFP_KERNEL);
  1719. if (!opp_table->prop_name)
  1720. return -ENOMEM;
  1721. }
  1722. return 0;
  1723. }
  1724. /**
  1725. * _opp_put_prop_name() - Releases resources blocked for prop-name
  1726. * @opp_table: OPP table returned by _opp_set_prop_name().
  1727. *
  1728. * This is required only for the V2 bindings, and is called for a matching
  1729. * _opp_set_prop_name(). Until this is called, the opp_table structure
  1730. * will not be freed.
  1731. */
  1732. static void _opp_put_prop_name(struct opp_table *opp_table)
  1733. {
  1734. if (opp_table->prop_name) {
  1735. kfree(opp_table->prop_name);
  1736. opp_table->prop_name = NULL;
  1737. }
  1738. }
  1739. /**
  1740. * _opp_set_regulators() - Set regulator names for the device
  1741. * @dev: Device for which regulator name is being set.
  1742. * @names: Array of pointers to the names of the regulator.
  1743. * @count: Number of regulators.
  1744. *
  1745. * In order to support OPP switching, OPP layer needs to know the name of the
  1746. * device's regulators, as the core would be required to switch voltages as
  1747. * well.
  1748. *
  1749. * This must be called before any OPPs are initialized for the device.
  1750. */
  1751. static int _opp_set_regulators(struct opp_table *opp_table, struct device *dev,
  1752. const char * const names[])
  1753. {
  1754. const char * const *temp = names;
  1755. struct regulator *reg;
  1756. int count = 0, ret, i;
  1757. /* Count number of regulators */
  1758. while (*temp++)
  1759. count++;
  1760. if (!count)
  1761. return -EINVAL;
  1762. /* Another CPU that shares the OPP table has set the regulators ? */
  1763. if (opp_table->regulators)
  1764. return 0;
  1765. opp_table->regulators = kmalloc_array(count,
  1766. sizeof(*opp_table->regulators),
  1767. GFP_KERNEL);
  1768. if (!opp_table->regulators)
  1769. return -ENOMEM;
  1770. for (i = 0; i < count; i++) {
  1771. reg = regulator_get_optional(dev, names[i]);
  1772. if (IS_ERR(reg)) {
  1773. ret = dev_err_probe(dev, PTR_ERR(reg),
  1774. "%s: no regulator (%s) found\n",
  1775. __func__, names[i]);
  1776. goto free_regulators;
  1777. }
  1778. opp_table->regulators[i] = reg;
  1779. }
  1780. opp_table->regulator_count = count;
  1781. /* Set generic config_regulators() for single regulators here */
  1782. if (count == 1)
  1783. opp_table->config_regulators = _opp_config_regulator_single;
  1784. return 0;
  1785. free_regulators:
  1786. while (i != 0)
  1787. regulator_put(opp_table->regulators[--i]);
  1788. kfree(opp_table->regulators);
  1789. opp_table->regulators = NULL;
  1790. opp_table->regulator_count = -1;
  1791. return ret;
  1792. }
  1793. /**
  1794. * _opp_put_regulators() - Releases resources blocked for regulator
  1795. * @opp_table: OPP table returned from _opp_set_regulators().
  1796. */
  1797. static void _opp_put_regulators(struct opp_table *opp_table)
  1798. {
  1799. int i;
  1800. if (!opp_table->regulators)
  1801. return;
  1802. if (opp_table->enabled) {
  1803. for (i = opp_table->regulator_count - 1; i >= 0; i--)
  1804. regulator_disable(opp_table->regulators[i]);
  1805. }
  1806. for (i = opp_table->regulator_count - 1; i >= 0; i--)
  1807. regulator_put(opp_table->regulators[i]);
  1808. kfree(opp_table->regulators);
  1809. opp_table->regulators = NULL;
  1810. opp_table->regulator_count = -1;
  1811. }
  1812. static void _put_clks(struct opp_table *opp_table, int count)
  1813. {
  1814. int i;
  1815. for (i = count - 1; i >= 0; i--)
  1816. clk_put(opp_table->clks[i]);
  1817. kfree(opp_table->clks);
  1818. opp_table->clks = NULL;
  1819. }
  1820. /**
  1821. * _opp_set_clknames() - Set clk names for the device
  1822. * @dev: Device for which clk names is being set.
  1823. * @names: Clk names.
  1824. *
  1825. * In order to support OPP switching, OPP layer needs to get pointers to the
  1826. * clocks for the device. Simple cases work fine without using this routine
  1827. * (i.e. by passing connection-id as NULL), but for a device with multiple
  1828. * clocks available, the OPP core needs to know the exact names of the clks to
  1829. * use.
  1830. *
  1831. * This must be called before any OPPs are initialized for the device.
  1832. */
  1833. static int _opp_set_clknames(struct opp_table *opp_table, struct device *dev,
  1834. const char * const names[],
  1835. config_clks_t config_clks)
  1836. {
  1837. const char * const *temp = names;
  1838. int count = 0, ret, i;
  1839. struct clk *clk;
  1840. /* Count number of clks */
  1841. while (*temp++)
  1842. count++;
  1843. /*
  1844. * This is a special case where we have a single clock, whose connection
  1845. * id name is NULL, i.e. first two entries are NULL in the array.
  1846. */
  1847. if (!count && !names[1])
  1848. count = 1;
  1849. /* Fail early for invalid configurations */
  1850. if (!count || (!config_clks && count > 1))
  1851. return -EINVAL;
  1852. /* Another CPU that shares the OPP table has set the clkname ? */
  1853. if (opp_table->clks)
  1854. return 0;
  1855. opp_table->clks = kmalloc_array(count, sizeof(*opp_table->clks),
  1856. GFP_KERNEL);
  1857. if (!opp_table->clks)
  1858. return -ENOMEM;
  1859. /* Find clks for the device */
  1860. for (i = 0; i < count; i++) {
  1861. clk = clk_get(dev, names[i]);
  1862. if (IS_ERR(clk)) {
  1863. ret = dev_err_probe(dev, PTR_ERR(clk),
  1864. "%s: Couldn't find clock with name: %s\n",
  1865. __func__, names[i]);
  1866. goto free_clks;
  1867. }
  1868. opp_table->clks[i] = clk;
  1869. }
  1870. opp_table->clk_count = count;
  1871. opp_table->config_clks = config_clks;
  1872. /* Set generic single clk set here */
  1873. if (count == 1) {
  1874. if (!opp_table->config_clks)
  1875. opp_table->config_clks = _opp_config_clk_single;
  1876. /*
  1877. * We could have just dropped the "clk" field and used "clks"
  1878. * everywhere. Instead we kept the "clk" field around for
  1879. * following reasons:
  1880. *
  1881. * - avoiding clks[0] everywhere else.
  1882. * - not running single clk helpers for multiple clk usecase by
  1883. * mistake.
  1884. *
  1885. * Since this is single-clk case, just update the clk pointer
  1886. * too.
  1887. */
  1888. opp_table->clk = opp_table->clks[0];
  1889. }
  1890. return 0;
  1891. free_clks:
  1892. _put_clks(opp_table, i);
  1893. return ret;
  1894. }
  1895. /**
  1896. * _opp_put_clknames() - Releases resources blocked for clks.
  1897. * @opp_table: OPP table returned from _opp_set_clknames().
  1898. */
  1899. static void _opp_put_clknames(struct opp_table *opp_table)
  1900. {
  1901. if (!opp_table->clks)
  1902. return;
  1903. opp_table->config_clks = NULL;
  1904. opp_table->clk = ERR_PTR(-ENODEV);
  1905. _put_clks(opp_table, opp_table->clk_count);
  1906. }
  1907. /**
  1908. * _opp_set_config_regulators_helper() - Register custom set regulator helper.
  1909. * @dev: Device for which the helper is getting registered.
  1910. * @config_regulators: Custom set regulator helper.
  1911. *
  1912. * This is useful to support platforms with multiple regulators per device.
  1913. *
  1914. * This must be called before any OPPs are initialized for the device.
  1915. */
  1916. static int _opp_set_config_regulators_helper(struct opp_table *opp_table,
  1917. struct device *dev, config_regulators_t config_regulators)
  1918. {
  1919. /* Another CPU that shares the OPP table has set the helper ? */
  1920. if (!opp_table->config_regulators)
  1921. opp_table->config_regulators = config_regulators;
  1922. return 0;
  1923. }
  1924. /**
  1925. * _opp_put_config_regulators_helper() - Releases resources blocked for
  1926. * config_regulators helper.
  1927. * @opp_table: OPP table returned from _opp_set_config_regulators_helper().
  1928. *
  1929. * Release resources blocked for platform specific config_regulators helper.
  1930. */
  1931. static void _opp_put_config_regulators_helper(struct opp_table *opp_table)
  1932. {
  1933. if (opp_table->config_regulators)
  1934. opp_table->config_regulators = NULL;
  1935. }
  1936. static void _detach_genpd(struct opp_table *opp_table)
  1937. {
  1938. int index;
  1939. if (!opp_table->genpd_virt_devs)
  1940. return;
  1941. for (index = 0; index < opp_table->required_opp_count; index++) {
  1942. if (!opp_table->genpd_virt_devs[index])
  1943. continue;
  1944. dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
  1945. opp_table->genpd_virt_devs[index] = NULL;
  1946. }
  1947. kfree(opp_table->genpd_virt_devs);
  1948. opp_table->genpd_virt_devs = NULL;
  1949. }
  1950. /**
  1951. * _opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
  1952. * @dev: Consumer device for which the genpd is getting attached.
  1953. * @names: Null terminated array of pointers containing names of genpd to attach.
  1954. * @virt_devs: Pointer to return the array of virtual devices.
  1955. *
  1956. * Multiple generic power domains for a device are supported with the help of
  1957. * virtual genpd devices, which are created for each consumer device - genpd
  1958. * pair. These are the device structures which are attached to the power domain
  1959. * and are required by the OPP core to set the performance state of the genpd.
  1960. * The same API also works for the case where single genpd is available and so
  1961. * we don't need to support that separately.
  1962. *
  1963. * This helper will normally be called by the consumer driver of the device
  1964. * "dev", as only that has details of the genpd names.
  1965. *
  1966. * This helper needs to be called once with a list of all genpd to attach.
  1967. * Otherwise the original device structure will be used instead by the OPP core.
  1968. *
  1969. * The order of entries in the names array must match the order in which
  1970. * "required-opps" are added in DT.
  1971. */
  1972. static int _opp_attach_genpd(struct opp_table *opp_table, struct device *dev,
  1973. const char * const *names, struct device ***virt_devs)
  1974. {
  1975. struct device *virt_dev;
  1976. int index = 0, ret = -EINVAL;
  1977. const char * const *name = names;
  1978. if (opp_table->genpd_virt_devs)
  1979. return 0;
  1980. /*
  1981. * If the genpd's OPP table isn't already initialized, parsing of the
  1982. * required-opps fail for dev. We should retry this after genpd's OPP
  1983. * table is added.
  1984. */
  1985. if (!opp_table->required_opp_count)
  1986. return -EPROBE_DEFER;
  1987. mutex_lock(&opp_table->genpd_virt_dev_lock);
  1988. opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
  1989. sizeof(*opp_table->genpd_virt_devs),
  1990. GFP_KERNEL);
  1991. if (!opp_table->genpd_virt_devs)
  1992. goto unlock;
  1993. while (*name) {
  1994. if (index >= opp_table->required_opp_count) {
  1995. dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
  1996. *name, opp_table->required_opp_count, index);
  1997. goto err;
  1998. }
  1999. virt_dev = dev_pm_domain_attach_by_name(dev, *name);
  2000. if (IS_ERR_OR_NULL(virt_dev)) {
  2001. ret = virt_dev ? PTR_ERR(virt_dev) : -ENODEV;
  2002. dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
  2003. goto err;
  2004. }
  2005. opp_table->genpd_virt_devs[index] = virt_dev;
  2006. index++;
  2007. name++;
  2008. }
  2009. if (virt_devs)
  2010. *virt_devs = opp_table->genpd_virt_devs;
  2011. mutex_unlock(&opp_table->genpd_virt_dev_lock);
  2012. return 0;
  2013. err:
  2014. _detach_genpd(opp_table);
  2015. unlock:
  2016. mutex_unlock(&opp_table->genpd_virt_dev_lock);
  2017. return ret;
  2018. }
  2019. /**
  2020. * _opp_detach_genpd() - Detach genpd(s) from the device.
  2021. * @opp_table: OPP table returned by _opp_attach_genpd().
  2022. *
  2023. * This detaches the genpd(s), resets the virtual device pointers, and puts the
  2024. * OPP table.
  2025. */
  2026. static void _opp_detach_genpd(struct opp_table *opp_table)
  2027. {
  2028. /*
  2029. * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
  2030. * used in parallel.
  2031. */
  2032. mutex_lock(&opp_table->genpd_virt_dev_lock);
  2033. _detach_genpd(opp_table);
  2034. mutex_unlock(&opp_table->genpd_virt_dev_lock);
  2035. }
  2036. static void _opp_clear_config(struct opp_config_data *data)
  2037. {
  2038. if (data->flags & OPP_CONFIG_GENPD)
  2039. _opp_detach_genpd(data->opp_table);
  2040. if (data->flags & OPP_CONFIG_REGULATOR)
  2041. _opp_put_regulators(data->opp_table);
  2042. if (data->flags & OPP_CONFIG_SUPPORTED_HW)
  2043. _opp_put_supported_hw(data->opp_table);
  2044. if (data->flags & OPP_CONFIG_REGULATOR_HELPER)
  2045. _opp_put_config_regulators_helper(data->opp_table);
  2046. if (data->flags & OPP_CONFIG_PROP_NAME)
  2047. _opp_put_prop_name(data->opp_table);
  2048. if (data->flags & OPP_CONFIG_CLK)
  2049. _opp_put_clknames(data->opp_table);
  2050. dev_pm_opp_put_opp_table(data->opp_table);
  2051. kfree(data);
  2052. }
  2053. /**
  2054. * dev_pm_opp_set_config() - Set OPP configuration for the device.
  2055. * @dev: Device for which configuration is being set.
  2056. * @config: OPP configuration.
  2057. *
  2058. * This allows all device OPP configurations to be performed at once.
  2059. *
  2060. * This must be called before any OPPs are initialized for the device. This may
  2061. * be called multiple times for the same OPP table, for example once for each
  2062. * CPU that share the same table. This must be balanced by the same number of
  2063. * calls to dev_pm_opp_clear_config() in order to free the OPP table properly.
  2064. *
  2065. * This returns a token to the caller, which must be passed to
  2066. * dev_pm_opp_clear_config() to free the resources later. The value of the
  2067. * returned token will be >= 1 for success and negative for errors. The minimum
  2068. * value of 1 is chosen here to make it easy for callers to manage the resource.
  2069. */
  2070. int dev_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
  2071. {
  2072. struct opp_table *opp_table;
  2073. struct opp_config_data *data;
  2074. unsigned int id;
  2075. int ret;
  2076. data = kmalloc(sizeof(*data), GFP_KERNEL);
  2077. if (!data)
  2078. return -ENOMEM;
  2079. opp_table = _add_opp_table(dev, false);
  2080. if (IS_ERR(opp_table)) {
  2081. kfree(data);
  2082. return PTR_ERR(opp_table);
  2083. }
  2084. data->opp_table = opp_table;
  2085. data->flags = 0;
  2086. /* This should be called before OPPs are initialized */
  2087. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  2088. ret = -EBUSY;
  2089. goto err;
  2090. }
  2091. /* Configure clocks */
  2092. if (config->clk_names) {
  2093. ret = _opp_set_clknames(opp_table, dev, config->clk_names,
  2094. config->config_clks);
  2095. if (ret)
  2096. goto err;
  2097. data->flags |= OPP_CONFIG_CLK;
  2098. } else if (config->config_clks) {
  2099. /* Don't allow config callback without clocks */
  2100. ret = -EINVAL;
  2101. goto err;
  2102. }
  2103. /* Configure property names */
  2104. if (config->prop_name) {
  2105. ret = _opp_set_prop_name(opp_table, config->prop_name);
  2106. if (ret)
  2107. goto err;
  2108. data->flags |= OPP_CONFIG_PROP_NAME;
  2109. }
  2110. /* Configure config_regulators helper */
  2111. if (config->config_regulators) {
  2112. ret = _opp_set_config_regulators_helper(opp_table, dev,
  2113. config->config_regulators);
  2114. if (ret)
  2115. goto err;
  2116. data->flags |= OPP_CONFIG_REGULATOR_HELPER;
  2117. }
  2118. /* Configure supported hardware */
  2119. if (config->supported_hw) {
  2120. ret = _opp_set_supported_hw(opp_table, config->supported_hw,
  2121. config->supported_hw_count);
  2122. if (ret)
  2123. goto err;
  2124. data->flags |= OPP_CONFIG_SUPPORTED_HW;
  2125. }
  2126. /* Configure supplies */
  2127. if (config->regulator_names) {
  2128. ret = _opp_set_regulators(opp_table, dev,
  2129. config->regulator_names);
  2130. if (ret)
  2131. goto err;
  2132. data->flags |= OPP_CONFIG_REGULATOR;
  2133. }
  2134. /* Attach genpds */
  2135. if (config->genpd_names) {
  2136. ret = _opp_attach_genpd(opp_table, dev, config->genpd_names,
  2137. config->virt_devs);
  2138. if (ret)
  2139. goto err;
  2140. data->flags |= OPP_CONFIG_GENPD;
  2141. }
  2142. ret = xa_alloc(&opp_configs, &id, data, XA_LIMIT(1, INT_MAX),
  2143. GFP_KERNEL);
  2144. if (ret)
  2145. goto err;
  2146. return id;
  2147. err:
  2148. _opp_clear_config(data);
  2149. return ret;
  2150. }
  2151. EXPORT_SYMBOL_GPL(dev_pm_opp_set_config);
  2152. /**
  2153. * dev_pm_opp_clear_config() - Releases resources blocked for OPP configuration.
  2154. * @opp_table: OPP table returned from dev_pm_opp_set_config().
  2155. *
  2156. * This allows all device OPP configurations to be cleared at once. This must be
  2157. * called once for each call made to dev_pm_opp_set_config(), in order to free
  2158. * the OPPs properly.
  2159. *
  2160. * Currently the first call itself ends up freeing all the OPP configurations,
  2161. * while the later ones only drop the OPP table reference. This works well for
  2162. * now as we would never want to use an half initialized OPP table and want to
  2163. * remove the configurations together.
  2164. */
  2165. void dev_pm_opp_clear_config(int token)
  2166. {
  2167. struct opp_config_data *data;
  2168. /*
  2169. * This lets the callers call this unconditionally and keep their code
  2170. * simple.
  2171. */
  2172. if (unlikely(token <= 0))
  2173. return;
  2174. data = xa_erase(&opp_configs, token);
  2175. if (WARN_ON(!data))
  2176. return;
  2177. _opp_clear_config(data);
  2178. }
  2179. EXPORT_SYMBOL_GPL(dev_pm_opp_clear_config);
  2180. static void devm_pm_opp_config_release(void *token)
  2181. {
  2182. dev_pm_opp_clear_config((unsigned long)token);
  2183. }
  2184. /**
  2185. * devm_pm_opp_set_config() - Set OPP configuration for the device.
  2186. * @dev: Device for which configuration is being set.
  2187. * @config: OPP configuration.
  2188. *
  2189. * This allows all device OPP configurations to be performed at once.
  2190. * This is a resource-managed variant of dev_pm_opp_set_config().
  2191. *
  2192. * Return: 0 on success and errorno otherwise.
  2193. */
  2194. int devm_pm_opp_set_config(struct device *dev, struct dev_pm_opp_config *config)
  2195. {
  2196. int token = dev_pm_opp_set_config(dev, config);
  2197. if (token < 0)
  2198. return token;
  2199. return devm_add_action_or_reset(dev, devm_pm_opp_config_release,
  2200. (void *) ((unsigned long) token));
  2201. }
  2202. EXPORT_SYMBOL_GPL(devm_pm_opp_set_config);
  2203. /**
  2204. * dev_pm_opp_xlate_required_opp() - Find required OPP for @src_table OPP.
  2205. * @src_table: OPP table which has @dst_table as one of its required OPP table.
  2206. * @dst_table: Required OPP table of the @src_table.
  2207. * @src_opp: OPP from the @src_table.
  2208. *
  2209. * This function returns the OPP (present in @dst_table) pointed out by the
  2210. * "required-opps" property of the @src_opp (present in @src_table).
  2211. *
  2212. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  2213. * use.
  2214. *
  2215. * Return: pointer to 'struct dev_pm_opp' on success and errorno otherwise.
  2216. */
  2217. struct dev_pm_opp *dev_pm_opp_xlate_required_opp(struct opp_table *src_table,
  2218. struct opp_table *dst_table,
  2219. struct dev_pm_opp *src_opp)
  2220. {
  2221. struct dev_pm_opp *opp, *dest_opp = ERR_PTR(-ENODEV);
  2222. int i;
  2223. if (!src_table || !dst_table || !src_opp ||
  2224. !src_table->required_opp_tables)
  2225. return ERR_PTR(-EINVAL);
  2226. /* required-opps not fully initialized yet */
  2227. if (lazy_linking_pending(src_table))
  2228. return ERR_PTR(-EBUSY);
  2229. for (i = 0; i < src_table->required_opp_count; i++) {
  2230. if (src_table->required_opp_tables[i] == dst_table) {
  2231. mutex_lock(&src_table->lock);
  2232. list_for_each_entry(opp, &src_table->opp_list, node) {
  2233. if (opp == src_opp) {
  2234. dest_opp = opp->required_opps[i];
  2235. dev_pm_opp_get(dest_opp);
  2236. break;
  2237. }
  2238. }
  2239. mutex_unlock(&src_table->lock);
  2240. break;
  2241. }
  2242. }
  2243. if (IS_ERR(dest_opp)) {
  2244. pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__,
  2245. src_table, dst_table);
  2246. }
  2247. return dest_opp;
  2248. }
  2249. EXPORT_SYMBOL_GPL(dev_pm_opp_xlate_required_opp);
  2250. /**
  2251. * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
  2252. * @src_table: OPP table which has dst_table as one of its required OPP table.
  2253. * @dst_table: Required OPP table of the src_table.
  2254. * @pstate: Current performance state of the src_table.
  2255. *
  2256. * This Returns pstate of the OPP (present in @dst_table) pointed out by the
  2257. * "required-opps" property of the OPP (present in @src_table) which has
  2258. * performance state set to @pstate.
  2259. *
  2260. * Return: Zero or positive performance state on success, otherwise negative
  2261. * value on errors.
  2262. */
  2263. int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
  2264. struct opp_table *dst_table,
  2265. unsigned int pstate)
  2266. {
  2267. struct dev_pm_opp *opp;
  2268. int dest_pstate = -EINVAL;
  2269. int i;
  2270. /*
  2271. * Normally the src_table will have the "required_opps" property set to
  2272. * point to one of the OPPs in the dst_table, but in some cases the
  2273. * genpd and its master have one to one mapping of performance states
  2274. * and so none of them have the "required-opps" property set. Return the
  2275. * pstate of the src_table as it is in such cases.
  2276. */
  2277. if (!src_table || !src_table->required_opp_count)
  2278. return pstate;
  2279. /* required-opps not fully initialized yet */
  2280. if (lazy_linking_pending(src_table))
  2281. return -EBUSY;
  2282. for (i = 0; i < src_table->required_opp_count; i++) {
  2283. if (src_table->required_opp_tables[i]->np == dst_table->np)
  2284. break;
  2285. }
  2286. if (unlikely(i == src_table->required_opp_count)) {
  2287. pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
  2288. __func__, src_table, dst_table);
  2289. return -EINVAL;
  2290. }
  2291. mutex_lock(&src_table->lock);
  2292. list_for_each_entry(opp, &src_table->opp_list, node) {
  2293. if (opp->pstate == pstate) {
  2294. dest_pstate = opp->required_opps[i]->pstate;
  2295. goto unlock;
  2296. }
  2297. }
  2298. pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
  2299. dst_table);
  2300. unlock:
  2301. mutex_unlock(&src_table->lock);
  2302. return dest_pstate;
  2303. }
  2304. /**
  2305. * dev_pm_opp_add() - Add an OPP table from a table definitions
  2306. * @dev: device for which we do this operation
  2307. * @freq: Frequency in Hz for this OPP
  2308. * @u_volt: Voltage in uVolts for this OPP
  2309. *
  2310. * This function adds an opp definition to the opp table and returns status.
  2311. * The opp is made available by default and it can be controlled using
  2312. * dev_pm_opp_enable/disable functions.
  2313. *
  2314. * Return:
  2315. * 0 On success OR
  2316. * Duplicate OPPs (both freq and volt are same) and opp->available
  2317. * -EEXIST Freq are same and volt are different OR
  2318. * Duplicate OPPs (both freq and volt are same) and !opp->available
  2319. * -ENOMEM Memory allocation failure
  2320. */
  2321. int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
  2322. {
  2323. struct opp_table *opp_table;
  2324. int ret;
  2325. opp_table = _add_opp_table(dev, true);
  2326. if (IS_ERR(opp_table))
  2327. return PTR_ERR(opp_table);
  2328. /* Fix regulator count for dynamic OPPs */
  2329. opp_table->regulator_count = 1;
  2330. ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
  2331. if (ret)
  2332. dev_pm_opp_put_opp_table(opp_table);
  2333. return ret;
  2334. }
  2335. EXPORT_SYMBOL_GPL(dev_pm_opp_add);
  2336. /**
  2337. * _opp_set_availability() - helper to set the availability of an opp
  2338. * @dev: device for which we do this operation
  2339. * @freq: OPP frequency to modify availability
  2340. * @availability_req: availability status requested for this opp
  2341. *
  2342. * Set the availability of an OPP, opp_{enable,disable} share a common logic
  2343. * which is isolated here.
  2344. *
  2345. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  2346. * copy operation, returns 0 if no modification was done OR modification was
  2347. * successful.
  2348. */
  2349. static int _opp_set_availability(struct device *dev, unsigned long freq,
  2350. bool availability_req)
  2351. {
  2352. struct opp_table *opp_table;
  2353. struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
  2354. int r = 0;
  2355. /* Find the opp_table */
  2356. opp_table = _find_opp_table(dev);
  2357. if (IS_ERR(opp_table)) {
  2358. r = PTR_ERR(opp_table);
  2359. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  2360. return r;
  2361. }
  2362. if (!assert_single_clk(opp_table)) {
  2363. r = -EINVAL;
  2364. goto put_table;
  2365. }
  2366. mutex_lock(&opp_table->lock);
  2367. /* Do we have the frequency? */
  2368. list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
  2369. if (tmp_opp->rates[0] == freq) {
  2370. opp = tmp_opp;
  2371. break;
  2372. }
  2373. }
  2374. if (IS_ERR(opp)) {
  2375. r = PTR_ERR(opp);
  2376. goto unlock;
  2377. }
  2378. /* Is update really needed? */
  2379. if (opp->available == availability_req)
  2380. goto unlock;
  2381. opp->available = availability_req;
  2382. dev_pm_opp_get(opp);
  2383. mutex_unlock(&opp_table->lock);
  2384. /* Notify the change of the OPP availability */
  2385. if (availability_req)
  2386. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
  2387. opp);
  2388. else
  2389. blocking_notifier_call_chain(&opp_table->head,
  2390. OPP_EVENT_DISABLE, opp);
  2391. dev_pm_opp_put(opp);
  2392. goto put_table;
  2393. unlock:
  2394. mutex_unlock(&opp_table->lock);
  2395. put_table:
  2396. dev_pm_opp_put_opp_table(opp_table);
  2397. return r;
  2398. }
  2399. /**
  2400. * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
  2401. * @dev: device for which we do this operation
  2402. * @freq: OPP frequency to adjust voltage of
  2403. * @u_volt: new OPP target voltage
  2404. * @u_volt_min: new OPP min voltage
  2405. * @u_volt_max: new OPP max voltage
  2406. *
  2407. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  2408. * copy operation, returns 0 if no modifcation was done OR modification was
  2409. * successful.
  2410. */
  2411. int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
  2412. unsigned long u_volt, unsigned long u_volt_min,
  2413. unsigned long u_volt_max)
  2414. {
  2415. struct opp_table *opp_table;
  2416. struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
  2417. int r = 0;
  2418. /* Find the opp_table */
  2419. opp_table = _find_opp_table(dev);
  2420. if (IS_ERR(opp_table)) {
  2421. r = PTR_ERR(opp_table);
  2422. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  2423. return r;
  2424. }
  2425. if (!assert_single_clk(opp_table)) {
  2426. r = -EINVAL;
  2427. goto put_table;
  2428. }
  2429. mutex_lock(&opp_table->lock);
  2430. /* Do we have the frequency? */
  2431. list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
  2432. if (tmp_opp->rates[0] == freq) {
  2433. opp = tmp_opp;
  2434. break;
  2435. }
  2436. }
  2437. if (IS_ERR(opp)) {
  2438. r = PTR_ERR(opp);
  2439. goto adjust_unlock;
  2440. }
  2441. /* Is update really needed? */
  2442. if (opp->supplies->u_volt == u_volt)
  2443. goto adjust_unlock;
  2444. opp->supplies->u_volt = u_volt;
  2445. opp->supplies->u_volt_min = u_volt_min;
  2446. opp->supplies->u_volt_max = u_volt_max;
  2447. dev_pm_opp_get(opp);
  2448. mutex_unlock(&opp_table->lock);
  2449. /* Notify the voltage change of the OPP */
  2450. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
  2451. opp);
  2452. dev_pm_opp_put(opp);
  2453. goto put_table;
  2454. adjust_unlock:
  2455. mutex_unlock(&opp_table->lock);
  2456. put_table:
  2457. dev_pm_opp_put_opp_table(opp_table);
  2458. return r;
  2459. }
  2460. EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
  2461. /**
  2462. * dev_pm_opp_enable() - Enable a specific OPP
  2463. * @dev: device for which we do this operation
  2464. * @freq: OPP frequency to enable
  2465. *
  2466. * Enables a provided opp. If the operation is valid, this returns 0, else the
  2467. * corresponding error value. It is meant to be used for users an OPP available
  2468. * after being temporarily made unavailable with dev_pm_opp_disable.
  2469. *
  2470. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  2471. * copy operation, returns 0 if no modification was done OR modification was
  2472. * successful.
  2473. */
  2474. int dev_pm_opp_enable(struct device *dev, unsigned long freq)
  2475. {
  2476. return _opp_set_availability(dev, freq, true);
  2477. }
  2478. EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
  2479. /**
  2480. * dev_pm_opp_disable() - Disable a specific OPP
  2481. * @dev: device for which we do this operation
  2482. * @freq: OPP frequency to disable
  2483. *
  2484. * Disables a provided opp. If the operation is valid, this returns
  2485. * 0, else the corresponding error value. It is meant to be a temporary
  2486. * control by users to make this OPP not available until the circumstances are
  2487. * right to make it available again (with a call to dev_pm_opp_enable).
  2488. *
  2489. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  2490. * copy operation, returns 0 if no modification was done OR modification was
  2491. * successful.
  2492. */
  2493. int dev_pm_opp_disable(struct device *dev, unsigned long freq)
  2494. {
  2495. return _opp_set_availability(dev, freq, false);
  2496. }
  2497. EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
  2498. /**
  2499. * dev_pm_opp_register_notifier() - Register OPP notifier for the device
  2500. * @dev: Device for which notifier needs to be registered
  2501. * @nb: Notifier block to be registered
  2502. *
  2503. * Return: 0 on success or a negative error value.
  2504. */
  2505. int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
  2506. {
  2507. struct opp_table *opp_table;
  2508. int ret;
  2509. opp_table = _find_opp_table(dev);
  2510. if (IS_ERR(opp_table))
  2511. return PTR_ERR(opp_table);
  2512. ret = blocking_notifier_chain_register(&opp_table->head, nb);
  2513. dev_pm_opp_put_opp_table(opp_table);
  2514. return ret;
  2515. }
  2516. EXPORT_SYMBOL(dev_pm_opp_register_notifier);
  2517. /**
  2518. * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
  2519. * @dev: Device for which notifier needs to be unregistered
  2520. * @nb: Notifier block to be unregistered
  2521. *
  2522. * Return: 0 on success or a negative error value.
  2523. */
  2524. int dev_pm_opp_unregister_notifier(struct device *dev,
  2525. struct notifier_block *nb)
  2526. {
  2527. struct opp_table *opp_table;
  2528. int ret;
  2529. opp_table = _find_opp_table(dev);
  2530. if (IS_ERR(opp_table))
  2531. return PTR_ERR(opp_table);
  2532. ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
  2533. dev_pm_opp_put_opp_table(opp_table);
  2534. return ret;
  2535. }
  2536. EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
  2537. /**
  2538. * dev_pm_opp_remove_table() - Free all OPPs associated with the device
  2539. * @dev: device pointer used to lookup OPP table.
  2540. *
  2541. * Free both OPPs created using static entries present in DT and the
  2542. * dynamically added entries.
  2543. */
  2544. void dev_pm_opp_remove_table(struct device *dev)
  2545. {
  2546. struct opp_table *opp_table;
  2547. /* Check for existing table for 'dev' */
  2548. opp_table = _find_opp_table(dev);
  2549. if (IS_ERR(opp_table)) {
  2550. int error = PTR_ERR(opp_table);
  2551. if (error != -ENODEV)
  2552. WARN(1, "%s: opp_table: %d\n",
  2553. IS_ERR_OR_NULL(dev) ?
  2554. "Invalid device" : dev_name(dev),
  2555. error);
  2556. return;
  2557. }
  2558. /*
  2559. * Drop the extra reference only if the OPP table was successfully added
  2560. * with dev_pm_opp_of_add_table() earlier.
  2561. **/
  2562. if (_opp_remove_all_static(opp_table))
  2563. dev_pm_opp_put_opp_table(opp_table);
  2564. /* Drop reference taken by _find_opp_table() */
  2565. dev_pm_opp_put_opp_table(opp_table);
  2566. }
  2567. EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
  2568. /**
  2569. * dev_pm_opp_sync_regulators() - Sync state of voltage regulators
  2570. * @dev: device for which we do this operation
  2571. *
  2572. * Sync voltage state of the OPP table regulators.
  2573. *
  2574. * Return: 0 on success or a negative error value.
  2575. */
  2576. int dev_pm_opp_sync_regulators(struct device *dev)
  2577. {
  2578. struct opp_table *opp_table;
  2579. struct regulator *reg;
  2580. int i, ret = 0;
  2581. /* Device may not have OPP table */
  2582. opp_table = _find_opp_table(dev);
  2583. if (IS_ERR(opp_table))
  2584. return 0;
  2585. /* Regulator may not be required for the device */
  2586. if (unlikely(!opp_table->regulators))
  2587. goto put_table;
  2588. /* Nothing to sync if voltage wasn't changed */
  2589. if (!opp_table->enabled)
  2590. goto put_table;
  2591. for (i = 0; i < opp_table->regulator_count; i++) {
  2592. reg = opp_table->regulators[i];
  2593. ret = regulator_sync_voltage(reg);
  2594. if (ret)
  2595. break;
  2596. }
  2597. put_table:
  2598. /* Drop reference taken by _find_opp_table() */
  2599. dev_pm_opp_put_opp_table(opp_table);
  2600. return ret;
  2601. }
  2602. EXPORT_SYMBOL_GPL(dev_pm_opp_sync_regulators);