soc-core.c 53 KB

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