swr-mstr-ctrl.c 87 KB

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