soc-core.c 58 KB

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