devfreq.c 21 KB

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