cam_soc_util.c 66 KB

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