pcm.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2021-2022 Intel Corporation
  4. //
  5. // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
  6. // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
  7. //
  8. #include <linux/debugfs.h>
  9. #include <linux/device.h>
  10. #include <sound/hda_register.h>
  11. #include <sound/hdaudio_ext.h>
  12. #include <sound/pcm_params.h>
  13. #include <sound/soc-acpi.h>
  14. #include <sound/soc-acpi-intel-match.h>
  15. #include <sound/soc-component.h>
  16. #include "avs.h"
  17. #include "path.h"
  18. #include "pcm.h"
  19. #include "topology.h"
  20. #include "../../codecs/hda.h"
  21. struct avs_dma_data {
  22. struct avs_tplg_path_template *template;
  23. struct avs_path *path;
  24. struct avs_dev *adev;
  25. /* LINK-stream utilized in BE operations while HOST in FE ones. */
  26. union {
  27. struct hdac_ext_stream *link_stream;
  28. struct hdac_ext_stream *host_stream;
  29. };
  30. struct work_struct period_elapsed_work;
  31. struct snd_pcm_substream *substream;
  32. };
  33. static struct avs_tplg_path_template *
  34. avs_dai_find_path_template(struct snd_soc_dai *dai, bool is_fe, int direction)
  35. {
  36. struct snd_soc_dapm_widget *dw = snd_soc_dai_get_widget(dai, direction);
  37. struct snd_soc_dapm_path *dp;
  38. enum snd_soc_dapm_direction dir;
  39. if (direction == SNDRV_PCM_STREAM_CAPTURE) {
  40. dir = is_fe ? SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN;
  41. } else {
  42. dir = is_fe ? SND_SOC_DAPM_DIR_IN : SND_SOC_DAPM_DIR_OUT;
  43. }
  44. dp = list_first_entry_or_null(&dw->edges[dir], typeof(*dp), list_node[dir]);
  45. if (!dp)
  46. return NULL;
  47. /* Get the other widget, with actual path template data */
  48. dw = (dp->source == dw) ? dp->sink : dp->source;
  49. return dw->priv;
  50. }
  51. static void avs_period_elapsed_work(struct work_struct *work)
  52. {
  53. struct avs_dma_data *data = container_of(work, struct avs_dma_data, period_elapsed_work);
  54. snd_pcm_period_elapsed(data->substream);
  55. }
  56. void avs_period_elapsed(struct snd_pcm_substream *substream)
  57. {
  58. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  59. struct snd_soc_dai *dai = snd_soc_rtd_to_cpu(rtd, 0);
  60. struct avs_dma_data *data = snd_soc_dai_get_dma_data(dai, substream);
  61. schedule_work(&data->period_elapsed_work);
  62. }
  63. static int avs_dai_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  64. {
  65. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  66. struct avs_dev *adev = to_avs_dev(dai->component->dev);
  67. struct avs_tplg_path_template *template;
  68. struct avs_dma_data *data;
  69. template = avs_dai_find_path_template(dai, !rtd->dai_link->no_pcm, substream->stream);
  70. if (!template) {
  71. dev_err(dai->dev, "no %s path for dai %s, invalid tplg?\n",
  72. snd_pcm_stream_str(substream), dai->name);
  73. return -EINVAL;
  74. }
  75. data = kzalloc(sizeof(*data), GFP_KERNEL);
  76. if (!data)
  77. return -ENOMEM;
  78. data->substream = substream;
  79. data->template = template;
  80. data->adev = adev;
  81. INIT_WORK(&data->period_elapsed_work, avs_period_elapsed_work);
  82. snd_soc_dai_set_dma_data(dai, substream, data);
  83. if (rtd->dai_link->ignore_suspend)
  84. adev->num_lp_paths++;
  85. return 0;
  86. }
  87. static void avs_dai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  88. {
  89. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  90. struct avs_dma_data *data;
  91. data = snd_soc_dai_get_dma_data(dai, substream);
  92. if (rtd->dai_link->ignore_suspend)
  93. data->adev->num_lp_paths--;
  94. snd_soc_dai_set_dma_data(dai, substream, NULL);
  95. kfree(data);
  96. }
  97. static int avs_dai_hw_params(struct snd_pcm_substream *substream,
  98. struct snd_pcm_hw_params *fe_hw_params,
  99. struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai,
  100. int dma_id)
  101. {
  102. struct avs_dma_data *data;
  103. struct avs_path *path;
  104. int ret;
  105. data = snd_soc_dai_get_dma_data(dai, substream);
  106. dev_dbg(dai->dev, "%s FE hw_params str %p rtd %p",
  107. __func__, substream, substream->runtime);
  108. dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n",
  109. params_rate(fe_hw_params), params_channels(fe_hw_params),
  110. params_width(fe_hw_params), params_physical_width(fe_hw_params));
  111. dev_dbg(dai->dev, "%s BE hw_params str %p rtd %p",
  112. __func__, substream, substream->runtime);
  113. dev_dbg(dai->dev, "rate %d chn %d vbd %d bd %d\n",
  114. params_rate(be_hw_params), params_channels(be_hw_params),
  115. params_width(be_hw_params), params_physical_width(be_hw_params));
  116. path = avs_path_create(data->adev, dma_id, data->template, fe_hw_params, be_hw_params);
  117. if (IS_ERR(path)) {
  118. ret = PTR_ERR(path);
  119. dev_err(dai->dev, "create path failed: %d\n", ret);
  120. return ret;
  121. }
  122. data->path = path;
  123. return 0;
  124. }
  125. static int avs_dai_be_hw_params(struct snd_pcm_substream *substream,
  126. struct snd_pcm_hw_params *be_hw_params, struct snd_soc_dai *dai,
  127. int dma_id)
  128. {
  129. struct snd_pcm_hw_params *fe_hw_params = NULL;
  130. struct snd_soc_pcm_runtime *fe, *be;
  131. struct snd_soc_dpcm *dpcm;
  132. be = snd_soc_substream_to_rtd(substream);
  133. for_each_dpcm_fe(be, substream->stream, dpcm) {
  134. fe = dpcm->fe;
  135. fe_hw_params = &fe->dpcm[substream->stream].hw_params;
  136. }
  137. return avs_dai_hw_params(substream, fe_hw_params, be_hw_params, dai, dma_id);
  138. }
  139. static int avs_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  140. {
  141. struct avs_dma_data *data;
  142. int ret;
  143. data = snd_soc_dai_get_dma_data(dai, substream);
  144. if (!data->path)
  145. return 0;
  146. ret = avs_path_reset(data->path);
  147. if (ret < 0) {
  148. dev_err(dai->dev, "reset path failed: %d\n", ret);
  149. return ret;
  150. }
  151. ret = avs_path_pause(data->path);
  152. if (ret < 0)
  153. dev_err(dai->dev, "pause path failed: %d\n", ret);
  154. return ret;
  155. }
  156. static int avs_dai_nonhda_be_hw_params(struct snd_pcm_substream *substream,
  157. struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
  158. {
  159. struct avs_dma_data *data;
  160. data = snd_soc_dai_get_dma_data(dai, substream);
  161. if (data->path)
  162. return 0;
  163. /* Actual port-id comes from topology. */
  164. return avs_dai_be_hw_params(substream, hw_params, dai, 0);
  165. }
  166. static int avs_dai_nonhda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  167. {
  168. struct avs_dma_data *data;
  169. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  170. data = snd_soc_dai_get_dma_data(dai, substream);
  171. if (data->path) {
  172. avs_path_free(data->path);
  173. data->path = NULL;
  174. }
  175. return 0;
  176. }
  177. static int avs_dai_nonhda_be_trigger(struct snd_pcm_substream *substream, int cmd,
  178. struct snd_soc_dai *dai)
  179. {
  180. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  181. struct avs_dma_data *data;
  182. int ret = 0;
  183. data = snd_soc_dai_get_dma_data(dai, substream);
  184. switch (cmd) {
  185. case SNDRV_PCM_TRIGGER_RESUME:
  186. if (rtd->dai_link->ignore_suspend)
  187. break;
  188. fallthrough;
  189. case SNDRV_PCM_TRIGGER_START:
  190. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  191. ret = avs_path_pause(data->path);
  192. if (ret < 0) {
  193. dev_err(dai->dev, "pause BE path failed: %d\n", ret);
  194. break;
  195. }
  196. ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
  197. if (ret < 0)
  198. dev_err(dai->dev, "run BE path failed: %d\n", ret);
  199. break;
  200. case SNDRV_PCM_TRIGGER_SUSPEND:
  201. if (rtd->dai_link->ignore_suspend)
  202. break;
  203. fallthrough;
  204. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  205. case SNDRV_PCM_TRIGGER_STOP:
  206. ret = avs_path_pause(data->path);
  207. if (ret < 0)
  208. dev_err(dai->dev, "pause BE path failed: %d\n", ret);
  209. ret = avs_path_reset(data->path);
  210. if (ret < 0)
  211. dev_err(dai->dev, "reset BE path failed: %d\n", ret);
  212. break;
  213. default:
  214. ret = -EINVAL;
  215. break;
  216. }
  217. return ret;
  218. }
  219. static const struct snd_soc_dai_ops avs_dai_nonhda_be_ops = {
  220. .startup = avs_dai_startup,
  221. .shutdown = avs_dai_shutdown,
  222. .hw_params = avs_dai_nonhda_be_hw_params,
  223. .hw_free = avs_dai_nonhda_be_hw_free,
  224. .prepare = avs_dai_prepare,
  225. .trigger = avs_dai_nonhda_be_trigger,
  226. };
  227. static int avs_dai_hda_be_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  228. {
  229. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  230. struct hdac_ext_stream *link_stream;
  231. struct avs_dma_data *data;
  232. struct hda_codec *codec;
  233. int ret;
  234. ret = avs_dai_startup(substream, dai);
  235. if (ret)
  236. return ret;
  237. codec = dev_to_hda_codec(snd_soc_rtd_to_codec(rtd, 0)->dev);
  238. link_stream = snd_hdac_ext_stream_assign(&codec->bus->core, substream,
  239. HDAC_EXT_STREAM_TYPE_LINK);
  240. if (!link_stream) {
  241. avs_dai_shutdown(substream, dai);
  242. return -EBUSY;
  243. }
  244. data = snd_soc_dai_get_dma_data(dai, substream);
  245. data->link_stream = link_stream;
  246. substream->runtime->private_data = link_stream;
  247. return 0;
  248. }
  249. static void avs_dai_hda_be_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  250. {
  251. struct avs_dma_data *data = snd_soc_dai_get_dma_data(dai, substream);
  252. snd_hdac_ext_stream_release(data->link_stream, HDAC_EXT_STREAM_TYPE_LINK);
  253. substream->runtime->private_data = NULL;
  254. avs_dai_shutdown(substream, dai);
  255. }
  256. static int avs_dai_hda_be_hw_params(struct snd_pcm_substream *substream,
  257. struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
  258. {
  259. struct avs_dma_data *data;
  260. data = snd_soc_dai_get_dma_data(dai, substream);
  261. if (data->path)
  262. return 0;
  263. return avs_dai_be_hw_params(substream, hw_params, dai,
  264. hdac_stream(data->link_stream)->stream_tag - 1);
  265. }
  266. static int avs_dai_hda_be_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  267. {
  268. struct avs_dma_data *data;
  269. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  270. struct hdac_ext_stream *link_stream;
  271. struct hdac_ext_link *link;
  272. struct hda_codec *codec;
  273. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  274. data = snd_soc_dai_get_dma_data(dai, substream);
  275. if (!data->path)
  276. return 0;
  277. link_stream = data->link_stream;
  278. link_stream->link_prepared = false;
  279. avs_path_free(data->path);
  280. data->path = NULL;
  281. /* clear link <-> stream mapping */
  282. codec = dev_to_hda_codec(snd_soc_rtd_to_codec(rtd, 0)->dev);
  283. link = snd_hdac_ext_bus_get_hlink_by_addr(&codec->bus->core, codec->core.addr);
  284. if (!link)
  285. return -EINVAL;
  286. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  287. snd_hdac_ext_bus_link_clear_stream_id(link, hdac_stream(link_stream)->stream_tag);
  288. return 0;
  289. }
  290. static int avs_dai_hda_be_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  291. {
  292. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  293. struct snd_pcm_runtime *runtime = substream->runtime;
  294. const struct snd_soc_pcm_stream *stream_info;
  295. struct hdac_ext_stream *link_stream;
  296. struct hdac_ext_link *link;
  297. struct avs_dma_data *data;
  298. struct hda_codec *codec;
  299. struct hdac_bus *bus;
  300. unsigned int format_val;
  301. unsigned int bits;
  302. int ret;
  303. data = snd_soc_dai_get_dma_data(dai, substream);
  304. link_stream = data->link_stream;
  305. if (link_stream->link_prepared)
  306. return 0;
  307. codec = dev_to_hda_codec(snd_soc_rtd_to_codec(rtd, 0)->dev);
  308. bus = &codec->bus->core;
  309. stream_info = snd_soc_dai_get_pcm_stream(dai, substream->stream);
  310. bits = snd_hdac_stream_format_bits(runtime->format, runtime->subformat,
  311. stream_info->sig_bits);
  312. format_val = snd_hdac_stream_format(runtime->channels, bits, runtime->rate);
  313. snd_hdac_ext_stream_decouple(bus, link_stream, true);
  314. snd_hdac_ext_stream_reset(link_stream);
  315. snd_hdac_ext_stream_setup(link_stream, format_val);
  316. link = snd_hdac_ext_bus_get_hlink_by_addr(bus, codec->core.addr);
  317. if (!link)
  318. return -EINVAL;
  319. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  320. snd_hdac_ext_bus_link_set_stream_id(link, hdac_stream(link_stream)->stream_tag);
  321. ret = avs_dai_prepare(substream, dai);
  322. if (ret)
  323. return ret;
  324. link_stream->link_prepared = true;
  325. return 0;
  326. }
  327. static int avs_dai_hda_be_trigger(struct snd_pcm_substream *substream, int cmd,
  328. struct snd_soc_dai *dai)
  329. {
  330. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  331. struct avs_dma_data *data;
  332. int ret = 0;
  333. dev_dbg(dai->dev, "entry %s cmd=%d\n", __func__, cmd);
  334. data = snd_soc_dai_get_dma_data(dai, substream);
  335. switch (cmd) {
  336. case SNDRV_PCM_TRIGGER_RESUME:
  337. if (rtd->dai_link->ignore_suspend)
  338. break;
  339. fallthrough;
  340. case SNDRV_PCM_TRIGGER_START:
  341. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  342. snd_hdac_ext_stream_start(data->link_stream);
  343. ret = avs_path_pause(data->path);
  344. if (ret < 0) {
  345. dev_err(dai->dev, "pause BE path failed: %d\n", ret);
  346. break;
  347. }
  348. ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
  349. if (ret < 0)
  350. dev_err(dai->dev, "run BE path failed: %d\n", ret);
  351. break;
  352. case SNDRV_PCM_TRIGGER_SUSPEND:
  353. if (rtd->dai_link->ignore_suspend)
  354. break;
  355. fallthrough;
  356. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  357. case SNDRV_PCM_TRIGGER_STOP:
  358. ret = avs_path_pause(data->path);
  359. if (ret < 0)
  360. dev_err(dai->dev, "pause BE path failed: %d\n", ret);
  361. snd_hdac_ext_stream_clear(data->link_stream);
  362. ret = avs_path_reset(data->path);
  363. if (ret < 0)
  364. dev_err(dai->dev, "reset BE path failed: %d\n", ret);
  365. break;
  366. default:
  367. ret = -EINVAL;
  368. break;
  369. }
  370. return ret;
  371. }
  372. static const struct snd_soc_dai_ops avs_dai_hda_be_ops = {
  373. .startup = avs_dai_hda_be_startup,
  374. .shutdown = avs_dai_hda_be_shutdown,
  375. .hw_params = avs_dai_hda_be_hw_params,
  376. .hw_free = avs_dai_hda_be_hw_free,
  377. .prepare = avs_dai_hda_be_prepare,
  378. .trigger = avs_dai_hda_be_trigger,
  379. };
  380. static int hw_rule_param_size(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  381. {
  382. struct snd_interval *interval = hw_param_interval(params, rule->var);
  383. struct snd_interval to;
  384. snd_interval_any(&to);
  385. to.integer = interval->integer;
  386. to.max = interval->max;
  387. /*
  388. * Commonly 2ms buffer size is used in HDA scenarios whereas 4ms is used
  389. * when streaming through GPDMA. Align to the latter to account for both.
  390. */
  391. to.min = params_rate(params) / 1000 * 4;
  392. if (rule->var == SNDRV_PCM_HW_PARAM_PERIOD_SIZE)
  393. to.min /= params_periods(params);
  394. return snd_interval_refine(interval, &to);
  395. }
  396. static int avs_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
  397. {
  398. struct snd_pcm_runtime *runtime = substream->runtime;
  399. int ret;
  400. ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  401. if (ret < 0)
  402. return ret;
  403. /* Avoid wrap-around with wall-clock. */
  404. ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME, 20, 178000000);
  405. if (ret < 0)
  406. return ret;
  407. /* Adjust buffer and period size based on the audio format. */
  408. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, hw_rule_param_size, NULL,
  409. SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_CHANNELS,
  410. SNDRV_PCM_HW_PARAM_RATE, -1);
  411. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, hw_rule_param_size, NULL,
  412. SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_CHANNELS,
  413. SNDRV_PCM_HW_PARAM_RATE, -1);
  414. return 0;
  415. }
  416. static int avs_dai_fe_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  417. {
  418. struct hdac_ext_stream *host_stream;
  419. struct avs_dma_data *data;
  420. struct hdac_bus *bus;
  421. int ret;
  422. ret = avs_pcm_hw_constraints_init(substream);
  423. if (ret)
  424. return ret;
  425. ret = avs_dai_startup(substream, dai);
  426. if (ret)
  427. return ret;
  428. data = snd_soc_dai_get_dma_data(dai, substream);
  429. bus = &data->adev->base.core;
  430. host_stream = snd_hdac_ext_stream_assign(bus, substream, HDAC_EXT_STREAM_TYPE_HOST);
  431. if (!host_stream) {
  432. avs_dai_shutdown(substream, dai);
  433. return -EBUSY;
  434. }
  435. data->host_stream = host_stream;
  436. snd_pcm_set_sync(substream);
  437. dev_dbg(dai->dev, "%s fe STARTUP tag %d str %p",
  438. __func__, hdac_stream(host_stream)->stream_tag, substream);
  439. return 0;
  440. }
  441. static void avs_dai_fe_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  442. {
  443. struct avs_dma_data *data;
  444. data = snd_soc_dai_get_dma_data(dai, substream);
  445. snd_hdac_ext_stream_release(data->host_stream, HDAC_EXT_STREAM_TYPE_HOST);
  446. avs_dai_shutdown(substream, dai);
  447. }
  448. static int avs_dai_fe_hw_params(struct snd_pcm_substream *substream,
  449. struct snd_pcm_hw_params *hw_params, struct snd_soc_dai *dai)
  450. {
  451. struct snd_pcm_hw_params *be_hw_params = NULL;
  452. struct snd_soc_pcm_runtime *fe, *be;
  453. struct snd_soc_dpcm *dpcm;
  454. struct avs_dma_data *data;
  455. struct hdac_ext_stream *host_stream;
  456. int ret;
  457. data = snd_soc_dai_get_dma_data(dai, substream);
  458. if (data->path)
  459. return 0;
  460. host_stream = data->host_stream;
  461. hdac_stream(host_stream)->bufsize = 0;
  462. hdac_stream(host_stream)->period_bytes = 0;
  463. hdac_stream(host_stream)->format_val = 0;
  464. fe = snd_soc_substream_to_rtd(substream);
  465. for_each_dpcm_be(fe, substream->stream, dpcm) {
  466. be = dpcm->be;
  467. be_hw_params = &be->dpcm[substream->stream].hw_params;
  468. }
  469. ret = avs_dai_hw_params(substream, hw_params, be_hw_params, dai,
  470. hdac_stream(host_stream)->stream_tag - 1);
  471. if (ret)
  472. goto create_err;
  473. ret = avs_path_bind(data->path);
  474. if (ret < 0) {
  475. dev_err(dai->dev, "bind FE <-> BE failed: %d\n", ret);
  476. goto bind_err;
  477. }
  478. return 0;
  479. bind_err:
  480. avs_path_free(data->path);
  481. data->path = NULL;
  482. create_err:
  483. snd_pcm_lib_free_pages(substream);
  484. return ret;
  485. }
  486. static int __avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  487. {
  488. struct avs_dma_data *data;
  489. struct hdac_ext_stream *host_stream;
  490. int ret;
  491. dev_dbg(dai->dev, "%s fe HW_FREE str %p rtd %p",
  492. __func__, substream, substream->runtime);
  493. data = snd_soc_dai_get_dma_data(dai, substream);
  494. if (!data->path)
  495. return 0;
  496. host_stream = data->host_stream;
  497. ret = avs_path_unbind(data->path);
  498. if (ret < 0)
  499. dev_err(dai->dev, "unbind FE <-> BE failed: %d\n", ret);
  500. avs_path_free(data->path);
  501. data->path = NULL;
  502. snd_hdac_stream_cleanup(hdac_stream(host_stream));
  503. hdac_stream(host_stream)->prepared = false;
  504. return ret;
  505. }
  506. static int avs_dai_fe_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  507. {
  508. int ret;
  509. ret = __avs_dai_fe_hw_free(substream, dai);
  510. snd_pcm_lib_free_pages(substream);
  511. return ret;
  512. }
  513. static int avs_dai_fe_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  514. {
  515. struct snd_pcm_runtime *runtime = substream->runtime;
  516. const struct snd_soc_pcm_stream *stream_info;
  517. struct avs_dma_data *data;
  518. struct hdac_ext_stream *host_stream;
  519. unsigned int format_val;
  520. struct hdac_bus *bus;
  521. unsigned int bits;
  522. int ret;
  523. data = snd_soc_dai_get_dma_data(dai, substream);
  524. host_stream = data->host_stream;
  525. if (hdac_stream(host_stream)->prepared)
  526. return 0;
  527. bus = hdac_stream(host_stream)->bus;
  528. snd_hdac_ext_stream_decouple(bus, data->host_stream, true);
  529. snd_hdac_stream_reset(hdac_stream(host_stream));
  530. stream_info = snd_soc_dai_get_pcm_stream(dai, substream->stream);
  531. bits = snd_hdac_stream_format_bits(runtime->format, runtime->subformat,
  532. stream_info->sig_bits);
  533. format_val = snd_hdac_stream_format(runtime->channels, bits, runtime->rate);
  534. ret = snd_hdac_stream_set_params(hdac_stream(host_stream), format_val);
  535. if (ret < 0)
  536. return ret;
  537. ret = snd_hdac_ext_host_stream_setup(host_stream, false);
  538. if (ret < 0)
  539. return ret;
  540. ret = avs_dai_prepare(substream, dai);
  541. if (ret)
  542. return ret;
  543. hdac_stream(host_stream)->prepared = true;
  544. return 0;
  545. }
  546. static void avs_hda_stream_start(struct hdac_bus *bus, struct hdac_ext_stream *host_stream)
  547. {
  548. struct hdac_stream *first_running = NULL;
  549. struct hdac_stream *pos;
  550. struct avs_dev *adev = hdac_to_avs(bus);
  551. list_for_each_entry(pos, &bus->stream_list, list) {
  552. if (pos->running) {
  553. if (first_running)
  554. break; /* more than one running */
  555. first_running = pos;
  556. }
  557. }
  558. /*
  559. * If host_stream is a CAPTURE stream and will be the only one running,
  560. * disable L1SEN to avoid sound clipping.
  561. */
  562. if (!first_running) {
  563. if (hdac_stream(host_stream)->direction == SNDRV_PCM_STREAM_CAPTURE)
  564. avs_hda_l1sen_enable(adev, false);
  565. snd_hdac_stream_start(hdac_stream(host_stream));
  566. return;
  567. }
  568. snd_hdac_stream_start(hdac_stream(host_stream));
  569. /*
  570. * If host_stream is the first stream to break the rule above,
  571. * re-enable L1SEN.
  572. */
  573. if (list_entry_is_head(pos, &bus->stream_list, list) &&
  574. first_running->direction == SNDRV_PCM_STREAM_CAPTURE)
  575. avs_hda_l1sen_enable(adev, true);
  576. }
  577. static void avs_hda_stream_stop(struct hdac_bus *bus, struct hdac_ext_stream *host_stream)
  578. {
  579. struct hdac_stream *first_running = NULL;
  580. struct hdac_stream *pos;
  581. struct avs_dev *adev = hdac_to_avs(bus);
  582. list_for_each_entry(pos, &bus->stream_list, list) {
  583. if (pos == hdac_stream(host_stream))
  584. continue; /* ignore stream that is about to be stopped */
  585. if (pos->running) {
  586. if (first_running)
  587. break; /* more than one running */
  588. first_running = pos;
  589. }
  590. }
  591. /*
  592. * If host_stream is a CAPTURE stream and is the only one running,
  593. * re-enable L1SEN.
  594. */
  595. if (!first_running) {
  596. snd_hdac_stream_stop(hdac_stream(host_stream));
  597. if (hdac_stream(host_stream)->direction == SNDRV_PCM_STREAM_CAPTURE)
  598. avs_hda_l1sen_enable(adev, true);
  599. return;
  600. }
  601. /*
  602. * If by stopping host_stream there is only a single, CAPTURE stream running
  603. * left, disable L1SEN to avoid sound clipping.
  604. */
  605. if (list_entry_is_head(pos, &bus->stream_list, list) &&
  606. first_running->direction == SNDRV_PCM_STREAM_CAPTURE)
  607. avs_hda_l1sen_enable(adev, false);
  608. snd_hdac_stream_stop(hdac_stream(host_stream));
  609. }
  610. static int avs_dai_fe_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
  611. {
  612. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  613. struct avs_dma_data *data;
  614. struct hdac_ext_stream *host_stream;
  615. struct hdac_bus *bus;
  616. unsigned long flags;
  617. int ret = 0;
  618. data = snd_soc_dai_get_dma_data(dai, substream);
  619. host_stream = data->host_stream;
  620. bus = hdac_stream(host_stream)->bus;
  621. switch (cmd) {
  622. case SNDRV_PCM_TRIGGER_RESUME:
  623. if (rtd->dai_link->ignore_suspend)
  624. break;
  625. fallthrough;
  626. case SNDRV_PCM_TRIGGER_START:
  627. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  628. spin_lock_irqsave(&bus->reg_lock, flags);
  629. avs_hda_stream_start(bus, host_stream);
  630. spin_unlock_irqrestore(&bus->reg_lock, flags);
  631. /* Timeout on DRSM poll shall not stop the resume so ignore the result. */
  632. if (cmd == SNDRV_PCM_TRIGGER_RESUME)
  633. snd_hdac_stream_wait_drsm(hdac_stream(host_stream));
  634. ret = avs_path_pause(data->path);
  635. if (ret < 0) {
  636. dev_err(dai->dev, "pause FE path failed: %d\n", ret);
  637. break;
  638. }
  639. ret = avs_path_run(data->path, AVS_TPLG_TRIGGER_AUTO);
  640. if (ret < 0)
  641. dev_err(dai->dev, "run FE path failed: %d\n", ret);
  642. break;
  643. case SNDRV_PCM_TRIGGER_SUSPEND:
  644. if (rtd->dai_link->ignore_suspend)
  645. break;
  646. fallthrough;
  647. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  648. case SNDRV_PCM_TRIGGER_STOP:
  649. ret = avs_path_pause(data->path);
  650. if (ret < 0)
  651. dev_err(dai->dev, "pause FE path failed: %d\n", ret);
  652. spin_lock_irqsave(&bus->reg_lock, flags);
  653. avs_hda_stream_stop(bus, host_stream);
  654. spin_unlock_irqrestore(&bus->reg_lock, flags);
  655. ret = avs_path_reset(data->path);
  656. if (ret < 0)
  657. dev_err(dai->dev, "reset FE path failed: %d\n", ret);
  658. break;
  659. default:
  660. ret = -EINVAL;
  661. break;
  662. }
  663. return ret;
  664. }
  665. const struct snd_soc_dai_ops avs_dai_fe_ops = {
  666. .startup = avs_dai_fe_startup,
  667. .shutdown = avs_dai_fe_shutdown,
  668. .hw_params = avs_dai_fe_hw_params,
  669. .hw_free = avs_dai_fe_hw_free,
  670. .prepare = avs_dai_fe_prepare,
  671. .trigger = avs_dai_fe_trigger,
  672. };
  673. static ssize_t topology_name_read(struct file *file, char __user *user_buf, size_t count,
  674. loff_t *ppos)
  675. {
  676. struct snd_soc_component *component = file->private_data;
  677. struct snd_soc_card *card = component->card;
  678. struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
  679. char buf[64];
  680. size_t len;
  681. len = scnprintf(buf, sizeof(buf), "%s/%s\n", component->driver->topology_name_prefix,
  682. mach->tplg_filename);
  683. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  684. }
  685. static const struct file_operations topology_name_fops = {
  686. .open = simple_open,
  687. .read = topology_name_read,
  688. .llseek = default_llseek,
  689. };
  690. static int avs_component_load_libraries(struct avs_soc_component *acomp)
  691. {
  692. struct avs_tplg *tplg = acomp->tplg;
  693. struct avs_dev *adev = to_avs_dev(acomp->base.dev);
  694. int ret;
  695. if (!tplg->num_libs)
  696. return 0;
  697. /* Parent device may be asleep and library loading involves IPCs. */
  698. ret = pm_runtime_resume_and_get(adev->dev);
  699. if (ret < 0)
  700. return ret;
  701. avs_hda_power_gating_enable(adev, false);
  702. avs_hda_clock_gating_enable(adev, false);
  703. avs_hda_l1sen_enable(adev, false);
  704. ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs);
  705. avs_hda_l1sen_enable(adev, true);
  706. avs_hda_clock_gating_enable(adev, true);
  707. avs_hda_power_gating_enable(adev, true);
  708. if (!ret)
  709. ret = avs_module_info_init(adev, false);
  710. pm_runtime_mark_last_busy(adev->dev);
  711. pm_runtime_put_autosuspend(adev->dev);
  712. return ret;
  713. }
  714. static int avs_component_probe(struct snd_soc_component *component)
  715. {
  716. struct snd_soc_card *card = component->card;
  717. struct snd_soc_acpi_mach *mach;
  718. struct avs_soc_component *acomp;
  719. struct avs_dev *adev;
  720. char *filename;
  721. int ret;
  722. dev_dbg(card->dev, "probing %s card %s\n", component->name, card->name);
  723. mach = dev_get_platdata(card->dev);
  724. acomp = to_avs_soc_component(component);
  725. adev = to_avs_dev(component->dev);
  726. acomp->tplg = avs_tplg_new(component);
  727. if (!acomp->tplg)
  728. return -ENOMEM;
  729. if (!mach->tplg_filename)
  730. goto finalize;
  731. /* Load specified topology and create debugfs for it. */
  732. filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix,
  733. mach->tplg_filename);
  734. if (!filename)
  735. return -ENOMEM;
  736. ret = avs_load_topology(component, filename);
  737. kfree(filename);
  738. if (ret == -ENOENT && !strncmp(mach->tplg_filename, "hda-", 4)) {
  739. unsigned int vendor_id;
  740. if (sscanf(mach->tplg_filename, "hda-%08x-tplg.bin", &vendor_id) != 1)
  741. return ret;
  742. if (((vendor_id >> 16) & 0xFFFF) == 0x8086)
  743. mach->tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL,
  744. "hda-8086-generic-tplg.bin");
  745. else
  746. mach->tplg_filename = devm_kasprintf(adev->dev, GFP_KERNEL,
  747. "hda-generic-tplg.bin");
  748. if (!mach->tplg_filename)
  749. return -ENOMEM;
  750. filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix,
  751. mach->tplg_filename);
  752. if (!filename)
  753. return -ENOMEM;
  754. dev_info(card->dev, "trying to load fallback topology %s\n", mach->tplg_filename);
  755. ret = avs_load_topology(component, filename);
  756. kfree(filename);
  757. }
  758. if (ret < 0)
  759. return ret;
  760. ret = avs_component_load_libraries(acomp);
  761. if (ret < 0) {
  762. dev_err(card->dev, "libraries loading failed: %d\n", ret);
  763. goto err_load_libs;
  764. }
  765. finalize:
  766. debugfs_create_file("topology_name", 0444, component->debugfs_root, component,
  767. &topology_name_fops);
  768. mutex_lock(&adev->comp_list_mutex);
  769. list_add_tail(&acomp->node, &adev->comp_list);
  770. mutex_unlock(&adev->comp_list_mutex);
  771. return 0;
  772. err_load_libs:
  773. avs_remove_topology(component);
  774. return ret;
  775. }
  776. static void avs_component_remove(struct snd_soc_component *component)
  777. {
  778. struct avs_soc_component *acomp = to_avs_soc_component(component);
  779. struct snd_soc_acpi_mach *mach;
  780. struct avs_dev *adev = to_avs_dev(component->dev);
  781. int ret;
  782. mach = dev_get_platdata(component->card->dev);
  783. mutex_lock(&adev->comp_list_mutex);
  784. list_del(&acomp->node);
  785. mutex_unlock(&adev->comp_list_mutex);
  786. if (mach->tplg_filename) {
  787. ret = avs_remove_topology(component);
  788. if (ret < 0)
  789. dev_err(component->dev, "unload topology failed: %d\n", ret);
  790. }
  791. }
  792. static int avs_dai_resume_hw_params(struct snd_soc_dai *dai, struct avs_dma_data *data)
  793. {
  794. struct snd_pcm_substream *substream;
  795. struct snd_soc_pcm_runtime *rtd;
  796. int ret;
  797. substream = data->substream;
  798. rtd = snd_soc_substream_to_rtd(substream);
  799. ret = dai->driver->ops->hw_params(substream, &rtd->dpcm[substream->stream].hw_params, dai);
  800. if (ret)
  801. dev_err(dai->dev, "hw_params on resume failed: %d\n", ret);
  802. return ret;
  803. }
  804. static int avs_dai_resume_fe_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
  805. {
  806. struct hdac_ext_stream *host_stream;
  807. struct hdac_stream *hstream;
  808. struct hdac_bus *bus;
  809. int ret;
  810. host_stream = data->host_stream;
  811. hstream = hdac_stream(host_stream);
  812. bus = hdac_stream(host_stream)->bus;
  813. /* Set DRSM before programming stream and position registers. */
  814. snd_hdac_stream_drsm_enable(bus, true, hstream->index);
  815. ret = dai->driver->ops->prepare(data->substream, dai);
  816. if (ret) {
  817. dev_err(dai->dev, "prepare FE on resume failed: %d\n", ret);
  818. return ret;
  819. }
  820. writel(host_stream->pphcllpl, host_stream->pphc_addr + AZX_REG_PPHCLLPL);
  821. writel(host_stream->pphcllpu, host_stream->pphc_addr + AZX_REG_PPHCLLPU);
  822. writel(host_stream->pphcldpl, host_stream->pphc_addr + AZX_REG_PPHCLDPL);
  823. writel(host_stream->pphcldpu, host_stream->pphc_addr + AZX_REG_PPHCLDPU);
  824. /* As per HW spec recommendation, program LPIB and DPIB to the same value. */
  825. snd_hdac_stream_set_lpib(hstream, hstream->lpib);
  826. snd_hdac_stream_set_dpibr(bus, hstream, hstream->lpib);
  827. return 0;
  828. }
  829. static int avs_dai_resume_be_prepare(struct snd_soc_dai *dai, struct avs_dma_data *data)
  830. {
  831. int ret;
  832. ret = dai->driver->ops->prepare(data->substream, dai);
  833. if (ret)
  834. dev_err(dai->dev, "prepare BE on resume failed: %d\n", ret);
  835. return ret;
  836. }
  837. static int avs_dai_suspend_fe_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
  838. {
  839. struct hdac_ext_stream *host_stream;
  840. int ret;
  841. host_stream = data->host_stream;
  842. /* Store position addresses so we can resume from them later on. */
  843. hdac_stream(host_stream)->lpib = snd_hdac_stream_get_pos_lpib(hdac_stream(host_stream));
  844. host_stream->pphcllpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPL);
  845. host_stream->pphcllpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLLPU);
  846. host_stream->pphcldpl = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPL);
  847. host_stream->pphcldpu = readl(host_stream->pphc_addr + AZX_REG_PPHCLDPU);
  848. ret = __avs_dai_fe_hw_free(data->substream, dai);
  849. if (ret < 0)
  850. dev_err(dai->dev, "hw_free FE on suspend failed: %d\n", ret);
  851. return ret;
  852. }
  853. static int avs_dai_suspend_be_hw_free(struct snd_soc_dai *dai, struct avs_dma_data *data)
  854. {
  855. int ret;
  856. ret = dai->driver->ops->hw_free(data->substream, dai);
  857. if (ret < 0)
  858. dev_err(dai->dev, "hw_free BE on suspend failed: %d\n", ret);
  859. return ret;
  860. }
  861. static int avs_component_pm_op(struct snd_soc_component *component, bool be,
  862. int (*op)(struct snd_soc_dai *, struct avs_dma_data *))
  863. {
  864. struct snd_soc_pcm_runtime *rtd;
  865. struct avs_dma_data *data;
  866. struct snd_soc_dai *dai;
  867. int ret;
  868. for_each_component_dais(component, dai) {
  869. data = snd_soc_dai_dma_data_get_playback(dai);
  870. if (data) {
  871. rtd = snd_soc_substream_to_rtd(data->substream);
  872. if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
  873. ret = op(dai, data);
  874. if (ret < 0) {
  875. __snd_pcm_set_state(data->substream->runtime,
  876. SNDRV_PCM_STATE_DISCONNECTED);
  877. return ret;
  878. }
  879. }
  880. }
  881. data = snd_soc_dai_dma_data_get_capture(dai);
  882. if (data) {
  883. rtd = snd_soc_substream_to_rtd(data->substream);
  884. if (rtd->dai_link->no_pcm == be && !rtd->dai_link->ignore_suspend) {
  885. ret = op(dai, data);
  886. if (ret < 0) {
  887. __snd_pcm_set_state(data->substream->runtime,
  888. SNDRV_PCM_STATE_DISCONNECTED);
  889. return ret;
  890. }
  891. }
  892. }
  893. }
  894. return 0;
  895. }
  896. static int avs_component_resume_hw_params(struct snd_soc_component *component, bool be)
  897. {
  898. return avs_component_pm_op(component, be, &avs_dai_resume_hw_params);
  899. }
  900. static int avs_component_resume_prepare(struct snd_soc_component *component, bool be)
  901. {
  902. int (*prepare_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
  903. if (be)
  904. prepare_cb = &avs_dai_resume_be_prepare;
  905. else
  906. prepare_cb = &avs_dai_resume_fe_prepare;
  907. return avs_component_pm_op(component, be, prepare_cb);
  908. }
  909. static int avs_component_suspend_hw_free(struct snd_soc_component *component, bool be)
  910. {
  911. int (*hw_free_cb)(struct snd_soc_dai *dai, struct avs_dma_data *data);
  912. if (be)
  913. hw_free_cb = &avs_dai_suspend_be_hw_free;
  914. else
  915. hw_free_cb = &avs_dai_suspend_fe_hw_free;
  916. return avs_component_pm_op(component, be, hw_free_cb);
  917. }
  918. static int avs_component_suspend(struct snd_soc_component *component)
  919. {
  920. int ret;
  921. /*
  922. * When freeing paths, FEs need to be first as they perform
  923. * path unbinding.
  924. */
  925. ret = avs_component_suspend_hw_free(component, false);
  926. if (ret)
  927. return ret;
  928. return avs_component_suspend_hw_free(component, true);
  929. }
  930. static int avs_component_resume(struct snd_soc_component *component)
  931. {
  932. int ret;
  933. /*
  934. * When creating paths, FEs need to be last as they perform
  935. * path binding.
  936. */
  937. ret = avs_component_resume_hw_params(component, true);
  938. if (ret)
  939. return ret;
  940. ret = avs_component_resume_hw_params(component, false);
  941. if (ret)
  942. return ret;
  943. /* It is expected that the LINK stream is prepared first. */
  944. ret = avs_component_resume_prepare(component, true);
  945. if (ret)
  946. return ret;
  947. return avs_component_resume_prepare(component, false);
  948. }
  949. static const struct snd_pcm_hardware avs_pcm_hardware = {
  950. .info = SNDRV_PCM_INFO_MMAP |
  951. SNDRV_PCM_INFO_MMAP_VALID |
  952. SNDRV_PCM_INFO_INTERLEAVED |
  953. SNDRV_PCM_INFO_PAUSE |
  954. SNDRV_PCM_INFO_RESUME |
  955. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  956. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  957. SNDRV_PCM_FMTBIT_S32_LE,
  958. .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 |
  959. SNDRV_PCM_SUBFMTBIT_MSBITS_24 |
  960. SNDRV_PCM_SUBFMTBIT_MSBITS_MAX,
  961. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  962. .period_bytes_min = 128,
  963. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  964. .periods_min = 2,
  965. .periods_max = AZX_MAX_FRAG,
  966. .fifo_size = 0,
  967. };
  968. static int avs_component_open(struct snd_soc_component *component,
  969. struct snd_pcm_substream *substream)
  970. {
  971. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  972. /* only FE DAI links are handled here */
  973. if (rtd->dai_link->no_pcm)
  974. return 0;
  975. return snd_soc_set_runtime_hwparams(substream, &avs_pcm_hardware);
  976. }
  977. static unsigned int avs_hda_stream_dpib_read(struct hdac_ext_stream *stream)
  978. {
  979. return readl(hdac_stream(stream)->bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE +
  980. (AZX_REG_VS_SDXDPIB_XINTERVAL * hdac_stream(stream)->index));
  981. }
  982. static snd_pcm_uframes_t
  983. avs_component_pointer(struct snd_soc_component *component, struct snd_pcm_substream *substream)
  984. {
  985. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  986. struct avs_dma_data *data;
  987. struct hdac_ext_stream *host_stream;
  988. unsigned int pos;
  989. data = snd_soc_dai_get_dma_data(snd_soc_rtd_to_cpu(rtd, 0), substream);
  990. if (!data->host_stream)
  991. return 0;
  992. host_stream = data->host_stream;
  993. pos = avs_hda_stream_dpib_read(host_stream);
  994. if (pos >= hdac_stream(host_stream)->bufsize)
  995. pos = 0;
  996. return bytes_to_frames(substream->runtime, pos);
  997. }
  998. static int avs_component_mmap(struct snd_soc_component *component,
  999. struct snd_pcm_substream *substream,
  1000. struct vm_area_struct *vma)
  1001. {
  1002. return snd_pcm_lib_default_mmap(substream, vma);
  1003. }
  1004. #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
  1005. static int avs_component_construct(struct snd_soc_component *component,
  1006. struct snd_soc_pcm_runtime *rtd)
  1007. {
  1008. struct snd_soc_dai *dai = snd_soc_rtd_to_cpu(rtd, 0);
  1009. struct snd_pcm *pcm = rtd->pcm;
  1010. if (dai->driver->playback.channels_min)
  1011. snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
  1012. SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
  1013. MAX_PREALLOC_SIZE);
  1014. if (dai->driver->capture.channels_min)
  1015. snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  1016. SNDRV_DMA_TYPE_DEV_SG, component->dev, 0,
  1017. MAX_PREALLOC_SIZE);
  1018. return 0;
  1019. }
  1020. static const struct snd_soc_component_driver avs_component_driver = {
  1021. .name = "avs-pcm",
  1022. .probe = avs_component_probe,
  1023. .remove = avs_component_remove,
  1024. .suspend = avs_component_suspend,
  1025. .resume = avs_component_resume,
  1026. .open = avs_component_open,
  1027. .pointer = avs_component_pointer,
  1028. .mmap = avs_component_mmap,
  1029. .pcm_construct = avs_component_construct,
  1030. .module_get_upon_open = 1, /* increment refcount when a pcm is opened */
  1031. .topology_name_prefix = "intel/avs",
  1032. };
  1033. int avs_soc_component_register(struct device *dev, const char *name,
  1034. const struct snd_soc_component_driver *drv,
  1035. struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais)
  1036. {
  1037. struct avs_soc_component *acomp;
  1038. int ret;
  1039. acomp = devm_kzalloc(dev, sizeof(*acomp), GFP_KERNEL);
  1040. if (!acomp)
  1041. return -ENOMEM;
  1042. ret = snd_soc_component_initialize(&acomp->base, drv, dev);
  1043. if (ret < 0)
  1044. return ret;
  1045. /* force name change after ASoC is done with its init */
  1046. acomp->base.name = name;
  1047. INIT_LIST_HEAD(&acomp->node);
  1048. return snd_soc_add_component(&acomp->base, cpu_dais, num_cpu_dais);
  1049. }
  1050. static struct snd_soc_dai_driver dmic_cpu_dais[] = {
  1051. {
  1052. .name = "DMIC Pin",
  1053. .ops = &avs_dai_nonhda_be_ops,
  1054. .capture = {
  1055. .stream_name = "DMIC Rx",
  1056. .channels_min = 1,
  1057. .channels_max = 4,
  1058. .rates = SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000,
  1059. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  1060. },
  1061. },
  1062. {
  1063. .name = "DMIC WoV Pin",
  1064. .ops = &avs_dai_nonhda_be_ops,
  1065. .capture = {
  1066. .stream_name = "DMIC WoV Rx",
  1067. .channels_min = 1,
  1068. .channels_max = 4,
  1069. .rates = SNDRV_PCM_RATE_16000,
  1070. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1071. },
  1072. },
  1073. };
  1074. int avs_dmic_platform_register(struct avs_dev *adev, const char *name)
  1075. {
  1076. return avs_soc_component_register(adev->dev, name, &avs_component_driver, dmic_cpu_dais,
  1077. ARRAY_SIZE(dmic_cpu_dais));
  1078. }
  1079. static const struct snd_soc_dai_driver i2s_dai_template = {
  1080. .ops = &avs_dai_nonhda_be_ops,
  1081. .playback = {
  1082. .channels_min = 1,
  1083. .channels_max = 8,
  1084. .rates = SNDRV_PCM_RATE_8000_192000 |
  1085. SNDRV_PCM_RATE_12000 |
  1086. SNDRV_PCM_RATE_24000 |
  1087. SNDRV_PCM_RATE_128000,
  1088. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  1089. SNDRV_PCM_FMTBIT_S32_LE,
  1090. .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 |
  1091. SNDRV_PCM_SUBFMTBIT_MSBITS_24 |
  1092. SNDRV_PCM_SUBFMTBIT_MSBITS_MAX,
  1093. },
  1094. .capture = {
  1095. .channels_min = 1,
  1096. .channels_max = 8,
  1097. .rates = SNDRV_PCM_RATE_8000_192000 |
  1098. SNDRV_PCM_RATE_12000 |
  1099. SNDRV_PCM_RATE_24000 |
  1100. SNDRV_PCM_RATE_128000,
  1101. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  1102. SNDRV_PCM_FMTBIT_S32_LE,
  1103. .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 |
  1104. SNDRV_PCM_SUBFMTBIT_MSBITS_24 |
  1105. SNDRV_PCM_SUBFMTBIT_MSBITS_MAX,
  1106. },
  1107. };
  1108. int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask,
  1109. unsigned long *tdms)
  1110. {
  1111. struct snd_soc_dai_driver *cpus, *dai;
  1112. size_t ssp_count, cpu_count;
  1113. int i, j;
  1114. ssp_count = adev->hw_cfg.i2s_caps.ctrl_count;
  1115. cpu_count = 0;
  1116. for_each_set_bit(i, &port_mask, ssp_count)
  1117. if (!tdms || test_bit(0, &tdms[i]))
  1118. cpu_count++;
  1119. if (tdms)
  1120. for_each_set_bit(i, &port_mask, ssp_count)
  1121. cpu_count += hweight_long(tdms[i]);
  1122. cpus = devm_kzalloc(adev->dev, sizeof(*cpus) * cpu_count, GFP_KERNEL);
  1123. if (!cpus)
  1124. return -ENOMEM;
  1125. dai = cpus;
  1126. for_each_set_bit(i, &port_mask, ssp_count) {
  1127. if (!tdms || test_bit(0, &tdms[i])) {
  1128. memcpy(dai, &i2s_dai_template, sizeof(*dai));
  1129. dai->name =
  1130. devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d Pin", i);
  1131. dai->playback.stream_name =
  1132. devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Tx", i);
  1133. dai->capture.stream_name =
  1134. devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d Rx", i);
  1135. if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
  1136. return -ENOMEM;
  1137. dai++;
  1138. }
  1139. }
  1140. if (!tdms)
  1141. goto plat_register;
  1142. for_each_set_bit(i, &port_mask, ssp_count) {
  1143. for_each_set_bit(j, &tdms[i], ssp_count) {
  1144. memcpy(dai, &i2s_dai_template, sizeof(*dai));
  1145. dai->name =
  1146. devm_kasprintf(adev->dev, GFP_KERNEL, "SSP%d:%d Pin", i, j);
  1147. dai->playback.stream_name =
  1148. devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Tx", i, j);
  1149. dai->capture.stream_name =
  1150. devm_kasprintf(adev->dev, GFP_KERNEL, "ssp%d:%d Rx", i, j);
  1151. if (!dai->name || !dai->playback.stream_name || !dai->capture.stream_name)
  1152. return -ENOMEM;
  1153. dai++;
  1154. }
  1155. }
  1156. plat_register:
  1157. return avs_soc_component_register(adev->dev, name, &avs_component_driver, cpus, cpu_count);
  1158. }
  1159. /* HD-Audio CPU DAI template */
  1160. static const struct snd_soc_dai_driver hda_cpu_dai = {
  1161. .ops = &avs_dai_hda_be_ops,
  1162. .playback = {
  1163. .channels_min = 1,
  1164. .channels_max = 8,
  1165. .rates = SNDRV_PCM_RATE_8000_192000,
  1166. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  1167. SNDRV_PCM_FMTBIT_S32_LE,
  1168. .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 |
  1169. SNDRV_PCM_SUBFMTBIT_MSBITS_24 |
  1170. SNDRV_PCM_SUBFMTBIT_MSBITS_MAX,
  1171. },
  1172. .capture = {
  1173. .channels_min = 1,
  1174. .channels_max = 8,
  1175. .rates = SNDRV_PCM_RATE_8000_192000,
  1176. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  1177. SNDRV_PCM_FMTBIT_S32_LE,
  1178. .subformats = SNDRV_PCM_SUBFMTBIT_MSBITS_20 |
  1179. SNDRV_PCM_SUBFMTBIT_MSBITS_24 |
  1180. SNDRV_PCM_SUBFMTBIT_MSBITS_MAX,
  1181. },
  1182. };
  1183. static void avs_component_hda_unregister_dais(struct snd_soc_component *component)
  1184. {
  1185. struct snd_soc_acpi_mach *mach;
  1186. struct snd_soc_dai *dai, *save;
  1187. struct hda_codec *codec;
  1188. char name[32];
  1189. mach = dev_get_platdata(component->card->dev);
  1190. codec = mach->pdata;
  1191. snprintf(name, sizeof(name), "%s-cpu", dev_name(&codec->core.dev));
  1192. for_each_component_dais_safe(component, dai, save) {
  1193. int stream;
  1194. if (!strstr(dai->driver->name, name))
  1195. continue;
  1196. for_each_pcm_streams(stream)
  1197. snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream));
  1198. snd_soc_unregister_dai(dai);
  1199. }
  1200. }
  1201. static int avs_component_hda_probe(struct snd_soc_component *component)
  1202. {
  1203. struct snd_soc_dapm_context *dapm;
  1204. struct snd_soc_dai_driver *dais;
  1205. struct snd_soc_acpi_mach *mach;
  1206. struct hda_codec *codec;
  1207. struct hda_pcm *pcm;
  1208. const char *cname;
  1209. int pcm_count = 0, ret, i;
  1210. mach = dev_get_platdata(component->card->dev);
  1211. if (!mach)
  1212. return -EINVAL;
  1213. codec = mach->pdata;
  1214. if (list_empty(&codec->pcm_list_head))
  1215. return -EINVAL;
  1216. list_for_each_entry(pcm, &codec->pcm_list_head, list)
  1217. pcm_count++;
  1218. dais = devm_kcalloc(component->dev, pcm_count, sizeof(*dais),
  1219. GFP_KERNEL);
  1220. if (!dais)
  1221. return -ENOMEM;
  1222. cname = dev_name(&codec->core.dev);
  1223. dapm = snd_soc_component_get_dapm(component);
  1224. pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
  1225. for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
  1226. struct snd_soc_dai *dai;
  1227. memcpy(&dais[i], &hda_cpu_dai, sizeof(*dais));
  1228. dais[i].id = i;
  1229. dais[i].name = devm_kasprintf(component->dev, GFP_KERNEL,
  1230. "%s-cpu%d", cname, i);
  1231. if (!dais[i].name) {
  1232. ret = -ENOMEM;
  1233. goto exit;
  1234. }
  1235. if (pcm->stream[0].substreams) {
  1236. dais[i].playback.stream_name =
  1237. devm_kasprintf(component->dev, GFP_KERNEL,
  1238. "%s-cpu%d Tx", cname, i);
  1239. if (!dais[i].playback.stream_name) {
  1240. ret = -ENOMEM;
  1241. goto exit;
  1242. }
  1243. if (!hda_codec_is_display(codec)) {
  1244. dais[i].playback.formats = pcm->stream[0].formats;
  1245. dais[i].playback.subformats = pcm->stream[0].subformats;
  1246. dais[i].playback.rates = pcm->stream[0].rates;
  1247. dais[i].playback.channels_min = pcm->stream[0].channels_min;
  1248. dais[i].playback.channels_max = pcm->stream[0].channels_max;
  1249. dais[i].playback.sig_bits = pcm->stream[0].maxbps;
  1250. }
  1251. }
  1252. if (pcm->stream[1].substreams) {
  1253. dais[i].capture.stream_name =
  1254. devm_kasprintf(component->dev, GFP_KERNEL,
  1255. "%s-cpu%d Rx", cname, i);
  1256. if (!dais[i].capture.stream_name) {
  1257. ret = -ENOMEM;
  1258. goto exit;
  1259. }
  1260. if (!hda_codec_is_display(codec)) {
  1261. dais[i].capture.formats = pcm->stream[1].formats;
  1262. dais[i].capture.subformats = pcm->stream[1].subformats;
  1263. dais[i].capture.rates = pcm->stream[1].rates;
  1264. dais[i].capture.channels_min = pcm->stream[1].channels_min;
  1265. dais[i].capture.channels_max = pcm->stream[1].channels_max;
  1266. dais[i].capture.sig_bits = pcm->stream[1].maxbps;
  1267. }
  1268. }
  1269. dai = snd_soc_register_dai(component, &dais[i], false);
  1270. if (!dai) {
  1271. dev_err(component->dev, "register dai for %s failed\n",
  1272. pcm->name);
  1273. ret = -EINVAL;
  1274. goto exit;
  1275. }
  1276. ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
  1277. if (ret < 0) {
  1278. dev_err(component->dev, "create widgets failed: %d\n",
  1279. ret);
  1280. goto exit;
  1281. }
  1282. }
  1283. ret = avs_component_probe(component);
  1284. exit:
  1285. if (ret)
  1286. avs_component_hda_unregister_dais(component);
  1287. return ret;
  1288. }
  1289. static void avs_component_hda_remove(struct snd_soc_component *component)
  1290. {
  1291. avs_component_hda_unregister_dais(component);
  1292. avs_component_remove(component);
  1293. }
  1294. static int avs_component_hda_open(struct snd_soc_component *component,
  1295. struct snd_pcm_substream *substream)
  1296. {
  1297. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  1298. if (!rtd->dai_link->no_pcm) {
  1299. struct snd_pcm_hardware hwparams = avs_pcm_hardware;
  1300. struct snd_soc_pcm_runtime *be;
  1301. struct snd_soc_dpcm *dpcm;
  1302. int dir = substream->stream;
  1303. /*
  1304. * Support the DPCM reparenting while still fulfilling expectations of HDAudio
  1305. * common code - a valid stream pointer at substream->runtime->private_data -
  1306. * by having all FEs point to the same private data.
  1307. */
  1308. for_each_dpcm_be(rtd, dir, dpcm) {
  1309. struct snd_pcm_substream *be_substream;
  1310. be = dpcm->be;
  1311. if (be->dpcm[dir].users == 1)
  1312. break;
  1313. be_substream = snd_soc_dpcm_get_substream(be, dir);
  1314. substream->runtime->private_data = be_substream->runtime->private_data;
  1315. break;
  1316. }
  1317. /* RESUME unsupported for de-coupled HD-Audio capture. */
  1318. if (dir == SNDRV_PCM_STREAM_CAPTURE)
  1319. hwparams.info &= ~SNDRV_PCM_INFO_RESUME;
  1320. return snd_soc_set_runtime_hwparams(substream, &hwparams);
  1321. }
  1322. return 0;
  1323. }
  1324. static const struct snd_soc_component_driver avs_hda_component_driver = {
  1325. .name = "avs-hda-pcm",
  1326. .probe = avs_component_hda_probe,
  1327. .remove = avs_component_hda_remove,
  1328. .suspend = avs_component_suspend,
  1329. .resume = avs_component_resume,
  1330. .open = avs_component_hda_open,
  1331. .pointer = avs_component_pointer,
  1332. .mmap = avs_component_mmap,
  1333. .pcm_construct = avs_component_construct,
  1334. /*
  1335. * hda platform component's probe() is dependent on
  1336. * codec->pcm_list_head, it needs to be initialized after codec
  1337. * component. remove_order is here for completeness sake
  1338. */
  1339. .probe_order = SND_SOC_COMP_ORDER_LATE,
  1340. .remove_order = SND_SOC_COMP_ORDER_EARLY,
  1341. .module_get_upon_open = 1,
  1342. .topology_name_prefix = "intel/avs",
  1343. };
  1344. int avs_hda_platform_register(struct avs_dev *adev, const char *name)
  1345. {
  1346. return avs_soc_component_register(adev->dev, name,
  1347. &avs_hda_component_driver, NULL, 0);
  1348. }