swr-mstr-ctrl.c 101 KB

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