core.c 46 KB

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