swr-mstr-ctrl.c 101 KB

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