soc-core.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. /*
  2. * soc-core.c -- ALSA SoC Audio Layer
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. *
  7. * Author: Liam Girdwood
  8. * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
  9. * with code, comments and ideas from :-
  10. * Richard Purdie <richard@openedhand.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. * Revision history
  18. * 12th Aug 2005 Initial version.
  19. * 25th Oct 2005 Working Codec, Interface and Platform registration.
  20. *
  21. * TODO:
  22. * o Add hw rules to enforce rates, etc.
  23. * o More testing with other codecs/machines.
  24. * o Add more codecs and platforms to ensure good API coverage.
  25. * o Support TDM on PCM and I2S
  26. */
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/init.h>
  30. #include <linux/delay.h>
  31. #include <linux/pm.h>
  32. #include <linux/bitops.h>
  33. #include <linux/platform_device.h>
  34. #include <sound/driver.h>
  35. #include <sound/core.h>
  36. #include <sound/pcm.h>
  37. #include <sound/pcm_params.h>
  38. #include <sound/soc.h>
  39. #include <sound/soc-dapm.h>
  40. #include <sound/initval.h>
  41. /* debug */
  42. #define SOC_DEBUG 0
  43. #if SOC_DEBUG
  44. #define dbg(format, arg...) printk(format, ## arg)
  45. #else
  46. #define dbg(format, arg...)
  47. #endif
  48. /* debug DAI capabilities matching */
  49. #define SOC_DEBUG_DAI 0
  50. #if SOC_DEBUG_DAI
  51. #define dbgc(format, arg...) printk(format, ## arg)
  52. #else
  53. #define dbgc(format, arg...)
  54. #endif
  55. #define CODEC_CPU(codec, cpu) ((codec << 4) | cpu)
  56. static DEFINE_MUTEX(pcm_mutex);
  57. static DEFINE_MUTEX(io_mutex);
  58. static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  59. /* supported sample rates */
  60. /* ATTENTION: these values depend on the definition in pcm.h! */
  61. static const unsigned int rates[] = {
  62. 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  63. 48000, 64000, 88200, 96000, 176400, 192000
  64. };
  65. /*
  66. * This is a timeout to do a DAPM powerdown after a stream is closed().
  67. * It can be used to eliminate pops between different playback streams, e.g.
  68. * between two audio tracks.
  69. */
  70. static int pmdown_time = 5000;
  71. module_param(pmdown_time, int, 0);
  72. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  73. /*
  74. * This function forces any delayed work to be queued and run.
  75. */
  76. static int run_delayed_work(struct delayed_work *dwork)
  77. {
  78. int ret;
  79. /* cancel any work waiting to be queued. */
  80. ret = cancel_delayed_work(dwork);
  81. /* if there was any work waiting then we run it now and
  82. * wait for it's completion */
  83. if (ret) {
  84. schedule_delayed_work(dwork, 0);
  85. flush_scheduled_work();
  86. }
  87. return ret;
  88. }
  89. #ifdef CONFIG_SND_SOC_AC97_BUS
  90. /* unregister ac97 codec */
  91. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  92. {
  93. if (codec->ac97->dev.bus)
  94. device_unregister(&codec->ac97->dev);
  95. return 0;
  96. }
  97. /* stop no dev release warning */
  98. static void soc_ac97_device_release(struct device *dev){}
  99. /* register ac97 codec to bus */
  100. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  101. {
  102. int err;
  103. codec->ac97->dev.bus = &ac97_bus_type;
  104. codec->ac97->dev.parent = NULL;
  105. codec->ac97->dev.release = soc_ac97_device_release;
  106. snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
  107. codec->card->number, 0, codec->name);
  108. err = device_register(&codec->ac97->dev);
  109. if (err < 0) {
  110. snd_printk(KERN_ERR "Can't register ac97 bus\n");
  111. codec->ac97->dev.bus = NULL;
  112. return err;
  113. }
  114. return 0;
  115. }
  116. #endif
  117. static inline const char* get_dai_name(int type)
  118. {
  119. switch(type) {
  120. case SND_SOC_DAI_AC97:
  121. return "AC97";
  122. case SND_SOC_DAI_I2S:
  123. return "I2S";
  124. case SND_SOC_DAI_PCM:
  125. return "PCM";
  126. }
  127. return NULL;
  128. }
  129. /* get rate format from rate */
  130. static inline int soc_get_rate_format(int rate)
  131. {
  132. int i;
  133. for (i = 0; i < ARRAY_SIZE(rates); i++) {
  134. if (rates[i] == rate)
  135. return 1 << i;
  136. }
  137. return 0;
  138. }
  139. /* gets the audio system mclk/sysclk for the given parameters */
  140. static unsigned inline int soc_get_mclk(struct snd_soc_pcm_runtime *rtd,
  141. struct snd_soc_clock_info *info)
  142. {
  143. struct snd_soc_device *socdev = rtd->socdev;
  144. struct snd_soc_machine *machine = socdev->machine;
  145. int i;
  146. /* find the matching machine config and get it's mclk for the given
  147. * sample rate and hardware format */
  148. for(i = 0; i < machine->num_links; i++) {
  149. if (machine->dai_link[i].cpu_dai == rtd->cpu_dai &&
  150. machine->dai_link[i].config_sysclk)
  151. return machine->dai_link[i].config_sysclk(rtd, info);
  152. }
  153. return 0;
  154. }
  155. /* changes a bitclk multiplier mask to a divider mask */
  156. static u64 soc_bfs_rcw_to_div(u64 bfs, int rate, unsigned int mclk,
  157. unsigned int pcmfmt, unsigned int chn)
  158. {
  159. int i, j;
  160. u64 bfs_ = 0;
  161. int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
  162. if (size <= 0)
  163. return 0;
  164. /* the minimum bit clock that has enough bandwidth */
  165. min = size * rate * chn;
  166. dbgc("rcw --> div min bclk %d with mclk %d\n", min, mclk);
  167. for (i = 0; i < 64; i++) {
  168. if ((bfs >> i) & 0x1) {
  169. j = min * (i + 1);
  170. bfs_ |= SND_SOC_FSBD(mclk/j);
  171. dbgc("rcw --> div support mult %d\n",
  172. SND_SOC_FSBD_REAL(1<<i));
  173. }
  174. }
  175. return bfs_;
  176. }
  177. /* changes a bitclk divider mask to a multiplier mask */
  178. static u64 soc_bfs_div_to_rcw(u64 bfs, int rate, unsigned int mclk,
  179. unsigned int pcmfmt, unsigned int chn)
  180. {
  181. int i, j;
  182. u64 bfs_ = 0;
  183. int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
  184. if (size <= 0)
  185. return 0;
  186. /* the minimum bit clock that has enough bandwidth */
  187. min = size * rate * chn;
  188. dbgc("div to rcw min bclk %d with mclk %d\n", min, mclk);
  189. for (i = 0; i < 64; i++) {
  190. if ((bfs >> i) & 0x1) {
  191. j = mclk / (i + 1);
  192. if (j >= min) {
  193. bfs_ |= SND_SOC_FSBW(j/min);
  194. dbgc("div --> rcw support div %d\n",
  195. SND_SOC_FSBW_REAL(1<<i));
  196. }
  197. }
  198. }
  199. return bfs_;
  200. }
  201. /* changes a constant bitclk to a multiplier mask */
  202. static u64 soc_bfs_rate_to_rcw(u64 bfs, int rate, unsigned int mclk,
  203. unsigned int pcmfmt, unsigned int chn)
  204. {
  205. unsigned int bfs_ = rate * bfs;
  206. int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
  207. if (size <= 0)
  208. return 0;
  209. /* the minimum bit clock that has enough bandwidth */
  210. min = size * rate * chn;
  211. dbgc("rate --> rcw min bclk %d with mclk %d\n", min, mclk);
  212. if (bfs_ < min)
  213. return 0;
  214. else {
  215. bfs_ = SND_SOC_FSBW(bfs_/min);
  216. dbgc("rate --> rcw support div %d\n", SND_SOC_FSBW_REAL(bfs_));
  217. return bfs_;
  218. }
  219. }
  220. /* changes a bitclk multiplier mask to a divider mask */
  221. static u64 soc_bfs_rate_to_div(u64 bfs, int rate, unsigned int mclk,
  222. unsigned int pcmfmt, unsigned int chn)
  223. {
  224. unsigned int bfs_ = rate * bfs;
  225. int size = snd_pcm_format_physical_width(pcmfmt), min = 0;
  226. if (size <= 0)
  227. return 0;
  228. /* the minimum bit clock that has enough bandwidth */
  229. min = size * rate * chn;
  230. dbgc("rate --> div min bclk %d with mclk %d\n", min, mclk);
  231. if (bfs_ < min)
  232. return 0;
  233. else {
  234. bfs_ = SND_SOC_FSBW(mclk/bfs_);
  235. dbgc("rate --> div support div %d\n", SND_SOC_FSBD_REAL(bfs_));
  236. return bfs_;
  237. }
  238. }
  239. /* Matches codec DAI and SoC CPU DAI hardware parameters */
  240. static int soc_hw_match_params(struct snd_pcm_substream *substream,
  241. struct snd_pcm_hw_params *params)
  242. {
  243. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  244. struct snd_soc_dai_mode *codec_dai_mode = NULL;
  245. struct snd_soc_dai_mode *cpu_dai_mode = NULL;
  246. struct snd_soc_clock_info clk_info;
  247. unsigned int fs, mclk, rate = params_rate(params),
  248. chn, j, k, cpu_bclk, codec_bclk, pcmrate;
  249. u16 fmt = 0;
  250. u64 codec_bfs, cpu_bfs;
  251. dbg("asoc: match version %s\n", SND_SOC_VERSION);
  252. clk_info.rate = rate;
  253. pcmrate = soc_get_rate_format(rate);
  254. /* try and find a match from the codec and cpu DAI capabilities */
  255. for (j = 0; j < rtd->codec_dai->caps.num_modes; j++) {
  256. for (k = 0; k < rtd->cpu_dai->caps.num_modes; k++) {
  257. codec_dai_mode = &rtd->codec_dai->caps.mode[j];
  258. cpu_dai_mode = &rtd->cpu_dai->caps.mode[k];
  259. if (!(codec_dai_mode->pcmrate & cpu_dai_mode->pcmrate &
  260. pcmrate)) {
  261. dbgc("asoc: DAI[%d:%d] failed to match rate\n", j, k);
  262. continue;
  263. }
  264. fmt = codec_dai_mode->fmt & cpu_dai_mode->fmt;
  265. if (!(fmt & SND_SOC_DAIFMT_FORMAT_MASK)) {
  266. dbgc("asoc: DAI[%d:%d] failed to match format\n", j, k);
  267. continue;
  268. }
  269. if (!(fmt & SND_SOC_DAIFMT_CLOCK_MASK)) {
  270. dbgc("asoc: DAI[%d:%d] failed to match clock masters\n",
  271. j, k);
  272. continue;
  273. }
  274. if (!(fmt & SND_SOC_DAIFMT_INV_MASK)) {
  275. dbgc("asoc: DAI[%d:%d] failed to match invert\n", j, k);
  276. continue;
  277. }
  278. if (!(codec_dai_mode->pcmfmt & cpu_dai_mode->pcmfmt)) {
  279. dbgc("asoc: DAI[%d:%d] failed to match pcm format\n", j, k);
  280. continue;
  281. }
  282. if (!(codec_dai_mode->pcmdir & cpu_dai_mode->pcmdir)) {
  283. dbgc("asoc: DAI[%d:%d] failed to match direction\n", j, k);
  284. continue;
  285. }
  286. /* todo - still need to add tdm selection */
  287. rtd->cpu_dai->dai_runtime.fmt =
  288. rtd->codec_dai->dai_runtime.fmt =
  289. 1 << (ffs(fmt & SND_SOC_DAIFMT_FORMAT_MASK) -1) |
  290. 1 << (ffs(fmt & SND_SOC_DAIFMT_CLOCK_MASK) - 1) |
  291. 1 << (ffs(fmt & SND_SOC_DAIFMT_INV_MASK) - 1);
  292. clk_info.bclk_master =
  293. rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK;
  294. /* make sure the ratio between rate and master
  295. * clock is acceptable*/
  296. fs = (cpu_dai_mode->fs & codec_dai_mode->fs);
  297. if (fs == 0) {
  298. dbgc("asoc: DAI[%d:%d] failed to match FS\n", j, k);
  299. continue;
  300. }
  301. clk_info.fs = rtd->cpu_dai->dai_runtime.fs =
  302. rtd->codec_dai->dai_runtime.fs = fs;
  303. /* calculate audio system clocking using slowest clocks possible*/
  304. mclk = soc_get_mclk(rtd, &clk_info);
  305. if (mclk == 0) {
  306. dbgc("asoc: DAI[%d:%d] configuration not clockable\n", j, k);
  307. dbgc("asoc: rate %d fs %d master %x\n", rate, fs,
  308. clk_info.bclk_master);
  309. continue;
  310. }
  311. /* calculate word size (per channel) and frame size */
  312. rtd->codec_dai->dai_runtime.pcmfmt =
  313. rtd->cpu_dai->dai_runtime.pcmfmt =
  314. 1 << params_format(params);
  315. chn = params_channels(params);
  316. /* i2s always has left and right */
  317. if (params_channels(params) == 1 &&
  318. rtd->cpu_dai->dai_runtime.fmt & (SND_SOC_DAIFMT_I2S |
  319. SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_LEFT_J))
  320. chn <<= 1;
  321. /* Calculate bfs - the ratio between bitclock and the sample rate
  322. * We must take into consideration the dividers and multipliers
  323. * used in the codec and cpu DAI modes. We always choose the
  324. * lowest possible clocks to reduce power.
  325. */
  326. switch (CODEC_CPU(codec_dai_mode->flags, cpu_dai_mode->flags)) {
  327. case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_DIV):
  328. /* cpu & codec bfs dividers */
  329. rtd->cpu_dai->dai_runtime.bfs =
  330. rtd->codec_dai->dai_runtime.bfs =
  331. 1 << (fls(codec_dai_mode->bfs & cpu_dai_mode->bfs) - 1);
  332. break;
  333. case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RCW):
  334. /* normalise bfs codec divider & cpu rcw mult */
  335. codec_bfs = soc_bfs_div_to_rcw(codec_dai_mode->bfs, rate,
  336. mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
  337. rtd->cpu_dai->dai_runtime.bfs =
  338. 1 << (ffs(codec_bfs & cpu_dai_mode->bfs) - 1);
  339. cpu_bfs = soc_bfs_rcw_to_div(cpu_dai_mode->bfs, rate, mclk,
  340. rtd->codec_dai->dai_runtime.pcmfmt, chn);
  341. rtd->codec_dai->dai_runtime.bfs =
  342. 1 << (fls(codec_dai_mode->bfs & cpu_bfs) - 1);
  343. break;
  344. case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_DIV):
  345. /* normalise bfs codec rcw mult & cpu divider */
  346. codec_bfs = soc_bfs_rcw_to_div(codec_dai_mode->bfs, rate,
  347. mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
  348. rtd->cpu_dai->dai_runtime.bfs =
  349. 1 << (fls(codec_bfs & cpu_dai_mode->bfs) -1);
  350. cpu_bfs = soc_bfs_div_to_rcw(cpu_dai_mode->bfs, rate, mclk,
  351. rtd->codec_dai->dai_runtime.pcmfmt, chn);
  352. rtd->codec_dai->dai_runtime.bfs =
  353. 1 << (ffs(codec_dai_mode->bfs & cpu_bfs) -1);
  354. break;
  355. case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RCW):
  356. /* codec & cpu bfs rate rcw multipliers */
  357. rtd->cpu_dai->dai_runtime.bfs =
  358. rtd->codec_dai->dai_runtime.bfs =
  359. 1 << (ffs(codec_dai_mode->bfs & cpu_dai_mode->bfs) -1);
  360. break;
  361. case CODEC_CPU(SND_SOC_DAI_BFS_DIV, SND_SOC_DAI_BFS_RATE):
  362. /* normalise cpu bfs rate const multiplier & codec div */
  363. cpu_bfs = soc_bfs_rate_to_div(cpu_dai_mode->bfs, rate,
  364. mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
  365. if(codec_dai_mode->bfs & cpu_bfs) {
  366. rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
  367. rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
  368. } else
  369. rtd->cpu_dai->dai_runtime.bfs = 0;
  370. break;
  371. case CODEC_CPU(SND_SOC_DAI_BFS_RCW, SND_SOC_DAI_BFS_RATE):
  372. /* normalise cpu bfs rate const multiplier & codec rcw mult */
  373. cpu_bfs = soc_bfs_rate_to_rcw(cpu_dai_mode->bfs, rate,
  374. mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
  375. if(codec_dai_mode->bfs & cpu_bfs) {
  376. rtd->codec_dai->dai_runtime.bfs = cpu_bfs;
  377. rtd->cpu_dai->dai_runtime.bfs = cpu_dai_mode->bfs;
  378. } else
  379. rtd->cpu_dai->dai_runtime.bfs = 0;
  380. break;
  381. case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RCW):
  382. /* normalise cpu bfs rate rcw multiplier & codec const mult */
  383. codec_bfs = soc_bfs_rate_to_rcw(codec_dai_mode->bfs, rate,
  384. mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
  385. if(cpu_dai_mode->bfs & codec_bfs) {
  386. rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
  387. rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
  388. } else
  389. rtd->cpu_dai->dai_runtime.bfs = 0;
  390. break;
  391. case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_DIV):
  392. /* normalise cpu bfs div & codec const mult */
  393. codec_bfs = soc_bfs_rate_to_div(codec_dai_mode->bfs, rate,
  394. mclk, rtd->codec_dai->dai_runtime.pcmfmt, chn);
  395. if(cpu_dai_mode->bfs & codec_bfs) {
  396. rtd->cpu_dai->dai_runtime.bfs = codec_bfs;
  397. rtd->codec_dai->dai_runtime.bfs = codec_dai_mode->bfs;
  398. } else
  399. rtd->cpu_dai->dai_runtime.bfs = 0;
  400. break;
  401. case CODEC_CPU(SND_SOC_DAI_BFS_RATE, SND_SOC_DAI_BFS_RATE):
  402. /* cpu & codec constant mult */
  403. if(codec_dai_mode->bfs == cpu_dai_mode->bfs)
  404. rtd->cpu_dai->dai_runtime.bfs =
  405. rtd->codec_dai->dai_runtime.bfs =
  406. codec_dai_mode->bfs;
  407. else
  408. rtd->cpu_dai->dai_runtime.bfs =
  409. rtd->codec_dai->dai_runtime.bfs = 0;
  410. break;
  411. }
  412. /* make sure the bit clock speed is acceptable */
  413. if (!rtd->cpu_dai->dai_runtime.bfs ||
  414. !rtd->codec_dai->dai_runtime.bfs) {
  415. dbgc("asoc: DAI[%d:%d] failed to match BFS\n", j, k);
  416. dbgc("asoc: cpu_dai %llu codec %llu\n",
  417. rtd->cpu_dai->dai_runtime.bfs,
  418. rtd->codec_dai->dai_runtime.bfs);
  419. dbgc("asoc: mclk %d hwfmt %x\n", mclk, fmt);
  420. continue;
  421. }
  422. goto found;
  423. }
  424. }
  425. printk(KERN_ERR "asoc: no matching DAI found between codec and CPU\n");
  426. return -EINVAL;
  427. found:
  428. /* we have matching DAI's, so complete the runtime info */
  429. rtd->codec_dai->dai_runtime.pcmrate =
  430. rtd->cpu_dai->dai_runtime.pcmrate =
  431. soc_get_rate_format(rate);
  432. rtd->codec_dai->dai_runtime.priv = codec_dai_mode->priv;
  433. rtd->cpu_dai->dai_runtime.priv = cpu_dai_mode->priv;
  434. rtd->codec_dai->dai_runtime.flags = codec_dai_mode->flags;
  435. rtd->cpu_dai->dai_runtime.flags = cpu_dai_mode->flags;
  436. /* for debug atm */
  437. dbg("asoc: DAI[%d:%d] Match OK\n", j, k);
  438. if (rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
  439. codec_bclk = (rtd->codec_dai->dai_runtime.fs * params_rate(params)) /
  440. SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
  441. dbg("asoc: codec fs %d mclk %d bfs div %d bclk %d\n",
  442. rtd->codec_dai->dai_runtime.fs, mclk,
  443. SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
  444. } else if(rtd->codec_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
  445. codec_bclk = params_rate(params) * rtd->codec_dai->dai_runtime.bfs;
  446. dbg("asoc: codec fs %d mclk %d bfs rate mult %llu bclk %d\n",
  447. rtd->codec_dai->dai_runtime.fs, mclk,
  448. rtd->codec_dai->dai_runtime.bfs, codec_bclk);
  449. } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
  450. codec_bclk = params_rate(params) * params_channels(params) *
  451. snd_pcm_format_physical_width(rtd->codec_dai->dai_runtime.pcmfmt) *
  452. SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs);
  453. dbg("asoc: codec fs %d mclk %d bfs rcw mult %d bclk %d\n",
  454. rtd->codec_dai->dai_runtime.fs, mclk,
  455. SND_SOC_FSBW_REAL(rtd->codec_dai->dai_runtime.bfs), codec_bclk);
  456. } else
  457. codec_bclk = 0;
  458. if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_DIV) {
  459. cpu_bclk = (rtd->cpu_dai->dai_runtime.fs * params_rate(params)) /
  460. SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs);
  461. dbg("asoc: cpu fs %d mclk %d bfs div %d bclk %d\n",
  462. rtd->cpu_dai->dai_runtime.fs, mclk,
  463. SND_SOC_FSBD_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
  464. } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RATE) {
  465. cpu_bclk = params_rate(params) * rtd->cpu_dai->dai_runtime.bfs;
  466. dbg("asoc: cpu fs %d mclk %d bfs rate mult %llu bclk %d\n",
  467. rtd->cpu_dai->dai_runtime.fs, mclk,
  468. rtd->cpu_dai->dai_runtime.bfs, cpu_bclk);
  469. } else if (rtd->cpu_dai->dai_runtime.flags == SND_SOC_DAI_BFS_RCW) {
  470. cpu_bclk = params_rate(params) * params_channels(params) *
  471. snd_pcm_format_physical_width(rtd->cpu_dai->dai_runtime.pcmfmt) *
  472. SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs);
  473. dbg("asoc: cpu fs %d mclk %d bfs mult rcw %d bclk %d\n",
  474. rtd->cpu_dai->dai_runtime.fs, mclk,
  475. SND_SOC_FSBW_REAL(rtd->cpu_dai->dai_runtime.bfs), cpu_bclk);
  476. } else
  477. cpu_bclk = 0;
  478. /*
  479. * Check we have matching bitclocks. If we don't then it means the
  480. * sysclock returned by either the codec or cpu DAI (selected by the
  481. * machine sysclock function) is wrong compared with the supported DAI
  482. * modes for the codec or cpu DAI. Check your codec or CPU DAI
  483. * config_sysclock() functions.
  484. */
  485. if (cpu_bclk != codec_bclk && cpu_bclk){
  486. printk(KERN_ERR
  487. "asoc: codec and cpu bitclocks differ, audio may be wrong speed\n"
  488. );
  489. printk(KERN_ERR "asoc: codec %d != cpu %d\n", codec_bclk, cpu_bclk);
  490. }
  491. switch(rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
  492. case SND_SOC_DAIFMT_CBM_CFM:
  493. dbg("asoc: DAI codec BCLK master, LRC master\n");
  494. break;
  495. case SND_SOC_DAIFMT_CBS_CFM:
  496. dbg("asoc: DAI codec BCLK slave, LRC master\n");
  497. break;
  498. case SND_SOC_DAIFMT_CBM_CFS:
  499. dbg("asoc: DAI codec BCLK master, LRC slave\n");
  500. break;
  501. case SND_SOC_DAIFMT_CBS_CFS:
  502. dbg("asoc: DAI codec BCLK slave, LRC slave\n");
  503. break;
  504. }
  505. dbg("asoc: mode %x, invert %x\n",
  506. rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK,
  507. rtd->cpu_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK);
  508. dbg("asoc: audio rate %d chn %d fmt %x\n", params_rate(params),
  509. params_channels(params), params_format(params));
  510. return 0;
  511. }
  512. static inline u32 get_rates(struct snd_soc_dai_mode *modes, int nmodes)
  513. {
  514. int i;
  515. u32 rates = 0;
  516. for(i = 0; i < nmodes; i++)
  517. rates |= modes[i].pcmrate;
  518. return rates;
  519. }
  520. static inline u64 get_formats(struct snd_soc_dai_mode *modes, int nmodes)
  521. {
  522. int i;
  523. u64 formats = 0;
  524. for(i = 0; i < nmodes; i++)
  525. formats |= modes[i].pcmfmt;
  526. return formats;
  527. }
  528. /*
  529. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  530. * then initialized and any private data can be allocated. This also calls
  531. * startup for the cpu DAI, platform, machine and codec DAI.
  532. */
  533. static int soc_pcm_open(struct snd_pcm_substream *substream)
  534. {
  535. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  536. struct snd_soc_device *socdev = rtd->socdev;
  537. struct snd_pcm_runtime *runtime = substream->runtime;
  538. struct snd_soc_machine *machine = socdev->machine;
  539. struct snd_soc_platform *platform = socdev->platform;
  540. struct snd_soc_codec_dai *codec_dai = rtd->codec_dai;
  541. struct snd_soc_cpu_dai *cpu_dai = rtd->cpu_dai;
  542. int ret = 0;
  543. mutex_lock(&pcm_mutex);
  544. /* startup the audio subsystem */
  545. if (rtd->cpu_dai->ops.startup) {
  546. ret = rtd->cpu_dai->ops.startup(substream);
  547. if (ret < 0) {
  548. printk(KERN_ERR "asoc: can't open interface %s\n",
  549. rtd->cpu_dai->name);
  550. goto out;
  551. }
  552. }
  553. if (platform->pcm_ops->open) {
  554. ret = platform->pcm_ops->open(substream);
  555. if (ret < 0) {
  556. printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
  557. goto platform_err;
  558. }
  559. }
  560. if (machine->ops && machine->ops->startup) {
  561. ret = machine->ops->startup(substream);
  562. if (ret < 0) {
  563. printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
  564. goto machine_err;
  565. }
  566. }
  567. if (rtd->codec_dai->ops.startup) {
  568. ret = rtd->codec_dai->ops.startup(substream);
  569. if (ret < 0) {
  570. printk(KERN_ERR "asoc: can't open codec %s\n",
  571. rtd->codec_dai->name);
  572. goto codec_dai_err;
  573. }
  574. }
  575. /* create runtime params from DMA, codec and cpu DAI */
  576. if (runtime->hw.rates)
  577. runtime->hw.rates &=
  578. get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
  579. get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
  580. else
  581. runtime->hw.rates =
  582. get_rates(codec_dai->caps.mode, codec_dai->caps.num_modes) &
  583. get_rates(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
  584. if (runtime->hw.formats)
  585. runtime->hw.formats &=
  586. get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
  587. get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
  588. else
  589. runtime->hw.formats =
  590. get_formats(codec_dai->caps.mode, codec_dai->caps.num_modes) &
  591. get_formats(cpu_dai->caps.mode, cpu_dai->caps.num_modes);
  592. /* Check that the codec and cpu DAI's are compatible */
  593. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  594. runtime->hw.rate_min =
  595. max(rtd->codec_dai->playback.rate_min,
  596. rtd->cpu_dai->playback.rate_min);
  597. runtime->hw.rate_max =
  598. min(rtd->codec_dai->playback.rate_max,
  599. rtd->cpu_dai->playback.rate_max);
  600. runtime->hw.channels_min =
  601. max(rtd->codec_dai->playback.channels_min,
  602. rtd->cpu_dai->playback.channels_min);
  603. runtime->hw.channels_max =
  604. min(rtd->codec_dai->playback.channels_max,
  605. rtd->cpu_dai->playback.channels_max);
  606. } else {
  607. runtime->hw.rate_min =
  608. max(rtd->codec_dai->capture.rate_min,
  609. rtd->cpu_dai->capture.rate_min);
  610. runtime->hw.rate_max =
  611. min(rtd->codec_dai->capture.rate_max,
  612. rtd->cpu_dai->capture.rate_max);
  613. runtime->hw.channels_min =
  614. max(rtd->codec_dai->capture.channels_min,
  615. rtd->cpu_dai->capture.channels_min);
  616. runtime->hw.channels_max =
  617. min(rtd->codec_dai->capture.channels_max,
  618. rtd->cpu_dai->capture.channels_max);
  619. }
  620. snd_pcm_limit_hw_rates(runtime);
  621. if (!runtime->hw.rates) {
  622. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  623. rtd->codec_dai->name, rtd->cpu_dai->name);
  624. goto codec_dai_err;
  625. }
  626. if (!runtime->hw.formats) {
  627. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  628. rtd->codec_dai->name, rtd->cpu_dai->name);
  629. goto codec_dai_err;
  630. }
  631. if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
  632. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  633. rtd->codec_dai->name, rtd->cpu_dai->name);
  634. goto codec_dai_err;
  635. }
  636. dbg("asoc: %s <-> %s info:\n", rtd->codec_dai->name, rtd->cpu_dai->name);
  637. dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
  638. dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  639. runtime->hw.channels_max);
  640. dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  641. runtime->hw.rate_max);
  642. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  643. rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
  644. else
  645. rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
  646. rtd->cpu_dai->active = rtd->codec_dai->active = 1;
  647. rtd->cpu_dai->runtime = runtime;
  648. socdev->codec->active++;
  649. mutex_unlock(&pcm_mutex);
  650. return 0;
  651. codec_dai_err:
  652. if (machine->ops && machine->ops->shutdown)
  653. machine->ops->shutdown(substream);
  654. machine_err:
  655. if (platform->pcm_ops->close)
  656. platform->pcm_ops->close(substream);
  657. platform_err:
  658. if (rtd->cpu_dai->ops.shutdown)
  659. rtd->cpu_dai->ops.shutdown(substream);
  660. out:
  661. mutex_unlock(&pcm_mutex);
  662. return ret;
  663. }
  664. /*
  665. * Power down the audio subsytem pmdown_time msecs after close is called.
  666. * This is to ensure there are no pops or clicks in between any music tracks
  667. * due to DAPM power cycling.
  668. */
  669. static void close_delayed_work(struct work_struct *work)
  670. {
  671. struct snd_soc_device *socdev =
  672. container_of(work, struct snd_soc_device, delayed_work.work);
  673. struct snd_soc_codec *codec = socdev->codec;
  674. struct snd_soc_codec_dai *codec_dai;
  675. int i;
  676. mutex_lock(&pcm_mutex);
  677. for(i = 0; i < codec->num_dai; i++) {
  678. codec_dai = &codec->dai[i];
  679. dbg("pop wq checking: %s status: %s waiting: %s\n",
  680. codec_dai->playback.stream_name,
  681. codec_dai->playback.active ? "active" : "inactive",
  682. codec_dai->pop_wait ? "yes" : "no");
  683. /* are we waiting on this codec DAI stream */
  684. if (codec_dai->pop_wait == 1) {
  685. codec_dai->pop_wait = 0;
  686. snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
  687. SND_SOC_DAPM_STREAM_STOP);
  688. /* power down the codec power domain if no longer active */
  689. if (codec->active == 0) {
  690. dbg("pop wq D3 %s %s\n", codec->name,
  691. codec_dai->playback.stream_name);
  692. if (codec->dapm_event)
  693. codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  694. }
  695. }
  696. }
  697. mutex_unlock(&pcm_mutex);
  698. }
  699. /*
  700. * Called by ALSA when a PCM substream is closed. Private data can be
  701. * freed here. The cpu DAI, codec DAI, machine and platform are also
  702. * shutdown.
  703. */
  704. static int soc_codec_close(struct snd_pcm_substream *substream)
  705. {
  706. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  707. struct snd_soc_device *socdev = rtd->socdev;
  708. struct snd_soc_machine *machine = socdev->machine;
  709. struct snd_soc_platform *platform = socdev->platform;
  710. struct snd_soc_codec *codec = socdev->codec;
  711. mutex_lock(&pcm_mutex);
  712. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  713. rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 0;
  714. else
  715. rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 0;
  716. if (rtd->codec_dai->playback.active == 0 &&
  717. rtd->codec_dai->capture.active == 0) {
  718. rtd->cpu_dai->active = rtd->codec_dai->active = 0;
  719. }
  720. codec->active--;
  721. if (rtd->cpu_dai->ops.shutdown)
  722. rtd->cpu_dai->ops.shutdown(substream);
  723. if (rtd->codec_dai->ops.shutdown)
  724. rtd->codec_dai->ops.shutdown(substream);
  725. if (machine->ops && machine->ops->shutdown)
  726. machine->ops->shutdown(substream);
  727. if (platform->pcm_ops->close)
  728. platform->pcm_ops->close(substream);
  729. rtd->cpu_dai->runtime = NULL;
  730. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  731. /* start delayed pop wq here for playback streams */
  732. rtd->codec_dai->pop_wait = 1;
  733. schedule_delayed_work(&socdev->delayed_work,
  734. msecs_to_jiffies(pmdown_time));
  735. } else {
  736. /* capture streams can be powered down now */
  737. snd_soc_dapm_stream_event(codec, rtd->codec_dai->capture.stream_name,
  738. SND_SOC_DAPM_STREAM_STOP);
  739. if (codec->active == 0 && rtd->codec_dai->pop_wait == 0){
  740. if (codec->dapm_event)
  741. codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  742. }
  743. }
  744. mutex_unlock(&pcm_mutex);
  745. return 0;
  746. }
  747. /*
  748. * Called by ALSA when the PCM substream is prepared, can set format, sample
  749. * rate, etc. This function is non atomic and can be called multiple times,
  750. * it can refer to the runtime info.
  751. */
  752. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  753. {
  754. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  755. struct snd_soc_device *socdev = rtd->socdev;
  756. struct snd_soc_platform *platform = socdev->platform;
  757. struct snd_soc_codec *codec = socdev->codec;
  758. int ret = 0;
  759. mutex_lock(&pcm_mutex);
  760. if (platform->pcm_ops->prepare) {
  761. ret = platform->pcm_ops->prepare(substream);
  762. if (ret < 0) {
  763. printk(KERN_ERR "asoc: platform prepare error\n");
  764. goto out;
  765. }
  766. }
  767. if (rtd->codec_dai->ops.prepare) {
  768. ret = rtd->codec_dai->ops.prepare(substream);
  769. if (ret < 0) {
  770. printk(KERN_ERR "asoc: codec DAI prepare error\n");
  771. goto out;
  772. }
  773. }
  774. if (rtd->cpu_dai->ops.prepare)
  775. ret = rtd->cpu_dai->ops.prepare(substream);
  776. /* we only want to start a DAPM playback stream if we are not waiting
  777. * on an existing one stopping */
  778. if (rtd->codec_dai->pop_wait) {
  779. /* we are waiting for the delayed work to start */
  780. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  781. snd_soc_dapm_stream_event(codec,
  782. rtd->codec_dai->capture.stream_name,
  783. SND_SOC_DAPM_STREAM_START);
  784. else {
  785. rtd->codec_dai->pop_wait = 0;
  786. cancel_delayed_work(&socdev->delayed_work);
  787. if (rtd->codec_dai->digital_mute)
  788. rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
  789. }
  790. } else {
  791. /* no delayed work - do we need to power up codec */
  792. if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
  793. if (codec->dapm_event)
  794. codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
  795. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  796. snd_soc_dapm_stream_event(codec,
  797. rtd->codec_dai->playback.stream_name,
  798. SND_SOC_DAPM_STREAM_START);
  799. else
  800. snd_soc_dapm_stream_event(codec,
  801. rtd->codec_dai->capture.stream_name,
  802. SND_SOC_DAPM_STREAM_START);
  803. if (codec->dapm_event)
  804. codec->dapm_event(codec, SNDRV_CTL_POWER_D0);
  805. if (rtd->codec_dai->digital_mute)
  806. rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
  807. } else {
  808. /* codec already powered - power on widgets */
  809. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  810. snd_soc_dapm_stream_event(codec,
  811. rtd->codec_dai->playback.stream_name,
  812. SND_SOC_DAPM_STREAM_START);
  813. else
  814. snd_soc_dapm_stream_event(codec,
  815. rtd->codec_dai->capture.stream_name,
  816. SND_SOC_DAPM_STREAM_START);
  817. if (rtd->codec_dai->digital_mute)
  818. rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
  819. }
  820. }
  821. out:
  822. mutex_unlock(&pcm_mutex);
  823. return ret;
  824. }
  825. /*
  826. * Called by ALSA when the hardware params are set by application. This
  827. * function can also be called multiple times and can allocate buffers
  828. * (using snd_pcm_lib_* ). It's non-atomic.
  829. */
  830. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  831. struct snd_pcm_hw_params *params)
  832. {
  833. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  834. struct snd_soc_device *socdev = rtd->socdev;
  835. struct snd_soc_platform *platform = socdev->platform;
  836. struct snd_soc_machine *machine = socdev->machine;
  837. int ret = 0;
  838. mutex_lock(&pcm_mutex);
  839. /* we don't need to match any AC97 params */
  840. if (rtd->cpu_dai->type != SND_SOC_DAI_AC97) {
  841. ret = soc_hw_match_params(substream, params);
  842. if (ret < 0)
  843. goto out;
  844. } else {
  845. struct snd_soc_clock_info clk_info;
  846. clk_info.rate = params_rate(params);
  847. ret = soc_get_mclk(rtd, &clk_info);
  848. if (ret < 0)
  849. goto out;
  850. }
  851. if (rtd->codec_dai->ops.hw_params) {
  852. ret = rtd->codec_dai->ops.hw_params(substream, params);
  853. if (ret < 0) {
  854. printk(KERN_ERR "asoc: can't set codec %s hw params\n",
  855. rtd->codec_dai->name);
  856. goto out;
  857. }
  858. }
  859. if (rtd->cpu_dai->ops.hw_params) {
  860. ret = rtd->cpu_dai->ops.hw_params(substream, params);
  861. if (ret < 0) {
  862. printk(KERN_ERR "asoc: can't set interface %s hw params\n",
  863. rtd->cpu_dai->name);
  864. goto interface_err;
  865. }
  866. }
  867. if (platform->pcm_ops->hw_params) {
  868. ret = platform->pcm_ops->hw_params(substream, params);
  869. if (ret < 0) {
  870. printk(KERN_ERR "asoc: can't set platform %s hw params\n",
  871. platform->name);
  872. goto platform_err;
  873. }
  874. }
  875. if (machine->ops && machine->ops->hw_params) {
  876. ret = machine->ops->hw_params(substream, params);
  877. if (ret < 0) {
  878. printk(KERN_ERR "asoc: machine hw_params failed\n");
  879. goto machine_err;
  880. }
  881. }
  882. out:
  883. mutex_unlock(&pcm_mutex);
  884. return ret;
  885. machine_err:
  886. if (platform->pcm_ops->hw_free)
  887. platform->pcm_ops->hw_free(substream);
  888. platform_err:
  889. if (rtd->cpu_dai->ops.hw_free)
  890. rtd->cpu_dai->ops.hw_free(substream);
  891. interface_err:
  892. if (rtd->codec_dai->ops.hw_free)
  893. rtd->codec_dai->ops.hw_free(substream);
  894. mutex_unlock(&pcm_mutex);
  895. return ret;
  896. }
  897. /*
  898. * Free's resources allocated by hw_params, can be called multiple times
  899. */
  900. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  901. {
  902. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  903. struct snd_soc_device *socdev = rtd->socdev;
  904. struct snd_soc_platform *platform = socdev->platform;
  905. struct snd_soc_codec *codec = socdev->codec;
  906. struct snd_soc_machine *machine = socdev->machine;
  907. mutex_lock(&pcm_mutex);
  908. /* apply codec digital mute */
  909. if (!codec->active && rtd->codec_dai->digital_mute)
  910. rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 1);
  911. /* free any machine hw params */
  912. if (machine->ops && machine->ops->hw_free)
  913. machine->ops->hw_free(substream);
  914. /* free any DMA resources */
  915. if (platform->pcm_ops->hw_free)
  916. platform->pcm_ops->hw_free(substream);
  917. /* now free hw params for the DAI's */
  918. if (rtd->codec_dai->ops.hw_free)
  919. rtd->codec_dai->ops.hw_free(substream);
  920. if (rtd->cpu_dai->ops.hw_free)
  921. rtd->cpu_dai->ops.hw_free(substream);
  922. mutex_unlock(&pcm_mutex);
  923. return 0;
  924. }
  925. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  926. {
  927. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  928. struct snd_soc_device *socdev = rtd->socdev;
  929. struct snd_soc_platform *platform = socdev->platform;
  930. int ret;
  931. if (rtd->codec_dai->ops.trigger) {
  932. ret = rtd->codec_dai->ops.trigger(substream, cmd);
  933. if (ret < 0)
  934. return ret;
  935. }
  936. if (platform->pcm_ops->trigger) {
  937. ret = platform->pcm_ops->trigger(substream, cmd);
  938. if (ret < 0)
  939. return ret;
  940. }
  941. if (rtd->cpu_dai->ops.trigger) {
  942. ret = rtd->cpu_dai->ops.trigger(substream, cmd);
  943. if (ret < 0)
  944. return ret;
  945. }
  946. return 0;
  947. }
  948. /* ASoC PCM operations */
  949. static struct snd_pcm_ops soc_pcm_ops = {
  950. .open = soc_pcm_open,
  951. .close = soc_codec_close,
  952. .hw_params = soc_pcm_hw_params,
  953. .hw_free = soc_pcm_hw_free,
  954. .prepare = soc_pcm_prepare,
  955. .trigger = soc_pcm_trigger,
  956. };
  957. #ifdef CONFIG_PM
  958. /* powers down audio subsystem for suspend */
  959. static int soc_suspend(struct platform_device *pdev, pm_message_t state)
  960. {
  961. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  962. struct snd_soc_machine *machine = socdev->machine;
  963. struct snd_soc_platform *platform = socdev->platform;
  964. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  965. struct snd_soc_codec *codec = socdev->codec;
  966. int i;
  967. /* mute any active DAC's */
  968. for(i = 0; i < machine->num_links; i++) {
  969. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  970. if (dai->digital_mute && dai->playback.active)
  971. dai->digital_mute(codec, dai, 1);
  972. }
  973. if (machine->suspend_pre)
  974. machine->suspend_pre(pdev, state);
  975. for(i = 0; i < machine->num_links; i++) {
  976. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  977. if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
  978. cpu_dai->suspend(pdev, cpu_dai);
  979. if (platform->suspend)
  980. platform->suspend(pdev, cpu_dai);
  981. }
  982. /* close any waiting streams and save state */
  983. run_delayed_work(&socdev->delayed_work);
  984. codec->suspend_dapm_state = codec->dapm_state;
  985. for(i = 0; i < codec->num_dai; i++) {
  986. char *stream = codec->dai[i].playback.stream_name;
  987. if (stream != NULL)
  988. snd_soc_dapm_stream_event(codec, stream,
  989. SND_SOC_DAPM_STREAM_SUSPEND);
  990. stream = codec->dai[i].capture.stream_name;
  991. if (stream != NULL)
  992. snd_soc_dapm_stream_event(codec, stream,
  993. SND_SOC_DAPM_STREAM_SUSPEND);
  994. }
  995. if (codec_dev->suspend)
  996. codec_dev->suspend(pdev, state);
  997. for(i = 0; i < machine->num_links; i++) {
  998. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  999. if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
  1000. cpu_dai->suspend(pdev, cpu_dai);
  1001. }
  1002. if (machine->suspend_post)
  1003. machine->suspend_post(pdev, state);
  1004. return 0;
  1005. }
  1006. /* powers up audio subsystem after a suspend */
  1007. static int soc_resume(struct platform_device *pdev)
  1008. {
  1009. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1010. struct snd_soc_machine *machine = socdev->machine;
  1011. struct snd_soc_platform *platform = socdev->platform;
  1012. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  1013. struct snd_soc_codec *codec = socdev->codec;
  1014. int i;
  1015. if (machine->resume_pre)
  1016. machine->resume_pre(pdev);
  1017. for(i = 0; i < machine->num_links; i++) {
  1018. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  1019. if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
  1020. cpu_dai->resume(pdev, cpu_dai);
  1021. }
  1022. if (codec_dev->resume)
  1023. codec_dev->resume(pdev);
  1024. for(i = 0; i < codec->num_dai; i++) {
  1025. char* stream = codec->dai[i].playback.stream_name;
  1026. if (stream != NULL)
  1027. snd_soc_dapm_stream_event(codec, stream,
  1028. SND_SOC_DAPM_STREAM_RESUME);
  1029. stream = codec->dai[i].capture.stream_name;
  1030. if (stream != NULL)
  1031. snd_soc_dapm_stream_event(codec, stream,
  1032. SND_SOC_DAPM_STREAM_RESUME);
  1033. }
  1034. /* unmute any active DAC's */
  1035. for(i = 0; i < machine->num_links; i++) {
  1036. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  1037. if (dai->digital_mute && dai->playback.active)
  1038. dai->digital_mute(codec, dai, 0);
  1039. }
  1040. for(i = 0; i < machine->num_links; i++) {
  1041. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  1042. if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
  1043. cpu_dai->resume(pdev, cpu_dai);
  1044. if (platform->resume)
  1045. platform->resume(pdev, cpu_dai);
  1046. }
  1047. if (machine->resume_post)
  1048. machine->resume_post(pdev);
  1049. return 0;
  1050. }
  1051. #else
  1052. #define soc_suspend NULL
  1053. #define soc_resume NULL
  1054. #endif
  1055. /* probes a new socdev */
  1056. static int soc_probe(struct platform_device *pdev)
  1057. {
  1058. int ret = 0, i;
  1059. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1060. struct snd_soc_machine *machine = socdev->machine;
  1061. struct snd_soc_platform *platform = socdev->platform;
  1062. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  1063. if (machine->probe) {
  1064. ret = machine->probe(pdev);
  1065. if(ret < 0)
  1066. return ret;
  1067. }
  1068. for (i = 0; i < machine->num_links; i++) {
  1069. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  1070. if (cpu_dai->probe) {
  1071. ret = cpu_dai->probe(pdev);
  1072. if(ret < 0)
  1073. goto cpu_dai_err;
  1074. }
  1075. }
  1076. if (codec_dev->probe) {
  1077. ret = codec_dev->probe(pdev);
  1078. if(ret < 0)
  1079. goto cpu_dai_err;
  1080. }
  1081. if (platform->probe) {
  1082. ret = platform->probe(pdev);
  1083. if(ret < 0)
  1084. goto platform_err;
  1085. }
  1086. /* DAPM stream work */
  1087. INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
  1088. return 0;
  1089. platform_err:
  1090. if (codec_dev->remove)
  1091. codec_dev->remove(pdev);
  1092. cpu_dai_err:
  1093. for (i--; i >= 0; i--) {
  1094. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  1095. if (cpu_dai->remove)
  1096. cpu_dai->remove(pdev);
  1097. }
  1098. if (machine->remove)
  1099. machine->remove(pdev);
  1100. return ret;
  1101. }
  1102. /* removes a socdev */
  1103. static int soc_remove(struct platform_device *pdev)
  1104. {
  1105. int i;
  1106. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1107. struct snd_soc_machine *machine = socdev->machine;
  1108. struct snd_soc_platform *platform = socdev->platform;
  1109. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  1110. run_delayed_work(&socdev->delayed_work);
  1111. if (platform->remove)
  1112. platform->remove(pdev);
  1113. if (codec_dev->remove)
  1114. codec_dev->remove(pdev);
  1115. for (i = 0; i < machine->num_links; i++) {
  1116. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  1117. if (cpu_dai->remove)
  1118. cpu_dai->remove(pdev);
  1119. }
  1120. if (machine->remove)
  1121. machine->remove(pdev);
  1122. return 0;
  1123. }
  1124. /* ASoC platform driver */
  1125. static struct platform_driver soc_driver = {
  1126. .driver = {
  1127. .name = "soc-audio",
  1128. },
  1129. .probe = soc_probe,
  1130. .remove = soc_remove,
  1131. .suspend = soc_suspend,
  1132. .resume = soc_resume,
  1133. };
  1134. /* create a new pcm */
  1135. static int soc_new_pcm(struct snd_soc_device *socdev,
  1136. struct snd_soc_dai_link *dai_link, int num)
  1137. {
  1138. struct snd_soc_codec *codec = socdev->codec;
  1139. struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
  1140. struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
  1141. struct snd_soc_pcm_runtime *rtd;
  1142. struct snd_pcm *pcm;
  1143. char new_name[64];
  1144. int ret = 0, playback = 0, capture = 0;
  1145. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  1146. if (rtd == NULL)
  1147. return -ENOMEM;
  1148. rtd->cpu_dai = cpu_dai;
  1149. rtd->codec_dai = codec_dai;
  1150. rtd->socdev = socdev;
  1151. /* check client and interface hw capabilities */
  1152. sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
  1153. get_dai_name(cpu_dai->type), num);
  1154. if (codec_dai->playback.channels_min)
  1155. playback = 1;
  1156. if (codec_dai->capture.channels_min)
  1157. capture = 1;
  1158. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  1159. capture, &pcm);
  1160. if (ret < 0) {
  1161. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  1162. kfree(rtd);
  1163. return ret;
  1164. }
  1165. pcm->private_data = rtd;
  1166. soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
  1167. soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
  1168. soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
  1169. soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
  1170. soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
  1171. soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
  1172. soc_pcm_ops.page = socdev->platform->pcm_ops->page;
  1173. if (playback)
  1174. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  1175. if (capture)
  1176. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  1177. ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
  1178. if (ret < 0) {
  1179. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  1180. kfree(rtd);
  1181. return ret;
  1182. }
  1183. pcm->private_free = socdev->platform->pcm_free;
  1184. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  1185. cpu_dai->name);
  1186. return ret;
  1187. }
  1188. /* codec register dump */
  1189. static ssize_t codec_reg_show(struct device *dev,
  1190. struct device_attribute *attr, char *buf)
  1191. {
  1192. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  1193. struct snd_soc_codec *codec = devdata->codec;
  1194. int i, step = 1, count = 0;
  1195. if (!codec->reg_cache_size)
  1196. return 0;
  1197. if (codec->reg_cache_step)
  1198. step = codec->reg_cache_step;
  1199. count += sprintf(buf, "%s registers\n", codec->name);
  1200. for(i = 0; i < codec->reg_cache_size; i += step)
  1201. count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
  1202. return count;
  1203. }
  1204. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  1205. /**
  1206. * snd_soc_new_ac97_codec - initailise AC97 device
  1207. * @codec: audio codec
  1208. * @ops: AC97 bus operations
  1209. * @num: AC97 codec number
  1210. *
  1211. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1212. */
  1213. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1214. struct snd_ac97_bus_ops *ops, int num)
  1215. {
  1216. mutex_lock(&codec->mutex);
  1217. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1218. if (codec->ac97 == NULL) {
  1219. mutex_unlock(&codec->mutex);
  1220. return -ENOMEM;
  1221. }
  1222. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1223. if (codec->ac97->bus == NULL) {
  1224. kfree(codec->ac97);
  1225. codec->ac97 = NULL;
  1226. mutex_unlock(&codec->mutex);
  1227. return -ENOMEM;
  1228. }
  1229. codec->ac97->bus->ops = ops;
  1230. codec->ac97->num = num;
  1231. mutex_unlock(&codec->mutex);
  1232. return 0;
  1233. }
  1234. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1235. /**
  1236. * snd_soc_free_ac97_codec - free AC97 codec device
  1237. * @codec: audio codec
  1238. *
  1239. * Frees AC97 codec device resources.
  1240. */
  1241. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1242. {
  1243. mutex_lock(&codec->mutex);
  1244. kfree(codec->ac97->bus);
  1245. kfree(codec->ac97);
  1246. codec->ac97 = NULL;
  1247. mutex_unlock(&codec->mutex);
  1248. }
  1249. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1250. /**
  1251. * snd_soc_update_bits - update codec register bits
  1252. * @codec: audio codec
  1253. * @reg: codec register
  1254. * @mask: register mask
  1255. * @value: new value
  1256. *
  1257. * Writes new register value.
  1258. *
  1259. * Returns 1 for change else 0.
  1260. */
  1261. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1262. unsigned short mask, unsigned short value)
  1263. {
  1264. int change;
  1265. unsigned short old, new;
  1266. mutex_lock(&io_mutex);
  1267. old = snd_soc_read(codec, reg);
  1268. new = (old & ~mask) | value;
  1269. change = old != new;
  1270. if (change)
  1271. snd_soc_write(codec, reg, new);
  1272. mutex_unlock(&io_mutex);
  1273. return change;
  1274. }
  1275. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1276. /**
  1277. * snd_soc_test_bits - test register for change
  1278. * @codec: audio codec
  1279. * @reg: codec register
  1280. * @mask: register mask
  1281. * @value: new value
  1282. *
  1283. * Tests a register with a new value and checks if the new value is
  1284. * different from the old value.
  1285. *
  1286. * Returns 1 for change else 0.
  1287. */
  1288. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1289. unsigned short mask, unsigned short value)
  1290. {
  1291. int change;
  1292. unsigned short old, new;
  1293. mutex_lock(&io_mutex);
  1294. old = snd_soc_read(codec, reg);
  1295. new = (old & ~mask) | value;
  1296. change = old != new;
  1297. mutex_unlock(&io_mutex);
  1298. return change;
  1299. }
  1300. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1301. /**
  1302. * snd_soc_get_rate - get int sample rate
  1303. * @hwpcmrate: the hardware pcm rate
  1304. *
  1305. * Returns the audio rate integaer value, else 0.
  1306. */
  1307. int snd_soc_get_rate(int hwpcmrate)
  1308. {
  1309. int rate = ffs(hwpcmrate) - 1;
  1310. if (rate > ARRAY_SIZE(rates))
  1311. return 0;
  1312. return rates[rate];
  1313. }
  1314. EXPORT_SYMBOL_GPL(snd_soc_get_rate);
  1315. /**
  1316. * snd_soc_new_pcms - create new sound card and pcms
  1317. * @socdev: the SoC audio device
  1318. *
  1319. * Create a new sound card based upon the codec and interface pcms.
  1320. *
  1321. * Returns 0 for success, else error.
  1322. */
  1323. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
  1324. {
  1325. struct snd_soc_codec *codec = socdev->codec;
  1326. struct snd_soc_machine *machine = socdev->machine;
  1327. int ret = 0, i;
  1328. mutex_lock(&codec->mutex);
  1329. /* register a sound card */
  1330. codec->card = snd_card_new(idx, xid, codec->owner, 0);
  1331. if (!codec->card) {
  1332. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  1333. codec->name);
  1334. mutex_unlock(&codec->mutex);
  1335. return -ENODEV;
  1336. }
  1337. codec->card->dev = socdev->dev;
  1338. codec->card->private_data = codec;
  1339. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  1340. /* create the pcms */
  1341. for(i = 0; i < machine->num_links; i++) {
  1342. ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
  1343. if (ret < 0) {
  1344. printk(KERN_ERR "asoc: can't create pcm %s\n",
  1345. machine->dai_link[i].stream_name);
  1346. mutex_unlock(&codec->mutex);
  1347. return ret;
  1348. }
  1349. }
  1350. mutex_unlock(&codec->mutex);
  1351. return ret;
  1352. }
  1353. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  1354. /**
  1355. * snd_soc_register_card - register sound card
  1356. * @socdev: the SoC audio device
  1357. *
  1358. * Register a SoC sound card. Also registers an AC97 device if the
  1359. * codec is AC97 for ad hoc devices.
  1360. *
  1361. * Returns 0 for success, else error.
  1362. */
  1363. int snd_soc_register_card(struct snd_soc_device *socdev)
  1364. {
  1365. struct snd_soc_codec *codec = socdev->codec;
  1366. struct snd_soc_machine *machine = socdev->machine;
  1367. int ret = 0, i, ac97 = 0, err = 0;
  1368. mutex_lock(&codec->mutex);
  1369. for(i = 0; i < machine->num_links; i++) {
  1370. if (socdev->machine->dai_link[i].init) {
  1371. err = socdev->machine->dai_link[i].init(codec);
  1372. if (err < 0) {
  1373. printk(KERN_ERR "asoc: failed to init %s\n",
  1374. socdev->machine->dai_link[i].stream_name);
  1375. continue;
  1376. }
  1377. }
  1378. if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
  1379. ac97 = 1;
  1380. }
  1381. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  1382. "%s", machine->name);
  1383. snprintf(codec->card->longname, sizeof(codec->card->longname),
  1384. "%s (%s)", machine->name, codec->name);
  1385. ret = snd_card_register(codec->card);
  1386. if (ret < 0) {
  1387. printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
  1388. codec->name);
  1389. goto out;
  1390. }
  1391. #ifdef CONFIG_SND_SOC_AC97_BUS
  1392. if (ac97) {
  1393. ret = soc_ac97_dev_register(codec);
  1394. if (ret < 0) {
  1395. printk(KERN_ERR "asoc: AC97 device register failed\n");
  1396. snd_card_free(codec->card);
  1397. goto out;
  1398. }
  1399. }
  1400. #endif
  1401. err = snd_soc_dapm_sys_add(socdev->dev);
  1402. if (err < 0)
  1403. printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
  1404. err = device_create_file(socdev->dev, &dev_attr_codec_reg);
  1405. if (err < 0)
  1406. printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
  1407. out:
  1408. mutex_unlock(&codec->mutex);
  1409. return ret;
  1410. }
  1411. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  1412. /**
  1413. * snd_soc_free_pcms - free sound card and pcms
  1414. * @socdev: the SoC audio device
  1415. *
  1416. * Frees sound card and pcms associated with the socdev.
  1417. * Also unregister the codec if it is an AC97 device.
  1418. */
  1419. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  1420. {
  1421. struct snd_soc_codec *codec = socdev->codec;
  1422. mutex_lock(&codec->mutex);
  1423. #ifdef CONFIG_SND_SOC_AC97_BUS
  1424. if (codec->ac97)
  1425. soc_ac97_dev_unregister(codec);
  1426. #endif
  1427. if (codec->card)
  1428. snd_card_free(codec->card);
  1429. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1430. mutex_unlock(&codec->mutex);
  1431. }
  1432. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1433. /**
  1434. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1435. * @substream: the pcm substream
  1436. * @hw: the hardware parameters
  1437. *
  1438. * Sets the substream runtime hardware parameters.
  1439. */
  1440. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1441. const struct snd_pcm_hardware *hw)
  1442. {
  1443. struct snd_pcm_runtime *runtime = substream->runtime;
  1444. runtime->hw.info = hw->info;
  1445. runtime->hw.formats = hw->formats;
  1446. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1447. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1448. runtime->hw.periods_min = hw->periods_min;
  1449. runtime->hw.periods_max = hw->periods_max;
  1450. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1451. runtime->hw.fifo_size = hw->fifo_size;
  1452. return 0;
  1453. }
  1454. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1455. /**
  1456. * snd_soc_cnew - create new control
  1457. * @_template: control template
  1458. * @data: control private data
  1459. * @lnng_name: control long name
  1460. *
  1461. * Create a new mixer control from a template control.
  1462. *
  1463. * Returns 0 for success, else error.
  1464. */
  1465. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1466. void *data, char *long_name)
  1467. {
  1468. struct snd_kcontrol_new template;
  1469. memcpy(&template, _template, sizeof(template));
  1470. if (long_name)
  1471. template.name = long_name;
  1472. template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1473. template.index = 0;
  1474. return snd_ctl_new1(&template, data);
  1475. }
  1476. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1477. /**
  1478. * snd_soc_info_enum_double - enumerated double mixer info callback
  1479. * @kcontrol: mixer control
  1480. * @uinfo: control element information
  1481. *
  1482. * Callback to provide information about a double enumerated
  1483. * mixer control.
  1484. *
  1485. * Returns 0 for success.
  1486. */
  1487. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1488. struct snd_ctl_elem_info *uinfo)
  1489. {
  1490. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1491. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1492. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1493. uinfo->value.enumerated.items = e->mask;
  1494. if (uinfo->value.enumerated.item > e->mask - 1)
  1495. uinfo->value.enumerated.item = e->mask - 1;
  1496. strcpy(uinfo->value.enumerated.name,
  1497. e->texts[uinfo->value.enumerated.item]);
  1498. return 0;
  1499. }
  1500. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1501. /**
  1502. * snd_soc_get_enum_double - enumerated double mixer get callback
  1503. * @kcontrol: mixer control
  1504. * @uinfo: control element information
  1505. *
  1506. * Callback to get the value of a double enumerated mixer.
  1507. *
  1508. * Returns 0 for success.
  1509. */
  1510. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1511. struct snd_ctl_elem_value *ucontrol)
  1512. {
  1513. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1514. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1515. unsigned short val, bitmask;
  1516. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1517. ;
  1518. val = snd_soc_read(codec, e->reg);
  1519. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1520. if (e->shift_l != e->shift_r)
  1521. ucontrol->value.enumerated.item[1] =
  1522. (val >> e->shift_r) & (bitmask - 1);
  1523. return 0;
  1524. }
  1525. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1526. /**
  1527. * snd_soc_put_enum_double - enumerated double mixer put callback
  1528. * @kcontrol: mixer control
  1529. * @uinfo: control element information
  1530. *
  1531. * Callback to set the value of a double enumerated mixer.
  1532. *
  1533. * Returns 0 for success.
  1534. */
  1535. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1536. struct snd_ctl_elem_value *ucontrol)
  1537. {
  1538. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1539. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1540. unsigned short val;
  1541. unsigned short mask, bitmask;
  1542. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1543. ;
  1544. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  1545. return -EINVAL;
  1546. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1547. mask = (bitmask - 1) << e->shift_l;
  1548. if (e->shift_l != e->shift_r) {
  1549. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  1550. return -EINVAL;
  1551. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1552. mask |= (bitmask - 1) << e->shift_r;
  1553. }
  1554. return snd_soc_update_bits(codec, e->reg, mask, val);
  1555. }
  1556. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1557. /**
  1558. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1559. * @kcontrol: mixer control
  1560. * @uinfo: control element information
  1561. *
  1562. * Callback to provide information about an external enumerated
  1563. * single mixer.
  1564. *
  1565. * Returns 0 for success.
  1566. */
  1567. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1568. struct snd_ctl_elem_info *uinfo)
  1569. {
  1570. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1571. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1572. uinfo->count = 1;
  1573. uinfo->value.enumerated.items = e->mask;
  1574. if (uinfo->value.enumerated.item > e->mask - 1)
  1575. uinfo->value.enumerated.item = e->mask - 1;
  1576. strcpy(uinfo->value.enumerated.name,
  1577. e->texts[uinfo->value.enumerated.item]);
  1578. return 0;
  1579. }
  1580. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1581. /**
  1582. * snd_soc_info_volsw_ext - external single mixer info callback
  1583. * @kcontrol: mixer control
  1584. * @uinfo: control element information
  1585. *
  1586. * Callback to provide information about a single external mixer control.
  1587. *
  1588. * Returns 0 for success.
  1589. */
  1590. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1591. struct snd_ctl_elem_info *uinfo)
  1592. {
  1593. int mask = kcontrol->private_value;
  1594. uinfo->type =
  1595. mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1596. uinfo->count = 1;
  1597. uinfo->value.integer.min = 0;
  1598. uinfo->value.integer.max = mask;
  1599. return 0;
  1600. }
  1601. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1602. /**
  1603. * snd_soc_info_bool_ext - external single boolean mixer info callback
  1604. * @kcontrol: mixer control
  1605. * @uinfo: control element information
  1606. *
  1607. * Callback to provide information about a single boolean external mixer control.
  1608. *
  1609. * Returns 0 for success.
  1610. */
  1611. int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
  1612. struct snd_ctl_elem_info *uinfo)
  1613. {
  1614. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1615. uinfo->count = 1;
  1616. uinfo->value.integer.min = 0;
  1617. uinfo->value.integer.max = 1;
  1618. return 0;
  1619. }
  1620. EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext);
  1621. /**
  1622. * snd_soc_info_volsw - single mixer info callback
  1623. * @kcontrol: mixer control
  1624. * @uinfo: control element information
  1625. *
  1626. * Callback to provide information about a single mixer control.
  1627. *
  1628. * Returns 0 for success.
  1629. */
  1630. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1631. struct snd_ctl_elem_info *uinfo)
  1632. {
  1633. int mask = (kcontrol->private_value >> 16) & 0xff;
  1634. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1635. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1636. uinfo->type =
  1637. mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1638. uinfo->count = shift == rshift ? 1 : 2;
  1639. uinfo->value.integer.min = 0;
  1640. uinfo->value.integer.max = mask;
  1641. return 0;
  1642. }
  1643. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1644. /**
  1645. * snd_soc_get_volsw - single mixer get callback
  1646. * @kcontrol: mixer control
  1647. * @uinfo: control element information
  1648. *
  1649. * Callback to get the value of a single mixer control.
  1650. *
  1651. * Returns 0 for success.
  1652. */
  1653. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1654. struct snd_ctl_elem_value *ucontrol)
  1655. {
  1656. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1657. int reg = kcontrol->private_value & 0xff;
  1658. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1659. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1660. int mask = (kcontrol->private_value >> 16) & 0xff;
  1661. int invert = (kcontrol->private_value >> 24) & 0x01;
  1662. ucontrol->value.integer.value[0] =
  1663. (snd_soc_read(codec, reg) >> shift) & mask;
  1664. if (shift != rshift)
  1665. ucontrol->value.integer.value[1] =
  1666. (snd_soc_read(codec, reg) >> rshift) & mask;
  1667. if (invert) {
  1668. ucontrol->value.integer.value[0] =
  1669. mask - ucontrol->value.integer.value[0];
  1670. if (shift != rshift)
  1671. ucontrol->value.integer.value[1] =
  1672. mask - ucontrol->value.integer.value[1];
  1673. }
  1674. return 0;
  1675. }
  1676. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1677. /**
  1678. * snd_soc_put_volsw - single mixer put callback
  1679. * @kcontrol: mixer control
  1680. * @uinfo: control element information
  1681. *
  1682. * Callback to set the value of a single mixer control.
  1683. *
  1684. * Returns 0 for success.
  1685. */
  1686. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1687. struct snd_ctl_elem_value *ucontrol)
  1688. {
  1689. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1690. int reg = kcontrol->private_value & 0xff;
  1691. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1692. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1693. int mask = (kcontrol->private_value >> 16) & 0xff;
  1694. int invert = (kcontrol->private_value >> 24) & 0x01;
  1695. int err;
  1696. unsigned short val, val2, val_mask;
  1697. val = (ucontrol->value.integer.value[0] & mask);
  1698. if (invert)
  1699. val = mask - val;
  1700. val_mask = mask << shift;
  1701. val = val << shift;
  1702. if (shift != rshift) {
  1703. val2 = (ucontrol->value.integer.value[1] & mask);
  1704. if (invert)
  1705. val2 = mask - val2;
  1706. val_mask |= mask << rshift;
  1707. val |= val2 << rshift;
  1708. }
  1709. err = snd_soc_update_bits(codec, reg, val_mask, val);
  1710. return err;
  1711. }
  1712. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1713. /**
  1714. * snd_soc_info_volsw_2r - double mixer info callback
  1715. * @kcontrol: mixer control
  1716. * @uinfo: control element information
  1717. *
  1718. * Callback to provide information about a double mixer control that
  1719. * spans 2 codec registers.
  1720. *
  1721. * Returns 0 for success.
  1722. */
  1723. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1724. struct snd_ctl_elem_info *uinfo)
  1725. {
  1726. int mask = (kcontrol->private_value >> 12) & 0xff;
  1727. uinfo->type =
  1728. mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1729. uinfo->count = 2;
  1730. uinfo->value.integer.min = 0;
  1731. uinfo->value.integer.max = mask;
  1732. return 0;
  1733. }
  1734. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1735. /**
  1736. * snd_soc_get_volsw_2r - double mixer get callback
  1737. * @kcontrol: mixer control
  1738. * @uinfo: control element information
  1739. *
  1740. * Callback to get the value of a double mixer control that spans 2 registers.
  1741. *
  1742. * Returns 0 for success.
  1743. */
  1744. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1745. struct snd_ctl_elem_value *ucontrol)
  1746. {
  1747. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1748. int reg = kcontrol->private_value & 0xff;
  1749. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1750. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1751. int mask = (kcontrol->private_value >> 12) & 0xff;
  1752. int invert = (kcontrol->private_value >> 20) & 0x01;
  1753. ucontrol->value.integer.value[0] =
  1754. (snd_soc_read(codec, reg) >> shift) & mask;
  1755. ucontrol->value.integer.value[1] =
  1756. (snd_soc_read(codec, reg2) >> shift) & mask;
  1757. if (invert) {
  1758. ucontrol->value.integer.value[0] =
  1759. mask - ucontrol->value.integer.value[0];
  1760. ucontrol->value.integer.value[1] =
  1761. mask - ucontrol->value.integer.value[1];
  1762. }
  1763. return 0;
  1764. }
  1765. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1766. /**
  1767. * snd_soc_put_volsw_2r - double mixer set callback
  1768. * @kcontrol: mixer control
  1769. * @uinfo: control element information
  1770. *
  1771. * Callback to set the value of a double mixer control that spans 2 registers.
  1772. *
  1773. * Returns 0 for success.
  1774. */
  1775. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1776. struct snd_ctl_elem_value *ucontrol)
  1777. {
  1778. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1779. int reg = kcontrol->private_value & 0xff;
  1780. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1781. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1782. int mask = (kcontrol->private_value >> 12) & 0xff;
  1783. int invert = (kcontrol->private_value >> 20) & 0x01;
  1784. int err;
  1785. unsigned short val, val2, val_mask;
  1786. val_mask = mask << shift;
  1787. val = (ucontrol->value.integer.value[0] & mask);
  1788. val2 = (ucontrol->value.integer.value[1] & mask);
  1789. if (invert) {
  1790. val = mask - val;
  1791. val2 = mask - val2;
  1792. }
  1793. val = val << shift;
  1794. val2 = val2 << shift;
  1795. if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
  1796. return err;
  1797. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  1798. return err;
  1799. }
  1800. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1801. static int __devinit snd_soc_init(void)
  1802. {
  1803. printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
  1804. return platform_driver_register(&soc_driver);
  1805. }
  1806. static void snd_soc_exit(void)
  1807. {
  1808. platform_driver_unregister(&soc_driver);
  1809. }
  1810. module_init(snd_soc_init);
  1811. module_exit(snd_soc_exit);
  1812. /* Module information */
  1813. MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
  1814. MODULE_DESCRIPTION("ALSA SoC Core");
  1815. MODULE_LICENSE("GPL");