swr-mstr-ctrl.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  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/clk.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/of.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/uaccess.h>
  26. #include <soc/soundwire.h>
  27. #include <soc/swr-wcd.h>
  28. #include <linux/regmap.h>
  29. #include "swrm_registers.h"
  30. #include "swr-mstr-ctrl.h"
  31. #include "swrm_port_config.h"
  32. #define SWR_BROADCAST_CMD_ID 0x0F
  33. #define SWR_AUTO_SUSPEND_DELAY 3 /* delay in sec */
  34. #define SWR_DEV_ID_MASK 0xFFFFFFFF
  35. #define SWR_REG_VAL_PACK(data, dev, id, reg) \
  36. ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
  37. /* pm runtime auto suspend timer in msecs */
  38. static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
  39. module_param(auto_suspend_timer, int, 0664);
  40. MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
  41. enum {
  42. SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
  43. SWR_ATTACHED_OK, /* Device is attached */
  44. SWR_ALERT, /* Device alters master for any interrupts */
  45. SWR_RESERVED, /* Reserved */
  46. };
  47. enum {
  48. MASTER_ID_WSA = 1,
  49. MASTER_ID_RX,
  50. MASTER_ID_TX
  51. };
  52. #define MASTER_ID_MASK 0xF
  53. #define TRUE 1
  54. #define FALSE 0
  55. #define SWRM_MAX_PORT_REG 40
  56. #define SWRM_MAX_INIT_REG 8
  57. #define SWR_MSTR_MAX_REG_ADDR 0x1740
  58. #define SWR_MSTR_START_REG_ADDR 0x00
  59. #define SWR_MSTR_MAX_BUF_LEN 32
  60. #define BYTES_PER_LINE 12
  61. #define SWR_MSTR_RD_BUF_LEN 8
  62. #define SWR_MSTR_WR_BUF_LEN 32
  63. #define MAX_FIFO_RD_FAIL_RETRY 3
  64. static struct swr_mstr_ctrl *dbgswrm;
  65. static struct dentry *debugfs_swrm_dent;
  66. static struct dentry *debugfs_peek;
  67. static struct dentry *debugfs_poke;
  68. static struct dentry *debugfs_reg_dump;
  69. static unsigned int read_data;
  70. static bool swrm_is_msm_variant(int val)
  71. {
  72. return (val == SWRM_VERSION_1_3);
  73. }
  74. static int swrm_debug_open(struct inode *inode, struct file *file)
  75. {
  76. file->private_data = inode->i_private;
  77. return 0;
  78. }
  79. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  80. {
  81. char *token;
  82. int base, cnt;
  83. token = strsep(&buf, " ");
  84. for (cnt = 0; cnt < num_of_par; cnt++) {
  85. if (token) {
  86. if ((token[1] == 'x') || (token[1] == 'X'))
  87. base = 16;
  88. else
  89. base = 10;
  90. if (kstrtou32(token, base, &param1[cnt]) != 0)
  91. return -EINVAL;
  92. token = strsep(&buf, " ");
  93. } else
  94. return -EINVAL;
  95. }
  96. return 0;
  97. }
  98. static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
  99. loff_t *ppos)
  100. {
  101. int i, reg_val, len;
  102. ssize_t total = 0;
  103. char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
  104. if (!ubuf || !ppos)
  105. return 0;
  106. for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
  107. i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
  108. reg_val = dbgswrm->read(dbgswrm->handle, i);
  109. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
  110. if ((total + len) >= count - 1)
  111. break;
  112. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  113. pr_err("%s: fail to copy reg dump\n", __func__);
  114. total = -EFAULT;
  115. goto copy_err;
  116. }
  117. *ppos += len;
  118. total += len;
  119. }
  120. copy_err:
  121. return total;
  122. }
  123. static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
  124. size_t count, loff_t *ppos)
  125. {
  126. char lbuf[SWR_MSTR_RD_BUF_LEN];
  127. char *access_str;
  128. ssize_t ret_cnt;
  129. if (!count || !file || !ppos || !ubuf)
  130. return -EINVAL;
  131. access_str = file->private_data;
  132. if (*ppos < 0)
  133. return -EINVAL;
  134. if (!strcmp(access_str, "swrm_peek")) {
  135. snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
  136. ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
  137. strnlen(lbuf, 7));
  138. } else if (!strcmp(access_str, "swrm_reg_dump")) {
  139. ret_cnt = swrm_reg_show(ubuf, count, ppos);
  140. } else {
  141. pr_err("%s: %s not permitted to read\n", __func__, access_str);
  142. ret_cnt = -EPERM;
  143. }
  144. return ret_cnt;
  145. }
  146. static ssize_t swrm_debug_write(struct file *filp,
  147. const char __user *ubuf, size_t cnt, loff_t *ppos)
  148. {
  149. char lbuf[SWR_MSTR_WR_BUF_LEN];
  150. int rc;
  151. u32 param[5];
  152. char *access_str;
  153. if (!filp || !ppos || !ubuf)
  154. return -EINVAL;
  155. access_str = filp->private_data;
  156. if (cnt > sizeof(lbuf) - 1)
  157. return -EINVAL;
  158. rc = copy_from_user(lbuf, ubuf, cnt);
  159. if (rc)
  160. return -EFAULT;
  161. lbuf[cnt] = '\0';
  162. if (!strcmp(access_str, "swrm_poke")) {
  163. /* write */
  164. rc = get_parameters(lbuf, param, 2);
  165. if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
  166. (param[1] <= 0xFFFFFFFF) &&
  167. (rc == 0))
  168. rc = dbgswrm->write(dbgswrm->handle, param[0],
  169. param[1]);
  170. else
  171. rc = -EINVAL;
  172. } else if (!strcmp(access_str, "swrm_peek")) {
  173. /* read */
  174. rc = get_parameters(lbuf, param, 1);
  175. if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
  176. read_data = dbgswrm->read(dbgswrm->handle, param[0]);
  177. else
  178. rc = -EINVAL;
  179. }
  180. if (rc == 0)
  181. rc = cnt;
  182. else
  183. pr_err("%s: rc = %d\n", __func__, rc);
  184. return rc;
  185. }
  186. static const struct file_operations swrm_debug_ops = {
  187. .open = swrm_debug_open,
  188. .write = swrm_debug_write,
  189. .read = swrm_debug_read,
  190. };
  191. static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
  192. {
  193. if (!swrm->clk || !swrm->handle)
  194. return -EINVAL;
  195. if (enable) {
  196. swrm->clk_ref_count++;
  197. if (swrm->clk_ref_count == 1) {
  198. swrm->clk(swrm->handle, true);
  199. swrm->state = SWR_MSTR_UP;
  200. }
  201. } else if (--swrm->clk_ref_count == 0) {
  202. swrm->clk(swrm->handle, false);
  203. swrm->state = SWR_MSTR_DOWN;
  204. } else if (swrm->clk_ref_count < 0) {
  205. pr_err("%s: swrm clk count mismatch\n", __func__);
  206. swrm->clk_ref_count = 0;
  207. }
  208. return 0;
  209. }
  210. static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
  211. u16 reg, u32 *value)
  212. {
  213. u32 temp = (u32)(*value);
  214. int ret;
  215. ret = swrm_clk_request(swrm, TRUE);
  216. if (ret)
  217. return -EINVAL;
  218. iowrite32(temp, swrm->swrm_dig_base + reg);
  219. swrm_clk_request(swrm, FALSE);
  220. return 0;
  221. }
  222. static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
  223. u16 reg, u32 *value)
  224. {
  225. u32 temp = 0;
  226. int ret;
  227. ret = swrm_clk_request(swrm, TRUE);
  228. if (ret)
  229. return -EINVAL;
  230. temp = ioread32(swrm->swrm_dig_base + reg);
  231. *value = temp;
  232. swrm_clk_request(swrm, FALSE);
  233. return 0;
  234. }
  235. static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
  236. {
  237. u32 val = 0;
  238. if (swrm->read)
  239. val = swrm->read(swrm->handle, reg_addr);
  240. else
  241. swrm_ahb_read(swrm, reg_addr, &val);
  242. return val;
  243. }
  244. static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
  245. {
  246. if (swrm->write)
  247. swrm->write(swrm->handle, reg_addr, val);
  248. else
  249. swrm_ahb_write(swrm, reg_addr, &val);
  250. }
  251. static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
  252. u32 *val, unsigned int length)
  253. {
  254. int i = 0;
  255. if (swrm->bulk_write)
  256. swrm->bulk_write(swrm->handle, reg_addr, val, length);
  257. else {
  258. for (i = 0; i < length; i++) {
  259. /* wait for FIFO WR command to complete to avoid overflow */
  260. usleep_range(100, 105);
  261. swr_master_write(swrm, reg_addr[i], val[i]);
  262. }
  263. }
  264. return 0;
  265. }
  266. static bool swrm_is_port_en(struct swr_master *mstr)
  267. {
  268. return !!(mstr->num_port);
  269. }
  270. static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
  271. {
  272. u8 master_device_id;
  273. int i;
  274. /* update device_id for tx/rx */
  275. master_device_id = MASTER_ID_WSA;
  276. switch (master_device_id & MASTER_ID_MASK) {
  277. case MASTER_ID_WSA:
  278. /* get port params for wsa */
  279. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  280. /* wsa uses single frame structure for all configurations */
  281. if (!swrm->mport_cfg[i].port_en)
  282. continue;
  283. swrm->mport_cfg[i].sinterval = wsa_frame_superset[i].si;
  284. swrm->mport_cfg[i].offset1 = wsa_frame_superset[i].off1;
  285. swrm->mport_cfg[i].offset2 = wsa_frame_superset[i].off2;
  286. }
  287. break;
  288. case MASTER_ID_RX:
  289. /* get port params for rx */
  290. break;
  291. case MASTER_ID_TX:
  292. /* get port params for tx */
  293. break;
  294. default: /* MASTER_GENERIC*/
  295. /* computer generic frame parameters */
  296. return -EINVAL;
  297. }
  298. return 0;
  299. }
  300. static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
  301. u8 *mstr_ch_mask, u8 mstr_prt_type,
  302. u8 slv_port_id)
  303. {
  304. int i, j;
  305. *mstr_port_id = 0;
  306. for (i = 1; i <= swrm->num_ports; i++) {
  307. for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
  308. if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
  309. goto found;
  310. }
  311. }
  312. found:
  313. if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
  314. dev_err(swrm->dev, "%s: port type not supported by master\n",
  315. __func__);
  316. return -EINVAL;
  317. }
  318. /* id 0 corresponds to master port 1 */
  319. *mstr_port_id = i - 1;
  320. *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
  321. return 0;
  322. }
  323. static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
  324. u8 dev_addr, u16 reg_addr)
  325. {
  326. u32 val;
  327. u8 id = *cmd_id;
  328. if (id != SWR_BROADCAST_CMD_ID) {
  329. if (id < 14)
  330. id += 1;
  331. else
  332. id = 0;
  333. *cmd_id = id;
  334. }
  335. val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
  336. return val;
  337. }
  338. static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
  339. u8 dev_addr, u8 cmd_id, u16 reg_addr,
  340. u32 len)
  341. {
  342. u32 val;
  343. u32 retry_attempt = 0;
  344. val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
  345. /* wait for FIFO RD to complete to avoid overflow */
  346. usleep_range(100, 105);
  347. swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
  348. /* wait for FIFO RD CMD complete to avoid overflow */
  349. usleep_range(250, 255);
  350. retry_read:
  351. *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
  352. dev_dbg(swrm->dev,
  353. "%s: reg: 0x%x, cmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
  354. __func__, reg_addr, cmd_id, dev_addr, *cmd_data);
  355. if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
  356. if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
  357. /* wait 500 us before retry on fifo read failure */
  358. usleep_range(500, 505);
  359. retry_attempt++;
  360. goto retry_read;
  361. } else {
  362. dev_err_ratelimited(swrm->dev,
  363. "%s: failed to read fifo\n", __func__);
  364. }
  365. }
  366. return 0;
  367. }
  368. static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
  369. u8 dev_addr, u8 cmd_id, u16 reg_addr)
  370. {
  371. u32 val;
  372. int ret = 0;
  373. if (!cmd_id)
  374. val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
  375. dev_addr, reg_addr);
  376. else
  377. val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
  378. dev_addr, reg_addr);
  379. dev_dbg(swrm->dev,
  380. "%s: reg: 0x%x, cmd_id: 0x%x, val:0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
  381. __func__, reg_addr, cmd_id, val, dev_addr, cmd_data);
  382. /* wait for FIFO WR command to complete to avoid overflow */
  383. usleep_range(250, 255);
  384. swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
  385. if (cmd_id == 0xF) {
  386. /*
  387. * sleep for 10ms for MSM soundwire variant to allow broadcast
  388. * command to complete.
  389. */
  390. if (swrm_is_msm_variant(swrm->version))
  391. usleep_range(10000, 10100);
  392. else
  393. wait_for_completion_timeout(&swrm->broadcast,
  394. (2 * HZ/10));
  395. }
  396. return ret;
  397. }
  398. static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
  399. void *buf, u32 len)
  400. {
  401. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  402. int ret = 0;
  403. int val;
  404. u8 *reg_val = (u8 *)buf;
  405. if (!swrm) {
  406. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  407. return -EINVAL;
  408. }
  409. if (dev_num)
  410. ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr,
  411. len);
  412. else
  413. val = swr_master_read(swrm, reg_addr);
  414. if (!ret)
  415. *reg_val = (u8)val;
  416. pm_runtime_mark_last_busy(swrm->dev);
  417. return ret;
  418. }
  419. static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
  420. const void *buf)
  421. {
  422. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  423. int ret = 0;
  424. u8 reg_val = *(u8 *)buf;
  425. if (!swrm) {
  426. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  427. return -EINVAL;
  428. }
  429. if (dev_num)
  430. ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
  431. else
  432. swr_master_write(swrm, reg_addr, reg_val);
  433. pm_runtime_mark_last_busy(swrm->dev);
  434. return ret;
  435. }
  436. static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
  437. const void *buf, size_t len)
  438. {
  439. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  440. int ret = 0;
  441. int i;
  442. u32 *val;
  443. u32 *swr_fifo_reg;
  444. if (!swrm || !swrm->handle) {
  445. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  446. return -EINVAL;
  447. }
  448. if (len <= 0)
  449. return -EINVAL;
  450. if (dev_num) {
  451. swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
  452. if (!swr_fifo_reg) {
  453. ret = -ENOMEM;
  454. goto err;
  455. }
  456. val = kcalloc(len, sizeof(u32), GFP_KERNEL);
  457. if (!val) {
  458. ret = -ENOMEM;
  459. goto mem_fail;
  460. }
  461. for (i = 0; i < len; i++) {
  462. val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
  463. ((u8 *)buf)[i],
  464. dev_num,
  465. ((u16 *)reg)[i]);
  466. swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
  467. }
  468. ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
  469. if (ret) {
  470. dev_err(&master->dev, "%s: bulk write failed\n",
  471. __func__);
  472. ret = -EINVAL;
  473. }
  474. } else {
  475. dev_err(&master->dev,
  476. "%s: No support of Bulk write for master regs\n",
  477. __func__);
  478. ret = -EINVAL;
  479. goto err;
  480. }
  481. kfree(val);
  482. mem_fail:
  483. kfree(swr_fifo_reg);
  484. err:
  485. pm_runtime_mark_last_busy(swrm->dev);
  486. return ret;
  487. }
  488. static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
  489. {
  490. return (swr_master_read(swrm, SWRM_MCP_STATUS) &
  491. SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
  492. }
  493. static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
  494. u8 row, u8 col)
  495. {
  496. swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
  497. SWRS_SCP_FRAME_CTRL_BANK(bank));
  498. }
  499. static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
  500. u8 slv_port, u8 dev_num)
  501. {
  502. struct swr_port_info *port_req = NULL;
  503. list_for_each_entry(port_req, &mport->port_req_list, list) {
  504. /* Store dev_id instead of dev_num if enumeration is changed run_time */
  505. if ((port_req->slave_port_id == slv_port)
  506. && (port_req->dev_num == dev_num))
  507. return port_req;
  508. }
  509. return NULL;
  510. }
  511. static bool swrm_remove_from_group(struct swr_master *master)
  512. {
  513. struct swr_device *swr_dev;
  514. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  515. bool is_removed = false;
  516. if (!swrm)
  517. goto end;
  518. mutex_lock(&swrm->mlock);
  519. if ((swrm->num_rx_chs > 1) &&
  520. (swrm->num_rx_chs == swrm->num_cfg_devs)) {
  521. list_for_each_entry(swr_dev, &master->devices,
  522. dev_list) {
  523. swr_dev->group_id = SWR_GROUP_NONE;
  524. master->gr_sid = 0;
  525. }
  526. is_removed = true;
  527. }
  528. mutex_unlock(&swrm->mlock);
  529. end:
  530. return is_removed;
  531. }
  532. static void swrm_disable_ports(struct swr_master *master,
  533. u8 bank)
  534. {
  535. u32 value;
  536. struct swr_port_info *port_req;
  537. int i;
  538. struct swrm_mports *mport;
  539. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  540. if (!swrm) {
  541. pr_err("%s: swrm is null\n", __func__);
  542. return;
  543. }
  544. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  545. master->num_port);
  546. for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
  547. mport = &(swrm->mport_cfg[i]);
  548. if (!mport->port_en)
  549. continue;
  550. list_for_each_entry(port_req, &mport->port_req_list, list) {
  551. /* skip ports with no change req's*/
  552. if (port_req->req_ch == port_req->ch_en)
  553. continue;
  554. swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
  555. port_req->dev_num, 0x00,
  556. SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
  557. bank));
  558. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
  559. __func__, i,
  560. (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
  561. }
  562. value = ((mport->req_ch)
  563. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  564. value |= ((mport->offset2)
  565. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  566. value |= ((mport->offset1)
  567. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  568. value |= mport->sinterval;
  569. swr_master_write(swrm,
  570. SWRM_DP_PORT_CTRL_BANK(i+1, bank),
  571. value);
  572. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  573. __func__, i,
  574. (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
  575. }
  576. }
  577. static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
  578. {
  579. struct swr_port_info *port_req, *next;
  580. int i;
  581. struct swrm_mports *mport;
  582. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  583. if (!swrm) {
  584. pr_err("%s: swrm is null\n", __func__);
  585. return;
  586. }
  587. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  588. master->num_port);
  589. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  590. mport = &(swrm->mport_cfg[i]);
  591. list_for_each_entry_safe(port_req, next,
  592. &mport->port_req_list, list) {
  593. /* skip ports without new ch req */
  594. if (port_req->ch_en == port_req->req_ch)
  595. continue;
  596. /* remove new ch req's*/
  597. port_req->req_ch = port_req->ch_en;
  598. /* If no streams enabled on port, remove the port req */
  599. if (port_req->ch_en == 0) {
  600. list_del(&port_req->list);
  601. kfree(port_req);
  602. }
  603. }
  604. /* remove new ch req's on mport*/
  605. mport->req_ch = mport->ch_en;
  606. if (!(mport->ch_en)) {
  607. mport->port_en = false;
  608. master->port_en_mask &= ~i;
  609. }
  610. }
  611. }
  612. static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
  613. {
  614. u32 value, slv_id;
  615. struct swr_port_info *port_req;
  616. int i;
  617. struct swrm_mports *mport;
  618. u32 reg[SWRM_MAX_PORT_REG];
  619. u32 val[SWRM_MAX_PORT_REG];
  620. int len = 0;
  621. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  622. if (!swrm) {
  623. pr_err("%s: swrm is null\n", __func__);
  624. return;
  625. }
  626. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  627. master->num_port);
  628. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  629. mport = &(swrm->mport_cfg[i]);
  630. if (!mport->port_en)
  631. continue;
  632. list_for_each_entry(port_req, &mport->port_req_list, list) {
  633. slv_id = port_req->slave_port_id;
  634. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  635. val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
  636. port_req->dev_num, 0x00,
  637. SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
  638. bank));
  639. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  640. val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
  641. port_req->dev_num, 0x00,
  642. SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
  643. bank));
  644. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  645. val[len++] = SWR_REG_VAL_PACK(mport->offset1,
  646. port_req->dev_num, 0x00,
  647. SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
  648. bank));
  649. if (port_req->slave_port_id) {
  650. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  651. val[len++] = SWR_REG_VAL_PACK(mport->offset2,
  652. port_req->dev_num, 0x00,
  653. SWRS_DP_OFFSET_CONTROL_2_BANK(
  654. slv_id, bank));
  655. }
  656. port_req->ch_en = port_req->req_ch;
  657. }
  658. value = ((mport->req_ch)
  659. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  660. value |= ((mport->offset2)
  661. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  662. value |= ((mport->offset1)
  663. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  664. value |= mport->sinterval;
  665. reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
  666. val[len++] = value;
  667. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  668. __func__, i,
  669. (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
  670. mport->ch_en = mport->req_ch;
  671. }
  672. swr_master_bulk_write(swrm, reg, val, len);
  673. }
  674. static void swrm_apply_port_config(struct swr_master *master)
  675. {
  676. u8 bank;
  677. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  678. if (!swrm) {
  679. pr_err("%s: Invalid handle to swr controller\n",
  680. __func__);
  681. return;
  682. }
  683. bank = get_inactive_bank_num(swrm);
  684. dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
  685. __func__, bank, master->num_port);
  686. swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
  687. SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
  688. swrm_copy_data_port_config(master, bank);
  689. }
  690. static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
  691. {
  692. u8 bank;
  693. u32 value, n_col;
  694. int ret;
  695. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  696. int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
  697. SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
  698. SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
  699. u8 inactive_bank;
  700. if (!swrm) {
  701. pr_err("%s: swrm is null\n", __func__);
  702. return -EFAULT;
  703. }
  704. mutex_lock(&swrm->mlock);
  705. if (enable)
  706. pm_runtime_get_sync(swrm->dev);
  707. bank = get_inactive_bank_num(swrm);
  708. if (enable) {
  709. ret = swrm_get_port_config(swrm);
  710. if (ret) {
  711. /* cannot accommodate ports */
  712. swrm_cleanup_disabled_port_reqs(master);
  713. pm_runtime_mark_last_busy(swrm->dev);
  714. pm_runtime_put_autosuspend(swrm->dev);
  715. mutex_unlock(&swrm->mlock);
  716. return -EINVAL;
  717. }
  718. /* apply the new port config*/
  719. swrm_apply_port_config(master);
  720. } else {
  721. swrm_disable_ports(master, bank);
  722. }
  723. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
  724. __func__, enable, swrm->num_cfg_devs);
  725. if (enable) {
  726. /* set Row = 48 and col = 16 */
  727. n_col = SWR_MAX_COL;
  728. } else {
  729. /*
  730. * Do not change to 48x2 if there are still active ports
  731. */
  732. if (!master->num_port)
  733. n_col = SWR_MIN_COL;
  734. else
  735. n_col = SWR_MAX_COL;
  736. }
  737. value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
  738. value &= (~mask);
  739. value |= ((0 << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  740. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  741. (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  742. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  743. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  744. SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  745. enable_bank_switch(swrm, bank, SWR_MAX_ROW, n_col);
  746. inactive_bank = bank ? 0 : 1;
  747. if (enable)
  748. swrm_copy_data_port_config(master, inactive_bank);
  749. else {
  750. swrm_disable_ports(master, inactive_bank);
  751. swrm_cleanup_disabled_port_reqs(master);
  752. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  753. __func__);
  754. pm_runtime_mark_last_busy(swrm->dev);
  755. pm_runtime_put_autosuspend(swrm->dev);
  756. }
  757. mutex_unlock(&swrm->mlock);
  758. return 0;
  759. }
  760. static int swrm_connect_port(struct swr_master *master,
  761. struct swr_params *portinfo)
  762. {
  763. int i;
  764. struct swr_port_info *port_req;
  765. int ret = 0;
  766. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  767. struct swrm_mports *mport;
  768. u8 mstr_port_id, mstr_ch_msk;
  769. dev_dbg(&master->dev, "%s: enter\n", __func__);
  770. if (!portinfo)
  771. return -EINVAL;
  772. if (!swrm) {
  773. dev_err(&master->dev,
  774. "%s: Invalid handle to swr controller\n",
  775. __func__);
  776. return -EINVAL;
  777. }
  778. mutex_lock(&swrm->mlock);
  779. for (i = 0; i < portinfo->num_port; i++) {
  780. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
  781. portinfo->port_type[i],
  782. portinfo->port_id[i]);
  783. if (ret) {
  784. dev_err(&master->dev,
  785. "%s: mstr portid for slv port %d not found\n",
  786. __func__, portinfo->port_id[i]);
  787. goto port_fail;
  788. }
  789. mport = &(swrm->mport_cfg[mstr_port_id]);
  790. /* get port req */
  791. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  792. portinfo->dev_num);
  793. if (!port_req) {
  794. dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
  795. __func__, portinfo->port_id[i],
  796. portinfo->dev_num);
  797. port_req = kzalloc(sizeof(struct swr_port_info),
  798. GFP_KERNEL);
  799. if (!port_req) {
  800. ret = -ENOMEM;
  801. goto mem_fail;
  802. }
  803. port_req->dev_num = portinfo->dev_num;
  804. port_req->slave_port_id = portinfo->port_id[i];
  805. port_req->num_ch = portinfo->num_ch[i];
  806. port_req->ch_rate = portinfo->ch_rate[i];
  807. port_req->ch_en = 0;
  808. port_req->master_port_id = mstr_port_id;
  809. list_add(&port_req->list, &mport->port_req_list);
  810. }
  811. port_req->req_ch |= portinfo->ch_en[i];
  812. dev_dbg(&master->dev,
  813. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
  814. __func__, port_req->master_port_id,
  815. port_req->slave_port_id, port_req->ch_rate,
  816. port_req->num_ch);
  817. /* Put the port req on master port */
  818. mport = &(swrm->mport_cfg[mstr_port_id]);
  819. mport->port_en = true;
  820. mport->req_ch |= mstr_ch_msk;
  821. master->port_en_mask |= (1 << mstr_port_id);
  822. }
  823. master->num_port += portinfo->num_port;
  824. swr_port_response(master, portinfo->tid);
  825. mutex_unlock(&swrm->mlock);
  826. return 0;
  827. port_fail:
  828. mem_fail:
  829. /* cleanup port reqs in error condition */
  830. swrm_cleanup_disabled_port_reqs(master);
  831. mutex_unlock(&swrm->mlock);
  832. return ret;
  833. }
  834. static int swrm_disconnect_port(struct swr_master *master,
  835. struct swr_params *portinfo)
  836. {
  837. int i, ret = 0;
  838. struct swr_port_info *port_req;
  839. struct swrm_mports *mport;
  840. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  841. u8 mstr_port_id, mstr_ch_mask;
  842. if (!swrm) {
  843. dev_err(&master->dev,
  844. "%s: Invalid handle to swr controller\n",
  845. __func__);
  846. return -EINVAL;
  847. }
  848. if (!portinfo) {
  849. dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
  850. return -EINVAL;
  851. }
  852. mutex_lock(&swrm->mlock);
  853. for (i = 0; i < portinfo->num_port; i++) {
  854. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
  855. portinfo->port_type[i], portinfo->port_id[i]);
  856. if (ret) {
  857. dev_err(&master->dev,
  858. "%s: mstr portid for slv port %d not found\n",
  859. __func__, portinfo->port_id[i]);
  860. mutex_unlock(&swrm->mlock);
  861. return -EINVAL;
  862. }
  863. mport = &(swrm->mport_cfg[mstr_port_id]);
  864. /* get port req */
  865. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  866. portinfo->dev_num);
  867. if (!port_req) {
  868. dev_err(&master->dev, "%s:port not enabled : port %d\n",
  869. __func__, portinfo->port_id[i]);
  870. return -EINVAL;
  871. }
  872. port_req->req_ch &= ~portinfo->ch_en[i];
  873. mport->req_ch &= ~mstr_ch_mask;
  874. }
  875. master->num_port -= portinfo->num_port;
  876. swr_port_response(master, portinfo->tid);
  877. mutex_unlock(&swrm->mlock);
  878. return 0;
  879. }
  880. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  881. int status, u8 *devnum)
  882. {
  883. int i;
  884. int new_sts = status;
  885. int ret = SWR_NOT_PRESENT;
  886. if (status != swrm->slave_status) {
  887. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  888. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  889. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  890. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  891. *devnum = i;
  892. break;
  893. }
  894. status >>= 2;
  895. swrm->slave_status >>= 2;
  896. }
  897. swrm->slave_status = new_sts;
  898. }
  899. return ret;
  900. }
  901. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  902. {
  903. struct swr_mstr_ctrl *swrm = dev;
  904. u32 value, intr_sts;
  905. int status, chg_sts, i;
  906. u8 devnum = 0;
  907. int ret = IRQ_HANDLED;
  908. struct swr_device *swr_dev;
  909. struct swr_master *mstr = &swrm->master;
  910. mutex_lock(&swrm->reslock);
  911. swrm_clk_request(swrm, true);
  912. mutex_unlock(&swrm->reslock);
  913. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  914. intr_sts &= SWRM_INTERRUPT_STATUS_RMSK;
  915. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  916. value = intr_sts & (1 << i);
  917. if (!value)
  918. continue;
  919. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, value);
  920. switch (value) {
  921. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  922. dev_dbg(swrm->dev, "Trigger irq to slave device\n");
  923. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  924. swrm_check_slave_change_status(swrm, status,
  925. &devnum);
  926. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  927. if (swr_dev->dev_num != devnum)
  928. continue;
  929. if (swr_dev->slave_irq)
  930. handle_nested_irq(swr_dev->slave_irq);
  931. }
  932. break;
  933. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  934. dev_dbg(swrm->dev, "SWR new slave attached\n");
  935. break;
  936. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  937. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  938. if (status == swrm->slave_status) {
  939. dev_dbg(swrm->dev,
  940. "%s: No change in slave status: %d\n",
  941. __func__, status);
  942. break;
  943. }
  944. chg_sts = swrm_check_slave_change_status(swrm, status,
  945. &devnum);
  946. switch (chg_sts) {
  947. case SWR_NOT_PRESENT:
  948. dev_dbg(swrm->dev, "device %d got detached\n",
  949. devnum);
  950. break;
  951. case SWR_ATTACHED_OK:
  952. dev_dbg(swrm->dev, "device %d got attached\n",
  953. devnum);
  954. break;
  955. case SWR_ALERT:
  956. dev_dbg(swrm->dev,
  957. "device %d has pending interrupt\n",
  958. devnum);
  959. break;
  960. }
  961. break;
  962. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  963. dev_err_ratelimited(swrm->dev,
  964. "SWR bus clsh detected\n");
  965. break;
  966. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  967. dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
  968. break;
  969. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  970. dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
  971. break;
  972. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  973. dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
  974. break;
  975. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  976. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  977. dev_err_ratelimited(swrm->dev,
  978. "SWR CMD error, fifo status 0x%x, flushing fifo\n",
  979. value);
  980. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  981. break;
  982. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  983. dev_dbg(swrm->dev, "SWR Port collision detected\n");
  984. break;
  985. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  986. dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
  987. break;
  988. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  989. complete(&swrm->broadcast);
  990. dev_dbg(swrm->dev, "SWR cmd id finished\n");
  991. break;
  992. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
  993. break;
  994. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  995. break;
  996. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  997. break;
  998. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  999. complete(&swrm->reset);
  1000. break;
  1001. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  1002. break;
  1003. default:
  1004. dev_err_ratelimited(swrm->dev,
  1005. "SWR unknown interrupt\n");
  1006. ret = IRQ_NONE;
  1007. break;
  1008. }
  1009. }
  1010. mutex_lock(&swrm->reslock);
  1011. swrm_clk_request(swrm, false);
  1012. mutex_unlock(&swrm->reslock);
  1013. return ret;
  1014. }
  1015. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  1016. {
  1017. u32 val;
  1018. swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1019. val = (swrm->slave_status >> (devnum * 2));
  1020. val &= SWRM_MCP_SLV_STATUS_MASK;
  1021. return val;
  1022. }
  1023. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  1024. u8 *dev_num)
  1025. {
  1026. int i;
  1027. u64 id = 0;
  1028. int ret = -EINVAL;
  1029. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1030. struct swr_device *swr_dev;
  1031. u32 num_dev = 0;
  1032. if (!swrm) {
  1033. pr_err("%s: Invalid handle to swr controller\n",
  1034. __func__);
  1035. return ret;
  1036. }
  1037. if (swrm->num_dev)
  1038. num_dev = swrm->num_dev;
  1039. else
  1040. num_dev = mstr->num_dev;
  1041. pm_runtime_get_sync(swrm->dev);
  1042. for (i = 1; i < (num_dev + 1); i++) {
  1043. id = ((u64)(swr_master_read(swrm,
  1044. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  1045. id |= swr_master_read(swrm,
  1046. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  1047. /*
  1048. * As pm_runtime_get_sync() brings all slaves out of reset
  1049. * update logical device number for all slaves.
  1050. */
  1051. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1052. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  1053. u32 status = swrm_get_device_status(swrm, i);
  1054. if ((status == 0x01) || (status == 0x02)) {
  1055. swr_dev->dev_num = i;
  1056. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  1057. *dev_num = i;
  1058. ret = 0;
  1059. }
  1060. dev_dbg(swrm->dev,
  1061. "%s: devnum %d is assigned for dev addr %lx\n",
  1062. __func__, i, swr_dev->addr);
  1063. }
  1064. }
  1065. }
  1066. }
  1067. if (ret)
  1068. dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
  1069. __func__, dev_id);
  1070. pm_runtime_mark_last_busy(swrm->dev);
  1071. pm_runtime_put_autosuspend(swrm->dev);
  1072. return ret;
  1073. }
  1074. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  1075. {
  1076. int ret = 0;
  1077. u32 val;
  1078. u8 row_ctrl = SWR_MAX_ROW;
  1079. u8 col_ctrl = SWR_MIN_COL;
  1080. u8 ssp_period = 1;
  1081. u8 retry_cmd_num = 3;
  1082. u32 reg[SWRM_MAX_INIT_REG];
  1083. u32 value[SWRM_MAX_INIT_REG];
  1084. int len = 0;
  1085. /* Clear Rows and Cols */
  1086. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1087. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1088. (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1089. reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
  1090. value[len++] = val;
  1091. /* Set Auto enumeration flag */
  1092. reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
  1093. value[len++] = 1;
  1094. /* Mask soundwire interrupts */
  1095. reg[len] = SWRM_INTERRUPT_MASK_ADDR;
  1096. value[len++] = 0x1FFFD;
  1097. /* Configure No pings */
  1098. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1099. val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
  1100. val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
  1101. reg[len] = SWRM_MCP_CFG_ADDR;
  1102. value[len++] = val;
  1103. /* Configure number of retries of a read/write cmd */
  1104. val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
  1105. reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
  1106. value[len++] = val;
  1107. /* Set IRQ to PULSE */
  1108. reg[len] = SWRM_COMP_CFG_ADDR;
  1109. value[len++] = 0x02;
  1110. reg[len] = SWRM_COMP_CFG_ADDR;
  1111. value[len++] = 0x03;
  1112. reg[len] = SWRM_INTERRUPT_CLEAR;
  1113. value[len++] = 0x08;
  1114. swr_master_bulk_write(swrm, reg, value, len);
  1115. return ret;
  1116. }
  1117. static int swrm_probe(struct platform_device *pdev)
  1118. {
  1119. struct swr_mstr_ctrl *swrm;
  1120. struct swr_ctrl_platform_data *pdata;
  1121. u32 i, num_ports, port_num, port_type, ch_mask;
  1122. u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
  1123. int ret = 0;
  1124. /* Allocate soundwire master driver structure */
  1125. swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
  1126. GFP_KERNEL);
  1127. if (!swrm) {
  1128. ret = -ENOMEM;
  1129. goto err_memory_fail;
  1130. }
  1131. swrm->dev = &pdev->dev;
  1132. platform_set_drvdata(pdev, swrm);
  1133. swr_set_ctrl_data(&swrm->master, swrm);
  1134. pdata = dev_get_platdata(&pdev->dev);
  1135. if (!pdata) {
  1136. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  1137. __func__);
  1138. ret = -EINVAL;
  1139. goto err_pdata_fail;
  1140. }
  1141. swrm->handle = (void *)pdata->handle;
  1142. if (!swrm->handle) {
  1143. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  1144. __func__);
  1145. ret = -EINVAL;
  1146. goto err_pdata_fail;
  1147. }
  1148. if (!(of_property_read_u32(pdev->dev.of_node,
  1149. "swrm-io-base", &swrm->swrm_base_reg)))
  1150. ret = of_property_read_u32(pdev->dev.of_node,
  1151. "swrm-io-base", &swrm->swrm_base_reg);
  1152. if (!swrm->swrm_base_reg) {
  1153. swrm->read = pdata->read;
  1154. if (!swrm->read) {
  1155. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  1156. __func__);
  1157. ret = -EINVAL;
  1158. goto err_pdata_fail;
  1159. }
  1160. swrm->write = pdata->write;
  1161. if (!swrm->write) {
  1162. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  1163. __func__);
  1164. ret = -EINVAL;
  1165. goto err_pdata_fail;
  1166. }
  1167. swrm->bulk_write = pdata->bulk_write;
  1168. if (!swrm->bulk_write) {
  1169. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  1170. __func__);
  1171. ret = -EINVAL;
  1172. goto err_pdata_fail;
  1173. }
  1174. } else {
  1175. swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
  1176. swrm->swrm_base_reg, SWRM_MAX_REGISTER);
  1177. }
  1178. swrm->clk = pdata->clk;
  1179. if (!swrm->clk) {
  1180. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  1181. __func__);
  1182. ret = -EINVAL;
  1183. goto err_pdata_fail;
  1184. }
  1185. /* Parse soundwire port mapping */
  1186. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
  1187. &num_ports);
  1188. if (ret) {
  1189. dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
  1190. goto err_pdata_fail;
  1191. }
  1192. swrm->num_ports = num_ports;
  1193. if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
  1194. &map_size)) {
  1195. dev_err(swrm->dev, "missing port mapping\n");
  1196. goto err_pdata_fail;
  1197. }
  1198. map_length = map_size / (3 * sizeof(u32));
  1199. if (num_ports > SWR_MSTR_PORT_LEN) {
  1200. dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
  1201. __func__);
  1202. ret = -EINVAL;
  1203. goto err_pdata_fail;
  1204. }
  1205. temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
  1206. if (!temp) {
  1207. ret = -ENOMEM;
  1208. goto err_pdata_fail;
  1209. }
  1210. ret = of_property_read_u32_array(pdev->dev.of_node,
  1211. "qcom,swr-port-mapping", temp, 3 * map_length);
  1212. if (ret) {
  1213. dev_err(swrm->dev, "%s: Failed to read port mapping\n",
  1214. __func__);
  1215. goto err_pdata_fail;
  1216. }
  1217. for (i = 0; i < map_length; i++) {
  1218. port_num = temp[3 * i];
  1219. port_type = temp[3 * i + 1];
  1220. ch_mask = temp[3 * i + 2];
  1221. if (port_num != old_port_num)
  1222. ch_iter = 0;
  1223. swrm->port_mapping[port_num][ch_iter].port_type = port_type;
  1224. swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
  1225. old_port_num = port_num;
  1226. }
  1227. devm_kfree(&pdev->dev, temp);
  1228. swrm->reg_irq = pdata->reg_irq;
  1229. swrm->master.read = swrm_read;
  1230. swrm->master.write = swrm_write;
  1231. swrm->master.bulk_write = swrm_bulk_write;
  1232. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  1233. swrm->master.connect_port = swrm_connect_port;
  1234. swrm->master.disconnect_port = swrm_disconnect_port;
  1235. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  1236. swrm->master.remove_from_group = swrm_remove_from_group;
  1237. swrm->master.dev.parent = &pdev->dev;
  1238. swrm->master.dev.of_node = pdev->dev.of_node;
  1239. swrm->master.num_port = 0;
  1240. swrm->rcmd_id = 0;
  1241. swrm->wcmd_id = 0;
  1242. swrm->slave_status = 0;
  1243. swrm->num_rx_chs = 0;
  1244. swrm->clk_ref_count = 0;
  1245. swrm->state = SWR_MSTR_RESUME;
  1246. init_completion(&swrm->reset);
  1247. init_completion(&swrm->broadcast);
  1248. mutex_init(&swrm->mlock);
  1249. mutex_init(&swrm->reslock);
  1250. mutex_init(&swrm->force_down_lock);
  1251. for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
  1252. INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
  1253. ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
  1254. &swrm->num_dev);
  1255. if (ret) {
  1256. dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
  1257. __func__, "qcom,swr-num-dev");
  1258. } else {
  1259. if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
  1260. dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
  1261. __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
  1262. ret = -EINVAL;
  1263. goto err_pdata_fail;
  1264. }
  1265. }
  1266. if (swrm->reg_irq) {
  1267. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  1268. SWR_IRQ_REGISTER);
  1269. if (ret) {
  1270. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  1271. __func__, ret);
  1272. goto err_irq_fail;
  1273. }
  1274. } else {
  1275. swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
  1276. if (swrm->irq < 0) {
  1277. dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
  1278. __func__, swrm->irq);
  1279. goto err_irq_fail;
  1280. }
  1281. ret = request_threaded_irq(swrm->irq, NULL,
  1282. swr_mstr_interrupt,
  1283. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1284. "swr_master_irq", swrm);
  1285. if (ret) {
  1286. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  1287. __func__, ret);
  1288. goto err_irq_fail;
  1289. }
  1290. }
  1291. ret = swr_register_master(&swrm->master);
  1292. if (ret) {
  1293. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  1294. goto err_mstr_fail;
  1295. }
  1296. /* Add devices registered with board-info as the
  1297. * controller will be up now
  1298. */
  1299. swr_master_add_boarddevices(&swrm->master);
  1300. mutex_lock(&swrm->mlock);
  1301. swrm_clk_request(swrm, true);
  1302. ret = swrm_master_init(swrm);
  1303. if (ret < 0) {
  1304. dev_err(&pdev->dev,
  1305. "%s: Error in master Initialization , err %d\n",
  1306. __func__, ret);
  1307. mutex_unlock(&swrm->mlock);
  1308. goto err_mstr_fail;
  1309. }
  1310. swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
  1311. mutex_unlock(&swrm->mlock);
  1312. if (pdev->dev.of_node)
  1313. of_register_swr_devices(&swrm->master);
  1314. dbgswrm = swrm;
  1315. debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  1316. if (!IS_ERR(debugfs_swrm_dent)) {
  1317. debugfs_peek = debugfs_create_file("swrm_peek",
  1318. S_IFREG | 0444, debugfs_swrm_dent,
  1319. (void *) "swrm_peek", &swrm_debug_ops);
  1320. debugfs_poke = debugfs_create_file("swrm_poke",
  1321. S_IFREG | 0444, debugfs_swrm_dent,
  1322. (void *) "swrm_poke", &swrm_debug_ops);
  1323. debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  1324. S_IFREG | 0444, debugfs_swrm_dent,
  1325. (void *) "swrm_reg_dump",
  1326. &swrm_debug_ops);
  1327. }
  1328. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1329. pm_runtime_use_autosuspend(&pdev->dev);
  1330. pm_runtime_set_active(&pdev->dev);
  1331. pm_runtime_enable(&pdev->dev);
  1332. pm_runtime_mark_last_busy(&pdev->dev);
  1333. return 0;
  1334. err_mstr_fail:
  1335. if (swrm->reg_irq)
  1336. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1337. swrm, SWR_IRQ_FREE);
  1338. else if (swrm->irq)
  1339. free_irq(swrm->irq, swrm);
  1340. err_irq_fail:
  1341. mutex_destroy(&swrm->mlock);
  1342. mutex_destroy(&swrm->reslock);
  1343. mutex_destroy(&swrm->force_down_lock);
  1344. err_pdata_fail:
  1345. err_memory_fail:
  1346. return ret;
  1347. }
  1348. static int swrm_remove(struct platform_device *pdev)
  1349. {
  1350. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1351. if (swrm->reg_irq)
  1352. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1353. swrm, SWR_IRQ_FREE);
  1354. else if (swrm->irq)
  1355. free_irq(swrm->irq, swrm);
  1356. pm_runtime_disable(&pdev->dev);
  1357. pm_runtime_set_suspended(&pdev->dev);
  1358. swr_unregister_master(&swrm->master);
  1359. mutex_destroy(&swrm->mlock);
  1360. mutex_destroy(&swrm->reslock);
  1361. mutex_destroy(&swrm->force_down_lock);
  1362. devm_kfree(&pdev->dev, swrm);
  1363. return 0;
  1364. }
  1365. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  1366. {
  1367. u32 val;
  1368. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  1369. swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
  1370. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1371. val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
  1372. swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
  1373. swrm->state = SWR_MSTR_PAUSE;
  1374. return 0;
  1375. }
  1376. #ifdef CONFIG_PM
  1377. static int swrm_runtime_resume(struct device *dev)
  1378. {
  1379. struct platform_device *pdev = to_platform_device(dev);
  1380. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1381. int ret = 0;
  1382. struct swr_master *mstr = &swrm->master;
  1383. struct swr_device *swr_dev;
  1384. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  1385. __func__, swrm->state);
  1386. mutex_lock(&swrm->reslock);
  1387. if ((swrm->state == SWR_MSTR_PAUSE) ||
  1388. (swrm->state == SWR_MSTR_DOWN)) {
  1389. if (swrm->state == SWR_MSTR_DOWN) {
  1390. if (swrm_clk_request(swrm, true))
  1391. goto exit;
  1392. }
  1393. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1394. ret = swr_device_up(swr_dev);
  1395. if (ret) {
  1396. dev_err(dev,
  1397. "%s: failed to wakeup swr dev %d\n",
  1398. __func__, swr_dev->dev_num);
  1399. swrm_clk_request(swrm, false);
  1400. goto exit;
  1401. }
  1402. }
  1403. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  1404. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  1405. swrm_master_init(swrm);
  1406. }
  1407. exit:
  1408. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1409. mutex_unlock(&swrm->reslock);
  1410. return ret;
  1411. }
  1412. static int swrm_runtime_suspend(struct device *dev)
  1413. {
  1414. struct platform_device *pdev = to_platform_device(dev);
  1415. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1416. int ret = 0;
  1417. struct swr_master *mstr = &swrm->master;
  1418. struct swr_device *swr_dev;
  1419. int current_state = 0;
  1420. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  1421. __func__, swrm->state);
  1422. mutex_lock(&swrm->reslock);
  1423. mutex_lock(&swrm->force_down_lock);
  1424. current_state = swrm->state;
  1425. mutex_unlock(&swrm->force_down_lock);
  1426. if ((current_state == SWR_MSTR_RESUME) ||
  1427. (current_state == SWR_MSTR_UP) ||
  1428. (current_state == SWR_MSTR_SSR)) {
  1429. if ((current_state != SWR_MSTR_SSR) &&
  1430. swrm_is_port_en(&swrm->master)) {
  1431. dev_dbg(dev, "%s ports are enabled\n", __func__);
  1432. ret = -EBUSY;
  1433. goto exit;
  1434. }
  1435. swrm_clk_pause(swrm);
  1436. swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
  1437. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1438. ret = swr_device_down(swr_dev);
  1439. if (ret) {
  1440. dev_err(dev,
  1441. "%s: failed to shutdown swr dev %d\n",
  1442. __func__, swr_dev->dev_num);
  1443. goto exit;
  1444. }
  1445. }
  1446. swrm_clk_request(swrm, false);
  1447. }
  1448. exit:
  1449. mutex_unlock(&swrm->reslock);
  1450. return ret;
  1451. }
  1452. #endif /* CONFIG_PM */
  1453. static int swrm_device_down(struct device *dev)
  1454. {
  1455. struct platform_device *pdev = to_platform_device(dev);
  1456. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1457. int ret = 0;
  1458. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  1459. mutex_lock(&swrm->force_down_lock);
  1460. swrm->state = SWR_MSTR_SSR;
  1461. mutex_unlock(&swrm->force_down_lock);
  1462. /* Use pm runtime function to tear down */
  1463. ret = pm_runtime_put_sync_suspend(dev);
  1464. pm_runtime_get_noresume(dev);
  1465. return ret;
  1466. }
  1467. /**
  1468. * swrm_wcd_notify - parent device can notify to soundwire master through
  1469. * this function
  1470. * @pdev: pointer to platform device structure
  1471. * @id: command id from parent to the soundwire master
  1472. * @data: data from parent device to soundwire master
  1473. */
  1474. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  1475. {
  1476. struct swr_mstr_ctrl *swrm;
  1477. int ret = 0;
  1478. struct swr_master *mstr;
  1479. struct swr_device *swr_dev;
  1480. if (!pdev) {
  1481. pr_err("%s: pdev is NULL\n", __func__);
  1482. return -EINVAL;
  1483. }
  1484. swrm = platform_get_drvdata(pdev);
  1485. if (!swrm) {
  1486. dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
  1487. return -EINVAL;
  1488. }
  1489. mstr = &swrm->master;
  1490. switch (id) {
  1491. case SWR_DEVICE_DOWN:
  1492. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  1493. mutex_lock(&swrm->mlock);
  1494. if ((swrm->state == SWR_MSTR_PAUSE) ||
  1495. (swrm->state == SWR_MSTR_DOWN))
  1496. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  1497. __func__, swrm->state);
  1498. else
  1499. swrm_device_down(&pdev->dev);
  1500. mutex_unlock(&swrm->mlock);
  1501. break;
  1502. case SWR_DEVICE_UP:
  1503. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  1504. mutex_lock(&swrm->mlock);
  1505. mutex_lock(&swrm->reslock);
  1506. if ((swrm->state == SWR_MSTR_RESUME) ||
  1507. (swrm->state == SWR_MSTR_UP)) {
  1508. dev_dbg(swrm->dev, "%s: SWR master is already UP: %d\n",
  1509. __func__, swrm->state);
  1510. } else {
  1511. pm_runtime_mark_last_busy(&pdev->dev);
  1512. mutex_unlock(&swrm->reslock);
  1513. pm_runtime_get_sync(&pdev->dev);
  1514. mutex_lock(&swrm->reslock);
  1515. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1516. ret = swr_reset_device(swr_dev);
  1517. if (ret) {
  1518. dev_err(swrm->dev,
  1519. "%s: failed to reset swr device %d\n",
  1520. __func__, swr_dev->dev_num);
  1521. swrm_clk_request(swrm, false);
  1522. }
  1523. }
  1524. pm_runtime_mark_last_busy(&pdev->dev);
  1525. pm_runtime_put_autosuspend(&pdev->dev);
  1526. }
  1527. mutex_unlock(&swrm->reslock);
  1528. mutex_unlock(&swrm->mlock);
  1529. break;
  1530. case SWR_SET_NUM_RX_CH:
  1531. if (!data) {
  1532. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  1533. ret = -EINVAL;
  1534. } else {
  1535. mutex_lock(&swrm->mlock);
  1536. swrm->num_rx_chs = *(int *)data;
  1537. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  1538. list_for_each_entry(swr_dev, &mstr->devices,
  1539. dev_list) {
  1540. ret = swr_set_device_group(swr_dev,
  1541. SWR_BROADCAST);
  1542. if (ret)
  1543. dev_err(swrm->dev,
  1544. "%s: set num ch failed\n",
  1545. __func__);
  1546. }
  1547. } else {
  1548. list_for_each_entry(swr_dev, &mstr->devices,
  1549. dev_list) {
  1550. ret = swr_set_device_group(swr_dev,
  1551. SWR_GROUP_NONE);
  1552. if (ret)
  1553. dev_err(swrm->dev,
  1554. "%s: set num ch failed\n",
  1555. __func__);
  1556. }
  1557. }
  1558. mutex_unlock(&swrm->mlock);
  1559. }
  1560. break;
  1561. default:
  1562. dev_err(swrm->dev, "%s: swr master unknown id %d\n",
  1563. __func__, id);
  1564. break;
  1565. }
  1566. return ret;
  1567. }
  1568. EXPORT_SYMBOL(swrm_wcd_notify);
  1569. #ifdef CONFIG_PM_SLEEP
  1570. static int swrm_suspend(struct device *dev)
  1571. {
  1572. int ret = -EBUSY;
  1573. struct platform_device *pdev = to_platform_device(dev);
  1574. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1575. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  1576. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  1577. ret = swrm_runtime_suspend(dev);
  1578. if (!ret) {
  1579. /*
  1580. * Synchronize runtime-pm and system-pm states:
  1581. * At this point, we are already suspended. If
  1582. * runtime-pm still thinks its active, then
  1583. * make sure its status is in sync with HW
  1584. * status. The three below calls let the
  1585. * runtime-pm know that we are suspended
  1586. * already without re-invoking the suspend
  1587. * callback
  1588. */
  1589. pm_runtime_disable(dev);
  1590. pm_runtime_set_suspended(dev);
  1591. pm_runtime_enable(dev);
  1592. }
  1593. }
  1594. if (ret == -EBUSY) {
  1595. /*
  1596. * There is a possibility that some audio stream is active
  1597. * during suspend. We dont want to return suspend failure in
  1598. * that case so that display and relevant components can still
  1599. * go to suspend.
  1600. * If there is some other error, then it should be passed-on
  1601. * to system level suspend
  1602. */
  1603. ret = 0;
  1604. }
  1605. return ret;
  1606. }
  1607. static int swrm_resume(struct device *dev)
  1608. {
  1609. int ret = 0;
  1610. struct platform_device *pdev = to_platform_device(dev);
  1611. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1612. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  1613. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  1614. ret = swrm_runtime_resume(dev);
  1615. if (!ret) {
  1616. pm_runtime_mark_last_busy(dev);
  1617. pm_request_autosuspend(dev);
  1618. }
  1619. }
  1620. return ret;
  1621. }
  1622. #endif /* CONFIG_PM_SLEEP */
  1623. static const struct dev_pm_ops swrm_dev_pm_ops = {
  1624. SET_SYSTEM_SLEEP_PM_OPS(
  1625. swrm_suspend,
  1626. swrm_resume
  1627. )
  1628. SET_RUNTIME_PM_OPS(
  1629. swrm_runtime_suspend,
  1630. swrm_runtime_resume,
  1631. NULL
  1632. )
  1633. };
  1634. static const struct of_device_id swrm_dt_match[] = {
  1635. {
  1636. .compatible = "qcom,swr-mstr",
  1637. },
  1638. {}
  1639. };
  1640. static struct platform_driver swr_mstr_driver = {
  1641. .probe = swrm_probe,
  1642. .remove = swrm_remove,
  1643. .driver = {
  1644. .name = SWR_WCD_NAME,
  1645. .owner = THIS_MODULE,
  1646. .pm = &swrm_dev_pm_ops,
  1647. .of_match_table = swrm_dt_match,
  1648. },
  1649. };
  1650. static int __init swrm_init(void)
  1651. {
  1652. return platform_driver_register(&swr_mstr_driver);
  1653. }
  1654. module_init(swrm_init);
  1655. static void __exit swrm_exit(void)
  1656. {
  1657. platform_driver_unregister(&swr_mstr_driver);
  1658. }
  1659. module_exit(swrm_exit);
  1660. MODULE_LICENSE("GPL v2");
  1661. MODULE_DESCRIPTION("SoundWire Master Controller");
  1662. MODULE_ALIAS("platform:swr-mstr");