swr-mstr-ctrl.c 103 KB

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