devfreq.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. /**
  206. * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
  207. * @devfreq: the devfreq instance.
  208. *
  209. * Helper function to stop devfreq device load monitoing. Function
  210. * to be called from governor in response to DEVFREQ_GOV_STOP
  211. * event when device is removed from devfreq framework.
  212. */
  213. void devfreq_monitor_stop(struct devfreq *devfreq)
  214. {
  215. cancel_delayed_work_sync(&devfreq->work);
  216. }
  217. /**
  218. * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
  219. * @devfreq: the devfreq instance.
  220. *
  221. * Helper function to suspend devfreq device load monitoing. Function
  222. * to be called from governor in response to DEVFREQ_GOV_SUSPEND
  223. * event or when polling interval is set to zero.
  224. *
  225. * Note: Though this function is same as devfreq_monitor_stop(),
  226. * intentionally kept separate to provide hooks for collecting
  227. * transition statistics.
  228. */
  229. void devfreq_monitor_suspend(struct devfreq *devfreq)
  230. {
  231. mutex_lock(&devfreq->lock);
  232. if (devfreq->stop_polling) {
  233. mutex_unlock(&devfreq->lock);
  234. return;
  235. }
  236. devfreq->stop_polling = true;
  237. mutex_unlock(&devfreq->lock);
  238. cancel_delayed_work_sync(&devfreq->work);
  239. }
  240. /**
  241. * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
  242. * @devfreq: the devfreq instance.
  243. *
  244. * Helper function to resume devfreq device load monitoing. Function
  245. * to be called from governor in response to DEVFREQ_GOV_RESUME
  246. * event or when polling interval is set to non-zero.
  247. */
  248. void devfreq_monitor_resume(struct devfreq *devfreq)
  249. {
  250. mutex_lock(&devfreq->lock);
  251. if (!devfreq->stop_polling)
  252. goto out;
  253. if (!delayed_work_pending(&devfreq->work) &&
  254. devfreq->profile->polling_ms)
  255. queue_delayed_work(devfreq_wq, &devfreq->work,
  256. msecs_to_jiffies(devfreq->profile->polling_ms));
  257. devfreq->stop_polling = false;
  258. out:
  259. mutex_unlock(&devfreq->lock);
  260. }
  261. /**
  262. * devfreq_interval_update() - Update device devfreq monitoring interval
  263. * @devfreq: the devfreq instance.
  264. * @delay: new polling interval to be set.
  265. *
  266. * Helper function to set new load monitoring polling interval. Function
  267. * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
  268. */
  269. void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
  270. {
  271. unsigned int cur_delay = devfreq->profile->polling_ms;
  272. unsigned int new_delay = *delay;
  273. mutex_lock(&devfreq->lock);
  274. devfreq->profile->polling_ms = new_delay;
  275. if (devfreq->stop_polling)
  276. goto out;
  277. /* if new delay is zero, stop polling */
  278. if (!new_delay) {
  279. mutex_unlock(&devfreq->lock);
  280. cancel_delayed_work_sync(&devfreq->work);
  281. return;
  282. }
  283. /* if current delay is zero, start polling with new delay */
  284. if (!cur_delay) {
  285. queue_delayed_work(devfreq_wq, &devfreq->work,
  286. msecs_to_jiffies(devfreq->profile->polling_ms));
  287. goto out;
  288. }
  289. /* if current delay is greater than new delay, restart polling */
  290. if (cur_delay > new_delay) {
  291. mutex_unlock(&devfreq->lock);
  292. cancel_delayed_work_sync(&devfreq->work);
  293. mutex_lock(&devfreq->lock);
  294. if (!devfreq->stop_polling)
  295. queue_delayed_work(devfreq_wq, &devfreq->work,
  296. msecs_to_jiffies(devfreq->profile->polling_ms));
  297. }
  298. out:
  299. mutex_unlock(&devfreq->lock);
  300. }
  301. /**
  302. * devfreq_notifier_call() - Notify that the device frequency requirements
  303. * has been changed out of devfreq framework.
  304. * @nb: the notifier_block (supposed to be devfreq->nb)
  305. * @type: not used
  306. * @devp: not used
  307. *
  308. * Called by a notifier that uses devfreq->nb.
  309. */
  310. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  311. void *devp)
  312. {
  313. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  314. int ret;
  315. mutex_lock(&devfreq->lock);
  316. ret = update_devfreq(devfreq);
  317. mutex_unlock(&devfreq->lock);
  318. return ret;
  319. }
  320. /**
  321. * _remove_devfreq() - Remove devfreq from the list and release its resources.
  322. * @devfreq: the devfreq struct
  323. * @skip: skip calling device_unregister().
  324. */
  325. static void _remove_devfreq(struct devfreq *devfreq, bool skip)
  326. {
  327. mutex_lock(&devfreq_list_lock);
  328. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  329. mutex_unlock(&devfreq_list_lock);
  330. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  331. return;
  332. }
  333. list_del(&devfreq->node);
  334. mutex_unlock(&devfreq_list_lock);
  335. if (devfreq->governor)
  336. devfreq->governor->event_handler(devfreq,
  337. DEVFREQ_GOV_STOP, NULL);
  338. if (devfreq->profile->exit)
  339. devfreq->profile->exit(devfreq->dev.parent);
  340. if (!skip && get_device(&devfreq->dev)) {
  341. device_unregister(&devfreq->dev);
  342. put_device(&devfreq->dev);
  343. }
  344. mutex_destroy(&devfreq->lock);
  345. kfree(devfreq);
  346. }
  347. /**
  348. * devfreq_dev_release() - Callback for struct device to release the device.
  349. * @dev: the devfreq device
  350. *
  351. * This calls _remove_devfreq() if _remove_devfreq() is not called.
  352. * Note that devfreq_dev_release() could be called by _remove_devfreq() as
  353. * well as by others unregistering the device.
  354. */
  355. static void devfreq_dev_release(struct device *dev)
  356. {
  357. struct devfreq *devfreq = to_devfreq(dev);
  358. _remove_devfreq(devfreq, true);
  359. }
  360. /**
  361. * devfreq_add_device() - Add devfreq feature to the device
  362. * @dev: the device to add devfreq feature.
  363. * @profile: device-specific profile to run devfreq.
  364. * @governor_name: name of the policy to choose frequency.
  365. * @data: private data for the governor. The devfreq framework does not
  366. * touch this value.
  367. */
  368. struct devfreq *devfreq_add_device(struct device *dev,
  369. struct devfreq_dev_profile *profile,
  370. const char *governor_name,
  371. void *data)
  372. {
  373. struct devfreq *devfreq;
  374. struct devfreq_governor *governor;
  375. int err = 0;
  376. if (!dev || !profile || !governor_name) {
  377. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  378. return ERR_PTR(-EINVAL);
  379. }
  380. mutex_lock(&devfreq_list_lock);
  381. devfreq = find_device_devfreq(dev);
  382. mutex_unlock(&devfreq_list_lock);
  383. if (!IS_ERR(devfreq)) {
  384. dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
  385. err = -EINVAL;
  386. goto err_out;
  387. }
  388. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  389. if (!devfreq) {
  390. dev_err(dev, "%s: Unable to create devfreq for the device\n",
  391. __func__);
  392. err = -ENOMEM;
  393. goto err_out;
  394. }
  395. mutex_init(&devfreq->lock);
  396. mutex_lock(&devfreq->lock);
  397. devfreq->dev.parent = dev;
  398. devfreq->dev.class = devfreq_class;
  399. devfreq->dev.release = devfreq_dev_release;
  400. devfreq->profile = profile;
  401. strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
  402. devfreq->previous_freq = profile->initial_freq;
  403. devfreq->data = data;
  404. devfreq->nb.notifier_call = devfreq_notifier_call;
  405. devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
  406. devfreq->profile->max_state *
  407. devfreq->profile->max_state,
  408. GFP_KERNEL);
  409. devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) *
  410. devfreq->profile->max_state,
  411. GFP_KERNEL);
  412. devfreq->last_stat_updated = jiffies;
  413. dev_set_name(&devfreq->dev, dev_name(dev));
  414. err = device_register(&devfreq->dev);
  415. if (err) {
  416. put_device(&devfreq->dev);
  417. mutex_unlock(&devfreq->lock);
  418. goto err_dev;
  419. }
  420. mutex_unlock(&devfreq->lock);
  421. mutex_lock(&devfreq_list_lock);
  422. list_add(&devfreq->node, &devfreq_list);
  423. governor = find_devfreq_governor(devfreq->governor_name);
  424. if (!IS_ERR(governor))
  425. devfreq->governor = governor;
  426. if (devfreq->governor)
  427. err = devfreq->governor->event_handler(devfreq,
  428. DEVFREQ_GOV_START, NULL);
  429. mutex_unlock(&devfreq_list_lock);
  430. if (err) {
  431. dev_err(dev, "%s: Unable to start governor for the device\n",
  432. __func__);
  433. goto err_init;
  434. }
  435. return devfreq;
  436. err_init:
  437. list_del(&devfreq->node);
  438. device_unregister(&devfreq->dev);
  439. err_dev:
  440. kfree(devfreq);
  441. err_out:
  442. return ERR_PTR(err);
  443. }
  444. EXPORT_SYMBOL(devfreq_add_device);
  445. /**
  446. * devfreq_remove_device() - Remove devfreq feature from a device.
  447. * @devfreq: the devfreq instance to be removed
  448. */
  449. int devfreq_remove_device(struct devfreq *devfreq)
  450. {
  451. if (!devfreq)
  452. return -EINVAL;
  453. _remove_devfreq(devfreq, false);
  454. return 0;
  455. }
  456. EXPORT_SYMBOL(devfreq_remove_device);
  457. /**
  458. * devfreq_suspend_device() - Suspend devfreq of a device.
  459. * @devfreq: the devfreq instance to be suspended
  460. */
  461. int devfreq_suspend_device(struct devfreq *devfreq)
  462. {
  463. if (!devfreq)
  464. return -EINVAL;
  465. if (!devfreq->governor)
  466. return 0;
  467. return devfreq->governor->event_handler(devfreq,
  468. DEVFREQ_GOV_SUSPEND, NULL);
  469. }
  470. EXPORT_SYMBOL(devfreq_suspend_device);
  471. /**
  472. * devfreq_resume_device() - Resume devfreq of a device.
  473. * @devfreq: the devfreq instance to be resumed
  474. */
  475. int devfreq_resume_device(struct devfreq *devfreq)
  476. {
  477. if (!devfreq)
  478. return -EINVAL;
  479. if (!devfreq->governor)
  480. return 0;
  481. return devfreq->governor->event_handler(devfreq,
  482. DEVFREQ_GOV_RESUME, NULL);
  483. }
  484. EXPORT_SYMBOL(devfreq_resume_device);
  485. /**
  486. * devfreq_add_governor() - Add devfreq governor
  487. * @governor: the devfreq governor to be added
  488. */
  489. int devfreq_add_governor(struct devfreq_governor *governor)
  490. {
  491. struct devfreq_governor *g;
  492. struct devfreq *devfreq;
  493. int err = 0;
  494. if (!governor) {
  495. pr_err("%s: Invalid parameters.\n", __func__);
  496. return -EINVAL;
  497. }
  498. mutex_lock(&devfreq_list_lock);
  499. g = find_devfreq_governor(governor->name);
  500. if (!IS_ERR(g)) {
  501. pr_err("%s: governor %s already registered\n", __func__,
  502. g->name);
  503. err = -EINVAL;
  504. goto err_out;
  505. }
  506. list_add(&governor->node, &devfreq_governor_list);
  507. list_for_each_entry(devfreq, &devfreq_list, node) {
  508. int ret = 0;
  509. struct device *dev = devfreq->dev.parent;
  510. if (!strncmp(devfreq->governor_name, governor->name,
  511. DEVFREQ_NAME_LEN)) {
  512. /* The following should never occur */
  513. if (devfreq->governor) {
  514. dev_warn(dev,
  515. "%s: Governor %s already present\n",
  516. __func__, devfreq->governor->name);
  517. ret = devfreq->governor->event_handler(devfreq,
  518. DEVFREQ_GOV_STOP, NULL);
  519. if (ret) {
  520. dev_warn(dev,
  521. "%s: Governor %s stop = %d\n",
  522. __func__,
  523. devfreq->governor->name, ret);
  524. }
  525. /* Fall through */
  526. }
  527. devfreq->governor = governor;
  528. ret = devfreq->governor->event_handler(devfreq,
  529. DEVFREQ_GOV_START, NULL);
  530. if (ret) {
  531. dev_warn(dev, "%s: Governor %s start=%d\n",
  532. __func__, devfreq->governor->name,
  533. ret);
  534. }
  535. }
  536. }
  537. err_out:
  538. mutex_unlock(&devfreq_list_lock);
  539. return err;
  540. }
  541. EXPORT_SYMBOL(devfreq_add_governor);
  542. /**
  543. * devfreq_remove_device() - Remove devfreq feature from a device.
  544. * @governor: the devfreq governor to be removed
  545. */
  546. int devfreq_remove_governor(struct devfreq_governor *governor)
  547. {
  548. struct devfreq_governor *g;
  549. struct devfreq *devfreq;
  550. int err = 0;
  551. if (!governor) {
  552. pr_err("%s: Invalid parameters.\n", __func__);
  553. return -EINVAL;
  554. }
  555. mutex_lock(&devfreq_list_lock);
  556. g = find_devfreq_governor(governor->name);
  557. if (IS_ERR(g)) {
  558. pr_err("%s: governor %s not registered\n", __func__,
  559. g->name);
  560. err = -EINVAL;
  561. goto err_out;
  562. }
  563. list_for_each_entry(devfreq, &devfreq_list, node) {
  564. int ret;
  565. struct device *dev = devfreq->dev.parent;
  566. if (!strncmp(devfreq->governor_name, governor->name,
  567. DEVFREQ_NAME_LEN)) {
  568. /* we should have a devfreq governor! */
  569. if (!devfreq->governor) {
  570. dev_warn(dev, "%s: Governor %s NOT present\n",
  571. __func__, governor->name);
  572. continue;
  573. /* Fall through */
  574. }
  575. ret = devfreq->governor->event_handler(devfreq,
  576. DEVFREQ_GOV_STOP, NULL);
  577. if (ret) {
  578. dev_warn(dev, "%s: Governor %s stop=%d\n",
  579. __func__, devfreq->governor->name,
  580. ret);
  581. }
  582. devfreq->governor = NULL;
  583. }
  584. }
  585. list_del(&governor->node);
  586. err_out:
  587. mutex_unlock(&devfreq_list_lock);
  588. return err;
  589. }
  590. EXPORT_SYMBOL(devfreq_remove_governor);
  591. static ssize_t show_governor(struct device *dev,
  592. struct device_attribute *attr, char *buf)
  593. {
  594. if (!to_devfreq(dev)->governor)
  595. return -EINVAL;
  596. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  597. }
  598. static ssize_t show_freq(struct device *dev,
  599. struct device_attribute *attr, char *buf)
  600. {
  601. unsigned long freq;
  602. struct devfreq *devfreq = to_devfreq(dev);
  603. if (devfreq->profile->get_cur_freq &&
  604. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  605. return sprintf(buf, "%lu\n", freq);
  606. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  607. }
  608. static ssize_t show_target_freq(struct device *dev,
  609. struct device_attribute *attr, char *buf)
  610. {
  611. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  612. }
  613. static ssize_t show_polling_interval(struct device *dev,
  614. struct device_attribute *attr, char *buf)
  615. {
  616. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  617. }
  618. static ssize_t store_polling_interval(struct device *dev,
  619. struct device_attribute *attr,
  620. const char *buf, size_t count)
  621. {
  622. struct devfreq *df = to_devfreq(dev);
  623. unsigned int value;
  624. int ret;
  625. if (!df->governor)
  626. return -EINVAL;
  627. ret = sscanf(buf, "%u", &value);
  628. if (ret != 1)
  629. return -EINVAL;
  630. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  631. ret = count;
  632. return ret;
  633. }
  634. static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr,
  635. const char *buf, size_t count)
  636. {
  637. struct devfreq *df = to_devfreq(dev);
  638. unsigned long value;
  639. int ret;
  640. unsigned long max;
  641. ret = sscanf(buf, "%lu", &value);
  642. if (ret != 1)
  643. return -EINVAL;
  644. mutex_lock(&df->lock);
  645. max = df->max_freq;
  646. if (value && max && value > max) {
  647. ret = -EINVAL;
  648. goto unlock;
  649. }
  650. df->min_freq = value;
  651. update_devfreq(df);
  652. ret = count;
  653. unlock:
  654. mutex_unlock(&df->lock);
  655. return ret;
  656. }
  657. static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr,
  658. char *buf)
  659. {
  660. return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq);
  661. }
  662. static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr,
  663. const char *buf, size_t count)
  664. {
  665. struct devfreq *df = to_devfreq(dev);
  666. unsigned long value;
  667. int ret;
  668. unsigned long min;
  669. ret = sscanf(buf, "%lu", &value);
  670. if (ret != 1)
  671. return -EINVAL;
  672. mutex_lock(&df->lock);
  673. min = df->min_freq;
  674. if (value && min && value < min) {
  675. ret = -EINVAL;
  676. goto unlock;
  677. }
  678. df->max_freq = value;
  679. update_devfreq(df);
  680. ret = count;
  681. unlock:
  682. mutex_unlock(&df->lock);
  683. return ret;
  684. }
  685. static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr,
  686. char *buf)
  687. {
  688. return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq);
  689. }
  690. static ssize_t show_available_freqs(struct device *d,
  691. struct device_attribute *attr,
  692. char *buf)
  693. {
  694. struct devfreq *df = to_devfreq(d);
  695. struct device *dev = df->dev.parent;
  696. struct opp *opp;
  697. ssize_t count = 0;
  698. unsigned long freq = 0;
  699. rcu_read_lock();
  700. do {
  701. opp = opp_find_freq_ceil(dev, &freq);
  702. if (IS_ERR(opp))
  703. break;
  704. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  705. "%lu ", freq);
  706. freq++;
  707. } while (1);
  708. rcu_read_unlock();
  709. /* Truncate the trailing space */
  710. if (count)
  711. count--;
  712. count += sprintf(&buf[count], "\n");
  713. return count;
  714. }
  715. static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr,
  716. char *buf)
  717. {
  718. struct devfreq *devfreq = to_devfreq(dev);
  719. ssize_t len;
  720. int i, j, err;
  721. unsigned int max_state = devfreq->profile->max_state;
  722. err = devfreq_update_status(devfreq, devfreq->previous_freq);
  723. if (err)
  724. return 0;
  725. len = sprintf(buf, " From : To\n");
  726. len += sprintf(buf + len, " :");
  727. for (i = 0; i < max_state; i++)
  728. len += sprintf(buf + len, "%8u",
  729. devfreq->profile->freq_table[i]);
  730. len += sprintf(buf + len, " time(ms)\n");
  731. for (i = 0; i < max_state; i++) {
  732. if (devfreq->profile->freq_table[i]
  733. == devfreq->previous_freq) {
  734. len += sprintf(buf + len, "*");
  735. } else {
  736. len += sprintf(buf + len, " ");
  737. }
  738. len += sprintf(buf + len, "%8u:",
  739. devfreq->profile->freq_table[i]);
  740. for (j = 0; j < max_state; j++)
  741. len += sprintf(buf + len, "%8u",
  742. devfreq->trans_table[(i * max_state) + j]);
  743. len += sprintf(buf + len, "%10u\n",
  744. jiffies_to_msecs(devfreq->time_in_state[i]));
  745. }
  746. len += sprintf(buf + len, "Total transition : %u\n",
  747. devfreq->total_trans);
  748. return len;
  749. }
  750. static struct device_attribute devfreq_attrs[] = {
  751. __ATTR(governor, S_IRUGO, show_governor, NULL),
  752. __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
  753. __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL),
  754. __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
  755. __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
  756. store_polling_interval),
  757. __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
  758. __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
  759. __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL),
  760. { },
  761. };
  762. static int __init devfreq_init(void)
  763. {
  764. devfreq_class = class_create(THIS_MODULE, "devfreq");
  765. if (IS_ERR(devfreq_class)) {
  766. pr_err("%s: couldn't create class\n", __FILE__);
  767. return PTR_ERR(devfreq_class);
  768. }
  769. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  770. if (IS_ERR(devfreq_wq)) {
  771. class_destroy(devfreq_class);
  772. pr_err("%s: couldn't create workqueue\n", __FILE__);
  773. return PTR_ERR(devfreq_wq);
  774. }
  775. devfreq_class->dev_attrs = devfreq_attrs;
  776. return 0;
  777. }
  778. subsys_initcall(devfreq_init);
  779. static void __exit devfreq_exit(void)
  780. {
  781. class_destroy(devfreq_class);
  782. destroy_workqueue(devfreq_wq);
  783. }
  784. module_exit(devfreq_exit);
  785. /*
  786. * The followings are helper functions for devfreq user device drivers with
  787. * OPP framework.
  788. */
  789. /**
  790. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  791. * freq value given to target callback.
  792. * @dev: The devfreq user device. (parent of devfreq)
  793. * @freq: The frequency given to target function
  794. * @flags: Flags handed from devfreq framework.
  795. *
  796. */
  797. struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
  798. u32 flags)
  799. {
  800. struct opp *opp;
  801. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  802. /* The freq is an upper bound. opp should be lower */
  803. opp = opp_find_freq_floor(dev, freq);
  804. /* If not available, use the closest opp */
  805. if (opp == ERR_PTR(-ENODEV))
  806. opp = opp_find_freq_ceil(dev, freq);
  807. } else {
  808. /* The freq is an lower bound. opp should be higher */
  809. opp = opp_find_freq_ceil(dev, freq);
  810. /* If not available, use the closest opp */
  811. if (opp == ERR_PTR(-ENODEV))
  812. opp = opp_find_freq_floor(dev, freq);
  813. }
  814. return opp;
  815. }
  816. /**
  817. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  818. * for any changes in the OPP availability
  819. * changes
  820. * @dev: The devfreq user device. (parent of devfreq)
  821. * @devfreq: The devfreq object.
  822. */
  823. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  824. {
  825. struct srcu_notifier_head *nh = opp_get_notifier(dev);
  826. if (IS_ERR(nh))
  827. return PTR_ERR(nh);
  828. return srcu_notifier_chain_register(nh, &devfreq->nb);
  829. }
  830. /**
  831. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  832. * notified for any changes in the OPP
  833. * availability changes anymore.
  834. * @dev: The devfreq user device. (parent of devfreq)
  835. * @devfreq: The devfreq object.
  836. *
  837. * At exit() callback of devfreq_dev_profile, this must be included if
  838. * devfreq_recommended_opp is used.
  839. */
  840. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  841. {
  842. struct srcu_notifier_head *nh = opp_get_notifier(dev);
  843. if (IS_ERR(nh))
  844. return PTR_ERR(nh);
  845. return srcu_notifier_chain_unregister(nh, &devfreq->nb);
  846. }
  847. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  848. MODULE_DESCRIPTION("devfreq class support");
  849. MODULE_LICENSE("GPL");