soc-pcm.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  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. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  333. snd_soc_dai_digital_mute(codec_dai, 1);
  334. if (cpu_dai->driver->ops->shutdown)
  335. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  336. if (codec_dai->driver->ops->shutdown)
  337. codec_dai->driver->ops->shutdown(substream, codec_dai);
  338. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  339. rtd->dai_link->ops->shutdown(substream);
  340. if (platform->driver->ops && platform->driver->ops->close)
  341. platform->driver->ops->close(substream);
  342. cpu_dai->runtime = NULL;
  343. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  344. if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
  345. rtd->dai_link->ignore_pmdown_time) {
  346. /* powered down playback stream now */
  347. snd_soc_dapm_stream_event(rtd,
  348. SNDRV_PCM_STREAM_PLAYBACK,
  349. SND_SOC_DAPM_STREAM_STOP);
  350. } else {
  351. /* start delayed pop wq here for playback streams */
  352. rtd->pop_wait = 1;
  353. schedule_delayed_work(&rtd->delayed_work,
  354. msecs_to_jiffies(rtd->pmdown_time));
  355. }
  356. } else {
  357. /* capture streams can be powered down now */
  358. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
  359. SND_SOC_DAPM_STREAM_STOP);
  360. }
  361. mutex_unlock(&rtd->pcm_mutex);
  362. pm_runtime_put(platform->dev);
  363. pm_runtime_put(codec_dai->dev);
  364. pm_runtime_put(cpu_dai->dev);
  365. return 0;
  366. }
  367. /*
  368. * Called by ALSA when the PCM substream is prepared, can set format, sample
  369. * rate, etc. This function is non atomic and can be called multiple times,
  370. * it can refer to the runtime info.
  371. */
  372. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  373. {
  374. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  375. struct snd_soc_platform *platform = rtd->platform;
  376. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  377. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  378. int ret = 0;
  379. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  380. if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
  381. ret = rtd->dai_link->ops->prepare(substream);
  382. if (ret < 0) {
  383. dev_err(rtd->card->dev, "ASoC: machine prepare error:"
  384. " %d\n", ret);
  385. goto out;
  386. }
  387. }
  388. if (platform->driver->ops && platform->driver->ops->prepare) {
  389. ret = platform->driver->ops->prepare(substream);
  390. if (ret < 0) {
  391. dev_err(platform->dev, "ASoC: platform prepare error:"
  392. " %d\n", ret);
  393. goto out;
  394. }
  395. }
  396. if (codec_dai->driver->ops->prepare) {
  397. ret = codec_dai->driver->ops->prepare(substream, codec_dai);
  398. if (ret < 0) {
  399. dev_err(codec_dai->dev, "ASoC: DAI prepare error: %d\n",
  400. ret);
  401. goto out;
  402. }
  403. }
  404. if (cpu_dai->driver->ops->prepare) {
  405. ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
  406. if (ret < 0) {
  407. dev_err(cpu_dai->dev, "ASoC: DAI prepare error: %d\n",
  408. ret);
  409. goto out;
  410. }
  411. }
  412. /* cancel any delayed stream shutdown that is pending */
  413. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  414. rtd->pop_wait) {
  415. rtd->pop_wait = 0;
  416. cancel_delayed_work(&rtd->delayed_work);
  417. }
  418. snd_soc_dapm_stream_event(rtd, substream->stream,
  419. SND_SOC_DAPM_STREAM_START);
  420. snd_soc_dai_digital_mute(codec_dai, 0);
  421. out:
  422. mutex_unlock(&rtd->pcm_mutex);
  423. return ret;
  424. }
  425. /*
  426. * Called by ALSA when the hardware params are set by application. This
  427. * function can also be called multiple times and can allocate buffers
  428. * (using snd_pcm_lib_* ). It's non-atomic.
  429. */
  430. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  431. struct snd_pcm_hw_params *params)
  432. {
  433. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  434. struct snd_soc_platform *platform = rtd->platform;
  435. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  436. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  437. int ret = 0;
  438. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  439. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
  440. ret = rtd->dai_link->ops->hw_params(substream, params);
  441. if (ret < 0) {
  442. dev_err(rtd->card->dev, "ASoC: machine hw_params"
  443. " failed: %d\n", ret);
  444. goto out;
  445. }
  446. }
  447. if (codec_dai->driver->ops->hw_params) {
  448. ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
  449. if (ret < 0) {
  450. dev_err(codec_dai->dev, "ASoC: can't set %s hw params:"
  451. " %d\n", codec_dai->name, ret);
  452. goto codec_err;
  453. }
  454. }
  455. if (cpu_dai->driver->ops->hw_params) {
  456. ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
  457. if (ret < 0) {
  458. dev_err(cpu_dai->dev, "ASoC: %s hw params failed: %d\n",
  459. cpu_dai->name, ret);
  460. goto interface_err;
  461. }
  462. }
  463. if (platform->driver->ops && platform->driver->ops->hw_params) {
  464. ret = platform->driver->ops->hw_params(substream, params);
  465. if (ret < 0) {
  466. dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
  467. platform->name, ret);
  468. goto platform_err;
  469. }
  470. }
  471. /* store the rate for each DAIs */
  472. cpu_dai->rate = params_rate(params);
  473. codec_dai->rate = params_rate(params);
  474. out:
  475. mutex_unlock(&rtd->pcm_mutex);
  476. return ret;
  477. platform_err:
  478. if (cpu_dai->driver->ops->hw_free)
  479. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  480. interface_err:
  481. if (codec_dai->driver->ops->hw_free)
  482. codec_dai->driver->ops->hw_free(substream, codec_dai);
  483. codec_err:
  484. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  485. rtd->dai_link->ops->hw_free(substream);
  486. mutex_unlock(&rtd->pcm_mutex);
  487. return ret;
  488. }
  489. /*
  490. * Frees resources allocated by hw_params, can be called multiple times
  491. */
  492. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  493. {
  494. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  495. struct snd_soc_platform *platform = rtd->platform;
  496. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  497. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  498. struct snd_soc_codec *codec = rtd->codec;
  499. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  500. /* apply codec digital mute */
  501. if (!codec->active)
  502. snd_soc_dai_digital_mute(codec_dai, 1);
  503. /* free any machine hw params */
  504. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  505. rtd->dai_link->ops->hw_free(substream);
  506. /* free any DMA resources */
  507. if (platform->driver->ops && platform->driver->ops->hw_free)
  508. platform->driver->ops->hw_free(substream);
  509. /* now free hw params for the DAIs */
  510. if (codec_dai->driver->ops->hw_free)
  511. codec_dai->driver->ops->hw_free(substream, codec_dai);
  512. if (cpu_dai->driver->ops->hw_free)
  513. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  514. mutex_unlock(&rtd->pcm_mutex);
  515. return 0;
  516. }
  517. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  518. {
  519. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  520. struct snd_soc_platform *platform = rtd->platform;
  521. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  522. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  523. int ret;
  524. if (codec_dai->driver->ops->trigger) {
  525. ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
  526. if (ret < 0)
  527. return ret;
  528. }
  529. if (platform->driver->ops && platform->driver->ops->trigger) {
  530. ret = platform->driver->ops->trigger(substream, cmd);
  531. if (ret < 0)
  532. return ret;
  533. }
  534. if (cpu_dai->driver->ops->trigger) {
  535. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  536. if (ret < 0)
  537. return ret;
  538. }
  539. return 0;
  540. }
  541. static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
  542. int cmd)
  543. {
  544. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  545. struct snd_soc_platform *platform = rtd->platform;
  546. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  547. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  548. int ret;
  549. if (codec_dai->driver->ops->bespoke_trigger) {
  550. ret = codec_dai->driver->ops->bespoke_trigger(substream, cmd, codec_dai);
  551. if (ret < 0)
  552. return ret;
  553. }
  554. if (platform->driver->bespoke_trigger) {
  555. ret = platform->driver->bespoke_trigger(substream, cmd);
  556. if (ret < 0)
  557. return ret;
  558. }
  559. if (cpu_dai->driver->ops->bespoke_trigger) {
  560. ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
  561. if (ret < 0)
  562. return ret;
  563. }
  564. return 0;
  565. }
  566. /*
  567. * soc level wrapper for pointer callback
  568. * If cpu_dai, codec_dai, platform driver has the delay callback, than
  569. * the runtime->delay will be updated accordingly.
  570. */
  571. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  572. {
  573. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  574. struct snd_soc_platform *platform = rtd->platform;
  575. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  576. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  577. struct snd_pcm_runtime *runtime = substream->runtime;
  578. snd_pcm_uframes_t offset = 0;
  579. snd_pcm_sframes_t delay = 0;
  580. if (platform->driver->ops && platform->driver->ops->pointer)
  581. offset = platform->driver->ops->pointer(substream);
  582. if (cpu_dai->driver->ops->delay)
  583. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  584. if (codec_dai->driver->ops->delay)
  585. delay += codec_dai->driver->ops->delay(substream, codec_dai);
  586. if (platform->driver->delay)
  587. delay += platform->driver->delay(substream, codec_dai);
  588. runtime->delay = delay;
  589. return offset;
  590. }
  591. /* connect a FE and BE */
  592. static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
  593. struct snd_soc_pcm_runtime *be, int stream)
  594. {
  595. struct snd_soc_dpcm *dpcm;
  596. /* only add new dpcms */
  597. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  598. if (dpcm->be == be && dpcm->fe == fe)
  599. return 0;
  600. }
  601. dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
  602. if (!dpcm)
  603. return -ENOMEM;
  604. dpcm->be = be;
  605. dpcm->fe = fe;
  606. be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
  607. dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
  608. list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
  609. list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
  610. dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
  611. stream ? "capture" : "playback", fe->dai_link->name,
  612. stream ? "<-" : "->", be->dai_link->name);
  613. #ifdef CONFIG_DEBUG_FS
  614. dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
  615. fe->debugfs_dpcm_root, &dpcm->state);
  616. #endif
  617. return 1;
  618. }
  619. /* reparent a BE onto another FE */
  620. static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
  621. struct snd_soc_pcm_runtime *be, int stream)
  622. {
  623. struct snd_soc_dpcm *dpcm;
  624. struct snd_pcm_substream *fe_substream, *be_substream;
  625. /* reparent if BE is connected to other FEs */
  626. if (!be->dpcm[stream].users)
  627. return;
  628. be_substream = snd_soc_dpcm_get_substream(be, stream);
  629. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  630. if (dpcm->fe == fe)
  631. continue;
  632. dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
  633. stream ? "capture" : "playback",
  634. dpcm->fe->dai_link->name,
  635. stream ? "<-" : "->", dpcm->be->dai_link->name);
  636. fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
  637. be_substream->runtime = fe_substream->runtime;
  638. break;
  639. }
  640. }
  641. /* disconnect a BE and FE */
  642. static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
  643. {
  644. struct snd_soc_dpcm *dpcm, *d;
  645. list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
  646. dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
  647. stream ? "capture" : "playback",
  648. dpcm->be->dai_link->name);
  649. if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
  650. continue;
  651. dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
  652. stream ? "capture" : "playback", fe->dai_link->name,
  653. stream ? "<-" : "->", dpcm->be->dai_link->name);
  654. /* BEs still alive need new FE */
  655. dpcm_be_reparent(fe, dpcm->be, stream);
  656. #ifdef CONFIG_DEBUG_FS
  657. debugfs_remove(dpcm->debugfs_state);
  658. #endif
  659. list_del(&dpcm->list_be);
  660. list_del(&dpcm->list_fe);
  661. kfree(dpcm);
  662. }
  663. }
  664. /* get BE for DAI widget and stream */
  665. static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
  666. struct snd_soc_dapm_widget *widget, int stream)
  667. {
  668. struct snd_soc_pcm_runtime *be;
  669. int i;
  670. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  671. for (i = 0; i < card->num_links; i++) {
  672. be = &card->rtd[i];
  673. if (!be->dai_link->no_pcm)
  674. continue;
  675. if (be->cpu_dai->playback_widget == widget ||
  676. be->codec_dai->playback_widget == widget)
  677. return be;
  678. }
  679. } else {
  680. for (i = 0; i < card->num_links; i++) {
  681. be = &card->rtd[i];
  682. if (!be->dai_link->no_pcm)
  683. continue;
  684. if (be->cpu_dai->capture_widget == widget ||
  685. be->codec_dai->capture_widget == widget)
  686. return be;
  687. }
  688. }
  689. dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
  690. stream ? "capture" : "playback", widget->name);
  691. return NULL;
  692. }
  693. static inline struct snd_soc_dapm_widget *
  694. rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
  695. {
  696. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  697. return rtd->cpu_dai->playback_widget;
  698. else
  699. return rtd->cpu_dai->capture_widget;
  700. }
  701. static inline struct snd_soc_dapm_widget *
  702. rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
  703. {
  704. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  705. return rtd->codec_dai->playback_widget;
  706. else
  707. return rtd->codec_dai->capture_widget;
  708. }
  709. static int widget_in_list(struct snd_soc_dapm_widget_list *list,
  710. struct snd_soc_dapm_widget *widget)
  711. {
  712. int i;
  713. for (i = 0; i < list->num_widgets; i++) {
  714. if (widget == list->widgets[i])
  715. return 1;
  716. }
  717. return 0;
  718. }
  719. static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
  720. int stream, struct snd_soc_dapm_widget_list **list_)
  721. {
  722. struct snd_soc_dai *cpu_dai = fe->cpu_dai;
  723. struct snd_soc_dapm_widget_list *list;
  724. int paths;
  725. list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
  726. sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
  727. if (list == NULL)
  728. return -ENOMEM;
  729. /* get number of valid DAI paths and their widgets */
  730. paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
  731. dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
  732. stream ? "capture" : "playback");
  733. *list_ = list;
  734. return paths;
  735. }
  736. static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
  737. {
  738. kfree(*list);
  739. }
  740. static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
  741. struct snd_soc_dapm_widget_list **list_)
  742. {
  743. struct snd_soc_dpcm *dpcm;
  744. struct snd_soc_dapm_widget_list *list = *list_;
  745. struct snd_soc_dapm_widget *widget;
  746. int prune = 0;
  747. /* Destroy any old FE <--> BE connections */
  748. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  749. /* is there a valid CPU DAI widget for this BE */
  750. widget = rtd_get_cpu_widget(dpcm->be, stream);
  751. /* prune the BE if it's no longer in our active list */
  752. if (widget && widget_in_list(list, widget))
  753. continue;
  754. /* is there a valid CODEC DAI widget for this BE */
  755. widget = rtd_get_codec_widget(dpcm->be, stream);
  756. /* prune the BE if it's no longer in our active list */
  757. if (widget && widget_in_list(list, widget))
  758. continue;
  759. dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
  760. stream ? "capture" : "playback",
  761. dpcm->be->dai_link->name, fe->dai_link->name);
  762. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  763. dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  764. prune++;
  765. }
  766. dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
  767. return prune;
  768. }
  769. static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
  770. struct snd_soc_dapm_widget_list **list_)
  771. {
  772. struct snd_soc_card *card = fe->card;
  773. struct snd_soc_dapm_widget_list *list = *list_;
  774. struct snd_soc_pcm_runtime *be;
  775. int i, new = 0, err;
  776. /* Create any new FE <--> BE connections */
  777. for (i = 0; i < list->num_widgets; i++) {
  778. if (list->widgets[i]->id != snd_soc_dapm_dai)
  779. continue;
  780. /* is there a valid BE rtd for this widget */
  781. be = dpcm_get_be(card, list->widgets[i], stream);
  782. if (!be) {
  783. dev_err(fe->dev, "ASoC: no BE found for %s\n",
  784. list->widgets[i]->name);
  785. continue;
  786. }
  787. /* make sure BE is a real BE */
  788. if (!be->dai_link->no_pcm)
  789. continue;
  790. /* don't connect if FE is not running */
  791. if (!fe->dpcm[stream].runtime)
  792. continue;
  793. /* newly connected FE and BE */
  794. err = dpcm_be_connect(fe, be, stream);
  795. if (err < 0) {
  796. dev_err(fe->dev, "ASoC: can't connect %s\n",
  797. list->widgets[i]->name);
  798. break;
  799. } else if (err == 0) /* already connected */
  800. continue;
  801. /* new */
  802. be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  803. new++;
  804. }
  805. dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
  806. return new;
  807. }
  808. /*
  809. * Find the corresponding BE DAIs that source or sink audio to this
  810. * FE substream.
  811. */
  812. static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
  813. int stream, struct snd_soc_dapm_widget_list **list, int new)
  814. {
  815. if (new)
  816. return dpcm_add_paths(fe, stream, list);
  817. else
  818. return dpcm_prune_paths(fe, stream, list);
  819. }
  820. static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
  821. {
  822. struct snd_soc_dpcm *dpcm;
  823. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  824. dpcm->be->dpcm[stream].runtime_update =
  825. SND_SOC_DPCM_UPDATE_NO;
  826. }
  827. static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
  828. int stream)
  829. {
  830. struct snd_soc_dpcm *dpcm;
  831. /* disable any enabled and non active backends */
  832. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  833. struct snd_soc_pcm_runtime *be = dpcm->be;
  834. struct snd_pcm_substream *be_substream =
  835. snd_soc_dpcm_get_substream(be, stream);
  836. if (be->dpcm[stream].users == 0)
  837. dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
  838. stream ? "capture" : "playback",
  839. be->dpcm[stream].state);
  840. if (--be->dpcm[stream].users != 0)
  841. continue;
  842. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  843. continue;
  844. soc_pcm_close(be_substream);
  845. be_substream->runtime = NULL;
  846. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  847. }
  848. }
  849. static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
  850. {
  851. struct snd_soc_dpcm *dpcm;
  852. int err, count = 0;
  853. /* only startup BE DAIs that are either sinks or sources to this FE DAI */
  854. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  855. struct snd_soc_pcm_runtime *be = dpcm->be;
  856. struct snd_pcm_substream *be_substream =
  857. snd_soc_dpcm_get_substream(be, stream);
  858. /* is this op for this BE ? */
  859. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  860. continue;
  861. /* first time the dpcm is open ? */
  862. if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
  863. dev_err(be->dev, "ASoC: too many users %s at open %d\n",
  864. stream ? "capture" : "playback",
  865. be->dpcm[stream].state);
  866. if (be->dpcm[stream].users++ != 0)
  867. continue;
  868. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
  869. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
  870. continue;
  871. dev_dbg(be->dev, "ASoC: open BE %s\n", be->dai_link->name);
  872. be_substream->runtime = be->dpcm[stream].runtime;
  873. err = soc_pcm_open(be_substream);
  874. if (err < 0) {
  875. dev_err(be->dev, "ASoC: BE open failed %d\n", err);
  876. be->dpcm[stream].users--;
  877. if (be->dpcm[stream].users < 0)
  878. dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
  879. stream ? "capture" : "playback",
  880. be->dpcm[stream].state);
  881. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  882. goto unwind;
  883. }
  884. be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  885. count++;
  886. }
  887. return count;
  888. unwind:
  889. /* disable any enabled and non active backends */
  890. list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  891. struct snd_soc_pcm_runtime *be = dpcm->be;
  892. struct snd_pcm_substream *be_substream =
  893. snd_soc_dpcm_get_substream(be, stream);
  894. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  895. continue;
  896. if (be->dpcm[stream].users == 0)
  897. dev_err(be->dev, "ASoC: no users %s at close %d\n",
  898. stream ? "capture" : "playback",
  899. be->dpcm[stream].state);
  900. if (--be->dpcm[stream].users != 0)
  901. continue;
  902. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  903. continue;
  904. soc_pcm_close(be_substream);
  905. be_substream->runtime = NULL;
  906. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  907. }
  908. return err;
  909. }
  910. static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
  911. {
  912. struct snd_pcm_runtime *runtime = substream->runtime;
  913. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  914. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  915. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  916. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  917. runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
  918. runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
  919. runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
  920. runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
  921. runtime->hw.formats &= cpu_dai_drv->playback.formats;
  922. runtime->hw.rates = cpu_dai_drv->playback.rates;
  923. } else {
  924. runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
  925. runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
  926. runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
  927. runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
  928. runtime->hw.formats &= cpu_dai_drv->capture.formats;
  929. runtime->hw.rates = cpu_dai_drv->capture.rates;
  930. }
  931. }
  932. static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
  933. {
  934. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  935. struct snd_pcm_runtime *runtime = fe_substream->runtime;
  936. int stream = fe_substream->stream, ret = 0;
  937. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  938. ret = dpcm_be_dai_startup(fe, fe_substream->stream);
  939. if (ret < 0) {
  940. dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
  941. goto be_err;
  942. }
  943. dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
  944. /* start the DAI frontend */
  945. ret = soc_pcm_open(fe_substream);
  946. if (ret < 0) {
  947. dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
  948. goto unwind;
  949. }
  950. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  951. dpcm_set_fe_runtime(fe_substream);
  952. snd_pcm_limit_hw_rates(runtime);
  953. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  954. return 0;
  955. unwind:
  956. dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
  957. be_err:
  958. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  959. return ret;
  960. }
  961. static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  962. {
  963. struct snd_soc_dpcm *dpcm;
  964. /* only shutdown BEs that are either sinks or sources to this FE DAI */
  965. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  966. struct snd_soc_pcm_runtime *be = dpcm->be;
  967. struct snd_pcm_substream *be_substream =
  968. snd_soc_dpcm_get_substream(be, stream);
  969. /* is this op for this BE ? */
  970. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  971. continue;
  972. if (be->dpcm[stream].users == 0)
  973. dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
  974. stream ? "capture" : "playback",
  975. be->dpcm[stream].state);
  976. if (--be->dpcm[stream].users != 0)
  977. continue;
  978. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  979. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
  980. continue;
  981. dev_dbg(be->dev, "ASoC: close BE %s\n",
  982. dpcm->fe->dai_link->name);
  983. soc_pcm_close(be_substream);
  984. be_substream->runtime = NULL;
  985. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  986. }
  987. return 0;
  988. }
  989. static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
  990. {
  991. struct snd_soc_pcm_runtime *fe = substream->private_data;
  992. int stream = substream->stream;
  993. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  994. /* shutdown the BEs */
  995. dpcm_be_dai_shutdown(fe, substream->stream);
  996. dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
  997. /* now shutdown the frontend */
  998. soc_pcm_close(substream);
  999. /* run the stream event for each BE */
  1000. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  1001. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1002. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1003. return 0;
  1004. }
  1005. static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
  1006. {
  1007. struct snd_soc_dpcm *dpcm;
  1008. /* only hw_params backends that are either sinks or sources
  1009. * to this frontend DAI */
  1010. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1011. struct snd_soc_pcm_runtime *be = dpcm->be;
  1012. struct snd_pcm_substream *be_substream =
  1013. snd_soc_dpcm_get_substream(be, stream);
  1014. /* is this op for this BE ? */
  1015. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1016. continue;
  1017. /* only free hw when no longer used - check all FEs */
  1018. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1019. continue;
  1020. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1021. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1022. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  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. return ret;
  1406. }
  1407. /* keep going if FE state is > open */
  1408. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
  1409. return 0;
  1410. ret = dpcm_be_dai_hw_params(fe, stream);
  1411. if (ret < 0) {
  1412. goto close;
  1413. return ret;
  1414. }
  1415. /* keep going if FE state is > hw_params */
  1416. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
  1417. return 0;
  1418. ret = dpcm_be_dai_prepare(fe, stream);
  1419. if (ret < 0) {
  1420. goto hw_free;
  1421. return ret;
  1422. }
  1423. /* run the stream event for each BE */
  1424. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
  1425. /* keep going if FE state is > prepare */
  1426. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
  1427. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
  1428. return 0;
  1429. if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
  1430. /* call trigger on the frontend - FE takes care of all BE triggers */
  1431. dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
  1432. fe->dai_link->name);
  1433. ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
  1434. if (ret < 0) {
  1435. dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
  1436. goto hw_free;
  1437. }
  1438. } else {
  1439. dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
  1440. fe->dai_link->name);
  1441. ret = dpcm_be_dai_trigger(fe, stream,
  1442. SNDRV_PCM_TRIGGER_START);
  1443. if (ret < 0) {
  1444. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  1445. goto hw_free;
  1446. }
  1447. }
  1448. return 0;
  1449. hw_free:
  1450. dpcm_be_dai_hw_free(fe, stream);
  1451. close:
  1452. dpcm_be_dai_shutdown(fe, stream);
  1453. disconnect:
  1454. /* disconnect any non started BEs */
  1455. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1456. struct snd_soc_pcm_runtime *be = dpcm->be;
  1457. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1458. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1459. }
  1460. return ret;
  1461. }
  1462. static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
  1463. {
  1464. int ret;
  1465. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1466. ret = dpcm_run_update_startup(fe, stream);
  1467. if (ret < 0)
  1468. dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
  1469. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1470. return ret;
  1471. }
  1472. static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
  1473. {
  1474. int ret;
  1475. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1476. ret = dpcm_run_update_shutdown(fe, stream);
  1477. if (ret < 0)
  1478. dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
  1479. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1480. return ret;
  1481. }
  1482. /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
  1483. * any DAI links.
  1484. */
  1485. int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
  1486. {
  1487. struct snd_soc_card *card;
  1488. int i, old, new, paths;
  1489. if (widget->codec)
  1490. card = widget->codec->card;
  1491. else if (widget->platform)
  1492. card = widget->platform->card;
  1493. else
  1494. return -EINVAL;
  1495. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1496. for (i = 0; i < card->num_rtd; i++) {
  1497. struct snd_soc_dapm_widget_list *list;
  1498. struct snd_soc_pcm_runtime *fe = &card->rtd[i];
  1499. /* make sure link is FE */
  1500. if (!fe->dai_link->dynamic)
  1501. continue;
  1502. /* only check active links */
  1503. if (!fe->cpu_dai->active)
  1504. continue;
  1505. /* DAPM sync will call this to update DSP paths */
  1506. dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
  1507. fe->dai_link->name);
  1508. /* skip if FE doesn't have playback capability */
  1509. if (!fe->cpu_dai->driver->playback.channels_min)
  1510. goto capture;
  1511. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
  1512. if (paths < 0) {
  1513. dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
  1514. fe->dai_link->name, "playback");
  1515. mutex_unlock(&card->mutex);
  1516. return paths;
  1517. }
  1518. /* update any new playback paths */
  1519. new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
  1520. if (new) {
  1521. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1522. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1523. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1524. }
  1525. /* update any old playback paths */
  1526. old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
  1527. if (old) {
  1528. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1529. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1530. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1531. }
  1532. capture:
  1533. /* skip if FE doesn't have capture capability */
  1534. if (!fe->cpu_dai->driver->capture.channels_min)
  1535. continue;
  1536. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
  1537. if (paths < 0) {
  1538. dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
  1539. fe->dai_link->name, "capture");
  1540. mutex_unlock(&card->mutex);
  1541. return paths;
  1542. }
  1543. /* update any new capture paths */
  1544. new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
  1545. if (new) {
  1546. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  1547. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  1548. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  1549. }
  1550. /* update any old capture paths */
  1551. old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
  1552. if (old) {
  1553. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  1554. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  1555. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  1556. }
  1557. dpcm_path_put(&list);
  1558. }
  1559. mutex_unlock(&card->mutex);
  1560. return 0;
  1561. }
  1562. int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
  1563. {
  1564. struct snd_soc_dpcm *dpcm;
  1565. struct list_head *clients =
  1566. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
  1567. list_for_each_entry(dpcm, clients, list_be) {
  1568. struct snd_soc_pcm_runtime *be = dpcm->be;
  1569. struct snd_soc_dai *dai = be->codec_dai;
  1570. struct snd_soc_dai_driver *drv = dai->driver;
  1571. if (be->dai_link->ignore_suspend)
  1572. continue;
  1573. dev_dbg(be->dev, "ASoC: BE digital mute %s\n", be->dai_link->name);
  1574. if (drv->ops->digital_mute && dai->playback_active)
  1575. drv->ops->digital_mute(dai, mute);
  1576. }
  1577. return 0;
  1578. }
  1579. static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
  1580. {
  1581. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1582. struct snd_soc_dpcm *dpcm;
  1583. struct snd_soc_dapm_widget_list *list;
  1584. int ret;
  1585. int stream = fe_substream->stream;
  1586. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1587. fe->dpcm[stream].runtime = fe_substream->runtime;
  1588. if (dpcm_path_get(fe, stream, &list) <= 0) {
  1589. dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
  1590. fe->dai_link->name, stream ? "capture" : "playback");
  1591. }
  1592. /* calculate valid and active FE <-> BE dpcms */
  1593. dpcm_process_paths(fe, stream, &list, 1);
  1594. ret = dpcm_fe_dai_startup(fe_substream);
  1595. if (ret < 0) {
  1596. /* clean up all links */
  1597. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  1598. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1599. dpcm_be_disconnect(fe, stream);
  1600. fe->dpcm[stream].runtime = NULL;
  1601. }
  1602. dpcm_clear_pending_state(fe, stream);
  1603. dpcm_path_put(&list);
  1604. mutex_unlock(&fe->card->mutex);
  1605. return ret;
  1606. }
  1607. static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
  1608. {
  1609. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1610. struct snd_soc_dpcm *dpcm;
  1611. int stream = fe_substream->stream, ret;
  1612. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1613. ret = dpcm_fe_dai_shutdown(fe_substream);
  1614. /* mark FE's links ready to prune */
  1615. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  1616. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1617. dpcm_be_disconnect(fe, stream);
  1618. fe->dpcm[stream].runtime = NULL;
  1619. mutex_unlock(&fe->card->mutex);
  1620. return ret;
  1621. }
  1622. /* create a new pcm */
  1623. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  1624. {
  1625. struct snd_soc_platform *platform = rtd->platform;
  1626. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1627. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1628. struct snd_pcm *pcm;
  1629. char new_name[64];
  1630. int ret = 0, playback = 0, capture = 0;
  1631. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
  1632. if (cpu_dai->driver->playback.channels_min)
  1633. playback = 1;
  1634. if (cpu_dai->driver->capture.channels_min)
  1635. capture = 1;
  1636. } else {
  1637. if (codec_dai->driver->playback.channels_min)
  1638. playback = 1;
  1639. if (codec_dai->driver->capture.channels_min)
  1640. capture = 1;
  1641. }
  1642. /* create the PCM */
  1643. if (rtd->dai_link->no_pcm) {
  1644. snprintf(new_name, sizeof(new_name), "(%s)",
  1645. rtd->dai_link->stream_name);
  1646. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  1647. playback, capture, &pcm);
  1648. } else {
  1649. if (rtd->dai_link->dynamic)
  1650. snprintf(new_name, sizeof(new_name), "%s (*)",
  1651. rtd->dai_link->stream_name);
  1652. else
  1653. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  1654. rtd->dai_link->stream_name, codec_dai->name, num);
  1655. ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
  1656. capture, &pcm);
  1657. }
  1658. if (ret < 0) {
  1659. dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
  1660. rtd->dai_link->name);
  1661. return ret;
  1662. }
  1663. dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
  1664. /* DAPM dai link stream work */
  1665. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  1666. rtd->pcm = pcm;
  1667. pcm->private_data = rtd;
  1668. if (rtd->dai_link->no_pcm) {
  1669. if (playback)
  1670. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  1671. if (capture)
  1672. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  1673. goto out;
  1674. }
  1675. /* ASoC PCM operations */
  1676. if (rtd->dai_link->dynamic) {
  1677. rtd->ops.open = dpcm_fe_dai_open;
  1678. rtd->ops.hw_params = dpcm_fe_dai_hw_params;
  1679. rtd->ops.prepare = dpcm_fe_dai_prepare;
  1680. rtd->ops.trigger = dpcm_fe_dai_trigger;
  1681. rtd->ops.hw_free = dpcm_fe_dai_hw_free;
  1682. rtd->ops.close = dpcm_fe_dai_close;
  1683. rtd->ops.pointer = soc_pcm_pointer;
  1684. rtd->ops.ioctl = soc_pcm_ioctl;
  1685. } else {
  1686. rtd->ops.open = soc_pcm_open;
  1687. rtd->ops.hw_params = soc_pcm_hw_params;
  1688. rtd->ops.prepare = soc_pcm_prepare;
  1689. rtd->ops.trigger = soc_pcm_trigger;
  1690. rtd->ops.hw_free = soc_pcm_hw_free;
  1691. rtd->ops.close = soc_pcm_close;
  1692. rtd->ops.pointer = soc_pcm_pointer;
  1693. rtd->ops.ioctl = soc_pcm_ioctl;
  1694. }
  1695. if (platform->driver->ops) {
  1696. rtd->ops.ack = platform->driver->ops->ack;
  1697. rtd->ops.copy = platform->driver->ops->copy;
  1698. rtd->ops.silence = platform->driver->ops->silence;
  1699. rtd->ops.page = platform->driver->ops->page;
  1700. rtd->ops.mmap = platform->driver->ops->mmap;
  1701. }
  1702. if (playback)
  1703. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
  1704. if (capture)
  1705. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
  1706. if (platform->driver->pcm_new) {
  1707. ret = platform->driver->pcm_new(rtd);
  1708. if (ret < 0) {
  1709. dev_err(platform->dev,
  1710. "ASoC: pcm constructor failed: %d\n",
  1711. ret);
  1712. return ret;
  1713. }
  1714. }
  1715. pcm->private_free = platform->driver->pcm_free;
  1716. out:
  1717. dev_info(rtd->card->dev, " %s <-> %s mapping ok\n", codec_dai->name,
  1718. cpu_dai->name);
  1719. return ret;
  1720. }
  1721. /* is the current PCM operation for this FE ? */
  1722. int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
  1723. {
  1724. if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
  1725. return 1;
  1726. return 0;
  1727. }
  1728. EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
  1729. /* is the current PCM operation for this BE ? */
  1730. int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
  1731. struct snd_soc_pcm_runtime *be, int stream)
  1732. {
  1733. if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
  1734. ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
  1735. be->dpcm[stream].runtime_update))
  1736. return 1;
  1737. return 0;
  1738. }
  1739. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
  1740. /* get the substream for this BE */
  1741. struct snd_pcm_substream *
  1742. snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
  1743. {
  1744. return be->pcm->streams[stream].substream;
  1745. }
  1746. EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
  1747. /* get the BE runtime state */
  1748. enum snd_soc_dpcm_state
  1749. snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
  1750. {
  1751. return be->dpcm[stream].state;
  1752. }
  1753. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
  1754. /* set the BE runtime state */
  1755. void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
  1756. int stream, enum snd_soc_dpcm_state state)
  1757. {
  1758. be->dpcm[stream].state = state;
  1759. }
  1760. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
  1761. /*
  1762. * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
  1763. * are not running, paused or suspended for the specified stream direction.
  1764. */
  1765. int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
  1766. struct snd_soc_pcm_runtime *be, int stream)
  1767. {
  1768. struct snd_soc_dpcm *dpcm;
  1769. int state;
  1770. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  1771. if (dpcm->fe == fe)
  1772. continue;
  1773. state = dpcm->fe->dpcm[stream].state;
  1774. if (state == SND_SOC_DPCM_STATE_START ||
  1775. state == SND_SOC_DPCM_STATE_PAUSED ||
  1776. state == SND_SOC_DPCM_STATE_SUSPEND)
  1777. return 0;
  1778. }
  1779. /* it's safe to free/stop this BE DAI */
  1780. return 1;
  1781. }
  1782. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
  1783. /*
  1784. * We can only change hw params a BE DAI if any of it's FE are not prepared,
  1785. * running, paused or suspended for the specified stream direction.
  1786. */
  1787. int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
  1788. struct snd_soc_pcm_runtime *be, int stream)
  1789. {
  1790. struct snd_soc_dpcm *dpcm;
  1791. int state;
  1792. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  1793. if (dpcm->fe == fe)
  1794. continue;
  1795. state = dpcm->fe->dpcm[stream].state;
  1796. if (state == SND_SOC_DPCM_STATE_START ||
  1797. state == SND_SOC_DPCM_STATE_PAUSED ||
  1798. state == SND_SOC_DPCM_STATE_SUSPEND ||
  1799. state == SND_SOC_DPCM_STATE_PREPARE)
  1800. return 0;
  1801. }
  1802. /* it's safe to change hw_params */
  1803. return 1;
  1804. }
  1805. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
  1806. int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
  1807. int cmd, struct snd_soc_platform *platform)
  1808. {
  1809. if (platform->driver->ops->trigger)
  1810. return platform->driver->ops->trigger(substream, cmd);
  1811. return 0;
  1812. }
  1813. EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
  1814. #ifdef CONFIG_DEBUG_FS
  1815. static char *dpcm_state_string(enum snd_soc_dpcm_state state)
  1816. {
  1817. switch (state) {
  1818. case SND_SOC_DPCM_STATE_NEW:
  1819. return "new";
  1820. case SND_SOC_DPCM_STATE_OPEN:
  1821. return "open";
  1822. case SND_SOC_DPCM_STATE_HW_PARAMS:
  1823. return "hw_params";
  1824. case SND_SOC_DPCM_STATE_PREPARE:
  1825. return "prepare";
  1826. case SND_SOC_DPCM_STATE_START:
  1827. return "start";
  1828. case SND_SOC_DPCM_STATE_STOP:
  1829. return "stop";
  1830. case SND_SOC_DPCM_STATE_SUSPEND:
  1831. return "suspend";
  1832. case SND_SOC_DPCM_STATE_PAUSED:
  1833. return "paused";
  1834. case SND_SOC_DPCM_STATE_HW_FREE:
  1835. return "hw_free";
  1836. case SND_SOC_DPCM_STATE_CLOSE:
  1837. return "close";
  1838. }
  1839. return "unknown";
  1840. }
  1841. static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
  1842. int stream, char *buf, size_t size)
  1843. {
  1844. struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
  1845. struct snd_soc_dpcm *dpcm;
  1846. ssize_t offset = 0;
  1847. /* FE state */
  1848. offset += snprintf(buf + offset, size - offset,
  1849. "[%s - %s]\n", fe->dai_link->name,
  1850. stream ? "Capture" : "Playback");
  1851. offset += snprintf(buf + offset, size - offset, "State: %s\n",
  1852. dpcm_state_string(fe->dpcm[stream].state));
  1853. if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1854. (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  1855. offset += snprintf(buf + offset, size - offset,
  1856. "Hardware Params: "
  1857. "Format = %s, Channels = %d, Rate = %d\n",
  1858. snd_pcm_format_name(params_format(params)),
  1859. params_channels(params),
  1860. params_rate(params));
  1861. /* BEs state */
  1862. offset += snprintf(buf + offset, size - offset, "Backends:\n");
  1863. if (list_empty(&fe->dpcm[stream].be_clients)) {
  1864. offset += snprintf(buf + offset, size - offset,
  1865. " No active DSP links\n");
  1866. goto out;
  1867. }
  1868. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1869. struct snd_soc_pcm_runtime *be = dpcm->be;
  1870. params = &dpcm->hw_params;
  1871. offset += snprintf(buf + offset, size - offset,
  1872. "- %s\n", be->dai_link->name);
  1873. offset += snprintf(buf + offset, size - offset,
  1874. " State: %s\n",
  1875. dpcm_state_string(be->dpcm[stream].state));
  1876. if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1877. (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  1878. offset += snprintf(buf + offset, size - offset,
  1879. " Hardware Params: "
  1880. "Format = %s, Channels = %d, Rate = %d\n",
  1881. snd_pcm_format_name(params_format(params)),
  1882. params_channels(params),
  1883. params_rate(params));
  1884. }
  1885. out:
  1886. return offset;
  1887. }
  1888. static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
  1889. size_t count, loff_t *ppos)
  1890. {
  1891. struct snd_soc_pcm_runtime *fe = file->private_data;
  1892. ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
  1893. char *buf;
  1894. buf = kmalloc(out_count, GFP_KERNEL);
  1895. if (!buf)
  1896. return -ENOMEM;
  1897. if (fe->cpu_dai->driver->playback.channels_min)
  1898. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
  1899. buf + offset, out_count - offset);
  1900. if (fe->cpu_dai->driver->capture.channels_min)
  1901. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
  1902. buf + offset, out_count - offset);
  1903. ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
  1904. kfree(buf);
  1905. return ret;
  1906. }
  1907. static const struct file_operations dpcm_state_fops = {
  1908. .open = simple_open,
  1909. .read = dpcm_state_read_file,
  1910. .llseek = default_llseek,
  1911. };
  1912. int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
  1913. {
  1914. if (!rtd->dai_link)
  1915. return 0;
  1916. rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
  1917. rtd->card->debugfs_card_root);
  1918. if (!rtd->debugfs_dpcm_root) {
  1919. dev_dbg(rtd->dev,
  1920. "ASoC: Failed to create dpcm debugfs directory %s\n",
  1921. rtd->dai_link->name);
  1922. return -EINVAL;
  1923. }
  1924. rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
  1925. rtd->debugfs_dpcm_root,
  1926. rtd, &dpcm_state_fops);
  1927. return 0;
  1928. }
  1929. #endif