soc-pcm.c 65 KB

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