core.c 84 KB

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