soc-pcm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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, SNDRV_PCM_STREAM_PLAYBACK,
  274. codec_dai, SND_SOC_DAPM_STREAM_STOP);
  275. }
  276. mutex_unlock(&rtd->pcm_mutex);
  277. }
  278. /*
  279. * Called by ALSA when a PCM substream is closed. Private data can be
  280. * freed here. The cpu DAI, codec DAI, machine and platform are also
  281. * shutdown.
  282. */
  283. static int soc_pcm_close(struct snd_pcm_substream *substream)
  284. {
  285. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  286. struct snd_soc_platform *platform = rtd->platform;
  287. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  288. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  289. struct snd_soc_codec *codec = rtd->codec;
  290. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  291. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  292. cpu_dai->playback_active--;
  293. codec_dai->playback_active--;
  294. } else {
  295. cpu_dai->capture_active--;
  296. codec_dai->capture_active--;
  297. }
  298. cpu_dai->active--;
  299. codec_dai->active--;
  300. codec->active--;
  301. /* clear the corresponding DAIs rate when inactive */
  302. if (!cpu_dai->active)
  303. cpu_dai->rate = 0;
  304. if (!codec_dai->active)
  305. codec_dai->rate = 0;
  306. /* Muting the DAC suppresses artifacts caused during digital
  307. * shutdown, for example from stopping clocks.
  308. */
  309. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  310. snd_soc_dai_digital_mute(codec_dai, 1);
  311. if (cpu_dai->driver->ops->shutdown)
  312. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  313. if (codec_dai->driver->ops->shutdown)
  314. codec_dai->driver->ops->shutdown(substream, codec_dai);
  315. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  316. rtd->dai_link->ops->shutdown(substream);
  317. if (platform->driver->ops && platform->driver->ops->close)
  318. platform->driver->ops->close(substream);
  319. cpu_dai->runtime = NULL;
  320. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  321. if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
  322. rtd->dai_link->ignore_pmdown_time) {
  323. /* powered down playback stream now */
  324. snd_soc_dapm_stream_event(rtd,
  325. SNDRV_PCM_STREAM_PLAYBACK,
  326. codec_dai,
  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, SNDRV_PCM_STREAM_CAPTURE,
  337. codec_dai, SND_SOC_DAPM_STREAM_STOP);
  338. }
  339. mutex_unlock(&rtd->pcm_mutex);
  340. pm_runtime_put(platform->dev);
  341. pm_runtime_put(codec_dai->dev);
  342. pm_runtime_put(cpu_dai->dev);
  343. return 0;
  344. }
  345. /*
  346. * Called by ALSA when the PCM substream is prepared, can set format, sample
  347. * rate, etc. This function is non atomic and can be called multiple times,
  348. * it can refer to the runtime info.
  349. */
  350. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  351. {
  352. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  353. struct snd_soc_platform *platform = rtd->platform;
  354. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  355. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  356. int ret = 0;
  357. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  358. if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
  359. ret = rtd->dai_link->ops->prepare(substream);
  360. if (ret < 0) {
  361. pr_err("asoc: machine prepare error: %d\n", ret);
  362. goto out;
  363. }
  364. }
  365. if (platform->driver->ops && platform->driver->ops->prepare) {
  366. ret = platform->driver->ops->prepare(substream);
  367. if (ret < 0) {
  368. dev_err(platform->dev, "platform prepare error: %d\n",
  369. ret);
  370. goto out;
  371. }
  372. }
  373. if (codec_dai->driver->ops->prepare) {
  374. ret = codec_dai->driver->ops->prepare(substream, codec_dai);
  375. if (ret < 0) {
  376. dev_err(codec_dai->dev, "DAI prepare error: %d\n",
  377. ret);
  378. goto out;
  379. }
  380. }
  381. if (cpu_dai->driver->ops->prepare) {
  382. ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
  383. if (ret < 0) {
  384. dev_err(cpu_dai->dev, "DAI prepare error: %d\n",
  385. ret);
  386. goto out;
  387. }
  388. }
  389. /* cancel any delayed stream shutdown that is pending */
  390. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  391. codec_dai->pop_wait) {
  392. codec_dai->pop_wait = 0;
  393. cancel_delayed_work(&rtd->delayed_work);
  394. }
  395. snd_soc_dapm_stream_event(rtd, substream->stream, codec_dai,
  396. SND_SOC_DAPM_STREAM_START);
  397. snd_soc_dai_digital_mute(codec_dai, 0);
  398. out:
  399. mutex_unlock(&rtd->pcm_mutex);
  400. return ret;
  401. }
  402. /*
  403. * Called by ALSA when the hardware params are set by application. This
  404. * function can also be called multiple times and can allocate buffers
  405. * (using snd_pcm_lib_* ). It's non-atomic.
  406. */
  407. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  408. struct snd_pcm_hw_params *params)
  409. {
  410. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  411. struct snd_soc_platform *platform = rtd->platform;
  412. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  413. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  414. int ret = 0;
  415. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  416. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
  417. ret = rtd->dai_link->ops->hw_params(substream, params);
  418. if (ret < 0) {
  419. pr_err("asoc: machine hw_params failed: %d\n", ret);
  420. goto out;
  421. }
  422. }
  423. if (codec_dai->driver->ops->hw_params) {
  424. ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
  425. if (ret < 0) {
  426. dev_err(codec_dai->dev, "can't set %s hw params: %d\n",
  427. codec_dai->name, ret);
  428. goto codec_err;
  429. }
  430. }
  431. if (cpu_dai->driver->ops->hw_params) {
  432. ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
  433. if (ret < 0) {
  434. dev_err(cpu_dai->dev, "%s hw params failed: %d\n",
  435. cpu_dai->name, ret);
  436. goto interface_err;
  437. }
  438. }
  439. if (platform->driver->ops && platform->driver->ops->hw_params) {
  440. ret = platform->driver->ops->hw_params(substream, params);
  441. if (ret < 0) {
  442. dev_err(platform->dev, "%s hw params failed: %d\n",
  443. platform->name, ret);
  444. goto platform_err;
  445. }
  446. }
  447. /* store the rate for each DAIs */
  448. cpu_dai->rate = params_rate(params);
  449. codec_dai->rate = params_rate(params);
  450. out:
  451. mutex_unlock(&rtd->pcm_mutex);
  452. return ret;
  453. platform_err:
  454. if (cpu_dai->driver->ops->hw_free)
  455. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  456. interface_err:
  457. if (codec_dai->driver->ops->hw_free)
  458. codec_dai->driver->ops->hw_free(substream, codec_dai);
  459. codec_err:
  460. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  461. rtd->dai_link->ops->hw_free(substream);
  462. mutex_unlock(&rtd->pcm_mutex);
  463. return ret;
  464. }
  465. /*
  466. * Frees resources allocated by hw_params, can be called multiple times
  467. */
  468. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  469. {
  470. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  471. struct snd_soc_platform *platform = rtd->platform;
  472. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  473. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  474. struct snd_soc_codec *codec = rtd->codec;
  475. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  476. /* apply codec digital mute */
  477. if (!codec->active)
  478. snd_soc_dai_digital_mute(codec_dai, 1);
  479. /* free any machine hw params */
  480. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  481. rtd->dai_link->ops->hw_free(substream);
  482. /* free any DMA resources */
  483. if (platform->driver->ops && platform->driver->ops->hw_free)
  484. platform->driver->ops->hw_free(substream);
  485. /* now free hw params for the DAIs */
  486. if (codec_dai->driver->ops->hw_free)
  487. codec_dai->driver->ops->hw_free(substream, codec_dai);
  488. if (cpu_dai->driver->ops->hw_free)
  489. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  490. mutex_unlock(&rtd->pcm_mutex);
  491. return 0;
  492. }
  493. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  494. {
  495. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  496. struct snd_soc_platform *platform = rtd->platform;
  497. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  498. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  499. int ret;
  500. if (codec_dai->driver->ops->trigger) {
  501. ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
  502. if (ret < 0)
  503. return ret;
  504. }
  505. if (platform->driver->ops && platform->driver->ops->trigger) {
  506. ret = platform->driver->ops->trigger(substream, cmd);
  507. if (ret < 0)
  508. return ret;
  509. }
  510. if (cpu_dai->driver->ops->trigger) {
  511. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  512. if (ret < 0)
  513. return ret;
  514. }
  515. return 0;
  516. }
  517. /*
  518. * soc level wrapper for pointer callback
  519. * If cpu_dai, codec_dai, platform driver has the delay callback, than
  520. * the runtime->delay will be updated accordingly.
  521. */
  522. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  523. {
  524. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  525. struct snd_soc_platform *platform = rtd->platform;
  526. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  527. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  528. struct snd_pcm_runtime *runtime = substream->runtime;
  529. snd_pcm_uframes_t offset = 0;
  530. snd_pcm_sframes_t delay = 0;
  531. if (platform->driver->ops && platform->driver->ops->pointer)
  532. offset = platform->driver->ops->pointer(substream);
  533. if (cpu_dai->driver->ops->delay)
  534. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  535. if (codec_dai->driver->ops->delay)
  536. delay += codec_dai->driver->ops->delay(substream, codec_dai);
  537. if (platform->driver->delay)
  538. delay += platform->driver->delay(substream, codec_dai);
  539. runtime->delay = delay;
  540. return offset;
  541. }
  542. /* create a new pcm */
  543. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  544. {
  545. struct snd_soc_codec *codec = rtd->codec;
  546. struct snd_soc_platform *platform = rtd->platform;
  547. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  548. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  549. struct snd_pcm_ops *soc_pcm_ops = &rtd->ops;
  550. struct snd_pcm *pcm;
  551. char new_name[64];
  552. int ret = 0, playback = 0, capture = 0;
  553. soc_pcm_ops->open = soc_pcm_open;
  554. soc_pcm_ops->close = soc_pcm_close;
  555. soc_pcm_ops->hw_params = soc_pcm_hw_params;
  556. soc_pcm_ops->hw_free = soc_pcm_hw_free;
  557. soc_pcm_ops->prepare = soc_pcm_prepare;
  558. soc_pcm_ops->trigger = soc_pcm_trigger;
  559. soc_pcm_ops->pointer = soc_pcm_pointer;
  560. /* check client and interface hw capabilities */
  561. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  562. rtd->dai_link->stream_name, codec_dai->name, num);
  563. if (codec_dai->driver->playback.channels_min)
  564. playback = 1;
  565. if (codec_dai->driver->capture.channels_min)
  566. capture = 1;
  567. dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
  568. ret = snd_pcm_new(rtd->card->snd_card, new_name,
  569. num, playback, capture, &pcm);
  570. if (ret < 0) {
  571. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  572. return ret;
  573. }
  574. /* DAPM dai link stream work */
  575. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  576. rtd->pcm = pcm;
  577. pcm->private_data = rtd;
  578. if (platform->driver->ops) {
  579. soc_pcm_ops->mmap = platform->driver->ops->mmap;
  580. soc_pcm_ops->pointer = platform->driver->ops->pointer;
  581. soc_pcm_ops->ioctl = platform->driver->ops->ioctl;
  582. soc_pcm_ops->copy = platform->driver->ops->copy;
  583. soc_pcm_ops->silence = platform->driver->ops->silence;
  584. soc_pcm_ops->ack = platform->driver->ops->ack;
  585. soc_pcm_ops->page = platform->driver->ops->page;
  586. }
  587. if (playback)
  588. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, soc_pcm_ops);
  589. if (capture)
  590. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, soc_pcm_ops);
  591. if (platform->driver->pcm_new) {
  592. ret = platform->driver->pcm_new(rtd);
  593. if (ret < 0) {
  594. pr_err("asoc: platform pcm constructor failed\n");
  595. return ret;
  596. }
  597. }
  598. pcm->private_free = platform->driver->pcm_free;
  599. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  600. cpu_dai->name);
  601. return ret;
  602. }