devfreq.c 37 KB

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