swr-mstr-ctrl.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  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(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. bank = get_inactive_bank_num(swrm);
  859. if (enable) {
  860. if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
  861. dev_dbg(swrm->dev, "%s:No pending connect port req\n",
  862. __func__);
  863. goto exit;
  864. }
  865. clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
  866. ret = swrm_get_port_config(swrm);
  867. if (ret) {
  868. /* cannot accommodate ports */
  869. swrm_cleanup_disabled_port_reqs(master);
  870. mutex_unlock(&swrm->mlock);
  871. return -EINVAL;
  872. }
  873. swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
  874. SWRM_INTERRUPT_STATUS_MASK);
  875. /* apply the new port config*/
  876. swrm_apply_port_config(master);
  877. } else {
  878. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  879. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  880. __func__);
  881. goto exit;
  882. }
  883. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  884. swrm_disable_ports(master, bank);
  885. }
  886. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
  887. __func__, enable, swrm->num_cfg_devs);
  888. if (enable) {
  889. /* set col = 16 */
  890. n_col = SWR_MAX_COL;
  891. } else {
  892. /*
  893. * Do not change to col = 2 if there are still active ports
  894. */
  895. if (!master->num_port)
  896. n_col = SWR_MIN_COL;
  897. else
  898. n_col = SWR_MAX_COL;
  899. }
  900. /* Use default 50 * x, frame shape. Change based on mclk */
  901. if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
  902. dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
  903. n_col ? 16 : 2);
  904. n_row = SWR_ROW_64;
  905. } else {
  906. dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
  907. n_col ? 16 : 2);
  908. n_row = SWR_ROW_50;
  909. }
  910. value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
  911. value &= (~mask);
  912. value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  913. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  914. (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  915. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  916. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  917. SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  918. enable_bank_switch(swrm, bank, n_row, n_col);
  919. inactive_bank = bank ? 0 : 1;
  920. if (enable)
  921. swrm_copy_data_port_config(master, inactive_bank);
  922. else {
  923. swrm_disable_ports(master, inactive_bank);
  924. swrm_cleanup_disabled_port_reqs(master);
  925. }
  926. if (!swrm_is_port_en(master)) {
  927. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  928. __func__);
  929. pm_runtime_mark_last_busy(swrm->dev);
  930. pm_runtime_put_autosuspend(swrm->dev);
  931. }
  932. exit:
  933. mutex_unlock(&swrm->mlock);
  934. return 0;
  935. }
  936. static int swrm_connect_port(struct swr_master *master,
  937. struct swr_params *portinfo)
  938. {
  939. int i;
  940. struct swr_port_info *port_req;
  941. int ret = 0;
  942. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  943. struct swrm_mports *mport;
  944. u8 mstr_port_id, mstr_ch_msk;
  945. dev_dbg(&master->dev, "%s: enter\n", __func__);
  946. if (!portinfo)
  947. return -EINVAL;
  948. if (!swrm) {
  949. dev_err(&master->dev,
  950. "%s: Invalid handle to swr controller\n",
  951. __func__);
  952. return -EINVAL;
  953. }
  954. mutex_lock(&swrm->mlock);
  955. mutex_lock(&swrm->devlock);
  956. if (!swrm->dev_up) {
  957. mutex_unlock(&swrm->devlock);
  958. mutex_unlock(&swrm->mlock);
  959. return -EINVAL;
  960. }
  961. mutex_unlock(&swrm->devlock);
  962. if (!swrm_is_port_en(master))
  963. pm_runtime_get_sync(swrm->dev);
  964. for (i = 0; i < portinfo->num_port; i++) {
  965. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
  966. portinfo->port_type[i],
  967. portinfo->port_id[i]);
  968. if (ret) {
  969. dev_err(&master->dev,
  970. "%s: mstr portid for slv port %d not found\n",
  971. __func__, portinfo->port_id[i]);
  972. goto port_fail;
  973. }
  974. mport = &(swrm->mport_cfg[mstr_port_id]);
  975. /* get port req */
  976. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  977. portinfo->dev_num);
  978. if (!port_req) {
  979. dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
  980. __func__, portinfo->port_id[i],
  981. portinfo->dev_num);
  982. port_req = kzalloc(sizeof(struct swr_port_info),
  983. GFP_KERNEL);
  984. if (!port_req) {
  985. ret = -ENOMEM;
  986. goto mem_fail;
  987. }
  988. port_req->dev_num = portinfo->dev_num;
  989. port_req->slave_port_id = portinfo->port_id[i];
  990. port_req->num_ch = portinfo->num_ch[i];
  991. port_req->ch_rate = portinfo->ch_rate[i];
  992. port_req->ch_en = 0;
  993. port_req->master_port_id = mstr_port_id;
  994. list_add(&port_req->list, &mport->port_req_list);
  995. }
  996. port_req->req_ch |= portinfo->ch_en[i];
  997. dev_dbg(&master->dev,
  998. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
  999. __func__, port_req->master_port_id,
  1000. port_req->slave_port_id, port_req->ch_rate,
  1001. port_req->num_ch);
  1002. /* Put the port req on master port */
  1003. mport = &(swrm->mport_cfg[mstr_port_id]);
  1004. mport->port_en = true;
  1005. mport->req_ch |= mstr_ch_msk;
  1006. master->port_en_mask |= (1 << mstr_port_id);
  1007. }
  1008. master->num_port += portinfo->num_port;
  1009. set_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1010. swr_port_response(master, portinfo->tid);
  1011. mutex_unlock(&swrm->mlock);
  1012. return 0;
  1013. port_fail:
  1014. mem_fail:
  1015. /* cleanup port reqs in error condition */
  1016. swrm_cleanup_disabled_port_reqs(master);
  1017. mutex_unlock(&swrm->mlock);
  1018. return ret;
  1019. }
  1020. static int swrm_disconnect_port(struct swr_master *master,
  1021. struct swr_params *portinfo)
  1022. {
  1023. int i, ret = 0;
  1024. struct swr_port_info *port_req;
  1025. struct swrm_mports *mport;
  1026. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1027. u8 mstr_port_id, mstr_ch_mask;
  1028. if (!swrm) {
  1029. dev_err(&master->dev,
  1030. "%s: Invalid handle to swr controller\n",
  1031. __func__);
  1032. return -EINVAL;
  1033. }
  1034. if (!portinfo) {
  1035. dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
  1036. return -EINVAL;
  1037. }
  1038. mutex_lock(&swrm->mlock);
  1039. for (i = 0; i < portinfo->num_port; i++) {
  1040. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
  1041. portinfo->port_type[i], portinfo->port_id[i]);
  1042. if (ret) {
  1043. dev_err(&master->dev,
  1044. "%s: mstr portid for slv port %d not found\n",
  1045. __func__, portinfo->port_id[i]);
  1046. mutex_unlock(&swrm->mlock);
  1047. return -EINVAL;
  1048. }
  1049. mport = &(swrm->mport_cfg[mstr_port_id]);
  1050. /* get port req */
  1051. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1052. portinfo->dev_num);
  1053. if (!port_req) {
  1054. dev_err(&master->dev, "%s:port not enabled : port %d\n",
  1055. __func__, portinfo->port_id[i]);
  1056. mutex_unlock(&swrm->mlock);
  1057. return -EINVAL;
  1058. }
  1059. port_req->req_ch &= ~portinfo->ch_en[i];
  1060. mport->req_ch &= ~mstr_ch_mask;
  1061. }
  1062. master->num_port -= portinfo->num_port;
  1063. set_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1064. swr_port_response(master, portinfo->tid);
  1065. mutex_unlock(&swrm->mlock);
  1066. return 0;
  1067. }
  1068. static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
  1069. int status, u8 *devnum)
  1070. {
  1071. int i;
  1072. bool found = false;
  1073. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1074. if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
  1075. *devnum = i;
  1076. found = true;
  1077. break;
  1078. }
  1079. status >>= 2;
  1080. }
  1081. if (found)
  1082. return 0;
  1083. else
  1084. return -EINVAL;
  1085. }
  1086. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  1087. int status, u8 *devnum)
  1088. {
  1089. int i;
  1090. int new_sts = status;
  1091. int ret = SWR_NOT_PRESENT;
  1092. if (status != swrm->slave_status) {
  1093. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1094. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  1095. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  1096. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  1097. *devnum = i;
  1098. break;
  1099. }
  1100. status >>= 2;
  1101. swrm->slave_status >>= 2;
  1102. }
  1103. swrm->slave_status = new_sts;
  1104. }
  1105. return ret;
  1106. }
  1107. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  1108. {
  1109. struct swr_mstr_ctrl *swrm = dev;
  1110. u32 value, intr_sts, intr_sts_masked;
  1111. u32 temp = 0;
  1112. u32 status, chg_sts, i;
  1113. u8 devnum = 0;
  1114. int ret = IRQ_HANDLED;
  1115. struct swr_device *swr_dev;
  1116. struct swr_master *mstr = &swrm->master;
  1117. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1118. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1119. return IRQ_NONE;
  1120. }
  1121. mutex_lock(&swrm->reslock);
  1122. if (swrm_clk_request(swrm, true)) {
  1123. mutex_unlock(&swrm->reslock);
  1124. goto exit;
  1125. }
  1126. mutex_unlock(&swrm->reslock);
  1127. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1128. intr_sts_masked = intr_sts & swrm->intr_mask;
  1129. handle_irq:
  1130. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  1131. value = intr_sts_masked & (1 << i);
  1132. if (!value)
  1133. continue;
  1134. switch (value) {
  1135. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  1136. dev_dbg(swrm->dev, "Trigger irq to slave device\n");
  1137. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1138. ret = swrm_find_alert_slave(swrm, status, &devnum);
  1139. if (ret) {
  1140. dev_err_ratelimited(swrm->dev,
  1141. "no slave alert found.spurious interrupt\n");
  1142. break;
  1143. }
  1144. swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
  1145. SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  1146. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1147. SWRS_SCP_INT_STATUS_CLEAR_1);
  1148. swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
  1149. SWRS_SCP_INT_STATUS_CLEAR_1);
  1150. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1151. if (swr_dev->dev_num != devnum)
  1152. continue;
  1153. if (swr_dev->slave_irq) {
  1154. do {
  1155. handle_nested_irq(
  1156. irq_find_mapping(
  1157. swr_dev->slave_irq, 0));
  1158. } while (swr_dev->slave_irq_pending);
  1159. }
  1160. }
  1161. break;
  1162. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  1163. dev_dbg(swrm->dev, "SWR new slave attached\n");
  1164. break;
  1165. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  1166. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1167. if (status == swrm->slave_status) {
  1168. dev_dbg(swrm->dev,
  1169. "%s: No change in slave status: %d\n",
  1170. __func__, status);
  1171. break;
  1172. }
  1173. chg_sts = swrm_check_slave_change_status(swrm, status,
  1174. &devnum);
  1175. switch (chg_sts) {
  1176. case SWR_NOT_PRESENT:
  1177. dev_dbg(swrm->dev, "device %d got detached\n",
  1178. devnum);
  1179. break;
  1180. case SWR_ATTACHED_OK:
  1181. dev_dbg(swrm->dev, "device %d got attached\n",
  1182. devnum);
  1183. /* enable host irq from slave device*/
  1184. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
  1185. SWRS_SCP_INT_STATUS_CLEAR_1);
  1186. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1187. SWRS_SCP_INT_STATUS_MASK_1);
  1188. break;
  1189. case SWR_ALERT:
  1190. dev_dbg(swrm->dev,
  1191. "device %d has pending interrupt\n",
  1192. devnum);
  1193. break;
  1194. }
  1195. break;
  1196. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  1197. dev_err_ratelimited(swrm->dev,
  1198. "SWR bus clsh detected\n");
  1199. break;
  1200. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  1201. dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
  1202. break;
  1203. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  1204. dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
  1205. break;
  1206. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  1207. dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
  1208. break;
  1209. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  1210. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  1211. dev_err_ratelimited(swrm->dev,
  1212. "SWR CMD error, fifo status 0x%x, flushing fifo\n",
  1213. value);
  1214. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  1215. break;
  1216. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  1217. dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
  1218. swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
  1219. swr_master_write(swrm,
  1220. SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
  1221. break;
  1222. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  1223. dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
  1224. swrm->intr_mask &=
  1225. ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
  1226. swr_master_write(swrm,
  1227. SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
  1228. break;
  1229. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  1230. complete(&swrm->broadcast);
  1231. dev_dbg(swrm->dev, "SWR cmd id finished\n");
  1232. break;
  1233. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
  1234. break;
  1235. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  1236. break;
  1237. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  1238. break;
  1239. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  1240. complete(&swrm->reset);
  1241. break;
  1242. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  1243. break;
  1244. default:
  1245. dev_err_ratelimited(swrm->dev,
  1246. "SWR unknown interrupt\n");
  1247. ret = IRQ_NONE;
  1248. break;
  1249. }
  1250. }
  1251. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
  1252. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
  1253. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1254. intr_sts_masked = intr_sts & swrm->intr_mask;
  1255. if (intr_sts_masked) {
  1256. dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
  1257. goto handle_irq;
  1258. }
  1259. mutex_lock(&swrm->reslock);
  1260. swrm_clk_request(swrm, false);
  1261. mutex_unlock(&swrm->reslock);
  1262. exit:
  1263. swrm_unlock_sleep(swrm);
  1264. return ret;
  1265. }
  1266. static irqreturn_t swr_mstr_interrupt_v2(int irq, void *dev)
  1267. {
  1268. struct swr_mstr_ctrl *swrm = dev;
  1269. u32 value, intr_sts, intr_sts_masked;
  1270. u32 temp = 0;
  1271. u32 status, chg_sts, i;
  1272. u8 devnum = 0;
  1273. int ret = IRQ_HANDLED;
  1274. struct swr_device *swr_dev;
  1275. struct swr_master *mstr = &swrm->master;
  1276. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1277. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1278. return IRQ_NONE;
  1279. }
  1280. mutex_lock(&swrm->reslock);
  1281. if (swrm->lpass_core_hw_vote)
  1282. clk_prepare_enable(swrm->lpass_core_hw_vote);
  1283. swrm_clk_request(swrm, true);
  1284. mutex_unlock(&swrm->reslock);
  1285. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1286. intr_sts_masked = intr_sts & swrm->intr_mask;
  1287. handle_irq:
  1288. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  1289. value = intr_sts_masked & (1 << i);
  1290. if (!value)
  1291. continue;
  1292. switch (value) {
  1293. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  1294. dev_dbg(swrm->dev, "%s: Trigger irq to slave device\n",
  1295. __func__);
  1296. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1297. ret = swrm_find_alert_slave(swrm, status, &devnum);
  1298. if (ret) {
  1299. dev_err_ratelimited(swrm->dev,
  1300. "%s: no slave alert found.spurious interrupt\n",
  1301. __func__);
  1302. break;
  1303. }
  1304. swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
  1305. SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  1306. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1307. SWRS_SCP_INT_STATUS_CLEAR_1);
  1308. swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
  1309. SWRS_SCP_INT_STATUS_CLEAR_1);
  1310. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1311. if (swr_dev->dev_num != devnum)
  1312. continue;
  1313. if (swr_dev->slave_irq) {
  1314. do {
  1315. handle_nested_irq(
  1316. irq_find_mapping(
  1317. swr_dev->slave_irq, 0));
  1318. } while (swr_dev->slave_irq_pending);
  1319. }
  1320. }
  1321. break;
  1322. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  1323. dev_dbg(swrm->dev, "%s: SWR new slave attached\n",
  1324. __func__);
  1325. break;
  1326. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  1327. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1328. if (status == swrm->slave_status) {
  1329. dev_dbg(swrm->dev,
  1330. "%s: No change in slave status: %d\n",
  1331. __func__, status);
  1332. break;
  1333. }
  1334. chg_sts = swrm_check_slave_change_status(swrm, status,
  1335. &devnum);
  1336. switch (chg_sts) {
  1337. case SWR_NOT_PRESENT:
  1338. dev_dbg(swrm->dev,
  1339. "%s: device %d got detached\n",
  1340. __func__, devnum);
  1341. break;
  1342. case SWR_ATTACHED_OK:
  1343. dev_dbg(swrm->dev,
  1344. "%s: device %d got attached\n",
  1345. __func__, devnum);
  1346. /* enable host irq from slave device*/
  1347. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
  1348. SWRS_SCP_INT_STATUS_CLEAR_1);
  1349. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1350. SWRS_SCP_INT_STATUS_MASK_1);
  1351. break;
  1352. case SWR_ALERT:
  1353. dev_dbg(swrm->dev,
  1354. "%s: device %d has pending interrupt\n",
  1355. __func__, devnum);
  1356. break;
  1357. }
  1358. break;
  1359. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  1360. dev_err_ratelimited(swrm->dev,
  1361. "%s: SWR bus clsh detected\n",
  1362. __func__);
  1363. break;
  1364. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  1365. dev_dbg(swrm->dev, "%s: SWR read FIFO overflow\n",
  1366. __func__);
  1367. break;
  1368. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  1369. dev_dbg(swrm->dev, "%s: SWR read FIFO underflow\n",
  1370. __func__);
  1371. break;
  1372. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  1373. dev_dbg(swrm->dev, "%s: SWR write FIFO overflow\n",
  1374. __func__);
  1375. break;
  1376. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  1377. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  1378. dev_err_ratelimited(swrm->dev,
  1379. "%s: SWR CMD error, fifo status 0x%x, flushing fifo\n",
  1380. __func__, value);
  1381. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  1382. break;
  1383. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  1384. dev_err_ratelimited(swrm->dev,
  1385. "%s: SWR Port collision detected\n",
  1386. __func__);
  1387. swrm->intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
  1388. swr_master_write(swrm,
  1389. SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
  1390. break;
  1391. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  1392. dev_dbg(swrm->dev,
  1393. "%s: SWR read enable valid mismatch\n",
  1394. __func__);
  1395. swrm->intr_mask &=
  1396. ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
  1397. swr_master_write(swrm,
  1398. SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, swrm->intr_mask);
  1399. break;
  1400. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  1401. complete(&swrm->broadcast);
  1402. dev_dbg(swrm->dev, "%s: SWR cmd id finished\n",
  1403. __func__);
  1404. break;
  1405. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED_V2:
  1406. break;
  1407. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL_V2:
  1408. break;
  1409. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED_V2:
  1410. break;
  1411. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED_V2:
  1412. break;
  1413. case SWRM_INTERRUPT_STATUS_EXT_CLK_STOP_WAKEUP:
  1414. if (swrm->state == SWR_MSTR_UP)
  1415. dev_dbg(swrm->dev,
  1416. "%s:SWR Master is already up\n",
  1417. __func__);
  1418. else
  1419. dev_err_ratelimited(swrm->dev,
  1420. "%s: SWR wokeup during clock stop\n",
  1421. __func__);
  1422. break;
  1423. default:
  1424. dev_err_ratelimited(swrm->dev,
  1425. "%s: SWR unknown interrupt value: %d\n",
  1426. __func__, value);
  1427. ret = IRQ_NONE;
  1428. break;
  1429. }
  1430. }
  1431. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
  1432. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
  1433. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1434. intr_sts_masked = intr_sts & swrm->intr_mask;
  1435. if (intr_sts_masked) {
  1436. dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
  1437. goto handle_irq;
  1438. }
  1439. mutex_lock(&swrm->reslock);
  1440. swrm_clk_request(swrm, false);
  1441. if (swrm->lpass_core_hw_vote)
  1442. clk_disable_unprepare(swrm->lpass_core_hw_vote);
  1443. mutex_unlock(&swrm->reslock);
  1444. swrm_unlock_sleep(swrm);
  1445. return ret;
  1446. }
  1447. static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
  1448. {
  1449. struct swr_mstr_ctrl *swrm = dev;
  1450. int ret = IRQ_HANDLED;
  1451. if (!swrm || !(swrm->dev)) {
  1452. pr_err("%s: swrm or dev is null\n", __func__);
  1453. return IRQ_NONE;
  1454. }
  1455. mutex_lock(&swrm->devlock);
  1456. if (!swrm->dev_up) {
  1457. if (swrm->wake_irq > 0)
  1458. disable_irq_nosync(swrm->wake_irq);
  1459. mutex_unlock(&swrm->devlock);
  1460. return ret;
  1461. }
  1462. mutex_unlock(&swrm->devlock);
  1463. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1464. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1465. goto exit;
  1466. }
  1467. if (swrm->wake_irq > 0)
  1468. disable_irq_nosync(swrm->wake_irq);
  1469. pm_runtime_get_sync(swrm->dev);
  1470. pm_runtime_mark_last_busy(swrm->dev);
  1471. pm_runtime_put_autosuspend(swrm->dev);
  1472. swrm_unlock_sleep(swrm);
  1473. exit:
  1474. return ret;
  1475. }
  1476. static void swrm_wakeup_work(struct work_struct *work)
  1477. {
  1478. struct swr_mstr_ctrl *swrm;
  1479. swrm = container_of(work, struct swr_mstr_ctrl,
  1480. wakeup_work);
  1481. if (!swrm || !(swrm->dev)) {
  1482. pr_err("%s: swrm or dev is null\n", __func__);
  1483. return;
  1484. }
  1485. mutex_lock(&swrm->devlock);
  1486. if (!swrm->dev_up) {
  1487. mutex_unlock(&swrm->devlock);
  1488. goto exit;
  1489. }
  1490. mutex_unlock(&swrm->devlock);
  1491. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1492. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1493. goto exit;
  1494. }
  1495. pm_runtime_get_sync(swrm->dev);
  1496. pm_runtime_mark_last_busy(swrm->dev);
  1497. pm_runtime_put_autosuspend(swrm->dev);
  1498. swrm_unlock_sleep(swrm);
  1499. exit:
  1500. pm_relax(swrm->dev);
  1501. }
  1502. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  1503. {
  1504. u32 val;
  1505. swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1506. val = (swrm->slave_status >> (devnum * 2));
  1507. val &= SWRM_MCP_SLV_STATUS_MASK;
  1508. return val;
  1509. }
  1510. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  1511. u8 *dev_num)
  1512. {
  1513. int i;
  1514. u64 id = 0;
  1515. int ret = -EINVAL;
  1516. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1517. struct swr_device *swr_dev;
  1518. u32 num_dev = 0;
  1519. if (!swrm) {
  1520. pr_err("%s: Invalid handle to swr controller\n",
  1521. __func__);
  1522. return ret;
  1523. }
  1524. if (swrm->num_dev)
  1525. num_dev = swrm->num_dev;
  1526. else
  1527. num_dev = mstr->num_dev;
  1528. mutex_lock(&swrm->devlock);
  1529. if (!swrm->dev_up) {
  1530. mutex_unlock(&swrm->devlock);
  1531. return ret;
  1532. }
  1533. mutex_unlock(&swrm->devlock);
  1534. pm_runtime_get_sync(swrm->dev);
  1535. for (i = 1; i < (num_dev + 1); i++) {
  1536. id = ((u64)(swr_master_read(swrm,
  1537. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  1538. id |= swr_master_read(swrm,
  1539. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  1540. /*
  1541. * As pm_runtime_get_sync() brings all slaves out of reset
  1542. * update logical device number for all slaves.
  1543. */
  1544. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1545. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  1546. u32 status = swrm_get_device_status(swrm, i);
  1547. if ((status == 0x01) || (status == 0x02)) {
  1548. swr_dev->dev_num = i;
  1549. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  1550. *dev_num = i;
  1551. ret = 0;
  1552. }
  1553. dev_dbg(swrm->dev,
  1554. "%s: devnum %d is assigned for dev addr %lx\n",
  1555. __func__, i, swr_dev->addr);
  1556. }
  1557. }
  1558. }
  1559. }
  1560. if (ret)
  1561. dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
  1562. __func__, dev_id);
  1563. pm_runtime_mark_last_busy(swrm->dev);
  1564. pm_runtime_put_autosuspend(swrm->dev);
  1565. return ret;
  1566. }
  1567. static void swrm_device_wakeup_vote(struct swr_master *mstr)
  1568. {
  1569. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1570. if (!swrm) {
  1571. pr_err("%s: Invalid handle to swr controller\n",
  1572. __func__);
  1573. return;
  1574. }
  1575. if (unlikely(swrm_lock_sleep(swrm) == false)) {
  1576. dev_err(swrm->dev, "%s Failed to hold suspend\n", __func__);
  1577. return;
  1578. }
  1579. pm_runtime_get_sync(swrm->dev);
  1580. }
  1581. static void swrm_device_wakeup_unvote(struct swr_master *mstr)
  1582. {
  1583. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1584. if (!swrm) {
  1585. pr_err("%s: Invalid handle to swr controller\n",
  1586. __func__);
  1587. return;
  1588. }
  1589. pm_runtime_mark_last_busy(swrm->dev);
  1590. pm_runtime_put_autosuspend(swrm->dev);
  1591. swrm_unlock_sleep(swrm);
  1592. }
  1593. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  1594. {
  1595. int ret = 0;
  1596. u32 val;
  1597. u8 row_ctrl = SWR_ROW_50;
  1598. u8 col_ctrl = SWR_MIN_COL;
  1599. u8 ssp_period = 1;
  1600. u8 retry_cmd_num = 3;
  1601. u32 reg[SWRM_MAX_INIT_REG];
  1602. u32 value[SWRM_MAX_INIT_REG];
  1603. int len = 0;
  1604. /* Clear Rows and Cols */
  1605. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1606. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1607. (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1608. reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
  1609. value[len++] = val;
  1610. /* Set Auto enumeration flag */
  1611. reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
  1612. value[len++] = 1;
  1613. /* Configure No pings */
  1614. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1615. val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
  1616. val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
  1617. reg[len] = SWRM_MCP_CFG_ADDR;
  1618. value[len++] = val;
  1619. /* Configure number of retries of a read/write cmd */
  1620. val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
  1621. reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
  1622. value[len++] = val;
  1623. reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
  1624. value[len++] = 0x2;
  1625. /* Set IRQ to PULSE */
  1626. reg[len] = SWRM_COMP_CFG_ADDR;
  1627. value[len++] = 0x02;
  1628. reg[len] = SWRM_COMP_CFG_ADDR;
  1629. value[len++] = 0x03;
  1630. reg[len] = SWRM_INTERRUPT_CLEAR;
  1631. value[len++] = 0xFFFFFFFF;
  1632. swrm->intr_mask = SWRM_INTERRUPT_STATUS_MASK;
  1633. /* Mask soundwire interrupts */
  1634. reg[len] = SWRM_INTERRUPT_MASK_ADDR;
  1635. value[len++] = swrm->intr_mask;
  1636. reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
  1637. value[len++] = swrm->intr_mask;
  1638. swr_master_bulk_write(swrm, reg, value, len);
  1639. /*
  1640. * For SWR master version 1.5.1, continue
  1641. * execute on command ignore.
  1642. */
  1643. if (swrm->version == SWRM_VERSION_1_5_1)
  1644. swr_master_write(swrm, SWRM_CMD_FIFO_CFG_ADDR,
  1645. (swr_master_read(swrm,
  1646. SWRM_CMD_FIFO_CFG_ADDR) | 0x80000000));
  1647. return ret;
  1648. }
  1649. static int swrm_event_notify(struct notifier_block *self,
  1650. unsigned long action, void *data)
  1651. {
  1652. struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
  1653. event_notifier);
  1654. if (!swrm || !(swrm->dev)) {
  1655. pr_err("%s: swrm or dev is NULL\n", __func__);
  1656. return -EINVAL;
  1657. }
  1658. switch (action) {
  1659. case MSM_AUD_DC_EVENT:
  1660. schedule_work(&(swrm->dc_presence_work));
  1661. break;
  1662. case SWR_WAKE_IRQ_EVENT:
  1663. if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
  1664. swrm->ipc_wakeup_triggered = true;
  1665. pm_stay_awake(swrm->dev);
  1666. schedule_work(&swrm->wakeup_work);
  1667. }
  1668. break;
  1669. default:
  1670. dev_err(swrm->dev, "%s: invalid event type: %lu\n",
  1671. __func__, action);
  1672. return -EINVAL;
  1673. }
  1674. return 0;
  1675. }
  1676. static void swrm_notify_work_fn(struct work_struct *work)
  1677. {
  1678. struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
  1679. dc_presence_work);
  1680. if (!swrm || !swrm->pdev) {
  1681. pr_err("%s: swrm or pdev is NULL\n", __func__);
  1682. return;
  1683. }
  1684. swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
  1685. }
  1686. static int swrm_probe(struct platform_device *pdev)
  1687. {
  1688. struct swr_mstr_ctrl *swrm;
  1689. struct swr_ctrl_platform_data *pdata;
  1690. u32 i, num_ports, port_num, port_type, ch_mask;
  1691. u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
  1692. int ret = 0;
  1693. struct clk *lpass_core_hw_vote = NULL;
  1694. /* Allocate soundwire master driver structure */
  1695. swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
  1696. GFP_KERNEL);
  1697. if (!swrm) {
  1698. ret = -ENOMEM;
  1699. goto err_memory_fail;
  1700. }
  1701. swrm->pdev = pdev;
  1702. swrm->dev = &pdev->dev;
  1703. platform_set_drvdata(pdev, swrm);
  1704. swr_set_ctrl_data(&swrm->master, swrm);
  1705. pdata = dev_get_platdata(&pdev->dev);
  1706. if (!pdata) {
  1707. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  1708. __func__);
  1709. ret = -EINVAL;
  1710. goto err_pdata_fail;
  1711. }
  1712. swrm->handle = (void *)pdata->handle;
  1713. if (!swrm->handle) {
  1714. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  1715. __func__);
  1716. ret = -EINVAL;
  1717. goto err_pdata_fail;
  1718. }
  1719. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
  1720. &swrm->master_id);
  1721. if (ret) {
  1722. dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
  1723. goto err_pdata_fail;
  1724. }
  1725. if (!(of_property_read_u32(pdev->dev.of_node,
  1726. "swrm-io-base", &swrm->swrm_base_reg)))
  1727. ret = of_property_read_u32(pdev->dev.of_node,
  1728. "swrm-io-base", &swrm->swrm_base_reg);
  1729. if (!swrm->swrm_base_reg) {
  1730. swrm->read = pdata->read;
  1731. if (!swrm->read) {
  1732. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  1733. __func__);
  1734. ret = -EINVAL;
  1735. goto err_pdata_fail;
  1736. }
  1737. swrm->write = pdata->write;
  1738. if (!swrm->write) {
  1739. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  1740. __func__);
  1741. ret = -EINVAL;
  1742. goto err_pdata_fail;
  1743. }
  1744. swrm->bulk_write = pdata->bulk_write;
  1745. if (!swrm->bulk_write) {
  1746. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  1747. __func__);
  1748. ret = -EINVAL;
  1749. goto err_pdata_fail;
  1750. }
  1751. } else {
  1752. swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
  1753. swrm->swrm_base_reg, SWRM_MAX_REGISTER);
  1754. }
  1755. swrm->clk = pdata->clk;
  1756. if (!swrm->clk) {
  1757. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  1758. __func__);
  1759. ret = -EINVAL;
  1760. goto err_pdata_fail;
  1761. }
  1762. if (of_property_read_u32(pdev->dev.of_node,
  1763. "qcom,swr-clock-stop-mode0",
  1764. &swrm->clk_stop_mode0_supp)) {
  1765. swrm->clk_stop_mode0_supp = FALSE;
  1766. }
  1767. ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
  1768. &swrm->num_dev);
  1769. if (ret) {
  1770. dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
  1771. __func__, "qcom,swr-num-dev");
  1772. } else {
  1773. if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
  1774. dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
  1775. __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
  1776. ret = -EINVAL;
  1777. goto err_pdata_fail;
  1778. }
  1779. }
  1780. /* Parse soundwire port mapping */
  1781. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
  1782. &num_ports);
  1783. if (ret) {
  1784. dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
  1785. goto err_pdata_fail;
  1786. }
  1787. swrm->num_ports = num_ports;
  1788. if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
  1789. &map_size)) {
  1790. dev_err(swrm->dev, "missing port mapping\n");
  1791. goto err_pdata_fail;
  1792. }
  1793. map_length = map_size / (3 * sizeof(u32));
  1794. if (num_ports > SWR_MSTR_PORT_LEN) {
  1795. dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
  1796. __func__);
  1797. ret = -EINVAL;
  1798. goto err_pdata_fail;
  1799. }
  1800. temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
  1801. if (!temp) {
  1802. ret = -ENOMEM;
  1803. goto err_pdata_fail;
  1804. }
  1805. ret = of_property_read_u32_array(pdev->dev.of_node,
  1806. "qcom,swr-port-mapping", temp, 3 * map_length);
  1807. if (ret) {
  1808. dev_err(swrm->dev, "%s: Failed to read port mapping\n",
  1809. __func__);
  1810. goto err_pdata_fail;
  1811. }
  1812. for (i = 0; i < map_length; i++) {
  1813. port_num = temp[3 * i];
  1814. port_type = temp[3 * i + 1];
  1815. ch_mask = temp[3 * i + 2];
  1816. if (port_num != old_port_num)
  1817. ch_iter = 0;
  1818. swrm->port_mapping[port_num][ch_iter].port_type = port_type;
  1819. swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
  1820. old_port_num = port_num;
  1821. }
  1822. devm_kfree(&pdev->dev, temp);
  1823. swrm->reg_irq = pdata->reg_irq;
  1824. swrm->master.read = swrm_read;
  1825. swrm->master.write = swrm_write;
  1826. swrm->master.bulk_write = swrm_bulk_write;
  1827. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  1828. swrm->master.connect_port = swrm_connect_port;
  1829. swrm->master.disconnect_port = swrm_disconnect_port;
  1830. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  1831. swrm->master.remove_from_group = swrm_remove_from_group;
  1832. swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
  1833. swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
  1834. swrm->master.dev.parent = &pdev->dev;
  1835. swrm->master.dev.of_node = pdev->dev.of_node;
  1836. swrm->master.num_port = 0;
  1837. swrm->rcmd_id = 0;
  1838. swrm->wcmd_id = 0;
  1839. swrm->slave_status = 0;
  1840. swrm->num_rx_chs = 0;
  1841. swrm->clk_ref_count = 0;
  1842. swrm->swr_irq_wakeup_capable = 0;
  1843. swrm->mclk_freq = MCLK_FREQ;
  1844. swrm->dev_up = true;
  1845. swrm->state = SWR_MSTR_UP;
  1846. swrm->ipc_wakeup = false;
  1847. swrm->ipc_wakeup_triggered = false;
  1848. init_completion(&swrm->reset);
  1849. init_completion(&swrm->broadcast);
  1850. init_completion(&swrm->clk_off_complete);
  1851. mutex_init(&swrm->mlock);
  1852. mutex_init(&swrm->reslock);
  1853. mutex_init(&swrm->force_down_lock);
  1854. mutex_init(&swrm->iolock);
  1855. mutex_init(&swrm->clklock);
  1856. mutex_init(&swrm->devlock);
  1857. mutex_init(&swrm->pm_lock);
  1858. swrm->wlock_holders = 0;
  1859. swrm->pm_state = SWRM_PM_SLEEPABLE;
  1860. init_waitqueue_head(&swrm->pm_wq);
  1861. pm_qos_add_request(&swrm->pm_qos_req,
  1862. PM_QOS_CPU_DMA_LATENCY,
  1863. PM_QOS_DEFAULT_VALUE);
  1864. for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
  1865. INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
  1866. if (swrm->reg_irq) {
  1867. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  1868. SWR_IRQ_REGISTER);
  1869. if (ret) {
  1870. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  1871. __func__, ret);
  1872. goto err_irq_fail;
  1873. }
  1874. } else {
  1875. swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
  1876. if (swrm->irq < 0) {
  1877. dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
  1878. __func__, swrm->irq);
  1879. goto err_irq_fail;
  1880. }
  1881. ret = request_threaded_irq(swrm->irq, NULL,
  1882. swr_mstr_interrupt_v2,
  1883. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1884. "swr_master_irq", swrm);
  1885. if (ret) {
  1886. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  1887. __func__, ret);
  1888. goto err_irq_fail;
  1889. }
  1890. }
  1891. /* Make inband tx interrupts as wakeup capable for slave irq */
  1892. ret = of_property_read_u32(pdev->dev.of_node,
  1893. "qcom,swr-mstr-irq-wakeup-capable",
  1894. &swrm->swr_irq_wakeup_capable);
  1895. if (ret)
  1896. dev_dbg(swrm->dev, "%s: swrm irq wakeup capable not defined\n",
  1897. __func__);
  1898. if (swrm->swr_irq_wakeup_capable)
  1899. irq_set_irq_wake(swrm->irq, 1);
  1900. ret = swr_register_master(&swrm->master);
  1901. if (ret) {
  1902. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  1903. goto err_mstr_fail;
  1904. }
  1905. /* Add devices registered with board-info as the
  1906. * controller will be up now
  1907. */
  1908. swr_master_add_boarddevices(&swrm->master);
  1909. mutex_lock(&swrm->mlock);
  1910. swrm_clk_request(swrm, true);
  1911. ret = swrm_master_init(swrm);
  1912. if (ret < 0) {
  1913. dev_err(&pdev->dev,
  1914. "%s: Error in master Initialization , err %d\n",
  1915. __func__, ret);
  1916. mutex_unlock(&swrm->mlock);
  1917. goto err_mstr_fail;
  1918. }
  1919. swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
  1920. mutex_unlock(&swrm->mlock);
  1921. INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
  1922. if (pdev->dev.of_node)
  1923. of_register_swr_devices(&swrm->master);
  1924. /* Register LPASS core hw vote */
  1925. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  1926. if (IS_ERR(lpass_core_hw_vote)) {
  1927. ret = PTR_ERR(lpass_core_hw_vote);
  1928. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  1929. __func__, "lpass_core_hw_vote", ret);
  1930. lpass_core_hw_vote = NULL;
  1931. ret = 0;
  1932. }
  1933. swrm->lpass_core_hw_vote = lpass_core_hw_vote;
  1934. dbgswrm = swrm;
  1935. debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  1936. if (!IS_ERR(debugfs_swrm_dent)) {
  1937. debugfs_peek = debugfs_create_file("swrm_peek",
  1938. S_IFREG | 0444, debugfs_swrm_dent,
  1939. (void *) "swrm_peek", &swrm_debug_ops);
  1940. debugfs_poke = debugfs_create_file("swrm_poke",
  1941. S_IFREG | 0444, debugfs_swrm_dent,
  1942. (void *) "swrm_poke", &swrm_debug_ops);
  1943. debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  1944. S_IFREG | 0444, debugfs_swrm_dent,
  1945. (void *) "swrm_reg_dump",
  1946. &swrm_debug_ops);
  1947. }
  1948. ret = device_init_wakeup(swrm->dev, true);
  1949. if (ret) {
  1950. dev_err(swrm->dev, "Device wakeup init failed: %d\n", ret);
  1951. goto err_irq_wakeup_fail;
  1952. }
  1953. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1954. pm_runtime_use_autosuspend(&pdev->dev);
  1955. pm_runtime_set_active(&pdev->dev);
  1956. pm_runtime_enable(&pdev->dev);
  1957. pm_runtime_mark_last_busy(&pdev->dev);
  1958. INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
  1959. swrm->event_notifier.notifier_call = swrm_event_notify;
  1960. msm_aud_evt_register_client(&swrm->event_notifier);
  1961. return 0;
  1962. err_irq_wakeup_fail:
  1963. device_init_wakeup(swrm->dev, false);
  1964. err_mstr_fail:
  1965. if (swrm->reg_irq)
  1966. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1967. swrm, SWR_IRQ_FREE);
  1968. else if (swrm->irq)
  1969. free_irq(swrm->irq, swrm);
  1970. err_irq_fail:
  1971. mutex_destroy(&swrm->mlock);
  1972. mutex_destroy(&swrm->reslock);
  1973. mutex_destroy(&swrm->force_down_lock);
  1974. mutex_destroy(&swrm->iolock);
  1975. mutex_destroy(&swrm->clklock);
  1976. mutex_destroy(&swrm->pm_lock);
  1977. pm_qos_remove_request(&swrm->pm_qos_req);
  1978. err_pdata_fail:
  1979. err_memory_fail:
  1980. return ret;
  1981. }
  1982. static int swrm_remove(struct platform_device *pdev)
  1983. {
  1984. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1985. if (swrm->reg_irq)
  1986. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1987. swrm, SWR_IRQ_FREE);
  1988. else if (swrm->irq)
  1989. free_irq(swrm->irq, swrm);
  1990. else if (swrm->wake_irq > 0)
  1991. free_irq(swrm->wake_irq, swrm);
  1992. if (swrm->swr_irq_wakeup_capable)
  1993. irq_set_irq_wake(swrm->irq, 0);
  1994. cancel_work_sync(&swrm->wakeup_work);
  1995. pm_runtime_disable(&pdev->dev);
  1996. pm_runtime_set_suspended(&pdev->dev);
  1997. swr_unregister_master(&swrm->master);
  1998. msm_aud_evt_unregister_client(&swrm->event_notifier);
  1999. device_init_wakeup(swrm->dev, false);
  2000. mutex_destroy(&swrm->mlock);
  2001. mutex_destroy(&swrm->reslock);
  2002. mutex_destroy(&swrm->iolock);
  2003. mutex_destroy(&swrm->clklock);
  2004. mutex_destroy(&swrm->force_down_lock);
  2005. mutex_destroy(&swrm->pm_lock);
  2006. pm_qos_remove_request(&swrm->pm_qos_req);
  2007. devm_kfree(&pdev->dev, swrm);
  2008. return 0;
  2009. }
  2010. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  2011. {
  2012. u32 val;
  2013. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  2014. swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
  2015. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  2016. val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
  2017. swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
  2018. return 0;
  2019. }
  2020. #ifdef CONFIG_PM
  2021. static int swrm_runtime_resume(struct device *dev)
  2022. {
  2023. struct platform_device *pdev = to_platform_device(dev);
  2024. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2025. int ret = 0;
  2026. struct swr_master *mstr = &swrm->master;
  2027. struct swr_device *swr_dev;
  2028. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  2029. __func__, swrm->state);
  2030. mutex_lock(&swrm->reslock);
  2031. if (swrm->lpass_core_hw_vote)
  2032. ret = clk_prepare_enable(swrm->lpass_core_hw_vote);
  2033. if (ret < 0)
  2034. dev_err(dev, "%s:lpass core hw enable failed\n",
  2035. __func__);
  2036. if ((swrm->state == SWR_MSTR_DOWN) ||
  2037. (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
  2038. if (swrm->clk_stop_mode0_supp) {
  2039. if (swrm->ipc_wakeup)
  2040. msm_aud_evt_blocking_notifier_call_chain(
  2041. SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  2042. }
  2043. if (swrm_clk_request(swrm, true))
  2044. goto exit;
  2045. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  2046. enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
  2047. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2048. ret = swr_device_up(swr_dev);
  2049. if (ret == -ENODEV) {
  2050. dev_dbg(dev,
  2051. "%s slave device up not implemented\n",
  2052. __func__);
  2053. ret = 0;
  2054. } else if (ret) {
  2055. dev_err(dev,
  2056. "%s: failed to wakeup swr dev %d\n",
  2057. __func__, swr_dev->dev_num);
  2058. swrm_clk_request(swrm, false);
  2059. goto exit;
  2060. }
  2061. }
  2062. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  2063. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  2064. swrm_master_init(swrm);
  2065. swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
  2066. SWRS_SCP_INT_STATUS_MASK_1);
  2067. } else {
  2068. /*wake up from clock stop*/
  2069. swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
  2070. usleep_range(100, 105);
  2071. }
  2072. swrm->state = SWR_MSTR_UP;
  2073. }
  2074. exit:
  2075. if (swrm->lpass_core_hw_vote)
  2076. clk_disable_unprepare(swrm->lpass_core_hw_vote);
  2077. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  2078. mutex_unlock(&swrm->reslock);
  2079. return ret;
  2080. }
  2081. static int swrm_runtime_suspend(struct device *dev)
  2082. {
  2083. struct platform_device *pdev = to_platform_device(dev);
  2084. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2085. int ret = 0;
  2086. struct swr_master *mstr = &swrm->master;
  2087. struct swr_device *swr_dev;
  2088. int current_state = 0;
  2089. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  2090. __func__, swrm->state);
  2091. mutex_lock(&swrm->reslock);
  2092. mutex_lock(&swrm->force_down_lock);
  2093. current_state = swrm->state;
  2094. mutex_unlock(&swrm->force_down_lock);
  2095. if (swrm->lpass_core_hw_vote)
  2096. ret = clk_prepare_enable(swrm->lpass_core_hw_vote);
  2097. if (ret < 0)
  2098. dev_err(dev, "%s:lpass core hw enable failed\n",
  2099. __func__);
  2100. if ((current_state == SWR_MSTR_UP) ||
  2101. (current_state == SWR_MSTR_SSR)) {
  2102. if ((current_state != SWR_MSTR_SSR) &&
  2103. swrm_is_port_en(&swrm->master)) {
  2104. dev_dbg(dev, "%s ports are enabled\n", __func__);
  2105. ret = -EBUSY;
  2106. goto exit;
  2107. }
  2108. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  2109. enable_bank_switch(swrm, 0, SWR_ROW_50, SWR_MIN_COL);
  2110. swrm_clk_pause(swrm);
  2111. swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
  2112. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2113. ret = swr_device_down(swr_dev);
  2114. if (ret == -ENODEV) {
  2115. dev_dbg_ratelimited(dev,
  2116. "%s slave device down not implemented\n",
  2117. __func__);
  2118. ret = 0;
  2119. } else if (ret) {
  2120. dev_err(dev,
  2121. "%s: failed to shutdown swr dev %d\n",
  2122. __func__, swr_dev->dev_num);
  2123. goto exit;
  2124. }
  2125. }
  2126. } else {
  2127. /* clock stop sequence */
  2128. swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
  2129. SWRS_SCP_CONTROL);
  2130. usleep_range(100, 105);
  2131. }
  2132. swrm_clk_request(swrm, false);
  2133. if (swrm->clk_stop_mode0_supp) {
  2134. if (swrm->wake_irq > 0) {
  2135. enable_irq(swrm->wake_irq);
  2136. } else if (swrm->ipc_wakeup) {
  2137. msm_aud_evt_blocking_notifier_call_chain(
  2138. SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  2139. swrm->ipc_wakeup_triggered = false;
  2140. }
  2141. }
  2142. }
  2143. /* Retain SSR state until resume */
  2144. if (current_state != SWR_MSTR_SSR)
  2145. swrm->state = SWR_MSTR_DOWN;
  2146. exit:
  2147. if (swrm->lpass_core_hw_vote)
  2148. clk_disable_unprepare(swrm->lpass_core_hw_vote);
  2149. mutex_unlock(&swrm->reslock);
  2150. return ret;
  2151. }
  2152. #endif /* CONFIG_PM */
  2153. static int swrm_device_down(struct device *dev)
  2154. {
  2155. struct platform_device *pdev = to_platform_device(dev);
  2156. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2157. int ret = 0;
  2158. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  2159. mutex_lock(&swrm->force_down_lock);
  2160. swrm->state = SWR_MSTR_SSR;
  2161. mutex_unlock(&swrm->force_down_lock);
  2162. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  2163. ret = swrm_runtime_suspend(dev);
  2164. if (!ret) {
  2165. pm_runtime_disable(dev);
  2166. pm_runtime_set_suspended(dev);
  2167. pm_runtime_enable(dev);
  2168. }
  2169. }
  2170. return 0;
  2171. }
  2172. int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
  2173. {
  2174. int ret = 0;
  2175. int irq, dir_apps_irq;
  2176. if (!swrm->ipc_wakeup) {
  2177. irq = of_get_named_gpio(swrm->dev->of_node,
  2178. "qcom,swr-wakeup-irq", 0);
  2179. if (gpio_is_valid(irq)) {
  2180. swrm->wake_irq = gpio_to_irq(irq);
  2181. if (swrm->wake_irq < 0) {
  2182. dev_err(swrm->dev,
  2183. "Unable to configure irq\n");
  2184. return swrm->wake_irq;
  2185. }
  2186. } else {
  2187. dir_apps_irq = platform_get_irq_byname(swrm->pdev,
  2188. "swr_wake_irq");
  2189. if (dir_apps_irq < 0) {
  2190. dev_err(swrm->dev,
  2191. "TLMM connect gpio not found\n");
  2192. return -EINVAL;
  2193. }
  2194. swrm->wake_irq = dir_apps_irq;
  2195. }
  2196. ret = request_threaded_irq(swrm->wake_irq, NULL,
  2197. swrm_wakeup_interrupt,
  2198. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  2199. "swr_wake_irq", swrm);
  2200. if (ret) {
  2201. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  2202. __func__, ret);
  2203. return -EINVAL;
  2204. }
  2205. irq_set_irq_wake(swrm->wake_irq, 1);
  2206. }
  2207. return ret;
  2208. }
  2209. static int swrm_alloc_port_mem(struct device *dev, struct swr_mstr_ctrl *swrm,
  2210. u32 uc, u32 size)
  2211. {
  2212. if (!swrm->port_param) {
  2213. swrm->port_param = devm_kzalloc(dev,
  2214. sizeof(swrm->port_param) * SWR_UC_MAX,
  2215. GFP_KERNEL);
  2216. if (!swrm->port_param)
  2217. return -ENOMEM;
  2218. }
  2219. if (!swrm->port_param[uc]) {
  2220. swrm->port_param[uc] = devm_kcalloc(dev, size,
  2221. sizeof(struct port_params),
  2222. GFP_KERNEL);
  2223. if (!swrm->port_param[uc])
  2224. return -ENOMEM;
  2225. } else {
  2226. dev_err_ratelimited(swrm->dev, "%s: called more than once\n",
  2227. __func__);
  2228. }
  2229. return 0;
  2230. }
  2231. static int swrm_copy_port_config(struct swr_mstr_ctrl *swrm,
  2232. struct swrm_port_config *port_cfg,
  2233. u32 size)
  2234. {
  2235. int idx;
  2236. struct port_params *params;
  2237. int uc = port_cfg->uc;
  2238. int ret = 0;
  2239. for (idx = 0; idx < size; idx++) {
  2240. params = &((struct port_params *)port_cfg->params)[idx];
  2241. if (!params) {
  2242. dev_err(swrm->dev, "%s: Invalid params\n", __func__);
  2243. ret = -EINVAL;
  2244. break;
  2245. }
  2246. memcpy(&swrm->port_param[uc][idx], params,
  2247. sizeof(struct port_params));
  2248. }
  2249. return ret;
  2250. }
  2251. /**
  2252. * swrm_wcd_notify - parent device can notify to soundwire master through
  2253. * this function
  2254. * @pdev: pointer to platform device structure
  2255. * @id: command id from parent to the soundwire master
  2256. * @data: data from parent device to soundwire master
  2257. */
  2258. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  2259. {
  2260. struct swr_mstr_ctrl *swrm;
  2261. int ret = 0;
  2262. struct swr_master *mstr;
  2263. struct swr_device *swr_dev;
  2264. struct swrm_port_config *port_cfg;
  2265. if (!pdev) {
  2266. pr_err("%s: pdev is NULL\n", __func__);
  2267. return -EINVAL;
  2268. }
  2269. swrm = platform_get_drvdata(pdev);
  2270. if (!swrm) {
  2271. dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
  2272. return -EINVAL;
  2273. }
  2274. mstr = &swrm->master;
  2275. switch (id) {
  2276. case SWR_CLK_FREQ:
  2277. if (!data) {
  2278. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  2279. ret = -EINVAL;
  2280. } else {
  2281. mutex_lock(&swrm->mlock);
  2282. swrm->mclk_freq = *(int *)data;
  2283. mutex_unlock(&swrm->mlock);
  2284. }
  2285. break;
  2286. case SWR_DEVICE_SSR_DOWN:
  2287. mutex_lock(&swrm->devlock);
  2288. swrm->dev_up = false;
  2289. mutex_unlock(&swrm->devlock);
  2290. mutex_lock(&swrm->reslock);
  2291. swrm->state = SWR_MSTR_SSR;
  2292. mutex_unlock(&swrm->reslock);
  2293. break;
  2294. case SWR_DEVICE_SSR_UP:
  2295. /* wait for clk voting to be zero */
  2296. reinit_completion(&swrm->clk_off_complete);
  2297. if (swrm->clk_ref_count &&
  2298. !wait_for_completion_timeout(&swrm->clk_off_complete,
  2299. msecs_to_jiffies(500)))
  2300. dev_err(swrm->dev, "%s: clock voting not zero\n",
  2301. __func__);
  2302. mutex_lock(&swrm->devlock);
  2303. swrm->dev_up = true;
  2304. mutex_unlock(&swrm->devlock);
  2305. break;
  2306. case SWR_DEVICE_DOWN:
  2307. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  2308. mutex_lock(&swrm->mlock);
  2309. if (swrm->state == SWR_MSTR_DOWN)
  2310. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  2311. __func__, swrm->state);
  2312. else
  2313. swrm_device_down(&pdev->dev);
  2314. mutex_unlock(&swrm->mlock);
  2315. break;
  2316. case SWR_DEVICE_UP:
  2317. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  2318. mutex_lock(&swrm->devlock);
  2319. if (!swrm->dev_up) {
  2320. dev_dbg(swrm->dev, "SSR not complete yet\n");
  2321. mutex_unlock(&swrm->devlock);
  2322. return -EBUSY;
  2323. }
  2324. mutex_unlock(&swrm->devlock);
  2325. mutex_lock(&swrm->mlock);
  2326. pm_runtime_mark_last_busy(&pdev->dev);
  2327. pm_runtime_get_sync(&pdev->dev);
  2328. mutex_lock(&swrm->reslock);
  2329. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  2330. ret = swr_reset_device(swr_dev);
  2331. if (ret) {
  2332. dev_err(swrm->dev,
  2333. "%s: failed to reset swr device %d\n",
  2334. __func__, swr_dev->dev_num);
  2335. swrm_clk_request(swrm, false);
  2336. }
  2337. }
  2338. pm_runtime_mark_last_busy(&pdev->dev);
  2339. pm_runtime_put_autosuspend(&pdev->dev);
  2340. mutex_unlock(&swrm->reslock);
  2341. mutex_unlock(&swrm->mlock);
  2342. break;
  2343. case SWR_SET_NUM_RX_CH:
  2344. if (!data) {
  2345. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  2346. ret = -EINVAL;
  2347. } else {
  2348. mutex_lock(&swrm->mlock);
  2349. swrm->num_rx_chs = *(int *)data;
  2350. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  2351. list_for_each_entry(swr_dev, &mstr->devices,
  2352. dev_list) {
  2353. ret = swr_set_device_group(swr_dev,
  2354. SWR_BROADCAST);
  2355. if (ret)
  2356. dev_err(swrm->dev,
  2357. "%s: set num ch failed\n",
  2358. __func__);
  2359. }
  2360. } else {
  2361. list_for_each_entry(swr_dev, &mstr->devices,
  2362. dev_list) {
  2363. ret = swr_set_device_group(swr_dev,
  2364. SWR_GROUP_NONE);
  2365. if (ret)
  2366. dev_err(swrm->dev,
  2367. "%s: set num ch failed\n",
  2368. __func__);
  2369. }
  2370. }
  2371. mutex_unlock(&swrm->mlock);
  2372. }
  2373. break;
  2374. case SWR_REGISTER_WAKE_IRQ:
  2375. if (!data) {
  2376. dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
  2377. __func__);
  2378. ret = -EINVAL;
  2379. } else {
  2380. mutex_lock(&swrm->mlock);
  2381. swrm->ipc_wakeup = *(u32 *)data;
  2382. ret = swrm_register_wake_irq(swrm);
  2383. if (ret)
  2384. dev_err(swrm->dev, "%s: register wake_irq failed\n",
  2385. __func__);
  2386. mutex_unlock(&swrm->mlock);
  2387. }
  2388. break;
  2389. case SWR_SET_PORT_MAP:
  2390. if (!data) {
  2391. dev_err(swrm->dev, "%s: data is NULL for id=%d\n",
  2392. __func__, id);
  2393. ret = -EINVAL;
  2394. } else {
  2395. mutex_lock(&swrm->mlock);
  2396. port_cfg = (struct swrm_port_config *)data;
  2397. if (!port_cfg->size) {
  2398. ret = -EINVAL;
  2399. goto done;
  2400. }
  2401. ret = swrm_alloc_port_mem(&pdev->dev, swrm,
  2402. port_cfg->uc, port_cfg->size);
  2403. if (!ret)
  2404. swrm_copy_port_config(swrm, port_cfg,
  2405. port_cfg->size);
  2406. done:
  2407. mutex_unlock(&swrm->mlock);
  2408. }
  2409. break;
  2410. default:
  2411. dev_err(swrm->dev, "%s: swr master unknown id %d\n",
  2412. __func__, id);
  2413. break;
  2414. }
  2415. return ret;
  2416. }
  2417. EXPORT_SYMBOL(swrm_wcd_notify);
  2418. /*
  2419. * swrm_pm_cmpxchg:
  2420. * Check old state and exchange with pm new state
  2421. * if old state matches with current state
  2422. *
  2423. * @swrm: pointer to wcd core resource
  2424. * @o: pm old state
  2425. * @n: pm new state
  2426. *
  2427. * Returns old state
  2428. */
  2429. static enum swrm_pm_state swrm_pm_cmpxchg(
  2430. struct swr_mstr_ctrl *swrm,
  2431. enum swrm_pm_state o,
  2432. enum swrm_pm_state n)
  2433. {
  2434. enum swrm_pm_state old;
  2435. if (!swrm)
  2436. return o;
  2437. mutex_lock(&swrm->pm_lock);
  2438. old = swrm->pm_state;
  2439. if (old == o)
  2440. swrm->pm_state = n;
  2441. mutex_unlock(&swrm->pm_lock);
  2442. return old;
  2443. }
  2444. static bool swrm_lock_sleep(struct swr_mstr_ctrl *swrm)
  2445. {
  2446. enum swrm_pm_state os;
  2447. /*
  2448. * swrm_{lock/unlock}_sleep will be called by swr irq handler
  2449. * and slave wake up requests..
  2450. *
  2451. * If system didn't resume, we can simply return false so
  2452. * IRQ handler can return without handling IRQ.
  2453. */
  2454. mutex_lock(&swrm->pm_lock);
  2455. if (swrm->wlock_holders++ == 0) {
  2456. dev_dbg(swrm->dev, "%s: holding wake lock\n", __func__);
  2457. pm_qos_update_request(&swrm->pm_qos_req,
  2458. msm_cpuidle_get_deep_idle_latency());
  2459. pm_stay_awake(swrm->dev);
  2460. }
  2461. mutex_unlock(&swrm->pm_lock);
  2462. if (!wait_event_timeout(swrm->pm_wq,
  2463. ((os = swrm_pm_cmpxchg(swrm,
  2464. SWRM_PM_SLEEPABLE,
  2465. SWRM_PM_AWAKE)) ==
  2466. SWRM_PM_SLEEPABLE ||
  2467. (os == SWRM_PM_AWAKE)),
  2468. msecs_to_jiffies(
  2469. SWRM_SYSTEM_RESUME_TIMEOUT_MS))) {
  2470. dev_err(swrm->dev, "%s: system didn't resume within %dms, s %d, w %d\n",
  2471. __func__, SWRM_SYSTEM_RESUME_TIMEOUT_MS, swrm->pm_state,
  2472. swrm->wlock_holders);
  2473. swrm_unlock_sleep(swrm);
  2474. return false;
  2475. }
  2476. wake_up_all(&swrm->pm_wq);
  2477. return true;
  2478. }
  2479. static void swrm_unlock_sleep(struct swr_mstr_ctrl *swrm)
  2480. {
  2481. mutex_lock(&swrm->pm_lock);
  2482. if (--swrm->wlock_holders == 0) {
  2483. dev_dbg(swrm->dev, "%s: releasing wake lock pm_state %d -> %d\n",
  2484. __func__, swrm->pm_state, SWRM_PM_SLEEPABLE);
  2485. /*
  2486. * if swrm_lock_sleep failed, pm_state would be still
  2487. * swrm_PM_ASLEEP, don't overwrite
  2488. */
  2489. if (likely(swrm->pm_state == SWRM_PM_AWAKE))
  2490. swrm->pm_state = SWRM_PM_SLEEPABLE;
  2491. pm_qos_update_request(&swrm->pm_qos_req,
  2492. PM_QOS_DEFAULT_VALUE);
  2493. pm_relax(swrm->dev);
  2494. }
  2495. mutex_unlock(&swrm->pm_lock);
  2496. wake_up_all(&swrm->pm_wq);
  2497. }
  2498. #ifdef CONFIG_PM_SLEEP
  2499. static int swrm_suspend(struct device *dev)
  2500. {
  2501. int ret = -EBUSY;
  2502. struct platform_device *pdev = to_platform_device(dev);
  2503. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2504. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  2505. mutex_lock(&swrm->pm_lock);
  2506. if (swrm->pm_state == SWRM_PM_SLEEPABLE) {
  2507. dev_dbg(swrm->dev, "%s: suspending system, state %d, wlock %d\n",
  2508. __func__, swrm->pm_state,
  2509. swrm->wlock_holders);
  2510. swrm->pm_state = SWRM_PM_ASLEEP;
  2511. } else if (swrm->pm_state == SWRM_PM_AWAKE) {
  2512. /*
  2513. * unlock to wait for pm_state == SWRM_PM_SLEEPABLE
  2514. * then set to SWRM_PM_ASLEEP
  2515. */
  2516. dev_dbg(swrm->dev, "%s: waiting to suspend system, state %d, wlock %d\n",
  2517. __func__, swrm->pm_state,
  2518. swrm->wlock_holders);
  2519. mutex_unlock(&swrm->pm_lock);
  2520. if (!(wait_event_timeout(swrm->pm_wq, swrm_pm_cmpxchg(
  2521. swrm, SWRM_PM_SLEEPABLE,
  2522. SWRM_PM_ASLEEP) ==
  2523. SWRM_PM_SLEEPABLE,
  2524. msecs_to_jiffies(
  2525. SWRM_SYS_SUSPEND_WAIT)))) {
  2526. dev_dbg(swrm->dev, "%s: suspend failed state %d, wlock %d\n",
  2527. __func__, swrm->pm_state,
  2528. swrm->wlock_holders);
  2529. return -EBUSY;
  2530. } else {
  2531. dev_dbg(swrm->dev,
  2532. "%s: done, state %d, wlock %d\n",
  2533. __func__, swrm->pm_state,
  2534. swrm->wlock_holders);
  2535. }
  2536. mutex_lock(&swrm->pm_lock);
  2537. } else if (swrm->pm_state == SWRM_PM_ASLEEP) {
  2538. dev_dbg(swrm->dev, "%s: system is already suspended, state %d, wlock %d\n",
  2539. __func__, swrm->pm_state,
  2540. swrm->wlock_holders);
  2541. }
  2542. mutex_unlock(&swrm->pm_lock);
  2543. if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
  2544. ret = swrm_runtime_suspend(dev);
  2545. if (!ret) {
  2546. /*
  2547. * Synchronize runtime-pm and system-pm states:
  2548. * At this point, we are already suspended. If
  2549. * runtime-pm still thinks its active, then
  2550. * make sure its status is in sync with HW
  2551. * status. The three below calls let the
  2552. * runtime-pm know that we are suspended
  2553. * already without re-invoking the suspend
  2554. * callback
  2555. */
  2556. pm_runtime_disable(dev);
  2557. pm_runtime_set_suspended(dev);
  2558. pm_runtime_enable(dev);
  2559. }
  2560. }
  2561. if (ret == -EBUSY) {
  2562. /*
  2563. * There is a possibility that some audio stream is active
  2564. * during suspend. We dont want to return suspend failure in
  2565. * that case so that display and relevant components can still
  2566. * go to suspend.
  2567. * If there is some other error, then it should be passed-on
  2568. * to system level suspend
  2569. */
  2570. ret = 0;
  2571. }
  2572. return ret;
  2573. }
  2574. static int swrm_resume(struct device *dev)
  2575. {
  2576. int ret = 0;
  2577. struct platform_device *pdev = to_platform_device(dev);
  2578. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2579. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  2580. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  2581. ret = swrm_runtime_resume(dev);
  2582. if (!ret) {
  2583. pm_runtime_mark_last_busy(dev);
  2584. pm_request_autosuspend(dev);
  2585. }
  2586. }
  2587. mutex_lock(&swrm->pm_lock);
  2588. if (swrm->pm_state == SWRM_PM_ASLEEP) {
  2589. dev_dbg(swrm->dev,
  2590. "%s: resuming system, state %d, wlock %d\n",
  2591. __func__, swrm->pm_state,
  2592. swrm->wlock_holders);
  2593. swrm->pm_state = SWRM_PM_SLEEPABLE;
  2594. } else {
  2595. dev_dbg(swrm->dev, "%s: system is already awake, state %d wlock %d\n",
  2596. __func__, swrm->pm_state,
  2597. swrm->wlock_holders);
  2598. }
  2599. mutex_unlock(&swrm->pm_lock);
  2600. wake_up_all(&swrm->pm_wq);
  2601. return ret;
  2602. }
  2603. #endif /* CONFIG_PM_SLEEP */
  2604. static const struct dev_pm_ops swrm_dev_pm_ops = {
  2605. SET_SYSTEM_SLEEP_PM_OPS(
  2606. swrm_suspend,
  2607. swrm_resume
  2608. )
  2609. SET_RUNTIME_PM_OPS(
  2610. swrm_runtime_suspend,
  2611. swrm_runtime_resume,
  2612. NULL
  2613. )
  2614. };
  2615. static const struct of_device_id swrm_dt_match[] = {
  2616. {
  2617. .compatible = "qcom,swr-mstr",
  2618. },
  2619. {}
  2620. };
  2621. static struct platform_driver swr_mstr_driver = {
  2622. .probe = swrm_probe,
  2623. .remove = swrm_remove,
  2624. .driver = {
  2625. .name = SWR_WCD_NAME,
  2626. .owner = THIS_MODULE,
  2627. .pm = &swrm_dev_pm_ops,
  2628. .of_match_table = swrm_dt_match,
  2629. .suppress_bind_attrs = true,
  2630. },
  2631. };
  2632. static int __init swrm_init(void)
  2633. {
  2634. return platform_driver_register(&swr_mstr_driver);
  2635. }
  2636. module_init(swrm_init);
  2637. static void __exit swrm_exit(void)
  2638. {
  2639. platform_driver_unregister(&swr_mstr_driver);
  2640. }
  2641. module_exit(swrm_exit);
  2642. MODULE_LICENSE("GPL v2");
  2643. MODULE_DESCRIPTION("SoundWire Master Controller");
  2644. MODULE_ALIAS("platform:swr-mstr");