devfreq.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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/kmod.h>
  14. #include <linux/sched.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/export.h>
  19. #include <linux/slab.h>
  20. #include <linux/stat.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/devfreq.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/list.h>
  26. #include <linux/printk.h>
  27. #include <linux/hrtimer.h>
  28. #include <linux/of.h>
  29. #include "governor.h"
  30. static struct class *devfreq_class;
  31. /*
  32. * devfreq core provides delayed work based load monitoring helper
  33. * functions. Governors can use these or can implement their own
  34. * monitoring mechanism.
  35. */
  36. static struct workqueue_struct *devfreq_wq;
  37. /* The list of all device-devfreq governors */
  38. static LIST_HEAD(devfreq_governor_list);
  39. /* The list of all device-devfreq */
  40. static LIST_HEAD(devfreq_list);
  41. static DEFINE_MUTEX(devfreq_list_lock);
  42. /**
  43. * find_device_devfreq() - find devfreq struct using device pointer
  44. * @dev: device pointer used to lookup device devfreq.
  45. *
  46. * Search the list of device devfreqs and return the matched device's
  47. * devfreq info. devfreq_list_lock should be held by the caller.
  48. */
  49. static struct devfreq *find_device_devfreq(struct device *dev)
  50. {
  51. struct devfreq *tmp_devfreq;
  52. if (IS_ERR_OR_NULL(dev)) {
  53. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  54. return ERR_PTR(-EINVAL);
  55. }
  56. WARN(!mutex_is_locked(&devfreq_list_lock),
  57. "devfreq_list_lock must be locked.");
  58. list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
  59. if (tmp_devfreq->dev.parent == dev)
  60. return tmp_devfreq;
  61. }
  62. return ERR_PTR(-ENODEV);
  63. }
  64. static unsigned long find_available_min_freq(struct devfreq *devfreq)
  65. {
  66. struct dev_pm_opp *opp;
  67. unsigned long min_freq = 0;
  68. opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
  69. if (IS_ERR(opp))
  70. min_freq = 0;
  71. else
  72. dev_pm_opp_put(opp);
  73. return min_freq;
  74. }
  75. static unsigned long find_available_max_freq(struct devfreq *devfreq)
  76. {
  77. struct dev_pm_opp *opp;
  78. unsigned long max_freq = ULONG_MAX;
  79. opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
  80. if (IS_ERR(opp))
  81. max_freq = 0;
  82. else
  83. dev_pm_opp_put(opp);
  84. return max_freq;
  85. }
  86. /**
  87. * devfreq_get_freq_level() - Lookup freq_table for the frequency
  88. * @devfreq: the devfreq instance
  89. * @freq: the target frequency
  90. */
  91. static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
  92. {
  93. int lev;
  94. for (lev = 0; lev < devfreq->profile->max_state; lev++)
  95. if (freq == devfreq->profile->freq_table[lev])
  96. return lev;
  97. return -EINVAL;
  98. }
  99. static int set_freq_table(struct devfreq *devfreq)
  100. {
  101. struct devfreq_dev_profile *profile = devfreq->profile;
  102. struct dev_pm_opp *opp;
  103. unsigned long freq;
  104. int i, count;
  105. /* Initialize the freq_table from OPP table */
  106. count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
  107. if (count <= 0)
  108. return -EINVAL;
  109. profile->max_state = count;
  110. profile->freq_table = devm_kcalloc(devfreq->dev.parent,
  111. profile->max_state,
  112. sizeof(*profile->freq_table),
  113. GFP_KERNEL);
  114. if (!profile->freq_table) {
  115. profile->max_state = 0;
  116. return -ENOMEM;
  117. }
  118. for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
  119. opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
  120. if (IS_ERR(opp)) {
  121. devm_kfree(devfreq->dev.parent, profile->freq_table);
  122. profile->max_state = 0;
  123. return PTR_ERR(opp);
  124. }
  125. dev_pm_opp_put(opp);
  126. profile->freq_table[i] = freq;
  127. }
  128. return 0;
  129. }
  130. /**
  131. * devfreq_update_status() - Update statistics of devfreq behavior
  132. * @devfreq: the devfreq instance
  133. * @freq: the update target frequency
  134. */
  135. int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
  136. {
  137. int lev, prev_lev, ret = 0;
  138. unsigned long cur_time;
  139. cur_time = jiffies;
  140. /* Immediately exit if previous_freq is not initialized yet. */
  141. if (!devfreq->previous_freq)
  142. goto out;
  143. prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
  144. if (prev_lev < 0) {
  145. ret = prev_lev;
  146. goto out;
  147. }
  148. devfreq->time_in_state[prev_lev] +=
  149. cur_time - devfreq->last_stat_updated;
  150. lev = devfreq_get_freq_level(devfreq, freq);
  151. if (lev < 0) {
  152. ret = lev;
  153. goto out;
  154. }
  155. if (lev != prev_lev) {
  156. devfreq->trans_table[(prev_lev *
  157. devfreq->profile->max_state) + lev]++;
  158. devfreq->total_trans++;
  159. }
  160. out:
  161. devfreq->last_stat_updated = cur_time;
  162. return ret;
  163. }
  164. EXPORT_SYMBOL(devfreq_update_status);
  165. /**
  166. * find_devfreq_governor() - find devfreq governor from name
  167. * @name: name of the governor
  168. *
  169. * Search the list of devfreq governors and return the matched
  170. * governor's pointer. devfreq_list_lock should be held by the caller.
  171. */
  172. static struct devfreq_governor *find_devfreq_governor(const char *name)
  173. {
  174. struct devfreq_governor *tmp_governor;
  175. if (IS_ERR_OR_NULL(name)) {
  176. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  177. return ERR_PTR(-EINVAL);
  178. }
  179. WARN(!mutex_is_locked(&devfreq_list_lock),
  180. "devfreq_list_lock must be locked.");
  181. list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
  182. if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
  183. return tmp_governor;
  184. }
  185. return ERR_PTR(-ENODEV);
  186. }
  187. /**
  188. * try_then_request_governor() - Try to find the governor and request the
  189. * module if is not found.
  190. * @name: name of the governor
  191. *
  192. * Search the list of devfreq governors and request the module and try again
  193. * if is not found. This can happen when both drivers (the governor driver
  194. * and the driver that call devfreq_add_device) are built as modules.
  195. * devfreq_list_lock should be held by the caller. Returns the matched
  196. * governor's pointer.
  197. */
  198. static struct devfreq_governor *try_then_request_governor(const char *name)
  199. {
  200. struct devfreq_governor *governor;
  201. int err = 0;
  202. if (IS_ERR_OR_NULL(name)) {
  203. pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
  204. return ERR_PTR(-EINVAL);
  205. }
  206. WARN(!mutex_is_locked(&devfreq_list_lock),
  207. "devfreq_list_lock must be locked.");
  208. governor = find_devfreq_governor(name);
  209. if (IS_ERR(governor)) {
  210. mutex_unlock(&devfreq_list_lock);
  211. if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
  212. DEVFREQ_NAME_LEN))
  213. err = request_module("governor_%s", "simpleondemand");
  214. else
  215. err = request_module("governor_%s", name);
  216. /* Restore previous state before return */
  217. mutex_lock(&devfreq_list_lock);
  218. if (err)
  219. return NULL;
  220. governor = find_devfreq_governor(name);
  221. }
  222. return governor;
  223. }
  224. static int devfreq_notify_transition(struct devfreq *devfreq,
  225. struct devfreq_freqs *freqs, unsigned int state)
  226. {
  227. if (!devfreq)
  228. return -EINVAL;
  229. switch (state) {
  230. case DEVFREQ_PRECHANGE:
  231. srcu_notifier_call_chain(&devfreq->transition_notifier_list,
  232. DEVFREQ_PRECHANGE, freqs);
  233. break;
  234. case DEVFREQ_POSTCHANGE:
  235. srcu_notifier_call_chain(&devfreq->transition_notifier_list,
  236. DEVFREQ_POSTCHANGE, freqs);
  237. break;
  238. default:
  239. return -EINVAL;
  240. }
  241. return 0;
  242. }
  243. /* Load monitoring helper functions for governors use */
  244. /**
  245. * update_devfreq() - Reevaluate the device and configure frequency.
  246. * @devfreq: the devfreq instance.
  247. *
  248. * Note: Lock devfreq->lock before calling update_devfreq
  249. * This function is exported for governors.
  250. */
  251. int update_devfreq(struct devfreq *devfreq)
  252. {
  253. struct devfreq_freqs freqs;
  254. unsigned long freq, cur_freq, min_freq, max_freq;
  255. int err = 0;
  256. u32 flags = 0;
  257. if (!mutex_is_locked(&devfreq->lock)) {
  258. WARN(true, "devfreq->lock must be locked by the caller.\n");
  259. return -EINVAL;
  260. }
  261. if (!devfreq->governor)
  262. return -EINVAL;
  263. /* Reevaluate the proper frequency */
  264. err = devfreq->governor->get_target_freq(devfreq, &freq);
  265. if (err)
  266. return err;
  267. /*
  268. * Adjust the frequency with user freq, QoS and available freq.
  269. *
  270. * List from the highest priority
  271. * max_freq
  272. * min_freq
  273. */
  274. max_freq = min(devfreq->scaling_max_freq, devfreq->max_freq);
  275. min_freq = max(devfreq->scaling_min_freq, devfreq->min_freq);
  276. if (freq < min_freq) {
  277. freq = min_freq;
  278. flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
  279. }
  280. if (freq > max_freq) {
  281. freq = max_freq;
  282. flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
  283. }
  284. if (devfreq->profile->get_cur_freq)
  285. devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
  286. else
  287. cur_freq = devfreq->previous_freq;
  288. freqs.old = cur_freq;
  289. freqs.new = freq;
  290. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
  291. err = devfreq->profile->target(devfreq->dev.parent, &freq, flags);
  292. if (err) {
  293. freqs.new = cur_freq;
  294. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
  295. return err;
  296. }
  297. freqs.new = freq;
  298. devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
  299. if (devfreq_update_status(devfreq, freq))
  300. dev_err(&devfreq->dev,
  301. "Couldn't update frequency transition information.\n");
  302. devfreq->previous_freq = freq;
  303. return err;
  304. }
  305. EXPORT_SYMBOL(update_devfreq);
  306. /**
  307. * devfreq_monitor() - Periodically poll devfreq objects.
  308. * @work: the work struct used to run devfreq_monitor periodically.
  309. *
  310. */
  311. static void devfreq_monitor(struct work_struct *work)
  312. {
  313. int err;
  314. struct devfreq *devfreq = container_of(work,
  315. struct devfreq, work.work);
  316. mutex_lock(&devfreq->lock);
  317. err = update_devfreq(devfreq);
  318. if (err)
  319. dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
  320. queue_delayed_work(devfreq_wq, &devfreq->work,
  321. msecs_to_jiffies(devfreq->profile->polling_ms));
  322. mutex_unlock(&devfreq->lock);
  323. }
  324. /**
  325. * devfreq_monitor_start() - Start load monitoring of devfreq instance
  326. * @devfreq: the devfreq instance.
  327. *
  328. * Helper function for starting devfreq device load monitoing. By
  329. * default delayed work based monitoring is supported. Function
  330. * to be called from governor in response to DEVFREQ_GOV_START
  331. * event when device is added to devfreq framework.
  332. */
  333. void devfreq_monitor_start(struct devfreq *devfreq)
  334. {
  335. INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
  336. if (devfreq->profile->polling_ms)
  337. queue_delayed_work(devfreq_wq, &devfreq->work,
  338. msecs_to_jiffies(devfreq->profile->polling_ms));
  339. }
  340. EXPORT_SYMBOL(devfreq_monitor_start);
  341. /**
  342. * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
  343. * @devfreq: the devfreq instance.
  344. *
  345. * Helper function to stop devfreq device load monitoing. Function
  346. * to be called from governor in response to DEVFREQ_GOV_STOP
  347. * event when device is removed from devfreq framework.
  348. */
  349. void devfreq_monitor_stop(struct devfreq *devfreq)
  350. {
  351. cancel_delayed_work_sync(&devfreq->work);
  352. }
  353. EXPORT_SYMBOL(devfreq_monitor_stop);
  354. /**
  355. * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
  356. * @devfreq: the devfreq instance.
  357. *
  358. * Helper function to suspend devfreq device load monitoing. Function
  359. * to be called from governor in response to DEVFREQ_GOV_SUSPEND
  360. * event or when polling interval is set to zero.
  361. *
  362. * Note: Though this function is same as devfreq_monitor_stop(),
  363. * intentionally kept separate to provide hooks for collecting
  364. * transition statistics.
  365. */
  366. void devfreq_monitor_suspend(struct devfreq *devfreq)
  367. {
  368. mutex_lock(&devfreq->lock);
  369. if (devfreq->stop_polling) {
  370. mutex_unlock(&devfreq->lock);
  371. return;
  372. }
  373. devfreq_update_status(devfreq, devfreq->previous_freq);
  374. devfreq->stop_polling = true;
  375. mutex_unlock(&devfreq->lock);
  376. cancel_delayed_work_sync(&devfreq->work);
  377. }
  378. EXPORT_SYMBOL(devfreq_monitor_suspend);
  379. /**
  380. * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
  381. * @devfreq: the devfreq instance.
  382. *
  383. * Helper function to resume devfreq device load monitoing. Function
  384. * to be called from governor in response to DEVFREQ_GOV_RESUME
  385. * event or when polling interval is set to non-zero.
  386. */
  387. void devfreq_monitor_resume(struct devfreq *devfreq)
  388. {
  389. unsigned long freq;
  390. mutex_lock(&devfreq->lock);
  391. if (!devfreq->stop_polling)
  392. goto out;
  393. if (!delayed_work_pending(&devfreq->work) &&
  394. devfreq->profile->polling_ms)
  395. queue_delayed_work(devfreq_wq, &devfreq->work,
  396. msecs_to_jiffies(devfreq->profile->polling_ms));
  397. devfreq->last_stat_updated = jiffies;
  398. devfreq->stop_polling = false;
  399. if (devfreq->profile->get_cur_freq &&
  400. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  401. devfreq->previous_freq = freq;
  402. out:
  403. mutex_unlock(&devfreq->lock);
  404. }
  405. EXPORT_SYMBOL(devfreq_monitor_resume);
  406. /**
  407. * devfreq_interval_update() - Update device devfreq monitoring interval
  408. * @devfreq: the devfreq instance.
  409. * @delay: new polling interval to be set.
  410. *
  411. * Helper function to set new load monitoring polling interval. Function
  412. * to be called from governor in response to DEVFREQ_GOV_INTERVAL event.
  413. */
  414. void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay)
  415. {
  416. unsigned int cur_delay = devfreq->profile->polling_ms;
  417. unsigned int new_delay = *delay;
  418. mutex_lock(&devfreq->lock);
  419. devfreq->profile->polling_ms = new_delay;
  420. if (devfreq->stop_polling)
  421. goto out;
  422. /* if new delay is zero, stop polling */
  423. if (!new_delay) {
  424. mutex_unlock(&devfreq->lock);
  425. cancel_delayed_work_sync(&devfreq->work);
  426. return;
  427. }
  428. /* if current delay is zero, start polling with new delay */
  429. if (!cur_delay) {
  430. queue_delayed_work(devfreq_wq, &devfreq->work,
  431. msecs_to_jiffies(devfreq->profile->polling_ms));
  432. goto out;
  433. }
  434. /* if current delay is greater than new delay, restart polling */
  435. if (cur_delay > new_delay) {
  436. mutex_unlock(&devfreq->lock);
  437. cancel_delayed_work_sync(&devfreq->work);
  438. mutex_lock(&devfreq->lock);
  439. if (!devfreq->stop_polling)
  440. queue_delayed_work(devfreq_wq, &devfreq->work,
  441. msecs_to_jiffies(devfreq->profile->polling_ms));
  442. }
  443. out:
  444. mutex_unlock(&devfreq->lock);
  445. }
  446. EXPORT_SYMBOL(devfreq_interval_update);
  447. /**
  448. * devfreq_notifier_call() - Notify that the device frequency requirements
  449. * has been changed out of devfreq framework.
  450. * @nb: the notifier_block (supposed to be devfreq->nb)
  451. * @type: not used
  452. * @devp: not used
  453. *
  454. * Called by a notifier that uses devfreq->nb.
  455. */
  456. static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
  457. void *devp)
  458. {
  459. struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
  460. int ret;
  461. mutex_lock(&devfreq->lock);
  462. devfreq->scaling_min_freq = find_available_min_freq(devfreq);
  463. if (!devfreq->scaling_min_freq) {
  464. mutex_unlock(&devfreq->lock);
  465. return -EINVAL;
  466. }
  467. devfreq->scaling_max_freq = find_available_max_freq(devfreq);
  468. if (!devfreq->scaling_max_freq) {
  469. mutex_unlock(&devfreq->lock);
  470. return -EINVAL;
  471. }
  472. ret = update_devfreq(devfreq);
  473. mutex_unlock(&devfreq->lock);
  474. return ret;
  475. }
  476. /**
  477. * devfreq_dev_release() - Callback for struct device to release the device.
  478. * @dev: the devfreq device
  479. *
  480. * Remove devfreq from the list and release its resources.
  481. */
  482. static void devfreq_dev_release(struct device *dev)
  483. {
  484. struct devfreq *devfreq = to_devfreq(dev);
  485. mutex_lock(&devfreq_list_lock);
  486. if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) {
  487. mutex_unlock(&devfreq_list_lock);
  488. dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n");
  489. return;
  490. }
  491. list_del(&devfreq->node);
  492. mutex_unlock(&devfreq_list_lock);
  493. if (devfreq->profile->exit)
  494. devfreq->profile->exit(devfreq->dev.parent);
  495. mutex_destroy(&devfreq->lock);
  496. kfree(devfreq);
  497. }
  498. /**
  499. * devfreq_add_device() - Add devfreq feature to the device
  500. * @dev: the device to add devfreq feature.
  501. * @profile: device-specific profile to run devfreq.
  502. * @governor_name: name of the policy to choose frequency.
  503. * @data: private data for the governor. The devfreq framework does not
  504. * touch this value.
  505. */
  506. struct devfreq *devfreq_add_device(struct device *dev,
  507. struct devfreq_dev_profile *profile,
  508. const char *governor_name,
  509. void *data)
  510. {
  511. struct devfreq *devfreq;
  512. struct devfreq_governor *governor;
  513. static atomic_t devfreq_no = ATOMIC_INIT(-1);
  514. int err = 0;
  515. if (!dev || !profile || !governor_name) {
  516. dev_err(dev, "%s: Invalid parameters.\n", __func__);
  517. return ERR_PTR(-EINVAL);
  518. }
  519. mutex_lock(&devfreq_list_lock);
  520. devfreq = find_device_devfreq(dev);
  521. mutex_unlock(&devfreq_list_lock);
  522. if (!IS_ERR(devfreq)) {
  523. dev_err(dev, "%s: Unable to create devfreq for the device.\n",
  524. __func__);
  525. err = -EINVAL;
  526. goto err_out;
  527. }
  528. devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
  529. if (!devfreq) {
  530. err = -ENOMEM;
  531. goto err_out;
  532. }
  533. mutex_init(&devfreq->lock);
  534. mutex_lock(&devfreq->lock);
  535. devfreq->dev.parent = dev;
  536. devfreq->dev.class = devfreq_class;
  537. devfreq->dev.release = devfreq_dev_release;
  538. devfreq->profile = profile;
  539. strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
  540. devfreq->previous_freq = profile->initial_freq;
  541. devfreq->last_status.current_frequency = profile->initial_freq;
  542. devfreq->data = data;
  543. devfreq->nb.notifier_call = devfreq_notifier_call;
  544. if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
  545. mutex_unlock(&devfreq->lock);
  546. err = set_freq_table(devfreq);
  547. if (err < 0)
  548. goto err_out;
  549. mutex_lock(&devfreq->lock);
  550. }
  551. devfreq->scaling_min_freq = find_available_min_freq(devfreq);
  552. if (!devfreq->scaling_min_freq) {
  553. mutex_unlock(&devfreq->lock);
  554. err = -EINVAL;
  555. goto err_dev;
  556. }
  557. devfreq->min_freq = devfreq->scaling_min_freq;
  558. devfreq->scaling_max_freq = find_available_max_freq(devfreq);
  559. if (!devfreq->scaling_max_freq) {
  560. mutex_unlock(&devfreq->lock);
  561. err = -EINVAL;
  562. goto err_dev;
  563. }
  564. devfreq->max_freq = devfreq->scaling_max_freq;
  565. dev_set_name(&devfreq->dev, "devfreq%d",
  566. atomic_inc_return(&devfreq_no));
  567. err = device_register(&devfreq->dev);
  568. if (err) {
  569. mutex_unlock(&devfreq->lock);
  570. put_device(&devfreq->dev);
  571. goto err_out;
  572. }
  573. devfreq->trans_table =
  574. devm_kzalloc(&devfreq->dev,
  575. array3_size(sizeof(unsigned int),
  576. devfreq->profile->max_state,
  577. devfreq->profile->max_state),
  578. GFP_KERNEL);
  579. devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
  580. devfreq->profile->max_state,
  581. sizeof(unsigned long),
  582. GFP_KERNEL);
  583. devfreq->last_stat_updated = jiffies;
  584. srcu_init_notifier_head(&devfreq->transition_notifier_list);
  585. mutex_unlock(&devfreq->lock);
  586. mutex_lock(&devfreq_list_lock);
  587. governor = try_then_request_governor(devfreq->governor_name);
  588. if (IS_ERR(governor)) {
  589. dev_err(dev, "%s: Unable to find governor for the device\n",
  590. __func__);
  591. err = PTR_ERR(governor);
  592. goto err_init;
  593. }
  594. devfreq->governor = governor;
  595. err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
  596. NULL);
  597. if (err) {
  598. dev_err(dev, "%s: Unable to start governor for the device\n",
  599. __func__);
  600. goto err_init;
  601. }
  602. list_add(&devfreq->node, &devfreq_list);
  603. mutex_unlock(&devfreq_list_lock);
  604. return devfreq;
  605. err_init:
  606. mutex_unlock(&devfreq_list_lock);
  607. devfreq_remove_device(devfreq);
  608. devfreq = NULL;
  609. err_dev:
  610. kfree(devfreq);
  611. err_out:
  612. return ERR_PTR(err);
  613. }
  614. EXPORT_SYMBOL(devfreq_add_device);
  615. /**
  616. * devfreq_remove_device() - Remove devfreq feature from a device.
  617. * @devfreq: the devfreq instance to be removed
  618. *
  619. * The opposite of devfreq_add_device().
  620. */
  621. int devfreq_remove_device(struct devfreq *devfreq)
  622. {
  623. if (!devfreq)
  624. return -EINVAL;
  625. if (devfreq->governor)
  626. devfreq->governor->event_handler(devfreq,
  627. DEVFREQ_GOV_STOP, NULL);
  628. device_unregister(&devfreq->dev);
  629. return 0;
  630. }
  631. EXPORT_SYMBOL(devfreq_remove_device);
  632. static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
  633. {
  634. struct devfreq **r = res;
  635. if (WARN_ON(!r || !*r))
  636. return 0;
  637. return *r == data;
  638. }
  639. static void devm_devfreq_dev_release(struct device *dev, void *res)
  640. {
  641. devfreq_remove_device(*(struct devfreq **)res);
  642. }
  643. /**
  644. * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
  645. * @dev: the device to add devfreq feature.
  646. * @profile: device-specific profile to run devfreq.
  647. * @governor_name: name of the policy to choose frequency.
  648. * @data: private data for the governor. The devfreq framework does not
  649. * touch this value.
  650. *
  651. * This function manages automatically the memory of devfreq device using device
  652. * resource management and simplify the free operation for memory of devfreq
  653. * device.
  654. */
  655. struct devfreq *devm_devfreq_add_device(struct device *dev,
  656. struct devfreq_dev_profile *profile,
  657. const char *governor_name,
  658. void *data)
  659. {
  660. struct devfreq **ptr, *devfreq;
  661. ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
  662. if (!ptr)
  663. return ERR_PTR(-ENOMEM);
  664. devfreq = devfreq_add_device(dev, profile, governor_name, data);
  665. if (IS_ERR(devfreq)) {
  666. devres_free(ptr);
  667. return devfreq;
  668. }
  669. *ptr = devfreq;
  670. devres_add(dev, ptr);
  671. return devfreq;
  672. }
  673. EXPORT_SYMBOL(devm_devfreq_add_device);
  674. #ifdef CONFIG_OF
  675. /*
  676. * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
  677. * @dev - instance to the given device
  678. * @index - index into list of devfreq
  679. *
  680. * return the instance of devfreq device
  681. */
  682. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  683. {
  684. struct device_node *node;
  685. struct devfreq *devfreq;
  686. if (!dev)
  687. return ERR_PTR(-EINVAL);
  688. if (!dev->of_node)
  689. return ERR_PTR(-EINVAL);
  690. node = of_parse_phandle(dev->of_node, "devfreq", index);
  691. if (!node)
  692. return ERR_PTR(-ENODEV);
  693. mutex_lock(&devfreq_list_lock);
  694. list_for_each_entry(devfreq, &devfreq_list, node) {
  695. if (devfreq->dev.parent
  696. && devfreq->dev.parent->of_node == node) {
  697. mutex_unlock(&devfreq_list_lock);
  698. of_node_put(node);
  699. return devfreq;
  700. }
  701. }
  702. mutex_unlock(&devfreq_list_lock);
  703. of_node_put(node);
  704. return ERR_PTR(-EPROBE_DEFER);
  705. }
  706. #else
  707. struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
  708. {
  709. return ERR_PTR(-ENODEV);
  710. }
  711. #endif /* CONFIG_OF */
  712. EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
  713. /**
  714. * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
  715. * @dev: the device to add devfreq feature.
  716. * @devfreq: the devfreq instance to be removed
  717. */
  718. void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
  719. {
  720. WARN_ON(devres_release(dev, devm_devfreq_dev_release,
  721. devm_devfreq_dev_match, devfreq));
  722. }
  723. EXPORT_SYMBOL(devm_devfreq_remove_device);
  724. /**
  725. * devfreq_suspend_device() - Suspend devfreq of a device.
  726. * @devfreq: the devfreq instance to be suspended
  727. *
  728. * This function is intended to be called by the pm callbacks
  729. * (e.g., runtime_suspend, suspend) of the device driver that
  730. * holds the devfreq.
  731. */
  732. int devfreq_suspend_device(struct devfreq *devfreq)
  733. {
  734. if (!devfreq)
  735. return -EINVAL;
  736. if (!devfreq->governor)
  737. return 0;
  738. return devfreq->governor->event_handler(devfreq,
  739. DEVFREQ_GOV_SUSPEND, NULL);
  740. }
  741. EXPORT_SYMBOL(devfreq_suspend_device);
  742. /**
  743. * devfreq_resume_device() - Resume devfreq of a device.
  744. * @devfreq: the devfreq instance to be resumed
  745. *
  746. * This function is intended to be called by the pm callbacks
  747. * (e.g., runtime_resume, resume) of the device driver that
  748. * holds the devfreq.
  749. */
  750. int devfreq_resume_device(struct devfreq *devfreq)
  751. {
  752. if (!devfreq)
  753. return -EINVAL;
  754. if (!devfreq->governor)
  755. return 0;
  756. return devfreq->governor->event_handler(devfreq,
  757. DEVFREQ_GOV_RESUME, NULL);
  758. }
  759. EXPORT_SYMBOL(devfreq_resume_device);
  760. /**
  761. * devfreq_add_governor() - Add devfreq governor
  762. * @governor: the devfreq governor to be added
  763. */
  764. int devfreq_add_governor(struct devfreq_governor *governor)
  765. {
  766. struct devfreq_governor *g;
  767. struct devfreq *devfreq;
  768. int err = 0;
  769. if (!governor) {
  770. pr_err("%s: Invalid parameters.\n", __func__);
  771. return -EINVAL;
  772. }
  773. mutex_lock(&devfreq_list_lock);
  774. g = find_devfreq_governor(governor->name);
  775. if (!IS_ERR(g)) {
  776. pr_err("%s: governor %s already registered\n", __func__,
  777. g->name);
  778. err = -EINVAL;
  779. goto err_out;
  780. }
  781. list_add(&governor->node, &devfreq_governor_list);
  782. list_for_each_entry(devfreq, &devfreq_list, node) {
  783. int ret = 0;
  784. struct device *dev = devfreq->dev.parent;
  785. if (!strncmp(devfreq->governor_name, governor->name,
  786. DEVFREQ_NAME_LEN)) {
  787. /* The following should never occur */
  788. if (devfreq->governor) {
  789. dev_warn(dev,
  790. "%s: Governor %s already present\n",
  791. __func__, devfreq->governor->name);
  792. ret = devfreq->governor->event_handler(devfreq,
  793. DEVFREQ_GOV_STOP, NULL);
  794. if (ret) {
  795. dev_warn(dev,
  796. "%s: Governor %s stop = %d\n",
  797. __func__,
  798. devfreq->governor->name, ret);
  799. }
  800. /* Fall through */
  801. }
  802. devfreq->governor = governor;
  803. ret = devfreq->governor->event_handler(devfreq,
  804. DEVFREQ_GOV_START, NULL);
  805. if (ret) {
  806. dev_warn(dev, "%s: Governor %s start=%d\n",
  807. __func__, devfreq->governor->name,
  808. ret);
  809. }
  810. }
  811. }
  812. err_out:
  813. mutex_unlock(&devfreq_list_lock);
  814. return err;
  815. }
  816. EXPORT_SYMBOL(devfreq_add_governor);
  817. /**
  818. * devfreq_remove_governor() - Remove devfreq feature from a device.
  819. * @governor: the devfreq governor to be removed
  820. */
  821. int devfreq_remove_governor(struct devfreq_governor *governor)
  822. {
  823. struct devfreq_governor *g;
  824. struct devfreq *devfreq;
  825. int err = 0;
  826. if (!governor) {
  827. pr_err("%s: Invalid parameters.\n", __func__);
  828. return -EINVAL;
  829. }
  830. mutex_lock(&devfreq_list_lock);
  831. g = find_devfreq_governor(governor->name);
  832. if (IS_ERR(g)) {
  833. pr_err("%s: governor %s not registered\n", __func__,
  834. governor->name);
  835. err = PTR_ERR(g);
  836. goto err_out;
  837. }
  838. list_for_each_entry(devfreq, &devfreq_list, node) {
  839. int ret;
  840. struct device *dev = devfreq->dev.parent;
  841. if (!strncmp(devfreq->governor_name, governor->name,
  842. DEVFREQ_NAME_LEN)) {
  843. /* we should have a devfreq governor! */
  844. if (!devfreq->governor) {
  845. dev_warn(dev, "%s: Governor %s NOT present\n",
  846. __func__, governor->name);
  847. continue;
  848. /* Fall through */
  849. }
  850. ret = devfreq->governor->event_handler(devfreq,
  851. DEVFREQ_GOV_STOP, NULL);
  852. if (ret) {
  853. dev_warn(dev, "%s: Governor %s stop=%d\n",
  854. __func__, devfreq->governor->name,
  855. ret);
  856. }
  857. devfreq->governor = NULL;
  858. }
  859. }
  860. list_del(&governor->node);
  861. err_out:
  862. mutex_unlock(&devfreq_list_lock);
  863. return err;
  864. }
  865. EXPORT_SYMBOL(devfreq_remove_governor);
  866. static ssize_t governor_show(struct device *dev,
  867. struct device_attribute *attr, char *buf)
  868. {
  869. if (!to_devfreq(dev)->governor)
  870. return -EINVAL;
  871. return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
  872. }
  873. static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
  874. const char *buf, size_t count)
  875. {
  876. struct devfreq *df = to_devfreq(dev);
  877. int ret;
  878. char str_governor[DEVFREQ_NAME_LEN + 1];
  879. struct devfreq_governor *governor;
  880. ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
  881. if (ret != 1)
  882. return -EINVAL;
  883. mutex_lock(&devfreq_list_lock);
  884. governor = try_then_request_governor(str_governor);
  885. if (IS_ERR(governor)) {
  886. ret = PTR_ERR(governor);
  887. goto out;
  888. }
  889. if (df->governor == governor) {
  890. ret = 0;
  891. goto out;
  892. } else if ((df->governor && df->governor->immutable) ||
  893. governor->immutable) {
  894. ret = -EINVAL;
  895. goto out;
  896. }
  897. if (df->governor) {
  898. ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
  899. if (ret) {
  900. dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
  901. __func__, df->governor->name, ret);
  902. goto out;
  903. }
  904. }
  905. df->governor = governor;
  906. strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
  907. ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
  908. if (ret)
  909. dev_warn(dev, "%s: Governor %s not started(%d)\n",
  910. __func__, df->governor->name, ret);
  911. out:
  912. mutex_unlock(&devfreq_list_lock);
  913. if (!ret)
  914. ret = count;
  915. return ret;
  916. }
  917. static DEVICE_ATTR_RW(governor);
  918. static ssize_t available_governors_show(struct device *d,
  919. struct device_attribute *attr,
  920. char *buf)
  921. {
  922. struct devfreq *df = to_devfreq(d);
  923. ssize_t count = 0;
  924. mutex_lock(&devfreq_list_lock);
  925. /*
  926. * The devfreq with immutable governor (e.g., passive) shows
  927. * only own governor.
  928. */
  929. if (df->governor->immutable) {
  930. count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
  931. "%s ", df->governor_name);
  932. /*
  933. * The devfreq device shows the registered governor except for
  934. * immutable governors such as passive governor .
  935. */
  936. } else {
  937. struct devfreq_governor *governor;
  938. list_for_each_entry(governor, &devfreq_governor_list, node) {
  939. if (governor->immutable)
  940. continue;
  941. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  942. "%s ", governor->name);
  943. }
  944. }
  945. mutex_unlock(&devfreq_list_lock);
  946. /* Truncate the trailing space */
  947. if (count)
  948. count--;
  949. count += sprintf(&buf[count], "\n");
  950. return count;
  951. }
  952. static DEVICE_ATTR_RO(available_governors);
  953. static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
  954. char *buf)
  955. {
  956. unsigned long freq;
  957. struct devfreq *devfreq = to_devfreq(dev);
  958. if (devfreq->profile->get_cur_freq &&
  959. !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
  960. return sprintf(buf, "%lu\n", freq);
  961. return sprintf(buf, "%lu\n", devfreq->previous_freq);
  962. }
  963. static DEVICE_ATTR_RO(cur_freq);
  964. static ssize_t target_freq_show(struct device *dev,
  965. struct device_attribute *attr, char *buf)
  966. {
  967. return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
  968. }
  969. static DEVICE_ATTR_RO(target_freq);
  970. static ssize_t polling_interval_show(struct device *dev,
  971. struct device_attribute *attr, char *buf)
  972. {
  973. return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
  974. }
  975. static ssize_t polling_interval_store(struct device *dev,
  976. struct device_attribute *attr,
  977. const char *buf, size_t count)
  978. {
  979. struct devfreq *df = to_devfreq(dev);
  980. unsigned int value;
  981. int ret;
  982. if (!df->governor)
  983. return -EINVAL;
  984. ret = sscanf(buf, "%u", &value);
  985. if (ret != 1)
  986. return -EINVAL;
  987. df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value);
  988. ret = count;
  989. return ret;
  990. }
  991. static DEVICE_ATTR_RW(polling_interval);
  992. static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
  993. const char *buf, size_t count)
  994. {
  995. struct devfreq *df = to_devfreq(dev);
  996. unsigned long value;
  997. int ret;
  998. ret = sscanf(buf, "%lu", &value);
  999. if (ret != 1)
  1000. return -EINVAL;
  1001. mutex_lock(&df->lock);
  1002. if (value) {
  1003. if (value > df->max_freq) {
  1004. ret = -EINVAL;
  1005. goto unlock;
  1006. }
  1007. } else {
  1008. unsigned long *freq_table = df->profile->freq_table;
  1009. /* Get minimum frequency according to sorting order */
  1010. if (freq_table[0] < freq_table[df->profile->max_state - 1])
  1011. value = freq_table[0];
  1012. else
  1013. value = freq_table[df->profile->max_state - 1];
  1014. }
  1015. df->min_freq = value;
  1016. update_devfreq(df);
  1017. ret = count;
  1018. unlock:
  1019. mutex_unlock(&df->lock);
  1020. return ret;
  1021. }
  1022. static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
  1023. char *buf)
  1024. {
  1025. struct devfreq *df = to_devfreq(dev);
  1026. return sprintf(buf, "%lu\n", max(df->scaling_min_freq, df->min_freq));
  1027. }
  1028. static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
  1029. const char *buf, size_t count)
  1030. {
  1031. struct devfreq *df = to_devfreq(dev);
  1032. unsigned long value;
  1033. int ret;
  1034. ret = sscanf(buf, "%lu", &value);
  1035. if (ret != 1)
  1036. return -EINVAL;
  1037. mutex_lock(&df->lock);
  1038. if (value) {
  1039. if (value < df->min_freq) {
  1040. ret = -EINVAL;
  1041. goto unlock;
  1042. }
  1043. } else {
  1044. unsigned long *freq_table = df->profile->freq_table;
  1045. /* Get maximum frequency according to sorting order */
  1046. if (freq_table[0] < freq_table[df->profile->max_state - 1])
  1047. value = freq_table[df->profile->max_state - 1];
  1048. else
  1049. value = freq_table[0];
  1050. }
  1051. df->max_freq = value;
  1052. update_devfreq(df);
  1053. ret = count;
  1054. unlock:
  1055. mutex_unlock(&df->lock);
  1056. return ret;
  1057. }
  1058. static DEVICE_ATTR_RW(min_freq);
  1059. static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
  1060. char *buf)
  1061. {
  1062. struct devfreq *df = to_devfreq(dev);
  1063. return sprintf(buf, "%lu\n", min(df->scaling_max_freq, df->max_freq));
  1064. }
  1065. static DEVICE_ATTR_RW(max_freq);
  1066. static ssize_t available_frequencies_show(struct device *d,
  1067. struct device_attribute *attr,
  1068. char *buf)
  1069. {
  1070. struct devfreq *df = to_devfreq(d);
  1071. ssize_t count = 0;
  1072. int i;
  1073. mutex_lock(&df->lock);
  1074. for (i = 0; i < df->profile->max_state; i++)
  1075. count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
  1076. "%lu ", df->profile->freq_table[i]);
  1077. mutex_unlock(&df->lock);
  1078. /* Truncate the trailing space */
  1079. if (count)
  1080. count--;
  1081. count += sprintf(&buf[count], "\n");
  1082. return count;
  1083. }
  1084. static DEVICE_ATTR_RO(available_frequencies);
  1085. static ssize_t trans_stat_show(struct device *dev,
  1086. struct device_attribute *attr, char *buf)
  1087. {
  1088. struct devfreq *devfreq = to_devfreq(dev);
  1089. ssize_t len;
  1090. int i, j;
  1091. unsigned int max_state = devfreq->profile->max_state;
  1092. if (!devfreq->stop_polling &&
  1093. devfreq_update_status(devfreq, devfreq->previous_freq))
  1094. return 0;
  1095. if (max_state == 0)
  1096. return sprintf(buf, "Not Supported.\n");
  1097. len = sprintf(buf, " From : To\n");
  1098. len += sprintf(buf + len, " :");
  1099. for (i = 0; i < max_state; i++)
  1100. len += sprintf(buf + len, "%10lu",
  1101. devfreq->profile->freq_table[i]);
  1102. len += sprintf(buf + len, " time(ms)\n");
  1103. for (i = 0; i < max_state; i++) {
  1104. if (devfreq->profile->freq_table[i]
  1105. == devfreq->previous_freq) {
  1106. len += sprintf(buf + len, "*");
  1107. } else {
  1108. len += sprintf(buf + len, " ");
  1109. }
  1110. len += sprintf(buf + len, "%10lu:",
  1111. devfreq->profile->freq_table[i]);
  1112. for (j = 0; j < max_state; j++)
  1113. len += sprintf(buf + len, "%10u",
  1114. devfreq->trans_table[(i * max_state) + j]);
  1115. len += sprintf(buf + len, "%10u\n",
  1116. jiffies_to_msecs(devfreq->time_in_state[i]));
  1117. }
  1118. len += sprintf(buf + len, "Total transition : %u\n",
  1119. devfreq->total_trans);
  1120. return len;
  1121. }
  1122. static DEVICE_ATTR_RO(trans_stat);
  1123. static struct attribute *devfreq_attrs[] = {
  1124. &dev_attr_governor.attr,
  1125. &dev_attr_available_governors.attr,
  1126. &dev_attr_cur_freq.attr,
  1127. &dev_attr_available_frequencies.attr,
  1128. &dev_attr_target_freq.attr,
  1129. &dev_attr_polling_interval.attr,
  1130. &dev_attr_min_freq.attr,
  1131. &dev_attr_max_freq.attr,
  1132. &dev_attr_trans_stat.attr,
  1133. NULL,
  1134. };
  1135. ATTRIBUTE_GROUPS(devfreq);
  1136. static int __init devfreq_init(void)
  1137. {
  1138. devfreq_class = class_create(THIS_MODULE, "devfreq");
  1139. if (IS_ERR(devfreq_class)) {
  1140. pr_err("%s: couldn't create class\n", __FILE__);
  1141. return PTR_ERR(devfreq_class);
  1142. }
  1143. devfreq_wq = create_freezable_workqueue("devfreq_wq");
  1144. if (!devfreq_wq) {
  1145. class_destroy(devfreq_class);
  1146. pr_err("%s: couldn't create workqueue\n", __FILE__);
  1147. return -ENOMEM;
  1148. }
  1149. devfreq_class->dev_groups = devfreq_groups;
  1150. return 0;
  1151. }
  1152. subsys_initcall(devfreq_init);
  1153. /*
  1154. * The following are helper functions for devfreq user device drivers with
  1155. * OPP framework.
  1156. */
  1157. /**
  1158. * devfreq_recommended_opp() - Helper function to get proper OPP for the
  1159. * freq value given to target callback.
  1160. * @dev: The devfreq user device. (parent of devfreq)
  1161. * @freq: The frequency given to target function
  1162. * @flags: Flags handed from devfreq framework.
  1163. *
  1164. * The callers are required to call dev_pm_opp_put() for the returned OPP after
  1165. * use.
  1166. */
  1167. struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
  1168. unsigned long *freq,
  1169. u32 flags)
  1170. {
  1171. struct dev_pm_opp *opp;
  1172. if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
  1173. /* The freq is an upper bound. opp should be lower */
  1174. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1175. /* If not available, use the closest opp */
  1176. if (opp == ERR_PTR(-ERANGE))
  1177. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1178. } else {
  1179. /* The freq is an lower bound. opp should be higher */
  1180. opp = dev_pm_opp_find_freq_ceil(dev, freq);
  1181. /* If not available, use the closest opp */
  1182. if (opp == ERR_PTR(-ERANGE))
  1183. opp = dev_pm_opp_find_freq_floor(dev, freq);
  1184. }
  1185. return opp;
  1186. }
  1187. EXPORT_SYMBOL(devfreq_recommended_opp);
  1188. /**
  1189. * devfreq_register_opp_notifier() - Helper function to get devfreq notified
  1190. * for any changes in the OPP availability
  1191. * changes
  1192. * @dev: The devfreq user device. (parent of devfreq)
  1193. * @devfreq: The devfreq object.
  1194. */
  1195. int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1196. {
  1197. return dev_pm_opp_register_notifier(dev, &devfreq->nb);
  1198. }
  1199. EXPORT_SYMBOL(devfreq_register_opp_notifier);
  1200. /**
  1201. * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
  1202. * notified for any changes in the OPP
  1203. * availability changes anymore.
  1204. * @dev: The devfreq user device. (parent of devfreq)
  1205. * @devfreq: The devfreq object.
  1206. *
  1207. * At exit() callback of devfreq_dev_profile, this must be included if
  1208. * devfreq_recommended_opp is used.
  1209. */
  1210. int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
  1211. {
  1212. return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
  1213. }
  1214. EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
  1215. static void devm_devfreq_opp_release(struct device *dev, void *res)
  1216. {
  1217. devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
  1218. }
  1219. /**
  1220. * devm_ devfreq_register_opp_notifier()
  1221. * - Resource-managed devfreq_register_opp_notifier()
  1222. * @dev: The devfreq user device. (parent of devfreq)
  1223. * @devfreq: The devfreq object.
  1224. */
  1225. int devm_devfreq_register_opp_notifier(struct device *dev,
  1226. struct devfreq *devfreq)
  1227. {
  1228. struct devfreq **ptr;
  1229. int ret;
  1230. ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
  1231. if (!ptr)
  1232. return -ENOMEM;
  1233. ret = devfreq_register_opp_notifier(dev, devfreq);
  1234. if (ret) {
  1235. devres_free(ptr);
  1236. return ret;
  1237. }
  1238. *ptr = devfreq;
  1239. devres_add(dev, ptr);
  1240. return 0;
  1241. }
  1242. EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
  1243. /**
  1244. * devm_devfreq_unregister_opp_notifier()
  1245. * - Resource-managed devfreq_unregister_opp_notifier()
  1246. * @dev: The devfreq user device. (parent of devfreq)
  1247. * @devfreq: The devfreq object.
  1248. */
  1249. void devm_devfreq_unregister_opp_notifier(struct device *dev,
  1250. struct devfreq *devfreq)
  1251. {
  1252. WARN_ON(devres_release(dev, devm_devfreq_opp_release,
  1253. devm_devfreq_dev_match, devfreq));
  1254. }
  1255. EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
  1256. /**
  1257. * devfreq_register_notifier() - Register a driver with devfreq
  1258. * @devfreq: The devfreq object.
  1259. * @nb: The notifier block to register.
  1260. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1261. */
  1262. int devfreq_register_notifier(struct devfreq *devfreq,
  1263. struct notifier_block *nb,
  1264. unsigned int list)
  1265. {
  1266. int ret = 0;
  1267. if (!devfreq)
  1268. return -EINVAL;
  1269. switch (list) {
  1270. case DEVFREQ_TRANSITION_NOTIFIER:
  1271. ret = srcu_notifier_chain_register(
  1272. &devfreq->transition_notifier_list, nb);
  1273. break;
  1274. default:
  1275. ret = -EINVAL;
  1276. }
  1277. return ret;
  1278. }
  1279. EXPORT_SYMBOL(devfreq_register_notifier);
  1280. /*
  1281. * devfreq_unregister_notifier() - Unregister a driver with devfreq
  1282. * @devfreq: The devfreq object.
  1283. * @nb: The notifier block to be unregistered.
  1284. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1285. */
  1286. int devfreq_unregister_notifier(struct devfreq *devfreq,
  1287. struct notifier_block *nb,
  1288. unsigned int list)
  1289. {
  1290. int ret = 0;
  1291. if (!devfreq)
  1292. return -EINVAL;
  1293. switch (list) {
  1294. case DEVFREQ_TRANSITION_NOTIFIER:
  1295. ret = srcu_notifier_chain_unregister(
  1296. &devfreq->transition_notifier_list, nb);
  1297. break;
  1298. default:
  1299. ret = -EINVAL;
  1300. }
  1301. return ret;
  1302. }
  1303. EXPORT_SYMBOL(devfreq_unregister_notifier);
  1304. struct devfreq_notifier_devres {
  1305. struct devfreq *devfreq;
  1306. struct notifier_block *nb;
  1307. unsigned int list;
  1308. };
  1309. static void devm_devfreq_notifier_release(struct device *dev, void *res)
  1310. {
  1311. struct devfreq_notifier_devres *this = res;
  1312. devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
  1313. }
  1314. /**
  1315. * devm_devfreq_register_notifier()
  1316. - Resource-managed devfreq_register_notifier()
  1317. * @dev: The devfreq user device. (parent of devfreq)
  1318. * @devfreq: The devfreq object.
  1319. * @nb: The notifier block to be unregistered.
  1320. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1321. */
  1322. int devm_devfreq_register_notifier(struct device *dev,
  1323. struct devfreq *devfreq,
  1324. struct notifier_block *nb,
  1325. unsigned int list)
  1326. {
  1327. struct devfreq_notifier_devres *ptr;
  1328. int ret;
  1329. ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
  1330. GFP_KERNEL);
  1331. if (!ptr)
  1332. return -ENOMEM;
  1333. ret = devfreq_register_notifier(devfreq, nb, list);
  1334. if (ret) {
  1335. devres_free(ptr);
  1336. return ret;
  1337. }
  1338. ptr->devfreq = devfreq;
  1339. ptr->nb = nb;
  1340. ptr->list = list;
  1341. devres_add(dev, ptr);
  1342. return 0;
  1343. }
  1344. EXPORT_SYMBOL(devm_devfreq_register_notifier);
  1345. /**
  1346. * devm_devfreq_unregister_notifier()
  1347. - Resource-managed devfreq_unregister_notifier()
  1348. * @dev: The devfreq user device. (parent of devfreq)
  1349. * @devfreq: The devfreq object.
  1350. * @nb: The notifier block to be unregistered.
  1351. * @list: DEVFREQ_TRANSITION_NOTIFIER.
  1352. */
  1353. void devm_devfreq_unregister_notifier(struct device *dev,
  1354. struct devfreq *devfreq,
  1355. struct notifier_block *nb,
  1356. unsigned int list)
  1357. {
  1358. WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
  1359. devm_devfreq_dev_match, devfreq));
  1360. }
  1361. EXPORT_SYMBOL(devm_devfreq_unregister_notifier);