soc-pcm.c 79 KB

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