swr-wcd-ctrl.c 48 KB

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