swr-mstr-ctrl.c 109 KB

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