cam_soc_util.c 91 KB

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