soc-pcm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. printk(KERN_ERR "asoc: can't open interface %s\n",
  110. cpu_dai->name);
  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. printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
  118. goto platform_err;
  119. }
  120. }
  121. if (codec_dai->driver->ops->startup) {
  122. ret = codec_dai->driver->ops->startup(substream, codec_dai);
  123. if (ret < 0) {
  124. printk(KERN_ERR "asoc: can't open codec %s\n",
  125. codec_dai->name);
  126. goto codec_dai_err;
  127. }
  128. }
  129. if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
  130. ret = rtd->dai_link->ops->startup(substream);
  131. if (ret < 0) {
  132. printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
  133. goto machine_err;
  134. }
  135. }
  136. /* Check that the codec and cpu DAIs are compatible */
  137. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  138. runtime->hw.rate_min =
  139. max(codec_dai_drv->playback.rate_min,
  140. cpu_dai_drv->playback.rate_min);
  141. runtime->hw.rate_max =
  142. min(codec_dai_drv->playback.rate_max,
  143. cpu_dai_drv->playback.rate_max);
  144. runtime->hw.channels_min =
  145. max(codec_dai_drv->playback.channels_min,
  146. cpu_dai_drv->playback.channels_min);
  147. runtime->hw.channels_max =
  148. min(codec_dai_drv->playback.channels_max,
  149. cpu_dai_drv->playback.channels_max);
  150. runtime->hw.formats =
  151. codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
  152. runtime->hw.rates =
  153. codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
  154. if (codec_dai_drv->playback.rates
  155. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  156. runtime->hw.rates |= cpu_dai_drv->playback.rates;
  157. if (cpu_dai_drv->playback.rates
  158. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  159. runtime->hw.rates |= codec_dai_drv->playback.rates;
  160. } else {
  161. runtime->hw.rate_min =
  162. max(codec_dai_drv->capture.rate_min,
  163. cpu_dai_drv->capture.rate_min);
  164. runtime->hw.rate_max =
  165. min(codec_dai_drv->capture.rate_max,
  166. cpu_dai_drv->capture.rate_max);
  167. runtime->hw.channels_min =
  168. max(codec_dai_drv->capture.channels_min,
  169. cpu_dai_drv->capture.channels_min);
  170. runtime->hw.channels_max =
  171. min(codec_dai_drv->capture.channels_max,
  172. cpu_dai_drv->capture.channels_max);
  173. runtime->hw.formats =
  174. codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
  175. runtime->hw.rates =
  176. codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
  177. if (codec_dai_drv->capture.rates
  178. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  179. runtime->hw.rates |= cpu_dai_drv->capture.rates;
  180. if (cpu_dai_drv->capture.rates
  181. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  182. runtime->hw.rates |= codec_dai_drv->capture.rates;
  183. }
  184. ret = -EINVAL;
  185. snd_pcm_limit_hw_rates(runtime);
  186. if (!runtime->hw.rates) {
  187. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  188. codec_dai->name, cpu_dai->name);
  189. goto config_err;
  190. }
  191. if (!runtime->hw.formats) {
  192. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  193. codec_dai->name, cpu_dai->name);
  194. goto config_err;
  195. }
  196. if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
  197. runtime->hw.channels_min > runtime->hw.channels_max) {
  198. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  199. codec_dai->name, cpu_dai->name);
  200. goto config_err;
  201. }
  202. soc_pcm_apply_msb(substream, codec_dai);
  203. soc_pcm_apply_msb(substream, cpu_dai);
  204. /* Symmetry only applies if we've already got an active stream. */
  205. if (cpu_dai->active) {
  206. ret = soc_pcm_apply_symmetry(substream, cpu_dai);
  207. if (ret != 0)
  208. goto config_err;
  209. }
  210. if (codec_dai->active) {
  211. ret = soc_pcm_apply_symmetry(substream, codec_dai);
  212. if (ret != 0)
  213. goto config_err;
  214. }
  215. pr_debug("asoc: %s <-> %s info:\n",
  216. codec_dai->name, cpu_dai->name);
  217. pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
  218. pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  219. runtime->hw.channels_max);
  220. pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  221. runtime->hw.rate_max);
  222. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  223. cpu_dai->playback_active++;
  224. codec_dai->playback_active++;
  225. } else {
  226. cpu_dai->capture_active++;
  227. codec_dai->capture_active++;
  228. }
  229. cpu_dai->active++;
  230. codec_dai->active++;
  231. rtd->codec->active++;
  232. mutex_unlock(&rtd->pcm_mutex);
  233. return 0;
  234. config_err:
  235. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  236. rtd->dai_link->ops->shutdown(substream);
  237. machine_err:
  238. if (codec_dai->driver->ops->shutdown)
  239. codec_dai->driver->ops->shutdown(substream, codec_dai);
  240. codec_dai_err:
  241. if (platform->driver->ops && platform->driver->ops->close)
  242. platform->driver->ops->close(substream);
  243. platform_err:
  244. if (cpu_dai->driver->ops->shutdown)
  245. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  246. out:
  247. mutex_unlock(&rtd->pcm_mutex);
  248. pm_runtime_put(platform->dev);
  249. pm_runtime_put(codec_dai->dev);
  250. pm_runtime_put(cpu_dai->dev);
  251. return ret;
  252. }
  253. /*
  254. * Power down the audio subsystem pmdown_time msecs after close is called.
  255. * This is to ensure there are no pops or clicks in between any music tracks
  256. * due to DAPM power cycling.
  257. */
  258. static void close_delayed_work(struct work_struct *work)
  259. {
  260. struct snd_soc_pcm_runtime *rtd =
  261. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  262. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  263. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  264. pr_debug("pop wq checking: %s status: %s waiting: %s\n",
  265. codec_dai->driver->playback.stream_name,
  266. codec_dai->playback_active ? "active" : "inactive",
  267. codec_dai->pop_wait ? "yes" : "no");
  268. /* are we waiting on this codec DAI stream */
  269. if (codec_dai->pop_wait == 1) {
  270. codec_dai->pop_wait = 0;
  271. snd_soc_dapm_stream_event(rtd,
  272. codec_dai->driver->playback.stream_name,
  273. SND_SOC_DAPM_STREAM_STOP);
  274. }
  275. mutex_unlock(&rtd->pcm_mutex);
  276. }
  277. /*
  278. * Called by ALSA when a PCM substream is closed. Private data can be
  279. * freed here. The cpu DAI, codec DAI, machine and platform are also
  280. * shutdown.
  281. */
  282. static int soc_pcm_close(struct snd_pcm_substream *substream)
  283. {
  284. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  285. struct snd_soc_platform *platform = rtd->platform;
  286. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  287. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  288. struct snd_soc_codec *codec = rtd->codec;
  289. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  290. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  291. cpu_dai->playback_active--;
  292. codec_dai->playback_active--;
  293. } else {
  294. cpu_dai->capture_active--;
  295. codec_dai->capture_active--;
  296. }
  297. cpu_dai->active--;
  298. codec_dai->active--;
  299. codec->active--;
  300. /* clear the corresponding DAIs rate when inactive */
  301. if (!cpu_dai->active)
  302. cpu_dai->rate = 0;
  303. if (!codec_dai->active)
  304. codec_dai->rate = 0;
  305. /* Muting the DAC suppresses artifacts caused during digital
  306. * shutdown, for example from stopping clocks.
  307. */
  308. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  309. snd_soc_dai_digital_mute(codec_dai, 1);
  310. if (cpu_dai->driver->ops->shutdown)
  311. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  312. if (codec_dai->driver->ops->shutdown)
  313. codec_dai->driver->ops->shutdown(substream, codec_dai);
  314. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  315. rtd->dai_link->ops->shutdown(substream);
  316. if (platform->driver->ops && platform->driver->ops->close)
  317. platform->driver->ops->close(substream);
  318. cpu_dai->runtime = NULL;
  319. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  320. if (codec->ignore_pmdown_time ||
  321. rtd->dai_link->ignore_pmdown_time) {
  322. /* powered down playback stream now */
  323. snd_soc_dapm_stream_event(rtd,
  324. codec_dai->driver->playback.stream_name,
  325. SND_SOC_DAPM_STREAM_STOP);
  326. } else {
  327. /* start delayed pop wq here for playback streams */
  328. codec_dai->pop_wait = 1;
  329. schedule_delayed_work(&rtd->delayed_work,
  330. msecs_to_jiffies(rtd->pmdown_time));
  331. }
  332. } else {
  333. /* capture streams can be powered down now */
  334. snd_soc_dapm_stream_event(rtd,
  335. codec_dai->driver->capture.stream_name,
  336. SND_SOC_DAPM_STREAM_STOP);
  337. }
  338. mutex_unlock(&rtd->pcm_mutex);
  339. pm_runtime_put(platform->dev);
  340. pm_runtime_put(codec_dai->dev);
  341. pm_runtime_put(cpu_dai->dev);
  342. return 0;
  343. }
  344. /*
  345. * Called by ALSA when the PCM substream is prepared, can set format, sample
  346. * rate, etc. This function is non atomic and can be called multiple times,
  347. * it can refer to the runtime info.
  348. */
  349. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  350. {
  351. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  352. struct snd_soc_platform *platform = rtd->platform;
  353. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  354. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  355. int ret = 0;
  356. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  357. if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
  358. ret = rtd->dai_link->ops->prepare(substream);
  359. if (ret < 0) {
  360. printk(KERN_ERR "asoc: machine prepare error\n");
  361. goto out;
  362. }
  363. }
  364. if (platform->driver->ops && platform->driver->ops->prepare) {
  365. ret = platform->driver->ops->prepare(substream);
  366. if (ret < 0) {
  367. printk(KERN_ERR "asoc: platform prepare error\n");
  368. goto out;
  369. }
  370. }
  371. if (codec_dai->driver->ops->prepare) {
  372. ret = codec_dai->driver->ops->prepare(substream, codec_dai);
  373. if (ret < 0) {
  374. printk(KERN_ERR "asoc: codec DAI prepare error\n");
  375. goto out;
  376. }
  377. }
  378. if (cpu_dai->driver->ops->prepare) {
  379. ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
  380. if (ret < 0) {
  381. printk(KERN_ERR "asoc: cpu DAI prepare error\n");
  382. goto out;
  383. }
  384. }
  385. /* cancel any delayed stream shutdown that is pending */
  386. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  387. codec_dai->pop_wait) {
  388. codec_dai->pop_wait = 0;
  389. cancel_delayed_work(&rtd->delayed_work);
  390. }
  391. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  392. snd_soc_dapm_stream_event(rtd,
  393. codec_dai->driver->playback.stream_name,
  394. SND_SOC_DAPM_STREAM_START);
  395. else
  396. snd_soc_dapm_stream_event(rtd,
  397. codec_dai->driver->capture.stream_name,
  398. SND_SOC_DAPM_STREAM_START);
  399. snd_soc_dai_digital_mute(codec_dai, 0);
  400. out:
  401. mutex_unlock(&rtd->pcm_mutex);
  402. return ret;
  403. }
  404. /*
  405. * Called by ALSA when the hardware params are set by application. This
  406. * function can also be called multiple times and can allocate buffers
  407. * (using snd_pcm_lib_* ). It's non-atomic.
  408. */
  409. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  410. struct snd_pcm_hw_params *params)
  411. {
  412. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  413. struct snd_soc_platform *platform = rtd->platform;
  414. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  415. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  416. int ret = 0;
  417. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  418. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
  419. ret = rtd->dai_link->ops->hw_params(substream, params);
  420. if (ret < 0) {
  421. printk(KERN_ERR "asoc: machine hw_params failed\n");
  422. goto out;
  423. }
  424. }
  425. if (codec_dai->driver->ops->hw_params) {
  426. ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
  427. if (ret < 0) {
  428. printk(KERN_ERR "asoc: can't set codec %s hw params\n",
  429. codec_dai->name);
  430. goto codec_err;
  431. }
  432. }
  433. if (cpu_dai->driver->ops->hw_params) {
  434. ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
  435. if (ret < 0) {
  436. printk(KERN_ERR "asoc: interface %s hw params failed\n",
  437. cpu_dai->name);
  438. goto interface_err;
  439. }
  440. }
  441. if (platform->driver->ops && platform->driver->ops->hw_params) {
  442. ret = platform->driver->ops->hw_params(substream, params);
  443. if (ret < 0) {
  444. printk(KERN_ERR "asoc: platform %s hw params failed\n",
  445. platform->name);
  446. goto platform_err;
  447. }
  448. }
  449. /* store the rate for each DAIs */
  450. cpu_dai->rate = params_rate(params);
  451. codec_dai->rate = params_rate(params);
  452. out:
  453. mutex_unlock(&rtd->pcm_mutex);
  454. return ret;
  455. platform_err:
  456. if (cpu_dai->driver->ops->hw_free)
  457. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  458. interface_err:
  459. if (codec_dai->driver->ops->hw_free)
  460. codec_dai->driver->ops->hw_free(substream, codec_dai);
  461. codec_err:
  462. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  463. rtd->dai_link->ops->hw_free(substream);
  464. mutex_unlock(&rtd->pcm_mutex);
  465. return ret;
  466. }
  467. /*
  468. * Frees resources allocated by hw_params, can be called multiple times
  469. */
  470. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  471. {
  472. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  473. struct snd_soc_platform *platform = rtd->platform;
  474. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  475. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  476. struct snd_soc_codec *codec = rtd->codec;
  477. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  478. /* apply codec digital mute */
  479. if (!codec->active)
  480. snd_soc_dai_digital_mute(codec_dai, 1);
  481. /* free any machine hw params */
  482. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  483. rtd->dai_link->ops->hw_free(substream);
  484. /* free any DMA resources */
  485. if (platform->driver->ops && platform->driver->ops->hw_free)
  486. platform->driver->ops->hw_free(substream);
  487. /* now free hw params for the DAIs */
  488. if (codec_dai->driver->ops->hw_free)
  489. codec_dai->driver->ops->hw_free(substream, codec_dai);
  490. if (cpu_dai->driver->ops->hw_free)
  491. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  492. mutex_unlock(&rtd->pcm_mutex);
  493. return 0;
  494. }
  495. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  496. {
  497. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  498. struct snd_soc_platform *platform = rtd->platform;
  499. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  500. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  501. int ret;
  502. if (codec_dai->driver->ops->trigger) {
  503. ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
  504. if (ret < 0)
  505. return ret;
  506. }
  507. if (platform->driver->ops && platform->driver->ops->trigger) {
  508. ret = platform->driver->ops->trigger(substream, cmd);
  509. if (ret < 0)
  510. return ret;
  511. }
  512. if (cpu_dai->driver->ops->trigger) {
  513. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  514. if (ret < 0)
  515. return ret;
  516. }
  517. return 0;
  518. }
  519. /*
  520. * soc level wrapper for pointer callback
  521. * If cpu_dai, codec_dai, platform driver has the delay callback, than
  522. * the runtime->delay will be updated accordingly.
  523. */
  524. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  525. {
  526. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  527. struct snd_soc_platform *platform = rtd->platform;
  528. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  529. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  530. struct snd_pcm_runtime *runtime = substream->runtime;
  531. snd_pcm_uframes_t offset = 0;
  532. snd_pcm_sframes_t delay = 0;
  533. if (platform->driver->ops && platform->driver->ops->pointer)
  534. offset = platform->driver->ops->pointer(substream);
  535. if (cpu_dai->driver->ops->delay)
  536. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  537. if (codec_dai->driver->ops->delay)
  538. delay += codec_dai->driver->ops->delay(substream, codec_dai);
  539. if (platform->driver->delay)
  540. delay += platform->driver->delay(substream, codec_dai);
  541. runtime->delay = delay;
  542. return offset;
  543. }
  544. /* create a new pcm */
  545. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  546. {
  547. struct snd_soc_codec *codec = rtd->codec;
  548. struct snd_soc_platform *platform = rtd->platform;
  549. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  550. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  551. struct snd_pcm_ops *soc_pcm_ops = &rtd->ops;
  552. struct snd_pcm *pcm;
  553. char new_name[64];
  554. int ret = 0, playback = 0, capture = 0;
  555. soc_pcm_ops->open = soc_pcm_open;
  556. soc_pcm_ops->close = soc_pcm_close;
  557. soc_pcm_ops->hw_params = soc_pcm_hw_params;
  558. soc_pcm_ops->hw_free = soc_pcm_hw_free;
  559. soc_pcm_ops->prepare = soc_pcm_prepare;
  560. soc_pcm_ops->trigger = soc_pcm_trigger;
  561. soc_pcm_ops->pointer = soc_pcm_pointer;
  562. /* check client and interface hw capabilities */
  563. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  564. rtd->dai_link->stream_name, codec_dai->name, num);
  565. if (codec_dai->driver->playback.channels_min)
  566. playback = 1;
  567. if (codec_dai->driver->capture.channels_min)
  568. capture = 1;
  569. dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
  570. ret = snd_pcm_new(rtd->card->snd_card, new_name,
  571. num, playback, capture, &pcm);
  572. if (ret < 0) {
  573. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  574. return ret;
  575. }
  576. /* DAPM dai link stream work */
  577. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  578. rtd->pcm = pcm;
  579. pcm->private_data = rtd;
  580. if (platform->driver->ops) {
  581. soc_pcm_ops->mmap = platform->driver->ops->mmap;
  582. soc_pcm_ops->pointer = platform->driver->ops->pointer;
  583. soc_pcm_ops->ioctl = platform->driver->ops->ioctl;
  584. soc_pcm_ops->copy = platform->driver->ops->copy;
  585. soc_pcm_ops->silence = platform->driver->ops->silence;
  586. soc_pcm_ops->ack = platform->driver->ops->ack;
  587. soc_pcm_ops->page = platform->driver->ops->page;
  588. }
  589. if (playback)
  590. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, soc_pcm_ops);
  591. if (capture)
  592. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, soc_pcm_ops);
  593. if (platform->driver->pcm_new) {
  594. ret = platform->driver->pcm_new(rtd);
  595. if (ret < 0) {
  596. pr_err("asoc: platform pcm constructor failed\n");
  597. return ret;
  598. }
  599. }
  600. pcm->private_free = platform->driver->pcm_free;
  601. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  602. cpu_dai->name);
  603. return ret;
  604. }