soc-pcm.c 84 KB

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