swr-mstr-ctrl.c 91 KB

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