devfreq.c 29 KB

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