swr-mstr-ctrl.c 97 KB

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