soc-pcm.c 19 KB

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