soc-pcm.c 80 KB

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