soc-pcm.c 66 KB

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