clk.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217
  1. /*
  2. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  3. * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/clk/clk-conf.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/err.h>
  18. #include <linux/list.h>
  19. #include <linux/slab.h>
  20. #include <linux/of.h>
  21. #include <linux/device.h>
  22. #include <linux/init.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/sched.h>
  25. #include <linux/clkdev.h>
  26. #include "clk.h"
  27. static DEFINE_SPINLOCK(enable_lock);
  28. static DEFINE_MUTEX(prepare_lock);
  29. static struct task_struct *prepare_owner;
  30. static struct task_struct *enable_owner;
  31. static int prepare_refcnt;
  32. static int enable_refcnt;
  33. static HLIST_HEAD(clk_root_list);
  34. static HLIST_HEAD(clk_orphan_list);
  35. static LIST_HEAD(clk_notifier_list);
  36. static struct hlist_head *all_lists[] = {
  37. &clk_root_list,
  38. &clk_orphan_list,
  39. NULL,
  40. };
  41. static struct hlist_head *orphan_list[] = {
  42. &clk_orphan_list,
  43. NULL,
  44. };
  45. /*** private data structures ***/
  46. struct clk_core {
  47. const char *name;
  48. const struct clk_ops *ops;
  49. struct clk_hw *hw;
  50. struct module *owner;
  51. struct device *dev;
  52. struct clk_core *parent;
  53. const char **parent_names;
  54. struct clk_core **parents;
  55. u8 num_parents;
  56. u8 new_parent_index;
  57. unsigned long rate;
  58. unsigned long req_rate;
  59. unsigned long new_rate;
  60. struct clk_core *new_parent;
  61. struct clk_core *new_child;
  62. unsigned long flags;
  63. bool orphan;
  64. unsigned int enable_count;
  65. unsigned int prepare_count;
  66. unsigned int protect_count;
  67. unsigned long min_rate;
  68. unsigned long max_rate;
  69. unsigned long accuracy;
  70. int phase;
  71. struct clk_duty duty;
  72. struct hlist_head children;
  73. struct hlist_node child_node;
  74. struct hlist_head clks;
  75. unsigned int notifier_count;
  76. #ifdef CONFIG_DEBUG_FS
  77. struct dentry *dentry;
  78. struct hlist_node debug_node;
  79. #endif
  80. struct kref ref;
  81. };
  82. #define CREATE_TRACE_POINTS
  83. #include <trace/events/clk.h>
  84. struct clk {
  85. struct clk_core *core;
  86. const char *dev_id;
  87. const char *con_id;
  88. unsigned long min_rate;
  89. unsigned long max_rate;
  90. unsigned int exclusive_count;
  91. struct hlist_node clks_node;
  92. };
  93. /*** runtime pm ***/
  94. static int clk_pm_runtime_get(struct clk_core *core)
  95. {
  96. int ret = 0;
  97. if (!core->dev)
  98. return 0;
  99. ret = pm_runtime_get_sync(core->dev);
  100. if (ret < 0) {
  101. pm_runtime_put_noidle(core->dev);
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. static void clk_pm_runtime_put(struct clk_core *core)
  107. {
  108. if (!core->dev)
  109. return;
  110. pm_runtime_put_sync(core->dev);
  111. }
  112. /*** locking ***/
  113. static void clk_prepare_lock(void)
  114. {
  115. if (!mutex_trylock(&prepare_lock)) {
  116. if (prepare_owner == current) {
  117. prepare_refcnt++;
  118. return;
  119. }
  120. mutex_lock(&prepare_lock);
  121. }
  122. WARN_ON_ONCE(prepare_owner != NULL);
  123. WARN_ON_ONCE(prepare_refcnt != 0);
  124. prepare_owner = current;
  125. prepare_refcnt = 1;
  126. }
  127. static void clk_prepare_unlock(void)
  128. {
  129. WARN_ON_ONCE(prepare_owner != current);
  130. WARN_ON_ONCE(prepare_refcnt == 0);
  131. if (--prepare_refcnt)
  132. return;
  133. prepare_owner = NULL;
  134. mutex_unlock(&prepare_lock);
  135. }
  136. static unsigned long clk_enable_lock(void)
  137. __acquires(enable_lock)
  138. {
  139. unsigned long flags;
  140. /*
  141. * On UP systems, spin_trylock_irqsave() always returns true, even if
  142. * we already hold the lock. So, in that case, we rely only on
  143. * reference counting.
  144. */
  145. if (!IS_ENABLED(CONFIG_SMP) ||
  146. !spin_trylock_irqsave(&enable_lock, flags)) {
  147. if (enable_owner == current) {
  148. enable_refcnt++;
  149. __acquire(enable_lock);
  150. if (!IS_ENABLED(CONFIG_SMP))
  151. local_save_flags(flags);
  152. return flags;
  153. }
  154. spin_lock_irqsave(&enable_lock, flags);
  155. }
  156. WARN_ON_ONCE(enable_owner != NULL);
  157. WARN_ON_ONCE(enable_refcnt != 0);
  158. enable_owner = current;
  159. enable_refcnt = 1;
  160. return flags;
  161. }
  162. static void clk_enable_unlock(unsigned long flags)
  163. __releases(enable_lock)
  164. {
  165. WARN_ON_ONCE(enable_owner != current);
  166. WARN_ON_ONCE(enable_refcnt == 0);
  167. if (--enable_refcnt) {
  168. __release(enable_lock);
  169. return;
  170. }
  171. enable_owner = NULL;
  172. spin_unlock_irqrestore(&enable_lock, flags);
  173. }
  174. static bool clk_core_rate_is_protected(struct clk_core *core)
  175. {
  176. return core->protect_count;
  177. }
  178. static bool clk_core_is_prepared(struct clk_core *core)
  179. {
  180. bool ret = false;
  181. /*
  182. * .is_prepared is optional for clocks that can prepare
  183. * fall back to software usage counter if it is missing
  184. */
  185. if (!core->ops->is_prepared)
  186. return core->prepare_count;
  187. if (!clk_pm_runtime_get(core)) {
  188. ret = core->ops->is_prepared(core->hw);
  189. clk_pm_runtime_put(core);
  190. }
  191. return ret;
  192. }
  193. static bool clk_core_is_enabled(struct clk_core *core)
  194. {
  195. bool ret = false;
  196. /*
  197. * .is_enabled is only mandatory for clocks that gate
  198. * fall back to software usage counter if .is_enabled is missing
  199. */
  200. if (!core->ops->is_enabled)
  201. return core->enable_count;
  202. /*
  203. * Check if clock controller's device is runtime active before
  204. * calling .is_enabled callback. If not, assume that clock is
  205. * disabled, because we might be called from atomic context, from
  206. * which pm_runtime_get() is not allowed.
  207. * This function is called mainly from clk_disable_unused_subtree,
  208. * which ensures proper runtime pm activation of controller before
  209. * taking enable spinlock, but the below check is needed if one tries
  210. * to call it from other places.
  211. */
  212. if (core->dev) {
  213. pm_runtime_get_noresume(core->dev);
  214. if (!pm_runtime_active(core->dev)) {
  215. ret = false;
  216. goto done;
  217. }
  218. }
  219. ret = core->ops->is_enabled(core->hw);
  220. done:
  221. if (core->dev)
  222. pm_runtime_put(core->dev);
  223. return ret;
  224. }
  225. /*** helper functions ***/
  226. const char *__clk_get_name(const struct clk *clk)
  227. {
  228. return !clk ? NULL : clk->core->name;
  229. }
  230. EXPORT_SYMBOL_GPL(__clk_get_name);
  231. const char *clk_hw_get_name(const struct clk_hw *hw)
  232. {
  233. return hw->core->name;
  234. }
  235. EXPORT_SYMBOL_GPL(clk_hw_get_name);
  236. struct clk_hw *__clk_get_hw(struct clk *clk)
  237. {
  238. return !clk ? NULL : clk->core->hw;
  239. }
  240. EXPORT_SYMBOL_GPL(__clk_get_hw);
  241. unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
  242. {
  243. return hw->core->num_parents;
  244. }
  245. EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
  246. struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
  247. {
  248. return hw->core->parent ? hw->core->parent->hw : NULL;
  249. }
  250. EXPORT_SYMBOL_GPL(clk_hw_get_parent);
  251. static struct clk_core *__clk_lookup_subtree(const char *name,
  252. struct clk_core *core)
  253. {
  254. struct clk_core *child;
  255. struct clk_core *ret;
  256. if (!strcmp(core->name, name))
  257. return core;
  258. hlist_for_each_entry(child, &core->children, child_node) {
  259. ret = __clk_lookup_subtree(name, child);
  260. if (ret)
  261. return ret;
  262. }
  263. return NULL;
  264. }
  265. static struct clk_core *clk_core_lookup(const char *name)
  266. {
  267. struct clk_core *root_clk;
  268. struct clk_core *ret;
  269. if (!name)
  270. return NULL;
  271. /* search the 'proper' clk tree first */
  272. hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
  273. ret = __clk_lookup_subtree(name, root_clk);
  274. if (ret)
  275. return ret;
  276. }
  277. /* if not found, then search the orphan tree */
  278. hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
  279. ret = __clk_lookup_subtree(name, root_clk);
  280. if (ret)
  281. return ret;
  282. }
  283. return NULL;
  284. }
  285. static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
  286. u8 index)
  287. {
  288. if (!core || index >= core->num_parents)
  289. return NULL;
  290. if (!core->parents[index])
  291. core->parents[index] =
  292. clk_core_lookup(core->parent_names[index]);
  293. return core->parents[index];
  294. }
  295. struct clk_hw *
  296. clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
  297. {
  298. struct clk_core *parent;
  299. parent = clk_core_get_parent_by_index(hw->core, index);
  300. return !parent ? NULL : parent->hw;
  301. }
  302. EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
  303. unsigned int __clk_get_enable_count(struct clk *clk)
  304. {
  305. return !clk ? 0 : clk->core->enable_count;
  306. }
  307. static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
  308. {
  309. unsigned long ret;
  310. if (!core) {
  311. ret = 0;
  312. goto out;
  313. }
  314. ret = core->rate;
  315. if (!core->num_parents)
  316. goto out;
  317. if (!core->parent)
  318. ret = 0;
  319. out:
  320. return ret;
  321. }
  322. unsigned long clk_hw_get_rate(const struct clk_hw *hw)
  323. {
  324. return clk_core_get_rate_nolock(hw->core);
  325. }
  326. EXPORT_SYMBOL_GPL(clk_hw_get_rate);
  327. static unsigned long __clk_get_accuracy(struct clk_core *core)
  328. {
  329. if (!core)
  330. return 0;
  331. return core->accuracy;
  332. }
  333. unsigned long __clk_get_flags(struct clk *clk)
  334. {
  335. return !clk ? 0 : clk->core->flags;
  336. }
  337. EXPORT_SYMBOL_GPL(__clk_get_flags);
  338. unsigned long clk_hw_get_flags(const struct clk_hw *hw)
  339. {
  340. return hw->core->flags;
  341. }
  342. EXPORT_SYMBOL_GPL(clk_hw_get_flags);
  343. bool clk_hw_is_prepared(const struct clk_hw *hw)
  344. {
  345. return clk_core_is_prepared(hw->core);
  346. }
  347. bool clk_hw_rate_is_protected(const struct clk_hw *hw)
  348. {
  349. return clk_core_rate_is_protected(hw->core);
  350. }
  351. bool clk_hw_is_enabled(const struct clk_hw *hw)
  352. {
  353. return clk_core_is_enabled(hw->core);
  354. }
  355. bool __clk_is_enabled(struct clk *clk)
  356. {
  357. if (!clk)
  358. return false;
  359. return clk_core_is_enabled(clk->core);
  360. }
  361. EXPORT_SYMBOL_GPL(__clk_is_enabled);
  362. static bool mux_is_better_rate(unsigned long rate, unsigned long now,
  363. unsigned long best, unsigned long flags)
  364. {
  365. if (flags & CLK_MUX_ROUND_CLOSEST)
  366. return abs(now - rate) < abs(best - rate);
  367. return now <= rate && now > best;
  368. }
  369. int clk_mux_determine_rate_flags(struct clk_hw *hw,
  370. struct clk_rate_request *req,
  371. unsigned long flags)
  372. {
  373. struct clk_core *core = hw->core, *parent, *best_parent = NULL;
  374. int i, num_parents, ret;
  375. unsigned long best = 0;
  376. struct clk_rate_request parent_req = *req;
  377. /* if NO_REPARENT flag set, pass through to current parent */
  378. if (core->flags & CLK_SET_RATE_NO_REPARENT) {
  379. parent = core->parent;
  380. if (core->flags & CLK_SET_RATE_PARENT) {
  381. ret = __clk_determine_rate(parent ? parent->hw : NULL,
  382. &parent_req);
  383. if (ret)
  384. return ret;
  385. best = parent_req.rate;
  386. } else if (parent) {
  387. best = clk_core_get_rate_nolock(parent);
  388. } else {
  389. best = clk_core_get_rate_nolock(core);
  390. }
  391. goto out;
  392. }
  393. /* find the parent that can provide the fastest rate <= rate */
  394. num_parents = core->num_parents;
  395. for (i = 0; i < num_parents; i++) {
  396. parent = clk_core_get_parent_by_index(core, i);
  397. if (!parent)
  398. continue;
  399. if (core->flags & CLK_SET_RATE_PARENT) {
  400. parent_req = *req;
  401. ret = __clk_determine_rate(parent->hw, &parent_req);
  402. if (ret)
  403. continue;
  404. } else {
  405. parent_req.rate = clk_core_get_rate_nolock(parent);
  406. }
  407. if (mux_is_better_rate(req->rate, parent_req.rate,
  408. best, flags)) {
  409. best_parent = parent;
  410. best = parent_req.rate;
  411. }
  412. }
  413. if (!best_parent)
  414. return -EINVAL;
  415. out:
  416. if (best_parent)
  417. req->best_parent_hw = best_parent->hw;
  418. req->best_parent_rate = best;
  419. req->rate = best;
  420. return 0;
  421. }
  422. EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
  423. struct clk *__clk_lookup(const char *name)
  424. {
  425. struct clk_core *core = clk_core_lookup(name);
  426. return !core ? NULL : core->hw->clk;
  427. }
  428. static void clk_core_get_boundaries(struct clk_core *core,
  429. unsigned long *min_rate,
  430. unsigned long *max_rate)
  431. {
  432. struct clk *clk_user;
  433. *min_rate = core->min_rate;
  434. *max_rate = core->max_rate;
  435. hlist_for_each_entry(clk_user, &core->clks, clks_node)
  436. *min_rate = max(*min_rate, clk_user->min_rate);
  437. hlist_for_each_entry(clk_user, &core->clks, clks_node)
  438. *max_rate = min(*max_rate, clk_user->max_rate);
  439. }
  440. void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
  441. unsigned long max_rate)
  442. {
  443. hw->core->min_rate = min_rate;
  444. hw->core->max_rate = max_rate;
  445. }
  446. EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
  447. /*
  448. * Helper for finding best parent to provide a given frequency. This can be used
  449. * directly as a determine_rate callback (e.g. for a mux), or from a more
  450. * complex clock that may combine a mux with other operations.
  451. */
  452. int __clk_mux_determine_rate(struct clk_hw *hw,
  453. struct clk_rate_request *req)
  454. {
  455. return clk_mux_determine_rate_flags(hw, req, 0);
  456. }
  457. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
  458. int __clk_mux_determine_rate_closest(struct clk_hw *hw,
  459. struct clk_rate_request *req)
  460. {
  461. return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
  462. }
  463. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
  464. /*** clk api ***/
  465. static void clk_core_rate_unprotect(struct clk_core *core)
  466. {
  467. lockdep_assert_held(&prepare_lock);
  468. if (!core)
  469. return;
  470. if (WARN(core->protect_count == 0,
  471. "%s already unprotected\n", core->name))
  472. return;
  473. if (--core->protect_count > 0)
  474. return;
  475. clk_core_rate_unprotect(core->parent);
  476. }
  477. static int clk_core_rate_nuke_protect(struct clk_core *core)
  478. {
  479. int ret;
  480. lockdep_assert_held(&prepare_lock);
  481. if (!core)
  482. return -EINVAL;
  483. if (core->protect_count == 0)
  484. return 0;
  485. ret = core->protect_count;
  486. core->protect_count = 1;
  487. clk_core_rate_unprotect(core);
  488. return ret;
  489. }
  490. /**
  491. * clk_rate_exclusive_put - release exclusivity over clock rate control
  492. * @clk: the clk over which the exclusivity is released
  493. *
  494. * clk_rate_exclusive_put() completes a critical section during which a clock
  495. * consumer cannot tolerate any other consumer making any operation on the
  496. * clock which could result in a rate change or rate glitch. Exclusive clocks
  497. * cannot have their rate changed, either directly or indirectly due to changes
  498. * further up the parent chain of clocks. As a result, clocks up parent chain
  499. * also get under exclusive control of the calling consumer.
  500. *
  501. * If exlusivity is claimed more than once on clock, even by the same consumer,
  502. * the rate effectively gets locked as exclusivity can't be preempted.
  503. *
  504. * Calls to clk_rate_exclusive_put() must be balanced with calls to
  505. * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
  506. * error status.
  507. */
  508. void clk_rate_exclusive_put(struct clk *clk)
  509. {
  510. if (!clk)
  511. return;
  512. clk_prepare_lock();
  513. /*
  514. * if there is something wrong with this consumer protect count, stop
  515. * here before messing with the provider
  516. */
  517. if (WARN_ON(clk->exclusive_count <= 0))
  518. goto out;
  519. clk_core_rate_unprotect(clk->core);
  520. clk->exclusive_count--;
  521. out:
  522. clk_prepare_unlock();
  523. }
  524. EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
  525. static void clk_core_rate_protect(struct clk_core *core)
  526. {
  527. lockdep_assert_held(&prepare_lock);
  528. if (!core)
  529. return;
  530. if (core->protect_count == 0)
  531. clk_core_rate_protect(core->parent);
  532. core->protect_count++;
  533. }
  534. static void clk_core_rate_restore_protect(struct clk_core *core, int count)
  535. {
  536. lockdep_assert_held(&prepare_lock);
  537. if (!core)
  538. return;
  539. if (count == 0)
  540. return;
  541. clk_core_rate_protect(core);
  542. core->protect_count = count;
  543. }
  544. /**
  545. * clk_rate_exclusive_get - get exclusivity over the clk rate control
  546. * @clk: the clk over which the exclusity of rate control is requested
  547. *
  548. * clk_rate_exlusive_get() begins a critical section during which a clock
  549. * consumer cannot tolerate any other consumer making any operation on the
  550. * clock which could result in a rate change or rate glitch. Exclusive clocks
  551. * cannot have their rate changed, either directly or indirectly due to changes
  552. * further up the parent chain of clocks. As a result, clocks up parent chain
  553. * also get under exclusive control of the calling consumer.
  554. *
  555. * If exlusivity is claimed more than once on clock, even by the same consumer,
  556. * the rate effectively gets locked as exclusivity can't be preempted.
  557. *
  558. * Calls to clk_rate_exclusive_get() should be balanced with calls to
  559. * clk_rate_exclusive_put(). Calls to this function may sleep.
  560. * Returns 0 on success, -EERROR otherwise
  561. */
  562. int clk_rate_exclusive_get(struct clk *clk)
  563. {
  564. if (!clk)
  565. return 0;
  566. clk_prepare_lock();
  567. clk_core_rate_protect(clk->core);
  568. clk->exclusive_count++;
  569. clk_prepare_unlock();
  570. return 0;
  571. }
  572. EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
  573. static void clk_core_unprepare(struct clk_core *core)
  574. {
  575. lockdep_assert_held(&prepare_lock);
  576. if (!core)
  577. return;
  578. if (WARN(core->prepare_count == 0,
  579. "%s already unprepared\n", core->name))
  580. return;
  581. if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
  582. "Unpreparing critical %s\n", core->name))
  583. return;
  584. if (core->flags & CLK_SET_RATE_GATE)
  585. clk_core_rate_unprotect(core);
  586. if (--core->prepare_count > 0)
  587. return;
  588. WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
  589. trace_clk_unprepare(core);
  590. if (core->ops->unprepare)
  591. core->ops->unprepare(core->hw);
  592. clk_pm_runtime_put(core);
  593. trace_clk_unprepare_complete(core);
  594. clk_core_unprepare(core->parent);
  595. }
  596. static void clk_core_unprepare_lock(struct clk_core *core)
  597. {
  598. clk_prepare_lock();
  599. clk_core_unprepare(core);
  600. clk_prepare_unlock();
  601. }
  602. /**
  603. * clk_unprepare - undo preparation of a clock source
  604. * @clk: the clk being unprepared
  605. *
  606. * clk_unprepare may sleep, which differentiates it from clk_disable. In a
  607. * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
  608. * if the operation may sleep. One example is a clk which is accessed over
  609. * I2c. In the complex case a clk gate operation may require a fast and a slow
  610. * part. It is this reason that clk_unprepare and clk_disable are not mutually
  611. * exclusive. In fact clk_disable must be called before clk_unprepare.
  612. */
  613. void clk_unprepare(struct clk *clk)
  614. {
  615. if (IS_ERR_OR_NULL(clk))
  616. return;
  617. clk_core_unprepare_lock(clk->core);
  618. }
  619. EXPORT_SYMBOL_GPL(clk_unprepare);
  620. static int clk_core_prepare(struct clk_core *core)
  621. {
  622. int ret = 0;
  623. lockdep_assert_held(&prepare_lock);
  624. if (!core)
  625. return 0;
  626. if (core->prepare_count == 0) {
  627. ret = clk_pm_runtime_get(core);
  628. if (ret)
  629. return ret;
  630. ret = clk_core_prepare(core->parent);
  631. if (ret)
  632. goto runtime_put;
  633. trace_clk_prepare(core);
  634. if (core->ops->prepare)
  635. ret = core->ops->prepare(core->hw);
  636. trace_clk_prepare_complete(core);
  637. if (ret)
  638. goto unprepare;
  639. }
  640. core->prepare_count++;
  641. /*
  642. * CLK_SET_RATE_GATE is a special case of clock protection
  643. * Instead of a consumer claiming exclusive rate control, it is
  644. * actually the provider which prevents any consumer from making any
  645. * operation which could result in a rate change or rate glitch while
  646. * the clock is prepared.
  647. */
  648. if (core->flags & CLK_SET_RATE_GATE)
  649. clk_core_rate_protect(core);
  650. return 0;
  651. unprepare:
  652. clk_core_unprepare(core->parent);
  653. runtime_put:
  654. clk_pm_runtime_put(core);
  655. return ret;
  656. }
  657. static int clk_core_prepare_lock(struct clk_core *core)
  658. {
  659. int ret;
  660. clk_prepare_lock();
  661. ret = clk_core_prepare(core);
  662. clk_prepare_unlock();
  663. return ret;
  664. }
  665. /**
  666. * clk_prepare - prepare a clock source
  667. * @clk: the clk being prepared
  668. *
  669. * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
  670. * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
  671. * operation may sleep. One example is a clk which is accessed over I2c. In
  672. * the complex case a clk ungate operation may require a fast and a slow part.
  673. * It is this reason that clk_prepare and clk_enable are not mutually
  674. * exclusive. In fact clk_prepare must be called before clk_enable.
  675. * Returns 0 on success, -EERROR otherwise.
  676. */
  677. int clk_prepare(struct clk *clk)
  678. {
  679. if (!clk)
  680. return 0;
  681. return clk_core_prepare_lock(clk->core);
  682. }
  683. EXPORT_SYMBOL_GPL(clk_prepare);
  684. static void clk_core_disable(struct clk_core *core)
  685. {
  686. lockdep_assert_held(&enable_lock);
  687. if (!core)
  688. return;
  689. if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
  690. return;
  691. if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
  692. "Disabling critical %s\n", core->name))
  693. return;
  694. if (--core->enable_count > 0)
  695. return;
  696. trace_clk_disable_rcuidle(core);
  697. if (core->ops->disable)
  698. core->ops->disable(core->hw);
  699. trace_clk_disable_complete_rcuidle(core);
  700. clk_core_disable(core->parent);
  701. }
  702. static void clk_core_disable_lock(struct clk_core *core)
  703. {
  704. unsigned long flags;
  705. flags = clk_enable_lock();
  706. clk_core_disable(core);
  707. clk_enable_unlock(flags);
  708. }
  709. /**
  710. * clk_disable - gate a clock
  711. * @clk: the clk being gated
  712. *
  713. * clk_disable must not sleep, which differentiates it from clk_unprepare. In
  714. * a simple case, clk_disable can be used instead of clk_unprepare to gate a
  715. * clk if the operation is fast and will never sleep. One example is a
  716. * SoC-internal clk which is controlled via simple register writes. In the
  717. * complex case a clk gate operation may require a fast and a slow part. It is
  718. * this reason that clk_unprepare and clk_disable are not mutually exclusive.
  719. * In fact clk_disable must be called before clk_unprepare.
  720. */
  721. void clk_disable(struct clk *clk)
  722. {
  723. if (IS_ERR_OR_NULL(clk))
  724. return;
  725. clk_core_disable_lock(clk->core);
  726. }
  727. EXPORT_SYMBOL_GPL(clk_disable);
  728. static int clk_core_enable(struct clk_core *core)
  729. {
  730. int ret = 0;
  731. lockdep_assert_held(&enable_lock);
  732. if (!core)
  733. return 0;
  734. if (WARN(core->prepare_count == 0,
  735. "Enabling unprepared %s\n", core->name))
  736. return -ESHUTDOWN;
  737. if (core->enable_count == 0) {
  738. ret = clk_core_enable(core->parent);
  739. if (ret)
  740. return ret;
  741. trace_clk_enable_rcuidle(core);
  742. if (core->ops->enable)
  743. ret = core->ops->enable(core->hw);
  744. trace_clk_enable_complete_rcuidle(core);
  745. if (ret) {
  746. clk_core_disable(core->parent);
  747. return ret;
  748. }
  749. }
  750. core->enable_count++;
  751. return 0;
  752. }
  753. static int clk_core_enable_lock(struct clk_core *core)
  754. {
  755. unsigned long flags;
  756. int ret;
  757. flags = clk_enable_lock();
  758. ret = clk_core_enable(core);
  759. clk_enable_unlock(flags);
  760. return ret;
  761. }
  762. /**
  763. * clk_enable - ungate a clock
  764. * @clk: the clk being ungated
  765. *
  766. * clk_enable must not sleep, which differentiates it from clk_prepare. In a
  767. * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
  768. * if the operation will never sleep. One example is a SoC-internal clk which
  769. * is controlled via simple register writes. In the complex case a clk ungate
  770. * operation may require a fast and a slow part. It is this reason that
  771. * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
  772. * must be called before clk_enable. Returns 0 on success, -EERROR
  773. * otherwise.
  774. */
  775. int clk_enable(struct clk *clk)
  776. {
  777. if (!clk)
  778. return 0;
  779. return clk_core_enable_lock(clk->core);
  780. }
  781. EXPORT_SYMBOL_GPL(clk_enable);
  782. static int clk_core_prepare_enable(struct clk_core *core)
  783. {
  784. int ret;
  785. ret = clk_core_prepare_lock(core);
  786. if (ret)
  787. return ret;
  788. ret = clk_core_enable_lock(core);
  789. if (ret)
  790. clk_core_unprepare_lock(core);
  791. return ret;
  792. }
  793. static void clk_core_disable_unprepare(struct clk_core *core)
  794. {
  795. clk_core_disable_lock(core);
  796. clk_core_unprepare_lock(core);
  797. }
  798. static void clk_unprepare_unused_subtree(struct clk_core *core)
  799. {
  800. struct clk_core *child;
  801. lockdep_assert_held(&prepare_lock);
  802. hlist_for_each_entry(child, &core->children, child_node)
  803. clk_unprepare_unused_subtree(child);
  804. if (core->prepare_count)
  805. return;
  806. if (core->flags & CLK_IGNORE_UNUSED)
  807. return;
  808. if (clk_pm_runtime_get(core))
  809. return;
  810. if (clk_core_is_prepared(core)) {
  811. trace_clk_unprepare(core);
  812. if (core->ops->unprepare_unused)
  813. core->ops->unprepare_unused(core->hw);
  814. else if (core->ops->unprepare)
  815. core->ops->unprepare(core->hw);
  816. trace_clk_unprepare_complete(core);
  817. }
  818. clk_pm_runtime_put(core);
  819. }
  820. static void clk_disable_unused_subtree(struct clk_core *core)
  821. {
  822. struct clk_core *child;
  823. unsigned long flags;
  824. lockdep_assert_held(&prepare_lock);
  825. hlist_for_each_entry(child, &core->children, child_node)
  826. clk_disable_unused_subtree(child);
  827. if (core->flags & CLK_OPS_PARENT_ENABLE)
  828. clk_core_prepare_enable(core->parent);
  829. if (clk_pm_runtime_get(core))
  830. goto unprepare_out;
  831. flags = clk_enable_lock();
  832. if (core->enable_count)
  833. goto unlock_out;
  834. if (core->flags & CLK_IGNORE_UNUSED)
  835. goto unlock_out;
  836. /*
  837. * some gate clocks have special needs during the disable-unused
  838. * sequence. call .disable_unused if available, otherwise fall
  839. * back to .disable
  840. */
  841. if (clk_core_is_enabled(core)) {
  842. trace_clk_disable(core);
  843. if (core->ops->disable_unused)
  844. core->ops->disable_unused(core->hw);
  845. else if (core->ops->disable)
  846. core->ops->disable(core->hw);
  847. trace_clk_disable_complete(core);
  848. }
  849. unlock_out:
  850. clk_enable_unlock(flags);
  851. clk_pm_runtime_put(core);
  852. unprepare_out:
  853. if (core->flags & CLK_OPS_PARENT_ENABLE)
  854. clk_core_disable_unprepare(core->parent);
  855. }
  856. static bool clk_ignore_unused;
  857. static int __init clk_ignore_unused_setup(char *__unused)
  858. {
  859. clk_ignore_unused = true;
  860. return 1;
  861. }
  862. __setup("clk_ignore_unused", clk_ignore_unused_setup);
  863. static int clk_disable_unused(void)
  864. {
  865. struct clk_core *core;
  866. if (clk_ignore_unused) {
  867. pr_warn("clk: Not disabling unused clocks\n");
  868. return 0;
  869. }
  870. clk_prepare_lock();
  871. hlist_for_each_entry(core, &clk_root_list, child_node)
  872. clk_disable_unused_subtree(core);
  873. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  874. clk_disable_unused_subtree(core);
  875. hlist_for_each_entry(core, &clk_root_list, child_node)
  876. clk_unprepare_unused_subtree(core);
  877. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  878. clk_unprepare_unused_subtree(core);
  879. clk_prepare_unlock();
  880. return 0;
  881. }
  882. late_initcall_sync(clk_disable_unused);
  883. static int clk_core_determine_round_nolock(struct clk_core *core,
  884. struct clk_rate_request *req)
  885. {
  886. long rate;
  887. lockdep_assert_held(&prepare_lock);
  888. if (!core)
  889. return 0;
  890. /*
  891. * At this point, core protection will be disabled if
  892. * - if the provider is not protected at all
  893. * - if the calling consumer is the only one which has exclusivity
  894. * over the provider
  895. */
  896. if (clk_core_rate_is_protected(core)) {
  897. req->rate = core->rate;
  898. } else if (core->ops->determine_rate) {
  899. return core->ops->determine_rate(core->hw, req);
  900. } else if (core->ops->round_rate) {
  901. rate = core->ops->round_rate(core->hw, req->rate,
  902. &req->best_parent_rate);
  903. if (rate < 0)
  904. return rate;
  905. req->rate = rate;
  906. } else {
  907. return -EINVAL;
  908. }
  909. return 0;
  910. }
  911. static void clk_core_init_rate_req(struct clk_core * const core,
  912. struct clk_rate_request *req)
  913. {
  914. struct clk_core *parent;
  915. if (WARN_ON(!core || !req))
  916. return;
  917. parent = core->parent;
  918. if (parent) {
  919. req->best_parent_hw = parent->hw;
  920. req->best_parent_rate = parent->rate;
  921. } else {
  922. req->best_parent_hw = NULL;
  923. req->best_parent_rate = 0;
  924. }
  925. }
  926. static bool clk_core_can_round(struct clk_core * const core)
  927. {
  928. if (core->ops->determine_rate || core->ops->round_rate)
  929. return true;
  930. return false;
  931. }
  932. static int clk_core_round_rate_nolock(struct clk_core *core,
  933. struct clk_rate_request *req)
  934. {
  935. lockdep_assert_held(&prepare_lock);
  936. if (!core) {
  937. req->rate = 0;
  938. return 0;
  939. }
  940. clk_core_init_rate_req(core, req);
  941. if (clk_core_can_round(core))
  942. return clk_core_determine_round_nolock(core, req);
  943. else if (core->flags & CLK_SET_RATE_PARENT)
  944. return clk_core_round_rate_nolock(core->parent, req);
  945. req->rate = core->rate;
  946. return 0;
  947. }
  948. /**
  949. * __clk_determine_rate - get the closest rate actually supported by a clock
  950. * @hw: determine the rate of this clock
  951. * @req: target rate request
  952. *
  953. * Useful for clk_ops such as .set_rate and .determine_rate.
  954. */
  955. int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  956. {
  957. if (!hw) {
  958. req->rate = 0;
  959. return 0;
  960. }
  961. return clk_core_round_rate_nolock(hw->core, req);
  962. }
  963. EXPORT_SYMBOL_GPL(__clk_determine_rate);
  964. unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
  965. {
  966. int ret;
  967. struct clk_rate_request req;
  968. clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
  969. req.rate = rate;
  970. ret = clk_core_round_rate_nolock(hw->core, &req);
  971. if (ret)
  972. return 0;
  973. return req.rate;
  974. }
  975. EXPORT_SYMBOL_GPL(clk_hw_round_rate);
  976. /**
  977. * clk_round_rate - round the given rate for a clk
  978. * @clk: the clk for which we are rounding a rate
  979. * @rate: the rate which is to be rounded
  980. *
  981. * Takes in a rate as input and rounds it to a rate that the clk can actually
  982. * use which is then returned. If clk doesn't support round_rate operation
  983. * then the parent rate is returned.
  984. */
  985. long clk_round_rate(struct clk *clk, unsigned long rate)
  986. {
  987. struct clk_rate_request req;
  988. int ret;
  989. if (!clk)
  990. return 0;
  991. clk_prepare_lock();
  992. if (clk->exclusive_count)
  993. clk_core_rate_unprotect(clk->core);
  994. clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
  995. req.rate = rate;
  996. ret = clk_core_round_rate_nolock(clk->core, &req);
  997. if (clk->exclusive_count)
  998. clk_core_rate_protect(clk->core);
  999. clk_prepare_unlock();
  1000. if (ret)
  1001. return ret;
  1002. return req.rate;
  1003. }
  1004. EXPORT_SYMBOL_GPL(clk_round_rate);
  1005. /**
  1006. * __clk_notify - call clk notifier chain
  1007. * @core: clk that is changing rate
  1008. * @msg: clk notifier type (see include/linux/clk.h)
  1009. * @old_rate: old clk rate
  1010. * @new_rate: new clk rate
  1011. *
  1012. * Triggers a notifier call chain on the clk rate-change notification
  1013. * for 'clk'. Passes a pointer to the struct clk and the previous
  1014. * and current rates to the notifier callback. Intended to be called by
  1015. * internal clock code only. Returns NOTIFY_DONE from the last driver
  1016. * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
  1017. * a driver returns that.
  1018. */
  1019. static int __clk_notify(struct clk_core *core, unsigned long msg,
  1020. unsigned long old_rate, unsigned long new_rate)
  1021. {
  1022. struct clk_notifier *cn;
  1023. struct clk_notifier_data cnd;
  1024. int ret = NOTIFY_DONE;
  1025. cnd.old_rate = old_rate;
  1026. cnd.new_rate = new_rate;
  1027. list_for_each_entry(cn, &clk_notifier_list, node) {
  1028. if (cn->clk->core == core) {
  1029. cnd.clk = cn->clk;
  1030. ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  1031. &cnd);
  1032. if (ret & NOTIFY_STOP_MASK)
  1033. return ret;
  1034. }
  1035. }
  1036. return ret;
  1037. }
  1038. /**
  1039. * __clk_recalc_accuracies
  1040. * @core: first clk in the subtree
  1041. *
  1042. * Walks the subtree of clks starting with clk and recalculates accuracies as
  1043. * it goes. Note that if a clk does not implement the .recalc_accuracy
  1044. * callback then it is assumed that the clock will take on the accuracy of its
  1045. * parent.
  1046. */
  1047. static void __clk_recalc_accuracies(struct clk_core *core)
  1048. {
  1049. unsigned long parent_accuracy = 0;
  1050. struct clk_core *child;
  1051. lockdep_assert_held(&prepare_lock);
  1052. if (core->parent)
  1053. parent_accuracy = core->parent->accuracy;
  1054. if (core->ops->recalc_accuracy)
  1055. core->accuracy = core->ops->recalc_accuracy(core->hw,
  1056. parent_accuracy);
  1057. else
  1058. core->accuracy = parent_accuracy;
  1059. hlist_for_each_entry(child, &core->children, child_node)
  1060. __clk_recalc_accuracies(child);
  1061. }
  1062. static long clk_core_get_accuracy(struct clk_core *core)
  1063. {
  1064. unsigned long accuracy;
  1065. clk_prepare_lock();
  1066. if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
  1067. __clk_recalc_accuracies(core);
  1068. accuracy = __clk_get_accuracy(core);
  1069. clk_prepare_unlock();
  1070. return accuracy;
  1071. }
  1072. /**
  1073. * clk_get_accuracy - return the accuracy of clk
  1074. * @clk: the clk whose accuracy is being returned
  1075. *
  1076. * Simply returns the cached accuracy of the clk, unless
  1077. * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
  1078. * issued.
  1079. * If clk is NULL then returns 0.
  1080. */
  1081. long clk_get_accuracy(struct clk *clk)
  1082. {
  1083. if (!clk)
  1084. return 0;
  1085. return clk_core_get_accuracy(clk->core);
  1086. }
  1087. EXPORT_SYMBOL_GPL(clk_get_accuracy);
  1088. static unsigned long clk_recalc(struct clk_core *core,
  1089. unsigned long parent_rate)
  1090. {
  1091. unsigned long rate = parent_rate;
  1092. if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
  1093. rate = core->ops->recalc_rate(core->hw, parent_rate);
  1094. clk_pm_runtime_put(core);
  1095. }
  1096. return rate;
  1097. }
  1098. /**
  1099. * __clk_recalc_rates
  1100. * @core: first clk in the subtree
  1101. * @msg: notification type (see include/linux/clk.h)
  1102. *
  1103. * Walks the subtree of clks starting with clk and recalculates rates as it
  1104. * goes. Note that if a clk does not implement the .recalc_rate callback then
  1105. * it is assumed that the clock will take on the rate of its parent.
  1106. *
  1107. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  1108. * if necessary.
  1109. */
  1110. static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
  1111. {
  1112. unsigned long old_rate;
  1113. unsigned long parent_rate = 0;
  1114. struct clk_core *child;
  1115. lockdep_assert_held(&prepare_lock);
  1116. old_rate = core->rate;
  1117. if (core->parent)
  1118. parent_rate = core->parent->rate;
  1119. core->rate = clk_recalc(core, parent_rate);
  1120. /*
  1121. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  1122. * & ABORT_RATE_CHANGE notifiers
  1123. */
  1124. if (core->notifier_count && msg)
  1125. __clk_notify(core, msg, old_rate, core->rate);
  1126. hlist_for_each_entry(child, &core->children, child_node)
  1127. __clk_recalc_rates(child, msg);
  1128. }
  1129. static unsigned long clk_core_get_rate(struct clk_core *core)
  1130. {
  1131. unsigned long rate;
  1132. clk_prepare_lock();
  1133. if (core && (core->flags & CLK_GET_RATE_NOCACHE))
  1134. __clk_recalc_rates(core, 0);
  1135. rate = clk_core_get_rate_nolock(core);
  1136. clk_prepare_unlock();
  1137. return rate;
  1138. }
  1139. /**
  1140. * clk_get_rate - return the rate of clk
  1141. * @clk: the clk whose rate is being returned
  1142. *
  1143. * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
  1144. * is set, which means a recalc_rate will be issued.
  1145. * If clk is NULL then returns 0.
  1146. */
  1147. unsigned long clk_get_rate(struct clk *clk)
  1148. {
  1149. if (!clk)
  1150. return 0;
  1151. return clk_core_get_rate(clk->core);
  1152. }
  1153. EXPORT_SYMBOL_GPL(clk_get_rate);
  1154. static int clk_fetch_parent_index(struct clk_core *core,
  1155. struct clk_core *parent)
  1156. {
  1157. int i;
  1158. if (!parent)
  1159. return -EINVAL;
  1160. for (i = 0; i < core->num_parents; i++)
  1161. if (clk_core_get_parent_by_index(core, i) == parent)
  1162. return i;
  1163. return -EINVAL;
  1164. }
  1165. /*
  1166. * Update the orphan status of @core and all its children.
  1167. */
  1168. static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
  1169. {
  1170. struct clk_core *child;
  1171. core->orphan = is_orphan;
  1172. hlist_for_each_entry(child, &core->children, child_node)
  1173. clk_core_update_orphan_status(child, is_orphan);
  1174. }
  1175. static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
  1176. {
  1177. bool was_orphan = core->orphan;
  1178. hlist_del(&core->child_node);
  1179. if (new_parent) {
  1180. bool becomes_orphan = new_parent->orphan;
  1181. /* avoid duplicate POST_RATE_CHANGE notifications */
  1182. if (new_parent->new_child == core)
  1183. new_parent->new_child = NULL;
  1184. hlist_add_head(&core->child_node, &new_parent->children);
  1185. if (was_orphan != becomes_orphan)
  1186. clk_core_update_orphan_status(core, becomes_orphan);
  1187. } else {
  1188. hlist_add_head(&core->child_node, &clk_orphan_list);
  1189. if (!was_orphan)
  1190. clk_core_update_orphan_status(core, true);
  1191. }
  1192. core->parent = new_parent;
  1193. }
  1194. static struct clk_core *__clk_set_parent_before(struct clk_core *core,
  1195. struct clk_core *parent)
  1196. {
  1197. unsigned long flags;
  1198. struct clk_core *old_parent = core->parent;
  1199. /*
  1200. * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
  1201. *
  1202. * 2. Migrate prepare state between parents and prevent race with
  1203. * clk_enable().
  1204. *
  1205. * If the clock is not prepared, then a race with
  1206. * clk_enable/disable() is impossible since we already have the
  1207. * prepare lock (future calls to clk_enable() need to be preceded by
  1208. * a clk_prepare()).
  1209. *
  1210. * If the clock is prepared, migrate the prepared state to the new
  1211. * parent and also protect against a race with clk_enable() by
  1212. * forcing the clock and the new parent on. This ensures that all
  1213. * future calls to clk_enable() are practically NOPs with respect to
  1214. * hardware and software states.
  1215. *
  1216. * See also: Comment for clk_set_parent() below.
  1217. */
  1218. /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
  1219. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  1220. clk_core_prepare_enable(old_parent);
  1221. clk_core_prepare_enable(parent);
  1222. }
  1223. /* migrate prepare count if > 0 */
  1224. if (core->prepare_count) {
  1225. clk_core_prepare_enable(parent);
  1226. clk_core_enable_lock(core);
  1227. }
  1228. /* update the clk tree topology */
  1229. flags = clk_enable_lock();
  1230. clk_reparent(core, parent);
  1231. clk_enable_unlock(flags);
  1232. return old_parent;
  1233. }
  1234. static void __clk_set_parent_after(struct clk_core *core,
  1235. struct clk_core *parent,
  1236. struct clk_core *old_parent)
  1237. {
  1238. /*
  1239. * Finish the migration of prepare state and undo the changes done
  1240. * for preventing a race with clk_enable().
  1241. */
  1242. if (core->prepare_count) {
  1243. clk_core_disable_lock(core);
  1244. clk_core_disable_unprepare(old_parent);
  1245. }
  1246. /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
  1247. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  1248. clk_core_disable_unprepare(parent);
  1249. clk_core_disable_unprepare(old_parent);
  1250. }
  1251. }
  1252. static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
  1253. u8 p_index)
  1254. {
  1255. unsigned long flags;
  1256. int ret = 0;
  1257. struct clk_core *old_parent;
  1258. old_parent = __clk_set_parent_before(core, parent);
  1259. trace_clk_set_parent(core, parent);
  1260. /* change clock input source */
  1261. if (parent && core->ops->set_parent)
  1262. ret = core->ops->set_parent(core->hw, p_index);
  1263. trace_clk_set_parent_complete(core, parent);
  1264. if (ret) {
  1265. flags = clk_enable_lock();
  1266. clk_reparent(core, old_parent);
  1267. clk_enable_unlock(flags);
  1268. __clk_set_parent_after(core, old_parent, parent);
  1269. return ret;
  1270. }
  1271. __clk_set_parent_after(core, parent, old_parent);
  1272. return 0;
  1273. }
  1274. /**
  1275. * __clk_speculate_rates
  1276. * @core: first clk in the subtree
  1277. * @parent_rate: the "future" rate of clk's parent
  1278. *
  1279. * Walks the subtree of clks starting with clk, speculating rates as it
  1280. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  1281. *
  1282. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  1283. * pre-rate change notifications and returns early if no clks in the
  1284. * subtree have subscribed to the notifications. Note that if a clk does not
  1285. * implement the .recalc_rate callback then it is assumed that the clock will
  1286. * take on the rate of its parent.
  1287. */
  1288. static int __clk_speculate_rates(struct clk_core *core,
  1289. unsigned long parent_rate)
  1290. {
  1291. struct clk_core *child;
  1292. unsigned long new_rate;
  1293. int ret = NOTIFY_DONE;
  1294. lockdep_assert_held(&prepare_lock);
  1295. new_rate = clk_recalc(core, parent_rate);
  1296. /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
  1297. if (core->notifier_count)
  1298. ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
  1299. if (ret & NOTIFY_STOP_MASK) {
  1300. pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
  1301. __func__, core->name, ret);
  1302. goto out;
  1303. }
  1304. hlist_for_each_entry(child, &core->children, child_node) {
  1305. ret = __clk_speculate_rates(child, new_rate);
  1306. if (ret & NOTIFY_STOP_MASK)
  1307. break;
  1308. }
  1309. out:
  1310. return ret;
  1311. }
  1312. static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
  1313. struct clk_core *new_parent, u8 p_index)
  1314. {
  1315. struct clk_core *child;
  1316. core->new_rate = new_rate;
  1317. core->new_parent = new_parent;
  1318. core->new_parent_index = p_index;
  1319. /* include clk in new parent's PRE_RATE_CHANGE notifications */
  1320. core->new_child = NULL;
  1321. if (new_parent && new_parent != core->parent)
  1322. new_parent->new_child = core;
  1323. hlist_for_each_entry(child, &core->children, child_node) {
  1324. child->new_rate = clk_recalc(child, new_rate);
  1325. clk_calc_subtree(child, child->new_rate, NULL, 0);
  1326. }
  1327. }
  1328. /*
  1329. * calculate the new rates returning the topmost clock that has to be
  1330. * changed.
  1331. */
  1332. static struct clk_core *clk_calc_new_rates(struct clk_core *core,
  1333. unsigned long rate)
  1334. {
  1335. struct clk_core *top = core;
  1336. struct clk_core *old_parent, *parent;
  1337. unsigned long best_parent_rate = 0;
  1338. unsigned long new_rate;
  1339. unsigned long min_rate;
  1340. unsigned long max_rate;
  1341. int p_index = 0;
  1342. long ret;
  1343. /* sanity */
  1344. if (IS_ERR_OR_NULL(core))
  1345. return NULL;
  1346. /* save parent rate, if it exists */
  1347. parent = old_parent = core->parent;
  1348. if (parent)
  1349. best_parent_rate = parent->rate;
  1350. clk_core_get_boundaries(core, &min_rate, &max_rate);
  1351. /* find the closest rate and parent clk/rate */
  1352. if (clk_core_can_round(core)) {
  1353. struct clk_rate_request req;
  1354. req.rate = rate;
  1355. req.min_rate = min_rate;
  1356. req.max_rate = max_rate;
  1357. clk_core_init_rate_req(core, &req);
  1358. ret = clk_core_determine_round_nolock(core, &req);
  1359. if (ret < 0)
  1360. return NULL;
  1361. best_parent_rate = req.best_parent_rate;
  1362. new_rate = req.rate;
  1363. parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
  1364. if (new_rate < min_rate || new_rate > max_rate)
  1365. return NULL;
  1366. } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
  1367. /* pass-through clock without adjustable parent */
  1368. core->new_rate = core->rate;
  1369. return NULL;
  1370. } else {
  1371. /* pass-through clock with adjustable parent */
  1372. top = clk_calc_new_rates(parent, rate);
  1373. new_rate = parent->new_rate;
  1374. goto out;
  1375. }
  1376. /* some clocks must be gated to change parent */
  1377. if (parent != old_parent &&
  1378. (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
  1379. pr_debug("%s: %s not gated but wants to reparent\n",
  1380. __func__, core->name);
  1381. return NULL;
  1382. }
  1383. /* try finding the new parent index */
  1384. if (parent && core->num_parents > 1) {
  1385. p_index = clk_fetch_parent_index(core, parent);
  1386. if (p_index < 0) {
  1387. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1388. __func__, parent->name, core->name);
  1389. return NULL;
  1390. }
  1391. }
  1392. if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
  1393. best_parent_rate != parent->rate)
  1394. top = clk_calc_new_rates(parent, best_parent_rate);
  1395. out:
  1396. clk_calc_subtree(core, new_rate, parent, p_index);
  1397. return top;
  1398. }
  1399. /*
  1400. * Notify about rate changes in a subtree. Always walk down the whole tree
  1401. * so that in case of an error we can walk down the whole tree again and
  1402. * abort the change.
  1403. */
  1404. static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
  1405. unsigned long event)
  1406. {
  1407. struct clk_core *child, *tmp_clk, *fail_clk = NULL;
  1408. int ret = NOTIFY_DONE;
  1409. if (core->rate == core->new_rate)
  1410. return NULL;
  1411. if (core->notifier_count) {
  1412. ret = __clk_notify(core, event, core->rate, core->new_rate);
  1413. if (ret & NOTIFY_STOP_MASK)
  1414. fail_clk = core;
  1415. }
  1416. hlist_for_each_entry(child, &core->children, child_node) {
  1417. /* Skip children who will be reparented to another clock */
  1418. if (child->new_parent && child->new_parent != core)
  1419. continue;
  1420. tmp_clk = clk_propagate_rate_change(child, event);
  1421. if (tmp_clk)
  1422. fail_clk = tmp_clk;
  1423. }
  1424. /* handle the new child who might not be in core->children yet */
  1425. if (core->new_child) {
  1426. tmp_clk = clk_propagate_rate_change(core->new_child, event);
  1427. if (tmp_clk)
  1428. fail_clk = tmp_clk;
  1429. }
  1430. return fail_clk;
  1431. }
  1432. /*
  1433. * walk down a subtree and set the new rates notifying the rate
  1434. * change on the way
  1435. */
  1436. static void clk_change_rate(struct clk_core *core)
  1437. {
  1438. struct clk_core *child;
  1439. struct hlist_node *tmp;
  1440. unsigned long old_rate;
  1441. unsigned long best_parent_rate = 0;
  1442. bool skip_set_rate = false;
  1443. struct clk_core *old_parent;
  1444. struct clk_core *parent = NULL;
  1445. old_rate = core->rate;
  1446. if (core->new_parent) {
  1447. parent = core->new_parent;
  1448. best_parent_rate = core->new_parent->rate;
  1449. } else if (core->parent) {
  1450. parent = core->parent;
  1451. best_parent_rate = core->parent->rate;
  1452. }
  1453. if (clk_pm_runtime_get(core))
  1454. return;
  1455. if (core->flags & CLK_SET_RATE_UNGATE) {
  1456. unsigned long flags;
  1457. clk_core_prepare(core);
  1458. flags = clk_enable_lock();
  1459. clk_core_enable(core);
  1460. clk_enable_unlock(flags);
  1461. }
  1462. if (core->new_parent && core->new_parent != core->parent) {
  1463. old_parent = __clk_set_parent_before(core, core->new_parent);
  1464. trace_clk_set_parent(core, core->new_parent);
  1465. if (core->ops->set_rate_and_parent) {
  1466. skip_set_rate = true;
  1467. core->ops->set_rate_and_parent(core->hw, core->new_rate,
  1468. best_parent_rate,
  1469. core->new_parent_index);
  1470. } else if (core->ops->set_parent) {
  1471. core->ops->set_parent(core->hw, core->new_parent_index);
  1472. }
  1473. trace_clk_set_parent_complete(core, core->new_parent);
  1474. __clk_set_parent_after(core, core->new_parent, old_parent);
  1475. }
  1476. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1477. clk_core_prepare_enable(parent);
  1478. trace_clk_set_rate(core, core->new_rate);
  1479. if (!skip_set_rate && core->ops->set_rate)
  1480. core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
  1481. trace_clk_set_rate_complete(core, core->new_rate);
  1482. core->rate = clk_recalc(core, best_parent_rate);
  1483. if (core->flags & CLK_SET_RATE_UNGATE) {
  1484. unsigned long flags;
  1485. flags = clk_enable_lock();
  1486. clk_core_disable(core);
  1487. clk_enable_unlock(flags);
  1488. clk_core_unprepare(core);
  1489. }
  1490. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1491. clk_core_disable_unprepare(parent);
  1492. if (core->notifier_count && old_rate != core->rate)
  1493. __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
  1494. if (core->flags & CLK_RECALC_NEW_RATES)
  1495. (void)clk_calc_new_rates(core, core->new_rate);
  1496. /*
  1497. * Use safe iteration, as change_rate can actually swap parents
  1498. * for certain clock types.
  1499. */
  1500. hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
  1501. /* Skip children who will be reparented to another clock */
  1502. if (child->new_parent && child->new_parent != core)
  1503. continue;
  1504. clk_change_rate(child);
  1505. }
  1506. /* handle the new child who might not be in core->children yet */
  1507. if (core->new_child)
  1508. clk_change_rate(core->new_child);
  1509. clk_pm_runtime_put(core);
  1510. }
  1511. static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
  1512. unsigned long req_rate)
  1513. {
  1514. int ret, cnt;
  1515. struct clk_rate_request req;
  1516. lockdep_assert_held(&prepare_lock);
  1517. if (!core)
  1518. return 0;
  1519. /* simulate what the rate would be if it could be freely set */
  1520. cnt = clk_core_rate_nuke_protect(core);
  1521. if (cnt < 0)
  1522. return cnt;
  1523. clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
  1524. req.rate = req_rate;
  1525. ret = clk_core_round_rate_nolock(core, &req);
  1526. /* restore the protection */
  1527. clk_core_rate_restore_protect(core, cnt);
  1528. return ret ? 0 : req.rate;
  1529. }
  1530. static int clk_core_set_rate_nolock(struct clk_core *core,
  1531. unsigned long req_rate)
  1532. {
  1533. struct clk_core *top, *fail_clk;
  1534. unsigned long rate;
  1535. int ret = 0;
  1536. if (!core)
  1537. return 0;
  1538. rate = clk_core_req_round_rate_nolock(core, req_rate);
  1539. /* bail early if nothing to do */
  1540. if (rate == clk_core_get_rate_nolock(core))
  1541. return 0;
  1542. /* fail on a direct rate set of a protected provider */
  1543. if (clk_core_rate_is_protected(core))
  1544. return -EBUSY;
  1545. /* calculate new rates and get the topmost changed clock */
  1546. top = clk_calc_new_rates(core, req_rate);
  1547. if (!top)
  1548. return -EINVAL;
  1549. ret = clk_pm_runtime_get(core);
  1550. if (ret)
  1551. return ret;
  1552. /* notify that we are about to change rates */
  1553. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  1554. if (fail_clk) {
  1555. pr_debug("%s: failed to set %s rate\n", __func__,
  1556. fail_clk->name);
  1557. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  1558. ret = -EBUSY;
  1559. goto err;
  1560. }
  1561. /* change the rates */
  1562. clk_change_rate(top);
  1563. core->req_rate = req_rate;
  1564. err:
  1565. clk_pm_runtime_put(core);
  1566. return ret;
  1567. }
  1568. /**
  1569. * clk_set_rate - specify a new rate for clk
  1570. * @clk: the clk whose rate is being changed
  1571. * @rate: the new rate for clk
  1572. *
  1573. * In the simplest case clk_set_rate will only adjust the rate of clk.
  1574. *
  1575. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
  1576. * propagate up to clk's parent; whether or not this happens depends on the
  1577. * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
  1578. * after calling .round_rate then upstream parent propagation is ignored. If
  1579. * *parent_rate comes back with a new rate for clk's parent then we propagate
  1580. * up to clk's parent and set its rate. Upward propagation will continue
  1581. * until either a clk does not support the CLK_SET_RATE_PARENT flag or
  1582. * .round_rate stops requesting changes to clk's parent_rate.
  1583. *
  1584. * Rate changes are accomplished via tree traversal that also recalculates the
  1585. * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
  1586. *
  1587. * Returns 0 on success, -EERROR otherwise.
  1588. */
  1589. int clk_set_rate(struct clk *clk, unsigned long rate)
  1590. {
  1591. int ret;
  1592. if (!clk)
  1593. return 0;
  1594. /* prevent racing with updates to the clock topology */
  1595. clk_prepare_lock();
  1596. if (clk->exclusive_count)
  1597. clk_core_rate_unprotect(clk->core);
  1598. ret = clk_core_set_rate_nolock(clk->core, rate);
  1599. if (clk->exclusive_count)
  1600. clk_core_rate_protect(clk->core);
  1601. clk_prepare_unlock();
  1602. return ret;
  1603. }
  1604. EXPORT_SYMBOL_GPL(clk_set_rate);
  1605. /**
  1606. * clk_set_rate_exclusive - specify a new rate get exclusive control
  1607. * @clk: the clk whose rate is being changed
  1608. * @rate: the new rate for clk
  1609. *
  1610. * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
  1611. * within a critical section
  1612. *
  1613. * This can be used initially to ensure that at least 1 consumer is
  1614. * statisfied when several consumers are competing for exclusivity over the
  1615. * same clock provider.
  1616. *
  1617. * The exclusivity is not applied if setting the rate failed.
  1618. *
  1619. * Calls to clk_rate_exclusive_get() should be balanced with calls to
  1620. * clk_rate_exclusive_put().
  1621. *
  1622. * Returns 0 on success, -EERROR otherwise.
  1623. */
  1624. int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
  1625. {
  1626. int ret;
  1627. if (!clk)
  1628. return 0;
  1629. /* prevent racing with updates to the clock topology */
  1630. clk_prepare_lock();
  1631. /*
  1632. * The temporary protection removal is not here, on purpose
  1633. * This function is meant to be used instead of clk_rate_protect,
  1634. * so before the consumer code path protect the clock provider
  1635. */
  1636. ret = clk_core_set_rate_nolock(clk->core, rate);
  1637. if (!ret) {
  1638. clk_core_rate_protect(clk->core);
  1639. clk->exclusive_count++;
  1640. }
  1641. clk_prepare_unlock();
  1642. return ret;
  1643. }
  1644. EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
  1645. /**
  1646. * clk_set_rate_range - set a rate range for a clock source
  1647. * @clk: clock source
  1648. * @min: desired minimum clock rate in Hz, inclusive
  1649. * @max: desired maximum clock rate in Hz, inclusive
  1650. *
  1651. * Returns success (0) or negative errno.
  1652. */
  1653. int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
  1654. {
  1655. int ret = 0;
  1656. unsigned long old_min, old_max, rate;
  1657. if (!clk)
  1658. return 0;
  1659. if (min > max) {
  1660. pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
  1661. __func__, clk->core->name, clk->dev_id, clk->con_id,
  1662. min, max);
  1663. return -EINVAL;
  1664. }
  1665. clk_prepare_lock();
  1666. if (clk->exclusive_count)
  1667. clk_core_rate_unprotect(clk->core);
  1668. /* Save the current values in case we need to rollback the change */
  1669. old_min = clk->min_rate;
  1670. old_max = clk->max_rate;
  1671. clk->min_rate = min;
  1672. clk->max_rate = max;
  1673. rate = clk_core_get_rate_nolock(clk->core);
  1674. if (rate < min || rate > max) {
  1675. /*
  1676. * FIXME:
  1677. * We are in bit of trouble here, current rate is outside the
  1678. * the requested range. We are going try to request appropriate
  1679. * range boundary but there is a catch. It may fail for the
  1680. * usual reason (clock broken, clock protected, etc) but also
  1681. * because:
  1682. * - round_rate() was not favorable and fell on the wrong
  1683. * side of the boundary
  1684. * - the determine_rate() callback does not really check for
  1685. * this corner case when determining the rate
  1686. */
  1687. if (rate < min)
  1688. rate = min;
  1689. else
  1690. rate = max;
  1691. ret = clk_core_set_rate_nolock(clk->core, rate);
  1692. if (ret) {
  1693. /* rollback the changes */
  1694. clk->min_rate = old_min;
  1695. clk->max_rate = old_max;
  1696. }
  1697. }
  1698. if (clk->exclusive_count)
  1699. clk_core_rate_protect(clk->core);
  1700. clk_prepare_unlock();
  1701. return ret;
  1702. }
  1703. EXPORT_SYMBOL_GPL(clk_set_rate_range);
  1704. /**
  1705. * clk_set_min_rate - set a minimum clock rate for a clock source
  1706. * @clk: clock source
  1707. * @rate: desired minimum clock rate in Hz, inclusive
  1708. *
  1709. * Returns success (0) or negative errno.
  1710. */
  1711. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  1712. {
  1713. if (!clk)
  1714. return 0;
  1715. return clk_set_rate_range(clk, rate, clk->max_rate);
  1716. }
  1717. EXPORT_SYMBOL_GPL(clk_set_min_rate);
  1718. /**
  1719. * clk_set_max_rate - set a maximum clock rate for a clock source
  1720. * @clk: clock source
  1721. * @rate: desired maximum clock rate in Hz, inclusive
  1722. *
  1723. * Returns success (0) or negative errno.
  1724. */
  1725. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  1726. {
  1727. if (!clk)
  1728. return 0;
  1729. return clk_set_rate_range(clk, clk->min_rate, rate);
  1730. }
  1731. EXPORT_SYMBOL_GPL(clk_set_max_rate);
  1732. /**
  1733. * clk_get_parent - return the parent of a clk
  1734. * @clk: the clk whose parent gets returned
  1735. *
  1736. * Simply returns clk->parent. Returns NULL if clk is NULL.
  1737. */
  1738. struct clk *clk_get_parent(struct clk *clk)
  1739. {
  1740. struct clk *parent;
  1741. if (!clk)
  1742. return NULL;
  1743. clk_prepare_lock();
  1744. /* TODO: Create a per-user clk and change callers to call clk_put */
  1745. parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
  1746. clk_prepare_unlock();
  1747. return parent;
  1748. }
  1749. EXPORT_SYMBOL_GPL(clk_get_parent);
  1750. static struct clk_core *__clk_init_parent(struct clk_core *core)
  1751. {
  1752. u8 index = 0;
  1753. if (core->num_parents > 1 && core->ops->get_parent)
  1754. index = core->ops->get_parent(core->hw);
  1755. return clk_core_get_parent_by_index(core, index);
  1756. }
  1757. static void clk_core_reparent(struct clk_core *core,
  1758. struct clk_core *new_parent)
  1759. {
  1760. clk_reparent(core, new_parent);
  1761. __clk_recalc_accuracies(core);
  1762. __clk_recalc_rates(core, POST_RATE_CHANGE);
  1763. }
  1764. void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
  1765. {
  1766. if (!hw)
  1767. return;
  1768. clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
  1769. }
  1770. /**
  1771. * clk_has_parent - check if a clock is a possible parent for another
  1772. * @clk: clock source
  1773. * @parent: parent clock source
  1774. *
  1775. * This function can be used in drivers that need to check that a clock can be
  1776. * the parent of another without actually changing the parent.
  1777. *
  1778. * Returns true if @parent is a possible parent for @clk, false otherwise.
  1779. */
  1780. bool clk_has_parent(struct clk *clk, struct clk *parent)
  1781. {
  1782. struct clk_core *core, *parent_core;
  1783. /* NULL clocks should be nops, so return success if either is NULL. */
  1784. if (!clk || !parent)
  1785. return true;
  1786. core = clk->core;
  1787. parent_core = parent->core;
  1788. /* Optimize for the case where the parent is already the parent. */
  1789. if (core->parent == parent_core)
  1790. return true;
  1791. return match_string(core->parent_names, core->num_parents,
  1792. parent_core->name) >= 0;
  1793. }
  1794. EXPORT_SYMBOL_GPL(clk_has_parent);
  1795. static int clk_core_set_parent_nolock(struct clk_core *core,
  1796. struct clk_core *parent)
  1797. {
  1798. int ret = 0;
  1799. int p_index = 0;
  1800. unsigned long p_rate = 0;
  1801. lockdep_assert_held(&prepare_lock);
  1802. if (!core)
  1803. return 0;
  1804. if (core->parent == parent)
  1805. return 0;
  1806. /* verify ops for for multi-parent clks */
  1807. if (core->num_parents > 1 && !core->ops->set_parent)
  1808. return -EPERM;
  1809. /* check that we are allowed to re-parent if the clock is in use */
  1810. if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
  1811. return -EBUSY;
  1812. if (clk_core_rate_is_protected(core))
  1813. return -EBUSY;
  1814. /* try finding the new parent index */
  1815. if (parent) {
  1816. p_index = clk_fetch_parent_index(core, parent);
  1817. if (p_index < 0) {
  1818. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1819. __func__, parent->name, core->name);
  1820. return p_index;
  1821. }
  1822. p_rate = parent->rate;
  1823. }
  1824. ret = clk_pm_runtime_get(core);
  1825. if (ret)
  1826. return ret;
  1827. /* propagate PRE_RATE_CHANGE notifications */
  1828. ret = __clk_speculate_rates(core, p_rate);
  1829. /* abort if a driver objects */
  1830. if (ret & NOTIFY_STOP_MASK)
  1831. goto runtime_put;
  1832. /* do the re-parent */
  1833. ret = __clk_set_parent(core, parent, p_index);
  1834. /* propagate rate an accuracy recalculation accordingly */
  1835. if (ret) {
  1836. __clk_recalc_rates(core, ABORT_RATE_CHANGE);
  1837. } else {
  1838. __clk_recalc_rates(core, POST_RATE_CHANGE);
  1839. __clk_recalc_accuracies(core);
  1840. }
  1841. runtime_put:
  1842. clk_pm_runtime_put(core);
  1843. return ret;
  1844. }
  1845. /**
  1846. * clk_set_parent - switch the parent of a mux clk
  1847. * @clk: the mux clk whose input we are switching
  1848. * @parent: the new input to clk
  1849. *
  1850. * Re-parent clk to use parent as its new input source. If clk is in
  1851. * prepared state, the clk will get enabled for the duration of this call. If
  1852. * that's not acceptable for a specific clk (Eg: the consumer can't handle
  1853. * that, the reparenting is glitchy in hardware, etc), use the
  1854. * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
  1855. *
  1856. * After successfully changing clk's parent clk_set_parent will update the
  1857. * clk topology, sysfs topology and propagate rate recalculation via
  1858. * __clk_recalc_rates.
  1859. *
  1860. * Returns 0 on success, -EERROR otherwise.
  1861. */
  1862. int clk_set_parent(struct clk *clk, struct clk *parent)
  1863. {
  1864. int ret;
  1865. if (!clk)
  1866. return 0;
  1867. clk_prepare_lock();
  1868. if (clk->exclusive_count)
  1869. clk_core_rate_unprotect(clk->core);
  1870. ret = clk_core_set_parent_nolock(clk->core,
  1871. parent ? parent->core : NULL);
  1872. if (clk->exclusive_count)
  1873. clk_core_rate_protect(clk->core);
  1874. clk_prepare_unlock();
  1875. return ret;
  1876. }
  1877. EXPORT_SYMBOL_GPL(clk_set_parent);
  1878. static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
  1879. {
  1880. int ret = -EINVAL;
  1881. lockdep_assert_held(&prepare_lock);
  1882. if (!core)
  1883. return 0;
  1884. if (clk_core_rate_is_protected(core))
  1885. return -EBUSY;
  1886. trace_clk_set_phase(core, degrees);
  1887. if (core->ops->set_phase) {
  1888. ret = core->ops->set_phase(core->hw, degrees);
  1889. if (!ret)
  1890. core->phase = degrees;
  1891. }
  1892. trace_clk_set_phase_complete(core, degrees);
  1893. return ret;
  1894. }
  1895. /**
  1896. * clk_set_phase - adjust the phase shift of a clock signal
  1897. * @clk: clock signal source
  1898. * @degrees: number of degrees the signal is shifted
  1899. *
  1900. * Shifts the phase of a clock signal by the specified
  1901. * degrees. Returns 0 on success, -EERROR otherwise.
  1902. *
  1903. * This function makes no distinction about the input or reference
  1904. * signal that we adjust the clock signal phase against. For example
  1905. * phase locked-loop clock signal generators we may shift phase with
  1906. * respect to feedback clock signal input, but for other cases the
  1907. * clock phase may be shifted with respect to some other, unspecified
  1908. * signal.
  1909. *
  1910. * Additionally the concept of phase shift does not propagate through
  1911. * the clock tree hierarchy, which sets it apart from clock rates and
  1912. * clock accuracy. A parent clock phase attribute does not have an
  1913. * impact on the phase attribute of a child clock.
  1914. */
  1915. int clk_set_phase(struct clk *clk, int degrees)
  1916. {
  1917. int ret;
  1918. if (!clk)
  1919. return 0;
  1920. /* sanity check degrees */
  1921. degrees %= 360;
  1922. if (degrees < 0)
  1923. degrees += 360;
  1924. clk_prepare_lock();
  1925. if (clk->exclusive_count)
  1926. clk_core_rate_unprotect(clk->core);
  1927. ret = clk_core_set_phase_nolock(clk->core, degrees);
  1928. if (clk->exclusive_count)
  1929. clk_core_rate_protect(clk->core);
  1930. clk_prepare_unlock();
  1931. return ret;
  1932. }
  1933. EXPORT_SYMBOL_GPL(clk_set_phase);
  1934. static int clk_core_get_phase(struct clk_core *core)
  1935. {
  1936. int ret;
  1937. clk_prepare_lock();
  1938. /* Always try to update cached phase if possible */
  1939. if (core->ops->get_phase)
  1940. core->phase = core->ops->get_phase(core->hw);
  1941. ret = core->phase;
  1942. clk_prepare_unlock();
  1943. return ret;
  1944. }
  1945. /**
  1946. * clk_get_phase - return the phase shift of a clock signal
  1947. * @clk: clock signal source
  1948. *
  1949. * Returns the phase shift of a clock node in degrees, otherwise returns
  1950. * -EERROR.
  1951. */
  1952. int clk_get_phase(struct clk *clk)
  1953. {
  1954. if (!clk)
  1955. return 0;
  1956. return clk_core_get_phase(clk->core);
  1957. }
  1958. EXPORT_SYMBOL_GPL(clk_get_phase);
  1959. static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
  1960. {
  1961. /* Assume a default value of 50% */
  1962. core->duty.num = 1;
  1963. core->duty.den = 2;
  1964. }
  1965. static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
  1966. static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
  1967. {
  1968. struct clk_duty *duty = &core->duty;
  1969. int ret = 0;
  1970. if (!core->ops->get_duty_cycle)
  1971. return clk_core_update_duty_cycle_parent_nolock(core);
  1972. ret = core->ops->get_duty_cycle(core->hw, duty);
  1973. if (ret)
  1974. goto reset;
  1975. /* Don't trust the clock provider too much */
  1976. if (duty->den == 0 || duty->num > duty->den) {
  1977. ret = -EINVAL;
  1978. goto reset;
  1979. }
  1980. return 0;
  1981. reset:
  1982. clk_core_reset_duty_cycle_nolock(core);
  1983. return ret;
  1984. }
  1985. static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
  1986. {
  1987. int ret = 0;
  1988. if (core->parent &&
  1989. core->flags & CLK_DUTY_CYCLE_PARENT) {
  1990. ret = clk_core_update_duty_cycle_nolock(core->parent);
  1991. memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
  1992. } else {
  1993. clk_core_reset_duty_cycle_nolock(core);
  1994. }
  1995. return ret;
  1996. }
  1997. static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
  1998. struct clk_duty *duty);
  1999. static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
  2000. struct clk_duty *duty)
  2001. {
  2002. int ret;
  2003. lockdep_assert_held(&prepare_lock);
  2004. if (clk_core_rate_is_protected(core))
  2005. return -EBUSY;
  2006. trace_clk_set_duty_cycle(core, duty);
  2007. if (!core->ops->set_duty_cycle)
  2008. return clk_core_set_duty_cycle_parent_nolock(core, duty);
  2009. ret = core->ops->set_duty_cycle(core->hw, duty);
  2010. if (!ret)
  2011. memcpy(&core->duty, duty, sizeof(*duty));
  2012. trace_clk_set_duty_cycle_complete(core, duty);
  2013. return ret;
  2014. }
  2015. static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
  2016. struct clk_duty *duty)
  2017. {
  2018. int ret = 0;
  2019. if (core->parent &&
  2020. core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
  2021. ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
  2022. memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
  2023. }
  2024. return ret;
  2025. }
  2026. /**
  2027. * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
  2028. * @clk: clock signal source
  2029. * @num: numerator of the duty cycle ratio to be applied
  2030. * @den: denominator of the duty cycle ratio to be applied
  2031. *
  2032. * Apply the duty cycle ratio if the ratio is valid and the clock can
  2033. * perform this operation
  2034. *
  2035. * Returns (0) on success, a negative errno otherwise.
  2036. */
  2037. int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
  2038. {
  2039. int ret;
  2040. struct clk_duty duty;
  2041. if (!clk)
  2042. return 0;
  2043. /* sanity check the ratio */
  2044. if (den == 0 || num > den)
  2045. return -EINVAL;
  2046. duty.num = num;
  2047. duty.den = den;
  2048. clk_prepare_lock();
  2049. if (clk->exclusive_count)
  2050. clk_core_rate_unprotect(clk->core);
  2051. ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
  2052. if (clk->exclusive_count)
  2053. clk_core_rate_protect(clk->core);
  2054. clk_prepare_unlock();
  2055. return ret;
  2056. }
  2057. EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
  2058. static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
  2059. unsigned int scale)
  2060. {
  2061. struct clk_duty *duty = &core->duty;
  2062. int ret;
  2063. clk_prepare_lock();
  2064. ret = clk_core_update_duty_cycle_nolock(core);
  2065. if (!ret)
  2066. ret = mult_frac(scale, duty->num, duty->den);
  2067. clk_prepare_unlock();
  2068. return ret;
  2069. }
  2070. /**
  2071. * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
  2072. * @clk: clock signal source
  2073. * @scale: scaling factor to be applied to represent the ratio as an integer
  2074. *
  2075. * Returns the duty cycle ratio of a clock node multiplied by the provided
  2076. * scaling factor, or negative errno on error.
  2077. */
  2078. int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
  2079. {
  2080. if (!clk)
  2081. return 0;
  2082. return clk_core_get_scaled_duty_cycle(clk->core, scale);
  2083. }
  2084. EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
  2085. /**
  2086. * clk_is_match - check if two clk's point to the same hardware clock
  2087. * @p: clk compared against q
  2088. * @q: clk compared against p
  2089. *
  2090. * Returns true if the two struct clk pointers both point to the same hardware
  2091. * clock node. Put differently, returns true if struct clk *p and struct clk *q
  2092. * share the same struct clk_core object.
  2093. *
  2094. * Returns false otherwise. Note that two NULL clks are treated as matching.
  2095. */
  2096. bool clk_is_match(const struct clk *p, const struct clk *q)
  2097. {
  2098. /* trivial case: identical struct clk's or both NULL */
  2099. if (p == q)
  2100. return true;
  2101. /* true if clk->core pointers match. Avoid dereferencing garbage */
  2102. if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
  2103. if (p->core == q->core)
  2104. return true;
  2105. return false;
  2106. }
  2107. EXPORT_SYMBOL_GPL(clk_is_match);
  2108. /*** debugfs support ***/
  2109. #ifdef CONFIG_DEBUG_FS
  2110. #include <linux/debugfs.h>
  2111. static struct dentry *rootdir;
  2112. static int inited = 0;
  2113. static DEFINE_MUTEX(clk_debug_lock);
  2114. static HLIST_HEAD(clk_debug_list);
  2115. static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
  2116. int level)
  2117. {
  2118. if (!c)
  2119. return;
  2120. seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
  2121. level * 3 + 1, "",
  2122. 30 - level * 3, c->name,
  2123. c->enable_count, c->prepare_count, c->protect_count,
  2124. clk_core_get_rate(c), clk_core_get_accuracy(c),
  2125. clk_core_get_phase(c),
  2126. clk_core_get_scaled_duty_cycle(c, 100000));
  2127. }
  2128. static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
  2129. int level)
  2130. {
  2131. struct clk_core *child;
  2132. if (!c)
  2133. return;
  2134. clk_summary_show_one(s, c, level);
  2135. hlist_for_each_entry(child, &c->children, child_node)
  2136. clk_summary_show_subtree(s, child, level + 1);
  2137. }
  2138. static int clk_summary_show(struct seq_file *s, void *data)
  2139. {
  2140. struct clk_core *c;
  2141. struct hlist_head **lists = (struct hlist_head **)s->private;
  2142. seq_puts(s, " enable prepare protect duty\n");
  2143. seq_puts(s, " clock count count count rate accuracy phase cycle\n");
  2144. seq_puts(s, "---------------------------------------------------------------------------------------------\n");
  2145. clk_prepare_lock();
  2146. for (; *lists; lists++)
  2147. hlist_for_each_entry(c, *lists, child_node)
  2148. clk_summary_show_subtree(s, c, 0);
  2149. clk_prepare_unlock();
  2150. return 0;
  2151. }
  2152. DEFINE_SHOW_ATTRIBUTE(clk_summary);
  2153. static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
  2154. {
  2155. if (!c)
  2156. return;
  2157. /* This should be JSON format, i.e. elements separated with a comma */
  2158. seq_printf(s, "\"%s\": { ", c->name);
  2159. seq_printf(s, "\"enable_count\": %d,", c->enable_count);
  2160. seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
  2161. seq_printf(s, "\"protect_count\": %d,", c->protect_count);
  2162. seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
  2163. seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
  2164. seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
  2165. seq_printf(s, "\"duty_cycle\": %u",
  2166. clk_core_get_scaled_duty_cycle(c, 100000));
  2167. }
  2168. static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
  2169. {
  2170. struct clk_core *child;
  2171. if (!c)
  2172. return;
  2173. clk_dump_one(s, c, level);
  2174. hlist_for_each_entry(child, &c->children, child_node) {
  2175. seq_putc(s, ',');
  2176. clk_dump_subtree(s, child, level + 1);
  2177. }
  2178. seq_putc(s, '}');
  2179. }
  2180. static int clk_dump_show(struct seq_file *s, void *data)
  2181. {
  2182. struct clk_core *c;
  2183. bool first_node = true;
  2184. struct hlist_head **lists = (struct hlist_head **)s->private;
  2185. seq_putc(s, '{');
  2186. clk_prepare_lock();
  2187. for (; *lists; lists++) {
  2188. hlist_for_each_entry(c, *lists, child_node) {
  2189. if (!first_node)
  2190. seq_putc(s, ',');
  2191. first_node = false;
  2192. clk_dump_subtree(s, c, 0);
  2193. }
  2194. }
  2195. clk_prepare_unlock();
  2196. seq_puts(s, "}\n");
  2197. return 0;
  2198. }
  2199. DEFINE_SHOW_ATTRIBUTE(clk_dump);
  2200. static const struct {
  2201. unsigned long flag;
  2202. const char *name;
  2203. } clk_flags[] = {
  2204. #define ENTRY(f) { f, #f }
  2205. ENTRY(CLK_SET_RATE_GATE),
  2206. ENTRY(CLK_SET_PARENT_GATE),
  2207. ENTRY(CLK_SET_RATE_PARENT),
  2208. ENTRY(CLK_IGNORE_UNUSED),
  2209. ENTRY(CLK_IS_BASIC),
  2210. ENTRY(CLK_GET_RATE_NOCACHE),
  2211. ENTRY(CLK_SET_RATE_NO_REPARENT),
  2212. ENTRY(CLK_GET_ACCURACY_NOCACHE),
  2213. ENTRY(CLK_RECALC_NEW_RATES),
  2214. ENTRY(CLK_SET_RATE_UNGATE),
  2215. ENTRY(CLK_IS_CRITICAL),
  2216. ENTRY(CLK_OPS_PARENT_ENABLE),
  2217. ENTRY(CLK_DUTY_CYCLE_PARENT),
  2218. #undef ENTRY
  2219. };
  2220. static int clk_flags_show(struct seq_file *s, void *data)
  2221. {
  2222. struct clk_core *core = s->private;
  2223. unsigned long flags = core->flags;
  2224. unsigned int i;
  2225. for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
  2226. if (flags & clk_flags[i].flag) {
  2227. seq_printf(s, "%s\n", clk_flags[i].name);
  2228. flags &= ~clk_flags[i].flag;
  2229. }
  2230. }
  2231. if (flags) {
  2232. /* Unknown flags */
  2233. seq_printf(s, "0x%lx\n", flags);
  2234. }
  2235. return 0;
  2236. }
  2237. DEFINE_SHOW_ATTRIBUTE(clk_flags);
  2238. static int possible_parents_show(struct seq_file *s, void *data)
  2239. {
  2240. struct clk_core *core = s->private;
  2241. int i;
  2242. for (i = 0; i < core->num_parents - 1; i++)
  2243. seq_printf(s, "%s ", core->parent_names[i]);
  2244. seq_printf(s, "%s\n", core->parent_names[i]);
  2245. return 0;
  2246. }
  2247. DEFINE_SHOW_ATTRIBUTE(possible_parents);
  2248. static int clk_duty_cycle_show(struct seq_file *s, void *data)
  2249. {
  2250. struct clk_core *core = s->private;
  2251. struct clk_duty *duty = &core->duty;
  2252. seq_printf(s, "%u/%u\n", duty->num, duty->den);
  2253. return 0;
  2254. }
  2255. DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
  2256. static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
  2257. {
  2258. struct dentry *root;
  2259. if (!core || !pdentry)
  2260. return;
  2261. root = debugfs_create_dir(core->name, pdentry);
  2262. core->dentry = root;
  2263. debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
  2264. debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
  2265. debugfs_create_u32("clk_phase", 0444, root, &core->phase);
  2266. debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
  2267. debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
  2268. debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
  2269. debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
  2270. debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
  2271. debugfs_create_file("clk_duty_cycle", 0444, root, core,
  2272. &clk_duty_cycle_fops);
  2273. if (core->num_parents > 1)
  2274. debugfs_create_file("clk_possible_parents", 0444, root, core,
  2275. &possible_parents_fops);
  2276. if (core->ops->debug_init)
  2277. core->ops->debug_init(core->hw, core->dentry);
  2278. }
  2279. /**
  2280. * clk_debug_register - add a clk node to the debugfs clk directory
  2281. * @core: the clk being added to the debugfs clk directory
  2282. *
  2283. * Dynamically adds a clk to the debugfs clk directory if debugfs has been
  2284. * initialized. Otherwise it bails out early since the debugfs clk directory
  2285. * will be created lazily by clk_debug_init as part of a late_initcall.
  2286. */
  2287. static void clk_debug_register(struct clk_core *core)
  2288. {
  2289. mutex_lock(&clk_debug_lock);
  2290. hlist_add_head(&core->debug_node, &clk_debug_list);
  2291. if (inited)
  2292. clk_debug_create_one(core, rootdir);
  2293. mutex_unlock(&clk_debug_lock);
  2294. }
  2295. /**
  2296. * clk_debug_unregister - remove a clk node from the debugfs clk directory
  2297. * @core: the clk being removed from the debugfs clk directory
  2298. *
  2299. * Dynamically removes a clk and all its child nodes from the
  2300. * debugfs clk directory if clk->dentry points to debugfs created by
  2301. * clk_debug_register in __clk_core_init.
  2302. */
  2303. static void clk_debug_unregister(struct clk_core *core)
  2304. {
  2305. mutex_lock(&clk_debug_lock);
  2306. hlist_del_init(&core->debug_node);
  2307. debugfs_remove_recursive(core->dentry);
  2308. core->dentry = NULL;
  2309. mutex_unlock(&clk_debug_lock);
  2310. }
  2311. /**
  2312. * clk_debug_init - lazily populate the debugfs clk directory
  2313. *
  2314. * clks are often initialized very early during boot before memory can be
  2315. * dynamically allocated and well before debugfs is setup. This function
  2316. * populates the debugfs clk directory once at boot-time when we know that
  2317. * debugfs is setup. It should only be called once at boot-time, all other clks
  2318. * added dynamically will be done so with clk_debug_register.
  2319. */
  2320. static int __init clk_debug_init(void)
  2321. {
  2322. struct clk_core *core;
  2323. rootdir = debugfs_create_dir("clk", NULL);
  2324. debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
  2325. &clk_summary_fops);
  2326. debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
  2327. &clk_dump_fops);
  2328. debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
  2329. &clk_summary_fops);
  2330. debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
  2331. &clk_dump_fops);
  2332. mutex_lock(&clk_debug_lock);
  2333. hlist_for_each_entry(core, &clk_debug_list, debug_node)
  2334. clk_debug_create_one(core, rootdir);
  2335. inited = 1;
  2336. mutex_unlock(&clk_debug_lock);
  2337. return 0;
  2338. }
  2339. late_initcall(clk_debug_init);
  2340. #else
  2341. static inline void clk_debug_register(struct clk_core *core) { }
  2342. static inline void clk_debug_reparent(struct clk_core *core,
  2343. struct clk_core *new_parent)
  2344. {
  2345. }
  2346. static inline void clk_debug_unregister(struct clk_core *core)
  2347. {
  2348. }
  2349. #endif
  2350. /**
  2351. * __clk_core_init - initialize the data structures in a struct clk_core
  2352. * @core: clk_core being initialized
  2353. *
  2354. * Initializes the lists in struct clk_core, queries the hardware for the
  2355. * parent and rate and sets them both.
  2356. */
  2357. static int __clk_core_init(struct clk_core *core)
  2358. {
  2359. int i, ret;
  2360. struct clk_core *orphan;
  2361. struct hlist_node *tmp2;
  2362. unsigned long rate;
  2363. if (!core)
  2364. return -EINVAL;
  2365. clk_prepare_lock();
  2366. ret = clk_pm_runtime_get(core);
  2367. if (ret)
  2368. goto unlock;
  2369. /* check to see if a clock with this name is already registered */
  2370. if (clk_core_lookup(core->name)) {
  2371. pr_debug("%s: clk %s already initialized\n",
  2372. __func__, core->name);
  2373. ret = -EEXIST;
  2374. goto out;
  2375. }
  2376. /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
  2377. if (core->ops->set_rate &&
  2378. !((core->ops->round_rate || core->ops->determine_rate) &&
  2379. core->ops->recalc_rate)) {
  2380. pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
  2381. __func__, core->name);
  2382. ret = -EINVAL;
  2383. goto out;
  2384. }
  2385. if (core->ops->set_parent && !core->ops->get_parent) {
  2386. pr_err("%s: %s must implement .get_parent & .set_parent\n",
  2387. __func__, core->name);
  2388. ret = -EINVAL;
  2389. goto out;
  2390. }
  2391. if (core->num_parents > 1 && !core->ops->get_parent) {
  2392. pr_err("%s: %s must implement .get_parent as it has multi parents\n",
  2393. __func__, core->name);
  2394. ret = -EINVAL;
  2395. goto out;
  2396. }
  2397. if (core->ops->set_rate_and_parent &&
  2398. !(core->ops->set_parent && core->ops->set_rate)) {
  2399. pr_err("%s: %s must implement .set_parent & .set_rate\n",
  2400. __func__, core->name);
  2401. ret = -EINVAL;
  2402. goto out;
  2403. }
  2404. /* throw a WARN if any entries in parent_names are NULL */
  2405. for (i = 0; i < core->num_parents; i++)
  2406. WARN(!core->parent_names[i],
  2407. "%s: invalid NULL in %s's .parent_names\n",
  2408. __func__, core->name);
  2409. core->parent = __clk_init_parent(core);
  2410. /*
  2411. * Populate core->parent if parent has already been clk_core_init'd. If
  2412. * parent has not yet been clk_core_init'd then place clk in the orphan
  2413. * list. If clk doesn't have any parents then place it in the root
  2414. * clk list.
  2415. *
  2416. * Every time a new clk is clk_init'd then we walk the list of orphan
  2417. * clocks and re-parent any that are children of the clock currently
  2418. * being clk_init'd.
  2419. */
  2420. if (core->parent) {
  2421. hlist_add_head(&core->child_node,
  2422. &core->parent->children);
  2423. core->orphan = core->parent->orphan;
  2424. } else if (!core->num_parents) {
  2425. hlist_add_head(&core->child_node, &clk_root_list);
  2426. core->orphan = false;
  2427. } else {
  2428. hlist_add_head(&core->child_node, &clk_orphan_list);
  2429. core->orphan = true;
  2430. }
  2431. /*
  2432. * optional platform-specific magic
  2433. *
  2434. * The .init callback is not used by any of the basic clock types, but
  2435. * exists for weird hardware that must perform initialization magic.
  2436. * Please consider other ways of solving initialization problems before
  2437. * using this callback, as its use is discouraged.
  2438. */
  2439. if (core->ops->init)
  2440. core->ops->init(core->hw);
  2441. /*
  2442. * Set clk's accuracy. The preferred method is to use
  2443. * .recalc_accuracy. For simple clocks and lazy developers the default
  2444. * fallback is to use the parent's accuracy. If a clock doesn't have a
  2445. * parent (or is orphaned) then accuracy is set to zero (perfect
  2446. * clock).
  2447. */
  2448. if (core->ops->recalc_accuracy)
  2449. core->accuracy = core->ops->recalc_accuracy(core->hw,
  2450. __clk_get_accuracy(core->parent));
  2451. else if (core->parent)
  2452. core->accuracy = core->parent->accuracy;
  2453. else
  2454. core->accuracy = 0;
  2455. /*
  2456. * Set clk's phase.
  2457. * Since a phase is by definition relative to its parent, just
  2458. * query the current clock phase, or just assume it's in phase.
  2459. */
  2460. if (core->ops->get_phase)
  2461. core->phase = core->ops->get_phase(core->hw);
  2462. else
  2463. core->phase = 0;
  2464. /*
  2465. * Set clk's duty cycle.
  2466. */
  2467. clk_core_update_duty_cycle_nolock(core);
  2468. /*
  2469. * Set clk's rate. The preferred method is to use .recalc_rate. For
  2470. * simple clocks and lazy developers the default fallback is to use the
  2471. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  2472. * then rate is set to zero.
  2473. */
  2474. if (core->ops->recalc_rate)
  2475. rate = core->ops->recalc_rate(core->hw,
  2476. clk_core_get_rate_nolock(core->parent));
  2477. else if (core->parent)
  2478. rate = core->parent->rate;
  2479. else
  2480. rate = 0;
  2481. core->rate = core->req_rate = rate;
  2482. /*
  2483. * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
  2484. * don't get accidentally disabled when walking the orphan tree and
  2485. * reparenting clocks
  2486. */
  2487. if (core->flags & CLK_IS_CRITICAL) {
  2488. unsigned long flags;
  2489. ret = clk_core_prepare(core);
  2490. if (ret)
  2491. goto out;
  2492. flags = clk_enable_lock();
  2493. ret = clk_core_enable(core);
  2494. clk_enable_unlock(flags);
  2495. if (ret) {
  2496. clk_core_unprepare(core);
  2497. goto out;
  2498. }
  2499. }
  2500. /*
  2501. * walk the list of orphan clocks and reparent any that newly finds a
  2502. * parent.
  2503. */
  2504. hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
  2505. struct clk_core *parent = __clk_init_parent(orphan);
  2506. /*
  2507. * We need to use __clk_set_parent_before() and _after() to
  2508. * to properly migrate any prepare/enable count of the orphan
  2509. * clock. This is important for CLK_IS_CRITICAL clocks, which
  2510. * are enabled during init but might not have a parent yet.
  2511. */
  2512. if (parent) {
  2513. /* update the clk tree topology */
  2514. __clk_set_parent_before(orphan, parent);
  2515. __clk_set_parent_after(orphan, parent, NULL);
  2516. __clk_recalc_accuracies(orphan);
  2517. __clk_recalc_rates(orphan, 0);
  2518. }
  2519. }
  2520. kref_init(&core->ref);
  2521. out:
  2522. clk_pm_runtime_put(core);
  2523. unlock:
  2524. if (ret)
  2525. hlist_del_init(&core->child_node);
  2526. clk_prepare_unlock();
  2527. if (!ret)
  2528. clk_debug_register(core);
  2529. return ret;
  2530. }
  2531. struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
  2532. const char *con_id)
  2533. {
  2534. struct clk *clk;
  2535. /* This is to allow this function to be chained to others */
  2536. if (IS_ERR_OR_NULL(hw))
  2537. return ERR_CAST(hw);
  2538. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  2539. if (!clk)
  2540. return ERR_PTR(-ENOMEM);
  2541. clk->core = hw->core;
  2542. clk->dev_id = dev_id;
  2543. clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
  2544. clk->max_rate = ULONG_MAX;
  2545. clk_prepare_lock();
  2546. hlist_add_head(&clk->clks_node, &hw->core->clks);
  2547. clk_prepare_unlock();
  2548. return clk;
  2549. }
  2550. /* keep in sync with __clk_put */
  2551. void __clk_free_clk(struct clk *clk)
  2552. {
  2553. clk_prepare_lock();
  2554. hlist_del(&clk->clks_node);
  2555. clk_prepare_unlock();
  2556. kfree_const(clk->con_id);
  2557. kfree(clk);
  2558. }
  2559. /**
  2560. * clk_register - allocate a new clock, register it and return an opaque cookie
  2561. * @dev: device that is registering this clock
  2562. * @hw: link to hardware-specific clock data
  2563. *
  2564. * clk_register is the primary interface for populating the clock tree with new
  2565. * clock nodes. It returns a pointer to the newly allocated struct clk which
  2566. * cannot be dereferenced by driver code but may be used in conjunction with the
  2567. * rest of the clock API. In the event of an error clk_register will return an
  2568. * error code; drivers must test for an error code after calling clk_register.
  2569. */
  2570. struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  2571. {
  2572. int i, ret;
  2573. struct clk_core *core;
  2574. core = kzalloc(sizeof(*core), GFP_KERNEL);
  2575. if (!core) {
  2576. ret = -ENOMEM;
  2577. goto fail_out;
  2578. }
  2579. core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
  2580. if (!core->name) {
  2581. ret = -ENOMEM;
  2582. goto fail_name;
  2583. }
  2584. if (WARN_ON(!hw->init->ops)) {
  2585. ret = -EINVAL;
  2586. goto fail_ops;
  2587. }
  2588. core->ops = hw->init->ops;
  2589. if (dev && pm_runtime_enabled(dev))
  2590. core->dev = dev;
  2591. if (dev && dev->driver)
  2592. core->owner = dev->driver->owner;
  2593. core->hw = hw;
  2594. core->flags = hw->init->flags;
  2595. core->num_parents = hw->init->num_parents;
  2596. core->min_rate = 0;
  2597. core->max_rate = ULONG_MAX;
  2598. hw->core = core;
  2599. /* allocate local copy in case parent_names is __initdata */
  2600. core->parent_names = kcalloc(core->num_parents, sizeof(char *),
  2601. GFP_KERNEL);
  2602. if (!core->parent_names) {
  2603. ret = -ENOMEM;
  2604. goto fail_parent_names;
  2605. }
  2606. /* copy each string name in case parent_names is __initdata */
  2607. for (i = 0; i < core->num_parents; i++) {
  2608. core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
  2609. GFP_KERNEL);
  2610. if (!core->parent_names[i]) {
  2611. ret = -ENOMEM;
  2612. goto fail_parent_names_copy;
  2613. }
  2614. }
  2615. /* avoid unnecessary string look-ups of clk_core's possible parents. */
  2616. core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
  2617. GFP_KERNEL);
  2618. if (!core->parents) {
  2619. ret = -ENOMEM;
  2620. goto fail_parents;
  2621. };
  2622. INIT_HLIST_HEAD(&core->clks);
  2623. hw->clk = __clk_create_clk(hw, NULL, NULL);
  2624. if (IS_ERR(hw->clk)) {
  2625. ret = PTR_ERR(hw->clk);
  2626. goto fail_parents;
  2627. }
  2628. ret = __clk_core_init(core);
  2629. if (!ret)
  2630. return hw->clk;
  2631. __clk_free_clk(hw->clk);
  2632. hw->clk = NULL;
  2633. fail_parents:
  2634. kfree(core->parents);
  2635. fail_parent_names_copy:
  2636. while (--i >= 0)
  2637. kfree_const(core->parent_names[i]);
  2638. kfree(core->parent_names);
  2639. fail_parent_names:
  2640. fail_ops:
  2641. kfree_const(core->name);
  2642. fail_name:
  2643. kfree(core);
  2644. fail_out:
  2645. return ERR_PTR(ret);
  2646. }
  2647. EXPORT_SYMBOL_GPL(clk_register);
  2648. /**
  2649. * clk_hw_register - register a clk_hw and return an error code
  2650. * @dev: device that is registering this clock
  2651. * @hw: link to hardware-specific clock data
  2652. *
  2653. * clk_hw_register is the primary interface for populating the clock tree with
  2654. * new clock nodes. It returns an integer equal to zero indicating success or
  2655. * less than zero indicating failure. Drivers must test for an error code after
  2656. * calling clk_hw_register().
  2657. */
  2658. int clk_hw_register(struct device *dev, struct clk_hw *hw)
  2659. {
  2660. return PTR_ERR_OR_ZERO(clk_register(dev, hw));
  2661. }
  2662. EXPORT_SYMBOL_GPL(clk_hw_register);
  2663. /* Free memory allocated for a clock. */
  2664. static void __clk_release(struct kref *ref)
  2665. {
  2666. struct clk_core *core = container_of(ref, struct clk_core, ref);
  2667. int i = core->num_parents;
  2668. lockdep_assert_held(&prepare_lock);
  2669. kfree(core->parents);
  2670. while (--i >= 0)
  2671. kfree_const(core->parent_names[i]);
  2672. kfree(core->parent_names);
  2673. kfree_const(core->name);
  2674. kfree(core);
  2675. }
  2676. /*
  2677. * Empty clk_ops for unregistered clocks. These are used temporarily
  2678. * after clk_unregister() was called on a clock and until last clock
  2679. * consumer calls clk_put() and the struct clk object is freed.
  2680. */
  2681. static int clk_nodrv_prepare_enable(struct clk_hw *hw)
  2682. {
  2683. return -ENXIO;
  2684. }
  2685. static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
  2686. {
  2687. WARN_ON_ONCE(1);
  2688. }
  2689. static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
  2690. unsigned long parent_rate)
  2691. {
  2692. return -ENXIO;
  2693. }
  2694. static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
  2695. {
  2696. return -ENXIO;
  2697. }
  2698. static const struct clk_ops clk_nodrv_ops = {
  2699. .enable = clk_nodrv_prepare_enable,
  2700. .disable = clk_nodrv_disable_unprepare,
  2701. .prepare = clk_nodrv_prepare_enable,
  2702. .unprepare = clk_nodrv_disable_unprepare,
  2703. .set_rate = clk_nodrv_set_rate,
  2704. .set_parent = clk_nodrv_set_parent,
  2705. };
  2706. static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
  2707. struct clk_core *target)
  2708. {
  2709. int i;
  2710. struct clk_core *child;
  2711. for (i = 0; i < root->num_parents; i++)
  2712. if (root->parents[i] == target)
  2713. root->parents[i] = NULL;
  2714. hlist_for_each_entry(child, &root->children, child_node)
  2715. clk_core_evict_parent_cache_subtree(child, target);
  2716. }
  2717. /* Remove this clk from all parent caches */
  2718. static void clk_core_evict_parent_cache(struct clk_core *core)
  2719. {
  2720. struct hlist_head **lists;
  2721. struct clk_core *root;
  2722. lockdep_assert_held(&prepare_lock);
  2723. for (lists = all_lists; *lists; lists++)
  2724. hlist_for_each_entry(root, *lists, child_node)
  2725. clk_core_evict_parent_cache_subtree(root, core);
  2726. }
  2727. /**
  2728. * clk_unregister - unregister a currently registered clock
  2729. * @clk: clock to unregister
  2730. */
  2731. void clk_unregister(struct clk *clk)
  2732. {
  2733. unsigned long flags;
  2734. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  2735. return;
  2736. clk_debug_unregister(clk->core);
  2737. clk_prepare_lock();
  2738. if (clk->core->ops == &clk_nodrv_ops) {
  2739. pr_err("%s: unregistered clock: %s\n", __func__,
  2740. clk->core->name);
  2741. goto unlock;
  2742. }
  2743. /*
  2744. * Assign empty clock ops for consumers that might still hold
  2745. * a reference to this clock.
  2746. */
  2747. flags = clk_enable_lock();
  2748. clk->core->ops = &clk_nodrv_ops;
  2749. clk_enable_unlock(flags);
  2750. if (!hlist_empty(&clk->core->children)) {
  2751. struct clk_core *child;
  2752. struct hlist_node *t;
  2753. /* Reparent all children to the orphan list. */
  2754. hlist_for_each_entry_safe(child, t, &clk->core->children,
  2755. child_node)
  2756. clk_core_set_parent_nolock(child, NULL);
  2757. }
  2758. clk_core_evict_parent_cache(clk->core);
  2759. hlist_del_init(&clk->core->child_node);
  2760. if (clk->core->prepare_count)
  2761. pr_warn("%s: unregistering prepared clock: %s\n",
  2762. __func__, clk->core->name);
  2763. if (clk->core->protect_count)
  2764. pr_warn("%s: unregistering protected clock: %s\n",
  2765. __func__, clk->core->name);
  2766. kref_put(&clk->core->ref, __clk_release);
  2767. unlock:
  2768. clk_prepare_unlock();
  2769. }
  2770. EXPORT_SYMBOL_GPL(clk_unregister);
  2771. /**
  2772. * clk_hw_unregister - unregister a currently registered clk_hw
  2773. * @hw: hardware-specific clock data to unregister
  2774. */
  2775. void clk_hw_unregister(struct clk_hw *hw)
  2776. {
  2777. clk_unregister(hw->clk);
  2778. }
  2779. EXPORT_SYMBOL_GPL(clk_hw_unregister);
  2780. static void devm_clk_release(struct device *dev, void *res)
  2781. {
  2782. clk_unregister(*(struct clk **)res);
  2783. }
  2784. static void devm_clk_hw_release(struct device *dev, void *res)
  2785. {
  2786. clk_hw_unregister(*(struct clk_hw **)res);
  2787. }
  2788. /**
  2789. * devm_clk_register - resource managed clk_register()
  2790. * @dev: device that is registering this clock
  2791. * @hw: link to hardware-specific clock data
  2792. *
  2793. * Managed clk_register(). Clocks returned from this function are
  2794. * automatically clk_unregister()ed on driver detach. See clk_register() for
  2795. * more information.
  2796. */
  2797. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
  2798. {
  2799. struct clk *clk;
  2800. struct clk **clkp;
  2801. clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
  2802. if (!clkp)
  2803. return ERR_PTR(-ENOMEM);
  2804. clk = clk_register(dev, hw);
  2805. if (!IS_ERR(clk)) {
  2806. *clkp = clk;
  2807. devres_add(dev, clkp);
  2808. } else {
  2809. devres_free(clkp);
  2810. }
  2811. return clk;
  2812. }
  2813. EXPORT_SYMBOL_GPL(devm_clk_register);
  2814. /**
  2815. * devm_clk_hw_register - resource managed clk_hw_register()
  2816. * @dev: device that is registering this clock
  2817. * @hw: link to hardware-specific clock data
  2818. *
  2819. * Managed clk_hw_register(). Clocks registered by this function are
  2820. * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
  2821. * for more information.
  2822. */
  2823. int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
  2824. {
  2825. struct clk_hw **hwp;
  2826. int ret;
  2827. hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
  2828. if (!hwp)
  2829. return -ENOMEM;
  2830. ret = clk_hw_register(dev, hw);
  2831. if (!ret) {
  2832. *hwp = hw;
  2833. devres_add(dev, hwp);
  2834. } else {
  2835. devres_free(hwp);
  2836. }
  2837. return ret;
  2838. }
  2839. EXPORT_SYMBOL_GPL(devm_clk_hw_register);
  2840. static int devm_clk_match(struct device *dev, void *res, void *data)
  2841. {
  2842. struct clk *c = res;
  2843. if (WARN_ON(!c))
  2844. return 0;
  2845. return c == data;
  2846. }
  2847. static int devm_clk_hw_match(struct device *dev, void *res, void *data)
  2848. {
  2849. struct clk_hw *hw = res;
  2850. if (WARN_ON(!hw))
  2851. return 0;
  2852. return hw == data;
  2853. }
  2854. /**
  2855. * devm_clk_unregister - resource managed clk_unregister()
  2856. * @clk: clock to unregister
  2857. *
  2858. * Deallocate a clock allocated with devm_clk_register(). Normally
  2859. * this function will not need to be called and the resource management
  2860. * code will ensure that the resource is freed.
  2861. */
  2862. void devm_clk_unregister(struct device *dev, struct clk *clk)
  2863. {
  2864. WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
  2865. }
  2866. EXPORT_SYMBOL_GPL(devm_clk_unregister);
  2867. /**
  2868. * devm_clk_hw_unregister - resource managed clk_hw_unregister()
  2869. * @dev: device that is unregistering the hardware-specific clock data
  2870. * @hw: link to hardware-specific clock data
  2871. *
  2872. * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
  2873. * this function will not need to be called and the resource management
  2874. * code will ensure that the resource is freed.
  2875. */
  2876. void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
  2877. {
  2878. WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
  2879. hw));
  2880. }
  2881. EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
  2882. /*
  2883. * clkdev helpers
  2884. */
  2885. int __clk_get(struct clk *clk)
  2886. {
  2887. struct clk_core *core = !clk ? NULL : clk->core;
  2888. if (core) {
  2889. if (!try_module_get(core->owner))
  2890. return 0;
  2891. kref_get(&core->ref);
  2892. }
  2893. return 1;
  2894. }
  2895. /* keep in sync with __clk_free_clk */
  2896. void __clk_put(struct clk *clk)
  2897. {
  2898. struct module *owner;
  2899. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  2900. return;
  2901. clk_prepare_lock();
  2902. /*
  2903. * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
  2904. * given user should be balanced with calls to clk_rate_exclusive_put()
  2905. * and by that same consumer
  2906. */
  2907. if (WARN_ON(clk->exclusive_count)) {
  2908. /* We voiced our concern, let's sanitize the situation */
  2909. clk->core->protect_count -= (clk->exclusive_count - 1);
  2910. clk_core_rate_unprotect(clk->core);
  2911. clk->exclusive_count = 0;
  2912. }
  2913. hlist_del(&clk->clks_node);
  2914. if (clk->min_rate > clk->core->req_rate ||
  2915. clk->max_rate < clk->core->req_rate)
  2916. clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
  2917. owner = clk->core->owner;
  2918. kref_put(&clk->core->ref, __clk_release);
  2919. clk_prepare_unlock();
  2920. module_put(owner);
  2921. kfree_const(clk->con_id);
  2922. kfree(clk);
  2923. }
  2924. /*** clk rate change notifiers ***/
  2925. /**
  2926. * clk_notifier_register - add a clk rate change notifier
  2927. * @clk: struct clk * to watch
  2928. * @nb: struct notifier_block * with callback info
  2929. *
  2930. * Request notification when clk's rate changes. This uses an SRCU
  2931. * notifier because we want it to block and notifier unregistrations are
  2932. * uncommon. The callbacks associated with the notifier must not
  2933. * re-enter into the clk framework by calling any top-level clk APIs;
  2934. * this will cause a nested prepare_lock mutex.
  2935. *
  2936. * In all notification cases (pre, post and abort rate change) the original
  2937. * clock rate is passed to the callback via struct clk_notifier_data.old_rate
  2938. * and the new frequency is passed via struct clk_notifier_data.new_rate.
  2939. *
  2940. * clk_notifier_register() must be called from non-atomic context.
  2941. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  2942. * allocation failure; otherwise, passes along the return value of
  2943. * srcu_notifier_chain_register().
  2944. */
  2945. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  2946. {
  2947. struct clk_notifier *cn;
  2948. int ret = -ENOMEM;
  2949. if (!clk || !nb)
  2950. return -EINVAL;
  2951. clk_prepare_lock();
  2952. /* search the list of notifiers for this clk */
  2953. list_for_each_entry(cn, &clk_notifier_list, node)
  2954. if (cn->clk == clk)
  2955. goto found;
  2956. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  2957. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  2958. if (!cn)
  2959. goto out;
  2960. cn->clk = clk;
  2961. srcu_init_notifier_head(&cn->notifier_head);
  2962. list_add(&cn->node, &clk_notifier_list);
  2963. found:
  2964. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  2965. clk->core->notifier_count++;
  2966. out:
  2967. clk_prepare_unlock();
  2968. return ret;
  2969. }
  2970. EXPORT_SYMBOL_GPL(clk_notifier_register);
  2971. /**
  2972. * clk_notifier_unregister - remove a clk rate change notifier
  2973. * @clk: struct clk *
  2974. * @nb: struct notifier_block * with callback info
  2975. *
  2976. * Request no further notification for changes to 'clk' and frees memory
  2977. * allocated in clk_notifier_register.
  2978. *
  2979. * Returns -EINVAL if called with null arguments; otherwise, passes
  2980. * along the return value of srcu_notifier_chain_unregister().
  2981. */
  2982. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  2983. {
  2984. struct clk_notifier *cn;
  2985. int ret = -ENOENT;
  2986. if (!clk || !nb)
  2987. return -EINVAL;
  2988. clk_prepare_lock();
  2989. list_for_each_entry(cn, &clk_notifier_list, node) {
  2990. if (cn->clk == clk) {
  2991. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  2992. clk->core->notifier_count--;
  2993. /* XXX the notifier code should handle this better */
  2994. if (!cn->notifier_head.head) {
  2995. srcu_cleanup_notifier_head(&cn->notifier_head);
  2996. list_del(&cn->node);
  2997. kfree(cn);
  2998. }
  2999. break;
  3000. }
  3001. }
  3002. clk_prepare_unlock();
  3003. return ret;
  3004. }
  3005. EXPORT_SYMBOL_GPL(clk_notifier_unregister);
  3006. #ifdef CONFIG_OF
  3007. /**
  3008. * struct of_clk_provider - Clock provider registration structure
  3009. * @link: Entry in global list of clock providers
  3010. * @node: Pointer to device tree node of clock provider
  3011. * @get: Get clock callback. Returns NULL or a struct clk for the
  3012. * given clock specifier
  3013. * @data: context pointer to be passed into @get callback
  3014. */
  3015. struct of_clk_provider {
  3016. struct list_head link;
  3017. struct device_node *node;
  3018. struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
  3019. struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
  3020. void *data;
  3021. };
  3022. static const struct of_device_id __clk_of_table_sentinel
  3023. __used __section(__clk_of_table_end);
  3024. static LIST_HEAD(of_clk_providers);
  3025. static DEFINE_MUTEX(of_clk_mutex);
  3026. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  3027. void *data)
  3028. {
  3029. return data;
  3030. }
  3031. EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
  3032. struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
  3033. {
  3034. return data;
  3035. }
  3036. EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
  3037. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
  3038. {
  3039. struct clk_onecell_data *clk_data = data;
  3040. unsigned int idx = clkspec->args[0];
  3041. if (idx >= clk_data->clk_num) {
  3042. pr_err("%s: invalid clock index %u\n", __func__, idx);
  3043. return ERR_PTR(-EINVAL);
  3044. }
  3045. return clk_data->clks[idx];
  3046. }
  3047. EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
  3048. struct clk_hw *
  3049. of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
  3050. {
  3051. struct clk_hw_onecell_data *hw_data = data;
  3052. unsigned int idx = clkspec->args[0];
  3053. if (idx >= hw_data->num) {
  3054. pr_err("%s: invalid index %u\n", __func__, idx);
  3055. return ERR_PTR(-EINVAL);
  3056. }
  3057. return hw_data->hws[idx];
  3058. }
  3059. EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
  3060. /**
  3061. * of_clk_add_provider() - Register a clock provider for a node
  3062. * @np: Device node pointer associated with clock provider
  3063. * @clk_src_get: callback for decoding clock
  3064. * @data: context pointer for @clk_src_get callback.
  3065. */
  3066. int of_clk_add_provider(struct device_node *np,
  3067. struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
  3068. void *data),
  3069. void *data)
  3070. {
  3071. struct of_clk_provider *cp;
  3072. int ret;
  3073. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  3074. if (!cp)
  3075. return -ENOMEM;
  3076. cp->node = of_node_get(np);
  3077. cp->data = data;
  3078. cp->get = clk_src_get;
  3079. mutex_lock(&of_clk_mutex);
  3080. list_add(&cp->link, &of_clk_providers);
  3081. mutex_unlock(&of_clk_mutex);
  3082. pr_debug("Added clock from %pOF\n", np);
  3083. ret = of_clk_set_defaults(np, true);
  3084. if (ret < 0)
  3085. of_clk_del_provider(np);
  3086. return ret;
  3087. }
  3088. EXPORT_SYMBOL_GPL(of_clk_add_provider);
  3089. /**
  3090. * of_clk_add_hw_provider() - Register a clock provider for a node
  3091. * @np: Device node pointer associated with clock provider
  3092. * @get: callback for decoding clk_hw
  3093. * @data: context pointer for @get callback.
  3094. */
  3095. int of_clk_add_hw_provider(struct device_node *np,
  3096. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  3097. void *data),
  3098. void *data)
  3099. {
  3100. struct of_clk_provider *cp;
  3101. int ret;
  3102. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  3103. if (!cp)
  3104. return -ENOMEM;
  3105. cp->node = of_node_get(np);
  3106. cp->data = data;
  3107. cp->get_hw = get;
  3108. mutex_lock(&of_clk_mutex);
  3109. list_add(&cp->link, &of_clk_providers);
  3110. mutex_unlock(&of_clk_mutex);
  3111. pr_debug("Added clk_hw provider from %pOF\n", np);
  3112. ret = of_clk_set_defaults(np, true);
  3113. if (ret < 0)
  3114. of_clk_del_provider(np);
  3115. return ret;
  3116. }
  3117. EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
  3118. static void devm_of_clk_release_provider(struct device *dev, void *res)
  3119. {
  3120. of_clk_del_provider(*(struct device_node **)res);
  3121. }
  3122. int devm_of_clk_add_hw_provider(struct device *dev,
  3123. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  3124. void *data),
  3125. void *data)
  3126. {
  3127. struct device_node **ptr, *np;
  3128. int ret;
  3129. ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
  3130. GFP_KERNEL);
  3131. if (!ptr)
  3132. return -ENOMEM;
  3133. np = dev->of_node;
  3134. ret = of_clk_add_hw_provider(np, get, data);
  3135. if (!ret) {
  3136. *ptr = np;
  3137. devres_add(dev, ptr);
  3138. } else {
  3139. devres_free(ptr);
  3140. }
  3141. return ret;
  3142. }
  3143. EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
  3144. /**
  3145. * of_clk_del_provider() - Remove a previously registered clock provider
  3146. * @np: Device node pointer associated with clock provider
  3147. */
  3148. void of_clk_del_provider(struct device_node *np)
  3149. {
  3150. struct of_clk_provider *cp;
  3151. mutex_lock(&of_clk_mutex);
  3152. list_for_each_entry(cp, &of_clk_providers, link) {
  3153. if (cp->node == np) {
  3154. list_del(&cp->link);
  3155. of_node_put(cp->node);
  3156. kfree(cp);
  3157. break;
  3158. }
  3159. }
  3160. mutex_unlock(&of_clk_mutex);
  3161. }
  3162. EXPORT_SYMBOL_GPL(of_clk_del_provider);
  3163. static int devm_clk_provider_match(struct device *dev, void *res, void *data)
  3164. {
  3165. struct device_node **np = res;
  3166. if (WARN_ON(!np || !*np))
  3167. return 0;
  3168. return *np == data;
  3169. }
  3170. void devm_of_clk_del_provider(struct device *dev)
  3171. {
  3172. int ret;
  3173. ret = devres_release(dev, devm_of_clk_release_provider,
  3174. devm_clk_provider_match, dev->of_node);
  3175. WARN_ON(ret);
  3176. }
  3177. EXPORT_SYMBOL(devm_of_clk_del_provider);
  3178. static struct clk_hw *
  3179. __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
  3180. struct of_phandle_args *clkspec)
  3181. {
  3182. struct clk *clk;
  3183. if (provider->get_hw)
  3184. return provider->get_hw(clkspec, provider->data);
  3185. clk = provider->get(clkspec, provider->data);
  3186. if (IS_ERR(clk))
  3187. return ERR_CAST(clk);
  3188. return __clk_get_hw(clk);
  3189. }
  3190. struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
  3191. const char *dev_id, const char *con_id)
  3192. {
  3193. struct of_clk_provider *provider;
  3194. struct clk *clk = ERR_PTR(-EPROBE_DEFER);
  3195. struct clk_hw *hw;
  3196. if (!clkspec)
  3197. return ERR_PTR(-EINVAL);
  3198. /* Check if we have such a provider in our array */
  3199. mutex_lock(&of_clk_mutex);
  3200. list_for_each_entry(provider, &of_clk_providers, link) {
  3201. if (provider->node == clkspec->np) {
  3202. hw = __of_clk_get_hw_from_provider(provider, clkspec);
  3203. clk = __clk_create_clk(hw, dev_id, con_id);
  3204. }
  3205. if (!IS_ERR(clk)) {
  3206. if (!__clk_get(clk)) {
  3207. __clk_free_clk(clk);
  3208. clk = ERR_PTR(-ENOENT);
  3209. }
  3210. break;
  3211. }
  3212. }
  3213. mutex_unlock(&of_clk_mutex);
  3214. return clk;
  3215. }
  3216. /**
  3217. * of_clk_get_from_provider() - Lookup a clock from a clock provider
  3218. * @clkspec: pointer to a clock specifier data structure
  3219. *
  3220. * This function looks up a struct clk from the registered list of clock
  3221. * providers, an input is a clock specifier data structure as returned
  3222. * from the of_parse_phandle_with_args() function call.
  3223. */
  3224. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  3225. {
  3226. return __of_clk_get_from_provider(clkspec, NULL, __func__);
  3227. }
  3228. EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
  3229. /**
  3230. * of_clk_get_parent_count() - Count the number of clocks a device node has
  3231. * @np: device node to count
  3232. *
  3233. * Returns: The number of clocks that are possible parents of this node
  3234. */
  3235. unsigned int of_clk_get_parent_count(struct device_node *np)
  3236. {
  3237. int count;
  3238. count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  3239. if (count < 0)
  3240. return 0;
  3241. return count;
  3242. }
  3243. EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
  3244. const char *of_clk_get_parent_name(struct device_node *np, int index)
  3245. {
  3246. struct of_phandle_args clkspec;
  3247. struct property *prop;
  3248. const char *clk_name;
  3249. const __be32 *vp;
  3250. u32 pv;
  3251. int rc;
  3252. int count;
  3253. struct clk *clk;
  3254. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  3255. &clkspec);
  3256. if (rc)
  3257. return NULL;
  3258. index = clkspec.args_count ? clkspec.args[0] : 0;
  3259. count = 0;
  3260. /* if there is an indices property, use it to transfer the index
  3261. * specified into an array offset for the clock-output-names property.
  3262. */
  3263. of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
  3264. if (index == pv) {
  3265. index = count;
  3266. break;
  3267. }
  3268. count++;
  3269. }
  3270. /* We went off the end of 'clock-indices' without finding it */
  3271. if (prop && !vp)
  3272. return NULL;
  3273. if (of_property_read_string_index(clkspec.np, "clock-output-names",
  3274. index,
  3275. &clk_name) < 0) {
  3276. /*
  3277. * Best effort to get the name if the clock has been
  3278. * registered with the framework. If the clock isn't
  3279. * registered, we return the node name as the name of
  3280. * the clock as long as #clock-cells = 0.
  3281. */
  3282. clk = of_clk_get_from_provider(&clkspec);
  3283. if (IS_ERR(clk)) {
  3284. if (clkspec.args_count == 0)
  3285. clk_name = clkspec.np->name;
  3286. else
  3287. clk_name = NULL;
  3288. } else {
  3289. clk_name = __clk_get_name(clk);
  3290. clk_put(clk);
  3291. }
  3292. }
  3293. of_node_put(clkspec.np);
  3294. return clk_name;
  3295. }
  3296. EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
  3297. /**
  3298. * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
  3299. * number of parents
  3300. * @np: Device node pointer associated with clock provider
  3301. * @parents: pointer to char array that hold the parents' names
  3302. * @size: size of the @parents array
  3303. *
  3304. * Return: number of parents for the clock node.
  3305. */
  3306. int of_clk_parent_fill(struct device_node *np, const char **parents,
  3307. unsigned int size)
  3308. {
  3309. unsigned int i = 0;
  3310. while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
  3311. i++;
  3312. return i;
  3313. }
  3314. EXPORT_SYMBOL_GPL(of_clk_parent_fill);
  3315. struct clock_provider {
  3316. void (*clk_init_cb)(struct device_node *);
  3317. struct device_node *np;
  3318. struct list_head node;
  3319. };
  3320. /*
  3321. * This function looks for a parent clock. If there is one, then it
  3322. * checks that the provider for this parent clock was initialized, in
  3323. * this case the parent clock will be ready.
  3324. */
  3325. static int parent_ready(struct device_node *np)
  3326. {
  3327. int i = 0;
  3328. while (true) {
  3329. struct clk *clk = of_clk_get(np, i);
  3330. /* this parent is ready we can check the next one */
  3331. if (!IS_ERR(clk)) {
  3332. clk_put(clk);
  3333. i++;
  3334. continue;
  3335. }
  3336. /* at least one parent is not ready, we exit now */
  3337. if (PTR_ERR(clk) == -EPROBE_DEFER)
  3338. return 0;
  3339. /*
  3340. * Here we make assumption that the device tree is
  3341. * written correctly. So an error means that there is
  3342. * no more parent. As we didn't exit yet, then the
  3343. * previous parent are ready. If there is no clock
  3344. * parent, no need to wait for them, then we can
  3345. * consider their absence as being ready
  3346. */
  3347. return 1;
  3348. }
  3349. }
  3350. /**
  3351. * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
  3352. * @np: Device node pointer associated with clock provider
  3353. * @index: clock index
  3354. * @flags: pointer to top-level framework flags
  3355. *
  3356. * Detects if the clock-critical property exists and, if so, sets the
  3357. * corresponding CLK_IS_CRITICAL flag.
  3358. *
  3359. * Do not use this function. It exists only for legacy Device Tree
  3360. * bindings, such as the one-clock-per-node style that are outdated.
  3361. * Those bindings typically put all clock data into .dts and the Linux
  3362. * driver has no clock data, thus making it impossible to set this flag
  3363. * correctly from the driver. Only those drivers may call
  3364. * of_clk_detect_critical from their setup functions.
  3365. *
  3366. * Return: error code or zero on success
  3367. */
  3368. int of_clk_detect_critical(struct device_node *np,
  3369. int index, unsigned long *flags)
  3370. {
  3371. struct property *prop;
  3372. const __be32 *cur;
  3373. uint32_t idx;
  3374. if (!np || !flags)
  3375. return -EINVAL;
  3376. of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
  3377. if (index == idx)
  3378. *flags |= CLK_IS_CRITICAL;
  3379. return 0;
  3380. }
  3381. /**
  3382. * of_clk_init() - Scan and init clock providers from the DT
  3383. * @matches: array of compatible values and init functions for providers.
  3384. *
  3385. * This function scans the device tree for matching clock providers
  3386. * and calls their initialization functions. It also does it by trying
  3387. * to follow the dependencies.
  3388. */
  3389. void __init of_clk_init(const struct of_device_id *matches)
  3390. {
  3391. const struct of_device_id *match;
  3392. struct device_node *np;
  3393. struct clock_provider *clk_provider, *next;
  3394. bool is_init_done;
  3395. bool force = false;
  3396. LIST_HEAD(clk_provider_list);
  3397. if (!matches)
  3398. matches = &__clk_of_table;
  3399. /* First prepare the list of the clocks providers */
  3400. for_each_matching_node_and_match(np, matches, &match) {
  3401. struct clock_provider *parent;
  3402. if (!of_device_is_available(np))
  3403. continue;
  3404. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  3405. if (!parent) {
  3406. list_for_each_entry_safe(clk_provider, next,
  3407. &clk_provider_list, node) {
  3408. list_del(&clk_provider->node);
  3409. of_node_put(clk_provider->np);
  3410. kfree(clk_provider);
  3411. }
  3412. of_node_put(np);
  3413. return;
  3414. }
  3415. parent->clk_init_cb = match->data;
  3416. parent->np = of_node_get(np);
  3417. list_add_tail(&parent->node, &clk_provider_list);
  3418. }
  3419. while (!list_empty(&clk_provider_list)) {
  3420. is_init_done = false;
  3421. list_for_each_entry_safe(clk_provider, next,
  3422. &clk_provider_list, node) {
  3423. if (force || parent_ready(clk_provider->np)) {
  3424. /* Don't populate platform devices */
  3425. of_node_set_flag(clk_provider->np,
  3426. OF_POPULATED);
  3427. clk_provider->clk_init_cb(clk_provider->np);
  3428. of_clk_set_defaults(clk_provider->np, true);
  3429. list_del(&clk_provider->node);
  3430. of_node_put(clk_provider->np);
  3431. kfree(clk_provider);
  3432. is_init_done = true;
  3433. }
  3434. }
  3435. /*
  3436. * We didn't manage to initialize any of the
  3437. * remaining providers during the last loop, so now we
  3438. * initialize all the remaining ones unconditionally
  3439. * in case the clock parent was not mandatory
  3440. */
  3441. if (!is_init_done)
  3442. force = true;
  3443. }
  3444. }
  3445. #endif