cam_soc_util.c 79 KB

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