swr-mstr-ctrl.c 109 KB

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