soc-pcm.c 64 KB

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