cam_soc_util.c 116 KB

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