devfreq.c 38 KB

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