cam_soc_util.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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. /**
  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. if (soc_info->num_mem_block > 0) {
  1093. rc = of_property_read_u32_array(of_node, "reg-cam-base",
  1094. soc_info->mem_block_cam_base, soc_info->num_mem_block);
  1095. if (rc) {
  1096. CAM_ERR(CAM_UTIL, "Error reading register offsets");
  1097. return rc;
  1098. }
  1099. }
  1100. rc = of_property_read_string_index(of_node, "interrupt-names", 0,
  1101. &soc_info->irq_name);
  1102. if (rc) {
  1103. CAM_DBG(CAM_UTIL, "No interrupt line preset for: %s",
  1104. soc_info->dev_name);
  1105. rc = 0;
  1106. } else {
  1107. soc_info->irq_line =
  1108. platform_get_resource_byname(soc_info->pdev,
  1109. IORESOURCE_IRQ, soc_info->irq_name);
  1110. if (!soc_info->irq_line) {
  1111. CAM_ERR(CAM_UTIL, "no irq resource");
  1112. rc = -ENODEV;
  1113. return rc;
  1114. }
  1115. }
  1116. rc = of_property_read_string_index(of_node, "compatible", 0,
  1117. (const char **)&soc_info->compatible);
  1118. if (rc) {
  1119. CAM_DBG(CAM_UTIL, "No compatible string present for: %s",
  1120. soc_info->dev_name);
  1121. rc = 0;
  1122. }
  1123. rc = cam_soc_util_get_dt_regulator_info(soc_info);
  1124. if (rc)
  1125. return rc;
  1126. rc = cam_soc_util_get_dt_clk_info(soc_info);
  1127. if (rc)
  1128. return rc;
  1129. rc = cam_soc_util_get_gpio_info(soc_info);
  1130. if (rc)
  1131. return rc;
  1132. if (of_find_property(of_node, "qcom,cam-cx-ipeak", NULL))
  1133. rc = cam_cx_ipeak_register_cx_ipeak(soc_info);
  1134. return rc;
  1135. }
  1136. /**
  1137. * cam_soc_util_get_regulator()
  1138. *
  1139. * @brief: Get regulator resource named vdd
  1140. *
  1141. * @dev: Device associated with regulator
  1142. * @reg: Return pointer to be filled with regulator on success
  1143. * @rgltr_name: Name of regulator to get
  1144. *
  1145. * @return: 0 for Success, negative value for failure
  1146. */
  1147. static int cam_soc_util_get_regulator(struct device *dev,
  1148. struct regulator **reg, const char *rgltr_name)
  1149. {
  1150. int rc = 0;
  1151. *reg = regulator_get(dev, rgltr_name);
  1152. if (IS_ERR_OR_NULL(*reg)) {
  1153. rc = PTR_ERR(*reg);
  1154. rc = rc ? rc : -EINVAL;
  1155. CAM_ERR(CAM_UTIL, "Regulator %s get failed %d", rgltr_name, rc);
  1156. *reg = NULL;
  1157. }
  1158. return rc;
  1159. }
  1160. int cam_soc_util_regulator_disable(struct regulator *rgltr,
  1161. const char *rgltr_name, uint32_t rgltr_min_volt,
  1162. uint32_t rgltr_max_volt, uint32_t rgltr_op_mode,
  1163. uint32_t rgltr_delay_ms)
  1164. {
  1165. int32_t rc = 0;
  1166. if (!rgltr) {
  1167. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  1168. return -EINVAL;
  1169. }
  1170. rc = regulator_disable(rgltr);
  1171. if (rc) {
  1172. CAM_ERR(CAM_UTIL, "%s regulator disable failed", rgltr_name);
  1173. return rc;
  1174. }
  1175. if (rgltr_delay_ms > 20)
  1176. msleep(rgltr_delay_ms);
  1177. else if (rgltr_delay_ms)
  1178. usleep_range(rgltr_delay_ms * 1000,
  1179. (rgltr_delay_ms * 1000) + 1000);
  1180. if (regulator_count_voltages(rgltr) > 0) {
  1181. regulator_set_load(rgltr, 0);
  1182. regulator_set_voltage(rgltr, 0, rgltr_max_volt);
  1183. }
  1184. return rc;
  1185. }
  1186. int cam_soc_util_regulator_enable(struct regulator *rgltr,
  1187. const char *rgltr_name,
  1188. uint32_t rgltr_min_volt, uint32_t rgltr_max_volt,
  1189. uint32_t rgltr_op_mode, uint32_t rgltr_delay)
  1190. {
  1191. int32_t rc = 0;
  1192. if (!rgltr) {
  1193. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  1194. return -EINVAL;
  1195. }
  1196. if (regulator_count_voltages(rgltr) > 0) {
  1197. CAM_DBG(CAM_UTIL, "voltage min=%d, max=%d",
  1198. rgltr_min_volt, rgltr_max_volt);
  1199. rc = regulator_set_voltage(
  1200. rgltr, rgltr_min_volt, rgltr_max_volt);
  1201. if (rc) {
  1202. CAM_ERR(CAM_UTIL, "%s set voltage failed", rgltr_name);
  1203. return rc;
  1204. }
  1205. rc = regulator_set_load(rgltr, rgltr_op_mode);
  1206. if (rc) {
  1207. CAM_ERR(CAM_UTIL, "%s set optimum mode failed",
  1208. rgltr_name);
  1209. return rc;
  1210. }
  1211. }
  1212. rc = regulator_enable(rgltr);
  1213. if (rc) {
  1214. CAM_ERR(CAM_UTIL, "%s regulator_enable failed", rgltr_name);
  1215. return rc;
  1216. }
  1217. if (rgltr_delay > 20)
  1218. msleep(rgltr_delay);
  1219. else if (rgltr_delay)
  1220. usleep_range(rgltr_delay * 1000,
  1221. (rgltr_delay * 1000) + 1000);
  1222. return rc;
  1223. }
  1224. static int cam_soc_util_request_pinctrl(
  1225. struct cam_hw_soc_info *soc_info)
  1226. {
  1227. struct cam_soc_pinctrl_info *device_pctrl = &soc_info->pinctrl_info;
  1228. struct device *dev = soc_info->dev;
  1229. device_pctrl->pinctrl = devm_pinctrl_get(dev);
  1230. if (IS_ERR_OR_NULL(device_pctrl->pinctrl)) {
  1231. CAM_DBG(CAM_UTIL, "Pinctrl not available");
  1232. device_pctrl->pinctrl = NULL;
  1233. return 0;
  1234. }
  1235. device_pctrl->gpio_state_active =
  1236. pinctrl_lookup_state(device_pctrl->pinctrl,
  1237. CAM_SOC_PINCTRL_STATE_DEFAULT);
  1238. if (IS_ERR_OR_NULL(device_pctrl->gpio_state_active)) {
  1239. CAM_ERR(CAM_UTIL,
  1240. "Failed to get the active state pinctrl handle");
  1241. device_pctrl->gpio_state_active = NULL;
  1242. return -EINVAL;
  1243. }
  1244. device_pctrl->gpio_state_suspend
  1245. = pinctrl_lookup_state(device_pctrl->pinctrl,
  1246. CAM_SOC_PINCTRL_STATE_SLEEP);
  1247. if (IS_ERR_OR_NULL(device_pctrl->gpio_state_suspend)) {
  1248. CAM_ERR(CAM_UTIL,
  1249. "Failed to get the suspend state pinctrl handle");
  1250. device_pctrl->gpio_state_suspend = NULL;
  1251. return -EINVAL;
  1252. }
  1253. return 0;
  1254. }
  1255. static void cam_soc_util_regulator_disable_default(
  1256. struct cam_hw_soc_info *soc_info)
  1257. {
  1258. int j = 0;
  1259. uint32_t num_rgltr = soc_info->num_rgltr;
  1260. for (j = num_rgltr-1; j >= 0; j--) {
  1261. if (soc_info->rgltr_ctrl_support == true) {
  1262. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  1263. soc_info->rgltr_name[j],
  1264. soc_info->rgltr_min_volt[j],
  1265. soc_info->rgltr_max_volt[j],
  1266. soc_info->rgltr_op_mode[j],
  1267. soc_info->rgltr_delay[j]);
  1268. } else {
  1269. if (soc_info->rgltr[j])
  1270. regulator_disable(soc_info->rgltr[j]);
  1271. }
  1272. }
  1273. }
  1274. static int cam_soc_util_regulator_enable_default(
  1275. struct cam_hw_soc_info *soc_info)
  1276. {
  1277. int j = 0, rc = 0;
  1278. uint32_t num_rgltr = soc_info->num_rgltr;
  1279. for (j = 0; j < num_rgltr; j++) {
  1280. if (soc_info->rgltr_ctrl_support == true) {
  1281. rc = cam_soc_util_regulator_enable(soc_info->rgltr[j],
  1282. soc_info->rgltr_name[j],
  1283. soc_info->rgltr_min_volt[j],
  1284. soc_info->rgltr_max_volt[j],
  1285. soc_info->rgltr_op_mode[j],
  1286. soc_info->rgltr_delay[j]);
  1287. } else {
  1288. if (soc_info->rgltr[j])
  1289. rc = regulator_enable(soc_info->rgltr[j]);
  1290. }
  1291. if (rc) {
  1292. CAM_ERR(CAM_UTIL, "%s enable failed",
  1293. soc_info->rgltr_name[j]);
  1294. goto disable_rgltr;
  1295. }
  1296. }
  1297. return rc;
  1298. disable_rgltr:
  1299. for (j--; j >= 0; j--) {
  1300. if (soc_info->rgltr_ctrl_support == true) {
  1301. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  1302. soc_info->rgltr_name[j],
  1303. soc_info->rgltr_min_volt[j],
  1304. soc_info->rgltr_max_volt[j],
  1305. soc_info->rgltr_op_mode[j],
  1306. soc_info->rgltr_delay[j]);
  1307. } else {
  1308. if (soc_info->rgltr[j])
  1309. regulator_disable(soc_info->rgltr[j]);
  1310. }
  1311. }
  1312. return rc;
  1313. }
  1314. int cam_soc_util_request_platform_resource(
  1315. struct cam_hw_soc_info *soc_info,
  1316. irq_handler_t handler, void *irq_data)
  1317. {
  1318. int i = 0, rc = 0;
  1319. if (!soc_info || !soc_info->dev) {
  1320. CAM_ERR(CAM_UTIL, "Invalid parameters");
  1321. return -EINVAL;
  1322. }
  1323. for (i = 0; i < soc_info->num_mem_block; i++) {
  1324. if (soc_info->reserve_mem) {
  1325. if (!request_mem_region(soc_info->mem_block[i]->start,
  1326. resource_size(soc_info->mem_block[i]),
  1327. soc_info->mem_block_name[i])){
  1328. CAM_ERR(CAM_UTIL,
  1329. "Error Mem region request Failed:%s",
  1330. soc_info->mem_block_name[i]);
  1331. rc = -ENOMEM;
  1332. goto unmap_base;
  1333. }
  1334. }
  1335. soc_info->reg_map[i].mem_base = ioremap(
  1336. soc_info->mem_block[i]->start,
  1337. resource_size(soc_info->mem_block[i]));
  1338. if (!soc_info->reg_map[i].mem_base) {
  1339. CAM_ERR(CAM_UTIL, "i= %d base NULL", i);
  1340. rc = -ENOMEM;
  1341. goto unmap_base;
  1342. }
  1343. soc_info->reg_map[i].mem_cam_base =
  1344. soc_info->mem_block_cam_base[i];
  1345. soc_info->reg_map[i].size =
  1346. resource_size(soc_info->mem_block[i]);
  1347. soc_info->num_reg_map++;
  1348. }
  1349. for (i = 0; i < soc_info->num_rgltr; i++) {
  1350. if (soc_info->rgltr_name[i] == NULL) {
  1351. CAM_ERR(CAM_UTIL, "can't find regulator name");
  1352. goto put_regulator;
  1353. }
  1354. rc = cam_soc_util_get_regulator(soc_info->dev,
  1355. &soc_info->rgltr[i],
  1356. soc_info->rgltr_name[i]);
  1357. if (rc)
  1358. goto put_regulator;
  1359. }
  1360. if (soc_info->irq_line) {
  1361. rc = devm_request_irq(soc_info->dev, soc_info->irq_line->start,
  1362. handler, IRQF_TRIGGER_RISING,
  1363. soc_info->irq_name, irq_data);
  1364. if (rc) {
  1365. CAM_ERR(CAM_UTIL, "irq request fail");
  1366. rc = -EBUSY;
  1367. goto put_regulator;
  1368. }
  1369. disable_irq(soc_info->irq_line->start);
  1370. soc_info->irq_data = irq_data;
  1371. }
  1372. /* Get Clock */
  1373. for (i = 0; i < soc_info->num_clk; i++) {
  1374. soc_info->clk[i] = clk_get(soc_info->dev,
  1375. soc_info->clk_name[i]);
  1376. if (!soc_info->clk[i]) {
  1377. CAM_ERR(CAM_UTIL, "get failed for %s",
  1378. soc_info->clk_name[i]);
  1379. rc = -ENOENT;
  1380. goto put_clk;
  1381. }
  1382. }
  1383. rc = cam_soc_util_request_pinctrl(soc_info);
  1384. if (rc)
  1385. CAM_DBG(CAM_UTIL, "Failed in request pinctrl, rc=%d", rc);
  1386. rc = cam_soc_util_request_gpio_table(soc_info, true);
  1387. if (rc) {
  1388. CAM_ERR(CAM_UTIL, "Failed in request gpio table, rc=%d", rc);
  1389. goto put_clk;
  1390. }
  1391. if (soc_info->clk_control_enable)
  1392. cam_soc_util_create_clk_lvl_debugfs(soc_info);
  1393. return rc;
  1394. put_clk:
  1395. if (i == -1)
  1396. i = soc_info->num_clk;
  1397. for (i = i - 1; i >= 0; i--) {
  1398. if (soc_info->clk[i]) {
  1399. clk_put(soc_info->clk[i]);
  1400. soc_info->clk[i] = NULL;
  1401. }
  1402. }
  1403. if (soc_info->irq_line) {
  1404. disable_irq(soc_info->irq_line->start);
  1405. devm_free_irq(soc_info->dev,
  1406. soc_info->irq_line->start, irq_data);
  1407. }
  1408. put_regulator:
  1409. if (i == -1)
  1410. i = soc_info->num_rgltr;
  1411. for (i = i - 1; i >= 0; i--) {
  1412. if (soc_info->rgltr[i]) {
  1413. regulator_disable(soc_info->rgltr[i]);
  1414. regulator_put(soc_info->rgltr[i]);
  1415. soc_info->rgltr[i] = NULL;
  1416. }
  1417. }
  1418. unmap_base:
  1419. if (i == -1)
  1420. i = soc_info->num_reg_map;
  1421. for (i = i - 1; i >= 0; i--) {
  1422. if (soc_info->reserve_mem)
  1423. release_mem_region(soc_info->mem_block[i]->start,
  1424. resource_size(soc_info->mem_block[i]));
  1425. iounmap(soc_info->reg_map[i].mem_base);
  1426. soc_info->reg_map[i].mem_base = NULL;
  1427. soc_info->reg_map[i].size = 0;
  1428. }
  1429. return rc;
  1430. }
  1431. int cam_soc_util_release_platform_resource(struct cam_hw_soc_info *soc_info)
  1432. {
  1433. int i;
  1434. if (!soc_info || !soc_info->dev) {
  1435. CAM_ERR(CAM_UTIL, "Invalid parameter");
  1436. return -EINVAL;
  1437. }
  1438. for (i = soc_info->num_clk - 1; i >= 0; i--) {
  1439. clk_put(soc_info->clk[i]);
  1440. soc_info->clk[i] = NULL;
  1441. }
  1442. for (i = soc_info->num_rgltr - 1; i >= 0; i--) {
  1443. if (soc_info->rgltr[i]) {
  1444. regulator_put(soc_info->rgltr[i]);
  1445. soc_info->rgltr[i] = NULL;
  1446. }
  1447. }
  1448. for (i = soc_info->num_reg_map - 1; i >= 0; i--) {
  1449. iounmap(soc_info->reg_map[i].mem_base);
  1450. soc_info->reg_map[i].mem_base = NULL;
  1451. soc_info->reg_map[i].size = 0;
  1452. }
  1453. if (soc_info->irq_line) {
  1454. disable_irq(soc_info->irq_line->start);
  1455. devm_free_irq(soc_info->dev,
  1456. soc_info->irq_line->start, soc_info->irq_data);
  1457. }
  1458. if (soc_info->pinctrl_info.pinctrl)
  1459. devm_pinctrl_put(soc_info->pinctrl_info.pinctrl);
  1460. /* release for gpio */
  1461. cam_soc_util_request_gpio_table(soc_info, false);
  1462. if (soc_info->clk_control_enable)
  1463. cam_soc_util_remove_clk_lvl_debugfs(soc_info);
  1464. return 0;
  1465. }
  1466. int cam_soc_util_enable_platform_resource(struct cam_hw_soc_info *soc_info,
  1467. bool enable_clocks, enum cam_vote_level clk_level, bool enable_irq)
  1468. {
  1469. int rc = 0;
  1470. if (!soc_info)
  1471. return -EINVAL;
  1472. rc = cam_soc_util_regulator_enable_default(soc_info);
  1473. if (rc) {
  1474. CAM_ERR(CAM_UTIL, "Regulators enable failed");
  1475. return rc;
  1476. }
  1477. if (enable_clocks) {
  1478. rc = cam_soc_util_clk_enable_default(soc_info, clk_level);
  1479. if (rc)
  1480. goto disable_regulator;
  1481. }
  1482. if (enable_irq) {
  1483. rc = cam_soc_util_irq_enable(soc_info);
  1484. if (rc)
  1485. goto disable_clk;
  1486. }
  1487. if (soc_info->pinctrl_info.pinctrl &&
  1488. soc_info->pinctrl_info.gpio_state_active) {
  1489. rc = pinctrl_select_state(soc_info->pinctrl_info.pinctrl,
  1490. soc_info->pinctrl_info.gpio_state_active);
  1491. if (rc)
  1492. goto disable_irq;
  1493. }
  1494. return rc;
  1495. disable_irq:
  1496. if (enable_irq)
  1497. cam_soc_util_irq_disable(soc_info);
  1498. disable_clk:
  1499. if (enable_clocks)
  1500. cam_soc_util_clk_disable_default(soc_info);
  1501. disable_regulator:
  1502. cam_soc_util_regulator_disable_default(soc_info);
  1503. return rc;
  1504. }
  1505. int cam_soc_util_disable_platform_resource(struct cam_hw_soc_info *soc_info,
  1506. bool disable_clocks, bool disable_irq)
  1507. {
  1508. int rc = 0;
  1509. if (!soc_info)
  1510. return -EINVAL;
  1511. if (disable_irq)
  1512. rc |= cam_soc_util_irq_disable(soc_info);
  1513. if (disable_clocks)
  1514. cam_soc_util_clk_disable_default(soc_info);
  1515. cam_soc_util_regulator_disable_default(soc_info);
  1516. if (soc_info->pinctrl_info.pinctrl &&
  1517. soc_info->pinctrl_info.gpio_state_suspend)
  1518. rc = pinctrl_select_state(soc_info->pinctrl_info.pinctrl,
  1519. soc_info->pinctrl_info.gpio_state_suspend);
  1520. return rc;
  1521. }
  1522. int cam_soc_util_reg_dump(struct cam_hw_soc_info *soc_info,
  1523. uint32_t base_index, uint32_t offset, int size)
  1524. {
  1525. void __iomem *base_addr = NULL;
  1526. CAM_DBG(CAM_UTIL, "base_idx %u size=%d", base_index, size);
  1527. if (!soc_info || base_index >= soc_info->num_reg_map ||
  1528. size <= 0 || (offset + size) >=
  1529. CAM_SOC_GET_REG_MAP_SIZE(soc_info, base_index))
  1530. return -EINVAL;
  1531. base_addr = CAM_SOC_GET_REG_MAP_START(soc_info, base_index);
  1532. /*
  1533. * All error checking already done above,
  1534. * hence ignoring the return value below.
  1535. */
  1536. cam_io_dump(base_addr, offset, size);
  1537. return 0;
  1538. }
  1539. static int cam_soc_util_dump_cont_reg_range(
  1540. struct cam_hw_soc_info *soc_info,
  1541. struct cam_reg_range_read_desc *reg_read, uint32_t base_idx,
  1542. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  1543. {
  1544. int i = 0, rc = 0;
  1545. uint32_t write_idx = 0;
  1546. if (!soc_info || !dump_out_buf || !reg_read || !cmd_buf_end) {
  1547. CAM_ERR(CAM_UTIL,
  1548. "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK cmd_buf_end: %pK",
  1549. soc_info, dump_out_buf, reg_read, cmd_buf_end);
  1550. rc = -EINVAL;
  1551. goto end;
  1552. }
  1553. if ((reg_read->num_values) && ((reg_read->num_values > U32_MAX / 2) ||
  1554. (sizeof(uint32_t) > ((U32_MAX -
  1555. sizeof(struct cam_reg_dump_out_buffer) -
  1556. dump_out_buf->bytes_written) /
  1557. (reg_read->num_values * 2))))) {
  1558. CAM_ERR(CAM_UTIL,
  1559. "Integer Overflow bytes_written: [%u] num_values: [%u]",
  1560. dump_out_buf->bytes_written, reg_read->num_values);
  1561. rc = -EOVERFLOW;
  1562. goto end;
  1563. }
  1564. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  1565. (uintptr_t)(sizeof(struct cam_reg_dump_out_buffer)
  1566. - sizeof(uint32_t) + dump_out_buf->bytes_written +
  1567. (reg_read->num_values * 2 * sizeof(uint32_t)))) {
  1568. CAM_ERR(CAM_UTIL,
  1569. "Insufficient space in out buffer num_values: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  1570. reg_read->num_values, cmd_buf_end,
  1571. (uintptr_t)dump_out_buf);
  1572. rc = -EINVAL;
  1573. goto end;
  1574. }
  1575. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  1576. for (i = 0; i < reg_read->num_values; i++) {
  1577. if ((reg_read->offset + (i * sizeof(uint32_t))) >
  1578. (uint32_t)soc_info->reg_map[base_idx].size) {
  1579. CAM_ERR(CAM_UTIL,
  1580. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  1581. (reg_read->offset + (i * sizeof(uint32_t))),
  1582. (uint32_t)soc_info->reg_map[base_idx].size);
  1583. rc = -EINVAL;
  1584. goto end;
  1585. }
  1586. dump_out_buf->dump_data[write_idx++] = reg_read->offset +
  1587. (i * sizeof(uint32_t));
  1588. dump_out_buf->dump_data[write_idx++] =
  1589. cam_soc_util_r(soc_info, base_idx,
  1590. (reg_read->offset + (i * sizeof(uint32_t))));
  1591. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1592. }
  1593. end:
  1594. return rc;
  1595. }
  1596. static int cam_soc_util_dump_dmi_reg_range(
  1597. struct cam_hw_soc_info *soc_info,
  1598. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  1599. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  1600. {
  1601. int i = 0, rc = 0;
  1602. uint32_t write_idx = 0;
  1603. if (!soc_info || !dump_out_buf || !dmi_read || !cmd_buf_end) {
  1604. CAM_ERR(CAM_UTIL,
  1605. "Invalid input args soc_info: %pK, dump_out_buffer: %pK",
  1606. soc_info, dump_out_buf);
  1607. rc = -EINVAL;
  1608. goto end;
  1609. }
  1610. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  1611. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  1612. CAM_ERR(CAM_UTIL,
  1613. "Invalid number of requested writes, pre: %d post: %d",
  1614. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  1615. rc = -EINVAL;
  1616. goto end;
  1617. }
  1618. if ((dmi_read->num_pre_writes + dmi_read->dmi_data_read.num_values)
  1619. && ((dmi_read->num_pre_writes > U32_MAX / 2) ||
  1620. (dmi_read->dmi_data_read.num_values > U32_MAX / 2) ||
  1621. ((dmi_read->num_pre_writes * 2) > U32_MAX -
  1622. (dmi_read->dmi_data_read.num_values * 2)) ||
  1623. (sizeof(uint32_t) > ((U32_MAX -
  1624. sizeof(struct cam_reg_dump_out_buffer) -
  1625. dump_out_buf->bytes_written) / ((dmi_read->num_pre_writes +
  1626. dmi_read->dmi_data_read.num_values) * 2))))) {
  1627. CAM_ERR(CAM_UTIL,
  1628. "Integer Overflow bytes_written: [%u] num_pre_writes: [%u] num_values: [%u]",
  1629. dump_out_buf->bytes_written, dmi_read->num_pre_writes,
  1630. dmi_read->dmi_data_read.num_values);
  1631. rc = -EOVERFLOW;
  1632. goto end;
  1633. }
  1634. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  1635. (uintptr_t)(
  1636. sizeof(struct cam_reg_dump_out_buffer) - sizeof(uint32_t) +
  1637. (dump_out_buf->bytes_written +
  1638. (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
  1639. (dmi_read->dmi_data_read.num_values * 2 *
  1640. sizeof(uint32_t))))) {
  1641. CAM_ERR(CAM_UTIL,
  1642. "Insufficient space in out buffer num_read_val: [%d] num_write_val: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  1643. dmi_read->dmi_data_read.num_values,
  1644. dmi_read->num_pre_writes, cmd_buf_end,
  1645. (uintptr_t)dump_out_buf);
  1646. rc = -EINVAL;
  1647. goto end;
  1648. }
  1649. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  1650. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  1651. if (dmi_read->pre_read_config[i].offset >
  1652. (uint32_t)soc_info->reg_map[base_idx].size) {
  1653. CAM_ERR(CAM_UTIL,
  1654. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  1655. dmi_read->pre_read_config[i].offset,
  1656. (uint32_t)soc_info->reg_map[base_idx].size);
  1657. rc = -EINVAL;
  1658. goto end;
  1659. }
  1660. cam_soc_util_w_mb(soc_info, base_idx,
  1661. dmi_read->pre_read_config[i].offset,
  1662. dmi_read->pre_read_config[i].value);
  1663. dump_out_buf->dump_data[write_idx++] =
  1664. dmi_read->pre_read_config[i].offset;
  1665. dump_out_buf->dump_data[write_idx++] =
  1666. dmi_read->pre_read_config[i].value;
  1667. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1668. }
  1669. if (dmi_read->dmi_data_read.offset >
  1670. (uint32_t)soc_info->reg_map[base_idx].size) {
  1671. CAM_ERR(CAM_UTIL,
  1672. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  1673. dmi_read->dmi_data_read.offset,
  1674. (uint32_t)soc_info->reg_map[base_idx].size);
  1675. rc = -EINVAL;
  1676. goto end;
  1677. }
  1678. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  1679. dump_out_buf->dump_data[write_idx++] =
  1680. dmi_read->dmi_data_read.offset;
  1681. dump_out_buf->dump_data[write_idx++] =
  1682. cam_soc_util_r_mb(soc_info, base_idx,
  1683. dmi_read->dmi_data_read.offset);
  1684. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1685. }
  1686. for (i = 0; i < dmi_read->num_post_writes; i++) {
  1687. if (dmi_read->post_read_config[i].offset >
  1688. (uint32_t)soc_info->reg_map[base_idx].size) {
  1689. CAM_ERR(CAM_UTIL,
  1690. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  1691. dmi_read->post_read_config[i].offset,
  1692. (uint32_t)soc_info->reg_map[base_idx].size);
  1693. rc = -EINVAL;
  1694. goto end;
  1695. }
  1696. cam_soc_util_w_mb(soc_info, base_idx,
  1697. dmi_read->post_read_config[i].offset,
  1698. dmi_read->post_read_config[i].value);
  1699. }
  1700. end:
  1701. return rc;
  1702. }
  1703. int cam_soc_util_reg_dump_to_cmd_buf(void *ctx,
  1704. struct cam_cmd_buf_desc *cmd_desc, uint64_t req_id,
  1705. cam_soc_util_regspace_data_cb reg_data_cb)
  1706. {
  1707. int rc = 0, i, j;
  1708. uintptr_t cpu_addr = 0;
  1709. uintptr_t cmd_buf_start = 0;
  1710. uintptr_t cmd_in_data_end = 0;
  1711. uintptr_t cmd_buf_end = 0;
  1712. uint32_t reg_base_type = 0;
  1713. size_t buf_size = 0, remain_len = 0;
  1714. struct cam_reg_dump_input_info *reg_input_info = NULL;
  1715. struct cam_reg_dump_desc *reg_dump_desc = NULL;
  1716. struct cam_reg_dump_out_buffer *dump_out_buf = NULL;
  1717. struct cam_reg_read_info *reg_read_info = NULL;
  1718. struct cam_hw_soc_info *soc_info;
  1719. uint32_t reg_base_idx = 0;
  1720. if (!ctx || !cmd_desc || !reg_data_cb) {
  1721. CAM_ERR(CAM_UTIL, "Invalid args to reg dump [%pK] [%pK]",
  1722. cmd_desc, reg_data_cb);
  1723. return -EINVAL;
  1724. }
  1725. if (!cmd_desc->length || !cmd_desc->size) {
  1726. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  1727. cmd_desc->length, cmd_desc->size);
  1728. return -EINVAL;
  1729. }
  1730. rc = cam_mem_get_cpu_buf(cmd_desc->mem_handle, &cpu_addr, &buf_size);
  1731. if (rc || !cpu_addr || (buf_size == 0)) {
  1732. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  1733. rc, (void *)cpu_addr);
  1734. goto end;
  1735. }
  1736. CAM_DBG(CAM_UTIL, "Get cpu buf success req_id: %llu buf_size: %zu",
  1737. req_id, buf_size);
  1738. if ((buf_size < sizeof(uint32_t)) ||
  1739. ((size_t)cmd_desc->offset > (buf_size - sizeof(uint32_t)))) {
  1740. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  1741. (size_t)cmd_desc->offset);
  1742. rc = -EINVAL;
  1743. goto end;
  1744. }
  1745. remain_len = buf_size - (size_t)cmd_desc->offset;
  1746. if ((remain_len < (size_t)cmd_desc->size) || (cmd_desc->size <
  1747. cmd_desc->length)) {
  1748. CAM_ERR(CAM_UTIL,
  1749. "Invalid params for cmd buf len: %zu size: %zu remain_len: %zu",
  1750. (size_t)cmd_desc->length, (size_t)cmd_desc->length,
  1751. remain_len);
  1752. rc = -EINVAL;
  1753. goto end;
  1754. }
  1755. cmd_buf_start = cpu_addr + (uintptr_t)cmd_desc->offset;
  1756. cmd_in_data_end = cmd_buf_start + (uintptr_t)cmd_desc->length;
  1757. cmd_buf_end = cmd_buf_start + (uintptr_t)cmd_desc->size;
  1758. if ((cmd_buf_end <= cmd_buf_start) ||
  1759. (cmd_in_data_end <= cmd_buf_start)) {
  1760. CAM_ERR(CAM_UTIL,
  1761. "Invalid length or size for cmd buf: [%zu] [%zu]",
  1762. (size_t)cmd_desc->length, (size_t)cmd_desc->size);
  1763. rc = -EINVAL;
  1764. goto end;
  1765. }
  1766. CAM_DBG(CAM_UTIL,
  1767. "Buffer params start [%pK] input_end [%pK] buf_end [%pK]",
  1768. cmd_buf_start, cmd_in_data_end, cmd_buf_end);
  1769. reg_input_info = (struct cam_reg_dump_input_info *) cmd_buf_start;
  1770. if ((reg_input_info->num_dump_sets > 1) && (sizeof(uint32_t) >
  1771. ((U32_MAX - sizeof(struct cam_reg_dump_input_info)) /
  1772. (reg_input_info->num_dump_sets - 1)))) {
  1773. CAM_ERR(CAM_UTIL,
  1774. "Integer Overflow req_id: [%llu] num_dump_sets: [%u]",
  1775. req_id, reg_input_info->num_dump_sets);
  1776. rc = -EOVERFLOW;
  1777. goto end;
  1778. }
  1779. if ((!reg_input_info->num_dump_sets) ||
  1780. ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  1781. (sizeof(struct cam_reg_dump_input_info) +
  1782. ((reg_input_info->num_dump_sets - 1) * sizeof(uint32_t))))) {
  1783. CAM_ERR(CAM_UTIL,
  1784. "Invalid number of dump sets, req_id: [%llu] num_dump_sets: [%u]",
  1785. req_id, reg_input_info->num_dump_sets);
  1786. rc = -EINVAL;
  1787. goto end;
  1788. }
  1789. CAM_DBG(CAM_UTIL,
  1790. "reg_input_info req_id: %llu ctx %pK num_dump_sets: %d",
  1791. req_id, ctx, reg_input_info->num_dump_sets);
  1792. for (i = 0; i < reg_input_info->num_dump_sets; i++) {
  1793. if ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  1794. reg_input_info->dump_set_offsets[i]) {
  1795. CAM_ERR(CAM_UTIL,
  1796. "Invalid dump set offset: [%pK], cmd_buf_start: [%pK] cmd_in_data_end: [%pK]",
  1797. (uintptr_t)reg_input_info->dump_set_offsets[i],
  1798. cmd_buf_start, cmd_in_data_end);
  1799. rc = -EINVAL;
  1800. goto end;
  1801. }
  1802. reg_dump_desc = (struct cam_reg_dump_desc *)
  1803. (cmd_buf_start +
  1804. (uintptr_t)reg_input_info->dump_set_offsets[i]);
  1805. if ((reg_dump_desc->num_read_range > 1) &&
  1806. (sizeof(struct cam_reg_read_info) > ((U32_MAX -
  1807. sizeof(struct cam_reg_dump_desc)) /
  1808. (reg_dump_desc->num_read_range - 1)))) {
  1809. CAM_ERR(CAM_UTIL,
  1810. "Integer Overflow req_id: [%llu] num_read_range: [%u]",
  1811. req_id, reg_dump_desc->num_read_range);
  1812. rc = -EOVERFLOW;
  1813. goto end;
  1814. }
  1815. if ((!reg_dump_desc->num_read_range) ||
  1816. ((cmd_in_data_end - (uintptr_t)reg_dump_desc) <=
  1817. (uintptr_t)(sizeof(struct cam_reg_dump_desc) +
  1818. ((reg_dump_desc->num_read_range - 1) *
  1819. sizeof(struct cam_reg_read_info))))) {
  1820. CAM_ERR(CAM_UTIL,
  1821. "Invalid number of read ranges, req_id: [%llu] num_read_range: [%d]",
  1822. req_id, reg_dump_desc->num_read_range);
  1823. rc = -EINVAL;
  1824. goto end;
  1825. }
  1826. if ((cmd_buf_end - cmd_buf_start) <= (uintptr_t)
  1827. (reg_dump_desc->dump_buffer_offset +
  1828. sizeof(struct cam_reg_dump_out_buffer))) {
  1829. CAM_ERR(CAM_UTIL,
  1830. "Invalid out buffer offset: [%pK], cmd_buf_start: [%pK] cmd_buf_end: [%pK]",
  1831. (uintptr_t)reg_dump_desc->dump_buffer_offset,
  1832. cmd_buf_start, cmd_buf_end);
  1833. rc = -EINVAL;
  1834. goto end;
  1835. }
  1836. dump_out_buf = (struct cam_reg_dump_out_buffer *)
  1837. (cmd_buf_start +
  1838. (uintptr_t)reg_dump_desc->dump_buffer_offset);
  1839. dump_out_buf->req_id = req_id;
  1840. dump_out_buf->bytes_written = 0;
  1841. reg_base_type = reg_dump_desc->reg_base_type;
  1842. if (reg_base_type == 0 || reg_base_type >
  1843. CAM_REG_DUMP_BASE_TYPE_CAMNOC) {
  1844. CAM_ERR(CAM_UTIL,
  1845. "Invalid Reg dump base type: %d",
  1846. reg_base_type);
  1847. rc = -EINVAL;
  1848. goto end;
  1849. }
  1850. rc = reg_data_cb(reg_base_type, ctx, &soc_info, &reg_base_idx);
  1851. if (rc || !soc_info) {
  1852. CAM_ERR(CAM_UTIL,
  1853. "Reg space data callback failed rc: %d soc_info: [%pK]",
  1854. rc, soc_info);
  1855. rc = -EINVAL;
  1856. goto end;
  1857. }
  1858. if (reg_base_idx > soc_info->num_reg_map) {
  1859. CAM_ERR(CAM_UTIL,
  1860. "Invalid reg base idx: %d num reg map: %d",
  1861. reg_base_idx, soc_info->num_reg_map);
  1862. rc = -EINVAL;
  1863. goto end;
  1864. }
  1865. CAM_DBG(CAM_UTIL,
  1866. "Reg data callback success req_id: %llu base_type: %d base_idx: %d num_read_range: %d",
  1867. req_id, reg_base_type, reg_base_idx,
  1868. reg_dump_desc->num_read_range);
  1869. for (j = 0; j < reg_dump_desc->num_read_range; j++) {
  1870. CAM_DBG(CAM_UTIL,
  1871. "Number of bytes written to cmd buffer: %u req_id: %llu",
  1872. dump_out_buf->bytes_written, req_id);
  1873. reg_read_info = &reg_dump_desc->read_range[j];
  1874. if (reg_read_info->type ==
  1875. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  1876. rc = cam_soc_util_dump_cont_reg_range(soc_info,
  1877. &reg_read_info->reg_read, reg_base_idx,
  1878. dump_out_buf, cmd_buf_end);
  1879. } else if (reg_read_info->type ==
  1880. CAM_REG_DUMP_READ_TYPE_DMI) {
  1881. rc = cam_soc_util_dump_dmi_reg_range(soc_info,
  1882. &reg_read_info->dmi_read, reg_base_idx,
  1883. dump_out_buf, cmd_buf_end);
  1884. } else {
  1885. CAM_ERR(CAM_UTIL,
  1886. "Invalid Reg dump read type: %d",
  1887. reg_read_info->type);
  1888. rc = -EINVAL;
  1889. goto end;
  1890. }
  1891. if (rc) {
  1892. CAM_ERR(CAM_UTIL,
  1893. "Reg range read failed rc: %d reg_base_idx: %d dump_out_buf: %pK",
  1894. rc, reg_base_idx, dump_out_buf);
  1895. goto end;
  1896. }
  1897. }
  1898. }
  1899. end:
  1900. return rc;
  1901. }