clk-alpha-pll.c 90 KB

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