cam_soc_util.c 56 KB

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