soc-pcm.c 78 KB

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