cam_soc_util.c 94 KB

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