swr-mstr-ctrl.c 105 KB

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