intel.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. // Copyright(c) 2015-17 Intel Corporation.
  3. /*
  4. * Soundwire Intel Master Driver
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/cleanup.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/delay.h>
  10. #include <linux/io.h>
  11. #include <sound/pcm_params.h>
  12. #include <linux/pm_runtime.h>
  13. #include <sound/soc.h>
  14. #include <linux/soundwire/sdw_registers.h>
  15. #include <linux/soundwire/sdw.h>
  16. #include <linux/soundwire/sdw_intel.h>
  17. #include "cadence_master.h"
  18. #include "bus.h"
  19. #include "intel.h"
  20. static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
  21. {
  22. int timeout = 10;
  23. u32 reg_read;
  24. do {
  25. reg_read = readl(base + offset);
  26. if ((reg_read & mask) == target)
  27. return 0;
  28. timeout--;
  29. usleep_range(50, 100);
  30. } while (timeout != 0);
  31. return -EAGAIN;
  32. }
  33. static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
  34. {
  35. writel(value, base + offset);
  36. return intel_wait_bit(base, offset, mask, 0);
  37. }
  38. static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
  39. {
  40. writel(value, base + offset);
  41. return intel_wait_bit(base, offset, mask, mask);
  42. }
  43. /*
  44. * debugfs
  45. */
  46. #ifdef CONFIG_DEBUG_FS
  47. #define RD_BUF (2 * PAGE_SIZE)
  48. static ssize_t intel_sprintf(void __iomem *mem, bool l,
  49. char *buf, size_t pos, unsigned int reg)
  50. {
  51. int value;
  52. if (l)
  53. value = intel_readl(mem, reg);
  54. else
  55. value = intel_readw(mem, reg);
  56. return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
  57. }
  58. static int intel_reg_show(struct seq_file *s_file, void *data)
  59. {
  60. struct sdw_intel *sdw = s_file->private;
  61. void __iomem *s = sdw->link_res->shim;
  62. void __iomem *a = sdw->link_res->alh;
  63. ssize_t ret;
  64. int i, j;
  65. unsigned int links, reg;
  66. char *buf __free(kfree) = kzalloc(RD_BUF, GFP_KERNEL);
  67. if (!buf)
  68. return -ENOMEM;
  69. links = intel_readl(s, SDW_SHIM_LCAP) & SDW_SHIM_LCAP_LCOUNT_MASK;
  70. ret = scnprintf(buf, RD_BUF, "Register Value\n");
  71. ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
  72. for (i = 0; i < links; i++) {
  73. reg = SDW_SHIM_LCAP + i * 4;
  74. ret += intel_sprintf(s, true, buf, ret, reg);
  75. }
  76. for (i = 0; i < links; i++) {
  77. ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
  78. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
  79. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
  80. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
  81. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
  82. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
  83. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
  84. ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
  85. /*
  86. * the value 10 is the number of PDIs. We will need a
  87. * cleanup to remove hard-coded Intel configurations
  88. * from cadence_master.c
  89. */
  90. for (j = 0; j < 10; j++) {
  91. ret += intel_sprintf(s, false, buf, ret,
  92. SDW_SHIM_PCMSYCHM(i, j));
  93. ret += intel_sprintf(s, false, buf, ret,
  94. SDW_SHIM_PCMSYCHC(i, j));
  95. }
  96. ret += scnprintf(buf + ret, RD_BUF - ret, "\n IOCTL, CTMCTL\n");
  97. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
  98. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
  99. }
  100. ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
  101. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
  102. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
  103. ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
  104. for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
  105. ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
  106. seq_printf(s_file, "%s", buf);
  107. return 0;
  108. }
  109. DEFINE_SHOW_ATTRIBUTE(intel_reg);
  110. static int intel_set_m_datamode(void *data, u64 value)
  111. {
  112. struct sdw_intel *sdw = data;
  113. struct sdw_bus *bus = &sdw->cdns.bus;
  114. if (value > SDW_PORT_DATA_MODE_STATIC_1)
  115. return -EINVAL;
  116. /* Userspace changed the hardware state behind the kernel's back */
  117. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  118. bus->params.m_data_mode = value;
  119. return 0;
  120. }
  121. DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
  122. intel_set_m_datamode, "%llu\n");
  123. static int intel_set_s_datamode(void *data, u64 value)
  124. {
  125. struct sdw_intel *sdw = data;
  126. struct sdw_bus *bus = &sdw->cdns.bus;
  127. if (value > SDW_PORT_DATA_MODE_STATIC_1)
  128. return -EINVAL;
  129. /* Userspace changed the hardware state behind the kernel's back */
  130. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  131. bus->params.s_data_mode = value;
  132. return 0;
  133. }
  134. DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
  135. intel_set_s_datamode, "%llu\n");
  136. static void intel_debugfs_init(struct sdw_intel *sdw)
  137. {
  138. struct dentry *root = sdw->cdns.bus.debugfs;
  139. if (!root)
  140. return;
  141. sdw->debugfs = debugfs_create_dir("intel-sdw", root);
  142. debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
  143. &intel_reg_fops);
  144. debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
  145. &intel_set_m_datamode_fops);
  146. debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
  147. &intel_set_s_datamode_fops);
  148. sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
  149. }
  150. static void intel_debugfs_exit(struct sdw_intel *sdw)
  151. {
  152. debugfs_remove_recursive(sdw->debugfs);
  153. }
  154. #else
  155. static void intel_debugfs_init(struct sdw_intel *sdw) {}
  156. static void intel_debugfs_exit(struct sdw_intel *sdw) {}
  157. #endif /* CONFIG_DEBUG_FS */
  158. /*
  159. * shim ops
  160. */
  161. /* this needs to be called with shim_lock */
  162. static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
  163. {
  164. void __iomem *shim = sdw->link_res->shim;
  165. unsigned int link_id = sdw->instance;
  166. u16 ioctl;
  167. /* Switch to MIP from Glue logic */
  168. ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
  169. ioctl &= ~(SDW_SHIM_IOCTL_DOE);
  170. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  171. usleep_range(10, 15);
  172. ioctl &= ~(SDW_SHIM_IOCTL_DO);
  173. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  174. usleep_range(10, 15);
  175. ioctl |= (SDW_SHIM_IOCTL_MIF);
  176. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  177. usleep_range(10, 15);
  178. ioctl &= ~(SDW_SHIM_IOCTL_BKE);
  179. ioctl &= ~(SDW_SHIM_IOCTL_COE);
  180. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  181. usleep_range(10, 15);
  182. /* at this point Master IP has full control of the I/Os */
  183. }
  184. /* this needs to be called with shim_lock */
  185. static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
  186. {
  187. unsigned int link_id = sdw->instance;
  188. void __iomem *shim = sdw->link_res->shim;
  189. u16 ioctl;
  190. /* Glue logic */
  191. ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
  192. ioctl |= SDW_SHIM_IOCTL_BKE;
  193. ioctl |= SDW_SHIM_IOCTL_COE;
  194. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  195. usleep_range(10, 15);
  196. ioctl &= ~(SDW_SHIM_IOCTL_MIF);
  197. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  198. usleep_range(10, 15);
  199. /* at this point Integration Glue has full control of the I/Os */
  200. }
  201. /* this needs to be called with shim_lock */
  202. static void intel_shim_init(struct sdw_intel *sdw)
  203. {
  204. void __iomem *shim = sdw->link_res->shim;
  205. unsigned int link_id = sdw->instance;
  206. u16 ioctl = 0, act;
  207. /* Initialize Shim */
  208. ioctl |= SDW_SHIM_IOCTL_BKE;
  209. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  210. usleep_range(10, 15);
  211. ioctl |= SDW_SHIM_IOCTL_WPDD;
  212. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  213. usleep_range(10, 15);
  214. ioctl |= SDW_SHIM_IOCTL_DO;
  215. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  216. usleep_range(10, 15);
  217. ioctl |= SDW_SHIM_IOCTL_DOE;
  218. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  219. usleep_range(10, 15);
  220. intel_shim_glue_to_master_ip(sdw);
  221. act = intel_readw(shim, SDW_SHIM_CTMCTL(link_id));
  222. u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
  223. act |= SDW_SHIM_CTMCTL_DACTQE;
  224. act |= SDW_SHIM_CTMCTL_DODS;
  225. intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
  226. usleep_range(10, 15);
  227. }
  228. static int intel_shim_check_wake(struct sdw_intel *sdw)
  229. {
  230. void __iomem *shim;
  231. u16 wake_sts;
  232. shim = sdw->link_res->shim;
  233. wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
  234. return wake_sts & BIT(sdw->instance);
  235. }
  236. static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
  237. {
  238. void __iomem *shim = sdw->link_res->shim;
  239. unsigned int link_id = sdw->instance;
  240. u16 wake_en, wake_sts;
  241. mutex_lock(sdw->link_res->shim_lock);
  242. wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
  243. if (wake_enable) {
  244. /* Enable the wakeup */
  245. wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
  246. intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
  247. } else {
  248. /* Disable the wake up interrupt */
  249. wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
  250. intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
  251. /* Clear wake status */
  252. wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
  253. wake_sts |= (SDW_SHIM_WAKESTS_STATUS << link_id);
  254. intel_writew(shim, SDW_SHIM_WAKESTS, wake_sts);
  255. }
  256. mutex_unlock(sdw->link_res->shim_lock);
  257. }
  258. static bool intel_check_cmdsync_unlocked(struct sdw_intel *sdw)
  259. {
  260. void __iomem *shim = sdw->link_res->shim;
  261. int sync_reg;
  262. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  263. return !!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK);
  264. }
  265. static int intel_link_power_up(struct sdw_intel *sdw)
  266. {
  267. unsigned int link_id = sdw->instance;
  268. void __iomem *shim = sdw->link_res->shim;
  269. u32 *shim_mask = sdw->link_res->shim_mask;
  270. struct sdw_bus *bus = &sdw->cdns.bus;
  271. struct sdw_master_prop *prop = &bus->prop;
  272. u32 spa_mask, cpa_mask;
  273. u32 link_control;
  274. int ret = 0;
  275. u32 clock_source;
  276. u32 syncprd;
  277. u32 sync_reg;
  278. bool lcap_mlcs;
  279. mutex_lock(sdw->link_res->shim_lock);
  280. /*
  281. * The hardware relies on an internal counter, typically 4kHz,
  282. * to generate the SoundWire SSP - which defines a 'safe'
  283. * synchronization point between commands and audio transport
  284. * and allows for multi link synchronization. The SYNCPRD value
  285. * is only dependent on the oscillator clock provided to
  286. * the IP, so adjust based on _DSD properties reported in DSDT
  287. * tables. The values reported are based on either 24MHz
  288. * (CNL/CML) or 38.4 MHz (ICL/TGL+). On MeteorLake additional
  289. * frequencies are available with the MLCS clock source selection.
  290. */
  291. lcap_mlcs = intel_readl(shim, SDW_SHIM_LCAP) & SDW_SHIM_LCAP_MLCS_MASK;
  292. if (prop->mclk_freq % 6000000) {
  293. if (prop->mclk_freq % 2400000) {
  294. if (lcap_mlcs) {
  295. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24_576;
  296. clock_source = SDW_SHIM_MLCS_CARDINAL_CLK;
  297. } else {
  298. dev_err(sdw->cdns.dev, "%s: invalid clock configuration, mclk %d lcap_mlcs %d\n",
  299. __func__, prop->mclk_freq, lcap_mlcs);
  300. ret = -EINVAL;
  301. goto out;
  302. }
  303. } else {
  304. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
  305. clock_source = SDW_SHIM_MLCS_XTAL_CLK;
  306. }
  307. } else {
  308. if (lcap_mlcs) {
  309. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_96;
  310. clock_source = SDW_SHIM_MLCS_AUDIO_PLL_CLK;
  311. } else {
  312. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
  313. clock_source = SDW_SHIM_MLCS_XTAL_CLK;
  314. }
  315. }
  316. if (!*shim_mask) {
  317. dev_dbg(sdw->cdns.dev, "powering up all links\n");
  318. /* we first need to program the SyncPRD/CPU registers */
  319. dev_dbg(sdw->cdns.dev,
  320. "first link up, programming SYNCPRD\n");
  321. /* set SyncPRD period */
  322. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  323. u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
  324. /* Set SyncCPU bit */
  325. sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
  326. intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
  327. /* Link power up sequence */
  328. link_control = intel_readl(shim, SDW_SHIM_LCTL);
  329. /* only power-up enabled links */
  330. spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
  331. cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
  332. link_control |= spa_mask;
  333. ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
  334. if (ret < 0) {
  335. dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
  336. goto out;
  337. }
  338. /* SyncCPU will change once link is active */
  339. ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
  340. SDW_SHIM_SYNC_SYNCCPU, 0);
  341. if (ret < 0) {
  342. dev_err(sdw->cdns.dev,
  343. "Failed to set SHIM_SYNC: %d\n", ret);
  344. goto out;
  345. }
  346. /* update link clock if needed */
  347. if (lcap_mlcs) {
  348. link_control = intel_readl(shim, SDW_SHIM_LCTL);
  349. u32p_replace_bits(&link_control, clock_source, SDW_SHIM_LCTL_MLCS_MASK);
  350. intel_writel(shim, SDW_SHIM_LCTL, link_control);
  351. }
  352. }
  353. *shim_mask |= BIT(link_id);
  354. sdw->cdns.link_up = true;
  355. intel_shim_init(sdw);
  356. out:
  357. mutex_unlock(sdw->link_res->shim_lock);
  358. return ret;
  359. }
  360. static int intel_link_power_down(struct sdw_intel *sdw)
  361. {
  362. u32 link_control, spa_mask, cpa_mask;
  363. unsigned int link_id = sdw->instance;
  364. void __iomem *shim = sdw->link_res->shim;
  365. u32 *shim_mask = sdw->link_res->shim_mask;
  366. int ret = 0;
  367. mutex_lock(sdw->link_res->shim_lock);
  368. if (!(*shim_mask & BIT(link_id)))
  369. dev_err(sdw->cdns.dev,
  370. "%s: Unbalanced power-up/down calls\n", __func__);
  371. sdw->cdns.link_up = false;
  372. intel_shim_master_ip_to_glue(sdw);
  373. *shim_mask &= ~BIT(link_id);
  374. if (!*shim_mask) {
  375. dev_dbg(sdw->cdns.dev, "powering down all links\n");
  376. /* Link power down sequence */
  377. link_control = intel_readl(shim, SDW_SHIM_LCTL);
  378. /* only power-down enabled links */
  379. spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
  380. cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
  381. link_control &= spa_mask;
  382. ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
  383. if (ret < 0) {
  384. dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
  385. /*
  386. * we leave the sdw->cdns.link_up flag as false since we've disabled
  387. * the link at this point and cannot handle interrupts any longer.
  388. */
  389. }
  390. }
  391. mutex_unlock(sdw->link_res->shim_lock);
  392. return ret;
  393. }
  394. static void intel_shim_sync_arm(struct sdw_intel *sdw)
  395. {
  396. void __iomem *shim = sdw->link_res->shim;
  397. u32 sync_reg;
  398. mutex_lock(sdw->link_res->shim_lock);
  399. /* update SYNC register */
  400. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  401. sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
  402. intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
  403. mutex_unlock(sdw->link_res->shim_lock);
  404. }
  405. static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
  406. {
  407. void __iomem *shim = sdw->link_res->shim;
  408. u32 sync_reg;
  409. /* Read SYNC register */
  410. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  411. /*
  412. * Set SyncGO bit to synchronously trigger a bank switch for
  413. * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
  414. * the Masters.
  415. */
  416. sync_reg |= SDW_SHIM_SYNC_SYNCGO;
  417. intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
  418. return 0;
  419. }
  420. static int intel_shim_sync_go(struct sdw_intel *sdw)
  421. {
  422. int ret;
  423. mutex_lock(sdw->link_res->shim_lock);
  424. ret = intel_shim_sync_go_unlocked(sdw);
  425. mutex_unlock(sdw->link_res->shim_lock);
  426. return ret;
  427. }
  428. /*
  429. * PDI routines
  430. */
  431. static void intel_pdi_init(struct sdw_intel *sdw,
  432. struct sdw_cdns_stream_config *config)
  433. {
  434. void __iomem *shim = sdw->link_res->shim;
  435. unsigned int link_id = sdw->instance;
  436. int pcm_cap;
  437. /* PCM Stream Capability */
  438. pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
  439. config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
  440. config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
  441. config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
  442. dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
  443. config->pcm_bd, config->pcm_in, config->pcm_out);
  444. }
  445. static int
  446. intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
  447. {
  448. void __iomem *shim = sdw->link_res->shim;
  449. unsigned int link_id = sdw->instance;
  450. int count;
  451. count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
  452. /*
  453. * WORKAROUND: on all existing Intel controllers, pdi
  454. * number 2 reports channel count as 1 even though it
  455. * supports 8 channels. Performing hardcoding for pdi
  456. * number 2.
  457. */
  458. if (pdi_num == 2)
  459. count = 7;
  460. /* zero based values for channel count in register */
  461. count++;
  462. return count;
  463. }
  464. static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
  465. struct sdw_cdns_pdi *pdi,
  466. unsigned int num_pdi,
  467. unsigned int *num_ch)
  468. {
  469. int i, ch_count = 0;
  470. for (i = 0; i < num_pdi; i++) {
  471. pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
  472. ch_count += pdi->ch_count;
  473. pdi++;
  474. }
  475. *num_ch = ch_count;
  476. return 0;
  477. }
  478. static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
  479. struct sdw_cdns_streams *stream)
  480. {
  481. intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
  482. &stream->num_ch_bd);
  483. intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
  484. &stream->num_ch_in);
  485. intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
  486. &stream->num_ch_out);
  487. return 0;
  488. }
  489. static void
  490. intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
  491. {
  492. void __iomem *shim = sdw->link_res->shim;
  493. unsigned int link_id = sdw->instance;
  494. int pdi_conf = 0;
  495. /* the Bulk and PCM streams are not contiguous */
  496. pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
  497. if (pdi->num >= 2)
  498. pdi->intel_alh_id += 2;
  499. /*
  500. * Program stream parameters to stream SHIM register
  501. * This is applicable for PCM stream only.
  502. */
  503. if (pdi->type != SDW_STREAM_PCM)
  504. return;
  505. if (pdi->dir == SDW_DATA_DIR_RX)
  506. pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
  507. else
  508. pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
  509. u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
  510. u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
  511. u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
  512. intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
  513. }
  514. static void
  515. intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
  516. {
  517. void __iomem *alh = sdw->link_res->alh;
  518. unsigned int link_id = sdw->instance;
  519. unsigned int conf;
  520. /* the Bulk and PCM streams are not contiguous */
  521. pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
  522. if (pdi->num >= 2)
  523. pdi->intel_alh_id += 2;
  524. /* Program Stream config ALH register */
  525. conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
  526. u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
  527. u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
  528. intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
  529. }
  530. static int intel_params_stream(struct sdw_intel *sdw,
  531. struct snd_pcm_substream *substream,
  532. struct snd_soc_dai *dai,
  533. struct snd_pcm_hw_params *hw_params,
  534. int link_id, int alh_stream_id)
  535. {
  536. struct sdw_intel_link_res *res = sdw->link_res;
  537. struct sdw_intel_stream_params_data params_data;
  538. params_data.substream = substream;
  539. params_data.dai = dai;
  540. params_data.hw_params = hw_params;
  541. params_data.link_id = link_id;
  542. params_data.alh_stream_id = alh_stream_id;
  543. if (res->ops && res->ops->params_stream && res->dev)
  544. return res->ops->params_stream(res->dev,
  545. &params_data);
  546. return -EIO;
  547. }
  548. /*
  549. * DAI routines
  550. */
  551. static int intel_free_stream(struct sdw_intel *sdw,
  552. struct snd_pcm_substream *substream,
  553. struct snd_soc_dai *dai,
  554. int link_id)
  555. {
  556. struct sdw_intel_link_res *res = sdw->link_res;
  557. struct sdw_intel_stream_free_data free_data;
  558. free_data.substream = substream;
  559. free_data.dai = dai;
  560. free_data.link_id = link_id;
  561. if (res->ops && res->ops->free_stream && res->dev)
  562. return res->ops->free_stream(res->dev, &free_data);
  563. return 0;
  564. }
  565. static int intel_hw_params(struct snd_pcm_substream *substream,
  566. struct snd_pcm_hw_params *params,
  567. struct snd_soc_dai *dai)
  568. {
  569. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  570. struct sdw_intel *sdw = cdns_to_intel(cdns);
  571. struct sdw_cdns_dai_runtime *dai_runtime;
  572. struct sdw_cdns_pdi *pdi;
  573. struct sdw_stream_config sconfig;
  574. int ch, dir;
  575. int ret;
  576. dai_runtime = cdns->dai_runtime_array[dai->id];
  577. if (!dai_runtime)
  578. return -EIO;
  579. ch = params_channels(params);
  580. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  581. dir = SDW_DATA_DIR_RX;
  582. else
  583. dir = SDW_DATA_DIR_TX;
  584. pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
  585. if (!pdi)
  586. return -EINVAL;
  587. /* do run-time configurations for SHIM, ALH and PDI/PORT */
  588. intel_pdi_shim_configure(sdw, pdi);
  589. intel_pdi_alh_configure(sdw, pdi);
  590. sdw_cdns_config_stream(cdns, ch, dir, pdi);
  591. /* store pdi and hw_params, may be needed in prepare step */
  592. dai_runtime->paused = false;
  593. dai_runtime->suspended = false;
  594. dai_runtime->pdi = pdi;
  595. /* Inform DSP about PDI stream number */
  596. ret = intel_params_stream(sdw, substream, dai, params,
  597. sdw->instance,
  598. pdi->intel_alh_id);
  599. if (ret)
  600. return ret;
  601. sconfig.direction = dir;
  602. sconfig.ch_count = ch;
  603. sconfig.frame_rate = params_rate(params);
  604. sconfig.type = dai_runtime->stream_type;
  605. sconfig.bps = snd_pcm_format_width(params_format(params));
  606. /* Port configuration */
  607. struct sdw_port_config *pconfig __free(kfree) = kzalloc(sizeof(*pconfig),
  608. GFP_KERNEL);
  609. if (!pconfig)
  610. return -ENOMEM;
  611. pconfig->num = pdi->num;
  612. pconfig->ch_mask = (1 << ch) - 1;
  613. ret = sdw_stream_add_master(&cdns->bus, &sconfig,
  614. pconfig, 1, dai_runtime->stream);
  615. if (ret)
  616. dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
  617. return ret;
  618. }
  619. static int intel_prepare(struct snd_pcm_substream *substream,
  620. struct snd_soc_dai *dai)
  621. {
  622. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  623. struct sdw_intel *sdw = cdns_to_intel(cdns);
  624. struct sdw_cdns_dai_runtime *dai_runtime;
  625. int ch, dir;
  626. int ret = 0;
  627. dai_runtime = cdns->dai_runtime_array[dai->id];
  628. if (!dai_runtime) {
  629. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  630. __func__);
  631. return -EIO;
  632. }
  633. if (dai_runtime->suspended) {
  634. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  635. struct snd_pcm_hw_params *hw_params;
  636. hw_params = &rtd->dpcm[substream->stream].hw_params;
  637. dai_runtime->suspended = false;
  638. /*
  639. * .prepare() is called after system resume, where we
  640. * need to reinitialize the SHIM/ALH/Cadence IP.
  641. * .prepare() is also called to deal with underflows,
  642. * but in those cases we cannot touch ALH/SHIM
  643. * registers
  644. */
  645. /* configure stream */
  646. ch = params_channels(hw_params);
  647. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  648. dir = SDW_DATA_DIR_RX;
  649. else
  650. dir = SDW_DATA_DIR_TX;
  651. intel_pdi_shim_configure(sdw, dai_runtime->pdi);
  652. intel_pdi_alh_configure(sdw, dai_runtime->pdi);
  653. sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
  654. /* Inform DSP about PDI stream number */
  655. ret = intel_params_stream(sdw, substream, dai,
  656. hw_params,
  657. sdw->instance,
  658. dai_runtime->pdi->intel_alh_id);
  659. }
  660. return ret;
  661. }
  662. static int
  663. intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  664. {
  665. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  666. struct sdw_intel *sdw = cdns_to_intel(cdns);
  667. struct sdw_cdns_dai_runtime *dai_runtime;
  668. int ret;
  669. dai_runtime = cdns->dai_runtime_array[dai->id];
  670. if (!dai_runtime)
  671. return -EIO;
  672. /*
  673. * The sdw stream state will transition to RELEASED when stream->
  674. * master_list is empty. So the stream state will transition to
  675. * DEPREPARED for the first cpu-dai and to RELEASED for the last
  676. * cpu-dai.
  677. */
  678. ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
  679. if (ret < 0) {
  680. dev_err(dai->dev, "remove master from stream %s failed: %d\n",
  681. dai_runtime->stream->name, ret);
  682. return ret;
  683. }
  684. ret = intel_free_stream(sdw, substream, dai, sdw->instance);
  685. if (ret < 0) {
  686. dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
  687. return ret;
  688. }
  689. dai_runtime->pdi = NULL;
  690. return 0;
  691. }
  692. static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
  693. void *stream, int direction)
  694. {
  695. return cdns_set_sdw_stream(dai, stream, direction);
  696. }
  697. static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
  698. int direction)
  699. {
  700. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  701. struct sdw_cdns_dai_runtime *dai_runtime;
  702. dai_runtime = cdns->dai_runtime_array[dai->id];
  703. if (!dai_runtime)
  704. return ERR_PTR(-EINVAL);
  705. return dai_runtime->stream;
  706. }
  707. static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
  708. {
  709. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  710. struct sdw_cdns_dai_runtime *dai_runtime;
  711. int ret = 0;
  712. dai_runtime = cdns->dai_runtime_array[dai->id];
  713. if (!dai_runtime) {
  714. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  715. __func__);
  716. return -EIO;
  717. }
  718. switch (cmd) {
  719. case SNDRV_PCM_TRIGGER_SUSPEND:
  720. /*
  721. * The .prepare callback is used to deal with xruns and resume operations.
  722. * In the case of xruns, the DMAs and SHIM registers cannot be touched,
  723. * but for resume operations the DMAs and SHIM registers need to be initialized.
  724. * the .trigger callback is used to track the suspend case only.
  725. */
  726. dai_runtime->suspended = true;
  727. break;
  728. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  729. dai_runtime->paused = true;
  730. break;
  731. case SNDRV_PCM_TRIGGER_STOP:
  732. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  733. dai_runtime->paused = false;
  734. break;
  735. default:
  736. break;
  737. }
  738. return ret;
  739. }
  740. static int intel_component_probe(struct snd_soc_component *component)
  741. {
  742. int ret;
  743. /*
  744. * make sure the device is pm_runtime_active before initiating
  745. * bus transactions during the card registration.
  746. * We use pm_runtime_resume() here, without taking a reference
  747. * and releasing it immediately.
  748. */
  749. ret = pm_runtime_resume(component->dev);
  750. if (ret < 0 && ret != -EACCES)
  751. return ret;
  752. return 0;
  753. }
  754. static int intel_component_dais_suspend(struct snd_soc_component *component)
  755. {
  756. struct snd_soc_dai *dai;
  757. /*
  758. * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
  759. * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
  760. * Since the component suspend is called last, we can trap this corner case
  761. * and force the DAIs to release their resources.
  762. */
  763. for_each_component_dais(component, dai) {
  764. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  765. struct sdw_cdns_dai_runtime *dai_runtime;
  766. dai_runtime = cdns->dai_runtime_array[dai->id];
  767. if (!dai_runtime)
  768. continue;
  769. if (dai_runtime->suspended)
  770. continue;
  771. if (dai_runtime->paused)
  772. dai_runtime->suspended = true;
  773. }
  774. return 0;
  775. }
  776. static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
  777. .hw_params = intel_hw_params,
  778. .prepare = intel_prepare,
  779. .hw_free = intel_hw_free,
  780. .trigger = intel_trigger,
  781. .set_stream = intel_pcm_set_sdw_stream,
  782. .get_stream = intel_get_sdw_stream,
  783. };
  784. static const struct snd_soc_component_driver dai_component = {
  785. .name = "soundwire",
  786. .probe = intel_component_probe,
  787. .suspend = intel_component_dais_suspend,
  788. .legacy_dai_naming = 1,
  789. };
  790. static int intel_create_dai(struct sdw_cdns *cdns,
  791. struct snd_soc_dai_driver *dais,
  792. enum intel_pdi_type type,
  793. u32 num, u32 off, u32 max_ch)
  794. {
  795. int i;
  796. if (num == 0)
  797. return 0;
  798. for (i = off; i < (off + num); i++) {
  799. dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
  800. "SDW%d Pin%d",
  801. cdns->instance, i);
  802. if (!dais[i].name)
  803. return -ENOMEM;
  804. if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
  805. dais[i].playback.channels_min = 1;
  806. dais[i].playback.channels_max = max_ch;
  807. }
  808. if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
  809. dais[i].capture.channels_min = 1;
  810. dais[i].capture.channels_max = max_ch;
  811. }
  812. dais[i].ops = &intel_pcm_dai_ops;
  813. }
  814. return 0;
  815. }
  816. static int intel_register_dai(struct sdw_intel *sdw)
  817. {
  818. struct sdw_cdns_dai_runtime **dai_runtime_array;
  819. struct sdw_cdns_stream_config config;
  820. struct sdw_cdns *cdns = &sdw->cdns;
  821. struct sdw_cdns_streams *stream;
  822. struct snd_soc_dai_driver *dais;
  823. int num_dai, ret, off = 0;
  824. /* Read the PDI config and initialize cadence PDI */
  825. intel_pdi_init(sdw, &config);
  826. ret = sdw_cdns_pdi_init(cdns, config);
  827. if (ret)
  828. return ret;
  829. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
  830. /* DAIs are created based on total number of PDIs supported */
  831. num_dai = cdns->pcm.num_pdi;
  832. dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
  833. sizeof(struct sdw_cdns_dai_runtime *),
  834. GFP_KERNEL);
  835. if (!dai_runtime_array)
  836. return -ENOMEM;
  837. cdns->dai_runtime_array = dai_runtime_array;
  838. dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
  839. if (!dais)
  840. return -ENOMEM;
  841. /* Create PCM DAIs */
  842. stream = &cdns->pcm;
  843. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
  844. off, stream->num_ch_in);
  845. if (ret)
  846. return ret;
  847. off += cdns->pcm.num_in;
  848. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
  849. off, stream->num_ch_out);
  850. if (ret)
  851. return ret;
  852. off += cdns->pcm.num_out;
  853. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
  854. off, stream->num_ch_bd);
  855. if (ret)
  856. return ret;
  857. return devm_snd_soc_register_component(cdns->dev, &dai_component,
  858. dais, num_dai);
  859. }
  860. const struct sdw_intel_hw_ops sdw_intel_cnl_hw_ops = {
  861. .debugfs_init = intel_debugfs_init,
  862. .debugfs_exit = intel_debugfs_exit,
  863. .register_dai = intel_register_dai,
  864. .check_clock_stop = intel_check_clock_stop,
  865. .start_bus = intel_start_bus,
  866. .start_bus_after_reset = intel_start_bus_after_reset,
  867. .start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
  868. .stop_bus = intel_stop_bus,
  869. .link_power_up = intel_link_power_up,
  870. .link_power_down = intel_link_power_down,
  871. .shim_check_wake = intel_shim_check_wake,
  872. .shim_wake = intel_shim_wake,
  873. .pre_bank_switch = intel_pre_bank_switch,
  874. .post_bank_switch = intel_post_bank_switch,
  875. .sync_arm = intel_shim_sync_arm,
  876. .sync_go_unlocked = intel_shim_sync_go_unlocked,
  877. .sync_go = intel_shim_sync_go,
  878. .sync_check_cmdsync_unlocked = intel_check_cmdsync_unlocked,
  879. };
  880. EXPORT_SYMBOL_NS(sdw_intel_cnl_hw_ops, SOUNDWIRE_INTEL);