swr-mstr-ctrl.c 83 KB

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