cam_soc_util.c 116 KB

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