soc-pcm.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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 <linux/export.h>
  25. #include <linux/debugfs.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/soc.h>
  30. #include <sound/soc-dpcm.h>
  31. #include <sound/initval.h>
  32. #define DPCM_MAX_BE_USERS 8
  33. /* DPCM stream event, send event to FE and all active BEs. */
  34. static int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
  35. int event)
  36. {
  37. struct snd_soc_dpcm *dpcm;
  38. list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
  39. struct snd_soc_pcm_runtime *be = dpcm->be;
  40. dev_dbg(be->dev, "pm: BE %s event %d dir %d\n",
  41. be->dai_link->name, event, dir);
  42. snd_soc_dapm_stream_event(be, dir, event);
  43. }
  44. snd_soc_dapm_stream_event(fe, dir, event);
  45. return 0;
  46. }
  47. static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
  48. struct snd_soc_dai *soc_dai)
  49. {
  50. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  51. int ret;
  52. if (!soc_dai->driver->symmetric_rates &&
  53. !rtd->dai_link->symmetric_rates)
  54. return 0;
  55. /* This can happen if multiple streams are starting simultaneously -
  56. * the second can need to get its constraints before the first has
  57. * picked a rate. Complain and allow the application to carry on.
  58. */
  59. if (!soc_dai->rate) {
  60. dev_warn(soc_dai->dev,
  61. "Not enforcing symmetric_rates due to race\n");
  62. return 0;
  63. }
  64. dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
  65. ret = snd_pcm_hw_constraint_minmax(substream->runtime,
  66. SNDRV_PCM_HW_PARAM_RATE,
  67. soc_dai->rate, soc_dai->rate);
  68. if (ret < 0) {
  69. dev_err(soc_dai->dev,
  70. "Unable to apply rate symmetry constraint: %d\n", ret);
  71. return ret;
  72. }
  73. return 0;
  74. }
  75. /*
  76. * List of sample sizes that might go over the bus for parameter
  77. * application. There ought to be a wildcard sample size for things
  78. * like the DAC/ADC resolution to use but there isn't right now.
  79. */
  80. static int sample_sizes[] = {
  81. 24, 32,
  82. };
  83. static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
  84. struct snd_soc_dai *dai)
  85. {
  86. int ret, i, bits;
  87. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  88. bits = dai->driver->playback.sig_bits;
  89. else
  90. bits = dai->driver->capture.sig_bits;
  91. if (!bits)
  92. return;
  93. for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
  94. if (bits >= sample_sizes[i])
  95. continue;
  96. ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
  97. sample_sizes[i], bits);
  98. if (ret != 0)
  99. dev_warn(dai->dev,
  100. "Failed to set MSB %d/%d: %d\n",
  101. bits, sample_sizes[i], ret);
  102. }
  103. }
  104. /*
  105. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  106. * then initialized and any private data can be allocated. This also calls
  107. * startup for the cpu DAI, platform, machine and codec DAI.
  108. */
  109. static int soc_pcm_open(struct snd_pcm_substream *substream)
  110. {
  111. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  112. struct snd_pcm_runtime *runtime = substream->runtime;
  113. struct snd_soc_platform *platform = rtd->platform;
  114. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  115. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  116. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  117. struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
  118. int ret = 0;
  119. pm_runtime_get_sync(cpu_dai->dev);
  120. pm_runtime_get_sync(codec_dai->dev);
  121. pm_runtime_get_sync(platform->dev);
  122. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  123. /* startup the audio subsystem */
  124. if (cpu_dai->driver->ops->startup) {
  125. ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
  126. if (ret < 0) {
  127. dev_err(cpu_dai->dev, "can't open interface %s: %d\n",
  128. cpu_dai->name, ret);
  129. goto out;
  130. }
  131. }
  132. if (platform->driver->ops && platform->driver->ops->open) {
  133. ret = platform->driver->ops->open(substream);
  134. if (ret < 0) {
  135. dev_err(platform->dev, "can't open platform %s: %d\n",
  136. platform->name, ret);
  137. goto platform_err;
  138. }
  139. }
  140. if (codec_dai->driver->ops->startup) {
  141. ret = codec_dai->driver->ops->startup(substream, codec_dai);
  142. if (ret < 0) {
  143. dev_err(codec_dai->dev, "can't open codec %s: %d\n",
  144. codec_dai->name, ret);
  145. goto codec_dai_err;
  146. }
  147. }
  148. if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
  149. ret = rtd->dai_link->ops->startup(substream);
  150. if (ret < 0) {
  151. pr_err("asoc: %s startup failed: %d\n",
  152. rtd->dai_link->name, ret);
  153. goto machine_err;
  154. }
  155. }
  156. /* Dynamic PCM DAI links compat checks use dynamic capabilities */
  157. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
  158. goto dynamic;
  159. /* Check that the codec and cpu DAIs are compatible */
  160. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  161. runtime->hw.rate_min =
  162. max(codec_dai_drv->playback.rate_min,
  163. cpu_dai_drv->playback.rate_min);
  164. runtime->hw.rate_max =
  165. min(codec_dai_drv->playback.rate_max,
  166. cpu_dai_drv->playback.rate_max);
  167. runtime->hw.channels_min =
  168. max(codec_dai_drv->playback.channels_min,
  169. cpu_dai_drv->playback.channels_min);
  170. runtime->hw.channels_max =
  171. min(codec_dai_drv->playback.channels_max,
  172. cpu_dai_drv->playback.channels_max);
  173. runtime->hw.formats =
  174. codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
  175. runtime->hw.rates =
  176. codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
  177. if (codec_dai_drv->playback.rates
  178. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  179. runtime->hw.rates |= cpu_dai_drv->playback.rates;
  180. if (cpu_dai_drv->playback.rates
  181. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  182. runtime->hw.rates |= codec_dai_drv->playback.rates;
  183. } else {
  184. runtime->hw.rate_min =
  185. max(codec_dai_drv->capture.rate_min,
  186. cpu_dai_drv->capture.rate_min);
  187. runtime->hw.rate_max =
  188. min(codec_dai_drv->capture.rate_max,
  189. cpu_dai_drv->capture.rate_max);
  190. runtime->hw.channels_min =
  191. max(codec_dai_drv->capture.channels_min,
  192. cpu_dai_drv->capture.channels_min);
  193. runtime->hw.channels_max =
  194. min(codec_dai_drv->capture.channels_max,
  195. cpu_dai_drv->capture.channels_max);
  196. runtime->hw.formats =
  197. codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
  198. runtime->hw.rates =
  199. codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
  200. if (codec_dai_drv->capture.rates
  201. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  202. runtime->hw.rates |= cpu_dai_drv->capture.rates;
  203. if (cpu_dai_drv->capture.rates
  204. & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
  205. runtime->hw.rates |= codec_dai_drv->capture.rates;
  206. }
  207. ret = -EINVAL;
  208. snd_pcm_limit_hw_rates(runtime);
  209. if (!runtime->hw.rates) {
  210. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  211. codec_dai->name, cpu_dai->name);
  212. goto config_err;
  213. }
  214. if (!runtime->hw.formats) {
  215. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  216. codec_dai->name, cpu_dai->name);
  217. goto config_err;
  218. }
  219. if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
  220. runtime->hw.channels_min > runtime->hw.channels_max) {
  221. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  222. codec_dai->name, cpu_dai->name);
  223. goto config_err;
  224. }
  225. soc_pcm_apply_msb(substream, codec_dai);
  226. soc_pcm_apply_msb(substream, cpu_dai);
  227. /* Symmetry only applies if we've already got an active stream. */
  228. if (cpu_dai->active) {
  229. ret = soc_pcm_apply_symmetry(substream, cpu_dai);
  230. if (ret != 0)
  231. goto config_err;
  232. }
  233. if (codec_dai->active) {
  234. ret = soc_pcm_apply_symmetry(substream, codec_dai);
  235. if (ret != 0)
  236. goto config_err;
  237. }
  238. pr_debug("asoc: %s <-> %s info:\n",
  239. codec_dai->name, cpu_dai->name);
  240. pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
  241. pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  242. runtime->hw.channels_max);
  243. pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  244. runtime->hw.rate_max);
  245. dynamic:
  246. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  247. cpu_dai->playback_active++;
  248. codec_dai->playback_active++;
  249. } else {
  250. cpu_dai->capture_active++;
  251. codec_dai->capture_active++;
  252. }
  253. cpu_dai->active++;
  254. codec_dai->active++;
  255. rtd->codec->active++;
  256. mutex_unlock(&rtd->pcm_mutex);
  257. return 0;
  258. config_err:
  259. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  260. rtd->dai_link->ops->shutdown(substream);
  261. machine_err:
  262. if (codec_dai->driver->ops->shutdown)
  263. codec_dai->driver->ops->shutdown(substream, codec_dai);
  264. codec_dai_err:
  265. if (platform->driver->ops && platform->driver->ops->close)
  266. platform->driver->ops->close(substream);
  267. platform_err:
  268. if (cpu_dai->driver->ops->shutdown)
  269. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  270. out:
  271. mutex_unlock(&rtd->pcm_mutex);
  272. pm_runtime_put(platform->dev);
  273. pm_runtime_put(codec_dai->dev);
  274. pm_runtime_put(cpu_dai->dev);
  275. return ret;
  276. }
  277. /*
  278. * Power down the audio subsystem pmdown_time msecs after close is called.
  279. * This is to ensure there are no pops or clicks in between any music tracks
  280. * due to DAPM power cycling.
  281. */
  282. static void close_delayed_work(struct work_struct *work)
  283. {
  284. struct snd_soc_pcm_runtime *rtd =
  285. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  286. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  287. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  288. pr_debug("pop wq checking: %s status: %s waiting: %s\n",
  289. codec_dai->driver->playback.stream_name,
  290. codec_dai->playback_active ? "active" : "inactive",
  291. codec_dai->pop_wait ? "yes" : "no");
  292. /* are we waiting on this codec DAI stream */
  293. if (codec_dai->pop_wait == 1) {
  294. codec_dai->pop_wait = 0;
  295. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  296. SND_SOC_DAPM_STREAM_STOP);
  297. }
  298. mutex_unlock(&rtd->pcm_mutex);
  299. }
  300. /*
  301. * Called by ALSA when a PCM substream is closed. Private data can be
  302. * freed here. The cpu DAI, codec DAI, machine and platform are also
  303. * shutdown.
  304. */
  305. static int soc_pcm_close(struct snd_pcm_substream *substream)
  306. {
  307. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  308. struct snd_soc_platform *platform = rtd->platform;
  309. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  310. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  311. struct snd_soc_codec *codec = rtd->codec;
  312. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  313. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  314. cpu_dai->playback_active--;
  315. codec_dai->playback_active--;
  316. } else {
  317. cpu_dai->capture_active--;
  318. codec_dai->capture_active--;
  319. }
  320. cpu_dai->active--;
  321. codec_dai->active--;
  322. codec->active--;
  323. /* clear the corresponding DAIs rate when inactive */
  324. if (!cpu_dai->active)
  325. cpu_dai->rate = 0;
  326. if (!codec_dai->active)
  327. codec_dai->rate = 0;
  328. /* Muting the DAC suppresses artifacts caused during digital
  329. * shutdown, for example from stopping clocks.
  330. */
  331. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  332. snd_soc_dai_digital_mute(codec_dai, 1);
  333. if (cpu_dai->driver->ops->shutdown)
  334. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  335. if (codec_dai->driver->ops->shutdown)
  336. codec_dai->driver->ops->shutdown(substream, codec_dai);
  337. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  338. rtd->dai_link->ops->shutdown(substream);
  339. if (platform->driver->ops && platform->driver->ops->close)
  340. platform->driver->ops->close(substream);
  341. cpu_dai->runtime = NULL;
  342. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  343. if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
  344. rtd->dai_link->ignore_pmdown_time) {
  345. /* powered down playback stream now */
  346. snd_soc_dapm_stream_event(rtd,
  347. SNDRV_PCM_STREAM_PLAYBACK,
  348. SND_SOC_DAPM_STREAM_STOP);
  349. } else {
  350. /* start delayed pop wq here for playback streams */
  351. codec_dai->pop_wait = 1;
  352. schedule_delayed_work(&rtd->delayed_work,
  353. msecs_to_jiffies(rtd->pmdown_time));
  354. }
  355. } else {
  356. /* capture streams can be powered down now */
  357. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
  358. SND_SOC_DAPM_STREAM_STOP);
  359. }
  360. mutex_unlock(&rtd->pcm_mutex);
  361. pm_runtime_put(platform->dev);
  362. pm_runtime_put(codec_dai->dev);
  363. pm_runtime_put(cpu_dai->dev);
  364. return 0;
  365. }
  366. /*
  367. * Called by ALSA when the PCM substream is prepared, can set format, sample
  368. * rate, etc. This function is non atomic and can be called multiple times,
  369. * it can refer to the runtime info.
  370. */
  371. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  372. {
  373. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  374. struct snd_soc_platform *platform = rtd->platform;
  375. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  376. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  377. int ret = 0;
  378. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  379. if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
  380. ret = rtd->dai_link->ops->prepare(substream);
  381. if (ret < 0) {
  382. pr_err("asoc: machine prepare error: %d\n", ret);
  383. goto out;
  384. }
  385. }
  386. if (platform->driver->ops && platform->driver->ops->prepare) {
  387. ret = platform->driver->ops->prepare(substream);
  388. if (ret < 0) {
  389. dev_err(platform->dev, "platform prepare error: %d\n",
  390. ret);
  391. goto out;
  392. }
  393. }
  394. if (codec_dai->driver->ops->prepare) {
  395. ret = codec_dai->driver->ops->prepare(substream, codec_dai);
  396. if (ret < 0) {
  397. dev_err(codec_dai->dev, "DAI prepare error: %d\n",
  398. ret);
  399. goto out;
  400. }
  401. }
  402. if (cpu_dai->driver->ops->prepare) {
  403. ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
  404. if (ret < 0) {
  405. dev_err(cpu_dai->dev, "DAI prepare error: %d\n",
  406. ret);
  407. goto out;
  408. }
  409. }
  410. /* cancel any delayed stream shutdown that is pending */
  411. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  412. codec_dai->pop_wait) {
  413. codec_dai->pop_wait = 0;
  414. cancel_delayed_work(&rtd->delayed_work);
  415. }
  416. snd_soc_dapm_stream_event(rtd, substream->stream,
  417. SND_SOC_DAPM_STREAM_START);
  418. snd_soc_dai_digital_mute(codec_dai, 0);
  419. out:
  420. mutex_unlock(&rtd->pcm_mutex);
  421. return ret;
  422. }
  423. /*
  424. * Called by ALSA when the hardware params are set by application. This
  425. * function can also be called multiple times and can allocate buffers
  426. * (using snd_pcm_lib_* ). It's non-atomic.
  427. */
  428. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  429. struct snd_pcm_hw_params *params)
  430. {
  431. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  432. struct snd_soc_platform *platform = rtd->platform;
  433. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  434. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  435. int ret = 0;
  436. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  437. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
  438. ret = rtd->dai_link->ops->hw_params(substream, params);
  439. if (ret < 0) {
  440. pr_err("asoc: machine hw_params failed: %d\n", ret);
  441. goto out;
  442. }
  443. }
  444. if (codec_dai->driver->ops->hw_params) {
  445. ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
  446. if (ret < 0) {
  447. dev_err(codec_dai->dev, "can't set %s hw params: %d\n",
  448. codec_dai->name, ret);
  449. goto codec_err;
  450. }
  451. }
  452. if (cpu_dai->driver->ops->hw_params) {
  453. ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
  454. if (ret < 0) {
  455. dev_err(cpu_dai->dev, "%s hw params failed: %d\n",
  456. cpu_dai->name, ret);
  457. goto interface_err;
  458. }
  459. }
  460. if (platform->driver->ops && platform->driver->ops->hw_params) {
  461. ret = platform->driver->ops->hw_params(substream, params);
  462. if (ret < 0) {
  463. dev_err(platform->dev, "%s hw params failed: %d\n",
  464. platform->name, ret);
  465. goto platform_err;
  466. }
  467. }
  468. /* store the rate for each DAIs */
  469. cpu_dai->rate = params_rate(params);
  470. codec_dai->rate = params_rate(params);
  471. out:
  472. mutex_unlock(&rtd->pcm_mutex);
  473. return ret;
  474. platform_err:
  475. if (cpu_dai->driver->ops->hw_free)
  476. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  477. interface_err:
  478. if (codec_dai->driver->ops->hw_free)
  479. codec_dai->driver->ops->hw_free(substream, codec_dai);
  480. codec_err:
  481. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  482. rtd->dai_link->ops->hw_free(substream);
  483. mutex_unlock(&rtd->pcm_mutex);
  484. return ret;
  485. }
  486. /*
  487. * Frees resources allocated by hw_params, can be called multiple times
  488. */
  489. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  490. {
  491. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  492. struct snd_soc_platform *platform = rtd->platform;
  493. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  494. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  495. struct snd_soc_codec *codec = rtd->codec;
  496. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  497. /* apply codec digital mute */
  498. if (!codec->active)
  499. snd_soc_dai_digital_mute(codec_dai, 1);
  500. /* free any machine hw params */
  501. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  502. rtd->dai_link->ops->hw_free(substream);
  503. /* free any DMA resources */
  504. if (platform->driver->ops && platform->driver->ops->hw_free)
  505. platform->driver->ops->hw_free(substream);
  506. /* now free hw params for the DAIs */
  507. if (codec_dai->driver->ops->hw_free)
  508. codec_dai->driver->ops->hw_free(substream, codec_dai);
  509. if (cpu_dai->driver->ops->hw_free)
  510. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  511. mutex_unlock(&rtd->pcm_mutex);
  512. return 0;
  513. }
  514. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  515. {
  516. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  517. struct snd_soc_platform *platform = rtd->platform;
  518. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  519. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  520. int ret;
  521. if (codec_dai->driver->ops->trigger) {
  522. ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
  523. if (ret < 0)
  524. return ret;
  525. }
  526. if (platform->driver->ops && platform->driver->ops->trigger) {
  527. ret = platform->driver->ops->trigger(substream, cmd);
  528. if (ret < 0)
  529. return ret;
  530. }
  531. if (cpu_dai->driver->ops->trigger) {
  532. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  533. if (ret < 0)
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. /*
  539. * soc level wrapper for pointer callback
  540. * If cpu_dai, codec_dai, platform driver has the delay callback, than
  541. * the runtime->delay will be updated accordingly.
  542. */
  543. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  544. {
  545. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  546. struct snd_soc_platform *platform = rtd->platform;
  547. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  548. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  549. struct snd_pcm_runtime *runtime = substream->runtime;
  550. snd_pcm_uframes_t offset = 0;
  551. snd_pcm_sframes_t delay = 0;
  552. if (platform->driver->ops && platform->driver->ops->pointer)
  553. offset = platform->driver->ops->pointer(substream);
  554. if (cpu_dai->driver->ops->delay)
  555. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  556. if (codec_dai->driver->ops->delay)
  557. delay += codec_dai->driver->ops->delay(substream, codec_dai);
  558. if (platform->driver->delay)
  559. delay += platform->driver->delay(substream, codec_dai);
  560. runtime->delay = delay;
  561. return offset;
  562. }
  563. /* connect a FE and BE */
  564. static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
  565. struct snd_soc_pcm_runtime *be, int stream)
  566. {
  567. struct snd_soc_dpcm *dpcm;
  568. /* only add new dpcms */
  569. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  570. if (dpcm->be == be && dpcm->fe == fe)
  571. return 0;
  572. }
  573. dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
  574. if (!dpcm)
  575. return -ENOMEM;
  576. dpcm->be = be;
  577. dpcm->fe = fe;
  578. be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
  579. dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
  580. list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
  581. list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
  582. dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
  583. stream ? "capture" : "playback", fe->dai_link->name,
  584. stream ? "<-" : "->", be->dai_link->name);
  585. #ifdef CONFIG_DEBUG_FS
  586. dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
  587. fe->debugfs_dpcm_root, &dpcm->state);
  588. #endif
  589. return 1;
  590. }
  591. /* reparent a BE onto another FE */
  592. static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
  593. struct snd_soc_pcm_runtime *be, int stream)
  594. {
  595. struct snd_soc_dpcm *dpcm;
  596. struct snd_pcm_substream *fe_substream, *be_substream;
  597. /* reparent if BE is connected to other FEs */
  598. if (!be->dpcm[stream].users)
  599. return;
  600. be_substream = snd_soc_dpcm_get_substream(be, stream);
  601. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  602. if (dpcm->fe == fe)
  603. continue;
  604. dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
  605. stream ? "capture" : "playback",
  606. dpcm->fe->dai_link->name,
  607. stream ? "<-" : "->", dpcm->be->dai_link->name);
  608. fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
  609. be_substream->runtime = fe_substream->runtime;
  610. break;
  611. }
  612. }
  613. /* disconnect a BE and FE */
  614. static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
  615. {
  616. struct snd_soc_dpcm *dpcm, *d;
  617. list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
  618. dev_dbg(fe->dev, "BE %s disconnect check for %s\n",
  619. stream ? "capture" : "playback",
  620. dpcm->be->dai_link->name);
  621. if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
  622. continue;
  623. dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
  624. stream ? "capture" : "playback", fe->dai_link->name,
  625. stream ? "<-" : "->", dpcm->be->dai_link->name);
  626. /* BEs still alive need new FE */
  627. dpcm_be_reparent(fe, dpcm->be, stream);
  628. #ifdef CONFIG_DEBUG_FS
  629. debugfs_remove(dpcm->debugfs_state);
  630. #endif
  631. list_del(&dpcm->list_be);
  632. list_del(&dpcm->list_fe);
  633. kfree(dpcm);
  634. }
  635. }
  636. /* get BE for DAI widget and stream */
  637. static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
  638. struct snd_soc_dapm_widget *widget, int stream)
  639. {
  640. struct snd_soc_pcm_runtime *be;
  641. int i;
  642. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  643. for (i = 0; i < card->num_links; i++) {
  644. be = &card->rtd[i];
  645. if (be->cpu_dai->playback_widget == widget ||
  646. be->codec_dai->playback_widget == widget)
  647. return be;
  648. }
  649. } else {
  650. for (i = 0; i < card->num_links; i++) {
  651. be = &card->rtd[i];
  652. if (be->cpu_dai->capture_widget == widget ||
  653. be->codec_dai->capture_widget == widget)
  654. return be;
  655. }
  656. }
  657. dev_err(card->dev, "can't get %s BE for %s\n",
  658. stream ? "capture" : "playback", widget->name);
  659. return NULL;
  660. }
  661. static inline struct snd_soc_dapm_widget *
  662. rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
  663. {
  664. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  665. return rtd->cpu_dai->playback_widget;
  666. else
  667. return rtd->cpu_dai->capture_widget;
  668. }
  669. static inline struct snd_soc_dapm_widget *
  670. rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
  671. {
  672. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  673. return rtd->codec_dai->playback_widget;
  674. else
  675. return rtd->codec_dai->capture_widget;
  676. }
  677. static int widget_in_list(struct snd_soc_dapm_widget_list *list,
  678. struct snd_soc_dapm_widget *widget)
  679. {
  680. int i;
  681. for (i = 0; i < list->num_widgets; i++) {
  682. if (widget == list->widgets[i])
  683. return 1;
  684. }
  685. return 0;
  686. }
  687. static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
  688. int stream, struct snd_soc_dapm_widget_list **list_)
  689. {
  690. struct snd_soc_dai *cpu_dai = fe->cpu_dai;
  691. struct snd_soc_dapm_widget_list *list;
  692. int paths;
  693. list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
  694. sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
  695. if (list == NULL)
  696. return -ENOMEM;
  697. /* get number of valid DAI paths and their widgets */
  698. paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
  699. dev_dbg(fe->dev, "found %d audio %s paths\n", paths,
  700. stream ? "capture" : "playback");
  701. *list_ = list;
  702. return paths;
  703. }
  704. static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
  705. {
  706. kfree(*list);
  707. }
  708. static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
  709. struct snd_soc_dapm_widget_list **list_)
  710. {
  711. struct snd_soc_dpcm *dpcm;
  712. struct snd_soc_dapm_widget_list *list = *list_;
  713. struct snd_soc_dapm_widget *widget;
  714. int prune = 0;
  715. /* Destroy any old FE <--> BE connections */
  716. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  717. /* is there a valid CPU DAI widget for this BE */
  718. widget = rtd_get_cpu_widget(dpcm->be, stream);
  719. /* prune the BE if it's no longer in our active list */
  720. if (widget && widget_in_list(list, widget))
  721. continue;
  722. /* is there a valid CODEC DAI widget for this BE */
  723. widget = rtd_get_codec_widget(dpcm->be, stream);
  724. /* prune the BE if it's no longer in our active list */
  725. if (widget && widget_in_list(list, widget))
  726. continue;
  727. dev_dbg(fe->dev, "pruning %s BE %s for %s\n",
  728. stream ? "capture" : "playback",
  729. dpcm->be->dai_link->name, fe->dai_link->name);
  730. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  731. dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  732. prune++;
  733. }
  734. dev_dbg(fe->dev, "found %d old BE paths for pruning\n", prune);
  735. return prune;
  736. }
  737. static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
  738. struct snd_soc_dapm_widget_list **list_)
  739. {
  740. struct snd_soc_card *card = fe->card;
  741. struct snd_soc_dapm_widget_list *list = *list_;
  742. struct snd_soc_pcm_runtime *be;
  743. int i, new = 0, err;
  744. /* Create any new FE <--> BE connections */
  745. for (i = 0; i < list->num_widgets; i++) {
  746. if (list->widgets[i]->id != snd_soc_dapm_dai)
  747. continue;
  748. /* is there a valid BE rtd for this widget */
  749. be = dpcm_get_be(card, list->widgets[i], stream);
  750. if (!be) {
  751. dev_err(fe->dev, "no BE found for %s\n",
  752. list->widgets[i]->name);
  753. continue;
  754. }
  755. /* make sure BE is a real BE */
  756. if (!be->dai_link->no_pcm)
  757. continue;
  758. /* don't connect if FE is not running */
  759. if (!fe->dpcm[stream].runtime)
  760. continue;
  761. /* newly connected FE and BE */
  762. err = dpcm_be_connect(fe, be, stream);
  763. if (err < 0) {
  764. dev_err(fe->dev, "can't connect %s\n",
  765. list->widgets[i]->name);
  766. break;
  767. } else if (err == 0) /* already connected */
  768. continue;
  769. /* new */
  770. be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  771. new++;
  772. }
  773. dev_dbg(fe->dev, "found %d new BE paths\n", new);
  774. return new;
  775. }
  776. /*
  777. * Find the corresponding BE DAIs that source or sink audio to this
  778. * FE substream.
  779. */
  780. static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
  781. int stream, struct snd_soc_dapm_widget_list **list, int new)
  782. {
  783. if (new)
  784. return dpcm_add_paths(fe, stream, list);
  785. else
  786. return dpcm_prune_paths(fe, stream, list);
  787. }
  788. static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
  789. {
  790. struct snd_soc_dpcm *dpcm;
  791. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  792. dpcm->be->dpcm[stream].runtime_update =
  793. SND_SOC_DPCM_UPDATE_NO;
  794. }
  795. static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
  796. int stream)
  797. {
  798. struct snd_soc_dpcm *dpcm;
  799. /* disable any enabled and non active backends */
  800. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  801. struct snd_soc_pcm_runtime *be = dpcm->be;
  802. struct snd_pcm_substream *be_substream =
  803. snd_soc_dpcm_get_substream(be, stream);
  804. if (be->dpcm[stream].users == 0)
  805. dev_err(be->dev, "no users %s at close - state %d\n",
  806. stream ? "capture" : "playback",
  807. be->dpcm[stream].state);
  808. if (--be->dpcm[stream].users != 0)
  809. continue;
  810. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  811. continue;
  812. soc_pcm_close(be_substream);
  813. be_substream->runtime = NULL;
  814. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  815. }
  816. }
  817. static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
  818. {
  819. struct snd_soc_dpcm *dpcm;
  820. int err, count = 0;
  821. /* only startup BE DAIs that are either sinks or sources to this FE DAI */
  822. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  823. struct snd_soc_pcm_runtime *be = dpcm->be;
  824. struct snd_pcm_substream *be_substream =
  825. snd_soc_dpcm_get_substream(be, stream);
  826. /* is this op for this BE ? */
  827. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  828. continue;
  829. /* first time the dpcm is open ? */
  830. if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
  831. dev_err(be->dev, "too many users %s at open %d\n",
  832. stream ? "capture" : "playback",
  833. be->dpcm[stream].state);
  834. if (be->dpcm[stream].users++ != 0)
  835. continue;
  836. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
  837. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
  838. continue;
  839. dev_dbg(be->dev, "dpcm: open BE %s\n", be->dai_link->name);
  840. be_substream->runtime = be->dpcm[stream].runtime;
  841. err = soc_pcm_open(be_substream);
  842. if (err < 0) {
  843. dev_err(be->dev, "BE open failed %d\n", err);
  844. be->dpcm[stream].users--;
  845. if (be->dpcm[stream].users < 0)
  846. dev_err(be->dev, "no users %s at unwind %d\n",
  847. stream ? "capture" : "playback",
  848. be->dpcm[stream].state);
  849. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  850. goto unwind;
  851. }
  852. be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  853. count++;
  854. }
  855. return count;
  856. unwind:
  857. /* disable any enabled and non active backends */
  858. list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  859. struct snd_soc_pcm_runtime *be = dpcm->be;
  860. struct snd_pcm_substream *be_substream =
  861. snd_soc_dpcm_get_substream(be, stream);
  862. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  863. continue;
  864. if (be->dpcm[stream].users == 0)
  865. dev_err(be->dev, "no users %s at close %d\n",
  866. stream ? "capture" : "playback",
  867. be->dpcm[stream].state);
  868. if (--be->dpcm[stream].users != 0)
  869. continue;
  870. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  871. continue;
  872. soc_pcm_close(be_substream);
  873. be_substream->runtime = NULL;
  874. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  875. }
  876. return err;
  877. }
  878. void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
  879. {
  880. struct snd_pcm_runtime *runtime = substream->runtime;
  881. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  882. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  883. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  884. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  885. runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
  886. runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
  887. runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
  888. runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
  889. runtime->hw.formats &= cpu_dai_drv->playback.formats;
  890. runtime->hw.rates = cpu_dai_drv->playback.rates;
  891. } else {
  892. runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
  893. runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
  894. runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
  895. runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
  896. runtime->hw.formats &= cpu_dai_drv->capture.formats;
  897. runtime->hw.rates = cpu_dai_drv->capture.rates;
  898. }
  899. }
  900. static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
  901. {
  902. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  903. struct snd_pcm_runtime *runtime = fe_substream->runtime;
  904. int stream = fe_substream->stream, ret = 0;
  905. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  906. ret = dpcm_be_dai_startup(fe, fe_substream->stream);
  907. if (ret < 0) {
  908. dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret);
  909. goto be_err;
  910. }
  911. dev_dbg(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name);
  912. /* start the DAI frontend */
  913. ret = soc_pcm_open(fe_substream);
  914. if (ret < 0) {
  915. dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret);
  916. goto unwind;
  917. }
  918. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  919. dpcm_set_fe_runtime(fe_substream);
  920. snd_pcm_limit_hw_rates(runtime);
  921. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  922. return 0;
  923. unwind:
  924. dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
  925. be_err:
  926. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  927. return ret;
  928. }
  929. static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  930. {
  931. struct snd_soc_dpcm *dpcm;
  932. /* only shutdown BEs that are either sinks or sources to this FE DAI */
  933. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  934. struct snd_soc_pcm_runtime *be = dpcm->be;
  935. struct snd_pcm_substream *be_substream =
  936. snd_soc_dpcm_get_substream(be, stream);
  937. /* is this op for this BE ? */
  938. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  939. continue;
  940. if (be->dpcm[stream].users == 0)
  941. dev_err(be->dev, "no users %s at close - state %d\n",
  942. stream ? "capture" : "playback",
  943. be->dpcm[stream].state);
  944. if (--be->dpcm[stream].users != 0)
  945. continue;
  946. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  947. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
  948. continue;
  949. dev_dbg(be->dev, "dpcm: close BE %s\n",
  950. dpcm->fe->dai_link->name);
  951. soc_pcm_close(be_substream);
  952. be_substream->runtime = NULL;
  953. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  954. }
  955. return 0;
  956. }
  957. static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
  958. {
  959. struct snd_soc_pcm_runtime *fe = substream->private_data;
  960. int stream = substream->stream;
  961. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  962. /* shutdown the BEs */
  963. dpcm_be_dai_shutdown(fe, substream->stream);
  964. dev_dbg(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name);
  965. /* now shutdown the frontend */
  966. soc_pcm_close(substream);
  967. /* run the stream event for each BE */
  968. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  969. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  970. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  971. return 0;
  972. }
  973. static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
  974. {
  975. struct snd_soc_dpcm *dpcm;
  976. /* only hw_params backends that are either sinks or sources
  977. * to this frontend DAI */
  978. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  979. struct snd_soc_pcm_runtime *be = dpcm->be;
  980. struct snd_pcm_substream *be_substream =
  981. snd_soc_dpcm_get_substream(be, stream);
  982. /* is this op for this BE ? */
  983. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  984. continue;
  985. /* only free hw when no longer used - check all FEs */
  986. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  987. continue;
  988. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  989. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  990. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  991. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  992. continue;
  993. dev_dbg(be->dev, "dpcm: hw_free BE %s\n",
  994. dpcm->fe->dai_link->name);
  995. soc_pcm_hw_free(be_substream);
  996. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  997. }
  998. return 0;
  999. }
  1000. int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
  1001. {
  1002. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1003. int err, stream = substream->stream;
  1004. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1005. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1006. dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name);
  1007. /* call hw_free on the frontend */
  1008. err = soc_pcm_hw_free(substream);
  1009. if (err < 0)
  1010. dev_err(fe->dev,"dpcm: hw_free FE %s failed\n",
  1011. fe->dai_link->name);
  1012. /* only hw_params backends that are either sinks or sources
  1013. * to this frontend DAI */
  1014. err = dpcm_be_dai_hw_free(fe, stream);
  1015. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1016. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1017. mutex_unlock(&fe->card->mutex);
  1018. return 0;
  1019. }
  1020. static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
  1021. {
  1022. struct snd_soc_dpcm *dpcm;
  1023. int ret;
  1024. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1025. struct snd_soc_pcm_runtime *be = dpcm->be;
  1026. struct snd_pcm_substream *be_substream =
  1027. snd_soc_dpcm_get_substream(be, stream);
  1028. /* is this op for this BE ? */
  1029. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1030. continue;
  1031. /* only allow hw_params() if no connected FEs are running */
  1032. if (!snd_soc_dpcm_can_be_params(fe, be, stream))
  1033. continue;
  1034. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1035. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1036. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
  1037. continue;
  1038. dev_dbg(be->dev, "dpcm: hw_params BE %s\n",
  1039. dpcm->fe->dai_link->name);
  1040. /* copy params for each dpcm */
  1041. memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
  1042. sizeof(struct snd_pcm_hw_params));
  1043. /* perform any hw_params fixups */
  1044. if (be->dai_link->be_hw_params_fixup) {
  1045. ret = be->dai_link->be_hw_params_fixup(be,
  1046. &dpcm->hw_params);
  1047. if (ret < 0) {
  1048. dev_err(be->dev,
  1049. "dpcm: hw_params BE fixup failed %d\n",
  1050. ret);
  1051. goto unwind;
  1052. }
  1053. }
  1054. ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
  1055. if (ret < 0) {
  1056. dev_err(dpcm->be->dev,
  1057. "dpcm: hw_params BE failed %d\n", ret);
  1058. goto unwind;
  1059. }
  1060. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1061. }
  1062. return 0;
  1063. unwind:
  1064. /* disable any enabled and non active backends */
  1065. list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1066. struct snd_soc_pcm_runtime *be = dpcm->be;
  1067. struct snd_pcm_substream *be_substream =
  1068. snd_soc_dpcm_get_substream(be, stream);
  1069. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1070. continue;
  1071. /* only allow hw_free() if no connected FEs are running */
  1072. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1073. continue;
  1074. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1075. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1076. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1077. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1078. continue;
  1079. soc_pcm_hw_free(be_substream);
  1080. }
  1081. return ret;
  1082. }
  1083. int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
  1084. struct snd_pcm_hw_params *params)
  1085. {
  1086. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1087. int ret, stream = substream->stream;
  1088. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1089. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1090. memcpy(&fe->dpcm[substream->stream].hw_params, params,
  1091. sizeof(struct snd_pcm_hw_params));
  1092. ret = dpcm_be_dai_hw_params(fe, substream->stream);
  1093. if (ret < 0) {
  1094. dev_err(fe->dev,"dpcm: hw_params BE failed %d\n", ret);
  1095. goto out;
  1096. }
  1097. dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n",
  1098. fe->dai_link->name, params_rate(params),
  1099. params_channels(params), params_format(params));
  1100. /* call hw_params on the frontend */
  1101. ret = soc_pcm_hw_params(substream, params);
  1102. if (ret < 0) {
  1103. dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret);
  1104. dpcm_be_dai_hw_free(fe, stream);
  1105. } else
  1106. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1107. out:
  1108. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1109. mutex_unlock(&fe->card->mutex);
  1110. return ret;
  1111. }
  1112. static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
  1113. struct snd_pcm_substream *substream, int cmd)
  1114. {
  1115. int ret;
  1116. dev_dbg(dpcm->be->dev, "dpcm: trigger BE %s cmd %d\n",
  1117. dpcm->fe->dai_link->name, cmd);
  1118. ret = soc_pcm_trigger(substream, cmd);
  1119. if (ret < 0)
  1120. dev_err(dpcm->be->dev,"dpcm: trigger BE failed %d\n", ret);
  1121. return ret;
  1122. }
  1123. int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, int cmd)
  1124. {
  1125. struct snd_soc_dpcm *dpcm;
  1126. int ret = 0;
  1127. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1128. struct snd_soc_pcm_runtime *be = dpcm->be;
  1129. struct snd_pcm_substream *be_substream =
  1130. snd_soc_dpcm_get_substream(be, stream);
  1131. /* is this op for this BE ? */
  1132. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1133. continue;
  1134. switch (cmd) {
  1135. case SNDRV_PCM_TRIGGER_START:
  1136. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1137. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1138. continue;
  1139. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1140. if (ret)
  1141. return ret;
  1142. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1143. break;
  1144. case SNDRV_PCM_TRIGGER_RESUME:
  1145. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
  1146. continue;
  1147. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1148. if (ret)
  1149. return ret;
  1150. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1151. break;
  1152. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1153. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
  1154. continue;
  1155. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1156. if (ret)
  1157. return ret;
  1158. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1159. break;
  1160. case SNDRV_PCM_TRIGGER_STOP:
  1161. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1162. continue;
  1163. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1164. continue;
  1165. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1166. if (ret)
  1167. return ret;
  1168. be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  1169. break;
  1170. case SNDRV_PCM_TRIGGER_SUSPEND:
  1171. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
  1172. continue;
  1173. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1174. continue;
  1175. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1176. if (ret)
  1177. return ret;
  1178. be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
  1179. break;
  1180. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1181. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1182. continue;
  1183. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1184. continue;
  1185. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1186. if (ret)
  1187. return ret;
  1188. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  1189. break;
  1190. }
  1191. }
  1192. return ret;
  1193. }
  1194. EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
  1195. int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
  1196. {
  1197. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1198. int stream = substream->stream, ret;
  1199. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1200. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1201. switch (trigger) {
  1202. case SND_SOC_DPCM_TRIGGER_PRE:
  1203. /* call trigger on the frontend before the backend. */
  1204. dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n",
  1205. fe->dai_link->name, cmd);
  1206. ret = soc_pcm_trigger(substream, cmd);
  1207. if (ret < 0) {
  1208. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1209. goto out;
  1210. }
  1211. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1212. break;
  1213. case SND_SOC_DPCM_TRIGGER_POST:
  1214. /* call trigger on the frontend after the backend. */
  1215. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1216. if (ret < 0) {
  1217. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1218. goto out;
  1219. }
  1220. dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n",
  1221. fe->dai_link->name, cmd);
  1222. ret = soc_pcm_trigger(substream, cmd);
  1223. break;
  1224. default:
  1225. dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd,
  1226. fe->dai_link->name);
  1227. ret = -EINVAL;
  1228. goto out;
  1229. }
  1230. switch (cmd) {
  1231. case SNDRV_PCM_TRIGGER_START:
  1232. case SNDRV_PCM_TRIGGER_RESUME:
  1233. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1234. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1235. break;
  1236. case SNDRV_PCM_TRIGGER_STOP:
  1237. case SNDRV_PCM_TRIGGER_SUSPEND:
  1238. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1239. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  1240. break;
  1241. }
  1242. out:
  1243. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1244. return ret;
  1245. }
  1246. static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
  1247. {
  1248. struct snd_soc_dpcm *dpcm;
  1249. int ret = 0;
  1250. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1251. struct snd_soc_pcm_runtime *be = dpcm->be;
  1252. struct snd_pcm_substream *be_substream =
  1253. snd_soc_dpcm_get_substream(be, stream);
  1254. /* is this op for this BE ? */
  1255. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1256. continue;
  1257. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1258. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1259. continue;
  1260. dev_dbg(be->dev, "dpcm: prepare BE %s\n",
  1261. dpcm->fe->dai_link->name);
  1262. ret = soc_pcm_prepare(be_substream);
  1263. if (ret < 0) {
  1264. dev_err(be->dev, "dpcm: backend prepare failed %d\n",
  1265. ret);
  1266. break;
  1267. }
  1268. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  1269. }
  1270. return ret;
  1271. }
  1272. int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
  1273. {
  1274. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1275. int stream = substream->stream, ret = 0;
  1276. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1277. dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name);
  1278. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1279. /* there is no point preparing this FE if there are no BEs */
  1280. if (list_empty(&fe->dpcm[stream].be_clients)) {
  1281. dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n",
  1282. fe->dai_link->name);
  1283. ret = -EINVAL;
  1284. goto out;
  1285. }
  1286. ret = dpcm_be_dai_prepare(fe, substream->stream);
  1287. if (ret < 0)
  1288. goto out;
  1289. /* call prepare on the frontend */
  1290. ret = soc_pcm_prepare(substream);
  1291. if (ret < 0) {
  1292. dev_err(fe->dev,"dpcm: prepare FE %s failed\n",
  1293. fe->dai_link->name);
  1294. goto out;
  1295. }
  1296. /* run the stream event for each BE */
  1297. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  1298. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  1299. out:
  1300. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1301. mutex_unlock(&fe->card->mutex);
  1302. return ret;
  1303. }
  1304. int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
  1305. {
  1306. struct snd_soc_dpcm *dpcm;
  1307. struct list_head *clients =
  1308. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
  1309. list_for_each_entry(dpcm, clients, list_be) {
  1310. struct snd_soc_pcm_runtime *be = dpcm->be;
  1311. struct snd_soc_dai *dai = be->codec_dai;
  1312. struct snd_soc_dai_driver *drv = dai->driver;
  1313. if (be->dai_link->ignore_suspend)
  1314. continue;
  1315. dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name);
  1316. if (drv->ops->digital_mute && dai->playback_active)
  1317. drv->ops->digital_mute(dai, mute);
  1318. }
  1319. return 0;
  1320. }
  1321. int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
  1322. {
  1323. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1324. struct snd_soc_dpcm *dpcm;
  1325. struct snd_soc_dapm_widget_list *list;
  1326. int ret;
  1327. int stream = fe_substream->stream;
  1328. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1329. fe->dpcm[stream].runtime = fe_substream->runtime;
  1330. if (dpcm_path_get(fe, stream, &list) <= 0) {
  1331. dev_warn(fe->dev, "asoc: %s no valid %s route\n",
  1332. fe->dai_link->name, stream ? "capture" : "playback");
  1333. mutex_unlock(&fe->card->mutex);
  1334. return -EINVAL;
  1335. }
  1336. /* calculate valid and active FE <-> BE dpcms */
  1337. dpcm_process_paths(fe, stream, &list, 1);
  1338. ret = dpcm_fe_dai_startup(fe_substream);
  1339. if (ret < 0) {
  1340. /* clean up all links */
  1341. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  1342. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1343. dpcm_be_disconnect(fe, stream);
  1344. fe->dpcm[stream].runtime = NULL;
  1345. }
  1346. dpcm_clear_pending_state(fe, stream);
  1347. dpcm_path_put(&list);
  1348. mutex_unlock(&fe->card->mutex);
  1349. return ret;
  1350. }
  1351. int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
  1352. {
  1353. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1354. struct snd_soc_dpcm *dpcm;
  1355. int stream = fe_substream->stream, ret;
  1356. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1357. ret = dpcm_fe_dai_shutdown(fe_substream);
  1358. /* mark FE's links ready to prune */
  1359. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  1360. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1361. dpcm_be_disconnect(fe, stream);
  1362. fe->dpcm[stream].runtime = NULL;
  1363. mutex_unlock(&fe->card->mutex);
  1364. return ret;
  1365. }
  1366. /* create a new pcm */
  1367. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  1368. {
  1369. struct snd_soc_codec *codec = rtd->codec;
  1370. struct snd_soc_platform *platform = rtd->platform;
  1371. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1372. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1373. struct snd_pcm *pcm;
  1374. char new_name[64];
  1375. int ret = 0, playback = 0, capture = 0;
  1376. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
  1377. if (cpu_dai->driver->playback.channels_min)
  1378. playback = 1;
  1379. if (cpu_dai->driver->capture.channels_min)
  1380. capture = 1;
  1381. } else {
  1382. if (codec_dai->driver->playback.channels_min)
  1383. playback = 1;
  1384. if (codec_dai->driver->capture.channels_min)
  1385. capture = 1;
  1386. }
  1387. /* create the PCM */
  1388. if (rtd->dai_link->no_pcm) {
  1389. snprintf(new_name, sizeof(new_name), "(%s)",
  1390. rtd->dai_link->stream_name);
  1391. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  1392. playback, capture, &pcm);
  1393. } else {
  1394. if (rtd->dai_link->dynamic)
  1395. snprintf(new_name, sizeof(new_name), "%s (*)",
  1396. rtd->dai_link->stream_name);
  1397. else
  1398. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  1399. rtd->dai_link->stream_name, codec_dai->name, num);
  1400. ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
  1401. capture, &pcm);
  1402. }
  1403. if (ret < 0) {
  1404. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  1405. return ret;
  1406. }
  1407. dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name);
  1408. /* DAPM dai link stream work */
  1409. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  1410. rtd->pcm = pcm;
  1411. pcm->private_data = rtd;
  1412. if (rtd->dai_link->no_pcm) {
  1413. if (playback)
  1414. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  1415. if (capture)
  1416. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  1417. goto out;
  1418. }
  1419. /* ASoC PCM operations */
  1420. if (rtd->dai_link->dynamic) {
  1421. rtd->ops.open = dpcm_fe_dai_open;
  1422. rtd->ops.hw_params = dpcm_fe_dai_hw_params;
  1423. rtd->ops.prepare = dpcm_fe_dai_prepare;
  1424. rtd->ops.trigger = dpcm_fe_dai_trigger;
  1425. rtd->ops.hw_free = dpcm_fe_dai_hw_free;
  1426. rtd->ops.close = dpcm_fe_dai_close;
  1427. rtd->ops.pointer = soc_pcm_pointer;
  1428. } else {
  1429. rtd->ops.open = soc_pcm_open;
  1430. rtd->ops.hw_params = soc_pcm_hw_params;
  1431. rtd->ops.prepare = soc_pcm_prepare;
  1432. rtd->ops.trigger = soc_pcm_trigger;
  1433. rtd->ops.hw_free = soc_pcm_hw_free;
  1434. rtd->ops.close = soc_pcm_close;
  1435. rtd->ops.pointer = soc_pcm_pointer;
  1436. }
  1437. if (platform->driver->ops) {
  1438. rtd->ops.ack = platform->driver->ops->ack;
  1439. rtd->ops.copy = platform->driver->ops->copy;
  1440. rtd->ops.silence = platform->driver->ops->silence;
  1441. rtd->ops.page = platform->driver->ops->page;
  1442. rtd->ops.mmap = platform->driver->ops->mmap;
  1443. }
  1444. if (playback)
  1445. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
  1446. if (capture)
  1447. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
  1448. if (platform->driver->pcm_new) {
  1449. ret = platform->driver->pcm_new(rtd);
  1450. if (ret < 0) {
  1451. pr_err("asoc: platform pcm constructor failed\n");
  1452. return ret;
  1453. }
  1454. }
  1455. pcm->private_free = platform->driver->pcm_free;
  1456. out:
  1457. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  1458. cpu_dai->name);
  1459. return ret;
  1460. }
  1461. /* is the current PCM operation for this FE ? */
  1462. int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
  1463. {
  1464. if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
  1465. return 1;
  1466. return 0;
  1467. }
  1468. EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
  1469. /* is the current PCM operation for this BE ? */
  1470. int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
  1471. struct snd_soc_pcm_runtime *be, int stream)
  1472. {
  1473. if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
  1474. ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
  1475. be->dpcm[stream].runtime_update))
  1476. return 1;
  1477. return 0;
  1478. }
  1479. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
  1480. /* get the substream for this BE */
  1481. struct snd_pcm_substream *
  1482. snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
  1483. {
  1484. return be->pcm->streams[stream].substream;
  1485. }
  1486. EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
  1487. /* get the BE runtime state */
  1488. enum snd_soc_dpcm_state
  1489. snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
  1490. {
  1491. return be->dpcm[stream].state;
  1492. }
  1493. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
  1494. /* set the BE runtime state */
  1495. void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
  1496. int stream, enum snd_soc_dpcm_state state)
  1497. {
  1498. be->dpcm[stream].state = state;
  1499. }
  1500. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
  1501. /*
  1502. * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
  1503. * are not running, paused or suspended for the specified stream direction.
  1504. */
  1505. int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
  1506. struct snd_soc_pcm_runtime *be, int stream)
  1507. {
  1508. struct snd_soc_dpcm *dpcm;
  1509. int state;
  1510. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  1511. if (dpcm->fe == fe)
  1512. continue;
  1513. state = dpcm->fe->dpcm[stream].state;
  1514. if (state == SND_SOC_DPCM_STATE_START ||
  1515. state == SND_SOC_DPCM_STATE_PAUSED ||
  1516. state == SND_SOC_DPCM_STATE_SUSPEND)
  1517. return 0;
  1518. }
  1519. /* it's safe to free/stop this BE DAI */
  1520. return 1;
  1521. }
  1522. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
  1523. /*
  1524. * We can only change hw params a BE DAI if any of it's FE are not prepared,
  1525. * running, paused or suspended for the specified stream direction.
  1526. */
  1527. int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
  1528. struct snd_soc_pcm_runtime *be, int stream)
  1529. {
  1530. struct snd_soc_dpcm *dpcm;
  1531. int state;
  1532. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  1533. if (dpcm->fe == fe)
  1534. continue;
  1535. state = dpcm->fe->dpcm[stream].state;
  1536. if (state == SND_SOC_DPCM_STATE_START ||
  1537. state == SND_SOC_DPCM_STATE_PAUSED ||
  1538. state == SND_SOC_DPCM_STATE_SUSPEND ||
  1539. state == SND_SOC_DPCM_STATE_PREPARE)
  1540. return 0;
  1541. }
  1542. /* it's safe to change hw_params */
  1543. return 1;
  1544. }
  1545. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
  1546. #ifdef CONFIG_DEBUG_FS
  1547. static char *dpcm_state_string(enum snd_soc_dpcm_state state)
  1548. {
  1549. switch (state) {
  1550. case SND_SOC_DPCM_STATE_NEW:
  1551. return "new";
  1552. case SND_SOC_DPCM_STATE_OPEN:
  1553. return "open";
  1554. case SND_SOC_DPCM_STATE_HW_PARAMS:
  1555. return "hw_params";
  1556. case SND_SOC_DPCM_STATE_PREPARE:
  1557. return "prepare";
  1558. case SND_SOC_DPCM_STATE_START:
  1559. return "start";
  1560. case SND_SOC_DPCM_STATE_STOP:
  1561. return "stop";
  1562. case SND_SOC_DPCM_STATE_SUSPEND:
  1563. return "suspend";
  1564. case SND_SOC_DPCM_STATE_PAUSED:
  1565. return "paused";
  1566. case SND_SOC_DPCM_STATE_HW_FREE:
  1567. return "hw_free";
  1568. case SND_SOC_DPCM_STATE_CLOSE:
  1569. return "close";
  1570. }
  1571. return "unknown";
  1572. }
  1573. static int dpcm_state_open_file(struct inode *inode, struct file *file)
  1574. {
  1575. file->private_data = inode->i_private;
  1576. return 0;
  1577. }
  1578. static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
  1579. int stream, char *buf, size_t size)
  1580. {
  1581. struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
  1582. struct snd_soc_dpcm *dpcm;
  1583. ssize_t offset = 0;
  1584. /* FE state */
  1585. offset += snprintf(buf + offset, size - offset,
  1586. "[%s - %s]\n", fe->dai_link->name,
  1587. stream ? "Capture" : "Playback");
  1588. offset += snprintf(buf + offset, size - offset, "State: %s\n",
  1589. dpcm_state_string(fe->dpcm[stream].state));
  1590. if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1591. (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  1592. offset += snprintf(buf + offset, size - offset,
  1593. "Hardware Params: "
  1594. "Format = %s, Channels = %d, Rate = %d\n",
  1595. snd_pcm_format_name(params_format(params)),
  1596. params_channels(params),
  1597. params_rate(params));
  1598. /* BEs state */
  1599. offset += snprintf(buf + offset, size - offset, "Backends:\n");
  1600. if (list_empty(&fe->dpcm[stream].be_clients)) {
  1601. offset += snprintf(buf + offset, size - offset,
  1602. " No active DSP links\n");
  1603. goto out;
  1604. }
  1605. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1606. struct snd_soc_pcm_runtime *be = dpcm->be;
  1607. params = &dpcm->hw_params;
  1608. offset += snprintf(buf + offset, size - offset,
  1609. "- %s\n", be->dai_link->name);
  1610. offset += snprintf(buf + offset, size - offset,
  1611. " State: %s\n",
  1612. dpcm_state_string(be->dpcm[stream].state));
  1613. if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1614. (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  1615. offset += snprintf(buf + offset, size - offset,
  1616. " Hardware Params: "
  1617. "Format = %s, Channels = %d, Rate = %d\n",
  1618. snd_pcm_format_name(params_format(params)),
  1619. params_channels(params),
  1620. params_rate(params));
  1621. }
  1622. out:
  1623. return offset;
  1624. }
  1625. static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
  1626. size_t count, loff_t *ppos)
  1627. {
  1628. struct snd_soc_pcm_runtime *fe = file->private_data;
  1629. ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
  1630. char *buf;
  1631. buf = kmalloc(out_count, GFP_KERNEL);
  1632. if (!buf)
  1633. return -ENOMEM;
  1634. if (fe->cpu_dai->driver->playback.channels_min)
  1635. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
  1636. buf + offset, out_count - offset);
  1637. if (fe->cpu_dai->driver->capture.channels_min)
  1638. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
  1639. buf + offset, out_count - offset);
  1640. ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
  1641. kfree(buf);
  1642. return ret;
  1643. }
  1644. static const struct file_operations dpcm_state_fops = {
  1645. .open = dpcm_state_open_file,
  1646. .read = dpcm_state_read_file,
  1647. .llseek = default_llseek,
  1648. };
  1649. int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
  1650. {
  1651. rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
  1652. rtd->card->debugfs_card_root);
  1653. if (!rtd->debugfs_dpcm_root) {
  1654. dev_dbg(rtd->dev,
  1655. "ASoC: Failed to create dpcm debugfs directory %s\n",
  1656. rtd->dai_link->name);
  1657. return -EINVAL;
  1658. }
  1659. rtd->debugfs_dpcm_state = debugfs_create_file("state", 0644,
  1660. rtd->debugfs_dpcm_root,
  1661. rtd, &dpcm_state_fops);
  1662. return 0;
  1663. }
  1664. #endif