swr-mstr-ctrl.c 80 KB

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