clk-alpha-pll.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015, 2018, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021, 2023-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/export.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/regmap.h>
  10. #include <linux/delay.h>
  11. #include "clk-alpha-pll.h"
  12. #include "common.h"
  13. #define PLL_MODE(p) ((p)->offset + 0x0)
  14. # define PLL_OUTCTRL BIT(0)
  15. # define PLL_BYPASSNL BIT(1)
  16. # define PLL_RESET_N BIT(2)
  17. # define PLL_OFFLINE_REQ BIT(7)
  18. # define PLL_LOCK_COUNT_SHIFT 8
  19. # define PLL_LOCK_COUNT_MASK 0x3f
  20. # define PLL_BIAS_COUNT_SHIFT 14
  21. # define PLL_BIAS_COUNT_MASK 0x3f
  22. # define PLL_VOTE_FSM_ENA BIT(20)
  23. # define PLL_FSM_ENA BIT(20)
  24. # define PLL_VOTE_FSM_RESET BIT(21)
  25. # define PLL_UPDATE BIT(22)
  26. # define PLL_UPDATE_BYPASS BIT(23)
  27. # define PLL_FSM_LEGACY_MODE BIT(24)
  28. # define PLL_OFFLINE_ACK BIT(28)
  29. # define ALPHA_PLL_ACK_LATCH BIT(29)
  30. # define PLL_ACTIVE_FLAG BIT(30)
  31. # define PLL_LOCK_DET BIT(31)
  32. #define PLL_L_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_L_VAL])
  33. #define PLL_CAL_L_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_CAL_L_VAL])
  34. #define PLL_ALPHA_VAL(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL])
  35. #define PLL_ALPHA_VAL_U(p) ((p)->offset + (p)->regs[PLL_OFF_ALPHA_VAL_U])
  36. #define PLL_USER_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL])
  37. # define PLL_POST_DIV_SHIFT 8
  38. # define PLL_POST_DIV_MASK(p) GENMASK((p)->width ? (p)->width - 1 : 3, 0)
  39. # define PLL_ALPHA_MSB BIT(15)
  40. # define PLL_ALPHA_EN BIT(24)
  41. # define PLL_ALPHA_MODE BIT(25)
  42. # define PLL_VCO_SHIFT 20
  43. # define PLL_VCO_MASK 0x3
  44. #define PLL_USER_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U])
  45. #define PLL_USER_CTL_U1(p) ((p)->offset + (p)->regs[PLL_OFF_USER_CTL_U1])
  46. #define PLL_CONFIG_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL])
  47. #define PLL_CONFIG_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U])
  48. #define PLL_CONFIG_CTL_U1(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U1])
  49. #define PLL_CONFIG_CTL_U2(p) ((p)->offset + (p)->regs[PLL_OFF_CONFIG_CTL_U2])
  50. #define PLL_TEST_CTL(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL])
  51. #define PLL_TEST_CTL_U(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U])
  52. #define PLL_TEST_CTL_U1(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U1])
  53. #define PLL_TEST_CTL_U2(p) ((p)->offset + (p)->regs[PLL_OFF_TEST_CTL_U2])
  54. #define PLL_STATUS(p) ((p)->offset + (p)->regs[PLL_OFF_STATUS])
  55. #define PLL_OPMODE(p) ((p)->offset + (p)->regs[PLL_OFF_OPMODE])
  56. #define PLL_FRAC(p) ((p)->offset + (p)->regs[PLL_OFF_FRAC])
  57. const u8 clk_alpha_pll_regs[][PLL_OFF_MAX_REGS] = {
  58. [CLK_ALPHA_PLL_TYPE_DEFAULT] = {
  59. [PLL_OFF_L_VAL] = 0x04,
  60. [PLL_OFF_ALPHA_VAL] = 0x08,
  61. [PLL_OFF_ALPHA_VAL_U] = 0x0c,
  62. [PLL_OFF_USER_CTL] = 0x10,
  63. [PLL_OFF_USER_CTL_U] = 0x14,
  64. [PLL_OFF_CONFIG_CTL] = 0x18,
  65. [PLL_OFF_TEST_CTL] = 0x1c,
  66. [PLL_OFF_TEST_CTL_U] = 0x20,
  67. [PLL_OFF_STATUS] = 0x24,
  68. },
  69. [CLK_ALPHA_PLL_TYPE_HUAYRA] = {
  70. [PLL_OFF_L_VAL] = 0x04,
  71. [PLL_OFF_ALPHA_VAL] = 0x08,
  72. [PLL_OFF_USER_CTL] = 0x10,
  73. [PLL_OFF_CONFIG_CTL] = 0x14,
  74. [PLL_OFF_CONFIG_CTL_U] = 0x18,
  75. [PLL_OFF_TEST_CTL] = 0x1c,
  76. [PLL_OFF_TEST_CTL_U] = 0x20,
  77. [PLL_OFF_STATUS] = 0x24,
  78. },
  79. [CLK_ALPHA_PLL_TYPE_HUAYRA_APSS] = {
  80. [PLL_OFF_L_VAL] = 0x08,
  81. [PLL_OFF_ALPHA_VAL] = 0x10,
  82. [PLL_OFF_USER_CTL] = 0x18,
  83. [PLL_OFF_CONFIG_CTL] = 0x20,
  84. [PLL_OFF_CONFIG_CTL_U] = 0x24,
  85. [PLL_OFF_STATUS] = 0x28,
  86. [PLL_OFF_TEST_CTL] = 0x30,
  87. [PLL_OFF_TEST_CTL_U] = 0x34,
  88. },
  89. [CLK_ALPHA_PLL_TYPE_HUAYRA_2290] = {
  90. [PLL_OFF_L_VAL] = 0x04,
  91. [PLL_OFF_ALPHA_VAL] = 0x08,
  92. [PLL_OFF_USER_CTL] = 0x0c,
  93. [PLL_OFF_CONFIG_CTL] = 0x10,
  94. [PLL_OFF_CONFIG_CTL_U] = 0x14,
  95. [PLL_OFF_CONFIG_CTL_U1] = 0x18,
  96. [PLL_OFF_TEST_CTL] = 0x1c,
  97. [PLL_OFF_TEST_CTL_U] = 0x20,
  98. [PLL_OFF_TEST_CTL_U1] = 0x24,
  99. [PLL_OFF_OPMODE] = 0x28,
  100. [PLL_OFF_STATUS] = 0x38,
  101. },
  102. [CLK_ALPHA_PLL_TYPE_BRAMMO] = {
  103. [PLL_OFF_L_VAL] = 0x04,
  104. [PLL_OFF_ALPHA_VAL] = 0x08,
  105. [PLL_OFF_ALPHA_VAL_U] = 0x0c,
  106. [PLL_OFF_USER_CTL] = 0x10,
  107. [PLL_OFF_CONFIG_CTL] = 0x18,
  108. [PLL_OFF_TEST_CTL] = 0x1c,
  109. [PLL_OFF_STATUS] = 0x24,
  110. },
  111. [CLK_ALPHA_PLL_TYPE_FABIA] = {
  112. [PLL_OFF_L_VAL] = 0x04,
  113. [PLL_OFF_USER_CTL] = 0x0c,
  114. [PLL_OFF_USER_CTL_U] = 0x10,
  115. [PLL_OFF_CONFIG_CTL] = 0x14,
  116. [PLL_OFF_CONFIG_CTL_U] = 0x18,
  117. [PLL_OFF_TEST_CTL] = 0x1c,
  118. [PLL_OFF_TEST_CTL_U] = 0x20,
  119. [PLL_OFF_STATUS] = 0x24,
  120. [PLL_OFF_OPMODE] = 0x2c,
  121. [PLL_OFF_FRAC] = 0x38,
  122. },
  123. [CLK_ALPHA_PLL_TYPE_TRION] = {
  124. [PLL_OFF_L_VAL] = 0x04,
  125. [PLL_OFF_CAL_L_VAL] = 0x08,
  126. [PLL_OFF_USER_CTL] = 0x0c,
  127. [PLL_OFF_USER_CTL_U] = 0x10,
  128. [PLL_OFF_USER_CTL_U1] = 0x14,
  129. [PLL_OFF_CONFIG_CTL] = 0x18,
  130. [PLL_OFF_CONFIG_CTL_U] = 0x1c,
  131. [PLL_OFF_CONFIG_CTL_U1] = 0x20,
  132. [PLL_OFF_TEST_CTL] = 0x24,
  133. [PLL_OFF_TEST_CTL_U] = 0x28,
  134. [PLL_OFF_TEST_CTL_U1] = 0x2c,
  135. [PLL_OFF_STATUS] = 0x30,
  136. [PLL_OFF_OPMODE] = 0x38,
  137. [PLL_OFF_ALPHA_VAL] = 0x40,
  138. },
  139. [CLK_ALPHA_PLL_TYPE_AGERA] = {
  140. [PLL_OFF_L_VAL] = 0x04,
  141. [PLL_OFF_ALPHA_VAL] = 0x08,
  142. [PLL_OFF_USER_CTL] = 0x0c,
  143. [PLL_OFF_CONFIG_CTL] = 0x10,
  144. [PLL_OFF_CONFIG_CTL_U] = 0x14,
  145. [PLL_OFF_TEST_CTL] = 0x18,
  146. [PLL_OFF_TEST_CTL_U] = 0x1c,
  147. [PLL_OFF_STATUS] = 0x2c,
  148. },
  149. [CLK_ALPHA_PLL_TYPE_ZONDA] = {
  150. [PLL_OFF_L_VAL] = 0x04,
  151. [PLL_OFF_ALPHA_VAL] = 0x08,
  152. [PLL_OFF_USER_CTL] = 0x0c,
  153. [PLL_OFF_CONFIG_CTL] = 0x10,
  154. [PLL_OFF_CONFIG_CTL_U] = 0x14,
  155. [PLL_OFF_CONFIG_CTL_U1] = 0x18,
  156. [PLL_OFF_TEST_CTL] = 0x1c,
  157. [PLL_OFF_TEST_CTL_U] = 0x20,
  158. [PLL_OFF_TEST_CTL_U1] = 0x24,
  159. [PLL_OFF_OPMODE] = 0x28,
  160. [PLL_OFF_STATUS] = 0x38,
  161. },
  162. [CLK_ALPHA_PLL_TYPE_LUCID_EVO] = {
  163. [PLL_OFF_OPMODE] = 0x04,
  164. [PLL_OFF_STATUS] = 0x0c,
  165. [PLL_OFF_L_VAL] = 0x10,
  166. [PLL_OFF_ALPHA_VAL] = 0x14,
  167. [PLL_OFF_USER_CTL] = 0x18,
  168. [PLL_OFF_USER_CTL_U] = 0x1c,
  169. [PLL_OFF_CONFIG_CTL] = 0x20,
  170. [PLL_OFF_CONFIG_CTL_U] = 0x24,
  171. [PLL_OFF_CONFIG_CTL_U1] = 0x28,
  172. [PLL_OFF_TEST_CTL] = 0x2c,
  173. [PLL_OFF_TEST_CTL_U] = 0x30,
  174. [PLL_OFF_TEST_CTL_U1] = 0x34,
  175. },
  176. [CLK_ALPHA_PLL_TYPE_LUCID_OLE] = {
  177. [PLL_OFF_OPMODE] = 0x04,
  178. [PLL_OFF_STATE] = 0x08,
  179. [PLL_OFF_STATUS] = 0x0c,
  180. [PLL_OFF_L_VAL] = 0x10,
  181. [PLL_OFF_ALPHA_VAL] = 0x14,
  182. [PLL_OFF_USER_CTL] = 0x18,
  183. [PLL_OFF_USER_CTL_U] = 0x1c,
  184. [PLL_OFF_CONFIG_CTL] = 0x20,
  185. [PLL_OFF_CONFIG_CTL_U] = 0x24,
  186. [PLL_OFF_CONFIG_CTL_U1] = 0x28,
  187. [PLL_OFF_TEST_CTL] = 0x2c,
  188. [PLL_OFF_TEST_CTL_U] = 0x30,
  189. [PLL_OFF_TEST_CTL_U1] = 0x34,
  190. [PLL_OFF_TEST_CTL_U2] = 0x38,
  191. },
  192. [CLK_ALPHA_PLL_TYPE_RIVIAN_EVO] = {
  193. [PLL_OFF_OPMODE] = 0x04,
  194. [PLL_OFF_STATUS] = 0x0c,
  195. [PLL_OFF_L_VAL] = 0x10,
  196. [PLL_OFF_USER_CTL] = 0x14,
  197. [PLL_OFF_USER_CTL_U] = 0x18,
  198. [PLL_OFF_CONFIG_CTL] = 0x1c,
  199. [PLL_OFF_CONFIG_CTL_U] = 0x20,
  200. [PLL_OFF_CONFIG_CTL_U1] = 0x24,
  201. [PLL_OFF_TEST_CTL] = 0x28,
  202. [PLL_OFF_TEST_CTL_U] = 0x2c,
  203. },
  204. [CLK_ALPHA_PLL_TYPE_DEFAULT_EVO] = {
  205. [PLL_OFF_L_VAL] = 0x04,
  206. [PLL_OFF_ALPHA_VAL] = 0x08,
  207. [PLL_OFF_ALPHA_VAL_U] = 0x0c,
  208. [PLL_OFF_TEST_CTL] = 0x10,
  209. [PLL_OFF_TEST_CTL_U] = 0x14,
  210. [PLL_OFF_USER_CTL] = 0x18,
  211. [PLL_OFF_USER_CTL_U] = 0x1c,
  212. [PLL_OFF_CONFIG_CTL] = 0x20,
  213. [PLL_OFF_STATUS] = 0x24,
  214. },
  215. [CLK_ALPHA_PLL_TYPE_BRAMMO_EVO] = {
  216. [PLL_OFF_L_VAL] = 0x04,
  217. [PLL_OFF_ALPHA_VAL] = 0x08,
  218. [PLL_OFF_ALPHA_VAL_U] = 0x0c,
  219. [PLL_OFF_TEST_CTL] = 0x10,
  220. [PLL_OFF_TEST_CTL_U] = 0x14,
  221. [PLL_OFF_USER_CTL] = 0x18,
  222. [PLL_OFF_CONFIG_CTL] = 0x1C,
  223. [PLL_OFF_STATUS] = 0x20,
  224. },
  225. [CLK_ALPHA_PLL_TYPE_STROMER] = {
  226. [PLL_OFF_L_VAL] = 0x08,
  227. [PLL_OFF_ALPHA_VAL] = 0x10,
  228. [PLL_OFF_ALPHA_VAL_U] = 0x14,
  229. [PLL_OFF_USER_CTL] = 0x18,
  230. [PLL_OFF_USER_CTL_U] = 0x1c,
  231. [PLL_OFF_CONFIG_CTL] = 0x20,
  232. [PLL_OFF_STATUS] = 0x28,
  233. [PLL_OFF_TEST_CTL] = 0x30,
  234. [PLL_OFF_TEST_CTL_U] = 0x34,
  235. },
  236. [CLK_ALPHA_PLL_TYPE_STROMER_PLUS] = {
  237. [PLL_OFF_L_VAL] = 0x04,
  238. [PLL_OFF_USER_CTL] = 0x08,
  239. [PLL_OFF_USER_CTL_U] = 0x0c,
  240. [PLL_OFF_CONFIG_CTL] = 0x10,
  241. [PLL_OFF_TEST_CTL] = 0x14,
  242. [PLL_OFF_TEST_CTL_U] = 0x18,
  243. [PLL_OFF_STATUS] = 0x1c,
  244. [PLL_OFF_ALPHA_VAL] = 0x24,
  245. [PLL_OFF_ALPHA_VAL_U] = 0x28,
  246. },
  247. [CLK_ALPHA_PLL_TYPE_ZONDA_OLE] = {
  248. [PLL_OFF_L_VAL] = 0x04,
  249. [PLL_OFF_ALPHA_VAL] = 0x08,
  250. [PLL_OFF_USER_CTL] = 0x0c,
  251. [PLL_OFF_USER_CTL_U] = 0x10,
  252. [PLL_OFF_CONFIG_CTL] = 0x14,
  253. [PLL_OFF_CONFIG_CTL_U] = 0x18,
  254. [PLL_OFF_CONFIG_CTL_U1] = 0x1c,
  255. [PLL_OFF_CONFIG_CTL_U2] = 0x20,
  256. [PLL_OFF_TEST_CTL] = 0x24,
  257. [PLL_OFF_TEST_CTL_U] = 0x28,
  258. [PLL_OFF_TEST_CTL_U1] = 0x2c,
  259. [PLL_OFF_OPMODE] = 0x30,
  260. [PLL_OFF_STATUS] = 0x3c,
  261. },
  262. [CLK_ALPHA_PLL_TYPE_NSS_HUAYRA] = {
  263. [PLL_OFF_L_VAL] = 0x04,
  264. [PLL_OFF_ALPHA_VAL] = 0x08,
  265. [PLL_OFF_TEST_CTL] = 0x0c,
  266. [PLL_OFF_TEST_CTL_U] = 0x10,
  267. [PLL_OFF_USER_CTL] = 0x14,
  268. [PLL_OFF_CONFIG_CTL] = 0x18,
  269. [PLL_OFF_CONFIG_CTL_U] = 0x1c,
  270. [PLL_OFF_STATUS] = 0x20,
  271. },
  272. };
  273. EXPORT_SYMBOL_GPL(clk_alpha_pll_regs);
  274. /*
  275. * Even though 40 bits are present, use only 32 for ease of calculation.
  276. */
  277. #define ALPHA_REG_BITWIDTH 40
  278. #define ALPHA_REG_16BIT_WIDTH 16
  279. #define ALPHA_BITWIDTH 32U
  280. #define ALPHA_SHIFT(w) min(w, ALPHA_BITWIDTH)
  281. #define ALPHA_PLL_STATUS_REG_SHIFT 8
  282. #define PLL_HUAYRA_M_WIDTH 8
  283. #define PLL_HUAYRA_M_SHIFT 8
  284. #define PLL_HUAYRA_M_MASK 0xff
  285. #define PLL_HUAYRA_N_SHIFT 0
  286. #define PLL_HUAYRA_N_MASK 0xff
  287. #define PLL_HUAYRA_ALPHA_WIDTH 16
  288. #define PLL_STANDBY 0x0
  289. #define PLL_RUN 0x1
  290. #define PLL_OUT_MASK 0x7
  291. #define PLL_RATE_MARGIN 500
  292. /* TRION PLL specific settings and offsets */
  293. #define TRION_PLL_CAL_VAL 0x44
  294. #define TRION_PCAL_DONE BIT(26)
  295. /* LUCID PLL specific settings and offsets */
  296. #define LUCID_PCAL_DONE BIT(27)
  297. /* LUCID 5LPE PLL specific settings and offsets */
  298. #define LUCID_5LPE_PCAL_DONE BIT(11)
  299. #define LUCID_5LPE_ALPHA_PLL_ACK_LATCH BIT(13)
  300. #define LUCID_5LPE_PLL_LATCH_INPUT BIT(14)
  301. #define LUCID_5LPE_ENABLE_VOTE_RUN BIT(21)
  302. /* LUCID EVO PLL specific settings and offsets */
  303. #define LUCID_EVO_PCAL_NOT_DONE BIT(8)
  304. #define LUCID_EVO_ENABLE_VOTE_RUN BIT(25)
  305. #define LUCID_EVO_PLL_L_VAL_MASK GENMASK(15, 0)
  306. #define LUCID_EVO_PLL_CAL_L_VAL_SHIFT 16
  307. #define LUCID_OLE_PLL_RINGOSC_CAL_L_VAL_SHIFT 24
  308. /* ZONDA PLL specific */
  309. #define ZONDA_PLL_OUT_MASK 0xf
  310. #define ZONDA_STAY_IN_CFA BIT(16)
  311. #define ZONDA_PLL_FREQ_LOCK_DET BIT(29)
  312. #define pll_alpha_width(p) \
  313. ((PLL_ALPHA_VAL_U(p) - PLL_ALPHA_VAL(p) == 4) ? \
  314. ALPHA_REG_BITWIDTH : ALPHA_REG_16BIT_WIDTH)
  315. #define pll_has_64bit_config(p) ((PLL_CONFIG_CTL_U(p) - PLL_CONFIG_CTL(p)) == 4)
  316. #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
  317. struct clk_alpha_pll, clkr)
  318. #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
  319. struct clk_alpha_pll_postdiv, clkr)
  320. static int wait_for_pll(struct clk_alpha_pll *pll, u32 mask, bool inverse,
  321. const char *action)
  322. {
  323. u32 val;
  324. int count;
  325. int ret;
  326. const char *name = clk_hw_get_name(&pll->clkr.hw);
  327. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  328. if (ret)
  329. return ret;
  330. for (count = 200; count > 0; count--) {
  331. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  332. if (ret)
  333. return ret;
  334. if (inverse && !(val & mask))
  335. return 0;
  336. else if ((val & mask) == mask)
  337. return 0;
  338. udelay(1);
  339. }
  340. WARN(1, "%s failed to %s!\n", name, action);
  341. return -ETIMEDOUT;
  342. }
  343. #define wait_for_pll_enable_active(pll) \
  344. wait_for_pll(pll, PLL_ACTIVE_FLAG, 0, "enable")
  345. #define wait_for_pll_enable_lock(pll) \
  346. wait_for_pll(pll, PLL_LOCK_DET, 0, "enable")
  347. #define wait_for_zonda_pll_freq_lock(pll) \
  348. wait_for_pll(pll, ZONDA_PLL_FREQ_LOCK_DET, 0, "freq enable")
  349. #define wait_for_pll_disable(pll) \
  350. wait_for_pll(pll, PLL_ACTIVE_FLAG, 1, "disable")
  351. #define wait_for_pll_offline(pll) \
  352. wait_for_pll(pll, PLL_OFFLINE_ACK, 0, "offline")
  353. #define wait_for_pll_update(pll) \
  354. wait_for_pll(pll, PLL_UPDATE, 1, "update")
  355. #define wait_for_pll_update_ack_set(pll) \
  356. wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 0, "update_ack_set")
  357. #define wait_for_pll_update_ack_clear(pll) \
  358. wait_for_pll(pll, ALPHA_PLL_ACK_LATCH, 1, "update_ack_clear")
  359. static void clk_alpha_pll_write_config(struct regmap *regmap, unsigned int reg,
  360. unsigned int val)
  361. {
  362. if (val)
  363. regmap_write(regmap, reg, val);
  364. }
  365. void clk_alpha_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  366. const struct alpha_pll_config *config)
  367. {
  368. u32 val, mask;
  369. regmap_write(regmap, PLL_L_VAL(pll), config->l);
  370. regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  371. regmap_write(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  372. if (pll_has_64bit_config(pll))
  373. regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
  374. config->config_ctl_hi_val);
  375. if (pll_alpha_width(pll) > 32)
  376. regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
  377. val = config->main_output_mask;
  378. val |= config->aux_output_mask;
  379. val |= config->aux2_output_mask;
  380. val |= config->early_output_mask;
  381. val |= config->pre_div_val;
  382. val |= config->post_div_val;
  383. val |= config->vco_val;
  384. val |= config->alpha_en_mask;
  385. val |= config->alpha_mode_mask;
  386. mask = config->main_output_mask;
  387. mask |= config->aux_output_mask;
  388. mask |= config->aux2_output_mask;
  389. mask |= config->early_output_mask;
  390. mask |= config->pre_div_mask;
  391. mask |= config->post_div_mask;
  392. mask |= config->vco_mask;
  393. mask |= config->alpha_en_mask;
  394. mask |= config->alpha_mode_mask;
  395. regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
  396. if (config->test_ctl_mask)
  397. regmap_update_bits(regmap, PLL_TEST_CTL(pll),
  398. config->test_ctl_mask,
  399. config->test_ctl_val);
  400. else
  401. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
  402. config->test_ctl_val);
  403. if (config->test_ctl_hi_mask)
  404. regmap_update_bits(regmap, PLL_TEST_CTL_U(pll),
  405. config->test_ctl_hi_mask,
  406. config->test_ctl_hi_val);
  407. else
  408. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
  409. config->test_ctl_hi_val);
  410. if (pll->flags & SUPPORTS_FSM_MODE)
  411. qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
  412. }
  413. EXPORT_SYMBOL_GPL(clk_alpha_pll_configure);
  414. static int clk_alpha_pll_hwfsm_enable(struct clk_hw *hw)
  415. {
  416. int ret;
  417. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  418. u32 val;
  419. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  420. if (ret)
  421. return ret;
  422. val |= PLL_FSM_ENA;
  423. if (pll->flags & SUPPORTS_OFFLINE_REQ)
  424. val &= ~PLL_OFFLINE_REQ;
  425. ret = regmap_write(pll->clkr.regmap, PLL_MODE(pll), val);
  426. if (ret)
  427. return ret;
  428. /* Make sure enable request goes through before waiting for update */
  429. mb();
  430. return wait_for_pll_enable_active(pll);
  431. }
  432. static void clk_alpha_pll_hwfsm_disable(struct clk_hw *hw)
  433. {
  434. int ret;
  435. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  436. u32 val;
  437. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  438. if (ret)
  439. return;
  440. if (pll->flags & SUPPORTS_OFFLINE_REQ) {
  441. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  442. PLL_OFFLINE_REQ, PLL_OFFLINE_REQ);
  443. if (ret)
  444. return;
  445. ret = wait_for_pll_offline(pll);
  446. if (ret)
  447. return;
  448. }
  449. /* Disable hwfsm */
  450. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  451. PLL_FSM_ENA, 0);
  452. if (ret)
  453. return;
  454. wait_for_pll_disable(pll);
  455. }
  456. static int pll_is_enabled(struct clk_hw *hw, u32 mask)
  457. {
  458. int ret;
  459. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  460. u32 val;
  461. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  462. if (ret)
  463. return ret;
  464. return !!(val & mask);
  465. }
  466. static int clk_alpha_pll_hwfsm_is_enabled(struct clk_hw *hw)
  467. {
  468. return pll_is_enabled(hw, PLL_ACTIVE_FLAG);
  469. }
  470. static int clk_alpha_pll_is_enabled(struct clk_hw *hw)
  471. {
  472. return pll_is_enabled(hw, PLL_LOCK_DET);
  473. }
  474. static int clk_alpha_pll_enable(struct clk_hw *hw)
  475. {
  476. int ret;
  477. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  478. u32 val, mask;
  479. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  480. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  481. if (ret)
  482. return ret;
  483. /* If in FSM mode, just vote for it */
  484. if (val & PLL_VOTE_FSM_ENA) {
  485. ret = clk_enable_regmap(hw);
  486. if (ret)
  487. return ret;
  488. return wait_for_pll_enable_active(pll);
  489. }
  490. /* Skip if already enabled */
  491. if ((val & mask) == mask)
  492. return 0;
  493. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  494. PLL_BYPASSNL, PLL_BYPASSNL);
  495. if (ret)
  496. return ret;
  497. /*
  498. * H/W requires a 5us delay between disabling the bypass and
  499. * de-asserting the reset.
  500. */
  501. mb();
  502. udelay(5);
  503. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  504. PLL_RESET_N, PLL_RESET_N);
  505. if (ret)
  506. return ret;
  507. ret = wait_for_pll_enable_lock(pll);
  508. if (ret)
  509. return ret;
  510. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll),
  511. PLL_OUTCTRL, PLL_OUTCTRL);
  512. /* Ensure that the write above goes through before returning. */
  513. mb();
  514. return ret;
  515. }
  516. static void clk_alpha_pll_disable(struct clk_hw *hw)
  517. {
  518. int ret;
  519. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  520. u32 val, mask;
  521. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  522. if (ret)
  523. return;
  524. /* If in FSM mode, just unvote it */
  525. if (val & PLL_VOTE_FSM_ENA) {
  526. clk_disable_regmap(hw);
  527. return;
  528. }
  529. mask = PLL_OUTCTRL;
  530. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
  531. /* Delay of 2 output clock ticks required until output is disabled */
  532. mb();
  533. udelay(1);
  534. mask = PLL_RESET_N | PLL_BYPASSNL;
  535. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), mask, 0);
  536. }
  537. static unsigned long
  538. alpha_pll_calc_rate(u64 prate, u32 l, u32 a, u32 alpha_width)
  539. {
  540. return (prate * l) + ((prate * a) >> ALPHA_SHIFT(alpha_width));
  541. }
  542. static unsigned long
  543. alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a,
  544. u32 alpha_width)
  545. {
  546. u64 remainder;
  547. u64 quotient;
  548. quotient = rate;
  549. remainder = do_div(quotient, prate);
  550. *l = quotient;
  551. if (!remainder) {
  552. *a = 0;
  553. return rate;
  554. }
  555. /* Upper ALPHA_BITWIDTH bits of Alpha */
  556. quotient = remainder << ALPHA_SHIFT(alpha_width);
  557. remainder = do_div(quotient, prate);
  558. if (remainder)
  559. quotient++;
  560. *a = quotient;
  561. return alpha_pll_calc_rate(prate, *l, *a, alpha_width);
  562. }
  563. static const struct pll_vco *
  564. alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
  565. {
  566. const struct pll_vco *v = pll->vco_table;
  567. const struct pll_vco *end = v + pll->num_vco;
  568. for (; v < end; v++)
  569. if (rate >= v->min_freq && rate <= v->max_freq)
  570. return v;
  571. return NULL;
  572. }
  573. static unsigned long
  574. clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  575. {
  576. u32 l, low, high, ctl;
  577. u64 a = 0, prate = parent_rate;
  578. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  579. u32 alpha_width = pll_alpha_width(pll);
  580. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  581. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  582. if (ctl & PLL_ALPHA_EN) {
  583. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &low);
  584. if (alpha_width > 32) {
  585. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
  586. &high);
  587. a = (u64)high << 32 | low;
  588. } else {
  589. a = low & GENMASK(alpha_width - 1, 0);
  590. }
  591. if (alpha_width > ALPHA_BITWIDTH)
  592. a >>= alpha_width - ALPHA_BITWIDTH;
  593. }
  594. return alpha_pll_calc_rate(prate, l, a, alpha_width);
  595. }
  596. static int __clk_alpha_pll_update_latch(struct clk_alpha_pll *pll)
  597. {
  598. int ret;
  599. u32 mode;
  600. regmap_read(pll->clkr.regmap, PLL_MODE(pll), &mode);
  601. /* Latch the input to the PLL */
  602. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
  603. PLL_UPDATE);
  604. /* Wait for 2 reference cycle before checking ACK bit */
  605. udelay(1);
  606. /*
  607. * PLL will latch the new L, Alpha and freq control word.
  608. * PLL will respond by raising PLL_ACK_LATCH output when new programming
  609. * has been latched in and PLL is being updated. When
  610. * UPDATE_LOGIC_BYPASS bit is not set, PLL_UPDATE will be cleared
  611. * automatically by hardware when PLL_ACK_LATCH is asserted by PLL.
  612. */
  613. if (mode & PLL_UPDATE_BYPASS) {
  614. ret = wait_for_pll_update_ack_set(pll);
  615. if (ret)
  616. return ret;
  617. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE, 0);
  618. } else {
  619. ret = wait_for_pll_update(pll);
  620. if (ret)
  621. return ret;
  622. }
  623. ret = wait_for_pll_update_ack_clear(pll);
  624. if (ret)
  625. return ret;
  626. /* Wait for PLL output to stabilize */
  627. udelay(10);
  628. return 0;
  629. }
  630. static int clk_alpha_pll_update_latch(struct clk_alpha_pll *pll,
  631. int (*is_enabled)(struct clk_hw *))
  632. {
  633. if (!is_enabled(&pll->clkr.hw) ||
  634. !(pll->flags & SUPPORTS_DYNAMIC_UPDATE))
  635. return 0;
  636. return __clk_alpha_pll_update_latch(pll);
  637. }
  638. static int __clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  639. unsigned long prate,
  640. int (*is_enabled)(struct clk_hw *))
  641. {
  642. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  643. const struct pll_vco *vco;
  644. u32 l, alpha_width = pll_alpha_width(pll);
  645. u64 a;
  646. rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  647. vco = alpha_pll_find_vco(pll, rate);
  648. if (pll->vco_table && !vco) {
  649. pr_err("%s: alpha pll not in a valid vco range\n",
  650. clk_hw_get_name(hw));
  651. return -EINVAL;
  652. }
  653. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  654. if (alpha_width > ALPHA_BITWIDTH)
  655. a <<= alpha_width - ALPHA_BITWIDTH;
  656. if (alpha_width > 32)
  657. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll), a >> 32);
  658. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  659. if (vco) {
  660. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  661. PLL_VCO_MASK << PLL_VCO_SHIFT,
  662. vco->val << PLL_VCO_SHIFT);
  663. }
  664. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  665. PLL_ALPHA_EN, PLL_ALPHA_EN);
  666. return clk_alpha_pll_update_latch(pll, is_enabled);
  667. }
  668. static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  669. unsigned long prate)
  670. {
  671. return __clk_alpha_pll_set_rate(hw, rate, prate,
  672. clk_alpha_pll_is_enabled);
  673. }
  674. static int clk_alpha_pll_hwfsm_set_rate(struct clk_hw *hw, unsigned long rate,
  675. unsigned long prate)
  676. {
  677. return __clk_alpha_pll_set_rate(hw, rate, prate,
  678. clk_alpha_pll_hwfsm_is_enabled);
  679. }
  680. static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  681. unsigned long *prate)
  682. {
  683. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  684. u32 l, alpha_width = pll_alpha_width(pll);
  685. u64 a;
  686. unsigned long min_freq, max_freq;
  687. rate = alpha_pll_round_rate(rate, *prate, &l, &a, alpha_width);
  688. if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
  689. return rate;
  690. min_freq = pll->vco_table[0].min_freq;
  691. max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
  692. return clamp(rate, min_freq, max_freq);
  693. }
  694. void clk_huayra_2290_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  695. const struct alpha_pll_config *config)
  696. {
  697. u32 val;
  698. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  699. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
  700. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
  701. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
  702. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
  703. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll), config->test_ctl_hi1_val);
  704. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
  705. clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  706. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
  707. /* Set PLL_BYPASSNL */
  708. regmap_update_bits(regmap, PLL_MODE(pll), PLL_BYPASSNL, PLL_BYPASSNL);
  709. regmap_read(regmap, PLL_MODE(pll), &val);
  710. /* Wait 5 us between setting BYPASS and deasserting reset */
  711. udelay(5);
  712. /* Take PLL out from reset state */
  713. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  714. regmap_read(regmap, PLL_MODE(pll), &val);
  715. /* Wait 50us for PLL_LOCK_DET bit to go high */
  716. usleep_range(50, 55);
  717. /* Enable PLL output */
  718. regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, PLL_OUTCTRL);
  719. }
  720. EXPORT_SYMBOL_GPL(clk_huayra_2290_pll_configure);
  721. static unsigned long
  722. alpha_huayra_pll_calc_rate(u64 prate, u32 l, u32 a)
  723. {
  724. /*
  725. * a contains 16 bit alpha_val in two’s complement number in the range
  726. * of [-0.5, 0.5).
  727. */
  728. if (a >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
  729. l -= 1;
  730. return (prate * l) + (prate * a >> PLL_HUAYRA_ALPHA_WIDTH);
  731. }
  732. static unsigned long
  733. alpha_huayra_pll_round_rate(unsigned long rate, unsigned long prate,
  734. u32 *l, u32 *a)
  735. {
  736. u64 remainder;
  737. u64 quotient;
  738. quotient = rate;
  739. remainder = do_div(quotient, prate);
  740. *l = quotient;
  741. if (!remainder) {
  742. *a = 0;
  743. return rate;
  744. }
  745. quotient = remainder << PLL_HUAYRA_ALPHA_WIDTH;
  746. remainder = do_div(quotient, prate);
  747. if (remainder)
  748. quotient++;
  749. /*
  750. * alpha_val should be in two’s complement number in the range
  751. * of [-0.5, 0.5) so if quotient >= 0.5 then increment the l value
  752. * since alpha value will be subtracted in this case.
  753. */
  754. if (quotient >= BIT(PLL_HUAYRA_ALPHA_WIDTH - 1))
  755. *l += 1;
  756. *a = quotient;
  757. return alpha_huayra_pll_calc_rate(prate, *l, *a);
  758. }
  759. static unsigned long
  760. alpha_pll_huayra_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  761. {
  762. u64 rate = parent_rate, tmp;
  763. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  764. u32 l, alpha = 0, ctl, alpha_m, alpha_n;
  765. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  766. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  767. if (ctl & PLL_ALPHA_EN) {
  768. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &alpha);
  769. /*
  770. * Depending upon alpha_mode, it can be treated as M/N value or
  771. * as a two’s complement number. When alpha_mode=1,
  772. * pll_alpha_val<15:8>=M and pll_apla_val<7:0>=N
  773. *
  774. * Fout=FIN*(L+(M/N))
  775. *
  776. * M is a signed number (-128 to 127) and N is unsigned
  777. * (0 to 255). M/N has to be within +/-0.5.
  778. *
  779. * When alpha_mode=0, it is a two’s complement number in the
  780. * range [-0.5, 0.5).
  781. *
  782. * Fout=FIN*(L+(alpha_val)/2^16)
  783. *
  784. * where alpha_val is two’s complement number.
  785. */
  786. if (!(ctl & PLL_ALPHA_MODE))
  787. return alpha_huayra_pll_calc_rate(rate, l, alpha);
  788. alpha_m = alpha >> PLL_HUAYRA_M_SHIFT & PLL_HUAYRA_M_MASK;
  789. alpha_n = alpha >> PLL_HUAYRA_N_SHIFT & PLL_HUAYRA_N_MASK;
  790. rate *= l;
  791. tmp = parent_rate;
  792. if (alpha_m >= BIT(PLL_HUAYRA_M_WIDTH - 1)) {
  793. alpha_m = BIT(PLL_HUAYRA_M_WIDTH) - alpha_m;
  794. tmp *= alpha_m;
  795. do_div(tmp, alpha_n);
  796. rate -= tmp;
  797. } else {
  798. tmp *= alpha_m;
  799. do_div(tmp, alpha_n);
  800. rate += tmp;
  801. }
  802. return rate;
  803. }
  804. return alpha_huayra_pll_calc_rate(rate, l, alpha);
  805. }
  806. static int alpha_pll_huayra_set_rate(struct clk_hw *hw, unsigned long rate,
  807. unsigned long prate)
  808. {
  809. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  810. u32 l, a, ctl, cur_alpha = 0;
  811. rate = alpha_huayra_pll_round_rate(rate, prate, &l, &a);
  812. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  813. if (ctl & PLL_ALPHA_EN)
  814. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &cur_alpha);
  815. /*
  816. * Huayra PLL supports PLL dynamic programming. User can change L_VAL,
  817. * without having to go through the power on sequence.
  818. */
  819. if (clk_alpha_pll_is_enabled(hw)) {
  820. if (cur_alpha != a) {
  821. pr_err("%s: clock needs to be gated\n",
  822. clk_hw_get_name(hw));
  823. return -EBUSY;
  824. }
  825. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  826. /* Ensure that the write above goes to detect L val change. */
  827. mb();
  828. return wait_for_pll_enable_lock(pll);
  829. }
  830. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  831. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  832. if (a == 0)
  833. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  834. PLL_ALPHA_EN, 0x0);
  835. else
  836. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  837. PLL_ALPHA_EN | PLL_ALPHA_MODE, PLL_ALPHA_EN);
  838. return 0;
  839. }
  840. static long alpha_pll_huayra_round_rate(struct clk_hw *hw, unsigned long rate,
  841. unsigned long *prate)
  842. {
  843. u32 l, a;
  844. return alpha_huayra_pll_round_rate(rate, *prate, &l, &a);
  845. }
  846. static int trion_pll_is_enabled(struct clk_alpha_pll *pll,
  847. struct regmap *regmap)
  848. {
  849. u32 mode_val, opmode_val;
  850. int ret;
  851. ret = regmap_read(regmap, PLL_MODE(pll), &mode_val);
  852. ret |= regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
  853. if (ret)
  854. return 0;
  855. return ((opmode_val & PLL_RUN) && (mode_val & PLL_OUTCTRL));
  856. }
  857. static int clk_trion_pll_is_enabled(struct clk_hw *hw)
  858. {
  859. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  860. return trion_pll_is_enabled(pll, pll->clkr.regmap);
  861. }
  862. static int clk_trion_pll_enable(struct clk_hw *hw)
  863. {
  864. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  865. struct regmap *regmap = pll->clkr.regmap;
  866. u32 val;
  867. int ret;
  868. ret = regmap_read(regmap, PLL_MODE(pll), &val);
  869. if (ret)
  870. return ret;
  871. /* If in FSM mode, just vote for it */
  872. if (val & PLL_VOTE_FSM_ENA) {
  873. ret = clk_enable_regmap(hw);
  874. if (ret)
  875. return ret;
  876. return wait_for_pll_enable_active(pll);
  877. }
  878. /* Set operation mode to RUN */
  879. regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
  880. ret = wait_for_pll_enable_lock(pll);
  881. if (ret)
  882. return ret;
  883. /* Enable the PLL outputs */
  884. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
  885. PLL_OUT_MASK, PLL_OUT_MASK);
  886. if (ret)
  887. return ret;
  888. /* Enable the global PLL outputs */
  889. return regmap_update_bits(regmap, PLL_MODE(pll),
  890. PLL_OUTCTRL, PLL_OUTCTRL);
  891. }
  892. static void clk_trion_pll_disable(struct clk_hw *hw)
  893. {
  894. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  895. struct regmap *regmap = pll->clkr.regmap;
  896. u32 val;
  897. int ret;
  898. ret = regmap_read(regmap, PLL_MODE(pll), &val);
  899. if (ret)
  900. return;
  901. /* If in FSM mode, just unvote it */
  902. if (val & PLL_VOTE_FSM_ENA) {
  903. clk_disable_regmap(hw);
  904. return;
  905. }
  906. /* Disable the global PLL output */
  907. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  908. if (ret)
  909. return;
  910. /* Disable the PLL outputs */
  911. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
  912. PLL_OUT_MASK, 0);
  913. if (ret)
  914. return;
  915. /* Place the PLL mode in STANDBY */
  916. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  917. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  918. }
  919. static unsigned long
  920. clk_trion_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  921. {
  922. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  923. u32 l, frac, alpha_width = pll_alpha_width(pll);
  924. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  925. regmap_read(pll->clkr.regmap, PLL_ALPHA_VAL(pll), &frac);
  926. return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
  927. }
  928. const struct clk_ops clk_alpha_pll_fixed_ops = {
  929. .enable = clk_alpha_pll_enable,
  930. .disable = clk_alpha_pll_disable,
  931. .is_enabled = clk_alpha_pll_is_enabled,
  932. .recalc_rate = clk_alpha_pll_recalc_rate,
  933. };
  934. EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_ops);
  935. const struct clk_ops clk_alpha_pll_ops = {
  936. .enable = clk_alpha_pll_enable,
  937. .disable = clk_alpha_pll_disable,
  938. .is_enabled = clk_alpha_pll_is_enabled,
  939. .recalc_rate = clk_alpha_pll_recalc_rate,
  940. .round_rate = clk_alpha_pll_round_rate,
  941. .set_rate = clk_alpha_pll_set_rate,
  942. };
  943. EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
  944. const struct clk_ops clk_alpha_pll_huayra_ops = {
  945. .enable = clk_alpha_pll_enable,
  946. .disable = clk_alpha_pll_disable,
  947. .is_enabled = clk_alpha_pll_is_enabled,
  948. .recalc_rate = alpha_pll_huayra_recalc_rate,
  949. .round_rate = alpha_pll_huayra_round_rate,
  950. .set_rate = alpha_pll_huayra_set_rate,
  951. };
  952. EXPORT_SYMBOL_GPL(clk_alpha_pll_huayra_ops);
  953. const struct clk_ops clk_alpha_pll_hwfsm_ops = {
  954. .enable = clk_alpha_pll_hwfsm_enable,
  955. .disable = clk_alpha_pll_hwfsm_disable,
  956. .is_enabled = clk_alpha_pll_hwfsm_is_enabled,
  957. .recalc_rate = clk_alpha_pll_recalc_rate,
  958. .round_rate = clk_alpha_pll_round_rate,
  959. .set_rate = clk_alpha_pll_hwfsm_set_rate,
  960. };
  961. EXPORT_SYMBOL_GPL(clk_alpha_pll_hwfsm_ops);
  962. const struct clk_ops clk_alpha_pll_fixed_trion_ops = {
  963. .enable = clk_trion_pll_enable,
  964. .disable = clk_trion_pll_disable,
  965. .is_enabled = clk_trion_pll_is_enabled,
  966. .recalc_rate = clk_trion_pll_recalc_rate,
  967. .round_rate = clk_alpha_pll_round_rate,
  968. };
  969. EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_trion_ops);
  970. static unsigned long
  971. clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  972. {
  973. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  974. u32 ctl;
  975. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  976. ctl >>= PLL_POST_DIV_SHIFT;
  977. ctl &= PLL_POST_DIV_MASK(pll);
  978. return parent_rate >> fls(ctl);
  979. }
  980. static const struct clk_div_table clk_alpha_div_table[] = {
  981. { 0x0, 1 },
  982. { 0x1, 2 },
  983. { 0x3, 4 },
  984. { 0x7, 8 },
  985. { 0xf, 16 },
  986. { }
  987. };
  988. static const struct clk_div_table clk_alpha_2bit_div_table[] = {
  989. { 0x0, 1 },
  990. { 0x1, 2 },
  991. { 0x3, 4 },
  992. { }
  993. };
  994. static long
  995. clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  996. unsigned long *prate)
  997. {
  998. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  999. const struct clk_div_table *table;
  1000. if (pll->width == 2)
  1001. table = clk_alpha_2bit_div_table;
  1002. else
  1003. table = clk_alpha_div_table;
  1004. return divider_round_rate(hw, rate, prate, table,
  1005. pll->width, CLK_DIVIDER_POWER_OF_TWO);
  1006. }
  1007. static long
  1008. clk_alpha_pll_postdiv_round_ro_rate(struct clk_hw *hw, unsigned long rate,
  1009. unsigned long *prate)
  1010. {
  1011. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1012. u32 ctl, div;
  1013. regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &ctl);
  1014. ctl >>= PLL_POST_DIV_SHIFT;
  1015. ctl &= BIT(pll->width) - 1;
  1016. div = 1 << fls(ctl);
  1017. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
  1018. *prate = clk_hw_round_rate(clk_hw_get_parent(hw), div * rate);
  1019. return DIV_ROUND_UP_ULL((u64)*prate, div);
  1020. }
  1021. static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  1022. unsigned long parent_rate)
  1023. {
  1024. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1025. int div;
  1026. /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
  1027. div = DIV_ROUND_UP_ULL(parent_rate, rate) - 1;
  1028. return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  1029. PLL_POST_DIV_MASK(pll) << PLL_POST_DIV_SHIFT,
  1030. div << PLL_POST_DIV_SHIFT);
  1031. }
  1032. const struct clk_ops clk_alpha_pll_postdiv_ops = {
  1033. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  1034. .round_rate = clk_alpha_pll_postdiv_round_rate,
  1035. .set_rate = clk_alpha_pll_postdiv_set_rate,
  1036. };
  1037. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);
  1038. const struct clk_ops clk_alpha_pll_postdiv_ro_ops = {
  1039. .round_rate = clk_alpha_pll_postdiv_round_ro_rate,
  1040. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  1041. };
  1042. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ro_ops);
  1043. void clk_fabia_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  1044. const struct alpha_pll_config *config)
  1045. {
  1046. u32 val, mask;
  1047. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
  1048. clk_alpha_pll_write_config(regmap, PLL_FRAC(pll), config->alpha);
  1049. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
  1050. config->config_ctl_val);
  1051. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
  1052. config->config_ctl_hi_val);
  1053. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
  1054. config->user_ctl_val);
  1055. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll),
  1056. config->user_ctl_hi_val);
  1057. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
  1058. config->test_ctl_val);
  1059. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
  1060. config->test_ctl_hi_val);
  1061. if (config->post_div_mask) {
  1062. mask = config->post_div_mask;
  1063. val = config->post_div_val;
  1064. regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
  1065. }
  1066. if (pll->flags & SUPPORTS_FSM_LEGACY_MODE)
  1067. regmap_update_bits(regmap, PLL_MODE(pll), PLL_FSM_LEGACY_MODE,
  1068. PLL_FSM_LEGACY_MODE);
  1069. regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
  1070. PLL_UPDATE_BYPASS);
  1071. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1072. }
  1073. EXPORT_SYMBOL_GPL(clk_fabia_pll_configure);
  1074. static int alpha_pll_fabia_enable(struct clk_hw *hw)
  1075. {
  1076. int ret;
  1077. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1078. u32 val, opmode_val;
  1079. struct regmap *regmap = pll->clkr.regmap;
  1080. ret = regmap_read(regmap, PLL_MODE(pll), &val);
  1081. if (ret)
  1082. return ret;
  1083. /* If in FSM mode, just vote for it */
  1084. if (val & PLL_VOTE_FSM_ENA) {
  1085. ret = clk_enable_regmap(hw);
  1086. if (ret)
  1087. return ret;
  1088. return wait_for_pll_enable_active(pll);
  1089. }
  1090. ret = regmap_read(regmap, PLL_OPMODE(pll), &opmode_val);
  1091. if (ret)
  1092. return ret;
  1093. /* Skip If PLL is already running */
  1094. if ((opmode_val & PLL_RUN) && (val & PLL_OUTCTRL))
  1095. return 0;
  1096. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1097. if (ret)
  1098. return ret;
  1099. ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1100. if (ret)
  1101. return ret;
  1102. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N,
  1103. PLL_RESET_N);
  1104. if (ret)
  1105. return ret;
  1106. ret = regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
  1107. if (ret)
  1108. return ret;
  1109. ret = wait_for_pll_enable_lock(pll);
  1110. if (ret)
  1111. return ret;
  1112. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll),
  1113. PLL_OUT_MASK, PLL_OUT_MASK);
  1114. if (ret)
  1115. return ret;
  1116. return regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL,
  1117. PLL_OUTCTRL);
  1118. }
  1119. static void alpha_pll_fabia_disable(struct clk_hw *hw)
  1120. {
  1121. int ret;
  1122. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1123. u32 val;
  1124. struct regmap *regmap = pll->clkr.regmap;
  1125. ret = regmap_read(regmap, PLL_MODE(pll), &val);
  1126. if (ret)
  1127. return;
  1128. /* If in FSM mode, just unvote it */
  1129. if (val & PLL_FSM_ENA) {
  1130. clk_disable_regmap(hw);
  1131. return;
  1132. }
  1133. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1134. if (ret)
  1135. return;
  1136. /* Disable main outputs */
  1137. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
  1138. if (ret)
  1139. return;
  1140. /* Place the PLL in STANDBY */
  1141. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1142. }
  1143. static unsigned long alpha_pll_fabia_recalc_rate(struct clk_hw *hw,
  1144. unsigned long parent_rate)
  1145. {
  1146. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1147. u32 l, frac, alpha_width = pll_alpha_width(pll);
  1148. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  1149. regmap_read(pll->clkr.regmap, PLL_FRAC(pll), &frac);
  1150. return alpha_pll_calc_rate(parent_rate, l, frac, alpha_width);
  1151. }
  1152. /*
  1153. * Due to limited number of bits for fractional rate programming, the
  1154. * rounded up rate could be marginally higher than the requested rate.
  1155. */
  1156. static int alpha_pll_check_rate_margin(struct clk_hw *hw,
  1157. unsigned long rrate, unsigned long rate)
  1158. {
  1159. unsigned long rate_margin = rate + PLL_RATE_MARGIN;
  1160. if (rrate > rate_margin || rrate < rate) {
  1161. pr_err("%s: Rounded rate %lu not within range [%lu, %lu)\n",
  1162. clk_hw_get_name(hw), rrate, rate, rate_margin);
  1163. return -EINVAL;
  1164. }
  1165. return 0;
  1166. }
  1167. static int alpha_pll_fabia_set_rate(struct clk_hw *hw, unsigned long rate,
  1168. unsigned long prate)
  1169. {
  1170. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1171. u32 l, alpha_width = pll_alpha_width(pll);
  1172. unsigned long rrate;
  1173. int ret;
  1174. u64 a;
  1175. rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  1176. ret = alpha_pll_check_rate_margin(hw, rrate, rate);
  1177. if (ret < 0)
  1178. return ret;
  1179. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  1180. regmap_write(pll->clkr.regmap, PLL_FRAC(pll), a);
  1181. return __clk_alpha_pll_update_latch(pll);
  1182. }
  1183. static int alpha_pll_fabia_prepare(struct clk_hw *hw)
  1184. {
  1185. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1186. const struct pll_vco *vco;
  1187. struct clk_hw *parent_hw;
  1188. unsigned long cal_freq, rrate;
  1189. u32 cal_l, val, alpha_width = pll_alpha_width(pll);
  1190. const char *name = clk_hw_get_name(hw);
  1191. u64 a;
  1192. int ret;
  1193. /* Check if calibration needs to be done i.e. PLL is in reset */
  1194. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  1195. if (ret)
  1196. return ret;
  1197. /* Return early if calibration is not needed. */
  1198. if (val & PLL_RESET_N)
  1199. return 0;
  1200. vco = alpha_pll_find_vco(pll, clk_hw_get_rate(hw));
  1201. if (!vco) {
  1202. pr_err("%s: alpha pll not in a valid vco range\n", name);
  1203. return -EINVAL;
  1204. }
  1205. cal_freq = DIV_ROUND_CLOSEST((pll->vco_table[0].min_freq +
  1206. pll->vco_table[0].max_freq) * 54, 100);
  1207. parent_hw = clk_hw_get_parent(hw);
  1208. if (!parent_hw)
  1209. return -EINVAL;
  1210. rrate = alpha_pll_round_rate(cal_freq, clk_hw_get_rate(parent_hw),
  1211. &cal_l, &a, alpha_width);
  1212. ret = alpha_pll_check_rate_margin(hw, rrate, cal_freq);
  1213. if (ret < 0)
  1214. return ret;
  1215. /* Setup PLL for calibration frequency */
  1216. regmap_write(pll->clkr.regmap, PLL_CAL_L_VAL(pll), cal_l);
  1217. /* Bringup the PLL at calibration frequency */
  1218. ret = clk_alpha_pll_enable(hw);
  1219. if (ret) {
  1220. pr_err("%s: alpha pll calibration failed\n", name);
  1221. return ret;
  1222. }
  1223. clk_alpha_pll_disable(hw);
  1224. return 0;
  1225. }
  1226. const struct clk_ops clk_alpha_pll_fabia_ops = {
  1227. .prepare = alpha_pll_fabia_prepare,
  1228. .enable = alpha_pll_fabia_enable,
  1229. .disable = alpha_pll_fabia_disable,
  1230. .is_enabled = clk_alpha_pll_is_enabled,
  1231. .set_rate = alpha_pll_fabia_set_rate,
  1232. .recalc_rate = alpha_pll_fabia_recalc_rate,
  1233. .round_rate = clk_alpha_pll_round_rate,
  1234. };
  1235. EXPORT_SYMBOL_GPL(clk_alpha_pll_fabia_ops);
  1236. const struct clk_ops clk_alpha_pll_fixed_fabia_ops = {
  1237. .enable = alpha_pll_fabia_enable,
  1238. .disable = alpha_pll_fabia_disable,
  1239. .is_enabled = clk_alpha_pll_is_enabled,
  1240. .recalc_rate = alpha_pll_fabia_recalc_rate,
  1241. .round_rate = clk_alpha_pll_round_rate,
  1242. };
  1243. EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_fabia_ops);
  1244. static unsigned long clk_alpha_pll_postdiv_fabia_recalc_rate(struct clk_hw *hw,
  1245. unsigned long parent_rate)
  1246. {
  1247. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1248. u32 i, div = 1, val;
  1249. int ret;
  1250. ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
  1251. if (ret)
  1252. return ret;
  1253. val >>= pll->post_div_shift;
  1254. val &= BIT(pll->width) - 1;
  1255. for (i = 0; i < pll->num_post_div; i++) {
  1256. if (pll->post_div_table[i].val == val) {
  1257. div = pll->post_div_table[i].div;
  1258. break;
  1259. }
  1260. }
  1261. return (parent_rate / div);
  1262. }
  1263. static unsigned long
  1264. clk_trion_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  1265. {
  1266. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1267. struct regmap *regmap = pll->clkr.regmap;
  1268. u32 i, div = 1, val;
  1269. regmap_read(regmap, PLL_USER_CTL(pll), &val);
  1270. val >>= pll->post_div_shift;
  1271. val &= PLL_POST_DIV_MASK(pll);
  1272. for (i = 0; i < pll->num_post_div; i++) {
  1273. if (pll->post_div_table[i].val == val) {
  1274. div = pll->post_div_table[i].div;
  1275. break;
  1276. }
  1277. }
  1278. return (parent_rate / div);
  1279. }
  1280. static long
  1281. clk_trion_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  1282. unsigned long *prate)
  1283. {
  1284. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1285. return divider_round_rate(hw, rate, prate, pll->post_div_table,
  1286. pll->width, CLK_DIVIDER_ROUND_CLOSEST);
  1287. };
  1288. static int
  1289. clk_trion_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  1290. unsigned long parent_rate)
  1291. {
  1292. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1293. struct regmap *regmap = pll->clkr.regmap;
  1294. int i, val = 0, div;
  1295. div = DIV_ROUND_UP_ULL(parent_rate, rate);
  1296. for (i = 0; i < pll->num_post_div; i++) {
  1297. if (pll->post_div_table[i].div == div) {
  1298. val = pll->post_div_table[i].val;
  1299. break;
  1300. }
  1301. }
  1302. return regmap_update_bits(regmap, PLL_USER_CTL(pll),
  1303. PLL_POST_DIV_MASK(pll) << pll->post_div_shift,
  1304. val << pll->post_div_shift);
  1305. }
  1306. const struct clk_ops clk_alpha_pll_postdiv_trion_ops = {
  1307. .recalc_rate = clk_trion_pll_postdiv_recalc_rate,
  1308. .round_rate = clk_trion_pll_postdiv_round_rate,
  1309. .set_rate = clk_trion_pll_postdiv_set_rate,
  1310. };
  1311. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_trion_ops);
  1312. static long clk_alpha_pll_postdiv_fabia_round_rate(struct clk_hw *hw,
  1313. unsigned long rate, unsigned long *prate)
  1314. {
  1315. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1316. return divider_round_rate(hw, rate, prate, pll->post_div_table,
  1317. pll->width, CLK_DIVIDER_ROUND_CLOSEST);
  1318. }
  1319. static int clk_alpha_pll_postdiv_fabia_set_rate(struct clk_hw *hw,
  1320. unsigned long rate, unsigned long parent_rate)
  1321. {
  1322. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1323. int i, val = 0, div, ret;
  1324. /*
  1325. * If the PLL is in FSM mode, then treat set_rate callback as a
  1326. * no-operation.
  1327. */
  1328. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  1329. if (ret)
  1330. return ret;
  1331. if (val & PLL_VOTE_FSM_ENA)
  1332. return 0;
  1333. div = DIV_ROUND_UP_ULL(parent_rate, rate);
  1334. for (i = 0; i < pll->num_post_div; i++) {
  1335. if (pll->post_div_table[i].div == div) {
  1336. val = pll->post_div_table[i].val;
  1337. break;
  1338. }
  1339. }
  1340. return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  1341. (BIT(pll->width) - 1) << pll->post_div_shift,
  1342. val << pll->post_div_shift);
  1343. }
  1344. const struct clk_ops clk_alpha_pll_postdiv_fabia_ops = {
  1345. .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
  1346. .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
  1347. .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
  1348. };
  1349. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_fabia_ops);
  1350. /**
  1351. * clk_trion_pll_configure - configure the trion pll
  1352. *
  1353. * @pll: clk alpha pll
  1354. * @regmap: register map
  1355. * @config: configuration to apply for pll
  1356. */
  1357. void clk_trion_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  1358. const struct alpha_pll_config *config)
  1359. {
  1360. /*
  1361. * If the bootloader left the PLL enabled it's likely that there are
  1362. * RCGs that will lock up if we disable the PLL below.
  1363. */
  1364. if (trion_pll_is_enabled(pll, regmap)) {
  1365. pr_debug("Trion PLL is already enabled, skipping configuration\n");
  1366. return;
  1367. }
  1368. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
  1369. regmap_write(regmap, PLL_CAL_L_VAL(pll), TRION_PLL_CAL_VAL);
  1370. clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  1371. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
  1372. config->config_ctl_val);
  1373. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
  1374. config->config_ctl_hi_val);
  1375. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll),
  1376. config->config_ctl_hi1_val);
  1377. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
  1378. config->user_ctl_val);
  1379. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll),
  1380. config->user_ctl_hi_val);
  1381. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U1(pll),
  1382. config->user_ctl_hi1_val);
  1383. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
  1384. config->test_ctl_val);
  1385. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
  1386. config->test_ctl_hi_val);
  1387. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll),
  1388. config->test_ctl_hi1_val);
  1389. regmap_update_bits(regmap, PLL_MODE(pll), PLL_UPDATE_BYPASS,
  1390. PLL_UPDATE_BYPASS);
  1391. /* Disable PLL output */
  1392. regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1393. /* Set operation mode to OFF */
  1394. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1395. /* Place the PLL in STANDBY mode */
  1396. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1397. }
  1398. EXPORT_SYMBOL_GPL(clk_trion_pll_configure);
  1399. /*
  1400. * The TRION PLL requires a power-on self-calibration which happens when the
  1401. * PLL comes out of reset. Calibrate in case it is not completed.
  1402. */
  1403. static int __alpha_pll_trion_prepare(struct clk_hw *hw, u32 pcal_done)
  1404. {
  1405. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1406. u32 val;
  1407. int ret;
  1408. /* Return early if calibration is not needed. */
  1409. regmap_read(pll->clkr.regmap, PLL_STATUS(pll), &val);
  1410. if (val & pcal_done)
  1411. return 0;
  1412. /* On/off to calibrate */
  1413. ret = clk_trion_pll_enable(hw);
  1414. if (!ret)
  1415. clk_trion_pll_disable(hw);
  1416. return ret;
  1417. }
  1418. static int alpha_pll_trion_prepare(struct clk_hw *hw)
  1419. {
  1420. return __alpha_pll_trion_prepare(hw, TRION_PCAL_DONE);
  1421. }
  1422. static int alpha_pll_lucid_prepare(struct clk_hw *hw)
  1423. {
  1424. return __alpha_pll_trion_prepare(hw, LUCID_PCAL_DONE);
  1425. }
  1426. static int __alpha_pll_trion_set_rate(struct clk_hw *hw, unsigned long rate,
  1427. unsigned long prate, u32 latch_bit, u32 latch_ack)
  1428. {
  1429. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1430. unsigned long rrate;
  1431. u32 val, l, alpha_width = pll_alpha_width(pll);
  1432. u64 a;
  1433. int ret;
  1434. rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  1435. ret = alpha_pll_check_rate_margin(hw, rrate, rate);
  1436. if (ret < 0)
  1437. return ret;
  1438. regmap_update_bits(pll->clkr.regmap, PLL_L_VAL(pll), LUCID_EVO_PLL_L_VAL_MASK, l);
  1439. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  1440. /* Latch the PLL input */
  1441. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), latch_bit, latch_bit);
  1442. if (ret)
  1443. return ret;
  1444. /* Wait for 2 reference cycles before checking the ACK bit. */
  1445. udelay(1);
  1446. regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  1447. if (!(val & latch_ack)) {
  1448. pr_err("Lucid PLL latch failed. Output may be unstable!\n");
  1449. return -EINVAL;
  1450. }
  1451. /* Return the latch input to 0 */
  1452. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), latch_bit, 0);
  1453. if (ret)
  1454. return ret;
  1455. if (clk_hw_is_enabled(hw)) {
  1456. ret = wait_for_pll_enable_lock(pll);
  1457. if (ret)
  1458. return ret;
  1459. }
  1460. /* Wait for PLL output to stabilize */
  1461. udelay(100);
  1462. return 0;
  1463. }
  1464. static int alpha_pll_trion_set_rate(struct clk_hw *hw, unsigned long rate,
  1465. unsigned long prate)
  1466. {
  1467. return __alpha_pll_trion_set_rate(hw, rate, prate, PLL_UPDATE, ALPHA_PLL_ACK_LATCH);
  1468. }
  1469. const struct clk_ops clk_alpha_pll_trion_ops = {
  1470. .prepare = alpha_pll_trion_prepare,
  1471. .enable = clk_trion_pll_enable,
  1472. .disable = clk_trion_pll_disable,
  1473. .is_enabled = clk_trion_pll_is_enabled,
  1474. .recalc_rate = clk_trion_pll_recalc_rate,
  1475. .round_rate = clk_alpha_pll_round_rate,
  1476. .set_rate = alpha_pll_trion_set_rate,
  1477. };
  1478. EXPORT_SYMBOL_GPL(clk_alpha_pll_trion_ops);
  1479. const struct clk_ops clk_alpha_pll_lucid_ops = {
  1480. .prepare = alpha_pll_lucid_prepare,
  1481. .enable = clk_trion_pll_enable,
  1482. .disable = clk_trion_pll_disable,
  1483. .is_enabled = clk_trion_pll_is_enabled,
  1484. .recalc_rate = clk_trion_pll_recalc_rate,
  1485. .round_rate = clk_alpha_pll_round_rate,
  1486. .set_rate = alpha_pll_trion_set_rate,
  1487. };
  1488. EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_ops);
  1489. const struct clk_ops clk_alpha_pll_postdiv_lucid_ops = {
  1490. .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
  1491. .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
  1492. .set_rate = clk_alpha_pll_postdiv_fabia_set_rate,
  1493. };
  1494. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_ops);
  1495. void clk_agera_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  1496. const struct alpha_pll_config *config)
  1497. {
  1498. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
  1499. clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  1500. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
  1501. config->user_ctl_val);
  1502. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
  1503. config->config_ctl_val);
  1504. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
  1505. config->config_ctl_hi_val);
  1506. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
  1507. config->test_ctl_val);
  1508. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
  1509. config->test_ctl_hi_val);
  1510. }
  1511. EXPORT_SYMBOL_GPL(clk_agera_pll_configure);
  1512. static int clk_alpha_pll_agera_set_rate(struct clk_hw *hw, unsigned long rate,
  1513. unsigned long prate)
  1514. {
  1515. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1516. u32 l, alpha_width = pll_alpha_width(pll);
  1517. int ret;
  1518. unsigned long rrate;
  1519. u64 a;
  1520. rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  1521. ret = alpha_pll_check_rate_margin(hw, rrate, rate);
  1522. if (ret < 0)
  1523. return ret;
  1524. /* change L_VAL without having to go through the power on sequence */
  1525. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  1526. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  1527. if (clk_hw_is_enabled(hw))
  1528. return wait_for_pll_enable_lock(pll);
  1529. return 0;
  1530. }
  1531. const struct clk_ops clk_alpha_pll_agera_ops = {
  1532. .enable = clk_alpha_pll_enable,
  1533. .disable = clk_alpha_pll_disable,
  1534. .is_enabled = clk_alpha_pll_is_enabled,
  1535. .recalc_rate = alpha_pll_fabia_recalc_rate,
  1536. .round_rate = clk_alpha_pll_round_rate,
  1537. .set_rate = clk_alpha_pll_agera_set_rate,
  1538. };
  1539. EXPORT_SYMBOL_GPL(clk_alpha_pll_agera_ops);
  1540. /**
  1541. * clk_lucid_5lpe_pll_configure - configure the lucid 5lpe pll
  1542. *
  1543. * @pll: clk alpha pll
  1544. * @regmap: register map
  1545. * @config: configuration to apply for pll
  1546. */
  1547. void clk_lucid_5lpe_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  1548. const struct alpha_pll_config *config)
  1549. {
  1550. /*
  1551. * If the bootloader left the PLL enabled it's likely that there are
  1552. * RCGs that will lock up if we disable the PLL below.
  1553. */
  1554. if (trion_pll_is_enabled(pll, regmap)) {
  1555. pr_debug("Lucid 5LPE PLL is already enabled, skipping configuration\n");
  1556. return;
  1557. }
  1558. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
  1559. regmap_write(regmap, PLL_CAL_L_VAL(pll), TRION_PLL_CAL_VAL);
  1560. clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  1561. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll),
  1562. config->config_ctl_val);
  1563. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll),
  1564. config->config_ctl_hi_val);
  1565. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll),
  1566. config->config_ctl_hi1_val);
  1567. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll),
  1568. config->user_ctl_val);
  1569. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll),
  1570. config->user_ctl_hi_val);
  1571. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U1(pll),
  1572. config->user_ctl_hi1_val);
  1573. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll),
  1574. config->test_ctl_val);
  1575. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll),
  1576. config->test_ctl_hi_val);
  1577. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll),
  1578. config->test_ctl_hi1_val);
  1579. /* Disable PLL output */
  1580. regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1581. /* Set operation mode to OFF */
  1582. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1583. /* Place the PLL in STANDBY mode */
  1584. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1585. }
  1586. EXPORT_SYMBOL_GPL(clk_lucid_5lpe_pll_configure);
  1587. static int alpha_pll_lucid_5lpe_enable(struct clk_hw *hw)
  1588. {
  1589. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1590. u32 val;
  1591. int ret;
  1592. ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
  1593. if (ret)
  1594. return ret;
  1595. /* If in FSM mode, just vote for it */
  1596. if (val & LUCID_5LPE_ENABLE_VOTE_RUN) {
  1597. ret = clk_enable_regmap(hw);
  1598. if (ret)
  1599. return ret;
  1600. return wait_for_pll_enable_lock(pll);
  1601. }
  1602. /* Check if PLL is already enabled, return if enabled */
  1603. ret = trion_pll_is_enabled(pll, pll->clkr.regmap);
  1604. if (ret < 0)
  1605. return ret;
  1606. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1607. if (ret)
  1608. return ret;
  1609. regmap_write(pll->clkr.regmap, PLL_OPMODE(pll), PLL_RUN);
  1610. ret = wait_for_pll_enable_lock(pll);
  1611. if (ret)
  1612. return ret;
  1613. /* Enable the PLL outputs */
  1614. ret = regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, PLL_OUT_MASK);
  1615. if (ret)
  1616. return ret;
  1617. /* Enable the global PLL outputs */
  1618. return regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_OUTCTRL, PLL_OUTCTRL);
  1619. }
  1620. static void alpha_pll_lucid_5lpe_disable(struct clk_hw *hw)
  1621. {
  1622. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1623. u32 val;
  1624. int ret;
  1625. ret = regmap_read(pll->clkr.regmap, PLL_USER_CTL(pll), &val);
  1626. if (ret)
  1627. return;
  1628. /* If in FSM mode, just unvote it */
  1629. if (val & LUCID_5LPE_ENABLE_VOTE_RUN) {
  1630. clk_disable_regmap(hw);
  1631. return;
  1632. }
  1633. /* Disable the global PLL output */
  1634. ret = regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1635. if (ret)
  1636. return;
  1637. /* Disable the PLL outputs */
  1638. ret = regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
  1639. if (ret)
  1640. return;
  1641. /* Place the PLL mode in STANDBY */
  1642. regmap_write(pll->clkr.regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1643. }
  1644. /*
  1645. * The Lucid 5LPE PLL requires a power-on self-calibration which happens
  1646. * when the PLL comes out of reset. Calibrate in case it is not completed.
  1647. */
  1648. static int alpha_pll_lucid_5lpe_prepare(struct clk_hw *hw)
  1649. {
  1650. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1651. struct clk_hw *p;
  1652. u32 val = 0;
  1653. int ret;
  1654. /* Return early if calibration is not needed. */
  1655. regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  1656. if (val & LUCID_5LPE_PCAL_DONE)
  1657. return 0;
  1658. p = clk_hw_get_parent(hw);
  1659. if (!p)
  1660. return -EINVAL;
  1661. ret = alpha_pll_lucid_5lpe_enable(hw);
  1662. if (ret)
  1663. return ret;
  1664. alpha_pll_lucid_5lpe_disable(hw);
  1665. return 0;
  1666. }
  1667. static int alpha_pll_lucid_5lpe_set_rate(struct clk_hw *hw, unsigned long rate,
  1668. unsigned long prate)
  1669. {
  1670. return __alpha_pll_trion_set_rate(hw, rate, prate,
  1671. LUCID_5LPE_PLL_LATCH_INPUT,
  1672. LUCID_5LPE_ALPHA_PLL_ACK_LATCH);
  1673. }
  1674. static int __clk_lucid_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  1675. unsigned long parent_rate,
  1676. unsigned long enable_vote_run)
  1677. {
  1678. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  1679. struct regmap *regmap = pll->clkr.regmap;
  1680. int i, val, div, ret;
  1681. u32 mask;
  1682. /*
  1683. * If the PLL is in FSM mode, then treat set_rate callback as a
  1684. * no-operation.
  1685. */
  1686. ret = regmap_read(regmap, PLL_USER_CTL(pll), &val);
  1687. if (ret)
  1688. return ret;
  1689. if (val & enable_vote_run)
  1690. return 0;
  1691. if (!pll->post_div_table) {
  1692. pr_err("Missing the post_div_table for the %s PLL\n",
  1693. clk_hw_get_name(&pll->clkr.hw));
  1694. return -EINVAL;
  1695. }
  1696. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
  1697. for (i = 0; i < pll->num_post_div; i++) {
  1698. if (pll->post_div_table[i].div == div) {
  1699. val = pll->post_div_table[i].val;
  1700. break;
  1701. }
  1702. }
  1703. mask = GENMASK(pll->width + pll->post_div_shift - 1, pll->post_div_shift);
  1704. return regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  1705. mask, val << pll->post_div_shift);
  1706. }
  1707. static int clk_lucid_5lpe_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  1708. unsigned long parent_rate)
  1709. {
  1710. return __clk_lucid_pll_postdiv_set_rate(hw, rate, parent_rate, LUCID_5LPE_ENABLE_VOTE_RUN);
  1711. }
  1712. const struct clk_ops clk_alpha_pll_lucid_5lpe_ops = {
  1713. .prepare = alpha_pll_lucid_5lpe_prepare,
  1714. .enable = alpha_pll_lucid_5lpe_enable,
  1715. .disable = alpha_pll_lucid_5lpe_disable,
  1716. .is_enabled = clk_trion_pll_is_enabled,
  1717. .recalc_rate = clk_trion_pll_recalc_rate,
  1718. .round_rate = clk_alpha_pll_round_rate,
  1719. .set_rate = alpha_pll_lucid_5lpe_set_rate,
  1720. };
  1721. EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_5lpe_ops);
  1722. const struct clk_ops clk_alpha_pll_fixed_lucid_5lpe_ops = {
  1723. .enable = alpha_pll_lucid_5lpe_enable,
  1724. .disable = alpha_pll_lucid_5lpe_disable,
  1725. .is_enabled = clk_trion_pll_is_enabled,
  1726. .recalc_rate = clk_trion_pll_recalc_rate,
  1727. .round_rate = clk_alpha_pll_round_rate,
  1728. };
  1729. EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_lucid_5lpe_ops);
  1730. const struct clk_ops clk_alpha_pll_postdiv_lucid_5lpe_ops = {
  1731. .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
  1732. .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
  1733. .set_rate = clk_lucid_5lpe_pll_postdiv_set_rate,
  1734. };
  1735. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_5lpe_ops);
  1736. void clk_zonda_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  1737. const struct alpha_pll_config *config)
  1738. {
  1739. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
  1740. clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  1741. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  1742. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
  1743. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
  1744. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
  1745. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll), config->user_ctl_hi_val);
  1746. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U1(pll), config->user_ctl_hi1_val);
  1747. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
  1748. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
  1749. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll), config->test_ctl_hi1_val);
  1750. regmap_update_bits(regmap, PLL_MODE(pll), PLL_BYPASSNL, 0);
  1751. /* Disable PLL output */
  1752. regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1753. /* Set operation mode to OFF */
  1754. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1755. /* Place the PLL in STANDBY mode */
  1756. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1757. }
  1758. EXPORT_SYMBOL_GPL(clk_zonda_pll_configure);
  1759. static int clk_zonda_pll_enable(struct clk_hw *hw)
  1760. {
  1761. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1762. struct regmap *regmap = pll->clkr.regmap;
  1763. u32 val;
  1764. int ret;
  1765. regmap_read(regmap, PLL_MODE(pll), &val);
  1766. /* If in FSM mode, just vote for it */
  1767. if (val & PLL_VOTE_FSM_ENA) {
  1768. ret = clk_enable_regmap(hw);
  1769. if (ret)
  1770. return ret;
  1771. return wait_for_pll_enable_active(pll);
  1772. }
  1773. /* Get the PLL out of bypass mode */
  1774. regmap_update_bits(regmap, PLL_MODE(pll), PLL_BYPASSNL, PLL_BYPASSNL);
  1775. /*
  1776. * H/W requires a 1us delay between disabling the bypass and
  1777. * de-asserting the reset.
  1778. */
  1779. udelay(1);
  1780. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1781. /* Set operation mode to RUN */
  1782. regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
  1783. regmap_read(regmap, PLL_TEST_CTL(pll), &val);
  1784. /* If cfa mode then poll for freq lock */
  1785. if (val & ZONDA_STAY_IN_CFA)
  1786. ret = wait_for_zonda_pll_freq_lock(pll);
  1787. else
  1788. ret = wait_for_pll_enable_lock(pll);
  1789. if (ret)
  1790. return ret;
  1791. /* Enable the PLL outputs */
  1792. regmap_update_bits(regmap, PLL_USER_CTL(pll), ZONDA_PLL_OUT_MASK, ZONDA_PLL_OUT_MASK);
  1793. /* Enable the global PLL outputs */
  1794. regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, PLL_OUTCTRL);
  1795. return 0;
  1796. }
  1797. static void clk_zonda_pll_disable(struct clk_hw *hw)
  1798. {
  1799. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1800. struct regmap *regmap = pll->clkr.regmap;
  1801. u32 val;
  1802. regmap_read(regmap, PLL_MODE(pll), &val);
  1803. /* If in FSM mode, just unvote it */
  1804. if (val & PLL_VOTE_FSM_ENA) {
  1805. clk_disable_regmap(hw);
  1806. return;
  1807. }
  1808. /* Disable the global PLL output */
  1809. regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1810. /* Disable the PLL outputs */
  1811. regmap_update_bits(regmap, PLL_USER_CTL(pll), ZONDA_PLL_OUT_MASK, 0);
  1812. /* Put the PLL in bypass and reset */
  1813. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N | PLL_BYPASSNL, 0);
  1814. /* Place the PLL mode in OFF state */
  1815. regmap_write(regmap, PLL_OPMODE(pll), 0x0);
  1816. }
  1817. static void zonda_pll_adjust_l_val(unsigned long rate, unsigned long prate, u32 *l)
  1818. {
  1819. u64 remainder, quotient;
  1820. quotient = rate;
  1821. remainder = do_div(quotient, prate);
  1822. *l = rate + (u32)(remainder * 2 >= prate);
  1823. }
  1824. static int clk_zonda_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  1825. unsigned long prate)
  1826. {
  1827. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1828. unsigned long rrate;
  1829. u32 test_ctl_val;
  1830. u32 l, alpha_width = pll_alpha_width(pll);
  1831. u64 a;
  1832. int ret;
  1833. rrate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  1834. ret = alpha_pll_check_rate_margin(hw, rrate, rate);
  1835. if (ret < 0)
  1836. return ret;
  1837. if (a & PLL_ALPHA_MSB)
  1838. zonda_pll_adjust_l_val(rate, prate, &l);
  1839. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  1840. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  1841. if (!clk_hw_is_enabled(hw))
  1842. return 0;
  1843. /* Wait before polling for the frequency latch */
  1844. udelay(5);
  1845. /* Read stay in cfa mode */
  1846. regmap_read(pll->clkr.regmap, PLL_TEST_CTL(pll), &test_ctl_val);
  1847. /* If cfa mode then poll for freq lock */
  1848. if (test_ctl_val & ZONDA_STAY_IN_CFA)
  1849. ret = wait_for_zonda_pll_freq_lock(pll);
  1850. else
  1851. ret = wait_for_pll_enable_lock(pll);
  1852. if (ret)
  1853. return ret;
  1854. /* Wait for PLL output to stabilize */
  1855. udelay(100);
  1856. return 0;
  1857. }
  1858. const struct clk_ops clk_alpha_pll_zonda_ops = {
  1859. .enable = clk_zonda_pll_enable,
  1860. .disable = clk_zonda_pll_disable,
  1861. .is_enabled = clk_trion_pll_is_enabled,
  1862. .recalc_rate = clk_trion_pll_recalc_rate,
  1863. .round_rate = clk_alpha_pll_round_rate,
  1864. .set_rate = clk_zonda_pll_set_rate,
  1865. };
  1866. EXPORT_SYMBOL_GPL(clk_alpha_pll_zonda_ops);
  1867. void clk_lucid_evo_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  1868. const struct alpha_pll_config *config)
  1869. {
  1870. u32 lval = config->l;
  1871. /*
  1872. * If the bootloader left the PLL enabled it's likely that there are
  1873. * RCGs that will lock up if we disable the PLL below.
  1874. */
  1875. if (trion_pll_is_enabled(pll, regmap)) {
  1876. pr_debug("Lucid Evo PLL is already enabled, skipping configuration\n");
  1877. return;
  1878. }
  1879. lval |= TRION_PLL_CAL_VAL << LUCID_EVO_PLL_CAL_L_VAL_SHIFT;
  1880. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), lval);
  1881. clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  1882. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  1883. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
  1884. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
  1885. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
  1886. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll), config->user_ctl_hi_val);
  1887. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
  1888. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
  1889. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll), config->test_ctl_hi1_val);
  1890. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U2(pll), config->test_ctl_hi2_val);
  1891. /* Disable PLL output */
  1892. regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1893. /* Set operation mode to STANDBY and de-assert the reset */
  1894. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1895. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1896. }
  1897. EXPORT_SYMBOL_GPL(clk_lucid_evo_pll_configure);
  1898. void clk_lucid_ole_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  1899. const struct alpha_pll_config *config)
  1900. {
  1901. u32 lval = config->l;
  1902. lval |= TRION_PLL_CAL_VAL << LUCID_EVO_PLL_CAL_L_VAL_SHIFT;
  1903. lval |= TRION_PLL_CAL_VAL << LUCID_OLE_PLL_RINGOSC_CAL_L_VAL_SHIFT;
  1904. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), lval);
  1905. clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  1906. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  1907. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
  1908. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
  1909. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
  1910. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll), config->user_ctl_hi_val);
  1911. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
  1912. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
  1913. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll), config->test_ctl_hi1_val);
  1914. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U2(pll), config->test_ctl_hi2_val);
  1915. /* Disable PLL output */
  1916. regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1917. /* Set operation mode to STANDBY and de-assert the reset */
  1918. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1919. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1920. }
  1921. EXPORT_SYMBOL_GPL(clk_lucid_ole_pll_configure);
  1922. static int alpha_pll_lucid_evo_enable(struct clk_hw *hw)
  1923. {
  1924. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1925. struct regmap *regmap = pll->clkr.regmap;
  1926. u32 val;
  1927. int ret;
  1928. ret = regmap_read(regmap, PLL_USER_CTL(pll), &val);
  1929. if (ret)
  1930. return ret;
  1931. /* If in FSM mode, just vote for it */
  1932. if (val & LUCID_EVO_ENABLE_VOTE_RUN) {
  1933. ret = clk_enable_regmap(hw);
  1934. if (ret)
  1935. return ret;
  1936. return wait_for_pll_enable_lock(pll);
  1937. }
  1938. /* Check if PLL is already enabled */
  1939. ret = trion_pll_is_enabled(pll, regmap);
  1940. if (ret < 0) {
  1941. return ret;
  1942. } else if (ret) {
  1943. pr_warn("%s PLL is already enabled\n", clk_hw_get_name(&pll->clkr.hw));
  1944. return 0;
  1945. }
  1946. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, PLL_RESET_N);
  1947. if (ret)
  1948. return ret;
  1949. /* Set operation mode to RUN */
  1950. regmap_write(regmap, PLL_OPMODE(pll), PLL_RUN);
  1951. ret = wait_for_pll_enable_lock(pll);
  1952. if (ret)
  1953. return ret;
  1954. /* Enable the PLL outputs */
  1955. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, PLL_OUT_MASK);
  1956. if (ret)
  1957. return ret;
  1958. /* Enable the global PLL outputs */
  1959. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, PLL_OUTCTRL);
  1960. if (ret)
  1961. return ret;
  1962. /* Ensure that the write above goes through before returning. */
  1963. mb();
  1964. return ret;
  1965. }
  1966. static void _alpha_pll_lucid_evo_disable(struct clk_hw *hw, bool reset)
  1967. {
  1968. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1969. struct regmap *regmap = pll->clkr.regmap;
  1970. u32 val;
  1971. int ret;
  1972. ret = regmap_read(regmap, PLL_USER_CTL(pll), &val);
  1973. if (ret)
  1974. return;
  1975. /* If in FSM mode, just unvote it */
  1976. if (val & LUCID_EVO_ENABLE_VOTE_RUN) {
  1977. clk_disable_regmap(hw);
  1978. return;
  1979. }
  1980. /* Disable the global PLL output */
  1981. ret = regmap_update_bits(regmap, PLL_MODE(pll), PLL_OUTCTRL, 0);
  1982. if (ret)
  1983. return;
  1984. /* Disable the PLL outputs */
  1985. ret = regmap_update_bits(regmap, PLL_USER_CTL(pll), PLL_OUT_MASK, 0);
  1986. if (ret)
  1987. return;
  1988. /* Place the PLL mode in STANDBY */
  1989. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  1990. if (reset)
  1991. regmap_update_bits(regmap, PLL_MODE(pll), PLL_RESET_N, 0);
  1992. }
  1993. static int _alpha_pll_lucid_evo_prepare(struct clk_hw *hw, bool reset)
  1994. {
  1995. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  1996. struct clk_hw *p;
  1997. u32 val = 0;
  1998. int ret;
  1999. /* Return early if calibration is not needed. */
  2000. regmap_read(pll->clkr.regmap, PLL_MODE(pll), &val);
  2001. if (!(val & LUCID_EVO_PCAL_NOT_DONE))
  2002. return 0;
  2003. p = clk_hw_get_parent(hw);
  2004. if (!p)
  2005. return -EINVAL;
  2006. ret = alpha_pll_lucid_evo_enable(hw);
  2007. if (ret)
  2008. return ret;
  2009. _alpha_pll_lucid_evo_disable(hw, reset);
  2010. return 0;
  2011. }
  2012. static void alpha_pll_lucid_evo_disable(struct clk_hw *hw)
  2013. {
  2014. _alpha_pll_lucid_evo_disable(hw, false);
  2015. }
  2016. static int alpha_pll_lucid_evo_prepare(struct clk_hw *hw)
  2017. {
  2018. return _alpha_pll_lucid_evo_prepare(hw, false);
  2019. }
  2020. static void alpha_pll_reset_lucid_evo_disable(struct clk_hw *hw)
  2021. {
  2022. _alpha_pll_lucid_evo_disable(hw, true);
  2023. }
  2024. static int alpha_pll_reset_lucid_evo_prepare(struct clk_hw *hw)
  2025. {
  2026. return _alpha_pll_lucid_evo_prepare(hw, true);
  2027. }
  2028. static unsigned long alpha_pll_lucid_evo_recalc_rate(struct clk_hw *hw,
  2029. unsigned long parent_rate)
  2030. {
  2031. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  2032. struct regmap *regmap = pll->clkr.regmap;
  2033. u32 l, frac;
  2034. regmap_read(regmap, PLL_L_VAL(pll), &l);
  2035. l &= LUCID_EVO_PLL_L_VAL_MASK;
  2036. regmap_read(regmap, PLL_ALPHA_VAL(pll), &frac);
  2037. return alpha_pll_calc_rate(parent_rate, l, frac, pll_alpha_width(pll));
  2038. }
  2039. static int clk_lucid_evo_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  2040. unsigned long parent_rate)
  2041. {
  2042. return __clk_lucid_pll_postdiv_set_rate(hw, rate, parent_rate, LUCID_EVO_ENABLE_VOTE_RUN);
  2043. }
  2044. const struct clk_ops clk_alpha_pll_fixed_lucid_evo_ops = {
  2045. .enable = alpha_pll_lucid_evo_enable,
  2046. .disable = alpha_pll_lucid_evo_disable,
  2047. .is_enabled = clk_trion_pll_is_enabled,
  2048. .recalc_rate = alpha_pll_lucid_evo_recalc_rate,
  2049. .round_rate = clk_alpha_pll_round_rate,
  2050. };
  2051. EXPORT_SYMBOL_GPL(clk_alpha_pll_fixed_lucid_evo_ops);
  2052. const struct clk_ops clk_alpha_pll_postdiv_lucid_evo_ops = {
  2053. .recalc_rate = clk_alpha_pll_postdiv_fabia_recalc_rate,
  2054. .round_rate = clk_alpha_pll_postdiv_fabia_round_rate,
  2055. .set_rate = clk_lucid_evo_pll_postdiv_set_rate,
  2056. };
  2057. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_lucid_evo_ops);
  2058. const struct clk_ops clk_alpha_pll_lucid_evo_ops = {
  2059. .prepare = alpha_pll_lucid_evo_prepare,
  2060. .enable = alpha_pll_lucid_evo_enable,
  2061. .disable = alpha_pll_lucid_evo_disable,
  2062. .is_enabled = clk_trion_pll_is_enabled,
  2063. .recalc_rate = alpha_pll_lucid_evo_recalc_rate,
  2064. .round_rate = clk_alpha_pll_round_rate,
  2065. .set_rate = alpha_pll_lucid_5lpe_set_rate,
  2066. };
  2067. EXPORT_SYMBOL_GPL(clk_alpha_pll_lucid_evo_ops);
  2068. const struct clk_ops clk_alpha_pll_reset_lucid_evo_ops = {
  2069. .prepare = alpha_pll_reset_lucid_evo_prepare,
  2070. .enable = alpha_pll_lucid_evo_enable,
  2071. .disable = alpha_pll_reset_lucid_evo_disable,
  2072. .is_enabled = clk_trion_pll_is_enabled,
  2073. .recalc_rate = alpha_pll_lucid_evo_recalc_rate,
  2074. .round_rate = clk_alpha_pll_round_rate,
  2075. .set_rate = alpha_pll_lucid_5lpe_set_rate,
  2076. };
  2077. EXPORT_SYMBOL_GPL(clk_alpha_pll_reset_lucid_evo_ops);
  2078. void clk_rivian_evo_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  2079. const struct alpha_pll_config *config)
  2080. {
  2081. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  2082. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
  2083. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
  2084. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
  2085. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
  2086. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
  2087. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
  2088. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll), config->user_ctl_hi_val);
  2089. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  2090. regmap_update_bits(regmap, PLL_MODE(pll),
  2091. PLL_RESET_N | PLL_BYPASSNL | PLL_OUTCTRL,
  2092. PLL_RESET_N | PLL_BYPASSNL);
  2093. }
  2094. EXPORT_SYMBOL_GPL(clk_rivian_evo_pll_configure);
  2095. static unsigned long clk_rivian_evo_pll_recalc_rate(struct clk_hw *hw,
  2096. unsigned long parent_rate)
  2097. {
  2098. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  2099. u32 l;
  2100. regmap_read(pll->clkr.regmap, PLL_L_VAL(pll), &l);
  2101. return parent_rate * l;
  2102. }
  2103. static long clk_rivian_evo_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  2104. unsigned long *prate)
  2105. {
  2106. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  2107. unsigned long min_freq, max_freq;
  2108. u32 l;
  2109. u64 a;
  2110. rate = alpha_pll_round_rate(rate, *prate, &l, &a, 0);
  2111. if (!pll->vco_table || alpha_pll_find_vco(pll, rate))
  2112. return rate;
  2113. min_freq = pll->vco_table[0].min_freq;
  2114. max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
  2115. return clamp(rate, min_freq, max_freq);
  2116. }
  2117. const struct clk_ops clk_alpha_pll_rivian_evo_ops = {
  2118. .enable = alpha_pll_lucid_5lpe_enable,
  2119. .disable = alpha_pll_lucid_5lpe_disable,
  2120. .is_enabled = clk_trion_pll_is_enabled,
  2121. .recalc_rate = clk_rivian_evo_pll_recalc_rate,
  2122. .round_rate = clk_rivian_evo_pll_round_rate,
  2123. };
  2124. EXPORT_SYMBOL_GPL(clk_alpha_pll_rivian_evo_ops);
  2125. void clk_stromer_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  2126. const struct alpha_pll_config *config)
  2127. {
  2128. u32 val, val_u, mask, mask_u;
  2129. regmap_write(regmap, PLL_L_VAL(pll), config->l);
  2130. regmap_write(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  2131. regmap_write(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  2132. if (pll_has_64bit_config(pll))
  2133. regmap_write(regmap, PLL_CONFIG_CTL_U(pll),
  2134. config->config_ctl_hi_val);
  2135. if (pll_alpha_width(pll) > 32)
  2136. regmap_write(regmap, PLL_ALPHA_VAL_U(pll), config->alpha_hi);
  2137. val = config->main_output_mask;
  2138. val |= config->aux_output_mask;
  2139. val |= config->aux2_output_mask;
  2140. val |= config->early_output_mask;
  2141. val |= config->pre_div_val;
  2142. val |= config->post_div_val;
  2143. val |= config->vco_val;
  2144. val |= config->alpha_en_mask;
  2145. val |= config->alpha_mode_mask;
  2146. mask = config->main_output_mask;
  2147. mask |= config->aux_output_mask;
  2148. mask |= config->aux2_output_mask;
  2149. mask |= config->early_output_mask;
  2150. mask |= config->pre_div_mask;
  2151. mask |= config->post_div_mask;
  2152. mask |= config->vco_mask;
  2153. mask |= config->alpha_en_mask;
  2154. mask |= config->alpha_mode_mask;
  2155. regmap_update_bits(regmap, PLL_USER_CTL(pll), mask, val);
  2156. /* Stromer APSS PLL does not enable LOCK_DET by default, so enable it */
  2157. val_u = config->status_val << ALPHA_PLL_STATUS_REG_SHIFT;
  2158. val_u |= config->lock_det;
  2159. mask_u = config->status_mask;
  2160. mask_u |= config->lock_det;
  2161. regmap_update_bits(regmap, PLL_USER_CTL_U(pll), mask_u, val_u);
  2162. regmap_write(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
  2163. regmap_write(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
  2164. if (pll->flags & SUPPORTS_FSM_MODE)
  2165. qcom_pll_set_fsm_mode(regmap, PLL_MODE(pll), 6, 0);
  2166. }
  2167. EXPORT_SYMBOL_GPL(clk_stromer_pll_configure);
  2168. static int clk_alpha_pll_stromer_determine_rate(struct clk_hw *hw,
  2169. struct clk_rate_request *req)
  2170. {
  2171. u32 l;
  2172. u64 a;
  2173. req->rate = alpha_pll_round_rate(req->rate, req->best_parent_rate,
  2174. &l, &a, ALPHA_REG_BITWIDTH);
  2175. return 0;
  2176. }
  2177. static int clk_alpha_pll_stromer_set_rate(struct clk_hw *hw, unsigned long rate,
  2178. unsigned long prate)
  2179. {
  2180. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  2181. int ret;
  2182. u32 l;
  2183. u64 a;
  2184. rate = alpha_pll_round_rate(rate, prate, &l, &a, ALPHA_REG_BITWIDTH);
  2185. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  2186. a <<= ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH;
  2187. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  2188. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
  2189. a >> ALPHA_BITWIDTH);
  2190. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  2191. PLL_ALPHA_EN, PLL_ALPHA_EN);
  2192. if (!clk_hw_is_enabled(hw))
  2193. return 0;
  2194. /*
  2195. * Stromer PLL supports Dynamic programming.
  2196. * It allows the PLL frequency to be changed on-the-fly without first
  2197. * execution of a shutdown procedure followed by a bring up procedure.
  2198. */
  2199. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_UPDATE,
  2200. PLL_UPDATE);
  2201. ret = wait_for_pll_update(pll);
  2202. if (ret)
  2203. return ret;
  2204. return wait_for_pll_enable_lock(pll);
  2205. }
  2206. const struct clk_ops clk_alpha_pll_stromer_ops = {
  2207. .enable = clk_alpha_pll_enable,
  2208. .disable = clk_alpha_pll_disable,
  2209. .is_enabled = clk_alpha_pll_is_enabled,
  2210. .recalc_rate = clk_alpha_pll_recalc_rate,
  2211. .determine_rate = clk_alpha_pll_stromer_determine_rate,
  2212. .set_rate = clk_alpha_pll_stromer_set_rate,
  2213. };
  2214. EXPORT_SYMBOL_GPL(clk_alpha_pll_stromer_ops);
  2215. static int clk_alpha_pll_stromer_plus_set_rate(struct clk_hw *hw,
  2216. unsigned long rate,
  2217. unsigned long prate)
  2218. {
  2219. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  2220. u32 l, alpha_width = pll_alpha_width(pll);
  2221. int ret, pll_mode;
  2222. u64 a;
  2223. rate = alpha_pll_round_rate(rate, prate, &l, &a, alpha_width);
  2224. ret = regmap_read(pll->clkr.regmap, PLL_MODE(pll), &pll_mode);
  2225. if (ret)
  2226. return ret;
  2227. regmap_write(pll->clkr.regmap, PLL_MODE(pll), 0);
  2228. /* Delay of 2 output clock ticks required until output is disabled */
  2229. udelay(1);
  2230. regmap_write(pll->clkr.regmap, PLL_L_VAL(pll), l);
  2231. if (alpha_width > ALPHA_BITWIDTH)
  2232. a <<= alpha_width - ALPHA_BITWIDTH;
  2233. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL(pll), a);
  2234. regmap_write(pll->clkr.regmap, PLL_ALPHA_VAL_U(pll),
  2235. a >> ALPHA_BITWIDTH);
  2236. regmap_update_bits(pll->clkr.regmap, PLL_USER_CTL(pll),
  2237. PLL_ALPHA_EN, PLL_ALPHA_EN);
  2238. regmap_write(pll->clkr.regmap, PLL_MODE(pll), PLL_BYPASSNL);
  2239. /* Wait five micro seconds or more */
  2240. udelay(5);
  2241. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_RESET_N,
  2242. PLL_RESET_N);
  2243. /* The lock time should be less than 50 micro seconds worst case */
  2244. usleep_range(50, 60);
  2245. ret = wait_for_pll_enable_lock(pll);
  2246. if (ret) {
  2247. pr_err("Wait for PLL enable lock failed [%s] %d\n",
  2248. clk_hw_get_name(hw), ret);
  2249. return ret;
  2250. }
  2251. if (pll_mode & PLL_OUTCTRL)
  2252. regmap_update_bits(pll->clkr.regmap, PLL_MODE(pll), PLL_OUTCTRL,
  2253. PLL_OUTCTRL);
  2254. return 0;
  2255. }
  2256. const struct clk_ops clk_alpha_pll_stromer_plus_ops = {
  2257. .prepare = clk_alpha_pll_enable,
  2258. .unprepare = clk_alpha_pll_disable,
  2259. .is_enabled = clk_alpha_pll_is_enabled,
  2260. .recalc_rate = clk_alpha_pll_recalc_rate,
  2261. .determine_rate = clk_alpha_pll_stromer_determine_rate,
  2262. .set_rate = clk_alpha_pll_stromer_plus_set_rate,
  2263. };
  2264. EXPORT_SYMBOL_GPL(clk_alpha_pll_stromer_plus_ops);
  2265. void clk_regera_pll_configure(struct clk_alpha_pll *pll, struct regmap *regmap,
  2266. const struct alpha_pll_config *config)
  2267. {
  2268. clk_alpha_pll_write_config(regmap, PLL_L_VAL(pll), config->l);
  2269. clk_alpha_pll_write_config(regmap, PLL_ALPHA_VAL(pll), config->alpha);
  2270. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL(pll), config->config_ctl_val);
  2271. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U(pll), config->config_ctl_hi_val);
  2272. clk_alpha_pll_write_config(regmap, PLL_CONFIG_CTL_U1(pll), config->config_ctl_hi1_val);
  2273. clk_alpha_pll_write_config(regmap, PLL_USER_CTL(pll), config->user_ctl_val);
  2274. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U(pll), config->user_ctl_hi_val);
  2275. clk_alpha_pll_write_config(regmap, PLL_USER_CTL_U1(pll), config->user_ctl_hi1_val);
  2276. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL(pll), config->test_ctl_val);
  2277. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U(pll), config->test_ctl_hi_val);
  2278. clk_alpha_pll_write_config(regmap, PLL_TEST_CTL_U1(pll), config->test_ctl_hi1_val);
  2279. /* Set operation mode to STANDBY */
  2280. regmap_write(regmap, PLL_OPMODE(pll), PLL_STANDBY);
  2281. }
  2282. EXPORT_SYMBOL_GPL(clk_regera_pll_configure);
  2283. const struct clk_ops clk_alpha_pll_regera_ops = {
  2284. .enable = clk_zonda_pll_enable,
  2285. .disable = clk_zonda_pll_disable,
  2286. .is_enabled = clk_alpha_pll_is_enabled,
  2287. .recalc_rate = clk_trion_pll_recalc_rate,
  2288. .round_rate = clk_alpha_pll_round_rate,
  2289. .set_rate = clk_zonda_pll_set_rate,
  2290. };
  2291. EXPORT_SYMBOL_GPL(clk_alpha_pll_regera_ops);