cam_soc_util.c 66 KB

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