devfreq.c 28 KB

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