devfreq.c 28 KB

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