swr-mstr-ctrl.c 82 KB

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