swr-mstr-ctrl.c 75 KB

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