soc-pcm.c 19 KB

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