soc-pcm.c 65 KB

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