core.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464
  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/slab.h>
  15. #include <linux/device.h>
  16. #include <linux/export.h>
  17. #include <linux/pm_domain.h>
  18. #include <linux/regulator/consumer.h>
  19. #include "opp.h"
  20. /*
  21. * The root of the list of all opp-tables. All opp_table structures branch off
  22. * from here, with each opp_table containing the list of opps it supports in
  23. * various states of availability.
  24. */
  25. LIST_HEAD(opp_tables);
  26. /* Lock to allow exclusive modification to the device and opp lists */
  27. DEFINE_MUTEX(opp_table_lock);
  28. static struct opp_device *_find_opp_dev(const struct device *dev,
  29. struct opp_table *opp_table)
  30. {
  31. struct opp_device *opp_dev;
  32. list_for_each_entry(opp_dev, &opp_table->dev_list, node)
  33. if (opp_dev->dev == dev)
  34. return opp_dev;
  35. return NULL;
  36. }
  37. static struct opp_table *_find_opp_table_unlocked(struct device *dev)
  38. {
  39. struct opp_table *opp_table;
  40. bool found;
  41. list_for_each_entry(opp_table, &opp_tables, node) {
  42. mutex_lock(&opp_table->lock);
  43. found = !!_find_opp_dev(dev, opp_table);
  44. mutex_unlock(&opp_table->lock);
  45. if (found) {
  46. _get_opp_table_kref(opp_table);
  47. return opp_table;
  48. }
  49. }
  50. return ERR_PTR(-ENODEV);
  51. }
  52. /**
  53. * _find_opp_table() - find opp_table struct using device pointer
  54. * @dev: device pointer used to lookup OPP table
  55. *
  56. * Search OPP table for one containing matching device.
  57. *
  58. * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or
  59. * -EINVAL based on type of error.
  60. *
  61. * The callers must call dev_pm_opp_put_opp_table() after the table is used.
  62. */
  63. struct opp_table *_find_opp_table(struct device *dev)
  64. {
  65. struct opp_table *opp_table;
  66. if (IS_ERR_OR_NULL(dev)) {
  67. pr_err("%s: Invalid parameters\n", __func__);
  68. return ERR_PTR(-EINVAL);
  69. }
  70. mutex_lock(&opp_table_lock);
  71. opp_table = _find_opp_table_unlocked(dev);
  72. mutex_unlock(&opp_table_lock);
  73. return opp_table;
  74. }
  75. /**
  76. * dev_pm_opp_get_voltage() - Gets the voltage corresponding to an opp
  77. * @opp: opp for which voltage has to be returned for
  78. *
  79. * Return: voltage in micro volt corresponding to the opp, else
  80. * return 0
  81. *
  82. * This is useful only for devices with single power supply.
  83. */
  84. unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp)
  85. {
  86. if (IS_ERR_OR_NULL(opp)) {
  87. pr_err("%s: Invalid parameters\n", __func__);
  88. return 0;
  89. }
  90. return opp->supplies[0].u_volt;
  91. }
  92. EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage);
  93. /**
  94. * dev_pm_opp_get_freq() - Gets the frequency corresponding to an available opp
  95. * @opp: opp for which frequency has to be returned for
  96. *
  97. * Return: frequency in hertz corresponding to the opp, else
  98. * return 0
  99. */
  100. unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp)
  101. {
  102. if (IS_ERR_OR_NULL(opp)) {
  103. pr_err("%s: Invalid parameters\n", __func__);
  104. return 0;
  105. }
  106. return opp->rate;
  107. }
  108. EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq);
  109. /**
  110. * dev_pm_opp_get_level() - Gets the level corresponding to an available opp
  111. * @opp: opp for which level value has to be returned for
  112. *
  113. * Return: level read from device tree corresponding to the opp, else
  114. * return 0.
  115. */
  116. unsigned int dev_pm_opp_get_level(struct dev_pm_opp *opp)
  117. {
  118. if (IS_ERR_OR_NULL(opp) || !opp->available) {
  119. pr_err("%s: Invalid parameters\n", __func__);
  120. return 0;
  121. }
  122. return opp->level;
  123. }
  124. EXPORT_SYMBOL_GPL(dev_pm_opp_get_level);
  125. /**
  126. * dev_pm_opp_is_turbo() - Returns if opp is turbo OPP or not
  127. * @opp: opp for which turbo mode is being verified
  128. *
  129. * Turbo OPPs are not for normal use, and can be enabled (under certain
  130. * conditions) for short duration of times to finish high throughput work
  131. * quickly. Running on them for longer times may overheat the chip.
  132. *
  133. * Return: true if opp is turbo opp, else false.
  134. */
  135. bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp)
  136. {
  137. if (IS_ERR_OR_NULL(opp) || !opp->available) {
  138. pr_err("%s: Invalid parameters\n", __func__);
  139. return false;
  140. }
  141. return opp->turbo;
  142. }
  143. EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo);
  144. /**
  145. * dev_pm_opp_get_max_clock_latency() - Get max clock latency in nanoseconds
  146. * @dev: device for which we do this operation
  147. *
  148. * Return: This function returns the max clock latency in nanoseconds.
  149. */
  150. unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev)
  151. {
  152. struct opp_table *opp_table;
  153. unsigned long clock_latency_ns;
  154. opp_table = _find_opp_table(dev);
  155. if (IS_ERR(opp_table))
  156. return 0;
  157. clock_latency_ns = opp_table->clock_latency_ns_max;
  158. dev_pm_opp_put_opp_table(opp_table);
  159. return clock_latency_ns;
  160. }
  161. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency);
  162. /**
  163. * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds
  164. * @dev: device for which we do this operation
  165. *
  166. * Return: This function returns the max voltage latency in nanoseconds.
  167. */
  168. unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev)
  169. {
  170. struct opp_table *opp_table;
  171. struct dev_pm_opp *opp;
  172. struct regulator *reg;
  173. unsigned long latency_ns = 0;
  174. int ret, i, count;
  175. struct {
  176. unsigned long min;
  177. unsigned long max;
  178. } *uV;
  179. opp_table = _find_opp_table(dev);
  180. if (IS_ERR(opp_table))
  181. return 0;
  182. /* Regulator may not be required for the device */
  183. if (!opp_table->regulators)
  184. goto put_opp_table;
  185. count = opp_table->regulator_count;
  186. uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
  187. if (!uV)
  188. goto put_opp_table;
  189. mutex_lock(&opp_table->lock);
  190. for (i = 0; i < count; i++) {
  191. uV[i].min = ~0;
  192. uV[i].max = 0;
  193. list_for_each_entry(opp, &opp_table->opp_list, node) {
  194. if (!opp->available)
  195. continue;
  196. if (opp->supplies[i].u_volt_min < uV[i].min)
  197. uV[i].min = opp->supplies[i].u_volt_min;
  198. if (opp->supplies[i].u_volt_max > uV[i].max)
  199. uV[i].max = opp->supplies[i].u_volt_max;
  200. }
  201. }
  202. mutex_unlock(&opp_table->lock);
  203. /*
  204. * The caller needs to ensure that opp_table (and hence the regulator)
  205. * isn't freed, while we are executing this routine.
  206. */
  207. for (i = 0; i < count; i++) {
  208. reg = opp_table->regulators[i];
  209. ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
  210. if (ret > 0)
  211. latency_ns += ret * 1000;
  212. }
  213. kfree(uV);
  214. put_opp_table:
  215. dev_pm_opp_put_opp_table(opp_table);
  216. return latency_ns;
  217. }
  218. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
  219. /**
  220. * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
  221. * nanoseconds
  222. * @dev: device for which we do this operation
  223. *
  224. * Return: This function returns the max transition latency, in nanoseconds, to
  225. * switch from one OPP to other.
  226. */
  227. unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
  228. {
  229. return dev_pm_opp_get_max_volt_latency(dev) +
  230. dev_pm_opp_get_max_clock_latency(dev);
  231. }
  232. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
  233. /**
  234. * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
  235. * @dev: device for which we do this operation
  236. *
  237. * Return: This function returns the frequency of the OPP marked as suspend_opp
  238. * if one is available, else returns 0;
  239. */
  240. unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
  241. {
  242. struct opp_table *opp_table;
  243. unsigned long freq = 0;
  244. opp_table = _find_opp_table(dev);
  245. if (IS_ERR(opp_table))
  246. return 0;
  247. if (opp_table->suspend_opp && opp_table->suspend_opp->available)
  248. freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
  249. dev_pm_opp_put_opp_table(opp_table);
  250. return freq;
  251. }
  252. EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
  253. int _get_opp_count(struct opp_table *opp_table)
  254. {
  255. struct dev_pm_opp *opp;
  256. int count = 0;
  257. mutex_lock(&opp_table->lock);
  258. list_for_each_entry(opp, &opp_table->opp_list, node) {
  259. if (opp->available)
  260. count++;
  261. }
  262. mutex_unlock(&opp_table->lock);
  263. return count;
  264. }
  265. /**
  266. * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
  267. * @dev: device for which we do this operation
  268. *
  269. * Return: This function returns the number of available opps if there are any,
  270. * else returns 0 if none or the corresponding error value.
  271. */
  272. int dev_pm_opp_get_opp_count(struct device *dev)
  273. {
  274. struct opp_table *opp_table;
  275. int count;
  276. opp_table = _find_opp_table(dev);
  277. if (IS_ERR(opp_table)) {
  278. count = PTR_ERR(opp_table);
  279. dev_dbg(dev, "%s: OPP table not found (%d)\n",
  280. __func__, count);
  281. return count;
  282. }
  283. count = _get_opp_count(opp_table);
  284. dev_pm_opp_put_opp_table(opp_table);
  285. return count;
  286. }
  287. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count);
  288. /**
  289. * dev_pm_opp_find_freq_exact() - search for an exact frequency
  290. * @dev: device for which we do this operation
  291. * @freq: frequency to search for
  292. * @available: true/false - match for available opp
  293. *
  294. * Return: Searches for exact match in the opp table and returns pointer to the
  295. * matching opp if found, else returns ERR_PTR in case of error and should
  296. * be handled using IS_ERR. Error return values can be:
  297. * EINVAL: for bad pointer
  298. * ERANGE: no match found for search
  299. * ENODEV: if device not found in list of registered devices
  300. *
  301. * Note: available is a modifier for the search. if available=true, then the
  302. * match is for exact matching frequency and is available in the stored OPP
  303. * table. if false, the match is for exact frequency which is not available.
  304. *
  305. * This provides a mechanism to enable an opp which is not available currently
  306. * or the opposite as well.
  307. *
  308. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  309. * use.
  310. */
  311. struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev,
  312. unsigned long freq,
  313. bool available)
  314. {
  315. struct opp_table *opp_table;
  316. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  317. opp_table = _find_opp_table(dev);
  318. if (IS_ERR(opp_table)) {
  319. int r = PTR_ERR(opp_table);
  320. dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
  321. return ERR_PTR(r);
  322. }
  323. mutex_lock(&opp_table->lock);
  324. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  325. if (temp_opp->available == available &&
  326. temp_opp->rate == freq) {
  327. opp = temp_opp;
  328. /* Increment the reference count of OPP */
  329. dev_pm_opp_get(opp);
  330. break;
  331. }
  332. }
  333. mutex_unlock(&opp_table->lock);
  334. dev_pm_opp_put_opp_table(opp_table);
  335. return opp;
  336. }
  337. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_exact);
  338. /**
  339. * dev_pm_opp_find_level_exact() - search for an exact level
  340. * @dev: device for which we do this operation
  341. * @level: level to search for
  342. *
  343. * Return: Searches for exact match in the opp table and returns pointer to the
  344. * matching opp if found, else returns ERR_PTR in case of error and should
  345. * be handled using IS_ERR. Error return values can be:
  346. * EINVAL: for bad pointer
  347. * ERANGE: no match found for search
  348. * ENODEV: if device not found in list of registered devices
  349. *
  350. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  351. * use.
  352. */
  353. struct dev_pm_opp *dev_pm_opp_find_level_exact(struct device *dev,
  354. unsigned int level)
  355. {
  356. struct opp_table *opp_table;
  357. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  358. opp_table = _find_opp_table(dev);
  359. if (IS_ERR(opp_table)) {
  360. int r = PTR_ERR(opp_table);
  361. dev_err(dev, "%s: OPP table not found (%d)\n", __func__, r);
  362. return ERR_PTR(r);
  363. }
  364. mutex_lock(&opp_table->lock);
  365. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  366. if (temp_opp->level == level) {
  367. opp = temp_opp;
  368. /* Increment the reference count of OPP */
  369. dev_pm_opp_get(opp);
  370. break;
  371. }
  372. }
  373. mutex_unlock(&opp_table->lock);
  374. dev_pm_opp_put_opp_table(opp_table);
  375. return opp;
  376. }
  377. EXPORT_SYMBOL_GPL(dev_pm_opp_find_level_exact);
  378. static noinline struct dev_pm_opp *_find_freq_ceil(struct opp_table *opp_table,
  379. unsigned long *freq)
  380. {
  381. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  382. mutex_lock(&opp_table->lock);
  383. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  384. if (temp_opp->available && temp_opp->rate >= *freq) {
  385. opp = temp_opp;
  386. *freq = opp->rate;
  387. /* Increment the reference count of OPP */
  388. dev_pm_opp_get(opp);
  389. break;
  390. }
  391. }
  392. mutex_unlock(&opp_table->lock);
  393. return opp;
  394. }
  395. /**
  396. * dev_pm_opp_find_freq_ceil() - Search for an rounded ceil freq
  397. * @dev: device for which we do this operation
  398. * @freq: Start frequency
  399. *
  400. * Search for the matching ceil *available* OPP from a starting freq
  401. * for a device.
  402. *
  403. * Return: matching *opp and refreshes *freq accordingly, else returns
  404. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  405. * values can be:
  406. * EINVAL: for bad pointer
  407. * ERANGE: no match found for search
  408. * ENODEV: if device not found in list of registered devices
  409. *
  410. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  411. * use.
  412. */
  413. struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev,
  414. unsigned long *freq)
  415. {
  416. struct opp_table *opp_table;
  417. struct dev_pm_opp *opp;
  418. if (!dev || !freq) {
  419. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  420. return ERR_PTR(-EINVAL);
  421. }
  422. opp_table = _find_opp_table(dev);
  423. if (IS_ERR(opp_table))
  424. return ERR_CAST(opp_table);
  425. opp = _find_freq_ceil(opp_table, freq);
  426. dev_pm_opp_put_opp_table(opp_table);
  427. return opp;
  428. }
  429. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil);
  430. /**
  431. * dev_pm_opp_find_freq_floor() - Search for a rounded floor freq
  432. * @dev: device for which we do this operation
  433. * @freq: Start frequency
  434. *
  435. * Search for the matching floor *available* OPP from a starting freq
  436. * for a device.
  437. *
  438. * Return: matching *opp and refreshes *freq accordingly, else returns
  439. * ERR_PTR in case of error and should be handled using IS_ERR. Error return
  440. * values can be:
  441. * EINVAL: for bad pointer
  442. * ERANGE: no match found for search
  443. * ENODEV: if device not found in list of registered devices
  444. *
  445. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  446. * use.
  447. */
  448. struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev,
  449. unsigned long *freq)
  450. {
  451. struct opp_table *opp_table;
  452. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  453. if (!dev || !freq) {
  454. dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq);
  455. return ERR_PTR(-EINVAL);
  456. }
  457. opp_table = _find_opp_table(dev);
  458. if (IS_ERR(opp_table))
  459. return ERR_CAST(opp_table);
  460. mutex_lock(&opp_table->lock);
  461. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  462. if (temp_opp->available) {
  463. /* go to the next node, before choosing prev */
  464. if (temp_opp->rate > *freq)
  465. break;
  466. else
  467. opp = temp_opp;
  468. }
  469. }
  470. /* Increment the reference count of OPP */
  471. if (!IS_ERR(opp))
  472. dev_pm_opp_get(opp);
  473. mutex_unlock(&opp_table->lock);
  474. dev_pm_opp_put_opp_table(opp_table);
  475. if (!IS_ERR(opp))
  476. *freq = opp->rate;
  477. return opp;
  478. }
  479. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor);
  480. /**
  481. * dev_pm_opp_find_freq_ceil_by_volt() - Find OPP with highest frequency for
  482. * target voltage.
  483. * @dev: Device for which we do this operation.
  484. * @u_volt: Target voltage.
  485. *
  486. * Search for OPP with highest (ceil) frequency and has voltage <= u_volt.
  487. *
  488. * Return: matching *opp, else returns ERR_PTR in case of error which should be
  489. * handled using IS_ERR.
  490. *
  491. * Error return values can be:
  492. * EINVAL: bad parameters
  493. *
  494. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  495. * use.
  496. */
  497. struct dev_pm_opp *dev_pm_opp_find_freq_ceil_by_volt(struct device *dev,
  498. unsigned long u_volt)
  499. {
  500. struct opp_table *opp_table;
  501. struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE);
  502. if (!dev || !u_volt) {
  503. dev_err(dev, "%s: Invalid argument volt=%lu\n", __func__,
  504. u_volt);
  505. return ERR_PTR(-EINVAL);
  506. }
  507. opp_table = _find_opp_table(dev);
  508. if (IS_ERR(opp_table))
  509. return ERR_CAST(opp_table);
  510. mutex_lock(&opp_table->lock);
  511. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  512. if (temp_opp->available) {
  513. if (temp_opp->supplies[0].u_volt > u_volt)
  514. break;
  515. opp = temp_opp;
  516. }
  517. }
  518. /* Increment the reference count of OPP */
  519. if (!IS_ERR(opp))
  520. dev_pm_opp_get(opp);
  521. mutex_unlock(&opp_table->lock);
  522. dev_pm_opp_put_opp_table(opp_table);
  523. return opp;
  524. }
  525. EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil_by_volt);
  526. static int _set_opp_voltage(struct device *dev, struct regulator *reg,
  527. struct dev_pm_opp_supply *supply)
  528. {
  529. int ret;
  530. /* Regulator not available for device */
  531. if (IS_ERR(reg)) {
  532. dev_dbg(dev, "%s: regulator not available: %ld\n", __func__,
  533. PTR_ERR(reg));
  534. return 0;
  535. }
  536. dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__,
  537. supply->u_volt_min, supply->u_volt, supply->u_volt_max);
  538. ret = regulator_set_voltage_triplet(reg, supply->u_volt_min,
  539. supply->u_volt, supply->u_volt_max);
  540. if (ret)
  541. dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n",
  542. __func__, supply->u_volt_min, supply->u_volt,
  543. supply->u_volt_max, ret);
  544. return ret;
  545. }
  546. static inline int _generic_set_opp_clk_only(struct device *dev, struct clk *clk,
  547. unsigned long freq)
  548. {
  549. int ret;
  550. ret = clk_set_rate(clk, freq);
  551. if (ret) {
  552. dev_err(dev, "%s: failed to set clock rate: %d\n", __func__,
  553. ret);
  554. }
  555. return ret;
  556. }
  557. static int _generic_set_opp_regulator(struct opp_table *opp_table,
  558. struct device *dev,
  559. unsigned long old_freq,
  560. unsigned long freq,
  561. struct dev_pm_opp_supply *old_supply,
  562. struct dev_pm_opp_supply *new_supply)
  563. {
  564. struct regulator *reg = opp_table->regulators[0];
  565. int ret;
  566. /* This function only supports single regulator per device */
  567. if (WARN_ON(opp_table->regulator_count > 1)) {
  568. dev_err(dev, "multiple regulators are not supported\n");
  569. return -EINVAL;
  570. }
  571. /* Scaling up? Scale voltage before frequency */
  572. if (freq >= old_freq) {
  573. ret = _set_opp_voltage(dev, reg, new_supply);
  574. if (ret)
  575. goto restore_voltage;
  576. }
  577. /* Change frequency */
  578. ret = _generic_set_opp_clk_only(dev, opp_table->clk, freq);
  579. if (ret)
  580. goto restore_voltage;
  581. /* Scaling down? Scale voltage after frequency */
  582. if (freq < old_freq) {
  583. ret = _set_opp_voltage(dev, reg, new_supply);
  584. if (ret)
  585. goto restore_freq;
  586. }
  587. /*
  588. * Enable the regulator after setting its voltages, otherwise it breaks
  589. * some boot-enabled regulators.
  590. */
  591. if (unlikely(!opp_table->enabled)) {
  592. ret = regulator_enable(reg);
  593. if (ret < 0)
  594. dev_warn(dev, "Failed to enable regulator: %d", ret);
  595. }
  596. return 0;
  597. restore_freq:
  598. if (_generic_set_opp_clk_only(dev, opp_table->clk, old_freq))
  599. dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
  600. __func__, old_freq);
  601. restore_voltage:
  602. /* This shouldn't harm even if the voltages weren't updated earlier */
  603. if (old_supply)
  604. _set_opp_voltage(dev, reg, old_supply);
  605. return ret;
  606. }
  607. static int _set_opp_bw(const struct opp_table *opp_table,
  608. struct dev_pm_opp *opp, struct device *dev, bool remove)
  609. {
  610. u32 avg, peak;
  611. int i, ret;
  612. if (!opp_table->paths)
  613. return 0;
  614. for (i = 0; i < opp_table->path_count; i++) {
  615. if (remove) {
  616. avg = 0;
  617. peak = 0;
  618. } else {
  619. avg = opp->bandwidth[i].avg;
  620. peak = opp->bandwidth[i].peak;
  621. }
  622. ret = icc_set_bw(opp_table->paths[i], avg, peak);
  623. if (ret) {
  624. dev_err(dev, "Failed to %s bandwidth[%d]: %d\n",
  625. remove ? "remove" : "set", i, ret);
  626. return ret;
  627. }
  628. }
  629. return 0;
  630. }
  631. static int _set_opp_custom(const struct opp_table *opp_table,
  632. struct device *dev, unsigned long old_freq,
  633. unsigned long freq,
  634. struct dev_pm_opp_supply *old_supply,
  635. struct dev_pm_opp_supply *new_supply)
  636. {
  637. struct dev_pm_set_opp_data *data;
  638. int size;
  639. data = opp_table->set_opp_data;
  640. data->regulators = opp_table->regulators;
  641. data->regulator_count = opp_table->regulator_count;
  642. data->clk = opp_table->clk;
  643. data->dev = dev;
  644. data->old_opp.rate = old_freq;
  645. size = sizeof(*old_supply) * opp_table->regulator_count;
  646. if (!old_supply)
  647. memset(data->old_opp.supplies, 0, size);
  648. else
  649. memcpy(data->old_opp.supplies, old_supply, size);
  650. data->new_opp.rate = freq;
  651. memcpy(data->new_opp.supplies, new_supply, size);
  652. return opp_table->set_opp(data);
  653. }
  654. static int _set_required_opp(struct device *dev, struct device *pd_dev,
  655. struct dev_pm_opp *opp, int i)
  656. {
  657. unsigned int pstate = likely(opp) ? opp->required_opps[i]->pstate : 0;
  658. int ret;
  659. if (!pd_dev)
  660. return 0;
  661. ret = dev_pm_genpd_set_performance_state(pd_dev, pstate);
  662. if (ret) {
  663. dev_err(dev, "Failed to set performance rate of %s: %d (%d)\n",
  664. dev_name(pd_dev), pstate, ret);
  665. }
  666. return ret;
  667. }
  668. /* This is only called for PM domain for now */
  669. static int _set_required_opps(struct device *dev,
  670. struct opp_table *opp_table,
  671. struct dev_pm_opp *opp, bool up)
  672. {
  673. struct opp_table **required_opp_tables = opp_table->required_opp_tables;
  674. struct device **genpd_virt_devs = opp_table->genpd_virt_devs;
  675. int i, ret = 0;
  676. if (!required_opp_tables)
  677. return 0;
  678. /* Single genpd case */
  679. if (!genpd_virt_devs)
  680. return _set_required_opp(dev, dev, opp, 0);
  681. /* Multiple genpd case */
  682. /*
  683. * Acquire genpd_virt_dev_lock to make sure we don't use a genpd_dev
  684. * after it is freed from another thread.
  685. */
  686. mutex_lock(&opp_table->genpd_virt_dev_lock);
  687. /* Scaling up? Set required OPPs in normal order, else reverse */
  688. if (up) {
  689. for (i = 0; i < opp_table->required_opp_count; i++) {
  690. ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
  691. if (ret)
  692. break;
  693. }
  694. } else {
  695. for (i = opp_table->required_opp_count - 1; i >= 0; i--) {
  696. ret = _set_required_opp(dev, genpd_virt_devs[i], opp, i);
  697. if (ret)
  698. break;
  699. }
  700. }
  701. mutex_unlock(&opp_table->genpd_virt_dev_lock);
  702. return ret;
  703. }
  704. /**
  705. * dev_pm_opp_set_bw() - sets bandwidth levels corresponding to an opp
  706. * @dev: device for which we do this operation
  707. * @opp: opp based on which the bandwidth levels are to be configured
  708. *
  709. * This configures the bandwidth to the levels specified by the OPP. However
  710. * if the OPP specified is NULL the bandwidth levels are cleared out.
  711. *
  712. * Return: 0 on success or a negative error value.
  713. */
  714. int dev_pm_opp_set_bw(struct device *dev, struct dev_pm_opp *opp)
  715. {
  716. struct opp_table *opp_table;
  717. int ret;
  718. opp_table = _find_opp_table(dev);
  719. if (IS_ERR(opp_table)) {
  720. dev_err(dev, "%s: device opp table doesn't exist\n", __func__);
  721. return PTR_ERR(opp_table);
  722. }
  723. if (opp)
  724. ret = _set_opp_bw(opp_table, opp, dev, false);
  725. else
  726. ret = _set_opp_bw(opp_table, NULL, dev, true);
  727. dev_pm_opp_put_opp_table(opp_table);
  728. return ret;
  729. }
  730. EXPORT_SYMBOL_GPL(dev_pm_opp_set_bw);
  731. static int _opp_set_rate_zero(struct device *dev, struct opp_table *opp_table)
  732. {
  733. int ret;
  734. if (!opp_table->enabled)
  735. return 0;
  736. /*
  737. * Some drivers need to support cases where some platforms may
  738. * have OPP table for the device, while others don't and
  739. * opp_set_rate() just needs to behave like clk_set_rate().
  740. */
  741. if (!_get_opp_count(opp_table))
  742. return 0;
  743. ret = _set_opp_bw(opp_table, NULL, dev, true);
  744. if (ret)
  745. return ret;
  746. if (opp_table->regulators)
  747. regulator_disable(opp_table->regulators[0]);
  748. ret = _set_required_opps(dev, opp_table, NULL, false);
  749. opp_table->enabled = false;
  750. return ret;
  751. }
  752. /**
  753. * dev_pm_opp_set_rate() - Configure new OPP based on frequency
  754. * @dev: device for which we do this operation
  755. * @target_freq: frequency to achieve
  756. *
  757. * This configures the power-supplies to the levels specified by the OPP
  758. * corresponding to the target_freq, and programs the clock to a value <=
  759. * target_freq, as rounded by clk_round_rate(). Device wanting to run at fmax
  760. * provided by the opp, should have already rounded to the target OPP's
  761. * frequency.
  762. */
  763. int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
  764. {
  765. struct opp_table *opp_table;
  766. unsigned long freq, old_freq, temp_freq;
  767. struct dev_pm_opp *old_opp, *opp;
  768. struct clk *clk;
  769. int ret;
  770. opp_table = _find_opp_table(dev);
  771. if (IS_ERR(opp_table)) {
  772. dev_err(dev, "%s: device opp doesn't exist\n", __func__);
  773. return PTR_ERR(opp_table);
  774. }
  775. if (unlikely(!target_freq)) {
  776. ret = _opp_set_rate_zero(dev, opp_table);
  777. goto put_opp_table;
  778. }
  779. clk = opp_table->clk;
  780. if (IS_ERR(clk)) {
  781. dev_err(dev, "%s: No clock available for the device\n",
  782. __func__);
  783. ret = PTR_ERR(clk);
  784. goto put_opp_table;
  785. }
  786. freq = clk_round_rate(clk, target_freq);
  787. if ((long)freq <= 0)
  788. freq = target_freq;
  789. old_freq = clk_get_rate(clk);
  790. /* Return early if nothing to do */
  791. if (opp_table->enabled && old_freq == freq) {
  792. dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
  793. __func__, freq);
  794. ret = 0;
  795. goto put_opp_table;
  796. }
  797. /*
  798. * For IO devices which require an OPP on some platforms/SoCs
  799. * while just needing to scale the clock on some others
  800. * we look for empty OPP tables with just a clock handle and
  801. * scale only the clk. This makes dev_pm_opp_set_rate()
  802. * equivalent to a clk_set_rate()
  803. */
  804. if (!_get_opp_count(opp_table)) {
  805. ret = _generic_set_opp_clk_only(dev, clk, freq);
  806. goto put_opp_table;
  807. }
  808. temp_freq = old_freq;
  809. old_opp = _find_freq_ceil(opp_table, &temp_freq);
  810. if (IS_ERR(old_opp)) {
  811. dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
  812. __func__, old_freq, PTR_ERR(old_opp));
  813. }
  814. temp_freq = freq;
  815. opp = _find_freq_ceil(opp_table, &temp_freq);
  816. if (IS_ERR(opp)) {
  817. ret = PTR_ERR(opp);
  818. dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
  819. __func__, freq, ret);
  820. goto put_old_opp;
  821. }
  822. dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
  823. old_freq, freq);
  824. /* Scaling up? Configure required OPPs before frequency */
  825. if (freq >= old_freq) {
  826. ret = _set_required_opps(dev, opp_table, opp, true);
  827. if (ret)
  828. goto put_opp;
  829. }
  830. if (opp_table->set_opp) {
  831. ret = _set_opp_custom(opp_table, dev, old_freq, freq,
  832. IS_ERR(old_opp) ? NULL : old_opp->supplies,
  833. opp->supplies);
  834. } else if (opp_table->regulators) {
  835. ret = _generic_set_opp_regulator(opp_table, dev, old_freq, freq,
  836. IS_ERR(old_opp) ? NULL : old_opp->supplies,
  837. opp->supplies);
  838. } else {
  839. /* Only frequency scaling */
  840. ret = _generic_set_opp_clk_only(dev, clk, freq);
  841. }
  842. /* Scaling down? Configure required OPPs after frequency */
  843. if (!ret && freq < old_freq) {
  844. ret = _set_required_opps(dev, opp_table, opp, false);
  845. if (ret)
  846. dev_err(dev, "Failed to set required opps: %d\n", ret);
  847. }
  848. if (!ret) {
  849. ret = _set_opp_bw(opp_table, opp, dev, false);
  850. if (!ret)
  851. opp_table->enabled = true;
  852. }
  853. put_opp:
  854. dev_pm_opp_put(opp);
  855. put_old_opp:
  856. if (!IS_ERR(old_opp))
  857. dev_pm_opp_put(old_opp);
  858. put_opp_table:
  859. dev_pm_opp_put_opp_table(opp_table);
  860. return ret;
  861. }
  862. EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
  863. /* OPP-dev Helpers */
  864. static void _remove_opp_dev(struct opp_device *opp_dev,
  865. struct opp_table *opp_table)
  866. {
  867. opp_debug_unregister(opp_dev, opp_table);
  868. list_del(&opp_dev->node);
  869. kfree(opp_dev);
  870. }
  871. static struct opp_device *_add_opp_dev_unlocked(const struct device *dev,
  872. struct opp_table *opp_table)
  873. {
  874. struct opp_device *opp_dev;
  875. opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
  876. if (!opp_dev)
  877. return NULL;
  878. /* Initialize opp-dev */
  879. opp_dev->dev = dev;
  880. list_add(&opp_dev->node, &opp_table->dev_list);
  881. /* Create debugfs entries for the opp_table */
  882. opp_debug_register(opp_dev, opp_table);
  883. return opp_dev;
  884. }
  885. struct opp_device *_add_opp_dev(const struct device *dev,
  886. struct opp_table *opp_table)
  887. {
  888. struct opp_device *opp_dev;
  889. mutex_lock(&opp_table->lock);
  890. opp_dev = _add_opp_dev_unlocked(dev, opp_table);
  891. mutex_unlock(&opp_table->lock);
  892. return opp_dev;
  893. }
  894. static struct opp_table *_allocate_opp_table(struct device *dev, int index)
  895. {
  896. struct opp_table *opp_table;
  897. struct opp_device *opp_dev;
  898. int ret;
  899. /*
  900. * Allocate a new OPP table. In the infrequent case where a new
  901. * device is needed to be added, we pay this penalty.
  902. */
  903. opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
  904. if (!opp_table)
  905. return ERR_PTR(-ENOMEM);
  906. mutex_init(&opp_table->lock);
  907. mutex_init(&opp_table->genpd_virt_dev_lock);
  908. INIT_LIST_HEAD(&opp_table->dev_list);
  909. /* Mark regulator count uninitialized */
  910. opp_table->regulator_count = -1;
  911. opp_dev = _add_opp_dev(dev, opp_table);
  912. if (!opp_dev) {
  913. ret = -ENOMEM;
  914. goto err;
  915. }
  916. _of_init_opp_table(opp_table, dev, index);
  917. /* Find clk for the device */
  918. opp_table->clk = clk_get(dev, NULL);
  919. if (IS_ERR(opp_table->clk)) {
  920. ret = PTR_ERR(opp_table->clk);
  921. if (ret == -EPROBE_DEFER)
  922. goto remove_opp_dev;
  923. dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__, ret);
  924. }
  925. /* Find interconnect path(s) for the device */
  926. ret = dev_pm_opp_of_find_icc_paths(dev, opp_table);
  927. if (ret) {
  928. if (ret == -EPROBE_DEFER)
  929. goto put_clk;
  930. dev_warn(dev, "%s: Error finding interconnect paths: %d\n",
  931. __func__, ret);
  932. }
  933. BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
  934. INIT_LIST_HEAD(&opp_table->opp_list);
  935. kref_init(&opp_table->kref);
  936. /* Secure the device table modification */
  937. list_add(&opp_table->node, &opp_tables);
  938. return opp_table;
  939. put_clk:
  940. if (!IS_ERR(opp_table->clk))
  941. clk_put(opp_table->clk);
  942. remove_opp_dev:
  943. _remove_opp_dev(opp_dev, opp_table);
  944. err:
  945. kfree(opp_table);
  946. return ERR_PTR(ret);
  947. }
  948. void _get_opp_table_kref(struct opp_table *opp_table)
  949. {
  950. kref_get(&opp_table->kref);
  951. }
  952. static struct opp_table *_opp_get_opp_table(struct device *dev, int index)
  953. {
  954. struct opp_table *opp_table;
  955. /* Hold our table modification lock here */
  956. mutex_lock(&opp_table_lock);
  957. opp_table = _find_opp_table_unlocked(dev);
  958. if (!IS_ERR(opp_table))
  959. goto unlock;
  960. opp_table = _managed_opp(dev, index);
  961. if (opp_table) {
  962. if (!_add_opp_dev_unlocked(dev, opp_table)) {
  963. dev_pm_opp_put_opp_table(opp_table);
  964. opp_table = ERR_PTR(-ENOMEM);
  965. }
  966. goto unlock;
  967. }
  968. opp_table = _allocate_opp_table(dev, index);
  969. unlock:
  970. mutex_unlock(&opp_table_lock);
  971. return opp_table;
  972. }
  973. struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
  974. {
  975. return _opp_get_opp_table(dev, 0);
  976. }
  977. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
  978. struct opp_table *dev_pm_opp_get_opp_table_indexed(struct device *dev,
  979. int index)
  980. {
  981. return _opp_get_opp_table(dev, index);
  982. }
  983. static void _opp_table_kref_release(struct kref *kref)
  984. {
  985. struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
  986. struct opp_device *opp_dev, *temp;
  987. int i;
  988. /* Drop the lock as soon as we can */
  989. list_del(&opp_table->node);
  990. mutex_unlock(&opp_table_lock);
  991. _of_clear_opp_table(opp_table);
  992. /* Release clk */
  993. if (!IS_ERR(opp_table->clk))
  994. clk_put(opp_table->clk);
  995. if (opp_table->paths) {
  996. for (i = 0; i < opp_table->path_count; i++)
  997. icc_put(opp_table->paths[i]);
  998. kfree(opp_table->paths);
  999. }
  1000. WARN_ON(!list_empty(&opp_table->opp_list));
  1001. list_for_each_entry_safe(opp_dev, temp, &opp_table->dev_list, node) {
  1002. /*
  1003. * The OPP table is getting removed, drop the performance state
  1004. * constraints.
  1005. */
  1006. if (opp_table->genpd_performance_state)
  1007. dev_pm_genpd_set_performance_state((struct device *)(opp_dev->dev), 0);
  1008. _remove_opp_dev(opp_dev, opp_table);
  1009. }
  1010. mutex_destroy(&opp_table->genpd_virt_dev_lock);
  1011. mutex_destroy(&opp_table->lock);
  1012. kfree(opp_table);
  1013. }
  1014. void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
  1015. {
  1016. kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
  1017. &opp_table_lock);
  1018. }
  1019. EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
  1020. void _opp_free(struct dev_pm_opp *opp)
  1021. {
  1022. kfree(opp);
  1023. }
  1024. static void _opp_kref_release(struct dev_pm_opp *opp,
  1025. struct opp_table *opp_table)
  1026. {
  1027. /*
  1028. * Notify the changes in the availability of the operable
  1029. * frequency/voltage list.
  1030. */
  1031. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
  1032. _of_opp_free_required_opps(opp_table, opp);
  1033. opp_debug_remove_one(opp);
  1034. list_del(&opp->node);
  1035. kfree(opp);
  1036. }
  1037. static void _opp_kref_release_unlocked(struct kref *kref)
  1038. {
  1039. struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
  1040. struct opp_table *opp_table = opp->opp_table;
  1041. _opp_kref_release(opp, opp_table);
  1042. }
  1043. static void _opp_kref_release_locked(struct kref *kref)
  1044. {
  1045. struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
  1046. struct opp_table *opp_table = opp->opp_table;
  1047. _opp_kref_release(opp, opp_table);
  1048. mutex_unlock(&opp_table->lock);
  1049. }
  1050. void dev_pm_opp_get(struct dev_pm_opp *opp)
  1051. {
  1052. kref_get(&opp->kref);
  1053. }
  1054. void dev_pm_opp_put(struct dev_pm_opp *opp)
  1055. {
  1056. kref_put_mutex(&opp->kref, _opp_kref_release_locked,
  1057. &opp->opp_table->lock);
  1058. }
  1059. EXPORT_SYMBOL_GPL(dev_pm_opp_put);
  1060. static void dev_pm_opp_put_unlocked(struct dev_pm_opp *opp)
  1061. {
  1062. kref_put(&opp->kref, _opp_kref_release_unlocked);
  1063. }
  1064. /**
  1065. * dev_pm_opp_remove() - Remove an OPP from OPP table
  1066. * @dev: device for which we do this operation
  1067. * @freq: OPP to remove with matching 'freq'
  1068. *
  1069. * This function removes an opp from the opp table.
  1070. */
  1071. void dev_pm_opp_remove(struct device *dev, unsigned long freq)
  1072. {
  1073. struct dev_pm_opp *opp;
  1074. struct opp_table *opp_table;
  1075. bool found = false;
  1076. opp_table = _find_opp_table(dev);
  1077. if (IS_ERR(opp_table))
  1078. return;
  1079. mutex_lock(&opp_table->lock);
  1080. list_for_each_entry(opp, &opp_table->opp_list, node) {
  1081. if (opp->rate == freq) {
  1082. found = true;
  1083. break;
  1084. }
  1085. }
  1086. mutex_unlock(&opp_table->lock);
  1087. if (found) {
  1088. dev_pm_opp_put(opp);
  1089. /* Drop the reference taken by dev_pm_opp_add() */
  1090. dev_pm_opp_put_opp_table(opp_table);
  1091. } else {
  1092. dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
  1093. __func__, freq);
  1094. }
  1095. /* Drop the reference taken by _find_opp_table() */
  1096. dev_pm_opp_put_opp_table(opp_table);
  1097. }
  1098. EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
  1099. bool _opp_remove_all_static(struct opp_table *opp_table)
  1100. {
  1101. struct dev_pm_opp *opp, *tmp;
  1102. bool ret = true;
  1103. mutex_lock(&opp_table->lock);
  1104. if (!opp_table->parsed_static_opps) {
  1105. ret = false;
  1106. goto unlock;
  1107. }
  1108. if (--opp_table->parsed_static_opps)
  1109. goto unlock;
  1110. list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
  1111. if (!opp->dynamic)
  1112. dev_pm_opp_put_unlocked(opp);
  1113. }
  1114. unlock:
  1115. mutex_unlock(&opp_table->lock);
  1116. return ret;
  1117. }
  1118. /**
  1119. * dev_pm_opp_remove_all_dynamic() - Remove all dynamically created OPPs
  1120. * @dev: device for which we do this operation
  1121. *
  1122. * This function removes all dynamically created OPPs from the opp table.
  1123. */
  1124. void dev_pm_opp_remove_all_dynamic(struct device *dev)
  1125. {
  1126. struct opp_table *opp_table;
  1127. struct dev_pm_opp *opp, *temp;
  1128. int count = 0;
  1129. opp_table = _find_opp_table(dev);
  1130. if (IS_ERR(opp_table))
  1131. return;
  1132. mutex_lock(&opp_table->lock);
  1133. list_for_each_entry_safe(opp, temp, &opp_table->opp_list, node) {
  1134. if (opp->dynamic) {
  1135. dev_pm_opp_put_unlocked(opp);
  1136. count++;
  1137. }
  1138. }
  1139. mutex_unlock(&opp_table->lock);
  1140. /* Drop the references taken by dev_pm_opp_add() */
  1141. while (count--)
  1142. dev_pm_opp_put_opp_table(opp_table);
  1143. /* Drop the reference taken by _find_opp_table() */
  1144. dev_pm_opp_put_opp_table(opp_table);
  1145. }
  1146. EXPORT_SYMBOL_GPL(dev_pm_opp_remove_all_dynamic);
  1147. struct dev_pm_opp *_opp_allocate(struct opp_table *table)
  1148. {
  1149. struct dev_pm_opp *opp;
  1150. int supply_count, supply_size, icc_size;
  1151. /* Allocate space for at least one supply */
  1152. supply_count = table->regulator_count > 0 ? table->regulator_count : 1;
  1153. supply_size = sizeof(*opp->supplies) * supply_count;
  1154. icc_size = sizeof(*opp->bandwidth) * table->path_count;
  1155. /* allocate new OPP node and supplies structures */
  1156. opp = kzalloc(sizeof(*opp) + supply_size + icc_size, GFP_KERNEL);
  1157. if (!opp)
  1158. return NULL;
  1159. /* Put the supplies at the end of the OPP structure as an empty array */
  1160. opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
  1161. if (icc_size)
  1162. opp->bandwidth = (struct dev_pm_opp_icc_bw *)(opp->supplies + supply_count);
  1163. INIT_LIST_HEAD(&opp->node);
  1164. return opp;
  1165. }
  1166. static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
  1167. struct opp_table *opp_table)
  1168. {
  1169. struct regulator *reg;
  1170. int i;
  1171. if (!opp_table->regulators)
  1172. return true;
  1173. for (i = 0; i < opp_table->regulator_count; i++) {
  1174. reg = opp_table->regulators[i];
  1175. if (!regulator_is_supported_voltage(reg,
  1176. opp->supplies[i].u_volt_min,
  1177. opp->supplies[i].u_volt_max)) {
  1178. pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
  1179. __func__, opp->supplies[i].u_volt_min,
  1180. opp->supplies[i].u_volt_max);
  1181. return false;
  1182. }
  1183. }
  1184. return true;
  1185. }
  1186. int _opp_compare_key(struct dev_pm_opp *opp1, struct dev_pm_opp *opp2)
  1187. {
  1188. if (opp1->rate != opp2->rate)
  1189. return opp1->rate < opp2->rate ? -1 : 1;
  1190. if (opp1->bandwidth && opp2->bandwidth &&
  1191. opp1->bandwidth[0].peak != opp2->bandwidth[0].peak)
  1192. return opp1->bandwidth[0].peak < opp2->bandwidth[0].peak ? -1 : 1;
  1193. if (opp1->level != opp2->level)
  1194. return opp1->level < opp2->level ? -1 : 1;
  1195. return 0;
  1196. }
  1197. static int _opp_is_duplicate(struct device *dev, struct dev_pm_opp *new_opp,
  1198. struct opp_table *opp_table,
  1199. struct list_head **head)
  1200. {
  1201. struct dev_pm_opp *opp;
  1202. int opp_cmp;
  1203. /*
  1204. * Insert new OPP in order of increasing frequency and discard if
  1205. * already present.
  1206. *
  1207. * Need to use &opp_table->opp_list in the condition part of the 'for'
  1208. * loop, don't replace it with head otherwise it will become an infinite
  1209. * loop.
  1210. */
  1211. list_for_each_entry(opp, &opp_table->opp_list, node) {
  1212. opp_cmp = _opp_compare_key(new_opp, opp);
  1213. if (opp_cmp > 0) {
  1214. *head = &opp->node;
  1215. continue;
  1216. }
  1217. if (opp_cmp < 0)
  1218. return 0;
  1219. /* Duplicate OPPs */
  1220. dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
  1221. __func__, opp->rate, opp->supplies[0].u_volt,
  1222. opp->available, new_opp->rate,
  1223. new_opp->supplies[0].u_volt, new_opp->available);
  1224. /* Should we compare voltages for all regulators here ? */
  1225. return opp->available &&
  1226. new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
  1227. }
  1228. return 0;
  1229. }
  1230. /*
  1231. * Returns:
  1232. * 0: On success. And appropriate error message for duplicate OPPs.
  1233. * -EBUSY: For OPP with same freq/volt and is available. The callers of
  1234. * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
  1235. * sure we don't print error messages unnecessarily if different parts of
  1236. * kernel try to initialize the OPP table.
  1237. * -EEXIST: For OPP with same freq but different volt or is unavailable. This
  1238. * should be considered an error by the callers of _opp_add().
  1239. */
  1240. int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
  1241. struct opp_table *opp_table, bool rate_not_available)
  1242. {
  1243. struct list_head *head;
  1244. int ret;
  1245. mutex_lock(&opp_table->lock);
  1246. head = &opp_table->opp_list;
  1247. if (likely(!rate_not_available)) {
  1248. ret = _opp_is_duplicate(dev, new_opp, opp_table, &head);
  1249. if (ret) {
  1250. mutex_unlock(&opp_table->lock);
  1251. return ret;
  1252. }
  1253. }
  1254. list_add(&new_opp->node, head);
  1255. mutex_unlock(&opp_table->lock);
  1256. new_opp->opp_table = opp_table;
  1257. kref_init(&new_opp->kref);
  1258. opp_debug_create_one(new_opp, opp_table);
  1259. if (!_opp_supported_by_regulators(new_opp, opp_table)) {
  1260. new_opp->available = false;
  1261. dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
  1262. __func__, new_opp->rate);
  1263. }
  1264. return 0;
  1265. }
  1266. /**
  1267. * _opp_add_v1() - Allocate a OPP based on v1 bindings.
  1268. * @opp_table: OPP table
  1269. * @dev: device for which we do this operation
  1270. * @freq: Frequency in Hz for this OPP
  1271. * @u_volt: Voltage in uVolts for this OPP
  1272. * @dynamic: Dynamically added OPPs.
  1273. *
  1274. * This function adds an opp definition to the opp table and returns status.
  1275. * The opp is made available by default and it can be controlled using
  1276. * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
  1277. *
  1278. * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
  1279. * and freed by dev_pm_opp_of_remove_table.
  1280. *
  1281. * Return:
  1282. * 0 On success OR
  1283. * Duplicate OPPs (both freq and volt are same) and opp->available
  1284. * -EEXIST Freq are same and volt are different OR
  1285. * Duplicate OPPs (both freq and volt are same) and !opp->available
  1286. * -ENOMEM Memory allocation failure
  1287. */
  1288. int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
  1289. unsigned long freq, long u_volt, bool dynamic)
  1290. {
  1291. struct dev_pm_opp *new_opp;
  1292. unsigned long tol;
  1293. int ret;
  1294. new_opp = _opp_allocate(opp_table);
  1295. if (!new_opp)
  1296. return -ENOMEM;
  1297. /* populate the opp table */
  1298. new_opp->rate = freq;
  1299. tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
  1300. new_opp->supplies[0].u_volt = u_volt;
  1301. new_opp->supplies[0].u_volt_min = u_volt - tol;
  1302. new_opp->supplies[0].u_volt_max = u_volt + tol;
  1303. new_opp->available = true;
  1304. new_opp->dynamic = dynamic;
  1305. ret = _opp_add(dev, new_opp, opp_table, false);
  1306. if (ret) {
  1307. /* Don't return error for duplicate OPPs */
  1308. if (ret == -EBUSY)
  1309. ret = 0;
  1310. goto free_opp;
  1311. }
  1312. /*
  1313. * Notify the changes in the availability of the operable
  1314. * frequency/voltage list.
  1315. */
  1316. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
  1317. return 0;
  1318. free_opp:
  1319. _opp_free(new_opp);
  1320. return ret;
  1321. }
  1322. /**
  1323. * dev_pm_opp_set_supported_hw() - Set supported platforms
  1324. * @dev: Device for which supported-hw has to be set.
  1325. * @versions: Array of hierarchy of versions to match.
  1326. * @count: Number of elements in the array.
  1327. *
  1328. * This is required only for the V2 bindings, and it enables a platform to
  1329. * specify the hierarchy of versions it supports. OPP layer will then enable
  1330. * OPPs, which are available for those versions, based on its 'opp-supported-hw'
  1331. * property.
  1332. */
  1333. struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
  1334. const u32 *versions, unsigned int count)
  1335. {
  1336. struct opp_table *opp_table;
  1337. opp_table = dev_pm_opp_get_opp_table(dev);
  1338. if (IS_ERR(opp_table))
  1339. return opp_table;
  1340. /* Make sure there are no concurrent readers while updating opp_table */
  1341. WARN_ON(!list_empty(&opp_table->opp_list));
  1342. /* Another CPU that shares the OPP table has set the property ? */
  1343. if (opp_table->supported_hw)
  1344. return opp_table;
  1345. opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
  1346. GFP_KERNEL);
  1347. if (!opp_table->supported_hw) {
  1348. dev_pm_opp_put_opp_table(opp_table);
  1349. return ERR_PTR(-ENOMEM);
  1350. }
  1351. opp_table->supported_hw_count = count;
  1352. return opp_table;
  1353. }
  1354. EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
  1355. /**
  1356. * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
  1357. * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
  1358. *
  1359. * This is required only for the V2 bindings, and is called for a matching
  1360. * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
  1361. * will not be freed.
  1362. */
  1363. void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
  1364. {
  1365. /* Make sure there are no concurrent readers while updating opp_table */
  1366. WARN_ON(!list_empty(&opp_table->opp_list));
  1367. kfree(opp_table->supported_hw);
  1368. opp_table->supported_hw = NULL;
  1369. opp_table->supported_hw_count = 0;
  1370. dev_pm_opp_put_opp_table(opp_table);
  1371. }
  1372. EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
  1373. /**
  1374. * dev_pm_opp_set_prop_name() - Set prop-extn name
  1375. * @dev: Device for which the prop-name has to be set.
  1376. * @name: name to postfix to properties.
  1377. *
  1378. * This is required only for the V2 bindings, and it enables a platform to
  1379. * specify the extn to be used for certain property names. The properties to
  1380. * which the extension will apply are opp-microvolt and opp-microamp. OPP core
  1381. * should postfix the property name with -<name> while looking for them.
  1382. */
  1383. struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
  1384. {
  1385. struct opp_table *opp_table;
  1386. opp_table = dev_pm_opp_get_opp_table(dev);
  1387. if (IS_ERR(opp_table))
  1388. return opp_table;
  1389. /* Make sure there are no concurrent readers while updating opp_table */
  1390. WARN_ON(!list_empty(&opp_table->opp_list));
  1391. /* Another CPU that shares the OPP table has set the property ? */
  1392. if (opp_table->prop_name)
  1393. return opp_table;
  1394. opp_table->prop_name = kstrdup(name, GFP_KERNEL);
  1395. if (!opp_table->prop_name) {
  1396. dev_pm_opp_put_opp_table(opp_table);
  1397. return ERR_PTR(-ENOMEM);
  1398. }
  1399. return opp_table;
  1400. }
  1401. EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
  1402. /**
  1403. * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
  1404. * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
  1405. *
  1406. * This is required only for the V2 bindings, and is called for a matching
  1407. * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
  1408. * will not be freed.
  1409. */
  1410. void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
  1411. {
  1412. /* Make sure there are no concurrent readers while updating opp_table */
  1413. WARN_ON(!list_empty(&opp_table->opp_list));
  1414. kfree(opp_table->prop_name);
  1415. opp_table->prop_name = NULL;
  1416. dev_pm_opp_put_opp_table(opp_table);
  1417. }
  1418. EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
  1419. static int _allocate_set_opp_data(struct opp_table *opp_table)
  1420. {
  1421. struct dev_pm_set_opp_data *data;
  1422. int len, count = opp_table->regulator_count;
  1423. if (WARN_ON(!opp_table->regulators))
  1424. return -EINVAL;
  1425. /* space for set_opp_data */
  1426. len = sizeof(*data);
  1427. /* space for old_opp.supplies and new_opp.supplies */
  1428. len += 2 * sizeof(struct dev_pm_opp_supply) * count;
  1429. data = kzalloc(len, GFP_KERNEL);
  1430. if (!data)
  1431. return -ENOMEM;
  1432. data->old_opp.supplies = (void *)(data + 1);
  1433. data->new_opp.supplies = data->old_opp.supplies + count;
  1434. opp_table->set_opp_data = data;
  1435. return 0;
  1436. }
  1437. static void _free_set_opp_data(struct opp_table *opp_table)
  1438. {
  1439. kfree(opp_table->set_opp_data);
  1440. opp_table->set_opp_data = NULL;
  1441. }
  1442. /**
  1443. * dev_pm_opp_set_regulators() - Set regulator names for the device
  1444. * @dev: Device for which regulator name is being set.
  1445. * @names: Array of pointers to the names of the regulator.
  1446. * @count: Number of regulators.
  1447. *
  1448. * In order to support OPP switching, OPP layer needs to know the name of the
  1449. * device's regulators, as the core would be required to switch voltages as
  1450. * well.
  1451. *
  1452. * This must be called before any OPPs are initialized for the device.
  1453. */
  1454. struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
  1455. const char * const names[],
  1456. unsigned int count)
  1457. {
  1458. struct opp_table *opp_table;
  1459. struct regulator *reg;
  1460. int ret, i;
  1461. opp_table = dev_pm_opp_get_opp_table(dev);
  1462. if (IS_ERR(opp_table))
  1463. return opp_table;
  1464. /* This should be called before OPPs are initialized */
  1465. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  1466. ret = -EBUSY;
  1467. goto err;
  1468. }
  1469. /* Another CPU that shares the OPP table has set the regulators ? */
  1470. if (opp_table->regulators)
  1471. return opp_table;
  1472. opp_table->regulators = kmalloc_array(count,
  1473. sizeof(*opp_table->regulators),
  1474. GFP_KERNEL);
  1475. if (!opp_table->regulators) {
  1476. ret = -ENOMEM;
  1477. goto err;
  1478. }
  1479. for (i = 0; i < count; i++) {
  1480. reg = regulator_get_optional(dev, names[i]);
  1481. if (IS_ERR(reg)) {
  1482. ret = PTR_ERR(reg);
  1483. if (ret != -EPROBE_DEFER)
  1484. dev_err(dev, "%s: no regulator (%s) found: %d\n",
  1485. __func__, names[i], ret);
  1486. goto free_regulators;
  1487. }
  1488. opp_table->regulators[i] = reg;
  1489. }
  1490. opp_table->regulator_count = count;
  1491. /* Allocate block only once to pass to set_opp() routines */
  1492. ret = _allocate_set_opp_data(opp_table);
  1493. if (ret)
  1494. goto free_regulators;
  1495. return opp_table;
  1496. free_regulators:
  1497. while (i != 0)
  1498. regulator_put(opp_table->regulators[--i]);
  1499. kfree(opp_table->regulators);
  1500. opp_table->regulators = NULL;
  1501. opp_table->regulator_count = -1;
  1502. err:
  1503. dev_pm_opp_put_opp_table(opp_table);
  1504. return ERR_PTR(ret);
  1505. }
  1506. EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
  1507. /**
  1508. * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
  1509. * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
  1510. */
  1511. void dev_pm_opp_put_regulators(struct opp_table *opp_table)
  1512. {
  1513. int i;
  1514. if (!opp_table->regulators)
  1515. goto put_opp_table;
  1516. /* Make sure there are no concurrent readers while updating opp_table */
  1517. WARN_ON(!list_empty(&opp_table->opp_list));
  1518. if (opp_table->enabled) {
  1519. for (i = opp_table->regulator_count - 1; i >= 0; i--)
  1520. regulator_disable(opp_table->regulators[i]);
  1521. }
  1522. for (i = opp_table->regulator_count - 1; i >= 0; i--)
  1523. regulator_put(opp_table->regulators[i]);
  1524. _free_set_opp_data(opp_table);
  1525. kfree(opp_table->regulators);
  1526. opp_table->regulators = NULL;
  1527. opp_table->regulator_count = -1;
  1528. put_opp_table:
  1529. dev_pm_opp_put_opp_table(opp_table);
  1530. }
  1531. EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
  1532. /**
  1533. * dev_pm_opp_set_clkname() - Set clk name for the device
  1534. * @dev: Device for which clk name is being set.
  1535. * @name: Clk name.
  1536. *
  1537. * In order to support OPP switching, OPP layer needs to get pointer to the
  1538. * clock for the device. Simple cases work fine without using this routine (i.e.
  1539. * by passing connection-id as NULL), but for a device with multiple clocks
  1540. * available, the OPP core needs to know the exact name of the clk to use.
  1541. *
  1542. * This must be called before any OPPs are initialized for the device.
  1543. */
  1544. struct opp_table *dev_pm_opp_set_clkname(struct device *dev, const char *name)
  1545. {
  1546. struct opp_table *opp_table;
  1547. int ret;
  1548. opp_table = dev_pm_opp_get_opp_table(dev);
  1549. if (IS_ERR(opp_table))
  1550. return opp_table;
  1551. /* This should be called before OPPs are initialized */
  1552. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  1553. ret = -EBUSY;
  1554. goto err;
  1555. }
  1556. /* Already have default clk set, free it */
  1557. if (!IS_ERR(opp_table->clk))
  1558. clk_put(opp_table->clk);
  1559. /* Find clk for the device */
  1560. opp_table->clk = clk_get(dev, name);
  1561. if (IS_ERR(opp_table->clk)) {
  1562. ret = PTR_ERR(opp_table->clk);
  1563. if (ret != -EPROBE_DEFER) {
  1564. dev_err(dev, "%s: Couldn't find clock: %d\n", __func__,
  1565. ret);
  1566. }
  1567. goto err;
  1568. }
  1569. return opp_table;
  1570. err:
  1571. dev_pm_opp_put_opp_table(opp_table);
  1572. return ERR_PTR(ret);
  1573. }
  1574. EXPORT_SYMBOL_GPL(dev_pm_opp_set_clkname);
  1575. /**
  1576. * dev_pm_opp_put_clkname() - Releases resources blocked for clk.
  1577. * @opp_table: OPP table returned from dev_pm_opp_set_clkname().
  1578. */
  1579. void dev_pm_opp_put_clkname(struct opp_table *opp_table)
  1580. {
  1581. /* Make sure there are no concurrent readers while updating opp_table */
  1582. WARN_ON(!list_empty(&opp_table->opp_list));
  1583. clk_put(opp_table->clk);
  1584. opp_table->clk = ERR_PTR(-EINVAL);
  1585. dev_pm_opp_put_opp_table(opp_table);
  1586. }
  1587. EXPORT_SYMBOL_GPL(dev_pm_opp_put_clkname);
  1588. /**
  1589. * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
  1590. * @dev: Device for which the helper is getting registered.
  1591. * @set_opp: Custom set OPP helper.
  1592. *
  1593. * This is useful to support complex platforms (like platforms with multiple
  1594. * regulators per device), instead of the generic OPP set rate helper.
  1595. *
  1596. * This must be called before any OPPs are initialized for the device.
  1597. */
  1598. struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
  1599. int (*set_opp)(struct dev_pm_set_opp_data *data))
  1600. {
  1601. struct opp_table *opp_table;
  1602. if (!set_opp)
  1603. return ERR_PTR(-EINVAL);
  1604. opp_table = dev_pm_opp_get_opp_table(dev);
  1605. if (IS_ERR(opp_table))
  1606. return opp_table;
  1607. /* This should be called before OPPs are initialized */
  1608. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  1609. dev_pm_opp_put_opp_table(opp_table);
  1610. return ERR_PTR(-EBUSY);
  1611. }
  1612. /* Another CPU that shares the OPP table has set the helper ? */
  1613. if (!opp_table->set_opp)
  1614. opp_table->set_opp = set_opp;
  1615. return opp_table;
  1616. }
  1617. EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
  1618. /**
  1619. * dev_pm_opp_unregister_set_opp_helper() - Releases resources blocked for
  1620. * set_opp helper
  1621. * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
  1622. *
  1623. * Release resources blocked for platform specific set_opp helper.
  1624. */
  1625. void dev_pm_opp_unregister_set_opp_helper(struct opp_table *opp_table)
  1626. {
  1627. /* Make sure there are no concurrent readers while updating opp_table */
  1628. WARN_ON(!list_empty(&opp_table->opp_list));
  1629. opp_table->set_opp = NULL;
  1630. dev_pm_opp_put_opp_table(opp_table);
  1631. }
  1632. EXPORT_SYMBOL_GPL(dev_pm_opp_unregister_set_opp_helper);
  1633. static void _opp_detach_genpd(struct opp_table *opp_table)
  1634. {
  1635. int index;
  1636. if (!opp_table->genpd_virt_devs)
  1637. return;
  1638. for (index = 0; index < opp_table->required_opp_count; index++) {
  1639. if (!opp_table->genpd_virt_devs[index])
  1640. continue;
  1641. dev_pm_domain_detach(opp_table->genpd_virt_devs[index], false);
  1642. opp_table->genpd_virt_devs[index] = NULL;
  1643. }
  1644. kfree(opp_table->genpd_virt_devs);
  1645. opp_table->genpd_virt_devs = NULL;
  1646. }
  1647. /**
  1648. * dev_pm_opp_attach_genpd - Attach genpd(s) for the device and save virtual device pointer
  1649. * @dev: Consumer device for which the genpd is getting attached.
  1650. * @names: Null terminated array of pointers containing names of genpd to attach.
  1651. * @virt_devs: Pointer to return the array of virtual devices.
  1652. *
  1653. * Multiple generic power domains for a device are supported with the help of
  1654. * virtual genpd devices, which are created for each consumer device - genpd
  1655. * pair. These are the device structures which are attached to the power domain
  1656. * and are required by the OPP core to set the performance state of the genpd.
  1657. * The same API also works for the case where single genpd is available and so
  1658. * we don't need to support that separately.
  1659. *
  1660. * This helper will normally be called by the consumer driver of the device
  1661. * "dev", as only that has details of the genpd names.
  1662. *
  1663. * This helper needs to be called once with a list of all genpd to attach.
  1664. * Otherwise the original device structure will be used instead by the OPP core.
  1665. *
  1666. * The order of entries in the names array must match the order in which
  1667. * "required-opps" are added in DT.
  1668. */
  1669. struct opp_table *dev_pm_opp_attach_genpd(struct device *dev,
  1670. const char **names, struct device ***virt_devs)
  1671. {
  1672. struct opp_table *opp_table;
  1673. struct device *virt_dev;
  1674. int index = 0, ret = -EINVAL;
  1675. const char **name = names;
  1676. opp_table = dev_pm_opp_get_opp_table(dev);
  1677. if (IS_ERR(opp_table))
  1678. return opp_table;
  1679. if (opp_table->genpd_virt_devs)
  1680. return opp_table;
  1681. /*
  1682. * If the genpd's OPP table isn't already initialized, parsing of the
  1683. * required-opps fail for dev. We should retry this after genpd's OPP
  1684. * table is added.
  1685. */
  1686. if (!opp_table->required_opp_count) {
  1687. ret = -EPROBE_DEFER;
  1688. goto put_table;
  1689. }
  1690. mutex_lock(&opp_table->genpd_virt_dev_lock);
  1691. opp_table->genpd_virt_devs = kcalloc(opp_table->required_opp_count,
  1692. sizeof(*opp_table->genpd_virt_devs),
  1693. GFP_KERNEL);
  1694. if (!opp_table->genpd_virt_devs)
  1695. goto unlock;
  1696. while (*name) {
  1697. if (index >= opp_table->required_opp_count) {
  1698. dev_err(dev, "Index can't be greater than required-opp-count - 1, %s (%d : %d)\n",
  1699. *name, opp_table->required_opp_count, index);
  1700. goto err;
  1701. }
  1702. virt_dev = dev_pm_domain_attach_by_name(dev, *name);
  1703. if (IS_ERR(virt_dev)) {
  1704. ret = PTR_ERR(virt_dev);
  1705. dev_err(dev, "Couldn't attach to pm_domain: %d\n", ret);
  1706. goto err;
  1707. }
  1708. opp_table->genpd_virt_devs[index] = virt_dev;
  1709. index++;
  1710. name++;
  1711. }
  1712. if (virt_devs)
  1713. *virt_devs = opp_table->genpd_virt_devs;
  1714. mutex_unlock(&opp_table->genpd_virt_dev_lock);
  1715. return opp_table;
  1716. err:
  1717. _opp_detach_genpd(opp_table);
  1718. unlock:
  1719. mutex_unlock(&opp_table->genpd_virt_dev_lock);
  1720. put_table:
  1721. dev_pm_opp_put_opp_table(opp_table);
  1722. return ERR_PTR(ret);
  1723. }
  1724. EXPORT_SYMBOL_GPL(dev_pm_opp_attach_genpd);
  1725. /**
  1726. * dev_pm_opp_detach_genpd() - Detach genpd(s) from the device.
  1727. * @opp_table: OPP table returned by dev_pm_opp_attach_genpd().
  1728. *
  1729. * This detaches the genpd(s), resets the virtual device pointers, and puts the
  1730. * OPP table.
  1731. */
  1732. void dev_pm_opp_detach_genpd(struct opp_table *opp_table)
  1733. {
  1734. /*
  1735. * Acquire genpd_virt_dev_lock to make sure virt_dev isn't getting
  1736. * used in parallel.
  1737. */
  1738. mutex_lock(&opp_table->genpd_virt_dev_lock);
  1739. _opp_detach_genpd(opp_table);
  1740. mutex_unlock(&opp_table->genpd_virt_dev_lock);
  1741. dev_pm_opp_put_opp_table(opp_table);
  1742. }
  1743. EXPORT_SYMBOL_GPL(dev_pm_opp_detach_genpd);
  1744. /**
  1745. * dev_pm_opp_xlate_performance_state() - Find required OPP's pstate for src_table.
  1746. * @src_table: OPP table which has dst_table as one of its required OPP table.
  1747. * @dst_table: Required OPP table of the src_table.
  1748. * @pstate: Current performance state of the src_table.
  1749. *
  1750. * This Returns pstate of the OPP (present in @dst_table) pointed out by the
  1751. * "required-opps" property of the OPP (present in @src_table) which has
  1752. * performance state set to @pstate.
  1753. *
  1754. * Return: Zero or positive performance state on success, otherwise negative
  1755. * value on errors.
  1756. */
  1757. int dev_pm_opp_xlate_performance_state(struct opp_table *src_table,
  1758. struct opp_table *dst_table,
  1759. unsigned int pstate)
  1760. {
  1761. struct dev_pm_opp *opp;
  1762. int dest_pstate = -EINVAL;
  1763. int i;
  1764. /*
  1765. * Normally the src_table will have the "required_opps" property set to
  1766. * point to one of the OPPs in the dst_table, but in some cases the
  1767. * genpd and its master have one to one mapping of performance states
  1768. * and so none of them have the "required-opps" property set. Return the
  1769. * pstate of the src_table as it is in such cases.
  1770. */
  1771. if (!src_table->required_opp_count)
  1772. return pstate;
  1773. for (i = 0; i < src_table->required_opp_count; i++) {
  1774. if (src_table->required_opp_tables[i]->np == dst_table->np)
  1775. break;
  1776. }
  1777. if (unlikely(i == src_table->required_opp_count)) {
  1778. pr_err("%s: Couldn't find matching OPP table (%p: %p)\n",
  1779. __func__, src_table, dst_table);
  1780. return -EINVAL;
  1781. }
  1782. mutex_lock(&src_table->lock);
  1783. list_for_each_entry(opp, &src_table->opp_list, node) {
  1784. if (opp->pstate == pstate) {
  1785. dest_pstate = opp->required_opps[i]->pstate;
  1786. goto unlock;
  1787. }
  1788. }
  1789. pr_err("%s: Couldn't find matching OPP (%p: %p)\n", __func__, src_table,
  1790. dst_table);
  1791. unlock:
  1792. mutex_unlock(&src_table->lock);
  1793. return dest_pstate;
  1794. }
  1795. /**
  1796. * dev_pm_opp_add() - Add an OPP table from a table definitions
  1797. * @dev: device for which we do this operation
  1798. * @freq: Frequency in Hz for this OPP
  1799. * @u_volt: Voltage in uVolts for this OPP
  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.
  1804. *
  1805. * Return:
  1806. * 0 On success OR
  1807. * Duplicate OPPs (both freq and volt are same) and opp->available
  1808. * -EEXIST Freq are same and volt are different OR
  1809. * Duplicate OPPs (both freq and volt are same) and !opp->available
  1810. * -ENOMEM Memory allocation failure
  1811. */
  1812. int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
  1813. {
  1814. struct opp_table *opp_table;
  1815. int ret;
  1816. opp_table = dev_pm_opp_get_opp_table(dev);
  1817. if (IS_ERR(opp_table))
  1818. return PTR_ERR(opp_table);
  1819. /* Fix regulator count for dynamic OPPs */
  1820. opp_table->regulator_count = 1;
  1821. ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
  1822. if (ret)
  1823. dev_pm_opp_put_opp_table(opp_table);
  1824. return ret;
  1825. }
  1826. EXPORT_SYMBOL_GPL(dev_pm_opp_add);
  1827. /**
  1828. * _opp_set_availability() - helper to set the availability of an opp
  1829. * @dev: device for which we do this operation
  1830. * @freq: OPP frequency to modify availability
  1831. * @availability_req: availability status requested for this opp
  1832. *
  1833. * Set the availability of an OPP, opp_{enable,disable} share a common logic
  1834. * which is isolated here.
  1835. *
  1836. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1837. * copy operation, returns 0 if no modification was done OR modification was
  1838. * successful.
  1839. */
  1840. static int _opp_set_availability(struct device *dev, unsigned long freq,
  1841. bool availability_req)
  1842. {
  1843. struct opp_table *opp_table;
  1844. struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
  1845. int r = 0;
  1846. /* Find the opp_table */
  1847. opp_table = _find_opp_table(dev);
  1848. if (IS_ERR(opp_table)) {
  1849. r = PTR_ERR(opp_table);
  1850. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  1851. return r;
  1852. }
  1853. mutex_lock(&opp_table->lock);
  1854. /* Do we have the frequency? */
  1855. list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
  1856. if (tmp_opp->rate == freq) {
  1857. opp = tmp_opp;
  1858. break;
  1859. }
  1860. }
  1861. if (IS_ERR(opp)) {
  1862. r = PTR_ERR(opp);
  1863. goto unlock;
  1864. }
  1865. /* Is update really needed? */
  1866. if (opp->available == availability_req)
  1867. goto unlock;
  1868. opp->available = availability_req;
  1869. dev_pm_opp_get(opp);
  1870. mutex_unlock(&opp_table->lock);
  1871. /* Notify the change of the OPP availability */
  1872. if (availability_req)
  1873. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
  1874. opp);
  1875. else
  1876. blocking_notifier_call_chain(&opp_table->head,
  1877. OPP_EVENT_DISABLE, opp);
  1878. dev_pm_opp_put(opp);
  1879. goto put_table;
  1880. unlock:
  1881. mutex_unlock(&opp_table->lock);
  1882. put_table:
  1883. dev_pm_opp_put_opp_table(opp_table);
  1884. return r;
  1885. }
  1886. /**
  1887. * dev_pm_opp_adjust_voltage() - helper to change the voltage of an OPP
  1888. * @dev: device for which we do this operation
  1889. * @freq: OPP frequency to adjust voltage of
  1890. * @u_volt: new OPP target voltage
  1891. * @u_volt_min: new OPP min voltage
  1892. * @u_volt_max: new OPP max voltage
  1893. *
  1894. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1895. * copy operation, returns 0 if no modifcation was done OR modification was
  1896. * successful.
  1897. */
  1898. int dev_pm_opp_adjust_voltage(struct device *dev, unsigned long freq,
  1899. unsigned long u_volt, unsigned long u_volt_min,
  1900. unsigned long u_volt_max)
  1901. {
  1902. struct opp_table *opp_table;
  1903. struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
  1904. int r = 0;
  1905. /* Find the opp_table */
  1906. opp_table = _find_opp_table(dev);
  1907. if (IS_ERR(opp_table)) {
  1908. r = PTR_ERR(opp_table);
  1909. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  1910. return r;
  1911. }
  1912. mutex_lock(&opp_table->lock);
  1913. /* Do we have the frequency? */
  1914. list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
  1915. if (tmp_opp->rate == freq) {
  1916. opp = tmp_opp;
  1917. break;
  1918. }
  1919. }
  1920. if (IS_ERR(opp)) {
  1921. r = PTR_ERR(opp);
  1922. goto adjust_unlock;
  1923. }
  1924. /* Is update really needed? */
  1925. if (opp->supplies->u_volt == u_volt)
  1926. goto adjust_unlock;
  1927. opp->supplies->u_volt = u_volt;
  1928. opp->supplies->u_volt_min = u_volt_min;
  1929. opp->supplies->u_volt_max = u_volt_max;
  1930. dev_pm_opp_get(opp);
  1931. mutex_unlock(&opp_table->lock);
  1932. /* Notify the voltage change of the OPP */
  1933. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADJUST_VOLTAGE,
  1934. opp);
  1935. dev_pm_opp_put(opp);
  1936. goto adjust_put_table;
  1937. adjust_unlock:
  1938. mutex_unlock(&opp_table->lock);
  1939. adjust_put_table:
  1940. dev_pm_opp_put_opp_table(opp_table);
  1941. return r;
  1942. }
  1943. EXPORT_SYMBOL_GPL(dev_pm_opp_adjust_voltage);
  1944. /**
  1945. * dev_pm_opp_enable() - Enable a specific OPP
  1946. * @dev: device for which we do this operation
  1947. * @freq: OPP frequency to enable
  1948. *
  1949. * Enables a provided opp. If the operation is valid, this returns 0, else the
  1950. * corresponding error value. It is meant to be used for users an OPP available
  1951. * after being temporarily made unavailable with dev_pm_opp_disable.
  1952. *
  1953. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1954. * copy operation, returns 0 if no modification was done OR modification was
  1955. * successful.
  1956. */
  1957. int dev_pm_opp_enable(struct device *dev, unsigned long freq)
  1958. {
  1959. return _opp_set_availability(dev, freq, true);
  1960. }
  1961. EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
  1962. /**
  1963. * dev_pm_opp_disable() - Disable a specific OPP
  1964. * @dev: device for which we do this operation
  1965. * @freq: OPP frequency to disable
  1966. *
  1967. * Disables a provided opp. If the operation is valid, this returns
  1968. * 0, else the corresponding error value. It is meant to be a temporary
  1969. * control by users to make this OPP not available until the circumstances are
  1970. * right to make it available again (with a call to dev_pm_opp_enable).
  1971. *
  1972. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1973. * copy operation, returns 0 if no modification was done OR modification was
  1974. * successful.
  1975. */
  1976. int dev_pm_opp_disable(struct device *dev, unsigned long freq)
  1977. {
  1978. return _opp_set_availability(dev, freq, false);
  1979. }
  1980. EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
  1981. /**
  1982. * dev_pm_opp_register_notifier() - Register OPP notifier for the device
  1983. * @dev: Device for which notifier needs to be registered
  1984. * @nb: Notifier block to be registered
  1985. *
  1986. * Return: 0 on success or a negative error value.
  1987. */
  1988. int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
  1989. {
  1990. struct opp_table *opp_table;
  1991. int ret;
  1992. opp_table = _find_opp_table(dev);
  1993. if (IS_ERR(opp_table))
  1994. return PTR_ERR(opp_table);
  1995. ret = blocking_notifier_chain_register(&opp_table->head, nb);
  1996. dev_pm_opp_put_opp_table(opp_table);
  1997. return ret;
  1998. }
  1999. EXPORT_SYMBOL(dev_pm_opp_register_notifier);
  2000. /**
  2001. * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
  2002. * @dev: Device for which notifier needs to be unregistered
  2003. * @nb: Notifier block to be unregistered
  2004. *
  2005. * Return: 0 on success or a negative error value.
  2006. */
  2007. int dev_pm_opp_unregister_notifier(struct device *dev,
  2008. struct notifier_block *nb)
  2009. {
  2010. struct opp_table *opp_table;
  2011. int ret;
  2012. opp_table = _find_opp_table(dev);
  2013. if (IS_ERR(opp_table))
  2014. return PTR_ERR(opp_table);
  2015. ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
  2016. dev_pm_opp_put_opp_table(opp_table);
  2017. return ret;
  2018. }
  2019. EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
  2020. /**
  2021. * dev_pm_opp_remove_table() - Free all OPPs associated with the device
  2022. * @dev: device pointer used to lookup OPP table.
  2023. *
  2024. * Free both OPPs created using static entries present in DT and the
  2025. * dynamically added entries.
  2026. */
  2027. void dev_pm_opp_remove_table(struct device *dev)
  2028. {
  2029. struct opp_table *opp_table;
  2030. /* Check for existing table for 'dev' */
  2031. opp_table = _find_opp_table(dev);
  2032. if (IS_ERR(opp_table)) {
  2033. int error = PTR_ERR(opp_table);
  2034. if (error != -ENODEV)
  2035. WARN(1, "%s: opp_table: %d\n",
  2036. IS_ERR_OR_NULL(dev) ?
  2037. "Invalid device" : dev_name(dev),
  2038. error);
  2039. return;
  2040. }
  2041. /*
  2042. * Drop the extra reference only if the OPP table was successfully added
  2043. * with dev_pm_opp_of_add_table() earlier.
  2044. **/
  2045. if (_opp_remove_all_static(opp_table))
  2046. dev_pm_opp_put_opp_table(opp_table);
  2047. /* Drop reference taken by _find_opp_table() */
  2048. dev_pm_opp_put_opp_table(opp_table);
  2049. }
  2050. EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);