soc-pcm.c 66 KB

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