cam_soc_util.c 95 KB

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