core.c 85 KB

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