swr-mstr-ctrl.c 82 KB

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