devfreq.c 38 KB

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