soc-pcm.c 65 KB

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