devfreq.c 23 KB

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