soc-pcm.c 79 KB

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