swr-mstr-ctrl.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674
  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,
  1097. port_req->dev_num, 0x00,
  1098. SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
  1099. bank));
  1100. /* Assumption: If different channels in the same port
  1101. * on master is enabled for different slaves, then each
  1102. * slave offset should be configured differently.
  1103. */
  1104. if (prev_mport == mport)
  1105. offset1 += mport->offset1;
  1106. else {
  1107. offset1 = mport->offset1;
  1108. prev_mport = mport;
  1109. }
  1110. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  1111. val[len++] = SWR_REG_VAL_PACK(offset1,
  1112. port_req->dev_num, 0x00,
  1113. SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
  1114. bank));
  1115. if (mport->offset2 != SWR_INVALID_PARAM) {
  1116. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  1117. val[len++] = SWR_REG_VAL_PACK(mport->offset2,
  1118. port_req->dev_num, 0x00,
  1119. SWRS_DP_OFFSET_CONTROL_2_BANK(
  1120. slv_id, bank));
  1121. }
  1122. if (mport->hstart != SWR_INVALID_PARAM
  1123. && mport->hstop != SWR_INVALID_PARAM) {
  1124. hparams = (mport->hstart << 4) | mport->hstop;
  1125. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  1126. val[len++] = SWR_REG_VAL_PACK(hparams,
  1127. port_req->dev_num, 0x00,
  1128. SWRS_DP_HCONTROL_BANK(slv_id,
  1129. bank));
  1130. }
  1131. if (mport->word_length != SWR_INVALID_PARAM) {
  1132. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  1133. val[len++] =
  1134. SWR_REG_VAL_PACK(mport->word_length,
  1135. port_req->dev_num, 0x00,
  1136. SWRS_DP_BLOCK_CONTROL_1(slv_id));
  1137. }
  1138. if (mport->blk_pack_mode != SWR_INVALID_PARAM
  1139. && swrm->master_id != MASTER_ID_WSA) {
  1140. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  1141. val[len++] =
  1142. SWR_REG_VAL_PACK(mport->blk_pack_mode,
  1143. port_req->dev_num, 0x00,
  1144. SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
  1145. bank));
  1146. }
  1147. if (mport->blk_grp_count != SWR_INVALID_PARAM) {
  1148. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  1149. val[len++] =
  1150. SWR_REG_VAL_PACK(mport->blk_grp_count,
  1151. port_req->dev_num, 0x00,
  1152. SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
  1153. bank));
  1154. }
  1155. if (mport->lane_ctrl != SWR_INVALID_PARAM) {
  1156. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  1157. val[len++] =
  1158. SWR_REG_VAL_PACK(mport->lane_ctrl,
  1159. port_req->dev_num, 0x00,
  1160. SWRS_DP_LANE_CONTROL_BANK(slv_id,
  1161. bank));
  1162. }
  1163. port_req->ch_en = port_req->req_ch;
  1164. }
  1165. value = ((mport->req_ch)
  1166. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  1167. if (mport->offset2 != SWR_INVALID_PARAM)
  1168. value |= ((mport->offset2)
  1169. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  1170. value |= ((mport->offset1)
  1171. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  1172. value |= (mport->sinterval & 0xFF);
  1173. reg[len] = SWRM_DP_PORT_CTRL_BANK((i + 1), bank);
  1174. val[len++] = value;
  1175. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  1176. __func__, i,
  1177. (SWRM_DP_PORT_CTRL_BANK((i + 1), bank)), value);
  1178. reg[len] = SWRM_DP_SAMPLECTRL2_BANK((i + 1), bank);
  1179. val[len++] = ((mport->sinterval >> 8) & 0xFF);
  1180. if (mport->lane_ctrl != SWR_INVALID_PARAM) {
  1181. reg[len] = SWRM_DP_PORT_CTRL_2_BANK((i + 1), bank);
  1182. val[len++] = mport->lane_ctrl;
  1183. }
  1184. if (mport->word_length != SWR_INVALID_PARAM) {
  1185. reg[len] = SWRM_DP_BLOCK_CTRL_1((i + 1));
  1186. val[len++] = mport->word_length;
  1187. }
  1188. if (mport->blk_grp_count != SWR_INVALID_PARAM) {
  1189. reg[len] = SWRM_DP_BLOCK_CTRL2_BANK((i + 1), bank);
  1190. val[len++] = mport->blk_grp_count;
  1191. }
  1192. if (mport->hstart != SWR_INVALID_PARAM
  1193. && mport->hstop != SWR_INVALID_PARAM) {
  1194. reg[len] = SWRM_DP_PORT_HCTRL_BANK((i + 1), bank);
  1195. hparams = (mport->hstop << 4) | mport->hstart;
  1196. val[len++] = hparams;
  1197. } else {
  1198. reg[len] = SWRM_DP_PORT_HCTRL_BANK((i + 1), bank);
  1199. hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
  1200. val[len++] = hparams;
  1201. }
  1202. if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
  1203. reg[len] = SWRM_DP_BLOCK_CTRL3_BANK((i + 1), bank);
  1204. val[len++] = mport->blk_pack_mode;
  1205. }
  1206. mport->ch_en = mport->req_ch;
  1207. }
  1208. swrm_reg_dump(swrm, reg, val, len, __func__);
  1209. swr_master_bulk_write(swrm, reg, val, len);
  1210. }
  1211. static void swrm_apply_port_config(struct swr_master *master)
  1212. {
  1213. u8 bank;
  1214. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1215. if (!swrm) {
  1216. pr_err("%s: Invalid handle to swr controller\n",
  1217. __func__);
  1218. return;
  1219. }
  1220. bank = get_inactive_bank_num(swrm);
  1221. dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
  1222. __func__, bank, master->num_port);
  1223. if (!swrm->disable_div2_clk_switch)
  1224. swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
  1225. SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
  1226. swrm_copy_data_port_config(master, bank);
  1227. }
  1228. static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
  1229. {
  1230. u8 bank;
  1231. u32 value = 0, n_row = 0, n_col = 0;
  1232. u32 row = 0, col = 0;
  1233. int bus_clk_div_factor;
  1234. int ret;
  1235. u8 ssp_period = 0;
  1236. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1237. int mask = (SWRM_ROW_CTRL_MASK | SWRM_COL_CTRL_MASK |
  1238. SWRM_CLK_DIV_MASK | SWRM_SSP_PERIOD_MASK);
  1239. u8 inactive_bank;
  1240. int frame_sync = SWRM_FRAME_SYNC_SEL;
  1241. if (!swrm) {
  1242. pr_err("%s: swrm is null\n", __func__);
  1243. return -EFAULT;
  1244. }
  1245. mutex_lock(&swrm->mlock);
  1246. /*
  1247. * During disable if master is already down, which implies an ssr/pdr
  1248. * scenario, just mark ports as disabled and exit
  1249. */
  1250. if (swrm->state == SWR_MSTR_SSR && !enable) {
  1251. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  1252. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  1253. __func__);
  1254. goto exit;
  1255. }
  1256. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1257. swrm_cleanup_disabled_port_reqs(master);
  1258. if (!swrm_is_port_en(master)) {
  1259. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  1260. __func__);
  1261. pm_runtime_mark_last_busy(swrm->dev);
  1262. pm_runtime_put_autosuspend(swrm->dev);
  1263. }
  1264. goto exit;
  1265. }
  1266. bank = get_inactive_bank_num(swrm);
  1267. if (enable) {
  1268. if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
  1269. dev_dbg(swrm->dev, "%s:No pending connect port req\n",
  1270. __func__);
  1271. goto exit;
  1272. }
  1273. clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1274. ret = swrm_get_port_config(swrm);
  1275. if (ret) {
  1276. /* cannot accommodate ports */
  1277. swrm_cleanup_disabled_port_reqs(master);
  1278. mutex_unlock(&swrm->mlock);
  1279. return -EINVAL;
  1280. }
  1281. swr_master_write(swrm, SWRM_CPU1_INTERRUPT_EN,
  1282. SWRM_INTERRUPT_STATUS_MASK);
  1283. /* apply the new port config*/
  1284. swrm_apply_port_config(master);
  1285. } else {
  1286. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  1287. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  1288. __func__);
  1289. goto exit;
  1290. }
  1291. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1292. swrm_disable_ports(master, bank);
  1293. }
  1294. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d freq %d\n",
  1295. __func__, enable, swrm->num_cfg_devs, swrm->mclk_freq);
  1296. if (enable) {
  1297. /* set col = 16 */
  1298. n_col = SWR_MAX_COL;
  1299. col = SWRM_COL_16;
  1300. if (swrm->bus_clk == MCLK_FREQ_LP) {
  1301. n_col = SWR_MIN_COL;
  1302. col = SWRM_COL_02;
  1303. }
  1304. } else {
  1305. /*
  1306. * Do not change to col = 2 if there are still active ports
  1307. */
  1308. if (!master->num_port) {
  1309. n_col = SWR_MIN_COL;
  1310. col = SWRM_COL_02;
  1311. } else {
  1312. n_col = SWR_MAX_COL;
  1313. col = SWRM_COL_16;
  1314. }
  1315. }
  1316. /* Use default 50 * x, frame shape. Change based on mclk */
  1317. if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
  1318. dev_dbg(swrm->dev, "setting 64 x %d frameshape\n", col);
  1319. n_row = SWR_ROW_64;
  1320. row = SWRM_ROW_64;
  1321. frame_sync = SWRM_FRAME_SYNC_SEL_NATIVE;
  1322. } else {
  1323. dev_dbg(swrm->dev, "setting 50 x %d frameshape\n", col);
  1324. n_row = SWR_ROW_50;
  1325. row = SWRM_ROW_50;
  1326. frame_sync = SWRM_FRAME_SYNC_SEL;
  1327. }
  1328. ssp_period = swrm_get_ssp_period(swrm, row, col, frame_sync);
  1329. bus_clk_div_factor = swrm_get_clk_div(swrm->mclk_freq, swrm->bus_clk);
  1330. dev_dbg(swrm->dev, "%s: ssp_period: %d, bus_clk_div:%d \n", __func__,
  1331. ssp_period, bus_clk_div_factor);
  1332. value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK(bank));
  1333. value &= (~mask);
  1334. value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1335. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1336. (bus_clk_div_factor <<
  1337. SWRM_MCP_FRAME_CTRL_BANK_CLK_DIV_VALUE_SHFT) |
  1338. ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1339. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK(bank), value);
  1340. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  1341. SWRM_MCP_FRAME_CTRL_BANK(bank), value);
  1342. enable_bank_switch(swrm, bank, n_row, n_col);
  1343. inactive_bank = bank ? 0 : 1;
  1344. if (enable)
  1345. swrm_copy_data_port_config(master, inactive_bank);
  1346. else {
  1347. swrm_disable_ports(master, inactive_bank);
  1348. swrm_cleanup_disabled_port_reqs(master);
  1349. }
  1350. if (!swrm_is_port_en(master)) {
  1351. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  1352. __func__);
  1353. pm_runtime_mark_last_busy(swrm->dev);
  1354. pm_runtime_put_autosuspend(swrm->dev);
  1355. }
  1356. exit:
  1357. mutex_unlock(&swrm->mlock);
  1358. return 0;
  1359. }
  1360. static int swrm_connect_port(struct swr_master *master,
  1361. struct swr_params *portinfo)
  1362. {
  1363. int i;
  1364. struct swr_port_info *port_req;
  1365. int ret = 0;
  1366. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1367. struct swrm_mports *mport;
  1368. u8 mstr_port_id, mstr_ch_msk;
  1369. dev_dbg(&master->dev, "%s: enter\n", __func__);
  1370. if (!portinfo)
  1371. return -EINVAL;
  1372. if (!swrm) {
  1373. dev_err(&master->dev,
  1374. "%s: Invalid handle to swr controller\n",
  1375. __func__);
  1376. return -EINVAL;
  1377. }
  1378. mutex_lock(&swrm->mlock);
  1379. mutex_lock(&swrm->devlock);
  1380. if (!swrm->dev_up) {
  1381. mutex_unlock(&swrm->devlock);
  1382. mutex_unlock(&swrm->mlock);
  1383. return -EINVAL;
  1384. }
  1385. mutex_unlock(&swrm->devlock);
  1386. if (!swrm_is_port_en(master))
  1387. pm_runtime_get_sync(swrm->dev);
  1388. for (i = 0; i < portinfo->num_port; i++) {
  1389. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
  1390. portinfo->port_type[i],
  1391. portinfo->port_id[i]);
  1392. if (ret) {
  1393. dev_err(&master->dev,
  1394. "%s: mstr portid for slv port %d not found\n",
  1395. __func__, portinfo->port_id[i]);
  1396. goto port_fail;
  1397. }
  1398. mport = &(swrm->mport_cfg[mstr_port_id]);
  1399. /* get port req */
  1400. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1401. portinfo->dev_num);
  1402. if (!port_req) {
  1403. dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
  1404. __func__, portinfo->port_id[i],
  1405. portinfo->dev_num);
  1406. port_req = kzalloc(sizeof(struct swr_port_info),
  1407. GFP_KERNEL);
  1408. if (!port_req) {
  1409. ret = -ENOMEM;
  1410. goto mem_fail;
  1411. }
  1412. port_req->dev_num = portinfo->dev_num;
  1413. port_req->slave_port_id = portinfo->port_id[i];
  1414. port_req->num_ch = portinfo->num_ch[i];
  1415. port_req->ch_rate = portinfo->ch_rate[i];
  1416. port_req->ch_en = 0;
  1417. port_req->master_port_id = mstr_port_id;
  1418. list_add(&port_req->list, &mport->port_req_list);
  1419. }
  1420. port_req->req_ch |= portinfo->ch_en[i];
  1421. dev_dbg(&master->dev,
  1422. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
  1423. __func__, port_req->master_port_id,
  1424. port_req->slave_port_id, port_req->ch_rate,
  1425. port_req->num_ch);
  1426. /* Put the port req on master port */
  1427. mport = &(swrm->mport_cfg[mstr_port_id]);
  1428. mport->port_en = true;
  1429. mport->req_ch |= mstr_ch_msk;
  1430. master->port_en_mask |= (1 << mstr_port_id);
  1431. if (swrm->clk_stop_mode0_supp &&
  1432. swrm->dynamic_port_map_supported) {
  1433. mport->ch_rate += portinfo->ch_rate[i];
  1434. swrm_update_bus_clk(swrm);
  1435. }
  1436. }
  1437. master->num_port += portinfo->num_port;
  1438. set_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1439. swr_port_response(master, portinfo->tid);
  1440. mutex_unlock(&swrm->mlock);
  1441. return 0;
  1442. port_fail:
  1443. mem_fail:
  1444. /* cleanup port reqs in error condition */
  1445. swrm_cleanup_disabled_port_reqs(master);
  1446. mutex_unlock(&swrm->mlock);
  1447. return ret;
  1448. }
  1449. static int swrm_disconnect_port(struct swr_master *master,
  1450. struct swr_params *portinfo)
  1451. {
  1452. int i, ret = 0;
  1453. struct swr_port_info *port_req;
  1454. struct swrm_mports *mport;
  1455. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1456. u8 mstr_port_id, mstr_ch_mask;
  1457. if (!swrm) {
  1458. dev_err(&master->dev,
  1459. "%s: Invalid handle to swr controller\n",
  1460. __func__);
  1461. return -EINVAL;
  1462. }
  1463. if (!portinfo) {
  1464. dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
  1465. return -EINVAL;
  1466. }
  1467. mutex_lock(&swrm->mlock);
  1468. for (i = 0; i < portinfo->num_port; i++) {
  1469. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
  1470. portinfo->port_type[i], portinfo->port_id[i]);
  1471. if (ret) {
  1472. dev_err(&master->dev,
  1473. "%s: mstr portid for slv port %d not found\n",
  1474. __func__, portinfo->port_id[i]);
  1475. mutex_unlock(&swrm->mlock);
  1476. return -EINVAL;
  1477. }
  1478. mport = &(swrm->mport_cfg[mstr_port_id]);
  1479. /* get port req */
  1480. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1481. portinfo->dev_num);
  1482. if (!port_req) {
  1483. dev_err(&master->dev, "%s:port not enabled : port %d\n",
  1484. __func__, portinfo->port_id[i]);
  1485. mutex_unlock(&swrm->mlock);
  1486. return -EINVAL;
  1487. }
  1488. port_req->req_ch &= ~portinfo->ch_en[i];
  1489. mport->req_ch &= ~mstr_ch_mask;
  1490. if (swrm->clk_stop_mode0_supp &&
  1491. swrm->dynamic_port_map_supported &&
  1492. !mport->req_ch) {
  1493. mport->ch_rate = 0;
  1494. swrm_update_bus_clk(swrm);
  1495. }
  1496. }
  1497. master->num_port -= portinfo->num_port;
  1498. set_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1499. swr_port_response(master, portinfo->tid);
  1500. mutex_unlock(&swrm->mlock);
  1501. return 0;
  1502. }
  1503. static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
  1504. int status, u8 *devnum)
  1505. {
  1506. int i;
  1507. bool found = false;
  1508. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1509. if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
  1510. *devnum = i;
  1511. found = true;
  1512. break;
  1513. }
  1514. status >>= 2;
  1515. }
  1516. if (found)
  1517. return 0;
  1518. else
  1519. return -EINVAL;
  1520. }
  1521. static void swrm_enable_slave_irq(struct swr_mstr_ctrl *swrm)
  1522. {
  1523. int i;
  1524. int status = 0;
  1525. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1526. if (!status) {
  1527. dev_dbg_ratelimited(swrm->dev, "%s: slaves status is 0x%x\n",
  1528. __func__, status);
  1529. return;
  1530. }
  1531. dev_dbg(swrm->dev, "%s: slave status: 0x%x\n", __func__, status);
  1532. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1533. if (status & SWRM_MCP_SLV_STATUS_MASK) {
  1534. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, i, 0x0,
  1535. SWRS_SCP_INT_STATUS_CLEAR_1);
  1536. swrm_cmd_fifo_wr_cmd(swrm, 0x4, i, 0x0,
  1537. SWRS_SCP_INT_STATUS_MASK_1);
  1538. }
  1539. status >>= 2;
  1540. }
  1541. }
  1542. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  1543. int status, u8 *devnum)
  1544. {
  1545. int i;
  1546. int new_sts = status;
  1547. int ret = SWR_NOT_PRESENT;
  1548. if (status != swrm->slave_status) {
  1549. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1550. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  1551. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  1552. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  1553. *devnum = i;
  1554. break;
  1555. }
  1556. status >>= 2;
  1557. swrm->slave_status >>= 2;
  1558. }
  1559. swrm->slave_status = new_sts;
  1560. }
  1561. return ret;
  1562. }
  1563. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  1564. {
  1565. struct swr_mstr_ctrl *swrm = dev;
  1566. u32 value, intr_sts, intr_sts_masked;
  1567. u32 temp = 0;
  1568. u32 status, chg_sts, i;
  1569. u8 devnum = 0;
  1570. int ret = IRQ_HANDLED;
  1571. struct swr_device *swr_dev;
  1572. struct swr_master *mstr = &swrm->master;
  1573. int retry = 5;
  1574. trace_printk("%s enter\n", __func__);
  1575. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1576. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1577. return IRQ_NONE;
  1578. }
  1579. mutex_lock(&swrm->reslock);
  1580. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  1581. ret = IRQ_NONE;
  1582. goto exit;
  1583. }
  1584. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true)) {
  1585. ret = IRQ_NONE;
  1586. goto err_audio_hw_vote;
  1587. }
  1588. ret = swrm_clk_request(swrm, true);
  1589. if (ret) {
  1590. dev_err(dev, "%s: swrm clk failed\n", __func__);
  1591. ret = IRQ_NONE;
  1592. goto err_audio_core_vote;
  1593. }
  1594. mutex_unlock(&swrm->reslock);
  1595. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1596. intr_sts_masked = intr_sts & swrm->intr_mask;
  1597. dev_dbg(swrm->dev, "%s: status: 0x%x \n", __func__, intr_sts_masked);
  1598. trace_printk("%s: status: 0x%x \n", __func__, intr_sts_masked);
  1599. handle_irq:
  1600. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  1601. value = intr_sts_masked & (1 << i);
  1602. if (!value)
  1603. continue;
  1604. switch (value) {
  1605. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  1606. dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
  1607. __func__);
  1608. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1609. ret = swrm_find_alert_slave(swrm, status, &devnum);
  1610. if (ret) {
  1611. dev_err_ratelimited(swrm->dev,
  1612. "%s: no slave alert found.spurious interrupt\n",
  1613. __func__);
  1614. break;
  1615. }
  1616. swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
  1617. SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  1618. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1619. SWRS_SCP_INT_STATUS_CLEAR_1);
  1620. swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
  1621. SWRS_SCP_INT_STATUS_CLEAR_1);
  1622. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1623. if (swr_dev->dev_num != devnum)
  1624. continue;
  1625. if (swr_dev->slave_irq) {
  1626. do {
  1627. swr_dev->slave_irq_pending = 0;
  1628. handle_nested_irq(
  1629. irq_find_mapping(
  1630. swr_dev->slave_irq, 0));
  1631. } while (swr_dev->slave_irq_pending);
  1632. }
  1633. }
  1634. break;
  1635. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  1636. dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
  1637. __func__);
  1638. break;
  1639. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  1640. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1641. swrm_enable_slave_irq(swrm);
  1642. if (status == swrm->slave_status) {
  1643. dev_dbg(swrm->dev,
  1644. "%s: No change in slave status: 0x%x\n",
  1645. __func__, status);
  1646. break;
  1647. }
  1648. chg_sts = swrm_check_slave_change_status(swrm, status,
  1649. &devnum);
  1650. switch (chg_sts) {
  1651. case SWR_NOT_PRESENT:
  1652. dev_dbg(swrm->dev,
  1653. "%s: device %d got detached\n",
  1654. __func__, devnum);
  1655. if (devnum == 0) {
  1656. /*
  1657. * enable host irq if device 0 detached
  1658. * as hw will mask host_irq at slave
  1659. * but will not unmask it afterwards.
  1660. */
  1661. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
  1662. SWRS_SCP_INT_STATUS_CLEAR_1);
  1663. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1664. SWRS_SCP_INT_STATUS_MASK_1);
  1665. }
  1666. break;
  1667. case SWR_ATTACHED_OK:
  1668. dev_dbg(swrm->dev,
  1669. "%s: device %d got attached\n",
  1670. __func__, devnum);
  1671. /* enable host irq from slave device*/
  1672. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
  1673. SWRS_SCP_INT_STATUS_CLEAR_1);
  1674. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1675. SWRS_SCP_INT_STATUS_MASK_1);
  1676. break;
  1677. case SWR_ALERT:
  1678. dev_dbg(swrm->dev,
  1679. "%s: device %d has pending interrupt\n",
  1680. __func__, devnum);
  1681. break;
  1682. }
  1683. break;
  1684. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  1685. dev_err_ratelimited(swrm->dev,
  1686. "%s: SWR bus clsh detected\n",
  1687. __func__);
  1688. swrm->intr_mask &=
  1689. ~SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET;
  1690. swr_master_write(swrm,
  1691. SWRM_CPU1_INTERRUPT_EN,
  1692. swrm->intr_mask);
  1693. break;
  1694. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  1695. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  1696. dev_err(swrm->dev,
  1697. "%s: SWR read FIFO overflow fifo status\n",
  1698. __func__, value);
  1699. break;
  1700. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  1701. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  1702. dev_err(swrm->dev,
  1703. "%s: SWR read FIFO underflow fifo status\n",
  1704. __func__, value);
  1705. break;
  1706. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  1707. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  1708. dev_err(swrm->dev,
  1709. "%s: SWR write FIFO overflow fifo status\n",
  1710. __func__, value);
  1711. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  1712. break;
  1713. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  1714. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  1715. dev_err_ratelimited(swrm->dev,
  1716. "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
  1717. __func__, value);
  1718. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  1719. break;
  1720. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  1721. dev_err_ratelimited(swrm->dev,
  1722. "%s: SWR Port collision detected\n",
  1723. __func__);
  1724. swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
  1725. swr_master_write(swrm,
  1726. SWRM_CPU1_INTERRUPT_EN, swrm->intr_mask);
  1727. break;
  1728. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  1729. dev_dbg(swrm->dev,
  1730. "%s: SWR read enable valid mismatch\n",
  1731. __func__);
  1732. swrm->intr_mask &=
  1733. ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
  1734. swr_master_write(swrm,
  1735. SWRM_CPU1_INTERRUPT_EN, swrm->intr_mask);
  1736. break;
  1737. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  1738. complete(&swrm->broadcast);
  1739. dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
  1740. __func__);
  1741. break;
  1742. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  1743. swr_master_write(swrm, SWRM_ENUMERATOR_CFG, 0);
  1744. while (swr_master_read(swrm, SWRM_ENUMERATOR_STATUS)) {
  1745. if (!retry) {
  1746. dev_dbg(swrm->dev,
  1747. "%s: ENUM status is not idle\n",
  1748. __func__);
  1749. break;
  1750. }
  1751. retry--;
  1752. }
  1753. swr_master_write(swrm, SWRM_ENUMERATOR_CFG, 1);
  1754. break;
  1755. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  1756. break;
  1757. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  1758. swrm_check_link_status(swrm, 0x1);
  1759. break;
  1760. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  1761. break;
  1762. case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
  1763. if (swrm->state == SWR_MSTR_UP)
  1764. dev_dbg(swrm->dev,
  1765. "%s:SWR Master is already up\n",
  1766. __func__);
  1767. else
  1768. dev_err_ratelimited(swrm->dev,
  1769. "%s: SWR wokeup during clock stop\n",
  1770. __func__);
  1771. /* It might be possible the slave device gets reset
  1772. * and slave interrupt gets missed. So re-enable
  1773. * Host IRQ and process slave pending
  1774. * interrupts, if any.
  1775. */
  1776. swrm_enable_slave_irq(swrm);
  1777. break;
  1778. default:
  1779. dev_err_ratelimited(swrm->dev,
  1780. "%s: SWR unknown interrupt value: %d\n",
  1781. __func__, value);
  1782. ret = IRQ_NONE;
  1783. break;
  1784. }
  1785. }
  1786. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
  1787. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
  1788. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1789. intr_sts_masked = intr_sts & swrm->intr_mask;
  1790. if (intr_sts_masked) {
  1791. dev_dbg(swrm->dev, "%s: new interrupt received 0x%x\n",
  1792. __func__, intr_sts_masked);
  1793. goto handle_irq;
  1794. }
  1795. mutex_lock(&swrm->reslock);
  1796. swrm_clk_request(swrm, false);
  1797. err_audio_core_vote:
  1798. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  1799. err_audio_hw_vote:
  1800. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  1801. exit:
  1802. mutex_unlock(&swrm->reslock);
  1803. swrm_unlock_sleep(swrm);
  1804. trace_printk("%s exit\n", __func__);
  1805. return ret;
  1806. }
  1807. static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
  1808. {
  1809. struct swr_mstr_ctrl *swrm = dev;
  1810. int ret = IRQ_HANDLED;
  1811. if (!swrm || !(swrm->dev)) {
  1812. pr_err("%s: swrm or dev is null\n", __func__);
  1813. return IRQ_NONE;
  1814. }
  1815. trace_printk("%s enter\n", __func__);
  1816. mutex_lock(&swrm->devlock);
  1817. if (!swrm->dev_up) {
  1818. if (swrm->wake_irq > 0) {
  1819. if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
  1820. pr_err("%s: irq data is NULL\n", __func__);
  1821. mutex_unlock(&swrm->devlock);
  1822. return IRQ_NONE;
  1823. }
  1824. mutex_lock(&swrm->irq_lock);
  1825. if (!irqd_irq_disabled(
  1826. irq_get_irq_data(swrm->wake_irq)))
  1827. disable_irq_nosync(swrm->wake_irq);
  1828. mutex_unlock(&swrm->irq_lock);
  1829. }
  1830. mutex_unlock(&swrm->devlock);
  1831. return ret;
  1832. }
  1833. mutex_unlock(&swrm->devlock);
  1834. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1835. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1836. goto exit;
  1837. }
  1838. if (swrm->wake_irq > 0) {
  1839. if (unlikely(!irq_get_irq_data(swrm->wake_irq))) {
  1840. pr_err("%s: irq data is NULL\n", __func__);
  1841. return IRQ_NONE;
  1842. }
  1843. mutex_lock(&swrm->irq_lock);
  1844. if (!irqd_irq_disabled(
  1845. irq_get_irq_data(swrm->wake_irq)))
  1846. disable_irq_nosync(swrm->wake_irq);
  1847. mutex_unlock(&swrm->irq_lock);
  1848. }
  1849. pm_runtime_get_sync(swrm->dev);
  1850. pm_runtime_mark_last_busy(swrm->dev);
  1851. pm_runtime_put_autosuspend(swrm->dev);
  1852. swrm_unlock_sleep(swrm);
  1853. exit:
  1854. trace_printk("%s exit\n", __func__);
  1855. return ret;
  1856. }
  1857. static void swrm_wakeup_work(struct work_struct *work)
  1858. {
  1859. struct swr_mstr_ctrl *swrm;
  1860. swrm = container_of(work, struct swr_mstr_ctrl,
  1861. wakeup_work);
  1862. if (!swrm || !(swrm->dev)) {
  1863. pr_err("%s: swrm or dev is null\n", __func__);
  1864. return;
  1865. }
  1866. trace_printk("%s enter\n", __func__);
  1867. mutex_lock(&swrm->devlock);
  1868. if (!swrm->dev_up) {
  1869. mutex_unlock(&swrm->devlock);
  1870. goto exit;
  1871. }
  1872. mutex_unlock(&swrm->devlock);
  1873. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1874. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1875. goto exit;
  1876. }
  1877. pm_runtime_get_sync(swrm->dev);
  1878. pm_runtime_mark_last_busy(swrm->dev);
  1879. pm_runtime_put_autosuspend(swrm->dev);
  1880. swrm_unlock_sleep(swrm);
  1881. exit:
  1882. trace_printk("%s exit\n", __func__);
  1883. pm_relax(swrm->dev);
  1884. }
  1885. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  1886. {
  1887. u32 val;
  1888. swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1889. val = (swrm->slave_status >> (devnum * 2));
  1890. val &= SWRM_MCP_SLV_STATUS_MASK;
  1891. return val;
  1892. }
  1893. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  1894. u8 *dev_num)
  1895. {
  1896. int i;
  1897. u64 id = 0;
  1898. int ret = -EINVAL;
  1899. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1900. struct swr_device *swr_dev;
  1901. u32 num_dev = 0;
  1902. if (!swrm) {
  1903. pr_err("%s: Invalid handle to swr controller\n",
  1904. __func__);
  1905. return ret;
  1906. }
  1907. if (swrm->num_dev)
  1908. num_dev = swrm->num_dev;
  1909. else
  1910. num_dev = mstr->num_dev;
  1911. mutex_lock(&swrm->devlock);
  1912. if (!swrm->dev_up) {
  1913. mutex_unlock(&swrm->devlock);
  1914. return ret;
  1915. }
  1916. mutex_unlock(&swrm->devlock);
  1917. pm_runtime_get_sync(swrm->dev);
  1918. for (i = 1; i < (num_dev + 1); i++) {
  1919. id = ((u64)(swr_master_read(swrm,
  1920. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  1921. id |= swr_master_read(swrm,
  1922. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  1923. /*
  1924. * As pm_runtime_get_sync() brings all slaves out of reset
  1925. * update logical device number for all slaves.
  1926. */
  1927. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1928. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  1929. u32 status = swrm_get_device_status(swrm, i);
  1930. if ((status == 0x01) || (status == 0x02)) {
  1931. swr_dev->dev_num = i;
  1932. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  1933. *dev_num = i;
  1934. ret = 0;
  1935. dev_info(swrm->dev,
  1936. "%s: devnum %d assigned for dev %llx\n",
  1937. __func__, i,
  1938. swr_dev->addr);
  1939. }
  1940. }
  1941. }
  1942. }
  1943. }
  1944. if (ret)
  1945. dev_err_ratelimited(swrm->dev,
  1946. "%s: device 0x%llx is not ready\n",
  1947. __func__, dev_id);
  1948. pm_runtime_mark_last_busy(swrm->dev);
  1949. pm_runtime_put_autosuspend(swrm->dev);
  1950. return ret;
  1951. }
  1952. static void swrm_device_wakeup_vote(struct swr_master *mstr)
  1953. {
  1954. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1955. if (!swrm) {
  1956. pr_err("%s: Invalid handle to swr controller\n",
  1957. __func__);
  1958. return;
  1959. }
  1960. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1961. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1962. return;
  1963. }
  1964. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true))
  1965. dev_err(swrm->dev, "%s:lpass core hw enable failed\n",
  1966. __func__);
  1967. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  1968. dev_err(swrm->dev, "%s:lpass audio hw enable failed\n",
  1969. __func__);
  1970. pm_runtime_get_sync(swrm->dev);
  1971. }
  1972. static void swrm_device_wakeup_unvote(struct swr_master *mstr)
  1973. {
  1974. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1975. if (!swrm) {
  1976. pr_err("%s: Invalid handle to swr controller\n",
  1977. __func__);
  1978. return;
  1979. }
  1980. pm_runtime_mark_last_busy(swrm->dev);
  1981. pm_runtime_put_autosuspend(swrm->dev);
  1982. swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false);
  1983. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  1984. swrm_unlock_sleep(swrm);
  1985. }
  1986. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  1987. {
  1988. int ret = 0, i = 0;
  1989. u32 val;
  1990. u8 row_ctrl = SWR_ROW_50;
  1991. u8 col_ctrl = SWR_MIN_COL;
  1992. u8 ssp_period = 1;
  1993. u8 retry_cmd_num = 3;
  1994. u32 reg[SWRM_MAX_INIT_REG];
  1995. u32 value[SWRM_MAX_INIT_REG];
  1996. u32 temp = 0;
  1997. int len = 0;
  1998. /* SW workaround to gate hw_ctl for SWR version >=1.6 */
  1999. if (swrm->version >= SWRM_VERSION_1_6) {
  2000. if (swrm->swrm_hctl_reg) {
  2001. temp = ioread32(swrm->swrm_hctl_reg);
  2002. temp &= 0xFFFFFFFD;
  2003. iowrite32(temp, swrm->swrm_hctl_reg);
  2004. usleep_range(500, 505);
  2005. temp = ioread32(swrm->swrm_hctl_reg);
  2006. dev_dbg(swrm->dev, "%s: hctl_reg val: 0x%x\n",
  2007. __func__, temp);
  2008. }
  2009. }
  2010. ssp_period = swrm_get_ssp_period(swrm, SWRM_ROW_50,
  2011. SWRM_COL_02, SWRM_FRAME_SYNC_SEL);
  2012. dev_dbg(swrm->dev, "%s: ssp_period: %d\n", __func__, ssp_period);
  2013. /* Clear Rows and Cols */
  2014. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  2015. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  2016. ((ssp_period - 1) << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  2017. reg[len] = SWRM_MCP_FRAME_CTRL_BANK(0);
  2018. value[len++] = val;
  2019. /* Set Auto enumeration flag */
  2020. reg[len] = SWRM_ENUMERATOR_CFG;
  2021. value[len++] = 1;
  2022. /* Configure No pings */
  2023. val = swr_master_read(swrm, SWRM_MCP_CFG);
  2024. val &= ~SWRM_NUM_PINGS_MASK;
  2025. val |= (0x1f << SWRM_NUM_PINGS_POS);
  2026. reg[len] = SWRM_MCP_CFG;
  2027. value[len++] = val;
  2028. /* Configure number of retries of a read/write cmd */
  2029. val = (retry_cmd_num);
  2030. reg[len] = SWRM_CMD_FIFO_CFG;
  2031. value[len++] = val;
  2032. reg[len] = SWRM_MCP_BUS_CTRL;
  2033. value[len++] = 0x2;
  2034. /* Set IRQ to PULSE */
  2035. reg[len] = SWRM_COMP_CFG;
  2036. value[len++] = 0x02;
  2037. reg[len] = SWRM_COMP_CFG;
  2038. value[len++] = 0x03;
  2039. reg[len] = SWRM_INTERRUPT_CLEAR;
  2040. value[len++] = 0xFFFFFFFF;
  2041. swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
  2042. /* Mask soundwire interrupts */
  2043. reg[len] = SWRM_INTERRUPT_EN;
  2044. value[len++] = swrm->intr_mask;
  2045. reg[len] = SWRM_CPU1_INTERRUPT_EN;
  2046. value[len++] = swrm->intr_mask;
  2047. swr_master_bulk_write(swrm, reg, value, len);
  2048. if (!swrm_check_link_status(swrm, 0x1)) {
  2049. dev_err(swrm->dev,
  2050. "%s: swr link failed to connect\n",
  2051. __func__);
  2052. for (i = 0; i < len; i++) {
  2053. usleep_range(50, 55);
  2054. dev_err(swrm->dev,
  2055. "%s:reg:0x%x val:0x%x\n",
  2056. __func__,
  2057. reg[i], swr_master_read(swrm, reg[i]));
  2058. }
  2059. return -EINVAL;
  2060. }
  2061. /* Execute it for versions >= 1.5.1 */
  2062. if (swrm->version >= SWRM_VERSION_1_5_1)
  2063. swr_master_write(swrm, SWRM_CMD_FIFO_CFG,
  2064. (swr_master_read(swrm,
  2065. SWRM_CMD_FIFO_CFG) | 0x80000000));
  2066. return ret;
  2067. }
  2068. static int swrm_event_notify(struct notifier_block *self,
  2069. unsigned long action, void *data)
  2070. {
  2071. struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
  2072. event_notifier);
  2073. if (!swrm || !(swrm->dev)) {
  2074. pr_err("%s: swrm or dev is NULL\n", __func__);
  2075. return -EINVAL;
  2076. }
  2077. switch (action) {
  2078. case MSM_AUD_DC_EVENT:
  2079. schedule_work(&(swrm->dc_presence_work));
  2080. break;
  2081. case SWR_WAKE_IRQ_EVENT:
  2082. if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
  2083. swrm->ipc_wakeup_triggered = true;
  2084. pm_stay_awake(swrm->dev);
  2085. schedule_work(&swrm->wakeup_work);
  2086. }
  2087. break;
  2088. default:
  2089. dev_err(swrm->dev, "%s: invalid event type: %lu\n",
  2090. __func__, action);
  2091. return -EINVAL;
  2092. }
  2093. return 0;
  2094. }
  2095. static void swrm_notify_work_fn(struct work_struct *work)
  2096. {
  2097. struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
  2098. dc_presence_work);
  2099. if (!swrm || !swrm->pdev) {
  2100. pr_err("%s: swrm or pdev is NULL\n", __func__);
  2101. return;
  2102. }
  2103. swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
  2104. }
  2105. static int swrm_probe(struct platform_device *pdev)
  2106. {
  2107. struct swr_mstr_ctrl *swrm;
  2108. struct swr_ctrl_platform_data *pdata;
  2109. u32 i, num_ports, port_num, port_type, ch_mask, swrm_hctl_reg = 0;
  2110. u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
  2111. int ret = 0;
  2112. struct clk *lpass_core_hw_vote = NULL;
  2113. struct clk *lpass_core_audio = NULL;
  2114. /* Allocate soundwire master driver structure */
  2115. swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
  2116. GFP_KERNEL);
  2117. if (!swrm) {
  2118. ret = -ENOMEM;
  2119. goto err_memory_fail;
  2120. }
  2121. swrm->pdev = pdev;
  2122. swrm->dev = &pdev->dev;
  2123. platform_set_drvdata(pdev, swrm);
  2124. swr_set_ctrl_data(&swrm->master, swrm);
  2125. pdata = dev_get_platdata(&pdev->dev);
  2126. if (!pdata) {
  2127. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  2128. __func__);
  2129. ret = -EINVAL;
  2130. goto err_pdata_fail;
  2131. }
  2132. swrm->handle = (void *)pdata->handle;
  2133. if (!swrm->handle) {
  2134. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  2135. __func__);
  2136. ret = -EINVAL;
  2137. goto err_pdata_fail;
  2138. }
  2139. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
  2140. &swrm->master_id);
  2141. if (ret) {
  2142. dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
  2143. goto err_pdata_fail;
  2144. }
  2145. ret = of_property_read_u32(pdev->dev.of_node, "qcom,dynamic-port-map-supported",
  2146. &swrm->dynamic_port_map_supported);
  2147. if (ret) {
  2148. dev_dbg(&pdev->dev,
  2149. "%s: failed to get dynamic port map support, use default\n",
  2150. __func__);
  2151. swrm->dynamic_port_map_supported = 1;
  2152. }
  2153. if (!(of_property_read_u32(pdev->dev.of_node,
  2154. "swrm-io-base", &swrm->swrm_base_reg)))
  2155. ret = of_property_read_u32(pdev->dev.of_node,
  2156. "swrm-io-base", &swrm->swrm_base_reg);
  2157. if (!swrm->swrm_base_reg) {
  2158. swrm->read = pdata->read;
  2159. if (!swrm->read) {
  2160. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  2161. __func__);
  2162. ret = -EINVAL;
  2163. goto err_pdata_fail;
  2164. }
  2165. swrm->write = pdata->write;
  2166. if (!swrm->write) {
  2167. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  2168. __func__);
  2169. ret = -EINVAL;
  2170. goto err_pdata_fail;
  2171. }
  2172. swrm->bulk_write = pdata->bulk_write;
  2173. if (!swrm->bulk_write) {
  2174. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  2175. __func__);
  2176. ret = -EINVAL;
  2177. goto err_pdata_fail;
  2178. }
  2179. } else {
  2180. swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
  2181. swrm->swrm_base_reg, SWRM_MAX_REGISTER);
  2182. }
  2183. swrm->core_vote = pdata->core_vote;
  2184. if (!(of_property_read_u32(pdev->dev.of_node,
  2185. "qcom,swrm-hctl-reg", &swrm_hctl_reg)))
  2186. swrm->swrm_hctl_reg = devm_ioremap(&pdev->dev,
  2187. swrm_hctl_reg, 0x4);
  2188. swrm->clk = pdata->clk;
  2189. if (!swrm->clk) {
  2190. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  2191. __func__);
  2192. ret = -EINVAL;
  2193. goto err_pdata_fail;
  2194. }
  2195. if (of_property_read_u32(pdev->dev.of_node,
  2196. "qcom,swr-clock-stop-mode0",
  2197. &swrm->clk_stop_mode0_supp)) {
  2198. swrm->clk_stop_mode0_supp = FALSE;
  2199. }
  2200. ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
  2201. &swrm->num_dev);
  2202. if (ret) {
  2203. dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
  2204. __func__, "qcom,swr-num-dev");
  2205. } else {
  2206. if (swrm->num_dev > SWRM_NUM_AUTO_ENUM_SLAVES) {
  2207. dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
  2208. __func__, swrm->num_dev,
  2209. SWRM_NUM_AUTO_ENUM_SLAVES);
  2210. ret = -EINVAL;
  2211. goto err_pdata_fail;
  2212. }
  2213. }
  2214. /* Parse soundwire port mapping */
  2215. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
  2216. &num_ports);
  2217. if (ret) {
  2218. dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
  2219. goto err_pdata_fail;
  2220. }
  2221. swrm->num_ports = num_ports;
  2222. if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
  2223. &map_size)) {
  2224. dev_err(swrm->dev, "missing port mapping\n");
  2225. goto err_pdata_fail;
  2226. }
  2227. map_length = map_size / (3 * sizeof(u32));
  2228. if (num_ports > SWR_MSTR_PORT_LEN) {
  2229. dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
  2230. __func__);
  2231. ret = -EINVAL;
  2232. goto err_pdata_fail;
  2233. }
  2234. temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
  2235. if (!temp) {
  2236. ret = -ENOMEM;
  2237. goto err_pdata_fail;
  2238. }
  2239. ret = of_property_read_u32_array(pdev->dev.of_node,
  2240. "qcom,swr-port-mapping", temp, 3 * map_length);
  2241. if (ret) {
  2242. dev_err(swrm->dev, "%s: Failed to read port mapping\n",
  2243. __func__);
  2244. goto err_pdata_fail;
  2245. }
  2246. for (i = 0; i < map_length; i++) {
  2247. port_num = temp[3 * i];
  2248. port_type = temp[3 * i + 1];
  2249. ch_mask = temp[3 * i + 2];
  2250. if (port_num != old_port_num)
  2251. ch_iter = 0;
  2252. swrm->port_mapping[port_num][ch_iter].port_type = port_type;
  2253. swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
  2254. old_port_num = port_num;
  2255. }
  2256. devm_kfree(&pdev->dev, temp);
  2257. swrm->reg_irq = pdata->reg_irq;
  2258. swrm->master.read = swrm_read;
  2259. swrm->master.write = swrm_write;
  2260. swrm->master.bulk_write = swrm_bulk_write;
  2261. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  2262. swrm->master.connect_port = swrm_connect_port;
  2263. swrm->master.disconnect_port = swrm_disconnect_port;
  2264. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  2265. swrm->master.remove_from_group = swrm_remove_from_group;
  2266. swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
  2267. swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
  2268. swrm->master.dev.parent = &pdev->dev;
  2269. swrm->master.dev.of_node = pdev->dev.of_node;
  2270. swrm->master.num_port = 0;
  2271. swrm->rcmd_id = 0;
  2272. swrm->wcmd_id = 0;
  2273. swrm->slave_status = 0;
  2274. swrm->num_rx_chs = 0;
  2275. swrm->clk_ref_count = 0;
  2276. swrm->swr_irq_wakeup_capable = 0;
  2277. swrm->mclk_freq = MCLK_FREQ;
  2278. swrm->bus_clk = MCLK_FREQ;
  2279. swrm->dev_up = true;
  2280. swrm->state = SWR_MSTR_UP;
  2281. swrm->ipc_wakeup = false;
  2282. swrm->ipc_wakeup_triggered = false;
  2283. swrm->disable_div2_clk_switch = FALSE;
  2284. init_completion(&swrm->reset);
  2285. init_completion(&swrm->broadcast);
  2286. init_completion(&swrm->clk_off_complete);
  2287. mutex_init(&swrm->irq_lock);
  2288. mutex_init(&swrm->mlock);
  2289. mutex_init(&swrm->reslock);
  2290. mutex_init(&swrm->force_down_lock);
  2291. mutex_init(&swrm->iolock);
  2292. mutex_init(&swrm->clklock);
  2293. mutex_init(&swrm->devlock);
  2294. mutex_init(&swrm->pm_lock);
  2295. swrm->wlock_holders = 0;
  2296. swrm->pm_state = SWRM_PM_SLEEPABLE;
  2297. init_waitqueue_head(&swrm->pm_wq);
  2298. pm_qos_add_request(&swrm->pm_qos_req,
  2299. PM_QOS_CPU_DMA_LATENCY,
  2300. PM_QOS_DEFAULT_VALUE);
  2301. for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
  2302. INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
  2303. if (of_property_read_u32(pdev->dev.of_node,
  2304. "qcom,disable-div2-clk-switch",
  2305. &swrm->disable_div2_clk_switch)) {
  2306. swrm->disable_div2_clk_switch = FALSE;
  2307. }
  2308. /* Register LPASS core hw vote */
  2309. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  2310. if (IS_ERR(lpass_core_hw_vote)) {
  2311. ret = PTR_ERR(lpass_core_hw_vote);
  2312. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  2313. __func__, "lpass_core_hw_vote", ret);
  2314. lpass_core_hw_vote = NULL;
  2315. ret = 0;
  2316. }
  2317. swrm->lpass_core_hw_vote = lpass_core_hw_vote;
  2318. /* Register LPASS audio core vote */
  2319. lpass_core_audio = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  2320. if (IS_ERR(lpass_core_audio)) {
  2321. ret = PTR_ERR(lpass_core_audio);
  2322. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  2323. __func__, "lpass_core_audio", ret);
  2324. lpass_core_audio = NULL;
  2325. ret = 0;
  2326. }
  2327. swrm->lpass_core_audio = lpass_core_audio;
  2328. if (swrm->reg_irq) {
  2329. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  2330. SWR_IRQ_REGISTER);
  2331. if (ret) {
  2332. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  2333. __func__, ret);
  2334. goto err_irq_fail;
  2335. }
  2336. } else {
  2337. swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
  2338. if (swrm->irq < 0) {
  2339. dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
  2340. __func__, swrm->irq);
  2341. goto err_irq_fail;
  2342. }
  2343. ret = request_threaded_irq(swrm->irq, NULL,
  2344. swr_mstr_interrupt,
  2345. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  2346. "swr_master_irq", swrm);
  2347. if (ret) {
  2348. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  2349. __func__, ret);
  2350. goto err_irq_fail;
  2351. }
  2352. }
  2353. /* Make inband tx interrupts as wakeup capable for slave irq */
  2354. ret = of_property_read_u32(pdev->dev.of_node,
  2355. "qcom,swr-mstr-irq-wakeup-capable",
  2356. &swrm->swr_irq_wakeup_capable);
  2357. if (ret)
  2358. dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
  2359. __func__);
  2360. if (swrm->swr_irq_wakeup_capable)
  2361. irq_set_irq_wake(swrm->irq, 1);
  2362. ret = swr_register_master(&swrm->master);
  2363. if (ret) {
  2364. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  2365. goto err_mstr_fail;
  2366. }
  2367. /* Add devices registered with board-info as the
  2368. * controller will be up now
  2369. */
  2370. swr_master_add_boarddevices(&swrm->master);
  2371. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  2372. dev_dbg(&pdev->dev, "%s: Audio HW Vote is failed\n", __func__);
  2373. mutex_lock(&swrm->mlock);
  2374. swrm_clk_request(swrm, true);
  2375. swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
  2376. ret = swrm_master_init(swrm);
  2377. if (ret < 0) {
  2378. dev_err(&pdev->dev,
  2379. "%s: Error in master Initialization , err %d\n",
  2380. __func__, ret);
  2381. mutex_unlock(&swrm->mlock);
  2382. ret = -EPROBE_DEFER;
  2383. goto err_mstr_init_fail;
  2384. }
  2385. mutex_unlock(&swrm->mlock);
  2386. INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
  2387. if (pdev->dev.of_node)
  2388. of_register_swr_devices(&swrm->master);
  2389. #ifdef CONFIG_DEBUG_FS
  2390. swrm->debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  2391. if (!IS_ERR(swrm->debugfs_swrm_dent)) {
  2392. swrm->debugfs_peek = debugfs_create_file("swrm_peek",
  2393. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2394. (void *) swrm, &swrm_debug_read_ops);
  2395. swrm->debugfs_poke = debugfs_create_file("swrm_poke",
  2396. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2397. (void *) swrm, &swrm_debug_write_ops);
  2398. swrm->debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  2399. S_IFREG | 0444, swrm->debugfs_swrm_dent,
  2400. (void *) swrm,
  2401. &swrm_debug_dump_ops);
  2402. }
  2403. #endif
  2404. ret = device_init_wakeup(swrm->dev, true);
  2405. if (ret) {
  2406. dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
  2407. goto err_irq_wakeup_fail;
  2408. }
  2409. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  2410. pm_runtime_use_autosuspend(&pdev->dev);
  2411. pm_runtime_set_active(&pdev->dev);
  2412. pm_runtime_enable(&pdev->dev);
  2413. pm_runtime_mark_last_busy(&pdev->dev);
  2414. INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
  2415. swrm->event_notifier.notifier_call = swrm_event_notify;
  2416. //msm_aud_evt_register_client(&swrm->event_notifier);
  2417. return 0;
  2418. err_irq_wakeup_fail:
  2419. device_init_wakeup(swrm->dev, false);
  2420. err_mstr_init_fail:
  2421. swr_unregister_master(&swrm->master);
  2422. err_mstr_fail:
  2423. if (swrm->reg_irq) {
  2424. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  2425. swrm, SWR_IRQ_FREE);
  2426. } else if (swrm->irq) {
  2427. irqd_set_trigger_type(
  2428. irq_get_irq_data(swrm->irq),
  2429. IRQ_TYPE_NONE);
  2430. if (swrm->swr_irq_wakeup_capable)
  2431. irq_set_irq_wake(swrm->irq, 0);
  2432. free_irq(swrm->irq, swrm);
  2433. }
  2434. err_irq_fail:
  2435. mutex_destroy(&swrm->irq_lock);
  2436. mutex_destroy(&swrm->mlock);
  2437. mutex_destroy(&swrm->reslock);
  2438. mutex_destroy(&swrm->force_down_lock);
  2439. mutex_destroy(&swrm->iolock);
  2440. mutex_destroy(&swrm->clklock);
  2441. mutex_destroy(&swrm->pm_lock);
  2442. pm_qos_remove_request(&swrm->pm_qos_req);
  2443. err_pdata_fail:
  2444. err_memory_fail:
  2445. return ret;
  2446. }
  2447. static int swrm_remove(struct platform_device *pdev)
  2448. {
  2449. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2450. if (swrm->reg_irq) {
  2451. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  2452. swrm, SWR_IRQ_FREE);
  2453. } else if (swrm->irq) {
  2454. irqd_set_trigger_type(
  2455. irq_get_irq_data(swrm->irq),
  2456. IRQ_TYPE_NONE);
  2457. if (swrm->swr_irq_wakeup_capable)
  2458. irq_set_irq_wake(swrm->irq, 0);
  2459. free_irq(swrm->irq, swrm);
  2460. } else if (swrm->wake_irq > 0) {
  2461. free_irq(swrm->wake_irq, swrm);
  2462. }
  2463. cancel_work_sync(&swrm->wakeup_work);
  2464. pm_runtime_disable(&pdev->dev);
  2465. pm_runtime_set_suspended(&pdev->dev);
  2466. swr_unregister_master(&swrm->master);
  2467. //msm_aud_evt_unregister_client(&swrm->event_notifier);
  2468. device_init_wakeup(swrm->dev, false);
  2469. mutex_destroy(&swrm->irq_lock);
  2470. mutex_destroy(&swrm->mlock);
  2471. mutex_destroy(&swrm->reslock);
  2472. mutex_destroy(&swrm->iolock);
  2473. mutex_destroy(&swrm->clklock);
  2474. mutex_destroy(&swrm->force_down_lock);
  2475. mutex_destroy(&swrm->pm_lock);
  2476. pm_qos_remove_request(&swrm->pm_qos_req);
  2477. devm_kfree(&pdev->dev, swrm);
  2478. return 0;
  2479. }
  2480. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  2481. {
  2482. u32 val;
  2483. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  2484. swr_master_write(swrm, SWRM_INTERRUPT_EN, 0x1FDFD);
  2485. val = swr_master_read(swrm, SWRM_MCP_CFG);
  2486. val |= 0x02;
  2487. swr_master_write(swrm, SWRM_MCP_CFG, val);
  2488. return 0;
  2489. }
  2490. #ifdef CONFIG_PM
  2491. static int swrm_runtime_resume(struct device *dev)
  2492. {
  2493. struct platform_device *pdev = to_platform_device(dev);
  2494. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2495. int ret = 0;
  2496. bool swrm_clk_req_err = false;
  2497. bool hw_core_err = false;
  2498. struct swr_master *mstr = &swrm->master;
  2499. struct swr_device *swr_dev;
  2500. u32 temp = 0;
  2501. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  2502. __func__, swrm->state);
  2503. trace_printk("%s: pm_runtime: resume, state:%d\n",
  2504. __func__, swrm->state);
  2505. mutex_lock(&swrm->reslock);
  2506. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  2507. dev_err(dev, "%s:lpass core hw enable failed\n",
  2508. __func__);
  2509. hw_core_err = true;
  2510. }
  2511. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, true))
  2512. dev_err(dev, "%s:lpass audio hw enable failed\n",
  2513. __func__);
  2514. if ((swrm->state == SWR_MSTR_DOWN) ||
  2515. (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
  2516. if (swrm->clk_stop_mode0_supp) {
  2517. if (swrm->wake_irq > 0) {
  2518. if (unlikely(!irq_get_irq_data
  2519. (swrm->wake_irq))) {
  2520. pr_err("%s: irq data is NULL\n",
  2521. __func__);
  2522. mutex_unlock(&swrm->reslock);
  2523. return IRQ_NONE;
  2524. }
  2525. mutex_lock(&swrm->irq_lock);
  2526. if (!irqd_irq_disabled(
  2527. irq_get_irq_data(swrm->wake_irq)))
  2528. disable_irq_nosync(swrm->wake_irq);
  2529. mutex_unlock(&swrm->irq_lock);
  2530. }
  2531. if (swrm->ipc_wakeup)
  2532. dev_err(dev, "%s:notifications disabled\n", __func__);
  2533. // msm_aud_evt_blocking_notifier_call_chain(
  2534. // SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  2535. }
  2536. if (swrm_clk_request(swrm, true)) {
  2537. /*
  2538. * Set autosuspend timer to 1 for
  2539. * master to enter into suspend.
  2540. */
  2541. swrm_clk_req_err = true;
  2542. goto exit;
  2543. }
  2544. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  2545. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2546. ret = swr_device_up(swr_dev);
  2547. if (ret == -ENODEV) {
  2548. dev_dbg(dev,
  2549. "%s slave device up not implemented\n",
  2550. __func__);
  2551. trace_printk(
  2552. "%s slave device up not implemented\n",
  2553. __func__);
  2554. ret = 0;
  2555. } else if (ret) {
  2556. dev_err(dev,
  2557. "%s: failed to wakeup swr dev %d\n",
  2558. __func__, swr_dev->dev_num);
  2559. swrm_clk_request(swrm, false);
  2560. goto exit;
  2561. }
  2562. }
  2563. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  2564. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  2565. swr_master_write(swrm, SWRM_MCP_BUS_CTRL, 0x01);
  2566. swrm_master_init(swrm);
  2567. /* wait for hw enumeration to complete */
  2568. usleep_range(100, 105);
  2569. if (!swrm_check_link_status(swrm, 0x1))
  2570. dev_dbg(dev, "%s:failed in connecting, ssr?\n",
  2571. __func__);
  2572. swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
  2573. SWRS_SCP_INT_STATUS_MASK_1);
  2574. if (swrm->state == SWR_MSTR_SSR) {
  2575. mutex_unlock(&swrm->reslock);
  2576. enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
  2577. mutex_lock(&swrm->reslock);
  2578. }
  2579. } else {
  2580. if (swrm->swrm_hctl_reg) {
  2581. temp = ioread32(swrm->swrm_hctl_reg);
  2582. temp &= 0xFFFFFFFD;
  2583. iowrite32(temp, swrm->swrm_hctl_reg);
  2584. }
  2585. /*wake up from clock stop*/
  2586. swr_master_write(swrm, SWRM_MCP_BUS_CTRL, 0x2);
  2587. /* clear and enable bus clash interrupt */
  2588. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x08);
  2589. swrm->intr_mask |= 0x08;
  2590. swr_master_write(swrm, SWRM_INTERRUPT_EN,
  2591. swrm->intr_mask);
  2592. swr_master_write(swrm,
  2593. SWRM_CPU1_INTERRUPT_EN,
  2594. swrm->intr_mask);
  2595. usleep_range(100, 105);
  2596. if (!swrm_check_link_status(swrm, 0x1))
  2597. dev_dbg(dev, "%s:failed in connecting, ssr?\n",
  2598. __func__);
  2599. }
  2600. swrm->state = SWR_MSTR_UP;
  2601. }
  2602. exit:
  2603. if (!hw_core_err)
  2604. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  2605. if (swrm_clk_req_err)
  2606. pm_runtime_set_autosuspend_delay(&pdev->dev,
  2607. ERR_AUTO_SUSPEND_TIMER_VAL);
  2608. else
  2609. pm_runtime_set_autosuspend_delay(&pdev->dev,
  2610. auto_suspend_timer);
  2611. if (swrm->req_clk_switch)
  2612. swrm->req_clk_switch = false;
  2613. mutex_unlock(&swrm->reslock);
  2614. trace_printk("%s: pm_runtime: resume done, state:%d\n",
  2615. __func__, swrm->state);
  2616. return ret;
  2617. }
  2618. static int swrm_runtime_suspend(struct device *dev)
  2619. {
  2620. struct platform_device *pdev = to_platform_device(dev);
  2621. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2622. int ret = 0;
  2623. bool hw_core_err = false;
  2624. struct swr_master *mstr = &swrm->master;
  2625. struct swr_device *swr_dev;
  2626. int current_state = 0;
  2627. trace_printk("%s: pm_runtime: suspend state: %d\n",
  2628. __func__, swrm->state);
  2629. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  2630. __func__, swrm->state);
  2631. mutex_lock(&swrm->reslock);
  2632. mutex_lock(&swrm->force_down_lock);
  2633. current_state = swrm->state;
  2634. mutex_unlock(&swrm->force_down_lock);
  2635. if (swrm_request_hw_vote(swrm, LPASS_HW_CORE, true)) {
  2636. dev_err(dev, "%s:lpass core hw enable failed\n",
  2637. __func__);
  2638. hw_core_err = true;
  2639. }
  2640. if ((current_state == SWR_MSTR_UP) ||
  2641. (current_state == SWR_MSTR_SSR)) {
  2642. if ((current_state != SWR_MSTR_SSR) &&
  2643. swrm_is_port_en(&swrm->master)) {
  2644. dev_dbg(dev, "%s ports are enabled\n", __func__);
  2645. trace_printk("%s ports are enabled\n", __func__);
  2646. ret = -EBUSY;
  2647. goto exit;
  2648. }
  2649. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  2650. dev_err(dev, "%s: clk stop mode not supported or SSR entry\n",
  2651. __func__);
  2652. mutex_unlock(&swrm->reslock);
  2653. enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
  2654. mutex_lock(&swrm->reslock);
  2655. swrm_clk_pause(swrm);
  2656. swr_master_write(swrm, SWRM_COMP_CFG, 0x00);
  2657. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2658. ret = swr_device_down(swr_dev);
  2659. if (ret == -ENODEV) {
  2660. dev_dbg_ratelimited(dev,
  2661. "%s slave device down not implemented\n",
  2662. __func__);
  2663. trace_printk(
  2664. "%s slave device down not implemented\n",
  2665. __func__);
  2666. ret = 0;
  2667. } else if (ret) {
  2668. dev_err(dev,
  2669. "%s: failed to shutdown swr dev %d\n",
  2670. __func__, swr_dev->dev_num);
  2671. trace_printk(
  2672. "%s: failed to shutdown swr dev %d\n",
  2673. __func__, swr_dev->dev_num);
  2674. goto exit;
  2675. }
  2676. }
  2677. trace_printk("%s: clk stop mode not supported or SSR exit\n",
  2678. __func__);
  2679. } else {
  2680. /* Mask bus clash interrupt */
  2681. swrm->intr_mask &= ~((u32)0x08);
  2682. swr_master_write(swrm, SWRM_INTERRUPT_EN,
  2683. swrm->intr_mask);
  2684. swr_master_write(swrm,
  2685. SWRM_CPU1_INTERRUPT_EN,
  2686. swrm->intr_mask);
  2687. mutex_unlock(&swrm->reslock);
  2688. /* clock stop sequence */
  2689. swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
  2690. SWRS_SCP_CONTROL);
  2691. mutex_lock(&swrm->reslock);
  2692. usleep_range(100, 105);
  2693. }
  2694. if (!swrm_check_link_status(swrm, 0x0))
  2695. dev_dbg(dev, "%s:failed in disconnecting, ssr?\n",
  2696. __func__);
  2697. ret = swrm_clk_request(swrm, false);
  2698. if (ret) {
  2699. dev_err(dev, "%s: swrmn clk failed\n", __func__);
  2700. ret = 0;
  2701. goto exit;
  2702. }
  2703. if (swrm->clk_stop_mode0_supp) {
  2704. if (swrm->wake_irq > 0) {
  2705. enable_irq(swrm->wake_irq);
  2706. } else if (swrm->ipc_wakeup) {
  2707. //msm_aud_evt_blocking_notifier_call_chain(
  2708. // SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  2709. dev_err(dev, "%s:notifications disabled\n", __func__);
  2710. swrm->ipc_wakeup_triggered = false;
  2711. }
  2712. }
  2713. }
  2714. if (swrm_request_hw_vote(swrm, LPASS_AUDIO_CORE, false))
  2715. dev_dbg(dev, "%s:lpass audio hw enable failed\n",
  2716. __func__);
  2717. /* Retain SSR state until resume */
  2718. if (current_state != SWR_MSTR_SSR)
  2719. swrm->state = SWR_MSTR_DOWN;
  2720. exit:
  2721. if (!hw_core_err)
  2722. swrm_request_hw_vote(swrm, LPASS_HW_CORE, false);
  2723. mutex_unlock(&swrm->reslock);
  2724. trace_printk("%s: pm_runtime: suspend done state: %d\n",
  2725. __func__, swrm->state);
  2726. return ret;
  2727. }
  2728. #endif /* CONFIG_PM */
  2729. static int swrm_device_suspend(struct device *dev)
  2730. {
  2731. struct platform_device *pdev = to_platform_device(dev);
  2732. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2733. int ret = 0;
  2734. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  2735. trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
  2736. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  2737. ret = swrm_runtime_suspend(dev);
  2738. if (!ret) {
  2739. pm_runtime_disable(dev);
  2740. pm_runtime_set_suspended(dev);
  2741. pm_runtime_enable(dev);
  2742. }
  2743. }
  2744. return 0;
  2745. }
  2746. static int swrm_device_down(struct device *dev)
  2747. {
  2748. struct platform_device *pdev = to_platform_device(dev);
  2749. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2750. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  2751. trace_printk("%s: swrm state: %d\n", __func__, swrm->state);
  2752. mutex_lock(&swrm->force_down_lock);
  2753. swrm->state = SWR_MSTR_SSR;
  2754. mutex_unlock(&swrm->force_down_lock);
  2755. swrm_device_suspend(dev);
  2756. return 0;
  2757. }
  2758. int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
  2759. {
  2760. int ret = 0;
  2761. int irq, dir_apps_irq;
  2762. if (!swrm->ipc_wakeup) {
  2763. irq = of_get_named_gpio(swrm->dev->of_node,
  2764. "qcom,swr-wakeup-irq", 0);
  2765. if (gpio_is_valid(irq)) {
  2766. swrm->wake_irq = gpio_to_irq(irq);
  2767. if (swrm->wake_irq < 0) {
  2768. dev_err(swrm->dev,
  2769. "Unable to configure irq\n");
  2770. return swrm->wake_irq;
  2771. }
  2772. } else {
  2773. dir_apps_irq = platform_get_irq_byname(swrm->pdev,
  2774. "swr_wake_irq");
  2775. if (dir_apps_irq < 0) {
  2776. dev_err(swrm->dev,
  2777. "TLMM connect gpio not found\n");
  2778. return -EINVAL;
  2779. }
  2780. swrm->wake_irq = dir_apps_irq;
  2781. }
  2782. ret = request_threaded_irq(swrm->wake_irq, NULL,
  2783. swrm_wakeup_interrupt,
  2784. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  2785. "swr_wake_irq", swrm);
  2786. if (ret) {
  2787. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  2788. __func__, ret);
  2789. return -EINVAL;
  2790. }
  2791. irq_set_irq_wake(swrm->wake_irq, 1);
  2792. }
  2793. return ret;
  2794. }
  2795. static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
  2796. u32 uc, u32 size)
  2797. {
  2798. if (!swrm->port_param) {
  2799. swrm->port_param = devm_kzalloc(dev,
  2800. sizeof(swrm->port_param) * SWR_UC_MAX,
  2801. GFP_KERNEL);
  2802. if (!swrm->port_param)
  2803. return -ENOMEM;
  2804. }
  2805. if (!swrm->port_param[uc]) {
  2806. swrm->port_param[uc] = devm_kcalloc(dev, size,
  2807. sizeof(struct port_params),
  2808. GFP_KERNEL);
  2809. if (!swrm->port_param[uc])
  2810. return -ENOMEM;
  2811. } else {
  2812. dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
  2813. __func__);
  2814. }
  2815. return 0;
  2816. }
  2817. static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
  2818. struct swrm_port_config *port_cfg,
  2819. u32 size)
  2820. {
  2821. int idx;
  2822. struct port_params *params;
  2823. int uc = port_cfg->uc;
  2824. int ret = 0;
  2825. for (idx = 0; idx < size; idx++) {
  2826. params = &((struct port_params *)port_cfg->params)[idx];
  2827. if (!params) {
  2828. dev_err(swrm->dev, "%s: Invalid params\n", __func__);
  2829. ret = -EINVAL;
  2830. break;
  2831. }
  2832. memcpy(&swrm->port_param[uc][idx], params,
  2833. sizeof(struct port_params));
  2834. }
  2835. return ret;
  2836. }
  2837. /**
  2838. * swrm_wcd_notify - parent device can notify to soundwire master through
  2839. * this function
  2840. * @pdev: pointer to platform device structure
  2841. * @id: command id from parent to the soundwire master
  2842. * @data: data from parent device to soundwire master
  2843. */
  2844. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  2845. {
  2846. struct swr_mstr_ctrl *swrm;
  2847. int ret = 0;
  2848. struct swr_master *mstr;
  2849. struct swr_device *swr_dev;
  2850. struct swrm_port_config *port_cfg;
  2851. if (!pdev) {
  2852. pr_err("%s: pdev is NULL\n", __func__);
  2853. return -EINVAL;
  2854. }
  2855. swrm = platform_get_drvdata(pdev);
  2856. if (!swrm) {
  2857. dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
  2858. return -EINVAL;
  2859. }
  2860. mstr = &swrm->master;
  2861. switch (id) {
  2862. case SWR_REQ_CLK_SWITCH:
  2863. /* This will put soundwire in clock stop mode and disable the
  2864. * clocks, if there is no active usecase running, so that the
  2865. * next activity on soundwire will request clock from new clock
  2866. * source.
  2867. */
  2868. if (!data) {
  2869. dev_err(swrm->dev, "%s: data is NULL for id:%d\n",
  2870. __func__, id);
  2871. ret = -EINVAL;
  2872. break;
  2873. }
  2874. mutex_lock(&swrm->mlock);
  2875. if (swrm->clk_src != *(int *)data) {
  2876. if (swrm->state == SWR_MSTR_UP) {
  2877. swrm->req_clk_switch = true;
  2878. swrm_device_suspend(&pdev->dev);
  2879. if (swrm->state == SWR_MSTR_UP)
  2880. swrm->req_clk_switch = false;
  2881. }
  2882. swrm->clk_src = *(int *)data;
  2883. }
  2884. mutex_unlock(&swrm->mlock);
  2885. break;
  2886. case SWR_CLK_FREQ:
  2887. if (!data) {
  2888. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  2889. ret = -EINVAL;
  2890. } else {
  2891. mutex_lock(&swrm->mlock);
  2892. if (swrm->mclk_freq != *(int *)data) {
  2893. dev_dbg(swrm->dev, "%s: freq change: force mstr down\n", __func__);
  2894. if (swrm->state == SWR_MSTR_DOWN)
  2895. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  2896. __func__, swrm->state);
  2897. else {
  2898. swrm->mclk_freq = *(int *)data;
  2899. swrm->bus_clk = swrm->mclk_freq;
  2900. swrm_switch_frame_shape(swrm,
  2901. swrm->bus_clk);
  2902. swrm_device_suspend(&pdev->dev);
  2903. }
  2904. /*
  2905. * add delay to ensure clk release happen
  2906. * if interrupt triggered for clk stop,
  2907. * wait for it to exit
  2908. */
  2909. usleep_range(10000, 10500);
  2910. }
  2911. swrm->mclk_freq = *(int *)data;
  2912. swrm->bus_clk = swrm->mclk_freq;
  2913. mutex_unlock(&swrm->mlock);
  2914. }
  2915. break;
  2916. case SWR_DEVICE_SSR_DOWN:
  2917. trace_printk("%s: swr device down called\n", __func__);
  2918. mutex_lock(&swrm->mlock);
  2919. if (swrm->state == SWR_MSTR_DOWN)
  2920. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  2921. __func__, swrm->state);
  2922. else
  2923. swrm_device_down(&pdev->dev);
  2924. mutex_lock(&swrm->devlock);
  2925. swrm->dev_up = false;
  2926. swrm->hw_core_clk_en = 0;
  2927. swrm->aud_core_clk_en = 0;
  2928. mutex_unlock(&swrm->devlock);
  2929. mutex_lock(&swrm->reslock);
  2930. swrm->state = SWR_MSTR_SSR;
  2931. mutex_unlock(&swrm->reslock);
  2932. mutex_unlock(&swrm->mlock);
  2933. break;
  2934. case SWR_DEVICE_SSR_UP:
  2935. /* wait for clk voting to be zero */
  2936. trace_printk("%s: swr device up called\n", __func__);
  2937. reinit_completion(&swrm->clk_off_complete);
  2938. if (swrm->clk_ref_count &&
  2939. !wait_for_completion_timeout(&swrm->clk_off_complete,
  2940. msecs_to_jiffies(500)))
  2941. dev_err(swrm->dev, "%s: clock voting not zero\n",
  2942. __func__);
  2943. mutex_lock(&swrm->devlock);
  2944. swrm->dev_up = true;
  2945. mutex_unlock(&swrm->devlock);
  2946. break;
  2947. case SWR_DEVICE_DOWN:
  2948. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  2949. trace_printk("%s: swr master down called\n", __func__);
  2950. mutex_lock(&swrm->mlock);
  2951. if (swrm->state == SWR_MSTR_DOWN)
  2952. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  2953. __func__, swrm->state);
  2954. else
  2955. swrm_device_down(&pdev->dev);
  2956. mutex_unlock(&swrm->mlock);
  2957. break;
  2958. case SWR_DEVICE_UP:
  2959. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  2960. trace_printk("%s: swr master up called\n", __func__);
  2961. mutex_lock(&swrm->devlock);
  2962. if (!swrm->dev_up) {
  2963. dev_dbg(swrm->dev, "SSR not complete yet\n");
  2964. mutex_unlock(&swrm->devlock);
  2965. return -EBUSY;
  2966. }
  2967. mutex_unlock(&swrm->devlock);
  2968. mutex_lock(&swrm->mlock);
  2969. pm_runtime_mark_last_busy(&pdev->dev);
  2970. pm_runtime_get_sync(&pdev->dev);
  2971. mutex_lock(&swrm->reslock);
  2972. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2973. ret = swr_reset_device(swr_dev);
  2974. if (ret == -ENODEV) {
  2975. dev_dbg_ratelimited(swrm->dev,
  2976. "%s slave reset not implemented\n",
  2977. __func__);
  2978. ret = 0;
  2979. } else if (ret) {
  2980. dev_err(swrm->dev,
  2981. "%s: failed to reset swr device %d\n",
  2982. __func__, swr_dev->dev_num);
  2983. swrm_clk_request(swrm, false);
  2984. }
  2985. }
  2986. pm_runtime_mark_last_busy(&pdev->dev);
  2987. pm_runtime_put_autosuspend(&pdev->dev);
  2988. mutex_unlock(&swrm->reslock);
  2989. mutex_unlock(&swrm->mlock);
  2990. break;
  2991. case SWR_SET_NUM_RX_CH:
  2992. if (!data) {
  2993. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  2994. ret = -EINVAL;
  2995. } else {
  2996. mutex_lock(&swrm->mlock);
  2997. swrm->num_rx_chs = *(int *)data;
  2998. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  2999. list_for_each_entry(swr_dev, &mstr->devices,
  3000. dev_list) {
  3001. ret = swr_set_device_group(swr_dev,
  3002. SWR_BROADCAST);
  3003. if (ret)
  3004. dev_err(swrm->dev,
  3005. "%s: set num ch failed\n",
  3006. __func__);
  3007. }
  3008. } else {
  3009. list_for_each_entry(swr_dev, &mstr->devices,
  3010. dev_list) {
  3011. ret = swr_set_device_group(swr_dev,
  3012. SWR_GROUP_NONE);
  3013. if (ret)
  3014. dev_err(swrm->dev,
  3015. "%s: set num ch failed\n",
  3016. __func__);
  3017. }
  3018. }
  3019. mutex_unlock(&swrm->mlock);
  3020. }
  3021. break;
  3022. case SWR_REGISTER_WAKE_IRQ:
  3023. if (!data) {
  3024. dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
  3025. __func__);
  3026. ret = -EINVAL;
  3027. } else {
  3028. mutex_lock(&swrm->mlock);
  3029. swrm->ipc_wakeup = *(u32 *)data;
  3030. ret = swrm_register_wake_irq(swrm);
  3031. if (ret)
  3032. dev_err(swrm->dev, "%s: register wake_irq failed\n",
  3033. __func__);
  3034. mutex_unlock(&swrm->mlock);
  3035. }
  3036. break;
  3037. case SWR_REGISTER_WAKEUP:
  3038. //msm_aud_evt_blocking_notifier_call_chain(
  3039. // SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  3040. break;
  3041. case SWR_DEREGISTER_WAKEUP:
  3042. //msm_aud_evt_blocking_notifier_call_chain(
  3043. // SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  3044. break;
  3045. case SWR_SET_PORT_MAP:
  3046. if (!data) {
  3047. dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
  3048. __func__, id);
  3049. ret = -EINVAL;
  3050. } else {
  3051. mutex_lock(&swrm->mlock);
  3052. port_cfg = (struct swrm_port_config *)data;
  3053. if (!port_cfg->size) {
  3054. ret = -EINVAL;
  3055. goto done;
  3056. }
  3057. ret = swrm_alloc_port_mem(&pdev->dev, swrm,
  3058. port_cfg->uc, port_cfg->size);
  3059. if (!ret)
  3060. swrm_copy_port_config(swrm, port_cfg,
  3061. port_cfg->size);
  3062. done:
  3063. mutex_unlock(&swrm->mlock);
  3064. }
  3065. break;
  3066. default:
  3067. dev_err(swrm->dev, "%s: swr master unknown id %d\n",
  3068. __func__, id);
  3069. break;
  3070. }
  3071. return ret;
  3072. }
  3073. EXPORT_SYMBOL(swrm_wcd_notify);
  3074. /*
  3075. * swrm_pm_cmpxchg:
  3076. * Check old state and exchange with pm new state
  3077. * if old state matches with current state
  3078. *
  3079. * @swrm: pointer to wcd core resource
  3080. * @o: pm old state
  3081. * @n: pm new state
  3082. *
  3083. * Returns old state
  3084. */
  3085. static enum swrm_pm_state swrm_pm_cmpxchg(
  3086. struct swr_mstr_ctrl *swrm,
  3087. enum swrm_pm_state o,
  3088. enum swrm_pm_state n)
  3089. {
  3090. enum swrm_pm_state old;
  3091. if (!swrm)
  3092. return o;
  3093. mutex_lock(&swrm->pm_lock);
  3094. old = swrm->pm_state;
  3095. if (old == o)
  3096. swrm->pm_state = n;
  3097. mutex_unlock(&swrm->pm_lock);
  3098. return old;
  3099. }
  3100. static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
  3101. {
  3102. enum swrm_pm_state os;
  3103. /*
  3104. * swrm_{lock/unlock}_sleep will be called by swr irq handler
  3105. * and slave wake up requests..
  3106. *
  3107. * If system didn't resume, we can simply return false so
  3108. * IRQ handler can return without handling IRQ.
  3109. */
  3110. mutex_lock(&swrm->pm_lock);
  3111. if (swrm->wlock_holders++ == 0) {
  3112. dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
  3113. pm_qos_update_request(&swrm->pm_qos_req,
  3114. msm_cpuidle_get_deep_idle_latency());
  3115. pm_stay_awake(swrm->dev);
  3116. }
  3117. mutex_unlock(&swrm->pm_lock);
  3118. if (!wait_event_timeout(swrm->pm_wq,
  3119. ((os = swrm_pm_cmpxchg(swrm,
  3120. SWRM_PM_SLEEPABLE,
  3121. SWRM_PM_AWAKE)) ==
  3122. SWRM_PM_SLEEPABLE ||
  3123. (os == SWRM_PM_AWAKE)),
  3124. msecs_to_jiffies(
  3125. SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
  3126. dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
  3127. __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
  3128. swrm->wlock_holders);
  3129. swrm_unlock_sleep(swrm);
  3130. return false;
  3131. }
  3132. wake_up_all(&swrm->pm_wq);
  3133. return true;
  3134. }
  3135. static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
  3136. {
  3137. mutex_lock(&swrm->pm_lock);
  3138. if (--swrm->wlock_holders == 0) {
  3139. dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
  3140. __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
  3141. /*
  3142. * if swrm_lock_sleep failed, pm_state would be still
  3143. * swrm_PM_ASLEEP, don't overwrite
  3144. */
  3145. if (likely(swrm->pm_state == SWRM_PM_AWAKE))
  3146. swrm->pm_state = SWRM_PM_SLEEPABLE;
  3147. pm_qos_update_request(&swrm->pm_qos_req,
  3148. PM_QOS_DEFAULT_VALUE);
  3149. pm_relax(swrm->dev);
  3150. }
  3151. mutex_unlock(&swrm->pm_lock);
  3152. wake_up_all(&swrm->pm_wq);
  3153. }
  3154. #ifdef CONFIG_PM_SLEEP
  3155. static int swrm_suspend(struct device *dev)
  3156. {
  3157. int ret = -EBUSY;
  3158. struct platform_device *pdev = to_platform_device(dev);
  3159. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3160. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  3161. mutex_lock(&swrm->pm_lock);
  3162. if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
  3163. dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
  3164. __func__, swrm->pm_state,
  3165. swrm->wlock_holders);
  3166. swrm->pm_state = SWRM_PM_ASLEEP;
  3167. } else if (swrm->pm_state == SWRM_PM_AWAKE) {
  3168. /*
  3169. * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
  3170. * then set to SWRM_PM_ASLEEP
  3171. */
  3172. dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
  3173. __func__, swrm->pm_state,
  3174. swrm->wlock_holders);
  3175. mutex_unlock(&swrm->pm_lock);
  3176. if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
  3177. swrm, SWRM_PM_SLEEPABLE,
  3178. SWRM_PM_ASLEEP) ==
  3179. SWRM_PM_SLEEPABLE,
  3180. msecs_to_jiffies(
  3181. SWRM_SYS_SUSPEND_WAIT)))) {
  3182. dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
  3183. __func__, swrm->pm_state,
  3184. swrm->wlock_holders);
  3185. return -EBUSY;
  3186. } else {
  3187. dev_dbg(swrm->dev,
  3188. "%s: done, state %d, wlock %d\n",
  3189. __func__, swrm->pm_state,
  3190. swrm->wlock_holders);
  3191. }
  3192. mutex_lock(&swrm->pm_lock);
  3193. } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
  3194. dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
  3195. __func__, swrm->pm_state,
  3196. swrm->wlock_holders);
  3197. }
  3198. mutex_unlock(&swrm->pm_lock);
  3199. if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
  3200. ret = swrm_runtime_suspend(dev);
  3201. if (!ret) {
  3202. /*
  3203. * Synchronize runtime-pm and system-pm states:
  3204. * At this point, we are already suspended. If
  3205. * runtime-pm still thinks its active, then
  3206. * make sure its status is in sync with HW
  3207. * status. The three below calls let the
  3208. * runtime-pm know that we are suspended
  3209. * already without re-invoking the suspend
  3210. * callback
  3211. */
  3212. pm_runtime_disable(dev);
  3213. pm_runtime_set_suspended(dev);
  3214. pm_runtime_enable(dev);
  3215. }
  3216. }
  3217. if (ret == -EBUSY) {
  3218. /*
  3219. * There is a possibility that some audio stream is active
  3220. * during suspend. We dont want to return suspend failure in
  3221. * that case so that display and relevant components can still
  3222. * go to suspend.
  3223. * If there is some other error, then it should be passed-on
  3224. * to system level suspend
  3225. */
  3226. ret = 0;
  3227. }
  3228. return ret;
  3229. }
  3230. static int swrm_resume(struct device *dev)
  3231. {
  3232. int ret = 0;
  3233. struct platform_device *pdev = to_platform_device(dev);
  3234. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  3235. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  3236. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  3237. ret = swrm_runtime_resume(dev);
  3238. if (!ret) {
  3239. pm_runtime_mark_last_busy(dev);
  3240. pm_request_autosuspend(dev);
  3241. }
  3242. }
  3243. mutex_lock(&swrm->pm_lock);
  3244. if (swrm->pm_state == SWRM_PM_ASLEEP) {
  3245. dev_dbg(swrm->dev,
  3246. "%s: resuming system, state %d, wlock %d\n",
  3247. __func__, swrm->pm_state,
  3248. swrm->wlock_holders);
  3249. swrm->pm_state = SWRM_PM_SLEEPABLE;
  3250. } else {
  3251. dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
  3252. __func__, swrm->pm_state,
  3253. swrm->wlock_holders);
  3254. }
  3255. mutex_unlock(&swrm->pm_lock);
  3256. wake_up_all(&swrm->pm_wq);
  3257. return ret;
  3258. }
  3259. #endif /* CONFIG_PM_SLEEP */
  3260. static const struct dev_pm_ops swrm_dev_pm_ops = {
  3261. SET_SYSTEM_SLEEP_PM_OPS(
  3262. swrm_suspend,
  3263. swrm_resume
  3264. )
  3265. SET_RUNTIME_PM_OPS(
  3266. swrm_runtime_suspend,
  3267. swrm_runtime_resume,
  3268. NULL
  3269. )
  3270. };
  3271. static const struct of_device_id swrm_dt_match[] = {
  3272. {
  3273. .compatible = "qcom,swr-mstr",
  3274. },
  3275. {}
  3276. };
  3277. static struct platform_driver swr_mstr_driver = {
  3278. .probe = swrm_probe,
  3279. .remove = swrm_remove,
  3280. .driver = {
  3281. .name = SWR_WCD_NAME,
  3282. .owner = THIS_MODULE,
  3283. .pm = &swrm_dev_pm_ops,
  3284. .of_match_table = swrm_dt_match,
  3285. .suppress_bind_attrs = true,
  3286. },
  3287. };
  3288. static int __init swrm_init(void)
  3289. {
  3290. return platform_driver_register(&swr_mstr_driver);
  3291. }
  3292. module_init(swrm_init);
  3293. static void __exit swrm_exit(void)
  3294. {
  3295. platform_driver_unregister(&swr_mstr_driver);
  3296. }
  3297. module_exit(swrm_exit);
  3298. MODULE_LICENSE("GPL v2");
  3299. MODULE_DESCRIPTION("SoundWire Master Controller");
  3300. MODULE_ALIAS("platform:swr-mstr");