cam_soc_util.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/of.h>
  6. #include <linux/clk.h>
  7. #include <linux/slab.h>
  8. #include <linux/gpio.h>
  9. #include <linux/of_gpio.h>
  10. #include "cam_soc_util.h"
  11. #include "cam_debug_util.h"
  12. #include "cam_cx_ipeak.h"
  13. static char supported_clk_info[256];
  14. static char debugfs_dir_name[64];
  15. int cam_soc_util_get_clk_level(struct cam_hw_soc_info *soc_info,
  16. int32_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 = %d",
  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(
  159. struct cam_hw_soc_info *soc_info)
  160. {
  161. struct dentry *dentry = NULL;
  162. if (!soc_info) {
  163. CAM_ERR(CAM_UTIL, "soc info is NULL");
  164. return -EINVAL;
  165. }
  166. if (soc_info->dentry)
  167. return 0;
  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. dentry = soc_info->dentry;
  172. dentry = debugfs_create_dir(debugfs_dir_name, NULL);
  173. if (!dentry) {
  174. CAM_ERR(CAM_UTIL, "failed to create debug directory");
  175. return -ENOMEM;
  176. }
  177. if (!debugfs_create_file("clk_lvl_options", 0444,
  178. dentry, soc_info, &cam_soc_util_clk_lvl_options)) {
  179. CAM_ERR(CAM_UTIL, "failed to create clk_lvl_options");
  180. goto err;
  181. }
  182. if (!debugfs_create_file("clk_lvl_control", 0644,
  183. dentry, soc_info, &cam_soc_util_clk_lvl_control)) {
  184. CAM_ERR(CAM_UTIL, "failed to create clk_lvl_control");
  185. goto err;
  186. }
  187. CAM_DBG(CAM_UTIL, "clk lvl debugfs for %s successfully created",
  188. soc_info->dev_name);
  189. return 0;
  190. err:
  191. debugfs_remove_recursive(dentry);
  192. dentry = NULL;
  193. return -ENOMEM;
  194. }
  195. /**
  196. * cam_soc_util_remove_clk_lvl_debugfs()
  197. *
  198. * @brief: Removes the debugfs files used to view/control
  199. * device clk rates
  200. *
  201. * @soc_info: Device soc information
  202. *
  203. */
  204. static void cam_soc_util_remove_clk_lvl_debugfs(
  205. struct cam_hw_soc_info *soc_info)
  206. {
  207. debugfs_remove_recursive(soc_info->dentry);
  208. soc_info->dentry = NULL;
  209. }
  210. int cam_soc_util_get_level_from_string(const char *string,
  211. enum cam_vote_level *level)
  212. {
  213. if (!level)
  214. return -EINVAL;
  215. if (!strcmp(string, "suspend")) {
  216. *level = CAM_SUSPEND_VOTE;
  217. } else if (!strcmp(string, "minsvs")) {
  218. *level = CAM_MINSVS_VOTE;
  219. } else if (!strcmp(string, "lowsvs")) {
  220. *level = CAM_LOWSVS_VOTE;
  221. } else if (!strcmp(string, "svs")) {
  222. *level = CAM_SVS_VOTE;
  223. } else if (!strcmp(string, "svs_l1")) {
  224. *level = CAM_SVSL1_VOTE;
  225. } else if (!strcmp(string, "nominal")) {
  226. *level = CAM_NOMINAL_VOTE;
  227. } else if (!strcmp(string, "nominal_l1")) {
  228. *level = CAM_NOMINALL1_VOTE;
  229. } else if (!strcmp(string, "turbo")) {
  230. *level = CAM_TURBO_VOTE;
  231. } else {
  232. CAM_ERR(CAM_UTIL, "Invalid string %s", string);
  233. return -EINVAL;
  234. }
  235. return 0;
  236. }
  237. /**
  238. * cam_soc_util_get_clk_level_to_apply()
  239. *
  240. * @brief: Get the clock level to apply. If the requested level
  241. * is not valid, bump the level to next available valid
  242. * level. If no higher level found, return failure.
  243. *
  244. * @soc_info: Device soc struct to be populated
  245. * @req_level: Requested level
  246. * @apply_level Level to apply
  247. *
  248. * @return: success or failure
  249. */
  250. static int cam_soc_util_get_clk_level_to_apply(
  251. struct cam_hw_soc_info *soc_info, enum cam_vote_level req_level,
  252. enum cam_vote_level *apply_level)
  253. {
  254. if (req_level >= CAM_MAX_VOTE) {
  255. CAM_ERR(CAM_UTIL, "Invalid clock level parameter %d",
  256. req_level);
  257. return -EINVAL;
  258. }
  259. if (soc_info->clk_level_valid[req_level] == true) {
  260. *apply_level = req_level;
  261. } else {
  262. int i;
  263. for (i = (req_level + 1); i < CAM_MAX_VOTE; i++)
  264. if (soc_info->clk_level_valid[i] == true) {
  265. *apply_level = i;
  266. break;
  267. }
  268. if (i == CAM_MAX_VOTE) {
  269. CAM_ERR(CAM_UTIL,
  270. "No valid clock level found to apply, req=%d",
  271. req_level);
  272. return -EINVAL;
  273. }
  274. }
  275. CAM_DBG(CAM_UTIL, "Req level %d, Applying %d",
  276. req_level, *apply_level);
  277. return 0;
  278. }
  279. int cam_soc_util_irq_enable(struct cam_hw_soc_info *soc_info)
  280. {
  281. if (!soc_info) {
  282. CAM_ERR(CAM_UTIL, "Invalid arguments");
  283. return -EINVAL;
  284. }
  285. if (!soc_info->irq_line) {
  286. CAM_ERR(CAM_UTIL, "No IRQ line available");
  287. return -ENODEV;
  288. }
  289. enable_irq(soc_info->irq_line->start);
  290. return 0;
  291. }
  292. int cam_soc_util_irq_disable(struct cam_hw_soc_info *soc_info)
  293. {
  294. if (!soc_info) {
  295. CAM_ERR(CAM_UTIL, "Invalid arguments");
  296. return -EINVAL;
  297. }
  298. if (!soc_info->irq_line) {
  299. CAM_ERR(CAM_UTIL, "No IRQ line available");
  300. return -ENODEV;
  301. }
  302. disable_irq(soc_info->irq_line->start);
  303. return 0;
  304. }
  305. long cam_soc_util_get_clk_round_rate(struct cam_hw_soc_info *soc_info,
  306. uint32_t clk_index, unsigned long clk_rate)
  307. {
  308. if (!soc_info || (clk_index >= soc_info->num_clk) || (clk_rate == 0)) {
  309. CAM_ERR(CAM_UTIL, "Invalid input params %pK, %d %lu",
  310. soc_info, clk_index, clk_rate);
  311. return clk_rate;
  312. }
  313. return clk_round_rate(soc_info->clk[clk_index], clk_rate);
  314. }
  315. int cam_soc_util_set_clk_flags(struct cam_hw_soc_info *soc_info,
  316. uint32_t clk_index, unsigned long flags)
  317. {
  318. if (!soc_info || (clk_index >= soc_info->num_clk)) {
  319. CAM_ERR(CAM_UTIL, "Invalid input params %pK, %d",
  320. soc_info, clk_index);
  321. return -EINVAL;
  322. }
  323. return clk_set_flags(soc_info->clk[clk_index], flags);
  324. }
  325. /**
  326. * cam_soc_util_set_clk_rate()
  327. *
  328. * @brief: Sets the given rate for the clk requested for
  329. *
  330. * @clk: Clock structure information for which rate is to be set
  331. * @clk_name: Name of the clock for which rate is being set
  332. * @clk_rate Clock rate to be set
  333. *
  334. * @return: Success or failure
  335. */
  336. static int cam_soc_util_set_clk_rate(struct clk *clk, const char *clk_name,
  337. int32_t clk_rate)
  338. {
  339. int rc = 0;
  340. long clk_rate_round;
  341. if (!clk || !clk_name)
  342. return -EINVAL;
  343. CAM_DBG(CAM_UTIL, "set %s, rate %d", clk_name, clk_rate);
  344. if (clk_rate > 0) {
  345. clk_rate_round = clk_round_rate(clk, clk_rate);
  346. CAM_DBG(CAM_UTIL, "new_rate %ld", clk_rate_round);
  347. if (clk_rate_round < 0) {
  348. CAM_ERR(CAM_UTIL, "round failed for clock %s rc = %ld",
  349. clk_name, clk_rate_round);
  350. return clk_rate_round;
  351. }
  352. rc = clk_set_rate(clk, clk_rate_round);
  353. if (rc) {
  354. CAM_ERR(CAM_UTIL, "set_rate failed on %s", clk_name);
  355. return rc;
  356. }
  357. } else if (clk_rate == INIT_RATE) {
  358. clk_rate_round = clk_get_rate(clk);
  359. CAM_DBG(CAM_UTIL, "init new_rate %ld", clk_rate_round);
  360. if (clk_rate_round == 0) {
  361. clk_rate_round = clk_round_rate(clk, 0);
  362. if (clk_rate_round <= 0) {
  363. CAM_ERR(CAM_UTIL, "round rate failed on %s",
  364. clk_name);
  365. return clk_rate_round;
  366. }
  367. }
  368. rc = clk_set_rate(clk, clk_rate_round);
  369. if (rc) {
  370. CAM_ERR(CAM_UTIL, "set_rate failed on %s", clk_name);
  371. return rc;
  372. }
  373. }
  374. return rc;
  375. }
  376. int cam_soc_util_set_src_clk_rate(struct cam_hw_soc_info *soc_info,
  377. int32_t clk_rate)
  378. {
  379. int rc = 0;
  380. int i = 0;
  381. int32_t src_clk_idx;
  382. int32_t scl_clk_idx;
  383. struct clk *clk = NULL;
  384. int32_t apply_level;
  385. uint32_t clk_level_override = 0;
  386. if (!soc_info || (soc_info->src_clk_idx < 0) ||
  387. (soc_info->src_clk_idx >= CAM_SOC_MAX_CLK)) {
  388. CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d",
  389. soc_info ? soc_info->src_clk_idx : -1);
  390. return -EINVAL;
  391. }
  392. src_clk_idx = soc_info->src_clk_idx;
  393. clk_level_override = soc_info->clk_level_override;
  394. if (clk_level_override && clk_rate)
  395. clk_rate =
  396. soc_info->clk_rate[clk_level_override][src_clk_idx];
  397. clk = soc_info->clk[src_clk_idx];
  398. rc = cam_soc_util_get_clk_level(soc_info, clk_rate, src_clk_idx,
  399. &apply_level);
  400. if (rc || (apply_level < 0) || (apply_level >= CAM_MAX_VOTE)) {
  401. CAM_ERR(CAM_UTIL,
  402. "set %s, rate %d dev_name = %s apply level = %d",
  403. soc_info->clk_name[src_clk_idx], clk_rate,
  404. soc_info->dev_name, apply_level);
  405. return -EINVAL;
  406. }
  407. CAM_DBG(CAM_UTIL, "set %s, rate %d dev_name = %s apply level = %d",
  408. soc_info->clk_name[src_clk_idx], clk_rate,
  409. soc_info->dev_name, apply_level);
  410. if ((soc_info->cam_cx_ipeak_enable) && (clk_rate >= 0)) {
  411. cam_cx_ipeak_update_vote_cx_ipeak(soc_info,
  412. apply_level);
  413. }
  414. rc = cam_soc_util_set_clk_rate(clk,
  415. soc_info->clk_name[src_clk_idx], clk_rate);
  416. if (rc) {
  417. CAM_ERR(CAM_UTIL,
  418. "SET_RATE Failed: src clk: %s, rate %d, dev_name = %s rc: %d",
  419. soc_info->clk_name[src_clk_idx], clk_rate,
  420. soc_info->dev_name, rc);
  421. return rc;
  422. }
  423. /* set clk rate for scalable clk if available */
  424. for (i = 0; i < soc_info->scl_clk_count; i++) {
  425. scl_clk_idx = soc_info->scl_clk_idx[i];
  426. if (scl_clk_idx < 0) {
  427. CAM_DBG(CAM_UTIL, "Scl clk index invalid");
  428. continue;
  429. }
  430. clk = soc_info->clk[scl_clk_idx];
  431. rc = cam_soc_util_set_clk_rate(clk,
  432. soc_info->clk_name[scl_clk_idx],
  433. soc_info->clk_rate[apply_level][scl_clk_idx]);
  434. if (rc) {
  435. CAM_WARN(CAM_UTIL,
  436. "SET_RATE Failed: scl clk: %s, rate %d dev_name = %s, rc: %d",
  437. soc_info->clk_name[scl_clk_idx],
  438. soc_info->clk_rate[apply_level][scl_clk_idx],
  439. soc_info->dev_name, rc);
  440. }
  441. }
  442. return 0;
  443. }
  444. int cam_soc_util_clk_put(struct clk **clk)
  445. {
  446. if (!(*clk)) {
  447. CAM_ERR(CAM_UTIL, "Invalid params clk");
  448. return -EINVAL;
  449. }
  450. clk_put(*clk);
  451. *clk = NULL;
  452. return 0;
  453. }
  454. static struct clk *cam_soc_util_option_clk_get(struct device_node *np,
  455. int index)
  456. {
  457. struct of_phandle_args clkspec;
  458. struct clk *clk;
  459. int rc;
  460. if (index < 0)
  461. return ERR_PTR(-EINVAL);
  462. rc = of_parse_phandle_with_args(np, "clocks-option", "#clock-cells",
  463. index, &clkspec);
  464. if (rc)
  465. return ERR_PTR(rc);
  466. clk = of_clk_get_from_provider(&clkspec);
  467. of_node_put(clkspec.np);
  468. return clk;
  469. }
  470. int cam_soc_util_get_option_clk_by_name(struct cam_hw_soc_info *soc_info,
  471. const char *clk_name, struct clk **clk, int32_t *clk_index,
  472. int32_t *clk_rate)
  473. {
  474. int index = 0;
  475. int rc = 0;
  476. struct device_node *of_node = NULL;
  477. if (!soc_info || !clk_name || !clk) {
  478. CAM_ERR(CAM_UTIL,
  479. "Invalid params soc_info %pK clk_name %s clk %pK",
  480. soc_info, clk_name, clk);
  481. return -EINVAL;
  482. }
  483. of_node = soc_info->dev->of_node;
  484. index = of_property_match_string(of_node, "clock-names-option",
  485. clk_name);
  486. if (index < 0) {
  487. CAM_INFO(CAM_UTIL, "No clk data for %s", clk_name);
  488. *clk_index = -1;
  489. *clk = ERR_PTR(-EINVAL);
  490. return -EINVAL;
  491. }
  492. *clk = cam_soc_util_option_clk_get(of_node, index);
  493. if (IS_ERR(*clk)) {
  494. CAM_ERR(CAM_UTIL, "No clk named %s found. Dev %s", clk_name,
  495. soc_info->dev_name);
  496. *clk_index = -1;
  497. return -EFAULT;
  498. }
  499. *clk_index = index;
  500. rc = of_property_read_u32_index(of_node, "clock-rates-option",
  501. index, clk_rate);
  502. if (rc) {
  503. CAM_ERR(CAM_UTIL,
  504. "Error reading clock-rates clk_name %s index %d",
  505. clk_name, index);
  506. cam_soc_util_clk_put(clk);
  507. *clk_rate = 0;
  508. return rc;
  509. }
  510. /*
  511. * Option clocks are assumed to be available to single Device here.
  512. * Hence use INIT_RATE instead of NO_SET_RATE.
  513. */
  514. *clk_rate = (*clk_rate == 0) ? (int32_t)INIT_RATE : *clk_rate;
  515. CAM_DBG(CAM_UTIL, "clk_name %s index %d clk_rate %d",
  516. clk_name, *clk_index, *clk_rate);
  517. return 0;
  518. }
  519. int cam_soc_util_clk_enable(struct clk *clk, const char *clk_name,
  520. int32_t clk_rate)
  521. {
  522. int rc = 0;
  523. if (!clk || !clk_name)
  524. return -EINVAL;
  525. rc = cam_soc_util_set_clk_rate(clk, clk_name, clk_rate);
  526. if (rc)
  527. return rc;
  528. rc = clk_prepare_enable(clk);
  529. if (rc) {
  530. CAM_ERR(CAM_UTIL, "enable failed for %s: rc(%d)", clk_name, rc);
  531. return rc;
  532. }
  533. return rc;
  534. }
  535. int cam_soc_util_clk_disable(struct clk *clk, const char *clk_name)
  536. {
  537. if (!clk || !clk_name)
  538. return -EINVAL;
  539. CAM_DBG(CAM_UTIL, "disable %s", clk_name);
  540. clk_disable_unprepare(clk);
  541. return 0;
  542. }
  543. /**
  544. * cam_soc_util_clk_enable_default()
  545. *
  546. * @brief: This function enables the default clocks present
  547. * in soc_info
  548. *
  549. * @soc_info: Device soc struct to be populated
  550. * @clk_level: Clk level to apply while enabling
  551. *
  552. * @return: success or failure
  553. */
  554. int cam_soc_util_clk_enable_default(struct cam_hw_soc_info *soc_info,
  555. enum cam_vote_level clk_level)
  556. {
  557. int i, rc = 0;
  558. enum cam_vote_level apply_level;
  559. if ((soc_info->num_clk == 0) ||
  560. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  561. CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
  562. soc_info->num_clk);
  563. return -EINVAL;
  564. }
  565. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
  566. &apply_level);
  567. if (rc)
  568. return rc;
  569. if (soc_info->cam_cx_ipeak_enable)
  570. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
  571. for (i = 0; i < soc_info->num_clk; i++) {
  572. rc = cam_soc_util_clk_enable(soc_info->clk[i],
  573. soc_info->clk_name[i],
  574. soc_info->clk_rate[apply_level][i]);
  575. if (rc)
  576. goto clk_disable;
  577. if (soc_info->cam_cx_ipeak_enable) {
  578. CAM_DBG(CAM_UTIL,
  579. "dev name = %s clk name = %s idx = %d\n"
  580. "apply_level = %d clc idx = %d",
  581. soc_info->dev_name, soc_info->clk_name[i], i,
  582. apply_level, i);
  583. }
  584. }
  585. return rc;
  586. clk_disable:
  587. if (soc_info->cam_cx_ipeak_enable)
  588. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  589. for (i--; i >= 0; i--) {
  590. cam_soc_util_clk_disable(soc_info->clk[i],
  591. soc_info->clk_name[i]);
  592. }
  593. return rc;
  594. }
  595. /**
  596. * cam_soc_util_clk_disable_default()
  597. *
  598. * @brief: This function disables the default clocks present
  599. * in soc_info
  600. *
  601. * @soc_info: device soc struct to be populated
  602. *
  603. * @return: success or failure
  604. */
  605. void cam_soc_util_clk_disable_default(struct cam_hw_soc_info *soc_info)
  606. {
  607. int i;
  608. if (soc_info->num_clk == 0)
  609. return;
  610. if (soc_info->cam_cx_ipeak_enable)
  611. cam_cx_ipeak_unvote_cx_ipeak(soc_info);
  612. for (i = soc_info->num_clk - 1; i >= 0; i--)
  613. cam_soc_util_clk_disable(soc_info->clk[i],
  614. soc_info->clk_name[i]);
  615. }
  616. /**
  617. * cam_soc_util_get_dt_clk_info()
  618. *
  619. * @brief: Parse the DT and populate the Clock properties
  620. *
  621. * @soc_info: device soc struct to be populated
  622. * @src_clk_str name of src clock that has rate control
  623. *
  624. * @return: success or failure
  625. */
  626. static int cam_soc_util_get_dt_clk_info(struct cam_hw_soc_info *soc_info)
  627. {
  628. struct device_node *of_node = NULL;
  629. int count;
  630. int num_clk_rates, num_clk_levels;
  631. int i, j, rc;
  632. int32_t num_clk_level_strings;
  633. const char *src_clk_str = NULL;
  634. const char *scl_clk_str = NULL;
  635. const char *clk_control_debugfs = NULL;
  636. const char *clk_cntl_lvl_string = NULL;
  637. enum cam_vote_level level;
  638. if (!soc_info || !soc_info->dev)
  639. return -EINVAL;
  640. of_node = soc_info->dev->of_node;
  641. if (!of_property_read_bool(of_node, "use-shared-clk")) {
  642. CAM_DBG(CAM_UTIL, "No shared clk parameter defined");
  643. soc_info->use_shared_clk = false;
  644. } else {
  645. soc_info->use_shared_clk = true;
  646. }
  647. count = of_property_count_strings(of_node, "clock-names");
  648. CAM_DBG(CAM_UTIL, "E: dev_name = %s count = %d",
  649. soc_info->dev_name, count);
  650. if (count > CAM_SOC_MAX_CLK) {
  651. CAM_ERR(CAM_UTIL, "invalid count of clocks, count=%d", count);
  652. rc = -EINVAL;
  653. return rc;
  654. }
  655. if (count <= 0) {
  656. CAM_DBG(CAM_UTIL, "No clock-names found");
  657. count = 0;
  658. soc_info->num_clk = count;
  659. return 0;
  660. }
  661. soc_info->num_clk = count;
  662. for (i = 0; i < count; i++) {
  663. rc = of_property_read_string_index(of_node, "clock-names",
  664. i, &(soc_info->clk_name[i]));
  665. CAM_DBG(CAM_UTIL, "clock-names[%d] = %s",
  666. i, soc_info->clk_name[i]);
  667. if (rc) {
  668. CAM_ERR(CAM_UTIL,
  669. "i= %d count= %d reading clock-names failed",
  670. i, count);
  671. return rc;
  672. }
  673. }
  674. num_clk_rates = of_property_count_u32_elems(of_node, "clock-rates");
  675. if (num_clk_rates <= 0) {
  676. CAM_ERR(CAM_UTIL, "reading clock-rates count failed");
  677. return -EINVAL;
  678. }
  679. if ((num_clk_rates % soc_info->num_clk) != 0) {
  680. CAM_ERR(CAM_UTIL,
  681. "mismatch clk/rates, No of clocks=%d, No of rates=%d",
  682. soc_info->num_clk, num_clk_rates);
  683. return -EINVAL;
  684. }
  685. num_clk_levels = (num_clk_rates / soc_info->num_clk);
  686. num_clk_level_strings = of_property_count_strings(of_node,
  687. "clock-cntl-level");
  688. if (num_clk_level_strings != num_clk_levels) {
  689. CAM_ERR(CAM_UTIL,
  690. "Mismatch No of levels=%d, No of level string=%d",
  691. num_clk_levels, num_clk_level_strings);
  692. return -EINVAL;
  693. }
  694. for (i = 0; i < num_clk_levels; i++) {
  695. rc = of_property_read_string_index(of_node,
  696. "clock-cntl-level", i, &clk_cntl_lvl_string);
  697. if (rc) {
  698. CAM_ERR(CAM_UTIL,
  699. "Error reading clock-cntl-level, rc=%d", rc);
  700. return rc;
  701. }
  702. rc = cam_soc_util_get_level_from_string(clk_cntl_lvl_string,
  703. &level);
  704. if (rc)
  705. return rc;
  706. CAM_DBG(CAM_UTIL,
  707. "[%d] : %s %d", i, clk_cntl_lvl_string, level);
  708. soc_info->clk_level_valid[level] = true;
  709. for (j = 0; j < soc_info->num_clk; j++) {
  710. rc = of_property_read_u32_index(of_node, "clock-rates",
  711. ((i * soc_info->num_clk) + j),
  712. &soc_info->clk_rate[level][j]);
  713. if (rc) {
  714. CAM_ERR(CAM_UTIL,
  715. "Error reading clock-rates, rc=%d",
  716. rc);
  717. return rc;
  718. }
  719. soc_info->clk_rate[level][j] =
  720. (soc_info->clk_rate[level][j] == 0) ?
  721. (int32_t)NO_SET_RATE :
  722. soc_info->clk_rate[level][j];
  723. CAM_DBG(CAM_UTIL, "soc_info->clk_rate[%d][%d] = %d",
  724. level, j,
  725. soc_info->clk_rate[level][j]);
  726. }
  727. }
  728. soc_info->src_clk_idx = -1;
  729. rc = of_property_read_string_index(of_node, "src-clock-name", 0,
  730. &src_clk_str);
  731. if (rc || !src_clk_str) {
  732. CAM_DBG(CAM_UTIL, "No src_clk_str found");
  733. rc = 0;
  734. goto end;
  735. }
  736. for (i = 0; i < soc_info->num_clk; i++) {
  737. if (strcmp(soc_info->clk_name[i], src_clk_str) == 0) {
  738. soc_info->src_clk_idx = i;
  739. CAM_DBG(CAM_UTIL, "src clock = %s, index = %d",
  740. src_clk_str, i);
  741. break;
  742. }
  743. }
  744. /* scalable clk info parsing */
  745. soc_info->scl_clk_count = 0;
  746. soc_info->scl_clk_count = of_property_count_strings(of_node,
  747. "scl-clk-names");
  748. if ((soc_info->scl_clk_count <= 0) ||
  749. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  750. if (soc_info->scl_clk_count == -EINVAL) {
  751. CAM_DBG(CAM_UTIL, "scl_clk_name prop not avialable");
  752. } else if ((soc_info->scl_clk_count == -ENODATA) ||
  753. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  754. CAM_ERR(CAM_UTIL, "Invalid scl_clk_count: %d",
  755. soc_info->scl_clk_count);
  756. return -EINVAL;
  757. }
  758. CAM_DBG(CAM_UTIL, "Invalid scl_clk count: %d",
  759. soc_info->scl_clk_count);
  760. soc_info->scl_clk_count = -1;
  761. } else {
  762. CAM_DBG(CAM_UTIL, "No of scalable clocks: %d",
  763. soc_info->scl_clk_count);
  764. for (i = 0; i < soc_info->scl_clk_count; i++) {
  765. rc = of_property_read_string_index(of_node,
  766. "scl-clk-names", i,
  767. (const char **)&scl_clk_str);
  768. if (rc || !scl_clk_str) {
  769. CAM_WARN(CAM_UTIL, "scl_clk_str is NULL");
  770. soc_info->scl_clk_idx[i] = -1;
  771. continue;
  772. }
  773. for (j = 0; j < soc_info->num_clk; j++) {
  774. if (strnstr(scl_clk_str, soc_info->clk_name[j],
  775. strlen(scl_clk_str))) {
  776. soc_info->scl_clk_idx[i] = j;
  777. CAM_DBG(CAM_UTIL,
  778. "scl clock = %s, index = %d",
  779. scl_clk_str, j);
  780. break;
  781. }
  782. }
  783. }
  784. }
  785. rc = of_property_read_string_index(of_node,
  786. "clock-control-debugfs", 0, &clk_control_debugfs);
  787. if (rc || !clk_control_debugfs) {
  788. CAM_DBG(CAM_UTIL, "No clock_control_debugfs property found");
  789. rc = 0;
  790. goto end;
  791. }
  792. if (strcmp("true", clk_control_debugfs) == 0)
  793. soc_info->clk_control_enable = true;
  794. CAM_DBG(CAM_UTIL, "X: dev_name = %s count = %d",
  795. soc_info->dev_name, count);
  796. end:
  797. return rc;
  798. }
  799. int cam_soc_util_set_clk_rate_level(struct cam_hw_soc_info *soc_info,
  800. enum cam_vote_level clk_level)
  801. {
  802. int i, rc = 0;
  803. enum cam_vote_level apply_level;
  804. if ((soc_info->num_clk == 0) ||
  805. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  806. CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
  807. soc_info->num_clk);
  808. return -EINVAL;
  809. }
  810. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
  811. &apply_level);
  812. if (rc)
  813. return rc;
  814. if (soc_info->cam_cx_ipeak_enable)
  815. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
  816. for (i = 0; i < soc_info->num_clk; i++) {
  817. rc = cam_soc_util_set_clk_rate(soc_info->clk[i],
  818. soc_info->clk_name[i],
  819. soc_info->clk_rate[apply_level][i]);
  820. if (rc < 0) {
  821. CAM_DBG(CAM_UTIL,
  822. "dev name = %s clk_name = %s idx = %d\n"
  823. "apply_level = %d",
  824. soc_info->dev_name, soc_info->clk_name[i],
  825. i, apply_level);
  826. if (soc_info->cam_cx_ipeak_enable)
  827. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  828. break;
  829. }
  830. }
  831. return rc;
  832. };
  833. static int cam_soc_util_get_dt_gpio_req_tbl(struct device_node *of_node,
  834. struct cam_soc_gpio_data *gconf, uint16_t *gpio_array,
  835. uint16_t gpio_array_size)
  836. {
  837. int32_t rc = 0, i = 0;
  838. uint32_t count = 0;
  839. uint32_t *val_array = NULL;
  840. if (!of_get_property(of_node, "gpio-req-tbl-num", &count))
  841. return 0;
  842. count /= sizeof(uint32_t);
  843. if (!count) {
  844. CAM_ERR(CAM_UTIL, "gpio-req-tbl-num 0");
  845. return 0;
  846. }
  847. val_array = kcalloc(count, sizeof(uint32_t), GFP_KERNEL);
  848. if (!val_array)
  849. return -ENOMEM;
  850. gconf->cam_gpio_req_tbl = kcalloc(count, sizeof(struct gpio),
  851. GFP_KERNEL);
  852. if (!gconf->cam_gpio_req_tbl) {
  853. rc = -ENOMEM;
  854. goto free_val_array;
  855. }
  856. gconf->cam_gpio_req_tbl_size = count;
  857. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-num",
  858. val_array, count);
  859. if (rc) {
  860. CAM_ERR(CAM_UTIL, "failed in reading gpio-req-tbl-num, rc = %d",
  861. rc);
  862. goto free_gpio_req_tbl;
  863. }
  864. for (i = 0; i < count; i++) {
  865. if (val_array[i] >= gpio_array_size) {
  866. CAM_ERR(CAM_UTIL, "gpio req tbl index %d invalid",
  867. val_array[i]);
  868. goto free_gpio_req_tbl;
  869. }
  870. gconf->cam_gpio_req_tbl[i].gpio = gpio_array[val_array[i]];
  871. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].gpio = %d", i,
  872. gconf->cam_gpio_req_tbl[i].gpio);
  873. }
  874. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-flags",
  875. val_array, count);
  876. if (rc) {
  877. CAM_ERR(CAM_UTIL, "Failed in gpio-req-tbl-flags, rc %d", rc);
  878. goto free_gpio_req_tbl;
  879. }
  880. for (i = 0; i < count; i++) {
  881. gconf->cam_gpio_req_tbl[i].flags = val_array[i];
  882. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].flags = %ld", i,
  883. gconf->cam_gpio_req_tbl[i].flags);
  884. }
  885. for (i = 0; i < count; i++) {
  886. rc = of_property_read_string_index(of_node,
  887. "gpio-req-tbl-label", i,
  888. &gconf->cam_gpio_req_tbl[i].label);
  889. if (rc) {
  890. CAM_ERR(CAM_UTIL, "Failed rc %d", rc);
  891. goto free_gpio_req_tbl;
  892. }
  893. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].label = %s", i,
  894. gconf->cam_gpio_req_tbl[i].label);
  895. }
  896. kfree(val_array);
  897. return rc;
  898. free_gpio_req_tbl:
  899. kfree(gconf->cam_gpio_req_tbl);
  900. free_val_array:
  901. kfree(val_array);
  902. gconf->cam_gpio_req_tbl_size = 0;
  903. return rc;
  904. }
  905. static int cam_soc_util_get_gpio_info(struct cam_hw_soc_info *soc_info)
  906. {
  907. int32_t rc = 0, i = 0;
  908. uint16_t *gpio_array = NULL;
  909. int16_t gpio_array_size = 0;
  910. struct cam_soc_gpio_data *gconf = NULL;
  911. struct device_node *of_node = NULL;
  912. if (!soc_info || !soc_info->dev)
  913. return -EINVAL;
  914. of_node = soc_info->dev->of_node;
  915. /* Validate input parameters */
  916. if (!of_node) {
  917. CAM_ERR(CAM_UTIL, "Invalid param of_node");
  918. return -EINVAL;
  919. }
  920. gpio_array_size = of_gpio_count(of_node);
  921. if (gpio_array_size <= 0)
  922. return 0;
  923. CAM_DBG(CAM_UTIL, "gpio count %d", gpio_array_size);
  924. gpio_array = kcalloc(gpio_array_size, sizeof(uint16_t), GFP_KERNEL);
  925. if (!gpio_array)
  926. goto free_gpio_conf;
  927. for (i = 0; i < gpio_array_size; i++) {
  928. gpio_array[i] = of_get_gpio(of_node, i);
  929. CAM_DBG(CAM_UTIL, "gpio_array[%d] = %d", i, gpio_array[i]);
  930. }
  931. gconf = kzalloc(sizeof(*gconf), GFP_KERNEL);
  932. if (!gconf)
  933. return -ENOMEM;
  934. rc = cam_soc_util_get_dt_gpio_req_tbl(of_node, gconf, gpio_array,
  935. gpio_array_size);
  936. if (rc) {
  937. CAM_ERR(CAM_UTIL, "failed in msm_camera_get_dt_gpio_req_tbl");
  938. goto free_gpio_array;
  939. }
  940. gconf->cam_gpio_common_tbl = kcalloc(gpio_array_size,
  941. sizeof(struct gpio), GFP_KERNEL);
  942. if (!gconf->cam_gpio_common_tbl) {
  943. rc = -ENOMEM;
  944. goto free_gpio_array;
  945. }
  946. for (i = 0; i < gpio_array_size; i++)
  947. gconf->cam_gpio_common_tbl[i].gpio = gpio_array[i];
  948. gconf->cam_gpio_common_tbl_size = gpio_array_size;
  949. soc_info->gpio_data = gconf;
  950. kfree(gpio_array);
  951. return rc;
  952. free_gpio_array:
  953. kfree(gpio_array);
  954. free_gpio_conf:
  955. kfree(gconf);
  956. soc_info->gpio_data = NULL;
  957. return rc;
  958. }
  959. static int cam_soc_util_request_gpio_table(
  960. struct cam_hw_soc_info *soc_info, bool gpio_en)
  961. {
  962. int rc = 0, i = 0;
  963. uint8_t size = 0;
  964. struct cam_soc_gpio_data *gpio_conf =
  965. soc_info->gpio_data;
  966. struct gpio *gpio_tbl = NULL;
  967. if (!gpio_conf) {
  968. CAM_DBG(CAM_UTIL, "No GPIO entry");
  969. return 0;
  970. }
  971. if (gpio_conf->cam_gpio_common_tbl_size <= 0) {
  972. CAM_ERR(CAM_UTIL, "GPIO table size is invalid");
  973. return -EINVAL;
  974. }
  975. size = gpio_conf->cam_gpio_req_tbl_size;
  976. gpio_tbl = gpio_conf->cam_gpio_req_tbl;
  977. if (!gpio_tbl || !size) {
  978. CAM_ERR(CAM_UTIL, "Invalid gpio_tbl %pK / size %d",
  979. gpio_tbl, size);
  980. return -EINVAL;
  981. }
  982. for (i = 0; i < size; i++) {
  983. CAM_DBG(CAM_UTIL, "i=%d, gpio=%d dir=%ld", i,
  984. gpio_tbl[i].gpio, gpio_tbl[i].flags);
  985. }
  986. if (gpio_en) {
  987. for (i = 0; i < size; i++) {
  988. rc = gpio_request_one(gpio_tbl[i].gpio,
  989. gpio_tbl[i].flags, gpio_tbl[i].label);
  990. if (rc) {
  991. /*
  992. * After GPIO request fails, contine to
  993. * apply new gpios, outout a error message
  994. * for driver bringup debug
  995. */
  996. CAM_ERR(CAM_UTIL, "gpio %d:%s request fails",
  997. gpio_tbl[i].gpio, gpio_tbl[i].label);
  998. }
  999. }
  1000. } else {
  1001. gpio_free_array(gpio_tbl, size);
  1002. }
  1003. return rc;
  1004. }
  1005. static int cam_soc_util_get_dt_regulator_info
  1006. (struct cam_hw_soc_info *soc_info)
  1007. {
  1008. int rc = 0, count = 0, i = 0;
  1009. struct device_node *of_node = NULL;
  1010. if (!soc_info || !soc_info->dev) {
  1011. CAM_ERR(CAM_UTIL, "Invalid parameters");
  1012. return -EINVAL;
  1013. }
  1014. of_node = soc_info->dev->of_node;
  1015. soc_info->num_rgltr = 0;
  1016. count = of_property_count_strings(of_node, "regulator-names");
  1017. if (count != -EINVAL) {
  1018. if (count <= 0) {
  1019. CAM_ERR(CAM_UTIL, "no regulators found");
  1020. count = 0;
  1021. return -EINVAL;
  1022. }
  1023. soc_info->num_rgltr = count;
  1024. } else {
  1025. CAM_DBG(CAM_UTIL, "No regulators node found");
  1026. return 0;
  1027. }
  1028. for (i = 0; i < soc_info->num_rgltr; i++) {
  1029. rc = of_property_read_string_index(of_node,
  1030. "regulator-names", i, &soc_info->rgltr_name[i]);
  1031. CAM_DBG(CAM_UTIL, "rgltr_name[%d] = %s",
  1032. i, soc_info->rgltr_name[i]);
  1033. if (rc) {
  1034. CAM_ERR(CAM_UTIL, "no regulator resource at cnt=%d", i);
  1035. return -ENODEV;
  1036. }
  1037. }
  1038. if (!of_property_read_bool(of_node, "rgltr-cntrl-support")) {
  1039. CAM_DBG(CAM_UTIL, "No regulator control parameter defined");
  1040. soc_info->rgltr_ctrl_support = false;
  1041. return 0;
  1042. }
  1043. soc_info->rgltr_ctrl_support = true;
  1044. rc = of_property_read_u32_array(of_node, "rgltr-min-voltage",
  1045. soc_info->rgltr_min_volt, soc_info->num_rgltr);
  1046. if (rc) {
  1047. CAM_ERR(CAM_UTIL, "No minimum volatage value found, rc=%d", rc);
  1048. return -EINVAL;
  1049. }
  1050. rc = of_property_read_u32_array(of_node, "rgltr-max-voltage",
  1051. soc_info->rgltr_max_volt, soc_info->num_rgltr);
  1052. if (rc) {
  1053. CAM_ERR(CAM_UTIL, "No maximum volatage value found, rc=%d", rc);
  1054. return -EINVAL;
  1055. }
  1056. rc = of_property_read_u32_array(of_node, "rgltr-load-current",
  1057. soc_info->rgltr_op_mode, soc_info->num_rgltr);
  1058. if (rc) {
  1059. CAM_ERR(CAM_UTIL, "No Load curent found rc=%d", rc);
  1060. return -EINVAL;
  1061. }
  1062. return rc;
  1063. }
  1064. int cam_soc_util_get_dt_properties(struct cam_hw_soc_info *soc_info)
  1065. {
  1066. struct device_node *of_node = NULL;
  1067. int count = 0, i = 0, rc = 0;
  1068. if (!soc_info || !soc_info->dev)
  1069. return -EINVAL;
  1070. of_node = soc_info->dev->of_node;
  1071. rc = of_property_read_u32(of_node, "cell-index", &soc_info->index);
  1072. if (rc) {
  1073. CAM_ERR(CAM_UTIL, "device %s failed to read cell-index",
  1074. soc_info->dev_name);
  1075. return rc;
  1076. }
  1077. count = of_property_count_strings(of_node, "reg-names");
  1078. if (count <= 0) {
  1079. CAM_DBG(CAM_UTIL, "no reg-names found for: %s",
  1080. soc_info->dev_name);
  1081. count = 0;
  1082. }
  1083. soc_info->num_mem_block = count;
  1084. for (i = 0; i < soc_info->num_mem_block; i++) {
  1085. rc = of_property_read_string_index(of_node, "reg-names", i,
  1086. &soc_info->mem_block_name[i]);
  1087. if (rc) {
  1088. CAM_ERR(CAM_UTIL, "failed to read reg-names at %d", i);
  1089. return rc;
  1090. }
  1091. soc_info->mem_block[i] =
  1092. platform_get_resource_byname(soc_info->pdev,
  1093. IORESOURCE_MEM, soc_info->mem_block_name[i]);
  1094. if (!soc_info->mem_block[i]) {
  1095. CAM_ERR(CAM_UTIL, "no mem resource by name %s",
  1096. soc_info->mem_block_name[i]);
  1097. rc = -ENODEV;
  1098. return rc;
  1099. }
  1100. }
  1101. if (soc_info->num_mem_block > 0) {
  1102. rc = of_property_read_u32_array(of_node, "reg-cam-base",
  1103. soc_info->mem_block_cam_base, soc_info->num_mem_block);
  1104. if (rc) {
  1105. CAM_ERR(CAM_UTIL, "Error reading register offsets");
  1106. return rc;
  1107. }
  1108. }
  1109. rc = of_property_read_string_index(of_node, "interrupt-names", 0,
  1110. &soc_info->irq_name);
  1111. if (rc) {
  1112. CAM_DBG(CAM_UTIL, "No interrupt line preset for: %s",
  1113. soc_info->dev_name);
  1114. rc = 0;
  1115. } else {
  1116. soc_info->irq_line =
  1117. platform_get_resource_byname(soc_info->pdev,
  1118. IORESOURCE_IRQ, soc_info->irq_name);
  1119. if (!soc_info->irq_line) {
  1120. CAM_ERR(CAM_UTIL, "no irq resource");
  1121. rc = -ENODEV;
  1122. return rc;
  1123. }
  1124. }
  1125. rc = of_property_read_string_index(of_node, "compatible", 0,
  1126. (const char **)&soc_info->compatible);
  1127. if (rc) {
  1128. CAM_DBG(CAM_UTIL, "No compatible string present for: %s",
  1129. soc_info->dev_name);
  1130. rc = 0;
  1131. }
  1132. rc = cam_soc_util_get_dt_regulator_info(soc_info);
  1133. if (rc)
  1134. return rc;
  1135. rc = cam_soc_util_get_dt_clk_info(soc_info);
  1136. if (rc)
  1137. return rc;
  1138. rc = cam_soc_util_get_gpio_info(soc_info);
  1139. if (rc)
  1140. return rc;
  1141. if (of_find_property(of_node, "qcom,cam-cx-ipeak", NULL))
  1142. rc = cam_cx_ipeak_register_cx_ipeak(soc_info);
  1143. return rc;
  1144. }
  1145. /**
  1146. * cam_soc_util_get_regulator()
  1147. *
  1148. * @brief: Get regulator resource named vdd
  1149. *
  1150. * @dev: Device associated with regulator
  1151. * @reg: Return pointer to be filled with regulator on success
  1152. * @rgltr_name: Name of regulator to get
  1153. *
  1154. * @return: 0 for Success, negative value for failure
  1155. */
  1156. static int cam_soc_util_get_regulator(struct device *dev,
  1157. struct regulator **reg, const char *rgltr_name)
  1158. {
  1159. int rc = 0;
  1160. *reg = regulator_get(dev, rgltr_name);
  1161. if (IS_ERR_OR_NULL(*reg)) {
  1162. rc = PTR_ERR(*reg);
  1163. rc = rc ? rc : -EINVAL;
  1164. CAM_ERR(CAM_UTIL, "Regulator %s get failed %d", rgltr_name, rc);
  1165. *reg = NULL;
  1166. }
  1167. return rc;
  1168. }
  1169. int cam_soc_util_regulator_disable(struct regulator *rgltr,
  1170. const char *rgltr_name, uint32_t rgltr_min_volt,
  1171. uint32_t rgltr_max_volt, uint32_t rgltr_op_mode,
  1172. uint32_t rgltr_delay_ms)
  1173. {
  1174. int32_t rc = 0;
  1175. if (!rgltr) {
  1176. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  1177. return -EINVAL;
  1178. }
  1179. rc = regulator_disable(rgltr);
  1180. if (rc) {
  1181. CAM_ERR(CAM_UTIL, "%s regulator disable failed", rgltr_name);
  1182. return rc;
  1183. }
  1184. if (rgltr_delay_ms > 20)
  1185. msleep(rgltr_delay_ms);
  1186. else if (rgltr_delay_ms)
  1187. usleep_range(rgltr_delay_ms * 1000,
  1188. (rgltr_delay_ms * 1000) + 1000);
  1189. if (regulator_count_voltages(rgltr) > 0) {
  1190. regulator_set_load(rgltr, 0);
  1191. regulator_set_voltage(rgltr, 0, rgltr_max_volt);
  1192. }
  1193. return rc;
  1194. }
  1195. int cam_soc_util_regulator_enable(struct regulator *rgltr,
  1196. const char *rgltr_name,
  1197. uint32_t rgltr_min_volt, uint32_t rgltr_max_volt,
  1198. uint32_t rgltr_op_mode, uint32_t rgltr_delay)
  1199. {
  1200. int32_t rc = 0;
  1201. if (!rgltr) {
  1202. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  1203. return -EINVAL;
  1204. }
  1205. if (regulator_count_voltages(rgltr) > 0) {
  1206. CAM_DBG(CAM_UTIL, "voltage min=%d, max=%d",
  1207. rgltr_min_volt, rgltr_max_volt);
  1208. rc = regulator_set_voltage(
  1209. rgltr, rgltr_min_volt, rgltr_max_volt);
  1210. if (rc) {
  1211. CAM_ERR(CAM_UTIL, "%s set voltage failed", rgltr_name);
  1212. return rc;
  1213. }
  1214. rc = regulator_set_load(rgltr, rgltr_op_mode);
  1215. if (rc) {
  1216. CAM_ERR(CAM_UTIL, "%s set optimum mode failed",
  1217. rgltr_name);
  1218. return rc;
  1219. }
  1220. }
  1221. rc = regulator_enable(rgltr);
  1222. if (rc) {
  1223. CAM_ERR(CAM_UTIL, "%s regulator_enable failed", rgltr_name);
  1224. return rc;
  1225. }
  1226. if (rgltr_delay > 20)
  1227. msleep(rgltr_delay);
  1228. else if (rgltr_delay)
  1229. usleep_range(rgltr_delay * 1000,
  1230. (rgltr_delay * 1000) + 1000);
  1231. return rc;
  1232. }
  1233. static int cam_soc_util_request_pinctrl(
  1234. struct cam_hw_soc_info *soc_info)
  1235. {
  1236. struct cam_soc_pinctrl_info *device_pctrl = &soc_info->pinctrl_info;
  1237. struct device *dev = soc_info->dev;
  1238. device_pctrl->pinctrl = devm_pinctrl_get(dev);
  1239. if (IS_ERR_OR_NULL(device_pctrl->pinctrl)) {
  1240. CAM_DBG(CAM_UTIL, "Pinctrl not available");
  1241. device_pctrl->pinctrl = NULL;
  1242. return 0;
  1243. }
  1244. device_pctrl->gpio_state_active =
  1245. pinctrl_lookup_state(device_pctrl->pinctrl,
  1246. CAM_SOC_PINCTRL_STATE_DEFAULT);
  1247. if (IS_ERR_OR_NULL(device_pctrl->gpio_state_active)) {
  1248. CAM_ERR(CAM_UTIL,
  1249. "Failed to get the active state pinctrl handle");
  1250. device_pctrl->gpio_state_active = NULL;
  1251. return -EINVAL;
  1252. }
  1253. device_pctrl->gpio_state_suspend
  1254. = pinctrl_lookup_state(device_pctrl->pinctrl,
  1255. CAM_SOC_PINCTRL_STATE_SLEEP);
  1256. if (IS_ERR_OR_NULL(device_pctrl->gpio_state_suspend)) {
  1257. CAM_ERR(CAM_UTIL,
  1258. "Failed to get the suspend state pinctrl handle");
  1259. device_pctrl->gpio_state_suspend = NULL;
  1260. return -EINVAL;
  1261. }
  1262. return 0;
  1263. }
  1264. static void cam_soc_util_regulator_disable_default(
  1265. struct cam_hw_soc_info *soc_info)
  1266. {
  1267. int j = 0;
  1268. uint32_t num_rgltr = soc_info->num_rgltr;
  1269. for (j = num_rgltr-1; j >= 0; j--) {
  1270. if (soc_info->rgltr_ctrl_support == true) {
  1271. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  1272. soc_info->rgltr_name[j],
  1273. soc_info->rgltr_min_volt[j],
  1274. soc_info->rgltr_max_volt[j],
  1275. soc_info->rgltr_op_mode[j],
  1276. soc_info->rgltr_delay[j]);
  1277. } else {
  1278. if (soc_info->rgltr[j])
  1279. regulator_disable(soc_info->rgltr[j]);
  1280. }
  1281. }
  1282. }
  1283. static int cam_soc_util_regulator_enable_default(
  1284. struct cam_hw_soc_info *soc_info)
  1285. {
  1286. int j = 0, rc = 0;
  1287. uint32_t num_rgltr = soc_info->num_rgltr;
  1288. for (j = 0; j < num_rgltr; j++) {
  1289. if (soc_info->rgltr_ctrl_support == true) {
  1290. rc = cam_soc_util_regulator_enable(soc_info->rgltr[j],
  1291. soc_info->rgltr_name[j],
  1292. soc_info->rgltr_min_volt[j],
  1293. soc_info->rgltr_max_volt[j],
  1294. soc_info->rgltr_op_mode[j],
  1295. soc_info->rgltr_delay[j]);
  1296. } else {
  1297. if (soc_info->rgltr[j])
  1298. rc = regulator_enable(soc_info->rgltr[j]);
  1299. }
  1300. if (rc) {
  1301. CAM_ERR(CAM_UTIL, "%s enable failed",
  1302. soc_info->rgltr_name[j]);
  1303. goto disable_rgltr;
  1304. }
  1305. }
  1306. return rc;
  1307. disable_rgltr:
  1308. for (j--; j >= 0; j--) {
  1309. if (soc_info->rgltr_ctrl_support == true) {
  1310. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  1311. soc_info->rgltr_name[j],
  1312. soc_info->rgltr_min_volt[j],
  1313. soc_info->rgltr_max_volt[j],
  1314. soc_info->rgltr_op_mode[j],
  1315. soc_info->rgltr_delay[j]);
  1316. } else {
  1317. if (soc_info->rgltr[j])
  1318. regulator_disable(soc_info->rgltr[j]);
  1319. }
  1320. }
  1321. return rc;
  1322. }
  1323. int cam_soc_util_request_platform_resource(
  1324. struct cam_hw_soc_info *soc_info,
  1325. irq_handler_t handler, void *irq_data)
  1326. {
  1327. int i = 0, rc = 0;
  1328. if (!soc_info || !soc_info->dev) {
  1329. CAM_ERR(CAM_UTIL, "Invalid parameters");
  1330. return -EINVAL;
  1331. }
  1332. for (i = 0; i < soc_info->num_mem_block; i++) {
  1333. if (soc_info->reserve_mem) {
  1334. if (!request_mem_region(soc_info->mem_block[i]->start,
  1335. resource_size(soc_info->mem_block[i]),
  1336. soc_info->mem_block_name[i])){
  1337. CAM_ERR(CAM_UTIL,
  1338. "Error Mem region request Failed:%s",
  1339. soc_info->mem_block_name[i]);
  1340. rc = -ENOMEM;
  1341. goto unmap_base;
  1342. }
  1343. }
  1344. soc_info->reg_map[i].mem_base = ioremap(
  1345. soc_info->mem_block[i]->start,
  1346. resource_size(soc_info->mem_block[i]));
  1347. if (!soc_info->reg_map[i].mem_base) {
  1348. CAM_ERR(CAM_UTIL, "i= %d base NULL", i);
  1349. rc = -ENOMEM;
  1350. goto unmap_base;
  1351. }
  1352. soc_info->reg_map[i].mem_cam_base =
  1353. soc_info->mem_block_cam_base[i];
  1354. soc_info->reg_map[i].size =
  1355. resource_size(soc_info->mem_block[i]);
  1356. soc_info->num_reg_map++;
  1357. }
  1358. for (i = 0; i < soc_info->num_rgltr; i++) {
  1359. if (soc_info->rgltr_name[i] == NULL) {
  1360. CAM_ERR(CAM_UTIL, "can't find regulator name");
  1361. goto put_regulator;
  1362. }
  1363. rc = cam_soc_util_get_regulator(soc_info->dev,
  1364. &soc_info->rgltr[i],
  1365. soc_info->rgltr_name[i]);
  1366. if (rc)
  1367. goto put_regulator;
  1368. }
  1369. if (soc_info->irq_line) {
  1370. rc = devm_request_irq(soc_info->dev, soc_info->irq_line->start,
  1371. handler, IRQF_TRIGGER_RISING,
  1372. soc_info->irq_name, irq_data);
  1373. if (rc) {
  1374. CAM_ERR(CAM_UTIL, "irq request fail");
  1375. rc = -EBUSY;
  1376. goto put_regulator;
  1377. }
  1378. disable_irq(soc_info->irq_line->start);
  1379. soc_info->irq_data = irq_data;
  1380. }
  1381. /* Get Clock */
  1382. for (i = 0; i < soc_info->num_clk; i++) {
  1383. soc_info->clk[i] = clk_get(soc_info->dev,
  1384. soc_info->clk_name[i]);
  1385. if (!soc_info->clk[i]) {
  1386. CAM_ERR(CAM_UTIL, "get failed for %s",
  1387. soc_info->clk_name[i]);
  1388. rc = -ENOENT;
  1389. goto put_clk;
  1390. }
  1391. }
  1392. rc = cam_soc_util_request_pinctrl(soc_info);
  1393. if (rc)
  1394. CAM_DBG(CAM_UTIL, "Failed in request pinctrl, rc=%d", rc);
  1395. rc = cam_soc_util_request_gpio_table(soc_info, true);
  1396. if (rc) {
  1397. CAM_ERR(CAM_UTIL, "Failed in request gpio table, rc=%d", rc);
  1398. goto put_clk;
  1399. }
  1400. if (soc_info->clk_control_enable)
  1401. cam_soc_util_create_clk_lvl_debugfs(soc_info);
  1402. return rc;
  1403. put_clk:
  1404. if (i == -1)
  1405. i = soc_info->num_clk;
  1406. for (i = i - 1; i >= 0; i--) {
  1407. if (soc_info->clk[i]) {
  1408. clk_put(soc_info->clk[i]);
  1409. soc_info->clk[i] = NULL;
  1410. }
  1411. }
  1412. if (soc_info->irq_line) {
  1413. disable_irq(soc_info->irq_line->start);
  1414. devm_free_irq(soc_info->dev,
  1415. soc_info->irq_line->start, irq_data);
  1416. }
  1417. put_regulator:
  1418. if (i == -1)
  1419. i = soc_info->num_rgltr;
  1420. for (i = i - 1; i >= 0; i--) {
  1421. if (soc_info->rgltr[i]) {
  1422. regulator_disable(soc_info->rgltr[i]);
  1423. regulator_put(soc_info->rgltr[i]);
  1424. soc_info->rgltr[i] = NULL;
  1425. }
  1426. }
  1427. unmap_base:
  1428. if (i == -1)
  1429. i = soc_info->num_reg_map;
  1430. for (i = i - 1; i >= 0; i--) {
  1431. if (soc_info->reserve_mem)
  1432. release_mem_region(soc_info->mem_block[i]->start,
  1433. resource_size(soc_info->mem_block[i]));
  1434. iounmap(soc_info->reg_map[i].mem_base);
  1435. soc_info->reg_map[i].mem_base = NULL;
  1436. soc_info->reg_map[i].size = 0;
  1437. }
  1438. return rc;
  1439. }
  1440. int cam_soc_util_release_platform_resource(struct cam_hw_soc_info *soc_info)
  1441. {
  1442. int i;
  1443. if (!soc_info || !soc_info->dev) {
  1444. CAM_ERR(CAM_UTIL, "Invalid parameter");
  1445. return -EINVAL;
  1446. }
  1447. for (i = soc_info->num_clk - 1; i >= 0; i--) {
  1448. clk_put(soc_info->clk[i]);
  1449. soc_info->clk[i] = NULL;
  1450. }
  1451. for (i = soc_info->num_rgltr - 1; i >= 0; i--) {
  1452. if (soc_info->rgltr[i]) {
  1453. regulator_put(soc_info->rgltr[i]);
  1454. soc_info->rgltr[i] = NULL;
  1455. }
  1456. }
  1457. for (i = soc_info->num_reg_map - 1; i >= 0; i--) {
  1458. iounmap(soc_info->reg_map[i].mem_base);
  1459. soc_info->reg_map[i].mem_base = NULL;
  1460. soc_info->reg_map[i].size = 0;
  1461. }
  1462. if (soc_info->irq_line) {
  1463. disable_irq(soc_info->irq_line->start);
  1464. devm_free_irq(soc_info->dev,
  1465. soc_info->irq_line->start, soc_info->irq_data);
  1466. }
  1467. if (soc_info->pinctrl_info.pinctrl)
  1468. devm_pinctrl_put(soc_info->pinctrl_info.pinctrl);
  1469. /* release for gpio */
  1470. cam_soc_util_request_gpio_table(soc_info, false);
  1471. if (soc_info->clk_control_enable)
  1472. cam_soc_util_remove_clk_lvl_debugfs(soc_info);
  1473. return 0;
  1474. }
  1475. int cam_soc_util_enable_platform_resource(struct cam_hw_soc_info *soc_info,
  1476. bool enable_clocks, enum cam_vote_level clk_level, bool enable_irq)
  1477. {
  1478. int rc = 0;
  1479. if (!soc_info)
  1480. return -EINVAL;
  1481. rc = cam_soc_util_regulator_enable_default(soc_info);
  1482. if (rc) {
  1483. CAM_ERR(CAM_UTIL, "Regulators enable failed");
  1484. return rc;
  1485. }
  1486. if (enable_clocks) {
  1487. rc = cam_soc_util_clk_enable_default(soc_info, clk_level);
  1488. if (rc)
  1489. goto disable_regulator;
  1490. }
  1491. if (enable_irq) {
  1492. rc = cam_soc_util_irq_enable(soc_info);
  1493. if (rc)
  1494. goto disable_clk;
  1495. }
  1496. if (soc_info->pinctrl_info.pinctrl &&
  1497. soc_info->pinctrl_info.gpio_state_active) {
  1498. rc = pinctrl_select_state(soc_info->pinctrl_info.pinctrl,
  1499. soc_info->pinctrl_info.gpio_state_active);
  1500. if (rc)
  1501. goto disable_irq;
  1502. }
  1503. return rc;
  1504. disable_irq:
  1505. if (enable_irq)
  1506. cam_soc_util_irq_disable(soc_info);
  1507. disable_clk:
  1508. if (enable_clocks)
  1509. cam_soc_util_clk_disable_default(soc_info);
  1510. disable_regulator:
  1511. cam_soc_util_regulator_disable_default(soc_info);
  1512. return rc;
  1513. }
  1514. int cam_soc_util_disable_platform_resource(struct cam_hw_soc_info *soc_info,
  1515. bool disable_clocks, bool disable_irq)
  1516. {
  1517. int rc = 0;
  1518. if (!soc_info)
  1519. return -EINVAL;
  1520. if (disable_irq)
  1521. rc |= cam_soc_util_irq_disable(soc_info);
  1522. if (disable_clocks)
  1523. cam_soc_util_clk_disable_default(soc_info);
  1524. cam_soc_util_regulator_disable_default(soc_info);
  1525. if (soc_info->pinctrl_info.pinctrl &&
  1526. soc_info->pinctrl_info.gpio_state_suspend)
  1527. rc = pinctrl_select_state(soc_info->pinctrl_info.pinctrl,
  1528. soc_info->pinctrl_info.gpio_state_suspend);
  1529. return rc;
  1530. }
  1531. int cam_soc_util_reg_dump(struct cam_hw_soc_info *soc_info,
  1532. uint32_t base_index, uint32_t offset, int size)
  1533. {
  1534. void __iomem *base_addr = NULL;
  1535. CAM_DBG(CAM_UTIL, "base_idx %u size=%d", base_index, size);
  1536. if (!soc_info || base_index >= soc_info->num_reg_map ||
  1537. size <= 0 || (offset + size) >=
  1538. CAM_SOC_GET_REG_MAP_SIZE(soc_info, base_index))
  1539. return -EINVAL;
  1540. base_addr = CAM_SOC_GET_REG_MAP_START(soc_info, base_index);
  1541. /*
  1542. * All error checking already done above,
  1543. * hence ignoring the return value below.
  1544. */
  1545. cam_io_dump(base_addr, offset, size);
  1546. return 0;
  1547. }