cam_soc_util.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/of.h>
  6. #include <linux/clk.h>
  7. #include <linux/slab.h>
  8. #include <linux/gpio.h>
  9. #include <linux/of_gpio.h>
  10. #include "cam_soc_util.h"
  11. #include "cam_debug_util.h"
  12. #include "cam_cx_ipeak.h"
  13. #include "cam_mem_mgr.h"
  14. #include "cam_presil_hw_access.h"
  15. #define CAM_TO_MASK(bitn) (1 << (int)(bitn))
  16. #define CAM_IS_BIT_SET(mask, bit) ((mask) & CAM_TO_MASK(bit))
  17. #define CAM_SET_BIT(mask, bit) ((mask) |= CAM_TO_MASK(bit))
  18. #define CAM_CLEAR_BIT(mask, bit) ((mask) &= ~CAM_TO_MASK(bit))
  19. #define CAM_SS_START_PRESIL 0x08c00000
  20. #define CAM_SS_START 0x0ac00000
  21. #define CAM_CLK_DIRNAME "clk"
  22. static uint skip_mmrm_set_rate;
  23. module_param(skip_mmrm_set_rate, uint, 0644);
  24. /**
  25. * struct cam_clk_wrapper_clk: This represents an entry corresponding to a
  26. * shared clock in Clk wrapper. Clients that share
  27. * the same clock are registered to this clk entry
  28. * and set rate from them is consolidated before
  29. * setting it to clk driver.
  30. *
  31. * @list: List pointer to point to next shared clk entry
  32. * @clk_id: Clk Id of this clock
  33. * @curr_clk_rate: Current clock rate set for this clock
  34. * @client_list: List of clients registered to this shared clock entry
  35. * @num_clients: Number of registered clients
  36. * @active_clients: Number of active clients
  37. * @mmrm_client: MMRM Client handle for src clock
  38. * @soc_info: soc_info of client with which mmrm handle is created.
  39. * This is used as unique identifier for a client and mmrm
  40. * callback data. When client corresponds to this soc_info is
  41. * unregistered, need to unregister mmrm handle as well.
  42. * @is_nrt_dev: Whether this clock corresponds to NRT device
  43. * @min_clk_rate: Minimum clk rate that this clock supports
  44. **/
  45. struct cam_clk_wrapper_clk {
  46. struct list_head list;
  47. uint32_t clk_id;
  48. int64_t curr_clk_rate;
  49. struct list_head client_list;
  50. uint32_t num_clients;
  51. uint32_t active_clients;
  52. void *mmrm_handle;
  53. struct cam_hw_soc_info *soc_info;
  54. bool is_nrt_dev;
  55. int64_t min_clk_rate;
  56. };
  57. /**
  58. * struct cam_clk_wrapper_client: This represents a client (device) that wants
  59. * to share the clock with some other client.
  60. *
  61. * @list: List pointer to point to next client that share the
  62. * same clock
  63. * @soc_info: soc_info of client. This is used as unique identifier
  64. * for a client
  65. * @clk: Clk handle
  66. * @curr_clk_rate: Current clock rate set for this client
  67. **/
  68. struct cam_clk_wrapper_client {
  69. struct list_head list;
  70. struct cam_hw_soc_info *soc_info;
  71. struct clk *clk;
  72. int64_t curr_clk_rate;
  73. };
  74. static char supported_clk_info[256];
  75. static DEFINE_MUTEX(wrapper_lock);
  76. static LIST_HEAD(wrapper_clk_list);
  77. #if IS_REACHABLE(CONFIG_MSM_MMRM)
  78. bool cam_is_mmrm_supported_on_current_chip(void)
  79. {
  80. bool is_supported;
  81. is_supported = mmrm_client_check_scaling_supported(MMRM_CLIENT_CLOCK,
  82. MMRM_CLIENT_DOMAIN_CAMERA);
  83. CAM_DBG(CAM_UTIL, "is mmrm supported: %s",
  84. CAM_BOOL_TO_YESNO(is_supported));;
  85. return is_supported;
  86. }
  87. int cam_mmrm_notifier_callback(
  88. struct mmrm_client_notifier_data *notifier_data)
  89. {
  90. if (!notifier_data) {
  91. CAM_ERR(CAM_UTIL, "Invalid notifier data");
  92. return -EBADR;
  93. }
  94. if (notifier_data->cb_type == MMRM_CLIENT_RESOURCE_VALUE_CHANGE) {
  95. struct cam_hw_soc_info *soc_info = notifier_data->pvt_data;
  96. CAM_WARN(CAM_UTIL, "Dev %s Clk %s value change from %ld to %ld",
  97. soc_info->dev_name,
  98. (soc_info->src_clk_idx == -1) ? "No src clk" :
  99. soc_info->clk_name[soc_info->src_clk_idx],
  100. notifier_data->cb_data.val_chng.old_val,
  101. notifier_data->cb_data.val_chng.new_val);
  102. }
  103. return 0;
  104. }
  105. int cam_soc_util_register_mmrm_client(
  106. uint32_t clk_id, struct clk *clk, bool is_nrt_dev,
  107. struct cam_hw_soc_info *soc_info, const char *clk_name,
  108. void **mmrm_handle)
  109. {
  110. struct mmrm_client *mmrm_client;
  111. struct mmrm_client_desc desc = { };
  112. if (!mmrm_handle) {
  113. CAM_ERR(CAM_UTIL, "Invalid mmrm input");
  114. return -EINVAL;
  115. }
  116. *mmrm_handle = (void *)NULL;
  117. if (!cam_is_mmrm_supported_on_current_chip())
  118. return 0;
  119. desc.client_type = MMRM_CLIENT_CLOCK;
  120. desc.client_info.desc.client_domain = MMRM_CLIENT_DOMAIN_CAMERA;
  121. desc.client_info.desc.client_id = clk_id;
  122. desc.client_info.desc.clk = clk;
  123. snprintf((char *)desc.client_info.desc.name,
  124. sizeof(desc.client_info.desc.name), "%s_%s",
  125. soc_info->dev_name, clk_name);
  126. desc.priority = is_nrt_dev ?
  127. MMRM_CLIENT_PRIOR_LOW : MMRM_CLIENT_PRIOR_HIGH;
  128. desc.pvt_data = soc_info;
  129. desc.notifier_callback_fn = cam_mmrm_notifier_callback;
  130. mmrm_client = mmrm_client_register(&desc);
  131. if (!mmrm_client) {
  132. CAM_ERR(CAM_UTIL, "MMRM Register failed Dev %s clk %s id %d",
  133. soc_info->dev_name, clk_name, clk_id);
  134. return -EINVAL;
  135. }
  136. CAM_DBG(CAM_UTIL,
  137. "MMRM Register success Dev %s is_nrt_dev %d clk %s id %d handle=%pK",
  138. soc_info->dev_name, is_nrt_dev, clk_name, clk_id, mmrm_client);
  139. *mmrm_handle = (void *)mmrm_client;
  140. return 0;
  141. }
  142. int cam_soc_util_unregister_mmrm_client(
  143. void *mmrm_handle)
  144. {
  145. int rc = 0;
  146. CAM_DBG(CAM_UTIL, "MMRM UnRegister handle=%pK", mmrm_handle);
  147. if (mmrm_handle) {
  148. rc = mmrm_client_deregister((struct mmrm_client *)mmrm_handle);
  149. if (rc)
  150. CAM_ERR(CAM_UTIL,
  151. "Failed in deregister handle=%pK, rc %d",
  152. mmrm_handle, rc);
  153. }
  154. return rc;
  155. }
  156. static int cam_soc_util_set_rate_through_mmrm(
  157. void *mmrm_handle, bool is_nrt_dev, long min_rate,
  158. long req_rate, uint32_t num_hw_blocks)
  159. {
  160. int rc = 0;
  161. struct mmrm_client_data client_data;
  162. struct mmrm_client_res_value val;
  163. client_data.num_hw_blocks = num_hw_blocks;
  164. client_data.flags = 0;
  165. CAM_DBG(CAM_UTIL,
  166. "mmrm=%pK, nrt=%d, min_rate=%ld req_rate %ld, num_blocks=%d",
  167. mmrm_handle, is_nrt_dev, min_rate, req_rate, num_hw_blocks);
  168. if (is_nrt_dev) {
  169. val.min = min_rate;
  170. val.cur = req_rate;
  171. rc = mmrm_client_set_value_in_range(
  172. (struct mmrm_client *)mmrm_handle, &client_data, &val);
  173. } else {
  174. rc = mmrm_client_set_value(
  175. (struct mmrm_client *)mmrm_handle,
  176. &client_data, req_rate);
  177. }
  178. if (rc)
  179. CAM_ERR(CAM_UTIL, "Set rate failed rate %ld rc %d",
  180. req_rate, rc);
  181. return rc;
  182. }
  183. #else
  184. int cam_soc_util_register_mmrm_client(
  185. uint32_t clk_id, struct clk *clk, bool is_nrt_dev,
  186. struct cam_hw_soc_info *soc_info, const char *clk_name,
  187. void **mmrm_handle)
  188. {
  189. if (!mmrm_handle) {
  190. CAM_ERR(CAM_UTIL, "Invalid mmrm input");
  191. return -EINVAL;
  192. }
  193. *mmrm_handle = NULL;
  194. return 0;
  195. }
  196. int cam_soc_util_unregister_mmrm_client(
  197. void *mmrm_handle)
  198. {
  199. return 0;
  200. }
  201. static int cam_soc_util_set_rate_through_mmrm(
  202. void *mmrm_handle, bool is_nrt_dev, long min_rate,
  203. long req_rate, uint32_t num_hw_blocks)
  204. {
  205. return 0;
  206. }
  207. #endif
  208. static int cam_soc_util_clk_wrapper_register_entry(
  209. uint32_t clk_id, struct clk *clk, bool is_src_clk,
  210. struct cam_hw_soc_info *soc_info, int64_t min_clk_rate,
  211. const char *clk_name)
  212. {
  213. struct cam_clk_wrapper_clk *wrapper_clk;
  214. struct cam_clk_wrapper_client *wrapper_client;
  215. bool clock_found = false;
  216. int rc = 0;
  217. mutex_lock(&wrapper_lock);
  218. list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
  219. CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
  220. wrapper_clk->clk_id, wrapper_clk->num_clients);
  221. if (wrapper_clk->clk_id == clk_id) {
  222. clock_found = true;
  223. list_for_each_entry(wrapper_client,
  224. &wrapper_clk->client_list, list) {
  225. CAM_DBG(CAM_UTIL,
  226. "Clk id %d entry client %s",
  227. wrapper_clk->clk_id,
  228. wrapper_client->soc_info->dev_name);
  229. if (wrapper_client->soc_info == soc_info) {
  230. CAM_ERR(CAM_UTIL,
  231. "Register with same soc info, clk id %d, client %s",
  232. clk_id, soc_info->dev_name);
  233. rc = -EINVAL;
  234. goto end;
  235. }
  236. }
  237. break;
  238. }
  239. }
  240. if (!clock_found) {
  241. CAM_DBG(CAM_UTIL, "Adding new entry for clk id %d", clk_id);
  242. wrapper_clk = kzalloc(sizeof(struct cam_clk_wrapper_clk),
  243. GFP_KERNEL);
  244. if (!wrapper_clk) {
  245. CAM_ERR(CAM_UTIL,
  246. "Failed in allocating new clk entry %d",
  247. clk_id);
  248. rc = -ENOMEM;
  249. goto end;
  250. }
  251. wrapper_clk->clk_id = clk_id;
  252. INIT_LIST_HEAD(&wrapper_clk->list);
  253. INIT_LIST_HEAD(&wrapper_clk->client_list);
  254. list_add_tail(&wrapper_clk->list, &wrapper_clk_list);
  255. }
  256. wrapper_client = kzalloc(sizeof(struct cam_clk_wrapper_client),
  257. GFP_KERNEL);
  258. if (!wrapper_client) {
  259. CAM_ERR(CAM_UTIL, "Failed in allocating new client entry %d",
  260. clk_id);
  261. rc = -ENOMEM;
  262. goto end;
  263. }
  264. wrapper_client->soc_info = soc_info;
  265. wrapper_client->clk = clk;
  266. if (is_src_clk && !wrapper_clk->mmrm_handle) {
  267. wrapper_clk->is_nrt_dev = soc_info->is_nrt_dev;
  268. wrapper_clk->min_clk_rate = min_clk_rate;
  269. wrapper_clk->soc_info = soc_info;
  270. rc = cam_soc_util_register_mmrm_client(clk_id, clk,
  271. wrapper_clk->is_nrt_dev, soc_info, clk_name,
  272. &wrapper_clk->mmrm_handle);
  273. if (rc) {
  274. CAM_ERR(CAM_UTIL,
  275. "Failed in register mmrm client Dev %s clk id %d",
  276. soc_info->dev_name, clk_id);
  277. kfree(wrapper_client);
  278. goto end;
  279. }
  280. }
  281. INIT_LIST_HEAD(&wrapper_client->list);
  282. list_add_tail(&wrapper_client->list, &wrapper_clk->client_list);
  283. wrapper_clk->num_clients++;
  284. CAM_DBG(CAM_UTIL,
  285. "Adding new client %s for clk[%s] id %d, num clients %d",
  286. soc_info->dev_name, clk_name, clk_id, wrapper_clk->num_clients);
  287. end:
  288. mutex_unlock(&wrapper_lock);
  289. return rc;
  290. }
  291. static int cam_soc_util_clk_wrapper_unregister_entry(
  292. uint32_t clk_id, struct cam_hw_soc_info *soc_info)
  293. {
  294. struct cam_clk_wrapper_clk *wrapper_clk;
  295. struct cam_clk_wrapper_client *wrapper_client;
  296. bool clock_found = false;
  297. bool client_found = false;
  298. int rc = 0;
  299. mutex_lock(&wrapper_lock);
  300. list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
  301. CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
  302. wrapper_clk->clk_id, wrapper_clk->num_clients);
  303. if (wrapper_clk->clk_id == clk_id) {
  304. clock_found = true;
  305. list_for_each_entry(wrapper_client,
  306. &wrapper_clk->client_list, list) {
  307. CAM_DBG(CAM_UTIL, "Clk id %d entry client %s",
  308. wrapper_clk->clk_id,
  309. wrapper_client->soc_info->dev_name);
  310. if (wrapper_client->soc_info == soc_info) {
  311. client_found = true;
  312. break;
  313. }
  314. }
  315. break;
  316. }
  317. }
  318. if (!clock_found) {
  319. CAM_ERR(CAM_UTIL, "Shared clk id %d entry not found", clk_id);
  320. rc = -EINVAL;
  321. goto end;
  322. }
  323. if (!client_found) {
  324. CAM_ERR(CAM_UTIL,
  325. "Client %pK for Shared clk id %d entry not found",
  326. soc_info, clk_id);
  327. rc = -EINVAL;
  328. goto end;
  329. }
  330. wrapper_clk->num_clients--;
  331. if (wrapper_clk->mmrm_handle && (wrapper_clk->soc_info == soc_info)) {
  332. cam_soc_util_unregister_mmrm_client(wrapper_clk->mmrm_handle);
  333. wrapper_clk->mmrm_handle = NULL;
  334. wrapper_clk->soc_info = NULL;
  335. }
  336. list_del_init(&wrapper_client->list);
  337. kfree(wrapper_client);
  338. CAM_DBG(CAM_UTIL, "Unregister client %s for clk id %d, num clients %d",
  339. soc_info->dev_name, clk_id, wrapper_clk->num_clients);
  340. if (!wrapper_clk->num_clients) {
  341. list_del_init(&wrapper_clk->list);
  342. kfree(wrapper_clk);
  343. }
  344. end:
  345. mutex_unlock(&wrapper_lock);
  346. return rc;
  347. }
  348. static int cam_soc_util_clk_wrapper_set_clk_rate(
  349. uint32_t clk_id, struct cam_hw_soc_info *soc_info,
  350. struct clk *clk, int64_t clk_rate)
  351. {
  352. struct cam_clk_wrapper_clk *wrapper_clk;
  353. struct cam_clk_wrapper_client *wrapper_client;
  354. bool clk_found = false;
  355. bool client_found = false;
  356. int rc = 0;
  357. int64_t final_clk_rate = 0;
  358. uint32_t active_clients = 0;
  359. if (!soc_info || !clk) {
  360. CAM_ERR(CAM_UTIL, "Invalid param soc_info %pK clk %pK",
  361. soc_info, clk);
  362. return -EINVAL;
  363. }
  364. mutex_lock(&wrapper_lock);
  365. list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
  366. CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
  367. wrapper_clk->clk_id, wrapper_clk->num_clients);
  368. if (wrapper_clk->clk_id == clk_id) {
  369. clk_found = true;
  370. break;
  371. }
  372. }
  373. if (!clk_found) {
  374. CAM_ERR(CAM_UTIL, "Clk entry not found id %d client %s",
  375. clk_id, soc_info->dev_name);
  376. rc = -EINVAL;
  377. goto end;
  378. }
  379. list_for_each_entry(wrapper_client, &wrapper_clk->client_list, list) {
  380. CAM_DBG(CAM_UTIL, "Clk id %d client %s, clk rate %lld",
  381. wrapper_clk->clk_id, wrapper_client->soc_info->dev_name,
  382. wrapper_client->curr_clk_rate);
  383. if (wrapper_client->soc_info == soc_info) {
  384. client_found = true;
  385. CAM_DBG(CAM_UTIL,
  386. "Clk enable clk id %d, client %s curr %ld new %ld",
  387. clk_id, wrapper_client->soc_info->dev_name,
  388. wrapper_client->curr_clk_rate, clk_rate);
  389. wrapper_client->curr_clk_rate = clk_rate;
  390. }
  391. if (wrapper_client->curr_clk_rate > 0)
  392. active_clients++;
  393. if (final_clk_rate < wrapper_client->curr_clk_rate)
  394. final_clk_rate = wrapper_client->curr_clk_rate;
  395. }
  396. if (!client_found) {
  397. CAM_ERR(CAM_UTIL,
  398. "Wrapper clk enable without client entry clk id %d client %s",
  399. clk_id, soc_info->dev_name);
  400. rc = -EINVAL;
  401. goto end;
  402. }
  403. CAM_DBG(CAM_UTIL,
  404. "Clk id %d, client %s, clients rate %ld, curr %ld final %ld",
  405. wrapper_clk->clk_id, soc_info->dev_name, clk_rate,
  406. wrapper_clk->curr_clk_rate, final_clk_rate);
  407. if ((final_clk_rate != wrapper_clk->curr_clk_rate) ||
  408. (active_clients != wrapper_clk->active_clients)) {
  409. bool set_rate_finish = false;
  410. if (!skip_mmrm_set_rate && wrapper_clk->mmrm_handle) {
  411. rc = cam_soc_util_set_rate_through_mmrm(
  412. wrapper_clk->mmrm_handle,
  413. wrapper_clk->is_nrt_dev,
  414. wrapper_clk->min_clk_rate,
  415. final_clk_rate, active_clients);
  416. if (rc) {
  417. CAM_ERR(CAM_UTIL,
  418. "set_rate through mmrm failed clk_id %d, rate=%ld",
  419. wrapper_clk->clk_id, final_clk_rate);
  420. goto end;
  421. }
  422. set_rate_finish = true;
  423. }
  424. if (!set_rate_finish && final_clk_rate &&
  425. (final_clk_rate != wrapper_clk->curr_clk_rate)) {
  426. rc = clk_set_rate(clk, final_clk_rate);
  427. if (rc) {
  428. CAM_ERR(CAM_UTIL, "set_rate failed on clk %d",
  429. wrapper_clk->clk_id);
  430. goto end;
  431. }
  432. }
  433. wrapper_clk->curr_clk_rate = final_clk_rate;
  434. wrapper_clk->active_clients = active_clients;
  435. }
  436. end:
  437. mutex_unlock(&wrapper_lock);
  438. return rc;
  439. }
  440. int cam_soc_util_get_clk_level(struct cam_hw_soc_info *soc_info,
  441. int64_t clk_rate, int clk_idx, int32_t *clk_lvl)
  442. {
  443. int i;
  444. long clk_rate_round;
  445. if (!soc_info || (clk_idx < 0) || (clk_idx >= CAM_SOC_MAX_CLK)) {
  446. CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d", clk_idx);
  447. *clk_lvl = -1;
  448. return -EINVAL;
  449. }
  450. clk_rate_round = clk_round_rate(soc_info->clk[clk_idx], clk_rate);
  451. if (clk_rate_round < 0) {
  452. CAM_ERR(CAM_UTIL, "round failed rc = %ld",
  453. clk_rate_round);
  454. *clk_lvl = -1;
  455. return -EINVAL;
  456. }
  457. for (i = 0; i < CAM_MAX_VOTE; i++) {
  458. if ((soc_info->clk_level_valid[i]) &&
  459. (soc_info->clk_rate[i][clk_idx] >=
  460. clk_rate_round)) {
  461. CAM_DBG(CAM_UTIL,
  462. "soc = %d round rate = %ld actual = %lld",
  463. soc_info->clk_rate[i][clk_idx],
  464. clk_rate_round, clk_rate);
  465. *clk_lvl = i;
  466. return 0;
  467. }
  468. }
  469. CAM_WARN(CAM_UTIL, "Invalid clock rate %ld", clk_rate_round);
  470. *clk_lvl = -1;
  471. return -EINVAL;
  472. }
  473. /**
  474. * cam_soc_util_get_string_from_level()
  475. *
  476. * @brief: Returns the string for a given clk level
  477. *
  478. * @level: Clock level
  479. *
  480. * @return: String corresponding to the clk level
  481. */
  482. static const char *cam_soc_util_get_string_from_level(
  483. enum cam_vote_level level)
  484. {
  485. switch (level) {
  486. case CAM_SUSPEND_VOTE:
  487. return "";
  488. case CAM_MINSVS_VOTE:
  489. return "MINSVS[1]";
  490. case CAM_LOWSVS_VOTE:
  491. return "LOWSVS[2]";
  492. case CAM_SVS_VOTE:
  493. return "SVS[3]";
  494. case CAM_SVSL1_VOTE:
  495. return "SVSL1[4]";
  496. case CAM_NOMINAL_VOTE:
  497. return "NOM[5]";
  498. case CAM_NOMINALL1_VOTE:
  499. return "NOML1[6]";
  500. case CAM_TURBO_VOTE:
  501. return "TURBO[7]";
  502. default:
  503. return "";
  504. }
  505. }
  506. /**
  507. * cam_soc_util_get_supported_clk_levels()
  508. *
  509. * @brief: Returns the string of all the supported clk levels for
  510. * the given device
  511. *
  512. * @soc_info: Device soc information
  513. *
  514. * @return: String containing all supported clk levels
  515. */
  516. static const char *cam_soc_util_get_supported_clk_levels(
  517. struct cam_hw_soc_info *soc_info)
  518. {
  519. int i = 0;
  520. memset(supported_clk_info, 0, sizeof(supported_clk_info));
  521. strlcat(supported_clk_info, "Supported levels: ",
  522. sizeof(supported_clk_info));
  523. for (i = 0; i < CAM_MAX_VOTE; i++) {
  524. if (soc_info->clk_level_valid[i] == true) {
  525. strlcat(supported_clk_info,
  526. cam_soc_util_get_string_from_level(i),
  527. sizeof(supported_clk_info));
  528. strlcat(supported_clk_info, " ",
  529. sizeof(supported_clk_info));
  530. }
  531. }
  532. strlcat(supported_clk_info, "\n", sizeof(supported_clk_info));
  533. return supported_clk_info;
  534. }
  535. static int cam_soc_util_clk_lvl_options_open(struct inode *inode,
  536. struct file *file)
  537. {
  538. file->private_data = inode->i_private;
  539. return 0;
  540. }
  541. static ssize_t cam_soc_util_clk_lvl_options_read(struct file *file,
  542. char __user *clk_info, size_t size_t, loff_t *loff_t)
  543. {
  544. struct cam_hw_soc_info *soc_info =
  545. (struct cam_hw_soc_info *)file->private_data;
  546. const char *display_string =
  547. cam_soc_util_get_supported_clk_levels(soc_info);
  548. return simple_read_from_buffer(clk_info, size_t, loff_t, display_string,
  549. strlen(display_string));
  550. }
  551. static const struct file_operations cam_soc_util_clk_lvl_options = {
  552. .open = cam_soc_util_clk_lvl_options_open,
  553. .read = cam_soc_util_clk_lvl_options_read,
  554. };
  555. static int cam_soc_util_set_clk_lvl(void *data, u64 val)
  556. {
  557. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  558. if (val <= CAM_SUSPEND_VOTE || val >= CAM_MAX_VOTE)
  559. return 0;
  560. if (soc_info->clk_level_valid[val] == true)
  561. soc_info->clk_level_override = val;
  562. else
  563. soc_info->clk_level_override = 0;
  564. return 0;
  565. }
  566. static int cam_soc_util_get_clk_lvl(void *data, u64 *val)
  567. {
  568. struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
  569. *val = soc_info->clk_level_override;
  570. return 0;
  571. }
  572. DEFINE_SIMPLE_ATTRIBUTE(cam_soc_util_clk_lvl_control,
  573. cam_soc_util_get_clk_lvl, cam_soc_util_set_clk_lvl, "%08llu");
  574. /**
  575. * cam_soc_util_create_clk_lvl_debugfs()
  576. *
  577. * @brief: Creates debugfs files to view/control device clk rates
  578. *
  579. * @soc_info: Device soc information
  580. *
  581. * @return: Success or failure
  582. */
  583. static int cam_soc_util_create_clk_lvl_debugfs(struct cam_hw_soc_info *soc_info)
  584. {
  585. int rc = 0;
  586. struct dentry *dbgfileptr = NULL, *clkdirptr = NULL;
  587. if (!cam_debugfs_available())
  588. return 0;
  589. if (soc_info->dentry) {
  590. CAM_DBG(CAM_UTIL, "Debugfs entry for %s already exists",
  591. soc_info->dev_name);
  592. goto end;
  593. }
  594. rc = cam_debugfs_lookup_subdir(CAM_CLK_DIRNAME, &clkdirptr);
  595. if (rc) {
  596. rc = cam_debugfs_create_subdir(CAM_CLK_DIRNAME, &clkdirptr);
  597. if (rc) {
  598. CAM_ERR(CAM_UTIL, "DebugFS could not create clk directory!");
  599. rc = -ENOENT;
  600. goto end;
  601. }
  602. }
  603. dbgfileptr = debugfs_create_dir(soc_info->dev_name, clkdirptr);
  604. if (IS_ERR_OR_NULL(dbgfileptr)) {
  605. CAM_ERR(CAM_UTIL, "DebugFS could not create directory for dev:%s!",
  606. soc_info->dev_name);
  607. rc = -ENOENT;
  608. goto end;
  609. }
  610. /* Store parent inode for cleanup in caller */
  611. soc_info->dentry = dbgfileptr;
  612. dbgfileptr = debugfs_create_file("clk_lvl_options", 0444,
  613. soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_options);
  614. dbgfileptr = debugfs_create_file("clk_lvl_control", 0644,
  615. soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_control);
  616. rc = PTR_ERR_OR_ZERO(dbgfileptr);
  617. end:
  618. return rc;
  619. }
  620. int cam_soc_util_get_level_from_string(const char *string,
  621. enum cam_vote_level *level)
  622. {
  623. if (!level)
  624. return -EINVAL;
  625. if (!strcmp(string, "suspend")) {
  626. *level = CAM_SUSPEND_VOTE;
  627. } else if (!strcmp(string, "minsvs")) {
  628. *level = CAM_MINSVS_VOTE;
  629. } else if (!strcmp(string, "lowsvs")) {
  630. *level = CAM_LOWSVS_VOTE;
  631. } else if (!strcmp(string, "svs")) {
  632. *level = CAM_SVS_VOTE;
  633. } else if (!strcmp(string, "svs_l1")) {
  634. *level = CAM_SVSL1_VOTE;
  635. } else if (!strcmp(string, "nominal")) {
  636. *level = CAM_NOMINAL_VOTE;
  637. } else if (!strcmp(string, "nominal_l1")) {
  638. *level = CAM_NOMINALL1_VOTE;
  639. } else if (!strcmp(string, "turbo")) {
  640. *level = CAM_TURBO_VOTE;
  641. } else {
  642. CAM_ERR(CAM_UTIL, "Invalid string %s", string);
  643. return -EINVAL;
  644. }
  645. return 0;
  646. }
  647. /**
  648. * cam_soc_util_get_clk_level_to_apply()
  649. *
  650. * @brief: Get the clock level to apply. If the requested level
  651. * is not valid, bump the level to next available valid
  652. * level. If no higher level found, return failure.
  653. *
  654. * @soc_info: Device soc struct to be populated
  655. * @req_level: Requested level
  656. * @apply_level Level to apply
  657. *
  658. * @return: success or failure
  659. */
  660. static int cam_soc_util_get_clk_level_to_apply(
  661. struct cam_hw_soc_info *soc_info, enum cam_vote_level req_level,
  662. enum cam_vote_level *apply_level)
  663. {
  664. if (req_level >= CAM_MAX_VOTE) {
  665. CAM_ERR(CAM_UTIL, "Invalid clock level parameter %d",
  666. req_level);
  667. return -EINVAL;
  668. }
  669. if (soc_info->clk_level_valid[req_level] == true) {
  670. *apply_level = req_level;
  671. } else {
  672. int i;
  673. for (i = (req_level + 1); i < CAM_MAX_VOTE; i++)
  674. if (soc_info->clk_level_valid[i] == true) {
  675. *apply_level = i;
  676. break;
  677. }
  678. if (i == CAM_MAX_VOTE) {
  679. CAM_ERR(CAM_UTIL,
  680. "No valid clock level found to apply, req=%d",
  681. req_level);
  682. return -EINVAL;
  683. }
  684. }
  685. CAM_DBG(CAM_UTIL, "Req level %d, Applying %d",
  686. req_level, *apply_level);
  687. return 0;
  688. }
  689. int cam_soc_util_irq_enable(struct cam_hw_soc_info *soc_info)
  690. {
  691. if (!soc_info) {
  692. CAM_ERR(CAM_UTIL, "Invalid arguments");
  693. return -EINVAL;
  694. }
  695. if (!soc_info->irq_line) {
  696. CAM_ERR(CAM_UTIL, "No IRQ line available");
  697. return -ENODEV;
  698. }
  699. enable_irq(soc_info->irq_line->start);
  700. return 0;
  701. }
  702. int cam_soc_util_irq_disable(struct cam_hw_soc_info *soc_info)
  703. {
  704. if (!soc_info) {
  705. CAM_ERR(CAM_UTIL, "Invalid arguments");
  706. return -EINVAL;
  707. }
  708. if (!soc_info->irq_line) {
  709. CAM_ERR(CAM_UTIL, "No IRQ line available");
  710. return -ENODEV;
  711. }
  712. disable_irq(soc_info->irq_line->start);
  713. return 0;
  714. }
  715. long cam_soc_util_get_clk_round_rate(struct cam_hw_soc_info *soc_info,
  716. uint32_t clk_index, unsigned long clk_rate)
  717. {
  718. if (!soc_info || (clk_index >= soc_info->num_clk) || (clk_rate == 0)) {
  719. CAM_ERR(CAM_UTIL, "Invalid input params %pK, %d %lu",
  720. soc_info, clk_index, clk_rate);
  721. return clk_rate;
  722. }
  723. return clk_round_rate(soc_info->clk[clk_index], clk_rate);
  724. }
  725. /**
  726. * cam_soc_util_set_clk_rate()
  727. *
  728. * @brief: Sets the given rate for the clk requested for
  729. *
  730. * @clk: Clock structure information for which rate is to be set
  731. * @clk_name: Name of the clock for which rate is being set
  732. * @clk_rate: Clock rate to be set
  733. * @shared_clk: Whether this is a shared clk
  734. * @is_src_clk: Whether this is source clk
  735. * @clk_id: Clock ID
  736. * @applied_clk_rate: Final clock rate set to the clk
  737. *
  738. * @return: Success or failure
  739. */
  740. static int cam_soc_util_set_clk_rate(struct cam_hw_soc_info *soc_info,
  741. struct clk *clk, const char *clk_name,
  742. int64_t clk_rate, bool shared_clk, bool is_src_clk, uint32_t clk_id,
  743. unsigned long *applied_clk_rate)
  744. {
  745. int rc = 0;
  746. long clk_rate_round = -1;
  747. bool set_rate = false;
  748. if (!clk || !clk_name) {
  749. CAM_ERR(CAM_UTIL, "Invalid input clk %pK clk_name %pK",
  750. clk, clk_name);
  751. return -EINVAL;
  752. }
  753. CAM_DBG(CAM_UTIL, "set %s, rate %lld", clk_name, clk_rate);
  754. if (clk_rate > 0) {
  755. clk_rate_round = clk_round_rate(clk, clk_rate);
  756. CAM_DBG(CAM_UTIL, "new_rate %ld", clk_rate_round);
  757. if (clk_rate_round < 0) {
  758. CAM_ERR(CAM_UTIL, "round failed for clock %s rc = %ld",
  759. clk_name, clk_rate_round);
  760. return clk_rate_round;
  761. }
  762. set_rate = true;
  763. } else if (clk_rate == INIT_RATE) {
  764. clk_rate_round = clk_get_rate(clk);
  765. CAM_DBG(CAM_UTIL, "init new_rate %ld", clk_rate_round);
  766. if (clk_rate_round == 0) {
  767. clk_rate_round = clk_round_rate(clk, 0);
  768. if (clk_rate_round <= 0) {
  769. CAM_ERR(CAM_UTIL, "round rate failed on %s",
  770. clk_name);
  771. return clk_rate_round;
  772. }
  773. }
  774. set_rate = true;
  775. }
  776. if (set_rate) {
  777. if (shared_clk) {
  778. CAM_DBG(CAM_UTIL,
  779. "Dev %s clk %s id %d Set Shared clk %ld",
  780. soc_info->dev_name, clk_name, clk_id,
  781. clk_rate_round);
  782. cam_soc_util_clk_wrapper_set_clk_rate(
  783. clk_id, soc_info, clk, clk_rate_round);
  784. } else {
  785. bool set_rate_finish = false;
  786. CAM_DBG(CAM_UTIL,
  787. "Dev %s clk %s clk_id %d src_idx %d src_clk_id %d",
  788. soc_info->dev_name, clk_name, clk_id,
  789. soc_info->src_clk_idx,
  790. (soc_info->src_clk_idx == -1) ? -1 :
  791. soc_info->clk_id[soc_info->src_clk_idx]);
  792. if (is_src_clk && soc_info->mmrm_handle &&
  793. !skip_mmrm_set_rate) {
  794. uint32_t idx = soc_info->src_clk_idx;
  795. uint32_t min_level = soc_info->lowest_clk_level;
  796. rc = cam_soc_util_set_rate_through_mmrm(
  797. soc_info->mmrm_handle,
  798. soc_info->is_nrt_dev,
  799. soc_info->clk_rate[min_level][idx],
  800. clk_rate_round, 1);
  801. if (rc) {
  802. CAM_ERR(CAM_UTIL,
  803. "set_rate through mmrm failed on %s clk_id %d, rate=%ld",
  804. clk_name, clk_id,
  805. clk_rate_round);
  806. return rc;
  807. }
  808. set_rate_finish = true;
  809. }
  810. if (!set_rate_finish) {
  811. rc = clk_set_rate(clk, clk_rate_round);
  812. if (rc) {
  813. CAM_ERR(CAM_UTIL, "set_rate failed on %s", clk_name);
  814. return rc;
  815. }
  816. }
  817. }
  818. }
  819. if (applied_clk_rate)
  820. *applied_clk_rate = clk_rate_round;
  821. return rc;
  822. }
  823. int cam_soc_util_set_src_clk_rate(struct cam_hw_soc_info *soc_info,
  824. int64_t clk_rate)
  825. {
  826. int rc = 0;
  827. int i = 0;
  828. int32_t src_clk_idx;
  829. int32_t scl_clk_idx;
  830. struct clk *clk = NULL;
  831. int32_t apply_level;
  832. uint32_t clk_level_override = 0;
  833. if (!soc_info || (soc_info->src_clk_idx < 0) ||
  834. (soc_info->src_clk_idx >= CAM_SOC_MAX_CLK)) {
  835. CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d",
  836. soc_info ? soc_info->src_clk_idx : -1);
  837. return -EINVAL;
  838. }
  839. src_clk_idx = soc_info->src_clk_idx;
  840. clk_level_override = soc_info->clk_level_override;
  841. if (clk_level_override && clk_rate)
  842. clk_rate =
  843. soc_info->clk_rate[clk_level_override][src_clk_idx];
  844. clk = soc_info->clk[src_clk_idx];
  845. rc = cam_soc_util_get_clk_level(soc_info, clk_rate, src_clk_idx,
  846. &apply_level);
  847. if (rc || (apply_level < 0) || (apply_level >= CAM_MAX_VOTE)) {
  848. CAM_ERR(CAM_UTIL,
  849. "set %s, rate %lld dev_name = %s apply level = %d",
  850. soc_info->clk_name[src_clk_idx], clk_rate,
  851. soc_info->dev_name, apply_level);
  852. return -EINVAL;
  853. }
  854. CAM_DBG(CAM_UTIL, "set %s, rate %lld dev_name = %s apply level = %d",
  855. soc_info->clk_name[src_clk_idx], clk_rate,
  856. soc_info->dev_name, apply_level);
  857. if ((soc_info->cam_cx_ipeak_enable) && (clk_rate >= 0)) {
  858. cam_cx_ipeak_update_vote_cx_ipeak(soc_info,
  859. apply_level);
  860. }
  861. rc = cam_soc_util_set_clk_rate(soc_info, clk,
  862. soc_info->clk_name[src_clk_idx], clk_rate,
  863. CAM_IS_BIT_SET(soc_info->shared_clk_mask, src_clk_idx),
  864. true, soc_info->clk_id[src_clk_idx],
  865. &soc_info->applied_src_clk_rate);
  866. if (rc) {
  867. CAM_ERR(CAM_UTIL,
  868. "SET_RATE Failed: src clk: %s, rate %lld, dev_name = %s rc: %d",
  869. soc_info->clk_name[src_clk_idx], clk_rate,
  870. soc_info->dev_name, rc);
  871. return rc;
  872. }
  873. /* set clk rate for scalable clk if available */
  874. for (i = 0; i < soc_info->scl_clk_count; i++) {
  875. scl_clk_idx = soc_info->scl_clk_idx[i];
  876. if (scl_clk_idx < 0) {
  877. CAM_DBG(CAM_UTIL, "Scl clk index invalid");
  878. continue;
  879. }
  880. clk = soc_info->clk[scl_clk_idx];
  881. rc = cam_soc_util_set_clk_rate(soc_info, clk,
  882. soc_info->clk_name[scl_clk_idx],
  883. soc_info->clk_rate[apply_level][scl_clk_idx],
  884. CAM_IS_BIT_SET(soc_info->shared_clk_mask, scl_clk_idx),
  885. false, soc_info->clk_id[scl_clk_idx],
  886. NULL);
  887. if (rc) {
  888. CAM_WARN(CAM_UTIL,
  889. "SET_RATE Failed: scl clk: %s, rate %d dev_name = %s, rc: %d",
  890. soc_info->clk_name[scl_clk_idx],
  891. soc_info->clk_rate[apply_level][scl_clk_idx],
  892. soc_info->dev_name, rc);
  893. }
  894. }
  895. return 0;
  896. }
  897. int cam_soc_util_put_optional_clk(struct cam_hw_soc_info *soc_info,
  898. int32_t clk_indx)
  899. {
  900. if (clk_indx < 0) {
  901. CAM_ERR(CAM_UTIL, "Invalid params clk %d", clk_indx);
  902. return -EINVAL;
  903. }
  904. if (CAM_IS_BIT_SET(soc_info->optional_shared_clk_mask, clk_indx))
  905. cam_soc_util_clk_wrapper_unregister_entry(
  906. soc_info->optional_clk_id[clk_indx], soc_info);
  907. clk_put(soc_info->optional_clk[clk_indx]);
  908. soc_info->optional_clk[clk_indx] = NULL;
  909. return 0;
  910. }
  911. static struct clk *cam_soc_util_option_clk_get(struct device_node *np,
  912. int index, uint32_t *clk_id)
  913. {
  914. struct of_phandle_args clkspec;
  915. struct clk *clk;
  916. int rc;
  917. if (index < 0)
  918. return ERR_PTR(-EINVAL);
  919. rc = of_parse_phandle_with_args(np, "clocks-option", "#clock-cells",
  920. index, &clkspec);
  921. if (rc)
  922. return ERR_PTR(rc);
  923. clk = of_clk_get_from_provider(&clkspec);
  924. *clk_id = clkspec.args[0];
  925. of_node_put(clkspec.np);
  926. return clk;
  927. }
  928. int cam_soc_util_get_option_clk_by_name(struct cam_hw_soc_info *soc_info,
  929. const char *clk_name, int32_t *clk_index)
  930. {
  931. int index = 0;
  932. int rc = 0;
  933. struct device_node *of_node = NULL;
  934. uint32_t shared_clk_val;
  935. if (!soc_info || !clk_name || !clk_index) {
  936. CAM_ERR(CAM_UTIL,
  937. "Invalid params soc_info %pK clk_name %s clk_index %pK",
  938. soc_info, clk_name, clk_index);
  939. return -EINVAL;
  940. }
  941. of_node = soc_info->dev->of_node;
  942. index = of_property_match_string(of_node, "clock-names-option",
  943. clk_name);
  944. if (index < 0) {
  945. CAM_DBG(CAM_UTIL, "No clk data for %s", clk_name);
  946. *clk_index = -1;
  947. return -EINVAL;
  948. }
  949. if (index >= CAM_SOC_MAX_OPT_CLK) {
  950. CAM_ERR(CAM_UTIL, "Insufficient optional clk entries %d %d",
  951. index, CAM_SOC_MAX_OPT_CLK);
  952. return -EINVAL;
  953. }
  954. of_property_read_string_index(of_node, "clock-names-option",
  955. index, &(soc_info->optional_clk_name[index]));
  956. soc_info->optional_clk[index] = cam_soc_util_option_clk_get(of_node,
  957. index, &soc_info->optional_clk_id[index]);
  958. if (IS_ERR(soc_info->optional_clk[index])) {
  959. CAM_ERR(CAM_UTIL, "No clk named %s found. Dev %s", clk_name,
  960. soc_info->dev_name);
  961. *clk_index = -1;
  962. return -EFAULT;
  963. }
  964. *clk_index = index;
  965. rc = of_property_read_u32_index(of_node, "clock-rates-option",
  966. index, &soc_info->optional_clk_rate[index]);
  967. if (rc) {
  968. CAM_ERR(CAM_UTIL,
  969. "Error reading clock-rates clk_name %s index %d",
  970. clk_name, index);
  971. goto error;
  972. }
  973. /*
  974. * Option clocks are assumed to be available to single Device here.
  975. * Hence use INIT_RATE instead of NO_SET_RATE.
  976. */
  977. soc_info->optional_clk_rate[index] =
  978. (soc_info->optional_clk_rate[index] == 0) ?
  979. (int32_t)INIT_RATE : soc_info->optional_clk_rate[index];
  980. CAM_DBG(CAM_UTIL, "clk_name %s index %d clk_rate %d",
  981. clk_name, *clk_index, soc_info->optional_clk_rate[index]);
  982. rc = of_property_read_u32_index(of_node, "shared-clks-option",
  983. index, &shared_clk_val);
  984. if (rc) {
  985. CAM_DBG(CAM_UTIL, "Not shared clk %s index %d",
  986. clk_name, index);
  987. } else if (shared_clk_val > 1) {
  988. CAM_WARN(CAM_UTIL, "Invalid shared clk val %d", shared_clk_val);
  989. } else {
  990. CAM_DBG(CAM_UTIL,
  991. "Dev %s shared clk %s index %d, clk id %d, shared_clk_val %d",
  992. soc_info->dev_name, clk_name, index,
  993. soc_info->optional_clk_id[index], shared_clk_val);
  994. if (shared_clk_val) {
  995. CAM_SET_BIT(soc_info->optional_shared_clk_mask, index);
  996. /* Create a wrapper entry if this is a shared clock */
  997. CAM_DBG(CAM_UTIL,
  998. "Dev %s, clk %s, id %d register wrapper entry for shared clk",
  999. soc_info->dev_name,
  1000. soc_info->optional_clk_name[index],
  1001. soc_info->optional_clk_id[index]);
  1002. rc = cam_soc_util_clk_wrapper_register_entry(
  1003. soc_info->optional_clk_id[index],
  1004. soc_info->optional_clk[index], false,
  1005. soc_info,
  1006. soc_info->optional_clk_rate[index],
  1007. soc_info->optional_clk_name[index]);
  1008. if (rc) {
  1009. CAM_ERR(CAM_UTIL,
  1010. "Failed in registering shared clk Dev %s id %d",
  1011. soc_info->dev_name,
  1012. soc_info->optional_clk_id[index]);
  1013. goto error;
  1014. }
  1015. }
  1016. }
  1017. return 0;
  1018. error:
  1019. clk_put(soc_info->optional_clk[index]);
  1020. soc_info->optional_clk_rate[index] = 0;
  1021. soc_info->optional_clk[index] = NULL;
  1022. *clk_index = -1;
  1023. return rc;
  1024. }
  1025. int cam_soc_util_clk_enable(struct cam_hw_soc_info *soc_info,
  1026. bool optional_clk, int32_t clk_idx, int32_t apply_level,
  1027. unsigned long *applied_clock_rate)
  1028. {
  1029. int rc = 0;
  1030. struct clk *clk;
  1031. const char *clk_name;
  1032. int32_t clk_rate;
  1033. uint32_t shared_clk_mask;
  1034. uint32_t clk_id;
  1035. bool is_src_clk = false;
  1036. if (!soc_info || (clk_idx < 0) || (apply_level >= CAM_MAX_VOTE)) {
  1037. CAM_ERR(CAM_UTIL, "Invalid param %d %d", clk_idx, apply_level);
  1038. return -EINVAL;
  1039. }
  1040. if (optional_clk) {
  1041. clk = soc_info->optional_clk[clk_idx];
  1042. clk_name = soc_info->optional_clk_name[clk_idx];
  1043. clk_rate = (apply_level == -1) ?
  1044. 0 : soc_info->optional_clk_rate[clk_idx];
  1045. shared_clk_mask = soc_info->optional_shared_clk_mask;
  1046. clk_id = soc_info->optional_clk_id[clk_idx];
  1047. } else {
  1048. clk = soc_info->clk[clk_idx];
  1049. clk_name = soc_info->clk_name[clk_idx];
  1050. clk_rate = (apply_level == -1) ?
  1051. 0 : soc_info->clk_rate[apply_level][clk_idx];
  1052. shared_clk_mask = soc_info->shared_clk_mask;
  1053. clk_id = soc_info->clk_id[clk_idx];
  1054. if (clk_idx == soc_info->src_clk_idx)
  1055. is_src_clk = true;
  1056. }
  1057. rc = cam_soc_util_set_clk_rate(soc_info, clk, clk_name, clk_rate,
  1058. CAM_IS_BIT_SET(shared_clk_mask, clk_idx), is_src_clk, clk_id,
  1059. applied_clock_rate);
  1060. if (rc)
  1061. return rc;
  1062. rc = clk_prepare_enable(clk);
  1063. if (rc) {
  1064. CAM_ERR(CAM_UTIL, "enable failed for %s: rc(%d)", clk_name, rc);
  1065. return rc;
  1066. }
  1067. return rc;
  1068. }
  1069. int cam_soc_util_clk_disable(struct cam_hw_soc_info *soc_info,
  1070. bool optional_clk, int32_t clk_idx)
  1071. {
  1072. struct clk *clk;
  1073. const char *clk_name;
  1074. uint32_t shared_clk_mask;
  1075. uint32_t clk_id;
  1076. if (!soc_info || (clk_idx < 0)) {
  1077. CAM_ERR(CAM_UTIL, "Invalid param %d", clk_idx);
  1078. return -EINVAL;
  1079. }
  1080. if (optional_clk) {
  1081. clk = soc_info->optional_clk[clk_idx];
  1082. clk_name = soc_info->optional_clk_name[clk_idx];
  1083. shared_clk_mask = soc_info->optional_shared_clk_mask;
  1084. clk_id = soc_info->optional_clk_id[clk_idx];
  1085. } else {
  1086. clk = soc_info->clk[clk_idx];
  1087. clk_name = soc_info->clk_name[clk_idx];
  1088. shared_clk_mask = soc_info->shared_clk_mask;
  1089. clk_id = soc_info->clk_id[clk_idx];
  1090. }
  1091. CAM_DBG(CAM_UTIL, "disable %s", clk_name);
  1092. clk_disable_unprepare(clk);
  1093. if (CAM_IS_BIT_SET(shared_clk_mask, clk_idx)) {
  1094. CAM_DBG(CAM_UTIL,
  1095. "Dev %s clk %s Disabling Shared clk, set 0 rate",
  1096. soc_info->dev_name, clk_name);
  1097. cam_soc_util_clk_wrapper_set_clk_rate(clk_id, soc_info, clk, 0);
  1098. } else if (soc_info->mmrm_handle && (!skip_mmrm_set_rate) &&
  1099. (soc_info->src_clk_idx == clk_idx)) {
  1100. CAM_DBG(CAM_UTIL,
  1101. "Dev %s Disabling %s clk, set 0 rate", soc_info->dev_name, clk_name);
  1102. cam_soc_util_set_rate_through_mmrm(
  1103. soc_info->mmrm_handle,
  1104. soc_info->is_nrt_dev,
  1105. 0, 0, 1);
  1106. }
  1107. return 0;
  1108. }
  1109. /**
  1110. * cam_soc_util_clk_enable_default()
  1111. *
  1112. * @brief: This function enables the default clocks present
  1113. * in soc_info
  1114. *
  1115. * @soc_info: Device soc struct to be populated
  1116. * @clk_level: Clk level to apply while enabling
  1117. *
  1118. * @return: success or failure
  1119. */
  1120. int cam_soc_util_clk_enable_default(struct cam_hw_soc_info *soc_info,
  1121. enum cam_vote_level clk_level)
  1122. {
  1123. int i, rc = 0;
  1124. enum cam_vote_level apply_level;
  1125. unsigned long applied_clk_rate;
  1126. if ((soc_info->num_clk == 0) ||
  1127. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  1128. CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
  1129. soc_info->num_clk);
  1130. return -EINVAL;
  1131. }
  1132. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
  1133. &apply_level);
  1134. if (rc)
  1135. return rc;
  1136. if (soc_info->cam_cx_ipeak_enable)
  1137. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
  1138. for (i = 0; i < soc_info->num_clk; i++) {
  1139. rc = cam_soc_util_clk_enable(soc_info, false, i, apply_level,
  1140. &applied_clk_rate);
  1141. if (rc)
  1142. goto clk_disable;
  1143. if (i == soc_info->src_clk_idx)
  1144. soc_info->applied_src_clk_rate = applied_clk_rate;
  1145. if (soc_info->cam_cx_ipeak_enable) {
  1146. CAM_DBG(CAM_UTIL,
  1147. "dev name = %s clk name = %s idx = %d\n"
  1148. "apply_level = %d clc idx = %d",
  1149. soc_info->dev_name, soc_info->clk_name[i], i,
  1150. apply_level, i);
  1151. }
  1152. }
  1153. return rc;
  1154. clk_disable:
  1155. if (soc_info->cam_cx_ipeak_enable)
  1156. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  1157. for (i--; i >= 0; i--) {
  1158. cam_soc_util_clk_disable(soc_info, false, i);
  1159. }
  1160. return rc;
  1161. }
  1162. /**
  1163. * cam_soc_util_clk_disable_default()
  1164. *
  1165. * @brief: This function disables the default clocks present
  1166. * in soc_info
  1167. *
  1168. * @soc_info: device soc struct to be populated
  1169. *
  1170. * @return: success or failure
  1171. */
  1172. void cam_soc_util_clk_disable_default(struct cam_hw_soc_info *soc_info)
  1173. {
  1174. int i;
  1175. if (soc_info->num_clk == 0)
  1176. return;
  1177. if (soc_info->cam_cx_ipeak_enable)
  1178. cam_cx_ipeak_unvote_cx_ipeak(soc_info);
  1179. for (i = soc_info->num_clk - 1; i >= 0; i--)
  1180. cam_soc_util_clk_disable(soc_info, false, i);
  1181. }
  1182. /**
  1183. * cam_soc_util_get_dt_clk_info()
  1184. *
  1185. * @brief: Parse the DT and populate the Clock properties
  1186. *
  1187. * @soc_info: device soc struct to be populated
  1188. * @src_clk_str name of src clock that has rate control
  1189. *
  1190. * @return: success or failure
  1191. */
  1192. static int cam_soc_util_get_dt_clk_info(struct cam_hw_soc_info *soc_info)
  1193. {
  1194. struct device_node *of_node = NULL;
  1195. int count;
  1196. int num_clk_rates, num_clk_levels;
  1197. int i, j, rc;
  1198. int32_t num_clk_level_strings;
  1199. const char *src_clk_str = NULL;
  1200. const char *scl_clk_str = NULL;
  1201. const char *clk_control_debugfs = NULL;
  1202. const char *clk_cntl_lvl_string = NULL;
  1203. enum cam_vote_level level;
  1204. int shared_clk_cnt;
  1205. struct of_phandle_args clk_args = {0};
  1206. if (!soc_info || !soc_info->dev)
  1207. return -EINVAL;
  1208. of_node = soc_info->dev->of_node;
  1209. if (!of_property_read_bool(of_node, "use-shared-clk")) {
  1210. CAM_DBG(CAM_UTIL, "No shared clk parameter defined");
  1211. soc_info->use_shared_clk = false;
  1212. } else {
  1213. soc_info->use_shared_clk = true;
  1214. }
  1215. count = of_property_count_strings(of_node, "clock-names");
  1216. CAM_DBG(CAM_UTIL, "E: dev_name = %s count = %d",
  1217. soc_info->dev_name, count);
  1218. if (count > CAM_SOC_MAX_CLK) {
  1219. CAM_ERR(CAM_UTIL, "invalid count of clocks, count=%d", count);
  1220. rc = -EINVAL;
  1221. return rc;
  1222. }
  1223. if (count <= 0) {
  1224. CAM_DBG(CAM_UTIL, "No clock-names found");
  1225. count = 0;
  1226. soc_info->num_clk = count;
  1227. return 0;
  1228. }
  1229. soc_info->num_clk = count;
  1230. for (i = 0; i < count; i++) {
  1231. rc = of_property_read_string_index(of_node, "clock-names",
  1232. i, &(soc_info->clk_name[i]));
  1233. CAM_DBG(CAM_UTIL, "clock-names[%d] = %s",
  1234. i, soc_info->clk_name[i]);
  1235. if (rc) {
  1236. CAM_ERR(CAM_UTIL,
  1237. "i= %d count= %d reading clock-names failed",
  1238. i, count);
  1239. return rc;
  1240. }
  1241. }
  1242. num_clk_rates = of_property_count_u32_elems(of_node, "clock-rates");
  1243. if (num_clk_rates <= 0) {
  1244. CAM_ERR(CAM_UTIL, "reading clock-rates count failed");
  1245. return -EINVAL;
  1246. }
  1247. if ((num_clk_rates % soc_info->num_clk) != 0) {
  1248. CAM_ERR(CAM_UTIL,
  1249. "mismatch clk/rates, No of clocks=%d, No of rates=%d",
  1250. soc_info->num_clk, num_clk_rates);
  1251. return -EINVAL;
  1252. }
  1253. num_clk_levels = (num_clk_rates / soc_info->num_clk);
  1254. num_clk_level_strings = of_property_count_strings(of_node,
  1255. "clock-cntl-level");
  1256. if (num_clk_level_strings != num_clk_levels) {
  1257. CAM_ERR(CAM_UTIL,
  1258. "Mismatch No of levels=%d, No of level string=%d",
  1259. num_clk_levels, num_clk_level_strings);
  1260. return -EINVAL;
  1261. }
  1262. soc_info->lowest_clk_level = CAM_TURBO_VOTE;
  1263. for (i = 0; i < num_clk_levels; i++) {
  1264. rc = of_property_read_string_index(of_node,
  1265. "clock-cntl-level", i, &clk_cntl_lvl_string);
  1266. if (rc) {
  1267. CAM_ERR(CAM_UTIL,
  1268. "Error reading clock-cntl-level, rc=%d", rc);
  1269. return rc;
  1270. }
  1271. rc = cam_soc_util_get_level_from_string(clk_cntl_lvl_string,
  1272. &level);
  1273. if (rc)
  1274. return rc;
  1275. CAM_DBG(CAM_UTIL,
  1276. "[%d] : %s %d", i, clk_cntl_lvl_string, level);
  1277. soc_info->clk_level_valid[level] = true;
  1278. for (j = 0; j < soc_info->num_clk; j++) {
  1279. rc = of_property_read_u32_index(of_node, "clock-rates",
  1280. ((i * soc_info->num_clk) + j),
  1281. &soc_info->clk_rate[level][j]);
  1282. if (rc) {
  1283. CAM_ERR(CAM_UTIL,
  1284. "Error reading clock-rates, rc=%d",
  1285. rc);
  1286. return rc;
  1287. }
  1288. soc_info->clk_rate[level][j] =
  1289. (soc_info->clk_rate[level][j] == 0) ?
  1290. (int32_t)NO_SET_RATE :
  1291. soc_info->clk_rate[level][j];
  1292. CAM_DBG(CAM_UTIL, "soc_info->clk_rate[%d][%d] = %d",
  1293. level, j,
  1294. soc_info->clk_rate[level][j]);
  1295. }
  1296. if ((level > CAM_MINSVS_VOTE) &&
  1297. (level < soc_info->lowest_clk_level))
  1298. soc_info->lowest_clk_level = level;
  1299. }
  1300. soc_info->src_clk_idx = -1;
  1301. rc = of_property_read_string_index(of_node, "src-clock-name", 0,
  1302. &src_clk_str);
  1303. if (rc || !src_clk_str) {
  1304. CAM_DBG(CAM_UTIL, "No src_clk_str found");
  1305. rc = 0;
  1306. goto end;
  1307. }
  1308. for (i = 0; i < soc_info->num_clk; i++) {
  1309. if (strcmp(soc_info->clk_name[i], src_clk_str) == 0) {
  1310. soc_info->src_clk_idx = i;
  1311. CAM_DBG(CAM_UTIL, "src clock = %s, index = %d",
  1312. src_clk_str, i);
  1313. }
  1314. rc = of_parse_phandle_with_args(of_node, "clocks",
  1315. "#clock-cells", i, &clk_args);
  1316. if (rc) {
  1317. CAM_ERR(CAM_CPAS,
  1318. "failed to clock info rc=%d", rc);
  1319. rc = -EINVAL;
  1320. goto end;
  1321. }
  1322. soc_info->clk_id[i] = clk_args.args[0];
  1323. of_node_put(clk_args.np);
  1324. CAM_DBG(CAM_UTIL, "Dev %s clk %s id %d",
  1325. soc_info->dev_name, soc_info->clk_name[i],
  1326. soc_info->clk_id[i]);
  1327. }
  1328. CAM_DBG(CAM_UTIL, "Dev %s src_clk_idx %d, lowest_clk_level %d",
  1329. soc_info->dev_name, soc_info->src_clk_idx,
  1330. soc_info->lowest_clk_level);
  1331. soc_info->shared_clk_mask = 0;
  1332. shared_clk_cnt = of_property_count_u32_elems(of_node, "shared-clks");
  1333. if (shared_clk_cnt <= 0) {
  1334. CAM_DBG(CAM_UTIL, "Dev %s, no shared clks", soc_info->dev_name);
  1335. } else if (shared_clk_cnt != count) {
  1336. CAM_ERR(CAM_UTIL, "Dev %s, incorrect shared clock count %d %d",
  1337. soc_info->dev_name, shared_clk_cnt, count);
  1338. rc = -EINVAL;
  1339. goto end;
  1340. } else {
  1341. uint32_t shared_clk_val;
  1342. for (i = 0; i < shared_clk_cnt; i++) {
  1343. rc = of_property_read_u32_index(of_node,
  1344. "shared-clks", i, &shared_clk_val);
  1345. if (rc || (shared_clk_val > 1)) {
  1346. CAM_ERR(CAM_UTIL,
  1347. "Incorrect shared clk info at %d, val=%d, count=%d",
  1348. i, shared_clk_val, shared_clk_cnt);
  1349. rc = -EINVAL;
  1350. goto end;
  1351. }
  1352. if (shared_clk_val)
  1353. CAM_SET_BIT(soc_info->shared_clk_mask, i);
  1354. }
  1355. CAM_DBG(CAM_UTIL, "Dev %s shared clk mask 0x%x",
  1356. soc_info->dev_name, soc_info->shared_clk_mask);
  1357. }
  1358. /* scalable clk info parsing */
  1359. soc_info->scl_clk_count = 0;
  1360. soc_info->scl_clk_count = of_property_count_strings(of_node,
  1361. "scl-clk-names");
  1362. if ((soc_info->scl_clk_count <= 0) ||
  1363. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  1364. if (soc_info->scl_clk_count == -EINVAL) {
  1365. CAM_DBG(CAM_UTIL, "scl_clk_name prop not avialable");
  1366. } else if ((soc_info->scl_clk_count == -ENODATA) ||
  1367. (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
  1368. CAM_ERR(CAM_UTIL, "Invalid scl_clk_count: %d",
  1369. soc_info->scl_clk_count);
  1370. return -EINVAL;
  1371. }
  1372. CAM_DBG(CAM_UTIL, "Invalid scl_clk count: %d",
  1373. soc_info->scl_clk_count);
  1374. soc_info->scl_clk_count = -1;
  1375. } else {
  1376. CAM_DBG(CAM_UTIL, "No of scalable clocks: %d",
  1377. soc_info->scl_clk_count);
  1378. for (i = 0; i < soc_info->scl_clk_count; i++) {
  1379. rc = of_property_read_string_index(of_node,
  1380. "scl-clk-names", i,
  1381. (const char **)&scl_clk_str);
  1382. if (rc || !scl_clk_str) {
  1383. CAM_WARN(CAM_UTIL, "scl_clk_str is NULL");
  1384. soc_info->scl_clk_idx[i] = -1;
  1385. continue;
  1386. }
  1387. for (j = 0; j < soc_info->num_clk; j++) {
  1388. if (strnstr(scl_clk_str, soc_info->clk_name[j],
  1389. strlen(scl_clk_str))) {
  1390. soc_info->scl_clk_idx[i] = j;
  1391. CAM_DBG(CAM_UTIL,
  1392. "scl clock = %s, index = %d",
  1393. scl_clk_str, j);
  1394. break;
  1395. }
  1396. }
  1397. }
  1398. }
  1399. rc = of_property_read_string_index(of_node,
  1400. "clock-control-debugfs", 0, &clk_control_debugfs);
  1401. if (rc || !clk_control_debugfs) {
  1402. CAM_DBG(CAM_UTIL, "No clock_control_debugfs property found");
  1403. rc = 0;
  1404. goto end;
  1405. }
  1406. if (strcmp("true", clk_control_debugfs) == 0)
  1407. soc_info->clk_control_enable = true;
  1408. CAM_DBG(CAM_UTIL, "X: dev_name = %s count = %d",
  1409. soc_info->dev_name, count);
  1410. end:
  1411. return rc;
  1412. }
  1413. int cam_soc_util_set_clk_rate_level(struct cam_hw_soc_info *soc_info,
  1414. enum cam_vote_level clk_level, bool do_not_set_src_clk)
  1415. {
  1416. int i, rc = 0;
  1417. enum cam_vote_level apply_level;
  1418. unsigned long applied_clk_rate;
  1419. if ((soc_info->num_clk == 0) ||
  1420. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  1421. CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
  1422. soc_info->num_clk);
  1423. return -EINVAL;
  1424. }
  1425. rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
  1426. &apply_level);
  1427. if (rc)
  1428. return rc;
  1429. if (soc_info->cam_cx_ipeak_enable)
  1430. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
  1431. for (i = 0; i < soc_info->num_clk; i++) {
  1432. if (do_not_set_src_clk && (i == soc_info->src_clk_idx)) {
  1433. CAM_DBG(CAM_UTIL, "Skipping set rate for src clk %s",
  1434. soc_info->clk_name[i]);
  1435. continue;
  1436. }
  1437. CAM_DBG(CAM_UTIL, "Set rate for clk %s rate %d",
  1438. soc_info->clk_name[i],
  1439. soc_info->clk_rate[apply_level][i]);
  1440. rc = cam_soc_util_set_clk_rate(soc_info, soc_info->clk[i],
  1441. soc_info->clk_name[i],
  1442. soc_info->clk_rate[apply_level][i],
  1443. CAM_IS_BIT_SET(soc_info->shared_clk_mask, i),
  1444. (i == soc_info->src_clk_idx) ? true : false,
  1445. soc_info->clk_id[i],
  1446. &applied_clk_rate);
  1447. if (rc < 0) {
  1448. CAM_DBG(CAM_UTIL,
  1449. "dev name = %s clk_name = %s idx = %d\n"
  1450. "apply_level = %d",
  1451. soc_info->dev_name, soc_info->clk_name[i],
  1452. i, apply_level);
  1453. if (soc_info->cam_cx_ipeak_enable)
  1454. cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
  1455. break;
  1456. }
  1457. if (i == soc_info->src_clk_idx)
  1458. soc_info->applied_src_clk_rate = applied_clk_rate;
  1459. }
  1460. return rc;
  1461. };
  1462. static int cam_soc_util_get_dt_gpio_req_tbl(struct device_node *of_node,
  1463. struct cam_soc_gpio_data *gconf, uint16_t *gpio_array,
  1464. uint16_t gpio_array_size)
  1465. {
  1466. int32_t rc = 0, i = 0;
  1467. uint32_t count = 0;
  1468. uint32_t *val_array = NULL;
  1469. if (!of_get_property(of_node, "gpio-req-tbl-num", &count))
  1470. return 0;
  1471. count /= sizeof(uint32_t);
  1472. if (!count) {
  1473. CAM_ERR(CAM_UTIL, "gpio-req-tbl-num 0");
  1474. return 0;
  1475. }
  1476. val_array = kcalloc(count, sizeof(uint32_t), GFP_KERNEL);
  1477. if (!val_array)
  1478. return -ENOMEM;
  1479. gconf->cam_gpio_req_tbl = kcalloc(count, sizeof(struct gpio),
  1480. GFP_KERNEL);
  1481. if (!gconf->cam_gpio_req_tbl) {
  1482. rc = -ENOMEM;
  1483. goto free_val_array;
  1484. }
  1485. gconf->cam_gpio_req_tbl_size = count;
  1486. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-num",
  1487. val_array, count);
  1488. if (rc) {
  1489. CAM_ERR(CAM_UTIL, "failed in reading gpio-req-tbl-num, rc = %d",
  1490. rc);
  1491. goto free_gpio_req_tbl;
  1492. }
  1493. for (i = 0; i < count; i++) {
  1494. if (val_array[i] >= gpio_array_size) {
  1495. CAM_ERR(CAM_UTIL, "gpio req tbl index %d invalid",
  1496. val_array[i]);
  1497. goto free_gpio_req_tbl;
  1498. }
  1499. gconf->cam_gpio_req_tbl[i].gpio = gpio_array[val_array[i]];
  1500. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].gpio = %d", i,
  1501. gconf->cam_gpio_req_tbl[i].gpio);
  1502. }
  1503. rc = of_property_read_u32_array(of_node, "gpio-req-tbl-flags",
  1504. val_array, count);
  1505. if (rc) {
  1506. CAM_ERR(CAM_UTIL, "Failed in gpio-req-tbl-flags, rc %d", rc);
  1507. goto free_gpio_req_tbl;
  1508. }
  1509. for (i = 0; i < count; i++) {
  1510. gconf->cam_gpio_req_tbl[i].flags = val_array[i];
  1511. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].flags = %ld", i,
  1512. gconf->cam_gpio_req_tbl[i].flags);
  1513. }
  1514. for (i = 0; i < count; i++) {
  1515. rc = of_property_read_string_index(of_node,
  1516. "gpio-req-tbl-label", i,
  1517. &gconf->cam_gpio_req_tbl[i].label);
  1518. if (rc) {
  1519. CAM_ERR(CAM_UTIL, "Failed rc %d", rc);
  1520. goto free_gpio_req_tbl;
  1521. }
  1522. CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].label = %s", i,
  1523. gconf->cam_gpio_req_tbl[i].label);
  1524. }
  1525. kfree(val_array);
  1526. return rc;
  1527. free_gpio_req_tbl:
  1528. kfree(gconf->cam_gpio_req_tbl);
  1529. free_val_array:
  1530. kfree(val_array);
  1531. gconf->cam_gpio_req_tbl_size = 0;
  1532. return rc;
  1533. }
  1534. static int cam_soc_util_get_gpio_info(struct cam_hw_soc_info *soc_info)
  1535. {
  1536. int32_t rc = 0, i = 0;
  1537. uint16_t *gpio_array = NULL;
  1538. int16_t gpio_array_size = 0;
  1539. struct cam_soc_gpio_data *gconf = NULL;
  1540. struct device_node *of_node = NULL;
  1541. if (!soc_info || !soc_info->dev)
  1542. return -EINVAL;
  1543. of_node = soc_info->dev->of_node;
  1544. /* Validate input parameters */
  1545. if (!of_node) {
  1546. CAM_ERR(CAM_UTIL, "Invalid param of_node");
  1547. return -EINVAL;
  1548. }
  1549. gpio_array_size = of_gpio_count(of_node);
  1550. if (gpio_array_size <= 0)
  1551. return 0;
  1552. CAM_DBG(CAM_UTIL, "gpio count %d", gpio_array_size);
  1553. gpio_array = kcalloc(gpio_array_size, sizeof(uint16_t), GFP_KERNEL);
  1554. if (!gpio_array)
  1555. goto free_gpio_conf;
  1556. for (i = 0; i < gpio_array_size; i++) {
  1557. gpio_array[i] = of_get_gpio(of_node, i);
  1558. CAM_DBG(CAM_UTIL, "gpio_array[%d] = %d", i, gpio_array[i]);
  1559. }
  1560. gconf = kzalloc(sizeof(*gconf), GFP_KERNEL);
  1561. if (!gconf)
  1562. return -ENOMEM;
  1563. rc = cam_soc_util_get_dt_gpio_req_tbl(of_node, gconf, gpio_array,
  1564. gpio_array_size);
  1565. if (rc) {
  1566. CAM_ERR(CAM_UTIL, "failed in msm_camera_get_dt_gpio_req_tbl");
  1567. goto free_gpio_array;
  1568. }
  1569. gconf->cam_gpio_common_tbl = kcalloc(gpio_array_size,
  1570. sizeof(struct gpio), GFP_KERNEL);
  1571. if (!gconf->cam_gpio_common_tbl) {
  1572. rc = -ENOMEM;
  1573. goto free_gpio_array;
  1574. }
  1575. for (i = 0; i < gpio_array_size; i++)
  1576. gconf->cam_gpio_common_tbl[i].gpio = gpio_array[i];
  1577. gconf->cam_gpio_common_tbl_size = gpio_array_size;
  1578. soc_info->gpio_data = gconf;
  1579. kfree(gpio_array);
  1580. return rc;
  1581. free_gpio_array:
  1582. kfree(gpio_array);
  1583. free_gpio_conf:
  1584. kfree(gconf);
  1585. soc_info->gpio_data = NULL;
  1586. return rc;
  1587. }
  1588. static int cam_soc_util_request_gpio_table(
  1589. struct cam_hw_soc_info *soc_info, bool gpio_en)
  1590. {
  1591. int rc = 0, i = 0;
  1592. uint8_t size = 0;
  1593. struct cam_soc_gpio_data *gpio_conf =
  1594. soc_info->gpio_data;
  1595. struct gpio *gpio_tbl = NULL;
  1596. if (!gpio_conf) {
  1597. CAM_DBG(CAM_UTIL, "No GPIO entry");
  1598. return 0;
  1599. }
  1600. if (gpio_conf->cam_gpio_common_tbl_size <= 0) {
  1601. CAM_ERR(CAM_UTIL, "GPIO table size is invalid");
  1602. return -EINVAL;
  1603. }
  1604. size = gpio_conf->cam_gpio_req_tbl_size;
  1605. gpio_tbl = gpio_conf->cam_gpio_req_tbl;
  1606. if (!gpio_tbl || !size) {
  1607. CAM_ERR(CAM_UTIL, "Invalid gpio_tbl %pK / size %d",
  1608. gpio_tbl, size);
  1609. return -EINVAL;
  1610. }
  1611. for (i = 0; i < size; i++) {
  1612. CAM_DBG(CAM_UTIL, "i=%d, gpio=%d dir=%ld", i,
  1613. gpio_tbl[i].gpio, gpio_tbl[i].flags);
  1614. }
  1615. if (gpio_en) {
  1616. for (i = 0; i < size; i++) {
  1617. rc = gpio_request_one(gpio_tbl[i].gpio,
  1618. gpio_tbl[i].flags, gpio_tbl[i].label);
  1619. if (rc) {
  1620. /*
  1621. * After GPIO request fails, contine to
  1622. * apply new gpios, outout a error message
  1623. * for driver bringup debug
  1624. */
  1625. CAM_ERR(CAM_UTIL, "gpio %d:%s request fails",
  1626. gpio_tbl[i].gpio, gpio_tbl[i].label);
  1627. }
  1628. }
  1629. } else {
  1630. gpio_free_array(gpio_tbl, size);
  1631. }
  1632. return rc;
  1633. }
  1634. static int cam_soc_util_get_dt_regulator_info
  1635. (struct cam_hw_soc_info *soc_info)
  1636. {
  1637. int rc = 0, count = 0, i = 0;
  1638. struct device_node *of_node = NULL;
  1639. if (!soc_info || !soc_info->dev) {
  1640. CAM_ERR(CAM_UTIL, "Invalid parameters");
  1641. return -EINVAL;
  1642. }
  1643. of_node = soc_info->dev->of_node;
  1644. soc_info->num_rgltr = 0;
  1645. count = of_property_count_strings(of_node, "regulator-names");
  1646. if (count != -EINVAL) {
  1647. if (count <= 0) {
  1648. CAM_ERR(CAM_UTIL, "no regulators found");
  1649. count = 0;
  1650. return -EINVAL;
  1651. }
  1652. soc_info->num_rgltr = count;
  1653. } else {
  1654. CAM_DBG(CAM_UTIL, "No regulators node found");
  1655. return 0;
  1656. }
  1657. for (i = 0; i < soc_info->num_rgltr; i++) {
  1658. rc = of_property_read_string_index(of_node,
  1659. "regulator-names", i, &soc_info->rgltr_name[i]);
  1660. CAM_DBG(CAM_UTIL, "rgltr_name[%d] = %s",
  1661. i, soc_info->rgltr_name[i]);
  1662. if (rc) {
  1663. CAM_ERR(CAM_UTIL, "no regulator resource at cnt=%d", i);
  1664. return -ENODEV;
  1665. }
  1666. }
  1667. if (!of_property_read_bool(of_node, "rgltr-cntrl-support")) {
  1668. CAM_DBG(CAM_UTIL, "No regulator control parameter defined");
  1669. soc_info->rgltr_ctrl_support = false;
  1670. return 0;
  1671. }
  1672. soc_info->rgltr_ctrl_support = true;
  1673. rc = of_property_read_u32_array(of_node, "rgltr-min-voltage",
  1674. soc_info->rgltr_min_volt, soc_info->num_rgltr);
  1675. if (rc) {
  1676. CAM_ERR(CAM_UTIL, "No minimum volatage value found, rc=%d", rc);
  1677. return -EINVAL;
  1678. }
  1679. rc = of_property_read_u32_array(of_node, "rgltr-max-voltage",
  1680. soc_info->rgltr_max_volt, soc_info->num_rgltr);
  1681. if (rc) {
  1682. CAM_ERR(CAM_UTIL, "No maximum volatage value found, rc=%d", rc);
  1683. return -EINVAL;
  1684. }
  1685. rc = of_property_read_u32_array(of_node, "rgltr-load-current",
  1686. soc_info->rgltr_op_mode, soc_info->num_rgltr);
  1687. if (rc) {
  1688. CAM_ERR(CAM_UTIL, "No Load curent found rc=%d", rc);
  1689. return -EINVAL;
  1690. }
  1691. return rc;
  1692. }
  1693. #ifdef CONFIG_CAM_PRESIL
  1694. static uint32_t next_dummy_irq_line_num = 0x000f;
  1695. struct resource dummy_irq_line[512];
  1696. #endif
  1697. int cam_soc_util_get_dt_properties(struct cam_hw_soc_info *soc_info)
  1698. {
  1699. struct device_node *of_node = NULL;
  1700. int count = 0, i = 0, rc = 0;
  1701. if (!soc_info || !soc_info->dev)
  1702. return -EINVAL;
  1703. of_node = soc_info->dev->of_node;
  1704. rc = of_property_read_u32(of_node, "cell-index", &soc_info->index);
  1705. if (rc) {
  1706. CAM_ERR(CAM_UTIL, "device %s failed to read cell-index",
  1707. soc_info->dev_name);
  1708. return rc;
  1709. }
  1710. count = of_property_count_strings(of_node, "reg-names");
  1711. if (count <= 0) {
  1712. CAM_DBG(CAM_UTIL, "no reg-names found for: %s",
  1713. soc_info->dev_name);
  1714. count = 0;
  1715. }
  1716. soc_info->num_mem_block = count;
  1717. for (i = 0; i < soc_info->num_mem_block; i++) {
  1718. rc = of_property_read_string_index(of_node, "reg-names", i,
  1719. &soc_info->mem_block_name[i]);
  1720. if (rc) {
  1721. CAM_ERR(CAM_UTIL, "failed to read reg-names at %d", i);
  1722. return rc;
  1723. }
  1724. soc_info->mem_block[i] =
  1725. platform_get_resource_byname(soc_info->pdev,
  1726. IORESOURCE_MEM, soc_info->mem_block_name[i]);
  1727. if (!soc_info->mem_block[i]) {
  1728. CAM_ERR(CAM_UTIL, "no mem resource by name %s",
  1729. soc_info->mem_block_name[i]);
  1730. rc = -ENODEV;
  1731. return rc;
  1732. }
  1733. }
  1734. rc = of_property_read_string(of_node, "label", &soc_info->label_name);
  1735. if (rc)
  1736. CAM_DBG(CAM_UTIL, "Label is not available in the node: %d", rc);
  1737. if (soc_info->num_mem_block > 0) {
  1738. rc = of_property_read_u32_array(of_node, "reg-cam-base",
  1739. soc_info->mem_block_cam_base, soc_info->num_mem_block);
  1740. if (rc) {
  1741. CAM_ERR(CAM_UTIL, "Error reading register offsets");
  1742. return rc;
  1743. }
  1744. }
  1745. rc = of_property_read_string_index(of_node, "interrupt-names", 0,
  1746. &soc_info->irq_name);
  1747. if (rc) {
  1748. CAM_DBG(CAM_UTIL, "No interrupt line preset for: %s",
  1749. soc_info->dev_name);
  1750. rc = 0;
  1751. } else {
  1752. soc_info->irq_line =
  1753. platform_get_resource_byname(soc_info->pdev,
  1754. IORESOURCE_IRQ, soc_info->irq_name);
  1755. if (!soc_info->irq_line) {
  1756. CAM_ERR(CAM_UTIL, "no irq resource");
  1757. #ifndef CONFIG_CAM_PRESIL
  1758. rc = -ENODEV;
  1759. return rc;
  1760. #else
  1761. /* Pre-sil for new devices not present on old */
  1762. soc_info->irq_line =
  1763. &dummy_irq_line[next_dummy_irq_line_num++];
  1764. CAM_DBG(CAM_PRESIL, "interrupt line for dev %s irq name %s number %d",
  1765. soc_info->dev_name, soc_info->irq_name,
  1766. soc_info->irq_line->start);
  1767. #endif
  1768. }
  1769. }
  1770. rc = of_property_read_string_index(of_node, "compatible", 0,
  1771. (const char **)&soc_info->compatible);
  1772. if (rc) {
  1773. CAM_DBG(CAM_UTIL, "No compatible string present for: %s",
  1774. soc_info->dev_name);
  1775. rc = 0;
  1776. }
  1777. soc_info->is_nrt_dev = false;
  1778. if (of_property_read_bool(of_node, "nrt-device"))
  1779. soc_info->is_nrt_dev = true;
  1780. CAM_DBG(CAM_UTIL, "Dev %s, nrt_dev %d",
  1781. soc_info->dev_name, soc_info->is_nrt_dev);
  1782. rc = cam_soc_util_get_dt_regulator_info(soc_info);
  1783. if (rc)
  1784. return rc;
  1785. rc = cam_soc_util_get_dt_clk_info(soc_info);
  1786. if (rc)
  1787. return rc;
  1788. rc = cam_soc_util_get_gpio_info(soc_info);
  1789. if (rc)
  1790. return rc;
  1791. if (of_find_property(of_node, "qcom,cam-cx-ipeak", NULL))
  1792. rc = cam_cx_ipeak_register_cx_ipeak(soc_info);
  1793. return rc;
  1794. }
  1795. /**
  1796. * cam_soc_util_get_regulator()
  1797. *
  1798. * @brief: Get regulator resource named vdd
  1799. *
  1800. * @dev: Device associated with regulator
  1801. * @reg: Return pointer to be filled with regulator on success
  1802. * @rgltr_name: Name of regulator to get
  1803. *
  1804. * @return: 0 for Success, negative value for failure
  1805. */
  1806. static int cam_soc_util_get_regulator(struct device *dev,
  1807. struct regulator **reg, const char *rgltr_name)
  1808. {
  1809. int rc = 0;
  1810. *reg = regulator_get(dev, rgltr_name);
  1811. if (IS_ERR_OR_NULL(*reg)) {
  1812. rc = PTR_ERR(*reg);
  1813. rc = rc ? rc : -EINVAL;
  1814. CAM_ERR(CAM_UTIL, "Regulator %s get failed %d", rgltr_name, rc);
  1815. *reg = NULL;
  1816. }
  1817. return rc;
  1818. }
  1819. int cam_soc_util_regulator_disable(struct regulator *rgltr,
  1820. const char *rgltr_name, uint32_t rgltr_min_volt,
  1821. uint32_t rgltr_max_volt, uint32_t rgltr_op_mode,
  1822. uint32_t rgltr_delay_ms)
  1823. {
  1824. int32_t rc = 0;
  1825. if (!rgltr) {
  1826. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  1827. return -EINVAL;
  1828. }
  1829. rc = regulator_disable(rgltr);
  1830. if (rc) {
  1831. CAM_ERR(CAM_UTIL, "%s regulator disable failed", rgltr_name);
  1832. return rc;
  1833. }
  1834. if (rgltr_delay_ms > 20)
  1835. msleep(rgltr_delay_ms);
  1836. else if (rgltr_delay_ms)
  1837. usleep_range(rgltr_delay_ms * 1000,
  1838. (rgltr_delay_ms * 1000) + 1000);
  1839. if (regulator_count_voltages(rgltr) > 0) {
  1840. regulator_set_load(rgltr, 0);
  1841. regulator_set_voltage(rgltr, 0, rgltr_max_volt);
  1842. }
  1843. return rc;
  1844. }
  1845. int cam_soc_util_regulator_enable(struct regulator *rgltr,
  1846. const char *rgltr_name,
  1847. uint32_t rgltr_min_volt, uint32_t rgltr_max_volt,
  1848. uint32_t rgltr_op_mode, uint32_t rgltr_delay)
  1849. {
  1850. int32_t rc = 0;
  1851. if (!rgltr) {
  1852. CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
  1853. return -EINVAL;
  1854. }
  1855. if (regulator_count_voltages(rgltr) > 0) {
  1856. CAM_DBG(CAM_UTIL, "voltage min=%d, max=%d",
  1857. rgltr_min_volt, rgltr_max_volt);
  1858. rc = regulator_set_voltage(
  1859. rgltr, rgltr_min_volt, rgltr_max_volt);
  1860. if (rc) {
  1861. CAM_ERR(CAM_UTIL, "%s set voltage failed", rgltr_name);
  1862. return rc;
  1863. }
  1864. rc = regulator_set_load(rgltr, rgltr_op_mode);
  1865. if (rc) {
  1866. CAM_ERR(CAM_UTIL, "%s set optimum mode failed",
  1867. rgltr_name);
  1868. return rc;
  1869. }
  1870. }
  1871. rc = regulator_enable(rgltr);
  1872. if (rc) {
  1873. CAM_ERR(CAM_UTIL, "%s regulator_enable failed", rgltr_name);
  1874. return rc;
  1875. }
  1876. if (rgltr_delay > 20)
  1877. msleep(rgltr_delay);
  1878. else if (rgltr_delay)
  1879. usleep_range(rgltr_delay * 1000,
  1880. (rgltr_delay * 1000) + 1000);
  1881. return rc;
  1882. }
  1883. int cam_soc_util_select_pinctrl_state(struct cam_hw_soc_info *soc_info,
  1884. int pctrl_idx, bool active)
  1885. {
  1886. int rc = 0;
  1887. struct cam_soc_pinctrl_info *pctrl_info = &soc_info->pinctrl_info;
  1888. if (pctrl_idx >= CAM_SOC_MAX_PINCTRL_MAP) {
  1889. CAM_ERR(CAM_UTIL, "Invalid Map idx: %d max supported: %d",
  1890. pctrl_idx, CAM_SOC_MAX_PINCTRL_MAP);
  1891. return -EINVAL;
  1892. }
  1893. if (pctrl_info->pctrl_state[pctrl_idx].gpio_state_active &&
  1894. active &&
  1895. !pctrl_info->pctrl_state[pctrl_idx].is_active) {
  1896. rc = pinctrl_select_state(pctrl_info->pinctrl,
  1897. pctrl_info->pctrl_state[pctrl_idx].gpio_state_active);
  1898. if (rc)
  1899. CAM_ERR(CAM_UTIL,
  1900. "Pinctrl active state transition failed: rc: %d",
  1901. rc);
  1902. else {
  1903. pctrl_info->pctrl_state[pctrl_idx].is_active = true;
  1904. CAM_DBG(CAM_UTIL, "Pctrl_idx: %d is in active state",
  1905. pctrl_idx);
  1906. }
  1907. }
  1908. if (pctrl_info->pctrl_state[pctrl_idx].gpio_state_suspend &&
  1909. !active &&
  1910. pctrl_info->pctrl_state[pctrl_idx].is_active) {
  1911. rc = pinctrl_select_state(pctrl_info->pinctrl,
  1912. pctrl_info->pctrl_state[pctrl_idx].gpio_state_suspend);
  1913. if (rc)
  1914. CAM_ERR(CAM_UTIL,
  1915. "Pinctrl suspend state transition failed: rc: %d",
  1916. rc);
  1917. else {
  1918. pctrl_info->pctrl_state[pctrl_idx].is_active = false;
  1919. CAM_DBG(CAM_UTIL, "Pctrl_idx: %d is in suspend state",
  1920. pctrl_idx);
  1921. }
  1922. }
  1923. return rc;
  1924. }
  1925. static int cam_soc_util_request_pinctrl(
  1926. struct cam_hw_soc_info *soc_info)
  1927. {
  1928. struct cam_soc_pinctrl_info *device_pctrl = &soc_info->pinctrl_info;
  1929. struct device *dev = soc_info->dev;
  1930. struct device_node *of_node = dev->of_node;
  1931. uint32_t i = 0;
  1932. int rc = 0;
  1933. const char *name;
  1934. uint32_t idx;
  1935. char pctrl_active[50];
  1936. char pctrl_suspend[50];
  1937. int32_t num_of_map_idx = 0;
  1938. int32_t num_of_string = 0;
  1939. device_pctrl->pinctrl = devm_pinctrl_get(dev);
  1940. if (IS_ERR_OR_NULL(device_pctrl->pinctrl)) {
  1941. CAM_DBG(CAM_UTIL, "Pinctrl not available");
  1942. device_pctrl->pinctrl = NULL;
  1943. return 0;
  1944. }
  1945. num_of_map_idx = of_property_count_u32_elems(
  1946. of_node, "pctrl-idx-mapping");
  1947. if (num_of_map_idx <= 0) {
  1948. CAM_ERR(CAM_UTIL,
  1949. "Reading pctrl-idx-mapping failed");
  1950. return -EINVAL;
  1951. }
  1952. num_of_string = of_property_count_strings(
  1953. of_node, "pctrl-map-names");
  1954. if (num_of_string <= 0) {
  1955. CAM_ERR(CAM_UTIL, "no pinctrl-mapping found for: %s",
  1956. soc_info->dev_name);
  1957. device_pctrl->pinctrl = NULL;
  1958. return -EINVAL;
  1959. }
  1960. if (num_of_map_idx != num_of_string) {
  1961. CAM_ERR(CAM_UTIL,
  1962. "Incorrect inputs mapping-idx count: %d mapping-names: %d",
  1963. num_of_map_idx, num_of_string);
  1964. device_pctrl->pinctrl = NULL;
  1965. return -EINVAL;
  1966. }
  1967. if (num_of_map_idx > CAM_SOC_MAX_PINCTRL_MAP) {
  1968. CAM_ERR(CAM_UTIL, "Invalid mapping %u max supported: %d",
  1969. num_of_map_idx, CAM_SOC_MAX_PINCTRL_MAP);
  1970. return -EINVAL;
  1971. }
  1972. for (i = 0; i < num_of_map_idx; i++) {
  1973. memset(pctrl_active, '\0', sizeof(pctrl_active));
  1974. memset(pctrl_suspend, '\0', sizeof(pctrl_suspend));
  1975. of_property_read_u32_index(of_node,
  1976. "pctrl-idx-mapping", i, &idx);
  1977. if (idx >= CAM_SOC_MAX_PINCTRL_MAP) {
  1978. CAM_ERR(CAM_UTIL, "Invalid Index: %d max supported: %d",
  1979. idx, CAM_SOC_MAX_PINCTRL_MAP);
  1980. return -EINVAL;
  1981. }
  1982. rc = of_property_read_string_index(
  1983. of_node, "pctrl-map-names", i, &name);
  1984. if (rc) {
  1985. CAM_ERR(CAM_UTIL,
  1986. "failed to read pinctrl-mapping at %d", i);
  1987. return rc;
  1988. }
  1989. snprintf(pctrl_active, sizeof(pctrl_active),
  1990. "%s%s", name, "_active");
  1991. CAM_DBG(CAM_UTIL, "pctrl_active at index: %d name: %s",
  1992. i, pctrl_active);
  1993. snprintf(pctrl_suspend, sizeof(pctrl_suspend),
  1994. "%s%s", name, "_suspend");
  1995. CAM_DBG(CAM_UTIL, "pctrl_suspend at index: %d name: %s",
  1996. i, pctrl_suspend);
  1997. device_pctrl->pctrl_state[idx].gpio_state_active =
  1998. pinctrl_lookup_state(device_pctrl->pinctrl,
  1999. pctrl_active);
  2000. if (IS_ERR_OR_NULL(
  2001. device_pctrl->pctrl_state[idx].gpio_state_active)) {
  2002. CAM_ERR(CAM_UTIL,
  2003. "Failed to get the active state pinctrl handle");
  2004. device_pctrl->pctrl_state[idx].gpio_state_active =
  2005. NULL;
  2006. return -EINVAL;
  2007. }
  2008. device_pctrl->pctrl_state[idx].gpio_state_suspend =
  2009. pinctrl_lookup_state(device_pctrl->pinctrl,
  2010. pctrl_suspend);
  2011. if (IS_ERR_OR_NULL(
  2012. device_pctrl->pctrl_state[idx].gpio_state_suspend)) {
  2013. CAM_ERR(CAM_UTIL,
  2014. "Failed to get the active state pinctrl handle");
  2015. device_pctrl->pctrl_state[idx].gpio_state_suspend = NULL;
  2016. return -EINVAL;
  2017. }
  2018. }
  2019. return 0;
  2020. }
  2021. static void cam_soc_util_release_pinctrl(struct cam_hw_soc_info *soc_info)
  2022. {
  2023. if (soc_info->pinctrl_info.pinctrl)
  2024. devm_pinctrl_put(soc_info->pinctrl_info.pinctrl);
  2025. }
  2026. static void cam_soc_util_regulator_disable_default(
  2027. struct cam_hw_soc_info *soc_info)
  2028. {
  2029. int j = 0;
  2030. uint32_t num_rgltr = soc_info->num_rgltr;
  2031. for (j = num_rgltr-1; j >= 0; j--) {
  2032. if (soc_info->rgltr_ctrl_support == true) {
  2033. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  2034. soc_info->rgltr_name[j],
  2035. soc_info->rgltr_min_volt[j],
  2036. soc_info->rgltr_max_volt[j],
  2037. soc_info->rgltr_op_mode[j],
  2038. soc_info->rgltr_delay[j]);
  2039. } else {
  2040. if (soc_info->rgltr[j])
  2041. regulator_disable(soc_info->rgltr[j]);
  2042. }
  2043. }
  2044. }
  2045. static int cam_soc_util_regulator_enable_default(
  2046. struct cam_hw_soc_info *soc_info)
  2047. {
  2048. int j = 0, rc = 0;
  2049. uint32_t num_rgltr = soc_info->num_rgltr;
  2050. for (j = 0; j < num_rgltr; j++) {
  2051. if (soc_info->rgltr_ctrl_support == true) {
  2052. rc = cam_soc_util_regulator_enable(soc_info->rgltr[j],
  2053. soc_info->rgltr_name[j],
  2054. soc_info->rgltr_min_volt[j],
  2055. soc_info->rgltr_max_volt[j],
  2056. soc_info->rgltr_op_mode[j],
  2057. soc_info->rgltr_delay[j]);
  2058. } else {
  2059. if (soc_info->rgltr[j])
  2060. rc = regulator_enable(soc_info->rgltr[j]);
  2061. }
  2062. if (rc) {
  2063. CAM_ERR(CAM_UTIL, "%s enable failed",
  2064. soc_info->rgltr_name[j]);
  2065. goto disable_rgltr;
  2066. }
  2067. }
  2068. return rc;
  2069. disable_rgltr:
  2070. for (j--; j >= 0; j--) {
  2071. if (soc_info->rgltr_ctrl_support == true) {
  2072. cam_soc_util_regulator_disable(soc_info->rgltr[j],
  2073. soc_info->rgltr_name[j],
  2074. soc_info->rgltr_min_volt[j],
  2075. soc_info->rgltr_max_volt[j],
  2076. soc_info->rgltr_op_mode[j],
  2077. soc_info->rgltr_delay[j]);
  2078. } else {
  2079. if (soc_info->rgltr[j])
  2080. regulator_disable(soc_info->rgltr[j]);
  2081. }
  2082. }
  2083. return rc;
  2084. }
  2085. static bool cam_soc_util_is_presil_address_space(unsigned long mem_block_start)
  2086. {
  2087. if(mem_block_start >= CAM_SS_START_PRESIL && mem_block_start < CAM_SS_START)
  2088. return true;
  2089. return false;
  2090. }
  2091. #ifndef CONFIG_CAM_PRESIL
  2092. void __iomem * cam_soc_util_get_mem_base(
  2093. unsigned long mem_block_start,
  2094. unsigned long mem_block_size,
  2095. const char *mem_block_name,
  2096. uint32_t reserve_mem)
  2097. {
  2098. void __iomem * mem_base;
  2099. if (reserve_mem) {
  2100. if (!request_mem_region(mem_block_start,
  2101. mem_block_size,
  2102. mem_block_name)) {
  2103. CAM_ERR(CAM_UTIL,
  2104. "Error Mem region request Failed:%s",
  2105. mem_block_name);
  2106. return NULL;
  2107. }
  2108. }
  2109. mem_base = ioremap(mem_block_start, mem_block_size);
  2110. if (!mem_base) {
  2111. CAM_ERR(CAM_UTIL, "get mem base failed");
  2112. }
  2113. return mem_base;
  2114. }
  2115. int cam_soc_util_request_irq(struct device *dev,
  2116. unsigned int irq_line_start,
  2117. irq_handler_t handler,
  2118. unsigned long irqflags,
  2119. const char *irq_name,
  2120. void *irq_data,
  2121. unsigned long mem_block_start)
  2122. {
  2123. int rc;
  2124. rc = devm_request_irq(dev,
  2125. irq_line_start,
  2126. handler,
  2127. IRQF_TRIGGER_RISING,
  2128. irq_name,
  2129. irq_data);
  2130. if (rc) {
  2131. CAM_ERR(CAM_UTIL, "irq request fail rc %d", rc);
  2132. return -EBUSY;
  2133. }
  2134. disable_irq(irq_line_start);
  2135. return rc;
  2136. }
  2137. #else
  2138. void __iomem * cam_soc_util_get_mem_base(
  2139. unsigned long mem_block_start,
  2140. unsigned long mem_block_size,
  2141. const char *mem_block_name,
  2142. uint32_t reserve_mem)
  2143. {
  2144. void __iomem * mem_base;
  2145. if(cam_soc_util_is_presil_address_space(mem_block_start))
  2146. mem_base = (void __iomem *)mem_block_start;
  2147. else {
  2148. if (reserve_mem) {
  2149. if (!request_mem_region(mem_block_start,
  2150. mem_block_size,
  2151. mem_block_name)) {
  2152. CAM_ERR(CAM_UTIL,
  2153. "Error Mem region request Failed:%s",
  2154. mem_block_name);
  2155. return NULL;
  2156. }
  2157. }
  2158. mem_base = ioremap(mem_block_start, mem_block_size);
  2159. }
  2160. if (!mem_base) {
  2161. CAM_ERR(CAM_UTIL, "get mem base failed");
  2162. }
  2163. return mem_base;
  2164. }
  2165. int cam_soc_util_request_irq(struct device *dev,
  2166. unsigned int irq_line_start,
  2167. irq_handler_t handler,
  2168. unsigned long irqflags,
  2169. const char *irq_name,
  2170. void *irq_data,
  2171. unsigned long mem_block_start)
  2172. {
  2173. int rc;
  2174. if(cam_soc_util_is_presil_address_space(mem_block_start)) {
  2175. rc = devm_request_irq(dev,
  2176. irq_line_start,
  2177. handler,
  2178. irqflags,
  2179. irq_name,
  2180. irq_data);
  2181. if (rc) {
  2182. CAM_ERR(CAM_UTIL, "presil irq request fail");
  2183. return -EBUSY;
  2184. }
  2185. disable_irq(irq_line_start);
  2186. rc = !(cam_presil_subscribe_device_irq(irq_line_start,
  2187. handler, irq_data, irq_name));
  2188. CAM_DBG(CAM_PRESIL, "Subscribe presil IRQ: rc=%d NUM=%d Name=%s handler=0x%x",
  2189. rc, irq_line_start, irq_name, handler);
  2190. if (rc) {
  2191. CAM_ERR(CAM_UTIL, "presil irq request fail");
  2192. return -EBUSY;
  2193. }
  2194. } else {
  2195. rc = devm_request_irq(dev,
  2196. irq_line_start,
  2197. handler,
  2198. irqflags,
  2199. irq_name,
  2200. irq_data);
  2201. if (rc) {
  2202. CAM_ERR(CAM_UTIL, "irq request fail");
  2203. return -EBUSY;
  2204. }
  2205. disable_irq(irq_line_start);
  2206. CAM_INFO(CAM_UTIL, "Subscribe for non-presil IRQ success");
  2207. }
  2208. CAM_INFO(CAM_UTIL, "returning IRQ for mem_block_start 0x%0x rc %d",
  2209. mem_block_start, rc);
  2210. return rc;
  2211. }
  2212. #endif
  2213. int cam_soc_util_request_platform_resource(
  2214. struct cam_hw_soc_info *soc_info,
  2215. irq_handler_t handler, void *irq_data)
  2216. {
  2217. int i = 0, rc = 0;
  2218. if (!soc_info || !soc_info->dev) {
  2219. CAM_ERR(CAM_UTIL, "Invalid parameters");
  2220. return -EINVAL;
  2221. }
  2222. for (i = 0; i < soc_info->num_mem_block; i++) {
  2223. soc_info->reg_map[i].mem_base = cam_soc_util_get_mem_base(
  2224. soc_info->mem_block[i]->start,
  2225. resource_size(soc_info->mem_block[i]),
  2226. soc_info->mem_block_name[i],
  2227. soc_info->reserve_mem);
  2228. if (!soc_info->reg_map[i].mem_base) {
  2229. CAM_ERR(CAM_UTIL, "i= %d base NULL", i);
  2230. rc = -ENOMEM;
  2231. goto unmap_base;
  2232. }
  2233. soc_info->reg_map[i].mem_cam_base =
  2234. soc_info->mem_block_cam_base[i];
  2235. soc_info->reg_map[i].size =
  2236. resource_size(soc_info->mem_block[i]);
  2237. soc_info->num_reg_map++;
  2238. }
  2239. for (i = 0; i < soc_info->num_rgltr; i++) {
  2240. if (soc_info->rgltr_name[i] == NULL) {
  2241. CAM_ERR(CAM_UTIL, "can't find regulator name");
  2242. goto put_regulator;
  2243. }
  2244. rc = cam_soc_util_get_regulator(soc_info->dev,
  2245. &soc_info->rgltr[i],
  2246. soc_info->rgltr_name[i]);
  2247. if (rc)
  2248. goto put_regulator;
  2249. }
  2250. if (soc_info->irq_line) {
  2251. rc = cam_soc_util_request_irq(soc_info->dev,
  2252. soc_info->irq_line->start,
  2253. handler, IRQF_TRIGGER_RISING,
  2254. soc_info->irq_name, irq_data,
  2255. soc_info->mem_block[0]->start);
  2256. if (rc) {
  2257. CAM_ERR(CAM_UTIL, "irq request fail");
  2258. rc = -EBUSY;
  2259. goto put_regulator;
  2260. }
  2261. soc_info->irq_data = irq_data;
  2262. }
  2263. /* Get Clock */
  2264. for (i = 0; i < soc_info->num_clk; i++) {
  2265. soc_info->clk[i] = clk_get(soc_info->dev,
  2266. soc_info->clk_name[i]);
  2267. if (!soc_info->clk[i]) {
  2268. CAM_ERR(CAM_UTIL, "get failed for %s",
  2269. soc_info->clk_name[i]);
  2270. rc = -ENOENT;
  2271. goto put_clk;
  2272. }
  2273. /* Create a wrapper entry if this is a shared clock */
  2274. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i)) {
  2275. uint32_t min_level = soc_info->lowest_clk_level;
  2276. CAM_DBG(CAM_UTIL,
  2277. "Dev %s, clk %s, id %d register wrapper entry for shared clk",
  2278. soc_info->dev_name, soc_info->clk_name[i],
  2279. soc_info->clk_id[i]);
  2280. rc = cam_soc_util_clk_wrapper_register_entry(
  2281. soc_info->clk_id[i], soc_info->clk[i],
  2282. (i == soc_info->src_clk_idx) ? true : false,
  2283. soc_info, soc_info->clk_rate[min_level][i],
  2284. soc_info->clk_name[i]);
  2285. if (rc) {
  2286. CAM_ERR(CAM_UTIL,
  2287. "Failed in registering shared clk Dev %s id %d",
  2288. soc_info->dev_name,
  2289. soc_info->clk_id[i]);
  2290. clk_put(soc_info->clk[i]);
  2291. soc_info->clk[i] = NULL;
  2292. goto put_clk;
  2293. }
  2294. } else if (i == soc_info->src_clk_idx) {
  2295. rc = cam_soc_util_register_mmrm_client(
  2296. soc_info->clk_id[i], soc_info->clk[i],
  2297. soc_info->is_nrt_dev,
  2298. soc_info, soc_info->clk_name[i],
  2299. &soc_info->mmrm_handle);
  2300. if (rc) {
  2301. CAM_ERR(CAM_UTIL,
  2302. "Failed in register mmrm client Dev %s clk id %d",
  2303. soc_info->dev_name,
  2304. soc_info->clk_id[i]);
  2305. clk_put(soc_info->clk[i]);
  2306. soc_info->clk[i] = NULL;
  2307. goto put_clk;
  2308. }
  2309. }
  2310. }
  2311. rc = cam_soc_util_request_pinctrl(soc_info);
  2312. if (rc) {
  2313. CAM_ERR(CAM_UTIL, "Failed in requesting Pinctrl, rc: %d", rc);
  2314. goto put_clk;
  2315. }
  2316. rc = cam_soc_util_request_gpio_table(soc_info, true);
  2317. if (rc) {
  2318. CAM_ERR(CAM_UTIL, "Failed in request gpio table, rc=%d", rc);
  2319. goto put_clk;
  2320. }
  2321. if (soc_info->clk_control_enable)
  2322. cam_soc_util_create_clk_lvl_debugfs(soc_info);
  2323. return rc;
  2324. put_clk:
  2325. if (soc_info->mmrm_handle) {
  2326. cam_soc_util_unregister_mmrm_client(soc_info->mmrm_handle);
  2327. soc_info->mmrm_handle = NULL;
  2328. }
  2329. if (i == -1)
  2330. i = soc_info->num_clk;
  2331. for (i = i - 1; i >= 0; i--) {
  2332. if (soc_info->clk[i]) {
  2333. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i))
  2334. cam_soc_util_clk_wrapper_unregister_entry(
  2335. soc_info->clk_id[i], soc_info);
  2336. clk_put(soc_info->clk[i]);
  2337. soc_info->clk[i] = NULL;
  2338. }
  2339. }
  2340. if (soc_info->irq_line) {
  2341. disable_irq(soc_info->irq_line->start);
  2342. devm_free_irq(soc_info->dev,
  2343. soc_info->irq_line->start, irq_data);
  2344. }
  2345. put_regulator:
  2346. if (i == -1)
  2347. i = soc_info->num_rgltr;
  2348. for (i = i - 1; i >= 0; i--) {
  2349. if (soc_info->rgltr[i]) {
  2350. regulator_disable(soc_info->rgltr[i]);
  2351. regulator_put(soc_info->rgltr[i]);
  2352. soc_info->rgltr[i] = NULL;
  2353. }
  2354. }
  2355. unmap_base:
  2356. if (i == -1)
  2357. i = soc_info->num_reg_map;
  2358. for (i = i - 1; i >= 0; i--) {
  2359. if (soc_info->reserve_mem)
  2360. release_mem_region(soc_info->mem_block[i]->start,
  2361. resource_size(soc_info->mem_block[i]));
  2362. iounmap(soc_info->reg_map[i].mem_base);
  2363. soc_info->reg_map[i].mem_base = NULL;
  2364. soc_info->reg_map[i].size = 0;
  2365. }
  2366. return rc;
  2367. }
  2368. int cam_soc_util_release_platform_resource(struct cam_hw_soc_info *soc_info)
  2369. {
  2370. int i;
  2371. bool b_ret = false;
  2372. if (!soc_info || !soc_info->dev) {
  2373. CAM_ERR(CAM_UTIL, "Invalid parameter");
  2374. return -EINVAL;
  2375. }
  2376. if (soc_info->mmrm_handle) {
  2377. cam_soc_util_unregister_mmrm_client(soc_info->mmrm_handle);
  2378. soc_info->mmrm_handle = NULL;
  2379. }
  2380. for (i = soc_info->num_clk - 1; i >= 0; i--) {
  2381. if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i))
  2382. cam_soc_util_clk_wrapper_unregister_entry(
  2383. soc_info->clk_id[i], soc_info);
  2384. clk_put(soc_info->clk[i]);
  2385. soc_info->clk[i] = NULL;
  2386. }
  2387. for (i = soc_info->num_rgltr - 1; i >= 0; i--) {
  2388. if (soc_info->rgltr[i]) {
  2389. regulator_put(soc_info->rgltr[i]);
  2390. soc_info->rgltr[i] = NULL;
  2391. }
  2392. }
  2393. for (i = soc_info->num_reg_map - 1; i >= 0; i--) {
  2394. iounmap(soc_info->reg_map[i].mem_base);
  2395. soc_info->reg_map[i].mem_base = NULL;
  2396. soc_info->reg_map[i].size = 0;
  2397. }
  2398. if (soc_info->irq_line) {
  2399. if (cam_presil_mode_enabled()) {
  2400. if (cam_soc_util_is_presil_address_space(soc_info->mem_block[0]->start)) {
  2401. b_ret = cam_presil_unsubscribe_device_irq(
  2402. soc_info->irq_line->start);
  2403. CAM_DBG(CAM_PRESIL, "UnSubscribe IRQ: Ret=%d NUM=%d Name=%s",
  2404. b_ret, soc_info->irq_line->start, soc_info->irq_name);
  2405. }
  2406. }
  2407. disable_irq(soc_info->irq_line->start);
  2408. devm_free_irq(soc_info->dev,
  2409. soc_info->irq_line->start, soc_info->irq_data);
  2410. }
  2411. cam_soc_util_release_pinctrl(soc_info);
  2412. /* release for gpio */
  2413. cam_soc_util_request_gpio_table(soc_info, false);
  2414. soc_info->dentry = NULL;
  2415. return 0;
  2416. }
  2417. int cam_soc_util_enable_platform_resource(struct cam_hw_soc_info *soc_info,
  2418. bool enable_clocks, enum cam_vote_level clk_level, bool enable_irq)
  2419. {
  2420. int rc = 0;
  2421. if (!soc_info)
  2422. return -EINVAL;
  2423. rc = cam_soc_util_regulator_enable_default(soc_info);
  2424. if (rc) {
  2425. CAM_ERR(CAM_UTIL, "Regulators enable failed");
  2426. return rc;
  2427. }
  2428. if (enable_clocks) {
  2429. rc = cam_soc_util_clk_enable_default(soc_info, clk_level);
  2430. if (rc)
  2431. goto disable_regulator;
  2432. }
  2433. if (enable_irq) {
  2434. rc = cam_soc_util_irq_enable(soc_info);
  2435. if (rc)
  2436. goto disable_clk;
  2437. }
  2438. return rc;
  2439. disable_clk:
  2440. if (enable_clocks)
  2441. cam_soc_util_clk_disable_default(soc_info);
  2442. disable_regulator:
  2443. cam_soc_util_regulator_disable_default(soc_info);
  2444. return rc;
  2445. }
  2446. int cam_soc_util_disable_platform_resource(struct cam_hw_soc_info *soc_info,
  2447. bool disable_clocks, bool disable_irq)
  2448. {
  2449. int rc = 0;
  2450. if (!soc_info)
  2451. return -EINVAL;
  2452. if (disable_irq)
  2453. rc |= cam_soc_util_irq_disable(soc_info);
  2454. if (disable_clocks)
  2455. cam_soc_util_clk_disable_default(soc_info);
  2456. cam_soc_util_regulator_disable_default(soc_info);
  2457. return rc;
  2458. }
  2459. int cam_soc_util_reg_dump(struct cam_hw_soc_info *soc_info,
  2460. uint32_t base_index, uint32_t offset, int size)
  2461. {
  2462. void __iomem *base_addr = NULL;
  2463. CAM_DBG(CAM_UTIL, "base_idx %u size=%d", base_index, size);
  2464. if (!soc_info || base_index >= soc_info->num_reg_map ||
  2465. size <= 0 || (offset + size) >=
  2466. CAM_SOC_GET_REG_MAP_SIZE(soc_info, base_index))
  2467. return -EINVAL;
  2468. base_addr = CAM_SOC_GET_REG_MAP_START(soc_info, base_index);
  2469. /*
  2470. * All error checking already done above,
  2471. * hence ignoring the return value below.
  2472. */
  2473. cam_io_dump(base_addr, offset, size);
  2474. return 0;
  2475. }
  2476. static int cam_soc_util_dump_cont_reg_range(
  2477. struct cam_hw_soc_info *soc_info,
  2478. struct cam_reg_range_read_desc *reg_read, uint32_t base_idx,
  2479. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  2480. {
  2481. int i = 0, rc = 0;
  2482. uint32_t write_idx = 0;
  2483. if (!soc_info || !dump_out_buf || !reg_read || !cmd_buf_end) {
  2484. CAM_ERR(CAM_UTIL,
  2485. "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK cmd_buf_end: %pK",
  2486. soc_info, dump_out_buf, reg_read, cmd_buf_end);
  2487. rc = -EINVAL;
  2488. goto end;
  2489. }
  2490. if ((reg_read->num_values) && ((reg_read->num_values > U32_MAX / 2) ||
  2491. (sizeof(uint32_t) > ((U32_MAX -
  2492. sizeof(struct cam_reg_dump_out_buffer) -
  2493. dump_out_buf->bytes_written) /
  2494. (reg_read->num_values * 2))))) {
  2495. CAM_ERR(CAM_UTIL,
  2496. "Integer Overflow bytes_written: [%u] num_values: [%u]",
  2497. dump_out_buf->bytes_written, reg_read->num_values);
  2498. rc = -EOVERFLOW;
  2499. goto end;
  2500. }
  2501. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  2502. (uintptr_t)(sizeof(struct cam_reg_dump_out_buffer)
  2503. - sizeof(uint32_t) + dump_out_buf->bytes_written +
  2504. (reg_read->num_values * 2 * sizeof(uint32_t)))) {
  2505. CAM_ERR(CAM_UTIL,
  2506. "Insufficient space in out buffer num_values: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  2507. reg_read->num_values, cmd_buf_end,
  2508. (uintptr_t)dump_out_buf);
  2509. rc = -EINVAL;
  2510. goto end;
  2511. }
  2512. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  2513. for (i = 0; i < reg_read->num_values; i++) {
  2514. if ((reg_read->offset + (i * sizeof(uint32_t))) >
  2515. (uint32_t)soc_info->reg_map[base_idx].size) {
  2516. CAM_ERR(CAM_UTIL,
  2517. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  2518. (reg_read->offset + (i * sizeof(uint32_t))),
  2519. (uint32_t)soc_info->reg_map[base_idx].size);
  2520. rc = -EINVAL;
  2521. goto end;
  2522. }
  2523. dump_out_buf->dump_data[write_idx++] = reg_read->offset +
  2524. (i * sizeof(uint32_t));
  2525. dump_out_buf->dump_data[write_idx++] =
  2526. cam_soc_util_r(soc_info, base_idx,
  2527. (reg_read->offset + (i * sizeof(uint32_t))));
  2528. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  2529. }
  2530. end:
  2531. return rc;
  2532. }
  2533. static int cam_soc_util_dump_dmi_reg_range(
  2534. struct cam_hw_soc_info *soc_info,
  2535. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  2536. struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
  2537. {
  2538. int i = 0, rc = 0;
  2539. uint32_t write_idx = 0;
  2540. if (!soc_info || !dump_out_buf || !dmi_read || !cmd_buf_end) {
  2541. CAM_ERR(CAM_UTIL,
  2542. "Invalid input args soc_info: %pK, dump_out_buffer: %pK",
  2543. soc_info, dump_out_buf);
  2544. rc = -EINVAL;
  2545. goto end;
  2546. }
  2547. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  2548. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  2549. CAM_ERR(CAM_UTIL,
  2550. "Invalid number of requested writes, pre: %d post: %d",
  2551. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  2552. rc = -EINVAL;
  2553. goto end;
  2554. }
  2555. if ((dmi_read->num_pre_writes + dmi_read->dmi_data_read.num_values)
  2556. && ((dmi_read->num_pre_writes > U32_MAX / 2) ||
  2557. (dmi_read->dmi_data_read.num_values > U32_MAX / 2) ||
  2558. ((dmi_read->num_pre_writes * 2) > U32_MAX -
  2559. (dmi_read->dmi_data_read.num_values * 2)) ||
  2560. (sizeof(uint32_t) > ((U32_MAX -
  2561. sizeof(struct cam_reg_dump_out_buffer) -
  2562. dump_out_buf->bytes_written) / ((dmi_read->num_pre_writes +
  2563. dmi_read->dmi_data_read.num_values) * 2))))) {
  2564. CAM_ERR(CAM_UTIL,
  2565. "Integer Overflow bytes_written: [%u] num_pre_writes: [%u] num_values: [%u]",
  2566. dump_out_buf->bytes_written, dmi_read->num_pre_writes,
  2567. dmi_read->dmi_data_read.num_values);
  2568. rc = -EOVERFLOW;
  2569. goto end;
  2570. }
  2571. if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
  2572. (uintptr_t)(
  2573. sizeof(struct cam_reg_dump_out_buffer) - sizeof(uint32_t) +
  2574. (dump_out_buf->bytes_written +
  2575. (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
  2576. (dmi_read->dmi_data_read.num_values * 2 *
  2577. sizeof(uint32_t))))) {
  2578. CAM_ERR(CAM_UTIL,
  2579. "Insufficient space in out buffer num_read_val: [%d] num_write_val: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
  2580. dmi_read->dmi_data_read.num_values,
  2581. dmi_read->num_pre_writes, cmd_buf_end,
  2582. (uintptr_t)dump_out_buf);
  2583. rc = -EINVAL;
  2584. goto end;
  2585. }
  2586. write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
  2587. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  2588. if (dmi_read->pre_read_config[i].offset >
  2589. (uint32_t)soc_info->reg_map[base_idx].size) {
  2590. CAM_ERR(CAM_UTIL,
  2591. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  2592. dmi_read->pre_read_config[i].offset,
  2593. (uint32_t)soc_info->reg_map[base_idx].size);
  2594. rc = -EINVAL;
  2595. goto end;
  2596. }
  2597. cam_soc_util_w_mb(soc_info, base_idx,
  2598. dmi_read->pre_read_config[i].offset,
  2599. dmi_read->pre_read_config[i].value);
  2600. dump_out_buf->dump_data[write_idx++] =
  2601. dmi_read->pre_read_config[i].offset;
  2602. dump_out_buf->dump_data[write_idx++] =
  2603. dmi_read->pre_read_config[i].value;
  2604. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  2605. }
  2606. if (dmi_read->dmi_data_read.offset >
  2607. (uint32_t)soc_info->reg_map[base_idx].size) {
  2608. CAM_ERR(CAM_UTIL,
  2609. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  2610. dmi_read->dmi_data_read.offset,
  2611. (uint32_t)soc_info->reg_map[base_idx].size);
  2612. rc = -EINVAL;
  2613. goto end;
  2614. }
  2615. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  2616. dump_out_buf->dump_data[write_idx++] =
  2617. dmi_read->dmi_data_read.offset;
  2618. dump_out_buf->dump_data[write_idx++] =
  2619. cam_soc_util_r_mb(soc_info, base_idx,
  2620. dmi_read->dmi_data_read.offset);
  2621. dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
  2622. }
  2623. for (i = 0; i < dmi_read->num_post_writes; i++) {
  2624. if (dmi_read->post_read_config[i].offset >
  2625. (uint32_t)soc_info->reg_map[base_idx].size) {
  2626. CAM_ERR(CAM_UTIL,
  2627. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  2628. dmi_read->post_read_config[i].offset,
  2629. (uint32_t)soc_info->reg_map[base_idx].size);
  2630. rc = -EINVAL;
  2631. goto end;
  2632. }
  2633. cam_soc_util_w_mb(soc_info, base_idx,
  2634. dmi_read->post_read_config[i].offset,
  2635. dmi_read->post_read_config[i].value);
  2636. }
  2637. end:
  2638. return rc;
  2639. }
  2640. static int cam_soc_util_dump_dmi_reg_range_user_buf(
  2641. struct cam_hw_soc_info *soc_info,
  2642. struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
  2643. struct cam_hw_soc_dump_args *dump_args)
  2644. {
  2645. int i;
  2646. int rc;
  2647. size_t buf_len = 0;
  2648. uint8_t *dst;
  2649. size_t remain_len;
  2650. uint32_t min_len;
  2651. uint32_t *waddr, *start;
  2652. uintptr_t cpu_addr;
  2653. struct cam_hw_soc_dump_header *hdr;
  2654. if (!soc_info || !dump_args || !dmi_read) {
  2655. CAM_ERR(CAM_UTIL,
  2656. "Invalid input args soc_info: %pK, dump_args: %pK",
  2657. soc_info, dump_args);
  2658. rc = -EINVAL;
  2659. goto end;
  2660. }
  2661. if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
  2662. dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
  2663. CAM_ERR(CAM_UTIL,
  2664. "Invalid number of requested writes, pre: %d post: %d",
  2665. dmi_read->num_pre_writes, dmi_read->num_post_writes);
  2666. rc = -EINVAL;
  2667. goto end;
  2668. }
  2669. rc = cam_mem_get_cpu_buf(dump_args->buf_handle, &cpu_addr, &buf_len);
  2670. if (rc) {
  2671. CAM_ERR(CAM_UTIL, "Invalid handle %u rc %d",
  2672. dump_args->buf_handle, rc);
  2673. goto end;
  2674. }
  2675. if (buf_len <= dump_args->offset) {
  2676. CAM_WARN(CAM_UTIL, "Dump offset overshoot offset %zu len %zu",
  2677. dump_args->offset, buf_len);
  2678. rc = -ENOSPC;
  2679. goto end;
  2680. }
  2681. remain_len = buf_len - dump_args->offset;
  2682. min_len = (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
  2683. (dmi_read->dmi_data_read.num_values * 2 * sizeof(uint32_t)) +
  2684. sizeof(uint32_t);
  2685. if (remain_len < min_len) {
  2686. CAM_WARN(CAM_UTIL,
  2687. "Dump Buffer exhaust read %d write %d remain %zu min %u",
  2688. dmi_read->dmi_data_read.num_values,
  2689. dmi_read->num_pre_writes, remain_len,
  2690. min_len);
  2691. rc = -ENOSPC;
  2692. goto end;
  2693. }
  2694. dst = (uint8_t *)cpu_addr + dump_args->offset;
  2695. hdr = (struct cam_hw_soc_dump_header *)dst;
  2696. memset(hdr, 0, sizeof(struct cam_hw_soc_dump_header));
  2697. scnprintf(hdr->tag, CAM_SOC_HW_DUMP_TAG_MAX_LEN,
  2698. "DMI_DUMP:");
  2699. waddr = (uint32_t *)(dst + sizeof(struct cam_hw_soc_dump_header));
  2700. start = waddr;
  2701. hdr->word_size = sizeof(uint32_t);
  2702. *waddr = soc_info->index;
  2703. waddr++;
  2704. for (i = 0; i < dmi_read->num_pre_writes; i++) {
  2705. if (dmi_read->pre_read_config[i].offset >
  2706. (uint32_t)soc_info->reg_map[base_idx].size) {
  2707. CAM_ERR(CAM_UTIL,
  2708. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  2709. dmi_read->pre_read_config[i].offset,
  2710. (uint32_t)soc_info->reg_map[base_idx].size);
  2711. rc = -EINVAL;
  2712. goto end;
  2713. }
  2714. cam_soc_util_w_mb(soc_info, base_idx,
  2715. dmi_read->pre_read_config[i].offset,
  2716. dmi_read->pre_read_config[i].value);
  2717. *waddr++ = dmi_read->pre_read_config[i].offset;
  2718. *waddr++ = dmi_read->pre_read_config[i].value;
  2719. }
  2720. if (dmi_read->dmi_data_read.offset >
  2721. (uint32_t)soc_info->reg_map[base_idx].size) {
  2722. CAM_ERR(CAM_UTIL,
  2723. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  2724. dmi_read->dmi_data_read.offset,
  2725. (uint32_t)soc_info->reg_map[base_idx].size);
  2726. rc = -EINVAL;
  2727. goto end;
  2728. }
  2729. for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
  2730. *waddr++ = dmi_read->dmi_data_read.offset;
  2731. *waddr++ = cam_soc_util_r_mb(soc_info, base_idx,
  2732. dmi_read->dmi_data_read.offset);
  2733. }
  2734. for (i = 0; i < dmi_read->num_post_writes; i++) {
  2735. if (dmi_read->post_read_config[i].offset >
  2736. (uint32_t)soc_info->reg_map[base_idx].size) {
  2737. CAM_ERR(CAM_UTIL,
  2738. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  2739. dmi_read->post_read_config[i].offset,
  2740. (uint32_t)soc_info->reg_map[base_idx].size);
  2741. rc = -EINVAL;
  2742. goto end;
  2743. }
  2744. cam_soc_util_w_mb(soc_info, base_idx,
  2745. dmi_read->post_read_config[i].offset,
  2746. dmi_read->post_read_config[i].value);
  2747. }
  2748. hdr->size = (waddr - start) * hdr->word_size;
  2749. dump_args->offset += hdr->size +
  2750. sizeof(struct cam_hw_soc_dump_header);
  2751. end:
  2752. return rc;
  2753. }
  2754. static int cam_soc_util_dump_cont_reg_range_user_buf(
  2755. struct cam_hw_soc_info *soc_info,
  2756. struct cam_reg_range_read_desc *reg_read,
  2757. uint32_t base_idx,
  2758. struct cam_hw_soc_dump_args *dump_args)
  2759. {
  2760. int i;
  2761. int rc = 0;
  2762. size_t buf_len;
  2763. uint8_t *dst;
  2764. size_t remain_len;
  2765. uint32_t min_len;
  2766. uint32_t *waddr, *start;
  2767. uintptr_t cpu_addr;
  2768. struct cam_hw_soc_dump_header *hdr;
  2769. if (!soc_info || !dump_args || !reg_read) {
  2770. CAM_ERR(CAM_UTIL,
  2771. "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK",
  2772. soc_info, dump_args, reg_read);
  2773. rc = -EINVAL;
  2774. goto end;
  2775. }
  2776. rc = cam_mem_get_cpu_buf(dump_args->buf_handle, &cpu_addr, &buf_len);
  2777. if (rc) {
  2778. CAM_ERR(CAM_UTIL, "Invalid handle %u rc %d",
  2779. dump_args->buf_handle, rc);
  2780. goto end;
  2781. }
  2782. if (buf_len <= dump_args->offset) {
  2783. CAM_WARN(CAM_UTIL, "Dump offset overshoot %zu %zu",
  2784. dump_args->offset, buf_len);
  2785. rc = -ENOSPC;
  2786. goto end;
  2787. }
  2788. remain_len = buf_len - dump_args->offset;
  2789. min_len = (reg_read->num_values * 2 * sizeof(uint32_t)) +
  2790. sizeof(struct cam_hw_soc_dump_header) + sizeof(uint32_t);
  2791. if (remain_len < min_len) {
  2792. CAM_WARN(CAM_UTIL,
  2793. "Dump Buffer exhaust read_values %d remain %zu min %u",
  2794. reg_read->num_values,
  2795. remain_len,
  2796. min_len);
  2797. rc = -ENOSPC;
  2798. goto end;
  2799. }
  2800. dst = (uint8_t *)cpu_addr + dump_args->offset;
  2801. hdr = (struct cam_hw_soc_dump_header *)dst;
  2802. memset(hdr, 0, sizeof(struct cam_hw_soc_dump_header));
  2803. scnprintf(hdr->tag, CAM_SOC_HW_DUMP_TAG_MAX_LEN, "%s_REG:",
  2804. soc_info->dev_name);
  2805. waddr = (uint32_t *)(dst + sizeof(struct cam_hw_soc_dump_header));
  2806. start = waddr;
  2807. hdr->word_size = sizeof(uint32_t);
  2808. *waddr = soc_info->index;
  2809. waddr++;
  2810. for (i = 0; i < reg_read->num_values; i++) {
  2811. if ((reg_read->offset + (i * sizeof(uint32_t))) >
  2812. (uint32_t)soc_info->reg_map[base_idx].size) {
  2813. CAM_ERR(CAM_UTIL,
  2814. "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
  2815. (reg_read->offset + (i * sizeof(uint32_t))),
  2816. (uint32_t)soc_info->reg_map[base_idx].size);
  2817. rc = -EINVAL;
  2818. goto end;
  2819. }
  2820. waddr[0] = reg_read->offset + (i * sizeof(uint32_t));
  2821. waddr[1] = cam_soc_util_r(soc_info, base_idx,
  2822. (reg_read->offset + (i * sizeof(uint32_t))));
  2823. waddr += 2;
  2824. }
  2825. hdr->size = (waddr - start) * hdr->word_size;
  2826. dump_args->offset += hdr->size +
  2827. sizeof(struct cam_hw_soc_dump_header);
  2828. end:
  2829. return rc;
  2830. }
  2831. static int cam_soc_util_user_reg_dump(
  2832. struct cam_reg_dump_desc *reg_dump_desc,
  2833. struct cam_hw_soc_dump_args *dump_args,
  2834. struct cam_hw_soc_info *soc_info,
  2835. uint32_t reg_base_idx)
  2836. {
  2837. int rc = 0;
  2838. int i;
  2839. struct cam_reg_read_info *reg_read_info = NULL;
  2840. if (!dump_args || !reg_dump_desc || !soc_info) {
  2841. CAM_ERR(CAM_UTIL,
  2842. "Invalid input parameters %pK %pK %pK",
  2843. dump_args, reg_dump_desc, soc_info);
  2844. return -EINVAL;
  2845. }
  2846. for (i = 0; i < reg_dump_desc->num_read_range; i++) {
  2847. reg_read_info = &reg_dump_desc->read_range[i];
  2848. if (reg_read_info->type ==
  2849. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  2850. rc = cam_soc_util_dump_cont_reg_range_user_buf(
  2851. soc_info,
  2852. &reg_read_info->reg_read,
  2853. reg_base_idx,
  2854. dump_args);
  2855. } else if (reg_read_info->type ==
  2856. CAM_REG_DUMP_READ_TYPE_DMI) {
  2857. rc = cam_soc_util_dump_dmi_reg_range_user_buf(
  2858. soc_info,
  2859. &reg_read_info->dmi_read,
  2860. reg_base_idx,
  2861. dump_args);
  2862. } else {
  2863. CAM_ERR(CAM_UTIL,
  2864. "Invalid Reg dump read type: %d",
  2865. reg_read_info->type);
  2866. rc = -EINVAL;
  2867. goto end;
  2868. }
  2869. if (rc) {
  2870. CAM_ERR(CAM_UTIL,
  2871. "Reg range read failed rc: %d reg_base_idx: %d",
  2872. rc, reg_base_idx);
  2873. goto end;
  2874. }
  2875. }
  2876. end:
  2877. return rc;
  2878. }
  2879. int cam_soc_util_reg_dump_to_cmd_buf(void *ctx,
  2880. struct cam_cmd_buf_desc *cmd_desc, uint64_t req_id,
  2881. cam_soc_util_regspace_data_cb reg_data_cb,
  2882. struct cam_hw_soc_dump_args *soc_dump_args,
  2883. bool user_triggered_dump)
  2884. {
  2885. int rc = 0, i, j;
  2886. uintptr_t cpu_addr = 0;
  2887. uintptr_t cmd_buf_start = 0;
  2888. uintptr_t cmd_in_data_end = 0;
  2889. uintptr_t cmd_buf_end = 0;
  2890. uint32_t reg_base_type = 0;
  2891. size_t buf_size = 0, remain_len = 0;
  2892. struct cam_reg_dump_input_info *reg_input_info = NULL;
  2893. struct cam_reg_dump_desc *reg_dump_desc = NULL;
  2894. struct cam_reg_dump_out_buffer *dump_out_buf = NULL;
  2895. struct cam_reg_read_info *reg_read_info = NULL;
  2896. struct cam_hw_soc_info *soc_info;
  2897. uint32_t reg_base_idx = 0;
  2898. if (!ctx || !cmd_desc || !reg_data_cb) {
  2899. CAM_ERR(CAM_UTIL, "Invalid args to reg dump [%pK] [%pK]",
  2900. cmd_desc, reg_data_cb);
  2901. return -EINVAL;
  2902. }
  2903. if (!cmd_desc->length || !cmd_desc->size) {
  2904. CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
  2905. cmd_desc->length, cmd_desc->size);
  2906. return -EINVAL;
  2907. }
  2908. rc = cam_mem_get_cpu_buf(cmd_desc->mem_handle, &cpu_addr, &buf_size);
  2909. if (rc || !cpu_addr || (buf_size == 0)) {
  2910. CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
  2911. rc, (void *)cpu_addr);
  2912. goto end;
  2913. }
  2914. CAM_DBG(CAM_UTIL, "Get cpu buf success req_id: %llu buf_size: %zu",
  2915. req_id, buf_size);
  2916. if ((buf_size < sizeof(uint32_t)) ||
  2917. ((size_t)cmd_desc->offset > (buf_size - sizeof(uint32_t)))) {
  2918. CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
  2919. (size_t)cmd_desc->offset);
  2920. rc = -EINVAL;
  2921. goto end;
  2922. }
  2923. remain_len = buf_size - (size_t)cmd_desc->offset;
  2924. if ((remain_len < (size_t)cmd_desc->size) || (cmd_desc->size <
  2925. cmd_desc->length)) {
  2926. CAM_ERR(CAM_UTIL,
  2927. "Invalid params for cmd buf len: %zu size: %zu remain_len: %zu",
  2928. (size_t)cmd_desc->length, (size_t)cmd_desc->length,
  2929. remain_len);
  2930. rc = -EINVAL;
  2931. goto end;
  2932. }
  2933. cmd_buf_start = cpu_addr + (uintptr_t)cmd_desc->offset;
  2934. cmd_in_data_end = cmd_buf_start + (uintptr_t)cmd_desc->length;
  2935. cmd_buf_end = cmd_buf_start + (uintptr_t)cmd_desc->size;
  2936. if ((cmd_buf_end <= cmd_buf_start) ||
  2937. (cmd_in_data_end <= cmd_buf_start)) {
  2938. CAM_ERR(CAM_UTIL,
  2939. "Invalid length or size for cmd buf: [%zu] [%zu]",
  2940. (size_t)cmd_desc->length, (size_t)cmd_desc->size);
  2941. rc = -EINVAL;
  2942. goto end;
  2943. }
  2944. CAM_DBG(CAM_UTIL,
  2945. "Buffer params start [%pK] input_end [%pK] buf_end [%pK]",
  2946. cmd_buf_start, cmd_in_data_end, cmd_buf_end);
  2947. reg_input_info = (struct cam_reg_dump_input_info *) cmd_buf_start;
  2948. if ((reg_input_info->num_dump_sets > 1) && (sizeof(uint32_t) >
  2949. ((U32_MAX - sizeof(struct cam_reg_dump_input_info)) /
  2950. (reg_input_info->num_dump_sets - 1)))) {
  2951. CAM_ERR(CAM_UTIL,
  2952. "Integer Overflow req_id: [%llu] num_dump_sets: [%u]",
  2953. req_id, reg_input_info->num_dump_sets);
  2954. rc = -EOVERFLOW;
  2955. goto end;
  2956. }
  2957. if ((!reg_input_info->num_dump_sets) ||
  2958. ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  2959. (sizeof(struct cam_reg_dump_input_info) +
  2960. ((reg_input_info->num_dump_sets - 1) * sizeof(uint32_t))))) {
  2961. CAM_ERR(CAM_UTIL,
  2962. "Invalid number of dump sets, req_id: [%llu] num_dump_sets: [%u]",
  2963. req_id, reg_input_info->num_dump_sets);
  2964. rc = -EINVAL;
  2965. goto end;
  2966. }
  2967. CAM_DBG(CAM_UTIL,
  2968. "reg_input_info req_id: %llu ctx %pK num_dump_sets: %d",
  2969. req_id, ctx, reg_input_info->num_dump_sets);
  2970. for (i = 0; i < reg_input_info->num_dump_sets; i++) {
  2971. if ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
  2972. reg_input_info->dump_set_offsets[i]) {
  2973. CAM_ERR(CAM_UTIL,
  2974. "Invalid dump set offset: [%pK], cmd_buf_start: [%pK] cmd_in_data_end: [%pK]",
  2975. (uintptr_t)reg_input_info->dump_set_offsets[i],
  2976. cmd_buf_start, cmd_in_data_end);
  2977. rc = -EINVAL;
  2978. goto end;
  2979. }
  2980. reg_dump_desc = (struct cam_reg_dump_desc *)
  2981. (cmd_buf_start +
  2982. (uintptr_t)reg_input_info->dump_set_offsets[i]);
  2983. if ((reg_dump_desc->num_read_range > 1) &&
  2984. (sizeof(struct cam_reg_read_info) > ((U32_MAX -
  2985. sizeof(struct cam_reg_dump_desc)) /
  2986. (reg_dump_desc->num_read_range - 1)))) {
  2987. CAM_ERR(CAM_UTIL,
  2988. "Integer Overflow req_id: [%llu] num_read_range: [%u]",
  2989. req_id, reg_dump_desc->num_read_range);
  2990. rc = -EOVERFLOW;
  2991. goto end;
  2992. }
  2993. if ((!reg_dump_desc->num_read_range) ||
  2994. ((cmd_in_data_end - (uintptr_t)reg_dump_desc) <=
  2995. (uintptr_t)(sizeof(struct cam_reg_dump_desc) +
  2996. ((reg_dump_desc->num_read_range - 1) *
  2997. sizeof(struct cam_reg_read_info))))) {
  2998. CAM_ERR(CAM_UTIL,
  2999. "Invalid number of read ranges, req_id: [%llu] num_read_range: [%d]",
  3000. req_id, reg_dump_desc->num_read_range);
  3001. rc = -EINVAL;
  3002. goto end;
  3003. }
  3004. if ((cmd_buf_end - cmd_buf_start) <= (uintptr_t)
  3005. (reg_dump_desc->dump_buffer_offset +
  3006. sizeof(struct cam_reg_dump_out_buffer))) {
  3007. CAM_ERR(CAM_UTIL,
  3008. "Invalid out buffer offset: [%pK], cmd_buf_start: [%pK] cmd_buf_end: [%pK]",
  3009. (uintptr_t)reg_dump_desc->dump_buffer_offset,
  3010. cmd_buf_start, cmd_buf_end);
  3011. rc = -EINVAL;
  3012. goto end;
  3013. }
  3014. reg_base_type = reg_dump_desc->reg_base_type;
  3015. if (reg_base_type == 0 || reg_base_type >
  3016. CAM_REG_DUMP_BASE_TYPE_SFE_RIGHT) {
  3017. CAM_ERR(CAM_UTIL,
  3018. "Invalid Reg dump base type: %d",
  3019. reg_base_type);
  3020. rc = -EINVAL;
  3021. goto end;
  3022. }
  3023. rc = reg_data_cb(reg_base_type, ctx, &soc_info, &reg_base_idx);
  3024. if (rc || !soc_info) {
  3025. CAM_ERR(CAM_UTIL,
  3026. "Reg space data callback failed rc: %d soc_info: [%pK]",
  3027. rc, soc_info);
  3028. rc = -EINVAL;
  3029. goto end;
  3030. }
  3031. if (reg_base_idx > soc_info->num_reg_map) {
  3032. CAM_ERR(CAM_UTIL,
  3033. "Invalid reg base idx: %d num reg map: %d",
  3034. reg_base_idx, soc_info->num_reg_map);
  3035. rc = -EINVAL;
  3036. goto end;
  3037. }
  3038. CAM_DBG(CAM_UTIL,
  3039. "Reg data callback success req_id: %llu base_type: %d base_idx: %d num_read_range: %d",
  3040. req_id, reg_base_type, reg_base_idx,
  3041. reg_dump_desc->num_read_range);
  3042. /* If the dump request is triggered by user space
  3043. * buffer will be different from the buffer which is received
  3044. * in init packet. In this case, dump the data to the
  3045. * user provided buffer and exit.
  3046. */
  3047. if (user_triggered_dump) {
  3048. rc = cam_soc_util_user_reg_dump(reg_dump_desc,
  3049. soc_dump_args, soc_info, reg_base_idx);
  3050. CAM_INFO(CAM_UTIL,
  3051. "%s reg_base_idx %d dumped offset %u",
  3052. soc_info->dev_name, reg_base_idx,
  3053. soc_dump_args->offset);
  3054. goto end;
  3055. }
  3056. /* Below code is executed when data is dumped to the
  3057. * out buffer received in init packet
  3058. */
  3059. dump_out_buf = (struct cam_reg_dump_out_buffer *)
  3060. (cmd_buf_start +
  3061. (uintptr_t)reg_dump_desc->dump_buffer_offset);
  3062. dump_out_buf->req_id = req_id;
  3063. dump_out_buf->bytes_written = 0;
  3064. for (j = 0; j < reg_dump_desc->num_read_range; j++) {
  3065. CAM_DBG(CAM_UTIL,
  3066. "Number of bytes written to cmd buffer: %u req_id: %llu",
  3067. dump_out_buf->bytes_written, req_id);
  3068. reg_read_info = &reg_dump_desc->read_range[j];
  3069. if (reg_read_info->type ==
  3070. CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
  3071. rc = cam_soc_util_dump_cont_reg_range(soc_info,
  3072. &reg_read_info->reg_read, reg_base_idx,
  3073. dump_out_buf, cmd_buf_end);
  3074. } else if (reg_read_info->type ==
  3075. CAM_REG_DUMP_READ_TYPE_DMI) {
  3076. rc = cam_soc_util_dump_dmi_reg_range(soc_info,
  3077. &reg_read_info->dmi_read, reg_base_idx,
  3078. dump_out_buf, cmd_buf_end);
  3079. } else {
  3080. CAM_ERR(CAM_UTIL,
  3081. "Invalid Reg dump read type: %d",
  3082. reg_read_info->type);
  3083. rc = -EINVAL;
  3084. goto end;
  3085. }
  3086. if (rc) {
  3087. CAM_ERR(CAM_UTIL,
  3088. "Reg range read failed rc: %d reg_base_idx: %d dump_out_buf: %pK",
  3089. rc, reg_base_idx, dump_out_buf);
  3090. goto end;
  3091. }
  3092. }
  3093. }
  3094. end:
  3095. return rc;
  3096. }
  3097. /**
  3098. * cam_soc_util_print_clk_freq()
  3099. *
  3100. * @brief: This function gets the clk rates for each clk from clk
  3101. * driver and prints in log
  3102. *
  3103. * @soc_info: Device soc struct to be populated
  3104. *
  3105. * @return: success or failure
  3106. */
  3107. int cam_soc_util_print_clk_freq(struct cam_hw_soc_info *soc_info)
  3108. {
  3109. int i;
  3110. unsigned long clk_rate = 0;
  3111. if (!soc_info) {
  3112. CAM_ERR(CAM_UTIL, "Invalid soc info");
  3113. return -EINVAL;
  3114. }
  3115. if ((soc_info->num_clk == 0) ||
  3116. (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
  3117. CAM_ERR(CAM_UTIL, "[%s] Invalid number of clock %d",
  3118. soc_info->dev_name, soc_info->num_clk);
  3119. return -EINVAL;
  3120. }
  3121. for (i = 0; i < soc_info->num_clk; i++) {
  3122. clk_rate = clk_get_rate(soc_info->clk[i]);
  3123. CAM_INFO(CAM_UTIL,
  3124. "[%s] idx = %d clk name = %s clk_rate=%lld",
  3125. soc_info->dev_name, i, soc_info->clk_name[i],
  3126. clk_rate);
  3127. }
  3128. return 0;
  3129. }