swr-mstr-ctrl.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  1. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/irq.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/delay.h>
  20. #include <linux/kthread.h>
  21. #include <linux/bitops.h>
  22. #include <linux/clk.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/of.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/uaccess.h>
  27. #include <soc/soundwire.h>
  28. #include <soc/swr-wcd.h>
  29. #include <linux/regmap.h>
  30. #include <dsp/msm-audio-event-notify.h>
  31. #include "swrm_registers.h"
  32. #include "swr-mstr-ctrl.h"
  33. #include "swrm_port_config.h"
  34. #define SWR_BROADCAST_CMD_ID 0x0F
  35. #define SWR_AUTO_SUSPEND_DELAY 3 /* delay in sec */
  36. #define SWR_DEV_ID_MASK 0xFFFFFFFFFFFF
  37. #define SWR_REG_VAL_PACK(data, dev, id, reg) \
  38. ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
  39. #define SWR_INVALID_PARAM 0xFF
  40. #define SWR_HSTOP_MAX_VAL 0xF
  41. #define SWR_HSTART_MIN_VAL 0x0
  42. #define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
  43. /* pm runtime auto suspend timer in msecs */
  44. static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
  45. module_param(auto_suspend_timer, int, 0664);
  46. MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
  47. enum {
  48. SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
  49. SWR_ATTACHED_OK, /* Device is attached */
  50. SWR_ALERT, /* Device alters master for any interrupts */
  51. SWR_RESERVED, /* Reserved */
  52. };
  53. enum {
  54. MASTER_ID_WSA = 1,
  55. MASTER_ID_RX,
  56. MASTER_ID_TX
  57. };
  58. enum {
  59. ENABLE_PENDING,
  60. DISABLE_PENDING
  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 SWR_MSTR_MAX_REG_ADDR 0x1740
  67. #define SWR_MSTR_START_REG_ADDR 0x00
  68. #define SWR_MSTR_MAX_BUF_LEN 32
  69. #define BYTES_PER_LINE 12
  70. #define SWR_MSTR_RD_BUF_LEN 8
  71. #define SWR_MSTR_WR_BUF_LEN 32
  72. #define MAX_FIFO_RD_FAIL_RETRY 3
  73. static struct swr_mstr_ctrl *dbgswrm;
  74. static struct dentry *debugfs_swrm_dent;
  75. static struct dentry *debugfs_peek;
  76. static struct dentry *debugfs_poke;
  77. static struct dentry *debugfs_reg_dump;
  78. static unsigned int read_data;
  79. static bool swrm_is_msm_variant(int val)
  80. {
  81. return (val == SWRM_VERSION_1_3);
  82. }
  83. static int swrm_debug_open(struct inode *inode, struct file *file)
  84. {
  85. file->private_data = inode->i_private;
  86. return 0;
  87. }
  88. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  89. {
  90. char *token;
  91. int base, cnt;
  92. token = strsep(&buf, " ");
  93. for (cnt = 0; cnt < num_of_par; cnt++) {
  94. if (token) {
  95. if ((token[1] == 'x') || (token[1] == 'X'))
  96. base = 16;
  97. else
  98. base = 10;
  99. if (kstrtou32(token, base, &param1[cnt]) != 0)
  100. return -EINVAL;
  101. token = strsep(&buf, " ");
  102. } else
  103. return -EINVAL;
  104. }
  105. return 0;
  106. }
  107. static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
  108. loff_t *ppos)
  109. {
  110. int i, reg_val, len;
  111. ssize_t total = 0;
  112. char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
  113. if (!ubuf || !ppos)
  114. return 0;
  115. for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
  116. i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
  117. reg_val = dbgswrm->read(dbgswrm->handle, i);
  118. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
  119. if ((total + len) >= count - 1)
  120. break;
  121. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  122. pr_err("%s: fail to copy reg dump\n", __func__);
  123. total = -EFAULT;
  124. goto copy_err;
  125. }
  126. *ppos += len;
  127. total += len;
  128. }
  129. copy_err:
  130. return total;
  131. }
  132. static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
  133. size_t count, loff_t *ppos)
  134. {
  135. char lbuf[SWR_MSTR_RD_BUF_LEN];
  136. char *access_str;
  137. ssize_t ret_cnt;
  138. if (!count || !file || !ppos || !ubuf)
  139. return -EINVAL;
  140. access_str = file->private_data;
  141. if (*ppos < 0)
  142. return -EINVAL;
  143. if (!strcmp(access_str, "swrm_peek")) {
  144. snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
  145. ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
  146. strnlen(lbuf, 7));
  147. } else if (!strcmp(access_str, "swrm_reg_dump")) {
  148. ret_cnt = swrm_reg_show(ubuf, count, ppos);
  149. } else {
  150. pr_err("%s: %s not permitted to read\n", __func__, access_str);
  151. ret_cnt = -EPERM;
  152. }
  153. return ret_cnt;
  154. }
  155. static ssize_t swrm_debug_write(struct file *filp,
  156. const char __user *ubuf, size_t cnt, loff_t *ppos)
  157. {
  158. char lbuf[SWR_MSTR_WR_BUF_LEN];
  159. int rc;
  160. u32 param[5];
  161. char *access_str;
  162. if (!filp || !ppos || !ubuf)
  163. return -EINVAL;
  164. access_str = filp->private_data;
  165. if (cnt > sizeof(lbuf) - 1)
  166. return -EINVAL;
  167. rc = copy_from_user(lbuf, ubuf, cnt);
  168. if (rc)
  169. return -EFAULT;
  170. lbuf[cnt] = '\0';
  171. if (!strcmp(access_str, "swrm_poke")) {
  172. /* write */
  173. rc = get_parameters(lbuf, param, 2);
  174. if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
  175. (param[1] <= 0xFFFFFFFF) &&
  176. (rc == 0))
  177. rc = dbgswrm->write(dbgswrm->handle, param[0],
  178. param[1]);
  179. else
  180. rc = -EINVAL;
  181. } else if (!strcmp(access_str, "swrm_peek")) {
  182. /* read */
  183. rc = get_parameters(lbuf, param, 1);
  184. if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
  185. read_data = dbgswrm->read(dbgswrm->handle, param[0]);
  186. else
  187. rc = -EINVAL;
  188. }
  189. if (rc == 0)
  190. rc = cnt;
  191. else
  192. pr_err("%s: rc = %d\n", __func__, rc);
  193. return rc;
  194. }
  195. static const struct file_operations swrm_debug_ops = {
  196. .open = swrm_debug_open,
  197. .write = swrm_debug_write,
  198. .read = swrm_debug_read,
  199. };
  200. static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
  201. {
  202. int ret = 0;
  203. if (!swrm->clk || !swrm->handle)
  204. return -EINVAL;
  205. mutex_lock(&swrm->clklock);
  206. if (enable) {
  207. if (!swrm->dev_up)
  208. goto exit;
  209. swrm->clk_ref_count++;
  210. if (swrm->clk_ref_count == 1) {
  211. ret = swrm->clk(swrm->handle, true);
  212. if (ret) {
  213. dev_err(swrm->dev,
  214. "%s: clock enable req failed",
  215. __func__);
  216. --swrm->clk_ref_count;
  217. }
  218. }
  219. } else if (--swrm->clk_ref_count == 0) {
  220. swrm->clk(swrm->handle, false);
  221. complete(&swrm->clk_off_complete);
  222. }
  223. if (swrm->clk_ref_count < 0) {
  224. pr_err("%s: swrm clk count mismatch\n", __func__);
  225. swrm->clk_ref_count = 0;
  226. }
  227. exit:
  228. mutex_unlock(&swrm->clklock);
  229. return ret;
  230. }
  231. static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
  232. u16 reg, u32 *value)
  233. {
  234. u32 temp = (u32)(*value);
  235. int ret = 0;
  236. mutex_lock(&swrm->devlock);
  237. if (!swrm->dev_up)
  238. goto err;
  239. ret = swrm_clk_request(swrm, TRUE);
  240. if (ret) {
  241. dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
  242. __func__);
  243. goto err;
  244. }
  245. iowrite32(temp, swrm->swrm_dig_base + reg);
  246. swrm_clk_request(swrm, FALSE);
  247. err:
  248. mutex_unlock(&swrm->devlock);
  249. return ret;
  250. }
  251. static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
  252. u16 reg, u32 *value)
  253. {
  254. u32 temp = 0;
  255. int ret = 0;
  256. mutex_lock(&swrm->devlock);
  257. if (!swrm->dev_up)
  258. goto err;
  259. ret = swrm_clk_request(swrm, TRUE);
  260. if (ret) {
  261. dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
  262. __func__);
  263. goto err;
  264. }
  265. temp = ioread32(swrm->swrm_dig_base + reg);
  266. *value = temp;
  267. swrm_clk_request(swrm, FALSE);
  268. err:
  269. mutex_unlock(&swrm->devlock);
  270. return ret;
  271. }
  272. static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
  273. {
  274. u32 val = 0;
  275. if (swrm->read)
  276. val = swrm->read(swrm->handle, reg_addr);
  277. else
  278. swrm_ahb_read(swrm, reg_addr, &val);
  279. return val;
  280. }
  281. static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
  282. {
  283. if (swrm->write)
  284. swrm->write(swrm->handle, reg_addr, val);
  285. else
  286. swrm_ahb_write(swrm, reg_addr, &val);
  287. }
  288. static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
  289. u32 *val, unsigned int length)
  290. {
  291. int i = 0;
  292. if (swrm->bulk_write)
  293. swrm->bulk_write(swrm->handle, reg_addr, val, length);
  294. else {
  295. mutex_lock(&swrm->iolock);
  296. for (i = 0; i < length; i++) {
  297. /* wait for FIFO WR command to complete to avoid overflow */
  298. usleep_range(100, 105);
  299. swr_master_write(swrm, reg_addr[i], val[i]);
  300. }
  301. mutex_unlock(&swrm->iolock);
  302. }
  303. return 0;
  304. }
  305. static bool swrm_is_port_en(struct swr_master *mstr)
  306. {
  307. return !!(mstr->num_port);
  308. }
  309. static void copy_port_tables(struct swr_mstr_ctrl *swrm,
  310. struct port_params *params)
  311. {
  312. u8 i;
  313. struct port_params *config = params;
  314. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  315. /* wsa uses single frame structure for all configurations */
  316. if (!swrm->mport_cfg[i].port_en)
  317. continue;
  318. swrm->mport_cfg[i].sinterval = config[i].si;
  319. swrm->mport_cfg[i].offset1 = config[i].off1;
  320. swrm->mport_cfg[i].offset2 = config[i].off2;
  321. swrm->mport_cfg[i].hstart = config[i].hstart;
  322. swrm->mport_cfg[i].hstop = config[i].hstop;
  323. swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
  324. swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
  325. swrm->mport_cfg[i].word_length = config[i].wd_len;
  326. swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
  327. }
  328. }
  329. static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
  330. {
  331. struct port_params *params;
  332. switch (swrm->master_id) {
  333. case MASTER_ID_WSA:
  334. params = wsa_frame_superset;
  335. break;
  336. case MASTER_ID_RX:
  337. /* Two RX tables for dsd and without dsd enabled */
  338. if (swrm->mport_cfg[4].port_en)
  339. params = rx_frame_params_dsd;
  340. else
  341. params = rx_frame_params;
  342. break;
  343. case MASTER_ID_TX:
  344. params = tx_frame_params_superset;
  345. break;
  346. default: /* MASTER_GENERIC*/
  347. /* computer generic frame parameters */
  348. return -EINVAL;
  349. }
  350. copy_port_tables(swrm, params);
  351. return 0;
  352. }
  353. static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
  354. u8 *mstr_ch_mask, u8 mstr_prt_type,
  355. u8 slv_port_id)
  356. {
  357. int i, j;
  358. *mstr_port_id = 0;
  359. for (i = 1; i <= swrm->num_ports; i++) {
  360. for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
  361. if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
  362. goto found;
  363. }
  364. }
  365. found:
  366. if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
  367. dev_err(swrm->dev, "%s: port type not supported by master\n",
  368. __func__);
  369. return -EINVAL;
  370. }
  371. /* id 0 corresponds to master port 1 */
  372. *mstr_port_id = i - 1;
  373. *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
  374. return 0;
  375. }
  376. static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
  377. u8 dev_addr, u16 reg_addr)
  378. {
  379. u32 val;
  380. u8 id = *cmd_id;
  381. if (id != SWR_BROADCAST_CMD_ID) {
  382. if (id < 14)
  383. id += 1;
  384. else
  385. id = 0;
  386. *cmd_id = id;
  387. }
  388. val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
  389. return val;
  390. }
  391. static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
  392. u8 dev_addr, u8 cmd_id, u16 reg_addr,
  393. u32 len)
  394. {
  395. u32 val;
  396. u32 retry_attempt = 0;
  397. mutex_lock(&swrm->iolock);
  398. val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
  399. /* wait for FIFO RD to complete to avoid overflow */
  400. usleep_range(100, 105);
  401. swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
  402. /* wait for FIFO RD CMD complete to avoid overflow */
  403. usleep_range(250, 255);
  404. retry_read:
  405. *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
  406. dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
  407. dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
  408. cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
  409. if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
  410. if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
  411. /* wait 500 us before retry on fifo read failure */
  412. usleep_range(500, 505);
  413. retry_attempt++;
  414. goto retry_read;
  415. } else {
  416. dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
  417. rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
  418. __func__, reg_addr, cmd_id, swrm->rcmd_id,
  419. dev_addr, *cmd_data);
  420. dev_err_ratelimited(swrm->dev,
  421. "%s: failed to read fifo\n", __func__);
  422. }
  423. }
  424. mutex_unlock(&swrm->iolock);
  425. return 0;
  426. }
  427. static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
  428. u8 dev_addr, u8 cmd_id, u16 reg_addr)
  429. {
  430. u32 val;
  431. int ret = 0;
  432. mutex_lock(&swrm->iolock);
  433. if (!cmd_id)
  434. val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
  435. dev_addr, reg_addr);
  436. else
  437. val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
  438. dev_addr, reg_addr);
  439. dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
  440. dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
  441. reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
  442. /* wait for FIFO WR command to complete to avoid overflow */
  443. usleep_range(250, 255);
  444. swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
  445. if (cmd_id == 0xF) {
  446. /*
  447. * sleep for 10ms for MSM soundwire variant to allow broadcast
  448. * command to complete.
  449. */
  450. if (swrm_is_msm_variant(swrm->version))
  451. usleep_range(10000, 10100);
  452. else
  453. wait_for_completion_timeout(&swrm->broadcast,
  454. (2 * HZ/10));
  455. }
  456. mutex_unlock(&swrm->iolock);
  457. return ret;
  458. }
  459. static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
  460. void *buf, u32 len)
  461. {
  462. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  463. int ret = 0;
  464. int val;
  465. u8 *reg_val = (u8 *)buf;
  466. if (!swrm) {
  467. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  468. return -EINVAL;
  469. }
  470. if (!dev_num) {
  471. dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
  472. return -EINVAL;
  473. }
  474. mutex_lock(&swrm->devlock);
  475. if (!swrm->dev_up) {
  476. mutex_unlock(&swrm->devlock);
  477. return 0;
  478. }
  479. mutex_unlock(&swrm->devlock);
  480. pm_runtime_get_sync(swrm->dev);
  481. ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
  482. if (!ret)
  483. *reg_val = (u8)val;
  484. pm_runtime_put_autosuspend(swrm->dev);
  485. pm_runtime_mark_last_busy(swrm->dev);
  486. return ret;
  487. }
  488. static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
  489. const void *buf)
  490. {
  491. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  492. int ret = 0;
  493. u8 reg_val = *(u8 *)buf;
  494. if (!swrm) {
  495. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  496. return -EINVAL;
  497. }
  498. if (!dev_num) {
  499. dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
  500. return -EINVAL;
  501. }
  502. mutex_lock(&swrm->devlock);
  503. if (!swrm->dev_up) {
  504. mutex_unlock(&swrm->devlock);
  505. return 0;
  506. }
  507. mutex_unlock(&swrm->devlock);
  508. pm_runtime_get_sync(swrm->dev);
  509. ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
  510. pm_runtime_put_autosuspend(swrm->dev);
  511. pm_runtime_mark_last_busy(swrm->dev);
  512. return ret;
  513. }
  514. static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
  515. const void *buf, size_t len)
  516. {
  517. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  518. int ret = 0;
  519. int i;
  520. u32 *val;
  521. u32 *swr_fifo_reg;
  522. if (!swrm || !swrm->handle) {
  523. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  524. return -EINVAL;
  525. }
  526. if (len <= 0)
  527. return -EINVAL;
  528. mutex_lock(&swrm->devlock);
  529. if (!swrm->dev_up) {
  530. mutex_unlock(&swrm->devlock);
  531. return 0;
  532. }
  533. mutex_unlock(&swrm->devlock);
  534. pm_runtime_get_sync(swrm->dev);
  535. if (dev_num) {
  536. swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
  537. if (!swr_fifo_reg) {
  538. ret = -ENOMEM;
  539. goto err;
  540. }
  541. val = kcalloc(len, sizeof(u32), GFP_KERNEL);
  542. if (!val) {
  543. ret = -ENOMEM;
  544. goto mem_fail;
  545. }
  546. for (i = 0; i < len; i++) {
  547. val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
  548. ((u8 *)buf)[i],
  549. dev_num,
  550. ((u16 *)reg)[i]);
  551. swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
  552. }
  553. ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
  554. if (ret) {
  555. dev_err(&master->dev, "%s: bulk write failed\n",
  556. __func__);
  557. ret = -EINVAL;
  558. }
  559. } else {
  560. dev_err(&master->dev,
  561. "%s: No support of Bulk write for master regs\n",
  562. __func__);
  563. ret = -EINVAL;
  564. goto err;
  565. }
  566. kfree(val);
  567. mem_fail:
  568. kfree(swr_fifo_reg);
  569. err:
  570. pm_runtime_put_autosuspend(swrm->dev);
  571. pm_runtime_mark_last_busy(swrm->dev);
  572. return ret;
  573. }
  574. static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
  575. {
  576. return (swr_master_read(swrm, SWRM_MCP_STATUS) &
  577. SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
  578. }
  579. static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
  580. u8 row, u8 col)
  581. {
  582. swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
  583. SWRS_SCP_FRAME_CTRL_BANK(bank));
  584. }
  585. static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
  586. u8 slv_port, u8 dev_num)
  587. {
  588. struct swr_port_info *port_req = NULL;
  589. list_for_each_entry(port_req, &mport->port_req_list, list) {
  590. /* Store dev_id instead of dev_num if enumeration is changed run_time */
  591. if ((port_req->slave_port_id == slv_port)
  592. && (port_req->dev_num == dev_num))
  593. return port_req;
  594. }
  595. return NULL;
  596. }
  597. static bool swrm_remove_from_group(struct swr_master *master)
  598. {
  599. struct swr_device *swr_dev;
  600. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  601. bool is_removed = false;
  602. if (!swrm)
  603. goto end;
  604. mutex_lock(&swrm->mlock);
  605. if ((swrm->num_rx_chs > 1) &&
  606. (swrm->num_rx_chs == swrm->num_cfg_devs)) {
  607. list_for_each_entry(swr_dev, &master->devices,
  608. dev_list) {
  609. swr_dev->group_id = SWR_GROUP_NONE;
  610. master->gr_sid = 0;
  611. }
  612. is_removed = true;
  613. }
  614. mutex_unlock(&swrm->mlock);
  615. end:
  616. return is_removed;
  617. }
  618. static void swrm_disable_ports(struct swr_master *master,
  619. u8 bank)
  620. {
  621. u32 value;
  622. struct swr_port_info *port_req;
  623. int i;
  624. struct swrm_mports *mport;
  625. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  626. if (!swrm) {
  627. pr_err("%s: swrm is null\n", __func__);
  628. return;
  629. }
  630. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  631. master->num_port);
  632. for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
  633. mport = &(swrm->mport_cfg[i]);
  634. if (!mport->port_en)
  635. continue;
  636. list_for_each_entry(port_req, &mport->port_req_list, list) {
  637. /* skip ports with no change req's*/
  638. if (port_req->req_ch == port_req->ch_en)
  639. continue;
  640. swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
  641. port_req->dev_num, 0x00,
  642. SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
  643. bank));
  644. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
  645. __func__, i,
  646. (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
  647. }
  648. value = ((mport->req_ch)
  649. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  650. value |= ((mport->offset2)
  651. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  652. value |= ((mport->offset1)
  653. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  654. value |= mport->sinterval;
  655. swr_master_write(swrm,
  656. SWRM_DP_PORT_CTRL_BANK(i+1, bank),
  657. value);
  658. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  659. __func__, i,
  660. (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
  661. }
  662. }
  663. static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
  664. {
  665. struct swr_port_info *port_req, *next;
  666. int i;
  667. struct swrm_mports *mport;
  668. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  669. if (!swrm) {
  670. pr_err("%s: swrm is null\n", __func__);
  671. return;
  672. }
  673. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  674. master->num_port);
  675. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  676. mport = &(swrm->mport_cfg[i]);
  677. list_for_each_entry_safe(port_req, next,
  678. &mport->port_req_list, list) {
  679. /* skip ports without new ch req */
  680. if (port_req->ch_en == port_req->req_ch)
  681. continue;
  682. /* remove new ch req's*/
  683. port_req->ch_en = port_req->req_ch;
  684. /* If no streams enabled on port, remove the port req */
  685. if (port_req->ch_en == 0) {
  686. list_del(&port_req->list);
  687. kfree(port_req);
  688. }
  689. }
  690. /* remove new ch req's on mport*/
  691. mport->ch_en = mport->req_ch;
  692. if (!(mport->ch_en)) {
  693. mport->port_en = false;
  694. master->port_en_mask &= ~i;
  695. }
  696. }
  697. }
  698. static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
  699. {
  700. u32 value, slv_id;
  701. struct swr_port_info *port_req;
  702. int i;
  703. struct swrm_mports *mport;
  704. u32 reg[SWRM_MAX_PORT_REG];
  705. u32 val[SWRM_MAX_PORT_REG];
  706. int len = 0;
  707. u8 hparams;
  708. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  709. if (!swrm) {
  710. pr_err("%s: swrm is null\n", __func__);
  711. return;
  712. }
  713. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  714. master->num_port);
  715. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  716. mport = &(swrm->mport_cfg[i]);
  717. if (!mport->port_en)
  718. continue;
  719. list_for_each_entry(port_req, &mport->port_req_list, list) {
  720. slv_id = port_req->slave_port_id;
  721. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  722. val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
  723. port_req->dev_num, 0x00,
  724. SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
  725. bank));
  726. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  727. val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
  728. port_req->dev_num, 0x00,
  729. SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
  730. bank));
  731. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  732. val[len++] = SWR_REG_VAL_PACK(mport->offset1,
  733. port_req->dev_num, 0x00,
  734. SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
  735. bank));
  736. if (mport->offset2 != SWR_INVALID_PARAM) {
  737. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  738. val[len++] = SWR_REG_VAL_PACK(mport->offset2,
  739. port_req->dev_num, 0x00,
  740. SWRS_DP_OFFSET_CONTROL_2_BANK(
  741. slv_id, bank));
  742. }
  743. if (mport->hstart != SWR_INVALID_PARAM
  744. && mport->hstop != SWR_INVALID_PARAM) {
  745. hparams = (mport->hstart << 4) | mport->hstop;
  746. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  747. val[len++] = SWR_REG_VAL_PACK(hparams,
  748. port_req->dev_num, 0x00,
  749. SWRS_DP_HCONTROL_BANK(slv_id,
  750. bank));
  751. }
  752. if (mport->word_length != SWR_INVALID_PARAM) {
  753. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  754. val[len++] =
  755. SWR_REG_VAL_PACK(mport->word_length,
  756. port_req->dev_num, 0x00,
  757. SWRS_DP_BLOCK_CONTROL_1(slv_id));
  758. }
  759. if (mport->blk_pack_mode != SWR_INVALID_PARAM
  760. && swrm->master_id != MASTER_ID_WSA) {
  761. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  762. val[len++] =
  763. SWR_REG_VAL_PACK(mport->blk_pack_mode,
  764. port_req->dev_num, 0x00,
  765. SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
  766. bank));
  767. }
  768. if (mport->blk_grp_count != SWR_INVALID_PARAM) {
  769. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  770. val[len++] =
  771. SWR_REG_VAL_PACK(mport->blk_grp_count,
  772. port_req->dev_num, 0x00,
  773. SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
  774. bank));
  775. }
  776. if (mport->lane_ctrl != SWR_INVALID_PARAM) {
  777. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  778. val[len++] =
  779. SWR_REG_VAL_PACK(mport->lane_ctrl,
  780. port_req->dev_num, 0x00,
  781. SWRS_DP_LANE_CONTROL_BANK(slv_id,
  782. bank));
  783. }
  784. port_req->ch_en = port_req->req_ch;
  785. }
  786. value = ((mport->req_ch)
  787. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  788. if (mport->offset2 != SWR_INVALID_PARAM)
  789. value |= ((mport->offset2)
  790. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  791. value |= ((mport->offset1)
  792. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  793. value |= mport->sinterval;
  794. reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
  795. val[len++] = value;
  796. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  797. __func__, i,
  798. (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
  799. if (mport->lane_ctrl != SWR_INVALID_PARAM) {
  800. reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
  801. val[len++] = mport->lane_ctrl;
  802. }
  803. if (mport->word_length != SWR_INVALID_PARAM) {
  804. reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
  805. val[len++] = mport->word_length;
  806. }
  807. if (mport->blk_grp_count != SWR_INVALID_PARAM) {
  808. reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
  809. val[len++] = mport->blk_grp_count;
  810. }
  811. if (mport->hstart != SWR_INVALID_PARAM
  812. && mport->hstop != SWR_INVALID_PARAM) {
  813. reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
  814. hparams = (mport->hstop << 4) | mport->hstart;
  815. val[len++] = hparams;
  816. } else {
  817. reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
  818. hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
  819. val[len++] = hparams;
  820. }
  821. if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
  822. reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
  823. val[len++] = mport->blk_pack_mode;
  824. }
  825. mport->ch_en = mport->req_ch;
  826. }
  827. swr_master_bulk_write(swrm, reg, val, len);
  828. }
  829. static void swrm_apply_port_config(struct swr_master *master)
  830. {
  831. u8 bank;
  832. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  833. if (!swrm) {
  834. pr_err("%s: Invalid handle to swr controller\n",
  835. __func__);
  836. return;
  837. }
  838. bank = get_inactive_bank_num(swrm);
  839. dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
  840. __func__, bank, master->num_port);
  841. swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
  842. SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
  843. swrm_copy_data_port_config(master, bank);
  844. }
  845. static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
  846. {
  847. u8 bank;
  848. u32 value, n_row, n_col;
  849. int ret;
  850. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  851. int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
  852. SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
  853. SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
  854. u8 inactive_bank;
  855. if (!swrm) {
  856. pr_err("%s: swrm is null\n", __func__);
  857. return -EFAULT;
  858. }
  859. mutex_lock(&swrm->mlock);
  860. bank = get_inactive_bank_num(swrm);
  861. if (enable) {
  862. if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
  863. dev_dbg(swrm->dev, "%s:No pending connect port req\n",
  864. __func__);
  865. goto exit;
  866. }
  867. clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
  868. ret = swrm_get_port_config(swrm);
  869. if (ret) {
  870. /* cannot accommodate ports */
  871. swrm_cleanup_disabled_port_reqs(master);
  872. mutex_unlock(&swrm->mlock);
  873. return -EINVAL;
  874. }
  875. swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
  876. SWRM_INTERRUPT_STATUS_MASK);
  877. /* apply the new port config*/
  878. swrm_apply_port_config(master);
  879. } else {
  880. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  881. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  882. __func__);
  883. goto exit;
  884. }
  885. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  886. swrm_disable_ports(master, bank);
  887. }
  888. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
  889. __func__, enable, swrm->num_cfg_devs);
  890. if (enable) {
  891. /* set col = 16 */
  892. n_col = SWR_MAX_COL;
  893. } else {
  894. /*
  895. * Do not change to col = 2 if there are still active ports
  896. */
  897. if (!master->num_port)
  898. n_col = SWR_MIN_COL;
  899. else
  900. n_col = SWR_MAX_COL;
  901. }
  902. /* Use default 50 * x, frame shape. Change based on mclk */
  903. if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
  904. dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
  905. n_col ? 16 : 2);
  906. n_row = SWR_ROW_64;
  907. } else {
  908. dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
  909. n_col ? 16 : 2);
  910. n_row = SWR_ROW_50;
  911. }
  912. value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
  913. value &= (~mask);
  914. value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  915. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  916. (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  917. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  918. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  919. SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  920. enable_bank_switch(swrm, bank, n_row, n_col);
  921. inactive_bank = bank ? 0 : 1;
  922. if (enable)
  923. swrm_copy_data_port_config(master, inactive_bank);
  924. else {
  925. swrm_disable_ports(master, inactive_bank);
  926. swrm_cleanup_disabled_port_reqs(master);
  927. }
  928. if (!swrm_is_port_en(master)) {
  929. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  930. __func__);
  931. pm_runtime_mark_last_busy(swrm->dev);
  932. pm_runtime_put_autosuspend(swrm->dev);
  933. }
  934. exit:
  935. mutex_unlock(&swrm->mlock);
  936. return 0;
  937. }
  938. static int swrm_connect_port(struct swr_master *master,
  939. struct swr_params *portinfo)
  940. {
  941. int i;
  942. struct swr_port_info *port_req;
  943. int ret = 0;
  944. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  945. struct swrm_mports *mport;
  946. u8 mstr_port_id, mstr_ch_msk;
  947. dev_dbg(&master->dev, "%s: enter\n", __func__);
  948. if (!portinfo)
  949. return -EINVAL;
  950. if (!swrm) {
  951. dev_err(&master->dev,
  952. "%s: Invalid handle to swr controller\n",
  953. __func__);
  954. return -EINVAL;
  955. }
  956. mutex_lock(&swrm->mlock);
  957. mutex_lock(&swrm->devlock);
  958. if (!swrm->dev_up) {
  959. mutex_unlock(&swrm->devlock);
  960. mutex_unlock(&swrm->mlock);
  961. return -EINVAL;
  962. }
  963. mutex_unlock(&swrm->devlock);
  964. if (!swrm_is_port_en(master))
  965. pm_runtime_get_sync(swrm->dev);
  966. for (i = 0; i < portinfo->num_port; i++) {
  967. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
  968. portinfo->port_type[i],
  969. portinfo->port_id[i]);
  970. if (ret) {
  971. dev_err(&master->dev,
  972. "%s: mstr portid for slv port %d not found\n",
  973. __func__, portinfo->port_id[i]);
  974. goto port_fail;
  975. }
  976. mport = &(swrm->mport_cfg[mstr_port_id]);
  977. /* get port req */
  978. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  979. portinfo->dev_num);
  980. if (!port_req) {
  981. dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
  982. __func__, portinfo->port_id[i],
  983. portinfo->dev_num);
  984. port_req = kzalloc(sizeof(struct swr_port_info),
  985. GFP_KERNEL);
  986. if (!port_req) {
  987. ret = -ENOMEM;
  988. goto mem_fail;
  989. }
  990. port_req->dev_num = portinfo->dev_num;
  991. port_req->slave_port_id = portinfo->port_id[i];
  992. port_req->num_ch = portinfo->num_ch[i];
  993. port_req->ch_rate = portinfo->ch_rate[i];
  994. port_req->ch_en = 0;
  995. port_req->master_port_id = mstr_port_id;
  996. list_add(&port_req->list, &mport->port_req_list);
  997. }
  998. port_req->req_ch |= portinfo->ch_en[i];
  999. dev_dbg(&master->dev,
  1000. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
  1001. __func__, port_req->master_port_id,
  1002. port_req->slave_port_id, port_req->ch_rate,
  1003. port_req->num_ch);
  1004. /* Put the port req on master port */
  1005. mport = &(swrm->mport_cfg[mstr_port_id]);
  1006. mport->port_en = true;
  1007. mport->req_ch |= mstr_ch_msk;
  1008. master->port_en_mask |= (1 << mstr_port_id);
  1009. }
  1010. master->num_port += portinfo->num_port;
  1011. set_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1012. swr_port_response(master, portinfo->tid);
  1013. mutex_unlock(&swrm->mlock);
  1014. return 0;
  1015. port_fail:
  1016. mem_fail:
  1017. /* cleanup port reqs in error condition */
  1018. swrm_cleanup_disabled_port_reqs(master);
  1019. mutex_unlock(&swrm->mlock);
  1020. return ret;
  1021. }
  1022. static int swrm_disconnect_port(struct swr_master *master,
  1023. struct swr_params *portinfo)
  1024. {
  1025. int i, ret = 0;
  1026. struct swr_port_info *port_req;
  1027. struct swrm_mports *mport;
  1028. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1029. u8 mstr_port_id, mstr_ch_mask;
  1030. if (!swrm) {
  1031. dev_err(&master->dev,
  1032. "%s: Invalid handle to swr controller\n",
  1033. __func__);
  1034. return -EINVAL;
  1035. }
  1036. if (!portinfo) {
  1037. dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
  1038. return -EINVAL;
  1039. }
  1040. mutex_lock(&swrm->mlock);
  1041. for (i = 0; i < portinfo->num_port; i++) {
  1042. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
  1043. portinfo->port_type[i], portinfo->port_id[i]);
  1044. if (ret) {
  1045. dev_err(&master->dev,
  1046. "%s: mstr portid for slv port %d not found\n",
  1047. __func__, portinfo->port_id[i]);
  1048. mutex_unlock(&swrm->mlock);
  1049. return -EINVAL;
  1050. }
  1051. mport = &(swrm->mport_cfg[mstr_port_id]);
  1052. /* get port req */
  1053. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1054. portinfo->dev_num);
  1055. if (!port_req) {
  1056. dev_err(&master->dev, "%s:port not enabled : port %d\n",
  1057. __func__, portinfo->port_id[i]);
  1058. mutex_unlock(&swrm->mlock);
  1059. return -EINVAL;
  1060. }
  1061. port_req->req_ch &= ~portinfo->ch_en[i];
  1062. mport->req_ch &= ~mstr_ch_mask;
  1063. }
  1064. master->num_port -= portinfo->num_port;
  1065. set_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1066. swr_port_response(master, portinfo->tid);
  1067. mutex_unlock(&swrm->mlock);
  1068. return 0;
  1069. }
  1070. static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
  1071. int status, u8 *devnum)
  1072. {
  1073. int i;
  1074. bool found = false;
  1075. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1076. if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
  1077. *devnum = i;
  1078. found = true;
  1079. break;
  1080. }
  1081. status >>= 2;
  1082. }
  1083. if (found)
  1084. return 0;
  1085. else
  1086. return -EINVAL;
  1087. }
  1088. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  1089. int status, u8 *devnum)
  1090. {
  1091. int i;
  1092. int new_sts = status;
  1093. int ret = SWR_NOT_PRESENT;
  1094. if (status != swrm->slave_status) {
  1095. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1096. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  1097. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  1098. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  1099. *devnum = i;
  1100. break;
  1101. }
  1102. status >>= 2;
  1103. swrm->slave_status >>= 2;
  1104. }
  1105. swrm->slave_status = new_sts;
  1106. }
  1107. return ret;
  1108. }
  1109. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  1110. {
  1111. struct swr_mstr_ctrl *swrm = dev;
  1112. u32 value, intr_sts, intr_mask;
  1113. u32 temp = 0;
  1114. u32 status, chg_sts, i;
  1115. u8 devnum = 0;
  1116. int ret = IRQ_HANDLED;
  1117. struct swr_device *swr_dev;
  1118. struct swr_master *mstr = &swrm->master;
  1119. mutex_lock(&swrm->reslock);
  1120. swrm_clk_request(swrm, true);
  1121. mutex_unlock(&swrm->reslock);
  1122. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1123. intr_mask = swr_master_read(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN);
  1124. intr_sts &= intr_mask;
  1125. handle_irq:
  1126. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  1127. value = intr_sts & (1 << i);
  1128. if (!value)
  1129. continue;
  1130. switch (value) {
  1131. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  1132. dev_dbg(swrm->dev, "Trigger irq to slave device\n");
  1133. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1134. ret = swrm_find_alert_slave(swrm, status, &devnum);
  1135. if (ret) {
  1136. dev_err_ratelimited(swrm->dev,
  1137. "no slave alert found.spurious interrupt\n");
  1138. break;
  1139. }
  1140. swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
  1141. SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  1142. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1143. SWRS_SCP_INT_STATUS_CLEAR_1);
  1144. swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
  1145. SWRS_SCP_INT_STATUS_CLEAR_1);
  1146. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1147. if (swr_dev->dev_num != devnum)
  1148. continue;
  1149. if (swr_dev->slave_irq) {
  1150. do {
  1151. handle_nested_irq(
  1152. irq_find_mapping(
  1153. swr_dev->slave_irq, 0));
  1154. } while (swr_dev->slave_irq_pending);
  1155. }
  1156. }
  1157. break;
  1158. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  1159. dev_dbg(swrm->dev, "SWR new slave attached\n");
  1160. break;
  1161. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  1162. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1163. if (status == swrm->slave_status) {
  1164. dev_dbg(swrm->dev,
  1165. "%s: No change in slave status: %d\n",
  1166. __func__, status);
  1167. break;
  1168. }
  1169. chg_sts = swrm_check_slave_change_status(swrm, status,
  1170. &devnum);
  1171. switch (chg_sts) {
  1172. case SWR_NOT_PRESENT:
  1173. dev_dbg(swrm->dev, "device %d got detached\n",
  1174. devnum);
  1175. break;
  1176. case SWR_ATTACHED_OK:
  1177. dev_dbg(swrm->dev, "device %d got attached\n",
  1178. devnum);
  1179. /* enable host irq from slave device*/
  1180. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
  1181. SWRS_SCP_INT_STATUS_CLEAR_1);
  1182. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1183. SWRS_SCP_INT_STATUS_MASK_1);
  1184. break;
  1185. case SWR_ALERT:
  1186. dev_dbg(swrm->dev,
  1187. "device %d has pending interrupt\n",
  1188. devnum);
  1189. break;
  1190. }
  1191. break;
  1192. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  1193. dev_err_ratelimited(swrm->dev,
  1194. "SWR bus clsh detected\n");
  1195. break;
  1196. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  1197. dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
  1198. break;
  1199. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  1200. dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
  1201. break;
  1202. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  1203. dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
  1204. break;
  1205. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  1206. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  1207. dev_err_ratelimited(swrm->dev,
  1208. "SWR CMD error, fifo status 0x%x, flushing fifo\n",
  1209. value);
  1210. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  1211. break;
  1212. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  1213. dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
  1214. intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
  1215. swr_master_write(swrm,
  1216. SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, intr_mask);
  1217. break;
  1218. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  1219. dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
  1220. intr_mask &=
  1221. ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
  1222. swr_master_write(swrm,
  1223. SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, intr_mask);
  1224. break;
  1225. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  1226. complete(&swrm->broadcast);
  1227. dev_dbg(swrm->dev, "SWR cmd id finished\n");
  1228. break;
  1229. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
  1230. break;
  1231. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  1232. break;
  1233. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  1234. break;
  1235. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  1236. complete(&swrm->reset);
  1237. break;
  1238. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  1239. break;
  1240. default:
  1241. dev_err_ratelimited(swrm->dev,
  1242. "SWR unknown interrupt\n");
  1243. ret = IRQ_NONE;
  1244. break;
  1245. }
  1246. }
  1247. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
  1248. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
  1249. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1250. intr_sts &= intr_mask;
  1251. if (intr_sts) {
  1252. dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
  1253. goto handle_irq;
  1254. }
  1255. mutex_lock(&swrm->reslock);
  1256. swrm_clk_request(swrm, false);
  1257. mutex_unlock(&swrm->reslock);
  1258. return ret;
  1259. }
  1260. static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
  1261. {
  1262. struct swr_mstr_ctrl *swrm = dev;
  1263. int ret = IRQ_HANDLED;
  1264. if (!swrm || !(swrm->dev)) {
  1265. pr_err("%s: swrm or dev is null\n", __func__);
  1266. return IRQ_NONE;
  1267. }
  1268. mutex_lock(&swrm->devlock);
  1269. if (!swrm->dev_up) {
  1270. if (swrm->wake_irq > 0)
  1271. disable_irq_nosync(swrm->wake_irq);
  1272. mutex_unlock(&swrm->devlock);
  1273. return ret;
  1274. }
  1275. mutex_unlock(&swrm->devlock);
  1276. if (swrm->wake_irq > 0)
  1277. disable_irq_nosync(swrm->wake_irq);
  1278. pm_runtime_get_sync(swrm->dev);
  1279. pm_runtime_mark_last_busy(swrm->dev);
  1280. pm_runtime_put_autosuspend(swrm->dev);
  1281. return ret;
  1282. }
  1283. static void swrm_wakeup_work(struct work_struct *work)
  1284. {
  1285. struct swr_mstr_ctrl *swrm;
  1286. swrm = container_of(work, struct swr_mstr_ctrl,
  1287. wakeup_work);
  1288. if (!swrm || !(swrm->dev)) {
  1289. pr_err("%s: swrm or dev is null\n", __func__);
  1290. return;
  1291. }
  1292. mutex_lock(&swrm->devlock);
  1293. if (!swrm->dev_up) {
  1294. mutex_unlock(&swrm->devlock);
  1295. return;
  1296. }
  1297. mutex_unlock(&swrm->devlock);
  1298. pm_runtime_get_sync(swrm->dev);
  1299. pm_runtime_mark_last_busy(swrm->dev);
  1300. pm_runtime_put_autosuspend(swrm->dev);
  1301. }
  1302. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  1303. {
  1304. u32 val;
  1305. swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1306. val = (swrm->slave_status >> (devnum * 2));
  1307. val &= SWRM_MCP_SLV_STATUS_MASK;
  1308. return val;
  1309. }
  1310. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  1311. u8 *dev_num)
  1312. {
  1313. int i;
  1314. u64 id = 0;
  1315. int ret = -EINVAL;
  1316. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1317. struct swr_device *swr_dev;
  1318. u32 num_dev = 0;
  1319. if (!swrm) {
  1320. pr_err("%s: Invalid handle to swr controller\n",
  1321. __func__);
  1322. return ret;
  1323. }
  1324. if (swrm->num_dev)
  1325. num_dev = swrm->num_dev;
  1326. else
  1327. num_dev = mstr->num_dev;
  1328. mutex_lock(&swrm->devlock);
  1329. if (!swrm->dev_up) {
  1330. mutex_unlock(&swrm->devlock);
  1331. return ret;
  1332. }
  1333. mutex_unlock(&swrm->devlock);
  1334. pm_runtime_get_sync(swrm->dev);
  1335. for (i = 1; i < (num_dev + 1); i++) {
  1336. id = ((u64)(swr_master_read(swrm,
  1337. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  1338. id |= swr_master_read(swrm,
  1339. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  1340. /*
  1341. * As pm_runtime_get_sync() brings all slaves out of reset
  1342. * update logical device number for all slaves.
  1343. */
  1344. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1345. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  1346. u32 status = swrm_get_device_status(swrm, i);
  1347. if ((status == 0x01) || (status == 0x02)) {
  1348. swr_dev->dev_num = i;
  1349. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  1350. *dev_num = i;
  1351. ret = 0;
  1352. }
  1353. dev_dbg(swrm->dev,
  1354. "%s: devnum %d is assigned for dev addr %lx\n",
  1355. __func__, i, swr_dev->addr);
  1356. }
  1357. }
  1358. }
  1359. }
  1360. if (ret)
  1361. dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
  1362. __func__, dev_id);
  1363. pm_runtime_mark_last_busy(swrm->dev);
  1364. pm_runtime_put_autosuspend(swrm->dev);
  1365. return ret;
  1366. }
  1367. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  1368. {
  1369. int ret = 0;
  1370. u32 val;
  1371. u8 row_ctrl = SWR_ROW_50;
  1372. u8 col_ctrl = SWR_MIN_COL;
  1373. u8 ssp_period = 1;
  1374. u8 retry_cmd_num = 3;
  1375. u32 reg[SWRM_MAX_INIT_REG];
  1376. u32 value[SWRM_MAX_INIT_REG];
  1377. int len = 0;
  1378. /* Clear Rows and Cols */
  1379. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1380. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1381. (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1382. reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
  1383. value[len++] = val;
  1384. /* Set Auto enumeration flag */
  1385. reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
  1386. value[len++] = 1;
  1387. /* Configure No pings */
  1388. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1389. val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
  1390. val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
  1391. reg[len] = SWRM_MCP_CFG_ADDR;
  1392. value[len++] = val;
  1393. /* Configure number of retries of a read/write cmd */
  1394. val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
  1395. reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
  1396. value[len++] = val;
  1397. reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
  1398. value[len++] = 0x2;
  1399. /* Set IRQ to PULSE */
  1400. reg[len] = SWRM_COMP_CFG_ADDR;
  1401. value[len++] = 0x02;
  1402. reg[len] = SWRM_COMP_CFG_ADDR;
  1403. value[len++] = 0x03;
  1404. reg[len] = SWRM_INTERRUPT_CLEAR;
  1405. value[len++] = 0xFFFFFFFF;
  1406. /* Mask soundwire interrupts */
  1407. reg[len] = SWRM_INTERRUPT_MASK_ADDR;
  1408. value[len++] = 0x1FFFD;
  1409. reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
  1410. value[len++] = SWRM_INTERRUPT_STATUS_MASK;
  1411. swr_master_bulk_write(swrm, reg, value, len);
  1412. return ret;
  1413. }
  1414. static int swrm_event_notify(struct notifier_block *self,
  1415. unsigned long action, void *data)
  1416. {
  1417. struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
  1418. event_notifier);
  1419. if (!swrm || !(swrm->dev)) {
  1420. pr_err("%s: swrm or dev is NULL\n", __func__);
  1421. return -EINVAL;
  1422. }
  1423. switch (action) {
  1424. case MSM_AUD_DC_EVENT:
  1425. schedule_work(&(swrm->dc_presence_work));
  1426. break;
  1427. case SWR_WAKE_IRQ_EVENT:
  1428. if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
  1429. swrm->ipc_wakeup_triggered = true;
  1430. schedule_work(&swrm->wakeup_work);
  1431. }
  1432. break;
  1433. default:
  1434. dev_err(swrm->dev, "%s: invalid event type: %lu\n",
  1435. __func__, action);
  1436. return -EINVAL;
  1437. }
  1438. return 0;
  1439. }
  1440. static void swrm_notify_work_fn(struct work_struct *work)
  1441. {
  1442. struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
  1443. dc_presence_work);
  1444. if (!swrm || !swrm->pdev) {
  1445. pr_err("%s: swrm or pdev is NULL\n", __func__);
  1446. return;
  1447. }
  1448. swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
  1449. }
  1450. static int swrm_probe(struct platform_device *pdev)
  1451. {
  1452. struct swr_mstr_ctrl *swrm;
  1453. struct swr_ctrl_platform_data *pdata;
  1454. u32 i, num_ports, port_num, port_type, ch_mask;
  1455. u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
  1456. int ret = 0;
  1457. /* Allocate soundwire master driver structure */
  1458. swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
  1459. GFP_KERNEL);
  1460. if (!swrm) {
  1461. ret = -ENOMEM;
  1462. goto err_memory_fail;
  1463. }
  1464. swrm->pdev = pdev;
  1465. swrm->dev = &pdev->dev;
  1466. platform_set_drvdata(pdev, swrm);
  1467. swr_set_ctrl_data(&swrm->master, swrm);
  1468. pdata = dev_get_platdata(&pdev->dev);
  1469. if (!pdata) {
  1470. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  1471. __func__);
  1472. ret = -EINVAL;
  1473. goto err_pdata_fail;
  1474. }
  1475. swrm->handle = (void *)pdata->handle;
  1476. if (!swrm->handle) {
  1477. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  1478. __func__);
  1479. ret = -EINVAL;
  1480. goto err_pdata_fail;
  1481. }
  1482. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
  1483. &swrm->master_id);
  1484. if (ret) {
  1485. dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
  1486. goto err_pdata_fail;
  1487. }
  1488. if (!(of_property_read_u32(pdev->dev.of_node,
  1489. "swrm-io-base", &swrm->swrm_base_reg)))
  1490. ret = of_property_read_u32(pdev->dev.of_node,
  1491. "swrm-io-base", &swrm->swrm_base_reg);
  1492. if (!swrm->swrm_base_reg) {
  1493. swrm->read = pdata->read;
  1494. if (!swrm->read) {
  1495. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  1496. __func__);
  1497. ret = -EINVAL;
  1498. goto err_pdata_fail;
  1499. }
  1500. swrm->write = pdata->write;
  1501. if (!swrm->write) {
  1502. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  1503. __func__);
  1504. ret = -EINVAL;
  1505. goto err_pdata_fail;
  1506. }
  1507. swrm->bulk_write = pdata->bulk_write;
  1508. if (!swrm->bulk_write) {
  1509. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  1510. __func__);
  1511. ret = -EINVAL;
  1512. goto err_pdata_fail;
  1513. }
  1514. } else {
  1515. swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
  1516. swrm->swrm_base_reg, SWRM_MAX_REGISTER);
  1517. }
  1518. swrm->clk = pdata->clk;
  1519. if (!swrm->clk) {
  1520. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  1521. __func__);
  1522. ret = -EINVAL;
  1523. goto err_pdata_fail;
  1524. }
  1525. if (of_property_read_u32(pdev->dev.of_node,
  1526. "qcom,swr-clock-stop-mode0",
  1527. &swrm->clk_stop_mode0_supp)) {
  1528. swrm->clk_stop_mode0_supp = FALSE;
  1529. }
  1530. /* Parse soundwire port mapping */
  1531. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
  1532. &num_ports);
  1533. if (ret) {
  1534. dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
  1535. goto err_pdata_fail;
  1536. }
  1537. swrm->num_ports = num_ports;
  1538. if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
  1539. &map_size)) {
  1540. dev_err(swrm->dev, "missing port mapping\n");
  1541. goto err_pdata_fail;
  1542. }
  1543. map_length = map_size / (3 * sizeof(u32));
  1544. if (num_ports > SWR_MSTR_PORT_LEN) {
  1545. dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
  1546. __func__);
  1547. ret = -EINVAL;
  1548. goto err_pdata_fail;
  1549. }
  1550. temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
  1551. if (!temp) {
  1552. ret = -ENOMEM;
  1553. goto err_pdata_fail;
  1554. }
  1555. ret = of_property_read_u32_array(pdev->dev.of_node,
  1556. "qcom,swr-port-mapping", temp, 3 * map_length);
  1557. if (ret) {
  1558. dev_err(swrm->dev, "%s: Failed to read port mapping\n",
  1559. __func__);
  1560. goto err_pdata_fail;
  1561. }
  1562. for (i = 0; i < map_length; i++) {
  1563. port_num = temp[3 * i];
  1564. port_type = temp[3 * i + 1];
  1565. ch_mask = temp[3 * i + 2];
  1566. if (port_num != old_port_num)
  1567. ch_iter = 0;
  1568. swrm->port_mapping[port_num][ch_iter].port_type = port_type;
  1569. swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
  1570. old_port_num = port_num;
  1571. }
  1572. devm_kfree(&pdev->dev, temp);
  1573. swrm->reg_irq = pdata->reg_irq;
  1574. swrm->master.read = swrm_read;
  1575. swrm->master.write = swrm_write;
  1576. swrm->master.bulk_write = swrm_bulk_write;
  1577. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  1578. swrm->master.connect_port = swrm_connect_port;
  1579. swrm->master.disconnect_port = swrm_disconnect_port;
  1580. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  1581. swrm->master.remove_from_group = swrm_remove_from_group;
  1582. swrm->master.dev.parent = &pdev->dev;
  1583. swrm->master.dev.of_node = pdev->dev.of_node;
  1584. swrm->master.num_port = 0;
  1585. swrm->rcmd_id = 0;
  1586. swrm->wcmd_id = 0;
  1587. swrm->slave_status = 0;
  1588. swrm->num_rx_chs = 0;
  1589. swrm->clk_ref_count = 0;
  1590. swrm->mclk_freq = MCLK_FREQ;
  1591. swrm->dev_up = true;
  1592. swrm->state = SWR_MSTR_UP;
  1593. swrm->ipc_wakeup = false;
  1594. swrm->ipc_wakeup_triggered = false;
  1595. init_completion(&swrm->reset);
  1596. init_completion(&swrm->broadcast);
  1597. init_completion(&swrm->clk_off_complete);
  1598. mutex_init(&swrm->mlock);
  1599. mutex_init(&swrm->reslock);
  1600. mutex_init(&swrm->force_down_lock);
  1601. mutex_init(&swrm->iolock);
  1602. mutex_init(&swrm->clklock);
  1603. mutex_init(&swrm->devlock);
  1604. for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
  1605. INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
  1606. ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
  1607. &swrm->num_dev);
  1608. if (ret) {
  1609. dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
  1610. __func__, "qcom,swr-num-dev");
  1611. } else {
  1612. if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
  1613. dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
  1614. __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
  1615. ret = -EINVAL;
  1616. goto err_pdata_fail;
  1617. }
  1618. }
  1619. if (swrm->reg_irq) {
  1620. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  1621. SWR_IRQ_REGISTER);
  1622. if (ret) {
  1623. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  1624. __func__, ret);
  1625. goto err_irq_fail;
  1626. }
  1627. } else {
  1628. swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
  1629. if (swrm->irq < 0) {
  1630. dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
  1631. __func__, swrm->irq);
  1632. goto err_irq_fail;
  1633. }
  1634. ret = request_threaded_irq(swrm->irq, NULL,
  1635. swr_mstr_interrupt,
  1636. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1637. "swr_master_irq", swrm);
  1638. if (ret) {
  1639. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  1640. __func__, ret);
  1641. goto err_irq_fail;
  1642. }
  1643. }
  1644. ret = swr_register_master(&swrm->master);
  1645. if (ret) {
  1646. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  1647. goto err_mstr_fail;
  1648. }
  1649. /* Add devices registered with board-info as the
  1650. * controller will be up now
  1651. */
  1652. swr_master_add_boarddevices(&swrm->master);
  1653. mutex_lock(&swrm->mlock);
  1654. swrm_clk_request(swrm, true);
  1655. ret = swrm_master_init(swrm);
  1656. if (ret < 0) {
  1657. dev_err(&pdev->dev,
  1658. "%s: Error in master Initialization , err %d\n",
  1659. __func__, ret);
  1660. mutex_unlock(&swrm->mlock);
  1661. goto err_mstr_fail;
  1662. }
  1663. swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
  1664. mutex_unlock(&swrm->mlock);
  1665. INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
  1666. if (pdev->dev.of_node)
  1667. of_register_swr_devices(&swrm->master);
  1668. dbgswrm = swrm;
  1669. debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  1670. if (!IS_ERR(debugfs_swrm_dent)) {
  1671. debugfs_peek = debugfs_create_file("swrm_peek",
  1672. S_IFREG | 0444, debugfs_swrm_dent,
  1673. (void *) "swrm_peek", &swrm_debug_ops);
  1674. debugfs_poke = debugfs_create_file("swrm_poke",
  1675. S_IFREG | 0444, debugfs_swrm_dent,
  1676. (void *) "swrm_poke", &swrm_debug_ops);
  1677. debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  1678. S_IFREG | 0444, debugfs_swrm_dent,
  1679. (void *) "swrm_reg_dump",
  1680. &swrm_debug_ops);
  1681. }
  1682. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1683. pm_runtime_use_autosuspend(&pdev->dev);
  1684. pm_runtime_set_active(&pdev->dev);
  1685. pm_runtime_enable(&pdev->dev);
  1686. pm_runtime_mark_last_busy(&pdev->dev);
  1687. INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
  1688. swrm->event_notifier.notifier_call = swrm_event_notify;
  1689. msm_aud_evt_register_client(&swrm->event_notifier);
  1690. return 0;
  1691. err_mstr_fail:
  1692. if (swrm->reg_irq)
  1693. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1694. swrm, SWR_IRQ_FREE);
  1695. else if (swrm->irq)
  1696. free_irq(swrm->irq, swrm);
  1697. err_irq_fail:
  1698. mutex_destroy(&swrm->mlock);
  1699. mutex_destroy(&swrm->reslock);
  1700. mutex_destroy(&swrm->force_down_lock);
  1701. mutex_destroy(&swrm->iolock);
  1702. mutex_destroy(&swrm->clklock);
  1703. err_pdata_fail:
  1704. err_memory_fail:
  1705. return ret;
  1706. }
  1707. static int swrm_remove(struct platform_device *pdev)
  1708. {
  1709. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1710. if (swrm->reg_irq)
  1711. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1712. swrm, SWR_IRQ_FREE);
  1713. else if (swrm->irq)
  1714. free_irq(swrm->irq, swrm);
  1715. else if (swrm->wake_irq > 0)
  1716. free_irq(swrm->wake_irq, swrm);
  1717. pm_runtime_disable(&pdev->dev);
  1718. pm_runtime_set_suspended(&pdev->dev);
  1719. swr_unregister_master(&swrm->master);
  1720. msm_aud_evt_unregister_client(&swrm->event_notifier);
  1721. mutex_destroy(&swrm->mlock);
  1722. mutex_destroy(&swrm->reslock);
  1723. mutex_destroy(&swrm->iolock);
  1724. mutex_destroy(&swrm->clklock);
  1725. mutex_destroy(&swrm->force_down_lock);
  1726. devm_kfree(&pdev->dev, swrm);
  1727. return 0;
  1728. }
  1729. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  1730. {
  1731. u32 val;
  1732. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  1733. swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
  1734. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1735. val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
  1736. swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
  1737. return 0;
  1738. }
  1739. #ifdef CONFIG_PM
  1740. static int swrm_runtime_resume(struct device *dev)
  1741. {
  1742. struct platform_device *pdev = to_platform_device(dev);
  1743. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1744. int ret = 0;
  1745. struct swr_master *mstr = &swrm->master;
  1746. struct swr_device *swr_dev;
  1747. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  1748. __func__, swrm->state);
  1749. mutex_lock(&swrm->reslock);
  1750. if ((swrm->state == SWR_MSTR_DOWN) ||
  1751. (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
  1752. if (swrm->clk_stop_mode0_supp) {
  1753. if (swrm->ipc_wakeup)
  1754. msm_aud_evt_blocking_notifier_call_chain(
  1755. SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  1756. }
  1757. if (swrm_clk_request(swrm, true))
  1758. goto exit;
  1759. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  1760. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1761. ret = swr_device_up(swr_dev);
  1762. if (ret) {
  1763. dev_err(dev,
  1764. "%s: failed to wakeup swr dev %d\n",
  1765. __func__, swr_dev->dev_num);
  1766. swrm_clk_request(swrm, false);
  1767. goto exit;
  1768. }
  1769. }
  1770. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  1771. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  1772. swrm_master_init(swrm);
  1773. swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
  1774. SWRS_SCP_INT_STATUS_MASK_1);
  1775. } else {
  1776. /*wake up from clock stop*/
  1777. swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
  1778. usleep_range(100, 105);
  1779. }
  1780. swrm->state = SWR_MSTR_UP;
  1781. }
  1782. exit:
  1783. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1784. mutex_unlock(&swrm->reslock);
  1785. return ret;
  1786. }
  1787. static int swrm_runtime_suspend(struct device *dev)
  1788. {
  1789. struct platform_device *pdev = to_platform_device(dev);
  1790. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1791. int ret = 0;
  1792. struct swr_master *mstr = &swrm->master;
  1793. struct swr_device *swr_dev;
  1794. int current_state = 0;
  1795. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  1796. __func__, swrm->state);
  1797. mutex_lock(&swrm->reslock);
  1798. mutex_lock(&swrm->force_down_lock);
  1799. current_state = swrm->state;
  1800. mutex_unlock(&swrm->force_down_lock);
  1801. if ((current_state == SWR_MSTR_UP) ||
  1802. (current_state == SWR_MSTR_SSR)) {
  1803. if ((current_state != SWR_MSTR_SSR) &&
  1804. swrm_is_port_en(&swrm->master)) {
  1805. dev_dbg(dev, "%s ports are enabled\n", __func__);
  1806. ret = -EBUSY;
  1807. goto exit;
  1808. }
  1809. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  1810. swrm_clk_pause(swrm);
  1811. swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
  1812. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1813. ret = swr_device_down(swr_dev);
  1814. if (ret) {
  1815. dev_err(dev,
  1816. "%s: failed to shutdown swr dev %d\n",
  1817. __func__, swr_dev->dev_num);
  1818. goto exit;
  1819. }
  1820. }
  1821. } else {
  1822. /* clock stop sequence */
  1823. swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
  1824. SWRS_SCP_CONTROL);
  1825. usleep_range(100, 105);
  1826. }
  1827. swrm_clk_request(swrm, false);
  1828. if (swrm->clk_stop_mode0_supp) {
  1829. if (swrm->wake_irq > 0) {
  1830. enable_irq(swrm->wake_irq);
  1831. } else if (swrm->ipc_wakeup) {
  1832. msm_aud_evt_blocking_notifier_call_chain(
  1833. SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  1834. swrm->ipc_wakeup_triggered = false;
  1835. }
  1836. }
  1837. }
  1838. /* Retain SSR state until resume */
  1839. if (current_state != SWR_MSTR_SSR)
  1840. swrm->state = SWR_MSTR_DOWN;
  1841. exit:
  1842. mutex_unlock(&swrm->reslock);
  1843. return ret;
  1844. }
  1845. #endif /* CONFIG_PM */
  1846. static int swrm_device_down(struct device *dev)
  1847. {
  1848. struct platform_device *pdev = to_platform_device(dev);
  1849. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1850. int ret = 0;
  1851. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  1852. mutex_lock(&swrm->force_down_lock);
  1853. swrm->state = SWR_MSTR_SSR;
  1854. mutex_unlock(&swrm->force_down_lock);
  1855. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  1856. ret = swrm_runtime_suspend(dev);
  1857. if (!ret) {
  1858. pm_runtime_disable(dev);
  1859. pm_runtime_set_suspended(dev);
  1860. pm_runtime_enable(dev);
  1861. }
  1862. }
  1863. return 0;
  1864. }
  1865. int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
  1866. {
  1867. int ret = 0;
  1868. if (!swrm->ipc_wakeup) {
  1869. swrm->wake_irq = platform_get_irq_byname(swrm->pdev,
  1870. "swr_wake_irq");
  1871. if (swrm->wake_irq < 0) {
  1872. dev_err(swrm->dev,
  1873. "%s() error getting wake irq handle: %d\n",
  1874. __func__, swrm->wake_irq);
  1875. return -EINVAL;
  1876. }
  1877. ret = request_threaded_irq(swrm->wake_irq, NULL,
  1878. swrm_wakeup_interrupt,
  1879. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1880. "swr_wake_irq", swrm);
  1881. if (ret) {
  1882. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  1883. __func__, ret);
  1884. return -EINVAL;
  1885. }
  1886. /* Disable wake irq - enable it after clock stop */
  1887. disable_irq(swrm->wake_irq);
  1888. }
  1889. return ret;
  1890. }
  1891. /**
  1892. * swrm_wcd_notify - parent device can notify to soundwire master through
  1893. * this function
  1894. * @pdev: pointer to platform device structure
  1895. * @id: command id from parent to the soundwire master
  1896. * @data: data from parent device to soundwire master
  1897. */
  1898. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  1899. {
  1900. struct swr_mstr_ctrl *swrm;
  1901. int ret = 0;
  1902. struct swr_master *mstr;
  1903. struct swr_device *swr_dev;
  1904. if (!pdev) {
  1905. pr_err("%s: pdev is NULL\n", __func__);
  1906. return -EINVAL;
  1907. }
  1908. swrm = platform_get_drvdata(pdev);
  1909. if (!swrm) {
  1910. dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
  1911. return -EINVAL;
  1912. }
  1913. mstr = &swrm->master;
  1914. switch (id) {
  1915. case SWR_CLK_FREQ:
  1916. if (!data) {
  1917. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  1918. ret = -EINVAL;
  1919. } else {
  1920. mutex_lock(&swrm->mlock);
  1921. swrm->mclk_freq = *(int *)data;
  1922. mutex_unlock(&swrm->mlock);
  1923. }
  1924. break;
  1925. case SWR_DEVICE_SSR_DOWN:
  1926. mutex_lock(&swrm->devlock);
  1927. swrm->dev_up = false;
  1928. mutex_unlock(&swrm->devlock);
  1929. mutex_lock(&swrm->reslock);
  1930. swrm->state = SWR_MSTR_SSR;
  1931. mutex_unlock(&swrm->reslock);
  1932. break;
  1933. case SWR_DEVICE_SSR_UP:
  1934. /* wait for clk voting to be zero */
  1935. reinit_completion(&swrm->clk_off_complete);
  1936. if (swrm->clk_ref_count &&
  1937. !wait_for_completion_timeout(&swrm->clk_off_complete,
  1938. msecs_to_jiffies(200)))
  1939. dev_err(swrm->dev, "%s: clock voting not zero\n",
  1940. __func__);
  1941. mutex_lock(&swrm->devlock);
  1942. swrm->dev_up = true;
  1943. mutex_unlock(&swrm->devlock);
  1944. break;
  1945. case SWR_DEVICE_DOWN:
  1946. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  1947. mutex_lock(&swrm->mlock);
  1948. if (swrm->state == SWR_MSTR_DOWN)
  1949. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  1950. __func__, swrm->state);
  1951. else
  1952. swrm_device_down(&pdev->dev);
  1953. mutex_unlock(&swrm->mlock);
  1954. break;
  1955. case SWR_DEVICE_UP:
  1956. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  1957. mutex_lock(&swrm->devlock);
  1958. if (!swrm->dev_up) {
  1959. dev_dbg(swrm->dev, "SSR not complete yet\n");
  1960. mutex_unlock(&swrm->devlock);
  1961. return -EBUSY;
  1962. }
  1963. mutex_unlock(&swrm->devlock);
  1964. mutex_lock(&swrm->mlock);
  1965. pm_runtime_mark_last_busy(&pdev->dev);
  1966. pm_runtime_get_sync(&pdev->dev);
  1967. mutex_lock(&swrm->reslock);
  1968. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1969. ret = swr_reset_device(swr_dev);
  1970. if (ret) {
  1971. dev_err(swrm->dev,
  1972. "%s: failed to reset swr device %d\n",
  1973. __func__, swr_dev->dev_num);
  1974. swrm_clk_request(swrm, false);
  1975. }
  1976. }
  1977. pm_runtime_mark_last_busy(&pdev->dev);
  1978. pm_runtime_put_autosuspend(&pdev->dev);
  1979. mutex_unlock(&swrm->reslock);
  1980. mutex_unlock(&swrm->mlock);
  1981. break;
  1982. case SWR_SET_NUM_RX_CH:
  1983. if (!data) {
  1984. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  1985. ret = -EINVAL;
  1986. } else {
  1987. mutex_lock(&swrm->mlock);
  1988. swrm->num_rx_chs = *(int *)data;
  1989. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  1990. list_for_each_entry(swr_dev, &mstr->devices,
  1991. dev_list) {
  1992. ret = swr_set_device_group(swr_dev,
  1993. SWR_BROADCAST);
  1994. if (ret)
  1995. dev_err(swrm->dev,
  1996. "%s: set num ch failed\n",
  1997. __func__);
  1998. }
  1999. } else {
  2000. list_for_each_entry(swr_dev, &mstr->devices,
  2001. dev_list) {
  2002. ret = swr_set_device_group(swr_dev,
  2003. SWR_GROUP_NONE);
  2004. if (ret)
  2005. dev_err(swrm->dev,
  2006. "%s: set num ch failed\n",
  2007. __func__);
  2008. }
  2009. }
  2010. mutex_unlock(&swrm->mlock);
  2011. }
  2012. break;
  2013. case SWR_REGISTER_WAKE_IRQ:
  2014. if (!data) {
  2015. dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
  2016. __func__);
  2017. ret = -EINVAL;
  2018. } else {
  2019. mutex_lock(&swrm->mlock);
  2020. swrm->ipc_wakeup = *(u32 *)data;
  2021. ret = swrm_register_wake_irq(swrm);
  2022. if (ret)
  2023. dev_err(swrm->dev, "%s: register wake_irq failed\n",
  2024. __func__);
  2025. mutex_unlock(&swrm->mlock);
  2026. }
  2027. break;
  2028. default:
  2029. dev_err(swrm->dev, "%s: swr master unknown id %d\n",
  2030. __func__, id);
  2031. break;
  2032. }
  2033. return ret;
  2034. }
  2035. EXPORT_SYMBOL(swrm_wcd_notify);
  2036. #ifdef CONFIG_PM_SLEEP
  2037. static int swrm_suspend(struct device *dev)
  2038. {
  2039. int ret = -EBUSY;
  2040. struct platform_device *pdev = to_platform_device(dev);
  2041. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2042. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  2043. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  2044. ret = swrm_runtime_suspend(dev);
  2045. if (!ret) {
  2046. /*
  2047. * Synchronize runtime-pm and system-pm states:
  2048. * At this point, we are already suspended. If
  2049. * runtime-pm still thinks its active, then
  2050. * make sure its status is in sync with HW
  2051. * status. The three below calls let the
  2052. * runtime-pm know that we are suspended
  2053. * already without re-invoking the suspend
  2054. * callback
  2055. */
  2056. pm_runtime_disable(dev);
  2057. pm_runtime_set_suspended(dev);
  2058. pm_runtime_enable(dev);
  2059. }
  2060. }
  2061. if (ret == -EBUSY) {
  2062. /*
  2063. * There is a possibility that some audio stream is active
  2064. * during suspend. We dont want to return suspend failure in
  2065. * that case so that display and relevant components can still
  2066. * go to suspend.
  2067. * If there is some other error, then it should be passed-on
  2068. * to system level suspend
  2069. */
  2070. ret = 0;
  2071. }
  2072. return ret;
  2073. }
  2074. static int swrm_resume(struct device *dev)
  2075. {
  2076. int ret = 0;
  2077. struct platform_device *pdev = to_platform_device(dev);
  2078. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2079. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  2080. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  2081. ret = swrm_runtime_resume(dev);
  2082. if (!ret) {
  2083. pm_runtime_mark_last_busy(dev);
  2084. pm_request_autosuspend(dev);
  2085. }
  2086. }
  2087. return ret;
  2088. }
  2089. #endif /* CONFIG_PM_SLEEP */
  2090. static const struct dev_pm_ops swrm_dev_pm_ops = {
  2091. SET_SYSTEM_SLEEP_PM_OPS(
  2092. swrm_suspend,
  2093. swrm_resume
  2094. )
  2095. SET_RUNTIME_PM_OPS(
  2096. swrm_runtime_suspend,
  2097. swrm_runtime_resume,
  2098. NULL
  2099. )
  2100. };
  2101. static const struct of_device_id swrm_dt_match[] = {
  2102. {
  2103. .compatible = "qcom,swr-mstr",
  2104. },
  2105. {}
  2106. };
  2107. static struct platform_driver swr_mstr_driver = {
  2108. .probe = swrm_probe,
  2109. .remove = swrm_remove,
  2110. .driver = {
  2111. .name = SWR_WCD_NAME,
  2112. .owner = THIS_MODULE,
  2113. .pm = &swrm_dev_pm_ops,
  2114. .of_match_table = swrm_dt_match,
  2115. },
  2116. };
  2117. static int __init swrm_init(void)
  2118. {
  2119. return platform_driver_register(&swr_mstr_driver);
  2120. }
  2121. module_init(swrm_init);
  2122. static void __exit swrm_exit(void)
  2123. {
  2124. platform_driver_unregister(&swr_mstr_driver);
  2125. }
  2126. module_exit(swrm_exit);
  2127. MODULE_LICENSE("GPL v2");
  2128. MODULE_DESCRIPTION("SoundWire Master Controller");
  2129. MODULE_ALIAS("platform:swr-mstr");