soc-pcm.c 64 KB

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