cam_soc_util.c 95 KB

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