swr-mstr-ctrl.c 95 KB

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