swr-mstr-ctrl.c 106 KB

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