soc-pcm.c 71 KB

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