intel.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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_obj(*pconfig);
  608. if (!pconfig)
  609. return -ENOMEM;
  610. pconfig->num = pdi->num;
  611. pconfig->ch_mask = (1 << ch) - 1;
  612. ret = sdw_stream_add_master(&cdns->bus, &sconfig,
  613. pconfig, 1, dai_runtime->stream);
  614. if (ret)
  615. dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
  616. return ret;
  617. }
  618. static int intel_prepare(struct snd_pcm_substream *substream,
  619. struct snd_soc_dai *dai)
  620. {
  621. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  622. struct sdw_intel *sdw = cdns_to_intel(cdns);
  623. struct sdw_cdns_dai_runtime *dai_runtime;
  624. int ch, dir;
  625. int ret = 0;
  626. dai_runtime = cdns->dai_runtime_array[dai->id];
  627. if (!dai_runtime) {
  628. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  629. __func__);
  630. return -EIO;
  631. }
  632. if (dai_runtime->suspended) {
  633. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  634. struct snd_pcm_hw_params *hw_params;
  635. hw_params = &rtd->dpcm[substream->stream].hw_params;
  636. dai_runtime->suspended = false;
  637. /*
  638. * .prepare() is called after system resume, where we
  639. * need to reinitialize the SHIM/ALH/Cadence IP.
  640. * .prepare() is also called to deal with underflows,
  641. * but in those cases we cannot touch ALH/SHIM
  642. * registers
  643. */
  644. /* configure stream */
  645. ch = params_channels(hw_params);
  646. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  647. dir = SDW_DATA_DIR_RX;
  648. else
  649. dir = SDW_DATA_DIR_TX;
  650. intel_pdi_shim_configure(sdw, dai_runtime->pdi);
  651. intel_pdi_alh_configure(sdw, dai_runtime->pdi);
  652. sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
  653. /* Inform DSP about PDI stream number */
  654. ret = intel_params_stream(sdw, substream, dai,
  655. hw_params,
  656. sdw->instance,
  657. dai_runtime->pdi->intel_alh_id);
  658. }
  659. return ret;
  660. }
  661. static int
  662. intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  663. {
  664. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  665. struct sdw_intel *sdw = cdns_to_intel(cdns);
  666. struct sdw_cdns_dai_runtime *dai_runtime;
  667. int ret;
  668. dai_runtime = cdns->dai_runtime_array[dai->id];
  669. if (!dai_runtime)
  670. return -EIO;
  671. /*
  672. * The sdw stream state will transition to RELEASED when stream->
  673. * master_list is empty. So the stream state will transition to
  674. * DEPREPARED for the first cpu-dai and to RELEASED for the last
  675. * cpu-dai.
  676. */
  677. ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
  678. if (ret < 0) {
  679. dev_err(dai->dev, "remove master from stream %s failed: %d\n",
  680. dai_runtime->stream->name, ret);
  681. return ret;
  682. }
  683. ret = intel_free_stream(sdw, substream, dai, sdw->instance);
  684. if (ret < 0) {
  685. dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
  686. return ret;
  687. }
  688. dai_runtime->pdi = NULL;
  689. return 0;
  690. }
  691. static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
  692. void *stream, int direction)
  693. {
  694. return cdns_set_sdw_stream(dai, stream, direction);
  695. }
  696. static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
  697. int direction)
  698. {
  699. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  700. struct sdw_cdns_dai_runtime *dai_runtime;
  701. dai_runtime = cdns->dai_runtime_array[dai->id];
  702. if (!dai_runtime)
  703. return ERR_PTR(-EINVAL);
  704. return dai_runtime->stream;
  705. }
  706. static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
  707. {
  708. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  709. struct sdw_cdns_dai_runtime *dai_runtime;
  710. int ret = 0;
  711. dai_runtime = cdns->dai_runtime_array[dai->id];
  712. if (!dai_runtime) {
  713. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  714. __func__);
  715. return -EIO;
  716. }
  717. switch (cmd) {
  718. case SNDRV_PCM_TRIGGER_SUSPEND:
  719. /*
  720. * The .prepare callback is used to deal with xruns and resume operations.
  721. * In the case of xruns, the DMAs and SHIM registers cannot be touched,
  722. * but for resume operations the DMAs and SHIM registers need to be initialized.
  723. * the .trigger callback is used to track the suspend case only.
  724. */
  725. dai_runtime->suspended = true;
  726. break;
  727. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  728. dai_runtime->paused = true;
  729. break;
  730. case SNDRV_PCM_TRIGGER_STOP:
  731. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  732. dai_runtime->paused = false;
  733. break;
  734. default:
  735. break;
  736. }
  737. return ret;
  738. }
  739. static int intel_component_probe(struct snd_soc_component *component)
  740. {
  741. int ret;
  742. /*
  743. * make sure the device is pm_runtime_active before initiating
  744. * bus transactions during the card registration.
  745. * We use pm_runtime_resume() here, without taking a reference
  746. * and releasing it immediately.
  747. */
  748. ret = pm_runtime_resume(component->dev);
  749. if (ret < 0 && ret != -EACCES)
  750. return ret;
  751. return 0;
  752. }
  753. static int intel_component_dais_suspend(struct snd_soc_component *component)
  754. {
  755. struct snd_soc_dai *dai;
  756. /*
  757. * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
  758. * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
  759. * Since the component suspend is called last, we can trap this corner case
  760. * and force the DAIs to release their resources.
  761. */
  762. for_each_component_dais(component, dai) {
  763. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  764. struct sdw_cdns_dai_runtime *dai_runtime;
  765. dai_runtime = cdns->dai_runtime_array[dai->id];
  766. if (!dai_runtime)
  767. continue;
  768. if (dai_runtime->suspended)
  769. continue;
  770. if (dai_runtime->paused)
  771. dai_runtime->suspended = true;
  772. }
  773. return 0;
  774. }
  775. static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
  776. .hw_params = intel_hw_params,
  777. .prepare = intel_prepare,
  778. .hw_free = intel_hw_free,
  779. .trigger = intel_trigger,
  780. .set_stream = intel_pcm_set_sdw_stream,
  781. .get_stream = intel_get_sdw_stream,
  782. };
  783. static const struct snd_soc_component_driver dai_component = {
  784. .name = "soundwire",
  785. .probe = intel_component_probe,
  786. .suspend = intel_component_dais_suspend,
  787. .legacy_dai_naming = 1,
  788. };
  789. static int intel_create_dai(struct sdw_cdns *cdns,
  790. struct snd_soc_dai_driver *dais,
  791. enum intel_pdi_type type,
  792. u32 num, u32 off, u32 max_ch)
  793. {
  794. int i;
  795. if (num == 0)
  796. return 0;
  797. for (i = off; i < (off + num); i++) {
  798. dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
  799. "SDW%d Pin%d",
  800. cdns->instance, i);
  801. if (!dais[i].name)
  802. return -ENOMEM;
  803. if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
  804. dais[i].playback.channels_min = 1;
  805. dais[i].playback.channels_max = max_ch;
  806. }
  807. if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
  808. dais[i].capture.channels_min = 1;
  809. dais[i].capture.channels_max = max_ch;
  810. }
  811. dais[i].ops = &intel_pcm_dai_ops;
  812. }
  813. return 0;
  814. }
  815. static int intel_register_dai(struct sdw_intel *sdw)
  816. {
  817. struct sdw_cdns_dai_runtime **dai_runtime_array;
  818. struct sdw_cdns_stream_config config;
  819. struct sdw_cdns *cdns = &sdw->cdns;
  820. struct sdw_cdns_streams *stream;
  821. struct snd_soc_dai_driver *dais;
  822. int num_dai, ret, off = 0;
  823. /* Read the PDI config and initialize cadence PDI */
  824. intel_pdi_init(sdw, &config);
  825. ret = sdw_cdns_pdi_init(cdns, config);
  826. if (ret)
  827. return ret;
  828. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
  829. /* DAIs are created based on total number of PDIs supported */
  830. num_dai = cdns->pcm.num_pdi;
  831. dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
  832. sizeof(struct sdw_cdns_dai_runtime *),
  833. GFP_KERNEL);
  834. if (!dai_runtime_array)
  835. return -ENOMEM;
  836. cdns->dai_runtime_array = dai_runtime_array;
  837. dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
  838. if (!dais)
  839. return -ENOMEM;
  840. /* Create PCM DAIs */
  841. stream = &cdns->pcm;
  842. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
  843. off, stream->num_ch_in);
  844. if (ret)
  845. return ret;
  846. off += cdns->pcm.num_in;
  847. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
  848. off, stream->num_ch_out);
  849. if (ret)
  850. return ret;
  851. off += cdns->pcm.num_out;
  852. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
  853. off, stream->num_ch_bd);
  854. if (ret)
  855. return ret;
  856. return devm_snd_soc_register_component(cdns->dev, &dai_component,
  857. dais, num_dai);
  858. }
  859. const struct sdw_intel_hw_ops sdw_intel_cnl_hw_ops = {
  860. .debugfs_init = intel_debugfs_init,
  861. .debugfs_exit = intel_debugfs_exit,
  862. .register_dai = intel_register_dai,
  863. .check_clock_stop = intel_check_clock_stop,
  864. .start_bus = intel_start_bus,
  865. .start_bus_after_reset = intel_start_bus_after_reset,
  866. .start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
  867. .stop_bus = intel_stop_bus,
  868. .link_power_up = intel_link_power_up,
  869. .link_power_down = intel_link_power_down,
  870. .shim_check_wake = intel_shim_check_wake,
  871. .shim_wake = intel_shim_wake,
  872. .pre_bank_switch = intel_pre_bank_switch,
  873. .post_bank_switch = intel_post_bank_switch,
  874. .sync_arm = intel_shim_sync_arm,
  875. .sync_go_unlocked = intel_shim_sync_go_unlocked,
  876. .sync_go = intel_shim_sync_go,
  877. .sync_check_cmdsync_unlocked = intel_check_cmdsync_unlocked,
  878. };
  879. EXPORT_SYMBOL_NS(sdw_intel_cnl_hw_ops, "SOUNDWIRE_INTEL");