soc-pcm.c 64 KB

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