swr-mstr-ctrl.c 76 KB

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