swr-mstr-ctrl.c 84 KB

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