cam_soc_util.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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, uintptr_t cmd_buf_end)
  1553. {
  1554. int i = 0, rc = 0;
  1555. uint32_t write_idx = 0;
  1556. if (!soc_info || !dump_out_buf || !reg_read || !cmd_buf_end) {
  1557. CAM_ERR(CAM_UTIL,
  1558. "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK cmd_buf_end: %pK",
  1559. soc_info, dump_out_buf, reg_read, cmd_buf_end);
  1560. rc = -EINVAL;
  1561. goto end;
  1562. }
  1563. if ((reg_read->num_values) && ((reg_read->num_values > U32_MAX / 2) ||
  1564. (sizeof(uint32_t) > ((U32_MAX -
  1565. sizeof(struct cam_reg_dump_out_buffer) -
  1566. dump_out_buf->bytes_written) /
  1567. (reg_read->num_values * 2))))) {
  1568. CAM_ERR(CAM_UTIL,
  1569. "Integer Overflow bytes_written: [%u] num_values: [%u]",
  1570. dump_out_buf->bytes_written, reg_read->num_values);
  1571. rc = -EOVERFLOW;
  1572. goto end;
  1573. }
  1574. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  1575. (uintptr_t)(sizeof(struct cam_reg_dump_out_buffer)
  1576. - sizeof(uint32_t) + dump_out_buf->bytes_written +
  1577. (reg_read->num_values * 2 * sizeof(uint32_t)))) {
  1578. CAM_ERR(CAM_UTIL,
  1579. "Insufficient space in out buffer num_values: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  1580. reg_read->num_values, cmd_buf_end,
  1581. (uintptr_t)dump_out_buf);
  1582. rc = -EINVAL;
  1583. goto end;
  1584. }
  1585. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  1586. for (i = 0; i < reg_read->num_values; i++) {
  1587. if ((reg_read->offset + (i * sizeof(uint32_t))) >
  1588. (uint32_t)soc_info->reg_map[base_idx].size) {
  1589. CAM_ERR(CAM_UTIL,
  1590. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  1591. (reg_read->offset + (i * sizeof(uint32_t))),
  1592. (uint32_t)soc_info->reg_map[base_idx].size);
  1593. rc = -EINVAL;
  1594. goto end;
  1595. }
  1596. dump_out_buf->dump_data[write_idx++] = reg_read->offset +
  1597. (i * sizeof(uint32_t));
  1598. dump_out_buf->dump_data[write_idx++] =
  1599. cam_soc_util_r(soc_info, base_idx,
  1600. (reg_read->offset + (i * sizeof(uint32_t))));
  1601. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1602. }
  1603. end:
  1604. return rc;
  1605. }
  1606. static int cam_soc_util_dump_dmi_reg_range(
  1607. struct cam_hw_soc_info *soc_info,
  1608. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  1609. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  1610. {
  1611. int i = 0, rc = 0;
  1612. uint32_t write_idx = 0;
  1613. if (!soc_info || !dump_out_buf || !dmi_read || !cmd_buf_end) {
  1614. CAM_ERR(CAM_UTIL,
  1615. "Invalid input args soc_info: %pK, dump_out_buffer: %pK",
  1616. soc_info, dump_out_buf);
  1617. rc = -EINVAL;
  1618. goto end;
  1619. }
  1620. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  1621. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  1622. CAM_ERR(CAM_UTIL,
  1623. "Invalid number of requested writes, pre: %d post: %d",
  1624. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  1625. rc = -EINVAL;
  1626. goto end;
  1627. }
  1628. if ((dmi_read->num_pre_writes + dmi_read->dmi_data_read.num_values)
  1629. && ((dmi_read->num_pre_writes > U32_MAX / 2) ||
  1630. (dmi_read->dmi_data_read.num_values > U32_MAX / 2) ||
  1631. ((dmi_read->num_pre_writes * 2) > U32_MAX -
  1632. (dmi_read->dmi_data_read.num_values * 2)) ||
  1633. (sizeof(uint32_t) > ((U32_MAX -
  1634. sizeof(struct cam_reg_dump_out_buffer) -
  1635. dump_out_buf->bytes_written) / ((dmi_read->num_pre_writes +
  1636. dmi_read->dmi_data_read.num_values) * 2))))) {
  1637. CAM_ERR(CAM_UTIL,
  1638. "Integer Overflow bytes_written: [%u] num_pre_writes: [%u] num_values: [%u]",
  1639. dump_out_buf->bytes_written, dmi_read->num_pre_writes,
  1640. dmi_read->dmi_data_read.num_values);
  1641. rc = -EOVERFLOW;
  1642. goto end;
  1643. }
  1644. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  1645. (uintptr_t)(
  1646. sizeof(struct cam_reg_dump_out_buffer) - sizeof(uint32_t) +
  1647. (dump_out_buf->bytes_written +
  1648. (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
  1649. (dmi_read->dmi_data_read.num_values * 2 *
  1650. sizeof(uint32_t))))) {
  1651. CAM_ERR(CAM_UTIL,
  1652. "Insufficient space in out buffer num_read_val: [%d] num_write_val: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  1653. dmi_read->dmi_data_read.num_values,
  1654. dmi_read->num_pre_writes, cmd_buf_end,
  1655. (uintptr_t)dump_out_buf);
  1656. rc = -EINVAL;
  1657. goto end;
  1658. }
  1659. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  1660. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  1661. if (dmi_read->pre_read_config[i].offset >
  1662. (uint32_t)soc_info->reg_map[base_idx].size) {
  1663. CAM_ERR(CAM_UTIL,
  1664. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  1665. dmi_read->pre_read_config[i].offset,
  1666. (uint32_t)soc_info->reg_map[base_idx].size);
  1667. rc = -EINVAL;
  1668. goto end;
  1669. }
  1670. cam_soc_util_w_mb(soc_info, base_idx,
  1671. dmi_read->pre_read_config[i].offset,
  1672. dmi_read->pre_read_config[i].value);
  1673. dump_out_buf->dump_data[write_idx++] =
  1674. dmi_read->pre_read_config[i].offset;
  1675. dump_out_buf->dump_data[write_idx++] =
  1676. dmi_read->pre_read_config[i].value;
  1677. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1678. }
  1679. if (dmi_read->dmi_data_read.offset >
  1680. (uint32_t)soc_info->reg_map[base_idx].size) {
  1681. CAM_ERR(CAM_UTIL,
  1682. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  1683. dmi_read->dmi_data_read.offset,
  1684. (uint32_t)soc_info->reg_map[base_idx].size);
  1685. rc = -EINVAL;
  1686. goto end;
  1687. }
  1688. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  1689. dump_out_buf->dump_data[write_idx++] =
  1690. dmi_read->dmi_data_read.offset;
  1691. dump_out_buf->dump_data[write_idx++] =
  1692. cam_soc_util_r_mb(soc_info, base_idx,
  1693. dmi_read->dmi_data_read.offset);
  1694. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  1695. }
  1696. for (i = 0; i < dmi_read->num_post_writes; i++) {
  1697. if (dmi_read->post_read_config[i].offset >
  1698. (uint32_t)soc_info->reg_map[base_idx].size) {
  1699. CAM_ERR(CAM_UTIL,
  1700. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  1701. dmi_read->post_read_config[i].offset,
  1702. (uint32_t)soc_info->reg_map[base_idx].size);
  1703. rc = -EINVAL;
  1704. goto end;
  1705. }
  1706. cam_soc_util_w_mb(soc_info, base_idx,
  1707. dmi_read->post_read_config[i].offset,
  1708. dmi_read->post_read_config[i].value);
  1709. }
  1710. end:
  1711. return rc;
  1712. }
  1713. int cam_soc_util_reg_dump_to_cmd_buf(void *ctx,
  1714. struct cam_cmd_buf_desc *cmd_desc, uint64_t req_id,
  1715. cam_soc_util_regspace_data_cb reg_data_cb)
  1716. {
  1717. int rc = 0, i, j;
  1718. uintptr_t cpu_addr = 0;
  1719. uintptr_t cmd_buf_start = 0;
  1720. uintptr_t cmd_in_data_end = 0;
  1721. uintptr_t cmd_buf_end = 0;
  1722. uint32_t reg_base_type = 0;
  1723. size_t buf_size = 0, remain_len = 0;
  1724. struct cam_reg_dump_input_info *reg_input_info = NULL;
  1725. struct cam_reg_dump_desc *reg_dump_desc = NULL;
  1726. struct cam_reg_dump_out_buffer *dump_out_buf = NULL;
  1727. struct cam_reg_read_info *reg_read_info = NULL;
  1728. struct cam_hw_soc_info *soc_info;
  1729. uint32_t reg_base_idx = 0;
  1730. if (!ctx || !cmd_desc || !reg_data_cb) {
  1731. CAM_ERR(CAM_UTIL, "Invalid args to reg dump [%pK] [%pK]",
  1732. cmd_desc, reg_data_cb);
  1733. return -EINVAL;
  1734. }
  1735. if (!cmd_desc->length || !cmd_desc->size) {
  1736. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  1737. cmd_desc->length, cmd_desc->size);
  1738. return -EINVAL;
  1739. }
  1740. rc = cam_mem_get_cpu_buf(cmd_desc->mem_handle, &cpu_addr, &buf_size);
  1741. if (rc || !cpu_addr || (buf_size == 0)) {
  1742. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  1743. rc, (void *)cpu_addr);
  1744. goto end;
  1745. }
  1746. CAM_DBG(CAM_UTIL, "Get cpu buf success req_id: %llu buf_size: %zu",
  1747. req_id, buf_size);
  1748. if ((buf_size < sizeof(uint32_t)) ||
  1749. ((size_t)cmd_desc->offset > (buf_size - sizeof(uint32_t)))) {
  1750. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  1751. (size_t)cmd_desc->offset);
  1752. rc = -EINVAL;
  1753. goto end;
  1754. }
  1755. remain_len = buf_size - (size_t)cmd_desc->offset;
  1756. if ((remain_len < (size_t)cmd_desc->size) || (cmd_desc->size <
  1757. cmd_desc->length)) {
  1758. CAM_ERR(CAM_UTIL,
  1759. "Invalid params for cmd buf len: %zu size: %zu remain_len: %zu",
  1760. (size_t)cmd_desc->length, (size_t)cmd_desc->length,
  1761. remain_len);
  1762. rc = -EINVAL;
  1763. goto end;
  1764. }
  1765. cmd_buf_start = cpu_addr + (uintptr_t)cmd_desc->offset;
  1766. cmd_in_data_end = cmd_buf_start + (uintptr_t)cmd_desc->length;
  1767. cmd_buf_end = cmd_buf_start + (uintptr_t)cmd_desc->size;
  1768. if ((cmd_buf_end <= cmd_buf_start) ||
  1769. (cmd_in_data_end <= cmd_buf_start)) {
  1770. CAM_ERR(CAM_UTIL,
  1771. "Invalid length or size for cmd buf: [%zu] [%zu]",
  1772. (size_t)cmd_desc->length, (size_t)cmd_desc->size);
  1773. rc = -EINVAL;
  1774. goto end;
  1775. }
  1776. CAM_DBG(CAM_UTIL,
  1777. "Buffer params start [%pK] input_end [%pK] buf_end [%pK]",
  1778. cmd_buf_start, cmd_in_data_end, cmd_buf_end);
  1779. reg_input_info = (struct cam_reg_dump_input_info *) cmd_buf_start;
  1780. if ((reg_input_info->num_dump_sets > 1) && (sizeof(uint32_t) >
  1781. ((U32_MAX - sizeof(struct cam_reg_dump_input_info)) /
  1782. (reg_input_info->num_dump_sets - 1)))) {
  1783. CAM_ERR(CAM_UTIL,
  1784. "Integer Overflow req_id: [%llu] num_dump_sets: [%u]",
  1785. req_id, reg_input_info->num_dump_sets);
  1786. rc = -EOVERFLOW;
  1787. goto end;
  1788. }
  1789. if ((!reg_input_info->num_dump_sets) ||
  1790. ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  1791. (sizeof(struct cam_reg_dump_input_info) +
  1792. ((reg_input_info->num_dump_sets - 1) * sizeof(uint32_t))))) {
  1793. CAM_ERR(CAM_UTIL,
  1794. "Invalid number of dump sets, req_id: [%llu] num_dump_sets: [%u]",
  1795. req_id, reg_input_info->num_dump_sets);
  1796. rc = -EINVAL;
  1797. goto end;
  1798. }
  1799. CAM_DBG(CAM_UTIL,
  1800. "reg_input_info req_id: %llu ctx %pK num_dump_sets: %d",
  1801. req_id, ctx, reg_input_info->num_dump_sets);
  1802. for (i = 0; i < reg_input_info->num_dump_sets; i++) {
  1803. if ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  1804. reg_input_info->dump_set_offsets[i]) {
  1805. CAM_ERR(CAM_UTIL,
  1806. "Invalid dump set offset: [%pK], cmd_buf_start: [%pK] cmd_in_data_end: [%pK]",
  1807. (uintptr_t)reg_input_info->dump_set_offsets[i],
  1808. cmd_buf_start, cmd_in_data_end);
  1809. rc = -EINVAL;
  1810. goto end;
  1811. }
  1812. reg_dump_desc = (struct cam_reg_dump_desc *)
  1813. (cmd_buf_start +
  1814. (uintptr_t)reg_input_info->dump_set_offsets[i]);
  1815. if ((reg_dump_desc->num_read_range > 1) &&
  1816. (sizeof(struct cam_reg_read_info) > ((U32_MAX -
  1817. sizeof(struct cam_reg_dump_desc)) /
  1818. (reg_dump_desc->num_read_range - 1)))) {
  1819. CAM_ERR(CAM_UTIL,
  1820. "Integer Overflow req_id: [%llu] num_read_range: [%u]",
  1821. req_id, reg_dump_desc->num_read_range);
  1822. rc = -EOVERFLOW;
  1823. goto end;
  1824. }
  1825. if ((!reg_dump_desc->num_read_range) ||
  1826. ((cmd_in_data_end - (uintptr_t)reg_dump_desc) <=
  1827. (uintptr_t)(sizeof(struct cam_reg_dump_desc) +
  1828. ((reg_dump_desc->num_read_range - 1) *
  1829. sizeof(struct cam_reg_read_info))))) {
  1830. CAM_ERR(CAM_UTIL,
  1831. "Invalid number of read ranges, req_id: [%llu] num_read_range: [%d]",
  1832. req_id, reg_dump_desc->num_read_range);
  1833. rc = -EINVAL;
  1834. goto end;
  1835. }
  1836. if ((cmd_buf_end - cmd_buf_start) <= (uintptr_t)
  1837. (reg_dump_desc->dump_buffer_offset +
  1838. sizeof(struct cam_reg_dump_out_buffer))) {
  1839. CAM_ERR(CAM_UTIL,
  1840. "Invalid out buffer offset: [%pK], cmd_buf_start: [%pK] cmd_buf_end: [%pK]",
  1841. (uintptr_t)reg_dump_desc->dump_buffer_offset,
  1842. cmd_buf_start, cmd_buf_end);
  1843. rc = -EINVAL;
  1844. goto end;
  1845. }
  1846. dump_out_buf = (struct cam_reg_dump_out_buffer *)
  1847. (cmd_buf_start +
  1848. (uintptr_t)reg_dump_desc->dump_buffer_offset);
  1849. dump_out_buf->req_id = req_id;
  1850. dump_out_buf->bytes_written = 0;
  1851. reg_base_type = reg_dump_desc->reg_base_type;
  1852. if (reg_base_type == 0 || reg_base_type >
  1853. CAM_REG_DUMP_BASE_TYPE_CAMNOC) {
  1854. CAM_ERR(CAM_UTIL,
  1855. "Invalid Reg dump base type: %d",
  1856. reg_base_type);
  1857. rc = -EINVAL;
  1858. goto end;
  1859. }
  1860. rc = reg_data_cb(reg_base_type, ctx, &soc_info, &reg_base_idx);
  1861. if (rc || !soc_info) {
  1862. CAM_ERR(CAM_UTIL,
  1863. "Reg space data callback failed rc: %d soc_info: [%pK]",
  1864. rc, soc_info);
  1865. rc = -EINVAL;
  1866. goto end;
  1867. }
  1868. if (reg_base_idx > soc_info->num_reg_map) {
  1869. CAM_ERR(CAM_UTIL,
  1870. "Invalid reg base idx: %d num reg map: %d",
  1871. reg_base_idx, soc_info->num_reg_map);
  1872. rc = -EINVAL;
  1873. goto end;
  1874. }
  1875. CAM_DBG(CAM_UTIL,
  1876. "Reg data callback success req_id: %llu base_type: %d base_idx: %d num_read_range: %d",
  1877. req_id, reg_base_type, reg_base_idx,
  1878. reg_dump_desc->num_read_range);
  1879. for (j = 0; j < reg_dump_desc->num_read_range; j++) {
  1880. CAM_DBG(CAM_UTIL,
  1881. "Number of bytes written to cmd buffer: %u req_id: %llu",
  1882. dump_out_buf->bytes_written, req_id);
  1883. reg_read_info = &reg_dump_desc->read_range[j];
  1884. if (reg_read_info->type ==
  1885. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  1886. rc = cam_soc_util_dump_cont_reg_range(soc_info,
  1887. &reg_read_info->reg_read, reg_base_idx,
  1888. dump_out_buf, cmd_buf_end);
  1889. } else if (reg_read_info->type ==
  1890. CAM_REG_DUMP_READ_TYPE_DMI) {
  1891. rc = cam_soc_util_dump_dmi_reg_range(soc_info,
  1892. &reg_read_info->dmi_read, reg_base_idx,
  1893. dump_out_buf, cmd_buf_end);
  1894. } else {
  1895. CAM_ERR(CAM_UTIL,
  1896. "Invalid Reg dump read type: %d",
  1897. reg_read_info->type);
  1898. rc = -EINVAL;
  1899. goto end;
  1900. }
  1901. if (rc) {
  1902. CAM_ERR(CAM_UTIL,
  1903. "Reg range read failed rc: %d reg_base_idx: %d dump_out_buf: %pK",
  1904. rc, reg_base_idx, dump_out_buf);
  1905. goto end;
  1906. }
  1907. }
  1908. }
  1909. end:
  1910. return rc;
  1911. }