soc-pcm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * soc-pcm.c -- ALSA SoC PCM
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. * Copyright (C) 2010 Slimlogic Ltd.
  7. * Copyright (C) 2010 Texas Instruments Inc.
  8. *
  9. * Authors: Liam Girdwood <lrg@ti.com>
  10. * Mark Brown <broonie@opensource.wolfsonmicro.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/delay.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/slab.h>
  23. #include <linux/workqueue.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/initval.h>
  29. static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
  30. struct snd_soc_dai *soc_dai)
  31. {
  32. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  33. int ret;
  34. if (!soc_dai->driver->symmetric_rates &&
  35. !rtd->dai_link->symmetric_rates)
  36. return 0;
  37. /* This can happen if multiple streams are starting simultaneously -
  38. * the second can need to get its constraints before the first has
  39. * picked a rate. Complain and allow the application to carry on.
  40. */
  41. if (!soc_dai->rate) {
  42. dev_warn(soc_dai->dev,
  43. "Not enforcing symmetric_rates due to race\n");
  44. return 0;
  45. }
  46. dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
  47. ret = snd_pcm_hw_constraint_minmax(substream->runtime,
  48. SNDRV_PCM_HW_PARAM_RATE,
  49. soc_dai->rate, soc_dai->rate);
  50. if (ret < 0) {
  51. dev_err(soc_dai->dev,
  52. "Unable to apply rate symmetry constraint: %d\n", ret);
  53. return ret;
  54. }
  55. return 0;
  56. }
  57. /*
  58. * List of sample sizes that might go over the bus for parameter
  59. * application. There ought to be a wildcard sample size for things
  60. * like the DAC/ADC resolution to use but there isn't right now.
  61. */
  62. static int sample_sizes[] = {
  63. 24, 32,
  64. };
  65. static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
  66. struct snd_soc_dai *dai)
  67. {
  68. int ret, i, bits;
  69. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  70. bits = dai->driver->playback.sig_bits;
  71. else
  72. bits = dai->driver->capture.sig_bits;
  73. if (!bits)
  74. return;
  75. for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
  76. if (bits >= sample_sizes[i])
  77. continue;
  78. ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
  79. sample_sizes[i], bits);
  80. if (ret != 0)
  81. dev_warn(dai->dev,
  82. "Failed to set MSB %d/%d: %d\n",
  83. bits, sample_sizes[i], ret);
  84. }
  85. }
  86. /*
  87. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  88. * then initialized and any private data can be allocated. This also calls
  89. * startup for the cpu DAI, platform, machine and codec DAI.
  90. */
  91. static int soc_pcm_open(struct snd_pcm_substream *substream)
  92. {
  93. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  94. struct snd_pcm_runtime *runtime = substream->runtime;
  95. struct snd_soc_platform *platform = rtd->platform;
  96. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  97. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  98. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  99. struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
  100. int ret = 0;
  101. pm_runtime_get_sync(cpu_dai->dev);
  102. pm_runtime_get_sync(codec_dai->dev);
  103. pm_runtime_get_sync(platform->dev);
  104. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  105. /* startup the audio subsystem */
  106. if (cpu_dai->driver->ops->startup) {
  107. ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
  108. if (ret < 0) {
  109. dev_err(cpu_dai->dev, "can't open interface %s: %d\n",
  110. cpu_dai->name, ret);
  111. goto out;
  112. }
  113. }
  114. if (platform->driver->ops && platform->driver->ops->open) {
  115. ret = platform->driver->ops->open(substream);
  116. if (ret < 0) {
  117. dev_err(platform->dev, "can't open platform %s: %d\n",
  118. platform->name, ret);
  119. goto platform_err;
  120. }
  121. }
  122. if (codec_dai->driver->ops->startup) {
  123. ret = codec_dai->driver->ops->startup(substream, codec_dai);
  124. if (ret < 0) {
  125. dev_err(codec_dai->dev, "can't open codec %s: %d\n",
  126. codec_dai->name, ret);
  127. goto codec_dai_err;
  128. }
  129. }
  130. if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
  131. ret = rtd->dai_link->ops->startup(substream);
  132. if (ret < 0) {
  133. pr_err("asoc: %s startup failed: %d\n",
  134. rtd->dai_link->name, ret);
  135. goto machine_err;
  136. }
  137. }
  138. /* Check that the codec and cpu DAIs are compatible */
  139. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  140. runtime->hw.rate_min =
  141. max(codec_dai_drv->playback.rate_min,
  142. cpu_dai_drv->playback.rate_min);
  143. runtime->hw.rate_max =
  144. min(codec_dai_drv->playback.rate_max,
  145. cpu_dai_drv->playback.rate_max);
  146. runtime->hw.channels_min =
  147. max(codec_dai_drv->playback.channels_min,
  148. cpu_dai_drv->playback.channels_min);
  149. runtime->hw.channels_max =
  150. min(codec_dai_drv->playback.channels_max,
  151. cpu_dai_drv->playback.channels_max);
  152. runtime->hw.formats =
  153. codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
  154. runtime->hw.rates =
  155. codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
  156. if (codec_dai_drv->playback.rates
  157. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  158. runtime->hw.rates |= cpu_dai_drv->playback.rates;
  159. if (cpu_dai_drv->playback.rates
  160. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  161. runtime->hw.rates |= codec_dai_drv->playback.rates;
  162. } else {
  163. runtime->hw.rate_min =
  164. max(codec_dai_drv->capture.rate_min,
  165. cpu_dai_drv->capture.rate_min);
  166. runtime->hw.rate_max =
  167. min(codec_dai_drv->capture.rate_max,
  168. cpu_dai_drv->capture.rate_max);
  169. runtime->hw.channels_min =
  170. max(codec_dai_drv->capture.channels_min,
  171. cpu_dai_drv->capture.channels_min);
  172. runtime->hw.channels_max =
  173. min(codec_dai_drv->capture.channels_max,
  174. cpu_dai_drv->capture.channels_max);
  175. runtime->hw.formats =
  176. codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
  177. runtime->hw.rates =
  178. codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
  179. if (codec_dai_drv->capture.rates
  180. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  181. runtime->hw.rates |= cpu_dai_drv->capture.rates;
  182. if (cpu_dai_drv->capture.rates
  183. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  184. runtime->hw.rates |= codec_dai_drv->capture.rates;
  185. }
  186. ret = -EINVAL;
  187. snd_pcm_limit_hw_rates(runtime);
  188. if (!runtime->hw.rates) {
  189. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  190. codec_dai->name, cpu_dai->name);
  191. goto config_err;
  192. }
  193. if (!runtime->hw.formats) {
  194. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  195. codec_dai->name, cpu_dai->name);
  196. goto config_err;
  197. }
  198. if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
  199. runtime->hw.channels_min > runtime->hw.channels_max) {
  200. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  201. codec_dai->name, cpu_dai->name);
  202. goto config_err;
  203. }
  204. soc_pcm_apply_msb(substream, codec_dai);
  205. soc_pcm_apply_msb(substream, cpu_dai);
  206. /* Symmetry only applies if we've already got an active stream. */
  207. if (cpu_dai->active) {
  208. ret = soc_pcm_apply_symmetry(substream, cpu_dai);
  209. if (ret != 0)
  210. goto config_err;
  211. }
  212. if (codec_dai->active) {
  213. ret = soc_pcm_apply_symmetry(substream, codec_dai);
  214. if (ret != 0)
  215. goto config_err;
  216. }
  217. pr_debug("asoc: %s <-> %s info:\n",
  218. codec_dai->name, cpu_dai->name);
  219. pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
  220. pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  221. runtime->hw.channels_max);
  222. pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  223. runtime->hw.rate_max);
  224. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  225. cpu_dai->playback_active++;
  226. codec_dai->playback_active++;
  227. } else {
  228. cpu_dai->capture_active++;
  229. codec_dai->capture_active++;
  230. }
  231. cpu_dai->active++;
  232. codec_dai->active++;
  233. rtd->codec->active++;
  234. mutex_unlock(&rtd->pcm_mutex);
  235. return 0;
  236. config_err:
  237. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  238. rtd->dai_link->ops->shutdown(substream);
  239. machine_err:
  240. if (codec_dai->driver->ops->shutdown)
  241. codec_dai->driver->ops->shutdown(substream, codec_dai);
  242. codec_dai_err:
  243. if (platform->driver->ops && platform->driver->ops->close)
  244. platform->driver->ops->close(substream);
  245. platform_err:
  246. if (cpu_dai->driver->ops->shutdown)
  247. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  248. out:
  249. mutex_unlock(&rtd->pcm_mutex);
  250. pm_runtime_put(platform->dev);
  251. pm_runtime_put(codec_dai->dev);
  252. pm_runtime_put(cpu_dai->dev);
  253. return ret;
  254. }
  255. /*
  256. * Power down the audio subsystem pmdown_time msecs after close is called.
  257. * This is to ensure there are no pops or clicks in between any music tracks
  258. * due to DAPM power cycling.
  259. */
  260. static void close_delayed_work(struct work_struct *work)
  261. {
  262. struct snd_soc_pcm_runtime *rtd =
  263. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  264. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  265. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  266. pr_debug("pop wq checking: %s status: %s waiting: %s\n",
  267. codec_dai->driver->playback.stream_name,
  268. codec_dai->playback_active ? "active" : "inactive",
  269. codec_dai->pop_wait ? "yes" : "no");
  270. /* are we waiting on this codec DAI stream */
  271. if (codec_dai->pop_wait == 1) {
  272. codec_dai->pop_wait = 0;
  273. snd_soc_dapm_stream_event(rtd,
  274. codec_dai->driver->playback.stream_name,
  275. SND_SOC_DAPM_STREAM_STOP);
  276. }
  277. mutex_unlock(&rtd->pcm_mutex);
  278. }
  279. /*
  280. * Called by ALSA when a PCM substream is closed. Private data can be
  281. * freed here. The cpu DAI, codec DAI, machine and platform are also
  282. * shutdown.
  283. */
  284. static int soc_pcm_close(struct snd_pcm_substream *substream)
  285. {
  286. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  287. struct snd_soc_platform *platform = rtd->platform;
  288. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  289. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  290. struct snd_soc_codec *codec = rtd->codec;
  291. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  292. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  293. cpu_dai->playback_active--;
  294. codec_dai->playback_active--;
  295. } else {
  296. cpu_dai->capture_active--;
  297. codec_dai->capture_active--;
  298. }
  299. cpu_dai->active--;
  300. codec_dai->active--;
  301. codec->active--;
  302. /* clear the corresponding DAIs rate when inactive */
  303. if (!cpu_dai->active)
  304. cpu_dai->rate = 0;
  305. if (!codec_dai->active)
  306. codec_dai->rate = 0;
  307. /* Muting the DAC suppresses artifacts caused during digital
  308. * shutdown, for example from stopping clocks.
  309. */
  310. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  311. snd_soc_dai_digital_mute(codec_dai, 1);
  312. if (cpu_dai->driver->ops->shutdown)
  313. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  314. if (codec_dai->driver->ops->shutdown)
  315. codec_dai->driver->ops->shutdown(substream, codec_dai);
  316. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  317. rtd->dai_link->ops->shutdown(substream);
  318. if (platform->driver->ops && platform->driver->ops->close)
  319. platform->driver->ops->close(substream);
  320. cpu_dai->runtime = NULL;
  321. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  322. if (codec->ignore_pmdown_time ||
  323. rtd->dai_link->ignore_pmdown_time) {
  324. /* powered down playback stream now */
  325. snd_soc_dapm_stream_event(rtd,
  326. codec_dai->driver->playback.stream_name,
  327. SND_SOC_DAPM_STREAM_STOP);
  328. } else {
  329. /* start delayed pop wq here for playback streams */
  330. codec_dai->pop_wait = 1;
  331. schedule_delayed_work(&rtd->delayed_work,
  332. msecs_to_jiffies(rtd->pmdown_time));
  333. }
  334. } else {
  335. /* capture streams can be powered down now */
  336. snd_soc_dapm_stream_event(rtd,
  337. codec_dai->driver->capture.stream_name,
  338. SND_SOC_DAPM_STREAM_STOP);
  339. }
  340. mutex_unlock(&rtd->pcm_mutex);
  341. pm_runtime_put(platform->dev);
  342. pm_runtime_put(codec_dai->dev);
  343. pm_runtime_put(cpu_dai->dev);
  344. return 0;
  345. }
  346. /*
  347. * Called by ALSA when the PCM substream is prepared, can set format, sample
  348. * rate, etc. This function is non atomic and can be called multiple times,
  349. * it can refer to the runtime info.
  350. */
  351. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  352. {
  353. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  354. struct snd_soc_platform *platform = rtd->platform;
  355. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  356. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  357. int ret = 0;
  358. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  359. if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
  360. ret = rtd->dai_link->ops->prepare(substream);
  361. if (ret < 0) {
  362. pr_err("asoc: machine prepare error: %d\n", ret);
  363. goto out;
  364. }
  365. }
  366. if (platform->driver->ops && platform->driver->ops->prepare) {
  367. ret = platform->driver->ops->prepare(substream);
  368. if (ret < 0) {
  369. dev_err(platform->dev, "platform prepare error: %d\n",
  370. ret);
  371. goto out;
  372. }
  373. }
  374. if (codec_dai->driver->ops->prepare) {
  375. ret = codec_dai->driver->ops->prepare(substream, codec_dai);
  376. if (ret < 0) {
  377. dev_err(codec_dai->dev, "DAI prepare error: %d\n",
  378. ret);
  379. goto out;
  380. }
  381. }
  382. if (cpu_dai->driver->ops->prepare) {
  383. ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
  384. if (ret < 0) {
  385. dev_err(cpu_dai->dev, "DAI prepare error: %d\n",
  386. ret);
  387. goto out;
  388. }
  389. }
  390. /* cancel any delayed stream shutdown that is pending */
  391. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  392. codec_dai->pop_wait) {
  393. codec_dai->pop_wait = 0;
  394. cancel_delayed_work(&rtd->delayed_work);
  395. }
  396. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  397. snd_soc_dapm_stream_event(rtd,
  398. codec_dai->driver->playback.stream_name,
  399. SND_SOC_DAPM_STREAM_START);
  400. else
  401. snd_soc_dapm_stream_event(rtd,
  402. codec_dai->driver->capture.stream_name,
  403. SND_SOC_DAPM_STREAM_START);
  404. snd_soc_dai_digital_mute(codec_dai, 0);
  405. out:
  406. mutex_unlock(&rtd->pcm_mutex);
  407. return ret;
  408. }
  409. /*
  410. * Called by ALSA when the hardware params are set by application. This
  411. * function can also be called multiple times and can allocate buffers
  412. * (using snd_pcm_lib_* ). It's non-atomic.
  413. */
  414. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  415. struct snd_pcm_hw_params *params)
  416. {
  417. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  418. struct snd_soc_platform *platform = rtd->platform;
  419. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  420. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  421. int ret = 0;
  422. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  423. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
  424. ret = rtd->dai_link->ops->hw_params(substream, params);
  425. if (ret < 0) {
  426. pr_err("asoc: machine hw_params failed: %d\n", ret);
  427. goto out;
  428. }
  429. }
  430. if (codec_dai->driver->ops->hw_params) {
  431. ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
  432. if (ret < 0) {
  433. dev_err(codec_dai->dev, "can't set %s hw params: %d\n",
  434. codec_dai->name, ret);
  435. goto codec_err;
  436. }
  437. }
  438. if (cpu_dai->driver->ops->hw_params) {
  439. ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
  440. if (ret < 0) {
  441. dev_err(cpu_dai->dev, "%s hw params failed: %d\n",
  442. cpu_dai->name, ret);
  443. goto interface_err;
  444. }
  445. }
  446. if (platform->driver->ops && platform->driver->ops->hw_params) {
  447. ret = platform->driver->ops->hw_params(substream, params);
  448. if (ret < 0) {
  449. dev_err(platform->dev, "%s hw params failed: %d\n",
  450. platform->name, ret);
  451. goto platform_err;
  452. }
  453. }
  454. /* store the rate for each DAIs */
  455. cpu_dai->rate = params_rate(params);
  456. codec_dai->rate = params_rate(params);
  457. out:
  458. mutex_unlock(&rtd->pcm_mutex);
  459. return ret;
  460. platform_err:
  461. if (cpu_dai->driver->ops->hw_free)
  462. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  463. interface_err:
  464. if (codec_dai->driver->ops->hw_free)
  465. codec_dai->driver->ops->hw_free(substream, codec_dai);
  466. codec_err:
  467. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  468. rtd->dai_link->ops->hw_free(substream);
  469. mutex_unlock(&rtd->pcm_mutex);
  470. return ret;
  471. }
  472. /*
  473. * Frees resources allocated by hw_params, can be called multiple times
  474. */
  475. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  476. {
  477. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  478. struct snd_soc_platform *platform = rtd->platform;
  479. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  480. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  481. struct snd_soc_codec *codec = rtd->codec;
  482. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  483. /* apply codec digital mute */
  484. if (!codec->active)
  485. snd_soc_dai_digital_mute(codec_dai, 1);
  486. /* free any machine hw params */
  487. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  488. rtd->dai_link->ops->hw_free(substream);
  489. /* free any DMA resources */
  490. if (platform->driver->ops && platform->driver->ops->hw_free)
  491. platform->driver->ops->hw_free(substream);
  492. /* now free hw params for the DAIs */
  493. if (codec_dai->driver->ops->hw_free)
  494. codec_dai->driver->ops->hw_free(substream, codec_dai);
  495. if (cpu_dai->driver->ops->hw_free)
  496. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  497. mutex_unlock(&rtd->pcm_mutex);
  498. return 0;
  499. }
  500. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  501. {
  502. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  503. struct snd_soc_platform *platform = rtd->platform;
  504. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  505. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  506. int ret;
  507. if (codec_dai->driver->ops->trigger) {
  508. ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
  509. if (ret < 0)
  510. return ret;
  511. }
  512. if (platform->driver->ops && platform->driver->ops->trigger) {
  513. ret = platform->driver->ops->trigger(substream, cmd);
  514. if (ret < 0)
  515. return ret;
  516. }
  517. if (cpu_dai->driver->ops->trigger) {
  518. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  519. if (ret < 0)
  520. return ret;
  521. }
  522. return 0;
  523. }
  524. /*
  525. * soc level wrapper for pointer callback
  526. * If cpu_dai, codec_dai, platform driver has the delay callback, than
  527. * the runtime->delay will be updated accordingly.
  528. */
  529. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  530. {
  531. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  532. struct snd_soc_platform *platform = rtd->platform;
  533. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  534. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  535. struct snd_pcm_runtime *runtime = substream->runtime;
  536. snd_pcm_uframes_t offset = 0;
  537. snd_pcm_sframes_t delay = 0;
  538. if (platform->driver->ops && platform->driver->ops->pointer)
  539. offset = platform->driver->ops->pointer(substream);
  540. if (cpu_dai->driver->ops->delay)
  541. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  542. if (codec_dai->driver->ops->delay)
  543. delay += codec_dai->driver->ops->delay(substream, codec_dai);
  544. if (platform->driver->delay)
  545. delay += platform->driver->delay(substream, codec_dai);
  546. runtime->delay = delay;
  547. return offset;
  548. }
  549. /* create a new pcm */
  550. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  551. {
  552. struct snd_soc_codec *codec = rtd->codec;
  553. struct snd_soc_platform *platform = rtd->platform;
  554. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  555. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  556. struct snd_pcm_ops *soc_pcm_ops = &rtd->ops;
  557. struct snd_pcm *pcm;
  558. char new_name[64];
  559. int ret = 0, playback = 0, capture = 0;
  560. soc_pcm_ops->open = soc_pcm_open;
  561. soc_pcm_ops->close = soc_pcm_close;
  562. soc_pcm_ops->hw_params = soc_pcm_hw_params;
  563. soc_pcm_ops->hw_free = soc_pcm_hw_free;
  564. soc_pcm_ops->prepare = soc_pcm_prepare;
  565. soc_pcm_ops->trigger = soc_pcm_trigger;
  566. soc_pcm_ops->pointer = soc_pcm_pointer;
  567. /* check client and interface hw capabilities */
  568. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  569. rtd->dai_link->stream_name, codec_dai->name, num);
  570. if (codec_dai->driver->playback.channels_min)
  571. playback = 1;
  572. if (codec_dai->driver->capture.channels_min)
  573. capture = 1;
  574. dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
  575. ret = snd_pcm_new(rtd->card->snd_card, new_name,
  576. num, playback, capture, &pcm);
  577. if (ret < 0) {
  578. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  579. return ret;
  580. }
  581. /* DAPM dai link stream work */
  582. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  583. rtd->pcm = pcm;
  584. pcm->private_data = rtd;
  585. if (platform->driver->ops) {
  586. soc_pcm_ops->mmap = platform->driver->ops->mmap;
  587. soc_pcm_ops->pointer = platform->driver->ops->pointer;
  588. soc_pcm_ops->ioctl = platform->driver->ops->ioctl;
  589. soc_pcm_ops->copy = platform->driver->ops->copy;
  590. soc_pcm_ops->silence = platform->driver->ops->silence;
  591. soc_pcm_ops->ack = platform->driver->ops->ack;
  592. soc_pcm_ops->page = platform->driver->ops->page;
  593. }
  594. if (playback)
  595. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, soc_pcm_ops);
  596. if (capture)
  597. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, soc_pcm_ops);
  598. if (platform->driver->pcm_new) {
  599. ret = platform->driver->pcm_new(rtd);
  600. if (ret < 0) {
  601. pr_err("asoc: platform pcm constructor failed\n");
  602. return ret;
  603. }
  604. }
  605. pcm->private_free = platform->driver->pcm_free;
  606. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  607. cpu_dai->name);
  608. return ret;
  609. }