soc-pcm.c 65 KB

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