soc-pcm.c 19 KB

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