cam_soc_util.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/of.h>
  7. #include <linux/clk.h>
  8. #include <linux/slab.h>
  9. #include <linux/gpio.h>
  10. #include <linux/of_gpio.h>
  11. #include "cam_soc_util.h"
  12. #include "cam_debug_util.h"
  13. #include "cam_cx_ipeak.h"
  14. #include "cam_mem_mgr.h"
  15. #include "cam_presil_hw_access.h"
  16. #include "cam_compat.h"
  17. #if IS_ENABLED(CONFIG_QCOM_CRM)
  18. #include <soc/qcom/crm.h>
  19. #include <linux/clk/qcom.h>
  20. #endif
  21. #define CAM_TO_MASK(bitn) (1 << (int)(bitn))
  22. #define CAM_IS_BIT_SET(mask, bit) ((mask) & CAM_TO_MASK(bit))
  23. #define CAM_SET_BIT(mask, bit) ((mask) |= CAM_TO_MASK(bit))
  24. #define CAM_CLEAR_BIT(mask, bit) ((mask) &= ~CAM_TO_MASK(bit))
  25. #define CAM_SS_START_PRESIL 0x08c00000
  26. #define CAM_SS_START 0x0ac00000
  27. #define CAM_CLK_DIRNAME "clk"
  28. static uint skip_mmrm_set_rate;
  29. module_param(skip_mmrm_set_rate, uint, 0644);
  30. /**
  31. * struct cam_clk_wrapper_clk: This represents an entry corresponding to a
  32. * shared clock in Clk wrapper. Clients that share
  33. * the same clock are registered to this clk entry
  34. * and set rate from them is consolidated before
  35. * setting it to clk driver.
  36. *
  37. * @list: List pointer to point to next shared clk entry
  38. * @clk_id: Clk Id of this clock
  39. * @curr_clk_rate: Current clock rate set for this clock
  40. * @client_list: List of clients registered to this shared clock entry
  41. * @num_clients: Number of registered clients
  42. * @active_clients: Number of active clients
  43. * @mmrm_client: MMRM Client handle for src clock
  44. * @soc_info: soc_info of client with which mmrm handle is created.
  45. * This is used as unique identifier for a client and mmrm
  46. * callback data. When client corresponds to this soc_info is
  47. * unregistered, need to unregister mmrm handle as well.
  48. * @is_nrt_dev: Whether this clock corresponds to NRT device
  49. * @min_clk_rate: Minimum clk rate that this clock supports
  50. **/
  51. struct cam_clk_wrapper_clk {
  52. struct list_head list;
  53. uint32_t clk_id;
  54. int64_t curr_clk_rate;
  55. struct list_head client_list;
  56. uint32_t num_clients;
  57. uint32_t active_clients;
  58. void *mmrm_handle;
  59. struct cam_hw_soc_info *soc_info;
  60. bool is_nrt_dev;
  61. int64_t min_clk_rate;
  62. };
  63. /**
  64. * struct cam_clk_wrapper_client: This represents a client (device) that wants
  65. * to share the clock with some other client.
  66. *
  67. * @list: List pointer to point to next client that share the
  68. * same clock
  69. * @soc_info: soc_info of client. This is used as unique identifier
  70. * for a client
  71. * @clk: Clk handle
  72. * @curr_clk_rate: Current clock rate set for this client
  73. **/
  74. struct cam_clk_wrapper_client {
  75. struct list_head list;
  76. struct cam_hw_soc_info *soc_info;
  77. struct clk *clk;
  78. int64_t curr_clk_rate;
  79. };
  80. static char supported_clk_info[256];
  81. static DEFINE_MUTEX(wrapper_lock);
  82. static LIST_HEAD(wrapper_clk_list);
  83. #define CAM_IS_VALID_CESTA_IDX(idx) ((idx >= 0) && (idx < CAM_CESTA_MAX_CLIENTS))
  84. #define CAM_CRM_DEV_IDENTIFIER "cam_crm"
  85. const struct device *cam_cesta_crm_dev;
  86. #if IS_ENABLED(CONFIG_QCOM_CRM)
  87. static inline const struct device *cam_wrapper_crm_get_device(
  88. const char *name)
  89. {
  90. if (debug_bypass_drivers & CAM_BYPASS_CESTA) {
  91. CAM_WARN(CAM_UTIL, "Bypass crm get device");
  92. return (const struct device *)BYPASS_VALUE;
  93. }
  94. return crm_get_device(name);
  95. }
  96. static inline int cam_wrapper_crm_write_pwr_states(const struct device *dev,
  97. u32 drv_id)
  98. {
  99. if (debug_bypass_drivers & CAM_BYPASS_CESTA) {
  100. CAM_WARN(CAM_UTIL, "Bypass crm write pwr states");
  101. return 0;
  102. }
  103. return crm_write_pwr_states(cam_cesta_crm_dev, drv_id);
  104. }
  105. #endif
  106. #if IS_ENABLED(CONFIG_QCOM_CRM) && IS_ENABLED(CONFIG_SPECTRA_USE_CLK_CRM_API)
  107. static inline int cam_wrapper_qcom_clk_crm_set_rate(struct clk *clk,
  108. enum crm_drv_type client_type, u32 client_idx,
  109. u32 pwr_st, unsigned long rate)
  110. {
  111. if (debug_bypass_drivers & CAM_BYPASS_CESTA) {
  112. CAM_WARN(CAM_UTIL, "Bypass qcom clk crm set rate");
  113. return 0;
  114. }
  115. return qcom_clk_crm_set_rate(clk, client_type, client_idx, pwr_st, rate);
  116. }
  117. #endif
  118. static inline int cam_wrapper_clk_set_rate(struct clk *clk, unsigned long rate)
  119. {
  120. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  121. CAM_WARN(CAM_UTIL, "Bypass clk set rate");
  122. return 0;
  123. }
  124. return clk_set_rate(clk, rate);
  125. }
  126. static inline long cam_wrapper_clk_round_rate(struct clk *clk, unsigned long rate)
  127. {
  128. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  129. CAM_WARN(CAM_UTIL, "Bypass clk round rate");
  130. return rate;
  131. }
  132. return clk_round_rate(clk, rate);
  133. }
  134. inline unsigned long cam_wrapper_clk_get_rate(struct clk *clk)
  135. {
  136. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  137. CAM_WARN(CAM_UTIL, "Bypass clk get rate");
  138. return DEFAULT_CLK_VALUE;
  139. }
  140. return clk_get_rate(clk);
  141. }
  142. static inline struct clk *cam_wrapper_clk_get(struct device *dev, const char *id)
  143. {
  144. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  145. CAM_WARN(CAM_UTIL, "Bypass clk get");
  146. return (struct clk *)BYPASS_VALUE;
  147. }
  148. return clk_get(dev, id);
  149. }
  150. static inline void cam_wrapper_clk_put(struct clk *clk)
  151. {
  152. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  153. CAM_WARN(CAM_UTIL, "Bypass clk put");
  154. return;
  155. }
  156. clk_put(clk);
  157. }
  158. static inline struct clk *cam_wrapper_of_clk_get_from_provider(
  159. struct of_phandle_args *clkspec)
  160. {
  161. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  162. CAM_WARN(CAM_UTIL, "Bypass of clk get from provider");
  163. return (struct clk *)BYPASS_VALUE;
  164. }
  165. return of_clk_get_from_provider(clkspec);
  166. }
  167. static inline int cam_wrapper_clk_prepare_enable(struct clk *clk)
  168. {
  169. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  170. CAM_WARN(CAM_UTIL, "Bypass clk prepare enable");
  171. return 0;
  172. }
  173. return clk_prepare_enable(clk);
  174. }
  175. static inline void cam_wrapper_clk_disable_unprepare(struct clk *clk)
  176. {
  177. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  178. CAM_WARN(CAM_UTIL, "Bypass clk disable unprepare");
  179. return;
  180. }
  181. clk_disable_unprepare(clk);
  182. }
  183. static inline struct regulator *cam_wrapper_regulator_get(struct device *dev,
  184. const char *id)
  185. {
  186. if (debug_bypass_drivers & CAM_BYPASS_RGLTR) {
  187. CAM_WARN(CAM_UTIL, "Bypass regulator get");
  188. return (struct regulator *)BYPASS_VALUE;
  189. }
  190. return regulator_get(dev, id);
  191. }
  192. static inline void cam_wrapper_regulator_put(struct regulator *regulator)
  193. {
  194. if (debug_bypass_drivers & CAM_BYPASS_RGLTR) {
  195. CAM_WARN(CAM_UTIL, "Bypass regulator put");
  196. return;
  197. }
  198. regulator_put(regulator);
  199. }
  200. static inline int cam_wrapper_regulator_disable(struct regulator *regulator)
  201. {
  202. if (debug_bypass_drivers & CAM_BYPASS_RGLTR) {
  203. CAM_WARN(CAM_UTIL, "Bypass regulator disable");
  204. return 0;
  205. }
  206. return regulator_disable(regulator);
  207. }
  208. static inline int cam_wrapper_regulator_enable(struct regulator *regulator)
  209. {
  210. if (debug_bypass_drivers & CAM_BYPASS_RGLTR) {
  211. CAM_WARN(CAM_UTIL, "Bypass regulator enable");
  212. return 0;
  213. }
  214. return regulator_enable(regulator);
  215. }
  216. static inline int cam_wrapper_regulator_set_voltage(
  217. struct regulator *regulator, int min_uV, int max_uV)
  218. {
  219. if (debug_bypass_drivers & CAM_BYPASS_RGLTR) {
  220. CAM_WARN(CAM_UTIL, "Bypass regulator set voltage");
  221. return 0;
  222. }
  223. return regulator_set_voltage(regulator, min_uV, max_uV);
  224. }
  225. static inline int cam_wrapper_regulator_count_voltages(
  226. struct regulator *regulator)
  227. {
  228. if (debug_bypass_drivers & CAM_BYPASS_RGLTR) {
  229. CAM_WARN(CAM_UTIL, "Bypass regulator count voltages");
  230. return 0;
  231. }
  232. return regulator_count_voltages(regulator);
  233. }
  234. inline int cam_wrapper_regulator_set_load(
  235. struct regulator *regulator, int uA_load)
  236. {
  237. if (debug_bypass_drivers & CAM_BYPASS_RGLTR) {
  238. CAM_WARN(CAM_UTIL, "Bypass regulator set load");
  239. return 0;
  240. }
  241. return regulator_set_load(regulator, uA_load);
  242. }
  243. inline int cam_wrapper_regulator_set_mode(
  244. struct regulator *regulator, unsigned int mode)
  245. {
  246. if (debug_bypass_drivers & CAM_BYPASS_RGLTR_MODE) {
  247. CAM_WARN(CAM_UTIL, "Bypass regulator set mode");
  248. return 0;
  249. }
  250. return regulator_set_mode(regulator, mode);
  251. }
  252. static inline int cam_wrapper_regulator_is_enabled(
  253. struct regulator *regulator)
  254. {
  255. if (debug_bypass_drivers & CAM_BYPASS_RGLTR) {
  256. CAM_WARN(CAM_UTIL, "Bypass regulator is enabled");
  257. return 0;
  258. }
  259. return regulator_is_enabled(regulator);
  260. }
  261. inline void cam_soc_util_set_bypass_drivers(
  262. uint32_t bypass_drivers)
  263. {
  264. debug_bypass_drivers = bypass_drivers;
  265. CAM_INFO(CAM_UTIL, "bypass drivers %d", debug_bypass_drivers);
  266. }
  267. #if IS_ENABLED(CONFIG_QCOM_CRM)
  268. inline int cam_soc_util_cesta_populate_crm_device(void)
  269. {
  270. cam_cesta_crm_dev = cam_wrapper_crm_get_device(CAM_CRM_DEV_IDENTIFIER);
  271. if (!cam_cesta_crm_dev) {
  272. CAM_ERR(CAM_UTIL, "Failed to get cesta crm dev for %s", CAM_CRM_DEV_IDENTIFIER);
  273. return -ENODEV;
  274. }
  275. return 0;
  276. }
  277. int cam_soc_util_cesta_channel_switch(uint32_t cesta_client_idx, const char *identifier)
  278. {
  279. int rc = 0;
  280. if (!cam_cesta_crm_dev) {
  281. CAM_ERR(CAM_UTIL, "camera cesta crm device is null");
  282. return -EINVAL;
  283. }
  284. if (!CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  285. CAM_ERR(CAM_UTIL, "Invalid client index for camera cesta idx: %d max: %d",
  286. cesta_client_idx, CAM_CESTA_MAX_CLIENTS);
  287. return -EINVAL;
  288. }
  289. CAM_DBG(CAM_PERF, "CESTA Channel switch : hw client idx %d identifier=%s",
  290. cesta_client_idx, identifier);
  291. rc = cam_wrapper_crm_write_pwr_states(cam_cesta_crm_dev, cesta_client_idx);
  292. if (rc) {
  293. CAM_ERR(CAM_UTIL,
  294. "Failed to trigger cesta channel switch cesta_client_idx: %u rc: %d",
  295. cesta_client_idx, rc);
  296. return rc;
  297. }
  298. return rc;
  299. }
  300. #else
  301. inline int cam_soc_util_cesta_populate_crm_device(void)
  302. {
  303. CAM_ERR(CAM_UTIL, "Not supported");
  304. return -EOPNOTSUPP;
  305. }
  306. inline int cam_soc_util_cesta_channel_switch(uint32_t cesta_client_idx, const char *identifier)
  307. {
  308. CAM_ERR(CAM_UTIL, "Not supported, cesta_client_idx=%d, identifier=%s",
  309. cesta_client_idx, identifier);
  310. return -EOPNOTSUPP;
  311. }
  312. #endif
  313. #if IS_ENABLED(CONFIG_QCOM_CRM) && IS_ENABLED(CONFIG_SPECTRA_USE_CLK_CRM_API)
  314. static int cam_soc_util_set_cesta_clk_rate(struct cam_hw_soc_info *soc_info,
  315. uint32_t cesta_client_idx, unsigned long high_val, unsigned long low_val,
  316. unsigned long *applied_high_val, unsigned long *applied_low_val)
  317. {
  318. int32_t src_clk_idx;
  319. struct clk *clk = NULL;
  320. int rc = 0;
  321. if (!soc_info || (soc_info->src_clk_idx < 0) ||
  322. (soc_info->src_clk_idx >= CAM_SOC_MAX_CLK)) {
  323. CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d",
  324. soc_info ? soc_info->src_clk_idx : -1);
  325. return -EINVAL;
  326. }
  327. if (!CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  328. CAM_ERR(CAM_UTIL, "Invalid client index for camera cesta idx: %d max: %d",
  329. cesta_client_idx, CAM_CESTA_MAX_CLIENTS);
  330. return -EINVAL;
  331. }
  332. /* Only source clocks are supported by this API to set HW client clock votes */
  333. src_clk_idx = soc_info->src_clk_idx;
  334. clk = soc_info->clk[src_clk_idx];
  335. CAM_DBG(CAM_UTIL, "%s Requested clk rate [high low]: [%llu %llu] cesta_client_idx: %d",
  336. soc_info->clk_name[src_clk_idx], high_val, low_val, cesta_client_idx);
  337. rc = cam_wrapper_qcom_clk_crm_set_rate(
  338. clk, CRM_HW_DRV, cesta_client_idx, CRM_PWR_STATE1, high_val);
  339. if (rc) {
  340. CAM_ERR(CAM_UTIL,
  341. "Failed in setting cesta high clk rate, client idx: %u pwr state: %u clk_val: %llu rc: %d",
  342. cesta_client_idx, CRM_PWR_STATE1, high_val, rc);
  343. return rc;
  344. }
  345. rc = cam_wrapper_qcom_clk_crm_set_rate(
  346. clk, CRM_HW_DRV, cesta_client_idx, CRM_PWR_STATE0, low_val);
  347. if (rc) {
  348. CAM_ERR(CAM_UTIL,
  349. "Failed in setting cesta low clk rate, client idx: %u pwr state: %u clk_val: %llu rc: %d",
  350. cesta_client_idx, CRM_PWR_STATE0, low_val, rc);
  351. return rc;
  352. }
  353. if (applied_high_val)
  354. *applied_high_val = high_val;
  355. if (applied_low_val)
  356. *applied_low_val = low_val;
  357. return rc;
  358. }
  359. #else
  360. static inline int cam_soc_util_set_cesta_clk_rate(struct cam_hw_soc_info *soc_info,
  361. uint32_t cesta_client_idx, unsigned long high_val, unsigned long low_val,
  362. unsigned long *applied_high_val, unsigned long *applied_low_val)
  363. {
  364. CAM_ERR(CAM_UTIL, "Not supported, dev=%s, cesta_client_idx=%d, high_val=%ld, low_val=%ld",
  365. soc_info->dev_name, cesta_client_idx, high_val, low_val);
  366. return -EOPNOTSUPP;
  367. }
  368. #endif
  369. #if IS_REACHABLE(CONFIG_MSM_MMRM)
  370. bool cam_is_mmrm_supported_on_current_chip(void)
  371. {
  372. bool is_supported;
  373. is_supported = mmrm_client_check_scaling_supported(MMRM_CLIENT_CLOCK,
  374. MMRM_CLIENT_DOMAIN_CAMERA);
  375. CAM_DBG(CAM_UTIL, "is mmrm supported: %s",
  376. CAM_BOOL_TO_YESNO(is_supported));;
  377. return is_supported;
  378. }
  379. int cam_mmrm_notifier_callback(
  380. struct mmrm_client_notifier_data *notifier_data)
  381. {
  382. if (!notifier_data) {
  383. CAM_ERR(CAM_UTIL, "Invalid notifier data");
  384. return -EBADR;
  385. }
  386. if (notifier_data->cb_type == MMRM_CLIENT_RESOURCE_VALUE_CHANGE) {
  387. struct cam_hw_soc_info *soc_info = notifier_data->pvt_data;
  388. CAM_WARN(CAM_UTIL, "Dev %s Clk %s value change from %ld to %ld",
  389. soc_info->dev_name,
  390. (soc_info->src_clk_idx == -1) ? "No src clk" :
  391. soc_info->clk_name[soc_info->src_clk_idx],
  392. notifier_data->cb_data.val_chng.old_val,
  393. notifier_data->cb_data.val_chng.new_val);
  394. }
  395. return 0;
  396. }
  397. int cam_soc_util_register_mmrm_client(
  398. uint32_t clk_id, struct clk *clk, bool is_nrt_dev,
  399. struct cam_hw_soc_info *soc_info, const char *clk_name,
  400. void **mmrm_handle)
  401. {
  402. struct mmrm_client *mmrm_client;
  403. struct mmrm_client_desc desc = { };
  404. if (!mmrm_handle) {
  405. CAM_ERR(CAM_UTIL, "Invalid mmrm input");
  406. return -EINVAL;
  407. }
  408. *mmrm_handle = (void *)NULL;
  409. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  410. CAM_WARN(CAM_UTIL, "Bypass register mmrm client");
  411. return 0;
  412. }
  413. if (!cam_is_mmrm_supported_on_current_chip())
  414. return 0;
  415. desc.client_type = MMRM_CLIENT_CLOCK;
  416. desc.client_info.desc.client_domain = MMRM_CLIENT_DOMAIN_CAMERA;
  417. desc.client_info.desc.client_id = clk_id;
  418. desc.client_info.desc.clk = clk;
  419. snprintf((char *)desc.client_info.desc.name,
  420. sizeof(desc.client_info.desc.name), "%s_%s",
  421. soc_info->dev_name, clk_name);
  422. desc.priority = is_nrt_dev ?
  423. MMRM_CLIENT_PRIOR_LOW : MMRM_CLIENT_PRIOR_HIGH;
  424. desc.pvt_data = soc_info;
  425. desc.notifier_callback_fn = cam_mmrm_notifier_callback;
  426. mmrm_client = mmrm_client_register(&desc);
  427. if (!mmrm_client) {
  428. CAM_ERR(CAM_UTIL, "MMRM Register failed Dev %s clk %s id %d",
  429. soc_info->dev_name, clk_name, clk_id);
  430. return -EINVAL;
  431. }
  432. CAM_DBG(CAM_UTIL,
  433. "MMRM Register success Dev %s is_nrt_dev %d clk %s id %d handle=%pK",
  434. soc_info->dev_name, is_nrt_dev, clk_name, clk_id, mmrm_client);
  435. *mmrm_handle = (void *)mmrm_client;
  436. return 0;
  437. }
  438. int cam_soc_util_unregister_mmrm_client(
  439. void *mmrm_handle)
  440. {
  441. int rc = 0;
  442. CAM_DBG(CAM_UTIL, "MMRM UnRegister handle=%pK", mmrm_handle);
  443. if (mmrm_handle) {
  444. rc = mmrm_client_deregister((struct mmrm_client *)mmrm_handle);
  445. if (rc)
  446. CAM_ERR(CAM_UTIL,
  447. "Failed in deregister handle=%pK, rc %d",
  448. mmrm_handle, rc);
  449. }
  450. return rc;
  451. }
  452. static int cam_soc_util_set_rate_through_mmrm(
  453. void *mmrm_handle, bool is_nrt_dev, long min_rate,
  454. long req_rate, uint32_t num_hw_blocks)
  455. {
  456. int rc = 0;
  457. struct mmrm_client_data client_data;
  458. struct mmrm_client_res_value val;
  459. client_data.num_hw_blocks = num_hw_blocks;
  460. client_data.flags = 0;
  461. CAM_DBG(CAM_UTIL,
  462. "mmrm=%pK, nrt=%d, min_rate=%ld req_rate %ld, num_blocks=%d",
  463. mmrm_handle, is_nrt_dev, min_rate, req_rate, num_hw_blocks);
  464. if (is_nrt_dev) {
  465. val.min = min_rate;
  466. val.cur = req_rate;
  467. rc = mmrm_client_set_value_in_range(
  468. (struct mmrm_client *)mmrm_handle, &client_data, &val);
  469. } else {
  470. rc = mmrm_client_set_value(
  471. (struct mmrm_client *)mmrm_handle,
  472. &client_data, req_rate);
  473. }
  474. if (rc)
  475. CAM_ERR(CAM_UTIL, "Set rate failed rate %ld rc %d",
  476. req_rate, rc);
  477. return rc;
  478. }
  479. #else
  480. int cam_soc_util_register_mmrm_client(
  481. uint32_t clk_id, struct clk *clk, bool is_nrt_dev,
  482. struct cam_hw_soc_info *soc_info, const char *clk_name,
  483. void **mmrm_handle)
  484. {
  485. if (!mmrm_handle) {
  486. CAM_ERR(CAM_UTIL, "Invalid mmrm input");
  487. return -EINVAL;
  488. }
  489. *mmrm_handle = NULL;
  490. return 0;
  491. }
  492. int cam_soc_util_unregister_mmrm_client(
  493. void *mmrm_handle)
  494. {
  495. return 0;
  496. }
  497. static int cam_soc_util_set_rate_through_mmrm(
  498. void *mmrm_handle, bool is_nrt_dev, long min_rate,
  499. long req_rate, uint32_t num_hw_blocks)
  500. {
  501. return 0;
  502. }
  503. #endif
  504. static int cam_soc_util_clk_wrapper_register_entry(
  505. uint32_t clk_id, struct clk *clk, bool is_src_clk,
  506. struct cam_hw_soc_info *soc_info, int64_t min_clk_rate,
  507. const char *clk_name)
  508. {
  509. struct cam_clk_wrapper_clk *wrapper_clk;
  510. struct cam_clk_wrapper_client *wrapper_client;
  511. bool clock_found = false;
  512. int rc = 0;
  513. mutex_lock(&wrapper_lock);
  514. list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
  515. CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
  516. wrapper_clk->clk_id, wrapper_clk->num_clients);
  517. if (wrapper_clk->clk_id == clk_id) {
  518. clock_found = true;
  519. list_for_each_entry(wrapper_client,
  520. &wrapper_clk->client_list, list) {
  521. CAM_DBG(CAM_UTIL,
  522. "Clk id %d entry client %s",
  523. wrapper_clk->clk_id,
  524. wrapper_client->soc_info->dev_name);
  525. if (wrapper_client->soc_info == soc_info) {
  526. CAM_ERR(CAM_UTIL,
  527. "Register with same soc info, clk id %d, client %s",
  528. clk_id, soc_info->dev_name);
  529. rc = -EINVAL;
  530. goto end;
  531. }
  532. }
  533. break;
  534. }
  535. }
  536. if (!clock_found) {
  537. CAM_DBG(CAM_UTIL, "Adding new entry for clk id %d", clk_id);
  538. wrapper_clk = kzalloc(sizeof(struct cam_clk_wrapper_clk),
  539. GFP_KERNEL);
  540. if (!wrapper_clk) {
  541. CAM_ERR(CAM_UTIL,
  542. "Failed in allocating new clk entry %d",
  543. clk_id);
  544. rc = -ENOMEM;
  545. goto end;
  546. }
  547. wrapper_clk->clk_id = clk_id;
  548. INIT_LIST_HEAD(&wrapper_clk->list);
  549. INIT_LIST_HEAD(&wrapper_clk->client_list);
  550. list_add_tail(&wrapper_clk->list, &wrapper_clk_list);
  551. }
  552. wrapper_client = kzalloc(sizeof(struct cam_clk_wrapper_client),
  553. GFP_KERNEL);
  554. if (!wrapper_client) {
  555. CAM_ERR(CAM_UTIL, "Failed in allocating new client entry %d",
  556. clk_id);
  557. rc = -ENOMEM;
  558. goto end;
  559. }
  560. wrapper_client->soc_info = soc_info;
  561. wrapper_client->clk = clk;
  562. if (is_src_clk && !wrapper_clk->mmrm_handle) {
  563. wrapper_clk->is_nrt_dev = soc_info->is_nrt_dev;
  564. wrapper_clk->min_clk_rate = min_clk_rate;
  565. wrapper_clk->soc_info = soc_info;
  566. rc = cam_soc_util_register_mmrm_client(clk_id, clk,
  567. wrapper_clk->is_nrt_dev, soc_info, clk_name,
  568. &wrapper_clk->mmrm_handle);
  569. if (rc) {
  570. CAM_ERR(CAM_UTIL,
  571. "Failed in register mmrm client Dev %s clk id %d",
  572. soc_info->dev_name, clk_id);
  573. kfree(wrapper_client);
  574. goto end;
  575. }
  576. }
  577. INIT_LIST_HEAD(&wrapper_client->list);
  578. list_add_tail(&wrapper_client->list, &wrapper_clk->client_list);
  579. wrapper_clk->num_clients++;
  580. CAM_DBG(CAM_UTIL,
  581. "Adding new client %s for clk[%s] id %d, num clients %d",
  582. soc_info->dev_name, clk_name, clk_id, wrapper_clk->num_clients);
  583. end:
  584. mutex_unlock(&wrapper_lock);
  585. return rc;
  586. }
  587. static int cam_soc_util_clk_wrapper_unregister_entry(
  588. uint32_t clk_id, struct cam_hw_soc_info *soc_info)
  589. {
  590. struct cam_clk_wrapper_clk *wrapper_clk;
  591. struct cam_clk_wrapper_client *wrapper_client;
  592. bool clock_found = false;
  593. bool client_found = false;
  594. int rc = 0;
  595. mutex_lock(&wrapper_lock);
  596. list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
  597. CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
  598. wrapper_clk->clk_id, wrapper_clk->num_clients);
  599. if (wrapper_clk->clk_id == clk_id) {
  600. clock_found = true;
  601. list_for_each_entry(wrapper_client,
  602. &wrapper_clk->client_list, list) {
  603. CAM_DBG(CAM_UTIL, "Clk id %d entry client %s",
  604. wrapper_clk->clk_id,
  605. wrapper_client->soc_info->dev_name);
  606. if (wrapper_client->soc_info == soc_info) {
  607. client_found = true;
  608. break;
  609. }
  610. }
  611. break;
  612. }
  613. }
  614. if (!clock_found) {
  615. CAM_ERR(CAM_UTIL, "Shared clk id %d entry not found", clk_id);
  616. rc = -EINVAL;
  617. goto end;
  618. }
  619. if (!client_found) {
  620. CAM_ERR(CAM_UTIL,
  621. "Client %pK for Shared clk id %d entry not found",
  622. soc_info, clk_id);
  623. rc = -EINVAL;
  624. goto end;
  625. }
  626. wrapper_clk->num_clients--;
  627. if (wrapper_clk->mmrm_handle && (wrapper_clk->soc_info == soc_info)) {
  628. cam_soc_util_unregister_mmrm_client(wrapper_clk->mmrm_handle);
  629. wrapper_clk->mmrm_handle = NULL;
  630. wrapper_clk->soc_info = NULL;
  631. }
  632. list_del_init(&wrapper_client->list);
  633. kfree(wrapper_client);
  634. CAM_DBG(CAM_UTIL, "Unregister client %s for clk id %d, num clients %d",
  635. soc_info->dev_name, clk_id, wrapper_clk->num_clients);
  636. if (!wrapper_clk->num_clients) {
  637. list_del_init(&wrapper_clk->list);
  638. kfree(wrapper_clk);
  639. }
  640. end:
  641. mutex_unlock(&wrapper_lock);
  642. return rc;
  643. }
  644. static int cam_soc_util_clk_wrapper_set_clk_rate(
  645. uint32_t clk_id, struct cam_hw_soc_info *soc_info,
  646. struct clk *clk, int64_t clk_rate)
  647. {
  648. struct cam_clk_wrapper_clk *wrapper_clk;
  649. struct cam_clk_wrapper_client *wrapper_client;
  650. bool clk_found = false;
  651. bool client_found = false;
  652. int rc = 0;
  653. int64_t final_clk_rate = 0;
  654. uint32_t active_clients = 0;
  655. if (!soc_info || !clk) {
  656. CAM_ERR(CAM_UTIL, "Invalid param soc_info %pK clk %pK",
  657. soc_info, clk);
  658. return -EINVAL;
  659. }
  660. mutex_lock(&wrapper_lock);
  661. list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
  662. CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
  663. wrapper_clk->clk_id, wrapper_clk->num_clients);
  664. if (wrapper_clk->clk_id == clk_id) {
  665. clk_found = true;
  666. break;
  667. }
  668. }
  669. if (!clk_found) {
  670. CAM_ERR(CAM_UTIL, "Clk entry not found id %d client %s",
  671. clk_id, soc_info->dev_name);
  672. rc = -EINVAL;
  673. goto end;
  674. }
  675. list_for_each_entry(wrapper_client, &wrapper_clk->client_list, list) {
  676. CAM_DBG(CAM_UTIL, "Clk id %d client %s, clk rate %lld",
  677. wrapper_clk->clk_id, wrapper_client->soc_info->dev_name,
  678. wrapper_client->curr_clk_rate);
  679. if (wrapper_client->soc_info == soc_info) {
  680. client_found = true;
  681. CAM_DBG(CAM_UTIL,
  682. "Clk enable clk id %d, client %s curr %ld new %ld",
  683. clk_id, wrapper_client->soc_info->dev_name,
  684. wrapper_client->curr_clk_rate, clk_rate);
  685. wrapper_client->curr_clk_rate = clk_rate;
  686. }
  687. if (wrapper_client->curr_clk_rate > 0)
  688. active_clients++;
  689. if (final_clk_rate < wrapper_client->curr_clk_rate)
  690. final_clk_rate = wrapper_client->curr_clk_rate;
  691. }
  692. if (!client_found) {
  693. CAM_ERR(CAM_UTIL,
  694. "Wrapper clk enable without client entry clk id %d client %s",
  695. clk_id, soc_info->dev_name);
  696. rc = -EINVAL;
  697. goto end;
  698. }
  699. CAM_DBG(CAM_UTIL,
  700. "Clk id %d, client %s, clients rate %ld, curr %ld final %ld",
  701. wrapper_clk->clk_id, soc_info->dev_name, clk_rate,
  702. wrapper_clk->curr_clk_rate, final_clk_rate);
  703. if ((final_clk_rate != wrapper_clk->curr_clk_rate) ||
  704. (active_clients != wrapper_clk->active_clients)) {
  705. bool set_rate_finish = false;
  706. if (!skip_mmrm_set_rate && wrapper_clk->mmrm_handle) {
  707. rc = cam_soc_util_set_rate_through_mmrm(
  708. wrapper_clk->mmrm_handle,
  709. wrapper_clk->is_nrt_dev,
  710. wrapper_clk->min_clk_rate,
  711. final_clk_rate, active_clients);
  712. if (rc) {
  713. CAM_ERR(CAM_UTIL,
  714. "set_rate through mmrm failed clk_id %d, rate=%ld",
  715. wrapper_clk->clk_id, final_clk_rate);
  716. goto end;
  717. }
  718. set_rate_finish = true;
  719. }
  720. if (!set_rate_finish && final_clk_rate &&
  721. (final_clk_rate != wrapper_clk->curr_clk_rate)) {
  722. rc = cam_wrapper_clk_set_rate(clk, final_clk_rate);
  723. if (rc) {
  724. CAM_ERR(CAM_UTIL, "set_rate failed on clk %d",
  725. wrapper_clk->clk_id);
  726. goto end;
  727. }
  728. }
  729. wrapper_clk->curr_clk_rate = final_clk_rate;
  730. wrapper_clk->active_clients = active_clients;
  731. }
  732. end:
  733. mutex_unlock(&wrapper_lock);
  734. return rc;
  735. }
  736. int cam_soc_util_get_clk_level(struct cam_hw_soc_info *soc_info,
  737. int64_t clk_rate, int clk_idx, int32_t *clk_lvl)
  738. {
  739. int i;
  740. long clk_rate_round;
  741. if (!soc_info || (clk_idx < 0) || (clk_idx >= CAM_SOC_MAX_CLK)) {
  742. CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d", clk_idx);
  743. *clk_lvl = -1;
  744. return -EINVAL;
  745. }
  746. clk_rate_round = cam_wrapper_clk_round_rate(
  747. soc_info->clk[clk_idx], clk_rate);
  748. if (clk_rate_round < 0) {
  749. CAM_ERR(CAM_UTIL, "round failed rc = %ld",
  750. clk_rate_round);
  751. *clk_lvl = -1;
  752. return -EINVAL;
  753. }
  754. if (debug_bypass_drivers & CAM_BYPASS_CLKS) {
  755. CAM_WARN(CAM_UTIL, "Bypass get clk level");
  756. *clk_lvl = CAM_NOMINAL_VOTE;
  757. return 0;
  758. }
  759. for (i = 0; i < CAM_MAX_VOTE; i++) {
  760. if ((soc_info->clk_level_valid[i]) &&
  761. (soc_info->clk_rate[i][clk_idx] >=
  762. clk_rate_round)) {
  763. CAM_DBG(CAM_UTIL,
  764. "soc = %d round rate = %ld actual = %lld",
  765. soc_info->clk_rate[i][clk_idx],
  766. clk_rate_round, clk_rate);
  767. *clk_lvl = i;
  768. return 0;
  769. }
  770. }
  771. CAM_WARN(CAM_UTIL, "Invalid clock rate %ld", clk_rate_round);
  772. *clk_lvl = -1;
  773. return -EINVAL;
  774. }
  775. const char *cam_soc_util_get_string_from_level(enum cam_vote_level level)
  776. {
  777. switch (level) {
  778. case CAM_SUSPEND_VOTE:
  779. return "";
  780. case CAM_MINSVS_VOTE:
  781. return "MINSVS[1]";
  782. case CAM_LOWSVS_VOTE:
  783. return "LOWSVS[2]";
  784. case CAM_SVS_VOTE:
  785. return "SVS[3]";
  786. case CAM_SVSL1_VOTE:
  787. return "SVSL1[4]";
  788. case CAM_NOMINAL_VOTE:
  789. return "NOM[5]";
  790. case CAM_NOMINALL1_VOTE:
  791. return "NOML1[6]";
  792. case CAM_TURBO_VOTE:
  793. return "TURBO[7]";
  794. default:
  795. return "";
  796. }
  797. }
  798. /**
  799. * cam_soc_util_get_supported_clk_levels()
  800. *
  801. * @brief: Returns the string of all the supported clk levels for
  802. * the given device
  803. *
  804. * @soc_info: Device soc information
  805. *
  806. * @return: String containing all supported clk levels
  807. */
  808. static const char *cam_soc_util_get_supported_clk_levels(
  809. struct cam_hw_soc_info *soc_info)
  810. {
  811. int i = 0;
  812. scnprintf(supported_clk_info, sizeof(supported_clk_info), "Supported levels: ");
  813. for (i = 0; i < CAM_MAX_VOTE; i++) {
  814. if (soc_info->clk_level_valid[i] == true) {
  815. strlcat(supported_clk_info,
  816. cam_soc_util_get_string_from_level(i),
  817. sizeof(supported_clk_info));
  818. strlcat(supported_clk_info, " ",
  819. sizeof(supported_clk_info));
  820. }
  821. }
  822. strlcat(supported_clk_info, "\n", sizeof(supported_clk_info));
  823. return supported_clk_info;
  824. }
  825. static int cam_soc_util_clk_lvl_options_open(struct inode *inode,
  826. struct file *file)
  827. {
  828. file->private_data = inode->i_private;
  829. return 0;
  830. }
  831. static ssize_t cam_soc_util_clk_lvl_options_read(struct file *file,
  832. char __user *clk_info, size_t size_t, loff_t *loff_t)
  833. {
  834. struct cam_hw_soc_info *soc_info =
  835. (struct cam_hw_soc_info *)file->private_data;
  836. const char *display_string =
  837. cam_soc_util_get_supported_clk_levels(soc_info);
  838. return simple_read_from_buffer(clk_info, size_t, loff_t, display_string,
  839. strlen(display_string));
  840. }
  841. static const struct file_operations cam_soc_util_clk_lvl_options = {
  842. .open = cam_soc_util_clk_lvl_options_open,
  843. .read = cam_soc_util_clk_lvl_options_read,
  844. };
  845. static int cam_soc_util_set_clk_lvl_override(void *data, u64 val)
  846. {
  847. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  848. if ((val <= CAM_SUSPEND_VOTE) || (val >= CAM_MAX_VOTE)) {
  849. CAM_WARN(CAM_UTIL, "Invalid clk lvl override %d", val);
  850. return 0;
  851. }
  852. if (soc_info->clk_level_valid[val])
  853. soc_info->clk_level_override_high = val;
  854. else
  855. soc_info->clk_level_override_high = 0;
  856. return 0;
  857. }
  858. static int cam_soc_util_get_clk_lvl_override(void *data, u64 *val)
  859. {
  860. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  861. *val = soc_info->clk_level_override_high;
  862. return 0;
  863. }
  864. static int cam_soc_util_set_clk_lvl_override_low(void *data, u64 val)
  865. {
  866. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  867. if ((val <= CAM_SUSPEND_VOTE) || (val >= CAM_MAX_VOTE)) {
  868. CAM_WARN(CAM_UTIL, "Invalid clk lvl override %d", val);
  869. return 0;
  870. }
  871. if (soc_info->clk_level_valid[val])
  872. soc_info->clk_level_override_low = val;
  873. else
  874. soc_info->clk_level_override_low = 0;
  875. return 0;
  876. }
  877. static int cam_soc_util_get_clk_lvl_override_low(void *data, u64 *val)
  878. {
  879. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  880. *val = soc_info->clk_level_override_low;
  881. return 0;
  882. }
  883. DEFINE_SIMPLE_ATTRIBUTE(cam_soc_util_clk_lvl_control,
  884. cam_soc_util_get_clk_lvl_override, cam_soc_util_set_clk_lvl_override, "%08llu");
  885. DEFINE_SIMPLE_ATTRIBUTE(cam_soc_util_clk_lvl_control_low,
  886. cam_soc_util_get_clk_lvl_override_low, cam_soc_util_set_clk_lvl_override_low, "%08llu");
  887. /**
  888. * cam_soc_util_create_clk_lvl_debugfs()
  889. *
  890. * @brief: Creates debugfs files to view/control device clk rates
  891. *
  892. * @soc_info: Device soc information
  893. *
  894. * @return: Success or failure
  895. */
  896. static int cam_soc_util_create_clk_lvl_debugfs(struct cam_hw_soc_info *soc_info)
  897. {
  898. int rc = 0;
  899. struct dentry *clkdirptr = NULL;
  900. if (!cam_debugfs_available())
  901. return 0;
  902. if (soc_info->dentry) {
  903. CAM_DBG(CAM_UTIL, "Debugfs entry for %s already exists",
  904. soc_info->dev_name);
  905. goto end;
  906. }
  907. rc = cam_debugfs_lookup_subdir(CAM_CLK_DIRNAME, &clkdirptr);
  908. if (rc) {
  909. rc = cam_debugfs_create_subdir(CAM_CLK_DIRNAME, &clkdirptr);
  910. if (rc) {
  911. CAM_ERR(CAM_UTIL, "DebugFS could not create clk directory!");
  912. rc = -ENOENT;
  913. goto end;
  914. }
  915. }
  916. soc_info->dentry = debugfs_create_dir(soc_info->dev_name, clkdirptr);
  917. if (IS_ERR_OR_NULL(soc_info->dentry)) {
  918. CAM_ERR(CAM_UTIL, "DebugFS could not create directory for dev:%s!",
  919. soc_info->dev_name);
  920. rc = -ENOENT;
  921. goto end;
  922. }
  923. /* Store parent inode for cleanup in caller */
  924. debugfs_create_file("clk_lvl_options", 0444,
  925. soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_options);
  926. debugfs_create_file("clk_lvl_control", 0644,
  927. soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_control);
  928. debugfs_create_file("clk_lvl_control_low", 0644,
  929. soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_control_low);
  930. end:
  931. return rc;
  932. }
  933. int cam_soc_util_get_level_from_string(const char *string,
  934. enum cam_vote_level *level)
  935. {
  936. if (!level)
  937. return -EINVAL;
  938. if (!strcmp(string, "suspend")) {
  939. *level = CAM_SUSPEND_VOTE;
  940. } else if (!strcmp(string, "minsvs")) {
  941. *level = CAM_MINSVS_VOTE;
  942. } else if (!strcmp(string, "lowsvs")) {
  943. *level = CAM_LOWSVS_VOTE;
  944. } else if (!strcmp(string, "svs")) {
  945. *level = CAM_SVS_VOTE;
  946. } else if (!strcmp(string, "svs_l1")) {
  947. *level = CAM_SVSL1_VOTE;
  948. } else if (!strcmp(string, "nominal")) {
  949. *level = CAM_NOMINAL_VOTE;
  950. } else if (!strcmp(string, "nominal_l1")) {
  951. *level = CAM_NOMINALL1_VOTE;
  952. } else if (!strcmp(string, "turbo")) {
  953. *level = CAM_TURBO_VOTE;
  954. } else {
  955. CAM_ERR(CAM_UTIL, "Invalid string %s", string);
  956. return -EINVAL;
  957. }
  958. return 0;
  959. }
  960. /**
  961. * cam_soc_util_get_clk_level_to_apply()
  962. *
  963. * @brief: Get the clock level to apply. If the requested level
  964. * is not valid, bump the level to next available valid
  965. * level. If no higher level found, return failure.
  966. *
  967. * @soc_info: Device soc struct to be populated
  968. * @req_level: Requested level
  969. * @apply_level Level to apply
  970. *
  971. * @return: success or failure
  972. */
  973. static int cam_soc_util_get_clk_level_to_apply(
  974. struct cam_hw_soc_info *soc_info, enum cam_vote_level req_level,
  975. enum cam_vote_level *apply_level)
  976. {
  977. if (req_level >= CAM_MAX_VOTE) {
  978. CAM_ERR(CAM_UTIL, "Invalid clock level parameter %d",
  979. req_level);
  980. return -EINVAL;
  981. }
  982. if (soc_info->clk_level_valid[req_level] == true) {
  983. *apply_level = req_level;
  984. } else {
  985. int i;
  986. for (i = (req_level + 1); i < CAM_MAX_VOTE; i++)
  987. if (soc_info->clk_level_valid[i] == true) {
  988. *apply_level = i;
  989. break;
  990. }
  991. if (i == CAM_MAX_VOTE) {
  992. CAM_ERR(CAM_UTIL,
  993. "No valid clock level found to apply, req=%d",
  994. req_level);
  995. return -EINVAL;
  996. }
  997. }
  998. CAM_DBG(CAM_UTIL, "Req level %s, Applying %s",
  999. cam_soc_util_get_string_from_level(req_level),
  1000. cam_soc_util_get_string_from_level(*apply_level));
  1001. return 0;
  1002. }
  1003. int cam_soc_util_irq_enable(struct cam_hw_soc_info *soc_info)
  1004. {
  1005. int i, rc = 0;
  1006. if (!soc_info) {
  1007. CAM_ERR(CAM_UTIL, "Invalid arguments");
  1008. return -EINVAL;
  1009. }
  1010. for (i = 0; i < soc_info->irq_count; i++) {
  1011. if (soc_info->irq_num[i] < 0) {
  1012. CAM_ERR(CAM_UTIL, "No IRQ line available for irq: %s dev: %s",
  1013. soc_info->irq_name[i], soc_info->dev_name);
  1014. rc = -ENODEV;
  1015. goto disable_irq;
  1016. }
  1017. enable_irq(soc_info->irq_num[i]);
  1018. }
  1019. return rc;
  1020. disable_irq:
  1021. for (i = i - 1; i >= 0; i--)
  1022. disable_irq(soc_info->irq_num[i]);
  1023. return rc;
  1024. }
  1025. int cam_soc_util_irq_disable(struct cam_hw_soc_info *soc_info)
  1026. {
  1027. int i, rc = 0;
  1028. if (!soc_info) {
  1029. CAM_ERR(CAM_UTIL, "Invalid arguments");
  1030. return -EINVAL;
  1031. }
  1032. for (i = 0; i < soc_info->irq_count; i++) {
  1033. if (soc_info->irq_num[i] < 0) {
  1034. CAM_ERR(CAM_UTIL, "No IRQ line available irq: %s dev:",
  1035. soc_info->irq_name[i], soc_info->dev_name);
  1036. rc = -ENODEV;
  1037. continue;
  1038. }
  1039. disable_irq(soc_info->irq_num[i]);
  1040. }
  1041. return rc;
  1042. }
  1043. long cam_soc_util_get_clk_round_rate(struct cam_hw_soc_info *soc_info,
  1044. uint32_t clk_index, unsigned long clk_rate)
  1045. {
  1046. if (!soc_info || (clk_index >= soc_info->num_clk) || (clk_rate == 0)) {
  1047. CAM_ERR(CAM_UTIL, "Invalid input params %pK, %d %lu",
  1048. soc_info, clk_index, clk_rate);
  1049. return clk_rate;
  1050. }
  1051. return cam_wrapper_clk_round_rate(soc_info->clk[clk_index], clk_rate);
  1052. }
  1053. /**
  1054. * cam_soc_util_set_clk_rate()
  1055. *
  1056. * @brief: Sets the given rate for the clk requested for
  1057. *
  1058. * @clk: Clock structure information for which rate is to be set
  1059. * @clk_name: Name of the clock for which rate is being set
  1060. * @clk_rate: Clock rate to be set
  1061. * @shared_clk: Whether this is a shared clk
  1062. * @is_src_clk: Whether this is source clk
  1063. * @clk_id: Clock ID
  1064. * @applied_clk_rate: Final clock rate set to the clk
  1065. *
  1066. * @return: Success or failure
  1067. */
  1068. static int cam_soc_util_set_clk_rate(struct cam_hw_soc_info *soc_info,
  1069. struct clk *clk, const char *clk_name,
  1070. int64_t clk_rate, bool shared_clk, bool is_src_clk, uint32_t clk_id,
  1071. unsigned long *applied_clk_rate)
  1072. {
  1073. int rc = 0;
  1074. long clk_rate_round = -1;
  1075. bool set_rate = false;
  1076. if (!clk_name) {
  1077. CAM_ERR(CAM_UTIL, "Invalid input clk %pK clk_name %pK",
  1078. clk, clk_name);
  1079. return -EINVAL;
  1080. }
  1081. CAM_DBG(CAM_UTIL, "set %s, rate %lld", clk_name, clk_rate);
  1082. if (!clk)
  1083. return 0;
  1084. if (clk_rate > 0) {
  1085. clk_rate_round = cam_wrapper_clk_round_rate(clk, clk_rate);
  1086. CAM_DBG(CAM_UTIL, "new_rate %ld", clk_rate_round);
  1087. if (clk_rate_round < 0) {
  1088. CAM_ERR(CAM_UTIL, "round failed for clock %s rc = %ld",
  1089. clk_name, clk_rate_round);
  1090. return clk_rate_round;
  1091. }
  1092. set_rate = true;
  1093. } else if (clk_rate == INIT_RATE) {
  1094. clk_rate_round = cam_wrapper_clk_get_rate(clk);
  1095. CAM_DBG(CAM_UTIL, "init new_rate %ld", clk_rate_round);
  1096. if (clk_rate_round == 0) {
  1097. clk_rate_round = cam_wrapper_clk_round_rate(clk, 0);
  1098. if (clk_rate_round <= 0) {
  1099. CAM_ERR(CAM_UTIL, "round rate failed on %s",
  1100. clk_name);
  1101. return clk_rate_round;
  1102. }
  1103. }
  1104. set_rate = true;
  1105. }
  1106. if (set_rate) {
  1107. if (shared_clk) {
  1108. CAM_DBG(CAM_UTIL,
  1109. "Dev %s clk %s id %d Set Shared clk %ld",
  1110. soc_info->dev_name, clk_name, clk_id,
  1111. clk_rate_round);
  1112. cam_soc_util_clk_wrapper_set_clk_rate(
  1113. clk_id, soc_info, clk, clk_rate_round);
  1114. } else {
  1115. bool set_rate_finish = false;
  1116. CAM_DBG(CAM_UTIL,
  1117. "Dev %s clk %s clk_id %d src_idx %d src_clk_id %d",
  1118. soc_info->dev_name, clk_name, clk_id,
  1119. soc_info->src_clk_idx,
  1120. (soc_info->src_clk_idx == -1) ? -1 :
  1121. soc_info->clk_id[soc_info->src_clk_idx]);
  1122. if (is_src_clk && soc_info->mmrm_handle &&
  1123. !skip_mmrm_set_rate) {
  1124. uint32_t idx = soc_info->src_clk_idx;
  1125. uint32_t min_level = soc_info->lowest_clk_level;
  1126. rc = cam_soc_util_set_rate_through_mmrm(
  1127. soc_info->mmrm_handle,
  1128. soc_info->is_nrt_dev,
  1129. soc_info->clk_rate[min_level][idx],
  1130. clk_rate_round, 1);
  1131. if (rc) {
  1132. CAM_ERR(CAM_UTIL,
  1133. "set_rate through mmrm failed on %s clk_id %d, rate=%ld",
  1134. clk_name, clk_id,
  1135. clk_rate_round);
  1136. return rc;
  1137. }
  1138. set_rate_finish = true;
  1139. }
  1140. if (!set_rate_finish) {
  1141. rc = cam_wrapper_clk_set_rate(clk, clk_rate_round);
  1142. if (rc) {
  1143. CAM_ERR(CAM_UTIL, "set_rate failed on %s", clk_name);
  1144. return rc;
  1145. }
  1146. }
  1147. }
  1148. }
  1149. if (applied_clk_rate)
  1150. *applied_clk_rate = clk_rate_round;
  1151. return rc;
  1152. }
  1153. int cam_soc_util_set_src_clk_rate(struct cam_hw_soc_info *soc_info, int cesta_client_idx,
  1154. unsigned long clk_rate_high, unsigned long clk_rate_low)
  1155. {
  1156. int rc = 0;
  1157. int i = 0;
  1158. int32_t src_clk_idx;
  1159. int32_t scl_clk_idx;
  1160. struct clk *clk = NULL;
  1161. int32_t apply_level;
  1162. uint32_t clk_level_override_high = 0, clk_level_override_low = 0;
  1163. if (!soc_info || (soc_info->src_clk_idx < 0) ||
  1164. (soc_info->src_clk_idx >= CAM_SOC_MAX_CLK)) {
  1165. CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d",
  1166. soc_info ? soc_info->src_clk_idx : -1);
  1167. return -EINVAL;
  1168. }
  1169. src_clk_idx = soc_info->src_clk_idx;
  1170. clk_level_override_high = soc_info->clk_level_override_high;
  1171. clk_level_override_low = soc_info->clk_level_override_low;
  1172. if (clk_level_override_high && clk_rate_high)
  1173. clk_rate_high = soc_info->clk_rate[clk_level_override_high][src_clk_idx];
  1174. if (clk_level_override_low && clk_rate_low)
  1175. clk_rate_low = soc_info->clk_rate[clk_level_override_low][src_clk_idx];
  1176. clk = soc_info->clk[src_clk_idx];
  1177. rc = cam_soc_util_get_clk_level(soc_info, clk_rate_high, src_clk_idx,
  1178. &apply_level);
  1179. if (rc || (apply_level < 0) || (apply_level >= CAM_MAX_VOTE)) {
  1180. CAM_ERR(CAM_UTIL,
  1181. "set %s, rate %lld dev_name = %s apply level = %d",
  1182. soc_info->clk_name[src_clk_idx], clk_rate_high,
  1183. soc_info->dev_name, apply_level);
  1184. return -EINVAL;
  1185. }
  1186. CAM_DBG(CAM_UTIL,
  1187. "set %s, cesta_client_idx: %d rate [%ld %ld] dev_name = %s apply level = %d",
  1188. soc_info->clk_name[src_clk_idx], cesta_client_idx, clk_rate_high, clk_rate_low,
  1189. soc_info->dev_name, apply_level);
  1190. if ((soc_info->cam_cx_ipeak_enable) && (clk_rate_high > 0)) {
  1191. cam_cx_ipeak_update_vote_cx_ipeak(soc_info,
  1192. apply_level);
  1193. }
  1194. if (soc_info->is_clk_drv_en && CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  1195. rc = cam_soc_util_set_cesta_clk_rate(soc_info, cesta_client_idx, clk_rate_high,
  1196. clk_rate_low,
  1197. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].high,
  1198. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].low);
  1199. if (rc) {
  1200. CAM_ERR(CAM_UTIL,
  1201. "Failed in setting cesta clk rates[high low]:[%ld %ld] client_idx:%d rc:%d",
  1202. clk_rate_high, clk_rate_low, cesta_client_idx, rc);
  1203. return rc;
  1204. }
  1205. goto end;
  1206. }
  1207. rc = cam_soc_util_set_clk_rate(soc_info, clk,
  1208. soc_info->clk_name[src_clk_idx], clk_rate_high,
  1209. CAM_IS_BIT_SET(soc_info->shared_clk_mask, src_clk_idx),
  1210. true, soc_info->clk_id[src_clk_idx],
  1211. &soc_info->applied_src_clk_rates.sw_client);
  1212. if (rc) {
  1213. CAM_ERR(CAM_UTIL,
  1214. "SET_RATE Failed: src clk: %s, rate %lld, dev_name = %s rc: %d",
  1215. soc_info->clk_name[src_clk_idx], clk_rate_high,
  1216. soc_info->dev_name, rc);
  1217. return rc;
  1218. }
  1219. /* set clk rate for scalable clk if available */
  1220. for (i = 0; i < soc_info->scl_clk_count; i++) {
  1221. scl_clk_idx = soc_info->scl_clk_idx[i];
  1222. if (scl_clk_idx < 0) {
  1223. CAM_DBG(CAM_UTIL, "Scl clk index invalid");
  1224. continue;
  1225. }
  1226. clk = soc_info->clk[scl_clk_idx];
  1227. rc = cam_soc_util_set_clk_rate(soc_info, clk,
  1228. soc_info->clk_name[scl_clk_idx],
  1229. soc_info->clk_rate[apply_level][scl_clk_idx],
  1230. CAM_IS_BIT_SET(soc_info->shared_clk_mask, scl_clk_idx),
  1231. false, soc_info->clk_id[scl_clk_idx],
  1232. NULL);
  1233. if (rc) {
  1234. CAM_WARN(CAM_UTIL,
  1235. "SET_RATE Failed: scl clk: %s, rate %d dev_name = %s, rc: %d",
  1236. soc_info->clk_name[scl_clk_idx],
  1237. soc_info->clk_rate[apply_level][scl_clk_idx],
  1238. soc_info->dev_name, rc);
  1239. }
  1240. }
  1241. end:
  1242. return 0;
  1243. }
  1244. int cam_soc_util_put_optional_clk(struct cam_hw_soc_info *soc_info,
  1245. int32_t clk_indx)
  1246. {
  1247. if (clk_indx < 0) {
  1248. CAM_ERR(CAM_UTIL, "Invalid params clk %d", clk_indx);
  1249. return -EINVAL;
  1250. }
  1251. if (CAM_IS_BIT_SET(soc_info->optional_shared_clk_mask, clk_indx))
  1252. cam_soc_util_clk_wrapper_unregister_entry(
  1253. soc_info->optional_clk_id[clk_indx], soc_info);
  1254. cam_wrapper_clk_put(soc_info->optional_clk[clk_indx]);
  1255. soc_info->optional_clk[clk_indx] = NULL;
  1256. return 0;
  1257. }
  1258. static struct clk *cam_soc_util_option_clk_get(struct device_node *np,
  1259. int index, uint32_t *clk_id)
  1260. {
  1261. struct of_phandle_args clkspec;
  1262. struct clk *clk;
  1263. int rc;
  1264. if (index < 0)
  1265. return ERR_PTR(-EINVAL);
  1266. rc = of_parse_phandle_with_args(np, "clocks-option", "#clock-cells",
  1267. index, &clkspec);
  1268. if (rc)
  1269. return ERR_PTR(rc);
  1270. clk = cam_wrapper_of_clk_get_from_provider(&clkspec);
  1271. *clk_id = clkspec.args[0];
  1272. of_node_put(clkspec.np);
  1273. return clk;
  1274. }
  1275. int cam_soc_util_get_option_clk_by_name(struct cam_hw_soc_info *soc_info,
  1276. const char *clk_name, int32_t *clk_index)
  1277. {
  1278. int index = 0;
  1279. int rc = 0;
  1280. struct device_node *of_node = NULL;
  1281. uint32_t shared_clk_val;
  1282. if (!soc_info || !clk_name || !clk_index) {
  1283. CAM_ERR(CAM_UTIL,
  1284. "Invalid params soc_info %pK clk_name %s clk_index %pK",
  1285. soc_info, clk_name, clk_index);
  1286. return -EINVAL;
  1287. }
  1288. of_node = soc_info->dev->of_node;
  1289. index = of_property_match_string(of_node, "clock-names-option",
  1290. clk_name);
  1291. if (index < 0) {
  1292. CAM_DBG(CAM_UTIL, "No clk data for %s", clk_name);
  1293. *clk_index = -1;
  1294. return -EINVAL;
  1295. }
  1296. if (index >= CAM_SOC_MAX_OPT_CLK) {
  1297. CAM_ERR(CAM_UTIL, "Insufficient optional clk entries %d %d",
  1298. index, CAM_SOC_MAX_OPT_CLK);
  1299. return -EINVAL;
  1300. }
  1301. of_property_read_string_index(of_node, "clock-names-option",
  1302. index, &(soc_info->optional_clk_name[index]));
  1303. soc_info->optional_clk[index] = cam_soc_util_option_clk_get(of_node,
  1304. index, &soc_info->optional_clk_id[index]);
  1305. if (IS_ERR(soc_info->optional_clk[index])) {
  1306. CAM_ERR(CAM_UTIL, "No clk named %s found. Dev %s", clk_name,
  1307. soc_info->dev_name);
  1308. *clk_index = -1;
  1309. return -EFAULT;
  1310. }
  1311. *clk_index = index;
  1312. rc = of_property_read_u32_index(of_node, "clock-rates-option",
  1313. index, &soc_info->optional_clk_rate[index]);
  1314. if (rc) {
  1315. CAM_ERR(CAM_UTIL,
  1316. "Error reading clock-rates clk_name %s index %d",
  1317. clk_name, index);
  1318. goto error;
  1319. }
  1320. /*
  1321. * Option clocks are assumed to be available to single Device here.
  1322. * Hence use INIT_RATE instead of NO_SET_RATE.
  1323. */
  1324. soc_info->optional_clk_rate[index] =
  1325. (soc_info->optional_clk_rate[index] == 0) ?
  1326. (int32_t)INIT_RATE : soc_info->optional_clk_rate[index];
  1327. CAM_DBG(CAM_UTIL, "clk_name %s index %d clk_rate %d",
  1328. clk_name, *clk_index, soc_info->optional_clk_rate[index]);
  1329. rc = of_property_read_u32_index(of_node, "shared-clks-option",
  1330. index, &shared_clk_val);
  1331. if (rc) {
  1332. CAM_DBG(CAM_UTIL, "Not shared clk %s index %d",
  1333. clk_name, index);
  1334. } else if (shared_clk_val > 1) {
  1335. CAM_WARN(CAM_UTIL, "Invalid shared clk val %d", shared_clk_val);
  1336. } else {
  1337. CAM_DBG(CAM_UTIL,
  1338. "Dev %s shared clk %s index %d, clk id %d, shared_clk_val %d",
  1339. soc_info->dev_name, clk_name, index,
  1340. soc_info->optional_clk_id[index], shared_clk_val);
  1341. if (shared_clk_val) {
  1342. CAM_SET_BIT(soc_info->optional_shared_clk_mask, index);
  1343. /* Create a wrapper entry if this is a shared clock */
  1344. CAM_DBG(CAM_UTIL,
  1345. "Dev %s, clk %s, id %d register wrapper entry for shared clk",
  1346. soc_info->dev_name,
  1347. soc_info->optional_clk_name[index],
  1348. soc_info->optional_clk_id[index]);
  1349. rc = cam_soc_util_clk_wrapper_register_entry(
  1350. soc_info->optional_clk_id[index],
  1351. soc_info->optional_clk[index], false,
  1352. soc_info,
  1353. soc_info->optional_clk_rate[index],
  1354. soc_info->optional_clk_name[index]);
  1355. if (rc) {
  1356. CAM_ERR(CAM_UTIL,
  1357. "Failed in registering shared clk Dev %s id %d",
  1358. soc_info->dev_name,
  1359. soc_info->optional_clk_id[index]);
  1360. goto error;
  1361. }
  1362. }
  1363. }
  1364. return 0;
  1365. error:
  1366. cam_wrapper_clk_put(soc_info->optional_clk[index]);
  1367. soc_info->optional_clk_rate[index] = 0;
  1368. soc_info->optional_clk[index] = NULL;
  1369. *clk_index = -1;
  1370. return rc;
  1371. }
  1372. int cam_soc_util_clk_enable(struct cam_hw_soc_info *soc_info, int cesta_client_idx,
  1373. bool optional_clk, int32_t clk_idx, int32_t apply_level)
  1374. {
  1375. int rc = 0;
  1376. struct clk *clk;
  1377. const char *clk_name;
  1378. unsigned long clk_rate;
  1379. uint32_t shared_clk_mask;
  1380. uint32_t clk_id;
  1381. bool is_src_clk = false;
  1382. if (!soc_info || (clk_idx < 0) || (apply_level >= CAM_MAX_VOTE)) {
  1383. CAM_ERR(CAM_UTIL, "Invalid param %d %d", clk_idx, apply_level);
  1384. return -EINVAL;
  1385. }
  1386. if (optional_clk) {
  1387. clk = soc_info->optional_clk[clk_idx];
  1388. clk_name = soc_info->optional_clk_name[clk_idx];
  1389. clk_rate = (apply_level == -1) ?
  1390. 0 : soc_info->optional_clk_rate[clk_idx];
  1391. shared_clk_mask = soc_info->optional_shared_clk_mask;
  1392. clk_id = soc_info->optional_clk_id[clk_idx];
  1393. } else {
  1394. clk = soc_info->clk[clk_idx];
  1395. clk_name = soc_info->clk_name[clk_idx];
  1396. clk_rate = (apply_level == -1) ?
  1397. 0 : soc_info->clk_rate[apply_level][clk_idx];
  1398. shared_clk_mask = soc_info->shared_clk_mask;
  1399. clk_id = soc_info->clk_id[clk_idx];
  1400. if (clk_idx == soc_info->src_clk_idx)
  1401. is_src_clk = true;
  1402. }
  1403. if (!clk)
  1404. return 0;
  1405. if (is_src_clk && soc_info->is_clk_drv_en && CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  1406. rc = cam_soc_util_set_cesta_clk_rate(soc_info, cesta_client_idx, clk_rate, clk_rate,
  1407. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].high,
  1408. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].low);
  1409. if (rc) {
  1410. CAM_ERR(CAM_UTIL,
  1411. "[%s] Failed in setting cesta clk rates[high low]:[%ld %ld] client_idx:%d rc:%d",
  1412. soc_info->dev_name, clk_rate, clk_rate, cesta_client_idx, rc);
  1413. return rc;
  1414. }
  1415. rc = cam_soc_util_cesta_channel_switch(cesta_client_idx, soc_info->dev_name);
  1416. if (rc) {
  1417. CAM_ERR(CAM_UTIL,
  1418. "[%s] Failed to apply power states for cesta client:%d rc:%d",
  1419. soc_info->dev_name, cesta_client_idx, rc);
  1420. return rc;
  1421. }
  1422. } else {
  1423. rc = cam_soc_util_set_clk_rate(soc_info, clk, clk_name, clk_rate,
  1424. CAM_IS_BIT_SET(shared_clk_mask, clk_idx), is_src_clk, clk_id,
  1425. &soc_info->applied_src_clk_rates.sw_client);
  1426. if (rc) {
  1427. CAM_ERR(CAM_UTIL, "[%s] Failed in setting clk rate %ld rc:%d",
  1428. soc_info->dev_name, clk_rate, rc);
  1429. return rc;
  1430. }
  1431. }
  1432. CAM_DBG(CAM_UTIL, "[%s] : clk enable %s", soc_info->dev_name, clk_name);
  1433. rc = cam_wrapper_clk_prepare_enable(clk);
  1434. if (rc) {
  1435. CAM_ERR(CAM_UTIL, "enable failed for %s: rc(%d)", clk_name, rc);
  1436. return rc;
  1437. }
  1438. return rc;
  1439. }
  1440. int cam_soc_util_clk_disable(struct cam_hw_soc_info *soc_info, int cesta_client_idx,
  1441. bool optional_clk, int32_t clk_idx)
  1442. {
  1443. int rc = 0;
  1444. struct clk *clk;
  1445. const char *clk_name;
  1446. uint32_t shared_clk_mask;
  1447. uint32_t clk_id;
  1448. if (!soc_info || (clk_idx < 0)) {
  1449. CAM_ERR(CAM_UTIL, "Invalid param %d", clk_idx);
  1450. return -EINVAL;
  1451. }
  1452. if (optional_clk) {
  1453. clk = soc_info->optional_clk[clk_idx];
  1454. clk_name = soc_info->optional_clk_name[clk_idx];
  1455. shared_clk_mask = soc_info->optional_shared_clk_mask;
  1456. clk_id = soc_info->optional_clk_id[clk_idx];
  1457. } else {
  1458. clk = soc_info->clk[clk_idx];
  1459. clk_name = soc_info->clk_name[clk_idx];
  1460. shared_clk_mask = soc_info->shared_clk_mask;
  1461. clk_id = soc_info->clk_id[clk_idx];
  1462. }
  1463. CAM_DBG(CAM_UTIL, "disable %s", clk_name);
  1464. if (!clk)
  1465. return 0;
  1466. cam_wrapper_clk_disable_unprepare(clk);
  1467. if ((clk_idx == soc_info->src_clk_idx) && soc_info->is_clk_drv_en &&
  1468. CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  1469. rc = cam_soc_util_set_cesta_clk_rate(soc_info, cesta_client_idx, 0, 0,
  1470. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].high,
  1471. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].low);
  1472. if (rc) {
  1473. CAM_ERR(CAM_UTIL,
  1474. "Failed in setting cesta clk rates[high low]:[0 0] client_idx:%d rc:%d",
  1475. cesta_client_idx, rc);
  1476. return rc;
  1477. }
  1478. rc = cam_soc_util_cesta_channel_switch(cesta_client_idx, soc_info->dev_name);
  1479. if (rc) {
  1480. CAM_ERR(CAM_CSIPHY,
  1481. "Failed to apply power states for cesta_client_idx:%d rc:%d",
  1482. cesta_client_idx, rc);
  1483. return rc;
  1484. }
  1485. } else {
  1486. if (CAM_IS_BIT_SET(shared_clk_mask, clk_idx)) {
  1487. CAM_DBG(CAM_UTIL,
  1488. "Dev %s clk %s Disabling Shared clk, set 0 rate",
  1489. soc_info->dev_name, clk_name);
  1490. cam_soc_util_clk_wrapper_set_clk_rate(clk_id, soc_info, clk, 0);
  1491. } else if (soc_info->mmrm_handle && (!skip_mmrm_set_rate) &&
  1492. (soc_info->src_clk_idx == clk_idx)) {
  1493. CAM_DBG(CAM_UTIL, "Dev %s Disabling %s clk, set 0 rate",
  1494. soc_info->dev_name, clk_name);
  1495. cam_soc_util_set_rate_through_mmrm(
  1496. soc_info->mmrm_handle,
  1497. soc_info->is_nrt_dev,
  1498. 0, 0, 1);
  1499. }
  1500. }
  1501. return 0;
  1502. }
  1503. /**
  1504. * cam_soc_util_clk_enable_default()
  1505. *
  1506. * @brief: This function enables the default clocks present
  1507. * in soc_info
  1508. *
  1509. * @soc_info: Device soc struct to be populated
  1510. * @cesta_client_idx: CESTA Client idx for hw client based src clocks
  1511. * @clk_level: Clk level to apply while enabling
  1512. *
  1513. * @return: success or failure
  1514. */
  1515. int cam_soc_util_clk_enable_default(struct cam_hw_soc_info *soc_info,
  1516. int cesta_client_idx, enum cam_vote_level clk_level)
  1517. {
  1518. int i, rc = 0;
  1519. enum cam_vote_level apply_level;
  1520. if ((soc_info->num_clk == 0) ||
  1521. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  1522. CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
  1523. soc_info->num_clk);
  1524. return -EINVAL;
  1525. }
  1526. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
  1527. &apply_level);
  1528. if (rc) {
  1529. CAM_ERR(CAM_UTIL, "[%s] : failed to get level clk_level=%d, rc=%d",
  1530. soc_info->dev_name, clk_level, rc);
  1531. return rc;
  1532. }
  1533. if (soc_info->cam_cx_ipeak_enable)
  1534. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
  1535. CAM_DBG(CAM_UTIL, "Dev[%s] : cesta client %d, request level %s, apply level %s",
  1536. soc_info->dev_name, cesta_client_idx,
  1537. cam_soc_util_get_string_from_level(clk_level),
  1538. cam_soc_util_get_string_from_level(apply_level));
  1539. memset(&soc_info->applied_src_clk_rates, 0, sizeof(struct cam_soc_util_clk_rates));
  1540. for (i = 0; i < soc_info->num_clk; i++) {
  1541. rc = cam_soc_util_clk_enable(soc_info, cesta_client_idx, false, i, apply_level);
  1542. if (rc) {
  1543. CAM_ERR(CAM_UTIL,
  1544. "[%s] : failed to enable clk apply_level=%d, rc=%d, cesta_client_idx=%d",
  1545. soc_info->dev_name, apply_level, rc, cesta_client_idx);
  1546. goto clk_disable;
  1547. }
  1548. if (soc_info->cam_cx_ipeak_enable)
  1549. CAM_DBG(CAM_UTIL,
  1550. "dev name = %s clk name = %s idx = %d apply_level = %d clc idx = %d",
  1551. soc_info->dev_name, soc_info->clk_name[i], i, apply_level, i);
  1552. }
  1553. return rc;
  1554. clk_disable:
  1555. if (soc_info->cam_cx_ipeak_enable)
  1556. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  1557. for (i--; i >= 0; i--) {
  1558. cam_soc_util_clk_disable(soc_info, cesta_client_idx, false, i);
  1559. }
  1560. return rc;
  1561. }
  1562. /**
  1563. * cam_soc_util_clk_disable_default()
  1564. *
  1565. * @brief: This function disables the default clocks present
  1566. * in soc_info
  1567. *
  1568. * @soc_info: device soc struct to be populated
  1569. * @cesta_client_idx: CESTA Client idx for hw client based src clocks
  1570. *
  1571. * @return: success or failure
  1572. */
  1573. void cam_soc_util_clk_disable_default(struct cam_hw_soc_info *soc_info,
  1574. int cesta_client_idx)
  1575. {
  1576. int i;
  1577. if (soc_info->num_clk == 0)
  1578. return;
  1579. if (soc_info->cam_cx_ipeak_enable)
  1580. cam_cx_ipeak_unvote_cx_ipeak(soc_info);
  1581. for (i = soc_info->num_clk - 1; i >= 0; i--)
  1582. cam_soc_util_clk_disable(soc_info, cesta_client_idx, false, i);
  1583. }
  1584. /**
  1585. * cam_soc_util_get_dt_clk_info()
  1586. *
  1587. * @brief: Parse the DT and populate the Clock properties
  1588. *
  1589. * @soc_info: device soc struct to be populated
  1590. * @src_clk_str name of src clock that has rate control
  1591. *
  1592. * @return: success or failure
  1593. */
  1594. static int cam_soc_util_get_dt_clk_info(struct cam_hw_soc_info *soc_info)
  1595. {
  1596. struct device_node *of_node = NULL;
  1597. int count;
  1598. int num_clk_rates, num_clk_levels;
  1599. int i, j, rc;
  1600. int32_t num_clk_level_strings;
  1601. const char *src_clk_str = NULL;
  1602. const char *scl_clk_str = NULL;
  1603. const char *clk_control_debugfs = NULL;
  1604. const char *clk_cntl_lvl_string = NULL;
  1605. enum cam_vote_level level;
  1606. int shared_clk_cnt;
  1607. struct of_phandle_args clk_args = {0};
  1608. if (!soc_info || !soc_info->dev)
  1609. return -EINVAL;
  1610. of_node = soc_info->dev->of_node;
  1611. if (!of_property_read_bool(of_node, "use-shared-clk")) {
  1612. CAM_DBG(CAM_UTIL, "No shared clk parameter defined");
  1613. soc_info->use_shared_clk = false;
  1614. } else {
  1615. soc_info->use_shared_clk = true;
  1616. }
  1617. count = of_property_count_strings(of_node, "clock-names");
  1618. CAM_DBG(CAM_UTIL, "E: dev_name = %s count = %d",
  1619. soc_info->dev_name, count);
  1620. if (count > CAM_SOC_MAX_CLK) {
  1621. CAM_ERR(CAM_UTIL, "invalid count of clocks, count=%d", count);
  1622. rc = -EINVAL;
  1623. return rc;
  1624. }
  1625. if (count <= 0) {
  1626. CAM_DBG(CAM_UTIL, "No clock-names found");
  1627. count = 0;
  1628. soc_info->num_clk = count;
  1629. return 0;
  1630. }
  1631. soc_info->num_clk = count;
  1632. for (i = 0; i < count; i++) {
  1633. rc = of_property_read_string_index(of_node, "clock-names",
  1634. i, &(soc_info->clk_name[i]));
  1635. CAM_DBG(CAM_UTIL, "clock-names[%d] = %s",
  1636. i, soc_info->clk_name[i]);
  1637. if (rc) {
  1638. CAM_ERR(CAM_UTIL,
  1639. "i= %d count= %d reading clock-names failed",
  1640. i, count);
  1641. return rc;
  1642. }
  1643. }
  1644. num_clk_rates = of_property_count_u32_elems(of_node, "clock-rates");
  1645. if (num_clk_rates <= 0) {
  1646. CAM_ERR(CAM_UTIL, "reading clock-rates count failed");
  1647. return -EINVAL;
  1648. }
  1649. if ((num_clk_rates % soc_info->num_clk) != 0) {
  1650. CAM_ERR(CAM_UTIL,
  1651. "mismatch clk/rates, No of clocks=%d, No of rates=%d",
  1652. soc_info->num_clk, num_clk_rates);
  1653. return -EINVAL;
  1654. }
  1655. num_clk_levels = (num_clk_rates / soc_info->num_clk);
  1656. num_clk_level_strings = of_property_count_strings(of_node,
  1657. "clock-cntl-level");
  1658. if (num_clk_level_strings != num_clk_levels) {
  1659. CAM_ERR(CAM_UTIL,
  1660. "Mismatch No of levels=%d, No of level string=%d",
  1661. num_clk_levels, num_clk_level_strings);
  1662. return -EINVAL;
  1663. }
  1664. soc_info->lowest_clk_level = CAM_TURBO_VOTE;
  1665. for (i = 0; i < num_clk_levels; i++) {
  1666. rc = of_property_read_string_index(of_node,
  1667. "clock-cntl-level", i, &clk_cntl_lvl_string);
  1668. if (rc) {
  1669. CAM_ERR(CAM_UTIL,
  1670. "Error reading clock-cntl-level, rc=%d", rc);
  1671. return rc;
  1672. }
  1673. rc = cam_soc_util_get_level_from_string(clk_cntl_lvl_string,
  1674. &level);
  1675. if (rc)
  1676. return rc;
  1677. CAM_DBG(CAM_UTIL,
  1678. "[%d] : %s %d", i, clk_cntl_lvl_string, level);
  1679. soc_info->clk_level_valid[level] = true;
  1680. for (j = 0; j < soc_info->num_clk; j++) {
  1681. rc = of_property_read_u32_index(of_node, "clock-rates",
  1682. ((i * soc_info->num_clk) + j),
  1683. &soc_info->clk_rate[level][j]);
  1684. if (rc) {
  1685. CAM_ERR(CAM_UTIL,
  1686. "Error reading clock-rates, rc=%d",
  1687. rc);
  1688. return rc;
  1689. }
  1690. soc_info->clk_rate[level][j] =
  1691. (soc_info->clk_rate[level][j] == 0) ?
  1692. (int32_t)NO_SET_RATE :
  1693. soc_info->clk_rate[level][j];
  1694. CAM_DBG(CAM_UTIL, "soc_info->clk_rate[%d][%d] = %d",
  1695. level, j,
  1696. soc_info->clk_rate[level][j]);
  1697. }
  1698. if ((level > CAM_MINSVS_VOTE) &&
  1699. (level < soc_info->lowest_clk_level))
  1700. soc_info->lowest_clk_level = level;
  1701. }
  1702. soc_info->src_clk_idx = -1;
  1703. rc = of_property_read_string_index(of_node, "src-clock-name", 0,
  1704. &src_clk_str);
  1705. if (rc || !src_clk_str) {
  1706. CAM_DBG(CAM_UTIL, "No src_clk_str found");
  1707. rc = 0;
  1708. goto end;
  1709. }
  1710. for (i = 0; i < soc_info->num_clk; i++) {
  1711. if (strcmp(soc_info->clk_name[i], src_clk_str) == 0) {
  1712. soc_info->src_clk_idx = i;
  1713. CAM_DBG(CAM_UTIL, "src clock = %s, index = %d",
  1714. src_clk_str, i);
  1715. }
  1716. rc = of_parse_phandle_with_args(of_node, "clocks",
  1717. "#clock-cells", i, &clk_args);
  1718. if (rc) {
  1719. CAM_ERR(CAM_CPAS,
  1720. "failed to clock info rc=%d", rc);
  1721. rc = -EINVAL;
  1722. goto end;
  1723. }
  1724. soc_info->clk_id[i] = clk_args.args[0];
  1725. of_node_put(clk_args.np);
  1726. CAM_DBG(CAM_UTIL, "Dev %s clk %s id %d",
  1727. soc_info->dev_name, soc_info->clk_name[i],
  1728. soc_info->clk_id[i]);
  1729. }
  1730. CAM_DBG(CAM_UTIL, "Dev %s src_clk_idx %d, lowest_clk_level %d",
  1731. soc_info->dev_name, soc_info->src_clk_idx,
  1732. soc_info->lowest_clk_level);
  1733. soc_info->shared_clk_mask = 0;
  1734. shared_clk_cnt = of_property_count_u32_elems(of_node, "shared-clks");
  1735. if (shared_clk_cnt <= 0) {
  1736. CAM_DBG(CAM_UTIL, "Dev %s, no shared clks", soc_info->dev_name);
  1737. } else if (shared_clk_cnt != count) {
  1738. CAM_ERR(CAM_UTIL, "Dev %s, incorrect shared clock count %d %d",
  1739. soc_info->dev_name, shared_clk_cnt, count);
  1740. rc = -EINVAL;
  1741. goto end;
  1742. } else {
  1743. uint32_t shared_clk_val;
  1744. for (i = 0; i < shared_clk_cnt; i++) {
  1745. rc = of_property_read_u32_index(of_node,
  1746. "shared-clks", i, &shared_clk_val);
  1747. if (rc || (shared_clk_val > 1)) {
  1748. CAM_ERR(CAM_UTIL,
  1749. "Incorrect shared clk info at %d, val=%d, count=%d",
  1750. i, shared_clk_val, shared_clk_cnt);
  1751. rc = -EINVAL;
  1752. goto end;
  1753. }
  1754. if (shared_clk_val)
  1755. CAM_SET_BIT(soc_info->shared_clk_mask, i);
  1756. }
  1757. CAM_DBG(CAM_UTIL, "Dev %s shared clk mask 0x%x",
  1758. soc_info->dev_name, soc_info->shared_clk_mask);
  1759. }
  1760. /* scalable clk info parsing */
  1761. soc_info->scl_clk_count = 0;
  1762. soc_info->scl_clk_count = of_property_count_strings(of_node,
  1763. "scl-clk-names");
  1764. if ((soc_info->scl_clk_count <= 0) ||
  1765. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  1766. if (soc_info->scl_clk_count == -EINVAL) {
  1767. CAM_DBG(CAM_UTIL, "scl_clk_name prop not avialable");
  1768. } else if ((soc_info->scl_clk_count == -ENODATA) ||
  1769. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  1770. CAM_ERR(CAM_UTIL, "Invalid scl_clk_count: %d",
  1771. soc_info->scl_clk_count);
  1772. return -EINVAL;
  1773. }
  1774. CAM_DBG(CAM_UTIL, "Invalid scl_clk count: %d",
  1775. soc_info->scl_clk_count);
  1776. soc_info->scl_clk_count = -1;
  1777. } else {
  1778. CAM_DBG(CAM_UTIL, "No of scalable clocks: %d",
  1779. soc_info->scl_clk_count);
  1780. for (i = 0; i < soc_info->scl_clk_count; i++) {
  1781. rc = of_property_read_string_index(of_node,
  1782. "scl-clk-names", i,
  1783. (const char **)&scl_clk_str);
  1784. if (rc || !scl_clk_str) {
  1785. CAM_WARN(CAM_UTIL, "scl_clk_str is NULL");
  1786. soc_info->scl_clk_idx[i] = -1;
  1787. continue;
  1788. }
  1789. for (j = 0; j < soc_info->num_clk; j++) {
  1790. if (strnstr(scl_clk_str, soc_info->clk_name[j],
  1791. strlen(scl_clk_str))) {
  1792. soc_info->scl_clk_idx[i] = j;
  1793. CAM_DBG(CAM_UTIL,
  1794. "scl clock = %s, index = %d",
  1795. scl_clk_str, j);
  1796. break;
  1797. }
  1798. }
  1799. }
  1800. }
  1801. rc = of_property_read_string_index(of_node,
  1802. "clock-control-debugfs", 0, &clk_control_debugfs);
  1803. if (rc || !clk_control_debugfs) {
  1804. CAM_DBG(CAM_UTIL, "No clock_control_debugfs property found");
  1805. rc = 0;
  1806. goto end;
  1807. }
  1808. if (strcmp("true", clk_control_debugfs) == 0)
  1809. soc_info->clk_control_enable = true;
  1810. CAM_DBG(CAM_UTIL, "X: dev_name = %s count = %d",
  1811. soc_info->dev_name, count);
  1812. end:
  1813. return rc;
  1814. }
  1815. int cam_soc_util_set_clk_rate_level(struct cam_hw_soc_info *soc_info,
  1816. int cesta_client_idx, enum cam_vote_level clk_level_high,
  1817. enum cam_vote_level clk_level_low, bool do_not_set_src_clk)
  1818. {
  1819. int i, rc = 0;
  1820. enum cam_vote_level apply_level_high;
  1821. enum cam_vote_level apply_level_low = CAM_LOWSVS_VOTE;
  1822. unsigned long applied_clk_rate;
  1823. if ((soc_info->num_clk == 0) ||
  1824. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  1825. CAM_ERR(CAM_UTIL, "Invalid number of clock %d", soc_info->num_clk);
  1826. return -EINVAL;
  1827. }
  1828. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level_high,
  1829. &apply_level_high);
  1830. if (rc) {
  1831. CAM_ERR(CAM_UTIL, "[%s] : failed to get level clk_level_high=%d, rc=%d",
  1832. soc_info->dev_name, clk_level_high, rc);
  1833. return rc;
  1834. }
  1835. if (soc_info->is_clk_drv_en && CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  1836. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level_low,
  1837. &apply_level_low);
  1838. if (rc) {
  1839. CAM_ERR(CAM_UTIL, "[%s] : failed to get level clk_level_low=%d, rc=%d",
  1840. soc_info->dev_name, clk_level_low, rc);
  1841. return rc;
  1842. }
  1843. }
  1844. if (soc_info->cam_cx_ipeak_enable)
  1845. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level_high);
  1846. for (i = 0; i < soc_info->num_clk; i++) {
  1847. if (do_not_set_src_clk && (i == soc_info->src_clk_idx)) {
  1848. CAM_DBG(CAM_UTIL, "Skipping set rate for src clk %s",
  1849. soc_info->clk_name[i]);
  1850. continue;
  1851. }
  1852. if (soc_info->is_clk_drv_en && CAM_IS_VALID_CESTA_IDX(cesta_client_idx) &&
  1853. (i == soc_info->src_clk_idx)) {
  1854. rc = cam_soc_util_set_cesta_clk_rate(soc_info, cesta_client_idx,
  1855. soc_info->clk_rate[apply_level_high][i],
  1856. soc_info->clk_rate[apply_level_low][i],
  1857. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].high,
  1858. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].low);
  1859. if (rc) {
  1860. CAM_ERR(CAM_UTIL,
  1861. "Failed to set the req clk level[high low]: [%s %s] cesta_client_idx: %d",
  1862. cam_soc_util_get_string_from_level(apply_level_high),
  1863. cam_soc_util_get_string_from_level(apply_level_low),
  1864. cesta_client_idx);
  1865. break;
  1866. }
  1867. continue;
  1868. }
  1869. CAM_DBG(CAM_UTIL, "Set rate for clk %s rate %d", soc_info->clk_name[i],
  1870. soc_info->clk_rate[apply_level_high][i]);
  1871. rc = cam_soc_util_set_clk_rate(soc_info, soc_info->clk[i],
  1872. soc_info->clk_name[i],
  1873. soc_info->clk_rate[apply_level_high][i],
  1874. CAM_IS_BIT_SET(soc_info->shared_clk_mask, i),
  1875. (i == soc_info->src_clk_idx) ? true : false,
  1876. soc_info->clk_id[i],
  1877. &applied_clk_rate);
  1878. if (rc < 0) {
  1879. CAM_DBG(CAM_UTIL,
  1880. "dev name = %s clk_name = %s idx = %d apply_level = %s",
  1881. soc_info->dev_name, soc_info->clk_name[i],
  1882. i, cam_soc_util_get_string_from_level(apply_level_high));
  1883. if (soc_info->cam_cx_ipeak_enable)
  1884. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  1885. break;
  1886. }
  1887. if (i == soc_info->src_clk_idx)
  1888. soc_info->applied_src_clk_rates.sw_client = applied_clk_rate;
  1889. }
  1890. return rc;
  1891. };
  1892. static int cam_soc_util_get_dt_gpio_req_tbl(struct device_node *of_node,
  1893. struct cam_soc_gpio_data *gconf, uint16_t *gpio_array,
  1894. uint16_t gpio_array_size)
  1895. {
  1896. int32_t rc = 0, i = 0;
  1897. uint32_t count = 0;
  1898. uint32_t *val_array = NULL;
  1899. if (!of_get_property(of_node, "gpio-req-tbl-num", &count))
  1900. return 0;
  1901. count /= sizeof(uint32_t);
  1902. if (!count) {
  1903. CAM_ERR(CAM_UTIL, "gpio-req-tbl-num 0");
  1904. return 0;
  1905. }
  1906. val_array = kcalloc(count, sizeof(uint32_t), GFP_KERNEL);
  1907. if (!val_array)
  1908. return -ENOMEM;
  1909. gconf->cam_gpio_req_tbl = kcalloc(count, sizeof(struct gpio),
  1910. GFP_KERNEL);
  1911. if (!gconf->cam_gpio_req_tbl) {
  1912. rc = -ENOMEM;
  1913. goto free_val_array;
  1914. }
  1915. gconf->cam_gpio_req_tbl_size = count;
  1916. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-num",
  1917. val_array, count);
  1918. if (rc) {
  1919. CAM_ERR(CAM_UTIL, "failed in reading gpio-req-tbl-num, rc = %d",
  1920. rc);
  1921. goto free_gpio_req_tbl;
  1922. }
  1923. for (i = 0; i < count; i++) {
  1924. if (val_array[i] >= gpio_array_size) {
  1925. CAM_ERR(CAM_UTIL, "gpio req tbl index %d invalid",
  1926. val_array[i]);
  1927. goto free_gpio_req_tbl;
  1928. }
  1929. gconf->cam_gpio_req_tbl[i].gpio = gpio_array[val_array[i]];
  1930. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].gpio = %d", i,
  1931. gconf->cam_gpio_req_tbl[i].gpio);
  1932. }
  1933. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-flags",
  1934. val_array, count);
  1935. if (rc) {
  1936. CAM_ERR(CAM_UTIL, "Failed in gpio-req-tbl-flags, rc %d", rc);
  1937. goto free_gpio_req_tbl;
  1938. }
  1939. for (i = 0; i < count; i++) {
  1940. gconf->cam_gpio_req_tbl[i].flags = val_array[i];
  1941. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].flags = %ld", i,
  1942. gconf->cam_gpio_req_tbl[i].flags);
  1943. }
  1944. for (i = 0; i < count; i++) {
  1945. rc = of_property_read_string_index(of_node,
  1946. "gpio-req-tbl-label", i,
  1947. &gconf->cam_gpio_req_tbl[i].label);
  1948. if (rc) {
  1949. CAM_ERR(CAM_UTIL, "Failed rc %d", rc);
  1950. goto free_gpio_req_tbl;
  1951. }
  1952. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].label = %s", i,
  1953. gconf->cam_gpio_req_tbl[i].label);
  1954. }
  1955. kfree(val_array);
  1956. return rc;
  1957. free_gpio_req_tbl:
  1958. kfree(gconf->cam_gpio_req_tbl);
  1959. free_val_array:
  1960. kfree(val_array);
  1961. gconf->cam_gpio_req_tbl_size = 0;
  1962. return rc;
  1963. }
  1964. static int cam_soc_util_get_gpio_info(struct cam_hw_soc_info *soc_info)
  1965. {
  1966. int32_t rc = 0, i = 0;
  1967. uint16_t *gpio_array = NULL;
  1968. int16_t gpio_array_size = 0;
  1969. struct cam_soc_gpio_data *gconf = NULL;
  1970. struct device_node *of_node = NULL;
  1971. if (!soc_info || !soc_info->dev)
  1972. return -EINVAL;
  1973. of_node = soc_info->dev->of_node;
  1974. /* Validate input parameters */
  1975. if (!of_node) {
  1976. CAM_ERR(CAM_UTIL, "Invalid param of_node");
  1977. return -EINVAL;
  1978. }
  1979. gpio_array_size = of_gpio_count(of_node);
  1980. if (gpio_array_size <= 0)
  1981. return 0;
  1982. CAM_DBG(CAM_UTIL, "gpio count %d", gpio_array_size);
  1983. gpio_array = kcalloc(gpio_array_size, sizeof(uint16_t), GFP_KERNEL);
  1984. if (!gpio_array) {
  1985. rc = -ENOMEM;
  1986. goto err;
  1987. }
  1988. for (i = 0; i < gpio_array_size; i++) {
  1989. gpio_array[i] = of_get_gpio(of_node, i);
  1990. CAM_DBG(CAM_UTIL, "gpio_array[%d] = %d", i, gpio_array[i]);
  1991. }
  1992. gconf = kzalloc(sizeof(*gconf), GFP_KERNEL);
  1993. if (!gconf) {
  1994. rc = -ENOMEM;
  1995. goto free_gpio_array;
  1996. }
  1997. rc = cam_soc_util_get_dt_gpio_req_tbl(of_node, gconf, gpio_array,
  1998. gpio_array_size);
  1999. if (rc) {
  2000. CAM_ERR(CAM_UTIL, "failed in msm_camera_get_dt_gpio_req_tbl");
  2001. goto free_gpio_conf;
  2002. }
  2003. gconf->cam_gpio_common_tbl = kcalloc(gpio_array_size,
  2004. sizeof(struct gpio), GFP_KERNEL);
  2005. if (!gconf->cam_gpio_common_tbl) {
  2006. rc = -ENOMEM;
  2007. goto free_gpio_conf;
  2008. }
  2009. for (i = 0; i < gpio_array_size; i++)
  2010. gconf->cam_gpio_common_tbl[i].gpio = gpio_array[i];
  2011. gconf->cam_gpio_common_tbl_size = gpio_array_size;
  2012. soc_info->gpio_data = gconf;
  2013. kfree(gpio_array);
  2014. return rc;
  2015. free_gpio_conf:
  2016. kfree(gconf);
  2017. free_gpio_array:
  2018. kfree(gpio_array);
  2019. err:
  2020. soc_info->gpio_data = NULL;
  2021. return rc;
  2022. }
  2023. static int cam_soc_util_request_gpio_table(
  2024. struct cam_hw_soc_info *soc_info, bool gpio_en)
  2025. {
  2026. int rc = 0, i = 0;
  2027. uint8_t size = 0;
  2028. struct cam_soc_gpio_data *gpio_conf =
  2029. soc_info->gpio_data;
  2030. struct gpio *gpio_tbl = NULL;
  2031. if (!gpio_conf) {
  2032. CAM_DBG(CAM_UTIL, "No GPIO entry");
  2033. return 0;
  2034. }
  2035. if (gpio_conf->cam_gpio_common_tbl_size <= 0) {
  2036. CAM_ERR(CAM_UTIL, "GPIO table size is invalid");
  2037. return -EINVAL;
  2038. }
  2039. size = gpio_conf->cam_gpio_req_tbl_size;
  2040. gpio_tbl = gpio_conf->cam_gpio_req_tbl;
  2041. if (!gpio_tbl || !size) {
  2042. CAM_ERR(CAM_UTIL, "Invalid gpio_tbl %pK / size %d",
  2043. gpio_tbl, size);
  2044. return -EINVAL;
  2045. }
  2046. for (i = 0; i < size; i++) {
  2047. CAM_DBG(CAM_UTIL, "i=%d, gpio=%d dir=%ld", i,
  2048. gpio_tbl[i].gpio, gpio_tbl[i].flags);
  2049. }
  2050. if (gpio_en) {
  2051. for (i = 0; i < size; i++) {
  2052. rc = gpio_request_one(gpio_tbl[i].gpio,
  2053. gpio_tbl[i].flags, gpio_tbl[i].label);
  2054. if (rc) {
  2055. /*
  2056. * After GPIO request fails, contine to
  2057. * apply new gpios, outout a error message
  2058. * for driver bringup debug
  2059. */
  2060. CAM_ERR(CAM_UTIL, "gpio %d:%s request fails",
  2061. gpio_tbl[i].gpio, gpio_tbl[i].label);
  2062. }
  2063. }
  2064. } else {
  2065. gpio_free_array(gpio_tbl, size);
  2066. }
  2067. return rc;
  2068. }
  2069. static int cam_soc_util_get_dt_regulator_info
  2070. (struct cam_hw_soc_info *soc_info)
  2071. {
  2072. int rc = 0, count = 0, i = 0;
  2073. struct device_node *of_node = NULL;
  2074. if (!soc_info || !soc_info->dev) {
  2075. CAM_ERR(CAM_UTIL, "Invalid parameters");
  2076. return -EINVAL;
  2077. }
  2078. of_node = soc_info->dev->of_node;
  2079. soc_info->num_rgltr = 0;
  2080. count = of_property_count_strings(of_node, "regulator-names");
  2081. if (count != -EINVAL) {
  2082. if (count <= 0) {
  2083. CAM_ERR(CAM_UTIL, "no regulators found");
  2084. return -EINVAL;
  2085. }
  2086. soc_info->num_rgltr = count;
  2087. } else {
  2088. CAM_DBG(CAM_UTIL, "No regulators node found");
  2089. return 0;
  2090. }
  2091. if (soc_info->num_rgltr > CAM_SOC_MAX_REGULATOR) {
  2092. CAM_ERR(CAM_UTIL, "Invalid regulator count:%d",
  2093. soc_info->num_rgltr);
  2094. return -EINVAL;
  2095. }
  2096. for (i = 0; i < soc_info->num_rgltr; i++) {
  2097. rc = of_property_read_string_index(of_node,
  2098. "regulator-names", i, &soc_info->rgltr_name[i]);
  2099. CAM_DBG(CAM_UTIL, "rgltr_name[%d] = %s",
  2100. i, soc_info->rgltr_name[i]);
  2101. if (rc) {
  2102. CAM_ERR(CAM_UTIL, "no regulator resource at cnt=%d", i);
  2103. return -ENODEV;
  2104. }
  2105. }
  2106. if (!of_property_read_bool(of_node, "rgltr-cntrl-support")) {
  2107. CAM_DBG(CAM_UTIL, "No regulator control parameter defined");
  2108. soc_info->rgltr_ctrl_support = false;
  2109. return 0;
  2110. }
  2111. soc_info->rgltr_ctrl_support = true;
  2112. rc = of_property_read_u32_array(of_node, "rgltr-min-voltage",
  2113. soc_info->rgltr_min_volt, soc_info->num_rgltr);
  2114. if (rc) {
  2115. CAM_ERR(CAM_UTIL, "No minimum volatage value found, rc=%d", rc);
  2116. return -EINVAL;
  2117. }
  2118. rc = of_property_read_u32_array(of_node, "rgltr-max-voltage",
  2119. soc_info->rgltr_max_volt, soc_info->num_rgltr);
  2120. if (rc) {
  2121. CAM_ERR(CAM_UTIL, "No maximum volatage value found, rc=%d", rc);
  2122. return -EINVAL;
  2123. }
  2124. rc = of_property_read_u32_array(of_node, "rgltr-load-current",
  2125. soc_info->rgltr_op_mode, soc_info->num_rgltr);
  2126. if (rc) {
  2127. CAM_ERR(CAM_UTIL, "No Load curent found rc=%d", rc);
  2128. return -EINVAL;
  2129. }
  2130. return rc;
  2131. }
  2132. #ifdef CONFIG_CAM_PRESIL
  2133. static uint32_t next_dummy_irq_line_num = 0x000f;
  2134. struct resource dummy_irq_line[512];
  2135. #endif
  2136. int cam_soc_util_get_dt_properties(struct cam_hw_soc_info *soc_info)
  2137. {
  2138. struct device_node *of_node = NULL;
  2139. int count = 0, i = 0, rc = 0;
  2140. if (!soc_info || !soc_info->dev)
  2141. return -EINVAL;
  2142. of_node = soc_info->dev->of_node;
  2143. rc = of_property_read_u32(of_node, "cell-index", &soc_info->index);
  2144. if (rc) {
  2145. CAM_ERR(CAM_UTIL, "device %s failed to read cell-index",
  2146. soc_info->dev_name);
  2147. return rc;
  2148. }
  2149. count = of_property_count_strings(of_node, "reg-names");
  2150. if (count <= 0) {
  2151. CAM_DBG(CAM_UTIL, "no reg-names found for: %s",
  2152. soc_info->dev_name);
  2153. count = 0;
  2154. }
  2155. soc_info->num_mem_block = count;
  2156. for (i = 0; i < soc_info->num_mem_block; i++) {
  2157. rc = of_property_read_string_index(of_node, "reg-names", i,
  2158. &soc_info->mem_block_name[i]);
  2159. if (rc) {
  2160. CAM_ERR(CAM_UTIL, "failed to read reg-names at %d", i);
  2161. return rc;
  2162. }
  2163. soc_info->mem_block[i] =
  2164. platform_get_resource_byname(soc_info->pdev,
  2165. IORESOURCE_MEM, soc_info->mem_block_name[i]);
  2166. if (!soc_info->mem_block[i]) {
  2167. CAM_ERR(CAM_UTIL, "no mem resource by name %s",
  2168. soc_info->mem_block_name[i]);
  2169. rc = -ENODEV;
  2170. return rc;
  2171. }
  2172. }
  2173. rc = of_property_read_string(of_node, "label", &soc_info->label_name);
  2174. if (rc)
  2175. CAM_DBG(CAM_UTIL, "Label is not available in the node: %d", rc);
  2176. if (soc_info->num_mem_block > 0) {
  2177. rc = of_property_read_u32_array(of_node, "reg-cam-base",
  2178. soc_info->mem_block_cam_base, soc_info->num_mem_block);
  2179. if (rc) {
  2180. CAM_ERR(CAM_UTIL, "Error reading register offsets");
  2181. return rc;
  2182. }
  2183. }
  2184. count = of_property_count_strings(of_node, "interrupt-names");
  2185. if (count <= 0) {
  2186. CAM_DBG(CAM_UTIL, "No interrupt line present for: %s", soc_info->dev_name);
  2187. soc_info->irq_count = 0;
  2188. } else {
  2189. if (count > CAM_SOC_MAX_IRQ_LINES_PER_DEV) {
  2190. CAM_ERR(CAM_UTIL,
  2191. "Number of interrupt: %d exceeds maximum allowable interrupts: %d",
  2192. count, CAM_SOC_MAX_IRQ_LINES_PER_DEV);
  2193. return -EINVAL;
  2194. }
  2195. soc_info->irq_count = count;
  2196. for (i = 0; i < soc_info->irq_count; i++) {
  2197. rc = of_property_read_string_index(of_node, "interrupt-names",
  2198. i, &soc_info->irq_name[i]);
  2199. if (rc) {
  2200. CAM_ERR(CAM_UTIL, "failed to read interrupt name at %d", i);
  2201. return rc;
  2202. }
  2203. }
  2204. rc = cam_compat_util_get_irq(soc_info);
  2205. if (rc < 0) {
  2206. CAM_ERR(CAM_UTIL, "get irq resource failed: %d for: %s",
  2207. rc, soc_info->dev_name);
  2208. #ifndef CONFIG_CAM_PRESIL
  2209. return rc;
  2210. #else
  2211. /* Pre-sil for new devices not present on old */
  2212. for (i = 0; i < soc_info->irq_count; i++) {
  2213. soc_info->irq_line[i] =
  2214. &dummy_irq_line[next_dummy_irq_line_num++];
  2215. CAM_DBG(CAM_PRESIL,
  2216. "interrupt line for dev %s irq name %s number %d",
  2217. soc_info->dev_name, soc_info->irq_name[i],
  2218. soc_info->irq_line[i]->start);
  2219. }
  2220. #endif
  2221. }
  2222. }
  2223. rc = of_property_read_string_index(of_node, "compatible", 0,
  2224. (const char **)&soc_info->compatible);
  2225. if (rc)
  2226. CAM_DBG(CAM_UTIL, "No compatible string present for: %s",
  2227. soc_info->dev_name);
  2228. soc_info->is_nrt_dev = false;
  2229. if (of_property_read_bool(of_node, "nrt-device"))
  2230. soc_info->is_nrt_dev = true;
  2231. CAM_DBG(CAM_UTIL, "Dev %s, nrt_dev %d",
  2232. soc_info->dev_name, soc_info->is_nrt_dev);
  2233. rc = cam_soc_util_get_dt_regulator_info(soc_info);
  2234. if (rc)
  2235. return rc;
  2236. rc = cam_soc_util_get_dt_clk_info(soc_info);
  2237. if (rc)
  2238. return rc;
  2239. rc = cam_soc_util_get_gpio_info(soc_info);
  2240. if (rc)
  2241. return rc;
  2242. if (of_find_property(of_node, "qcom,cam-cx-ipeak", NULL))
  2243. rc = cam_cx_ipeak_register_cx_ipeak(soc_info);
  2244. return rc;
  2245. }
  2246. /**
  2247. * cam_soc_util_get_regulator()
  2248. *
  2249. * @brief: Get regulator resource named vdd
  2250. *
  2251. * @dev: Device associated with regulator
  2252. * @reg: Return pointer to be filled with regulator on success
  2253. * @rgltr_name: Name of regulator to get
  2254. *
  2255. * @return: 0 for Success, negative value for failure
  2256. */
  2257. static int cam_soc_util_get_regulator(struct device *dev,
  2258. struct regulator **reg, const char *rgltr_name)
  2259. {
  2260. int rc = 0;
  2261. *reg = cam_wrapper_regulator_get(dev, rgltr_name);
  2262. if (IS_ERR_OR_NULL(*reg)) {
  2263. rc = PTR_ERR(*reg);
  2264. rc = rc ? rc : -EINVAL;
  2265. CAM_ERR(CAM_UTIL, "Regulator %s get failed %d", rgltr_name, rc);
  2266. *reg = NULL;
  2267. }
  2268. return rc;
  2269. }
  2270. int cam_soc_util_regulator_disable(struct regulator *rgltr,
  2271. const char *rgltr_name, uint32_t rgltr_min_volt,
  2272. uint32_t rgltr_max_volt, uint32_t rgltr_op_mode,
  2273. uint32_t rgltr_delay_ms)
  2274. {
  2275. int32_t rc = 0;
  2276. if (!rgltr) {
  2277. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  2278. return -EINVAL;
  2279. }
  2280. rc = cam_wrapper_regulator_disable(rgltr);
  2281. if (rc) {
  2282. CAM_ERR(CAM_UTIL, "%s regulator disable failed", rgltr_name);
  2283. return rc;
  2284. }
  2285. if (rgltr_delay_ms > 20)
  2286. msleep(rgltr_delay_ms);
  2287. else if (rgltr_delay_ms)
  2288. usleep_range(rgltr_delay_ms * 1000,
  2289. (rgltr_delay_ms * 1000) + 1000);
  2290. if (cam_wrapper_regulator_count_voltages(rgltr) > 0) {
  2291. cam_wrapper_regulator_set_load(rgltr, 0);
  2292. cam_wrapper_regulator_set_voltage(rgltr, 0, rgltr_max_volt);
  2293. }
  2294. return rc;
  2295. }
  2296. int cam_soc_util_regulator_enable(struct regulator *rgltr,
  2297. const char *rgltr_name,
  2298. uint32_t rgltr_min_volt, uint32_t rgltr_max_volt,
  2299. uint32_t rgltr_op_mode, uint32_t rgltr_delay)
  2300. {
  2301. int32_t rc = 0;
  2302. if (!rgltr) {
  2303. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  2304. return -EINVAL;
  2305. }
  2306. if (cam_wrapper_regulator_count_voltages(rgltr) > 0) {
  2307. CAM_DBG(CAM_UTIL, "[%s] voltage min=%d, max=%d",
  2308. rgltr_name, rgltr_min_volt, rgltr_max_volt);
  2309. rc = cam_wrapper_regulator_set_voltage(
  2310. rgltr, rgltr_min_volt, rgltr_max_volt);
  2311. if (rc) {
  2312. CAM_ERR(CAM_UTIL, "%s set voltage failed", rgltr_name);
  2313. return rc;
  2314. }
  2315. rc = cam_wrapper_regulator_set_load(rgltr, rgltr_op_mode);
  2316. if (rc) {
  2317. CAM_ERR(CAM_UTIL, "%s set optimum mode failed",
  2318. rgltr_name);
  2319. return rc;
  2320. }
  2321. }
  2322. rc = cam_wrapper_regulator_enable(rgltr);
  2323. if (rc) {
  2324. CAM_ERR(CAM_UTIL, "%s regulator_enable failed", rgltr_name);
  2325. return rc;
  2326. }
  2327. if (rgltr_delay > 20)
  2328. msleep(rgltr_delay);
  2329. else if (rgltr_delay)
  2330. usleep_range(rgltr_delay * 1000,
  2331. (rgltr_delay * 1000) + 1000);
  2332. return rc;
  2333. }
  2334. int cam_soc_util_select_pinctrl_state(struct cam_hw_soc_info *soc_info,
  2335. int pctrl_idx, bool active)
  2336. {
  2337. int rc = 0;
  2338. struct cam_soc_pinctrl_info *pctrl_info = &soc_info->pinctrl_info;
  2339. if (pctrl_idx >= CAM_SOC_MAX_PINCTRL_MAP) {
  2340. CAM_ERR(CAM_UTIL, "Invalid Map idx: %d max supported: %d",
  2341. pctrl_idx, CAM_SOC_MAX_PINCTRL_MAP);
  2342. return -EINVAL;
  2343. }
  2344. if (pctrl_info->pctrl_state[pctrl_idx].gpio_state_active &&
  2345. active &&
  2346. !pctrl_info->pctrl_state[pctrl_idx].is_active) {
  2347. rc = pinctrl_select_state(pctrl_info->pinctrl,
  2348. pctrl_info->pctrl_state[pctrl_idx].gpio_state_active);
  2349. if (rc)
  2350. CAM_ERR(CAM_UTIL,
  2351. "Pinctrl active state transition failed: rc: %d",
  2352. rc);
  2353. else {
  2354. pctrl_info->pctrl_state[pctrl_idx].is_active = true;
  2355. CAM_DBG(CAM_UTIL, "Pctrl_idx: %d is in active state",
  2356. pctrl_idx);
  2357. }
  2358. }
  2359. if (pctrl_info->pctrl_state[pctrl_idx].gpio_state_suspend &&
  2360. !active &&
  2361. pctrl_info->pctrl_state[pctrl_idx].is_active) {
  2362. rc = pinctrl_select_state(pctrl_info->pinctrl,
  2363. pctrl_info->pctrl_state[pctrl_idx].gpio_state_suspend);
  2364. if (rc)
  2365. CAM_ERR(CAM_UTIL,
  2366. "Pinctrl suspend state transition failed: rc: %d",
  2367. rc);
  2368. else {
  2369. pctrl_info->pctrl_state[pctrl_idx].is_active = false;
  2370. CAM_DBG(CAM_UTIL, "Pctrl_idx: %d is in suspend state",
  2371. pctrl_idx);
  2372. }
  2373. }
  2374. return rc;
  2375. }
  2376. static int cam_soc_util_request_pinctrl(
  2377. struct cam_hw_soc_info *soc_info)
  2378. {
  2379. struct cam_soc_pinctrl_info *device_pctrl = &soc_info->pinctrl_info;
  2380. struct device *dev = soc_info->dev;
  2381. struct device_node *of_node = dev->of_node;
  2382. uint32_t i = 0;
  2383. int rc = 0;
  2384. const char *name;
  2385. uint32_t idx;
  2386. char pctrl_active[50];
  2387. char pctrl_suspend[50];
  2388. int32_t num_of_map_idx = 0;
  2389. int32_t num_of_string = 0;
  2390. device_pctrl->pinctrl = devm_pinctrl_get(dev);
  2391. if (IS_ERR_OR_NULL(device_pctrl->pinctrl)) {
  2392. CAM_DBG(CAM_UTIL, "Pinctrl not available");
  2393. device_pctrl->pinctrl = NULL;
  2394. return 0;
  2395. }
  2396. num_of_map_idx = of_property_count_u32_elems(
  2397. of_node, "pctrl-idx-mapping");
  2398. if (num_of_map_idx <= 0) {
  2399. CAM_ERR(CAM_UTIL,
  2400. "Reading pctrl-idx-mapping failed");
  2401. return -EINVAL;
  2402. }
  2403. num_of_string = of_property_count_strings(
  2404. of_node, "pctrl-map-names");
  2405. if (num_of_string <= 0) {
  2406. CAM_ERR(CAM_UTIL, "no pinctrl-mapping found for: %s",
  2407. soc_info->dev_name);
  2408. device_pctrl->pinctrl = NULL;
  2409. return -EINVAL;
  2410. }
  2411. if (num_of_map_idx != num_of_string) {
  2412. CAM_ERR(CAM_UTIL,
  2413. "Incorrect inputs mapping-idx count: %d mapping-names: %d",
  2414. num_of_map_idx, num_of_string);
  2415. device_pctrl->pinctrl = NULL;
  2416. return -EINVAL;
  2417. }
  2418. if (num_of_map_idx > CAM_SOC_MAX_PINCTRL_MAP) {
  2419. CAM_ERR(CAM_UTIL, "Invalid mapping %u max supported: %d",
  2420. num_of_map_idx, CAM_SOC_MAX_PINCTRL_MAP);
  2421. return -EINVAL;
  2422. }
  2423. for (i = 0; i < num_of_map_idx; i++) {
  2424. of_property_read_u32_index(of_node,
  2425. "pctrl-idx-mapping", i, &idx);
  2426. if (idx >= CAM_SOC_MAX_PINCTRL_MAP) {
  2427. CAM_ERR(CAM_UTIL, "Invalid Index: %d max supported: %d",
  2428. idx, CAM_SOC_MAX_PINCTRL_MAP);
  2429. return -EINVAL;
  2430. }
  2431. rc = of_property_read_string_index(
  2432. of_node, "pctrl-map-names", i, &name);
  2433. if (rc) {
  2434. CAM_ERR(CAM_UTIL,
  2435. "failed to read pinctrl-mapping at %d", i);
  2436. return rc;
  2437. }
  2438. snprintf(pctrl_active, sizeof(pctrl_active),
  2439. "%s%s", name, "_active");
  2440. CAM_DBG(CAM_UTIL, "pctrl_active at index: %d name: %s",
  2441. i, pctrl_active);
  2442. snprintf(pctrl_suspend, sizeof(pctrl_suspend),
  2443. "%s%s", name, "_suspend");
  2444. CAM_DBG(CAM_UTIL, "pctrl_suspend at index: %d name: %s",
  2445. i, pctrl_suspend);
  2446. device_pctrl->pctrl_state[idx].gpio_state_active =
  2447. pinctrl_lookup_state(device_pctrl->pinctrl,
  2448. pctrl_active);
  2449. if (IS_ERR_OR_NULL(
  2450. device_pctrl->pctrl_state[idx].gpio_state_active)) {
  2451. CAM_ERR(CAM_UTIL,
  2452. "Failed to get the active state pinctrl handle");
  2453. device_pctrl->pctrl_state[idx].gpio_state_active =
  2454. NULL;
  2455. return -EINVAL;
  2456. }
  2457. device_pctrl->pctrl_state[idx].gpio_state_suspend =
  2458. pinctrl_lookup_state(device_pctrl->pinctrl,
  2459. pctrl_suspend);
  2460. if (IS_ERR_OR_NULL(
  2461. device_pctrl->pctrl_state[idx].gpio_state_suspend)) {
  2462. CAM_ERR(CAM_UTIL,
  2463. "Failed to get the active state pinctrl handle");
  2464. device_pctrl->pctrl_state[idx].gpio_state_suspend = NULL;
  2465. return -EINVAL;
  2466. }
  2467. }
  2468. return 0;
  2469. }
  2470. static void cam_soc_util_release_pinctrl(struct cam_hw_soc_info *soc_info)
  2471. {
  2472. if (soc_info->pinctrl_info.pinctrl)
  2473. devm_pinctrl_put(soc_info->pinctrl_info.pinctrl);
  2474. }
  2475. static void cam_soc_util_regulator_disable_default(
  2476. struct cam_hw_soc_info *soc_info)
  2477. {
  2478. int j = 0;
  2479. uint32_t num_rgltr = soc_info->num_rgltr;
  2480. for (j = num_rgltr-1; j >= 0; j--) {
  2481. if (soc_info->rgltr_ctrl_support == true) {
  2482. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  2483. soc_info->rgltr_name[j],
  2484. soc_info->rgltr_min_volt[j],
  2485. soc_info->rgltr_max_volt[j],
  2486. soc_info->rgltr_op_mode[j],
  2487. soc_info->rgltr_delay[j]);
  2488. } else {
  2489. if (soc_info->rgltr[j])
  2490. cam_wrapper_regulator_disable(soc_info->rgltr[j]);
  2491. }
  2492. }
  2493. }
  2494. static int cam_soc_util_regulator_enable_default(
  2495. struct cam_hw_soc_info *soc_info)
  2496. {
  2497. int j = 0, rc = 0;
  2498. uint32_t num_rgltr = soc_info->num_rgltr;
  2499. if (num_rgltr > CAM_SOC_MAX_REGULATOR) {
  2500. CAM_ERR(CAM_UTIL,
  2501. "%s has invalid regulator number %d",
  2502. soc_info->dev_name, num_rgltr);
  2503. return -EINVAL;
  2504. }
  2505. for (j = 0; j < num_rgltr; j++) {
  2506. CAM_DBG(CAM_UTIL, "[%s] : start regulator %s enable, rgltr_ctrl_support %d",
  2507. soc_info->dev_name, soc_info->rgltr_name[j], soc_info->rgltr_ctrl_support);
  2508. if (soc_info->rgltr_ctrl_support == true) {
  2509. rc = cam_soc_util_regulator_enable(soc_info->rgltr[j],
  2510. soc_info->rgltr_name[j],
  2511. soc_info->rgltr_min_volt[j],
  2512. soc_info->rgltr_max_volt[j],
  2513. soc_info->rgltr_op_mode[j],
  2514. soc_info->rgltr_delay[j]);
  2515. } else {
  2516. if (soc_info->rgltr[j])
  2517. rc = cam_wrapper_regulator_enable(soc_info->rgltr[j]);
  2518. }
  2519. if (rc) {
  2520. CAM_ERR(CAM_UTIL, "%s enable failed",
  2521. soc_info->rgltr_name[j]);
  2522. goto disable_rgltr;
  2523. }
  2524. }
  2525. return rc;
  2526. disable_rgltr:
  2527. for (j--; j >= 0; j--) {
  2528. if (soc_info->rgltr_ctrl_support == true) {
  2529. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  2530. soc_info->rgltr_name[j],
  2531. soc_info->rgltr_min_volt[j],
  2532. soc_info->rgltr_max_volt[j],
  2533. soc_info->rgltr_op_mode[j],
  2534. soc_info->rgltr_delay[j]);
  2535. } else {
  2536. if (soc_info->rgltr[j])
  2537. cam_wrapper_regulator_disable(soc_info->rgltr[j]);
  2538. }
  2539. }
  2540. return rc;
  2541. }
  2542. static bool cam_soc_util_is_presil_address_space(unsigned long mem_block_start)
  2543. {
  2544. if(mem_block_start >= CAM_SS_START_PRESIL && mem_block_start < CAM_SS_START)
  2545. return true;
  2546. return false;
  2547. }
  2548. #ifndef CONFIG_CAM_PRESIL
  2549. void __iomem * cam_soc_util_get_mem_base(
  2550. unsigned long mem_block_start,
  2551. unsigned long mem_block_size,
  2552. const char *mem_block_name,
  2553. uint32_t reserve_mem)
  2554. {
  2555. void __iomem * mem_base;
  2556. if (reserve_mem) {
  2557. if (!request_mem_region(mem_block_start,
  2558. mem_block_size,
  2559. mem_block_name)) {
  2560. CAM_ERR(CAM_UTIL,
  2561. "Error Mem region request Failed:%s",
  2562. mem_block_name);
  2563. return NULL;
  2564. }
  2565. }
  2566. mem_base = ioremap(mem_block_start, mem_block_size);
  2567. if (!mem_base) {
  2568. CAM_ERR(CAM_UTIL, "get mem base failed");
  2569. }
  2570. return mem_base;
  2571. }
  2572. int cam_soc_util_request_irq(struct device *dev,
  2573. unsigned int irq_line_start,
  2574. irq_handler_t handler,
  2575. unsigned long irqflags,
  2576. const char *irq_name,
  2577. void *irq_data,
  2578. unsigned long mem_block_start)
  2579. {
  2580. int rc;
  2581. rc = devm_request_irq(dev,
  2582. irq_line_start,
  2583. handler,
  2584. IRQF_TRIGGER_RISING,
  2585. irq_name,
  2586. irq_data);
  2587. if (rc) {
  2588. CAM_ERR(CAM_UTIL, "irq request fail rc %d", rc);
  2589. return -EBUSY;
  2590. }
  2591. disable_irq(irq_line_start);
  2592. return rc;
  2593. }
  2594. #else
  2595. void __iomem * cam_soc_util_get_mem_base(
  2596. unsigned long mem_block_start,
  2597. unsigned long mem_block_size,
  2598. const char *mem_block_name,
  2599. uint32_t reserve_mem)
  2600. {
  2601. void __iomem * mem_base;
  2602. if(cam_soc_util_is_presil_address_space(mem_block_start))
  2603. mem_base = (void __iomem *)mem_block_start;
  2604. else {
  2605. if (reserve_mem) {
  2606. if (!request_mem_region(mem_block_start,
  2607. mem_block_size,
  2608. mem_block_name)) {
  2609. CAM_ERR(CAM_UTIL,
  2610. "Error Mem region request Failed:%s",
  2611. mem_block_name);
  2612. return NULL;
  2613. }
  2614. }
  2615. mem_base = ioremap(mem_block_start, mem_block_size);
  2616. }
  2617. if (!mem_base) {
  2618. CAM_ERR(CAM_UTIL, "get mem base failed");
  2619. }
  2620. return mem_base;
  2621. }
  2622. int cam_soc_util_request_irq(struct device *dev,
  2623. unsigned int irq_line_start,
  2624. irq_handler_t handler,
  2625. unsigned long irqflags,
  2626. const char *irq_name,
  2627. void *irq_data,
  2628. unsigned long mem_block_start)
  2629. {
  2630. int rc;
  2631. if(cam_soc_util_is_presil_address_space(mem_block_start)) {
  2632. rc = devm_request_irq(dev,
  2633. irq_line_start,
  2634. handler,
  2635. irqflags,
  2636. irq_name,
  2637. irq_data);
  2638. if (rc) {
  2639. CAM_ERR(CAM_UTIL, "presil irq request fail");
  2640. return -EBUSY;
  2641. }
  2642. disable_irq(irq_line_start);
  2643. rc = !(cam_presil_subscribe_device_irq(irq_line_start,
  2644. handler, irq_data, irq_name));
  2645. CAM_DBG(CAM_PRESIL, "Subscribe presil IRQ: rc=%d NUM=%d Name=%s handler=0x%x",
  2646. rc, irq_line_start, irq_name, handler);
  2647. if (rc) {
  2648. CAM_ERR(CAM_UTIL, "presil irq request fail");
  2649. return -EBUSY;
  2650. }
  2651. } else {
  2652. rc = devm_request_irq(dev,
  2653. irq_line_start,
  2654. handler,
  2655. irqflags,
  2656. irq_name,
  2657. irq_data);
  2658. if (rc) {
  2659. CAM_ERR(CAM_UTIL, "irq request fail");
  2660. return -EBUSY;
  2661. }
  2662. disable_irq(irq_line_start);
  2663. CAM_INFO(CAM_UTIL, "Subscribe for non-presil IRQ success");
  2664. }
  2665. CAM_INFO(CAM_UTIL, "returning IRQ for mem_block_start 0x%0x rc %d",
  2666. mem_block_start, rc);
  2667. return rc;
  2668. }
  2669. #endif
  2670. int cam_soc_util_request_platform_resource(
  2671. struct cam_hw_soc_info *soc_info,
  2672. irq_handler_t handler, void **irq_data)
  2673. {
  2674. int i = 0, rc = 0;
  2675. if (!soc_info || !soc_info->dev) {
  2676. CAM_ERR(CAM_UTIL, "Invalid parameters");
  2677. return -EINVAL;
  2678. }
  2679. if (unlikely(soc_info->irq_count > CAM_SOC_MAX_IRQ_LINES_PER_DEV)) {
  2680. CAM_ERR(CAM_UTIL, "Invalid irq count: %u Max IRQ per device: %d",
  2681. soc_info->irq_count, CAM_SOC_MAX_IRQ_LINES_PER_DEV);
  2682. return -EINVAL;
  2683. }
  2684. for (i = 0; i < soc_info->num_mem_block; i++) {
  2685. soc_info->reg_map[i].mem_base = cam_soc_util_get_mem_base(
  2686. soc_info->mem_block[i]->start,
  2687. resource_size(soc_info->mem_block[i]),
  2688. soc_info->mem_block_name[i],
  2689. soc_info->reserve_mem);
  2690. if (!soc_info->reg_map[i].mem_base) {
  2691. CAM_ERR(CAM_UTIL, "i= %d base NULL", i);
  2692. rc = -ENOMEM;
  2693. goto unmap_base;
  2694. }
  2695. soc_info->reg_map[i].mem_cam_base =
  2696. soc_info->mem_block_cam_base[i];
  2697. soc_info->reg_map[i].size =
  2698. resource_size(soc_info->mem_block[i]);
  2699. soc_info->num_reg_map++;
  2700. }
  2701. for (i = 0; i < soc_info->num_rgltr; i++) {
  2702. if (soc_info->rgltr_name[i] == NULL) {
  2703. CAM_ERR(CAM_UTIL, "can't find regulator name");
  2704. goto put_regulator;
  2705. }
  2706. rc = cam_soc_util_get_regulator(soc_info->dev,
  2707. &soc_info->rgltr[i],
  2708. soc_info->rgltr_name[i]);
  2709. if (rc)
  2710. goto put_regulator;
  2711. }
  2712. for (i = 0; i < soc_info->irq_count; i++) {
  2713. rc = cam_soc_util_request_irq(soc_info->dev, soc_info->irq_num[i],
  2714. handler, IRQF_TRIGGER_RISING, soc_info->irq_name[i],
  2715. irq_data[i], soc_info->mem_block[0]->start);
  2716. if (rc) {
  2717. CAM_ERR(CAM_UTIL, "irq request fail for irq name: %s dev: %s",
  2718. soc_info->irq_name[i], soc_info->dev_name);
  2719. rc = -EBUSY;
  2720. goto put_irq;
  2721. }
  2722. soc_info->irq_data[i] = irq_data[i];
  2723. }
  2724. /* Get Clock */
  2725. for (i = 0; i < soc_info->num_clk; i++) {
  2726. soc_info->clk[i] = cam_wrapper_clk_get(soc_info->dev,
  2727. soc_info->clk_name[i]);
  2728. if (IS_ERR(soc_info->clk[i])) {
  2729. CAM_ERR(CAM_UTIL, "get failed for %s",
  2730. soc_info->clk_name[i]);
  2731. rc = -ENOENT;
  2732. goto put_clk;
  2733. } else if (!soc_info->clk[i]) {
  2734. CAM_DBG(CAM_UTIL, "%s handle is NULL skip get",
  2735. soc_info->clk_name[i]);
  2736. continue;
  2737. }
  2738. /* Create a wrapper entry if this is a shared clock */
  2739. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i)) {
  2740. uint32_t min_level = soc_info->lowest_clk_level;
  2741. CAM_DBG(CAM_UTIL,
  2742. "Dev %s, clk %s, id %d register wrapper entry for shared clk",
  2743. soc_info->dev_name, soc_info->clk_name[i],
  2744. soc_info->clk_id[i]);
  2745. rc = cam_soc_util_clk_wrapper_register_entry(
  2746. soc_info->clk_id[i], soc_info->clk[i],
  2747. (i == soc_info->src_clk_idx) ? true : false,
  2748. soc_info, soc_info->clk_rate[min_level][i],
  2749. soc_info->clk_name[i]);
  2750. if (rc) {
  2751. CAM_ERR(CAM_UTIL,
  2752. "Failed in registering shared clk Dev %s id %d",
  2753. soc_info->dev_name,
  2754. soc_info->clk_id[i]);
  2755. cam_wrapper_clk_put(soc_info->clk[i]);
  2756. soc_info->clk[i] = NULL;
  2757. goto put_clk;
  2758. }
  2759. } else if (i == soc_info->src_clk_idx) {
  2760. rc = cam_soc_util_register_mmrm_client(
  2761. soc_info->clk_id[i], soc_info->clk[i],
  2762. soc_info->is_nrt_dev,
  2763. soc_info, soc_info->clk_name[i],
  2764. &soc_info->mmrm_handle);
  2765. if (rc) {
  2766. CAM_ERR(CAM_UTIL,
  2767. "Failed in register mmrm client Dev %s clk id %d",
  2768. soc_info->dev_name,
  2769. soc_info->clk_id[i]);
  2770. cam_wrapper_clk_put(soc_info->clk[i]);
  2771. soc_info->clk[i] = NULL;
  2772. goto put_clk;
  2773. }
  2774. }
  2775. }
  2776. rc = cam_soc_util_request_pinctrl(soc_info);
  2777. if (rc) {
  2778. CAM_ERR(CAM_UTIL, "Failed in requesting Pinctrl, rc: %d", rc);
  2779. goto put_clk;
  2780. }
  2781. rc = cam_soc_util_request_gpio_table(soc_info, true);
  2782. if (rc) {
  2783. CAM_ERR(CAM_UTIL, "Failed in request gpio table, rc=%d", rc);
  2784. goto put_clk;
  2785. }
  2786. if (soc_info->clk_control_enable)
  2787. cam_soc_util_create_clk_lvl_debugfs(soc_info);
  2788. return rc;
  2789. put_clk:
  2790. if (soc_info->mmrm_handle) {
  2791. cam_soc_util_unregister_mmrm_client(soc_info->mmrm_handle);
  2792. soc_info->mmrm_handle = NULL;
  2793. }
  2794. for (i = i - 1; i >= 0; i--) {
  2795. if (soc_info->clk[i]) {
  2796. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i))
  2797. cam_soc_util_clk_wrapper_unregister_entry(
  2798. soc_info->clk_id[i], soc_info);
  2799. cam_wrapper_clk_put(soc_info->clk[i]);
  2800. soc_info->clk[i] = NULL;
  2801. }
  2802. }
  2803. put_irq:
  2804. if (i == -1)
  2805. i = soc_info->irq_count;
  2806. for (i = i - 1; i >= 0; i--) {
  2807. if (soc_info->irq_num[i] > 0)
  2808. disable_irq(soc_info->irq_num[i]);
  2809. }
  2810. put_regulator:
  2811. if (i == -1)
  2812. i = soc_info->num_rgltr;
  2813. for (i = i - 1; i >= 0; i--) {
  2814. if (soc_info->rgltr[i]) {
  2815. cam_wrapper_regulator_disable(soc_info->rgltr[i]);
  2816. cam_wrapper_regulator_put(soc_info->rgltr[i]);
  2817. soc_info->rgltr[i] = NULL;
  2818. }
  2819. }
  2820. unmap_base:
  2821. if (i == -1)
  2822. i = soc_info->num_reg_map;
  2823. for (i = i - 1; i >= 0; i--) {
  2824. if (soc_info->reserve_mem)
  2825. release_mem_region(soc_info->mem_block[i]->start,
  2826. resource_size(soc_info->mem_block[i]));
  2827. iounmap(soc_info->reg_map[i].mem_base);
  2828. soc_info->reg_map[i].mem_base = NULL;
  2829. soc_info->reg_map[i].size = 0;
  2830. }
  2831. return rc;
  2832. }
  2833. int cam_soc_util_release_platform_resource(struct cam_hw_soc_info *soc_info)
  2834. {
  2835. int i;
  2836. bool b_ret = false;
  2837. if (!soc_info || !soc_info->dev) {
  2838. CAM_ERR(CAM_UTIL, "Invalid parameter");
  2839. return -EINVAL;
  2840. }
  2841. if (soc_info->mmrm_handle) {
  2842. cam_soc_util_unregister_mmrm_client(soc_info->mmrm_handle);
  2843. soc_info->mmrm_handle = NULL;
  2844. }
  2845. for (i = soc_info->num_clk - 1; i >= 0; i--) {
  2846. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i))
  2847. cam_soc_util_clk_wrapper_unregister_entry(
  2848. soc_info->clk_id[i], soc_info);
  2849. if (!soc_info->clk[i]) {
  2850. CAM_DBG(CAM_UTIL, "%s handle is NULL skip put",
  2851. soc_info->clk_name[i]);
  2852. continue;
  2853. }
  2854. cam_wrapper_clk_put(soc_info->clk[i]);
  2855. soc_info->clk[i] = NULL;
  2856. }
  2857. for (i = soc_info->num_rgltr - 1; i >= 0; i--) {
  2858. if (soc_info->rgltr[i]) {
  2859. cam_wrapper_regulator_put(soc_info->rgltr[i]);
  2860. soc_info->rgltr[i] = NULL;
  2861. }
  2862. }
  2863. for (i = soc_info->num_reg_map - 1; i >= 0; i--) {
  2864. iounmap(soc_info->reg_map[i].mem_base);
  2865. soc_info->reg_map[i].mem_base = NULL;
  2866. soc_info->reg_map[i].size = 0;
  2867. }
  2868. for (i = soc_info->irq_count; i >= 0; i--) {
  2869. if (soc_info->irq_num[i] > 0) {
  2870. if (cam_presil_mode_enabled()) {
  2871. if (cam_soc_util_is_presil_address_space(
  2872. soc_info->mem_block[0]->start)) {
  2873. b_ret = cam_presil_unsubscribe_device_irq(
  2874. soc_info->irq_line[i]->start);
  2875. CAM_DBG(CAM_PRESIL,
  2876. "UnSubscribe IRQ: Ret=%d NUM=%d Name=%s",
  2877. b_ret, soc_info->irq_line[i]->start,
  2878. soc_info->irq_name[i]);
  2879. }
  2880. }
  2881. disable_irq(soc_info->irq_num[i]);
  2882. }
  2883. }
  2884. cam_soc_util_release_pinctrl(soc_info);
  2885. /* release for gpio */
  2886. cam_soc_util_request_gpio_table(soc_info, false);
  2887. soc_info->dentry = NULL;
  2888. return 0;
  2889. }
  2890. int cam_soc_util_enable_platform_resource(struct cam_hw_soc_info *soc_info,
  2891. int cesta_client_idx, bool enable_clocks, enum cam_vote_level clk_level,
  2892. bool irq_enable)
  2893. {
  2894. int rc = 0, i;
  2895. if (!soc_info)
  2896. return -EINVAL;
  2897. rc = cam_soc_util_regulator_enable_default(soc_info);
  2898. if (rc) {
  2899. CAM_ERR(CAM_UTIL, "Regulators enable failed");
  2900. return rc;
  2901. }
  2902. if (enable_clocks) {
  2903. rc = cam_soc_util_clk_enable_default(soc_info, cesta_client_idx, clk_level);
  2904. if (rc)
  2905. goto disable_regulator;
  2906. }
  2907. if (irq_enable) {
  2908. for (i = 0; i < soc_info->irq_count; i++) {
  2909. if (soc_info->irq_num[i] < 0) {
  2910. CAM_ERR(CAM_UTIL, "No IRQ line available for irq: %s dev: %s",
  2911. soc_info->irq_name[i], soc_info->dev_name);
  2912. rc = -ENODEV;
  2913. goto disable_irq;
  2914. }
  2915. enable_irq(soc_info->irq_num[i]);
  2916. }
  2917. }
  2918. return rc;
  2919. disable_irq:
  2920. if (irq_enable) {
  2921. for (i = i - 1; i >= 0; i--)
  2922. disable_irq(soc_info->irq_num[i]);
  2923. }
  2924. if (enable_clocks)
  2925. cam_soc_util_clk_disable_default(soc_info, cesta_client_idx);
  2926. disable_regulator:
  2927. cam_soc_util_regulator_disable_default(soc_info);
  2928. return rc;
  2929. }
  2930. int cam_soc_util_disable_platform_resource(struct cam_hw_soc_info *soc_info,
  2931. int cesta_client_idx, bool disable_clocks, bool disable_irq)
  2932. {
  2933. int rc = 0;
  2934. if (!soc_info)
  2935. return -EINVAL;
  2936. if (disable_irq)
  2937. rc |= cam_soc_util_irq_disable(soc_info);
  2938. if (disable_clocks)
  2939. cam_soc_util_clk_disable_default(soc_info, cesta_client_idx);
  2940. cam_soc_util_regulator_disable_default(soc_info);
  2941. return rc;
  2942. }
  2943. int cam_soc_util_reg_dump(struct cam_hw_soc_info *soc_info,
  2944. uint32_t base_index, uint32_t offset, int size)
  2945. {
  2946. void __iomem *base_addr = NULL;
  2947. CAM_DBG(CAM_UTIL, "base_idx %u size=%d", base_index, size);
  2948. if (!soc_info || base_index >= soc_info->num_reg_map ||
  2949. size <= 0 || (offset + size) >=
  2950. CAM_SOC_GET_REG_MAP_SIZE(soc_info, base_index))
  2951. return -EINVAL;
  2952. base_addr = CAM_SOC_GET_REG_MAP_START(soc_info, base_index);
  2953. /*
  2954. * All error checking already done above,
  2955. * hence ignoring the return value below.
  2956. */
  2957. cam_io_dump(base_addr, offset, size);
  2958. return 0;
  2959. }
  2960. static int cam_soc_util_dump_cont_reg_range(
  2961. struct cam_hw_soc_info *soc_info,
  2962. struct cam_reg_range_read_desc *reg_read, uint32_t base_idx,
  2963. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  2964. {
  2965. int i = 0, rc = 0;
  2966. uint32_t write_idx = 0;
  2967. if (!soc_info || !dump_out_buf || !reg_read || !cmd_buf_end) {
  2968. CAM_ERR(CAM_UTIL,
  2969. "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK cmd_buf_end: %pK",
  2970. soc_info, dump_out_buf, reg_read, cmd_buf_end);
  2971. rc = -EINVAL;
  2972. goto end;
  2973. }
  2974. if ((reg_read->num_values) && ((reg_read->num_values > U32_MAX / 2) ||
  2975. (sizeof(uint32_t) > ((U32_MAX -
  2976. sizeof(struct cam_reg_dump_out_buffer) -
  2977. dump_out_buf->bytes_written) /
  2978. (reg_read->num_values * 2))))) {
  2979. CAM_ERR(CAM_UTIL,
  2980. "Integer Overflow bytes_written: [%u] num_values: [%u]",
  2981. dump_out_buf->bytes_written, reg_read->num_values);
  2982. rc = -EOVERFLOW;
  2983. goto end;
  2984. }
  2985. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  2986. (uintptr_t)(sizeof(struct cam_reg_dump_out_buffer)
  2987. - sizeof(uint32_t) + dump_out_buf->bytes_written +
  2988. (reg_read->num_values * 2 * sizeof(uint32_t)))) {
  2989. CAM_ERR(CAM_UTIL,
  2990. "Insufficient space in out buffer num_values: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  2991. reg_read->num_values, cmd_buf_end,
  2992. (uintptr_t)dump_out_buf);
  2993. rc = -EINVAL;
  2994. goto end;
  2995. }
  2996. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  2997. for (i = 0; i < reg_read->num_values; i++) {
  2998. if ((reg_read->offset + (i * sizeof(uint32_t))) >
  2999. (uint32_t)soc_info->reg_map[base_idx].size) {
  3000. CAM_ERR(CAM_UTIL,
  3001. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  3002. (reg_read->offset + (i * sizeof(uint32_t))),
  3003. (uint32_t)soc_info->reg_map[base_idx].size);
  3004. rc = -EINVAL;
  3005. goto end;
  3006. }
  3007. dump_out_buf->dump_data[write_idx++] = reg_read->offset +
  3008. (i * sizeof(uint32_t));
  3009. dump_out_buf->dump_data[write_idx++] =
  3010. cam_soc_util_r(soc_info, base_idx,
  3011. (reg_read->offset + (i * sizeof(uint32_t))));
  3012. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  3013. }
  3014. end:
  3015. return rc;
  3016. }
  3017. static int cam_soc_util_dump_dmi_reg_range(
  3018. struct cam_hw_soc_info *soc_info,
  3019. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  3020. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  3021. {
  3022. int i = 0, rc = 0;
  3023. uint32_t write_idx = 0;
  3024. if (!soc_info || !dump_out_buf || !dmi_read || !cmd_buf_end) {
  3025. CAM_ERR(CAM_UTIL,
  3026. "Invalid input args soc_info: %pK, dump_out_buffer: %pK",
  3027. soc_info, dump_out_buf);
  3028. rc = -EINVAL;
  3029. goto end;
  3030. }
  3031. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  3032. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  3033. CAM_ERR(CAM_UTIL,
  3034. "Invalid number of requested writes, pre: %d post: %d",
  3035. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  3036. rc = -EINVAL;
  3037. goto end;
  3038. }
  3039. if ((dmi_read->num_pre_writes + dmi_read->dmi_data_read.num_values)
  3040. && ((dmi_read->num_pre_writes > U32_MAX / 2) ||
  3041. (dmi_read->dmi_data_read.num_values > U32_MAX / 2) ||
  3042. ((dmi_read->num_pre_writes * 2) > U32_MAX -
  3043. (dmi_read->dmi_data_read.num_values * 2)) ||
  3044. (sizeof(uint32_t) > ((U32_MAX -
  3045. sizeof(struct cam_reg_dump_out_buffer) -
  3046. dump_out_buf->bytes_written) / ((dmi_read->num_pre_writes +
  3047. dmi_read->dmi_data_read.num_values) * 2))))) {
  3048. CAM_ERR(CAM_UTIL,
  3049. "Integer Overflow bytes_written: [%u] num_pre_writes: [%u] num_values: [%u]",
  3050. dump_out_buf->bytes_written, dmi_read->num_pre_writes,
  3051. dmi_read->dmi_data_read.num_values);
  3052. rc = -EOVERFLOW;
  3053. goto end;
  3054. }
  3055. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  3056. (uintptr_t)(
  3057. sizeof(struct cam_reg_dump_out_buffer) - sizeof(uint32_t) +
  3058. (dump_out_buf->bytes_written +
  3059. (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
  3060. (dmi_read->dmi_data_read.num_values * 2 *
  3061. sizeof(uint32_t))))) {
  3062. CAM_ERR(CAM_UTIL,
  3063. "Insufficient space in out buffer num_read_val: [%d] num_write_val: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  3064. dmi_read->dmi_data_read.num_values,
  3065. dmi_read->num_pre_writes, cmd_buf_end,
  3066. (uintptr_t)dump_out_buf);
  3067. rc = -EINVAL;
  3068. goto end;
  3069. }
  3070. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  3071. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  3072. if (dmi_read->pre_read_config[i].offset >
  3073. (uint32_t)soc_info->reg_map[base_idx].size) {
  3074. CAM_ERR(CAM_UTIL,
  3075. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  3076. dmi_read->pre_read_config[i].offset,
  3077. (uint32_t)soc_info->reg_map[base_idx].size);
  3078. rc = -EINVAL;
  3079. goto end;
  3080. }
  3081. cam_soc_util_w_mb(soc_info, base_idx,
  3082. dmi_read->pre_read_config[i].offset,
  3083. dmi_read->pre_read_config[i].value);
  3084. dump_out_buf->dump_data[write_idx++] =
  3085. dmi_read->pre_read_config[i].offset;
  3086. dump_out_buf->dump_data[write_idx++] =
  3087. dmi_read->pre_read_config[i].value;
  3088. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  3089. }
  3090. if (dmi_read->dmi_data_read.offset >
  3091. (uint32_t)soc_info->reg_map[base_idx].size) {
  3092. CAM_ERR(CAM_UTIL,
  3093. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  3094. dmi_read->dmi_data_read.offset,
  3095. (uint32_t)soc_info->reg_map[base_idx].size);
  3096. rc = -EINVAL;
  3097. goto end;
  3098. }
  3099. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  3100. dump_out_buf->dump_data[write_idx++] =
  3101. dmi_read->dmi_data_read.offset;
  3102. dump_out_buf->dump_data[write_idx++] =
  3103. cam_soc_util_r_mb(soc_info, base_idx,
  3104. dmi_read->dmi_data_read.offset);
  3105. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  3106. }
  3107. for (i = 0; i < dmi_read->num_post_writes; i++) {
  3108. if (dmi_read->post_read_config[i].offset >
  3109. (uint32_t)soc_info->reg_map[base_idx].size) {
  3110. CAM_ERR(CAM_UTIL,
  3111. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  3112. dmi_read->post_read_config[i].offset,
  3113. (uint32_t)soc_info->reg_map[base_idx].size);
  3114. rc = -EINVAL;
  3115. goto end;
  3116. }
  3117. cam_soc_util_w_mb(soc_info, base_idx,
  3118. dmi_read->post_read_config[i].offset,
  3119. dmi_read->post_read_config[i].value);
  3120. }
  3121. end:
  3122. return rc;
  3123. }
  3124. static int cam_soc_util_dump_dmi_reg_range_user_buf(
  3125. struct cam_hw_soc_info *soc_info,
  3126. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  3127. struct cam_hw_soc_dump_args *dump_args)
  3128. {
  3129. int i;
  3130. int rc;
  3131. size_t buf_len = 0;
  3132. uint8_t *dst;
  3133. size_t remain_len;
  3134. uint32_t min_len;
  3135. uint32_t *waddr, *start;
  3136. uintptr_t cpu_addr;
  3137. struct cam_hw_soc_dump_header *hdr;
  3138. if (!soc_info || !dump_args || !dmi_read) {
  3139. CAM_ERR(CAM_UTIL,
  3140. "Invalid input args soc_info: %pK, dump_args: %pK",
  3141. soc_info, dump_args);
  3142. rc = -EINVAL;
  3143. goto end;
  3144. }
  3145. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  3146. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  3147. CAM_ERR(CAM_UTIL,
  3148. "Invalid number of requested writes, pre: %d post: %d",
  3149. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  3150. rc = -EINVAL;
  3151. goto end;
  3152. }
  3153. rc = cam_mem_get_cpu_buf(dump_args->buf_handle, &cpu_addr, &buf_len);
  3154. if (rc) {
  3155. CAM_ERR(CAM_UTIL, "Invalid handle %u rc %d",
  3156. dump_args->buf_handle, rc);
  3157. goto end;
  3158. }
  3159. if (buf_len <= dump_args->offset) {
  3160. CAM_WARN(CAM_UTIL, "Dump offset overshoot offset %zu len %zu",
  3161. dump_args->offset, buf_len);
  3162. rc = -ENOSPC;
  3163. goto end;
  3164. }
  3165. remain_len = buf_len - dump_args->offset;
  3166. min_len = (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
  3167. (dmi_read->dmi_data_read.num_values * 2 * sizeof(uint32_t)) +
  3168. sizeof(uint32_t);
  3169. if (remain_len < min_len) {
  3170. CAM_WARN(CAM_UTIL,
  3171. "Dump Buffer exhaust read %d write %d remain %zu min %u",
  3172. dmi_read->dmi_data_read.num_values,
  3173. dmi_read->num_pre_writes, remain_len,
  3174. min_len);
  3175. rc = -ENOSPC;
  3176. goto end;
  3177. }
  3178. dst = (uint8_t *)cpu_addr + dump_args->offset;
  3179. hdr = (struct cam_hw_soc_dump_header *)dst;
  3180. memset(hdr, 0, sizeof(struct cam_hw_soc_dump_header));
  3181. scnprintf(hdr->tag, CAM_SOC_HW_DUMP_TAG_MAX_LEN,
  3182. "DMI_DUMP:");
  3183. waddr = (uint32_t *)(dst + sizeof(struct cam_hw_soc_dump_header));
  3184. start = waddr;
  3185. hdr->word_size = sizeof(uint32_t);
  3186. *waddr = soc_info->index;
  3187. waddr++;
  3188. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  3189. if (dmi_read->pre_read_config[i].offset >
  3190. (uint32_t)soc_info->reg_map[base_idx].size) {
  3191. CAM_ERR(CAM_UTIL,
  3192. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  3193. dmi_read->pre_read_config[i].offset,
  3194. (uint32_t)soc_info->reg_map[base_idx].size);
  3195. rc = -EINVAL;
  3196. goto end;
  3197. }
  3198. cam_soc_util_w_mb(soc_info, base_idx,
  3199. dmi_read->pre_read_config[i].offset,
  3200. dmi_read->pre_read_config[i].value);
  3201. *waddr++ = dmi_read->pre_read_config[i].offset;
  3202. *waddr++ = dmi_read->pre_read_config[i].value;
  3203. }
  3204. if (dmi_read->dmi_data_read.offset >
  3205. (uint32_t)soc_info->reg_map[base_idx].size) {
  3206. CAM_ERR(CAM_UTIL,
  3207. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  3208. dmi_read->dmi_data_read.offset,
  3209. (uint32_t)soc_info->reg_map[base_idx].size);
  3210. rc = -EINVAL;
  3211. goto end;
  3212. }
  3213. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  3214. *waddr++ = dmi_read->dmi_data_read.offset;
  3215. *waddr++ = cam_soc_util_r_mb(soc_info, base_idx,
  3216. dmi_read->dmi_data_read.offset);
  3217. }
  3218. for (i = 0; i < dmi_read->num_post_writes; i++) {
  3219. if (dmi_read->post_read_config[i].offset >
  3220. (uint32_t)soc_info->reg_map[base_idx].size) {
  3221. CAM_ERR(CAM_UTIL,
  3222. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  3223. dmi_read->post_read_config[i].offset,
  3224. (uint32_t)soc_info->reg_map[base_idx].size);
  3225. rc = -EINVAL;
  3226. goto end;
  3227. }
  3228. cam_soc_util_w_mb(soc_info, base_idx,
  3229. dmi_read->post_read_config[i].offset,
  3230. dmi_read->post_read_config[i].value);
  3231. }
  3232. hdr->size = (waddr - start) * hdr->word_size;
  3233. dump_args->offset += hdr->size +
  3234. sizeof(struct cam_hw_soc_dump_header);
  3235. end:
  3236. return rc;
  3237. }
  3238. static int cam_soc_util_dump_cont_reg_range_user_buf(
  3239. struct cam_hw_soc_info *soc_info,
  3240. struct cam_reg_range_read_desc *reg_read,
  3241. uint32_t base_idx,
  3242. struct cam_hw_soc_dump_args *dump_args)
  3243. {
  3244. int i;
  3245. int rc = 0;
  3246. size_t buf_len;
  3247. uint8_t *dst;
  3248. size_t remain_len;
  3249. uint32_t min_len;
  3250. uint32_t *waddr, *start;
  3251. uintptr_t cpu_addr;
  3252. struct cam_hw_soc_dump_header *hdr;
  3253. if (!soc_info || !dump_args || !reg_read) {
  3254. CAM_ERR(CAM_UTIL,
  3255. "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK",
  3256. soc_info, dump_args, reg_read);
  3257. rc = -EINVAL;
  3258. goto end;
  3259. }
  3260. rc = cam_mem_get_cpu_buf(dump_args->buf_handle, &cpu_addr, &buf_len);
  3261. if (rc) {
  3262. CAM_ERR(CAM_UTIL, "Invalid handle %u rc %d",
  3263. dump_args->buf_handle, rc);
  3264. goto end;
  3265. }
  3266. if (buf_len <= dump_args->offset) {
  3267. CAM_WARN(CAM_UTIL, "Dump offset overshoot %zu %zu",
  3268. dump_args->offset, buf_len);
  3269. rc = -ENOSPC;
  3270. goto end;
  3271. }
  3272. remain_len = buf_len - dump_args->offset;
  3273. min_len = (reg_read->num_values * 2 * sizeof(uint32_t)) +
  3274. sizeof(struct cam_hw_soc_dump_header) + sizeof(uint32_t);
  3275. if (remain_len < min_len) {
  3276. CAM_WARN(CAM_UTIL,
  3277. "Dump Buffer exhaust read_values %d remain %zu min %u",
  3278. reg_read->num_values,
  3279. remain_len,
  3280. min_len);
  3281. rc = -ENOSPC;
  3282. goto end;
  3283. }
  3284. dst = (uint8_t *)cpu_addr + dump_args->offset;
  3285. hdr = (struct cam_hw_soc_dump_header *)dst;
  3286. memset(hdr, 0, sizeof(struct cam_hw_soc_dump_header));
  3287. scnprintf(hdr->tag, CAM_SOC_HW_DUMP_TAG_MAX_LEN, "%s_REG:",
  3288. soc_info->dev_name);
  3289. waddr = (uint32_t *)(dst + sizeof(struct cam_hw_soc_dump_header));
  3290. start = waddr;
  3291. hdr->word_size = sizeof(uint32_t);
  3292. *waddr = soc_info->index;
  3293. waddr++;
  3294. for (i = 0; i < reg_read->num_values; i++) {
  3295. if ((reg_read->offset + (i * sizeof(uint32_t))) >
  3296. (uint32_t)soc_info->reg_map[base_idx].size) {
  3297. CAM_ERR(CAM_UTIL,
  3298. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  3299. (reg_read->offset + (i * sizeof(uint32_t))),
  3300. (uint32_t)soc_info->reg_map[base_idx].size);
  3301. rc = -EINVAL;
  3302. goto end;
  3303. }
  3304. waddr[0] = reg_read->offset + (i * sizeof(uint32_t));
  3305. waddr[1] = cam_soc_util_r(soc_info, base_idx,
  3306. (reg_read->offset + (i * sizeof(uint32_t))));
  3307. waddr += 2;
  3308. }
  3309. hdr->size = (waddr - start) * hdr->word_size;
  3310. dump_args->offset += hdr->size +
  3311. sizeof(struct cam_hw_soc_dump_header);
  3312. end:
  3313. return rc;
  3314. }
  3315. static int cam_soc_util_user_reg_dump(
  3316. struct cam_reg_dump_desc *reg_dump_desc,
  3317. struct cam_hw_soc_dump_args *dump_args,
  3318. struct cam_hw_soc_info *soc_info,
  3319. uint32_t reg_base_idx)
  3320. {
  3321. int rc = 0;
  3322. int i;
  3323. struct cam_reg_read_info *reg_read_info = NULL;
  3324. if (!dump_args || !reg_dump_desc || !soc_info) {
  3325. CAM_ERR(CAM_UTIL,
  3326. "Invalid input parameters %pK %pK %pK",
  3327. dump_args, reg_dump_desc, soc_info);
  3328. return -EINVAL;
  3329. }
  3330. for (i = 0; i < reg_dump_desc->num_read_range; i++) {
  3331. reg_read_info = &reg_dump_desc->read_range[i];
  3332. if (reg_read_info->type ==
  3333. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  3334. rc = cam_soc_util_dump_cont_reg_range_user_buf(
  3335. soc_info,
  3336. &reg_read_info->reg_read,
  3337. reg_base_idx,
  3338. dump_args);
  3339. } else if (reg_read_info->type ==
  3340. CAM_REG_DUMP_READ_TYPE_DMI) {
  3341. rc = cam_soc_util_dump_dmi_reg_range_user_buf(
  3342. soc_info,
  3343. &reg_read_info->dmi_read,
  3344. reg_base_idx,
  3345. dump_args);
  3346. } else {
  3347. CAM_ERR(CAM_UTIL,
  3348. "Invalid Reg dump read type: %d",
  3349. reg_read_info->type);
  3350. rc = -EINVAL;
  3351. goto end;
  3352. }
  3353. if (rc) {
  3354. CAM_ERR(CAM_UTIL,
  3355. "Reg range read failed rc: %d reg_base_idx: %d",
  3356. rc, reg_base_idx);
  3357. goto end;
  3358. }
  3359. }
  3360. end:
  3361. return rc;
  3362. }
  3363. int cam_soc_util_reg_dump_to_cmd_buf(void *ctx,
  3364. struct cam_cmd_buf_desc *cmd_desc, uint64_t req_id,
  3365. cam_soc_util_regspace_data_cb reg_data_cb,
  3366. struct cam_hw_soc_dump_args *soc_dump_args,
  3367. bool user_triggered_dump)
  3368. {
  3369. int rc = 0, i, j;
  3370. uintptr_t cpu_addr = 0;
  3371. uintptr_t cmd_buf_start = 0;
  3372. uintptr_t cmd_in_data_end = 0;
  3373. uintptr_t cmd_buf_end = 0;
  3374. uint32_t reg_base_type = 0;
  3375. size_t buf_size = 0, remain_len = 0;
  3376. struct cam_reg_dump_input_info *reg_input_info = NULL;
  3377. struct cam_reg_dump_desc *reg_dump_desc = NULL;
  3378. struct cam_reg_dump_out_buffer *dump_out_buf = NULL;
  3379. struct cam_reg_read_info *reg_read_info = NULL;
  3380. struct cam_hw_soc_info *soc_info;
  3381. uint32_t reg_base_idx = 0;
  3382. if (!ctx || !cmd_desc || !reg_data_cb) {
  3383. CAM_ERR(CAM_UTIL, "Invalid args to reg dump [%pK] [%pK]",
  3384. cmd_desc, reg_data_cb);
  3385. return -EINVAL;
  3386. }
  3387. if (!cmd_desc->length || !cmd_desc->size) {
  3388. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  3389. cmd_desc->length, cmd_desc->size);
  3390. return -EINVAL;
  3391. }
  3392. rc = cam_mem_get_cpu_buf(cmd_desc->mem_handle, &cpu_addr, &buf_size);
  3393. if (rc || !cpu_addr || (buf_size == 0)) {
  3394. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  3395. rc, (void *)cpu_addr);
  3396. goto end;
  3397. }
  3398. CAM_DBG(CAM_UTIL, "Get cpu buf success req_id: %llu buf_size: %zu",
  3399. req_id, buf_size);
  3400. if ((buf_size < sizeof(uint32_t)) ||
  3401. ((size_t)cmd_desc->offset > (buf_size - sizeof(uint32_t)))) {
  3402. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  3403. (size_t)cmd_desc->offset);
  3404. rc = -EINVAL;
  3405. goto end;
  3406. }
  3407. remain_len = buf_size - (size_t)cmd_desc->offset;
  3408. if ((remain_len < (size_t)cmd_desc->size) || (cmd_desc->size <
  3409. cmd_desc->length)) {
  3410. CAM_ERR(CAM_UTIL,
  3411. "Invalid params for cmd buf len: %zu size: %zu remain_len: %zu",
  3412. (size_t)cmd_desc->length, (size_t)cmd_desc->length,
  3413. remain_len);
  3414. rc = -EINVAL;
  3415. goto end;
  3416. }
  3417. cmd_buf_start = cpu_addr + (uintptr_t)cmd_desc->offset;
  3418. cmd_in_data_end = cmd_buf_start + (uintptr_t)cmd_desc->length;
  3419. cmd_buf_end = cmd_buf_start + (uintptr_t)cmd_desc->size;
  3420. if ((cmd_buf_end <= cmd_buf_start) ||
  3421. (cmd_in_data_end <= cmd_buf_start)) {
  3422. CAM_ERR(CAM_UTIL,
  3423. "Invalid length or size for cmd buf: [%zu] [%zu]",
  3424. (size_t)cmd_desc->length, (size_t)cmd_desc->size);
  3425. rc = -EINVAL;
  3426. goto end;
  3427. }
  3428. CAM_DBG(CAM_UTIL,
  3429. "Buffer params start [%pK] input_end [%pK] buf_end [%pK]",
  3430. cmd_buf_start, cmd_in_data_end, cmd_buf_end);
  3431. reg_input_info = (struct cam_reg_dump_input_info *) cmd_buf_start;
  3432. if ((reg_input_info->num_dump_sets > 1) && (sizeof(uint32_t) >
  3433. ((U32_MAX - sizeof(struct cam_reg_dump_input_info)) /
  3434. (reg_input_info->num_dump_sets - 1)))) {
  3435. CAM_ERR(CAM_UTIL,
  3436. "Integer Overflow req_id: [%llu] num_dump_sets: [%u]",
  3437. req_id, reg_input_info->num_dump_sets);
  3438. rc = -EOVERFLOW;
  3439. goto end;
  3440. }
  3441. if ((!reg_input_info->num_dump_sets) ||
  3442. ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  3443. (sizeof(struct cam_reg_dump_input_info) +
  3444. ((reg_input_info->num_dump_sets - 1) * sizeof(uint32_t))))) {
  3445. CAM_ERR(CAM_UTIL,
  3446. "Invalid number of dump sets, req_id: [%llu] num_dump_sets: [%u]",
  3447. req_id, reg_input_info->num_dump_sets);
  3448. rc = -EINVAL;
  3449. goto end;
  3450. }
  3451. CAM_DBG(CAM_UTIL,
  3452. "reg_input_info req_id: %llu ctx %pK num_dump_sets: %d",
  3453. req_id, ctx, reg_input_info->num_dump_sets);
  3454. for (i = 0; i < reg_input_info->num_dump_sets; i++) {
  3455. if ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  3456. reg_input_info->dump_set_offsets[i]) {
  3457. CAM_ERR(CAM_UTIL,
  3458. "Invalid dump set offset: [%pK], cmd_buf_start: [%pK] cmd_in_data_end: [%pK]",
  3459. (uintptr_t)reg_input_info->dump_set_offsets[i],
  3460. cmd_buf_start, cmd_in_data_end);
  3461. rc = -EINVAL;
  3462. goto end;
  3463. }
  3464. reg_dump_desc = (struct cam_reg_dump_desc *)
  3465. (cmd_buf_start +
  3466. (uintptr_t)reg_input_info->dump_set_offsets[i]);
  3467. if ((reg_dump_desc->num_read_range > 1) &&
  3468. (sizeof(struct cam_reg_read_info) > ((U32_MAX -
  3469. sizeof(struct cam_reg_dump_desc)) /
  3470. (reg_dump_desc->num_read_range - 1)))) {
  3471. CAM_ERR(CAM_UTIL,
  3472. "Integer Overflow req_id: [%llu] num_read_range: [%u]",
  3473. req_id, reg_dump_desc->num_read_range);
  3474. rc = -EOVERFLOW;
  3475. goto end;
  3476. }
  3477. if ((!reg_dump_desc->num_read_range) ||
  3478. ((cmd_in_data_end - (uintptr_t)reg_dump_desc) <=
  3479. (uintptr_t)(sizeof(struct cam_reg_dump_desc) +
  3480. ((reg_dump_desc->num_read_range - 1) *
  3481. sizeof(struct cam_reg_read_info))))) {
  3482. CAM_ERR(CAM_UTIL,
  3483. "Invalid number of read ranges, req_id: [%llu] num_read_range: [%d]",
  3484. req_id, reg_dump_desc->num_read_range);
  3485. rc = -EINVAL;
  3486. goto end;
  3487. }
  3488. if ((cmd_buf_end - cmd_buf_start) <= (uintptr_t)
  3489. (reg_dump_desc->dump_buffer_offset +
  3490. sizeof(struct cam_reg_dump_out_buffer))) {
  3491. CAM_ERR(CAM_UTIL,
  3492. "Invalid out buffer offset: [%pK], cmd_buf_start: [%pK] cmd_buf_end: [%pK]",
  3493. (uintptr_t)reg_dump_desc->dump_buffer_offset,
  3494. cmd_buf_start, cmd_buf_end);
  3495. rc = -EINVAL;
  3496. goto end;
  3497. }
  3498. reg_base_type = reg_dump_desc->reg_base_type;
  3499. if (reg_base_type == 0 || reg_base_type >
  3500. CAM_REG_DUMP_BASE_TYPE_SFE_RIGHT) {
  3501. CAM_ERR(CAM_UTIL,
  3502. "Invalid Reg dump base type: %d",
  3503. reg_base_type);
  3504. rc = -EINVAL;
  3505. goto end;
  3506. }
  3507. rc = reg_data_cb(reg_base_type, ctx, &soc_info, &reg_base_idx);
  3508. if (rc || !soc_info) {
  3509. CAM_ERR(CAM_UTIL,
  3510. "Reg space data callback failed rc: %d soc_info: [%pK]",
  3511. rc, soc_info);
  3512. rc = -EINVAL;
  3513. goto end;
  3514. }
  3515. if (reg_base_idx > soc_info->num_reg_map) {
  3516. CAM_ERR(CAM_UTIL,
  3517. "Invalid reg base idx: %d num reg map: %d",
  3518. reg_base_idx, soc_info->num_reg_map);
  3519. rc = -EINVAL;
  3520. goto end;
  3521. }
  3522. CAM_DBG(CAM_UTIL,
  3523. "Reg data callback success req_id: %llu base_type: %d base_idx: %d num_read_range: %d",
  3524. req_id, reg_base_type, reg_base_idx,
  3525. reg_dump_desc->num_read_range);
  3526. /* If the dump request is triggered by user space
  3527. * buffer will be different from the buffer which is received
  3528. * in init packet. In this case, dump the data to the
  3529. * user provided buffer and exit.
  3530. */
  3531. if (user_triggered_dump) {
  3532. rc = cam_soc_util_user_reg_dump(reg_dump_desc,
  3533. soc_dump_args, soc_info, reg_base_idx);
  3534. CAM_INFO(CAM_UTIL,
  3535. "%s reg_base_idx %d dumped offset %u",
  3536. soc_info->dev_name, reg_base_idx,
  3537. soc_dump_args->offset);
  3538. goto end;
  3539. }
  3540. /* Below code is executed when data is dumped to the
  3541. * out buffer received in init packet
  3542. */
  3543. dump_out_buf = (struct cam_reg_dump_out_buffer *)
  3544. (cmd_buf_start +
  3545. (uintptr_t)reg_dump_desc->dump_buffer_offset);
  3546. dump_out_buf->req_id = req_id;
  3547. dump_out_buf->bytes_written = 0;
  3548. for (j = 0; j < reg_dump_desc->num_read_range; j++) {
  3549. CAM_DBG(CAM_UTIL,
  3550. "Number of bytes written to cmd buffer: %u req_id: %llu",
  3551. dump_out_buf->bytes_written, req_id);
  3552. reg_read_info = &reg_dump_desc->read_range[j];
  3553. if (reg_read_info->type ==
  3554. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  3555. rc = cam_soc_util_dump_cont_reg_range(soc_info,
  3556. &reg_read_info->reg_read, reg_base_idx,
  3557. dump_out_buf, cmd_buf_end);
  3558. } else if (reg_read_info->type ==
  3559. CAM_REG_DUMP_READ_TYPE_DMI) {
  3560. rc = cam_soc_util_dump_dmi_reg_range(soc_info,
  3561. &reg_read_info->dmi_read, reg_base_idx,
  3562. dump_out_buf, cmd_buf_end);
  3563. } else {
  3564. CAM_ERR(CAM_UTIL,
  3565. "Invalid Reg dump read type: %d",
  3566. reg_read_info->type);
  3567. rc = -EINVAL;
  3568. goto end;
  3569. }
  3570. if (rc) {
  3571. CAM_ERR(CAM_UTIL,
  3572. "Reg range read failed rc: %d reg_base_idx: %d dump_out_buf: %pK",
  3573. rc, reg_base_idx, dump_out_buf);
  3574. goto end;
  3575. }
  3576. }
  3577. }
  3578. end:
  3579. return rc;
  3580. }
  3581. /**
  3582. * cam_soc_util_print_clk_freq()
  3583. *
  3584. * @brief: This function gets the clk rates for each clk from clk
  3585. * driver and prints in log
  3586. *
  3587. * @soc_info: Device soc struct to be populated
  3588. *
  3589. * @return: success or failure
  3590. */
  3591. int cam_soc_util_print_clk_freq(struct cam_hw_soc_info *soc_info)
  3592. {
  3593. int i;
  3594. unsigned long clk_rate = 0;
  3595. if (!soc_info) {
  3596. CAM_ERR(CAM_UTIL, "Invalid soc info");
  3597. return -EINVAL;
  3598. }
  3599. if ((soc_info->num_clk == 0) ||
  3600. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  3601. CAM_ERR(CAM_UTIL, "[%s] Invalid number of clock %d",
  3602. soc_info->dev_name, soc_info->num_clk);
  3603. return -EINVAL;
  3604. }
  3605. for (i = 0; i < soc_info->num_clk; i++) {
  3606. clk_rate = cam_wrapper_clk_get_rate(soc_info->clk[i]);
  3607. CAM_INFO(CAM_UTIL,
  3608. "[%s] idx = %d clk name = %s clk_rate=%lld",
  3609. soc_info->dev_name, i, soc_info->clk_name[i],
  3610. clk_rate);
  3611. }
  3612. return 0;
  3613. }
  3614. inline unsigned long cam_soc_util_get_applied_src_clk(
  3615. struct cam_hw_soc_info *soc_info, bool is_max)
  3616. {
  3617. unsigned long clk_rate;
  3618. /*
  3619. * For CRMC type, exa - ife, csid, cphy
  3620. * final clk = max(hw_client_0, hw_client_1, hw_client_2, sw_client)
  3621. * For CRMB type, exa - camnoc axi
  3622. * final clk = max(hw_client_0 + hw_client_1 + hw_client_2, sw_client)
  3623. */
  3624. if (is_max) {
  3625. clk_rate = max(soc_info->applied_src_clk_rates.hw_client[0].high,
  3626. soc_info->applied_src_clk_rates.hw_client[1].high);
  3627. clk_rate = max(clk_rate, soc_info->applied_src_clk_rates.hw_client[2].high);
  3628. clk_rate = max(clk_rate, soc_info->applied_src_clk_rates.sw_client);
  3629. } else {
  3630. clk_rate = max((soc_info->applied_src_clk_rates.hw_client[0].high +
  3631. soc_info->applied_src_clk_rates.hw_client[1].high +
  3632. soc_info->applied_src_clk_rates.hw_client[2].high),
  3633. soc_info->applied_src_clk_rates.sw_client);
  3634. }
  3635. return clk_rate;
  3636. }
  3637. int cam_soc_util_regulators_enabled(struct cam_hw_soc_info *soc_info)
  3638. {
  3639. int j = 0, rc = 0;
  3640. int enabled_cnt = 0;
  3641. for (j = 0; j < soc_info->num_rgltr; j++) {
  3642. if (soc_info->rgltr[j]) {
  3643. rc = cam_wrapper_regulator_is_enabled(soc_info->rgltr[j]);
  3644. if (rc < 0) {
  3645. CAM_ERR(CAM_UTIL, "%s regulator_is_enabled failed",
  3646. soc_info->rgltr_name[j]);
  3647. } else if (rc > 0) {
  3648. CAM_DBG(CAM_UTIL, "%s regulator enabled",
  3649. soc_info->rgltr_name[j]);
  3650. enabled_cnt++;
  3651. } else {
  3652. CAM_DBG(CAM_UTIL, "%s regulator is disabled",
  3653. soc_info->rgltr_name[j]);
  3654. }
  3655. }
  3656. }
  3657. return enabled_cnt;
  3658. }