cam_soc_util.c 106 KB

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