clk.c 103 KB

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