cam_soc_util.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223
  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_D1_VOTE:
  783. return "LOWSVSD1[2]";
  784. case CAM_LOWSVS_VOTE:
  785. return "LOWSVS[3]";
  786. case CAM_SVS_VOTE:
  787. return "SVS[4]";
  788. case CAM_SVSL1_VOTE:
  789. return "SVSL1[5]";
  790. case CAM_NOMINAL_VOTE:
  791. return "NOM[6]";
  792. case CAM_NOMINALL1_VOTE:
  793. return "NOML1[7]";
  794. case CAM_TURBO_VOTE:
  795. return "TURBO[8]";
  796. default:
  797. return "";
  798. }
  799. }
  800. /**
  801. * cam_soc_util_get_supported_clk_levels()
  802. *
  803. * @brief: Returns the string of all the supported clk levels for
  804. * the given device
  805. *
  806. * @soc_info: Device soc information
  807. *
  808. * @return: String containing all supported clk levels
  809. */
  810. static const char *cam_soc_util_get_supported_clk_levels(
  811. struct cam_hw_soc_info *soc_info)
  812. {
  813. int i = 0;
  814. scnprintf(supported_clk_info, sizeof(supported_clk_info), "Supported levels: ");
  815. for (i = 0; i < CAM_MAX_VOTE; i++) {
  816. if (soc_info->clk_level_valid[i] == true) {
  817. strlcat(supported_clk_info,
  818. cam_soc_util_get_string_from_level(i),
  819. sizeof(supported_clk_info));
  820. strlcat(supported_clk_info, " ",
  821. sizeof(supported_clk_info));
  822. }
  823. }
  824. strlcat(supported_clk_info, "\n", sizeof(supported_clk_info));
  825. return supported_clk_info;
  826. }
  827. static int cam_soc_util_clk_lvl_options_open(struct inode *inode,
  828. struct file *file)
  829. {
  830. file->private_data = inode->i_private;
  831. return 0;
  832. }
  833. static ssize_t cam_soc_util_clk_lvl_options_read(struct file *file,
  834. char __user *clk_info, size_t size_t, loff_t *loff_t)
  835. {
  836. struct cam_hw_soc_info *soc_info =
  837. (struct cam_hw_soc_info *)file->private_data;
  838. const char *display_string =
  839. cam_soc_util_get_supported_clk_levels(soc_info);
  840. return simple_read_from_buffer(clk_info, size_t, loff_t, display_string,
  841. strlen(display_string));
  842. }
  843. static const struct file_operations cam_soc_util_clk_lvl_options = {
  844. .open = cam_soc_util_clk_lvl_options_open,
  845. .read = cam_soc_util_clk_lvl_options_read,
  846. };
  847. static int cam_soc_util_set_clk_lvl_override(void *data, u64 val)
  848. {
  849. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  850. if ((val <= CAM_SUSPEND_VOTE) || (val >= CAM_MAX_VOTE)) {
  851. CAM_WARN(CAM_UTIL, "Invalid clk lvl override %d", val);
  852. return 0;
  853. }
  854. if (soc_info->clk_level_valid[val])
  855. soc_info->clk_level_override_high = val;
  856. else
  857. soc_info->clk_level_override_high = 0;
  858. return 0;
  859. }
  860. static int cam_soc_util_get_clk_lvl_override(void *data, u64 *val)
  861. {
  862. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  863. *val = soc_info->clk_level_override_high;
  864. return 0;
  865. }
  866. static int cam_soc_util_set_clk_lvl_override_low(void *data, u64 val)
  867. {
  868. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  869. if ((val <= CAM_SUSPEND_VOTE) || (val >= CAM_MAX_VOTE)) {
  870. CAM_WARN(CAM_UTIL, "Invalid clk lvl override %d", val);
  871. return 0;
  872. }
  873. if (soc_info->clk_level_valid[val])
  874. soc_info->clk_level_override_low = val;
  875. else
  876. soc_info->clk_level_override_low = 0;
  877. return 0;
  878. }
  879. static int cam_soc_util_get_clk_lvl_override_low(void *data, u64 *val)
  880. {
  881. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  882. *val = soc_info->clk_level_override_low;
  883. return 0;
  884. }
  885. DEFINE_SIMPLE_ATTRIBUTE(cam_soc_util_clk_lvl_control,
  886. cam_soc_util_get_clk_lvl_override, cam_soc_util_set_clk_lvl_override, "%08llu");
  887. DEFINE_SIMPLE_ATTRIBUTE(cam_soc_util_clk_lvl_control_low,
  888. cam_soc_util_get_clk_lvl_override_low, cam_soc_util_set_clk_lvl_override_low, "%08llu");
  889. /**
  890. * cam_soc_util_create_clk_lvl_debugfs()
  891. *
  892. * @brief: Creates debugfs files to view/control device clk rates
  893. *
  894. * @soc_info: Device soc information
  895. *
  896. * @return: Success or failure
  897. */
  898. static int cam_soc_util_create_clk_lvl_debugfs(struct cam_hw_soc_info *soc_info)
  899. {
  900. int rc = 0;
  901. struct dentry *clkdirptr = NULL;
  902. if (!cam_debugfs_available())
  903. return 0;
  904. if (soc_info->dentry) {
  905. CAM_DBG(CAM_UTIL, "Debugfs entry for %s already exists",
  906. soc_info->dev_name);
  907. goto end;
  908. }
  909. rc = cam_debugfs_lookup_subdir(CAM_CLK_DIRNAME, &clkdirptr);
  910. if (rc) {
  911. rc = cam_debugfs_create_subdir(CAM_CLK_DIRNAME, &clkdirptr);
  912. if (rc) {
  913. CAM_ERR(CAM_UTIL, "DebugFS could not create clk directory!");
  914. rc = -ENOENT;
  915. goto end;
  916. }
  917. }
  918. soc_info->dentry = debugfs_create_dir(soc_info->dev_name, clkdirptr);
  919. if (IS_ERR_OR_NULL(soc_info->dentry)) {
  920. CAM_ERR(CAM_UTIL, "DebugFS could not create directory for dev:%s!",
  921. soc_info->dev_name);
  922. rc = -ENOENT;
  923. goto end;
  924. }
  925. /* Store parent inode for cleanup in caller */
  926. debugfs_create_file("clk_lvl_options", 0444,
  927. soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_options);
  928. debugfs_create_file("clk_lvl_control", 0644,
  929. soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_control);
  930. debugfs_create_file("clk_lvl_control_low", 0644,
  931. soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_control_low);
  932. end:
  933. return rc;
  934. }
  935. int cam_soc_util_get_level_from_string(const char *string,
  936. enum cam_vote_level *level)
  937. {
  938. if (!level)
  939. return -EINVAL;
  940. if (!strcmp(string, "suspend")) {
  941. *level = CAM_SUSPEND_VOTE;
  942. } else if (!strcmp(string, "minsvs")) {
  943. *level = CAM_MINSVS_VOTE;
  944. } else if (!strcmp(string, "lowsvsd1")) {
  945. *level = CAM_LOWSVS_D1_VOTE;
  946. } else if (!strcmp(string, "lowsvs")) {
  947. *level = CAM_LOWSVS_VOTE;
  948. } else if (!strcmp(string, "svs")) {
  949. *level = CAM_SVS_VOTE;
  950. } else if (!strcmp(string, "svs_l1")) {
  951. *level = CAM_SVSL1_VOTE;
  952. } else if (!strcmp(string, "nominal")) {
  953. *level = CAM_NOMINAL_VOTE;
  954. } else if (!strcmp(string, "nominal_l1")) {
  955. *level = CAM_NOMINALL1_VOTE;
  956. } else if (!strcmp(string, "turbo")) {
  957. *level = CAM_TURBO_VOTE;
  958. } else {
  959. CAM_ERR(CAM_UTIL, "Invalid string %s", string);
  960. return -EINVAL;
  961. }
  962. return 0;
  963. }
  964. /**
  965. * cam_soc_util_get_clk_level_to_apply()
  966. *
  967. * @brief: Get the clock level to apply. If the requested level
  968. * is not valid, bump the level to next available valid
  969. * level. If no higher level found, return failure.
  970. *
  971. * @soc_info: Device soc struct to be populated
  972. * @req_level: Requested level
  973. * @apply_level Level to apply
  974. *
  975. * @return: success or failure
  976. */
  977. static int cam_soc_util_get_clk_level_to_apply(
  978. struct cam_hw_soc_info *soc_info, enum cam_vote_level req_level,
  979. enum cam_vote_level *apply_level)
  980. {
  981. if (req_level >= CAM_MAX_VOTE) {
  982. CAM_ERR(CAM_UTIL, "Invalid clock level parameter %d",
  983. req_level);
  984. return -EINVAL;
  985. }
  986. if (soc_info->clk_level_valid[req_level] == true) {
  987. *apply_level = req_level;
  988. } else {
  989. int i;
  990. for (i = (req_level + 1); i < CAM_MAX_VOTE; i++)
  991. if (soc_info->clk_level_valid[i] == true) {
  992. *apply_level = i;
  993. break;
  994. }
  995. if (i == CAM_MAX_VOTE) {
  996. CAM_ERR(CAM_UTIL,
  997. "No valid clock level found to apply, req=%d",
  998. req_level);
  999. return -EINVAL;
  1000. }
  1001. }
  1002. CAM_DBG(CAM_UTIL, "Req level %s, Applying %s",
  1003. cam_soc_util_get_string_from_level(req_level),
  1004. cam_soc_util_get_string_from_level(*apply_level));
  1005. return 0;
  1006. }
  1007. int cam_soc_util_irq_enable(struct cam_hw_soc_info *soc_info)
  1008. {
  1009. int i, rc = 0;
  1010. if (!soc_info) {
  1011. CAM_ERR(CAM_UTIL, "Invalid arguments");
  1012. return -EINVAL;
  1013. }
  1014. for (i = 0; i < soc_info->irq_count; i++) {
  1015. if (soc_info->irq_num[i] < 0) {
  1016. CAM_ERR(CAM_UTIL, "No IRQ line available for irq: %s dev: %s",
  1017. soc_info->irq_name[i], soc_info->dev_name);
  1018. rc = -ENODEV;
  1019. goto disable_irq;
  1020. }
  1021. enable_irq(soc_info->irq_num[i]);
  1022. }
  1023. return rc;
  1024. disable_irq:
  1025. for (i = i - 1; i >= 0; i--)
  1026. disable_irq(soc_info->irq_num[i]);
  1027. return rc;
  1028. }
  1029. int cam_soc_util_irq_disable(struct cam_hw_soc_info *soc_info)
  1030. {
  1031. int i, rc = 0;
  1032. if (!soc_info) {
  1033. CAM_ERR(CAM_UTIL, "Invalid arguments");
  1034. return -EINVAL;
  1035. }
  1036. for (i = 0; i < soc_info->irq_count; i++) {
  1037. if (soc_info->irq_num[i] < 0) {
  1038. CAM_ERR(CAM_UTIL, "No IRQ line available irq: %s dev:",
  1039. soc_info->irq_name[i], soc_info->dev_name);
  1040. rc = -ENODEV;
  1041. continue;
  1042. }
  1043. disable_irq(soc_info->irq_num[i]);
  1044. }
  1045. return rc;
  1046. }
  1047. long cam_soc_util_get_clk_round_rate(struct cam_hw_soc_info *soc_info,
  1048. uint32_t clk_index, unsigned long clk_rate)
  1049. {
  1050. if (!soc_info || (clk_index >= soc_info->num_clk) || (clk_rate == 0)) {
  1051. CAM_ERR(CAM_UTIL, "Invalid input params %pK, %d %lu",
  1052. soc_info, clk_index, clk_rate);
  1053. return clk_rate;
  1054. }
  1055. return cam_wrapper_clk_round_rate(soc_info->clk[clk_index], clk_rate);
  1056. }
  1057. /**
  1058. * cam_soc_util_set_clk_rate()
  1059. *
  1060. * @brief: Sets the given rate for the clk requested for
  1061. *
  1062. * @clk: Clock structure information for which rate is to be set
  1063. * @clk_name: Name of the clock for which rate is being set
  1064. * @clk_rate: Clock rate to be set
  1065. * @shared_clk: Whether this is a shared clk
  1066. * @is_src_clk: Whether this is source clk
  1067. * @clk_id: Clock ID
  1068. * @applied_clk_rate: Final clock rate set to the clk
  1069. *
  1070. * @return: Success or failure
  1071. */
  1072. static int cam_soc_util_set_clk_rate(struct cam_hw_soc_info *soc_info,
  1073. struct clk *clk, const char *clk_name,
  1074. int64_t clk_rate, bool shared_clk, bool is_src_clk, uint32_t clk_id,
  1075. unsigned long *applied_clk_rate)
  1076. {
  1077. int rc = 0;
  1078. long clk_rate_round = -1;
  1079. bool set_rate = false;
  1080. if (!clk_name) {
  1081. CAM_ERR(CAM_UTIL, "Invalid input clk %pK clk_name %pK",
  1082. clk, clk_name);
  1083. return -EINVAL;
  1084. }
  1085. CAM_DBG(CAM_UTIL, "set %s, rate %lld", clk_name, clk_rate);
  1086. if (!clk)
  1087. return 0;
  1088. if (clk_rate > 0) {
  1089. clk_rate_round = cam_wrapper_clk_round_rate(clk, clk_rate);
  1090. CAM_DBG(CAM_UTIL, "new_rate %ld", clk_rate_round);
  1091. if (clk_rate_round < 0) {
  1092. CAM_ERR(CAM_UTIL, "round failed for clock %s rc = %ld",
  1093. clk_name, clk_rate_round);
  1094. return clk_rate_round;
  1095. }
  1096. set_rate = true;
  1097. } else if (clk_rate == INIT_RATE) {
  1098. clk_rate_round = cam_wrapper_clk_get_rate(clk);
  1099. CAM_DBG(CAM_UTIL, "init new_rate %ld", clk_rate_round);
  1100. if (clk_rate_round == 0) {
  1101. clk_rate_round = cam_wrapper_clk_round_rate(clk, 0);
  1102. if (clk_rate_round <= 0) {
  1103. CAM_ERR(CAM_UTIL, "round rate failed on %s",
  1104. clk_name);
  1105. return clk_rate_round;
  1106. }
  1107. }
  1108. set_rate = true;
  1109. }
  1110. if (set_rate) {
  1111. if (shared_clk) {
  1112. CAM_DBG(CAM_UTIL,
  1113. "Dev %s clk %s id %d Set Shared clk %ld",
  1114. soc_info->dev_name, clk_name, clk_id,
  1115. clk_rate_round);
  1116. cam_soc_util_clk_wrapper_set_clk_rate(
  1117. clk_id, soc_info, clk, clk_rate_round);
  1118. } else {
  1119. bool set_rate_finish = false;
  1120. CAM_DBG(CAM_UTIL,
  1121. "Dev %s clk %s clk_id %d src_idx %d src_clk_id %d",
  1122. soc_info->dev_name, clk_name, clk_id,
  1123. soc_info->src_clk_idx,
  1124. (soc_info->src_clk_idx == -1) ? -1 :
  1125. soc_info->clk_id[soc_info->src_clk_idx]);
  1126. if (is_src_clk && soc_info->mmrm_handle &&
  1127. !skip_mmrm_set_rate) {
  1128. uint32_t idx = soc_info->src_clk_idx;
  1129. uint32_t min_level = soc_info->lowest_clk_level;
  1130. rc = cam_soc_util_set_rate_through_mmrm(
  1131. soc_info->mmrm_handle,
  1132. soc_info->is_nrt_dev,
  1133. soc_info->clk_rate[min_level][idx],
  1134. clk_rate_round, 1);
  1135. if (rc) {
  1136. CAM_ERR(CAM_UTIL,
  1137. "set_rate through mmrm failed on %s clk_id %d, rate=%ld",
  1138. clk_name, clk_id, clk_rate_round);
  1139. return rc;
  1140. }
  1141. set_rate_finish = true;
  1142. }
  1143. if (!set_rate_finish) {
  1144. rc = cam_wrapper_clk_set_rate(clk, clk_rate_round);
  1145. if (rc) {
  1146. CAM_ERR(CAM_UTIL, "set_rate failed on %s", clk_name);
  1147. return rc;
  1148. }
  1149. }
  1150. }
  1151. }
  1152. if (applied_clk_rate)
  1153. *applied_clk_rate = clk_rate_round;
  1154. return rc;
  1155. }
  1156. int cam_soc_util_set_src_clk_rate(struct cam_hw_soc_info *soc_info, int cesta_client_idx,
  1157. unsigned long clk_rate_high, unsigned long clk_rate_low)
  1158. {
  1159. int rc = 0;
  1160. int i = 0;
  1161. int32_t src_clk_idx;
  1162. int32_t scl_clk_idx;
  1163. struct clk *clk = NULL;
  1164. int32_t apply_level;
  1165. uint32_t clk_level_override_high = 0, clk_level_override_low = 0;
  1166. if (!soc_info || (soc_info->src_clk_idx < 0) ||
  1167. (soc_info->src_clk_idx >= CAM_SOC_MAX_CLK)) {
  1168. CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d",
  1169. soc_info ? soc_info->src_clk_idx : -1);
  1170. return -EINVAL;
  1171. }
  1172. src_clk_idx = soc_info->src_clk_idx;
  1173. clk_level_override_high = soc_info->clk_level_override_high;
  1174. clk_level_override_low = soc_info->clk_level_override_low;
  1175. if (clk_level_override_high && clk_rate_high)
  1176. clk_rate_high = soc_info->clk_rate[clk_level_override_high][src_clk_idx];
  1177. if (clk_level_override_low && clk_rate_low)
  1178. clk_rate_low = soc_info->clk_rate[clk_level_override_low][src_clk_idx];
  1179. clk = soc_info->clk[src_clk_idx];
  1180. rc = cam_soc_util_get_clk_level(soc_info, clk_rate_high, src_clk_idx,
  1181. &apply_level);
  1182. if (rc || (apply_level < 0) || (apply_level >= CAM_MAX_VOTE)) {
  1183. CAM_ERR(CAM_UTIL,
  1184. "set %s, rate %lld dev_name = %s apply level = %d",
  1185. soc_info->clk_name[src_clk_idx], clk_rate_high,
  1186. soc_info->dev_name, apply_level);
  1187. return -EINVAL;
  1188. }
  1189. CAM_DBG(CAM_UTIL,
  1190. "set %s, cesta_client_idx: %d rate [%ld %ld] dev_name = %s apply level = %d",
  1191. soc_info->clk_name[src_clk_idx], cesta_client_idx, clk_rate_high, clk_rate_low,
  1192. soc_info->dev_name, apply_level);
  1193. if ((soc_info->cam_cx_ipeak_enable) && (clk_rate_high > 0)) {
  1194. cam_cx_ipeak_update_vote_cx_ipeak(soc_info,
  1195. apply_level);
  1196. }
  1197. if (soc_info->is_clk_drv_en && CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  1198. rc = cam_soc_util_set_cesta_clk_rate(soc_info, cesta_client_idx, clk_rate_high,
  1199. clk_rate_low,
  1200. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].high,
  1201. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].low);
  1202. if (rc) {
  1203. CAM_ERR(CAM_UTIL,
  1204. "Failed in setting cesta clk rates[high low]:[%ld %ld] client_idx:%d rc:%d",
  1205. clk_rate_high, clk_rate_low, cesta_client_idx, rc);
  1206. return rc;
  1207. }
  1208. goto end;
  1209. }
  1210. rc = cam_soc_util_set_clk_rate(soc_info, clk,
  1211. soc_info->clk_name[src_clk_idx], clk_rate_high,
  1212. CAM_IS_BIT_SET(soc_info->shared_clk_mask, src_clk_idx),
  1213. true, soc_info->clk_id[src_clk_idx],
  1214. &soc_info->applied_src_clk_rates.sw_client);
  1215. if (rc) {
  1216. CAM_ERR(CAM_UTIL,
  1217. "SET_RATE Failed: src clk: %s, rate %lld, dev_name = %s rc: %d",
  1218. soc_info->clk_name[src_clk_idx], clk_rate_high,
  1219. soc_info->dev_name, rc);
  1220. return rc;
  1221. }
  1222. /* set clk rate for scalable clk if available */
  1223. for (i = 0; i < soc_info->scl_clk_count; i++) {
  1224. scl_clk_idx = soc_info->scl_clk_idx[i];
  1225. if (scl_clk_idx < 0) {
  1226. CAM_DBG(CAM_UTIL, "Scl clk index invalid");
  1227. continue;
  1228. }
  1229. clk = soc_info->clk[scl_clk_idx];
  1230. rc = cam_soc_util_set_clk_rate(soc_info, clk,
  1231. soc_info->clk_name[scl_clk_idx],
  1232. soc_info->clk_rate[apply_level][scl_clk_idx],
  1233. CAM_IS_BIT_SET(soc_info->shared_clk_mask, scl_clk_idx),
  1234. false, soc_info->clk_id[scl_clk_idx],
  1235. NULL);
  1236. if (rc) {
  1237. CAM_WARN(CAM_UTIL,
  1238. "SET_RATE Failed: scl clk: %s, rate %d dev_name = %s, rc: %d",
  1239. soc_info->clk_name[scl_clk_idx],
  1240. soc_info->clk_rate[apply_level][scl_clk_idx],
  1241. soc_info->dev_name, rc);
  1242. }
  1243. }
  1244. end:
  1245. return 0;
  1246. }
  1247. int cam_soc_util_put_optional_clk(struct cam_hw_soc_info *soc_info,
  1248. int32_t clk_indx)
  1249. {
  1250. if (clk_indx < 0) {
  1251. CAM_ERR(CAM_UTIL, "Invalid params clk %d", clk_indx);
  1252. return -EINVAL;
  1253. }
  1254. if (CAM_IS_BIT_SET(soc_info->optional_shared_clk_mask, clk_indx))
  1255. cam_soc_util_clk_wrapper_unregister_entry(
  1256. soc_info->optional_clk_id[clk_indx], soc_info);
  1257. cam_wrapper_clk_put(soc_info->optional_clk[clk_indx]);
  1258. soc_info->optional_clk[clk_indx] = NULL;
  1259. return 0;
  1260. }
  1261. static struct clk *cam_soc_util_option_clk_get(struct device_node *np,
  1262. int index, uint32_t *clk_id)
  1263. {
  1264. struct of_phandle_args clkspec;
  1265. struct clk *clk;
  1266. int rc;
  1267. if (index < 0)
  1268. return ERR_PTR(-EINVAL);
  1269. rc = of_parse_phandle_with_args(np, "clocks-option", "#clock-cells",
  1270. index, &clkspec);
  1271. if (rc)
  1272. return ERR_PTR(rc);
  1273. clk = cam_wrapper_of_clk_get_from_provider(&clkspec);
  1274. *clk_id = clkspec.args[0];
  1275. of_node_put(clkspec.np);
  1276. return clk;
  1277. }
  1278. int cam_soc_util_get_option_clk_by_name(struct cam_hw_soc_info *soc_info,
  1279. const char *clk_name, int32_t *clk_index)
  1280. {
  1281. int index = 0;
  1282. int rc = 0;
  1283. struct device_node *of_node = NULL;
  1284. uint32_t shared_clk_val;
  1285. if (!soc_info || !clk_name || !clk_index) {
  1286. CAM_ERR(CAM_UTIL,
  1287. "Invalid params soc_info %pK clk_name %s clk_index %pK",
  1288. soc_info, clk_name, clk_index);
  1289. return -EINVAL;
  1290. }
  1291. of_node = soc_info->dev->of_node;
  1292. index = of_property_match_string(of_node, "clock-names-option",
  1293. clk_name);
  1294. if (index < 0) {
  1295. CAM_DBG(CAM_UTIL, "No clk data for %s", clk_name);
  1296. *clk_index = -1;
  1297. return -EINVAL;
  1298. }
  1299. if (index >= CAM_SOC_MAX_OPT_CLK) {
  1300. CAM_ERR(CAM_UTIL, "Insufficient optional clk entries %d %d",
  1301. index, CAM_SOC_MAX_OPT_CLK);
  1302. return -EINVAL;
  1303. }
  1304. of_property_read_string_index(of_node, "clock-names-option",
  1305. index, &(soc_info->optional_clk_name[index]));
  1306. soc_info->optional_clk[index] = cam_soc_util_option_clk_get(of_node,
  1307. index, &soc_info->optional_clk_id[index]);
  1308. if (IS_ERR(soc_info->optional_clk[index])) {
  1309. CAM_ERR(CAM_UTIL, "No clk named %s found. Dev %s", clk_name,
  1310. soc_info->dev_name);
  1311. *clk_index = -1;
  1312. return -EFAULT;
  1313. }
  1314. *clk_index = index;
  1315. rc = of_property_read_u32_index(of_node, "clock-rates-option",
  1316. index, &soc_info->optional_clk_rate[index]);
  1317. if (rc) {
  1318. CAM_ERR(CAM_UTIL,
  1319. "Error reading clock-rates clk_name %s index %d",
  1320. clk_name, index);
  1321. goto error;
  1322. }
  1323. /*
  1324. * Option clocks are assumed to be available to single Device here.
  1325. * Hence use INIT_RATE instead of NO_SET_RATE.
  1326. */
  1327. soc_info->optional_clk_rate[index] =
  1328. (soc_info->optional_clk_rate[index] == 0) ?
  1329. (int32_t)INIT_RATE : soc_info->optional_clk_rate[index];
  1330. CAM_DBG(CAM_UTIL, "clk_name %s index %d clk_rate %d",
  1331. clk_name, *clk_index, soc_info->optional_clk_rate[index]);
  1332. rc = of_property_read_u32_index(of_node, "shared-clks-option",
  1333. index, &shared_clk_val);
  1334. if (rc) {
  1335. CAM_DBG(CAM_UTIL, "Not shared clk %s index %d",
  1336. clk_name, index);
  1337. } else if (shared_clk_val > 1) {
  1338. CAM_WARN(CAM_UTIL, "Invalid shared clk val %d", shared_clk_val);
  1339. } else {
  1340. CAM_DBG(CAM_UTIL,
  1341. "Dev %s shared clk %s index %d, clk id %d, shared_clk_val %d",
  1342. soc_info->dev_name, clk_name, index,
  1343. soc_info->optional_clk_id[index], shared_clk_val);
  1344. if (shared_clk_val) {
  1345. CAM_SET_BIT(soc_info->optional_shared_clk_mask, index);
  1346. /* Create a wrapper entry if this is a shared clock */
  1347. CAM_DBG(CAM_UTIL,
  1348. "Dev %s, clk %s, id %d register wrapper entry for shared clk",
  1349. soc_info->dev_name,
  1350. soc_info->optional_clk_name[index],
  1351. soc_info->optional_clk_id[index]);
  1352. rc = cam_soc_util_clk_wrapper_register_entry(
  1353. soc_info->optional_clk_id[index],
  1354. soc_info->optional_clk[index], false,
  1355. soc_info,
  1356. soc_info->optional_clk_rate[index],
  1357. soc_info->optional_clk_name[index]);
  1358. if (rc) {
  1359. CAM_ERR(CAM_UTIL,
  1360. "Failed in registering shared clk Dev %s id %d",
  1361. soc_info->dev_name,
  1362. soc_info->optional_clk_id[index]);
  1363. goto error;
  1364. }
  1365. }
  1366. }
  1367. return 0;
  1368. error:
  1369. cam_wrapper_clk_put(soc_info->optional_clk[index]);
  1370. soc_info->optional_clk_rate[index] = 0;
  1371. soc_info->optional_clk[index] = NULL;
  1372. *clk_index = -1;
  1373. return rc;
  1374. }
  1375. int cam_soc_util_clk_enable(struct cam_hw_soc_info *soc_info, int cesta_client_idx,
  1376. bool optional_clk, int32_t clk_idx, int32_t apply_level)
  1377. {
  1378. int rc = 0;
  1379. struct clk *clk;
  1380. const char *clk_name;
  1381. unsigned long clk_rate;
  1382. uint32_t shared_clk_mask;
  1383. uint32_t clk_id;
  1384. bool is_src_clk = false;
  1385. if (!soc_info || (clk_idx < 0) || (apply_level >= CAM_MAX_VOTE)) {
  1386. CAM_ERR(CAM_UTIL, "Invalid param %d %d", clk_idx, apply_level);
  1387. return -EINVAL;
  1388. }
  1389. if (optional_clk) {
  1390. clk = soc_info->optional_clk[clk_idx];
  1391. clk_name = soc_info->optional_clk_name[clk_idx];
  1392. clk_rate = (apply_level == -1) ?
  1393. 0 : soc_info->optional_clk_rate[clk_idx];
  1394. shared_clk_mask = soc_info->optional_shared_clk_mask;
  1395. clk_id = soc_info->optional_clk_id[clk_idx];
  1396. } else {
  1397. clk = soc_info->clk[clk_idx];
  1398. clk_name = soc_info->clk_name[clk_idx];
  1399. clk_rate = (apply_level == -1) ?
  1400. 0 : soc_info->clk_rate[apply_level][clk_idx];
  1401. shared_clk_mask = soc_info->shared_clk_mask;
  1402. clk_id = soc_info->clk_id[clk_idx];
  1403. if (clk_idx == soc_info->src_clk_idx)
  1404. is_src_clk = true;
  1405. }
  1406. if (!clk)
  1407. return 0;
  1408. if (is_src_clk && soc_info->is_clk_drv_en && CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  1409. rc = cam_soc_util_set_cesta_clk_rate(soc_info, cesta_client_idx, clk_rate, clk_rate,
  1410. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].high,
  1411. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].low);
  1412. if (rc) {
  1413. CAM_ERR(CAM_UTIL,
  1414. "[%s] Failed in setting cesta clk rates[high low]:[%ld %ld] client_idx:%d rc:%d",
  1415. soc_info->dev_name, clk_rate, clk_rate, cesta_client_idx, rc);
  1416. return rc;
  1417. }
  1418. rc = cam_soc_util_cesta_channel_switch(cesta_client_idx, soc_info->dev_name);
  1419. if (rc) {
  1420. CAM_ERR(CAM_UTIL,
  1421. "[%s] Failed to apply power states for cesta client:%d rc:%d",
  1422. soc_info->dev_name, cesta_client_idx, rc);
  1423. return rc;
  1424. }
  1425. } else {
  1426. rc = cam_soc_util_set_clk_rate(soc_info, clk, clk_name, clk_rate,
  1427. CAM_IS_BIT_SET(shared_clk_mask, clk_idx), is_src_clk, clk_id,
  1428. &soc_info->applied_src_clk_rates.sw_client);
  1429. if (rc) {
  1430. CAM_ERR(CAM_UTIL, "[%s] Failed in setting clk rate %ld rc:%d",
  1431. soc_info->dev_name, clk_rate, rc);
  1432. return rc;
  1433. }
  1434. }
  1435. CAM_DBG(CAM_UTIL, "[%s] : clk enable %s", soc_info->dev_name, clk_name);
  1436. rc = cam_wrapper_clk_prepare_enable(clk);
  1437. if (rc) {
  1438. CAM_ERR(CAM_UTIL, "enable failed for %s: rc(%d)", clk_name, rc);
  1439. return rc;
  1440. }
  1441. return rc;
  1442. }
  1443. int cam_soc_util_clk_disable(struct cam_hw_soc_info *soc_info, int cesta_client_idx,
  1444. bool optional_clk, int32_t clk_idx)
  1445. {
  1446. int rc = 0;
  1447. struct clk *clk;
  1448. const char *clk_name;
  1449. uint32_t shared_clk_mask;
  1450. uint32_t clk_id;
  1451. if (!soc_info || (clk_idx < 0)) {
  1452. CAM_ERR(CAM_UTIL, "Invalid param %d", clk_idx);
  1453. return -EINVAL;
  1454. }
  1455. if (optional_clk) {
  1456. clk = soc_info->optional_clk[clk_idx];
  1457. clk_name = soc_info->optional_clk_name[clk_idx];
  1458. shared_clk_mask = soc_info->optional_shared_clk_mask;
  1459. clk_id = soc_info->optional_clk_id[clk_idx];
  1460. } else {
  1461. clk = soc_info->clk[clk_idx];
  1462. clk_name = soc_info->clk_name[clk_idx];
  1463. shared_clk_mask = soc_info->shared_clk_mask;
  1464. clk_id = soc_info->clk_id[clk_idx];
  1465. }
  1466. CAM_DBG(CAM_UTIL, "disable %s", clk_name);
  1467. if (!clk)
  1468. return 0;
  1469. cam_wrapper_clk_disable_unprepare(clk);
  1470. if ((clk_idx == soc_info->src_clk_idx) && soc_info->is_clk_drv_en &&
  1471. CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  1472. rc = cam_soc_util_set_cesta_clk_rate(soc_info, cesta_client_idx, 0, 0,
  1473. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].high,
  1474. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].low);
  1475. if (rc) {
  1476. CAM_ERR(CAM_UTIL,
  1477. "Failed in setting cesta clk rates[high low]:[0 0] client_idx:%d rc:%d",
  1478. cesta_client_idx, rc);
  1479. return rc;
  1480. }
  1481. rc = cam_soc_util_cesta_channel_switch(cesta_client_idx, soc_info->dev_name);
  1482. if (rc) {
  1483. CAM_ERR(CAM_CSIPHY,
  1484. "Failed to apply power states for cesta_client_idx:%d rc:%d",
  1485. cesta_client_idx, rc);
  1486. return rc;
  1487. }
  1488. } else {
  1489. if (CAM_IS_BIT_SET(shared_clk_mask, clk_idx)) {
  1490. CAM_DBG(CAM_UTIL,
  1491. "Dev %s clk %s Disabling Shared clk, set 0 rate",
  1492. soc_info->dev_name, clk_name);
  1493. cam_soc_util_clk_wrapper_set_clk_rate(clk_id, soc_info, clk, 0);
  1494. } else if (soc_info->mmrm_handle && (!skip_mmrm_set_rate) &&
  1495. (soc_info->src_clk_idx == clk_idx)) {
  1496. CAM_DBG(CAM_UTIL, "Dev %s Disabling %s clk, set 0 rate",
  1497. soc_info->dev_name, clk_name);
  1498. cam_soc_util_set_rate_through_mmrm(
  1499. soc_info->mmrm_handle,
  1500. soc_info->is_nrt_dev,
  1501. 0, 0, 1);
  1502. }
  1503. }
  1504. return 0;
  1505. }
  1506. /**
  1507. * cam_soc_util_clk_enable_default()
  1508. *
  1509. * @brief: This function enables the default clocks present
  1510. * in soc_info
  1511. *
  1512. * @soc_info: Device soc struct to be populated
  1513. * @cesta_client_idx: CESTA Client idx for hw client based src clocks
  1514. * @clk_level: Clk level to apply while enabling
  1515. *
  1516. * @return: success or failure
  1517. */
  1518. int cam_soc_util_clk_enable_default(struct cam_hw_soc_info *soc_info,
  1519. int cesta_client_idx, enum cam_vote_level clk_level)
  1520. {
  1521. int i, rc = 0;
  1522. enum cam_vote_level apply_level;
  1523. if ((soc_info->num_clk == 0) ||
  1524. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  1525. CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
  1526. soc_info->num_clk);
  1527. return -EINVAL;
  1528. }
  1529. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
  1530. &apply_level);
  1531. if (rc) {
  1532. CAM_ERR(CAM_UTIL, "[%s] : failed to get level clk_level=%d, rc=%d",
  1533. soc_info->dev_name, clk_level, rc);
  1534. return rc;
  1535. }
  1536. if (soc_info->cam_cx_ipeak_enable)
  1537. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
  1538. CAM_DBG(CAM_UTIL, "Dev[%s] : cesta client %d, request level %s, apply level %s",
  1539. soc_info->dev_name, cesta_client_idx,
  1540. cam_soc_util_get_string_from_level(clk_level),
  1541. cam_soc_util_get_string_from_level(apply_level));
  1542. memset(&soc_info->applied_src_clk_rates, 0, sizeof(struct cam_soc_util_clk_rates));
  1543. for (i = 0; i < soc_info->num_clk; i++) {
  1544. rc = cam_soc_util_clk_enable(soc_info, cesta_client_idx, false, i, apply_level);
  1545. if (rc) {
  1546. CAM_ERR(CAM_UTIL,
  1547. "[%s] : failed to enable clk apply_level=%d, rc=%d, cesta_client_idx=%d",
  1548. soc_info->dev_name, apply_level, rc, cesta_client_idx);
  1549. goto clk_disable;
  1550. }
  1551. if (soc_info->cam_cx_ipeak_enable)
  1552. CAM_DBG(CAM_UTIL,
  1553. "dev name = %s clk name = %s idx = %d apply_level = %d clc idx = %d",
  1554. soc_info->dev_name, soc_info->clk_name[i], i, apply_level, i);
  1555. }
  1556. return rc;
  1557. clk_disable:
  1558. if (soc_info->cam_cx_ipeak_enable)
  1559. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  1560. for (i--; i >= 0; i--) {
  1561. cam_soc_util_clk_disable(soc_info, cesta_client_idx, false, i);
  1562. }
  1563. return rc;
  1564. }
  1565. /**
  1566. * cam_soc_util_clk_disable_default()
  1567. *
  1568. * @brief: This function disables the default clocks present
  1569. * in soc_info
  1570. *
  1571. * @soc_info: device soc struct to be populated
  1572. * @cesta_client_idx: CESTA Client idx for hw client based src clocks
  1573. *
  1574. * @return: success or failure
  1575. */
  1576. void cam_soc_util_clk_disable_default(struct cam_hw_soc_info *soc_info,
  1577. int cesta_client_idx)
  1578. {
  1579. int i;
  1580. if (soc_info->num_clk == 0)
  1581. return;
  1582. if (soc_info->cam_cx_ipeak_enable)
  1583. cam_cx_ipeak_unvote_cx_ipeak(soc_info);
  1584. for (i = soc_info->num_clk - 1; i >= 0; i--)
  1585. cam_soc_util_clk_disable(soc_info, cesta_client_idx, false, i);
  1586. }
  1587. /**
  1588. * cam_soc_util_get_dt_clk_info()
  1589. *
  1590. * @brief: Parse the DT and populate the Clock properties
  1591. *
  1592. * @soc_info: device soc struct to be populated
  1593. * @src_clk_str name of src clock that has rate control
  1594. *
  1595. * @return: success or failure
  1596. */
  1597. static int cam_soc_util_get_dt_clk_info(struct cam_hw_soc_info *soc_info)
  1598. {
  1599. struct device_node *of_node = NULL;
  1600. int count;
  1601. int num_clk_rates, num_clk_levels;
  1602. int i, j, rc;
  1603. int32_t num_clk_level_strings;
  1604. const char *src_clk_str = NULL;
  1605. const char *scl_clk_str = NULL;
  1606. const char *clk_control_debugfs = NULL;
  1607. const char *clk_cntl_lvl_string = NULL;
  1608. enum cam_vote_level level;
  1609. int shared_clk_cnt;
  1610. struct of_phandle_args clk_args = {0};
  1611. if (!soc_info || !soc_info->dev)
  1612. return -EINVAL;
  1613. of_node = soc_info->dev->of_node;
  1614. if (!of_property_read_bool(of_node, "use-shared-clk")) {
  1615. CAM_DBG(CAM_UTIL, "No shared clk parameter defined");
  1616. soc_info->use_shared_clk = false;
  1617. } else {
  1618. soc_info->use_shared_clk = true;
  1619. }
  1620. count = of_property_count_strings(of_node, "clock-names");
  1621. CAM_DBG(CAM_UTIL, "E: dev_name = %s count = %d",
  1622. soc_info->dev_name, count);
  1623. if (count > CAM_SOC_MAX_CLK) {
  1624. CAM_ERR(CAM_UTIL, "invalid count of clocks, count=%d", count);
  1625. rc = -EINVAL;
  1626. return rc;
  1627. }
  1628. if (count <= 0) {
  1629. CAM_DBG(CAM_UTIL, "No clock-names found");
  1630. count = 0;
  1631. soc_info->num_clk = count;
  1632. return 0;
  1633. }
  1634. soc_info->num_clk = count;
  1635. for (i = 0; i < count; i++) {
  1636. rc = of_property_read_string_index(of_node, "clock-names",
  1637. i, &(soc_info->clk_name[i]));
  1638. CAM_DBG(CAM_UTIL, "clock-names[%d] = %s",
  1639. i, soc_info->clk_name[i]);
  1640. if (rc) {
  1641. CAM_ERR(CAM_UTIL,
  1642. "i= %d count= %d reading clock-names failed",
  1643. i, count);
  1644. return rc;
  1645. }
  1646. }
  1647. num_clk_rates = of_property_count_u32_elems(of_node, "clock-rates");
  1648. if (num_clk_rates <= 0) {
  1649. CAM_ERR(CAM_UTIL, "reading clock-rates count failed");
  1650. return -EINVAL;
  1651. }
  1652. if ((num_clk_rates % soc_info->num_clk) != 0) {
  1653. CAM_ERR(CAM_UTIL,
  1654. "mismatch clk/rates, No of clocks=%d, No of rates=%d",
  1655. soc_info->num_clk, num_clk_rates);
  1656. return -EINVAL;
  1657. }
  1658. num_clk_levels = (num_clk_rates / soc_info->num_clk);
  1659. num_clk_level_strings = of_property_count_strings(of_node,
  1660. "clock-cntl-level");
  1661. if (num_clk_level_strings != num_clk_levels) {
  1662. CAM_ERR(CAM_UTIL,
  1663. "Mismatch No of levels=%d, No of level string=%d",
  1664. num_clk_levels, num_clk_level_strings);
  1665. return -EINVAL;
  1666. }
  1667. soc_info->lowest_clk_level = CAM_TURBO_VOTE;
  1668. for (i = 0; i < num_clk_levels; i++) {
  1669. rc = of_property_read_string_index(of_node,
  1670. "clock-cntl-level", i, &clk_cntl_lvl_string);
  1671. if (rc) {
  1672. CAM_ERR(CAM_UTIL,
  1673. "Error reading clock-cntl-level, rc=%d", rc);
  1674. return rc;
  1675. }
  1676. rc = cam_soc_util_get_level_from_string(clk_cntl_lvl_string,
  1677. &level);
  1678. if (rc)
  1679. return rc;
  1680. CAM_DBG(CAM_UTIL,
  1681. "[%d] : %s %d", i, clk_cntl_lvl_string, level);
  1682. soc_info->clk_level_valid[level] = true;
  1683. for (j = 0; j < soc_info->num_clk; j++) {
  1684. rc = of_property_read_u32_index(of_node, "clock-rates",
  1685. ((i * soc_info->num_clk) + j),
  1686. &soc_info->clk_rate[level][j]);
  1687. if (rc) {
  1688. CAM_ERR(CAM_UTIL,
  1689. "Error reading clock-rates, rc=%d",
  1690. rc);
  1691. return rc;
  1692. }
  1693. soc_info->clk_rate[level][j] =
  1694. (soc_info->clk_rate[level][j] == 0) ?
  1695. (int32_t)NO_SET_RATE :
  1696. soc_info->clk_rate[level][j];
  1697. CAM_DBG(CAM_UTIL, "soc_info->clk_rate[%d][%d] = %d",
  1698. level, j,
  1699. soc_info->clk_rate[level][j]);
  1700. }
  1701. if ((level > CAM_MINSVS_VOTE) &&
  1702. (level < soc_info->lowest_clk_level))
  1703. soc_info->lowest_clk_level = level;
  1704. }
  1705. soc_info->src_clk_idx = -1;
  1706. rc = of_property_read_string_index(of_node, "src-clock-name", 0,
  1707. &src_clk_str);
  1708. if (rc || !src_clk_str) {
  1709. CAM_DBG(CAM_UTIL, "No src_clk_str found");
  1710. rc = 0;
  1711. goto end;
  1712. }
  1713. for (i = 0; i < soc_info->num_clk; i++) {
  1714. if (strcmp(soc_info->clk_name[i], src_clk_str) == 0) {
  1715. soc_info->src_clk_idx = i;
  1716. CAM_DBG(CAM_UTIL, "src clock = %s, index = %d",
  1717. src_clk_str, i);
  1718. }
  1719. rc = of_parse_phandle_with_args(of_node, "clocks",
  1720. "#clock-cells", i, &clk_args);
  1721. if (rc) {
  1722. CAM_ERR(CAM_CPAS,
  1723. "failed to clock info rc=%d", rc);
  1724. rc = -EINVAL;
  1725. goto end;
  1726. }
  1727. soc_info->clk_id[i] = clk_args.args[0];
  1728. of_node_put(clk_args.np);
  1729. CAM_DBG(CAM_UTIL, "Dev %s clk %s id %d",
  1730. soc_info->dev_name, soc_info->clk_name[i],
  1731. soc_info->clk_id[i]);
  1732. }
  1733. CAM_DBG(CAM_UTIL, "Dev %s src_clk_idx %d, lowest_clk_level %d",
  1734. soc_info->dev_name, soc_info->src_clk_idx,
  1735. soc_info->lowest_clk_level);
  1736. soc_info->shared_clk_mask = 0;
  1737. shared_clk_cnt = of_property_count_u32_elems(of_node, "shared-clks");
  1738. if (shared_clk_cnt <= 0) {
  1739. CAM_DBG(CAM_UTIL, "Dev %s, no shared clks", soc_info->dev_name);
  1740. } else if (shared_clk_cnt != count) {
  1741. CAM_ERR(CAM_UTIL, "Dev %s, incorrect shared clock count %d %d",
  1742. soc_info->dev_name, shared_clk_cnt, count);
  1743. rc = -EINVAL;
  1744. goto end;
  1745. } else {
  1746. uint32_t shared_clk_val;
  1747. for (i = 0; i < shared_clk_cnt; i++) {
  1748. rc = of_property_read_u32_index(of_node,
  1749. "shared-clks", i, &shared_clk_val);
  1750. if (rc || (shared_clk_val > 1)) {
  1751. CAM_ERR(CAM_UTIL,
  1752. "Incorrect shared clk info at %d, val=%d, count=%d",
  1753. i, shared_clk_val, shared_clk_cnt);
  1754. rc = -EINVAL;
  1755. goto end;
  1756. }
  1757. if (shared_clk_val)
  1758. CAM_SET_BIT(soc_info->shared_clk_mask, i);
  1759. }
  1760. CAM_DBG(CAM_UTIL, "Dev %s shared clk mask 0x%x",
  1761. soc_info->dev_name, soc_info->shared_clk_mask);
  1762. }
  1763. /* scalable clk info parsing */
  1764. soc_info->scl_clk_count = 0;
  1765. soc_info->scl_clk_count = of_property_count_strings(of_node,
  1766. "scl-clk-names");
  1767. if ((soc_info->scl_clk_count <= 0) ||
  1768. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  1769. if (soc_info->scl_clk_count == -EINVAL) {
  1770. CAM_DBG(CAM_UTIL, "scl_clk_name prop not avialable");
  1771. } else if ((soc_info->scl_clk_count == -ENODATA) ||
  1772. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  1773. CAM_ERR(CAM_UTIL, "Invalid scl_clk_count: %d",
  1774. soc_info->scl_clk_count);
  1775. return -EINVAL;
  1776. }
  1777. CAM_DBG(CAM_UTIL, "Invalid scl_clk count: %d",
  1778. soc_info->scl_clk_count);
  1779. soc_info->scl_clk_count = -1;
  1780. } else {
  1781. CAM_DBG(CAM_UTIL, "No of scalable clocks: %d",
  1782. soc_info->scl_clk_count);
  1783. for (i = 0; i < soc_info->scl_clk_count; i++) {
  1784. rc = of_property_read_string_index(of_node,
  1785. "scl-clk-names", i,
  1786. (const char **)&scl_clk_str);
  1787. if (rc || !scl_clk_str) {
  1788. CAM_WARN(CAM_UTIL, "scl_clk_str is NULL");
  1789. soc_info->scl_clk_idx[i] = -1;
  1790. continue;
  1791. }
  1792. for (j = 0; j < soc_info->num_clk; j++) {
  1793. if (strnstr(scl_clk_str, soc_info->clk_name[j],
  1794. strlen(scl_clk_str))) {
  1795. soc_info->scl_clk_idx[i] = j;
  1796. CAM_DBG(CAM_UTIL,
  1797. "scl clock = %s, index = %d",
  1798. scl_clk_str, j);
  1799. break;
  1800. }
  1801. }
  1802. }
  1803. }
  1804. rc = of_property_read_string_index(of_node,
  1805. "clock-control-debugfs", 0, &clk_control_debugfs);
  1806. if (rc || !clk_control_debugfs) {
  1807. CAM_DBG(CAM_UTIL, "No clock_control_debugfs property found");
  1808. rc = 0;
  1809. goto end;
  1810. }
  1811. if (strcmp("true", clk_control_debugfs) == 0)
  1812. soc_info->clk_control_enable = true;
  1813. CAM_DBG(CAM_UTIL, "X: dev_name = %s count = %d",
  1814. soc_info->dev_name, count);
  1815. end:
  1816. return rc;
  1817. }
  1818. int cam_soc_util_set_clk_rate_level(struct cam_hw_soc_info *soc_info,
  1819. int cesta_client_idx, enum cam_vote_level clk_level_high,
  1820. enum cam_vote_level clk_level_low, bool do_not_set_src_clk)
  1821. {
  1822. int i, rc = 0;
  1823. enum cam_vote_level apply_level_high;
  1824. enum cam_vote_level apply_level_low = soc_info->lowest_clk_level;
  1825. unsigned long applied_clk_rate;
  1826. if ((soc_info->num_clk == 0) ||
  1827. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  1828. CAM_ERR(CAM_UTIL, "Invalid number of clock %d", soc_info->num_clk);
  1829. return -EINVAL;
  1830. }
  1831. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level_high,
  1832. &apply_level_high);
  1833. if (rc) {
  1834. CAM_ERR(CAM_UTIL, "[%s] : failed to get level clk_level_high=%d, rc=%d",
  1835. soc_info->dev_name, clk_level_high, rc);
  1836. return rc;
  1837. }
  1838. if (soc_info->is_clk_drv_en && CAM_IS_VALID_CESTA_IDX(cesta_client_idx)) {
  1839. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level_low,
  1840. &apply_level_low);
  1841. if (rc) {
  1842. CAM_ERR(CAM_UTIL, "[%s] : failed to get level clk_level_low=%d, rc=%d",
  1843. soc_info->dev_name, clk_level_low, rc);
  1844. return rc;
  1845. }
  1846. }
  1847. if (soc_info->cam_cx_ipeak_enable)
  1848. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level_high);
  1849. for (i = 0; i < soc_info->num_clk; i++) {
  1850. if (do_not_set_src_clk && (i == soc_info->src_clk_idx)) {
  1851. CAM_DBG(CAM_UTIL, "Skipping set rate for src clk %s",
  1852. soc_info->clk_name[i]);
  1853. continue;
  1854. }
  1855. if (soc_info->is_clk_drv_en && CAM_IS_VALID_CESTA_IDX(cesta_client_idx) &&
  1856. (i == soc_info->src_clk_idx)) {
  1857. rc = cam_soc_util_set_cesta_clk_rate(soc_info, cesta_client_idx,
  1858. soc_info->clk_rate[apply_level_high][i],
  1859. soc_info->clk_rate[apply_level_low][i],
  1860. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].high,
  1861. &soc_info->applied_src_clk_rates.hw_client[cesta_client_idx].low);
  1862. if (rc) {
  1863. CAM_ERR(CAM_UTIL,
  1864. "Failed to set the req clk level[high low]: [%s %s] cesta_client_idx: %d",
  1865. cam_soc_util_get_string_from_level(apply_level_high),
  1866. cam_soc_util_get_string_from_level(apply_level_low),
  1867. cesta_client_idx);
  1868. break;
  1869. }
  1870. continue;
  1871. }
  1872. CAM_DBG(CAM_UTIL, "Set rate for clk %s rate %d", soc_info->clk_name[i],
  1873. soc_info->clk_rate[apply_level_high][i]);
  1874. rc = cam_soc_util_set_clk_rate(soc_info, soc_info->clk[i],
  1875. soc_info->clk_name[i],
  1876. soc_info->clk_rate[apply_level_high][i],
  1877. CAM_IS_BIT_SET(soc_info->shared_clk_mask, i),
  1878. (i == soc_info->src_clk_idx) ? true : false,
  1879. soc_info->clk_id[i],
  1880. &applied_clk_rate);
  1881. if (rc < 0) {
  1882. CAM_DBG(CAM_UTIL,
  1883. "dev name = %s clk_name = %s idx = %d apply_level = %s",
  1884. soc_info->dev_name, soc_info->clk_name[i],
  1885. i, cam_soc_util_get_string_from_level(apply_level_high));
  1886. if (soc_info->cam_cx_ipeak_enable)
  1887. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  1888. break;
  1889. }
  1890. if (i == soc_info->src_clk_idx)
  1891. soc_info->applied_src_clk_rates.sw_client = applied_clk_rate;
  1892. }
  1893. return rc;
  1894. };
  1895. static int cam_soc_util_get_dt_gpio_req_tbl(struct device_node *of_node,
  1896. struct cam_soc_gpio_data *gconf, uint16_t *gpio_array,
  1897. uint16_t gpio_array_size)
  1898. {
  1899. int32_t rc = 0, i = 0;
  1900. uint32_t count = 0;
  1901. uint32_t *val_array = NULL;
  1902. if (!of_get_property(of_node, "gpio-req-tbl-num", &count))
  1903. return 0;
  1904. count /= sizeof(uint32_t);
  1905. if (!count) {
  1906. CAM_ERR(CAM_UTIL, "gpio-req-tbl-num 0");
  1907. return 0;
  1908. }
  1909. val_array = kcalloc(count, sizeof(uint32_t), GFP_KERNEL);
  1910. if (!val_array)
  1911. return -ENOMEM;
  1912. gconf->cam_gpio_req_tbl = kcalloc(count, sizeof(struct gpio),
  1913. GFP_KERNEL);
  1914. if (!gconf->cam_gpio_req_tbl) {
  1915. rc = -ENOMEM;
  1916. goto free_val_array;
  1917. }
  1918. gconf->cam_gpio_req_tbl_size = count;
  1919. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-num",
  1920. val_array, count);
  1921. if (rc) {
  1922. CAM_ERR(CAM_UTIL, "failed in reading gpio-req-tbl-num, rc = %d",
  1923. rc);
  1924. goto free_gpio_req_tbl;
  1925. }
  1926. for (i = 0; i < count; i++) {
  1927. if (val_array[i] >= gpio_array_size) {
  1928. CAM_ERR(CAM_UTIL, "gpio req tbl index %d invalid",
  1929. val_array[i]);
  1930. goto free_gpio_req_tbl;
  1931. }
  1932. gconf->cam_gpio_req_tbl[i].gpio = gpio_array[val_array[i]];
  1933. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].gpio = %d", i,
  1934. gconf->cam_gpio_req_tbl[i].gpio);
  1935. }
  1936. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-flags",
  1937. val_array, count);
  1938. if (rc) {
  1939. CAM_ERR(CAM_UTIL, "Failed in gpio-req-tbl-flags, rc %d", rc);
  1940. goto free_gpio_req_tbl;
  1941. }
  1942. for (i = 0; i < count; i++) {
  1943. gconf->cam_gpio_req_tbl[i].flags = val_array[i];
  1944. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].flags = %ld", i,
  1945. gconf->cam_gpio_req_tbl[i].flags);
  1946. }
  1947. for (i = 0; i < count; i++) {
  1948. rc = of_property_read_string_index(of_node,
  1949. "gpio-req-tbl-label", i,
  1950. &gconf->cam_gpio_req_tbl[i].label);
  1951. if (rc) {
  1952. CAM_ERR(CAM_UTIL, "Failed rc %d", rc);
  1953. goto free_gpio_req_tbl;
  1954. }
  1955. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].label = %s", i,
  1956. gconf->cam_gpio_req_tbl[i].label);
  1957. }
  1958. kfree(val_array);
  1959. return rc;
  1960. free_gpio_req_tbl:
  1961. kfree(gconf->cam_gpio_req_tbl);
  1962. free_val_array:
  1963. kfree(val_array);
  1964. gconf->cam_gpio_req_tbl_size = 0;
  1965. return rc;
  1966. }
  1967. static int cam_soc_util_get_gpio_info(struct cam_hw_soc_info *soc_info)
  1968. {
  1969. int32_t rc = 0, i = 0;
  1970. uint16_t *gpio_array = NULL;
  1971. int16_t gpio_array_size = 0;
  1972. struct cam_soc_gpio_data *gconf = NULL;
  1973. struct device_node *of_node = NULL;
  1974. if (!soc_info || !soc_info->dev)
  1975. return -EINVAL;
  1976. of_node = soc_info->dev->of_node;
  1977. /* Validate input parameters */
  1978. if (!of_node) {
  1979. CAM_ERR(CAM_UTIL, "Invalid param of_node");
  1980. return -EINVAL;
  1981. }
  1982. gpio_array_size = of_gpio_count(of_node);
  1983. if (gpio_array_size <= 0)
  1984. return 0;
  1985. CAM_DBG(CAM_UTIL, "gpio count %d", gpio_array_size);
  1986. gpio_array = kcalloc(gpio_array_size, sizeof(uint16_t), GFP_KERNEL);
  1987. if (!gpio_array) {
  1988. rc = -ENOMEM;
  1989. goto err;
  1990. }
  1991. for (i = 0; i < gpio_array_size; i++) {
  1992. gpio_array[i] = of_get_gpio(of_node, i);
  1993. CAM_DBG(CAM_UTIL, "gpio_array[%d] = %d", i, gpio_array[i]);
  1994. }
  1995. gconf = kzalloc(sizeof(*gconf), GFP_KERNEL);
  1996. if (!gconf) {
  1997. rc = -ENOMEM;
  1998. goto free_gpio_array;
  1999. }
  2000. rc = cam_soc_util_get_dt_gpio_req_tbl(of_node, gconf, gpio_array,
  2001. gpio_array_size);
  2002. if (rc) {
  2003. CAM_ERR(CAM_UTIL, "failed in msm_camera_get_dt_gpio_req_tbl");
  2004. goto free_gpio_conf;
  2005. }
  2006. gconf->cam_gpio_common_tbl = kcalloc(gpio_array_size,
  2007. sizeof(struct gpio), GFP_KERNEL);
  2008. if (!gconf->cam_gpio_common_tbl) {
  2009. rc = -ENOMEM;
  2010. goto free_gpio_conf;
  2011. }
  2012. for (i = 0; i < gpio_array_size; i++)
  2013. gconf->cam_gpio_common_tbl[i].gpio = gpio_array[i];
  2014. gconf->cam_gpio_common_tbl_size = gpio_array_size;
  2015. soc_info->gpio_data = gconf;
  2016. kfree(gpio_array);
  2017. return rc;
  2018. free_gpio_conf:
  2019. kfree(gconf);
  2020. free_gpio_array:
  2021. kfree(gpio_array);
  2022. err:
  2023. soc_info->gpio_data = NULL;
  2024. return rc;
  2025. }
  2026. static int cam_soc_util_request_gpio_table(
  2027. struct cam_hw_soc_info *soc_info, bool gpio_en)
  2028. {
  2029. int rc = 0, i = 0;
  2030. uint8_t size = 0;
  2031. struct cam_soc_gpio_data *gpio_conf =
  2032. soc_info->gpio_data;
  2033. struct gpio *gpio_tbl = NULL;
  2034. if (!gpio_conf) {
  2035. CAM_DBG(CAM_UTIL, "No GPIO entry");
  2036. return 0;
  2037. }
  2038. if (gpio_conf->cam_gpio_common_tbl_size <= 0) {
  2039. CAM_ERR(CAM_UTIL, "GPIO table size is invalid");
  2040. return -EINVAL;
  2041. }
  2042. size = gpio_conf->cam_gpio_req_tbl_size;
  2043. gpio_tbl = gpio_conf->cam_gpio_req_tbl;
  2044. if (!gpio_tbl || !size) {
  2045. CAM_ERR(CAM_UTIL, "Invalid gpio_tbl %pK / size %d",
  2046. gpio_tbl, size);
  2047. return -EINVAL;
  2048. }
  2049. for (i = 0; i < size; i++) {
  2050. CAM_DBG(CAM_UTIL, "i=%d, gpio=%d dir=%ld", i,
  2051. gpio_tbl[i].gpio, gpio_tbl[i].flags);
  2052. }
  2053. if (gpio_en) {
  2054. for (i = 0; i < size; i++) {
  2055. rc = gpio_request_one(gpio_tbl[i].gpio,
  2056. gpio_tbl[i].flags, gpio_tbl[i].label);
  2057. if (rc) {
  2058. /*
  2059. * After GPIO request fails, contine to
  2060. * apply new gpios, outout a error message
  2061. * for driver bringup debug
  2062. */
  2063. CAM_ERR(CAM_UTIL, "gpio %d:%s request fails",
  2064. gpio_tbl[i].gpio, gpio_tbl[i].label);
  2065. }
  2066. }
  2067. } else {
  2068. gpio_free_array(gpio_tbl, size);
  2069. }
  2070. return rc;
  2071. }
  2072. static int cam_soc_util_get_dt_regulator_info
  2073. (struct cam_hw_soc_info *soc_info)
  2074. {
  2075. int rc = 0, count = 0, i = 0;
  2076. struct device_node *of_node = NULL;
  2077. if (!soc_info || !soc_info->dev) {
  2078. CAM_ERR(CAM_UTIL, "Invalid parameters");
  2079. return -EINVAL;
  2080. }
  2081. of_node = soc_info->dev->of_node;
  2082. soc_info->num_rgltr = 0;
  2083. count = of_property_count_strings(of_node, "regulator-names");
  2084. if (count != -EINVAL) {
  2085. if (count <= 0) {
  2086. CAM_ERR(CAM_UTIL, "no regulators found");
  2087. return -EINVAL;
  2088. }
  2089. soc_info->num_rgltr = count;
  2090. } else {
  2091. CAM_DBG(CAM_UTIL, "No regulators node found");
  2092. return 0;
  2093. }
  2094. if (soc_info->num_rgltr > CAM_SOC_MAX_REGULATOR) {
  2095. CAM_ERR(CAM_UTIL, "Invalid regulator count:%d",
  2096. soc_info->num_rgltr);
  2097. return -EINVAL;
  2098. }
  2099. for (i = 0; i < soc_info->num_rgltr; i++) {
  2100. rc = of_property_read_string_index(of_node,
  2101. "regulator-names", i, &soc_info->rgltr_name[i]);
  2102. CAM_DBG(CAM_UTIL, "rgltr_name[%d] = %s",
  2103. i, soc_info->rgltr_name[i]);
  2104. if (rc) {
  2105. CAM_ERR(CAM_UTIL, "no regulator resource at cnt=%d", i);
  2106. return -ENODEV;
  2107. }
  2108. }
  2109. if (!of_property_read_bool(of_node, "rgltr-cntrl-support")) {
  2110. CAM_DBG(CAM_UTIL, "No regulator control parameter defined");
  2111. soc_info->rgltr_ctrl_support = false;
  2112. return 0;
  2113. }
  2114. soc_info->rgltr_ctrl_support = true;
  2115. rc = of_property_read_u32_array(of_node, "rgltr-min-voltage",
  2116. soc_info->rgltr_min_volt, soc_info->num_rgltr);
  2117. if (rc) {
  2118. CAM_ERR(CAM_UTIL, "No minimum volatage value found, rc=%d", rc);
  2119. return -EINVAL;
  2120. }
  2121. rc = of_property_read_u32_array(of_node, "rgltr-max-voltage",
  2122. soc_info->rgltr_max_volt, soc_info->num_rgltr);
  2123. if (rc) {
  2124. CAM_ERR(CAM_UTIL, "No maximum volatage value found, rc=%d", rc);
  2125. return -EINVAL;
  2126. }
  2127. rc = of_property_read_u32_array(of_node, "rgltr-load-current",
  2128. soc_info->rgltr_op_mode, soc_info->num_rgltr);
  2129. if (rc) {
  2130. CAM_ERR(CAM_UTIL, "No Load curent found rc=%d", rc);
  2131. return -EINVAL;
  2132. }
  2133. return rc;
  2134. }
  2135. #ifdef CONFIG_CAM_PRESIL
  2136. static uint32_t next_dummy_irq_line_num = 0x000f;
  2137. struct resource dummy_irq_line[512];
  2138. #endif
  2139. int cam_soc_util_get_dt_properties(struct cam_hw_soc_info *soc_info)
  2140. {
  2141. struct device_node *of_node = NULL;
  2142. int count = 0, i = 0, rc = 0;
  2143. if (!soc_info || !soc_info->dev)
  2144. return -EINVAL;
  2145. of_node = soc_info->dev->of_node;
  2146. rc = of_property_read_u32(of_node, "cell-index", &soc_info->index);
  2147. if (rc) {
  2148. CAM_ERR(CAM_UTIL, "device %s failed to read cell-index",
  2149. soc_info->dev_name);
  2150. return rc;
  2151. }
  2152. count = of_property_count_strings(of_node, "reg-names");
  2153. if (count <= 0) {
  2154. CAM_DBG(CAM_UTIL, "no reg-names found for: %s",
  2155. soc_info->dev_name);
  2156. count = 0;
  2157. }
  2158. soc_info->num_mem_block = count;
  2159. for (i = 0; i < soc_info->num_mem_block; i++) {
  2160. rc = of_property_read_string_index(of_node, "reg-names", i,
  2161. &soc_info->mem_block_name[i]);
  2162. if (rc) {
  2163. CAM_ERR(CAM_UTIL, "failed to read reg-names at %d", i);
  2164. return rc;
  2165. }
  2166. soc_info->mem_block[i] =
  2167. platform_get_resource_byname(soc_info->pdev,
  2168. IORESOURCE_MEM, soc_info->mem_block_name[i]);
  2169. if (!soc_info->mem_block[i]) {
  2170. CAM_ERR(CAM_UTIL, "no mem resource by name %s",
  2171. soc_info->mem_block_name[i]);
  2172. rc = -ENODEV;
  2173. return rc;
  2174. }
  2175. }
  2176. rc = of_property_read_string(of_node, "label", &soc_info->label_name);
  2177. if (rc)
  2178. CAM_DBG(CAM_UTIL, "Label is not available in the node: %d", rc);
  2179. if (soc_info->num_mem_block > 0) {
  2180. rc = of_property_read_u32_array(of_node, "reg-cam-base",
  2181. soc_info->mem_block_cam_base, soc_info->num_mem_block);
  2182. if (rc) {
  2183. CAM_ERR(CAM_UTIL, "Error reading register offsets");
  2184. return rc;
  2185. }
  2186. }
  2187. count = of_property_count_strings(of_node, "interrupt-names");
  2188. if (count <= 0) {
  2189. CAM_DBG(CAM_UTIL, "No interrupt line present for: %s", soc_info->dev_name);
  2190. soc_info->irq_count = 0;
  2191. } else {
  2192. if (count > CAM_SOC_MAX_IRQ_LINES_PER_DEV) {
  2193. CAM_ERR(CAM_UTIL,
  2194. "Number of interrupt: %d exceeds maximum allowable interrupts: %d",
  2195. count, CAM_SOC_MAX_IRQ_LINES_PER_DEV);
  2196. return -EINVAL;
  2197. }
  2198. soc_info->irq_count = count;
  2199. for (i = 0; i < soc_info->irq_count; i++) {
  2200. rc = of_property_read_string_index(of_node, "interrupt-names",
  2201. i, &soc_info->irq_name[i]);
  2202. if (rc) {
  2203. CAM_ERR(CAM_UTIL, "failed to read interrupt name at %d", i);
  2204. return rc;
  2205. }
  2206. }
  2207. rc = cam_compat_util_get_irq(soc_info);
  2208. if (rc < 0) {
  2209. CAM_ERR(CAM_UTIL, "get irq resource failed: %d for: %s",
  2210. rc, soc_info->dev_name);
  2211. #ifndef CONFIG_CAM_PRESIL
  2212. return rc;
  2213. #else
  2214. /* Pre-sil for new devices not present on old */
  2215. for (i = 0; i < soc_info->irq_count; i++) {
  2216. soc_info->irq_line[i] =
  2217. &dummy_irq_line[next_dummy_irq_line_num++];
  2218. CAM_DBG(CAM_PRESIL,
  2219. "interrupt line for dev %s irq name %s number %d",
  2220. soc_info->dev_name, soc_info->irq_name[i],
  2221. soc_info->irq_line[i]->start);
  2222. }
  2223. #endif
  2224. }
  2225. }
  2226. rc = of_property_read_string_index(of_node, "compatible", 0,
  2227. (const char **)&soc_info->compatible);
  2228. if (rc)
  2229. CAM_DBG(CAM_UTIL, "No compatible string present for: %s",
  2230. soc_info->dev_name);
  2231. soc_info->is_nrt_dev = false;
  2232. if (of_property_read_bool(of_node, "nrt-device"))
  2233. soc_info->is_nrt_dev = true;
  2234. CAM_DBG(CAM_UTIL, "Dev %s, nrt_dev %d",
  2235. soc_info->dev_name, soc_info->is_nrt_dev);
  2236. rc = cam_soc_util_get_dt_regulator_info(soc_info);
  2237. if (rc)
  2238. return rc;
  2239. rc = cam_soc_util_get_dt_clk_info(soc_info);
  2240. if (rc)
  2241. return rc;
  2242. rc = cam_soc_util_get_gpio_info(soc_info);
  2243. if (rc)
  2244. return rc;
  2245. if (of_find_property(of_node, "qcom,cam-cx-ipeak", NULL))
  2246. rc = cam_cx_ipeak_register_cx_ipeak(soc_info);
  2247. return rc;
  2248. }
  2249. /**
  2250. * cam_soc_util_get_regulator()
  2251. *
  2252. * @brief: Get regulator resource named vdd
  2253. *
  2254. * @dev: Device associated with regulator
  2255. * @reg: Return pointer to be filled with regulator on success
  2256. * @rgltr_name: Name of regulator to get
  2257. *
  2258. * @return: 0 for Success, negative value for failure
  2259. */
  2260. static int cam_soc_util_get_regulator(struct device *dev,
  2261. struct regulator **reg, const char *rgltr_name)
  2262. {
  2263. int rc = 0;
  2264. *reg = cam_wrapper_regulator_get(dev, rgltr_name);
  2265. if (IS_ERR_OR_NULL(*reg)) {
  2266. rc = PTR_ERR(*reg);
  2267. rc = rc ? rc : -EINVAL;
  2268. CAM_ERR(CAM_UTIL, "Regulator %s get failed %d", rgltr_name, rc);
  2269. *reg = NULL;
  2270. }
  2271. return rc;
  2272. }
  2273. int cam_soc_util_regulator_disable(struct regulator *rgltr,
  2274. const char *rgltr_name, uint32_t rgltr_min_volt,
  2275. uint32_t rgltr_max_volt, uint32_t rgltr_op_mode,
  2276. uint32_t rgltr_delay_ms)
  2277. {
  2278. int32_t rc = 0;
  2279. if (!rgltr) {
  2280. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  2281. return -EINVAL;
  2282. }
  2283. rc = cam_wrapper_regulator_disable(rgltr);
  2284. if (rc) {
  2285. CAM_ERR(CAM_UTIL, "%s regulator disable failed", rgltr_name);
  2286. return rc;
  2287. }
  2288. if (rgltr_delay_ms > 20)
  2289. msleep(rgltr_delay_ms);
  2290. else if (rgltr_delay_ms)
  2291. usleep_range(rgltr_delay_ms * 1000,
  2292. (rgltr_delay_ms * 1000) + 1000);
  2293. if (cam_wrapper_regulator_count_voltages(rgltr) > 0) {
  2294. cam_wrapper_regulator_set_load(rgltr, 0);
  2295. cam_wrapper_regulator_set_voltage(rgltr, 0, rgltr_max_volt);
  2296. }
  2297. return rc;
  2298. }
  2299. int cam_soc_util_regulator_enable(struct regulator *rgltr,
  2300. const char *rgltr_name,
  2301. uint32_t rgltr_min_volt, uint32_t rgltr_max_volt,
  2302. uint32_t rgltr_op_mode, uint32_t rgltr_delay)
  2303. {
  2304. int32_t rc = 0;
  2305. if (!rgltr) {
  2306. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  2307. return -EINVAL;
  2308. }
  2309. if (cam_wrapper_regulator_count_voltages(rgltr) > 0) {
  2310. CAM_DBG(CAM_UTIL, "[%s] voltage min=%d, max=%d",
  2311. rgltr_name, rgltr_min_volt, rgltr_max_volt);
  2312. rc = cam_wrapper_regulator_set_voltage(
  2313. rgltr, rgltr_min_volt, rgltr_max_volt);
  2314. if (rc) {
  2315. CAM_ERR(CAM_UTIL, "%s set voltage failed", rgltr_name);
  2316. return rc;
  2317. }
  2318. rc = cam_wrapper_regulator_set_load(rgltr, rgltr_op_mode);
  2319. if (rc) {
  2320. CAM_ERR(CAM_UTIL, "%s set optimum mode failed",
  2321. rgltr_name);
  2322. return rc;
  2323. }
  2324. }
  2325. rc = cam_wrapper_regulator_enable(rgltr);
  2326. if (rc) {
  2327. CAM_ERR(CAM_UTIL, "%s regulator_enable failed", rgltr_name);
  2328. return rc;
  2329. }
  2330. if (rgltr_delay > 20)
  2331. msleep(rgltr_delay);
  2332. else if (rgltr_delay)
  2333. usleep_range(rgltr_delay * 1000,
  2334. (rgltr_delay * 1000) + 1000);
  2335. return rc;
  2336. }
  2337. int cam_soc_util_select_pinctrl_state(struct cam_hw_soc_info *soc_info,
  2338. int pctrl_idx, bool active)
  2339. {
  2340. int rc = 0;
  2341. struct cam_soc_pinctrl_info *pctrl_info = &soc_info->pinctrl_info;
  2342. if (pctrl_idx >= CAM_SOC_MAX_PINCTRL_MAP) {
  2343. CAM_ERR(CAM_UTIL, "Invalid Map idx: %d max supported: %d",
  2344. pctrl_idx, CAM_SOC_MAX_PINCTRL_MAP);
  2345. return -EINVAL;
  2346. }
  2347. if (pctrl_info->pctrl_state[pctrl_idx].gpio_state_active &&
  2348. active &&
  2349. !pctrl_info->pctrl_state[pctrl_idx].is_active) {
  2350. rc = pinctrl_select_state(pctrl_info->pinctrl,
  2351. pctrl_info->pctrl_state[pctrl_idx].gpio_state_active);
  2352. if (rc)
  2353. CAM_ERR(CAM_UTIL,
  2354. "Pinctrl active state transition failed: rc: %d",
  2355. rc);
  2356. else {
  2357. pctrl_info->pctrl_state[pctrl_idx].is_active = true;
  2358. CAM_DBG(CAM_UTIL, "Pctrl_idx: %d is in active state",
  2359. pctrl_idx);
  2360. }
  2361. }
  2362. if (pctrl_info->pctrl_state[pctrl_idx].gpio_state_suspend &&
  2363. !active &&
  2364. pctrl_info->pctrl_state[pctrl_idx].is_active) {
  2365. rc = pinctrl_select_state(pctrl_info->pinctrl,
  2366. pctrl_info->pctrl_state[pctrl_idx].gpio_state_suspend);
  2367. if (rc)
  2368. CAM_ERR(CAM_UTIL,
  2369. "Pinctrl suspend state transition failed: rc: %d",
  2370. rc);
  2371. else {
  2372. pctrl_info->pctrl_state[pctrl_idx].is_active = false;
  2373. CAM_DBG(CAM_UTIL, "Pctrl_idx: %d is in suspend state",
  2374. pctrl_idx);
  2375. }
  2376. }
  2377. return rc;
  2378. }
  2379. static int cam_soc_util_request_pinctrl(
  2380. struct cam_hw_soc_info *soc_info)
  2381. {
  2382. struct cam_soc_pinctrl_info *device_pctrl = &soc_info->pinctrl_info;
  2383. struct device *dev = soc_info->dev;
  2384. struct device_node *of_node = dev->of_node;
  2385. uint32_t i = 0;
  2386. int rc = 0;
  2387. const char *name;
  2388. uint32_t idx;
  2389. char pctrl_active[50];
  2390. char pctrl_suspend[50];
  2391. int32_t num_of_map_idx = 0;
  2392. int32_t num_of_string = 0;
  2393. device_pctrl->pinctrl = devm_pinctrl_get(dev);
  2394. if (IS_ERR_OR_NULL(device_pctrl->pinctrl)) {
  2395. CAM_DBG(CAM_UTIL, "Pinctrl not available");
  2396. device_pctrl->pinctrl = NULL;
  2397. return 0;
  2398. }
  2399. num_of_map_idx = of_property_count_u32_elems(
  2400. of_node, "pctrl-idx-mapping");
  2401. if (num_of_map_idx <= 0) {
  2402. CAM_ERR(CAM_UTIL,
  2403. "Reading pctrl-idx-mapping failed");
  2404. return -EINVAL;
  2405. }
  2406. num_of_string = of_property_count_strings(
  2407. of_node, "pctrl-map-names");
  2408. if (num_of_string <= 0) {
  2409. CAM_ERR(CAM_UTIL, "no pinctrl-mapping found for: %s",
  2410. soc_info->dev_name);
  2411. device_pctrl->pinctrl = NULL;
  2412. return -EINVAL;
  2413. }
  2414. if (num_of_map_idx != num_of_string) {
  2415. CAM_ERR(CAM_UTIL,
  2416. "Incorrect inputs mapping-idx count: %d mapping-names: %d",
  2417. num_of_map_idx, num_of_string);
  2418. device_pctrl->pinctrl = NULL;
  2419. return -EINVAL;
  2420. }
  2421. if (num_of_map_idx > CAM_SOC_MAX_PINCTRL_MAP) {
  2422. CAM_ERR(CAM_UTIL, "Invalid mapping %u max supported: %d",
  2423. num_of_map_idx, CAM_SOC_MAX_PINCTRL_MAP);
  2424. return -EINVAL;
  2425. }
  2426. for (i = 0; i < num_of_map_idx; i++) {
  2427. of_property_read_u32_index(of_node,
  2428. "pctrl-idx-mapping", i, &idx);
  2429. if (idx >= CAM_SOC_MAX_PINCTRL_MAP) {
  2430. CAM_ERR(CAM_UTIL, "Invalid Index: %d max supported: %d",
  2431. idx, CAM_SOC_MAX_PINCTRL_MAP);
  2432. return -EINVAL;
  2433. }
  2434. rc = of_property_read_string_index(
  2435. of_node, "pctrl-map-names", i, &name);
  2436. if (rc) {
  2437. CAM_ERR(CAM_UTIL,
  2438. "failed to read pinctrl-mapping at %d", i);
  2439. return rc;
  2440. }
  2441. snprintf(pctrl_active, sizeof(pctrl_active),
  2442. "%s%s", name, "_active");
  2443. CAM_DBG(CAM_UTIL, "pctrl_active at index: %d name: %s",
  2444. i, pctrl_active);
  2445. snprintf(pctrl_suspend, sizeof(pctrl_suspend),
  2446. "%s%s", name, "_suspend");
  2447. CAM_DBG(CAM_UTIL, "pctrl_suspend at index: %d name: %s",
  2448. i, pctrl_suspend);
  2449. device_pctrl->pctrl_state[idx].gpio_state_active =
  2450. pinctrl_lookup_state(device_pctrl->pinctrl,
  2451. pctrl_active);
  2452. if (IS_ERR_OR_NULL(
  2453. device_pctrl->pctrl_state[idx].gpio_state_active)) {
  2454. CAM_ERR(CAM_UTIL,
  2455. "Failed to get the active state pinctrl handle");
  2456. device_pctrl->pctrl_state[idx].gpio_state_active =
  2457. NULL;
  2458. return -EINVAL;
  2459. }
  2460. device_pctrl->pctrl_state[idx].gpio_state_suspend =
  2461. pinctrl_lookup_state(device_pctrl->pinctrl,
  2462. pctrl_suspend);
  2463. if (IS_ERR_OR_NULL(
  2464. device_pctrl->pctrl_state[idx].gpio_state_suspend)) {
  2465. CAM_ERR(CAM_UTIL,
  2466. "Failed to get the active state pinctrl handle");
  2467. device_pctrl->pctrl_state[idx].gpio_state_suspend = NULL;
  2468. return -EINVAL;
  2469. }
  2470. }
  2471. return 0;
  2472. }
  2473. static void cam_soc_util_release_pinctrl(struct cam_hw_soc_info *soc_info)
  2474. {
  2475. if (soc_info->pinctrl_info.pinctrl)
  2476. devm_pinctrl_put(soc_info->pinctrl_info.pinctrl);
  2477. }
  2478. static void cam_soc_util_regulator_disable_default(
  2479. struct cam_hw_soc_info *soc_info)
  2480. {
  2481. int j = 0;
  2482. uint32_t num_rgltr = soc_info->num_rgltr;
  2483. for (j = num_rgltr-1; j >= 0; j--) {
  2484. if (soc_info->rgltr_ctrl_support == true) {
  2485. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  2486. soc_info->rgltr_name[j],
  2487. soc_info->rgltr_min_volt[j],
  2488. soc_info->rgltr_max_volt[j],
  2489. soc_info->rgltr_op_mode[j],
  2490. soc_info->rgltr_delay[j]);
  2491. } else {
  2492. if (soc_info->rgltr[j])
  2493. cam_wrapper_regulator_disable(soc_info->rgltr[j]);
  2494. }
  2495. }
  2496. }
  2497. static int cam_soc_util_regulator_enable_default(
  2498. struct cam_hw_soc_info *soc_info)
  2499. {
  2500. int j = 0, rc = 0;
  2501. uint32_t num_rgltr = soc_info->num_rgltr;
  2502. if (num_rgltr > CAM_SOC_MAX_REGULATOR) {
  2503. CAM_ERR(CAM_UTIL,
  2504. "%s has invalid regulator number %d",
  2505. soc_info->dev_name, num_rgltr);
  2506. return -EINVAL;
  2507. }
  2508. for (j = 0; j < num_rgltr; j++) {
  2509. CAM_DBG(CAM_UTIL, "[%s] : start regulator %s enable, rgltr_ctrl_support %d",
  2510. soc_info->dev_name, soc_info->rgltr_name[j], soc_info->rgltr_ctrl_support);
  2511. if (soc_info->rgltr_ctrl_support == true) {
  2512. rc = cam_soc_util_regulator_enable(soc_info->rgltr[j],
  2513. soc_info->rgltr_name[j],
  2514. soc_info->rgltr_min_volt[j],
  2515. soc_info->rgltr_max_volt[j],
  2516. soc_info->rgltr_op_mode[j],
  2517. soc_info->rgltr_delay[j]);
  2518. } else {
  2519. if (soc_info->rgltr[j])
  2520. rc = cam_wrapper_regulator_enable(soc_info->rgltr[j]);
  2521. }
  2522. if (rc) {
  2523. CAM_ERR(CAM_UTIL, "%s enable failed",
  2524. soc_info->rgltr_name[j]);
  2525. goto disable_rgltr;
  2526. }
  2527. }
  2528. return rc;
  2529. disable_rgltr:
  2530. for (j--; j >= 0; j--) {
  2531. if (soc_info->rgltr_ctrl_support == true) {
  2532. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  2533. soc_info->rgltr_name[j],
  2534. soc_info->rgltr_min_volt[j],
  2535. soc_info->rgltr_max_volt[j],
  2536. soc_info->rgltr_op_mode[j],
  2537. soc_info->rgltr_delay[j]);
  2538. } else {
  2539. if (soc_info->rgltr[j])
  2540. cam_wrapper_regulator_disable(soc_info->rgltr[j]);
  2541. }
  2542. }
  2543. return rc;
  2544. }
  2545. static bool cam_soc_util_is_presil_address_space(unsigned long mem_block_start)
  2546. {
  2547. if(mem_block_start >= CAM_SS_START_PRESIL && mem_block_start < CAM_SS_START)
  2548. return true;
  2549. return false;
  2550. }
  2551. #ifndef CONFIG_CAM_PRESIL
  2552. void __iomem * cam_soc_util_get_mem_base(
  2553. unsigned long mem_block_start,
  2554. unsigned long mem_block_size,
  2555. const char *mem_block_name,
  2556. uint32_t reserve_mem)
  2557. {
  2558. void __iomem * mem_base;
  2559. if (reserve_mem) {
  2560. if (!request_mem_region(mem_block_start,
  2561. mem_block_size,
  2562. mem_block_name)) {
  2563. CAM_ERR(CAM_UTIL,
  2564. "Error Mem region request Failed:%s",
  2565. mem_block_name);
  2566. return NULL;
  2567. }
  2568. }
  2569. mem_base = ioremap(mem_block_start, mem_block_size);
  2570. if (!mem_base) {
  2571. CAM_ERR(CAM_UTIL, "get mem base failed");
  2572. }
  2573. return mem_base;
  2574. }
  2575. int cam_soc_util_request_irq(struct device *dev,
  2576. unsigned int irq_line_start,
  2577. irq_handler_t handler,
  2578. unsigned long irqflags,
  2579. const char *irq_name,
  2580. void *irq_data,
  2581. unsigned long mem_block_start)
  2582. {
  2583. int rc;
  2584. rc = devm_request_irq(dev,
  2585. irq_line_start,
  2586. handler,
  2587. IRQF_TRIGGER_RISING,
  2588. irq_name,
  2589. irq_data);
  2590. if (rc) {
  2591. CAM_ERR(CAM_UTIL, "irq request fail rc %d", rc);
  2592. return -EBUSY;
  2593. }
  2594. disable_irq(irq_line_start);
  2595. return rc;
  2596. }
  2597. #else
  2598. void __iomem * cam_soc_util_get_mem_base(
  2599. unsigned long mem_block_start,
  2600. unsigned long mem_block_size,
  2601. const char *mem_block_name,
  2602. uint32_t reserve_mem)
  2603. {
  2604. void __iomem * mem_base;
  2605. if(cam_soc_util_is_presil_address_space(mem_block_start))
  2606. mem_base = (void __iomem *)mem_block_start;
  2607. else {
  2608. if (reserve_mem) {
  2609. if (!request_mem_region(mem_block_start,
  2610. mem_block_size,
  2611. mem_block_name)) {
  2612. CAM_ERR(CAM_UTIL,
  2613. "Error Mem region request Failed:%s",
  2614. mem_block_name);
  2615. return NULL;
  2616. }
  2617. }
  2618. mem_base = ioremap(mem_block_start, mem_block_size);
  2619. }
  2620. if (!mem_base) {
  2621. CAM_ERR(CAM_UTIL, "get mem base failed");
  2622. }
  2623. return mem_base;
  2624. }
  2625. int cam_soc_util_request_irq(struct device *dev,
  2626. unsigned int irq_line_start,
  2627. irq_handler_t handler,
  2628. unsigned long irqflags,
  2629. const char *irq_name,
  2630. void *irq_data,
  2631. unsigned long mem_block_start)
  2632. {
  2633. int rc;
  2634. if(cam_soc_util_is_presil_address_space(mem_block_start)) {
  2635. rc = devm_request_irq(dev,
  2636. irq_line_start,
  2637. handler,
  2638. irqflags,
  2639. irq_name,
  2640. irq_data);
  2641. if (rc) {
  2642. CAM_ERR(CAM_UTIL, "presil irq request fail");
  2643. return -EBUSY;
  2644. }
  2645. disable_irq(irq_line_start);
  2646. rc = !(cam_presil_subscribe_device_irq(irq_line_start,
  2647. handler, irq_data, irq_name));
  2648. CAM_DBG(CAM_PRESIL, "Subscribe presil IRQ: rc=%d NUM=%d Name=%s handler=0x%x",
  2649. rc, irq_line_start, irq_name, handler);
  2650. if (rc) {
  2651. CAM_ERR(CAM_UTIL, "presil irq request fail");
  2652. return -EBUSY;
  2653. }
  2654. } else {
  2655. rc = devm_request_irq(dev,
  2656. irq_line_start,
  2657. handler,
  2658. irqflags,
  2659. irq_name,
  2660. irq_data);
  2661. if (rc) {
  2662. CAM_ERR(CAM_UTIL, "irq request fail");
  2663. return -EBUSY;
  2664. }
  2665. disable_irq(irq_line_start);
  2666. CAM_INFO(CAM_UTIL, "Subscribe for non-presil IRQ success");
  2667. }
  2668. CAM_INFO(CAM_UTIL, "returning IRQ for mem_block_start 0x%0x rc %d",
  2669. mem_block_start, rc);
  2670. return rc;
  2671. }
  2672. #endif
  2673. int cam_soc_util_request_platform_resource(
  2674. struct cam_hw_soc_info *soc_info,
  2675. irq_handler_t handler, void **irq_data)
  2676. {
  2677. int i = 0, rc = 0;
  2678. if (!soc_info || !soc_info->dev) {
  2679. CAM_ERR(CAM_UTIL, "Invalid parameters");
  2680. return -EINVAL;
  2681. }
  2682. if (unlikely(soc_info->irq_count > CAM_SOC_MAX_IRQ_LINES_PER_DEV)) {
  2683. CAM_ERR(CAM_UTIL, "Invalid irq count: %u Max IRQ per device: %d",
  2684. soc_info->irq_count, CAM_SOC_MAX_IRQ_LINES_PER_DEV);
  2685. return -EINVAL;
  2686. }
  2687. for (i = 0; i < soc_info->num_mem_block; i++) {
  2688. soc_info->reg_map[i].mem_base = cam_soc_util_get_mem_base(
  2689. soc_info->mem_block[i]->start,
  2690. resource_size(soc_info->mem_block[i]),
  2691. soc_info->mem_block_name[i],
  2692. soc_info->reserve_mem);
  2693. if (!soc_info->reg_map[i].mem_base) {
  2694. CAM_ERR(CAM_UTIL, "i= %d base NULL", i);
  2695. rc = -ENOMEM;
  2696. goto unmap_base;
  2697. }
  2698. soc_info->reg_map[i].mem_cam_base =
  2699. soc_info->mem_block_cam_base[i];
  2700. soc_info->reg_map[i].size =
  2701. resource_size(soc_info->mem_block[i]);
  2702. soc_info->num_reg_map++;
  2703. }
  2704. for (i = 0; i < soc_info->num_rgltr; i++) {
  2705. if (soc_info->rgltr_name[i] == NULL) {
  2706. CAM_ERR(CAM_UTIL, "can't find regulator name");
  2707. goto put_regulator;
  2708. }
  2709. rc = cam_soc_util_get_regulator(soc_info->dev,
  2710. &soc_info->rgltr[i],
  2711. soc_info->rgltr_name[i]);
  2712. if (rc)
  2713. goto put_regulator;
  2714. }
  2715. for (i = 0; i < soc_info->irq_count; i++) {
  2716. rc = cam_soc_util_request_irq(soc_info->dev, soc_info->irq_num[i],
  2717. handler, IRQF_TRIGGER_RISING, soc_info->irq_name[i],
  2718. irq_data[i], soc_info->mem_block[0]->start);
  2719. if (rc) {
  2720. CAM_ERR(CAM_UTIL, "irq request fail for irq name: %s dev: %s",
  2721. soc_info->irq_name[i], soc_info->dev_name);
  2722. rc = -EBUSY;
  2723. goto put_irq;
  2724. }
  2725. soc_info->irq_data[i] = irq_data[i];
  2726. }
  2727. /* Get Clock */
  2728. for (i = 0; i < soc_info->num_clk; i++) {
  2729. soc_info->clk[i] = cam_wrapper_clk_get(soc_info->dev,
  2730. soc_info->clk_name[i]);
  2731. if (IS_ERR(soc_info->clk[i])) {
  2732. CAM_ERR(CAM_UTIL, "get failed for %s",
  2733. soc_info->clk_name[i]);
  2734. rc = -ENOENT;
  2735. goto put_clk;
  2736. } else if (!soc_info->clk[i]) {
  2737. CAM_DBG(CAM_UTIL, "%s handle is NULL skip get",
  2738. soc_info->clk_name[i]);
  2739. continue;
  2740. }
  2741. /* Create a wrapper entry if this is a shared clock */
  2742. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i)) {
  2743. uint32_t min_level = soc_info->lowest_clk_level;
  2744. CAM_DBG(CAM_UTIL,
  2745. "Dev %s, clk %s, id %d register wrapper entry for shared clk",
  2746. soc_info->dev_name, soc_info->clk_name[i],
  2747. soc_info->clk_id[i]);
  2748. rc = cam_soc_util_clk_wrapper_register_entry(
  2749. soc_info->clk_id[i], soc_info->clk[i],
  2750. (i == soc_info->src_clk_idx) ? true : false,
  2751. soc_info, soc_info->clk_rate[min_level][i],
  2752. soc_info->clk_name[i]);
  2753. if (rc) {
  2754. CAM_ERR(CAM_UTIL,
  2755. "Failed in registering shared clk Dev %s id %d",
  2756. soc_info->dev_name,
  2757. soc_info->clk_id[i]);
  2758. cam_wrapper_clk_put(soc_info->clk[i]);
  2759. soc_info->clk[i] = NULL;
  2760. goto put_clk;
  2761. }
  2762. } else if (i == soc_info->src_clk_idx) {
  2763. rc = cam_soc_util_register_mmrm_client(
  2764. soc_info->clk_id[i], soc_info->clk[i],
  2765. soc_info->is_nrt_dev,
  2766. soc_info, soc_info->clk_name[i],
  2767. &soc_info->mmrm_handle);
  2768. if (rc) {
  2769. CAM_ERR(CAM_UTIL,
  2770. "Failed in register mmrm client Dev %s clk id %d",
  2771. soc_info->dev_name,
  2772. soc_info->clk_id[i]);
  2773. cam_wrapper_clk_put(soc_info->clk[i]);
  2774. soc_info->clk[i] = NULL;
  2775. goto put_clk;
  2776. }
  2777. }
  2778. }
  2779. rc = cam_soc_util_request_pinctrl(soc_info);
  2780. if (rc) {
  2781. CAM_ERR(CAM_UTIL, "Failed in requesting Pinctrl, rc: %d", rc);
  2782. goto put_clk;
  2783. }
  2784. rc = cam_soc_util_request_gpio_table(soc_info, true);
  2785. if (rc) {
  2786. CAM_ERR(CAM_UTIL, "Failed in request gpio table, rc=%d", rc);
  2787. goto put_clk;
  2788. }
  2789. if (soc_info->clk_control_enable)
  2790. cam_soc_util_create_clk_lvl_debugfs(soc_info);
  2791. return rc;
  2792. put_clk:
  2793. if (soc_info->mmrm_handle) {
  2794. cam_soc_util_unregister_mmrm_client(soc_info->mmrm_handle);
  2795. soc_info->mmrm_handle = NULL;
  2796. }
  2797. for (i = i - 1; i >= 0; i--) {
  2798. if (soc_info->clk[i]) {
  2799. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i))
  2800. cam_soc_util_clk_wrapper_unregister_entry(
  2801. soc_info->clk_id[i], soc_info);
  2802. cam_wrapper_clk_put(soc_info->clk[i]);
  2803. soc_info->clk[i] = NULL;
  2804. }
  2805. }
  2806. put_irq:
  2807. if (i == -1)
  2808. i = soc_info->irq_count;
  2809. for (i = i - 1; i >= 0; i--) {
  2810. if (soc_info->irq_num[i] > 0)
  2811. disable_irq(soc_info->irq_num[i]);
  2812. }
  2813. put_regulator:
  2814. if (i == -1)
  2815. i = soc_info->num_rgltr;
  2816. for (i = i - 1; i >= 0; i--) {
  2817. if (soc_info->rgltr[i]) {
  2818. cam_wrapper_regulator_disable(soc_info->rgltr[i]);
  2819. cam_wrapper_regulator_put(soc_info->rgltr[i]);
  2820. soc_info->rgltr[i] = NULL;
  2821. }
  2822. }
  2823. unmap_base:
  2824. if (i == -1)
  2825. i = soc_info->num_reg_map;
  2826. for (i = i - 1; i >= 0; i--) {
  2827. if (soc_info->reserve_mem)
  2828. release_mem_region(soc_info->mem_block[i]->start,
  2829. resource_size(soc_info->mem_block[i]));
  2830. iounmap(soc_info->reg_map[i].mem_base);
  2831. soc_info->reg_map[i].mem_base = NULL;
  2832. soc_info->reg_map[i].size = 0;
  2833. }
  2834. return rc;
  2835. }
  2836. int cam_soc_util_release_platform_resource(struct cam_hw_soc_info *soc_info)
  2837. {
  2838. int i;
  2839. bool b_ret = false;
  2840. if (!soc_info || !soc_info->dev) {
  2841. CAM_ERR(CAM_UTIL, "Invalid parameter");
  2842. return -EINVAL;
  2843. }
  2844. if (soc_info->mmrm_handle) {
  2845. cam_soc_util_unregister_mmrm_client(soc_info->mmrm_handle);
  2846. soc_info->mmrm_handle = NULL;
  2847. }
  2848. for (i = soc_info->num_clk - 1; i >= 0; i--) {
  2849. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i))
  2850. cam_soc_util_clk_wrapper_unregister_entry(
  2851. soc_info->clk_id[i], soc_info);
  2852. if (!soc_info->clk[i]) {
  2853. CAM_DBG(CAM_UTIL, "%s handle is NULL skip put",
  2854. soc_info->clk_name[i]);
  2855. continue;
  2856. }
  2857. cam_wrapper_clk_put(soc_info->clk[i]);
  2858. soc_info->clk[i] = NULL;
  2859. }
  2860. for (i = soc_info->num_rgltr - 1; i >= 0; i--) {
  2861. if (soc_info->rgltr[i]) {
  2862. cam_wrapper_regulator_put(soc_info->rgltr[i]);
  2863. soc_info->rgltr[i] = NULL;
  2864. }
  2865. }
  2866. for (i = soc_info->num_reg_map - 1; i >= 0; i--) {
  2867. iounmap(soc_info->reg_map[i].mem_base);
  2868. soc_info->reg_map[i].mem_base = NULL;
  2869. soc_info->reg_map[i].size = 0;
  2870. }
  2871. for (i = soc_info->irq_count; i >= 0; i--) {
  2872. if (soc_info->irq_num[i] > 0) {
  2873. if (cam_presil_mode_enabled()) {
  2874. if (cam_soc_util_is_presil_address_space(
  2875. soc_info->mem_block[0]->start)) {
  2876. b_ret = cam_presil_unsubscribe_device_irq(
  2877. soc_info->irq_line[i]->start);
  2878. CAM_DBG(CAM_PRESIL,
  2879. "UnSubscribe IRQ: Ret=%d NUM=%d Name=%s",
  2880. b_ret, soc_info->irq_line[i]->start,
  2881. soc_info->irq_name[i]);
  2882. }
  2883. }
  2884. disable_irq(soc_info->irq_num[i]);
  2885. }
  2886. }
  2887. cam_soc_util_release_pinctrl(soc_info);
  2888. /* release for gpio */
  2889. cam_soc_util_request_gpio_table(soc_info, false);
  2890. soc_info->dentry = NULL;
  2891. return 0;
  2892. }
  2893. int cam_soc_util_enable_platform_resource(struct cam_hw_soc_info *soc_info,
  2894. int cesta_client_idx, bool enable_clocks, enum cam_vote_level clk_level,
  2895. bool irq_enable)
  2896. {
  2897. int rc = 0, i;
  2898. if (!soc_info)
  2899. return -EINVAL;
  2900. rc = cam_soc_util_regulator_enable_default(soc_info);
  2901. if (rc) {
  2902. CAM_ERR(CAM_UTIL, "Regulators enable failed");
  2903. return rc;
  2904. }
  2905. if (enable_clocks) {
  2906. rc = cam_soc_util_clk_enable_default(soc_info, cesta_client_idx, clk_level);
  2907. if (rc)
  2908. goto disable_regulator;
  2909. }
  2910. if (irq_enable) {
  2911. for (i = 0; i < soc_info->irq_count; i++) {
  2912. if (soc_info->irq_num[i] < 0) {
  2913. CAM_ERR(CAM_UTIL, "No IRQ line available for irq: %s dev: %s",
  2914. soc_info->irq_name[i], soc_info->dev_name);
  2915. rc = -ENODEV;
  2916. goto disable_irq;
  2917. }
  2918. enable_irq(soc_info->irq_num[i]);
  2919. }
  2920. }
  2921. return rc;
  2922. disable_irq:
  2923. if (irq_enable) {
  2924. for (i = i - 1; i >= 0; i--)
  2925. disable_irq(soc_info->irq_num[i]);
  2926. }
  2927. if (enable_clocks)
  2928. cam_soc_util_clk_disable_default(soc_info, cesta_client_idx);
  2929. disable_regulator:
  2930. cam_soc_util_regulator_disable_default(soc_info);
  2931. return rc;
  2932. }
  2933. int cam_soc_util_disable_platform_resource(struct cam_hw_soc_info *soc_info,
  2934. int cesta_client_idx, bool disable_clocks, bool disable_irq)
  2935. {
  2936. int rc = 0;
  2937. if (!soc_info)
  2938. return -EINVAL;
  2939. if (disable_irq)
  2940. rc |= cam_soc_util_irq_disable(soc_info);
  2941. if (disable_clocks)
  2942. cam_soc_util_clk_disable_default(soc_info, cesta_client_idx);
  2943. cam_soc_util_regulator_disable_default(soc_info);
  2944. return rc;
  2945. }
  2946. int cam_soc_util_reg_dump(struct cam_hw_soc_info *soc_info,
  2947. uint32_t base_index, uint32_t offset, int size)
  2948. {
  2949. void __iomem *base_addr = NULL;
  2950. CAM_DBG(CAM_UTIL, "base_idx %u size=%d", base_index, size);
  2951. if (!soc_info || base_index >= soc_info->num_reg_map ||
  2952. size <= 0 || (offset + size) >=
  2953. CAM_SOC_GET_REG_MAP_SIZE(soc_info, base_index))
  2954. return -EINVAL;
  2955. base_addr = CAM_SOC_GET_REG_MAP_START(soc_info, base_index);
  2956. /*
  2957. * All error checking already done above,
  2958. * hence ignoring the return value below.
  2959. */
  2960. cam_io_dump(base_addr, offset, size);
  2961. return 0;
  2962. }
  2963. static int cam_soc_util_dump_cont_reg_range(
  2964. struct cam_hw_soc_info *soc_info,
  2965. struct cam_reg_range_read_desc *reg_read, uint32_t base_idx,
  2966. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  2967. {
  2968. int i = 0, rc = 0, val = 0;
  2969. uint32_t write_idx = 0;
  2970. if (!soc_info || !dump_out_buf || !reg_read || !cmd_buf_end) {
  2971. CAM_ERR(CAM_UTIL,
  2972. "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK cmd_buf_end: %pK",
  2973. soc_info, dump_out_buf, reg_read, cmd_buf_end);
  2974. rc = -EINVAL;
  2975. goto end;
  2976. }
  2977. if ((reg_read->num_values) && ((reg_read->num_values > U32_MAX / 2) ||
  2978. (sizeof(uint32_t) > ((U32_MAX -
  2979. sizeof(struct cam_reg_dump_out_buffer) -
  2980. dump_out_buf->bytes_written) /
  2981. (reg_read->num_values * 2))))) {
  2982. CAM_ERR(CAM_UTIL,
  2983. "Integer Overflow bytes_written: [%u] num_values: [%u]",
  2984. dump_out_buf->bytes_written, reg_read->num_values);
  2985. rc = -EOVERFLOW;
  2986. goto end;
  2987. }
  2988. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  2989. (uintptr_t)(sizeof(struct cam_reg_dump_out_buffer)
  2990. - sizeof(uint32_t) + dump_out_buf->bytes_written +
  2991. (reg_read->num_values * 2 * sizeof(uint32_t)))) {
  2992. CAM_ERR(CAM_UTIL,
  2993. "Insufficient space in out buffer num_values: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  2994. reg_read->num_values, cmd_buf_end,
  2995. (uintptr_t)dump_out_buf);
  2996. rc = -EINVAL;
  2997. goto end;
  2998. }
  2999. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  3000. for (i = 0; i < reg_read->num_values; i++) {
  3001. val = cam_soc_util_r(soc_info, base_idx,
  3002. (reg_read->offset + (i * sizeof(uint32_t))));
  3003. if (!val)
  3004. CAM_WARN(CAM_UTIL, "Possibly fails to read");
  3005. dump_out_buf->dump_data[write_idx++] = reg_read->offset +
  3006. (i * sizeof(uint32_t));
  3007. dump_out_buf->dump_data[write_idx++] = val;
  3008. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  3009. }
  3010. end:
  3011. return rc;
  3012. }
  3013. static int cam_soc_util_dump_dmi_reg_range(
  3014. struct cam_hw_soc_info *soc_info,
  3015. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  3016. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  3017. {
  3018. int i = 0, rc = 0, val = 0;
  3019. uint32_t write_idx = 0;
  3020. if (!soc_info || !dump_out_buf || !dmi_read || !cmd_buf_end) {
  3021. CAM_ERR(CAM_UTIL,
  3022. "Invalid input args soc_info: %pK, dump_out_buffer: %pK",
  3023. soc_info, dump_out_buf);
  3024. rc = -EINVAL;
  3025. goto end;
  3026. }
  3027. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  3028. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  3029. CAM_ERR(CAM_UTIL,
  3030. "Invalid number of requested writes, pre: %d post: %d",
  3031. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  3032. rc = -EINVAL;
  3033. goto end;
  3034. }
  3035. if ((dmi_read->num_pre_writes + dmi_read->dmi_data_read.num_values)
  3036. && ((dmi_read->num_pre_writes > U32_MAX / 2) ||
  3037. (dmi_read->dmi_data_read.num_values > U32_MAX / 2) ||
  3038. ((dmi_read->num_pre_writes * 2) > U32_MAX -
  3039. (dmi_read->dmi_data_read.num_values * 2)) ||
  3040. (sizeof(uint32_t) > ((U32_MAX -
  3041. sizeof(struct cam_reg_dump_out_buffer) -
  3042. dump_out_buf->bytes_written) / ((dmi_read->num_pre_writes +
  3043. dmi_read->dmi_data_read.num_values) * 2))))) {
  3044. CAM_ERR(CAM_UTIL,
  3045. "Integer Overflow bytes_written: [%u] num_pre_writes: [%u] num_values: [%u]",
  3046. dump_out_buf->bytes_written, dmi_read->num_pre_writes,
  3047. dmi_read->dmi_data_read.num_values);
  3048. rc = -EOVERFLOW;
  3049. goto end;
  3050. }
  3051. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  3052. (uintptr_t)(
  3053. sizeof(struct cam_reg_dump_out_buffer) - sizeof(uint32_t) +
  3054. (dump_out_buf->bytes_written +
  3055. (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
  3056. (dmi_read->dmi_data_read.num_values * 2 *
  3057. sizeof(uint32_t))))) {
  3058. CAM_ERR(CAM_UTIL,
  3059. "Insufficient space in out buffer num_read_val: [%d] num_write_val: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  3060. dmi_read->dmi_data_read.num_values,
  3061. dmi_read->num_pre_writes, cmd_buf_end,
  3062. (uintptr_t)dump_out_buf);
  3063. rc = -EINVAL;
  3064. goto end;
  3065. }
  3066. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  3067. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  3068. rc = cam_soc_util_w_mb(soc_info, base_idx,
  3069. dmi_read->pre_read_config[i].offset,
  3070. dmi_read->pre_read_config[i].value);
  3071. if (rc) {
  3072. CAM_ERR(CAM_UTIL, "Fails to write for pre_read_config");
  3073. goto end;
  3074. }
  3075. dump_out_buf->dump_data[write_idx++] =
  3076. dmi_read->pre_read_config[i].offset;
  3077. dump_out_buf->dump_data[write_idx++] =
  3078. dmi_read->pre_read_config[i].value;
  3079. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  3080. }
  3081. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  3082. val = cam_soc_util_r_mb(soc_info, base_idx,
  3083. dmi_read->dmi_data_read.offset);
  3084. if (!val)
  3085. CAM_WARN(CAM_UTIL, "Possibly fails to read for dmi_data_read");
  3086. dump_out_buf->dump_data[write_idx++] =
  3087. dmi_read->dmi_data_read.offset;
  3088. dump_out_buf->dump_data[write_idx++] = val;
  3089. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  3090. }
  3091. for (i = 0; i < dmi_read->num_post_writes; i++) {
  3092. rc = cam_soc_util_w_mb(soc_info, base_idx,
  3093. dmi_read->post_read_config[i].offset,
  3094. dmi_read->post_read_config[i].value);
  3095. if (rc) {
  3096. CAM_ERR(CAM_UTIL, "Fails to write for post_read_config");
  3097. goto end;
  3098. }
  3099. }
  3100. end:
  3101. return rc;
  3102. }
  3103. static int cam_soc_util_dump_dmi_reg_range_user_buf(
  3104. struct cam_hw_soc_info *soc_info,
  3105. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  3106. struct cam_hw_soc_dump_args *dump_args)
  3107. {
  3108. int i;
  3109. int rc;
  3110. int val = 0;
  3111. size_t buf_len = 0;
  3112. uint8_t *dst;
  3113. size_t remain_len;
  3114. uint32_t min_len;
  3115. uint32_t *waddr, *start;
  3116. uintptr_t cpu_addr;
  3117. struct cam_hw_soc_dump_header *hdr;
  3118. if (!soc_info || !dump_args || !dmi_read) {
  3119. CAM_ERR(CAM_UTIL,
  3120. "Invalid input args soc_info: %pK, dump_args: %pK",
  3121. soc_info, dump_args);
  3122. return -EINVAL;
  3123. }
  3124. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  3125. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  3126. CAM_ERR(CAM_UTIL,
  3127. "Invalid number of requested writes, pre: %d post: %d",
  3128. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  3129. return -EINVAL;
  3130. }
  3131. rc = cam_mem_get_cpu_buf(dump_args->buf_handle, &cpu_addr, &buf_len);
  3132. if (rc) {
  3133. CAM_ERR(CAM_UTIL, "Invalid handle %u rc %d",
  3134. dump_args->buf_handle, rc);
  3135. return rc;
  3136. }
  3137. if (buf_len <= dump_args->offset) {
  3138. CAM_WARN(CAM_UTIL, "Dump offset overshoot offset %zu len %zu",
  3139. dump_args->offset, buf_len);
  3140. rc = -ENOSPC;
  3141. goto end;
  3142. }
  3143. remain_len = buf_len - dump_args->offset;
  3144. min_len = (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
  3145. (dmi_read->dmi_data_read.num_values * 2 * sizeof(uint32_t)) +
  3146. sizeof(uint32_t);
  3147. if (remain_len < min_len) {
  3148. CAM_WARN(CAM_UTIL,
  3149. "Dump Buffer exhaust read %d write %d remain %zu min %u",
  3150. dmi_read->dmi_data_read.num_values,
  3151. dmi_read->num_pre_writes, remain_len,
  3152. min_len);
  3153. rc = -ENOSPC;
  3154. goto end;
  3155. }
  3156. dst = (uint8_t *)cpu_addr + dump_args->offset;
  3157. hdr = (struct cam_hw_soc_dump_header *)dst;
  3158. memset(hdr, 0, sizeof(struct cam_hw_soc_dump_header));
  3159. scnprintf(hdr->tag, CAM_SOC_HW_DUMP_TAG_MAX_LEN,
  3160. "DMI_DUMP:");
  3161. waddr = (uint32_t *)(dst + sizeof(struct cam_hw_soc_dump_header));
  3162. start = waddr;
  3163. hdr->word_size = sizeof(uint32_t);
  3164. *waddr = soc_info->index;
  3165. waddr++;
  3166. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  3167. rc = cam_soc_util_w_mb(soc_info, base_idx,
  3168. dmi_read->pre_read_config[i].offset,
  3169. dmi_read->pre_read_config[i].value);
  3170. if (rc) {
  3171. CAM_ERR(CAM_UTIL, "Fails to write for pre_read_config");
  3172. goto end;
  3173. }
  3174. *waddr++ = dmi_read->pre_read_config[i].offset;
  3175. *waddr++ = dmi_read->pre_read_config[i].value;
  3176. }
  3177. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  3178. val = cam_soc_util_r_mb(soc_info, base_idx,
  3179. dmi_read->dmi_data_read.offset);
  3180. if (!val)
  3181. CAM_WARN(CAM_UTIL, "Possibly fails to read for dmi_data_read");
  3182. *waddr++ = dmi_read->dmi_data_read.offset;
  3183. *waddr++ = val;
  3184. }
  3185. for (i = 0; i < dmi_read->num_post_writes; i++) {
  3186. rc = cam_soc_util_w_mb(soc_info, base_idx,
  3187. dmi_read->post_read_config[i].offset,
  3188. dmi_read->post_read_config[i].value);
  3189. if (rc) {
  3190. CAM_ERR(CAM_UTIL, "Fails to write for post_read_config");
  3191. goto end;
  3192. }
  3193. }
  3194. hdr->size = (waddr - start) * hdr->word_size;
  3195. dump_args->offset += hdr->size +
  3196. sizeof(struct cam_hw_soc_dump_header);
  3197. end:
  3198. cam_mem_put_cpu_buf(dump_args->buf_handle);
  3199. return rc;
  3200. }
  3201. static int cam_soc_util_dump_cont_reg_range_user_buf(
  3202. struct cam_hw_soc_info *soc_info,
  3203. struct cam_reg_range_read_desc *reg_read,
  3204. uint32_t base_idx,
  3205. struct cam_hw_soc_dump_args *dump_args)
  3206. {
  3207. int i;
  3208. int rc = 0, val = 0;
  3209. size_t buf_len;
  3210. uint8_t *dst;
  3211. size_t remain_len;
  3212. uint32_t min_len;
  3213. uint32_t *waddr, *start;
  3214. uintptr_t cpu_addr;
  3215. struct cam_hw_soc_dump_header *hdr;
  3216. if (!soc_info || !dump_args || !reg_read) {
  3217. CAM_ERR(CAM_UTIL,
  3218. "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK",
  3219. soc_info, dump_args, reg_read);
  3220. return -EINVAL;
  3221. }
  3222. rc = cam_mem_get_cpu_buf(dump_args->buf_handle, &cpu_addr, &buf_len);
  3223. if (rc) {
  3224. CAM_ERR(CAM_UTIL, "Invalid handle %u rc %d",
  3225. dump_args->buf_handle, rc);
  3226. return rc;
  3227. }
  3228. if (buf_len <= dump_args->offset) {
  3229. CAM_WARN(CAM_UTIL, "Dump offset overshoot %zu %zu",
  3230. dump_args->offset, buf_len);
  3231. rc = -ENOSPC;
  3232. goto end;
  3233. }
  3234. remain_len = buf_len - dump_args->offset;
  3235. min_len = (reg_read->num_values * 2 * sizeof(uint32_t)) +
  3236. sizeof(struct cam_hw_soc_dump_header) + sizeof(uint32_t);
  3237. if (remain_len < min_len) {
  3238. CAM_WARN(CAM_UTIL,
  3239. "Dump Buffer exhaust read_values %d remain %zu min %u",
  3240. reg_read->num_values,
  3241. remain_len,
  3242. min_len);
  3243. rc = -ENOSPC;
  3244. goto end;
  3245. }
  3246. dst = (uint8_t *)cpu_addr + dump_args->offset;
  3247. hdr = (struct cam_hw_soc_dump_header *)dst;
  3248. memset(hdr, 0, sizeof(struct cam_hw_soc_dump_header));
  3249. scnprintf(hdr->tag, CAM_SOC_HW_DUMP_TAG_MAX_LEN, "%s_REG:",
  3250. soc_info->dev_name);
  3251. waddr = (uint32_t *)(dst + sizeof(struct cam_hw_soc_dump_header));
  3252. start = waddr;
  3253. hdr->word_size = sizeof(uint32_t);
  3254. *waddr = soc_info->index;
  3255. waddr++;
  3256. for (i = 0; i < reg_read->num_values; i++) {
  3257. val = cam_soc_util_r(soc_info, base_idx,
  3258. (reg_read->offset + (i * sizeof(uint32_t))));
  3259. if (!val)
  3260. CAM_WARN(CAM_UTIL, "Possibly fails to read");
  3261. waddr[0] = reg_read->offset + (i * sizeof(uint32_t));
  3262. waddr[1] = val;
  3263. waddr += 2;
  3264. }
  3265. hdr->size = (waddr - start) * hdr->word_size;
  3266. dump_args->offset += hdr->size +
  3267. sizeof(struct cam_hw_soc_dump_header);
  3268. end:
  3269. cam_mem_put_cpu_buf(dump_args->buf_handle);
  3270. return rc;
  3271. }
  3272. static int cam_soc_util_user_reg_dump(
  3273. struct cam_reg_dump_desc *reg_dump_desc,
  3274. struct cam_hw_soc_dump_args *dump_args,
  3275. struct cam_hw_soc_info *soc_info,
  3276. uint32_t reg_base_idx)
  3277. {
  3278. int rc = 0;
  3279. int i;
  3280. struct cam_reg_read_info *reg_read_info = NULL;
  3281. if (!dump_args || !reg_dump_desc || !soc_info) {
  3282. CAM_ERR(CAM_UTIL,
  3283. "Invalid input parameters %pK %pK %pK",
  3284. dump_args, reg_dump_desc, soc_info);
  3285. return -EINVAL;
  3286. }
  3287. for (i = 0; i < reg_dump_desc->num_read_range; i++) {
  3288. reg_read_info = &reg_dump_desc->read_range[i];
  3289. if (reg_read_info->type ==
  3290. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  3291. rc = cam_soc_util_dump_cont_reg_range_user_buf(
  3292. soc_info,
  3293. &reg_read_info->reg_read,
  3294. reg_base_idx,
  3295. dump_args);
  3296. } else if (reg_read_info->type ==
  3297. CAM_REG_DUMP_READ_TYPE_DMI) {
  3298. rc = cam_soc_util_dump_dmi_reg_range_user_buf(
  3299. soc_info,
  3300. &reg_read_info->dmi_read,
  3301. reg_base_idx,
  3302. dump_args);
  3303. } else {
  3304. CAM_ERR(CAM_UTIL,
  3305. "Invalid Reg dump read type: %d",
  3306. reg_read_info->type);
  3307. rc = -EINVAL;
  3308. goto end;
  3309. }
  3310. if (rc) {
  3311. CAM_ERR(CAM_UTIL,
  3312. "Reg range read failed rc: %d reg_base_idx: %d",
  3313. rc, reg_base_idx);
  3314. goto end;
  3315. }
  3316. }
  3317. end:
  3318. return rc;
  3319. }
  3320. int cam_soc_util_reg_dump_to_cmd_buf(void *ctx,
  3321. struct cam_cmd_buf_desc *cmd_desc, uint64_t req_id,
  3322. cam_soc_util_regspace_data_cb reg_data_cb,
  3323. struct cam_hw_soc_dump_args *soc_dump_args,
  3324. bool user_triggered_dump)
  3325. {
  3326. int rc = 0, i, j;
  3327. uintptr_t cpu_addr = 0;
  3328. uintptr_t cmd_buf_start = 0;
  3329. uintptr_t cmd_in_data_end = 0;
  3330. uintptr_t cmd_buf_end = 0;
  3331. uint32_t reg_base_type = 0;
  3332. size_t buf_size = 0, remain_len = 0;
  3333. struct cam_reg_dump_input_info *reg_input_info = NULL;
  3334. struct cam_reg_dump_desc *reg_dump_desc = NULL;
  3335. struct cam_reg_dump_out_buffer *dump_out_buf = NULL;
  3336. struct cam_reg_read_info *reg_read_info = NULL;
  3337. struct cam_hw_soc_info *soc_info;
  3338. uint32_t reg_base_idx = 0;
  3339. if (!ctx || !cmd_desc || !reg_data_cb) {
  3340. CAM_ERR(CAM_UTIL, "Invalid args to reg dump [%pK] [%pK]",
  3341. cmd_desc, reg_data_cb);
  3342. return -EINVAL;
  3343. }
  3344. if (!cmd_desc->length || !cmd_desc->size) {
  3345. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  3346. cmd_desc->length, cmd_desc->size);
  3347. return -EINVAL;
  3348. }
  3349. rc = cam_mem_get_cpu_buf(cmd_desc->mem_handle, &cpu_addr, &buf_size);
  3350. if (rc || !cpu_addr || (buf_size == 0)) {
  3351. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  3352. rc, (void *)cpu_addr);
  3353. if (rc)
  3354. return rc;
  3355. goto end;
  3356. }
  3357. CAM_DBG(CAM_UTIL, "Get cpu buf success req_id: %llu buf_size: %zu",
  3358. req_id, buf_size);
  3359. if ((buf_size < sizeof(uint32_t)) ||
  3360. ((size_t)cmd_desc->offset > (buf_size - sizeof(uint32_t)))) {
  3361. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  3362. (size_t)cmd_desc->offset);
  3363. rc = -EINVAL;
  3364. goto end;
  3365. }
  3366. remain_len = buf_size - (size_t)cmd_desc->offset;
  3367. if ((remain_len < (size_t)cmd_desc->size) || (cmd_desc->size <
  3368. cmd_desc->length)) {
  3369. CAM_ERR(CAM_UTIL,
  3370. "Invalid params for cmd buf len: %zu size: %zu remain_len: %zu",
  3371. (size_t)cmd_desc->length, (size_t)cmd_desc->length,
  3372. remain_len);
  3373. rc = -EINVAL;
  3374. goto end;
  3375. }
  3376. cmd_buf_start = cpu_addr + (uintptr_t)cmd_desc->offset;
  3377. cmd_in_data_end = cmd_buf_start + (uintptr_t)cmd_desc->length;
  3378. cmd_buf_end = cmd_buf_start + (uintptr_t)cmd_desc->size;
  3379. if ((cmd_buf_end <= cmd_buf_start) ||
  3380. (cmd_in_data_end <= cmd_buf_start)) {
  3381. CAM_ERR(CAM_UTIL,
  3382. "Invalid length or size for cmd buf: [%zu] [%zu]",
  3383. (size_t)cmd_desc->length, (size_t)cmd_desc->size);
  3384. rc = -EINVAL;
  3385. goto end;
  3386. }
  3387. CAM_DBG(CAM_UTIL,
  3388. "Buffer params start [%pK] input_end [%pK] buf_end [%pK]",
  3389. cmd_buf_start, cmd_in_data_end, cmd_buf_end);
  3390. reg_input_info = (struct cam_reg_dump_input_info *) cmd_buf_start;
  3391. if ((reg_input_info->num_dump_sets > 1) && (sizeof(uint32_t) >
  3392. ((U32_MAX - sizeof(struct cam_reg_dump_input_info)) /
  3393. (reg_input_info->num_dump_sets - 1)))) {
  3394. CAM_ERR(CAM_UTIL,
  3395. "Integer Overflow req_id: [%llu] num_dump_sets: [%u]",
  3396. req_id, reg_input_info->num_dump_sets);
  3397. rc = -EOVERFLOW;
  3398. goto end;
  3399. }
  3400. if ((!reg_input_info->num_dump_sets) ||
  3401. ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  3402. (sizeof(struct cam_reg_dump_input_info) +
  3403. ((reg_input_info->num_dump_sets - 1) * sizeof(uint32_t))))) {
  3404. CAM_ERR(CAM_UTIL,
  3405. "Invalid number of dump sets, req_id: [%llu] num_dump_sets: [%u]",
  3406. req_id, reg_input_info->num_dump_sets);
  3407. rc = -EINVAL;
  3408. goto end;
  3409. }
  3410. CAM_DBG(CAM_UTIL,
  3411. "reg_input_info req_id: %llu ctx %pK num_dump_sets: %d",
  3412. req_id, ctx, reg_input_info->num_dump_sets);
  3413. for (i = 0; i < reg_input_info->num_dump_sets; i++) {
  3414. if ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  3415. reg_input_info->dump_set_offsets[i]) {
  3416. CAM_ERR(CAM_UTIL,
  3417. "Invalid dump set offset: [%pK], cmd_buf_start: [%pK] cmd_in_data_end: [%pK]",
  3418. (uintptr_t)reg_input_info->dump_set_offsets[i],
  3419. cmd_buf_start, cmd_in_data_end);
  3420. rc = -EINVAL;
  3421. goto end;
  3422. }
  3423. reg_dump_desc = (struct cam_reg_dump_desc *)
  3424. (cmd_buf_start +
  3425. (uintptr_t)reg_input_info->dump_set_offsets[i]);
  3426. if ((reg_dump_desc->num_read_range > 1) &&
  3427. (sizeof(struct cam_reg_read_info) > ((U32_MAX -
  3428. sizeof(struct cam_reg_dump_desc)) /
  3429. (reg_dump_desc->num_read_range - 1)))) {
  3430. CAM_ERR(CAM_UTIL,
  3431. "Integer Overflow req_id: [%llu] num_read_range: [%u]",
  3432. req_id, reg_dump_desc->num_read_range);
  3433. rc = -EOVERFLOW;
  3434. goto end;
  3435. }
  3436. if ((!reg_dump_desc->num_read_range) ||
  3437. ((cmd_in_data_end - (uintptr_t)reg_dump_desc) <=
  3438. (uintptr_t)(sizeof(struct cam_reg_dump_desc) +
  3439. ((reg_dump_desc->num_read_range - 1) *
  3440. sizeof(struct cam_reg_read_info))))) {
  3441. CAM_ERR(CAM_UTIL,
  3442. "Invalid number of read ranges, req_id: [%llu] num_read_range: [%d]",
  3443. req_id, reg_dump_desc->num_read_range);
  3444. rc = -EINVAL;
  3445. goto end;
  3446. }
  3447. if ((cmd_buf_end - cmd_buf_start) <= (uintptr_t)
  3448. (reg_dump_desc->dump_buffer_offset +
  3449. sizeof(struct cam_reg_dump_out_buffer))) {
  3450. CAM_ERR(CAM_UTIL,
  3451. "Invalid out buffer offset: [%pK], cmd_buf_start: [%pK] cmd_buf_end: [%pK]",
  3452. (uintptr_t)reg_dump_desc->dump_buffer_offset,
  3453. cmd_buf_start, cmd_buf_end);
  3454. rc = -EINVAL;
  3455. goto end;
  3456. }
  3457. reg_base_type = reg_dump_desc->reg_base_type;
  3458. if (reg_base_type == 0 || reg_base_type >
  3459. CAM_REG_DUMP_BASE_TYPE_SFE_RIGHT) {
  3460. CAM_ERR(CAM_UTIL,
  3461. "Invalid Reg dump base type: %d",
  3462. reg_base_type);
  3463. rc = -EINVAL;
  3464. goto end;
  3465. }
  3466. rc = reg_data_cb(reg_base_type, ctx, &soc_info, &reg_base_idx);
  3467. if (rc || !soc_info) {
  3468. CAM_ERR(CAM_UTIL,
  3469. "Reg space data callback failed rc: %d soc_info: [%pK]",
  3470. rc, soc_info);
  3471. rc = -EINVAL;
  3472. goto end;
  3473. }
  3474. if (reg_base_idx > soc_info->num_reg_map) {
  3475. CAM_ERR(CAM_UTIL,
  3476. "Invalid reg base idx: %d num reg map: %d",
  3477. reg_base_idx, soc_info->num_reg_map);
  3478. rc = -EINVAL;
  3479. goto end;
  3480. }
  3481. CAM_DBG(CAM_UTIL,
  3482. "Reg data callback success req_id: %llu base_type: %d base_idx: %d num_read_range: %d",
  3483. req_id, reg_base_type, reg_base_idx,
  3484. reg_dump_desc->num_read_range);
  3485. /* If the dump request is triggered by user space
  3486. * buffer will be different from the buffer which is received
  3487. * in init packet. In this case, dump the data to the
  3488. * user provided buffer and exit.
  3489. */
  3490. if (user_triggered_dump) {
  3491. rc = cam_soc_util_user_reg_dump(reg_dump_desc,
  3492. soc_dump_args, soc_info, reg_base_idx);
  3493. CAM_INFO(CAM_UTIL,
  3494. "%s reg_base_idx %d dumped offset %u",
  3495. soc_info->dev_name, reg_base_idx,
  3496. soc_dump_args->offset);
  3497. goto end;
  3498. }
  3499. /* Below code is executed when data is dumped to the
  3500. * out buffer received in init packet
  3501. */
  3502. dump_out_buf = (struct cam_reg_dump_out_buffer *)
  3503. (cmd_buf_start +
  3504. (uintptr_t)reg_dump_desc->dump_buffer_offset);
  3505. dump_out_buf->req_id = req_id;
  3506. dump_out_buf->bytes_written = 0;
  3507. for (j = 0; j < reg_dump_desc->num_read_range; j++) {
  3508. CAM_DBG(CAM_UTIL,
  3509. "Number of bytes written to cmd buffer: %u req_id: %llu",
  3510. dump_out_buf->bytes_written, req_id);
  3511. reg_read_info = &reg_dump_desc->read_range[j];
  3512. if (reg_read_info->type ==
  3513. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  3514. rc = cam_soc_util_dump_cont_reg_range(soc_info,
  3515. &reg_read_info->reg_read, reg_base_idx,
  3516. dump_out_buf, cmd_buf_end);
  3517. } else if (reg_read_info->type ==
  3518. CAM_REG_DUMP_READ_TYPE_DMI) {
  3519. rc = cam_soc_util_dump_dmi_reg_range(soc_info,
  3520. &reg_read_info->dmi_read, reg_base_idx,
  3521. dump_out_buf, cmd_buf_end);
  3522. } else {
  3523. CAM_ERR(CAM_UTIL,
  3524. "Invalid Reg dump read type: %d",
  3525. reg_read_info->type);
  3526. rc = -EINVAL;
  3527. goto end;
  3528. }
  3529. if (rc) {
  3530. CAM_ERR(CAM_UTIL,
  3531. "Reg range read failed rc: %d reg_base_idx: %d dump_out_buf: %pK",
  3532. rc, reg_base_idx, dump_out_buf);
  3533. goto end;
  3534. }
  3535. }
  3536. }
  3537. end:
  3538. cam_mem_put_cpu_buf(cmd_desc->mem_handle);
  3539. return rc;
  3540. }
  3541. /**
  3542. * cam_soc_util_print_clk_freq()
  3543. *
  3544. * @brief: This function gets the clk rates for each clk from clk
  3545. * driver and prints in log
  3546. *
  3547. * @soc_info: Device soc struct to be populated
  3548. *
  3549. * @return: success or failure
  3550. */
  3551. int cam_soc_util_print_clk_freq(struct cam_hw_soc_info *soc_info)
  3552. {
  3553. int i;
  3554. unsigned long clk_rate = 0;
  3555. if (!soc_info) {
  3556. CAM_ERR(CAM_UTIL, "Invalid soc info");
  3557. return -EINVAL;
  3558. }
  3559. if ((soc_info->num_clk == 0) ||
  3560. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  3561. CAM_ERR(CAM_UTIL, "[%s] Invalid number of clock %d",
  3562. soc_info->dev_name, soc_info->num_clk);
  3563. return -EINVAL;
  3564. }
  3565. for (i = 0; i < soc_info->num_clk; i++) {
  3566. clk_rate = cam_wrapper_clk_get_rate(soc_info->clk[i]);
  3567. CAM_INFO(CAM_UTIL,
  3568. "[%s] idx = %d clk name = %s clk_rate=%lld",
  3569. soc_info->dev_name, i, soc_info->clk_name[i],
  3570. clk_rate);
  3571. }
  3572. return 0;
  3573. }
  3574. inline unsigned long cam_soc_util_get_applied_src_clk(
  3575. struct cam_hw_soc_info *soc_info, bool is_max)
  3576. {
  3577. unsigned long clk_rate;
  3578. /*
  3579. * For CRMC type, exa - ife, csid, cphy
  3580. * final clk = max(hw_client_0, hw_client_1, hw_client_2, sw_client)
  3581. * For CRMB type, exa - camnoc axi
  3582. * final clk = max(hw_client_0 + hw_client_1 + hw_client_2, sw_client)
  3583. */
  3584. if (is_max) {
  3585. clk_rate = max(soc_info->applied_src_clk_rates.hw_client[0].high,
  3586. soc_info->applied_src_clk_rates.hw_client[1].high);
  3587. clk_rate = max(clk_rate, soc_info->applied_src_clk_rates.hw_client[2].high);
  3588. clk_rate = max(clk_rate, soc_info->applied_src_clk_rates.sw_client);
  3589. } else {
  3590. clk_rate = max((soc_info->applied_src_clk_rates.hw_client[0].high +
  3591. soc_info->applied_src_clk_rates.hw_client[1].high +
  3592. soc_info->applied_src_clk_rates.hw_client[2].high),
  3593. soc_info->applied_src_clk_rates.sw_client);
  3594. }
  3595. return clk_rate;
  3596. }
  3597. int cam_soc_util_regulators_enabled(struct cam_hw_soc_info *soc_info)
  3598. {
  3599. int j = 0, rc = 0;
  3600. int enabled_cnt = 0;
  3601. for (j = 0; j < soc_info->num_rgltr; j++) {
  3602. if (soc_info->rgltr[j]) {
  3603. rc = cam_wrapper_regulator_is_enabled(soc_info->rgltr[j]);
  3604. if (rc < 0) {
  3605. CAM_ERR(CAM_UTIL, "%s regulator_is_enabled failed",
  3606. soc_info->rgltr_name[j]);
  3607. } else if (rc > 0) {
  3608. CAM_DBG(CAM_UTIL, "%s regulator enabled",
  3609. soc_info->rgltr_name[j]);
  3610. enabled_cnt++;
  3611. } else {
  3612. CAM_DBG(CAM_UTIL, "%s regulator is disabled",
  3613. soc_info->rgltr_name[j]);
  3614. }
  3615. }
  3616. }
  3617. return enabled_cnt;
  3618. }