sof-audio.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2019 Intel Corporation
  7. //
  8. // Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
  9. //
  10. #include <linux/bitfield.h>
  11. #include <trace/events/sof.h>
  12. #include "sof-audio.h"
  13. #include "ops.h"
  14. static bool is_virtual_widget(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
  15. const char *func)
  16. {
  17. switch (widget->id) {
  18. case snd_soc_dapm_out_drv:
  19. case snd_soc_dapm_output:
  20. case snd_soc_dapm_input:
  21. dev_dbg(sdev->dev, "%s: %s is a virtual widget\n", func, widget->name);
  22. return true;
  23. default:
  24. return false;
  25. }
  26. }
  27. static void sof_reset_route_setup_status(struct snd_sof_dev *sdev, struct snd_sof_widget *widget)
  28. {
  29. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  30. struct snd_sof_route *sroute;
  31. list_for_each_entry(sroute, &sdev->route_list, list)
  32. if (sroute->src_widget == widget || sroute->sink_widget == widget) {
  33. if (sroute->setup && tplg_ops && tplg_ops->route_free)
  34. tplg_ops->route_free(sdev, sroute);
  35. sroute->setup = false;
  36. }
  37. }
  38. static int sof_widget_free_unlocked(struct snd_sof_dev *sdev,
  39. struct snd_sof_widget *swidget)
  40. {
  41. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  42. struct snd_sof_pipeline *spipe = swidget->spipe;
  43. int err = 0;
  44. int ret;
  45. if (!swidget->private)
  46. return 0;
  47. trace_sof_widget_free(swidget);
  48. /* only free when use_count is 0 */
  49. if (--swidget->use_count)
  50. return 0;
  51. /* reset route setup status for all routes that contain this widget */
  52. sof_reset_route_setup_status(sdev, swidget);
  53. /* free DAI config and continue to free widget even if it fails */
  54. if (WIDGET_IS_DAI(swidget->id)) {
  55. struct snd_sof_dai_config_data data;
  56. unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_FREE;
  57. data.dai_data = DMA_CHAN_INVALID;
  58. if (tplg_ops && tplg_ops->dai_config) {
  59. err = tplg_ops->dai_config(sdev, swidget, flags, &data);
  60. if (err < 0)
  61. dev_err(sdev->dev, "failed to free config for widget %s\n",
  62. swidget->widget->name);
  63. }
  64. }
  65. /* continue to disable core even if IPC fails */
  66. if (tplg_ops && tplg_ops->widget_free) {
  67. ret = tplg_ops->widget_free(sdev, swidget);
  68. if (ret < 0 && !err)
  69. err = ret;
  70. }
  71. /*
  72. * decrement ref count for cores associated with all modules in the pipeline and clear
  73. * the complete flag
  74. */
  75. if (swidget->id == snd_soc_dapm_scheduler) {
  76. int i;
  77. for_each_set_bit(i, &spipe->core_mask, sdev->num_cores) {
  78. ret = snd_sof_dsp_core_put(sdev, i);
  79. if (ret < 0) {
  80. dev_err(sdev->dev, "failed to disable target core: %d for pipeline %s\n",
  81. i, swidget->widget->name);
  82. if (!err)
  83. err = ret;
  84. }
  85. }
  86. swidget->spipe->complete = 0;
  87. }
  88. /*
  89. * free the scheduler widget (same as pipe_widget) associated with the current swidget.
  90. * skip for static pipelines
  91. */
  92. if (swidget->spipe && swidget->dynamic_pipeline_widget &&
  93. swidget->id != snd_soc_dapm_scheduler) {
  94. ret = sof_widget_free_unlocked(sdev, swidget->spipe->pipe_widget);
  95. if (ret < 0 && !err)
  96. err = ret;
  97. }
  98. if (!err)
  99. dev_dbg(sdev->dev, "widget %s freed\n", swidget->widget->name);
  100. return err;
  101. }
  102. int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
  103. {
  104. int ret;
  105. mutex_lock(&swidget->setup_mutex);
  106. ret = sof_widget_free_unlocked(sdev, swidget);
  107. mutex_unlock(&swidget->setup_mutex);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL(sof_widget_free);
  111. static int sof_widget_setup_unlocked(struct snd_sof_dev *sdev,
  112. struct snd_sof_widget *swidget)
  113. {
  114. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  115. struct snd_sof_pipeline *spipe = swidget->spipe;
  116. bool use_count_decremented = false;
  117. int ret;
  118. int i;
  119. /* skip if there is no private data */
  120. if (!swidget->private)
  121. return 0;
  122. trace_sof_widget_setup(swidget);
  123. /* widget already set up */
  124. if (++swidget->use_count > 1)
  125. return 0;
  126. /*
  127. * The scheduler widget for a pipeline is not part of the connected DAPM
  128. * widget list and it needs to be set up before the widgets in the pipeline
  129. * are set up. The use_count for the scheduler widget is incremented for every
  130. * widget in a given pipeline to ensure that it is freed only after the last
  131. * widget in the pipeline is freed. Skip setting up scheduler widget for static pipelines.
  132. */
  133. if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
  134. if (!swidget->spipe || !swidget->spipe->pipe_widget) {
  135. dev_err(sdev->dev, "No pipeline set for %s\n", swidget->widget->name);
  136. ret = -EINVAL;
  137. goto use_count_dec;
  138. }
  139. ret = sof_widget_setup_unlocked(sdev, swidget->spipe->pipe_widget);
  140. if (ret < 0)
  141. goto use_count_dec;
  142. }
  143. /* update ref count for cores associated with all modules in the pipeline */
  144. if (swidget->id == snd_soc_dapm_scheduler) {
  145. for_each_set_bit(i, &spipe->core_mask, sdev->num_cores) {
  146. ret = snd_sof_dsp_core_get(sdev, i);
  147. if (ret < 0) {
  148. dev_err(sdev->dev, "failed to enable target core %d for pipeline %s\n",
  149. i, swidget->widget->name);
  150. goto pipe_widget_free;
  151. }
  152. }
  153. }
  154. /* setup widget in the DSP */
  155. if (tplg_ops && tplg_ops->widget_setup) {
  156. ret = tplg_ops->widget_setup(sdev, swidget);
  157. if (ret < 0)
  158. goto pipe_widget_free;
  159. }
  160. /* send config for DAI components */
  161. if (WIDGET_IS_DAI(swidget->id)) {
  162. unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS;
  163. /*
  164. * The config flags saved during BE DAI hw_params will be used for IPC3. IPC4 does
  165. * not use the flags argument.
  166. */
  167. if (tplg_ops && tplg_ops->dai_config) {
  168. ret = tplg_ops->dai_config(sdev, swidget, flags, NULL);
  169. if (ret < 0)
  170. goto widget_free;
  171. }
  172. }
  173. /* restore kcontrols for widget */
  174. if (tplg_ops && tplg_ops->control && tplg_ops->control->widget_kcontrol_setup) {
  175. ret = tplg_ops->control->widget_kcontrol_setup(sdev, swidget);
  176. if (ret < 0)
  177. goto widget_free;
  178. }
  179. dev_dbg(sdev->dev, "widget %s setup complete\n", swidget->widget->name);
  180. return 0;
  181. widget_free:
  182. /* widget use_count will be decremented by sof_widget_free() */
  183. sof_widget_free_unlocked(sdev, swidget);
  184. use_count_decremented = true;
  185. pipe_widget_free:
  186. if (swidget->id != snd_soc_dapm_scheduler) {
  187. sof_widget_free_unlocked(sdev, swidget->spipe->pipe_widget);
  188. } else {
  189. int j;
  190. /* decrement ref count for all cores that were updated previously */
  191. for_each_set_bit(j, &spipe->core_mask, sdev->num_cores) {
  192. if (j >= i)
  193. break;
  194. snd_sof_dsp_core_put(sdev, j);
  195. }
  196. }
  197. use_count_dec:
  198. if (!use_count_decremented)
  199. swidget->use_count--;
  200. return ret;
  201. }
  202. int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
  203. {
  204. int ret;
  205. mutex_lock(&swidget->setup_mutex);
  206. ret = sof_widget_setup_unlocked(sdev, swidget);
  207. mutex_unlock(&swidget->setup_mutex);
  208. return ret;
  209. }
  210. EXPORT_SYMBOL(sof_widget_setup);
  211. int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource,
  212. struct snd_soc_dapm_widget *wsink)
  213. {
  214. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  215. struct snd_sof_widget *src_widget = wsource->dobj.private;
  216. struct snd_sof_widget *sink_widget = wsink->dobj.private;
  217. struct snd_sof_route *sroute;
  218. bool route_found = false;
  219. /* ignore routes involving virtual widgets in topology */
  220. if (is_virtual_widget(sdev, src_widget->widget, __func__) ||
  221. is_virtual_widget(sdev, sink_widget->widget, __func__))
  222. return 0;
  223. /* find route matching source and sink widgets */
  224. list_for_each_entry(sroute, &sdev->route_list, list)
  225. if (sroute->src_widget == src_widget && sroute->sink_widget == sink_widget) {
  226. route_found = true;
  227. break;
  228. }
  229. if (!route_found) {
  230. dev_err(sdev->dev, "error: cannot find SOF route for source %s -> %s sink\n",
  231. wsource->name, wsink->name);
  232. return -EINVAL;
  233. }
  234. /* nothing to do if route is already set up */
  235. if (sroute->setup)
  236. return 0;
  237. if (tplg_ops && tplg_ops->route_setup) {
  238. int ret = tplg_ops->route_setup(sdev, sroute);
  239. if (ret < 0)
  240. return ret;
  241. }
  242. sroute->setup = true;
  243. return 0;
  244. }
  245. static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
  246. struct snd_soc_dapm_widget_list *list, int dir)
  247. {
  248. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  249. struct snd_soc_dapm_widget *widget;
  250. struct snd_sof_route *sroute;
  251. struct snd_soc_dapm_path *p;
  252. int ret = 0;
  253. int i;
  254. /*
  255. * Set up connections between widgets in the sink/source paths based on direction.
  256. * Some non-SOF widgets exist in topology either for compatibility or for the
  257. * purpose of connecting a pipeline from a host to a DAI in order to receive the DAPM
  258. * events. But they are not handled by the firmware. So ignore them.
  259. */
  260. if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
  261. for_each_dapm_widgets(list, i, widget) {
  262. if (!widget->dobj.private)
  263. continue;
  264. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  265. if (!widget_in_list(list, p->sink))
  266. continue;
  267. if (p->sink->dobj.private) {
  268. ret = sof_route_setup(sdev, widget, p->sink);
  269. if (ret < 0)
  270. return ret;
  271. }
  272. }
  273. }
  274. } else {
  275. for_each_dapm_widgets(list, i, widget) {
  276. if (!widget->dobj.private)
  277. continue;
  278. snd_soc_dapm_widget_for_each_source_path(widget, p) {
  279. if (!widget_in_list(list, p->source))
  280. continue;
  281. if (p->source->dobj.private) {
  282. ret = sof_route_setup(sdev, p->source, widget);
  283. if (ret < 0)
  284. return ret;
  285. }
  286. }
  287. }
  288. }
  289. /*
  290. * The above loop handles connections between widgets that belong to the DAPM widget list.
  291. * This is not sufficient to handle loopback cases between pipelines configured with
  292. * different directions, e.g. a sidetone or an amplifier feedback connected to a speaker
  293. * protection module.
  294. */
  295. list_for_each_entry(sroute, &sdev->route_list, list) {
  296. bool src_widget_in_dapm_list, sink_widget_in_dapm_list;
  297. struct snd_sof_widget *swidget;
  298. if (sroute->setup)
  299. continue;
  300. src_widget_in_dapm_list = widget_in_list(list, sroute->src_widget->widget);
  301. sink_widget_in_dapm_list = widget_in_list(list, sroute->sink_widget->widget);
  302. /*
  303. * if both source and sink are in the DAPM list, the route must already have been
  304. * set up above. And if neither are in the DAPM list, the route shouldn't be
  305. * handled now.
  306. */
  307. if (src_widget_in_dapm_list == sink_widget_in_dapm_list)
  308. continue;
  309. /*
  310. * At this point either the source widget or the sink widget is in the DAPM list
  311. * with a route that might need to be set up. Check the use_count of the widget
  312. * that is not in the DAPM list to confirm if it is in use currently before setting
  313. * up the route.
  314. */
  315. if (src_widget_in_dapm_list)
  316. swidget = sroute->sink_widget;
  317. else
  318. swidget = sroute->src_widget;
  319. mutex_lock(&swidget->setup_mutex);
  320. if (!swidget->use_count) {
  321. mutex_unlock(&swidget->setup_mutex);
  322. continue;
  323. }
  324. if (tplg_ops && tplg_ops->route_setup) {
  325. /*
  326. * this route will get freed when either the source widget or the sink
  327. * widget is freed during hw_free
  328. */
  329. ret = tplg_ops->route_setup(sdev, sroute);
  330. if (!ret)
  331. sroute->setup = true;
  332. }
  333. mutex_unlock(&swidget->setup_mutex);
  334. if (ret < 0)
  335. return ret;
  336. }
  337. return 0;
  338. }
  339. static void
  340. sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
  341. struct snd_soc_dapm_widget_list *list)
  342. {
  343. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  344. struct snd_sof_widget *swidget = widget->dobj.private;
  345. const struct sof_ipc_tplg_widget_ops *widget_ops;
  346. struct snd_soc_dapm_path *p;
  347. if (is_virtual_widget(sdev, widget, __func__))
  348. return;
  349. /* skip if the widget is in use or if it is already unprepared */
  350. if (!swidget || !swidget->prepared || swidget->use_count > 0)
  351. goto sink_unprepare;
  352. widget_ops = tplg_ops ? tplg_ops->widget : NULL;
  353. if (widget_ops && widget_ops[widget->id].ipc_unprepare)
  354. /* unprepare the source widget */
  355. widget_ops[widget->id].ipc_unprepare(swidget);
  356. swidget->prepared = false;
  357. sink_unprepare:
  358. /* unprepare all widgets in the sink paths */
  359. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  360. if (!widget_in_list(list, p->sink))
  361. continue;
  362. if (!p->walking && p->sink->dobj.private) {
  363. p->walking = true;
  364. sof_unprepare_widgets_in_path(sdev, p->sink, list);
  365. p->walking = false;
  366. }
  367. }
  368. }
  369. static int
  370. sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
  371. struct snd_pcm_hw_params *fe_params,
  372. struct snd_sof_platform_stream_params *platform_params,
  373. struct snd_pcm_hw_params *pipeline_params, int dir,
  374. struct snd_soc_dapm_widget_list *list)
  375. {
  376. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  377. struct snd_sof_widget *swidget = widget->dobj.private;
  378. const struct sof_ipc_tplg_widget_ops *widget_ops;
  379. struct snd_soc_dapm_path *p;
  380. int ret;
  381. if (is_virtual_widget(sdev, widget, __func__))
  382. return 0;
  383. widget_ops = tplg_ops ? tplg_ops->widget : NULL;
  384. if (!widget_ops)
  385. return 0;
  386. if (!swidget || !widget_ops[widget->id].ipc_prepare || swidget->prepared)
  387. goto sink_prepare;
  388. /* prepare the source widget */
  389. ret = widget_ops[widget->id].ipc_prepare(swidget, fe_params, platform_params,
  390. pipeline_params, dir);
  391. if (ret < 0) {
  392. dev_err(sdev->dev, "failed to prepare widget %s\n", widget->name);
  393. return ret;
  394. }
  395. swidget->prepared = true;
  396. sink_prepare:
  397. /* prepare all widgets in the sink paths */
  398. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  399. if (!widget_in_list(list, p->sink))
  400. continue;
  401. if (!p->walking && p->sink->dobj.private) {
  402. p->walking = true;
  403. ret = sof_prepare_widgets_in_path(sdev, p->sink, fe_params,
  404. platform_params, pipeline_params, dir,
  405. list);
  406. p->walking = false;
  407. if (ret < 0) {
  408. /* unprepare the source widget */
  409. if (widget_ops[widget->id].ipc_unprepare &&
  410. swidget && swidget->prepared && swidget->use_count == 0) {
  411. widget_ops[widget->id].ipc_unprepare(swidget);
  412. swidget->prepared = false;
  413. }
  414. return ret;
  415. }
  416. }
  417. }
  418. return 0;
  419. }
  420. /*
  421. * free all widgets in the sink path starting from the source widget
  422. * (DAI type for capture, AIF type for playback)
  423. */
  424. static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
  425. int dir, struct snd_sof_pcm *spcm)
  426. {
  427. struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
  428. struct snd_soc_dapm_path *p;
  429. int err;
  430. int ret = 0;
  431. if (is_virtual_widget(sdev, widget, __func__))
  432. return 0;
  433. if (widget->dobj.private) {
  434. err = sof_widget_free(sdev, widget->dobj.private);
  435. if (err < 0)
  436. ret = err;
  437. }
  438. /* free all widgets in the sink paths even in case of error to keep use counts balanced */
  439. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  440. if (!p->walking) {
  441. if (!widget_in_list(list, p->sink))
  442. continue;
  443. p->walking = true;
  444. err = sof_free_widgets_in_path(sdev, p->sink, dir, spcm);
  445. if (err < 0)
  446. ret = err;
  447. p->walking = false;
  448. }
  449. }
  450. return ret;
  451. }
  452. /*
  453. * set up all widgets in the sink path starting from the source widget
  454. * (DAI type for capture, AIF type for playback).
  455. * The error path in this function ensures that all successfully set up widgets getting freed.
  456. */
  457. static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
  458. int dir, struct snd_sof_pcm *spcm)
  459. {
  460. struct snd_sof_pcm_stream_pipeline_list *pipeline_list = &spcm->stream[dir].pipeline_list;
  461. struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
  462. struct snd_sof_widget *swidget = widget->dobj.private;
  463. struct snd_sof_pipeline *spipe;
  464. struct snd_soc_dapm_path *p;
  465. int ret;
  466. if (is_virtual_widget(sdev, widget, __func__))
  467. return 0;
  468. if (swidget) {
  469. int i;
  470. ret = sof_widget_setup(sdev, widget->dobj.private);
  471. if (ret < 0)
  472. return ret;
  473. /* skip populating the pipe_widgets array if it is NULL */
  474. if (!pipeline_list->pipelines)
  475. goto sink_setup;
  476. /*
  477. * Add the widget's pipe_widget to the list of pipelines to be triggered if not
  478. * already in the list. This will result in the pipelines getting added in the
  479. * order source to sink.
  480. */
  481. for (i = 0; i < pipeline_list->count; i++) {
  482. spipe = pipeline_list->pipelines[i];
  483. if (spipe == swidget->spipe)
  484. break;
  485. }
  486. if (i == pipeline_list->count) {
  487. pipeline_list->count++;
  488. pipeline_list->pipelines[i] = swidget->spipe;
  489. }
  490. }
  491. sink_setup:
  492. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  493. if (!p->walking) {
  494. if (!widget_in_list(list, p->sink))
  495. continue;
  496. p->walking = true;
  497. ret = sof_set_up_widgets_in_path(sdev, p->sink, dir, spcm);
  498. p->walking = false;
  499. if (ret < 0) {
  500. if (swidget)
  501. sof_widget_free(sdev, swidget);
  502. return ret;
  503. }
  504. }
  505. }
  506. return 0;
  507. }
  508. static int
  509. sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
  510. struct snd_pcm_hw_params *fe_params,
  511. struct snd_sof_platform_stream_params *platform_params, int dir,
  512. enum sof_widget_op op)
  513. {
  514. struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
  515. struct snd_soc_dapm_widget *widget;
  516. char *str;
  517. int ret = 0;
  518. int i;
  519. if (!list)
  520. return 0;
  521. for_each_dapm_widgets(list, i, widget) {
  522. if (is_virtual_widget(sdev, widget, __func__))
  523. continue;
  524. /* starting widget for playback is AIF type */
  525. if (dir == SNDRV_PCM_STREAM_PLAYBACK && widget->id != snd_soc_dapm_aif_in)
  526. continue;
  527. /* starting widget for capture is DAI type */
  528. if (dir == SNDRV_PCM_STREAM_CAPTURE && widget->id != snd_soc_dapm_dai_out)
  529. continue;
  530. switch (op) {
  531. case SOF_WIDGET_SETUP:
  532. ret = sof_set_up_widgets_in_path(sdev, widget, dir, spcm);
  533. str = "set up";
  534. break;
  535. case SOF_WIDGET_FREE:
  536. ret = sof_free_widgets_in_path(sdev, widget, dir, spcm);
  537. str = "free";
  538. break;
  539. case SOF_WIDGET_PREPARE:
  540. {
  541. struct snd_pcm_hw_params pipeline_params;
  542. str = "prepare";
  543. /*
  544. * When walking the list of connected widgets, the pipeline_params for each
  545. * widget is modified by the source widget in the path. Use a local
  546. * copy of the runtime params as the pipeline_params so that the runtime
  547. * params does not get overwritten.
  548. */
  549. memcpy(&pipeline_params, fe_params, sizeof(*fe_params));
  550. ret = sof_prepare_widgets_in_path(sdev, widget, fe_params, platform_params,
  551. &pipeline_params, dir, list);
  552. break;
  553. }
  554. case SOF_WIDGET_UNPREPARE:
  555. sof_unprepare_widgets_in_path(sdev, widget, list);
  556. break;
  557. default:
  558. dev_err(sdev->dev, "Invalid widget op %d\n", op);
  559. return -EINVAL;
  560. }
  561. if (ret < 0) {
  562. dev_err(sdev->dev, "Failed to %s connected widgets\n", str);
  563. return ret;
  564. }
  565. }
  566. return 0;
  567. }
  568. int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
  569. struct snd_pcm_hw_params *fe_params,
  570. struct snd_sof_platform_stream_params *platform_params,
  571. int dir)
  572. {
  573. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  574. struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
  575. struct snd_soc_dapm_widget *widget;
  576. int i, ret;
  577. /* nothing to set up */
  578. if (!list)
  579. return 0;
  580. /*
  581. * Prepare widgets for set up. The prepare step is used to allocate memory, assign
  582. * instance ID and pick the widget configuration based on the runtime PCM params.
  583. */
  584. ret = sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
  585. dir, SOF_WIDGET_PREPARE);
  586. if (ret < 0)
  587. return ret;
  588. /* Set up is used to send the IPC to the DSP to create the widget */
  589. ret = sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
  590. dir, SOF_WIDGET_SETUP);
  591. if (ret < 0) {
  592. sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params,
  593. dir, SOF_WIDGET_UNPREPARE);
  594. return ret;
  595. }
  596. /*
  597. * error in setting pipeline connections will result in route status being reset for
  598. * routes that were successfully set up when the widgets are freed.
  599. */
  600. ret = sof_setup_pipeline_connections(sdev, list, dir);
  601. if (ret < 0)
  602. goto widget_free;
  603. /* complete pipelines */
  604. for_each_dapm_widgets(list, i, widget) {
  605. struct snd_sof_widget *swidget = widget->dobj.private;
  606. struct snd_sof_widget *pipe_widget;
  607. struct snd_sof_pipeline *spipe;
  608. if (!swidget || sdev->dspless_mode_selected)
  609. continue;
  610. spipe = swidget->spipe;
  611. if (!spipe) {
  612. dev_err(sdev->dev, "no pipeline found for %s\n",
  613. swidget->widget->name);
  614. ret = -EINVAL;
  615. goto widget_free;
  616. }
  617. pipe_widget = spipe->pipe_widget;
  618. if (!pipe_widget) {
  619. dev_err(sdev->dev, "error: no pipeline widget found for %s\n",
  620. swidget->widget->name);
  621. ret = -EINVAL;
  622. goto widget_free;
  623. }
  624. if (spipe->complete)
  625. continue;
  626. if (tplg_ops && tplg_ops->pipeline_complete) {
  627. spipe->complete = tplg_ops->pipeline_complete(sdev, pipe_widget);
  628. if (spipe->complete < 0) {
  629. ret = spipe->complete;
  630. goto widget_free;
  631. }
  632. }
  633. }
  634. return 0;
  635. widget_free:
  636. sof_walk_widgets_in_order(sdev, spcm, fe_params, platform_params, dir,
  637. SOF_WIDGET_FREE);
  638. sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
  639. return ret;
  640. }
  641. int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir)
  642. {
  643. struct snd_sof_pcm_stream_pipeline_list *pipeline_list = &spcm->stream[dir].pipeline_list;
  644. struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
  645. int ret;
  646. /* nothing to free */
  647. if (!list)
  648. return 0;
  649. /* send IPC to free widget in the DSP */
  650. ret = sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_FREE);
  651. /* unprepare the widget */
  652. sof_walk_widgets_in_order(sdev, spcm, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
  653. snd_soc_dapm_dai_free_widgets(&list);
  654. spcm->stream[dir].list = NULL;
  655. pipeline_list->count = 0;
  656. return ret;
  657. }
  658. /*
  659. * helper to determine if there are only D0i3 compatible
  660. * streams active
  661. */
  662. bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev)
  663. {
  664. struct snd_pcm_substream *substream;
  665. struct snd_sof_pcm *spcm;
  666. bool d0i3_compatible_active = false;
  667. int dir;
  668. list_for_each_entry(spcm, &sdev->pcm_list, list) {
  669. for_each_pcm_streams(dir) {
  670. substream = spcm->stream[dir].substream;
  671. if (!substream || !substream->runtime)
  672. continue;
  673. /*
  674. * substream->runtime being not NULL indicates
  675. * that the stream is open. No need to check the
  676. * stream state.
  677. */
  678. if (!spcm->stream[dir].d0i3_compatible)
  679. return false;
  680. d0i3_compatible_active = true;
  681. }
  682. }
  683. return d0i3_compatible_active;
  684. }
  685. EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active);
  686. bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev)
  687. {
  688. struct snd_sof_pcm *spcm;
  689. list_for_each_entry(spcm, &sdev->pcm_list, list) {
  690. if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].suspend_ignored ||
  691. spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored)
  692. return true;
  693. }
  694. return false;
  695. }
  696. int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
  697. struct snd_sof_pcm *spcm, int dir, bool free_widget_list)
  698. {
  699. const struct sof_ipc_pcm_ops *pcm_ops = sof_ipc_get_ops(sdev, pcm);
  700. int ret;
  701. int err = 0;
  702. if (spcm->prepared[substream->stream]) {
  703. /* stop DMA first if needed */
  704. if (pcm_ops && pcm_ops->platform_stop_during_hw_free)
  705. snd_sof_pcm_platform_trigger(sdev, substream, SNDRV_PCM_TRIGGER_STOP);
  706. /* free PCM in the DSP */
  707. if (pcm_ops && pcm_ops->hw_free) {
  708. ret = pcm_ops->hw_free(sdev->component, substream);
  709. if (ret < 0) {
  710. dev_err(sdev->dev, "%s: pcm_ops hw_free failed %d\n",
  711. __func__, ret);
  712. err = ret;
  713. }
  714. }
  715. spcm->prepared[substream->stream] = false;
  716. spcm->pending_stop[substream->stream] = false;
  717. }
  718. /* reset the DMA */
  719. ret = snd_sof_pcm_platform_hw_free(sdev, substream);
  720. if (ret < 0) {
  721. dev_err(sdev->dev, "%s: platform hw free failed %d\n",
  722. __func__, ret);
  723. if (!err)
  724. err = ret;
  725. }
  726. /* free widget list */
  727. if (free_widget_list) {
  728. ret = sof_widget_list_free(sdev, spcm, dir);
  729. if (ret < 0) {
  730. dev_err(sdev->dev, "%s: sof_widget_list_free failed %d\n",
  731. __func__, ret);
  732. if (!err)
  733. err = ret;
  734. }
  735. }
  736. return err;
  737. }
  738. /*
  739. * Generic object lookup APIs.
  740. */
  741. struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
  742. const char *name)
  743. {
  744. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  745. struct snd_sof_pcm *spcm;
  746. list_for_each_entry(spcm, &sdev->pcm_list, list) {
  747. /* match with PCM dai name */
  748. if (strcmp(spcm->pcm.dai_name, name) == 0)
  749. return spcm;
  750. /* match with playback caps name if set */
  751. if (*spcm->pcm.caps[0].name &&
  752. !strcmp(spcm->pcm.caps[0].name, name))
  753. return spcm;
  754. /* match with capture caps name if set */
  755. if (*spcm->pcm.caps[1].name &&
  756. !strcmp(spcm->pcm.caps[1].name, name))
  757. return spcm;
  758. }
  759. return NULL;
  760. }
  761. struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
  762. unsigned int comp_id,
  763. int *direction)
  764. {
  765. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  766. struct snd_sof_pcm *spcm;
  767. int dir;
  768. list_for_each_entry(spcm, &sdev->pcm_list, list) {
  769. for_each_pcm_streams(dir) {
  770. if (spcm->stream[dir].comp_id == comp_id) {
  771. *direction = dir;
  772. return spcm;
  773. }
  774. }
  775. }
  776. return NULL;
  777. }
  778. struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
  779. const char *name)
  780. {
  781. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  782. struct snd_sof_widget *swidget;
  783. list_for_each_entry(swidget, &sdev->widget_list, list) {
  784. if (strcmp(name, swidget->widget->name) == 0)
  785. return swidget;
  786. }
  787. return NULL;
  788. }
  789. /* find widget by stream name and direction */
  790. struct snd_sof_widget *
  791. snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
  792. const char *pcm_name, int dir)
  793. {
  794. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  795. struct snd_sof_widget *swidget;
  796. enum snd_soc_dapm_type type;
  797. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  798. type = snd_soc_dapm_aif_in;
  799. else
  800. type = snd_soc_dapm_aif_out;
  801. list_for_each_entry(swidget, &sdev->widget_list, list) {
  802. if (!strcmp(pcm_name, swidget->widget->sname) &&
  803. swidget->id == type)
  804. return swidget;
  805. }
  806. return NULL;
  807. }
  808. struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
  809. const char *name)
  810. {
  811. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  812. struct snd_sof_dai *dai;
  813. list_for_each_entry(dai, &sdev->dai_list, list) {
  814. if (dai->name && (strcmp(name, dai->name) == 0))
  815. return dai;
  816. }
  817. return NULL;
  818. }
  819. static int sof_dai_get_param(struct snd_soc_pcm_runtime *rtd, int param_type)
  820. {
  821. struct snd_soc_component *component =
  822. snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  823. struct snd_sof_dai *dai =
  824. snd_sof_find_dai(component, (char *)rtd->dai_link->name);
  825. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  826. const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
  827. /* use the tplg configured mclk if existed */
  828. if (!dai)
  829. return 0;
  830. if (tplg_ops && tplg_ops->dai_get_param)
  831. return tplg_ops->dai_get_param(sdev, dai, param_type);
  832. return 0;
  833. }
  834. /*
  835. * Helper to get SSP MCLK from a pcm_runtime.
  836. * Return 0 if not exist.
  837. */
  838. int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd)
  839. {
  840. return sof_dai_get_param(rtd, SOF_DAI_PARAM_INTEL_SSP_MCLK);
  841. }
  842. EXPORT_SYMBOL(sof_dai_get_mclk);
  843. /*
  844. * Helper to get SSP BCLK from a pcm_runtime.
  845. * Return 0 if not exist.
  846. */
  847. int sof_dai_get_bclk(struct snd_soc_pcm_runtime *rtd)
  848. {
  849. return sof_dai_get_param(rtd, SOF_DAI_PARAM_INTEL_SSP_BCLK);
  850. }
  851. EXPORT_SYMBOL(sof_dai_get_bclk);
  852. /*
  853. * Helper to get SSP TDM slot number from a pcm_runtime.
  854. * Return 0 if not exist.
  855. */
  856. int sof_dai_get_tdm_slots(struct snd_soc_pcm_runtime *rtd)
  857. {
  858. return sof_dai_get_param(rtd, SOF_DAI_PARAM_INTEL_SSP_TDM_SLOTS);
  859. }
  860. EXPORT_SYMBOL(sof_dai_get_tdm_slots);