swr-wcd-ctrl.c 46 KB

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