cam_soc_util.c 88 KB

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