soc-core.c 58 KB

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