soc-pcm.c 82 KB

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