cam_soc_util.c 113 KB

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