cam_soc_util.c 95 KB

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