clk.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010-2011 Canonical Ltd <[email protected]>
  4. * Copyright (C) 2011-2012 Linaro Ltd <[email protected]>
  5. *
  6. * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/clk/clk-conf.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/err.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/sched.h>
  22. #include <linux/clkdev.h>
  23. #include "clk.h"
  24. static DEFINE_SPINLOCK(enable_lock);
  25. static DEFINE_MUTEX(prepare_lock);
  26. static struct task_struct *prepare_owner;
  27. static struct task_struct *enable_owner;
  28. static int prepare_refcnt;
  29. static int enable_refcnt;
  30. static HLIST_HEAD(clk_root_list);
  31. static HLIST_HEAD(clk_orphan_list);
  32. static LIST_HEAD(clk_notifier_list);
  33. static const struct hlist_head *all_lists[] = {
  34. &clk_root_list,
  35. &clk_orphan_list,
  36. NULL,
  37. };
  38. /*** private data structures ***/
  39. struct clk_parent_map {
  40. const struct clk_hw *hw;
  41. struct clk_core *core;
  42. const char *fw_name;
  43. const char *name;
  44. int index;
  45. };
  46. struct clk_core {
  47. const char *name;
  48. const struct clk_ops *ops;
  49. struct clk_hw *hw;
  50. struct module *owner;
  51. struct device *dev;
  52. struct device_node *of_node;
  53. struct clk_core *parent;
  54. struct clk_parent_map *parents;
  55. u8 num_parents;
  56. u8 new_parent_index;
  57. unsigned long rate;
  58. unsigned long req_rate;
  59. unsigned long new_rate;
  60. struct clk_core *new_parent;
  61. struct clk_core *new_child;
  62. unsigned long flags;
  63. bool orphan;
  64. bool rpm_enabled;
  65. bool need_sync;
  66. bool boot_enabled;
  67. unsigned int enable_count;
  68. unsigned int prepare_count;
  69. unsigned int protect_count;
  70. unsigned long min_rate;
  71. unsigned long max_rate;
  72. unsigned long accuracy;
  73. int phase;
  74. struct clk_duty duty;
  75. struct hlist_head children;
  76. struct hlist_node child_node;
  77. struct hlist_head clks;
  78. unsigned int notifier_count;
  79. #ifdef CONFIG_DEBUG_FS
  80. struct dentry *dentry;
  81. struct hlist_node debug_node;
  82. #endif
  83. struct kref ref;
  84. };
  85. #define CREATE_TRACE_POINTS
  86. #include <trace/events/clk.h>
  87. struct clk {
  88. struct clk_core *core;
  89. struct device *dev;
  90. const char *dev_id;
  91. const char *con_id;
  92. unsigned long min_rate;
  93. unsigned long max_rate;
  94. unsigned int exclusive_count;
  95. struct hlist_node clks_node;
  96. };
  97. /*** runtime pm ***/
  98. static int clk_pm_runtime_get(struct clk_core *core)
  99. {
  100. if (!core->rpm_enabled)
  101. return 0;
  102. return pm_runtime_resume_and_get(core->dev);
  103. }
  104. static void clk_pm_runtime_put(struct clk_core *core)
  105. {
  106. if (!core->rpm_enabled)
  107. return;
  108. pm_runtime_put_sync(core->dev);
  109. }
  110. /*** locking ***/
  111. static void clk_prepare_lock(void)
  112. {
  113. if (!mutex_trylock(&prepare_lock)) {
  114. if (prepare_owner == current) {
  115. prepare_refcnt++;
  116. return;
  117. }
  118. mutex_lock(&prepare_lock);
  119. }
  120. WARN_ON_ONCE(prepare_owner != NULL);
  121. WARN_ON_ONCE(prepare_refcnt != 0);
  122. prepare_owner = current;
  123. prepare_refcnt = 1;
  124. }
  125. static void clk_prepare_unlock(void)
  126. {
  127. WARN_ON_ONCE(prepare_owner != current);
  128. WARN_ON_ONCE(prepare_refcnt == 0);
  129. if (--prepare_refcnt)
  130. return;
  131. prepare_owner = NULL;
  132. mutex_unlock(&prepare_lock);
  133. }
  134. static unsigned long clk_enable_lock(void)
  135. __acquires(enable_lock)
  136. {
  137. unsigned long flags;
  138. /*
  139. * On UP systems, spin_trylock_irqsave() always returns true, even if
  140. * we already hold the lock. So, in that case, we rely only on
  141. * reference counting.
  142. */
  143. if (!IS_ENABLED(CONFIG_SMP) ||
  144. !spin_trylock_irqsave(&enable_lock, flags)) {
  145. if (enable_owner == current) {
  146. enable_refcnt++;
  147. __acquire(enable_lock);
  148. if (!IS_ENABLED(CONFIG_SMP))
  149. local_save_flags(flags);
  150. return flags;
  151. }
  152. spin_lock_irqsave(&enable_lock, flags);
  153. }
  154. WARN_ON_ONCE(enable_owner != NULL);
  155. WARN_ON_ONCE(enable_refcnt != 0);
  156. enable_owner = current;
  157. enable_refcnt = 1;
  158. return flags;
  159. }
  160. static void clk_enable_unlock(unsigned long flags)
  161. __releases(enable_lock)
  162. {
  163. WARN_ON_ONCE(enable_owner != current);
  164. WARN_ON_ONCE(enable_refcnt == 0);
  165. if (--enable_refcnt) {
  166. __release(enable_lock);
  167. return;
  168. }
  169. enable_owner = NULL;
  170. spin_unlock_irqrestore(&enable_lock, flags);
  171. }
  172. static bool clk_core_rate_is_protected(struct clk_core *core)
  173. {
  174. return core->protect_count;
  175. }
  176. static bool clk_core_is_prepared(struct clk_core *core)
  177. {
  178. bool ret = false;
  179. /*
  180. * .is_prepared is optional for clocks that can prepare
  181. * fall back to software usage counter if it is missing
  182. */
  183. if (!core->ops->is_prepared)
  184. return core->prepare_count;
  185. if (!clk_pm_runtime_get(core)) {
  186. ret = core->ops->is_prepared(core->hw);
  187. clk_pm_runtime_put(core);
  188. }
  189. return ret;
  190. }
  191. static bool clk_core_is_enabled(struct clk_core *core)
  192. {
  193. bool ret = false;
  194. /*
  195. * .is_enabled is only mandatory for clocks that gate
  196. * fall back to software usage counter if .is_enabled is missing
  197. */
  198. if (!core->ops->is_enabled)
  199. return core->enable_count;
  200. /*
  201. * Check if clock controller's device is runtime active before
  202. * calling .is_enabled callback. If not, assume that clock is
  203. * disabled, because we might be called from atomic context, from
  204. * which pm_runtime_get() is not allowed.
  205. * This function is called mainly from clk_disable_unused_subtree,
  206. * which ensures proper runtime pm activation of controller before
  207. * taking enable spinlock, but the below check is needed if one tries
  208. * to call it from other places.
  209. */
  210. if (core->rpm_enabled) {
  211. pm_runtime_get_noresume(core->dev);
  212. if (!pm_runtime_active(core->dev)) {
  213. ret = false;
  214. goto done;
  215. }
  216. }
  217. ret = core->ops->is_enabled(core->hw);
  218. done:
  219. if (core->rpm_enabled)
  220. pm_runtime_put(core->dev);
  221. return ret;
  222. }
  223. /*** helper functions ***/
  224. const char *__clk_get_name(const struct clk *clk)
  225. {
  226. return !clk ? NULL : clk->core->name;
  227. }
  228. EXPORT_SYMBOL_GPL(__clk_get_name);
  229. const char *clk_hw_get_name(const struct clk_hw *hw)
  230. {
  231. return hw->core->name;
  232. }
  233. EXPORT_SYMBOL_GPL(clk_hw_get_name);
  234. struct clk_hw *__clk_get_hw(struct clk *clk)
  235. {
  236. return !clk ? NULL : clk->core->hw;
  237. }
  238. EXPORT_SYMBOL_GPL(__clk_get_hw);
  239. unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
  240. {
  241. return hw->core->num_parents;
  242. }
  243. EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
  244. struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
  245. {
  246. return hw->core->parent ? hw->core->parent->hw : NULL;
  247. }
  248. EXPORT_SYMBOL_GPL(clk_hw_get_parent);
  249. static struct clk_core *__clk_lookup_subtree(const char *name,
  250. struct clk_core *core)
  251. {
  252. struct clk_core *child;
  253. struct clk_core *ret;
  254. if (!strcmp(core->name, name))
  255. return core;
  256. hlist_for_each_entry(child, &core->children, child_node) {
  257. ret = __clk_lookup_subtree(name, child);
  258. if (ret)
  259. return ret;
  260. }
  261. return NULL;
  262. }
  263. static struct clk_core *clk_core_lookup(const char *name)
  264. {
  265. struct clk_core *root_clk;
  266. struct clk_core *ret;
  267. if (!name)
  268. return NULL;
  269. /* search the 'proper' clk tree first */
  270. hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
  271. ret = __clk_lookup_subtree(name, root_clk);
  272. if (ret)
  273. return ret;
  274. }
  275. /* if not found, then search the orphan tree */
  276. hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
  277. ret = __clk_lookup_subtree(name, root_clk);
  278. if (ret)
  279. return ret;
  280. }
  281. return NULL;
  282. }
  283. #ifdef CONFIG_OF
  284. static int of_parse_clkspec(const struct device_node *np, int index,
  285. const char *name, struct of_phandle_args *out_args);
  286. static struct clk_hw *
  287. of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec);
  288. #else
  289. static inline int of_parse_clkspec(const struct device_node *np, int index,
  290. const char *name,
  291. struct of_phandle_args *out_args)
  292. {
  293. return -ENOENT;
  294. }
  295. static inline struct clk_hw *
  296. of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
  297. {
  298. return ERR_PTR(-ENOENT);
  299. }
  300. #endif
  301. /**
  302. * clk_core_get - Find the clk_core parent of a clk
  303. * @core: clk to find parent of
  304. * @p_index: parent index to search for
  305. *
  306. * This is the preferred method for clk providers to find the parent of a
  307. * clk when that parent is external to the clk controller. The parent_names
  308. * array is indexed and treated as a local name matching a string in the device
  309. * node's 'clock-names' property or as the 'con_id' matching the device's
  310. * dev_name() in a clk_lookup. This allows clk providers to use their own
  311. * namespace instead of looking for a globally unique parent string.
  312. *
  313. * For example the following DT snippet would allow a clock registered by the
  314. * clock-controller@c001 that has a clk_init_data::parent_data array
  315. * with 'xtal' in the 'name' member to find the clock provided by the
  316. * clock-controller@f00abcd without needing to get the globally unique name of
  317. * the xtal clk.
  318. *
  319. * parent: clock-controller@f00abcd {
  320. * reg = <0xf00abcd 0xabcd>;
  321. * #clock-cells = <0>;
  322. * };
  323. *
  324. * clock-controller@c001 {
  325. * reg = <0xc001 0xf00d>;
  326. * clocks = <&parent>;
  327. * clock-names = "xtal";
  328. * #clock-cells = <1>;
  329. * };
  330. *
  331. * Returns: -ENOENT when the provider can't be found or the clk doesn't
  332. * exist in the provider or the name can't be found in the DT node or
  333. * in a clkdev lookup. NULL when the provider knows about the clk but it
  334. * isn't provided on this system.
  335. * A valid clk_core pointer when the clk can be found in the provider.
  336. */
  337. static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
  338. {
  339. const char *name = core->parents[p_index].fw_name;
  340. int index = core->parents[p_index].index;
  341. struct clk_hw *hw = ERR_PTR(-ENOENT);
  342. struct device *dev = core->dev;
  343. const char *dev_id = dev ? dev_name(dev) : NULL;
  344. struct device_node *np = core->of_node;
  345. struct of_phandle_args clkspec;
  346. if (np && (name || index >= 0) &&
  347. !of_parse_clkspec(np, index, name, &clkspec)) {
  348. hw = of_clk_get_hw_from_clkspec(&clkspec);
  349. of_node_put(clkspec.np);
  350. } else if (name) {
  351. /*
  352. * If the DT search above couldn't find the provider fallback to
  353. * looking up via clkdev based clk_lookups.
  354. */
  355. hw = clk_find_hw(dev_id, name);
  356. }
  357. if (IS_ERR(hw))
  358. return ERR_CAST(hw);
  359. return hw->core;
  360. }
  361. static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
  362. {
  363. struct clk_parent_map *entry = &core->parents[index];
  364. struct clk_core *parent;
  365. if (entry->hw) {
  366. parent = entry->hw->core;
  367. } else {
  368. parent = clk_core_get(core, index);
  369. if (PTR_ERR(parent) == -ENOENT && entry->name)
  370. parent = clk_core_lookup(entry->name);
  371. }
  372. /*
  373. * We have a direct reference but it isn't registered yet?
  374. * Orphan it and let clk_reparent() update the orphan status
  375. * when the parent is registered.
  376. */
  377. if (!parent)
  378. parent = ERR_PTR(-EPROBE_DEFER);
  379. /* Only cache it if it's not an error */
  380. if (!IS_ERR(parent))
  381. entry->core = parent;
  382. }
  383. static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
  384. u8 index)
  385. {
  386. if (!core || index >= core->num_parents || !core->parents)
  387. return NULL;
  388. if (!core->parents[index].core)
  389. clk_core_fill_parent_index(core, index);
  390. return core->parents[index].core;
  391. }
  392. struct clk_hw *
  393. clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
  394. {
  395. struct clk_core *parent;
  396. parent = clk_core_get_parent_by_index(hw->core, index);
  397. return !parent ? NULL : parent->hw;
  398. }
  399. EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
  400. unsigned int __clk_get_enable_count(struct clk *clk)
  401. {
  402. return !clk ? 0 : clk->core->enable_count;
  403. }
  404. static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
  405. {
  406. if (!core)
  407. return 0;
  408. if (!core->num_parents || core->parent)
  409. return core->rate;
  410. /*
  411. * Clk must have a parent because num_parents > 0 but the parent isn't
  412. * known yet. Best to return 0 as the rate of this clk until we can
  413. * properly recalc the rate based on the parent's rate.
  414. */
  415. return 0;
  416. }
  417. unsigned long clk_hw_get_rate(const struct clk_hw *hw)
  418. {
  419. return clk_core_get_rate_nolock(hw->core);
  420. }
  421. EXPORT_SYMBOL_GPL(clk_hw_get_rate);
  422. static unsigned long clk_core_get_accuracy_no_lock(struct clk_core *core)
  423. {
  424. if (!core)
  425. return 0;
  426. return core->accuracy;
  427. }
  428. unsigned long clk_hw_get_flags(const struct clk_hw *hw)
  429. {
  430. return hw->core->flags;
  431. }
  432. EXPORT_SYMBOL_GPL(clk_hw_get_flags);
  433. bool clk_hw_is_prepared(const struct clk_hw *hw)
  434. {
  435. return clk_core_is_prepared(hw->core);
  436. }
  437. EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
  438. bool clk_hw_rate_is_protected(const struct clk_hw *hw)
  439. {
  440. return clk_core_rate_is_protected(hw->core);
  441. }
  442. EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
  443. bool clk_hw_is_enabled(const struct clk_hw *hw)
  444. {
  445. return clk_core_is_enabled(hw->core);
  446. }
  447. EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
  448. bool __clk_is_enabled(struct clk *clk)
  449. {
  450. if (!clk)
  451. return false;
  452. return clk_core_is_enabled(clk->core);
  453. }
  454. EXPORT_SYMBOL_GPL(__clk_is_enabled);
  455. static bool mux_is_better_rate(unsigned long rate, unsigned long now,
  456. unsigned long best, unsigned long flags)
  457. {
  458. if (flags & CLK_MUX_ROUND_CLOSEST)
  459. return abs(now - rate) < abs(best - rate);
  460. return now <= rate && now > best;
  461. }
  462. static void clk_core_init_rate_req(struct clk_core * const core,
  463. struct clk_rate_request *req,
  464. unsigned long rate);
  465. static int clk_core_round_rate_nolock(struct clk_core *core,
  466. struct clk_rate_request *req);
  467. static bool clk_core_has_parent(struct clk_core *core, const struct clk_core *parent)
  468. {
  469. struct clk_core *tmp;
  470. unsigned int i;
  471. /* Optimize for the case where the parent is already the parent. */
  472. if (core->parent == parent)
  473. return true;
  474. for (i = 0; i < core->num_parents; i++) {
  475. tmp = clk_core_get_parent_by_index(core, i);
  476. if (!tmp)
  477. continue;
  478. if (tmp == parent)
  479. return true;
  480. }
  481. return false;
  482. }
  483. static void
  484. clk_core_forward_rate_req(struct clk_core *core,
  485. const struct clk_rate_request *old_req,
  486. struct clk_core *parent,
  487. struct clk_rate_request *req,
  488. unsigned long parent_rate)
  489. {
  490. if (WARN_ON(!clk_core_has_parent(core, parent)))
  491. return;
  492. clk_core_init_rate_req(parent, req, parent_rate);
  493. if (req->min_rate < old_req->min_rate)
  494. req->min_rate = old_req->min_rate;
  495. if (req->max_rate > old_req->max_rate)
  496. req->max_rate = old_req->max_rate;
  497. }
  498. int clk_mux_determine_rate_flags(struct clk_hw *hw,
  499. struct clk_rate_request *req,
  500. unsigned long flags)
  501. {
  502. struct clk_core *core = hw->core, *parent, *best_parent = NULL;
  503. int i, num_parents, ret;
  504. unsigned long best = 0;
  505. /* if NO_REPARENT flag set, pass through to current parent */
  506. if (core->flags & CLK_SET_RATE_NO_REPARENT) {
  507. parent = core->parent;
  508. if (core->flags & CLK_SET_RATE_PARENT) {
  509. struct clk_rate_request parent_req;
  510. if (!parent) {
  511. req->rate = 0;
  512. return 0;
  513. }
  514. clk_core_forward_rate_req(core, req, parent, &parent_req, req->rate);
  515. ret = clk_core_round_rate_nolock(parent, &parent_req);
  516. if (ret)
  517. return ret;
  518. best = parent_req.rate;
  519. } else if (parent) {
  520. best = clk_core_get_rate_nolock(parent);
  521. } else {
  522. best = clk_core_get_rate_nolock(core);
  523. }
  524. goto out;
  525. }
  526. /* find the parent that can provide the fastest rate <= rate */
  527. num_parents = core->num_parents;
  528. for (i = 0; i < num_parents; i++) {
  529. unsigned long parent_rate;
  530. parent = clk_core_get_parent_by_index(core, i);
  531. if (!parent)
  532. continue;
  533. if (core->flags & CLK_SET_RATE_PARENT) {
  534. struct clk_rate_request parent_req;
  535. clk_core_forward_rate_req(core, req, parent, &parent_req, req->rate);
  536. ret = clk_core_round_rate_nolock(parent, &parent_req);
  537. if (ret)
  538. continue;
  539. parent_rate = parent_req.rate;
  540. } else {
  541. parent_rate = clk_core_get_rate_nolock(parent);
  542. }
  543. if (mux_is_better_rate(req->rate, parent_rate,
  544. best, flags)) {
  545. best_parent = parent;
  546. best = parent_rate;
  547. }
  548. }
  549. if (!best_parent)
  550. return -EINVAL;
  551. out:
  552. if (best_parent)
  553. req->best_parent_hw = best_parent->hw;
  554. req->best_parent_rate = best;
  555. req->rate = best;
  556. return 0;
  557. }
  558. EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
  559. struct clk *__clk_lookup(const char *name)
  560. {
  561. struct clk_core *core = clk_core_lookup(name);
  562. return !core ? NULL : core->hw->clk;
  563. }
  564. static void clk_core_get_boundaries(struct clk_core *core,
  565. unsigned long *min_rate,
  566. unsigned long *max_rate)
  567. {
  568. struct clk *clk_user;
  569. lockdep_assert_held(&prepare_lock);
  570. *min_rate = core->min_rate;
  571. *max_rate = core->max_rate;
  572. hlist_for_each_entry(clk_user, &core->clks, clks_node)
  573. *min_rate = max(*min_rate, clk_user->min_rate);
  574. hlist_for_each_entry(clk_user, &core->clks, clks_node)
  575. *max_rate = min(*max_rate, clk_user->max_rate);
  576. }
  577. /*
  578. * clk_hw_get_rate_range() - returns the clock rate range for a hw clk
  579. * @hw: the hw clk we want to get the range from
  580. * @min_rate: pointer to the variable that will hold the minimum
  581. * @max_rate: pointer to the variable that will hold the maximum
  582. *
  583. * Fills the @min_rate and @max_rate variables with the minimum and
  584. * maximum that clock can reach.
  585. */
  586. void clk_hw_get_rate_range(struct clk_hw *hw, unsigned long *min_rate,
  587. unsigned long *max_rate)
  588. {
  589. clk_core_get_boundaries(hw->core, min_rate, max_rate);
  590. }
  591. EXPORT_SYMBOL_GPL(clk_hw_get_rate_range);
  592. static bool clk_core_check_boundaries(struct clk_core *core,
  593. unsigned long min_rate,
  594. unsigned long max_rate)
  595. {
  596. struct clk *user;
  597. lockdep_assert_held(&prepare_lock);
  598. if (min_rate > core->max_rate || max_rate < core->min_rate)
  599. return false;
  600. hlist_for_each_entry(user, &core->clks, clks_node)
  601. if (min_rate > user->max_rate || max_rate < user->min_rate)
  602. return false;
  603. return true;
  604. }
  605. void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
  606. unsigned long max_rate)
  607. {
  608. hw->core->min_rate = min_rate;
  609. hw->core->max_rate = max_rate;
  610. }
  611. EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
  612. /*
  613. * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
  614. * @hw: mux type clk to determine rate on
  615. * @req: rate request, also used to return preferred parent and frequencies
  616. *
  617. * Helper for finding best parent to provide a given frequency. This can be used
  618. * directly as a determine_rate callback (e.g. for a mux), or from a more
  619. * complex clock that may combine a mux with other operations.
  620. *
  621. * Returns: 0 on success, -EERROR value on error
  622. */
  623. int __clk_mux_determine_rate(struct clk_hw *hw,
  624. struct clk_rate_request *req)
  625. {
  626. return clk_mux_determine_rate_flags(hw, req, 0);
  627. }
  628. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
  629. int __clk_mux_determine_rate_closest(struct clk_hw *hw,
  630. struct clk_rate_request *req)
  631. {
  632. return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
  633. }
  634. EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
  635. /*** clk api ***/
  636. static void clk_core_rate_unprotect(struct clk_core *core)
  637. {
  638. lockdep_assert_held(&prepare_lock);
  639. if (!core)
  640. return;
  641. if (WARN(core->protect_count == 0,
  642. "%s already unprotected\n", core->name))
  643. return;
  644. if (--core->protect_count > 0)
  645. return;
  646. clk_core_rate_unprotect(core->parent);
  647. }
  648. static int clk_core_rate_nuke_protect(struct clk_core *core)
  649. {
  650. int ret;
  651. lockdep_assert_held(&prepare_lock);
  652. if (!core)
  653. return -EINVAL;
  654. if (core->protect_count == 0)
  655. return 0;
  656. ret = core->protect_count;
  657. core->protect_count = 1;
  658. clk_core_rate_unprotect(core);
  659. return ret;
  660. }
  661. /**
  662. * clk_rate_exclusive_put - release exclusivity over clock rate control
  663. * @clk: the clk over which the exclusivity is released
  664. *
  665. * clk_rate_exclusive_put() completes a critical section during which a clock
  666. * consumer cannot tolerate any other consumer making any operation on the
  667. * clock which could result in a rate change or rate glitch. Exclusive clocks
  668. * cannot have their rate changed, either directly or indirectly due to changes
  669. * further up the parent chain of clocks. As a result, clocks up parent chain
  670. * also get under exclusive control of the calling consumer.
  671. *
  672. * If exlusivity is claimed more than once on clock, even by the same consumer,
  673. * the rate effectively gets locked as exclusivity can't be preempted.
  674. *
  675. * Calls to clk_rate_exclusive_put() must be balanced with calls to
  676. * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
  677. * error status.
  678. */
  679. void clk_rate_exclusive_put(struct clk *clk)
  680. {
  681. if (!clk)
  682. return;
  683. clk_prepare_lock();
  684. /*
  685. * if there is something wrong with this consumer protect count, stop
  686. * here before messing with the provider
  687. */
  688. if (WARN_ON(clk->exclusive_count <= 0))
  689. goto out;
  690. clk_core_rate_unprotect(clk->core);
  691. clk->exclusive_count--;
  692. out:
  693. clk_prepare_unlock();
  694. }
  695. EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
  696. static void clk_core_rate_protect(struct clk_core *core)
  697. {
  698. lockdep_assert_held(&prepare_lock);
  699. if (!core)
  700. return;
  701. if (core->protect_count == 0)
  702. clk_core_rate_protect(core->parent);
  703. core->protect_count++;
  704. }
  705. static void clk_core_rate_restore_protect(struct clk_core *core, int count)
  706. {
  707. lockdep_assert_held(&prepare_lock);
  708. if (!core)
  709. return;
  710. if (count == 0)
  711. return;
  712. clk_core_rate_protect(core);
  713. core->protect_count = count;
  714. }
  715. /**
  716. * clk_rate_exclusive_get - get exclusivity over the clk rate control
  717. * @clk: the clk over which the exclusity of rate control is requested
  718. *
  719. * clk_rate_exclusive_get() begins a critical section during which a clock
  720. * consumer cannot tolerate any other consumer making any operation on the
  721. * clock which could result in a rate change or rate glitch. Exclusive clocks
  722. * cannot have their rate changed, either directly or indirectly due to changes
  723. * further up the parent chain of clocks. As a result, clocks up parent chain
  724. * also get under exclusive control of the calling consumer.
  725. *
  726. * If exlusivity is claimed more than once on clock, even by the same consumer,
  727. * the rate effectively gets locked as exclusivity can't be preempted.
  728. *
  729. * Calls to clk_rate_exclusive_get() should be balanced with calls to
  730. * clk_rate_exclusive_put(). Calls to this function may sleep.
  731. * Returns 0 on success, -EERROR otherwise
  732. */
  733. int clk_rate_exclusive_get(struct clk *clk)
  734. {
  735. if (!clk)
  736. return 0;
  737. clk_prepare_lock();
  738. clk_core_rate_protect(clk->core);
  739. clk->exclusive_count++;
  740. clk_prepare_unlock();
  741. return 0;
  742. }
  743. EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
  744. static void clk_core_unprepare(struct clk_core *core)
  745. {
  746. lockdep_assert_held(&prepare_lock);
  747. if (!core)
  748. return;
  749. if (WARN(core->prepare_count == 0,
  750. "%s already unprepared\n", core->name))
  751. return;
  752. if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
  753. "Unpreparing critical %s\n", core->name))
  754. return;
  755. if (core->flags & CLK_SET_RATE_GATE)
  756. clk_core_rate_unprotect(core);
  757. if (--core->prepare_count > 0)
  758. return;
  759. WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
  760. trace_clk_unprepare(core);
  761. if (core->ops->unprepare)
  762. core->ops->unprepare(core->hw);
  763. trace_clk_unprepare_complete(core);
  764. clk_core_unprepare(core->parent);
  765. clk_pm_runtime_put(core);
  766. }
  767. static void clk_core_unprepare_lock(struct clk_core *core)
  768. {
  769. clk_prepare_lock();
  770. clk_core_unprepare(core);
  771. clk_prepare_unlock();
  772. }
  773. /**
  774. * clk_unprepare - undo preparation of a clock source
  775. * @clk: the clk being unprepared
  776. *
  777. * clk_unprepare may sleep, which differentiates it from clk_disable. In a
  778. * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
  779. * if the operation may sleep. One example is a clk which is accessed over
  780. * I2c. In the complex case a clk gate operation may require a fast and a slow
  781. * part. It is this reason that clk_unprepare and clk_disable are not mutually
  782. * exclusive. In fact clk_disable must be called before clk_unprepare.
  783. */
  784. void clk_unprepare(struct clk *clk)
  785. {
  786. if (IS_ERR_OR_NULL(clk))
  787. return;
  788. clk_core_unprepare_lock(clk->core);
  789. }
  790. EXPORT_SYMBOL_GPL(clk_unprepare);
  791. static int clk_core_prepare(struct clk_core *core)
  792. {
  793. int ret = 0;
  794. lockdep_assert_held(&prepare_lock);
  795. if (!core)
  796. return 0;
  797. if (core->prepare_count == 0) {
  798. ret = clk_pm_runtime_get(core);
  799. if (ret)
  800. return ret;
  801. ret = clk_core_prepare(core->parent);
  802. if (ret)
  803. goto runtime_put;
  804. trace_clk_prepare(core);
  805. if (core->ops->prepare)
  806. ret = core->ops->prepare(core->hw);
  807. trace_clk_prepare_complete(core);
  808. if (ret)
  809. goto unprepare;
  810. }
  811. core->prepare_count++;
  812. /*
  813. * CLK_SET_RATE_GATE is a special case of clock protection
  814. * Instead of a consumer claiming exclusive rate control, it is
  815. * actually the provider which prevents any consumer from making any
  816. * operation which could result in a rate change or rate glitch while
  817. * the clock is prepared.
  818. */
  819. if (core->flags & CLK_SET_RATE_GATE)
  820. clk_core_rate_protect(core);
  821. return 0;
  822. unprepare:
  823. clk_core_unprepare(core->parent);
  824. runtime_put:
  825. clk_pm_runtime_put(core);
  826. return ret;
  827. }
  828. static int clk_core_prepare_lock(struct clk_core *core)
  829. {
  830. int ret;
  831. clk_prepare_lock();
  832. ret = clk_core_prepare(core);
  833. clk_prepare_unlock();
  834. return ret;
  835. }
  836. /**
  837. * clk_prepare - prepare a clock source
  838. * @clk: the clk being prepared
  839. *
  840. * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
  841. * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
  842. * operation may sleep. One example is a clk which is accessed over I2c. In
  843. * the complex case a clk ungate operation may require a fast and a slow part.
  844. * It is this reason that clk_prepare and clk_enable are not mutually
  845. * exclusive. In fact clk_prepare must be called before clk_enable.
  846. * Returns 0 on success, -EERROR otherwise.
  847. */
  848. int clk_prepare(struct clk *clk)
  849. {
  850. if (!clk)
  851. return 0;
  852. return clk_core_prepare_lock(clk->core);
  853. }
  854. EXPORT_SYMBOL_GPL(clk_prepare);
  855. static void clk_core_disable(struct clk_core *core)
  856. {
  857. lockdep_assert_held(&enable_lock);
  858. if (!core)
  859. return;
  860. if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
  861. return;
  862. if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
  863. "Disabling critical %s\n", core->name))
  864. return;
  865. if (--core->enable_count > 0)
  866. return;
  867. trace_clk_disable_rcuidle(core);
  868. if (core->ops->disable)
  869. core->ops->disable(core->hw);
  870. trace_clk_disable_complete_rcuidle(core);
  871. clk_core_disable(core->parent);
  872. }
  873. static void clk_core_disable_lock(struct clk_core *core)
  874. {
  875. unsigned long flags;
  876. flags = clk_enable_lock();
  877. clk_core_disable(core);
  878. clk_enable_unlock(flags);
  879. }
  880. /**
  881. * clk_disable - gate a clock
  882. * @clk: the clk being gated
  883. *
  884. * clk_disable must not sleep, which differentiates it from clk_unprepare. In
  885. * a simple case, clk_disable can be used instead of clk_unprepare to gate a
  886. * clk if the operation is fast and will never sleep. One example is a
  887. * SoC-internal clk which is controlled via simple register writes. In the
  888. * complex case a clk gate operation may require a fast and a slow part. It is
  889. * this reason that clk_unprepare and clk_disable are not mutually exclusive.
  890. * In fact clk_disable must be called before clk_unprepare.
  891. */
  892. void clk_disable(struct clk *clk)
  893. {
  894. if (IS_ERR_OR_NULL(clk))
  895. return;
  896. clk_core_disable_lock(clk->core);
  897. }
  898. EXPORT_SYMBOL_GPL(clk_disable);
  899. static int clk_core_enable(struct clk_core *core)
  900. {
  901. int ret = 0;
  902. lockdep_assert_held(&enable_lock);
  903. if (!core)
  904. return 0;
  905. if (WARN(core->prepare_count == 0,
  906. "Enabling unprepared %s\n", core->name))
  907. return -ESHUTDOWN;
  908. if (core->enable_count == 0) {
  909. ret = clk_core_enable(core->parent);
  910. if (ret)
  911. return ret;
  912. trace_clk_enable_rcuidle(core);
  913. if (core->ops->enable)
  914. ret = core->ops->enable(core->hw);
  915. trace_clk_enable_complete_rcuidle(core);
  916. if (ret) {
  917. clk_core_disable(core->parent);
  918. return ret;
  919. }
  920. }
  921. core->enable_count++;
  922. return 0;
  923. }
  924. static int clk_core_enable_lock(struct clk_core *core)
  925. {
  926. unsigned long flags;
  927. int ret;
  928. flags = clk_enable_lock();
  929. ret = clk_core_enable(core);
  930. clk_enable_unlock(flags);
  931. return ret;
  932. }
  933. /**
  934. * clk_gate_restore_context - restore context for poweroff
  935. * @hw: the clk_hw pointer of clock whose state is to be restored
  936. *
  937. * The clock gate restore context function enables or disables
  938. * the gate clocks based on the enable_count. This is done in cases
  939. * where the clock context is lost and based on the enable_count
  940. * the clock either needs to be enabled/disabled. This
  941. * helps restore the state of gate clocks.
  942. */
  943. void clk_gate_restore_context(struct clk_hw *hw)
  944. {
  945. struct clk_core *core = hw->core;
  946. if (core->enable_count)
  947. core->ops->enable(hw);
  948. else
  949. core->ops->disable(hw);
  950. }
  951. EXPORT_SYMBOL_GPL(clk_gate_restore_context);
  952. static int clk_core_save_context(struct clk_core *core)
  953. {
  954. struct clk_core *child;
  955. int ret = 0;
  956. hlist_for_each_entry(child, &core->children, child_node) {
  957. ret = clk_core_save_context(child);
  958. if (ret < 0)
  959. return ret;
  960. }
  961. if (core->ops && core->ops->save_context)
  962. ret = core->ops->save_context(core->hw);
  963. return ret;
  964. }
  965. static void clk_core_restore_context(struct clk_core *core)
  966. {
  967. struct clk_core *child;
  968. if (core->ops && core->ops->restore_context)
  969. core->ops->restore_context(core->hw);
  970. hlist_for_each_entry(child, &core->children, child_node)
  971. clk_core_restore_context(child);
  972. }
  973. /**
  974. * clk_save_context - save clock context for poweroff
  975. *
  976. * Saves the context of the clock register for powerstates in which the
  977. * contents of the registers will be lost. Occurs deep within the suspend
  978. * code. Returns 0 on success.
  979. */
  980. int clk_save_context(void)
  981. {
  982. struct clk_core *clk;
  983. int ret;
  984. hlist_for_each_entry(clk, &clk_root_list, child_node) {
  985. ret = clk_core_save_context(clk);
  986. if (ret < 0)
  987. return ret;
  988. }
  989. hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
  990. ret = clk_core_save_context(clk);
  991. if (ret < 0)
  992. return ret;
  993. }
  994. return 0;
  995. }
  996. EXPORT_SYMBOL_GPL(clk_save_context);
  997. /**
  998. * clk_restore_context - restore clock context after poweroff
  999. *
  1000. * Restore the saved clock context upon resume.
  1001. *
  1002. */
  1003. void clk_restore_context(void)
  1004. {
  1005. struct clk_core *core;
  1006. hlist_for_each_entry(core, &clk_root_list, child_node)
  1007. clk_core_restore_context(core);
  1008. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  1009. clk_core_restore_context(core);
  1010. }
  1011. EXPORT_SYMBOL_GPL(clk_restore_context);
  1012. /**
  1013. * clk_enable - ungate a clock
  1014. * @clk: the clk being ungated
  1015. *
  1016. * clk_enable must not sleep, which differentiates it from clk_prepare. In a
  1017. * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
  1018. * if the operation will never sleep. One example is a SoC-internal clk which
  1019. * is controlled via simple register writes. In the complex case a clk ungate
  1020. * operation may require a fast and a slow part. It is this reason that
  1021. * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
  1022. * must be called before clk_enable. Returns 0 on success, -EERROR
  1023. * otherwise.
  1024. */
  1025. int clk_enable(struct clk *clk)
  1026. {
  1027. if (!clk)
  1028. return 0;
  1029. return clk_core_enable_lock(clk->core);
  1030. }
  1031. EXPORT_SYMBOL_GPL(clk_enable);
  1032. /**
  1033. * clk_is_enabled_when_prepared - indicate if preparing a clock also enables it.
  1034. * @clk: clock source
  1035. *
  1036. * Returns true if clk_prepare() implicitly enables the clock, effectively
  1037. * making clk_enable()/clk_disable() no-ops, false otherwise.
  1038. *
  1039. * This is of interest mainly to power management code where actually
  1040. * disabling the clock also requires unpreparing it to have any material
  1041. * effect.
  1042. *
  1043. * Regardless of the value returned here, the caller must always invoke
  1044. * clk_enable() or clk_prepare_enable() and counterparts for usage counts
  1045. * to be right.
  1046. */
  1047. bool clk_is_enabled_when_prepared(struct clk *clk)
  1048. {
  1049. return clk && !(clk->core->ops->enable && clk->core->ops->disable);
  1050. }
  1051. EXPORT_SYMBOL_GPL(clk_is_enabled_when_prepared);
  1052. static int clk_core_prepare_enable(struct clk_core *core)
  1053. {
  1054. int ret;
  1055. ret = clk_core_prepare_lock(core);
  1056. if (ret)
  1057. return ret;
  1058. ret = clk_core_enable_lock(core);
  1059. if (ret)
  1060. clk_core_unprepare_lock(core);
  1061. return ret;
  1062. }
  1063. static void clk_core_disable_unprepare(struct clk_core *core)
  1064. {
  1065. clk_core_disable_lock(core);
  1066. clk_core_unprepare_lock(core);
  1067. }
  1068. static void __init clk_unprepare_unused_subtree(struct clk_core *core)
  1069. {
  1070. struct clk_core *child;
  1071. lockdep_assert_held(&prepare_lock);
  1072. hlist_for_each_entry(child, &core->children, child_node)
  1073. clk_unprepare_unused_subtree(child);
  1074. if (dev_has_sync_state(core->dev) &&
  1075. !(core->flags & CLK_DONT_HOLD_STATE))
  1076. return;
  1077. if (core->prepare_count)
  1078. return;
  1079. if (core->flags & CLK_IGNORE_UNUSED)
  1080. return;
  1081. if (clk_pm_runtime_get(core))
  1082. return;
  1083. if (clk_core_is_prepared(core)) {
  1084. trace_clk_unprepare(core);
  1085. if (core->ops->unprepare_unused)
  1086. core->ops->unprepare_unused(core->hw);
  1087. else if (core->ops->unprepare)
  1088. core->ops->unprepare(core->hw);
  1089. trace_clk_unprepare_complete(core);
  1090. }
  1091. clk_pm_runtime_put(core);
  1092. }
  1093. static void __init clk_disable_unused_subtree(struct clk_core *core)
  1094. {
  1095. struct clk_core *child;
  1096. unsigned long flags;
  1097. lockdep_assert_held(&prepare_lock);
  1098. hlist_for_each_entry(child, &core->children, child_node)
  1099. clk_disable_unused_subtree(child);
  1100. if (dev_has_sync_state(core->dev) &&
  1101. !(core->flags & CLK_DONT_HOLD_STATE))
  1102. return;
  1103. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1104. clk_core_prepare_enable(core->parent);
  1105. if (clk_pm_runtime_get(core))
  1106. goto unprepare_out;
  1107. flags = clk_enable_lock();
  1108. if (core->enable_count)
  1109. goto unlock_out;
  1110. if (core->flags & CLK_IGNORE_UNUSED)
  1111. goto unlock_out;
  1112. /*
  1113. * some gate clocks have special needs during the disable-unused
  1114. * sequence. call .disable_unused if available, otherwise fall
  1115. * back to .disable
  1116. */
  1117. if (clk_core_is_enabled(core)) {
  1118. trace_clk_disable(core);
  1119. if (core->ops->disable_unused)
  1120. core->ops->disable_unused(core->hw);
  1121. else if (core->ops->disable)
  1122. core->ops->disable(core->hw);
  1123. trace_clk_disable_complete(core);
  1124. }
  1125. unlock_out:
  1126. clk_enable_unlock(flags);
  1127. clk_pm_runtime_put(core);
  1128. unprepare_out:
  1129. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1130. clk_core_disable_unprepare(core->parent);
  1131. }
  1132. static bool clk_ignore_unused __initdata;
  1133. static int __init clk_ignore_unused_setup(char *__unused)
  1134. {
  1135. clk_ignore_unused = true;
  1136. return 1;
  1137. }
  1138. __setup("clk_ignore_unused", clk_ignore_unused_setup);
  1139. static int __init clk_disable_unused(void)
  1140. {
  1141. struct clk_core *core;
  1142. if (clk_ignore_unused) {
  1143. pr_warn("clk: Not disabling unused clocks\n");
  1144. return 0;
  1145. }
  1146. clk_prepare_lock();
  1147. hlist_for_each_entry(core, &clk_root_list, child_node)
  1148. clk_disable_unused_subtree(core);
  1149. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  1150. clk_disable_unused_subtree(core);
  1151. hlist_for_each_entry(core, &clk_root_list, child_node)
  1152. clk_unprepare_unused_subtree(core);
  1153. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  1154. clk_unprepare_unused_subtree(core);
  1155. clk_prepare_unlock();
  1156. return 0;
  1157. }
  1158. late_initcall_sync(clk_disable_unused);
  1159. static void clk_unprepare_disable_dev_subtree(struct clk_core *core,
  1160. struct device *dev)
  1161. {
  1162. struct clk_core *child;
  1163. lockdep_assert_held(&prepare_lock);
  1164. hlist_for_each_entry(child, &core->children, child_node)
  1165. clk_unprepare_disable_dev_subtree(child, dev);
  1166. if (core->dev != dev || !core->need_sync)
  1167. return;
  1168. clk_core_disable_unprepare(core);
  1169. }
  1170. void clk_sync_state(struct device *dev)
  1171. {
  1172. struct clk_core *core;
  1173. clk_prepare_lock();
  1174. hlist_for_each_entry(core, &clk_root_list, child_node)
  1175. clk_unprepare_disable_dev_subtree(core, dev);
  1176. hlist_for_each_entry(core, &clk_orphan_list, child_node)
  1177. clk_unprepare_disable_dev_subtree(core, dev);
  1178. clk_prepare_unlock();
  1179. }
  1180. EXPORT_SYMBOL_GPL(clk_sync_state);
  1181. static int clk_core_determine_round_nolock(struct clk_core *core,
  1182. struct clk_rate_request *req)
  1183. {
  1184. long rate;
  1185. lockdep_assert_held(&prepare_lock);
  1186. if (!core)
  1187. return 0;
  1188. /*
  1189. * Some clock providers hand-craft their clk_rate_requests and
  1190. * might not fill min_rate and max_rate.
  1191. *
  1192. * If it's the case, clamping the rate is equivalent to setting
  1193. * the rate to 0 which is bad. Skip the clamping but complain so
  1194. * that it gets fixed, hopefully.
  1195. */
  1196. if (!req->min_rate && !req->max_rate)
  1197. pr_warn("%s: %s: clk_rate_request has initialized min or max rate.\n",
  1198. __func__, core->name);
  1199. else
  1200. req->rate = clamp(req->rate, req->min_rate, req->max_rate);
  1201. /*
  1202. * At this point, core protection will be disabled
  1203. * - if the provider is not protected at all
  1204. * - if the calling consumer is the only one which has exclusivity
  1205. * over the provider
  1206. */
  1207. if (clk_core_rate_is_protected(core)) {
  1208. req->rate = core->rate;
  1209. } else if (core->ops->determine_rate) {
  1210. return core->ops->determine_rate(core->hw, req);
  1211. } else if (core->ops->round_rate) {
  1212. rate = core->ops->round_rate(core->hw, req->rate,
  1213. &req->best_parent_rate);
  1214. if (rate < 0)
  1215. return rate;
  1216. req->rate = rate;
  1217. } else {
  1218. return -EINVAL;
  1219. }
  1220. return 0;
  1221. }
  1222. static void clk_core_init_rate_req(struct clk_core * const core,
  1223. struct clk_rate_request *req,
  1224. unsigned long rate)
  1225. {
  1226. struct clk_core *parent;
  1227. if (WARN_ON(!req))
  1228. return;
  1229. memset(req, 0, sizeof(*req));
  1230. req->max_rate = ULONG_MAX;
  1231. if (!core)
  1232. return;
  1233. req->rate = rate;
  1234. clk_core_get_boundaries(core, &req->min_rate, &req->max_rate);
  1235. parent = core->parent;
  1236. if (parent) {
  1237. req->best_parent_hw = parent->hw;
  1238. req->best_parent_rate = parent->rate;
  1239. } else {
  1240. req->best_parent_hw = NULL;
  1241. req->best_parent_rate = 0;
  1242. }
  1243. }
  1244. /**
  1245. * clk_hw_init_rate_request - Initializes a clk_rate_request
  1246. * @hw: the clk for which we want to submit a rate request
  1247. * @req: the clk_rate_request structure we want to initialise
  1248. * @rate: the rate which is to be requested
  1249. *
  1250. * Initializes a clk_rate_request structure to submit to
  1251. * __clk_determine_rate() or similar functions.
  1252. */
  1253. void clk_hw_init_rate_request(const struct clk_hw *hw,
  1254. struct clk_rate_request *req,
  1255. unsigned long rate)
  1256. {
  1257. if (WARN_ON(!hw || !req))
  1258. return;
  1259. clk_core_init_rate_req(hw->core, req, rate);
  1260. }
  1261. EXPORT_SYMBOL_GPL(clk_hw_init_rate_request);
  1262. /**
  1263. * clk_hw_forward_rate_request - Forwards a clk_rate_request to a clock's parent
  1264. * @hw: the original clock that got the rate request
  1265. * @old_req: the original clk_rate_request structure we want to forward
  1266. * @parent: the clk we want to forward @old_req to
  1267. * @req: the clk_rate_request structure we want to initialise
  1268. * @parent_rate: The rate which is to be requested to @parent
  1269. *
  1270. * Initializes a clk_rate_request structure to submit to a clock parent
  1271. * in __clk_determine_rate() or similar functions.
  1272. */
  1273. void clk_hw_forward_rate_request(const struct clk_hw *hw,
  1274. const struct clk_rate_request *old_req,
  1275. const struct clk_hw *parent,
  1276. struct clk_rate_request *req,
  1277. unsigned long parent_rate)
  1278. {
  1279. if (WARN_ON(!hw || !old_req || !parent || !req))
  1280. return;
  1281. clk_core_forward_rate_req(hw->core, old_req,
  1282. parent->core, req,
  1283. parent_rate);
  1284. }
  1285. EXPORT_SYMBOL_GPL(clk_hw_forward_rate_request);
  1286. static bool clk_core_can_round(struct clk_core * const core)
  1287. {
  1288. return core->ops->determine_rate || core->ops->round_rate;
  1289. }
  1290. static int clk_core_round_rate_nolock(struct clk_core *core,
  1291. struct clk_rate_request *req)
  1292. {
  1293. int ret;
  1294. lockdep_assert_held(&prepare_lock);
  1295. if (!core) {
  1296. req->rate = 0;
  1297. return 0;
  1298. }
  1299. if (clk_core_can_round(core))
  1300. return clk_core_determine_round_nolock(core, req);
  1301. if (core->flags & CLK_SET_RATE_PARENT) {
  1302. struct clk_rate_request parent_req;
  1303. clk_core_forward_rate_req(core, req, core->parent, &parent_req, req->rate);
  1304. ret = clk_core_round_rate_nolock(core->parent, &parent_req);
  1305. if (ret)
  1306. return ret;
  1307. req->best_parent_rate = parent_req.rate;
  1308. req->rate = parent_req.rate;
  1309. return 0;
  1310. }
  1311. req->rate = core->rate;
  1312. return 0;
  1313. }
  1314. /**
  1315. * __clk_determine_rate - get the closest rate actually supported by a clock
  1316. * @hw: determine the rate of this clock
  1317. * @req: target rate request
  1318. *
  1319. * Useful for clk_ops such as .set_rate and .determine_rate.
  1320. */
  1321. int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  1322. {
  1323. if (!hw) {
  1324. req->rate = 0;
  1325. return 0;
  1326. }
  1327. return clk_core_round_rate_nolock(hw->core, req);
  1328. }
  1329. EXPORT_SYMBOL_GPL(__clk_determine_rate);
  1330. /**
  1331. * clk_hw_round_rate() - round the given rate for a hw clk
  1332. * @hw: the hw clk for which we are rounding a rate
  1333. * @rate: the rate which is to be rounded
  1334. *
  1335. * Takes in a rate as input and rounds it to a rate that the clk can actually
  1336. * use.
  1337. *
  1338. * Context: prepare_lock must be held.
  1339. * For clk providers to call from within clk_ops such as .round_rate,
  1340. * .determine_rate.
  1341. *
  1342. * Return: returns rounded rate of hw clk if clk supports round_rate operation
  1343. * else returns the parent rate.
  1344. */
  1345. unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
  1346. {
  1347. int ret;
  1348. struct clk_rate_request req;
  1349. clk_core_init_rate_req(hw->core, &req, rate);
  1350. ret = clk_core_round_rate_nolock(hw->core, &req);
  1351. if (ret)
  1352. return 0;
  1353. return req.rate;
  1354. }
  1355. EXPORT_SYMBOL_GPL(clk_hw_round_rate);
  1356. /**
  1357. * clk_round_rate - round the given rate for a clk
  1358. * @clk: the clk for which we are rounding a rate
  1359. * @rate: the rate which is to be rounded
  1360. *
  1361. * Takes in a rate as input and rounds it to a rate that the clk can actually
  1362. * use which is then returned. If clk doesn't support round_rate operation
  1363. * then the parent rate is returned.
  1364. */
  1365. long clk_round_rate(struct clk *clk, unsigned long rate)
  1366. {
  1367. struct clk_rate_request req;
  1368. int ret;
  1369. if (!clk)
  1370. return 0;
  1371. clk_prepare_lock();
  1372. if (clk->exclusive_count)
  1373. clk_core_rate_unprotect(clk->core);
  1374. clk_core_init_rate_req(clk->core, &req, rate);
  1375. ret = clk_core_round_rate_nolock(clk->core, &req);
  1376. if (clk->exclusive_count)
  1377. clk_core_rate_protect(clk->core);
  1378. clk_prepare_unlock();
  1379. if (ret)
  1380. return ret;
  1381. return req.rate;
  1382. }
  1383. EXPORT_SYMBOL_GPL(clk_round_rate);
  1384. /**
  1385. * __clk_notify - call clk notifier chain
  1386. * @core: clk that is changing rate
  1387. * @msg: clk notifier type (see include/linux/clk.h)
  1388. * @old_rate: old clk rate
  1389. * @new_rate: new clk rate
  1390. *
  1391. * Triggers a notifier call chain on the clk rate-change notification
  1392. * for 'clk'. Passes a pointer to the struct clk and the previous
  1393. * and current rates to the notifier callback. Intended to be called by
  1394. * internal clock code only. Returns NOTIFY_DONE from the last driver
  1395. * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
  1396. * a driver returns that.
  1397. */
  1398. static int __clk_notify(struct clk_core *core, unsigned long msg,
  1399. unsigned long old_rate, unsigned long new_rate)
  1400. {
  1401. struct clk_notifier *cn;
  1402. struct clk_notifier_data cnd;
  1403. int ret = NOTIFY_DONE;
  1404. cnd.old_rate = old_rate;
  1405. cnd.new_rate = new_rate;
  1406. list_for_each_entry(cn, &clk_notifier_list, node) {
  1407. if (cn->clk->core == core) {
  1408. cnd.clk = cn->clk;
  1409. ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
  1410. &cnd);
  1411. if (ret & NOTIFY_STOP_MASK)
  1412. return ret;
  1413. }
  1414. }
  1415. return ret;
  1416. }
  1417. /**
  1418. * __clk_recalc_accuracies
  1419. * @core: first clk in the subtree
  1420. *
  1421. * Walks the subtree of clks starting with clk and recalculates accuracies as
  1422. * it goes. Note that if a clk does not implement the .recalc_accuracy
  1423. * callback then it is assumed that the clock will take on the accuracy of its
  1424. * parent.
  1425. */
  1426. static void __clk_recalc_accuracies(struct clk_core *core)
  1427. {
  1428. unsigned long parent_accuracy = 0;
  1429. struct clk_core *child;
  1430. lockdep_assert_held(&prepare_lock);
  1431. if (core->parent)
  1432. parent_accuracy = core->parent->accuracy;
  1433. if (core->ops->recalc_accuracy)
  1434. core->accuracy = core->ops->recalc_accuracy(core->hw,
  1435. parent_accuracy);
  1436. else
  1437. core->accuracy = parent_accuracy;
  1438. hlist_for_each_entry(child, &core->children, child_node)
  1439. __clk_recalc_accuracies(child);
  1440. }
  1441. static long clk_core_get_accuracy_recalc(struct clk_core *core)
  1442. {
  1443. if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
  1444. __clk_recalc_accuracies(core);
  1445. return clk_core_get_accuracy_no_lock(core);
  1446. }
  1447. /**
  1448. * clk_get_accuracy - return the accuracy of clk
  1449. * @clk: the clk whose accuracy is being returned
  1450. *
  1451. * Simply returns the cached accuracy of the clk, unless
  1452. * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
  1453. * issued.
  1454. * If clk is NULL then returns 0.
  1455. */
  1456. long clk_get_accuracy(struct clk *clk)
  1457. {
  1458. long accuracy;
  1459. if (!clk)
  1460. return 0;
  1461. clk_prepare_lock();
  1462. accuracy = clk_core_get_accuracy_recalc(clk->core);
  1463. clk_prepare_unlock();
  1464. return accuracy;
  1465. }
  1466. EXPORT_SYMBOL_GPL(clk_get_accuracy);
  1467. static unsigned long clk_recalc(struct clk_core *core,
  1468. unsigned long parent_rate)
  1469. {
  1470. unsigned long rate = parent_rate;
  1471. if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
  1472. rate = core->ops->recalc_rate(core->hw, parent_rate);
  1473. clk_pm_runtime_put(core);
  1474. }
  1475. return rate;
  1476. }
  1477. /**
  1478. * __clk_recalc_rates
  1479. * @core: first clk in the subtree
  1480. * @update_req: Whether req_rate should be updated with the new rate
  1481. * @msg: notification type (see include/linux/clk.h)
  1482. *
  1483. * Walks the subtree of clks starting with clk and recalculates rates as it
  1484. * goes. Note that if a clk does not implement the .recalc_rate callback then
  1485. * it is assumed that the clock will take on the rate of its parent.
  1486. *
  1487. * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
  1488. * if necessary.
  1489. */
  1490. static void __clk_recalc_rates(struct clk_core *core, bool update_req,
  1491. unsigned long msg)
  1492. {
  1493. unsigned long old_rate;
  1494. unsigned long parent_rate = 0;
  1495. struct clk_core *child;
  1496. lockdep_assert_held(&prepare_lock);
  1497. old_rate = core->rate;
  1498. if (core->parent)
  1499. parent_rate = core->parent->rate;
  1500. core->rate = clk_recalc(core, parent_rate);
  1501. if (update_req)
  1502. core->req_rate = core->rate;
  1503. /*
  1504. * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
  1505. * & ABORT_RATE_CHANGE notifiers
  1506. */
  1507. if (core->notifier_count && msg)
  1508. __clk_notify(core, msg, old_rate, core->rate);
  1509. hlist_for_each_entry(child, &core->children, child_node)
  1510. __clk_recalc_rates(child, update_req, msg);
  1511. }
  1512. static unsigned long clk_core_get_rate_recalc(struct clk_core *core)
  1513. {
  1514. if (core && (core->flags & CLK_GET_RATE_NOCACHE))
  1515. __clk_recalc_rates(core, false, 0);
  1516. return clk_core_get_rate_nolock(core);
  1517. }
  1518. /**
  1519. * clk_get_rate - return the rate of clk
  1520. * @clk: the clk whose rate is being returned
  1521. *
  1522. * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
  1523. * is set, which means a recalc_rate will be issued. Can be called regardless of
  1524. * the clock enabledness. If clk is NULL, or if an error occurred, then returns
  1525. * 0.
  1526. */
  1527. unsigned long clk_get_rate(struct clk *clk)
  1528. {
  1529. unsigned long rate;
  1530. if (!clk)
  1531. return 0;
  1532. clk_prepare_lock();
  1533. rate = clk_core_get_rate_recalc(clk->core);
  1534. clk_prepare_unlock();
  1535. return rate;
  1536. }
  1537. EXPORT_SYMBOL_GPL(clk_get_rate);
  1538. static int clk_fetch_parent_index(struct clk_core *core,
  1539. struct clk_core *parent)
  1540. {
  1541. int i;
  1542. if (!parent)
  1543. return -EINVAL;
  1544. for (i = 0; i < core->num_parents; i++) {
  1545. /* Found it first try! */
  1546. if (core->parents[i].core == parent)
  1547. return i;
  1548. /* Something else is here, so keep looking */
  1549. if (core->parents[i].core)
  1550. continue;
  1551. /* Maybe core hasn't been cached but the hw is all we know? */
  1552. if (core->parents[i].hw) {
  1553. if (core->parents[i].hw == parent->hw)
  1554. break;
  1555. /* Didn't match, but we're expecting a clk_hw */
  1556. continue;
  1557. }
  1558. /* Maybe it hasn't been cached (clk_set_parent() path) */
  1559. if (parent == clk_core_get(core, i))
  1560. break;
  1561. /* Fallback to comparing globally unique names */
  1562. if (core->parents[i].name &&
  1563. !strcmp(parent->name, core->parents[i].name))
  1564. break;
  1565. }
  1566. if (i == core->num_parents)
  1567. return -EINVAL;
  1568. core->parents[i].core = parent;
  1569. return i;
  1570. }
  1571. /**
  1572. * clk_hw_get_parent_index - return the index of the parent clock
  1573. * @hw: clk_hw associated with the clk being consumed
  1574. *
  1575. * Fetches and returns the index of parent clock. Returns -EINVAL if the given
  1576. * clock does not have a current parent.
  1577. */
  1578. int clk_hw_get_parent_index(struct clk_hw *hw)
  1579. {
  1580. struct clk_hw *parent = clk_hw_get_parent(hw);
  1581. if (WARN_ON(parent == NULL))
  1582. return -EINVAL;
  1583. return clk_fetch_parent_index(hw->core, parent->core);
  1584. }
  1585. EXPORT_SYMBOL_GPL(clk_hw_get_parent_index);
  1586. static void clk_core_hold_state(struct clk_core *core)
  1587. {
  1588. if (core->need_sync || !core->boot_enabled)
  1589. return;
  1590. if (core->orphan || !dev_has_sync_state(core->dev))
  1591. return;
  1592. if (core->flags & CLK_DONT_HOLD_STATE)
  1593. return;
  1594. core->need_sync = !clk_core_prepare_enable(core);
  1595. }
  1596. static void __clk_core_update_orphan_hold_state(struct clk_core *core)
  1597. {
  1598. struct clk_core *child;
  1599. if (core->orphan)
  1600. return;
  1601. clk_core_hold_state(core);
  1602. hlist_for_each_entry(child, &core->children, child_node)
  1603. __clk_core_update_orphan_hold_state(child);
  1604. }
  1605. /*
  1606. * Update the orphan status of @core and all its children.
  1607. */
  1608. static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
  1609. {
  1610. struct clk_core *child;
  1611. core->orphan = is_orphan;
  1612. hlist_for_each_entry(child, &core->children, child_node)
  1613. clk_core_update_orphan_status(child, is_orphan);
  1614. }
  1615. static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
  1616. {
  1617. bool was_orphan = core->orphan;
  1618. hlist_del(&core->child_node);
  1619. if (new_parent) {
  1620. bool becomes_orphan = new_parent->orphan;
  1621. /* avoid duplicate POST_RATE_CHANGE notifications */
  1622. if (new_parent->new_child == core)
  1623. new_parent->new_child = NULL;
  1624. hlist_add_head(&core->child_node, &new_parent->children);
  1625. if (was_orphan != becomes_orphan)
  1626. clk_core_update_orphan_status(core, becomes_orphan);
  1627. } else {
  1628. hlist_add_head(&core->child_node, &clk_orphan_list);
  1629. if (!was_orphan)
  1630. clk_core_update_orphan_status(core, true);
  1631. }
  1632. core->parent = new_parent;
  1633. }
  1634. static struct clk_core *__clk_set_parent_before(struct clk_core *core,
  1635. struct clk_core *parent)
  1636. {
  1637. unsigned long flags;
  1638. struct clk_core *old_parent = core->parent;
  1639. /*
  1640. * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
  1641. *
  1642. * 2. Migrate prepare state between parents and prevent race with
  1643. * clk_enable().
  1644. *
  1645. * If the clock is not prepared, then a race with
  1646. * clk_enable/disable() is impossible since we already have the
  1647. * prepare lock (future calls to clk_enable() need to be preceded by
  1648. * a clk_prepare()).
  1649. *
  1650. * If the clock is prepared, migrate the prepared state to the new
  1651. * parent and also protect against a race with clk_enable() by
  1652. * forcing the clock and the new parent on. This ensures that all
  1653. * future calls to clk_enable() are practically NOPs with respect to
  1654. * hardware and software states.
  1655. *
  1656. * See also: Comment for clk_set_parent() below.
  1657. */
  1658. /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
  1659. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  1660. clk_core_prepare_enable(old_parent);
  1661. clk_core_prepare_enable(parent);
  1662. }
  1663. /* migrate prepare count if > 0 */
  1664. if (core->prepare_count) {
  1665. clk_core_prepare_enable(parent);
  1666. clk_core_enable_lock(core);
  1667. }
  1668. /* update the clk tree topology */
  1669. flags = clk_enable_lock();
  1670. clk_reparent(core, parent);
  1671. clk_enable_unlock(flags);
  1672. return old_parent;
  1673. }
  1674. static void __clk_set_parent_after(struct clk_core *core,
  1675. struct clk_core *parent,
  1676. struct clk_core *old_parent)
  1677. {
  1678. /*
  1679. * Finish the migration of prepare state and undo the changes done
  1680. * for preventing a race with clk_enable().
  1681. */
  1682. if (core->prepare_count) {
  1683. clk_core_disable_lock(core);
  1684. clk_core_disable_unprepare(old_parent);
  1685. }
  1686. /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
  1687. if (core->flags & CLK_OPS_PARENT_ENABLE) {
  1688. clk_core_disable_unprepare(parent);
  1689. clk_core_disable_unprepare(old_parent);
  1690. }
  1691. }
  1692. static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
  1693. u8 p_index)
  1694. {
  1695. unsigned long flags;
  1696. int ret = 0;
  1697. struct clk_core *old_parent;
  1698. old_parent = __clk_set_parent_before(core, parent);
  1699. trace_clk_set_parent(core, parent);
  1700. /* change clock input source */
  1701. if (parent && core->ops->set_parent)
  1702. ret = core->ops->set_parent(core->hw, p_index);
  1703. trace_clk_set_parent_complete(core, parent);
  1704. if (ret) {
  1705. flags = clk_enable_lock();
  1706. clk_reparent(core, old_parent);
  1707. clk_enable_unlock(flags);
  1708. __clk_set_parent_after(core, old_parent, parent);
  1709. return ret;
  1710. }
  1711. __clk_set_parent_after(core, parent, old_parent);
  1712. return 0;
  1713. }
  1714. /**
  1715. * __clk_speculate_rates
  1716. * @core: first clk in the subtree
  1717. * @parent_rate: the "future" rate of clk's parent
  1718. *
  1719. * Walks the subtree of clks starting with clk, speculating rates as it
  1720. * goes and firing off PRE_RATE_CHANGE notifications as necessary.
  1721. *
  1722. * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
  1723. * pre-rate change notifications and returns early if no clks in the
  1724. * subtree have subscribed to the notifications. Note that if a clk does not
  1725. * implement the .recalc_rate callback then it is assumed that the clock will
  1726. * take on the rate of its parent.
  1727. */
  1728. static int __clk_speculate_rates(struct clk_core *core,
  1729. unsigned long parent_rate)
  1730. {
  1731. struct clk_core *child;
  1732. unsigned long new_rate;
  1733. int ret = NOTIFY_DONE;
  1734. lockdep_assert_held(&prepare_lock);
  1735. new_rate = clk_recalc(core, parent_rate);
  1736. /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
  1737. if (core->notifier_count)
  1738. ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
  1739. if (ret & NOTIFY_STOP_MASK) {
  1740. pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
  1741. __func__, core->name, ret);
  1742. goto out;
  1743. }
  1744. hlist_for_each_entry(child, &core->children, child_node) {
  1745. ret = __clk_speculate_rates(child, new_rate);
  1746. if (ret & NOTIFY_STOP_MASK)
  1747. break;
  1748. }
  1749. out:
  1750. return ret;
  1751. }
  1752. static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
  1753. struct clk_core *new_parent, u8 p_index)
  1754. {
  1755. struct clk_core *child;
  1756. core->new_rate = new_rate;
  1757. core->new_parent = new_parent;
  1758. core->new_parent_index = p_index;
  1759. /* include clk in new parent's PRE_RATE_CHANGE notifications */
  1760. core->new_child = NULL;
  1761. if (new_parent && new_parent != core->parent)
  1762. new_parent->new_child = core;
  1763. hlist_for_each_entry(child, &core->children, child_node) {
  1764. child->new_rate = clk_recalc(child, new_rate);
  1765. clk_calc_subtree(child, child->new_rate, NULL, 0);
  1766. }
  1767. }
  1768. /*
  1769. * calculate the new rates returning the topmost clock that has to be
  1770. * changed.
  1771. */
  1772. static struct clk_core *clk_calc_new_rates(struct clk_core *core,
  1773. unsigned long rate)
  1774. {
  1775. struct clk_core *top = core;
  1776. struct clk_core *old_parent, *parent;
  1777. unsigned long best_parent_rate = 0;
  1778. unsigned long new_rate;
  1779. unsigned long min_rate;
  1780. unsigned long max_rate;
  1781. int p_index = 0;
  1782. long ret;
  1783. /* sanity */
  1784. if (IS_ERR_OR_NULL(core))
  1785. return NULL;
  1786. /* save parent rate, if it exists */
  1787. parent = old_parent = core->parent;
  1788. if (parent)
  1789. best_parent_rate = parent->rate;
  1790. clk_core_get_boundaries(core, &min_rate, &max_rate);
  1791. /* find the closest rate and parent clk/rate */
  1792. if (clk_core_can_round(core)) {
  1793. struct clk_rate_request req;
  1794. clk_core_init_rate_req(core, &req, rate);
  1795. ret = clk_core_determine_round_nolock(core, &req);
  1796. if (ret < 0)
  1797. return NULL;
  1798. best_parent_rate = req.best_parent_rate;
  1799. new_rate = req.rate;
  1800. parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
  1801. if (new_rate < min_rate || new_rate > max_rate)
  1802. return NULL;
  1803. } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
  1804. /* pass-through clock without adjustable parent */
  1805. core->new_rate = core->rate;
  1806. return NULL;
  1807. } else {
  1808. /* pass-through clock with adjustable parent */
  1809. top = clk_calc_new_rates(parent, rate);
  1810. new_rate = parent->new_rate;
  1811. goto out;
  1812. }
  1813. /* some clocks must be gated to change parent */
  1814. if (parent != old_parent &&
  1815. (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
  1816. pr_debug("%s: %s not gated but wants to reparent\n",
  1817. __func__, core->name);
  1818. return NULL;
  1819. }
  1820. /* try finding the new parent index */
  1821. if (parent && core->num_parents > 1) {
  1822. p_index = clk_fetch_parent_index(core, parent);
  1823. if (p_index < 0) {
  1824. pr_debug("%s: clk %s can not be parent of clk %s\n",
  1825. __func__, parent->name, core->name);
  1826. return NULL;
  1827. }
  1828. }
  1829. if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
  1830. best_parent_rate != parent->rate)
  1831. top = clk_calc_new_rates(parent, best_parent_rate);
  1832. out:
  1833. clk_calc_subtree(core, new_rate, parent, p_index);
  1834. return top;
  1835. }
  1836. /*
  1837. * Notify about rate changes in a subtree. Always walk down the whole tree
  1838. * so that in case of an error we can walk down the whole tree again and
  1839. * abort the change.
  1840. */
  1841. static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
  1842. unsigned long event)
  1843. {
  1844. struct clk_core *child, *tmp_clk, *fail_clk = NULL;
  1845. int ret = NOTIFY_DONE;
  1846. if (core->rate == core->new_rate)
  1847. return NULL;
  1848. if (core->notifier_count) {
  1849. ret = __clk_notify(core, event, core->rate, core->new_rate);
  1850. if (ret & NOTIFY_STOP_MASK)
  1851. fail_clk = core;
  1852. }
  1853. if (core->ops->pre_rate_change) {
  1854. ret = core->ops->pre_rate_change(core->hw, core->rate,
  1855. core->new_rate);
  1856. if (ret)
  1857. fail_clk = core;
  1858. }
  1859. hlist_for_each_entry(child, &core->children, child_node) {
  1860. /* Skip children who will be reparented to another clock */
  1861. if (child->new_parent && child->new_parent != core)
  1862. continue;
  1863. tmp_clk = clk_propagate_rate_change(child, event);
  1864. if (tmp_clk)
  1865. fail_clk = tmp_clk;
  1866. }
  1867. /* handle the new child who might not be in core->children yet */
  1868. if (core->new_child) {
  1869. tmp_clk = clk_propagate_rate_change(core->new_child, event);
  1870. if (tmp_clk)
  1871. fail_clk = tmp_clk;
  1872. }
  1873. return fail_clk;
  1874. }
  1875. /*
  1876. * walk down a subtree and set the new rates notifying the rate
  1877. * change on the way
  1878. */
  1879. static void clk_change_rate(struct clk_core *core)
  1880. {
  1881. struct clk_core *child;
  1882. struct hlist_node *tmp;
  1883. unsigned long old_rate;
  1884. unsigned long best_parent_rate = 0;
  1885. bool skip_set_rate = false;
  1886. struct clk_core *old_parent;
  1887. struct clk_core *parent = NULL;
  1888. old_rate = core->rate;
  1889. if (core->new_parent) {
  1890. parent = core->new_parent;
  1891. best_parent_rate = core->new_parent->rate;
  1892. } else if (core->parent) {
  1893. parent = core->parent;
  1894. best_parent_rate = core->parent->rate;
  1895. }
  1896. if (clk_pm_runtime_get(core))
  1897. return;
  1898. if (core->flags & CLK_SET_RATE_UNGATE) {
  1899. clk_core_prepare(core);
  1900. clk_core_enable_lock(core);
  1901. }
  1902. if (core->new_parent && core->new_parent != core->parent) {
  1903. old_parent = __clk_set_parent_before(core, core->new_parent);
  1904. trace_clk_set_parent(core, core->new_parent);
  1905. if (core->ops->set_rate_and_parent) {
  1906. skip_set_rate = true;
  1907. core->ops->set_rate_and_parent(core->hw, core->new_rate,
  1908. best_parent_rate,
  1909. core->new_parent_index);
  1910. } else if (core->ops->set_parent) {
  1911. core->ops->set_parent(core->hw, core->new_parent_index);
  1912. }
  1913. trace_clk_set_parent_complete(core, core->new_parent);
  1914. __clk_set_parent_after(core, core->new_parent, old_parent);
  1915. }
  1916. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1917. clk_core_prepare_enable(parent);
  1918. trace_clk_set_rate(core, core->new_rate);
  1919. if (!skip_set_rate && core->ops->set_rate)
  1920. core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
  1921. trace_clk_set_rate_complete(core, core->new_rate);
  1922. core->rate = clk_recalc(core, best_parent_rate);
  1923. if (core->flags & CLK_SET_RATE_UNGATE) {
  1924. clk_core_disable_lock(core);
  1925. clk_core_unprepare(core);
  1926. }
  1927. if (core->flags & CLK_OPS_PARENT_ENABLE)
  1928. clk_core_disable_unprepare(parent);
  1929. if (core->notifier_count && old_rate != core->rate)
  1930. __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
  1931. if (core->flags & CLK_RECALC_NEW_RATES)
  1932. (void)clk_calc_new_rates(core, core->new_rate);
  1933. if (core->ops->post_rate_change)
  1934. core->ops->post_rate_change(core->hw, old_rate, core->rate);
  1935. /*
  1936. * Use safe iteration, as change_rate can actually swap parents
  1937. * for certain clock types.
  1938. */
  1939. hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
  1940. /* Skip children who will be reparented to another clock */
  1941. if (child->new_parent && child->new_parent != core)
  1942. continue;
  1943. clk_change_rate(child);
  1944. }
  1945. /* handle the new child who might not be in core->children yet */
  1946. if (core->new_child)
  1947. clk_change_rate(core->new_child);
  1948. clk_pm_runtime_put(core);
  1949. }
  1950. static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
  1951. unsigned long req_rate)
  1952. {
  1953. int ret, cnt;
  1954. struct clk_rate_request req;
  1955. lockdep_assert_held(&prepare_lock);
  1956. if (!core)
  1957. return 0;
  1958. /* simulate what the rate would be if it could be freely set */
  1959. cnt = clk_core_rate_nuke_protect(core);
  1960. if (cnt < 0)
  1961. return cnt;
  1962. clk_core_init_rate_req(core, &req, req_rate);
  1963. ret = clk_core_round_rate_nolock(core, &req);
  1964. /* restore the protection */
  1965. clk_core_rate_restore_protect(core, cnt);
  1966. return ret ? 0 : req.rate;
  1967. }
  1968. static int clk_core_set_rate_nolock(struct clk_core *core,
  1969. unsigned long req_rate)
  1970. {
  1971. struct clk_core *top, *fail_clk;
  1972. unsigned long rate;
  1973. int ret;
  1974. if (!core)
  1975. return 0;
  1976. rate = clk_core_req_round_rate_nolock(core, req_rate);
  1977. /* bail early if nothing to do */
  1978. if (rate == clk_core_get_rate_nolock(core))
  1979. return 0;
  1980. /* fail on a direct rate set of a protected provider */
  1981. if (clk_core_rate_is_protected(core))
  1982. return -EBUSY;
  1983. /* calculate new rates and get the topmost changed clock */
  1984. top = clk_calc_new_rates(core, req_rate);
  1985. if (!top)
  1986. return -EINVAL;
  1987. ret = clk_pm_runtime_get(core);
  1988. if (ret)
  1989. return ret;
  1990. /* notify that we are about to change rates */
  1991. fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
  1992. if (fail_clk) {
  1993. pr_debug("%s: failed to set %s rate\n", __func__,
  1994. fail_clk->name);
  1995. clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
  1996. ret = -EBUSY;
  1997. goto err;
  1998. }
  1999. /* change the rates */
  2000. clk_change_rate(top);
  2001. core->req_rate = req_rate;
  2002. err:
  2003. clk_pm_runtime_put(core);
  2004. return ret;
  2005. }
  2006. /**
  2007. * clk_set_rate - specify a new rate for clk
  2008. * @clk: the clk whose rate is being changed
  2009. * @rate: the new rate for clk
  2010. *
  2011. * In the simplest case clk_set_rate will only adjust the rate of clk.
  2012. *
  2013. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
  2014. * propagate up to clk's parent; whether or not this happens depends on the
  2015. * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
  2016. * after calling .round_rate then upstream parent propagation is ignored. If
  2017. * *parent_rate comes back with a new rate for clk's parent then we propagate
  2018. * up to clk's parent and set its rate. Upward propagation will continue
  2019. * until either a clk does not support the CLK_SET_RATE_PARENT flag or
  2020. * .round_rate stops requesting changes to clk's parent_rate.
  2021. *
  2022. * Rate changes are accomplished via tree traversal that also recalculates the
  2023. * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
  2024. *
  2025. * Returns 0 on success, -EERROR otherwise.
  2026. */
  2027. int clk_set_rate(struct clk *clk, unsigned long rate)
  2028. {
  2029. int ret;
  2030. if (!clk)
  2031. return 0;
  2032. /* prevent racing with updates to the clock topology */
  2033. clk_prepare_lock();
  2034. if (clk->exclusive_count)
  2035. clk_core_rate_unprotect(clk->core);
  2036. ret = clk_core_set_rate_nolock(clk->core, rate);
  2037. if (clk->exclusive_count)
  2038. clk_core_rate_protect(clk->core);
  2039. clk_prepare_unlock();
  2040. return ret;
  2041. }
  2042. EXPORT_SYMBOL_GPL(clk_set_rate);
  2043. /**
  2044. * clk_set_rate_exclusive - specify a new rate and get exclusive control
  2045. * @clk: the clk whose rate is being changed
  2046. * @rate: the new rate for clk
  2047. *
  2048. * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
  2049. * within a critical section
  2050. *
  2051. * This can be used initially to ensure that at least 1 consumer is
  2052. * satisfied when several consumers are competing for exclusivity over the
  2053. * same clock provider.
  2054. *
  2055. * The exclusivity is not applied if setting the rate failed.
  2056. *
  2057. * Calls to clk_rate_exclusive_get() should be balanced with calls to
  2058. * clk_rate_exclusive_put().
  2059. *
  2060. * Returns 0 on success, -EERROR otherwise.
  2061. */
  2062. int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
  2063. {
  2064. int ret;
  2065. if (!clk)
  2066. return 0;
  2067. /* prevent racing with updates to the clock topology */
  2068. clk_prepare_lock();
  2069. /*
  2070. * The temporary protection removal is not here, on purpose
  2071. * This function is meant to be used instead of clk_rate_protect,
  2072. * so before the consumer code path protect the clock provider
  2073. */
  2074. ret = clk_core_set_rate_nolock(clk->core, rate);
  2075. if (!ret) {
  2076. clk_core_rate_protect(clk->core);
  2077. clk->exclusive_count++;
  2078. }
  2079. clk_prepare_unlock();
  2080. return ret;
  2081. }
  2082. EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
  2083. static int clk_set_rate_range_nolock(struct clk *clk,
  2084. unsigned long min,
  2085. unsigned long max)
  2086. {
  2087. int ret = 0;
  2088. unsigned long old_min, old_max, rate;
  2089. lockdep_assert_held(&prepare_lock);
  2090. if (!clk)
  2091. return 0;
  2092. trace_clk_set_rate_range(clk->core, min, max);
  2093. if (min > max) {
  2094. pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
  2095. __func__, clk->core->name, clk->dev_id, clk->con_id,
  2096. min, max);
  2097. return -EINVAL;
  2098. }
  2099. if (clk->exclusive_count)
  2100. clk_core_rate_unprotect(clk->core);
  2101. /* Save the current values in case we need to rollback the change */
  2102. old_min = clk->min_rate;
  2103. old_max = clk->max_rate;
  2104. clk->min_rate = min;
  2105. clk->max_rate = max;
  2106. if (!clk_core_check_boundaries(clk->core, min, max)) {
  2107. ret = -EINVAL;
  2108. goto out;
  2109. }
  2110. rate = clk->core->req_rate;
  2111. if (clk->core->flags & CLK_GET_RATE_NOCACHE)
  2112. rate = clk_core_get_rate_recalc(clk->core);
  2113. /*
  2114. * Since the boundaries have been changed, let's give the
  2115. * opportunity to the provider to adjust the clock rate based on
  2116. * the new boundaries.
  2117. *
  2118. * We also need to handle the case where the clock is currently
  2119. * outside of the boundaries. Clamping the last requested rate
  2120. * to the current minimum and maximum will also handle this.
  2121. *
  2122. * FIXME:
  2123. * There is a catch. It may fail for the usual reason (clock
  2124. * broken, clock protected, etc) but also because:
  2125. * - round_rate() was not favorable and fell on the wrong
  2126. * side of the boundary
  2127. * - the determine_rate() callback does not really check for
  2128. * this corner case when determining the rate
  2129. */
  2130. rate = clamp(rate, min, max);
  2131. ret = clk_core_set_rate_nolock(clk->core, rate);
  2132. if (ret) {
  2133. /* rollback the changes */
  2134. clk->min_rate = old_min;
  2135. clk->max_rate = old_max;
  2136. }
  2137. out:
  2138. if (clk->exclusive_count)
  2139. clk_core_rate_protect(clk->core);
  2140. return ret;
  2141. }
  2142. /**
  2143. * clk_set_rate_range - set a rate range for a clock source
  2144. * @clk: clock source
  2145. * @min: desired minimum clock rate in Hz, inclusive
  2146. * @max: desired maximum clock rate in Hz, inclusive
  2147. *
  2148. * Return: 0 for success or negative errno on failure.
  2149. */
  2150. int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
  2151. {
  2152. int ret;
  2153. if (!clk)
  2154. return 0;
  2155. clk_prepare_lock();
  2156. ret = clk_set_rate_range_nolock(clk, min, max);
  2157. clk_prepare_unlock();
  2158. return ret;
  2159. }
  2160. EXPORT_SYMBOL_GPL(clk_set_rate_range);
  2161. /**
  2162. * clk_set_min_rate - set a minimum clock rate for a clock source
  2163. * @clk: clock source
  2164. * @rate: desired minimum clock rate in Hz, inclusive
  2165. *
  2166. * Returns success (0) or negative errno.
  2167. */
  2168. int clk_set_min_rate(struct clk *clk, unsigned long rate)
  2169. {
  2170. if (!clk)
  2171. return 0;
  2172. trace_clk_set_min_rate(clk->core, rate);
  2173. return clk_set_rate_range(clk, rate, clk->max_rate);
  2174. }
  2175. EXPORT_SYMBOL_GPL(clk_set_min_rate);
  2176. /**
  2177. * clk_set_max_rate - set a maximum clock rate for a clock source
  2178. * @clk: clock source
  2179. * @rate: desired maximum clock rate in Hz, inclusive
  2180. *
  2181. * Returns success (0) or negative errno.
  2182. */
  2183. int clk_set_max_rate(struct clk *clk, unsigned long rate)
  2184. {
  2185. if (!clk)
  2186. return 0;
  2187. trace_clk_set_max_rate(clk->core, rate);
  2188. return clk_set_rate_range(clk, clk->min_rate, rate);
  2189. }
  2190. EXPORT_SYMBOL_GPL(clk_set_max_rate);
  2191. /**
  2192. * clk_get_parent - return the parent of a clk
  2193. * @clk: the clk whose parent gets returned
  2194. *
  2195. * Simply returns clk->parent. Returns NULL if clk is NULL.
  2196. */
  2197. struct clk *clk_get_parent(struct clk *clk)
  2198. {
  2199. struct clk *parent;
  2200. if (!clk)
  2201. return NULL;
  2202. clk_prepare_lock();
  2203. /* TODO: Create a per-user clk and change callers to call clk_put */
  2204. parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
  2205. clk_prepare_unlock();
  2206. return parent;
  2207. }
  2208. EXPORT_SYMBOL_GPL(clk_get_parent);
  2209. static struct clk_core *__clk_init_parent(struct clk_core *core)
  2210. {
  2211. u8 index = 0;
  2212. if (core->num_parents > 1 && core->ops->get_parent)
  2213. index = core->ops->get_parent(core->hw);
  2214. return clk_core_get_parent_by_index(core, index);
  2215. }
  2216. static void clk_core_reparent(struct clk_core *core,
  2217. struct clk_core *new_parent)
  2218. {
  2219. clk_reparent(core, new_parent);
  2220. __clk_recalc_accuracies(core);
  2221. __clk_recalc_rates(core, true, POST_RATE_CHANGE);
  2222. }
  2223. void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
  2224. {
  2225. if (!hw)
  2226. return;
  2227. clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
  2228. }
  2229. /**
  2230. * clk_has_parent - check if a clock is a possible parent for another
  2231. * @clk: clock source
  2232. * @parent: parent clock source
  2233. *
  2234. * This function can be used in drivers that need to check that a clock can be
  2235. * the parent of another without actually changing the parent.
  2236. *
  2237. * Returns true if @parent is a possible parent for @clk, false otherwise.
  2238. */
  2239. bool clk_has_parent(const struct clk *clk, const struct clk *parent)
  2240. {
  2241. /* NULL clocks should be nops, so return success if either is NULL. */
  2242. if (!clk || !parent)
  2243. return true;
  2244. return clk_core_has_parent(clk->core, parent->core);
  2245. }
  2246. EXPORT_SYMBOL_GPL(clk_has_parent);
  2247. static int clk_core_set_parent_nolock(struct clk_core *core,
  2248. struct clk_core *parent)
  2249. {
  2250. int ret = 0;
  2251. int p_index = 0;
  2252. unsigned long p_rate = 0;
  2253. lockdep_assert_held(&prepare_lock);
  2254. if (!core)
  2255. return 0;
  2256. if (core->parent == parent)
  2257. return 0;
  2258. /* verify ops for multi-parent clks */
  2259. if (core->num_parents > 1 && !core->ops->set_parent)
  2260. return -EPERM;
  2261. /* check that we are allowed to re-parent if the clock is in use */
  2262. if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
  2263. return -EBUSY;
  2264. if (clk_core_rate_is_protected(core))
  2265. return -EBUSY;
  2266. /* try finding the new parent index */
  2267. if (parent) {
  2268. p_index = clk_fetch_parent_index(core, parent);
  2269. if (p_index < 0) {
  2270. pr_debug("%s: clk %s can not be parent of clk %s\n",
  2271. __func__, parent->name, core->name);
  2272. return p_index;
  2273. }
  2274. p_rate = parent->rate;
  2275. }
  2276. ret = clk_pm_runtime_get(core);
  2277. if (ret)
  2278. return ret;
  2279. /* propagate PRE_RATE_CHANGE notifications */
  2280. ret = __clk_speculate_rates(core, p_rate);
  2281. /* abort if a driver objects */
  2282. if (ret & NOTIFY_STOP_MASK)
  2283. goto runtime_put;
  2284. /* do the re-parent */
  2285. ret = __clk_set_parent(core, parent, p_index);
  2286. /* propagate rate an accuracy recalculation accordingly */
  2287. if (ret) {
  2288. __clk_recalc_rates(core, true, ABORT_RATE_CHANGE);
  2289. } else {
  2290. __clk_recalc_rates(core, true, POST_RATE_CHANGE);
  2291. __clk_recalc_accuracies(core);
  2292. }
  2293. runtime_put:
  2294. clk_pm_runtime_put(core);
  2295. return ret;
  2296. }
  2297. int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *parent)
  2298. {
  2299. return clk_core_set_parent_nolock(hw->core, parent->core);
  2300. }
  2301. EXPORT_SYMBOL_GPL(clk_hw_set_parent);
  2302. /**
  2303. * clk_set_parent - switch the parent of a mux clk
  2304. * @clk: the mux clk whose input we are switching
  2305. * @parent: the new input to clk
  2306. *
  2307. * Re-parent clk to use parent as its new input source. If clk is in
  2308. * prepared state, the clk will get enabled for the duration of this call. If
  2309. * that's not acceptable for a specific clk (Eg: the consumer can't handle
  2310. * that, the reparenting is glitchy in hardware, etc), use the
  2311. * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
  2312. *
  2313. * After successfully changing clk's parent clk_set_parent will update the
  2314. * clk topology, sysfs topology and propagate rate recalculation via
  2315. * __clk_recalc_rates.
  2316. *
  2317. * Returns 0 on success, -EERROR otherwise.
  2318. */
  2319. int clk_set_parent(struct clk *clk, struct clk *parent)
  2320. {
  2321. int ret;
  2322. if (!clk)
  2323. return 0;
  2324. clk_prepare_lock();
  2325. if (clk->exclusive_count)
  2326. clk_core_rate_unprotect(clk->core);
  2327. ret = clk_core_set_parent_nolock(clk->core,
  2328. parent ? parent->core : NULL);
  2329. if (clk->exclusive_count)
  2330. clk_core_rate_protect(clk->core);
  2331. clk_prepare_unlock();
  2332. return ret;
  2333. }
  2334. EXPORT_SYMBOL_GPL(clk_set_parent);
  2335. static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
  2336. {
  2337. int ret = -EINVAL;
  2338. lockdep_assert_held(&prepare_lock);
  2339. if (!core)
  2340. return 0;
  2341. if (clk_core_rate_is_protected(core))
  2342. return -EBUSY;
  2343. trace_clk_set_phase(core, degrees);
  2344. if (core->ops->set_phase) {
  2345. ret = core->ops->set_phase(core->hw, degrees);
  2346. if (!ret)
  2347. core->phase = degrees;
  2348. }
  2349. trace_clk_set_phase_complete(core, degrees);
  2350. return ret;
  2351. }
  2352. /**
  2353. * clk_set_phase - adjust the phase shift of a clock signal
  2354. * @clk: clock signal source
  2355. * @degrees: number of degrees the signal is shifted
  2356. *
  2357. * Shifts the phase of a clock signal by the specified
  2358. * degrees. Returns 0 on success, -EERROR otherwise.
  2359. *
  2360. * This function makes no distinction about the input or reference
  2361. * signal that we adjust the clock signal phase against. For example
  2362. * phase locked-loop clock signal generators we may shift phase with
  2363. * respect to feedback clock signal input, but for other cases the
  2364. * clock phase may be shifted with respect to some other, unspecified
  2365. * signal.
  2366. *
  2367. * Additionally the concept of phase shift does not propagate through
  2368. * the clock tree hierarchy, which sets it apart from clock rates and
  2369. * clock accuracy. A parent clock phase attribute does not have an
  2370. * impact on the phase attribute of a child clock.
  2371. */
  2372. int clk_set_phase(struct clk *clk, int degrees)
  2373. {
  2374. int ret;
  2375. if (!clk)
  2376. return 0;
  2377. /* sanity check degrees */
  2378. degrees %= 360;
  2379. if (degrees < 0)
  2380. degrees += 360;
  2381. clk_prepare_lock();
  2382. if (clk->exclusive_count)
  2383. clk_core_rate_unprotect(clk->core);
  2384. ret = clk_core_set_phase_nolock(clk->core, degrees);
  2385. if (clk->exclusive_count)
  2386. clk_core_rate_protect(clk->core);
  2387. clk_prepare_unlock();
  2388. return ret;
  2389. }
  2390. EXPORT_SYMBOL_GPL(clk_set_phase);
  2391. static int clk_core_get_phase(struct clk_core *core)
  2392. {
  2393. int ret;
  2394. lockdep_assert_held(&prepare_lock);
  2395. if (!core->ops->get_phase)
  2396. return 0;
  2397. /* Always try to update cached phase if possible */
  2398. ret = core->ops->get_phase(core->hw);
  2399. if (ret >= 0)
  2400. core->phase = ret;
  2401. return ret;
  2402. }
  2403. /**
  2404. * clk_get_phase - return the phase shift of a clock signal
  2405. * @clk: clock signal source
  2406. *
  2407. * Returns the phase shift of a clock node in degrees, otherwise returns
  2408. * -EERROR.
  2409. */
  2410. int clk_get_phase(struct clk *clk)
  2411. {
  2412. int ret;
  2413. if (!clk)
  2414. return 0;
  2415. clk_prepare_lock();
  2416. ret = clk_core_get_phase(clk->core);
  2417. clk_prepare_unlock();
  2418. return ret;
  2419. }
  2420. EXPORT_SYMBOL_GPL(clk_get_phase);
  2421. static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
  2422. {
  2423. /* Assume a default value of 50% */
  2424. core->duty.num = 1;
  2425. core->duty.den = 2;
  2426. }
  2427. static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
  2428. static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
  2429. {
  2430. struct clk_duty *duty = &core->duty;
  2431. int ret = 0;
  2432. if (!core->ops->get_duty_cycle)
  2433. return clk_core_update_duty_cycle_parent_nolock(core);
  2434. ret = core->ops->get_duty_cycle(core->hw, duty);
  2435. if (ret)
  2436. goto reset;
  2437. /* Don't trust the clock provider too much */
  2438. if (duty->den == 0 || duty->num > duty->den) {
  2439. ret = -EINVAL;
  2440. goto reset;
  2441. }
  2442. return 0;
  2443. reset:
  2444. clk_core_reset_duty_cycle_nolock(core);
  2445. return ret;
  2446. }
  2447. static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
  2448. {
  2449. int ret = 0;
  2450. if (core->parent &&
  2451. core->flags & CLK_DUTY_CYCLE_PARENT) {
  2452. ret = clk_core_update_duty_cycle_nolock(core->parent);
  2453. memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
  2454. } else {
  2455. clk_core_reset_duty_cycle_nolock(core);
  2456. }
  2457. return ret;
  2458. }
  2459. static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
  2460. struct clk_duty *duty);
  2461. static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
  2462. struct clk_duty *duty)
  2463. {
  2464. int ret;
  2465. lockdep_assert_held(&prepare_lock);
  2466. if (clk_core_rate_is_protected(core))
  2467. return -EBUSY;
  2468. trace_clk_set_duty_cycle(core, duty);
  2469. if (!core->ops->set_duty_cycle)
  2470. return clk_core_set_duty_cycle_parent_nolock(core, duty);
  2471. ret = core->ops->set_duty_cycle(core->hw, duty);
  2472. if (!ret)
  2473. memcpy(&core->duty, duty, sizeof(*duty));
  2474. trace_clk_set_duty_cycle_complete(core, duty);
  2475. return ret;
  2476. }
  2477. static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
  2478. struct clk_duty *duty)
  2479. {
  2480. int ret = 0;
  2481. if (core->parent &&
  2482. core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
  2483. ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
  2484. memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
  2485. }
  2486. return ret;
  2487. }
  2488. /**
  2489. * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
  2490. * @clk: clock signal source
  2491. * @num: numerator of the duty cycle ratio to be applied
  2492. * @den: denominator of the duty cycle ratio to be applied
  2493. *
  2494. * Apply the duty cycle ratio if the ratio is valid and the clock can
  2495. * perform this operation
  2496. *
  2497. * Returns (0) on success, a negative errno otherwise.
  2498. */
  2499. int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
  2500. {
  2501. int ret;
  2502. struct clk_duty duty;
  2503. if (!clk)
  2504. return 0;
  2505. /* sanity check the ratio */
  2506. if (den == 0 || num > den)
  2507. return -EINVAL;
  2508. duty.num = num;
  2509. duty.den = den;
  2510. clk_prepare_lock();
  2511. if (clk->exclusive_count)
  2512. clk_core_rate_unprotect(clk->core);
  2513. ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
  2514. if (clk->exclusive_count)
  2515. clk_core_rate_protect(clk->core);
  2516. clk_prepare_unlock();
  2517. return ret;
  2518. }
  2519. EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
  2520. static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
  2521. unsigned int scale)
  2522. {
  2523. struct clk_duty *duty = &core->duty;
  2524. int ret;
  2525. clk_prepare_lock();
  2526. ret = clk_core_update_duty_cycle_nolock(core);
  2527. if (!ret)
  2528. ret = mult_frac(scale, duty->num, duty->den);
  2529. clk_prepare_unlock();
  2530. return ret;
  2531. }
  2532. /**
  2533. * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
  2534. * @clk: clock signal source
  2535. * @scale: scaling factor to be applied to represent the ratio as an integer
  2536. *
  2537. * Returns the duty cycle ratio of a clock node multiplied by the provided
  2538. * scaling factor, or negative errno on error.
  2539. */
  2540. int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
  2541. {
  2542. if (!clk)
  2543. return 0;
  2544. return clk_core_get_scaled_duty_cycle(clk->core, scale);
  2545. }
  2546. EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
  2547. /**
  2548. * clk_is_match - check if two clk's point to the same hardware clock
  2549. * @p: clk compared against q
  2550. * @q: clk compared against p
  2551. *
  2552. * Returns true if the two struct clk pointers both point to the same hardware
  2553. * clock node. Put differently, returns true if struct clk *p and struct clk *q
  2554. * share the same struct clk_core object.
  2555. *
  2556. * Returns false otherwise. Note that two NULL clks are treated as matching.
  2557. */
  2558. bool clk_is_match(const struct clk *p, const struct clk *q)
  2559. {
  2560. /* trivial case: identical struct clk's or both NULL */
  2561. if (p == q)
  2562. return true;
  2563. /* true if clk->core pointers match. Avoid dereferencing garbage */
  2564. if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
  2565. if (p->core == q->core)
  2566. return true;
  2567. return false;
  2568. }
  2569. EXPORT_SYMBOL_GPL(clk_is_match);
  2570. /*** debugfs support ***/
  2571. #ifdef CONFIG_DEBUG_FS
  2572. #include <linux/debugfs.h>
  2573. static struct dentry *rootdir;
  2574. static int inited = 0;
  2575. static DEFINE_MUTEX(clk_debug_lock);
  2576. static HLIST_HEAD(clk_debug_list);
  2577. static struct hlist_head *orphan_list[] = {
  2578. &clk_orphan_list,
  2579. NULL,
  2580. };
  2581. static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
  2582. int level)
  2583. {
  2584. int phase;
  2585. seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
  2586. level * 3 + 1, "",
  2587. 30 - level * 3, c->name,
  2588. c->enable_count, c->prepare_count, c->protect_count,
  2589. clk_core_get_rate_recalc(c),
  2590. clk_core_get_accuracy_recalc(c));
  2591. phase = clk_core_get_phase(c);
  2592. if (phase >= 0)
  2593. seq_printf(s, "%5d", phase);
  2594. else
  2595. seq_puts(s, "-----");
  2596. seq_printf(s, " %6d", clk_core_get_scaled_duty_cycle(c, 100000));
  2597. if (c->ops->is_enabled)
  2598. seq_printf(s, " %9c\n", clk_core_is_enabled(c) ? 'Y' : 'N');
  2599. else if (!c->ops->enable)
  2600. seq_printf(s, " %9c\n", 'Y');
  2601. else
  2602. seq_printf(s, " %9c\n", '?');
  2603. }
  2604. static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
  2605. int level)
  2606. {
  2607. struct clk_core *child;
  2608. clk_pm_runtime_get(c);
  2609. clk_summary_show_one(s, c, level);
  2610. clk_pm_runtime_put(c);
  2611. hlist_for_each_entry(child, &c->children, child_node)
  2612. clk_summary_show_subtree(s, child, level + 1);
  2613. }
  2614. static int clk_summary_show(struct seq_file *s, void *data)
  2615. {
  2616. struct clk_core *c;
  2617. struct hlist_head **lists = (struct hlist_head **)s->private;
  2618. seq_puts(s, " enable prepare protect duty hardware\n");
  2619. seq_puts(s, " clock count count count rate accuracy phase cycle enable\n");
  2620. seq_puts(s, "-------------------------------------------------------------------------------------------------------\n");
  2621. clk_prepare_lock();
  2622. for (; *lists; lists++)
  2623. hlist_for_each_entry(c, *lists, child_node)
  2624. clk_summary_show_subtree(s, c, 0);
  2625. clk_prepare_unlock();
  2626. return 0;
  2627. }
  2628. DEFINE_SHOW_ATTRIBUTE(clk_summary);
  2629. static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
  2630. {
  2631. int phase;
  2632. unsigned long min_rate, max_rate;
  2633. clk_core_get_boundaries(c, &min_rate, &max_rate);
  2634. /* This should be JSON format, i.e. elements separated with a comma */
  2635. seq_printf(s, "\"%s\": { ", c->name);
  2636. seq_printf(s, "\"enable_count\": %d,", c->enable_count);
  2637. seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
  2638. seq_printf(s, "\"protect_count\": %d,", c->protect_count);
  2639. seq_printf(s, "\"rate\": %lu,", clk_core_get_rate_recalc(c));
  2640. seq_printf(s, "\"min_rate\": %lu,", min_rate);
  2641. seq_printf(s, "\"max_rate\": %lu,", max_rate);
  2642. seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy_recalc(c));
  2643. phase = clk_core_get_phase(c);
  2644. if (phase >= 0)
  2645. seq_printf(s, "\"phase\": %d,", phase);
  2646. seq_printf(s, "\"duty_cycle\": %u",
  2647. clk_core_get_scaled_duty_cycle(c, 100000));
  2648. }
  2649. static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
  2650. {
  2651. struct clk_core *child;
  2652. clk_dump_one(s, c, level);
  2653. hlist_for_each_entry(child, &c->children, child_node) {
  2654. seq_putc(s, ',');
  2655. clk_dump_subtree(s, child, level + 1);
  2656. }
  2657. seq_putc(s, '}');
  2658. }
  2659. static int clk_dump_show(struct seq_file *s, void *data)
  2660. {
  2661. struct clk_core *c;
  2662. bool first_node = true;
  2663. struct hlist_head **lists = (struct hlist_head **)s->private;
  2664. seq_putc(s, '{');
  2665. clk_prepare_lock();
  2666. for (; *lists; lists++) {
  2667. hlist_for_each_entry(c, *lists, child_node) {
  2668. if (!first_node)
  2669. seq_putc(s, ',');
  2670. first_node = false;
  2671. clk_dump_subtree(s, c, 0);
  2672. }
  2673. }
  2674. clk_prepare_unlock();
  2675. seq_puts(s, "}\n");
  2676. return 0;
  2677. }
  2678. DEFINE_SHOW_ATTRIBUTE(clk_dump);
  2679. #define CLOCK_ALLOW_WRITE_DEBUGFS
  2680. #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
  2681. /*
  2682. * This can be dangerous, therefore don't provide any real compile time
  2683. * configuration option for this feature.
  2684. * People who want to use this will need to modify the source code directly.
  2685. */
  2686. static int clk_rate_set(void *data, u64 val)
  2687. {
  2688. struct clk_core *core = data;
  2689. int ret;
  2690. clk_prepare_lock();
  2691. ret = clk_core_set_rate_nolock(core, val);
  2692. clk_prepare_unlock();
  2693. return ret;
  2694. }
  2695. #define clk_rate_mode 0644
  2696. static int clk_prepare_enable_set(void *data, u64 val)
  2697. {
  2698. struct clk_core *core = data;
  2699. int ret = 0;
  2700. if (val)
  2701. ret = clk_prepare_enable(core->hw->clk);
  2702. else
  2703. clk_disable_unprepare(core->hw->clk);
  2704. return ret;
  2705. }
  2706. static int clk_prepare_enable_get(void *data, u64 *val)
  2707. {
  2708. struct clk_core *core = data;
  2709. *val = core->enable_count && core->prepare_count;
  2710. return 0;
  2711. }
  2712. DEFINE_DEBUGFS_ATTRIBUTE(clk_prepare_enable_fops, clk_prepare_enable_get,
  2713. clk_prepare_enable_set, "%llu\n");
  2714. #else
  2715. #define clk_rate_set NULL
  2716. #define clk_rate_mode 0444
  2717. #endif
  2718. static int clk_rate_get(void *data, u64 *val)
  2719. {
  2720. struct clk_core *core = data;
  2721. clk_prepare_lock();
  2722. *val = clk_core_get_rate_recalc(core);
  2723. clk_prepare_unlock();
  2724. return 0;
  2725. }
  2726. DEFINE_DEBUGFS_ATTRIBUTE(clk_rate_fops, clk_rate_get, clk_rate_set, "%llu\n");
  2727. static const struct {
  2728. unsigned long flag;
  2729. const char *name;
  2730. } clk_flags[] = {
  2731. #define ENTRY(f) { f, #f }
  2732. ENTRY(CLK_SET_RATE_GATE),
  2733. ENTRY(CLK_SET_PARENT_GATE),
  2734. ENTRY(CLK_SET_RATE_PARENT),
  2735. ENTRY(CLK_IGNORE_UNUSED),
  2736. ENTRY(CLK_GET_RATE_NOCACHE),
  2737. ENTRY(CLK_SET_RATE_NO_REPARENT),
  2738. ENTRY(CLK_GET_ACCURACY_NOCACHE),
  2739. ENTRY(CLK_RECALC_NEW_RATES),
  2740. ENTRY(CLK_SET_RATE_UNGATE),
  2741. ENTRY(CLK_IS_CRITICAL),
  2742. ENTRY(CLK_OPS_PARENT_ENABLE),
  2743. ENTRY(CLK_DUTY_CYCLE_PARENT),
  2744. #undef ENTRY
  2745. };
  2746. static int clk_flags_show(struct seq_file *s, void *data)
  2747. {
  2748. struct clk_core *core = s->private;
  2749. unsigned long flags = core->flags;
  2750. unsigned int i;
  2751. for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
  2752. if (flags & clk_flags[i].flag) {
  2753. seq_printf(s, "%s\n", clk_flags[i].name);
  2754. flags &= ~clk_flags[i].flag;
  2755. }
  2756. }
  2757. if (flags) {
  2758. /* Unknown flags */
  2759. seq_printf(s, "0x%lx\n", flags);
  2760. }
  2761. return 0;
  2762. }
  2763. DEFINE_SHOW_ATTRIBUTE(clk_flags);
  2764. static void possible_parent_show(struct seq_file *s, struct clk_core *core,
  2765. unsigned int i, char terminator)
  2766. {
  2767. struct clk_core *parent;
  2768. const char *name = NULL;
  2769. /*
  2770. * Go through the following options to fetch a parent's name.
  2771. *
  2772. * 1. Fetch the registered parent clock and use its name
  2773. * 2. Use the global (fallback) name if specified
  2774. * 3. Use the local fw_name if provided
  2775. * 4. Fetch parent clock's clock-output-name if DT index was set
  2776. *
  2777. * This may still fail in some cases, such as when the parent is
  2778. * specified directly via a struct clk_hw pointer, but it isn't
  2779. * registered (yet).
  2780. */
  2781. parent = clk_core_get_parent_by_index(core, i);
  2782. if (parent) {
  2783. seq_puts(s, parent->name);
  2784. } else if (core->parents[i].name) {
  2785. seq_puts(s, core->parents[i].name);
  2786. } else if (core->parents[i].fw_name) {
  2787. seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
  2788. } else {
  2789. if (core->parents[i].index >= 0)
  2790. name = of_clk_get_parent_name(core->of_node, core->parents[i].index);
  2791. if (!name)
  2792. name = "(missing)";
  2793. seq_puts(s, name);
  2794. }
  2795. seq_putc(s, terminator);
  2796. }
  2797. static int possible_parents_show(struct seq_file *s, void *data)
  2798. {
  2799. struct clk_core *core = s->private;
  2800. int i;
  2801. for (i = 0; i < core->num_parents - 1; i++)
  2802. possible_parent_show(s, core, i, ' ');
  2803. possible_parent_show(s, core, i, '\n');
  2804. return 0;
  2805. }
  2806. DEFINE_SHOW_ATTRIBUTE(possible_parents);
  2807. static int current_parent_show(struct seq_file *s, void *data)
  2808. {
  2809. struct clk_core *core = s->private;
  2810. if (core->parent)
  2811. seq_printf(s, "%s\n", core->parent->name);
  2812. return 0;
  2813. }
  2814. DEFINE_SHOW_ATTRIBUTE(current_parent);
  2815. #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
  2816. static ssize_t current_parent_write(struct file *file, const char __user *ubuf,
  2817. size_t count, loff_t *ppos)
  2818. {
  2819. struct seq_file *s = file->private_data;
  2820. struct clk_core *core = s->private;
  2821. struct clk_core *parent;
  2822. u8 idx;
  2823. int err;
  2824. err = kstrtou8_from_user(ubuf, count, 0, &idx);
  2825. if (err < 0)
  2826. return err;
  2827. parent = clk_core_get_parent_by_index(core, idx);
  2828. if (!parent)
  2829. return -ENOENT;
  2830. clk_prepare_lock();
  2831. err = clk_core_set_parent_nolock(core, parent);
  2832. clk_prepare_unlock();
  2833. if (err)
  2834. return err;
  2835. return count;
  2836. }
  2837. static const struct file_operations current_parent_rw_fops = {
  2838. .open = current_parent_open,
  2839. .write = current_parent_write,
  2840. .read = seq_read,
  2841. .llseek = seq_lseek,
  2842. .release = single_release,
  2843. };
  2844. #endif
  2845. static int clk_duty_cycle_show(struct seq_file *s, void *data)
  2846. {
  2847. struct clk_core *core = s->private;
  2848. struct clk_duty *duty = &core->duty;
  2849. seq_printf(s, "%u/%u\n", duty->num, duty->den);
  2850. return 0;
  2851. }
  2852. DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
  2853. static int clk_min_rate_show(struct seq_file *s, void *data)
  2854. {
  2855. struct clk_core *core = s->private;
  2856. unsigned long min_rate, max_rate;
  2857. clk_prepare_lock();
  2858. clk_core_get_boundaries(core, &min_rate, &max_rate);
  2859. clk_prepare_unlock();
  2860. seq_printf(s, "%lu\n", min_rate);
  2861. return 0;
  2862. }
  2863. DEFINE_SHOW_ATTRIBUTE(clk_min_rate);
  2864. static int clk_max_rate_show(struct seq_file *s, void *data)
  2865. {
  2866. struct clk_core *core = s->private;
  2867. unsigned long min_rate, max_rate;
  2868. clk_prepare_lock();
  2869. clk_core_get_boundaries(core, &min_rate, &max_rate);
  2870. clk_prepare_unlock();
  2871. seq_printf(s, "%lu\n", max_rate);
  2872. return 0;
  2873. }
  2874. DEFINE_SHOW_ATTRIBUTE(clk_max_rate);
  2875. static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
  2876. {
  2877. struct dentry *root;
  2878. if (!core || !pdentry)
  2879. return;
  2880. root = debugfs_create_dir(core->name, pdentry);
  2881. core->dentry = root;
  2882. debugfs_create_file("clk_rate", clk_rate_mode, root, core,
  2883. &clk_rate_fops);
  2884. debugfs_create_file("clk_min_rate", 0444, root, core, &clk_min_rate_fops);
  2885. debugfs_create_file("clk_max_rate", 0444, root, core, &clk_max_rate_fops);
  2886. debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
  2887. debugfs_create_u32("clk_phase", 0444, root, &core->phase);
  2888. debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
  2889. debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
  2890. debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
  2891. debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
  2892. debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
  2893. debugfs_create_file("clk_duty_cycle", 0444, root, core,
  2894. &clk_duty_cycle_fops);
  2895. #ifdef CLOCK_ALLOW_WRITE_DEBUGFS
  2896. debugfs_create_file("clk_prepare_enable", 0644, root, core,
  2897. &clk_prepare_enable_fops);
  2898. if (core->num_parents > 1)
  2899. debugfs_create_file("clk_parent", 0644, root, core,
  2900. &current_parent_rw_fops);
  2901. else
  2902. #endif
  2903. if (core->num_parents > 0)
  2904. debugfs_create_file("clk_parent", 0444, root, core,
  2905. &current_parent_fops);
  2906. if (core->num_parents > 1)
  2907. debugfs_create_file("clk_possible_parents", 0444, root, core,
  2908. &possible_parents_fops);
  2909. if (core->ops->debug_init)
  2910. core->ops->debug_init(core->hw, core->dentry);
  2911. }
  2912. /**
  2913. * clk_debug_register - add a clk node to the debugfs clk directory
  2914. * @core: the clk being added to the debugfs clk directory
  2915. *
  2916. * Dynamically adds a clk to the debugfs clk directory if debugfs has been
  2917. * initialized. Otherwise it bails out early since the debugfs clk directory
  2918. * will be created lazily by clk_debug_init as part of a late_initcall.
  2919. */
  2920. static void clk_debug_register(struct clk_core *core)
  2921. {
  2922. mutex_lock(&clk_debug_lock);
  2923. hlist_add_head(&core->debug_node, &clk_debug_list);
  2924. if (inited)
  2925. clk_debug_create_one(core, rootdir);
  2926. mutex_unlock(&clk_debug_lock);
  2927. }
  2928. /**
  2929. * clk_debug_unregister - remove a clk node from the debugfs clk directory
  2930. * @core: the clk being removed from the debugfs clk directory
  2931. *
  2932. * Dynamically removes a clk and all its child nodes from the
  2933. * debugfs clk directory if clk->dentry points to debugfs created by
  2934. * clk_debug_register in __clk_core_init.
  2935. */
  2936. static void clk_debug_unregister(struct clk_core *core)
  2937. {
  2938. mutex_lock(&clk_debug_lock);
  2939. hlist_del_init(&core->debug_node);
  2940. debugfs_remove_recursive(core->dentry);
  2941. core->dentry = NULL;
  2942. mutex_unlock(&clk_debug_lock);
  2943. }
  2944. /**
  2945. * clk_debug_init - lazily populate the debugfs clk directory
  2946. *
  2947. * clks are often initialized very early during boot before memory can be
  2948. * dynamically allocated and well before debugfs is setup. This function
  2949. * populates the debugfs clk directory once at boot-time when we know that
  2950. * debugfs is setup. It should only be called once at boot-time, all other clks
  2951. * added dynamically will be done so with clk_debug_register.
  2952. */
  2953. static int __init clk_debug_init(void)
  2954. {
  2955. struct clk_core *core;
  2956. rootdir = debugfs_create_dir("clk", NULL);
  2957. debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
  2958. &clk_summary_fops);
  2959. debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
  2960. &clk_dump_fops);
  2961. debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
  2962. &clk_summary_fops);
  2963. debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
  2964. &clk_dump_fops);
  2965. mutex_lock(&clk_debug_lock);
  2966. hlist_for_each_entry(core, &clk_debug_list, debug_node)
  2967. clk_debug_create_one(core, rootdir);
  2968. inited = 1;
  2969. mutex_unlock(&clk_debug_lock);
  2970. return 0;
  2971. }
  2972. late_initcall(clk_debug_init);
  2973. #else
  2974. static inline void clk_debug_register(struct clk_core *core) { }
  2975. static inline void clk_debug_unregister(struct clk_core *core)
  2976. {
  2977. }
  2978. #endif
  2979. static void clk_core_reparent_orphans_nolock(void)
  2980. {
  2981. struct clk_core *orphan;
  2982. struct hlist_node *tmp2;
  2983. /*
  2984. * walk the list of orphan clocks and reparent any that newly finds a
  2985. * parent.
  2986. */
  2987. hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
  2988. struct clk_core *parent = __clk_init_parent(orphan);
  2989. /*
  2990. * We need to use __clk_set_parent_before() and _after() to
  2991. * properly migrate any prepare/enable count of the orphan
  2992. * clock. This is important for CLK_IS_CRITICAL clocks, which
  2993. * are enabled during init but might not have a parent yet.
  2994. */
  2995. if (parent) {
  2996. /* update the clk tree topology */
  2997. __clk_set_parent_before(orphan, parent);
  2998. __clk_set_parent_after(orphan, parent, NULL);
  2999. __clk_recalc_accuracies(orphan);
  3000. __clk_recalc_rates(orphan, true, 0);
  3001. __clk_core_update_orphan_hold_state(orphan);
  3002. /*
  3003. * __clk_init_parent() will set the initial req_rate to
  3004. * 0 if the clock doesn't have clk_ops::recalc_rate and
  3005. * is an orphan when it's registered.
  3006. *
  3007. * 'req_rate' is used by clk_set_rate_range() and
  3008. * clk_put() to trigger a clk_set_rate() call whenever
  3009. * the boundaries are modified. Let's make sure
  3010. * 'req_rate' is set to something non-zero so that
  3011. * clk_set_rate_range() doesn't drop the frequency.
  3012. */
  3013. orphan->req_rate = orphan->rate;
  3014. }
  3015. }
  3016. }
  3017. /**
  3018. * __clk_core_init - initialize the data structures in a struct clk_core
  3019. * @core: clk_core being initialized
  3020. *
  3021. * Initializes the lists in struct clk_core, queries the hardware for the
  3022. * parent and rate and sets them both.
  3023. */
  3024. static int __clk_core_init(struct clk_core *core)
  3025. {
  3026. int ret;
  3027. struct clk_core *parent;
  3028. unsigned long rate;
  3029. int phase;
  3030. clk_prepare_lock();
  3031. /*
  3032. * Set hw->core after grabbing the prepare_lock to synchronize with
  3033. * callers of clk_core_fill_parent_index() where we treat hw->core
  3034. * being NULL as the clk not being registered yet. This is crucial so
  3035. * that clks aren't parented until their parent is fully registered.
  3036. */
  3037. core->hw->core = core;
  3038. ret = clk_pm_runtime_get(core);
  3039. if (ret)
  3040. goto unlock;
  3041. /* check to see if a clock with this name is already registered */
  3042. if (clk_core_lookup(core->name)) {
  3043. pr_debug("%s: clk %s already initialized\n",
  3044. __func__, core->name);
  3045. ret = -EEXIST;
  3046. goto out;
  3047. }
  3048. /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
  3049. if (core->ops->set_rate &&
  3050. !((core->ops->round_rate || core->ops->determine_rate) &&
  3051. core->ops->recalc_rate)) {
  3052. pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
  3053. __func__, core->name);
  3054. ret = -EINVAL;
  3055. goto out;
  3056. }
  3057. if (core->ops->set_parent && !core->ops->get_parent) {
  3058. pr_err("%s: %s must implement .get_parent & .set_parent\n",
  3059. __func__, core->name);
  3060. ret = -EINVAL;
  3061. goto out;
  3062. }
  3063. if (core->num_parents > 1 && !core->ops->get_parent) {
  3064. pr_err("%s: %s must implement .get_parent as it has multi parents\n",
  3065. __func__, core->name);
  3066. ret = -EINVAL;
  3067. goto out;
  3068. }
  3069. if (core->ops->set_rate_and_parent &&
  3070. !(core->ops->set_parent && core->ops->set_rate)) {
  3071. pr_err("%s: %s must implement .set_parent & .set_rate\n",
  3072. __func__, core->name);
  3073. ret = -EINVAL;
  3074. goto out;
  3075. }
  3076. /*
  3077. * optional platform-specific magic
  3078. *
  3079. * The .init callback is not used by any of the basic clock types, but
  3080. * exists for weird hardware that must perform initialization magic for
  3081. * CCF to get an accurate view of clock for any other callbacks. It may
  3082. * also be used needs to perform dynamic allocations. Such allocation
  3083. * must be freed in the terminate() callback.
  3084. * This callback shall not be used to initialize the parameters state,
  3085. * such as rate, parent, etc ...
  3086. *
  3087. * If it exist, this callback should called before any other callback of
  3088. * the clock
  3089. */
  3090. if (core->ops->init) {
  3091. ret = core->ops->init(core->hw);
  3092. if (ret)
  3093. goto out;
  3094. }
  3095. parent = core->parent = __clk_init_parent(core);
  3096. /*
  3097. * Populate core->parent if parent has already been clk_core_init'd. If
  3098. * parent has not yet been clk_core_init'd then place clk in the orphan
  3099. * list. If clk doesn't have any parents then place it in the root
  3100. * clk list.
  3101. *
  3102. * Every time a new clk is clk_init'd then we walk the list of orphan
  3103. * clocks and re-parent any that are children of the clock currently
  3104. * being clk_init'd.
  3105. */
  3106. if (parent) {
  3107. hlist_add_head(&core->child_node, &parent->children);
  3108. core->orphan = parent->orphan;
  3109. } else if (!core->num_parents) {
  3110. hlist_add_head(&core->child_node, &clk_root_list);
  3111. core->orphan = false;
  3112. } else {
  3113. hlist_add_head(&core->child_node, &clk_orphan_list);
  3114. core->orphan = true;
  3115. }
  3116. /*
  3117. * Set clk's accuracy. The preferred method is to use
  3118. * .recalc_accuracy. For simple clocks and lazy developers the default
  3119. * fallback is to use the parent's accuracy. If a clock doesn't have a
  3120. * parent (or is orphaned) then accuracy is set to zero (perfect
  3121. * clock).
  3122. */
  3123. if (core->ops->recalc_accuracy)
  3124. core->accuracy = core->ops->recalc_accuracy(core->hw,
  3125. clk_core_get_accuracy_no_lock(parent));
  3126. else if (parent)
  3127. core->accuracy = parent->accuracy;
  3128. else
  3129. core->accuracy = 0;
  3130. /*
  3131. * Set clk's phase by clk_core_get_phase() caching the phase.
  3132. * Since a phase is by definition relative to its parent, just
  3133. * query the current clock phase, or just assume it's in phase.
  3134. */
  3135. phase = clk_core_get_phase(core);
  3136. if (phase < 0) {
  3137. ret = phase;
  3138. pr_warn("%s: Failed to get phase for clk '%s'\n", __func__,
  3139. core->name);
  3140. goto out;
  3141. }
  3142. /*
  3143. * Set clk's duty cycle.
  3144. */
  3145. clk_core_update_duty_cycle_nolock(core);
  3146. /*
  3147. * Set clk's rate. The preferred method is to use .recalc_rate. For
  3148. * simple clocks and lazy developers the default fallback is to use the
  3149. * parent's rate. If a clock doesn't have a parent (or is orphaned)
  3150. * then rate is set to zero.
  3151. */
  3152. if (core->ops->recalc_rate)
  3153. rate = core->ops->recalc_rate(core->hw,
  3154. clk_core_get_rate_nolock(parent));
  3155. else if (parent)
  3156. rate = parent->rate;
  3157. else
  3158. rate = 0;
  3159. core->rate = core->req_rate = rate;
  3160. core->boot_enabled = clk_core_is_enabled(core);
  3161. /*
  3162. * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
  3163. * don't get accidentally disabled when walking the orphan tree and
  3164. * reparenting clocks
  3165. */
  3166. if (core->flags & CLK_IS_CRITICAL) {
  3167. ret = clk_core_prepare(core);
  3168. if (ret) {
  3169. pr_warn("%s: critical clk '%s' failed to prepare\n",
  3170. __func__, core->name);
  3171. goto out;
  3172. }
  3173. ret = clk_core_enable_lock(core);
  3174. if (ret) {
  3175. pr_warn("%s: critical clk '%s' failed to enable\n",
  3176. __func__, core->name);
  3177. clk_core_unprepare(core);
  3178. goto out;
  3179. }
  3180. }
  3181. clk_core_hold_state(core);
  3182. clk_core_reparent_orphans_nolock();
  3183. kref_init(&core->ref);
  3184. out:
  3185. clk_pm_runtime_put(core);
  3186. unlock:
  3187. if (ret) {
  3188. hlist_del_init(&core->child_node);
  3189. core->hw->core = NULL;
  3190. }
  3191. clk_prepare_unlock();
  3192. if (!ret)
  3193. clk_debug_register(core);
  3194. return ret;
  3195. }
  3196. /**
  3197. * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
  3198. * @core: clk to add consumer to
  3199. * @clk: consumer to link to a clk
  3200. */
  3201. static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
  3202. {
  3203. clk_prepare_lock();
  3204. hlist_add_head(&clk->clks_node, &core->clks);
  3205. clk_prepare_unlock();
  3206. }
  3207. /**
  3208. * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
  3209. * @clk: consumer to unlink
  3210. */
  3211. static void clk_core_unlink_consumer(struct clk *clk)
  3212. {
  3213. lockdep_assert_held(&prepare_lock);
  3214. hlist_del(&clk->clks_node);
  3215. }
  3216. /**
  3217. * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
  3218. * @core: clk to allocate a consumer for
  3219. * @dev_id: string describing device name
  3220. * @con_id: connection ID string on device
  3221. *
  3222. * Returns: clk consumer left unlinked from the consumer list
  3223. */
  3224. static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
  3225. const char *con_id)
  3226. {
  3227. struct clk *clk;
  3228. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  3229. if (!clk)
  3230. return ERR_PTR(-ENOMEM);
  3231. clk->core = core;
  3232. clk->dev_id = dev_id;
  3233. clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
  3234. clk->max_rate = ULONG_MAX;
  3235. return clk;
  3236. }
  3237. /**
  3238. * free_clk - Free a clk consumer
  3239. * @clk: clk consumer to free
  3240. *
  3241. * Note, this assumes the clk has been unlinked from the clk_core consumer
  3242. * list.
  3243. */
  3244. static void free_clk(struct clk *clk)
  3245. {
  3246. kfree_const(clk->con_id);
  3247. kfree(clk);
  3248. }
  3249. /**
  3250. * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
  3251. * a clk_hw
  3252. * @dev: clk consumer device
  3253. * @hw: clk_hw associated with the clk being consumed
  3254. * @dev_id: string describing device name
  3255. * @con_id: connection ID string on device
  3256. *
  3257. * This is the main function used to create a clk pointer for use by clk
  3258. * consumers. It connects a consumer to the clk_core and clk_hw structures
  3259. * used by the framework and clk provider respectively.
  3260. */
  3261. struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
  3262. const char *dev_id, const char *con_id)
  3263. {
  3264. struct clk *clk;
  3265. struct clk_core *core;
  3266. /* This is to allow this function to be chained to others */
  3267. if (IS_ERR_OR_NULL(hw))
  3268. return ERR_CAST(hw);
  3269. core = hw->core;
  3270. clk = alloc_clk(core, dev_id, con_id);
  3271. if (IS_ERR(clk))
  3272. return clk;
  3273. clk->dev = dev;
  3274. if (!try_module_get(core->owner)) {
  3275. free_clk(clk);
  3276. return ERR_PTR(-ENOENT);
  3277. }
  3278. kref_get(&core->ref);
  3279. clk_core_link_consumer(core, clk);
  3280. return clk;
  3281. }
  3282. /**
  3283. * clk_hw_get_clk - get clk consumer given an clk_hw
  3284. * @hw: clk_hw associated with the clk being consumed
  3285. * @con_id: connection ID string on device
  3286. *
  3287. * Returns: new clk consumer
  3288. * This is the function to be used by providers which need
  3289. * to get a consumer clk and act on the clock element
  3290. * Calls to this function must be balanced with calls clk_put()
  3291. */
  3292. struct clk *clk_hw_get_clk(struct clk_hw *hw, const char *con_id)
  3293. {
  3294. struct device *dev = hw->core->dev;
  3295. const char *name = dev ? dev_name(dev) : NULL;
  3296. return clk_hw_create_clk(dev, hw, name, con_id);
  3297. }
  3298. EXPORT_SYMBOL(clk_hw_get_clk);
  3299. static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
  3300. {
  3301. const char *dst;
  3302. if (!src) {
  3303. if (must_exist)
  3304. return -EINVAL;
  3305. return 0;
  3306. }
  3307. *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
  3308. if (!dst)
  3309. return -ENOMEM;
  3310. return 0;
  3311. }
  3312. static int clk_core_populate_parent_map(struct clk_core *core,
  3313. const struct clk_init_data *init)
  3314. {
  3315. u8 num_parents = init->num_parents;
  3316. const char * const *parent_names = init->parent_names;
  3317. const struct clk_hw **parent_hws = init->parent_hws;
  3318. const struct clk_parent_data *parent_data = init->parent_data;
  3319. int i, ret = 0;
  3320. struct clk_parent_map *parents, *parent;
  3321. if (!num_parents)
  3322. return 0;
  3323. /*
  3324. * Avoid unnecessary string look-ups of clk_core's possible parents by
  3325. * having a cache of names/clk_hw pointers to clk_core pointers.
  3326. */
  3327. parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
  3328. core->parents = parents;
  3329. if (!parents)
  3330. return -ENOMEM;
  3331. /* Copy everything over because it might be __initdata */
  3332. for (i = 0, parent = parents; i < num_parents; i++, parent++) {
  3333. parent->index = -1;
  3334. if (parent_names) {
  3335. /* throw a WARN if any entries are NULL */
  3336. WARN(!parent_names[i],
  3337. "%s: invalid NULL in %s's .parent_names\n",
  3338. __func__, core->name);
  3339. ret = clk_cpy_name(&parent->name, parent_names[i],
  3340. true);
  3341. } else if (parent_data) {
  3342. parent->hw = parent_data[i].hw;
  3343. parent->index = parent_data[i].index;
  3344. ret = clk_cpy_name(&parent->fw_name,
  3345. parent_data[i].fw_name, false);
  3346. if (!ret)
  3347. ret = clk_cpy_name(&parent->name,
  3348. parent_data[i].name,
  3349. false);
  3350. } else if (parent_hws) {
  3351. parent->hw = parent_hws[i];
  3352. } else {
  3353. ret = -EINVAL;
  3354. WARN(1, "Must specify parents if num_parents > 0\n");
  3355. }
  3356. if (ret) {
  3357. do {
  3358. kfree_const(parents[i].name);
  3359. kfree_const(parents[i].fw_name);
  3360. } while (--i >= 0);
  3361. kfree(parents);
  3362. return ret;
  3363. }
  3364. }
  3365. return 0;
  3366. }
  3367. static void clk_core_free_parent_map(struct clk_core *core)
  3368. {
  3369. int i = core->num_parents;
  3370. if (!core->num_parents)
  3371. return;
  3372. while (--i >= 0) {
  3373. kfree_const(core->parents[i].name);
  3374. kfree_const(core->parents[i].fw_name);
  3375. }
  3376. kfree(core->parents);
  3377. }
  3378. static struct clk *
  3379. __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
  3380. {
  3381. int ret;
  3382. struct clk_core *core;
  3383. const struct clk_init_data *init = hw->init;
  3384. /*
  3385. * The init data is not supposed to be used outside of registration path.
  3386. * Set it to NULL so that provider drivers can't use it either and so that
  3387. * we catch use of hw->init early on in the core.
  3388. */
  3389. hw->init = NULL;
  3390. core = kzalloc(sizeof(*core), GFP_KERNEL);
  3391. if (!core) {
  3392. ret = -ENOMEM;
  3393. goto fail_out;
  3394. }
  3395. core->name = kstrdup_const(init->name, GFP_KERNEL);
  3396. if (!core->name) {
  3397. ret = -ENOMEM;
  3398. goto fail_name;
  3399. }
  3400. if (WARN_ON(!init->ops)) {
  3401. ret = -EINVAL;
  3402. goto fail_ops;
  3403. }
  3404. core->ops = init->ops;
  3405. if (dev && pm_runtime_enabled(dev))
  3406. core->rpm_enabled = true;
  3407. core->dev = dev;
  3408. core->of_node = np;
  3409. if (dev && dev->driver)
  3410. core->owner = dev->driver->owner;
  3411. core->hw = hw;
  3412. core->flags = init->flags;
  3413. core->num_parents = init->num_parents;
  3414. core->min_rate = 0;
  3415. core->max_rate = ULONG_MAX;
  3416. ret = clk_core_populate_parent_map(core, init);
  3417. if (ret)
  3418. goto fail_parents;
  3419. INIT_HLIST_HEAD(&core->clks);
  3420. /*
  3421. * Don't call clk_hw_create_clk() here because that would pin the
  3422. * provider module to itself and prevent it from ever being removed.
  3423. */
  3424. hw->clk = alloc_clk(core, NULL, NULL);
  3425. if (IS_ERR(hw->clk)) {
  3426. ret = PTR_ERR(hw->clk);
  3427. goto fail_create_clk;
  3428. }
  3429. clk_core_link_consumer(core, hw->clk);
  3430. ret = __clk_core_init(core);
  3431. if (!ret)
  3432. return hw->clk;
  3433. clk_prepare_lock();
  3434. clk_core_unlink_consumer(hw->clk);
  3435. clk_prepare_unlock();
  3436. free_clk(hw->clk);
  3437. hw->clk = NULL;
  3438. fail_create_clk:
  3439. clk_core_free_parent_map(core);
  3440. fail_parents:
  3441. fail_ops:
  3442. kfree_const(core->name);
  3443. fail_name:
  3444. kfree(core);
  3445. fail_out:
  3446. return ERR_PTR(ret);
  3447. }
  3448. /**
  3449. * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
  3450. * @dev: Device to get device node of
  3451. *
  3452. * Return: device node pointer of @dev, or the device node pointer of
  3453. * @dev->parent if dev doesn't have a device node, or NULL if neither
  3454. * @dev or @dev->parent have a device node.
  3455. */
  3456. static struct device_node *dev_or_parent_of_node(struct device *dev)
  3457. {
  3458. struct device_node *np;
  3459. if (!dev)
  3460. return NULL;
  3461. np = dev_of_node(dev);
  3462. if (!np)
  3463. np = dev_of_node(dev->parent);
  3464. return np;
  3465. }
  3466. /**
  3467. * clk_register - allocate a new clock, register it and return an opaque cookie
  3468. * @dev: device that is registering this clock
  3469. * @hw: link to hardware-specific clock data
  3470. *
  3471. * clk_register is the *deprecated* interface for populating the clock tree with
  3472. * new clock nodes. Use clk_hw_register() instead.
  3473. *
  3474. * Returns: a pointer to the newly allocated struct clk which
  3475. * cannot be dereferenced by driver code but may be used in conjunction with the
  3476. * rest of the clock API. In the event of an error clk_register will return an
  3477. * error code; drivers must test for an error code after calling clk_register.
  3478. */
  3479. struct clk *clk_register(struct device *dev, struct clk_hw *hw)
  3480. {
  3481. return __clk_register(dev, dev_or_parent_of_node(dev), hw);
  3482. }
  3483. EXPORT_SYMBOL_GPL(clk_register);
  3484. /**
  3485. * clk_hw_register - register a clk_hw and return an error code
  3486. * @dev: device that is registering this clock
  3487. * @hw: link to hardware-specific clock data
  3488. *
  3489. * clk_hw_register is the primary interface for populating the clock tree with
  3490. * new clock nodes. It returns an integer equal to zero indicating success or
  3491. * less than zero indicating failure. Drivers must test for an error code after
  3492. * calling clk_hw_register().
  3493. */
  3494. int clk_hw_register(struct device *dev, struct clk_hw *hw)
  3495. {
  3496. return PTR_ERR_OR_ZERO(__clk_register(dev, dev_or_parent_of_node(dev),
  3497. hw));
  3498. }
  3499. EXPORT_SYMBOL_GPL(clk_hw_register);
  3500. /*
  3501. * of_clk_hw_register - register a clk_hw and return an error code
  3502. * @node: device_node of device that is registering this clock
  3503. * @hw: link to hardware-specific clock data
  3504. *
  3505. * of_clk_hw_register() is the primary interface for populating the clock tree
  3506. * with new clock nodes when a struct device is not available, but a struct
  3507. * device_node is. It returns an integer equal to zero indicating success or
  3508. * less than zero indicating failure. Drivers must test for an error code after
  3509. * calling of_clk_hw_register().
  3510. */
  3511. int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
  3512. {
  3513. return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
  3514. }
  3515. EXPORT_SYMBOL_GPL(of_clk_hw_register);
  3516. /* Free memory allocated for a clock. */
  3517. static void __clk_release(struct kref *ref)
  3518. {
  3519. struct clk_core *core = container_of(ref, struct clk_core, ref);
  3520. lockdep_assert_held(&prepare_lock);
  3521. clk_core_free_parent_map(core);
  3522. kfree_const(core->name);
  3523. kfree(core);
  3524. }
  3525. /*
  3526. * Empty clk_ops for unregistered clocks. These are used temporarily
  3527. * after clk_unregister() was called on a clock and until last clock
  3528. * consumer calls clk_put() and the struct clk object is freed.
  3529. */
  3530. static int clk_nodrv_prepare_enable(struct clk_hw *hw)
  3531. {
  3532. return -ENXIO;
  3533. }
  3534. static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
  3535. {
  3536. WARN_ON_ONCE(1);
  3537. }
  3538. static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
  3539. unsigned long parent_rate)
  3540. {
  3541. return -ENXIO;
  3542. }
  3543. static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
  3544. {
  3545. return -ENXIO;
  3546. }
  3547. static const struct clk_ops clk_nodrv_ops = {
  3548. .enable = clk_nodrv_prepare_enable,
  3549. .disable = clk_nodrv_disable_unprepare,
  3550. .prepare = clk_nodrv_prepare_enable,
  3551. .unprepare = clk_nodrv_disable_unprepare,
  3552. .set_rate = clk_nodrv_set_rate,
  3553. .set_parent = clk_nodrv_set_parent,
  3554. };
  3555. static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
  3556. const struct clk_core *target)
  3557. {
  3558. int i;
  3559. struct clk_core *child;
  3560. for (i = 0; i < root->num_parents; i++)
  3561. if (root->parents[i].core == target)
  3562. root->parents[i].core = NULL;
  3563. hlist_for_each_entry(child, &root->children, child_node)
  3564. clk_core_evict_parent_cache_subtree(child, target);
  3565. }
  3566. /* Remove this clk from all parent caches */
  3567. static void clk_core_evict_parent_cache(struct clk_core *core)
  3568. {
  3569. const struct hlist_head **lists;
  3570. struct clk_core *root;
  3571. lockdep_assert_held(&prepare_lock);
  3572. for (lists = all_lists; *lists; lists++)
  3573. hlist_for_each_entry(root, *lists, child_node)
  3574. clk_core_evict_parent_cache_subtree(root, core);
  3575. }
  3576. /**
  3577. * clk_unregister - unregister a currently registered clock
  3578. * @clk: clock to unregister
  3579. */
  3580. void clk_unregister(struct clk *clk)
  3581. {
  3582. unsigned long flags;
  3583. const struct clk_ops *ops;
  3584. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  3585. return;
  3586. clk_debug_unregister(clk->core);
  3587. clk_prepare_lock();
  3588. ops = clk->core->ops;
  3589. if (ops == &clk_nodrv_ops) {
  3590. pr_err("%s: unregistered clock: %s\n", __func__,
  3591. clk->core->name);
  3592. goto unlock;
  3593. }
  3594. /*
  3595. * Assign empty clock ops for consumers that might still hold
  3596. * a reference to this clock.
  3597. */
  3598. flags = clk_enable_lock();
  3599. clk->core->ops = &clk_nodrv_ops;
  3600. clk_enable_unlock(flags);
  3601. if (ops->terminate)
  3602. ops->terminate(clk->core->hw);
  3603. if (!hlist_empty(&clk->core->children)) {
  3604. struct clk_core *child;
  3605. struct hlist_node *t;
  3606. /* Reparent all children to the orphan list. */
  3607. hlist_for_each_entry_safe(child, t, &clk->core->children,
  3608. child_node)
  3609. clk_core_set_parent_nolock(child, NULL);
  3610. }
  3611. clk_core_evict_parent_cache(clk->core);
  3612. hlist_del_init(&clk->core->child_node);
  3613. if (clk->core->prepare_count)
  3614. pr_warn("%s: unregistering prepared clock: %s\n",
  3615. __func__, clk->core->name);
  3616. if (clk->core->protect_count)
  3617. pr_warn("%s: unregistering protected clock: %s\n",
  3618. __func__, clk->core->name);
  3619. kref_put(&clk->core->ref, __clk_release);
  3620. free_clk(clk);
  3621. unlock:
  3622. clk_prepare_unlock();
  3623. }
  3624. EXPORT_SYMBOL_GPL(clk_unregister);
  3625. /**
  3626. * clk_hw_unregister - unregister a currently registered clk_hw
  3627. * @hw: hardware-specific clock data to unregister
  3628. */
  3629. void clk_hw_unregister(struct clk_hw *hw)
  3630. {
  3631. clk_unregister(hw->clk);
  3632. }
  3633. EXPORT_SYMBOL_GPL(clk_hw_unregister);
  3634. static void devm_clk_unregister_cb(struct device *dev, void *res)
  3635. {
  3636. clk_unregister(*(struct clk **)res);
  3637. }
  3638. static void devm_clk_hw_unregister_cb(struct device *dev, void *res)
  3639. {
  3640. clk_hw_unregister(*(struct clk_hw **)res);
  3641. }
  3642. /**
  3643. * devm_clk_register - resource managed clk_register()
  3644. * @dev: device that is registering this clock
  3645. * @hw: link to hardware-specific clock data
  3646. *
  3647. * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
  3648. *
  3649. * Clocks returned from this function are automatically clk_unregister()ed on
  3650. * driver detach. See clk_register() for more information.
  3651. */
  3652. struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
  3653. {
  3654. struct clk *clk;
  3655. struct clk **clkp;
  3656. clkp = devres_alloc(devm_clk_unregister_cb, sizeof(*clkp), GFP_KERNEL);
  3657. if (!clkp)
  3658. return ERR_PTR(-ENOMEM);
  3659. clk = clk_register(dev, hw);
  3660. if (!IS_ERR(clk)) {
  3661. *clkp = clk;
  3662. devres_add(dev, clkp);
  3663. } else {
  3664. devres_free(clkp);
  3665. }
  3666. return clk;
  3667. }
  3668. EXPORT_SYMBOL_GPL(devm_clk_register);
  3669. /**
  3670. * devm_clk_hw_register - resource managed clk_hw_register()
  3671. * @dev: device that is registering this clock
  3672. * @hw: link to hardware-specific clock data
  3673. *
  3674. * Managed clk_hw_register(). Clocks registered by this function are
  3675. * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
  3676. * for more information.
  3677. */
  3678. int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
  3679. {
  3680. struct clk_hw **hwp;
  3681. int ret;
  3682. hwp = devres_alloc(devm_clk_hw_unregister_cb, sizeof(*hwp), GFP_KERNEL);
  3683. if (!hwp)
  3684. return -ENOMEM;
  3685. ret = clk_hw_register(dev, hw);
  3686. if (!ret) {
  3687. *hwp = hw;
  3688. devres_add(dev, hwp);
  3689. } else {
  3690. devres_free(hwp);
  3691. }
  3692. return ret;
  3693. }
  3694. EXPORT_SYMBOL_GPL(devm_clk_hw_register);
  3695. static void devm_clk_release(struct device *dev, void *res)
  3696. {
  3697. clk_put(*(struct clk **)res);
  3698. }
  3699. /**
  3700. * devm_clk_hw_get_clk - resource managed clk_hw_get_clk()
  3701. * @dev: device that is registering this clock
  3702. * @hw: clk_hw associated with the clk being consumed
  3703. * @con_id: connection ID string on device
  3704. *
  3705. * Managed clk_hw_get_clk(). Clocks got with this function are
  3706. * automatically clk_put() on driver detach. See clk_put()
  3707. * for more information.
  3708. */
  3709. struct clk *devm_clk_hw_get_clk(struct device *dev, struct clk_hw *hw,
  3710. const char *con_id)
  3711. {
  3712. struct clk *clk;
  3713. struct clk **clkp;
  3714. /* This should not happen because it would mean we have drivers
  3715. * passing around clk_hw pointers instead of having the caller use
  3716. * proper clk_get() style APIs
  3717. */
  3718. WARN_ON_ONCE(dev != hw->core->dev);
  3719. clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
  3720. if (!clkp)
  3721. return ERR_PTR(-ENOMEM);
  3722. clk = clk_hw_get_clk(hw, con_id);
  3723. if (!IS_ERR(clk)) {
  3724. *clkp = clk;
  3725. devres_add(dev, clkp);
  3726. } else {
  3727. devres_free(clkp);
  3728. }
  3729. return clk;
  3730. }
  3731. EXPORT_SYMBOL_GPL(devm_clk_hw_get_clk);
  3732. /*
  3733. * clkdev helpers
  3734. */
  3735. void __clk_put(struct clk *clk)
  3736. {
  3737. struct module *owner;
  3738. if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
  3739. return;
  3740. clk_prepare_lock();
  3741. /*
  3742. * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
  3743. * given user should be balanced with calls to clk_rate_exclusive_put()
  3744. * and by that same consumer
  3745. */
  3746. if (WARN_ON(clk->exclusive_count)) {
  3747. /* We voiced our concern, let's sanitize the situation */
  3748. clk->core->protect_count -= (clk->exclusive_count - 1);
  3749. clk_core_rate_unprotect(clk->core);
  3750. clk->exclusive_count = 0;
  3751. }
  3752. hlist_del(&clk->clks_node);
  3753. /* If we had any boundaries on that clock, let's drop them. */
  3754. if (clk->min_rate > 0 || clk->max_rate < ULONG_MAX)
  3755. clk_set_rate_range_nolock(clk, 0, ULONG_MAX);
  3756. owner = clk->core->owner;
  3757. kref_put(&clk->core->ref, __clk_release);
  3758. clk_prepare_unlock();
  3759. module_put(owner);
  3760. free_clk(clk);
  3761. }
  3762. /*** clk rate change notifiers ***/
  3763. /**
  3764. * clk_notifier_register - add a clk rate change notifier
  3765. * @clk: struct clk * to watch
  3766. * @nb: struct notifier_block * with callback info
  3767. *
  3768. * Request notification when clk's rate changes. This uses an SRCU
  3769. * notifier because we want it to block and notifier unregistrations are
  3770. * uncommon. The callbacks associated with the notifier must not
  3771. * re-enter into the clk framework by calling any top-level clk APIs;
  3772. * this will cause a nested prepare_lock mutex.
  3773. *
  3774. * In all notification cases (pre, post and abort rate change) the original
  3775. * clock rate is passed to the callback via struct clk_notifier_data.old_rate
  3776. * and the new frequency is passed via struct clk_notifier_data.new_rate.
  3777. *
  3778. * clk_notifier_register() must be called from non-atomic context.
  3779. * Returns -EINVAL if called with null arguments, -ENOMEM upon
  3780. * allocation failure; otherwise, passes along the return value of
  3781. * srcu_notifier_chain_register().
  3782. */
  3783. int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
  3784. {
  3785. struct clk_notifier *cn;
  3786. int ret = -ENOMEM;
  3787. if (!clk || !nb)
  3788. return -EINVAL;
  3789. clk_prepare_lock();
  3790. /* search the list of notifiers for this clk */
  3791. list_for_each_entry(cn, &clk_notifier_list, node)
  3792. if (cn->clk == clk)
  3793. goto found;
  3794. /* if clk wasn't in the notifier list, allocate new clk_notifier */
  3795. cn = kzalloc(sizeof(*cn), GFP_KERNEL);
  3796. if (!cn)
  3797. goto out;
  3798. cn->clk = clk;
  3799. srcu_init_notifier_head(&cn->notifier_head);
  3800. list_add(&cn->node, &clk_notifier_list);
  3801. found:
  3802. ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
  3803. clk->core->notifier_count++;
  3804. out:
  3805. clk_prepare_unlock();
  3806. return ret;
  3807. }
  3808. EXPORT_SYMBOL_GPL(clk_notifier_register);
  3809. /**
  3810. * clk_notifier_unregister - remove a clk rate change notifier
  3811. * @clk: struct clk *
  3812. * @nb: struct notifier_block * with callback info
  3813. *
  3814. * Request no further notification for changes to 'clk' and frees memory
  3815. * allocated in clk_notifier_register.
  3816. *
  3817. * Returns -EINVAL if called with null arguments; otherwise, passes
  3818. * along the return value of srcu_notifier_chain_unregister().
  3819. */
  3820. int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
  3821. {
  3822. struct clk_notifier *cn;
  3823. int ret = -ENOENT;
  3824. if (!clk || !nb)
  3825. return -EINVAL;
  3826. clk_prepare_lock();
  3827. list_for_each_entry(cn, &clk_notifier_list, node) {
  3828. if (cn->clk == clk) {
  3829. ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
  3830. clk->core->notifier_count--;
  3831. /* XXX the notifier code should handle this better */
  3832. if (!cn->notifier_head.head) {
  3833. srcu_cleanup_notifier_head(&cn->notifier_head);
  3834. list_del(&cn->node);
  3835. kfree(cn);
  3836. }
  3837. break;
  3838. }
  3839. }
  3840. clk_prepare_unlock();
  3841. return ret;
  3842. }
  3843. EXPORT_SYMBOL_GPL(clk_notifier_unregister);
  3844. struct clk_notifier_devres {
  3845. struct clk *clk;
  3846. struct notifier_block *nb;
  3847. };
  3848. static void devm_clk_notifier_release(struct device *dev, void *res)
  3849. {
  3850. struct clk_notifier_devres *devres = res;
  3851. clk_notifier_unregister(devres->clk, devres->nb);
  3852. }
  3853. int devm_clk_notifier_register(struct device *dev, struct clk *clk,
  3854. struct notifier_block *nb)
  3855. {
  3856. struct clk_notifier_devres *devres;
  3857. int ret;
  3858. devres = devres_alloc(devm_clk_notifier_release,
  3859. sizeof(*devres), GFP_KERNEL);
  3860. if (!devres)
  3861. return -ENOMEM;
  3862. ret = clk_notifier_register(clk, nb);
  3863. if (!ret) {
  3864. devres->clk = clk;
  3865. devres->nb = nb;
  3866. devres_add(dev, devres);
  3867. } else {
  3868. devres_free(devres);
  3869. }
  3870. return ret;
  3871. }
  3872. EXPORT_SYMBOL_GPL(devm_clk_notifier_register);
  3873. #ifdef CONFIG_OF
  3874. static void clk_core_reparent_orphans(void)
  3875. {
  3876. clk_prepare_lock();
  3877. clk_core_reparent_orphans_nolock();
  3878. clk_prepare_unlock();
  3879. }
  3880. /**
  3881. * struct of_clk_provider - Clock provider registration structure
  3882. * @link: Entry in global list of clock providers
  3883. * @node: Pointer to device tree node of clock provider
  3884. * @get: Get clock callback. Returns NULL or a struct clk for the
  3885. * given clock specifier
  3886. * @get_hw: Get clk_hw callback. Returns NULL, ERR_PTR or a
  3887. * struct clk_hw for the given clock specifier
  3888. * @data: context pointer to be passed into @get callback
  3889. */
  3890. struct of_clk_provider {
  3891. struct list_head link;
  3892. struct device_node *node;
  3893. struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
  3894. struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
  3895. void *data;
  3896. };
  3897. extern struct of_device_id __clk_of_table;
  3898. static const struct of_device_id __clk_of_table_sentinel
  3899. __used __section("__clk_of_table_end");
  3900. static LIST_HEAD(of_clk_providers);
  3901. static DEFINE_MUTEX(of_clk_mutex);
  3902. struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
  3903. void *data)
  3904. {
  3905. return data;
  3906. }
  3907. EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
  3908. struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
  3909. {
  3910. return data;
  3911. }
  3912. EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
  3913. struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
  3914. {
  3915. struct clk_onecell_data *clk_data = data;
  3916. unsigned int idx = clkspec->args[0];
  3917. if (idx >= clk_data->clk_num) {
  3918. pr_err("%s: invalid clock index %u\n", __func__, idx);
  3919. return ERR_PTR(-EINVAL);
  3920. }
  3921. return clk_data->clks[idx];
  3922. }
  3923. EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
  3924. struct clk_hw *
  3925. of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
  3926. {
  3927. struct clk_hw_onecell_data *hw_data = data;
  3928. unsigned int idx = clkspec->args[0];
  3929. if (idx >= hw_data->num) {
  3930. pr_err("%s: invalid index %u\n", __func__, idx);
  3931. return ERR_PTR(-EINVAL);
  3932. }
  3933. return hw_data->hws[idx];
  3934. }
  3935. EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
  3936. /**
  3937. * of_clk_add_provider() - Register a clock provider for a node
  3938. * @np: Device node pointer associated with clock provider
  3939. * @clk_src_get: callback for decoding clock
  3940. * @data: context pointer for @clk_src_get callback.
  3941. *
  3942. * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
  3943. */
  3944. int of_clk_add_provider(struct device_node *np,
  3945. struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
  3946. void *data),
  3947. void *data)
  3948. {
  3949. struct of_clk_provider *cp;
  3950. int ret;
  3951. if (!np)
  3952. return 0;
  3953. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  3954. if (!cp)
  3955. return -ENOMEM;
  3956. cp->node = of_node_get(np);
  3957. cp->data = data;
  3958. cp->get = clk_src_get;
  3959. mutex_lock(&of_clk_mutex);
  3960. list_add(&cp->link, &of_clk_providers);
  3961. mutex_unlock(&of_clk_mutex);
  3962. pr_debug("Added clock from %pOF\n", np);
  3963. clk_core_reparent_orphans();
  3964. ret = of_clk_set_defaults(np, true);
  3965. if (ret < 0)
  3966. of_clk_del_provider(np);
  3967. fwnode_dev_initialized(&np->fwnode, true);
  3968. return ret;
  3969. }
  3970. EXPORT_SYMBOL_GPL(of_clk_add_provider);
  3971. /**
  3972. * of_clk_add_hw_provider() - Register a clock provider for a node
  3973. * @np: Device node pointer associated with clock provider
  3974. * @get: callback for decoding clk_hw
  3975. * @data: context pointer for @get callback.
  3976. */
  3977. int of_clk_add_hw_provider(struct device_node *np,
  3978. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  3979. void *data),
  3980. void *data)
  3981. {
  3982. struct of_clk_provider *cp;
  3983. int ret;
  3984. if (!np)
  3985. return 0;
  3986. cp = kzalloc(sizeof(*cp), GFP_KERNEL);
  3987. if (!cp)
  3988. return -ENOMEM;
  3989. cp->node = of_node_get(np);
  3990. cp->data = data;
  3991. cp->get_hw = get;
  3992. mutex_lock(&of_clk_mutex);
  3993. list_add(&cp->link, &of_clk_providers);
  3994. mutex_unlock(&of_clk_mutex);
  3995. pr_debug("Added clk_hw provider from %pOF\n", np);
  3996. clk_core_reparent_orphans();
  3997. ret = of_clk_set_defaults(np, true);
  3998. if (ret < 0)
  3999. of_clk_del_provider(np);
  4000. fwnode_dev_initialized(&np->fwnode, true);
  4001. return ret;
  4002. }
  4003. EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
  4004. static void devm_of_clk_release_provider(struct device *dev, void *res)
  4005. {
  4006. of_clk_del_provider(*(struct device_node **)res);
  4007. }
  4008. /*
  4009. * We allow a child device to use its parent device as the clock provider node
  4010. * for cases like MFD sub-devices where the child device driver wants to use
  4011. * devm_*() APIs but not list the device in DT as a sub-node.
  4012. */
  4013. static struct device_node *get_clk_provider_node(struct device *dev)
  4014. {
  4015. struct device_node *np, *parent_np;
  4016. np = dev->of_node;
  4017. parent_np = dev->parent ? dev->parent->of_node : NULL;
  4018. if (!of_find_property(np, "#clock-cells", NULL))
  4019. if (of_find_property(parent_np, "#clock-cells", NULL))
  4020. np = parent_np;
  4021. return np;
  4022. }
  4023. /**
  4024. * devm_of_clk_add_hw_provider() - Managed clk provider node registration
  4025. * @dev: Device acting as the clock provider (used for DT node and lifetime)
  4026. * @get: callback for decoding clk_hw
  4027. * @data: context pointer for @get callback
  4028. *
  4029. * Registers clock provider for given device's node. If the device has no DT
  4030. * node or if the device node lacks of clock provider information (#clock-cells)
  4031. * then the parent device's node is scanned for this information. If parent node
  4032. * has the #clock-cells then it is used in registration. Provider is
  4033. * automatically released at device exit.
  4034. *
  4035. * Return: 0 on success or an errno on failure.
  4036. */
  4037. int devm_of_clk_add_hw_provider(struct device *dev,
  4038. struct clk_hw *(*get)(struct of_phandle_args *clkspec,
  4039. void *data),
  4040. void *data)
  4041. {
  4042. struct device_node **ptr, *np;
  4043. int ret;
  4044. ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
  4045. GFP_KERNEL);
  4046. if (!ptr)
  4047. return -ENOMEM;
  4048. np = get_clk_provider_node(dev);
  4049. ret = of_clk_add_hw_provider(np, get, data);
  4050. if (!ret) {
  4051. *ptr = np;
  4052. devres_add(dev, ptr);
  4053. } else {
  4054. devres_free(ptr);
  4055. }
  4056. return ret;
  4057. }
  4058. EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
  4059. /**
  4060. * of_clk_del_provider() - Remove a previously registered clock provider
  4061. * @np: Device node pointer associated with clock provider
  4062. */
  4063. void of_clk_del_provider(struct device_node *np)
  4064. {
  4065. struct of_clk_provider *cp;
  4066. if (!np)
  4067. return;
  4068. mutex_lock(&of_clk_mutex);
  4069. list_for_each_entry(cp, &of_clk_providers, link) {
  4070. if (cp->node == np) {
  4071. list_del(&cp->link);
  4072. fwnode_dev_initialized(&np->fwnode, false);
  4073. of_node_put(cp->node);
  4074. kfree(cp);
  4075. break;
  4076. }
  4077. }
  4078. mutex_unlock(&of_clk_mutex);
  4079. }
  4080. EXPORT_SYMBOL_GPL(of_clk_del_provider);
  4081. /**
  4082. * of_parse_clkspec() - Parse a DT clock specifier for a given device node
  4083. * @np: device node to parse clock specifier from
  4084. * @index: index of phandle to parse clock out of. If index < 0, @name is used
  4085. * @name: clock name to find and parse. If name is NULL, the index is used
  4086. * @out_args: Result of parsing the clock specifier
  4087. *
  4088. * Parses a device node's "clocks" and "clock-names" properties to find the
  4089. * phandle and cells for the index or name that is desired. The resulting clock
  4090. * specifier is placed into @out_args, or an errno is returned when there's a
  4091. * parsing error. The @index argument is ignored if @name is non-NULL.
  4092. *
  4093. * Example:
  4094. *
  4095. * phandle1: clock-controller@1 {
  4096. * #clock-cells = <2>;
  4097. * }
  4098. *
  4099. * phandle2: clock-controller@2 {
  4100. * #clock-cells = <1>;
  4101. * }
  4102. *
  4103. * clock-consumer@3 {
  4104. * clocks = <&phandle1 1 2 &phandle2 3>;
  4105. * clock-names = "name1", "name2";
  4106. * }
  4107. *
  4108. * To get a device_node for `clock-controller@2' node you may call this
  4109. * function a few different ways:
  4110. *
  4111. * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
  4112. * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
  4113. * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
  4114. *
  4115. * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
  4116. * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
  4117. * the "clock-names" property of @np.
  4118. */
  4119. static int of_parse_clkspec(const struct device_node *np, int index,
  4120. const char *name, struct of_phandle_args *out_args)
  4121. {
  4122. int ret = -ENOENT;
  4123. /* Walk up the tree of devices looking for a clock property that matches */
  4124. while (np) {
  4125. /*
  4126. * For named clocks, first look up the name in the
  4127. * "clock-names" property. If it cannot be found, then index
  4128. * will be an error code and of_parse_phandle_with_args() will
  4129. * return -EINVAL.
  4130. */
  4131. if (name)
  4132. index = of_property_match_string(np, "clock-names", name);
  4133. ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
  4134. index, out_args);
  4135. if (!ret)
  4136. break;
  4137. if (name && index >= 0)
  4138. break;
  4139. /*
  4140. * No matching clock found on this node. If the parent node
  4141. * has a "clock-ranges" property, then we can try one of its
  4142. * clocks.
  4143. */
  4144. np = np->parent;
  4145. if (np && !of_get_property(np, "clock-ranges", NULL))
  4146. break;
  4147. index = 0;
  4148. }
  4149. return ret;
  4150. }
  4151. static struct clk_hw *
  4152. __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
  4153. struct of_phandle_args *clkspec)
  4154. {
  4155. struct clk *clk;
  4156. if (provider->get_hw)
  4157. return provider->get_hw(clkspec, provider->data);
  4158. clk = provider->get(clkspec, provider->data);
  4159. if (IS_ERR(clk))
  4160. return ERR_CAST(clk);
  4161. return __clk_get_hw(clk);
  4162. }
  4163. static struct clk_hw *
  4164. of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
  4165. {
  4166. struct of_clk_provider *provider;
  4167. struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
  4168. if (!clkspec)
  4169. return ERR_PTR(-EINVAL);
  4170. mutex_lock(&of_clk_mutex);
  4171. list_for_each_entry(provider, &of_clk_providers, link) {
  4172. if (provider->node == clkspec->np) {
  4173. hw = __of_clk_get_hw_from_provider(provider, clkspec);
  4174. if (!IS_ERR(hw))
  4175. break;
  4176. }
  4177. }
  4178. mutex_unlock(&of_clk_mutex);
  4179. return hw;
  4180. }
  4181. /**
  4182. * of_clk_get_from_provider() - Lookup a clock from a clock provider
  4183. * @clkspec: pointer to a clock specifier data structure
  4184. *
  4185. * This function looks up a struct clk from the registered list of clock
  4186. * providers, an input is a clock specifier data structure as returned
  4187. * from the of_parse_phandle_with_args() function call.
  4188. */
  4189. struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
  4190. {
  4191. struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
  4192. return clk_hw_create_clk(NULL, hw, NULL, __func__);
  4193. }
  4194. EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
  4195. struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
  4196. const char *con_id)
  4197. {
  4198. int ret;
  4199. struct clk_hw *hw;
  4200. struct of_phandle_args clkspec;
  4201. ret = of_parse_clkspec(np, index, con_id, &clkspec);
  4202. if (ret)
  4203. return ERR_PTR(ret);
  4204. hw = of_clk_get_hw_from_clkspec(&clkspec);
  4205. of_node_put(clkspec.np);
  4206. return hw;
  4207. }
  4208. static struct clk *__of_clk_get(struct device_node *np,
  4209. int index, const char *dev_id,
  4210. const char *con_id)
  4211. {
  4212. struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
  4213. return clk_hw_create_clk(NULL, hw, dev_id, con_id);
  4214. }
  4215. struct clk *of_clk_get(struct device_node *np, int index)
  4216. {
  4217. return __of_clk_get(np, index, np->full_name, NULL);
  4218. }
  4219. EXPORT_SYMBOL(of_clk_get);
  4220. /**
  4221. * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
  4222. * @np: pointer to clock consumer node
  4223. * @name: name of consumer's clock input, or NULL for the first clock reference
  4224. *
  4225. * This function parses the clocks and clock-names properties,
  4226. * and uses them to look up the struct clk from the registered list of clock
  4227. * providers.
  4228. */
  4229. struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
  4230. {
  4231. if (!np)
  4232. return ERR_PTR(-ENOENT);
  4233. return __of_clk_get(np, 0, np->full_name, name);
  4234. }
  4235. EXPORT_SYMBOL(of_clk_get_by_name);
  4236. /**
  4237. * of_clk_get_parent_count() - Count the number of clocks a device node has
  4238. * @np: device node to count
  4239. *
  4240. * Returns: The number of clocks that are possible parents of this node
  4241. */
  4242. unsigned int of_clk_get_parent_count(const struct device_node *np)
  4243. {
  4244. int count;
  4245. count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
  4246. if (count < 0)
  4247. return 0;
  4248. return count;
  4249. }
  4250. EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
  4251. const char *of_clk_get_parent_name(const struct device_node *np, int index)
  4252. {
  4253. struct of_phandle_args clkspec;
  4254. struct property *prop;
  4255. const char *clk_name;
  4256. const __be32 *vp;
  4257. u32 pv;
  4258. int rc;
  4259. int count;
  4260. struct clk *clk;
  4261. rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
  4262. &clkspec);
  4263. if (rc)
  4264. return NULL;
  4265. index = clkspec.args_count ? clkspec.args[0] : 0;
  4266. count = 0;
  4267. /* if there is an indices property, use it to transfer the index
  4268. * specified into an array offset for the clock-output-names property.
  4269. */
  4270. of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
  4271. if (index == pv) {
  4272. index = count;
  4273. break;
  4274. }
  4275. count++;
  4276. }
  4277. /* We went off the end of 'clock-indices' without finding it */
  4278. if (prop && !vp)
  4279. return NULL;
  4280. if (of_property_read_string_index(clkspec.np, "clock-output-names",
  4281. index,
  4282. &clk_name) < 0) {
  4283. /*
  4284. * Best effort to get the name if the clock has been
  4285. * registered with the framework. If the clock isn't
  4286. * registered, we return the node name as the name of
  4287. * the clock as long as #clock-cells = 0.
  4288. */
  4289. clk = of_clk_get_from_provider(&clkspec);
  4290. if (IS_ERR(clk)) {
  4291. if (clkspec.args_count == 0)
  4292. clk_name = clkspec.np->name;
  4293. else
  4294. clk_name = NULL;
  4295. } else {
  4296. clk_name = __clk_get_name(clk);
  4297. clk_put(clk);
  4298. }
  4299. }
  4300. of_node_put(clkspec.np);
  4301. return clk_name;
  4302. }
  4303. EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
  4304. /**
  4305. * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
  4306. * number of parents
  4307. * @np: Device node pointer associated with clock provider
  4308. * @parents: pointer to char array that hold the parents' names
  4309. * @size: size of the @parents array
  4310. *
  4311. * Return: number of parents for the clock node.
  4312. */
  4313. int of_clk_parent_fill(struct device_node *np, const char **parents,
  4314. unsigned int size)
  4315. {
  4316. unsigned int i = 0;
  4317. while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
  4318. i++;
  4319. return i;
  4320. }
  4321. EXPORT_SYMBOL_GPL(of_clk_parent_fill);
  4322. struct clock_provider {
  4323. void (*clk_init_cb)(struct device_node *);
  4324. struct device_node *np;
  4325. struct list_head node;
  4326. };
  4327. /*
  4328. * This function looks for a parent clock. If there is one, then it
  4329. * checks that the provider for this parent clock was initialized, in
  4330. * this case the parent clock will be ready.
  4331. */
  4332. static int parent_ready(struct device_node *np)
  4333. {
  4334. int i = 0;
  4335. while (true) {
  4336. struct clk *clk = of_clk_get(np, i);
  4337. /* this parent is ready we can check the next one */
  4338. if (!IS_ERR(clk)) {
  4339. clk_put(clk);
  4340. i++;
  4341. continue;
  4342. }
  4343. /* at least one parent is not ready, we exit now */
  4344. if (PTR_ERR(clk) == -EPROBE_DEFER)
  4345. return 0;
  4346. /*
  4347. * Here we make assumption that the device tree is
  4348. * written correctly. So an error means that there is
  4349. * no more parent. As we didn't exit yet, then the
  4350. * previous parent are ready. If there is no clock
  4351. * parent, no need to wait for them, then we can
  4352. * consider their absence as being ready
  4353. */
  4354. return 1;
  4355. }
  4356. }
  4357. /**
  4358. * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
  4359. * @np: Device node pointer associated with clock provider
  4360. * @index: clock index
  4361. * @flags: pointer to top-level framework flags
  4362. *
  4363. * Detects if the clock-critical property exists and, if so, sets the
  4364. * corresponding CLK_IS_CRITICAL flag.
  4365. *
  4366. * Do not use this function. It exists only for legacy Device Tree
  4367. * bindings, such as the one-clock-per-node style that are outdated.
  4368. * Those bindings typically put all clock data into .dts and the Linux
  4369. * driver has no clock data, thus making it impossible to set this flag
  4370. * correctly from the driver. Only those drivers may call
  4371. * of_clk_detect_critical from their setup functions.
  4372. *
  4373. * Return: error code or zero on success
  4374. */
  4375. int of_clk_detect_critical(struct device_node *np, int index,
  4376. unsigned long *flags)
  4377. {
  4378. struct property *prop;
  4379. const __be32 *cur;
  4380. uint32_t idx;
  4381. if (!np || !flags)
  4382. return -EINVAL;
  4383. of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
  4384. if (index == idx)
  4385. *flags |= CLK_IS_CRITICAL;
  4386. return 0;
  4387. }
  4388. /**
  4389. * of_clk_init() - Scan and init clock providers from the DT
  4390. * @matches: array of compatible values and init functions for providers.
  4391. *
  4392. * This function scans the device tree for matching clock providers
  4393. * and calls their initialization functions. It also does it by trying
  4394. * to follow the dependencies.
  4395. */
  4396. void __init of_clk_init(const struct of_device_id *matches)
  4397. {
  4398. const struct of_device_id *match;
  4399. struct device_node *np;
  4400. struct clock_provider *clk_provider, *next;
  4401. bool is_init_done;
  4402. bool force = false;
  4403. LIST_HEAD(clk_provider_list);
  4404. if (!matches)
  4405. matches = &__clk_of_table;
  4406. /* First prepare the list of the clocks providers */
  4407. for_each_matching_node_and_match(np, matches, &match) {
  4408. struct clock_provider *parent;
  4409. if (!of_device_is_available(np))
  4410. continue;
  4411. parent = kzalloc(sizeof(*parent), GFP_KERNEL);
  4412. if (!parent) {
  4413. list_for_each_entry_safe(clk_provider, next,
  4414. &clk_provider_list, node) {
  4415. list_del(&clk_provider->node);
  4416. of_node_put(clk_provider->np);
  4417. kfree(clk_provider);
  4418. }
  4419. of_node_put(np);
  4420. return;
  4421. }
  4422. parent->clk_init_cb = match->data;
  4423. parent->np = of_node_get(np);
  4424. list_add_tail(&parent->node, &clk_provider_list);
  4425. }
  4426. while (!list_empty(&clk_provider_list)) {
  4427. is_init_done = false;
  4428. list_for_each_entry_safe(clk_provider, next,
  4429. &clk_provider_list, node) {
  4430. if (force || parent_ready(clk_provider->np)) {
  4431. /* Don't populate platform devices */
  4432. of_node_set_flag(clk_provider->np,
  4433. OF_POPULATED);
  4434. clk_provider->clk_init_cb(clk_provider->np);
  4435. of_clk_set_defaults(clk_provider->np, true);
  4436. list_del(&clk_provider->node);
  4437. of_node_put(clk_provider->np);
  4438. kfree(clk_provider);
  4439. is_init_done = true;
  4440. }
  4441. }
  4442. /*
  4443. * We didn't manage to initialize any of the
  4444. * remaining providers during the last loop, so now we
  4445. * initialize all the remaining ones unconditionally
  4446. * in case the clock parent was not mandatory
  4447. */
  4448. if (!is_init_done)
  4449. force = true;
  4450. }
  4451. }
  4452. #endif