soc-pcm.c 19 KB

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