sof-audio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. All rights reserved.
  7. //
  8. // Author: Ranjani Sridharan <[email protected]>
  9. //
  10. #include <linux/bitfield.h>
  11. #include <trace/events/sof.h>
  12. #include "sof-audio.h"
  13. #include "sof-of-dev.h"
  14. #include "ops.h"
  15. static void sof_reset_route_setup_status(struct snd_sof_dev *sdev, struct snd_sof_widget *widget)
  16. {
  17. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  18. struct snd_sof_route *sroute;
  19. list_for_each_entry(sroute, &sdev->route_list, list)
  20. if (sroute->src_widget == widget || sroute->sink_widget == widget) {
  21. if (sroute->setup && tplg_ops->route_free)
  22. tplg_ops->route_free(sdev, sroute);
  23. sroute->setup = false;
  24. }
  25. }
  26. int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
  27. {
  28. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  29. int err = 0;
  30. int ret;
  31. if (!swidget->private)
  32. return 0;
  33. trace_sof_widget_free(swidget);
  34. /* only free when use_count is 0 */
  35. if (--swidget->use_count)
  36. return 0;
  37. /* reset route setup status for all routes that contain this widget */
  38. sof_reset_route_setup_status(sdev, swidget);
  39. /* continue to disable core even if IPC fails */
  40. if (tplg_ops->widget_free)
  41. err = tplg_ops->widget_free(sdev, swidget);
  42. /*
  43. * disable widget core. continue to route setup status and complete flag
  44. * even if this fails and return the appropriate error
  45. */
  46. ret = snd_sof_dsp_core_put(sdev, swidget->core);
  47. if (ret < 0) {
  48. dev_err(sdev->dev, "error: failed to disable target core: %d for widget %s\n",
  49. swidget->core, swidget->widget->name);
  50. if (!err)
  51. err = ret;
  52. }
  53. /*
  54. * free the scheduler widget (same as pipe_widget) associated with the current swidget.
  55. * skip for static pipelines
  56. */
  57. if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
  58. ret = sof_widget_free(sdev, swidget->pipe_widget);
  59. if (ret < 0 && !err)
  60. err = ret;
  61. swidget->pipe_widget->complete = 0;
  62. }
  63. if (!err)
  64. dev_dbg(sdev->dev, "widget %s freed\n", swidget->widget->name);
  65. return err;
  66. }
  67. EXPORT_SYMBOL(sof_widget_free);
  68. int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
  69. {
  70. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  71. int ret;
  72. /* skip if there is no private data */
  73. if (!swidget->private)
  74. return 0;
  75. trace_sof_widget_setup(swidget);
  76. /* widget already set up */
  77. if (++swidget->use_count > 1)
  78. return 0;
  79. /*
  80. * The scheduler widget for a pipeline is not part of the connected DAPM
  81. * widget list and it needs to be set up before the widgets in the pipeline
  82. * are set up. The use_count for the scheduler widget is incremented for every
  83. * widget in a given pipeline to ensure that it is freed only after the last
  84. * widget in the pipeline is freed. Skip setting up scheduler widget for static pipelines.
  85. */
  86. if (swidget->dynamic_pipeline_widget && swidget->id != snd_soc_dapm_scheduler) {
  87. if (!swidget->pipe_widget) {
  88. dev_err(sdev->dev, "No scheduler widget set for %s\n",
  89. swidget->widget->name);
  90. ret = -EINVAL;
  91. goto use_count_dec;
  92. }
  93. ret = sof_widget_setup(sdev, swidget->pipe_widget);
  94. if (ret < 0)
  95. goto use_count_dec;
  96. }
  97. /* enable widget core */
  98. ret = snd_sof_dsp_core_get(sdev, swidget->core);
  99. if (ret < 0) {
  100. dev_err(sdev->dev, "error: failed to enable target core for widget %s\n",
  101. swidget->widget->name);
  102. goto pipe_widget_free;
  103. }
  104. /* setup widget in the DSP */
  105. if (tplg_ops->widget_setup) {
  106. ret = tplg_ops->widget_setup(sdev, swidget);
  107. if (ret < 0)
  108. goto core_put;
  109. }
  110. /* send config for DAI components */
  111. if (WIDGET_IS_DAI(swidget->id)) {
  112. unsigned int flags = SOF_DAI_CONFIG_FLAGS_NONE;
  113. if (tplg_ops->dai_config) {
  114. ret = tplg_ops->dai_config(sdev, swidget, flags, NULL);
  115. if (ret < 0)
  116. goto widget_free;
  117. }
  118. }
  119. /* restore kcontrols for widget */
  120. if (tplg_ops->control->widget_kcontrol_setup) {
  121. ret = tplg_ops->control->widget_kcontrol_setup(sdev, swidget);
  122. if (ret < 0)
  123. goto widget_free;
  124. }
  125. dev_dbg(sdev->dev, "widget %s setup complete\n", swidget->widget->name);
  126. return 0;
  127. widget_free:
  128. /* widget use_count and core ref_count will both be decremented by sof_widget_free() */
  129. sof_widget_free(sdev, swidget);
  130. core_put:
  131. snd_sof_dsp_core_put(sdev, swidget->core);
  132. pipe_widget_free:
  133. if (swidget->id != snd_soc_dapm_scheduler)
  134. sof_widget_free(sdev, swidget->pipe_widget);
  135. use_count_dec:
  136. swidget->use_count--;
  137. return ret;
  138. }
  139. EXPORT_SYMBOL(sof_widget_setup);
  140. int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource,
  141. struct snd_soc_dapm_widget *wsink)
  142. {
  143. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  144. struct snd_sof_widget *src_widget = wsource->dobj.private;
  145. struct snd_sof_widget *sink_widget = wsink->dobj.private;
  146. struct snd_sof_route *sroute;
  147. bool route_found = false;
  148. int ret;
  149. /* ignore routes involving virtual widgets in topology */
  150. switch (src_widget->id) {
  151. case snd_soc_dapm_out_drv:
  152. case snd_soc_dapm_output:
  153. case snd_soc_dapm_input:
  154. return 0;
  155. default:
  156. break;
  157. }
  158. switch (sink_widget->id) {
  159. case snd_soc_dapm_out_drv:
  160. case snd_soc_dapm_output:
  161. case snd_soc_dapm_input:
  162. return 0;
  163. default:
  164. break;
  165. }
  166. /* find route matching source and sink widgets */
  167. list_for_each_entry(sroute, &sdev->route_list, list)
  168. if (sroute->src_widget == src_widget && sroute->sink_widget == sink_widget) {
  169. route_found = true;
  170. break;
  171. }
  172. if (!route_found) {
  173. dev_err(sdev->dev, "error: cannot find SOF route for source %s -> %s sink\n",
  174. wsource->name, wsink->name);
  175. return -EINVAL;
  176. }
  177. /* nothing to do if route is already set up */
  178. if (sroute->setup)
  179. return 0;
  180. ret = ipc_tplg_ops->route_setup(sdev, sroute);
  181. if (ret < 0)
  182. return ret;
  183. sroute->setup = true;
  184. return 0;
  185. }
  186. static int sof_setup_pipeline_connections(struct snd_sof_dev *sdev,
  187. struct snd_soc_dapm_widget_list *list, int dir)
  188. {
  189. struct snd_soc_dapm_widget *widget;
  190. struct snd_soc_dapm_path *p;
  191. int ret;
  192. int i;
  193. /*
  194. * Set up connections between widgets in the sink/source paths based on direction.
  195. * Some non-SOF widgets exist in topology either for compatibility or for the
  196. * purpose of connecting a pipeline from a host to a DAI in order to receive the DAPM
  197. * events. But they are not handled by the firmware. So ignore them.
  198. */
  199. if (dir == SNDRV_PCM_STREAM_PLAYBACK) {
  200. for_each_dapm_widgets(list, i, widget) {
  201. if (!widget->dobj.private)
  202. continue;
  203. snd_soc_dapm_widget_for_each_sink_path(widget, p)
  204. if (p->sink->dobj.private) {
  205. ret = sof_route_setup(sdev, widget, p->sink);
  206. if (ret < 0)
  207. return ret;
  208. }
  209. }
  210. } else {
  211. for_each_dapm_widgets(list, i, widget) {
  212. if (!widget->dobj.private)
  213. continue;
  214. snd_soc_dapm_widget_for_each_source_path(widget, p)
  215. if (p->source->dobj.private) {
  216. ret = sof_route_setup(sdev, p->source, widget);
  217. if (ret < 0)
  218. return ret;
  219. }
  220. }
  221. }
  222. return 0;
  223. }
  224. static void
  225. sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget)
  226. {
  227. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  228. const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
  229. struct snd_sof_widget *swidget = widget->dobj.private;
  230. struct snd_soc_dapm_path *p;
  231. /* skip if the widget is in use or if it is already unprepared */
  232. if (!swidget || !swidget->prepared || swidget->use_count > 0)
  233. goto sink_unprepare;
  234. if (widget_ops[widget->id].ipc_unprepare)
  235. /* unprepare the source widget */
  236. widget_ops[widget->id].ipc_unprepare(swidget);
  237. swidget->prepared = false;
  238. sink_unprepare:
  239. /* unprepare all widgets in the sink paths */
  240. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  241. if (!p->walking && p->sink->dobj.private) {
  242. p->walking = true;
  243. sof_unprepare_widgets_in_path(sdev, p->sink);
  244. p->walking = false;
  245. }
  246. }
  247. }
  248. static int
  249. sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
  250. struct snd_pcm_hw_params *fe_params,
  251. struct snd_sof_platform_stream_params *platform_params,
  252. struct snd_pcm_hw_params *pipeline_params, int dir)
  253. {
  254. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  255. const struct sof_ipc_tplg_widget_ops *widget_ops = ipc_tplg_ops->widget;
  256. struct snd_sof_widget *swidget = widget->dobj.private;
  257. struct snd_soc_dapm_path *p;
  258. int ret;
  259. if (!swidget || !widget_ops[widget->id].ipc_prepare || swidget->prepared)
  260. goto sink_prepare;
  261. /* prepare the source widget */
  262. ret = widget_ops[widget->id].ipc_prepare(swidget, fe_params, platform_params,
  263. pipeline_params, dir);
  264. if (ret < 0) {
  265. dev_err(sdev->dev, "failed to prepare widget %s\n", widget->name);
  266. return ret;
  267. }
  268. swidget->prepared = true;
  269. sink_prepare:
  270. /* prepare all widgets in the sink paths */
  271. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  272. if (!p->walking && p->sink->dobj.private) {
  273. p->walking = true;
  274. ret = sof_prepare_widgets_in_path(sdev, p->sink, fe_params,
  275. platform_params, pipeline_params, dir);
  276. p->walking = false;
  277. if (ret < 0) {
  278. /* unprepare the source widget */
  279. if (widget_ops[widget->id].ipc_unprepare &&
  280. swidget && swidget->prepared) {
  281. widget_ops[widget->id].ipc_unprepare(swidget);
  282. swidget->prepared = false;
  283. }
  284. return ret;
  285. }
  286. }
  287. }
  288. return 0;
  289. }
  290. /*
  291. * free all widgets in the sink path starting from the source widget
  292. * (DAI type for capture, AIF type for playback)
  293. */
  294. static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
  295. int dir)
  296. {
  297. struct snd_soc_dapm_path *p;
  298. int err;
  299. int ret = 0;
  300. /* free all widgets even in case of error to keep use counts balanced */
  301. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  302. if (!p->walking && p->sink->dobj.private && widget->dobj.private) {
  303. p->walking = true;
  304. if (WIDGET_IS_AIF_OR_DAI(widget->id)) {
  305. err = sof_widget_free(sdev, widget->dobj.private);
  306. if (err < 0)
  307. ret = err;
  308. }
  309. err = sof_widget_free(sdev, p->sink->dobj.private);
  310. if (err < 0)
  311. ret = err;
  312. err = sof_free_widgets_in_path(sdev, p->sink, dir);
  313. if (err < 0)
  314. ret = err;
  315. p->walking = false;
  316. }
  317. }
  318. return ret;
  319. }
  320. /*
  321. * set up all widgets in the sink path starting from the source widget
  322. * (DAI type for capture, AIF type for playback).
  323. * The error path in this function ensures that all successfully set up widgets getting freed.
  324. */
  325. static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *widget,
  326. int dir)
  327. {
  328. struct snd_soc_dapm_path *p;
  329. int ret;
  330. snd_soc_dapm_widget_for_each_sink_path(widget, p) {
  331. if (!p->walking && p->sink->dobj.private && widget->dobj.private) {
  332. p->walking = true;
  333. if (WIDGET_IS_AIF_OR_DAI(widget->id)) {
  334. ret = sof_widget_setup(sdev, widget->dobj.private);
  335. if (ret < 0)
  336. goto out;
  337. }
  338. ret = sof_widget_setup(sdev, p->sink->dobj.private);
  339. if (ret < 0) {
  340. if (WIDGET_IS_AIF_OR_DAI(widget->id))
  341. sof_widget_free(sdev, widget->dobj.private);
  342. goto out;
  343. }
  344. ret = sof_set_up_widgets_in_path(sdev, p->sink, dir);
  345. if (ret < 0) {
  346. if (WIDGET_IS_AIF_OR_DAI(widget->id))
  347. sof_widget_free(sdev, widget->dobj.private);
  348. sof_widget_free(sdev, p->sink->dobj.private);
  349. }
  350. out:
  351. p->walking = false;
  352. if (ret < 0)
  353. return ret;
  354. }
  355. }
  356. return 0;
  357. }
  358. static int
  359. sof_walk_widgets_in_order(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget_list *list,
  360. struct snd_pcm_hw_params *fe_params,
  361. struct snd_sof_platform_stream_params *platform_params, int dir,
  362. enum sof_widget_op op)
  363. {
  364. struct snd_soc_dapm_widget *widget;
  365. char *str;
  366. int ret = 0;
  367. int i;
  368. for_each_dapm_widgets(list, i, widget) {
  369. /* starting widget for playback is AIF type */
  370. if (dir == SNDRV_PCM_STREAM_PLAYBACK && widget->id != snd_soc_dapm_aif_in)
  371. continue;
  372. /* starting widget for capture is DAI type */
  373. if (dir == SNDRV_PCM_STREAM_CAPTURE && widget->id != snd_soc_dapm_dai_out)
  374. continue;
  375. switch (op) {
  376. case SOF_WIDGET_SETUP:
  377. ret = sof_set_up_widgets_in_path(sdev, widget, dir);
  378. str = "set up";
  379. break;
  380. case SOF_WIDGET_FREE:
  381. ret = sof_free_widgets_in_path(sdev, widget, dir);
  382. str = "free";
  383. break;
  384. case SOF_WIDGET_PREPARE:
  385. {
  386. struct snd_pcm_hw_params pipeline_params;
  387. str = "prepare";
  388. /*
  389. * When walking the list of connected widgets, the pipeline_params for each
  390. * widget is modified by the source widget in the path. Use a local
  391. * copy of the runtime params as the pipeline_params so that the runtime
  392. * params does not get overwritten.
  393. */
  394. memcpy(&pipeline_params, fe_params, sizeof(*fe_params));
  395. ret = sof_prepare_widgets_in_path(sdev, widget, fe_params,
  396. platform_params, &pipeline_params, dir);
  397. break;
  398. }
  399. case SOF_WIDGET_UNPREPARE:
  400. sof_unprepare_widgets_in_path(sdev, widget);
  401. break;
  402. default:
  403. dev_err(sdev->dev, "Invalid widget op %d\n", op);
  404. return -EINVAL;
  405. }
  406. if (ret < 0) {
  407. dev_err(sdev->dev, "Failed to %s connected widgets\n", str);
  408. return ret;
  409. }
  410. }
  411. return 0;
  412. }
  413. int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
  414. struct snd_pcm_hw_params *fe_params,
  415. struct snd_sof_platform_stream_params *platform_params,
  416. int dir)
  417. {
  418. const struct sof_ipc_tplg_ops *ipc_tplg_ops = sdev->ipc->ops->tplg;
  419. struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
  420. struct snd_soc_dapm_widget *widget;
  421. int i, ret;
  422. /* nothing to set up */
  423. if (!list)
  424. return 0;
  425. /*
  426. * Prepare widgets for set up. The prepare step is used to allocate memory, assign
  427. * instance ID and pick the widget configuration based on the runtime PCM params.
  428. */
  429. ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
  430. dir, SOF_WIDGET_PREPARE);
  431. if (ret < 0)
  432. return ret;
  433. /* Set up is used to send the IPC to the DSP to create the widget */
  434. ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
  435. dir, SOF_WIDGET_SETUP);
  436. if (ret < 0) {
  437. ret = sof_walk_widgets_in_order(sdev, list, fe_params, platform_params,
  438. dir, SOF_WIDGET_UNPREPARE);
  439. return ret;
  440. }
  441. /*
  442. * error in setting pipeline connections will result in route status being reset for
  443. * routes that were successfully set up when the widgets are freed.
  444. */
  445. ret = sof_setup_pipeline_connections(sdev, list, dir);
  446. if (ret < 0)
  447. goto widget_free;
  448. /* complete pipelines */
  449. for_each_dapm_widgets(list, i, widget) {
  450. struct snd_sof_widget *swidget = widget->dobj.private;
  451. struct snd_sof_widget *pipe_widget;
  452. if (!swidget)
  453. continue;
  454. pipe_widget = swidget->pipe_widget;
  455. if (!pipe_widget) {
  456. dev_err(sdev->dev, "error: no pipeline widget found for %s\n",
  457. swidget->widget->name);
  458. ret = -EINVAL;
  459. goto widget_free;
  460. }
  461. if (pipe_widget->complete)
  462. continue;
  463. if (ipc_tplg_ops->pipeline_complete) {
  464. pipe_widget->complete = ipc_tplg_ops->pipeline_complete(sdev, pipe_widget);
  465. if (pipe_widget->complete < 0) {
  466. ret = pipe_widget->complete;
  467. goto widget_free;
  468. }
  469. }
  470. }
  471. return 0;
  472. widget_free:
  473. sof_walk_widgets_in_order(sdev, list, fe_params, platform_params, dir,
  474. SOF_WIDGET_FREE);
  475. sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
  476. return ret;
  477. }
  478. int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir)
  479. {
  480. struct snd_soc_dapm_widget_list *list = spcm->stream[dir].list;
  481. int ret;
  482. /* nothing to free */
  483. if (!list)
  484. return 0;
  485. /* send IPC to free widget in the DSP */
  486. ret = sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_FREE);
  487. /* unprepare the widget */
  488. sof_walk_widgets_in_order(sdev, list, NULL, NULL, dir, SOF_WIDGET_UNPREPARE);
  489. snd_soc_dapm_dai_free_widgets(&list);
  490. spcm->stream[dir].list = NULL;
  491. return ret;
  492. }
  493. /*
  494. * helper to determine if there are only D0i3 compatible
  495. * streams active
  496. */
  497. bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev)
  498. {
  499. struct snd_pcm_substream *substream;
  500. struct snd_sof_pcm *spcm;
  501. bool d0i3_compatible_active = false;
  502. int dir;
  503. list_for_each_entry(spcm, &sdev->pcm_list, list) {
  504. for_each_pcm_streams(dir) {
  505. substream = spcm->stream[dir].substream;
  506. if (!substream || !substream->runtime)
  507. continue;
  508. /*
  509. * substream->runtime being not NULL indicates
  510. * that the stream is open. No need to check the
  511. * stream state.
  512. */
  513. if (!spcm->stream[dir].d0i3_compatible)
  514. return false;
  515. d0i3_compatible_active = true;
  516. }
  517. }
  518. return d0i3_compatible_active;
  519. }
  520. EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active);
  521. bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev)
  522. {
  523. struct snd_sof_pcm *spcm;
  524. list_for_each_entry(spcm, &sdev->pcm_list, list) {
  525. if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].suspend_ignored ||
  526. spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored)
  527. return true;
  528. }
  529. return false;
  530. }
  531. int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
  532. struct snd_sof_pcm *spcm, int dir, bool free_widget_list)
  533. {
  534. const struct sof_ipc_pcm_ops *pcm_ops = sdev->ipc->ops->pcm;
  535. int ret;
  536. /* Send PCM_FREE IPC to reset pipeline */
  537. if (pcm_ops->hw_free && spcm->prepared[substream->stream]) {
  538. ret = pcm_ops->hw_free(sdev->component, substream);
  539. if (ret < 0)
  540. return ret;
  541. }
  542. spcm->prepared[substream->stream] = false;
  543. /* stop the DMA */
  544. ret = snd_sof_pcm_platform_hw_free(sdev, substream);
  545. if (ret < 0)
  546. return ret;
  547. /* free widget list */
  548. if (free_widget_list) {
  549. ret = sof_widget_list_free(sdev, spcm, dir);
  550. if (ret < 0)
  551. dev_err(sdev->dev, "failed to free widgets during suspend\n");
  552. }
  553. return ret;
  554. }
  555. /*
  556. * Generic object lookup APIs.
  557. */
  558. struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
  559. const char *name)
  560. {
  561. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  562. struct snd_sof_pcm *spcm;
  563. list_for_each_entry(spcm, &sdev->pcm_list, list) {
  564. /* match with PCM dai name */
  565. if (strcmp(spcm->pcm.dai_name, name) == 0)
  566. return spcm;
  567. /* match with playback caps name if set */
  568. if (*spcm->pcm.caps[0].name &&
  569. !strcmp(spcm->pcm.caps[0].name, name))
  570. return spcm;
  571. /* match with capture caps name if set */
  572. if (*spcm->pcm.caps[1].name &&
  573. !strcmp(spcm->pcm.caps[1].name, name))
  574. return spcm;
  575. }
  576. return NULL;
  577. }
  578. struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
  579. unsigned int comp_id,
  580. int *direction)
  581. {
  582. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  583. struct snd_sof_pcm *spcm;
  584. int dir;
  585. list_for_each_entry(spcm, &sdev->pcm_list, list) {
  586. for_each_pcm_streams(dir) {
  587. if (spcm->stream[dir].comp_id == comp_id) {
  588. *direction = dir;
  589. return spcm;
  590. }
  591. }
  592. }
  593. return NULL;
  594. }
  595. struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
  596. const char *name)
  597. {
  598. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  599. struct snd_sof_widget *swidget;
  600. list_for_each_entry(swidget, &sdev->widget_list, list) {
  601. if (strcmp(name, swidget->widget->name) == 0)
  602. return swidget;
  603. }
  604. return NULL;
  605. }
  606. /* find widget by stream name and direction */
  607. struct snd_sof_widget *
  608. snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
  609. const char *pcm_name, int dir)
  610. {
  611. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  612. struct snd_sof_widget *swidget;
  613. enum snd_soc_dapm_type type;
  614. if (dir == SNDRV_PCM_STREAM_PLAYBACK)
  615. type = snd_soc_dapm_aif_in;
  616. else
  617. type = snd_soc_dapm_aif_out;
  618. list_for_each_entry(swidget, &sdev->widget_list, list) {
  619. if (!strcmp(pcm_name, swidget->widget->sname) &&
  620. swidget->id == type)
  621. return swidget;
  622. }
  623. return NULL;
  624. }
  625. struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
  626. const char *name)
  627. {
  628. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  629. struct snd_sof_dai *dai;
  630. list_for_each_entry(dai, &sdev->dai_list, list) {
  631. if (dai->name && (strcmp(name, dai->name) == 0))
  632. return dai;
  633. }
  634. return NULL;
  635. }
  636. static int sof_dai_get_clk(struct snd_soc_pcm_runtime *rtd, int clk_type)
  637. {
  638. struct snd_soc_component *component =
  639. snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  640. struct snd_sof_dai *dai =
  641. snd_sof_find_dai(component, (char *)rtd->dai_link->name);
  642. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  643. const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
  644. /* use the tplg configured mclk if existed */
  645. if (!dai)
  646. return 0;
  647. if (tplg_ops->dai_get_clk)
  648. return tplg_ops->dai_get_clk(sdev, dai, clk_type);
  649. return 0;
  650. }
  651. /*
  652. * Helper to get SSP MCLK from a pcm_runtime.
  653. * Return 0 if not exist.
  654. */
  655. int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd)
  656. {
  657. return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_MCLK);
  658. }
  659. EXPORT_SYMBOL(sof_dai_get_mclk);
  660. /*
  661. * Helper to get SSP BCLK from a pcm_runtime.
  662. * Return 0 if not exist.
  663. */
  664. int sof_dai_get_bclk(struct snd_soc_pcm_runtime *rtd)
  665. {
  666. return sof_dai_get_clk(rtd, SOF_DAI_CLK_INTEL_SSP_BCLK);
  667. }
  668. EXPORT_SYMBOL(sof_dai_get_bclk);
  669. static struct snd_sof_of_mach *sof_of_machine_select(struct snd_sof_dev *sdev)
  670. {
  671. struct snd_sof_pdata *sof_pdata = sdev->pdata;
  672. const struct sof_dev_desc *desc = sof_pdata->desc;
  673. struct snd_sof_of_mach *mach = desc->of_machines;
  674. if (!mach)
  675. return NULL;
  676. for (; mach->compatible; mach++) {
  677. if (of_machine_is_compatible(mach->compatible)) {
  678. sof_pdata->tplg_filename = mach->sof_tplg_filename;
  679. if (mach->fw_filename)
  680. sof_pdata->fw_filename = mach->fw_filename;
  681. return mach;
  682. }
  683. }
  684. return NULL;
  685. }
  686. /*
  687. * SOF Driver enumeration.
  688. */
  689. int sof_machine_check(struct snd_sof_dev *sdev)
  690. {
  691. struct snd_sof_pdata *sof_pdata = sdev->pdata;
  692. const struct sof_dev_desc *desc = sof_pdata->desc;
  693. struct snd_soc_acpi_mach *mach;
  694. if (!IS_ENABLED(CONFIG_SND_SOC_SOF_FORCE_NOCODEC_MODE)) {
  695. const struct snd_sof_of_mach *of_mach;
  696. /* find machine */
  697. mach = snd_sof_machine_select(sdev);
  698. if (mach) {
  699. sof_pdata->machine = mach;
  700. if (sof_pdata->subsystem_id_set) {
  701. mach->mach_params.subsystem_vendor = sof_pdata->subsystem_vendor;
  702. mach->mach_params.subsystem_device = sof_pdata->subsystem_device;
  703. mach->mach_params.subsystem_id_set = true;
  704. }
  705. snd_sof_set_mach_params(mach, sdev);
  706. return 0;
  707. }
  708. of_mach = sof_of_machine_select(sdev);
  709. if (of_mach) {
  710. sof_pdata->of_machine = of_mach;
  711. return 0;
  712. }
  713. if (!IS_ENABLED(CONFIG_SND_SOC_SOF_NOCODEC)) {
  714. dev_err(sdev->dev, "error: no matching ASoC machine driver found - aborting probe\n");
  715. return -ENODEV;
  716. }
  717. } else {
  718. dev_warn(sdev->dev, "Force to use nocodec mode\n");
  719. }
  720. /* select nocodec mode */
  721. dev_warn(sdev->dev, "Using nocodec machine driver\n");
  722. mach = devm_kzalloc(sdev->dev, sizeof(*mach), GFP_KERNEL);
  723. if (!mach)
  724. return -ENOMEM;
  725. mach->drv_name = "sof-nocodec";
  726. if (!sof_pdata->tplg_filename)
  727. sof_pdata->tplg_filename = desc->nocodec_tplg_filename;
  728. sof_pdata->machine = mach;
  729. snd_sof_set_mach_params(mach, sdev);
  730. return 0;
  731. }
  732. EXPORT_SYMBOL(sof_machine_check);
  733. int sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
  734. {
  735. struct snd_sof_pdata *plat_data = pdata;
  736. const char *drv_name;
  737. const void *mach;
  738. int size;
  739. drv_name = plat_data->machine->drv_name;
  740. mach = plat_data->machine;
  741. size = sizeof(*plat_data->machine);
  742. /* register machine driver, pass machine info as pdata */
  743. plat_data->pdev_mach =
  744. platform_device_register_data(sdev->dev, drv_name,
  745. PLATFORM_DEVID_NONE, mach, size);
  746. if (IS_ERR(plat_data->pdev_mach))
  747. return PTR_ERR(plat_data->pdev_mach);
  748. dev_dbg(sdev->dev, "created machine %s\n",
  749. dev_name(&plat_data->pdev_mach->dev));
  750. return 0;
  751. }
  752. EXPORT_SYMBOL(sof_machine_register);
  753. void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
  754. {
  755. struct snd_sof_pdata *plat_data = pdata;
  756. if (!IS_ERR_OR_NULL(plat_data->pdev_mach))
  757. platform_device_unregister(plat_data->pdev_mach);
  758. }
  759. EXPORT_SYMBOL(sof_machine_unregister);