swr-mstr-ctrl.c 77 KB

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