devfreq.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 (unlikely(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 (unlikely(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 freuqency with user freq and QoS.
  153. *
  154. * List from the highest proiority
  155. * max_freq (probably called by thermal when it's too hot)
  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. * @skip: skip calling device_unregister().
  341. */
  342. static void _remove_devfreq(struct devfreq *devfreq, bool skip)
  343. {
  344. mutex_lock(&devfreq_list_lock);
  345. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  346. mutex_unlock(&devfreq_list_lock);
  347. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  348. return;
  349. }
  350. list_del(&devfreq->node);
  351. mutex_unlock(&devfreq_list_lock);
  352. if (devfreq->governor)
  353. devfreq->governor->event_handler(devfreq,
  354. DEVFREQ_GOV_STOP, NULL);
  355. if (devfreq->profile->exit)
  356. devfreq->profile->exit(devfreq->dev.parent);
  357. if (!skip && get_device(&devfreq->dev)) {
  358. device_unregister(&devfreq->dev);
  359. put_device(&devfreq->dev);
  360. }
  361. mutex_destroy(&devfreq->lock);
  362. kfree(devfreq);
  363. }
  364. /**
  365. * devfreq_dev_release() - Callback for struct device to release the device.
  366. * @dev: the devfreq device
  367. *
  368. * This calls _remove_devfreq() if _remove_devfreq() is not called.
  369. * Note that devfreq_dev_release() could be called by _remove_devfreq() as
  370. * well as by others unregistering the device.
  371. */
  372. static void devfreq_dev_release(struct device *dev)
  373. {
  374. struct devfreq *devfreq = to_devfreq(dev);
  375. _remove_devfreq(devfreq, true);
  376. }
  377. /**
  378. * devfreq_add_device() - Add devfreq feature to the device
  379. * @dev: the device to add devfreq feature.
  380. * @profile: device-specific profile to run devfreq.
  381. * @governor_name: name of the policy to choose frequency.
  382. * @data: private data for the governor. The devfreq framework does not
  383. * touch this value.
  384. */
  385. struct devfreq *devfreq_add_device(struct device *dev,
  386. struct devfreq_dev_profile *profile,
  387. const char *governor_name,
  388. void *data)
  389. {
  390. struct devfreq *devfreq;
  391. struct devfreq_governor *governor;
  392. int err = 0;
  393. if (!dev || !profile || !governor_name) {
  394. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  395. return ERR_PTR(-EINVAL);
  396. }
  397. mutex_lock(&devfreq_list_lock);
  398. devfreq = find_device_devfreq(dev);
  399. mutex_unlock(&devfreq_list_lock);
  400. if (!IS_ERR(devfreq)) {
  401. dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
  402. err = -EINVAL;
  403. goto err_out;
  404. }
  405. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  406. if (!devfreq) {
  407. dev_err(dev, "%s: Unable to create devfreq for the device\n",
  408. __func__);
  409. err = -ENOMEM;
  410. goto err_out;
  411. }
  412. mutex_init(&devfreq->lock);
  413. mutex_lock(&devfreq->lock);
  414. devfreq->dev.parent = dev;
  415. devfreq->dev.class = devfreq_class;
  416. devfreq->dev.release = devfreq_dev_release;
  417. devfreq->profile = profile;
  418. strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
  419. devfreq->previous_freq = profile->initial_freq;
  420. devfreq->data = data;
  421. devfreq->nb.notifier_call = devfreq_notifier_call;
  422. devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
  423. devfreq->profile->max_state *
  424. devfreq->profile->max_state,
  425. GFP_KERNEL);
  426. devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) *
  427. devfreq->profile->max_state,
  428. GFP_KERNEL);
  429. devfreq->last_stat_updated = jiffies;
  430. dev_set_name(&devfreq->dev, "%s", dev_name(dev));
  431. err = device_register(&devfreq->dev);
  432. if (err) {
  433. put_device(&devfreq->dev);
  434. mutex_unlock(&devfreq->lock);
  435. goto err_dev;
  436. }
  437. mutex_unlock(&devfreq->lock);
  438. mutex_lock(&devfreq_list_lock);
  439. list_add(&devfreq->node, &devfreq_list);
  440. governor = find_devfreq_governor(devfreq->governor_name);
  441. if (!IS_ERR(governor))
  442. devfreq->governor = governor;
  443. if (devfreq->governor)
  444. err = devfreq->governor->event_handler(devfreq,
  445. DEVFREQ_GOV_START, NULL);
  446. mutex_unlock(&devfreq_list_lock);
  447. if (err) {
  448. dev_err(dev, "%s: Unable to start governor for the device\n",
  449. __func__);
  450. goto err_init;
  451. }
  452. return devfreq;
  453. err_init:
  454. list_del(&devfreq->node);
  455. device_unregister(&devfreq->dev);
  456. err_dev:
  457. kfree(devfreq);
  458. err_out:
  459. return ERR_PTR(err);
  460. }
  461. EXPORT_SYMBOL(devfreq_add_device);
  462. /**
  463. * devfreq_remove_device() - Remove devfreq feature from a device.
  464. * @devfreq: the devfreq instance to be removed
  465. *
  466. * The opposite of devfreq_add_device().
  467. */
  468. int devfreq_remove_device(struct devfreq *devfreq)
  469. {
  470. if (!devfreq)
  471. return -EINVAL;
  472. _remove_devfreq(devfreq, false);
  473. return 0;
  474. }
  475. EXPORT_SYMBOL(devfreq_remove_device);
  476. /**
  477. * devfreq_suspend_device() - Suspend devfreq of a device.
  478. * @devfreq: the devfreq instance to be suspended
  479. *
  480. * This function is intended to be called by the pm callbacks
  481. * (e.g., runtime_suspend, suspend) of the device driver that
  482. * holds the devfreq.
  483. */
  484. int devfreq_suspend_device(struct devfreq *devfreq)
  485. {
  486. if (!devfreq)
  487. return -EINVAL;
  488. if (!devfreq->governor)
  489. return 0;
  490. return devfreq->governor->event_handler(devfreq,
  491. DEVFREQ_GOV_SUSPEND, NULL);
  492. }
  493. EXPORT_SYMBOL(devfreq_suspend_device);
  494. /**
  495. * devfreq_resume_device() - Resume devfreq of a device.
  496. * @devfreq: the devfreq instance to be resumed
  497. *
  498. * This function is intended to be called by the pm callbacks
  499. * (e.g., runtime_resume, resume) of the device driver that
  500. * holds the devfreq.
  501. */
  502. int devfreq_resume_device(struct devfreq *devfreq)
  503. {
  504. if (!devfreq)
  505. return -EINVAL;
  506. if (!devfreq->governor)
  507. return 0;
  508. return devfreq->governor->event_handler(devfreq,
  509. DEVFREQ_GOV_RESUME, NULL);
  510. }
  511. EXPORT_SYMBOL(devfreq_resume_device);
  512. /**
  513. * devfreq_add_governor() - Add devfreq governor
  514. * @governor: the devfreq governor to be added
  515. */
  516. int devfreq_add_governor(struct devfreq_governor *governor)
  517. {
  518. struct devfreq_governor *g;
  519. struct devfreq *devfreq;
  520. int err = 0;
  521. if (!governor) {
  522. pr_err("%s: Invalid parameters.\n", __func__);
  523. return -EINVAL;
  524. }
  525. mutex_lock(&devfreq_list_lock);
  526. g = find_devfreq_governor(governor->name);
  527. if (!IS_ERR(g)) {
  528. pr_err("%s: governor %s already registered\n", __func__,
  529. g->name);
  530. err = -EINVAL;
  531. goto err_out;
  532. }
  533. list_add(&governor->node, &devfreq_governor_list);
  534. list_for_each_entry(devfreq, &devfreq_list, node) {
  535. int ret = 0;
  536. struct device *dev = devfreq->dev.parent;
  537. if (!strncmp(devfreq->governor_name, governor->name,
  538. DEVFREQ_NAME_LEN)) {
  539. /* The following should never occur */
  540. if (devfreq->governor) {
  541. dev_warn(dev,
  542. "%s: Governor %s already present\n",
  543. __func__, devfreq->governor->name);
  544. ret = devfreq->governor->event_handler(devfreq,
  545. DEVFREQ_GOV_STOP, NULL);
  546. if (ret) {
  547. dev_warn(dev,
  548. "%s: Governor %s stop = %d\n",
  549. __func__,
  550. devfreq->governor->name, ret);
  551. }
  552. /* Fall through */
  553. }
  554. devfreq->governor = governor;
  555. ret = devfreq->governor->event_handler(devfreq,
  556. DEVFREQ_GOV_START, NULL);
  557. if (ret) {
  558. dev_warn(dev, "%s: Governor %s start=%d\n",
  559. __func__, devfreq->governor->name,
  560. ret);
  561. }
  562. }
  563. }
  564. err_out:
  565. mutex_unlock(&devfreq_list_lock);
  566. return err;
  567. }
  568. EXPORT_SYMBOL(devfreq_add_governor);
  569. /**
  570. * devfreq_remove_device() - Remove devfreq feature from a device.
  571. * @governor: the devfreq governor to be removed
  572. */
  573. int devfreq_remove_governor(struct devfreq_governor *governor)
  574. {
  575. struct devfreq_governor *g;
  576. struct devfreq *devfreq;
  577. int err = 0;
  578. if (!governor) {
  579. pr_err("%s: Invalid parameters.\n", __func__);
  580. return -EINVAL;
  581. }
  582. mutex_lock(&devfreq_list_lock);
  583. g = find_devfreq_governor(governor->name);
  584. if (IS_ERR(g)) {
  585. pr_err("%s: governor %s not registered\n", __func__,
  586. governor->name);
  587. err = PTR_ERR(g);
  588. goto err_out;
  589. }
  590. list_for_each_entry(devfreq, &devfreq_list, node) {
  591. int ret;
  592. struct device *dev = devfreq->dev.parent;
  593. if (!strncmp(devfreq->governor_name, governor->name,
  594. DEVFREQ_NAME_LEN)) {
  595. /* we should have a devfreq governor! */
  596. if (!devfreq->governor) {
  597. dev_warn(dev, "%s: Governor %s NOT present\n",
  598. __func__, governor->name);
  599. continue;
  600. /* Fall through */
  601. }
  602. ret = devfreq->governor->event_handler(devfreq,
  603. DEVFREQ_GOV_STOP, NULL);
  604. if (ret) {
  605. dev_warn(dev, "%s: Governor %s stop=%d\n",
  606. __func__, devfreq->governor->name,
  607. ret);
  608. }
  609. devfreq->governor = NULL;
  610. }
  611. }
  612. list_del(&governor->node);
  613. err_out:
  614. mutex_unlock(&devfreq_list_lock);
  615. return err;
  616. }
  617. EXPORT_SYMBOL(devfreq_remove_governor);
  618. static ssize_t governor_show(struct device *dev,
  619. struct device_attribute *attr, char *buf)
  620. {
  621. if (!to_devfreq(dev)->governor)
  622. return -EINVAL;
  623. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  624. }
  625. static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
  626. const char *buf, size_t count)
  627. {
  628. struct devfreq *df = to_devfreq(dev);
  629. int ret;
  630. char str_governor[DEVFREQ_NAME_LEN + 1];
  631. struct devfreq_governor *governor;
  632. ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
  633. if (ret != 1)
  634. return -EINVAL;
  635. mutex_lock(&devfreq_list_lock);
  636. governor = find_devfreq_governor(str_governor);
  637. if (IS_ERR(governor)) {
  638. ret = PTR_ERR(governor);
  639. goto out;
  640. }
  641. if (df->governor == governor)
  642. goto out;
  643. if (df->governor) {
  644. ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
  645. if (ret) {
  646. dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
  647. __func__, df->governor->name, ret);
  648. goto out;
  649. }
  650. }
  651. df->governor = governor;
  652. strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
  653. ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
  654. if (ret)
  655. dev_warn(dev, "%s: Governor %s not started(%d)\n",
  656. __func__, df->governor->name, ret);
  657. out:
  658. mutex_unlock(&devfreq_list_lock);
  659. if (!ret)
  660. ret = count;
  661. return ret;
  662. }
  663. static DEVICE_ATTR_RW(governor);
  664. static ssize_t available_governors_show(struct device *d,
  665. struct device_attribute *attr,
  666. char *buf)
  667. {
  668. struct devfreq_governor *tmp_governor;
  669. ssize_t count = 0;
  670. mutex_lock(&devfreq_list_lock);
  671. list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
  672. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  673. "%s ", tmp_governor->name);
  674. mutex_unlock(&devfreq_list_lock);
  675. /* Truncate the trailing space */
  676. if (count)
  677. count--;
  678. count += sprintf(&buf[count], "\n");
  679. return count;
  680. }
  681. static DEVICE_ATTR_RO(available_governors);
  682. static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
  683. char *buf)
  684. {
  685. unsigned long freq;
  686. struct devfreq *devfreq = to_devfreq(dev);
  687. if (devfreq->profile->get_cur_freq &&
  688. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  689. return sprintf(buf, "%lu\n", freq);
  690. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  691. }
  692. static DEVICE_ATTR_RO(cur_freq);
  693. static ssize_t target_freq_show(struct device *dev,
  694. struct device_attribute *attr, char *buf)
  695. {
  696. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  697. }
  698. static DEVICE_ATTR_RO(target_freq);
  699. static ssize_t polling_interval_show(struct device *dev,
  700. struct device_attribute *attr, char *buf)
  701. {
  702. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  703. }
  704. static ssize_t polling_interval_store(struct device *dev,
  705. struct device_attribute *attr,
  706. const char *buf, size_t count)
  707. {
  708. struct devfreq *df = to_devfreq(dev);
  709. unsigned int value;
  710. int ret;
  711. if (!df->governor)
  712. return -EINVAL;
  713. ret = sscanf(buf, "%u", &value);
  714. if (ret != 1)
  715. return -EINVAL;
  716. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  717. ret = count;
  718. return ret;
  719. }
  720. static DEVICE_ATTR_RW(polling_interval);
  721. static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
  722. const char *buf, size_t count)
  723. {
  724. struct devfreq *df = to_devfreq(dev);
  725. unsigned long value;
  726. int ret;
  727. unsigned long max;
  728. ret = sscanf(buf, "%lu", &value);
  729. if (ret != 1)
  730. return -EINVAL;
  731. mutex_lock(&df->lock);
  732. max = df->max_freq;
  733. if (value && max && value > max) {
  734. ret = -EINVAL;
  735. goto unlock;
  736. }
  737. df->min_freq = value;
  738. update_devfreq(df);
  739. ret = count;
  740. unlock:
  741. mutex_unlock(&df->lock);
  742. return ret;
  743. }
  744. static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
  745. char *buf)
  746. {
  747. return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
  748. }
  749. static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
  750. const char *buf, size_t count)
  751. {
  752. struct devfreq *df = to_devfreq(dev);
  753. unsigned long value;
  754. int ret;
  755. unsigned long min;
  756. ret = sscanf(buf, "%lu", &value);
  757. if (ret != 1)
  758. return -EINVAL;
  759. mutex_lock(&df->lock);
  760. min = df->min_freq;
  761. if (value && min && value < min) {
  762. ret = -EINVAL;
  763. goto unlock;
  764. }
  765. df->max_freq = value;
  766. update_devfreq(df);
  767. ret = count;
  768. unlock:
  769. mutex_unlock(&df->lock);
  770. return ret;
  771. }
  772. static DEVICE_ATTR_RW(min_freq);
  773. static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
  774. char *buf)
  775. {
  776. return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
  777. }
  778. static DEVICE_ATTR_RW(max_freq);
  779. static ssize_t available_frequencies_show(struct device *d,
  780. struct device_attribute *attr,
  781. char *buf)
  782. {
  783. struct devfreq *df = to_devfreq(d);
  784. struct device *dev = df->dev.parent;
  785. struct dev_pm_opp *opp;
  786. ssize_t count = 0;
  787. unsigned long freq = 0;
  788. rcu_read_lock();
  789. do {
  790. opp = dev_pm_opp_find_freq_ceil(dev, &freq);
  791. if (IS_ERR(opp))
  792. break;
  793. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  794. "%lu ", freq);
  795. freq++;
  796. } while (1);
  797. rcu_read_unlock();
  798. /* Truncate the trailing space */
  799. if (count)
  800. count--;
  801. count += sprintf(&buf[count], "\n");
  802. return count;
  803. }
  804. static DEVICE_ATTR_RO(available_frequencies);
  805. static ssize_t trans_stat_show(struct device *dev,
  806. struct device_attribute *attr, char *buf)
  807. {
  808. struct devfreq *devfreq = to_devfreq(dev);
  809. ssize_t len;
  810. int i, j;
  811. unsigned int max_state = devfreq->profile->max_state;
  812. if (!devfreq->stop_polling &&
  813. devfreq_update_status(devfreq, devfreq->previous_freq))
  814. return 0;
  815. len = sprintf(buf, " From : To\n");
  816. len += sprintf(buf + len, " :");
  817. for (i = 0; i < max_state; i++)
  818. len += sprintf(buf + len, "%8u",
  819. devfreq->profile->freq_table[i]);
  820. len += sprintf(buf + len, " time(ms)\n");
  821. for (i = 0; i < max_state; i++) {
  822. if (devfreq->profile->freq_table[i]
  823. == devfreq->previous_freq) {
  824. len += sprintf(buf + len, "*");
  825. } else {
  826. len += sprintf(buf + len, " ");
  827. }
  828. len += sprintf(buf + len, "%8u:",
  829. devfreq->profile->freq_table[i]);
  830. for (j = 0; j < max_state; j++)
  831. len += sprintf(buf + len, "%8u",
  832. devfreq->trans_table[(i * max_state) + j]);
  833. len += sprintf(buf + len, "%10u\n",
  834. jiffies_to_msecs(devfreq->time_in_state[i]));
  835. }
  836. len += sprintf(buf + len, "Total transition : %u\n",
  837. devfreq->total_trans);
  838. return len;
  839. }
  840. static DEVICE_ATTR_RO(trans_stat);
  841. static struct attribute *devfreq_attrs[] = {
  842. &dev_attr_governor.attr,
  843. &dev_attr_available_governors.attr,
  844. &dev_attr_cur_freq.attr,
  845. &dev_attr_available_frequencies.attr,
  846. &dev_attr_target_freq.attr,
  847. &dev_attr_polling_interval.attr,
  848. &dev_attr_min_freq.attr,
  849. &dev_attr_max_freq.attr,
  850. &dev_attr_trans_stat.attr,
  851. NULL,
  852. };
  853. ATTRIBUTE_GROUPS(devfreq);
  854. static int __init devfreq_init(void)
  855. {
  856. devfreq_class = class_create(THIS_MODULE, "devfreq");
  857. if (IS_ERR(devfreq_class)) {
  858. pr_err("%s: couldn't create class\n", __FILE__);
  859. return PTR_ERR(devfreq_class);
  860. }
  861. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  862. if (!devfreq_wq) {
  863. class_destroy(devfreq_class);
  864. pr_err("%s: couldn't create workqueue\n", __FILE__);
  865. return -ENOMEM;
  866. }
  867. devfreq_class->dev_groups = devfreq_groups;
  868. return 0;
  869. }
  870. subsys_initcall(devfreq_init);
  871. static void __exit devfreq_exit(void)
  872. {
  873. class_destroy(devfreq_class);
  874. destroy_workqueue(devfreq_wq);
  875. }
  876. module_exit(devfreq_exit);
  877. /*
  878. * The followings are helper functions for devfreq user device drivers with
  879. * OPP framework.
  880. */
  881. /**
  882. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  883. * freq value given to target callback.
  884. * @dev: The devfreq user device. (parent of devfreq)
  885. * @freq: The frequency given to target function
  886. * @flags: Flags handed from devfreq framework.
  887. *
  888. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  889. * protected pointer. The reason for the same is that the opp pointer which is
  890. * returned will remain valid for use with opp_get_{voltage, freq} only while
  891. * under the locked area. The pointer returned must be used prior to unlocking
  892. * with rcu_read_unlock() to maintain the integrity of the pointer.
  893. */
  894. struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  895. unsigned long *freq,
  896. u32 flags)
  897. {
  898. struct dev_pm_opp *opp;
  899. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  900. /* The freq is an upper bound. opp should be lower */
  901. opp = dev_pm_opp_find_freq_floor(dev, freq);
  902. /* If not available, use the closest opp */
  903. if (opp == ERR_PTR(-ERANGE))
  904. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  905. } else {
  906. /* The freq is an lower bound. opp should be higher */
  907. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  908. /* If not available, use the closest opp */
  909. if (opp == ERR_PTR(-ERANGE))
  910. opp = dev_pm_opp_find_freq_floor(dev, freq);
  911. }
  912. return opp;
  913. }
  914. /**
  915. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  916. * for any changes in the OPP availability
  917. * changes
  918. * @dev: The devfreq user device. (parent of devfreq)
  919. * @devfreq: The devfreq object.
  920. */
  921. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  922. {
  923. struct srcu_notifier_head *nh;
  924. int ret = 0;
  925. rcu_read_lock();
  926. nh = dev_pm_opp_get_notifier(dev);
  927. if (IS_ERR(nh))
  928. ret = PTR_ERR(nh);
  929. rcu_read_unlock();
  930. if (!ret)
  931. ret = srcu_notifier_chain_register(nh, &devfreq->nb);
  932. return ret;
  933. }
  934. /**
  935. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  936. * notified for any changes in the OPP
  937. * availability changes anymore.
  938. * @dev: The devfreq user device. (parent of devfreq)
  939. * @devfreq: The devfreq object.
  940. *
  941. * At exit() callback of devfreq_dev_profile, this must be included if
  942. * devfreq_recommended_opp is used.
  943. */
  944. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  945. {
  946. struct srcu_notifier_head *nh;
  947. int ret = 0;
  948. rcu_read_lock();
  949. nh = dev_pm_opp_get_notifier(dev);
  950. if (IS_ERR(nh))
  951. ret = PTR_ERR(nh);
  952. rcu_read_unlock();
  953. if (!ret)
  954. ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
  955. return ret;
  956. }
  957. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  958. MODULE_DESCRIPTION("devfreq class support");
  959. MODULE_LICENSE("GPL");