swr-mstr-ctrl.c 86 KB

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