soc-pcm.c 90 KB

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