soc-pcm.c 80 KB

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