soc-pcm.c 92 KB

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