cam_soc_util.c 94 KB

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