swr-mstr-ctrl.c 110 KB

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