cam_soc_util.c 65 KB

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