intel_ace2x.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. // Copyright(c) 2023 Intel Corporation
  3. /*
  4. * Soundwire Intel ops for LunarLake
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/cleanup.h>
  8. #include <linux/device.h>
  9. #include <linux/soundwire/sdw_registers.h>
  10. #include <linux/soundwire/sdw.h>
  11. #include <linux/soundwire/sdw_intel.h>
  12. #include <sound/hdaudio.h>
  13. #include <sound/hda-mlink.h>
  14. #include <sound/hda_register.h>
  15. #include <sound/pcm_params.h>
  16. #include "cadence_master.h"
  17. #include "bus.h"
  18. #include "intel.h"
  19. /*
  20. * shim vendor-specific (vs) ops
  21. */
  22. static void intel_shim_vs_init(struct sdw_intel *sdw)
  23. {
  24. void __iomem *shim_vs = sdw->link_res->shim_vs;
  25. struct sdw_bus *bus = &sdw->cdns.bus;
  26. struct sdw_intel_prop *intel_prop;
  27. u16 clde;
  28. u16 doaise2;
  29. u16 dodse2;
  30. u16 clds;
  31. u16 clss;
  32. u16 doaise;
  33. u16 doais;
  34. u16 dodse;
  35. u16 dods;
  36. u16 act;
  37. intel_prop = bus->vendor_specific_prop;
  38. clde = intel_prop->clde;
  39. doaise2 = intel_prop->doaise2;
  40. dodse2 = intel_prop->dodse2;
  41. clds = intel_prop->clds;
  42. clss = intel_prop->clss;
  43. doaise = intel_prop->doaise;
  44. doais = intel_prop->doais;
  45. dodse = intel_prop->dodse;
  46. dods = intel_prop->dods;
  47. act = intel_readw(shim_vs, SDW_SHIM2_INTEL_VS_ACTMCTL);
  48. u16p_replace_bits(&act, clde, SDW_SHIM3_INTEL_VS_ACTMCTL_CLDE);
  49. u16p_replace_bits(&act, doaise2, SDW_SHIM3_INTEL_VS_ACTMCTL_DOAISE2);
  50. u16p_replace_bits(&act, dodse2, SDW_SHIM3_INTEL_VS_ACTMCTL_DODSE2);
  51. u16p_replace_bits(&act, clds, SDW_SHIM3_INTEL_VS_ACTMCTL_CLDS);
  52. u16p_replace_bits(&act, clss, SDW_SHIM3_INTEL_VS_ACTMCTL_CLSS);
  53. u16p_replace_bits(&act, doaise, SDW_SHIM2_INTEL_VS_ACTMCTL_DOAISE);
  54. u16p_replace_bits(&act, doais, SDW_SHIM2_INTEL_VS_ACTMCTL_DOAIS);
  55. u16p_replace_bits(&act, dodse, SDW_SHIM2_INTEL_VS_ACTMCTL_DODSE);
  56. u16p_replace_bits(&act, dods, SDW_SHIM2_INTEL_VS_ACTMCTL_DODS);
  57. act |= SDW_SHIM2_INTEL_VS_ACTMCTL_DACTQE;
  58. intel_writew(shim_vs, SDW_SHIM2_INTEL_VS_ACTMCTL, act);
  59. usleep_range(10, 15);
  60. }
  61. static void intel_shim_vs_set_clock_source(struct sdw_intel *sdw, u32 source)
  62. {
  63. void __iomem *shim_vs = sdw->link_res->shim_vs;
  64. u32 val;
  65. val = intel_readl(shim_vs, SDW_SHIM2_INTEL_VS_LVSCTL);
  66. u32p_replace_bits(&val, source, SDW_SHIM2_INTEL_VS_LVSCTL_MLCS);
  67. intel_writel(shim_vs, SDW_SHIM2_INTEL_VS_LVSCTL, val);
  68. dev_dbg(sdw->cdns.dev, "clock source %d LVSCTL %#x\n", source, val);
  69. }
  70. static int intel_shim_check_wake(struct sdw_intel *sdw)
  71. {
  72. /*
  73. * We follow the HDaudio example and resume unconditionally
  74. * without checking the WAKESTS bit for that specific link
  75. */
  76. return 1;
  77. }
  78. static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
  79. {
  80. u16 lsdiid = 0;
  81. u16 wake_en;
  82. u16 wake_sts;
  83. int ret;
  84. mutex_lock(sdw->link_res->shim_lock);
  85. ret = hdac_bus_eml_sdw_get_lsdiid_unlocked(sdw->link_res->hbus, sdw->instance, &lsdiid);
  86. if (ret < 0)
  87. goto unlock;
  88. wake_en = snd_hdac_chip_readw(sdw->link_res->hbus, WAKEEN);
  89. if (wake_enable) {
  90. /* Enable the wakeup */
  91. wake_en |= lsdiid;
  92. snd_hdac_chip_writew(sdw->link_res->hbus, WAKEEN, wake_en);
  93. } else {
  94. /* Disable the wake up interrupt */
  95. wake_en &= ~lsdiid;
  96. snd_hdac_chip_writew(sdw->link_res->hbus, WAKEEN, wake_en);
  97. /* Clear wake status (W1C) */
  98. wake_sts = snd_hdac_chip_readw(sdw->link_res->hbus, STATESTS);
  99. wake_sts |= lsdiid;
  100. snd_hdac_chip_writew(sdw->link_res->hbus, STATESTS, wake_sts);
  101. }
  102. unlock:
  103. mutex_unlock(sdw->link_res->shim_lock);
  104. }
  105. static int intel_link_power_up(struct sdw_intel *sdw)
  106. {
  107. struct sdw_bus *bus = &sdw->cdns.bus;
  108. struct sdw_master_prop *prop = &bus->prop;
  109. u32 *shim_mask = sdw->link_res->shim_mask;
  110. unsigned int link_id = sdw->instance;
  111. u32 clock_source;
  112. u32 syncprd;
  113. int ret;
  114. if (prop->mclk_freq % 6000000) {
  115. if (prop->mclk_freq % 2400000) {
  116. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24_576;
  117. clock_source = SDW_SHIM2_MLCS_CARDINAL_CLK;
  118. } else {
  119. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
  120. clock_source = SDW_SHIM2_MLCS_XTAL_CLK;
  121. }
  122. } else {
  123. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_96;
  124. clock_source = SDW_SHIM2_MLCS_AUDIO_PLL_CLK;
  125. }
  126. mutex_lock(sdw->link_res->shim_lock);
  127. ret = hdac_bus_eml_sdw_power_up_unlocked(sdw->link_res->hbus, link_id);
  128. if (ret < 0) {
  129. dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_power_up failed: %d\n",
  130. __func__, ret);
  131. goto out;
  132. }
  133. intel_shim_vs_set_clock_source(sdw, clock_source);
  134. if (!*shim_mask) {
  135. /* we first need to program the SyncPRD/CPU registers */
  136. dev_dbg(sdw->cdns.dev, "first link up, programming SYNCPRD\n");
  137. ret = hdac_bus_eml_sdw_set_syncprd_unlocked(sdw->link_res->hbus, syncprd);
  138. if (ret < 0) {
  139. dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_set_syncprd failed: %d\n",
  140. __func__, ret);
  141. goto out;
  142. }
  143. /* SYNCPU will change once link is active */
  144. ret = hdac_bus_eml_sdw_wait_syncpu_unlocked(sdw->link_res->hbus);
  145. if (ret < 0) {
  146. dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_wait_syncpu failed: %d\n",
  147. __func__, ret);
  148. goto out;
  149. }
  150. }
  151. *shim_mask |= BIT(link_id);
  152. sdw->cdns.link_up = true;
  153. intel_shim_vs_init(sdw);
  154. out:
  155. mutex_unlock(sdw->link_res->shim_lock);
  156. return ret;
  157. }
  158. static int intel_link_power_down(struct sdw_intel *sdw)
  159. {
  160. u32 *shim_mask = sdw->link_res->shim_mask;
  161. unsigned int link_id = sdw->instance;
  162. int ret;
  163. mutex_lock(sdw->link_res->shim_lock);
  164. sdw->cdns.link_up = false;
  165. *shim_mask &= ~BIT(link_id);
  166. ret = hdac_bus_eml_sdw_power_down_unlocked(sdw->link_res->hbus, link_id);
  167. if (ret < 0) {
  168. dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_power_down failed: %d\n",
  169. __func__, ret);
  170. /*
  171. * we leave the sdw->cdns.link_up flag as false since we've disabled
  172. * the link at this point and cannot handle interrupts any longer.
  173. */
  174. }
  175. mutex_unlock(sdw->link_res->shim_lock);
  176. return ret;
  177. }
  178. static void intel_sync_arm(struct sdw_intel *sdw)
  179. {
  180. unsigned int link_id = sdw->instance;
  181. mutex_lock(sdw->link_res->shim_lock);
  182. hdac_bus_eml_sdw_sync_arm_unlocked(sdw->link_res->hbus, link_id);
  183. mutex_unlock(sdw->link_res->shim_lock);
  184. }
  185. static int intel_sync_go_unlocked(struct sdw_intel *sdw)
  186. {
  187. int ret;
  188. ret = hdac_bus_eml_sdw_sync_go_unlocked(sdw->link_res->hbus);
  189. if (ret < 0)
  190. dev_err(sdw->cdns.dev, "%s: SyncGO clear failed: %d\n", __func__, ret);
  191. return ret;
  192. }
  193. static int intel_sync_go(struct sdw_intel *sdw)
  194. {
  195. int ret;
  196. mutex_lock(sdw->link_res->shim_lock);
  197. ret = intel_sync_go_unlocked(sdw);
  198. mutex_unlock(sdw->link_res->shim_lock);
  199. return ret;
  200. }
  201. static bool intel_check_cmdsync_unlocked(struct sdw_intel *sdw)
  202. {
  203. return hdac_bus_eml_sdw_check_cmdsync_unlocked(sdw->link_res->hbus);
  204. }
  205. /* DAI callbacks */
  206. static int intel_params_stream(struct sdw_intel *sdw,
  207. struct snd_pcm_substream *substream,
  208. struct snd_soc_dai *dai,
  209. struct snd_pcm_hw_params *hw_params,
  210. int link_id, int alh_stream_id)
  211. {
  212. struct sdw_intel_link_res *res = sdw->link_res;
  213. struct sdw_intel_stream_params_data params_data;
  214. params_data.substream = substream;
  215. params_data.dai = dai;
  216. params_data.hw_params = hw_params;
  217. params_data.link_id = link_id;
  218. params_data.alh_stream_id = alh_stream_id;
  219. if (res->ops && res->ops->params_stream && res->dev)
  220. return res->ops->params_stream(res->dev,
  221. &params_data);
  222. return -EIO;
  223. }
  224. static int intel_free_stream(struct sdw_intel *sdw,
  225. struct snd_pcm_substream *substream,
  226. struct snd_soc_dai *dai,
  227. int link_id)
  228. {
  229. struct sdw_intel_link_res *res = sdw->link_res;
  230. struct sdw_intel_stream_free_data free_data;
  231. free_data.substream = substream;
  232. free_data.dai = dai;
  233. free_data.link_id = link_id;
  234. if (res->ops && res->ops->free_stream && res->dev)
  235. return res->ops->free_stream(res->dev,
  236. &free_data);
  237. return 0;
  238. }
  239. /*
  240. * DAI operations
  241. */
  242. static int intel_hw_params(struct snd_pcm_substream *substream,
  243. struct snd_pcm_hw_params *params,
  244. struct snd_soc_dai *dai)
  245. {
  246. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  247. struct sdw_intel *sdw = cdns_to_intel(cdns);
  248. struct sdw_cdns_dai_runtime *dai_runtime;
  249. struct sdw_cdns_pdi *pdi;
  250. struct sdw_stream_config sconfig;
  251. int ch, dir;
  252. int ret;
  253. dai_runtime = cdns->dai_runtime_array[dai->id];
  254. if (!dai_runtime)
  255. return -EIO;
  256. ch = params_channels(params);
  257. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  258. dir = SDW_DATA_DIR_RX;
  259. else
  260. dir = SDW_DATA_DIR_TX;
  261. pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
  262. if (!pdi)
  263. return -EINVAL;
  264. /* use same definitions for alh_id as previous generations */
  265. pdi->intel_alh_id = (sdw->instance * 16) + pdi->num + 3;
  266. if (pdi->num >= 2)
  267. pdi->intel_alh_id += 2;
  268. /* the SHIM will be configured in the callback functions */
  269. sdw_cdns_config_stream(cdns, ch, dir, pdi);
  270. /* store pdi and state, may be needed in prepare step */
  271. dai_runtime->paused = false;
  272. dai_runtime->suspended = false;
  273. dai_runtime->pdi = pdi;
  274. /* Inform DSP about PDI stream number */
  275. ret = intel_params_stream(sdw, substream, dai, params,
  276. sdw->instance,
  277. pdi->intel_alh_id);
  278. if (ret)
  279. return ret;
  280. sconfig.direction = dir;
  281. sconfig.ch_count = ch;
  282. sconfig.frame_rate = params_rate(params);
  283. sconfig.type = dai_runtime->stream_type;
  284. sconfig.bps = snd_pcm_format_width(params_format(params));
  285. /* Port configuration */
  286. struct sdw_port_config *pconfig __free(kfree) = kzalloc(sizeof(*pconfig),
  287. GFP_KERNEL);
  288. if (!pconfig)
  289. return -ENOMEM;
  290. pconfig->num = pdi->num;
  291. pconfig->ch_mask = (1 << ch) - 1;
  292. ret = sdw_stream_add_master(&cdns->bus, &sconfig,
  293. pconfig, 1, dai_runtime->stream);
  294. if (ret)
  295. dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
  296. return ret;
  297. }
  298. static int intel_prepare(struct snd_pcm_substream *substream,
  299. struct snd_soc_dai *dai)
  300. {
  301. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  302. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  303. struct sdw_intel *sdw = cdns_to_intel(cdns);
  304. struct sdw_cdns_dai_runtime *dai_runtime;
  305. struct snd_pcm_hw_params *hw_params;
  306. int ch, dir;
  307. dai_runtime = cdns->dai_runtime_array[dai->id];
  308. if (!dai_runtime) {
  309. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  310. __func__);
  311. return -EIO;
  312. }
  313. hw_params = &rtd->dpcm[substream->stream].hw_params;
  314. if (dai_runtime->suspended) {
  315. dai_runtime->suspended = false;
  316. /*
  317. * .prepare() is called after system resume, where we
  318. * need to reinitialize the SHIM/ALH/Cadence IP.
  319. * .prepare() is also called to deal with underflows,
  320. * but in those cases we cannot touch ALH/SHIM
  321. * registers
  322. */
  323. /* configure stream */
  324. ch = params_channels(hw_params);
  325. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  326. dir = SDW_DATA_DIR_RX;
  327. else
  328. dir = SDW_DATA_DIR_TX;
  329. /* the SHIM will be configured in the callback functions */
  330. sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
  331. }
  332. /* Inform DSP about PDI stream number */
  333. return intel_params_stream(sdw, substream, dai, hw_params, sdw->instance,
  334. dai_runtime->pdi->intel_alh_id);
  335. }
  336. static int
  337. intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  338. {
  339. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  340. struct sdw_intel *sdw = cdns_to_intel(cdns);
  341. struct sdw_cdns_dai_runtime *dai_runtime;
  342. int ret;
  343. dai_runtime = cdns->dai_runtime_array[dai->id];
  344. if (!dai_runtime)
  345. return -EIO;
  346. /*
  347. * The sdw stream state will transition to RELEASED when stream->
  348. * master_list is empty. So the stream state will transition to
  349. * DEPREPARED for the first cpu-dai and to RELEASED for the last
  350. * cpu-dai.
  351. */
  352. ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
  353. if (ret < 0) {
  354. dev_err(dai->dev, "remove master from stream %s failed: %d\n",
  355. dai_runtime->stream->name, ret);
  356. return ret;
  357. }
  358. ret = intel_free_stream(sdw, substream, dai, sdw->instance);
  359. if (ret < 0) {
  360. dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
  361. return ret;
  362. }
  363. dai_runtime->pdi = NULL;
  364. return 0;
  365. }
  366. static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
  367. void *stream, int direction)
  368. {
  369. return cdns_set_sdw_stream(dai, stream, direction);
  370. }
  371. static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
  372. int direction)
  373. {
  374. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  375. struct sdw_cdns_dai_runtime *dai_runtime;
  376. dai_runtime = cdns->dai_runtime_array[dai->id];
  377. if (!dai_runtime)
  378. return ERR_PTR(-EINVAL);
  379. return dai_runtime->stream;
  380. }
  381. static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
  382. {
  383. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  384. struct sdw_intel *sdw = cdns_to_intel(cdns);
  385. struct sdw_intel_link_res *res = sdw->link_res;
  386. struct sdw_cdns_dai_runtime *dai_runtime;
  387. int ret = 0;
  388. /*
  389. * The .trigger callback is used to program HDaudio DMA and send required IPC to audio
  390. * firmware.
  391. */
  392. if (res->ops && res->ops->trigger) {
  393. ret = res->ops->trigger(substream, cmd, dai);
  394. if (ret < 0)
  395. return ret;
  396. }
  397. dai_runtime = cdns->dai_runtime_array[dai->id];
  398. if (!dai_runtime) {
  399. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  400. __func__);
  401. return -EIO;
  402. }
  403. switch (cmd) {
  404. case SNDRV_PCM_TRIGGER_SUSPEND:
  405. /*
  406. * The .prepare callback is used to deal with xruns and resume operations.
  407. * In the case of xruns, the DMAs and SHIM registers cannot be touched,
  408. * but for resume operations the DMAs and SHIM registers need to be initialized.
  409. * the .trigger callback is used to track the suspend case only.
  410. */
  411. dai_runtime->suspended = true;
  412. break;
  413. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  414. dai_runtime->paused = true;
  415. break;
  416. case SNDRV_PCM_TRIGGER_STOP:
  417. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  418. dai_runtime->paused = false;
  419. break;
  420. default:
  421. break;
  422. }
  423. return ret;
  424. }
  425. static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
  426. .hw_params = intel_hw_params,
  427. .prepare = intel_prepare,
  428. .hw_free = intel_hw_free,
  429. .trigger = intel_trigger,
  430. .set_stream = intel_pcm_set_sdw_stream,
  431. .get_stream = intel_get_sdw_stream,
  432. };
  433. static const struct snd_soc_component_driver dai_component = {
  434. .name = "soundwire",
  435. };
  436. /*
  437. * PDI routines
  438. */
  439. static void intel_pdi_init(struct sdw_intel *sdw,
  440. struct sdw_cdns_stream_config *config)
  441. {
  442. void __iomem *shim = sdw->link_res->shim;
  443. int pcm_cap;
  444. /* PCM Stream Capability */
  445. pcm_cap = intel_readw(shim, SDW_SHIM2_PCMSCAP);
  446. config->pcm_bd = FIELD_GET(SDW_SHIM2_PCMSCAP_BSS, pcm_cap);
  447. config->pcm_in = FIELD_GET(SDW_SHIM2_PCMSCAP_ISS, pcm_cap);
  448. config->pcm_out = FIELD_GET(SDW_SHIM2_PCMSCAP_ISS, pcm_cap);
  449. dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
  450. config->pcm_bd, config->pcm_in, config->pcm_out);
  451. }
  452. static int
  453. intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
  454. {
  455. void __iomem *shim = sdw->link_res->shim;
  456. /* zero based values for channel count in register */
  457. return intel_readw(shim, SDW_SHIM2_PCMSYCHC(pdi_num)) + 1;
  458. }
  459. static void intel_pdi_get_ch_update(struct sdw_intel *sdw,
  460. struct sdw_cdns_pdi *pdi,
  461. unsigned int num_pdi,
  462. unsigned int *num_ch)
  463. {
  464. int ch_count = 0;
  465. int i;
  466. for (i = 0; i < num_pdi; i++) {
  467. pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
  468. ch_count += pdi->ch_count;
  469. pdi++;
  470. }
  471. *num_ch = ch_count;
  472. }
  473. static void intel_pdi_stream_ch_update(struct sdw_intel *sdw,
  474. struct sdw_cdns_streams *stream)
  475. {
  476. intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
  477. &stream->num_ch_bd);
  478. intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
  479. &stream->num_ch_in);
  480. intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
  481. &stream->num_ch_out);
  482. }
  483. static int intel_create_dai(struct sdw_cdns *cdns,
  484. struct snd_soc_dai_driver *dais,
  485. enum intel_pdi_type type,
  486. u32 num, u32 off, u32 max_ch)
  487. {
  488. int i;
  489. if (!num)
  490. return 0;
  491. for (i = off; i < (off + num); i++) {
  492. dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
  493. "SDW%d Pin%d",
  494. cdns->instance, i);
  495. if (!dais[i].name)
  496. return -ENOMEM;
  497. if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
  498. dais[i].playback.channels_min = 1;
  499. dais[i].playback.channels_max = max_ch;
  500. }
  501. if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
  502. dais[i].capture.channels_min = 1;
  503. dais[i].capture.channels_max = max_ch;
  504. }
  505. dais[i].ops = &intel_pcm_dai_ops;
  506. }
  507. return 0;
  508. }
  509. static int intel_register_dai(struct sdw_intel *sdw)
  510. {
  511. struct sdw_cdns_dai_runtime **dai_runtime_array;
  512. struct sdw_cdns_stream_config config;
  513. struct sdw_cdns *cdns = &sdw->cdns;
  514. struct sdw_cdns_streams *stream;
  515. struct snd_soc_dai_driver *dais;
  516. int num_dai;
  517. int ret;
  518. int off = 0;
  519. /* Read the PDI config and initialize cadence PDI */
  520. intel_pdi_init(sdw, &config);
  521. ret = sdw_cdns_pdi_init(cdns, config);
  522. if (ret)
  523. return ret;
  524. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
  525. /* DAIs are created based on total number of PDIs supported */
  526. num_dai = cdns->pcm.num_pdi;
  527. dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
  528. sizeof(struct sdw_cdns_dai_runtime *),
  529. GFP_KERNEL);
  530. if (!dai_runtime_array)
  531. return -ENOMEM;
  532. cdns->dai_runtime_array = dai_runtime_array;
  533. dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
  534. if (!dais)
  535. return -ENOMEM;
  536. /* Create PCM DAIs */
  537. stream = &cdns->pcm;
  538. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
  539. off, stream->num_ch_in);
  540. if (ret)
  541. return ret;
  542. off += cdns->pcm.num_in;
  543. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
  544. off, stream->num_ch_out);
  545. if (ret)
  546. return ret;
  547. off += cdns->pcm.num_out;
  548. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
  549. off, stream->num_ch_bd);
  550. if (ret)
  551. return ret;
  552. return devm_snd_soc_register_component(cdns->dev, &dai_component,
  553. dais, num_dai);
  554. }
  555. static void intel_program_sdi(struct sdw_intel *sdw, int dev_num)
  556. {
  557. int ret;
  558. ret = hdac_bus_eml_sdw_set_lsdiid(sdw->link_res->hbus, sdw->instance, dev_num);
  559. if (ret < 0)
  560. dev_err(sdw->cdns.dev, "%s: could not set lsdiid for link %d %d\n",
  561. __func__, sdw->instance, dev_num);
  562. }
  563. static int intel_get_link_count(struct sdw_intel *sdw)
  564. {
  565. int ret;
  566. ret = hdac_bus_eml_get_count(sdw->link_res->hbus, true, AZX_REG_ML_LEPTR_ID_SDW);
  567. if (!ret) {
  568. dev_err(sdw->cdns.dev, "%s: could not retrieve link count\n", __func__);
  569. return -ENODEV;
  570. }
  571. if (ret > SDW_INTEL_MAX_LINKS) {
  572. dev_err(sdw->cdns.dev, "%s: link count %d exceed max %d\n", __func__, ret, SDW_INTEL_MAX_LINKS);
  573. return -EINVAL;
  574. }
  575. return ret;
  576. }
  577. const struct sdw_intel_hw_ops sdw_intel_lnl_hw_ops = {
  578. .debugfs_init = intel_ace2x_debugfs_init,
  579. .debugfs_exit = intel_ace2x_debugfs_exit,
  580. .get_link_count = intel_get_link_count,
  581. .register_dai = intel_register_dai,
  582. .check_clock_stop = intel_check_clock_stop,
  583. .start_bus = intel_start_bus,
  584. .start_bus_after_reset = intel_start_bus_after_reset,
  585. .start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
  586. .stop_bus = intel_stop_bus,
  587. .link_power_up = intel_link_power_up,
  588. .link_power_down = intel_link_power_down,
  589. .shim_check_wake = intel_shim_check_wake,
  590. .shim_wake = intel_shim_wake,
  591. .pre_bank_switch = intel_pre_bank_switch,
  592. .post_bank_switch = intel_post_bank_switch,
  593. .sync_arm = intel_sync_arm,
  594. .sync_go_unlocked = intel_sync_go_unlocked,
  595. .sync_go = intel_sync_go,
  596. .sync_check_cmdsync_unlocked = intel_check_cmdsync_unlocked,
  597. .program_sdi = intel_program_sdi,
  598. };
  599. EXPORT_SYMBOL_NS(sdw_intel_lnl_hw_ops, SOUNDWIRE_INTEL);
  600. MODULE_IMPORT_NS(SND_SOC_SOF_HDA_MLINK);