devfreq.c 31 KB

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