devfreq.c 28 KB

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