cam_soc_util.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, 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. int cam_soc_util_set_clk_flags(struct cam_hw_soc_info *soc_info,
  317. uint32_t clk_index, unsigned long flags)
  318. {
  319. if (!soc_info || (clk_index >= soc_info->num_clk)) {
  320. CAM_ERR(CAM_UTIL, "Invalid input params %pK, %d",
  321. soc_info, clk_index);
  322. return -EINVAL;
  323. }
  324. return clk_set_flags(soc_info->clk[clk_index], flags);
  325. }
  326. /**
  327. * cam_soc_util_set_clk_rate()
  328. *
  329. * @brief: Sets the given rate for the clk requested for
  330. *
  331. * @clk: Clock structure information for which rate is to be set
  332. * @clk_name: Name of the clock for which rate is being set
  333. * @clk_rate Clock rate to be set
  334. *
  335. * @return: Success or failure
  336. */
  337. static int cam_soc_util_set_clk_rate(struct clk *clk, const char *clk_name,
  338. int32_t clk_rate)
  339. {
  340. int rc = 0;
  341. long clk_rate_round;
  342. if (!clk || !clk_name)
  343. return -EINVAL;
  344. CAM_DBG(CAM_UTIL, "set %s, rate %d", clk_name, clk_rate);
  345. if (clk_rate > 0) {
  346. clk_rate_round = clk_round_rate(clk, clk_rate);
  347. CAM_DBG(CAM_UTIL, "new_rate %ld", clk_rate_round);
  348. if (clk_rate_round < 0) {
  349. CAM_ERR(CAM_UTIL, "round failed for clock %s rc = %ld",
  350. clk_name, clk_rate_round);
  351. return clk_rate_round;
  352. }
  353. rc = clk_set_rate(clk, clk_rate_round);
  354. if (rc) {
  355. CAM_ERR(CAM_UTIL, "set_rate failed on %s", clk_name);
  356. return rc;
  357. }
  358. } else if (clk_rate == INIT_RATE) {
  359. clk_rate_round = clk_get_rate(clk);
  360. CAM_DBG(CAM_UTIL, "init new_rate %ld", clk_rate_round);
  361. if (clk_rate_round == 0) {
  362. clk_rate_round = clk_round_rate(clk, 0);
  363. if (clk_rate_round <= 0) {
  364. CAM_ERR(CAM_UTIL, "round rate failed on %s",
  365. clk_name);
  366. return clk_rate_round;
  367. }
  368. }
  369. rc = clk_set_rate(clk, clk_rate_round);
  370. if (rc) {
  371. CAM_ERR(CAM_UTIL, "set_rate failed on %s", clk_name);
  372. return rc;
  373. }
  374. }
  375. return rc;
  376. }
  377. int cam_soc_util_set_src_clk_rate(struct cam_hw_soc_info *soc_info,
  378. int32_t clk_rate)
  379. {
  380. int rc = 0;
  381. int i = 0;
  382. int32_t src_clk_idx;
  383. int32_t scl_clk_idx;
  384. struct clk *clk = NULL;
  385. int32_t apply_level;
  386. uint32_t clk_level_override = 0;
  387. if (!soc_info || (soc_info->src_clk_idx < 0) ||
  388. (soc_info->src_clk_idx >= CAM_SOC_MAX_CLK)) {
  389. CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d",
  390. soc_info ? soc_info->src_clk_idx : -1);
  391. return -EINVAL;
  392. }
  393. src_clk_idx = soc_info->src_clk_idx;
  394. clk_level_override = soc_info->clk_level_override;
  395. if (clk_level_override && clk_rate)
  396. clk_rate =
  397. soc_info->clk_rate[clk_level_override][src_clk_idx];
  398. clk = soc_info->clk[src_clk_idx];
  399. rc = cam_soc_util_get_clk_level(soc_info, clk_rate, src_clk_idx,
  400. &apply_level);
  401. if (rc || (apply_level < 0) || (apply_level >= CAM_MAX_VOTE)) {
  402. CAM_ERR(CAM_UTIL,
  403. "set %s, rate %d dev_name = %s apply level = %d",
  404. soc_info->clk_name[src_clk_idx], clk_rate,
  405. soc_info->dev_name, apply_level);
  406. return -EINVAL;
  407. }
  408. CAM_DBG(CAM_UTIL, "set %s, rate %d dev_name = %s apply level = %d",
  409. soc_info->clk_name[src_clk_idx], clk_rate,
  410. soc_info->dev_name, apply_level);
  411. if ((soc_info->cam_cx_ipeak_enable) && (clk_rate >= 0)) {
  412. cam_cx_ipeak_update_vote_cx_ipeak(soc_info,
  413. apply_level);
  414. }
  415. rc = cam_soc_util_set_clk_rate(clk,
  416. soc_info->clk_name[src_clk_idx], clk_rate);
  417. if (rc) {
  418. CAM_ERR(CAM_UTIL,
  419. "SET_RATE Failed: src clk: %s, rate %d, dev_name = %s rc: %d",
  420. soc_info->clk_name[src_clk_idx], clk_rate,
  421. soc_info->dev_name, rc);
  422. return rc;
  423. }
  424. /* set clk rate for scalable clk if available */
  425. for (i = 0; i < soc_info->scl_clk_count; i++) {
  426. scl_clk_idx = soc_info->scl_clk_idx[i];
  427. if (scl_clk_idx < 0) {
  428. CAM_DBG(CAM_UTIL, "Scl clk index invalid");
  429. continue;
  430. }
  431. clk = soc_info->clk[scl_clk_idx];
  432. rc = cam_soc_util_set_clk_rate(clk,
  433. soc_info->clk_name[scl_clk_idx],
  434. soc_info->clk_rate[apply_level][scl_clk_idx]);
  435. if (rc) {
  436. CAM_WARN(CAM_UTIL,
  437. "SET_RATE Failed: scl clk: %s, rate %d dev_name = %s, rc: %d",
  438. soc_info->clk_name[scl_clk_idx],
  439. soc_info->clk_rate[apply_level][scl_clk_idx],
  440. soc_info->dev_name, rc);
  441. }
  442. }
  443. return 0;
  444. }
  445. int cam_soc_util_clk_put(struct clk **clk)
  446. {
  447. if (!(*clk)) {
  448. CAM_ERR(CAM_UTIL, "Invalid params clk");
  449. return -EINVAL;
  450. }
  451. clk_put(*clk);
  452. *clk = NULL;
  453. return 0;
  454. }
  455. static struct clk *cam_soc_util_option_clk_get(struct device_node *np,
  456. int index)
  457. {
  458. struct of_phandle_args clkspec;
  459. struct clk *clk;
  460. int rc;
  461. if (index < 0)
  462. return ERR_PTR(-EINVAL);
  463. rc = of_parse_phandle_with_args(np, "clocks-option", "#clock-cells",
  464. index, &clkspec);
  465. if (rc)
  466. return ERR_PTR(rc);
  467. clk = of_clk_get_from_provider(&clkspec);
  468. of_node_put(clkspec.np);
  469. return clk;
  470. }
  471. int cam_soc_util_get_option_clk_by_name(struct cam_hw_soc_info *soc_info,
  472. const char *clk_name, struct clk **clk, int32_t *clk_index,
  473. int32_t *clk_rate)
  474. {
  475. int index = 0;
  476. int rc = 0;
  477. struct device_node *of_node = NULL;
  478. if (!soc_info || !clk_name || !clk) {
  479. CAM_ERR(CAM_UTIL,
  480. "Invalid params soc_info %pK clk_name %s clk %pK",
  481. soc_info, clk_name, clk);
  482. return -EINVAL;
  483. }
  484. of_node = soc_info->dev->of_node;
  485. index = of_property_match_string(of_node, "clock-names-option",
  486. clk_name);
  487. if (index < 0) {
  488. CAM_INFO(CAM_UTIL, "No clk data for %s", clk_name);
  489. *clk_index = -1;
  490. *clk = ERR_PTR(-EINVAL);
  491. return -EINVAL;
  492. }
  493. *clk = cam_soc_util_option_clk_get(of_node, index);
  494. if (IS_ERR(*clk)) {
  495. CAM_ERR(CAM_UTIL, "No clk named %s found. Dev %s", clk_name,
  496. soc_info->dev_name);
  497. *clk_index = -1;
  498. return -EFAULT;
  499. }
  500. *clk_index = index;
  501. rc = of_property_read_u32_index(of_node, "clock-rates-option",
  502. index, clk_rate);
  503. if (rc) {
  504. CAM_ERR(CAM_UTIL,
  505. "Error reading clock-rates clk_name %s index %d",
  506. clk_name, index);
  507. cam_soc_util_clk_put(clk);
  508. *clk_rate = 0;
  509. return rc;
  510. }
  511. /*
  512. * Option clocks are assumed to be available to single Device here.
  513. * Hence use INIT_RATE instead of NO_SET_RATE.
  514. */
  515. *clk_rate = (*clk_rate == 0) ? (int32_t)INIT_RATE : *clk_rate;
  516. CAM_DBG(CAM_UTIL, "clk_name %s index %d clk_rate %d",
  517. clk_name, *clk_index, *clk_rate);
  518. return 0;
  519. }
  520. int cam_soc_util_clk_enable(struct clk *clk, const char *clk_name,
  521. int32_t clk_rate)
  522. {
  523. int rc = 0;
  524. if (!clk || !clk_name)
  525. return -EINVAL;
  526. rc = cam_soc_util_set_clk_rate(clk, clk_name, clk_rate);
  527. if (rc)
  528. return rc;
  529. rc = clk_prepare_enable(clk);
  530. if (rc) {
  531. CAM_ERR(CAM_UTIL, "enable failed for %s: rc(%d)", clk_name, rc);
  532. return rc;
  533. }
  534. return rc;
  535. }
  536. int cam_soc_util_clk_disable(struct clk *clk, const char *clk_name)
  537. {
  538. if (!clk || !clk_name)
  539. return -EINVAL;
  540. CAM_DBG(CAM_UTIL, "disable %s", clk_name);
  541. clk_disable_unprepare(clk);
  542. return 0;
  543. }
  544. /**
  545. * cam_soc_util_clk_enable_default()
  546. *
  547. * @brief: This function enables the default clocks present
  548. * in soc_info
  549. *
  550. * @soc_info: Device soc struct to be populated
  551. * @clk_level: Clk level to apply while enabling
  552. *
  553. * @return: success or failure
  554. */
  555. int cam_soc_util_clk_enable_default(struct cam_hw_soc_info *soc_info,
  556. enum cam_vote_level clk_level)
  557. {
  558. int i, rc = 0;
  559. enum cam_vote_level apply_level;
  560. if ((soc_info->num_clk == 0) ||
  561. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  562. CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
  563. soc_info->num_clk);
  564. return -EINVAL;
  565. }
  566. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
  567. &apply_level);
  568. if (rc)
  569. return rc;
  570. if (soc_info->cam_cx_ipeak_enable)
  571. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
  572. for (i = 0; i < soc_info->num_clk; i++) {
  573. rc = cam_soc_util_clk_enable(soc_info->clk[i],
  574. soc_info->clk_name[i],
  575. soc_info->clk_rate[apply_level][i]);
  576. if (rc)
  577. goto clk_disable;
  578. if (soc_info->cam_cx_ipeak_enable) {
  579. CAM_DBG(CAM_UTIL,
  580. "dev name = %s clk name = %s idx = %d\n"
  581. "apply_level = %d clc idx = %d",
  582. soc_info->dev_name, soc_info->clk_name[i], i,
  583. apply_level, i);
  584. }
  585. }
  586. return rc;
  587. clk_disable:
  588. if (soc_info->cam_cx_ipeak_enable)
  589. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  590. for (i--; i >= 0; i--) {
  591. cam_soc_util_clk_disable(soc_info->clk[i],
  592. soc_info->clk_name[i]);
  593. }
  594. return rc;
  595. }
  596. /**
  597. * cam_soc_util_clk_disable_default()
  598. *
  599. * @brief: This function disables the default clocks present
  600. * in soc_info
  601. *
  602. * @soc_info: device soc struct to be populated
  603. *
  604. * @return: success or failure
  605. */
  606. void cam_soc_util_clk_disable_default(struct cam_hw_soc_info *soc_info)
  607. {
  608. int i;
  609. if (soc_info->num_clk == 0)
  610. return;
  611. if (soc_info->cam_cx_ipeak_enable)
  612. cam_cx_ipeak_unvote_cx_ipeak(soc_info);
  613. for (i = soc_info->num_clk - 1; i >= 0; i--)
  614. cam_soc_util_clk_disable(soc_info->clk[i],
  615. soc_info->clk_name[i]);
  616. }
  617. /**
  618. * cam_soc_util_get_dt_clk_info()
  619. *
  620. * @brief: Parse the DT and populate the Clock properties
  621. *
  622. * @soc_info: device soc struct to be populated
  623. * @src_clk_str name of src clock that has rate control
  624. *
  625. * @return: success or failure
  626. */
  627. static int cam_soc_util_get_dt_clk_info(struct cam_hw_soc_info *soc_info)
  628. {
  629. struct device_node *of_node = NULL;
  630. int count;
  631. int num_clk_rates, num_clk_levels;
  632. int i, j, rc;
  633. int32_t num_clk_level_strings;
  634. const char *src_clk_str = NULL;
  635. const char *scl_clk_str = NULL;
  636. const char *clk_control_debugfs = NULL;
  637. const char *clk_cntl_lvl_string = NULL;
  638. enum cam_vote_level level;
  639. if (!soc_info || !soc_info->dev)
  640. return -EINVAL;
  641. of_node = soc_info->dev->of_node;
  642. if (!of_property_read_bool(of_node, "use-shared-clk")) {
  643. CAM_DBG(CAM_UTIL, "No shared clk parameter defined");
  644. soc_info->use_shared_clk = false;
  645. } else {
  646. soc_info->use_shared_clk = true;
  647. }
  648. count = of_property_count_strings(of_node, "clock-names");
  649. CAM_DBG(CAM_UTIL, "E: dev_name = %s count = %d",
  650. soc_info->dev_name, count);
  651. if (count > CAM_SOC_MAX_CLK) {
  652. CAM_ERR(CAM_UTIL, "invalid count of clocks, count=%d", count);
  653. rc = -EINVAL;
  654. return rc;
  655. }
  656. if (count <= 0) {
  657. CAM_DBG(CAM_UTIL, "No clock-names found");
  658. count = 0;
  659. soc_info->num_clk = count;
  660. return 0;
  661. }
  662. soc_info->num_clk = count;
  663. for (i = 0; i < count; i++) {
  664. rc = of_property_read_string_index(of_node, "clock-names",
  665. i, &(soc_info->clk_name[i]));
  666. CAM_DBG(CAM_UTIL, "clock-names[%d] = %s",
  667. i, soc_info->clk_name[i]);
  668. if (rc) {
  669. CAM_ERR(CAM_UTIL,
  670. "i= %d count= %d reading clock-names failed",
  671. i, count);
  672. return rc;
  673. }
  674. }
  675. num_clk_rates = of_property_count_u32_elems(of_node, "clock-rates");
  676. if (num_clk_rates <= 0) {
  677. CAM_ERR(CAM_UTIL, "reading clock-rates count failed");
  678. return -EINVAL;
  679. }
  680. if ((num_clk_rates % soc_info->num_clk) != 0) {
  681. CAM_ERR(CAM_UTIL,
  682. "mismatch clk/rates, No of clocks=%d, No of rates=%d",
  683. soc_info->num_clk, num_clk_rates);
  684. return -EINVAL;
  685. }
  686. num_clk_levels = (num_clk_rates / soc_info->num_clk);
  687. num_clk_level_strings = of_property_count_strings(of_node,
  688. "clock-cntl-level");
  689. if (num_clk_level_strings != num_clk_levels) {
  690. CAM_ERR(CAM_UTIL,
  691. "Mismatch No of levels=%d, No of level string=%d",
  692. num_clk_levels, num_clk_level_strings);
  693. return -EINVAL;
  694. }
  695. for (i = 0; i < num_clk_levels; i++) {
  696. rc = of_property_read_string_index(of_node,
  697. "clock-cntl-level", i, &clk_cntl_lvl_string);
  698. if (rc) {
  699. CAM_ERR(CAM_UTIL,
  700. "Error reading clock-cntl-level, rc=%d", rc);
  701. return rc;
  702. }
  703. rc = cam_soc_util_get_level_from_string(clk_cntl_lvl_string,
  704. &level);
  705. if (rc)
  706. return rc;
  707. CAM_DBG(CAM_UTIL,
  708. "[%d] : %s %d", i, clk_cntl_lvl_string, level);
  709. soc_info->clk_level_valid[level] = true;
  710. for (j = 0; j < soc_info->num_clk; j++) {
  711. rc = of_property_read_u32_index(of_node, "clock-rates",
  712. ((i * soc_info->num_clk) + j),
  713. &soc_info->clk_rate[level][j]);
  714. if (rc) {
  715. CAM_ERR(CAM_UTIL,
  716. "Error reading clock-rates, rc=%d",
  717. rc);
  718. return rc;
  719. }
  720. soc_info->clk_rate[level][j] =
  721. (soc_info->clk_rate[level][j] == 0) ?
  722. (int32_t)NO_SET_RATE :
  723. soc_info->clk_rate[level][j];
  724. CAM_DBG(CAM_UTIL, "soc_info->clk_rate[%d][%d] = %d",
  725. level, j,
  726. soc_info->clk_rate[level][j]);
  727. }
  728. }
  729. soc_info->src_clk_idx = -1;
  730. rc = of_property_read_string_index(of_node, "src-clock-name", 0,
  731. &src_clk_str);
  732. if (rc || !src_clk_str) {
  733. CAM_DBG(CAM_UTIL, "No src_clk_str found");
  734. rc = 0;
  735. goto end;
  736. }
  737. for (i = 0; i < soc_info->num_clk; i++) {
  738. if (strcmp(soc_info->clk_name[i], src_clk_str) == 0) {
  739. soc_info->src_clk_idx = i;
  740. CAM_DBG(CAM_UTIL, "src clock = %s, index = %d",
  741. src_clk_str, i);
  742. break;
  743. }
  744. }
  745. /* scalable clk info parsing */
  746. soc_info->scl_clk_count = 0;
  747. soc_info->scl_clk_count = of_property_count_strings(of_node,
  748. "scl-clk-names");
  749. if ((soc_info->scl_clk_count <= 0) ||
  750. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  751. if (soc_info->scl_clk_count == -EINVAL) {
  752. CAM_DBG(CAM_UTIL, "scl_clk_name prop not avialable");
  753. } else if ((soc_info->scl_clk_count == -ENODATA) ||
  754. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  755. CAM_ERR(CAM_UTIL, "Invalid scl_clk_count: %d",
  756. soc_info->scl_clk_count);
  757. return -EINVAL;
  758. }
  759. CAM_DBG(CAM_UTIL, "Invalid scl_clk count: %d",
  760. soc_info->scl_clk_count);
  761. soc_info->scl_clk_count = -1;
  762. } else {
  763. CAM_DBG(CAM_UTIL, "No of scalable clocks: %d",
  764. soc_info->scl_clk_count);
  765. for (i = 0; i < soc_info->scl_clk_count; i++) {
  766. rc = of_property_read_string_index(of_node,
  767. "scl-clk-names", i,
  768. (const char **)&scl_clk_str);
  769. if (rc || !scl_clk_str) {
  770. CAM_WARN(CAM_UTIL, "scl_clk_str is NULL");
  771. soc_info->scl_clk_idx[i] = -1;
  772. continue;
  773. }
  774. for (j = 0; j < soc_info->num_clk; j++) {
  775. if (strnstr(scl_clk_str, soc_info->clk_name[j],
  776. strlen(scl_clk_str))) {
  777. soc_info->scl_clk_idx[i] = j;
  778. CAM_DBG(CAM_UTIL,
  779. "scl clock = %s, index = %d",
  780. scl_clk_str, j);
  781. break;
  782. }
  783. }
  784. }
  785. }
  786. rc = of_property_read_string_index(of_node,
  787. "clock-control-debugfs", 0, &clk_control_debugfs);
  788. if (rc || !clk_control_debugfs) {
  789. CAM_DBG(CAM_UTIL, "No clock_control_debugfs property found");
  790. rc = 0;
  791. goto end;
  792. }
  793. if (strcmp("true", clk_control_debugfs) == 0)
  794. soc_info->clk_control_enable = true;
  795. CAM_DBG(CAM_UTIL, "X: dev_name = %s count = %d",
  796. soc_info->dev_name, count);
  797. end:
  798. return rc;
  799. }
  800. int cam_soc_util_set_clk_rate_level(struct cam_hw_soc_info *soc_info,
  801. enum cam_vote_level clk_level)
  802. {
  803. int i, rc = 0;
  804. enum cam_vote_level apply_level;
  805. if ((soc_info->num_clk == 0) ||
  806. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  807. CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
  808. soc_info->num_clk);
  809. return -EINVAL;
  810. }
  811. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
  812. &apply_level);
  813. if (rc)
  814. return rc;
  815. if (soc_info->cam_cx_ipeak_enable)
  816. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
  817. for (i = 0; i < soc_info->num_clk; i++) {
  818. rc = cam_soc_util_set_clk_rate(soc_info->clk[i],
  819. soc_info->clk_name[i],
  820. soc_info->clk_rate[apply_level][i]);
  821. if (rc < 0) {
  822. CAM_DBG(CAM_UTIL,
  823. "dev name = %s clk_name = %s idx = %d\n"
  824. "apply_level = %d",
  825. soc_info->dev_name, soc_info->clk_name[i],
  826. i, apply_level);
  827. if (soc_info->cam_cx_ipeak_enable)
  828. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  829. break;
  830. }
  831. }
  832. return rc;
  833. };
  834. static int cam_soc_util_get_dt_gpio_req_tbl(struct device_node *of_node,
  835. struct cam_soc_gpio_data *gconf, uint16_t *gpio_array,
  836. uint16_t gpio_array_size)
  837. {
  838. int32_t rc = 0, i = 0;
  839. uint32_t count = 0;
  840. uint32_t *val_array = NULL;
  841. if (!of_get_property(of_node, "gpio-req-tbl-num", &count))
  842. return 0;
  843. count /= sizeof(uint32_t);
  844. if (!count) {
  845. CAM_ERR(CAM_UTIL, "gpio-req-tbl-num 0");
  846. return 0;
  847. }
  848. val_array = kcalloc(count, sizeof(uint32_t), GFP_KERNEL);
  849. if (!val_array)
  850. return -ENOMEM;
  851. gconf->cam_gpio_req_tbl = kcalloc(count, sizeof(struct gpio),
  852. GFP_KERNEL);
  853. if (!gconf->cam_gpio_req_tbl) {
  854. rc = -ENOMEM;
  855. goto free_val_array;
  856. }
  857. gconf->cam_gpio_req_tbl_size = count;
  858. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-num",
  859. val_array, count);
  860. if (rc) {
  861. CAM_ERR(CAM_UTIL, "failed in reading gpio-req-tbl-num, rc = %d",
  862. rc);
  863. goto free_gpio_req_tbl;
  864. }
  865. for (i = 0; i < count; i++) {
  866. if (val_array[i] >= gpio_array_size) {
  867. CAM_ERR(CAM_UTIL, "gpio req tbl index %d invalid",
  868. val_array[i]);
  869. goto free_gpio_req_tbl;
  870. }
  871. gconf->cam_gpio_req_tbl[i].gpio = gpio_array[val_array[i]];
  872. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].gpio = %d", i,
  873. gconf->cam_gpio_req_tbl[i].gpio);
  874. }
  875. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-flags",
  876. val_array, count);
  877. if (rc) {
  878. CAM_ERR(CAM_UTIL, "Failed in gpio-req-tbl-flags, rc %d", rc);
  879. goto free_gpio_req_tbl;
  880. }
  881. for (i = 0; i < count; i++) {
  882. gconf->cam_gpio_req_tbl[i].flags = val_array[i];
  883. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].flags = %ld", i,
  884. gconf->cam_gpio_req_tbl[i].flags);
  885. }
  886. for (i = 0; i < count; i++) {
  887. rc = of_property_read_string_index(of_node,
  888. "gpio-req-tbl-label", i,
  889. &gconf->cam_gpio_req_tbl[i].label);
  890. if (rc) {
  891. CAM_ERR(CAM_UTIL, "Failed rc %d", rc);
  892. goto free_gpio_req_tbl;
  893. }
  894. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].label = %s", i,
  895. gconf->cam_gpio_req_tbl[i].label);
  896. }
  897. kfree(val_array);
  898. return rc;
  899. free_gpio_req_tbl:
  900. kfree(gconf->cam_gpio_req_tbl);
  901. free_val_array:
  902. kfree(val_array);
  903. gconf->cam_gpio_req_tbl_size = 0;
  904. return rc;
  905. }
  906. static int cam_soc_util_get_gpio_info(struct cam_hw_soc_info *soc_info)
  907. {
  908. int32_t rc = 0, i = 0;
  909. uint16_t *gpio_array = NULL;
  910. int16_t gpio_array_size = 0;
  911. struct cam_soc_gpio_data *gconf = NULL;
  912. struct device_node *of_node = NULL;
  913. if (!soc_info || !soc_info->dev)
  914. return -EINVAL;
  915. of_node = soc_info->dev->of_node;
  916. /* Validate input parameters */
  917. if (!of_node) {
  918. CAM_ERR(CAM_UTIL, "Invalid param of_node");
  919. return -EINVAL;
  920. }
  921. gpio_array_size = of_gpio_count(of_node);
  922. if (gpio_array_size <= 0)
  923. return 0;
  924. CAM_DBG(CAM_UTIL, "gpio count %d", gpio_array_size);
  925. gpio_array = kcalloc(gpio_array_size, sizeof(uint16_t), GFP_KERNEL);
  926. if (!gpio_array)
  927. goto free_gpio_conf;
  928. for (i = 0; i < gpio_array_size; i++) {
  929. gpio_array[i] = of_get_gpio(of_node, i);
  930. CAM_DBG(CAM_UTIL, "gpio_array[%d] = %d", i, gpio_array[i]);
  931. }
  932. gconf = kzalloc(sizeof(*gconf), GFP_KERNEL);
  933. if (!gconf)
  934. return -ENOMEM;
  935. rc = cam_soc_util_get_dt_gpio_req_tbl(of_node, gconf, gpio_array,
  936. gpio_array_size);
  937. if (rc) {
  938. CAM_ERR(CAM_UTIL, "failed in msm_camera_get_dt_gpio_req_tbl");
  939. goto free_gpio_array;
  940. }
  941. gconf->cam_gpio_common_tbl = kcalloc(gpio_array_size,
  942. sizeof(struct gpio), GFP_KERNEL);
  943. if (!gconf->cam_gpio_common_tbl) {
  944. rc = -ENOMEM;
  945. goto free_gpio_array;
  946. }
  947. for (i = 0; i < gpio_array_size; i++)
  948. gconf->cam_gpio_common_tbl[i].gpio = gpio_array[i];
  949. gconf->cam_gpio_common_tbl_size = gpio_array_size;
  950. soc_info->gpio_data = gconf;
  951. kfree(gpio_array);
  952. return rc;
  953. free_gpio_array:
  954. kfree(gpio_array);
  955. free_gpio_conf:
  956. kfree(gconf);
  957. soc_info->gpio_data = NULL;
  958. return rc;
  959. }
  960. static int cam_soc_util_request_gpio_table(
  961. struct cam_hw_soc_info *soc_info, bool gpio_en)
  962. {
  963. int rc = 0, i = 0;
  964. uint8_t size = 0;
  965. struct cam_soc_gpio_data *gpio_conf =
  966. soc_info->gpio_data;
  967. struct gpio *gpio_tbl = NULL;
  968. if (!gpio_conf) {
  969. CAM_DBG(CAM_UTIL, "No GPIO entry");
  970. return 0;
  971. }
  972. if (gpio_conf->cam_gpio_common_tbl_size <= 0) {
  973. CAM_ERR(CAM_UTIL, "GPIO table size is invalid");
  974. return -EINVAL;
  975. }
  976. size = gpio_conf->cam_gpio_req_tbl_size;
  977. gpio_tbl = gpio_conf->cam_gpio_req_tbl;
  978. if (!gpio_tbl || !size) {
  979. CAM_ERR(CAM_UTIL, "Invalid gpio_tbl %pK / size %d",
  980. gpio_tbl, size);
  981. return -EINVAL;
  982. }
  983. for (i = 0; i < size; i++) {
  984. CAM_DBG(CAM_UTIL, "i=%d, gpio=%d dir=%ld", i,
  985. gpio_tbl[i].gpio, gpio_tbl[i].flags);
  986. }
  987. if (gpio_en) {
  988. for (i = 0; i < size; i++) {
  989. rc = gpio_request_one(gpio_tbl[i].gpio,
  990. gpio_tbl[i].flags, gpio_tbl[i].label);
  991. if (rc) {
  992. /*
  993. * After GPIO request fails, contine to
  994. * apply new gpios, outout a error message
  995. * for driver bringup debug
  996. */
  997. CAM_ERR(CAM_UTIL, "gpio %d:%s request fails",
  998. gpio_tbl[i].gpio, gpio_tbl[i].label);
  999. }
  1000. }
  1001. } else {
  1002. gpio_free_array(gpio_tbl, size);
  1003. }
  1004. return rc;
  1005. }
  1006. static int cam_soc_util_get_dt_regulator_info
  1007. (struct cam_hw_soc_info *soc_info)
  1008. {
  1009. int rc = 0, count = 0, i = 0;
  1010. struct device_node *of_node = NULL;
  1011. if (!soc_info || !soc_info->dev) {
  1012. CAM_ERR(CAM_UTIL, "Invalid parameters");
  1013. return -EINVAL;
  1014. }
  1015. of_node = soc_info->dev->of_node;
  1016. soc_info->num_rgltr = 0;
  1017. count = of_property_count_strings(of_node, "regulator-names");
  1018. if (count != -EINVAL) {
  1019. if (count <= 0) {
  1020. CAM_ERR(CAM_UTIL, "no regulators found");
  1021. count = 0;
  1022. return -EINVAL;
  1023. }
  1024. soc_info->num_rgltr = count;
  1025. } else {
  1026. CAM_DBG(CAM_UTIL, "No regulators node found");
  1027. return 0;
  1028. }
  1029. for (i = 0; i < soc_info->num_rgltr; i++) {
  1030. rc = of_property_read_string_index(of_node,
  1031. "regulator-names", i, &soc_info->rgltr_name[i]);
  1032. CAM_DBG(CAM_UTIL, "rgltr_name[%d] = %s",
  1033. i, soc_info->rgltr_name[i]);
  1034. if (rc) {
  1035. CAM_ERR(CAM_UTIL, "no regulator resource at cnt=%d", i);
  1036. return -ENODEV;
  1037. }
  1038. }
  1039. if (!of_property_read_bool(of_node, "rgltr-cntrl-support")) {
  1040. CAM_DBG(CAM_UTIL, "No regulator control parameter defined");
  1041. soc_info->rgltr_ctrl_support = false;
  1042. return 0;
  1043. }
  1044. soc_info->rgltr_ctrl_support = true;
  1045. rc = of_property_read_u32_array(of_node, "rgltr-min-voltage",
  1046. soc_info->rgltr_min_volt, soc_info->num_rgltr);
  1047. if (rc) {
  1048. CAM_ERR(CAM_UTIL, "No minimum volatage value found, rc=%d", rc);
  1049. return -EINVAL;
  1050. }
  1051. rc = of_property_read_u32_array(of_node, "rgltr-max-voltage",
  1052. soc_info->rgltr_max_volt, soc_info->num_rgltr);
  1053. if (rc) {
  1054. CAM_ERR(CAM_UTIL, "No maximum volatage value found, rc=%d", rc);
  1055. return -EINVAL;
  1056. }
  1057. rc = of_property_read_u32_array(of_node, "rgltr-load-current",
  1058. soc_info->rgltr_op_mode, soc_info->num_rgltr);
  1059. if (rc) {
  1060. CAM_ERR(CAM_UTIL, "No Load curent found rc=%d", rc);
  1061. return -EINVAL;
  1062. }
  1063. return rc;
  1064. }
  1065. int cam_soc_util_get_dt_properties(struct cam_hw_soc_info *soc_info)
  1066. {
  1067. struct device_node *of_node = NULL;
  1068. int count = 0, i = 0, rc = 0;
  1069. if (!soc_info || !soc_info->dev)
  1070. return -EINVAL;
  1071. of_node = soc_info->dev->of_node;
  1072. rc = of_property_read_u32(of_node, "cell-index", &soc_info->index);
  1073. if (rc) {
  1074. CAM_ERR(CAM_UTIL, "device %s failed to read cell-index",
  1075. soc_info->dev_name);
  1076. return rc;
  1077. }
  1078. count = of_property_count_strings(of_node, "reg-names");
  1079. if (count <= 0) {
  1080. CAM_DBG(CAM_UTIL, "no reg-names found for: %s",
  1081. soc_info->dev_name);
  1082. count = 0;
  1083. }
  1084. soc_info->num_mem_block = count;
  1085. for (i = 0; i < soc_info->num_mem_block; i++) {
  1086. rc = of_property_read_string_index(of_node, "reg-names", i,
  1087. &soc_info->mem_block_name[i]);
  1088. if (rc) {
  1089. CAM_ERR(CAM_UTIL, "failed to read reg-names at %d", i);
  1090. return rc;
  1091. }
  1092. soc_info->mem_block[i] =
  1093. platform_get_resource_byname(soc_info->pdev,
  1094. IORESOURCE_MEM, soc_info->mem_block_name[i]);
  1095. if (!soc_info->mem_block[i]) {
  1096. CAM_ERR(CAM_UTIL, "no mem resource by name %s",
  1097. soc_info->mem_block_name[i]);
  1098. rc = -ENODEV;
  1099. return rc;
  1100. }
  1101. }
  1102. if (soc_info->num_mem_block > 0) {
  1103. rc = of_property_read_u32_array(of_node, "reg-cam-base",
  1104. soc_info->mem_block_cam_base, soc_info->num_mem_block);
  1105. if (rc) {
  1106. CAM_ERR(CAM_UTIL, "Error reading register offsets");
  1107. return rc;
  1108. }
  1109. }
  1110. rc = of_property_read_string_index(of_node, "interrupt-names", 0,
  1111. &soc_info->irq_name);
  1112. if (rc) {
  1113. CAM_DBG(CAM_UTIL, "No interrupt line preset for: %s",
  1114. soc_info->dev_name);
  1115. rc = 0;
  1116. } else {
  1117. soc_info->irq_line =
  1118. platform_get_resource_byname(soc_info->pdev,
  1119. IORESOURCE_IRQ, soc_info->irq_name);
  1120. if (!soc_info->irq_line) {
  1121. CAM_ERR(CAM_UTIL, "no irq resource");
  1122. rc = -ENODEV;
  1123. return rc;
  1124. }
  1125. }
  1126. rc = of_property_read_string_index(of_node, "compatible", 0,
  1127. (const char **)&soc_info->compatible);
  1128. if (rc) {
  1129. CAM_DBG(CAM_UTIL, "No compatible string present for: %s",
  1130. soc_info->dev_name);
  1131. rc = 0;
  1132. }
  1133. rc = cam_soc_util_get_dt_regulator_info(soc_info);
  1134. if (rc)
  1135. return rc;
  1136. rc = cam_soc_util_get_dt_clk_info(soc_info);
  1137. if (rc)
  1138. return rc;
  1139. rc = cam_soc_util_get_gpio_info(soc_info);
  1140. if (rc)
  1141. return rc;
  1142. if (of_find_property(of_node, "qcom,cam-cx-ipeak", NULL))
  1143. rc = cam_cx_ipeak_register_cx_ipeak(soc_info);
  1144. return rc;
  1145. }
  1146. /**
  1147. * cam_soc_util_get_regulator()
  1148. *
  1149. * @brief: Get regulator resource named vdd
  1150. *
  1151. * @dev: Device associated with regulator
  1152. * @reg: Return pointer to be filled with regulator on success
  1153. * @rgltr_name: Name of regulator to get
  1154. *
  1155. * @return: 0 for Success, negative value for failure
  1156. */
  1157. static int cam_soc_util_get_regulator(struct device *dev,
  1158. struct regulator **reg, const char *rgltr_name)
  1159. {
  1160. int rc = 0;
  1161. *reg = regulator_get(dev, rgltr_name);
  1162. if (IS_ERR_OR_NULL(*reg)) {
  1163. rc = PTR_ERR(*reg);
  1164. rc = rc ? rc : -EINVAL;
  1165. CAM_ERR(CAM_UTIL, "Regulator %s get failed %d", rgltr_name, rc);
  1166. *reg = NULL;
  1167. }
  1168. return rc;
  1169. }
  1170. int cam_soc_util_regulator_disable(struct regulator *rgltr,
  1171. const char *rgltr_name, uint32_t rgltr_min_volt,
  1172. uint32_t rgltr_max_volt, uint32_t rgltr_op_mode,
  1173. uint32_t rgltr_delay_ms)
  1174. {
  1175. int32_t rc = 0;
  1176. if (!rgltr) {
  1177. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  1178. return -EINVAL;
  1179. }
  1180. rc = regulator_disable(rgltr);
  1181. if (rc) {
  1182. CAM_ERR(CAM_UTIL, "%s regulator disable failed", rgltr_name);
  1183. return rc;
  1184. }
  1185. if (rgltr_delay_ms > 20)
  1186. msleep(rgltr_delay_ms);
  1187. else if (rgltr_delay_ms)
  1188. usleep_range(rgltr_delay_ms * 1000,
  1189. (rgltr_delay_ms * 1000) + 1000);
  1190. if (regulator_count_voltages(rgltr) > 0) {
  1191. regulator_set_load(rgltr, 0);
  1192. regulator_set_voltage(rgltr, 0, rgltr_max_volt);
  1193. }
  1194. return rc;
  1195. }
  1196. int cam_soc_util_regulator_enable(struct regulator *rgltr,
  1197. const char *rgltr_name,
  1198. uint32_t rgltr_min_volt, uint32_t rgltr_max_volt,
  1199. uint32_t rgltr_op_mode, uint32_t rgltr_delay)
  1200. {
  1201. int32_t rc = 0;
  1202. if (!rgltr) {
  1203. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  1204. return -EINVAL;
  1205. }
  1206. if (regulator_count_voltages(rgltr) > 0) {
  1207. CAM_DBG(CAM_UTIL, "voltage min=%d, max=%d",
  1208. rgltr_min_volt, rgltr_max_volt);
  1209. rc = regulator_set_voltage(
  1210. rgltr, rgltr_min_volt, rgltr_max_volt);
  1211. if (rc) {
  1212. CAM_ERR(CAM_UTIL, "%s set voltage failed", rgltr_name);
  1213. return rc;
  1214. }
  1215. rc = regulator_set_load(rgltr, rgltr_op_mode);
  1216. if (rc) {
  1217. CAM_ERR(CAM_UTIL, "%s set optimum mode failed",
  1218. rgltr_name);
  1219. return rc;
  1220. }
  1221. }
  1222. rc = regulator_enable(rgltr);
  1223. if (rc) {
  1224. CAM_ERR(CAM_UTIL, "%s regulator_enable failed", rgltr_name);
  1225. return rc;
  1226. }
  1227. if (rgltr_delay > 20)
  1228. msleep(rgltr_delay);
  1229. else if (rgltr_delay)
  1230. usleep_range(rgltr_delay * 1000,
  1231. (rgltr_delay * 1000) + 1000);
  1232. return rc;
  1233. }
  1234. static int cam_soc_util_request_pinctrl(
  1235. struct cam_hw_soc_info *soc_info)
  1236. {
  1237. struct cam_soc_pinctrl_info *device_pctrl = &soc_info->pinctrl_info;
  1238. struct device *dev = soc_info->dev;
  1239. device_pctrl->pinctrl = devm_pinctrl_get(dev);
  1240. if (IS_ERR_OR_NULL(device_pctrl->pinctrl)) {
  1241. CAM_DBG(CAM_UTIL, "Pinctrl not available");
  1242. device_pctrl->pinctrl = NULL;
  1243. return 0;
  1244. }
  1245. device_pctrl->gpio_state_active =
  1246. pinctrl_lookup_state(device_pctrl->pinctrl,
  1247. CAM_SOC_PINCTRL_STATE_DEFAULT);
  1248. if (IS_ERR_OR_NULL(device_pctrl->gpio_state_active)) {
  1249. CAM_ERR(CAM_UTIL,
  1250. "Failed to get the active state pinctrl handle");
  1251. device_pctrl->gpio_state_active = NULL;
  1252. return -EINVAL;
  1253. }
  1254. device_pctrl->gpio_state_suspend
  1255. = pinctrl_lookup_state(device_pctrl->pinctrl,
  1256. CAM_SOC_PINCTRL_STATE_SLEEP);
  1257. if (IS_ERR_OR_NULL(device_pctrl->gpio_state_suspend)) {
  1258. CAM_ERR(CAM_UTIL,
  1259. "Failed to get the suspend state pinctrl handle");
  1260. device_pctrl->gpio_state_suspend = NULL;
  1261. return -EINVAL;
  1262. }
  1263. return 0;
  1264. }
  1265. static void cam_soc_util_regulator_disable_default(
  1266. struct cam_hw_soc_info *soc_info)
  1267. {
  1268. int j = 0;
  1269. uint32_t num_rgltr = soc_info->num_rgltr;
  1270. for (j = num_rgltr-1; j >= 0; j--) {
  1271. if (soc_info->rgltr_ctrl_support == true) {
  1272. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  1273. soc_info->rgltr_name[j],
  1274. soc_info->rgltr_min_volt[j],
  1275. soc_info->rgltr_max_volt[j],
  1276. soc_info->rgltr_op_mode[j],
  1277. soc_info->rgltr_delay[j]);
  1278. } else {
  1279. if (soc_info->rgltr[j])
  1280. regulator_disable(soc_info->rgltr[j]);
  1281. }
  1282. }
  1283. }
  1284. static int cam_soc_util_regulator_enable_default(
  1285. struct cam_hw_soc_info *soc_info)
  1286. {
  1287. int j = 0, rc = 0;
  1288. uint32_t num_rgltr = soc_info->num_rgltr;
  1289. for (j = 0; j < num_rgltr; j++) {
  1290. if (soc_info->rgltr_ctrl_support == true) {
  1291. rc = cam_soc_util_regulator_enable(soc_info->rgltr[j],
  1292. soc_info->rgltr_name[j],
  1293. soc_info->rgltr_min_volt[j],
  1294. soc_info->rgltr_max_volt[j],
  1295. soc_info->rgltr_op_mode[j],
  1296. soc_info->rgltr_delay[j]);
  1297. } else {
  1298. if (soc_info->rgltr[j])
  1299. rc = regulator_enable(soc_info->rgltr[j]);
  1300. }
  1301. if (rc) {
  1302. CAM_ERR(CAM_UTIL, "%s enable failed",
  1303. soc_info->rgltr_name[j]);
  1304. goto disable_rgltr;
  1305. }
  1306. }
  1307. return rc;
  1308. disable_rgltr:
  1309. for (j--; j >= 0; j--) {
  1310. if (soc_info->rgltr_ctrl_support == true) {
  1311. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  1312. soc_info->rgltr_name[j],
  1313. soc_info->rgltr_min_volt[j],
  1314. soc_info->rgltr_max_volt[j],
  1315. soc_info->rgltr_op_mode[j],
  1316. soc_info->rgltr_delay[j]);
  1317. } else {
  1318. if (soc_info->rgltr[j])
  1319. regulator_disable(soc_info->rgltr[j]);
  1320. }
  1321. }
  1322. return rc;
  1323. }
  1324. int cam_soc_util_request_platform_resource(
  1325. struct cam_hw_soc_info *soc_info,
  1326. irq_handler_t handler, void *irq_data)
  1327. {
  1328. int i = 0, rc = 0;
  1329. if (!soc_info || !soc_info->dev) {
  1330. CAM_ERR(CAM_UTIL, "Invalid parameters");
  1331. return -EINVAL;
  1332. }
  1333. for (i = 0; i < soc_info->num_mem_block; i++) {
  1334. if (soc_info->reserve_mem) {
  1335. if (!request_mem_region(soc_info->mem_block[i]->start,
  1336. resource_size(soc_info->mem_block[i]),
  1337. soc_info->mem_block_name[i])){
  1338. CAM_ERR(CAM_UTIL,
  1339. "Error Mem region request Failed:%s",
  1340. soc_info->mem_block_name[i]);
  1341. rc = -ENOMEM;
  1342. goto unmap_base;
  1343. }
  1344. }
  1345. soc_info->reg_map[i].mem_base = ioremap(
  1346. soc_info->mem_block[i]->start,
  1347. resource_size(soc_info->mem_block[i]));
  1348. if (!soc_info->reg_map[i].mem_base) {
  1349. CAM_ERR(CAM_UTIL, "i= %d base NULL", i);
  1350. rc = -ENOMEM;
  1351. goto unmap_base;
  1352. }
  1353. soc_info->reg_map[i].mem_cam_base =
  1354. soc_info->mem_block_cam_base[i];
  1355. soc_info->reg_map[i].size =
  1356. resource_size(soc_info->mem_block[i]);
  1357. soc_info->num_reg_map++;
  1358. }
  1359. for (i = 0; i < soc_info->num_rgltr; i++) {
  1360. if (soc_info->rgltr_name[i] == NULL) {
  1361. CAM_ERR(CAM_UTIL, "can't find regulator name");
  1362. goto put_regulator;
  1363. }
  1364. rc = cam_soc_util_get_regulator(soc_info->dev,
  1365. &soc_info->rgltr[i],
  1366. soc_info->rgltr_name[i]);
  1367. if (rc)
  1368. goto put_regulator;
  1369. }
  1370. if (soc_info->irq_line) {
  1371. rc = devm_request_irq(soc_info->dev, soc_info->irq_line->start,
  1372. handler, IRQF_TRIGGER_RISING,
  1373. soc_info->irq_name, irq_data);
  1374. if (rc) {
  1375. CAM_ERR(CAM_UTIL, "irq request fail");
  1376. rc = -EBUSY;
  1377. goto put_regulator;
  1378. }
  1379. disable_irq(soc_info->irq_line->start);
  1380. soc_info->irq_data = irq_data;
  1381. }
  1382. /* Get Clock */
  1383. for (i = 0; i < soc_info->num_clk; i++) {
  1384. soc_info->clk[i] = clk_get(soc_info->dev,
  1385. soc_info->clk_name[i]);
  1386. if (!soc_info->clk[i]) {
  1387. CAM_ERR(CAM_UTIL, "get failed for %s",
  1388. soc_info->clk_name[i]);
  1389. rc = -ENOENT;
  1390. goto put_clk;
  1391. }
  1392. }
  1393. rc = cam_soc_util_request_pinctrl(soc_info);
  1394. if (rc)
  1395. CAM_DBG(CAM_UTIL, "Failed in request pinctrl, rc=%d", rc);
  1396. rc = cam_soc_util_request_gpio_table(soc_info, true);
  1397. if (rc) {
  1398. CAM_ERR(CAM_UTIL, "Failed in request gpio table, rc=%d", rc);
  1399. goto put_clk;
  1400. }
  1401. if (soc_info->clk_control_enable)
  1402. cam_soc_util_create_clk_lvl_debugfs(soc_info);
  1403. return rc;
  1404. put_clk:
  1405. if (i == -1)
  1406. i = soc_info->num_clk;
  1407. for (i = i - 1; i >= 0; i--) {
  1408. if (soc_info->clk[i]) {
  1409. clk_put(soc_info->clk[i]);
  1410. soc_info->clk[i] = NULL;
  1411. }
  1412. }
  1413. if (soc_info->irq_line) {
  1414. disable_irq(soc_info->irq_line->start);
  1415. devm_free_irq(soc_info->dev,
  1416. soc_info->irq_line->start, irq_data);
  1417. }
  1418. put_regulator:
  1419. if (i == -1)
  1420. i = soc_info->num_rgltr;
  1421. for (i = i - 1; i >= 0; i--) {
  1422. if (soc_info->rgltr[i]) {
  1423. regulator_disable(soc_info->rgltr[i]);
  1424. regulator_put(soc_info->rgltr[i]);
  1425. soc_info->rgltr[i] = NULL;
  1426. }
  1427. }
  1428. unmap_base:
  1429. if (i == -1)
  1430. i = soc_info->num_reg_map;
  1431. for (i = i - 1; i >= 0; i--) {
  1432. if (soc_info->reserve_mem)
  1433. release_mem_region(soc_info->mem_block[i]->start,
  1434. resource_size(soc_info->mem_block[i]));
  1435. iounmap(soc_info->reg_map[i].mem_base);
  1436. soc_info->reg_map[i].mem_base = NULL;
  1437. soc_info->reg_map[i].size = 0;
  1438. }
  1439. return rc;
  1440. }
  1441. int cam_soc_util_release_platform_resource(struct cam_hw_soc_info *soc_info)
  1442. {
  1443. int i;
  1444. if (!soc_info || !soc_info->dev) {
  1445. CAM_ERR(CAM_UTIL, "Invalid parameter");
  1446. return -EINVAL;
  1447. }
  1448. for (i = soc_info->num_clk - 1; i >= 0; i--) {
  1449. clk_put(soc_info->clk[i]);
  1450. soc_info->clk[i] = NULL;
  1451. }
  1452. for (i = soc_info->num_rgltr - 1; i >= 0; i--) {
  1453. if (soc_info->rgltr[i]) {
  1454. regulator_put(soc_info->rgltr[i]);
  1455. soc_info->rgltr[i] = NULL;
  1456. }
  1457. }
  1458. for (i = soc_info->num_reg_map - 1; i >= 0; i--) {
  1459. iounmap(soc_info->reg_map[i].mem_base);
  1460. soc_info->reg_map[i].mem_base = NULL;
  1461. soc_info->reg_map[i].size = 0;
  1462. }
  1463. if (soc_info->irq_line) {
  1464. disable_irq(soc_info->irq_line->start);
  1465. devm_free_irq(soc_info->dev,
  1466. soc_info->irq_line->start, soc_info->irq_data);
  1467. }
  1468. if (soc_info->pinctrl_info.pinctrl)
  1469. devm_pinctrl_put(soc_info->pinctrl_info.pinctrl);
  1470. /* release for gpio */
  1471. cam_soc_util_request_gpio_table(soc_info, false);
  1472. if (soc_info->clk_control_enable)
  1473. cam_soc_util_remove_clk_lvl_debugfs(soc_info);
  1474. return 0;
  1475. }
  1476. int cam_soc_util_enable_platform_resource(struct cam_hw_soc_info *soc_info,
  1477. bool enable_clocks, enum cam_vote_level clk_level, bool enable_irq)
  1478. {
  1479. int rc = 0;
  1480. if (!soc_info)
  1481. return -EINVAL;
  1482. rc = cam_soc_util_regulator_enable_default(soc_info);
  1483. if (rc) {
  1484. CAM_ERR(CAM_UTIL, "Regulators enable failed");
  1485. return rc;
  1486. }
  1487. if (enable_clocks) {
  1488. rc = cam_soc_util_clk_enable_default(soc_info, clk_level);
  1489. if (rc)
  1490. goto disable_regulator;
  1491. }
  1492. if (enable_irq) {
  1493. rc = cam_soc_util_irq_enable(soc_info);
  1494. if (rc)
  1495. goto disable_clk;
  1496. }
  1497. if (soc_info->pinctrl_info.pinctrl &&
  1498. soc_info->pinctrl_info.gpio_state_active) {
  1499. rc = pinctrl_select_state(soc_info->pinctrl_info.pinctrl,
  1500. soc_info->pinctrl_info.gpio_state_active);
  1501. if (rc)
  1502. goto disable_irq;
  1503. }
  1504. return rc;
  1505. disable_irq:
  1506. if (enable_irq)
  1507. cam_soc_util_irq_disable(soc_info);
  1508. disable_clk:
  1509. if (enable_clocks)
  1510. cam_soc_util_clk_disable_default(soc_info);
  1511. disable_regulator:
  1512. cam_soc_util_regulator_disable_default(soc_info);
  1513. return rc;
  1514. }
  1515. int cam_soc_util_disable_platform_resource(struct cam_hw_soc_info *soc_info,
  1516. bool disable_clocks, bool disable_irq)
  1517. {
  1518. int rc = 0;
  1519. if (!soc_info)
  1520. return -EINVAL;
  1521. if (disable_irq)
  1522. rc |= cam_soc_util_irq_disable(soc_info);
  1523. if (disable_clocks)
  1524. cam_soc_util_clk_disable_default(soc_info);
  1525. cam_soc_util_regulator_disable_default(soc_info);
  1526. if (soc_info->pinctrl_info.pinctrl &&
  1527. soc_info->pinctrl_info.gpio_state_suspend)
  1528. rc = pinctrl_select_state(soc_info->pinctrl_info.pinctrl,
  1529. soc_info->pinctrl_info.gpio_state_suspend);
  1530. return rc;
  1531. }
  1532. int cam_soc_util_reg_dump(struct cam_hw_soc_info *soc_info,
  1533. uint32_t base_index, uint32_t offset, int size)
  1534. {
  1535. void __iomem *base_addr = NULL;
  1536. CAM_DBG(CAM_UTIL, "base_idx %u size=%d", base_index, size);
  1537. if (!soc_info || base_index >= soc_info->num_reg_map ||
  1538. size <= 0 || (offset + size) >=
  1539. CAM_SOC_GET_REG_MAP_SIZE(soc_info, base_index))
  1540. return -EINVAL;
  1541. base_addr = CAM_SOC_GET_REG_MAP_START(soc_info, base_index);
  1542. /*
  1543. * All error checking already done above,
  1544. * hence ignoring the return value below.
  1545. */
  1546. cam_io_dump(base_addr, offset, size);
  1547. return 0;
  1548. }
  1549. static int cam_soc_util_dump_cont_reg_range(
  1550. struct cam_hw_soc_info *soc_info,
  1551. struct cam_reg_range_read_desc *reg_read, uint32_t base_idx,
  1552. struct cam_reg_dump_out_buffer *dump_out_buf, size_t max_out_size)
  1553. {
  1554. int i = 0;
  1555. uint32_t write_idx = 0;
  1556. if (!soc_info || !dump_out_buf) {
  1557. CAM_ERR(CAM_UTIL,
  1558. "Invalid input args soc_info: %pK, dump_out_buffer: %pK",
  1559. soc_info, dump_out_buf);
  1560. return -EINVAL;
  1561. }
  1562. if (reg_read->num_values >= ((max_out_size -
  1563. (size_t)dump_out_buf->bytes_written) / sizeof(uint32_t))) {
  1564. CAM_ERR(CAM_UTIL,
  1565. "Invalid num values to read: %d max values: %d bytes written: %d",
  1566. reg_read->num_values, (max_out_size /
  1567. sizeof(uint32_t)), dump_out_buf->bytes_written);
  1568. return -EINVAL;
  1569. }
  1570. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  1571. for (i = 0; i < reg_read->num_values; i++) {
  1572. dump_out_buf->dump_data[write_idx++] = reg_read->offset +
  1573. (i * 4);
  1574. dump_out_buf->dump_data[write_idx++] =
  1575. cam_soc_util_r(soc_info, base_idx,
  1576. (reg_read->offset + (i * 4)));
  1577. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1578. }
  1579. return 0;
  1580. }
  1581. static int cam_soc_util_dump_dmi_reg_range(
  1582. struct cam_hw_soc_info *soc_info,
  1583. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  1584. struct cam_reg_dump_out_buffer *dump_out_buf, size_t max_out_size)
  1585. {
  1586. int i = 0;
  1587. uint32_t write_idx = 0;
  1588. if (!soc_info || !dump_out_buf) {
  1589. CAM_ERR(CAM_UTIL,
  1590. "Invalid input args soc_info: %pK, dump_out_buffer: %pK",
  1591. soc_info, dump_out_buf);
  1592. return -EINVAL;
  1593. }
  1594. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  1595. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  1596. CAM_ERR(CAM_UTIL,
  1597. "Invalid number of requested writes, pre: %d post: %d",
  1598. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  1599. return -EINVAL;
  1600. }
  1601. if (dmi_read->dmi_data_read.num_values >= ((max_out_size -
  1602. (size_t)dump_out_buf->bytes_written) / sizeof(uint32_t))) {
  1603. CAM_ERR(CAM_UTIL,
  1604. "Invalid num values to read: %d max values: %d bytes written: %d",
  1605. dmi_read->dmi_data_read.num_values, (max_out_size /
  1606. sizeof(uint32_t)), dump_out_buf->bytes_written);
  1607. return -EINVAL;
  1608. }
  1609. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  1610. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  1611. cam_soc_util_w_mb(soc_info, base_idx,
  1612. dmi_read->pre_read_config[i].offset,
  1613. dmi_read->pre_read_config[i].value);
  1614. dump_out_buf->dump_data[write_idx++] =
  1615. dmi_read->pre_read_config[i].offset;
  1616. dump_out_buf->dump_data[write_idx++] =
  1617. dmi_read->pre_read_config[i].value;
  1618. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1619. }
  1620. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  1621. dump_out_buf->dump_data[write_idx++] =
  1622. dmi_read->dmi_data_read.offset;
  1623. dump_out_buf->dump_data[write_idx++] =
  1624. cam_soc_util_r_mb(soc_info, base_idx,
  1625. dmi_read->dmi_data_read.offset);
  1626. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1627. }
  1628. for (i = 0; i < dmi_read->num_post_writes; i++) {
  1629. cam_soc_util_w_mb(soc_info, base_idx,
  1630. dmi_read->post_read_config[i].offset,
  1631. dmi_read->post_read_config[i].value);
  1632. }
  1633. return 0;
  1634. }
  1635. int cam_soc_util_reg_dump_to_cmd_buf(void *ctx,
  1636. struct cam_cmd_buf_desc *cmd_desc, uint64_t req_id,
  1637. cam_soc_util_regspace_data_cb reg_data_cb)
  1638. {
  1639. int rc = 0, i, j;
  1640. uintptr_t cpu_addr = 0;
  1641. uintptr_t cmd_buf_start = 0;
  1642. uintptr_t cmd_in_data_end = 0;
  1643. uintptr_t cmd_buf_end = 0;
  1644. uint32_t reg_base_type = 0;
  1645. size_t buf_size = 0, remain_len = 0;
  1646. size_t max_out_size = 0;
  1647. struct cam_reg_dump_input_info *reg_input_info = NULL;
  1648. struct cam_reg_dump_desc *reg_dump_desc = NULL;
  1649. struct cam_reg_dump_out_buffer *dump_out_buf = NULL;
  1650. struct cam_reg_read_info *reg_read_info = NULL;
  1651. struct cam_hw_soc_info *soc_info;
  1652. uint32_t reg_base_idx = 0;
  1653. if (!ctx || !cmd_desc || !reg_data_cb) {
  1654. CAM_ERR(CAM_UTIL, "Invalid args to reg dump [%pK] [%pK]",
  1655. cmd_desc, reg_data_cb);
  1656. return -EINVAL;
  1657. }
  1658. if (!cmd_desc->length || !cmd_desc->size) {
  1659. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  1660. cmd_desc->length, cmd_desc->size);
  1661. return -EINVAL;
  1662. }
  1663. rc = cam_mem_get_cpu_buf(cmd_desc->mem_handle, &cpu_addr, &buf_size);
  1664. if (rc || !cpu_addr || (buf_size == 0)) {
  1665. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  1666. rc, (void *)cpu_addr);
  1667. return rc;
  1668. }
  1669. CAM_DBG(CAM_UTIL, "Get cpu buf success req_id: %llu buf_size: %zu",
  1670. req_id, buf_size);
  1671. if ((buf_size < sizeof(uint32_t)) ||
  1672. ((size_t)cmd_desc->offset > (buf_size - sizeof(uint32_t)))) {
  1673. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  1674. (size_t)cmd_desc->offset);
  1675. return -EINVAL;
  1676. }
  1677. remain_len = buf_size - (size_t)cmd_desc->offset;
  1678. if (remain_len < (size_t)cmd_desc->length) {
  1679. CAM_ERR(CAM_UTIL, "Invalid length for cmd buf: %zu",
  1680. (size_t)cmd_desc->length);
  1681. return -EINVAL;
  1682. }
  1683. cmd_buf_start = (uintptr_t)(((uint8_t *)cpu_addr) + cmd_desc->offset);
  1684. cmd_in_data_end = (uintptr_t)(((uint8_t *)cmd_buf_start) +
  1685. cmd_desc->length);
  1686. cmd_buf_end = (uintptr_t)(((uint8_t *)cmd_buf_start) +
  1687. cmd_desc->size);
  1688. if (cmd_buf_end < cmd_in_data_end) {
  1689. CAM_ERR(CAM_UTIL,
  1690. "Invalid length and size for cmd buf: [%zu] [%zu]",
  1691. (size_t)cmd_desc->length, (size_t)cmd_desc->size);
  1692. return -EINVAL;
  1693. }
  1694. CAM_DBG(CAM_UTIL,
  1695. "Buffer params start [%pK] input_end [%pK] buf_end [%pK]",
  1696. cmd_buf_start, cmd_in_data_end, cmd_buf_end);
  1697. reg_input_info = (struct cam_reg_dump_input_info *) cmd_buf_start;
  1698. if (!reg_input_info->num_dump_sets) {
  1699. CAM_ERR(CAM_UTIL, "Zero dump sets found in input info");
  1700. return -EINVAL;
  1701. }
  1702. CAM_DBG(CAM_UTIL,
  1703. "reg_input_info req_id: %llu ctx %pK num_dump_sets: %d",
  1704. req_id, ctx, reg_input_info->num_dump_sets);
  1705. for (i = 0; i < reg_input_info->num_dump_sets; i++) {
  1706. if ((remain_len - sizeof(uint32_t)) <=
  1707. (size_t)reg_input_info->dump_set_offsets[i]) {
  1708. CAM_ERR(CAM_UTIL,
  1709. "Invalid dump set offset: [%zu], remain len: [%zu]",
  1710. (size_t)reg_input_info->dump_set_offsets[i],
  1711. remain_len);
  1712. return -EINVAL;
  1713. }
  1714. reg_dump_desc = (struct cam_reg_dump_desc *)
  1715. ((uint8_t *)cmd_buf_start +
  1716. reg_input_info->dump_set_offsets[i]);
  1717. if (!reg_dump_desc->num_read_range) {
  1718. CAM_ERR(CAM_UTIL, "Zero ranges found in input info");
  1719. return -EINVAL;
  1720. }
  1721. if ((remain_len - sizeof(uint32_t))
  1722. <= (size_t)reg_dump_desc->dump_buffer_offset) {
  1723. CAM_ERR(CAM_UTIL,
  1724. "Invalid out buffer offset: [%zu], remain len: [%zu]",
  1725. (size_t)reg_dump_desc->dump_buffer_offset,
  1726. remain_len);
  1727. return -EINVAL;
  1728. }
  1729. dump_out_buf = (struct cam_reg_dump_out_buffer *)
  1730. ((uint8_t *)cmd_buf_start +
  1731. reg_dump_desc->dump_buffer_offset);
  1732. dump_out_buf->req_id = req_id;
  1733. dump_out_buf->bytes_written = 0;
  1734. max_out_size = (size_t)(cmd_desc->size -
  1735. reg_dump_desc->dump_buffer_offset);
  1736. reg_base_type = reg_dump_desc->reg_base_type;
  1737. if (reg_base_type == 0 || reg_base_type >
  1738. CAM_REG_DUMP_BASE_TYPE_CAMNOC) {
  1739. CAM_ERR(CAM_UTIL,
  1740. "Invalid Reg dump base type: %d",
  1741. reg_base_type);
  1742. return -EINVAL;
  1743. }
  1744. rc = reg_data_cb(reg_base_type, ctx, &soc_info, &reg_base_idx);
  1745. if (rc || !soc_info) {
  1746. CAM_ERR(CAM_UTIL,
  1747. "Reg space data callback failed rc: %d soc_info: [%pK]",
  1748. rc, soc_info);
  1749. return -EINVAL;
  1750. }
  1751. if (reg_base_idx > soc_info->num_reg_map) {
  1752. CAM_ERR(CAM_UTIL,
  1753. "Invalid reg base idx: %d num reg map: %d",
  1754. reg_base_idx, soc_info->num_reg_map);
  1755. return -EINVAL;
  1756. }
  1757. CAM_DBG(CAM_UTIL,
  1758. "Reg data callback success req_id: %llu base_type: %d base_idx: %d num_read_range: %d",
  1759. req_id, reg_base_type, reg_base_idx,
  1760. reg_dump_desc->num_read_range);
  1761. for (j = 0; j < reg_dump_desc->num_read_range; j++) {
  1762. CAM_DBG(CAM_UTIL,
  1763. "Number of bytes written to cmd buffer: %u req_id: %llu",
  1764. dump_out_buf->bytes_written, req_id);
  1765. reg_read_info = &reg_dump_desc->read_range[j];
  1766. if (reg_read_info->type ==
  1767. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  1768. rc = cam_soc_util_dump_cont_reg_range(soc_info,
  1769. &reg_read_info->reg_read, reg_base_idx,
  1770. dump_out_buf, max_out_size);
  1771. } else if (reg_read_info->type ==
  1772. CAM_REG_DUMP_READ_TYPE_DMI) {
  1773. rc = cam_soc_util_dump_dmi_reg_range(soc_info,
  1774. &reg_read_info->dmi_read, reg_base_idx,
  1775. dump_out_buf, max_out_size);
  1776. } else {
  1777. CAM_ERR(CAM_UTIL,
  1778. "Invalid Reg dump read type: %d",
  1779. reg_read_info->type);
  1780. return -EINVAL;
  1781. }
  1782. if (rc) {
  1783. CAM_ERR(CAM_UTIL,
  1784. "Reg range read failed rc: %d reg_base_idx: %d dump_out_buf: %pK",
  1785. rc, reg_base_idx, dump_out_buf);
  1786. return rc;
  1787. }
  1788. }
  1789. }
  1790. return 0;
  1791. }