soc-core.c 58 KB

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