cam_soc_util.c 64 KB

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