soc-core.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  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 \nasoc: min ch %d max ch %d\n
  513. asoc: min rate %d max rate %d\n",
  514. runtime->hw.rates, runtime->hw.channels_min,
  515. runtime->hw.channels_max, runtime->hw.rate_min, runtime->hw.rate_max);
  516. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  517. rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 1;
  518. else
  519. rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 1;
  520. rtd->cpu_dai->active = rtd->codec_dai->active = 1;
  521. rtd->cpu_dai->runtime = runtime;
  522. socdev->codec->active++;
  523. mutex_unlock(&pcm_mutex);
  524. return 0;
  525. codec_dai_err:
  526. if (machine->ops && machine->ops->shutdown)
  527. machine->ops->shutdown(substream);
  528. machine_err:
  529. if (platform->pcm_ops->close)
  530. platform->pcm_ops->close(substream);
  531. platform_err:
  532. if (rtd->cpu_dai->ops.shutdown)
  533. rtd->cpu_dai->ops.shutdown(substream);
  534. out:
  535. mutex_unlock(&pcm_mutex);
  536. return ret;
  537. }
  538. /*
  539. * Power down the audio subsytem pmdown_time msecs after close is called.
  540. * This is to ensure there are no pops or clicks in between any music tracks
  541. * due to DAPM power cycling.
  542. */
  543. static void close_delayed_work(void *data)
  544. {
  545. struct snd_soc_device *socdev = data;
  546. struct snd_soc_codec *codec = socdev->codec;
  547. struct snd_soc_codec_dai *codec_dai;
  548. int i;
  549. mutex_lock(&pcm_mutex);
  550. for(i = 0; i < codec->num_dai; i++) {
  551. codec_dai = &codec->dai[i];
  552. dbg("pop wq checking: %s status: %s waiting: %s\n",
  553. codec_dai->playback.stream_name,
  554. codec_dai->playback.active ? "active" : "inactive",
  555. codec_dai->pop_wait ? "yes" : "no");
  556. /* are we waiting on this codec DAI stream */
  557. if (codec_dai->pop_wait == 1) {
  558. codec_dai->pop_wait = 0;
  559. snd_soc_dapm_stream_event(codec, codec_dai->playback.stream_name,
  560. SND_SOC_DAPM_STREAM_STOP);
  561. /* power down the codec power domain if no longer active */
  562. if (codec->active == 0) {
  563. dbg("pop wq D3 %s %s\n", codec->name,
  564. codec_dai->playback.stream_name);
  565. if (codec->dapm_event)
  566. codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  567. }
  568. }
  569. }
  570. mutex_unlock(&pcm_mutex);
  571. }
  572. /*
  573. * Called by ALSA when a PCM substream is closed. Private data can be
  574. * freed here. The cpu DAI, codec DAI, machine and platform are also
  575. * shutdown.
  576. */
  577. static int soc_codec_close(struct snd_pcm_substream *substream)
  578. {
  579. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  580. struct snd_soc_device *socdev = rtd->socdev;
  581. struct snd_soc_machine *machine = socdev->machine;
  582. struct snd_soc_platform *platform = socdev->platform;
  583. struct snd_soc_codec *codec = socdev->codec;
  584. mutex_lock(&pcm_mutex);
  585. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  586. rtd->cpu_dai->playback.active = rtd->codec_dai->playback.active = 0;
  587. else
  588. rtd->cpu_dai->capture.active = rtd->codec_dai->capture.active = 0;
  589. if (rtd->codec_dai->playback.active == 0 &&
  590. rtd->codec_dai->capture.active == 0) {
  591. rtd->cpu_dai->active = rtd->codec_dai->active = 0;
  592. }
  593. codec->active--;
  594. if (rtd->cpu_dai->ops.shutdown)
  595. rtd->cpu_dai->ops.shutdown(substream);
  596. if (rtd->codec_dai->ops.shutdown)
  597. rtd->codec_dai->ops.shutdown(substream);
  598. if (machine->ops && machine->ops->shutdown)
  599. machine->ops->shutdown(substream);
  600. if (platform->pcm_ops->close)
  601. platform->pcm_ops->close(substream);
  602. rtd->cpu_dai->runtime = NULL;
  603. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  604. /* start delayed pop wq here for playback streams */
  605. rtd->codec_dai->pop_wait = 1;
  606. queue_delayed_work(soc_workq, &soc_stream_work,
  607. msecs_to_jiffies(pmdown_time));
  608. } else {
  609. /* capture streams can be powered down now */
  610. snd_soc_dapm_stream_event(codec, rtd->codec_dai->capture.stream_name,
  611. SND_SOC_DAPM_STREAM_STOP);
  612. if (codec->active == 0 && rtd->codec_dai->pop_wait == 0){
  613. if (codec->dapm_event)
  614. codec->dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  615. }
  616. }
  617. mutex_unlock(&pcm_mutex);
  618. return 0;
  619. }
  620. /*
  621. * Called by ALSA when the PCM substream is prepared, can set format, sample
  622. * rate, etc. This function is non atomic and can be called multiple times,
  623. * it can refer to the runtime info.
  624. */
  625. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  626. {
  627. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  628. struct snd_soc_device *socdev = rtd->socdev;
  629. struct snd_soc_platform *platform = socdev->platform;
  630. struct snd_soc_codec *codec = socdev->codec;
  631. int ret = 0;
  632. mutex_lock(&pcm_mutex);
  633. if (platform->pcm_ops->prepare) {
  634. ret = platform->pcm_ops->prepare(substream);
  635. if (ret < 0)
  636. goto out;
  637. }
  638. if (rtd->codec_dai->ops.prepare) {
  639. ret = rtd->codec_dai->ops.prepare(substream);
  640. if (ret < 0)
  641. goto out;
  642. }
  643. if (rtd->cpu_dai->ops.prepare)
  644. ret = rtd->cpu_dai->ops.prepare(substream);
  645. /* we only want to start a DAPM playback stream if we are not waiting
  646. * on an existing one stopping */
  647. if (rtd->codec_dai->pop_wait) {
  648. /* we are waiting for the delayed work to start */
  649. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  650. snd_soc_dapm_stream_event(codec,
  651. rtd->codec_dai->capture.stream_name,
  652. SND_SOC_DAPM_STREAM_START);
  653. else {
  654. rtd->codec_dai->pop_wait = 0;
  655. cancel_delayed_work(&soc_stream_work);
  656. if (rtd->codec_dai->digital_mute)
  657. rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
  658. }
  659. } else {
  660. /* no delayed work - do we need to power up codec */
  661. if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
  662. if (codec->dapm_event)
  663. codec->dapm_event(codec, SNDRV_CTL_POWER_D1);
  664. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  665. snd_soc_dapm_stream_event(codec,
  666. rtd->codec_dai->playback.stream_name,
  667. SND_SOC_DAPM_STREAM_START);
  668. else
  669. snd_soc_dapm_stream_event(codec,
  670. rtd->codec_dai->capture.stream_name,
  671. SND_SOC_DAPM_STREAM_START);
  672. if (codec->dapm_event)
  673. codec->dapm_event(codec, SNDRV_CTL_POWER_D0);
  674. if (rtd->codec_dai->digital_mute)
  675. rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
  676. } else {
  677. /* codec already powered - power on widgets */
  678. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  679. snd_soc_dapm_stream_event(codec,
  680. rtd->codec_dai->playback.stream_name,
  681. SND_SOC_DAPM_STREAM_START);
  682. else
  683. snd_soc_dapm_stream_event(codec,
  684. rtd->codec_dai->capture.stream_name,
  685. SND_SOC_DAPM_STREAM_START);
  686. if (rtd->codec_dai->digital_mute)
  687. rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 0);
  688. }
  689. }
  690. out:
  691. mutex_unlock(&pcm_mutex);
  692. return ret;
  693. }
  694. /*
  695. * Called by ALSA when the hardware params are set by application. This
  696. * function can also be called multiple times and can allocate buffers
  697. * (using snd_pcm_lib_* ). It's non-atomic.
  698. */
  699. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  700. struct snd_pcm_hw_params *params)
  701. {
  702. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  703. struct snd_soc_device *socdev = rtd->socdev;
  704. struct snd_soc_platform *platform = socdev->platform;
  705. struct snd_soc_machine *machine = socdev->machine;
  706. int ret = 0;
  707. mutex_lock(&pcm_mutex);
  708. /* we don't need to match any AC97 params */
  709. if (rtd->cpu_dai->type != SND_SOC_DAI_AC97) {
  710. ret = soc_hw_match_params(substream, params);
  711. if (ret < 0)
  712. goto out;
  713. } else {
  714. struct snd_soc_clock_info clk_info;
  715. clk_info.rate = params_rate(params);
  716. ret = soc_get_mclk(rtd, &clk_info);
  717. if (ret < 0)
  718. goto out;
  719. }
  720. if (rtd->codec_dai->ops.hw_params) {
  721. ret = rtd->codec_dai->ops.hw_params(substream, params);
  722. if (ret < 0) {
  723. printk(KERN_ERR "asoc: can't set codec %s hw params\n",
  724. rtd->codec_dai->name);
  725. goto out;
  726. }
  727. }
  728. if (rtd->cpu_dai->ops.hw_params) {
  729. ret = rtd->cpu_dai->ops.hw_params(substream, params);
  730. if (ret < 0) {
  731. printk(KERN_ERR "asoc: can't set interface %s hw params\n",
  732. rtd->cpu_dai->name);
  733. goto interface_err;
  734. }
  735. }
  736. if (platform->pcm_ops->hw_params) {
  737. ret = platform->pcm_ops->hw_params(substream, params);
  738. if (ret < 0) {
  739. printk(KERN_ERR "asoc: can't set platform %s hw params\n",
  740. platform->name);
  741. goto platform_err;
  742. }
  743. }
  744. if (machine->ops && machine->ops->hw_params) {
  745. ret = machine->ops->hw_params(substream, params);
  746. if (ret < 0) {
  747. printk(KERN_ERR "asoc: machine hw_params failed\n");
  748. goto machine_err;
  749. }
  750. }
  751. out:
  752. mutex_unlock(&pcm_mutex);
  753. return ret;
  754. machine_err:
  755. if (platform->pcm_ops->hw_free)
  756. platform->pcm_ops->hw_free(substream);
  757. platform_err:
  758. if (rtd->cpu_dai->ops.hw_free)
  759. rtd->cpu_dai->ops.hw_free(substream);
  760. interface_err:
  761. if (rtd->codec_dai->ops.hw_free)
  762. rtd->codec_dai->ops.hw_free(substream);
  763. mutex_unlock(&pcm_mutex);
  764. return ret;
  765. }
  766. /*
  767. * Free's resources allocated by hw_params, can be called multiple times
  768. */
  769. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  770. {
  771. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  772. struct snd_soc_device *socdev = rtd->socdev;
  773. struct snd_soc_platform *platform = socdev->platform;
  774. struct snd_soc_codec *codec = socdev->codec;
  775. struct snd_soc_machine *machine = socdev->machine;
  776. mutex_lock(&pcm_mutex);
  777. /* apply codec digital mute */
  778. if (!codec->active && rtd->codec_dai->digital_mute)
  779. rtd->codec_dai->digital_mute(codec, rtd->codec_dai, 1);
  780. /* free any machine hw params */
  781. if (machine->ops && machine->ops->hw_free)
  782. machine->ops->hw_free(substream);
  783. /* free any DMA resources */
  784. if (platform->pcm_ops->hw_free)
  785. platform->pcm_ops->hw_free(substream);
  786. /* now free hw params for the DAI's */
  787. if (rtd->codec_dai->ops.hw_free)
  788. rtd->codec_dai->ops.hw_free(substream);
  789. if (rtd->cpu_dai->ops.hw_free)
  790. rtd->cpu_dai->ops.hw_free(substream);
  791. mutex_unlock(&pcm_mutex);
  792. return 0;
  793. }
  794. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  795. {
  796. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  797. struct snd_soc_device *socdev = rtd->socdev;
  798. struct snd_soc_platform *platform = socdev->platform;
  799. int ret;
  800. if (rtd->codec_dai->ops.trigger) {
  801. ret = rtd->codec_dai->ops.trigger(substream, cmd);
  802. if (ret < 0)
  803. return ret;
  804. }
  805. if (platform->pcm_ops->trigger) {
  806. ret = platform->pcm_ops->trigger(substream, cmd);
  807. if (ret < 0)
  808. return ret;
  809. }
  810. if (rtd->cpu_dai->ops.trigger) {
  811. ret = rtd->cpu_dai->ops.trigger(substream, cmd);
  812. if (ret < 0)
  813. return ret;
  814. }
  815. return 0;
  816. }
  817. /* ASoC PCM operations */
  818. static struct snd_pcm_ops soc_pcm_ops = {
  819. .open = soc_pcm_open,
  820. .close = soc_codec_close,
  821. .hw_params = soc_pcm_hw_params,
  822. .hw_free = soc_pcm_hw_free,
  823. .prepare = soc_pcm_prepare,
  824. .trigger = soc_pcm_trigger,
  825. };
  826. #ifdef CONFIG_PM
  827. /* powers down audio subsystem for suspend */
  828. static int soc_suspend(struct platform_device *pdev, pm_message_t state)
  829. {
  830. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  831. struct snd_soc_machine *machine = socdev->machine;
  832. struct snd_soc_platform *platform = socdev->platform;
  833. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  834. struct snd_soc_codec *codec = socdev->codec;
  835. int i;
  836. /* mute any active DAC's */
  837. for(i = 0; i < machine->num_links; i++) {
  838. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  839. if (dai->digital_mute && dai->playback.active)
  840. dai->digital_mute(codec, dai, 1);
  841. }
  842. if (machine->suspend_pre)
  843. machine->suspend_pre(pdev, state);
  844. for(i = 0; i < machine->num_links; i++) {
  845. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  846. if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
  847. cpu_dai->suspend(pdev, cpu_dai);
  848. if (platform->suspend)
  849. platform->suspend(pdev, cpu_dai);
  850. }
  851. /* close any waiting streams and save state */
  852. flush_workqueue(soc_workq);
  853. codec->suspend_dapm_state = codec->dapm_state;
  854. for(i = 0; i < codec->num_dai; i++) {
  855. char *stream = codec->dai[i].playback.stream_name;
  856. if (stream != NULL)
  857. snd_soc_dapm_stream_event(codec, stream,
  858. SND_SOC_DAPM_STREAM_SUSPEND);
  859. stream = codec->dai[i].capture.stream_name;
  860. if (stream != NULL)
  861. snd_soc_dapm_stream_event(codec, stream,
  862. SND_SOC_DAPM_STREAM_SUSPEND);
  863. }
  864. if (codec_dev->suspend)
  865. codec_dev->suspend(pdev, state);
  866. for(i = 0; i < machine->num_links; i++) {
  867. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  868. if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
  869. cpu_dai->suspend(pdev, cpu_dai);
  870. }
  871. if (machine->suspend_post)
  872. machine->suspend_post(pdev, state);
  873. return 0;
  874. }
  875. /* powers up audio subsystem after a suspend */
  876. static int soc_resume(struct platform_device *pdev)
  877. {
  878. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  879. struct snd_soc_machine *machine = socdev->machine;
  880. struct snd_soc_platform *platform = socdev->platform;
  881. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  882. struct snd_soc_codec *codec = socdev->codec;
  883. int i;
  884. if (machine->resume_pre)
  885. machine->resume_pre(pdev);
  886. for(i = 0; i < machine->num_links; i++) {
  887. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  888. if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
  889. cpu_dai->resume(pdev, cpu_dai);
  890. }
  891. if (codec_dev->resume)
  892. codec_dev->resume(pdev);
  893. for(i = 0; i < codec->num_dai; i++) {
  894. char* stream = codec->dai[i].playback.stream_name;
  895. if (stream != NULL)
  896. snd_soc_dapm_stream_event(codec, stream,
  897. SND_SOC_DAPM_STREAM_RESUME);
  898. stream = codec->dai[i].capture.stream_name;
  899. if (stream != NULL)
  900. snd_soc_dapm_stream_event(codec, stream,
  901. SND_SOC_DAPM_STREAM_RESUME);
  902. }
  903. /* unmute any active DAC's */
  904. for(i = 0; i < machine->num_links; i++) {
  905. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  906. if (dai->digital_mute && dai->playback.active)
  907. dai->digital_mute(codec, dai, 0);
  908. }
  909. for(i = 0; i < machine->num_links; i++) {
  910. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  911. if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
  912. cpu_dai->resume(pdev, cpu_dai);
  913. if (platform->resume)
  914. platform->resume(pdev, cpu_dai);
  915. }
  916. if (machine->resume_post)
  917. machine->resume_post(pdev);
  918. return 0;
  919. }
  920. #else
  921. #define soc_suspend NULL
  922. #define soc_resume NULL
  923. #endif
  924. /* probes a new socdev */
  925. static int soc_probe(struct platform_device *pdev)
  926. {
  927. int ret = 0, i;
  928. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  929. struct snd_soc_machine *machine = socdev->machine;
  930. struct snd_soc_platform *platform = socdev->platform;
  931. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  932. if (machine->probe) {
  933. ret = machine->probe(pdev);
  934. if(ret < 0)
  935. return ret;
  936. }
  937. for (i = 0; i < machine->num_links; i++) {
  938. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  939. if (cpu_dai->probe) {
  940. ret = cpu_dai->probe(pdev);
  941. if(ret < 0)
  942. goto cpu_dai_err;
  943. }
  944. }
  945. if (codec_dev->probe) {
  946. ret = codec_dev->probe(pdev);
  947. if(ret < 0)
  948. goto cpu_dai_err;
  949. }
  950. if (platform->probe) {
  951. ret = platform->probe(pdev);
  952. if(ret < 0)
  953. goto platform_err;
  954. }
  955. /* DAPM stream work */
  956. soc_workq = create_workqueue("kdapm");
  957. if (soc_workq == NULL)
  958. goto work_err;
  959. INIT_WORK(&soc_stream_work, close_delayed_work, socdev);
  960. return 0;
  961. work_err:
  962. if (platform->remove)
  963. platform->remove(pdev);
  964. platform_err:
  965. if (codec_dev->remove)
  966. codec_dev->remove(pdev);
  967. cpu_dai_err:
  968. for (i--; i > 0; i--) {
  969. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  970. if (cpu_dai->remove)
  971. cpu_dai->remove(pdev);
  972. }
  973. if (machine->remove)
  974. machine->remove(pdev);
  975. return ret;
  976. }
  977. /* removes a socdev */
  978. static int soc_remove(struct platform_device *pdev)
  979. {
  980. int i;
  981. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  982. struct snd_soc_machine *machine = socdev->machine;
  983. struct snd_soc_platform *platform = socdev->platform;
  984. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  985. if (soc_workq)
  986. destroy_workqueue(soc_workq);
  987. if (platform->remove)
  988. platform->remove(pdev);
  989. if (codec_dev->remove)
  990. codec_dev->remove(pdev);
  991. for (i = 0; i < machine->num_links; i++) {
  992. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  993. if (cpu_dai->remove)
  994. cpu_dai->remove(pdev);
  995. }
  996. if (machine->remove)
  997. machine->remove(pdev);
  998. return 0;
  999. }
  1000. /* ASoC platform driver */
  1001. static struct platform_driver soc_driver = {
  1002. .driver = {
  1003. .name = "soc-audio",
  1004. },
  1005. .probe = soc_probe,
  1006. .remove = soc_remove,
  1007. .suspend = soc_suspend,
  1008. .resume = soc_resume,
  1009. };
  1010. /* create a new pcm */
  1011. static int soc_new_pcm(struct snd_soc_device *socdev,
  1012. struct snd_soc_dai_link *dai_link, int num)
  1013. {
  1014. struct snd_soc_codec *codec = socdev->codec;
  1015. struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
  1016. struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
  1017. struct snd_soc_pcm_runtime *rtd;
  1018. struct snd_pcm *pcm;
  1019. char new_name[64];
  1020. int ret = 0, playback = 0, capture = 0;
  1021. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  1022. if (rtd == NULL)
  1023. return -ENOMEM;
  1024. rtd->cpu_dai = cpu_dai;
  1025. rtd->codec_dai = codec_dai;
  1026. rtd->socdev = socdev;
  1027. /* check client and interface hw capabilities */
  1028. sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
  1029. get_dai_name(cpu_dai->type), num);
  1030. if (codec_dai->playback.channels_min)
  1031. playback = 1;
  1032. if (codec_dai->capture.channels_min)
  1033. capture = 1;
  1034. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  1035. capture, &pcm);
  1036. if (ret < 0) {
  1037. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  1038. kfree(rtd);
  1039. return ret;
  1040. }
  1041. pcm->private_data = rtd;
  1042. soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
  1043. soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
  1044. soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
  1045. soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
  1046. soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
  1047. soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
  1048. soc_pcm_ops.page = socdev->platform->pcm_ops->page;
  1049. if (playback)
  1050. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  1051. if (capture)
  1052. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  1053. ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
  1054. if (ret < 0) {
  1055. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  1056. kfree(rtd);
  1057. return ret;
  1058. }
  1059. pcm->private_free = socdev->platform->pcm_free;
  1060. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  1061. cpu_dai->name);
  1062. return ret;
  1063. }
  1064. /* codec register dump */
  1065. static ssize_t codec_reg_show(struct device *dev,
  1066. struct device_attribute *attr, char *buf)
  1067. {
  1068. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  1069. struct snd_soc_codec *codec = devdata->codec;
  1070. int i, step = 1, count = 0;
  1071. if (!codec->reg_cache_size)
  1072. return 0;
  1073. if (codec->reg_cache_step)
  1074. step = codec->reg_cache_step;
  1075. count += sprintf(buf, "%s registers\n", codec->name);
  1076. for(i = 0; i < codec->reg_cache_size; i += step)
  1077. count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
  1078. return count;
  1079. }
  1080. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  1081. /**
  1082. * snd_soc_new_ac97_codec - initailise AC97 device
  1083. * @codec: audio codec
  1084. * @ops: AC97 bus operations
  1085. * @num: AC97 codec number
  1086. *
  1087. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1088. */
  1089. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1090. struct snd_ac97_bus_ops *ops, int num)
  1091. {
  1092. mutex_lock(&codec->mutex);
  1093. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1094. if (codec->ac97 == NULL) {
  1095. mutex_unlock(&codec->mutex);
  1096. return -ENOMEM;
  1097. }
  1098. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1099. if (codec->ac97->bus == NULL) {
  1100. kfree(codec->ac97);
  1101. codec->ac97 = NULL;
  1102. mutex_unlock(&codec->mutex);
  1103. return -ENOMEM;
  1104. }
  1105. codec->ac97->bus->ops = ops;
  1106. codec->ac97->num = num;
  1107. mutex_unlock(&codec->mutex);
  1108. return 0;
  1109. }
  1110. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1111. /**
  1112. * snd_soc_free_ac97_codec - free AC97 codec device
  1113. * @codec: audio codec
  1114. *
  1115. * Frees AC97 codec device resources.
  1116. */
  1117. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1118. {
  1119. mutex_lock(&codec->mutex);
  1120. kfree(codec->ac97->bus);
  1121. kfree(codec->ac97);
  1122. codec->ac97 = NULL;
  1123. mutex_unlock(&codec->mutex);
  1124. }
  1125. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1126. /**
  1127. * snd_soc_update_bits - update codec register bits
  1128. * @codec: audio codec
  1129. * @reg: codec register
  1130. * @mask: register mask
  1131. * @value: new value
  1132. *
  1133. * Writes new register value.
  1134. *
  1135. * Returns 1 for change else 0.
  1136. */
  1137. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1138. unsigned short mask, unsigned short value)
  1139. {
  1140. int change;
  1141. unsigned short old, new;
  1142. mutex_lock(&io_mutex);
  1143. old = snd_soc_read(codec, reg);
  1144. new = (old & ~mask) | value;
  1145. change = old != new;
  1146. if (change)
  1147. snd_soc_write(codec, reg, new);
  1148. mutex_unlock(&io_mutex);
  1149. return change;
  1150. }
  1151. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1152. /**
  1153. * snd_soc_test_bits - test register for change
  1154. * @codec: audio codec
  1155. * @reg: codec register
  1156. * @mask: register mask
  1157. * @value: new value
  1158. *
  1159. * Tests a register with a new value and checks if the new value is
  1160. * different from the old value.
  1161. *
  1162. * Returns 1 for change else 0.
  1163. */
  1164. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1165. unsigned short mask, unsigned short value)
  1166. {
  1167. int change;
  1168. unsigned short old, new;
  1169. mutex_lock(&io_mutex);
  1170. old = snd_soc_read(codec, reg);
  1171. new = (old & ~mask) | value;
  1172. change = old != new;
  1173. mutex_unlock(&io_mutex);
  1174. return change;
  1175. }
  1176. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1177. /**
  1178. * snd_soc_get_rate - get int sample rate
  1179. * @hwpcmrate: the hardware pcm rate
  1180. *
  1181. * Returns the audio rate integaer value, else 0.
  1182. */
  1183. int snd_soc_get_rate(int hwpcmrate)
  1184. {
  1185. int rate = ffs(hwpcmrate) - 1;
  1186. if (rate > ARRAY_SIZE(rates))
  1187. return 0;
  1188. return rates[rate];
  1189. }
  1190. EXPORT_SYMBOL_GPL(snd_soc_get_rate);
  1191. /**
  1192. * snd_soc_new_pcms - create new sound card and pcms
  1193. * @socdev: the SoC audio device
  1194. *
  1195. * Create a new sound card based upon the codec and interface pcms.
  1196. *
  1197. * Returns 0 for success, else error.
  1198. */
  1199. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char * xid)
  1200. {
  1201. struct snd_soc_codec *codec = socdev->codec;
  1202. struct snd_soc_machine *machine = socdev->machine;
  1203. int ret = 0, i;
  1204. mutex_lock(&codec->mutex);
  1205. /* register a sound card */
  1206. codec->card = snd_card_new(idx, xid, codec->owner, 0);
  1207. if (!codec->card) {
  1208. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  1209. codec->name);
  1210. mutex_unlock(&codec->mutex);
  1211. return -ENODEV;
  1212. }
  1213. codec->card->dev = socdev->dev;
  1214. codec->card->private_data = codec;
  1215. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  1216. /* create the pcms */
  1217. for(i = 0; i < machine->num_links; i++) {
  1218. ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
  1219. if (ret < 0) {
  1220. printk(KERN_ERR "asoc: can't create pcm %s\n",
  1221. machine->dai_link[i].stream_name);
  1222. mutex_unlock(&codec->mutex);
  1223. return ret;
  1224. }
  1225. }
  1226. mutex_unlock(&codec->mutex);
  1227. return ret;
  1228. }
  1229. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  1230. /**
  1231. * snd_soc_register_card - register sound card
  1232. * @socdev: the SoC audio device
  1233. *
  1234. * Register a SoC sound card. Also registers an AC97 device if the
  1235. * codec is AC97 for ad hoc devices.
  1236. *
  1237. * Returns 0 for success, else error.
  1238. */
  1239. int snd_soc_register_card(struct snd_soc_device *socdev)
  1240. {
  1241. struct snd_soc_codec *codec = socdev->codec;
  1242. struct snd_soc_machine *machine = socdev->machine;
  1243. int ret = 0, i, ac97 = 0;
  1244. mutex_lock(&codec->mutex);
  1245. for(i = 0; i < machine->num_links; i++) {
  1246. if (socdev->machine->dai_link[i].init)
  1247. socdev->machine->dai_link[i].init(codec);
  1248. if (socdev->machine->dai_link[i].cpu_dai->type == SND_SOC_DAI_AC97)
  1249. ac97 = 1;
  1250. }
  1251. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  1252. "%s", machine->name);
  1253. snprintf(codec->card->longname, sizeof(codec->card->longname),
  1254. "%s (%s)", machine->name, codec->name);
  1255. ret = snd_card_register(codec->card);
  1256. if (ret < 0) {
  1257. printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
  1258. codec->name);
  1259. mutex_unlock(&codec->mutex);
  1260. return ret;
  1261. }
  1262. #ifdef CONFIG_SND_SOC_AC97_BUS
  1263. if (ac97)
  1264. soc_ac97_dev_register(codec);
  1265. #endif
  1266. snd_soc_dapm_sys_add(socdev->dev);
  1267. device_create_file(socdev->dev, &dev_attr_codec_reg);
  1268. mutex_unlock(&codec->mutex);
  1269. return ret;
  1270. }
  1271. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  1272. /**
  1273. * snd_soc_free_pcms - free sound card and pcms
  1274. * @socdev: the SoC audio device
  1275. *
  1276. * Frees sound card and pcms associated with the socdev.
  1277. * Also unregister the codec if it is an AC97 device.
  1278. */
  1279. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  1280. {
  1281. struct snd_soc_codec *codec = socdev->codec;
  1282. mutex_lock(&codec->mutex);
  1283. #ifdef CONFIG_SND_SOC_AC97_BUS
  1284. if (codec->ac97)
  1285. soc_ac97_dev_unregister(codec);
  1286. #endif
  1287. if (codec->card)
  1288. snd_card_free(codec->card);
  1289. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1290. mutex_unlock(&codec->mutex);
  1291. }
  1292. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1293. /**
  1294. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1295. * @substream: the pcm substream
  1296. * @hw: the hardware parameters
  1297. *
  1298. * Sets the substream runtime hardware parameters.
  1299. */
  1300. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1301. const struct snd_pcm_hardware *hw)
  1302. {
  1303. struct snd_pcm_runtime *runtime = substream->runtime;
  1304. runtime->hw.info = hw->info;
  1305. runtime->hw.formats = hw->formats;
  1306. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1307. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1308. runtime->hw.periods_min = hw->periods_min;
  1309. runtime->hw.periods_max = hw->periods_max;
  1310. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1311. runtime->hw.fifo_size = hw->fifo_size;
  1312. return 0;
  1313. }
  1314. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1315. /**
  1316. * snd_soc_cnew - create new control
  1317. * @_template: control template
  1318. * @data: control private data
  1319. * @lnng_name: control long name
  1320. *
  1321. * Create a new mixer control from a template control.
  1322. *
  1323. * Returns 0 for success, else error.
  1324. */
  1325. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1326. void *data, char *long_name)
  1327. {
  1328. struct snd_kcontrol_new template;
  1329. memcpy(&template, _template, sizeof(template));
  1330. if (long_name)
  1331. template.name = long_name;
  1332. template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1333. template.index = 0;
  1334. return snd_ctl_new1(&template, data);
  1335. }
  1336. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1337. /**
  1338. * snd_soc_info_enum_double - enumerated double mixer info callback
  1339. * @kcontrol: mixer control
  1340. * @uinfo: control element information
  1341. *
  1342. * Callback to provide information about a double enumerated
  1343. * mixer control.
  1344. *
  1345. * Returns 0 for success.
  1346. */
  1347. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1348. struct snd_ctl_elem_info *uinfo)
  1349. {
  1350. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1351. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1352. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1353. uinfo->value.enumerated.items = e->mask;
  1354. if (uinfo->value.enumerated.item > e->mask - 1)
  1355. uinfo->value.enumerated.item = e->mask - 1;
  1356. strcpy(uinfo->value.enumerated.name,
  1357. e->texts[uinfo->value.enumerated.item]);
  1358. return 0;
  1359. }
  1360. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1361. /**
  1362. * snd_soc_get_enum_double - enumerated double mixer get callback
  1363. * @kcontrol: mixer control
  1364. * @uinfo: control element information
  1365. *
  1366. * Callback to get the value of a double enumerated mixer.
  1367. *
  1368. * Returns 0 for success.
  1369. */
  1370. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1371. struct snd_ctl_elem_value *ucontrol)
  1372. {
  1373. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1374. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1375. unsigned short val, bitmask;
  1376. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1377. ;
  1378. val = snd_soc_read(codec, e->reg);
  1379. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1380. if (e->shift_l != e->shift_r)
  1381. ucontrol->value.enumerated.item[1] =
  1382. (val >> e->shift_r) & (bitmask - 1);
  1383. return 0;
  1384. }
  1385. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1386. /**
  1387. * snd_soc_put_enum_double - enumerated double mixer put callback
  1388. * @kcontrol: mixer control
  1389. * @uinfo: control element information
  1390. *
  1391. * Callback to set the value of a double enumerated mixer.
  1392. *
  1393. * Returns 0 for success.
  1394. */
  1395. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1396. struct snd_ctl_elem_value *ucontrol)
  1397. {
  1398. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1399. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1400. unsigned short val;
  1401. unsigned short mask, bitmask;
  1402. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1403. ;
  1404. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  1405. return -EINVAL;
  1406. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1407. mask = (bitmask - 1) << e->shift_l;
  1408. if (e->shift_l != e->shift_r) {
  1409. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  1410. return -EINVAL;
  1411. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1412. mask |= (bitmask - 1) << e->shift_r;
  1413. }
  1414. return snd_soc_update_bits(codec, e->reg, mask, val);
  1415. }
  1416. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1417. /**
  1418. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1419. * @kcontrol: mixer control
  1420. * @uinfo: control element information
  1421. *
  1422. * Callback to provide information about an external enumerated
  1423. * single mixer.
  1424. *
  1425. * Returns 0 for success.
  1426. */
  1427. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1428. struct snd_ctl_elem_info *uinfo)
  1429. {
  1430. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1431. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1432. uinfo->count = 1;
  1433. uinfo->value.enumerated.items = e->mask;
  1434. if (uinfo->value.enumerated.item > e->mask - 1)
  1435. uinfo->value.enumerated.item = e->mask - 1;
  1436. strcpy(uinfo->value.enumerated.name,
  1437. e->texts[uinfo->value.enumerated.item]);
  1438. return 0;
  1439. }
  1440. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1441. /**
  1442. * snd_soc_info_volsw_ext - external single mixer info callback
  1443. * @kcontrol: mixer control
  1444. * @uinfo: control element information
  1445. *
  1446. * Callback to provide information about a single external mixer control.
  1447. *
  1448. * Returns 0 for success.
  1449. */
  1450. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1451. struct snd_ctl_elem_info *uinfo)
  1452. {
  1453. int mask = kcontrol->private_value;
  1454. uinfo->type =
  1455. mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1456. uinfo->count = 1;
  1457. uinfo->value.integer.min = 0;
  1458. uinfo->value.integer.max = mask;
  1459. return 0;
  1460. }
  1461. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1462. /**
  1463. * snd_soc_info_bool_ext - external single boolean mixer info callback
  1464. * @kcontrol: mixer control
  1465. * @uinfo: control element information
  1466. *
  1467. * Callback to provide information about a single boolean external mixer control.
  1468. *
  1469. * Returns 0 for success.
  1470. */
  1471. int snd_soc_info_bool_ext(struct snd_kcontrol *kcontrol,
  1472. struct snd_ctl_elem_info *uinfo)
  1473. {
  1474. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1475. uinfo->count = 1;
  1476. uinfo->value.integer.min = 0;
  1477. uinfo->value.integer.max = 1;
  1478. return 0;
  1479. }
  1480. EXPORT_SYMBOL_GPL(snd_soc_info_bool_ext);
  1481. /**
  1482. * snd_soc_info_volsw - single mixer info callback
  1483. * @kcontrol: mixer control
  1484. * @uinfo: control element information
  1485. *
  1486. * Callback to provide information about a single mixer control.
  1487. *
  1488. * Returns 0 for success.
  1489. */
  1490. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1491. struct snd_ctl_elem_info *uinfo)
  1492. {
  1493. int mask = (kcontrol->private_value >> 16) & 0xff;
  1494. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1495. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1496. uinfo->type =
  1497. mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1498. uinfo->count = shift == rshift ? 1 : 2;
  1499. uinfo->value.integer.min = 0;
  1500. uinfo->value.integer.max = mask;
  1501. return 0;
  1502. }
  1503. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1504. /**
  1505. * snd_soc_get_volsw - single mixer get callback
  1506. * @kcontrol: mixer control
  1507. * @uinfo: control element information
  1508. *
  1509. * Callback to get the value of a single mixer control.
  1510. *
  1511. * Returns 0 for success.
  1512. */
  1513. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1514. struct snd_ctl_elem_value *ucontrol)
  1515. {
  1516. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1517. int reg = kcontrol->private_value & 0xff;
  1518. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1519. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1520. int mask = (kcontrol->private_value >> 16) & 0xff;
  1521. int invert = (kcontrol->private_value >> 24) & 0x01;
  1522. ucontrol->value.integer.value[0] =
  1523. (snd_soc_read(codec, reg) >> shift) & mask;
  1524. if (shift != rshift)
  1525. ucontrol->value.integer.value[1] =
  1526. (snd_soc_read(codec, reg) >> rshift) & mask;
  1527. if (invert) {
  1528. ucontrol->value.integer.value[0] =
  1529. mask - ucontrol->value.integer.value[0];
  1530. if (shift != rshift)
  1531. ucontrol->value.integer.value[1] =
  1532. mask - ucontrol->value.integer.value[1];
  1533. }
  1534. return 0;
  1535. }
  1536. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1537. /**
  1538. * snd_soc_put_volsw - single mixer put callback
  1539. * @kcontrol: mixer control
  1540. * @uinfo: control element information
  1541. *
  1542. * Callback to set the value of a single mixer control.
  1543. *
  1544. * Returns 0 for success.
  1545. */
  1546. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1547. struct snd_ctl_elem_value *ucontrol)
  1548. {
  1549. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1550. int reg = kcontrol->private_value & 0xff;
  1551. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1552. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1553. int mask = (kcontrol->private_value >> 16) & 0xff;
  1554. int invert = (kcontrol->private_value >> 24) & 0x01;
  1555. int err;
  1556. unsigned short val, val2, val_mask;
  1557. val = (ucontrol->value.integer.value[0] & mask);
  1558. if (invert)
  1559. val = mask - val;
  1560. val_mask = mask << shift;
  1561. val = val << shift;
  1562. if (shift != rshift) {
  1563. val2 = (ucontrol->value.integer.value[1] & mask);
  1564. if (invert)
  1565. val2 = mask - val2;
  1566. val_mask |= mask << rshift;
  1567. val |= val2 << rshift;
  1568. }
  1569. err = snd_soc_update_bits(codec, reg, val_mask, val);
  1570. return err;
  1571. }
  1572. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1573. /**
  1574. * snd_soc_info_volsw_2r - double mixer info callback
  1575. * @kcontrol: mixer control
  1576. * @uinfo: control element information
  1577. *
  1578. * Callback to provide information about a double mixer control that
  1579. * spans 2 codec registers.
  1580. *
  1581. * Returns 0 for success.
  1582. */
  1583. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1584. struct snd_ctl_elem_info *uinfo)
  1585. {
  1586. int mask = (kcontrol->private_value >> 12) & 0xff;
  1587. uinfo->type =
  1588. mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1589. uinfo->count = 2;
  1590. uinfo->value.integer.min = 0;
  1591. uinfo->value.integer.max = mask;
  1592. return 0;
  1593. }
  1594. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1595. /**
  1596. * snd_soc_get_volsw_2r - double mixer get callback
  1597. * @kcontrol: mixer control
  1598. * @uinfo: control element information
  1599. *
  1600. * Callback to get the value of a double mixer control that spans 2 registers.
  1601. *
  1602. * Returns 0 for success.
  1603. */
  1604. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1605. struct snd_ctl_elem_value *ucontrol)
  1606. {
  1607. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1608. int reg = kcontrol->private_value & 0xff;
  1609. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1610. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1611. int mask = (kcontrol->private_value >> 12) & 0xff;
  1612. int invert = (kcontrol->private_value >> 20) & 0x01;
  1613. ucontrol->value.integer.value[0] =
  1614. (snd_soc_read(codec, reg) >> shift) & mask;
  1615. ucontrol->value.integer.value[1] =
  1616. (snd_soc_read(codec, reg2) >> shift) & mask;
  1617. if (invert) {
  1618. ucontrol->value.integer.value[0] =
  1619. mask - ucontrol->value.integer.value[0];
  1620. ucontrol->value.integer.value[1] =
  1621. mask - ucontrol->value.integer.value[1];
  1622. }
  1623. return 0;
  1624. }
  1625. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1626. /**
  1627. * snd_soc_put_volsw_2r - double mixer set callback
  1628. * @kcontrol: mixer control
  1629. * @uinfo: control element information
  1630. *
  1631. * Callback to set the value of a double mixer control that spans 2 registers.
  1632. *
  1633. * Returns 0 for success.
  1634. */
  1635. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1636. struct snd_ctl_elem_value *ucontrol)
  1637. {
  1638. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1639. int reg = kcontrol->private_value & 0xff;
  1640. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1641. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1642. int mask = (kcontrol->private_value >> 12) & 0xff;
  1643. int invert = (kcontrol->private_value >> 20) & 0x01;
  1644. int err;
  1645. unsigned short val, val2, val_mask;
  1646. val_mask = mask << shift;
  1647. val = (ucontrol->value.integer.value[0] & mask);
  1648. val2 = (ucontrol->value.integer.value[1] & mask);
  1649. if (invert) {
  1650. val = mask - val;
  1651. val2 = mask - val2;
  1652. }
  1653. val = val << shift;
  1654. val2 = val2 << shift;
  1655. if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
  1656. return err;
  1657. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  1658. return err;
  1659. }
  1660. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1661. static int __devinit snd_soc_init(void)
  1662. {
  1663. printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
  1664. return platform_driver_register(&soc_driver);
  1665. }
  1666. static void snd_soc_exit(void)
  1667. {
  1668. platform_driver_unregister(&soc_driver);
  1669. }
  1670. module_init(snd_soc_init);
  1671. module_exit(snd_soc_exit);
  1672. /* Module information */
  1673. MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
  1674. MODULE_DESCRIPTION("ALSA SoC Core");
  1675. MODULE_LICENSE("GPL");