swr-mstr-ctrl.c 86 KB

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