soc-core.c 52 KB

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