soc-pcm.c 65 KB

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