cam_soc_util.c 88 KB

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