swr-mstr-ctrl.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/irq.h>
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/io.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/kthread.h>
  15. #include <linux/bitops.h>
  16. #include <linux/clk.h>
  17. #include <linux/gpio.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/of.h>
  21. #include <soc/soundwire.h>
  22. #include <soc/swr-common.h>
  23. #include <linux/regmap.h>
  24. #include <dsp/msm-audio-event-notify.h>
  25. #include "swr-mstr-registers.h"
  26. #include "swr-slave-registers.h"
  27. #include <dsp/digital-cdc-rsc-mgr.h>
  28. #include "swr-mstr-ctrl.h"
  29. #define SWR_NUM_PORTS 4 /* TODO - Get this info from DT */
  30. #define SWRM_FRAME_SYNC_SEL 4000 /* 4KHz */
  31. #define SWRM_FRAME_SYNC_SEL_NATIVE 3675 /* 3.675KHz */
  32. #define SWRM_PCM_OUT 0
  33. #define SWRM_PCM_IN 1
  34. #define SWRM_SYSTEM_RESUME_TIMEOUT_MS 700
  35. #define SWRM_SYS_SUSPEND_WAIT 1
  36. #define SWRM_DSD_PARAMS_PORT 4
  37. #define SWRM_SPK_DAC_PORT_RECEIVER 0
  38. #define SWR_BROADCAST_CMD_ID 0x0F
  39. #define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
  40. #define SWR_REG_VAL_PACK(data, dev, id, reg) \
  41. ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
  42. #define SWR_INVALID_PARAM 0xFF
  43. #define SWR_HSTOP_MAX_VAL 0xF
  44. #define SWR_HSTART_MIN_VAL 0x0
  45. #define ERR_AUTO_SUSPEND_TIMER_VAL 0x1
  46. #define SWRM_LINK_STATUS_RETRY_CNT 100
  47. #define SWRM_ROW_48 48
  48. #define SWRM_ROW_50 50
  49. #define SWRM_ROW_64 64
  50. #define SWRM_COL_02 02
  51. #define SWRM_COL_16 16
  52. #define SWRS_SCP_INT_STATUS_CLEAR_1 0x40
  53. #define SWRS_SCP_INT_STATUS_MASK_1 0x41
  54. #define SWRM_MCP_SLV_STATUS_MASK 0x03
  55. #define SWRM_ROW_CTRL_MASK 0xF8
  56. #define SWRM_COL_CTRL_MASK 0x07
  57. #define SWRM_CLK_DIV_MASK 0x700
  58. #define SWRM_SSP_PERIOD_MASK 0xff0000
  59. #define SWRM_NUM_PINGS_MASK 0x3E0000
  60. #define SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT 3
  61. #define SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT 0
  62. #define SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT 8
  63. #define SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT 16
  64. #define SWRM_NUM_PINGS_POS 0x11
  65. #define SWRM_DP_PORT_CTRL_EN_CHAN_SHFT 0x18
  66. #define SWRM_DP_PORT_CTRL_OFFSET2_SHFT 0x10
  67. #define SWRM_DP_PORT_CTRL_OFFSET1_SHFT 0x08
  68. #define SWR_OVERFLOW_RETRY_COUNT 30
  69. #define CPU_IDLE_LATENCY 10
  70. #define SWRM_REG_GAP_START 0x2C54
  71. #define SWRM_REG_GAP_END 0x4000
  72. #define SAMPLING_RATE_44P1KHZ 44100
  73. #define SAMPLING_RATE_88P2KHZ 88200
  74. #define SAMPLING_RATE_176P4KHZ 176400
  75. #define SAMPLING_RATE_352P8KHZ 352800
  76. #define SAMPLING_RATE_48KHZ 48000
  77. #define SAMPLING_RATE_96KHZ 96000
  78. #define SAMPLING_RATE_192KHZ 192000
  79. #define SAMPLING_RATE_384KHZ 384000
  80. /* pm runtime auto suspend timer in msecs */
  81. static int auto_suspend_timer = 500;
  82. module_param(auto_suspend_timer, int, 0664);
  83. MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
  84. enum {
  85. SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
  86. SWR_ATTACHED_OK, /* Device is attached */
  87. SWR_ALERT, /* Device alters master for any interrupts */
  88. SWR_RESERVED, /* Reserved */
  89. };
  90. enum {
  91. MASTER_ID_WSA = 1,
  92. MASTER_ID_RX,
  93. MASTER_ID_TX,
  94. MASTER_ID_WSA2,
  95. MASTER_ID_BT = 5
  96. };
  97. enum {
  98. ENABLE_PENDING,
  99. DISABLE_PENDING
  100. };
  101. enum {
  102. LPASS_HW_CORE,
  103. LPASS_AUDIO_CORE,
  104. };
  105. enum {
  106. SWRM_WR_CHECK_AVAIL,
  107. SWRM_RD_CHECK_AVAIL,
  108. };
  109. #define TRUE 1
  110. #define FALSE 0
  111. #define SWRM_MAX_PORT_REG 120
  112. #define SWRM_MAX_INIT_REG 12
  113. #define MAX_FIFO_RD_FAIL_RETRY 3
  114. static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm);
  115. static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm);
  116. static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr);
  117. static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val);
  118. static int swrm_runtime_resume(struct device *dev);
  119. static void swrm_wait_for_fifo_avail(struct swr_mstr_ctrl *swrm, int swrm_rd_wr);
  120. static u8 swrm_get_clk_div(int mclk_freq, int bus_clk_freq)
  121. {
  122. int clk_div = 0;
  123. u8 div_val = 0;
  124. if (!mclk_freq || !bus_clk_freq)
  125. return 0;
  126. clk_div = (mclk_freq / bus_clk_freq);
  127. switch (clk_div) {
  128. case 32:
  129. div_val = 5;
  130. break;
  131. case 16:
  132. div_val = 4;
  133. break;
  134. case 8:
  135. div_val = 3;
  136. break;
  137. case 4:
  138. div_val = 2;
  139. break;
  140. case 2:
  141. div_val = 1;
  142. break;
  143. case 1:
  144. default:
  145. div_val = 0;
  146. break;
  147. }
  148. return div_val;
  149. }
  150. static bool swrm_is_msm_variant(int val)
  151. {
  152. return (val == SWRM_VERSION_1_3);
  153. }
  154. static u8 get_cmd_id(struct swr_mstr_ctrl *swrm)
  155. {
  156. u8 id;
  157. id = swrm->cmd_id;
  158. swrm->cmd_id = (swrm->cmd_id == 0xE) ? 0 : ((swrm->cmd_id + 1) % 16);
  159. return id;
  160. }
  161. #ifdef CONFIG_DEBUG_FS
  162. static int swrm_debug_open(struct inode *inode, struct file *file)
  163. {
  164. file->private_data = inode->i_private;
  165. return 0;
  166. }
  167. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  168. {
  169. char *token;
  170. int base, cnt;
  171. token = strsep(&buf, " ");
  172. for (cnt = 0; cnt < num_of_par; cnt++) {
  173. if (token) {
  174. if ((token[1] == 'x') || (token[1] == 'X'))
  175. base = 16;
  176. else
  177. base = 10;
  178. if (kstrtou32(token, base, &param1[cnt]) != 0)
  179. return -EINVAL;
  180. token = strsep(&buf, " ");
  181. } else
  182. return -EINVAL;
  183. }
  184. return 0;
  185. }
  186. static ssize_t swrm_reg_show(struct swr_mstr_ctrl *swrm, char __user *ubuf,
  187. size_t count, loff_t *ppos)
  188. {
  189. int i, reg_val, len;
  190. ssize_t total = 0;
  191. char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
  192. if (!ubuf || !ppos)
  193. return 0;
  194. i = ((int) *ppos + SWRM_BASE);
  195. for (; i <= SWRM_MAX_REGISTER; i += 4) {
  196. /* No registers between SWRM_REG_GAP_START to SWRM_REG_GAP_END */
  197. if (i > SWRM_REG_GAP_START && i < SWRM_REG_GAP_END)
  198. continue;
  199. usleep_range(100, 150);
  200. reg_val = swr_master_read(swrm, i);
  201. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
  202. if (len < 0) {
  203. pr_err_ratelimited("%s: fail to fill the buffer\n", __func__);
  204. total = -EFAULT;
  205. goto copy_err;
  206. }
  207. if ((total + len) >= count - 1)
  208. break;
  209. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  210. pr_err_ratelimited("%s: fail to copy reg dump\n", __func__);
  211. total = -EFAULT;
  212. goto copy_err;
  213. }
  214. *ppos += 4;
  215. total += len;
  216. }
  217. copy_err:
  218. return total;
  219. }
  220. static ssize_t swrm_debug_reg_dump(struct file *file, char __user *ubuf,
  221. size_t count, loff_t *ppos)
  222. {
  223. struct swr_mstr_ctrl *swrm;
  224. if (!count || !file || !ppos || !ubuf)
  225. return -EINVAL;
  226. swrm = file->private_data;
  227. if (!swrm)
  228. return -EINVAL;
  229. if (*ppos < 0)
  230. return -EINVAL;
  231. return swrm_reg_show(swrm, ubuf, count, ppos);
  232. }
  233. static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
  234. size_t count, loff_t *ppos)
  235. {
  236. char lbuf[SWR_MSTR_RD_BUF_LEN];
  237. struct swr_mstr_ctrl *swrm = NULL;
  238. if (!count || !file || !ppos || !ubuf)
  239. return -EINVAL;
  240. swrm = file->private_data;
  241. if (!swrm)
  242. return -EINVAL;
  243. if (*ppos < 0)
  244. return -EINVAL;
  245. snprintf(lbuf, sizeof(lbuf), "0x%x\n", swrm->read_data);
  246. return simple_read_from_buffer(ubuf, count, ppos, lbuf,
  247. strnlen(lbuf, 7));
  248. }
  249. static ssize_t swrm_debug_peek_write(struct file *file, const char __user *ubuf,
  250. size_t count, loff_t *ppos)
  251. {
  252. char lbuf[SWR_MSTR_RD_BUF_LEN];
  253. int rc;
  254. u32 param[5];
  255. struct swr_mstr_ctrl *swrm = NULL;
  256. if (!count || !file || !ppos || !ubuf)
  257. return -EINVAL;
  258. swrm = file->private_data;
  259. if (!swrm)
  260. return -EINVAL;
  261. if (*ppos < 0)
  262. return -EINVAL;
  263. if (count > sizeof(lbuf) - 1)
  264. return -EINVAL;
  265. rc = copy_from_user(lbuf, ubuf, count);
  266. if (rc)
  267. return -EFAULT;
  268. lbuf[count] = '\0';
  269. rc = get_parameters(lbuf, param, 1);
  270. if ((param[0] <= SWRM_MAX_REGISTER) && (rc == 0) && (param[0] % 4 == 0))
  271. swrm->read_data = swr_master_read(swrm, param[0]);
  272. else
  273. rc = -EINVAL;
  274. if (rc == 0)
  275. rc = count;
  276. else
  277. dev_err_ratelimited(swrm->dev, "%s: rc = %d\n", __func__, rc);
  278. return rc;
  279. }
  280. static ssize_t swrm_debug_write(struct file *file,
  281. const char __user *ubuf, size_t count, loff_t *ppos)
  282. {
  283. char lbuf[SWR_MSTR_WR_BUF_LEN];
  284. int rc;
  285. u32 param[5];
  286. struct swr_mstr_ctrl *swrm;
  287. if (!file || !ppos || !ubuf)
  288. return -EINVAL;
  289. swrm = file->private_data;
  290. if (!swrm)
  291. return -EINVAL;
  292. if (count > sizeof(lbuf) - 1)
  293. return -EINVAL;
  294. rc = copy_from_user(lbuf, ubuf, count);
  295. if (rc)
  296. return -EFAULT;
  297. lbuf[count] = '\0';
  298. rc = get_parameters(lbuf, param, 2);
  299. if ((param[0] <= SWRM_MAX_REGISTER) &&
  300. (param[1] <= 0xFFFFFFFF) &&
  301. (rc == 0) && (param[0] % 4 == 0))
  302. swr_master_write(swrm, param[0], param[1]);
  303. else
  304. rc = -EINVAL;
  305. if (rc == 0)
  306. rc = count;
  307. else
  308. pr_err_ratelimited("%s: rc = %d\n", __func__, rc);
  309. return rc;
  310. }
  311. static const struct file_operations swrm_debug_read_ops = {
  312. .open = swrm_debug_open,
  313. .write = swrm_debug_peek_write,
  314. .read = swrm_debug_read,
  315. };
  316. static const struct file_operations swrm_debug_write_ops = {
  317. .open = swrm_debug_open,
  318. .write = swrm_debug_write,
  319. };
  320. static const struct file_operations swrm_debug_dump_ops = {
  321. .open = swrm_debug_open,
  322. .read = swrm_debug_reg_dump,
  323. };
  324. #endif
  325. static void swrm_reg_dump(struct swr_mstr_ctrl *swrm,
  326. u32 *reg, u32 *val, int len, const char* func)
  327. {
  328. int i = 0;
  329. for (i = 0; i < len; i++)
  330. dev_dbg(swrm->dev, "%s: reg = 0x%x val = 0x%x\n",
  331. func, reg[i], val[i]);
  332. }
  333. static bool is_swr_clk_needed(struct swr_mstr_ctrl *swrm)
  334. {
  335. return ((swrm->version <= SWRM_VERSION_1_5_1) ? true : false);
  336. }
  337. static int swrm_request_hw_vote(struct swr_mstr_ctrl *swrm,
  338. int core_type, bool enable)
  339. {
  340. int ret = 0;
  341. mutex_lock(&swrm->devlock);
  342. if (core_type == LPASS_HW_CORE) {
  343. if (swrm->lpass_core_hw_vote) {
  344. if (enable) {
  345. if (!swrm->dev_up) {
  346. dev_dbg(swrm->dev, "%s: device is down or SSR state\n",
  347. __func__);
  348. mutex_unlock(&swrm->devlock);
  349. return -ENODEV;
  350. }
  351. if (++swrm->hw_core_clk_en == 1) {
  352. ret =
  353. digital_cdc_rsc_mgr_hw_vote_enable(
  354. swrm->lpass_core_hw_vote, swrm->dev);
  355. if (ret < 0) {
  356. dev_err_ratelimited(swrm->dev,
  357. "%s:lpass core hw enable failed\n",
  358. __func__);
  359. --swrm->hw_core_clk_en;
  360. }
  361. }
  362. } else {
  363. --swrm->hw_core_clk_en;
  364. if (swrm->hw_core_clk_en < 0)
  365. swrm->hw_core_clk_en = 0;
  366. else if (swrm->hw_core_clk_en == 0)
  367. digital_cdc_rsc_mgr_hw_vote_disable(
  368. swrm->lpass_core_hw_vote, swrm->dev);
  369. }
  370. }
  371. }
  372. if (core_type == LPASS_AUDIO_CORE) {
  373. if (swrm->lpass_core_audio) {
  374. if (enable) {
  375. if (!swrm->dev_up) {
  376. dev_dbg(swrm->dev, "%s: device is down or SSR state\n",
  377. __func__);
  378. mutex_unlock(&swrm->devlock);
  379. return -ENODEV;
  380. }
  381. if (++swrm->aud_core_clk_en == 1) {
  382. ret =
  383. digital_cdc_rsc_mgr_hw_vote_enable(
  384. swrm->lpass_core_audio, swrm->dev);
  385. if (ret < 0) {
  386. dev_err_ratelimited(swrm->dev,
  387. "%s:lpass audio hw enable failed\n",
  388. __func__);
  389. --swrm->aud_core_clk_en;
  390. }
  391. }
  392. } else {
  393. --swrm->aud_core_clk_en;
  394. if (swrm->aud_core_clk_en < 0)
  395. swrm->aud_core_clk_en = 0;
  396. else if (swrm->aud_core_clk_en == 0)
  397. digital_cdc_rsc_mgr_hw_vote_disable(
  398. swrm->lpass_core_audio, swrm->dev);
  399. }
  400. }
  401. }
  402. mutex_unlock(&swrm->devlock);
  403. dev_dbg(swrm->dev, "%s: hw_clk_en: %d audio_core_clk_en: %d\n",
  404. __func__, swrm->hw_core_clk_en, swrm->aud_core_clk_en);
  405. return ret;
  406. }
  407. static int swrm_get_ssp_period(struct swr_mstr_ctrl *swrm,
  408. int row, int col,
  409. int frame_sync)
  410. {
  411. if (!swrm || !row || !col || !frame_sync)
  412. return 1;
  413. return ((swrm->bus_clk * 2) / ((row * col) * frame_sync));
  414. }
  415. static int swrm_core_vote_request(struct swr_mstr_ctrl *swrm, bool enable)
  416. {
  417. int ret = 0;
  418. static DEFINE_RATELIMIT_STATE(rtl, 1 * HZ, 1);
  419. if (!swrm->handle)
  420. return -EINVAL;
  421. mutex_lock(&swrm->clklock);
  422. if (!swrm->dev_up) {
  423. ret = -ENODEV;
  424. goto exit;
  425. }
  426. if (swrm->core_vote) {
  427. ret = swrm->core_vote(swrm->handle, enable);
  428. if (ret)
  429. if (__ratelimit(&rtl))
  430. dev_err_ratelimited(swrm->dev,
  431. "%s: core vote request failed\n", __func__);
  432. }
  433. exit:
  434. mutex_unlock(&swrm->clklock);
  435. return ret;
  436. }
  437. static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
  438. {
  439. int ret = 0;
  440. if (!swrm->clk || !swrm->handle)
  441. return -EINVAL;
  442. mutex_lock(&swrm->clklock);
  443. if (enable) {
  444. if (!swrm->dev_up) {
  445. ret = -ENODEV;
  446. goto exit;
  447. }
  448. if (is_swr_clk_needed(swrm)) {
  449. if (swrm->core_vote) {
  450. ret = swrm->core_vote(swrm->handle, true);
  451. if (ret) {
  452. dev_err_ratelimited(swrm->dev,
  453. "%s: core vote request failed\n",
  454. __func__);
  455. swrm->core_vote(swrm->handle, false);
  456. goto exit;
  457. }
  458. ret = swrm->core_vote(swrm->handle, false);
  459. }
  460. }
  461. swrm->clk_ref_count++;
  462. if (swrm->clk_ref_count == 1) {
  463. ret = swrm->clk(swrm->handle, true);
  464. if (ret) {
  465. dev_err_ratelimited(swrm->dev,
  466. "%s: clock enable req failed",
  467. __func__);
  468. --swrm->clk_ref_count;
  469. }
  470. }
  471. } else if (--swrm->clk_ref_count == 0) {
  472. swrm->clk(swrm->handle, false);
  473. complete(&swrm->clk_off_complete);
  474. }
  475. if (swrm->clk_ref_count < 0) {
  476. dev_err_ratelimited(swrm->dev, "%s: swrm clk count mismatch\n", __func__);
  477. swrm->clk_ref_count = 0;
  478. }
  479. exit:
  480. mutex_unlock(&swrm->clklock);
  481. return ret;
  482. }
  483. static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
  484. u16 reg, u32 *value)
  485. {
  486. u32 temp = (u32)(*value);
  487. int ret = 0;
  488. int vote_ret = 0;
  489. mutex_lock(&swrm->devlock);
  490. if (!swrm->dev_up)
  491. goto err;
  492. if (is_swr_clk_needed(swrm)) {
  493. ret = swrm_clk_request(swrm, TRUE);
  494. if (ret) {
  495. dev_err_ratelimited(swrm->dev,
  496. "%s: clock request failed\n",
  497. __func__);
  498. goto err;
  499. }
  500. } else {
  501. vote_ret = swrm_core_vote_request(swrm, true);
  502. if (vote_ret == -ENOTSYNC)
  503. goto err_vote;
  504. else if (vote_ret)
  505. goto err;
  506. }
  507. iowrite32(temp, swrm->swrm_dig_base + reg);
  508. if (is_swr_clk_needed(swrm))
  509. swrm_clk_request(swrm, FALSE);
  510. err_vote:
  511. if (!is_swr_clk_needed(swrm))
  512. swrm_core_vote_request(swrm, false);
  513. err:
  514. mutex_unlock(&swrm->devlock);
  515. return ret;
  516. }
  517. static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
  518. u16 reg, u32 *value)
  519. {
  520. u32 temp = 0;
  521. int ret = 0;
  522. int vote_ret = 0;
  523. mutex_lock(&swrm->devlock);
  524. if (!swrm->dev_up)
  525. goto err;
  526. if (is_swr_clk_needed(swrm)) {
  527. ret = swrm_clk_request(swrm, TRUE);
  528. if (ret) {
  529. dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
  530. __func__);
  531. goto err;
  532. }
  533. } else {
  534. vote_ret = swrm_core_vote_request(swrm, true);
  535. if (vote_ret == -ENOTSYNC)
  536. goto err_vote;
  537. else if (vote_ret)
  538. goto err;
  539. }
  540. temp = ioread32(swrm->swrm_dig_base + reg);
  541. *value = temp;
  542. if (is_swr_clk_needed(swrm))
  543. swrm_clk_request(swrm, FALSE);
  544. err_vote:
  545. if (!is_swr_clk_needed(swrm))
  546. swrm_core_vote_request(swrm, false);
  547. err:
  548. mutex_unlock(&swrm->devlock);
  549. return ret;
  550. }
  551. static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
  552. {
  553. u32 val = 0;
  554. if (swrm->read)
  555. val = swrm->read(swrm->handle, reg_addr);
  556. else
  557. swrm_ahb_read(swrm, reg_addr, &val);
  558. return val;
  559. }
  560. static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
  561. {
  562. if (swrm->write)
  563. swrm->write(swrm->handle, reg_addr, val);
  564. else
  565. swrm_ahb_write(swrm, reg_addr, &val);
  566. }
  567. static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
  568. u32 *val, unsigned int length)
  569. {
  570. int i = 0;
  571. if (swrm->bulk_write)
  572. swrm->bulk_write(swrm->handle, reg_addr, val, length);
  573. else {
  574. mutex_lock(&swrm->iolock);
  575. for (i = 0; i < length; i++) {
  576. /* wait for FIFO WR command to complete to avoid overflow */
  577. /*
  578. * Reduce sleep from 100us to 50us to meet KPIs
  579. * This still meets the hardware spec
  580. */
  581. usleep_range(50, 55);
  582. if (reg_addr[i] == SWRM_CMD_FIFO_WR_CMD(swrm->ee_val))
  583. swrm_wait_for_fifo_avail(swrm,
  584. SWRM_WR_CHECK_AVAIL);
  585. swr_master_write(swrm, reg_addr[i], val[i]);
  586. }
  587. usleep_range(100, 110);
  588. mutex_unlock(&swrm->iolock);
  589. }
  590. return 0;
  591. }
  592. static bool swrm_check_link_status(struct swr_mstr_ctrl *swrm, bool active)
  593. {
  594. int retry = SWRM_LINK_STATUS_RETRY_CNT;
  595. int ret = false;
  596. int status = active ? 0x1 : 0x0;
  597. int comp_sts = 0x0;
  598. if ((swrm->version <= SWRM_VERSION_1_5_1))
  599. return true;
  600. do {
  601. #ifdef CONFIG_SWRM_VER_2P0
  602. comp_sts = swr_master_read(swrm, SWRM_LINK_STATUS(swrm->ee_val)) & 0x01;
  603. #else
  604. comp_sts = swr_master_read(swrm, SWRM_COMP_STATUS) & 0x01;
  605. #endif
  606. /* check comp status and status requested met */
  607. if ((comp_sts && status) || (!comp_sts && !status)) {
  608. ret = true;
  609. break;
  610. }
  611. retry--;
  612. usleep_range(500, 510);
  613. } while (retry);
  614. if (retry == 0)
  615. dev_err_ratelimited(swrm->dev, "%s: link status not %s\n", __func__,
  616. active ? "connected" : "disconnected");
  617. return ret;
  618. }
  619. static bool swrm_is_port_en(struct swr_master *mstr)
  620. {
  621. return !!(mstr->num_port);
  622. }
  623. static void copy_port_tables(struct swr_mstr_ctrl *swrm,
  624. struct port_params *params)
  625. {
  626. u8 i;
  627. struct port_params *config = params;
  628. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  629. /* wsa uses single frame structure for all configurations */
  630. if (!swrm->mport_cfg[i].port_en)
  631. continue;
  632. swrm->mport_cfg[i].sinterval = config[i].si;
  633. swrm->mport_cfg[i].offset1 = config[i].off1;
  634. swrm->mport_cfg[i].offset2 = config[i].off2;
  635. swrm->mport_cfg[i].hstart = config[i].hstart;
  636. swrm->mport_cfg[i].hstop = config[i].hstop;
  637. swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
  638. swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
  639. swrm->mport_cfg[i].word_length = config[i].wd_len;
  640. swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
  641. swrm->mport_cfg[i].dir = config[i].dir;
  642. swrm->mport_cfg[i].stream_type = config[i].stream_type;
  643. }
  644. }
  645. static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
  646. {
  647. struct port_params *params;
  648. u32 usecase = 0;
  649. if (swrm->master_id == MASTER_ID_TX || swrm->master_id == MASTER_ID_BT)
  650. return 0;
  651. /* TODO - Send usecase information to avoid checking for master_id */
  652. if (swrm->mport_cfg[SWRM_DSD_PARAMS_PORT].port_en &&
  653. (swrm->master_id == MASTER_ID_RX))
  654. usecase = 1;
  655. else if ((swrm->master_id == MASTER_ID_RX) &&
  656. (swrm->bus_clk == SWR_CLK_RATE_11P2896MHZ))
  657. usecase = 2;
  658. if ((swrm->master_id == MASTER_ID_WSA) &&
  659. swrm->mport_cfg[SWRM_SPK_DAC_PORT_RECEIVER].port_en &&
  660. swrm->mport_cfg[SWRM_SPK_DAC_PORT_RECEIVER].ch_rate ==
  661. SWR_CLK_RATE_4P8MHZ)
  662. usecase = 1;
  663. params = swrm->port_param[usecase];
  664. copy_port_tables(swrm, params);
  665. return 0;
  666. }
  667. static bool swrm_is_fractional_sample_rate(u32 sample_rate)
  668. {
  669. switch (sample_rate) {
  670. case SAMPLING_RATE_44P1KHZ:
  671. case SAMPLING_RATE_88P2KHZ:
  672. case SAMPLING_RATE_176P4KHZ:
  673. case SAMPLING_RATE_352P8KHZ:
  674. return true;
  675. default:
  676. return false;
  677. }
  678. }
  679. static bool swrm_is_flow_ctrl_needed(struct swrm_mports *mport, u32 bus_clk)
  680. {
  681. struct swr_port_info *port_req = NULL;
  682. list_for_each_entry(port_req, &mport->port_req_list, list) {
  683. if (swrm_is_fractional_sample_rate(port_req->req_ch_rate) &&
  684. (bus_clk % port_req->req_ch_rate)) {
  685. pr_debug("%s: flow control needed on Master port ID %d\n",
  686. __func__, port_req->master_port_id);
  687. return true;
  688. }
  689. }
  690. return false;
  691. }
  692. static int swrm_pcm_port_config(struct swr_mstr_ctrl *swrm, u8 port_num,
  693. struct swrm_mports *mport, bool enable)
  694. {
  695. u16 reg_addr = 0;
  696. u32 reg_val = 0;
  697. u8 stream_type = mport->stream_type;
  698. bool dir = mport->dir;
  699. u32 flow_mode = (dir) ? SWRM_DP_PORT_CONTROL__FLOW_MODE_PULL :
  700. SWRM_DP_PORT_CONTROL__FLOW_MODE_PUSH;
  701. if (!port_num || port_num > SWR_MSTR_PORT_LEN) {
  702. dev_err_ratelimited(swrm->dev, "%s: invalid port: %d\n",
  703. __func__, port_num);
  704. return -EINVAL;
  705. }
  706. switch (stream_type) {
  707. case SWR_PCM:
  708. case SWR_PDM_32:
  709. if (swrm->version != SWRM_VERSION_1_7) {
  710. if (dir)
  711. reg_addr = SWRM_DIN_DP_PCM_PORT_CTRL(port_num);
  712. else
  713. reg_addr = SWRM_DOUT_DP_PCM_PORT_CTRL(port_num);
  714. reg_val = enable ? 0x3 : 0x0;
  715. swr_master_write(swrm, reg_addr, reg_val);
  716. } else if (stream_type == SWR_PCM) {
  717. if (dir)
  718. reg_addr = SWRM_DIN_DP_PCM_PORT_CTRL(port_num);
  719. else
  720. reg_addr = SWRM_DOUT_DP_PCM_PORT_CTRL(port_num);
  721. swr_master_write(swrm, reg_addr, enable);
  722. }
  723. break;
  724. case SWR_PDM:
  725. default:
  726. return 0;
  727. }
  728. if (swrm->version == SWRM_VERSION_1_7) {
  729. reg_val = SWRM_COMP_FEATURE_CFG_DEFAULT_VAL_V1P7;
  730. if (enable) {
  731. if (swrm->pcm_enable_count == 0) {
  732. reg_val |= SWRM_COMP_FEATURE_CFG_PCM_EN_MASK;
  733. swr_master_write(swrm, SWRM_COMP_FEATURE_CFG, reg_val);
  734. }
  735. swrm->pcm_enable_count++;
  736. } else {
  737. if (swrm->pcm_enable_count > 0)
  738. swrm->pcm_enable_count--;
  739. if (swrm->pcm_enable_count == 0)
  740. swr_master_write(swrm, SWRM_COMP_FEATURE_CFG, reg_val);
  741. }
  742. }
  743. dev_dbg(swrm->dev, "%s : pcm port %s, reg_val = %d, for addr %x\n",
  744. __func__, enable ? "Enabled" : "disabled", reg_val, reg_addr);
  745. if (swrm_is_flow_ctrl_needed(mport, swrm->bus_clk) && enable) {
  746. /*Flow control pull/push mode. */
  747. reg_addr = SWRM_DP_PORT_CONTROL(port_num);
  748. reg_val = swr_master_read(swrm, reg_addr);
  749. reg_val |= flow_mode;
  750. swr_master_write(swrm, reg_addr, reg_val);
  751. /*SELF GEN SUBRATE ENABLE*/
  752. reg_addr = ((dir) ? SWRM_DIN_DP_PCM_PORT_CTRL(port_num) :
  753. SWRM_DOUT_DP_PCM_PORT_CTRL(port_num));
  754. reg_val = swr_master_read(swrm, reg_addr);
  755. reg_val |= SWRM_DOUT_DP_PCM_PORT_CTRL__SELF_GEN_SUB_RATE_EN;
  756. swr_master_write(swrm, reg_addr, reg_val);
  757. /*M VALID SAMPLE*/
  758. reg_addr = SWRM_DP_FLOW_CTRL_M_VALID_SAMPLE(port_num);
  759. swr_master_write(swrm, reg_addr, 147);
  760. /*N REPEAT PERIOD*/
  761. reg_addr = SWRM_DP_FLOW_CTRL_N_REPEAT_PERIOD(port_num);
  762. swr_master_write(swrm, reg_addr, 160);
  763. }
  764. if (!enable) {
  765. /* Reset flow control configuration registers to defaults. */
  766. swr_master_write(swrm, SWRM_DP_PORT_CONTROL(port_num), 0x0);
  767. swr_master_write(swrm, SWRM_DP_FLOW_CTRL_M_VALID_SAMPLE(port_num), 0x1);
  768. swr_master_write(swrm, SWRM_DP_FLOW_CTRL_N_REPEAT_PERIOD(port_num), 0x1);
  769. }
  770. return 0;
  771. }
  772. static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
  773. u8 *mstr_ch_mask, u8 mstr_prt_type,
  774. u8 slv_port_id)
  775. {
  776. int i, j;
  777. *mstr_port_id = 0;
  778. for (i = 1; i <= swrm->num_ports; i++) {
  779. for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
  780. if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
  781. goto found;
  782. }
  783. }
  784. found:
  785. if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
  786. dev_err_ratelimited(swrm->dev, "%s: port type not supported by master\n",
  787. __func__);
  788. return -EINVAL;
  789. }
  790. /* id 0 corresponds to master port 1 */
  791. *mstr_port_id = i - 1;
  792. *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
  793. return 0;
  794. }
  795. static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
  796. u8 dev_addr, u16 reg_addr)
  797. {
  798. u32 val;
  799. u8 id = *cmd_id;
  800. if (id != SWR_BROADCAST_CMD_ID) {
  801. if (id < 14)
  802. id += 1;
  803. else
  804. id = 0;
  805. *cmd_id = id;
  806. }
  807. val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
  808. return val;
  809. }
  810. static void swrm_wait_for_fifo_avail(struct swr_mstr_ctrl *swrm, int swrm_rd_wr)
  811. {
  812. u32 fifo_outstanding_cmd;
  813. u32 fifo_retry_count = SWR_OVERFLOW_RETRY_COUNT;
  814. if (swrm_rd_wr) {
  815. /* Check for fifo underflow during read */
  816. /* Check no of outstanding commands in fifo before read */
  817. fifo_outstanding_cmd = ((swr_master_read(swrm,
  818. SWRM_CMD_FIFO_STATUS(swrm->ee_val)) & 0x001F0000) >> 16);
  819. if (fifo_outstanding_cmd == 0) {
  820. while (fifo_retry_count) {
  821. usleep_range(500, 510);
  822. fifo_outstanding_cmd =
  823. ((swr_master_read (swrm,
  824. SWRM_CMD_FIFO_STATUS(swrm->ee_val)) & 0x001F0000)
  825. >> 16);
  826. fifo_retry_count--;
  827. if (fifo_outstanding_cmd > 0)
  828. break;
  829. }
  830. }
  831. if (fifo_outstanding_cmd == 0)
  832. dev_err_ratelimited(swrm->dev,
  833. "%s err read underflow\n", __func__);
  834. } else {
  835. /* Check for fifo overflow during write */
  836. /* Check no of outstanding commands in fifo before write */
  837. fifo_outstanding_cmd = ((swr_master_read(swrm,
  838. SWRM_CMD_FIFO_STATUS(swrm->ee_val)) & 0x00001F00)
  839. >> 8);
  840. if (fifo_outstanding_cmd == swrm->wr_fifo_depth) {
  841. while (fifo_retry_count) {
  842. usleep_range(500, 510);
  843. fifo_outstanding_cmd =
  844. ((swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val))
  845. & 0x00001F00) >> 8);
  846. fifo_retry_count--;
  847. if (fifo_outstanding_cmd < swrm->wr_fifo_depth)
  848. break;
  849. }
  850. }
  851. if (fifo_outstanding_cmd == swrm->wr_fifo_depth)
  852. dev_err_ratelimited(swrm->dev,
  853. "%s err write overflow\n", __func__);
  854. }
  855. }
  856. static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
  857. u8 dev_addr, u8 cmd_id, u16 reg_addr,
  858. u32 len)
  859. {
  860. u32 val;
  861. u32 retry_attempt = 0;
  862. mutex_lock(&swrm->iolock);
  863. val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
  864. if (swrm->read) {
  865. /* skip delay if read is handled in platform driver */
  866. swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD(swrm->ee_val), val);
  867. } else {
  868. /*
  869. * Check for outstanding cmd wrt. write fifo depth to avoid
  870. * overflow as read will also increase write fifo cnt.
  871. */
  872. swrm_wait_for_fifo_avail(swrm, SWRM_WR_CHECK_AVAIL);
  873. /* wait for FIFO RD to complete to avoid overflow */
  874. usleep_range(100, 105);
  875. swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD(swrm->ee_val), val);
  876. /* wait for FIFO RD CMD complete to avoid overflow */
  877. usleep_range(250, 255);
  878. }
  879. /* Check if slave responds properly after FIFO RD is complete */
  880. swrm_wait_for_fifo_avail(swrm, SWRM_RD_CHECK_AVAIL);
  881. retry_read:
  882. *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO(swrm->ee_val));
  883. dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
  884. dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
  885. cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
  886. if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
  887. if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
  888. /* wait 500 us before retry on fifo read failure */
  889. usleep_range(500, 505);
  890. if (retry_attempt == (MAX_FIFO_RD_FAIL_RETRY - 1)) {
  891. swr_master_write(swrm,
  892. SWRM_CMD_FIFO_RD_CMD(swrm->ee_val),
  893. val);
  894. }
  895. retry_attempt++;
  896. goto retry_read;
  897. } else {
  898. dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
  899. rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
  900. __func__, reg_addr, cmd_id, swrm->rcmd_id,
  901. dev_addr, *cmd_data);
  902. dev_err_ratelimited(swrm->dev,
  903. "%s: failed to read fifo\n", __func__);
  904. }
  905. }
  906. mutex_unlock(&swrm->iolock);
  907. return 0;
  908. }
  909. static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
  910. u8 dev_addr, u8 cmd_id, u16 reg_addr)
  911. {
  912. u32 val;
  913. int ret = 0;
  914. mutex_lock(&swrm->iolock);
  915. if (!cmd_id)
  916. val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
  917. dev_addr, reg_addr);
  918. else
  919. val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
  920. dev_addr, reg_addr);
  921. dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
  922. dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
  923. reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
  924. /*
  925. * Check for outstanding cmd wrt. write fifo depth to avoid
  926. * overflow.
  927. */
  928. swrm_wait_for_fifo_avail(swrm, SWRM_WR_CHECK_AVAIL);
  929. swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD(swrm->ee_val), val);
  930. /*
  931. * wait for FIFO WR command to complete to avoid overflow
  932. * skip delay if write is handled in platform driver.
  933. */
  934. if(!swrm->write)
  935. usleep_range(150, 155);
  936. if (cmd_id == 0xF) {
  937. /*
  938. * sleep for 10ms for MSM soundwire variant to allow broadcast
  939. * command to complete.
  940. */
  941. if (swrm_is_msm_variant(swrm->version))
  942. usleep_range(10000, 10100);
  943. else
  944. wait_for_completion_timeout(&swrm->broadcast,
  945. (2 * HZ/10));
  946. }
  947. mutex_unlock(&swrm->iolock);
  948. return ret;
  949. }
  950. static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
  951. void *buf, u32 len)
  952. {
  953. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  954. int ret = 0;
  955. int val;
  956. u8 *reg_val = (u8 *)buf;
  957. if (!swrm) {
  958. dev_err_ratelimited(&master->dev, "%s: swrm is NULL\n", __func__);
  959. return -EINVAL;
  960. }
  961. if (!dev_num) {
  962. dev_err_ratelimited(&master->dev, "%s: invalid slave dev num\n", __func__);
  963. return -EINVAL;
  964. }
  965. mutex_lock(&swrm->devlock);
  966. if (!swrm->dev_up) {
  967. mutex_unlock(&swrm->devlock);
  968. return 0;
  969. }
  970. mutex_unlock(&swrm->devlock);
  971. pm_runtime_get_sync(swrm->dev);
  972. if (swrm->req_clk_switch)
  973. swrm_runtime_resume(swrm->dev);
  974. ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num,
  975. get_cmd_id(swrm), reg_addr, len);
  976. if (!ret)
  977. *reg_val = (u8)val;
  978. pm_runtime_put_autosuspend(swrm->dev);
  979. pm_runtime_mark_last_busy(swrm->dev);
  980. return ret;
  981. }
  982. static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
  983. const void *buf)
  984. {
  985. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  986. int ret = 0;
  987. u8 reg_val = *(u8 *)buf;
  988. if (!swrm) {
  989. dev_err_ratelimited(&master->dev, "%s: swrm is NULL\n", __func__);
  990. return -EINVAL;
  991. }
  992. if (!dev_num) {
  993. dev_err_ratelimited(&master->dev, "%s: invalid slave dev num\n", __func__);
  994. return -EINVAL;
  995. }
  996. mutex_lock(&swrm->devlock);
  997. if (!swrm->dev_up) {
  998. mutex_unlock(&swrm->devlock);
  999. return 0;
  1000. }
  1001. mutex_unlock(&swrm->devlock);
  1002. pm_runtime_get_sync(swrm->dev);
  1003. if (swrm->req_clk_switch)
  1004. swrm_runtime_resume(swrm->dev);
  1005. ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num,
  1006. get_cmd_id(swrm), reg_addr);
  1007. pm_runtime_put_autosuspend(swrm->dev);
  1008. pm_runtime_mark_last_busy(swrm->dev);
  1009. return ret;
  1010. }
  1011. static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
  1012. const void *buf, size_t len)
  1013. {
  1014. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1015. int ret = 0;
  1016. int i;
  1017. u32 *val;
  1018. u32 *swr_fifo_reg;
  1019. if (!swrm || !swrm->handle) {
  1020. dev_err_ratelimited(&master->dev, "%s: swrm is NULL\n", __func__);
  1021. return -EINVAL;
  1022. }
  1023. if (len <= 0)
  1024. return -EINVAL;
  1025. mutex_lock(&swrm->devlock);
  1026. if (!swrm->dev_up) {
  1027. mutex_unlock(&swrm->devlock);
  1028. return 0;
  1029. }
  1030. mutex_unlock(&swrm->devlock);
  1031. pm_runtime_get_sync(swrm->dev);
  1032. if (dev_num) {
  1033. swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
  1034. if (!swr_fifo_reg) {
  1035. ret = -ENOMEM;
  1036. goto err;
  1037. }
  1038. val = kcalloc(len, sizeof(u32), GFP_KERNEL);
  1039. if (!val) {
  1040. ret = -ENOMEM;
  1041. goto mem_fail;
  1042. }
  1043. for (i = 0; i < len; i++) {
  1044. val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
  1045. ((u8 *)buf)[i],
  1046. dev_num,
  1047. ((u16 *)reg)[i]);
  1048. swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1049. }
  1050. ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
  1051. if (ret) {
  1052. dev_err_ratelimited(&master->dev, "%s: bulk write failed\n",
  1053. __func__);
  1054. ret = -EINVAL;
  1055. }
  1056. } else {
  1057. dev_err_ratelimited(&master->dev,
  1058. "%s: No support of Bulk write for master regs\n",
  1059. __func__);
  1060. ret = -EINVAL;
  1061. goto err;
  1062. }
  1063. kfree(val);
  1064. mem_fail:
  1065. kfree(swr_fifo_reg);
  1066. err:
  1067. pm_runtime_put_autosuspend(swrm->dev);
  1068. pm_runtime_mark_last_busy(swrm->dev);
  1069. return ret;
  1070. }
  1071. static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
  1072. {
  1073. return (swr_master_read(swrm, SWRM_MCP_STATUS) & 0x01) ? 0 : 1;
  1074. }
  1075. static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
  1076. u8 row, u8 col)
  1077. {
  1078. swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
  1079. SWRS_SCP_FRAME_CTRL_BANK(bank));
  1080. }
  1081. static void swrm_switch_frame_shape(struct swr_mstr_ctrl *swrm, int mclk_freq)
  1082. {
  1083. u8 bank;
  1084. u32 n_row, n_col;
  1085. u32 value = 0;
  1086. u32 row = 0, col = 0;
  1087. u8 ssp_period = 0;
  1088. int frame_sync = SWRM_FRAME_SYNC_SEL;
  1089. if (mclk_freq == MCLK_FREQ_NATIVE) {
  1090. n_col = SWR_MAX_COL;
  1091. col = SWRM_COL_16;
  1092. n_row = SWR_ROW_64;
  1093. row = SWRM_ROW_64;
  1094. frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
  1095. } else if (mclk_freq == MCLK_FREQ_12288) {
  1096. n_col = SWR_MIN_COL;
  1097. col = SWRM_COL_02;
  1098. n_row = SWR_ROW_64;
  1099. row = SWRM_ROW_64;
  1100. frame_sync = SWRM_FRAME_SYNC_SEL;
  1101. } else {
  1102. n_col = SWR_MIN_COL;
  1103. col = SWRM_COL_02;
  1104. n_row = SWR_ROW_50;
  1105. row = SWRM_ROW_50;
  1106. frame_sync = SWRM_FRAME_SYNC_SEL;
  1107. }
  1108. bank = get_inactive_bank_num(swrm);
  1109. ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
  1110. dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
  1111. value = ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1112. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1113. ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1114. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK(bank), value);
  1115. enable_bank_switch(swrm, bank, n_row, n_col);
  1116. }
  1117. static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
  1118. u8 slv_port, u8 dev_num)
  1119. {
  1120. struct swr_port_info *port_req = NULL;
  1121. list_for_each_entry(port_req, &mport->port_req_list, list) {
  1122. /* Store dev_id instead of dev_num if enumeration is changed run_time */
  1123. if ((port_req->slave_port_id == slv_port)
  1124. && (port_req->dev_num == dev_num))
  1125. return port_req;
  1126. }
  1127. return NULL;
  1128. }
  1129. static bool swrm_remove_from_group(struct swr_master *master)
  1130. {
  1131. struct swr_device *swr_dev;
  1132. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1133. bool is_removed = false;
  1134. if (!swrm)
  1135. goto end;
  1136. mutex_lock(&swrm->mlock);
  1137. if (swrm->num_rx_chs > 1) {
  1138. list_for_each_entry(swr_dev, &master->devices,
  1139. dev_list) {
  1140. swr_dev->group_id = SWR_GROUP_NONE;
  1141. master->gr_sid = 0;
  1142. }
  1143. is_removed = true;
  1144. }
  1145. mutex_unlock(&swrm->mlock);
  1146. end:
  1147. return is_removed;
  1148. }
  1149. int swrm_get_clk_div_rate(int mclk_freq, int bus_clk_freq)
  1150. {
  1151. if (!bus_clk_freq)
  1152. return mclk_freq;
  1153. if (mclk_freq == SWR_CLK_RATE_9P6MHZ) {
  1154. if (bus_clk_freq <= SWR_CLK_RATE_0P6MHZ)
  1155. bus_clk_freq = SWR_CLK_RATE_0P6MHZ;
  1156. else if (bus_clk_freq <= SWR_CLK_RATE_1P2MHZ)
  1157. bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
  1158. else if (bus_clk_freq <= SWR_CLK_RATE_2P4MHZ)
  1159. bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
  1160. else if(bus_clk_freq <= SWR_CLK_RATE_4P8MHZ)
  1161. bus_clk_freq = SWR_CLK_RATE_4P8MHZ;
  1162. else if(bus_clk_freq <= SWR_CLK_RATE_9P6MHZ)
  1163. bus_clk_freq = SWR_CLK_RATE_9P6MHZ;
  1164. else
  1165. bus_clk_freq = SWR_CLK_RATE_9P6MHZ;
  1166. } else if (mclk_freq == SWR_CLK_RATE_11P2896MHZ)
  1167. bus_clk_freq = SWR_CLK_RATE_11P2896MHZ;
  1168. else if (mclk_freq == SWR_CLK_RATE_12P288MHZ)
  1169. bus_clk_freq = SWR_CLK_RATE_12P288MHZ;
  1170. return bus_clk_freq;
  1171. }
  1172. static int swrm_update_bus_clk(struct swr_mstr_ctrl *swrm)
  1173. {
  1174. int ret = 0;
  1175. int agg_clk = 0;
  1176. int i;
  1177. for (i = 0; i < SWR_MSTR_PORT_LEN; i++)
  1178. agg_clk += swrm->mport_cfg[i].ch_rate;
  1179. if (agg_clk)
  1180. swrm->bus_clk = swrm_get_clk_div_rate(swrm->mclk_freq,
  1181. agg_clk);
  1182. else
  1183. swrm->bus_clk = swrm->mclk_freq;
  1184. dev_dbg(swrm->dev, "%s: all_port_clk: %d, bus_clk: %d\n",
  1185. __func__, agg_clk, swrm->bus_clk);
  1186. return ret;
  1187. }
  1188. static void swrm_disable_ports(struct swr_master *master,
  1189. u8 bank)
  1190. {
  1191. u32 value;
  1192. struct swr_port_info *port_req;
  1193. int i;
  1194. struct swrm_mports *mport;
  1195. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1196. if (!swrm) {
  1197. pr_err_ratelimited("%s: swrm is null\n", __func__);
  1198. return;
  1199. }
  1200. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  1201. master->num_port);
  1202. for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
  1203. mport = &(swrm->mport_cfg[i]);
  1204. if (!mport->port_en)
  1205. continue;
  1206. list_for_each_entry(port_req, &mport->port_req_list, list) {
  1207. /* skip ports with no change req's*/
  1208. if (port_req->req_ch == port_req->ch_en)
  1209. continue;
  1210. swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
  1211. port_req->dev_num, get_cmd_id(swrm),
  1212. SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
  1213. bank));
  1214. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
  1215. __func__, i,
  1216. (SWRM_DP_PORT_CTRL_BANK((i + 1), bank)));
  1217. }
  1218. value = ((mport->req_ch)
  1219. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  1220. value |= ((mport->offset2)
  1221. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  1222. value |= ((mport->offset1)
  1223. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  1224. value |= (mport->sinterval & 0xFF);
  1225. swr_master_write(swrm,
  1226. SWRM_DP_PORT_CTRL_BANK((i + 1), bank),
  1227. value);
  1228. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  1229. __func__, i,
  1230. (SWRM_DP_PORT_CTRL_BANK((i + 1), bank)), value);
  1231. if (!mport->req_ch)
  1232. swrm_pcm_port_config(swrm, (i + 1), mport, false);
  1233. }
  1234. }
  1235. static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
  1236. {
  1237. struct swr_port_info *port_req, *next;
  1238. int i;
  1239. struct swrm_mports *mport;
  1240. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1241. if (!swrm) {
  1242. pr_err_ratelimited("%s: swrm is null\n", __func__);
  1243. return;
  1244. }
  1245. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  1246. master->num_port);
  1247. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  1248. mport = &(swrm->mport_cfg[i]);
  1249. list_for_each_entry_safe(port_req, next,
  1250. &mport->port_req_list, list) {
  1251. /* skip ports without new ch req */
  1252. if (port_req->ch_en == port_req->req_ch)
  1253. continue;
  1254. /* remove new ch req's*/
  1255. port_req->ch_en = port_req->req_ch;
  1256. /* If no streams enabled on port, remove the port req */
  1257. if (port_req->ch_en == 0) {
  1258. list_del(&port_req->list);
  1259. kfree(port_req);
  1260. }
  1261. }
  1262. /* remove new ch req's on mport*/
  1263. mport->ch_en = mport->req_ch;
  1264. if (!(mport->ch_en)) {
  1265. mport->port_en = false;
  1266. master->port_en_mask &= ~i;
  1267. }
  1268. }
  1269. }
  1270. static u8 swrm_get_controller_offset1(struct swr_mstr_ctrl *swrm,
  1271. u8* dev_offset, u8 off1)
  1272. {
  1273. u8 offset1 = 0x0F;
  1274. int i = 0;
  1275. if (swrm->master_id == MASTER_ID_TX) {
  1276. for (i = 1; i < SWRM_NUM_AUTO_ENUM_SLAVES; i++) {
  1277. pr_debug("%s: dev offset: %d\n",
  1278. __func__, dev_offset[i]);
  1279. if (offset1 > dev_offset[i])
  1280. offset1 = dev_offset[i];
  1281. }
  1282. } else {
  1283. offset1 = off1;
  1284. }
  1285. pr_debug("%s: offset: %d\n", __func__, offset1);
  1286. return offset1;
  1287. }
  1288. static int swrm_get_uc(int bus_clk)
  1289. {
  1290. switch (bus_clk) {
  1291. case SWR_CLK_RATE_4P8MHZ:
  1292. return SWR_UC1;
  1293. case SWR_CLK_RATE_1P2MHZ:
  1294. return SWR_UC2;
  1295. case SWR_CLK_RATE_0P6MHZ:
  1296. return SWR_UC3;
  1297. case SWR_CLK_RATE_9P6MHZ:
  1298. default:
  1299. return SWR_UC0;
  1300. }
  1301. return SWR_UC0;
  1302. }
  1303. static int swrm_adjust_sample_rate(u32 sample_rate)
  1304. {
  1305. switch (sample_rate) {
  1306. case SAMPLING_RATE_44P1KHZ:
  1307. return SAMPLING_RATE_48KHZ;
  1308. case SAMPLING_RATE_88P2KHZ:
  1309. return SAMPLING_RATE_96KHZ;
  1310. case SAMPLING_RATE_176P4KHZ:
  1311. return SAMPLING_RATE_192KHZ;
  1312. case SAMPLING_RATE_352P8KHZ:
  1313. return SAMPLING_RATE_384KHZ;
  1314. default:
  1315. return sample_rate;
  1316. }
  1317. }
  1318. static void swrm_get_device_frame_shape(struct swr_mstr_ctrl *swrm,
  1319. struct swrm_mports *mport,
  1320. struct swr_port_info *port_req)
  1321. {
  1322. u32 uc = SWR_UC0;
  1323. u32 port_id_offset = 0;
  1324. if (swrm->master_id == MASTER_ID_TX) {
  1325. uc = swrm_get_uc(swrm->bus_clk);
  1326. port_id_offset = (port_req->dev_num - 1) *
  1327. SWR_MAX_DEV_PORT_NUM +
  1328. port_req->slave_port_id;
  1329. if (port_id_offset >= SWR_MAX_MSTR_PORT_NUM)
  1330. return;
  1331. port_req->sinterval =
  1332. ((swrm->bus_clk * 2) / port_req->ch_rate) - 1;
  1333. port_req->offset1 = swrm->pp[uc][port_id_offset].offset1;
  1334. port_req->offset2 = 0x00;
  1335. port_req->hstart = 0xFF;
  1336. port_req->hstop = 0xFF;
  1337. port_req->word_length = 0xFF;
  1338. port_req->blk_pack_mode = 0xFF;
  1339. port_req->blk_grp_count = 0xFF;
  1340. port_req->lane_ctrl = swrm->pp[uc][port_id_offset].lane_ctrl;
  1341. } else if (swrm->master_id == MASTER_ID_BT) {
  1342. port_req->sinterval =
  1343. ((swrm->bus_clk * 2) / port_req->ch_rate) - 1;
  1344. if (mport->dir == 0)
  1345. port_req->offset1 = 0;
  1346. else
  1347. port_req->offset1 = 0x14;
  1348. port_req->offset2 = 0x00;
  1349. port_req->hstart = 1;
  1350. port_req->hstop = 0xF;
  1351. port_req->word_length = 0xF;
  1352. port_req->blk_pack_mode = 0xFF;
  1353. port_req->blk_grp_count = 0xFF;
  1354. port_req->lane_ctrl = 0;
  1355. } else {
  1356. /* copy master port config to slave */
  1357. port_req->sinterval = mport->sinterval;
  1358. port_req->offset1 = mport->offset1;
  1359. port_req->offset2 = mport->offset2;
  1360. port_req->hstart = mport->hstart;
  1361. port_req->hstop = mport->hstop;
  1362. port_req->word_length = mport->word_length;
  1363. port_req->blk_pack_mode = mport->blk_pack_mode;
  1364. port_req->blk_grp_count = mport->blk_grp_count;
  1365. port_req->lane_ctrl = mport->lane_ctrl;
  1366. }
  1367. if (swrm->master_id == MASTER_ID_WSA) {
  1368. uc = swrm_get_uc(swrm->bus_clk);
  1369. port_id_offset = (port_req->dev_num - 1) *
  1370. SWR_MAX_DEV_PORT_NUM +
  1371. port_req->slave_port_id;
  1372. if (port_id_offset >= SWR_MAX_MSTR_PORT_NUM ||
  1373. !swrm->pp[uc][port_id_offset].offset1)
  1374. return;
  1375. port_req->offset1 = swrm->pp[uc][port_id_offset].offset1;
  1376. }
  1377. }
  1378. static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
  1379. {
  1380. u32 value = 0, slv_id = 0;
  1381. struct swr_port_info *port_req;
  1382. int i, j;
  1383. u16 sinterval = 0xFFFF;
  1384. u8 lane_ctrl = 0;
  1385. struct swrm_mports *mport;
  1386. u32 reg[SWRM_MAX_PORT_REG];
  1387. u32 val[SWRM_MAX_PORT_REG];
  1388. int len = 0;
  1389. u8 hparams = 0;
  1390. u32 controller_offset = 0;
  1391. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1392. u8 dev_offset[SWRM_NUM_AUTO_ENUM_SLAVES];
  1393. if (!swrm) {
  1394. pr_err_ratelimited("%s: swrm is null\n", __func__);
  1395. return;
  1396. }
  1397. memset(dev_offset, 0xff, SWRM_NUM_AUTO_ENUM_SLAVES);
  1398. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  1399. master->num_port);
  1400. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  1401. mport = &(swrm->mport_cfg[i]);
  1402. if (!mport->port_en)
  1403. continue;
  1404. swrm_pcm_port_config(swrm, (i + 1), mport, true);
  1405. j = 0;
  1406. lane_ctrl = 0;
  1407. sinterval = 0xFFFF;
  1408. list_for_each_entry(port_req, &mport->port_req_list, list) {
  1409. if (!port_req->dev_num)
  1410. continue;
  1411. j++;
  1412. slv_id = port_req->slave_port_id;
  1413. /* Assumption: If different channels in the same port
  1414. * on master is enabled for different slaves, then each
  1415. * slave offset should be configured differently.
  1416. */
  1417. swrm_get_device_frame_shape(swrm, mport, port_req);
  1418. if (j == 1) {
  1419. sinterval = port_req->sinterval;
  1420. lane_ctrl = port_req->lane_ctrl;
  1421. } else if (sinterval != port_req->sinterval ||
  1422. lane_ctrl != port_req->lane_ctrl) {
  1423. dev_err_ratelimited(swrm->dev,
  1424. "%s:slaves/slave ports attaching to mport%d"\
  1425. " are not using same SI or data lane, update slave tables,"\
  1426. "bailing out without setting port config\n",
  1427. __func__, i);
  1428. return;
  1429. }
  1430. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1431. val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
  1432. port_req->dev_num, get_cmd_id(swrm),
  1433. SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
  1434. bank));
  1435. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1436. val[len++] = SWR_REG_VAL_PACK(
  1437. port_req->sinterval & 0xFF,
  1438. port_req->dev_num, get_cmd_id(swrm),
  1439. SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
  1440. bank));
  1441. /* Only wite MSB if SI > 0xFF */
  1442. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1443. val[len++] = SWR_REG_VAL_PACK(
  1444. (port_req->sinterval >> 8) & 0xFF,
  1445. port_req->dev_num, get_cmd_id(swrm),
  1446. SWRS_DP_SAMPLE_CONTROL_2_BANK(slv_id,
  1447. bank));
  1448. if (port_req->offset1 != SWR_INVALID_PARAM) {
  1449. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1450. val[len++] = SWR_REG_VAL_PACK(port_req->offset1,
  1451. port_req->dev_num, get_cmd_id(swrm),
  1452. SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
  1453. bank));
  1454. }
  1455. if (port_req->offset2 != SWR_INVALID_PARAM) {
  1456. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1457. val[len++] = SWR_REG_VAL_PACK(port_req->offset2,
  1458. port_req->dev_num, get_cmd_id(swrm),
  1459. SWRS_DP_OFFSET_CONTROL_2_BANK(
  1460. slv_id, bank));
  1461. }
  1462. if (port_req->hstart != SWR_INVALID_PARAM
  1463. && port_req->hstop != SWR_INVALID_PARAM) {
  1464. hparams = (port_req->hstart << 4) |
  1465. port_req->hstop;
  1466. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1467. val[len++] = SWR_REG_VAL_PACK(hparams,
  1468. port_req->dev_num, get_cmd_id(swrm),
  1469. SWRS_DP_HCONTROL_BANK(slv_id,
  1470. bank));
  1471. }
  1472. if (port_req->word_length != SWR_INVALID_PARAM) {
  1473. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1474. val[len++] =
  1475. SWR_REG_VAL_PACK(port_req->word_length,
  1476. port_req->dev_num, get_cmd_id(swrm),
  1477. SWRS_DP_BLOCK_CONTROL_1(slv_id));
  1478. }
  1479. if (port_req->blk_pack_mode != SWR_INVALID_PARAM) {
  1480. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1481. val[len++] =
  1482. SWR_REG_VAL_PACK(
  1483. port_req->blk_pack_mode,
  1484. port_req->dev_num, get_cmd_id(swrm),
  1485. SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
  1486. bank));
  1487. }
  1488. if (port_req->blk_grp_count != SWR_INVALID_PARAM) {
  1489. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1490. val[len++] =
  1491. SWR_REG_VAL_PACK(
  1492. port_req->blk_grp_count,
  1493. port_req->dev_num, get_cmd_id(swrm),
  1494. SWRS_DP_BLOCK_CONTROL_2_BANK(
  1495. slv_id, bank));
  1496. }
  1497. if (port_req->lane_ctrl != SWR_INVALID_PARAM) {
  1498. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1499. val[len++] =
  1500. SWR_REG_VAL_PACK(port_req->lane_ctrl,
  1501. port_req->dev_num, get_cmd_id(swrm),
  1502. SWRS_DP_LANE_CONTROL_BANK(
  1503. slv_id, bank));
  1504. }
  1505. if (port_req->req_ch_rate != port_req->ch_rate) {
  1506. dev_dbg(swrm->dev, "requested sample rate is fractional");
  1507. if (mport->dir == 0) {
  1508. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1509. val[len++] =
  1510. SWR_REG_VAL_PACK(1,
  1511. port_req->dev_num, get_cmd_id(swrm),
  1512. SWRS_DP_PORT_CONTROL(
  1513. slv_id));
  1514. } else if (mport->dir == 1) {
  1515. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1516. val[len++] =
  1517. SWR_REG_VAL_PACK(2,
  1518. port_req->dev_num, get_cmd_id(swrm),
  1519. SWRS_DP_PORT_CONTROL(
  1520. slv_id));
  1521. }
  1522. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1523. val[len++] = SWR_REG_VAL_PACK(4,
  1524. port_req->dev_num, get_cmd_id(swrm),
  1525. SWRS_DPn_FEATURE_EN(port_req->slave_port_id));
  1526. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1527. val[len++] = SWR_REG_VAL_PACK(1,
  1528. port_req->dev_num, get_cmd_id(swrm),
  1529. SWRS_DPn_FLOW_CTRL_N_REPEAT_PERIOD(
  1530. port_req->slave_port_id));
  1531. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1532. val[len++] = SWR_REG_VAL_PACK(1,
  1533. port_req->dev_num, get_cmd_id(swrm),
  1534. SWRS_DPn_FLOW_CTRL_M_VALID_SAMPLE(
  1535. port_req->slave_port_id));
  1536. } else {
  1537. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1538. val[len++] = SWR_REG_VAL_PACK(0, port_req->dev_num,
  1539. get_cmd_id(swrm), SWRS_DP_PORT_CONTROL(slv_id));
  1540. reg[len] = SWRM_CMD_FIFO_WR_CMD(swrm->ee_val);
  1541. val[len++] = SWR_REG_VAL_PACK(0, port_req->dev_num,
  1542. get_cmd_id(swrm),
  1543. SWRS_DPn_FEATURE_EN(port_req->slave_port_id));
  1544. }
  1545. port_req->ch_en = port_req->req_ch;
  1546. dev_offset[port_req->dev_num] = port_req->offset1;
  1547. }
  1548. if (swrm->master_id == MASTER_ID_TX) {
  1549. mport->sinterval = sinterval;
  1550. mport->lane_ctrl = lane_ctrl;
  1551. } else if (swrm->master_id == MASTER_ID_BT) {
  1552. mport->sinterval = sinterval;
  1553. mport->lane_ctrl = lane_ctrl;
  1554. mport->word_length = 0xF;
  1555. mport->hstart = 1;
  1556. mport->hstop = 0xF;
  1557. }
  1558. value = ((mport->req_ch)
  1559. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  1560. if (mport->offset2 != SWR_INVALID_PARAM)
  1561. value |= ((mport->offset2)
  1562. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  1563. controller_offset = (swrm_get_controller_offset1(swrm,
  1564. dev_offset, mport->offset1));
  1565. value |= (controller_offset << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  1566. mport->offset1 = controller_offset;
  1567. value |= (mport->sinterval & 0xFF);
  1568. reg[len] = SWRM_DP_PORT_CTRL_BANK((i + 1), bank);
  1569. val[len++] = value;
  1570. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  1571. __func__, (i + 1),
  1572. (SWRM_DP_PORT_CTRL_BANK((i + 1), bank)), value);
  1573. reg[len] = SWRM_DP_SAMPLECTRL2_BANK((i + 1), bank);
  1574. val[len++] = ((mport->sinterval >> 8) & 0xFF);
  1575. if (mport->lane_ctrl != SWR_INVALID_PARAM) {
  1576. reg[len] = SWRM_DP_PORT_CTRL_2_BANK((i + 1), bank);
  1577. val[len++] = mport->lane_ctrl;
  1578. }
  1579. if (mport->word_length != SWR_INVALID_PARAM) {
  1580. reg[len] = SWRM_DP_BLOCK_CTRL_1((i + 1));
  1581. val[len++] = mport->word_length;
  1582. }
  1583. if (mport->blk_grp_count != SWR_INVALID_PARAM) {
  1584. reg[len] = SWRM_DP_BLOCK_CTRL2_BANK((i + 1), bank);
  1585. val[len++] = mport->blk_grp_count;
  1586. }
  1587. if (mport->hstart != SWR_INVALID_PARAM
  1588. && mport->hstop != SWR_INVALID_PARAM) {
  1589. reg[len] = SWRM_DP_PORT_HCTRL_BANK((i + 1), bank);
  1590. hparams = (mport->hstop << 4) | mport->hstart;
  1591. val[len++] = hparams;
  1592. } else {
  1593. reg[len] = SWRM_DP_PORT_HCTRL_BANK((i + 1), bank);
  1594. hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
  1595. val[len++] = hparams;
  1596. }
  1597. if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
  1598. reg[len] = SWRM_DP_BLOCK_CTRL3_BANK((i + 1), bank);
  1599. val[len++] = mport->blk_pack_mode;
  1600. }
  1601. mport->ch_en = mport->req_ch;
  1602. }
  1603. swrm_reg_dump(swrm, reg, val, len, __func__);
  1604. swr_master_bulk_write(swrm, reg, val, len);
  1605. }
  1606. static void swrm_apply_port_config(struct swr_master *master)
  1607. {
  1608. u8 bank;
  1609. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1610. if (!swrm) {
  1611. pr_err_ratelimited("%s: Invalid handle to swr controller\n",
  1612. __func__);
  1613. return;
  1614. }
  1615. bank = get_inactive_bank_num(swrm);
  1616. dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
  1617. __func__, bank, master->num_port);
  1618. if (!swrm->disable_div2_clk_switch)
  1619. swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, get_cmd_id(swrm),
  1620. SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
  1621. swrm_copy_data_port_config(master, bank);
  1622. }
  1623. static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
  1624. {
  1625. u8 bank;
  1626. u32 value = 0, n_row = 0, n_col = 0;
  1627. u32 row = 0, col = 0;
  1628. int bus_clk_div_factor;
  1629. int ret;
  1630. u8 ssp_period = 0;
  1631. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1632. int mask = (SWRM_ROW_CTRL_MASK | SWRM_COL_CTRL_MASK |
  1633. SWRM_CLK_DIV_MASK | SWRM_SSP_PERIOD_MASK);
  1634. u8 inactive_bank;
  1635. int frame_sync = SWRM_FRAME_SYNC_SEL;
  1636. if (!swrm) {
  1637. pr_err_ratelimited("%s: swrm is null\n", __func__);
  1638. return -EFAULT;
  1639. }
  1640. mutex_lock(&swrm->mlock);
  1641. /*
  1642. * During disable if master is already down, which implies an ssr/pdr
  1643. * scenario, just mark ports as disabled and exit
  1644. */
  1645. if (swrm->state == SWR_MSTR_SSR && !enable) {
  1646. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  1647. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  1648. __func__);
  1649. goto exit;
  1650. }
  1651. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1652. swrm_cleanup_disabled_port_reqs(master);
  1653. /* reset enable_count to 0 in SSR if master is already down */
  1654. swrm->pcm_enable_count = 0;
  1655. if (!swrm_is_port_en(master)) {
  1656. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  1657. __func__);
  1658. pm_runtime_mark_last_busy(swrm->dev);
  1659. pm_runtime_put_autosuspend(swrm->dev);
  1660. }
  1661. goto exit;
  1662. }
  1663. bank = get_inactive_bank_num(swrm);
  1664. if (enable) {
  1665. if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
  1666. dev_dbg(swrm->dev, "%s:No pending connect port req\n",
  1667. __func__);
  1668. goto exit;
  1669. }
  1670. clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1671. ret = swrm_get_port_config(swrm);
  1672. if (ret) {
  1673. /* cannot accommodate ports */
  1674. swrm_cleanup_disabled_port_reqs(master);
  1675. mutex_unlock(&swrm->mlock);
  1676. return -EINVAL;
  1677. }
  1678. swr_master_write(swrm, SWRM_INTERRUPT_EN(swrm->ee_val),
  1679. SWRM_INTERRUPT_STATUS_MASK);
  1680. /* apply the new port config*/
  1681. swrm_apply_port_config(master);
  1682. } else {
  1683. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  1684. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  1685. __func__);
  1686. goto exit;
  1687. }
  1688. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1689. swrm_disable_ports(master, bank);
  1690. }
  1691. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d freq %d\n",
  1692. __func__, enable, swrm->num_cfg_devs, swrm->mclk_freq);
  1693. if (enable) {
  1694. /* set col = 16 */
  1695. n_col = SWR_MAX_COL;
  1696. col = SWRM_COL_16;
  1697. if (swrm->bus_clk == MCLK_FREQ_LP) {
  1698. n_col = SWR_MIN_COL;
  1699. col = SWRM_COL_02;
  1700. }
  1701. } else {
  1702. /*
  1703. * Do not change to col = 2 if there are still active ports
  1704. */
  1705. if (!master->num_port) {
  1706. n_col = SWR_MIN_COL;
  1707. col = SWRM_COL_02;
  1708. } else {
  1709. n_col = SWR_MAX_COL;
  1710. col = SWRM_COL_16;
  1711. }
  1712. }
  1713. /* Use default 50 * x, frame shape. Change based on mclk */
  1714. if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
  1715. dev_dbg(swrm->dev, "setting 64 x %d frameshape\n", col);
  1716. n_row = SWR_ROW_64;
  1717. row = SWRM_ROW_64;
  1718. frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
  1719. } else if (swrm->mclk_freq == MCLK_FREQ_12288) {
  1720. dev_dbg(swrm->dev, "setting 64 x %d frameshape\n", col);
  1721. n_row = SWR_ROW_64;
  1722. row = SWRM_ROW_64;
  1723. frame_sync = SWRM_FRAME_SYNC_SEL;
  1724. } else {
  1725. dev_dbg(swrm->dev, "setting 50 x %d frameshape\n", col);
  1726. n_row = SWR_ROW_50;
  1727. row = SWRM_ROW_50;
  1728. frame_sync = SWRM_FRAME_SYNC_SEL;
  1729. }
  1730. ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
  1731. bus_clk_div_factor = swrm_get_clk_div(swrm->mclk_freq, swrm->bus_clk);
  1732. dev_dbg(swrm->dev, "%s: ssp_period: %d, bus_clk_div:%d \n", __func__,
  1733. ssp_period, bus_clk_div_factor);
  1734. value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK(bank));
  1735. value &= (~mask);
  1736. value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1737. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1738. (bus_clk_div_factor <<
  1739. SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT) |
  1740. ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1741. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK(bank), value);
  1742. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  1743. SWRM_MCP_FRAME_CTRL_BANK(bank), value);
  1744. enable_bank_switch(swrm, bank, n_row, n_col);
  1745. inactive_bank = bank ? 0 : 1;
  1746. if (enable)
  1747. swrm_copy_data_port_config(master, inactive_bank);
  1748. else {
  1749. swrm_disable_ports(master, inactive_bank);
  1750. swrm_cleanup_disabled_port_reqs(master);
  1751. }
  1752. if (!swrm_is_port_en(master)) {
  1753. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  1754. __func__);
  1755. pm_runtime_mark_last_busy(swrm->dev);
  1756. if (!enable)
  1757. pm_runtime_set_autosuspend_delay(swrm->dev, 80);
  1758. pm_runtime_put_autosuspend(swrm->dev);
  1759. }
  1760. exit:
  1761. mutex_unlock(&swrm->mlock);
  1762. return 0;
  1763. }
  1764. static int swrm_connect_port(struct swr_master *master,
  1765. struct swr_params *portinfo)
  1766. {
  1767. int i;
  1768. struct swr_port_info *port_req;
  1769. int ret = 0;
  1770. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1771. struct swrm_mports *mport;
  1772. u8 mstr_port_id, mstr_ch_msk;
  1773. dev_dbg(&master->dev, "%s: enter\n", __func__);
  1774. if (!portinfo)
  1775. return -EINVAL;
  1776. if (!swrm) {
  1777. dev_err_ratelimited(&master->dev,
  1778. "%s: Invalid handle to swr controller\n",
  1779. __func__);
  1780. return -EINVAL;
  1781. }
  1782. mutex_lock(&swrm->mlock);
  1783. mutex_lock(&swrm->devlock);
  1784. if (!swrm->dev_up) {
  1785. swr_port_response(master, portinfo->tid);
  1786. mutex_unlock(&swrm->devlock);
  1787. mutex_unlock(&swrm->mlock);
  1788. return -EINVAL;
  1789. }
  1790. mutex_unlock(&swrm->devlock);
  1791. if (!swrm_is_port_en(master))
  1792. pm_runtime_get_sync(swrm->dev);
  1793. for (i = 0; i < portinfo->num_port; i++) {
  1794. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
  1795. portinfo->port_type[i],
  1796. portinfo->port_id[i]);
  1797. if (ret) {
  1798. dev_err_ratelimited(&master->dev,
  1799. "%s: mstr portid for slv port %d not found\n",
  1800. __func__, portinfo->port_id[i]);
  1801. goto port_fail;
  1802. }
  1803. mport = &(swrm->mport_cfg[mstr_port_id]);
  1804. /* get port req */
  1805. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1806. portinfo->dev_num);
  1807. if (!port_req) {
  1808. dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
  1809. __func__, portinfo->port_id[i],
  1810. portinfo->dev_num);
  1811. port_req = kzalloc(sizeof(struct swr_port_info),
  1812. GFP_KERNEL);
  1813. if (!port_req) {
  1814. ret = -ENOMEM;
  1815. goto mem_fail;
  1816. }
  1817. port_req->dev_num = portinfo->dev_num;
  1818. port_req->slave_port_id = portinfo->port_id[i];
  1819. port_req->num_ch = portinfo->num_ch[i];
  1820. port_req->ch_rate = portinfo->ch_rate[i];
  1821. port_req->req_ch_rate = portinfo->ch_rate[i];
  1822. if (swrm_is_fractional_sample_rate(port_req->ch_rate))
  1823. port_req->ch_rate = swrm_adjust_sample_rate(port_req->ch_rate);
  1824. port_req->ch_en = 0;
  1825. port_req->master_port_id = mstr_port_id;
  1826. list_add(&port_req->list, &mport->port_req_list);
  1827. }
  1828. port_req->req_ch |= portinfo->ch_en[i];
  1829. dev_dbg(&master->dev,
  1830. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d req_ch_rate %d\n",
  1831. __func__, port_req->master_port_id,
  1832. port_req->slave_port_id, port_req->ch_rate,
  1833. port_req->num_ch, port_req->req_ch_rate);
  1834. /* Put the port req on master port */
  1835. mport = &(swrm->mport_cfg[mstr_port_id]);
  1836. mport->port_en = true;
  1837. mport->req_ch |= mstr_ch_msk;
  1838. master->port_en_mask |= (1 << mstr_port_id);
  1839. if (swrm->clk_stop_mode0_supp &&
  1840. swrm->dynamic_port_map_supported) {
  1841. mport->ch_rate += portinfo->ch_rate[i];
  1842. swrm_update_bus_clk(swrm);
  1843. } else {
  1844. /*
  1845. * Fallback to assign slave port ch_rate
  1846. * as master port uses same ch_rate as slave
  1847. * unlike soundwire TX master ports where
  1848. * unified ports and multiple slave port
  1849. * channels can attach to same master port
  1850. */
  1851. mport->ch_rate = portinfo->ch_rate[i];
  1852. }
  1853. }
  1854. master->num_port += portinfo->num_port;
  1855. set_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1856. swr_port_response(master, portinfo->tid);
  1857. mutex_unlock(&swrm->mlock);
  1858. return 0;
  1859. port_fail:
  1860. mem_fail:
  1861. swr_port_response(master, portinfo->tid);
  1862. /* cleanup port reqs in error condition */
  1863. swrm_cleanup_disabled_port_reqs(master);
  1864. mutex_unlock(&swrm->mlock);
  1865. return ret;
  1866. }
  1867. static int swrm_disconnect_port(struct swr_master *master,
  1868. struct swr_params *portinfo)
  1869. {
  1870. int i, ret = 0;
  1871. struct swr_port_info *port_req;
  1872. struct swrm_mports *mport;
  1873. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1874. u8 mstr_port_id, mstr_ch_mask;
  1875. u8 num_port = 0;
  1876. if (!swrm) {
  1877. dev_err_ratelimited(&master->dev,
  1878. "%s: Invalid handle to swr controller\n",
  1879. __func__);
  1880. return -EINVAL;
  1881. }
  1882. if (!portinfo) {
  1883. dev_err_ratelimited(&master->dev, "%s: portinfo is NULL\n", __func__);
  1884. return -EINVAL;
  1885. }
  1886. mutex_lock(&swrm->mlock);
  1887. for (i = 0; i < portinfo->num_port; i++) {
  1888. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
  1889. portinfo->port_type[i], portinfo->port_id[i]);
  1890. if (ret) {
  1891. dev_err_ratelimited(&master->dev,
  1892. "%s: mstr portid for slv port %d not found\n",
  1893. __func__, portinfo->port_id[i]);
  1894. goto err;
  1895. }
  1896. mport = &(swrm->mport_cfg[mstr_port_id]);
  1897. /* get port req */
  1898. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1899. portinfo->dev_num);
  1900. if (!port_req) {
  1901. dev_err_ratelimited(&master->dev, "%s:port not enabled : port %d\n",
  1902. __func__, portinfo->port_id[i]);
  1903. continue;
  1904. }
  1905. port_req->req_ch &= ~portinfo->ch_en[i];
  1906. mport->req_ch &= ~mstr_ch_mask;
  1907. if (swrm->clk_stop_mode0_supp &&
  1908. swrm->dynamic_port_map_supported &&
  1909. !mport->req_ch) {
  1910. mport->ch_rate = 0;
  1911. swrm_update_bus_clk(swrm);
  1912. }
  1913. num_port++;
  1914. }
  1915. if (master->num_port > num_port)
  1916. master->num_port -= num_port;
  1917. else
  1918. master->num_port = 0;
  1919. set_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1920. swr_port_response(master, portinfo->tid);
  1921. mutex_unlock(&swrm->mlock);
  1922. return 0;
  1923. err:
  1924. swr_port_response(master, portinfo->tid);
  1925. mutex_unlock(&swrm->mlock);
  1926. return -EINVAL;
  1927. }
  1928. static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
  1929. int status, u8 *devnum)
  1930. {
  1931. int i;
  1932. bool found = false;
  1933. for (i = 0; i < (swrm->num_dev + 1); i++) {
  1934. if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
  1935. *devnum = i;
  1936. found = true;
  1937. break;
  1938. }
  1939. status >>= 2;
  1940. }
  1941. if (found)
  1942. return 0;
  1943. else
  1944. return -EINVAL;
  1945. }
  1946. static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
  1947. {
  1948. int i;
  1949. int status = 0;
  1950. u32 temp;
  1951. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1952. if (!status) {
  1953. dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
  1954. __func__, status);
  1955. return;
  1956. }
  1957. dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
  1958. for (i = 0; i < (swrm->num_dev + 1); i++) {
  1959. if (status & SWRM_MCP_SLV_STATUS_MASK) {
  1960. if (!swrm->clk_stop_wakeup) {
  1961. swrm_cmd_fifo_rd_cmd(swrm, &temp, i,
  1962. get_cmd_id(swrm), SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  1963. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, i,
  1964. get_cmd_id(swrm), SWRS_SCP_INT_STATUS_CLEAR_1);
  1965. }
  1966. swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, get_cmd_id(swrm),
  1967. SWRS_SCP_INT_STATUS_MASK_1);
  1968. }
  1969. status >>= 2;
  1970. }
  1971. }
  1972. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  1973. int status, u8 *devnum)
  1974. {
  1975. int i;
  1976. int new_sts = status;
  1977. int ret = SWR_NOT_PRESENT;
  1978. if (status != swrm->slave_status) {
  1979. for (i = 0; i < (swrm->num_dev + 1); i++) {
  1980. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  1981. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  1982. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  1983. *devnum = i;
  1984. break;
  1985. }
  1986. status >>= 2;
  1987. swrm->slave_status >>= 2;
  1988. }
  1989. swrm->slave_status = new_sts;
  1990. }
  1991. return ret;
  1992. }
  1993. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  1994. {
  1995. struct swr_mstr_ctrl *swrm = dev;
  1996. u32 value, intr_sts, intr_sts_masked;
  1997. u32 temp = 0;
  1998. u32 status, chg_sts, i;
  1999. u8 devnum = 0;
  2000. int ret = IRQ_HANDLED;
  2001. struct swr_device *swr_dev;
  2002. struct swr_master *mstr = &swrm->master;
  2003. int retry = 5;
  2004. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  2005. dev_err_ratelimited(swrm->dev, "%s Failed to hold suspend\n", __func__);
  2006. return IRQ_NONE;
  2007. }
  2008. mutex_lock(&swrm->reslock);
  2009. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  2010. ret = IRQ_NONE;
  2011. goto exit;
  2012. }
  2013. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
  2014. ret = IRQ_NONE;
  2015. goto err_audio_hw_vote;
  2016. }
  2017. ret = swrm_clk_request(swrm, true);
  2018. if (ret) {
  2019. dev_err_ratelimited(dev, "%s: swrm clk failed\n", __func__);
  2020. ret = IRQ_NONE;
  2021. goto err_audio_core_vote;
  2022. }
  2023. mutex_unlock(&swrm->reslock);
  2024. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS(swrm->ee_val));
  2025. intr_sts_masked = intr_sts & swrm->intr_mask;
  2026. dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
  2027. handle_irq:
  2028. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  2029. value = intr_sts_masked & (1 << i);
  2030. if (!value)
  2031. continue;
  2032. switch (value) {
  2033. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  2034. dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
  2035. __func__);
  2036. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  2037. ret = swrm_find_alert_slave(swrm, status, &devnum);
  2038. if (ret) {
  2039. dev_err_ratelimited(swrm->dev,
  2040. "%s: no slave alert found.spurious interrupt\n",
  2041. __func__);
  2042. break;
  2043. }
  2044. swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum,
  2045. get_cmd_id(swrm),
  2046. SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  2047. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum,
  2048. get_cmd_id(swrm),
  2049. SWRS_SCP_INT_STATUS_CLEAR_1);
  2050. swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum,
  2051. get_cmd_id(swrm),
  2052. SWRS_SCP_INT_STATUS_CLEAR_1);
  2053. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2054. if (swr_dev->dev_num != devnum)
  2055. continue;
  2056. if (swr_dev->slave_irq) {
  2057. do {
  2058. swr_dev->slave_irq_pending = 0;
  2059. handle_nested_irq(
  2060. irq_find_mapping(
  2061. swr_dev->slave_irq, 0));
  2062. } while (swr_dev->slave_irq_pending && swrm->dev_up);
  2063. }
  2064. }
  2065. break;
  2066. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  2067. dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
  2068. __func__);
  2069. break;
  2070. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  2071. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  2072. swrm_enable_slave_irq(swrm);
  2073. if (status == swrm->slave_status) {
  2074. dev_dbg(swrm->dev,
  2075. "%s: No change in slave status: 0x%x\n",
  2076. __func__, status);
  2077. break;
  2078. }
  2079. chg_sts = swrm_check_slave_change_status(swrm, status,
  2080. &devnum);
  2081. switch (chg_sts) {
  2082. case SWR_NOT_PRESENT:
  2083. dev_dbg(swrm->dev,
  2084. "%s: device %d got detached\n",
  2085. __func__, devnum);
  2086. if (devnum == 0) {
  2087. /*
  2088. * enable host irq if device 0 detached
  2089. * as hw will mask host_irq at slave
  2090. * but will not unmask it afterwards.
  2091. */
  2092. swrm->enable_slave_irq = true;
  2093. }
  2094. break;
  2095. case SWR_ATTACHED_OK:
  2096. dev_dbg(swrm->dev,
  2097. "%s: device %d got attached\n",
  2098. __func__, devnum);
  2099. /* enable host irq from slave device*/
  2100. swrm->enable_slave_irq = true;
  2101. break;
  2102. case SWR_ALERT:
  2103. dev_dbg(swrm->dev,
  2104. "%s: device %d has pending interrupt\n",
  2105. __func__, devnum);
  2106. break;
  2107. }
  2108. break;
  2109. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  2110. dev_err_ratelimited(swrm->dev,
  2111. "%s: SWR bus clsh detected\n",
  2112. __func__);
  2113. swrm->intr_mask &=
  2114. ~SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET;
  2115. swr_master_write(swrm,
  2116. SWRM_INTERRUPT_EN(swrm->ee_val),
  2117. swrm->intr_mask);
  2118. break;
  2119. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  2120. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  2121. dev_err_ratelimited(swrm->dev,
  2122. "%s: SWR read FIFO overflow fifo status %x\n",
  2123. __func__, value);
  2124. break;
  2125. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  2126. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  2127. dev_err_ratelimited(swrm->dev,
  2128. "%s: SWR read FIFO underflow fifo status %x\n",
  2129. __func__, value);
  2130. break;
  2131. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  2132. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  2133. dev_err_ratelimited(swrm->dev,
  2134. "%s: SWR write FIFO overflow fifo status %x\n",
  2135. __func__, value);
  2136. break;
  2137. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  2138. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  2139. dev_err_ratelimited(swrm->dev,
  2140. "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
  2141. __func__, value);
  2142. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  2143. break;
  2144. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  2145. dev_err_ratelimited(swrm->dev,
  2146. "%s: SWR Port collision detected\n",
  2147. __func__);
  2148. swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
  2149. swr_master_write(swrm,
  2150. SWRM_INTERRUPT_EN(swrm->ee_val),
  2151. swrm->intr_mask);
  2152. break;
  2153. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  2154. dev_dbg(swrm->dev,
  2155. "%s: SWR read enable valid mismatch\n",
  2156. __func__);
  2157. swrm->intr_mask &=
  2158. ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
  2159. swr_master_write(swrm,
  2160. SWRM_INTERRUPT_EN(swrm->ee_val),
  2161. swrm->intr_mask);
  2162. break;
  2163. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  2164. complete(&swrm->broadcast);
  2165. dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
  2166. __func__);
  2167. break;
  2168. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  2169. swr_master_write(swrm, SWRM_ENUMERATOR_CFG, 0);
  2170. while (swr_master_read(swrm, SWRM_ENUMERATOR_STATUS)) {
  2171. if (!retry) {
  2172. dev_dbg(swrm->dev,
  2173. "%s: ENUM status is not idle\n",
  2174. __func__);
  2175. break;
  2176. }
  2177. retry--;
  2178. }
  2179. swr_master_write(swrm, SWRM_ENUMERATOR_CFG, 1);
  2180. break;
  2181. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  2182. break;
  2183. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  2184. swrm_check_link_status(swrm, 0x1);
  2185. break;
  2186. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  2187. break;
  2188. case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
  2189. if (swrm->state == SWR_MSTR_UP) {
  2190. dev_dbg(swrm->dev,
  2191. "%s:SWR Master is already up\n",
  2192. __func__);
  2193. } else {
  2194. dev_err_ratelimited(swrm->dev,
  2195. "%s: SWR wokeup during clock stop\n",
  2196. __func__);
  2197. /* It might be possible the slave device gets
  2198. * reset and slave interrupt gets missed. So
  2199. * re-enable Host IRQ and process slave pending
  2200. * interrupts, if any.
  2201. */
  2202. swrm->clk_stop_wakeup = true;
  2203. swrm_enable_slave_irq(swrm);
  2204. swrm->clk_stop_wakeup = false;
  2205. }
  2206. break;
  2207. #ifdef CONFIG_SWRM_VER_2P0
  2208. case SWRM_INTERRUPT_STATUS_CMD_IGNORED_AND_EXEC_CONTINUED:
  2209. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS(swrm->ee_val));
  2210. dev_err_ratelimited(swrm->dev,
  2211. "%s: SWR CMD Ignored, fifo status 0x%x\n",
  2212. __func__, value);
  2213. /* Wait 3.5ms to clear */
  2214. usleep_range(3500, 3505);
  2215. break;
  2216. #endif
  2217. case SWRM_INTERRUPT_STATUS_DOUT_RATE_MISMATCH:
  2218. dev_err(swrm->dev,
  2219. "%s: SWR Port Channel rate mismatch\n", __func__);
  2220. swrm->intr_mask &=
  2221. ~SWRM_INTERRUPT_STATUS_DOUT_RATE_MISMATCH;
  2222. swr_master_write(swrm,
  2223. SWRM_INTERRUPT_EN(swrm->ee_val), swrm->intr_mask);
  2224. break;
  2225. default:
  2226. dev_err_ratelimited(swrm->dev,
  2227. "%s: SWR unknown interrupt value: %d\n",
  2228. __func__, value);
  2229. ret = IRQ_NONE;
  2230. break;
  2231. }
  2232. }
  2233. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR(swrm->ee_val), intr_sts);
  2234. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR(swrm->ee_val), 0x0);
  2235. if (swrm->enable_slave_irq) {
  2236. /* Enable slave irq here */
  2237. swrm_enable_slave_irq(swrm);
  2238. swrm->enable_slave_irq = false;
  2239. }
  2240. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS(swrm->ee_val));
  2241. intr_sts_masked = intr_sts & swrm->intr_mask;
  2242. if (intr_sts_masked && !pm_runtime_suspended(swrm->dev)) {
  2243. dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
  2244. __func__, intr_sts_masked);
  2245. goto handle_irq;
  2246. }
  2247. mutex_lock(&swrm->reslock);
  2248. swrm_clk_request(swrm, false);
  2249. err_audio_core_vote:
  2250. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  2251. err_audio_hw_vote:
  2252. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  2253. exit:
  2254. mutex_unlock(&swrm->reslock);
  2255. swrm_unlock_sleep(swrm);
  2256. return ret;
  2257. }
  2258. static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
  2259. {
  2260. struct swr_mstr_ctrl *swrm = dev;
  2261. int ret = IRQ_HANDLED;
  2262. if (!swrm || !(swrm->dev)) {
  2263. pr_err_ratelimited("%s: swrm or dev is null\n", __func__);
  2264. return IRQ_NONE;
  2265. }
  2266. mutex_lock(&swrm->devlock);
  2267. if (swrm->state == SWR_MSTR_SSR || !swrm->dev_up) {
  2268. if (swrm->wake_irq > 0) {
  2269. if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
  2270. pr_err_ratelimited("%s: irq data is NULL\n", __func__);
  2271. mutex_unlock(&swrm->devlock);
  2272. return IRQ_NONE;
  2273. }
  2274. mutex_lock(&swrm->irq_lock);
  2275. if (!irqd_irq_disabled(
  2276. irq_get_irq_data(swrm->wake_irq))) {
  2277. irq_set_irq_wake(swrm->wake_irq, 0);
  2278. disable_irq_nosync(swrm->wake_irq);
  2279. }
  2280. mutex_unlock(&swrm->irq_lock);
  2281. }
  2282. mutex_unlock(&swrm->devlock);
  2283. return ret;
  2284. }
  2285. mutex_unlock(&swrm->devlock);
  2286. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  2287. dev_err_ratelimited(swrm->dev, "%s Failed to hold suspend\n", __func__);
  2288. goto exit;
  2289. }
  2290. if (swrm->wake_irq > 0) {
  2291. if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
  2292. pr_err_ratelimited("%s: irq data is NULL\n", __func__);
  2293. return IRQ_NONE;
  2294. }
  2295. mutex_lock(&swrm->irq_lock);
  2296. if (!irqd_irq_disabled(irq_get_irq_data(swrm->wake_irq))) {
  2297. irq_set_irq_wake(swrm->wake_irq, 0);
  2298. disable_irq_nosync(swrm->wake_irq);
  2299. }
  2300. mutex_unlock(&swrm->irq_lock);
  2301. }
  2302. pm_runtime_get_sync(swrm->dev);
  2303. pm_runtime_mark_last_busy(swrm->dev);
  2304. pm_runtime_put_autosuspend(swrm->dev);
  2305. swrm_unlock_sleep(swrm);
  2306. exit:
  2307. return ret;
  2308. }
  2309. static void swrm_wakeup_work(struct work_struct *work)
  2310. {
  2311. struct swr_mstr_ctrl *swrm;
  2312. swrm = container_of(work, struct swr_mstr_ctrl,
  2313. wakeup_work);
  2314. if (!swrm || !(swrm->dev)) {
  2315. pr_err("%s: swrm or dev is null\n", __func__);
  2316. return;
  2317. }
  2318. mutex_lock(&swrm->devlock);
  2319. if (!swrm->dev_up) {
  2320. mutex_unlock(&swrm->devlock);
  2321. goto exit;
  2322. }
  2323. mutex_unlock(&swrm->devlock);
  2324. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  2325. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  2326. goto exit;
  2327. }
  2328. pm_runtime_get_sync(swrm->dev);
  2329. pm_runtime_mark_last_busy(swrm->dev);
  2330. pm_runtime_put_autosuspend(swrm->dev);
  2331. swrm_unlock_sleep(swrm);
  2332. exit:
  2333. pm_relax(swrm->dev);
  2334. }
  2335. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  2336. {
  2337. u32 val;
  2338. swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  2339. val = (swrm->slave_status >> (devnum * 2));
  2340. val &= SWRM_MCP_SLV_STATUS_MASK;
  2341. return val;
  2342. }
  2343. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  2344. u8 *dev_num)
  2345. {
  2346. int i;
  2347. u64 id = 0;
  2348. int ret = -EINVAL;
  2349. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  2350. struct swr_device *swr_dev;
  2351. u32 num_dev = 0;
  2352. if (!swrm) {
  2353. pr_err("%s: Invalid handle to swr controller\n",
  2354. __func__);
  2355. return ret;
  2356. }
  2357. num_dev = swrm->num_dev;
  2358. mutex_lock(&swrm->devlock);
  2359. if (!swrm->dev_up) {
  2360. mutex_unlock(&swrm->devlock);
  2361. return ret;
  2362. }
  2363. mutex_unlock(&swrm->devlock);
  2364. pm_runtime_get_sync(swrm->dev);
  2365. for (i = 1; i < (num_dev + 1); i++) {
  2366. id = ((u64)(swr_master_read(swrm,
  2367. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  2368. id |= swr_master_read(swrm,
  2369. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  2370. /*
  2371. * As pm_runtime_get_sync() brings all slaves out of reset
  2372. * update logical device number for all slaves.
  2373. */
  2374. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2375. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  2376. u32 status = swrm_get_device_status(swrm, i);
  2377. if ((status == 0x01) || (status == 0x02)) {
  2378. swr_dev->dev_num = i;
  2379. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  2380. *dev_num = i;
  2381. ret = 0;
  2382. dev_info(swrm->dev,
  2383. "%s: devnum %d assigned for dev %llx\n",
  2384. __func__, i,
  2385. swr_dev->addr);
  2386. }
  2387. }
  2388. }
  2389. }
  2390. }
  2391. if (ret)
  2392. dev_err(swrm->dev,
  2393. "%s: device 0x%llx is not ready\n",
  2394. __func__, dev_id);
  2395. pm_runtime_mark_last_busy(swrm->dev);
  2396. pm_runtime_put_autosuspend(swrm->dev);
  2397. return ret;
  2398. }
  2399. static int swrm_init_port_params(struct swr_master *mstr, u32 dev_num,
  2400. u32 num_ports,
  2401. struct swr_dev_frame_config *uc_arr)
  2402. {
  2403. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  2404. int i, j, port_id_offset;
  2405. if (!swrm) {
  2406. pr_err("%s: Invalid handle to swr controller\n", __func__);
  2407. return 0;
  2408. }
  2409. if (dev_num == 0) {
  2410. pr_err("%s: Invalid device number 0\n", __func__);
  2411. return -EINVAL;
  2412. }
  2413. for (i = 0; i < SWR_UC_MAX; i++) {
  2414. for (j = 0; j < num_ports; j++) {
  2415. port_id_offset = (dev_num - 1) * SWR_MAX_DEV_PORT_NUM + j;
  2416. swrm->pp[i][port_id_offset].offset1 = uc_arr[i].pp[j].offset1;
  2417. swrm->pp[i][port_id_offset].lane_ctrl = uc_arr[i].pp[j].lane_ctrl;
  2418. }
  2419. }
  2420. return 0;
  2421. }
  2422. static void swrm_device_wakeup_vote(struct swr_master *mstr)
  2423. {
  2424. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  2425. if (!swrm) {
  2426. pr_err_ratelimited("%s: Invalid handle to swr controller\n",
  2427. __func__);
  2428. return;
  2429. }
  2430. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  2431. dev_err_ratelimited(swrm->dev, "%s Failed to hold suspend\n", __func__);
  2432. return;
  2433. }
  2434. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true))
  2435. dev_err_ratelimited(swrm->dev, "%s:lpass core hw enable failed\n",
  2436. __func__);
  2437. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  2438. dev_err_ratelimited(swrm->dev, "%s:lpass audio hw enable failed\n",
  2439. __func__);
  2440. pm_runtime_get_sync(swrm->dev);
  2441. }
  2442. static void swrm_device_wakeup_unvote(struct swr_master *mstr)
  2443. {
  2444. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  2445. if (!swrm) {
  2446. pr_err_ratelimited("%s: Invalid handle to swr controller\n",
  2447. __func__);
  2448. return;
  2449. }
  2450. pm_runtime_mark_last_busy(swrm->dev);
  2451. pm_runtime_put_autosuspend(swrm->dev);
  2452. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  2453. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  2454. swrm_unlock_sleep(swrm);
  2455. }
  2456. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  2457. {
  2458. int ret = 0, i = 0;
  2459. u32 val;
  2460. u8 row_ctrl = SWR_ROW_50;
  2461. u8 col_ctrl = SWR_MIN_COL;
  2462. u8 num_rows = SWRM_ROW_50;
  2463. u8 ssp_period = 1;
  2464. u8 retry_cmd_num = 3;
  2465. u32 reg[SWRM_MAX_INIT_REG];
  2466. u32 value[SWRM_MAX_INIT_REG];
  2467. u32 temp = 0;
  2468. int len = 0;
  2469. /* Change no of retry counts to 1 for wsa to avoid underflow */
  2470. if (swrm->master_id == MASTER_ID_WSA)
  2471. retry_cmd_num = 1;
  2472. /* SW workaround to gate hw_ctl for SWR version >=1.6 */
  2473. if (swrm->version >= SWRM_VERSION_1_6) {
  2474. if (swrm->swrm_hctl_reg) {
  2475. temp = ioread32(swrm->swrm_hctl_reg);
  2476. temp &= 0xFFFFFFFD;
  2477. iowrite32(temp, swrm->swrm_hctl_reg);
  2478. usleep_range(500, 505);
  2479. temp = ioread32(swrm->swrm_hctl_reg);
  2480. dev_dbg(swrm->dev, "%s: hctl_reg val: 0x%x\n",
  2481. __func__, temp);
  2482. }
  2483. }
  2484. if (swrm->master_id == MASTER_ID_BT) {
  2485. row_ctrl = SWR_ROW_64;
  2486. num_rows = SWRM_ROW_64;
  2487. }
  2488. ssp_period = swrm_get_ssp_period(swrm, num_rows,
  2489. SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
  2490. dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
  2491. /* Clear Rows and Cols */
  2492. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  2493. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  2494. ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  2495. reg[len] = SWRM_MCP_FRAME_CTRL_BANK(0);
  2496. value[len++] = val;
  2497. /* Set Auto enumeration flag */
  2498. reg[len] = SWRM_ENUMERATOR_CFG;
  2499. value[len++] = 1;
  2500. /* Configure No pings */
  2501. val = swr_master_read(swrm, SWRM_MCP_CFG);
  2502. val &= ~SWRM_NUM_PINGS_MASK;
  2503. val |= (0x1f << SWRM_NUM_PINGS_POS);
  2504. reg[len] = SWRM_MCP_CFG;
  2505. value[len++] = val;
  2506. /* Configure number of retries of a read/write cmd */
  2507. val = (retry_cmd_num);
  2508. reg[len] = SWRM_CMD_FIFO_CFG;
  2509. value[len++] = val;
  2510. if (swrm->version >= SWRM_VERSION_1_7) {
  2511. reg[len] = SWRM_LINK_MANAGER_EE;
  2512. value[len++] = swrm->ee_val;
  2513. }
  2514. if (swrm->master_id == MASTER_ID_BT) {
  2515. /* Enable self_gen_frame_sync. */
  2516. reg[len] = SWRM_SELF_GENERATE_FRAME_SYNC;
  2517. value[len++] = 0x01;
  2518. }
  2519. #ifdef CONFIG_SWRM_VER_1P7
  2520. reg[len] = SWRM_MCP_BUS_CTRL;
  2521. if (swrm->version < SWRM_VERSION_1_7)
  2522. value[len++] = 0x2;
  2523. else
  2524. value[len++] = 0x2 << swrm->ee_val;
  2525. #endif
  2526. /* Set IRQ to PULSE */
  2527. reg[len] = SWRM_COMP_CFG;
  2528. value[len++] = 0x02;
  2529. reg[len] = SWRM_INTERRUPT_CLEAR(swrm->ee_val);
  2530. value[len++] = 0xFFFFFFFF;
  2531. swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
  2532. /* Mask soundwire interrupts */
  2533. reg[len] = SWRM_INTERRUPT_EN(swrm->ee_val);
  2534. value[len++] = swrm->intr_mask;
  2535. reg[len] = SWRM_COMP_CFG;
  2536. value[len++] = 0x03;
  2537. #ifdef CONFIG_SWRM_VER_2P0
  2538. reg[len] = SWRM_CLK_CTRL(swrm->ee_val);
  2539. value[len++] = 0x01;
  2540. #endif
  2541. swr_master_bulk_write(swrm, reg, value, len);
  2542. if (!swrm_check_link_status(swrm, 0x1)) {
  2543. dev_err(swrm->dev,
  2544. "%s: swr link failed to connect\n",
  2545. __func__);
  2546. for (i = 0; i < len; i++) {
  2547. usleep_range(50, 55);
  2548. dev_err(swrm->dev,
  2549. "%s:reg:0x%x val:0x%x\n",
  2550. __func__,
  2551. reg[i], swr_master_read(swrm, reg[i]));
  2552. }
  2553. return -EINVAL;
  2554. }
  2555. /* Execute it for versions >= 1.5.1 */
  2556. if (swrm->version >= SWRM_VERSION_1_5_1)
  2557. swr_master_write(swrm, SWRM_CMD_FIFO_CFG,
  2558. (swr_master_read(swrm,
  2559. SWRM_CMD_FIFO_CFG) | 0x80000000));
  2560. return ret;
  2561. }
  2562. static int swrm_event_notify(struct notifier_block *self,
  2563. unsigned long action, void *data)
  2564. {
  2565. struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
  2566. event_notifier);
  2567. if (!swrm || !(swrm->dev)) {
  2568. pr_err_ratelimited("%s: swrm or dev is NULL\n", __func__);
  2569. return -EINVAL;
  2570. }
  2571. switch (action) {
  2572. case MSM_AUD_DC_EVENT:
  2573. schedule_work(&(swrm->dc_presence_work));
  2574. break;
  2575. case SWR_WAKE_IRQ_EVENT:
  2576. if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
  2577. swrm->ipc_wakeup_triggered = true;
  2578. pm_stay_awake(swrm->dev);
  2579. schedule_work(&swrm->wakeup_work);
  2580. }
  2581. break;
  2582. default:
  2583. dev_err_ratelimited(swrm->dev, "%s: invalid event type: %lu\n",
  2584. __func__, action);
  2585. return -EINVAL;
  2586. }
  2587. return 0;
  2588. }
  2589. static void swrm_notify_work_fn(struct work_struct *work)
  2590. {
  2591. struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
  2592. dc_presence_work);
  2593. if (!swrm || !swrm->pdev) {
  2594. pr_err_ratelimited("%s: swrm or pdev is NULL\n", __func__);
  2595. return;
  2596. }
  2597. swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
  2598. }
  2599. static int swrm_probe(struct platform_device *pdev)
  2600. {
  2601. struct swr_mstr_ctrl *swrm;
  2602. struct swr_ctrl_platform_data *pdata;
  2603. u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
  2604. u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
  2605. int ret = 0;
  2606. struct clk *lpass_core_hw_vote = NULL;
  2607. struct clk *lpass_core_audio = NULL;
  2608. u32 swrm_hw_ver = 0;
  2609. /* Allocate soundwire master driver structure */
  2610. swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
  2611. GFP_KERNEL);
  2612. if (!swrm) {
  2613. ret = -ENOMEM;
  2614. goto err_memory_fail;
  2615. }
  2616. swrm->pdev = pdev;
  2617. swrm->dev = &pdev->dev;
  2618. platform_set_drvdata(pdev, swrm);
  2619. swr_set_ctrl_data(&swrm->master, swrm);
  2620. pdata = dev_get_platdata(&pdev->dev);
  2621. if (!pdata) {
  2622. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  2623. __func__);
  2624. ret = -EINVAL;
  2625. goto err_pdata_fail;
  2626. }
  2627. swrm->handle = (void *)pdata->handle;
  2628. if (!swrm->handle) {
  2629. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  2630. __func__);
  2631. ret = -EINVAL;
  2632. goto err_pdata_fail;
  2633. }
  2634. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-master-ee-val",
  2635. &swrm->ee_val);
  2636. if (ret) {
  2637. dev_dbg(&pdev->dev,
  2638. "%s: ee_val not specified, initialize with default val\n",
  2639. __func__);
  2640. swrm->ee_val = 0x1;
  2641. }
  2642. ret = of_property_read_u32(pdev->dev.of_node,
  2643. "qcom,swr-master-version",
  2644. &swrm->version);
  2645. if (ret) {
  2646. dev_dbg(&pdev->dev, "%s: swrm version not defined, use default\n",
  2647. __func__);
  2648. swrm->version = SWRM_VERSION_2_0;
  2649. }
  2650. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
  2651. &swrm->master_id);
  2652. if (ret) {
  2653. dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
  2654. goto err_pdata_fail;
  2655. }
  2656. ret = of_property_read_u32(pdev->dev.of_node, "qcom,dynamic-port-map-supported",
  2657. &swrm->dynamic_port_map_supported);
  2658. if (ret) {
  2659. dev_dbg(&pdev->dev,
  2660. "%s: failed to get dynamic port map support, use default\n",
  2661. __func__);
  2662. swrm->dynamic_port_map_supported = 1;
  2663. }
  2664. if (!(of_property_read_u32(pdev->dev.of_node,
  2665. "swrm-io-base", &swrm->swrm_base_reg)))
  2666. ret = of_property_read_u32(pdev->dev.of_node,
  2667. "swrm-io-base", &swrm->swrm_base_reg);
  2668. if (!swrm->swrm_base_reg) {
  2669. swrm->read = pdata->read;
  2670. if (!swrm->read) {
  2671. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  2672. __func__);
  2673. ret = -EINVAL;
  2674. goto err_pdata_fail;
  2675. }
  2676. swrm->write = pdata->write;
  2677. if (!swrm->write) {
  2678. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  2679. __func__);
  2680. ret = -EINVAL;
  2681. goto err_pdata_fail;
  2682. }
  2683. swrm->bulk_write = pdata->bulk_write;
  2684. if (!swrm->bulk_write) {
  2685. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  2686. __func__);
  2687. ret = -EINVAL;
  2688. goto err_pdata_fail;
  2689. }
  2690. } else {
  2691. swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
  2692. swrm->swrm_base_reg, SWRM_MAX_REGISTER);
  2693. }
  2694. swrm->core_vote = pdata->core_vote;
  2695. if (!(of_property_read_u32(pdev->dev.of_node,
  2696. "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
  2697. swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
  2698. swrm_hctl_reg, 0x4);
  2699. swrm->clk = pdata->clk;
  2700. if (!swrm->clk) {
  2701. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  2702. __func__);
  2703. ret = -EINVAL;
  2704. goto err_pdata_fail;
  2705. }
  2706. if (of_property_read_u32(pdev->dev.of_node,
  2707. "qcom,swr-clock-stop-mode0",
  2708. &swrm->clk_stop_mode0_supp)) {
  2709. swrm->clk_stop_mode0_supp = FALSE;
  2710. }
  2711. /* Parse soundwire port mapping */
  2712. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
  2713. &num_ports);
  2714. if (ret) {
  2715. dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
  2716. goto err_pdata_fail;
  2717. }
  2718. swrm->num_ports = num_ports;
  2719. if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
  2720. &map_size)) {
  2721. dev_err(swrm->dev, "missing port mapping\n");
  2722. goto err_pdata_fail;
  2723. }
  2724. swrm->pcm_enable_count = 0;
  2725. map_length = map_size / (3 * sizeof(u32));
  2726. if (num_ports > SWR_MSTR_PORT_LEN) {
  2727. dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
  2728. __func__);
  2729. ret = -EINVAL;
  2730. goto err_pdata_fail;
  2731. }
  2732. temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
  2733. if (!temp) {
  2734. ret = -ENOMEM;
  2735. goto err_pdata_fail;
  2736. }
  2737. ret = of_property_read_u32_array(pdev->dev.of_node,
  2738. "qcom,swr-port-mapping", temp, 3 * map_length);
  2739. if (ret) {
  2740. dev_err(swrm->dev, "%s: Failed to read port mapping\n",
  2741. __func__);
  2742. goto err_pdata_fail;
  2743. }
  2744. for (i = 0; i < map_length; i++) {
  2745. port_num = temp[3 * i];
  2746. port_type = temp[3 * i + 1];
  2747. ch_mask = temp[3 * i + 2];
  2748. if (port_num != old_port_num)
  2749. ch_iter = 0;
  2750. if (port_num > SWR_MSTR_PORT_LEN ||
  2751. ch_iter >= SWR_MAX_CH_PER_PORT) {
  2752. dev_err(&pdev->dev,
  2753. "%s:invalid port_num %d or ch_iter %d\n",
  2754. __func__, port_num, ch_iter);
  2755. goto err_pdata_fail;
  2756. }
  2757. swrm->port_mapping[port_num][ch_iter].port_type = port_type;
  2758. if (swrm->master_id == MASTER_ID_BT)
  2759. swrm->port_mapping[port_num][ch_iter++].ch_mask = 1;
  2760. else
  2761. swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
  2762. old_port_num = port_num;
  2763. }
  2764. devm_kfree(&pdev->dev, temp);
  2765. ret = of_property_read_u32(pdev->dev.of_node, "qcom,is-always-on",
  2766. &swrm->is_always_on);
  2767. if (ret)
  2768. dev_dbg(&pdev->dev, "%s: failed to get is_always_on flag\n", __func__);
  2769. swrm->reg_irq = pdata->reg_irq;
  2770. swrm->master.read = swrm_read;
  2771. swrm->master.write = swrm_write;
  2772. swrm->master.bulk_write = swrm_bulk_write;
  2773. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  2774. swrm->master.init_port_params = swrm_init_port_params;
  2775. swrm->master.connect_port = swrm_connect_port;
  2776. swrm->master.disconnect_port = swrm_disconnect_port;
  2777. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  2778. swrm->master.remove_from_group = swrm_remove_from_group;
  2779. swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
  2780. swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
  2781. swrm->master.dev.parent = &pdev->dev;
  2782. swrm->master.dev.of_node = pdev->dev.of_node;
  2783. swrm->master.num_port = 0;
  2784. swrm->rcmd_id = 0;
  2785. swrm->wcmd_id = 0;
  2786. swrm->cmd_id = 0;
  2787. swrm->slave_status = 0;
  2788. swrm->num_rx_chs = 0;
  2789. swrm->clk_ref_count = 0;
  2790. swrm->swr_irq_wakeup_capable = 0;
  2791. swrm->mclk_freq = MCLK_FREQ;
  2792. swrm->bus_clk = MCLK_FREQ;
  2793. if (swrm->master_id == MASTER_ID_BT) {
  2794. swrm->mclk_freq = MCLK_FREQ_12288;
  2795. swrm->bus_clk = MCLK_FREQ_12288;
  2796. }
  2797. swrm->dev_up = true;
  2798. swrm->state = SWR_MSTR_UP;
  2799. swrm->ipc_wakeup = false;
  2800. swrm->enable_slave_irq = false;
  2801. swrm->clk_stop_wakeup = false;
  2802. swrm->ipc_wakeup_triggered = false;
  2803. swrm->disable_div2_clk_switch = FALSE;
  2804. init_completion(&swrm->reset);
  2805. init_completion(&swrm->broadcast);
  2806. init_completion(&swrm->clk_off_complete);
  2807. mutex_init(&swrm->irq_lock);
  2808. mutex_init(&swrm->mlock);
  2809. mutex_init(&swrm->reslock);
  2810. mutex_init(&swrm->force_down_lock);
  2811. mutex_init(&swrm->iolock);
  2812. mutex_init(&swrm->clklock);
  2813. mutex_init(&swrm->devlock);
  2814. mutex_init(&swrm->pm_lock);
  2815. mutex_init(&swrm->runtime_lock);
  2816. swrm->wlock_holders = 0;
  2817. swrm->pm_state = SWRM_PM_SLEEPABLE;
  2818. init_waitqueue_head(&swrm->pm_wq);
  2819. cpu_latency_qos_add_request(&swrm->pm_qos_req,
  2820. PM_QOS_DEFAULT_VALUE);
  2821. for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++) {
  2822. INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
  2823. if (swrm->master_id == MASTER_ID_TX || swrm->master_id == MASTER_ID_BT) {
  2824. swrm->mport_cfg[i].sinterval = 0xFFFF;
  2825. if (swrm->master_id == MASTER_ID_BT && i > 3)
  2826. swrm->mport_cfg[i].offset1 = 0x14;
  2827. else
  2828. swrm->mport_cfg[i].offset1 = 0x00;
  2829. swrm->mport_cfg[i].offset2 = 0x00;
  2830. swrm->mport_cfg[i].hstart = 0xFF;
  2831. swrm->mport_cfg[i].hstop = 0xFF;
  2832. swrm->mport_cfg[i].blk_pack_mode = 0xFF;
  2833. swrm->mport_cfg[i].blk_grp_count = 0xFF;
  2834. swrm->mport_cfg[i].word_length = 0xFF;
  2835. swrm->mport_cfg[i].lane_ctrl = 0x00;
  2836. if (swrm->master_id == MASTER_ID_BT && i > 3)
  2837. swrm->mport_cfg[i].dir = 0x01;
  2838. else
  2839. swrm->mport_cfg[i].dir = 0x00;
  2840. swrm->mport_cfg[i].stream_type =
  2841. (swrm->master_id == MASTER_ID_TX) ? 0x00 : 0x01;
  2842. }
  2843. }
  2844. if (of_property_read_u32(pdev->dev.of_node,
  2845. "qcom,disable-div2-clk-switch",
  2846. &swrm->disable_div2_clk_switch)) {
  2847. swrm->disable_div2_clk_switch = FALSE;
  2848. }
  2849. /* Register LPASS core hw vote */
  2850. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  2851. if (IS_ERR(lpass_core_hw_vote)) {
  2852. ret = PTR_ERR(lpass_core_hw_vote);
  2853. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  2854. __func__, "lpass_core_hw_vote", ret);
  2855. lpass_core_hw_vote = NULL;
  2856. ret = 0;
  2857. }
  2858. swrm->lpass_core_hw_vote = lpass_core_hw_vote;
  2859. /* Register LPASS audio core vote */
  2860. lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  2861. if (IS_ERR(lpass_core_audio)) {
  2862. ret = PTR_ERR(lpass_core_audio);
  2863. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  2864. __func__, "lpass_core_audio", ret);
  2865. lpass_core_audio = NULL;
  2866. ret = 0;
  2867. }
  2868. swrm->lpass_core_audio = lpass_core_audio;
  2869. if (swrm->reg_irq) {
  2870. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  2871. SWR_IRQ_REGISTER);
  2872. if (ret) {
  2873. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  2874. __func__, ret);
  2875. goto err_irq_fail;
  2876. }
  2877. } else {
  2878. swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
  2879. if (swrm->irq < 0) {
  2880. dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
  2881. __func__, swrm->irq);
  2882. goto err_irq_fail;
  2883. }
  2884. ret = request_threaded_irq(swrm->irq, NULL,
  2885. swr_mstr_interrupt,
  2886. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  2887. "swr_master_irq", swrm);
  2888. if (ret) {
  2889. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  2890. __func__, ret);
  2891. goto err_irq_fail;
  2892. }
  2893. }
  2894. /* Make inband tx interrupts as wakeup capable for slave irq */
  2895. ret = of_property_read_u32(pdev->dev.of_node,
  2896. "qcom,swr-mstr-irq-wakeup-capable",
  2897. &swrm->swr_irq_wakeup_capable);
  2898. if (ret)
  2899. dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
  2900. __func__);
  2901. if (swrm->swr_irq_wakeup_capable) {
  2902. irq_set_irq_wake(swrm->irq, 1);
  2903. ret = device_init_wakeup(swrm->dev, true);
  2904. if (ret)
  2905. dev_info(swrm->dev,
  2906. "%s: Device wakeup init failed: %d\n",
  2907. __func__, ret);
  2908. }
  2909. ret = swr_register_master(&swrm->master);
  2910. if (ret) {
  2911. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  2912. goto err_mstr_fail;
  2913. }
  2914. /* Add devices registered with board-info as the
  2915. * controller will be up now
  2916. */
  2917. swr_master_add_boarddevices(&swrm->master);
  2918. if (!swrm->is_always_on && swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  2919. dev_dbg(&pdev->dev, "%s: Audio HW Vote is failed\n", __func__);
  2920. mutex_lock(&swrm->mlock);
  2921. swrm_clk_request(swrm, true);
  2922. swrm->rd_fifo_depth = ((swr_master_read(swrm, SWRM_COMP_PARAMS)
  2923. & SWRM_COMP_PARAMS_RD_FIFO_DEPTH) >> 15);
  2924. swrm->wr_fifo_depth = ((swr_master_read(swrm, SWRM_COMP_PARAMS)
  2925. & SWRM_COMP_PARAMS_WR_FIFO_DEPTH) >> 10);
  2926. swrm_hw_ver = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
  2927. if (swrm->version != swrm_hw_ver) {
  2928. dev_info(&pdev->dev,
  2929. "%s: version specified in dtsi: 0x%x not match with HW read version 0x%x\n",
  2930. __func__, swrm->version, swrm_hw_ver);
  2931. swrm->version = swrm_hw_ver;
  2932. }
  2933. swrm->num_auto_enum = ((swr_master_read(swrm, SWRM_COMP_PARAMS)
  2934. & SWRM_COMP_PARAMS_AUTO_ENUM_SLAVES) >> 20);
  2935. ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
  2936. &swrm->num_dev);
  2937. if (ret) {
  2938. dev_err(&pdev->dev, "%s: Looking up %s property failed\n",
  2939. __func__, "qcom,swr-num-dev");
  2940. mutex_unlock(&swrm->mlock);
  2941. goto err_parse_num_dev;
  2942. } else {
  2943. if (swrm->num_dev > swrm->num_auto_enum) {
  2944. dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
  2945. __func__, swrm->num_dev,
  2946. swrm->num_auto_enum);
  2947. ret = -EINVAL;
  2948. mutex_unlock(&swrm->mlock);
  2949. goto err_parse_num_dev;
  2950. } else {
  2951. dev_dbg(&pdev->dev,
  2952. "max swr devices expected to attach - %d, supported auto_enum - %d\n",
  2953. swrm->num_dev, swrm->num_auto_enum);
  2954. }
  2955. }
  2956. ret = swrm_master_init(swrm);
  2957. if (ret < 0) {
  2958. dev_err(&pdev->dev,
  2959. "%s: Error in master Initialization , err %d\n",
  2960. __func__, ret);
  2961. mutex_unlock(&swrm->mlock);
  2962. ret = -EPROBE_DEFER;
  2963. goto err_mstr_init_fail;
  2964. }
  2965. mutex_unlock(&swrm->mlock);
  2966. INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
  2967. if (pdev->dev.of_node)
  2968. of_register_swr_devices(&swrm->master);
  2969. #ifdef CONFIG_DEBUG_FS
  2970. swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  2971. if (!IS_ERR(swrm->debugfs_swrm_dent)) {
  2972. swrm->debugfs_peek = debugfs_create_file("swrm_peek",
  2973. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2974. (void *) swrm, &swrm_debug_read_ops);
  2975. swrm->debugfs_poke = debugfs_create_file("swrm_poke",
  2976. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2977. (void *) swrm, &swrm_debug_write_ops);
  2978. swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  2979. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2980. (void *) swrm,
  2981. &swrm_debug_dump_ops);
  2982. }
  2983. #endif
  2984. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  2985. pm_runtime_use_autosuspend(&pdev->dev);
  2986. pm_runtime_set_active(&pdev->dev);
  2987. pm_runtime_enable(&pdev->dev);
  2988. pm_runtime_mark_last_busy(&pdev->dev);
  2989. INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
  2990. swrm->event_notifier.notifier_call = swrm_event_notify;
  2991. //msm_aud_evt_register_client(&swrm->event_notifier);
  2992. return 0;
  2993. err_parse_num_dev:
  2994. err_mstr_init_fail:
  2995. swr_unregister_master(&swrm->master);
  2996. device_init_wakeup(swrm->dev, false);
  2997. err_mstr_fail:
  2998. if (swrm->reg_irq) {
  2999. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  3000. swrm, SWR_IRQ_FREE);
  3001. } else if (swrm->irq) {
  3002. if (irq_get_irq_data(swrm->irq) != NULL)
  3003. irqd_set_trigger_type(
  3004. irq_get_irq_data(swrm->irq),
  3005. IRQ_TYPE_NONE);
  3006. if (swrm->swr_irq_wakeup_capable)
  3007. irq_set_irq_wake(swrm->irq, 0);
  3008. free_irq(swrm->irq, swrm);
  3009. }
  3010. err_irq_fail:
  3011. mutex_destroy(&swrm->irq_lock);
  3012. mutex_destroy(&swrm->mlock);
  3013. mutex_destroy(&swrm->reslock);
  3014. mutex_destroy(&swrm->force_down_lock);
  3015. mutex_destroy(&swrm->iolock);
  3016. mutex_destroy(&swrm->clklock);
  3017. mutex_destroy(&swrm->pm_lock);
  3018. mutex_destroy(&swrm->runtime_lock);
  3019. cpu_latency_qos_remove_request(&swrm->pm_qos_req);
  3020. err_pdata_fail:
  3021. err_memory_fail:
  3022. return ret;
  3023. }
  3024. static int swrm_remove(struct platform_device *pdev)
  3025. {
  3026. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3027. if (swrm->reg_irq) {
  3028. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  3029. swrm, SWR_IRQ_FREE);
  3030. } else if (swrm->irq) {
  3031. if (irq_get_irq_data(swrm->irq) != NULL)
  3032. irqd_set_trigger_type(
  3033. irq_get_irq_data(swrm->irq),
  3034. IRQ_TYPE_NONE);
  3035. if (swrm->swr_irq_wakeup_capable) {
  3036. irq_set_irq_wake(swrm->irq, 0);
  3037. device_init_wakeup(swrm->dev, false);
  3038. }
  3039. free_irq(swrm->irq, swrm);
  3040. } else if (swrm->wake_irq > 0) {
  3041. free_irq(swrm->wake_irq, swrm);
  3042. }
  3043. cancel_work_sync(&swrm->wakeup_work);
  3044. pm_runtime_disable(&pdev->dev);
  3045. pm_runtime_set_suspended(&pdev->dev);
  3046. swr_unregister_master(&swrm->master);
  3047. //msm_aud_evt_unregister_client(&swrm->event_notifier);
  3048. mutex_destroy(&swrm->irq_lock);
  3049. mutex_destroy(&swrm->mlock);
  3050. mutex_destroy(&swrm->reslock);
  3051. mutex_destroy(&swrm->iolock);
  3052. mutex_destroy(&swrm->clklock);
  3053. mutex_destroy(&swrm->force_down_lock);
  3054. mutex_destroy(&swrm->pm_lock);
  3055. mutex_destroy(&swrm->runtime_lock);
  3056. cpu_latency_qos_remove_request(&swrm->pm_qos_req);
  3057. devm_kfree(&pdev->dev, swrm);
  3058. return 0;
  3059. }
  3060. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  3061. {
  3062. u32 val;
  3063. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  3064. swr_master_write(swrm, SWRM_INTERRUPT_EN(swrm->ee_val),
  3065. SWRM_INTERRUPT_STATUS_MASK);
  3066. val = swr_master_read(swrm, SWRM_MCP_CFG);
  3067. val |= 0x02;
  3068. swr_master_write(swrm, SWRM_MCP_CFG, val);
  3069. return 0;
  3070. }
  3071. #ifdef CONFIG_PM
  3072. static int swrm_runtime_resume(struct device *dev)
  3073. {
  3074. struct platform_device *pdev = to_platform_device(dev);
  3075. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3076. int ret = 0, val = 0;
  3077. bool swrm_clk_req_err = false;
  3078. bool hw_core_err = false, aud_core_err = false;
  3079. struct swr_master *mstr = &swrm->master;
  3080. struct swr_device *swr_dev;
  3081. u32 temp = 0;
  3082. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  3083. __func__, swrm->state);
  3084. mutex_lock(&swrm->runtime_lock);
  3085. mutex_lock(&swrm->reslock);
  3086. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  3087. dev_err_ratelimited(dev, "%s:lpass core hw enable failed\n",
  3088. __func__);
  3089. hw_core_err = true;
  3090. pm_runtime_set_autosuspend_delay(&pdev->dev,
  3091. ERR_AUTO_SUSPEND_TIMER_VAL);
  3092. if (swrm->req_clk_switch)
  3093. swrm->req_clk_switch = false;
  3094. mutex_unlock(&swrm->reslock);
  3095. mutex_unlock(&swrm->runtime_lock);
  3096. return 0;
  3097. }
  3098. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
  3099. dev_err_ratelimited(dev, "%s:lpass audio hw enable failed\n",
  3100. __func__);
  3101. aud_core_err = true;
  3102. }
  3103. if ((swrm->state == SWR_MSTR_DOWN) ||
  3104. (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
  3105. if (swrm->clk_stop_mode0_supp) {
  3106. if (swrm->wake_irq > 0) {
  3107. if (unlikely(!irq_get_irq_data
  3108. (swrm->wake_irq))) {
  3109. pr_err_ratelimited("%s: irq data is NULL\n",
  3110. __func__);
  3111. mutex_unlock(&swrm->reslock);
  3112. mutex_unlock(&swrm->runtime_lock);
  3113. return IRQ_NONE;
  3114. }
  3115. mutex_lock(&swrm->irq_lock);
  3116. if (!irqd_irq_disabled(irq_get_irq_data(swrm->wake_irq))) {
  3117. irq_set_irq_wake(swrm->wake_irq, 0);
  3118. disable_irq_nosync(swrm->wake_irq);
  3119. }
  3120. mutex_unlock(&swrm->irq_lock);
  3121. }
  3122. if (swrm->ipc_wakeup)
  3123. dev_err_ratelimited(dev, "%s:notifications disabled\n", __func__);
  3124. // msm_aud_evt_blocking_notifier_call_chain(
  3125. // SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  3126. }
  3127. if (swrm_clk_request(swrm, true)) {
  3128. /*
  3129. * Set autosuspend timer to 1 for
  3130. * master to enter into suspend.
  3131. */
  3132. swrm_clk_req_err = true;
  3133. goto exit;
  3134. }
  3135. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  3136. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  3137. ret = swr_device_up(swr_dev);
  3138. if (ret == -ENODEV) {
  3139. dev_dbg(dev,
  3140. "%s slave device up not implemented\n",
  3141. __func__);
  3142. ret = 0;
  3143. } else if (ret) {
  3144. dev_err_ratelimited(dev,
  3145. "%s: failed to wakeup swr dev %d\n",
  3146. __func__, swr_dev->dev_num);
  3147. swrm_clk_request(swrm, false);
  3148. goto exit;
  3149. }
  3150. }
  3151. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  3152. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  3153. swr_master_write(swrm, SWRM_MCP_BUS_CTRL, 0x01);
  3154. swrm_master_init(swrm);
  3155. /* wait for hw enumeration to complete */
  3156. usleep_range(100, 105);
  3157. if (!swrm_check_link_status(swrm, 0x1))
  3158. dev_dbg(dev, "%s:failed in connecting, ssr?\n",
  3159. __func__);
  3160. swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, get_cmd_id(swrm),
  3161. SWRS_SCP_INT_STATUS_MASK_1);
  3162. if (swrm->state == SWR_MSTR_SSR) {
  3163. mutex_unlock(&swrm->reslock);
  3164. enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
  3165. mutex_lock(&swrm->reslock);
  3166. }
  3167. } else {
  3168. if (swrm->swrm_hctl_reg) {
  3169. temp = ioread32(swrm->swrm_hctl_reg);
  3170. temp &= 0xFFFFFFFD;
  3171. iowrite32(temp, swrm->swrm_hctl_reg);
  3172. }
  3173. /*wake up from clock stop*/
  3174. #ifdef CONFIG_SWRM_VER_2P0
  3175. val = 0x01;
  3176. swr_master_write(swrm,
  3177. SWRM_CLK_CTRL(swrm->ee_val), val);
  3178. #else
  3179. if (swrm->version < SWRM_VERSION_1_7)
  3180. val = 0x2;
  3181. else
  3182. val = 0x2 << swrm->ee_val;
  3183. swr_master_write(swrm, SWRM_MCP_BUS_CTRL, val);
  3184. #endif
  3185. /* clear and enable bus clash interrupt */
  3186. swr_master_write(swrm,
  3187. SWRM_INTERRUPT_CLEAR(swrm->ee_val), 0x08);
  3188. swrm->intr_mask |= 0x08;
  3189. swr_master_write(swrm, SWRM_INTERRUPT_EN(swrm->ee_val),
  3190. swrm->intr_mask);
  3191. usleep_range(100, 105);
  3192. if (!swrm_check_link_status(swrm, 0x1))
  3193. dev_dbg(dev, "%s:failed in connecting, ssr?\n",
  3194. __func__);
  3195. }
  3196. swrm->state = SWR_MSTR_UP;
  3197. }
  3198. exit:
  3199. if (swrm->is_always_on && !aud_core_err)
  3200. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  3201. if (!hw_core_err)
  3202. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  3203. if (swrm_clk_req_err || aud_core_err || hw_core_err)
  3204. pm_runtime_set_autosuspend_delay(&pdev->dev,
  3205. ERR_AUTO_SUSPEND_TIMER_VAL);
  3206. else
  3207. pm_runtime_set_autosuspend_delay(&pdev->dev,
  3208. auto_suspend_timer);
  3209. if (swrm->req_clk_switch)
  3210. swrm->req_clk_switch = false;
  3211. mutex_unlock(&swrm->reslock);
  3212. mutex_unlock(&swrm->runtime_lock);
  3213. return ret;
  3214. }
  3215. static int swrm_runtime_suspend(struct device *dev)
  3216. {
  3217. struct platform_device *pdev = to_platform_device(dev);
  3218. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3219. int ret = 0;
  3220. bool hw_core_err = false, aud_core_err = false;
  3221. struct swr_master *mstr = &swrm->master;
  3222. struct swr_device *swr_dev;
  3223. int current_state = 0;
  3224. struct irq_data *irq_data = NULL;
  3225. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  3226. __func__, swrm->state);
  3227. if (swrm->state == SWR_MSTR_SSR_RESET) {
  3228. swrm->state = SWR_MSTR_SSR;
  3229. return 0;
  3230. }
  3231. mutex_lock(&swrm->runtime_lock);
  3232. mutex_lock(&swrm->reslock);
  3233. mutex_lock(&swrm->force_down_lock);
  3234. current_state = swrm->state;
  3235. mutex_unlock(&swrm->force_down_lock);
  3236. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  3237. dev_err_ratelimited(dev, "%s:lpass core hw enable failed\n",
  3238. __func__);
  3239. hw_core_err = true;
  3240. }
  3241. if (swrm->is_always_on && swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  3242. aud_core_err = true;
  3243. if ((current_state == SWR_MSTR_UP) ||
  3244. (current_state == SWR_MSTR_SSR)) {
  3245. if ((current_state != SWR_MSTR_SSR) &&
  3246. swrm_is_port_en(&swrm->master)) {
  3247. dev_dbg(dev, "%s ports are enabled\n", __func__);
  3248. ret = -EBUSY;
  3249. goto exit;
  3250. }
  3251. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  3252. dev_err_ratelimited(dev, "%s: clk stop mode not supported or SSR entry\n",
  3253. __func__);
  3254. if (swrm->state == SWR_MSTR_SSR)
  3255. goto chk_lnk_status;
  3256. mutex_unlock(&swrm->reslock);
  3257. enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
  3258. mutex_lock(&swrm->reslock);
  3259. swrm_clk_pause(swrm);
  3260. swr_master_write(swrm, SWRM_COMP_CFG, 0x00);
  3261. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  3262. ret = swr_device_down(swr_dev);
  3263. if (ret == -ENODEV) {
  3264. dev_dbg_ratelimited(dev,
  3265. "%s slave device down not implemented\n",
  3266. __func__);
  3267. ret = 0;
  3268. } else if (ret) {
  3269. dev_err_ratelimited(dev,
  3270. "%s: failed to shutdown swr dev %d\n",
  3271. __func__, swr_dev->dev_num);
  3272. goto exit;
  3273. }
  3274. }
  3275. } else {
  3276. /* Mask bus clash interrupt */
  3277. swrm->intr_mask &= ~((u32)0x08);
  3278. swr_master_write(swrm, SWRM_INTERRUPT_EN(swrm->ee_val),
  3279. swrm->intr_mask);
  3280. mutex_unlock(&swrm->reslock);
  3281. /* clock stop sequence */
  3282. swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
  3283. SWRS_SCP_CONTROL);
  3284. mutex_lock(&swrm->reslock);
  3285. usleep_range(100, 105);
  3286. }
  3287. chk_lnk_status:
  3288. if (!swrm_check_link_status(swrm, 0x0))
  3289. dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
  3290. __func__);
  3291. ret = swrm_clk_request(swrm, false);
  3292. if (ret) {
  3293. dev_err_ratelimited(dev, "%s: swrmn clk failed\n", __func__);
  3294. ret = 0;
  3295. goto exit;
  3296. }
  3297. if (swrm->clk_stop_mode0_supp) {
  3298. if (swrm->wake_irq > 0) {
  3299. irq_data = irq_get_irq_data(swrm->wake_irq);
  3300. mutex_lock(&swrm->irq_lock);
  3301. if (irq_data && irqd_irq_disabled(irq_data)) {
  3302. irq_set_irq_wake(swrm->wake_irq, 1);
  3303. enable_irq(swrm->wake_irq);
  3304. }
  3305. mutex_unlock(&swrm->irq_lock);
  3306. } else if (swrm->ipc_wakeup) {
  3307. //msm_aud_evt_blocking_notifier_call_chain(
  3308. // SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  3309. dev_err_ratelimited(dev, "%s:notifications disabled\n", __func__);
  3310. swrm->ipc_wakeup_triggered = false;
  3311. }
  3312. }
  3313. }
  3314. /* Retain SSR state until resume */
  3315. if (current_state != SWR_MSTR_SSR)
  3316. swrm->state = SWR_MSTR_DOWN;
  3317. exit:
  3318. if (!swrm->is_always_on && swrm->state != SWR_MSTR_UP) {
  3319. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false))
  3320. dev_dbg(dev, "%s:lpass audio hw enable failed\n",
  3321. __func__);
  3322. } else if (swrm->is_always_on && !aud_core_err)
  3323. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  3324. if (!hw_core_err)
  3325. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  3326. mutex_unlock(&swrm->reslock);
  3327. mutex_unlock(&swrm->runtime_lock);
  3328. dev_dbg(dev, "%s: pm_runtime: suspend done state: %d\n",
  3329. __func__, swrm->state);
  3330. pm_runtime_set_autosuspend_delay(dev, auto_suspend_timer);
  3331. return ret;
  3332. }
  3333. #endif /* CONFIG_PM */
  3334. static int swrm_device_suspend(struct device *dev)
  3335. {
  3336. struct platform_device *pdev = to_platform_device(dev);
  3337. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3338. int ret = 0;
  3339. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  3340. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  3341. ret = swrm_runtime_suspend(dev);
  3342. if (!ret) {
  3343. pm_runtime_disable(dev);
  3344. pm_runtime_set_suspended(dev);
  3345. pm_runtime_enable(dev);
  3346. }
  3347. }
  3348. return 0;
  3349. }
  3350. static int swrm_device_down(struct device *dev)
  3351. {
  3352. struct platform_device *pdev = to_platform_device(dev);
  3353. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3354. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  3355. mutex_lock(&swrm->force_down_lock);
  3356. swrm->state = SWR_MSTR_SSR;
  3357. mutex_unlock(&swrm->force_down_lock);
  3358. swrm_device_suspend(dev);
  3359. return 0;
  3360. }
  3361. int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
  3362. {
  3363. int ret = 0;
  3364. int irq, dir_apps_irq;
  3365. if (!swrm->ipc_wakeup) {
  3366. irq = of_get_named_gpio(swrm->dev->of_node,
  3367. "qcom,swr-wakeup-irq", 0);
  3368. if (gpio_is_valid(irq)) {
  3369. swrm->wake_irq = gpio_to_irq(irq);
  3370. if (swrm->wake_irq < 0) {
  3371. dev_err_ratelimited(swrm->dev,
  3372. "Unable to configure irq\n");
  3373. return swrm->wake_irq;
  3374. }
  3375. } else {
  3376. dir_apps_irq = platform_get_irq_byname(swrm->pdev,
  3377. "swr_wake_irq");
  3378. if (dir_apps_irq < 0) {
  3379. dev_err_ratelimited(swrm->dev,
  3380. "TLMM connect gpio not found\n");
  3381. return -EINVAL;
  3382. }
  3383. swrm->wake_irq = dir_apps_irq;
  3384. }
  3385. mutex_lock(&swrm->irq_lock);
  3386. ret = request_threaded_irq(swrm->wake_irq, NULL,
  3387. swrm_wakeup_interrupt,
  3388. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  3389. "swr_wake_irq", swrm);
  3390. if (ret) {
  3391. dev_err_ratelimited(swrm->dev, "%s: Failed to request irq %d\n",
  3392. __func__, ret);
  3393. mutex_unlock(&swrm->irq_lock);
  3394. return -EINVAL;
  3395. }
  3396. irq_set_irq_wake(swrm->wake_irq, 1);
  3397. mutex_unlock(&swrm->irq_lock);
  3398. }
  3399. return ret;
  3400. }
  3401. static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
  3402. u32 uc, u32 size)
  3403. {
  3404. if (!swrm->port_param) {
  3405. swrm->port_param = devm_kzalloc(dev,
  3406. sizeof(swrm->port_param) * SWR_UC_MAX,
  3407. GFP_KERNEL);
  3408. if (!swrm->port_param)
  3409. return -ENOMEM;
  3410. }
  3411. if (!swrm->port_param[uc]) {
  3412. swrm->port_param[uc] = devm_kcalloc(dev, size,
  3413. sizeof(struct port_params),
  3414. GFP_KERNEL);
  3415. if (!swrm->port_param[uc])
  3416. return -ENOMEM;
  3417. } else {
  3418. dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
  3419. __func__);
  3420. }
  3421. return 0;
  3422. }
  3423. static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
  3424. struct swrm_port_config *port_cfg,
  3425. u32 size)
  3426. {
  3427. int idx;
  3428. struct port_params *params;
  3429. int uc = port_cfg->uc;
  3430. int ret = 0;
  3431. for (idx = 0; idx < size; idx++) {
  3432. params = &((struct port_params *)port_cfg->params)[idx];
  3433. if (!params) {
  3434. dev_err_ratelimited(swrm->dev, "%s: Invalid params\n", __func__);
  3435. ret = -EINVAL;
  3436. break;
  3437. }
  3438. memcpy(&swrm->port_param[uc][idx], params,
  3439. sizeof(struct port_params));
  3440. }
  3441. return ret;
  3442. }
  3443. /**
  3444. * swrm_wcd_notify - parent device can notify to soundwire master through
  3445. * this function
  3446. * @pdev: pointer to platform device structure
  3447. * @id: command id from parent to the soundwire master
  3448. * @data: data from parent device to soundwire master
  3449. */
  3450. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  3451. {
  3452. struct swr_mstr_ctrl *swrm;
  3453. int ret = 0;
  3454. struct swr_master *mstr;
  3455. struct swr_device *swr_dev;
  3456. struct swrm_port_config *port_cfg;
  3457. if (!pdev) {
  3458. pr_err_ratelimited("%s: pdev is NULL\n", __func__);
  3459. return -EINVAL;
  3460. }
  3461. swrm = platform_get_drvdata(pdev);
  3462. if (!swrm) {
  3463. dev_err_ratelimited(&pdev->dev, "%s: swrm is NULL\n", __func__);
  3464. return -EINVAL;
  3465. }
  3466. mstr = &swrm->master;
  3467. switch (id) {
  3468. case SWR_REQ_CLK_SWITCH:
  3469. /* This will put soundwire in clock stop mode and disable the
  3470. * clocks, if there is no active usecase running, so that the
  3471. * next activity on soundwire will request clock from new clock
  3472. * source.
  3473. */
  3474. if (!data) {
  3475. dev_err_ratelimited(swrm->dev, "%s: data is NULL for id:%d\n",
  3476. __func__, id);
  3477. ret = -EINVAL;
  3478. break;
  3479. }
  3480. mutex_lock(&swrm->mlock);
  3481. if (swrm->clk_src != *(int *)data) {
  3482. if (swrm->state == SWR_MSTR_UP) {
  3483. swrm->req_clk_switch = true;
  3484. swrm_device_suspend(&pdev->dev);
  3485. if (swrm->state == SWR_MSTR_UP)
  3486. swrm->req_clk_switch = false;
  3487. }
  3488. swrm->clk_src = *(int *)data;
  3489. }
  3490. mutex_unlock(&swrm->mlock);
  3491. break;
  3492. case SWR_CLK_FREQ:
  3493. if (!data) {
  3494. dev_err_ratelimited(swrm->dev, "%s: data is NULL\n", __func__);
  3495. ret = -EINVAL;
  3496. } else {
  3497. mutex_lock(&swrm->mlock);
  3498. if (swrm->mclk_freq != *(int *)data) {
  3499. dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
  3500. if (swrm->state == SWR_MSTR_DOWN)
  3501. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  3502. __func__, swrm->state);
  3503. else {
  3504. swrm->mclk_freq = *(int *)data;
  3505. swrm->bus_clk = swrm->mclk_freq;
  3506. swrm_switch_frame_shape(swrm,
  3507. swrm->bus_clk);
  3508. swrm_device_suspend(&pdev->dev);
  3509. }
  3510. /*
  3511. * add delay to ensure clk release happen
  3512. * if interrupt triggered for clk stop,
  3513. * wait for it to exit
  3514. */
  3515. usleep_range(10000, 10500);
  3516. }
  3517. swrm->mclk_freq = *(int *)data;
  3518. swrm->bus_clk = swrm->mclk_freq;
  3519. mutex_unlock(&swrm->mlock);
  3520. }
  3521. break;
  3522. case SWR_DEVICE_SSR_DOWN:
  3523. mutex_lock(&swrm->mlock);
  3524. mutex_lock(&swrm->devlock);
  3525. swrm->dev_up = false;
  3526. mutex_unlock(&swrm->devlock);
  3527. if (swrm->state == SWR_MSTR_DOWN)
  3528. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  3529. __func__, swrm->state);
  3530. else
  3531. swrm_device_down(&pdev->dev);
  3532. mutex_lock(&swrm->devlock);
  3533. if (swrm->hw_core_clk_en)
  3534. digital_cdc_rsc_mgr_hw_vote_disable(
  3535. swrm->lpass_core_hw_vote, swrm->dev);
  3536. swrm->hw_core_clk_en = 0;
  3537. if (swrm->aud_core_clk_en)
  3538. digital_cdc_rsc_mgr_hw_vote_disable(
  3539. swrm->lpass_core_audio, swrm->dev);
  3540. swrm->aud_core_clk_en = 0;
  3541. mutex_unlock(&swrm->devlock);
  3542. mutex_lock(&swrm->reslock);
  3543. swrm->state = SWR_MSTR_SSR;
  3544. mutex_unlock(&swrm->reslock);
  3545. mutex_unlock(&swrm->mlock);
  3546. break;
  3547. case SWR_DEVICE_SSR_UP:
  3548. /* wait for clk voting to be zero */
  3549. reinit_completion(&swrm->clk_off_complete);
  3550. if (swrm->clk_ref_count &&
  3551. !wait_for_completion_timeout(&swrm->clk_off_complete,
  3552. msecs_to_jiffies(500)))
  3553. dev_err_ratelimited(swrm->dev, "%s: clock voting not zero\n",
  3554. __func__);
  3555. if (swrm->state == SWR_MSTR_UP ||
  3556. pm_runtime_autosuspend_expiration(swrm->dev)) {
  3557. swrm->state = SWR_MSTR_SSR_RESET;
  3558. dev_dbg(swrm->dev,
  3559. "%s:suspend swr if active at SSR up\n",
  3560. __func__);
  3561. pm_runtime_set_autosuspend_delay(swrm->dev,
  3562. ERR_AUTO_SUSPEND_TIMER_VAL);
  3563. usleep_range(50000, 50100);
  3564. swrm->state = SWR_MSTR_SSR;
  3565. }
  3566. mutex_lock(&swrm->devlock);
  3567. swrm->dev_up = true;
  3568. mutex_unlock(&swrm->devlock);
  3569. break;
  3570. case SWR_DEVICE_DOWN:
  3571. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  3572. mutex_lock(&swrm->mlock);
  3573. if (swrm->state == SWR_MSTR_DOWN)
  3574. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  3575. __func__, swrm->state);
  3576. else
  3577. swrm_device_down(&pdev->dev);
  3578. mutex_unlock(&swrm->mlock);
  3579. break;
  3580. case SWR_DEVICE_UP:
  3581. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  3582. mutex_lock(&swrm->devlock);
  3583. if (!swrm->dev_up) {
  3584. dev_dbg(swrm->dev, "SSR not complete yet\n");
  3585. mutex_unlock(&swrm->devlock);
  3586. return -EBUSY;
  3587. }
  3588. mutex_unlock(&swrm->devlock);
  3589. mutex_lock(&swrm->mlock);
  3590. pm_runtime_mark_last_busy(&pdev->dev);
  3591. pm_runtime_get_sync(&pdev->dev);
  3592. mutex_lock(&swrm->reslock);
  3593. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  3594. ret = swr_reset_device(swr_dev);
  3595. if (ret == -ENODEV) {
  3596. dev_dbg_ratelimited(swrm->dev,
  3597. "%s slave reset not implemented\n",
  3598. __func__);
  3599. ret = 0;
  3600. } else if (ret) {
  3601. dev_err_ratelimited(swrm->dev,
  3602. "%s: failed to reset swr device %d\n",
  3603. __func__, swr_dev->dev_num);
  3604. swrm_clk_request(swrm, false);
  3605. }
  3606. }
  3607. pm_runtime_mark_last_busy(&pdev->dev);
  3608. pm_runtime_put_autosuspend(&pdev->dev);
  3609. mutex_unlock(&swrm->reslock);
  3610. mutex_unlock(&swrm->mlock);
  3611. break;
  3612. case SWR_SET_NUM_RX_CH:
  3613. if (!data) {
  3614. dev_err_ratelimited(swrm->dev, "%s: data is NULL\n", __func__);
  3615. ret = -EINVAL;
  3616. } else {
  3617. mutex_lock(&swrm->mlock);
  3618. swrm->num_rx_chs = *(int *)data;
  3619. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  3620. list_for_each_entry(swr_dev, &mstr->devices,
  3621. dev_list) {
  3622. ret = swr_set_device_group(swr_dev,
  3623. SWR_BROADCAST);
  3624. if (ret)
  3625. dev_err_ratelimited(swrm->dev,
  3626. "%s: set num ch failed\n",
  3627. __func__);
  3628. }
  3629. } else {
  3630. list_for_each_entry(swr_dev, &mstr->devices,
  3631. dev_list) {
  3632. ret = swr_set_device_group(swr_dev,
  3633. SWR_GROUP_NONE);
  3634. if (ret)
  3635. dev_err_ratelimited(swrm->dev,
  3636. "%s: set num ch failed\n",
  3637. __func__);
  3638. }
  3639. }
  3640. mutex_unlock(&swrm->mlock);
  3641. }
  3642. break;
  3643. case SWR_REGISTER_WAKE_IRQ:
  3644. if (!data) {
  3645. dev_err_ratelimited(swrm->dev, "%s: reg wake irq data is NULL\n",
  3646. __func__);
  3647. ret = -EINVAL;
  3648. } else {
  3649. mutex_lock(&swrm->mlock);
  3650. swrm->ipc_wakeup = *(u32 *)data;
  3651. ret = swrm_register_wake_irq(swrm);
  3652. if (ret)
  3653. dev_err_ratelimited(swrm->dev, "%s: register wake_irq failed\n",
  3654. __func__);
  3655. mutex_unlock(&swrm->mlock);
  3656. }
  3657. break;
  3658. case SWR_REGISTER_WAKEUP:
  3659. //msm_aud_evt_blocking_notifier_call_chain(
  3660. // SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  3661. break;
  3662. case SWR_DEREGISTER_WAKEUP:
  3663. //msm_aud_evt_blocking_notifier_call_chain(
  3664. // SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  3665. break;
  3666. case SWR_SET_PORT_MAP:
  3667. if (!data) {
  3668. dev_err_ratelimited(swrm->dev, "%s: data is NULL for id=%d\n",
  3669. __func__, id);
  3670. ret = -EINVAL;
  3671. } else {
  3672. mutex_lock(&swrm->mlock);
  3673. port_cfg = (struct swrm_port_config *)data;
  3674. if (!port_cfg->size) {
  3675. ret = -EINVAL;
  3676. goto done;
  3677. }
  3678. ret = swrm_alloc_port_mem(&pdev->dev, swrm,
  3679. port_cfg->uc, port_cfg->size);
  3680. if (!ret)
  3681. swrm_copy_port_config(swrm, port_cfg,
  3682. port_cfg->size);
  3683. done:
  3684. mutex_unlock(&swrm->mlock);
  3685. }
  3686. break;
  3687. default:
  3688. dev_err_ratelimited(swrm->dev, "%s: swr master unknown id %d\n",
  3689. __func__, id);
  3690. break;
  3691. }
  3692. return ret;
  3693. }
  3694. EXPORT_SYMBOL(swrm_wcd_notify);
  3695. /*
  3696. * swrm_pm_cmpxchg:
  3697. * Check old state and exchange with pm new state
  3698. * if old state matches with current state
  3699. *
  3700. * @swrm: pointer to wcd core resource
  3701. * @o: pm old state
  3702. * @n: pm new state
  3703. *
  3704. * Returns old state
  3705. */
  3706. static enum swrm_pm_state swrm_pm_cmpxchg(
  3707. struct swr_mstr_ctrl *swrm,
  3708. enum swrm_pm_state o,
  3709. enum swrm_pm_state n)
  3710. {
  3711. enum swrm_pm_state old;
  3712. if (!swrm)
  3713. return o;
  3714. mutex_lock(&swrm->pm_lock);
  3715. old = swrm->pm_state;
  3716. if (old == o)
  3717. swrm->pm_state = n;
  3718. mutex_unlock(&swrm->pm_lock);
  3719. return old;
  3720. }
  3721. static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
  3722. {
  3723. enum swrm_pm_state os;
  3724. /*
  3725. * swrm_{lock/unlock}_sleep will be called by swr irq handler
  3726. * and slave wake up requests..
  3727. *
  3728. * If system didn't resume, we can simply return false so
  3729. * IRQ handler can return without handling IRQ.
  3730. */
  3731. mutex_lock(&swrm->pm_lock);
  3732. if (swrm->wlock_holders++ == 0) {
  3733. dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
  3734. cpu_latency_qos_update_request(&swrm->pm_qos_req,
  3735. CPU_IDLE_LATENCY);
  3736. pm_stay_awake(swrm->dev);
  3737. }
  3738. mutex_unlock(&swrm->pm_lock);
  3739. if (!wait_event_timeout(swrm->pm_wq,
  3740. ((os = swrm_pm_cmpxchg(swrm,
  3741. SWRM_PM_SLEEPABLE,
  3742. SWRM_PM_AWAKE)) ==
  3743. SWRM_PM_SLEEPABLE ||
  3744. (os == SWRM_PM_AWAKE)),
  3745. msecs_to_jiffies(
  3746. SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
  3747. dev_err_ratelimited(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
  3748. __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
  3749. swrm->wlock_holders);
  3750. swrm_unlock_sleep(swrm);
  3751. return false;
  3752. }
  3753. wake_up_all(&swrm->pm_wq);
  3754. return true;
  3755. }
  3756. static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
  3757. {
  3758. mutex_lock(&swrm->pm_lock);
  3759. if (--swrm->wlock_holders == 0) {
  3760. dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
  3761. __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
  3762. /*
  3763. * if swrm_lock_sleep failed, pm_state would be still
  3764. * swrm_PM_ASLEEP, don't overwrite
  3765. */
  3766. if (likely(swrm->pm_state == SWRM_PM_AWAKE))
  3767. swrm->pm_state = SWRM_PM_SLEEPABLE;
  3768. cpu_latency_qos_update_request(&swrm->pm_qos_req,
  3769. PM_QOS_DEFAULT_VALUE);
  3770. pm_relax(swrm->dev);
  3771. }
  3772. mutex_unlock(&swrm->pm_lock);
  3773. wake_up_all(&swrm->pm_wq);
  3774. }
  3775. #ifdef CONFIG_PM_SLEEP
  3776. static int swrm_suspend(struct device *dev)
  3777. {
  3778. int ret = -EBUSY;
  3779. struct platform_device *pdev = to_platform_device(dev);
  3780. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3781. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  3782. mutex_lock(&swrm->pm_lock);
  3783. if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
  3784. dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
  3785. __func__, swrm->pm_state,
  3786. swrm->wlock_holders);
  3787. /*
  3788. * before updating the pm_state to ASLEEP, check if device is
  3789. * runtime suspended or not. If it is not, then first make it
  3790. * runtime suspend, and then update the pm_state to ASLEEP.
  3791. */
  3792. mutex_unlock(&swrm->pm_lock); /* release pm_lock before dev suspend */
  3793. swrm_device_suspend(swrm->dev); /* runtime suspend the device */
  3794. mutex_lock(&swrm->pm_lock); /* acquire pm_lock and update state */
  3795. if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
  3796. swrm->pm_state = SWRM_PM_ASLEEP;
  3797. } else if (swrm->pm_state == SWRM_PM_AWAKE) {
  3798. ret = -EBUSY;
  3799. mutex_unlock(&swrm->pm_lock);
  3800. goto check_ebusy;
  3801. }
  3802. } else if (swrm->pm_state == SWRM_PM_AWAKE) {
  3803. /*
  3804. * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
  3805. * then set to SWRM_PM_ASLEEP
  3806. */
  3807. dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
  3808. __func__, swrm->pm_state,
  3809. swrm->wlock_holders);
  3810. mutex_unlock(&swrm->pm_lock);
  3811. if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
  3812. swrm, SWRM_PM_SLEEPABLE,
  3813. SWRM_PM_ASLEEP) ==
  3814. SWRM_PM_SLEEPABLE,
  3815. msecs_to_jiffies(
  3816. SWRM_SYS_SUSPEND_WAIT)))) {
  3817. dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
  3818. __func__, swrm->pm_state,
  3819. swrm->wlock_holders);
  3820. return 0;
  3821. } else {
  3822. dev_dbg(swrm->dev,
  3823. "%s: done, state %d, wlock %d\n",
  3824. __func__, swrm->pm_state,
  3825. swrm->wlock_holders);
  3826. }
  3827. mutex_lock(&swrm->pm_lock);
  3828. } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
  3829. dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
  3830. __func__, swrm->pm_state,
  3831. swrm->wlock_holders);
  3832. }
  3833. mutex_unlock(&swrm->pm_lock);
  3834. if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
  3835. ret = swrm_runtime_suspend(dev);
  3836. if (!ret) {
  3837. /*
  3838. * Synchronize runtime-pm and system-pm states:
  3839. * At this point, we are already suspended. If
  3840. * runtime-pm still thinks its active, then
  3841. * make sure its status is in sync with HW
  3842. * status. The three below calls let the
  3843. * runtime-pm know that we are suspended
  3844. * already without re-invoking the suspend
  3845. * callback
  3846. */
  3847. pm_runtime_disable(dev);
  3848. pm_runtime_set_suspended(dev);
  3849. pm_runtime_enable(dev);
  3850. }
  3851. }
  3852. check_ebusy:
  3853. if (ret == -EBUSY) {
  3854. /*
  3855. * There is a possibility that some audio stream is active
  3856. * during suspend. We dont want to return suspend failure in
  3857. * that case so that display and relevant components can still
  3858. * go to suspend.
  3859. * If there is some other error, then it should be passed-on
  3860. * to system level suspend
  3861. */
  3862. ret = 0;
  3863. }
  3864. return ret;
  3865. }
  3866. static int swrm_resume(struct device *dev)
  3867. {
  3868. int ret = 0;
  3869. struct platform_device *pdev = to_platform_device(dev);
  3870. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3871. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  3872. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  3873. ret = swrm_runtime_resume(dev);
  3874. if (!ret) {
  3875. pm_runtime_mark_last_busy(dev);
  3876. pm_request_autosuspend(dev);
  3877. }
  3878. }
  3879. mutex_lock(&swrm->pm_lock);
  3880. if (swrm->pm_state == SWRM_PM_ASLEEP) {
  3881. dev_dbg(swrm->dev,
  3882. "%s: resuming system, state %d, wlock %d\n",
  3883. __func__, swrm->pm_state,
  3884. swrm->wlock_holders);
  3885. swrm->pm_state = SWRM_PM_SLEEPABLE;
  3886. } else {
  3887. dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
  3888. __func__, swrm->pm_state,
  3889. swrm->wlock_holders);
  3890. }
  3891. mutex_unlock(&swrm->pm_lock);
  3892. wake_up_all(&swrm->pm_wq);
  3893. return ret;
  3894. }
  3895. #endif /* CONFIG_PM_SLEEP */
  3896. static const struct dev_pm_ops swrm_dev_pm_ops = {
  3897. SET_SYSTEM_SLEEP_PM_OPS(
  3898. swrm_suspend,
  3899. swrm_resume
  3900. )
  3901. SET_RUNTIME_PM_OPS(
  3902. swrm_runtime_suspend,
  3903. swrm_runtime_resume,
  3904. NULL
  3905. )
  3906. };
  3907. static const struct of_device_id swrm_dt_match[] = {
  3908. {
  3909. .compatible = "qcom,swr-mstr",
  3910. },
  3911. {}
  3912. };
  3913. static struct platform_driver swr_mstr_driver = {
  3914. .probe = swrm_probe,
  3915. .remove = swrm_remove,
  3916. .driver = {
  3917. .name = SWR_NAME,
  3918. .owner = THIS_MODULE,
  3919. .pm = &swrm_dev_pm_ops,
  3920. .of_match_table = swrm_dt_match,
  3921. .suppress_bind_attrs = true,
  3922. },
  3923. };
  3924. static int __init swrm_init(void)
  3925. {
  3926. return platform_driver_register(&swr_mstr_driver);
  3927. }
  3928. module_init(swrm_init);
  3929. static void __exit swrm_exit(void)
  3930. {
  3931. platform_driver_unregister(&swr_mstr_driver);
  3932. }
  3933. module_exit(swrm_exit);
  3934. MODULE_LICENSE("GPL v2");
  3935. MODULE_DESCRIPTION("SoundWire Master Controller");
  3936. MODULE_ALIAS("platform:swr-mstr");