core.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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/regulator/consumer.h>
  21. #include "opp.h"
  22. /*
  23. * The root of the list of all opp-tables. All opp_table structures branch off
  24. * from here, with each opp_table containing the list of opps it supports in
  25. * various states of availability.
  26. */
  27. LIST_HEAD(opp_tables);
  28. /* Lock to allow exclusive modification to the device and opp lists */
  29. DEFINE_MUTEX(opp_table_lock);
  30. static void dev_pm_opp_get(struct dev_pm_opp *opp);
  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, **regulators;
  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. regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL);
  170. if (!regulators)
  171. goto put_opp_table;
  172. uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL);
  173. if (!uV)
  174. goto free_regulators;
  175. memcpy(regulators, opp_table->regulators, count * sizeof(*regulators));
  176. mutex_lock(&opp_table->lock);
  177. for (i = 0; i < count; i++) {
  178. uV[i].min = ~0;
  179. uV[i].max = 0;
  180. list_for_each_entry(opp, &opp_table->opp_list, node) {
  181. if (!opp->available)
  182. continue;
  183. if (opp->supplies[i].u_volt_min < uV[i].min)
  184. uV[i].min = opp->supplies[i].u_volt_min;
  185. if (opp->supplies[i].u_volt_max > uV[i].max)
  186. uV[i].max = opp->supplies[i].u_volt_max;
  187. }
  188. }
  189. mutex_unlock(&opp_table->lock);
  190. /*
  191. * The caller needs to ensure that opp_table (and hence the regulator)
  192. * isn't freed, while we are executing this routine.
  193. */
  194. for (i = 0; i < count; i++) {
  195. reg = regulators[i];
  196. ret = regulator_set_voltage_time(reg, uV[i].min, uV[i].max);
  197. if (ret > 0)
  198. latency_ns += ret * 1000;
  199. }
  200. kfree(uV);
  201. free_regulators:
  202. kfree(regulators);
  203. put_opp_table:
  204. dev_pm_opp_put_opp_table(opp_table);
  205. return latency_ns;
  206. }
  207. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_volt_latency);
  208. /**
  209. * dev_pm_opp_get_max_transition_latency() - Get max transition latency in
  210. * nanoseconds
  211. * @dev: device for which we do this operation
  212. *
  213. * Return: This function returns the max transition latency, in nanoseconds, to
  214. * switch from one OPP to other.
  215. */
  216. unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev)
  217. {
  218. return dev_pm_opp_get_max_volt_latency(dev) +
  219. dev_pm_opp_get_max_clock_latency(dev);
  220. }
  221. EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency);
  222. /**
  223. * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz
  224. * @dev: device for which we do this operation
  225. *
  226. * Return: This function returns the frequency of the OPP marked as suspend_opp
  227. * if one is available, else returns 0;
  228. */
  229. unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev)
  230. {
  231. struct opp_table *opp_table;
  232. unsigned long freq = 0;
  233. opp_table = _find_opp_table(dev);
  234. if (IS_ERR(opp_table))
  235. return 0;
  236. if (opp_table->suspend_opp && opp_table->suspend_opp->available)
  237. freq = dev_pm_opp_get_freq(opp_table->suspend_opp);
  238. dev_pm_opp_put_opp_table(opp_table);
  239. return freq;
  240. }
  241. EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq);
  242. /**
  243. * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table
  244. * @dev: device for which we do this operation
  245. *
  246. * Return: This function returns the number of available opps if there are any,
  247. * else returns 0 if none or the corresponding error value.
  248. */
  249. int dev_pm_opp_get_opp_count(struct device *dev)
  250. {
  251. struct opp_table *opp_table;
  252. struct dev_pm_opp *temp_opp;
  253. int count = 0;
  254. opp_table = _find_opp_table(dev);
  255. if (IS_ERR(opp_table)) {
  256. count = PTR_ERR(opp_table);
  257. dev_err(dev, "%s: OPP table not found (%d)\n",
  258. __func__, count);
  259. return count;
  260. }
  261. mutex_lock(&opp_table->lock);
  262. list_for_each_entry(temp_opp, &opp_table->opp_list, node) {
  263. if (temp_opp->available)
  264. count++;
  265. }
  266. mutex_unlock(&opp_table->lock);
  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 int _generic_set_opp(struct dev_pm_set_opp_data *data)
  456. {
  457. struct dev_pm_opp_supply *old_supply = data->old_opp.supplies;
  458. struct dev_pm_opp_supply *new_supply = data->new_opp.supplies;
  459. unsigned long old_freq = data->old_opp.rate, freq = data->new_opp.rate;
  460. struct regulator *reg = data->regulators[0];
  461. struct device *dev= data->dev;
  462. int ret;
  463. /* This function only supports single regulator per device */
  464. if (WARN_ON(data->regulator_count > 1)) {
  465. dev_err(dev, "multiple regulators are not supported\n");
  466. return -EINVAL;
  467. }
  468. /* Scaling up? Scale voltage before frequency */
  469. if (freq > old_freq) {
  470. ret = _set_opp_voltage(dev, reg, new_supply);
  471. if (ret)
  472. goto restore_voltage;
  473. }
  474. /* Change frequency */
  475. ret = _generic_set_opp_clk_only(dev, data->clk, old_freq, freq);
  476. if (ret)
  477. goto restore_voltage;
  478. /* Scaling down? Scale voltage after frequency */
  479. if (freq < old_freq) {
  480. ret = _set_opp_voltage(dev, reg, new_supply);
  481. if (ret)
  482. goto restore_freq;
  483. }
  484. return 0;
  485. restore_freq:
  486. if (_generic_set_opp_clk_only(dev, data->clk, freq, old_freq))
  487. dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n",
  488. __func__, old_freq);
  489. restore_voltage:
  490. /* This shouldn't harm even if the voltages weren't updated earlier */
  491. if (old_supply->u_volt)
  492. _set_opp_voltage(dev, reg, old_supply);
  493. return ret;
  494. }
  495. /**
  496. * dev_pm_opp_set_rate() - Configure new OPP based on frequency
  497. * @dev: device for which we do this operation
  498. * @target_freq: frequency to achieve
  499. *
  500. * This configures the power-supplies and clock source to the levels specified
  501. * by the OPP corresponding to the target_freq.
  502. */
  503. int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq)
  504. {
  505. struct opp_table *opp_table;
  506. unsigned long freq, old_freq;
  507. int (*set_opp)(struct dev_pm_set_opp_data *data);
  508. struct dev_pm_opp *old_opp, *opp;
  509. struct regulator **regulators;
  510. struct dev_pm_set_opp_data *data;
  511. struct clk *clk;
  512. int ret, size;
  513. if (unlikely(!target_freq)) {
  514. dev_err(dev, "%s: Invalid target frequency %lu\n", __func__,
  515. target_freq);
  516. return -EINVAL;
  517. }
  518. opp_table = _find_opp_table(dev);
  519. if (IS_ERR(opp_table)) {
  520. dev_err(dev, "%s: device opp doesn't exist\n", __func__);
  521. return PTR_ERR(opp_table);
  522. }
  523. clk = opp_table->clk;
  524. if (IS_ERR(clk)) {
  525. dev_err(dev, "%s: No clock available for the device\n",
  526. __func__);
  527. ret = PTR_ERR(clk);
  528. goto put_opp_table;
  529. }
  530. freq = clk_round_rate(clk, target_freq);
  531. if ((long)freq <= 0)
  532. freq = target_freq;
  533. old_freq = clk_get_rate(clk);
  534. /* Return early if nothing to do */
  535. if (old_freq == freq) {
  536. dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n",
  537. __func__, freq);
  538. ret = 0;
  539. goto put_opp_table;
  540. }
  541. old_opp = _find_freq_ceil(opp_table, &old_freq);
  542. if (IS_ERR(old_opp)) {
  543. dev_err(dev, "%s: failed to find current OPP for freq %lu (%ld)\n",
  544. __func__, old_freq, PTR_ERR(old_opp));
  545. }
  546. opp = _find_freq_ceil(opp_table, &freq);
  547. if (IS_ERR(opp)) {
  548. ret = PTR_ERR(opp);
  549. dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n",
  550. __func__, freq, ret);
  551. goto put_old_opp;
  552. }
  553. dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__,
  554. old_freq, freq);
  555. regulators = opp_table->regulators;
  556. /* Only frequency scaling */
  557. if (!regulators) {
  558. ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq);
  559. goto put_opps;
  560. }
  561. if (opp_table->set_opp)
  562. set_opp = opp_table->set_opp;
  563. else
  564. set_opp = _generic_set_opp;
  565. data = opp_table->set_opp_data;
  566. data->regulators = regulators;
  567. data->regulator_count = opp_table->regulator_count;
  568. data->clk = clk;
  569. data->dev = dev;
  570. data->old_opp.rate = old_freq;
  571. size = sizeof(*opp->supplies) * opp_table->regulator_count;
  572. if (IS_ERR(old_opp))
  573. memset(data->old_opp.supplies, 0, size);
  574. else
  575. memcpy(data->old_opp.supplies, old_opp->supplies, size);
  576. data->new_opp.rate = freq;
  577. memcpy(data->new_opp.supplies, opp->supplies, size);
  578. ret = set_opp(data);
  579. put_opps:
  580. dev_pm_opp_put(opp);
  581. put_old_opp:
  582. if (!IS_ERR(old_opp))
  583. dev_pm_opp_put(old_opp);
  584. put_opp_table:
  585. dev_pm_opp_put_opp_table(opp_table);
  586. return ret;
  587. }
  588. EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate);
  589. /* OPP-dev Helpers */
  590. static void _remove_opp_dev(struct opp_device *opp_dev,
  591. struct opp_table *opp_table)
  592. {
  593. opp_debug_unregister(opp_dev, opp_table);
  594. list_del(&opp_dev->node);
  595. kfree(opp_dev);
  596. }
  597. struct opp_device *_add_opp_dev(const struct device *dev,
  598. struct opp_table *opp_table)
  599. {
  600. struct opp_device *opp_dev;
  601. int ret;
  602. opp_dev = kzalloc(sizeof(*opp_dev), GFP_KERNEL);
  603. if (!opp_dev)
  604. return NULL;
  605. /* Initialize opp-dev */
  606. opp_dev->dev = dev;
  607. list_add(&opp_dev->node, &opp_table->dev_list);
  608. /* Create debugfs entries for the opp_table */
  609. ret = opp_debug_register(opp_dev, opp_table);
  610. if (ret)
  611. dev_err(dev, "%s: Failed to register opp debugfs (%d)\n",
  612. __func__, ret);
  613. return opp_dev;
  614. }
  615. static struct opp_table *_allocate_opp_table(struct device *dev)
  616. {
  617. struct opp_table *opp_table;
  618. struct opp_device *opp_dev;
  619. int ret;
  620. /*
  621. * Allocate a new OPP table. In the infrequent case where a new
  622. * device is needed to be added, we pay this penalty.
  623. */
  624. opp_table = kzalloc(sizeof(*opp_table), GFP_KERNEL);
  625. if (!opp_table)
  626. return NULL;
  627. INIT_LIST_HEAD(&opp_table->dev_list);
  628. opp_dev = _add_opp_dev(dev, opp_table);
  629. if (!opp_dev) {
  630. kfree(opp_table);
  631. return NULL;
  632. }
  633. _of_init_opp_table(opp_table, dev);
  634. /* Find clk for the device */
  635. opp_table->clk = clk_get(dev, NULL);
  636. if (IS_ERR(opp_table->clk)) {
  637. ret = PTR_ERR(opp_table->clk);
  638. if (ret != -EPROBE_DEFER)
  639. dev_dbg(dev, "%s: Couldn't find clock: %d\n", __func__,
  640. ret);
  641. }
  642. BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head);
  643. INIT_LIST_HEAD(&opp_table->opp_list);
  644. mutex_init(&opp_table->lock);
  645. kref_init(&opp_table->kref);
  646. /* Secure the device table modification */
  647. list_add(&opp_table->node, &opp_tables);
  648. return opp_table;
  649. }
  650. void _get_opp_table_kref(struct opp_table *opp_table)
  651. {
  652. kref_get(&opp_table->kref);
  653. }
  654. struct opp_table *dev_pm_opp_get_opp_table(struct device *dev)
  655. {
  656. struct opp_table *opp_table;
  657. /* Hold our table modification lock here */
  658. mutex_lock(&opp_table_lock);
  659. opp_table = _find_opp_table_unlocked(dev);
  660. if (!IS_ERR(opp_table))
  661. goto unlock;
  662. opp_table = _allocate_opp_table(dev);
  663. unlock:
  664. mutex_unlock(&opp_table_lock);
  665. return opp_table;
  666. }
  667. EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table);
  668. static void _opp_table_kref_release(struct kref *kref)
  669. {
  670. struct opp_table *opp_table = container_of(kref, struct opp_table, kref);
  671. struct opp_device *opp_dev;
  672. /* Release clk */
  673. if (!IS_ERR(opp_table->clk))
  674. clk_put(opp_table->clk);
  675. opp_dev = list_first_entry(&opp_table->dev_list, struct opp_device,
  676. node);
  677. _remove_opp_dev(opp_dev, opp_table);
  678. /* dev_list must be empty now */
  679. WARN_ON(!list_empty(&opp_table->dev_list));
  680. mutex_destroy(&opp_table->lock);
  681. list_del(&opp_table->node);
  682. kfree(opp_table);
  683. mutex_unlock(&opp_table_lock);
  684. }
  685. void dev_pm_opp_put_opp_table(struct opp_table *opp_table)
  686. {
  687. kref_put_mutex(&opp_table->kref, _opp_table_kref_release,
  688. &opp_table_lock);
  689. }
  690. EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table);
  691. void _opp_free(struct dev_pm_opp *opp)
  692. {
  693. kfree(opp);
  694. }
  695. static void _opp_kref_release(struct kref *kref)
  696. {
  697. struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref);
  698. struct opp_table *opp_table = opp->opp_table;
  699. /*
  700. * Notify the changes in the availability of the operable
  701. * frequency/voltage list.
  702. */
  703. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp);
  704. opp_debug_remove_one(opp);
  705. list_del(&opp->node);
  706. kfree(opp);
  707. mutex_unlock(&opp_table->lock);
  708. dev_pm_opp_put_opp_table(opp_table);
  709. }
  710. static void dev_pm_opp_get(struct dev_pm_opp *opp)
  711. {
  712. kref_get(&opp->kref);
  713. }
  714. void dev_pm_opp_put(struct dev_pm_opp *opp)
  715. {
  716. kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock);
  717. }
  718. EXPORT_SYMBOL_GPL(dev_pm_opp_put);
  719. /**
  720. * dev_pm_opp_remove() - Remove an OPP from OPP table
  721. * @dev: device for which we do this operation
  722. * @freq: OPP to remove with matching 'freq'
  723. *
  724. * This function removes an opp from the opp table.
  725. */
  726. void dev_pm_opp_remove(struct device *dev, unsigned long freq)
  727. {
  728. struct dev_pm_opp *opp;
  729. struct opp_table *opp_table;
  730. bool found = false;
  731. opp_table = _find_opp_table(dev);
  732. if (IS_ERR(opp_table))
  733. return;
  734. mutex_lock(&opp_table->lock);
  735. list_for_each_entry(opp, &opp_table->opp_list, node) {
  736. if (opp->rate == freq) {
  737. found = true;
  738. break;
  739. }
  740. }
  741. mutex_unlock(&opp_table->lock);
  742. if (found) {
  743. dev_pm_opp_put(opp);
  744. } else {
  745. dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n",
  746. __func__, freq);
  747. }
  748. dev_pm_opp_put_opp_table(opp_table);
  749. }
  750. EXPORT_SYMBOL_GPL(dev_pm_opp_remove);
  751. struct dev_pm_opp *_opp_allocate(struct opp_table *table)
  752. {
  753. struct dev_pm_opp *opp;
  754. int count, supply_size;
  755. /* Allocate space for at least one supply */
  756. count = table->regulator_count ? table->regulator_count : 1;
  757. supply_size = sizeof(*opp->supplies) * count;
  758. /* allocate new OPP node and supplies structures */
  759. opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL);
  760. if (!opp)
  761. return NULL;
  762. /* Put the supplies at the end of the OPP structure as an empty array */
  763. opp->supplies = (struct dev_pm_opp_supply *)(opp + 1);
  764. INIT_LIST_HEAD(&opp->node);
  765. return opp;
  766. }
  767. static bool _opp_supported_by_regulators(struct dev_pm_opp *opp,
  768. struct opp_table *opp_table)
  769. {
  770. struct regulator *reg;
  771. int i;
  772. for (i = 0; i < opp_table->regulator_count; i++) {
  773. reg = opp_table->regulators[i];
  774. if (!regulator_is_supported_voltage(reg,
  775. opp->supplies[i].u_volt_min,
  776. opp->supplies[i].u_volt_max)) {
  777. pr_warn("%s: OPP minuV: %lu maxuV: %lu, not supported by regulator\n",
  778. __func__, opp->supplies[i].u_volt_min,
  779. opp->supplies[i].u_volt_max);
  780. return false;
  781. }
  782. }
  783. return true;
  784. }
  785. /*
  786. * Returns:
  787. * 0: On success. And appropriate error message for duplicate OPPs.
  788. * -EBUSY: For OPP with same freq/volt and is available. The callers of
  789. * _opp_add() must return 0 if they receive -EBUSY from it. This is to make
  790. * sure we don't print error messages unnecessarily if different parts of
  791. * kernel try to initialize the OPP table.
  792. * -EEXIST: For OPP with same freq but different volt or is unavailable. This
  793. * should be considered an error by the callers of _opp_add().
  794. */
  795. int _opp_add(struct device *dev, struct dev_pm_opp *new_opp,
  796. struct opp_table *opp_table)
  797. {
  798. struct dev_pm_opp *opp;
  799. struct list_head *head;
  800. int ret;
  801. /*
  802. * Insert new OPP in order of increasing frequency and discard if
  803. * already present.
  804. *
  805. * Need to use &opp_table->opp_list in the condition part of the 'for'
  806. * loop, don't replace it with head otherwise it will become an infinite
  807. * loop.
  808. */
  809. mutex_lock(&opp_table->lock);
  810. head = &opp_table->opp_list;
  811. list_for_each_entry(opp, &opp_table->opp_list, node) {
  812. if (new_opp->rate > opp->rate) {
  813. head = &opp->node;
  814. continue;
  815. }
  816. if (new_opp->rate < opp->rate)
  817. break;
  818. /* Duplicate OPPs */
  819. dev_warn(dev, "%s: duplicate OPPs detected. Existing: freq: %lu, volt: %lu, enabled: %d. New: freq: %lu, volt: %lu, enabled: %d\n",
  820. __func__, opp->rate, opp->supplies[0].u_volt,
  821. opp->available, new_opp->rate,
  822. new_opp->supplies[0].u_volt, new_opp->available);
  823. /* Should we compare voltages for all regulators here ? */
  824. ret = opp->available &&
  825. new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST;
  826. mutex_unlock(&opp_table->lock);
  827. return ret;
  828. }
  829. list_add(&new_opp->node, head);
  830. mutex_unlock(&opp_table->lock);
  831. new_opp->opp_table = opp_table;
  832. kref_init(&new_opp->kref);
  833. /* Get a reference to the OPP table */
  834. _get_opp_table_kref(opp_table);
  835. ret = opp_debug_create_one(new_opp, opp_table);
  836. if (ret)
  837. dev_err(dev, "%s: Failed to register opp to debugfs (%d)\n",
  838. __func__, ret);
  839. if (!_opp_supported_by_regulators(new_opp, opp_table)) {
  840. new_opp->available = false;
  841. dev_warn(dev, "%s: OPP not supported by regulators (%lu)\n",
  842. __func__, new_opp->rate);
  843. }
  844. return 0;
  845. }
  846. /**
  847. * _opp_add_v1() - Allocate a OPP based on v1 bindings.
  848. * @opp_table: OPP table
  849. * @dev: device for which we do this operation
  850. * @freq: Frequency in Hz for this OPP
  851. * @u_volt: Voltage in uVolts for this OPP
  852. * @dynamic: Dynamically added OPPs.
  853. *
  854. * This function adds an opp definition to the opp table and returns status.
  855. * The opp is made available by default and it can be controlled using
  856. * dev_pm_opp_enable/disable functions and may be removed by dev_pm_opp_remove.
  857. *
  858. * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table
  859. * and freed by dev_pm_opp_of_remove_table.
  860. *
  861. * Return:
  862. * 0 On success OR
  863. * Duplicate OPPs (both freq and volt are same) and opp->available
  864. * -EEXIST Freq are same and volt are different OR
  865. * Duplicate OPPs (both freq and volt are same) and !opp->available
  866. * -ENOMEM Memory allocation failure
  867. */
  868. int _opp_add_v1(struct opp_table *opp_table, struct device *dev,
  869. unsigned long freq, long u_volt, bool dynamic)
  870. {
  871. struct dev_pm_opp *new_opp;
  872. unsigned long tol;
  873. int ret;
  874. new_opp = _opp_allocate(opp_table);
  875. if (!new_opp)
  876. return -ENOMEM;
  877. /* populate the opp table */
  878. new_opp->rate = freq;
  879. tol = u_volt * opp_table->voltage_tolerance_v1 / 100;
  880. new_opp->supplies[0].u_volt = u_volt;
  881. new_opp->supplies[0].u_volt_min = u_volt - tol;
  882. new_opp->supplies[0].u_volt_max = u_volt + tol;
  883. new_opp->available = true;
  884. new_opp->dynamic = dynamic;
  885. ret = _opp_add(dev, new_opp, opp_table);
  886. if (ret) {
  887. /* Don't return error for duplicate OPPs */
  888. if (ret == -EBUSY)
  889. ret = 0;
  890. goto free_opp;
  891. }
  892. /*
  893. * Notify the changes in the availability of the operable
  894. * frequency/voltage list.
  895. */
  896. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp);
  897. return 0;
  898. free_opp:
  899. _opp_free(new_opp);
  900. return ret;
  901. }
  902. /**
  903. * dev_pm_opp_set_supported_hw() - Set supported platforms
  904. * @dev: Device for which supported-hw has to be set.
  905. * @versions: Array of hierarchy of versions to match.
  906. * @count: Number of elements in the array.
  907. *
  908. * This is required only for the V2 bindings, and it enables a platform to
  909. * specify the hierarchy of versions it supports. OPP layer will then enable
  910. * OPPs, which are available for those versions, based on its 'opp-supported-hw'
  911. * property.
  912. */
  913. struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev,
  914. const u32 *versions, unsigned int count)
  915. {
  916. struct opp_table *opp_table;
  917. int ret;
  918. opp_table = dev_pm_opp_get_opp_table(dev);
  919. if (!opp_table)
  920. return ERR_PTR(-ENOMEM);
  921. /* Make sure there are no concurrent readers while updating opp_table */
  922. WARN_ON(!list_empty(&opp_table->opp_list));
  923. /* Do we already have a version hierarchy associated with opp_table? */
  924. if (opp_table->supported_hw) {
  925. dev_err(dev, "%s: Already have supported hardware list\n",
  926. __func__);
  927. ret = -EBUSY;
  928. goto err;
  929. }
  930. opp_table->supported_hw = kmemdup(versions, count * sizeof(*versions),
  931. GFP_KERNEL);
  932. if (!opp_table->supported_hw) {
  933. ret = -ENOMEM;
  934. goto err;
  935. }
  936. opp_table->supported_hw_count = count;
  937. return opp_table;
  938. err:
  939. dev_pm_opp_put_opp_table(opp_table);
  940. return ERR_PTR(ret);
  941. }
  942. EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw);
  943. /**
  944. * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw
  945. * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw().
  946. *
  947. * This is required only for the V2 bindings, and is called for a matching
  948. * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure
  949. * will not be freed.
  950. */
  951. void dev_pm_opp_put_supported_hw(struct opp_table *opp_table)
  952. {
  953. /* Make sure there are no concurrent readers while updating opp_table */
  954. WARN_ON(!list_empty(&opp_table->opp_list));
  955. if (!opp_table->supported_hw) {
  956. pr_err("%s: Doesn't have supported hardware list\n",
  957. __func__);
  958. return;
  959. }
  960. kfree(opp_table->supported_hw);
  961. opp_table->supported_hw = NULL;
  962. opp_table->supported_hw_count = 0;
  963. dev_pm_opp_put_opp_table(opp_table);
  964. }
  965. EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw);
  966. /**
  967. * dev_pm_opp_set_prop_name() - Set prop-extn name
  968. * @dev: Device for which the prop-name has to be set.
  969. * @name: name to postfix to properties.
  970. *
  971. * This is required only for the V2 bindings, and it enables a platform to
  972. * specify the extn to be used for certain property names. The properties to
  973. * which the extension will apply are opp-microvolt and opp-microamp. OPP core
  974. * should postfix the property name with -<name> while looking for them.
  975. */
  976. struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name)
  977. {
  978. struct opp_table *opp_table;
  979. int ret;
  980. opp_table = dev_pm_opp_get_opp_table(dev);
  981. if (!opp_table)
  982. return ERR_PTR(-ENOMEM);
  983. /* Make sure there are no concurrent readers while updating opp_table */
  984. WARN_ON(!list_empty(&opp_table->opp_list));
  985. /* Do we already have a prop-name associated with opp_table? */
  986. if (opp_table->prop_name) {
  987. dev_err(dev, "%s: Already have prop-name %s\n", __func__,
  988. opp_table->prop_name);
  989. ret = -EBUSY;
  990. goto err;
  991. }
  992. opp_table->prop_name = kstrdup(name, GFP_KERNEL);
  993. if (!opp_table->prop_name) {
  994. ret = -ENOMEM;
  995. goto err;
  996. }
  997. return opp_table;
  998. err:
  999. dev_pm_opp_put_opp_table(opp_table);
  1000. return ERR_PTR(ret);
  1001. }
  1002. EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name);
  1003. /**
  1004. * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name
  1005. * @opp_table: OPP table returned by dev_pm_opp_set_prop_name().
  1006. *
  1007. * This is required only for the V2 bindings, and is called for a matching
  1008. * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure
  1009. * will not be freed.
  1010. */
  1011. void dev_pm_opp_put_prop_name(struct opp_table *opp_table)
  1012. {
  1013. /* Make sure there are no concurrent readers while updating opp_table */
  1014. WARN_ON(!list_empty(&opp_table->opp_list));
  1015. if (!opp_table->prop_name) {
  1016. pr_err("%s: Doesn't have a prop-name\n", __func__);
  1017. return;
  1018. }
  1019. kfree(opp_table->prop_name);
  1020. opp_table->prop_name = NULL;
  1021. dev_pm_opp_put_opp_table(opp_table);
  1022. }
  1023. EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name);
  1024. static int _allocate_set_opp_data(struct opp_table *opp_table)
  1025. {
  1026. struct dev_pm_set_opp_data *data;
  1027. int len, count = opp_table->regulator_count;
  1028. if (WARN_ON(!count))
  1029. return -EINVAL;
  1030. /* space for set_opp_data */
  1031. len = sizeof(*data);
  1032. /* space for old_opp.supplies and new_opp.supplies */
  1033. len += 2 * sizeof(struct dev_pm_opp_supply) * count;
  1034. data = kzalloc(len, GFP_KERNEL);
  1035. if (!data)
  1036. return -ENOMEM;
  1037. data->old_opp.supplies = (void *)(data + 1);
  1038. data->new_opp.supplies = data->old_opp.supplies + count;
  1039. opp_table->set_opp_data = data;
  1040. return 0;
  1041. }
  1042. static void _free_set_opp_data(struct opp_table *opp_table)
  1043. {
  1044. kfree(opp_table->set_opp_data);
  1045. opp_table->set_opp_data = NULL;
  1046. }
  1047. /**
  1048. * dev_pm_opp_set_regulators() - Set regulator names for the device
  1049. * @dev: Device for which regulator name is being set.
  1050. * @names: Array of pointers to the names of the regulator.
  1051. * @count: Number of regulators.
  1052. *
  1053. * In order to support OPP switching, OPP layer needs to know the name of the
  1054. * device's regulators, as the core would be required to switch voltages as
  1055. * well.
  1056. *
  1057. * This must be called before any OPPs are initialized for the device.
  1058. */
  1059. struct opp_table *dev_pm_opp_set_regulators(struct device *dev,
  1060. const char * const names[],
  1061. unsigned int count)
  1062. {
  1063. struct opp_table *opp_table;
  1064. struct regulator *reg;
  1065. int ret, i;
  1066. opp_table = dev_pm_opp_get_opp_table(dev);
  1067. if (!opp_table)
  1068. return ERR_PTR(-ENOMEM);
  1069. /* This should be called before OPPs are initialized */
  1070. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  1071. ret = -EBUSY;
  1072. goto err;
  1073. }
  1074. /* Already have regulators set */
  1075. if (opp_table->regulators) {
  1076. ret = -EBUSY;
  1077. goto err;
  1078. }
  1079. opp_table->regulators = kmalloc_array(count,
  1080. sizeof(*opp_table->regulators),
  1081. GFP_KERNEL);
  1082. if (!opp_table->regulators) {
  1083. ret = -ENOMEM;
  1084. goto err;
  1085. }
  1086. for (i = 0; i < count; i++) {
  1087. reg = regulator_get_optional(dev, names[i]);
  1088. if (IS_ERR(reg)) {
  1089. ret = PTR_ERR(reg);
  1090. if (ret != -EPROBE_DEFER)
  1091. dev_err(dev, "%s: no regulator (%s) found: %d\n",
  1092. __func__, names[i], ret);
  1093. goto free_regulators;
  1094. }
  1095. opp_table->regulators[i] = reg;
  1096. }
  1097. opp_table->regulator_count = count;
  1098. /* Allocate block only once to pass to set_opp() routines */
  1099. ret = _allocate_set_opp_data(opp_table);
  1100. if (ret)
  1101. goto free_regulators;
  1102. return opp_table;
  1103. free_regulators:
  1104. while (i != 0)
  1105. regulator_put(opp_table->regulators[--i]);
  1106. kfree(opp_table->regulators);
  1107. opp_table->regulators = NULL;
  1108. opp_table->regulator_count = 0;
  1109. err:
  1110. dev_pm_opp_put_opp_table(opp_table);
  1111. return ERR_PTR(ret);
  1112. }
  1113. EXPORT_SYMBOL_GPL(dev_pm_opp_set_regulators);
  1114. /**
  1115. * dev_pm_opp_put_regulators() - Releases resources blocked for regulator
  1116. * @opp_table: OPP table returned from dev_pm_opp_set_regulators().
  1117. */
  1118. void dev_pm_opp_put_regulators(struct opp_table *opp_table)
  1119. {
  1120. int i;
  1121. if (!opp_table->regulators) {
  1122. pr_err("%s: Doesn't have regulators set\n", __func__);
  1123. return;
  1124. }
  1125. /* Make sure there are no concurrent readers while updating opp_table */
  1126. WARN_ON(!list_empty(&opp_table->opp_list));
  1127. for (i = opp_table->regulator_count - 1; i >= 0; i--)
  1128. regulator_put(opp_table->regulators[i]);
  1129. _free_set_opp_data(opp_table);
  1130. kfree(opp_table->regulators);
  1131. opp_table->regulators = NULL;
  1132. opp_table->regulator_count = 0;
  1133. dev_pm_opp_put_opp_table(opp_table);
  1134. }
  1135. EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators);
  1136. /**
  1137. * dev_pm_opp_register_set_opp_helper() - Register custom set OPP helper
  1138. * @dev: Device for which the helper is getting registered.
  1139. * @set_opp: Custom set OPP helper.
  1140. *
  1141. * This is useful to support complex platforms (like platforms with multiple
  1142. * regulators per device), instead of the generic OPP set rate helper.
  1143. *
  1144. * This must be called before any OPPs are initialized for the device.
  1145. */
  1146. struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev,
  1147. int (*set_opp)(struct dev_pm_set_opp_data *data))
  1148. {
  1149. struct opp_table *opp_table;
  1150. int ret;
  1151. if (!set_opp)
  1152. return ERR_PTR(-EINVAL);
  1153. opp_table = dev_pm_opp_get_opp_table(dev);
  1154. if (!opp_table)
  1155. return ERR_PTR(-ENOMEM);
  1156. /* This should be called before OPPs are initialized */
  1157. if (WARN_ON(!list_empty(&opp_table->opp_list))) {
  1158. ret = -EBUSY;
  1159. goto err;
  1160. }
  1161. /* Already have custom set_opp helper */
  1162. if (WARN_ON(opp_table->set_opp)) {
  1163. ret = -EBUSY;
  1164. goto err;
  1165. }
  1166. opp_table->set_opp = set_opp;
  1167. return opp_table;
  1168. err:
  1169. dev_pm_opp_put_opp_table(opp_table);
  1170. return ERR_PTR(ret);
  1171. }
  1172. EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper);
  1173. /**
  1174. * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for
  1175. * set_opp helper
  1176. * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper().
  1177. *
  1178. * Release resources blocked for platform specific set_opp helper.
  1179. */
  1180. void dev_pm_opp_register_put_opp_helper(struct opp_table *opp_table)
  1181. {
  1182. if (!opp_table->set_opp) {
  1183. pr_err("%s: Doesn't have custom set_opp helper set\n",
  1184. __func__);
  1185. return;
  1186. }
  1187. /* Make sure there are no concurrent readers while updating opp_table */
  1188. WARN_ON(!list_empty(&opp_table->opp_list));
  1189. opp_table->set_opp = NULL;
  1190. dev_pm_opp_put_opp_table(opp_table);
  1191. }
  1192. EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper);
  1193. /**
  1194. * dev_pm_opp_add() - Add an OPP table from a table definitions
  1195. * @dev: device for which we do this operation
  1196. * @freq: Frequency in Hz for this OPP
  1197. * @u_volt: Voltage in uVolts for this OPP
  1198. *
  1199. * This function adds an opp definition to the opp table and returns status.
  1200. * The opp is made available by default and it can be controlled using
  1201. * dev_pm_opp_enable/disable functions.
  1202. *
  1203. * Return:
  1204. * 0 On success OR
  1205. * Duplicate OPPs (both freq and volt are same) and opp->available
  1206. * -EEXIST Freq are same and volt are different OR
  1207. * Duplicate OPPs (both freq and volt are same) and !opp->available
  1208. * -ENOMEM Memory allocation failure
  1209. */
  1210. int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt)
  1211. {
  1212. struct opp_table *opp_table;
  1213. int ret;
  1214. opp_table = dev_pm_opp_get_opp_table(dev);
  1215. if (!opp_table)
  1216. return -ENOMEM;
  1217. ret = _opp_add_v1(opp_table, dev, freq, u_volt, true);
  1218. dev_pm_opp_put_opp_table(opp_table);
  1219. return ret;
  1220. }
  1221. EXPORT_SYMBOL_GPL(dev_pm_opp_add);
  1222. /**
  1223. * _opp_set_availability() - helper to set the availability of an opp
  1224. * @dev: device for which we do this operation
  1225. * @freq: OPP frequency to modify availability
  1226. * @availability_req: availability status requested for this opp
  1227. *
  1228. * Set the availability of an OPP, opp_{enable,disable} share a common logic
  1229. * which is isolated here.
  1230. *
  1231. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1232. * copy operation, returns 0 if no modification was done OR modification was
  1233. * successful.
  1234. */
  1235. static int _opp_set_availability(struct device *dev, unsigned long freq,
  1236. bool availability_req)
  1237. {
  1238. struct opp_table *opp_table;
  1239. struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV);
  1240. int r = 0;
  1241. /* Find the opp_table */
  1242. opp_table = _find_opp_table(dev);
  1243. if (IS_ERR(opp_table)) {
  1244. r = PTR_ERR(opp_table);
  1245. dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r);
  1246. return r;
  1247. }
  1248. mutex_lock(&opp_table->lock);
  1249. /* Do we have the frequency? */
  1250. list_for_each_entry(tmp_opp, &opp_table->opp_list, node) {
  1251. if (tmp_opp->rate == freq) {
  1252. opp = tmp_opp;
  1253. break;
  1254. }
  1255. }
  1256. if (IS_ERR(opp)) {
  1257. r = PTR_ERR(opp);
  1258. goto unlock;
  1259. }
  1260. /* Is update really needed? */
  1261. if (opp->available == availability_req)
  1262. goto unlock;
  1263. opp->available = availability_req;
  1264. /* Notify the change of the OPP availability */
  1265. if (availability_req)
  1266. blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE,
  1267. opp);
  1268. else
  1269. blocking_notifier_call_chain(&opp_table->head,
  1270. OPP_EVENT_DISABLE, opp);
  1271. unlock:
  1272. mutex_unlock(&opp_table->lock);
  1273. dev_pm_opp_put_opp_table(opp_table);
  1274. return r;
  1275. }
  1276. /**
  1277. * dev_pm_opp_enable() - Enable a specific OPP
  1278. * @dev: device for which we do this operation
  1279. * @freq: OPP frequency to enable
  1280. *
  1281. * Enables a provided opp. If the operation is valid, this returns 0, else the
  1282. * corresponding error value. It is meant to be used for users an OPP available
  1283. * after being temporarily made unavailable with dev_pm_opp_disable.
  1284. *
  1285. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1286. * copy operation, returns 0 if no modification was done OR modification was
  1287. * successful.
  1288. */
  1289. int dev_pm_opp_enable(struct device *dev, unsigned long freq)
  1290. {
  1291. return _opp_set_availability(dev, freq, true);
  1292. }
  1293. EXPORT_SYMBOL_GPL(dev_pm_opp_enable);
  1294. /**
  1295. * dev_pm_opp_disable() - Disable a specific OPP
  1296. * @dev: device for which we do this operation
  1297. * @freq: OPP frequency to disable
  1298. *
  1299. * Disables a provided opp. If the operation is valid, this returns
  1300. * 0, else the corresponding error value. It is meant to be a temporary
  1301. * control by users to make this OPP not available until the circumstances are
  1302. * right to make it available again (with a call to dev_pm_opp_enable).
  1303. *
  1304. * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the
  1305. * copy operation, returns 0 if no modification was done OR modification was
  1306. * successful.
  1307. */
  1308. int dev_pm_opp_disable(struct device *dev, unsigned long freq)
  1309. {
  1310. return _opp_set_availability(dev, freq, false);
  1311. }
  1312. EXPORT_SYMBOL_GPL(dev_pm_opp_disable);
  1313. /**
  1314. * dev_pm_opp_register_notifier() - Register OPP notifier for the device
  1315. * @dev: Device for which notifier needs to be registered
  1316. * @nb: Notifier block to be registered
  1317. *
  1318. * Return: 0 on success or a negative error value.
  1319. */
  1320. int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb)
  1321. {
  1322. struct opp_table *opp_table;
  1323. int ret;
  1324. opp_table = _find_opp_table(dev);
  1325. if (IS_ERR(opp_table))
  1326. return PTR_ERR(opp_table);
  1327. ret = blocking_notifier_chain_register(&opp_table->head, nb);
  1328. dev_pm_opp_put_opp_table(opp_table);
  1329. return ret;
  1330. }
  1331. EXPORT_SYMBOL(dev_pm_opp_register_notifier);
  1332. /**
  1333. * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device
  1334. * @dev: Device for which notifier needs to be unregistered
  1335. * @nb: Notifier block to be unregistered
  1336. *
  1337. * Return: 0 on success or a negative error value.
  1338. */
  1339. int dev_pm_opp_unregister_notifier(struct device *dev,
  1340. struct notifier_block *nb)
  1341. {
  1342. struct opp_table *opp_table;
  1343. int ret;
  1344. opp_table = _find_opp_table(dev);
  1345. if (IS_ERR(opp_table))
  1346. return PTR_ERR(opp_table);
  1347. ret = blocking_notifier_chain_unregister(&opp_table->head, nb);
  1348. dev_pm_opp_put_opp_table(opp_table);
  1349. return ret;
  1350. }
  1351. EXPORT_SYMBOL(dev_pm_opp_unregister_notifier);
  1352. /*
  1353. * Free OPPs either created using static entries present in DT or even the
  1354. * dynamically added entries based on remove_all param.
  1355. */
  1356. void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev,
  1357. bool remove_all)
  1358. {
  1359. struct dev_pm_opp *opp, *tmp;
  1360. /* Find if opp_table manages a single device */
  1361. if (list_is_singular(&opp_table->dev_list)) {
  1362. /* Free static OPPs */
  1363. list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) {
  1364. if (remove_all || !opp->dynamic)
  1365. dev_pm_opp_put(opp);
  1366. }
  1367. } else {
  1368. _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table);
  1369. }
  1370. }
  1371. void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all)
  1372. {
  1373. struct opp_table *opp_table;
  1374. /* Check for existing table for 'dev' */
  1375. opp_table = _find_opp_table(dev);
  1376. if (IS_ERR(opp_table)) {
  1377. int error = PTR_ERR(opp_table);
  1378. if (error != -ENODEV)
  1379. WARN(1, "%s: opp_table: %d\n",
  1380. IS_ERR_OR_NULL(dev) ?
  1381. "Invalid device" : dev_name(dev),
  1382. error);
  1383. return;
  1384. }
  1385. _dev_pm_opp_remove_table(opp_table, dev, remove_all);
  1386. dev_pm_opp_put_opp_table(opp_table);
  1387. }
  1388. /**
  1389. * dev_pm_opp_remove_table() - Free all OPPs associated with the device
  1390. * @dev: device pointer used to lookup OPP table.
  1391. *
  1392. * Free both OPPs created using static entries present in DT and the
  1393. * dynamically added entries.
  1394. */
  1395. void dev_pm_opp_remove_table(struct device *dev)
  1396. {
  1397. _dev_pm_opp_find_and_remove_table(dev, true);
  1398. }
  1399. EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);