swr-mstr-ctrl.c 103 KB

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