swr-mstr-ctrl.c 107 KB

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