soc-pcm.c 64 KB

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