devfreq.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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. devfreq->profile->polling_ms = new_delay;
  302. if (devfreq->stop_polling)
  303. goto out;
  304. /* if new delay is zero, stop polling */
  305. if (!new_delay) {
  306. mutex_unlock(&devfreq->lock);
  307. cancel_delayed_work_sync(&devfreq->work);
  308. return;
  309. }
  310. /* if current delay is zero, start polling with new delay */
  311. if (!cur_delay) {
  312. queue_delayed_work(devfreq_wq, &devfreq->work,
  313. msecs_to_jiffies(devfreq->profile->polling_ms));
  314. goto out;
  315. }
  316. /* if current delay is greater than new delay, restart polling */
  317. if (cur_delay > new_delay) {
  318. mutex_unlock(&devfreq->lock);
  319. cancel_delayed_work_sync(&devfreq->work);
  320. mutex_lock(&devfreq->lock);
  321. if (!devfreq->stop_polling)
  322. queue_delayed_work(devfreq_wq, &devfreq->work,
  323. msecs_to_jiffies(devfreq->profile->polling_ms));
  324. }
  325. out:
  326. mutex_unlock(&devfreq->lock);
  327. }
  328. EXPORT_SYMBOL(devfreq_interval_update);
  329. /**
  330. * devfreq_notifier_call() - Notify that the device frequency requirements
  331. * has been changed out of devfreq framework.
  332. * @nb: the notifier_block (supposed to be devfreq->nb)
  333. * @type: not used
  334. * @devp: not used
  335. *
  336. * Called by a notifier that uses devfreq->nb.
  337. */
  338. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  339. void *devp)
  340. {
  341. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  342. int ret;
  343. mutex_lock(&devfreq->lock);
  344. ret = update_devfreq(devfreq);
  345. mutex_unlock(&devfreq->lock);
  346. return ret;
  347. }
  348. /**
  349. * _remove_devfreq() - Remove devfreq from the list and release its resources.
  350. * @devfreq: the devfreq struct
  351. * @skip: skip calling device_unregister().
  352. */
  353. static void _remove_devfreq(struct devfreq *devfreq)
  354. {
  355. mutex_lock(&devfreq_list_lock);
  356. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  357. mutex_unlock(&devfreq_list_lock);
  358. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  359. return;
  360. }
  361. list_del(&devfreq->node);
  362. mutex_unlock(&devfreq_list_lock);
  363. if (devfreq->governor)
  364. devfreq->governor->event_handler(devfreq,
  365. DEVFREQ_GOV_STOP, NULL);
  366. if (devfreq->profile->exit)
  367. devfreq->profile->exit(devfreq->dev.parent);
  368. mutex_destroy(&devfreq->lock);
  369. kfree(devfreq);
  370. }
  371. /**
  372. * devfreq_dev_release() - Callback for struct device to release the device.
  373. * @dev: the devfreq device
  374. *
  375. * This calls _remove_devfreq() if _remove_devfreq() is not called.
  376. */
  377. static void devfreq_dev_release(struct device *dev)
  378. {
  379. struct devfreq *devfreq = to_devfreq(dev);
  380. _remove_devfreq(devfreq);
  381. }
  382. /**
  383. * find_governor_data - Find device specific private data for a governor.
  384. * @profile: The profile to search.
  385. * @governor_name: The governor to search for.
  386. *
  387. * Look up the device specific data for a governor.
  388. */
  389. static void *find_governor_data(struct devfreq_dev_profile *profile,
  390. const char *governor_name)
  391. {
  392. void *data = NULL;
  393. int i;
  394. if (profile->governor_data == NULL)
  395. return NULL;
  396. for (i = 0; i < profile->num_governor_data; i++) {
  397. if (strncmp(governor_name, profile->governor_data[i].name,
  398. DEVFREQ_NAME_LEN) == 0) {
  399. data = profile->governor_data[i].data;
  400. break;
  401. }
  402. }
  403. return data;
  404. }
  405. /**
  406. * devfreq_add_device() - Add devfreq feature to the device
  407. * @dev: the device to add devfreq feature.
  408. * @profile: device-specific profile to run devfreq.
  409. * @governor_name: name of the policy to choose frequency.
  410. * @data: private data for the governor. The devfreq framework does not
  411. * touch this value.
  412. */
  413. struct devfreq *devfreq_add_device(struct device *dev,
  414. struct devfreq_dev_profile *profile,
  415. const char *governor_name,
  416. void *data)
  417. {
  418. struct devfreq *devfreq;
  419. struct devfreq_governor *governor;
  420. int err = 0;
  421. if (!dev || !profile || !governor_name) {
  422. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  423. return ERR_PTR(-EINVAL);
  424. }
  425. mutex_lock(&devfreq_list_lock);
  426. devfreq = find_device_devfreq(dev);
  427. mutex_unlock(&devfreq_list_lock);
  428. if (!IS_ERR(devfreq)) {
  429. dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__);
  430. err = -EINVAL;
  431. goto err_out;
  432. }
  433. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  434. if (!devfreq) {
  435. dev_err(dev, "%s: Unable to create devfreq for the device\n",
  436. __func__);
  437. err = -ENOMEM;
  438. goto err_out;
  439. }
  440. mutex_init(&devfreq->lock);
  441. mutex_lock(&devfreq->lock);
  442. devfreq->dev.parent = dev;
  443. devfreq->dev.class = devfreq_class;
  444. devfreq->dev.release = devfreq_dev_release;
  445. devfreq->profile = profile;
  446. strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
  447. devfreq->previous_freq = profile->initial_freq;
  448. devfreq->data = data ? data : find_governor_data(devfreq->profile,
  449. governor_name);
  450. devfreq->nb.notifier_call = devfreq_notifier_call;
  451. devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) *
  452. devfreq->profile->max_state *
  453. devfreq->profile->max_state,
  454. GFP_KERNEL);
  455. devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) *
  456. devfreq->profile->max_state,
  457. GFP_KERNEL);
  458. devfreq->last_stat_updated = jiffies;
  459. devfreq_set_freq_limits(devfreq);
  460. dev_set_name(&devfreq->dev, dev_name(dev));
  461. err = device_register(&devfreq->dev);
  462. if (err) {
  463. put_device(&devfreq->dev);
  464. mutex_unlock(&devfreq->lock);
  465. goto err_dev;
  466. }
  467. mutex_unlock(&devfreq->lock);
  468. mutex_lock(&devfreq_list_lock);
  469. list_add(&devfreq->node, &devfreq_list);
  470. governor = find_devfreq_governor(devfreq->governor_name);
  471. if (!IS_ERR(governor))
  472. devfreq->governor = governor;
  473. if (devfreq->governor)
  474. err = devfreq->governor->event_handler(devfreq,
  475. DEVFREQ_GOV_START, NULL);
  476. mutex_unlock(&devfreq_list_lock);
  477. if (err) {
  478. dev_err(dev, "%s: Unable to start governor for the device\n",
  479. __func__);
  480. goto err_init;
  481. }
  482. return devfreq;
  483. err_init:
  484. list_del(&devfreq->node);
  485. device_unregister(&devfreq->dev);
  486. err_dev:
  487. kfree(devfreq);
  488. err_out:
  489. return ERR_PTR(err);
  490. }
  491. EXPORT_SYMBOL(devfreq_add_device);
  492. /**
  493. * devfreq_remove_device() - Remove devfreq feature from a device.
  494. * @devfreq: the devfreq instance to be removed
  495. */
  496. int devfreq_remove_device(struct devfreq *devfreq)
  497. {
  498. if (!devfreq)
  499. return -EINVAL;
  500. device_unregister(&devfreq->dev);
  501. put_device(&devfreq->dev);
  502. return 0;
  503. }
  504. EXPORT_SYMBOL(devfreq_remove_device);
  505. /**
  506. * devfreq_suspend_device() - Suspend devfreq of a device.
  507. * @devfreq: the devfreq instance to be suspended
  508. */
  509. int devfreq_suspend_device(struct devfreq *devfreq)
  510. {
  511. if (!devfreq)
  512. return -EINVAL;
  513. if (!devfreq->governor)
  514. return 0;
  515. return devfreq->governor->event_handler(devfreq,
  516. DEVFREQ_GOV_SUSPEND, NULL);
  517. }
  518. EXPORT_SYMBOL(devfreq_suspend_device);
  519. /**
  520. * devfreq_resume_device() - Resume devfreq of a device.
  521. * @devfreq: the devfreq instance to be resumed
  522. */
  523. int devfreq_resume_device(struct devfreq *devfreq)
  524. {
  525. if (!devfreq)
  526. return -EINVAL;
  527. if (!devfreq->governor)
  528. return 0;
  529. return devfreq->governor->event_handler(devfreq,
  530. DEVFREQ_GOV_RESUME, NULL);
  531. }
  532. EXPORT_SYMBOL(devfreq_resume_device);
  533. /**
  534. * devfreq_add_governor() - Add devfreq governor
  535. * @governor: the devfreq governor to be added
  536. */
  537. int devfreq_add_governor(struct devfreq_governor *governor)
  538. {
  539. struct devfreq_governor *g;
  540. struct devfreq *devfreq;
  541. int err = 0;
  542. if (!governor) {
  543. pr_err("%s: Invalid parameters.\n", __func__);
  544. return -EINVAL;
  545. }
  546. mutex_lock(&devfreq_list_lock);
  547. g = find_devfreq_governor(governor->name);
  548. if (!IS_ERR(g)) {
  549. pr_err("%s: governor %s already registered\n", __func__,
  550. g->name);
  551. err = -EINVAL;
  552. goto err_out;
  553. }
  554. list_add(&governor->node, &devfreq_governor_list);
  555. list_for_each_entry(devfreq, &devfreq_list, node) {
  556. int ret = 0;
  557. struct device *dev = devfreq->dev.parent;
  558. if (!strncmp(devfreq->governor_name, governor->name,
  559. DEVFREQ_NAME_LEN)) {
  560. /* The following should never occur */
  561. if (devfreq->governor) {
  562. dev_warn(dev,
  563. "%s: Governor %s already present\n",
  564. __func__, devfreq->governor->name);
  565. ret = devfreq->governor->event_handler(devfreq,
  566. DEVFREQ_GOV_STOP, NULL);
  567. if (ret) {
  568. dev_warn(dev,
  569. "%s: Governor %s stop = %d\n",
  570. __func__,
  571. devfreq->governor->name, ret);
  572. }
  573. /* Fall through */
  574. }
  575. devfreq->governor = governor;
  576. ret = devfreq->governor->event_handler(devfreq,
  577. DEVFREQ_GOV_START, NULL);
  578. if (ret) {
  579. dev_warn(dev, "%s: Governor %s start=%d\n",
  580. __func__, devfreq->governor->name,
  581. ret);
  582. }
  583. }
  584. }
  585. err_out:
  586. mutex_unlock(&devfreq_list_lock);
  587. return err;
  588. }
  589. EXPORT_SYMBOL(devfreq_add_governor);
  590. /**
  591. * devfreq_remove_device() - Remove devfreq feature from a device.
  592. * @governor: the devfreq governor to be removed
  593. */
  594. int devfreq_remove_governor(struct devfreq_governor *governor)
  595. {
  596. struct devfreq_governor *g;
  597. struct devfreq *devfreq;
  598. int err = 0;
  599. if (!governor) {
  600. pr_err("%s: Invalid parameters.\n", __func__);
  601. return -EINVAL;
  602. }
  603. mutex_lock(&devfreq_list_lock);
  604. g = find_devfreq_governor(governor->name);
  605. if (IS_ERR(g)) {
  606. pr_err("%s: governor %s not registered\n", __func__,
  607. governor->name);
  608. err = PTR_ERR(g);
  609. goto err_out;
  610. }
  611. list_for_each_entry(devfreq, &devfreq_list, node) {
  612. int ret;
  613. struct device *dev = devfreq->dev.parent;
  614. if (!strncmp(devfreq->governor_name, governor->name,
  615. DEVFREQ_NAME_LEN)) {
  616. /* we should have a devfreq governor! */
  617. if (!devfreq->governor) {
  618. dev_warn(dev, "%s: Governor %s NOT present\n",
  619. __func__, governor->name);
  620. continue;
  621. /* Fall through */
  622. }
  623. ret = devfreq->governor->event_handler(devfreq,
  624. DEVFREQ_GOV_STOP, NULL);
  625. if (ret) {
  626. dev_warn(dev, "%s: Governor %s stop=%d\n",
  627. __func__, devfreq->governor->name,
  628. ret);
  629. }
  630. devfreq->governor = NULL;
  631. }
  632. }
  633. list_del(&governor->node);
  634. err_out:
  635. mutex_unlock(&devfreq_list_lock);
  636. return err;
  637. }
  638. EXPORT_SYMBOL(devfreq_remove_governor);
  639. static ssize_t show_governor(struct device *dev,
  640. struct device_attribute *attr, char *buf)
  641. {
  642. if (!to_devfreq(dev)->governor)
  643. return -EINVAL;
  644. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  645. }
  646. static ssize_t store_governor(struct device *dev, struct device_attribute *attr,
  647. const char *buf, size_t count)
  648. {
  649. struct devfreq *df = to_devfreq(dev);
  650. int ret;
  651. char str_governor[DEVFREQ_NAME_LEN + 1];
  652. struct devfreq_governor *governor;
  653. ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
  654. if (ret != 1)
  655. return -EINVAL;
  656. mutex_lock(&devfreq_list_lock);
  657. governor = find_devfreq_governor(str_governor);
  658. if (IS_ERR(governor)) {
  659. ret = PTR_ERR(governor);
  660. goto out;
  661. }
  662. if (df->governor == governor)
  663. goto out;
  664. if (df->governor) {
  665. ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
  666. if (ret) {
  667. dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
  668. __func__, df->governor->name, ret);
  669. goto out;
  670. }
  671. }
  672. df->data = find_governor_data(df->profile, str_governor);
  673. df->governor = governor;
  674. strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
  675. ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
  676. if (ret)
  677. dev_warn(dev, "%s: Governor %s not started(%d)\n",
  678. __func__, df->governor->name, ret);
  679. out:
  680. mutex_unlock(&devfreq_list_lock);
  681. if (!ret)
  682. ret = count;
  683. return ret;
  684. }
  685. static ssize_t show_available_governors(struct device *d,
  686. struct device_attribute *attr,
  687. char *buf)
  688. {
  689. struct devfreq_governor *tmp_governor;
  690. ssize_t count = 0;
  691. mutex_lock(&devfreq_list_lock);
  692. list_for_each_entry(tmp_governor, &devfreq_governor_list, node)
  693. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  694. "%s ", tmp_governor->name);
  695. mutex_unlock(&devfreq_list_lock);
  696. /* Truncate the trailing space */
  697. if (count)
  698. count--;
  699. count += sprintf(&buf[count], "\n");
  700. return count;
  701. }
  702. static ssize_t show_freq(struct device *dev,
  703. struct device_attribute *attr, char *buf)
  704. {
  705. unsigned long freq;
  706. struct devfreq *devfreq = to_devfreq(dev);
  707. if (devfreq->profile->get_cur_freq &&
  708. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  709. return sprintf(buf, "%lu\n", freq);
  710. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  711. }
  712. static ssize_t show_target_freq(struct device *dev,
  713. struct device_attribute *attr, char *buf)
  714. {
  715. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  716. }
  717. static ssize_t show_polling_interval(struct device *dev,
  718. struct device_attribute *attr, char *buf)
  719. {
  720. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  721. }
  722. static ssize_t store_polling_interval(struct device *dev,
  723. struct device_attribute *attr,
  724. const char *buf, size_t count)
  725. {
  726. struct devfreq *df = to_devfreq(dev);
  727. unsigned int value;
  728. int ret;
  729. if (!df->governor)
  730. return -EINVAL;
  731. ret = sscanf(buf, "%u", &value);
  732. if (ret != 1)
  733. return -EINVAL;
  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. struct device *dev = df->dev.parent;
  800. struct opp *opp;
  801. ssize_t count = 0;
  802. unsigned long freq = 0;
  803. rcu_read_lock();
  804. do {
  805. opp = opp_find_freq_ceil(dev, &freq);
  806. if (IS_ERR(opp))
  807. break;
  808. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  809. "%lu ", freq);
  810. freq++;
  811. } while (1);
  812. rcu_read_unlock();
  813. /* Truncate the trailing space */
  814. if (count)
  815. count--;
  816. count += sprintf(&buf[count], "\n");
  817. return count;
  818. }
  819. static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr,
  820. char *buf)
  821. {
  822. struct devfreq *devfreq = to_devfreq(dev);
  823. ssize_t len;
  824. int i, j, err;
  825. unsigned int max_state = devfreq->profile->max_state;
  826. err = devfreq_update_status(devfreq, devfreq->previous_freq);
  827. if (err)
  828. return 0;
  829. len = sprintf(buf, " From : To\n");
  830. len += sprintf(buf + len, " :");
  831. for (i = 0; i < max_state; i++)
  832. len += sprintf(buf + len, "%8u",
  833. devfreq->profile->freq_table[i]);
  834. len += sprintf(buf + len, " time(ms)\n");
  835. for (i = 0; i < max_state; i++) {
  836. if (devfreq->profile->freq_table[i]
  837. == devfreq->previous_freq) {
  838. len += sprintf(buf + len, "*");
  839. } else {
  840. len += sprintf(buf + len, " ");
  841. }
  842. len += sprintf(buf + len, "%8u:",
  843. devfreq->profile->freq_table[i]);
  844. for (j = 0; j < max_state; j++)
  845. len += sprintf(buf + len, "%8u",
  846. devfreq->trans_table[(i * max_state) + j]);
  847. len += sprintf(buf + len, "%10u\n",
  848. jiffies_to_msecs(devfreq->time_in_state[i]));
  849. }
  850. len += sprintf(buf + len, "Total transition : %u\n",
  851. devfreq->total_trans);
  852. return len;
  853. }
  854. static ssize_t show_time_in_state(struct device *dev, struct device_attribute *attr,
  855. char *buf)
  856. {
  857. struct devfreq *devfreq = to_devfreq(dev);
  858. ssize_t len = 0;
  859. int i, err;
  860. err = devfreq_update_status(devfreq, devfreq->previous_freq);
  861. if (err)
  862. return 0;
  863. for (i = 0; i < devfreq->profile->max_state; i++) {
  864. len += sprintf(buf + len, "%u %u\n",
  865. devfreq->profile->freq_table[i],
  866. jiffies_to_msecs(devfreq->time_in_state[i]));
  867. }
  868. return len;
  869. }
  870. static struct device_attribute devfreq_attrs[] = {
  871. __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor),
  872. __ATTR(available_governors, S_IRUGO, show_available_governors, NULL),
  873. __ATTR(cur_freq, S_IRUGO, show_freq, NULL),
  874. __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL),
  875. __ATTR(target_freq, S_IRUGO, show_target_freq, NULL),
  876. __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval,
  877. store_polling_interval),
  878. __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq),
  879. __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq),
  880. __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL),
  881. __ATTR(time_in_state, S_IRUGO, show_time_in_state, NULL),
  882. { },
  883. };
  884. static int __init devfreq_init(void)
  885. {
  886. devfreq_class = class_create(THIS_MODULE, "devfreq");
  887. if (IS_ERR(devfreq_class)) {
  888. pr_err("%s: couldn't create class\n", __FILE__);
  889. return PTR_ERR(devfreq_class);
  890. }
  891. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  892. if (IS_ERR(devfreq_wq)) {
  893. class_destroy(devfreq_class);
  894. pr_err("%s: couldn't create workqueue\n", __FILE__);
  895. return PTR_ERR(devfreq_wq);
  896. }
  897. devfreq_class->dev_attrs = devfreq_attrs;
  898. return 0;
  899. }
  900. subsys_initcall(devfreq_init);
  901. static void __exit devfreq_exit(void)
  902. {
  903. class_destroy(devfreq_class);
  904. destroy_workqueue(devfreq_wq);
  905. }
  906. module_exit(devfreq_exit);
  907. /*
  908. * The followings are helper functions for devfreq user device drivers with
  909. * OPP framework.
  910. */
  911. /**
  912. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  913. * freq value given to target callback.
  914. * @dev: The devfreq user device. (parent of devfreq)
  915. * @freq: The frequency given to target function
  916. * @flags: Flags handed from devfreq framework.
  917. *
  918. * Locking: This function must be called under rcu_read_lock(). opp is a rcu
  919. * protected pointer. The reason for the same is that the opp pointer which is
  920. * returned will remain valid for use with opp_get_{voltage, freq} only while
  921. * under the locked area. The pointer returned must be used prior to unlocking
  922. * with rcu_read_unlock() to maintain the integrity of the pointer.
  923. */
  924. struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq,
  925. u32 flags)
  926. {
  927. struct opp *opp;
  928. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  929. /* The freq is an upper bound. opp should be lower */
  930. opp = opp_find_freq_floor(dev, freq);
  931. /* If not available, use the closest opp */
  932. if (opp == ERR_PTR(-ERANGE))
  933. opp = opp_find_freq_ceil(dev, freq);
  934. } else {
  935. /* The freq is an lower bound. opp should be higher */
  936. opp = opp_find_freq_ceil(dev, freq);
  937. /* If not available, use the closest opp */
  938. if (opp == ERR_PTR(-ERANGE))
  939. opp = opp_find_freq_floor(dev, freq);
  940. }
  941. return opp;
  942. }
  943. /**
  944. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  945. * for any changes in the OPP availability
  946. * changes
  947. * @dev: The devfreq user device. (parent of devfreq)
  948. * @devfreq: The devfreq object.
  949. */
  950. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  951. {
  952. struct srcu_notifier_head *nh;
  953. int ret = 0;
  954. rcu_read_lock();
  955. nh = opp_get_notifier(dev);
  956. if (IS_ERR(nh))
  957. ret = PTR_ERR(nh);
  958. rcu_read_unlock();
  959. if (!ret)
  960. ret = srcu_notifier_chain_register(nh, &devfreq->nb);
  961. return ret;
  962. }
  963. /**
  964. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  965. * notified for any changes in the OPP
  966. * availability changes anymore.
  967. * @dev: The devfreq user device. (parent of devfreq)
  968. * @devfreq: The devfreq object.
  969. *
  970. * At exit() callback of devfreq_dev_profile, this must be included if
  971. * devfreq_recommended_opp is used.
  972. */
  973. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  974. {
  975. struct srcu_notifier_head *nh;
  976. int ret = 0;
  977. rcu_read_lock();
  978. nh = opp_get_notifier(dev);
  979. if (IS_ERR(nh))
  980. ret = PTR_ERR(nh);
  981. rcu_read_unlock();
  982. if (!ret)
  983. ret = srcu_notifier_chain_unregister(nh, &devfreq->nb);
  984. return ret;
  985. }
  986. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  987. MODULE_DESCRIPTION("devfreq class support");
  988. MODULE_LICENSE("GPL");