swr-mstr-ctrl.c 94 KB

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