soc-pcm.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488
  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->card->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->card->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->card->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->card->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->card->pcm_mutex, rtd->card->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->card->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->card->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->card->pcm_mutex, rtd->card->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->card->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->card->pcm_mutex, rtd->card->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->card->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->card->pcm_mutex, rtd->card->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->card->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->card->pcm_mutex, rtd->card->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 (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  805. codec_dai->tx_mask)
  806. soc_pcm_codec_params_fixup(&codec_params,
  807. codec_dai->tx_mask);
  808. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
  809. codec_dai->rx_mask)
  810. soc_pcm_codec_params_fixup(&codec_params,
  811. codec_dai->rx_mask);
  812. ret = soc_dai_hw_params(substream, &codec_params, codec_dai);
  813. if(ret < 0)
  814. goto codec_err;
  815. codec_dai->rate = params_rate(&codec_params);
  816. codec_dai->channels = params_channels(&codec_params);
  817. codec_dai->sample_bits = snd_pcm_format_physical_width(
  818. params_format(&codec_params));
  819. }
  820. ret = soc_dai_hw_params(substream, params, cpu_dai);
  821. if (ret < 0)
  822. goto interface_err;
  823. for_each_rtdcom(rtd, rtdcom) {
  824. component = rtdcom->component;
  825. if (!component->driver->ops ||
  826. !component->driver->ops->hw_params)
  827. continue;
  828. ret = component->driver->ops->hw_params(substream, params);
  829. if (ret < 0) {
  830. dev_err(component->dev,
  831. "ASoC: %s hw params failed: %d\n",
  832. component->name, ret);
  833. goto component_err;
  834. }
  835. }
  836. component = NULL;
  837. /* store the parameters for each DAIs */
  838. cpu_dai->rate = params_rate(params);
  839. cpu_dai->channels = params_channels(params);
  840. cpu_dai->sample_bits =
  841. snd_pcm_format_physical_width(params_format(params));
  842. ret = soc_pcm_params_symmetry(substream, params);
  843. if (ret)
  844. goto component_err;
  845. out:
  846. mutex_unlock(&rtd->card->pcm_mutex);
  847. return ret;
  848. component_err:
  849. soc_pcm_components_hw_free(substream, component);
  850. if (cpu_dai->driver->ops->hw_free)
  851. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  852. interface_err:
  853. i = rtd->num_codecs;
  854. codec_err:
  855. while (--i >= 0) {
  856. struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
  857. if (codec_dai->driver->ops->hw_free)
  858. codec_dai->driver->ops->hw_free(substream, codec_dai);
  859. codec_dai->rate = 0;
  860. }
  861. if (rtd->dai_link->ops->hw_free)
  862. rtd->dai_link->ops->hw_free(substream);
  863. mutex_unlock(&rtd->card->pcm_mutex);
  864. return ret;
  865. }
  866. /*
  867. * Frees resources allocated by hw_params, can be called multiple times
  868. */
  869. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  870. {
  871. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  872. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  873. struct snd_soc_dai *codec_dai;
  874. bool playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  875. int i;
  876. mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass);
  877. /* clear the corresponding DAIs parameters when going to be inactive */
  878. if (cpu_dai->active == 1) {
  879. cpu_dai->rate = 0;
  880. cpu_dai->channels = 0;
  881. cpu_dai->sample_bits = 0;
  882. }
  883. for (i = 0; i < rtd->num_codecs; i++) {
  884. codec_dai = rtd->codec_dais[i];
  885. if (codec_dai->active == 1) {
  886. codec_dai->rate = 0;
  887. codec_dai->channels = 0;
  888. codec_dai->sample_bits = 0;
  889. }
  890. }
  891. /* apply codec digital mute */
  892. for (i = 0; i < rtd->num_codecs; i++) {
  893. if ((playback && rtd->codec_dais[i]->playback_active == 1) ||
  894. (!playback && rtd->codec_dais[i]->capture_active == 1))
  895. snd_soc_dai_digital_mute(rtd->codec_dais[i], 1,
  896. substream->stream);
  897. }
  898. /* free any machine hw params */
  899. if (rtd->dai_link->ops->hw_free)
  900. rtd->dai_link->ops->hw_free(substream);
  901. /* free any component resources */
  902. soc_pcm_components_hw_free(substream, NULL);
  903. /* now free hw params for the DAIs */
  904. for (i = 0; i < rtd->num_codecs; i++) {
  905. codec_dai = rtd->codec_dais[i];
  906. if (codec_dai->driver->ops->hw_free)
  907. codec_dai->driver->ops->hw_free(substream, codec_dai);
  908. }
  909. if (cpu_dai->driver->ops->hw_free)
  910. cpu_dai->driver->ops->hw_free(substream, cpu_dai);
  911. mutex_unlock(&rtd->card->pcm_mutex);
  912. return 0;
  913. }
  914. static int soc_pcm_trigger_start(struct snd_pcm_substream *substream, int cmd)
  915. {
  916. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  917. struct snd_soc_component *component;
  918. struct snd_soc_rtdcom_list *rtdcom;
  919. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  920. struct snd_soc_dai *codec_dai;
  921. int i, ret;
  922. if (rtd->dai_link->ops->trigger) {
  923. ret = rtd->dai_link->ops->trigger(substream, cmd);
  924. if (ret < 0)
  925. return ret;
  926. }
  927. for_each_rtdcom(rtd, rtdcom) {
  928. component = rtdcom->component;
  929. if (!component->driver->ops ||
  930. !component->driver->ops->trigger)
  931. continue;
  932. ret = component->driver->ops->trigger(substream, cmd);
  933. if (ret < 0)
  934. return ret;
  935. }
  936. if (cpu_dai->driver->ops->trigger) {
  937. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  938. if (ret < 0)
  939. return ret;
  940. }
  941. for (i = 0; i < rtd->num_codecs; i++) {
  942. codec_dai = rtd->codec_dais[i];
  943. if (codec_dai->driver->ops->trigger) {
  944. ret = codec_dai->driver->ops->trigger(substream,
  945. cmd, codec_dai);
  946. if (ret < 0)
  947. return ret;
  948. }
  949. }
  950. return 0;
  951. }
  952. static int soc_pcm_trigger_stop(struct snd_pcm_substream *substream, int cmd)
  953. {
  954. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  955. struct snd_soc_component *component;
  956. struct snd_soc_rtdcom_list *rtdcom;
  957. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  958. struct snd_soc_dai *codec_dai;
  959. int i, ret;
  960. for (i = 0; i < rtd->num_codecs; i++) {
  961. codec_dai = rtd->codec_dais[i];
  962. if (codec_dai->driver->ops->trigger) {
  963. ret = codec_dai->driver->ops->trigger(substream,
  964. cmd, codec_dai);
  965. if (ret < 0)
  966. return ret;
  967. }
  968. }
  969. if (cpu_dai->driver->ops->trigger) {
  970. ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
  971. if (ret < 0)
  972. return ret;
  973. }
  974. for_each_rtdcom(rtd, rtdcom) {
  975. component = rtdcom->component;
  976. if (!component->driver->ops ||
  977. !component->driver->ops->trigger)
  978. continue;
  979. ret = component->driver->ops->trigger(substream, cmd);
  980. if (ret < 0)
  981. return ret;
  982. }
  983. if (rtd->dai_link->ops->trigger) {
  984. ret = rtd->dai_link->ops->trigger(substream, cmd);
  985. if (ret < 0)
  986. return ret;
  987. }
  988. return 0;
  989. }
  990. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  991. {
  992. int ret;
  993. switch (cmd) {
  994. case SNDRV_PCM_TRIGGER_START:
  995. case SNDRV_PCM_TRIGGER_RESUME:
  996. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  997. ret = soc_pcm_trigger_start(substream, cmd);
  998. break;
  999. case SNDRV_PCM_TRIGGER_STOP:
  1000. case SNDRV_PCM_TRIGGER_SUSPEND:
  1001. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1002. ret = soc_pcm_trigger_stop(substream, cmd);
  1003. break;
  1004. default:
  1005. return -EINVAL;
  1006. }
  1007. return ret;
  1008. }
  1009. static int soc_pcm_bespoke_trigger(struct snd_pcm_substream *substream,
  1010. int cmd)
  1011. {
  1012. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1013. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1014. struct snd_soc_dai *codec_dai;
  1015. int i, ret;
  1016. for (i = 0; i < rtd->num_codecs; i++) {
  1017. codec_dai = rtd->codec_dais[i];
  1018. if (codec_dai->driver->ops->bespoke_trigger) {
  1019. ret = codec_dai->driver->ops->bespoke_trigger(substream,
  1020. cmd, codec_dai);
  1021. if (ret < 0)
  1022. return ret;
  1023. }
  1024. }
  1025. if (cpu_dai->driver->ops->bespoke_trigger) {
  1026. ret = cpu_dai->driver->ops->bespoke_trigger(substream, cmd, cpu_dai);
  1027. if (ret < 0)
  1028. return ret;
  1029. }
  1030. return 0;
  1031. }
  1032. /*
  1033. * soc level wrapper for pointer callback
  1034. * If cpu_dai, codec_dai, component driver has the delay callback, then
  1035. * the runtime->delay will be updated accordingly.
  1036. */
  1037. static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
  1038. {
  1039. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1040. struct snd_soc_component *component;
  1041. struct snd_soc_rtdcom_list *rtdcom;
  1042. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1043. struct snd_soc_dai *codec_dai;
  1044. struct snd_pcm_runtime *runtime = substream->runtime;
  1045. snd_pcm_uframes_t offset = 0;
  1046. snd_pcm_sframes_t delay = 0;
  1047. snd_pcm_sframes_t codec_delay = 0;
  1048. int i;
  1049. /* clearing the previous total delay */
  1050. runtime->delay = 0;
  1051. for_each_rtdcom(rtd, rtdcom) {
  1052. component = rtdcom->component;
  1053. if (!component->driver->ops ||
  1054. !component->driver->ops->pointer)
  1055. continue;
  1056. /* FIXME: use 1st pointer */
  1057. offset = component->driver->ops->pointer(substream);
  1058. break;
  1059. }
  1060. /* base delay if assigned in pointer callback */
  1061. delay = runtime->delay;
  1062. if (cpu_dai->driver->ops->delay)
  1063. delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
  1064. for (i = 0; i < rtd->num_codecs; i++) {
  1065. codec_dai = rtd->codec_dais[i];
  1066. if (codec_dai->driver->ops->delay)
  1067. codec_delay = max(codec_delay,
  1068. codec_dai->driver->ops->delay(substream,
  1069. codec_dai));
  1070. }
  1071. delay += codec_delay;
  1072. runtime->delay += delay;
  1073. return offset;
  1074. }
  1075. /* connect a FE and BE */
  1076. static int dpcm_be_connect(struct snd_soc_pcm_runtime *fe,
  1077. struct snd_soc_pcm_runtime *be, int stream)
  1078. {
  1079. struct snd_soc_dpcm *dpcm;
  1080. /* only add new dpcms */
  1081. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1082. if (dpcm->be == be && dpcm->fe == fe)
  1083. return 0;
  1084. }
  1085. dpcm = kzalloc(sizeof(struct snd_soc_dpcm), GFP_KERNEL);
  1086. if (!dpcm)
  1087. return -ENOMEM;
  1088. dpcm->be = be;
  1089. dpcm->fe = fe;
  1090. be->dpcm[stream].runtime = fe->dpcm[stream].runtime;
  1091. dpcm->state = SND_SOC_DPCM_LINK_STATE_NEW;
  1092. list_add(&dpcm->list_be, &fe->dpcm[stream].be_clients);
  1093. list_add(&dpcm->list_fe, &be->dpcm[stream].fe_clients);
  1094. dev_dbg(fe->dev, "connected new DPCM %s path %s %s %s\n",
  1095. stream ? "capture" : "playback", fe->dai_link->name,
  1096. stream ? "<-" : "->", be->dai_link->name);
  1097. #ifdef CONFIG_DEBUG_FS
  1098. if (fe->debugfs_dpcm_root)
  1099. dpcm->debugfs_state = debugfs_create_u32(be->dai_link->name, 0644,
  1100. fe->debugfs_dpcm_root, &dpcm->state);
  1101. #endif
  1102. return 1;
  1103. }
  1104. /* reparent a BE onto another FE */
  1105. static void dpcm_be_reparent(struct snd_soc_pcm_runtime *fe,
  1106. struct snd_soc_pcm_runtime *be, int stream)
  1107. {
  1108. struct snd_soc_dpcm *dpcm;
  1109. struct snd_pcm_substream *fe_substream, *be_substream;
  1110. /* reparent if BE is connected to other FEs */
  1111. if (!be->dpcm[stream].users)
  1112. return;
  1113. be_substream = snd_soc_dpcm_get_substream(be, stream);
  1114. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  1115. if (dpcm->fe == fe)
  1116. continue;
  1117. dev_dbg(fe->dev, "reparent %s path %s %s %s\n",
  1118. stream ? "capture" : "playback",
  1119. dpcm->fe->dai_link->name,
  1120. stream ? "<-" : "->", dpcm->be->dai_link->name);
  1121. fe_substream = snd_soc_dpcm_get_substream(dpcm->fe, stream);
  1122. be_substream->runtime = fe_substream->runtime;
  1123. break;
  1124. }
  1125. }
  1126. /* disconnect a BE and FE */
  1127. void dpcm_be_disconnect(struct snd_soc_pcm_runtime *fe, int stream)
  1128. {
  1129. struct snd_soc_dpcm *dpcm, *d;
  1130. list_for_each_entry_safe(dpcm, d, &fe->dpcm[stream].be_clients, list_be) {
  1131. dev_dbg(fe->dev, "ASoC: BE %s disconnect check for %s\n",
  1132. stream ? "capture" : "playback",
  1133. dpcm->be->dai_link->name);
  1134. if (dpcm->state != SND_SOC_DPCM_LINK_STATE_FREE)
  1135. continue;
  1136. dev_dbg(fe->dev, "freed DSP %s path %s %s %s\n",
  1137. stream ? "capture" : "playback", fe->dai_link->name,
  1138. stream ? "<-" : "->", dpcm->be->dai_link->name);
  1139. /* BEs still alive need new FE */
  1140. dpcm_be_reparent(fe, dpcm->be, stream);
  1141. #ifdef CONFIG_DEBUG_FS
  1142. debugfs_remove(dpcm->debugfs_state);
  1143. #endif
  1144. list_del(&dpcm->list_be);
  1145. list_del(&dpcm->list_fe);
  1146. kfree(dpcm);
  1147. }
  1148. }
  1149. /* get BE for DAI widget and stream */
  1150. static struct snd_soc_pcm_runtime *dpcm_get_be(struct snd_soc_card *card,
  1151. struct snd_soc_dapm_widget *widget, int stream)
  1152. {
  1153. struct snd_soc_pcm_runtime *be;
  1154. int i;
  1155. dev_dbg(card->dev, "ASoC: find BE for widget %s\n", widget->name);
  1156. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1157. list_for_each_entry(be, &card->rtd_list, list) {
  1158. if (!be->dai_link->no_pcm)
  1159. continue;
  1160. dev_dbg(card->dev, "ASoC: try BE : %s\n",
  1161. be->cpu_dai->playback_widget ?
  1162. be->cpu_dai->playback_widget->name : "(not set)");
  1163. if (be->cpu_dai->playback_widget == widget)
  1164. return be;
  1165. for (i = 0; i < be->num_codecs; i++) {
  1166. struct snd_soc_dai *dai = be->codec_dais[i];
  1167. if (dai->playback_widget == widget)
  1168. return be;
  1169. }
  1170. }
  1171. } else {
  1172. list_for_each_entry(be, &card->rtd_list, list) {
  1173. if (!be->dai_link->no_pcm)
  1174. continue;
  1175. dev_dbg(card->dev, "ASoC: try BE %s\n",
  1176. be->cpu_dai->capture_widget ?
  1177. be->cpu_dai->capture_widget->name : "(not set)");
  1178. if (be->cpu_dai->capture_widget == widget)
  1179. return be;
  1180. for (i = 0; i < be->num_codecs; i++) {
  1181. struct snd_soc_dai *dai = be->codec_dais[i];
  1182. if (dai->capture_widget == widget)
  1183. return be;
  1184. }
  1185. }
  1186. }
  1187. /* dai link name and stream name set correctly ? */
  1188. dev_err(card->dev, "ASoC: can't get %s BE for %s\n",
  1189. stream ? "capture" : "playback", widget->name);
  1190. return NULL;
  1191. }
  1192. static inline struct snd_soc_dapm_widget *
  1193. dai_get_widget(struct snd_soc_dai *dai, int stream)
  1194. {
  1195. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1196. return dai->playback_widget;
  1197. else
  1198. return dai->capture_widget;
  1199. }
  1200. static int widget_in_list(struct snd_soc_dapm_widget_list *list,
  1201. struct snd_soc_dapm_widget *widget)
  1202. {
  1203. int i;
  1204. for (i = 0; i < list->num_widgets; i++) {
  1205. if (widget == list->widgets[i])
  1206. return 1;
  1207. }
  1208. return 0;
  1209. }
  1210. static bool dpcm_end_walk_at_be(struct snd_soc_dapm_widget *widget,
  1211. enum snd_soc_dapm_direction dir)
  1212. {
  1213. struct snd_soc_card *card = widget->dapm->card;
  1214. struct snd_soc_pcm_runtime *rtd;
  1215. int i;
  1216. if (dir == SND_SOC_DAPM_DIR_OUT) {
  1217. list_for_each_entry(rtd, &card->rtd_list, list) {
  1218. if (!rtd->dai_link->no_pcm)
  1219. continue;
  1220. if (rtd->cpu_dai->playback_widget == widget)
  1221. return true;
  1222. for (i = 0; i < rtd->num_codecs; ++i) {
  1223. struct snd_soc_dai *dai = rtd->codec_dais[i];
  1224. if (dai->playback_widget == widget)
  1225. return true;
  1226. }
  1227. }
  1228. } else { /* SND_SOC_DAPM_DIR_IN */
  1229. list_for_each_entry(rtd, &card->rtd_list, list) {
  1230. if (!rtd->dai_link->no_pcm)
  1231. continue;
  1232. if (rtd->cpu_dai->capture_widget == widget)
  1233. return true;
  1234. for (i = 0; i < rtd->num_codecs; ++i) {
  1235. struct snd_soc_dai *dai = rtd->codec_dais[i];
  1236. if (dai->capture_widget == widget)
  1237. return true;
  1238. }
  1239. }
  1240. }
  1241. return false;
  1242. }
  1243. int dpcm_path_get(struct snd_soc_pcm_runtime *fe,
  1244. int stream, struct snd_soc_dapm_widget_list **list)
  1245. {
  1246. struct snd_soc_dai *cpu_dai = fe->cpu_dai;
  1247. int paths;
  1248. /* get number of valid DAI paths and their widgets */
  1249. paths = snd_soc_dapm_dai_get_connected_widgets(cpu_dai, stream, list,
  1250. dpcm_end_walk_at_be);
  1251. dev_dbg(fe->dev, "ASoC: found %d audio %s paths\n", paths,
  1252. stream ? "capture" : "playback");
  1253. return paths;
  1254. }
  1255. static int dpcm_prune_paths(struct snd_soc_pcm_runtime *fe, int stream,
  1256. struct snd_soc_dapm_widget_list **list_)
  1257. {
  1258. struct snd_soc_dpcm *dpcm;
  1259. struct snd_soc_dapm_widget_list *list = *list_;
  1260. struct snd_soc_dapm_widget *widget;
  1261. int prune = 0;
  1262. /* Destroy any old FE <--> BE connections */
  1263. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1264. unsigned int i;
  1265. /* is there a valid CPU DAI widget for this BE */
  1266. widget = dai_get_widget(dpcm->be->cpu_dai, stream);
  1267. /* prune the BE if it's no longer in our active list */
  1268. if (widget && widget_in_list(list, widget))
  1269. continue;
  1270. /* is there a valid CODEC DAI widget for this BE */
  1271. for (i = 0; i < dpcm->be->num_codecs; i++) {
  1272. struct snd_soc_dai *dai = dpcm->be->codec_dais[i];
  1273. widget = dai_get_widget(dai, stream);
  1274. /* prune the BE if it's no longer in our active list */
  1275. if (widget && widget_in_list(list, widget))
  1276. continue;
  1277. }
  1278. dev_dbg(fe->dev, "ASoC: pruning %s BE %s for %s\n",
  1279. stream ? "capture" : "playback",
  1280. dpcm->be->dai_link->name, fe->dai_link->name);
  1281. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  1282. dpcm->be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1283. prune++;
  1284. }
  1285. dev_dbg(fe->dev, "ASoC: found %d old BE paths for pruning\n", prune);
  1286. return prune;
  1287. }
  1288. static int dpcm_add_paths(struct snd_soc_pcm_runtime *fe, int stream,
  1289. struct snd_soc_dapm_widget_list **list_)
  1290. {
  1291. struct snd_soc_card *card = fe->card;
  1292. struct snd_soc_dapm_widget_list *list = *list_;
  1293. struct snd_soc_pcm_runtime *be;
  1294. int i, new = 0, err;
  1295. /* Create any new FE <--> BE connections */
  1296. for (i = 0; i < list->num_widgets; i++) {
  1297. switch (list->widgets[i]->id) {
  1298. case snd_soc_dapm_dai_in:
  1299. if (stream != SNDRV_PCM_STREAM_PLAYBACK)
  1300. continue;
  1301. break;
  1302. case snd_soc_dapm_dai_out:
  1303. if (stream != SNDRV_PCM_STREAM_CAPTURE)
  1304. continue;
  1305. break;
  1306. default:
  1307. continue;
  1308. }
  1309. /* is there a valid BE rtd for this widget */
  1310. be = dpcm_get_be(card, list->widgets[i], stream);
  1311. if (!be) {
  1312. dev_err(fe->dev, "ASoC: no BE found for %s\n",
  1313. list->widgets[i]->name);
  1314. continue;
  1315. }
  1316. /* make sure BE is a real BE */
  1317. if (!be->dai_link->no_pcm)
  1318. continue;
  1319. /* don't connect if FE is not running */
  1320. if (!fe->dpcm[stream].runtime && !fe->fe_compr)
  1321. continue;
  1322. /* newly connected FE and BE */
  1323. err = dpcm_be_connect(fe, be, stream);
  1324. if (err < 0) {
  1325. dev_err(fe->dev, "ASoC: can't connect %s\n",
  1326. list->widgets[i]->name);
  1327. break;
  1328. } else if (err == 0) /* already connected */
  1329. continue;
  1330. /* new */
  1331. be->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_BE;
  1332. new++;
  1333. }
  1334. dev_dbg(fe->dev, "ASoC: found %d new BE paths\n", new);
  1335. return new;
  1336. }
  1337. /*
  1338. * Find the corresponding BE DAIs that source or sink audio to this
  1339. * FE substream.
  1340. */
  1341. int dpcm_process_paths(struct snd_soc_pcm_runtime *fe,
  1342. int stream, struct snd_soc_dapm_widget_list **list, int new)
  1343. {
  1344. if (new)
  1345. return dpcm_add_paths(fe, stream, list);
  1346. else
  1347. return dpcm_prune_paths(fe, stream, list);
  1348. }
  1349. void dpcm_clear_pending_state(struct snd_soc_pcm_runtime *fe, int stream)
  1350. {
  1351. struct snd_soc_dpcm *dpcm;
  1352. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  1353. dpcm->be->dpcm[stream].runtime_update =
  1354. SND_SOC_DPCM_UPDATE_NO;
  1355. }
  1356. static void dpcm_be_dai_startup_unwind(struct snd_soc_pcm_runtime *fe,
  1357. int stream)
  1358. {
  1359. struct snd_soc_dpcm *dpcm;
  1360. /* disable any enabled and non active backends */
  1361. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1362. struct snd_soc_pcm_runtime *be = dpcm->be;
  1363. struct snd_pcm_substream *be_substream =
  1364. snd_soc_dpcm_get_substream(be, stream);
  1365. if (be->dpcm[stream].users == 0)
  1366. dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
  1367. stream ? "capture" : "playback",
  1368. be->dpcm[stream].state);
  1369. if (--be->dpcm[stream].users != 0)
  1370. continue;
  1371. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  1372. continue;
  1373. soc_pcm_close(be_substream);
  1374. be_substream->runtime = NULL;
  1375. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1376. }
  1377. }
  1378. int dpcm_be_dai_startup(struct snd_soc_pcm_runtime *fe, int stream)
  1379. {
  1380. struct snd_soc_dpcm *dpcm;
  1381. int err, count = 0;
  1382. /* only startup BE DAIs that are either sinks or sources to this FE DAI */
  1383. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1384. struct snd_soc_pcm_runtime *be = dpcm->be;
  1385. struct snd_pcm_substream *be_substream =
  1386. snd_soc_dpcm_get_substream(be, stream);
  1387. if (!be_substream) {
  1388. dev_err(be->dev, "ASoC: no backend %s stream\n",
  1389. stream ? "capture" : "playback");
  1390. continue;
  1391. }
  1392. /* is this op for this BE ? */
  1393. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1394. continue;
  1395. /* first time the dpcm is open ? */
  1396. if (be->dpcm[stream].users == DPCM_MAX_BE_USERS)
  1397. dev_err(be->dev, "ASoC: too many users %s at open %d\n",
  1398. stream ? "capture" : "playback",
  1399. be->dpcm[stream].state);
  1400. if (be->dpcm[stream].users++ != 0)
  1401. continue;
  1402. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_NEW) &&
  1403. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_CLOSE))
  1404. continue;
  1405. dev_dbg(be->dev, "ASoC: open %s BE %s\n",
  1406. stream ? "capture" : "playback", be->dai_link->name);
  1407. be_substream->runtime = be->dpcm[stream].runtime;
  1408. err = soc_pcm_open(be_substream);
  1409. if (err < 0) {
  1410. dev_err(be->dev, "ASoC: BE open failed %d\n", err);
  1411. be->dpcm[stream].users--;
  1412. if (be->dpcm[stream].users < 0)
  1413. dev_err(be->dev, "ASoC: no users %s at unwind %d\n",
  1414. stream ? "capture" : "playback",
  1415. be->dpcm[stream].state);
  1416. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1417. goto unwind;
  1418. }
  1419. be->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  1420. count++;
  1421. }
  1422. return count;
  1423. unwind:
  1424. /* disable any enabled and non active backends */
  1425. list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1426. struct snd_soc_pcm_runtime *be = dpcm->be;
  1427. struct snd_pcm_substream *be_substream =
  1428. snd_soc_dpcm_get_substream(be, stream);
  1429. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1430. continue;
  1431. if (be->dpcm[stream].users == 0)
  1432. dev_err(be->dev, "ASoC: no users %s at close %d\n",
  1433. stream ? "capture" : "playback",
  1434. be->dpcm[stream].state);
  1435. if (--be->dpcm[stream].users != 0)
  1436. continue;
  1437. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)
  1438. continue;
  1439. soc_pcm_close(be_substream);
  1440. be_substream->runtime = NULL;
  1441. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1442. }
  1443. return err;
  1444. }
  1445. static void dpcm_init_runtime_hw(struct snd_pcm_runtime *runtime,
  1446. struct snd_soc_pcm_stream *stream)
  1447. {
  1448. runtime->hw.rate_min = stream->rate_min;
  1449. runtime->hw.rate_max = stream->rate_max;
  1450. runtime->hw.channels_min = stream->channels_min;
  1451. runtime->hw.channels_max = stream->channels_max;
  1452. if (runtime->hw.formats)
  1453. runtime->hw.formats &= stream->formats;
  1454. else
  1455. runtime->hw.formats = stream->formats;
  1456. runtime->hw.rates = stream->rates;
  1457. }
  1458. static void dpcm_runtime_merge_format(struct snd_pcm_substream *substream,
  1459. u64 *formats)
  1460. {
  1461. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1462. struct snd_soc_dpcm *dpcm;
  1463. int stream = substream->stream;
  1464. if (!fe->dai_link->dpcm_merged_format)
  1465. return;
  1466. /*
  1467. * It returns merged BE codec format
  1468. * if FE want to use it (= dpcm_merged_format)
  1469. */
  1470. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1471. struct snd_soc_pcm_runtime *be = dpcm->be;
  1472. struct snd_soc_dai_driver *codec_dai_drv;
  1473. struct snd_soc_pcm_stream *codec_stream;
  1474. int i;
  1475. for (i = 0; i < be->num_codecs; i++) {
  1476. /*
  1477. * Skip CODECs which don't support the current stream
  1478. * type. See soc_pcm_init_runtime_hw() for more details
  1479. */
  1480. if (!snd_soc_dai_stream_valid(be->codec_dais[i],
  1481. stream))
  1482. continue;
  1483. codec_dai_drv = be->codec_dais[i]->driver;
  1484. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1485. codec_stream = &codec_dai_drv->playback;
  1486. else
  1487. codec_stream = &codec_dai_drv->capture;
  1488. *formats &= codec_stream->formats;
  1489. }
  1490. }
  1491. }
  1492. static void dpcm_runtime_merge_chan(struct snd_pcm_substream *substream,
  1493. unsigned int *channels_min,
  1494. unsigned int *channels_max)
  1495. {
  1496. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1497. struct snd_soc_dpcm *dpcm;
  1498. int stream = substream->stream;
  1499. if (!fe->dai_link->dpcm_merged_chan)
  1500. return;
  1501. /*
  1502. * It returns merged BE codec channel;
  1503. * if FE want to use it (= dpcm_merged_chan)
  1504. */
  1505. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1506. struct snd_soc_pcm_runtime *be = dpcm->be;
  1507. struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
  1508. struct snd_soc_dai_driver *codec_dai_drv;
  1509. struct snd_soc_pcm_stream *codec_stream;
  1510. struct snd_soc_pcm_stream *cpu_stream;
  1511. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1512. cpu_stream = &cpu_dai_drv->playback;
  1513. else
  1514. cpu_stream = &cpu_dai_drv->capture;
  1515. *channels_min = max(*channels_min, cpu_stream->channels_min);
  1516. *channels_max = min(*channels_max, cpu_stream->channels_max);
  1517. /*
  1518. * chan min/max cannot be enforced if there are multiple CODEC
  1519. * DAIs connected to a single CPU DAI, use CPU DAI's directly
  1520. */
  1521. if (be->num_codecs == 1) {
  1522. codec_dai_drv = be->codec_dais[0]->driver;
  1523. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1524. codec_stream = &codec_dai_drv->playback;
  1525. else
  1526. codec_stream = &codec_dai_drv->capture;
  1527. *channels_min = max(*channels_min,
  1528. codec_stream->channels_min);
  1529. *channels_max = min(*channels_max,
  1530. codec_stream->channels_max);
  1531. }
  1532. }
  1533. }
  1534. static void dpcm_runtime_merge_rate(struct snd_pcm_substream *substream,
  1535. unsigned int *rates,
  1536. unsigned int *rate_min,
  1537. unsigned int *rate_max)
  1538. {
  1539. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1540. struct snd_soc_dpcm *dpcm;
  1541. int stream = substream->stream;
  1542. if (!fe->dai_link->dpcm_merged_rate)
  1543. return;
  1544. /*
  1545. * It returns merged BE codec channel;
  1546. * if FE want to use it (= dpcm_merged_chan)
  1547. */
  1548. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1549. struct snd_soc_pcm_runtime *be = dpcm->be;
  1550. struct snd_soc_dai_driver *cpu_dai_drv = be->cpu_dai->driver;
  1551. struct snd_soc_dai_driver *codec_dai_drv;
  1552. struct snd_soc_pcm_stream *codec_stream;
  1553. struct snd_soc_pcm_stream *cpu_stream;
  1554. int i;
  1555. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1556. cpu_stream = &cpu_dai_drv->playback;
  1557. else
  1558. cpu_stream = &cpu_dai_drv->capture;
  1559. *rate_min = max(*rate_min, cpu_stream->rate_min);
  1560. *rate_max = min_not_zero(*rate_max, cpu_stream->rate_max);
  1561. *rates = snd_pcm_rate_mask_intersect(*rates, cpu_stream->rates);
  1562. for (i = 0; i < be->num_codecs; i++) {
  1563. /*
  1564. * Skip CODECs which don't support the current stream
  1565. * type. See soc_pcm_init_runtime_hw() for more details
  1566. */
  1567. if (!snd_soc_dai_stream_valid(be->codec_dais[i],
  1568. stream))
  1569. continue;
  1570. codec_dai_drv = be->codec_dais[i]->driver;
  1571. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1572. codec_stream = &codec_dai_drv->playback;
  1573. else
  1574. codec_stream = &codec_dai_drv->capture;
  1575. *rate_min = max(*rate_min, codec_stream->rate_min);
  1576. *rate_max = min_not_zero(*rate_max,
  1577. codec_stream->rate_max);
  1578. *rates = snd_pcm_rate_mask_intersect(*rates,
  1579. codec_stream->rates);
  1580. }
  1581. }
  1582. }
  1583. static void dpcm_set_fe_runtime(struct snd_pcm_substream *substream)
  1584. {
  1585. struct snd_pcm_runtime *runtime = substream->runtime;
  1586. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1587. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1588. struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
  1589. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1590. dpcm_init_runtime_hw(runtime, &cpu_dai_drv->playback);
  1591. else
  1592. dpcm_init_runtime_hw(runtime, &cpu_dai_drv->capture);
  1593. dpcm_runtime_merge_format(substream, &runtime->hw.formats);
  1594. dpcm_runtime_merge_chan(substream, &runtime->hw.channels_min,
  1595. &runtime->hw.channels_max);
  1596. dpcm_runtime_merge_rate(substream, &runtime->hw.rates,
  1597. &runtime->hw.rate_min, &runtime->hw.rate_max);
  1598. }
  1599. static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd);
  1600. /* Set FE's runtime_update state; the state is protected via PCM stream lock
  1601. * for avoiding the race with trigger callback.
  1602. * If the state is unset and a trigger is pending while the previous operation,
  1603. * process the pending trigger action here.
  1604. */
  1605. static void dpcm_set_fe_update_state(struct snd_soc_pcm_runtime *fe,
  1606. int stream, enum snd_soc_dpcm_update state)
  1607. {
  1608. struct snd_pcm_substream *substream =
  1609. snd_soc_dpcm_get_substream(fe, stream);
  1610. snd_pcm_stream_lock_irq(substream);
  1611. if (state == SND_SOC_DPCM_UPDATE_NO && fe->dpcm[stream].trigger_pending) {
  1612. dpcm_fe_dai_do_trigger(substream,
  1613. fe->dpcm[stream].trigger_pending - 1);
  1614. fe->dpcm[stream].trigger_pending = 0;
  1615. }
  1616. fe->dpcm[stream].runtime_update = state;
  1617. snd_pcm_stream_unlock_irq(substream);
  1618. }
  1619. static int dpcm_apply_symmetry(struct snd_pcm_substream *fe_substream,
  1620. int stream)
  1621. {
  1622. struct snd_soc_dpcm *dpcm;
  1623. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1624. struct snd_soc_dai *fe_cpu_dai = fe->cpu_dai;
  1625. int err;
  1626. /* apply symmetry for FE */
  1627. if (soc_pcm_has_symmetry(fe_substream))
  1628. fe_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1629. /* Symmetry only applies if we've got an active stream. */
  1630. if (fe_cpu_dai->active) {
  1631. err = soc_pcm_apply_symmetry(fe_substream, fe_cpu_dai);
  1632. if (err < 0)
  1633. return err;
  1634. }
  1635. /* apply symmetry for BE */
  1636. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1637. struct snd_soc_pcm_runtime *be = dpcm->be;
  1638. struct snd_pcm_substream *be_substream =
  1639. snd_soc_dpcm_get_substream(be, stream);
  1640. struct snd_soc_pcm_runtime *rtd = be_substream->private_data;
  1641. int i;
  1642. if (rtd->dai_link->be_hw_params_fixup)
  1643. continue;
  1644. if (soc_pcm_has_symmetry(be_substream))
  1645. be_substream->runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  1646. /* Symmetry only applies if we've got an active stream. */
  1647. if (rtd->cpu_dai->active) {
  1648. err = soc_pcm_apply_symmetry(fe_substream,
  1649. rtd->cpu_dai);
  1650. if (err < 0)
  1651. return err;
  1652. }
  1653. for (i = 0; i < rtd->num_codecs; i++) {
  1654. if (rtd->codec_dais[i]->active) {
  1655. err = soc_pcm_apply_symmetry(fe_substream,
  1656. rtd->codec_dais[i]);
  1657. if (err < 0)
  1658. return err;
  1659. }
  1660. }
  1661. }
  1662. return 0;
  1663. }
  1664. static int dpcm_fe_dai_startup(struct snd_pcm_substream *fe_substream)
  1665. {
  1666. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  1667. struct snd_pcm_runtime *runtime = fe_substream->runtime;
  1668. int stream = fe_substream->stream, ret = 0;
  1669. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1670. ret = dpcm_be_dai_startup(fe, fe_substream->stream);
  1671. if (ret < 0) {
  1672. dev_err(fe->dev,"ASoC: failed to start some BEs %d\n", ret);
  1673. goto be_err;
  1674. }
  1675. dev_dbg(fe->dev, "ASoC: open FE %s\n", fe->dai_link->name);
  1676. /* start the DAI frontend */
  1677. ret = soc_pcm_open(fe_substream);
  1678. if (ret < 0) {
  1679. dev_err(fe->dev,"ASoC: failed to start FE %d\n", ret);
  1680. goto unwind;
  1681. }
  1682. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  1683. dpcm_set_fe_runtime(fe_substream);
  1684. snd_pcm_limit_hw_rates(runtime);
  1685. ret = dpcm_apply_symmetry(fe_substream, stream);
  1686. if (ret < 0) {
  1687. dev_err(fe->dev, "ASoC: failed to apply dpcm symmetry %d\n",
  1688. ret);
  1689. goto unwind;
  1690. }
  1691. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1692. return 0;
  1693. unwind:
  1694. dpcm_be_dai_startup_unwind(fe, fe_substream->stream);
  1695. be_err:
  1696. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1697. return ret;
  1698. }
  1699. int dpcm_be_dai_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  1700. {
  1701. struct snd_soc_dpcm *dpcm;
  1702. /* only shutdown BEs that are either sinks or sources to this FE DAI */
  1703. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1704. struct snd_soc_pcm_runtime *be = dpcm->be;
  1705. struct snd_pcm_substream *be_substream =
  1706. snd_soc_dpcm_get_substream(be, stream);
  1707. /* is this op for this BE ? */
  1708. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1709. continue;
  1710. if (be->dpcm[stream].users == 0)
  1711. dev_err(be->dev, "ASoC: no users %s at close - state %d\n",
  1712. stream ? "capture" : "playback",
  1713. be->dpcm[stream].state);
  1714. if (--be->dpcm[stream].users != 0)
  1715. continue;
  1716. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1717. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN)) {
  1718. soc_pcm_hw_free(be_substream);
  1719. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1720. }
  1721. dev_dbg(be->dev, "ASoC: close BE %s\n",
  1722. be->dai_link->name);
  1723. soc_pcm_close(be_substream);
  1724. be_substream->runtime = NULL;
  1725. be->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1726. }
  1727. return 0;
  1728. }
  1729. static int dpcm_fe_dai_shutdown(struct snd_pcm_substream *substream)
  1730. {
  1731. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1732. int stream = substream->stream;
  1733. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1734. /* shutdown the BEs */
  1735. dpcm_be_dai_shutdown(fe, substream->stream);
  1736. dev_dbg(fe->dev, "ASoC: close FE %s\n", fe->dai_link->name);
  1737. /* now shutdown the frontend */
  1738. soc_pcm_close(substream);
  1739. /* run the stream event for each BE */
  1740. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  1741. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  1742. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1743. return 0;
  1744. }
  1745. int dpcm_be_dai_hw_free(struct snd_soc_pcm_runtime *fe, int stream)
  1746. {
  1747. struct snd_soc_dpcm *dpcm;
  1748. /* only hw_params backends that are either sinks or sources
  1749. * to this frontend DAI */
  1750. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1751. struct snd_soc_pcm_runtime *be = dpcm->be;
  1752. struct snd_pcm_substream *be_substream =
  1753. snd_soc_dpcm_get_substream(be, stream);
  1754. /* is this op for this BE ? */
  1755. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1756. continue;
  1757. /* only free hw when no longer used - check all FEs */
  1758. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1759. continue;
  1760. /* do not free hw if this BE is used by other FE */
  1761. if (be->dpcm[stream].users > 1)
  1762. continue;
  1763. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1764. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1765. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1766. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED) &&
  1767. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
  1768. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
  1769. continue;
  1770. dev_dbg(be->dev, "ASoC: hw_free BE %s\n",
  1771. be->dai_link->name);
  1772. soc_pcm_hw_free(be_substream);
  1773. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1774. }
  1775. return 0;
  1776. }
  1777. static int dpcm_fe_dai_hw_free(struct snd_pcm_substream *substream)
  1778. {
  1779. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1780. int err, stream = substream->stream;
  1781. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1782. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1783. dev_dbg(fe->dev, "ASoC: hw_free FE %s\n", fe->dai_link->name);
  1784. /* call hw_free on the frontend */
  1785. err = soc_pcm_hw_free(substream);
  1786. if (err < 0)
  1787. dev_err(fe->dev,"ASoC: hw_free FE %s failed\n",
  1788. fe->dai_link->name);
  1789. /* only hw_params backends that are either sinks or sources
  1790. * to this frontend DAI */
  1791. err = dpcm_be_dai_hw_free(fe, stream);
  1792. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_FREE;
  1793. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1794. mutex_unlock(&fe->card->mutex);
  1795. return 0;
  1796. }
  1797. int dpcm_be_dai_hw_params(struct snd_soc_pcm_runtime *fe, int stream)
  1798. {
  1799. struct snd_soc_dpcm *dpcm;
  1800. int ret;
  1801. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1802. struct snd_soc_pcm_runtime *be = dpcm->be;
  1803. struct snd_pcm_substream *be_substream =
  1804. snd_soc_dpcm_get_substream(be, stream);
  1805. /* is this op for this BE ? */
  1806. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1807. continue;
  1808. /* copy params for each dpcm */
  1809. memcpy(&dpcm->hw_params, &fe->dpcm[stream].hw_params,
  1810. sizeof(struct snd_pcm_hw_params));
  1811. /* perform any hw_params fixups */
  1812. if (be->dai_link->be_hw_params_fixup) {
  1813. ret = be->dai_link->be_hw_params_fixup(be,
  1814. &dpcm->hw_params);
  1815. if (ret < 0) {
  1816. dev_err(be->dev,
  1817. "ASoC: hw_params BE fixup failed %d\n",
  1818. ret);
  1819. goto unwind;
  1820. }
  1821. }
  1822. /* only allow hw_params() if no connected FEs are running */
  1823. if (!snd_soc_dpcm_can_be_params(fe, be, stream))
  1824. continue;
  1825. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1826. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1827. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE))
  1828. continue;
  1829. dev_dbg(be->dev, "ASoC: hw_params BE %s\n",
  1830. be->dai_link->name);
  1831. ret = soc_pcm_hw_params(be_substream, &dpcm->hw_params);
  1832. if (ret < 0) {
  1833. dev_err(dpcm->be->dev,
  1834. "ASoC: hw_params BE failed %d\n", ret);
  1835. goto unwind;
  1836. }
  1837. be->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1838. }
  1839. return 0;
  1840. unwind:
  1841. /* disable any enabled and non active backends */
  1842. list_for_each_entry_continue_reverse(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1843. struct snd_soc_pcm_runtime *be = dpcm->be;
  1844. struct snd_pcm_substream *be_substream =
  1845. snd_soc_dpcm_get_substream(be, stream);
  1846. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1847. continue;
  1848. /* only allow hw_free() if no connected FEs are running */
  1849. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1850. continue;
  1851. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_OPEN) &&
  1852. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  1853. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_FREE) &&
  1854. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1855. continue;
  1856. soc_pcm_hw_free(be_substream);
  1857. }
  1858. return ret;
  1859. }
  1860. static int dpcm_fe_dai_hw_params(struct snd_pcm_substream *substream,
  1861. struct snd_pcm_hw_params *params)
  1862. {
  1863. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1864. int ret, stream = substream->stream;
  1865. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  1866. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  1867. memcpy(&fe->dpcm[substream->stream].hw_params, params,
  1868. sizeof(struct snd_pcm_hw_params));
  1869. ret = dpcm_be_dai_hw_params(fe, substream->stream);
  1870. if (ret < 0) {
  1871. dev_err(fe->dev,"ASoC: hw_params BE failed %d\n", ret);
  1872. goto out;
  1873. }
  1874. dev_dbg(fe->dev, "ASoC: hw_params FE %s rate %d chan %x fmt %d\n",
  1875. fe->dai_link->name, params_rate(params),
  1876. params_channels(params), params_format(params));
  1877. /* call hw_params on the frontend */
  1878. ret = soc_pcm_hw_params(substream, params);
  1879. if (ret < 0) {
  1880. dev_err(fe->dev,"ASoC: hw_params FE failed %d\n", ret);
  1881. dpcm_be_dai_hw_free(fe, stream);
  1882. } else
  1883. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_HW_PARAMS;
  1884. out:
  1885. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  1886. mutex_unlock(&fe->card->mutex);
  1887. return ret;
  1888. }
  1889. static int dpcm_do_trigger(struct snd_soc_dpcm *dpcm,
  1890. struct snd_pcm_substream *substream, int cmd)
  1891. {
  1892. int ret;
  1893. dev_dbg(dpcm->be->dev, "ASoC: trigger BE %s cmd %d\n",
  1894. dpcm->be->dai_link->name, cmd);
  1895. ret = soc_pcm_trigger(substream, cmd);
  1896. if (ret < 0)
  1897. dev_err(dpcm->be->dev,"ASoC: trigger BE failed %d\n", ret);
  1898. return ret;
  1899. }
  1900. int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
  1901. int cmd)
  1902. {
  1903. struct snd_soc_dpcm *dpcm;
  1904. int ret = 0;
  1905. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  1906. struct snd_soc_pcm_runtime *be = dpcm->be;
  1907. struct snd_pcm_substream *be_substream =
  1908. snd_soc_dpcm_get_substream(be, stream);
  1909. /* is this op for this BE ? */
  1910. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  1911. continue;
  1912. switch (cmd) {
  1913. case SNDRV_PCM_TRIGGER_START:
  1914. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PREPARE) &&
  1915. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP))
  1916. continue;
  1917. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1918. if (ret)
  1919. return ret;
  1920. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1921. break;
  1922. case SNDRV_PCM_TRIGGER_RESUME:
  1923. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND))
  1924. continue;
  1925. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1926. if (ret)
  1927. return ret;
  1928. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1929. break;
  1930. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1931. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
  1932. continue;
  1933. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1934. if (ret)
  1935. return ret;
  1936. be->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  1937. break;
  1938. case SNDRV_PCM_TRIGGER_STOP:
  1939. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1940. continue;
  1941. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1942. continue;
  1943. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1944. if (ret)
  1945. return ret;
  1946. be->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  1947. break;
  1948. case SNDRV_PCM_TRIGGER_SUSPEND:
  1949. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1950. continue;
  1951. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1952. continue;
  1953. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1954. if (ret)
  1955. return ret;
  1956. be->dpcm[stream].state = SND_SOC_DPCM_STATE_SUSPEND;
  1957. break;
  1958. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1959. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  1960. continue;
  1961. if (!snd_soc_dpcm_can_be_free_stop(fe, be, stream))
  1962. continue;
  1963. ret = dpcm_do_trigger(dpcm, be_substream, cmd);
  1964. if (ret)
  1965. return ret;
  1966. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  1967. break;
  1968. }
  1969. }
  1970. return ret;
  1971. }
  1972. EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
  1973. static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
  1974. {
  1975. struct snd_soc_pcm_runtime *fe = substream->private_data;
  1976. int stream = substream->stream, ret;
  1977. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  1978. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  1979. switch (trigger) {
  1980. case SND_SOC_DPCM_TRIGGER_PRE:
  1981. /* call trigger on the frontend before the backend. */
  1982. dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
  1983. fe->dai_link->name, cmd);
  1984. ret = soc_pcm_trigger(substream, cmd);
  1985. if (ret < 0) {
  1986. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  1987. goto out;
  1988. }
  1989. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1990. break;
  1991. case SND_SOC_DPCM_TRIGGER_POST:
  1992. /* call trigger on the frontend after the backend. */
  1993. ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
  1994. if (ret < 0) {
  1995. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  1996. goto out;
  1997. }
  1998. dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
  1999. fe->dai_link->name, cmd);
  2000. ret = soc_pcm_trigger(substream, cmd);
  2001. break;
  2002. case SND_SOC_DPCM_TRIGGER_BESPOKE:
  2003. /* bespoke trigger() - handles both FE and BEs */
  2004. dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd %d\n",
  2005. fe->dai_link->name, cmd);
  2006. ret = soc_pcm_bespoke_trigger(substream, cmd);
  2007. if (ret < 0) {
  2008. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  2009. goto out;
  2010. }
  2011. break;
  2012. default:
  2013. dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
  2014. fe->dai_link->name);
  2015. ret = -EINVAL;
  2016. goto out;
  2017. }
  2018. switch (cmd) {
  2019. case SNDRV_PCM_TRIGGER_START:
  2020. case SNDRV_PCM_TRIGGER_RESUME:
  2021. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  2022. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  2023. break;
  2024. case SNDRV_PCM_TRIGGER_STOP:
  2025. case SNDRV_PCM_TRIGGER_SUSPEND:
  2026. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  2027. break;
  2028. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  2029. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  2030. break;
  2031. }
  2032. out:
  2033. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  2034. return ret;
  2035. }
  2036. static int dpcm_fe_dai_trigger(struct snd_pcm_substream *substream, int cmd)
  2037. {
  2038. struct snd_soc_pcm_runtime *fe = substream->private_data;
  2039. int stream = substream->stream;
  2040. /* if FE's runtime_update is already set, we're in race;
  2041. * process this trigger later at exit
  2042. */
  2043. if (fe->dpcm[stream].runtime_update != SND_SOC_DPCM_UPDATE_NO) {
  2044. fe->dpcm[stream].trigger_pending = cmd + 1;
  2045. return 0; /* delayed, assuming it's successful */
  2046. }
  2047. /* we're alone, let's trigger */
  2048. return dpcm_fe_dai_do_trigger(substream, cmd);
  2049. }
  2050. int dpcm_be_dai_prepare(struct snd_soc_pcm_runtime *fe, int stream)
  2051. {
  2052. struct snd_soc_dpcm *dpcm;
  2053. int ret = 0;
  2054. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  2055. struct snd_soc_pcm_runtime *be = dpcm->be;
  2056. struct snd_pcm_substream *be_substream =
  2057. snd_soc_dpcm_get_substream(be, stream);
  2058. /* is this op for this BE ? */
  2059. if (!snd_soc_dpcm_be_can_update(fe, be, stream))
  2060. continue;
  2061. if ((be->dpcm[stream].state != SND_SOC_DPCM_STATE_HW_PARAMS) &&
  2062. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_STOP) &&
  2063. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_SUSPEND) &&
  2064. (be->dpcm[stream].state != SND_SOC_DPCM_STATE_PAUSED))
  2065. continue;
  2066. dev_dbg(be->dev, "ASoC: prepare BE %s\n",
  2067. be->dai_link->name);
  2068. ret = soc_pcm_prepare(be_substream);
  2069. if (ret < 0) {
  2070. dev_err(be->dev, "ASoC: backend prepare failed %d\n",
  2071. ret);
  2072. break;
  2073. }
  2074. be->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  2075. }
  2076. return ret;
  2077. }
  2078. static int dpcm_fe_dai_prepare(struct snd_pcm_substream *substream)
  2079. {
  2080. struct snd_soc_pcm_runtime *fe = substream->private_data;
  2081. int stream = substream->stream, ret = 0;
  2082. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  2083. dev_dbg(fe->dev, "ASoC: prepare FE %s\n", fe->dai_link->name);
  2084. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_FE);
  2085. /* there is no point preparing this FE if there are no BEs */
  2086. if (list_empty(&fe->dpcm[stream].be_clients)) {
  2087. dev_err(fe->dev, "ASoC: no backend DAIs enabled for %s\n",
  2088. fe->dai_link->name);
  2089. ret = -EINVAL;
  2090. goto out;
  2091. }
  2092. ret = dpcm_be_dai_prepare(fe, substream->stream);
  2093. if (ret < 0)
  2094. goto out;
  2095. /* call prepare on the frontend */
  2096. ret = soc_pcm_prepare(substream);
  2097. if (ret < 0) {
  2098. dev_err(fe->dev,"ASoC: prepare FE %s failed\n",
  2099. fe->dai_link->name);
  2100. goto out;
  2101. }
  2102. /* run the stream event for each BE */
  2103. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  2104. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  2105. out:
  2106. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  2107. mutex_unlock(&fe->card->mutex);
  2108. return ret;
  2109. }
  2110. static int soc_pcm_ioctl(struct snd_pcm_substream *substream,
  2111. unsigned int cmd, void *arg)
  2112. {
  2113. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2114. struct snd_soc_component *component;
  2115. struct snd_soc_rtdcom_list *rtdcom;
  2116. for_each_rtdcom(rtd, rtdcom) {
  2117. component = rtdcom->component;
  2118. if (!component->driver->ops ||
  2119. !component->driver->ops->ioctl)
  2120. continue;
  2121. /* FIXME: use 1st ioctl */
  2122. return component->driver->ops->ioctl(substream, cmd, arg);
  2123. }
  2124. return snd_pcm_lib_ioctl(substream, cmd, arg);
  2125. }
  2126. static int dpcm_run_update_shutdown(struct snd_soc_pcm_runtime *fe, int stream)
  2127. {
  2128. struct snd_pcm_substream *substream =
  2129. snd_soc_dpcm_get_substream(fe, stream);
  2130. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  2131. int err;
  2132. dev_dbg(fe->dev, "ASoC: runtime %s close on FE %s\n",
  2133. stream ? "capture" : "playback", fe->dai_link->name);
  2134. if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
  2135. /* call bespoke trigger - FE takes care of all BE triggers */
  2136. dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd stop\n",
  2137. fe->dai_link->name);
  2138. err = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  2139. if (err < 0)
  2140. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
  2141. } else {
  2142. dev_dbg(fe->dev, "ASoC: trigger FE %s cmd stop\n",
  2143. fe->dai_link->name);
  2144. err = dpcm_be_dai_trigger(fe, stream, SNDRV_PCM_TRIGGER_STOP);
  2145. if (err < 0)
  2146. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", err);
  2147. }
  2148. err = dpcm_be_dai_hw_free(fe, stream);
  2149. if (err < 0)
  2150. dev_err(fe->dev,"ASoC: hw_free FE failed %d\n", err);
  2151. err = dpcm_be_dai_shutdown(fe, stream);
  2152. if (err < 0)
  2153. dev_err(fe->dev,"ASoC: shutdown FE failed %d\n", err);
  2154. /* run the stream event for each BE */
  2155. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
  2156. return 0;
  2157. }
  2158. static int dpcm_run_update_startup(struct snd_soc_pcm_runtime *fe, int stream)
  2159. {
  2160. struct snd_pcm_substream *substream =
  2161. snd_soc_dpcm_get_substream(fe, stream);
  2162. struct snd_soc_dpcm *dpcm;
  2163. enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
  2164. int ret;
  2165. dev_dbg(fe->dev, "ASoC: runtime %s open on FE %s\n",
  2166. stream ? "capture" : "playback", fe->dai_link->name);
  2167. /* Only start the BE if the FE is ready */
  2168. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_FREE ||
  2169. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_CLOSE)
  2170. return -EINVAL;
  2171. /* startup must always be called for new BEs */
  2172. ret = dpcm_be_dai_startup(fe, stream);
  2173. if (ret < 0)
  2174. goto disconnect;
  2175. /* keep going if FE state is > open */
  2176. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_OPEN)
  2177. return 0;
  2178. ret = dpcm_be_dai_hw_params(fe, stream);
  2179. if (ret < 0)
  2180. goto close;
  2181. /* keep going if FE state is > hw_params */
  2182. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_HW_PARAMS)
  2183. return 0;
  2184. ret = dpcm_be_dai_prepare(fe, stream);
  2185. if (ret < 0)
  2186. goto hw_free;
  2187. /* run the stream event for each BE */
  2188. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_NOP);
  2189. /* keep going if FE state is > prepare */
  2190. if (fe->dpcm[stream].state == SND_SOC_DPCM_STATE_PREPARE ||
  2191. fe->dpcm[stream].state == SND_SOC_DPCM_STATE_STOP)
  2192. return 0;
  2193. if (trigger == SND_SOC_DPCM_TRIGGER_BESPOKE) {
  2194. /* call trigger on the frontend - FE takes care of all BE triggers */
  2195. dev_dbg(fe->dev, "ASoC: bespoke trigger FE %s cmd start\n",
  2196. fe->dai_link->name);
  2197. ret = soc_pcm_bespoke_trigger(substream, SNDRV_PCM_TRIGGER_START);
  2198. if (ret < 0) {
  2199. dev_err(fe->dev,"ASoC: bespoke trigger FE failed %d\n", ret);
  2200. goto hw_free;
  2201. }
  2202. } else {
  2203. dev_dbg(fe->dev, "ASoC: trigger FE %s cmd start\n",
  2204. fe->dai_link->name);
  2205. ret = dpcm_be_dai_trigger(fe, stream,
  2206. SNDRV_PCM_TRIGGER_START);
  2207. if (ret < 0) {
  2208. dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
  2209. goto hw_free;
  2210. }
  2211. }
  2212. return 0;
  2213. hw_free:
  2214. dpcm_be_dai_hw_free(fe, stream);
  2215. close:
  2216. dpcm_be_dai_shutdown(fe, stream);
  2217. disconnect:
  2218. /* disconnect any non started BEs */
  2219. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  2220. struct snd_soc_pcm_runtime *be = dpcm->be;
  2221. if (be->dpcm[stream].state != SND_SOC_DPCM_STATE_START)
  2222. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  2223. }
  2224. return ret;
  2225. }
  2226. static int dpcm_run_new_update(struct snd_soc_pcm_runtime *fe, int stream)
  2227. {
  2228. int ret;
  2229. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
  2230. ret = dpcm_run_update_startup(fe, stream);
  2231. if (ret < 0)
  2232. dev_err(fe->dev, "ASoC: failed to startup some BEs\n");
  2233. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  2234. return ret;
  2235. }
  2236. static int dpcm_run_old_update(struct snd_soc_pcm_runtime *fe, int stream)
  2237. {
  2238. int ret;
  2239. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_BE);
  2240. ret = dpcm_run_update_shutdown(fe, stream);
  2241. if (ret < 0)
  2242. dev_err(fe->dev, "ASoC: failed to shutdown some BEs\n");
  2243. dpcm_set_fe_update_state(fe, stream, SND_SOC_DPCM_UPDATE_NO);
  2244. return ret;
  2245. }
  2246. static int soc_dpcm_fe_runtime_update(struct snd_soc_pcm_runtime *fe, int new)
  2247. {
  2248. struct snd_soc_dapm_widget_list *list;
  2249. int count, paths;
  2250. if (!fe->dai_link->dynamic)
  2251. return 0;
  2252. /* only check active links */
  2253. if (!fe->cpu_dai->active)
  2254. return 0;
  2255. /* DAPM sync will call this to update DSP paths */
  2256. dev_dbg(fe->dev, "ASoC: DPCM %s runtime update for FE %s\n",
  2257. new ? "new" : "old", fe->dai_link->name);
  2258. /* skip if FE doesn't have playback capability */
  2259. if (!fe->cpu_dai->driver->playback.channels_min ||
  2260. !fe->codec_dai->driver->playback.channels_min)
  2261. goto capture;
  2262. /* skip if FE isn't currently playing */
  2263. if (!fe->cpu_dai->playback_active || !fe->codec_dai->playback_active)
  2264. goto capture;
  2265. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_PLAYBACK, &list);
  2266. if (paths < 0) {
  2267. dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
  2268. fe->dai_link->name, "playback");
  2269. return paths;
  2270. }
  2271. /* update any playback paths */
  2272. count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_PLAYBACK, &list, new);
  2273. if (count) {
  2274. if (new)
  2275. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  2276. else
  2277. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_PLAYBACK);
  2278. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_PLAYBACK);
  2279. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_PLAYBACK);
  2280. }
  2281. dpcm_path_put(&list);
  2282. capture:
  2283. /* skip if FE doesn't have capture capability */
  2284. if (!fe->cpu_dai->driver->capture.channels_min ||
  2285. !fe->codec_dai->driver->capture.channels_min)
  2286. return 0;
  2287. /* skip if FE isn't currently capturing */
  2288. if (!fe->cpu_dai->capture_active || !fe->codec_dai->capture_active)
  2289. return 0;
  2290. paths = dpcm_path_get(fe, SNDRV_PCM_STREAM_CAPTURE, &list);
  2291. if (paths < 0) {
  2292. dev_warn(fe->dev, "ASoC: %s no valid %s path\n",
  2293. fe->dai_link->name, "capture");
  2294. return paths;
  2295. }
  2296. /* update any old capture paths */
  2297. count = dpcm_process_paths(fe, SNDRV_PCM_STREAM_CAPTURE, &list, new);
  2298. if (count) {
  2299. if (new)
  2300. dpcm_run_new_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  2301. else
  2302. dpcm_run_old_update(fe, SNDRV_PCM_STREAM_CAPTURE);
  2303. dpcm_clear_pending_state(fe, SNDRV_PCM_STREAM_CAPTURE);
  2304. dpcm_be_disconnect(fe, SNDRV_PCM_STREAM_CAPTURE);
  2305. }
  2306. dpcm_path_put(&list);
  2307. return 0;
  2308. }
  2309. /* Called by DAPM mixer/mux changes to update audio routing between PCMs and
  2310. * any DAI links.
  2311. */
  2312. int soc_dpcm_runtime_update(struct snd_soc_card *card)
  2313. {
  2314. struct snd_soc_pcm_runtime *fe;
  2315. int ret = 0;
  2316. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  2317. /* shutdown all old paths first */
  2318. list_for_each_entry(fe, &card->rtd_list, list) {
  2319. ret = soc_dpcm_fe_runtime_update(fe, 0);
  2320. if (ret)
  2321. goto out;
  2322. }
  2323. /* bring new paths up */
  2324. list_for_each_entry(fe, &card->rtd_list, list) {
  2325. ret = soc_dpcm_fe_runtime_update(fe, 1);
  2326. if (ret)
  2327. goto out;
  2328. }
  2329. out:
  2330. mutex_unlock(&card->mutex);
  2331. return ret;
  2332. }
  2333. int soc_dpcm_be_digital_mute(struct snd_soc_pcm_runtime *fe, int mute)
  2334. {
  2335. struct snd_soc_dpcm *dpcm;
  2336. struct list_head *clients =
  2337. &fe->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients;
  2338. list_for_each_entry(dpcm, clients, list_be) {
  2339. struct snd_soc_pcm_runtime *be = dpcm->be;
  2340. int i;
  2341. if (be->dai_link->ignore_suspend)
  2342. continue;
  2343. for (i = 0; i < be->num_codecs; i++) {
  2344. struct snd_soc_dai *dai = be->codec_dais[i];
  2345. struct snd_soc_dai_driver *drv = dai->driver;
  2346. dev_dbg(be->dev, "ASoC: BE digital mute %s\n",
  2347. be->dai_link->name);
  2348. if (drv->ops && drv->ops->digital_mute &&
  2349. dai->playback_active)
  2350. drv->ops->digital_mute(dai, mute);
  2351. }
  2352. }
  2353. return 0;
  2354. }
  2355. static int dpcm_fe_dai_open(struct snd_pcm_substream *fe_substream)
  2356. {
  2357. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  2358. struct snd_soc_dpcm *dpcm;
  2359. struct snd_soc_dapm_widget_list *list;
  2360. int ret;
  2361. int stream = fe_substream->stream;
  2362. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  2363. fe->dpcm[stream].runtime = fe_substream->runtime;
  2364. ret = dpcm_path_get(fe, stream, &list);
  2365. if (ret < 0) {
  2366. mutex_unlock(&fe->card->mutex);
  2367. return ret;
  2368. } else if (ret == 0) {
  2369. dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
  2370. fe->dai_link->name, stream ? "capture" : "playback");
  2371. }
  2372. /* calculate valid and active FE <-> BE dpcms */
  2373. dpcm_process_paths(fe, stream, &list, 1);
  2374. ret = dpcm_fe_dai_startup(fe_substream);
  2375. if (ret < 0) {
  2376. /* clean up all links */
  2377. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  2378. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  2379. dpcm_be_disconnect(fe, stream);
  2380. fe->dpcm[stream].runtime = NULL;
  2381. }
  2382. dpcm_clear_pending_state(fe, stream);
  2383. dpcm_path_put(&list);
  2384. mutex_unlock(&fe->card->mutex);
  2385. return ret;
  2386. }
  2387. static int dpcm_fe_dai_close(struct snd_pcm_substream *fe_substream)
  2388. {
  2389. struct snd_soc_pcm_runtime *fe = fe_substream->private_data;
  2390. struct snd_soc_dpcm *dpcm;
  2391. int stream = fe_substream->stream, ret;
  2392. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  2393. ret = dpcm_fe_dai_shutdown(fe_substream);
  2394. /* mark FE's links ready to prune */
  2395. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  2396. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  2397. dpcm_be_disconnect(fe, stream);
  2398. fe->dpcm[stream].runtime = NULL;
  2399. mutex_unlock(&fe->card->mutex);
  2400. return ret;
  2401. }
  2402. static void soc_pcm_private_free(struct snd_pcm *pcm)
  2403. {
  2404. struct snd_soc_pcm_runtime *rtd = pcm->private_data;
  2405. struct snd_soc_rtdcom_list *rtdcom;
  2406. struct snd_soc_component *component;
  2407. /* need to sync the delayed work before releasing resources */
  2408. flush_delayed_work(&rtd->delayed_work);
  2409. for_each_rtdcom(rtd, rtdcom) {
  2410. component = rtdcom->component;
  2411. if (component->driver->pcm_free)
  2412. component->driver->pcm_free(pcm);
  2413. }
  2414. }
  2415. static int soc_rtdcom_ack(struct snd_pcm_substream *substream)
  2416. {
  2417. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2418. struct snd_soc_rtdcom_list *rtdcom;
  2419. struct snd_soc_component *component;
  2420. for_each_rtdcom(rtd, rtdcom) {
  2421. component = rtdcom->component;
  2422. if (!component->driver->ops ||
  2423. !component->driver->ops->ack)
  2424. continue;
  2425. /* FIXME. it returns 1st ask now */
  2426. return component->driver->ops->ack(substream);
  2427. }
  2428. return -EINVAL;
  2429. }
  2430. static int soc_rtdcom_copy_user(struct snd_pcm_substream *substream, int channel,
  2431. unsigned long pos, void __user *buf,
  2432. unsigned long bytes)
  2433. {
  2434. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2435. struct snd_soc_rtdcom_list *rtdcom;
  2436. struct snd_soc_component *component;
  2437. for_each_rtdcom(rtd, rtdcom) {
  2438. component = rtdcom->component;
  2439. if (!component->driver->ops ||
  2440. !component->driver->ops->copy_user)
  2441. continue;
  2442. /* FIXME. it returns 1st copy now */
  2443. return component->driver->ops->copy_user(substream, channel,
  2444. pos, buf, bytes);
  2445. }
  2446. return -EINVAL;
  2447. }
  2448. static int soc_rtdcom_copy_kernel(struct snd_pcm_substream *substream, int channel,
  2449. unsigned long pos, void *buf, unsigned long bytes)
  2450. {
  2451. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2452. struct snd_soc_rtdcom_list *rtdcom;
  2453. struct snd_soc_component *component;
  2454. for_each_rtdcom(rtd, rtdcom) {
  2455. component = rtdcom->component;
  2456. if (!component->driver->ops ||
  2457. !component->driver->ops->copy_kernel)
  2458. continue;
  2459. /* FIXME. it returns 1st copy now */
  2460. return component->driver->ops->copy_kernel(substream, channel,
  2461. pos, buf, bytes);
  2462. }
  2463. return -EINVAL;
  2464. }
  2465. static int soc_rtdcom_fill_silence(struct snd_pcm_substream *substream, int channel,
  2466. unsigned long pos, unsigned long bytes)
  2467. {
  2468. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2469. struct snd_soc_rtdcom_list *rtdcom;
  2470. struct snd_soc_component *component;
  2471. for_each_rtdcom(rtd, rtdcom) {
  2472. component = rtdcom->component;
  2473. if (!component->driver->ops ||
  2474. !component->driver->ops->fill_silence)
  2475. continue;
  2476. /* FIXME. it returns 1st silence now */
  2477. return component->driver->ops->fill_silence(substream, channel,
  2478. pos, bytes);
  2479. }
  2480. return -EINVAL;
  2481. }
  2482. static struct page *soc_rtdcom_page(struct snd_pcm_substream *substream,
  2483. unsigned long offset)
  2484. {
  2485. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2486. struct snd_soc_rtdcom_list *rtdcom;
  2487. struct snd_soc_component *component;
  2488. struct page *page;
  2489. for_each_rtdcom(rtd, rtdcom) {
  2490. component = rtdcom->component;
  2491. if (!component->driver->ops ||
  2492. !component->driver->ops->page)
  2493. continue;
  2494. /* FIXME. it returns 1st page now */
  2495. page = component->driver->ops->page(substream, offset);
  2496. if (page)
  2497. return page;
  2498. }
  2499. return NULL;
  2500. }
  2501. static int soc_rtdcom_mmap(struct snd_pcm_substream *substream,
  2502. struct vm_area_struct *vma)
  2503. {
  2504. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2505. struct snd_soc_rtdcom_list *rtdcom;
  2506. struct snd_soc_component *component;
  2507. for_each_rtdcom(rtd, rtdcom) {
  2508. component = rtdcom->component;
  2509. if (!component->driver->ops ||
  2510. !component->driver->ops->mmap)
  2511. continue;
  2512. /* FIXME. it returns 1st mmap now */
  2513. return component->driver->ops->mmap(substream, vma);
  2514. }
  2515. return -EINVAL;
  2516. }
  2517. /* create a new pcm */
  2518. int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
  2519. {
  2520. struct snd_soc_dai *codec_dai;
  2521. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  2522. struct snd_soc_component *component;
  2523. struct snd_soc_rtdcom_list *rtdcom;
  2524. struct snd_pcm *pcm;
  2525. char new_name[64];
  2526. int ret = 0, playback = 0, capture = 0;
  2527. int i;
  2528. if (rtd->dai_link->dynamic || rtd->dai_link->no_pcm) {
  2529. playback = rtd->dai_link->dpcm_playback;
  2530. capture = rtd->dai_link->dpcm_capture;
  2531. } else {
  2532. for (i = 0; i < rtd->num_codecs; i++) {
  2533. codec_dai = rtd->codec_dais[i];
  2534. if (codec_dai->driver->playback.channels_min)
  2535. playback = 1;
  2536. if (codec_dai->driver->capture.channels_min)
  2537. capture = 1;
  2538. }
  2539. capture = capture && cpu_dai->driver->capture.channels_min;
  2540. playback = playback && cpu_dai->driver->playback.channels_min;
  2541. }
  2542. if (rtd->dai_link->playback_only) {
  2543. playback = 1;
  2544. capture = 0;
  2545. }
  2546. if (rtd->dai_link->capture_only) {
  2547. playback = 0;
  2548. capture = 1;
  2549. }
  2550. /* create the PCM */
  2551. if (rtd->dai_link->no_pcm) {
  2552. snprintf(new_name, sizeof(new_name), "(%s)",
  2553. rtd->dai_link->stream_name);
  2554. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  2555. playback, capture, &pcm);
  2556. } else {
  2557. if (rtd->dai_link->dynamic)
  2558. snprintf(new_name, sizeof(new_name), "%s (*)",
  2559. rtd->dai_link->stream_name);
  2560. else
  2561. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  2562. rtd->dai_link->stream_name,
  2563. (rtd->num_codecs > 1) ?
  2564. "multicodec" : rtd->codec_dai->name, num);
  2565. ret = snd_pcm_new(rtd->card->snd_card, new_name, num, playback,
  2566. capture, &pcm);
  2567. }
  2568. if (ret < 0) {
  2569. dev_err(rtd->card->dev, "ASoC: can't create pcm for %s\n",
  2570. rtd->dai_link->name);
  2571. return ret;
  2572. }
  2573. dev_dbg(rtd->card->dev, "ASoC: registered pcm #%d %s\n",num, new_name);
  2574. /* DAPM dai link stream work */
  2575. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  2576. pcm->nonatomic = rtd->dai_link->nonatomic;
  2577. rtd->pcm = pcm;
  2578. pcm->private_data = rtd;
  2579. if (rtd->dai_link->no_pcm) {
  2580. if (playback)
  2581. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  2582. if (capture)
  2583. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  2584. goto out;
  2585. }
  2586. /* ASoC PCM operations */
  2587. if (rtd->dai_link->dynamic) {
  2588. rtd->ops.open = dpcm_fe_dai_open;
  2589. rtd->ops.hw_params = dpcm_fe_dai_hw_params;
  2590. rtd->ops.prepare = dpcm_fe_dai_prepare;
  2591. rtd->ops.trigger = dpcm_fe_dai_trigger;
  2592. rtd->ops.hw_free = dpcm_fe_dai_hw_free;
  2593. rtd->ops.close = dpcm_fe_dai_close;
  2594. rtd->ops.pointer = soc_pcm_pointer;
  2595. rtd->ops.ioctl = soc_pcm_ioctl;
  2596. } else {
  2597. rtd->ops.open = soc_pcm_open;
  2598. rtd->ops.hw_params = soc_pcm_hw_params;
  2599. rtd->ops.prepare = soc_pcm_prepare;
  2600. rtd->ops.trigger = soc_pcm_trigger;
  2601. rtd->ops.hw_free = soc_pcm_hw_free;
  2602. rtd->ops.close = soc_pcm_close;
  2603. rtd->ops.pointer = soc_pcm_pointer;
  2604. rtd->ops.ioctl = soc_pcm_ioctl;
  2605. }
  2606. for_each_rtdcom(rtd, rtdcom) {
  2607. const struct snd_pcm_ops *ops = rtdcom->component->driver->ops;
  2608. if (!ops)
  2609. continue;
  2610. if (ops->ack)
  2611. rtd->ops.ack = soc_rtdcom_ack;
  2612. if (ops->copy_user)
  2613. rtd->ops.copy_user = soc_rtdcom_copy_user;
  2614. if (ops->copy_kernel)
  2615. rtd->ops.copy_kernel = soc_rtdcom_copy_kernel;
  2616. if (ops->fill_silence)
  2617. rtd->ops.fill_silence = soc_rtdcom_fill_silence;
  2618. if (ops->page)
  2619. rtd->ops.page = soc_rtdcom_page;
  2620. if (ops->mmap)
  2621. rtd->ops.mmap = soc_rtdcom_mmap;
  2622. }
  2623. if (playback)
  2624. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &rtd->ops);
  2625. if (capture)
  2626. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &rtd->ops);
  2627. for_each_rtdcom(rtd, rtdcom) {
  2628. component = rtdcom->component;
  2629. if (!component->driver->pcm_new)
  2630. continue;
  2631. ret = component->driver->pcm_new(rtd);
  2632. if (ret < 0) {
  2633. dev_err(component->dev,
  2634. "ASoC: pcm constructor failed: %d\n",
  2635. ret);
  2636. return ret;
  2637. }
  2638. }
  2639. pcm->private_free = soc_pcm_private_free;
  2640. out:
  2641. dev_info(rtd->card->dev, "%s <-> %s mapping ok\n",
  2642. (rtd->num_codecs > 1) ? "multicodec" : rtd->codec_dai->name,
  2643. cpu_dai->name);
  2644. return ret;
  2645. }
  2646. /* is the current PCM operation for this FE ? */
  2647. int snd_soc_dpcm_fe_can_update(struct snd_soc_pcm_runtime *fe, int stream)
  2648. {
  2649. if (fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE)
  2650. return 1;
  2651. return 0;
  2652. }
  2653. EXPORT_SYMBOL_GPL(snd_soc_dpcm_fe_can_update);
  2654. /* is the current PCM operation for this BE ? */
  2655. int snd_soc_dpcm_be_can_update(struct snd_soc_pcm_runtime *fe,
  2656. struct snd_soc_pcm_runtime *be, int stream)
  2657. {
  2658. if ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_FE) ||
  2659. ((fe->dpcm[stream].runtime_update == SND_SOC_DPCM_UPDATE_BE) &&
  2660. be->dpcm[stream].runtime_update))
  2661. return 1;
  2662. return 0;
  2663. }
  2664. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_can_update);
  2665. /* get the substream for this BE */
  2666. struct snd_pcm_substream *
  2667. snd_soc_dpcm_get_substream(struct snd_soc_pcm_runtime *be, int stream)
  2668. {
  2669. return be->pcm->streams[stream].substream;
  2670. }
  2671. EXPORT_SYMBOL_GPL(snd_soc_dpcm_get_substream);
  2672. /* get the BE runtime state */
  2673. enum snd_soc_dpcm_state
  2674. snd_soc_dpcm_be_get_state(struct snd_soc_pcm_runtime *be, int stream)
  2675. {
  2676. return be->dpcm[stream].state;
  2677. }
  2678. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_get_state);
  2679. /* set the BE runtime state */
  2680. void snd_soc_dpcm_be_set_state(struct snd_soc_pcm_runtime *be,
  2681. int stream, enum snd_soc_dpcm_state state)
  2682. {
  2683. be->dpcm[stream].state = state;
  2684. }
  2685. EXPORT_SYMBOL_GPL(snd_soc_dpcm_be_set_state);
  2686. /*
  2687. * We can only hw_free, stop, pause or suspend a BE DAI if any of it's FE
  2688. * are not running, paused or suspended for the specified stream direction.
  2689. */
  2690. int snd_soc_dpcm_can_be_free_stop(struct snd_soc_pcm_runtime *fe,
  2691. struct snd_soc_pcm_runtime *be, int stream)
  2692. {
  2693. struct snd_soc_dpcm *dpcm;
  2694. int state;
  2695. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  2696. if (dpcm->fe == fe)
  2697. continue;
  2698. state = dpcm->fe->dpcm[stream].state;
  2699. if (state == SND_SOC_DPCM_STATE_START ||
  2700. state == SND_SOC_DPCM_STATE_PAUSED ||
  2701. state == SND_SOC_DPCM_STATE_SUSPEND)
  2702. return 0;
  2703. }
  2704. /* it's safe to free/stop this BE DAI */
  2705. return 1;
  2706. }
  2707. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_free_stop);
  2708. /*
  2709. * We can only change hw params a BE DAI if any of it's FE are not prepared,
  2710. * running, paused or suspended for the specified stream direction.
  2711. */
  2712. int snd_soc_dpcm_can_be_params(struct snd_soc_pcm_runtime *fe,
  2713. struct snd_soc_pcm_runtime *be, int stream)
  2714. {
  2715. struct snd_soc_dpcm *dpcm;
  2716. int state;
  2717. list_for_each_entry(dpcm, &be->dpcm[stream].fe_clients, list_fe) {
  2718. if (dpcm->fe == fe)
  2719. continue;
  2720. state = dpcm->fe->dpcm[stream].state;
  2721. if (state == SND_SOC_DPCM_STATE_START ||
  2722. state == SND_SOC_DPCM_STATE_PAUSED ||
  2723. state == SND_SOC_DPCM_STATE_SUSPEND ||
  2724. state == SND_SOC_DPCM_STATE_PREPARE)
  2725. return 0;
  2726. }
  2727. /* it's safe to change hw_params */
  2728. return 1;
  2729. }
  2730. EXPORT_SYMBOL_GPL(snd_soc_dpcm_can_be_params);
  2731. #ifdef CONFIG_DEBUG_FS
  2732. static const char *dpcm_state_string(enum snd_soc_dpcm_state state)
  2733. {
  2734. switch (state) {
  2735. case SND_SOC_DPCM_STATE_NEW:
  2736. return "new";
  2737. case SND_SOC_DPCM_STATE_OPEN:
  2738. return "open";
  2739. case SND_SOC_DPCM_STATE_HW_PARAMS:
  2740. return "hw_params";
  2741. case SND_SOC_DPCM_STATE_PREPARE:
  2742. return "prepare";
  2743. case SND_SOC_DPCM_STATE_START:
  2744. return "start";
  2745. case SND_SOC_DPCM_STATE_STOP:
  2746. return "stop";
  2747. case SND_SOC_DPCM_STATE_SUSPEND:
  2748. return "suspend";
  2749. case SND_SOC_DPCM_STATE_PAUSED:
  2750. return "paused";
  2751. case SND_SOC_DPCM_STATE_HW_FREE:
  2752. return "hw_free";
  2753. case SND_SOC_DPCM_STATE_CLOSE:
  2754. return "close";
  2755. }
  2756. return "unknown";
  2757. }
  2758. static ssize_t dpcm_show_state(struct snd_soc_pcm_runtime *fe,
  2759. int stream, char *buf, size_t size)
  2760. {
  2761. struct snd_pcm_hw_params *params = &fe->dpcm[stream].hw_params;
  2762. struct snd_soc_dpcm *dpcm;
  2763. ssize_t offset = 0;
  2764. /* FE state */
  2765. offset += snprintf(buf + offset, size - offset,
  2766. "[%s - %s]\n", fe->dai_link->name,
  2767. stream ? "Capture" : "Playback");
  2768. offset += snprintf(buf + offset, size - offset, "State: %s\n",
  2769. dpcm_state_string(fe->dpcm[stream].state));
  2770. if ((fe->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  2771. (fe->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  2772. offset += snprintf(buf + offset, size - offset,
  2773. "Hardware Params: "
  2774. "Format = %s, Channels = %d, Rate = %d\n",
  2775. snd_pcm_format_name(params_format(params)),
  2776. params_channels(params),
  2777. params_rate(params));
  2778. /* BEs state */
  2779. offset += snprintf(buf + offset, size - offset, "Backends:\n");
  2780. if (list_empty(&fe->dpcm[stream].be_clients)) {
  2781. offset += snprintf(buf + offset, size - offset,
  2782. " No active DSP links\n");
  2783. goto out;
  2784. }
  2785. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
  2786. struct snd_soc_pcm_runtime *be = dpcm->be;
  2787. params = &dpcm->hw_params;
  2788. offset += snprintf(buf + offset, size - offset,
  2789. "- %s\n", be->dai_link->name);
  2790. offset += snprintf(buf + offset, size - offset,
  2791. " State: %s\n",
  2792. dpcm_state_string(be->dpcm[stream].state));
  2793. if ((be->dpcm[stream].state >= SND_SOC_DPCM_STATE_HW_PARAMS) &&
  2794. (be->dpcm[stream].state <= SND_SOC_DPCM_STATE_STOP))
  2795. offset += snprintf(buf + offset, size - offset,
  2796. " Hardware Params: "
  2797. "Format = %s, Channels = %d, Rate = %d\n",
  2798. snd_pcm_format_name(params_format(params)),
  2799. params_channels(params),
  2800. params_rate(params));
  2801. }
  2802. out:
  2803. return offset;
  2804. }
  2805. static ssize_t dpcm_state_read_file(struct file *file, char __user *user_buf,
  2806. size_t count, loff_t *ppos)
  2807. {
  2808. struct snd_soc_pcm_runtime *fe = file->private_data;
  2809. ssize_t out_count = PAGE_SIZE, offset = 0, ret = 0;
  2810. char *buf;
  2811. buf = kmalloc(out_count, GFP_KERNEL);
  2812. if (!buf)
  2813. return -ENOMEM;
  2814. if (fe->cpu_dai->driver->playback.channels_min)
  2815. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_PLAYBACK,
  2816. buf + offset, out_count - offset);
  2817. if (fe->cpu_dai->driver->capture.channels_min)
  2818. offset += dpcm_show_state(fe, SNDRV_PCM_STREAM_CAPTURE,
  2819. buf + offset, out_count - offset);
  2820. ret = simple_read_from_buffer(user_buf, count, ppos, buf, offset);
  2821. kfree(buf);
  2822. return ret;
  2823. }
  2824. static const struct file_operations dpcm_state_fops = {
  2825. .open = simple_open,
  2826. .read = dpcm_state_read_file,
  2827. .llseek = default_llseek,
  2828. };
  2829. void soc_dpcm_debugfs_add(struct snd_soc_pcm_runtime *rtd)
  2830. {
  2831. if (!rtd->dai_link)
  2832. return;
  2833. if (!rtd->card->debugfs_card_root)
  2834. return;
  2835. rtd->debugfs_dpcm_root = debugfs_create_dir(rtd->dai_link->name,
  2836. rtd->card->debugfs_card_root);
  2837. if (!rtd->debugfs_dpcm_root) {
  2838. dev_dbg(rtd->dev,
  2839. "ASoC: Failed to create dpcm debugfs directory %s\n",
  2840. rtd->dai_link->name);
  2841. return;
  2842. }
  2843. debugfs_create_file("state", 0444, rtd->debugfs_dpcm_root,
  2844. rtd, &dpcm_state_fops);
  2845. }
  2846. #endif