soc-pcm.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  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_dai_stream_valid() - check if a DAI supports the given stream
  36. *
  37. * Returns true if the DAI supports the indicated stream type.
  38. */
  39. static bool snd_soc_dai_stream_valid(struct snd_soc_dai *dai, int stream)
  40. {
  41. struct snd_soc_pcm_stream *codec_stream;
  42. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  43. codec_stream = &dai->driver->playback;
  44. else
  45. codec_stream = &dai->driver->capture;
  46. /* If the codec specifies any rate at all, it supports the stream. */
  47. return codec_stream->rates;
  48. }
  49. /**
  50. * snd_soc_runtime_activate() - Increment active count for PCM runtime components
  51. * @rtd: ASoC PCM runtime that is activated
  52. * @stream: Direction of the PCM stream
  53. *
  54. * Increments the active count for all the DAIs and components attached to a PCM
  55. * runtime. Should typically be called when a stream is opened.
  56. *
  57. * Must be called with the rtd->pcm_mutex being held
  58. */
  59. void snd_soc_runtime_activate(struct snd_soc_pcm_runtime *rtd, int stream)
  60. {
  61. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  62. int i;
  63. lockdep_assert_held(&rtd->pcm_mutex);
  64. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  65. cpu_dai->playback_active++;
  66. for (i = 0; i < rtd->num_codecs; i++)
  67. rtd->codec_dais[i]->playback_active++;
  68. } else {
  69. cpu_dai->capture_active++;
  70. for (i = 0; i < rtd->num_codecs; i++)
  71. rtd->codec_dais[i]->capture_active++;
  72. }
  73. cpu_dai->active++;
  74. cpu_dai->component->active++;
  75. for (i = 0; i < rtd->num_codecs; i++) {
  76. rtd->codec_dais[i]->active++;
  77. rtd->codec_dais[i]->component->active++;
  78. }
  79. }
  80. /**
  81. * snd_soc_runtime_deactivate() - Decrement active count for PCM runtime components
  82. * @rtd: ASoC PCM runtime that is deactivated
  83. * @stream: Direction of the PCM stream
  84. *
  85. * Decrements the active count for all the DAIs and components attached to a PCM
  86. * runtime. Should typically be called when a stream is closed.
  87. *
  88. * Must be called with the rtd->pcm_mutex being held
  89. */
  90. void snd_soc_runtime_deactivate(struct snd_soc_pcm_runtime *rtd, int stream)
  91. {
  92. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  93. int i;
  94. lockdep_assert_held(&rtd->pcm_mutex);
  95. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  96. cpu_dai->playback_active--;
  97. for (i = 0; i < rtd->num_codecs; i++)
  98. rtd->codec_dais[i]->playback_active--;
  99. } else {
  100. cpu_dai->capture_active--;
  101. for (i = 0; i < rtd->num_codecs; i++)
  102. rtd->codec_dais[i]->capture_active--;
  103. }
  104. cpu_dai->active--;
  105. cpu_dai->component->active--;
  106. for (i = 0; i < rtd->num_codecs; i++) {
  107. rtd->codec_dais[i]->component->active--;
  108. rtd->codec_dais[i]->active--;
  109. }
  110. }
  111. /**
  112. * snd_soc_runtime_ignore_pmdown_time() - Check whether to ignore the power down delay
  113. * @rtd: The ASoC PCM runtime that should be checked.
  114. *
  115. * This function checks whether the power down delay should be ignored for a
  116. * specific PCM runtime. Returns true if the delay is 0, if it the DAI link has
  117. * been configured to ignore the delay, or if none of the components benefits
  118. * from having the delay.
  119. */
  120. bool snd_soc_runtime_ignore_pmdown_time(struct snd_soc_pcm_runtime *rtd)
  121. {
  122. int i;
  123. bool ignore = true;
  124. if (!rtd->pmdown_time || rtd->dai_link->ignore_pmdown_time)
  125. return true;
  126. for (i = 0; i < rtd->num_codecs; i++)
  127. ignore &= rtd->codec_dais[i]->component->ignore_pmdown_time;
  128. return rtd->cpu_dai->component->ignore_pmdown_time && ignore;
  129. }
  130. /**
  131. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  132. * @substream: the pcm substream
  133. * @hw: the hardware parameters
  134. *
  135. * Sets the substream runtime hardware parameters.
  136. */
  137. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  138. const struct snd_pcm_hardware *hw)
  139. {
  140. struct snd_pcm_runtime *runtime = substream->runtime;
  141. runtime->hw.info = hw->info;
  142. runtime->hw.formats = hw->formats;
  143. runtime->hw.period_bytes_min = hw->period_bytes_min;
  144. runtime->hw.period_bytes_max = hw->period_bytes_max;
  145. runtime->hw.periods_min = hw->periods_min;
  146. runtime->hw.periods_max = hw->periods_max;
  147. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  148. runtime->hw.fifo_size = hw->fifo_size;
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  152. /* DPCM stream event, send event to FE and all active BEs. */
  153. int dpcm_dapm_stream_event(struct snd_soc_pcm_runtime *fe, int dir,
  154. int event)
  155. {
  156. struct snd_soc_dpcm *dpcm;
  157. list_for_each_entry(dpcm, &fe->dpcm[dir].be_clients, list_be) {
  158. struct snd_soc_pcm_runtime *be = dpcm->be;
  159. dev_dbg(be->dev, "ASoC: BE %s event %d dir %d\n",
  160. be->dai_link->name, event, dir);
  161. if ((event == SND_SOC_DAPM_STREAM_STOP) &&
  162. (be->dpcm[dir].users >= 1))
  163. continue;
  164. snd_soc_dapm_stream_event(be, dir, event);
  165. }
  166. snd_soc_dapm_stream_event(fe, dir, event);
  167. return 0;
  168. }
  169. static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
  170. struct snd_soc_dai *soc_dai)
  171. {
  172. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  173. int ret;
  174. if (soc_dai->rate && (soc_dai->driver->symmetric_rates ||
  175. rtd->dai_link->symmetric_rates)) {
  176. dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %dHz rate\n",
  177. soc_dai->rate);
  178. ret = snd_pcm_hw_constraint_single(substream->runtime,
  179. SNDRV_PCM_HW_PARAM_RATE,
  180. soc_dai->rate);
  181. if (ret < 0) {
  182. dev_err(soc_dai->dev,
  183. "ASoC: Unable to apply rate constraint: %d\n",
  184. ret);
  185. return ret;
  186. }
  187. }
  188. if (soc_dai->channels && (soc_dai->driver->symmetric_channels ||
  189. rtd->dai_link->symmetric_channels)) {
  190. dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d channel(s)\n",
  191. soc_dai->channels);
  192. ret = snd_pcm_hw_constraint_single(substream->runtime,
  193. SNDRV_PCM_HW_PARAM_CHANNELS,
  194. soc_dai->channels);
  195. if (ret < 0) {
  196. dev_err(soc_dai->dev,
  197. "ASoC: Unable to apply channel symmetry constraint: %d\n",
  198. ret);
  199. return ret;
  200. }
  201. }
  202. if (soc_dai->sample_bits && (soc_dai->driver->symmetric_samplebits ||
  203. rtd->dai_link->symmetric_samplebits)) {
  204. dev_dbg(soc_dai->dev, "ASoC: Symmetry forces %d sample bits\n",
  205. soc_dai->sample_bits);
  206. ret = snd_pcm_hw_constraint_single(substream->runtime,
  207. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  208. soc_dai->sample_bits);
  209. if (ret < 0) {
  210. dev_err(soc_dai->dev,
  211. "ASoC: Unable to apply sample bits symmetry constraint: %d\n",
  212. ret);
  213. return ret;
  214. }
  215. }
  216. return 0;
  217. }
  218. static int soc_pcm_params_symmetry(struct snd_pcm_substream *substream,
  219. struct snd_pcm_hw_params *params)
  220. {
  221. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  222. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  223. unsigned int rate, channels, sample_bits, symmetry, i;
  224. rate = params_rate(params);
  225. channels = params_channels(params);
  226. sample_bits = snd_pcm_format_physical_width(params_format(params));
  227. /* reject unmatched parameters when applying symmetry */
  228. symmetry = cpu_dai->driver->symmetric_rates ||
  229. rtd->dai_link->symmetric_rates;
  230. for (i = 0; i < rtd->num_codecs; i++)
  231. symmetry |= rtd->codec_dais[i]->driver->symmetric_rates;
  232. if (symmetry && cpu_dai->rate && cpu_dai->rate != rate) {
  233. dev_err(rtd->dev, "ASoC: unmatched rate symmetry: %d - %d\n",
  234. cpu_dai->rate, rate);
  235. return -EINVAL;
  236. }
  237. symmetry = cpu_dai->driver->symmetric_channels ||
  238. rtd->dai_link->symmetric_channels;
  239. for (i = 0; i < rtd->num_codecs; i++)
  240. symmetry |= rtd->codec_dais[i]->driver->symmetric_channels;
  241. if (symmetry && cpu_dai->channels && cpu_dai->channels != channels) {
  242. dev_err(rtd->dev, "ASoC: unmatched channel symmetry: %d - %d\n",
  243. cpu_dai->channels, channels);
  244. return -EINVAL;
  245. }
  246. symmetry = cpu_dai->driver->symmetric_samplebits ||
  247. rtd->dai_link->symmetric_samplebits;
  248. for (i = 0; i < rtd->num_codecs; i++)
  249. symmetry |= rtd->codec_dais[i]->driver->symmetric_samplebits;
  250. if (symmetry && cpu_dai->sample_bits && cpu_dai->sample_bits != sample_bits) {
  251. dev_err(rtd->dev, "ASoC: unmatched sample bits symmetry: %d - %d\n",
  252. cpu_dai->sample_bits, sample_bits);
  253. return -EINVAL;
  254. }
  255. return 0;
  256. }
  257. static bool soc_pcm_has_symmetry(struct snd_pcm_substream *substream)
  258. {
  259. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  260. struct snd_soc_dai_driver *cpu_driver = rtd->cpu_dai->driver;
  261. struct snd_soc_dai_link *link = rtd->dai_link;
  262. unsigned int symmetry, i;
  263. symmetry = cpu_driver->symmetric_rates || link->symmetric_rates ||
  264. cpu_driver->symmetric_channels || link->symmetric_channels ||
  265. cpu_driver->symmetric_samplebits || link->symmetric_samplebits;
  266. for (i = 0; i < rtd->num_codecs; i++)
  267. symmetry = symmetry ||
  268. rtd->codec_dais[i]->driver->symmetric_rates ||
  269. rtd->codec_dais[i]->driver->symmetric_channels ||
  270. rtd->codec_dais[i]->driver->symmetric_samplebits;
  271. return symmetry;
  272. }
  273. static void soc_pcm_set_msb(struct snd_pcm_substream *substream, int bits)
  274. {
  275. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  276. int ret;
  277. if (!bits)
  278. return;
  279. ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 0, bits);
  280. if (ret != 0)
  281. dev_warn(rtd->dev, "ASoC: Failed to set MSB %d: %d\n",
  282. bits, ret);
  283. }
  284. static void soc_pcm_apply_msb(struct snd_pcm_substream *substream)
  285. {
  286. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  287. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  288. struct snd_soc_dai *codec_dai;
  289. int i;
  290. unsigned int bits = 0, cpu_bits;
  291. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  292. for (i = 0; i < rtd->num_codecs; i++) {
  293. codec_dai = rtd->codec_dais[i];
  294. if (codec_dai->driver->playback.sig_bits == 0) {
  295. bits = 0;
  296. break;
  297. }
  298. bits = max(codec_dai->driver->playback.sig_bits, bits);
  299. }
  300. cpu_bits = cpu_dai->driver->playback.sig_bits;
  301. } else {
  302. for (i = 0; i < rtd->num_codecs; i++) {
  303. codec_dai = rtd->codec_dais[i];
  304. if (codec_dai->driver->capture.sig_bits == 0) {
  305. bits = 0;
  306. break;
  307. }
  308. bits = max(codec_dai->driver->capture.sig_bits, bits);
  309. }
  310. cpu_bits = cpu_dai->driver->capture.sig_bits;
  311. }
  312. soc_pcm_set_msb(substream, bits);
  313. soc_pcm_set_msb(substream, cpu_bits);
  314. }
  315. static void soc_pcm_init_runtime_hw(struct snd_pcm_substream *substream)
  316. {
  317. struct snd_pcm_runtime *runtime = substream->runtime;
  318. struct snd_pcm_hardware *hw = &runtime->hw;
  319. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  320. struct snd_soc_dai_driver *cpu_dai_drv = rtd->cpu_dai->driver;
  321. struct snd_soc_dai_driver *codec_dai_drv;
  322. struct snd_soc_pcm_stream *codec_stream;
  323. struct snd_soc_pcm_stream *cpu_stream;
  324. unsigned int chan_min = 0, chan_max = UINT_MAX;
  325. unsigned int rate_min = 0, rate_max = UINT_MAX;
  326. unsigned int rates = UINT_MAX;
  327. u64 formats = ULLONG_MAX;
  328. int i;
  329. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  330. cpu_stream = &cpu_dai_drv->playback;
  331. else
  332. cpu_stream = &cpu_dai_drv->capture;
  333. /* first calculate min/max only for CODECs in the DAI link */
  334. for (i = 0; i < rtd->num_codecs; i++) {
  335. /*
  336. * Skip CODECs which don't support the current stream type.
  337. * Otherwise, since the rate, channel, and format values will
  338. * zero in that case, we would have no usable settings left,
  339. * causing the resulting setup to fail.
  340. * At least one CODEC should match, otherwise we should have
  341. * bailed out on a higher level, since there would be no
  342. * CODEC to support the transfer direction in that case.
  343. */
  344. if (!snd_soc_dai_stream_valid(rtd->codec_dais[i],
  345. substream->stream))
  346. continue;
  347. codec_dai_drv = rtd->codec_dais[i]->driver;
  348. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  349. codec_stream = &codec_dai_drv->playback;
  350. else
  351. codec_stream = &codec_dai_drv->capture;
  352. chan_min = max(chan_min, codec_stream->channels_min);
  353. chan_max = min(chan_max, codec_stream->channels_max);
  354. rate_min = max(rate_min, codec_stream->rate_min);
  355. rate_max = min_not_zero(rate_max, codec_stream->rate_max);
  356. formats &= codec_stream->formats;
  357. rates = snd_pcm_rate_mask_intersect(codec_stream->rates, rates);
  358. }
  359. /*
  360. * chan min/max cannot be enforced if there are multiple CODEC DAIs
  361. * connected to a single CPU DAI, use CPU DAI's directly and let
  362. * channel allocation be fixed up later
  363. */
  364. if (rtd->num_codecs > 1) {
  365. chan_min = cpu_stream->channels_min;
  366. chan_max = cpu_stream->channels_max;
  367. }
  368. hw->channels_min = max(chan_min, cpu_stream->channels_min);
  369. hw->channels_max = min(chan_max, cpu_stream->channels_max);
  370. if (hw->formats)
  371. hw->formats &= formats & cpu_stream->formats;
  372. else
  373. hw->formats = formats & cpu_stream->formats;
  374. hw->rates = snd_pcm_rate_mask_intersect(rates, cpu_stream->rates);
  375. snd_pcm_limit_hw_rates(runtime);
  376. hw->rate_min = max(hw->rate_min, cpu_stream->rate_min);
  377. hw->rate_min = max(hw->rate_min, rate_min);
  378. hw->rate_max = min_not_zero(hw->rate_max, cpu_stream->rate_max);
  379. hw->rate_max = min_not_zero(hw->rate_max, rate_max);
  380. }
  381. /*
  382. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  383. * then initialized and any private data can be allocated. This also calls
  384. * startup for the cpu DAI, platform, machine and codec DAI.
  385. */
  386. static int soc_pcm_open(struct snd_pcm_substream *substream)
  387. {
  388. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  389. struct snd_pcm_runtime *runtime = substream->runtime;
  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;
  393. const char *codec_dai_name = "multicodec";
  394. int i, ret = 0;
  395. pinctrl_pm_select_default_state(cpu_dai->dev);
  396. for (i = 0; i < rtd->num_codecs; i++)
  397. pinctrl_pm_select_default_state(rtd->codec_dais[i]->dev);
  398. pm_runtime_get_sync(cpu_dai->dev);
  399. for (i = 0; i < rtd->num_codecs; i++)
  400. pm_runtime_get_sync(rtd->codec_dais[i]->dev);
  401. pm_runtime_get_sync(platform->dev);
  402. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  403. /* startup the audio subsystem */
  404. if (cpu_dai->driver->ops && cpu_dai->driver->ops->startup) {
  405. ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
  406. if (ret < 0) {
  407. dev_err(cpu_dai->dev, "ASoC: can't open interface"
  408. " %s: %d\n", cpu_dai->name, ret);
  409. goto out;
  410. }
  411. }
  412. if (platform->driver->ops && platform->driver->ops->open) {
  413. ret = platform->driver->ops->open(substream);
  414. if (ret < 0) {
  415. dev_err(platform->dev, "ASoC: can't open platform"
  416. " %s: %d\n", platform->component.name, ret);
  417. goto platform_err;
  418. }
  419. }
  420. for (i = 0; i < rtd->num_codecs; i++) {
  421. codec_dai = rtd->codec_dais[i];
  422. if (codec_dai->driver->ops && codec_dai->driver->ops->startup) {
  423. ret = codec_dai->driver->ops->startup(substream,
  424. codec_dai);
  425. if (ret < 0) {
  426. dev_err(codec_dai->dev,
  427. "ASoC: can't open codec %s: %d\n",
  428. codec_dai->name, ret);
  429. goto codec_dai_err;
  430. }
  431. }
  432. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  433. codec_dai->tx_mask = 0;
  434. else
  435. codec_dai->rx_mask = 0;
  436. }
  437. if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
  438. ret = rtd->dai_link->ops->startup(substream);
  439. if (ret < 0) {
  440. pr_err("ASoC: %s startup failed: %d\n",
  441. rtd->dai_link->name, ret);
  442. goto machine_err;
  443. }
  444. }
  445. /* Dynamic PCM DAI links compat checks use dynamic capabilities */
  446. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm)
  447. goto dynamic;
  448. /* Check that the codec and cpu DAIs are compatible */
  449. soc_pcm_init_runtime_hw(substream);
  450. if (rtd->num_codecs == 1)
  451. codec_dai_name = rtd->codec_dai->name;
  452. if (soc_pcm_has_symmetry(substream))
  453. runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  454. ret = -EINVAL;
  455. if (!runtime->hw.rates) {
  456. printk(KERN_ERR "ASoC: %s <-> %s No matching rates\n",
  457. codec_dai_name, cpu_dai->name);
  458. goto config_err;
  459. }
  460. if (!runtime->hw.formats) {
  461. printk(KERN_ERR "ASoC: %s <-> %s No matching formats\n",
  462. codec_dai_name, cpu_dai->name);
  463. goto config_err;
  464. }
  465. if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
  466. runtime->hw.channels_min > runtime->hw.channels_max) {
  467. printk(KERN_ERR "ASoC: %s <-> %s No matching channels\n",
  468. codec_dai_name, cpu_dai->name);
  469. goto config_err;
  470. }
  471. soc_pcm_apply_msb(substream);
  472. /* Symmetry only applies if we've already got an active stream. */
  473. if (cpu_dai->active) {
  474. ret = soc_pcm_apply_symmetry(substream, cpu_dai);
  475. if (ret != 0)
  476. goto config_err;
  477. }
  478. for (i = 0; i < rtd->num_codecs; i++) {
  479. if (rtd->codec_dais[i]->active) {
  480. ret = soc_pcm_apply_symmetry(substream,
  481. rtd->codec_dais[i]);
  482. if (ret != 0)
  483. goto config_err;
  484. }
  485. }
  486. pr_debug("ASoC: %s <-> %s info:\n",
  487. codec_dai_name, cpu_dai->name);
  488. pr_debug("ASoC: rate mask 0x%x\n", runtime->hw.rates);
  489. pr_debug("ASoC: min ch %d max ch %d\n", runtime->hw.channels_min,
  490. runtime->hw.channels_max);
  491. pr_debug("ASoC: min rate %d max rate %d\n", runtime->hw.rate_min,
  492. runtime->hw.rate_max);
  493. dynamic:
  494. snd_soc_runtime_activate(rtd, substream->stream);
  495. mutex_unlock(&rtd->pcm_mutex);
  496. return 0;
  497. config_err:
  498. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  499. rtd->dai_link->ops->shutdown(substream);
  500. machine_err:
  501. i = rtd->num_codecs;
  502. codec_dai_err:
  503. while (--i >= 0) {
  504. codec_dai = rtd->codec_dais[i];
  505. if (codec_dai->driver->ops->shutdown)
  506. codec_dai->driver->ops->shutdown(substream, codec_dai);
  507. }
  508. if (platform->driver->ops && platform->driver->ops->close)
  509. platform->driver->ops->close(substream);
  510. platform_err:
  511. if (cpu_dai->driver->ops->shutdown)
  512. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  513. out:
  514. mutex_unlock(&rtd->pcm_mutex);
  515. pm_runtime_put(platform->dev);
  516. for (i = 0; i < rtd->num_codecs; i++)
  517. pm_runtime_put(rtd->codec_dais[i]->dev);
  518. pm_runtime_put(cpu_dai->dev);
  519. for (i = 0; i < rtd->num_codecs; i++) {
  520. if (!rtd->codec_dais[i]->active)
  521. pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
  522. }
  523. if (!cpu_dai->active)
  524. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  525. return ret;
  526. }
  527. /*
  528. * Power down the audio subsystem pmdown_time msecs after close is called.
  529. * This is to ensure there are no pops or clicks in between any music tracks
  530. * due to DAPM power cycling.
  531. */
  532. static void close_delayed_work(struct work_struct *work)
  533. {
  534. struct snd_soc_pcm_runtime *rtd =
  535. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  536. struct snd_soc_dai *codec_dai = rtd->codec_dais[0];
  537. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  538. dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
  539. codec_dai->driver->playback.stream_name,
  540. codec_dai->playback_active ? "active" : "inactive",
  541. rtd->pop_wait ? "yes" : "no");
  542. /* are we waiting on this codec DAI stream */
  543. if (rtd->pop_wait == 1) {
  544. rtd->pop_wait = 0;
  545. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  546. SND_SOC_DAPM_STREAM_STOP);
  547. }
  548. mutex_unlock(&rtd->pcm_mutex);
  549. }
  550. /*
  551. * Called by ALSA when a PCM substream is closed. Private data can be
  552. * freed here. The cpu DAI, codec DAI, machine and platform are also
  553. * shutdown.
  554. */
  555. static int soc_pcm_close(struct snd_pcm_substream *substream)
  556. {
  557. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  558. struct snd_soc_platform *platform = rtd->platform;
  559. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  560. struct snd_soc_dai *codec_dai;
  561. int i;
  562. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  563. snd_soc_runtime_deactivate(rtd, substream->stream);
  564. /* clear the corresponding DAIs rate when inactive */
  565. if (!cpu_dai->active)
  566. cpu_dai->rate = 0;
  567. for (i = 0; i < rtd->num_codecs; i++) {
  568. codec_dai = rtd->codec_dais[i];
  569. if (!codec_dai->active)
  570. codec_dai->rate = 0;
  571. }
  572. snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
  573. if (cpu_dai->driver->ops->shutdown)
  574. cpu_dai->driver->ops->shutdown(substream, cpu_dai);
  575. for (i = 0; i < rtd->num_codecs; i++) {
  576. codec_dai = rtd->codec_dais[i];
  577. if (codec_dai->driver->ops->shutdown)
  578. codec_dai->driver->ops->shutdown(substream, codec_dai);
  579. }
  580. if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
  581. rtd->dai_link->ops->shutdown(substream);
  582. if (platform->driver->ops && platform->driver->ops->close)
  583. platform->driver->ops->close(substream);
  584. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  585. if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
  586. /* powered down playback stream now */
  587. snd_soc_dapm_stream_event(rtd,
  588. SNDRV_PCM_STREAM_PLAYBACK,
  589. SND_SOC_DAPM_STREAM_STOP);
  590. } else {
  591. /* start delayed pop wq here for playback streams */
  592. rtd->pop_wait = 1;
  593. queue_delayed_work(system_power_efficient_wq,
  594. &rtd->delayed_work,
  595. msecs_to_jiffies(rtd->pmdown_time));
  596. }
  597. } else {
  598. /* capture streams can be powered down now */
  599. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
  600. SND_SOC_DAPM_STREAM_STOP);
  601. }
  602. mutex_unlock(&rtd->pcm_mutex);
  603. pm_runtime_put(platform->dev);
  604. for (i = 0; i < rtd->num_codecs; i++)
  605. pm_runtime_put(rtd->codec_dais[i]->dev);
  606. pm_runtime_put(cpu_dai->dev);
  607. for (i = 0; i < rtd->num_codecs; i++) {
  608. if (!rtd->codec_dais[i]->active)
  609. pinctrl_pm_select_sleep_state(rtd->codec_dais[i]->dev);
  610. }
  611. if (!cpu_dai->active)
  612. pinctrl_pm_select_sleep_state(cpu_dai->dev);
  613. return 0;
  614. }
  615. /*
  616. * Called by ALSA when the PCM substream is prepared, can set format, sample
  617. * rate, etc. This function is non atomic and can be called multiple times,
  618. * it can refer to the runtime info.
  619. */
  620. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  621. {
  622. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  623. struct snd_soc_platform *platform = rtd->platform;
  624. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  625. struct snd_soc_dai *codec_dai;
  626. int i, ret = 0;
  627. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  628. if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
  629. ret = rtd->dai_link->ops->prepare(substream);
  630. if (ret < 0) {
  631. dev_err(rtd->card->dev, "ASoC: machine prepare error:"
  632. " %d\n", ret);
  633. goto out;
  634. }
  635. }
  636. if (platform->driver->ops && platform->driver->ops->prepare) {
  637. ret = platform->driver->ops->prepare(substream);
  638. if (ret < 0) {
  639. dev_err(platform->dev, "ASoC: platform prepare error:"
  640. " %d\n", ret);
  641. goto out;
  642. }
  643. }
  644. for (i = 0; i < rtd->num_codecs; i++) {
  645. codec_dai = rtd->codec_dais[i];
  646. if (codec_dai->driver->ops && codec_dai->driver->ops->prepare) {
  647. ret = codec_dai->driver->ops->prepare(substream,
  648. codec_dai);
  649. if (ret < 0) {
  650. dev_err(codec_dai->dev,
  651. "ASoC: codec DAI prepare error: %d\n",
  652. ret);
  653. goto out;
  654. }
  655. }
  656. }
  657. if (cpu_dai->driver->ops && cpu_dai->driver->ops->prepare) {
  658. ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
  659. if (ret < 0) {
  660. dev_err(cpu_dai->dev,
  661. "ASoC: cpu DAI prepare error: %d\n", ret);
  662. goto out;
  663. }
  664. }
  665. /* cancel any delayed stream shutdown that is pending */
  666. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  667. rtd->pop_wait) {
  668. rtd->pop_wait = 0;
  669. cancel_delayed_work(&rtd->delayed_work);
  670. }
  671. snd_soc_dapm_stream_event(rtd, substream->stream,
  672. SND_SOC_DAPM_STREAM_START);
  673. for (i = 0; i < rtd->num_codecs; i++)
  674. snd_soc_dai_digital_mute(rtd->codec_dais[i], 0,
  675. substream->stream);
  676. snd_soc_dai_digital_mute(cpu_dai, 0, substream->stream);
  677. out:
  678. mutex_unlock(&rtd->pcm_mutex);
  679. return ret;
  680. }
  681. static void soc_pcm_codec_params_fixup(struct snd_pcm_hw_params *params,
  682. unsigned int mask)
  683. {
  684. struct snd_interval *interval;
  685. int channels = hweight_long(mask);
  686. interval = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  687. interval->min = channels;
  688. interval->max = channels;
  689. }
  690. int soc_dai_hw_params(struct snd_pcm_substream *substream,
  691. struct snd_pcm_hw_params *params,
  692. struct snd_soc_dai *dai)
  693. {
  694. int ret;
  695. if (dai->driver->ops && dai->driver->ops->hw_params) {
  696. ret = dai->driver->ops->hw_params(substream, params, dai);
  697. if (ret < 0) {
  698. dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
  699. dai->name, ret);
  700. return ret;
  701. }
  702. }
  703. return 0;
  704. }
  705. /*
  706. * Called by ALSA when the hardware params are set by application. This
  707. * function can also be called multiple times and can allocate buffers
  708. * (using snd_pcm_lib_* ). It's non-atomic.
  709. */
  710. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  711. struct snd_pcm_hw_params *params)
  712. {
  713. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  714. struct snd_soc_platform *platform = rtd->platform;
  715. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  716. int i, ret = 0;
  717. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  718. ret = soc_pcm_params_symmetry(substream, params);
  719. if (ret)
  720. goto out;
  721. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
  722. ret = rtd->dai_link->ops->hw_params(substream, params);
  723. if (ret < 0) {
  724. dev_err(rtd->card->dev, "ASoC: machine hw_params"
  725. " failed: %d\n", ret);
  726. goto out;
  727. }
  728. }
  729. for (i = 0; i < rtd->num_codecs; i++) {
  730. struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  731. struct snd_pcm_hw_params codec_params;
  732. /*
  733. * Skip CODECs which don't support the current stream type,
  734. * the idea being that if a CODEC is not used for the currently
  735. * set up transfer direction, it should not need to be
  736. * configured, especially since the configuration used might
  737. * not even be supported by that CODEC. There may be cases
  738. * however where a CODEC needs to be set up although it is
  739. * actually not being used for the transfer, e.g. if a
  740. * capture-only CODEC is acting as an LRCLK and/or BCLK master
  741. * for the DAI link including a playback-only CODEC.
  742. * If this becomes necessary, we will have to augment the
  743. * machine driver setup with information on how to act, so
  744. * we can do the right thing here.
  745. */
  746. if (!snd_soc_dai_stream_valid(codec_dai, substream->stream))
  747. continue;
  748. /* copy params for each codec */
  749. codec_params = *params;
  750. /* fixup params based on TDM slot masks */
  751. if (codec_dai->tx_mask)
  752. soc_pcm_codec_params_fixup(&codec_params,
  753. codec_dai->tx_mask);
  754. if (codec_dai->rx_mask)
  755. soc_pcm_codec_params_fixup(&codec_params,
  756. codec_dai->rx_mask);
  757. ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
  758. if(ret < 0)
  759. goto codec_err;
  760. codec_dai->rate = params_rate(&codec_params);
  761. codec_dai->channels = params_channels(&codec_params);
  762. codec_dai->sample_bits = snd_pcm_format_physical_width(
  763. params_format(&codec_params));
  764. }
  765. ret = soc_dai_hw_params(substream, params, cpu_dai);
  766. if (ret < 0)
  767. goto interface_err;
  768. if (platform->driver->ops && platform->driver->ops->hw_params) {
  769. ret = platform->driver->ops->hw_params(substream, params);
  770. if (ret < 0) {
  771. dev_err(platform->dev, "ASoC: %s hw params failed: %d\n",
  772. platform->component.name, ret);
  773. goto platform_err;
  774. }
  775. }
  776. /* store the parameters for each DAIs */
  777. cpu_dai->rate = params_rate(params);
  778. cpu_dai->channels = params_channels(params);
  779. cpu_dai->sample_bits =
  780. snd_pcm_format_physical_width(params_format(params));
  781. out:
  782. mutex_unlock(&rtd->pcm_mutex);
  783. return ret;
  784. platform_err:
  785. if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
  786. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  787. interface_err:
  788. i = rtd->num_codecs;
  789. codec_err:
  790. while (--i >= 0) {
  791. struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  792. if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
  793. codec_dai->driver->ops->hw_free(substream, codec_dai);
  794. codec_dai->rate = 0;
  795. }
  796. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  797. rtd->dai_link->ops->hw_free(substream);
  798. mutex_unlock(&rtd->pcm_mutex);
  799. return ret;
  800. }
  801. /*
  802. * Frees resources allocated by hw_params, can be called multiple times
  803. */
  804. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  805. {
  806. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  807. struct snd_soc_platform *platform = rtd->platform;
  808. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  809. struct snd_soc_dai *codec_dai;
  810. bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  811. int i;
  812. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  813. /* clear the corresponding DAIs parameters when going to be inactive */
  814. if (cpu_dai->active == 1) {
  815. cpu_dai->rate = 0;
  816. cpu_dai->channels = 0;
  817. cpu_dai->sample_bits = 0;
  818. }
  819. for (i = 0; i < rtd->num_codecs; i++) {
  820. codec_dai = rtd->codec_dais[i];
  821. if (codec_dai->active == 1) {
  822. codec_dai->rate = 0;
  823. codec_dai->channels = 0;
  824. codec_dai->sample_bits = 0;
  825. }
  826. }
  827. /* apply codec digital mute */
  828. for (i = 0; i < rtd->num_codecs; i++) {
  829. if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
  830. (!playback && rtd->codec_dais[i]->capture_active == 1))
  831. snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
  832. substream->stream);
  833. }
  834. /* free any machine hw params */
  835. if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
  836. rtd->dai_link->ops->hw_free(substream);
  837. /* free any DMA resources */
  838. if (platform->driver->ops && platform->driver->ops->hw_free)
  839. platform->driver->ops->hw_free(substream);
  840. /* now free hw params for the DAIs */
  841. for (i = 0; i < rtd->num_codecs; i++) {
  842. codec_dai = rtd->codec_dais[i];
  843. if (codec_dai->driver->ops && codec_dai->driver->ops->hw_free)
  844. codec_dai->driver->ops->hw_free(substream, codec_dai);
  845. }
  846. if (cpu_dai->driver->ops && cpu_dai->driver->ops->hw_free)
  847. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  848. mutex_unlock(&rtd->pcm_mutex);
  849. return 0;
  850. }
  851. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  852. {
  853. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  854. struct snd_soc_platform *platform = rtd->platform;
  855. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  856. struct snd_soc_dai *codec_dai;
  857. int i, ret;
  858. for (i = 0; i < rtd->num_codecs; i++) {
  859. codec_dai = rtd->codec_dais[i];
  860. if (codec_dai->driver->ops && codec_dai->driver->ops->trigger) {
  861. ret = codec_dai->driver->ops->trigger(substream,
  862. cmd, codec_dai);
  863. if (ret < 0)
  864. return ret;
  865. }
  866. }
  867. if (platform->driver->ops && platform->driver->ops->trigger) {
  868. ret = platform->driver->ops->trigger(substream, cmd);
  869. if (ret < 0)
  870. return ret;
  871. }
  872. if (cpu_dai->driver->ops && cpu_dai->driver->ops->trigger) {
  873. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  874. if (ret < 0)
  875. return ret;
  876. }
  877. if (rtd->dai_link->ops && rtd->dai_link->ops->trigger) {
  878. ret = rtd->dai_link->ops->trigger(substream, cmd);
  879. if (ret < 0)
  880. return ret;
  881. }
  882. return 0;
  883. }
  884. static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
  885. int cmd)
  886. {
  887. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  888. struct snd_soc_platform *platform = rtd->platform;
  889. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  890. struct snd_soc_dai *codec_dai;
  891. int i, ret;
  892. for (i = 0; i < rtd->num_codecs; i++) {
  893. codec_dai = rtd->codec_dais[i];
  894. if (codec_dai->driver->ops &&
  895. codec_dai->driver->ops->bespoke_trigger) {
  896. ret = codec_dai->driver->ops->bespoke_trigger(substream,
  897. cmd, codec_dai);
  898. if (ret < 0)
  899. return ret;
  900. }
  901. }
  902. if (platform->driver->bespoke_trigger) {
  903. ret = platform->driver->bespoke_trigger(substream, cmd);
  904. if (ret < 0)
  905. return ret;
  906. }
  907. if (cpu_dai->driver->ops && cpu_dai->driver->ops->bespoke_trigger) {
  908. ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
  909. if (ret < 0)
  910. return ret;
  911. }
  912. return 0;
  913. }
  914. /*
  915. * soc level wrapper for pointer callback
  916. * If cpu_dai, codec_dai, platform driver has the delay callback, than
  917. * the runtime->delay will be updated accordingly.
  918. */
  919. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  920. {
  921. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  922. struct snd_soc_platform *platform = rtd->platform;
  923. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  924. struct snd_soc_dai *codec_dai;
  925. struct snd_pcm_runtime *runtime = substream->runtime;
  926. snd_pcm_uframes_t offset = 0;
  927. snd_pcm_sframes_t delay = 0;
  928. snd_pcm_sframes_t codec_delay = 0;
  929. int i;
  930. if (platform->driver->ops && platform->driver->ops->pointer)
  931. offset = platform->driver->ops->pointer(substream);
  932. if (cpu_dai->driver->ops && cpu_dai->driver->ops->delay)
  933. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  934. for (i = 0; i < rtd->num_codecs; i++) {
  935. codec_dai = rtd->codec_dais[i];
  936. if (codec_dai->driver->ops && codec_dai->driver->ops->delay)
  937. codec_delay = max(codec_delay,
  938. codec_dai->driver->ops->delay(substream,
  939. codec_dai));
  940. }
  941. delay += codec_delay;
  942. /*
  943. * None of the existing platform drivers implement delay(), so
  944. * for now the codec_dai of first multicodec entry is used
  945. */
  946. if (platform->driver->delay)
  947. delay += platform->driver->delay(substream, rtd->codec_dais[0]);
  948. runtime->delay = delay;
  949. return offset;
  950. }
  951. /* connect a FE and BE */
  952. static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
  953. struct snd_soc_pcm_runtime *be, int stream)
  954. {
  955. struct snd_soc_dpcm *dpcm;
  956. /* only add new dpcms */
  957. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  958. if (dpcm->be == be && dpcm->fe == fe)
  959. return 0;
  960. }
  961. dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
  962. if (!dpcm)
  963. return -ENOMEM;
  964. dpcm->be = be;
  965. dpcm->fe = fe;
  966. be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
  967. dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
  968. list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
  969. list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
  970. dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
  971. stream ? "capture" : "playback", fe->dai_link->name,
  972. stream ? "<-" : "->", be->dai_link->name);
  973. #ifdef CONFIG_DEBUG_FS
  974. if (fe->debugfs_dpcm_root)
  975. dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
  976. fe->debugfs_dpcm_root, &dpcm->state);
  977. #endif
  978. return 1;
  979. }
  980. /* reparent a BE onto another FE */
  981. static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
  982. struct snd_soc_pcm_runtime *be, int stream)
  983. {
  984. struct snd_soc_dpcm *dpcm;
  985. struct snd_pcm_substream *fe_substream, *be_substream;
  986. /* reparent if BE is connected to other FEs */
  987. if (!be->dpcm[stream].users)
  988. return;
  989. be_substream = snd_soc_dpcm_get_substream(be, stream);
  990. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  991. if (dpcm->fe == fe)
  992. continue;
  993. dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
  994. stream ? "capture" : "playback",
  995. dpcm->fe->dai_link->name,
  996. stream ? "<-" : "->", dpcm->be->dai_link->name);
  997. fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
  998. be_substream->runtime = fe_substream->runtime;
  999. break;
  1000. }
  1001. }
  1002. /* disconnect a BE and FE */
  1003. void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
  1004. {
  1005. struct snd_soc_dpcm *dpcm, *d;
  1006. list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
  1007. dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
  1008. stream ? "capture" : "playback",
  1009. dpcm->be->dai_link->name);
  1010. if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
  1011. continue;
  1012. dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
  1013. stream ? "capture" : "playback", fe->dai_link->name,
  1014. stream ? "<-" : "->", dpcm->be->dai_link->name);
  1015. /* BEs still alive need new FE */
  1016. dpcm_be_reparent(fe, dpcm->be, stream);
  1017. #ifdef CONFIG_DEBUG_FS
  1018. debugfs_remove(dpcm->debugfs_state);
  1019. #endif
  1020. list_del(&dpcm->list_be);
  1021. list_del(&dpcm->list_fe);
  1022. kfree(dpcm);
  1023. }
  1024. }
  1025. /* get BE for DAI widget and stream */
  1026. static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
  1027. struct snd_soc_dapm_widget *widget, int stream)
  1028. {
  1029. struct snd_soc_pcm_runtime *be;
  1030. int i, j;
  1031. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1032. for (i = 0; i < card->num_links; i++) {
  1033. be = &card->rtd[i];
  1034. if (!be->dai_link->no_pcm)
  1035. continue;
  1036. if (be->cpu_dai->playback_widget == widget)
  1037. return be;
  1038. for (j = 0; j < be->num_codecs; j++) {
  1039. struct snd_soc_dai *dai = be->codec_dais[j];
  1040. if (dai->playback_widget == widget)
  1041. return be;
  1042. }
  1043. }
  1044. } else {
  1045. for (i = 0; i < card->num_links; i++) {
  1046. be = &card->rtd[i];
  1047. if (!be->dai_link->no_pcm)
  1048. continue;
  1049. if (be->cpu_dai->capture_widget == widget)
  1050. return be;
  1051. for (j = 0; j < be->num_codecs; j++) {
  1052. struct snd_soc_dai *dai = be->codec_dais[j];
  1053. if (dai->capture_widget == widget)
  1054. return be;
  1055. }
  1056. }
  1057. }
  1058. dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
  1059. stream ? "capture" : "playback", widget->name);
  1060. return NULL;
  1061. }
  1062. static inline struct snd_soc_dapm_widget *
  1063. dai_get_widget(struct snd_soc_dai *dai, int stream)
  1064. {
  1065. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1066. return dai->playback_widget;
  1067. else
  1068. return dai->capture_widget;
  1069. }
  1070. static int widget_in_list(struct snd_soc_dapm_widget_list *list,
  1071. struct snd_soc_dapm_widget *widget)
  1072. {
  1073. int i;
  1074. for (i = 0; i < list->num_widgets; i++) {
  1075. if (widget == list->widgets[i])
  1076. return 1;
  1077. }
  1078. return 0;
  1079. }
  1080. int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
  1081. int stream, struct snd_soc_dapm_widget_list **list)
  1082. {
  1083. struct snd_soc_dai *cpu_dai = fe->cpu_dai;
  1084. int paths;
  1085. /* get number of valid DAI paths and their widgets */
  1086. paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list);
  1087. dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
  1088. stream ? "capture" : "playback");
  1089. return paths;
  1090. }
  1091. static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
  1092. struct snd_soc_dapm_widget_list **list_)
  1093. {
  1094. struct snd_soc_dpcm *dpcm;
  1095. struct snd_soc_dapm_widget_list *list = *list_;
  1096. struct snd_soc_dapm_widget *widget;
  1097. int prune = 0;
  1098. /* Destroy any old FE <--> BE connections */
  1099. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1100. unsigned int i;
  1101. /* is there a valid CPU DAI widget for this BE */
  1102. widget = dai_get_widget(dpcm->be->cpu_dai, stream);
  1103. /* prune the BE if it's no longer in our active list */
  1104. if (widget && widget_in_list(list, widget))
  1105. continue;
  1106. /* is there a valid CODEC DAI widget for this BE */
  1107. for (i = 0; i < dpcm->be->num_codecs; i++) {
  1108. struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
  1109. widget = dai_get_widget(dai, stream);
  1110. /* prune the BE if it's no longer in our active list */
  1111. if (widget && widget_in_list(list, widget))
  1112. continue;
  1113. }
  1114. dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
  1115. stream ? "capture" : "playback",
  1116. dpcm->be->dai_link->name, fe->dai_link->name);
  1117. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1118. dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1119. prune++;
  1120. }
  1121. dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
  1122. return prune;
  1123. }
  1124. static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
  1125. struct snd_soc_dapm_widget_list **list_)
  1126. {
  1127. struct snd_soc_card *card = fe->card;
  1128. struct snd_soc_dapm_widget_list *list = *list_;
  1129. struct snd_soc_pcm_runtime *be;
  1130. int i, new = 0, err;
  1131. /* Create any new FE <--> BE connections */
  1132. for (i = 0; i < list->num_widgets; i++) {
  1133. switch (list->widgets[i]->id) {
  1134. case snd_soc_dapm_dai_in:
  1135. if (stream != SNDRV_PCM_STREAM_PLAYBACK)
  1136. continue;
  1137. break;
  1138. case snd_soc_dapm_dai_out:
  1139. if (stream != SNDRV_PCM_STREAM_CAPTURE)
  1140. continue;
  1141. break;
  1142. default:
  1143. continue;
  1144. }
  1145. /* is there a valid BE rtd for this widget */
  1146. be = dpcm_get_be(card, list->widgets[i], stream);
  1147. if (!be) {
  1148. dev_err(fe->dev, "ASoC: no BE found for %s\n",
  1149. list->widgets[i]->name);
  1150. continue;
  1151. }
  1152. /* make sure BE is a real BE */
  1153. if (!be->dai_link->no_pcm)
  1154. continue;
  1155. /* don't connect if FE is not running */
  1156. if (!fe->dpcm[stream].runtime && !fe->fe_compr)
  1157. continue;
  1158. /* newly connected FE and BE */
  1159. err = dpcm_be_connect(fe, be, stream);
  1160. if (err < 0) {
  1161. dev_err(fe->dev, "ASoC: can't connect %s\n",
  1162. list->widgets[i]->name);
  1163. break;
  1164. } else if (err == 0) /* already connected */
  1165. continue;
  1166. /* new */
  1167. be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1168. new++;
  1169. }
  1170. dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
  1171. return new;
  1172. }
  1173. /*
  1174. * Find the corresponding BE DAIs that source or sink audio to this
  1175. * FE substream.
  1176. */
  1177. int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
  1178. int stream, struct snd_soc_dapm_widget_list **list, int new)
  1179. {
  1180. if (new)
  1181. return dpcm_add_paths(fe, stream, list);
  1182. else
  1183. return dpcm_prune_paths(fe, stream, list);
  1184. }
  1185. void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
  1186. {
  1187. struct snd_soc_dpcm *dpcm;
  1188. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  1189. dpcm->be->dpcm[stream].runtime_update =
  1190. SND_SOC_DPCM_UPDATE_NO;
  1191. }
  1192. static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
  1193. int stream)
  1194. {
  1195. struct snd_soc_dpcm *dpcm;
  1196. /* disable any enabled and non active backends */
  1197. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1198. struct snd_soc_pcm_runtime *be = dpcm->be;
  1199. struct snd_pcm_substream *be_substream =
  1200. snd_soc_dpcm_get_substream(be, stream);
  1201. if (be->dpcm[stream].users == 0)
  1202. dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
  1203. stream ? "capture" : "playback",
  1204. be->dpcm[stream].state);
  1205. if (--be->dpcm[stream].users != 0)
  1206. continue;
  1207. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  1208. continue;
  1209. soc_pcm_close(be_substream);
  1210. be_substream->runtime = NULL;
  1211. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1212. }
  1213. }
  1214. int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
  1215. {
  1216. struct snd_soc_dpcm *dpcm;
  1217. int err, count = 0;
  1218. /* only startup BE DAIs that are either sinks or sources to this FE DAI */
  1219. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1220. struct snd_soc_pcm_runtime *be = dpcm->be;
  1221. struct snd_pcm_substream *be_substream =
  1222. snd_soc_dpcm_get_substream(be, stream);
  1223. if (!be_substream) {
  1224. dev_err(be->dev, "ASoC: no backend %s stream\n",
  1225. stream ? "capture" : "playback");
  1226. continue;
  1227. }
  1228. /* is this op for this BE ? */
  1229. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1230. continue;
  1231. /* first time the dpcm is open ? */
  1232. if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
  1233. dev_err(be->dev, "ASoC: too many users %s at open %d\n",
  1234. stream ? "capture" : "playback",
  1235. be->dpcm[stream].state);
  1236. if (be->dpcm[stream].users++ != 0)
  1237. continue;
  1238. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
  1239. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
  1240. continue;
  1241. dev_dbg(be->dev, "ASoC: open %s BE %s\n",
  1242. stream ? "capture" : "playback", be->dai_link->name);
  1243. be_substream->runtime = be->dpcm[stream].runtime;
  1244. err = soc_pcm_open(be_substream);
  1245. if (err < 0) {
  1246. dev_err(be->dev, "ASoC: BE open failed %d\n", err);
  1247. be->dpcm[stream].users--;
  1248. if (be->dpcm[stream].users < 0)
  1249. dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
  1250. stream ? "capture" : "playback",
  1251. be->dpcm[stream].state);
  1252. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1253. goto unwind;
  1254. }
  1255. be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  1256. count++;
  1257. }
  1258. return count;
  1259. unwind:
  1260. /* disable any enabled and non active backends */
  1261. list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1262. struct snd_soc_pcm_runtime *be = dpcm->be;
  1263. struct snd_pcm_substream *be_substream =
  1264. snd_soc_dpcm_get_substream(be, stream);
  1265. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1266. continue;
  1267. if (be->dpcm[stream].users == 0)
  1268. dev_err(be->dev, "ASoC: no users %s at close %d\n",
  1269. stream ? "capture" : "playback",
  1270. be->dpcm[stream].state);
  1271. if (--be->dpcm[stream].users != 0)
  1272. continue;
  1273. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  1274. continue;
  1275. soc_pcm_close(be_substream);
  1276. be_substream->runtime = NULL;
  1277. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1278. }
  1279. return err;
  1280. }
  1281. static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
  1282. struct snd_soc_pcm_stream *stream,
  1283. u64 formats)
  1284. {
  1285. runtime->hw.rate_min = stream->rate_min;
  1286. runtime->hw.rate_max = stream->rate_max;
  1287. runtime->hw.channels_min = stream->channels_min;
  1288. runtime->hw.channels_max = stream->channels_max;
  1289. if (runtime->hw.formats)
  1290. runtime->hw.formats &= formats & stream->formats;
  1291. else
  1292. runtime->hw.formats = formats & stream->formats;
  1293. runtime->hw.rates = stream->rates;
  1294. }
  1295. static u64 dpcm_runtime_base_format(struct snd_pcm_substream *substream)
  1296. {
  1297. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1298. struct snd_soc_dpcm *dpcm;
  1299. u64 formats = ULLONG_MAX;
  1300. int stream = substream->stream;
  1301. if (!fe->dai_link->dpcm_merged_format)
  1302. return formats;
  1303. /*
  1304. * It returns merged BE codec format
  1305. * if FE want to use it (= dpcm_merged_format)
  1306. */
  1307. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1308. struct snd_soc_pcm_runtime *be = dpcm->be;
  1309. struct snd_soc_dai_driver *codec_dai_drv;
  1310. struct snd_soc_pcm_stream *codec_stream;
  1311. int i;
  1312. for (i = 0; i < be->num_codecs; i++) {
  1313. /*
  1314. * Skip CODECs which don't support the current stream
  1315. * type. See soc_pcm_init_runtime_hw() for more details
  1316. */
  1317. if (!snd_soc_dai_stream_valid(be->codec_dais[i],
  1318. stream))
  1319. continue;
  1320. codec_dai_drv = be->codec_dais[i]->driver;
  1321. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1322. codec_stream = &codec_dai_drv->playback;
  1323. else
  1324. codec_stream = &codec_dai_drv->capture;
  1325. formats &= codec_stream->formats;
  1326. }
  1327. }
  1328. return formats;
  1329. }
  1330. static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
  1331. {
  1332. struct snd_pcm_runtime *runtime = substream->runtime;
  1333. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1334. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1335. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  1336. u64 format = dpcm_runtime_base_format(substream);
  1337. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1338. dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback, format);
  1339. else
  1340. dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture, format);
  1341. }
  1342. static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
  1343. /* Set FE's runtime_update state; the state is protected via PCM stream lock
  1344. * for avoiding the race with trigger callback.
  1345. * If the state is unset and a trigger is pending while the previous operation,
  1346. * process the pending trigger action here.
  1347. */
  1348. static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
  1349. int stream, enum snd_soc_dpcm_update state)
  1350. {
  1351. struct snd_pcm_substream *substream =
  1352. snd_soc_dpcm_get_substream(fe, stream);
  1353. snd_pcm_stream_lock_irq(substream);
  1354. if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
  1355. dpcm_fe_dai_do_trigger(substream,
  1356. fe->dpcm[stream].trigger_pending - 1);
  1357. fe->dpcm[stream].trigger_pending = 0;
  1358. }
  1359. fe->dpcm[stream].runtime_update = state;
  1360. snd_pcm_stream_unlock_irq(substream);
  1361. }
  1362. static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
  1363. {
  1364. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1365. struct snd_pcm_runtime *runtime = fe_substream->runtime;
  1366. int stream = fe_substream->stream, ret = 0;
  1367. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1368. ret = dpcm_be_dai_startup(fe, fe_substream->stream);
  1369. if (ret < 0) {
  1370. dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
  1371. goto be_err;
  1372. }
  1373. dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
  1374. /* start the DAI frontend */
  1375. ret = soc_pcm_open(fe_substream);
  1376. if (ret < 0) {
  1377. dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
  1378. goto unwind;
  1379. }
  1380. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  1381. dpcm_set_fe_runtime(fe_substream);
  1382. snd_pcm_limit_hw_rates(runtime);
  1383. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1384. return 0;
  1385. unwind:
  1386. dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
  1387. be_err:
  1388. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1389. return ret;
  1390. }
  1391. int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  1392. {
  1393. struct snd_soc_dpcm *dpcm;
  1394. /* only shutdown BEs that are either sinks or sources to this FE DAI */
  1395. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1396. struct snd_soc_pcm_runtime *be = dpcm->be;
  1397. struct snd_pcm_substream *be_substream =
  1398. snd_soc_dpcm_get_substream(be, stream);
  1399. /* is this op for this BE ? */
  1400. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1401. continue;
  1402. if (be->dpcm[stream].users == 0)
  1403. dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
  1404. stream ? "capture" : "playback",
  1405. be->dpcm[stream].state);
  1406. if (--be->dpcm[stream].users != 0)
  1407. continue;
  1408. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1409. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
  1410. soc_pcm_hw_free(be_substream);
  1411. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1412. }
  1413. dev_dbg(be->dev, "ASoC: close BE %s\n",
  1414. dpcm->fe->dai_link->name);
  1415. soc_pcm_close(be_substream);
  1416. be_substream->runtime = NULL;
  1417. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1418. }
  1419. return 0;
  1420. }
  1421. static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
  1422. {
  1423. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1424. int stream = substream->stream;
  1425. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1426. /* shutdown the BEs */
  1427. dpcm_be_dai_shutdown(fe, substream->stream);
  1428. dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
  1429. /* now shutdown the frontend */
  1430. soc_pcm_close(substream);
  1431. /* run the stream event for each BE */
  1432. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  1433. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1434. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1435. return 0;
  1436. }
  1437. int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
  1438. {
  1439. struct snd_soc_dpcm *dpcm;
  1440. /* only hw_params backends that are either sinks or sources
  1441. * to this frontend DAI */
  1442. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1443. struct snd_soc_pcm_runtime *be = dpcm->be;
  1444. struct snd_pcm_substream *be_substream =
  1445. snd_soc_dpcm_get_substream(be, stream);
  1446. /* is this op for this BE ? */
  1447. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1448. continue;
  1449. /* only free hw when no longer used - check all FEs */
  1450. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1451. continue;
  1452. /* do not free hw if this BE is used by other FE */
  1453. if (be->dpcm[stream].users > 1)
  1454. continue;
  1455. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1456. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1457. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1458. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
  1459. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
  1460. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
  1461. continue;
  1462. dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
  1463. dpcm->fe->dai_link->name);
  1464. soc_pcm_hw_free(be_substream);
  1465. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1466. }
  1467. return 0;
  1468. }
  1469. static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
  1470. {
  1471. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1472. int err, stream = substream->stream;
  1473. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1474. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1475. dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
  1476. /* call hw_free on the frontend */
  1477. err = soc_pcm_hw_free(substream);
  1478. if (err < 0)
  1479. dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
  1480. fe->dai_link->name);
  1481. /* only hw_params backends that are either sinks or sources
  1482. * to this frontend DAI */
  1483. err = dpcm_be_dai_hw_free(fe, stream);
  1484. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1485. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1486. mutex_unlock(&fe->card->mutex);
  1487. return 0;
  1488. }
  1489. int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
  1490. {
  1491. struct snd_soc_dpcm *dpcm;
  1492. int ret;
  1493. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1494. struct snd_soc_pcm_runtime *be = dpcm->be;
  1495. struct snd_pcm_substream *be_substream =
  1496. snd_soc_dpcm_get_substream(be, stream);
  1497. /* is this op for this BE ? */
  1498. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1499. continue;
  1500. /* only allow hw_params() if no connected FEs are running */
  1501. if (!snd_soc_dpcm_can_be_params(fe, be, stream))
  1502. continue;
  1503. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1504. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1505. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
  1506. continue;
  1507. dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
  1508. dpcm->fe->dai_link->name);
  1509. /* copy params for each dpcm */
  1510. memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
  1511. sizeof(struct snd_pcm_hw_params));
  1512. /* perform any hw_params fixups */
  1513. if (be->dai_link->be_hw_params_fixup) {
  1514. ret = be->dai_link->be_hw_params_fixup(be,
  1515. &dpcm->hw_params);
  1516. if (ret < 0) {
  1517. dev_err(be->dev,
  1518. "ASoC: hw_params BE fixup failed %d\n",
  1519. ret);
  1520. goto unwind;
  1521. }
  1522. }
  1523. ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
  1524. if (ret < 0) {
  1525. dev_err(dpcm->be->dev,
  1526. "ASoC: hw_params BE failed %d\n", ret);
  1527. goto unwind;
  1528. }
  1529. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1530. }
  1531. return 0;
  1532. unwind:
  1533. /* disable any enabled and non active backends */
  1534. list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1535. struct snd_soc_pcm_runtime *be = dpcm->be;
  1536. struct snd_pcm_substream *be_substream =
  1537. snd_soc_dpcm_get_substream(be, stream);
  1538. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1539. continue;
  1540. /* only allow hw_free() if no connected FEs are running */
  1541. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1542. continue;
  1543. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1544. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1545. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1546. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1547. continue;
  1548. soc_pcm_hw_free(be_substream);
  1549. }
  1550. return ret;
  1551. }
  1552. static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
  1553. struct snd_pcm_hw_params *params)
  1554. {
  1555. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1556. int ret, stream = substream->stream;
  1557. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1558. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1559. memcpy(&fe->dpcm[substream->stream].hw_params, params,
  1560. sizeof(struct snd_pcm_hw_params));
  1561. ret = dpcm_be_dai_hw_params(fe, substream->stream);
  1562. if (ret < 0) {
  1563. dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
  1564. goto out;
  1565. }
  1566. dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
  1567. fe->dai_link->name, params_rate(params),
  1568. params_channels(params), params_format(params));
  1569. /* call hw_params on the frontend */
  1570. ret = soc_pcm_hw_params(substream, params);
  1571. if (ret < 0) {
  1572. dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
  1573. dpcm_be_dai_hw_free(fe, stream);
  1574. } else
  1575. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1576. out:
  1577. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1578. mutex_unlock(&fe->card->mutex);
  1579. return ret;
  1580. }
  1581. static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
  1582. struct snd_pcm_substream *substream, int cmd)
  1583. {
  1584. int ret;
  1585. dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
  1586. dpcm->fe->dai_link->name, cmd);
  1587. ret = soc_pcm_trigger(substream, cmd);
  1588. if (ret < 0)
  1589. dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
  1590. return ret;
  1591. }
  1592. int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
  1593. int cmd)
  1594. {
  1595. struct snd_soc_dpcm *dpcm;
  1596. int ret = 0;
  1597. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1598. struct snd_soc_pcm_runtime *be = dpcm->be;
  1599. struct snd_pcm_substream *be_substream =
  1600. snd_soc_dpcm_get_substream(be, stream);
  1601. /* is this op for this BE ? */
  1602. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1603. continue;
  1604. switch (cmd) {
  1605. case SNDRV_PCM_TRIGGER_START:
  1606. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1607. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1608. continue;
  1609. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1610. if (ret)
  1611. return ret;
  1612. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1613. break;
  1614. case SNDRV_PCM_TRIGGER_RESUME:
  1615. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
  1616. continue;
  1617. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1618. if (ret)
  1619. return ret;
  1620. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1621. break;
  1622. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1623. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
  1624. continue;
  1625. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1626. if (ret)
  1627. return ret;
  1628. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1629. break;
  1630. case SNDRV_PCM_TRIGGER_STOP:
  1631. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1632. continue;
  1633. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1634. continue;
  1635. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1636. if (ret)
  1637. return ret;
  1638. be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  1639. break;
  1640. case SNDRV_PCM_TRIGGER_SUSPEND:
  1641. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1642. continue;
  1643. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1644. continue;
  1645. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1646. if (ret)
  1647. return ret;
  1648. be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
  1649. break;
  1650. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1651. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1652. continue;
  1653. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1654. continue;
  1655. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1656. if (ret)
  1657. return ret;
  1658. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  1659. break;
  1660. }
  1661. }
  1662. return ret;
  1663. }
  1664. EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
  1665. static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
  1666. {
  1667. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1668. int stream = substream->stream, ret;
  1669. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1670. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1671. switch (trigger) {
  1672. case SND_SOC_DPCM_TRIGGER_PRE:
  1673. /* call trigger on the frontend before the backend. */
  1674. dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
  1675. fe->dai_link->name, cmd);
  1676. ret = soc_pcm_trigger(substream, cmd);
  1677. if (ret < 0) {
  1678. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  1679. goto out;
  1680. }
  1681. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1682. break;
  1683. case SND_SOC_DPCM_TRIGGER_POST:
  1684. /* call trigger on the frontend after the backend. */
  1685. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1686. if (ret < 0) {
  1687. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  1688. goto out;
  1689. }
  1690. dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
  1691. fe->dai_link->name, cmd);
  1692. ret = soc_pcm_trigger(substream, cmd);
  1693. break;
  1694. case SND_SOC_DPCM_TRIGGER_BESPOKE:
  1695. /* bespoke trigger() - handles both FE and BEs */
  1696. dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
  1697. fe->dai_link->name, cmd);
  1698. ret = soc_pcm_bespoke_trigger(substream, cmd);
  1699. if (ret < 0) {
  1700. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  1701. goto out;
  1702. }
  1703. break;
  1704. default:
  1705. dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
  1706. fe->dai_link->name);
  1707. ret = -EINVAL;
  1708. goto out;
  1709. }
  1710. switch (cmd) {
  1711. case SNDRV_PCM_TRIGGER_START:
  1712. case SNDRV_PCM_TRIGGER_RESUME:
  1713. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1714. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1715. break;
  1716. case SNDRV_PCM_TRIGGER_STOP:
  1717. case SNDRV_PCM_TRIGGER_SUSPEND:
  1718. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  1719. break;
  1720. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1721. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  1722. break;
  1723. }
  1724. out:
  1725. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  1726. return ret;
  1727. }
  1728. static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
  1729. {
  1730. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1731. int stream = substream->stream;
  1732. /* if FE's runtime_update is already set, we're in race;
  1733. * process this trigger later at exit
  1734. */
  1735. if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
  1736. fe->dpcm[stream].trigger_pending = cmd + 1;
  1737. return 0; /* delayed, assuming it's successful */
  1738. }
  1739. /* we're alone, let's trigger */
  1740. return dpcm_fe_dai_do_trigger(substream, cmd);
  1741. }
  1742. int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
  1743. {
  1744. struct snd_soc_dpcm *dpcm;
  1745. int ret = 0;
  1746. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1747. struct snd_soc_pcm_runtime *be = dpcm->be;
  1748. struct snd_pcm_substream *be_substream =
  1749. snd_soc_dpcm_get_substream(be, stream);
  1750. /* is this op for this BE ? */
  1751. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1752. continue;
  1753. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1754. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1755. continue;
  1756. dev_dbg(be->dev, "ASoC: prepare BE %s\n",
  1757. dpcm->fe->dai_link->name);
  1758. ret = soc_pcm_prepare(be_substream);
  1759. if (ret < 0) {
  1760. dev_err(be->dev, "ASoC: backend prepare failed %d\n",
  1761. ret);
  1762. break;
  1763. }
  1764. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  1765. }
  1766. return ret;
  1767. }
  1768. static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
  1769. {
  1770. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1771. int stream = substream->stream, ret = 0;
  1772. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1773. dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
  1774. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1775. /* there is no point preparing this FE if there are no BEs */
  1776. if (list_empty(&fe->dpcm[stream].be_clients)) {
  1777. dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
  1778. fe->dai_link->name);
  1779. ret = -EINVAL;
  1780. goto out;
  1781. }
  1782. ret = dpcm_be_dai_prepare(fe, substream->stream);
  1783. if (ret < 0)
  1784. goto out;
  1785. /* call prepare on the frontend */
  1786. ret = soc_pcm_prepare(substream);
  1787. if (ret < 0) {
  1788. dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
  1789. fe->dai_link->name);
  1790. goto out;
  1791. }
  1792. /* run the stream event for each BE */
  1793. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  1794. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  1795. out:
  1796. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1797. mutex_unlock(&fe->card->mutex);
  1798. return ret;
  1799. }
  1800. static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
  1801. unsigned int cmd, void *arg)
  1802. {
  1803. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1804. struct snd_soc_platform *platform = rtd->platform;
  1805. if (platform->driver->ops && platform->driver->ops->ioctl)
  1806. return platform->driver->ops->ioctl(substream, cmd, arg);
  1807. return snd_pcm_lib_ioctl(substream, cmd, arg);
  1808. }
  1809. static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  1810. {
  1811. struct snd_pcm_substream *substream =
  1812. snd_soc_dpcm_get_substream(fe, stream);
  1813. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1814. int err;
  1815. dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
  1816. stream ? "capture" : "playback", fe->dai_link->name);
  1817. if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
  1818. /* call bespoke trigger - FE takes care of all BE triggers */
  1819. dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
  1820. fe->dai_link->name);
  1821. err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  1822. if (err < 0)
  1823. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
  1824. } else {
  1825. dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
  1826. fe->dai_link->name);
  1827. err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
  1828. if (err < 0)
  1829. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
  1830. }
  1831. err = dpcm_be_dai_hw_free(fe, stream);
  1832. if (err < 0)
  1833. dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
  1834. err = dpcm_be_dai_shutdown(fe, stream);
  1835. if (err < 0)
  1836. dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
  1837. /* run the stream event for each BE */
  1838. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
  1839. return 0;
  1840. }
  1841. static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
  1842. {
  1843. struct snd_pcm_substream *substream =
  1844. snd_soc_dpcm_get_substream(fe, stream);
  1845. struct snd_soc_dpcm *dpcm;
  1846. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1847. int ret;
  1848. dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
  1849. stream ? "capture" : "playback", fe->dai_link->name);
  1850. /* Only start the BE if the FE is ready */
  1851. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
  1852. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
  1853. return -EINVAL;
  1854. /* startup must always be called for new BEs */
  1855. ret = dpcm_be_dai_startup(fe, stream);
  1856. if (ret < 0)
  1857. goto disconnect;
  1858. /* keep going if FE state is > open */
  1859. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
  1860. return 0;
  1861. ret = dpcm_be_dai_hw_params(fe, stream);
  1862. if (ret < 0)
  1863. goto close;
  1864. /* keep going if FE state is > hw_params */
  1865. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
  1866. return 0;
  1867. ret = dpcm_be_dai_prepare(fe, stream);
  1868. if (ret < 0)
  1869. goto hw_free;
  1870. /* run the stream event for each BE */
  1871. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
  1872. /* keep going if FE state is > prepare */
  1873. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
  1874. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
  1875. return 0;
  1876. if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
  1877. /* call trigger on the frontend - FE takes care of all BE triggers */
  1878. dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
  1879. fe->dai_link->name);
  1880. ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
  1881. if (ret < 0) {
  1882. dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
  1883. goto hw_free;
  1884. }
  1885. } else {
  1886. dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
  1887. fe->dai_link->name);
  1888. ret = dpcm_be_dai_trigger(fe, stream,
  1889. SNDRV_PCM_TRIGGER_START);
  1890. if (ret < 0) {
  1891. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  1892. goto hw_free;
  1893. }
  1894. }
  1895. return 0;
  1896. hw_free:
  1897. dpcm_be_dai_hw_free(fe, stream);
  1898. close:
  1899. dpcm_be_dai_shutdown(fe, stream);
  1900. disconnect:
  1901. /* disconnect any non started BEs */
  1902. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1903. struct snd_soc_pcm_runtime *be = dpcm->be;
  1904. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1905. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1906. }
  1907. return ret;
  1908. }
  1909. static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
  1910. {
  1911. int ret;
  1912. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
  1913. ret = dpcm_run_update_startup(fe, stream);
  1914. if (ret < 0)
  1915. dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
  1916. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1917. return ret;
  1918. }
  1919. static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
  1920. {
  1921. int ret;
  1922. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
  1923. ret = dpcm_run_update_shutdown(fe, stream);
  1924. if (ret < 0)
  1925. dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
  1926. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1927. return ret;
  1928. }
  1929. /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
  1930. * any DAI links.
  1931. */
  1932. int soc_dpcm_runtime_update(struct snd_soc_card *card)
  1933. {
  1934. int i, old, new, paths;
  1935. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1936. for (i = 0; i < card->num_rtd; i++) {
  1937. struct snd_soc_dapm_widget_list *list;
  1938. struct snd_soc_pcm_runtime *fe = &card->rtd[i];
  1939. /* make sure link is FE */
  1940. if (!fe->dai_link->dynamic)
  1941. continue;
  1942. /* only check active links */
  1943. if (!fe->cpu_dai->active)
  1944. continue;
  1945. /* DAPM sync will call this to update DSP paths */
  1946. dev_dbg(fe->dev, "ASoC: DPCM runtime update for FE %s\n",
  1947. fe->dai_link->name);
  1948. /* skip if FE doesn't have playback capability */
  1949. if (!fe->cpu_dai->driver->playback.channels_min
  1950. || !fe->codec_dai->driver->playback.channels_min)
  1951. goto capture;
  1952. /* skip if FE isn't currently playing */
  1953. if (!fe->cpu_dai->playback_active
  1954. || !fe->codec_dai->playback_active)
  1955. goto capture;
  1956. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
  1957. if (paths < 0) {
  1958. dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
  1959. fe->dai_link->name, "playback");
  1960. mutex_unlock(&card->mutex);
  1961. return paths;
  1962. }
  1963. /* update any new playback paths */
  1964. new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 1);
  1965. if (new) {
  1966. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1967. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1968. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1969. }
  1970. /* update any old playback paths */
  1971. old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, 0);
  1972. if (old) {
  1973. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1974. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1975. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  1976. }
  1977. dpcm_path_put(&list);
  1978. capture:
  1979. /* skip if FE doesn't have capture capability */
  1980. if (!fe->cpu_dai->driver->capture.channels_min
  1981. || !fe->codec_dai->driver->capture.channels_min)
  1982. continue;
  1983. /* skip if FE isn't currently capturing */
  1984. if (!fe->cpu_dai->capture_active
  1985. || !fe->codec_dai->capture_active)
  1986. continue;
  1987. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
  1988. if (paths < 0) {
  1989. dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
  1990. fe->dai_link->name, "capture");
  1991. mutex_unlock(&card->mutex);
  1992. return paths;
  1993. }
  1994. /* update any new capture paths */
  1995. new = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 1);
  1996. if (new) {
  1997. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  1998. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  1999. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  2000. }
  2001. /* update any old capture paths */
  2002. old = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, 0);
  2003. if (old) {
  2004. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  2005. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  2006. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  2007. }
  2008. dpcm_path_put(&list);
  2009. }
  2010. mutex_unlock(&card->mutex);
  2011. return 0;
  2012. }
  2013. int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
  2014. {
  2015. struct snd_soc_dpcm *dpcm;
  2016. struct list_head *clients =
  2017. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
  2018. list_for_each_entry(dpcm, clients, list_be) {
  2019. struct snd_soc_pcm_runtime *be = dpcm->be;
  2020. int i;
  2021. if (be->dai_link->ignore_suspend)
  2022. continue;
  2023. for (i = 0; i < be->num_codecs; i++) {
  2024. struct snd_soc_dai *dai = be->codec_dais[i];
  2025. struct snd_soc_dai_driver *drv = dai->driver;
  2026. dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
  2027. be->dai_link->name);
  2028. if (drv->ops && drv->ops->digital_mute &&
  2029. dai->playback_active)
  2030. drv->ops->digital_mute(dai, mute);
  2031. }
  2032. }
  2033. return 0;
  2034. }
  2035. static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
  2036. {
  2037. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  2038. struct snd_soc_dpcm *dpcm;
  2039. struct snd_soc_dapm_widget_list *list;
  2040. int ret;
  2041. int stream = fe_substream->stream;
  2042. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  2043. fe->dpcm[stream].runtime = fe_substream->runtime;
  2044. ret = dpcm_path_get(fe, stream, &list);
  2045. if (ret < 0) {
  2046. mutex_unlock(&fe->card->mutex);
  2047. return ret;
  2048. } else if (ret == 0) {
  2049. dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
  2050. fe->dai_link->name, stream ? "capture" : "playback");
  2051. }
  2052. /* calculate valid and active FE <-> BE dpcms */
  2053. dpcm_process_paths(fe, stream, &list, 1);
  2054. ret = dpcm_fe_dai_startup(fe_substream);
  2055. if (ret < 0) {
  2056. /* clean up all links */
  2057. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  2058. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  2059. dpcm_be_disconnect(fe, stream);
  2060. fe->dpcm[stream].runtime = NULL;
  2061. }
  2062. dpcm_clear_pending_state(fe, stream);
  2063. dpcm_path_put(&list);
  2064. mutex_unlock(&fe->card->mutex);
  2065. return ret;
  2066. }
  2067. static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
  2068. {
  2069. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  2070. struct snd_soc_dpcm *dpcm;
  2071. int stream = fe_substream->stream, ret;
  2072. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  2073. ret = dpcm_fe_dai_shutdown(fe_substream);
  2074. /* mark FE's links ready to prune */
  2075. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  2076. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  2077. dpcm_be_disconnect(fe, stream);
  2078. fe->dpcm[stream].runtime = NULL;
  2079. mutex_unlock(&fe->card->mutex);
  2080. return ret;
  2081. }
  2082. /* create a new pcm */
  2083. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  2084. {
  2085. struct snd_soc_platform *platform = rtd->platform;
  2086. struct snd_soc_dai *codec_dai;
  2087. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  2088. struct snd_pcm *pcm;
  2089. char new_name[64];
  2090. int ret = 0, playback = 0, capture = 0;
  2091. int i;
  2092. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
  2093. playback = rtd->dai_link->dpcm_playback;
  2094. capture = rtd->dai_link->dpcm_capture;
  2095. } else {
  2096. for (i = 0; i < rtd->num_codecs; i++) {
  2097. codec_dai = rtd->codec_dais[i];
  2098. if (codec_dai->driver->playback.channels_min)
  2099. playback = 1;
  2100. if (codec_dai->driver->capture.channels_min)
  2101. capture = 1;
  2102. }
  2103. capture = capture && cpu_dai->driver->capture.channels_min;
  2104. playback = playback && cpu_dai->driver->playback.channels_min;
  2105. }
  2106. if (rtd->dai_link->playback_only) {
  2107. playback = 1;
  2108. capture = 0;
  2109. }
  2110. if (rtd->dai_link->capture_only) {
  2111. playback = 0;
  2112. capture = 1;
  2113. }
  2114. /* create the PCM */
  2115. if (rtd->dai_link->no_pcm) {
  2116. snprintf(new_name, sizeof(new_name), "(%s)",
  2117. rtd->dai_link->stream_name);
  2118. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  2119. playback, capture, &pcm);
  2120. } else {
  2121. if (rtd->dai_link->dynamic)
  2122. snprintf(new_name, sizeof(new_name), "%s (*)",
  2123. rtd->dai_link->stream_name);
  2124. else
  2125. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  2126. rtd->dai_link->stream_name,
  2127. (rtd->num_codecs > 1) ?
  2128. "multicodec" : rtd->codec_dai->name, num);
  2129. ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
  2130. capture, &pcm);
  2131. }
  2132. if (ret < 0) {
  2133. dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
  2134. rtd->dai_link->name);
  2135. return ret;
  2136. }
  2137. dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
  2138. /* DAPM dai link stream work */
  2139. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  2140. pcm->nonatomic = rtd->dai_link->nonatomic;
  2141. rtd->pcm = pcm;
  2142. pcm->private_data = rtd;
  2143. if (rtd->dai_link->no_pcm) {
  2144. if (playback)
  2145. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  2146. if (capture)
  2147. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  2148. goto out;
  2149. }
  2150. /* ASoC PCM operations */
  2151. if (rtd->dai_link->dynamic) {
  2152. rtd->ops.open = dpcm_fe_dai_open;
  2153. rtd->ops.hw_params = dpcm_fe_dai_hw_params;
  2154. rtd->ops.prepare = dpcm_fe_dai_prepare;
  2155. rtd->ops.trigger = dpcm_fe_dai_trigger;
  2156. rtd->ops.hw_free = dpcm_fe_dai_hw_free;
  2157. rtd->ops.close = dpcm_fe_dai_close;
  2158. rtd->ops.pointer = soc_pcm_pointer;
  2159. rtd->ops.ioctl = soc_pcm_ioctl;
  2160. } else {
  2161. rtd->ops.open = soc_pcm_open;
  2162. rtd->ops.hw_params = soc_pcm_hw_params;
  2163. rtd->ops.prepare = soc_pcm_prepare;
  2164. rtd->ops.trigger = soc_pcm_trigger;
  2165. rtd->ops.hw_free = soc_pcm_hw_free;
  2166. rtd->ops.close = soc_pcm_close;
  2167. rtd->ops.pointer = soc_pcm_pointer;
  2168. rtd->ops.ioctl = soc_pcm_ioctl;
  2169. }
  2170. if (platform->driver->ops) {
  2171. rtd->ops.ack = platform->driver->ops->ack;
  2172. rtd->ops.copy = platform->driver->ops->copy;
  2173. rtd->ops.silence = platform->driver->ops->silence;
  2174. rtd->ops.page = platform->driver->ops->page;
  2175. rtd->ops.mmap = platform->driver->ops->mmap;
  2176. }
  2177. if (playback)
  2178. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
  2179. if (capture)
  2180. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
  2181. if (platform->driver->pcm_new) {
  2182. ret = platform->driver->pcm_new(rtd);
  2183. if (ret < 0) {
  2184. dev_err(platform->dev,
  2185. "ASoC: pcm constructor failed: %d\n",
  2186. ret);
  2187. return ret;
  2188. }
  2189. }
  2190. pcm->private_free = platform->driver->pcm_free;
  2191. out:
  2192. dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
  2193. (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
  2194. cpu_dai->name);
  2195. return ret;
  2196. }
  2197. /* is the current PCM operation for this FE ? */
  2198. int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
  2199. {
  2200. if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
  2201. return 1;
  2202. return 0;
  2203. }
  2204. EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
  2205. /* is the current PCM operation for this BE ? */
  2206. int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
  2207. struct snd_soc_pcm_runtime *be, int stream)
  2208. {
  2209. if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
  2210. ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
  2211. be->dpcm[stream].runtime_update))
  2212. return 1;
  2213. return 0;
  2214. }
  2215. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
  2216. /* get the substream for this BE */
  2217. struct snd_pcm_substream *
  2218. snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
  2219. {
  2220. return be->pcm->streams[stream].substream;
  2221. }
  2222. EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
  2223. /* get the BE runtime state */
  2224. enum snd_soc_dpcm_state
  2225. snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
  2226. {
  2227. return be->dpcm[stream].state;
  2228. }
  2229. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
  2230. /* set the BE runtime state */
  2231. void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
  2232. int stream, enum snd_soc_dpcm_state state)
  2233. {
  2234. be->dpcm[stream].state = state;
  2235. }
  2236. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
  2237. /*
  2238. * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
  2239. * are not running, paused or suspended for the specified stream direction.
  2240. */
  2241. int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
  2242. struct snd_soc_pcm_runtime *be, int stream)
  2243. {
  2244. struct snd_soc_dpcm *dpcm;
  2245. int state;
  2246. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  2247. if (dpcm->fe == fe)
  2248. continue;
  2249. state = dpcm->fe->dpcm[stream].state;
  2250. if (state == SND_SOC_DPCM_STATE_START ||
  2251. state == SND_SOC_DPCM_STATE_PAUSED ||
  2252. state == SND_SOC_DPCM_STATE_SUSPEND)
  2253. return 0;
  2254. }
  2255. /* it's safe to free/stop this BE DAI */
  2256. return 1;
  2257. }
  2258. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
  2259. /*
  2260. * We can only change hw params a BE DAI if any of it's FE are not prepared,
  2261. * running, paused or suspended for the specified stream direction.
  2262. */
  2263. int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
  2264. struct snd_soc_pcm_runtime *be, int stream)
  2265. {
  2266. struct snd_soc_dpcm *dpcm;
  2267. int state;
  2268. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  2269. if (dpcm->fe == fe)
  2270. continue;
  2271. state = dpcm->fe->dpcm[stream].state;
  2272. if (state == SND_SOC_DPCM_STATE_START ||
  2273. state == SND_SOC_DPCM_STATE_PAUSED ||
  2274. state == SND_SOC_DPCM_STATE_SUSPEND ||
  2275. state == SND_SOC_DPCM_STATE_PREPARE)
  2276. return 0;
  2277. }
  2278. /* it's safe to change hw_params */
  2279. return 1;
  2280. }
  2281. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
  2282. int snd_soc_platform_trigger(struct snd_pcm_substream *substream,
  2283. int cmd, struct snd_soc_platform *platform)
  2284. {
  2285. if (platform->driver->ops && platform->driver->ops->trigger)
  2286. return platform->driver->ops->trigger(substream, cmd);
  2287. return 0;
  2288. }
  2289. EXPORT_SYMBOL_GPL(snd_soc_platform_trigger);
  2290. #ifdef CONFIG_DEBUG_FS
  2291. static char *dpcm_state_string(enum snd_soc_dpcm_state state)
  2292. {
  2293. switch (state) {
  2294. case SND_SOC_DPCM_STATE_NEW:
  2295. return "new";
  2296. case SND_SOC_DPCM_STATE_OPEN:
  2297. return "open";
  2298. case SND_SOC_DPCM_STATE_HW_PARAMS:
  2299. return "hw_params";
  2300. case SND_SOC_DPCM_STATE_PREPARE:
  2301. return "prepare";
  2302. case SND_SOC_DPCM_STATE_START:
  2303. return "start";
  2304. case SND_SOC_DPCM_STATE_STOP:
  2305. return "stop";
  2306. case SND_SOC_DPCM_STATE_SUSPEND:
  2307. return "suspend";
  2308. case SND_SOC_DPCM_STATE_PAUSED:
  2309. return "paused";
  2310. case SND_SOC_DPCM_STATE_HW_FREE:
  2311. return "hw_free";
  2312. case SND_SOC_DPCM_STATE_CLOSE:
  2313. return "close";
  2314. }
  2315. return "unknown";
  2316. }
  2317. static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
  2318. int stream, char *buf, size_t size)
  2319. {
  2320. struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
  2321. struct snd_soc_dpcm *dpcm;
  2322. ssize_t offset = 0;
  2323. /* FE state */
  2324. offset += snprintf(buf + offset, size - offset,
  2325. "[%s - %s]\n", fe->dai_link->name,
  2326. stream ? "Capture" : "Playback");
  2327. offset += snprintf(buf + offset, size - offset, "State: %s\n",
  2328. dpcm_state_string(fe->dpcm[stream].state));
  2329. if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  2330. (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  2331. offset += snprintf(buf + offset, size - offset,
  2332. "Hardware Params: "
  2333. "Format = %s, Channels = %d, Rate = %d\n",
  2334. snd_pcm_format_name(params_format(params)),
  2335. params_channels(params),
  2336. params_rate(params));
  2337. /* BEs state */
  2338. offset += snprintf(buf + offset, size - offset, "Backends:\n");
  2339. if (list_empty(&fe->dpcm[stream].be_clients)) {
  2340. offset += snprintf(buf + offset, size - offset,
  2341. " No active DSP links\n");
  2342. goto out;
  2343. }
  2344. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  2345. struct snd_soc_pcm_runtime *be = dpcm->be;
  2346. params = &dpcm->hw_params;
  2347. offset += snprintf(buf + offset, size - offset,
  2348. "- %s\n", be->dai_link->name);
  2349. offset += snprintf(buf + offset, size - offset,
  2350. " State: %s\n",
  2351. dpcm_state_string(be->dpcm[stream].state));
  2352. if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  2353. (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  2354. offset += snprintf(buf + offset, size - offset,
  2355. " Hardware Params: "
  2356. "Format = %s, Channels = %d, Rate = %d\n",
  2357. snd_pcm_format_name(params_format(params)),
  2358. params_channels(params),
  2359. params_rate(params));
  2360. }
  2361. out:
  2362. return offset;
  2363. }
  2364. static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
  2365. size_t count, loff_t *ppos)
  2366. {
  2367. struct snd_soc_pcm_runtime *fe = file->private_data;
  2368. ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
  2369. char *buf;
  2370. buf = kmalloc(out_count, GFP_KERNEL);
  2371. if (!buf)
  2372. return -ENOMEM;
  2373. if (fe->cpu_dai->driver->playback.channels_min)
  2374. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
  2375. buf + offset, out_count - offset);
  2376. if (fe->cpu_dai->driver->capture.channels_min)
  2377. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
  2378. buf + offset, out_count - offset);
  2379. ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
  2380. kfree(buf);
  2381. return ret;
  2382. }
  2383. static const struct file_operations dpcm_state_fops = {
  2384. .open = simple_open,
  2385. .read = dpcm_state_read_file,
  2386. .llseek = default_llseek,
  2387. };
  2388. void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
  2389. {
  2390. if (!rtd->dai_link)
  2391. return;
  2392. if (!rtd->card->debugfs_card_root)
  2393. return;
  2394. rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
  2395. rtd->card->debugfs_card_root);
  2396. if (!rtd->debugfs_dpcm_root) {
  2397. dev_dbg(rtd->dev,
  2398. "ASoC: Failed to create dpcm debugfs directory %s\n",
  2399. rtd->dai_link->name);
  2400. return;
  2401. }
  2402. rtd->debugfs_dpcm_state = debugfs_create_file("state", 0444,
  2403. rtd->debugfs_dpcm_root,
  2404. rtd, &dpcm_state_fops);
  2405. }
  2406. #endif