soc-pcm.c 19 KB

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