soc-pcm.c 19 KB

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