swr-mstr-ctrl.c 85 KB

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