devfreq.c 40 KB

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