intel_ace2x.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 <linux/string_choices.h>
  13. #include <sound/hdaudio.h>
  14. #include <sound/hda-mlink.h>
  15. #include <sound/hda-sdw-bpt.h>
  16. #include <sound/hda_register.h>
  17. #include <sound/pcm_params.h>
  18. #include "cadence_master.h"
  19. #include "bus.h"
  20. #include "intel.h"
  21. static int sdw_slave_bpt_stream_add(struct sdw_slave *slave, struct sdw_stream_runtime *stream)
  22. {
  23. struct sdw_stream_config sconfig = {0};
  24. struct sdw_port_config pconfig = {0};
  25. int ret;
  26. /* arbitrary configuration */
  27. sconfig.frame_rate = 16000;
  28. sconfig.ch_count = 1;
  29. sconfig.bps = 32; /* this is required for BPT/BRA */
  30. sconfig.direction = SDW_DATA_DIR_RX;
  31. sconfig.type = SDW_STREAM_BPT;
  32. pconfig.num = 0;
  33. pconfig.ch_mask = BIT(0);
  34. ret = sdw_stream_add_slave(slave, &sconfig, &pconfig, 1, stream);
  35. if (ret)
  36. dev_err(&slave->dev, "%s: failed: %d\n", __func__, ret);
  37. return ret;
  38. }
  39. #define READ_PDI1_MIN_SIZE 12
  40. static int intel_ace2x_bpt_open_stream(struct sdw_intel *sdw, struct sdw_slave *slave,
  41. struct sdw_bpt_msg *msg)
  42. {
  43. struct sdw_cdns *cdns = &sdw->cdns;
  44. struct sdw_bus *bus = &cdns->bus;
  45. struct sdw_master_prop *prop = &bus->prop;
  46. struct sdw_stream_runtime *stream;
  47. struct sdw_stream_config sconfig;
  48. struct sdw_port_config *pconfig;
  49. unsigned int pdi0_buf_size_pre_frame;
  50. unsigned int pdi1_buf_size_pre_frame;
  51. unsigned int pdi0_buffer_size_;
  52. unsigned int pdi1_buffer_size_;
  53. unsigned int pdi0_buffer_size;
  54. unsigned int tx_dma_bandwidth;
  55. unsigned int pdi1_buffer_size;
  56. unsigned int rx_dma_bandwidth;
  57. unsigned int fake_num_frames;
  58. unsigned int data_per_frame;
  59. unsigned int tx_total_bytes;
  60. struct sdw_cdns_pdi *pdi0;
  61. struct sdw_cdns_pdi *pdi1;
  62. unsigned int rx_alignment;
  63. unsigned int tx_alignment;
  64. unsigned int num_frames_;
  65. unsigned int num_frames;
  66. unsigned int fake_size;
  67. unsigned int tx_pad;
  68. unsigned int rx_pad;
  69. int command;
  70. int ret1;
  71. int ret;
  72. int dir;
  73. int len;
  74. int i;
  75. stream = sdw_alloc_stream("BPT", SDW_STREAM_BPT);
  76. if (!stream)
  77. return -ENOMEM;
  78. cdns->bus.bpt_stream = stream;
  79. ret = sdw_slave_bpt_stream_add(slave, stream);
  80. if (ret < 0)
  81. goto release_stream;
  82. /* handle PDI0 first */
  83. dir = SDW_DATA_DIR_TX;
  84. pdi0 = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, 1, dir, 0);
  85. if (!pdi0) {
  86. dev_err(cdns->dev, "%s: sdw_cdns_alloc_pdi0 failed\n", __func__);
  87. ret = -EINVAL;
  88. goto remove_slave;
  89. }
  90. sdw_cdns_config_stream(cdns, 1, dir, pdi0);
  91. /* handle PDI1 */
  92. dir = SDW_DATA_DIR_RX;
  93. pdi1 = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, 1, dir, 1);
  94. if (!pdi1) {
  95. dev_err(cdns->dev, "%s: sdw_cdns_alloc_pdi1 failed\n", __func__);
  96. ret = -EINVAL;
  97. goto remove_slave;
  98. }
  99. sdw_cdns_config_stream(cdns, 1, dir, pdi1);
  100. /*
  101. * the port config direction, number of channels and frame
  102. * rate is totally arbitrary
  103. */
  104. sconfig.direction = dir;
  105. sconfig.ch_count = 1;
  106. sconfig.frame_rate = 16000;
  107. sconfig.type = SDW_STREAM_BPT;
  108. sconfig.bps = 32; /* this is required for BPT/BRA */
  109. /* Port configuration */
  110. pconfig = kzalloc_objs(*pconfig, 2);
  111. if (!pconfig) {
  112. ret = -ENOMEM;
  113. goto remove_slave;
  114. }
  115. for (i = 0; i < 2 /* num_pdi */; i++) {
  116. pconfig[i].num = i;
  117. pconfig[i].ch_mask = 1;
  118. }
  119. ret = sdw_stream_add_master(&cdns->bus, &sconfig, pconfig, 2, stream);
  120. kfree(pconfig);
  121. if (ret < 0) {
  122. dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
  123. goto remove_slave;
  124. }
  125. ret = sdw_prepare_stream(cdns->bus.bpt_stream);
  126. if (ret < 0)
  127. goto remove_master;
  128. command = (msg->flags & SDW_MSG_FLAG_WRITE) ? 0 : 1;
  129. ret = sdw_cdns_bpt_find_bandwidth(command, cdns->bus.params.row,
  130. cdns->bus.params.col,
  131. prop->default_frame_rate,
  132. &tx_dma_bandwidth, &rx_dma_bandwidth);
  133. if (ret < 0)
  134. goto deprepare_stream;
  135. len = 0;
  136. pdi0_buffer_size = 0;
  137. pdi1_buffer_size = 0;
  138. num_frames = 0;
  139. /* Add up pdi buffer size and frame numbers of each BPT sections */
  140. for (i = 0; i < msg->sections; i++) {
  141. ret = sdw_cdns_bpt_find_buffer_sizes(command, cdns->bus.params.row,
  142. cdns->bus.params.col,
  143. msg->sec[i].len, SDW_BPT_MSG_MAX_BYTES,
  144. &data_per_frame, &pdi0_buffer_size_,
  145. &pdi1_buffer_size_, &num_frames_);
  146. if (ret < 0)
  147. goto deprepare_stream;
  148. len += msg->sec[i].len;
  149. pdi0_buffer_size += pdi0_buffer_size_;
  150. pdi1_buffer_size += pdi1_buffer_size_;
  151. num_frames += num_frames_;
  152. }
  153. sdw->bpt_ctx.pdi0_buffer_size = pdi0_buffer_size;
  154. sdw->bpt_ctx.pdi1_buffer_size = pdi1_buffer_size;
  155. sdw->bpt_ctx.num_frames = num_frames;
  156. sdw->bpt_ctx.data_per_frame = data_per_frame;
  157. rx_alignment = hda_sdw_bpt_get_buf_size_alignment(rx_dma_bandwidth);
  158. tx_alignment = hda_sdw_bpt_get_buf_size_alignment(tx_dma_bandwidth);
  159. if (command) { /* read */
  160. /* Get buffer size of a full frame */
  161. ret = sdw_cdns_bpt_find_buffer_sizes(command, cdns->bus.params.row,
  162. cdns->bus.params.col,
  163. data_per_frame, SDW_BPT_MSG_MAX_BYTES,
  164. &data_per_frame, &pdi0_buf_size_pre_frame,
  165. &pdi1_buf_size_pre_frame, &fake_num_frames);
  166. if (ret < 0)
  167. goto deprepare_stream;
  168. /* find fake pdi1 buffer size */
  169. rx_pad = rx_alignment - (pdi1_buffer_size % rx_alignment);
  170. while (rx_pad <= READ_PDI1_MIN_SIZE)
  171. rx_pad += rx_alignment;
  172. pdi1_buffer_size += rx_pad;
  173. /* It is fine if we request more than enough byte to read */
  174. fake_num_frames = DIV_ROUND_UP(rx_pad, pdi1_buf_size_pre_frame);
  175. fake_size = fake_num_frames * data_per_frame;
  176. /* find fake pdi0 buffer size */
  177. pdi0_buffer_size += (fake_num_frames * pdi0_buf_size_pre_frame);
  178. tx_pad = tx_alignment - (pdi0_buffer_size % tx_alignment);
  179. pdi0_buffer_size += tx_pad;
  180. } else { /* write */
  181. /*
  182. * For the write command, the rx data block is 4, and the rx buffer size of a frame
  183. * is 8. So the rx buffer size (pdi0_buffer_size) is always a multiple of rx
  184. * alignment.
  185. */
  186. tx_pad = tx_alignment - (pdi0_buffer_size % tx_alignment);
  187. pdi0_buffer_size += tx_pad;
  188. }
  189. dev_dbg(cdns->dev, "Message len %d transferred in %d frames (%d per frame)\n",
  190. len, num_frames, data_per_frame);
  191. dev_dbg(cdns->dev, "sizes pdi0 %d pdi1 %d tx_bandwidth %d rx_bandwidth %d\n",
  192. pdi0_buffer_size, pdi1_buffer_size, tx_dma_bandwidth, rx_dma_bandwidth);
  193. ret = hda_sdw_bpt_open(cdns->dev->parent, /* PCI device */
  194. sdw->instance, &sdw->bpt_ctx.bpt_tx_stream,
  195. &sdw->bpt_ctx.dmab_tx_bdl, pdi0_buffer_size, tx_dma_bandwidth,
  196. &sdw->bpt_ctx.bpt_rx_stream, &sdw->bpt_ctx.dmab_rx_bdl,
  197. pdi1_buffer_size, rx_dma_bandwidth);
  198. if (ret < 0) {
  199. dev_err(cdns->dev, "%s: hda_sdw_bpt_open failed %d\n", __func__, ret);
  200. goto deprepare_stream;
  201. }
  202. if (!command) {
  203. ret = sdw_cdns_prepare_write_dma_buffer(msg->dev_num, msg->sec, msg->sections,
  204. data_per_frame,
  205. sdw->bpt_ctx.dmab_tx_bdl.area,
  206. pdi0_buffer_size, &tx_total_bytes);
  207. } else {
  208. ret = sdw_cdns_prepare_read_dma_buffer(msg->dev_num, msg->sec, msg->sections,
  209. data_per_frame,
  210. sdw->bpt_ctx.dmab_tx_bdl.area,
  211. pdi0_buffer_size, &tx_total_bytes,
  212. fake_size);
  213. }
  214. if (!ret)
  215. return 0;
  216. dev_err(cdns->dev, "%s: sdw_prepare_%s_dma_buffer failed %d\n",
  217. __func__, str_read_write(command), ret);
  218. ret1 = hda_sdw_bpt_close(cdns->dev->parent, /* PCI device */
  219. sdw->bpt_ctx.bpt_tx_stream, &sdw->bpt_ctx.dmab_tx_bdl,
  220. sdw->bpt_ctx.bpt_rx_stream, &sdw->bpt_ctx.dmab_rx_bdl);
  221. if (ret1 < 0)
  222. dev_err(cdns->dev, "%s: hda_sdw_bpt_close failed: ret %d\n",
  223. __func__, ret1);
  224. deprepare_stream:
  225. sdw_deprepare_stream(cdns->bus.bpt_stream);
  226. remove_master:
  227. ret1 = sdw_stream_remove_master(&cdns->bus, cdns->bus.bpt_stream);
  228. if (ret1 < 0)
  229. dev_err(cdns->dev, "%s: remove master failed: %d\n",
  230. __func__, ret1);
  231. remove_slave:
  232. ret1 = sdw_stream_remove_slave(slave, cdns->bus.bpt_stream);
  233. if (ret1 < 0)
  234. dev_err(cdns->dev, "%s: remove slave failed: %d\n",
  235. __func__, ret1);
  236. release_stream:
  237. sdw_release_stream(cdns->bus.bpt_stream);
  238. cdns->bus.bpt_stream = NULL;
  239. return ret;
  240. }
  241. static void intel_ace2x_bpt_close_stream(struct sdw_intel *sdw, struct sdw_slave *slave,
  242. struct sdw_bpt_msg *msg)
  243. {
  244. struct sdw_cdns *cdns = &sdw->cdns;
  245. int ret;
  246. ret = hda_sdw_bpt_close(cdns->dev->parent /* PCI device */, sdw->bpt_ctx.bpt_tx_stream,
  247. &sdw->bpt_ctx.dmab_tx_bdl, sdw->bpt_ctx.bpt_rx_stream,
  248. &sdw->bpt_ctx.dmab_rx_bdl);
  249. if (ret < 0)
  250. dev_err(cdns->dev, "%s: hda_sdw_bpt_close failed: ret %d\n",
  251. __func__, ret);
  252. ret = sdw_deprepare_stream(cdns->bus.bpt_stream);
  253. if (ret < 0)
  254. dev_err(cdns->dev, "%s: sdw_deprepare_stream failed: ret %d\n",
  255. __func__, ret);
  256. ret = sdw_stream_remove_master(&cdns->bus, cdns->bus.bpt_stream);
  257. if (ret < 0)
  258. dev_err(cdns->dev, "%s: remove master failed: %d\n",
  259. __func__, ret);
  260. ret = sdw_stream_remove_slave(slave, cdns->bus.bpt_stream);
  261. if (ret < 0)
  262. dev_err(cdns->dev, "%s: remove slave failed: %d\n",
  263. __func__, ret);
  264. cdns->bus.bpt_stream = NULL;
  265. }
  266. #define INTEL_BPT_MSG_BYTE_MIN 16
  267. static int intel_ace2x_bpt_send_async(struct sdw_intel *sdw, struct sdw_slave *slave,
  268. struct sdw_bpt_msg *msg)
  269. {
  270. struct sdw_cdns *cdns = &sdw->cdns;
  271. int len = 0;
  272. int ret;
  273. int i;
  274. for (i = 0; i < msg->sections; i++)
  275. len += msg->sec[i].len;
  276. if (len < INTEL_BPT_MSG_BYTE_MIN) {
  277. dev_err(cdns->dev, "BPT message length %d is less than the minimum bytes %d\n",
  278. len, INTEL_BPT_MSG_BYTE_MIN);
  279. return -EINVAL;
  280. }
  281. dev_dbg(cdns->dev, "BPT Transfer start\n");
  282. ret = intel_ace2x_bpt_open_stream(sdw, slave, msg);
  283. if (ret < 0)
  284. return ret;
  285. ret = hda_sdw_bpt_send_async(cdns->dev->parent, /* PCI device */
  286. sdw->bpt_ctx.bpt_tx_stream, sdw->bpt_ctx.bpt_rx_stream);
  287. if (ret < 0) {
  288. dev_err(cdns->dev, "%s: hda_sdw_bpt_send_async failed: %d\n",
  289. __func__, ret);
  290. intel_ace2x_bpt_close_stream(sdw, slave, msg);
  291. return ret;
  292. }
  293. ret = sdw_enable_stream(cdns->bus.bpt_stream);
  294. if (ret < 0) {
  295. dev_err(cdns->dev, "%s: sdw_stream_enable failed: %d\n",
  296. __func__, ret);
  297. intel_ace2x_bpt_close_stream(sdw, slave, msg);
  298. }
  299. return ret;
  300. }
  301. static int intel_ace2x_bpt_wait(struct sdw_intel *sdw, struct sdw_slave *slave,
  302. struct sdw_bpt_msg *msg)
  303. {
  304. struct sdw_cdns *cdns = &sdw->cdns;
  305. int ret;
  306. dev_dbg(cdns->dev, "BPT Transfer wait\n");
  307. ret = hda_sdw_bpt_wait(cdns->dev->parent, /* PCI device */
  308. sdw->bpt_ctx.bpt_tx_stream, sdw->bpt_ctx.bpt_rx_stream);
  309. if (ret < 0)
  310. dev_err(cdns->dev, "%s: hda_sdw_bpt_wait failed: %d\n", __func__, ret);
  311. ret = sdw_disable_stream(cdns->bus.bpt_stream);
  312. if (ret < 0) {
  313. dev_err(cdns->dev, "%s: sdw_stream_enable failed: %d\n",
  314. __func__, ret);
  315. goto err;
  316. }
  317. if (msg->flags & SDW_MSG_FLAG_WRITE) {
  318. ret = sdw_cdns_check_write_response(cdns->dev, sdw->bpt_ctx.dmab_rx_bdl.area,
  319. sdw->bpt_ctx.pdi1_buffer_size,
  320. sdw->bpt_ctx.num_frames);
  321. if (ret < 0)
  322. dev_err(cdns->dev, "%s: BPT Write failed %d\n", __func__, ret);
  323. } else {
  324. ret = sdw_cdns_check_read_response(cdns->dev, sdw->bpt_ctx.dmab_rx_bdl.area,
  325. sdw->bpt_ctx.pdi1_buffer_size,
  326. msg->sec, msg->sections, sdw->bpt_ctx.num_frames,
  327. sdw->bpt_ctx.data_per_frame);
  328. if (ret < 0)
  329. dev_err(cdns->dev, "%s: BPT Read failed %d\n", __func__, ret);
  330. }
  331. err:
  332. intel_ace2x_bpt_close_stream(sdw, slave, msg);
  333. return ret;
  334. }
  335. /*
  336. * shim vendor-specific (vs) ops
  337. */
  338. static void intel_shim_vs_init(struct sdw_intel *sdw)
  339. {
  340. void __iomem *shim_vs = sdw->link_res->shim_vs;
  341. struct sdw_bus *bus = &sdw->cdns.bus;
  342. struct sdw_intel_prop *intel_prop;
  343. u16 clde;
  344. u16 doaise2;
  345. u16 dodse2;
  346. u16 clds;
  347. u16 clss;
  348. u16 doaise;
  349. u16 doais;
  350. u16 dodse;
  351. u16 dods;
  352. u16 act;
  353. intel_prop = bus->vendor_specific_prop;
  354. clde = intel_prop->clde;
  355. doaise2 = intel_prop->doaise2;
  356. dodse2 = intel_prop->dodse2;
  357. clds = intel_prop->clds;
  358. clss = intel_prop->clss;
  359. doaise = intel_prop->doaise;
  360. doais = intel_prop->doais;
  361. dodse = intel_prop->dodse;
  362. dods = intel_prop->dods;
  363. act = intel_readw(shim_vs, SDW_SHIM2_INTEL_VS_ACTMCTL);
  364. u16p_replace_bits(&act, clde, SDW_SHIM3_INTEL_VS_ACTMCTL_CLDE);
  365. u16p_replace_bits(&act, doaise2, SDW_SHIM3_INTEL_VS_ACTMCTL_DOAISE2);
  366. u16p_replace_bits(&act, dodse2, SDW_SHIM3_INTEL_VS_ACTMCTL_DODSE2);
  367. u16p_replace_bits(&act, clds, SDW_SHIM3_INTEL_VS_ACTMCTL_CLDS);
  368. u16p_replace_bits(&act, clss, SDW_SHIM3_INTEL_VS_ACTMCTL_CLSS);
  369. u16p_replace_bits(&act, doaise, SDW_SHIM2_INTEL_VS_ACTMCTL_DOAISE);
  370. u16p_replace_bits(&act, doais, SDW_SHIM2_INTEL_VS_ACTMCTL_DOAIS);
  371. u16p_replace_bits(&act, dodse, SDW_SHIM2_INTEL_VS_ACTMCTL_DODSE);
  372. u16p_replace_bits(&act, dods, SDW_SHIM2_INTEL_VS_ACTMCTL_DODS);
  373. act |= SDW_SHIM2_INTEL_VS_ACTMCTL_DACTQE;
  374. intel_writew(shim_vs, SDW_SHIM2_INTEL_VS_ACTMCTL, act);
  375. usleep_range(10, 15);
  376. }
  377. static void intel_shim_vs_set_clock_source(struct sdw_intel *sdw, u32 source)
  378. {
  379. void __iomem *shim_vs = sdw->link_res->shim_vs;
  380. u32 val;
  381. val = intel_readl(shim_vs, SDW_SHIM2_INTEL_VS_LVSCTL);
  382. u32p_replace_bits(&val, source, SDW_SHIM2_INTEL_VS_LVSCTL_MLCS);
  383. intel_writel(shim_vs, SDW_SHIM2_INTEL_VS_LVSCTL, val);
  384. dev_dbg(sdw->cdns.dev, "clock source %d LVSCTL %#x\n", source, val);
  385. }
  386. static int intel_shim_check_wake(struct sdw_intel *sdw)
  387. {
  388. /*
  389. * We follow the HDaudio example and resume unconditionally
  390. * without checking the WAKESTS bit for that specific link
  391. */
  392. return 1;
  393. }
  394. static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
  395. {
  396. u16 lsdiid = 0;
  397. u16 wake_en;
  398. u16 wake_sts;
  399. int ret;
  400. mutex_lock(sdw->link_res->shim_lock);
  401. ret = hdac_bus_eml_sdw_get_lsdiid_unlocked(sdw->link_res->hbus, sdw->instance, &lsdiid);
  402. if (ret < 0)
  403. goto unlock;
  404. wake_en = snd_hdac_chip_readw(sdw->link_res->hbus, WAKEEN);
  405. if (wake_enable) {
  406. /* Enable the wakeup */
  407. wake_en |= lsdiid;
  408. snd_hdac_chip_writew(sdw->link_res->hbus, WAKEEN, wake_en);
  409. } else {
  410. /* Disable the wake up interrupt */
  411. wake_en &= ~lsdiid;
  412. snd_hdac_chip_writew(sdw->link_res->hbus, WAKEEN, wake_en);
  413. /* Clear wake status (W1C) */
  414. wake_sts = snd_hdac_chip_readw(sdw->link_res->hbus, STATESTS);
  415. wake_sts |= lsdiid;
  416. snd_hdac_chip_writew(sdw->link_res->hbus, STATESTS, wake_sts);
  417. }
  418. unlock:
  419. mutex_unlock(sdw->link_res->shim_lock);
  420. }
  421. static int intel_link_power_up(struct sdw_intel *sdw)
  422. {
  423. struct sdw_bus *bus = &sdw->cdns.bus;
  424. struct sdw_master_prop *prop = &bus->prop;
  425. u32 *shim_mask = sdw->link_res->shim_mask;
  426. unsigned int link_id = sdw->instance;
  427. u32 clock_source;
  428. u32 syncprd;
  429. int ret;
  430. if (prop->mclk_freq % 6000000) {
  431. if (prop->mclk_freq % 2400000) {
  432. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24_576;
  433. clock_source = SDW_SHIM2_MLCS_CARDINAL_CLK;
  434. } else {
  435. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
  436. clock_source = SDW_SHIM2_MLCS_XTAL_CLK;
  437. }
  438. } else {
  439. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_96;
  440. clock_source = SDW_SHIM2_MLCS_AUDIO_PLL_CLK;
  441. }
  442. mutex_lock(sdw->link_res->shim_lock);
  443. ret = hdac_bus_eml_sdw_power_up_unlocked(sdw->link_res->hbus, link_id);
  444. if (ret < 0) {
  445. dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_power_up failed: %d\n",
  446. __func__, ret);
  447. goto out;
  448. }
  449. intel_shim_vs_set_clock_source(sdw, clock_source);
  450. if (!*shim_mask) {
  451. /* we first need to program the SyncPRD/CPU registers */
  452. dev_dbg(sdw->cdns.dev, "first link up, programming SYNCPRD\n");
  453. ret = hdac_bus_eml_sdw_set_syncprd_unlocked(sdw->link_res->hbus, syncprd);
  454. if (ret < 0) {
  455. dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_set_syncprd failed: %d\n",
  456. __func__, ret);
  457. goto out;
  458. }
  459. /* SYNCPU will change once link is active */
  460. ret = hdac_bus_eml_sdw_wait_syncpu_unlocked(sdw->link_res->hbus);
  461. if (ret < 0) {
  462. dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_wait_syncpu failed: %d\n",
  463. __func__, ret);
  464. goto out;
  465. }
  466. hdac_bus_eml_enable_interrupt_unlocked(sdw->link_res->hbus, true,
  467. AZX_REG_ML_LEPTR_ID_SDW, true);
  468. }
  469. *shim_mask |= BIT(link_id);
  470. sdw->cdns.link_up = true;
  471. intel_shim_vs_init(sdw);
  472. out:
  473. mutex_unlock(sdw->link_res->shim_lock);
  474. return ret;
  475. }
  476. static int intel_link_power_down(struct sdw_intel *sdw)
  477. {
  478. u32 *shim_mask = sdw->link_res->shim_mask;
  479. unsigned int link_id = sdw->instance;
  480. int ret;
  481. mutex_lock(sdw->link_res->shim_lock);
  482. sdw->cdns.link_up = false;
  483. *shim_mask &= ~BIT(link_id);
  484. if (!*shim_mask)
  485. hdac_bus_eml_enable_interrupt_unlocked(sdw->link_res->hbus, true,
  486. AZX_REG_ML_LEPTR_ID_SDW, false);
  487. ret = hdac_bus_eml_sdw_power_down_unlocked(sdw->link_res->hbus, link_id);
  488. if (ret < 0) {
  489. dev_err(sdw->cdns.dev, "%s: hdac_bus_eml_sdw_power_down failed: %d\n",
  490. __func__, ret);
  491. /*
  492. * we leave the sdw->cdns.link_up flag as false since we've disabled
  493. * the link at this point and cannot handle interrupts any longer.
  494. */
  495. }
  496. mutex_unlock(sdw->link_res->shim_lock);
  497. return ret;
  498. }
  499. static void intel_sync_arm(struct sdw_intel *sdw)
  500. {
  501. unsigned int link_id = sdw->instance;
  502. mutex_lock(sdw->link_res->shim_lock);
  503. hdac_bus_eml_sdw_sync_arm_unlocked(sdw->link_res->hbus, link_id);
  504. mutex_unlock(sdw->link_res->shim_lock);
  505. }
  506. static int intel_sync_go_unlocked(struct sdw_intel *sdw)
  507. {
  508. int ret;
  509. ret = hdac_bus_eml_sdw_sync_go_unlocked(sdw->link_res->hbus);
  510. if (ret < 0)
  511. dev_err(sdw->cdns.dev, "%s: SyncGO clear failed: %d\n", __func__, ret);
  512. return ret;
  513. }
  514. static int intel_sync_go(struct sdw_intel *sdw)
  515. {
  516. int ret;
  517. mutex_lock(sdw->link_res->shim_lock);
  518. ret = intel_sync_go_unlocked(sdw);
  519. mutex_unlock(sdw->link_res->shim_lock);
  520. return ret;
  521. }
  522. static bool intel_check_cmdsync_unlocked(struct sdw_intel *sdw)
  523. {
  524. return hdac_bus_eml_sdw_check_cmdsync_unlocked(sdw->link_res->hbus);
  525. }
  526. /* DAI callbacks */
  527. static int intel_params_stream(struct sdw_intel *sdw,
  528. struct snd_pcm_substream *substream,
  529. struct snd_soc_dai *dai,
  530. struct snd_pcm_hw_params *hw_params,
  531. int link_id, int alh_stream_id)
  532. {
  533. struct sdw_intel_link_res *res = sdw->link_res;
  534. struct sdw_intel_stream_params_data params_data;
  535. params_data.substream = substream;
  536. params_data.dai = dai;
  537. params_data.hw_params = hw_params;
  538. params_data.link_id = link_id;
  539. params_data.alh_stream_id = alh_stream_id;
  540. if (res->ops && res->ops->params_stream && res->dev)
  541. return res->ops->params_stream(res->dev,
  542. &params_data);
  543. return -EIO;
  544. }
  545. static int intel_free_stream(struct sdw_intel *sdw,
  546. struct snd_pcm_substream *substream,
  547. struct snd_soc_dai *dai,
  548. int link_id)
  549. {
  550. struct sdw_intel_link_res *res = sdw->link_res;
  551. struct sdw_intel_stream_free_data free_data;
  552. free_data.substream = substream;
  553. free_data.dai = dai;
  554. free_data.link_id = link_id;
  555. if (res->ops && res->ops->free_stream && res->dev)
  556. return res->ops->free_stream(res->dev,
  557. &free_data);
  558. return 0;
  559. }
  560. /*
  561. * DAI operations
  562. */
  563. static int intel_hw_params(struct snd_pcm_substream *substream,
  564. struct snd_pcm_hw_params *params,
  565. struct snd_soc_dai *dai)
  566. {
  567. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  568. struct sdw_intel *sdw = cdns_to_intel(cdns);
  569. struct sdw_cdns_dai_runtime *dai_runtime;
  570. struct sdw_cdns_pdi *pdi;
  571. struct sdw_stream_config sconfig;
  572. int ch, dir;
  573. int ret;
  574. dai_runtime = cdns->dai_runtime_array[dai->id];
  575. if (!dai_runtime)
  576. return -EIO;
  577. ch = params_channels(params);
  578. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  579. dir = SDW_DATA_DIR_RX;
  580. else
  581. dir = SDW_DATA_DIR_TX;
  582. pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
  583. if (!pdi)
  584. return -EINVAL;
  585. /* use same definitions for alh_id as previous generations */
  586. pdi->intel_alh_id = (sdw->instance * 16) + pdi->num + 3;
  587. if (pdi->num >= 2)
  588. pdi->intel_alh_id += 2;
  589. /* the SHIM will be configured in the callback functions */
  590. sdw_cdns_config_stream(cdns, ch, dir, pdi);
  591. /* store pdi and state, 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 snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  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. struct snd_pcm_hw_params *hw_params;
  626. int ch, dir;
  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. hw_params = &rtd->dpcm[substream->stream].hw_params;
  634. if (dai_runtime->suspended) {
  635. dai_runtime->suspended = false;
  636. /*
  637. * .prepare() is called after system resume, where we
  638. * need to reinitialize the SHIM/ALH/Cadence IP.
  639. * .prepare() is also called to deal with underflows,
  640. * but in those cases we cannot touch ALH/SHIM
  641. * registers
  642. */
  643. /* configure stream */
  644. ch = params_channels(hw_params);
  645. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  646. dir = SDW_DATA_DIR_RX;
  647. else
  648. dir = SDW_DATA_DIR_TX;
  649. /* the SHIM will be configured in the callback functions */
  650. sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
  651. }
  652. /* Inform DSP about PDI stream number */
  653. return intel_params_stream(sdw, substream, dai, hw_params, sdw->instance,
  654. dai_runtime->pdi->intel_alh_id);
  655. }
  656. static int
  657. intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  658. {
  659. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  660. struct sdw_intel *sdw = cdns_to_intel(cdns);
  661. struct sdw_cdns_dai_runtime *dai_runtime;
  662. int ret;
  663. dai_runtime = cdns->dai_runtime_array[dai->id];
  664. if (!dai_runtime)
  665. return -EIO;
  666. /*
  667. * The sdw stream state will transition to RELEASED when stream->
  668. * master_list is empty. So the stream state will transition to
  669. * DEPREPARED for the first cpu-dai and to RELEASED for the last
  670. * cpu-dai.
  671. */
  672. ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
  673. if (ret < 0) {
  674. dev_err(dai->dev, "remove master from stream %s failed: %d\n",
  675. dai_runtime->stream->name, ret);
  676. return ret;
  677. }
  678. ret = intel_free_stream(sdw, substream, dai, sdw->instance);
  679. if (ret < 0) {
  680. dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
  681. return ret;
  682. }
  683. dai_runtime->pdi = NULL;
  684. return 0;
  685. }
  686. static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
  687. void *stream, int direction)
  688. {
  689. return cdns_set_sdw_stream(dai, stream, direction);
  690. }
  691. static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
  692. int direction)
  693. {
  694. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  695. struct sdw_cdns_dai_runtime *dai_runtime;
  696. dai_runtime = cdns->dai_runtime_array[dai->id];
  697. if (!dai_runtime)
  698. return ERR_PTR(-EINVAL);
  699. return dai_runtime->stream;
  700. }
  701. static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
  702. {
  703. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  704. struct sdw_intel *sdw = cdns_to_intel(cdns);
  705. struct sdw_intel_link_res *res = sdw->link_res;
  706. struct sdw_cdns_dai_runtime *dai_runtime;
  707. int ret = 0;
  708. /*
  709. * The .trigger callback is used to program HDaudio DMA and send required IPC to audio
  710. * firmware.
  711. */
  712. if (res->ops && res->ops->trigger) {
  713. ret = res->ops->trigger(substream, cmd, dai);
  714. if (ret < 0)
  715. return ret;
  716. }
  717. dai_runtime = cdns->dai_runtime_array[dai->id];
  718. if (!dai_runtime) {
  719. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  720. __func__);
  721. return -EIO;
  722. }
  723. switch (cmd) {
  724. case SNDRV_PCM_TRIGGER_SUSPEND:
  725. /*
  726. * The .prepare callback is used to deal with xruns and resume operations.
  727. * In the case of xruns, the DMAs and SHIM registers cannot be touched,
  728. * but for resume operations the DMAs and SHIM registers need to be initialized.
  729. * the .trigger callback is used to track the suspend case only.
  730. */
  731. dai_runtime->suspended = true;
  732. break;
  733. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  734. dai_runtime->paused = true;
  735. break;
  736. case SNDRV_PCM_TRIGGER_STOP:
  737. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  738. dai_runtime->paused = false;
  739. break;
  740. default:
  741. break;
  742. }
  743. return ret;
  744. }
  745. static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
  746. .hw_params = intel_hw_params,
  747. .prepare = intel_prepare,
  748. .hw_free = intel_hw_free,
  749. .trigger = intel_trigger,
  750. .set_stream = intel_pcm_set_sdw_stream,
  751. .get_stream = intel_get_sdw_stream,
  752. };
  753. static const struct snd_soc_component_driver dai_component = {
  754. .name = "soundwire",
  755. };
  756. /*
  757. * PDI routines
  758. */
  759. static void intel_pdi_init(struct sdw_intel *sdw,
  760. struct sdw_cdns_stream_config *config)
  761. {
  762. void __iomem *shim = sdw->link_res->shim;
  763. int pcm_cap;
  764. /* PCM Stream Capability */
  765. pcm_cap = intel_readw(shim, SDW_SHIM2_PCMSCAP);
  766. config->pcm_bd = FIELD_GET(SDW_SHIM2_PCMSCAP_BSS, pcm_cap);
  767. config->pcm_in = FIELD_GET(SDW_SHIM2_PCMSCAP_ISS, pcm_cap);
  768. config->pcm_out = FIELD_GET(SDW_SHIM2_PCMSCAP_ISS, pcm_cap);
  769. dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
  770. config->pcm_bd, config->pcm_in, config->pcm_out);
  771. }
  772. static int
  773. intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
  774. {
  775. void __iomem *shim = sdw->link_res->shim;
  776. /* zero based values for channel count in register */
  777. return intel_readw(shim, SDW_SHIM2_PCMSYCHC(pdi_num)) + 1;
  778. }
  779. static void intel_pdi_get_ch_update(struct sdw_intel *sdw,
  780. struct sdw_cdns_pdi *pdi,
  781. unsigned int num_pdi,
  782. unsigned int *num_ch)
  783. {
  784. int ch_count = 0;
  785. int i;
  786. for (i = 0; i < num_pdi; i++) {
  787. pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
  788. ch_count += pdi->ch_count;
  789. pdi++;
  790. }
  791. *num_ch = ch_count;
  792. }
  793. static void intel_pdi_stream_ch_update(struct sdw_intel *sdw,
  794. struct sdw_cdns_streams *stream)
  795. {
  796. intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
  797. &stream->num_ch_bd);
  798. intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
  799. &stream->num_ch_in);
  800. intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
  801. &stream->num_ch_out);
  802. }
  803. static int intel_create_dai(struct sdw_cdns *cdns,
  804. struct snd_soc_dai_driver *dais,
  805. enum intel_pdi_type type,
  806. u32 num, u32 off, u32 max_ch)
  807. {
  808. int i;
  809. if (!num)
  810. return 0;
  811. for (i = off; i < (off + num); i++) {
  812. dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
  813. "SDW%d Pin%d",
  814. cdns->instance, i);
  815. if (!dais[i].name)
  816. return -ENOMEM;
  817. if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
  818. dais[i].playback.channels_min = 1;
  819. dais[i].playback.channels_max = max_ch;
  820. }
  821. if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
  822. dais[i].capture.channels_min = 1;
  823. dais[i].capture.channels_max = max_ch;
  824. }
  825. dais[i].ops = &intel_pcm_dai_ops;
  826. }
  827. return 0;
  828. }
  829. static int intel_register_dai(struct sdw_intel *sdw)
  830. {
  831. struct sdw_cdns_dai_runtime **dai_runtime_array;
  832. struct sdw_cdns_stream_config config;
  833. struct sdw_cdns *cdns = &sdw->cdns;
  834. struct sdw_cdns_streams *stream;
  835. struct snd_soc_dai_driver *dais;
  836. int num_dai;
  837. int ret;
  838. int off = 0;
  839. /* Read the PDI config and initialize cadence PDI */
  840. intel_pdi_init(sdw, &config);
  841. ret = sdw_cdns_pdi_init(cdns, config);
  842. if (ret)
  843. return ret;
  844. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
  845. /* DAIs are created based on total number of PDIs supported */
  846. num_dai = cdns->pcm.num_pdi;
  847. dai_runtime_array = devm_kcalloc(cdns->dev, num_dai,
  848. sizeof(struct sdw_cdns_dai_runtime *),
  849. GFP_KERNEL);
  850. if (!dai_runtime_array)
  851. return -ENOMEM;
  852. cdns->dai_runtime_array = dai_runtime_array;
  853. dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
  854. if (!dais)
  855. return -ENOMEM;
  856. /* Create PCM DAIs */
  857. stream = &cdns->pcm;
  858. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
  859. off, stream->num_ch_in);
  860. if (ret)
  861. return ret;
  862. off += cdns->pcm.num_in;
  863. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
  864. off, stream->num_ch_out);
  865. if (ret)
  866. return ret;
  867. off += cdns->pcm.num_out;
  868. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
  869. off, stream->num_ch_bd);
  870. if (ret)
  871. return ret;
  872. return devm_snd_soc_register_component(cdns->dev, &dai_component,
  873. dais, num_dai);
  874. }
  875. static void intel_program_sdi(struct sdw_intel *sdw, int dev_num)
  876. {
  877. int ret;
  878. ret = hdac_bus_eml_sdw_set_lsdiid(sdw->link_res->hbus, sdw->instance, dev_num);
  879. if (ret < 0)
  880. dev_err(sdw->cdns.dev, "%s: could not set lsdiid for link %d %d\n",
  881. __func__, sdw->instance, dev_num);
  882. }
  883. static int intel_get_link_count(struct sdw_intel *sdw)
  884. {
  885. int ret;
  886. ret = hdac_bus_eml_get_count(sdw->link_res->hbus, true, AZX_REG_ML_LEPTR_ID_SDW);
  887. if (!ret) {
  888. dev_err(sdw->cdns.dev, "%s: could not retrieve link count\n", __func__);
  889. return -ENODEV;
  890. }
  891. if (ret > SDW_INTEL_MAX_LINKS) {
  892. dev_err(sdw->cdns.dev, "%s: link count %d exceed max %d\n", __func__, ret, SDW_INTEL_MAX_LINKS);
  893. return -EINVAL;
  894. }
  895. return ret;
  896. }
  897. const struct sdw_intel_hw_ops sdw_intel_lnl_hw_ops = {
  898. .debugfs_init = intel_ace2x_debugfs_init,
  899. .debugfs_exit = intel_ace2x_debugfs_exit,
  900. .get_link_count = intel_get_link_count,
  901. .register_dai = intel_register_dai,
  902. .check_clock_stop = intel_check_clock_stop,
  903. .start_bus = intel_start_bus,
  904. .start_bus_after_reset = intel_start_bus_after_reset,
  905. .start_bus_after_clock_stop = intel_start_bus_after_clock_stop,
  906. .stop_bus = intel_stop_bus,
  907. .link_power_up = intel_link_power_up,
  908. .link_power_down = intel_link_power_down,
  909. .shim_check_wake = intel_shim_check_wake,
  910. .shim_wake = intel_shim_wake,
  911. .pre_bank_switch = intel_pre_bank_switch,
  912. .post_bank_switch = intel_post_bank_switch,
  913. .sync_arm = intel_sync_arm,
  914. .sync_go_unlocked = intel_sync_go_unlocked,
  915. .sync_go = intel_sync_go,
  916. .sync_check_cmdsync_unlocked = intel_check_cmdsync_unlocked,
  917. .program_sdi = intel_program_sdi,
  918. .bpt_send_async = intel_ace2x_bpt_send_async,
  919. .bpt_wait = intel_ace2x_bpt_wait,
  920. };
  921. EXPORT_SYMBOL_NS(sdw_intel_lnl_hw_ops, "SOUNDWIRE_INTEL");
  922. MODULE_IMPORT_NS("SND_SOC_SOF_HDA_MLINK");
  923. MODULE_IMPORT_NS("SND_SOC_SOF_INTEL_HDA_SDW_BPT");