swr-mstr-ctrl.c 76 KB

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