cam_soc_util.c 107 KB

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