cam_soc_util.c 65 KB

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