swr-mstr-ctrl.c 109 KB

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