12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
- * Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved.
- */
- #include <linux/of.h>
- #include <linux/clk.h>
- #include <linux/slab.h>
- #include <linux/gpio.h>
- #include <linux/of_gpio.h>
- #include "cam_soc_util.h"
- #include "cam_debug_util.h"
- #include "cam_cx_ipeak.h"
- #include "cam_mem_mgr.h"
- #include "cam_presil_hw_access.h"
- #include "cam_compat.h"
- #define CAM_TO_MASK(bitn) (1 << (int)(bitn))
- #define CAM_IS_BIT_SET(mask, bit) ((mask) & CAM_TO_MASK(bit))
- #define CAM_SET_BIT(mask, bit) ((mask) |= CAM_TO_MASK(bit))
- #define CAM_CLEAR_BIT(mask, bit) ((mask) &= ~CAM_TO_MASK(bit))
- #define CAM_SS_START_PRESIL 0x08c00000
- #define CAM_SS_START 0x0ac00000
- #define CAM_CLK_DIRNAME "clk"
- static uint skip_mmrm_set_rate;
- module_param(skip_mmrm_set_rate, uint, 0644);
- /**
- * struct cam_clk_wrapper_clk: This represents an entry corresponding to a
- * shared clock in Clk wrapper. Clients that share
- * the same clock are registered to this clk entry
- * and set rate from them is consolidated before
- * setting it to clk driver.
- *
- * @list: List pointer to point to next shared clk entry
- * @clk_id: Clk Id of this clock
- * @curr_clk_rate: Current clock rate set for this clock
- * @client_list: List of clients registered to this shared clock entry
- * @num_clients: Number of registered clients
- * @active_clients: Number of active clients
- * @mmrm_client: MMRM Client handle for src clock
- * @soc_info: soc_info of client with which mmrm handle is created.
- * This is used as unique identifier for a client and mmrm
- * callback data. When client corresponds to this soc_info is
- * unregistered, need to unregister mmrm handle as well.
- * @is_nrt_dev: Whether this clock corresponds to NRT device
- * @min_clk_rate: Minimum clk rate that this clock supports
- **/
- struct cam_clk_wrapper_clk {
- struct list_head list;
- uint32_t clk_id;
- int64_t curr_clk_rate;
- struct list_head client_list;
- uint32_t num_clients;
- uint32_t active_clients;
- void *mmrm_handle;
- struct cam_hw_soc_info *soc_info;
- bool is_nrt_dev;
- int64_t min_clk_rate;
- };
- /**
- * struct cam_clk_wrapper_client: This represents a client (device) that wants
- * to share the clock with some other client.
- *
- * @list: List pointer to point to next client that share the
- * same clock
- * @soc_info: soc_info of client. This is used as unique identifier
- * for a client
- * @clk: Clk handle
- * @curr_clk_rate: Current clock rate set for this client
- **/
- struct cam_clk_wrapper_client {
- struct list_head list;
- struct cam_hw_soc_info *soc_info;
- struct clk *clk;
- int64_t curr_clk_rate;
- };
- static char supported_clk_info[256];
- static DEFINE_MUTEX(wrapper_lock);
- static LIST_HEAD(wrapper_clk_list);
- #if IS_REACHABLE(CONFIG_MSM_MMRM)
- bool cam_is_mmrm_supported_on_current_chip(void)
- {
- bool is_supported;
- is_supported = mmrm_client_check_scaling_supported(MMRM_CLIENT_CLOCK,
- MMRM_CLIENT_DOMAIN_CAMERA);
- CAM_DBG(CAM_UTIL, "is mmrm supported: %s",
- CAM_BOOL_TO_YESNO(is_supported));;
- return is_supported;
- }
- int cam_mmrm_notifier_callback(
- struct mmrm_client_notifier_data *notifier_data)
- {
- if (!notifier_data) {
- CAM_ERR(CAM_UTIL, "Invalid notifier data");
- return -EBADR;
- }
- if (notifier_data->cb_type == MMRM_CLIENT_RESOURCE_VALUE_CHANGE) {
- struct cam_hw_soc_info *soc_info = notifier_data->pvt_data;
- CAM_WARN(CAM_UTIL, "Dev %s Clk %s value change from %ld to %ld",
- soc_info->dev_name,
- (soc_info->src_clk_idx == -1) ? "No src clk" :
- soc_info->clk_name[soc_info->src_clk_idx],
- notifier_data->cb_data.val_chng.old_val,
- notifier_data->cb_data.val_chng.new_val);
- }
- return 0;
- }
- int cam_soc_util_register_mmrm_client(
- uint32_t clk_id, struct clk *clk, bool is_nrt_dev,
- struct cam_hw_soc_info *soc_info, const char *clk_name,
- void **mmrm_handle)
- {
- struct mmrm_client *mmrm_client;
- struct mmrm_client_desc desc = { };
- if (!mmrm_handle) {
- CAM_ERR(CAM_UTIL, "Invalid mmrm input");
- return -EINVAL;
- }
- *mmrm_handle = (void *)NULL;
- if (!cam_is_mmrm_supported_on_current_chip())
- return 0;
- desc.client_type = MMRM_CLIENT_CLOCK;
- desc.client_info.desc.client_domain = MMRM_CLIENT_DOMAIN_CAMERA;
- desc.client_info.desc.client_id = clk_id;
- desc.client_info.desc.clk = clk;
- snprintf((char *)desc.client_info.desc.name,
- sizeof(desc.client_info.desc.name), "%s_%s",
- soc_info->dev_name, clk_name);
- desc.priority = is_nrt_dev ?
- MMRM_CLIENT_PRIOR_LOW : MMRM_CLIENT_PRIOR_HIGH;
- desc.pvt_data = soc_info;
- desc.notifier_callback_fn = cam_mmrm_notifier_callback;
- mmrm_client = mmrm_client_register(&desc);
- if (!mmrm_client) {
- CAM_ERR(CAM_UTIL, "MMRM Register failed Dev %s clk %s id %d",
- soc_info->dev_name, clk_name, clk_id);
- return -EINVAL;
- }
- CAM_DBG(CAM_UTIL,
- "MMRM Register success Dev %s is_nrt_dev %d clk %s id %d handle=%pK",
- soc_info->dev_name, is_nrt_dev, clk_name, clk_id, mmrm_client);
- *mmrm_handle = (void *)mmrm_client;
- return 0;
- }
- int cam_soc_util_unregister_mmrm_client(
- void *mmrm_handle)
- {
- int rc = 0;
- CAM_DBG(CAM_UTIL, "MMRM UnRegister handle=%pK", mmrm_handle);
- if (mmrm_handle) {
- rc = mmrm_client_deregister((struct mmrm_client *)mmrm_handle);
- if (rc)
- CAM_ERR(CAM_UTIL,
- "Failed in deregister handle=%pK, rc %d",
- mmrm_handle, rc);
- }
- return rc;
- }
- static int cam_soc_util_set_rate_through_mmrm(
- void *mmrm_handle, bool is_nrt_dev, long min_rate,
- long req_rate, uint32_t num_hw_blocks)
- {
- int rc = 0;
- struct mmrm_client_data client_data;
- struct mmrm_client_res_value val;
- client_data.num_hw_blocks = num_hw_blocks;
- client_data.flags = 0;
- CAM_DBG(CAM_UTIL,
- "mmrm=%pK, nrt=%d, min_rate=%ld req_rate %ld, num_blocks=%d",
- mmrm_handle, is_nrt_dev, min_rate, req_rate, num_hw_blocks);
- if (is_nrt_dev) {
- val.min = min_rate;
- val.cur = req_rate;
- rc = mmrm_client_set_value_in_range(
- (struct mmrm_client *)mmrm_handle, &client_data, &val);
- } else {
- rc = mmrm_client_set_value(
- (struct mmrm_client *)mmrm_handle,
- &client_data, req_rate);
- }
- if (rc)
- CAM_ERR(CAM_UTIL, "Set rate failed rate %ld rc %d",
- req_rate, rc);
- return rc;
- }
- #else
- int cam_soc_util_register_mmrm_client(
- uint32_t clk_id, struct clk *clk, bool is_nrt_dev,
- struct cam_hw_soc_info *soc_info, const char *clk_name,
- void **mmrm_handle)
- {
- if (!mmrm_handle) {
- CAM_ERR(CAM_UTIL, "Invalid mmrm input");
- return -EINVAL;
- }
- *mmrm_handle = NULL;
- return 0;
- }
- int cam_soc_util_unregister_mmrm_client(
- void *mmrm_handle)
- {
- return 0;
- }
- static int cam_soc_util_set_rate_through_mmrm(
- void *mmrm_handle, bool is_nrt_dev, long min_rate,
- long req_rate, uint32_t num_hw_blocks)
- {
- return 0;
- }
- #endif
- static int cam_soc_util_clk_wrapper_register_entry(
- uint32_t clk_id, struct clk *clk, bool is_src_clk,
- struct cam_hw_soc_info *soc_info, int64_t min_clk_rate,
- const char *clk_name)
- {
- struct cam_clk_wrapper_clk *wrapper_clk;
- struct cam_clk_wrapper_client *wrapper_client;
- bool clock_found = false;
- int rc = 0;
- mutex_lock(&wrapper_lock);
- list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
- CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
- wrapper_clk->clk_id, wrapper_clk->num_clients);
- if (wrapper_clk->clk_id == clk_id) {
- clock_found = true;
- list_for_each_entry(wrapper_client,
- &wrapper_clk->client_list, list) {
- CAM_DBG(CAM_UTIL,
- "Clk id %d entry client %s",
- wrapper_clk->clk_id,
- wrapper_client->soc_info->dev_name);
- if (wrapper_client->soc_info == soc_info) {
- CAM_ERR(CAM_UTIL,
- "Register with same soc info, clk id %d, client %s",
- clk_id, soc_info->dev_name);
- rc = -EINVAL;
- goto end;
- }
- }
- break;
- }
- }
- if (!clock_found) {
- CAM_DBG(CAM_UTIL, "Adding new entry for clk id %d", clk_id);
- wrapper_clk = kzalloc(sizeof(struct cam_clk_wrapper_clk),
- GFP_KERNEL);
- if (!wrapper_clk) {
- CAM_ERR(CAM_UTIL,
- "Failed in allocating new clk entry %d",
- clk_id);
- rc = -ENOMEM;
- goto end;
- }
- wrapper_clk->clk_id = clk_id;
- INIT_LIST_HEAD(&wrapper_clk->list);
- INIT_LIST_HEAD(&wrapper_clk->client_list);
- list_add_tail(&wrapper_clk->list, &wrapper_clk_list);
- }
- wrapper_client = kzalloc(sizeof(struct cam_clk_wrapper_client),
- GFP_KERNEL);
- if (!wrapper_client) {
- CAM_ERR(CAM_UTIL, "Failed in allocating new client entry %d",
- clk_id);
- rc = -ENOMEM;
- goto end;
- }
- wrapper_client->soc_info = soc_info;
- wrapper_client->clk = clk;
- if (is_src_clk && !wrapper_clk->mmrm_handle) {
- wrapper_clk->is_nrt_dev = soc_info->is_nrt_dev;
- wrapper_clk->min_clk_rate = min_clk_rate;
- wrapper_clk->soc_info = soc_info;
- rc = cam_soc_util_register_mmrm_client(clk_id, clk,
- wrapper_clk->is_nrt_dev, soc_info, clk_name,
- &wrapper_clk->mmrm_handle);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Failed in register mmrm client Dev %s clk id %d",
- soc_info->dev_name, clk_id);
- kfree(wrapper_client);
- goto end;
- }
- }
- INIT_LIST_HEAD(&wrapper_client->list);
- list_add_tail(&wrapper_client->list, &wrapper_clk->client_list);
- wrapper_clk->num_clients++;
- CAM_DBG(CAM_UTIL,
- "Adding new client %s for clk[%s] id %d, num clients %d",
- soc_info->dev_name, clk_name, clk_id, wrapper_clk->num_clients);
- end:
- mutex_unlock(&wrapper_lock);
- return rc;
- }
- static int cam_soc_util_clk_wrapper_unregister_entry(
- uint32_t clk_id, struct cam_hw_soc_info *soc_info)
- {
- struct cam_clk_wrapper_clk *wrapper_clk;
- struct cam_clk_wrapper_client *wrapper_client;
- bool clock_found = false;
- bool client_found = false;
- int rc = 0;
- mutex_lock(&wrapper_lock);
- list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
- CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
- wrapper_clk->clk_id, wrapper_clk->num_clients);
- if (wrapper_clk->clk_id == clk_id) {
- clock_found = true;
- list_for_each_entry(wrapper_client,
- &wrapper_clk->client_list, list) {
- CAM_DBG(CAM_UTIL, "Clk id %d entry client %s",
- wrapper_clk->clk_id,
- wrapper_client->soc_info->dev_name);
- if (wrapper_client->soc_info == soc_info) {
- client_found = true;
- break;
- }
- }
- break;
- }
- }
- if (!clock_found) {
- CAM_ERR(CAM_UTIL, "Shared clk id %d entry not found", clk_id);
- rc = -EINVAL;
- goto end;
- }
- if (!client_found) {
- CAM_ERR(CAM_UTIL,
- "Client %pK for Shared clk id %d entry not found",
- soc_info, clk_id);
- rc = -EINVAL;
- goto end;
- }
- wrapper_clk->num_clients--;
- if (wrapper_clk->mmrm_handle && (wrapper_clk->soc_info == soc_info)) {
- cam_soc_util_unregister_mmrm_client(wrapper_clk->mmrm_handle);
- wrapper_clk->mmrm_handle = NULL;
- wrapper_clk->soc_info = NULL;
- }
- list_del_init(&wrapper_client->list);
- kfree(wrapper_client);
- CAM_DBG(CAM_UTIL, "Unregister client %s for clk id %d, num clients %d",
- soc_info->dev_name, clk_id, wrapper_clk->num_clients);
- if (!wrapper_clk->num_clients) {
- list_del_init(&wrapper_clk->list);
- kfree(wrapper_clk);
- }
- end:
- mutex_unlock(&wrapper_lock);
- return rc;
- }
- static int cam_soc_util_clk_wrapper_set_clk_rate(
- uint32_t clk_id, struct cam_hw_soc_info *soc_info,
- struct clk *clk, int64_t clk_rate)
- {
- struct cam_clk_wrapper_clk *wrapper_clk;
- struct cam_clk_wrapper_client *wrapper_client;
- bool clk_found = false;
- bool client_found = false;
- int rc = 0;
- int64_t final_clk_rate = 0;
- uint32_t active_clients = 0;
- if (!soc_info || !clk) {
- CAM_ERR(CAM_UTIL, "Invalid param soc_info %pK clk %pK",
- soc_info, clk);
- return -EINVAL;
- }
- mutex_lock(&wrapper_lock);
- list_for_each_entry(wrapper_clk, &wrapper_clk_list, list) {
- CAM_DBG(CAM_UTIL, "Clk list id %d num clients %d",
- wrapper_clk->clk_id, wrapper_clk->num_clients);
- if (wrapper_clk->clk_id == clk_id) {
- clk_found = true;
- break;
- }
- }
- if (!clk_found) {
- CAM_ERR(CAM_UTIL, "Clk entry not found id %d client %s",
- clk_id, soc_info->dev_name);
- rc = -EINVAL;
- goto end;
- }
- list_for_each_entry(wrapper_client, &wrapper_clk->client_list, list) {
- CAM_DBG(CAM_UTIL, "Clk id %d client %s, clk rate %lld",
- wrapper_clk->clk_id, wrapper_client->soc_info->dev_name,
- wrapper_client->curr_clk_rate);
- if (wrapper_client->soc_info == soc_info) {
- client_found = true;
- CAM_DBG(CAM_UTIL,
- "Clk enable clk id %d, client %s curr %ld new %ld",
- clk_id, wrapper_client->soc_info->dev_name,
- wrapper_client->curr_clk_rate, clk_rate);
- wrapper_client->curr_clk_rate = clk_rate;
- }
- if (wrapper_client->curr_clk_rate > 0)
- active_clients++;
- if (final_clk_rate < wrapper_client->curr_clk_rate)
- final_clk_rate = wrapper_client->curr_clk_rate;
- }
- if (!client_found) {
- CAM_ERR(CAM_UTIL,
- "Wrapper clk enable without client entry clk id %d client %s",
- clk_id, soc_info->dev_name);
- rc = -EINVAL;
- goto end;
- }
- CAM_DBG(CAM_UTIL,
- "Clk id %d, client %s, clients rate %ld, curr %ld final %ld",
- wrapper_clk->clk_id, soc_info->dev_name, clk_rate,
- wrapper_clk->curr_clk_rate, final_clk_rate);
- if ((final_clk_rate != wrapper_clk->curr_clk_rate) ||
- (active_clients != wrapper_clk->active_clients)) {
- bool set_rate_finish = false;
- if (!skip_mmrm_set_rate && wrapper_clk->mmrm_handle) {
- rc = cam_soc_util_set_rate_through_mmrm(
- wrapper_clk->mmrm_handle,
- wrapper_clk->is_nrt_dev,
- wrapper_clk->min_clk_rate,
- final_clk_rate, active_clients);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "set_rate through mmrm failed clk_id %d, rate=%ld",
- wrapper_clk->clk_id, final_clk_rate);
- goto end;
- }
- set_rate_finish = true;
- }
- if (!set_rate_finish && final_clk_rate &&
- (final_clk_rate != wrapper_clk->curr_clk_rate)) {
- rc = clk_set_rate(clk, final_clk_rate);
- if (rc) {
- CAM_ERR(CAM_UTIL, "set_rate failed on clk %d",
- wrapper_clk->clk_id);
- goto end;
- }
- }
- wrapper_clk->curr_clk_rate = final_clk_rate;
- wrapper_clk->active_clients = active_clients;
- }
- end:
- mutex_unlock(&wrapper_lock);
- return rc;
- }
- int cam_soc_util_get_clk_level(struct cam_hw_soc_info *soc_info,
- int64_t clk_rate, int clk_idx, int32_t *clk_lvl)
- {
- int i;
- long clk_rate_round;
- if (!soc_info || (clk_idx < 0) || (clk_idx >= CAM_SOC_MAX_CLK)) {
- CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d", clk_idx);
- *clk_lvl = -1;
- return -EINVAL;
- }
- clk_rate_round = clk_round_rate(soc_info->clk[clk_idx], clk_rate);
- if (clk_rate_round < 0) {
- CAM_ERR(CAM_UTIL, "round failed rc = %ld",
- clk_rate_round);
- *clk_lvl = -1;
- return -EINVAL;
- }
- for (i = 0; i < CAM_MAX_VOTE; i++) {
- if ((soc_info->clk_level_valid[i]) &&
- (soc_info->clk_rate[i][clk_idx] >=
- clk_rate_round)) {
- CAM_DBG(CAM_UTIL,
- "soc = %d round rate = %ld actual = %lld",
- soc_info->clk_rate[i][clk_idx],
- clk_rate_round, clk_rate);
- *clk_lvl = i;
- return 0;
- }
- }
- CAM_WARN(CAM_UTIL, "Invalid clock rate %ld", clk_rate_round);
- *clk_lvl = -1;
- return -EINVAL;
- }
- /**
- * cam_soc_util_get_string_from_level()
- *
- * @brief: Returns the string for a given clk level
- *
- * @level: Clock level
- *
- * @return: String corresponding to the clk level
- */
- static const char *cam_soc_util_get_string_from_level(
- enum cam_vote_level level)
- {
- switch (level) {
- case CAM_SUSPEND_VOTE:
- return "";
- case CAM_MINSVS_VOTE:
- return "MINSVS[1]";
- case CAM_LOWSVS_VOTE:
- return "LOWSVS[2]";
- case CAM_SVS_VOTE:
- return "SVS[3]";
- case CAM_SVSL1_VOTE:
- return "SVSL1[4]";
- case CAM_NOMINAL_VOTE:
- return "NOM[5]";
- case CAM_NOMINALL1_VOTE:
- return "NOML1[6]";
- case CAM_TURBO_VOTE:
- return "TURBO[7]";
- default:
- return "";
- }
- }
- /**
- * cam_soc_util_get_supported_clk_levels()
- *
- * @brief: Returns the string of all the supported clk levels for
- * the given device
- *
- * @soc_info: Device soc information
- *
- * @return: String containing all supported clk levels
- */
- static const char *cam_soc_util_get_supported_clk_levels(
- struct cam_hw_soc_info *soc_info)
- {
- int i = 0;
- scnprintf(supported_clk_info, sizeof(supported_clk_info), "Supported levels: ");
- for (i = 0; i < CAM_MAX_VOTE; i++) {
- if (soc_info->clk_level_valid[i] == true) {
- strlcat(supported_clk_info,
- cam_soc_util_get_string_from_level(i),
- sizeof(supported_clk_info));
- strlcat(supported_clk_info, " ",
- sizeof(supported_clk_info));
- }
- }
- strlcat(supported_clk_info, "\n", sizeof(supported_clk_info));
- return supported_clk_info;
- }
- static int cam_soc_util_clk_lvl_options_open(struct inode *inode,
- struct file *file)
- {
- file->private_data = inode->i_private;
- return 0;
- }
- static ssize_t cam_soc_util_clk_lvl_options_read(struct file *file,
- char __user *clk_info, size_t size_t, loff_t *loff_t)
- {
- struct cam_hw_soc_info *soc_info =
- (struct cam_hw_soc_info *)file->private_data;
- const char *display_string =
- cam_soc_util_get_supported_clk_levels(soc_info);
- return simple_read_from_buffer(clk_info, size_t, loff_t, display_string,
- strlen(display_string));
- }
- static const struct file_operations cam_soc_util_clk_lvl_options = {
- .open = cam_soc_util_clk_lvl_options_open,
- .read = cam_soc_util_clk_lvl_options_read,
- };
- static int cam_soc_util_set_clk_lvl(void *data, u64 val)
- {
- struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
- if (val <= CAM_SUSPEND_VOTE || val >= CAM_MAX_VOTE)
- return 0;
- if (soc_info->clk_level_valid[val] == true)
- soc_info->clk_level_override = val;
- else
- soc_info->clk_level_override = 0;
- return 0;
- }
- static int cam_soc_util_get_clk_lvl(void *data, u64 *val)
- {
- struct cam_hw_soc_info *soc_info = (struct cam_hw_soc_info *)data;
- *val = soc_info->clk_level_override;
- return 0;
- }
- DEFINE_SIMPLE_ATTRIBUTE(cam_soc_util_clk_lvl_control,
- cam_soc_util_get_clk_lvl, cam_soc_util_set_clk_lvl, "%08llu");
- /**
- * cam_soc_util_create_clk_lvl_debugfs()
- *
- * @brief: Creates debugfs files to view/control device clk rates
- *
- * @soc_info: Device soc information
- *
- * @return: Success or failure
- */
- static int cam_soc_util_create_clk_lvl_debugfs(struct cam_hw_soc_info *soc_info)
- {
- int rc = 0;
- struct dentry *dbgfileptr = NULL, *clkdirptr = NULL;
- if (!cam_debugfs_available())
- return 0;
- if (soc_info->dentry) {
- CAM_DBG(CAM_UTIL, "Debugfs entry for %s already exists",
- soc_info->dev_name);
- goto end;
- }
- rc = cam_debugfs_lookup_subdir(CAM_CLK_DIRNAME, &clkdirptr);
- if (rc) {
- rc = cam_debugfs_create_subdir(CAM_CLK_DIRNAME, &clkdirptr);
- if (rc) {
- CAM_ERR(CAM_UTIL, "DebugFS could not create clk directory!");
- rc = -ENOENT;
- goto end;
- }
- }
- dbgfileptr = debugfs_create_dir(soc_info->dev_name, clkdirptr);
- if (IS_ERR_OR_NULL(dbgfileptr)) {
- CAM_ERR(CAM_UTIL, "DebugFS could not create directory for dev:%s!",
- soc_info->dev_name);
- rc = -ENOENT;
- goto end;
- }
- /* Store parent inode for cleanup in caller */
- soc_info->dentry = dbgfileptr;
- dbgfileptr = debugfs_create_file("clk_lvl_options", 0444,
- soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_options);
- dbgfileptr = debugfs_create_file("clk_lvl_control", 0644,
- soc_info->dentry, soc_info, &cam_soc_util_clk_lvl_control);
- rc = PTR_ERR_OR_ZERO(dbgfileptr);
- end:
- return rc;
- }
- int cam_soc_util_get_level_from_string(const char *string,
- enum cam_vote_level *level)
- {
- if (!level)
- return -EINVAL;
- if (!strcmp(string, "suspend")) {
- *level = CAM_SUSPEND_VOTE;
- } else if (!strcmp(string, "minsvs")) {
- *level = CAM_MINSVS_VOTE;
- } else if (!strcmp(string, "lowsvs")) {
- *level = CAM_LOWSVS_VOTE;
- } else if (!strcmp(string, "svs")) {
- *level = CAM_SVS_VOTE;
- } else if (!strcmp(string, "svs_l1")) {
- *level = CAM_SVSL1_VOTE;
- } else if (!strcmp(string, "nominal")) {
- *level = CAM_NOMINAL_VOTE;
- } else if (!strcmp(string, "nominal_l1")) {
- *level = CAM_NOMINALL1_VOTE;
- } else if (!strcmp(string, "turbo")) {
- *level = CAM_TURBO_VOTE;
- } else {
- CAM_ERR(CAM_UTIL, "Invalid string %s", string);
- return -EINVAL;
- }
- return 0;
- }
- /**
- * cam_soc_util_get_clk_level_to_apply()
- *
- * @brief: Get the clock level to apply. If the requested level
- * is not valid, bump the level to next available valid
- * level. If no higher level found, return failure.
- *
- * @soc_info: Device soc struct to be populated
- * @req_level: Requested level
- * @apply_level Level to apply
- *
- * @return: success or failure
- */
- static int cam_soc_util_get_clk_level_to_apply(
- struct cam_hw_soc_info *soc_info, enum cam_vote_level req_level,
- enum cam_vote_level *apply_level)
- {
- if (req_level >= CAM_MAX_VOTE) {
- CAM_ERR(CAM_UTIL, "Invalid clock level parameter %d",
- req_level);
- return -EINVAL;
- }
- if (soc_info->clk_level_valid[req_level] == true) {
- *apply_level = req_level;
- } else {
- int i;
- for (i = (req_level + 1); i < CAM_MAX_VOTE; i++)
- if (soc_info->clk_level_valid[i] == true) {
- *apply_level = i;
- break;
- }
- if (i == CAM_MAX_VOTE) {
- CAM_ERR(CAM_UTIL,
- "No valid clock level found to apply, req=%d",
- req_level);
- return -EINVAL;
- }
- }
- CAM_DBG(CAM_UTIL, "Req level %d, Applying %d",
- req_level, *apply_level);
- return 0;
- }
- int cam_soc_util_irq_enable(struct cam_hw_soc_info *soc_info)
- {
- if (!soc_info) {
- CAM_ERR(CAM_UTIL, "Invalid arguments");
- return -EINVAL;
- }
- if (soc_info->irq_num < 0) {
- CAM_ERR(CAM_UTIL, "No IRQ line available");
- return -ENODEV;
- }
- enable_irq(soc_info->irq_num);
- return 0;
- }
- int cam_soc_util_irq_disable(struct cam_hw_soc_info *soc_info)
- {
- if (!soc_info) {
- CAM_ERR(CAM_UTIL, "Invalid arguments");
- return -EINVAL;
- }
- if (soc_info->irq_num < 0) {
- CAM_ERR(CAM_UTIL, "No IRQ line available");
- return -ENODEV;
- }
- disable_irq(soc_info->irq_num);
- return 0;
- }
- long cam_soc_util_get_clk_round_rate(struct cam_hw_soc_info *soc_info,
- uint32_t clk_index, unsigned long clk_rate)
- {
- if (!soc_info || (clk_index >= soc_info->num_clk) || (clk_rate == 0)) {
- CAM_ERR(CAM_UTIL, "Invalid input params %pK, %d %lu",
- soc_info, clk_index, clk_rate);
- return clk_rate;
- }
- return clk_round_rate(soc_info->clk[clk_index], clk_rate);
- }
- /**
- * cam_soc_util_set_clk_rate()
- *
- * @brief: Sets the given rate for the clk requested for
- *
- * @clk: Clock structure information for which rate is to be set
- * @clk_name: Name of the clock for which rate is being set
- * @clk_rate: Clock rate to be set
- * @shared_clk: Whether this is a shared clk
- * @is_src_clk: Whether this is source clk
- * @clk_id: Clock ID
- * @applied_clk_rate: Final clock rate set to the clk
- *
- * @return: Success or failure
- */
- static int cam_soc_util_set_clk_rate(struct cam_hw_soc_info *soc_info,
- struct clk *clk, const char *clk_name,
- int64_t clk_rate, bool shared_clk, bool is_src_clk, uint32_t clk_id,
- unsigned long *applied_clk_rate)
- {
- int rc = 0;
- long clk_rate_round = -1;
- bool set_rate = false;
- if (!clk_name) {
- CAM_ERR(CAM_UTIL, "Invalid input clk %pK clk_name %pK",
- clk, clk_name);
- return -EINVAL;
- }
- CAM_DBG(CAM_UTIL, "set %s, rate %lld", clk_name, clk_rate);
- if (!clk)
- return 0;
- if (clk_rate > 0) {
- clk_rate_round = clk_round_rate(clk, clk_rate);
- CAM_DBG(CAM_UTIL, "new_rate %ld", clk_rate_round);
- if (clk_rate_round < 0) {
- CAM_ERR(CAM_UTIL, "round failed for clock %s rc = %ld",
- clk_name, clk_rate_round);
- return clk_rate_round;
- }
- set_rate = true;
- } else if (clk_rate == INIT_RATE) {
- clk_rate_round = clk_get_rate(clk);
- CAM_DBG(CAM_UTIL, "init new_rate %ld", clk_rate_round);
- if (clk_rate_round == 0) {
- clk_rate_round = clk_round_rate(clk, 0);
- if (clk_rate_round <= 0) {
- CAM_ERR(CAM_UTIL, "round rate failed on %s",
- clk_name);
- return clk_rate_round;
- }
- }
- set_rate = true;
- }
- if (set_rate) {
- if (shared_clk) {
- CAM_DBG(CAM_UTIL,
- "Dev %s clk %s id %d Set Shared clk %ld",
- soc_info->dev_name, clk_name, clk_id,
- clk_rate_round);
- cam_soc_util_clk_wrapper_set_clk_rate(
- clk_id, soc_info, clk, clk_rate_round);
- } else {
- bool set_rate_finish = false;
- CAM_DBG(CAM_UTIL,
- "Dev %s clk %s clk_id %d src_idx %d src_clk_id %d",
- soc_info->dev_name, clk_name, clk_id,
- soc_info->src_clk_idx,
- (soc_info->src_clk_idx == -1) ? -1 :
- soc_info->clk_id[soc_info->src_clk_idx]);
- if (is_src_clk && soc_info->mmrm_handle &&
- !skip_mmrm_set_rate) {
- uint32_t idx = soc_info->src_clk_idx;
- uint32_t min_level = soc_info->lowest_clk_level;
- rc = cam_soc_util_set_rate_through_mmrm(
- soc_info->mmrm_handle,
- soc_info->is_nrt_dev,
- soc_info->clk_rate[min_level][idx],
- clk_rate_round, 1);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "set_rate through mmrm failed on %s clk_id %d, rate=%ld",
- clk_name, clk_id,
- clk_rate_round);
- return rc;
- }
- set_rate_finish = true;
- }
- if (!set_rate_finish) {
- rc = clk_set_rate(clk, clk_rate_round);
- if (rc) {
- CAM_ERR(CAM_UTIL, "set_rate failed on %s", clk_name);
- return rc;
- }
- }
- }
- }
- if (applied_clk_rate)
- *applied_clk_rate = clk_rate_round;
- return rc;
- }
- int cam_soc_util_set_src_clk_rate(struct cam_hw_soc_info *soc_info,
- int64_t clk_rate)
- {
- int rc = 0;
- int i = 0;
- int32_t src_clk_idx;
- int32_t scl_clk_idx;
- struct clk *clk = NULL;
- int32_t apply_level;
- uint32_t clk_level_override = 0;
- if (!soc_info || (soc_info->src_clk_idx < 0) ||
- (soc_info->src_clk_idx >= CAM_SOC_MAX_CLK)) {
- CAM_ERR(CAM_UTIL, "Invalid src_clk_idx: %d",
- soc_info ? soc_info->src_clk_idx : -1);
- return -EINVAL;
- }
- src_clk_idx = soc_info->src_clk_idx;
- clk_level_override = soc_info->clk_level_override;
- if (clk_level_override && clk_rate)
- clk_rate =
- soc_info->clk_rate[clk_level_override][src_clk_idx];
- clk = soc_info->clk[src_clk_idx];
- rc = cam_soc_util_get_clk_level(soc_info, clk_rate, src_clk_idx,
- &apply_level);
- if (rc || (apply_level < 0) || (apply_level >= CAM_MAX_VOTE)) {
- CAM_ERR(CAM_UTIL,
- "set %s, rate %lld dev_name = %s apply level = %d",
- soc_info->clk_name[src_clk_idx], clk_rate,
- soc_info->dev_name, apply_level);
- return -EINVAL;
- }
- CAM_DBG(CAM_UTIL, "set %s, rate %lld dev_name = %s apply level = %d",
- soc_info->clk_name[src_clk_idx], clk_rate,
- soc_info->dev_name, apply_level);
- if ((soc_info->cam_cx_ipeak_enable) && (clk_rate >= 0)) {
- cam_cx_ipeak_update_vote_cx_ipeak(soc_info,
- apply_level);
- }
- rc = cam_soc_util_set_clk_rate(soc_info, clk,
- soc_info->clk_name[src_clk_idx], clk_rate,
- CAM_IS_BIT_SET(soc_info->shared_clk_mask, src_clk_idx),
- true, soc_info->clk_id[src_clk_idx],
- &soc_info->applied_src_clk_rate);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "SET_RATE Failed: src clk: %s, rate %lld, dev_name = %s rc: %d",
- soc_info->clk_name[src_clk_idx], clk_rate,
- soc_info->dev_name, rc);
- return rc;
- }
- /* set clk rate for scalable clk if available */
- for (i = 0; i < soc_info->scl_clk_count; i++) {
- scl_clk_idx = soc_info->scl_clk_idx[i];
- if (scl_clk_idx < 0) {
- CAM_DBG(CAM_UTIL, "Scl clk index invalid");
- continue;
- }
- clk = soc_info->clk[scl_clk_idx];
- rc = cam_soc_util_set_clk_rate(soc_info, clk,
- soc_info->clk_name[scl_clk_idx],
- soc_info->clk_rate[apply_level][scl_clk_idx],
- CAM_IS_BIT_SET(soc_info->shared_clk_mask, scl_clk_idx),
- false, soc_info->clk_id[scl_clk_idx],
- NULL);
- if (rc) {
- CAM_WARN(CAM_UTIL,
- "SET_RATE Failed: scl clk: %s, rate %d dev_name = %s, rc: %d",
- soc_info->clk_name[scl_clk_idx],
- soc_info->clk_rate[apply_level][scl_clk_idx],
- soc_info->dev_name, rc);
- }
- }
- return 0;
- }
- int cam_soc_util_put_optional_clk(struct cam_hw_soc_info *soc_info,
- int32_t clk_indx)
- {
- if (clk_indx < 0) {
- CAM_ERR(CAM_UTIL, "Invalid params clk %d", clk_indx);
- return -EINVAL;
- }
- if (CAM_IS_BIT_SET(soc_info->optional_shared_clk_mask, clk_indx))
- cam_soc_util_clk_wrapper_unregister_entry(
- soc_info->optional_clk_id[clk_indx], soc_info);
- clk_put(soc_info->optional_clk[clk_indx]);
- soc_info->optional_clk[clk_indx] = NULL;
- return 0;
- }
- static struct clk *cam_soc_util_option_clk_get(struct device_node *np,
- int index, uint32_t *clk_id)
- {
- struct of_phandle_args clkspec;
- struct clk *clk;
- int rc;
- if (index < 0)
- return ERR_PTR(-EINVAL);
- rc = of_parse_phandle_with_args(np, "clocks-option", "#clock-cells",
- index, &clkspec);
- if (rc)
- return ERR_PTR(rc);
- clk = of_clk_get_from_provider(&clkspec);
- *clk_id = clkspec.args[0];
- of_node_put(clkspec.np);
- return clk;
- }
- int cam_soc_util_get_option_clk_by_name(struct cam_hw_soc_info *soc_info,
- const char *clk_name, int32_t *clk_index)
- {
- int index = 0;
- int rc = 0;
- struct device_node *of_node = NULL;
- uint32_t shared_clk_val;
- if (!soc_info || !clk_name || !clk_index) {
- CAM_ERR(CAM_UTIL,
- "Invalid params soc_info %pK clk_name %s clk_index %pK",
- soc_info, clk_name, clk_index);
- return -EINVAL;
- }
- of_node = soc_info->dev->of_node;
- index = of_property_match_string(of_node, "clock-names-option",
- clk_name);
- if (index < 0) {
- CAM_DBG(CAM_UTIL, "No clk data for %s", clk_name);
- *clk_index = -1;
- return -EINVAL;
- }
- if (index >= CAM_SOC_MAX_OPT_CLK) {
- CAM_ERR(CAM_UTIL, "Insufficient optional clk entries %d %d",
- index, CAM_SOC_MAX_OPT_CLK);
- return -EINVAL;
- }
- of_property_read_string_index(of_node, "clock-names-option",
- index, &(soc_info->optional_clk_name[index]));
- soc_info->optional_clk[index] = cam_soc_util_option_clk_get(of_node,
- index, &soc_info->optional_clk_id[index]);
- if (IS_ERR(soc_info->optional_clk[index])) {
- CAM_ERR(CAM_UTIL, "No clk named %s found. Dev %s", clk_name,
- soc_info->dev_name);
- *clk_index = -1;
- return -EFAULT;
- }
- *clk_index = index;
- rc = of_property_read_u32_index(of_node, "clock-rates-option",
- index, &soc_info->optional_clk_rate[index]);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Error reading clock-rates clk_name %s index %d",
- clk_name, index);
- goto error;
- }
- /*
- * Option clocks are assumed to be available to single Device here.
- * Hence use INIT_RATE instead of NO_SET_RATE.
- */
- soc_info->optional_clk_rate[index] =
- (soc_info->optional_clk_rate[index] == 0) ?
- (int32_t)INIT_RATE : soc_info->optional_clk_rate[index];
- CAM_DBG(CAM_UTIL, "clk_name %s index %d clk_rate %d",
- clk_name, *clk_index, soc_info->optional_clk_rate[index]);
- rc = of_property_read_u32_index(of_node, "shared-clks-option",
- index, &shared_clk_val);
- if (rc) {
- CAM_DBG(CAM_UTIL, "Not shared clk %s index %d",
- clk_name, index);
- } else if (shared_clk_val > 1) {
- CAM_WARN(CAM_UTIL, "Invalid shared clk val %d", shared_clk_val);
- } else {
- CAM_DBG(CAM_UTIL,
- "Dev %s shared clk %s index %d, clk id %d, shared_clk_val %d",
- soc_info->dev_name, clk_name, index,
- soc_info->optional_clk_id[index], shared_clk_val);
- if (shared_clk_val) {
- CAM_SET_BIT(soc_info->optional_shared_clk_mask, index);
- /* Create a wrapper entry if this is a shared clock */
- CAM_DBG(CAM_UTIL,
- "Dev %s, clk %s, id %d register wrapper entry for shared clk",
- soc_info->dev_name,
- soc_info->optional_clk_name[index],
- soc_info->optional_clk_id[index]);
- rc = cam_soc_util_clk_wrapper_register_entry(
- soc_info->optional_clk_id[index],
- soc_info->optional_clk[index], false,
- soc_info,
- soc_info->optional_clk_rate[index],
- soc_info->optional_clk_name[index]);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Failed in registering shared clk Dev %s id %d",
- soc_info->dev_name,
- soc_info->optional_clk_id[index]);
- goto error;
- }
- }
- }
- return 0;
- error:
- clk_put(soc_info->optional_clk[index]);
- soc_info->optional_clk_rate[index] = 0;
- soc_info->optional_clk[index] = NULL;
- *clk_index = -1;
- return rc;
- }
- int cam_soc_util_clk_enable(struct cam_hw_soc_info *soc_info,
- bool optional_clk, int32_t clk_idx, int32_t apply_level,
- unsigned long *applied_clock_rate)
- {
- int rc = 0;
- struct clk *clk;
- const char *clk_name;
- int32_t clk_rate;
- uint32_t shared_clk_mask;
- uint32_t clk_id;
- bool is_src_clk = false;
- if (!soc_info || (clk_idx < 0) || (apply_level >= CAM_MAX_VOTE)) {
- CAM_ERR(CAM_UTIL, "Invalid param %d %d", clk_idx, apply_level);
- return -EINVAL;
- }
- if (optional_clk) {
- clk = soc_info->optional_clk[clk_idx];
- clk_name = soc_info->optional_clk_name[clk_idx];
- clk_rate = (apply_level == -1) ?
- 0 : soc_info->optional_clk_rate[clk_idx];
- shared_clk_mask = soc_info->optional_shared_clk_mask;
- clk_id = soc_info->optional_clk_id[clk_idx];
- } else {
- clk = soc_info->clk[clk_idx];
- clk_name = soc_info->clk_name[clk_idx];
- clk_rate = (apply_level == -1) ?
- 0 : soc_info->clk_rate[apply_level][clk_idx];
- shared_clk_mask = soc_info->shared_clk_mask;
- clk_id = soc_info->clk_id[clk_idx];
- if (clk_idx == soc_info->src_clk_idx)
- is_src_clk = true;
- }
- if (!clk)
- return 0;
- rc = cam_soc_util_set_clk_rate(soc_info, clk, clk_name, clk_rate,
- CAM_IS_BIT_SET(shared_clk_mask, clk_idx), is_src_clk, clk_id,
- applied_clock_rate);
- if (rc)
- return rc;
- rc = clk_prepare_enable(clk);
- if (rc) {
- CAM_ERR(CAM_UTIL, "enable failed for %s: rc(%d)", clk_name, rc);
- return rc;
- }
- return rc;
- }
- int cam_soc_util_clk_disable(struct cam_hw_soc_info *soc_info,
- bool optional_clk, int32_t clk_idx)
- {
- struct clk *clk;
- const char *clk_name;
- uint32_t shared_clk_mask;
- uint32_t clk_id;
- if (!soc_info || (clk_idx < 0)) {
- CAM_ERR(CAM_UTIL, "Invalid param %d", clk_idx);
- return -EINVAL;
- }
- if (optional_clk) {
- clk = soc_info->optional_clk[clk_idx];
- clk_name = soc_info->optional_clk_name[clk_idx];
- shared_clk_mask = soc_info->optional_shared_clk_mask;
- clk_id = soc_info->optional_clk_id[clk_idx];
- } else {
- clk = soc_info->clk[clk_idx];
- clk_name = soc_info->clk_name[clk_idx];
- shared_clk_mask = soc_info->shared_clk_mask;
- clk_id = soc_info->clk_id[clk_idx];
- }
- CAM_DBG(CAM_UTIL, "disable %s", clk_name);
- if (!clk)
- return 0;
- clk_disable_unprepare(clk);
- if (CAM_IS_BIT_SET(shared_clk_mask, clk_idx)) {
- CAM_DBG(CAM_UTIL,
- "Dev %s clk %s Disabling Shared clk, set 0 rate",
- soc_info->dev_name, clk_name);
- cam_soc_util_clk_wrapper_set_clk_rate(clk_id, soc_info, clk, 0);
- } else if (soc_info->mmrm_handle && (!skip_mmrm_set_rate) &&
- (soc_info->src_clk_idx == clk_idx)) {
- CAM_DBG(CAM_UTIL,
- "Dev %s Disabling %s clk, set 0 rate", soc_info->dev_name, clk_name);
- cam_soc_util_set_rate_through_mmrm(
- soc_info->mmrm_handle,
- soc_info->is_nrt_dev,
- 0, 0, 1);
- }
- return 0;
- }
- /**
- * cam_soc_util_clk_enable_default()
- *
- * @brief: This function enables the default clocks present
- * in soc_info
- *
- * @soc_info: Device soc struct to be populated
- * @clk_level: Clk level to apply while enabling
- *
- * @return: success or failure
- */
- int cam_soc_util_clk_enable_default(struct cam_hw_soc_info *soc_info,
- enum cam_vote_level clk_level)
- {
- int i, rc = 0;
- enum cam_vote_level apply_level;
- unsigned long applied_clk_rate;
- if ((soc_info->num_clk == 0) ||
- (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
- CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
- soc_info->num_clk);
- return -EINVAL;
- }
- rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
- &apply_level);
- if (rc)
- return rc;
- if (soc_info->cam_cx_ipeak_enable)
- cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
- for (i = 0; i < soc_info->num_clk; i++) {
- rc = cam_soc_util_clk_enable(soc_info, false, i, apply_level,
- &applied_clk_rate);
- if (rc)
- goto clk_disable;
- if (i == soc_info->src_clk_idx)
- soc_info->applied_src_clk_rate = applied_clk_rate;
- if (soc_info->cam_cx_ipeak_enable) {
- CAM_DBG(CAM_UTIL,
- "dev name = %s clk name = %s idx = %d\n"
- "apply_level = %d clc idx = %d",
- soc_info->dev_name, soc_info->clk_name[i], i,
- apply_level, i);
- }
- }
- return rc;
- clk_disable:
- if (soc_info->cam_cx_ipeak_enable)
- cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
- for (i--; i >= 0; i--) {
- cam_soc_util_clk_disable(soc_info, false, i);
- }
- return rc;
- }
- /**
- * cam_soc_util_clk_disable_default()
- *
- * @brief: This function disables the default clocks present
- * in soc_info
- *
- * @soc_info: device soc struct to be populated
- *
- * @return: success or failure
- */
- void cam_soc_util_clk_disable_default(struct cam_hw_soc_info *soc_info)
- {
- int i;
- if (soc_info->num_clk == 0)
- return;
- if (soc_info->cam_cx_ipeak_enable)
- cam_cx_ipeak_unvote_cx_ipeak(soc_info);
- for (i = soc_info->num_clk - 1; i >= 0; i--)
- cam_soc_util_clk_disable(soc_info, false, i);
- }
- /**
- * cam_soc_util_get_dt_clk_info()
- *
- * @brief: Parse the DT and populate the Clock properties
- *
- * @soc_info: device soc struct to be populated
- * @src_clk_str name of src clock that has rate control
- *
- * @return: success or failure
- */
- static int cam_soc_util_get_dt_clk_info(struct cam_hw_soc_info *soc_info)
- {
- struct device_node *of_node = NULL;
- int count;
- int num_clk_rates, num_clk_levels;
- int i, j, rc;
- int32_t num_clk_level_strings;
- const char *src_clk_str = NULL;
- const char *scl_clk_str = NULL;
- const char *clk_control_debugfs = NULL;
- const char *clk_cntl_lvl_string = NULL;
- enum cam_vote_level level;
- int shared_clk_cnt;
- struct of_phandle_args clk_args = {0};
- if (!soc_info || !soc_info->dev)
- return -EINVAL;
- of_node = soc_info->dev->of_node;
- if (!of_property_read_bool(of_node, "use-shared-clk")) {
- CAM_DBG(CAM_UTIL, "No shared clk parameter defined");
- soc_info->use_shared_clk = false;
- } else {
- soc_info->use_shared_clk = true;
- }
- count = of_property_count_strings(of_node, "clock-names");
- CAM_DBG(CAM_UTIL, "E: dev_name = %s count = %d",
- soc_info->dev_name, count);
- if (count > CAM_SOC_MAX_CLK) {
- CAM_ERR(CAM_UTIL, "invalid count of clocks, count=%d", count);
- rc = -EINVAL;
- return rc;
- }
- if (count <= 0) {
- CAM_DBG(CAM_UTIL, "No clock-names found");
- count = 0;
- soc_info->num_clk = count;
- return 0;
- }
- soc_info->num_clk = count;
- for (i = 0; i < count; i++) {
- rc = of_property_read_string_index(of_node, "clock-names",
- i, &(soc_info->clk_name[i]));
- CAM_DBG(CAM_UTIL, "clock-names[%d] = %s",
- i, soc_info->clk_name[i]);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "i= %d count= %d reading clock-names failed",
- i, count);
- return rc;
- }
- }
- num_clk_rates = of_property_count_u32_elems(of_node, "clock-rates");
- if (num_clk_rates <= 0) {
- CAM_ERR(CAM_UTIL, "reading clock-rates count failed");
- return -EINVAL;
- }
- if ((num_clk_rates % soc_info->num_clk) != 0) {
- CAM_ERR(CAM_UTIL,
- "mismatch clk/rates, No of clocks=%d, No of rates=%d",
- soc_info->num_clk, num_clk_rates);
- return -EINVAL;
- }
- num_clk_levels = (num_clk_rates / soc_info->num_clk);
- num_clk_level_strings = of_property_count_strings(of_node,
- "clock-cntl-level");
- if (num_clk_level_strings != num_clk_levels) {
- CAM_ERR(CAM_UTIL,
- "Mismatch No of levels=%d, No of level string=%d",
- num_clk_levels, num_clk_level_strings);
- return -EINVAL;
- }
- soc_info->lowest_clk_level = CAM_TURBO_VOTE;
- for (i = 0; i < num_clk_levels; i++) {
- rc = of_property_read_string_index(of_node,
- "clock-cntl-level", i, &clk_cntl_lvl_string);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Error reading clock-cntl-level, rc=%d", rc);
- return rc;
- }
- rc = cam_soc_util_get_level_from_string(clk_cntl_lvl_string,
- &level);
- if (rc)
- return rc;
- CAM_DBG(CAM_UTIL,
- "[%d] : %s %d", i, clk_cntl_lvl_string, level);
- soc_info->clk_level_valid[level] = true;
- for (j = 0; j < soc_info->num_clk; j++) {
- rc = of_property_read_u32_index(of_node, "clock-rates",
- ((i * soc_info->num_clk) + j),
- &soc_info->clk_rate[level][j]);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Error reading clock-rates, rc=%d",
- rc);
- return rc;
- }
- soc_info->clk_rate[level][j] =
- (soc_info->clk_rate[level][j] == 0) ?
- (int32_t)NO_SET_RATE :
- soc_info->clk_rate[level][j];
- CAM_DBG(CAM_UTIL, "soc_info->clk_rate[%d][%d] = %d",
- level, j,
- soc_info->clk_rate[level][j]);
- }
- if ((level > CAM_MINSVS_VOTE) &&
- (level < soc_info->lowest_clk_level))
- soc_info->lowest_clk_level = level;
- }
- soc_info->src_clk_idx = -1;
- rc = of_property_read_string_index(of_node, "src-clock-name", 0,
- &src_clk_str);
- if (rc || !src_clk_str) {
- CAM_DBG(CAM_UTIL, "No src_clk_str found");
- rc = 0;
- goto end;
- }
- for (i = 0; i < soc_info->num_clk; i++) {
- if (strcmp(soc_info->clk_name[i], src_clk_str) == 0) {
- soc_info->src_clk_idx = i;
- CAM_DBG(CAM_UTIL, "src clock = %s, index = %d",
- src_clk_str, i);
- }
- rc = of_parse_phandle_with_args(of_node, "clocks",
- "#clock-cells", i, &clk_args);
- if (rc) {
- CAM_ERR(CAM_CPAS,
- "failed to clock info rc=%d", rc);
- rc = -EINVAL;
- goto end;
- }
- soc_info->clk_id[i] = clk_args.args[0];
- of_node_put(clk_args.np);
- CAM_DBG(CAM_UTIL, "Dev %s clk %s id %d",
- soc_info->dev_name, soc_info->clk_name[i],
- soc_info->clk_id[i]);
- }
- CAM_DBG(CAM_UTIL, "Dev %s src_clk_idx %d, lowest_clk_level %d",
- soc_info->dev_name, soc_info->src_clk_idx,
- soc_info->lowest_clk_level);
- soc_info->shared_clk_mask = 0;
- shared_clk_cnt = of_property_count_u32_elems(of_node, "shared-clks");
- if (shared_clk_cnt <= 0) {
- CAM_DBG(CAM_UTIL, "Dev %s, no shared clks", soc_info->dev_name);
- } else if (shared_clk_cnt != count) {
- CAM_ERR(CAM_UTIL, "Dev %s, incorrect shared clock count %d %d",
- soc_info->dev_name, shared_clk_cnt, count);
- rc = -EINVAL;
- goto end;
- } else {
- uint32_t shared_clk_val;
- for (i = 0; i < shared_clk_cnt; i++) {
- rc = of_property_read_u32_index(of_node,
- "shared-clks", i, &shared_clk_val);
- if (rc || (shared_clk_val > 1)) {
- CAM_ERR(CAM_UTIL,
- "Incorrect shared clk info at %d, val=%d, count=%d",
- i, shared_clk_val, shared_clk_cnt);
- rc = -EINVAL;
- goto end;
- }
- if (shared_clk_val)
- CAM_SET_BIT(soc_info->shared_clk_mask, i);
- }
- CAM_DBG(CAM_UTIL, "Dev %s shared clk mask 0x%x",
- soc_info->dev_name, soc_info->shared_clk_mask);
- }
- /* scalable clk info parsing */
- soc_info->scl_clk_count = 0;
- soc_info->scl_clk_count = of_property_count_strings(of_node,
- "scl-clk-names");
- if ((soc_info->scl_clk_count <= 0) ||
- (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
- if (soc_info->scl_clk_count == -EINVAL) {
- CAM_DBG(CAM_UTIL, "scl_clk_name prop not avialable");
- } else if ((soc_info->scl_clk_count == -ENODATA) ||
- (soc_info->scl_clk_count > CAM_SOC_MAX_CLK)) {
- CAM_ERR(CAM_UTIL, "Invalid scl_clk_count: %d",
- soc_info->scl_clk_count);
- return -EINVAL;
- }
- CAM_DBG(CAM_UTIL, "Invalid scl_clk count: %d",
- soc_info->scl_clk_count);
- soc_info->scl_clk_count = -1;
- } else {
- CAM_DBG(CAM_UTIL, "No of scalable clocks: %d",
- soc_info->scl_clk_count);
- for (i = 0; i < soc_info->scl_clk_count; i++) {
- rc = of_property_read_string_index(of_node,
- "scl-clk-names", i,
- (const char **)&scl_clk_str);
- if (rc || !scl_clk_str) {
- CAM_WARN(CAM_UTIL, "scl_clk_str is NULL");
- soc_info->scl_clk_idx[i] = -1;
- continue;
- }
- for (j = 0; j < soc_info->num_clk; j++) {
- if (strnstr(scl_clk_str, soc_info->clk_name[j],
- strlen(scl_clk_str))) {
- soc_info->scl_clk_idx[i] = j;
- CAM_DBG(CAM_UTIL,
- "scl clock = %s, index = %d",
- scl_clk_str, j);
- break;
- }
- }
- }
- }
- rc = of_property_read_string_index(of_node,
- "clock-control-debugfs", 0, &clk_control_debugfs);
- if (rc || !clk_control_debugfs) {
- CAM_DBG(CAM_UTIL, "No clock_control_debugfs property found");
- rc = 0;
- goto end;
- }
- if (strcmp("true", clk_control_debugfs) == 0)
- soc_info->clk_control_enable = true;
- CAM_DBG(CAM_UTIL, "X: dev_name = %s count = %d",
- soc_info->dev_name, count);
- end:
- return rc;
- }
- int cam_soc_util_set_clk_rate_level(struct cam_hw_soc_info *soc_info,
- enum cam_vote_level clk_level, bool do_not_set_src_clk)
- {
- int i, rc = 0;
- enum cam_vote_level apply_level;
- unsigned long applied_clk_rate;
- if ((soc_info->num_clk == 0) ||
- (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
- CAM_ERR(CAM_UTIL, "Invalid number of clock %d",
- soc_info->num_clk);
- return -EINVAL;
- }
- rc = cam_soc_util_get_clk_level_to_apply(soc_info, clk_level,
- &apply_level);
- if (rc)
- return rc;
- if (soc_info->cam_cx_ipeak_enable)
- cam_cx_ipeak_update_vote_cx_ipeak(soc_info, apply_level);
- for (i = 0; i < soc_info->num_clk; i++) {
- if (do_not_set_src_clk && (i == soc_info->src_clk_idx)) {
- CAM_DBG(CAM_UTIL, "Skipping set rate for src clk %s",
- soc_info->clk_name[i]);
- continue;
- }
- CAM_DBG(CAM_UTIL, "Set rate for clk %s rate %d",
- soc_info->clk_name[i],
- soc_info->clk_rate[apply_level][i]);
- rc = cam_soc_util_set_clk_rate(soc_info, soc_info->clk[i],
- soc_info->clk_name[i],
- soc_info->clk_rate[apply_level][i],
- CAM_IS_BIT_SET(soc_info->shared_clk_mask, i),
- (i == soc_info->src_clk_idx) ? true : false,
- soc_info->clk_id[i],
- &applied_clk_rate);
- if (rc < 0) {
- CAM_DBG(CAM_UTIL,
- "dev name = %s clk_name = %s idx = %d\n"
- "apply_level = %d",
- soc_info->dev_name, soc_info->clk_name[i],
- i, apply_level);
- if (soc_info->cam_cx_ipeak_enable)
- cam_cx_ipeak_update_vote_cx_ipeak(soc_info, 0);
- break;
- }
- if (i == soc_info->src_clk_idx)
- soc_info->applied_src_clk_rate = applied_clk_rate;
- }
- return rc;
- };
- static int cam_soc_util_get_dt_gpio_req_tbl(struct device_node *of_node,
- struct cam_soc_gpio_data *gconf, uint16_t *gpio_array,
- uint16_t gpio_array_size)
- {
- int32_t rc = 0, i = 0;
- uint32_t count = 0;
- uint32_t *val_array = NULL;
- if (!of_get_property(of_node, "gpio-req-tbl-num", &count))
- return 0;
- count /= sizeof(uint32_t);
- if (!count) {
- CAM_ERR(CAM_UTIL, "gpio-req-tbl-num 0");
- return 0;
- }
- val_array = kcalloc(count, sizeof(uint32_t), GFP_KERNEL);
- if (!val_array)
- return -ENOMEM;
- gconf->cam_gpio_req_tbl = kcalloc(count, sizeof(struct gpio),
- GFP_KERNEL);
- if (!gconf->cam_gpio_req_tbl) {
- rc = -ENOMEM;
- goto free_val_array;
- }
- gconf->cam_gpio_req_tbl_size = count;
- rc = of_property_read_u32_array(of_node, "gpio-req-tbl-num",
- val_array, count);
- if (rc) {
- CAM_ERR(CAM_UTIL, "failed in reading gpio-req-tbl-num, rc = %d",
- rc);
- goto free_gpio_req_tbl;
- }
- for (i = 0; i < count; i++) {
- if (val_array[i] >= gpio_array_size) {
- CAM_ERR(CAM_UTIL, "gpio req tbl index %d invalid",
- val_array[i]);
- goto free_gpio_req_tbl;
- }
- gconf->cam_gpio_req_tbl[i].gpio = gpio_array[val_array[i]];
- CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].gpio = %d", i,
- gconf->cam_gpio_req_tbl[i].gpio);
- }
- rc = of_property_read_u32_array(of_node, "gpio-req-tbl-flags",
- val_array, count);
- if (rc) {
- CAM_ERR(CAM_UTIL, "Failed in gpio-req-tbl-flags, rc %d", rc);
- goto free_gpio_req_tbl;
- }
- for (i = 0; i < count; i++) {
- gconf->cam_gpio_req_tbl[i].flags = val_array[i];
- CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].flags = %ld", i,
- gconf->cam_gpio_req_tbl[i].flags);
- }
- for (i = 0; i < count; i++) {
- rc = of_property_read_string_index(of_node,
- "gpio-req-tbl-label", i,
- &gconf->cam_gpio_req_tbl[i].label);
- if (rc) {
- CAM_ERR(CAM_UTIL, "Failed rc %d", rc);
- goto free_gpio_req_tbl;
- }
- CAM_DBG(CAM_UTIL, "cam_gpio_req_tbl[%d].label = %s", i,
- gconf->cam_gpio_req_tbl[i].label);
- }
- kfree(val_array);
- return rc;
- free_gpio_req_tbl:
- kfree(gconf->cam_gpio_req_tbl);
- free_val_array:
- kfree(val_array);
- gconf->cam_gpio_req_tbl_size = 0;
- return rc;
- }
- static int cam_soc_util_get_gpio_info(struct cam_hw_soc_info *soc_info)
- {
- int32_t rc = 0, i = 0;
- uint16_t *gpio_array = NULL;
- int16_t gpio_array_size = 0;
- struct cam_soc_gpio_data *gconf = NULL;
- struct device_node *of_node = NULL;
- if (!soc_info || !soc_info->dev)
- return -EINVAL;
- of_node = soc_info->dev->of_node;
- /* Validate input parameters */
- if (!of_node) {
- CAM_ERR(CAM_UTIL, "Invalid param of_node");
- return -EINVAL;
- }
- gpio_array_size = of_gpio_count(of_node);
- if (gpio_array_size <= 0)
- return 0;
- CAM_DBG(CAM_UTIL, "gpio count %d", gpio_array_size);
- gpio_array = kcalloc(gpio_array_size, sizeof(uint16_t), GFP_KERNEL);
- if (!gpio_array)
- goto free_gpio_conf;
- for (i = 0; i < gpio_array_size; i++) {
- gpio_array[i] = of_get_gpio(of_node, i);
- CAM_DBG(CAM_UTIL, "gpio_array[%d] = %d", i, gpio_array[i]);
- }
- gconf = kzalloc(sizeof(*gconf), GFP_KERNEL);
- if (!gconf)
- return -ENOMEM;
- rc = cam_soc_util_get_dt_gpio_req_tbl(of_node, gconf, gpio_array,
- gpio_array_size);
- if (rc) {
- CAM_ERR(CAM_UTIL, "failed in msm_camera_get_dt_gpio_req_tbl");
- goto free_gpio_array;
- }
- gconf->cam_gpio_common_tbl = kcalloc(gpio_array_size,
- sizeof(struct gpio), GFP_KERNEL);
- if (!gconf->cam_gpio_common_tbl) {
- rc = -ENOMEM;
- goto free_gpio_array;
- }
- for (i = 0; i < gpio_array_size; i++)
- gconf->cam_gpio_common_tbl[i].gpio = gpio_array[i];
- gconf->cam_gpio_common_tbl_size = gpio_array_size;
- soc_info->gpio_data = gconf;
- kfree(gpio_array);
- return rc;
- free_gpio_array:
- kfree(gpio_array);
- free_gpio_conf:
- kfree(gconf);
- soc_info->gpio_data = NULL;
- return rc;
- }
- static int cam_soc_util_request_gpio_table(
- struct cam_hw_soc_info *soc_info, bool gpio_en)
- {
- int rc = 0, i = 0;
- uint8_t size = 0;
- struct cam_soc_gpio_data *gpio_conf =
- soc_info->gpio_data;
- struct gpio *gpio_tbl = NULL;
- if (!gpio_conf) {
- CAM_DBG(CAM_UTIL, "No GPIO entry");
- return 0;
- }
- if (gpio_conf->cam_gpio_common_tbl_size <= 0) {
- CAM_ERR(CAM_UTIL, "GPIO table size is invalid");
- return -EINVAL;
- }
- size = gpio_conf->cam_gpio_req_tbl_size;
- gpio_tbl = gpio_conf->cam_gpio_req_tbl;
- if (!gpio_tbl || !size) {
- CAM_ERR(CAM_UTIL, "Invalid gpio_tbl %pK / size %d",
- gpio_tbl, size);
- return -EINVAL;
- }
- for (i = 0; i < size; i++) {
- CAM_DBG(CAM_UTIL, "i=%d, gpio=%d dir=%ld", i,
- gpio_tbl[i].gpio, gpio_tbl[i].flags);
- }
- if (gpio_en) {
- for (i = 0; i < size; i++) {
- rc = gpio_request_one(gpio_tbl[i].gpio,
- gpio_tbl[i].flags, gpio_tbl[i].label);
- if (rc) {
- /*
- * After GPIO request fails, contine to
- * apply new gpios, outout a error message
- * for driver bringup debug
- */
- CAM_ERR(CAM_UTIL, "gpio %d:%s request fails",
- gpio_tbl[i].gpio, gpio_tbl[i].label);
- }
- }
- } else {
- gpio_free_array(gpio_tbl, size);
- }
- return rc;
- }
- static int cam_soc_util_get_dt_regulator_info
- (struct cam_hw_soc_info *soc_info)
- {
- int rc = 0, count = 0, i = 0;
- struct device_node *of_node = NULL;
- if (!soc_info || !soc_info->dev) {
- CAM_ERR(CAM_UTIL, "Invalid parameters");
- return -EINVAL;
- }
- of_node = soc_info->dev->of_node;
- soc_info->num_rgltr = 0;
- count = of_property_count_strings(of_node, "regulator-names");
- if (count != -EINVAL) {
- if (count <= 0) {
- CAM_ERR(CAM_UTIL, "no regulators found");
- count = 0;
- return -EINVAL;
- }
- soc_info->num_rgltr = count;
- } else {
- CAM_DBG(CAM_UTIL, "No regulators node found");
- return 0;
- }
- if (soc_info->num_rgltr > CAM_SOC_MAX_REGULATOR) {
- CAM_ERR(CAM_UTIL, "Invalid regulator count:%d",
- soc_info->num_rgltr);
- return -EINVAL;
- }
- for (i = 0; i < soc_info->num_rgltr; i++) {
- rc = of_property_read_string_index(of_node,
- "regulator-names", i, &soc_info->rgltr_name[i]);
- CAM_DBG(CAM_UTIL, "rgltr_name[%d] = %s",
- i, soc_info->rgltr_name[i]);
- if (rc) {
- CAM_ERR(CAM_UTIL, "no regulator resource at cnt=%d", i);
- return -ENODEV;
- }
- }
- if (!of_property_read_bool(of_node, "rgltr-cntrl-support")) {
- CAM_DBG(CAM_UTIL, "No regulator control parameter defined");
- soc_info->rgltr_ctrl_support = false;
- return 0;
- }
- soc_info->rgltr_ctrl_support = true;
- rc = of_property_read_u32_array(of_node, "rgltr-min-voltage",
- soc_info->rgltr_min_volt, soc_info->num_rgltr);
- if (rc) {
- CAM_ERR(CAM_UTIL, "No minimum volatage value found, rc=%d", rc);
- return -EINVAL;
- }
- rc = of_property_read_u32_array(of_node, "rgltr-max-voltage",
- soc_info->rgltr_max_volt, soc_info->num_rgltr);
- if (rc) {
- CAM_ERR(CAM_UTIL, "No maximum volatage value found, rc=%d", rc);
- return -EINVAL;
- }
- rc = of_property_read_u32_array(of_node, "rgltr-load-current",
- soc_info->rgltr_op_mode, soc_info->num_rgltr);
- if (rc) {
- CAM_ERR(CAM_UTIL, "No Load curent found rc=%d", rc);
- return -EINVAL;
- }
- return rc;
- }
- #ifdef CONFIG_CAM_PRESIL
- static uint32_t next_dummy_irq_line_num = 0x000f;
- struct resource dummy_irq_line[512];
- #endif
- int cam_soc_util_get_dt_properties(struct cam_hw_soc_info *soc_info)
- {
- struct device_node *of_node = NULL;
- int count = 0, i = 0, rc = 0;
- if (!soc_info || !soc_info->dev)
- return -EINVAL;
- of_node = soc_info->dev->of_node;
- rc = of_property_read_u32(of_node, "cell-index", &soc_info->index);
- if (rc) {
- CAM_ERR(CAM_UTIL, "device %s failed to read cell-index",
- soc_info->dev_name);
- return rc;
- }
- count = of_property_count_strings(of_node, "reg-names");
- if (count <= 0) {
- CAM_DBG(CAM_UTIL, "no reg-names found for: %s",
- soc_info->dev_name);
- count = 0;
- }
- soc_info->num_mem_block = count;
- for (i = 0; i < soc_info->num_mem_block; i++) {
- rc = of_property_read_string_index(of_node, "reg-names", i,
- &soc_info->mem_block_name[i]);
- if (rc) {
- CAM_ERR(CAM_UTIL, "failed to read reg-names at %d", i);
- return rc;
- }
- soc_info->mem_block[i] =
- platform_get_resource_byname(soc_info->pdev,
- IORESOURCE_MEM, soc_info->mem_block_name[i]);
- if (!soc_info->mem_block[i]) {
- CAM_ERR(CAM_UTIL, "no mem resource by name %s",
- soc_info->mem_block_name[i]);
- rc = -ENODEV;
- return rc;
- }
- }
- rc = of_property_read_string(of_node, "label", &soc_info->label_name);
- if (rc)
- CAM_DBG(CAM_UTIL, "Label is not available in the node: %d", rc);
- if (soc_info->num_mem_block > 0) {
- rc = of_property_read_u32_array(of_node, "reg-cam-base",
- soc_info->mem_block_cam_base, soc_info->num_mem_block);
- if (rc) {
- CAM_ERR(CAM_UTIL, "Error reading register offsets");
- return rc;
- }
- }
- rc = of_property_read_string_index(of_node, "interrupt-names", 0,
- &soc_info->irq_name);
- if (rc) {
- CAM_DBG(CAM_UTIL, "No interrupt line preset for: %s",
- soc_info->dev_name);
- rc = 0;
- } else {
- rc = cam_compat_util_get_irq(soc_info);
- if (rc < 0) {
- CAM_ERR(CAM_UTIL, "get irq resource failed: %d", rc);
- #ifndef CONFIG_CAM_PRESIL
- return rc;
- #else
- /* Pre-sil for new devices not present on old */
- soc_info->irq_line =
- &dummy_irq_line[next_dummy_irq_line_num++];
- CAM_DBG(CAM_PRESIL, "interrupt line for dev %s irq name %s number %d",
- soc_info->dev_name, soc_info->irq_name,
- soc_info->irq_line->start);
- #endif
- }
- }
- rc = of_property_read_string_index(of_node, "compatible", 0,
- (const char **)&soc_info->compatible);
- if (rc) {
- CAM_DBG(CAM_UTIL, "No compatible string present for: %s",
- soc_info->dev_name);
- rc = 0;
- }
- soc_info->is_nrt_dev = false;
- if (of_property_read_bool(of_node, "nrt-device"))
- soc_info->is_nrt_dev = true;
- CAM_DBG(CAM_UTIL, "Dev %s, nrt_dev %d",
- soc_info->dev_name, soc_info->is_nrt_dev);
- rc = cam_soc_util_get_dt_regulator_info(soc_info);
- if (rc)
- return rc;
- rc = cam_soc_util_get_dt_clk_info(soc_info);
- if (rc)
- return rc;
- rc = cam_soc_util_get_gpio_info(soc_info);
- if (rc)
- return rc;
- if (of_find_property(of_node, "qcom,cam-cx-ipeak", NULL))
- rc = cam_cx_ipeak_register_cx_ipeak(soc_info);
- return rc;
- }
- /**
- * cam_soc_util_get_regulator()
- *
- * @brief: Get regulator resource named vdd
- *
- * @dev: Device associated with regulator
- * @reg: Return pointer to be filled with regulator on success
- * @rgltr_name: Name of regulator to get
- *
- * @return: 0 for Success, negative value for failure
- */
- static int cam_soc_util_get_regulator(struct device *dev,
- struct regulator **reg, const char *rgltr_name)
- {
- int rc = 0;
- *reg = regulator_get(dev, rgltr_name);
- if (IS_ERR_OR_NULL(*reg)) {
- rc = PTR_ERR(*reg);
- rc = rc ? rc : -EINVAL;
- CAM_ERR(CAM_UTIL, "Regulator %s get failed %d", rgltr_name, rc);
- *reg = NULL;
- }
- return rc;
- }
- int cam_soc_util_regulator_disable(struct regulator *rgltr,
- const char *rgltr_name, uint32_t rgltr_min_volt,
- uint32_t rgltr_max_volt, uint32_t rgltr_op_mode,
- uint32_t rgltr_delay_ms)
- {
- int32_t rc = 0;
- if (!rgltr) {
- CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
- return -EINVAL;
- }
- rc = regulator_disable(rgltr);
- if (rc) {
- CAM_ERR(CAM_UTIL, "%s regulator disable failed", rgltr_name);
- return rc;
- }
- if (rgltr_delay_ms > 20)
- msleep(rgltr_delay_ms);
- else if (rgltr_delay_ms)
- usleep_range(rgltr_delay_ms * 1000,
- (rgltr_delay_ms * 1000) + 1000);
- if (regulator_count_voltages(rgltr) > 0) {
- regulator_set_load(rgltr, 0);
- regulator_set_voltage(rgltr, 0, rgltr_max_volt);
- }
- return rc;
- }
- int cam_soc_util_regulator_enable(struct regulator *rgltr,
- const char *rgltr_name,
- uint32_t rgltr_min_volt, uint32_t rgltr_max_volt,
- uint32_t rgltr_op_mode, uint32_t rgltr_delay)
- {
- int32_t rc = 0;
- if (!rgltr) {
- CAM_ERR(CAM_UTIL, "Invalid NULL parameter");
- return -EINVAL;
- }
- if (regulator_count_voltages(rgltr) > 0) {
- CAM_DBG(CAM_UTIL, "voltage min=%d, max=%d",
- rgltr_min_volt, rgltr_max_volt);
- rc = regulator_set_voltage(
- rgltr, rgltr_min_volt, rgltr_max_volt);
- if (rc) {
- CAM_ERR(CAM_UTIL, "%s set voltage failed", rgltr_name);
- return rc;
- }
- rc = regulator_set_load(rgltr, rgltr_op_mode);
- if (rc) {
- CAM_ERR(CAM_UTIL, "%s set optimum mode failed",
- rgltr_name);
- return rc;
- }
- }
- rc = regulator_enable(rgltr);
- if (rc) {
- CAM_ERR(CAM_UTIL, "%s regulator_enable failed", rgltr_name);
- return rc;
- }
- if (rgltr_delay > 20)
- msleep(rgltr_delay);
- else if (rgltr_delay)
- usleep_range(rgltr_delay * 1000,
- (rgltr_delay * 1000) + 1000);
- return rc;
- }
- int cam_soc_util_select_pinctrl_state(struct cam_hw_soc_info *soc_info,
- int pctrl_idx, bool active)
- {
- int rc = 0;
- struct cam_soc_pinctrl_info *pctrl_info = &soc_info->pinctrl_info;
- if (pctrl_idx >= CAM_SOC_MAX_PINCTRL_MAP) {
- CAM_ERR(CAM_UTIL, "Invalid Map idx: %d max supported: %d",
- pctrl_idx, CAM_SOC_MAX_PINCTRL_MAP);
- return -EINVAL;
- }
- if (pctrl_info->pctrl_state[pctrl_idx].gpio_state_active &&
- active &&
- !pctrl_info->pctrl_state[pctrl_idx].is_active) {
- rc = pinctrl_select_state(pctrl_info->pinctrl,
- pctrl_info->pctrl_state[pctrl_idx].gpio_state_active);
- if (rc)
- CAM_ERR(CAM_UTIL,
- "Pinctrl active state transition failed: rc: %d",
- rc);
- else {
- pctrl_info->pctrl_state[pctrl_idx].is_active = true;
- CAM_DBG(CAM_UTIL, "Pctrl_idx: %d is in active state",
- pctrl_idx);
- }
- }
- if (pctrl_info->pctrl_state[pctrl_idx].gpio_state_suspend &&
- !active &&
- pctrl_info->pctrl_state[pctrl_idx].is_active) {
- rc = pinctrl_select_state(pctrl_info->pinctrl,
- pctrl_info->pctrl_state[pctrl_idx].gpio_state_suspend);
- if (rc)
- CAM_ERR(CAM_UTIL,
- "Pinctrl suspend state transition failed: rc: %d",
- rc);
- else {
- pctrl_info->pctrl_state[pctrl_idx].is_active = false;
- CAM_DBG(CAM_UTIL, "Pctrl_idx: %d is in suspend state",
- pctrl_idx);
- }
- }
- return rc;
- }
- static int cam_soc_util_request_pinctrl(
- struct cam_hw_soc_info *soc_info)
- {
- struct cam_soc_pinctrl_info *device_pctrl = &soc_info->pinctrl_info;
- struct device *dev = soc_info->dev;
- struct device_node *of_node = dev->of_node;
- uint32_t i = 0;
- int rc = 0;
- const char *name;
- uint32_t idx;
- char pctrl_active[50];
- char pctrl_suspend[50];
- int32_t num_of_map_idx = 0;
- int32_t num_of_string = 0;
- device_pctrl->pinctrl = devm_pinctrl_get(dev);
- if (IS_ERR_OR_NULL(device_pctrl->pinctrl)) {
- CAM_DBG(CAM_UTIL, "Pinctrl not available");
- device_pctrl->pinctrl = NULL;
- return 0;
- }
- num_of_map_idx = of_property_count_u32_elems(
- of_node, "pctrl-idx-mapping");
- if (num_of_map_idx <= 0) {
- CAM_ERR(CAM_UTIL,
- "Reading pctrl-idx-mapping failed");
- return -EINVAL;
- }
- num_of_string = of_property_count_strings(
- of_node, "pctrl-map-names");
- if (num_of_string <= 0) {
- CAM_ERR(CAM_UTIL, "no pinctrl-mapping found for: %s",
- soc_info->dev_name);
- device_pctrl->pinctrl = NULL;
- return -EINVAL;
- }
- if (num_of_map_idx != num_of_string) {
- CAM_ERR(CAM_UTIL,
- "Incorrect inputs mapping-idx count: %d mapping-names: %d",
- num_of_map_idx, num_of_string);
- device_pctrl->pinctrl = NULL;
- return -EINVAL;
- }
- if (num_of_map_idx > CAM_SOC_MAX_PINCTRL_MAP) {
- CAM_ERR(CAM_UTIL, "Invalid mapping %u max supported: %d",
- num_of_map_idx, CAM_SOC_MAX_PINCTRL_MAP);
- return -EINVAL;
- }
- for (i = 0; i < num_of_map_idx; i++) {
- of_property_read_u32_index(of_node,
- "pctrl-idx-mapping", i, &idx);
- if (idx >= CAM_SOC_MAX_PINCTRL_MAP) {
- CAM_ERR(CAM_UTIL, "Invalid Index: %d max supported: %d",
- idx, CAM_SOC_MAX_PINCTRL_MAP);
- return -EINVAL;
- }
- rc = of_property_read_string_index(
- of_node, "pctrl-map-names", i, &name);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "failed to read pinctrl-mapping at %d", i);
- return rc;
- }
- snprintf(pctrl_active, sizeof(pctrl_active),
- "%s%s", name, "_active");
- CAM_DBG(CAM_UTIL, "pctrl_active at index: %d name: %s",
- i, pctrl_active);
- snprintf(pctrl_suspend, sizeof(pctrl_suspend),
- "%s%s", name, "_suspend");
- CAM_DBG(CAM_UTIL, "pctrl_suspend at index: %d name: %s",
- i, pctrl_suspend);
- device_pctrl->pctrl_state[idx].gpio_state_active =
- pinctrl_lookup_state(device_pctrl->pinctrl,
- pctrl_active);
- if (IS_ERR_OR_NULL(
- device_pctrl->pctrl_state[idx].gpio_state_active)) {
- CAM_ERR(CAM_UTIL,
- "Failed to get the active state pinctrl handle");
- device_pctrl->pctrl_state[idx].gpio_state_active =
- NULL;
- return -EINVAL;
- }
- device_pctrl->pctrl_state[idx].gpio_state_suspend =
- pinctrl_lookup_state(device_pctrl->pinctrl,
- pctrl_suspend);
- if (IS_ERR_OR_NULL(
- device_pctrl->pctrl_state[idx].gpio_state_suspend)) {
- CAM_ERR(CAM_UTIL,
- "Failed to get the active state pinctrl handle");
- device_pctrl->pctrl_state[idx].gpio_state_suspend = NULL;
- return -EINVAL;
- }
- }
- return 0;
- }
- static void cam_soc_util_release_pinctrl(struct cam_hw_soc_info *soc_info)
- {
- if (soc_info->pinctrl_info.pinctrl)
- devm_pinctrl_put(soc_info->pinctrl_info.pinctrl);
- }
- static void cam_soc_util_regulator_disable_default(
- struct cam_hw_soc_info *soc_info)
- {
- int j = 0;
- uint32_t num_rgltr = soc_info->num_rgltr;
- for (j = num_rgltr-1; j >= 0; j--) {
- if (soc_info->rgltr_ctrl_support == true) {
- cam_soc_util_regulator_disable(soc_info->rgltr[j],
- soc_info->rgltr_name[j],
- soc_info->rgltr_min_volt[j],
- soc_info->rgltr_max_volt[j],
- soc_info->rgltr_op_mode[j],
- soc_info->rgltr_delay[j]);
- } else {
- if (soc_info->rgltr[j])
- regulator_disable(soc_info->rgltr[j]);
- }
- }
- }
- static int cam_soc_util_regulator_enable_default(
- struct cam_hw_soc_info *soc_info)
- {
- int j = 0, rc = 0;
- uint32_t num_rgltr = soc_info->num_rgltr;
- if (num_rgltr > CAM_SOC_MAX_REGULATOR) {
- CAM_ERR(CAM_UTIL,
- "%s has invalid regulator number %d",
- soc_info->dev_name, num_rgltr);
- return -EINVAL;
- }
- for (j = 0; j < num_rgltr; j++) {
- if (soc_info->rgltr_ctrl_support == true) {
- rc = cam_soc_util_regulator_enable(soc_info->rgltr[j],
- soc_info->rgltr_name[j],
- soc_info->rgltr_min_volt[j],
- soc_info->rgltr_max_volt[j],
- soc_info->rgltr_op_mode[j],
- soc_info->rgltr_delay[j]);
- } else {
- if (soc_info->rgltr[j])
- rc = regulator_enable(soc_info->rgltr[j]);
- }
- if (rc) {
- CAM_ERR(CAM_UTIL, "%s enable failed",
- soc_info->rgltr_name[j]);
- goto disable_rgltr;
- }
- }
- return rc;
- disable_rgltr:
- for (j--; j >= 0; j--) {
- if (soc_info->rgltr_ctrl_support == true) {
- cam_soc_util_regulator_disable(soc_info->rgltr[j],
- soc_info->rgltr_name[j],
- soc_info->rgltr_min_volt[j],
- soc_info->rgltr_max_volt[j],
- soc_info->rgltr_op_mode[j],
- soc_info->rgltr_delay[j]);
- } else {
- if (soc_info->rgltr[j])
- regulator_disable(soc_info->rgltr[j]);
- }
- }
- return rc;
- }
- static bool cam_soc_util_is_presil_address_space(unsigned long mem_block_start)
- {
- if(mem_block_start >= CAM_SS_START_PRESIL && mem_block_start < CAM_SS_START)
- return true;
- return false;
- }
- #ifndef CONFIG_CAM_PRESIL
- void __iomem * cam_soc_util_get_mem_base(
- unsigned long mem_block_start,
- unsigned long mem_block_size,
- const char *mem_block_name,
- uint32_t reserve_mem)
- {
- void __iomem * mem_base;
- if (reserve_mem) {
- if (!request_mem_region(mem_block_start,
- mem_block_size,
- mem_block_name)) {
- CAM_ERR(CAM_UTIL,
- "Error Mem region request Failed:%s",
- mem_block_name);
- return NULL;
- }
- }
- mem_base = ioremap(mem_block_start, mem_block_size);
- if (!mem_base) {
- CAM_ERR(CAM_UTIL, "get mem base failed");
- }
- return mem_base;
- }
- int cam_soc_util_request_irq(struct device *dev,
- unsigned int irq_line_start,
- irq_handler_t handler,
- unsigned long irqflags,
- const char *irq_name,
- void *irq_data,
- unsigned long mem_block_start)
- {
- int rc;
- rc = devm_request_irq(dev,
- irq_line_start,
- handler,
- IRQF_TRIGGER_RISING,
- irq_name,
- irq_data);
- if (rc) {
- CAM_ERR(CAM_UTIL, "irq request fail rc %d", rc);
- return -EBUSY;
- }
- disable_irq(irq_line_start);
- return rc;
- }
- #else
- void __iomem * cam_soc_util_get_mem_base(
- unsigned long mem_block_start,
- unsigned long mem_block_size,
- const char *mem_block_name,
- uint32_t reserve_mem)
- {
- void __iomem * mem_base;
- if(cam_soc_util_is_presil_address_space(mem_block_start))
- mem_base = (void __iomem *)mem_block_start;
- else {
- if (reserve_mem) {
- if (!request_mem_region(mem_block_start,
- mem_block_size,
- mem_block_name)) {
- CAM_ERR(CAM_UTIL,
- "Error Mem region request Failed:%s",
- mem_block_name);
- return NULL;
- }
- }
- mem_base = ioremap(mem_block_start, mem_block_size);
- }
- if (!mem_base) {
- CAM_ERR(CAM_UTIL, "get mem base failed");
- }
- return mem_base;
- }
- int cam_soc_util_request_irq(struct device *dev,
- unsigned int irq_line_start,
- irq_handler_t handler,
- unsigned long irqflags,
- const char *irq_name,
- void *irq_data,
- unsigned long mem_block_start)
- {
- int rc;
- if(cam_soc_util_is_presil_address_space(mem_block_start)) {
- rc = devm_request_irq(dev,
- irq_line_start,
- handler,
- irqflags,
- irq_name,
- irq_data);
- if (rc) {
- CAM_ERR(CAM_UTIL, "presil irq request fail");
- return -EBUSY;
- }
- disable_irq(irq_line_start);
- rc = !(cam_presil_subscribe_device_irq(irq_line_start,
- handler, irq_data, irq_name));
- CAM_DBG(CAM_PRESIL, "Subscribe presil IRQ: rc=%d NUM=%d Name=%s handler=0x%x",
- rc, irq_line_start, irq_name, handler);
- if (rc) {
- CAM_ERR(CAM_UTIL, "presil irq request fail");
- return -EBUSY;
- }
- } else {
- rc = devm_request_irq(dev,
- irq_line_start,
- handler,
- irqflags,
- irq_name,
- irq_data);
- if (rc) {
- CAM_ERR(CAM_UTIL, "irq request fail");
- return -EBUSY;
- }
- disable_irq(irq_line_start);
- CAM_INFO(CAM_UTIL, "Subscribe for non-presil IRQ success");
- }
- CAM_INFO(CAM_UTIL, "returning IRQ for mem_block_start 0x%0x rc %d",
- mem_block_start, rc);
- return rc;
- }
- #endif
- int cam_soc_util_request_platform_resource(
- struct cam_hw_soc_info *soc_info,
- irq_handler_t handler, void *irq_data)
- {
- int i = 0, rc = 0;
- if (!soc_info || !soc_info->dev) {
- CAM_ERR(CAM_UTIL, "Invalid parameters");
- return -EINVAL;
- }
- for (i = 0; i < soc_info->num_mem_block; i++) {
- soc_info->reg_map[i].mem_base = cam_soc_util_get_mem_base(
- soc_info->mem_block[i]->start,
- resource_size(soc_info->mem_block[i]),
- soc_info->mem_block_name[i],
- soc_info->reserve_mem);
- if (!soc_info->reg_map[i].mem_base) {
- CAM_ERR(CAM_UTIL, "i= %d base NULL", i);
- rc = -ENOMEM;
- goto unmap_base;
- }
- soc_info->reg_map[i].mem_cam_base =
- soc_info->mem_block_cam_base[i];
- soc_info->reg_map[i].size =
- resource_size(soc_info->mem_block[i]);
- soc_info->num_reg_map++;
- }
- for (i = 0; i < soc_info->num_rgltr; i++) {
- if (soc_info->rgltr_name[i] == NULL) {
- CAM_ERR(CAM_UTIL, "can't find regulator name");
- goto put_regulator;
- }
- rc = cam_soc_util_get_regulator(soc_info->dev,
- &soc_info->rgltr[i],
- soc_info->rgltr_name[i]);
- if (rc)
- goto put_regulator;
- }
- if (soc_info->irq_num > 0) {
- rc = cam_soc_util_request_irq(soc_info->dev,
- soc_info->irq_num,
- handler, IRQF_TRIGGER_RISING,
- soc_info->irq_name, irq_data,
- soc_info->mem_block[0]->start);
- if (rc) {
- CAM_ERR(CAM_UTIL, "irq request fail");
- rc = -EBUSY;
- goto put_regulator;
- }
- soc_info->irq_data = irq_data;
- }
- /* Get Clock */
- for (i = 0; i < soc_info->num_clk; i++) {
- soc_info->clk[i] = clk_get(soc_info->dev,
- soc_info->clk_name[i]);
- if (IS_ERR(soc_info->clk[i])) {
- CAM_ERR(CAM_UTIL, "get failed for %s",
- soc_info->clk_name[i]);
- rc = -ENOENT;
- goto put_clk;
- } else if (!soc_info->clk[i]) {
- CAM_DBG(CAM_UTIL, "%s handle is NULL skip get",
- soc_info->clk_name[i]);
- continue;
- }
- /* Create a wrapper entry if this is a shared clock */
- if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i)) {
- uint32_t min_level = soc_info->lowest_clk_level;
- CAM_DBG(CAM_UTIL,
- "Dev %s, clk %s, id %d register wrapper entry for shared clk",
- soc_info->dev_name, soc_info->clk_name[i],
- soc_info->clk_id[i]);
- rc = cam_soc_util_clk_wrapper_register_entry(
- soc_info->clk_id[i], soc_info->clk[i],
- (i == soc_info->src_clk_idx) ? true : false,
- soc_info, soc_info->clk_rate[min_level][i],
- soc_info->clk_name[i]);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Failed in registering shared clk Dev %s id %d",
- soc_info->dev_name,
- soc_info->clk_id[i]);
- clk_put(soc_info->clk[i]);
- soc_info->clk[i] = NULL;
- goto put_clk;
- }
- } else if (i == soc_info->src_clk_idx) {
- rc = cam_soc_util_register_mmrm_client(
- soc_info->clk_id[i], soc_info->clk[i],
- soc_info->is_nrt_dev,
- soc_info, soc_info->clk_name[i],
- &soc_info->mmrm_handle);
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Failed in register mmrm client Dev %s clk id %d",
- soc_info->dev_name,
- soc_info->clk_id[i]);
- clk_put(soc_info->clk[i]);
- soc_info->clk[i] = NULL;
- goto put_clk;
- }
- }
- }
- rc = cam_soc_util_request_pinctrl(soc_info);
- if (rc) {
- CAM_ERR(CAM_UTIL, "Failed in requesting Pinctrl, rc: %d", rc);
- goto put_clk;
- }
- rc = cam_soc_util_request_gpio_table(soc_info, true);
- if (rc) {
- CAM_ERR(CAM_UTIL, "Failed in request gpio table, rc=%d", rc);
- goto put_clk;
- }
- if (soc_info->clk_control_enable)
- cam_soc_util_create_clk_lvl_debugfs(soc_info);
- return rc;
- put_clk:
- if (soc_info->mmrm_handle) {
- cam_soc_util_unregister_mmrm_client(soc_info->mmrm_handle);
- soc_info->mmrm_handle = NULL;
- }
- if (i == -1)
- i = soc_info->num_clk;
- for (i = i - 1; i >= 0; i--) {
- if (soc_info->clk[i]) {
- if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i))
- cam_soc_util_clk_wrapper_unregister_entry(
- soc_info->clk_id[i], soc_info);
- clk_put(soc_info->clk[i]);
- soc_info->clk[i] = NULL;
- }
- }
- if (soc_info->irq_num > 0) {
- disable_irq(soc_info->irq_num);
- devm_free_irq(soc_info->dev,
- soc_info->irq_num, irq_data);
- }
- put_regulator:
- if (i == -1)
- i = soc_info->num_rgltr;
- for (i = i - 1; i >= 0; i--) {
- if (soc_info->rgltr[i]) {
- regulator_disable(soc_info->rgltr[i]);
- regulator_put(soc_info->rgltr[i]);
- soc_info->rgltr[i] = NULL;
- }
- }
- unmap_base:
- if (i == -1)
- i = soc_info->num_reg_map;
- for (i = i - 1; i >= 0; i--) {
- if (soc_info->reserve_mem)
- release_mem_region(soc_info->mem_block[i]->start,
- resource_size(soc_info->mem_block[i]));
- iounmap(soc_info->reg_map[i].mem_base);
- soc_info->reg_map[i].mem_base = NULL;
- soc_info->reg_map[i].size = 0;
- }
- return rc;
- }
- int cam_soc_util_release_platform_resource(struct cam_hw_soc_info *soc_info)
- {
- int i;
- bool b_ret = false;
- if (!soc_info || !soc_info->dev) {
- CAM_ERR(CAM_UTIL, "Invalid parameter");
- return -EINVAL;
- }
- if (soc_info->mmrm_handle) {
- cam_soc_util_unregister_mmrm_client(soc_info->mmrm_handle);
- soc_info->mmrm_handle = NULL;
- }
- for (i = soc_info->num_clk - 1; i >= 0; i--) {
- if (CAM_IS_BIT_SET(soc_info->shared_clk_mask, i))
- cam_soc_util_clk_wrapper_unregister_entry(
- soc_info->clk_id[i], soc_info);
- if (!soc_info->clk[i]) {
- CAM_DBG(CAM_UTIL, "%s handle is NULL skip put",
- soc_info->clk_name[i]);
- continue;
- }
- clk_put(soc_info->clk[i]);
- soc_info->clk[i] = NULL;
- }
- for (i = soc_info->num_rgltr - 1; i >= 0; i--) {
- if (soc_info->rgltr[i]) {
- regulator_put(soc_info->rgltr[i]);
- soc_info->rgltr[i] = NULL;
- }
- }
- for (i = soc_info->num_reg_map - 1; i >= 0; i--) {
- iounmap(soc_info->reg_map[i].mem_base);
- soc_info->reg_map[i].mem_base = NULL;
- soc_info->reg_map[i].size = 0;
- }
- if (soc_info->irq_num > 0) {
- if (cam_presil_mode_enabled()) {
- if (cam_soc_util_is_presil_address_space(soc_info->mem_block[0]->start)) {
- b_ret = cam_presil_unsubscribe_device_irq(
- soc_info->irq_line->start);
- CAM_DBG(CAM_PRESIL, "UnSubscribe IRQ: Ret=%d NUM=%d Name=%s",
- b_ret, soc_info->irq_line->start, soc_info->irq_name);
- }
- }
- disable_irq(soc_info->irq_num);
- devm_free_irq(soc_info->dev,
- soc_info->irq_num, soc_info->irq_data);
- }
- cam_soc_util_release_pinctrl(soc_info);
- /* release for gpio */
- cam_soc_util_request_gpio_table(soc_info, false);
- soc_info->dentry = NULL;
- return 0;
- }
- int cam_soc_util_enable_platform_resource(struct cam_hw_soc_info *soc_info,
- bool enable_clocks, enum cam_vote_level clk_level, bool enable_irq)
- {
- int rc = 0;
- if (!soc_info)
- return -EINVAL;
- rc = cam_soc_util_regulator_enable_default(soc_info);
- if (rc) {
- CAM_ERR(CAM_UTIL, "Regulators enable failed");
- return rc;
- }
- if (enable_clocks) {
- rc = cam_soc_util_clk_enable_default(soc_info, clk_level);
- if (rc)
- goto disable_regulator;
- }
- if (enable_irq) {
- rc = cam_soc_util_irq_enable(soc_info);
- if (rc)
- goto disable_clk;
- }
- return rc;
- disable_clk:
- if (enable_clocks)
- cam_soc_util_clk_disable_default(soc_info);
- disable_regulator:
- cam_soc_util_regulator_disable_default(soc_info);
- return rc;
- }
- int cam_soc_util_disable_platform_resource(struct cam_hw_soc_info *soc_info,
- bool disable_clocks, bool disable_irq)
- {
- int rc = 0;
- if (!soc_info)
- return -EINVAL;
- if (disable_irq)
- rc |= cam_soc_util_irq_disable(soc_info);
- if (disable_clocks)
- cam_soc_util_clk_disable_default(soc_info);
- cam_soc_util_regulator_disable_default(soc_info);
- return rc;
- }
- int cam_soc_util_reg_dump(struct cam_hw_soc_info *soc_info,
- uint32_t base_index, uint32_t offset, int size)
- {
- void __iomem *base_addr = NULL;
- CAM_DBG(CAM_UTIL, "base_idx %u size=%d", base_index, size);
- if (!soc_info || base_index >= soc_info->num_reg_map ||
- size <= 0 || (offset + size) >=
- CAM_SOC_GET_REG_MAP_SIZE(soc_info, base_index))
- return -EINVAL;
- base_addr = CAM_SOC_GET_REG_MAP_START(soc_info, base_index);
- /*
- * All error checking already done above,
- * hence ignoring the return value below.
- */
- cam_io_dump(base_addr, offset, size);
- return 0;
- }
- static int cam_soc_util_dump_cont_reg_range(
- struct cam_hw_soc_info *soc_info,
- struct cam_reg_range_read_desc *reg_read, uint32_t base_idx,
- struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
- {
- int i = 0, rc = 0;
- uint32_t write_idx = 0;
- if (!soc_info || !dump_out_buf || !reg_read || !cmd_buf_end) {
- CAM_ERR(CAM_UTIL,
- "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK cmd_buf_end: %pK",
- soc_info, dump_out_buf, reg_read, cmd_buf_end);
- rc = -EINVAL;
- goto end;
- }
- if ((reg_read->num_values) && ((reg_read->num_values > U32_MAX / 2) ||
- (sizeof(uint32_t) > ((U32_MAX -
- sizeof(struct cam_reg_dump_out_buffer) -
- dump_out_buf->bytes_written) /
- (reg_read->num_values * 2))))) {
- CAM_ERR(CAM_UTIL,
- "Integer Overflow bytes_written: [%u] num_values: [%u]",
- dump_out_buf->bytes_written, reg_read->num_values);
- rc = -EOVERFLOW;
- goto end;
- }
- if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
- (uintptr_t)(sizeof(struct cam_reg_dump_out_buffer)
- - sizeof(uint32_t) + dump_out_buf->bytes_written +
- (reg_read->num_values * 2 * sizeof(uint32_t)))) {
- CAM_ERR(CAM_UTIL,
- "Insufficient space in out buffer num_values: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
- reg_read->num_values, cmd_buf_end,
- (uintptr_t)dump_out_buf);
- rc = -EINVAL;
- goto end;
- }
- write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
- for (i = 0; i < reg_read->num_values; i++) {
- if ((reg_read->offset + (i * sizeof(uint32_t))) >
- (uint32_t)soc_info->reg_map[base_idx].size) {
- CAM_ERR(CAM_UTIL,
- "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
- (reg_read->offset + (i * sizeof(uint32_t))),
- (uint32_t)soc_info->reg_map[base_idx].size);
- rc = -EINVAL;
- goto end;
- }
- dump_out_buf->dump_data[write_idx++] = reg_read->offset +
- (i * sizeof(uint32_t));
- dump_out_buf->dump_data[write_idx++] =
- cam_soc_util_r(soc_info, base_idx,
- (reg_read->offset + (i * sizeof(uint32_t))));
- dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
- }
- end:
- return rc;
- }
- static int cam_soc_util_dump_dmi_reg_range(
- struct cam_hw_soc_info *soc_info,
- struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
- struct cam_reg_dump_out_buffer *dump_out_buf, uintptr_t cmd_buf_end)
- {
- int i = 0, rc = 0;
- uint32_t write_idx = 0;
- if (!soc_info || !dump_out_buf || !dmi_read || !cmd_buf_end) {
- CAM_ERR(CAM_UTIL,
- "Invalid input args soc_info: %pK, dump_out_buffer: %pK",
- soc_info, dump_out_buf);
- rc = -EINVAL;
- goto end;
- }
- if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
- dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
- CAM_ERR(CAM_UTIL,
- "Invalid number of requested writes, pre: %d post: %d",
- dmi_read->num_pre_writes, dmi_read->num_post_writes);
- rc = -EINVAL;
- goto end;
- }
- if ((dmi_read->num_pre_writes + dmi_read->dmi_data_read.num_values)
- && ((dmi_read->num_pre_writes > U32_MAX / 2) ||
- (dmi_read->dmi_data_read.num_values > U32_MAX / 2) ||
- ((dmi_read->num_pre_writes * 2) > U32_MAX -
- (dmi_read->dmi_data_read.num_values * 2)) ||
- (sizeof(uint32_t) > ((U32_MAX -
- sizeof(struct cam_reg_dump_out_buffer) -
- dump_out_buf->bytes_written) / ((dmi_read->num_pre_writes +
- dmi_read->dmi_data_read.num_values) * 2))))) {
- CAM_ERR(CAM_UTIL,
- "Integer Overflow bytes_written: [%u] num_pre_writes: [%u] num_values: [%u]",
- dump_out_buf->bytes_written, dmi_read->num_pre_writes,
- dmi_read->dmi_data_read.num_values);
- rc = -EOVERFLOW;
- goto end;
- }
- if ((cmd_buf_end - (uintptr_t)dump_out_buf) <=
- (uintptr_t)(
- sizeof(struct cam_reg_dump_out_buffer) - sizeof(uint32_t) +
- (dump_out_buf->bytes_written +
- (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
- (dmi_read->dmi_data_read.num_values * 2 *
- sizeof(uint32_t))))) {
- CAM_ERR(CAM_UTIL,
- "Insufficient space in out buffer num_read_val: [%d] num_write_val: [%d] cmd_buf_end: %pK dump_out_buf: %pK",
- dmi_read->dmi_data_read.num_values,
- dmi_read->num_pre_writes, cmd_buf_end,
- (uintptr_t)dump_out_buf);
- rc = -EINVAL;
- goto end;
- }
- write_idx = dump_out_buf->bytes_written / sizeof(uint32_t);
- for (i = 0; i < dmi_read->num_pre_writes; i++) {
- if (dmi_read->pre_read_config[i].offset >
- (uint32_t)soc_info->reg_map[base_idx].size) {
- CAM_ERR(CAM_UTIL,
- "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
- dmi_read->pre_read_config[i].offset,
- (uint32_t)soc_info->reg_map[base_idx].size);
- rc = -EINVAL;
- goto end;
- }
- cam_soc_util_w_mb(soc_info, base_idx,
- dmi_read->pre_read_config[i].offset,
- dmi_read->pre_read_config[i].value);
- dump_out_buf->dump_data[write_idx++] =
- dmi_read->pre_read_config[i].offset;
- dump_out_buf->dump_data[write_idx++] =
- dmi_read->pre_read_config[i].value;
- dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
- }
- if (dmi_read->dmi_data_read.offset >
- (uint32_t)soc_info->reg_map[base_idx].size) {
- CAM_ERR(CAM_UTIL,
- "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
- dmi_read->dmi_data_read.offset,
- (uint32_t)soc_info->reg_map[base_idx].size);
- rc = -EINVAL;
- goto end;
- }
- for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
- dump_out_buf->dump_data[write_idx++] =
- dmi_read->dmi_data_read.offset;
- dump_out_buf->dump_data[write_idx++] =
- cam_soc_util_r_mb(soc_info, base_idx,
- dmi_read->dmi_data_read.offset);
- dump_out_buf->bytes_written += (2 * sizeof(uint32_t));
- }
- for (i = 0; i < dmi_read->num_post_writes; i++) {
- if (dmi_read->post_read_config[i].offset >
- (uint32_t)soc_info->reg_map[base_idx].size) {
- CAM_ERR(CAM_UTIL,
- "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
- dmi_read->post_read_config[i].offset,
- (uint32_t)soc_info->reg_map[base_idx].size);
- rc = -EINVAL;
- goto end;
- }
- cam_soc_util_w_mb(soc_info, base_idx,
- dmi_read->post_read_config[i].offset,
- dmi_read->post_read_config[i].value);
- }
- end:
- return rc;
- }
- static int cam_soc_util_dump_dmi_reg_range_user_buf(
- struct cam_hw_soc_info *soc_info,
- struct cam_dmi_read_desc *dmi_read, uint32_t base_idx,
- struct cam_hw_soc_dump_args *dump_args)
- {
- int i;
- int rc;
- size_t buf_len = 0;
- uint8_t *dst;
- size_t remain_len;
- uint32_t min_len;
- uint32_t *waddr, *start;
- uintptr_t cpu_addr;
- struct cam_hw_soc_dump_header *hdr;
- if (!soc_info || !dump_args || !dmi_read) {
- CAM_ERR(CAM_UTIL,
- "Invalid input args soc_info: %pK, dump_args: %pK",
- soc_info, dump_args);
- rc = -EINVAL;
- goto end;
- }
- if (dmi_read->num_pre_writes > CAM_REG_DUMP_DMI_CONFIG_MAX ||
- dmi_read->num_post_writes > CAM_REG_DUMP_DMI_CONFIG_MAX) {
- CAM_ERR(CAM_UTIL,
- "Invalid number of requested writes, pre: %d post: %d",
- dmi_read->num_pre_writes, dmi_read->num_post_writes);
- rc = -EINVAL;
- goto end;
- }
- rc = cam_mem_get_cpu_buf(dump_args->buf_handle, &cpu_addr, &buf_len);
- if (rc) {
- CAM_ERR(CAM_UTIL, "Invalid handle %u rc %d",
- dump_args->buf_handle, rc);
- goto end;
- }
- if (buf_len <= dump_args->offset) {
- CAM_WARN(CAM_UTIL, "Dump offset overshoot offset %zu len %zu",
- dump_args->offset, buf_len);
- rc = -ENOSPC;
- goto end;
- }
- remain_len = buf_len - dump_args->offset;
- min_len = (dmi_read->num_pre_writes * 2 * sizeof(uint32_t)) +
- (dmi_read->dmi_data_read.num_values * 2 * sizeof(uint32_t)) +
- sizeof(uint32_t);
- if (remain_len < min_len) {
- CAM_WARN(CAM_UTIL,
- "Dump Buffer exhaust read %d write %d remain %zu min %u",
- dmi_read->dmi_data_read.num_values,
- dmi_read->num_pre_writes, remain_len,
- min_len);
- rc = -ENOSPC;
- goto end;
- }
- dst = (uint8_t *)cpu_addr + dump_args->offset;
- hdr = (struct cam_hw_soc_dump_header *)dst;
- memset(hdr, 0, sizeof(struct cam_hw_soc_dump_header));
- scnprintf(hdr->tag, CAM_SOC_HW_DUMP_TAG_MAX_LEN,
- "DMI_DUMP:");
- waddr = (uint32_t *)(dst + sizeof(struct cam_hw_soc_dump_header));
- start = waddr;
- hdr->word_size = sizeof(uint32_t);
- *waddr = soc_info->index;
- waddr++;
- for (i = 0; i < dmi_read->num_pre_writes; i++) {
- if (dmi_read->pre_read_config[i].offset >
- (uint32_t)soc_info->reg_map[base_idx].size) {
- CAM_ERR(CAM_UTIL,
- "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
- dmi_read->pre_read_config[i].offset,
- (uint32_t)soc_info->reg_map[base_idx].size);
- rc = -EINVAL;
- goto end;
- }
- cam_soc_util_w_mb(soc_info, base_idx,
- dmi_read->pre_read_config[i].offset,
- dmi_read->pre_read_config[i].value);
- *waddr++ = dmi_read->pre_read_config[i].offset;
- *waddr++ = dmi_read->pre_read_config[i].value;
- }
- if (dmi_read->dmi_data_read.offset >
- (uint32_t)soc_info->reg_map[base_idx].size) {
- CAM_ERR(CAM_UTIL,
- "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
- dmi_read->dmi_data_read.offset,
- (uint32_t)soc_info->reg_map[base_idx].size);
- rc = -EINVAL;
- goto end;
- }
- for (i = 0; i < dmi_read->dmi_data_read.num_values; i++) {
- *waddr++ = dmi_read->dmi_data_read.offset;
- *waddr++ = cam_soc_util_r_mb(soc_info, base_idx,
- dmi_read->dmi_data_read.offset);
- }
- for (i = 0; i < dmi_read->num_post_writes; i++) {
- if (dmi_read->post_read_config[i].offset >
- (uint32_t)soc_info->reg_map[base_idx].size) {
- CAM_ERR(CAM_UTIL,
- "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
- dmi_read->post_read_config[i].offset,
- (uint32_t)soc_info->reg_map[base_idx].size);
- rc = -EINVAL;
- goto end;
- }
- cam_soc_util_w_mb(soc_info, base_idx,
- dmi_read->post_read_config[i].offset,
- dmi_read->post_read_config[i].value);
- }
- hdr->size = (waddr - start) * hdr->word_size;
- dump_args->offset += hdr->size +
- sizeof(struct cam_hw_soc_dump_header);
- end:
- return rc;
- }
- static int cam_soc_util_dump_cont_reg_range_user_buf(
- struct cam_hw_soc_info *soc_info,
- struct cam_reg_range_read_desc *reg_read,
- uint32_t base_idx,
- struct cam_hw_soc_dump_args *dump_args)
- {
- int i;
- int rc = 0;
- size_t buf_len;
- uint8_t *dst;
- size_t remain_len;
- uint32_t min_len;
- uint32_t *waddr, *start;
- uintptr_t cpu_addr;
- struct cam_hw_soc_dump_header *hdr;
- if (!soc_info || !dump_args || !reg_read) {
- CAM_ERR(CAM_UTIL,
- "Invalid input args soc_info: %pK, dump_out_buffer: %pK reg_read: %pK",
- soc_info, dump_args, reg_read);
- rc = -EINVAL;
- goto end;
- }
- rc = cam_mem_get_cpu_buf(dump_args->buf_handle, &cpu_addr, &buf_len);
- if (rc) {
- CAM_ERR(CAM_UTIL, "Invalid handle %u rc %d",
- dump_args->buf_handle, rc);
- goto end;
- }
- if (buf_len <= dump_args->offset) {
- CAM_WARN(CAM_UTIL, "Dump offset overshoot %zu %zu",
- dump_args->offset, buf_len);
- rc = -ENOSPC;
- goto end;
- }
- remain_len = buf_len - dump_args->offset;
- min_len = (reg_read->num_values * 2 * sizeof(uint32_t)) +
- sizeof(struct cam_hw_soc_dump_header) + sizeof(uint32_t);
- if (remain_len < min_len) {
- CAM_WARN(CAM_UTIL,
- "Dump Buffer exhaust read_values %d remain %zu min %u",
- reg_read->num_values,
- remain_len,
- min_len);
- rc = -ENOSPC;
- goto end;
- }
- dst = (uint8_t *)cpu_addr + dump_args->offset;
- hdr = (struct cam_hw_soc_dump_header *)dst;
- memset(hdr, 0, sizeof(struct cam_hw_soc_dump_header));
- scnprintf(hdr->tag, CAM_SOC_HW_DUMP_TAG_MAX_LEN, "%s_REG:",
- soc_info->dev_name);
- waddr = (uint32_t *)(dst + sizeof(struct cam_hw_soc_dump_header));
- start = waddr;
- hdr->word_size = sizeof(uint32_t);
- *waddr = soc_info->index;
- waddr++;
- for (i = 0; i < reg_read->num_values; i++) {
- if ((reg_read->offset + (i * sizeof(uint32_t))) >
- (uint32_t)soc_info->reg_map[base_idx].size) {
- CAM_ERR(CAM_UTIL,
- "Reg offset out of range, offset: 0x%X reg_map size: 0x%X",
- (reg_read->offset + (i * sizeof(uint32_t))),
- (uint32_t)soc_info->reg_map[base_idx].size);
- rc = -EINVAL;
- goto end;
- }
- waddr[0] = reg_read->offset + (i * sizeof(uint32_t));
- waddr[1] = cam_soc_util_r(soc_info, base_idx,
- (reg_read->offset + (i * sizeof(uint32_t))));
- waddr += 2;
- }
- hdr->size = (waddr - start) * hdr->word_size;
- dump_args->offset += hdr->size +
- sizeof(struct cam_hw_soc_dump_header);
- end:
- return rc;
- }
- static int cam_soc_util_user_reg_dump(
- struct cam_reg_dump_desc *reg_dump_desc,
- struct cam_hw_soc_dump_args *dump_args,
- struct cam_hw_soc_info *soc_info,
- uint32_t reg_base_idx)
- {
- int rc = 0;
- int i;
- struct cam_reg_read_info *reg_read_info = NULL;
- if (!dump_args || !reg_dump_desc || !soc_info) {
- CAM_ERR(CAM_UTIL,
- "Invalid input parameters %pK %pK %pK",
- dump_args, reg_dump_desc, soc_info);
- return -EINVAL;
- }
- for (i = 0; i < reg_dump_desc->num_read_range; i++) {
- reg_read_info = ®_dump_desc->read_range[i];
- if (reg_read_info->type ==
- CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
- rc = cam_soc_util_dump_cont_reg_range_user_buf(
- soc_info,
- ®_read_info->reg_read,
- reg_base_idx,
- dump_args);
- } else if (reg_read_info->type ==
- CAM_REG_DUMP_READ_TYPE_DMI) {
- rc = cam_soc_util_dump_dmi_reg_range_user_buf(
- soc_info,
- ®_read_info->dmi_read,
- reg_base_idx,
- dump_args);
- } else {
- CAM_ERR(CAM_UTIL,
- "Invalid Reg dump read type: %d",
- reg_read_info->type);
- rc = -EINVAL;
- goto end;
- }
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Reg range read failed rc: %d reg_base_idx: %d",
- rc, reg_base_idx);
- goto end;
- }
- }
- end:
- return rc;
- }
- int cam_soc_util_reg_dump_to_cmd_buf(void *ctx,
- struct cam_cmd_buf_desc *cmd_desc, uint64_t req_id,
- cam_soc_util_regspace_data_cb reg_data_cb,
- struct cam_hw_soc_dump_args *soc_dump_args,
- bool user_triggered_dump)
- {
- int rc = 0, i, j;
- uintptr_t cpu_addr = 0;
- uintptr_t cmd_buf_start = 0;
- uintptr_t cmd_in_data_end = 0;
- uintptr_t cmd_buf_end = 0;
- uint32_t reg_base_type = 0;
- size_t buf_size = 0, remain_len = 0;
- struct cam_reg_dump_input_info *reg_input_info = NULL;
- struct cam_reg_dump_desc *reg_dump_desc = NULL;
- struct cam_reg_dump_out_buffer *dump_out_buf = NULL;
- struct cam_reg_read_info *reg_read_info = NULL;
- struct cam_hw_soc_info *soc_info;
- uint32_t reg_base_idx = 0;
- if (!ctx || !cmd_desc || !reg_data_cb) {
- CAM_ERR(CAM_UTIL, "Invalid args to reg dump [%pK] [%pK]",
- cmd_desc, reg_data_cb);
- return -EINVAL;
- }
- if (!cmd_desc->length || !cmd_desc->size) {
- CAM_ERR(CAM_UTIL, "Invalid cmd buf size %d %d",
- cmd_desc->length, cmd_desc->size);
- return -EINVAL;
- }
- rc = cam_mem_get_cpu_buf(cmd_desc->mem_handle, &cpu_addr, &buf_size);
- if (rc || !cpu_addr || (buf_size == 0)) {
- CAM_ERR(CAM_UTIL, "Failed in Get cpu addr, rc=%d, cpu_addr=%pK",
- rc, (void *)cpu_addr);
- goto end;
- }
- CAM_DBG(CAM_UTIL, "Get cpu buf success req_id: %llu buf_size: %zu",
- req_id, buf_size);
- if ((buf_size < sizeof(uint32_t)) ||
- ((size_t)cmd_desc->offset > (buf_size - sizeof(uint32_t)))) {
- CAM_ERR(CAM_UTIL, "Invalid offset for cmd buf: %zu",
- (size_t)cmd_desc->offset);
- rc = -EINVAL;
- goto end;
- }
- remain_len = buf_size - (size_t)cmd_desc->offset;
- if ((remain_len < (size_t)cmd_desc->size) || (cmd_desc->size <
- cmd_desc->length)) {
- CAM_ERR(CAM_UTIL,
- "Invalid params for cmd buf len: %zu size: %zu remain_len: %zu",
- (size_t)cmd_desc->length, (size_t)cmd_desc->length,
- remain_len);
- rc = -EINVAL;
- goto end;
- }
- cmd_buf_start = cpu_addr + (uintptr_t)cmd_desc->offset;
- cmd_in_data_end = cmd_buf_start + (uintptr_t)cmd_desc->length;
- cmd_buf_end = cmd_buf_start + (uintptr_t)cmd_desc->size;
- if ((cmd_buf_end <= cmd_buf_start) ||
- (cmd_in_data_end <= cmd_buf_start)) {
- CAM_ERR(CAM_UTIL,
- "Invalid length or size for cmd buf: [%zu] [%zu]",
- (size_t)cmd_desc->length, (size_t)cmd_desc->size);
- rc = -EINVAL;
- goto end;
- }
- CAM_DBG(CAM_UTIL,
- "Buffer params start [%pK] input_end [%pK] buf_end [%pK]",
- cmd_buf_start, cmd_in_data_end, cmd_buf_end);
- reg_input_info = (struct cam_reg_dump_input_info *) cmd_buf_start;
- if ((reg_input_info->num_dump_sets > 1) && (sizeof(uint32_t) >
- ((U32_MAX - sizeof(struct cam_reg_dump_input_info)) /
- (reg_input_info->num_dump_sets - 1)))) {
- CAM_ERR(CAM_UTIL,
- "Integer Overflow req_id: [%llu] num_dump_sets: [%u]",
- req_id, reg_input_info->num_dump_sets);
- rc = -EOVERFLOW;
- goto end;
- }
- if ((!reg_input_info->num_dump_sets) ||
- ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
- (sizeof(struct cam_reg_dump_input_info) +
- ((reg_input_info->num_dump_sets - 1) * sizeof(uint32_t))))) {
- CAM_ERR(CAM_UTIL,
- "Invalid number of dump sets, req_id: [%llu] num_dump_sets: [%u]",
- req_id, reg_input_info->num_dump_sets);
- rc = -EINVAL;
- goto end;
- }
- CAM_DBG(CAM_UTIL,
- "reg_input_info req_id: %llu ctx %pK num_dump_sets: %d",
- req_id, ctx, reg_input_info->num_dump_sets);
- for (i = 0; i < reg_input_info->num_dump_sets; i++) {
- if ((cmd_in_data_end - cmd_buf_start) <= (uintptr_t)
- reg_input_info->dump_set_offsets[i]) {
- CAM_ERR(CAM_UTIL,
- "Invalid dump set offset: [%pK], cmd_buf_start: [%pK] cmd_in_data_end: [%pK]",
- (uintptr_t)reg_input_info->dump_set_offsets[i],
- cmd_buf_start, cmd_in_data_end);
- rc = -EINVAL;
- goto end;
- }
- reg_dump_desc = (struct cam_reg_dump_desc *)
- (cmd_buf_start +
- (uintptr_t)reg_input_info->dump_set_offsets[i]);
- if ((reg_dump_desc->num_read_range > 1) &&
- (sizeof(struct cam_reg_read_info) > ((U32_MAX -
- sizeof(struct cam_reg_dump_desc)) /
- (reg_dump_desc->num_read_range - 1)))) {
- CAM_ERR(CAM_UTIL,
- "Integer Overflow req_id: [%llu] num_read_range: [%u]",
- req_id, reg_dump_desc->num_read_range);
- rc = -EOVERFLOW;
- goto end;
- }
- if ((!reg_dump_desc->num_read_range) ||
- ((cmd_in_data_end - (uintptr_t)reg_dump_desc) <=
- (uintptr_t)(sizeof(struct cam_reg_dump_desc) +
- ((reg_dump_desc->num_read_range - 1) *
- sizeof(struct cam_reg_read_info))))) {
- CAM_ERR(CAM_UTIL,
- "Invalid number of read ranges, req_id: [%llu] num_read_range: [%d]",
- req_id, reg_dump_desc->num_read_range);
- rc = -EINVAL;
- goto end;
- }
- if ((cmd_buf_end - cmd_buf_start) <= (uintptr_t)
- (reg_dump_desc->dump_buffer_offset +
- sizeof(struct cam_reg_dump_out_buffer))) {
- CAM_ERR(CAM_UTIL,
- "Invalid out buffer offset: [%pK], cmd_buf_start: [%pK] cmd_buf_end: [%pK]",
- (uintptr_t)reg_dump_desc->dump_buffer_offset,
- cmd_buf_start, cmd_buf_end);
- rc = -EINVAL;
- goto end;
- }
- reg_base_type = reg_dump_desc->reg_base_type;
- if (reg_base_type == 0 || reg_base_type >
- CAM_REG_DUMP_BASE_TYPE_SFE_RIGHT) {
- CAM_ERR(CAM_UTIL,
- "Invalid Reg dump base type: %d",
- reg_base_type);
- rc = -EINVAL;
- goto end;
- }
- rc = reg_data_cb(reg_base_type, ctx, &soc_info, ®_base_idx);
- if (rc || !soc_info) {
- CAM_ERR(CAM_UTIL,
- "Reg space data callback failed rc: %d soc_info: [%pK]",
- rc, soc_info);
- rc = -EINVAL;
- goto end;
- }
- if (reg_base_idx > soc_info->num_reg_map) {
- CAM_ERR(CAM_UTIL,
- "Invalid reg base idx: %d num reg map: %d",
- reg_base_idx, soc_info->num_reg_map);
- rc = -EINVAL;
- goto end;
- }
- CAM_DBG(CAM_UTIL,
- "Reg data callback success req_id: %llu base_type: %d base_idx: %d num_read_range: %d",
- req_id, reg_base_type, reg_base_idx,
- reg_dump_desc->num_read_range);
- /* If the dump request is triggered by user space
- * buffer will be different from the buffer which is received
- * in init packet. In this case, dump the data to the
- * user provided buffer and exit.
- */
- if (user_triggered_dump) {
- rc = cam_soc_util_user_reg_dump(reg_dump_desc,
- soc_dump_args, soc_info, reg_base_idx);
- CAM_INFO(CAM_UTIL,
- "%s reg_base_idx %d dumped offset %u",
- soc_info->dev_name, reg_base_idx,
- soc_dump_args->offset);
- goto end;
- }
- /* Below code is executed when data is dumped to the
- * out buffer received in init packet
- */
- dump_out_buf = (struct cam_reg_dump_out_buffer *)
- (cmd_buf_start +
- (uintptr_t)reg_dump_desc->dump_buffer_offset);
- dump_out_buf->req_id = req_id;
- dump_out_buf->bytes_written = 0;
- for (j = 0; j < reg_dump_desc->num_read_range; j++) {
- CAM_DBG(CAM_UTIL,
- "Number of bytes written to cmd buffer: %u req_id: %llu",
- dump_out_buf->bytes_written, req_id);
- reg_read_info = ®_dump_desc->read_range[j];
- if (reg_read_info->type ==
- CAM_REG_DUMP_READ_TYPE_CONT_RANGE) {
- rc = cam_soc_util_dump_cont_reg_range(soc_info,
- ®_read_info->reg_read, reg_base_idx,
- dump_out_buf, cmd_buf_end);
- } else if (reg_read_info->type ==
- CAM_REG_DUMP_READ_TYPE_DMI) {
- rc = cam_soc_util_dump_dmi_reg_range(soc_info,
- ®_read_info->dmi_read, reg_base_idx,
- dump_out_buf, cmd_buf_end);
- } else {
- CAM_ERR(CAM_UTIL,
- "Invalid Reg dump read type: %d",
- reg_read_info->type);
- rc = -EINVAL;
- goto end;
- }
- if (rc) {
- CAM_ERR(CAM_UTIL,
- "Reg range read failed rc: %d reg_base_idx: %d dump_out_buf: %pK",
- rc, reg_base_idx, dump_out_buf);
- goto end;
- }
- }
- }
- end:
- return rc;
- }
- /**
- * cam_soc_util_print_clk_freq()
- *
- * @brief: This function gets the clk rates for each clk from clk
- * driver and prints in log
- *
- * @soc_info: Device soc struct to be populated
- *
- * @return: success or failure
- */
- int cam_soc_util_print_clk_freq(struct cam_hw_soc_info *soc_info)
- {
- int i;
- unsigned long clk_rate = 0;
- if (!soc_info) {
- CAM_ERR(CAM_UTIL, "Invalid soc info");
- return -EINVAL;
- }
- if ((soc_info->num_clk == 0) ||
- (soc_info->num_clk >= CAM_SOC_MAX_CLK)) {
- CAM_ERR(CAM_UTIL, "[%s] Invalid number of clock %d",
- soc_info->dev_name, soc_info->num_clk);
- return -EINVAL;
- }
- for (i = 0; i < soc_info->num_clk; i++) {
- clk_rate = clk_get_rate(soc_info->clk[i]);
- CAM_INFO(CAM_UTIL,
- "[%s] idx = %d clk name = %s clk_rate=%lld",
- soc_info->dev_name, i, soc_info->clk_name[i],
- clk_rate);
- }
- return 0;
- }
- int cam_soc_util_regulators_enabled(struct cam_hw_soc_info *soc_info)
- {
- int j = 0, rc = 0;
- int enabled_cnt = 0;
- for (j = 0; j < soc_info->num_rgltr; j++) {
- if (soc_info->rgltr[j]) {
- rc = regulator_is_enabled(soc_info->rgltr[j]);
- if (rc < 0) {
- CAM_ERR(CAM_UTIL, "%s regulator_is_enabled failed",
- soc_info->rgltr_name[j]);
- } else if (rc > 0) {
- CAM_DBG(CAM_UTIL, "%s regulator enabled",
- soc_info->rgltr_name[j]);
- enabled_cnt++;
- } else {
- CAM_DBG(CAM_UTIL, "%s regulator is disabled",
- soc_info->rgltr_name[j]);
- }
- }
- }
- return enabled_cnt;
- }
|