swr-mstr-ctrl.c 93 KB

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