cam_soc_util.c 95 KB

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