soc-pcm.c 83 KB

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