soc-pcm.c 93 KB

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