swr-mstr-ctrl.c 82 KB

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