swr-mstr-ctrl.c 89 KB

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