devfreq.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /*
  2. * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
  3. * for Non-CPU Devices.
  4. *
  5. * Copyright (C) 2011 Samsung Electronics
  6. * MyungJoo Ham <myungjoo.ham@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/export.h>
  18. #include <linux/slab.h>
  19. #include <linux/stat.h>
  20. #include <linux/pm_opp.h>
  21. #include <linux/devfreq.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/list.h>
  25. #include <linux/printk.h>
  26. #include <linux/hrtimer.h>
  27. #include <linux/of.h>
  28. #include "governor.h"
  29. #define MAX(a,b) ((a > b) ? a : b)
  30. #define MIN(a,b) ((a < b) ? a : b)
  31. static struct class *devfreq_class;
  32. /*
  33. * devfreq core provides delayed work based load monitoring helper
  34. * functions. Governors can use these or can implement their own
  35. * monitoring mechanism.
  36. */
  37. static struct workqueue_struct *devfreq_wq;
  38. /* The list of all device-devfreq governors */
  39. static LIST_HEAD(devfreq_governor_list);
  40. /* The list of all device-devfreq */
  41. static LIST_HEAD(devfreq_list);
  42. static DEFINE_MUTEX(devfreq_list_lock);
  43. /**
  44. * find_device_devfreq() - find devfreq struct using device pointer
  45. * @dev: device pointer used to lookup device devfreq.
  46. *
  47. * Search the list of device devfreqs and return the matched device's
  48. * devfreq info. devfreq_list_lock should be held by the caller.
  49. */
  50. static struct devfreq *find_device_devfreq(struct device *dev)
  51. {
  52. struct devfreq *tmp_devfreq;
  53. if (IS_ERR_OR_NULL(dev)) {
  54. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  55. return ERR_PTR(-EINVAL);
  56. }
  57. WARN(!mutex_is_locked(&devfreq_list_lock),
  58. "devfreq_list_lock must be locked.");
  59. list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
  60. if (tmp_devfreq->dev.parent == dev)
  61. return tmp_devfreq;
  62. }
  63. return ERR_PTR(-ENODEV);
  64. }
  65. static unsigned long find_available_min_freq(struct devfreq *devfreq)
  66. {
  67. struct dev_pm_opp *opp;
  68. unsigned long min_freq = 0;
  69. opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
  70. if (IS_ERR(opp))
  71. min_freq = 0;
  72. else
  73. dev_pm_opp_put(opp);
  74. return min_freq;
  75. }
  76. static unsigned long find_available_max_freq(struct devfreq *devfreq)
  77. {
  78. struct dev_pm_opp *opp;
  79. unsigned long max_freq = ULONG_MAX;
  80. opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
  81. if (IS_ERR(opp))
  82. max_freq = 0;
  83. else
  84. dev_pm_opp_put(opp);
  85. return max_freq;
  86. }
  87. /**
  88. * devfreq_get_freq_level() - Lookup freq_table for the frequency
  89. * @devfreq: the devfreq instance
  90. * @freq: the target frequency
  91. */
  92. static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
  93. {
  94. int lev;
  95. for (lev = 0; lev < devfreq->profile->max_state; lev++)
  96. if (freq == devfreq->profile->freq_table[lev])
  97. return lev;
  98. return -EINVAL;
  99. }
  100. static int set_freq_table(struct devfreq *devfreq)
  101. {
  102. struct devfreq_dev_profile *profile = devfreq->profile;
  103. struct dev_pm_opp *opp;
  104. unsigned long freq;
  105. int i, count;
  106. /* Initialize the freq_table from OPP table */
  107. count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
  108. if (count <= 0)
  109. return -EINVAL;
  110. profile->max_state = count;
  111. profile->freq_table = devm_kcalloc(devfreq->dev.parent,
  112. profile->max_state,
  113. sizeof(*profile->freq_table),
  114. GFP_KERNEL);
  115. if (!profile->freq_table) {
  116. profile->max_state = 0;
  117. return -ENOMEM;
  118. }
  119. for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
  120. opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
  121. if (IS_ERR(opp)) {
  122. devm_kfree(devfreq->dev.parent, profile->freq_table);
  123. profile->max_state = 0;
  124. return PTR_ERR(opp);
  125. }
  126. dev_pm_opp_put(opp);
  127. profile->freq_table[i] = freq;
  128. }
  129. return 0;
  130. }
  131. /**
  132. * devfreq_update_status() - Update statistics of devfreq behavior
  133. * @devfreq: the devfreq instance
  134. * @freq: the update target frequency
  135. */
  136. int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
  137. {
  138. int lev, prev_lev, ret = 0;
  139. unsigned long cur_time;
  140. cur_time = jiffies;
  141. /* Immediately exit if previous_freq is not initialized yet. */
  142. if (!devfreq->previous_freq)
  143. goto out;
  144. prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
  145. if (prev_lev < 0) {
  146. ret = prev_lev;
  147. goto out;
  148. }
  149. devfreq->time_in_state[prev_lev] +=
  150. cur_time - devfreq->last_stat_updated;
  151. lev = devfreq_get_freq_level(devfreq, freq);
  152. if (lev < 0) {
  153. ret = lev;
  154. goto out;
  155. }
  156. if (lev != prev_lev) {
  157. devfreq->trans_table[(prev_lev *
  158. devfreq->profile->max_state) + lev]++;
  159. devfreq->total_trans++;
  160. }
  161. out:
  162. devfreq->last_stat_updated = cur_time;
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(devfreq_update_status);
  166. /**
  167. * find_devfreq_governor() - find devfreq governor from name
  168. * @name: name of the governor
  169. *
  170. * Search the list of devfreq governors and return the matched
  171. * governor's pointer. devfreq_list_lock should be held by the caller.
  172. */
  173. static struct devfreq_governor *find_devfreq_governor(const char *name)
  174. {
  175. struct devfreq_governor *tmp_governor;
  176. if (IS_ERR_OR_NULL(name)) {
  177. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  178. return ERR_PTR(-EINVAL);
  179. }
  180. WARN(!mutex_is_locked(&devfreq_list_lock),
  181. "devfreq_list_lock must be locked.");
  182. list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
  183. if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
  184. return tmp_governor;
  185. }
  186. return ERR_PTR(-ENODEV);
  187. }
  188. static int devfreq_notify_transition(struct devfreq *devfreq,
  189. struct devfreq_freqs *freqs, unsigned int state)
  190. {
  191. if (!devfreq)
  192. return -EINVAL;
  193. switch (state) {
  194. case DEVFREQ_PRECHANGE:
  195. srcu_notifier_call_chain(&devfreq->transition_notifier_list,
  196. DEVFREQ_PRECHANGE, freqs);
  197. break;
  198. case DEVFREQ_POSTCHANGE:
  199. srcu_notifier_call_chain(&devfreq->transition_notifier_list,
  200. DEVFREQ_POSTCHANGE, freqs);
  201. break;
  202. default:
  203. return -EINVAL;
  204. }
  205. return 0;
  206. }
  207. /* Load monitoring helper functions for governors use */
  208. /**
  209. * update_devfreq() - Reevaluate the device and configure frequency.
  210. * @devfreq: the devfreq instance.
  211. *
  212. * Note: Lock devfreq->lock before calling update_devfreq
  213. * This function is exported for governors.
  214. */
  215. int update_devfreq(struct devfreq *devfreq)
  216. {
  217. struct devfreq_freqs freqs;
  218. unsigned long freq, cur_freq, min_freq, max_freq;
  219. int err = 0;
  220. u32 flags = 0;
  221. if (!mutex_is_locked(&devfreq->lock)) {
  222. WARN(true, "devfreq->lock must be locked by the caller.\n");
  223. return -EINVAL;
  224. }
  225. if (!devfreq->governor)
  226. return -EINVAL;
  227. /* Reevaluate the proper frequency */
  228. err = devfreq->governor->get_target_freq(devfreq, &freq);
  229. if (err)
  230. return err;
  231. /*
  232. * Adjust the frequency with user freq, QoS and available freq.
  233. *
  234. * List from the highest priority
  235. * max_freq
  236. * min_freq
  237. */
  238. max_freq = MIN(devfreq->scaling_max_freq, devfreq->max_freq);
  239. min_freq = MAX(devfreq->scaling_min_freq, devfreq->min_freq);
  240. if (min_freq && freq < min_freq) {
  241. freq = min_freq;
  242. flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
  243. }
  244. if (max_freq && freq > max_freq) {
  245. freq = max_freq;
  246. flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
  247. }
  248. if (devfreq->profile->get_cur_freq)
  249. devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
  250. else
  251. cur_freq = devfreq->previous_freq;
  252. freqs.old = cur_freq;
  253. freqs.new = freq;
  254. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
  255. err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
  256. if (err) {
  257. freqs.new = cur_freq;
  258. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
  259. return err;
  260. }
  261. freqs.new = freq;
  262. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
  263. if (devfreq_update_status(devfreq, freq))
  264. dev_err(&devfreq->dev,
  265. "Couldn't update frequency transition information.\n");
  266. devfreq->previous_freq = freq;
  267. return err;
  268. }
  269. EXPORT_SYMBOL(update_devfreq);
  270. /**
  271. * devfreq_monitor() - Periodically poll devfreq objects.
  272. * @work: the work struct used to run devfreq_monitor periodically.
  273. *
  274. */
  275. static void devfreq_monitor(struct work_struct *work)
  276. {
  277. int err;
  278. struct devfreq *devfreq = container_of(work,
  279. struct devfreq, work.work);
  280. mutex_lock(&devfreq->lock);
  281. err = update_devfreq(devfreq);
  282. if (err)
  283. dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
  284. queue_delayed_work(devfreq_wq, &devfreq->work,
  285. msecs_to_jiffies(devfreq->profile->polling_ms));
  286. mutex_unlock(&devfreq->lock);
  287. }
  288. /**
  289. * devfreq_monitor_start() - Start load monitoring of devfreq instance
  290. * @devfreq: the devfreq instance.
  291. *
  292. * Helper function for starting devfreq device load monitoing. By
  293. * default delayed work based monitoring is supported. Function
  294. * to be called from governor in response to DEVFREQ_GOV_START
  295. * event when device is added to devfreq framework.
  296. */
  297. void devfreq_monitor_start(struct devfreq *devfreq)
  298. {
  299. INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
  300. if (devfreq->profile->polling_ms)
  301. queue_delayed_work(devfreq_wq, &devfreq->work,
  302. msecs_to_jiffies(devfreq->profile->polling_ms));
  303. }
  304. EXPORT_SYMBOL(devfreq_monitor_start);
  305. /**
  306. * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
  307. * @devfreq: the devfreq instance.
  308. *
  309. * Helper function to stop devfreq device load monitoing. Function
  310. * to be called from governor in response to DEVFREQ_GOV_STOP
  311. * event when device is removed from devfreq framework.
  312. */
  313. void devfreq_monitor_stop(struct devfreq *devfreq)
  314. {
  315. cancel_delayed_work_sync(&devfreq->work);
  316. }
  317. EXPORT_SYMBOL(devfreq_monitor_stop);
  318. /**
  319. * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
  320. * @devfreq: the devfreq instance.
  321. *
  322. * Helper function to suspend devfreq device load monitoing. Function
  323. * to be called from governor in response to DEVFREQ_GOV_SUSPEND
  324. * event or when polling interval is set to zero.
  325. *
  326. * Note: Though this function is same as devfreq_monitor_stop(),
  327. * intentionally kept separate to provide hooks for collecting
  328. * transition statistics.
  329. */
  330. void devfreq_monitor_suspend(struct devfreq *devfreq)
  331. {
  332. mutex_lock(&devfreq->lock);
  333. if (devfreq->stop_polling) {
  334. mutex_unlock(&devfreq->lock);
  335. return;
  336. }
  337. devfreq_update_status(devfreq, devfreq->previous_freq);
  338. devfreq->stop_polling = true;
  339. mutex_unlock(&devfreq->lock);
  340. cancel_delayed_work_sync(&devfreq->work);
  341. }
  342. EXPORT_SYMBOL(devfreq_monitor_suspend);
  343. /**
  344. * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
  345. * @devfreq: the devfreq instance.
  346. *
  347. * Helper function to resume devfreq device load monitoing. Function
  348. * to be called from governor in response to DEVFREQ_GOV_RESUME
  349. * event or when polling interval is set to non-zero.
  350. */
  351. void devfreq_monitor_resume(struct devfreq *devfreq)
  352. {
  353. unsigned long freq;
  354. mutex_lock(&devfreq->lock);
  355. if (!devfreq->stop_polling)
  356. goto out;
  357. if (!delayed_work_pending(&devfreq->work) &&
  358. devfreq->profile->polling_ms)
  359. queue_delayed_work(devfreq_wq, &devfreq->work,
  360. msecs_to_jiffies(devfreq->profile->polling_ms));
  361. devfreq->last_stat_updated = jiffies;
  362. devfreq->stop_polling = false;
  363. if (devfreq->profile->get_cur_freq &&
  364. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  365. devfreq->previous_freq = freq;
  366. out:
  367. mutex_unlock(&devfreq->lock);
  368. }
  369. EXPORT_SYMBOL(devfreq_monitor_resume);
  370. /**
  371. * devfreq_interval_update() - Update device devfreq monitoring interval
  372. * @devfreq: the devfreq instance.
  373. * @delay: new polling interval to be set.
  374. *
  375. * Helper function to set new load monitoring polling interval. Function
  376. * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
  377. */
  378. void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
  379. {
  380. unsigned int cur_delay = devfreq->profile->polling_ms;
  381. unsigned int new_delay = *delay;
  382. mutex_lock(&devfreq->lock);
  383. devfreq->profile->polling_ms = new_delay;
  384. if (devfreq->stop_polling)
  385. goto out;
  386. /* if new delay is zero, stop polling */
  387. if (!new_delay) {
  388. mutex_unlock(&devfreq->lock);
  389. cancel_delayed_work_sync(&devfreq->work);
  390. return;
  391. }
  392. /* if current delay is zero, start polling with new delay */
  393. if (!cur_delay) {
  394. queue_delayed_work(devfreq_wq, &devfreq->work,
  395. msecs_to_jiffies(devfreq->profile->polling_ms));
  396. goto out;
  397. }
  398. /* if current delay is greater than new delay, restart polling */
  399. if (cur_delay > new_delay) {
  400. mutex_unlock(&devfreq->lock);
  401. cancel_delayed_work_sync(&devfreq->work);
  402. mutex_lock(&devfreq->lock);
  403. if (!devfreq->stop_polling)
  404. queue_delayed_work(devfreq_wq, &devfreq->work,
  405. msecs_to_jiffies(devfreq->profile->polling_ms));
  406. }
  407. out:
  408. mutex_unlock(&devfreq->lock);
  409. }
  410. EXPORT_SYMBOL(devfreq_interval_update);
  411. /**
  412. * devfreq_notifier_call() - Notify that the device frequency requirements
  413. * has been changed out of devfreq framework.
  414. * @nb: the notifier_block (supposed to be devfreq->nb)
  415. * @type: not used
  416. * @devp: not used
  417. *
  418. * Called by a notifier that uses devfreq->nb.
  419. */
  420. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  421. void *devp)
  422. {
  423. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  424. int ret;
  425. mutex_lock(&devfreq->lock);
  426. devfreq->scaling_min_freq = find_available_min_freq(devfreq);
  427. if (!devfreq->scaling_min_freq) {
  428. mutex_unlock(&devfreq->lock);
  429. return -EINVAL;
  430. }
  431. devfreq->scaling_max_freq = find_available_max_freq(devfreq);
  432. if (!devfreq->scaling_max_freq) {
  433. mutex_unlock(&devfreq->lock);
  434. return -EINVAL;
  435. }
  436. ret = update_devfreq(devfreq);
  437. mutex_unlock(&devfreq->lock);
  438. return ret;
  439. }
  440. /**
  441. * devfreq_dev_release() - Callback for struct device to release the device.
  442. * @dev: the devfreq device
  443. *
  444. * Remove devfreq from the list and release its resources.
  445. */
  446. static void devfreq_dev_release(struct device *dev)
  447. {
  448. struct devfreq *devfreq = to_devfreq(dev);
  449. mutex_lock(&devfreq_list_lock);
  450. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  451. mutex_unlock(&devfreq_list_lock);
  452. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  453. return;
  454. }
  455. list_del(&devfreq->node);
  456. mutex_unlock(&devfreq_list_lock);
  457. if (devfreq->governor)
  458. devfreq->governor->event_handler(devfreq,
  459. DEVFREQ_GOV_STOP, NULL);
  460. if (devfreq->profile->exit)
  461. devfreq->profile->exit(devfreq->dev.parent);
  462. mutex_destroy(&devfreq->lock);
  463. kfree(devfreq);
  464. }
  465. /**
  466. * devfreq_add_device() - Add devfreq feature to the device
  467. * @dev: the device to add devfreq feature.
  468. * @profile: device-specific profile to run devfreq.
  469. * @governor_name: name of the policy to choose frequency.
  470. * @data: private data for the governor. The devfreq framework does not
  471. * touch this value.
  472. */
  473. struct devfreq *devfreq_add_device(struct device *dev,
  474. struct devfreq_dev_profile *profile,
  475. const char *governor_name,
  476. void *data)
  477. {
  478. struct devfreq *devfreq;
  479. struct devfreq_governor *governor;
  480. static atomic_t devfreq_no = ATOMIC_INIT(-1);
  481. int err = 0;
  482. if (!dev || !profile || !governor_name) {
  483. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  484. return ERR_PTR(-EINVAL);
  485. }
  486. mutex_lock(&devfreq_list_lock);
  487. devfreq = find_device_devfreq(dev);
  488. mutex_unlock(&devfreq_list_lock);
  489. if (!IS_ERR(devfreq)) {
  490. dev_err(dev, "%s: Unable to create devfreq for the device.\n",
  491. __func__);
  492. err = -EINVAL;
  493. goto err_out;
  494. }
  495. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  496. if (!devfreq) {
  497. err = -ENOMEM;
  498. goto err_out;
  499. }
  500. mutex_init(&devfreq->lock);
  501. mutex_lock(&devfreq->lock);
  502. devfreq->dev.parent = dev;
  503. devfreq->dev.class = devfreq_class;
  504. devfreq->dev.release = devfreq_dev_release;
  505. devfreq->profile = profile;
  506. strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
  507. devfreq->previous_freq = profile->initial_freq;
  508. devfreq->last_status.current_frequency = profile->initial_freq;
  509. devfreq->data = data;
  510. devfreq->nb.notifier_call = devfreq_notifier_call;
  511. if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
  512. mutex_unlock(&devfreq->lock);
  513. err = set_freq_table(devfreq);
  514. if (err < 0)
  515. goto err_out;
  516. mutex_lock(&devfreq->lock);
  517. }
  518. devfreq->min_freq = find_available_min_freq(devfreq);
  519. if (!devfreq->min_freq) {
  520. mutex_unlock(&devfreq->lock);
  521. err = -EINVAL;
  522. goto err_dev;
  523. }
  524. devfreq->scaling_min_freq = devfreq->min_freq;
  525. devfreq->max_freq = find_available_max_freq(devfreq);
  526. if (!devfreq->max_freq) {
  527. mutex_unlock(&devfreq->lock);
  528. err = -EINVAL;
  529. goto err_dev;
  530. }
  531. devfreq->scaling_max_freq = devfreq->max_freq;
  532. dev_set_name(&devfreq->dev, "devfreq%d",
  533. atomic_inc_return(&devfreq_no));
  534. err = device_register(&devfreq->dev);
  535. if (err) {
  536. mutex_unlock(&devfreq->lock);
  537. goto err_dev;
  538. }
  539. devfreq->trans_table = devm_kzalloc(&devfreq->dev,
  540. sizeof(unsigned int) *
  541. devfreq->profile->max_state *
  542. devfreq->profile->max_state,
  543. GFP_KERNEL);
  544. devfreq->time_in_state = devm_kzalloc(&devfreq->dev,
  545. sizeof(unsigned long) *
  546. devfreq->profile->max_state,
  547. GFP_KERNEL);
  548. devfreq->last_stat_updated = jiffies;
  549. srcu_init_notifier_head(&devfreq->transition_notifier_list);
  550. mutex_unlock(&devfreq->lock);
  551. mutex_lock(&devfreq_list_lock);
  552. list_add(&devfreq->node, &devfreq_list);
  553. governor = find_devfreq_governor(devfreq->governor_name);
  554. if (IS_ERR(governor)) {
  555. dev_err(dev, "%s: Unable to find governor for the device\n",
  556. __func__);
  557. err = PTR_ERR(governor);
  558. goto err_init;
  559. }
  560. devfreq->governor = governor;
  561. err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
  562. NULL);
  563. if (err) {
  564. dev_err(dev, "%s: Unable to start governor for the device\n",
  565. __func__);
  566. goto err_init;
  567. }
  568. mutex_unlock(&devfreq_list_lock);
  569. return devfreq;
  570. err_init:
  571. list_del(&devfreq->node);
  572. mutex_unlock(&devfreq_list_lock);
  573. device_unregister(&devfreq->dev);
  574. err_dev:
  575. if (devfreq)
  576. kfree(devfreq);
  577. err_out:
  578. return ERR_PTR(err);
  579. }
  580. EXPORT_SYMBOL(devfreq_add_device);
  581. /**
  582. * devfreq_remove_device() - Remove devfreq feature from a device.
  583. * @devfreq: the devfreq instance to be removed
  584. *
  585. * The opposite of devfreq_add_device().
  586. */
  587. int devfreq_remove_device(struct devfreq *devfreq)
  588. {
  589. if (!devfreq)
  590. return -EINVAL;
  591. device_unregister(&devfreq->dev);
  592. return 0;
  593. }
  594. EXPORT_SYMBOL(devfreq_remove_device);
  595. static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
  596. {
  597. struct devfreq **r = res;
  598. if (WARN_ON(!r || !*r))
  599. return 0;
  600. return *r == data;
  601. }
  602. static void devm_devfreq_dev_release(struct device *dev, void *res)
  603. {
  604. devfreq_remove_device(*(struct devfreq **)res);
  605. }
  606. /**
  607. * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
  608. * @dev: the device to add devfreq feature.
  609. * @profile: device-specific profile to run devfreq.
  610. * @governor_name: name of the policy to choose frequency.
  611. * @data: private data for the governor. The devfreq framework does not
  612. * touch this value.
  613. *
  614. * This function manages automatically the memory of devfreq device using device
  615. * resource management and simplify the free operation for memory of devfreq
  616. * device.
  617. */
  618. struct devfreq *devm_devfreq_add_device(struct device *dev,
  619. struct devfreq_dev_profile *profile,
  620. const char *governor_name,
  621. void *data)
  622. {
  623. struct devfreq **ptr, *devfreq;
  624. ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
  625. if (!ptr)
  626. return ERR_PTR(-ENOMEM);
  627. devfreq = devfreq_add_device(dev, profile, governor_name, data);
  628. if (IS_ERR(devfreq)) {
  629. devres_free(ptr);
  630. return ERR_PTR(-ENOMEM);
  631. }
  632. *ptr = devfreq;
  633. devres_add(dev, ptr);
  634. return devfreq;
  635. }
  636. EXPORT_SYMBOL(devm_devfreq_add_device);
  637. #ifdef CONFIG_OF
  638. /*
  639. * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
  640. * @dev - instance to the given device
  641. * @index - index into list of devfreq
  642. *
  643. * return the instance of devfreq device
  644. */
  645. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  646. {
  647. struct device_node *node;
  648. struct devfreq *devfreq;
  649. if (!dev)
  650. return ERR_PTR(-EINVAL);
  651. if (!dev->of_node)
  652. return ERR_PTR(-EINVAL);
  653. node = of_parse_phandle(dev->of_node, "devfreq", index);
  654. if (!node)
  655. return ERR_PTR(-ENODEV);
  656. mutex_lock(&devfreq_list_lock);
  657. list_for_each_entry(devfreq, &devfreq_list, node) {
  658. if (devfreq->dev.parent
  659. && devfreq->dev.parent->of_node == node) {
  660. mutex_unlock(&devfreq_list_lock);
  661. of_node_put(node);
  662. return devfreq;
  663. }
  664. }
  665. mutex_unlock(&devfreq_list_lock);
  666. of_node_put(node);
  667. return ERR_PTR(-EPROBE_DEFER);
  668. }
  669. #else
  670. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  671. {
  672. return ERR_PTR(-ENODEV);
  673. }
  674. #endif /* CONFIG_OF */
  675. EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
  676. /**
  677. * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
  678. * @dev: the device to add devfreq feature.
  679. * @devfreq: the devfreq instance to be removed
  680. */
  681. void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
  682. {
  683. WARN_ON(devres_release(dev, devm_devfreq_dev_release,
  684. devm_devfreq_dev_match, devfreq));
  685. }
  686. EXPORT_SYMBOL(devm_devfreq_remove_device);
  687. /**
  688. * devfreq_suspend_device() - Suspend devfreq of a device.
  689. * @devfreq: the devfreq instance to be suspended
  690. *
  691. * This function is intended to be called by the pm callbacks
  692. * (e.g., runtime_suspend, suspend) of the device driver that
  693. * holds the devfreq.
  694. */
  695. int devfreq_suspend_device(struct devfreq *devfreq)
  696. {
  697. if (!devfreq)
  698. return -EINVAL;
  699. if (!devfreq->governor)
  700. return 0;
  701. return devfreq->governor->event_handler(devfreq,
  702. DEVFREQ_GOV_SUSPEND, NULL);
  703. }
  704. EXPORT_SYMBOL(devfreq_suspend_device);
  705. /**
  706. * devfreq_resume_device() - Resume devfreq of a device.
  707. * @devfreq: the devfreq instance to be resumed
  708. *
  709. * This function is intended to be called by the pm callbacks
  710. * (e.g., runtime_resume, resume) of the device driver that
  711. * holds the devfreq.
  712. */
  713. int devfreq_resume_device(struct devfreq *devfreq)
  714. {
  715. if (!devfreq)
  716. return -EINVAL;
  717. if (!devfreq->governor)
  718. return 0;
  719. return devfreq->governor->event_handler(devfreq,
  720. DEVFREQ_GOV_RESUME, NULL);
  721. }
  722. EXPORT_SYMBOL(devfreq_resume_device);
  723. /**
  724. * devfreq_add_governor() - Add devfreq governor
  725. * @governor: the devfreq governor to be added
  726. */
  727. int devfreq_add_governor(struct devfreq_governor *governor)
  728. {
  729. struct devfreq_governor *g;
  730. struct devfreq *devfreq;
  731. int err = 0;
  732. if (!governor) {
  733. pr_err("%s: Invalid parameters.\n", __func__);
  734. return -EINVAL;
  735. }
  736. mutex_lock(&devfreq_list_lock);
  737. g = find_devfreq_governor(governor->name);
  738. if (!IS_ERR(g)) {
  739. pr_err("%s: governor %s already registered\n", __func__,
  740. g->name);
  741. err = -EINVAL;
  742. goto err_out;
  743. }
  744. list_add(&governor->node, &devfreq_governor_list);
  745. list_for_each_entry(devfreq, &devfreq_list, node) {
  746. int ret = 0;
  747. struct device *dev = devfreq->dev.parent;
  748. if (!strncmp(devfreq->governor_name, governor->name,
  749. DEVFREQ_NAME_LEN)) {
  750. /* The following should never occur */
  751. if (devfreq->governor) {
  752. dev_warn(dev,
  753. "%s: Governor %s already present\n",
  754. __func__, devfreq->governor->name);
  755. ret = devfreq->governor->event_handler(devfreq,
  756. DEVFREQ_GOV_STOP, NULL);
  757. if (ret) {
  758. dev_warn(dev,
  759. "%s: Governor %s stop = %d\n",
  760. __func__,
  761. devfreq->governor->name, ret);
  762. }
  763. /* Fall through */
  764. }
  765. devfreq->governor = governor;
  766. ret = devfreq->governor->event_handler(devfreq,
  767. DEVFREQ_GOV_START, NULL);
  768. if (ret) {
  769. dev_warn(dev, "%s: Governor %s start=%d\n",
  770. __func__, devfreq->governor->name,
  771. ret);
  772. }
  773. }
  774. }
  775. err_out:
  776. mutex_unlock(&devfreq_list_lock);
  777. return err;
  778. }
  779. EXPORT_SYMBOL(devfreq_add_governor);
  780. /**
  781. * devfreq_remove_governor() - Remove devfreq feature from a device.
  782. * @governor: the devfreq governor to be removed
  783. */
  784. int devfreq_remove_governor(struct devfreq_governor *governor)
  785. {
  786. struct devfreq_governor *g;
  787. struct devfreq *devfreq;
  788. int err = 0;
  789. if (!governor) {
  790. pr_err("%s: Invalid parameters.\n", __func__);
  791. return -EINVAL;
  792. }
  793. mutex_lock(&devfreq_list_lock);
  794. g = find_devfreq_governor(governor->name);
  795. if (IS_ERR(g)) {
  796. pr_err("%s: governor %s not registered\n", __func__,
  797. governor->name);
  798. err = PTR_ERR(g);
  799. goto err_out;
  800. }
  801. list_for_each_entry(devfreq, &devfreq_list, node) {
  802. int ret;
  803. struct device *dev = devfreq->dev.parent;
  804. if (!strncmp(devfreq->governor_name, governor->name,
  805. DEVFREQ_NAME_LEN)) {
  806. /* we should have a devfreq governor! */
  807. if (!devfreq->governor) {
  808. dev_warn(dev, "%s: Governor %s NOT present\n",
  809. __func__, governor->name);
  810. continue;
  811. /* Fall through */
  812. }
  813. ret = devfreq->governor->event_handler(devfreq,
  814. DEVFREQ_GOV_STOP, NULL);
  815. if (ret) {
  816. dev_warn(dev, "%s: Governor %s stop=%d\n",
  817. __func__, devfreq->governor->name,
  818. ret);
  819. }
  820. devfreq->governor = NULL;
  821. }
  822. }
  823. list_del(&governor->node);
  824. err_out:
  825. mutex_unlock(&devfreq_list_lock);
  826. return err;
  827. }
  828. EXPORT_SYMBOL(devfreq_remove_governor);
  829. static ssize_t governor_show(struct device *dev,
  830. struct device_attribute *attr, char *buf)
  831. {
  832. if (!to_devfreq(dev)->governor)
  833. return -EINVAL;
  834. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  835. }
  836. static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
  837. const char *buf, size_t count)
  838. {
  839. struct devfreq *df = to_devfreq(dev);
  840. int ret;
  841. char str_governor[DEVFREQ_NAME_LEN + 1];
  842. struct devfreq_governor *governor;
  843. ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
  844. if (ret != 1)
  845. return -EINVAL;
  846. mutex_lock(&devfreq_list_lock);
  847. governor = find_devfreq_governor(str_governor);
  848. if (IS_ERR(governor)) {
  849. ret = PTR_ERR(governor);
  850. goto out;
  851. }
  852. if (df->governor == governor) {
  853. ret = 0;
  854. goto out;
  855. } else if (df->governor->immutable || governor->immutable) {
  856. ret = -EINVAL;
  857. goto out;
  858. }
  859. if (df->governor) {
  860. ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
  861. if (ret) {
  862. dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
  863. __func__, df->governor->name, ret);
  864. goto out;
  865. }
  866. }
  867. df->governor = governor;
  868. strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
  869. ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
  870. if (ret)
  871. dev_warn(dev, "%s: Governor %s not started(%d)\n",
  872. __func__, df->governor->name, ret);
  873. out:
  874. mutex_unlock(&devfreq_list_lock);
  875. if (!ret)
  876. ret = count;
  877. return ret;
  878. }
  879. static DEVICE_ATTR_RW(governor);
  880. static ssize_t available_governors_show(struct device *d,
  881. struct device_attribute *attr,
  882. char *buf)
  883. {
  884. struct devfreq *df = to_devfreq(d);
  885. ssize_t count = 0;
  886. mutex_lock(&devfreq_list_lock);
  887. /*
  888. * The devfreq with immutable governor (e.g., passive) shows
  889. * only own governor.
  890. */
  891. if (df->governor->immutable) {
  892. count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
  893. "%s ", df->governor_name);
  894. /*
  895. * The devfreq device shows the registered governor except for
  896. * immutable governors such as passive governor .
  897. */
  898. } else {
  899. struct devfreq_governor *governor;
  900. list_for_each_entry(governor, &devfreq_governor_list, node) {
  901. if (governor->immutable)
  902. continue;
  903. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  904. "%s ", governor->name);
  905. }
  906. }
  907. mutex_unlock(&devfreq_list_lock);
  908. /* Truncate the trailing space */
  909. if (count)
  910. count--;
  911. count += sprintf(&buf[count], "\n");
  912. return count;
  913. }
  914. static DEVICE_ATTR_RO(available_governors);
  915. static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
  916. char *buf)
  917. {
  918. unsigned long freq;
  919. struct devfreq *devfreq = to_devfreq(dev);
  920. if (devfreq->profile->get_cur_freq &&
  921. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  922. return sprintf(buf, "%lu\n", freq);
  923. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  924. }
  925. static DEVICE_ATTR_RO(cur_freq);
  926. static ssize_t target_freq_show(struct device *dev,
  927. struct device_attribute *attr, char *buf)
  928. {
  929. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  930. }
  931. static DEVICE_ATTR_RO(target_freq);
  932. static ssize_t polling_interval_show(struct device *dev,
  933. struct device_attribute *attr, char *buf)
  934. {
  935. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  936. }
  937. static ssize_t polling_interval_store(struct device *dev,
  938. struct device_attribute *attr,
  939. const char *buf, size_t count)
  940. {
  941. struct devfreq *df = to_devfreq(dev);
  942. unsigned int value;
  943. int ret;
  944. if (!df->governor)
  945. return -EINVAL;
  946. ret = sscanf(buf, "%u", &value);
  947. if (ret != 1)
  948. return -EINVAL;
  949. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  950. ret = count;
  951. return ret;
  952. }
  953. static DEVICE_ATTR_RW(polling_interval);
  954. static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
  955. const char *buf, size_t count)
  956. {
  957. struct devfreq *df = to_devfreq(dev);
  958. unsigned long value;
  959. int ret;
  960. unsigned long max;
  961. ret = sscanf(buf, "%lu", &value);
  962. if (ret != 1)
  963. return -EINVAL;
  964. mutex_lock(&df->lock);
  965. max = df->max_freq;
  966. if (value && max && value > max) {
  967. ret = -EINVAL;
  968. goto unlock;
  969. }
  970. df->min_freq = value;
  971. update_devfreq(df);
  972. ret = count;
  973. unlock:
  974. mutex_unlock(&df->lock);
  975. return ret;
  976. }
  977. static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
  978. char *buf)
  979. {
  980. struct devfreq *df = to_devfreq(dev);
  981. return sprintf(buf, "%lu\n", MAX(df->scaling_min_freq, df->min_freq));
  982. }
  983. static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
  984. const char *buf, size_t count)
  985. {
  986. struct devfreq *df = to_devfreq(dev);
  987. unsigned long value;
  988. int ret;
  989. unsigned long min;
  990. ret = sscanf(buf, "%lu", &value);
  991. if (ret != 1)
  992. return -EINVAL;
  993. mutex_lock(&df->lock);
  994. min = df->min_freq;
  995. if (value && min && value < min) {
  996. ret = -EINVAL;
  997. goto unlock;
  998. }
  999. df->max_freq = value;
  1000. update_devfreq(df);
  1001. ret = count;
  1002. unlock:
  1003. mutex_unlock(&df->lock);
  1004. return ret;
  1005. }
  1006. static DEVICE_ATTR_RW(min_freq);
  1007. static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
  1008. char *buf)
  1009. {
  1010. struct devfreq *df = to_devfreq(dev);
  1011. return sprintf(buf, "%lu\n", MIN(df->scaling_max_freq, df->max_freq));
  1012. }
  1013. static DEVICE_ATTR_RW(max_freq);
  1014. static ssize_t available_frequencies_show(struct device *d,
  1015. struct device_attribute *attr,
  1016. char *buf)
  1017. {
  1018. struct devfreq *df = to_devfreq(d);
  1019. ssize_t count = 0;
  1020. int i;
  1021. mutex_lock(&df->lock);
  1022. for (i = 0; i < df->profile->max_state; i++)
  1023. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  1024. "%lu ", df->profile->freq_table[i]);
  1025. mutex_unlock(&df->lock);
  1026. /* Truncate the trailing space */
  1027. if (count)
  1028. count--;
  1029. count += sprintf(&buf[count], "\n");
  1030. return count;
  1031. }
  1032. static DEVICE_ATTR_RO(available_frequencies);
  1033. static ssize_t trans_stat_show(struct device *dev,
  1034. struct device_attribute *attr, char *buf)
  1035. {
  1036. struct devfreq *devfreq = to_devfreq(dev);
  1037. ssize_t len;
  1038. int i, j;
  1039. unsigned int max_state = devfreq->profile->max_state;
  1040. if (!devfreq->stop_polling &&
  1041. devfreq_update_status(devfreq, devfreq->previous_freq))
  1042. return 0;
  1043. if (max_state == 0)
  1044. return sprintf(buf, "Not Supported.\n");
  1045. len = sprintf(buf, " From : To\n");
  1046. len += sprintf(buf + len, " :");
  1047. for (i = 0; i < max_state; i++)
  1048. len += sprintf(buf + len, "%10lu",
  1049. devfreq->profile->freq_table[i]);
  1050. len += sprintf(buf + len, " time(ms)\n");
  1051. for (i = 0; i < max_state; i++) {
  1052. if (devfreq->profile->freq_table[i]
  1053. == devfreq->previous_freq) {
  1054. len += sprintf(buf + len, "*");
  1055. } else {
  1056. len += sprintf(buf + len, " ");
  1057. }
  1058. len += sprintf(buf + len, "%10lu:",
  1059. devfreq->profile->freq_table[i]);
  1060. for (j = 0; j < max_state; j++)
  1061. len += sprintf(buf + len, "%10u",
  1062. devfreq->trans_table[(i * max_state) + j]);
  1063. len += sprintf(buf + len, "%10u\n",
  1064. jiffies_to_msecs(devfreq->time_in_state[i]));
  1065. }
  1066. len += sprintf(buf + len, "Total transition : %u\n",
  1067. devfreq->total_trans);
  1068. return len;
  1069. }
  1070. static DEVICE_ATTR_RO(trans_stat);
  1071. static struct attribute *devfreq_attrs[] = {
  1072. &dev_attr_governor.attr,
  1073. &dev_attr_available_governors.attr,
  1074. &dev_attr_cur_freq.attr,
  1075. &dev_attr_available_frequencies.attr,
  1076. &dev_attr_target_freq.attr,
  1077. &dev_attr_polling_interval.attr,
  1078. &dev_attr_min_freq.attr,
  1079. &dev_attr_max_freq.attr,
  1080. &dev_attr_trans_stat.attr,
  1081. NULL,
  1082. };
  1083. ATTRIBUTE_GROUPS(devfreq);
  1084. static int __init devfreq_init(void)
  1085. {
  1086. devfreq_class = class_create(THIS_MODULE, "devfreq");
  1087. if (IS_ERR(devfreq_class)) {
  1088. pr_err("%s: couldn't create class\n", __FILE__);
  1089. return PTR_ERR(devfreq_class);
  1090. }
  1091. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  1092. if (!devfreq_wq) {
  1093. class_destroy(devfreq_class);
  1094. pr_err("%s: couldn't create workqueue\n", __FILE__);
  1095. return -ENOMEM;
  1096. }
  1097. devfreq_class->dev_groups = devfreq_groups;
  1098. return 0;
  1099. }
  1100. subsys_initcall(devfreq_init);
  1101. /*
  1102. * The following are helper functions for devfreq user device drivers with
  1103. * OPP framework.
  1104. */
  1105. /**
  1106. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  1107. * freq value given to target callback.
  1108. * @dev: The devfreq user device. (parent of devfreq)
  1109. * @freq: The frequency given to target function
  1110. * @flags: Flags handed from devfreq framework.
  1111. *
  1112. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  1113. * use.
  1114. */
  1115. struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  1116. unsigned long *freq,
  1117. u32 flags)
  1118. {
  1119. struct dev_pm_opp *opp;
  1120. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  1121. /* The freq is an upper bound. opp should be lower */
  1122. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1123. /* If not available, use the closest opp */
  1124. if (opp == ERR_PTR(-ERANGE))
  1125. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1126. } else {
  1127. /* The freq is an lower bound. opp should be higher */
  1128. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1129. /* If not available, use the closest opp */
  1130. if (opp == ERR_PTR(-ERANGE))
  1131. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1132. }
  1133. return opp;
  1134. }
  1135. EXPORT_SYMBOL(devfreq_recommended_opp);
  1136. /**
  1137. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  1138. * for any changes in the OPP availability
  1139. * changes
  1140. * @dev: The devfreq user device. (parent of devfreq)
  1141. * @devfreq: The devfreq object.
  1142. */
  1143. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1144. {
  1145. return dev_pm_opp_register_notifier(dev, &devfreq->nb);
  1146. }
  1147. EXPORT_SYMBOL(devfreq_register_opp_notifier);
  1148. /**
  1149. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  1150. * notified for any changes in the OPP
  1151. * availability changes anymore.
  1152. * @dev: The devfreq user device. (parent of devfreq)
  1153. * @devfreq: The devfreq object.
  1154. *
  1155. * At exit() callback of devfreq_dev_profile, this must be included if
  1156. * devfreq_recommended_opp is used.
  1157. */
  1158. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1159. {
  1160. return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
  1161. }
  1162. EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
  1163. static void devm_devfreq_opp_release(struct device *dev, void *res)
  1164. {
  1165. devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
  1166. }
  1167. /**
  1168. * devm_ devfreq_register_opp_notifier()
  1169. * - Resource-managed devfreq_register_opp_notifier()
  1170. * @dev: The devfreq user device. (parent of devfreq)
  1171. * @devfreq: The devfreq object.
  1172. */
  1173. int devm_devfreq_register_opp_notifier(struct device *dev,
  1174. struct devfreq *devfreq)
  1175. {
  1176. struct devfreq **ptr;
  1177. int ret;
  1178. ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
  1179. if (!ptr)
  1180. return -ENOMEM;
  1181. ret = devfreq_register_opp_notifier(dev, devfreq);
  1182. if (ret) {
  1183. devres_free(ptr);
  1184. return ret;
  1185. }
  1186. *ptr = devfreq;
  1187. devres_add(dev, ptr);
  1188. return 0;
  1189. }
  1190. EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
  1191. /**
  1192. * devm_devfreq_unregister_opp_notifier()
  1193. * - Resource-managed devfreq_unregister_opp_notifier()
  1194. * @dev: The devfreq user device. (parent of devfreq)
  1195. * @devfreq: The devfreq object.
  1196. */
  1197. void devm_devfreq_unregister_opp_notifier(struct device *dev,
  1198. struct devfreq *devfreq)
  1199. {
  1200. WARN_ON(devres_release(dev, devm_devfreq_opp_release,
  1201. devm_devfreq_dev_match, devfreq));
  1202. }
  1203. EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
  1204. /**
  1205. * devfreq_register_notifier() - Register a driver with devfreq
  1206. * @devfreq: The devfreq object.
  1207. * @nb: The notifier block to register.
  1208. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1209. */
  1210. int devfreq_register_notifier(struct devfreq *devfreq,
  1211. struct notifier_block *nb,
  1212. unsigned int list)
  1213. {
  1214. int ret = 0;
  1215. if (!devfreq)
  1216. return -EINVAL;
  1217. switch (list) {
  1218. case DEVFREQ_TRANSITION_NOTIFIER:
  1219. ret = srcu_notifier_chain_register(
  1220. &devfreq->transition_notifier_list, nb);
  1221. break;
  1222. default:
  1223. ret = -EINVAL;
  1224. }
  1225. return ret;
  1226. }
  1227. EXPORT_SYMBOL(devfreq_register_notifier);
  1228. /*
  1229. * devfreq_unregister_notifier() - Unregister a driver with devfreq
  1230. * @devfreq: The devfreq object.
  1231. * @nb: The notifier block to be unregistered.
  1232. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1233. */
  1234. int devfreq_unregister_notifier(struct devfreq *devfreq,
  1235. struct notifier_block *nb,
  1236. unsigned int list)
  1237. {
  1238. int ret = 0;
  1239. if (!devfreq)
  1240. return -EINVAL;
  1241. switch (list) {
  1242. case DEVFREQ_TRANSITION_NOTIFIER:
  1243. ret = srcu_notifier_chain_unregister(
  1244. &devfreq->transition_notifier_list, nb);
  1245. break;
  1246. default:
  1247. ret = -EINVAL;
  1248. }
  1249. return ret;
  1250. }
  1251. EXPORT_SYMBOL(devfreq_unregister_notifier);
  1252. struct devfreq_notifier_devres {
  1253. struct devfreq *devfreq;
  1254. struct notifier_block *nb;
  1255. unsigned int list;
  1256. };
  1257. static void devm_devfreq_notifier_release(struct device *dev, void *res)
  1258. {
  1259. struct devfreq_notifier_devres *this = res;
  1260. devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
  1261. }
  1262. /**
  1263. * devm_devfreq_register_notifier()
  1264. - Resource-managed devfreq_register_notifier()
  1265. * @dev: The devfreq user device. (parent of devfreq)
  1266. * @devfreq: The devfreq object.
  1267. * @nb: The notifier block to be unregistered.
  1268. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1269. */
  1270. int devm_devfreq_register_notifier(struct device *dev,
  1271. struct devfreq *devfreq,
  1272. struct notifier_block *nb,
  1273. unsigned int list)
  1274. {
  1275. struct devfreq_notifier_devres *ptr;
  1276. int ret;
  1277. ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
  1278. GFP_KERNEL);
  1279. if (!ptr)
  1280. return -ENOMEM;
  1281. ret = devfreq_register_notifier(devfreq, nb, list);
  1282. if (ret) {
  1283. devres_free(ptr);
  1284. return ret;
  1285. }
  1286. ptr->devfreq = devfreq;
  1287. ptr->nb = nb;
  1288. ptr->list = list;
  1289. devres_add(dev, ptr);
  1290. return 0;
  1291. }
  1292. EXPORT_SYMBOL(devm_devfreq_register_notifier);
  1293. /**
  1294. * devm_devfreq_unregister_notifier()
  1295. - Resource-managed devfreq_unregister_notifier()
  1296. * @dev: The devfreq user device. (parent of devfreq)
  1297. * @devfreq: The devfreq object.
  1298. * @nb: The notifier block to be unregistered.
  1299. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1300. */
  1301. void devm_devfreq_unregister_notifier(struct device *dev,
  1302. struct devfreq *devfreq,
  1303. struct notifier_block *nb,
  1304. unsigned int list)
  1305. {
  1306. WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
  1307. devm_devfreq_dev_match, devfreq));
  1308. }
  1309. EXPORT_SYMBOL(devm_devfreq_unregister_notifier);