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