soc-pcm.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265
  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. /*
  539. * soc level wrapper for pointer callback
  540. * If cpu_dai, codec_dai, platform driver has the delay callback, than
  541. * the runtime->delay will be updated accordingly.
  542. */
  543. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  544. {
  545. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  546. struct snd_soc_platform *platform = rtd->platform;
  547. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  548. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  549. struct snd_pcm_runtime *runtime = substream->runtime;
  550. snd_pcm_uframes_t offset = 0;
  551. snd_pcm_sframes_t delay = 0;
  552. if (platform->driver->ops && platform->driver->ops->pointer)
  553. offset = platform->driver->ops->pointer(substream);
  554. if (cpu_dai->driver->ops->delay)
  555. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  556. if (codec_dai->driver->ops->delay)
  557. delay += codec_dai->driver->ops->delay(substream, codec_dai);
  558. if (platform->driver->delay)
  559. delay += platform->driver->delay(substream, codec_dai);
  560. runtime->delay = delay;
  561. return offset;
  562. }
  563. /* connect a FE and BE */
  564. static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
  565. struct snd_soc_pcm_runtime *be, int stream)
  566. {
  567. struct snd_soc_dpcm *dpcm;
  568. /* only add new dpcms */
  569. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  570. if (dpcm->be == be && dpcm->fe == fe)
  571. return 0;
  572. }
  573. dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
  574. if (!dpcm)
  575. return -ENOMEM;
  576. dpcm->be = be;
  577. dpcm->fe = fe;
  578. be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
  579. dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
  580. list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
  581. list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
  582. dev_dbg(fe->dev, " connected new DPCM %s path %s %s %s\n",
  583. stream ? "capture" : "playback", fe->dai_link->name,
  584. stream ? "<-" : "->", be->dai_link->name);
  585. #ifdef CONFIG_DEBUG_FS
  586. dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
  587. fe->debugfs_dpcm_root, &dpcm->state);
  588. #endif
  589. return 1;
  590. }
  591. /* reparent a BE onto another FE */
  592. static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
  593. struct snd_soc_pcm_runtime *be, int stream)
  594. {
  595. struct snd_soc_dpcm *dpcm;
  596. struct snd_pcm_substream *fe_substream, *be_substream;
  597. /* reparent if BE is connected to other FEs */
  598. if (!be->dpcm[stream].users)
  599. return;
  600. be_substream = snd_soc_dpcm_get_substream(be, stream);
  601. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  602. if (dpcm->fe == fe)
  603. continue;
  604. dev_dbg(fe->dev, " reparent %s path %s %s %s\n",
  605. stream ? "capture" : "playback",
  606. dpcm->fe->dai_link->name,
  607. stream ? "<-" : "->", dpcm->be->dai_link->name);
  608. fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
  609. be_substream->runtime = fe_substream->runtime;
  610. break;
  611. }
  612. }
  613. /* disconnect a BE and FE */
  614. static void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
  615. {
  616. struct snd_soc_dpcm *dpcm, *d;
  617. list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
  618. dev_dbg(fe->dev, "BE %s disconnect check for %s\n",
  619. stream ? "capture" : "playback",
  620. dpcm->be->dai_link->name);
  621. if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
  622. continue;
  623. dev_dbg(fe->dev, " freed DSP %s path %s %s %s\n",
  624. stream ? "capture" : "playback", fe->dai_link->name,
  625. stream ? "<-" : "->", dpcm->be->dai_link->name);
  626. /* BEs still alive need new FE */
  627. dpcm_be_reparent(fe, dpcm->be, stream);
  628. #ifdef CONFIG_DEBUG_FS
  629. debugfs_remove(dpcm->debugfs_state);
  630. #endif
  631. list_del(&dpcm->list_be);
  632. list_del(&dpcm->list_fe);
  633. kfree(dpcm);
  634. }
  635. }
  636. /* get BE for DAI widget and stream */
  637. static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
  638. struct snd_soc_dapm_widget *widget, int stream)
  639. {
  640. struct snd_soc_pcm_runtime *be;
  641. int i;
  642. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  643. for (i = 0; i < card->num_links; i++) {
  644. be = &card->rtd[i];
  645. if (be->cpu_dai->playback_widget == widget ||
  646. be->codec_dai->playback_widget == widget)
  647. return be;
  648. }
  649. } else {
  650. for (i = 0; i < card->num_links; i++) {
  651. be = &card->rtd[i];
  652. if (be->cpu_dai->capture_widget == widget ||
  653. be->codec_dai->capture_widget == widget)
  654. return be;
  655. }
  656. }
  657. dev_err(card->dev, "can't get %s BE for %s\n",
  658. stream ? "capture" : "playback", widget->name);
  659. return NULL;
  660. }
  661. static inline struct snd_soc_dapm_widget *
  662. rtd_get_cpu_widget(struct snd_soc_pcm_runtime *rtd, int stream)
  663. {
  664. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  665. return rtd->cpu_dai->playback_widget;
  666. else
  667. return rtd->cpu_dai->capture_widget;
  668. }
  669. static inline struct snd_soc_dapm_widget *
  670. rtd_get_codec_widget(struct snd_soc_pcm_runtime *rtd, int stream)
  671. {
  672. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  673. return rtd->codec_dai->playback_widget;
  674. else
  675. return rtd->codec_dai->capture_widget;
  676. }
  677. static int widget_in_list(struct snd_soc_dapm_widget_list *list,
  678. struct snd_soc_dapm_widget *widget)
  679. {
  680. int i;
  681. for (i = 0; i < list->num_widgets; i++) {
  682. if (widget == list->widgets[i])
  683. return 1;
  684. }
  685. return 0;
  686. }
  687. static int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
  688. int stream, struct snd_soc_dapm_widget_list **list_)
  689. {
  690. struct snd_soc_dai *cpu_dai = fe->cpu_dai;
  691. struct snd_soc_dapm_widget_list *list;
  692. int paths;
  693. list = kzalloc(sizeof(struct snd_soc_dapm_widget_list) +
  694. sizeof(struct snd_soc_dapm_widget *), GFP_KERNEL);
  695. if (list == NULL)
  696. return -ENOMEM;
  697. /* get number of valid DAI paths and their widgets */
  698. paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, &list);
  699. dev_dbg(fe->dev, "found %d audio %s paths\n", paths,
  700. stream ? "capture" : "playback");
  701. *list_ = list;
  702. return paths;
  703. }
  704. static inline void dpcm_path_put(struct snd_soc_dapm_widget_list **list)
  705. {
  706. kfree(*list);
  707. }
  708. static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
  709. struct snd_soc_dapm_widget_list **list_)
  710. {
  711. struct snd_soc_dpcm *dpcm;
  712. struct snd_soc_dapm_widget_list *list = *list_;
  713. struct snd_soc_dapm_widget *widget;
  714. int prune = 0;
  715. /* Destroy any old FE <--> BE connections */
  716. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  717. /* is there a valid CPU DAI widget for this BE */
  718. widget = rtd_get_cpu_widget(dpcm->be, stream);
  719. /* prune the BE if it's no longer in our active list */
  720. if (widget && widget_in_list(list, widget))
  721. continue;
  722. /* is there a valid CODEC DAI widget for this BE */
  723. widget = rtd_get_codec_widget(dpcm->be, stream);
  724. /* prune the BE if it's no longer in our active list */
  725. if (widget && widget_in_list(list, widget))
  726. continue;
  727. dev_dbg(fe->dev, "pruning %s BE %s for %s\n",
  728. stream ? "capture" : "playback",
  729. dpcm->be->dai_link->name, fe->dai_link->name);
  730. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  731. dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  732. prune++;
  733. }
  734. dev_dbg(fe->dev, "found %d old BE paths for pruning\n", prune);
  735. return prune;
  736. }
  737. static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
  738. struct snd_soc_dapm_widget_list **list_)
  739. {
  740. struct snd_soc_card *card = fe->card;
  741. struct snd_soc_dapm_widget_list *list = *list_;
  742. struct snd_soc_pcm_runtime *be;
  743. int i, new = 0, err;
  744. /* Create any new FE <--> BE connections */
  745. for (i = 0; i < list->num_widgets; i++) {
  746. if (list->widgets[i]->id != snd_soc_dapm_dai)
  747. continue;
  748. /* is there a valid BE rtd for this widget */
  749. be = dpcm_get_be(card, list->widgets[i], stream);
  750. if (!be) {
  751. dev_err(fe->dev, "no BE found for %s\n",
  752. list->widgets[i]->name);
  753. continue;
  754. }
  755. /* make sure BE is a real BE */
  756. if (!be->dai_link->no_pcm)
  757. continue;
  758. /* don't connect if FE is not running */
  759. if (!fe->dpcm[stream].runtime)
  760. continue;
  761. /* newly connected FE and BE */
  762. err = dpcm_be_connect(fe, be, stream);
  763. if (err < 0) {
  764. dev_err(fe->dev, "can't connect %s\n",
  765. list->widgets[i]->name);
  766. break;
  767. } else if (err == 0) /* already connected */
  768. continue;
  769. /* new */
  770. be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  771. new++;
  772. }
  773. dev_dbg(fe->dev, "found %d new BE paths\n", new);
  774. return new;
  775. }
  776. /*
  777. * Find the corresponding BE DAIs that source or sink audio to this
  778. * FE substream.
  779. */
  780. static int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
  781. int stream, struct snd_soc_dapm_widget_list **list, int new)
  782. {
  783. if (new)
  784. return dpcm_add_paths(fe, stream, list);
  785. else
  786. return dpcm_prune_paths(fe, stream, list);
  787. }
  788. static void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
  789. {
  790. struct snd_soc_dpcm *dpcm;
  791. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  792. dpcm->be->dpcm[stream].runtime_update =
  793. SND_SOC_DPCM_UPDATE_NO;
  794. }
  795. static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
  796. int stream)
  797. {
  798. struct snd_soc_dpcm *dpcm;
  799. /* disable any enabled and non active backends */
  800. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  801. struct snd_soc_pcm_runtime *be = dpcm->be;
  802. struct snd_pcm_substream *be_substream =
  803. snd_soc_dpcm_get_substream(be, stream);
  804. if (be->dpcm[stream].users == 0)
  805. dev_err(be->dev, "no users %s at close - state %d\n",
  806. stream ? "capture" : "playback",
  807. be->dpcm[stream].state);
  808. if (--be->dpcm[stream].users != 0)
  809. continue;
  810. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  811. continue;
  812. soc_pcm_close(be_substream);
  813. be_substream->runtime = NULL;
  814. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  815. }
  816. }
  817. static int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
  818. {
  819. struct snd_soc_dpcm *dpcm;
  820. int err, count = 0;
  821. /* only startup BE DAIs that are either sinks or sources to this FE DAI */
  822. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  823. struct snd_soc_pcm_runtime *be = dpcm->be;
  824. struct snd_pcm_substream *be_substream =
  825. snd_soc_dpcm_get_substream(be, stream);
  826. /* is this op for this BE ? */
  827. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  828. continue;
  829. /* first time the dpcm is open ? */
  830. if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
  831. dev_err(be->dev, "too many users %s at open %d\n",
  832. stream ? "capture" : "playback",
  833. be->dpcm[stream].state);
  834. if (be->dpcm[stream].users++ != 0)
  835. continue;
  836. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
  837. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
  838. continue;
  839. dev_dbg(be->dev, "dpcm: open BE %s\n", be->dai_link->name);
  840. be_substream->runtime = be->dpcm[stream].runtime;
  841. err = soc_pcm_open(be_substream);
  842. if (err < 0) {
  843. dev_err(be->dev, "BE open failed %d\n", err);
  844. be->dpcm[stream].users--;
  845. if (be->dpcm[stream].users < 0)
  846. dev_err(be->dev, "no users %s at unwind %d\n",
  847. stream ? "capture" : "playback",
  848. be->dpcm[stream].state);
  849. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  850. goto unwind;
  851. }
  852. be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  853. count++;
  854. }
  855. return count;
  856. unwind:
  857. /* disable any enabled and non active backends */
  858. list_for_each_entry_continue_reverse(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 (!snd_soc_dpcm_be_can_update(fe, be, stream))
  863. continue;
  864. if (be->dpcm[stream].users == 0)
  865. dev_err(be->dev, "no users %s at close %d\n",
  866. stream ? "capture" : "playback",
  867. be->dpcm[stream].state);
  868. if (--be->dpcm[stream].users != 0)
  869. continue;
  870. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  871. continue;
  872. soc_pcm_close(be_substream);
  873. be_substream->runtime = NULL;
  874. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  875. }
  876. return err;
  877. }
  878. void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
  879. {
  880. struct snd_pcm_runtime *runtime = substream->runtime;
  881. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  882. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  883. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  884. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  885. runtime->hw.rate_min = cpu_dai_drv->playback.rate_min;
  886. runtime->hw.rate_max = cpu_dai_drv->playback.rate_max;
  887. runtime->hw.channels_min = cpu_dai_drv->playback.channels_min;
  888. runtime->hw.channels_max = cpu_dai_drv->playback.channels_max;
  889. runtime->hw.formats &= cpu_dai_drv->playback.formats;
  890. runtime->hw.rates = cpu_dai_drv->playback.rates;
  891. } else {
  892. runtime->hw.rate_min = cpu_dai_drv->capture.rate_min;
  893. runtime->hw.rate_max = cpu_dai_drv->capture.rate_max;
  894. runtime->hw.channels_min = cpu_dai_drv->capture.channels_min;
  895. runtime->hw.channels_max = cpu_dai_drv->capture.channels_max;
  896. runtime->hw.formats &= cpu_dai_drv->capture.formats;
  897. runtime->hw.rates = cpu_dai_drv->capture.rates;
  898. }
  899. }
  900. static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
  901. {
  902. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  903. struct snd_pcm_runtime *runtime = fe_substream->runtime;
  904. int stream = fe_substream->stream, ret = 0;
  905. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  906. ret = dpcm_be_dai_startup(fe, fe_substream->stream);
  907. if (ret < 0) {
  908. dev_err(fe->dev,"dpcm: failed to start some BEs %d\n", ret);
  909. goto be_err;
  910. }
  911. dev_dbg(fe->dev, "dpcm: open FE %s\n", fe->dai_link->name);
  912. /* start the DAI frontend */
  913. ret = soc_pcm_open(fe_substream);
  914. if (ret < 0) {
  915. dev_err(fe->dev,"dpcm: failed to start FE %d\n", ret);
  916. goto unwind;
  917. }
  918. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  919. dpcm_set_fe_runtime(fe_substream);
  920. snd_pcm_limit_hw_rates(runtime);
  921. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  922. return 0;
  923. unwind:
  924. dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
  925. be_err:
  926. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  927. return ret;
  928. }
  929. static int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  930. {
  931. struct snd_soc_dpcm *dpcm;
  932. /* only shutdown BEs that are either sinks or sources to this FE DAI */
  933. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  934. struct snd_soc_pcm_runtime *be = dpcm->be;
  935. struct snd_pcm_substream *be_substream =
  936. snd_soc_dpcm_get_substream(be, stream);
  937. /* is this op for this BE ? */
  938. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  939. continue;
  940. if (be->dpcm[stream].users == 0)
  941. dev_err(be->dev, "no users %s at close - state %d\n",
  942. stream ? "capture" : "playback",
  943. be->dpcm[stream].state);
  944. if (--be->dpcm[stream].users != 0)
  945. continue;
  946. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  947. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN))
  948. continue;
  949. dev_dbg(be->dev, "dpcm: close BE %s\n",
  950. dpcm->fe->dai_link->name);
  951. soc_pcm_close(be_substream);
  952. be_substream->runtime = NULL;
  953. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  954. }
  955. return 0;
  956. }
  957. static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
  958. {
  959. struct snd_soc_pcm_runtime *fe = substream->private_data;
  960. int stream = substream->stream;
  961. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  962. /* shutdown the BEs */
  963. dpcm_be_dai_shutdown(fe, substream->stream);
  964. dev_dbg(fe->dev, "dpcm: close FE %s\n", fe->dai_link->name);
  965. /* now shutdown the frontend */
  966. soc_pcm_close(substream);
  967. /* run the stream event for each BE */
  968. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  969. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  970. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  971. return 0;
  972. }
  973. static int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
  974. {
  975. struct snd_soc_dpcm *dpcm;
  976. /* only hw_params backends that are either sinks or sources
  977. * to this frontend DAI */
  978. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  979. struct snd_soc_pcm_runtime *be = dpcm->be;
  980. struct snd_pcm_substream *be_substream =
  981. snd_soc_dpcm_get_substream(be, stream);
  982. /* is this op for this BE ? */
  983. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  984. continue;
  985. /* only free hw when no longer used - check all FEs */
  986. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  987. continue;
  988. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  989. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  990. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  991. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  992. continue;
  993. dev_dbg(be->dev, "dpcm: hw_free BE %s\n",
  994. dpcm->fe->dai_link->name);
  995. soc_pcm_hw_free(be_substream);
  996. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  997. }
  998. return 0;
  999. }
  1000. int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
  1001. {
  1002. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1003. int err, stream = substream->stream;
  1004. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1005. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1006. dev_dbg(fe->dev, "dpcm: hw_free FE %s\n", fe->dai_link->name);
  1007. /* call hw_free on the frontend */
  1008. err = soc_pcm_hw_free(substream);
  1009. if (err < 0)
  1010. dev_err(fe->dev,"dpcm: hw_free FE %s failed\n",
  1011. fe->dai_link->name);
  1012. /* only hw_params backends that are either sinks or sources
  1013. * to this frontend DAI */
  1014. err = dpcm_be_dai_hw_free(fe, stream);
  1015. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1016. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1017. mutex_unlock(&fe->card->mutex);
  1018. return 0;
  1019. }
  1020. static int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
  1021. {
  1022. struct snd_soc_dpcm *dpcm;
  1023. int ret;
  1024. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1025. struct snd_soc_pcm_runtime *be = dpcm->be;
  1026. struct snd_pcm_substream *be_substream =
  1027. snd_soc_dpcm_get_substream(be, stream);
  1028. /* is this op for this BE ? */
  1029. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1030. continue;
  1031. /* only allow hw_params() if no connected FEs are running */
  1032. if (!snd_soc_dpcm_can_be_params(fe, be, stream))
  1033. continue;
  1034. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1035. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1036. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
  1037. continue;
  1038. dev_dbg(be->dev, "dpcm: hw_params BE %s\n",
  1039. dpcm->fe->dai_link->name);
  1040. /* copy params for each dpcm */
  1041. memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
  1042. sizeof(struct snd_pcm_hw_params));
  1043. /* perform any hw_params fixups */
  1044. if (be->dai_link->be_hw_params_fixup) {
  1045. ret = be->dai_link->be_hw_params_fixup(be,
  1046. &dpcm->hw_params);
  1047. if (ret < 0) {
  1048. dev_err(be->dev,
  1049. "dpcm: hw_params BE fixup failed %d\n",
  1050. ret);
  1051. goto unwind;
  1052. }
  1053. }
  1054. ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
  1055. if (ret < 0) {
  1056. dev_err(dpcm->be->dev,
  1057. "dpcm: hw_params BE failed %d\n", ret);
  1058. goto unwind;
  1059. }
  1060. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1061. }
  1062. return 0;
  1063. unwind:
  1064. /* disable any enabled and non active backends */
  1065. list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1066. struct snd_soc_pcm_runtime *be = dpcm->be;
  1067. struct snd_pcm_substream *be_substream =
  1068. snd_soc_dpcm_get_substream(be, stream);
  1069. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1070. continue;
  1071. /* only allow hw_free() if no connected FEs are running */
  1072. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1073. continue;
  1074. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1075. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1076. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1077. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1078. continue;
  1079. soc_pcm_hw_free(be_substream);
  1080. }
  1081. return ret;
  1082. }
  1083. int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
  1084. struct snd_pcm_hw_params *params)
  1085. {
  1086. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1087. int ret, stream = substream->stream;
  1088. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1089. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1090. memcpy(&fe->dpcm[substream->stream].hw_params, params,
  1091. sizeof(struct snd_pcm_hw_params));
  1092. ret = dpcm_be_dai_hw_params(fe, substream->stream);
  1093. if (ret < 0) {
  1094. dev_err(fe->dev,"dpcm: hw_params BE failed %d\n", ret);
  1095. goto out;
  1096. }
  1097. dev_dbg(fe->dev, "dpcm: hw_params FE %s rate %d chan %x fmt %d\n",
  1098. fe->dai_link->name, params_rate(params),
  1099. params_channels(params), params_format(params));
  1100. /* call hw_params on the frontend */
  1101. ret = soc_pcm_hw_params(substream, params);
  1102. if (ret < 0) {
  1103. dev_err(fe->dev,"dpcm: hw_params FE failed %d\n", ret);
  1104. dpcm_be_dai_hw_free(fe, stream);
  1105. } else
  1106. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1107. out:
  1108. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1109. mutex_unlock(&fe->card->mutex);
  1110. return ret;
  1111. }
  1112. static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
  1113. struct snd_pcm_substream *substream, int cmd)
  1114. {
  1115. int ret;
  1116. dev_dbg(dpcm->be->dev, "dpcm: trigger BE %s cmd %d\n",
  1117. dpcm->fe->dai_link->name, cmd);
  1118. ret = soc_pcm_trigger(substream, cmd);
  1119. if (ret < 0)
  1120. dev_err(dpcm->be->dev,"dpcm: trigger BE failed %d\n", ret);
  1121. return ret;
  1122. }
  1123. int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream, int cmd)
  1124. {
  1125. struct snd_soc_dpcm *dpcm;
  1126. int ret = 0;
  1127. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1128. struct snd_soc_pcm_runtime *be = dpcm->be;
  1129. struct snd_pcm_substream *be_substream =
  1130. snd_soc_dpcm_get_substream(be, stream);
  1131. /* is this op for this BE ? */
  1132. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1133. continue;
  1134. switch (cmd) {
  1135. case SNDRV_PCM_TRIGGER_START:
  1136. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1137. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1138. continue;
  1139. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1140. if (ret)
  1141. return ret;
  1142. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1143. break;
  1144. case SNDRV_PCM_TRIGGER_RESUME:
  1145. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
  1146. continue;
  1147. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1148. if (ret)
  1149. return ret;
  1150. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1151. break;
  1152. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1153. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
  1154. continue;
  1155. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1156. if (ret)
  1157. return ret;
  1158. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1159. break;
  1160. case SNDRV_PCM_TRIGGER_STOP:
  1161. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1162. continue;
  1163. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  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_STOP;
  1169. break;
  1170. case SNDRV_PCM_TRIGGER_SUSPEND:
  1171. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP)
  1172. continue;
  1173. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1174. continue;
  1175. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1176. if (ret)
  1177. return ret;
  1178. be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
  1179. break;
  1180. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1181. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1182. continue;
  1183. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1184. continue;
  1185. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1186. if (ret)
  1187. return ret;
  1188. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  1189. break;
  1190. }
  1191. }
  1192. return ret;
  1193. }
  1194. EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
  1195. int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
  1196. {
  1197. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1198. int stream = substream->stream, ret;
  1199. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1200. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1201. switch (trigger) {
  1202. case SND_SOC_DPCM_TRIGGER_PRE:
  1203. /* call trigger on the frontend before the backend. */
  1204. dev_dbg(fe->dev, "dpcm: pre trigger FE %s cmd %d\n",
  1205. fe->dai_link->name, cmd);
  1206. ret = soc_pcm_trigger(substream, cmd);
  1207. if (ret < 0) {
  1208. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1209. goto out;
  1210. }
  1211. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1212. break;
  1213. case SND_SOC_DPCM_TRIGGER_POST:
  1214. /* call trigger on the frontend after the backend. */
  1215. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1216. if (ret < 0) {
  1217. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1218. goto out;
  1219. }
  1220. dev_dbg(fe->dev, "dpcm: post trigger FE %s cmd %d\n",
  1221. fe->dai_link->name, cmd);
  1222. ret = soc_pcm_trigger(substream, cmd);
  1223. break;
  1224. default:
  1225. dev_err(fe->dev, "dpcm: invalid trigger cmd %d for %s\n", cmd,
  1226. fe->dai_link->name);
  1227. ret = -EINVAL;
  1228. goto out;
  1229. }
  1230. switch (cmd) {
  1231. case SNDRV_PCM_TRIGGER_START:
  1232. case SNDRV_PCM_TRIGGER_RESUME:
  1233. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1234. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1235. break;
  1236. case SNDRV_PCM_TRIGGER_STOP:
  1237. case SNDRV_PCM_TRIGGER_SUSPEND:
  1238. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1239. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  1240. break;
  1241. }
  1242. out:
  1243. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1244. return ret;
  1245. }
  1246. static int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
  1247. {
  1248. struct snd_soc_dpcm *dpcm;
  1249. int ret = 0;
  1250. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1251. struct snd_soc_pcm_runtime *be = dpcm->be;
  1252. struct snd_pcm_substream *be_substream =
  1253. snd_soc_dpcm_get_substream(be, stream);
  1254. /* is this op for this BE ? */
  1255. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1256. continue;
  1257. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1258. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1259. continue;
  1260. dev_dbg(be->dev, "dpcm: prepare BE %s\n",
  1261. dpcm->fe->dai_link->name);
  1262. ret = soc_pcm_prepare(be_substream);
  1263. if (ret < 0) {
  1264. dev_err(be->dev, "dpcm: backend prepare failed %d\n",
  1265. ret);
  1266. break;
  1267. }
  1268. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  1269. }
  1270. return ret;
  1271. }
  1272. int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
  1273. {
  1274. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1275. int stream = substream->stream, ret = 0;
  1276. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1277. dev_dbg(fe->dev, "dpcm: prepare FE %s\n", fe->dai_link->name);
  1278. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1279. /* there is no point preparing this FE if there are no BEs */
  1280. if (list_empty(&fe->dpcm[stream].be_clients)) {
  1281. dev_err(fe->dev, "dpcm: no backend DAIs enabled for %s\n",
  1282. fe->dai_link->name);
  1283. ret = -EINVAL;
  1284. goto out;
  1285. }
  1286. ret = dpcm_be_dai_prepare(fe, substream->stream);
  1287. if (ret < 0)
  1288. goto out;
  1289. /* call prepare on the frontend */
  1290. ret = soc_pcm_prepare(substream);
  1291. if (ret < 0) {
  1292. dev_err(fe->dev,"dpcm: prepare FE %s failed\n",
  1293. fe->dai_link->name);
  1294. goto out;
  1295. }
  1296. /* run the stream event for each BE */
  1297. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  1298. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  1299. out:
  1300. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1301. mutex_unlock(&fe->card->mutex);
  1302. return ret;
  1303. }
  1304. static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  1305. {
  1306. int err;
  1307. dev_dbg(fe->dev, "runtime %s close on FE %s\n",
  1308. stream ? "capture" : "playback", fe->dai_link->name);
  1309. err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
  1310. if (err < 0)
  1311. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", err);
  1312. err = dpcm_be_dai_hw_free(fe, stream);
  1313. if (err < 0)
  1314. dev_err(fe->dev,"dpcm: hw_free FE failed %d\n", err);
  1315. err = dpcm_be_dai_shutdown(fe, stream);
  1316. if (err < 0)
  1317. dev_err(fe->dev,"dpcm: shutdown FE failed %d\n", err);
  1318. /* run the stream event for each BE */
  1319. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
  1320. return 0;
  1321. }
  1322. static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
  1323. {
  1324. struct snd_soc_dpcm *dpcm;
  1325. int ret;
  1326. dev_dbg(fe->dev, "runtime %s open on FE %s\n",
  1327. stream ? "capture" : "playback", fe->dai_link->name);
  1328. /* Only start the BE if the FE is ready */
  1329. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
  1330. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
  1331. return -EINVAL;
  1332. /* startup must always be called for new BEs */
  1333. ret = dpcm_be_dai_startup(fe, stream);
  1334. if (ret < 0) {
  1335. goto disconnect;
  1336. return ret;
  1337. }
  1338. /* keep going if FE state is > open */
  1339. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
  1340. return 0;
  1341. ret = dpcm_be_dai_hw_params(fe, stream);
  1342. if (ret < 0) {
  1343. goto close;
  1344. return ret;
  1345. }
  1346. /* keep going if FE state is > hw_params */
  1347. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
  1348. return 0;
  1349. ret = dpcm_be_dai_prepare(fe, stream);
  1350. if (ret < 0) {
  1351. goto hw_free;
  1352. return ret;
  1353. }
  1354. /* run the stream event for each BE */
  1355. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
  1356. /* keep going if FE state is > prepare */
  1357. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
  1358. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
  1359. return 0;
  1360. dev_dbg(fe->dev, "dpcm: trigger FE %s cmd start\n",
  1361. fe->dai_link->name);
  1362. ret = dpcm_be_dai_trigger(fe, stream,
  1363. SNDRV_PCM_TRIGGER_START);
  1364. if (ret < 0) {
  1365. dev_err(fe->dev,"dpcm: trigger FE failed %d\n", ret);
  1366. goto hw_free;
  1367. }
  1368. return 0;
  1369. hw_free:
  1370. dpcm_be_dai_hw_free(fe, stream);
  1371. close:
  1372. dpcm_be_dai_shutdown(fe, stream);
  1373. disconnect:
  1374. /* disconnect any non started BEs */
  1375. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1376. struct snd_soc_pcm_runtime *be = dpcm->be;
  1377. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1378. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1379. }
  1380. return ret;
  1381. }
  1382. static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
  1383. {
  1384. int ret;
  1385. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1386. ret = dpcm_run_update_startup(fe, stream);
  1387. if (ret < 0)
  1388. dev_err(fe->dev, "failed to startup some BEs\n");
  1389. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1390. return ret;
  1391. }
  1392. static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
  1393. {
  1394. int ret;
  1395. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1396. ret = dpcm_run_update_shutdown(fe, stream);
  1397. if (ret < 0)
  1398. dev_err(fe->dev, "failed to shutdown some BEs\n");
  1399. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1400. return ret;
  1401. }
  1402. /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
  1403. * any DAI links.
  1404. */
  1405. int soc_dpcm_runtime_update(struct snd_soc_dapm_widget *widget)
  1406. {
  1407. struct snd_soc_card *card;
  1408. int i, old, new, paths;
  1409. if (widget->codec)
  1410. card = widget->codec->card;
  1411. else if (widget->platform)
  1412. card = widget->platform->card;
  1413. else
  1414. return -EINVAL;
  1415. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1416. for (i = 0; i < card->num_rtd; i++) {
  1417. struct snd_soc_dapm_widget_list *list;
  1418. struct snd_soc_pcm_runtime *fe = &card->rtd[i];
  1419. /* make sure link is FE */
  1420. if (!fe->dai_link->dynamic)
  1421. continue;
  1422. /* only check active links */
  1423. if (!fe->cpu_dai->active)
  1424. continue;
  1425. /* DAPM sync will call this to update DSP paths */
  1426. dev_dbg(fe->dev, "DPCM runtime update for FE %s\n",
  1427. fe->dai_link->name);
  1428. /* skip if FE doesn't have playback capability */
  1429. if (!fe->cpu_dai->driver->playback.channels_min)
  1430. goto capture;
  1431. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
  1432. if (paths < 0) {
  1433. dev_warn(fe->dev, "%s no valid %s path\n",
  1434. fe->dai_link->name, "playback");
  1435. mutex_unlock(&card->mutex);
  1436. return paths;
  1437. }
  1438. /* update any new playback paths */
  1439. new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
  1440. if (new) {
  1441. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1442. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1443. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1444. }
  1445. /* update any old playback paths */
  1446. old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
  1447. if (old) {
  1448. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1449. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1450. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1451. }
  1452. capture:
  1453. /* skip if FE doesn't have capture capability */
  1454. if (!fe->cpu_dai->driver->capture.channels_min)
  1455. continue;
  1456. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
  1457. if (paths < 0) {
  1458. dev_warn(fe->dev, "%s no valid %s path\n",
  1459. fe->dai_link->name, "capture");
  1460. mutex_unlock(&card->mutex);
  1461. return paths;
  1462. }
  1463. /* update any new capture paths */
  1464. new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
  1465. if (new) {
  1466. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  1467. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  1468. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  1469. }
  1470. /* update any old capture paths */
  1471. old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
  1472. if (old) {
  1473. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  1474. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  1475. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  1476. }
  1477. dpcm_path_put(&list);
  1478. }
  1479. mutex_unlock(&card->mutex);
  1480. return 0;
  1481. }
  1482. int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
  1483. {
  1484. struct snd_soc_dpcm *dpcm;
  1485. struct list_head *clients =
  1486. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
  1487. list_for_each_entry(dpcm, clients, list_be) {
  1488. struct snd_soc_pcm_runtime *be = dpcm->be;
  1489. struct snd_soc_dai *dai = be->codec_dai;
  1490. struct snd_soc_dai_driver *drv = dai->driver;
  1491. if (be->dai_link->ignore_suspend)
  1492. continue;
  1493. dev_dbg(be->dev, "BE digital mute %s\n", be->dai_link->name);
  1494. if (drv->ops->digital_mute && dai->playback_active)
  1495. drv->ops->digital_mute(dai, mute);
  1496. }
  1497. return 0;
  1498. }
  1499. int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
  1500. {
  1501. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1502. struct snd_soc_dpcm *dpcm;
  1503. struct snd_soc_dapm_widget_list *list;
  1504. int ret;
  1505. int stream = fe_substream->stream;
  1506. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1507. fe->dpcm[stream].runtime = fe_substream->runtime;
  1508. if (dpcm_path_get(fe, stream, &list) <= 0) {
  1509. dev_warn(fe->dev, "asoc: %s no valid %s route\n",
  1510. fe->dai_link->name, stream ? "capture" : "playback");
  1511. mutex_unlock(&fe->card->mutex);
  1512. return -EINVAL;
  1513. }
  1514. /* calculate valid and active FE <-> BE dpcms */
  1515. dpcm_process_paths(fe, stream, &list, 1);
  1516. ret = dpcm_fe_dai_startup(fe_substream);
  1517. if (ret < 0) {
  1518. /* clean up all links */
  1519. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  1520. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1521. dpcm_be_disconnect(fe, stream);
  1522. fe->dpcm[stream].runtime = NULL;
  1523. }
  1524. dpcm_clear_pending_state(fe, stream);
  1525. dpcm_path_put(&list);
  1526. mutex_unlock(&fe->card->mutex);
  1527. return ret;
  1528. }
  1529. int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
  1530. {
  1531. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1532. struct snd_soc_dpcm *dpcm;
  1533. int stream = fe_substream->stream, ret;
  1534. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1535. ret = dpcm_fe_dai_shutdown(fe_substream);
  1536. /* mark FE's links ready to prune */
  1537. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  1538. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1539. dpcm_be_disconnect(fe, stream);
  1540. fe->dpcm[stream].runtime = NULL;
  1541. mutex_unlock(&fe->card->mutex);
  1542. return ret;
  1543. }
  1544. /* create a new pcm */
  1545. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  1546. {
  1547. struct snd_soc_codec *codec = rtd->codec;
  1548. struct snd_soc_platform *platform = rtd->platform;
  1549. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1550. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1551. struct snd_pcm *pcm;
  1552. char new_name[64];
  1553. int ret = 0, playback = 0, capture = 0;
  1554. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
  1555. if (cpu_dai->driver->playback.channels_min)
  1556. playback = 1;
  1557. if (cpu_dai->driver->capture.channels_min)
  1558. capture = 1;
  1559. } else {
  1560. if (codec_dai->driver->playback.channels_min)
  1561. playback = 1;
  1562. if (codec_dai->driver->capture.channels_min)
  1563. capture = 1;
  1564. }
  1565. /* create the PCM */
  1566. if (rtd->dai_link->no_pcm) {
  1567. snprintf(new_name, sizeof(new_name), "(%s)",
  1568. rtd->dai_link->stream_name);
  1569. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  1570. playback, capture, &pcm);
  1571. } else {
  1572. if (rtd->dai_link->dynamic)
  1573. snprintf(new_name, sizeof(new_name), "%s (*)",
  1574. rtd->dai_link->stream_name);
  1575. else
  1576. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  1577. rtd->dai_link->stream_name, codec_dai->name, num);
  1578. ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
  1579. capture, &pcm);
  1580. }
  1581. if (ret < 0) {
  1582. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  1583. return ret;
  1584. }
  1585. dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num, new_name);
  1586. /* DAPM dai link stream work */
  1587. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  1588. rtd->pcm = pcm;
  1589. pcm->private_data = rtd;
  1590. if (rtd->dai_link->no_pcm) {
  1591. if (playback)
  1592. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  1593. if (capture)
  1594. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  1595. goto out;
  1596. }
  1597. /* ASoC PCM operations */
  1598. if (rtd->dai_link->dynamic) {
  1599. rtd->ops.open = dpcm_fe_dai_open;
  1600. rtd->ops.hw_params = dpcm_fe_dai_hw_params;
  1601. rtd->ops.prepare = dpcm_fe_dai_prepare;
  1602. rtd->ops.trigger = dpcm_fe_dai_trigger;
  1603. rtd->ops.hw_free = dpcm_fe_dai_hw_free;
  1604. rtd->ops.close = dpcm_fe_dai_close;
  1605. rtd->ops.pointer = soc_pcm_pointer;
  1606. } else {
  1607. rtd->ops.open = soc_pcm_open;
  1608. rtd->ops.hw_params = soc_pcm_hw_params;
  1609. rtd->ops.prepare = soc_pcm_prepare;
  1610. rtd->ops.trigger = soc_pcm_trigger;
  1611. rtd->ops.hw_free = soc_pcm_hw_free;
  1612. rtd->ops.close = soc_pcm_close;
  1613. rtd->ops.pointer = soc_pcm_pointer;
  1614. }
  1615. if (platform->driver->ops) {
  1616. rtd->ops.ack = platform->driver->ops->ack;
  1617. rtd->ops.copy = platform->driver->ops->copy;
  1618. rtd->ops.silence = platform->driver->ops->silence;
  1619. rtd->ops.page = platform->driver->ops->page;
  1620. rtd->ops.mmap = platform->driver->ops->mmap;
  1621. }
  1622. if (playback)
  1623. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
  1624. if (capture)
  1625. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
  1626. if (platform->driver->pcm_new) {
  1627. ret = platform->driver->pcm_new(rtd);
  1628. if (ret < 0) {
  1629. pr_err("asoc: platform pcm constructor failed\n");
  1630. return ret;
  1631. }
  1632. }
  1633. pcm->private_free = platform->driver->pcm_free;
  1634. out:
  1635. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  1636. cpu_dai->name);
  1637. return ret;
  1638. }
  1639. /* is the current PCM operation for this FE ? */
  1640. int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
  1641. {
  1642. if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
  1643. return 1;
  1644. return 0;
  1645. }
  1646. EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
  1647. /* is the current PCM operation for this BE ? */
  1648. int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
  1649. struct snd_soc_pcm_runtime *be, int stream)
  1650. {
  1651. if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
  1652. ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
  1653. be->dpcm[stream].runtime_update))
  1654. return 1;
  1655. return 0;
  1656. }
  1657. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
  1658. /* get the substream for this BE */
  1659. struct snd_pcm_substream *
  1660. snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
  1661. {
  1662. return be->pcm->streams[stream].substream;
  1663. }
  1664. EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
  1665. /* get the BE runtime state */
  1666. enum snd_soc_dpcm_state
  1667. snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
  1668. {
  1669. return be->dpcm[stream].state;
  1670. }
  1671. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
  1672. /* set the BE runtime state */
  1673. void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
  1674. int stream, enum snd_soc_dpcm_state state)
  1675. {
  1676. be->dpcm[stream].state = state;
  1677. }
  1678. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
  1679. /*
  1680. * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
  1681. * are not running, paused or suspended for the specified stream direction.
  1682. */
  1683. int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
  1684. struct snd_soc_pcm_runtime *be, int stream)
  1685. {
  1686. struct snd_soc_dpcm *dpcm;
  1687. int state;
  1688. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  1689. if (dpcm->fe == fe)
  1690. continue;
  1691. state = dpcm->fe->dpcm[stream].state;
  1692. if (state == SND_SOC_DPCM_STATE_START ||
  1693. state == SND_SOC_DPCM_STATE_PAUSED ||
  1694. state == SND_SOC_DPCM_STATE_SUSPEND)
  1695. return 0;
  1696. }
  1697. /* it's safe to free/stop this BE DAI */
  1698. return 1;
  1699. }
  1700. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
  1701. /*
  1702. * We can only change hw params a BE DAI if any of it's FE are not prepared,
  1703. * running, paused or suspended for the specified stream direction.
  1704. */
  1705. int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
  1706. struct snd_soc_pcm_runtime *be, int stream)
  1707. {
  1708. struct snd_soc_dpcm *dpcm;
  1709. int state;
  1710. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  1711. if (dpcm->fe == fe)
  1712. continue;
  1713. state = dpcm->fe->dpcm[stream].state;
  1714. if (state == SND_SOC_DPCM_STATE_START ||
  1715. state == SND_SOC_DPCM_STATE_PAUSED ||
  1716. state == SND_SOC_DPCM_STATE_SUSPEND ||
  1717. state == SND_SOC_DPCM_STATE_PREPARE)
  1718. return 0;
  1719. }
  1720. /* it's safe to change hw_params */
  1721. return 1;
  1722. }
  1723. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
  1724. #ifdef CONFIG_DEBUG_FS
  1725. static char *dpcm_state_string(enum snd_soc_dpcm_state state)
  1726. {
  1727. switch (state) {
  1728. case SND_SOC_DPCM_STATE_NEW:
  1729. return "new";
  1730. case SND_SOC_DPCM_STATE_OPEN:
  1731. return "open";
  1732. case SND_SOC_DPCM_STATE_HW_PARAMS:
  1733. return "hw_params";
  1734. case SND_SOC_DPCM_STATE_PREPARE:
  1735. return "prepare";
  1736. case SND_SOC_DPCM_STATE_START:
  1737. return "start";
  1738. case SND_SOC_DPCM_STATE_STOP:
  1739. return "stop";
  1740. case SND_SOC_DPCM_STATE_SUSPEND:
  1741. return "suspend";
  1742. case SND_SOC_DPCM_STATE_PAUSED:
  1743. return "paused";
  1744. case SND_SOC_DPCM_STATE_HW_FREE:
  1745. return "hw_free";
  1746. case SND_SOC_DPCM_STATE_CLOSE:
  1747. return "close";
  1748. }
  1749. return "unknown";
  1750. }
  1751. static int dpcm_state_open_file(struct inode *inode, struct file *file)
  1752. {
  1753. file->private_data = inode->i_private;
  1754. return 0;
  1755. }
  1756. static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
  1757. int stream, char *buf, size_t size)
  1758. {
  1759. struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
  1760. struct snd_soc_dpcm *dpcm;
  1761. ssize_t offset = 0;
  1762. /* FE state */
  1763. offset += snprintf(buf + offset, size - offset,
  1764. "[%s - %s]\n", fe->dai_link->name,
  1765. stream ? "Capture" : "Playback");
  1766. offset += snprintf(buf + offset, size - offset, "State: %s\n",
  1767. dpcm_state_string(fe->dpcm[stream].state));
  1768. if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1769. (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  1770. offset += snprintf(buf + offset, size - offset,
  1771. "Hardware Params: "
  1772. "Format = %s, Channels = %d, Rate = %d\n",
  1773. snd_pcm_format_name(params_format(params)),
  1774. params_channels(params),
  1775. params_rate(params));
  1776. /* BEs state */
  1777. offset += snprintf(buf + offset, size - offset, "Backends:\n");
  1778. if (list_empty(&fe->dpcm[stream].be_clients)) {
  1779. offset += snprintf(buf + offset, size - offset,
  1780. " No active DSP links\n");
  1781. goto out;
  1782. }
  1783. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1784. struct snd_soc_pcm_runtime *be = dpcm->be;
  1785. params = &dpcm->hw_params;
  1786. offset += snprintf(buf + offset, size - offset,
  1787. "- %s\n", be->dai_link->name);
  1788. offset += snprintf(buf + offset, size - offset,
  1789. " State: %s\n",
  1790. dpcm_state_string(be->dpcm[stream].state));
  1791. if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1792. (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  1793. offset += snprintf(buf + offset, size - offset,
  1794. " Hardware Params: "
  1795. "Format = %s, Channels = %d, Rate = %d\n",
  1796. snd_pcm_format_name(params_format(params)),
  1797. params_channels(params),
  1798. params_rate(params));
  1799. }
  1800. out:
  1801. return offset;
  1802. }
  1803. static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
  1804. size_t count, loff_t *ppos)
  1805. {
  1806. struct snd_soc_pcm_runtime *fe = file->private_data;
  1807. ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
  1808. char *buf;
  1809. buf = kmalloc(out_count, GFP_KERNEL);
  1810. if (!buf)
  1811. return -ENOMEM;
  1812. if (fe->cpu_dai->driver->playback.channels_min)
  1813. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
  1814. buf + offset, out_count - offset);
  1815. if (fe->cpu_dai->driver->capture.channels_min)
  1816. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
  1817. buf + offset, out_count - offset);
  1818. ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
  1819. kfree(buf);
  1820. return ret;
  1821. }
  1822. static const struct file_operations dpcm_state_fops = {
  1823. .open = dpcm_state_open_file,
  1824. .read = dpcm_state_read_file,
  1825. .llseek = default_llseek,
  1826. };
  1827. int soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
  1828. {
  1829. rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
  1830. rtd->card->debugfs_card_root);
  1831. if (!rtd->debugfs_dpcm_root) {
  1832. dev_dbg(rtd->dev,
  1833. "ASoC: Failed to create dpcm debugfs directory %s\n",
  1834. rtd->dai_link->name);
  1835. return -EINVAL;
  1836. }
  1837. rtd->debugfs_dpcm_state = debugfs_create_file("state", 0644,
  1838. rtd->debugfs_dpcm_root,
  1839. rtd, &dpcm_state_fops);
  1840. return 0;
  1841. }
  1842. #endif